use scirs2_core::ndarray::{s, Array1, Array2, ArrayView1, ArrayView2, ScalarOperand};
use scirs2_core::numeric::{Float, NumAssign};
use scirs2_core::random::prelude::*;
use std::iter::Sum;
pub struct CsrMatrix<F> {
pub(super) nrows: usize,
pub(super) ncols: usize,
pub(super) data: Vec<F>,
pub(super) indices: Vec<usize>,
pub(super) indptr: Vec<usize>,
}
impl<F> CsrMatrix<F>
where
F: Float + NumAssign + Sum + Send + Sync + ScalarOperand + 'static,
{
pub fn new(
nrows: usize,
ncols: usize,
data: Vec<F>,
indices: Vec<usize>,
indptr: Vec<usize>,
) -> Self {
Self {
nrows,
ncols,
data,
indices,
indptr,
}
}
pub fn nnz(&self) -> usize {
self.data.len()
}
pub fn from_dense(dense: &ArrayView2<F>, threshold: F) -> Self {
let (m, n) = dense.dim();
let mut data = Vec::new();
let mut indices = Vec::new();
let mut indptr = Vec::with_capacity(m + 1);
indptr.push(0);
for i in 0..m {
for j in 0..n {
let val = dense[[i, j]];
if val.abs() >= threshold {
data.push(val);
indices.push(j);
}
}
indptr.push(data.len());
}
Self {
nrows: m,
ncols: n,
data,
indices,
indptr,
}
}
}