Medial Code Documentation
Loading...
Searching...
No Matches
SparseDiagonalProduct.h
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
13namespace 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
27namespace internal {
28
29enum {
30 SDP_AsScalarProduct,
31 SDP_AsCwiseProduct
32};
33
34template<typename SparseXprType, typename DiagonalCoeffType, int SDP_Tag>
36
37template<typename Lhs, typename Rhs, int ProductTag>
38struct product_evaluator<Product<Lhs, Rhs, DefaultProduct>, ProductTag, DiagonalShape, SparseShape>
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
48template<typename Lhs, typename Rhs, int ProductTag>
49struct product_evaluator<Product<Lhs, Rhs, DefaultProduct>, ProductTag, SparseShape, DiagonalShape>
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
59template<typename SparseXprType, typename DiagonalCoeffType>
61{
62protected:
64 typedef typename SparseXprType::Scalar Scalar;
65
66public:
67 class InnerIterator : public SparseXprInnerIterator
68 {
69 public:
71 : SparseXprInnerIterator(xprEval.m_sparseXprImpl, outer),
72 m_coeff(xprEval.m_diagCoeffImpl.coeff(outer))
73 {}
74
75 EIGEN_STRONG_INLINE Scalar value() const { return m_coeff * SparseXprInnerIterator::value(); }
76 protected:
77 typename DiagonalCoeffType::Scalar m_coeff;
78 };
79
81 : m_sparseXprImpl(sparseXpr), m_diagCoeffImpl(diagCoeff)
82 {}
83
84protected:
85 evaluator<SparseXprType> m_sparseXprImpl;
86 evaluator<DiagonalCoeffType> m_diagCoeffImpl;
87};
88
89
90template<typename SparseXprType, typename DiagCoeffType>
92{
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 DiagCoeffNested;
98
100 {
102 public:
104 : m_sparseIter(xprEval.m_sparseXprEval, outer), m_diagCoeffNested(xprEval.m_diagCoeffNested)
105 {}
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
113 EIGEN_STRONG_INLINE InnerIterator& operator++() { ++m_sparseIter; return *this; }
114 inline operator bool() const { return m_sparseIter; }
115
116 protected:
117 SparseXprIter m_sparseIter;
118 DiagCoeffNested m_diagCoeffNested;
119 };
120
122 : m_sparseXprEval(sparseXpr), m_diagCoeffNested(diagCoeff)
123 {}
124
125protected:
126 evaluator<SparseXprType> m_sparseXprEval;
127 DiagCoeffNested m_diagCoeffNested;
128};
129
130} // end namespace internal
131
132} // end namespace Eigen
133
134#endif // EIGEN_SPARSE_DIAGONAL_PRODUCT_H
An InnerIterator allows to loop over the element of any matrix expression.
Definition CoreIterators.h:34
Expression of the product of two arbitrary matrices or vectors.
Definition Product.h:111
Pseudo expression representing a solving operation.
Definition Solve.h:63
const unsigned int RowMajorBit
for a matrix, this means that the storage order is row-major.
Definition Constants.h:61
Definition Constants.h:514
Definition Constants.h:520
Definition CoreEvaluators.h:82
Definition XprHelper.h:398
Definition ForwardDeclarations.h:165
Definition SparseDiagonalProduct.h:35