10 #ifndef EIGEN_SPARSE_CWISE_BINARY_OP_H
11 #define EIGEN_SPARSE_CWISE_BINARY_OP_H
32 template<
typename BinaryOp,
typename Lhs,
typename Rhs>
33 class CwiseBinaryOpImpl<BinaryOp, Lhs, Rhs, Sparse>
34 :
public SparseMatrixBase<CwiseBinaryOp<BinaryOp, Lhs, Rhs> >
37 typedef CwiseBinaryOp<BinaryOp, Lhs, Rhs> Derived;
38 EIGEN_SPARSE_PUBLIC_INTERFACE(Derived)
41 typedef typename internal::traits<Lhs>::StorageKind LhsStorageKind;
42 typedef typename internal::traits<Rhs>::StorageKind RhsStorageKind;
44 (!internal::is_same<LhsStorageKind,RhsStorageKind>::value)
45 || ((Lhs::Flags&
RowMajorBit) == (Rhs::Flags&RowMajorBit))),
46 THE_STORAGE_ORDER_OF_BOTH_SIDES_MUST_MATCH);
52 template<
typename BinaryOp,
typename Lhs,
typename Rhs,
typename Derived,
53 typename _LhsStorageMode =
typename traits<Lhs>::StorageKind,
54 typename _RhsStorageMode =
typename traits<Rhs>::StorageKind>
55 class sparse_cwise_binary_op_inner_iterator_selector;
63 template<
typename BinaryOp,
typename Lhs,
typename Rhs>
64 struct binary_evaluator<CwiseBinaryOp<BinaryOp, Lhs, Rhs>, IteratorBased, IteratorBased>
65 : evaluator_base<CwiseBinaryOp<BinaryOp, Lhs, Rhs> >
68 typedef typename evaluator<Lhs>::InnerIterator LhsIterator;
69 typedef typename evaluator<Rhs>::InnerIterator RhsIterator;
70 typedef CwiseBinaryOp<BinaryOp, Lhs, Rhs> XprType;
71 typedef typename traits<XprType>::Scalar Scalar;
72 typedef typename XprType::StorageIndex StorageIndex;
75 class ReverseInnerIterator;
80 EIGEN_STRONG_INLINE InnerIterator(
const binary_evaluator& aEval, Index outer)
81 : m_lhsIter(aEval.m_lhsImpl,outer), m_rhsIter(aEval.m_rhsImpl,outer), m_functor(aEval.m_functor)
86 EIGEN_STRONG_INLINE InnerIterator& operator++()
88 if (m_lhsIter && m_rhsIter && (m_lhsIter.index() == m_rhsIter.index()))
90 m_id = m_lhsIter.index();
91 m_value = m_functor(m_lhsIter.value(), m_rhsIter.value());
95 else if (m_lhsIter && (!m_rhsIter || (m_lhsIter.index() < m_rhsIter.index())))
97 m_id = m_lhsIter.index();
98 m_value = m_functor(m_lhsIter.value(), Scalar(0));
101 else if (m_rhsIter && (!m_lhsIter || (m_lhsIter.index() > m_rhsIter.index())))
103 m_id = m_rhsIter.index();
104 m_value = m_functor(Scalar(0), m_rhsIter.value());
115 EIGEN_STRONG_INLINE Scalar value()
const {
return m_value; }
117 EIGEN_STRONG_INLINE StorageIndex index()
const {
return m_id; }
118 EIGEN_STRONG_INLINE Index row()
const {
return Lhs::IsRowMajor ? m_lhsIter.row() : index(); }
119 EIGEN_STRONG_INLINE Index col()
const {
return Lhs::IsRowMajor ? index() : m_lhsIter.col(); }
121 EIGEN_STRONG_INLINE
operator bool()
const {
return m_id>=0; }
124 LhsIterator m_lhsIter;
125 RhsIterator m_rhsIter;
126 const BinaryOp& m_functor;
133 CoeffReadCost = evaluator<Lhs>::CoeffReadCost + evaluator<Rhs>::CoeffReadCost + functor_traits<BinaryOp>::Cost,
134 Flags = XprType::Flags
137 explicit binary_evaluator(
const XprType& xpr)
138 : m_functor(xpr.functor()),
139 m_lhsImpl(xpr.lhs()),
143 inline Index nonZerosEstimate()
const {
144 return m_lhsImpl.nonZerosEstimate() + m_rhsImpl.nonZerosEstimate();
148 const BinaryOp m_functor;
149 evaluator<Lhs> m_lhsImpl;
150 evaluator<Rhs> m_rhsImpl;
154 template<
typename T,
typename Lhs,
typename Rhs>
155 struct binary_evaluator<CwiseBinaryOp<scalar_product_op<T>, Lhs, Rhs>, IteratorBased, IteratorBased>
156 : evaluator_base<CwiseBinaryOp<scalar_product_op<T>, Lhs, Rhs> >
159 typedef scalar_product_op<T> BinaryOp;
160 typedef typename evaluator<Lhs>::InnerIterator LhsIterator;
161 typedef typename evaluator<Rhs>::InnerIterator RhsIterator;
162 typedef CwiseBinaryOp<BinaryOp, Lhs, Rhs> XprType;
163 typedef typename XprType::StorageIndex StorageIndex;
164 typedef typename traits<XprType>::Scalar Scalar;
167 class ReverseInnerIterator;
172 EIGEN_STRONG_INLINE InnerIterator(
const binary_evaluator& aEval, Index outer)
173 : m_lhsIter(aEval.m_lhsImpl,outer), m_rhsIter(aEval.m_rhsImpl,outer), m_functor(aEval.m_functor)
175 while (m_lhsIter && m_rhsIter && (m_lhsIter.index() != m_rhsIter.index()))
177 if (m_lhsIter.index() < m_rhsIter.index())
184 EIGEN_STRONG_INLINE InnerIterator& operator++()
188 while (m_lhsIter && m_rhsIter && (m_lhsIter.index() != m_rhsIter.index()))
190 if (m_lhsIter.index() < m_rhsIter.index())
198 EIGEN_STRONG_INLINE Scalar value()
const {
return m_functor(m_lhsIter.value(), m_rhsIter.value()); }
200 EIGEN_STRONG_INLINE StorageIndex index()
const {
return m_lhsIter.index(); }
201 EIGEN_STRONG_INLINE Index row()
const {
return m_lhsIter.row(); }
202 EIGEN_STRONG_INLINE Index col()
const {
return m_lhsIter.col(); }
204 EIGEN_STRONG_INLINE
operator bool()
const {
return (m_lhsIter && m_rhsIter); }
207 LhsIterator m_lhsIter;
208 RhsIterator m_rhsIter;
209 const BinaryOp& m_functor;
214 CoeffReadCost = evaluator<Lhs>::CoeffReadCost + evaluator<Rhs>::CoeffReadCost + functor_traits<BinaryOp>::Cost,
215 Flags = XprType::Flags
218 explicit binary_evaluator(
const XprType& xpr)
219 : m_functor(xpr.functor()),
220 m_lhsImpl(xpr.lhs()),
224 inline Index nonZerosEstimate()
const {
225 return (std::min)(m_lhsImpl.nonZerosEstimate(), m_rhsImpl.nonZerosEstimate());
229 const BinaryOp m_functor;
230 evaluator<Lhs> m_lhsImpl;
231 evaluator<Rhs> m_rhsImpl;
235 template<
typename T,
typename Lhs,
typename Rhs>
236 struct binary_evaluator<CwiseBinaryOp<scalar_product_op<T>, Lhs, Rhs>, IndexBased, IteratorBased>
237 : evaluator_base<CwiseBinaryOp<scalar_product_op<T>, Lhs, Rhs> >
240 typedef scalar_product_op<T> BinaryOp;
241 typedef evaluator<Lhs> LhsEvaluator;
242 typedef typename evaluator<Rhs>::InnerIterator RhsIterator;
243 typedef CwiseBinaryOp<BinaryOp, Lhs, Rhs> XprType;
244 typedef typename XprType::StorageIndex StorageIndex;
245 typedef typename traits<XprType>::Scalar Scalar;
248 class ReverseInnerIterator;
255 EIGEN_STRONG_INLINE InnerIterator(
const binary_evaluator& aEval, Index outer)
256 : m_lhsEval(aEval.m_lhsImpl), m_rhsIter(aEval.m_rhsImpl,outer), m_functor(aEval.m_functor), m_outer(outer)
259 EIGEN_STRONG_INLINE InnerIterator& operator++()
265 EIGEN_STRONG_INLINE Scalar value()
const
266 {
return m_functor(m_lhsEval.coeff(IsRowMajor?m_outer:m_rhsIter.index(),IsRowMajor?m_rhsIter.index():m_outer), m_rhsIter.value()); }
268 EIGEN_STRONG_INLINE StorageIndex index()
const {
return m_rhsIter.index(); }
269 EIGEN_STRONG_INLINE Index row()
const {
return m_rhsIter.row(); }
270 EIGEN_STRONG_INLINE Index col()
const {
return m_rhsIter.col(); }
272 EIGEN_STRONG_INLINE
operator bool()
const {
return m_rhsIter; }
275 const LhsEvaluator &m_lhsEval;
276 RhsIterator m_rhsIter;
277 const BinaryOp& m_functor;
283 CoeffReadCost = evaluator<Lhs>::CoeffReadCost + evaluator<Rhs>::CoeffReadCost + functor_traits<BinaryOp>::Cost,
284 Flags = XprType::Flags
287 explicit binary_evaluator(
const XprType& xpr)
288 : m_functor(xpr.functor()),
289 m_lhsImpl(xpr.lhs()),
293 inline Index nonZerosEstimate()
const {
294 return m_rhsImpl.nonZerosEstimate();
298 const BinaryOp m_functor;
299 evaluator<Lhs> m_lhsImpl;
300 evaluator<Rhs> m_rhsImpl;
304 template<
typename T,
typename Lhs,
typename Rhs>
305 struct binary_evaluator<CwiseBinaryOp<scalar_product_op<T>, Lhs, Rhs>, IteratorBased, IndexBased>
306 : evaluator_base<CwiseBinaryOp<scalar_product_op<T>, Lhs, Rhs> >
309 typedef scalar_product_op<T> BinaryOp;
310 typedef typename evaluator<Lhs>::InnerIterator LhsIterator;
311 typedef evaluator<Rhs> RhsEvaluator;
312 typedef CwiseBinaryOp<BinaryOp, Lhs, Rhs> XprType;
313 typedef typename XprType::StorageIndex StorageIndex;
314 typedef typename traits<XprType>::Scalar Scalar;
317 class ReverseInnerIterator;
324 EIGEN_STRONG_INLINE InnerIterator(
const binary_evaluator& aEval, Index outer)
325 : m_lhsIter(aEval.m_lhsImpl,outer), m_rhsEval(aEval.m_rhsImpl), m_functor(aEval.m_functor), m_outer(outer)
328 EIGEN_STRONG_INLINE InnerIterator& operator++()
334 EIGEN_STRONG_INLINE Scalar value()
const
335 {
return m_functor(m_lhsIter.value(),
336 m_rhsEval.coeff(IsRowMajor?m_outer:m_lhsIter.index(),IsRowMajor?m_lhsIter.index():m_outer)); }
338 EIGEN_STRONG_INLINE StorageIndex index()
const {
return m_lhsIter.index(); }
339 EIGEN_STRONG_INLINE Index row()
const {
return m_lhsIter.row(); }
340 EIGEN_STRONG_INLINE Index col()
const {
return m_lhsIter.col(); }
342 EIGEN_STRONG_INLINE
operator bool()
const {
return m_lhsIter; }
345 LhsIterator m_lhsIter;
346 const evaluator<Rhs> &m_rhsEval;
347 const BinaryOp& m_functor;
353 CoeffReadCost = evaluator<Lhs>::CoeffReadCost + evaluator<Rhs>::CoeffReadCost + functor_traits<BinaryOp>::Cost,
354 Flags = XprType::Flags
357 explicit binary_evaluator(
const XprType& xpr)
358 : m_functor(xpr.functor()),
359 m_lhsImpl(xpr.lhs()),
363 inline Index nonZerosEstimate()
const {
364 return m_lhsImpl.nonZerosEstimate();
368 const BinaryOp m_functor;
369 evaluator<Lhs> m_lhsImpl;
370 evaluator<Rhs> m_rhsImpl;
379 template<
typename Derived>
380 template<
typename OtherDerived>
381 EIGEN_STRONG_INLINE Derived &
382 SparseMatrixBase<Derived>::operator-=(
const SparseMatrixBase<OtherDerived> &other)
384 return derived() = derived() - other.derived();
387 template<
typename Derived>
388 template<
typename OtherDerived>
389 EIGEN_STRONG_INLINE Derived &
390 SparseMatrixBase<Derived>::operator+=(
const SparseMatrixBase<OtherDerived>& other)
392 return derived() = derived() + other.derived();
395 template<
typename Derived>
396 template<
typename OtherDerived>
397 Derived& SparseMatrixBase<Derived>::operator+=(
const DiagonalBase<OtherDerived>& other)
399 call_assignment_no_alias(derived(), other.derived(), internal::add_assign_op<Scalar>());
403 template<
typename Derived>
404 template<
typename OtherDerived>
405 Derived& SparseMatrixBase<Derived>::operator-=(
const DiagonalBase<OtherDerived>& other)
407 call_assignment_no_alias(derived(), other.derived(), internal::sub_assign_op<Scalar>());
411 template<
typename Derived>
412 template<
typename OtherDerived>
413 EIGEN_STRONG_INLINE
const EIGEN_SPARSE_CWISE_PRODUCT_RETURN_TYPE
416 return EIGEN_SPARSE_CWISE_PRODUCT_RETURN_TYPE(derived(), other.derived());
421 #endif // EIGEN_SPARSE_CWISE_BINARY_OP_H
const CwiseBinaryOp< internal::scalar_product_op< typename Derived::Scalar, typename OtherDerived::Scalar >, const Derived, const OtherDerived > cwiseProduct(const Eigen::SparseMatrixBase< OtherDerived > &other) const
Definition: SparseMatrixBase.h:24
const unsigned int RowMajorBit
Definition: Constants.h:53
Definition: Eigen_Colamd.h:54