fix tests

This commit is contained in:
Saurabh 2022-04-01 15:26:55 -06:00
parent e3fd0e7393
commit 19c7f19afa
2 changed files with 55 additions and 9 deletions

View File

@ -113,10 +113,10 @@
//! # use nalgebra_sparse::csr::CsrMatrix; //! # use nalgebra_sparse::csr::CsrMatrix;
//! # let a = CsrMatrix::identity(10); let b = CsrMatrix::identity(10); //! # let a = CsrMatrix::identity(10); let b = CsrMatrix::identity(10);
//! # let mut c = CsrMatrix::identity(10); //! # let mut c = CsrMatrix::identity(10);
//! use nalgebra_sparse::ops::{Op, serial::spmm_csr_prealloc_checked}; //! use nalgebra_sparse::ops::{Op, serial::spmm_csr_prealloc};
//! //!
//! // Evaluate the expression `c <- 3.0 * c + 2.0 * a^T * b //! // Evaluate the expression `c <- 3.0 * c + 2.0 * a^T * b
//! spmm_csr_prealloc_checked(3.0, &mut c, 2.0, Op::Transpose(&a), Op::NoOp(&b)) //! spmm_csr_prealloc(3.0, &mut c, 2.0, Op::Transpose(&a), Op::NoOp(&b))
//! .expect("We assume that the pattern of C is able to accommodate the result."); //! .expect("We assume that the pattern of C is able to accommodate the result.");
//! ``` //! ```
//! Compared to the simpler example, this snippet is harder to read, but it calls a single //! Compared to the simpler example, this snippet is harder to read, but it calls a single

View File

@ -5,9 +5,9 @@ use crate::common::{
use nalgebra_sparse::csc::CscMatrix; use nalgebra_sparse::csc::CscMatrix;
use nalgebra_sparse::csr::CsrMatrix; use nalgebra_sparse::csr::CsrMatrix;
use nalgebra_sparse::ops::serial::{ use nalgebra_sparse::ops::serial::{
spadd_csc_prealloc, spadd_csr_prealloc, spadd_pattern, spmm_csc_dense, spadd_csc_prealloc, spadd_csr_prealloc, spadd_pattern, spmm_csc_dense, spmm_csc_prealloc,
spmm_csc_prealloc_checked, spmm_csr_dense, spmm_csr_pattern, spmm_csr_prealloc_checked, spmm_csc_prealloc_unchecked, spmm_csr_dense, spmm_csr_pattern, spmm_csr_prealloc,
spsolve_csc_lower_triangular, spmm_csr_prealloc_unchecked, spsolve_csc_lower_triangular,
}; };
use nalgebra_sparse::ops::Op; use nalgebra_sparse::ops::Op;
use nalgebra_sparse::pattern::SparsityPattern; use nalgebra_sparse::pattern::SparsityPattern;
@ -544,6 +544,29 @@ proptest! {
prop_assert_eq!(&c_pattern, c_csr.pattern()); prop_assert_eq!(&c_pattern, c_csr.pattern());
} }
#[test]
fn spmm_csr_prealloc_unchecked_test(SpmmCsrArgs { c, beta, alpha, a, b }
in spmm_csr_prealloc_args_strategy()
) {
// Test that we get the expected result by comparing to an equivalent dense operation
// (here we give in the C matrix, so the sparsity pattern is essentially fixed)
let mut c_sparse = c.clone();
spmm_csr_prealloc_unchecked(beta, &mut c_sparse, alpha, a.as_ref(), b.as_ref()).unwrap();
let mut c_dense = DMatrix::from(&c);
let op_a_dense = match a {
Op::NoOp(ref a) => DMatrix::from(a),
Op::Transpose(ref a) => DMatrix::from(a).transpose(),
};
let op_b_dense = match b {
Op::NoOp(ref b) => DMatrix::from(b),
Op::Transpose(ref b) => DMatrix::from(b).transpose(),
};
c_dense = beta * c_dense + alpha * &op_a_dense * op_b_dense;
prop_assert_eq!(&DMatrix::from(&c_sparse), &c_dense);
}
#[test] #[test]
fn spmm_csr_prealloc_test(SpmmCsrArgs { c, beta, alpha, a, b } fn spmm_csr_prealloc_test(SpmmCsrArgs { c, beta, alpha, a, b }
in spmm_csr_prealloc_args_strategy() in spmm_csr_prealloc_args_strategy()
@ -551,7 +574,7 @@ proptest! {
// Test that we get the expected result by comparing to an equivalent dense operation // Test that we get the expected result by comparing to an equivalent dense operation
// (here we give in the C matrix, so the sparsity pattern is essentially fixed) // (here we give in the C matrix, so the sparsity pattern is essentially fixed)
let mut c_sparse = c.clone(); let mut c_sparse = c.clone();
spmm_csr_prealloc_checked(beta, &mut c_sparse, alpha, a.as_ref(), b.as_ref()).unwrap(); spmm_csr_prealloc(beta, &mut c_sparse, alpha, a.as_ref(), b.as_ref()).unwrap();
let mut c_dense = DMatrix::from(&c); let mut c_dense = DMatrix::from(&c);
let op_a_dense = match a { let op_a_dense = match a {
@ -606,7 +629,7 @@ proptest! {
let result = catch_unwind(|| { let result = catch_unwind(|| {
let mut spmm_result = c.clone(); let mut spmm_result = c.clone();
spmm_csr_prealloc_checked(beta, &mut spmm_result, alpha, a.as_ref(), b.as_ref()).unwrap(); spmm_csr_prealloc(beta, &mut spmm_result, alpha, a.as_ref(), b.as_ref()).unwrap();
}); });
prop_assert!(result.is_err(), prop_assert!(result.is_err(),
@ -690,7 +713,30 @@ proptest! {
// Test that we get the expected result by comparing to an equivalent dense operation // Test that we get the expected result by comparing to an equivalent dense operation
// (here we give in the C matrix, so the sparsity pattern is essentially fixed) // (here we give in the C matrix, so the sparsity pattern is essentially fixed)
let mut c_sparse = c.clone(); let mut c_sparse = c.clone();
spmm_csc_prealloc_checked(beta, &mut c_sparse, alpha, a.as_ref(), b.as_ref()).unwrap(); spmm_csc_prealloc(beta, &mut c_sparse, alpha, a.as_ref(), b.as_ref()).unwrap();
let mut c_dense = DMatrix::from(&c);
let op_a_dense = match a {
Op::NoOp(ref a) => DMatrix::from(a),
Op::Transpose(ref a) => DMatrix::from(a).transpose(),
};
let op_b_dense = match b {
Op::NoOp(ref b) => DMatrix::from(b),
Op::Transpose(ref b) => DMatrix::from(b).transpose(),
};
c_dense = beta * c_dense + alpha * &op_a_dense * op_b_dense;
prop_assert_eq!(&DMatrix::from(&c_sparse), &c_dense);
}
#[test]
fn spmm_csc_prealloc_unchecked_test(SpmmCscArgs { c, beta, alpha, a, b }
in spmm_csc_prealloc_args_strategy()
) {
// Test that we get the expected result by comparing to an equivalent dense operation
// (here we give in the C matrix, so the sparsity pattern is essentially fixed)
let mut c_sparse = c.clone();
spmm_csc_prealloc_unchecked(beta, &mut c_sparse, alpha, a.as_ref(), b.as_ref()).unwrap();
let mut c_dense = DMatrix::from(&c); let mut c_dense = DMatrix::from(&c);
let op_a_dense = match a { let op_a_dense = match a {
@ -745,7 +791,7 @@ proptest! {
let result = catch_unwind(|| { let result = catch_unwind(|| {
let mut spmm_result = c.clone(); let mut spmm_result = c.clone();
spmm_csc_prealloc_checked(beta, &mut spmm_result, alpha, a.as_ref(), b.as_ref()).unwrap(); spmm_csc_prealloc(beta, &mut spmm_result, alpha, a.as_ref(), b.as_ref()).unwrap();
}); });
prop_assert!(result.is_err(), prop_assert!(result.is_err(),