#![cfg(feature = "sparse")]
use approx::assert_abs_diff_eq;
use onemkl::sparse::{BlockLayout, IndexBase, MatrixType, Operation, SparseMatrix};
#[test]
fn coo_diagonal_mv() {
let row_indx = vec![0, 1, 2];
let col_indx = vec![0, 1, 2];
let values = vec![1.0_f64, 2.0, 3.0];
let a = SparseMatrix::from_coo(3, 3, IndexBase::Zero, row_indx, col_indx, values).unwrap();
let x = [1.0_f64; 3];
let mut y = [0.0_f64; 3];
a.mv(
Operation::NoTrans,
1.0,
MatrixType::General,
&x,
0.0,
&mut y,
)
.unwrap();
assert_abs_diff_eq!(y[0], 1.0, epsilon = 1e-12);
assert_abs_diff_eq!(y[1], 2.0, epsilon = 1e-12);
assert_abs_diff_eq!(y[2], 3.0, epsilon = 1e-12);
}
#[test]
fn coo_unordered_entries() {
let row_indx = vec![1, 0, 0];
let col_indx = vec![1, 2, 0];
let values = vec![3.0_f64, 2.0, 1.0];
let a = SparseMatrix::from_coo(2, 3, IndexBase::Zero, row_indx, col_indx, values).unwrap();
let x = [1.0_f64; 3];
let mut y = [0.0_f64; 2];
a.mv(
Operation::NoTrans,
1.0,
MatrixType::General,
&x,
0.0,
&mut y,
)
.unwrap();
assert_abs_diff_eq!(y[0], 3.0, epsilon = 1e-12); assert_abs_diff_eq!(y[1], 3.0, epsilon = 1e-12);
}
#[test]
fn csc_dense_2x3() {
let col_ptr = vec![0, 1, 2, 3];
let row_indx = vec![0, 1, 0];
let values = vec![1.0_f64, 3.0, 2.0];
let a = SparseMatrix::from_csc(2, 3, IndexBase::Zero, col_ptr, row_indx, values).unwrap();
let x = [1.0_f64; 3];
let mut y = [0.0_f64; 2];
a.mv(
Operation::NoTrans,
1.0,
MatrixType::General,
&x,
0.0,
&mut y,
)
.unwrap();
assert_abs_diff_eq!(y[0], 3.0, epsilon = 1e-12); assert_abs_diff_eq!(y[1], 3.0, epsilon = 1e-12);
}
#[test]
fn csc_transposed_mv() {
let col_ptr = vec![0, 1, 2, 3];
let row_indx = vec![0, 1, 0];
let values = vec![1.0_f64, 3.0, 2.0];
let a = SparseMatrix::from_csc(2, 3, IndexBase::Zero, col_ptr, row_indx, values).unwrap();
let x = [1.0_f64, 1.0];
let mut y = [0.0_f64; 3];
a.mv(Operation::Trans, 1.0, MatrixType::General, &x, 0.0, &mut y)
.unwrap();
assert_abs_diff_eq!(y[0], 1.0, epsilon = 1e-12);
assert_abs_diff_eq!(y[1], 3.0, epsilon = 1e-12);
assert_abs_diff_eq!(y[2], 2.0, epsilon = 1e-12);
}
#[test]
fn bsr_block_diagonal_mv() {
let row_ptr = vec![0, 1, 2];
let col_idx = vec![0, 1];
let values = vec![1.0_f64, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0];
let a = SparseMatrix::from_bsr(
4,
4,
2,
BlockLayout::RowMajor,
IndexBase::Zero,
row_ptr,
col_idx,
values,
)
.unwrap();
let x = [1.0_f64; 4];
let mut y = [0.0_f64; 4];
a.mv(
Operation::NoTrans,
1.0,
MatrixType::General,
&x,
0.0,
&mut y,
)
.unwrap();
assert_abs_diff_eq!(y[0], 3.0, epsilon = 1e-12);
assert_abs_diff_eq!(y[1], 7.0, epsilon = 1e-12);
assert_abs_diff_eq!(y[2], 11.0, epsilon = 1e-12);
assert_abs_diff_eq!(y[3], 15.0, epsilon = 1e-12);
}
#[test]
fn invalid_coo_length_rejected() {
let row_indx = vec![0, 1];
let col_indx = vec![0, 1, 2];
let values = vec![1.0_f64, 2.0];
let r = SparseMatrix::from_coo(3, 3, IndexBase::Zero, row_indx, col_indx, values);
assert!(r.is_err());
}
#[test]
fn invalid_csc_col_ptr_rejected() {
let col_ptr = vec![0, 1, 2];
let row_indx = vec![0, 1];
let values = vec![1.0_f64, 2.0];
let r = SparseMatrix::from_csc(3, 3, IndexBase::Zero, col_ptr, row_indx, values);
assert!(r.is_err());
}
#[test]
fn invalid_bsr_block_size_rejected() {
let r = SparseMatrix::from_bsr(
3,
3,
2,
BlockLayout::RowMajor,
IndexBase::Zero,
vec![0, 1],
vec![0],
vec![1.0_f64, 2.0, 3.0, 4.0],
);
assert!(r.is_err());
}