Skip to content

Commit dd2668a

Browse files
sparse.gi
1 parent 7abdcb2 commit dd2668a

File tree

1 file changed

+75
-18
lines changed

1 file changed

+75
-18
lines changed

lib/Sparse/sparse.gi

Lines changed: 75 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,15 @@ local x;
7676

7777
#Multiplies the i-th row of a sparse matrix by k.
7878

79-
if IsZero(k) then M!.mat[i]:=[]; fi;
79+
if IsZero(k) then M!.mat[i]:=[];
80+
81+
else
8082

8183
for x in M!.mat[i] do
8284
x[2]:=k*x[2];
8385
od;
8486

87+
fi;
8588
end);
8689
####################################################
8790
####################################################
@@ -112,23 +115,28 @@ local x,z,r, pos;
112115

113116
#Adds k times the j-th row to the i-th row of a sparse matrix M.
114117

115-
Sort(M!.mat[i]); #Leave this in for safety. It should not be needed.
118+
if not IsZero(k) then
116119

117-
z:=List(M!.mat[i],a->a[1]);
120+
Sort(M!.mat[i]); #Leave this in for safety. It should not be needed.
118121

119-
for x in M!.mat[j] do
120-
pos:=PositionSet(z,x[1]);
121-
if pos=fail then
122-
Add(M!.mat[i],[x[1],k*x[2]]);
123-
else
124-
M!.mat[i][pos][2]:=M!.mat[i][pos][2]+k*x[2];
125-
fi;
126-
od;
122+
z:=List(M!.mat[i],a->a[1]);
127123

128-
M!.mat[i]:=Filtered(M!.mat[i],a->not IsZero(a[2]));
124+
for x in M!.mat[j] do
125+
pos:=PositionSet(z,x[1]);
126+
if pos=fail then
127+
Add(M!.mat[i],[x[1],k*x[2]]);
128+
else
129+
M!.mat[i][pos][2]:=M!.mat[i][pos][2]+k*x[2];
130+
if IsZero(M!.mat[i][pos][2]) then Unbind(M!.mat[i][pos][2]); fi;
131+
fi;
132+
od;
129133

130-
Sort(M!.mat[i]);
134+
#M!.mat[i]:=Filtered(M!.mat[i],a->not IsZero(a[2]));
135+
M!.mat[i]:=Filtered(M!.mat[i],a->IsBound(a));
136+
137+
Sort(M!.mat[i]);
131138

139+
fi;
132140
end);
133141
####################################################
134142
####################################################
@@ -160,15 +168,14 @@ if IsBound(M!.mat[i]) then
160168
if IsBound(M!.mat[i][1]) then
161169
if not IsBound(M!.heads[M!.mat[i][1][1]]) and not IsZero(M!.mat[i][1][2]) then
162170
M!.heads[M!.mat[i][1][1]]:=i;
163-
if not IsOne(M!.mat[i][1][2])then
171+
if not IsOne(M!.mat[i][1][2]) then
164172
SparseRowMult(M,i,M!.mat[i][1][2]^-1);
165173
fi;
166174
fi;
167175
fi;
168176
fi;
169177
od;
170178

171-
172179
for i in Difference([1..M!.rows],M!.heads) do
173180
SparseRowReduce(M,i);
174181
od;
@@ -217,16 +224,19 @@ if not IsSet(zz) then Print("List is not a set\n"); return fail; fi;
217224
for xx in M!.mat[r] do
218225
#pos:=Position(zz,xx[1]); #THIS IS BETTER ON SMALLER EXAMPLES
219226
pos:=PositionSet(zz,xx[1]); #THIS IS BETTER ON LARGER EXAMPLES
227+
220228
if pos=fail then
221229
Add(M!.mat[i],[xx[1],k*xx[2]]);
222230
else
223231
M!.mat[i][pos][2]:=M!.mat[i][pos][2]+k*xx[2];
224-
if IsZero(M!.mat[i][pos][2]) then M!.mat[i][pos][2]:=0; fi; #Apr 2024
232+
#if IsZero(M!.mat[i][pos][2]) then M!.mat[i][pos][2]:=0; fi; #Apr 2024
233+
if IsZero(M!.mat[i][pos][2]) then Unbind(M!.mat[i][pos]); fi; #Dec 2025
225234
fi;
235+
226236
od;
227-
#M!.mat[i]:=Filtered(M!.mat[i],a->not IsZero(a[2]));
228237

229-
M!.mat[i]:=Filtered(M!.mat[i],a->not a[2]=0); #Apr 24
238+
#M!.mat[i]:=Filtered(M!.mat[i],a-> not a[2]=0); #Apr 24
239+
M!.mat[i]:=Filtered(M!.mat[i],a->IsBound(a)); #Dec 2025
230240
Sort(M!.mat[i]);
231241
#############################################
232242
if Length(M!.mat[i])=0 then return true; fi;
@@ -238,6 +248,7 @@ if not IsOne(k) then
238248
SparseRowMult(M,i,k^-1);
239249
fi;
240250
M!.heads[first]:=i;
251+
241252
return true;
242253

243254

@@ -485,6 +496,52 @@ end);
485496
####################################################
486497
####################################################
487498

499+
####################################################
500+
####################################################
501+
InstallOtherMethod( \*,
502+
"for FFE and sparse matrix",
503+
[IsFFE, IsHapSparseMat],
504+
function( x, A )
505+
local p, B;
506+
p:=Characteristic(x);
507+
if A!.characteristic<>p and not A!.characteristic=0 then
508+
Print("Inconsistent field characteristics.\n");
509+
return fail;
510+
fi;
511+
B:=List(A!.mat,r->List(r,s->[s[1],x*s[2]]));
512+
return Objectify(HapSparseMat,
513+
rec( rows:=A!.rows,
514+
cols:=A!.cols,
515+
characteristic:=p,
516+
mat:=B));
517+
518+
end );
519+
####################################################
520+
####################################################
521+
522+
####################################################
523+
####################################################
524+
InstallOtherMethod( \*,
525+
"for FFE and sparse matrix",
526+
[IsHapSparseMat, IsFFE],
527+
function( A, x )
528+
local p, B;
529+
p:=Characteristic(x);
530+
if A!.characteristic<>p and not A!.characteristic=0 then
531+
Print("Inconsistent field characteristics.\n");
532+
return fail;
533+
fi;
534+
B:=List(A!.mat,r->List(r,s->[s[1],x*s[2]]));
535+
return Objectify(HapSparseMat,
536+
rec( rows:=A!.rows,
537+
cols:=A!.cols,
538+
characteristic:=p,
539+
mat:=B));
540+
541+
end );
542+
####################################################
543+
####################################################
544+
488545

489546

490547

0 commit comments

Comments
 (0)