Skip to content

Commit

Permalink
persistent betti numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
grahamknockillaree committed Mar 13, 2024
1 parent db403d8 commit 33f145f
Show file tree
Hide file tree
Showing 16 changed files with 341 additions and 47 deletions.
30 changes: 26 additions & 4 deletions lib/Homology/new_persistent.gi
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#RT:=0;

###################################################
###################################################
InstallGlobalFunction(PersistentBettiNumbersViaContractions,
function(FF,n,p)
local F, W, L, E, chnmap, cwmap, map, ln, k, f, homs;
function(FF,N,p)
local n,F, W, L, E, chnmap, cwmap, map, ln, k, f, homs;

if not (IsInt(N) or IsList(N)) then return fail; fi;
if IsInt(N) then n:=N; fi;
#F:=ContractedFilteredRegularCWComplex(FF);
F:=FF;
ln:=EvaluateProperty(F,"filtration_length");
Expand All @@ -24,19 +27,38 @@ cwmap:=Objectify( HapRegularCWMap,
Add(L,ChainMapOfRegularCWMap(cwmap));
od;


E:=[];
for k in [1..ln] do
Add(E,ChainComplexEquivalenceOfRegularCWComplex(W[k]));
od;


if IsInt(N) then
homs:=[];
for k in [1..ln-1] do
f:=Compose( E[k+1][1], Compose(L[k],E[k][2]) );
Add(homs,HomologyVectorSpace(TensorWithIntegersModP(f,p),n));
od;
return LinearHomomorphismsPersistenceMat(homs);
fi;

homs:=[];
for n in N do
homs[n+1]:=[];
od;

N:=SortedList(N);

for k in [1..ln-1] do
f:=Compose( E[k+1][1], Compose(L[k],E[k][2]) );
for n in N do
#RT:=0-Runtime();
Add(homs[n+1],HomologyVectorSpace(TensorWithIntegersModP(f,p),n));
#RT:=RT+Runtime(); Print([k,n,RT],"\n");
od;
od;

homs:=List(N,n-> LinearHomomorphismsPersistenceMat(homs[n+1]) );
return homs;
end);
###################################################
###################################################
Expand Down
100 changes: 98 additions & 2 deletions lib/Homology/persistent.gi
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ local
LM,deg,prime,
M,triv,ChnCmps,vsmaps,A,S,TS,TM,L,
bool,x,y,i,F,PC;

Print("This function is unreliable, slow and should not be used.\n");
##Input###################
LM:=arg[1];
deg:=arg[2];
Expand Down Expand Up @@ -488,7 +488,7 @@ function(arg)
local
M,Bettis,triv,Persist,deg,prime,TM,L,SL,bool,CollapseMat,
Collapses_dim2,Fun,x,y;

Print("This function is unreliable, slow and should not be used.\n");
##Input###################
deg:=arg[2];
prime:=arg[3];
Expand Down Expand Up @@ -746,6 +746,10 @@ Disp:=DISPLAY_PATH;
fi;
###############

if IsMatrix(P[1]) then
HAP_BarCodeCompactDisplayList(P);
else

B:=BarCode(P);
B:=Collected(B);

Expand Down Expand Up @@ -791,6 +795,95 @@ od;

AppendTo(barcodedot,"}");

Exec(Concatenation("dot -Tgif ",barcodedot ,">", barcodegif));
Exec(Concatenation(Disp," ",barcodegif));
Sleep(2);
Exec(Concatenation("rm -r ",barcodegif{[1..Length(barcodegif)-9]}));
fi;
end);
#################################################################
#################################################################

###########################################################
###########################################################
InstallGlobalFunction(HAP_BarCodeCompactDisplayList,
function(arg)
local cnt,Q,BB,P,B,i,j,Disp,tmpDir,barcodedot,barcodegif, lb, AppendTo, PrintTo;
#SHOULD MERGE THIS INTO PRECEDING FUNCTION
AppendTo:=HAP_AppendTo;
PrintTo:=HAP_PrintTo;

###############
P:=arg[1];
if Length(arg)=2 then
Disp:=arg[2];
else
Disp:=DISPLAY_PATH;
fi;
###############

B:=[];

cnt:=-1;;
for Q in P do
cnt:=cnt+1;
BB:=BarCode(Q);
BB:=Collected(BB);
Append(B,BB);
i:=[1..Length(B[1][1])]*0;;
j:=[1..Length(B[1][1])]*0;;
Add(B,[i,Concatenation("Betti ", String(cnt))]);
Add(B,[j,0]);
od;

tmpDir:=DirectoryTemporary();
barcodedot:=Filename(tmpDir,"tmpIn.log");
barcodegif:=Filename(tmpDir,"basic.gif");


PrintTo(barcodedot,"digraph finite_state_machine {\n\n");
AppendTo(barcodedot, "rankdir=LR;\n\n");
AppendTo(barcodedot,
"node [style=filled,shape=point]\n\n");

lb:=Length(B[1][1]);
for i in [1..Length(B)] do
for j in [1..Length(B[1][1])] do
if B[i][1][j]>0 then
AppendTo(barcodedot,"node [color=black,fontcolor=black];\n",i,".",j,"\n");
else
AppendTo(barcodedot,"node [color=white,fontcolor=white];\n",i,".",j,"\n");
fi;
od;
od;


for i in [1..Length(B)] do

if IsString(B[i][2]) then
AppendTo(barcodedot,i,".",1,"->",i,".",1+1," [label =\"",B[i][2]," \",color=white,arrowhead=none];\n");
fi;

for j in [1..Length(B[1][1])-1] do

if B[i][1][j]>0 and B[i][1][j+1]>0 then
if j=Position(B[i][1],1) or (Position(B[i][1],1)=lb and j=lb-1) then
AppendTo(barcodedot,i,".",j,"->",i,".",j+1," [label =\"",B[i][2]," \",color=black,arrowhead=none];\n");
else
AppendTo(barcodedot,i,".",j,"->",i,".",j+1," [color=black,arrowhead=none];\n");
fi;
else
if j=Position(B[i][1],1) or (Position(B[i][1],1)=lb and j=lb-1) then
AppendTo(barcodedot,i,".",j,"->",i,".",j+1," [label =\"",B[i][2]," \",color=white,arrowhead=none];\n");
else
AppendTo(barcodedot,i,".",j,"->",i,".",j+1," [color=white,arrowhead=none];\n");
fi;
fi;
od;
od;

AppendTo(barcodedot,"}");

Exec(Concatenation("dot -Tgif ",barcodedot ,">", barcodegif));
Exec(Concatenation(Disp," ",barcodegif));
Sleep(2);
Expand Down Expand Up @@ -1008,6 +1101,7 @@ local
ChnCmps,vsmaps,L,
U,CU,V,CV,W,CW,
x,y,i,F,PC,correction;
Print("This function is unreliable, slow and should not be used.\n");

##Input###################
LM:=arg[1];
Expand Down Expand Up @@ -1096,6 +1190,7 @@ end);
InstallGlobalFunction(PersistentHomologyOfFilteredPureCubicalComplex,
function(F,n)
local FF, C, D;
Print("This function is unreliable, slow and should not be used.\n");

############################
if n=0 and IsHapFilteredPureCubicalComplex(F) then
Expand Down Expand Up @@ -1131,6 +1226,7 @@ InstallGlobalFunction(PersistentHomologyOfFilteredPureCubicalComplex_alt,
function(FF,deg)
local flen, HEMin, HEMax, FT, PH, D, PHfirst, Pmat, PB, F, r, s,n;

Print("This function is unreliable, slow and should not be used.\n");
#############################
if IsBound(FF!.persistentHomology) then
if IsBound(FF!.persistentHomology[deg+1]) then
Expand Down
25 changes: 24 additions & 1 deletion lib/Manifolds/manifolds.gi
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,29 @@ end);
#####################################################
#####################################################

#####################################################
#####################################################
InstallOtherMethod(ClosedSurface,
"simplicial surface or genus +/- g (where - means nonorientable)",
[IsInt,IsString],
function(n,str)
local Y;
if str="CW" then
Y:=ClosedSurface(n);
Y:=RegularCWComplex(Y);;
Y:=BarycentricallySimplifiedComplex(Y);
return Y;
fi;
if str="Simplicial" then
Y:=ClosedSurface(n);
return Y;
fi;
Print("The second argument should be \"CW\" or \"Simplicial\".\n");
return fail;
end);
#####################################################
#####################################################


#####################################################
#####################################################
Expand Down Expand Up @@ -194,7 +217,7 @@ Print("The simplicial manifolds must have the same dimension.\n");
return fail;
fi;

e:=arg[3];
e:=-arg[3];
maxK:=1*K!.simplicesLst[Dimension(K)+1];
maxL:=1*L!.simplicesLst[Dimension(L)+1];

Expand Down
56 changes: 56 additions & 0 deletions lib/Operations/hapOps.gi
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,10 @@ InstallOtherMethod(PersistentBettiNumbers,
function(Y,n)
local W;

if n=0 and EvaluateProperty(Y,"monotonic")=true then
return HAP_BettiZeroMonotonic(Y);
fi;

W:=ContractedFilteredPureCubicalComplex(Y);
W:= FilteredPureCubicalComplexToCubicalComplex(W);
W:=FilteredCubicalComplexToFilteredRegularCWComplex(W);
Expand All @@ -1030,6 +1034,10 @@ InstallOtherMethod(PersistentBettiNumbers,
[IsHapFilteredPureCubicalComplex,IsInt,IsInt],
function(Y,n,prime)
local W;
if n=0 and EvaluateProperty(Y,"monotonic")=true then
return HAP_BettiZeroMonotonic(Y);
fi;

W:=ContractedFilteredPureCubicalComplex(Y);
W:= FilteredPureCubicalComplexToCubicalComplex(W);
W:=FilteredCubicalComplexToFilteredRegularCWComplex(W);
Expand All @@ -1045,6 +1053,10 @@ InstallOtherMethod(PersistentBettiNumbersAlt,
[IsHapFilteredPureCubicalComplex,IsInt,IsInt],
function(Y,n,prime)
local W;
if n=0 and EvaluateProperty(Y,"monotonic")=true then
return HAP_BettiZeroMonotonic(Y);
fi;

W:=ContractedFilteredPureCubicalComplex(Y);
W:= FilteredPureCubicalComplexToCubicalComplex(W);
W:=FilteredCubicalComplexToFilteredRegularCWComplex(W);
Expand All @@ -1053,6 +1065,8 @@ end);
##########################################################
##########################################################



##########################################################
##########################################################
InstallOtherMethod(PersistentBettiNumbersAlt,
Expand All @@ -1061,6 +1075,10 @@ InstallOtherMethod(PersistentBettiNumbersAlt,
function(Y,n)
local W;

if n=0 and EvaluateProperty(Y,"monotonic")=true then
return HAP_BettiZeroMonotonic(Y);
fi;

W:=ContractedFilteredPureCubicalComplex(Y);
W:= FilteredPureCubicalComplexToCubicalComplex(W);
W:=FilteredCubicalComplexToFilteredRegularCWComplex(W);
Expand All @@ -1069,6 +1087,31 @@ end);
##########################################################
##########################################################

##########################################################
##########################################################
InstallOtherMethod(PersistentBettiNumbersAlt,
"Betti number of a filtered pure cubical complex",
[IsHapFilteredPureCubicalComplex,IsList],
function(Y,N)
local W,P, NN;

W:=ContractedFilteredPureCubicalComplex(Y);

if 0 in N and EvaluateProperty(Y,"monotonic")=true then
P:=[ HAP_BettiZeroMonotonic(Y) ];
NN:=Filtered(N,i->not i=0);
Append(P,PersistentBettiNumbersAlt(W,NN));
return P;
fi;

W:= FilteredPureCubicalComplexToCubicalComplex(W);
W:=FilteredCubicalComplexToFilteredRegularCWComplex(W);
return PersistentBettiNumbersAlt(W,N);
end);
##########################################################
##########################################################





Expand Down Expand Up @@ -1182,6 +1225,19 @@ end);
##########################################################
##########################################################

##########################################################
##########################################################
InstallOtherMethod(PersistentBettiNumbersAlt,
"Betti number of a filtered regular CW-complex",
[IsHapFilteredRegularCWComplex,IsList],
function(Y,N);
Print("Using homology over GF(2).\n");
return PersistentBettiNumbersViaContractions(Y,N,2);
end);
##########################################################
##########################################################





Expand Down
Loading

0 comments on commit 33f145f

Please sign in to comment.