Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
momentum::OnlineHouseholderQR< T > Class Template Reference

Online (incremental) Householder QR solver for tall, column-sparse least-squares problems. More...

#include <online_householder_qr.h>

Public Types

using MatrixType = Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>
using VectorType = Eigen::Matrix<T, Eigen::Dynamic, 1>

Public Member Functions

 OnlineHouseholderQR (Eigen::Index n, T lambda=T(0))
 Constructs a solver for problems with n unknowns.
void addMutating (Eigen::Ref< MatrixType > A, Eigen::Ref< VectorType > b)
 In-place overload: factorizes A and b directly, mutating both buffers.
void addMutating (ColumnIndexedMatrix< MatrixType > A, Eigen::Ref< VectorType > b)
 In-place addition of a Jacobian/residual block to the running QR factorization.
void add (MatrixType A, VectorType b)
 Copying overload of addMutating().
VectorType result () const
 Solves R * x = y by back-substitution using the currently accumulated state and returns x.
const MatrixType & R () const
const VectorType & y () const
VectorType At_times_b () const
void reset ()
 Discards accumulated state, keeping the existing n and lambda.
void reset (Eigen::Index n, T lambda=T(0))
 Discards accumulated state and reconfigures the solver with new n and lambda.

Detailed Description

template<typename T>
class momentum::OnlineHouseholderQR< T >

Online (incremental) Householder QR solver for tall, column-sparse least-squares problems.

Solves

min || A * x - b ||_2

where a. n (the number of unknowns) is small-ish, b. A is tall and sparse (m >> n), BUT c. (A^T * A) is dense.

This turns out to be the common case for hand-model IK, because: a. The hand has only ~22 DOF, b. Each constraint touches only a few DOFs (typically the 3-4 on a single finger), but c. Constraints that touch the global DOFs tend to densify A^T*A. From the perspective of a QR factorization: since the R matrix has the same sparsity pattern as the L matrix in the Cholesky factorization of A^T*A, there is no point trying to exploit sparsity in the computation of R (e.g. by reordering columns). So instead, we accept the O(n^2) cost of a dense R and try to exploit sparsity in A.

It is possible to exploit sparsity in A if you do the factorization row-wise instead of column-wise as is generally done. One paper that uses Givens rotations to zero out one row at a time is: Solution of Sparse Linear Squares Problems using Givens Rotations (1980) by Alan George and Michael T. Heath

However, Givens rotations aren't really ideal for our case because you have to compute a c/s pair for each entry of the matrix, and it involves a square root. Also, row-wise operations are slow in general due to poor cache locality on Eigen matrices. What we do instead is apply Householder reflections to (k x n) blocks of the matrix; computing a Householder vector for a whole column only requires one square root, and it can be applied as a level-2 BLAS operation. Moreover, this maps perfectly to our problem; a Jacobian matrix from a single constraint has the sparsity pattern where entire columns are dropped, and these zero columns can just be skipped entirely by the Householder process provided we are careful about the order we process them in. And by processing the J submatrices one at a time, we never assemble the full A matrix, eliminating one major disadvantage of QR relative to the normal equations (hence the name "OnlineHouseholderQR").

Even better, we can do the whole operation in-place: the caller passes in the J and r matrix, and the columns of the J matrix are rotated out one by one, revealing the R matrix. At the end, a simple back-substitution using R gives the x vector. (In fact, the answer for the current submatrix of A,b is always available, as a consequence of doing the factorization sort-of row-wise.)

Note
Because the transforms are applied as we go, this method is not currently suitable for multiple right-hand-sides.

Usage contract

  1. Construct with the number of unknowns n and (optionally) a Levenberg-Marquardt damping lambda.
  2. Call add()/addMutating() once per Jacobian/residual block. Internal state (R_, y_) is mutated incrementally on each call.
  3. Call result() to back-substitute and recover x. R(), y(), and At_times_b() are also valid at any point and reflect the data accumulated so far.
  4. Call reset() to discard accumulated state and reuse the solver.

Definition at line 154 of file online_householder_qr.h.

Member Typedef Documentation

◆ MatrixType

template<typename T>
using momentum::OnlineHouseholderQR< T >::MatrixType = Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>

Definition at line 156 of file online_householder_qr.h.

◆ VectorType

template<typename T>
using momentum::OnlineHouseholderQR< T >::VectorType = Eigen::Matrix<T, Eigen::Dynamic, 1>

Definition at line 157 of file online_householder_qr.h.

Constructor & Destructor Documentation

◆ OnlineHouseholderQR()

template<typename T>
momentum::OnlineHouseholderQR< T >::OnlineHouseholderQR ( Eigen::Index n,
T lambda = T(0) )
explicit

Constructs a solver for problems with n unknowns.

The lambda parameter implements Gauss-Newton/Levenberg-Marquardt damping, emulating the augmented least-squares system

| [ lambda * I ] [ x ] - [ 0 ] |^2
| [ A ] [ b ] |

without materializing the extra lambda * I rows. R is initialized with lambda on its diagonal so the first add() call sees the regularization.

Definition at line 123 of file online_householder_qr.cpp.

References reset().

Member Function Documentation

◆ add()

template<typename T>
void momentum::OnlineHouseholderQR< T >::add ( MatrixType A,
VectorType b )

Copying overload of addMutating().

Note
Pass the arguments via std::move to avoid the internal copy when the caller no longer needs them.

Definition at line 166 of file online_householder_qr.cpp.

References addMutating().

◆ addMutating() [1/2]

template<typename T>
void momentum::OnlineHouseholderQR< T >::addMutating ( ColumnIndexedMatrix< MatrixType > A,
Eigen::Ref< VectorType > b )

In-place addition of a Jacobian/residual block to the running QR factorization.

Both A and b are required so we can apply Q to the right-hand side as R is being constructed. The factorization is computed inside the passed-in buffers, so callers who do not need the original data can avoid a copy.

Precondition
A.cols() equals the n passed to the constructor.
A.rows() equals b.size().
Postcondition
Internal R_ and y_ are updated to reflect the combined (prior + new) data. The contents of A and b are arbitrary on return.

Definition at line 171 of file online_householder_qr.cpp.

References momentum::ColumnIndexedMatrix< MatrixType >::col(), momentum::ColumnIndexedMatrix< MatrixType >::cols(), MT_CHECK, MT_PROFILE_FUNCTION, and momentum::ColumnIndexedMatrix< MatrixType >::rows().

◆ addMutating() [2/2]

template<typename T>
void momentum::OnlineHouseholderQR< T >::addMutating ( Eigen::Ref< MatrixType > A,
Eigen::Ref< VectorType > b )
inline

In-place overload: factorizes A and b directly, mutating both buffers.

Convenience wrapper that wraps A in a ColumnIndexedMatrix.

Definition at line 173 of file online_householder_qr.h.

References addMutating().

Referenced by add(), and addMutating().

◆ At_times_b()

template<typename T>
OnlineHouseholderQR< T >::VectorType momentum::OnlineHouseholderQR< T >::At_times_b ( ) const
nodiscard
Returns
A^T * b for the data accumulated so far, computed from R^T * y without needing the original A.

Definition at line 224 of file online_householder_qr.cpp.

◆ R()

template<typename T>
const MatrixType & momentum::OnlineHouseholderQR< T >::R ( ) const
inlinenodiscard
Returns
Upper-triangular R factor of the current accumulated factorization.

Definition at line 201 of file online_householder_qr.h.

◆ reset() [1/2]

template<typename T>
void momentum::OnlineHouseholderQR< T >::reset ( )

Discards accumulated state, keeping the existing n and lambda.

Definition at line 128 of file online_householder_qr.cpp.

References reset().

Referenced by OnlineHouseholderQR(), and reset().

◆ reset() [2/2]

template<typename T>
void momentum::OnlineHouseholderQR< T >::reset ( Eigen::Index n,
T lambda = T(0) )

Discards accumulated state and reconfigures the solver with new n and lambda.

Definition at line 133 of file online_householder_qr.cpp.

◆ result()

template<typename T>
OnlineHouseholderQR< T >::VectorType momentum::OnlineHouseholderQR< T >::result ( ) const
nodiscard

Solves R * x = y by back-substitution using the currently accumulated state and returns x.

Returns
Vector of length n containing the least-squares solution for the data added so far.

Definition at line 235 of file online_householder_qr.cpp.

◆ y()

template<typename T>
const VectorType & momentum::OnlineHouseholderQR< T >::y ( ) const
inlinenodiscard
Returns
The transformed right-hand side Q * b (top n entries).

Definition at line 205 of file online_householder_qr.h.


The documentation for this class was generated from the following files: