Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/eigen3/Eigen/src/Householder/BlockHouseholder.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // This file is part of Eigen, a lightweight C++ template library
0002 // for linear algebra.
0003 //
0004 // Copyright (C) 2010 Vincent Lejeune
0005 // Copyright (C) 2010 Gael Guennebaud <gael.guennebaud@inria.fr>
0006 //
0007 // This Source Code Form is subject to the terms of the Mozilla
0008 // Public License v. 2.0. If a copy of the MPL was not distributed
0009 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
0010 
0011 #ifndef EIGEN_BLOCK_HOUSEHOLDER_H
0012 #define EIGEN_BLOCK_HOUSEHOLDER_H
0013 
0014 // This file contains some helper function to deal with block householder reflectors
0015 
0016 namespace Eigen { 
0017 
0018 namespace internal {
0019   
0020 /** \internal */
0021 // template<typename TriangularFactorType,typename VectorsType,typename CoeffsType>
0022 // void make_block_householder_triangular_factor(TriangularFactorType& triFactor, const VectorsType& vectors, const CoeffsType& hCoeffs)
0023 // {
0024 //   typedef typename VectorsType::Scalar Scalar;
0025 //   const Index nbVecs = vectors.cols();
0026 //   eigen_assert(triFactor.rows() == nbVecs && triFactor.cols() == nbVecs && vectors.rows()>=nbVecs);
0027 // 
0028 //   for(Index i = 0; i < nbVecs; i++)
0029 //   {
0030 //     Index rs = vectors.rows() - i;
0031 //     // Warning, note that hCoeffs may alias with vectors.
0032 //     // It is then necessary to copy it before modifying vectors(i,i). 
0033 //     typename CoeffsType::Scalar h = hCoeffs(i);
0034 //     // This hack permits to pass trough nested Block<> and Transpose<> expressions.
0035 //     Scalar *Vii_ptr = const_cast<Scalar*>(vectors.data() + vectors.outerStride()*i + vectors.innerStride()*i);
0036 //     Scalar Vii = *Vii_ptr;
0037 //     *Vii_ptr = Scalar(1);
0038 //     triFactor.col(i).head(i).noalias() = -h * vectors.block(i, 0, rs, i).adjoint()
0039 //                                        * vectors.col(i).tail(rs);
0040 //     *Vii_ptr = Vii;
0041 //     // FIXME add .noalias() once the triangular product can work inplace
0042 //     triFactor.col(i).head(i) = triFactor.block(0,0,i,i).template triangularView<Upper>()
0043 //                              * triFactor.col(i).head(i);
0044 //     triFactor(i,i) = hCoeffs(i);
0045 //   }
0046 // }
0047 
0048 /** \internal */
0049 // This variant avoid modifications in vectors
0050 template<typename TriangularFactorType,typename VectorsType,typename CoeffsType>
0051 void make_block_householder_triangular_factor(TriangularFactorType& triFactor, const VectorsType& vectors, const CoeffsType& hCoeffs)
0052 {
0053   const Index nbVecs = vectors.cols();
0054   eigen_assert(triFactor.rows() == nbVecs && triFactor.cols() == nbVecs && vectors.rows()>=nbVecs);
0055 
0056   for(Index i = nbVecs-1; i >=0 ; --i)
0057   {
0058     Index rs = vectors.rows() - i - 1;
0059     Index rt = nbVecs-i-1;
0060 
0061     if(rt>0)
0062     {
0063       triFactor.row(i).tail(rt).noalias() = -hCoeffs(i) * vectors.col(i).tail(rs).adjoint()
0064                                                         * vectors.bottomRightCorner(rs, rt).template triangularView<UnitLower>();
0065             
0066       // FIXME use the following line with .noalias() once the triangular product can work inplace
0067       // triFactor.row(i).tail(rt) = triFactor.row(i).tail(rt) * triFactor.bottomRightCorner(rt,rt).template triangularView<Upper>();
0068       for(Index j=nbVecs-1; j>i; --j)
0069       {
0070         typename TriangularFactorType::Scalar z = triFactor(i,j);
0071         triFactor(i,j) = z * triFactor(j,j);
0072         if(nbVecs-j-1>0)
0073           triFactor.row(i).tail(nbVecs-j-1) += z * triFactor.row(j).tail(nbVecs-j-1);
0074       }
0075       
0076     }
0077     triFactor(i,i) = hCoeffs(i);
0078   }
0079 }
0080 
0081 /** \internal
0082   * if forward then perform   mat = H0 * H1 * H2 * mat
0083   * otherwise perform         mat = H2 * H1 * H0 * mat
0084   */
0085 template<typename MatrixType,typename VectorsType,typename CoeffsType>
0086 void apply_block_householder_on_the_left(MatrixType& mat, const VectorsType& vectors, const CoeffsType& hCoeffs, bool forward)
0087 {
0088   enum { TFactorSize = MatrixType::ColsAtCompileTime };
0089   Index nbVecs = vectors.cols();
0090   Matrix<typename MatrixType::Scalar, TFactorSize, TFactorSize, RowMajor> T(nbVecs,nbVecs);
0091   
0092   if(forward) make_block_householder_triangular_factor(T, vectors, hCoeffs);
0093   else        make_block_householder_triangular_factor(T, vectors, hCoeffs.conjugate());  
0094   const TriangularView<const VectorsType, UnitLower> V(vectors);
0095 
0096   // A -= V T V^* A
0097   Matrix<typename MatrixType::Scalar,VectorsType::ColsAtCompileTime,MatrixType::ColsAtCompileTime,
0098          (VectorsType::MaxColsAtCompileTime==1 && MatrixType::MaxColsAtCompileTime!=1)?RowMajor:ColMajor,
0099          VectorsType::MaxColsAtCompileTime,MatrixType::MaxColsAtCompileTime> tmp = V.adjoint() * mat;
0100   // FIXME add .noalias() once the triangular product can work inplace
0101   if(forward) tmp = T.template triangularView<Upper>()           * tmp;
0102   else        tmp = T.template triangularView<Upper>().adjoint() * tmp;
0103   mat.noalias() -= V * tmp;
0104 }
0105 
0106 } // end namespace internal
0107 
0108 } // end namespace Eigen
0109 
0110 #endif // EIGEN_BLOCK_HOUSEHOLDER_H