Medial Code Documentation
Loading...
Searching...
No Matches
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
13namespace Eigen {
14
15template<typename Decomposition, typename RhsType, typename StorageKind> class SolveImpl;
16
29namespace internal {
30
31// this solve_traits class permits to determine the evaluation type with respect to storage kind (Dense vs Sparse)
32template<typename Decomposition, typename RhsType,typename StorageKind> struct solve_traits;
33
34template<typename Decomposition, typename RhsType>
36{
37 typedef Matrix<typename RhsType::Scalar,
38 Decomposition::ColsAtCompileTime,
39 RhsType::ColsAtCompileTime,
40 RhsType::PlainObject::Options,
41 Decomposition::MaxColsAtCompileTime,
42 RhsType::MaxColsAtCompileTime> PlainObject;
43};
44
45template<typename Decomposition, typename RhsType>
47 : traits<typename solve_traits<Decomposition,RhsType,typename internal::traits<RhsType>::StorageKind>::PlainObject>
48{
52 enum {
53 Flags = BaseTraits::Flags & RowMajorBit,
54 CoeffReadCost = HugeCost
55 };
56};
57
58}
59
60
61template<typename Decomposition, typename RhsType>
62class Solve : public SolveImpl<Decomposition,RhsType,typename internal::traits<RhsType>::StorageKind>
63{
64public:
65 typedef typename internal::traits<Solve>::PlainObject PlainObject;
66 typedef typename internal::traits<Solve>::StorageIndex StorageIndex;
67
68 Solve(const Decomposition &dec, const RhsType &rhs)
69 : m_dec(dec), m_rhs(rhs)
70 {}
71
72 EIGEN_DEVICE_FUNC Index rows() const { return m_dec.cols(); }
73 EIGEN_DEVICE_FUNC Index cols() const { return m_rhs.cols(); }
74
75 EIGEN_DEVICE_FUNC const Decomposition& dec() const { return m_dec; }
76 EIGEN_DEVICE_FUNC const RhsType& rhs() const { return m_rhs; }
77
78protected:
79 const Decomposition &m_dec;
80 const RhsType &m_rhs;
81};
82
83
84// Specialization of the Solve expression for dense results
85template<typename Decomposition, typename RhsType>
87 : public MatrixBase<Solve<Decomposition,RhsType> >
88{
90
91public:
92
94 EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
95
96private:
97
98 Scalar coeff(Index row, Index col) const;
99 Scalar coeff(Index i) const;
100};
101
102// Generic API dispatcher
103template<typename Decomposition, typename RhsType, typename StorageKind>
104class SolveImpl : public internal::generic_xpr_base<Solve<Decomposition,RhsType>, MatrixXpr, StorageKind>::type
105{
106 public:
107 typedef typename internal::generic_xpr_base<Solve<Decomposition,RhsType>, MatrixXpr, StorageKind>::type Base;
108};
109
110namespace internal {
111
112// Evaluator of Solve -> eval into a temporary
113template<typename Decomposition, typename RhsType>
115 : public evaluator<typename Solve<Decomposition,RhsType>::PlainObject>
116{
118 typedef typename SolveType::PlainObject PlainObject;
120
121 enum { Flags = Base::Flags | EvalBeforeNestingBit };
122
123 EIGEN_DEVICE_FUNC explicit evaluator(const SolveType& solve)
124 : m_result(solve.rows(), solve.cols())
125 {
126 ::new (static_cast<Base*>(this)) Base(m_result);
127 solve.dec()._solve_impl(solve.rhs(), m_result);
128 }
129
130protected:
131 PlainObject m_result;
132};
133
134// Specialization for "dst = dec.solve(rhs)"
135// NOTE we need to specialize it for Dense2Dense to avoid ambiguous specialization error and a Sparse2Sparse specialization must exist somewhere
136template<typename DstXprType, typename DecType, typename RhsType, typename Scalar>
137struct Assignment<DstXprType, Solve<DecType,RhsType>, internal::assign_op<Scalar>, Dense2Dense, Scalar>
138{
140 static void run(DstXprType &dst, const SrcXprType &src, const internal::assign_op<Scalar> &)
141 {
142 // FIXME shall we resize dst here?
143 src.dec()._solve_impl(src.rhs(), dst);
144 }
145};
146
147// Specialization for "dst = dec.transpose().solve(rhs)"
148template<typename DstXprType, typename DecType, typename RhsType, typename Scalar>
149struct Assignment<DstXprType, Solve<Transpose<const DecType>,RhsType>, internal::assign_op<Scalar>, Dense2Dense, Scalar>
150{
152 static void run(DstXprType &dst, const SrcXprType &src, const internal::assign_op<Scalar> &)
153 {
154 src.dec().nestedExpression().template _solve_impl_transposed<false>(src.rhs(), dst);
155 }
156};
157
158// Specialization for "dst = dec.adjoint().solve(rhs)"
159template<typename DstXprType, typename DecType, typename RhsType, typename Scalar>
160struct Assignment<DstXprType, Solve<CwiseUnaryOp<internal::scalar_conjugate_op<typename DecType::Scalar>, const Transpose<const DecType> >,RhsType>, internal::assign_op<Scalar>, Dense2Dense, Scalar>
161{
163 static void run(DstXprType &dst, const SrcXprType &src, const internal::assign_op<Scalar> &)
164 {
165 src.dec().nestedExpression().nestedExpression().template _solve_impl_transposed<true>(src.rhs(), dst);
166 }
167};
168
169} // end namepsace internal
170
171} // end namespace Eigen
172
173#endif // EIGEN_SOLVE_H
Generic expression where a coefficient-wise unary operator is applied to an expression.
Definition CwiseUnaryOp.h:57
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:50
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:180
Definition Solve.h:105
Pseudo expression representing a solving operation.
Definition Solve.h:63
Expression of the transpose of a matrix.
Definition Transpose.h:55
const unsigned int EvalBeforeNestingBit
means the expression should be evaluated by the calling expression
Definition Constants.h:65
const unsigned int RowMajorBit
for a matrix, this means that the storage order is row-major.
Definition Constants.h:61
The type used to identify a dense storage.
Definition Constants.h:490
The type used to identify a matrix expression.
Definition Constants.h:505
Definition AssignEvaluator.h:684
Definition AssignEvaluator.h:674
Definition AssignmentFunctors.h:21
Definition CoreEvaluators.h:82
Definition XprHelper.h:445
Definition UnaryFunctors.h:109
Definition Solve.h:32
Definition ForwardDeclarations.h:17
Definition Meta.h:30