2020-09-25 13:21:13 +08:00
|
|
|
#[cfg(feature = "serde-serialize")]
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
|
|
use crate::allocator::Allocator;
|
2020-09-28 05:28:50 +08:00
|
|
|
use crate::base::{DefaultAllocator, MatrixN, VectorN, U1};
|
2020-09-27 08:34:35 +08:00
|
|
|
use crate::dimension::Dim;
|
2020-09-25 13:21:13 +08:00
|
|
|
use simba::scalar::ComplexField;
|
|
|
|
|
|
|
|
/// UDU factorization
|
|
|
|
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
|
|
|
|
#[derive(Clone, Debug)]
|
2020-09-27 08:34:35 +08:00
|
|
|
pub struct UDU<N: ComplexField, D: Dim>
|
2020-09-25 13:21:13 +08:00
|
|
|
where
|
2020-09-28 05:28:50 +08:00
|
|
|
DefaultAllocator: Allocator<N, D> + Allocator<N, D, D>,
|
2020-09-25 13:21:13 +08:00
|
|
|
{
|
|
|
|
/// The upper triangular matrix resulting from the factorization
|
|
|
|
pub u: MatrixN<N, D>,
|
|
|
|
/// The diagonal matrix resulting from the factorization
|
2020-09-28 05:28:50 +08:00
|
|
|
pub d: VectorN<N, D>,
|
2020-09-25 13:21:13 +08:00
|
|
|
}
|
|
|
|
|
2020-09-27 08:34:35 +08:00
|
|
|
impl<N: ComplexField, D: Dim> Copy for UDU<N, D>
|
2020-09-25 13:21:13 +08:00
|
|
|
where
|
2020-09-28 05:28:50 +08:00
|
|
|
DefaultAllocator: Allocator<N, D> + Allocator<N, D, D>,
|
|
|
|
VectorN<N, D>: Copy,
|
2020-09-25 13:21:13 +08:00
|
|
|
MatrixN<N, D>: Copy,
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-09-27 08:34:35 +08:00
|
|
|
impl<N: ComplexField, D: Dim> UDU<N, D>
|
2020-09-25 13:21:13 +08:00
|
|
|
where
|
2020-09-28 05:28:50 +08:00
|
|
|
DefaultAllocator: Allocator<N, D> + Allocator<N, D, D>,
|
2020-09-25 13:21:13 +08:00
|
|
|
{
|
2020-09-26 09:21:14 +08:00
|
|
|
/// Computes the UDU^T factorization
|
2020-10-27 04:39:34 +08:00
|
|
|
/// The input matrix `p` is assumed to be symmetric and this decomposition will only read the upper-triangular part of `p`.
|
2020-09-26 09:21:14 +08:00
|
|
|
/// Ref.: "Optimal control and estimation-Dover Publications", Robert F. Stengel, (1994) page 360
|
|
|
|
pub fn new(p: MatrixN<N, D>) -> Self {
|
2020-09-26 11:29:46 +08:00
|
|
|
let n = p.ncols();
|
2020-09-27 08:34:35 +08:00
|
|
|
let n_as_dim = D::from_usize(n);
|
|
|
|
|
2020-09-28 05:28:50 +08:00
|
|
|
let mut d = VectorN::<N, D>::zeros_generic(n_as_dim, U1);
|
2020-09-27 08:34:35 +08:00
|
|
|
let mut u = MatrixN::<N, D>::zeros_generic(n_as_dim, n_as_dim);
|
2020-09-25 13:21:13 +08:00
|
|
|
|
2020-09-28 05:28:50 +08:00
|
|
|
d[n - 1] = p[(n - 1, n - 1)];
|
2020-09-26 11:29:46 +08:00
|
|
|
u[(n - 1, n - 1)] = N::one();
|
2020-09-25 13:21:13 +08:00
|
|
|
|
2020-09-26 11:29:46 +08:00
|
|
|
for j in (0..n - 1).rev() {
|
2020-09-28 05:28:50 +08:00
|
|
|
u[(j, n - 1)] = p[(j, n - 1)] / d[n - 1];
|
2020-09-25 13:21:13 +08:00
|
|
|
}
|
|
|
|
|
2020-09-26 11:29:46 +08:00
|
|
|
for j in (0..n - 1).rev() {
|
|
|
|
for k in j + 1..n {
|
2020-09-28 05:28:50 +08:00
|
|
|
d[j] = d[j] + d[k] * u[(j, k)].powi(2);
|
2020-09-25 13:21:13 +08:00
|
|
|
}
|
|
|
|
|
2020-09-28 05:28:50 +08:00
|
|
|
d[j] = p[(j, j)] - d[j];
|
2020-09-25 13:21:13 +08:00
|
|
|
|
2020-09-26 09:21:14 +08:00
|
|
|
for i in (0..=j).rev() {
|
2020-09-26 11:29:46 +08:00
|
|
|
for k in j + 1..n {
|
2020-09-28 05:28:50 +08:00
|
|
|
u[(i, j)] = u[(i, j)] + d[k] * u[(j, k)] * u[(i, k)];
|
2020-09-25 13:21:13 +08:00
|
|
|
}
|
2020-09-26 09:21:14 +08:00
|
|
|
|
|
|
|
u[(i, j)] = p[(i, j)] - u[(i, j)];
|
|
|
|
|
2020-09-28 05:28:50 +08:00
|
|
|
u[(i, j)] /= d[j];
|
2020-09-25 13:21:13 +08:00
|
|
|
}
|
2020-09-26 09:21:14 +08:00
|
|
|
|
|
|
|
u[(j, j)] = N::one();
|
2020-09-25 13:21:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Self { u, d }
|
|
|
|
}
|
2020-09-28 05:28:50 +08:00
|
|
|
|
|
|
|
/// Returns the diagonal elements as a matrix
|
|
|
|
pub fn d_matrix(&self) -> MatrixN<N, D> {
|
|
|
|
MatrixN::from_diagonal(&self.d)
|
|
|
|
}
|
2020-09-25 13:21:13 +08:00
|
|
|
}
|