Eigen  5.0.1
 
Loading...
Searching...
No Matches
BlockHouseholder.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2010 Vincent Lejeune
5// Copyright (C) 2010 Gael Guennebaud <gael.guennebaud@inria.fr>
6//
7// This Source Code Form is subject to the terms of the Mozilla
8// Public License v. 2.0. If a copy of the MPL was not distributed
9// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10// SPDX-License-Identifier: MPL-2.0
11
12#ifndef EIGEN_BLOCK_HOUSEHOLDER_H
13#define EIGEN_BLOCK_HOUSEHOLDER_H
14
15// This file contains some helper functions to deal with block householder reflectors
16
17// IWYU pragma: private
18#include "./InternalHeaderCheck.h"
19
20namespace Eigen {
21
22namespace internal {
23
25// This variant avoids modifications in vectors
26template <typename TriangularFactorType, typename VectorsType, typename CoeffsType>
27void make_block_householder_triangular_factor(TriangularFactorType& triFactor, const VectorsType& vectors,
28 const CoeffsType& hCoeffs) {
29 const Index nbVecs = vectors.cols();
30 eigen_assert(triFactor.rows() == nbVecs && triFactor.cols() == nbVecs && vectors.rows() >= nbVecs);
31
32 for (Index i = nbVecs - 1; i >= 0; --i) {
33 Index rs = vectors.rows() - i - 1;
34 Index rt = nbVecs - i - 1;
35
36 if (rt > 0) {
37 triFactor.row(i).tail(rt).noalias() = -hCoeffs(i) * vectors.col(i).tail(rs).adjoint() *
38 vectors.bottomRightCorner(rs, rt).template triangularView<UnitLower>();
39
40 triFactor.row(i).tail(rt) =
41 (triFactor.row(i).tail(rt) * triFactor.bottomRightCorner(rt, rt).template triangularView<Upper>()).eval();
42 }
43 triFactor(i, i) = hCoeffs(i);
44 }
45}
46
59EIGEN_DIAGNOSTICS(push)
60EIGEN_DIAGNOSTICS_OFF(disable : 4789, ignored "-Warray-bounds")
61
62template <typename MatrixType, typename VectorsType, typename CoeffsType>
63void apply_block_householder_on_the_left(MatrixType& mat, const VectorsType& vectors, const CoeffsType& hCoeffs,
64 bool forward) {
65 enum { TFactorSize = VectorsType::ColsAtCompileTime };
66 const Index nbVecs = vectors.cols();
67 const Index nbBelow = vectors.rows() - nbVecs;
68 Matrix<typename MatrixType::Scalar, TFactorSize, TFactorSize, RowMajor> T(nbVecs, nbVecs);
69
70 if (forward)
71 make_block_householder_triangular_factor(T, vectors, hCoeffs);
72 else
73 make_block_householder_triangular_factor(T, vectors, hCoeffs.conjugate());
74
75 const auto V_top = vectors.topRows(nbVecs);
76
77 // tmp = V^* * mat, computed as V_top^* * mat.topRows(nbVecs) + V_bot^* * mat.bottomRows(nbBelow).
78 Matrix<typename MatrixType::Scalar, VectorsType::ColsAtCompileTime, MatrixType::ColsAtCompileTime,
79 (VectorsType::MaxColsAtCompileTime == 1 && MatrixType::MaxColsAtCompileTime != 1) ? RowMajor : ColMajor,
80 VectorsType::MaxColsAtCompileTime, MatrixType::MaxColsAtCompileTime>
81 tmp(nbVecs, mat.cols());
82 tmp.noalias() = V_top.template triangularView<UnitLower>().adjoint() * mat.topRows(nbVecs);
83 if (nbBelow > 0) {
84 tmp.noalias() += vectors.bottomRows(nbBelow).adjoint() * mat.bottomRows(nbBelow);
85 }
86
87 if (forward)
88 tmp = (T.template triangularView<Upper>() * tmp).eval();
89 else
90 tmp = (T.template triangularView<Upper>().adjoint() * tmp).eval();
91
92 // mat -= V * tmp, split along the same top/bottom partition.
93 mat.topRows(nbVecs).noalias() -= V_top.template triangularView<UnitLower>() * tmp;
94 if (nbBelow > 0) {
95 mat.bottomRows(nbBelow).noalias() -= vectors.bottomRows(nbBelow) * tmp;
96 }
97}
98
103template <typename MatrixType, typename VectorsType, typename CoeffsType>
104void apply_block_householder_on_the_right(MatrixType& mat, const VectorsType& vectors, const CoeffsType& hCoeffs,
105 bool forward) {
106 enum { TFactorSize = VectorsType::ColsAtCompileTime };
107 const Index nbVecs = vectors.cols();
108 const Index nbBelow = vectors.rows() - nbVecs;
109 Matrix<typename MatrixType::Scalar, TFactorSize, TFactorSize, RowMajor> T(nbVecs, nbVecs);
110
111 if (forward)
112 make_block_householder_triangular_factor(T, vectors, hCoeffs);
113 else
114 make_block_householder_triangular_factor(T, vectors, hCoeffs.conjugate());
115
116 const auto V_top = vectors.topRows(nbVecs);
117
118 // tmp = mat * V, split along V's top/bottom partition (see the left-apply for context).
119 Matrix<typename MatrixType::Scalar, MatrixType::RowsAtCompileTime, VectorsType::ColsAtCompileTime,
120 (VectorsType::MaxColsAtCompileTime == 1 && MatrixType::MaxRowsAtCompileTime != 1) ? ColMajor : RowMajor,
121 MatrixType::MaxRowsAtCompileTime, VectorsType::MaxColsAtCompileTime>
122 tmp(mat.rows(), nbVecs);
123 tmp.noalias() = mat.leftCols(nbVecs) * V_top.template triangularView<UnitLower>();
124 if (nbBelow > 0) {
125 tmp.noalias() += mat.rightCols(nbBelow) * vectors.bottomRows(nbBelow);
126 }
127
128 if (forward)
129 tmp = (tmp * T.template triangularView<Upper>()).eval();
130 else
131 tmp = (tmp * T.template triangularView<Upper>().adjoint()).eval();
132
133 // mat -= tmp * V^*, split along the same partition.
134 mat.leftCols(nbVecs).noalias() -= tmp * V_top.template triangularView<UnitLower>().adjoint();
135 if (nbBelow > 0) {
136 mat.rightCols(nbBelow).noalias() -= tmp * vectors.bottomRows(nbBelow).adjoint();
137 }
138}
139
140EIGEN_DIAGNOSTICS(pop)
141
142} // end namespace internal
143
144} // end namespace Eigen
145
146#endif // EIGEN_BLOCK_HOUSEHOLDER_H
@ ColMajor
Definition Constants.h:319
@ RowMajor
Definition Constants.h:321