GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it
1[1X3 [33X[0;0YThe Sparse Matrix Data Type[133X[101X234[1X3.1 [33X[0;0YThe inner workings of [5XGauss[105X[101X[1X sparse matrices[133X[101X56[33X[0;0YWhen doing any kind of computation there is a constant conflict between7memory load and speed. On the one hand, memory usage is bounded by the total8available memory, on the other hand, computation time should also not exceed9certain proportions. Memory usage and CPU time are generally inversely10proportional, because the computer needs more time to perform operations on11a compactified data structure. The idea of sparse matrices mirrors exactly12the need for less memory load, therefore it is natural that sparse13algorithms take more time than dense ones. However, if the matrix is14sufficiently large and sparse at the same time, sparse algorithms can easily15be faster than dense ones while maintaining minimal memory load.[133X1617[33X[0;0YIt should be noted that, although matrices that appear naturally in18homological algebra are almost always sparse, they do not have to stay19sparse under (R)REF algorithms, especially when the computation is concerned20with transformation matrices. Therefore, in a perfect world there should be21ways implemented to not only find out which data structure to use, but also22at what point to convert from one to the other. This was, however, not the23aim of the [5XGauss[105X package and is just one of many points in which this24package could be optimized or extended. Take a look at this matrix [22XM[122X:[133X2526┌── ─ ─ ─ ──┐27│ 0 0 2 9 0 │28│ 0 5 0 0 0 │29│ 0 0 0 1 0 │30└── ─ ─ ─ ──┘3132[33X[0;0YThe matrix [22XM[122X carries the same information as the following table, if and33only if you know how many rows and columns the matrix has. There is also the34matter of the base ring, but this is not important for now:[133X3536┌────── ──────┐37│ (i,j) Entry │38├────── ──────┤39│ (1,3) 2 │40│ (1,4) 9 │41│ (2,2) 5 │42│ (3,4) 1 │43└────── ──────┘4445[33X[0;0YThis table relates each index tuple to its nonzero entry, all other matrix46entries are defined to be zero. This only works for known dimensions of the47matrix, otherwise trailing zero rows and columns could get lost (notice how48the table gives no hint about the existence of a 5th column). To convert the49above table into a sparse data structure, one could list the table entries50in this way:[133X5152[22X[ [ 1, 3, 2 ], [ 1, 4, 9 ], [ 2, 2, 5 ], [ 3, 4, 1 ] ][122X5354[33X[0;0YHowever, this data structure would not be very efficient. Whenever you are55interested in a row [22Xi[122X of [22XM[122X (this happens all the time when performing56Gaussian elimination) the whole list would have to be searched for 3-tuples57of the form [22X[ i, *, * ][122X. This is why I tried to manage the row index by58putting the tuples into the corresponding list entry:[133X5960[22X[ [ 3, 2 ], [ 4, 9 ] ],[122X61[22X[ [ 2, 5 ] ],[122X62[22X[ [ 4, 1 ] ] ][122X6364[33X[0;0YAs you can see, this looks fairly complicated. However, the same information65can be stored in this form, which would become the final data structure for66[5XGauss[105X sparse matrices:[133X6768indices := [ [ 3, 4 ], entries:= [ [ 2, 9 ],69[ 2 ], [ 5 ],70[ 4 ] ] [ 1 ] ]7172[33X[0;0YAlthough now the number of rows is equal to the Length of both `indices' and73`entries', it is still stored in the sparse matrix. Here is the full data74structure (--> [2XSparseMatrix[102X ([14X3.2-1[114X)):[133X7576[4X[32X from SparseMatrix.gi [32X[104X77[4X DeclareRepresentation( "IsSparseMatrixRep",[104X78[4X IsSparseMatrix, [ "nrows", "ncols", "indices", "entries", "ring" ] );[104X79[4X [104X80[4X[32X[104X8182[33X[0;0YAs you can see, the matrix stores its ring to be on the safe side. This is83especially important for zero matrices, as there is no way to determine the84base ring from the sparse matrix structure. For further information on85sparse matrix construction and converting, refer to [2XSparseMatrix[102X ([14X3.2-1[114X).[133X868788[1X3.1-1 [33X[0;0YA special case: GF(2)[133X[101X8990[4X[32X from SparseMatrix.gi [32X[104X91[4X DeclareRepresentation( "IsSparseMatrixGF2Rep",[104X92[4X IsSparseMatrix, [ "nrows", "ncols", "indices", "ring" ] );[104X93[4X [104X94[4X[32X[104X9596[33X[0;0YBecause the nonzero entries of a matrix over GF(2) are all "1", the entries97of M are not stored at all. It is of course crucial that all operations and98algorithms make 100% sure that all appearing zero entries are deleted from99the `indices' as well as the `entries' list as they arise.[133X100101102[1X3.2 [33X[0;0YMethods and functions for sparse matrices[133X[101X103104[1X3.2-1 SparseMatrix[101X105106[29X[2XSparseMatrix[102X( [3Xmat[103X[, [3XR[103X] ) [32X function107[6XReturns:[106X [33X[0;10Ya sparse matrix over the ring [3XR[103X[133X108109[29X[2XSparseMatrix[102X( [3Xnrows[103X, [3Xncols[103X, [3Xindices[103X ) [32X function110[6XReturns:[106X [33X[0;10Ya sparse matrix over GF(2)[133X111112[29X[2XSparseMatrix[102X( [3Xnrows[103X, [3Xncols[103X, [3Xindices[103X, [3Xentries[103X[, [3XR[103X] ) [32X function113[6XReturns:[106X [33X[0;10Ya sparse matrix over the ring [3XR[103X[133X114115[33X[0;0YThe sparse matrix constructor. In the one-argument form the SparseMatrix116constructor converts a [5XGAP[105X matrix to a sparse matrix. If not provided the117base ring [3XR[103X is found automatically. For the multi-argument form [3Xnrows[103X and118[3Xncols[103X are the dimensions of the matrix. [3Xindices[103X must be a list of length119[3Xnrows[103X containing lists of the column indices of the matrix in ascending120order.[133X121122[4X[32X Example [32X[104X123[4X[25Xgap>[125X [27XM := [ [ 0 , 1 ], [ 3, 0 ] ] * One( GF(2) );[127X[104X124[4X[28X[ [ 0*Z(2), Z(2)^0 ], [ Z(2)^0, 0*Z(2) ] ][128X[104X125[4X[25Xgap>[125X [27XSM := SparseMatrix( M );[127X[104X126[4X[28X<a 2 x 2 sparse matrix over GF(2)>[128X[104X127[4X[25Xgap>[125X [27XIsSparseMatrix( SM );[127X[104X128[4X[28Xtrue[128X[104X129[4X[25Xgap>[125X [27XDisplay( SM );[127X[104X130[4X[28X . 1[128X[104X131[4X[28X 1 .[128X[104X132[4X[25Xgap>[125X [27XSN := SparseMatrix( 2, 2, [ [ 2 ], [ 1 ] ] );[127X[104X133[4X[28X<a 2 x 2 sparse matrix over GF(2)>[128X[104X134[4X[25Xgap>[125X [27XSN = SM;[127X[104X135[4X[28Xtrue[128X[104X136[4X[25Xgap>[125X [27XSN := SparseMatrix( 2, 3,[127X[104X137[4X[25X>[125X [27X [ [ 2 ], [ 1, 3 ] ],[127X[104X138[4X[25X>[125X [27X [ [ 1 ], [ 3, 2 ] ] * One( GF(5) ) );[127X[104X139[4X[28X<a 2 x 3 sparse matrix over GF(5)>[128X[104X140[4X[25Xgap>[125X [27XDisplay( SN );[127X[104X141[4X[28X . 1 .[128X[104X142[4X[28X 3 . 2[128X[104X143[4X[32X[104X144145[1X3.2-2 ConvertSparseMatrixToMatrix[101X146147[29X[2XConvertSparseMatrixToMatrix[102X( [3Xsm[103X ) [32X method148[6XReturns:[106X [33X[0;10Ya [5XGAP[105X matrix, [], or a list of empty lists[133X149150[33X[0;0YThis function converts the sparse matrix [3Xsm[103X into a [5XGAP[105X matrix. In case of151[10Xnrows(sm)=0[110X or [10Xncols(sm)=0[110X the return value is the empty list or a list of152empty lists, respectively.[133X153154[4X[32X Example [32X[104X155[4X[25Xgap>[125X [27XM := [ [ 0 , 1 ], [ 3, 0 ] ] * One( GF(3) );[127X[104X156[4X[28X[ [ 0*Z(3), Z(3)^0 ], [ 0*Z(3), 0*Z(3) ] ][128X[104X157[4X[25Xgap>[125X [27XSM := SparseMatrix( M );[127X[104X158[4X[28X<a 2 x 2 sparse matrix over GF(3)>[128X[104X159[4X[25Xgap>[125X [27XN := ConvertSparseMatrixToMatrix( SM );[127X[104X160[4X[28X[ [ 0*Z(3), Z(3)^0 ], [ 0*Z(3), 0*Z(3) ] ][128X[104X161[4X[25Xgap>[125X [27XM = N;[127X[104X162[4X[28Xtrue[128X[104X163[4X[32X[104X164165[1X3.2-3 CopyMat[101X166167[29X[2XCopyMat[102X( [3Xsm[103X ) [32X method168[6XReturns:[106X [33X[0;10Ya shallow copy of the sparse matrix [3Xsm[103X[133X169170[1X3.2-4 GetEntry[101X171172[29X[2XGetEntry[102X( [3Xsm[103X, [3Xi[103X, [3Xj[103X ) [32X method173[6XReturns:[106X [33X[0;10Ya ring element.[133X174175[33X[0;0YThis returns the entry [10Xsm[i,j][110X of the sparse matrix [3Xsm[103X[133X176177[1X3.2-5 SetEntry[101X178179[29X[2XSetEntry[102X( [3Xsm[103X, [3Xi[103X, [3Xj[103X, [3Xelm[103X ) [32X method180[6XReturns:[106X [33X[0;10Ynothing.[133X181182[33X[0;0YThis sets the entry [10Xsm[i,j][110X of the sparse matrix [3Xsm[103X to [3Xelm[103X.[133X183184[1X3.2-6 AddToEntry[101X185186[29X[2XAddToEntry[102X( [3Xsm[103X, [3Xi[103X, [3Xj[103X, [3Xelm[103X ) [32X method187[6XReturns:[106X [33X[0;10Y[9Xtrue[109X or a ring element[133X188189[33X[0;0YAddToEntry adds the element [3Xelm[103X to the sparse matrix [3Xsm[103X at the [3X(i,j)[103X-th190position. This is a Method with a side effect which returns true if you191tried to add zero or the sum of [10Xsm[i,j][110X and [3Xelm[103X otherwise. Please use this192method whenever possible.[133X193194[1X3.2-7 SparseZeroMatrix[101X195196[29X[2XSparseZeroMatrix[102X( [3Xnrows[103X[, [3Xring[103X] ) [32X function197[6XReturns:[106X [33X[0;10Ya sparse <[3Xnrows[103X x [3Xnrows[103X> zero matrix over the ring [3Xring[103X[133X198199[29X[2XSparseZeroMatrix[102X( [3Xnrows[103X, [3Xncols[103X[, [3Xring[103X] ) [32X function200[6XReturns:[106X [33X[0;10Ya sparse <[3Xnrows[103X x [3Xncols[103X> zero matrix over the ring [3Xring[103X[133X201202[1X3.2-8 SparseIdentityMatrix[101X203204[29X[2XSparseIdentityMatrix[102X( [3Xdim[103X[, [3Xring[103X] ) [32X function205[6XReturns:[106X [33X[0;10Ya sparse <[3Xdim[103X x [3Xdim[103X> identity matrix over the ring [3Xring[103X. If no206ring is specified (one should try to avoid this if possible) the207Rationals are the default ring.[133X208209[1X3.2-9 TransposedSparseMat[101X210211[29X[2XTransposedSparseMat[102X( [3Xsm[103X ) [32X method212[6XReturns:[106X [33X[0;10Ythe transposed matrix of the sparse matrix [3Xsm[103X[133X213214[1X3.2-10 CertainRows[101X215216[29X[2XCertainRows[102X( [3Xsm[103X, [3Xlist[103X ) [32X method217[6XReturns:[106X [33X[0;10Ythe submatrix [10Xsm{[list]}[110X as a sparse matrix[133X218219[1X3.2-11 CertainColumns[101X220221[29X[2XCertainColumns[102X( [3Xsm[103X, [3Xlist[103X ) [32X method222[6XReturns:[106X [33X[0;10Ythe submatrix [10Xsm{[1..nrows(sm)]}{[list]}[110X as a sparse matrix[133X223224[1X3.2-12 UnionOfRows[101X225226[29X[2XUnionOfRows[102X( [3XA[103X, [3XB[103X ) [32X method227[6XReturns:[106X [33X[0;10Ythe row union of the sparse matrices [3XA[103X and [3XB[103X[133X228229[1X3.2-13 UnionOfColumns[101X230231[29X[2XUnionOfColumns[102X( [3XA[103X, [3XB[103X ) [32X method232[6XReturns:[106X [33X[0;10Ythe column union of the sparse matrices [3XA[103X and [3XB[103X[133X233234[1X3.2-14 SparseDiagMat[101X235236[29X[2XSparseDiagMat[102X( [3Xlist[103X ) [32X function237[6XReturns:[106X [33X[0;10Ythe block diagonal matrix composed of the sparse matrices in [3Xlist[103X[133X238239[1X3.2-15 Nrows[101X240241[29X[2XNrows[102X( [3Xsm[103X ) [32X method242[6XReturns:[106X [33X[0;10Ythe number of rows of the sparse matrix [3Xsm[103X. This should be243preferred to the equivalent [10Xsm!.nrows[110X.[133X244245[1X3.2-16 Ncols[101X246247[29X[2XNcols[102X( [3Xsm[103X ) [32X method248[6XReturns:[106X [33X[0;10Ythe number of columns of the sparse matrix [3Xsm[103X. This should be249preferred to the equivalent [10Xsm!.ncols[110X.[133X250251[1X3.2-17 IndicesOfSparseMatrix[101X252253[29X[2XIndicesOfSparseMatrix[102X( [3Xsm[103X ) [32X method254[6XReturns:[106X [33X[0;10Ythe indices of the sparse matrix [3Xsm[103X as a ListList. This should be255preferred to the equivalent [10Xsm!.indices[110X.[133X256257[1X3.2-18 EntriesOfSparseMatrix[101X258259[29X[2XEntriesOfSparseMatrix[102X( [3Xsm[103X ) [32X method260[6XReturns:[106X [33X[0;10Ythe entries of the sparse matrix [3Xsm[103X as a ListList of ring261elements. This should be preferred to the equivalent [10Xsm!.entries[110X262and has the additional advantage of working for sparse matrices263over GF(2) which do not have any entries.[133X264265[1X3.2-19 RingOfDefinition[101X266267[29X[2XRingOfDefinition[102X( [3Xsm[103X ) [32X method268[6XReturns:[106X [33X[0;10Ythe base ring of the sparse matrix [3Xsm[103X. This should be preferred to269the equivalent [10Xsm!.ring[110X.[133X270271272273