SolverBase.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2015 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_SOLVERBASE_H
  10. #define EIGEN_SOLVERBASE_H
  11. namespace Eigen {
  12. namespace internal {
  13. } // end namespace internal
  14. /** \class SolverBase
  15. * \brief A base class for matrix decomposition and solvers
  16. *
  17. * \tparam Derived the actual type of the decomposition/solver.
  18. *
  19. * Any matrix decomposition inheriting this base class provide the following API:
  20. *
  21. * \code
  22. * MatrixType A, b, x;
  23. * DecompositionType dec(A);
  24. * x = dec.solve(b); // solve A * x = b
  25. * x = dec.transpose().solve(b); // solve A^T * x = b
  26. * x = dec.adjoint().solve(b); // solve A' * x = b
  27. * \endcode
  28. *
  29. * \warning Currently, any other usage of transpose() and adjoint() are not supported and will produce compilation errors.
  30. *
  31. * \sa class PartialPivLU, class FullPivLU
  32. */
  33. template<typename Derived>
  34. class SolverBase : public EigenBase<Derived>
  35. {
  36. public:
  37. typedef EigenBase<Derived> Base;
  38. typedef typename internal::traits<Derived>::Scalar Scalar;
  39. typedef Scalar CoeffReturnType;
  40. enum {
  41. RowsAtCompileTime = internal::traits<Derived>::RowsAtCompileTime,
  42. ColsAtCompileTime = internal::traits<Derived>::ColsAtCompileTime,
  43. SizeAtCompileTime = (internal::size_at_compile_time<internal::traits<Derived>::RowsAtCompileTime,
  44. internal::traits<Derived>::ColsAtCompileTime>::ret),
  45. MaxRowsAtCompileTime = internal::traits<Derived>::MaxRowsAtCompileTime,
  46. MaxColsAtCompileTime = internal::traits<Derived>::MaxColsAtCompileTime,
  47. MaxSizeAtCompileTime = (internal::size_at_compile_time<internal::traits<Derived>::MaxRowsAtCompileTime,
  48. internal::traits<Derived>::MaxColsAtCompileTime>::ret),
  49. IsVectorAtCompileTime = internal::traits<Derived>::MaxRowsAtCompileTime == 1
  50. || internal::traits<Derived>::MaxColsAtCompileTime == 1
  51. };
  52. /** Default constructor */
  53. SolverBase()
  54. {}
  55. ~SolverBase()
  56. {}
  57. using Base::derived;
  58. /** \returns an expression of the solution x of \f$ A x = b \f$ using the current decomposition of A.
  59. */
  60. template<typename Rhs>
  61. inline const Solve<Derived, Rhs>
  62. solve(const MatrixBase<Rhs>& b) const
  63. {
  64. eigen_assert(derived().rows()==b.rows() && "solve(): invalid number of rows of the right hand side matrix b");
  65. return Solve<Derived, Rhs>(derived(), b.derived());
  66. }
  67. /** \internal the return type of transpose() */
  68. typedef typename internal::add_const<Transpose<const Derived> >::type ConstTransposeReturnType;
  69. /** \returns an expression of the transposed of the factored matrix.
  70. *
  71. * A typical usage is to solve for the transposed problem A^T x = b:
  72. * \code x = dec.transpose().solve(b); \endcode
  73. *
  74. * \sa adjoint(), solve()
  75. */
  76. inline ConstTransposeReturnType transpose() const
  77. {
  78. return ConstTransposeReturnType(derived());
  79. }
  80. /** \internal the return type of adjoint() */
  81. typedef typename internal::conditional<NumTraits<Scalar>::IsComplex,
  82. CwiseUnaryOp<internal::scalar_conjugate_op<Scalar>, ConstTransposeReturnType>,
  83. ConstTransposeReturnType
  84. >::type AdjointReturnType;
  85. /** \returns an expression of the adjoint of the factored matrix
  86. *
  87. * A typical usage is to solve for the adjoint problem A' x = b:
  88. * \code x = dec.adjoint().solve(b); \endcode
  89. *
  90. * For real scalar types, this function is equivalent to transpose().
  91. *
  92. * \sa transpose(), solve()
  93. */
  94. inline AdjointReturnType adjoint() const
  95. {
  96. return AdjointReturnType(derived().transpose());
  97. }
  98. protected:
  99. };
  100. namespace internal {
  101. template<typename Derived>
  102. struct generic_xpr_base<Derived, MatrixXpr, SolverStorage>
  103. {
  104. typedef SolverBase<Derived> type;
  105. };
  106. } // end namespace internal
  107. } // end namespace Eigen
  108. #endif // EIGEN_SOLVERBASE_H