sublinear 0.3.0

High-performance sublinear-time solver for asymmetric diagonally dominant systems
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
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
//! Sparse matrix storage implementations.
//!
//! This module provides efficient storage formats for sparse matrices,
//! including CSR, CSC, COO, and graph adjacency representations.

use crate::types::{Precision, DimensionType, IndexType, NodeId};
use crate::error::{SolverError, Result};
use alloc::{vec::Vec, collections::BTreeMap};
use core::iter;

/// Compressed Sparse Row (CSR) storage format.
/// 
/// Efficient for row-wise operations and matrix-vector multiplication.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CSRStorage {
    /// Non-zero values in row-major order
    pub values: Vec<Precision>,
    /// Column indices corresponding to values
    pub col_indices: Vec<IndexType>,
    /// Row pointers: row_ptr[i] is the start of row i in values/col_indices
    pub row_ptr: Vec<IndexType>,
}

/// Compressed Sparse Column (CSC) storage format.
/// 
/// Efficient for column-wise operations.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CSCStorage {
    /// Non-zero values in column-major order
    pub values: Vec<Precision>,
    /// Row indices corresponding to values
    pub row_indices: Vec<IndexType>,
    /// Column pointers: col_ptr[j] is the start of column j in values/row_indices
    pub col_ptr: Vec<IndexType>,
}

/// Coordinate (COO) storage format.
/// 
/// Efficient for construction and random access patterns.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct COOStorage {
    /// Row indices
    pub row_indices: Vec<IndexType>,
    /// Column indices
    pub col_indices: Vec<IndexType>,
    /// Values
    pub values: Vec<Precision>,
}

/// Graph adjacency list storage format.
/// 
/// Optimized for graph algorithms like push methods.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct GraphStorage {
    /// Outgoing edges for each node
    pub out_edges: Vec<Vec<GraphEdge>>,
    /// Incoming edges for each node (for backward push)
    pub in_edges: Vec<Vec<GraphEdge>>,
    /// Node degrees for normalization
    pub degrees: Vec<Precision>,
}

/// Graph edge representation.
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct GraphEdge {
    /// Target node
    pub target: NodeId,
    /// Edge weight
    pub weight: Precision,
}

// CSR Implementation
impl CSRStorage {
    /// Create CSR storage from COO format.
    pub fn from_coo(coo: &COOStorage, rows: DimensionType, cols: DimensionType) -> Result<Self> {
        if coo.is_empty() {
            return Ok(Self {
                values: Vec::new(),
                col_indices: Vec::new(),
                row_ptr: vec![0; rows + 1],
            });
        }
        
        // Sort by row, then by column
        let mut sorted_entries: Vec<_> = coo.row_indices.iter()
            .zip(&coo.col_indices)
            .zip(&coo.values)
            .map(|((&r, &c), &v)| (r as usize, c, v))
            .collect();
        sorted_entries.sort_by(|a, b| a.0.cmp(&b.0).then(a.1.cmp(&b.1)));
        
        let mut values = Vec::new();
        let mut col_indices = Vec::new();
        let mut row_ptr = vec![0; rows + 1];
        
        let mut current_row = 0;
        let mut nnz_count = 0;
        
        for (row, col, value) in sorted_entries {
            // Skip zeros
            if value == 0.0 {
                continue;
            }
            
            // Update row pointers
            while current_row < row {
                current_row += 1;
                row_ptr[current_row] = nnz_count as IndexType;
            }
            
            values.push(value);
            col_indices.push(col);
            nnz_count += 1;
        }
        
        // Finalize remaining row pointers
        while current_row < rows {
            current_row += 1;
            row_ptr[current_row] = nnz_count as IndexType;
        }
        
        Ok(Self {
            values,
            col_indices,
            row_ptr,
        })
    }
    
    /// Create CSR storage from CSC format.
    pub fn from_csc(csc: &CSCStorage, rows: DimensionType, cols: DimensionType) -> Result<Self> {
        let triplets = csc.to_triplets()?;
        let coo = COOStorage::from_triplets(triplets)?;
        Self::from_coo(&coo, rows, cols)
    }
    
    /// Get matrix element at (row, col).
    pub fn get(&self, row: usize, col: usize) -> Option<Precision> {
        if row >= self.row_ptr.len() - 1 {
            return None;
        }
        
        let start = self.row_ptr[row] as usize;
        let end = self.row_ptr[row + 1] as usize;
        
        // Binary search for the column
        match self.col_indices[start..end].binary_search(&(col as IndexType)) {
            Ok(pos) => Some(self.values[start + pos]),
            Err(_) => None,
        }
    }
    
    /// Iterate over non-zero elements in a row.
    pub fn row_iter(&self, row: usize) -> CSRRowIter {
        if row >= self.row_ptr.len() - 1 {
            return CSRRowIter {
                col_indices: &[],
                values: &[],
                pos: 0,
            };
        }
        
        let start = self.row_ptr[row] as usize;
        let end = self.row_ptr[row + 1] as usize;
        
        CSRRowIter {
            col_indices: &self.col_indices[start..end],
            values: &self.values[start..end],
            pos: 0,
        }
    }
    
    /// Iterate over non-zero elements in a column (slow for CSR).
    pub fn col_iter(&self, col: usize) -> CSRColIter {
        CSRColIter {
            storage: self,
            col: col as IndexType,
            row: 0,
        }
    }
    
    /// Matrix-vector multiplication: result = A * x
    pub fn multiply_vector(&self, x: &[Precision], result: &mut [Precision]) {
        result.fill(0.0);
        self.multiply_vector_add(x, result);
    }
    
    /// Matrix-vector multiplication with accumulation: result += A * x
    pub fn multiply_vector_add(&self, x: &[Precision], result: &mut [Precision]) {
        for (row, mut row_sum) in result.iter_mut().enumerate() {
            let start = self.row_ptr[row] as usize;
            let end = self.row_ptr[row + 1] as usize;
            
            for i in start..end {
                let col = self.col_indices[i] as usize;
                *row_sum += self.values[i] * x[col];
            }
        }
    }
    
    /// Get number of non-zero elements.
    pub fn nnz(&self) -> usize {
        self.values.len()
    }
    
    /// Extract as coordinate triplets.
    pub fn to_triplets(&self) -> Result<Vec<(usize, usize, Precision)>> {
        let mut triplets = Vec::new();
        
        for row in 0..self.row_ptr.len() - 1 {
            let start = self.row_ptr[row] as usize;
            let end = self.row_ptr[row + 1] as usize;
            
            for i in start..end {
                let col = self.col_indices[i] as usize;
                let value = self.values[i];
                triplets.push((row, col, value));
            }
        }
        
        Ok(triplets)
    }
    
    /// Scale all values by a factor.
    pub fn scale(&mut self, factor: Precision) {
        for value in &mut self.values {
            *value *= factor;
        }
    }
    
    /// Add a value to the diagonal.
    pub fn add_diagonal(&mut self, alpha: Precision) {
        for row in 0..self.row_ptr.len() - 1 {
            let start = self.row_ptr[row] as usize;
            let end = self.row_ptr[row + 1] as usize;
            
            // Look for diagonal element
            if let Ok(pos) = self.col_indices[start..end].binary_search(&(row as IndexType)) {
                self.values[start + pos] += alpha;
            }
            // Note: If diagonal element doesn't exist, we'd need to restructure the matrix
        }
    }
}

/// Iterator over non-zero elements in a CSR row.
pub struct CSRRowIter<'a> {
    col_indices: &'a [IndexType],
    values: &'a [Precision],
    pos: usize,
}

impl<'a> Iterator for CSRRowIter<'a> {
    type Item = (IndexType, Precision);
    
    fn next(&mut self) -> Option<Self::Item> {
        if self.pos < self.col_indices.len() {
            let col = self.col_indices[self.pos];
            let val = self.values[self.pos];
            self.pos += 1;
            Some((col, val))
        } else {
            None
        }
    }
}

/// Iterator over non-zero elements in a CSR column (inefficient).
pub struct CSRColIter<'a> {
    storage: &'a CSRStorage,
    col: IndexType,
    row: usize,
}

impl<'a> Iterator for CSRColIter<'a> {
    type Item = (IndexType, Precision);
    
    fn next(&mut self) -> Option<Self::Item> {
        while self.row < self.storage.row_ptr.len() - 1 {
            let start = self.storage.row_ptr[self.row] as usize;
            let end = self.storage.row_ptr[self.row + 1] as usize;
            
            if let Ok(pos) = self.storage.col_indices[start..end].binary_search(&self.col) {
                let value = self.storage.values[start + pos];
                let row = self.row as IndexType;
                self.row += 1;
                return Some((row, value));
            }
            
            self.row += 1;
        }
        None
    }
}

// CSC Implementation
impl CSCStorage {
    /// Create CSC storage from COO format.
    pub fn from_coo(coo: &COOStorage, rows: DimensionType, cols: DimensionType) -> Result<Self> {
        if coo.is_empty() {
            return Ok(Self {
                values: Vec::new(),
                row_indices: Vec::new(),
                col_ptr: vec![0; cols + 1],
            });
        }
        
        // Sort by column, then by row
        let mut sorted_entries: Vec<_> = coo.row_indices.iter()
            .zip(&coo.col_indices)
            .zip(&coo.values)
            .map(|((&r, &c), &v)| (r, c as usize, v))
            .collect();
        sorted_entries.sort_by(|a, b| a.1.cmp(&b.1).then(a.0.cmp(&b.0)));
        
        let mut values = Vec::new();
        let mut row_indices = Vec::new();
        let mut col_ptr = vec![0; cols + 1];
        
        let mut current_col = 0;
        let mut nnz_count = 0;
        
        for (row, col, value) in sorted_entries {
            // Skip zeros
            if value == 0.0 {
                continue;
            }
            
            // Update column pointers
            while current_col < col {
                current_col += 1;
                col_ptr[current_col] = nnz_count as IndexType;
            }
            
            values.push(value);
            row_indices.push(row);
            nnz_count += 1;
        }
        
        // Finalize remaining column pointers
        while current_col < cols {
            current_col += 1;
            col_ptr[current_col] = nnz_count as IndexType;
        }
        
        Ok(Self {
            values,
            row_indices,
            col_ptr,
        })
    }
    
    /// Create CSC storage from CSR format.
    pub fn from_csr(csr: &CSRStorage, rows: DimensionType, cols: DimensionType) -> Result<Self> {
        let triplets = csr.to_triplets()?;
        let coo = COOStorage::from_triplets(triplets)?;
        Self::from_coo(&coo, rows, cols)
    }
    
    /// Get matrix element at (row, col).
    pub fn get(&self, row: usize, col: usize) -> Option<Precision> {
        if col >= self.col_ptr.len() - 1 {
            return None;
        }
        
        let start = self.col_ptr[col] as usize;
        let end = self.col_ptr[col + 1] as usize;
        
        // Binary search for the row
        match self.row_indices[start..end].binary_search(&(row as IndexType)) {
            Ok(pos) => Some(self.values[start + pos]),
            Err(_) => None,
        }
    }
    
    /// Iterate over non-zero elements in a row (slow for CSC).
    pub fn row_iter(&self, row: usize) -> CSCRowIter {
        CSCRowIter {
            storage: self,
            row: row as IndexType,
            col: 0,
        }
    }
    
    /// Iterate over non-zero elements in a column.
    pub fn col_iter(&self, col: usize) -> CSCColIter {
        if col >= self.col_ptr.len() - 1 {
            return CSCColIter {
                row_indices: &[],
                values: &[],
                pos: 0,
            };
        }
        
        let start = self.col_ptr[col] as usize;
        let end = self.col_ptr[col + 1] as usize;
        
        CSCColIter {
            row_indices: &self.row_indices[start..end],
            values: &self.values[start..end],
            pos: 0,
        }
    }
    
    /// Matrix-vector multiplication: result = A * x
    pub fn multiply_vector(&self, x: &[Precision], result: &mut [Precision]) {
        result.fill(0.0);
        self.multiply_vector_add(x, result);
    }
    
    /// Matrix-vector multiplication with accumulation: result += A * x
    pub fn multiply_vector_add(&self, x: &[Precision], result: &mut [Precision]) {
        for col in 0..self.col_ptr.len() - 1 {
            let x_col = x[col];
            if x_col == 0.0 {
                continue;
            }
            
            let start = self.col_ptr[col] as usize;
            let end = self.col_ptr[col + 1] as usize;
            
            for i in start..end {
                let row = self.row_indices[i] as usize;
                result[row] += self.values[i] * x_col;
            }
        }
    }
    
    /// Get number of non-zero elements.
    pub fn nnz(&self) -> usize {
        self.values.len()
    }
    
    /// Extract as coordinate triplets.
    pub fn to_triplets(&self) -> Result<Vec<(usize, usize, Precision)>> {
        let mut triplets = Vec::new();
        
        for col in 0..self.col_ptr.len() - 1 {
            let start = self.col_ptr[col] as usize;
            let end = self.col_ptr[col + 1] as usize;
            
            for i in start..end {
                let row = self.row_indices[i] as usize;
                let value = self.values[i];
                triplets.push((row, col, value));
            }
        }
        
        Ok(triplets)
    }
    
    /// Scale all values by a factor.
    pub fn scale(&mut self, factor: Precision) {
        for value in &mut self.values {
            *value *= factor;
        }
    }
    
    /// Add a value to the diagonal.
    pub fn add_diagonal(&mut self, alpha: Precision) {
        for col in 0..self.col_ptr.len() - 1 {
            let start = self.col_ptr[col] as usize;
            let end = self.col_ptr[col + 1] as usize;
            
            // Look for diagonal element
            if let Ok(pos) = self.row_indices[start..end].binary_search(&(col as IndexType)) {
                self.values[start + pos] += alpha;
            }
        }
    }
}

/// Iterator over non-zero elements in a CSC row (inefficient).
pub struct CSCRowIter<'a> {
    storage: &'a CSCStorage,
    row: IndexType,
    col: usize,
}

impl<'a> Iterator for CSCRowIter<'a> {
    type Item = (IndexType, Precision);
    
    fn next(&mut self) -> Option<Self::Item> {
        while self.col < self.storage.col_ptr.len() - 1 {
            let start = self.storage.col_ptr[self.col] as usize;
            let end = self.storage.col_ptr[self.col + 1] as usize;
            
            if let Ok(pos) = self.storage.row_indices[start..end].binary_search(&self.row) {
                let value = self.storage.values[start + pos];
                let col = self.col as IndexType;
                self.col += 1;
                return Some((col, value));
            }
            
            self.col += 1;
        }
        None
    }
}

/// Iterator over non-zero elements in a CSC column.
pub struct CSCColIter<'a> {
    row_indices: &'a [IndexType],
    values: &'a [Precision],
    pos: usize,
}

impl<'a> Iterator for CSCColIter<'a> {
    type Item = (IndexType, Precision);
    
    fn next(&mut self) -> Option<Self::Item> {
        if self.pos < self.row_indices.len() {
            let row = self.row_indices[self.pos];
            let val = self.values[self.pos];
            self.pos += 1;
            Some((row, val))
        } else {
            None
        }
    }
}

// COO Implementation
impl COOStorage {
    /// Create COO storage from triplets.
    pub fn from_triplets(triplets: Vec<(usize, usize, Precision)>) -> Result<Self> {
        let mut row_indices = Vec::new();
        let mut col_indices = Vec::new();
        let mut values = Vec::new();
        
        for (row, col, value) in triplets {
            if value != 0.0 {
                row_indices.push(row as IndexType);
                col_indices.push(col as IndexType);
                values.push(value);
            }
        }
        
        Ok(Self {
            row_indices,
            col_indices,
            values,
        })
    }
    
    /// Check if the storage is empty.
    pub fn is_empty(&self) -> bool {
        self.values.is_empty()
    }
    
    /// Get matrix element at (row, col) - O(n) search.
    pub fn get(&self, row: usize, col: usize) -> Option<Precision> {
        for i in 0..self.values.len() {
            if self.row_indices[i] as usize == row && self.col_indices[i] as usize == col {
                return Some(self.values[i]);
            }
        }
        None
    }
    
    /// Iterate over non-zero elements in a row.
    pub fn row_iter(&self, row: usize) -> COORowIter {
        COORowIter {
            storage: self,
            target_row: row as IndexType,
            pos: 0,
        }
    }
    
    /// Iterate over non-zero elements in a column.
    pub fn col_iter(&self, col: usize) -> COOColIter {
        COOColIter {
            storage: self,
            target_col: col as IndexType,
            pos: 0,
        }
    }
    
    /// Matrix-vector multiplication: result = A * x
    pub fn multiply_vector(&self, x: &[Precision], result: &mut [Precision]) {
        result.fill(0.0);
        self.multiply_vector_add(x, result);
    }
    
    /// Matrix-vector multiplication with accumulation: result += A * x
    pub fn multiply_vector_add(&self, x: &[Precision], result: &mut [Precision]) {
        for i in 0..self.values.len() {
            let row = self.row_indices[i] as usize;
            let col = self.col_indices[i] as usize;
            result[row] += self.values[i] * x[col];
        }
    }
    
    /// Get number of non-zero elements.
    pub fn nnz(&self) -> usize {
        self.values.len()
    }
    
    /// Extract as coordinate triplets.
    pub fn to_triplets(&self) -> Vec<(usize, usize, Precision)> {
        self.row_indices.iter()
            .zip(&self.col_indices)
            .zip(&self.values)
            .map(|((&r, &c), &v)| (r as usize, c as usize, v))
            .collect()
    }
    
    /// Scale all values by a factor.
    pub fn scale(&mut self, factor: Precision) {
        for value in &mut self.values {
            *value *= factor;
        }
    }
    
    /// Add a value to the diagonal.
    pub fn add_diagonal(&mut self, alpha: Precision, rows: DimensionType) {
        // For COO, we'd need to add new diagonal entries if they don't exist
        // This is a simplified implementation that only modifies existing diagonal entries
        for i in 0..self.values.len() {
            if self.row_indices[i] == self.col_indices[i] {
                self.values[i] += alpha;
            }
        }
    }
}

/// Iterator over non-zero elements in a COO row.
pub struct COORowIter<'a> {
    storage: &'a COOStorage,
    target_row: IndexType,
    pos: usize,
}

impl<'a> Iterator for COORowIter<'a> {
    type Item = (IndexType, Precision);
    
    fn next(&mut self) -> Option<Self::Item> {
        while self.pos < self.storage.values.len() {
            if self.storage.row_indices[self.pos] == self.target_row {
                let col = self.storage.col_indices[self.pos];
                let val = self.storage.values[self.pos];
                self.pos += 1;
                return Some((col, val));
            }
            self.pos += 1;
        }
        None
    }
}

/// Iterator over non-zero elements in a COO column.
pub struct COOColIter<'a> {
    storage: &'a COOStorage,
    target_col: IndexType,
    pos: usize,
}

impl<'a> Iterator for COOColIter<'a> {
    type Item = (IndexType, Precision);
    
    fn next(&mut self) -> Option<Self::Item> {
        while self.pos < self.storage.values.len() {
            if self.storage.col_indices[self.pos] == self.target_col {
                let row = self.storage.row_indices[self.pos];
                let val = self.storage.values[self.pos];
                self.pos += 1;
                return Some((row, val));
            }
            self.pos += 1;
        }
        None
    }
}

// Graph Implementation
impl GraphStorage {
    /// Create graph storage from triplets.
    pub fn from_triplets(triplets: Vec<(usize, usize, Precision)>, nodes: DimensionType) -> Result<Self> {
        let mut out_edges = vec![Vec::new(); nodes];
        let mut in_edges = vec![Vec::new(); nodes];
        let mut degrees = vec![0.0; nodes];
        
        for (row, col, weight) in triplets {
            if weight != 0.0 && row < nodes && col < nodes {
                out_edges[row].push(GraphEdge {
                    target: col as NodeId,
                    weight,
                });
                
                if row != col { // Don't double-count self-loops for in_edges
                    in_edges[col].push(GraphEdge {
                        target: row as NodeId,
                        weight,
                    });
                }
                
                degrees[row] += weight.abs();
            }
        }
        
        Ok(Self {
            out_edges,
            in_edges,
            degrees,
        })
    }
    
    /// Get matrix element at (row, col).
    pub fn get(&self, row: usize, col: usize) -> Option<Precision> {
        if row >= self.out_edges.len() {
            return None;
        }
        
        for edge in &self.out_edges[row] {
            if edge.target as usize == col {
                return Some(edge.weight);
            }
        }
        None
    }
    
    /// Iterate over non-zero elements in a row.
    pub fn row_iter(&self, row: usize) -> GraphRowIter {
        if row >= self.out_edges.len() {
            GraphRowIter {
                edges: &[],
                pos: 0,
            }
        } else {
            GraphRowIter {
                edges: &self.out_edges[row],
                pos: 0,
            }
        }
    }
    
    /// Iterate over non-zero elements in a column.
    pub fn col_iter(&self, col: usize) -> GraphColIter {
        if col >= self.in_edges.len() {
            GraphColIter {
                edges: &[],
                pos: 0,
            }
        } else {
            GraphColIter {
                edges: &self.in_edges[col],
                pos: 0,
            }
        }
    }
    
    /// Matrix-vector multiplication: result = A * x
    pub fn multiply_vector(&self, x: &[Precision], result: &mut [Precision]) {
        result.fill(0.0);
        self.multiply_vector_add(x, result);
    }
    
    /// Matrix-vector multiplication with accumulation: result += A * x
    pub fn multiply_vector_add(&self, x: &[Precision], result: &mut [Precision]) {
        for (row, edges) in self.out_edges.iter().enumerate() {
            for edge in edges {
                let col = edge.target as usize;
                if col < x.len() {
                    result[row] += edge.weight * x[col];
                }
            }
        }
    }
    
    /// Get number of non-zero elements.
    pub fn nnz(&self) -> usize {
        self.out_edges.iter().map(|edges| edges.len()).sum()
    }
    
    /// Extract as coordinate triplets.
    pub fn to_triplets(&self) -> Result<Vec<(usize, usize, Precision)>> {
        let mut triplets = Vec::new();
        
        for (row, edges) in self.out_edges.iter().enumerate() {
            for edge in edges {
                triplets.push((row, edge.target as usize, edge.weight));
            }
        }
        
        Ok(triplets)
    }
    
    /// Scale all edge weights by a factor.
    pub fn scale(&mut self, factor: Precision) {
        for edges in &mut self.out_edges {
            for edge in edges {
                edge.weight *= factor;
            }
        }
        
        for edges in &mut self.in_edges {
            for edge in edges {
                edge.weight *= factor;
            }
        }
        
        for degree in &mut self.degrees {
            *degree *= factor.abs();
        }
    }
    
    /// Add a value to the diagonal.
    pub fn add_diagonal(&mut self, alpha: Precision) {
        for (node, edges) in self.out_edges.iter_mut().enumerate() {
            // Look for self-loop
            let mut found = false;
            for edge in edges.iter_mut() {
                if edge.target as usize == node {
                    edge.weight += alpha;
                    found = true;
                    break;
                }
            }
            
            // Add self-loop if it doesn't exist
            if !found && alpha != 0.0 {
                edges.push(GraphEdge {
                    target: node as NodeId,
                    weight: alpha,
                });
            }
            
            // Update degree
            self.degrees[node] += alpha.abs();
        }
    }
    
    /// Get outgoing edges for a node.
    pub fn out_neighbors(&self, node: usize) -> &[GraphEdge] {
        if node < self.out_edges.len() {
            &self.out_edges[node]
        } else {
            &[]
        }
    }
    
    /// Get incoming edges for a node.
    pub fn in_neighbors(&self, node: usize) -> &[GraphEdge] {
        if node < self.in_edges.len() {
            &self.in_edges[node]
        } else {
            &[]
        }
    }
    
    /// Get node degree.
    pub fn degree(&self, node: usize) -> Precision {
        if node < self.degrees.len() {
            self.degrees[node]
        } else {
            0.0
        }
    }
}

/// Iterator over non-zero elements in a graph row.
pub struct GraphRowIter<'a> {
    edges: &'a [GraphEdge],
    pos: usize,
}

impl<'a> Iterator for GraphRowIter<'a> {
    type Item = (IndexType, Precision);
    
    fn next(&mut self) -> Option<Self::Item> {
        if self.pos < self.edges.len() {
            let edge = self.edges[self.pos];
            self.pos += 1;
            Some((edge.target, edge.weight))
        } else {
            None
        }
    }
}

/// Iterator over non-zero elements in a graph column.
pub struct GraphColIter<'a> {
    edges: &'a [GraphEdge],
    pos: usize,
}

impl<'a> Iterator for GraphColIter<'a> {
    type Item = (IndexType, Precision);
    
    fn next(&mut self) -> Option<Self::Item> {
        if self.pos < self.edges.len() {
            let edge = self.edges[self.pos];
            self.pos += 1;
            Some((edge.target, edge.weight))
        } else {
            None
        }
    }
}

#[cfg(all(test, feature = "std"))]
mod tests {
    use super::*;
    
    #[test]
    fn test_csr_creation() {
        let triplets = vec![(0, 0, 1.0), (0, 2, 2.0), (1, 1, 3.0), (2, 0, 4.0), (2, 2, 5.0)];
        let coo = COOStorage::from_triplets(triplets).unwrap();
        let csr = CSRStorage::from_coo(&coo, 3, 3).unwrap();
        
        assert_eq!(csr.nnz(), 5);
        assert_eq!(csr.get(0, 0), Some(1.0));
        assert_eq!(csr.get(0, 2), Some(2.0));
        assert_eq!(csr.get(1, 1), Some(3.0));
        assert_eq!(csr.get(0, 1), None);
    }
    
    #[test]
    fn test_csr_matrix_vector_multiply() {
        let triplets = vec![(0, 0, 2.0), (0, 1, 1.0), (1, 0, 1.0), (1, 1, 3.0)];
        let coo = COOStorage::from_triplets(triplets).unwrap();
        let csr = CSRStorage::from_coo(&coo, 2, 2).unwrap();
        
        let x = vec![1.0, 2.0];
        let mut result = vec![0.0; 2];
        
        csr.multiply_vector(&x, &mut result);
        assert_eq!(result, vec![4.0, 7.0]); // [2*1+1*2, 1*1+3*2]
    }
    
    #[test]
    fn test_graph_storage() {
        let triplets = vec![(0, 1, 0.5), (1, 0, 0.3), (1, 2, 0.7), (2, 1, 0.2)];
        let graph = GraphStorage::from_triplets(triplets, 3).unwrap();
        
        assert_eq!(graph.nnz(), 4);
        assert_eq!(graph.out_neighbors(1).len(), 2);
        assert_eq!(graph.in_neighbors(1).len(), 2);
        assert!(graph.degree(1) > 0.0);
    }
    
    #[test]
    fn test_format_conversions() {
        let triplets = vec![(0, 0, 1.0), (0, 2, 2.0), (1, 1, 3.0)];
        
        // COO -> CSR -> CSC -> COO roundtrip
        let coo1 = COOStorage::from_triplets(triplets.clone()).unwrap();
        let csr = CSRStorage::from_coo(&coo1, 2, 3).unwrap();
        let csc = CSCStorage::from_csr(&csr, 2, 3).unwrap();
        let triplets2 = csc.to_triplets().unwrap();
        
        // Sort both for comparison. `f64` does not implement `Ord` because of
        // NaN, so sort lexicographically on (row, col) — the values are
        // already exact small integers here, but we route them through
        // `total_cmp` rather than relying on PartialOrd.
        fn cmp_triplet(a: &(usize, usize, f64), b: &(usize, usize, f64)) -> std::cmp::Ordering {
            a.0.cmp(&b.0)
                .then_with(|| a.1.cmp(&b.1))
                .then_with(|| a.2.total_cmp(&b.2))
        }
        let mut t1 = triplets.clone();
        let mut t2 = triplets2;
        t1.sort_by(cmp_triplet);
        t2.sort_by(cmp_triplet);

        assert_eq!(t1, t2);
    }
}