Eigen  3.2.91
Solve.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2014 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 
10 #ifndef EIGEN_SOLVE_H
11 #define EIGEN_SOLVE_H
12 
13 namespace Eigen {
14 
15 template<typename Decomposition, typename RhsType, typename StorageKind> class SolveImpl;
16 
29 namespace internal {
30 
31 // this solve_traits class permits to determine the evaluation type with respect to storage kind (Dense vs Sparse)
32 template<typename Decomposition, typename RhsType,typename StorageKind> struct solve_traits;
33 
34 template<typename Decomposition, typename RhsType>
35 struct solve_traits<Decomposition,RhsType,Dense>
36 {
37  typedef typename Decomposition::MatrixType MatrixType;
38  typedef Matrix<typename RhsType::Scalar,
39  MatrixType::ColsAtCompileTime,
40  RhsType::ColsAtCompileTime,
41  RhsType::PlainObject::Options,
42  MatrixType::MaxColsAtCompileTime,
43  RhsType::MaxColsAtCompileTime> PlainObject;
44 };
45 
46 template<typename Decomposition, typename RhsType>
47 struct traits<Solve<Decomposition, RhsType> >
48  : traits<typename solve_traits<Decomposition,RhsType,typename internal::traits<RhsType>::StorageKind>::PlainObject>
49 {
50  typedef typename solve_traits<Decomposition,RhsType,typename internal::traits<RhsType>::StorageKind>::PlainObject PlainObject;
51  typedef typename promote_index_type<typename Decomposition::StorageIndex, typename RhsType::StorageIndex>::type StorageIndex;
52  typedef traits<PlainObject> BaseTraits;
53  enum {
54  Flags = BaseTraits::Flags & RowMajorBit,
55  CoeffReadCost = Dynamic
56  };
57 };
58 
59 }
60 
61 
62 template<typename Decomposition, typename RhsType>
63 class Solve : public SolveImpl<Decomposition,RhsType,typename internal::traits<RhsType>::StorageKind>
64 {
65 public:
66  typedef typename internal::traits<Solve>::PlainObject PlainObject;
67  typedef typename internal::traits<Solve>::StorageIndex StorageIndex;
68 
69  Solve(const Decomposition &dec, const RhsType &rhs)
70  : m_dec(dec), m_rhs(rhs)
71  {}
72 
73  EIGEN_DEVICE_FUNC Index rows() const { return m_dec.cols(); }
74  EIGEN_DEVICE_FUNC Index cols() const { return m_rhs.cols(); }
75 
76  EIGEN_DEVICE_FUNC const Decomposition& dec() const { return m_dec; }
77  EIGEN_DEVICE_FUNC const RhsType& rhs() const { return m_rhs; }
78 
79 protected:
80  const Decomposition &m_dec;
81  const RhsType &m_rhs;
82 };
83 
84 
85 // Specialization of the Solve expression for dense results
86 template<typename Decomposition, typename RhsType>
87 class SolveImpl<Decomposition,RhsType,Dense>
88  : public MatrixBase<Solve<Decomposition,RhsType> >
89 {
90  typedef Solve<Decomposition,RhsType> Derived;
91 
92 public:
93 
95  EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
96 
97 private:
98 
99  Scalar coeff(Index row, Index col) const;
100  Scalar coeff(Index i) const;
101 };
102 
103 // Generic API dispatcher
104 template<typename Decomposition, typename RhsType, typename StorageKind>
105 class SolveImpl : public internal::generic_xpr_base<Solve<Decomposition,RhsType>, MatrixXpr, StorageKind>::type
106 {
107  public:
108  typedef typename internal::generic_xpr_base<Solve<Decomposition,RhsType>, MatrixXpr, StorageKind>::type Base;
109 };
110 
111 namespace internal {
112 
113 // Evaluator of Solve -> eval into a temporary
114 template<typename Decomposition, typename RhsType>
115 struct evaluator<Solve<Decomposition,RhsType> >
116  : public evaluator<typename Solve<Decomposition,RhsType>::PlainObject>
117 {
118  typedef Solve<Decomposition,RhsType> SolveType;
119  typedef typename SolveType::PlainObject PlainObject;
120  typedef evaluator<PlainObject> Base;
121 
122  EIGEN_DEVICE_FUNC explicit evaluator(const SolveType& solve)
123  : m_result(solve.rows(), solve.cols())
124  {
125  ::new (static_cast<Base*>(this)) Base(m_result);
126  solve.dec()._solve_impl(solve.rhs(), m_result);
127  }
128 
129 protected:
130  PlainObject m_result;
131 };
132 
133 // Specialization for "dst = dec.solve(rhs)"
134 // NOTE we need to specialize it for Dense2Dense to avoid ambiguous specialization error and a Sparse2Sparse specialization must exist somewhere
135 template<typename DstXprType, typename DecType, typename RhsType, typename Scalar>
136 struct Assignment<DstXprType, Solve<DecType,RhsType>, internal::assign_op<Scalar>, Dense2Dense, Scalar>
137 {
138  typedef Solve<DecType,RhsType> SrcXprType;
139  static void run(DstXprType &dst, const SrcXprType &src, const internal::assign_op<Scalar> &)
140  {
141  // FIXME shall we resize dst here?
142  src.dec()._solve_impl(src.rhs(), dst);
143  }
144 };
145 
146 } // end namepsace internal
147 
148 } // end namespace Eigen
149 
150 #endif // EIGEN_SOLVE_H
Definition: LDLT.h:16
const unsigned int RowMajorBit
Definition: Constants.h:53
Definition: Constants.h:494
Definition: Eigen_Colamd.h:54
Definition: Constants.h:482
Pseudo expression representing a solving operation.
Definition: Solve.h:63
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48