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