SolveTriangular.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
  5. //
  6. // This Source Code Form is subject to the terms of the Mozilla
  7. // Public License v. 2.0. If a copy of the MPL was not distributed
  8. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. #ifndef EIGEN_SOLVETRIANGULAR_H
  10. #define EIGEN_SOLVETRIANGULAR_H
  11. namespace Eigen {
  12. namespace internal {
  13. // Forward declarations:
  14. // The following two routines are implemented in the products/TriangularSolver*.h files
  15. template<typename LhsScalar, typename RhsScalar, typename Index, int Side, int Mode, bool Conjugate, int StorageOrder>
  16. struct triangular_solve_vector;
  17. template <typename Scalar, typename Index, int Side, int Mode, bool Conjugate, int TriStorageOrder, int OtherStorageOrder, int OtherInnerStride>
  18. struct triangular_solve_matrix;
  19. // small helper struct extracting some traits on the underlying solver operation
  20. template<typename Lhs, typename Rhs, int Side>
  21. class trsolve_traits
  22. {
  23. private:
  24. enum {
  25. RhsIsVectorAtCompileTime = (Side==OnTheLeft ? Rhs::ColsAtCompileTime : Rhs::RowsAtCompileTime)==1
  26. };
  27. public:
  28. enum {
  29. Unrolling = (RhsIsVectorAtCompileTime && Rhs::SizeAtCompileTime != Dynamic && Rhs::SizeAtCompileTime <= 8)
  30. ? CompleteUnrolling : NoUnrolling,
  31. RhsVectors = RhsIsVectorAtCompileTime ? 1 : Dynamic
  32. };
  33. };
  34. template<typename Lhs, typename Rhs,
  35. int Side, // can be OnTheLeft/OnTheRight
  36. int Mode, // can be Upper/Lower | UnitDiag
  37. int Unrolling = trsolve_traits<Lhs,Rhs,Side>::Unrolling,
  38. int RhsVectors = trsolve_traits<Lhs,Rhs,Side>::RhsVectors
  39. >
  40. struct triangular_solver_selector;
  41. template<typename Lhs, typename Rhs, int Side, int Mode>
  42. struct triangular_solver_selector<Lhs,Rhs,Side,Mode,NoUnrolling,1>
  43. {
  44. typedef typename Lhs::Scalar LhsScalar;
  45. typedef typename Rhs::Scalar RhsScalar;
  46. typedef blas_traits<Lhs> LhsProductTraits;
  47. typedef typename LhsProductTraits::ExtractType ActualLhsType;
  48. typedef Map<Matrix<RhsScalar,Dynamic,1>, Aligned> MappedRhs;
  49. static void run(const Lhs& lhs, Rhs& rhs)
  50. {
  51. ActualLhsType actualLhs = LhsProductTraits::extract(lhs);
  52. // FIXME find a way to allow an inner stride if packet_traits<Scalar>::size==1
  53. bool useRhsDirectly = Rhs::InnerStrideAtCompileTime==1 || rhs.innerStride()==1;
  54. ei_declare_aligned_stack_constructed_variable(RhsScalar,actualRhs,rhs.size(),
  55. (useRhsDirectly ? rhs.data() : 0));
  56. if(!useRhsDirectly)
  57. MappedRhs(actualRhs,rhs.size()) = rhs;
  58. triangular_solve_vector<LhsScalar, RhsScalar, Index, Side, Mode, LhsProductTraits::NeedToConjugate,
  59. (int(Lhs::Flags) & RowMajorBit) ? RowMajor : ColMajor>
  60. ::run(actualLhs.cols(), actualLhs.data(), actualLhs.outerStride(), actualRhs);
  61. if(!useRhsDirectly)
  62. rhs = MappedRhs(actualRhs, rhs.size());
  63. }
  64. };
  65. // the rhs is a matrix
  66. template<typename Lhs, typename Rhs, int Side, int Mode>
  67. struct triangular_solver_selector<Lhs,Rhs,Side,Mode,NoUnrolling,Dynamic>
  68. {
  69. typedef typename Rhs::Scalar Scalar;
  70. typedef blas_traits<Lhs> LhsProductTraits;
  71. typedef typename LhsProductTraits::DirectLinearAccessType ActualLhsType;
  72. static void run(const Lhs& lhs, Rhs& rhs)
  73. {
  74. typename internal::add_const_on_value_type<ActualLhsType>::type actualLhs = LhsProductTraits::extract(lhs);
  75. const Index size = lhs.rows();
  76. const Index othersize = Side==OnTheLeft? rhs.cols() : rhs.rows();
  77. typedef internal::gemm_blocking_space<(Rhs::Flags&RowMajorBit) ? RowMajor : ColMajor,Scalar,Scalar,
  78. Rhs::MaxRowsAtCompileTime, Rhs::MaxColsAtCompileTime, Lhs::MaxRowsAtCompileTime,4> BlockingType;
  79. BlockingType blocking(rhs.rows(), rhs.cols(), size, 1, false);
  80. triangular_solve_matrix<Scalar,Index,Side,Mode,LhsProductTraits::NeedToConjugate,(int(Lhs::Flags) & RowMajorBit) ? RowMajor : ColMajor,
  81. (Rhs::Flags&RowMajorBit) ? RowMajor : ColMajor, Rhs::InnerStrideAtCompileTime>
  82. ::run(size, othersize, &actualLhs.coeffRef(0,0), actualLhs.outerStride(), &rhs.coeffRef(0,0), rhs.innerStride(), rhs.outerStride(), blocking);
  83. }
  84. };
  85. /***************************************************************************
  86. * meta-unrolling implementation
  87. ***************************************************************************/
  88. template<typename Lhs, typename Rhs, int Mode, int LoopIndex, int Size,
  89. bool Stop = LoopIndex==Size>
  90. struct triangular_solver_unroller;
  91. template<typename Lhs, typename Rhs, int Mode, int LoopIndex, int Size>
  92. struct triangular_solver_unroller<Lhs,Rhs,Mode,LoopIndex,Size,false> {
  93. enum {
  94. IsLower = ((Mode&Lower)==Lower),
  95. DiagIndex = IsLower ? LoopIndex : Size - LoopIndex - 1,
  96. StartIndex = IsLower ? 0 : DiagIndex+1
  97. };
  98. static void run(const Lhs& lhs, Rhs& rhs)
  99. {
  100. if (LoopIndex>0)
  101. rhs.coeffRef(DiagIndex) -= lhs.row(DiagIndex).template segment<LoopIndex>(StartIndex).transpose()
  102. .cwiseProduct(rhs.template segment<LoopIndex>(StartIndex)).sum();
  103. if(!(Mode & UnitDiag))
  104. rhs.coeffRef(DiagIndex) /= lhs.coeff(DiagIndex,DiagIndex);
  105. triangular_solver_unroller<Lhs,Rhs,Mode,LoopIndex+1,Size>::run(lhs,rhs);
  106. }
  107. };
  108. template<typename Lhs, typename Rhs, int Mode, int LoopIndex, int Size>
  109. struct triangular_solver_unroller<Lhs,Rhs,Mode,LoopIndex,Size,true> {
  110. static void run(const Lhs&, Rhs&) {}
  111. };
  112. template<typename Lhs, typename Rhs, int Mode>
  113. struct triangular_solver_selector<Lhs,Rhs,OnTheLeft,Mode,CompleteUnrolling,1> {
  114. static void run(const Lhs& lhs, Rhs& rhs)
  115. { triangular_solver_unroller<Lhs,Rhs,Mode,0,Rhs::SizeAtCompileTime>::run(lhs,rhs); }
  116. };
  117. template<typename Lhs, typename Rhs, int Mode>
  118. struct triangular_solver_selector<Lhs,Rhs,OnTheRight,Mode,CompleteUnrolling,1> {
  119. static void run(const Lhs& lhs, Rhs& rhs)
  120. {
  121. Transpose<const Lhs> trLhs(lhs);
  122. Transpose<Rhs> trRhs(rhs);
  123. triangular_solver_unroller<Transpose<const Lhs>,Transpose<Rhs>,
  124. ((Mode&Upper)==Upper ? Lower : Upper) | (Mode&UnitDiag),
  125. 0,Rhs::SizeAtCompileTime>::run(trLhs,trRhs);
  126. }
  127. };
  128. } // end namespace internal
  129. /***************************************************************************
  130. * TriangularView methods
  131. ***************************************************************************/
  132. #ifndef EIGEN_PARSED_BY_DOXYGEN
  133. template<typename MatrixType, unsigned int Mode>
  134. template<int Side, typename OtherDerived>
  135. void TriangularViewImpl<MatrixType,Mode,Dense>::solveInPlace(const MatrixBase<OtherDerived>& _other) const
  136. {
  137. OtherDerived& other = _other.const_cast_derived();
  138. eigen_assert( derived().cols() == derived().rows() && ((Side==OnTheLeft && derived().cols() == other.rows()) || (Side==OnTheRight && derived().cols() == other.cols())) );
  139. eigen_assert((!(Mode & ZeroDiag)) && bool(Mode & (Upper|Lower)));
  140. // If solving for a 0x0 matrix, nothing to do, simply return.
  141. if (derived().cols() == 0)
  142. return;
  143. enum { copy = (internal::traits<OtherDerived>::Flags & RowMajorBit) && OtherDerived::IsVectorAtCompileTime && OtherDerived::SizeAtCompileTime!=1};
  144. typedef typename internal::conditional<copy,
  145. typename internal::plain_matrix_type_column_major<OtherDerived>::type, OtherDerived&>::type OtherCopy;
  146. OtherCopy otherCopy(other);
  147. internal::triangular_solver_selector<MatrixType, typename internal::remove_reference<OtherCopy>::type,
  148. Side, Mode>::run(derived().nestedExpression(), otherCopy);
  149. if (copy)
  150. other = otherCopy;
  151. }
  152. template<typename Derived, unsigned int Mode>
  153. template<int Side, typename Other>
  154. const internal::triangular_solve_retval<Side,TriangularView<Derived,Mode>,Other>
  155. TriangularViewImpl<Derived,Mode,Dense>::solve(const MatrixBase<Other>& other) const
  156. {
  157. return internal::triangular_solve_retval<Side,TriangularViewType,Other>(derived(), other.derived());
  158. }
  159. #endif
  160. namespace internal {
  161. template<int Side, typename TriangularType, typename Rhs>
  162. struct traits<triangular_solve_retval<Side, TriangularType, Rhs> >
  163. {
  164. typedef typename internal::plain_matrix_type_column_major<Rhs>::type ReturnType;
  165. };
  166. template<int Side, typename TriangularType, typename Rhs> struct triangular_solve_retval
  167. : public ReturnByValue<triangular_solve_retval<Side, TriangularType, Rhs> >
  168. {
  169. typedef typename remove_all<typename Rhs::Nested>::type RhsNestedCleaned;
  170. typedef ReturnByValue<triangular_solve_retval> Base;
  171. triangular_solve_retval(const TriangularType& tri, const Rhs& rhs)
  172. : m_triangularMatrix(tri), m_rhs(rhs)
  173. {}
  174. inline Index rows() const { return m_rhs.rows(); }
  175. inline Index cols() const { return m_rhs.cols(); }
  176. template<typename Dest> inline void evalTo(Dest& dst) const
  177. {
  178. if(!is_same_dense(dst,m_rhs))
  179. dst = m_rhs;
  180. m_triangularMatrix.template solveInPlace<Side>(dst);
  181. }
  182. protected:
  183. const TriangularType& m_triangularMatrix;
  184. typename Rhs::Nested m_rhs;
  185. };
  186. } // namespace internal
  187. } // end namespace Eigen
  188. #endif // EIGEN_SOLVETRIANGULAR_H