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 #include "./InternalHeaderCheck.h"
17 
18 namespace Eigen {
19 
20 namespace internal {
21 
23 // template<typename TriangularFactorType,typename VectorsType,typename CoeffsType>
24 // void make_block_householder_triangular_factor(TriangularFactorType& triFactor, const VectorsType& vectors, const CoeffsType& hCoeffs)
25 // {
26 // typedef typename VectorsType::Scalar Scalar;
27 // const Index nbVecs = vectors.cols();
28 // eigen_assert(triFactor.rows() == nbVecs && triFactor.cols() == nbVecs && vectors.rows()>=nbVecs);
29 //
30 // for(Index i = 0; i < nbVecs; i++)
31 // {
32 // Index rs = vectors.rows() - i;
33 // // Warning, note that hCoeffs may alias with vectors.
34 // // It is then necessary to copy it before modifying vectors(i,i).
35 // typename CoeffsType::Scalar h = hCoeffs(i);
36 // // This hack permits to pass trough nested Block<> and Transpose<> expressions.
37 // Scalar *Vii_ptr = const_cast<Scalar*>(vectors.data() + vectors.outerStride()*i + vectors.innerStride()*i);
38 // Scalar Vii = *Vii_ptr;
39 // *Vii_ptr = Scalar(1);
40 // triFactor.col(i).head(i).noalias() = -h * vectors.block(i, 0, rs, i).adjoint()
41 // * vectors.col(i).tail(rs);
42 // *Vii_ptr = Vii;
43 // // FIXME add .noalias() once the triangular product can work inplace
44 // triFactor.col(i).head(i) = triFactor.block(0,0,i,i).template triangularView<Upper>()
45 // * triFactor.col(i).head(i);
46 // triFactor(i,i) = hCoeffs(i);
47 // }
48 // }
49 
51 // This variant avoid modifications in vectors
52 template<typename TriangularFactorType,typename VectorsType,typename CoeffsType>
53 void make_block_householder_triangular_factor(TriangularFactorType& triFactor, const VectorsType& vectors, const CoeffsType& hCoeffs)
54 {
55  const Index nbVecs = vectors.cols();
56  eigen_assert(triFactor.rows() == nbVecs && triFactor.cols() == nbVecs && vectors.rows()>=nbVecs);
57 
58  for(Index i = nbVecs-1; i >=0 ; --i)
59  {
60  Index rs = vectors.rows() - i - 1;
61  Index rt = nbVecs-i-1;
62 
63  if(rt>0)
64  {
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 triangularView<Upper>();
70  for(Index j=nbVecs-1; j>i; --j)
71  {
72  typename TriangularFactorType::Scalar z = triFactor(i,j);
73  triFactor(i,j) = z * triFactor(j,j);
74  if(nbVecs-j-1>0)
75  triFactor.row(i).tail(nbVecs-j-1) += z * triFactor.row(j).tail(nbVecs-j-1);
76  }
77 
78  }
79  triFactor(i,i) = hCoeffs(i);
80  }
81 }
82 
87 template<typename MatrixType,typename VectorsType,typename CoeffsType>
88 void apply_block_householder_on_the_left(MatrixType& mat, const VectorsType& vectors, const CoeffsType& hCoeffs, bool forward)
89 {
90  enum { TFactorSize = VectorsType::ColsAtCompileTime };
91  Index nbVecs = vectors.cols();
93 
94  if(forward) make_block_householder_triangular_factor(T, vectors, hCoeffs);
95  else 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> tmp = V.adjoint() * mat;
102  // FIXME add .noalias() once the triangular product can work inplace
103  if(forward) tmp = T.template triangularView<Upper>() * tmp;
104  else tmp = T.template triangularView<Upper>().adjoint() * tmp;
105  mat.noalias() -= V * tmp;
106 }
107 
108 } // end namespace internal
109 
110 } // end namespace Eigen
111 
112 #endif // EIGEN_BLOCK_HOUSEHOLDER_H
#define eigen_assert(x)
Definition: Macros.h:902
Matrix< float, 1, Dynamic > MatrixType
Eigen::Triplet< double > T
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:182
Expression of a triangular part in a matrix.
@ ColMajor
Definition: Constants.h:321
@ RowMajor
Definition: Constants.h:323
void make_block_householder_triangular_factor(TriangularFactorType &triFactor, const VectorsType &vectors, const CoeffsType &hCoeffs)
void apply_block_householder_on_the_left(MatrixType &mat, const VectorsType &vectors, const CoeffsType &hCoeffs, bool forward)
: InteropHeaders
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
std::ptrdiff_t j