2014-06-20 21:42:13 +08:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
|
|
|
|
// for linear algebra.
|
|
|
|
|
//
|
|
|
|
|
// Copyright (C) 2008-2014 Gael Guennebaud <gael.guennebaud@inria.fr>
|
|
|
|
|
//
|
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla
|
|
|
|
|
// Public License v. 2.0. If a copy of the MPL was not distributed
|
|
|
|
|
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
|
|
#ifndef EIGEN_SPARSEASSIGN_H
|
|
|
|
|
#define EIGEN_SPARSEASSIGN_H
|
|
|
|
|
|
|
|
|
|
namespace Eigen {
|
|
|
|
|
|
|
|
|
|
template<typename Derived>
|
|
|
|
|
template<typename OtherDerived>
|
|
|
|
|
Derived& SparseMatrixBase<Derived>::operator=(const EigenBase<OtherDerived> &other)
|
|
|
|
|
{
|
2015-06-24 23:54:09 +08:00
|
|
|
internal::call_assignment_no_alias(derived(), other.derived());
|
2014-06-20 21:42:13 +08:00
|
|
|
return derived();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename Derived>
|
|
|
|
|
template<typename OtherDerived>
|
|
|
|
|
Derived& SparseMatrixBase<Derived>::operator=(const ReturnByValue<OtherDerived>& other)
|
|
|
|
|
{
|
2014-07-22 15:32:40 +08:00
|
|
|
// TODO use the evaluator mechanism
|
2014-06-20 21:42:13 +08:00
|
|
|
other.evalTo(derived());
|
|
|
|
|
return derived();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename Derived>
|
|
|
|
|
template<typename OtherDerived>
|
|
|
|
|
inline Derived& SparseMatrixBase<Derived>::operator=(const SparseMatrixBase<OtherDerived>& other)
|
|
|
|
|
{
|
2015-09-02 04:31:30 +08:00
|
|
|
// by default sparse evaluation do not alias, so we can safely bypass the generic call_assignment routine
|
Relax mixing-type constraints for binary coefficient-wise operators:
- Replace internal::scalar_product_traits<A,B> by Eigen::ScalarBinaryOpTraits<A,B,OP>
- Remove the "functor_is_product_like" helper (was pretty ugly)
- Currently, OP is not used, but it is available to the user for fine grained tuning
- Currently, only the following operators have been generalized: *,/,+,-,=,*=,/=,+=,-=
- TODO: generalize all other binray operators (comparisons,pow,etc.)
- TODO: handle "scalar op array" operators (currently only * is handled)
- TODO: move the handling of the "void" scalar type to ScalarBinaryOpTraits
2016-06-06 21:11:41 +08:00
|
|
|
internal::Assignment<Derived,OtherDerived,internal::assign_op<Scalar,typename OtherDerived::Scalar> >
|
|
|
|
|
::run(derived(), other.derived(), internal::assign_op<Scalar,typename OtherDerived::Scalar>());
|
2014-06-20 21:42:13 +08:00
|
|
|
return derived();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename Derived>
|
|
|
|
|
inline Derived& SparseMatrixBase<Derived>::operator=(const Derived& other)
|
|
|
|
|
{
|
|
|
|
|
internal::call_assignment_no_alias(derived(), other.derived());
|
|
|
|
|
return derived();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
struct storage_kind_to_evaluator_kind<Sparse> {
|
|
|
|
|
typedef IteratorBased Kind;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
struct storage_kind_to_shape<Sparse> {
|
|
|
|
|
typedef SparseShape Shape;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct Sparse2Sparse {};
|
2014-06-25 23:23:52 +08:00
|
|
|
struct Sparse2Dense {};
|
2014-06-20 21:42:13 +08:00
|
|
|
|
2014-08-01 22:23:30 +08:00
|
|
|
template<> struct AssignmentKind<SparseShape, SparseShape> { typedef Sparse2Sparse Kind; };
|
|
|
|
|
template<> struct AssignmentKind<SparseShape, SparseTriangularShape> { typedef Sparse2Sparse Kind; };
|
|
|
|
|
template<> struct AssignmentKind<DenseShape, SparseShape> { typedef Sparse2Dense Kind; };
|
2015-11-05 00:02:32 +08:00
|
|
|
template<> struct AssignmentKind<DenseShape, SparseTriangularShape> { typedef Sparse2Dense Kind; };
|
2014-06-20 21:42:13 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
template<typename DstXprType, typename SrcXprType>
|
|
|
|
|
void assign_sparse_to_sparse(DstXprType &dst, const SrcXprType &src)
|
|
|
|
|
{
|
|
|
|
|
typedef typename DstXprType::Scalar Scalar;
|
2015-09-03 03:38:40 +08:00
|
|
|
typedef internal::evaluator<DstXprType> DstEvaluatorType;
|
|
|
|
|
typedef internal::evaluator<SrcXprType> SrcEvaluatorType;
|
2014-06-20 21:42:13 +08:00
|
|
|
|
|
|
|
|
SrcEvaluatorType srcEvaluator(src);
|
2014-07-01 17:48:49 +08:00
|
|
|
|
2014-06-20 21:42:13 +08:00
|
|
|
const bool transpose = (DstEvaluatorType::Flags & RowMajorBit) != (SrcEvaluatorType::Flags & RowMajorBit);
|
2014-07-19 20:55:56 +08:00
|
|
|
const Index outerEvaluationSize = (SrcEvaluatorType::Flags&RowMajorBit) ? src.rows() : src.cols();
|
2014-06-20 21:42:13 +08:00
|
|
|
if ((!transpose) && src.isRValue())
|
|
|
|
|
{
|
|
|
|
|
// eval without temporary
|
|
|
|
|
dst.resize(src.rows(), src.cols());
|
|
|
|
|
dst.setZero();
|
|
|
|
|
dst.reserve((std::max)(src.rows(),src.cols())*2);
|
2014-07-19 20:55:56 +08:00
|
|
|
for (Index j=0; j<outerEvaluationSize; ++j)
|
2014-06-20 21:42:13 +08:00
|
|
|
{
|
|
|
|
|
dst.startVec(j);
|
|
|
|
|
for (typename SrcEvaluatorType::InnerIterator it(srcEvaluator, j); it; ++it)
|
|
|
|
|
{
|
|
|
|
|
Scalar v = it.value();
|
|
|
|
|
dst.insertBackByOuterInner(j,it.index()) = v;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
dst.finalize();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// eval through a temporary
|
|
|
|
|
eigen_assert(( ((internal::traits<DstXprType>::SupportedAccessPatterns & OuterRandomAccessPattern)==OuterRandomAccessPattern) ||
|
|
|
|
|
(!((DstEvaluatorType::Flags & RowMajorBit) != (SrcEvaluatorType::Flags & RowMajorBit)))) &&
|
|
|
|
|
"the transpose operation is supposed to be handled in SparseMatrix::operator=");
|
|
|
|
|
|
|
|
|
|
enum { Flip = (DstEvaluatorType::Flags & RowMajorBit) != (SrcEvaluatorType::Flags & RowMajorBit) };
|
|
|
|
|
|
2014-07-19 20:55:56 +08:00
|
|
|
|
2014-06-20 21:42:13 +08:00
|
|
|
DstXprType temp(src.rows(), src.cols());
|
|
|
|
|
|
|
|
|
|
temp.reserve((std::max)(src.rows(),src.cols())*2);
|
2014-07-19 20:55:56 +08:00
|
|
|
for (Index j=0; j<outerEvaluationSize; ++j)
|
2014-06-20 21:42:13 +08:00
|
|
|
{
|
|
|
|
|
temp.startVec(j);
|
2014-06-27 21:53:51 +08:00
|
|
|
for (typename SrcEvaluatorType::InnerIterator it(srcEvaluator, j); it; ++it)
|
2014-06-20 21:42:13 +08:00
|
|
|
{
|
|
|
|
|
Scalar v = it.value();
|
|
|
|
|
temp.insertBackByOuterInner(Flip?it.index():j,Flip?j:it.index()) = v;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
temp.finalize();
|
|
|
|
|
|
|
|
|
|
dst = temp.markAsRValue();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Generic Sparse to Sparse assignment
|
2016-07-04 17:02:00 +08:00
|
|
|
template< typename DstXprType, typename SrcXprType, typename Functor>
|
|
|
|
|
struct Assignment<DstXprType, SrcXprType, Functor, Sparse2Sparse>
|
2014-06-20 21:42:13 +08:00
|
|
|
{
|
Relax mixing-type constraints for binary coefficient-wise operators:
- Replace internal::scalar_product_traits<A,B> by Eigen::ScalarBinaryOpTraits<A,B,OP>
- Remove the "functor_is_product_like" helper (was pretty ugly)
- Currently, OP is not used, but it is available to the user for fine grained tuning
- Currently, only the following operators have been generalized: *,/,+,-,=,*=,/=,+=,-=
- TODO: generalize all other binray operators (comparisons,pow,etc.)
- TODO: handle "scalar op array" operators (currently only * is handled)
- TODO: move the handling of the "void" scalar type to ScalarBinaryOpTraits
2016-06-06 21:11:41 +08:00
|
|
|
static void run(DstXprType &dst, const SrcXprType &src, const internal::assign_op<typename DstXprType::Scalar,typename SrcXprType::Scalar> &/*func*/)
|
2014-06-20 21:42:13 +08:00
|
|
|
{
|
|
|
|
|
assign_sparse_to_sparse(dst.derived(), src.derived());
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2015-10-30 17:57:11 +08:00
|
|
|
// Generic Sparse to Dense assignment
|
2016-07-04 17:02:00 +08:00
|
|
|
template< typename DstXprType, typename SrcXprType, typename Functor>
|
|
|
|
|
struct Assignment<DstXprType, SrcXprType, Functor, Sparse2Dense>
|
2014-06-25 23:23:52 +08:00
|
|
|
{
|
2014-08-01 22:23:30 +08:00
|
|
|
static void run(DstXprType &dst, const SrcXprType &src, const Functor &func)
|
|
|
|
|
{
|
Relax mixing-type constraints for binary coefficient-wise operators:
- Replace internal::scalar_product_traits<A,B> by Eigen::ScalarBinaryOpTraits<A,B,OP>
- Remove the "functor_is_product_like" helper (was pretty ugly)
- Currently, OP is not used, but it is available to the user for fine grained tuning
- Currently, only the following operators have been generalized: *,/,+,-,=,*=,/=,+=,-=
- TODO: generalize all other binray operators (comparisons,pow,etc.)
- TODO: handle "scalar op array" operators (currently only * is handled)
- TODO: move the handling of the "void" scalar type to ScalarBinaryOpTraits
2016-06-06 21:11:41 +08:00
|
|
|
if(internal::is_same<Functor,internal::assign_op<typename DstXprType::Scalar,typename SrcXprType::Scalar> >::value)
|
2015-10-29 20:16:15 +08:00
|
|
|
dst.setZero();
|
2014-06-25 23:23:52 +08:00
|
|
|
|
2015-09-03 03:38:40 +08:00
|
|
|
internal::evaluator<SrcXprType> srcEval(src);
|
2017-01-24 05:06:08 +08:00
|
|
|
resize_if_allowed(dst, src, func);
|
2015-09-03 03:38:40 +08:00
|
|
|
internal::evaluator<DstXprType> dstEval(dst);
|
2016-10-27 04:50:41 +08:00
|
|
|
|
2014-07-19 20:55:56 +08:00
|
|
|
const Index outerEvaluationSize = (internal::evaluator<SrcXprType>::Flags&RowMajorBit) ? src.rows() : src.cols();
|
|
|
|
|
for (Index j=0; j<outerEvaluationSize; ++j)
|
2014-06-25 23:23:52 +08:00
|
|
|
for (typename internal::evaluator<SrcXprType>::InnerIterator i(srcEval,j); i; ++i)
|
2015-10-29 20:16:15 +08:00
|
|
|
func.assignCoeff(dstEval.coeffRef(i.row(),i.col()), i.value());
|
2014-06-25 23:23:52 +08:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2014-09-01 20:50:59 +08:00
|
|
|
// Specialization for "dst = dec.solve(rhs)"
|
|
|
|
|
// NOTE we need to specialize it for Sparse2Sparse to avoid ambiguous specialization error
|
|
|
|
|
template<typename DstXprType, typename DecType, typename RhsType, typename Scalar>
|
2016-07-04 17:02:00 +08:00
|
|
|
struct Assignment<DstXprType, Solve<DecType,RhsType>, internal::assign_op<Scalar,Scalar>, Sparse2Sparse>
|
2014-09-01 20:50:59 +08:00
|
|
|
{
|
|
|
|
|
typedef Solve<DecType,RhsType> SrcXprType;
|
Relax mixing-type constraints for binary coefficient-wise operators:
- Replace internal::scalar_product_traits<A,B> by Eigen::ScalarBinaryOpTraits<A,B,OP>
- Remove the "functor_is_product_like" helper (was pretty ugly)
- Currently, OP is not used, but it is available to the user for fine grained tuning
- Currently, only the following operators have been generalized: *,/,+,-,=,*=,/=,+=,-=
- TODO: generalize all other binray operators (comparisons,pow,etc.)
- TODO: handle "scalar op array" operators (currently only * is handled)
- TODO: move the handling of the "void" scalar type to ScalarBinaryOpTraits
2016-06-06 21:11:41 +08:00
|
|
|
static void run(DstXprType &dst, const SrcXprType &src, const internal::assign_op<Scalar,Scalar> &)
|
2014-09-01 20:50:59 +08:00
|
|
|
{
|
2016-10-27 04:50:41 +08:00
|
|
|
Index dstRows = src.rows();
|
|
|
|
|
Index dstCols = src.cols();
|
|
|
|
|
if((dst.rows()!=dstRows) || (dst.cols()!=dstCols))
|
|
|
|
|
dst.resize(dstRows, dstCols);
|
|
|
|
|
|
2014-09-01 20:50:59 +08:00
|
|
|
src.dec()._solve_impl(src.rhs(), dst);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2015-06-24 23:55:00 +08:00
|
|
|
struct Diagonal2Sparse {};
|
|
|
|
|
|
|
|
|
|
template<> struct AssignmentKind<SparseShape,DiagonalShape> { typedef Diagonal2Sparse Kind; };
|
|
|
|
|
|
2016-07-04 17:02:00 +08:00
|
|
|
template< typename DstXprType, typename SrcXprType, typename Functor>
|
|
|
|
|
struct Assignment<DstXprType, SrcXprType, Functor, Diagonal2Sparse>
|
2015-06-24 23:55:00 +08:00
|
|
|
{
|
|
|
|
|
typedef typename DstXprType::StorageIndex StorageIndex;
|
2016-07-04 17:02:00 +08:00
|
|
|
typedef typename DstXprType::Scalar Scalar;
|
2015-06-24 23:55:00 +08:00
|
|
|
typedef Array<StorageIndex,Dynamic,1> ArrayXI;
|
|
|
|
|
typedef Array<Scalar,Dynamic,1> ArrayXS;
|
|
|
|
|
template<int Options>
|
Relax mixing-type constraints for binary coefficient-wise operators:
- Replace internal::scalar_product_traits<A,B> by Eigen::ScalarBinaryOpTraits<A,B,OP>
- Remove the "functor_is_product_like" helper (was pretty ugly)
- Currently, OP is not used, but it is available to the user for fine grained tuning
- Currently, only the following operators have been generalized: *,/,+,-,=,*=,/=,+=,-=
- TODO: generalize all other binray operators (comparisons,pow,etc.)
- TODO: handle "scalar op array" operators (currently only * is handled)
- TODO: move the handling of the "void" scalar type to ScalarBinaryOpTraits
2016-06-06 21:11:41 +08:00
|
|
|
static void run(SparseMatrix<Scalar,Options,StorageIndex> &dst, const SrcXprType &src, const internal::assign_op<typename DstXprType::Scalar,typename SrcXprType::Scalar> &/*func*/)
|
2015-06-24 23:55:00 +08:00
|
|
|
{
|
2016-10-27 04:50:41 +08:00
|
|
|
Index dstRows = src.rows();
|
|
|
|
|
Index dstCols = src.cols();
|
|
|
|
|
if((dst.rows()!=dstRows) || (dst.cols()!=dstCols))
|
|
|
|
|
dst.resize(dstRows, dstCols);
|
|
|
|
|
|
2015-06-24 23:55:00 +08:00
|
|
|
Index size = src.diagonal().size();
|
|
|
|
|
dst.makeCompressed();
|
|
|
|
|
dst.resizeNonZeros(size);
|
|
|
|
|
Map<ArrayXI>(dst.innerIndexPtr(), size).setLinSpaced(0,StorageIndex(size)-1);
|
|
|
|
|
Map<ArrayXI>(dst.outerIndexPtr(), size+1).setLinSpaced(0,StorageIndex(size));
|
|
|
|
|
Map<ArrayXS>(dst.valuePtr(), size) = src.diagonal();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename DstDerived>
|
Relax mixing-type constraints for binary coefficient-wise operators:
- Replace internal::scalar_product_traits<A,B> by Eigen::ScalarBinaryOpTraits<A,B,OP>
- Remove the "functor_is_product_like" helper (was pretty ugly)
- Currently, OP is not used, but it is available to the user for fine grained tuning
- Currently, only the following operators have been generalized: *,/,+,-,=,*=,/=,+=,-=
- TODO: generalize all other binray operators (comparisons,pow,etc.)
- TODO: handle "scalar op array" operators (currently only * is handled)
- TODO: move the handling of the "void" scalar type to ScalarBinaryOpTraits
2016-06-06 21:11:41 +08:00
|
|
|
static void run(SparseMatrixBase<DstDerived> &dst, const SrcXprType &src, const internal::assign_op<typename DstXprType::Scalar,typename SrcXprType::Scalar> &/*func*/)
|
2015-06-24 23:55:00 +08:00
|
|
|
{
|
|
|
|
|
dst.diagonal() = src.diagonal();
|
|
|
|
|
}
|
|
|
|
|
|
Relax mixing-type constraints for binary coefficient-wise operators:
- Replace internal::scalar_product_traits<A,B> by Eigen::ScalarBinaryOpTraits<A,B,OP>
- Remove the "functor_is_product_like" helper (was pretty ugly)
- Currently, OP is not used, but it is available to the user for fine grained tuning
- Currently, only the following operators have been generalized: *,/,+,-,=,*=,/=,+=,-=
- TODO: generalize all other binray operators (comparisons,pow,etc.)
- TODO: handle "scalar op array" operators (currently only * is handled)
- TODO: move the handling of the "void" scalar type to ScalarBinaryOpTraits
2016-06-06 21:11:41 +08:00
|
|
|
static void run(DstXprType &dst, const SrcXprType &src, const internal::add_assign_op<typename DstXprType::Scalar,typename SrcXprType::Scalar> &/*func*/)
|
2015-06-24 23:55:00 +08:00
|
|
|
{ dst.diagonal() += src.diagonal(); }
|
|
|
|
|
|
Relax mixing-type constraints for binary coefficient-wise operators:
- Replace internal::scalar_product_traits<A,B> by Eigen::ScalarBinaryOpTraits<A,B,OP>
- Remove the "functor_is_product_like" helper (was pretty ugly)
- Currently, OP is not used, but it is available to the user for fine grained tuning
- Currently, only the following operators have been generalized: *,/,+,-,=,*=,/=,+=,-=
- TODO: generalize all other binray operators (comparisons,pow,etc.)
- TODO: handle "scalar op array" operators (currently only * is handled)
- TODO: move the handling of the "void" scalar type to ScalarBinaryOpTraits
2016-06-06 21:11:41 +08:00
|
|
|
static void run(DstXprType &dst, const SrcXprType &src, const internal::sub_assign_op<typename DstXprType::Scalar,typename SrcXprType::Scalar> &/*func*/)
|
2015-06-24 23:55:00 +08:00
|
|
|
{ dst.diagonal() -= src.diagonal(); }
|
|
|
|
|
};
|
2014-06-20 21:42:13 +08:00
|
|
|
} // end namespace internal
|
|
|
|
|
|
|
|
|
|
} // end namespace Eigen
|
|
|
|
|
|
|
|
|
|
#endif // EIGEN_SPARSEASSIGN_H
|