33void applyHouseholderTransformation(
35 const Eigen::Ref<
const Eigen::Matrix<T, Eigen::Dynamic, 1>>& v_2m,
37 Eigen::Ref<Eigen::Matrix<T, Eigen::Dynamic, 1>> y_2m) {
38 MT_CHECK(v_2m.size() == y_2m.size());
41 const T dotProd = y_1 + v_2m.dot(y_2m);
42 const T scalar = dotProd * beta;
46 y_2m.noalias() -= scalar * v_2m;
56template <
typename T,
typename MatrixType,
typename RowType>
57void applyHouseholderTransformationBlock(
59 const Eigen::Ref<
const Eigen::Matrix<T, Eigen::Dynamic, 1>>& v_2m,
62 Eigen::Index colStart,
63 Eigen::Index colEnd) {
64 [[maybe_unused]]
const auto nCols = Y_2m.cols();
65 MT_CHECK(v_2m.rows() == Y_2m.rows());
70 [[maybe_unused]]
const auto colIndices = Y_2m.columnIndices();
71 MT_CHECK(colIndices.empty() || colIndices.size() >= colEnd);
74 while (jCol < colEnd) {
75 applyHouseholderTransformation<T>(beta, v_2m, Y_1(jCol), Y_2m.col(jCol));
102std::pair<T, T> computeHouseholderVec(
104 Eigen::Ref<Eigen::Matrix<T, Eigen::Dynamic, 1>>
107 const T sigma = vec.squaredNorm();
112 const T mu = std::sqrt(
sqr(x1) + sigma);
113 const T v1 = (x1 <= 0) ? (x1 - mu) : (-sigma / (x1 + mu));
114 const T beta = T(2) *
sqr(v1) / (sigma +
sqr(v1));
117 return std::make_pair(beta, mu);
139 R_.diagonal().setConstant(lambda);
151 for (
const auto& idx : colIndices) {
155 std::vector<Eigen::Index> colsCheckDups(std::begin(colIndices), std::end(colIndices));
156 std::sort(colsCheckDups.begin(), colsCheckDups.end());
157 MT_CHECK(std::unique(colsCheckDups.begin(), colsCheckDups.end()) == colsCheckDups.end());
173 Eigen::Ref<VectorType> b) {
183 const Eigen::Index n = R_.rows();
186 for (Eigen::Index iCol = 0; iCol < n; ++iCol) {
201 const auto [beta, mu] = computeHouseholderVec<T>(R_(iCol, iCol), A.
col(iCol));
214 const auto& v2m = A.
col(iCol);
215 for (Eigen::Index jCol = iCol + 1; jCol < n; ++jCol) {
216 applyHouseholderTransformation<T>(beta, v2m, R_(iCol, jCol), A.
col(jCol));
219 applyHouseholderTransformation<T>(beta, v2m, y_(iCol), b);
231 return R_.transpose() * y_;
242 return R_.template triangularView<Eigen::Upper>().solve(y_);
253 R_nn_.diagonal().setConstant(lambda);
261 Eigen::Ref<VectorType> b) {
262 const Eigen::Index n_diag = A_diag.
cols();
263 const Eigen::Index n_common = R_nn_.cols();
268 while (R_ii_.size() <= iBlock) {
271 while (y_i_.size() <= iBlock) {
274 while (R_in_.size() <= iBlock) {
278 if (R_ii_[iBlock].rows() == 0) {
279 R_ii_[iBlock] = MatrixType::Zero(n_diag, n_diag);
280 R_ii_[iBlock].diagonal().setConstant(lambda_);
282 y_i_[iBlock] = VectorType::Zero(n_diag);
284 R_in_[iBlock] = MatrixType::Zero(n_diag, n_common);
290 auto&
R_ii = R_ii_[iBlock];
291 auto&
y_i = y_i_[iBlock];
292 auto& R_in = R_in_[iBlock];
298 for (Eigen::Index iCol = 0; iCol < n_diag; ++iCol) {
299 if (A_diag.
col(iCol).isZero()) {
320 const auto [beta, mu] = computeHouseholderVec<T>(
R_ii(iCol, iCol), A_diag.
col(iCol));
327 R_ii(iCol, iCol) = mu;
329 const auto& v2m = A_diag.
col(iCol);
330 if (iCol + 1 < n_diag) {
331 applyHouseholderTransformationBlock<T>(beta, v2m,
R_ii.row(iCol), A_diag, iCol + 1, n_diag);
337 applyHouseholderTransformationBlock<T>(beta, v2m, R_in.row(iCol), A_common, 0, n_common);
340 applyHouseholderTransformation<T>(beta, v2m,
y_i(iCol), b);
346 for (Eigen::Index iCol = 0; iCol < n_common; ++iCol) {
347 const auto [beta, mu] = computeHouseholderVec<T>(R_nn(iCol, iCol), A_common.
col(iCol));
352 R_nn(iCol, iCol) = mu;
354 const auto& v2m = A_common.
col(iCol);
355 for (Eigen::Index jCol_common = iCol + 1; jCol_common < n_common; ++jCol_common) {
356 applyHouseholderTransformation<T>(
357 beta, v2m, R_nn_(iCol, jCol_common), A_common.
col(jCol_common));
360 applyHouseholderTransformation<T>(beta, v2m, y_n_(iCol), b);
366 const auto nCol_common = R_nn_.cols();
368 Eigen::Index nCol_diag = 0;
369 for (
const auto&
R_ii : R_ii_) {
370 nCol_diag +=
R_ii.cols();
373 const Eigen::Index nCol_total = nCol_diag + nCol_common;
375 MatrixType result = MatrixType::Zero(nCol_total, nCol_total);
377 for (
size_t iBlock = 0; iBlock < R_ii_.size(); ++iBlock) {
378 const auto&
R_ii = R_ii_[iBlock];
379 const auto& R_in = R_in_[iBlock];
381 MT_CHECK(R_in.cols() == nCol_common);
382 result.block(offset_cur, offset_cur,
R_ii.rows(),
R_ii.cols()) =
R_ii;
383 result.block(offset_cur, nCol_diag, R_in.rows(), nCol_common) = R_in;
384 offset_cur +=
R_ii.rows();
386 result.block(offset_cur, offset_cur, nCol_common, nCol_common) = R_nn_;
392 const auto nRow_common = y_n_.rows();
394 Eigen::Index nRow_diag = 0;
395 for (
const auto&
y_i : y_i_) {
396 nRow_diag +=
y_i.rows();
399 const Eigen::Index nRow_total = nRow_diag + nRow_common;
402 VectorType result = VectorType::Zero(nRow_total);
403 for (
size_t iBlock = 0; iBlock < R_ii_.size(); ++iBlock) {
404 const auto&
y_i = y_i_[iBlock];
405 result.segment(offset_cur,
y_i.rows()) =
y_i;
406 offset_cur +=
y_i.rows();
409 result.segment(offset_cur, nRow_common) = y_n_;
420 const auto nBlocks = R_ii_.size();
421 Eigen::Index nRows_diag = 0;
422 for (
const auto& block : R_ii_) {
423 nRows_diag += block.cols();
425 const Eigen::Index nRows_common = R_nn_.cols();
426 const Eigen::Index nRows_total = nRows_diag + nRows_common;
429 result.segment(nRows_diag, nRows_common) =
430 R_nn_.template triangularView<Eigen::Upper>().solve(y_n_);
432 const auto&
x_n = result.segment(nRows_diag, nRows_common);
433 Eigen::Index offset = 0;
434 for (
size_t i = 0; i < nBlocks; ++i) {
435 const auto nRows_cur = R_ii_[i].cols();
436 result.segment(offset, nRows_cur) =
437 R_ii_[i].template triangularView<Eigen::Upper>().solve(y_i_[i] - R_in_[i] *
x_n);
446 return R_nn_.template triangularView<Eigen::Upper>().solve(y_n_);
451 size_t iBlock)
const {
452 return R_ii_[iBlock].template triangularView<Eigen::Upper>().solve(
453 y_i_[iBlock] - R_in_[iBlock] * (R_nn_.template triangularView<Eigen::Upper>().solve(y_n_)));
458 size_t iBlock)
const {
462 return R_ii_[iBlock].transpose() * y_i_[iBlock];
470 for (
size_t iBlock = 0; iBlock < R_ii_.size(); ++iBlock) {
471 result.noalias() += R_in_[iBlock].transpose() * y_i_[iBlock];
479 Eigen::Index nRows_total = y_n_.rows();
480 for (
const auto&
y_i : y_i_) {
481 nRows_total +=
y_i.rows();
485 Eigen::Index offset = 0;
486 for (
size_t iBlock = 0; iBlock < R_ii_.size(); ++iBlock) {
487 const auto nRows_cur = y_i_[iBlock].rows();
488 result.segment(offset, nRows_cur).noalias() = R_ii_[iBlock].transpose() * y_i_[iBlock];
492 result.segment(offset, y_n_.rows()).noalias() = R_nn_.transpose() * y_n_;
493 for (
size_t iBlock = 0; iBlock < R_ii_.size(); ++iBlock) {
494 result.segment(offset, y_n_.rows()).noalias() += R_in_[iBlock].transpose() * y_i_[iBlock];
512 R_nn_.diagonal().setConstant(lambda_);
521 const Eigen::Index
n_band,
539 initializeDiagonal();
543void OnlineBandedHouseholderQR<T>::initializeDiagonal() {
548 const auto n_common = R_common_.cols();
551 R_common_.bottomRightCorner(n_common, n_common).diagonal().setConstant(lambda_);
554 if (R_band_.rows() > 0 && R_band_.cols() > 0) {
555 R_band_.template bottomRows<1>().setConstant(lambda_);
565 initializeDiagonal();
570 const Eigen::Index iCol_offset,
573 Eigen::Ref<VectorType> b) {
580 const Eigen::Index iCol_offset,
583 Eigen::Ref<VectorType> b) {
584 const auto n_common = R_common_.cols();
589 MT_CHECK(iCol_offset + A_band.
cols() <= R_band_.cols());
595 for (Eigen::Index iCol_local = 0; iCol_local < A_band.
cols(); ++iCol_local) {
596 const Eigen::Index iCol_global = iCol_local + iCol_offset;
612 const auto [beta, mu] =
613 computeHouseholderVec<T>(R_band_entry(iCol_global, iCol_global), A_band.
col(iCol_local));
620 R_band_entry(iCol_global, iCol_global) = mu;
622 const auto& v2m = A_band.
col(iCol_local);
627 for (Eigen::Index jCol_local = iCol_local + 1; jCol_local < A_band.
cols(); ++jCol_local) {
628 const auto jCol_global = jCol_local + iCol_offset;
629 applyHouseholderTransformation<T>(
630 beta, v2m, R_band_entry(iCol_global, jCol_global), A_band.
col(jCol_local));
635 applyHouseholderTransformationBlock<T>(
636 beta, v2m, R_common_.row(iCol_global), A_common, 0,
n_common);
639 applyHouseholderTransformation<T>(beta, v2m, y_(iCol_global), b);
646 Eigen::Ref<VectorType> b) {
652 const auto n_common = R_common_.cols();
653 const auto n_band = R_band_.cols();
658 for (Eigen::Index iCol = 0; iCol <
n_common; ++iCol) {
659 const auto [beta, mu] =
660 computeHouseholderVec<T>(R_common_(iCol +
n_band, iCol), A_common.
col(iCol));
665 R_common_(iCol +
n_band, iCol) = mu;
667 const auto& v2m = A_common.
col(iCol);
668 for (Eigen::Index jCol_common = iCol + 1; jCol_common <
n_common; ++jCol_common) {
669 applyHouseholderTransformation<T>(
670 beta, v2m, R_common_(iCol +
n_band, jCol_common), A_common.
col(jCol_common));
673 applyHouseholderTransformation<T>(beta, v2m, y_(
n_band + iCol), b);
683 const auto n_common = R_common_.cols();
684 const auto n_band = R_band_.cols();
689 MatrixType result = MatrixType::Zero(n_total, n_total);
693 for (Eigen::Index iCol_band = 0; iCol_band <
n_band; ++iCol_band) {
694 for (Eigen::Index jRow = iCol_band; (iCol_band - jRow) <
bandwidth && jRow >= 0; --jRow) {
695 result(jRow, iCol_band) = R_band_entry(jRow, iCol_band);
699 for (Eigen::Index jRow = 0; jRow < n_total; ++jRow) {
700 for (Eigen::Index iCol_common = 0; iCol_common <
n_common; ++iCol_common) {
701 result(jRow,
n_band + iCol_common) = R_common_(jRow, iCol_common);
716 VectorType result = VectorType::Zero(R_band_.cols() + R_common_.cols());
718 const auto n_common = R_common_.cols();
719 const auto n_band = R_band_.cols();
724 R_common_.bottomRows(
n_common).template triangularView<Eigen::Upper>().solve(
735 for (Eigen::Index iRow =
n_band - 1; iRow >= 0; --iRow) {
736 const T diag = R_band_entry(iRow, iRow);
742 T dotProd =
n_common > 0 ? R_common_.row(iRow).dot(result.tail(
n_common)) : T(0);
745 for (Eigen::Index jColOffset = 1; jColOffset < bandwidth_cur; ++jColOffset) {
746 dotProd += R_band_entry(iRow, iRow + jColOffset) * result(iRow + jColOffset);
748 result(iRow) = (y_(iRow) - dotProd) / diag;
761 VectorType result = VectorType::Zero(R_band_.cols() + R_common_.cols());
763 const auto n_common = R_common_.cols();
764 const auto n_band = R_band_.cols();
767 for (Eigen::Index iRow = 0; iRow <
n_band; ++iRow) {
769 for (Eigen::Index jColOffset = 0; jColOffset < bandwidth_cur; ++jColOffset) {
770 const Eigen::Index jCol = iRow + jColOffset;
771 result(jCol) += R_band_entry(iRow, jCol) * y_(iRow);
775 result.tail(
n_common).noalias() = R_common_.transpose() * y_;
#define MT_CHECK(condition,...)
Runtime check macros with two interchangeable backends:
View over a matrix that exposes a (possibly reordered) subset of its columns.
auto col(Eigen::Index colIdx)
Online Householder QR solver for band-plus-arrowhead matrices.
Eigen::Matrix< T, Eigen::Dynamic, 1 > VectorType
Eigen::Index bandwidth() const
VectorType x_dense() const
Retrieves the full dense result vector of length n_band() + n_common().
Eigen::Index n_common() const
VectorType At_times_b() const
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > MatrixType
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...
Eigen::Index n_band() const
MatrixType R_dense() const
void reset()
Discards accumulated state, keeping the existing dimensions and damping.
OnlineBandedHouseholderQR(Eigen::Index n_band, Eigen::Index n_common, Eigen::Index bandwidth, T lambda=T(0))
Constructs a banded solver.
Online Householder QR solver for least-squares problems with block-arrowhead structure.
MatrixType R_dense() const
VectorType At_times_b_dense() const
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
MatrixType y_dense() 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
VectorType At_times_b_n() const
Online (incremental) Householder QR solver for tall, column-sparse least-squares problems.
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.
VectorType At_times_b() const
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.
Eigen::Matrix< T, Dynamic, Dynamic > MatrixX
Dynamic-size, column-major dense matrix.
Vector< T, Dynamic > VectorX
Dynamic-size column vector. Common return type for solver residuals and parameter vectors.
T sqr(const T &x)
Computes the square of a value.
gsl::span< T > span
Span type alias using gsl::span (C++17 fallback).
Profiling macros that dispatch to one of three backends, selected at compile time:
#define MT_PROFILE_FUNCTION()