scirs2-sparse 0.4.2

Sparse matrix module for SciRS2 (scirs2-sparse)
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
//! Symmetric Sparse Array trait
//!
//! This module defines a trait for symmetric sparse array implementations.
//! It extends the base SparseArray trait with methods specific to symmetric
//! matrices.

use crate::coo_array::CooArray;
use crate::csr_array::CsrArray;
use crate::error::{SparseError, SparseResult};
use crate::sparray::{SparseArray, SparseSum};
use crate::sym_coo::SymCooArray;
use crate::sym_csr::SymCsrArray;
use scirs2_core::numeric::{Float, SparseElement};
use std::fmt::Debug;
use std::ops::{Add, Div, Mul, Sub};

/// Trait for symmetric sparse arrays
///
/// Extends the base SparseArray trait with methods specific to
/// symmetric matrices. Implementations guarantee that the matrix
/// is kept symmetric throughout all operations.
pub trait SymSparseArray<T>: SparseArray<T>
where
    T: SparseElement + Div<Output = T> + Float + PartialOrd + 'static,
{
    /// Get the number of stored non-zero elements
    ///
    /// For symmetric formats, this returns the number of elements
    /// actually stored (typically lower or upper triangular part).
    ///
    /// # Returns
    ///
    /// Number of stored non-zero elements
    fn nnz_stored(&self) -> usize;

    /// Check if the matrix is guaranteed to be symmetric
    ///
    /// For implementations of this trait, this should always return true.
    /// It's included for consistency with checking functions.
    ///
    /// # Returns
    ///
    /// Always true for SymSparseArray implementations
    fn is_symmetric(&self) -> bool {
        true
    }

    /// Convert to standard CSR array
    ///
    /// # Returns
    ///
    /// A standard CSR array with the full symmetric matrix
    fn to_csr(&self) -> SparseResult<CsrArray<T>>;

    /// Convert to standard COO array
    ///
    /// # Returns
    ///
    /// A standard COO array with the full symmetric matrix
    fn to_coo(&self) -> SparseResult<CooArray<T>>;

    /// Convert to symmetric CSR array
    ///
    /// # Returns
    ///
    /// A symmetric CSR array
    fn to_sym_csr(&self) -> SparseResult<SymCsrArray<T>>;

    /// Convert to symmetric COO array
    ///
    /// # Returns
    ///
    /// A symmetric COO array
    fn to_sym_coo(&self) -> SparseResult<SymCooArray<T>>;
}

/// Implementation of SymSparseArray for SymCsrArray
impl<T> SymSparseArray<T> for SymCsrArray<T>
where
    T: Float
        + SparseElement
        + Debug
        + Copy
        + 'static
        + Add<Output = T>
        + Sub<Output = T>
        + Mul<Output = T>
        + Div<Output = T>
        + scirs2_core::simd_ops::SimdUnifiedOps
        + SparseElement
        + Send
        + Sync,
{
    fn nnz_stored(&self) -> usize {
        self.inner().nnz_stored()
    }

    fn to_csr(&self) -> SparseResult<CsrArray<T>> {
        self.to_csr_array()
    }

    fn to_coo(&self) -> SparseResult<CooArray<T>> {
        // Extract matrix data
        let csr_inner = self.inner();
        let shape = csr_inner.shape;

        // Convert to triplets format for full symmetric matrix
        let mut rows = Vec::new();
        let mut cols = Vec::new();
        let mut data = Vec::new();

        for i in 0..shape.0 {
            for j_ptr in csr_inner.indptr[i]..csr_inner.indptr[i + 1] {
                let j = csr_inner.indices[j_ptr];
                let val = csr_inner.data[j_ptr];

                // Add the element itself
                rows.push(i);
                cols.push(j);
                data.push(val);

                // Add its symmetric pair (if not diagonal)
                if i != j {
                    rows.push(j);
                    cols.push(i);
                    data.push(val);
                }
            }
        }

        // Create a COO array from the triplets
        CooArray::from_triplets(&rows, &cols, &data, shape, false)
    }

    fn to_sym_csr(&self) -> SparseResult<SymCsrArray<T>> {
        // Already a SymCsrArray, return a clone
        Ok(Clone::clone(self))
    }

    fn to_sym_coo(&self) -> SparseResult<SymCooArray<T>> {
        // Convert the internal SymCsrMatrix to SymCooMatrix
        let csr_inner = self.inner();

        // Extract data from CSR format into COO format (lower triangular part only)
        let mut data = Vec::new();
        let mut rows = Vec::new();
        let mut cols = Vec::new();

        for i in 0..csr_inner.shape.0 {
            for j in csr_inner.indptr[i]..csr_inner.indptr[i + 1] {
                let col = csr_inner.indices[j];
                let val = csr_inner.data[j];

                data.push(val);
                rows.push(i);
                cols.push(col);
            }
        }

        use crate::sym_coo::SymCooMatrix;
        let sym_coo = SymCooMatrix::new(data, rows, cols, csr_inner.shape)?;

        Ok(SymCooArray::new(sym_coo))
    }
}

/// Implementation of SymSparseArray for SymCooArray
impl<T> SymSparseArray<T> for SymCooArray<T>
where
    T: Float
        + SparseElement
        + Debug
        + Copy
        + 'static
        + Add<Output = T>
        + Sub<Output = T>
        + Mul<Output = T>
        + Div<Output = T>
        + scirs2_core::simd_ops::SimdUnifiedOps
        + SparseElement
        + Send
        + Sync,
{
    fn nnz_stored(&self) -> usize {
        self.inner().nnz_stored()
    }

    fn to_csr(&self) -> SparseResult<CsrArray<T>> {
        // Convert to full COO, then to CSR
        let coo = self.to_coo_array()?;
        match coo.to_csr() {
            Ok(boxed_csr) => {
                // Convert Box<dyn SparseArray<T>> to CsrArray<T>
                match boxed_csr.as_any().downcast_ref::<CsrArray<T>>() {
                    Some(csr_array) => Ok(csr_array.clone()),
                    None => Err(SparseError::ConversionError(
                        "Failed to downcast to CsrArray".to_string(),
                    )),
                }
            }
            Err(e) => Err(e),
        }
    }

    fn to_coo(&self) -> SparseResult<CooArray<T>> {
        self.to_coo_array()
    }

    fn to_sym_csr(&self) -> SparseResult<SymCsrArray<T>> {
        // We already have a symmetric COO matrix with only the lower triangular part
        // Let's create a CSR matrix directly from it
        let coo_inner = self.inner();

        // Extract the triplets data
        let data = coo_inner.data.clone();
        let rows = coo_inner.rows.clone();
        let cols = coo_inner.cols.clone();
        let shape = coo_inner.shape;

        // Create a new CsrMatrix from these triplets
        let csr = crate::csr::CsrMatrix::new(data, rows, cols, shape)?;

        // Create a symmetric CSR matrix without checking symmetry (we know it's symmetric)
        use crate::sym_csr::SymCsrMatrix;

        // Extract the lower triangular part (already in the correct format)
        let mut sym_data = Vec::new();
        let mut sym_indices = Vec::new();
        let mut sym_indptr = vec![0];
        let n = shape.0;

        for i in 0..n {
            for j_ptr in csr.indptr[i]..csr.indptr[i + 1] {
                let j = csr.indices[j_ptr];
                let val = csr.data[j_ptr];

                // Only include elements in lower triangular part (including diagonal)
                if j <= i {
                    sym_data.push(val);
                    sym_indices.push(j);
                }
            }

            sym_indptr.push(sym_data.len());
        }

        let sym_csr = SymCsrMatrix::new(sym_data, sym_indptr, sym_indices, shape)?;

        Ok(SymCsrArray::new(sym_csr))
    }

    fn to_sym_coo(&self) -> SparseResult<SymCooArray<T>> {
        // Already a SymCooArray, return a clone
        Ok(Clone::clone(self))
    }
}

/// Implementation of SparseArray for SymCsrArray
impl<T> SparseArray<T> for SymCsrArray<T>
where
    T: Float
        + SparseElement
        + Debug
        + Copy
        + 'static
        + Add<Output = T>
        + Sub<Output = T>
        + Mul<Output = T>
        + Div<Output = T>
        + scirs2_core::simd_ops::SimdUnifiedOps
        + SparseElement
        + Send
        + Sync,
{
    fn to_coo(&self) -> SparseResult<Box<dyn SparseArray<T>>> {
        let coo_array = <Self as SymSparseArray<T>>::to_coo(self)?;
        Ok(Box::new(coo_array))
    }

    fn to_csr(&self) -> SparseResult<Box<dyn SparseArray<T>>> {
        // Convert to CsrArray (full matrix)
        let csr = <Self as SymSparseArray<T>>::to_csr(self)?;
        Ok(Box::new(csr))
    }
    fn shape(&self) -> (usize, usize) {
        self.inner().shape()
    }

    fn nnz(&self) -> usize {
        self.inner().nnz()
    }

    fn dtype(&self) -> &str {
        if std::any::TypeId::of::<T>() == std::any::TypeId::of::<f32>() {
            "f32"
        } else {
            "f64"
        }
    }

    fn get(&self, row: usize, col: usize) -> T {
        self.inner().get(row, col)
    }

    fn to_array(&self) -> scirs2_core::ndarray::Array2<T> {
        // Convert to dense vector of vectors, then to ndarray
        let dense = self.inner().to_dense();
        let mut array = scirs2_core::ndarray::Array2::zeros(self.shape());

        for i in 0..dense.len() {
            for j in 0..dense[i].len() {
                array[[i, j]] = dense[i][j];
            }
        }

        array
    }

    fn toarray(&self) -> scirs2_core::ndarray::Array2<T> {
        self.to_array()
    }

    fn set(&mut self, i: usize, j: usize, value: T) -> SparseResult<()> {
        Err(SparseError::NotImplemented(
            "Setting individual elements in SymCsrArray is not supported. Convert to another format first.".to_string()
        ))
    }

    fn dot_vector(
        &self,
        other: &scirs2_core::ndarray::ArrayView1<T>,
    ) -> SparseResult<scirs2_core::ndarray::Array1<T>> {
        // Use optimized symmetric matrix-vector product
        crate::sym_ops::sym_csr_matvec(self.inner(), other)
    }

    fn copy(&self) -> Box<dyn SparseArray<T>> {
        Box::new(Clone::clone(self))
    }

    fn sub(&self, other: &dyn SparseArray<T>) -> SparseResult<Box<dyn SparseArray<T>>> {
        // Subtraction will preserve symmetry if other is also symmetric
        // For simplicity, we'll convert to CSR, perform subtraction, and convert back
        let self_csr = <Self as SymSparseArray<T>>::to_csr(self)?;
        let result = self_csr.sub(other)?;

        // For now, we return the CSR result without converting back
        Ok(result)
    }

    fn div(&self, other: &dyn SparseArray<T>) -> SparseResult<Box<dyn SparseArray<T>>> {
        // Division may not preserve symmetry, so we'll just return a CSR result
        let self_csr = <Self as SymSparseArray<T>>::to_csr(self)?;
        self_csr.div(other)
    }

    fn eliminate_zeros(&mut self) {
        // No-op for SymCsrArray as it already maintains minimal storage
    }

    fn sort_indices(&mut self) {
        // No-op for SymCsrArray as indices are already sorted
    }

    fn sorted_indices(&self) -> Box<dyn SparseArray<T>> {
        // CSR format typically maintains sorted indices, so return a clone
        Box::new(Clone::clone(self))
    }

    fn has_sorted_indices(&self) -> bool {
        true
    }

    fn sum(&self, axis: Option<usize>) -> SparseResult<SparseSum<T>> {
        // Convert to CSR and use its implementation
        let csr = <Self as SymSparseArray<T>>::to_csr(self)?;
        SparseArray::<T>::sum(&csr, axis)
    }

    fn max(&self) -> T {
        // Convert to CSR and find the maximum value
        match <Self as SymSparseArray<T>>::to_csr(self) {
            Ok(csr) => SparseArray::<T>::max(&csr),
            Err(_) => T::nan(), // Return NaN if conversion fails
        }
    }

    fn min(&self) -> T {
        // Convert to CSR and find the minimum value
        match <Self as SymSparseArray<T>>::to_csr(self) {
            Ok(csr) => SparseArray::<T>::min(&csr),
            Err(_) => T::nan(), // Return NaN if conversion fails
        }
    }

    fn slice(
        &self,
        rows: (usize, usize),
        cols: (usize, usize),
    ) -> SparseResult<Box<dyn SparseArray<T>>> {
        // Slicing a symmetric matrix may not preserve symmetry
        // Convert to CSR and use its implementation
        let csr = <Self as SymSparseArray<T>>::to_csr(self)?;
        csr.slice(rows, cols)
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }

    fn find(
        &self,
    ) -> (
        scirs2_core::ndarray::Array1<usize>,
        scirs2_core::ndarray::Array1<usize>,
        scirs2_core::ndarray::Array1<T>,
    ) {
        // To get the full matrix coordinates and values, we need to convert to a full CSR matrix
        match <Self as SymSparseArray<T>>::to_csr(self) {
            Ok(csr) => csr.find(),
            Err(_) => (
                scirs2_core::ndarray::Array1::from_vec(Vec::new()),
                scirs2_core::ndarray::Array1::from_vec(Vec::new()),
                scirs2_core::ndarray::Array1::from_vec(Vec::new()),
            ),
        }
    }

    fn add(&self, other: &dyn SparseArray<T>) -> SparseResult<Box<dyn SparseArray<T>>> {
        // First check shapes are compatible
        let selfshape = self.shape();
        let othershape = other.shape();

        if selfshape != othershape {
            return Err(crate::error::SparseError::DimensionMismatch {
                expected: selfshape.0,
                found: othershape.0,
            });
        }

        // Convert both to CSR to perform addition
        let self_csr = <Self as SymSparseArray<T>>::to_csr(self)?;
        let other_csr_box = other.to_csr()?;

        // We need to unbox the other_csr to use it directly
        match other_csr_box.as_any().downcast_ref::<CsrArray<T>>() {
            Some(other_csr) => {
                // Add as standard CSR arrays
                let result = self_csr.add(other_csr)?;

                // Since we expect result to be symmetric (if the other was symmetric),
                // we can try to convert it back to a SymCsrArray

                // First convert the result to CSR Matrix
                use crate::csr::CsrMatrix;

                // We need to get the data from the result
                let (rows, cols, data) = result.find();
                let csr_matrix =
                    CsrMatrix::new(data.to_vec(), rows.to_vec(), cols.to_vec(), result.shape())?;

                // Convert to SymCsrMatrix
                use crate::sym_csr::SymCsrMatrix;
                let sym_csr = SymCsrMatrix::from_csr(&csr_matrix)?;

                // Create and return SymCsrArray
                let sym_csr_array = SymCsrArray::new(sym_csr);
                Ok(Box::new(sym_csr_array) as Box<dyn SparseArray<T>>)
            }
            None => {
                // If we can't downcast, convert both to dense arrays and add them
                let self_dense = self.to_array();
                let other_dense = other.to_array();

                // Create the result dense array
                let mut result_dense = scirs2_core::ndarray::Array2::zeros(selfshape);
                for i in 0..selfshape.0 {
                    for j in 0..selfshape.1 {
                        result_dense[[i, j]] = self_dense[[i, j]] + other_dense[[i, j]];
                    }
                }

                // Convert back to CSR
                // Convert result_dense to triplets
                let mut rows = Vec::new();
                let mut cols = Vec::new();
                let mut values = Vec::new();

                for i in 0..selfshape.0 {
                    for j in 0..selfshape.1 {
                        let val = result_dense[[i, j]];
                        if val != T::sparse_zero() {
                            rows.push(i);
                            cols.push(j);
                            values.push(val);
                        }
                    }
                }

                let csr = CsrArray::from_triplets(&rows, &cols, &values, selfshape, false)?;
                Ok(Box::new(csr) as Box<dyn SparseArray<T>>)
            }
        }
    }

    fn mul(&self, other: &dyn SparseArray<T>) -> SparseResult<Box<dyn SparseArray<T>>> {
        // First check shapes are compatible
        let selfshape = self.shape();
        let othershape = other.shape();

        if selfshape != othershape {
            return Err(crate::error::SparseError::DimensionMismatch {
                expected: selfshape.0,
                found: othershape.0,
            });
        }

        // Convert both to CSR to perform multiplication
        let self_csr = <Self as SymSparseArray<T>>::to_csr(self)?;
        let other_csr_box = other.to_csr()?;

        // We need to unbox the other_csr to use it directly
        match other_csr_box.as_any().downcast_ref::<CsrArray<T>>() {
            Some(other_csr) => {
                // Multiply as standard CSR arrays (element-wise)
                let result = self_csr.mul(other_csr)?;

                // Element-wise multiplication of symmetric matrices preserves symmetry,
                // so we can convert back to SymCsrArray

                // We need to get the data from the result
                let (rows, cols, data) = result.find();

                // Convert to CsrMatrix
                use crate::csr::CsrMatrix;
                let csr_matrix =
                    CsrMatrix::new(data.to_vec(), rows.to_vec(), cols.to_vec(), result.shape())?;

                // Convert to SymCsrMatrix
                use crate::sym_csr::SymCsrMatrix;
                let sym_csr = SymCsrMatrix::from_csr(&csr_matrix)?;

                // Create and return SymCsrArray
                let sym_csr_array = SymCsrArray::new(sym_csr);
                Ok(Box::new(sym_csr_array) as Box<dyn SparseArray<T>>)
            }
            None => {
                // If we can't downcast, convert both to dense arrays and multiply them
                let self_dense = self.to_array();
                let other_dense = other.to_array();

                // Create the result dense array
                let mut result_dense = scirs2_core::ndarray::Array2::zeros(selfshape);
                for i in 0..selfshape.0 {
                    for j in 0..selfshape.1 {
                        result_dense[[i, j]] = self_dense[[i, j]] * other_dense[[i, j]];
                    }
                }

                // Convert back to CSR
                // Convert result_dense to triplets
                let mut rows = Vec::new();
                let mut cols = Vec::new();
                let mut values = Vec::new();

                for i in 0..selfshape.0 {
                    for j in 0..selfshape.1 {
                        let val = result_dense[[i, j]];
                        if val != T::sparse_zero() {
                            rows.push(i);
                            cols.push(j);
                            values.push(val);
                        }
                    }
                }

                let csr = CsrArray::from_triplets(&rows, &cols, &values, selfshape, false)?;
                Ok(Box::new(csr) as Box<dyn SparseArray<T>>)
            }
        }
    }

    fn dot(&self, other: &dyn SparseArray<T>) -> SparseResult<Box<dyn SparseArray<T>>> {
        // For matrix multiplication, symmetry is not generally preserved
        // So we just convert to standard CSR, perform the operation, and return a CSR array

        let self_csr = <Self as SymSparseArray<T>>::to_csr(self)?;
        let result = self_csr.dot(other)?;

        // For dot product of a symmetric matrix with itself, the result is symmetric
        // We could check for this case and return a SymCsrArray, but for now we'll
        // keep it simple and just return the CSR array
        Ok(result)
    }

    fn transpose(&self) -> SparseResult<Box<dyn SparseArray<T>>> {
        // For symmetric matrices, transpose is the same as the original
        Ok(Box::new(Clone::clone(self)))
    }

    fn to_csc(&self) -> SparseResult<Box<dyn SparseArray<T>>> {
        // Convert to CSR, then to CSC
        let csr = <Self as SymSparseArray<T>>::to_csr(self)?;
        let csr_box: Box<dyn SparseArray<T>> = Box::new(csr);
        csr_box.to_csc()
    }

    fn to_dia(&self) -> SparseResult<Box<dyn SparseArray<T>>> {
        // Convert to CSR, then to DIA
        let csr = <Self as SymSparseArray<T>>::to_csr(self)?;
        let csr_box: Box<dyn SparseArray<T>> = Box::new(csr);
        csr_box.to_dia()
    }

    fn to_dok(&self) -> SparseResult<Box<dyn SparseArray<T>>> {
        // Convert to CSR, then to DOK
        let csr = <Self as SymSparseArray<T>>::to_csr(self)?;
        let csr_box: Box<dyn SparseArray<T>> = Box::new(csr);
        csr_box.to_dok()
    }

    fn to_lil(&self) -> SparseResult<Box<dyn SparseArray<T>>> {
        // Convert to CSR, then to LIL
        let csr = <Self as SymSparseArray<T>>::to_csr(self)?;
        let csr_box: Box<dyn SparseArray<T>> = Box::new(csr);
        csr_box.to_lil()
    }

    fn to_bsr(&self) -> SparseResult<Box<dyn SparseArray<T>>> {
        // Convert to CSR, then to BSR
        let csr = <Self as SymSparseArray<T>>::to_csr(self)?;
        let csr_box: Box<dyn SparseArray<T>> = Box::new(csr);
        csr_box.to_bsr()
    }
}

/// Implementation of SparseArray for SymCooArray
impl<T> SparseArray<T> for SymCooArray<T>
where
    T: Float
        + SparseElement
        + Debug
        + Copy
        + 'static
        + Add<Output = T>
        + Sub<Output = T>
        + Mul<Output = T>
        + Div<Output = T>
        + scirs2_core::simd_ops::SimdUnifiedOps
        + SparseElement
        + Send
        + Sync,
{
    fn to_coo(&self) -> SparseResult<Box<dyn SparseArray<T>>> {
        let coo_array = <Self as SymSparseArray<T>>::to_coo(self)?;
        Ok(Box::new(coo_array))
    }

    fn to_csr(&self) -> SparseResult<Box<dyn SparseArray<T>>> {
        // Convert to CsrArray (full matrix)
        let csr = <Self as SymSparseArray<T>>::to_csr(self)?;
        Ok(Box::new(csr))
    }
    fn shape(&self) -> (usize, usize) {
        self.inner().shape()
    }

    fn nnz(&self) -> usize {
        self.inner().nnz()
    }

    fn dtype(&self) -> &str {
        if std::any::TypeId::of::<T>() == std::any::TypeId::of::<f32>() {
            "f32"
        } else {
            "f64"
        }
    }

    fn get(&self, row: usize, col: usize) -> T {
        self.inner().get(row, col)
    }

    fn to_array(&self) -> scirs2_core::ndarray::Array2<T> {
        // Convert to dense vector of vectors, then to ndarray
        let dense = self.inner().to_dense();
        let mut array = scirs2_core::ndarray::Array2::zeros(self.shape());

        for i in 0..dense.len() {
            for j in 0..dense[i].len() {
                array[[i, j]] = dense[i][j];
            }
        }

        array
    }

    fn toarray(&self) -> scirs2_core::ndarray::Array2<T> {
        self.to_array()
    }

    fn set(&mut self, i: usize, j: usize, value: T) -> SparseResult<()> {
        Err(SparseError::NotImplemented(
            "Setting individual elements in SymCooArray is not supported. Convert to another format first.".to_string()
        ))
    }

    fn dot_vector(
        &self,
        other: &scirs2_core::ndarray::ArrayView1<T>,
    ) -> SparseResult<scirs2_core::ndarray::Array1<T>> {
        // Use optimized symmetric matrix-vector product
        crate::sym_ops::sym_coo_matvec(self.inner(), other)
    }

    fn copy(&self) -> Box<dyn SparseArray<T>> {
        Box::new(Clone::clone(self))
    }

    fn sub(&self, other: &dyn SparseArray<T>) -> SparseResult<Box<dyn SparseArray<T>>> {
        // For simplicity, we'll use the CSR implementation
        let self_csr = <Self as SymSparseArray<T>>::to_csr(self)?;
        self_csr.sub(other)
    }

    fn div(&self, other: &dyn SparseArray<T>) -> SparseResult<Box<dyn SparseArray<T>>> {
        // For simplicity, we'll use the CSR implementation
        let self_csr = <Self as SymSparseArray<T>>::to_csr(self)?;
        self_csr.div(other)
    }

    fn eliminate_zeros(&mut self) {
        // Not implemented for SymCooArray
        // Could be implemented by filtering out zero values in the future
    }

    fn sort_indices(&mut self) {
        // Not implemented for SymCooArray
        // Could be implemented by sorting indices by row, then column in the future
    }

    fn sorted_indices(&self) -> Box<dyn SparseArray<T>> {
        // Convert to SymCsrArray which has sorted indices
        match self.to_sym_csr() {
            Ok(csr) => Box::new(csr),
            Err(_) => Box::new(Clone::clone(self)), // Return self if conversion fails
        }
    }

    fn has_sorted_indices(&self) -> bool {
        false
    }

    fn sum(&self, axis: Option<usize>) -> SparseResult<SparseSum<T>> {
        // Convert to CSR and use its implementation
        let csr = <Self as SymSparseArray<T>>::to_csr(self)?;
        SparseArray::<T>::sum(&csr, axis)
    }

    fn max(&self) -> T {
        // Convert to CSR and find the maximum value
        match <Self as SymSparseArray<T>>::to_csr(self) {
            Ok(csr) => SparseArray::<T>::max(&csr),
            Err(_) => T::nan(), // Return NaN if conversion fails
        }
    }

    fn min(&self) -> T {
        // Convert to CSR and find the minimum value
        match <Self as SymSparseArray<T>>::to_csr(self) {
            Ok(csr) => SparseArray::<T>::min(&csr),
            Err(_) => T::nan(), // Return NaN if conversion fails
        }
    }

    fn slice(
        &self,
        rows: (usize, usize),
        cols: (usize, usize),
    ) -> SparseResult<Box<dyn SparseArray<T>>> {
        // Convert to CSR and use its implementation
        let csr = <Self as SymSparseArray<T>>::to_csr(self)?;
        csr.slice(rows, cols)
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }

    fn find(
        &self,
    ) -> (
        scirs2_core::ndarray::Array1<usize>,
        scirs2_core::ndarray::Array1<usize>,
        scirs2_core::ndarray::Array1<T>,
    ) {
        // To get the full matrix coordinates and values, we need to convert to a full COO matrix
        match <Self as SymSparseArray<T>>::to_coo(self) {
            Ok(coo) => coo.find(),
            Err(_) => (
                scirs2_core::ndarray::Array1::from_vec(Vec::new()),
                scirs2_core::ndarray::Array1::from_vec(Vec::new()),
                scirs2_core::ndarray::Array1::from_vec(Vec::new()),
            ),
        }
    }

    fn add(&self, other: &dyn SparseArray<T>) -> SparseResult<Box<dyn SparseArray<T>>> {
        // Convert to SymCsrArray and use its implementation
        self.to_sym_csr()?.add(other)
    }

    fn mul(&self, other: &dyn SparseArray<T>) -> SparseResult<Box<dyn SparseArray<T>>> {
        // Convert to SymCsrArray and use its implementation
        self.to_sym_csr()?.mul(other)
    }

    fn dot(&self, other: &dyn SparseArray<T>) -> SparseResult<Box<dyn SparseArray<T>>> {
        // Convert to CSR for dot product
        <Self as SymSparseArray<T>>::to_csr(self)?.dot(other)
    }

    fn transpose(&self) -> SparseResult<Box<dyn SparseArray<T>>> {
        // For symmetric matrices, transpose is the same as the original
        Ok(Box::new(Clone::clone(self)))
    }

    fn to_csc(&self) -> SparseResult<Box<dyn SparseArray<T>>> {
        // Convert to CSR, then to CSC
        let csr = <Self as SymSparseArray<T>>::to_csr(self)?;
        let csr_box: Box<dyn SparseArray<T>> = Box::new(csr);
        csr_box.to_csc()
    }

    fn to_dia(&self) -> SparseResult<Box<dyn SparseArray<T>>> {
        // Convert to CSR, then to DIA
        let csr = <Self as SymSparseArray<T>>::to_csr(self)?;
        let csr_box: Box<dyn SparseArray<T>> = Box::new(csr);
        csr_box.to_dia()
    }

    fn to_dok(&self) -> SparseResult<Box<dyn SparseArray<T>>> {
        // Convert to CSR, then to DOK
        let csr = <Self as SymSparseArray<T>>::to_csr(self)?;
        let csr_box: Box<dyn SparseArray<T>> = Box::new(csr);
        csr_box.to_dok()
    }

    fn to_lil(&self) -> SparseResult<Box<dyn SparseArray<T>>> {
        // Convert to CSR, then to LIL
        let csr = <Self as SymSparseArray<T>>::to_csr(self)?;
        let csr_box: Box<dyn SparseArray<T>> = Box::new(csr);
        csr_box.to_lil()
    }

    fn to_bsr(&self) -> SparseResult<Box<dyn SparseArray<T>>> {
        // Convert to CSR, then to BSR
        let csr = <Self as SymSparseArray<T>>::to_csr(self)?;
        let csr_box: Box<dyn SparseArray<T>> = Box::new(csr);
        csr_box.to_bsr()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::sym_csr::{SymCsrArray, SymCsrMatrix};

    // Create a simple symmetric matrix for testing
    fn create_test_sym_csr() -> SymCsrArray<f64> {
        // Create a simple symmetric matrix in CSR format
        // [2 1 0]
        // [1 2 3]
        // [0 3 1]

        // Lower triangular part only:
        // [2 0 0]
        // [1 2 0]
        // [0 3 1]

        let data = vec![2.0, 1.0, 2.0, 3.0, 1.0];
        let indices = vec![0, 0, 1, 1, 2];
        let indptr = vec![0, 1, 3, 5];

        let sym_matrix =
            SymCsrMatrix::new(data, indptr, indices, (3, 3)).expect("Operation failed");
        SymCsrArray::new(sym_matrix)
    }

    #[test]
    fn test_sym_sparse_array_trait() {
        let sym_csr = create_test_sym_csr();

        // Test basic properties
        assert_eq!(sym_csr.shape(), (3, 3));
        assert!(sym_csr.is_symmetric());

        // Test SparseArray methods
        assert_eq!(sym_csr.get(0, 0), 2.0);
        assert_eq!(sym_csr.get(0, 1), 1.0);
        assert_eq!(sym_csr.get(1, 0), 1.0); // Through symmetry

        // Test nnz_stored vs nnz
        assert_eq!(sym_csr.nnz_stored(), 5); // Only stored elements
        assert_eq!(sym_csr.nnz(), 7); // Including symmetric pairs

        // Test conversion between formats
        let sym_coo = sym_csr.to_sym_coo().expect("Operation failed");
        assert_eq!(sym_coo.shape(), (3, 3));
        assert!(sym_coo.is_symmetric());

        let csr = SymSparseArray::<f64>::to_csr(&sym_csr).expect("Operation failed");
        assert_eq!(csr.shape(), (3, 3));

        let coo = SymSparseArray::<f64>::to_coo(&sym_csr).expect("Operation failed");
        assert_eq!(coo.shape(), (3, 3));

        // Test that find() returns the full matrix elements
        let (rows, _cols, _data) = sym_csr.find();
        assert!(rows.len() > sym_csr.nnz_stored()); // Should include symmetric pairs
    }

    #[test]
    fn test_sym_sparse_array_operations() {
        let sym_csr = create_test_sym_csr();

        // Create another symmetric matrix for testing operations
        let sym_csr2 = create_test_sym_csr();

        // Test addition
        let sum = sym_csr.add(&sym_csr2).expect("Operation failed");
        assert_eq!(sum.shape(), (3, 3));
        assert_eq!(sum.get(0, 0), 4.0); // 2 + 2
        assert_eq!(sum.get(0, 1), 2.0); // 1 + 1
        assert_eq!(sum.get(1, 0), 2.0); // 1 + 1 (symmetric)

        // Test element-wise multiplication
        let prod = sym_csr.mul(&sym_csr2).expect("Operation failed");
        assert_eq!(prod.shape(), (3, 3));
        assert_eq!(prod.get(0, 0), 4.0); // 2 * 2
        assert_eq!(prod.get(0, 1), 1.0); // 1 * 1
        assert_eq!(prod.get(1, 0), 1.0); // 1 * 1 (symmetric)

        // Test matrix multiplication
        let dot = sym_csr.dot(&sym_csr2).expect("Operation failed");
        assert_eq!(dot.shape(), (3, 3));

        // Test transpose (should be no change for symmetric matrices)
        let trans = sym_csr.transpose().expect("Operation failed");
        assert_eq!(trans.shape(), sym_csr.shape());
        assert_eq!(SparseArray::get(&*trans, 0, 1), sym_csr.get(0, 1));
        assert_eq!(SparseArray::get(&*trans, 1, 0), sym_csr.get(1, 0));
    }
}