CAPD RedHom Library
Mathematica API examples

Install capdRedHom package in Mathematica

To use Mathematica bindings you need to have compiled library. We recomend to use precompiled binaries (see Download). The bindings are located in <INSTALL_PATH>/usr/local/libexec/capdredhom/capdRedHomM-<VERSION>.zip. You can find source code of the bindings in capdRedHom/programs/apiRedHom-mathematica.
To install the bindings into Mathematica you can use our installator install.m:

(** Insert bellow a correct path to bindings or call from command line
    MathematicaScript -script install.m capdRedHomM.zip **)
capdRedHomPKG=$ScriptCommandLine[[2]];

appsDir = FileNameJoin[{$UserBaseDirectory, "Applications"}];
pkgDir = FileNameJoin[{appsDir, "capdRedHom"}]

If[FileExistsQ[pkgDir],
 DeleteDirectory[pkgDir, DeleteContents -> True]]

(** ExtractArchive[capdRedHomPKG, appsDir] does not work with symlinks !**)

Run["tar xzf", capdRedHomPKG,  "-C", appsDir];


Print["Installed " <> capdRedHomPKG <> " into " <> appsDir];
Print["Restart Mathematica!"];

You can call the installator from command line as follow: MathematicaScript -script install.m <INSTALL_PATH>/usr/local/libexec/capdredhom/capdRedHomM-<VERSION>.zip

To check if all run-time dependencies are satisfied you can load a shared library in Python using following snippet:

python -c "import sys; from ctypes import *; lib = cdll.LoadLibrary(sys.argv[1]);" PATH_TO_LIBRARY

where PATH_TO_LIBRARY is like

# on Linux
~/.Mathematica/Applications/capdRedHom/LibraryResources/Linux-x86-64/libcapdapiRedHom_mathematica.so
# on macOS
~/Library/Mathematica/Applications/capdRedHom/LibraryResources/MacOSX-x86-64/libcapdapiRedHom_mathematica.dylib

In case of error your system will show you a message.

Compute Homology in Mathematica

MathematicaScript -script simplicial_1.m

<< capdRedHom`

(** Complex from list of simplices **)
simplices = {{0, 1, 2}, {0, 1, 3}, {0, 4}, {1, 4}};
Print[RedHomSimplicialBettiNumbersOverZ[simplices]];
Print[RedHomSimplicialBettiNumbersOverZ2[simplices]];
Print[RedHomSimplicialBettiNumbersOverZp[simplices, 3]];

Print[RedHomSimplicialHomologyOverZ[simplices]];
Print[RedHomSimplicialHomologyOverZ2[simplices]];
Print[RedHomSimplicialHomologyOverZp[simplices, 3]];






(**Topological sphere.Points coordinates.**)
simplices = {{2, 3, 4}, {1, 2, 3}, {1, 3, 4}, {1, 2, 4}};
Print[RedHomSimplicialBettiNumbersOverZ[simplices]];

(**Visualization.We need to have points and create a simplex from an \
abstract simplex.**)

points = {{0, 0, 0}, {1, 0, 0}, {0, 2, 0}, {0, 0, 3}};
SimplexCoordinate[simplex_] :=
  Map[Function[p, points[[p]]], simplex];
Graphics3D[Polygon[Map[SimplexCoordinate, simplices]]]


MathematicaScript -script cubical_1.m

Point Cloud Persistent Homology in Mathematica

MathematicaScript -script point_cloud_persistence.m

<< capdRedHom`

(** Compute persistence homology from a point cloud. Arguments:
- a list of points in n-dimensional euclidean space
- max complex dimension
- max distance
 **)

Print[RedHomPointCloudPersistentHomology[{{.1, .2, .3}, {.0, .0, .0}}, 1, .1]];





(** Random points example with visualization **)
Needs["HierarchicalClustering`"];


numberOfPoints = 100;
points = Table[{RandomReal[1], RandomReal[1], RandomReal[1]}, {i, numberOfPoints}];
distanceMatrix = DistanceMatrix[points];
Distance[p_, q_] := distanceMatrix[[p]][[q]];
SimplexCoordinate[simplex_] := Map[Function[p, points[[p]]], simplex];

maxDistance = 0.3;
maxDimension = 2;
(** compute persistence intervals **)
persistenceIntervals =
  RedHomPointCloudPersistentHomology[points, maxDimension,
   maxDistance];
(** compute simplicial Rips complex, only for visualization **)
complex =  RipsComplex[Distance, Length[points], maxDistance, maxDimension];


distance = 0.1;
dimension = maxDimension;
(** create filtration based on the precomputed Rips complex **)
filteredSimplices =
  ComplexFiltration[Distance, complex, distance, dimension];
(** For the filtration we can compute regular homology **)
filteredComplex = RedHomCreateSimplicialComplex[Union @@ filteredSimplices];
betti = RedHomBettiNumbers[filteredComplex];
Print[betti];

plotDimension = 2;
plot = PlotSimplicialComplex[filteredSimplices, plotDimension,  SimplexCoordinate];
Show[plot]