SparseDenseProduct.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) 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 {
18 };
19 
20 template<typename Lhs, typename Rhs> struct SparseDenseProductReturnType<Lhs,Rhs,1>
21 {
23 };
24 
25 template<typename Lhs, typename Rhs, int InnerSize> struct DenseSparseProductReturnType
26 {
28 };
29 
30 template<typename Lhs, typename Rhs> struct DenseSparseProductReturnType<Lhs,Rhs,1>
31 {
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;
43  typedef typename Lhs::Index Index;
44  typedef typename Lhs::Nested LhsNested;
45  typedef typename Rhs::Nested RhsNested;
48 
49  enum {
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>
68  : public SparseMatrixBase<SparseDenseOuterProduct<Lhs,Rhs,Tr> >
69 {
70  public:
71 
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;
115  public:
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  int m_outer;
129  Scalar m_factor;
130 };
131 
132 namespace internal {
133 template<typename Lhs, typename Rhs>
135  : traits<ProductBase<SparseTimeDenseProduct<Lhs,Rhs>, Lhs, Rhs> >
136 {
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>
145 
146 template<typename SparseLhsType, typename DenseRhsType, typename DenseResType>
147 struct sparse_time_dense_product_impl<SparseLhsType,DenseRhsType,DenseResType, RowMajor, true>
148 {
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  int 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 {
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 {
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 {
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 {
234 }
235 
236 } // end namespace internal
237 
238 template<typename Lhs, typename Rhs>
240  : public ProductBase<SparseTimeDenseProduct<Lhs,Rhs>, Lhs, Rhs>
241 {
242  public:
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:
255 };
256 
257 
258 // dense = dense * sparse
259 namespace internal {
260 template<typename Lhs, typename Rhs>
262  : traits<ProductBase<DenseTimeSparseProduct<Lhs,Rhs>, Lhs, Rhs> >
263 {
265 };
266 } // end namespace internal
267 
268 template<typename Lhs, typename Rhs>
270  : public ProductBase<DenseTimeSparseProduct<Lhs,Rhs>, Lhs, Rhs>
271 {
272  public:
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:
288 };
289 
290 // sparse * dense
291 template<typename Derived>
292 template<typename OtherDerived>
295 {
296  return typename SparseDenseProductReturnType<Derived,OtherDerived>::Type(derived(), other.derived());
297 }
298 
299 } // end namespace Eigen
300 
301 #endif // EIGEN_SPARSEDENSEPRODUCT_H
Block< Derived, 1, internal::traits< Derived >::ColsAtCompileTime, IsRowMajor > RowXpr
Definition: BlockMethods.h:18
void sparse_time_dense_product(const SparseLhsType &lhs, const DenseRhsType &rhs, DenseResType &res, const AlphaType &alpha)
#define EIGEN_PRODUCT_PUBLIC_INTERFACE(Derived)
Definition: ProductBase.h:46
return ReturnType(abs2(x.value()), x.derivatives()*(Scalar(2)*x.value()))
#define EIGEN_STRONG_INLINE
internal::traits< Derived >::Scalar Scalar
Definition: DenseBase.h:63
EIGEN_STRONG_INLINE Index rows() const
EIGEN_STRONG_INLINE Index cols() const
SparseTimeDenseProduct< Lhs, Rhs > Type
Expression of the transpose of a matrix.
Definition: Transpose.h:57
iterative scaling algorithm to equilibrate rows and column norms in matrices
Definition: matrix.hpp:471
SparseMatrixBase< SparseDenseOuterProduct > Base
SparseDenseOuterProduct< Lhs, Rhs, false > Type
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:88
#define EIGEN_STATIC_ASSERT(CONDITION, MSG)
Definition: StaticAssert.h:111
EIGEN_STRONG_INLINE InnerIterator(const SparseDenseOuterProduct &prod, Index outer)
const unsigned int RowMajorBit
scalar_product_traits< typename traits< Lhs >::Scalar, typename traits< Rhs >::Scalar >::ReturnType Scalar
EIGEN_STRONG_INLINE const _LhsNested & lhs() const
Base class of any sparse matrices or sparse expressions.
void scaleAndAddTo(Dest &dest, const Scalar &alpha) const
static void run(const SparseLhsType &lhs, const DenseRhsType &rhs, DenseResType &res, const typename Res::Scalar &alpha)
DenseTimeSparseProduct< Lhs, Rhs > Type
const ScalarMultipleReturnType operator*(const Scalar &scalar) const
EIGEN_STRONG_INLINE SparseDenseOuterProduct(const Rhs &rhs, const Lhs &lhs)
const Block< const Derived, 1, internal::traits< Derived >::ColsAtCompileTime, IsRowMajor > ConstRowXpr
Definition: BlockMethods.h:19
internal::traits< Derived >::Index Index
Definition: EigenBase.h:31
void rhs(const real_t *x, real_t *f)
#define EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
RowXpr row(Index i)
Definition: BlockMethods.h:725
static void run(const SparseLhsType &lhs, const DenseRhsType &rhs, DenseResType &res, const typename Res::Scalar &alpha)
EIGEN_STRONG_INLINE const _RhsNested & rhs() const
static void run(const SparseLhsType &lhs, const DenseRhsType &rhs, DenseResType &res, const typename Res::Scalar &alpha)
SparseDenseOuterProduct< Rhs, Lhs, true > Type
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
static void run(const SparseLhsType &lhs, const DenseRhsType &rhs, DenseResType &res, const typename Res::Scalar &alpha)
void scaleAndAddTo(Dest &dest, const Scalar &alpha) const


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Mon Jun 10 2019 12:35:07