/usr/share/gap/lib/matobj.gi is in gap-libs 4r8p8-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | ############################################################################
#
# matobj.gi
# by Max Neunhöffer
#
## Copyright (C) 2007 Max Neunhöffer, University of St Andrews
## This file is free software, see license information at the end.
#
# This file contains some generic methods for the vector/matrix interface
# defined in matobj1.gd and matobj2.gd.
#
############################################################################
InstallMethod( WeightOfVector, "generic method",
[IsRowVectorObj],
function(v)
local i,n;
n := 0;
for i in [1..Length(v)] do
if not(IsZero(v[i])) then
n := n + 1;
fi;
od;
return n;
end );
InstallMethod( DistanceOfVectors, "generic method",
[IsRowVectorObj, IsRowVectorObj],
function( v, w)
local i,n;
if Length(v) <> Length(w) then
Error("vectors must have same length");
return fail;
fi;
n := 0;
for i in [1..Length(v)] do
if v[i] <> w[i] then
n := n + 1;
fi;
od;
return n;
end );
InstallMethod( AddMatrix, "for two row list matrices and a scalar",
[ IsMutable and IsRowListMatrix, IsRowListMatrix, IsMultiplicativeElement ],
function( A, B, s )
local i;
if Length(A) <> Length(B) then
Error("Matrices must have equal length");
return;
fi;
for i in [1..Length(A)] do
AddRowVector(A[i],B[i],s);
od;
end );
InstallMethod( AddMatrix, "for two row list matrices",
[ IsMutable and IsRowListMatrix, IsRowListMatrix ],
function( A, B )
local i;
if Length(A) <> Length(B) then
Error("Matrices must have equal length");
return;
fi;
for i in [1..Length(A)] do
AddRowVector(A[i],B[i]);
od;
end );
InstallMethod( MultMatrix, "for a row list matrix",
[ IsMutable and IsRowListMatrix, IsMultiplicativeElement ],
function( A, s )
local i;
for i in [1..Length(A)] do
MultRowVector(A[i],s);
od;
end );
InstallMethod( ProductTransposedMatMat, "generic method",
[ IsMatrixObj, IsMatrixObj ],
function( A, B )
return TransposedMat(A) * B;
end );
InstallMethod( Matrix, "generic convenience method with 2 args",
[IsList,IsMatrixObj],
function( l, m )
if Length(l) = 0 then
Error("Matrix: two-argument version not allowed with empty first arg");
return;
fi;
if not(IsList(l[1]) or IsRowVectorObj(l[1])) then
Error("Matrix: flat data not supported in two-argument version");
return;
fi;
return Matrix(l,Length(l[1]),m);
end );
InstallMethod( Unfold, "for a matrix object, and a vector object",
[ IsMatrixObj, IsRowVectorObj ],
function( m, w )
local v,i,l;
if Length(m) = 0 then
return ZeroVector(0,w);
else
l := RowLength(m);
v := ZeroVector(Length(m)*l,w);
for i in [1..Length(m)] do
CopySubVector( m[i], v, [1..l], [(i-1)*l+1..i*l] );
od;
return v;
fi;
end );
InstallMethod( Fold, "for a vector, a positive int, and a matrix",
[ IsRowVectorObj, IsPosInt, IsMatrixObj ],
function( v, rl, t )
local rows,i,tt,m;
m := Matrix([],rl,t);
tt := ZeroVector(rl,v);
for i in [1..Length(v)/rl] do
CopySubVector(v,tt,[(i-1)*rl+1..i*rl],[1..rl]);
Add(m,ShallowCopy(tt));
od;
return m;
end );
InstallMethod( CompanionMatrix, "for a polynomial and a matrix",
[ IsUnivariatePolynomial, IsMatrixObj ],
function( po, m )
local l, n, q, ll, i, bd, one;
bd := BaseDomain(m);
one := One(bd);
l := CoefficientsOfUnivariatePolynomial(po);
n := Length(l)-1;
if not(IsOne(l[n+1])) then
Error("CompanionMatrix: polynomial is not monic");
return fail;
fi;
l := Vector(-l{[1..n]},CompatibleVector(m));
ll := 0*[1..n];
ll[n] := l;
for i in [1..n-1] do
ll[i] := ZeroMutable(l);
ll[i][i+1] := one;
od;
return Matrix(ll,n,m);
end );
InstallMethod( KroneckerProduct, "for two matrices",
[ IsMatrixObj, IsMatrixObj ],
function( A, B )
local rowsA, rowsB, colsA, colsB, newclass, AxB, i, j;
if not(IsIdenticalObj(BaseDomain(A),BaseDomain(B))) then
Error("KroneckerProduct: Matrices not over same base domain");
return;
fi;
rowsA := Length(A);
colsA := RowLength(A);
rowsB := Length(B);
colsB := RowLength(B);
AxB := ZeroMatrix( rowsA * rowsB, colsA * colsB, A );
# Cache matrices
# not implemented yet
for i in [1..rowsA] do
for j in [1..colsA] do
CopySubMatrix( A[i][j] * B, AxB,
[ 1 .. rowsB ], [ rowsB * (i-1) + 1 .. rowsB * i ],
[ 1 .. colsB ], [ (j-1) * colsB + 1 .. j * colsB ] );
od;
od;
if not(IsMutable(A)) and not(IsMutable(B)) then
MakeImmutable(AxB);
fi;
return AxB;
end );
InstallGlobalFunction( ConcatenationOfVectors,
function( arg )
local i,len,pos,res,total;
if Length( arg ) = 1 and IsList( arg[1] ) then
arg := arg[1];
fi;
if Length(arg) = 0 then
Error("must have at least one vector to concatenate");
fi;
total := Sum(arg,Length);
res := ZeroVector(total,arg[1]);
pos := 1;
for i in [1..Length(arg)] do
len := Length(arg[i]);
CopySubVector(arg[i],res,[1..len],[pos..pos+len-1]);
pos := pos + len;
od;
return res;
end );
InstallGlobalFunction( MakeVector,
function( arg )
local bd,l,ty;
if Length(arg) = 1 then
l := arg[1];
bd := DefaultField(l);
elif Length(arg) <> 2 then
Error("usage: MakeVector( <list> [,<basedomain>] )");
return fail;
else
l := arg[1];
bd := arg[2];
fi;
if IsFinite(bd) and IsField(bd) and Size(bd) = 2 then
ty := IsGF2VectorRep;
elif IsFinite(bd) and IsField(bd) and Size(bd) <= 256 then
ty := Is8BitVectorRep;
else
ty := IsPlistVectorRep;
fi;
return NewRowVector(ty,bd,l);
end );
InstallGlobalFunction( MakeMatrix,
function( arg )
local bd,l,len,rowlen,ty;
if Length(arg) = 1 then
l := arg[1];
bd := DefaultFieldOfMatrix(l);
elif Length(arg) <> 2 then
Error("usage: MakeVector( <list> [,<basedomain>] )");
return fail;
else
l := arg[1];
bd := arg[2];
fi;
len := Length(l);
if len = 0 then
Error("does not work for matrices with zero rows");
return fail;
fi;
rowlen := Length(l[1]);
if IsFinite(bd) and IsField(bd) and Size(bd) = 2 then
ty := IsGF2MatrixRep;
elif IsFinite(bd) and IsField(bd) and Size(bd) <= 256 then
ty := Is8BitMatrixRep;
else
ty := IsPlistMatrixRep;
fi;
return NewMatrix(ty,bd,rowlen,l);
end );
InstallMethod( ExtractSubVector, "generic method",
[ IsRowVectorObj, IsList ],
function( v, l )
return v{l};
end );
InstallOtherMethod( ScalarProduct, "generic method",
[ IsRowVectorObj, IsRowVectorObj ],
function( v, w )
local bd,i,s;
bd := BaseDomain(v);
s := Zero(bd);
if Length(v) <> Length(w) then
Error("vectors must have equal length");
return fail;
fi;
for i in [1..Length(v)] do
s := s + v[i]*w[i];
od;
return s;
end );
InstallMethod( TraceMat, "generic method",
[ IsMatrixObj ],
function( m )
local bd,i,s;
bd := BaseDomain(m);
s := Zero(bd);
if Length(m) <> RowLength(m) then
Error("matrix must be square");
return fail;
fi;
for i in [1..Length(m)] do
s := s + MatElm(m,i,i);
od;
return s;
end );
|