nalgebra-sparse 0.11.0

Sparse matrix computation based on nalgebra.
Documentation
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
use crate::common::csc_strategy;
use nalgebra::DMatrix;
use nalgebra::proptest::matrix;
use nalgebra_sparse::convert::serial::{
    convert_coo_csc, convert_coo_csr, convert_coo_dense, convert_csc_coo, convert_csc_csr,
    convert_csc_dense, convert_csr_coo, convert_csr_csc, convert_csr_dense, convert_dense_coo,
    convert_dense_csc, convert_dense_csr,
};
use nalgebra_sparse::coo::CooMatrix;
use nalgebra_sparse::csc::CscMatrix;
use nalgebra_sparse::csr::CsrMatrix;
use nalgebra_sparse::proptest::{coo_no_duplicates, coo_with_duplicates, csc, csr};
use proptest::prelude::*;

#[test]
fn test_convert_dense_coo() {
    // No duplicates
    {
        #[rustfmt::skip]
        let entries = &[1, 0, 3,
                        0, 5, 0];
        // The COO representation of a dense matrix is not unique.
        // Here we implicitly test that the coo matrix is indeed constructed from column-major
        // iteration of the dense matrix.
        let dense = DMatrix::from_row_slice(2, 3, entries);
        let coo = CooMatrix::try_from_triplets(2, 3, vec![0, 1, 0], vec![0, 1, 2], vec![1, 5, 3])
            .unwrap();

        assert_eq!(CooMatrix::from(&dense), coo);
        assert_eq!(DMatrix::from(&coo), dense);
    }

    // Duplicates
    // No duplicates
    {
        #[rustfmt::skip]
        let entries = &[1, 0, 3,
            0, 5, 0];
        // The COO representation of a dense matrix is not unique.
        // Here we implicitly test that the coo matrix is indeed constructed from column-major
        // iteration of the dense matrix.
        let dense = DMatrix::from_row_slice(2, 3, entries);
        let coo_no_dup =
            CooMatrix::try_from_triplets(2, 3, vec![0, 1, 0], vec![0, 1, 2], vec![1, 5, 3])
                .unwrap();
        let coo_dup = CooMatrix::try_from_triplets(
            2,
            3,
            vec![0, 1, 0, 1],
            vec![0, 1, 2, 1],
            vec![1, -2, 3, 7],
        )
        .unwrap();

        assert_eq!(CooMatrix::from(&dense), coo_no_dup);
        assert_eq!(DMatrix::from(&coo_dup), dense);
    }
}

#[test]
fn test_convert_coo_csr() {
    // No duplicates
    {
        let coo = {
            let mut coo = CooMatrix::new(3, 4);
            coo.push(1, 3, 4);
            coo.push(0, 1, 2);
            coo.push(2, 0, 1);
            coo.push(2, 3, 2);
            coo.push(2, 2, 1);
            coo
        };

        let expected_csr = CsrMatrix::try_from_csr_data(
            3,
            4,
            vec![0, 1, 2, 5],
            vec![1, 3, 0, 2, 3],
            vec![2, 4, 1, 1, 2],
        )
        .unwrap();

        assert_eq!(convert_coo_csr(&coo), expected_csr);
    }

    // Duplicates
    {
        let coo = {
            let mut coo = CooMatrix::new(3, 4);
            coo.push(1, 3, 4);
            coo.push(2, 3, 2);
            coo.push(0, 1, 2);
            coo.push(2, 0, 1);
            coo.push(2, 3, 2);
            coo.push(0, 1, 3);
            coo.push(2, 2, 1);
            coo
        };

        let expected_csr = CsrMatrix::try_from_csr_data(
            3,
            4,
            vec![0, 1, 2, 5],
            vec![1, 3, 0, 2, 3],
            vec![5, 4, 1, 1, 4],
        )
        .unwrap();

        assert_eq!(convert_coo_csr(&coo), expected_csr);
    }
}

#[test]
fn test_convert_csr_coo() {
    let csr = CsrMatrix::try_from_csr_data(
        3,
        4,
        vec![0, 1, 2, 5],
        vec![1, 3, 0, 2, 3],
        vec![5, 4, 1, 1, 4],
    )
    .unwrap();

    let expected_coo = CooMatrix::try_from_triplets(
        3,
        4,
        vec![0, 1, 2, 2, 2],
        vec![1, 3, 0, 2, 3],
        vec![5, 4, 1, 1, 4],
    )
    .unwrap();

    assert_eq!(convert_csr_coo(&csr), expected_coo);
}

#[test]
fn test_convert_coo_csc() {
    // No duplicates
    {
        let coo = {
            let mut coo = CooMatrix::new(3, 4);
            coo.push(1, 3, 4);
            coo.push(0, 1, 2);
            coo.push(2, 0, 1);
            coo.push(2, 3, 2);
            coo.push(2, 2, 1);
            coo
        };

        let expected_csc = CscMatrix::try_from_csc_data(
            3,
            4,
            vec![0, 1, 2, 3, 5],
            vec![2, 0, 2, 1, 2],
            vec![1, 2, 1, 4, 2],
        )
        .unwrap();

        assert_eq!(convert_coo_csc(&coo), expected_csc);
    }

    // Duplicates
    {
        let coo = {
            let mut coo = CooMatrix::new(3, 4);
            coo.push(1, 3, 4);
            coo.push(2, 3, 2);
            coo.push(0, 1, 2);
            coo.push(2, 0, 1);
            coo.push(2, 3, 2);
            coo.push(0, 1, 3);
            coo.push(2, 2, 1);
            coo
        };

        let expected_csc = CscMatrix::try_from_csc_data(
            3,
            4,
            vec![0, 1, 2, 3, 5],
            vec![2, 0, 2, 1, 2],
            vec![1, 5, 1, 4, 4],
        )
        .unwrap();

        assert_eq!(convert_coo_csc(&coo), expected_csc);
    }
}

#[test]
fn test_convert_csc_coo() {
    let csc = CscMatrix::try_from_csc_data(
        3,
        4,
        vec![0, 1, 2, 3, 5],
        vec![2, 0, 2, 1, 2],
        vec![1, 2, 1, 4, 2],
    )
    .unwrap();

    let expected_coo = CooMatrix::try_from_triplets(
        3,
        4,
        vec![2, 0, 2, 1, 2],
        vec![0, 1, 2, 3, 3],
        vec![1, 2, 1, 4, 2],
    )
    .unwrap();

    assert_eq!(convert_csc_coo(&csc), expected_coo);
}

#[test]
fn test_convert_csr_csc_bidirectional() {
    let csr = CsrMatrix::try_from_csr_data(
        3,
        4,
        vec![0, 3, 4, 6],
        vec![1, 2, 3, 0, 1, 3],
        vec![5, 3, 2, 2, 1, 4],
    )
    .unwrap();

    let csc = CscMatrix::try_from_csc_data(
        3,
        4,
        vec![0, 1, 3, 4, 6],
        vec![1, 0, 2, 0, 0, 2],
        vec![2, 5, 1, 3, 2, 4],
    )
    .unwrap();

    assert_eq!(convert_csr_csc(&csr), csc);
    assert_eq!(convert_csc_csr(&csc), csr);
}

#[test]
fn test_convert_csr_dense_bidirectional() {
    let csr = CsrMatrix::try_from_csr_data(
        3,
        4,
        vec![0, 3, 4, 6],
        vec![1, 2, 3, 0, 1, 3],
        vec![5, 3, 2, 2, 1, 4],
    )
    .unwrap();

    #[rustfmt::skip]
    let dense = DMatrix::from_row_slice(3, 4, &[
        0, 5, 3, 2,
        2, 0, 0, 0,
        0, 1, 0, 4
    ]);

    assert_eq!(convert_csr_dense(&csr), dense);
    assert_eq!(convert_dense_csr(&dense), csr);
}

#[test]
fn test_convert_csc_dense_bidirectional() {
    let csc = CscMatrix::try_from_csc_data(
        3,
        4,
        vec![0, 1, 3, 4, 6],
        vec![1, 0, 2, 0, 0, 2],
        vec![2, 5, 1, 3, 2, 4],
    )
    .unwrap();

    #[rustfmt::skip]
    let dense = DMatrix::from_row_slice(3, 4, &[
        0, 5, 3, 2,
        2, 0, 0, 0,
        0, 1, 0, 4
    ]);

    assert_eq!(convert_csc_dense(&csc), dense);
    assert_eq!(convert_dense_csc(&dense), csc);
}

fn coo_strategy() -> impl Strategy<Value = CooMatrix<i32>> {
    coo_with_duplicates(-5..=5, 0..=6usize, 0..=6usize, 40, 2)
}

fn coo_no_duplicates_strategy() -> impl Strategy<Value = CooMatrix<i32>> {
    coo_no_duplicates(-5..=5, 0..=6usize, 0..=6usize, 40)
}

fn csr_strategy() -> impl Strategy<Value = CsrMatrix<i32>> {
    csr(-5..=5, 0..=6usize, 0..=6usize, 40)
}

/// Avoid generating explicit zero values so that it is possible to reason about sparsity patterns
fn non_zero_csr_strategy() -> impl Strategy<Value = CsrMatrix<i32>> {
    csr(1..=5, 0..=6usize, 0..=6usize, 40)
}

/// Avoid generating explicit zero values so that it is possible to reason about sparsity patterns
fn non_zero_csc_strategy() -> impl Strategy<Value = CscMatrix<i32>> {
    csc(1..=5, 0..=6usize, 0..=6usize, 40)
}

fn dense_strategy() -> impl Strategy<Value = DMatrix<i32>> {
    matrix(-5..=5, 0..=6, 0..=6)
}

proptest! {

    #[test]
    fn convert_dense_coo_roundtrip(dense in matrix(-5 ..= 5, 0 ..=6, 0..=6)) {
        let coo = convert_dense_coo(&dense);
        let dense2 = convert_coo_dense(&coo);
        prop_assert_eq!(&dense, &dense2);
    }

    #[test]
    fn convert_coo_dense_coo_roundtrip(coo in coo_strategy()) {
        // We cannot compare the result of the roundtrip coo -> dense -> coo directly for
        // two reasons:
        //  1. the COO matrices will generally have different ordering of elements
        //  2. explicitly stored zero entries in the original matrix will be discarded
        //     when converting back to COO
        // Therefore we instead compare the results of converting the COO matrix
        // at the end of the roundtrip with its dense representation
        let dense = convert_coo_dense(&coo);
        let coo2 = convert_dense_coo(&dense);
        let dense2 = convert_coo_dense(&coo2);
        prop_assert_eq!(dense, dense2);
    }

    #[test]
    fn coo_from_dense_roundtrip(dense in dense_strategy()) {
        prop_assert_eq!(&dense, &DMatrix::from(&CooMatrix::from(&dense)));
    }

    #[test]
    fn convert_coo_csr_agrees_with_csr_dense(coo in coo_strategy()) {
        let coo_dense = convert_coo_dense(&coo);
        let csr = convert_coo_csr(&coo);
        let csr_dense = convert_csr_dense(&csr);
        prop_assert_eq!(csr_dense, coo_dense);

        // It might be that COO matrices have a higher nnz due to duplicates,
        // so we can only check that the CSR matrix has no more than the original COO matrix
        prop_assert!(csr.nnz() <= coo.nnz());
    }

    #[test]
    fn convert_coo_csr_nnz(coo in coo_no_duplicates_strategy()) {
        // Check that the NNZ are equal when converting from a CooMatrix without
        // duplicates to a CSR matrix
        let csr = convert_coo_csr(&coo);
        prop_assert_eq!(csr.nnz(), coo.nnz());
    }

    #[test]
    fn convert_csr_coo_roundtrip(csr in csr_strategy()) {
        let coo = convert_csr_coo(&csr);
        let csr2 = convert_coo_csr(&coo);
        prop_assert_eq!(csr2, csr);
    }

    #[test]
    fn coo_from_csr_roundtrip(csr in csr_strategy()) {
        prop_assert_eq!(&csr, &CsrMatrix::from(&CooMatrix::from(&csr)));
    }

    #[test]
    fn csr_from_dense_roundtrip(dense in dense_strategy()) {
        prop_assert_eq!(&dense, &DMatrix::from(&CsrMatrix::from(&dense)));
    }

    #[test]
    fn convert_csr_dense_roundtrip(csr in non_zero_csr_strategy()) {
        // Since we only generate CSR matrices with non-zero values, we know that the
        // number of explicitly stored entries when converting CSR->Dense->CSR should be
        // unchanged, so that we can verify that the result is the same as the input
        let dense = convert_csr_dense(&csr);
        let csr2 = convert_dense_csr(&dense);
        prop_assert_eq!(csr2, csr);
    }

    #[test]
    fn convert_csc_coo_roundtrip(csc in csc_strategy()) {
        let coo = convert_csc_coo(&csc);
        let csc2 = convert_coo_csc(&coo);
        prop_assert_eq!(csc2, csc);
    }

    #[test]
    fn coo_from_csc_roundtrip(csc in csc_strategy()) {
        prop_assert_eq!(&csc, &CscMatrix::from(&CooMatrix::from(&csc)));
    }

    #[test]
    fn convert_csc_dense_roundtrip(csc in non_zero_csc_strategy()) {
        // Since we only generate CSC matrices with non-zero values, we know that the
        // number of explicitly stored entries when converting CSC->Dense->CSC should be
        // unchanged, so that we can verify that the result is the same as the input
        let dense = convert_csc_dense(&csc);
        let csc2 = convert_dense_csc(&dense);
        prop_assert_eq!(csc2, csc);
    }

    #[test]
    fn csc_from_dense_roundtrip(dense in dense_strategy()) {
        prop_assert_eq!(&dense, &DMatrix::from(&CscMatrix::from(&dense)));
    }

    #[test]
    fn convert_coo_csc_agrees_with_csc_dense(coo in coo_strategy()) {
        let coo_dense = convert_coo_dense(&coo);
        let csc = convert_coo_csc(&coo);
        let csc_dense = convert_csc_dense(&csc);
        prop_assert_eq!(csc_dense, coo_dense);

        // It might be that COO matrices have a higher nnz due to duplicates,
        // so we can only check that the CSR matrix has no more than the original COO matrix
        prop_assert!(csc.nnz() <= coo.nnz());
    }

    #[test]
    fn convert_coo_csc_nnz(coo in coo_no_duplicates_strategy()) {
        // Check that the NNZ are equal when converting from a CooMatrix without
        // duplicates to a CSR matrix
        let csc = convert_coo_csc(&coo);
        prop_assert_eq!(csc.nnz(), coo.nnz());
    }

    #[test]
    fn convert_csc_csr_roundtrip(csc in csc_strategy()) {
        let csr = convert_csc_csr(&csc);
        let csc2 = convert_csr_csc(&csr);
        prop_assert_eq!(csc2, csc);
    }

    #[test]
    fn convert_csr_csc_roundtrip(csr in csr_strategy()) {
        let csc = convert_csr_csc(&csr);
        let csr2 = convert_csc_csr(&csc);
        prop_assert_eq!(csr2, csr);
    }

    #[test]
    fn csc_from_csr_roundtrip(csr in csr_strategy()) {
        prop_assert_eq!(&csr, &CsrMatrix::from(&CscMatrix::from(&csr)));
    }

    #[test]
    fn csr_from_csc_roundtrip(csc in csc_strategy()) {
        prop_assert_eq!(&csc, &CscMatrix::from(&CsrMatrix::from(&csc)));
    }
}