BlockHouseholder.h
Go to the documentation of this file.
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 
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 
19 namespace Eigen {
20 
21 namespace 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 through 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
54 template <typename TriangularFactorType, typename VectorsType, typename CoeffsType>
55 void 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 
85 template <typename MatrixType, typename VectorsType, typename CoeffsType>
86 void 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();
91 
92  if (forward)
94  else
95  make_block_householder_triangular_factor(T, vectors, hCoeffs.conjugate());
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
int i
Definition: BiCGSTAB_step_by_step.cpp:9
MatrixXcd V
Definition: EigenSolver_EigenSolver_MatrixType.cpp:15
Eigen::SparseMatrix< double > mat
Definition: EigenUnitTest.cpp:10
Eigen::Triplet< double > T
Definition: EigenUnitTest.cpp:11
#define eigen_assert(x)
Definition: Macros.h:910
SCALAR Scalar
Definition: bench_gemm.cpp:45
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
The matrix class, also used for vectors and row-vectors.
Definition: Eigen/Eigen/src/Core/Matrix.h:186
Expression of a triangular part in a matrix.
Definition: TriangularMatrix.h:167
@ ColMajor
Definition: Constants.h:318
@ RowMajor
Definition: Constants.h:320
Eigen::Matrix< Scalar, Dynamic, Dynamic, ColMajor > tmp
Definition: level3_impl.h:365
void make_block_householder_triangular_factor(TriangularFactorType &triFactor, const VectorsType &vectors, const CoeffsType &hCoeffs)
Definition: BlockHouseholder.h:55
void apply_block_householder_on_the_left(MatrixType &mat, const VectorsType &vectors, const CoeffsType &hCoeffs, bool forward)
Definition: BlockHouseholder.h:86
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
Definition: Eigen_Colamd.h:49
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2