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 
13 namespace Eigen {
14 
15 template<typename Derived>
16 template<typename OtherDerived>
18 {
19  internal::call_assignment_no_alias(derived(), other.derived());
20  return derived();
21 }
22 
23 template<typename Derived>
24 template<typename OtherDerived>
26 {
27  // TODO use the evaluator mechanism
28  other.evalTo(derived());
29  return derived();
30 }
31 
32 template<typename Derived>
33 template<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 
42 template<typename Derived>
43 inline Derived& SparseMatrixBase<Derived>::operator=(const Derived& other)
44 {
45  internal::call_assignment_no_alias(derived(), other.derived());
46  return derived();
47 }
48 
49 namespace internal {
50 
51 template<>
54 };
55 
56 template<>
58  typedef SparseShape Shape;
59 };
60 
61 struct Sparse2Sparse {};
62 struct Sparse2Dense {};
63 
64 template<> struct AssignmentKind<SparseShape, SparseShape> { typedef Sparse2Sparse Kind; };
66 template<> struct AssignmentKind<DenseShape, SparseShape> { typedef Sparse2Dense Kind; };
68 
69 
70 template<typename DstXprType, typename SrcXprType>
71 void 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::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::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
127 template< typename DstXprType, typename SrcXprType, typename Functor>
128 struct 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
137 template< typename DstXprType, typename SrcXprType, typename Functor>
138 struct Assignment<DstXprType, SrcXprType, Functor, Sparse2Dense>
139 {
140  static void run(DstXprType &dst, const SrcXprType &src, const Functor &func)
141  {
143  dst.setZero();
144 
145  internal::evaluator<SrcXprType> srcEval(src);
146  resize_if_allowed(dst, src, func);
147  internal::evaluator<DstXprType> dstEval(dst);
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 "dst = dec.solve(rhs)"
157 // NOTE we need to specialize it for Sparse2Sparse to avoid ambiguous specialization error
158 template<typename DstXprType, typename DecType, typename RhsType, typename Scalar>
159 struct Assignment<DstXprType, Solve<DecType,RhsType>, internal::assign_op<Scalar,Scalar>, Sparse2Sparse>
160 {
162  static void run(DstXprType &dst, const SrcXprType &src, const internal::assign_op<Scalar,Scalar> &)
163  {
164  Index dstRows = src.rows();
165  Index dstCols = src.cols();
166  if((dst.rows()!=dstRows) || (dst.cols()!=dstCols))
167  dst.resize(dstRows, dstCols);
168 
169  src.dec()._solve_impl(src.rhs(), dst);
170  }
171 };
172 
173 struct Diagonal2Sparse {};
174 
176 
177 template< typename DstXprType, typename SrcXprType, typename Functor>
178 struct Assignment<DstXprType, SrcXprType, Functor, Diagonal2Sparse>
179 {
180  typedef typename DstXprType::StorageIndex StorageIndex;
181  typedef typename DstXprType::Scalar Scalar;
184  template<int Options>
186  {
187  Index dstRows = src.rows();
188  Index dstCols = src.cols();
189  if((dst.rows()!=dstRows) || (dst.cols()!=dstCols))
190  dst.resize(dstRows, dstCols);
191 
192  Index size = src.diagonal().size();
193  dst.makeCompressed();
194  dst.resizeNonZeros(size);
195  Map<ArrayXI>(dst.innerIndexPtr(), size).setLinSpaced(0,StorageIndex(size)-1);
196  Map<ArrayXI>(dst.outerIndexPtr(), size+1).setLinSpaced(0,StorageIndex(size));
197  Map<ArrayXS>(dst.valuePtr(), size) = src.diagonal();
198  }
199 
200  template<typename DstDerived>
202  {
203  dst.diagonal() = src.diagonal();
204  }
205 
206  static void run(DstXprType &dst, const SrcXprType &src, const internal::add_assign_op<typename DstXprType::Scalar,typename SrcXprType::Scalar> &/*func*/)
207  { dst.diagonal() += src.diagonal(); }
208 
209  static void run(DstXprType &dst, const SrcXprType &src, const internal::sub_assign_op<typename DstXprType::Scalar,typename SrcXprType::Scalar> &/*func*/)
210  { dst.diagonal() -= src.diagonal(); }
211 };
212 } // end namespace internal
213 
214 } // end namespace Eigen
215 
216 #endif // EIGEN_SPARSEASSIGN_H
Eigen::SparseMatrix::resize
void resize(Index rows, Index cols)
Definition: SparseMatrix.h:621
Eigen::SparseMatrix::cols
Index cols() const
Definition: SparseMatrix.h:138
Eigen::internal::Assignment< DstXprType, Solve< DecType, RhsType >, internal::assign_op< Scalar, Scalar >, Sparse2Sparse >::SrcXprType
Solve< DecType, RhsType > SrcXprType
Definition: SparseAssign.h:161
Eigen::internal::Assignment< DstXprType, Solve< DecType, RhsType >, internal::assign_op< Scalar, Scalar >, Sparse2Sparse >::run
static void run(DstXprType &dst, const SrcXprType &src, const internal::assign_op< Scalar, Scalar > &)
Definition: SparseAssign.h:162
Eigen
Definition: common.h:73
Eigen::internal::Assignment< DstXprType, SrcXprType, Functor, Diagonal2Sparse >::ArrayXS
Array< Scalar, Dynamic, 1 > ArrayXS
Definition: SparseAssign.h:183
Eigen::SparseMatrix
A versatible sparse matrix representation.
Definition: SparseMatrix.h:96
Eigen::ReturnByValue
Definition: ReturnByValue.h:50
Eigen::EigenBase::derived
EIGEN_DEVICE_FUNC Derived & derived()
Definition: EigenBase.h:45
Eigen::internal::AssignmentKind< DenseShape, SparseTriangularShape >::Kind
Sparse2Dense Kind
Definition: SparseAssign.h:67
Eigen::DenseShape
Definition: Constants.h:512
Eigen::internal::SparseTriangularShape
Definition: SparseUtil.h:137
Eigen::internal::Assignment< DstXprType, SrcXprType, Functor, Diagonal2Sparse >::run
static void run(SparseMatrix< Scalar, Options, StorageIndex > &dst, const SrcXprType &src, const internal::assign_op< typename DstXprType::Scalar, typename SrcXprType::Scalar > &)
Definition: SparseAssign.h:185
Eigen::Sparse
Definition: Constants.h:494
Eigen::EigenBase
Definition: EigenBase.h:29
eigen_assert
#define eigen_assert(x)
Definition: Macros.h:579
Eigen::RowMajorBit
const unsigned int RowMajorBit
Definition: Constants.h:61
Eigen::Solve::cols
EIGEN_DEVICE_FUNC Index cols() const
Definition: Solve.h:73
Eigen::internal::storage_kind_to_shape
Definition: CoreEvaluators.h:29
Eigen::Array
General-purpose arrays with easy API for coefficient-wise operations.
Definition: Array.h:45
Eigen::internal::assign_sparse_to_sparse
void assign_sparse_to_sparse(DstXprType &dst, const SrcXprType &src)
Definition: SparseAssign.h:71
Eigen::SparseMatrixBase::operator=
Derived & operator=(const EigenBase< OtherDerived > &other)
Definition: SparseAssign.h:17
Eigen::internal::storage_kind_to_evaluator_kind< Sparse >::Kind
IteratorBased Kind
Definition: SparseAssign.h:53
Scalar
SCALAR Scalar
Definition: common.h:84
Eigen::internal::call_assignment_no_alias
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void call_assignment_no_alias(Dst &dst, const Src &src, const Func &func)
Definition: AssignEvaluator.h:819
Eigen::Solve::dec
const EIGEN_DEVICE_FUNC Decomposition & dec() const
Definition: Solve.h:75
Eigen::Solve::rhs
const EIGEN_DEVICE_FUNC RhsType & rhs() const
Definition: Solve.h:76
Eigen::internal::AssignmentKind< SparseShape, SparseTriangularShape >::Kind
Sparse2Sparse Kind
Definition: SparseAssign.h:65
Eigen::internal::Sparse2Sparse
Definition: SparseAssign.h:61
Eigen::ReturnByValue::evalTo
EIGEN_DEVICE_FUNC void evalTo(Dest &dst) const
Definition: ReturnByValue.h:61
Eigen::internal::Assignment< DstXprType, SrcXprType, Functor, Diagonal2Sparse >::StorageIndex
DstXprType::StorageIndex StorageIndex
Definition: SparseAssign.h:180
Eigen::internal::Assignment< DstXprType, SrcXprType, Functor, Diagonal2Sparse >::Scalar
DstXprType::Scalar Scalar
Definition: SparseAssign.h:181
Eigen::SparseMatrix::valuePtr
const Scalar * valuePtr() const
Definition: SparseMatrix.h:148
Eigen::internal::Sparse2Dense
Definition: SparseAssign.h:62
Eigen::internal::Assignment< DstXprType, SrcXprType, Functor, Diagonal2Sparse >::run
static void run(DstXprType &dst, const SrcXprType &src, const internal::add_assign_op< typename DstXprType::Scalar, typename SrcXprType::Scalar > &)
Definition: SparseAssign.h:206
Eigen::internal::Assignment< DstXprType, SrcXprType, Functor, Diagonal2Sparse >::ArrayXI
Array< StorageIndex, Dynamic, 1 > ArrayXI
Definition: SparseAssign.h:182
Eigen::SparseMatrix::innerIndexPtr
const StorageIndex * innerIndexPtr() const
Definition: SparseMatrix.h:157
Eigen::internal::storage_kind_to_shape< Sparse >::Shape
SparseShape Shape
Definition: SparseAssign.h:58
Eigen::internal::AssignmentKind< SparseShape, SparseShape >::Kind
Sparse2Sparse Kind
Definition: SparseAssign.h:64
Eigen::SparseShape
Definition: Constants.h:521
Eigen::internal::Diagonal2Sparse
Definition: SparseAssign.h:173
Eigen::internal::Assignment< DstXprType, SrcXprType, Functor, Sparse2Dense >::run
static void run(DstXprType &dst, const SrcXprType &src, const Functor &func)
Definition: SparseAssign.h:140
Eigen::internal::Assignment< DstXprType, SrcXprType, Functor, Diagonal2Sparse >::run
static void run(DstXprType &dst, const SrcXprType &src, const internal::sub_assign_op< typename DstXprType::Scalar, typename SrcXprType::Scalar > &)
Definition: SparseAssign.h:209
Eigen::internal::IteratorBased
Definition: Constants.h:529
Eigen::internal::Assignment< DstXprType, SrcXprType, Functor, Sparse2Sparse >::run
static void run(DstXprType &dst, const SrcXprType &src, const internal::assign_op< typename DstXprType::Scalar, typename SrcXprType::Scalar > &)
Definition: SparseAssign.h:130
Eigen::internal::evaluator
Definition: CoreEvaluators.h:90
Eigen::internal::storage_kind_to_evaluator_kind
Definition: CoreEvaluators.h:23
Eigen::Map
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:94
Eigen::Solve::rows
EIGEN_DEVICE_FUNC Index rows() const
Definition: Solve.h:72
Eigen::SparseMatrix::outerIndexPtr
const StorageIndex * outerIndexPtr() const
Definition: SparseMatrix.h:166
Eigen::internal::assign_op
Definition: AssignmentFunctors.h:21
Eigen::internal::resize_if_allowed
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void resize_if_allowed(DstXprType &dst, const SrcXprType &src, const Functor &)
Definition: AssignEvaluator.h:706
Eigen::Solve
Pseudo expression representing a solving operation.
Definition: Solve.h:62
Eigen::internal::traits
Definition: ForwardDeclarations.h:17
Eigen::internal::sub_assign_op
Definition: AssignmentFunctors.h:67
Eigen::TensorSycl::run
void run(Expr &expr, Dev &dev)
Definition: TensorSyclRun.h:47
Eigen::internal::Assignment
Definition: AssignEvaluator.h:770
Eigen::SparseMatrixBase
Base class of any sparse matrices or sparse expressions.
Definition: ForwardDeclarations.h:281
Eigen::SparseMatrixBase::derived
const Derived & derived() const
Definition: SparseMatrixBase.h:138
Eigen::internal::AssignmentKind< SparseShape, DiagonalShape >::Kind
Diagonal2Sparse Kind
Definition: SparseAssign.h:175
utility::tuple::size
static constexpr size_t size(Tuple< Args... > &)
Provides access to the number of elements in a tuple as a compile-time constant expression.
Definition: TensorSyclTuple.h:143
Eigen::internal::is_same
Definition: Meta.h:63
Eigen::internal::add_assign_op
Definition: AssignmentFunctors.h:46
Eigen::OuterRandomAccessPattern
const int OuterRandomAccessPattern
Definition: SparseUtil.h:49
internal
Definition: BandTriangularSolver.h:13
Eigen::internal::AssignmentKind< DenseShape, SparseShape >::Kind
Sparse2Dense Kind
Definition: SparseAssign.h:66
Eigen::SparseMatrix::rows
Index rows() const
Definition: SparseMatrix.h:136
max
#define max(a, b)
Definition: datatypes.h:20
Eigen::SparseMatrix::makeCompressed
void makeCompressed()
Definition: SparseMatrix.h:464
Eigen::internal::Assignment< DstXprType, SrcXprType, Functor, Diagonal2Sparse >::run
static void run(SparseMatrixBase< DstDerived > &dst, const SrcXprType &src, const internal::assign_op< typename DstXprType::Scalar, typename SrcXprType::Scalar > &)
Definition: SparseAssign.h:201
Eigen::SparseMatrix::resizeNonZeros
void resizeNonZeros(Index size)
Definition: SparseMatrix.h:644
Eigen::internal::AssignmentKind
Definition: AssignEvaluator.h:757
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
Eigen::DiagonalShape
Definition: Constants.h:515


control_box_rst
Author(s): Christoph Rösmann
autogenerated on Wed Mar 2 2022 00:06:15