1*bf2c3715SXin Li // This file is part of Eigen, a lightweight C++ template library
2*bf2c3715SXin Li // for linear algebra.
3*bf2c3715SXin Li //
4*bf2c3715SXin Li // Copyright (C) 2010 Vincent Lejeune
5*bf2c3715SXin Li // Copyright (C) 2010 Gael Guennebaud <[email protected]>
6*bf2c3715SXin Li //
7*bf2c3715SXin Li // This Source Code Form is subject to the terms of the Mozilla
8*bf2c3715SXin Li // Public License v. 2.0. If a copy of the MPL was not distributed
9*bf2c3715SXin Li // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10*bf2c3715SXin Li
11*bf2c3715SXin Li #ifndef EIGEN_BLOCK_HOUSEHOLDER_H
12*bf2c3715SXin Li #define EIGEN_BLOCK_HOUSEHOLDER_H
13*bf2c3715SXin Li
14*bf2c3715SXin Li // This file contains some helper function to deal with block householder reflectors
15*bf2c3715SXin Li
16*bf2c3715SXin Li namespace Eigen {
17*bf2c3715SXin Li
18*bf2c3715SXin Li namespace internal {
19*bf2c3715SXin Li
20*bf2c3715SXin Li /** \internal */
21*bf2c3715SXin Li // template<typename TriangularFactorType,typename VectorsType,typename CoeffsType>
22*bf2c3715SXin Li // void make_block_householder_triangular_factor(TriangularFactorType& triFactor, const VectorsType& vectors, const CoeffsType& hCoeffs)
23*bf2c3715SXin Li // {
24*bf2c3715SXin Li // typedef typename VectorsType::Scalar Scalar;
25*bf2c3715SXin Li // const Index nbVecs = vectors.cols();
26*bf2c3715SXin Li // eigen_assert(triFactor.rows() == nbVecs && triFactor.cols() == nbVecs && vectors.rows()>=nbVecs);
27*bf2c3715SXin Li //
28*bf2c3715SXin Li // for(Index i = 0; i < nbVecs; i++)
29*bf2c3715SXin Li // {
30*bf2c3715SXin Li // Index rs = vectors.rows() - i;
31*bf2c3715SXin Li // // Warning, note that hCoeffs may alias with vectors.
32*bf2c3715SXin Li // // It is then necessary to copy it before modifying vectors(i,i).
33*bf2c3715SXin Li // typename CoeffsType::Scalar h = hCoeffs(i);
34*bf2c3715SXin Li // // This hack permits to pass trough nested Block<> and Transpose<> expressions.
35*bf2c3715SXin Li // Scalar *Vii_ptr = const_cast<Scalar*>(vectors.data() + vectors.outerStride()*i + vectors.innerStride()*i);
36*bf2c3715SXin Li // Scalar Vii = *Vii_ptr;
37*bf2c3715SXin Li // *Vii_ptr = Scalar(1);
38*bf2c3715SXin Li // triFactor.col(i).head(i).noalias() = -h * vectors.block(i, 0, rs, i).adjoint()
39*bf2c3715SXin Li // * vectors.col(i).tail(rs);
40*bf2c3715SXin Li // *Vii_ptr = Vii;
41*bf2c3715SXin Li // // FIXME add .noalias() once the triangular product can work inplace
42*bf2c3715SXin Li // triFactor.col(i).head(i) = triFactor.block(0,0,i,i).template triangularView<Upper>()
43*bf2c3715SXin Li // * triFactor.col(i).head(i);
44*bf2c3715SXin Li // triFactor(i,i) = hCoeffs(i);
45*bf2c3715SXin Li // }
46*bf2c3715SXin Li // }
47*bf2c3715SXin Li
48*bf2c3715SXin Li /** \internal */
49*bf2c3715SXin Li // This variant avoid modifications in vectors
50*bf2c3715SXin Li template<typename TriangularFactorType,typename VectorsType,typename CoeffsType>
make_block_householder_triangular_factor(TriangularFactorType & triFactor,const VectorsType & vectors,const CoeffsType & hCoeffs)51*bf2c3715SXin Li void make_block_householder_triangular_factor(TriangularFactorType& triFactor, const VectorsType& vectors, const CoeffsType& hCoeffs)
52*bf2c3715SXin Li {
53*bf2c3715SXin Li const Index nbVecs = vectors.cols();
54*bf2c3715SXin Li eigen_assert(triFactor.rows() == nbVecs && triFactor.cols() == nbVecs && vectors.rows()>=nbVecs);
55*bf2c3715SXin Li
56*bf2c3715SXin Li for(Index i = nbVecs-1; i >=0 ; --i)
57*bf2c3715SXin Li {
58*bf2c3715SXin Li Index rs = vectors.rows() - i - 1;
59*bf2c3715SXin Li Index rt = nbVecs-i-1;
60*bf2c3715SXin Li
61*bf2c3715SXin Li if(rt>0)
62*bf2c3715SXin Li {
63*bf2c3715SXin Li triFactor.row(i).tail(rt).noalias() = -hCoeffs(i) * vectors.col(i).tail(rs).adjoint()
64*bf2c3715SXin Li * vectors.bottomRightCorner(rs, rt).template triangularView<UnitLower>();
65*bf2c3715SXin Li
66*bf2c3715SXin Li // FIXME use the following line with .noalias() once the triangular product can work inplace
67*bf2c3715SXin Li // triFactor.row(i).tail(rt) = triFactor.row(i).tail(rt) * triFactor.bottomRightCorner(rt,rt).template triangularView<Upper>();
68*bf2c3715SXin Li for(Index j=nbVecs-1; j>i; --j)
69*bf2c3715SXin Li {
70*bf2c3715SXin Li typename TriangularFactorType::Scalar z = triFactor(i,j);
71*bf2c3715SXin Li triFactor(i,j) = z * triFactor(j,j);
72*bf2c3715SXin Li if(nbVecs-j-1>0)
73*bf2c3715SXin Li triFactor.row(i).tail(nbVecs-j-1) += z * triFactor.row(j).tail(nbVecs-j-1);
74*bf2c3715SXin Li }
75*bf2c3715SXin Li
76*bf2c3715SXin Li }
77*bf2c3715SXin Li triFactor(i,i) = hCoeffs(i);
78*bf2c3715SXin Li }
79*bf2c3715SXin Li }
80*bf2c3715SXin Li
81*bf2c3715SXin Li /** \internal
82*bf2c3715SXin Li * if forward then perform mat = H0 * H1 * H2 * mat
83*bf2c3715SXin Li * otherwise perform mat = H2 * H1 * H0 * mat
84*bf2c3715SXin Li */
85*bf2c3715SXin Li template<typename MatrixType,typename VectorsType,typename CoeffsType>
apply_block_householder_on_the_left(MatrixType & mat,const VectorsType & vectors,const CoeffsType & hCoeffs,bool forward)86*bf2c3715SXin Li void apply_block_householder_on_the_left(MatrixType& mat, const VectorsType& vectors, const CoeffsType& hCoeffs, bool forward)
87*bf2c3715SXin Li {
88*bf2c3715SXin Li enum { TFactorSize = MatrixType::ColsAtCompileTime };
89*bf2c3715SXin Li Index nbVecs = vectors.cols();
90*bf2c3715SXin Li Matrix<typename MatrixType::Scalar, TFactorSize, TFactorSize, RowMajor> T(nbVecs,nbVecs);
91*bf2c3715SXin Li
92*bf2c3715SXin Li if(forward) make_block_householder_triangular_factor(T, vectors, hCoeffs);
93*bf2c3715SXin Li else make_block_householder_triangular_factor(T, vectors, hCoeffs.conjugate());
94*bf2c3715SXin Li const TriangularView<const VectorsType, UnitLower> V(vectors);
95*bf2c3715SXin Li
96*bf2c3715SXin Li // A -= V T V^* A
97*bf2c3715SXin Li Matrix<typename MatrixType::Scalar,VectorsType::ColsAtCompileTime,MatrixType::ColsAtCompileTime,
98*bf2c3715SXin Li (VectorsType::MaxColsAtCompileTime==1 && MatrixType::MaxColsAtCompileTime!=1)?RowMajor:ColMajor,
99*bf2c3715SXin Li VectorsType::MaxColsAtCompileTime,MatrixType::MaxColsAtCompileTime> tmp = V.adjoint() * mat;
100*bf2c3715SXin Li // FIXME add .noalias() once the triangular product can work inplace
101*bf2c3715SXin Li if(forward) tmp = T.template triangularView<Upper>() * tmp;
102*bf2c3715SXin Li else tmp = T.template triangularView<Upper>().adjoint() * tmp;
103*bf2c3715SXin Li mat.noalias() -= V * tmp;
104*bf2c3715SXin Li }
105*bf2c3715SXin Li
106*bf2c3715SXin Li } // end namespace internal
107*bf2c3715SXin Li
108*bf2c3715SXin Li } // end namespace Eigen
109*bf2c3715SXin Li
110*bf2c3715SXin Li #endif // EIGEN_BLOCK_HOUSEHOLDER_H
111