Solve.h
Go to the documentation of this file.
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 // IWYU pragma: private
14 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
18 template <typename Decomposition, typename RhsType, typename StorageKind>
19 class SolveImpl;
20 
33 namespace internal {
34 
35 // this solve_traits class permits to determine the evaluation type with respect to storage kind (Dense vs Sparse)
36 template <typename Decomposition, typename RhsType, typename StorageKind>
37 struct solve_traits;
38 
39 template <typename Decomposition, typename RhsType>
40 struct solve_traits<Decomposition, RhsType, Dense> {
41  typedef typename make_proper_matrix_type<typename RhsType::Scalar, Decomposition::ColsAtCompileTime,
42  RhsType::ColsAtCompileTime, RhsType::PlainObject::Options,
43  Decomposition::MaxColsAtCompileTime, RhsType::MaxColsAtCompileTime>::type
45 };
46 
47 template <typename Decomposition, typename RhsType>
48 struct traits<Solve<Decomposition, RhsType> >
49  : traits<
50  typename solve_traits<Decomposition, RhsType, typename internal::traits<RhsType>::StorageKind>::PlainObject> {
56  enum { Flags = BaseTraits::Flags & RowMajorBit, CoeffReadCost = HugeCost };
57 };
58 
59 } // namespace internal
60 
61 template <typename Decomposition, typename RhsType>
62 class Solve : public SolveImpl<Decomposition, RhsType, typename internal::traits<RhsType>::StorageKind> {
63  public:
66 
67  Solve(const Decomposition &dec, const RhsType &rhs) : m_dec(dec), m_rhs(rhs) {}
68 
71 
72  EIGEN_DEVICE_FUNC const Decomposition &dec() const { return m_dec; }
73  EIGEN_DEVICE_FUNC const RhsType &rhs() const { return m_rhs; }
74 
75  protected:
76  const Decomposition &m_dec;
78 };
79 
80 // Specialization of the Solve expression for dense results
81 template <typename Decomposition, typename RhsType>
82 class SolveImpl<Decomposition, RhsType, Dense> : public MatrixBase<Solve<Decomposition, RhsType> > {
84 
85  public:
88 
89  private:
91  Scalar coeff(Index i) const;
92 };
93 
94 // Generic API dispatcher
95 template <typename Decomposition, typename RhsType, typename StorageKind>
96 class SolveImpl : public internal::generic_xpr_base<Solve<Decomposition, RhsType>, MatrixXpr, StorageKind>::type {
97  public:
99 };
100 
101 namespace internal {
102 
103 // Evaluator of Solve -> eval into a temporary
104 template <typename Decomposition, typename RhsType>
105 struct evaluator<Solve<Decomposition, RhsType> >
106  : public evaluator<typename Solve<Decomposition, RhsType>::PlainObject> {
110 
111  enum { Flags = Base::Flags | EvalBeforeNestingBit };
112 
113  EIGEN_DEVICE_FUNC explicit evaluator(const SolveType &solve) : m_result(solve.rows(), solve.cols()) {
114  internal::construct_at<Base>(this, m_result);
115  solve.dec()._solve_impl(solve.rhs(), m_result);
116  }
117 
118  protected:
120 };
121 
122 // Specialization for "dst = dec.solve(rhs)"
123 // NOTE we need to specialize it for Dense2Dense to avoid ambiguous specialization error and a Sparse2Sparse
124 // specialization must exist somewhere
125 template <typename DstXprType, typename DecType, typename RhsType, typename Scalar>
126 struct Assignment<DstXprType, Solve<DecType, RhsType>, internal::assign_op<Scalar, Scalar>, Dense2Dense> {
128  static void run(DstXprType &dst, const SrcXprType &src, const internal::assign_op<Scalar, Scalar> &) {
129  Index dstRows = src.rows();
130  Index dstCols = src.cols();
131  if ((dst.rows() != dstRows) || (dst.cols() != dstCols)) dst.resize(dstRows, dstCols);
132 
133  src.dec()._solve_impl(src.rhs(), dst);
134  }
135 };
136 
137 // Specialization for "dst = dec.transpose().solve(rhs)"
138 template <typename DstXprType, typename DecType, typename RhsType, typename Scalar>
139 struct Assignment<DstXprType, Solve<Transpose<const DecType>, RhsType>, internal::assign_op<Scalar, Scalar>,
140  Dense2Dense> {
142  static void run(DstXprType &dst, const SrcXprType &src, const internal::assign_op<Scalar, Scalar> &) {
143  Index dstRows = src.rows();
144  Index dstCols = src.cols();
145  if ((dst.rows() != dstRows) || (dst.cols() != dstCols)) dst.resize(dstRows, dstCols);
146 
147  src.dec().nestedExpression().template _solve_impl_transposed<false>(src.rhs(), dst);
148  }
149 };
150 
151 // Specialization for "dst = dec.adjoint().solve(rhs)"
152 template <typename DstXprType, typename DecType, typename RhsType, typename Scalar>
153 struct Assignment<
154  DstXprType,
155  Solve<CwiseUnaryOp<internal::scalar_conjugate_op<typename DecType::Scalar>, const Transpose<const DecType> >,
156  RhsType>,
157  internal::assign_op<Scalar, Scalar>, Dense2Dense> {
159  RhsType>
161  static void run(DstXprType &dst, const SrcXprType &src, const internal::assign_op<Scalar, Scalar> &) {
162  Index dstRows = src.rows();
163  Index dstCols = src.cols();
164  if ((dst.rows() != dstRows) || (dst.cols() != dstCols)) dst.resize(dstRows, dstCols);
165 
166  src.dec().nestedExpression().nestedExpression().template _solve_impl_transposed<true>(src.rhs(), dst);
167  }
168 };
169 
170 } // end namespace internal
171 
172 } // end namespace Eigen
173 
174 #endif // EIGEN_SOLVE_H
int i
Definition: BiCGSTAB_step_by_step.cpp:9
#define EIGEN_NOEXCEPT
Definition: Macros.h:1267
#define EIGEN_CONSTEXPR
Definition: Macros.h:758
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:892
#define EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
Definition: Macros.h:1171
m col(1)
m row(1)
int rows
Definition: Tutorial_commainit_02.cpp:1
int cols
Definition: Tutorial_commainit_02.cpp:1
SCALAR Scalar
Definition: bench_gemm.cpp:45
Generic expression where a coefficient-wise unary operator is applied to an expression.
Definition: CwiseUnaryOp.h:53
internal::traits< Solve< Decomposition, RhsType > >::Scalar Scalar
Definition: DenseBase.h:62
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:52
MatrixBase< Solve< Decomposition, RhsType > > Base
Definition: Solve.h:86
Scalar coeff(Index row, Index col) const
Solve< Decomposition, RhsType > Derived
Definition: Solve.h:83
Definition: Solve.h:96
internal::generic_xpr_base< Solve< Decomposition, RhsType >, MatrixXpr, StorageKind >::type Base
Definition: Solve.h:98
Pseudo expression representing a solving operation.
Definition: Solve.h:62
internal::traits< Solve >::PlainObject PlainObject
Definition: Solve.h:64
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition: Solve.h:70
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition: Solve.h:69
internal::traits< Solve >::StorageIndex StorageIndex
Definition: Solve.h:65
const Decomposition & m_dec
Definition: Solve.h:76
EIGEN_DEVICE_FUNC const Decomposition & dec() const
Definition: Solve.h:72
Solve(const Decomposition &dec, const RhsType &rhs)
Definition: Solve.h:67
EIGEN_DEVICE_FUNC const RhsType & rhs() const
Definition: Solve.h:73
const internal::ref_selector< RhsType >::type m_rhs
Definition: Solve.h:77
Expression of the transpose of a matrix.
Definition: Transpose.h:56
Definition: XprHelper.h:352
const unsigned int EvalBeforeNestingBit
Definition: Constants.h:74
const unsigned int RowMajorBit
Definition: Constants.h:70
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
const int HugeCost
Definition: Constants.h:48
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
Extend namespace for flags.
Definition: fsi_chan_precond_driver.cc:56
type
Definition: compute_granudrum_aor.py:141
Definition: Eigen_Colamd.h:49
Update the problem specs before solve
Definition: steady_axisym_advection_diffusion.cc:353
Definition: Constants.h:519
Definition: Constants.h:534
Solve< CwiseUnaryOp< internal::scalar_conjugate_op< typename DecType::Scalar >, const Transpose< const DecType > >, RhsType > SrcXprType
Definition: Solve.h:160
static void run(DstXprType &dst, const SrcXprType &src, const internal::assign_op< Scalar, Scalar > &)
Definition: Solve.h:128
static void run(DstXprType &dst, const SrcXprType &src, const internal::assign_op< Scalar, Scalar > &)
Definition: Solve.h:142
Definition: AssignEvaluator.h:773
Definition: AssignEvaluator.h:756
Template functor for scalar/packet assignment.
Definition: AssignmentFunctors.h:25
Solve< Decomposition, RhsType > SolveType
Definition: Solve.h:107
EIGEN_DEVICE_FUNC evaluator(const SolveType &solve)
Definition: Solve.h:113
SolveType::PlainObject PlainObject
Definition: Solve.h:108
evaluator< PlainObject > Base
Definition: Solve.h:109
Definition: CoreEvaluators.h:104
Definition: XprHelper.h:575
std::conditional_t<(sizeof(I1)< sizeof(I2)), I2, I1 > type
Definition: XprHelper.h:146
std::conditional_t< bool(traits< T >::Flags &NestByRefBit), T const &, const T > type
Definition: XprHelper.h:507
Template functor to compute the conjugate of a complex value.
Definition: functors/UnaryFunctors.h:132
make_proper_matrix_type< typename RhsType::Scalar, Decomposition::ColsAtCompileTime, RhsType::ColsAtCompileTime, RhsType::PlainObject::Options, Decomposition::MaxColsAtCompileTime, RhsType::MaxColsAtCompileTime >::type PlainObject
Definition: Solve.h:44
Definition: Solve.h:37
promote_index_type< typename Decomposition::StorageIndex, typename RhsType::StorageIndex >::type StorageIndex
Definition: Solve.h:54
traits< PlainObject > BaseTraits
Definition: Solve.h:55
solve_traits< Decomposition, RhsType, typename internal::traits< RhsType >::StorageKind >::PlainObject PlainObject
Definition: Solve.h:52
Definition: ForwardDeclarations.h:21