10 #ifndef EIGEN_SPARSEASSIGN_H
11 #define EIGEN_SPARSEASSIGN_H
17 template<
typename Derived>
18 template<
typename OtherDerived>
25 template<
typename Derived>
26 template<
typename OtherDerived>
34 template<
typename Derived>
35 template<
typename OtherDerived>
39 internal::Assignment<Derived,OtherDerived,internal::assign_op<Scalar,typename OtherDerived::Scalar> >
40 ::run(derived(), other.
derived(), internal::assign_op<Scalar,typename OtherDerived::Scalar>());
44 template<
typename Derived>
54 struct storage_kind_to_evaluator_kind<
Sparse> {
55 typedef IteratorBased Kind;
59 struct storage_kind_to_shape<
Sparse> {
63 struct Sparse2Sparse {};
64 struct Sparse2Dense {};
66 template<>
struct AssignmentKind<SparseShape, SparseShape> {
typedef Sparse2Sparse Kind; };
67 template<>
struct AssignmentKind<SparseShape, SparseTriangularShape> {
typedef Sparse2Sparse Kind; };
68 template<>
struct AssignmentKind<DenseShape, SparseShape> {
typedef Sparse2Dense Kind; };
69 template<>
struct AssignmentKind<DenseShape, SparseTriangularShape> {
typedef Sparse2Dense Kind; };
72 template<
typename DstXprType,
typename SrcXprType>
75 typedef typename DstXprType::Scalar Scalar;
76 typedef internal::evaluator<DstXprType> DstEvaluatorType;
77 typedef internal::evaluator<SrcXprType> SrcEvaluatorType;
79 SrcEvaluatorType srcEvaluator(src);
81 constexpr
bool transpose = (DstEvaluatorType::Flags &
RowMajorBit) != (SrcEvaluatorType::Flags &
RowMajorBit);
82 const Index outerEvaluationSize = (SrcEvaluatorType::Flags&
RowMajorBit) ? src.rows() : src.cols();
84 Index reserveSize = 0;
85 for (
Index j = 0;
j < outerEvaluationSize; ++
j)
86 for (
typename SrcEvaluatorType::InnerIterator it(srcEvaluator,
j); it; ++it)
89 if ((!transpose) && src.isRValue())
92 dst.resize(src.rows(), src.cols());
94 dst.reserve(reserveSize);
95 for (
Index j=0;
j<outerEvaluationSize; ++
j)
98 for (
typename SrcEvaluatorType::InnerIterator it(srcEvaluator,
j); it; ++it)
100 Scalar
v = it.value();
101 dst.insertBackByOuterInner(
j,it.index()) =
v;
111 "the transpose operation is supposed to be handled in SparseMatrix::operator=");
116 DstXprType temp(src.rows(), src.cols());
118 temp.reserve(reserveSize);
119 for (
Index j=0;
j<outerEvaluationSize; ++
j)
122 for (
typename SrcEvaluatorType::InnerIterator it(srcEvaluator,
j); it; ++it)
124 Scalar
v = it.value();
125 temp.insertBackByOuterInner(Flip?it.index():
j,Flip?
j:it.index()) =
v;
130 dst = temp.markAsRValue();
135 template<
typename DstXprType,
typename SrcXprType,
typename Functor>
136 struct Assignment<DstXprType, SrcXprType, Functor, Sparse2Sparse>
138 static void run(DstXprType &dst,
const SrcXprType &src,
const internal::assign_op<typename DstXprType::Scalar,typename SrcXprType::Scalar> &)
145 template<
typename DstXprType,
typename SrcXprType,
typename Functor,
typename Weak>
146 struct Assignment<DstXprType, SrcXprType, Functor, Sparse2Dense, Weak>
148 static void run(DstXprType &dst,
const SrcXprType &src,
const Functor &func)
150 if(internal::is_same<Functor,internal::assign_op<typename DstXprType::Scalar,typename SrcXprType::Scalar> >::value)
153 internal::evaluator<SrcXprType> srcEval(src);
155 internal::evaluator<DstXprType> dstEval(dst);
157 const Index outerEvaluationSize = (internal::evaluator<SrcXprType>::Flags&
RowMajorBit) ? src.rows() : src.cols();
158 for (
Index j=0;
j<outerEvaluationSize; ++
j)
159 for (
typename internal::evaluator<SrcXprType>::InnerIterator
i(srcEval,
j);
i; ++
i)
160 func.assignCoeff(dstEval.coeffRef(
i.row(),
i.col()),
i.value());
165 template<
typename DstXprType,
typename Func1,
typename Func2>
166 struct assignment_from_dense_op_sparse
168 template<
typename SrcXprType,
typename InitialFunc>
170 void run(DstXprType &dst,
const SrcXprType &src,
const InitialFunc& )
172 #ifdef EIGEN_SPARSE_ASSIGNMENT_FROM_DENSE_OP_SPARSE_PLUGIN
173 EIGEN_SPARSE_ASSIGNMENT_FROM_DENSE_OP_SPARSE_PLUGIN
181 template<
typename Lhs,
typename Rhs,
typename Scalar>
183 std::enable_if_t<internal::is_same<typename internal::evaluator_traits<Rhs>::Shape,DenseShape>::value>
184 run(DstXprType &dst,
const CwiseBinaryOp<internal::scalar_sum_op<Scalar,Scalar>,
const Lhs,
const Rhs> &src,
185 const internal::assign_op<typename DstXprType::Scalar,Scalar>& )
187 #ifdef EIGEN_SPARSE_ASSIGNMENT_FROM_SPARSE_ADD_DENSE_PLUGIN
188 EIGEN_SPARSE_ASSIGNMENT_FROM_SPARSE_ADD_DENSE_PLUGIN
197 template<
typename Lhs,
typename Rhs,
typename Scalar>
199 std::enable_if_t<internal::is_same<typename internal::evaluator_traits<Rhs>::Shape,DenseShape>::value>
200 run(DstXprType &dst,
const CwiseBinaryOp<internal::scalar_difference_op<Scalar,Scalar>,
const Lhs,
const Rhs> &src,
201 const internal::assign_op<typename DstXprType::Scalar,Scalar>& )
203 #ifdef EIGEN_SPARSE_ASSIGNMENT_FROM_SPARSE_SUB_DENSE_PLUGIN
204 EIGEN_SPARSE_ASSIGNMENT_FROM_SPARSE_SUB_DENSE_PLUGIN
213 #define EIGEN_CATCH_ASSIGN_DENSE_OP_SPARSE(ASSIGN_OP,BINOP,ASSIGN_OP2) \
214 template< typename DstXprType, typename Lhs, typename Rhs, typename Scalar> \
215 struct Assignment<DstXprType, CwiseBinaryOp<internal::BINOP<Scalar,Scalar>, const Lhs, const Rhs>, internal::ASSIGN_OP<typename DstXprType::Scalar,Scalar>, \
217 std::enable_if_t< internal::is_same<typename internal::evaluator_traits<Lhs>::Shape,DenseShape>::value \
218 || internal::is_same<typename internal::evaluator_traits<Rhs>::Shape,DenseShape>::value>> \
219 : assignment_from_dense_op_sparse<DstXprType, internal::ASSIGN_OP<typename DstXprType::Scalar,typename Lhs::Scalar>, internal::ASSIGN_OP2<typename DstXprType::Scalar,typename Rhs::Scalar> > \
233 template<
typename DstXprType,
typename DecType,
typename RhsType,
typename Scalar>
234 struct Assignment<DstXprType,
Solve<DecType,RhsType>,
internal::assign_op<Scalar,Scalar>, Sparse2Sparse>
237 static void run(DstXprType &dst,
const SrcXprType &src,
const internal::assign_op<Scalar,Scalar> &)
240 Index dstCols = src.cols();
241 if((dst.rows()!=dstRows) || (dst.cols()!=dstCols))
242 dst.resize(dstRows, dstCols);
244 src.dec()._solve_impl(src.rhs(), dst);
248 struct Diagonal2Sparse {};
250 template<>
struct AssignmentKind<SparseShape,DiagonalShape> {
typedef Diagonal2Sparse Kind; };
252 template<
typename DstXprType,
typename SrcXprType,
typename Functor>
253 struct Assignment<DstXprType, SrcXprType, Functor, Diagonal2Sparse>
255 typedef typename DstXprType::StorageIndex StorageIndex;
256 typedef typename DstXprType::Scalar Scalar;
258 template<
int Options,
typename AssignFunc>
259 static void run(SparseMatrix<Scalar,Options,StorageIndex> &dst,
const SrcXprType &src,
const AssignFunc &func)
260 { dst.assignDiagonal(src.diagonal(), func); }
262 template<
typename DstDerived>
263 static void run(SparseMatrixBase<DstDerived> &dst,
const SrcXprType &src,
const internal::assign_op<typename DstXprType::Scalar,typename SrcXprType::Scalar> &)
264 { dst.derived().diagonal() = src.diagonal(); }
266 template<
typename DstDerived>
267 static void run(SparseMatrixBase<DstDerived> &dst,
const SrcXprType &src,
const internal::add_assign_op<typename DstXprType::Scalar,typename SrcXprType::Scalar> &)
268 { dst.derived().diagonal() += src.diagonal(); }
270 template<
typename DstDerived>
271 static void run(SparseMatrixBase<DstDerived> &dst,
const SrcXprType &src,
const internal::sub_assign_op<typename DstXprType::Scalar,typename SrcXprType::Scalar> &)
272 { dst.derived().diagonal() -= src.diagonal(); }
Array< int, Dynamic, 1 > v
#define EIGEN_DEVICE_FUNC
void evalTo(Dest &dst) const
Pseudo expression representing a solving operation.
EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Base class of any sparse matrices or sparse expressions.
Derived & operator=(const EigenBase< OtherDerived > &other)
const unsigned int RowMajorBit
void resize_if_allowed(DstXprType &dst, const SrcXprType &src, const Functor &)
void assign_sparse_to_sparse(DstXprType &dst, const SrcXprType &src)
EIGEN_CONSTEXPR void call_assignment_no_alias(Dst &dst, const Src &src, const Func &func)
EIGEN_CATCH_ASSIGN_DENSE_OP_SPARSE(assign_op, scalar_sum_op, add_assign_op)
const int OuterRandomAccessPattern
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.