use crate::traits::{Integer, Scalar};
pub fn coo_tocsr<I: Integer, T: Scalar>(
n_row: usize,
_n_col: usize,
nnz: usize,
a_i: &[I],
a_j: &[I],
a_x: &[T],
b_p: &mut [I],
b_j: &mut [I],
b_x: &mut [T],
) {
b_p.fill(I::zero());
for n in 0..nnz {
let i = a_i[n].to_usize().unwrap();
b_p[i] += I::one();
}
let mut cumsum = I::zero();
for i in 0..n_row {
let temp: I = b_p[i];
b_p[i] = cumsum;
cumsum = cumsum + temp;
}
b_p[n_row] = I::from(nnz).unwrap();
for n in 0..nnz {
let row = a_i[n].to_usize().unwrap();
let dest = b_p[row].to_usize().unwrap();
b_j[dest] = a_j[n];
b_x[dest] = a_x[n];
b_p[row] += I::one();
}
let mut last = I::zero();
for i in 0..=n_row {
let temp: I = b_p[i];
b_p[i] = last;
last = temp;
}
}
pub fn coo_todense<I: Integer, T: Scalar>(
n_row: usize,
n_col: usize,
nnz: usize,
a_i: &[I],
a_j: &[I],
a_x: &[T],
b_x: &mut [T],
fortran: bool,
) {
if !fortran {
for n in 0..nnz {
let i = n_col * a_i[n].to_usize().unwrap() + a_j[n].to_usize().unwrap();
b_x[i] += a_x[n];
}
} else {
for n in 0..nnz {
let i = n_row * a_j[n].to_usize().unwrap() + a_i[n].to_usize().unwrap();
b_x[i] += a_x[n];
}
}
}
pub fn coo_matvec<I: Integer, T: Scalar>(
nnz: usize,
a_i: &[I],
a_j: &[I],
a_x: &[T],
x_x: &[T],
y_x: &mut [T],
) {
for n in 0..nnz {
let i = a_i[n].to_usize().unwrap();
let j = a_j[n].to_usize().unwrap();
y_x[i] += a_x[n] * x_x[j];
}
}