WPILibC++ 2023.4.3
SparseAssign.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) 2008-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_SPARSEASSIGN_H
11#define EIGEN_SPARSEASSIGN_H
12
13namespace Eigen {
14
15template<typename Derived>
16template<typename OtherDerived>
18{
20 return derived();
21}
22
23template<typename Derived>
24template<typename OtherDerived>
26{
27 // TODO use the evaluator mechanism
28 other.evalTo(derived());
29 return derived();
30}
31
32template<typename Derived>
33template<typename OtherDerived>
35{
36 // by default sparse evaluation do not alias, so we can safely bypass the generic call_assignment routine
39 return derived();
40}
41
42template<typename Derived>
43inline Derived& SparseMatrixBase<Derived>::operator=(const Derived& other)
44{
45 internal::call_assignment_no_alias(derived(), other.derived());
46 return derived();
47}
48
49namespace internal {
50
51template<>
54};
56template<>
59};
60
61struct Sparse2Sparse {};
62struct Sparse2Dense {};
63
64template<> struct AssignmentKind<SparseShape, SparseShape> { typedef Sparse2Sparse Kind; };
66template<> struct AssignmentKind<DenseShape, SparseShape> { typedef Sparse2Dense Kind; };
68
69
70template<typename DstXprType, typename SrcXprType>
71void assign_sparse_to_sparse(DstXprType &dst, const SrcXprType &src)
72{
73 typedef typename DstXprType::Scalar Scalar;
74 typedef internal::evaluator<DstXprType> DstEvaluatorType;
75 typedef internal::evaluator<SrcXprType> SrcEvaluatorType;
76
77 SrcEvaluatorType srcEvaluator(src);
78
79 const bool transpose = (DstEvaluatorType::Flags & RowMajorBit) != (SrcEvaluatorType::Flags & RowMajorBit);
80 const Index outerEvaluationSize = (SrcEvaluatorType::Flags&RowMajorBit) ? src.rows() : src.cols();
81 if ((!transpose) && src.isRValue())
82 {
83 // eval without temporary
84 dst.resize(src.rows(), src.cols());
85 dst.setZero();
86 dst.reserve((std::min)(src.rows()*src.cols(), (std::max)(src.rows(),src.cols())*2));
87 for (Index j=0; j<outerEvaluationSize; ++j)
88 {
89 dst.startVec(j);
90 for (typename SrcEvaluatorType::InnerIterator it(srcEvaluator, j); it; ++it)
91 {
92 Scalar v = it.value();
93 dst.insertBackByOuterInner(j,it.index()) = v;
94 }
95 }
96 dst.finalize();
97 }
98 else
99 {
100 // eval through a temporary
102 (!((DstEvaluatorType::Flags & RowMajorBit) != (SrcEvaluatorType::Flags & RowMajorBit)))) &&
103 "the transpose operation is supposed to be handled in SparseMatrix::operator=");
104
105 enum { Flip = (DstEvaluatorType::Flags & RowMajorBit) != (SrcEvaluatorType::Flags & RowMajorBit) };
106
107
108 DstXprType temp(src.rows(), src.cols());
109
110 temp.reserve((std::min)(src.rows()*src.cols(), (std::max)(src.rows(),src.cols())*2));
111 for (Index j=0; j<outerEvaluationSize; ++j)
112 {
113 temp.startVec(j);
114 for (typename SrcEvaluatorType::InnerIterator it(srcEvaluator, j); it; ++it)
115 {
116 Scalar v = it.value();
117 temp.insertBackByOuterInner(Flip?it.index():j,Flip?j:it.index()) = v;
118 }
119 }
120 temp.finalize();
121
122 dst = temp.markAsRValue();
123 }
124}
125
126// Generic Sparse to Sparse assignment
127template< typename DstXprType, typename SrcXprType, typename Functor>
128struct Assignment<DstXprType, SrcXprType, Functor, Sparse2Sparse>
129{
130 static void run(DstXprType &dst, const SrcXprType &src, const internal::assign_op<typename DstXprType::Scalar,typename SrcXprType::Scalar> &/*func*/)
131 {
132 assign_sparse_to_sparse(dst.derived(), src.derived());
133 }
134};
135
136// Generic Sparse to Dense assignment
137template< typename DstXprType, typename SrcXprType, typename Functor, typename Weak>
138struct Assignment<DstXprType, SrcXprType, Functor, Sparse2Dense, Weak>
139{
140 static void run(DstXprType &dst, const SrcXprType &src, const Functor &func)
141 {
143 dst.setZero();
144
146 resize_if_allowed(dst, src, func);
148
149 const Index outerEvaluationSize = (internal::evaluator<SrcXprType>::Flags&RowMajorBit) ? src.rows() : src.cols();
150 for (Index j=0; j<outerEvaluationSize; ++j)
151 for (typename internal::evaluator<SrcXprType>::InnerIterator i(srcEval,j); i; ++i)
152 func.assignCoeff(dstEval.coeffRef(i.row(),i.col()), i.value());
153 }
154};
155
156// Specialization for dense ?= dense +/- sparse and dense ?= sparse +/- dense
157template<typename DstXprType, typename Func1, typename Func2>
159{
160 template<typename SrcXprType, typename InitialFunc>
162 void run(DstXprType &dst, const SrcXprType &src, const InitialFunc& /*func*/)
163 {
164 #ifdef EIGEN_SPARSE_ASSIGNMENT_FROM_DENSE_OP_SPARSE_PLUGIN
165 EIGEN_SPARSE_ASSIGNMENT_FROM_DENSE_OP_SPARSE_PLUGIN
166 #endif
167
168 call_assignment_no_alias(dst, src.lhs(), Func1());
169 call_assignment_no_alias(dst, src.rhs(), Func2());
170 }
171
172 // Specialization for dense1 = sparse + dense2; -> dense1 = dense2; dense1 += sparse;
173 template<typename Lhs, typename Rhs, typename Scalar>
176 run(DstXprType &dst, const CwiseBinaryOp<internal::scalar_sum_op<Scalar,Scalar>, const Lhs, const Rhs> &src,
178 {
179 #ifdef EIGEN_SPARSE_ASSIGNMENT_FROM_SPARSE_ADD_DENSE_PLUGIN
180 EIGEN_SPARSE_ASSIGNMENT_FROM_SPARSE_ADD_DENSE_PLUGIN
181 #endif
182
183 // Apply the dense matrix first, then the sparse one.
184 call_assignment_no_alias(dst, src.rhs(), Func1());
185 call_assignment_no_alias(dst, src.lhs(), Func2());
186 }
187
188 // Specialization for dense1 = sparse - dense2; -> dense1 = -dense2; dense1 += sparse;
189 template<typename Lhs, typename Rhs, typename Scalar>
192 run(DstXprType &dst, const CwiseBinaryOp<internal::scalar_difference_op<Scalar,Scalar>, const Lhs, const Rhs> &src,
194 {
195 #ifdef EIGEN_SPARSE_ASSIGNMENT_FROM_SPARSE_SUB_DENSE_PLUGIN
196 EIGEN_SPARSE_ASSIGNMENT_FROM_SPARSE_SUB_DENSE_PLUGIN
197 #endif
198
199 // Apply the dense matrix first, then the sparse one.
200 call_assignment_no_alias(dst, -src.rhs(), Func1());
201 call_assignment_no_alias(dst, src.lhs(), add_assign_op<typename DstXprType::Scalar,typename Lhs::Scalar>());
202 }
203};
205#define EIGEN_CATCH_ASSIGN_DENSE_OP_SPARSE(ASSIGN_OP,BINOP,ASSIGN_OP2) \
206 template< typename DstXprType, typename Lhs, typename Rhs, typename Scalar> \
207 struct Assignment<DstXprType, CwiseBinaryOp<internal::BINOP<Scalar,Scalar>, const Lhs, const Rhs>, internal::ASSIGN_OP<typename DstXprType::Scalar,Scalar>, \
208 Sparse2Dense, \
209 typename internal::enable_if< internal::is_same<typename internal::evaluator_traits<Lhs>::Shape,DenseShape>::value \
210 || internal::is_same<typename internal::evaluator_traits<Rhs>::Shape,DenseShape>::value>::type> \
211 : assignment_from_dense_op_sparse<DstXprType, internal::ASSIGN_OP<typename DstXprType::Scalar,typename Lhs::Scalar>, internal::ASSIGN_OP2<typename DstXprType::Scalar,typename Rhs::Scalar> > \
212 {}
213
217
221
222
223// Specialization for "dst = dec.solve(rhs)"
224// NOTE we need to specialize it for Sparse2Sparse to avoid ambiguous specialization error
225template<typename DstXprType, typename DecType, typename RhsType, typename Scalar>
226struct Assignment<DstXprType, Solve<DecType,RhsType>, internal::assign_op<Scalar,Scalar>, Sparse2Sparse>
227{
229 static void run(DstXprType &dst, const SrcXprType &src, const internal::assign_op<Scalar,Scalar> &)
230 {
231 Index dstRows = src.rows();
232 Index dstCols = src.cols();
233 if((dst.rows()!=dstRows) || (dst.cols()!=dstCols))
234 dst.resize(dstRows, dstCols);
235
236 src.dec()._solve_impl(src.rhs(), dst);
237 }
238};
239
241
243
244template< typename DstXprType, typename SrcXprType, typename Functor>
245struct Assignment<DstXprType, SrcXprType, Functor, Diagonal2Sparse>
246{
247 typedef typename DstXprType::StorageIndex StorageIndex;
248 typedef typename DstXprType::Scalar Scalar;
249
250 template<int Options, typename AssignFunc>
251 static void run(SparseMatrix<Scalar,Options,StorageIndex> &dst, const SrcXprType &src, const AssignFunc &func)
252 { dst.assignDiagonal(src.diagonal(), func); }
253
254 template<typename DstDerived>
256 { dst.derived().diagonal() = src.diagonal(); }
257
258 template<typename DstDerived>
260 { dst.derived().diagonal() += src.diagonal(); }
261
262 template<typename DstDerived>
264 { dst.derived().diagonal() -= src.diagonal(); }
265};
266} // end namespace internal
267
268} // end namespace Eigen
269
270#endif // EIGEN_SPARSEASSIGN_H
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:986
#define eigen_assert(x)
Definition: Macros.h:1047
#define EIGEN_STRONG_INLINE
Definition: Macros.h:927
Generic expression where a coefficient-wise binary operator is applied to two expressions.
Definition: CwiseBinaryOp.h:84
Definition: ReturnByValue.h:52
EIGEN_DEVICE_FUNC void evalTo(Dest &dst) const
Definition: ReturnByValue.h:61
Pseudo expression representing a solving operation.
Definition: Solve.h:63
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition: Solve.h:73
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition: Solve.h:72
EIGEN_DEVICE_FUNC const RhsType & rhs() const
Definition: Solve.h:76
EIGEN_DEVICE_FUNC const Decomposition & dec() const
Definition: Solve.h:75
Base class of any sparse matrices or sparse expressions.
Definition: SparseMatrixBase.h:28
const Derived & derived() const
Definition: SparseMatrixBase.h:143
Derived & operator=(const EigenBase< OtherDerived > &other)
Definition: SparseAssign.h:17
A versatible sparse matrix representation.
Definition: SparseMatrix.h:98
void assignDiagonal(const DiagXpr diagXpr, const Func &assignFunc)
Definition: SparseMatrix.h:927
Definition: core.h:1240
type
Definition: core.h:575
const unsigned int RowMajorBit
for a matrix, this means that the storage order is row-major.
Definition: Constants.h:66
constexpr common_t< T1, T2 > max(const T1 x, const T2 y) noexcept
Compile-time pairwise maximum function.
Definition: max.hpp:35
constexpr common_t< T1, T2 > min(const T1 x, const T2 y) noexcept
Compile-time pairwise minimum function.
Definition: min.hpp:35
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void resize_if_allowed(DstXprType &dst, const SrcXprType &src, const Functor &)
Definition: AssignEvaluator.h:750
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void call_assignment_no_alias(Dst &dst, const Src &src, const Func &func)
Definition: AssignEvaluator.h:873
void assign_sparse_to_sparse(DstXprType &dst, const SrcXprType &src)
Definition: SparseAssign.h:71
EIGEN_CATCH_ASSIGN_DENSE_OP_SPARSE(assign_op, scalar_sum_op, add_assign_op)
Namespace containing all symbols from the Eigen library.
Definition: MatrixExponential.h:16
const int OuterRandomAccessPattern
Definition: SparseUtil.h:49
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
Definition: Eigen_Colamd.h:50
Definition: Constants.h:528
Definition: Constants.h:531
Common base class for all classes T such that MatrixBase has an operator=(T) and a constructor Matrix...
Definition: EigenBase.h:30
EIGEN_DEVICE_FUNC Derived & derived()
Definition: EigenBase.h:46
The type used to identify a general sparse storage.
Definition: Constants.h:510
Definition: Constants.h:537
static void run(DstXprType &dst, const SrcXprType &src, const internal::assign_op< Scalar, Scalar > &)
Definition: SparseAssign.h:229
static void run(SparseMatrixBase< DstDerived > &dst, const SrcXprType &src, const internal::add_assign_op< typename DstXprType::Scalar, typename SrcXprType::Scalar > &)
Definition: SparseAssign.h:259
static void run(SparseMatrixBase< DstDerived > &dst, const SrcXprType &src, const internal::sub_assign_op< typename DstXprType::Scalar, typename SrcXprType::Scalar > &)
Definition: SparseAssign.h:263
DstXprType::StorageIndex StorageIndex
Definition: SparseAssign.h:247
static void run(SparseMatrixBase< DstDerived > &dst, const SrcXprType &src, const internal::assign_op< typename DstXprType::Scalar, typename SrcXprType::Scalar > &)
Definition: SparseAssign.h:255
static void run(SparseMatrix< Scalar, Options, StorageIndex > &dst, const SrcXprType &src, const AssignFunc &func)
Definition: SparseAssign.h:251
static void run(DstXprType &dst, const SrcXprType &src, const Functor &func)
Definition: SparseAssign.h:140
static void run(DstXprType &dst, const SrcXprType &src, const internal::assign_op< typename DstXprType::Scalar, typename SrcXprType::Scalar > &)
Definition: SparseAssign.h:130
Definition: AssignEvaluator.h:824
Sparse2Dense Kind
Definition: SparseAssign.h:66
Diagonal2Sparse Kind
Definition: SparseAssign.h:242
Sparse2Sparse Kind
Definition: SparseAssign.h:64
Definition: AssignEvaluator.h:817
Definition: SparseAssign.h:240
Definition: Constants.h:545
Definition: SparseAssign.h:62
Definition: SparseAssign.h:61
Definition: SparseUtil.h:137
Definition: AssignmentFunctors.h:46
Definition: AssignmentFunctors.h:21
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(DstXprType &dst, const SrcXprType &src, const InitialFunc &)
Definition: SparseAssign.h:162
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE internal::enable_if< internal::is_same< typenameinternal::evaluator_traits< Rhs >::Shape, DenseShape >::value >::type run(DstXprType &dst, const CwiseBinaryOp< internal::scalar_sum_op< Scalar, Scalar >, const Lhs, const Rhs > &src, const internal::assign_op< typename DstXprType::Scalar, Scalar > &)
Definition: SparseAssign.h:176
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE internal::enable_if< internal::is_same< typenameinternal::evaluator_traits< Rhs >::Shape, DenseShape >::value >::type run(DstXprType &dst, const CwiseBinaryOp< internal::scalar_difference_op< Scalar, Scalar >, const Lhs, const Rhs > &src, const internal::assign_op< typename DstXprType::Scalar, Scalar > &)
Definition: SparseAssign.h:192
Definition: Meta.h:273
Definition: CoreEvaluators.h:91
Definition: Meta.h:148
Definition: BinaryFunctors.h:350
Definition: BinaryFunctors.h:33
IteratorBased Kind
Definition: SparseAssign.h:53
SparseShape Shape
Definition: SparseAssign.h:58
Definition: CoreEvaluators.h:29
Definition: AssignmentFunctors.h:67
Definition: ForwardDeclarations.h:17