Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
SparseCwiseBinaryOp.h
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// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_SPARSE_CWISE_BINARY_OP_H
12#define EIGEN_SPARSE_CWISE_BINARY_OP_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
19// Here we have to handle 3 cases:
20// 1 - sparse op dense
21// 2 - dense op sparse
22// 3 - sparse op sparse
23// We also need to implement a 4th iterator for:
24// 4 - dense op dense
25// Finally, we also need to distinguish between the product and other operations :
26// configuration returned mode
27// 1 - sparse op dense product sparse
28// generic dense
29// 2 - dense op sparse product sparse
30// generic dense
31// 3 - sparse op sparse product sparse
32// generic sparse
33// 4 - dense op dense product dense
34// generic dense
35//
36// TODO: to ease compiler job, we could specialize product/quotient with a scalar
37// and fallback to cwise-unary evaluator using bind1st_op and bind2nd_op.
38
39template <typename BinaryOp, typename Lhs, typename Rhs>
40class CwiseBinaryOpImpl<BinaryOp, Lhs, Rhs, Sparse> : public SparseMatrixBase<CwiseBinaryOp<BinaryOp, Lhs, Rhs> > {
41 public:
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) ||
47 ((internal::evaluator<Lhs>::Flags & RowMajorBit) ==
48 (internal::evaluator<Rhs>::Flags & RowMajorBit))),
49 THE_STORAGE_ORDER_OF_BOTH_SIDES_MUST_MATCH)
50};
51
52namespace internal {
53
54// The default evaluator performs an "arithmetic" operation on two input arrays.
55// Given input arrays 'lhs' and 'rhs' and binary functor 'func',
56// the sparse destination array 'dst' is evaluated as follows:
57// if lhs(i,j) and rhs(i,j) are present, dst(i,j) = func(lhs(i,j), rhs(i,j))
58// if lhs(i,j) is present and rhs(i,j) is null, dst(i,j) = func(lhs(i,j), 0)
59// if lhs(i,j) is null and rhs(i,j) is present, dst(i,j) = func(0, rhs(i,j))
60
61// Generic "sparse OP sparse"
62template <typename BinaryOp, typename Lhs, typename Rhs>
63struct binary_evaluator<CwiseBinaryOp<BinaryOp, Lhs, Rhs>, IteratorBased, IteratorBased>
64 : evaluator_base<CwiseBinaryOp<BinaryOp, Lhs, Rhs> > {
65 protected:
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;
71
72 public:
73 class InnerIterator {
74 public:
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),
79 m_value(Scalar(0)) {
80 this->operator++();
81 }
82
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());
87 ++m_lhsIter;
88 ++m_rhsIter;
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));
92 ++m_lhsIter;
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());
96 ++m_rhsIter;
97 } else {
98 m_id = -1;
99 }
100 return *this;
101 }
102
103 EIGEN_STRONG_INLINE Scalar value() const { return m_value; }
104
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(); }
109
110 EIGEN_STRONG_INLINE operator bool() const { return m_id >= 0; }
111
112 protected:
113 LhsIterator m_lhsIter;
114 RhsIterator m_rhsIter;
115 const BinaryOp& m_functor;
116 Scalar m_value;
117 StorageIndex m_id;
118 };
119
120 enum {
121 CoeffReadCost =
122 int(evaluator<Lhs>::CoeffReadCost) + int(evaluator<Rhs>::CoeffReadCost) + int(functor_traits<BinaryOp>::Cost),
123 Flags = XprType::Flags
124 };
125
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);
129 }
130
131 inline Index nonZerosEstimate() const { return m_lhsImpl.nonZerosEstimate() + m_rhsImpl.nonZerosEstimate(); }
132
133 protected:
134 const BinaryOp m_functor;
135 evaluator<Lhs> m_lhsImpl;
136 evaluator<Rhs> m_rhsImpl;
137};
138
139// dense op sparse
140template <typename BinaryOp, typename Lhs, typename Rhs>
141struct binary_evaluator<CwiseBinaryOp<BinaryOp, Lhs, Rhs>, IndexBased, IteratorBased>
142 : evaluator_base<CwiseBinaryOp<BinaryOp, Lhs, Rhs> > {
143 protected:
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;
148
149 public:
150 class InnerIterator {
151 enum { IsRowMajor = (int(Rhs::Flags) & RowMajorBit) == RowMajorBit };
152
153 public:
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),
158 m_value(0),
159 m_id(-1),
160 m_innerSize(aEval.m_expr.rhs().innerSize()) {
161 this->operator++();
162 }
163
164 EIGEN_STRONG_INLINE InnerIterator& operator++() {
165 ++m_id;
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());
171 ++m_rhsIter;
172 } else
173 m_value = m_functor(lhsVal, typename Rhs::Scalar(0));
174 }
175
176 return *this;
177 }
178
179 EIGEN_STRONG_INLINE Scalar value() const {
180 eigen_internal_assert(m_id < m_innerSize);
181 return m_value;
182 }
183
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(); }
188
189 EIGEN_STRONG_INLINE operator bool() const { return m_id < m_innerSize; }
190
191 protected:
192 const evaluator<Lhs>& m_lhsEval;
193 RhsIterator m_rhsIter;
194 const BinaryOp& m_functor;
195 Scalar m_value;
196 StorageIndex m_id;
197 StorageIndex m_innerSize;
198 };
199
200 enum {
201 CoeffReadCost =
202 int(evaluator<Lhs>::CoeffReadCost) + int(evaluator<Rhs>::CoeffReadCost) + int(functor_traits<BinaryOp>::Cost),
203 Flags = XprType::Flags
204 };
205
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);
210 }
211
212 inline Index nonZerosEstimate() const { return m_expr.size(); }
213
214 protected:
215 const BinaryOp m_functor;
216 evaluator<Lhs> m_lhsImpl;
217 evaluator<Rhs> m_rhsImpl;
218 const XprType& m_expr;
219};
220
221// sparse op dense
222template <typename BinaryOp, typename Lhs, typename Rhs>
223struct binary_evaluator<CwiseBinaryOp<BinaryOp, Lhs, Rhs>, IteratorBased, IndexBased>
224 : evaluator_base<CwiseBinaryOp<BinaryOp, Lhs, Rhs> > {
225 protected:
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;
230
231 public:
232 class InnerIterator {
233 enum { IsRowMajor = (int(Lhs::Flags) & RowMajorBit) == RowMajorBit };
234
235 public:
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),
240 m_value(0),
241 m_id(-1),
242 m_innerSize(aEval.m_expr.lhs().innerSize()) {
243 this->operator++();
244 }
245
246 EIGEN_STRONG_INLINE InnerIterator& operator++() {
247 ++m_id;
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);
253 ++m_lhsIter;
254 } else
255 m_value = m_functor(typename Lhs::Scalar(0), rhsVal);
256 }
257
258 return *this;
259 }
260
261 EIGEN_STRONG_INLINE Scalar value() const {
262 eigen_internal_assert(m_id < m_innerSize);
263 return m_value;
264 }
265
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(); }
270
271 EIGEN_STRONG_INLINE operator bool() const { return m_id < m_innerSize; }
272
273 protected:
274 LhsIterator m_lhsIter;
275 const evaluator<Rhs>& m_rhsEval;
276 const BinaryOp& m_functor;
277 Scalar m_value;
278 StorageIndex m_id;
279 StorageIndex m_innerSize;
280 };
281
282 enum {
283 CoeffReadCost =
284 int(evaluator<Lhs>::CoeffReadCost) + int(evaluator<Rhs>::CoeffReadCost) + int(functor_traits<BinaryOp>::Cost),
285 Flags = XprType::Flags
286 };
287
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);
292 }
293
294 inline Index nonZerosEstimate() const { return m_expr.size(); }
295
296 protected:
297 const BinaryOp m_functor;
298 evaluator<Lhs> m_lhsImpl;
299 evaluator<Rhs> m_rhsImpl;
300 const XprType& m_expr;
301};
302
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;
308
309// "sparse .* sparse"
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) {}
316};
317// "dense .* sparse"
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) {}
324};
325// "sparse .* dense"
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) {}
332};
333
334// "sparse ./ dense"
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) {}
341};
342
343// "sparse && sparse"
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) {}
350};
351// "dense && sparse"
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) {}
358};
359// "sparse && dense"
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) {}
366};
367
368// The conjunction "^" evaluator performs a logical "and" or set "intersection" operation on two input arrays.
369// Given input arrays 'lhs' and 'rhs' and binary functor 'func',
370// the sparse destination array 'dst' is evaluated as follows:
371// if lhs(i,j) and rhs(i,j) are present, dst(i,j) = func(lhs(i,j), rhs(i,j))
372// if lhs(i,j) is present and rhs(i,j) is null, dst(i,j) is null
373// if lhs(i,j) is null and rhs(i,j) is present, dst(i,j) is null
374
375// "sparse ^ sparse"
376template <typename XprType>
377struct sparse_conjunction_evaluator<XprType, IteratorBased, IteratorBased> : evaluator_base<XprType> {
378 protected:
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;
386
387 public:
388 class InnerIterator {
389 public:
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())
394 ++m_lhsIter;
395 else
396 ++m_rhsIter;
397 }
398 }
399
400 EIGEN_STRONG_INLINE InnerIterator& operator++() {
401 ++m_lhsIter;
402 ++m_rhsIter;
403 while (m_lhsIter && m_rhsIter && (m_lhsIter.index() != m_rhsIter.index())) {
404 if (m_lhsIter.index() < m_rhsIter.index())
405 ++m_lhsIter;
406 else
407 ++m_rhsIter;
408 }
409 return *this;
410 }
411
412 EIGEN_STRONG_INLINE Scalar value() const { return m_functor(m_lhsIter.value(), m_rhsIter.value()); }
413
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(); }
418
419 EIGEN_STRONG_INLINE operator bool() const { return m_lhsIter && m_rhsIter; }
420
421 protected:
422 LhsIterator m_lhsIter;
423 RhsIterator m_rhsIter;
424 const BinaryOp& m_functor;
425 };
426
427 enum {
428 CoeffReadCost = int(evaluator<LhsArg>::CoeffReadCost) + int(evaluator<RhsArg>::CoeffReadCost) +
429 int(functor_traits<BinaryOp>::Cost),
430 Flags = XprType::Flags
431 };
432
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);
437 }
438
439 inline Index nonZerosEstimate() const {
440 return (std::min)(m_lhsImpl.nonZerosEstimate(), m_rhsImpl.nonZerosEstimate());
441 }
442
443 protected:
444 const BinaryOp m_functor;
445 evaluator<LhsArg> m_lhsImpl;
446 evaluator<RhsArg> m_rhsImpl;
447};
448
449// "dense ^ sparse"
450template <typename XprType>
451struct sparse_conjunction_evaluator<XprType, IndexBased, IteratorBased> : evaluator_base<XprType> {
452 protected:
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;
460
461 public:
462 class InnerIterator {
463 enum { IsRowMajor = (int(RhsArg::Flags) & RowMajorBit) == RowMajorBit };
464
465 public:
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) {}
468
469 EIGEN_STRONG_INLINE InnerIterator& operator++() {
470 ++m_rhsIter;
471 return *this;
472 }
473
474 EIGEN_STRONG_INLINE Scalar value() const {
475 return m_functor(
476 m_lhsEval.coeff(IsRowMajor ? m_outer : m_rhsIter.index(), IsRowMajor ? m_rhsIter.index() : m_outer),
477 m_rhsIter.value());
478 }
479
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(); }
484
485 EIGEN_STRONG_INLINE operator bool() const { return m_rhsIter; }
486
487 protected:
488 const LhsEvaluator& m_lhsEval;
489 RhsIterator m_rhsIter;
490 const BinaryOp& m_functor;
491 const Index m_outer;
492 };
493
494 enum {
495 CoeffReadCost = int(evaluator<LhsArg>::CoeffReadCost) + int(evaluator<RhsArg>::CoeffReadCost) +
496 int(functor_traits<BinaryOp>::Cost),
497 Flags = XprType::Flags
498 };
499
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);
504 }
505
506 inline Index nonZerosEstimate() const { return m_rhsImpl.nonZerosEstimate(); }
507
508 protected:
509 const BinaryOp m_functor;
510 evaluator<LhsArg> m_lhsImpl;
511 evaluator<RhsArg> m_rhsImpl;
512};
513
514// "sparse ^ dense"
515template <typename XprType>
516struct sparse_conjunction_evaluator<XprType, IteratorBased, IndexBased> : evaluator_base<XprType> {
517 protected:
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;
524
525 public:
526 class InnerIterator {
527 enum { IsRowMajor = (int(LhsArg::Flags) & RowMajorBit) == RowMajorBit };
528
529 public:
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) {}
532
533 EIGEN_STRONG_INLINE InnerIterator& operator++() {
534 ++m_lhsIter;
535 return *this;
536 }
537
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));
541 }
542
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(); }
547
548 EIGEN_STRONG_INLINE operator bool() const { return m_lhsIter; }
549
550 protected:
551 LhsIterator m_lhsIter;
552 const evaluator<RhsArg>& m_rhsEval;
553 const BinaryOp& m_functor;
554 const Index m_outer;
555 };
556
557 enum {
558 CoeffReadCost = int(evaluator<LhsArg>::CoeffReadCost) + int(evaluator<RhsArg>::CoeffReadCost) +
559 int(functor_traits<BinaryOp>::Cost),
560 Flags = XprType::Flags
561 };
562
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);
567 }
568
569 inline Index nonZerosEstimate() const { return m_lhsImpl.nonZerosEstimate(); }
570
571 protected:
572 const BinaryOp m_functor;
573 evaluator<LhsArg> m_lhsImpl;
574 evaluator<RhsArg> m_rhsImpl;
575};
576
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;
582
583// The disjunction "v" evaluator performs a logical "or" or set "union" operation on two input arrays.
584// Given input arrays 'lhs' and 'rhs' and binary functor 'func',
585// the sparse destination array 'dst' is evaluated as follows:
586// if lhs(i,j) and rhs(i,j) are present, dst(i,j) = func(lhs(i,j), rhs(i,j))
587// if lhs(i,j) is present and rhs(i,j) is null, dst(i,j) = lhs(i,j)
588// if lhs(i,j) is null and rhs(i,j) is present, dst(i,j) = rhs(i,j)
589
590// "sparse v sparse"
591template <typename XprType>
592struct sparse_disjunction_evaluator<XprType, IteratorBased, IteratorBased> : evaluator_base<XprType> {
593 protected:
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;
601
602 public:
603 class InnerIterator {
604 public:
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),
609 m_value(Scalar(0)) {
610 this->operator++();
611 }
612
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());
617 ++m_lhsIter;
618 ++m_rhsIter;
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();
622 ++m_lhsIter;
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();
626 ++m_rhsIter;
627 } else {
628 m_id = -1;
629 }
630 return *this;
631 }
632
633 EIGEN_STRONG_INLINE Scalar value() const { return m_value; }
634
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(); }
639
640 EIGEN_STRONG_INLINE operator bool() const { return m_id >= 0; }
641
642 protected:
643 LhsIterator m_lhsIter;
644 RhsIterator m_rhsIter;
645 const BinaryOp& m_functor;
646 Scalar m_value;
647 StorageIndex m_id;
648 };
649
650 enum {
651 CoeffReadCost = int(evaluator<LhsArg>::CoeffReadCost) + int(evaluator<RhsArg>::CoeffReadCost) +
652 int(functor_traits<BinaryOp>::Cost),
653 Flags = XprType::Flags
654 };
655
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);
660 }
661
662 inline Index nonZerosEstimate() const { return m_lhsImpl.nonZerosEstimate() + m_rhsImpl.nonZerosEstimate(); }
663
664 protected:
665 const BinaryOp m_functor;
666 evaluator<LhsArg> m_lhsImpl;
667 evaluator<RhsArg> m_rhsImpl;
668};
669
670// "dense v sparse"
671template <typename XprType>
672struct sparse_disjunction_evaluator<XprType, IndexBased, IteratorBased> : evaluator_base<XprType> {
673 protected:
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;
680
681 public:
682 class InnerIterator {
683 enum { IsRowMajor = (int(RhsArg::Flags) & RowMajorBit) == RowMajorBit };
684
685 public:
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),
690 m_value(0),
691 m_id(-1),
692 m_innerSize(aEval.m_expr.rhs().innerSize()) {
693 this->operator++();
694 }
695
696 EIGEN_STRONG_INLINE InnerIterator& operator++() {
697 ++m_id;
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());
702 ++m_rhsIter;
703 } else
704 m_value = lhsVal;
705 }
706
707 return *this;
708 }
709
710 EIGEN_STRONG_INLINE Scalar value() const {
711 eigen_internal_assert(m_id < m_innerSize);
712 return m_value;
713 }
714
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(); }
719
720 EIGEN_STRONG_INLINE operator bool() const { return m_id < m_innerSize; }
721
722 protected:
723 const evaluator<LhsArg>& m_lhsEval;
724 RhsIterator m_rhsIter;
725 const BinaryOp& m_functor;
726 Scalar m_value;
727 StorageIndex m_id;
728 StorageIndex m_innerSize;
729 };
730
731 enum {
732 CoeffReadCost = int(evaluator<LhsArg>::CoeffReadCost) + int(evaluator<RhsArg>::CoeffReadCost) +
733 int(functor_traits<BinaryOp>::Cost),
734 Flags = XprType::Flags
735 };
736
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);
741 }
742
743 inline Index nonZerosEstimate() const { return m_expr.size(); }
744
745 protected:
746 const BinaryOp m_functor;
747 evaluator<LhsArg> m_lhsImpl;
748 evaluator<RhsArg> m_rhsImpl;
749 const XprType& m_expr;
750};
751
752// "sparse v dense"
753template <typename XprType>
754struct sparse_disjunction_evaluator<XprType, IteratorBased, IndexBased> : evaluator_base<XprType> {
755 protected:
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;
762
763 public:
764 class InnerIterator {
765 enum { IsRowMajor = (int(LhsArg::Flags) & RowMajorBit) == RowMajorBit };
766
767 public:
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),
772 m_value(0),
773 m_id(-1),
774 m_innerSize(aEval.m_expr.lhs().innerSize()) {
775 this->operator++();
776 }
777
778 EIGEN_STRONG_INLINE InnerIterator& operator++() {
779 ++m_id;
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);
784 ++m_lhsIter;
785 } else
786 m_value = rhsVal;
787 }
788
789 return *this;
790 }
791
792 EIGEN_STRONG_INLINE Scalar value() const {
793 eigen_internal_assert(m_id < m_innerSize);
794 return m_value;
795 }
796
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(); }
801
802 EIGEN_STRONG_INLINE operator bool() const { return m_id < m_innerSize; }
803
804 protected:
805 LhsIterator m_lhsIter;
806 const evaluator<RhsArg>& m_rhsEval;
807 const BinaryOp& m_functor;
808 Scalar m_value;
809 StorageIndex m_id;
810 StorageIndex m_innerSize;
811 };
812
813 enum {
814 CoeffReadCost = int(evaluator<LhsArg>::CoeffReadCost) + int(evaluator<RhsArg>::CoeffReadCost) +
815 int(functor_traits<BinaryOp>::Cost),
816 Flags = XprType::Flags
817 };
818
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);
823 }
824
825 inline Index nonZerosEstimate() const { return m_expr.size(); }
826
827 protected:
828 const BinaryOp m_functor;
829 evaluator<LhsArg> m_lhsImpl;
830 evaluator<RhsArg> m_rhsImpl;
831 const XprType& m_expr;
832};
833
834// when DupFunc is wrapped with scalar_dup_op, use disjunction evaluator
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) {}
841};
842} // namespace internal
843
844/***************************************************************************
845 * Implementation of SparseMatrixBase and SparseCwise functions/operators
846 ***************************************************************************/
847
848template <typename Derived>
849template <typename OtherDerived>
850Derived& SparseMatrixBase<Derived>::operator+=(const EigenBase<OtherDerived>& other) {
851 call_assignment(derived(), other.derived(), internal::add_assign_op<Scalar, typename OtherDerived::Scalar>());
852 return derived();
853}
854
855template <typename Derived>
856template <typename OtherDerived>
857Derived& SparseMatrixBase<Derived>::operator-=(const EigenBase<OtherDerived>& other) {
858 call_assignment(derived(), other.derived(), internal::sub_assign_op<Scalar, typename OtherDerived::Scalar>());
859 return derived();
860}
861
862template <typename Derived>
863template <typename OtherDerived>
864EIGEN_STRONG_INLINE Derived& SparseMatrixBase<Derived>::operator-=(const SparseMatrixBase<OtherDerived>& other) {
865 return derived() = derived() - other.derived();
866}
867
868template <typename Derived>
869template <typename OtherDerived>
870EIGEN_STRONG_INLINE Derived& SparseMatrixBase<Derived>::operator+=(const SparseMatrixBase<OtherDerived>& other) {
871 return derived() = derived() + other.derived();
872}
873
874template <typename Derived>
875template <typename OtherDerived>
876Derived& SparseMatrixBase<Derived>::operator+=(const DiagonalBase<OtherDerived>& other) {
877 call_assignment_no_alias(derived(), other.derived(),
878 internal::add_assign_op<Scalar, typename OtherDerived::Scalar>());
879 return derived();
880}
881
882template <typename Derived>
883template <typename OtherDerived>
884Derived& SparseMatrixBase<Derived>::operator-=(const DiagonalBase<OtherDerived>& other) {
885 call_assignment_no_alias(derived(), other.derived(),
886 internal::sub_assign_op<Scalar, typename OtherDerived::Scalar>());
887 return derived();
888}
889
890template <typename Derived>
891template <typename OtherDerived>
892EIGEN_STRONG_INLINE const typename SparseMatrixBase<Derived>::template CwiseProductDenseReturnType<OtherDerived>::Type
894 return typename CwiseProductDenseReturnType<OtherDerived>::Type(derived(), other.derived());
895}
896
897template <typename DenseDerived, typename SparseDerived>
898EIGEN_STRONG_INLINE const
900 const DenseDerived, const SparseDerived>
901 operator+(const MatrixBase<DenseDerived>& a, const SparseMatrixBase<SparseDerived>& b) {
903 const DenseDerived, const SparseDerived>(a.derived(), b.derived());
904}
905
906template <typename SparseDerived, typename DenseDerived>
907EIGEN_STRONG_INLINE const
909 const SparseDerived, const DenseDerived>
910 operator+(const SparseMatrixBase<SparseDerived>& a, const MatrixBase<DenseDerived>& b) {
912 const SparseDerived, const DenseDerived>(a.derived(), b.derived());
913}
914
915template <typename DenseDerived, typename SparseDerived>
916EIGEN_STRONG_INLINE const
918 const DenseDerived, const SparseDerived>
919 operator-(const MatrixBase<DenseDerived>& a, const SparseMatrixBase<SparseDerived>& b) {
921 const DenseDerived, const SparseDerived>(a.derived(), b.derived());
922}
923
924template <typename SparseDerived, typename DenseDerived>
925EIGEN_STRONG_INLINE const
927 const SparseDerived, const DenseDerived>
928 operator-(const SparseMatrixBase<SparseDerived>& a, const MatrixBase<DenseDerived>& b) {
930 const SparseDerived, const DenseDerived>(a.derived(), b.derived());
931}
932
933} // end namespace Eigen
934
935#endif // EIGEN_SPARSE_CWISE_BINARY_OP_H
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