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::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
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, typename Weak>
138 struct Assignment<DstXprType, SrcXprType, Functor, Sparse2Dense, Weak>
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 dense ?= dense +/- sparse and dense ?= sparse +/- dense
157 template<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
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
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
197  #endif
198 
199  // Apply the dense matrix first, then the sparse one.
200  call_assignment_no_alias(dst, -src.rhs(), Func1());
202  }
203 };
204 
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 
214 EIGEN_CATCH_ASSIGN_DENSE_OP_SPARSE(assign_op, scalar_sum_op,add_assign_op);
215 EIGEN_CATCH_ASSIGN_DENSE_OP_SPARSE(add_assign_op,scalar_sum_op,add_assign_op);
216 EIGEN_CATCH_ASSIGN_DENSE_OP_SPARSE(sub_assign_op,scalar_sum_op,sub_assign_op);
217 
218 EIGEN_CATCH_ASSIGN_DENSE_OP_SPARSE(assign_op, scalar_difference_op,sub_assign_op);
219 EIGEN_CATCH_ASSIGN_DENSE_OP_SPARSE(add_assign_op,scalar_difference_op,sub_assign_op);
220 EIGEN_CATCH_ASSIGN_DENSE_OP_SPARSE(sub_assign_op,scalar_difference_op,add_assign_op);
221 
222 
223 // Specialization for "dst = dec.solve(rhs)"
224 // NOTE we need to specialize it for Sparse2Sparse to avoid ambiguous specialization error
225 template<typename DstXprType, typename DecType, typename RhsType, typename Scalar>
226 struct 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 
240 struct Diagonal2Sparse {};
241 
243 
244 template< typename DstXprType, typename SrcXprType, typename Functor>
245 struct 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
Eigen::internal::Assignment< DstXprType, Solve< DecType, RhsType >, internal::assign_op< Scalar, Scalar >, Sparse2Sparse >::SrcXprType
Solve< DecType, RhsType > SrcXprType
Definition: SparseAssign.h:228
Eigen::internal::Lhs
@ Lhs
Definition: TensorContractionMapper.h:19
EIGEN_DEVICE_FUNC
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:976
Eigen::SparseMatrix::assignDiagonal
void assignDiagonal(const DiagXpr diagXpr, const Func &assignFunc)
Definition: SparseMatrix.h:927
Eigen::internal::assignment_from_dense_op_sparse
Definition: SparseAssign.h:158
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:229
Eigen
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
Eigen::SparseMatrix
A versatible sparse matrix representation.
Definition: SparseMatrix.h:96
Eigen::ReturnByValue
Definition: ReturnByValue.h:50
Eigen::CwiseBinaryOp
Generic expression where a coefficient-wise binary operator is applied to two expressions.
Definition: CwiseBinaryOp.h:77
Eigen::internal::AssignmentKind< DenseShape, SparseTriangularShape >::Kind
Sparse2Dense Kind
Definition: SparseAssign.h:67
Eigen::DenseShape
Definition: Constants.h:528
Eigen::internal::SparseTriangularShape
Definition: SparseUtil.h:137
Eigen::Sparse
Definition: Constants.h:510
Eigen::EigenBase
Definition: EigenBase.h:29
eigen_assert
#define eigen_assert(x)
Definition: Macros.h:1037
Eigen::RowMajorBit
const unsigned int RowMajorBit
Definition: Constants.h:66
Eigen::internal::storage_kind_to_shape
Definition: CoreEvaluators.h:29
Eigen::internal::assign_sparse_to_sparse
void assign_sparse_to_sparse(DstXprType &dst, const SrcXprType &src)
Definition: SparseAssign.h:71
type
Definition: pytypes.h:1525
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
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:873
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::internal::Assignment< DstXprType, SrcXprType, Functor, Diagonal2Sparse >::StorageIndex
DstXprType::StorageIndex StorageIndex
Definition: SparseAssign.h:247
Eigen::internal::assignment_from_dense_op_sparse::run
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(DstXprType &dst, const SrcXprType &src, const InitialFunc &)
Definition: SparseAssign.h:162
Eigen::internal::Assignment< DstXprType, SrcXprType, Functor, Diagonal2Sparse >::Scalar
DstXprType::Scalar Scalar
Definition: SparseAssign.h:248
Functor
Definition: NonLinearOptimization.cpp:117
Eigen::internal::Sparse2Dense
Definition: SparseAssign.h:62
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
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_SPARSE_ASSIGNMENT_FROM_SPARSE_SUB_DENSE_PLUGIN
#define EIGEN_SPARSE_ASSIGNMENT_FROM_SPARSE_SUB_DENSE_PLUGIN
Definition: sparse_basic.cpp:19
Eigen::SparseShape
Definition: Constants.h:537
Eigen::internal::scalar_difference_op
Definition: BinaryFunctors.h:349
Eigen::internal::Diagonal2Sparse
Definition: SparseAssign.h:240
EIGEN_STRONG_INLINE
#define EIGEN_STRONG_INLINE
Definition: Macros.h:917
Eigen::internal::IteratorBased
Definition: Constants.h:545
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
gtsam.examples.DogLegOptimizerExample.run
def run(args)
Definition: DogLegOptimizerExample.py:21
Eigen::Solve::rows
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition: Solve.h:72
Eigen::internal::evaluator
Definition: CoreEvaluators.h:90
Eigen::internal::storage_kind_to_evaluator_kind
Definition: CoreEvaluators.h:23
Eigen::internal::assign_op
Definition: AssignmentFunctors.h:21
Eigen::internal::assignment_from_dense_op_sparse::run
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE internal::enable_if< internal::is_same< typename internal::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
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:750
Eigen::Solve
Pseudo expression representing a solving operation.
Definition: Solve.h:62
Eigen::internal::traits
Definition: ForwardDeclarations.h:17
Eigen::internal::Assignment< DstXprType, SrcXprType, Functor, Sparse2Dense, Weak >::run
static void run(DstXprType &dst, const SrcXprType &src, const Functor &func)
Definition: SparseAssign.h:140
Eigen::internal::Rhs
@ Rhs
Definition: TensorContractionMapper.h:18
Eigen::internal::Assignment< DstXprType, SrcXprType, Functor, Diagonal2Sparse >::run
static void run(SparseMatrixBase< DstDerived > &dst, const SrcXprType &src, const internal::sub_assign_op< typename DstXprType::Scalar, typename SrcXprType::Scalar > &)
Definition: SparseAssign.h:263
Eigen::internal::sub_assign_op
Definition: AssignmentFunctors.h:67
Eigen::internal::Assignment
Definition: AssignEvaluator.h:824
v
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
Eigen::SparseMatrixBase
Base class of any sparse matrices or sparse expressions.
Definition: ForwardDeclarations.h:301
Eigen::SparseMatrixBase::derived
const Derived & derived() const
Definition: SparseMatrixBase.h:143
Eigen::internal::AssignmentKind< SparseShape, DiagonalShape >::Kind
Diagonal2Sparse Kind
Definition: SparseAssign.h:242
Eigen::internal::Assignment< DstXprType, SrcXprType, Functor, Diagonal2Sparse >::run
static void run(SparseMatrix< Scalar, Options, StorageIndex > &dst, const SrcXprType &src, const AssignFunc &func)
Definition: SparseAssign.h:251
min
#define min(a, b)
Definition: datatypes.h:19
EIGEN_SPARSE_ASSIGNMENT_FROM_SPARSE_ADD_DENSE_PLUGIN
#define EIGEN_SPARSE_ASSIGNMENT_FROM_SPARSE_ADD_DENSE_PLUGIN
Definition: sparse_basic.cpp:18
Eigen::internal::is_same
Definition: Meta.h:148
Eigen::Solve::cols
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition: Solve.h:73
Eigen::internal::add_assign_op
Definition: AssignmentFunctors.h:46
Eigen::internal::scalar_sum_op
Definition: BinaryFunctors.h:32
Eigen::OuterRandomAccessPattern
const int OuterRandomAccessPattern
Definition: SparseUtil.h:49
internal
Definition: BandTriangularSolver.h:13
Eigen::internal::assignment_from_dense_op_sparse::run
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE internal::enable_if< internal::is_same< typename internal::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
Eigen::internal::AssignmentKind< DenseShape, SparseShape >::Kind
Sparse2Dense Kind
Definition: SparseAssign.h:66
Eigen::internal::enable_if
Definition: Meta.h:273
func
Definition: benchGeometry.cpp:23
Eigen::internal::Assignment< DstXprType, SrcXprType, Functor, Diagonal2Sparse >::run
static void run(SparseMatrixBase< DstDerived > &dst, const SrcXprType &src, const internal::add_assign_op< typename DstXprType::Scalar, typename SrcXprType::Scalar > &)
Definition: SparseAssign.h:259
max
#define max(a, b)
Definition: datatypes.h:20
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:255
Eigen::internal::EIGEN_CATCH_ASSIGN_DENSE_OP_SPARSE
EIGEN_CATCH_ASSIGN_DENSE_OP_SPARSE(assign_op, scalar_sum_op, add_assign_op)
test_callbacks.value
value
Definition: test_callbacks.py:160
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
pybind_wrapper_test_script.other
other
Definition: pybind_wrapper_test_script.py:42
Eigen::internal::AssignmentKind
Definition: AssignEvaluator.h:811
Scalar
SCALAR Scalar
Definition: bench_gemm.cpp:46
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
Eigen::DiagonalShape
Definition: Constants.h:531
EIGEN_SPARSE_ASSIGNMENT_FROM_DENSE_OP_SPARSE_PLUGIN
#define EIGEN_SPARSE_ASSIGNMENT_FROM_DENSE_OP_SPARSE_PLUGIN
Definition: sparse_basic.cpp:17


gtsam
Author(s):
autogenerated on Fri Nov 1 2024 03:35:33