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 namespace Eigen {
14 
15 // The product of a diagonal matrix with a sparse matrix can be easily
16 // implemented using expression template.
17 // We have two consider very different cases:
18 // 1 - diag * row-major sparse
19 // => each inner vector <=> scalar * sparse vector product
20 // => so we can reuse CwiseUnaryOp::InnerIterator
21 // 2 - diag * col-major sparse
22 // => each inner vector <=> densevector * sparse vector cwise product
23 // => again, we can reuse specialization of CwiseBinaryOp::InnerIterator
24 // for that particular case
25 // The two other cases are symmetric.
26 
27 namespace internal {
28 
29 enum {
32 };
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, Rhs::Flags&RowMajorBit?SDP_AsScalarProduct:SDP_AsCwiseProduct>
40 {
42  enum { CoeffReadCost = HugeCost, Flags = Rhs::Flags&RowMajorBit, Alignment = 0 }; // FIXME CoeffReadCost & Flags
43 
45  explicit product_evaluator(const XprType& xpr) : Base(xpr.rhs(), xpr.lhs().diagonal()) {}
46 };
47 
48 template<typename Lhs, typename Rhs, int ProductTag>
50  : public sparse_diagonal_product_evaluator<Lhs, Transpose<const typename Rhs::DiagonalVectorType>, Lhs::Flags&RowMajorBit?SDP_AsCwiseProduct:SDP_AsScalarProduct>
51 {
53  enum { CoeffReadCost = HugeCost, Flags = Lhs::Flags&RowMajorBit, Alignment = 0 }; // FIXME CoeffReadCost & Flags
54 
56  explicit product_evaluator(const XprType& xpr) : Base(xpr.lhs(), xpr.rhs().diagonal().transpose()) {}
57 };
58 
59 template<typename SparseXprType, typename DiagonalCoeffType>
60 struct sparse_diagonal_product_evaluator<SparseXprType, DiagonalCoeffType, SDP_AsScalarProduct>
61 {
62 protected:
64  typedef typename SparseXprType::Scalar Scalar;
65 
66 public:
68  {
69  public:
71  : SparseXprInnerIterator(xprEval.m_sparseXprImpl, outer),
72  m_coeff(xprEval.m_diagCoeffImpl.coeff(outer))
73  {}
74 
76  protected:
78  };
79 
80  sparse_diagonal_product_evaluator(const SparseXprType &sparseXpr, const DiagonalCoeffType &diagCoeff)
81  : m_sparseXprImpl(sparseXpr), m_diagCoeffImpl(diagCoeff)
82  {}
83 
84  Index nonZerosEstimate() const { return m_sparseXprImpl.nonZerosEstimate(); }
85 
86 protected:
89 };
90 
91 
92 template<typename SparseXprType, typename DiagCoeffType>
93 struct sparse_diagonal_product_evaluator<SparseXprType, DiagCoeffType, SDP_AsCwiseProduct>
94 {
95  typedef typename SparseXprType::Scalar Scalar;
96  typedef typename SparseXprType::StorageIndex StorageIndex;
97 
98  typedef typename nested_eval<DiagCoeffType,SparseXprType::IsRowMajor ? SparseXprType::RowsAtCompileTime
99  : SparseXprType::ColsAtCompileTime>::type DiagCoeffNested;
100 
102  {
104  public:
106  : m_sparseIter(xprEval.m_sparseXprEval, outer), m_diagCoeffNested(xprEval.m_diagCoeffNested)
107  {}
108 
109  inline Scalar value() const { return m_sparseIter.value() * m_diagCoeffNested.coeff(index()); }
110  inline StorageIndex index() const { return m_sparseIter.index(); }
111  inline Index outer() const { return m_sparseIter.outer(); }
112  inline Index col() const { return SparseXprType::IsRowMajor ? m_sparseIter.index() : m_sparseIter.outer(); }
113  inline Index row() const { return SparseXprType::IsRowMajor ? m_sparseIter.outer() : m_sparseIter.index(); }
114 
115  EIGEN_STRONG_INLINE InnerIterator& operator++() { ++m_sparseIter; return *this; }
116  inline operator bool() const { return m_sparseIter; }
117 
118  protected:
121  };
122 
123  sparse_diagonal_product_evaluator(const SparseXprType &sparseXpr, const DiagCoeffType &diagCoeff)
124  : m_sparseXprEval(sparseXpr), m_diagCoeffNested(diagCoeff)
125  {}
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
Eigen::internal::sparse_diagonal_product_evaluator< SparseXprType, DiagCoeffType, SDP_AsCwiseProduct >::DiagCoeffNested
nested_eval< DiagCoeffType, SparseXprType::IsRowMajor ? SparseXprType::RowsAtCompileTime :SparseXprType::ColsAtCompileTime >::type DiagCoeffNested
Definition: SparseDiagonalProduct.h:99
Eigen::DefaultProduct
@ DefaultProduct
Definition: Constants.h:500
Eigen::internal::Lhs
@ Lhs
Definition: TensorContractionMapper.h:19
Eigen::HugeCost
const int HugeCost
Definition: Constants.h:44
Eigen
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
Eigen::internal::sparse_diagonal_product_evaluator< SparseXprType, DiagCoeffType, SDP_AsCwiseProduct >::m_sparseXprEval
evaluator< SparseXprType > m_sparseXprEval
Definition: SparseDiagonalProduct.h:130
Eigen::internal::sparse_diagonal_product_evaluator< SparseXprType, DiagonalCoeffType, SDP_AsScalarProduct >::m_sparseXprImpl
evaluator< SparseXprType > m_sparseXprImpl
Definition: SparseDiagonalProduct.h:87
Eigen::internal::product_evaluator< Product< Lhs, Rhs, DefaultProduct >, ProductTag, DiagonalShape, SparseShape >::Base
sparse_diagonal_product_evaluator< Rhs, typename Lhs::DiagonalVectorType, Rhs::Flags &RowMajorBit?SDP_AsScalarProduct:SDP_AsCwiseProduct > Base
Definition: SparseDiagonalProduct.h:44
Eigen::RowMajorBit
const unsigned int RowMajorBit
Definition: Constants.h:66
Eigen::internal::sparse_diagonal_product_evaluator< SparseXprType, DiagCoeffType, SDP_AsCwiseProduct >::InnerIterator::index
StorageIndex index() const
Definition: SparseDiagonalProduct.h:110
type
Definition: pytypes.h:1525
Eigen::internal::nested_eval
Definition: XprHelper.h:457
Eigen::internal::sparse_diagonal_product_evaluator< SparseXprType, DiagCoeffType, SDP_AsCwiseProduct >::sparse_diagonal_product_evaluator
sparse_diagonal_product_evaluator(const SparseXprType &sparseXpr, const DiagCoeffType &diagCoeff)
Definition: SparseDiagonalProduct.h:123
diagonal
void diagonal(const MatrixType &m)
Definition: diagonal.cpp:12
Eigen::internal::product_evaluator< Product< Lhs, Rhs, DefaultProduct >, ProductTag, SparseShape, DiagonalShape >::Base
sparse_diagonal_product_evaluator< Lhs, Transpose< const typename Rhs::DiagonalVectorType >, Lhs::Flags &RowMajorBit?SDP_AsCwiseProduct:SDP_AsScalarProduct > Base
Definition: SparseDiagonalProduct.h:55
Eigen::internal::sparse_diagonal_product_evaluator< SparseXprType, DiagonalCoeffType, SDP_AsScalarProduct >::InnerIterator::value
EIGEN_STRONG_INLINE Scalar value() const
Definition: SparseDiagonalProduct.h:75
Eigen::internal::sparse_diagonal_product_evaluator< SparseXprType, DiagCoeffType, SDP_AsCwiseProduct >::InnerIterator::m_sparseIter
SparseXprIter m_sparseIter
Definition: SparseDiagonalProduct.h:119
Eigen::internal::product_evaluator< Product< Lhs, Rhs, DefaultProduct >, ProductTag, SparseShape, DiagonalShape >::product_evaluator
product_evaluator(const XprType &xpr)
Definition: SparseDiagonalProduct.h:56
Eigen::internal::sparse_diagonal_product_evaluator
Definition: SparseDiagonalProduct.h:35
Eigen::internal::sparse_diagonal_product_evaluator< SparseXprType, DiagonalCoeffType, SDP_AsScalarProduct >::nonZerosEstimate
Index nonZerosEstimate() const
Definition: SparseDiagonalProduct.h:84
Eigen::internal::sparse_diagonal_product_evaluator< SparseXprType, DiagonalCoeffType, SDP_AsScalarProduct >::m_diagCoeffImpl
evaluator< DiagonalCoeffType > m_diagCoeffImpl
Definition: SparseDiagonalProduct.h:88
Eigen::SparseShape
Definition: Constants.h:537
EIGEN_STRONG_INLINE
#define EIGEN_STRONG_INLINE
Definition: Macros.h:917
Eigen::internal::product_evaluator
Definition: ForwardDeclarations.h:164
Eigen::internal::product_evaluator< Product< Lhs, Rhs, DefaultProduct >, ProductTag, SparseShape, DiagonalShape >::XprType
Product< Lhs, Rhs, DefaultProduct > XprType
Definition: SparseDiagonalProduct.h:52
Eigen::Product
Expression of the product of two arbitrary matrices or vectors.
Definition: Product.h:71
Eigen::internal::sparse_diagonal_product_evaluator< SparseXprType, DiagCoeffType, SDP_AsCwiseProduct >::Scalar
SparseXprType::Scalar Scalar
Definition: SparseDiagonalProduct.h:95
Eigen::internal::evaluator
Definition: CoreEvaluators.h:90
Eigen::InnerIterator
An InnerIterator allows to loop over the element of any matrix expression.
Definition: CoreIterators.h:33
Eigen::internal::sparse_diagonal_product_evaluator< SparseXprType, DiagonalCoeffType, SDP_AsScalarProduct >::sparse_diagonal_product_evaluator
sparse_diagonal_product_evaluator(const SparseXprType &sparseXpr, const DiagonalCoeffType &diagCoeff)
Definition: SparseDiagonalProduct.h:80
Eigen::internal::sparse_diagonal_product_evaluator< SparseXprType, DiagCoeffType, SDP_AsCwiseProduct >::InnerIterator::InnerIterator
InnerIterator(const sparse_diagonal_product_evaluator &xprEval, Index outer)
Definition: SparseDiagonalProduct.h:105
Eigen::internal::sparse_diagonal_product_evaluator< SparseXprType, DiagCoeffType, SDP_AsCwiseProduct >::InnerIterator::operator++
EIGEN_STRONG_INLINE InnerIterator & operator++()
Definition: SparseDiagonalProduct.h:115
Eigen::internal::sparse_diagonal_product_evaluator< SparseXprType, DiagCoeffType, SDP_AsCwiseProduct >::InnerIterator::col
Index col() const
Definition: SparseDiagonalProduct.h:112
Eigen::internal::sparse_diagonal_product_evaluator< SparseXprType, DiagCoeffType, SDP_AsCwiseProduct >::InnerIterator::SparseXprIter
evaluator< SparseXprType >::InnerIterator SparseXprIter
Definition: SparseDiagonalProduct.h:103
Eigen::internal::sparse_diagonal_product_evaluator< SparseXprType, DiagCoeffType, SDP_AsCwiseProduct >::nonZerosEstimate
Index nonZerosEstimate() const
Definition: SparseDiagonalProduct.h:127
Eigen::internal::sparse_diagonal_product_evaluator< SparseXprType, DiagCoeffType, SDP_AsCwiseProduct >::m_diagCoeffNested
DiagCoeffNested m_diagCoeffNested
Definition: SparseDiagonalProduct.h:131
Eigen::internal::sparse_diagonal_product_evaluator< SparseXprType, DiagonalCoeffType, SDP_AsScalarProduct >::SparseXprInnerIterator
evaluator< SparseXprType >::InnerIterator SparseXprInnerIterator
Definition: SparseDiagonalProduct.h:63
Eigen::internal::sparse_diagonal_product_evaluator< SparseXprType, DiagCoeffType, SDP_AsCwiseProduct >::InnerIterator::row
Index row() const
Definition: SparseDiagonalProduct.h:113
Eigen::internal::sparse_diagonal_product_evaluator< SparseXprType, DiagCoeffType, SDP_AsCwiseProduct >::StorageIndex
SparseXprType::StorageIndex StorageIndex
Definition: SparseDiagonalProduct.h:96
Eigen::internal::sparse_diagonal_product_evaluator< SparseXprType, DiagonalCoeffType, SDP_AsScalarProduct >::InnerIterator::m_coeff
DiagonalCoeffType::Scalar m_coeff
Definition: SparseDiagonalProduct.h:77
Eigen::internal::Rhs
@ Rhs
Definition: TensorContractionMapper.h:18
Eigen::internal::sparse_diagonal_product_evaluator< SparseXprType, DiagCoeffType, SDP_AsCwiseProduct >::InnerIterator::m_diagCoeffNested
DiagCoeffNested m_diagCoeffNested
Definition: SparseDiagonalProduct.h:120
Eigen::internal::product_evaluator< Product< Lhs, Rhs, DefaultProduct >, ProductTag, DiagonalShape, SparseShape >::XprType
Product< Lhs, Rhs, DefaultProduct > XprType
Definition: SparseDiagonalProduct.h:41
Eigen::internal::SDP_AsScalarProduct
@ SDP_AsScalarProduct
Definition: SparseDiagonalProduct.h:30
Eigen::internal::sparse_diagonal_product_evaluator< SparseXprType, DiagCoeffType, SDP_AsCwiseProduct >::InnerIterator::value
Scalar value() const
Definition: SparseDiagonalProduct.h:109
Eigen::internal::sparse_diagonal_product_evaluator< SparseXprType, DiagonalCoeffType, SDP_AsScalarProduct >::Scalar
SparseXprType::Scalar Scalar
Definition: SparseDiagonalProduct.h:64
internal
Definition: BandTriangularSolver.h:13
Eigen::internal::SDP_AsCwiseProduct
@ SDP_AsCwiseProduct
Definition: SparseDiagonalProduct.h:31
Eigen::internal::sparse_diagonal_product_evaluator< SparseXprType, DiagonalCoeffType, SDP_AsScalarProduct >::InnerIterator::InnerIterator
InnerIterator(const sparse_diagonal_product_evaluator &xprEval, Index outer)
Definition: SparseDiagonalProduct.h:70
test_callbacks.value
value
Definition: test_callbacks.py:160
Eigen::internal::product_evaluator< Product< Lhs, Rhs, DefaultProduct >, ProductTag, DiagonalShape, SparseShape >::product_evaluator
product_evaluator(const XprType &xpr)
Definition: SparseDiagonalProduct.h:45
Scalar
SCALAR Scalar
Definition: bench_gemm.cpp:46
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
Eigen::DiagonalShape
Definition: Constants.h:531
Eigen::internal::sparse_diagonal_product_evaluator< SparseXprType, DiagCoeffType, SDP_AsCwiseProduct >::InnerIterator::outer
Index outer() const
Definition: SparseDiagonalProduct.h:111


gtsam
Author(s):
autogenerated on Thu Dec 19 2024 04:03:20