11#ifndef EIGEN_SPARSE_CWISE_BINARY_OP_H
12#define EIGEN_SPARSE_CWISE_BINARY_OP_H
15#include "./InternalHeaderCheck.h"
39template <
typename BinaryOp,
typename Lhs,
typename Rhs>
40class CwiseBinaryOpImpl<BinaryOp, Lhs, Rhs,
Sparse> :
public SparseMatrixBase<CwiseBinaryOp<BinaryOp, Lhs, Rhs> > {
42 using Derived = CwiseBinaryOp<BinaryOp, Lhs, Rhs>;
43 using Base = SparseMatrixBase<Derived>;
44 EIGEN_SPARSE_PUBLIC_INTERFACE(Derived)
45 EIGEN_STATIC_ASSERT(((!std::is_same<
typename internal::traits<Lhs>::StorageKind,
46 typename internal::traits<Rhs>::StorageKind>::value) ||
49 THE_STORAGE_ORDER_OF_BOTH_SIDES_MUST_MATCH)
62template <
typename BinaryOp,
typename Lhs,
typename Rhs>
63struct binary_evaluator<CwiseBinaryOp<BinaryOp, Lhs, Rhs>, IteratorBased, IteratorBased>
64 : evaluator_base<CwiseBinaryOp<BinaryOp, Lhs, Rhs> > {
66 using LhsIterator =
typename evaluator<Lhs>::InnerIterator;
67 using RhsIterator =
typename evaluator<Rhs>::InnerIterator;
68 using XprType = CwiseBinaryOp<BinaryOp, Lhs, Rhs>;
69 using Scalar =
typename traits<XprType>::Scalar;
70 using StorageIndex =
typename XprType::StorageIndex;
75 EIGEN_STRONG_INLINE InnerIterator(
const binary_evaluator& aEval, Index outer)
76 : m_lhsIter(aEval.m_lhsImpl, outer),
77 m_rhsIter(aEval.m_rhsImpl, outer),
78 m_functor(aEval.m_functor),
83 EIGEN_STRONG_INLINE InnerIterator& operator++() {
84 if (m_lhsIter && m_rhsIter && (m_lhsIter.index() == m_rhsIter.index())) {
85 m_id = convert_index<StorageIndex>(m_lhsIter.index());
86 m_value = m_functor(m_lhsIter.value(), m_rhsIter.value());
89 }
else if (m_lhsIter && (!m_rhsIter || (m_lhsIter.index() < m_rhsIter.index()))) {
90 m_id = convert_index<StorageIndex>(m_lhsIter.index());
91 m_value = m_functor(m_lhsIter.value(),
typename Rhs::Scalar(0));
93 }
else if (m_rhsIter && (!m_lhsIter || (m_lhsIter.index() > m_rhsIter.index()))) {
94 m_id = convert_index<StorageIndex>(m_rhsIter.index());
95 m_value = m_functor(
typename Lhs::Scalar(0), m_rhsIter.value());
103 EIGEN_STRONG_INLINE Scalar value()
const {
return m_value; }
105 EIGEN_STRONG_INLINE StorageIndex index()
const {
return m_id; }
106 EIGEN_STRONG_INLINE Index outer()
const {
return m_lhsIter.outer(); }
107 EIGEN_STRONG_INLINE Index row()
const {
return Lhs::IsRowMajor ? m_lhsIter.row() : index(); }
108 EIGEN_STRONG_INLINE Index col()
const {
return Lhs::IsRowMajor ? index() : m_lhsIter.col(); }
110 EIGEN_STRONG_INLINE
operator bool()
const {
return m_id >= 0; }
113 LhsIterator m_lhsIter;
114 RhsIterator m_rhsIter;
115 const BinaryOp& m_functor;
122 int(evaluator<Lhs>::CoeffReadCost) + int(evaluator<Rhs>::CoeffReadCost) + int(functor_traits<BinaryOp>::Cost),
123 Flags = XprType::Flags
126 explicit binary_evaluator(
const XprType& xpr) : m_functor(xpr.functor()), m_lhsImpl(xpr.lhs()), m_rhsImpl(xpr.rhs()) {
127 EIGEN_INTERNAL_CHECK_COST_VALUE(functor_traits<BinaryOp>::Cost);
128 EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
131 inline Index nonZerosEstimate()
const {
return m_lhsImpl.nonZerosEstimate() + m_rhsImpl.nonZerosEstimate(); }
134 const BinaryOp m_functor;
135 evaluator<Lhs> m_lhsImpl;
136 evaluator<Rhs> m_rhsImpl;
140template <
typename BinaryOp,
typename Lhs,
typename Rhs>
141struct binary_evaluator<CwiseBinaryOp<BinaryOp, Lhs, Rhs>, IndexBased, IteratorBased>
142 : evaluator_base<CwiseBinaryOp<BinaryOp, Lhs, Rhs> > {
144 using RhsIterator =
typename evaluator<Rhs>::InnerIterator;
145 using XprType = CwiseBinaryOp<BinaryOp, Lhs, Rhs>;
146 using Scalar =
typename traits<XprType>::Scalar;
147 using StorageIndex =
typename XprType::StorageIndex;
150 class InnerIterator {
151 enum { IsRowMajor = (int(Rhs::Flags) &
RowMajorBit) == RowMajorBit };
154 EIGEN_STRONG_INLINE InnerIterator(
const binary_evaluator& aEval, Index outer)
155 : m_lhsEval(aEval.m_lhsImpl),
156 m_rhsIter(aEval.m_rhsImpl, outer),
157 m_functor(aEval.m_functor),
160 m_innerSize(aEval.m_expr.rhs().innerSize()) {
164 EIGEN_STRONG_INLINE InnerIterator& operator++() {
166 if (m_id < m_innerSize) {
167 typename Lhs::Scalar lhsVal =
168 m_lhsEval.coeff(IsRowMajor ? m_rhsIter.outer() : m_id, IsRowMajor ? m_id : m_rhsIter.outer());
169 if (m_rhsIter && m_rhsIter.index() == m_id) {
170 m_value = m_functor(lhsVal, m_rhsIter.value());
173 m_value = m_functor(lhsVal,
typename Rhs::Scalar(0));
179 EIGEN_STRONG_INLINE Scalar value()
const {
180 eigen_internal_assert(m_id < m_innerSize);
184 EIGEN_STRONG_INLINE StorageIndex index()
const {
return m_id; }
185 EIGEN_STRONG_INLINE Index outer()
const {
return m_rhsIter.outer(); }
186 EIGEN_STRONG_INLINE Index row()
const {
return IsRowMajor ? m_rhsIter.outer() : m_id; }
187 EIGEN_STRONG_INLINE Index col()
const {
return IsRowMajor ? m_id : m_rhsIter.outer(); }
189 EIGEN_STRONG_INLINE
operator bool()
const {
return m_id < m_innerSize; }
192 const evaluator<Lhs>& m_lhsEval;
193 RhsIterator m_rhsIter;
194 const BinaryOp& m_functor;
197 StorageIndex m_innerSize;
202 int(evaluator<Lhs>::CoeffReadCost) + int(evaluator<Rhs>::CoeffReadCost) + int(functor_traits<BinaryOp>::Cost),
203 Flags = XprType::Flags
206 explicit binary_evaluator(
const XprType& xpr)
207 : m_functor(xpr.functor()), m_lhsImpl(xpr.lhs()), m_rhsImpl(xpr.rhs()), m_expr(xpr) {
208 EIGEN_INTERNAL_CHECK_COST_VALUE(functor_traits<BinaryOp>::Cost);
209 EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
212 inline Index nonZerosEstimate()
const {
return m_expr.size(); }
215 const BinaryOp m_functor;
216 evaluator<Lhs> m_lhsImpl;
217 evaluator<Rhs> m_rhsImpl;
218 const XprType& m_expr;
222template <
typename BinaryOp,
typename Lhs,
typename Rhs>
223struct binary_evaluator<CwiseBinaryOp<BinaryOp, Lhs, Rhs>, IteratorBased, IndexBased>
224 : evaluator_base<CwiseBinaryOp<BinaryOp, Lhs, Rhs> > {
226 using LhsIterator =
typename evaluator<Lhs>::InnerIterator;
227 using XprType = CwiseBinaryOp<BinaryOp, Lhs, Rhs>;
228 using Scalar =
typename traits<XprType>::Scalar;
229 using StorageIndex =
typename XprType::StorageIndex;
232 class InnerIterator {
233 enum { IsRowMajor = (int(Lhs::Flags) &
RowMajorBit) == RowMajorBit };
236 EIGEN_STRONG_INLINE InnerIterator(
const binary_evaluator& aEval, Index outer)
237 : m_lhsIter(aEval.m_lhsImpl, outer),
238 m_rhsEval(aEval.m_rhsImpl),
239 m_functor(aEval.m_functor),
242 m_innerSize(aEval.m_expr.lhs().innerSize()) {
246 EIGEN_STRONG_INLINE InnerIterator& operator++() {
248 if (m_id < m_innerSize) {
249 typename Rhs::Scalar rhsVal =
250 m_rhsEval.coeff(IsRowMajor ? m_lhsIter.outer() : m_id, IsRowMajor ? m_id : m_lhsIter.outer());
251 if (m_lhsIter && m_lhsIter.index() == m_id) {
252 m_value = m_functor(m_lhsIter.value(), rhsVal);
255 m_value = m_functor(
typename Lhs::Scalar(0), rhsVal);
261 EIGEN_STRONG_INLINE Scalar value()
const {
262 eigen_internal_assert(m_id < m_innerSize);
266 EIGEN_STRONG_INLINE StorageIndex index()
const {
return m_id; }
267 EIGEN_STRONG_INLINE Index outer()
const {
return m_lhsIter.outer(); }
268 EIGEN_STRONG_INLINE Index row()
const {
return IsRowMajor ? m_lhsIter.outer() : m_id; }
269 EIGEN_STRONG_INLINE Index col()
const {
return IsRowMajor ? m_id : m_lhsIter.outer(); }
271 EIGEN_STRONG_INLINE
operator bool()
const {
return m_id < m_innerSize; }
274 LhsIterator m_lhsIter;
275 const evaluator<Rhs>& m_rhsEval;
276 const BinaryOp& m_functor;
279 StorageIndex m_innerSize;
284 int(evaluator<Lhs>::CoeffReadCost) + int(evaluator<Rhs>::CoeffReadCost) + int(functor_traits<BinaryOp>::Cost),
285 Flags = XprType::Flags
288 explicit binary_evaluator(
const XprType& xpr)
289 : m_functor(xpr.functor()), m_lhsImpl(xpr.lhs()), m_rhsImpl(xpr.rhs()), m_expr(xpr) {
290 EIGEN_INTERNAL_CHECK_COST_VALUE(functor_traits<BinaryOp>::Cost);
291 EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
294 inline Index nonZerosEstimate()
const {
return m_expr.size(); }
297 const BinaryOp m_functor;
298 evaluator<Lhs> m_lhsImpl;
299 evaluator<Rhs> m_rhsImpl;
300 const XprType& m_expr;
303template <typename T, typename LhsKind = typename evaluator_traits<typename T::Lhs>::Kind,
304 typename RhsKind =
typename evaluator_traits<typename T::Rhs>::Kind,
305 typename LhsScalar =
typename traits<typename T::Lhs>::Scalar,
306 typename RhsScalar =
typename traits<typename T::Rhs>::Scalar>
307struct sparse_conjunction_evaluator;
310template <
typename T1,
typename T2,
typename Lhs,
typename Rhs>
311struct binary_evaluator<CwiseBinaryOp<scalar_product_op<T1, T2>, Lhs, Rhs>, IteratorBased, IteratorBased>
312 : sparse_conjunction_evaluator<CwiseBinaryOp<scalar_product_op<T1, T2>, Lhs, Rhs> > {
313 using XprType = CwiseBinaryOp<scalar_product_op<T1, T2>, Lhs, Rhs>;
314 using Base = sparse_conjunction_evaluator<XprType>;
315 explicit binary_evaluator(
const XprType& xpr) : Base(xpr) {}
318template <
typename T1,
typename T2,
typename Lhs,
typename Rhs>
319struct binary_evaluator<CwiseBinaryOp<scalar_product_op<T1, T2>, Lhs, Rhs>, IndexBased, IteratorBased>
320 : sparse_conjunction_evaluator<CwiseBinaryOp<scalar_product_op<T1, T2>, Lhs, Rhs> > {
321 using XprType = CwiseBinaryOp<scalar_product_op<T1, T2>, Lhs, Rhs>;
322 using Base = sparse_conjunction_evaluator<XprType>;
323 explicit binary_evaluator(
const XprType& xpr) : Base(xpr) {}
326template <
typename T1,
typename T2,
typename Lhs,
typename Rhs>
327struct binary_evaluator<CwiseBinaryOp<scalar_product_op<T1, T2>, Lhs, Rhs>, IteratorBased, IndexBased>
328 : sparse_conjunction_evaluator<CwiseBinaryOp<scalar_product_op<T1, T2>, Lhs, Rhs> > {
329 using XprType = CwiseBinaryOp<scalar_product_op<T1, T2>, Lhs, Rhs>;
330 using Base = sparse_conjunction_evaluator<XprType>;
331 explicit binary_evaluator(
const XprType& xpr) : Base(xpr) {}
335template <
typename T1,
typename T2,
typename Lhs,
typename Rhs>
336struct binary_evaluator<CwiseBinaryOp<scalar_quotient_op<T1, T2>, Lhs, Rhs>, IteratorBased, IndexBased>
337 : sparse_conjunction_evaluator<CwiseBinaryOp<scalar_quotient_op<T1, T2>, Lhs, Rhs> > {
338 using XprType = CwiseBinaryOp<scalar_quotient_op<T1, T2>, Lhs, Rhs>;
339 using Base = sparse_conjunction_evaluator<XprType>;
340 explicit binary_evaluator(
const XprType& xpr) : Base(xpr) {}
344template <
typename Lhs,
typename Rhs>
345struct binary_evaluator<CwiseBinaryOp<scalar_boolean_and_op<bool>, Lhs, Rhs>, IteratorBased, IteratorBased>
346 : sparse_conjunction_evaluator<CwiseBinaryOp<scalar_boolean_and_op<bool>, Lhs, Rhs> > {
347 using XprType = CwiseBinaryOp<scalar_boolean_and_op<bool>, Lhs, Rhs>;
348 using Base = sparse_conjunction_evaluator<XprType>;
349 explicit binary_evaluator(
const XprType& xpr) : Base(xpr) {}
352template <
typename Lhs,
typename Rhs>
353struct binary_evaluator<CwiseBinaryOp<scalar_boolean_and_op<bool>, Lhs, Rhs>, IndexBased, IteratorBased>
354 : sparse_conjunction_evaluator<CwiseBinaryOp<scalar_boolean_and_op<bool>, Lhs, Rhs> > {
355 using XprType = CwiseBinaryOp<scalar_boolean_and_op<bool>, Lhs, Rhs>;
356 using Base = sparse_conjunction_evaluator<XprType>;
357 explicit binary_evaluator(
const XprType& xpr) : Base(xpr) {}
360template <
typename Lhs,
typename Rhs>
361struct binary_evaluator<CwiseBinaryOp<scalar_boolean_and_op<bool>, Lhs, Rhs>, IteratorBased, IndexBased>
362 : sparse_conjunction_evaluator<CwiseBinaryOp<scalar_boolean_and_op<bool>, Lhs, Rhs> > {
363 using XprType = CwiseBinaryOp<scalar_boolean_and_op<bool>, Lhs, Rhs>;
364 using Base = sparse_conjunction_evaluator<XprType>;
365 explicit binary_evaluator(
const XprType& xpr) : Base(xpr) {}
376template <
typename XprType>
377struct sparse_conjunction_evaluator<XprType, IteratorBased, IteratorBased> : evaluator_base<XprType> {
379 using BinaryOp =
typename XprType::Functor;
380 using LhsArg =
typename XprType::Lhs;
381 using RhsArg =
typename XprType::Rhs;
382 using LhsIterator =
typename evaluator<LhsArg>::InnerIterator;
383 using RhsIterator =
typename evaluator<RhsArg>::InnerIterator;
384 using StorageIndex =
typename XprType::StorageIndex;
385 using Scalar =
typename traits<XprType>::Scalar;
388 class InnerIterator {
390 EIGEN_STRONG_INLINE InnerIterator(
const sparse_conjunction_evaluator& aEval, Index outer)
391 : m_lhsIter(aEval.m_lhsImpl, outer), m_rhsIter(aEval.m_rhsImpl, outer), m_functor(aEval.m_functor) {
392 while (m_lhsIter && m_rhsIter && (m_lhsIter.index() != m_rhsIter.index())) {
393 if (m_lhsIter.index() < m_rhsIter.index())
400 EIGEN_STRONG_INLINE InnerIterator& operator++() {
403 while (m_lhsIter && m_rhsIter && (m_lhsIter.index() != m_rhsIter.index())) {
404 if (m_lhsIter.index() < m_rhsIter.index())
412 EIGEN_STRONG_INLINE Scalar value()
const {
return m_functor(m_lhsIter.value(), m_rhsIter.value()); }
414 EIGEN_STRONG_INLINE StorageIndex index()
const {
return m_lhsIter.index(); }
415 EIGEN_STRONG_INLINE Index outer()
const {
return m_lhsIter.outer(); }
416 EIGEN_STRONG_INLINE Index row()
const {
return m_lhsIter.row(); }
417 EIGEN_STRONG_INLINE Index col()
const {
return m_lhsIter.col(); }
419 EIGEN_STRONG_INLINE
operator bool()
const {
return m_lhsIter && m_rhsIter; }
422 LhsIterator m_lhsIter;
423 RhsIterator m_rhsIter;
424 const BinaryOp& m_functor;
428 CoeffReadCost = int(evaluator<LhsArg>::CoeffReadCost) + int(evaluator<RhsArg>::CoeffReadCost) +
429 int(functor_traits<BinaryOp>::Cost),
430 Flags = XprType::Flags
433 explicit sparse_conjunction_evaluator(
const XprType& xpr)
434 : m_functor(xpr.functor()), m_lhsImpl(xpr.lhs()), m_rhsImpl(xpr.rhs()) {
435 EIGEN_INTERNAL_CHECK_COST_VALUE(functor_traits<BinaryOp>::Cost);
436 EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
439 inline Index nonZerosEstimate()
const {
440 return (std::min)(m_lhsImpl.nonZerosEstimate(), m_rhsImpl.nonZerosEstimate());
444 const BinaryOp m_functor;
445 evaluator<LhsArg> m_lhsImpl;
446 evaluator<RhsArg> m_rhsImpl;
450template <
typename XprType>
451struct sparse_conjunction_evaluator<XprType, IndexBased, IteratorBased> : evaluator_base<XprType> {
453 using BinaryOp =
typename XprType::Functor;
454 using LhsArg =
typename XprType::Lhs;
455 using RhsArg =
typename XprType::Rhs;
456 using LhsEvaluator = evaluator<LhsArg>;
457 using RhsIterator =
typename evaluator<RhsArg>::InnerIterator;
458 using StorageIndex =
typename XprType::StorageIndex;
459 using Scalar =
typename traits<XprType>::Scalar;
462 class InnerIterator {
463 enum { IsRowMajor = (int(RhsArg::Flags) &
RowMajorBit) == RowMajorBit };
466 EIGEN_STRONG_INLINE InnerIterator(
const sparse_conjunction_evaluator& aEval, Index outer)
467 : m_lhsEval(aEval.m_lhsImpl), m_rhsIter(aEval.m_rhsImpl, outer), m_functor(aEval.m_functor), m_outer(outer) {}
469 EIGEN_STRONG_INLINE InnerIterator& operator++() {
474 EIGEN_STRONG_INLINE Scalar value()
const {
476 m_lhsEval.coeff(IsRowMajor ? m_outer : m_rhsIter.index(), IsRowMajor ? m_rhsIter.index() : m_outer),
480 EIGEN_STRONG_INLINE StorageIndex index()
const {
return m_rhsIter.index(); }
481 EIGEN_STRONG_INLINE Index outer()
const {
return m_rhsIter.outer(); }
482 EIGEN_STRONG_INLINE Index row()
const {
return m_rhsIter.row(); }
483 EIGEN_STRONG_INLINE Index col()
const {
return m_rhsIter.col(); }
485 EIGEN_STRONG_INLINE
operator bool()
const {
return m_rhsIter; }
488 const LhsEvaluator& m_lhsEval;
489 RhsIterator m_rhsIter;
490 const BinaryOp& m_functor;
495 CoeffReadCost = int(evaluator<LhsArg>::CoeffReadCost) + int(evaluator<RhsArg>::CoeffReadCost) +
496 int(functor_traits<BinaryOp>::Cost),
497 Flags = XprType::Flags
500 explicit sparse_conjunction_evaluator(
const XprType& xpr)
501 : m_functor(xpr.functor()), m_lhsImpl(xpr.lhs()), m_rhsImpl(xpr.rhs()) {
502 EIGEN_INTERNAL_CHECK_COST_VALUE(functor_traits<BinaryOp>::Cost);
503 EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
506 inline Index nonZerosEstimate()
const {
return m_rhsImpl.nonZerosEstimate(); }
509 const BinaryOp m_functor;
510 evaluator<LhsArg> m_lhsImpl;
511 evaluator<RhsArg> m_rhsImpl;
515template <
typename XprType>
516struct sparse_conjunction_evaluator<XprType, IteratorBased, IndexBased> : evaluator_base<XprType> {
518 using BinaryOp =
typename XprType::Functor;
519 using LhsArg =
typename XprType::Lhs;
520 using RhsArg =
typename XprType::Rhs;
521 using LhsIterator =
typename evaluator<LhsArg>::InnerIterator;
522 using StorageIndex =
typename XprType::StorageIndex;
523 using Scalar =
typename traits<XprType>::Scalar;
526 class InnerIterator {
527 enum { IsRowMajor = (int(LhsArg::Flags) &
RowMajorBit) == RowMajorBit };
530 EIGEN_STRONG_INLINE InnerIterator(
const sparse_conjunction_evaluator& aEval, Index outer)
531 : m_lhsIter(aEval.m_lhsImpl, outer), m_rhsEval(aEval.m_rhsImpl), m_functor(aEval.m_functor), m_outer(outer) {}
533 EIGEN_STRONG_INLINE InnerIterator& operator++() {
538 EIGEN_STRONG_INLINE Scalar value()
const {
539 return m_functor(m_lhsIter.value(), m_rhsEval.coeff(IsRowMajor ? m_outer : m_lhsIter.index(),
540 IsRowMajor ? m_lhsIter.index() : m_outer));
543 EIGEN_STRONG_INLINE StorageIndex index()
const {
return m_lhsIter.index(); }
544 EIGEN_STRONG_INLINE Index outer()
const {
return m_lhsIter.outer(); }
545 EIGEN_STRONG_INLINE Index row()
const {
return m_lhsIter.row(); }
546 EIGEN_STRONG_INLINE Index col()
const {
return m_lhsIter.col(); }
548 EIGEN_STRONG_INLINE
operator bool()
const {
return m_lhsIter; }
551 LhsIterator m_lhsIter;
552 const evaluator<RhsArg>& m_rhsEval;
553 const BinaryOp& m_functor;
558 CoeffReadCost = int(evaluator<LhsArg>::CoeffReadCost) + int(evaluator<RhsArg>::CoeffReadCost) +
559 int(functor_traits<BinaryOp>::Cost),
560 Flags = XprType::Flags
563 explicit sparse_conjunction_evaluator(
const XprType& xpr)
564 : m_functor(xpr.functor()), m_lhsImpl(xpr.lhs()), m_rhsImpl(xpr.rhs()) {
565 EIGEN_INTERNAL_CHECK_COST_VALUE(functor_traits<BinaryOp>::Cost);
566 EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
569 inline Index nonZerosEstimate()
const {
return m_lhsImpl.nonZerosEstimate(); }
572 const BinaryOp m_functor;
573 evaluator<LhsArg> m_lhsImpl;
574 evaluator<RhsArg> m_rhsImpl;
577template <typename T, typename LhsKind = typename evaluator_traits<typename T::Lhs>::Kind,
578 typename RhsKind =
typename evaluator_traits<typename T::Rhs>::Kind,
579 typename LhsScalar =
typename traits<typename T::Lhs>::Scalar,
580 typename RhsScalar =
typename traits<typename T::Rhs>::Scalar>
581struct sparse_disjunction_evaluator;
591template <
typename XprType>
592struct sparse_disjunction_evaluator<XprType, IteratorBased, IteratorBased> : evaluator_base<XprType> {
594 using BinaryOp =
typename XprType::Functor;
595 using LhsArg =
typename XprType::Lhs;
596 using RhsArg =
typename XprType::Rhs;
597 using LhsIterator =
typename evaluator<LhsArg>::InnerIterator;
598 using RhsIterator =
typename evaluator<RhsArg>::InnerIterator;
599 using StorageIndex =
typename XprType::StorageIndex;
600 using Scalar =
typename traits<XprType>::Scalar;
603 class InnerIterator {
605 EIGEN_STRONG_INLINE InnerIterator(
const sparse_disjunction_evaluator& aEval, Index outer)
606 : m_lhsIter(aEval.m_lhsImpl, outer),
607 m_rhsIter(aEval.m_rhsImpl, outer),
608 m_functor(aEval.m_functor),
613 EIGEN_STRONG_INLINE InnerIterator& operator++() {
614 if (m_lhsIter && m_rhsIter && (m_lhsIter.index() == m_rhsIter.index())) {
615 m_id = m_lhsIter.index();
616 m_value = m_functor(m_lhsIter.value(), m_rhsIter.value());
619 }
else if (m_lhsIter && (!m_rhsIter || (m_lhsIter.index() < m_rhsIter.index()))) {
620 m_id = m_lhsIter.index();
621 m_value = m_lhsIter.value();
623 }
else if (m_rhsIter && (!m_lhsIter || (m_lhsIter.index() > m_rhsIter.index()))) {
624 m_id = m_rhsIter.index();
625 m_value = m_rhsIter.value();
633 EIGEN_STRONG_INLINE Scalar value()
const {
return m_value; }
635 EIGEN_STRONG_INLINE StorageIndex index()
const {
return m_id; }
636 EIGEN_STRONG_INLINE Index outer()
const {
return m_lhsIter.outer(); }
637 EIGEN_STRONG_INLINE Index row()
const {
return LhsArg::IsRowMajor ? m_lhsIter.row() : index(); }
638 EIGEN_STRONG_INLINE Index col()
const {
return LhsArg::IsRowMajor ? index() : m_lhsIter.col(); }
640 EIGEN_STRONG_INLINE
operator bool()
const {
return m_id >= 0; }
643 LhsIterator m_lhsIter;
644 RhsIterator m_rhsIter;
645 const BinaryOp& m_functor;
651 CoeffReadCost = int(evaluator<LhsArg>::CoeffReadCost) + int(evaluator<RhsArg>::CoeffReadCost) +
652 int(functor_traits<BinaryOp>::Cost),
653 Flags = XprType::Flags
656 explicit sparse_disjunction_evaluator(
const XprType& xpr)
657 : m_functor(xpr.functor()), m_lhsImpl(xpr.lhs()), m_rhsImpl(xpr.rhs()) {
658 EIGEN_INTERNAL_CHECK_COST_VALUE(functor_traits<BinaryOp>::Cost);
659 EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
662 inline Index nonZerosEstimate()
const {
return m_lhsImpl.nonZerosEstimate() + m_rhsImpl.nonZerosEstimate(); }
665 const BinaryOp m_functor;
666 evaluator<LhsArg> m_lhsImpl;
667 evaluator<RhsArg> m_rhsImpl;
671template <
typename XprType>
672struct sparse_disjunction_evaluator<XprType, IndexBased, IteratorBased> : evaluator_base<XprType> {
674 using BinaryOp =
typename XprType::Functor;
675 using LhsArg =
typename XprType::Lhs;
676 using RhsArg =
typename XprType::Rhs;
677 using RhsIterator =
typename evaluator<RhsArg>::InnerIterator;
678 using StorageIndex =
typename XprType::StorageIndex;
679 using Scalar =
typename traits<XprType>::Scalar;
682 class InnerIterator {
683 enum { IsRowMajor = (int(RhsArg::Flags) &
RowMajorBit) == RowMajorBit };
686 EIGEN_STRONG_INLINE InnerIterator(
const sparse_disjunction_evaluator& aEval, Index outer)
687 : m_lhsEval(aEval.m_lhsImpl),
688 m_rhsIter(aEval.m_rhsImpl, outer),
689 m_functor(aEval.m_functor),
692 m_innerSize(aEval.m_expr.rhs().innerSize()) {
696 EIGEN_STRONG_INLINE InnerIterator& operator++() {
698 if (m_id < m_innerSize) {
699 Scalar lhsVal = m_lhsEval.coeff(IsRowMajor ? m_rhsIter.outer() : m_id, IsRowMajor ? m_id : m_rhsIter.outer());
700 if (m_rhsIter && m_rhsIter.index() == m_id) {
701 m_value = m_functor(lhsVal, m_rhsIter.value());
710 EIGEN_STRONG_INLINE Scalar value()
const {
711 eigen_internal_assert(m_id < m_innerSize);
715 EIGEN_STRONG_INLINE StorageIndex index()
const {
return m_id; }
716 EIGEN_STRONG_INLINE Index outer()
const {
return m_rhsIter.outer(); }
717 EIGEN_STRONG_INLINE Index row()
const {
return IsRowMajor ? m_rhsIter.outer() : m_id; }
718 EIGEN_STRONG_INLINE Index col()
const {
return IsRowMajor ? m_id : m_rhsIter.outer(); }
720 EIGEN_STRONG_INLINE
operator bool()
const {
return m_id < m_innerSize; }
723 const evaluator<LhsArg>& m_lhsEval;
724 RhsIterator m_rhsIter;
725 const BinaryOp& m_functor;
728 StorageIndex m_innerSize;
732 CoeffReadCost = int(evaluator<LhsArg>::CoeffReadCost) + int(evaluator<RhsArg>::CoeffReadCost) +
733 int(functor_traits<BinaryOp>::Cost),
734 Flags = XprType::Flags
737 explicit sparse_disjunction_evaluator(
const XprType& xpr)
738 : m_functor(xpr.functor()), m_lhsImpl(xpr.lhs()), m_rhsImpl(xpr.rhs()), m_expr(xpr) {
739 EIGEN_INTERNAL_CHECK_COST_VALUE(functor_traits<BinaryOp>::Cost);
740 EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
743 inline Index nonZerosEstimate()
const {
return m_expr.size(); }
746 const BinaryOp m_functor;
747 evaluator<LhsArg> m_lhsImpl;
748 evaluator<RhsArg> m_rhsImpl;
749 const XprType& m_expr;
753template <
typename XprType>
754struct sparse_disjunction_evaluator<XprType, IteratorBased, IndexBased> : evaluator_base<XprType> {
756 using BinaryOp =
typename XprType::Functor;
757 using LhsArg =
typename XprType::Lhs;
758 using RhsArg =
typename XprType::Rhs;
759 using LhsIterator =
typename evaluator<LhsArg>::InnerIterator;
760 using StorageIndex =
typename XprType::StorageIndex;
761 using Scalar =
typename traits<XprType>::Scalar;
764 class InnerIterator {
765 enum { IsRowMajor = (int(LhsArg::Flags) &
RowMajorBit) == RowMajorBit };
768 EIGEN_STRONG_INLINE InnerIterator(
const sparse_disjunction_evaluator& aEval, Index outer)
769 : m_lhsIter(aEval.m_lhsImpl, outer),
770 m_rhsEval(aEval.m_rhsImpl),
771 m_functor(aEval.m_functor),
774 m_innerSize(aEval.m_expr.lhs().innerSize()) {
778 EIGEN_STRONG_INLINE InnerIterator& operator++() {
780 if (m_id < m_innerSize) {
781 Scalar rhsVal = m_rhsEval.coeff(IsRowMajor ? m_lhsIter.outer() : m_id, IsRowMajor ? m_id : m_lhsIter.outer());
782 if (m_lhsIter && m_lhsIter.index() == m_id) {
783 m_value = m_functor(m_lhsIter.value(), rhsVal);
792 EIGEN_STRONG_INLINE Scalar value()
const {
793 eigen_internal_assert(m_id < m_innerSize);
797 EIGEN_STRONG_INLINE StorageIndex index()
const {
return m_id; }
798 EIGEN_STRONG_INLINE Index outer()
const {
return m_lhsIter.outer(); }
799 EIGEN_STRONG_INLINE Index row()
const {
return IsRowMajor ? m_lhsIter.outer() : m_id; }
800 EIGEN_STRONG_INLINE Index col()
const {
return IsRowMajor ? m_id : m_lhsIter.outer(); }
802 EIGEN_STRONG_INLINE
operator bool()
const {
return m_id < m_innerSize; }
805 LhsIterator m_lhsIter;
806 const evaluator<RhsArg>& m_rhsEval;
807 const BinaryOp& m_functor;
810 StorageIndex m_innerSize;
814 CoeffReadCost = int(evaluator<LhsArg>::CoeffReadCost) + int(evaluator<RhsArg>::CoeffReadCost) +
815 int(functor_traits<BinaryOp>::Cost),
816 Flags = XprType::Flags
819 explicit sparse_disjunction_evaluator(
const XprType& xpr)
820 : m_functor(xpr.functor()), m_lhsImpl(xpr.lhs()), m_rhsImpl(xpr.rhs()), m_expr(xpr) {
821 EIGEN_INTERNAL_CHECK_COST_VALUE(functor_traits<BinaryOp>::Cost);
822 EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
825 inline Index nonZerosEstimate()
const {
return m_expr.size(); }
828 const BinaryOp m_functor;
829 evaluator<LhsArg> m_lhsImpl;
830 evaluator<RhsArg> m_rhsImpl;
831 const XprType& m_expr;
835template <
typename T1,
typename T2,
typename DupFunc,
typename Lhs,
typename Rhs>
836struct binary_evaluator<CwiseBinaryOp<scalar_disjunction_op<DupFunc, T1, T2>, Lhs, Rhs>, IteratorBased, IteratorBased>
837 : sparse_disjunction_evaluator<CwiseBinaryOp<scalar_disjunction_op<DupFunc, T1, T2>, Lhs, Rhs> > {
838 using XprType = CwiseBinaryOp<scalar_disjunction_op<DupFunc, T1, T2>, Lhs, Rhs>;
839 using Base = sparse_disjunction_evaluator<XprType>;
840 explicit binary_evaluator(
const XprType& xpr) : Base(xpr) {}
848template <
typename Derived>
849template <
typename OtherDerived>
851 call_assignment(derived(), other.derived(), internal::add_assign_op<Scalar, typename OtherDerived::Scalar>());
855template <
typename Derived>
856template <
typename OtherDerived>
858 call_assignment(derived(), other.derived(), internal::sub_assign_op<Scalar, typename OtherDerived::Scalar>());
862template <
typename Derived>
863template <
typename OtherDerived>
865 return derived() = derived() - other.derived();
868template <
typename Derived>
869template <
typename OtherDerived>
871 return derived() = derived() + other.derived();
874template <
typename Derived>
875template <
typename OtherDerived>
877 call_assignment_no_alias(derived(), other.derived(),
878 internal::add_assign_op<Scalar, typename OtherDerived::Scalar>());
882template <
typename Derived>
883template <
typename OtherDerived>
885 call_assignment_no_alias(derived(), other.derived(),
886 internal::sub_assign_op<Scalar, typename OtherDerived::Scalar>());
890template <
typename Derived>
891template <
typename OtherDerived>
894 return typename CwiseProductDenseReturnType<OtherDerived>::Type(derived(), other.derived());
897template <
typename DenseDerived,
typename SparseDerived>
898EIGEN_STRONG_INLINE
const
900 const DenseDerived,
const SparseDerived>
903 const DenseDerived,
const SparseDerived>(a.derived(), b.derived());
906template <
typename SparseDerived,
typename DenseDerived>
907EIGEN_STRONG_INLINE
const
909 const SparseDerived,
const DenseDerived>
912 const SparseDerived,
const DenseDerived>(a.derived(), b.derived());
915template <
typename DenseDerived,
typename SparseDerived>
916EIGEN_STRONG_INLINE
const
918 const DenseDerived,
const SparseDerived>
921 const DenseDerived,
const SparseDerived>(a.derived(), b.derived());
924template <
typename SparseDerived,
typename DenseDerived>
925EIGEN_STRONG_INLINE
const
927 const SparseDerived,
const DenseDerived>
930 const SparseDerived,
const DenseDerived>(a.derived(), b.derived());
Generic expression where a coefficient-wise binary operator is applied to two expressions.
Definition CwiseBinaryOp.h:91
Base class for diagonal matrices and expressions.
Definition DiagonalMatrix.h:34
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:53
Base class of any sparse matrices or sparse expressions.
Definition SparseMatrixBase.h:31
constexpr const CwiseBinaryOp< internal::scalar_product_op< Derived ::Scalar, OtherDerived ::Scalar >, const Derived, const OtherDerived > cwiseProduct(const Eigen::SparseMatrixBase< OtherDerived > &other) const
Definition SparseMatrixBase.h:25
constexpr unsigned int RowMajorBit
Definition Constants.h:71
Definition EigenBase.h:34
Definition Constants.h:545