Eigen  5.0.1
 
Loading...
Searching...
No Matches
SolveWithGuess.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_SOLVEWITHGUESS_H
12#define EIGEN_SOLVEWITHGUESS_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
19template <typename Decomposition, typename RhsType, typename GuessType>
20class SolveWithGuess;
21
35namespace internal {
36
37template <typename Decomposition, typename RhsType, typename GuessType>
38struct traits<SolveWithGuess<Decomposition, RhsType, GuessType> > : traits<Solve<Decomposition, RhsType> > {};
39
40} // namespace internal
41
42template <typename Decomposition, typename RhsType, typename GuessType>
43class SolveWithGuess : public internal::generic_xpr_base<SolveWithGuess<Decomposition, RhsType, GuessType>, MatrixXpr,
44 typename internal::traits<RhsType>::StorageKind>::type {
45 public:
46 using Scalar = typename internal::traits<SolveWithGuess>::Scalar;
47 using PlainObject = typename internal::traits<SolveWithGuess>::PlainObject;
48 using Base = typename internal::generic_xpr_base<SolveWithGuess<Decomposition, RhsType, GuessType>, MatrixXpr,
49 typename internal::traits<RhsType>::StorageKind>::type;
50 using Nested = typename internal::ref_selector<SolveWithGuess>::type;
51
52 SolveWithGuess(const Decomposition &dec, const RhsType &rhs, const GuessType &guess)
53 : m_dec(dec), m_rhs(rhs), m_guess(guess) {}
54
55 EIGEN_DEVICE_FUNC constexpr Index rows() const noexcept { return m_dec.cols(); }
56 EIGEN_DEVICE_FUNC constexpr Index cols() const noexcept { return m_rhs.cols(); }
57
58 EIGEN_DEVICE_FUNC const Decomposition &dec() const { return m_dec; }
59 EIGEN_DEVICE_FUNC const RhsType &rhs() const { return m_rhs; }
60 EIGEN_DEVICE_FUNC const GuessType &guess() const { return m_guess; }
61
62 protected:
63 const Decomposition &m_dec;
64 const RhsType &m_rhs;
65 const GuessType &m_guess;
66
67 private:
68 Scalar coeff(Index row, Index col) const;
69 Scalar coeff(Index i) const;
70};
71
72namespace internal {
73
74// Evaluator of SolveWithGuess -> eval into a temporary
75template <typename Decomposition, typename RhsType, typename GuessType>
76struct evaluator<SolveWithGuess<Decomposition, RhsType, GuessType> >
77 : public evaluator<typename SolveWithGuess<Decomposition, RhsType, GuessType>::PlainObject> {
79 using PlainObject = typename SolveType::PlainObject;
80 using Base = evaluator<PlainObject>;
81
82 evaluator(const SolveType &solve) : m_result(solve.rows(), solve.cols()) {
83 internal::construct_at<Base>(this, m_result);
84 m_result = solve.guess();
85 solve.dec()._solve_with_guess_impl(solve.rhs(), m_result);
86 }
87
88 protected:
89 PlainObject m_result;
90};
91
92// Specialization for "dst = dec.solveWithGuess(rhs)"
93// NOTE we need to specialize it for Dense2Dense to avoid ambiguous specialization error and a Sparse2Sparse
94// specialization must exist somewhere
95template <typename DstXprType, typename DecType, typename RhsType, typename GuessType, typename Scalar>
96struct Assignment<DstXprType, SolveWithGuess<DecType, RhsType, GuessType>, internal::assign_op<Scalar, Scalar>,
97 Dense2Dense> {
98 using SrcXprType = SolveWithGuess<DecType, RhsType, GuessType>;
99 static void run(DstXprType &dst, const SrcXprType &src, const internal::assign_op<Scalar, Scalar> &) {
100 Index dstRows = src.rows();
101 Index dstCols = src.cols();
102 if ((dst.rows() != dstRows) || (dst.cols() != dstCols)) dst.resize(dstRows, dstCols);
103
104 dst = src.guess();
105 src.dec()._solve_with_guess_impl(src.rhs(), dst);
106 }
107};
108
109} // end namespace internal
110
111} // end namespace Eigen
112
113#endif // EIGEN_SOLVEWITHGUESS_H
Pseudo expression representing a solving operation.
Definition SolveWithGuess.h:44
Definition Constants.h:557