| 1 |
use num::Zero; |
| 2 |
use simba::scalar::ClosedAddAssign; |
| 3 |
|
| 4 |
use crate::allocator::Allocator; |
| 5 |
use crate::sparse::cs_utils; |
| 6 |
use crate::sparse::{CsMatrix, CsStorage}; |
| 7 |
use crate::storage::Storage; |
| 8 |
use crate::{DefaultAllocator, Dim, Dyn, Matrix, OMatrix, Scalar}; |
| 9 |
|
| 10 |
impl<T: Scalar + Zero + ClosedAddAssign> CsMatrix<T> { |
| 11 |
/// Creates a column-compressed sparse matrix from a sparse matrix in triplet form. |
| 12 |
pub fn from_triplet( |
| 13 |
nrows: usize, |
| 14 |
ncols: usize, |
| 15 |
irows: &[usize], |
| 16 |
icols: &[usize], |
| 17 |
vals: &[T], |
| 18 |
) -> Self { |
| 19 |
Self::from_triplet_generic(Dyn(nrows), Dyn(ncols), irows, icols, vals) |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
impl<T: Scalar + Zero + ClosedAddAssign, R: Dim, C: Dim> CsMatrix<T, R, C> |
| 24 |
where |
| 25 |
DefaultAllocator: Allocator<C> + Allocator<R>, |
| 26 |
{ |
| 27 |
/// Creates a column-compressed sparse matrix from a sparse matrix in triplet form. |
| 28 |
pub fn from_triplet_generic( |
| 29 |
nrows: R, |
| 30 |
ncols: C, |
| 31 |
irows: &[usize], |
| 32 |
icols: &[usize], |
| 33 |
vals: &[T], |
| 34 |
) -> Self { |
| 35 |
assert!(vals.len() == irows.len()); |
| 36 |
assert!(vals.len() == icols.len()); |
| 37 |
|
| 38 |
let mut res = CsMatrix::new_uninitialized_generic(nrows, ncols, vals.len()); |
| 39 |
let mut workspace = res.data.p.clone(); |
| 40 |
|
| 41 |
// Column count. |
| 42 |
for j in icols.iter().cloned() { |
| 43 |
workspace[j] += 1; |
| 44 |
} |
| 45 |
|
| 46 |
let _ = cs_utils::cumsum(&mut workspace, &mut res.data.p); |
| 47 |
|
| 48 |
// Fill i and vals. |
| 49 |
for ((i, j), val) in irows |
| 50 |
.iter() |
| 51 |
.cloned() |
| 52 |
.zip(icols.iter().cloned()) |
| 53 |
.zip(vals.iter().cloned()) |
| 54 |
{ |
| 55 |
let offset = workspace[j]; |
| 56 |
res.data.i[offset] = i; |
| 57 |
res.data.vals[offset] = val; |
| 58 |
workspace[j] = offset + 1; |
| 59 |
} |
| 60 |
|
| 61 |
// Sort the result. |
| 62 |
res.sort(); |
| 63 |
res.dedup(); |
| 64 |
res |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
impl<T: Scalar + Zero, R: Dim, C: Dim, S> From<CsMatrix<T, R, C, S>> for OMatrix<T, R, C> |
| 69 |
where |
| 70 |
S: CsStorage<T, R, C>, |
| 71 |
DefaultAllocator: Allocator<R, C>, |
| 72 |
{ |
| 73 |
fn from(m: CsMatrix<T, R, C, S>) -> Self { |
| 74 |
let (nrows, ncols) = m.data.shape(); |
| 75 |
let mut res = OMatrix::zeros_generic(nrows, ncols); |
| 76 |
|
| 77 |
for j in 0..ncols.value() { |
| 78 |
for (i, val) in m.data.column_entries(j) { |
| 79 |
res[(i, j)] = val; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
res |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
impl<T: Scalar + Zero, R: Dim, C: Dim, S> From<Matrix<T, R, C, S>> for CsMatrix<T, R, C> |
| 88 |
where |
| 89 |
S: Storage<T, R, C>, |
| 90 |
DefaultAllocator: Allocator<R, C> + Allocator<C>, |
| 91 |
{ |
| 92 |
fn from(m: Matrix<T, R, C, S>) -> Self { |
| 93 |
let (nrows, ncols) = m.data.shape(); |
| 94 |
let len = m.iter().filter(|e| !e.is_zero()).count(); |
| 95 |
let mut res = CsMatrix::new_uninitialized_generic(nrows, ncols, len); |
| 96 |
let mut nz = 0; |
| 97 |
|
| 98 |
for j in 0..ncols.value() { |
| 99 |
let column = m.column(j); |
| 100 |
res.data.p[j] = nz; |
| 101 |
|
| 102 |
for i in 0..nrows.value() { |
| 103 |
if !column[i].is_zero() { |
| 104 |
res.data.i[nz] = i; |
| 105 |
res.data.vals[nz] = column[i].clone(); |
| 106 |
nz += 1; |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
res |
| 112 |
} |
| 113 |
} |