BlockHouseholder.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. #ifndef EIGEN_BLOCK_HOUSEHOLDER_H
  11. #define EIGEN_BLOCK_HOUSEHOLDER_H
  12. // This file contains some helper function to deal with block householder reflectors
  13. namespace Eigen {
  14. namespace internal {
  15. /** \internal */
  16. // template<typename TriangularFactorType,typename VectorsType,typename CoeffsType>
  17. // void make_block_householder_triangular_factor(TriangularFactorType& triFactor, const VectorsType& vectors, const CoeffsType& hCoeffs)
  18. // {
  19. // typedef typename VectorsType::Scalar Scalar;
  20. // const Index nbVecs = vectors.cols();
  21. // eigen_assert(triFactor.rows() == nbVecs && triFactor.cols() == nbVecs && vectors.rows()>=nbVecs);
  22. //
  23. // for(Index i = 0; i < nbVecs; i++)
  24. // {
  25. // Index rs = vectors.rows() - i;
  26. // // Warning, note that hCoeffs may alias with vectors.
  27. // // It is then necessary to copy it before modifying vectors(i,i).
  28. // typename CoeffsType::Scalar h = hCoeffs(i);
  29. // // This hack permits to pass trough nested Block<> and Transpose<> expressions.
  30. // Scalar *Vii_ptr = const_cast<Scalar*>(vectors.data() + vectors.outerStride()*i + vectors.innerStride()*i);
  31. // Scalar Vii = *Vii_ptr;
  32. // *Vii_ptr = Scalar(1);
  33. // triFactor.col(i).head(i).noalias() = -h * vectors.block(i, 0, rs, i).adjoint()
  34. // * vectors.col(i).tail(rs);
  35. // *Vii_ptr = Vii;
  36. // // FIXME add .noalias() once the triangular product can work inplace
  37. // triFactor.col(i).head(i) = triFactor.block(0,0,i,i).template triangularView<Upper>()
  38. // * triFactor.col(i).head(i);
  39. // triFactor(i,i) = hCoeffs(i);
  40. // }
  41. // }
  42. /** \internal */
  43. // This variant avoid modifications in vectors
  44. template<typename TriangularFactorType,typename VectorsType,typename CoeffsType>
  45. void make_block_householder_triangular_factor(TriangularFactorType& triFactor, const VectorsType& vectors, const CoeffsType& hCoeffs)
  46. {
  47. const Index nbVecs = vectors.cols();
  48. eigen_assert(triFactor.rows() == nbVecs && triFactor.cols() == nbVecs && vectors.rows()>=nbVecs);
  49. for(Index i = nbVecs-1; i >=0 ; --i)
  50. {
  51. Index rs = vectors.rows() - i - 1;
  52. Index rt = nbVecs-i-1;
  53. if(rt>0)
  54. {
  55. triFactor.row(i).tail(rt).noalias() = -hCoeffs(i) * vectors.col(i).tail(rs).adjoint()
  56. * vectors.bottomRightCorner(rs, rt).template triangularView<UnitLower>();
  57. // FIXME add .noalias() once the triangular product can work inplace
  58. triFactor.row(i).tail(rt) = triFactor.row(i).tail(rt) * triFactor.bottomRightCorner(rt,rt).template triangularView<Upper>();
  59. }
  60. triFactor(i,i) = hCoeffs(i);
  61. }
  62. }
  63. /** \internal
  64. * if forward then perform mat = H0 * H1 * H2 * mat
  65. * otherwise perform mat = H2 * H1 * H0 * mat
  66. */
  67. template<typename MatrixType,typename VectorsType,typename CoeffsType>
  68. void apply_block_householder_on_the_left(MatrixType& mat, const VectorsType& vectors, const CoeffsType& hCoeffs, bool forward)
  69. {
  70. enum { TFactorSize = MatrixType::ColsAtCompileTime };
  71. Index nbVecs = vectors.cols();
  72. Matrix<typename MatrixType::Scalar, TFactorSize, TFactorSize, RowMajor> T(nbVecs,nbVecs);
  73. if(forward) make_block_householder_triangular_factor(T, vectors, hCoeffs);
  74. else make_block_householder_triangular_factor(T, vectors, hCoeffs.conjugate());
  75. const TriangularView<const VectorsType, UnitLower> V(vectors);
  76. // A -= V T V^* A
  77. Matrix<typename MatrixType::Scalar,VectorsType::ColsAtCompileTime,MatrixType::ColsAtCompileTime,
  78. (VectorsType::MaxColsAtCompileTime==1 && MatrixType::MaxColsAtCompileTime!=1)?RowMajor:ColMajor,
  79. VectorsType::MaxColsAtCompileTime,MatrixType::MaxColsAtCompileTime> tmp = V.adjoint() * mat;
  80. // FIXME add .noalias() once the triangular product can work inplace
  81. if(forward) tmp = T.template triangularView<Upper>() * tmp;
  82. else tmp = T.template triangularView<Upper>().adjoint() * tmp;
  83. mat.noalias() -= V * tmp;
  84. }
  85. } // end namespace internal
  86. } // end namespace Eigen
  87. #endif // EIGEN_BLOCK_HOUSEHOLDER_H