Eigen  3.4.90 (git rev 67eeba6e720c5745abc77ae6c92ce0a44aa7b7ae)
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
14
15 namespace Eigen {
16
17 template<typename Decomposition, typename RhsType, typename StorageKind> class SolveImpl;
18
31 namespace internal {
32
33 // this solve_traits class permits to determine the evaluation type with respect to storage kind (Dense vs Sparse)
34 template<typename Decomposition, typename RhsType,typename StorageKind> struct solve_traits;
35
36 template<typename Decomposition, typename RhsType>
37 struct solve_traits<Decomposition,RhsType,Dense>
38 {
39  typedef typename make_proper_matrix_type<typename RhsType::Scalar,
40  Decomposition::ColsAtCompileTime,
41  RhsType::ColsAtCompileTime,
42  RhsType::PlainObject::Options,
43  Decomposition::MaxColsAtCompileTime,
44  RhsType::MaxColsAtCompileTime>::type PlainObject;
45 };
46
47 template<typename Decomposition, typename RhsType>
48 struct traits<Solve<Decomposition, RhsType> >
49  : traits<typename solve_traits<Decomposition,RhsType,typename internal::traits<RhsType>::StorageKind>::PlainObject>
50 {
51  typedef typename solve_traits<Decomposition,RhsType,typename internal::traits<RhsType>::StorageKind>::PlainObject PlainObject;
52  typedef typename promote_index_type<typename Decomposition::StorageIndex, typename RhsType::StorageIndex>::type StorageIndex;
53  typedef traits<PlainObject> BaseTraits;
54  enum {
55  Flags = BaseTraits::Flags & RowMajorBit,
57  };
58 };
59
60 }
61
62
63 template<typename Decomposition, typename RhsType>
64 class Solve : public SolveImpl<Decomposition,RhsType,typename internal::traits<RhsType>::StorageKind>
65 {
66 public:
67  typedef typename internal::traits<Solve>::PlainObject PlainObject;
68  typedef typename internal::traits<Solve>::StorageIndex StorageIndex;
69
70  Solve(const Decomposition &dec, const RhsType &rhs)
71  : m_dec(dec), m_rhs(rhs)
72  {}
73
74  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT { return m_dec.cols(); }
75  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT { return m_rhs.cols(); }
76
77  EIGEN_DEVICE_FUNC const Decomposition& dec() const { return m_dec; }
78  EIGEN_DEVICE_FUNC const RhsType& rhs() const { return m_rhs; }
79
80 protected:
81  const Decomposition &m_dec;
82  const typename internal::ref_selector<RhsType>::type m_rhs;
83 };
84
85
86 // Specialization of the Solve expression for dense results
87 template<typename Decomposition, typename RhsType>
88 class SolveImpl<Decomposition,RhsType,Dense>
89  : public MatrixBase<Solve<Decomposition,RhsType> >
90 {
91  typedef Solve<Decomposition,RhsType> Derived;
92
93 public:
94
96  EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
97
98 private:
99
100  Scalar coeff(Index row, Index col) const;
101  Scalar coeff(Index i) const;
102 };
103
104 // Generic API dispatcher
105 template<typename Decomposition, typename RhsType, typename StorageKind>
106 class SolveImpl : public internal::generic_xpr_base<Solve<Decomposition,RhsType>, MatrixXpr, StorageKind>::type
107 {
108  public:
109  typedef typename internal::generic_xpr_base<Solve<Decomposition,RhsType>, MatrixXpr, StorageKind>::type Base;
110 };
111
112 namespace internal {
113
114 // Evaluator of Solve -> eval into a temporary
115 template<typename Decomposition, typename RhsType>
116 struct evaluator<Solve<Decomposition,RhsType> >
117  : public evaluator<typename Solve<Decomposition,RhsType>::PlainObject>
118 {
119  typedef Solve<Decomposition,RhsType> SolveType;
120  typedef typename SolveType::PlainObject PlainObject;
121  typedef evaluator<PlainObject> Base;
122
123  enum { Flags = Base::Flags | EvalBeforeNestingBit };
124
125  EIGEN_DEVICE_FUNC explicit evaluator(const SolveType& solve)
126  : m_result(solve.rows(), solve.cols())
127  {
128  internal::construct_at<Base>(this, m_result);
129  solve.dec()._solve_impl(solve.rhs(), m_result);
130  }
131
132 protected:
133  PlainObject m_result;
134 };
135
136 // Specialization for "dst = dec.solve(rhs)"
137 // NOTE we need to specialize it for Dense2Dense to avoid ambiguous specialization error and a Sparse2Sparse specialization must exist somewhere
138 template<typename DstXprType, typename DecType, typename RhsType, typename Scalar>
139 struct Assignment<DstXprType, Solve<DecType,RhsType>, internal::assign_op<Scalar,Scalar>, Dense2Dense>
140 {
141  typedef Solve<DecType,RhsType> SrcXprType;
142  static void run(DstXprType &dst, const SrcXprType &src, const internal::assign_op<Scalar,Scalar> &)
143  {
144  Index dstRows = src.rows();
145  Index dstCols = src.cols();
146  if((dst.rows()!=dstRows) || (dst.cols()!=dstCols))
147  dst.resize(dstRows, dstCols);
148
149  src.dec()._solve_impl(src.rhs(), dst);
150  }
151 };
152
153 // Specialization for "dst = dec.transpose().solve(rhs)"
154 template<typename DstXprType, typename DecType, typename RhsType, typename Scalar>
155 struct Assignment<DstXprType, Solve<Transpose<const DecType>,RhsType>, internal::assign_op<Scalar,Scalar>, Dense2Dense>
156 {
157  typedef Solve<Transpose<const DecType>,RhsType> SrcXprType;
158  static void run(DstXprType &dst, const SrcXprType &src, const internal::assign_op<Scalar,Scalar> &)
159  {
160  Index dstRows = src.rows();
161  Index dstCols = src.cols();
162  if((dst.rows()!=dstRows) || (dst.cols()!=dstCols))
163  dst.resize(dstRows, dstCols);
164
165  src.dec().nestedExpression().template _solve_impl_transposed<false>(src.rhs(), dst);
166  }
167 };
168
169 // Specialization for "dst = dec.adjoint().solve(rhs)"
170 template<typename DstXprType, typename DecType, typename RhsType, typename Scalar>
171 struct Assignment<DstXprType, Solve<CwiseUnaryOp<internal::scalar_conjugate_op<typename DecType::Scalar>, const Transpose<const DecType> >,RhsType>,
172  internal::assign_op<Scalar,Scalar>, Dense2Dense>
173 {
174  typedef Solve<CwiseUnaryOp<internal::scalar_conjugate_op<typename DecType::Scalar>, const Transpose<const DecType> >,RhsType> SrcXprType;
175  static void run(DstXprType &dst, const SrcXprType &src, const internal::assign_op<Scalar,Scalar> &)
176  {
177  Index dstRows = src.rows();
178  Index dstCols = src.cols();
179  if((dst.rows()!=dstRows) || (dst.cols()!=dstCols))
180  dst.resize(dstRows, dstCols);
181
182  src.dec().nestedExpression().nestedExpression().template _solve_impl_transposed<true>(src.rhs(), dst);
183  }
184 };
185
186 } // end namespace internal
187
188 } // end namespace Eigen
189
190 #endif // EIGEN_SOLVE_H
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:52
Pseudo expression representing a solving operation.
Definition: Solve.h:65
const unsigned int EvalBeforeNestingBit
Definition: Constants.h:72
const unsigned int RowMajorBit
Definition: Constants.h:68
Namespace containing all symbols from the Eigen library.
Definition: Core:139
const int HugeCost
Definition: Constants.h:46
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:59
Definition: Constants.h:509