| 1 |
use na::{Matrix2, Vector3, geometry::Quaternion}; |
| 2 |
use num_traits::{One, Zero}; |
| 3 |
|
| 4 |
#[test] |
| 5 |
fn gemm_noncommutative() { |
| 6 |
type Qf64 = Quaternion<f64>; |
| 7 |
let i = Qf64::from_imag(Vector3::new(1.0, 0.0, 0.0)); |
| 8 |
let j = Qf64::from_imag(Vector3::new(0.0, 1.0, 0.0)); |
| 9 |
let k = Qf64::from_imag(Vector3::new(0.0, 0.0, 1.0)); |
| 10 |
|
| 11 |
let m1 = Matrix2::new(k, Qf64::zero(), j, i); |
| 12 |
// this is the inverse of m1 |
| 13 |
let m2 = Matrix2::new(-k, Qf64::zero(), Qf64::one(), -i); |
| 14 |
|
| 15 |
let mut res: Matrix2<Qf64> = Matrix2::zero(); |
| 16 |
res.gemm(Qf64::one(), &m1, &m2, Qf64::zero()); |
| 17 |
assert_eq!(res, Matrix2::identity()); |
| 18 |
|
| 19 |
let mut res: Matrix2<Qf64> = Matrix2::identity(); |
| 20 |
res.gemm(k, &m1, &m2, -k); |
| 21 |
assert_eq!(res, Matrix2::zero()); |
| 22 |
} |
| 23 |
|
| 24 |
#[cfg(feature = "proptest-support")] |
| 25 |
mod blas_proptest { |
| 26 |
use crate::proptest::{PROPTEST_F64, PROPTEST_MATRIX_DIM}; |
| 27 |
use na::{DMatrix, DVector}; |
| 28 |
use proptest::{prop_assert, proptest}; |
| 29 |
|
| 30 |
proptest! { |
| 31 |
/* |
| 32 |
* |
| 33 |
* Symmetric operators. |
| 34 |
* |
| 35 |
*/ |
| 36 |
#[test] |
| 37 |
fn gemv_symm(n in PROPTEST_MATRIX_DIM, alpha in PROPTEST_F64, beta in PROPTEST_F64) { |
| 38 |
let a = DMatrix::<f64>::new_random(n, n); |
| 39 |
let a = &a * a.transpose(); |
| 40 |
|
| 41 |
let x = DVector::new_random(n); |
| 42 |
let mut y1 = DVector::new_random(n); |
| 43 |
let mut y2 = y1.clone(); |
| 44 |
|
| 45 |
y1.gemv(alpha, &a, &x, beta); |
| 46 |
y2.sygemv(alpha, &a.lower_triangle(), &x, beta); |
| 47 |
|
| 48 |
prop_assert!(relative_eq!(y1, y2, epsilon = 1.0e-10)); |
| 49 |
|
| 50 |
y1.gemv(alpha, &a, &x, 0.0); |
| 51 |
y2.sygemv(alpha, &a.lower_triangle(), &x, 0.0); |
| 52 |
|
| 53 |
prop_assert!(relative_eq!(y1, y2, epsilon = 1.0e-10)) |
| 54 |
} |
| 55 |
|
| 56 |
#[test] |
| 57 |
fn gemv_tr(n in PROPTEST_MATRIX_DIM, alpha in PROPTEST_F64, beta in PROPTEST_F64) { |
| 58 |
let a = DMatrix::<f64>::new_random(n, n); |
| 59 |
let x = DVector::new_random(n); |
| 60 |
let mut y1 = DVector::new_random(n); |
| 61 |
let mut y2 = y1.clone(); |
| 62 |
|
| 63 |
y1.gemv(alpha, &a, &x, beta); |
| 64 |
y2.gemv_tr(alpha, &a.transpose(), &x, beta); |
| 65 |
|
| 66 |
prop_assert!(relative_eq!(y1, y2, epsilon = 1.0e-10)); |
| 67 |
|
| 68 |
y1.gemv(alpha, &a, &x, 0.0); |
| 69 |
y2.gemv_tr(alpha, &a.transpose(), &x, 0.0); |
| 70 |
|
| 71 |
prop_assert!(relative_eq!(y1, y2, epsilon = 1.0e-10)) |
| 72 |
} |
| 73 |
|
| 74 |
#[test] |
| 75 |
fn ger_symm(n in PROPTEST_MATRIX_DIM, alpha in PROPTEST_F64, beta in PROPTEST_F64) { |
| 76 |
let a = DMatrix::<f64>::new_random(n, n); |
| 77 |
let mut a1 = &a * a.transpose(); |
| 78 |
let mut a2 = a1.lower_triangle(); |
| 79 |
|
| 80 |
let x = DVector::new_random(n); |
| 81 |
let y = DVector::new_random(n); |
| 82 |
|
| 83 |
a1.ger(alpha, &x, &y, beta); |
| 84 |
a2.syger(alpha, &x, &y, beta); |
| 85 |
|
| 86 |
prop_assert!(relative_eq!(a1.lower_triangle(), a2)); |
| 87 |
|
| 88 |
a1.ger(alpha, &x, &y, 0.0); |
| 89 |
a2.syger(alpha, &x, &y, 0.0); |
| 90 |
|
| 91 |
prop_assert!(relative_eq!(a1.lower_triangle(), a2)) |
| 92 |
} |
| 93 |
|
| 94 |
#[test] |
| 95 |
fn quadform(n in PROPTEST_MATRIX_DIM, alpha in PROPTEST_F64, beta in PROPTEST_F64) { |
| 96 |
let rhs = DMatrix::<f64>::new_random(6, n); |
| 97 |
let mid = DMatrix::<f64>::new_random(6, 6); |
| 98 |
let mut res = DMatrix::new_random(n, n); |
| 99 |
|
| 100 |
let expected = &res * beta + rhs.transpose() * &mid * &rhs * alpha; |
| 101 |
|
| 102 |
res.quadform(alpha, &mid, &rhs, beta); |
| 103 |
|
| 104 |
prop_assert!(relative_eq!(res, expected, epsilon = 1.0e-7)) |
| 105 |
} |
| 106 |
|
| 107 |
#[test] |
| 108 |
fn quadform_tr(n in PROPTEST_MATRIX_DIM, alpha in PROPTEST_F64, beta in PROPTEST_F64) { |
| 109 |
let lhs = DMatrix::<f64>::new_random(6, n); |
| 110 |
let mid = DMatrix::<f64>::new_random(n, n); |
| 111 |
let mut res = DMatrix::new_random(6, 6); |
| 112 |
|
| 113 |
let expected = &res * beta + &lhs * &mid * lhs.transpose() * alpha; |
| 114 |
|
| 115 |
res.quadform_tr(alpha, &lhs, &mid , beta); |
| 116 |
|
| 117 |
prop_assert!(relative_eq!(res, expected, epsilon = 1.0e-7)) |
| 118 |
} |
| 119 |
} |
| 120 |
} |