-
Notifications
You must be signed in to change notification settings - Fork 24
/
compressed.go
578 lines (516 loc) · 19.7 KB
/
compressed.go
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
package sparse
import (
"github.com/james-bowman/sparse/blas"
"gonum.org/v1/gonum/mat"
)
var (
_ Sparser = (*CSR)(nil)
_ TypeConverter = (*CSR)(nil)
_ mat.Mutable = (*CSR)(nil)
_ mat.RowViewer = (*CSR)(nil)
_ mat.RowNonZeroDoer = (*CSR)(nil)
_ mat.Reseter = (*CSR)(nil)
_ Sparser = (*CSC)(nil)
_ TypeConverter = (*CSC)(nil)
_ mat.Mutable = (*CSC)(nil)
_ mat.ColViewer = (*CSC)(nil)
_ mat.ColNonZeroDoer = (*CSC)(nil)
)
// CSR is a Compressed Sparse Row format sparse matrix implementation (sometimes called Compressed Row
// Storage (CRS) format) and implements the Matrix interface from gonum/matrix. This allows large sparse
// (mostly zero values) matrices to be stored efficiently in memory (only storing non-zero values).
// CSR matrices are poor for constructing sparse matrices incrementally but very good for arithmetic operations.
// CSR, and their sibling CSC, matrices are similar to COOrdinate matrices except the row index slice is
// compressed. Rather than storing the row indices of each non zero values (length == NNZ) each element, i,
// of the slice contains the cumulative count of non zero values in the matrix up to row i-1 of the matrix.
// In this way, it is possible to address any element, i j, in the matrix with the following:
//
// for k := c.indptr[i]; k < c.indptr[i+1]; k++ {
// if c.ind[k] == j {
// return c.data[k]
// }
// }
//
// It should be clear that CSR is like CSC except the slices are row major order rather than column major and
// CSC is essentially the transpose of a CSR.
// As this type implements the gonum mat.Matrix interface, it may be used with any of the Gonum mat
// functions that accept Matrix types as parameters in place of other matrix types included in the Gonum
// mat package e.g. mat.Dense.
type CSR struct {
matrix blas.SparseMatrix
}
// NewCSR creates a new Compressed Sparse Row format sparse matrix.
// The matrix is initialised to the size of the specified r * c dimensions (rows * columns)
// with the specified slices containing row pointers and cols indexes of non-zero elements
// and the non-zero data values themselves respectively. The supplied slices will be used as the
// backing storage to the matrix so changes to values of the slices will be reflected in the created matrix
// and vice versa.
func NewCSR(r int, c int, ia []int, ja []int, data []float64) *CSR {
if uint(r) < 0 {
panic(mat.ErrRowAccess)
}
if uint(c) < 0 {
panic(mat.ErrColAccess)
}
return &CSR{
matrix: blas.SparseMatrix{
I: r, J: c,
Indptr: ia,
Ind: ja,
Data: data,
},
}
}
// Dims returns the size of the matrix as the number of rows and columns
func (c *CSR) Dims() (int, int) {
return c.matrix.I, c.matrix.J
}
// At returns the element of the matrix located at row i and column j. At will panic if specified values
// for i or j fall outside the dimensions of the matrix.
func (c *CSR) At(m, n int) float64 {
return c.matrix.At(m, n)
}
// Set sets the element of the matrix located at row i and column j to value v. Set will panic if
// specified values for i or j fall outside the dimensions of the matrix.
func (c *CSR) Set(m, n int, v float64) {
c.matrix.Set(m, n, v)
}
// T transposes the matrix creating a new CSC matrix sharing the same backing data storage but switching
// column and row sizes and index & index pointer slices i.e. rows become columns and columns become rows.
func (c *CSR) T() mat.Matrix {
return NewCSC(c.matrix.J, c.matrix.I, c.matrix.Indptr, c.matrix.Ind, c.matrix.Data)
}
// NNZ returns the Number of Non Zero elements in the sparse matrix.
func (c *CSR) NNZ() int {
return len(c.matrix.Data)
}
// Trace returns the trace.
func (c *CSR) Trace() float64 {
var trace float64
nrows := len(c.matrix.Indptr) - 1
for i := 0; i < nrows; i++ {
for j := c.matrix.Indptr[i]; j < c.matrix.Indptr[i+1]; j++ {
if c.matrix.Ind[j] == i {
trace += c.matrix.Data[j]
break
}
}
}
return trace
}
// RawMatrix returns a pointer to the underlying blas sparse matrix.
func (c *CSR) RawMatrix() *blas.SparseMatrix {
return &c.matrix
}
// DoNonZero calls the function fn for each of the non-zero elements of the receiver.
// The function fn takes a row/column index and the element value of the receiver at
// (i, j). The order of visiting to each non-zero element is row major.
func (c *CSR) DoNonZero(fn func(i, j int, v float64)) {
for i := 0; i < len(c.matrix.Indptr)-1; i++ {
c.DoRowNonZero(i, fn)
}
}
// DoRowNonZero calls the function fn for each of the non-zero elements of row i
// in the receiver. The function fn takes a row/column index and the element value
// of the receiver at (i, j).
func (c *CSR) DoRowNonZero(i int, fn func(i, j int, v float64)) {
for j := c.matrix.Indptr[i]; j < c.matrix.Indptr[i+1]; j++ {
fn(i, c.matrix.Ind[j], c.matrix.Data[j])
}
}
// Clone copies the specified matrix into the receiver
func (c *CSR) Clone(b mat.Matrix) {
row, col := b.Dims()
if csr, ok := b.(*CSR); ok {
c.cloneCSR(csr)
return
}
c.Reset()
c.reuseAs(row, col, row*col/10, true)
k := 0
for i := 0; i < c.matrix.I; i++ {
c.matrix.Indptr[i] = k
for j := 0; j < c.matrix.J; j++ {
if v := b.At(i, j); v != 0 {
c.matrix.Ind = append(c.matrix.Ind, j)
c.matrix.Data = append(c.matrix.Data, v)
k++
}
}
}
c.matrix.Indptr[c.matrix.I] = k
}
// cloneCSR copies the specified CSR matrix into the receiver
func (c *CSR) cloneCSR(b *CSR) {
c.Reset()
c.reuseAs(b.matrix.I, b.matrix.J, b.NNZ(), false)
copy(c.matrix.Indptr, b.matrix.Indptr)
copy(c.matrix.Ind, b.matrix.Ind)
copy(c.matrix.Data, b.matrix.Data)
}
// ToDense returns a mat.Dense dense format version of the matrix. The returned mat.Dense
// matrix will not share underlying storage with the receiver nor is the receiver modified by this call.
func (c *CSR) ToDense() *mat.Dense {
mat := mat.NewDense(c.matrix.I, c.matrix.J, nil)
for i := 0; i < len(c.matrix.Indptr)-1; i++ {
for j := c.matrix.Indptr[i]; j < c.matrix.Indptr[i+1]; j++ {
mat.Set(i, c.matrix.Ind[j], c.matrix.Data[j])
}
}
return mat
}
// ToDOK returns a DOK (Dictionary Of Keys) sparse format version of the matrix. The returned DOK
// matrix will not share underlying storage with the receiver nor is the receiver modified by this call.
func (c *CSR) ToDOK() *DOK {
dok := NewDOK(c.matrix.I, c.matrix.J)
for i := 0; i < len(c.matrix.Indptr)-1; i++ {
for j := c.matrix.Indptr[i]; j < c.matrix.Indptr[i+1]; j++ {
dok.Set(i, c.matrix.Ind[j], c.matrix.Data[j])
}
}
return dok
}
// ToCOO returns a COOrdinate sparse format version of the matrix. The returned COO matrix will
// not share underlying storage with the receiver nor is the receiver modified by this call.
func (c *CSR) ToCOO() *COO {
rows := make([]int, c.NNZ())
cols := make([]int, c.NNZ())
data := make([]float64, c.NNZ())
for i := 0; i < len(c.matrix.Indptr)-1; i++ {
for j := c.matrix.Indptr[i]; j < c.matrix.Indptr[i+1]; j++ {
rows[j] = i
}
}
copy(cols, c.matrix.Ind)
copy(data, c.matrix.Data)
coo := NewCOO(c.matrix.I, c.matrix.J, rows, cols, data)
return coo
}
// ToCSR returns the receiver
func (c *CSR) ToCSR() *CSR {
return c
}
// ToCSC returns a Compressed Sparse Column sparse format version of the matrix. The returned CSC matrix
// will not share underlying storage with the receiver nor is the receiver modified by this call.
// NB, the current implementation uses COO as an intermediate format so converts to COO before converting
// to CSC but attempts to reuse memory in the intermediate formats.
func (c *CSR) ToCSC() *CSC {
return c.ToCOO().ToCSCReuseMem()
}
// ToType returns an alternative format version fo the matrix in the format specified.
func (c *CSR) ToType(matType MatrixType) mat.Matrix {
return matType.Convert(c)
}
// RowNNZ returns the Number of Non Zero values in the specified row i. RowNNZ will panic if i is out of range.
func (c *CSR) RowNNZ(i int) int {
if uint(i) < 0 || uint(i) >= uint(c.matrix.I) {
panic(mat.ErrRowAccess)
}
return c.matrix.Indptr[i+1] - c.matrix.Indptr[i]
}
// RowView slices the Compressed Sparse Row matrix along its primary axis.
// Returns a VecCOO sparse Vector that shares the same storage with
// the receiver for row i.
func (c *CSR) RowView(i int) mat.Vector {
if i >= c.matrix.I || i < 0 {
panic(mat.ErrRowAccess)
}
return NewVector(
c.matrix.J,
c.matrix.Ind[c.matrix.Indptr[i]:c.matrix.Indptr[i+1]:c.matrix.Indptr[i+1]],
c.matrix.Data[c.matrix.Indptr[i]:c.matrix.Indptr[i+1]:c.matrix.Indptr[i+1]],
)
}
// ScatterRow returns a slice representing row i of the matrix in dense format. Row
// is used as the storage for the operation unless it is nil in which case, new
// storage of the correct length will be allocated. This method will panic if i
// is out of range or row is not the same length as the number of columns in the matrix i.e.
// the correct size to receive the dense representation of the row.
func (c *CSR) ScatterRow(i int, row []float64) []float64 {
if i >= c.matrix.I || i < 0 {
panic(mat.ErrRowAccess)
}
if row != nil && len(row) != c.matrix.J {
panic(mat.ErrRowLength)
}
if row == nil {
row = make([]float64, c.matrix.J)
}
blas.Dussc(
c.matrix.Data[c.matrix.Indptr[i]:c.matrix.Indptr[i+1]],
row,
1,
c.matrix.Ind[c.matrix.Indptr[i]:c.matrix.Indptr[i+1]],
)
return row
}
// Reset zeros the dimensions of the matrix so that it can be reused as the
// receiver of a dimensionally restricted operation.
//
// See the Gonum mat.Reseter interface for more information.
func (c *CSR) Reset() {
c.matrix.I, c.matrix.J = 0, 0
c.matrix.Indptr = c.matrix.Indptr[:0]
c.matrix.Ind = c.matrix.Ind[:0]
c.matrix.Data = c.matrix.Data[:0]
}
// IsZero returns whether the receiver is zero-sized. Zero-sized matrices can be the
// receiver for size-restricted operations. CSR matrices can be zeroed using the Reset
// method.
func (c *CSR) IsZero() bool {
return c.matrix.I == 0 && c.matrix.J == 0
}
// reuseAs resizes a zero-sized matrix to be rxc or checks a non-zero-sized matrix
// is already the correct size (rxc). If the matrix is resized, the method will
// ensure there is sufficient initial capacity allocated in the underlying storage
// to store up to nnz non-zero elements although this will be extended
// automatically later as needed (using Go's built-in append function).
func (c *CSR) reuseAs(row, col, nnz int, zero bool) {
if c.IsZero() {
c.matrix = blas.SparseMatrix{
I: row,
J: col,
}
} else if row != c.matrix.I || col != c.matrix.J {
panic(mat.ErrShape)
}
c.matrix.Indptr = useInts(c.matrix.Indptr, row+1, zero)
c.matrix.Ind = useInts(c.matrix.Ind, nnz, false)
c.matrix.Data = useFloats(c.matrix.Data, nnz, false)
if zero {
c.matrix.Ind = c.matrix.Ind[:0]
c.matrix.Data = c.matrix.Data[:0]
}
}
// checkOverlap checks whether the receiver overlaps or is an alias for the
// matrix a. The method returns true (indicating overlap) if c == a or if
// any of the receiver's internal data structures share underlying storage with a.
func (c *CSR) checkOverlap(a mat.Matrix) bool {
if c == a {
return true
}
switch a := a.(type) {
case *Vector:
return aliasInts(c.matrix.Ind, a.ind) ||
aliasFloats(c.matrix.Data, a.data)
case *COO:
return aliasInts(c.matrix.Ind, a.cols) ||
aliasInts(c.matrix.Ind, a.rows) ||
aliasFloats(c.matrix.Data, a.data)
case *CSR, *CSC:
m := a.(BlasCompatibleSparser).RawMatrix()
return aliasInts(c.matrix.Indptr, m.Indptr) ||
aliasInts(c.matrix.Ind, m.Ind) ||
aliasFloats(c.matrix.Data, m.Data)
default:
return false
}
}
// CSC is a Compressed Sparse Column format sparse matrix implementation (sometimes called Compressed Column
// Storage (CCS) format) and implements the Matrix interface from gonum/matrix. This allows large sparse
// (mostly zero values) matrices to be stored efficiently in memory (only storing non-zero values).
// CSC matrices are poor for constructing sparse matrices incrementally but very good for arithmetic operations.
// CSC, and their sibling CSR, matrices are similar to COOrdinate matrices except the column index slice is
// compressed. Rather than storing the column indices of each non zero values (length == NNZ) each element, i,
// of the slice contains the cumulative count of non zero values in the matrix up to column i-1 of the matrix.
// In this way, it is possible to address any element, j i, in the matrix with the following:
//
// for k := c.indptr[i]; k < c.indptr[i+1]; k++ {
// if c.ind[k] == j {
// return c.data[k]
// }
// }
//
// It should be clear that CSC is like CSR except the slices are column major order rather than row major and CSC
// is essentially the transpose of a CSR.
// As this type implements the gonum mat.Matrix interface, it may be used with any of the Gonum mat functions
// that accept Matrix types as parameters in place of other matrix types included in the Gonum mat package
// e.g. mat.Dense.
type CSC struct {
matrix blas.SparseMatrix
}
// NewCSC creates a new Compressed Sparse Column format sparse matrix.
// The matrix is initialised to the size of the specified r * c dimensions (rows * columns)
// with the specified slices containing column pointers and row indexes of non-zero elements
// and the non-zero data values themselves respectively. The supplied slices will be used as the
// backing storage to the matrix so changes to values of the slices will be reflected in the created matrix
// and vice versa.
func NewCSC(r int, c int, indptr []int, ind []int, data []float64) *CSC {
if uint(r) < 0 {
panic(mat.ErrRowAccess)
}
if uint(c) < 0 {
panic(mat.ErrColAccess)
}
return &CSC{
matrix: blas.SparseMatrix{
I: c, J: r,
Indptr: indptr,
Ind: ind,
Data: data,
},
}
}
// Dims returns the size of the matrix as the number of rows and columns
func (c *CSC) Dims() (int, int) {
return c.matrix.J, c.matrix.I
}
// At returns the element of the matrix located at row i and column j. At will panic if specified values
// for i or j fall outside the dimensions of the matrix.
func (c *CSC) At(m, n int) float64 {
return c.matrix.At(n, m)
}
// Set sets the element of the matrix located at row i and column j to value v. Set will panic if
// specified values for i or j fall outside the dimensions of the matrix.
func (c *CSC) Set(m, n int, v float64) {
c.matrix.Set(n, m, v)
}
// T transposes the matrix creating a new CSR matrix sharing the same backing data storage but switching
// column and row sizes and index & index pointer slices i.e. rows become columns and columns become rows.
func (c *CSC) T() mat.Matrix {
return NewCSR(c.matrix.I, c.matrix.J, c.matrix.Indptr, c.matrix.Ind, c.matrix.Data)
}
// DoNonZero calls the function fn for each of the non-zero elements of the receiver.
// The function fn takes a row/column index and the element value of the receiver at
// (i, j). The order of visiting to each non-zero element is column major.
func (c *CSC) DoNonZero(fn func(i, j int, v float64)) {
for i := 0; i < len(c.matrix.Indptr)-1; i++ {
c.DoColNonZero(i, fn)
}
}
// DoColNonZero calls the function fn for each of the non-zero elements of column j
// in the receiver. The function fn takes a row/column index and the element value
// of the receiver at (i, j).
func (c *CSC) DoColNonZero(j int, fn func(i, j int, v float64)) {
for i := c.matrix.Indptr[j]; i < c.matrix.Indptr[j+1]; i++ {
fn(c.matrix.Ind[i], j, c.matrix.Data[i])
}
}
// NNZ returns the Number of Non Zero elements in the sparse matrix.
func (c *CSC) NNZ() int {
return len(c.matrix.Data)
}
// Trace returns the trace.
func (c *CSC) Trace() float64 {
var trace float64
ncols := len(c.matrix.Indptr) - 1
for i := 0; i < ncols; i++ {
for j := c.matrix.Indptr[i]; j < c.matrix.Indptr[i+1]; j++ {
if c.matrix.Ind[j] == i {
trace += c.matrix.Data[j]
break
}
}
}
return trace
}
// RawMatrix returns a pointer to the underlying blas sparse matrix.
func (c *CSC) RawMatrix() *blas.SparseMatrix {
return &c.matrix
}
// ToDense returns a mat.Dense dense format version of the matrix. The returned mat.Dense
// matrix will not share underlying storage with the receiver nor is the receiver modified by this call.
func (c *CSC) ToDense() *mat.Dense {
mat := mat.NewDense(c.matrix.J, c.matrix.I, nil)
for i := 0; i < len(c.matrix.Indptr)-1; i++ {
for j := c.matrix.Indptr[i]; j < c.matrix.Indptr[i+1]; j++ {
mat.Set(c.matrix.Ind[j], i, c.matrix.Data[j])
}
}
return mat
}
// ToDOK returns a DOK (Dictionary Of Keys) sparse format version of the matrix. The returned DOK
// matrix will not share underlying storage with the receiver nor is the receiver modified by this call.
func (c *CSC) ToDOK() *DOK {
dok := NewDOK(c.matrix.J, c.matrix.I)
for i := 0; i < len(c.matrix.Indptr)-1; i++ {
for j := c.matrix.Indptr[i]; j < c.matrix.Indptr[i+1]; j++ {
dok.Set(c.matrix.Ind[j], i, c.matrix.Data[j])
}
}
return dok
}
// ToCOO returns a COOrdinate sparse format version of the matrix. The returned COO matrix will
// not share underlying storage with the receiver nor is the receiver modified by this call.
func (c *CSC) ToCOO() *COO {
rows := make([]int, c.NNZ())
cols := make([]int, c.NNZ())
data := make([]float64, c.NNZ())
for i := 0; i < len(c.matrix.Indptr)-1; i++ {
for j := c.matrix.Indptr[i]; j < c.matrix.Indptr[i+1]; j++ {
cols[j] = i
}
}
copy(rows, c.matrix.Ind)
copy(data, c.matrix.Data)
coo := NewCOO(c.matrix.J, c.matrix.I, rows, cols, data)
return coo
}
// ToCSR returns a Compressed Sparse Row sparse format version of the matrix. The returned CSR matrix
// will not share underlying storage with the receiver nor is the receiver modified by this call.
// NB, the current implementation uses COO as an intermediate format so converts to COO before converting
// to CSR but attempts to reuse memory in the intermediate formats.
func (c *CSC) ToCSR() *CSR {
return c.ToCOO().ToCSRReuseMem()
}
// ToCSC returns the receiver
func (c *CSC) ToCSC() *CSC {
return c
}
// ToType returns an alternative format version fo the matrix in the format specified.
func (c *CSC) ToType(matType MatrixType) mat.Matrix {
return matType.Convert(c)
}
// ColNNZ returns the Number of Non Zero values in the specified col i. ColNNZ will panic if i is out of range.
func (c *CSC) ColNNZ(i int) int {
if uint(i) < 0 || uint(i) >= uint(c.matrix.I) {
panic(mat.ErrColAccess)
}
return c.matrix.Indptr[i+1] - c.matrix.Indptr[i]
}
// ColView slices the Compressed Sparse Column matrix along its primary axis.
// Returns a VecCOO sparse Vector that shares the same underlying storage as
// column i of the receiver.
func (c *CSC) ColView(j int) mat.Vector {
if j >= c.matrix.I || j < 0 {
panic(mat.ErrColAccess)
}
return NewVector(
c.matrix.J,
c.matrix.Ind[c.matrix.Indptr[j]:c.matrix.Indptr[j+1]:c.matrix.Indptr[j+1]],
c.matrix.Data[c.matrix.Indptr[j]:c.matrix.Indptr[j+1]:c.matrix.Indptr[j+1]],
)
}
// ScatterCol returns a slice representing column j of the matrix in dense format. Col
// is used as the storage for the operation unless it is nil in which case, new
// storage of the correct length will be allocated. This method will panic if j
// is out of range or col is not the same length as the number of rows in the matrix i.e.
// the correct size to receive the dense representation of the column.
func (c *CSC) ScatterCol(j int, col []float64) []float64 {
if j >= c.matrix.I || j < 0 {
panic(mat.ErrColAccess)
}
if col != nil && len(col) != c.matrix.J {
panic(mat.ErrColLength)
}
if col == nil {
col = make([]float64, c.matrix.J)
}
blas.Dussc(
c.matrix.Data[c.matrix.Indptr[j]:c.matrix.Indptr[j+1]],
col,
1,
c.matrix.Ind[c.matrix.Indptr[j]:c.matrix.Indptr[j+1]],
)
return col
}
// Cull removes all entries within epsilon of 0.
func (c *CSR) Cull(epsilon float64) {
newM := c.matrix.Cull(epsilon)
c.matrix = *newM
}
// Cull removes all entries within epsilon of 0.
func (c *CSC) Cull(epsilon float64) {
newM := c.matrix.Cull(epsilon)
c.matrix = *newM
}