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
const int OuterRandomAccessPattern
Definition: SparseUtil.h:49
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void call_assignment_no_alias(Dst &dst, const Src &src, const Func &func)
A versatible sparse matrix representation.
Definition: SparseMatrix.h:96
EIGEN_DEVICE_FUNC Index cols() const
Definition: Solve.h:73
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void resize_if_allowed(DstXprType &dst, const SrcXprType &src, const Functor &)
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:88
void resize(Index rows, Index cols)
Definition: SparseMatrix.h:621
static void run(DstXprType &dst, const SrcXprType &src, const internal::assign_op< typename DstXprType::Scalar, typename SrcXprType::Scalar > &)
Definition: SparseAssign.h:130
Definition: LDLT.h:16
static constexpr size_t size(Tuple< Args... > &)
Provides access to the number of elements in a tuple as a compile-time constant expression.
EIGEN_DEVICE_FUNC void evalTo(Dest &dst) const
Definition: ReturnByValue.h:61
Derived & operator=(const EigenBase< OtherDerived > &other)
Definition: SparseAssign.h:17
static void run(DstXprType &dst, const SrcXprType &src, const internal::add_assign_op< typename DstXprType::Scalar, typename SrcXprType::Scalar > &)
Definition: SparseAssign.h:206
const Scalar * valuePtr() const
Definition: SparseMatrix.h:148
const unsigned int RowMajorBit
Definition: Constants.h:61
void resizeNonZeros(Index size)
Definition: SparseMatrix.h:644
Index rows() const
Definition: SparseMatrix.h:136
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half() max(const half &a, const half &b)
Definition: Half.h:438
EIGEN_DEVICE_FUNC Index rows() const
Definition: Solve.h:72
static void run(DstXprType &dst, const SrcXprType &src, const internal::sub_assign_op< typename DstXprType::Scalar, typename SrcXprType::Scalar > &)
Definition: SparseAssign.h:209
static void run(SparseMatrixBase< DstDerived > &dst, const SrcXprType &src, const internal::assign_op< typename DstXprType::Scalar, typename SrcXprType::Scalar > &)
Definition: SparseAssign.h:201
Base class of any sparse matrices or sparse expressions.
Index cols() const
Definition: SparseMatrix.h:138
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
#define eigen_assert(x)
Definition: Macros.h:577
static void run(DstXprType &dst, const SrcXprType &src, const Functor &func)
Definition: SparseAssign.h:140
EIGEN_DEVICE_FUNC const Decomposition & dec() const
Definition: Solve.h:75
void assign_sparse_to_sparse(DstXprType &dst, const SrcXprType &src)
Definition: SparseAssign.h:71
const StorageIndex * outerIndexPtr() const
Definition: SparseMatrix.h:166
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_DEVICE_FUNC const RhsType & rhs() const
Definition: Solve.h:76
const Derived & derived() const
General-purpose arrays with easy API for coefficient-wise operations.
Definition: Array.h:45
Pseudo expression representing a solving operation.
Definition: Solve.h:62
const StorageIndex * innerIndexPtr() const
Definition: SparseMatrix.h:157
static void run(DstXprType &dst, const SrcXprType &src, const internal::assign_op< Scalar, Scalar > &)
Definition: SparseAssign.h:162
void run(Expr &expr, Dev &dev)
Definition: TensorSyclRun.h:33
EIGEN_DEVICE_FUNC Derived & derived()
Definition: EigenBase.h:45


hebiros
Author(s): Xavier Artache , Matthew Tesch
autogenerated on Thu Sep 3 2020 04:08:50