arrowspace 0.26.2

Graph Wiring for embeddings using physical networks wiring. Provides graph analysis, vector search and a energy-distribution stats.
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
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
use crate::graph::GraphParams;
use crate::tests::test_helpers;
use approx::abs_diff_eq;
use smartcore::linalg::basic::{arrays::Array2, matrix::DenseMatrix};
use sprs::CsMat;

use crate::laplacian::*;

use log::debug;

// Helper function for creating test vectors with known similarities
fn create_test_vectors() -> Vec<Vec<f64>> {
    vec![
        vec![1.0, 0.0, 0.0], // Unit vector along x-axis
        vec![0.8, 0.6, 0.0], // ~53° from x-axis, cosine ≈ 0.8
        vec![0.0, 1.0, 0.0], // Unit vector along y-axis
        vec![0.0, 0.8, 0.6], // ~53° from y-axis
        vec![0.0, 0.0, 1.0], // Unit vector along z-axis
    ]
}

fn default_params() -> GraphParams {
    GraphParams {
        eps: 0.5,
        k: 3,
        topk: 2,
        p: 2.0,
        sigma: Some(0.1),
        normalise: false,
        sparsity_check: true,
    }
}

#[test]
fn test_basic_laplacian_construction() {
    let items = create_test_vectors();
    let params = default_params();

    let laplacian = build_laplacian_matrix(
        DenseMatrix::<f64>::from_2d_vec(&items).unwrap().transpose(),
        &params,
        None,
        false,
    );

    assert_eq!(laplacian.nnodes, 5);
    assert_eq!(laplacian.matrix.shape(), (3, 3));
    assert_eq!(laplacian.graph_params, params);
}

#[test]
fn test_laplacian_mathematical_properties() {
    let items = create_test_vectors();
    let params = default_params();
    let laplacian = build_laplacian_matrix(
        DenseMatrix::<f64>::from_2d_vec(&items).unwrap().transpose(),
        &params,
        None,
        false,
    );

    let n = laplacian.nnodes;

    // Property 1: Row sums should be zero (within numerical precision)
    for (i, row) in laplacian.matrix.outer_iterator().enumerate() {
        let row_sum: f64 = row.iter().map(|(_, &value)| value).sum();
        assert!(
            abs_diff_eq!(row_sum, 0.0, epsilon = 1e-12),
            r#"Row {i} sum should be zero, got {row_sum:.2e}"#
        );
    }

    // Property 2: Matrix should be symmetric
    // For sparse matrices, we need to check both directions carefully
    let mut asymmetry_violations = 0;
    for (i, row) in laplacian.matrix.outer_iterator().enumerate() {
        for (j, &l_ij) in row.iter() {
            let l_ji = laplacian.matrix.get(j, i).unwrap_or(&0.0);
            let diff = (l_ij - l_ji).abs();
            if diff > 1e-12 {
                asymmetry_violations += 1;
                if asymmetry_violations <= 5 {
                    // Show first 5 violations only
                    debug!(
                        "Asymmetry at ({}, {}): L_ij={:.2e}, L_ji={:.2e}, diff={:.2e}",
                        i, j, l_ij, l_ji, diff
                    );
                }
            }
            assert!(
                abs_diff_eq!(l_ij, l_ji, epsilon = 1e-12),
                r#"Matrix should be symmetric: L[{i},{j}]={l_ij:.2e} != L[{j},{i}]={l_ji:.2e}"#
            );
        }
    }

    // Property 3: Diagonal entries should be non-negative (degrees)
    for i in 0..n {
        let diagonal = laplacian.matrix.get(i, i).copied().unwrap_or(0.0);
        assert!(
            diagonal >= -1e-12,
            "Diagonal L[{},{}] should be non-negative, got {:.6}",
            i,
            i,
            diagonal
        );
    }

    // Property 4: Off-diagonal entries should be non-positive
    // Only need to check stored entries in sparse matrix
    for (i, row) in laplacian.matrix.outer_iterator().enumerate() {
        for (j, &value) in row.iter() {
            if i != j {
                assert!(
                    value <= 1e-12,
                    "Off-diagonal L[{},{}] should be non-positive, got {:.6}",
                    i,
                    j,
                    value
                );
            }
        }
    }

    // Additional sparse matrix specific checks

    // Property 5: Verify sparsity structure makes sense
    let expected_max_nnz = n * (params.k + 1); // k neighbors + diagonal
    assert!(
        laplacian.matrix.nnz() <= expected_max_nnz,
        "Matrix should be sparse: {} non-zeros > expected max {}",
        laplacian.matrix.nnz(),
        expected_max_nnz
    );

    // Property 7: Verify CSR structure integrity
    assert_eq!(
        laplacian.matrix.indices().len(),
        laplacian.matrix.nnz(),
        "CSR indices length should match nnz"
    );
    assert_eq!(
        laplacian.matrix.data().len(),
        laplacian.matrix.nnz(),
        "CSR data length should match nnz"
    );

    debug!(
        "✓ Laplacian mathematical properties verified for {}×{} sparse matrix with {} non-zeros",
        n,
        n,
        laplacian.matrix.nnz()
    );
}

#[test]
fn test_cosine_similarity_based_construction() {
    // Create vectors with known cosine similarities
    let items = vec![
        vec![1.0, 0.0],     // cos(0°) = 1.0 with itself
        vec![0.707, 0.707], // cos(45°) ≈ 0.707 with [1,0]
        vec![0.0, 1.0],     // cos(90°) = 0.0 with [1,0]
        vec![-1.0, 0.0],    // cos(180°) = -1.0 with [1,0]
    ];

    let params = GraphParams {
        eps: 2.0, // Increased to allow more connections
        k: 3,
        topk: 2,
        p: 1.0,
        sigma: Some(0.5), // Increased sigma for better discrimination
        normalise: true,
        sparsity_check: true,
    };

    let (_, adjacency) = test_helpers::build_laplacian_matrix_with_adjacency(&items, &params);

    // Check that similar vectors have higher adjacency weights
    let adj_01 = *adjacency.get(0, 1).unwrap(); // Should be highest (45° angle)
    let adj_02 = *adjacency.get(0, 2).unwrap(); // Should be medium (90° angle)
    let adj_03 = *adjacency.get(0, 3).unwrap(); // Should be lowest (180° angle)

    assert!(
        adj_01 > adj_02,
        "More similar vectors should have higher weights: {} > {}",
        adj_01,
        adj_02
    );

    // Fixed: Use approximate equality for cases that may be thresholded equally
    if adj_02 != adj_03 {
        assert!(
            adj_02 > adj_03,
            "More similar vectors should have higher weights: {} > {}",
            adj_02,
            adj_03
        );
    } else {
        // Both are at threshold - this is acceptable behavior
        debug!(
            "Note: 90° and 180° vectors have equal weights ({}) - likely thresholded",
            adj_02
        );
    }

    // Additional validation: ensure the highest similarity is clearly distinguished
    assert!(
        adj_01 > adj_02 + 0.05,
        "45° similarity should be significantly higher than 90°: {} > {}",
        adj_01,
        adj_02 + 0.05
    );
}

#[test]
fn test_eps_parameter_constraint() {
    let items = vec![
        vec![1.0, 0.0],
        vec![0.5, 0.0], // Distance = 1 - 0.5 = 0.5
        vec![0.0, 1.0], // Distance = 1 - 0 = 1.0
    ];

    // Test with restrictive eps
    let restrictive_params = GraphParams {
        eps: 0.3, // Only allow very close connections
        k: 10,
        topk: 4,
        p: 1.0,
        sigma: Some(0.1),
        normalise: true,
        sparsity_check: true,
    };

    let (_, adjacency_restrictive) =
        test_helpers::build_laplacian_matrix_with_adjacency(&items, &restrictive_params);

    // Test with permissive eps
    let permissive_params = GraphParams {
        eps: 1.5, // Allow most connections
        k: 10,
        topk: 4,
        p: 1.0,
        sigma: Some(0.1),
        normalise: true,
        sparsity_check: true,
    };

    let (_, adjacency_permissive) =
        test_helpers::build_laplacian_matrix_with_adjacency(&items, &permissive_params);

    // Count non-zero adjacency entries
    let count_restrictive = count_nonzero_adjacency(&adjacency_restrictive);
    let count_permissive = count_nonzero_adjacency(&adjacency_permissive);

    assert!(
        count_permissive >= count_restrictive,
        "Larger eps should allow more connections: {} >= {}",
        count_permissive,
        count_restrictive
    );
}

#[test]
fn test_k_parameter_constraint() {
    let items = create_test_vectors();

    let small_k_params = GraphParams {
        eps: 1.0,
        k: 1, // Only 1 neighbor per node
        topk: 1,
        p: 1.0,
        sigma: Some(0.1),
        normalise: true,
        sparsity_check: true,
    };

    let large_k_params = GraphParams {
        eps: 1.0,
        k: 4, // Up to 4 neighbors per node
        topk: 3,
        p: 1.0,
        sigma: Some(0.1),
        normalise: true,
        sparsity_check: true,
    };

    let (_, adj_small_k) =
        test_helpers::build_laplacian_matrix_with_adjacency(&items, &small_k_params);
    let (_, adj_large_k) =
        test_helpers::build_laplacian_matrix_with_adjacency(&items, &large_k_params);

    let connections_small_k = count_nonzero_adjacency(&adj_small_k);
    let connections_large_k = count_nonzero_adjacency(&adj_large_k);

    assert!(
        connections_large_k >= connections_small_k,
        "Larger k should allow more connections: {} >= {}",
        connections_large_k,
        connections_small_k
    );
}

#[test]
#[should_panic(expected = "items should be at least of shape (2,2): (1,1)")]
fn test_insufficient_data_panics() {
    let insufficient_items = vec![vec![1.0]]; // Only one item
    let params = default_params();
    build_laplacian_matrix(
        DenseMatrix::<f64>::from_2d_vec(&insufficient_items).unwrap(),
        &params,
        None,
        false,
    );
}

#[test]
fn test_numerical_stability() {
    // Test with very small values that might cause numerical issues
    let small_values = vec![vec![1e-10, 2e-10], vec![3e-10, 1e-10], vec![2e-10, 3e-10]];

    let params = GraphParams {
        eps: 1.0,
        k: 2,
        topk: 1,
        p: 2.0,
        sigma: Some(1e-8),
        normalise: false,
        sparsity_check: true,
    };

    let laplacian = build_laplacian_matrix(
        DenseMatrix::<f64>::from_2d_vec(&small_values).unwrap(),
        &params,
        None,
        false,
    );

    // Should produce finite values - check all stored entries in sparse matrix
    let mut _total_entries_checked = 0;
    let mut finite_entries = 0;

    for (i, row) in laplacian.matrix.outer_iterator().enumerate() {
        for (j, &val) in row.iter() {
            _total_entries_checked += 1;
            assert!(
                val.is_finite(),
                "Matrix entry [{},{}] should be finite, got {}",
                i,
                j,
                val
            );
            finite_entries += 1;
        }
    }

    // Also check that we can safely access diagonal entries (which should be stored)
    for i in 0..3 {
        let diagonal_val = laplacian.matrix.get(i, i).copied().unwrap_or(0.0);
        assert!(
            diagonal_val.is_finite(),
            "Diagonal entry [{},{}] should be finite, got {}",
            i,
            i,
            diagonal_val
        );
    }

    // Additional numerical stability checks for sparse matrices

    // Check that no entries are NaN or infinite
    let has_nan = laplacian.matrix.data().iter().any(|&x| x.is_nan());
    let has_inf = laplacian.matrix.data().iter().any(|&x| x.is_infinite());

    assert!(!has_nan, "Matrix should not contain NaN values");
    assert!(!has_inf, "Matrix should not contain infinite values");

    // Check that small values didn't cause degenerate sparsity
    assert!(
        laplacian.matrix.nnz() >= 3, // At least diagonal entries
        "Matrix should have at least diagonal entries, got {} non-zeros",
        laplacian.matrix.nnz()
    );

    // Check that the matrix structure is reasonable
    let max_possible_entries = 3 * 3; // 3x3 matrix
    assert!(
        laplacian.matrix.nnz() <= max_possible_entries,
        "Matrix has more entries ({}) than possible ({})",
        laplacian.matrix.nnz(),
        max_possible_entries
    );

    // Verify CSR structure integrity after numerical operations
    assert_eq!(
        laplacian.matrix.indices().len(),
        laplacian.matrix.data().len(),
        "CSR indices and data arrays should have same length"
    );

    // Check that row pointers are monotonic (CSR property)
    let indptr = laplacian.matrix.indptr();
    let indptr_slice = indptr.raw_storage();
    for i in 1..indptr_slice.len() {
        assert!(
            indptr_slice[i] >= indptr_slice[i - 1],
            "CSR row pointers should be monotonic: indptr[{}]={} < indptr[{}]={}",
            i,
            indptr_slice[i],
            i - 1,
            indptr_slice[i - 1]
        );
    }

    debug!(
        "✓ Numerical stability verified: checked {} stored entries, all finite",
        finite_entries
    );
    debug!(
        "  Matrix sparsity: {}/{} entries stored ({:.1}%)",
        laplacian.matrix.nnz(),
        9, // 3x3
        laplacian.matrix.nnz() as f64 / 9.0 * 100.0
    );
}

// Helper function to count non-zero adjacency entries
fn count_nonzero_adjacency(adjacency: &CsMat<f64>) -> usize {
    let (rows, cols) = adjacency.shape();
    let mut count = 0;
    for i in 0..rows {
        for j in 0..cols {
            let a = adjacency.get(i, j).unwrap_or(&0.0);
            if i != j && a.abs() > 1e-12 {
                count += 1;
            }
        }
    }
    count
}

#[test]
fn test_arrowspace_integration_pattern_sparse() {
    // Simulate the usage pattern from ArrowSpace protein example
    let protein_like_data = vec![
        vec![0.82, 0.11, 0.43, 0.28, 0.64],
        vec![0.79, 0.12, 0.45, 0.29, 0.61],
        vec![0.78, 0.13, 0.46, 0.27, 0.62],
        vec![0.81, 0.10, 0.44, 0.26, 0.63],
    ];

    let arrowspace_params = GraphParams {
        eps: 1e-2,
        k: 6,
        topk: 3,
        p: 2.0,
        sigma: None,
        normalise: true,
        sparsity_check: true,
    };

    let laplacian = build_laplacian_matrix(
        DenseMatrix::<f64>::from_2d_vec(&protein_like_data)
            .unwrap()
            .transpose(),
        &arrowspace_params,
        None,
        false,
    );

    // Basic shape checks
    assert_eq!(laplacian.nnodes, 4);
    let (rows, cols) = laplacian.matrix.shape();
    assert_eq!((rows, cols), (5, 5));

    // Numerical stability checks for Laplacian (sparse):
    // 1) Row sums ~ 0
    // 2) Symmetry: L[i,j] ~ L[j,i]
    // 3) Diagonal equals negative sum of off-diagonals (within tolerance)
    let tol = 1e-10;

    for i in 0..rows {
        let mut row_sum = 0.0;
        let mut off_diag_sum = 0.0;
        let mut diag = 0.0;

        if let Some(row) = laplacian.matrix.outer_view(i) {
            for (j, &v) in row.iter() {
                row_sum += v;
                if j == i {
                    diag = v;
                } else {
                    off_diag_sum += v;
                    // Symmetry check
                    let v_t = laplacian.matrix.get(j, i).copied().unwrap_or(0.0);
                    assert!(
                        (v - v_t).abs() < tol,
                        "Laplacian not symmetric at ({},{}) vs ({},{})",
                        i,
                        j,
                        j,
                        i
                    );
                }
            }
        }

        // Row sum ~ 0
        assert!(
            row_sum.abs() < tol,
            "Row {} does not sum to ~0 (sum = {})",
            i,
            row_sum
        );

        // Diagonal equals negative sum of off-diagonals
        assert!(
            (diag + off_diag_sum).abs() < tol,
            "Diagonal consistency failed at row {} (diag = {}, off-sum = {})",
            i,
            diag,
            off_diag_sum
        );
    }
}

#[test]
fn test_optimized_sparse_matrix_laplacian() {
    let items = vec![
        vec![1.0, 0.0, 0.0],
        vec![0.8, 0.2, 0.0],
        vec![0.0, 1.0, 0.0],
        vec![0.0, 0.8, 0.2],
    ];

    let params = GraphParams {
        eps: 0.8,
        k: 2,
        topk: 1,
        p: 2.0,
        sigma: Some(0.1),
        normalise: false,
        sparsity_check: true,
    };

    let laplacian = build_laplacian_matrix(
        DenseMatrix::<f64>::from_2d_vec(&items).unwrap().transpose(),
        &params,
        None,
        false,
    );

    // Verify structure
    assert_eq!(laplacian.nnodes, 4);
    assert_eq!(laplacian.matrix.shape().0, 3);
    assert_eq!(laplacian.matrix.shape().1, 3);

    // Verify sparse matrix properties
    assert!(
        laplacian.matrix.nnz() > 0,
        "Matrix should have some non-zero entries"
    );
    assert!(
        laplacian.matrix.nnz() <= 16, // 4x4 matrix
        "Matrix cannot have more than 16 entries"
    );

    // Verify Laplacian properties for sparse matrix
    // 1. Row sums should be zero
    for (i, row) in laplacian.matrix.outer_iterator().enumerate() {
        let row_sum: f64 = row.iter().map(|(_, &value)| value).sum();
        assert!(
            row_sum.abs() < 1e-10,
            "Row {} sum should be ~0, got {:.2e}",
            i,
            row_sum
        );
    }

    // 2. Matrix should be symmetric
    for (i, row) in laplacian.matrix.outer_iterator().enumerate() {
        for (j, &l_ij) in row.iter() {
            let l_ji = laplacian.matrix.get(j, i).copied().unwrap_or(0.0);
            let diff = (l_ij - l_ji).abs();
            assert!(
                diff < 1e-10,
                "Matrix should be symmetric at ({},{}): L_ij={:.2e}, L_ji={:.2e}",
                i,
                j,
                l_ij,
                l_ji
            );
        }
    }

    // 3. Diagonal entries should be non-negative and explicitly stored
    for i in 0..4 {
        let diagonal = laplacian.matrix.get(i, i).copied().unwrap_or(0.0);
        assert!(
            diagonal >= -1e-10,
            "Diagonal L[{},{}] should be non-negative, got {:.6}",
            i,
            i,
            diagonal
        );
    }

    // 4. Off-diagonal entries should be non-positive (only check stored entries)
    for (i, row) in laplacian.matrix.outer_iterator().enumerate() {
        for (j, &value) in row.iter() {
            if i != j {
                assert!(
                    value <= 1e-10,
                    "Off-diagonal L[{},{}] should be non-positive, got {:.6}",
                    i,
                    j,
                    value
                );
            }
        }
    }

    // 5. Verify sparse matrix structure integrity
    assert_eq!(
        laplacian.matrix.indices().len(),
        laplacian.matrix.data().len(),
        "CSR indices and data should have same length"
    );
    assert_eq!(
        laplacian.matrix.indptr().len(),
        4, // n+1 for 4x4 matrix
        "CSR indptr should have length n+1"
    );

    // 6. Check sparsity is reasonable for the parameters
    let expected_max_nnz = 4 * (params.k + 1); // Each row can have at most k neighbors + diagonal
    assert!(
        laplacian.matrix.nnz() <= expected_max_nnz,
        "Matrix should be reasonably sparse: {} > expected max {}",
        laplacian.matrix.nnz(),
        expected_max_nnz
    );

    debug!("✓ Sparse matrix Laplacian test passed");
    debug!(
        "  Matrix: {}×{} with {} non-zeros ({:.1}% sparse)",
        4,
        4,
        laplacian.matrix.nnz(),
        (1.0 - laplacian.matrix.nnz() as f64 / 16.0) * 100.0
    );

    debug!("Optimized Sparse Matrix Laplacian test passed");
}

#[test]
fn test_with_adjacency_output() {
    let items = vec![vec![1.0, 0.0], vec![0.9, 0.1], vec![0.0, 1.0]];

    let params = GraphParams {
        eps: 0.5,
        k: 2,
        topk: 1,
        p: 1.0,
        sigma: Some(0.2),
        normalise: false,
        sparsity_check: true,
    };

    let (laplacian, adjacency) =
        test_helpers::build_laplacian_matrix_with_adjacency(&items, &params);

    // Verify adjacency has zero diagonal
    for i in 0..3 {
        let diag_val = adjacency.get(i, i).copied().unwrap_or(0.0);
        assert_eq!(
            diag_val, 0.0,
            "Adjacency diagonal [{},{}] should be zero",
            i, i
        );
    }

    // Verify Laplacian = Degree - Adjacency
    for i in 0..3 {
        // Calculate degree from adjacency matrix (sum of row)
        let degree: f64 = if let Some(adj_row) = adjacency.outer_view(i) {
            adj_row.iter().map(|(_, &value)| value).sum()
        } else {
            0.0
        };

        // Check diagonal entry of Laplacian equals degree
        let laplacian_diag = laplacian.matrix.get(i, i).copied().unwrap_or(0.0);
        assert!(
            (laplacian_diag - degree).abs() < 1e-10,
            "Laplacian diagonal L[{},{}]={:.6} should equal degree {:.6}",
            i,
            i,
            laplacian_diag,
            degree
        );

        // Verify off-diagonal: L[i,j] = -A[i,j]
        for j in 0..3 {
            if i != j {
                let adjacency_val = adjacency.get(i, j).copied().unwrap_or(0.0);
                let laplacian_val = laplacian.matrix.get(i, j).copied().unwrap_or(0.0);
                let expected = -adjacency_val;

                assert!(
                    (laplacian_val - expected).abs() < 1e-10,
                    "Laplacian L[{},{}]={:.6} should equal -A[{},{}]={:.6}",
                    i,
                    j,
                    laplacian_val,
                    i,
                    j,
                    expected
                );
            }
        }
    }

    // Additional sparse matrix specific checks

    // Verify both matrices have same sparsity structure for off-diagonals
    for (i, lapl_row) in laplacian.matrix.outer_iterator().enumerate() {
        for (j, &lapl_val) in lapl_row.iter() {
            if i != j {
                let adj_val = adjacency.get(i, j).copied().unwrap_or(0.0);
                if adj_val != 0.0 {
                    // If adjacency has an entry, Laplacian should have its negative
                    assert!(
                        lapl_val.abs() > 1e-12,
                        "If A[{},{}]={:.6} != 0, then L[{},{}] should be non-zero",
                        i,
                        j,
                        adj_val,
                        i,
                        j
                    );
                }
            }
        }
    }

    // Check that adjacency matrix is symmetric
    for (i, adj_row) in adjacency.outer_iterator().enumerate() {
        for (j, &adj_val) in adj_row.iter() {
            let adj_transpose = adjacency.get(j, i).copied().unwrap_or(0.0);
            assert!(
                (adj_val - adj_transpose).abs() < 1e-10,
                "Adjacency should be symmetric: A[{},{}]={:.6} != A[{},{}]={:.6}",
                i,
                j,
                adj_val,
                j,
                i,
                adj_transpose
            );
        }
    }

    // Verify sparsity characteristics
    debug!(
        "✓ Adjacency matrix: {}×{} with {} non-zeros",
        adjacency.shape().0,
        adjacency.shape().1,
        adjacency.nnz()
    );
    debug!(
        "✓ Laplacian matrix: {}×{} with {} non-zeros",
        laplacian.matrix.shape().0,
        laplacian.matrix.shape().1,
        laplacian.matrix.nnz()
    );

    // Laplacian should have at least as many entries as adjacency (due to diagonal)
    assert!(
        laplacian.matrix.nnz() >= adjacency.nnz(),
        "Laplacian ({} nnz) should have at least as many entries as adjacency ({} nnz)",
        laplacian.matrix.nnz(),
        adjacency.nnz()
    );

    debug!("Sparse Adjacency + Laplacian test passed");
}