Fix tests for the UDU decomposition.

This commit is contained in:
Crozet Sébastien 2021-02-25 13:16:04 +01:00
parent 89ca2fe5fb
commit ab0d335b61
2 changed files with 13 additions and 17 deletions

View File

@ -7,7 +7,7 @@ use crate::dimension::Dim;
use crate::storage::Storage; use crate::storage::Storage;
use simba::scalar::RealField; use simba::scalar::RealField;
/// UDU factorization /// UDU factorization.
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
#[cfg_attr( #[cfg_attr(
feature = "serde-serialize", feature = "serde-serialize",
@ -42,8 +42,11 @@ impl<N: RealField, D: Dim> UDU<N, D>
where where
DefaultAllocator: Allocator<N, D> + Allocator<N, D, D>, DefaultAllocator: Allocator<N, D> + Allocator<N, D, D>,
{ {
/// Computes the UDU^T factorization /// Computes the UDU^T factorization.
/// The input matrix `p` is assumed to be symmetric and this decomposition will only read the upper-triangular part of `p`. ///
/// The input matrix `p` is assumed to be symmetric and this decomposition will only read
/// the upper-triangular part of `p`.
///
/// Ref.: "Optimal control and estimation-Dover Publications", Robert F. Stengel, (1994) page 360 /// Ref.: "Optimal control and estimation-Dover Publications", Robert F. Stengel, (1994) page 360
pub fn new(p: MatrixN<N, D>) -> Self { pub fn new(p: MatrixN<N, D>) -> Self {
let n = p.ncols(); let n = p.ncols();

View File

@ -1,5 +1,4 @@
use na::Matrix3; use na::{Matrix3, UDU};
use na::UDU;
#[test] #[test]
#[rustfmt::skip] #[rustfmt::skip]
@ -40,19 +39,14 @@ mod quickcheck_tests {
macro_rules! gen_tests( macro_rules! gen_tests(
($module: ident, $scalar: ty) => { ($module: ident, $scalar: ty) => {
mod $module { mod $module {
use std::cmp; use na::{UDU, DMatrix, Matrix4};
use na::{DMatrix, Matrix4};
#[allow(unused_imports)] #[allow(unused_imports)]
use crate::core::helper::{RandScalar, RandComplex}; use crate::core::helper::{RandScalar, RandComplex};
quickcheck! { quickcheck! {
fn udu(m: DMatrix<$scalar>) -> bool { fn udu(n: usize) -> bool {
let mut m = m; let n = std::cmp::max(1, std::cmp::min(n, 10));
if m.len() == 0 { let m = DMatrix::<$scalar>::new_random(n, n).map(|e| e.0).hermitian_part();
m = DMatrix::<$scalar>::new_random(1, 1);
}
let m = m.map(|e| e.0);
let udu = UDU::new(m.clone()); let udu = UDU::new(m.clone());
let p = &udu.u * &udu.d_matrix() * &udu.u.transpose(); let p = &udu.u * &udu.d_matrix() * &udu.u.transpose();
@ -61,18 +55,17 @@ mod quickcheck_tests {
} }
fn udu_static(m: Matrix4<$scalar>) -> bool { fn udu_static(m: Matrix4<$scalar>) -> bool {
let m = m.map(|e| e.0); let m = m.map(|e| e.0).hermitian_part();
let udu = UDU::new(m.clone()); let udu = UDU::new(m.clone());
let p = udu.u * udu.d_matrix() * udu.u.transpose(); let p = udu.u * udu.d_matrix() * udu.u.transpose();
relative_eq!(m, p, epsilon = 3.0e-16) relative_eq!(m, p, epsilon = 1.0e-7)
} }
} }
} }
} }
); );
gen_tests!(complex, RandComplex<f64>);
gen_tests!(f64, RandScalar<f64>); gen_tests!(f64, RandScalar<f64>);
} }