Eigen  3.4.90 (git rev 5a9f66fb35d03a4da9ef8976e67a61b30aa16dcf)
 
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 <[email protected]>
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
11#ifndef EIGEN_BLOCK_HOUSEHOLDER_H
12#define EIGEN_BLOCK_HOUSEHOLDER_H
13
14// This file contains some helper function to deal with block householder reflectors
15
16// IWYU pragma: private
17#include "./InternalHeaderCheck.h"
18
19namespace Eigen {
20
21namespace internal {
22
24// template<typename TriangularFactorType,typename VectorsType,typename CoeffsType>
25// void make_block_householder_triangular_factor(TriangularFactorType& triFactor, const VectorsType& vectors, const
26// CoeffsType& hCoeffs)
27// {
28// typedef typename VectorsType::Scalar Scalar;
29// const Index nbVecs = vectors.cols();
30// eigen_assert(triFactor.rows() == nbVecs && triFactor.cols() == nbVecs && vectors.rows()>=nbVecs);
31//
32// for(Index i = 0; i < nbVecs; i++)
33// {
34// Index rs = vectors.rows() - i;
35// // Warning, note that hCoeffs may alias with vectors.
36// // It is then necessary to copy it before modifying vectors(i,i).
37// typename CoeffsType::Scalar h = hCoeffs(i);
38// // This hack permits to pass trough nested Block<> and Transpose<> expressions.
39// Scalar *Vii_ptr = const_cast<Scalar*>(vectors.data() + vectors.outerStride()*i + vectors.innerStride()*i);
40// Scalar Vii = *Vii_ptr;
41// *Vii_ptr = Scalar(1);
42// triFactor.col(i).head(i).noalias() = -h * vectors.block(i, 0, rs, i).adjoint()
43// * vectors.col(i).tail(rs);
44// *Vii_ptr = Vii;
45// // FIXME add .noalias() once the triangular product can work inplace
46// triFactor.col(i).head(i) = triFactor.block(0,0,i,i).template triangularView<Upper>()
47// * triFactor.col(i).head(i);
48// triFactor(i,i) = hCoeffs(i);
49// }
50// }
51
53// This variant avoid modifications in vectors
54template <typename TriangularFactorType, typename VectorsType, typename CoeffsType>
55void make_block_householder_triangular_factor(TriangularFactorType& triFactor, const VectorsType& vectors,
56 const CoeffsType& hCoeffs) {
57 const Index nbVecs = vectors.cols();
58 eigen_assert(triFactor.rows() == nbVecs && triFactor.cols() == nbVecs && vectors.rows() >= nbVecs);
59
60 for (Index i = nbVecs - 1; i >= 0; --i) {
61 Index rs = vectors.rows() - i - 1;
62 Index rt = nbVecs - i - 1;
63
64 if (rt > 0) {
65 triFactor.row(i).tail(rt).noalias() = -hCoeffs(i) * vectors.col(i).tail(rs).adjoint() *
66 vectors.bottomRightCorner(rs, rt).template triangularView<UnitLower>();
67
68 // FIXME use the following line with .noalias() once the triangular product can work inplace
69 // triFactor.row(i).tail(rt) = triFactor.row(i).tail(rt) * triFactor.bottomRightCorner(rt,rt).template
70 // triangularView<Upper>();
71 for (Index j = nbVecs - 1; j > i; --j) {
72 typename TriangularFactorType::Scalar z = triFactor(i, j);
73 triFactor(i, j) = z * triFactor(j, j);
74 if (nbVecs - j - 1 > 0) triFactor.row(i).tail(nbVecs - j - 1) += z * triFactor.row(j).tail(nbVecs - j - 1);
75 }
76 }
77 triFactor(i, i) = hCoeffs(i);
78 }
79}
80
85template <typename MatrixType, typename VectorsType, typename CoeffsType>
86void apply_block_householder_on_the_left(MatrixType& mat, const VectorsType& vectors, const CoeffsType& hCoeffs,
87 bool forward) {
88 enum { TFactorSize = VectorsType::ColsAtCompileTime };
89 Index nbVecs = vectors.cols();
90 Matrix<typename MatrixType::Scalar, TFactorSize, TFactorSize, RowMajor> T(nbVecs, nbVecs);
91
92 if (forward)
93 make_block_householder_triangular_factor(T, vectors, hCoeffs);
94 else
95 make_block_householder_triangular_factor(T, vectors, hCoeffs.conjugate());
96 const TriangularView<const VectorsType, UnitLower> V(vectors);
97
98 // A -= V T V^* A
99 Matrix<typename MatrixType::Scalar, VectorsType::ColsAtCompileTime, MatrixType::ColsAtCompileTime,
100 (VectorsType::MaxColsAtCompileTime == 1 && MatrixType::MaxColsAtCompileTime != 1) ? RowMajor : ColMajor,
101 VectorsType::MaxColsAtCompileTime, MatrixType::MaxColsAtCompileTime>
102 tmp = V.adjoint() * mat;
103 // FIXME add .noalias() once the triangular product can work inplace
104 if (forward)
105 tmp = T.template triangularView<Upper>() * tmp;
106 else
107 tmp = T.template triangularView<Upper>().adjoint() * tmp;
108 mat.noalias() -= V * tmp;
109}
110
111} // end namespace internal
112
113} // end namespace Eigen
114
115#endif // EIGEN_BLOCK_HOUSEHOLDER_H
@ ColMajor
Definition Constants.h:318
@ RowMajor
Definition Constants.h:320
Namespace containing all symbols from the Eigen library.
Definition Core:137