Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
online_householder_qr.h
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
8#pragma once
9
14
15#include <Eigen/Core>
16
17#include <deque>
18#include <vector>
19
20namespace momentum {
21
25void validateColumnIndices(momentum::span<const Eigen::Index> colIndices, Eigen::Index maxEntry);
26
37template <typename MatrixType>
39 public:
40 using VectorType = Eigen::Matrix<typename MatrixType::Scalar, Eigen::Dynamic, 1>;
41
42 explicit ColumnIndexedMatrix(Eigen::Ref<MatrixType> mat) : mat_(mat), useColumnIndices_(false) {}
43
45 Eigen::Ref<MatrixType> mat,
47 : mat_(mat), columnIndices_(colIndices), useColumnIndices_(true) {
48 validateColumnIndices(colIndices, mat.cols());
49 }
50
51 [[nodiscard]] auto col(Eigen::Index colIdx) {
52 if (useColumnIndices_) {
53 return mat_.col(columnIndices_[colIdx]);
54 } else {
55 return mat_.col(colIdx);
56 }
57 }
58
59 [[nodiscard]] auto rows() const {
60 return mat_.rows();
61 }
62
63 [[nodiscard]] auto cols() const {
64 if (useColumnIndices_) {
65 return (Eigen::Index)columnIndices_.size();
66 } else {
67 return mat_.cols();
68 }
69 }
70
72 return columnIndices_;
73 }
74
78 [[nodiscard]] VectorType transpose_times(Eigen::Ref<VectorType> b) const {
79 if (useColumnIndices_) {
80 VectorType result(columnIndices_.size());
81 for (Eigen::Index iCol = 0; iCol < (Eigen::Index)columnIndices_.size(); ++iCol) {
82 result[iCol] = mat_.col(columnIndices_[iCol]).dot(b);
83 }
84 return result;
85 } else {
86 return mat_.transpose() * b;
87 }
88 }
89
90 private:
91 Eigen::Ref<MatrixType> mat_;
93 bool useColumnIndices_;
94};
95
153template <typename T>
155 public:
156 using MatrixType = Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>;
157 using VectorType = Eigen::Matrix<T, Eigen::Dynamic, 1>;
158
169 explicit OnlineHouseholderQR(Eigen::Index n, T lambda = T(0));
170
173 void addMutating(Eigen::Ref<MatrixType> A, Eigen::Ref<VectorType> b) {
175 }
176
187 void addMutating(ColumnIndexedMatrix<MatrixType> A, Eigen::Ref<VectorType> b);
188
192 void add(MatrixType A, VectorType b);
193
198 [[nodiscard]] VectorType result() const;
199
201 [[nodiscard]] const MatrixType& R() const {
202 return R_;
203 }
204
205 [[nodiscard]] const VectorType& y() const {
206 return y_;
207 }
208
210 [[nodiscard]] VectorType At_times_b() const;
211
213 void reset();
215 void reset(Eigen::Index n, T lambda = T(0));
216
217 private:
219 MatrixType R_;
220
222 VectorType y_;
223};
224
259template <typename T>
261 public:
262 using MatrixType = Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>;
263 using VectorType = Eigen::Matrix<T, Eigen::Dynamic, 1>;
264
274 explicit OnlineBlockHouseholderQR(Eigen::Index n_common, T lambda = T(0));
275
277 void add(size_t iBlock, MatrixType A_diag, MatrixType A_common, VectorType b) {
278 addMutating(iBlock, A_diag, A_common, b);
279 }
280
284 size_t iBlock,
285 Eigen::Ref<MatrixType> A_diag,
286 Eigen::Ref<MatrixType> A_common,
287 Eigen::Ref<VectorType> b) {
289 iBlock,
292 b);
293 }
294
308 void addMutating(
309 size_t iBlock,
312 Eigen::Ref<VectorType> b);
313
314 // You can retrieve the result x in one of two ways:
315 // 1. As a single, dense vector, which solves everything simultaneously, or
316 // 2. A block at a time.
317 // The latter is a little bit slower because it re-does the back-substitution
318 // for the final rows every time, but for the common case where the last set
319 // of variables is very small this should not be an issue.
320
322 [[nodiscard]] VectorType x_dense() const;
323
327 [[nodiscard]] VectorType x_i(size_t iBlock) const;
328
330 [[nodiscard]] VectorType x_n() const;
331
332 [[nodiscard]] const MatrixType& R_ii(size_t iBlock) const {
333 return R_ii_[iBlock];
334 }
335
336 [[nodiscard]] const VectorType& y_i(size_t iBlock) const {
337 return y_i_[iBlock];
338 }
339
340 [[nodiscard]] MatrixType R_dense() const;
341 [[nodiscard]] MatrixType y_dense() const;
342
343 [[nodiscard]] VectorType At_times_b_i(size_t iBlock) const;
344 [[nodiscard]] VectorType At_times_b_n() const;
345 [[nodiscard]] VectorType At_times_b_dense() const;
346 [[nodiscard]] T At_times_b_dot(Eigen::Ref<const VectorType> rhs) const;
347
348 void reset();
349
350 private:
351 const double lambda_;
352
360 std::deque<MatrixType> R_ii_;
361 std::deque<MatrixType> R_in_;
362 MatrixType R_nn_;
363
366 std::deque<VectorType> y_i_;
367 VectorType y_n_;
368};
369
405template <typename T>
407 public:
408 using MatrixType = Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>;
409 using VectorType = Eigen::Matrix<T, Eigen::Dynamic, 1>;
410
418 Eigen::Index n_band,
419 Eigen::Index n_common,
420 Eigen::Index bandwidth,
421 T lambda = T(0));
422
424 void add(size_t iCol_offset, MatrixType A_band, MatrixType A_common, VectorType b) {
425 addMutating(iCol_offset, A_band, A_common, b);
426 }
427
429 void add(MatrixType A_common, VectorType b) {
430 addMutating(A_common, b);
431 }
432
436 const Eigen::Index iCol_offset,
437 Eigen::Ref<MatrixType> A_band,
438 Eigen::Ref<MatrixType> A_common,
439 Eigen::Ref<VectorType> b) {
441 iCol_offset,
444 b);
445 }
446
448 void addMutating(Eigen::Ref<MatrixType> A_common, Eigen::Ref<VectorType> b) {
450 }
451
462 void addMutating(
463 Eigen::Index iCol_offset,
466 Eigen::Ref<VectorType> b);
467
469 void addMutating(ColumnIndexedMatrix<MatrixType> A_common, Eigen::Ref<VectorType> b);
470
488 void zeroBandedPart(
489 Eigen::Index iCol_offset,
492 Eigen::Ref<VectorType> b);
493
494 // You can retrieve the result x in one of two ways:
495 // 1. As a single, dense vector, which solves everything simultaneously, or
496 // 2. A block at a time.
497 // The latter is a little bit slower because it re-does the back-substitution
498 // for the final rows every time, but for the common case where the last set
499 // of variables is very small this should not be an issue.
500
502 [[nodiscard]] VectorType x_dense() const;
505 [[nodiscard]] MatrixType R_dense() const;
507 [[nodiscard]] MatrixType y_dense() const {
508 return y_;
509 }
510
512 [[nodiscard]] VectorType At_times_b() const;
513
515 void reset();
516
517 [[nodiscard]] Eigen::Index bandwidth() const {
518 return R_band_.rows();
519 }
520
521 [[nodiscard]] Eigen::Index n_band() const {
522 return R_band_.cols();
523 }
524
525 [[nodiscard]] Eigen::Index n_common() const {
526 return R_common_.cols();
527 }
528
529 private:
532 void initializeDiagonal();
533
534 [[nodiscard]] T R_band_entry(Eigen::Index iRow, Eigen::Index jCol) const {
535 const auto bandwidth = R_band_.rows();
536 MT_CHECK(iRow <= jCol);
537 MT_CHECK(jCol - iRow <= bandwidth);
538 return R_band_(bandwidth + iRow - jCol - 1, jCol);
539 }
540
541 [[nodiscard]] T& R_band_entry(Eigen::Index iRow, Eigen::Index jCol) {
542 const auto bandwidth = R_band_.rows();
543 MT_CHECK(iRow <= jCol);
544 MT_CHECK(jCol - iRow <= bandwidth);
545 return R_band_(bandwidth + iRow - jCol - 1, jCol);
546 }
547
548 const double lambda_;
549
562 MatrixType R_common_;
563
572 MatrixType R_band_;
573
575 VectorType y_;
576};
577
578} // namespace momentum
#define MT_CHECK(condition,...)
Runtime check macros with two interchangeable backends:
Definition checks.h:36
View over a matrix that exposes a (possibly reordered) subset of its columns.
momentum::span< const Eigen::Index > columnIndices() const
auto col(Eigen::Index colIdx)
ColumnIndexedMatrix(Eigen::Ref< MatrixType > mat)
VectorType transpose_times(Eigen::Ref< VectorType > b) const
Computes this->transpose() * b, returning a vector of length cols().
ColumnIndexedMatrix(Eigen::Ref< MatrixType > mat, momentum::span< const Eigen::Index > colIndices)
Eigen::Matrix< typename MatrixType::Scalar, Eigen::Dynamic, 1 > VectorType
Eigen::Matrix< T, Eigen::Dynamic, 1 > VectorType
VectorType x_dense() const
Retrieves the full dense result vector of length n_band() + n_common().
void add(size_t iCol_offset, MatrixType A_band, MatrixType A_common, VectorType b)
Copying overload of addMutating() that takes both banded and common contributions.
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > MatrixType
void add(MatrixType A_common, VectorType b)
Copying overload for contributions that only touch the common columns.
void zeroBandedPart(Eigen::Index iCol_offset, ColumnIndexedMatrix< MatrixType > A_band, ColumnIndexedMatrix< MatrixType > A_common, Eigen::Ref< VectorType > b)
Zeros out just the banded part of A_band by applying Householder rotations.
void addMutating(const Eigen::Index iCol_offset, Eigen::Ref< MatrixType > A_band, Eigen::Ref< MatrixType > A_common, Eigen::Ref< VectorType > b)
In-place addition; convenience wrapper that wraps the matrices in ColumnIndexedMatrix views without r...
void reset()
Discards accumulated state, keeping the existing dimensions and damping.
void addMutating(Eigen::Ref< MatrixType > A_common, Eigen::Ref< VectorType > b)
In-place addition for contributions that only touch the common columns.
OnlineBandedHouseholderQR(Eigen::Index n_band, Eigen::Index n_common, Eigen::Index bandwidth, T lambda=T(0))
Constructs a banded solver.
VectorType x_i(size_t iBlock) const
Retrieves the answer for a single diagonal block iBlock.
VectorType x_n() const
Retrieves the answer for just the final "common" rows.
OnlineBlockHouseholderQR(Eigen::Index n_common, T lambda=T(0))
Constructs a solver for problems with n_common shared unknowns.
const MatrixType & R_ii(size_t iBlock) const
VectorType x_dense() const
Retrieves the full dense result [x_0; x_1; ...; x_{B-1}; x_n].
T At_times_b_dot(Eigen::Ref< const VectorType > rhs) const
void addMutating(size_t iBlock, Eigen::Ref< MatrixType > A_diag, Eigen::Ref< MatrixType > A_common, Eigen::Ref< VectorType > b)
In-place addition; convenience wrapper that wraps the matrices in ColumnIndexedMatrix views without r...
const VectorType & y_i(size_t iBlock) const
Eigen::Matrix< T, Eigen::Dynamic, 1 > VectorType
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > MatrixType
VectorType At_times_b_i(size_t iBlock) const
void add(size_t iBlock, MatrixType A_diag, MatrixType A_common, VectorType b)
Copying overload of addMutating().
void reset()
Discards accumulated state, keeping the existing n and lambda.
Eigen::Matrix< T, Eigen::Dynamic, 1 > VectorType
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > MatrixType
void add(MatrixType A, VectorType b)
Copying overload of addMutating().
OnlineHouseholderQR(Eigen::Index n, T lambda=T(0))
Constructs a solver for problems with n unknowns.
VectorType result() const
Solves R * x = y by back-substitution using the currently accumulated state and returns x.
void addMutating(Eigen::Ref< MatrixType > A, Eigen::Ref< VectorType > b)
In-place overload: factorizes A and b directly, mutating both buffers.
void validateColumnIndices(momentum::span< const Eigen::Index > colIndices, Eigen::Index maxEntry)
Validates that every entry in colIndices is in [0, maxEntry); throws otherwise.
gsl::span< T > span
Span type alias using gsl::span (C++17 fallback).
Definition span_compat.h:54
Provides a C++17-compatible span type alias.