1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
|
#(C) Graham Ellis
####################################################
####################################################
InstallGlobalFunction(SparseMattoMat,
function(M)
local A,i, x;
#Inputs a soarse matrix M and returns a matrix
A:=NullMat(M!.rows,M!.cols);
for i in [1..M!.rows] do
if IsBound(M!.mat[i]) then
for x in M!.mat[i] do
A[i][x[1]]:=A[i][x[1]]+x[2];
od;
fi;
od;
if M!.characteristic>0 then
A:=A*One(GF(M!.characteristic));
fi;
return A;
end);
####################################################
####################################################
####################################################
####################################################
InstallGlobalFunction(SparseMat,
function(M)
local S, Rows, Cols, char, i, j;
#Inputs a matrix M and returns a sparse matrix
Rows:=Length(M);
if Rows=0 then Cols:=0;
else
Cols:=Length(M[1]);;
fi;
if Rows=0 then char:=0;
else
char:=Characteristic(M[1][1]);
fi;
S:=List([1..Rows],i->[]);
for i in [1..Rows] do
for j in [1..Cols] do
if not IsZero(M[i][j]) then
Add(S[i],[j,M[i][j]]);
fi;
od;
od;
return Objectify(HapSparseMat,
rec( rows:=Rows,
cols:=Cols,
characteristic:=char,
mat:=S));
end);
####################################################
####################################################
####################################################
####################################################
InstallGlobalFunction(SparseRowMult,
function(M,i,k)
local x;
#Multiplies the i-th row of a sparse matrix by k.
if IsZero(k) then M!.mat[i]:=[]; fi;
for x in M!.mat[i] do
x[2]:=k*x[2];
od;
end);
####################################################
####################################################
####################################################
####################################################
InstallGlobalFunction(SparseRowInterchange,
function(M,i,j)
local x;
#Interchanges the i-th and j-th rows of a sparse matrix M.
x:=M!.mat[i];
M!.mat[i]:=M!.mat[j];
M!.mat[j]:=x;
end);
####################################################
####################################################
####################################################
####################################################
InstallGlobalFunction(SparseRowAdd,
function(M,i,j,k)
local x,z,r, pos;
#Adds k times the j-th row to the i-th row of a sparse matrix M.
Sort(M!.mat[i]); #Leave this in for safety. It should not be needed.
z:=List(M!.mat[i],a->a[1]);
for x in M!.mat[j] do
pos:=PositionSet(z,x[1]);
if pos=fail then
Add(M!.mat[i],[x[1],k*x[2]]);
else
M!.mat[i][pos][2]:=M!.mat[i][pos][2]+k*x[2];
fi;
od;
M!.mat[i]:=Filtered(M!.mat[i],a->not IsZero(a[2]));
Sort(M!.mat[i]);
end);
####################################################
####################################################
####################################################
####################################################
InstallGlobalFunction(SparseSemiEchelon,
function(M)
local i, V;
if not IsHapSparseMat(M) then
Print("This function must be applied to a HAP sparse matrix.\n");
return fail; fi;
if not IsBound(M!.heads) then
M!.heads:=[]; ;
# i = M!.heads[j] is the number of the row whose first non-zero entry
# is in column j.
V:=[1..M!.rows];
for i in V do
if IsBound(M!.mat[i]) then
if not IsSSortedList(M!.mat[i]) then
Sort(M!.mat[i]);
fi;
if IsBound(M!.mat[i][1]) then
if not IsBound(M!.heads[M!.mat[i][1][1]]) and not IsZero(M!.mat[i][1][2]) then
M!.heads[M!.mat[i][1][1]]:=i;
if not IsOne(M!.mat[i][1][2])then
SparseRowMult(M,i,M!.mat[i][1][2]^-1);
fi;
fi;
fi;
fi;
od;
for i in Difference([1..M!.rows],M!.heads) do
SparseRowReduce(M,i);
od;
for i in [1..M!.rows] do
if IsBound(M!.mat[i]) then
M!.mat[i]:=Filtered(M!.mat[i],x->not x[2]=0);
if Length(M!.mat[i])=0 then Unbind(M!.mat[i]); fi;
fi;
od;
fi;
for i in [1..M!.cols] do ## Added Nov 2016
if not IsBound(M!.heads[i]) then ##
M!.heads[i]:=0; ##
fi; ##
od; ##
end);
####################################################
####################################################
####################################################
####################################################
InstallGlobalFunction(SparseRowReduce,
function(M,i)
local x, first, r,k, zz, xx, pos;
#Row reduce the i-th row of a sparse matrix if it is non-empty.
#The first non-zero entry of the reduced row will be 1.
#Return true if the matrix is modified, and false otherwise.
if not IsBound(M!.mat[i]) then return false; fi;
if Length(M!.mat[i])=0 then return false; fi;
first:=M!.mat[i][1][1];
while IsBound(M!.heads[first]) do
r:=M!.heads[first];
k:=-M!.mat[i][1][2];
###SparseRowAdd(M,i,r,k); #THIS TAKES ALL THE TIME
#############################################
zz:=List(M!.mat[i],a->a[1]);
for xx in M!.mat[r] do
pos:=Position(zz,xx[1]); #THIS IS BETTER ON SMALLER EXAMPLES
#pos:=PositionSet(zz,xx[1]); #THIS IS BETTER ON LARGER EXAMPLES
if pos=fail then
Add(M!.mat[i],[xx[1],k*xx[2]]);
else
M!.mat[i][pos][2]:=M!.mat[i][pos][2]+k*xx[2];
if IsZero(M!.mat[i][pos][2]) then M!.mat[i][pos][2]:=0; fi; #Apr 2024
fi;
od;
#M!.mat[i]:=Filtered(M!.mat[i],a->not IsZero(a[2]));
M!.mat[i]:=Filtered(M!.mat[i],a->not a[2]=0); #Apr 24
Sort(M!.mat[i]);
#############################################
if Length(M!.mat[i])=0 then return true; fi;
first:=M!.mat[i][1][1];
od;
k:=M!.mat[i][1][2];
if not IsOne(k) then
SparseRowMult(M,i,k^-1);
fi;
M!.heads[first]:=i;
return true;
end);
####################################################
####################################################
##########################################################
##########################################################
InstallGlobalFunction(SparseBoundaryMatrix,
function(C,n)
local
M,i;
M:=[1..C!.dimension(n)];
for i in [1..C!.dimension(n)] do
M[i]:=C!.boundary(n,i);
od;
return Objectify(HapSparseMat,
rec( rows:=C!.dimension(n),
cols:=C!.dimension(n-1),
characteristic:=EvaluateProperty(C,"characteristic"),
mat:=M));
end);
##########################################################
##########################################################
##########################################################
##########################################################
InstallGlobalFunction(TransposeOfSparseMat,
function(A)
local
M, entry, r;
M:=List([1..A!.cols],i->[]);
for r in [1..A!.rows] do
for entry in A!.mat[r] do
Add(M[entry[1]],[r,entry[2]]);
od;od;
return Objectify(HapSparseMat,
rec( rows:=A!.cols,
cols:=A!.rows,
characteristic:=EvaluateProperty(A,"characteristic"),
mat:=M));
end);
##########################################################
##########################################################
#####################################################################
#####################################################################
InstallOtherMethod(RankMatDestructive,
"Rank of a sparse matrix",
[IsHapSparseMat],
function(M) local T;
if M!.cols>=M!.rows then
SparseSemiEchelon(M);
#return Length(Flat(M!.heads));
return Length(Filtered(Flat(M!.heads),u->not IsZero(u)));
else
T:=TransposeOfSparseMat(M);
SparseSemiEchelon(T);
return Length(Filtered(Flat(T!.heads),u->not IsZero(u)));
fi;
end);
#####################################################################
#####################################################################
#####################################################################
#####################################################################
InstallOtherMethod(Rank,
"Rank of a sparse matrix",
[IsHapSparseMat],
function(M)
local mat, rk, tog, heads;
mat:=StructuralCopy(M!.mat);
if IsBound(M!.heads) then tog:=true; heads:=StructuralCopy(M!.heads); else tog:=false; fi;
SparseSemiEchelon(M);
#rk:= Length(Flat(M!.heads));
rk:= Length(Filtered(Flat(M!.heads),u->not IsZero(u)));
if tog then M!.heads:=heads; else Unbind(M!.heads); fi;
M!.mat:=mat;
return rk;
end);
#####################################################################
#####################################################################
#####################################################################
#####################################################################
InstallOtherMethod(Bettinumbers,
"Betti numbers of a sparse chain complex",
[IsHapSparseChainComplex,IsInt],
function(C,n)
local B,betti;
if n<0 then return 0; fi;
if n=0 then betti:=C!.dimension(n);
else
B:=SparseBoundaryMatrix(C,n);
betti:=C!.dimension(n)-RankMatDestructive(B);
fi;
B:=SparseBoundaryMatrix(C,n+1);
betti:=betti-RankMatDestructive(B);
return betti;
end);
#####################################################################
#####################################################################
#####################################################################
#####################################################################
InstallGlobalFunction(NullspaceSparseMatDestructive,
function(B)
local NS, heads, free, vec, revcol, pos, i,v,w, x,y,z,h,s;
NS:=[];
if not IsBound(B!.heads) then
SparseSemiEchelon(B);
fi;
heads:=Filtered([1..B!.cols],x->IsBound(B!.heads[x]));
free:=Difference([1..B!.cols],heads);
revcol:=Reversed([1..B!.cols]);
for x in free do
v:=[];
Add(v,[x,1]);
for y in revcol do
if y in heads then
h:=B!.mat[B!.heads[y]];
s:=0;
for z in h do
pos:=PositionProperty(v,a->a[1]=z[1]);
if IsInt(pos) then s:=s+z[2]*v[pos][2]; fi;
od;
if not s=0 then Add(v,[y,-s]); fi;
fi;
od;
Add(NS,v);
od;
return NS;
end);
#####################################################################
#####################################################################
###############################################################
###############################################################
InstallGlobalFunction(ReverseSparseMat,
function(B)
local i,x,d;
d:=B!.cols+1;
for i in [1..B!.rows] do
if IsBound(B!.mat[i]) then
for x in B!.mat[i] do
x[1]:=d-x[1];
od;
fi;
od;
if IsBound(B!.heads) then Unbind(B!.heads); fi;
end);
###############################################################
###############################################################
|