Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it

563550 views
1
2
3 The Sparse Matrix Data Type
3
4
5
3.1 The inner workings of Gauss sparse matrices
6
7
When doing any kind of computation there is a constant conflict between
8
memory load and speed. On the one hand, memory usage is bounded by the total
9
available memory, on the other hand, computation time should also not exceed
10
certain proportions. Memory usage and CPU time are generally inversely
11
proportional, because the computer needs more time to perform operations on
12
a compactified data structure. The idea of sparse matrices mirrors exactly
13
the need for less memory load, therefore it is natural that sparse
14
algorithms take more time than dense ones. However, if the matrix is
15
sufficiently large and sparse at the same time, sparse algorithms can easily
16
be faster than dense ones while maintaining minimal memory load.
17
18
It should be noted that, although matrices that appear naturally in
19
homological algebra are almost always sparse, they do not have to stay
20
sparse under (R)REF algorithms, especially when the computation is concerned
21
with transformation matrices. Therefore, in a perfect world there should be
22
ways implemented to not only find out which data structure to use, but also
23
at what point to convert from one to the other. This was, however, not the
24
aim of the Gauss package and is just one of many points in which this
25
package could be optimized or extended. Take a look at this matrix M:
26
27
┌── ─ ─ ─ ──┐
28
│ 0 0 2 9 0 │
29
│ 0 5 0 0 0 │
30
│ 0 0 0 1 0 │
31
└── ─ ─ ─ ──┘
32
33
The matrix M carries the same information as the following table, if and
34
only if you know how many rows and columns the matrix has. There is also the
35
matter of the base ring, but this is not important for now:
36
37
┌────── ──────┐
38
│ (i,j) Entry │
39
├────── ──────┤
40
│ (1,3) 2 │
41
│ (1,4) 9 │
42
│ (2,2) 5 │
43
│ (3,4) 1 │
44
└────── ──────┘
45
46
This table relates each index tuple to its nonzero entry, all other matrix
47
entries are defined to be zero. This only works for known dimensions of the
48
matrix, otherwise trailing zero rows and columns could get lost (notice how
49
the table gives no hint about the existence of a 5th column). To convert the
50
above table into a sparse data structure, one could list the table entries
51
in this way:
52
53
[ [ 1, 3, 2 ], [ 1, 4, 9 ], [ 2, 2, 5 ], [ 3, 4, 1 ] ]
54
55
However, this data structure would not be very efficient. Whenever you are
56
interested in a row i of M (this happens all the time when performing
57
Gaussian elimination) the whole list would have to be searched for 3-tuples
58
of the form [ i, *, * ]. This is why I tried to manage the row index by
59
putting the tuples into the corresponding list entry:
60
61
[ [ 3, 2 ], [ 4, 9 ] ],
62
[ [ 2, 5 ] ],
63
[ [ 4, 1 ] ] ]
64
65
As you can see, this looks fairly complicated. However, the same information
66
can be stored in this form, which would become the final data structure for
67
Gauss sparse matrices:
68
69
indices := [ [ 3, 4 ], entries:= [ [ 2, 9 ],
70
[ 2 ], [ 5 ],
71
[ 4 ] ] [ 1 ] ]
72
73
Although now the number of rows is equal to the Length of both `indices' and
74
`entries', it is still stored in the sparse matrix. Here is the full data
75
structure (--> SparseMatrix (3.2-1)):
76
77
 from SparseMatrix.gi 
78
 DeclareRepresentation( "IsSparseMatrixRep",
79
 IsSparseMatrix, [ "nrows", "ncols", "indices", "entries", "ring" ] );
80
 
81

82
83
As you can see, the matrix stores its ring to be on the safe side. This is
84
especially important for zero matrices, as there is no way to determine the
85
base ring from the sparse matrix structure. For further information on
86
sparse matrix construction and converting, refer to SparseMatrix (3.2-1).
87
88
89
3.1-1 A special case: GF(2)
90
91
 from SparseMatrix.gi 
92
 DeclareRepresentation( "IsSparseMatrixGF2Rep",
93
 IsSparseMatrix, [ "nrows", "ncols", "indices", "ring" ] );
94
 
95

96
97
Because the nonzero entries of a matrix over GF(2) are all "1", the entries
98
of M are not stored at all. It is of course crucial that all operations and
99
algorithms make 100% sure that all appearing zero entries are deleted from
100
the `indices' as well as the `entries' list as they arise.
101
102
103
3.2 Methods and functions for sparse matrices
104
105
3.2-1 SparseMatrix
106
107
SparseMatrix( mat[, R] )  function
108
Returns: a sparse matrix over the ring R
109
110
SparseMatrix( nrows, ncols, indices )  function
111
Returns: a sparse matrix over GF(2)
112
113
SparseMatrix( nrows, ncols, indices, entries[, R] )  function
114
Returns: a sparse matrix over the ring R
115
116
The sparse matrix constructor. In the one-argument form the SparseMatrix
117
constructor converts a GAP matrix to a sparse matrix. If not provided the
118
base ring R is found automatically. For the multi-argument form nrows and
119
ncols are the dimensions of the matrix. indices must be a list of length
120
nrows containing lists of the column indices of the matrix in ascending
121
order.
122
123
 Example 
124
gap> M := [ [ 0 , 1 ], [ 3, 0 ] ] * One( GF(2) );
125
[ [ 0*Z(2), Z(2)^0 ], [ Z(2)^0, 0*Z(2) ] ]
126
gap> SM := SparseMatrix( M );
127
<a 2 x 2 sparse matrix over GF(2)>
128
gap> IsSparseMatrix( SM );
129
true
130
gap> Display( SM );
131
 . 1
132
 1 .
133
gap> SN := SparseMatrix( 2, 2, [ [ 2 ], [ 1 ] ] );
134
<a 2 x 2 sparse matrix over GF(2)>
135
gap> SN = SM;
136
true
137
gap> SN := SparseMatrix( 2, 3,
138
>  [ [ 2 ], [ 1, 3 ] ],
139
>  [ [ 1 ], [ 3, 2 ] ] * One( GF(5) ) );
140
<a 2 x 3 sparse matrix over GF(5)>
141
gap> Display( SN );
142
 . 1 .
143
 3 . 2
144

145
146
3.2-2 ConvertSparseMatrixToMatrix
147
148
ConvertSparseMatrixToMatrix( sm )  method
149
Returns: a GAP matrix, [], or a list of empty lists
150
151
This function converts the sparse matrix sm into a GAP matrix. In case of
152
nrows(sm)=0 or ncols(sm)=0 the return value is the empty list or a list of
153
empty lists, respectively.
154
155
 Example 
156
gap> M := [ [ 0 , 1 ], [ 3, 0 ] ] * One( GF(3) );
157
[ [ 0*Z(3), Z(3)^0 ], [ 0*Z(3), 0*Z(3) ] ]
158
gap> SM := SparseMatrix( M );
159
<a 2 x 2 sparse matrix over GF(3)>
160
gap> N := ConvertSparseMatrixToMatrix( SM );
161
[ [ 0*Z(3), Z(3)^0 ], [ 0*Z(3), 0*Z(3) ] ]
162
gap> M = N;
163
true
164

165
166
3.2-3 CopyMat
167
168
CopyMat( sm )  method
169
Returns: a shallow copy of the sparse matrix sm
170
171
3.2-4 GetEntry
172
173
GetEntry( sm, i, j )  method
174
Returns: a ring element.
175
176
This returns the entry sm[i,j] of the sparse matrix sm
177
178
3.2-5 SetEntry
179
180
SetEntry( sm, i, j, elm )  method
181
Returns: nothing.
182
183
This sets the entry sm[i,j] of the sparse matrix sm to elm.
184
185
3.2-6 AddToEntry
186
187
AddToEntry( sm, i, j, elm )  method
188
Returns: true or a ring element
189
190
AddToEntry adds the element elm to the sparse matrix sm at the (i,j)-th
191
position. This is a Method with a side effect which returns true if you
192
tried to add zero or the sum of sm[i,j] and elm otherwise. Please use this
193
method whenever possible.
194
195
3.2-7 SparseZeroMatrix
196
197
SparseZeroMatrix( nrows[, ring] )  function
198
Returns: a sparse <nrows x nrows> zero matrix over the ring ring
199
200
SparseZeroMatrix( nrows, ncols[, ring] )  function
201
Returns: a sparse <nrows x ncols> zero matrix over the ring ring
202
203
3.2-8 SparseIdentityMatrix
204
205
SparseIdentityMatrix( dim[, ring] )  function
206
Returns: a sparse <dim x dim> identity matrix over the ring ring. If no
207
ring is specified (one should try to avoid this if possible) the
208
Rationals are the default ring.
209
210
3.2-9 TransposedSparseMat
211
212
TransposedSparseMat( sm )  method
213
Returns: the transposed matrix of the sparse matrix sm
214
215
3.2-10 CertainRows
216
217
CertainRows( sm, list )  method
218
Returns: the submatrix sm{[list]} as a sparse matrix
219
220
3.2-11 CertainColumns
221
222
CertainColumns( sm, list )  method
223
Returns: the submatrix sm{[1..nrows(sm)]}{[list]} as a sparse matrix
224
225
3.2-12 UnionOfRows
226
227
UnionOfRows( A, B )  method
228
Returns: the row union of the sparse matrices A and B
229
230
3.2-13 UnionOfColumns
231
232
UnionOfColumns( A, B )  method
233
Returns: the column union of the sparse matrices A and B
234
235
3.2-14 SparseDiagMat
236
237
SparseDiagMat( list )  function
238
Returns: the block diagonal matrix composed of the sparse matrices in list
239
240
3.2-15 Nrows
241
242
Nrows( sm )  method
243
Returns: the number of rows of the sparse matrix sm. This should be
244
preferred to the equivalent sm!.nrows.
245
246
3.2-16 Ncols
247
248
Ncols( sm )  method
249
Returns: the number of columns of the sparse matrix sm. This should be
250
preferred to the equivalent sm!.ncols.
251
252
3.2-17 IndicesOfSparseMatrix
253
254
IndicesOfSparseMatrix( sm )  method
255
Returns: the indices of the sparse matrix sm as a ListList. This should be
256
preferred to the equivalent sm!.indices.
257
258
3.2-18 EntriesOfSparseMatrix
259
260
EntriesOfSparseMatrix( sm )  method
261
Returns: the entries of the sparse matrix sm as a ListList of ring
262
elements. This should be preferred to the equivalent sm!.entries
263
and has the additional advantage of working for sparse matrices
264
over GF(2) which do not have any entries.
265
266
3.2-19 RingOfDefinition
267
268
RingOfDefinition( sm )  method
269
Returns: the base ring of the sparse matrix sm. This should be preferred to
270
the equivalent sm!.ring.
271
272
273