Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
covariance_matrix.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
9
11
12namespace momentum {
13
14template <typename T>
15void LowRankCovarianceMatrixT<T>::reset(T sigma, Eigen::Ref<const Eigen::MatrixX<T>> A) {
16 A_ = A;
17 sigma_ = sigma;
18
19 const Eigen::Index n = A_.cols();
20 const Eigen::Index m = A_.rows();
21
22 // Initialize the online QR with a sigma*I diagonal, then stream in the rows of A.
23 // The resulting R is the upper-triangular factor of the augmented matrix
24 // A_aug = [ sigma*I ; A ] so that A_aug^T * A_aug = sigma^2*I + A^T*A = C.
25 // Equivalently, R is (up to sign of diagonals) the Cholesky factor of C.
26 // The dummy zero rhs is required by the online QR API but unused here.
27 qrFactorization_.reset(n, sigma);
28 Eigen::VectorX<T> b = Eigen::VectorX<T>::Zero(m);
29 qrFactorization_.add(A_, b);
30}
31
32template <typename T>
34 return A_.cols();
35}
36
37template <typename T>
38const Eigen::MatrixX<T>& LowRankCovarianceMatrixT<T>::basis() const {
39 return A_;
40}
41
42template <typename T>
44 return sigma_;
45}
46
47template <typename T>
49 Eigen::Ref<const Eigen::VectorX<T>> rhs) const {
50 const auto& R = qrFactorization_.R();
51 MT_CHECK(R.rows() == rhs.size());
52
53 // C^{-1} * rhs = (R^T * R)^{-1} * rhs = R^{-1} * R^{-T} * rhs.
54 // Implemented as two triangular back-solves: first R^T y = rhs, then R x = y.
55 return R.template triangularView<Eigen::Upper>().solve(
56 R.template triangularView<Eigen::Upper>().transpose().solve(rhs));
57}
58
59template <typename T>
61 Eigen::Ref<const Eigen::VectorX<T>> rhs) const {
62 // C * rhs = (sigma^2 * I + A^T * A) * rhs computed directly from the stored
63 // basis A_; avoids forming the dense [n x n] matrix and exploits the low-rank
64 // structure (cost O(m*n) instead of O(n^2)).
65 Eigen::VectorX<T> result = (sigma_ * sigma_) * rhs;
66 result.noalias() += A_.transpose() * (A_ * rhs);
67 return result;
68}
69
70template <typename T>
72 Eigen::Ref<const Eigen::MatrixX<T>> rhs) const {
73 const auto& R = qrFactorization_.R();
74 MT_CHECK(rhs.rows() == dimension());
75
76 // Same C^{-1} = R^{-1} * R^{-T} solve as inverse_times_vec, applied column-wise.
77 return R.template triangularView<Eigen::Upper>().solve(
78 R.template triangularView<Eigen::Upper>().transpose().solve(rhs));
79}
80
81template <typename T>
83 Eigen::Ref<const Eigen::MatrixX<T>> rhs) const {
84 Eigen::MatrixX<T> result = (sigma_ * sigma_) * rhs;
85 result.noalias() += A_.transpose() * (A_ * rhs);
86 return result;
87}
88
89template <typename T>
90const Eigen::MatrixX<T>& LowRankCovarianceMatrixT<T>::R() const {
91 return qrFactorization_.R();
92}
93
94template <typename T>
96 const auto& R = qrFactorization_.R();
97
98 // log|R| = sum of log of diagonal entries (R is upper triangular).
99 // The MT_CHECK enforces strictly positive diagonals, which the
100 // OnlineHouseholderQR is expected to produce because the augmented matrix
101 // [sigma*I; A] has full column rank for any sigma > 0 (so R is non-singular)
102 // and the implementation chooses positive diagonal signs.
103 T result = 0;
104 for (Eigen::Index i = 0; i < dimension(); ++i) {
105 MT_CHECK(R(i, i) > 0);
106 result += std::log(R(i, i));
107 }
108
109 // C = R^T * R, so log|C| = log|R^T| + log|R| = 2 * log|R|.
110 return T(2) * result;
111}
112
113template <typename T>
117
120
121} // namespace momentum
#define MT_CHECK(condition,...)
Runtime check macros with two interchangeable backends:
Definition checks.h:36
Efficient and numerically stable representation of a low-rank-plus-diagonal Gaussian covariance matri...
Eigen::MatrixX< T > times_mat(Eigen::Ref< const Eigen::MatrixX< T > > rhs) const
Column-wise application of C to a matrix rhs.
Eigen::VectorX< T > times_vec(Eigen::Ref< const Eigen::VectorX< T > > rhs) const
Returns C * rhs = sigma^2 * rhs + A^T * (A * rhs).
Eigen::VectorX< T > inverse_times_vec(Eigen::Ref< const Eigen::VectorX< T > > rhs) const
Returns C^{-1} * rhs, computed via two triangular solves on R.
Eigen::Index dimension() const
Returns n, the dimension of the covariance (number of columns of A).
const Eigen::MatrixX< T > & R() const
Returns the upper-triangular R factor of the QR factorization of [sigma*I; A], so that C = R^T * R.
T inverse_logDeterminant() const
Returns log(det(C^{-1})) = -log(det(C)).
const Eigen::MatrixX< T > & basis() const
Returns the stored basis A passed to reset().
void reset(T sigma, Eigen::Ref< const Eigen::MatrixX< T > > A)
(Re)build the covariance from a regularizer sigma and basis A.
Eigen::MatrixX< T > inverse_times_mat(Eigen::Ref< const Eigen::MatrixX< T > > rhs) const
Column-wise application of C^{-1} to a matrix rhs.
T sigma() const
Returns the diagonal regularizer sigma passed to reset().
T logDeterminant() const
Returns log(det(C)), computed stably as 2 * sum(log|diag(R)|).