#[cfg(feature = "serde-serialize")] use serde::{Deserialize, Serialize}; use crate::allocator::Allocator; use crate::base::{DefaultAllocator, MatrixN}; use crate::dimension::DimName; use simba::scalar::ComplexField; /// UDU factorization #[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))] #[derive(Clone, Debug)] pub struct UDU where DefaultAllocator: Allocator, { /// The upper triangular matrix resulting from the factorization pub u: MatrixN, /// The diagonal matrix resulting from the factorization pub d: MatrixN, } impl Copy for UDU where DefaultAllocator: Allocator, MatrixN: Copy, { } impl UDU where DefaultAllocator: Allocator, { /// Computes the UDU^T factorization as per NASA's "Navigation Filter Best Practices", NTRS document ID 20180003657 /// section 7.2 page 64. /// NOTE: The provided matrix MUST be symmetric. pub fn new(matrix: MatrixN) -> Self { let mut d = MatrixN::::zeros(); let mut u = MatrixN::::zeros(); let n = matrix.ncols(); d[(n, n)] = matrix[(n, n)]; u[(n, n)] = N::one(); for j in (0..n - 1).rev() { u[(j, n)] = matrix[(j, n)] / d[(n, n)]; } for j in (1..n).rev() { d[(j, j)] = matrix[(j, j)]; for k in (j + 1..n).rev() { d[(j, j)] = d[(j, j)] + d[(k, k)] * u[(j, k)].powi(2); } u[(j, j)] = N::one(); for i in (0..j - 1).rev() { u[(i, j)] = matrix[(i, j)]; for k in j + 1..n { u[(i, j)] = u[(i, j)] + d[(k, k)] * u[(i, k)] * u[(j, k)]; } u[(i, j)] /= d[(j, j)]; } } Self { u, d } } }