Eigen  3.2.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
SparseDenseProduct.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008-2010 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_SPARSEDENSEPRODUCT_H
11 #define EIGEN_SPARSEDENSEPRODUCT_H
12 
13 namespace Eigen {
14 
15 template<typename Lhs, typename Rhs, int InnerSize> struct SparseDenseProductReturnType
16 {
17  typedef SparseTimeDenseProduct<Lhs,Rhs> Type;
18 };
19 
20 template<typename Lhs, typename Rhs> struct SparseDenseProductReturnType<Lhs,Rhs,1>
21 {
22  typedef SparseDenseOuterProduct<Lhs,Rhs,false> Type;
23 };
24 
25 template<typename Lhs, typename Rhs, int InnerSize> struct DenseSparseProductReturnType
26 {
27  typedef DenseTimeSparseProduct<Lhs,Rhs> Type;
28 };
29 
30 template<typename Lhs, typename Rhs> struct DenseSparseProductReturnType<Lhs,Rhs,1>
31 {
32  typedef SparseDenseOuterProduct<Rhs,Lhs,true> Type;
33 };
34 
35 namespace internal {
36 
37 template<typename Lhs, typename Rhs, bool Tr>
38 struct traits<SparseDenseOuterProduct<Lhs,Rhs,Tr> >
39 {
40  typedef Sparse StorageKind;
41  typedef typename scalar_product_traits<typename traits<Lhs>::Scalar,
42  typename traits<Rhs>::Scalar>::ReturnType Scalar;
43  typedef typename Lhs::Index Index;
44  typedef typename Lhs::Nested LhsNested;
45  typedef typename Rhs::Nested RhsNested;
46  typedef typename remove_all<LhsNested>::type _LhsNested;
47  typedef typename remove_all<RhsNested>::type _RhsNested;
48 
49  enum {
50  LhsCoeffReadCost = traits<_LhsNested>::CoeffReadCost,
51  RhsCoeffReadCost = traits<_RhsNested>::CoeffReadCost,
52 
53  RowsAtCompileTime = Tr ? int(traits<Rhs>::RowsAtCompileTime) : int(traits<Lhs>::RowsAtCompileTime),
54  ColsAtCompileTime = Tr ? int(traits<Lhs>::ColsAtCompileTime) : int(traits<Rhs>::ColsAtCompileTime),
55  MaxRowsAtCompileTime = Tr ? int(traits<Rhs>::MaxRowsAtCompileTime) : int(traits<Lhs>::MaxRowsAtCompileTime),
56  MaxColsAtCompileTime = Tr ? int(traits<Lhs>::MaxColsAtCompileTime) : int(traits<Rhs>::MaxColsAtCompileTime),
57 
58  Flags = Tr ? RowMajorBit : 0,
59 
60  CoeffReadCost = LhsCoeffReadCost + RhsCoeffReadCost + NumTraits<Scalar>::MulCost
61  };
62 };
63 
64 } // end namespace internal
65 
66 template<typename Lhs, typename Rhs, bool Tr>
67 class SparseDenseOuterProduct
68  : public SparseMatrixBase<SparseDenseOuterProduct<Lhs,Rhs,Tr> >
69 {
70  public:
71 
72  typedef SparseMatrixBase<SparseDenseOuterProduct> Base;
73  EIGEN_DENSE_PUBLIC_INTERFACE(SparseDenseOuterProduct)
74  typedef internal::traits<SparseDenseOuterProduct> Traits;
75 
76  private:
77 
78  typedef typename Traits::LhsNested LhsNested;
79  typedef typename Traits::RhsNested RhsNested;
80  typedef typename Traits::_LhsNested _LhsNested;
81  typedef typename Traits::_RhsNested _RhsNested;
82 
83  public:
84 
85  class InnerIterator;
86 
87  EIGEN_STRONG_INLINE SparseDenseOuterProduct(const Lhs& lhs, const Rhs& rhs)
88  : m_lhs(lhs), m_rhs(rhs)
89  {
90  EIGEN_STATIC_ASSERT(!Tr,YOU_MADE_A_PROGRAMMING_MISTAKE);
91  }
92 
93  EIGEN_STRONG_INLINE SparseDenseOuterProduct(const Rhs& rhs, const Lhs& lhs)
94  : m_lhs(lhs), m_rhs(rhs)
95  {
96  EIGEN_STATIC_ASSERT(Tr,YOU_MADE_A_PROGRAMMING_MISTAKE);
97  }
98 
99  EIGEN_STRONG_INLINE Index rows() const { return Tr ? m_rhs.rows() : m_lhs.rows(); }
100  EIGEN_STRONG_INLINE Index cols() const { return Tr ? m_lhs.cols() : m_rhs.cols(); }
101 
102  EIGEN_STRONG_INLINE const _LhsNested& lhs() const { return m_lhs; }
103  EIGEN_STRONG_INLINE const _RhsNested& rhs() const { return m_rhs; }
104 
105  protected:
106  LhsNested m_lhs;
107  RhsNested m_rhs;
108 };
109 
110 template<typename Lhs, typename Rhs, bool Transpose>
111 class SparseDenseOuterProduct<Lhs,Rhs,Transpose>::InnerIterator : public _LhsNested::InnerIterator
112 {
113  typedef typename _LhsNested::InnerIterator Base;
114  typedef typename SparseDenseOuterProduct::Index Index;
115  public:
116  EIGEN_STRONG_INLINE InnerIterator(const SparseDenseOuterProduct& prod, Index outer)
117  : Base(prod.lhs(), 0), m_outer(outer), m_factor(prod.rhs().coeff(outer))
118  {
119  }
120 
121  inline Index outer() const { return m_outer; }
122  inline Index row() const { return Transpose ? Base::row() : m_outer; }
123  inline Index col() const { return Transpose ? m_outer : Base::row(); }
124 
125  inline Scalar value() const { return Base::value() * m_factor; }
126 
127  protected:
128  Index m_outer;
129  Scalar m_factor;
130 };
131 
132 namespace internal {
133 template<typename Lhs, typename Rhs>
134 struct traits<SparseTimeDenseProduct<Lhs,Rhs> >
135  : traits<ProductBase<SparseTimeDenseProduct<Lhs,Rhs>, Lhs, Rhs> >
136 {
137  typedef Dense StorageKind;
138  typedef MatrixXpr XprKind;
139 };
140 
141 template<typename SparseLhsType, typename DenseRhsType, typename DenseResType,
142  int LhsStorageOrder = ((SparseLhsType::Flags&RowMajorBit)==RowMajorBit) ? RowMajor : ColMajor,
143  bool ColPerCol = ((DenseRhsType::Flags&RowMajorBit)==0) || DenseRhsType::ColsAtCompileTime==1>
144 struct sparse_time_dense_product_impl;
145 
146 template<typename SparseLhsType, typename DenseRhsType, typename DenseResType>
147 struct sparse_time_dense_product_impl<SparseLhsType,DenseRhsType,DenseResType, RowMajor, true>
148 {
149  typedef typename internal::remove_all<SparseLhsType>::type Lhs;
150  typedef typename internal::remove_all<DenseRhsType>::type Rhs;
151  typedef typename internal::remove_all<DenseResType>::type Res;
152  typedef typename Lhs::Index Index;
153  typedef typename Lhs::InnerIterator LhsInnerIterator;
154  static void run(const SparseLhsType& lhs, const DenseRhsType& rhs, DenseResType& res, const typename Res::Scalar& alpha)
155  {
156  for(Index c=0; c<rhs.cols(); ++c)
157  {
158  Index n = lhs.outerSize();
159  for(Index j=0; j<n; ++j)
160  {
161  typename Res::Scalar tmp(0);
162  for(LhsInnerIterator it(lhs,j); it ;++it)
163  tmp += it.value() * rhs.coeff(it.index(),c);
164  res.coeffRef(j,c) = alpha * tmp;
165  }
166  }
167  }
168 };
169 
170 template<typename SparseLhsType, typename DenseRhsType, typename DenseResType>
171 struct sparse_time_dense_product_impl<SparseLhsType,DenseRhsType,DenseResType, ColMajor, true>
172 {
173  typedef typename internal::remove_all<SparseLhsType>::type Lhs;
174  typedef typename internal::remove_all<DenseRhsType>::type Rhs;
175  typedef typename internal::remove_all<DenseResType>::type Res;
176  typedef typename Lhs::InnerIterator LhsInnerIterator;
177  typedef typename Lhs::Index Index;
178  static void run(const SparseLhsType& lhs, const DenseRhsType& rhs, DenseResType& res, const typename Res::Scalar& alpha)
179  {
180  for(Index c=0; c<rhs.cols(); ++c)
181  {
182  for(Index j=0; j<lhs.outerSize(); ++j)
183  {
184  typename Res::Scalar rhs_j = alpha * rhs.coeff(j,c);
185  for(LhsInnerIterator it(lhs,j); it ;++it)
186  res.coeffRef(it.index(),c) += it.value() * rhs_j;
187  }
188  }
189  }
190 };
191 
192 template<typename SparseLhsType, typename DenseRhsType, typename DenseResType>
193 struct sparse_time_dense_product_impl<SparseLhsType,DenseRhsType,DenseResType, RowMajor, false>
194 {
195  typedef typename internal::remove_all<SparseLhsType>::type Lhs;
196  typedef typename internal::remove_all<DenseRhsType>::type Rhs;
197  typedef typename internal::remove_all<DenseResType>::type Res;
198  typedef typename Lhs::InnerIterator LhsInnerIterator;
199  typedef typename Lhs::Index Index;
200  static void run(const SparseLhsType& lhs, const DenseRhsType& rhs, DenseResType& res, const typename Res::Scalar& alpha)
201  {
202  for(Index j=0; j<lhs.outerSize(); ++j)
203  {
204  typename Res::RowXpr res_j(res.row(j));
205  for(LhsInnerIterator it(lhs,j); it ;++it)
206  res_j += (alpha*it.value()) * rhs.row(it.index());
207  }
208  }
209 };
210 
211 template<typename SparseLhsType, typename DenseRhsType, typename DenseResType>
212 struct sparse_time_dense_product_impl<SparseLhsType,DenseRhsType,DenseResType, ColMajor, false>
213 {
214  typedef typename internal::remove_all<SparseLhsType>::type Lhs;
215  typedef typename internal::remove_all<DenseRhsType>::type Rhs;
216  typedef typename internal::remove_all<DenseResType>::type Res;
217  typedef typename Lhs::InnerIterator LhsInnerIterator;
218  typedef typename Lhs::Index Index;
219  static void run(const SparseLhsType& lhs, const DenseRhsType& rhs, DenseResType& res, const typename Res::Scalar& alpha)
220  {
221  for(Index j=0; j<lhs.outerSize(); ++j)
222  {
223  typename Rhs::ConstRowXpr rhs_j(rhs.row(j));
224  for(LhsInnerIterator it(lhs,j); it ;++it)
225  res.row(it.index()) += (alpha*it.value()) * rhs_j;
226  }
227  }
228 };
229 
230 template<typename SparseLhsType, typename DenseRhsType, typename DenseResType,typename AlphaType>
231 inline void sparse_time_dense_product(const SparseLhsType& lhs, const DenseRhsType& rhs, DenseResType& res, const AlphaType& alpha)
232 {
233  sparse_time_dense_product_impl<SparseLhsType,DenseRhsType,DenseResType>::run(lhs, rhs, res, alpha);
234 }
235 
236 } // end namespace internal
237 
238 template<typename Lhs, typename Rhs>
239 class SparseTimeDenseProduct
240  : public ProductBase<SparseTimeDenseProduct<Lhs,Rhs>, Lhs, Rhs>
241 {
242  public:
243  EIGEN_PRODUCT_PUBLIC_INTERFACE(SparseTimeDenseProduct)
244 
245  SparseTimeDenseProduct(const Lhs& lhs, const Rhs& rhs) : Base(lhs,rhs)
246  {}
247 
248  template<typename Dest> void scaleAndAddTo(Dest& dest, const Scalar& alpha) const
249  {
250  internal::sparse_time_dense_product(m_lhs, m_rhs, dest, alpha);
251  }
252 
253  private:
254  SparseTimeDenseProduct& operator=(const SparseTimeDenseProduct&);
255 };
256 
257 
258 // dense = dense * sparse
259 namespace internal {
260 template<typename Lhs, typename Rhs>
261 struct traits<DenseTimeSparseProduct<Lhs,Rhs> >
262  : traits<ProductBase<DenseTimeSparseProduct<Lhs,Rhs>, Lhs, Rhs> >
263 {
264  typedef Dense StorageKind;
265 };
266 } // end namespace internal
267 
268 template<typename Lhs, typename Rhs>
269 class DenseTimeSparseProduct
270  : public ProductBase<DenseTimeSparseProduct<Lhs,Rhs>, Lhs, Rhs>
271 {
272  public:
273  EIGEN_PRODUCT_PUBLIC_INTERFACE(DenseTimeSparseProduct)
274 
275  DenseTimeSparseProduct(const Lhs& lhs, const Rhs& rhs) : Base(lhs,rhs)
276  {}
277 
278  template<typename Dest> void scaleAndAddTo(Dest& dest, const Scalar& alpha) const
279  {
280  Transpose<const _LhsNested> lhs_t(m_lhs);
281  Transpose<const _RhsNested> rhs_t(m_rhs);
282  Transpose<Dest> dest_t(dest);
283  internal::sparse_time_dense_product(rhs_t, lhs_t, dest_t, alpha);
284  }
285 
286  private:
287  DenseTimeSparseProduct& operator=(const DenseTimeSparseProduct&);
288 };
289 
290 // sparse * dense
291 template<typename Derived>
292 template<typename OtherDerived>
293 inline const typename SparseDenseProductReturnType<Derived,OtherDerived>::Type
295 {
296  return typename SparseDenseProductReturnType<Derived,OtherDerived>::Type(derived(), other.derived());
297 }
298 
299 } // end namespace Eigen
300 
301 #endif // EIGEN_SPARSEDENSEPRODUCT_H
Definition: Constants.h:266
Definition: Constants.h:264
const ScalarMultipleReturnType operator*(const Scalar &scalar) const
Definition: SparseMatrixBase.h:50
const unsigned int RowMajorBit
Definition: Constants.h:53
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48