2021-04-12 18:14:16 +08:00
|
|
|
#[cfg(feature = "serde-serialize-no-std")]
|
2020-09-25 13:21:13 +08:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
|
|
use crate::allocator::Allocator;
|
2021-04-11 17:00:38 +08:00
|
|
|
use crate::base::{Const, DefaultAllocator, OMatrix, OVector};
|
2020-09-27 08:34:35 +08:00
|
|
|
use crate::dimension::Dim;
|
2020-10-27 12:06:37 +08:00
|
|
|
use crate::storage::Storage;
|
2020-10-29 06:04:46 +08:00
|
|
|
use simba::scalar::RealField;
|
2020-09-25 13:21:13 +08:00
|
|
|
|
2021-02-25 20:16:04 +08:00
|
|
|
/// UDU factorization.
|
2021-04-12 18:14:16 +08:00
|
|
|
#[cfg_attr(feature = "serde-serialize-no-std", derive(Serialize, Deserialize))]
|
2020-10-27 12:06:37 +08:00
|
|
|
#[cfg_attr(
|
2021-04-12 18:14:16 +08:00
|
|
|
feature = "serde-serialize-no-std",
|
2021-04-11 17:00:38 +08:00
|
|
|
serde(bound(serialize = "OVector<T, D>: Serialize, OMatrix<T, D, D>: Serialize"))
|
2020-10-27 12:06:37 +08:00
|
|
|
)]
|
|
|
|
#[cfg_attr(
|
2021-04-12 18:14:16 +08:00
|
|
|
feature = "serde-serialize-no-std",
|
2020-10-27 12:06:37 +08:00
|
|
|
serde(bound(
|
2021-04-11 17:00:38 +08:00
|
|
|
deserialize = "OVector<T, D>: Deserialize<'de>, OMatrix<T, D, D>: Deserialize<'de>"
|
2020-10-27 12:06:37 +08:00
|
|
|
))
|
|
|
|
)]
|
2020-09-25 13:21:13 +08:00
|
|
|
#[derive(Clone, Debug)]
|
2021-04-11 17:00:38 +08:00
|
|
|
pub struct UDU<T: RealField, D: Dim>
|
2020-09-25 13:21:13 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
DefaultAllocator: Allocator<T, D> + Allocator<T, D, D>,
|
2020-09-25 13:21:13 +08:00
|
|
|
{
|
|
|
|
/// The upper triangular matrix resulting from the factorization
|
2021-04-11 17:00:38 +08:00
|
|
|
pub u: OMatrix<T, D, D>,
|
2020-09-25 13:21:13 +08:00
|
|
|
/// The diagonal matrix resulting from the factorization
|
2021-04-11 17:00:38 +08:00
|
|
|
pub d: OVector<T, D>,
|
2020-09-25 13:21:13 +08:00
|
|
|
}
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<T: RealField, D: Dim> Copy for UDU<T, D>
|
2020-09-25 13:21:13 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
DefaultAllocator: Allocator<T, D> + Allocator<T, D, D>,
|
|
|
|
OVector<T, D>: Copy,
|
|
|
|
OMatrix<T, D, D>: Copy,
|
2020-09-25 13:21:13 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<T: RealField, D: Dim> UDU<T, D>
|
2020-09-25 13:21:13 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
DefaultAllocator: Allocator<T, D> + Allocator<T, D, D>,
|
2020-09-25 13:21:13 +08:00
|
|
|
{
|
2021-02-25 20:16:04 +08:00
|
|
|
/// 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`.
|
|
|
|
///
|
2020-09-26 09:21:14 +08:00
|
|
|
/// Ref.: "Optimal control and estimation-Dover Publications", Robert F. Stengel, (1994) page 360
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn new(p: OMatrix<T, D, D>) -> Option<Self> {
|
2020-09-26 11:29:46 +08:00
|
|
|
let n = p.ncols();
|
2020-10-27 12:06:37 +08:00
|
|
|
let n_dim = p.data.shape().1;
|
2020-09-27 08:34:35 +08:00
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
let mut d = OVector::zeros_generic(n_dim, Const::<1>);
|
|
|
|
let mut u = OMatrix::zeros_generic(n_dim, n_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)];
|
2021-03-01 00:52:14 +08:00
|
|
|
|
|
|
|
if d[n - 1].is_zero() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2020-10-27 12:06:37 +08:00
|
|
|
u.column_mut(n - 1)
|
2021-04-11 17:00:38 +08:00
|
|
|
.axpy(T::one() / d[n - 1], &p.column(n - 1), T::zero());
|
2020-09-25 13:21:13 +08:00
|
|
|
|
2020-09-26 11:29:46 +08:00
|
|
|
for j in (0..n - 1).rev() {
|
2020-10-27 12:06:37 +08:00
|
|
|
let mut d_j = d[j];
|
2020-09-26 11:29:46 +08:00
|
|
|
for k in j + 1..n {
|
2020-10-27 12:06:37 +08:00
|
|
|
d_j += d[k] * u[(j, k)].powi(2);
|
2020-09-25 13:21:13 +08:00
|
|
|
}
|
|
|
|
|
2020-10-27 12:06:37 +08:00
|
|
|
d[j] = p[(j, j)] - d_j;
|
2020-09-25 13:21:13 +08:00
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
if d[j].is_zero() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2020-09-26 09:21:14 +08:00
|
|
|
for i in (0..=j).rev() {
|
2020-10-27 12:06:37 +08:00
|
|
|
let mut u_ij = u[(i, j)];
|
2020-09-26 11:29:46 +08:00
|
|
|
for k in j + 1..n {
|
2020-10-27 12:06:37 +08:00
|
|
|
u_ij += d[k] * u[(j, k)] * u[(i, k)];
|
2020-09-25 13:21:13 +08:00
|
|
|
}
|
2020-09-26 09:21:14 +08:00
|
|
|
|
2020-10-27 12:06:37 +08:00
|
|
|
u[(i, j)] = (p[(i, j)] - u_ij) / d[j];
|
2020-09-25 13:21:13 +08:00
|
|
|
}
|
2020-09-26 09:21:14 +08:00
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
u[(j, j)] = T::one();
|
2020-09-25 13:21:13 +08:00
|
|
|
}
|
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
Some(Self { u, d })
|
2020-09-25 13:21:13 +08:00
|
|
|
}
|
2020-09-28 05:28:50 +08:00
|
|
|
|
|
|
|
/// Returns the diagonal elements as a matrix
|
2021-06-07 22:34:03 +08:00
|
|
|
#[must_use]
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn d_matrix(&self) -> OMatrix<T, D, D> {
|
|
|
|
OMatrix::from_diagonal(&self.d)
|
2020-09-28 05:28:50 +08:00
|
|
|
}
|
2020-09-25 13:21:13 +08:00
|
|
|
}
|