From 19c7f19afa27b99ef080800fc8724fd6e097d7a8 Mon Sep 17 00:00:00 2001 From: Saurabh Date: Fri, 1 Apr 2022 15:26:55 -0600 Subject: [PATCH] fix tests --- nalgebra-sparse/src/ops/mod.rs | 4 +- nalgebra-sparse/tests/unit_tests/ops.rs | 60 ++++++++++++++++++++++--- 2 files changed, 55 insertions(+), 9 deletions(-) diff --git a/nalgebra-sparse/src/ops/mod.rs b/nalgebra-sparse/src/ops/mod.rs index 6fedfb6c..9a73148c 100644 --- a/nalgebra-sparse/src/ops/mod.rs +++ b/nalgebra-sparse/src/ops/mod.rs @@ -113,10 +113,10 @@ //! # use nalgebra_sparse::csr::CsrMatrix; //! # let a = CsrMatrix::identity(10); let b = 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 -//! 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."); //! ``` //! Compared to the simpler example, this snippet is harder to read, but it calls a single diff --git a/nalgebra-sparse/tests/unit_tests/ops.rs b/nalgebra-sparse/tests/unit_tests/ops.rs index 20274d97..0a335567 100644 --- a/nalgebra-sparse/tests/unit_tests/ops.rs +++ b/nalgebra-sparse/tests/unit_tests/ops.rs @@ -5,9 +5,9 @@ use crate::common::{ use nalgebra_sparse::csc::CscMatrix; use nalgebra_sparse::csr::CsrMatrix; use nalgebra_sparse::ops::serial::{ - spadd_csc_prealloc, spadd_csr_prealloc, spadd_pattern, spmm_csc_dense, - spmm_csc_prealloc_checked, spmm_csr_dense, spmm_csr_pattern, spmm_csr_prealloc_checked, - spsolve_csc_lower_triangular, + spadd_csc_prealloc, spadd_csr_prealloc, spadd_pattern, spmm_csc_dense, spmm_csc_prealloc, + spmm_csc_prealloc_unchecked, spmm_csr_dense, spmm_csr_pattern, spmm_csr_prealloc, + spmm_csr_prealloc_unchecked, spsolve_csc_lower_triangular, }; use nalgebra_sparse::ops::Op; use nalgebra_sparse::pattern::SparsityPattern; @@ -544,6 +544,29 @@ proptest! { 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] fn spmm_csr_prealloc_test(SpmmCsrArgs { c, beta, alpha, a, b } 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 // (here we give in the C matrix, so the sparsity pattern is essentially fixed) 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 op_a_dense = match a { @@ -606,7 +629,7 @@ proptest! { let result = catch_unwind(|| { 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(), @@ -690,7 +713,30 @@ proptest! { // 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_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 op_a_dense = match a { @@ -745,7 +791,7 @@ proptest! { let result = catch_unwind(|| { 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(),