SparseDiagonalProduct.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2009-2015 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 
10 #ifndef EIGEN_SPARSE_DIAGONAL_PRODUCT_H
11 #define EIGEN_SPARSE_DIAGONAL_PRODUCT_H
12 
13 // IWYU pragma: private
14 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
18 // The product of a diagonal matrix with a sparse matrix can be easily
19 // implemented using expression template.
20 // We have two consider very different cases:
21 // 1 - diag * row-major sparse
22 // => each inner vector <=> scalar * sparse vector product
23 // => so we can reuse CwiseUnaryOp::InnerIterator
24 // 2 - diag * col-major sparse
25 // => each inner vector <=> densevector * sparse vector cwise product
26 // => again, we can reuse specialization of CwiseBinaryOp::InnerIterator
27 // for that particular case
28 // The two other cases are symmetric.
29 
30 namespace internal {
31 
33 
34 template <typename SparseXprType, typename DiagonalCoeffType, int SDP_Tag>
36 
37 template <typename Lhs, typename Rhs, int ProductTag>
39  : public sparse_diagonal_product_evaluator<Rhs, typename Lhs::DiagonalVectorType,
40  Rhs::Flags & RowMajorBit ? SDP_AsScalarProduct : SDP_AsCwiseProduct> {
42  enum { CoeffReadCost = HugeCost, Flags = Rhs::Flags & RowMajorBit, Alignment = 0 }; // FIXME CoeffReadCost & Flags
43 
44  typedef sparse_diagonal_product_evaluator<Rhs, typename Lhs::DiagonalVectorType,
47  explicit product_evaluator(const XprType &xpr) : Base(xpr.rhs(), xpr.lhs().diagonal()) {}
48 };
49 
50 template <typename Lhs, typename Rhs, int ProductTag>
52  : public sparse_diagonal_product_evaluator<Lhs, Transpose<const typename Rhs::DiagonalVectorType>,
53  Lhs::Flags & RowMajorBit ? SDP_AsCwiseProduct : SDP_AsScalarProduct> {
55  enum { CoeffReadCost = HugeCost, Flags = Lhs::Flags & RowMajorBit, Alignment = 0 }; // FIXME CoeffReadCost & Flags
56 
60  explicit product_evaluator(const XprType &xpr) : Base(xpr.lhs(), xpr.rhs().diagonal().transpose()) {}
61 };
62 
63 template <typename SparseXprType, typename DiagonalCoeffType>
64 struct sparse_diagonal_product_evaluator<SparseXprType, DiagonalCoeffType, SDP_AsScalarProduct> {
65  protected:
67  typedef typename SparseXprType::Scalar Scalar;
68 
69  public:
71  public:
73  : SparseXprInnerIterator(xprEval.m_sparseXprImpl, outer), m_coeff(xprEval.m_diagCoeffImpl.coeff(outer)) {}
74 
76 
77  protected:
79  };
80 
81  sparse_diagonal_product_evaluator(const SparseXprType &sparseXpr, const DiagonalCoeffType &diagCoeff)
82  : m_sparseXprImpl(sparseXpr), m_diagCoeffImpl(diagCoeff) {}
83 
84  Index nonZerosEstimate() const { return m_sparseXprImpl.nonZerosEstimate(); }
85 
86  protected:
89 };
90 
91 template <typename SparseXprType, typename DiagCoeffType>
92 struct sparse_diagonal_product_evaluator<SparseXprType, DiagCoeffType, SDP_AsCwiseProduct> {
93  typedef typename SparseXprType::Scalar Scalar;
94  typedef typename SparseXprType::StorageIndex StorageIndex;
95 
96  typedef typename nested_eval<DiagCoeffType, SparseXprType::IsRowMajor ? SparseXprType::RowsAtCompileTime
97  : SparseXprType::ColsAtCompileTime>::type
99 
102 
103  public:
105  : m_sparseIter(xprEval.m_sparseXprEval, outer), m_diagCoeffNested(xprEval.m_diagCoeffNested) {}
106 
107  inline Scalar value() const { return m_sparseIter.value() * m_diagCoeffNested.coeff(index()); }
108  inline StorageIndex index() const { return m_sparseIter.index(); }
109  inline Index outer() const { return m_sparseIter.outer(); }
110  inline Index col() const { return SparseXprType::IsRowMajor ? m_sparseIter.index() : m_sparseIter.outer(); }
111  inline Index row() const { return SparseXprType::IsRowMajor ? m_sparseIter.outer() : m_sparseIter.index(); }
112 
114  ++m_sparseIter;
115  return *this;
116  }
117  inline operator bool() const { return m_sparseIter; }
118 
119  protected:
122  };
123 
124  sparse_diagonal_product_evaluator(const SparseXprType &sparseXpr, const DiagCoeffType &diagCoeff)
125  : m_sparseXprEval(sparseXpr), m_diagCoeffNested(diagCoeff) {}
126 
127  Index nonZerosEstimate() const { return m_sparseXprEval.nonZerosEstimate(); }
128 
129  protected:
132 };
133 
134 } // end namespace internal
135 
136 } // end namespace Eigen
137 
138 #endif // EIGEN_SPARSE_DIAGONAL_PRODUCT_H
#define EIGEN_STRONG_INLINE
Definition: Macros.h:834
SCALAR Scalar
Definition: bench_gemm.cpp:45
An InnerIterator allows to loop over the element of any matrix expression.
Definition: CoreIterators.h:37
Expression of the product of two arbitrary matrices or vectors.
Definition: Product.h:202
EIGEN_STRONG_INLINE InnerIterator & operator++()
Definition: SparseDiagonalProduct.h:113
evaluator< SparseXprType >::InnerIterator SparseXprIter
Definition: SparseDiagonalProduct.h:101
InnerIterator(const sparse_diagonal_product_evaluator &xprEval, Index outer)
Definition: SparseDiagonalProduct.h:104
InnerIterator(const sparse_diagonal_product_evaluator &xprEval, Index outer)
Definition: SparseDiagonalProduct.h:72
void diagonal(const MatrixType &m)
Definition: diagonal.cpp:13
@ DefaultProduct
Definition: Constants.h:503
const unsigned int RowMajorBit
Definition: Constants.h:70
@ Lhs
Definition: TensorContractionMapper.h:20
@ Rhs
Definition: TensorContractionMapper.h:20
@ SDP_AsScalarProduct
Definition: SparseDiagonalProduct.h:32
@ SDP_AsCwiseProduct
Definition: SparseDiagonalProduct.h:32
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
squared absolute value
Definition: GlobalFunctions.h:87
const int HugeCost
Definition: Constants.h:48
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
Extend namespace for flags.
Definition: fsi_chan_precond_driver.cc:56
void transpose()
Definition: skew_symmetric_matrix3.cpp:135
type
Definition: compute_granudrum_aor.py:141
Definition: Eigen_Colamd.h:49
Definition: Constants.h:549
Definition: Constants.h:570
Definition: CoreEvaluators.h:104
Definition: XprHelper.h:533
sparse_diagonal_product_evaluator< Rhs, typename Lhs::DiagonalVectorType, Rhs::Flags &RowMajorBit ? SDP_AsScalarProduct :SDP_AsCwiseProduct > Base
Definition: SparseDiagonalProduct.h:46
sparse_diagonal_product_evaluator< Lhs, Transpose< const typename Rhs::DiagonalVectorType >, Lhs::Flags &RowMajorBit ? SDP_AsCwiseProduct :SDP_AsScalarProduct > Base
Definition: SparseDiagonalProduct.h:59
Definition: ForwardDeclarations.h:221
evaluator< SparseXprType > m_sparseXprEval
Definition: SparseDiagonalProduct.h:130
nested_eval< DiagCoeffType, SparseXprType::IsRowMajor ? SparseXprType::RowsAtCompileTime :SparseXprType::ColsAtCompileTime >::type DiagCoeffNested
Definition: SparseDiagonalProduct.h:98
SparseXprType::StorageIndex StorageIndex
Definition: SparseDiagonalProduct.h:94
sparse_diagonal_product_evaluator(const SparseXprType &sparseXpr, const DiagCoeffType &diagCoeff)
Definition: SparseDiagonalProduct.h:124
sparse_diagonal_product_evaluator(const SparseXprType &sparseXpr, const DiagonalCoeffType &diagCoeff)
Definition: SparseDiagonalProduct.h:81
evaluator< DiagonalCoeffType > m_diagCoeffImpl
Definition: SparseDiagonalProduct.h:88
evaluator< SparseXprType >::InnerIterator SparseXprInnerIterator
Definition: SparseDiagonalProduct.h:66
Definition: SparseDiagonalProduct.h:35