oxiblas-sparse 0.2.2

Sparse matrix support for OxiBLAS
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
//! ELLPACK (ELL) sparse matrix format.
//!
//! ELL stores matrix data using:
//! - `data`: 2D array of shape (nrows × max_nnz_per_row)
//! - `indices`: 2D array of column indices, same shape as data
//!
//! For an m×n matrix:
//! - Each row stores exactly `max_nnz_per_row` entries
//! - Rows with fewer non-zeros are padded with zeros and invalid indices
//!
//! # When to Use ELL
//!
//! ELL format is optimal for:
//! - Matrices with roughly uniform number of non-zeros per row
//! - GPU computation (enables coalesced memory access)
//! - Vector processors with SIMD operations
//!
//! ELL is NOT efficient for:
//! - Matrices with varying non-zeros per row (wastes memory on padding)
//! - Power-law graphs (a few rows have many entries)

use oxiblas_core::scalar::{Field, Scalar};

/// Error type for ELL matrix operations.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum EllError {
    /// Data array has wrong dimensions.
    InvalidDataDimensions {
        /// Expected rows.
        expected_rows: usize,
        /// Actual rows.
        actual_rows: usize,
        /// Expected columns per row.
        expected_width: usize,
        /// Actual columns per row.
        actual_width: usize,
    },
    /// Data and indices have different dimensions.
    DimensionMismatch {
        /// Data dimensions (rows, width).
        data_dims: (usize, usize),
        /// Indices dimensions (rows, width).
        indices_dims: (usize, usize),
    },
    /// Column index out of bounds.
    InvalidColumnIndex {
        /// Row where invalid index found.
        row: usize,
        /// Position within row.
        pos: usize,
        /// The invalid index.
        index: usize,
        /// Number of columns.
        ncols: usize,
    },
    /// Too many non-zeros in a row.
    TooManyNonZeros {
        /// Row with too many non-zeros.
        row: usize,
        /// Number of non-zeros.
        nnz: usize,
        /// Maximum allowed.
        max_nnz: usize,
    },
}

impl core::fmt::Display for EllError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::InvalidDataDimensions {
                expected_rows,
                actual_rows,
                expected_width,
                actual_width,
            } => {
                write!(
                    f,
                    "Invalid data dimensions: expected {expected_rows}×{expected_width}, got {actual_rows}×{actual_width}"
                )
            }
            Self::DimensionMismatch {
                data_dims,
                indices_dims,
            } => {
                write!(
                    f,
                    "Dimension mismatch: data is {}×{}, indices is {}×{}",
                    data_dims.0, data_dims.1, indices_dims.0, indices_dims.1
                )
            }
            Self::InvalidColumnIndex {
                row,
                pos,
                index,
                ncols,
            } => {
                write!(
                    f,
                    "Invalid column index {index} at row {row}, position {pos} (ncols={ncols})"
                )
            }
            Self::TooManyNonZeros { row, nnz, max_nnz } => {
                write!(f, "Row {row} has {nnz} non-zeros, exceeds max {max_nnz}")
            }
        }
    }
}

impl std::error::Error for EllError {}

/// ELLPACK sparse matrix format.
///
/// Efficient for:
/// - GPU computation
/// - Vectorized operations
/// - Matrices with uniform row lengths
///
/// # Storage
///
/// Each row stores exactly `width` entries. The `width` is typically the maximum
/// number of non-zeros in any row. Rows with fewer non-zeros are padded with
/// zeros and a special "invalid" column index (usually ncols or usize::MAX).
///
/// # Example
///
/// ```
/// use oxiblas_sparse::EllMatrix;
///
/// // Create a sparse matrix:
/// // [1 2 0 0]
/// // [0 3 4 0]
/// // [5 0 0 6]
/// let width = 2; // max 2 non-zeros per row
/// let data = vec![
///     vec![1.0, 2.0],  // row 0
///     vec![3.0, 4.0],  // row 1
///     vec![5.0, 6.0],  // row 2
/// ];
/// let indices = vec![
///     vec![0, 1],  // row 0
///     vec![1, 2],  // row 1
///     vec![0, 3],  // row 2
/// ];
///
/// let ell = EllMatrix::new(3, 4, width, data, indices).unwrap();
/// assert_eq!(ell.width(), 2);
/// ```
#[derive(Debug, Clone)]
pub struct EllMatrix<T: Scalar> {
    /// Number of rows.
    nrows: usize,
    /// Number of columns.
    ncols: usize,
    /// Maximum number of non-zeros per row (width of data/indices arrays).
    width: usize,
    /// Data array: data[row][k] is the k-th non-zero value in row.
    data: Vec<Vec<T>>,
    /// Column indices: indices[row][k] is the column of data[row][k].
    indices: Vec<Vec<usize>>,
}

/// Sentinel value for invalid/padding column indices.
const INVALID_INDEX: usize = usize::MAX;

impl<T: Scalar + Clone> EllMatrix<T> {
    /// Creates a new ELL matrix from raw components.
    ///
    /// # Arguments
    ///
    /// * `nrows` - Number of rows
    /// * `ncols` - Number of columns
    /// * `width` - Maximum non-zeros per row
    /// * `data` - Data array (nrows × width)
    /// * `indices` - Column indices array (nrows × width)
    ///
    /// # Errors
    ///
    /// Returns an error if the input is invalid.
    pub fn new(
        nrows: usize,
        ncols: usize,
        width: usize,
        data: Vec<Vec<T>>,
        indices: Vec<Vec<usize>>,
    ) -> Result<Self, EllError> {
        // Validate data dimensions
        if data.len() != nrows {
            return Err(EllError::InvalidDataDimensions {
                expected_rows: nrows,
                actual_rows: data.len(),
                expected_width: width,
                actual_width: if data.is_empty() { 0 } else { data[0].len() },
            });
        }

        for (i, row) in data.iter().enumerate() {
            if row.len() != width {
                return Err(EllError::InvalidDataDimensions {
                    expected_rows: nrows,
                    actual_rows: data.len(),
                    expected_width: width,
                    actual_width: row.len(),
                });
            }

            // Check corresponding indices row
            if i < indices.len() && indices[i].len() != width {
                return Err(EllError::DimensionMismatch {
                    data_dims: (nrows, width),
                    indices_dims: (indices.len(), indices[i].len()),
                });
            }
        }

        // Validate indices dimensions
        if indices.len() != nrows {
            return Err(EllError::DimensionMismatch {
                data_dims: (nrows, width),
                indices_dims: (
                    indices.len(),
                    if indices.is_empty() {
                        0
                    } else {
                        indices[0].len()
                    },
                ),
            });
        }

        // Validate column indices
        for (row, row_indices) in indices.iter().enumerate() {
            for (pos, &col) in row_indices.iter().enumerate() {
                if col != INVALID_INDEX && col >= ncols {
                    return Err(EllError::InvalidColumnIndex {
                        row,
                        pos,
                        index: col,
                        ncols,
                    });
                }
            }
        }

        Ok(Self {
            nrows,
            ncols,
            width,
            data,
            indices,
        })
    }

    /// Creates an ELL matrix without validation (unsafe but faster).
    ///
    /// # Safety
    ///
    /// The caller must ensure:
    /// - `data.len() == nrows` and each row has length `width`
    /// - `indices.len() == nrows` and each row has length `width`
    /// - All valid column indices are < ncols
    #[inline]
    pub unsafe fn new_unchecked(
        nrows: usize,
        ncols: usize,
        width: usize,
        data: Vec<Vec<T>>,
        indices: Vec<Vec<usize>>,
    ) -> Self {
        Self {
            nrows,
            ncols,
            width,
            data,
            indices,
        }
    }

    /// Creates an empty ELL matrix with given dimensions.
    pub fn zeros(nrows: usize, ncols: usize) -> Self {
        Self {
            nrows,
            ncols,
            width: 0,
            data: vec![Vec::new(); nrows],
            indices: vec![Vec::new(); nrows],
        }
    }

    /// Creates an identity matrix in ELL format.
    pub fn eye(n: usize) -> Self
    where
        T: Field,
    {
        Self {
            nrows: n,
            ncols: n,
            width: 1,
            data: (0..n).map(|_| vec![T::one()]).collect(),
            indices: (0..n).map(|i| vec![i]).collect(),
        }
    }

    /// Returns the number of rows.
    #[inline]
    pub fn nrows(&self) -> usize {
        self.nrows
    }

    /// Returns the number of columns.
    #[inline]
    pub fn ncols(&self) -> usize {
        self.ncols
    }

    /// Returns the shape (nrows, ncols).
    #[inline]
    pub fn shape(&self) -> (usize, usize) {
        (self.nrows, self.ncols)
    }

    /// Returns the width (max non-zeros per row).
    #[inline]
    pub fn width(&self) -> usize {
        self.width
    }

    /// Returns the number of non-zero elements.
    ///
    /// Note: This counts actual non-zeros, not stored values.
    pub fn nnz(&self) -> usize
    where
        T: Field,
    {
        let eps = <T as Scalar>::epsilon();
        let mut count = 0;

        for (row, indices_row) in self.indices.iter().enumerate() {
            for (k, &col) in indices_row.iter().enumerate() {
                if col != INVALID_INDEX && Scalar::abs(self.data[row][k].clone()) > eps {
                    count += 1;
                }
            }
        }

        count
    }

    /// Returns the total stored values (including padding).
    #[inline]
    pub fn nstored(&self) -> usize {
        self.nrows * self.width
    }

    /// Returns the storage efficiency (nnz / nstored).
    pub fn efficiency(&self) -> f64
    where
        T: Field,
    {
        if self.nstored() == 0 {
            1.0
        } else {
            self.nnz() as f64 / self.nstored() as f64
        }
    }

    /// Returns a reference to the data array.
    #[inline]
    pub fn data(&self) -> &[Vec<T>] {
        &self.data
    }

    /// Returns a mutable reference to the data array.
    #[inline]
    pub fn data_mut(&mut self) -> &mut [Vec<T>] {
        &mut self.data
    }

    /// Returns a reference to the indices array.
    #[inline]
    pub fn indices(&self) -> &[Vec<usize>] {
        &self.indices
    }

    /// Gets the value at (row, col), returning None if not present.
    pub fn get(&self, row: usize, col: usize) -> Option<&T> {
        if row >= self.nrows || col >= self.ncols {
            return None;
        }

        for k in 0..self.width {
            if self.indices[row][k] == col {
                return Some(&self.data[row][k]);
            }
        }

        None
    }

    /// Gets the value at (row, col), returning zero if not present.
    pub fn get_or_zero(&self, row: usize, col: usize) -> T
    where
        T: Field,
    {
        self.get(row, col).cloned().unwrap_or_else(T::zero)
    }

    /// Returns an iterator over non-zeros in a row as (col, value).
    pub fn row_iter(&self, row: usize) -> impl Iterator<Item = (usize, &T)> {
        self.indices[row]
            .iter()
            .zip(self.data[row].iter())
            .filter(|(col, _)| **col != INVALID_INDEX)
            .map(|(col, val)| (*col, val))
    }

    /// Returns an iterator over all non-zeros as (row, col, value).
    pub fn iter(&self) -> impl Iterator<Item = (usize, usize, &T)> + '_ {
        (0..self.nrows).flat_map(move |row| {
            self.indices[row]
                .iter()
                .zip(self.data[row].iter())
                .filter(|(col, _)| **col != INVALID_INDEX)
                .map(move |(col, val)| (row, *col, val))
        })
    }

    /// Matrix-vector product: y = A * x.
    pub fn matvec(&self, x: &[T], y: &mut [T])
    where
        T: Field,
    {
        assert_eq!(x.len(), self.ncols, "x length must equal ncols");
        assert_eq!(y.len(), self.nrows, "y length must equal nrows");

        for row in 0..self.nrows {
            let mut sum = T::zero();
            for k in 0..self.width {
                let col = self.indices[row][k];
                if col != INVALID_INDEX {
                    sum = sum + self.data[row][k].clone() * x[col].clone();
                }
            }
            y[row] = sum;
        }
    }

    /// Matrix-vector product returning a new vector: y = A * x.
    pub fn mul_vec(&self, x: &[T]) -> Vec<T>
    where
        T: Field,
    {
        let mut y = vec![T::zero(); self.nrows];
        self.matvec(x, &mut y);
        y
    }

    /// Converts to CSR format.
    pub fn to_csr(&self) -> crate::csr::CsrMatrix<T>
    where
        T: Field,
    {
        let eps = <T as Scalar>::epsilon();

        let mut row_ptrs = vec![0usize; self.nrows + 1];
        let mut col_indices = Vec::new();
        let mut values = Vec::new();

        for row in 0..self.nrows {
            let mut row_entries: Vec<(usize, T)> = Vec::new();

            for k in 0..self.width {
                let col = self.indices[row][k];
                if col != INVALID_INDEX {
                    let val = self.data[row][k].clone();
                    if Scalar::abs(val.clone()) > eps {
                        row_entries.push((col, val));
                    }
                }
            }

            // Sort by column index
            row_entries.sort_by_key(|(col, _)| *col);

            for (col, val) in row_entries {
                col_indices.push(col);
                values.push(val);
            }
            row_ptrs[row + 1] = values.len();
        }

        // Safety: we constructed valid CSR data
        unsafe {
            crate::csr::CsrMatrix::new_unchecked(
                self.nrows,
                self.ncols,
                row_ptrs,
                col_indices,
                values,
            )
        }
    }

    /// Converts to dense matrix.
    pub fn to_dense(&self) -> oxiblas_matrix::Mat<T>
    where
        T: Field + bytemuck::Zeroable,
    {
        let mut dense = oxiblas_matrix::Mat::zeros(self.nrows, self.ncols);

        for row in 0..self.nrows {
            for k in 0..self.width {
                let col = self.indices[row][k];
                if col != INVALID_INDEX {
                    dense[(row, col)] = self.data[row][k].clone();
                }
            }
        }

        dense
    }

    /// Creates an ELL matrix from a dense matrix.
    ///
    /// # Arguments
    ///
    /// * `dense` - Source dense matrix
    /// * `max_width` - Maximum width (if None, uses actual max non-zeros per row)
    pub fn from_dense(dense: &oxiblas_matrix::MatRef<'_, T>, max_width: Option<usize>) -> Self
    where
        T: Field,
    {
        let (nrows, ncols) = dense.shape();
        let eps = <T as Scalar>::epsilon();

        // First pass: find max non-zeros per row
        let mut row_nnz = vec![0usize; nrows];
        for i in 0..nrows {
            for j in 0..ncols {
                if Scalar::abs(dense[(i, j)].clone()) > eps {
                    row_nnz[i] += 1;
                }
            }
        }

        let width = max_width.unwrap_or_else(|| row_nnz.iter().copied().max().unwrap_or(0));

        // Build data and indices
        let mut data = Vec::with_capacity(nrows);
        let mut indices = Vec::with_capacity(nrows);

        for i in 0..nrows {
            let mut row_data = Vec::with_capacity(width);
            let mut row_indices = Vec::with_capacity(width);

            for j in 0..ncols {
                if row_data.len() >= width {
                    break;
                }
                let val = dense[(i, j)].clone();
                if Scalar::abs(val.clone()) > eps {
                    row_data.push(val);
                    row_indices.push(j);
                }
            }

            // Pad to width
            while row_data.len() < width {
                row_data.push(T::zero());
                row_indices.push(INVALID_INDEX);
            }

            data.push(row_data);
            indices.push(row_indices);
        }

        // Safety: we constructed valid ELL data
        unsafe { Self::new_unchecked(nrows, ncols, width, data, indices) }
    }

    /// Creates an ELL matrix from CSR format.
    ///
    /// # Arguments
    ///
    /// * `csr` - Source CSR matrix
    /// * `max_width` - Maximum width (if None, uses actual max non-zeros per row)
    pub fn from_csr(
        csr: &crate::csr::CsrMatrix<T>,
        max_width: Option<usize>,
    ) -> Result<Self, EllError>
    where
        T: Field,
    {
        let (nrows, ncols) = csr.shape();
        let row_ptrs = csr.row_ptrs();
        let csr_indices = csr.col_indices();
        let csr_values = csr.values();

        // Find max row width
        let actual_max: usize = (0..nrows)
            .map(|i| row_ptrs[i + 1] - row_ptrs[i])
            .max()
            .unwrap_or(0);

        let width = max_width.unwrap_or(actual_max);

        // Check if any row exceeds max_width
        if let Some(max_w) = max_width {
            for row in 0..nrows {
                let row_nnz = row_ptrs[row + 1] - row_ptrs[row];
                if row_nnz > max_w {
                    return Err(EllError::TooManyNonZeros {
                        row,
                        nnz: row_nnz,
                        max_nnz: max_w,
                    });
                }
            }
        }

        // Build data and indices
        let mut data = Vec::with_capacity(nrows);
        let mut indices = Vec::with_capacity(nrows);

        for row in 0..nrows {
            let start = row_ptrs[row];
            let end = row_ptrs[row + 1];
            let row_nnz = end - start;

            let mut row_data = Vec::with_capacity(width);
            let mut row_indices = Vec::with_capacity(width);

            for k in 0..row_nnz {
                row_data.push(csr_values[start + k].clone());
                row_indices.push(csr_indices[start + k]);
            }

            // Pad to width
            while row_data.len() < width {
                row_data.push(T::zero());
                row_indices.push(INVALID_INDEX);
            }

            data.push(row_data);
            indices.push(row_indices);
        }

        Ok(Self {
            nrows,
            ncols,
            width,
            data,
            indices,
        })
    }

    /// Scales all values by a scalar.
    pub fn scale(&mut self, alpha: T) {
        for row in &mut self.data {
            for val in row.iter_mut() {
                *val = val.clone() * alpha.clone();
            }
        }
    }

    /// Returns a scaled copy of this matrix.
    pub fn scaled(&self, alpha: T) -> Self {
        let mut result = self.clone();
        result.scale(alpha);
        result
    }

    /// Returns the transpose of this matrix.
    ///
    /// Note: This is less efficient than CSR/CSC transpose.
    pub fn transpose(&self) -> Self
    where
        T: Field,
    {
        // Convert to CSR, transpose, convert back
        let csr = self.to_csr();
        let csr_t = csr.transpose();
        Self::from_csr(&csr_t, Some(self.width)).unwrap_or_else(|_| {
            // If width is insufficient, use actual max
            Self::from_csr(&csr_t, None).expect("CSR transpose should be valid")
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_ell_new() {
        // [1 2 0 0]
        // [0 3 4 0]
        // [5 0 0 6]
        let data = vec![vec![1.0, 2.0], vec![3.0, 4.0], vec![5.0, 6.0]];
        let indices = vec![vec![0, 1], vec![1, 2], vec![0, 3]];

        let ell = EllMatrix::new(3, 4, 2, data, indices).unwrap();

        assert_eq!(ell.nrows(), 3);
        assert_eq!(ell.ncols(), 4);
        assert_eq!(ell.width(), 2);
    }

    #[test]
    fn test_ell_get() {
        let data = vec![vec![1.0, 2.0], vec![3.0, 4.0], vec![5.0, 6.0]];
        let indices = vec![vec![0, 1], vec![1, 2], vec![0, 3]];

        let ell = EllMatrix::new(3, 4, 2, data, indices).unwrap();

        assert_eq!(ell.get(0, 0), Some(&1.0));
        assert_eq!(ell.get(0, 1), Some(&2.0));
        assert_eq!(ell.get(1, 1), Some(&3.0));
        assert_eq!(ell.get(1, 2), Some(&4.0));
        assert_eq!(ell.get(2, 0), Some(&5.0));
        assert_eq!(ell.get(2, 3), Some(&6.0));

        // Zero elements
        assert_eq!(ell.get(0, 2), None);
        assert_eq!(ell.get(0, 3), None);
    }

    #[test]
    fn test_ell_matvec() {
        // [1 2 0 0]   [1]   [3]
        // [0 3 4 0] * [1] = [7]
        // [5 0 0 6]   [1]   [11]
        //             [1]
        let data = vec![vec![1.0, 2.0], vec![3.0, 4.0], vec![5.0, 6.0]];
        let indices = vec![vec![0, 1], vec![1, 2], vec![0, 3]];

        let ell = EllMatrix::new(3, 4, 2, data, indices).unwrap();
        let x = vec![1.0, 1.0, 1.0, 1.0];
        let y = ell.mul_vec(&x);

        assert!((y[0] - 3.0).abs() < 1e-10);
        assert!((y[1] - 7.0).abs() < 1e-10);
        assert!((y[2] - 11.0).abs() < 1e-10);
    }

    #[test]
    fn test_ell_with_padding() {
        // [1 0 0]
        // [2 3 4]
        // [0 5 0]
        let data = vec![
            vec![1.0, 0.0, 0.0], // row 0: 1 value, padded
            vec![2.0, 3.0, 4.0], // row 1: 3 values
            vec![5.0, 0.0, 0.0], // row 2: 1 value, padded
        ];
        let indices = vec![
            vec![0, INVALID_INDEX, INVALID_INDEX],
            vec![0, 1, 2],
            vec![1, INVALID_INDEX, INVALID_INDEX],
        ];

        let ell = EllMatrix::new(3, 3, 3, data, indices).unwrap();

        assert_eq!(ell.nnz(), 5);
        assert_eq!(ell.nstored(), 9);
        assert!((ell.efficiency() - 5.0 / 9.0).abs() < 1e-10);
    }

    #[test]
    fn test_ell_eye() {
        let ell: EllMatrix<f64> = EllMatrix::eye(4);

        assert_eq!(ell.nrows(), 4);
        assert_eq!(ell.ncols(), 4);
        assert_eq!(ell.width(), 1);

        for i in 0..4 {
            assert_eq!(ell.get(i, i), Some(&1.0));
        }
    }

    #[test]
    fn test_ell_to_dense() {
        let data = vec![vec![1.0, 2.0], vec![3.0, 4.0], vec![5.0, 6.0]];
        let indices = vec![vec![0, 1], vec![1, 2], vec![0, 3]];

        let ell = EllMatrix::new(3, 4, 2, data, indices).unwrap();
        let dense = ell.to_dense();

        assert!((dense[(0, 0)] - 1.0).abs() < 1e-10);
        assert!((dense[(0, 1)] - 2.0).abs() < 1e-10);
        assert!((dense[(0, 2)] - 0.0).abs() < 1e-10);
        assert!((dense[(1, 1)] - 3.0).abs() < 1e-10);
        assert!((dense[(1, 2)] - 4.0).abs() < 1e-10);
        assert!((dense[(2, 0)] - 5.0).abs() < 1e-10);
        assert!((dense[(2, 3)] - 6.0).abs() < 1e-10);
    }

    #[test]
    fn test_ell_to_csr() {
        let data = vec![vec![1.0, 2.0], vec![3.0, 4.0], vec![5.0, 6.0]];
        let indices = vec![vec![0, 1], vec![1, 2], vec![0, 3]];

        let ell = EllMatrix::new(3, 4, 2, data, indices).unwrap();
        let csr = ell.to_csr();

        assert_eq!(csr.nrows(), 3);
        assert_eq!(csr.ncols(), 4);
        assert_eq!(csr.nnz(), 6);
        assert_eq!(csr.get(0, 0), Some(&1.0));
        assert_eq!(csr.get(2, 3), Some(&6.0));
    }

    #[test]
    fn test_ell_from_dense() {
        use oxiblas_matrix::Mat;

        let dense = Mat::from_rows(&[
            &[1.0f64, 2.0, 0.0, 0.0],
            &[0.0, 3.0, 4.0, 0.0],
            &[5.0, 0.0, 0.0, 6.0],
        ]);

        let ell = EllMatrix::from_dense(&dense.as_ref(), None);

        assert_eq!(ell.width(), 2);
        assert_eq!(ell.get(0, 0), Some(&1.0));
        assert_eq!(ell.get(1, 2), Some(&4.0));
    }

    #[test]
    fn test_ell_from_csr() {
        let values = vec![1.0f64, 2.0, 3.0, 4.0, 5.0, 6.0];
        let col_indices = vec![0, 1, 1, 2, 0, 3];
        let row_ptrs = vec![0, 2, 4, 6];

        let csr = crate::csr::CsrMatrix::new(3, 4, row_ptrs, col_indices, values).unwrap();
        let ell = EllMatrix::from_csr(&csr, None).unwrap();

        assert_eq!(ell.width(), 2);
        assert_eq!(ell.get(0, 0), Some(&1.0));
        assert_eq!(ell.get(2, 3), Some(&6.0));
    }

    #[test]
    fn test_ell_scale() {
        let data = vec![vec![1.0, 2.0], vec![3.0, 4.0]];
        let indices = vec![vec![0, 1], vec![0, 1]];

        let mut ell = EllMatrix::new(2, 2, 2, data, indices).unwrap();
        ell.scale(2.0);

        assert_eq!(ell.get(0, 0), Some(&2.0));
        assert_eq!(ell.get(0, 1), Some(&4.0));
    }

    #[test]
    fn test_ell_transpose() {
        let data = vec![vec![1.0, 2.0], vec![3.0, 0.0]];
        let indices = vec![vec![0, 1], vec![0, INVALID_INDEX]];

        let ell = EllMatrix::new(2, 2, 2, data, indices).unwrap();
        let ell_t = ell.transpose();

        let dense = ell.to_dense();
        let dense_t = ell_t.to_dense();

        for i in 0..2 {
            for j in 0..2 {
                assert!((dense[(i, j)] - dense_t[(j, i)]).abs() < 1e-10);
            }
        }
    }

    #[test]
    fn test_ell_row_iter() {
        let data = vec![vec![1.0, 2.0], vec![3.0, 4.0]];
        let indices = vec![vec![0, 2], vec![1, INVALID_INDEX]];

        let ell = EllMatrix::new(2, 3, 2, data, indices).unwrap();

        let row0: Vec<_> = ell.row_iter(0).collect();
        assert_eq!(row0, vec![(0, &1.0), (2, &2.0)]);

        let row1: Vec<_> = ell.row_iter(1).collect();
        assert_eq!(row1, vec![(1, &3.0)]);
    }

    #[test]
    fn test_ell_invalid_column_index() {
        let data = vec![vec![1.0]];
        let indices = vec![vec![10]]; // Out of bounds

        let result = EllMatrix::new(1, 3, 1, data, indices);
        assert!(matches!(result, Err(EllError::InvalidColumnIndex { .. })));
    }

    #[test]
    fn test_ell_zeros() {
        let ell: EllMatrix<f64> = EllMatrix::zeros(5, 3);

        assert_eq!(ell.nrows(), 5);
        assert_eq!(ell.ncols(), 3);
        assert_eq!(ell.width(), 0);
        assert_eq!(ell.nnz(), 0);
    }
}