ProductBase.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-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_PRODUCTBASE_H
11 #define EIGEN_PRODUCTBASE_H
12 
13 namespace Eigen {
14 
20 namespace internal {
21 template<typename Derived, typename _Lhs, typename _Rhs>
22 struct traits<ProductBase<Derived,_Lhs,_Rhs> >
23 {
24  typedef MatrixXpr XprKind;
25  typedef typename remove_all<_Lhs>::type Lhs;
26  typedef typename remove_all<_Rhs>::type Rhs;
31  typename traits<Rhs>::Index>::type Index;
32  enum {
33  RowsAtCompileTime = traits<Lhs>::RowsAtCompileTime,
34  ColsAtCompileTime = traits<Rhs>::ColsAtCompileTime,
35  MaxRowsAtCompileTime = traits<Lhs>::MaxRowsAtCompileTime,
36  MaxColsAtCompileTime = traits<Rhs>::MaxColsAtCompileTime,
37  Flags = (MaxRowsAtCompileTime==1 ? RowMajorBit : 0)
39  // Note that EvalBeforeNestingBit and NestByRefBit
40  // are not used in practice because nested is overloaded for products
41  CoeffReadCost = 0 // FIXME why is it needed ?
42  };
43 };
44 }
45 
46 #define EIGEN_PRODUCT_PUBLIC_INTERFACE(Derived) \
47  typedef ProductBase<Derived, Lhs, Rhs > Base; \
48  EIGEN_DENSE_PUBLIC_INTERFACE(Derived) \
49  typedef typename Base::LhsNested LhsNested; \
50  typedef typename Base::_LhsNested _LhsNested; \
51  typedef typename Base::LhsBlasTraits LhsBlasTraits; \
52  typedef typename Base::ActualLhsType ActualLhsType; \
53  typedef typename Base::_ActualLhsType _ActualLhsType; \
54  typedef typename Base::RhsNested RhsNested; \
55  typedef typename Base::_RhsNested _RhsNested; \
56  typedef typename Base::RhsBlasTraits RhsBlasTraits; \
57  typedef typename Base::ActualRhsType ActualRhsType; \
58  typedef typename Base::_ActualRhsType _ActualRhsType; \
59  using Base::m_lhs; \
60  using Base::m_rhs;
61 
62 template<typename Derived, typename Lhs, typename Rhs>
63 class ProductBase : public MatrixBase<Derived>
64 {
65  public:
68 
69  typedef typename Lhs::Nested LhsNested;
70  typedef typename internal::remove_all<LhsNested>::type _LhsNested;
71  typedef internal::blas_traits<_LhsNested> LhsBlasTraits;
72  typedef typename LhsBlasTraits::DirectLinearAccessType ActualLhsType;
73  typedef typename internal::remove_all<ActualLhsType>::type _ActualLhsType;
74  typedef typename internal::traits<Lhs>::Scalar LhsScalar;
75 
76  typedef typename Rhs::Nested RhsNested;
77  typedef typename internal::remove_all<RhsNested>::type _RhsNested;
78  typedef internal::blas_traits<_RhsNested> RhsBlasTraits;
79  typedef typename RhsBlasTraits::DirectLinearAccessType ActualRhsType;
80  typedef typename internal::remove_all<ActualRhsType>::type _ActualRhsType;
81  typedef typename internal::traits<Rhs>::Scalar RhsScalar;
82 
83  // Diagonal of a product: no need to evaluate the arguments because they are going to be evaluated only once
84  typedef CoeffBasedProduct<LhsNested, RhsNested, 0> FullyLazyCoeffBaseProductType;
85 
86  public:
87 
88  typedef typename Base::PlainObject PlainObject;
89 
90  ProductBase(const Lhs& a_lhs, const Rhs& a_rhs)
91  : m_lhs(a_lhs), m_rhs(a_rhs)
92  {
93  eigen_assert(a_lhs.cols() == a_rhs.rows()
94  && "invalid matrix product"
95  && "if you wanted a coeff-wise or a dot product use the respective explicit functions");
96  }
97 
98  inline Index rows() const { return m_lhs.rows(); }
99  inline Index cols() const { return m_rhs.cols(); }
100 
101  template<typename Dest>
102  inline void evalTo(Dest& dst) const { dst.setZero(); scaleAndAddTo(dst,Scalar(1)); }
103 
104  template<typename Dest>
105  inline void addTo(Dest& dst) const { scaleAndAddTo(dst,Scalar(1)); }
106 
107  template<typename Dest>
108  inline void subTo(Dest& dst) const { scaleAndAddTo(dst,Scalar(-1)); }
109 
110  template<typename Dest>
111  inline void scaleAndAddTo(Dest& dst, const Scalar& alpha) const { derived().scaleAndAddTo(dst,alpha); }
112 
113  const _LhsNested& lhs() const { return m_lhs; }
114  const _RhsNested& rhs() const { return m_rhs; }
115 
116  // Implicit conversion to the nested type (trigger the evaluation of the product)
117  operator const PlainObject& () const
118  {
119  m_result.resize(m_lhs.rows(), m_rhs.cols());
120  derived().evalTo(m_result);
121  return m_result;
122  }
123 
125  { return FullyLazyCoeffBaseProductType(m_lhs, m_rhs); }
126 
127  template<int Index>
129  { return FullyLazyCoeffBaseProductType(m_lhs, m_rhs); }
130 
132  { return FullyLazyCoeffBaseProductType(m_lhs, m_rhs).diagonal(index); }
133 
134  // restrict coeff accessors to 1x1 expressions. No need to care about mutators here since this isnt a Lvalue expression
135  typename Base::CoeffReturnType coeff(Index row, Index col) const
136  {
137 #ifdef EIGEN2_SUPPORT
138  return lhs().row(row).cwiseProduct(rhs().col(col).transpose()).sum();
139 #else
141  eigen_assert(this->rows() == 1 && this->cols() == 1);
142  Matrix<Scalar,1,1> result = *this;
143  return result.coeff(row,col);
144 #endif
145  }
146 
147  typename Base::CoeffReturnType coeff(Index i) const
148  {
150  eigen_assert(this->rows() == 1 && this->cols() == 1);
151  Matrix<Scalar,1,1> result = *this;
152  return result.coeff(i);
153  }
154 
155  const Scalar& coeffRef(Index row, Index col) const
156  {
158  eigen_assert(this->rows() == 1 && this->cols() == 1);
159  return derived().coeffRef(row,col);
160  }
161 
162  const Scalar& coeffRef(Index i) const
163  {
165  eigen_assert(this->rows() == 1 && this->cols() == 1);
166  return derived().coeffRef(i);
167  }
168 
169  protected:
170 
173 
175 };
176 
177 // here we need to overload the nested rule for products
178 // such that the nested type is a const reference to a plain matrix
179 namespace internal {
180 template<typename Lhs, typename Rhs, int Mode, int N, typename PlainObject>
181 struct nested<GeneralProduct<Lhs,Rhs,Mode>, N, PlainObject>
182 {
183  typedef PlainObject const& type;
184 };
185 }
186 
187 template<typename NestedProduct>
189 
190 // Note that these two operator* functions are not defined as member
191 // functions of ProductBase, because, otherwise we would have to
192 // define all overloads defined in MatrixBase. Furthermore, Using
193 // "using Base::operator*" would not work with MSVC.
194 //
195 // Also note that here we accept any compatible scalar types
196 template<typename Derived,typename Lhs,typename Rhs>
198 operator*(const ProductBase<Derived,Lhs,Rhs>& prod, const typename Derived::Scalar& x)
199 { return ScaledProduct<Derived>(prod.derived(), x); }
200 
201 template<typename Derived,typename Lhs,typename Rhs>
203  const ScaledProduct<Derived> >::type
204 operator*(const ProductBase<Derived,Lhs,Rhs>& prod, const typename Derived::RealScalar& x)
205 { return ScaledProduct<Derived>(prod.derived(), x); }
206 
207 
208 template<typename Derived,typename Lhs,typename Rhs>
210 operator*(const typename Derived::Scalar& x,const ProductBase<Derived,Lhs,Rhs>& prod)
211 { return ScaledProduct<Derived>(prod.derived(), x); }
212 
213 template<typename Derived,typename Lhs,typename Rhs>
214 typename internal::enable_if<!internal::is_same<typename Derived::Scalar,typename Derived::RealScalar>::value,
215  const ScaledProduct<Derived> >::type
216 operator*(const typename Derived::RealScalar& x,const ProductBase<Derived,Lhs,Rhs>& prod)
217 { return ScaledProduct<Derived>(prod.derived(), x); }
218 
219 namespace internal {
220 template<typename NestedProduct>
221 struct traits<ScaledProduct<NestedProduct> >
222  : traits<ProductBase<ScaledProduct<NestedProduct>,
223  typename NestedProduct::_LhsNested,
224  typename NestedProduct::_RhsNested> >
225 {
227 };
228 }
229 
230 template<typename NestedProduct>
231 class ScaledProduct
232  : public ProductBase<ScaledProduct<NestedProduct>,
233  typename NestedProduct::_LhsNested,
234  typename NestedProduct::_RhsNested>
235 {
236  public:
238  typename NestedProduct::_LhsNested,
239  typename NestedProduct::_RhsNested> Base;
240  typedef typename Base::Scalar Scalar;
241  typedef typename Base::PlainObject PlainObject;
242 // EIGEN_PRODUCT_PUBLIC_INTERFACE(ScaledProduct)
243 
244  ScaledProduct(const NestedProduct& prod, const Scalar& x)
245  : Base(prod.lhs(),prod.rhs()), m_prod(prod), m_alpha(x) {}
246 
247  template<typename Dest>
248  inline void evalTo(Dest& dst) const { dst.setZero(); scaleAndAddTo(dst, Scalar(1)); }
249 
250  template<typename Dest>
251  inline void addTo(Dest& dst) const { scaleAndAddTo(dst, Scalar(1)); }
252 
253  template<typename Dest>
254  inline void subTo(Dest& dst) const { scaleAndAddTo(dst, Scalar(-1)); }
255 
256  template<typename Dest>
257  inline void scaleAndAddTo(Dest& dst, const Scalar& a_alpha) const { m_prod.derived().scaleAndAddTo(dst,a_alpha * m_alpha); }
258 
259  const Scalar& alpha() const { return m_alpha; }
260 
261  protected:
262  const NestedProduct& m_prod;
263  Scalar m_alpha;
264 };
265 
268 template<typename Derived>
269 template<typename ProductDerived, typename Lhs, typename Rhs>
271 {
272  other.derived().evalTo(derived());
273  return derived();
274 }
275 
276 } // end namespace Eigen
277 
278 #endif // EIGEN_PRODUCTBASE_H
const Diagonal< FullyLazyCoeffBaseProductType, Index > diagonal() const
Definition: ProductBase.h:128
Expression of the product of two general matrices or vectors.
const NestedProduct & m_prod
Definition: ProductBase.h:262
#define EIGEN_STATIC_ASSERT_SIZE_1x1(TYPE)
Definition: StaticAssert.h:186
#define N
promote_index_type< typename traits< Lhs >::Index, typename traits< Rhs >::Index >::type Index
Definition: ProductBase.h:31
void addTo(Dest &dst) const
Definition: ProductBase.h:105
Base::CoeffReturnType coeff(Index i) const
Definition: ProductBase.h:147
internal::traits< Derived >::Scalar Scalar
Definition: DenseBase.h:63
scalar_product_traits< typename Lhs::Scalar, typename Rhs::Scalar >::ReturnType Scalar
Definition: ProductBase.h:27
internal::traits< Derived >::Index Index
The type of indices.
Definition: DenseBase.h:61
const Scalar & coeffRef(Index i) const
Definition: ProductBase.h:162
MatrixBase< Derived > Base
Definition: ProductBase.h:66
iterative scaling algorithm to equilibrate rows and column norms in matrices
Definition: matrix.hpp:471
const internal::permut_matrix_product_retval< PermutationDerived, Derived, OnTheRight > operator*(const MatrixBase< Derived > &matrix, const PermutationBase< PermutationDerived > &permutation)
const unsigned int RowMajorBit
void addTo(Dest &dst) const
Definition: ProductBase.h:251
Base::Scalar Scalar
Definition: ProductBase.h:240
void subTo(Dest &dst) const
Definition: ProductBase.h:108
const _RhsNested & rhs() const
Definition: ProductBase.h:114
ProductBase< ScaledProduct< NestedProduct >, typename NestedProduct::_LhsNested, typename NestedProduct::_RhsNested > Base
Definition: ProductBase.h:239
EIGEN_STRONG_INLINE const Scalar & coeff(Index rowId, Index colId) const
Base::CoeffReturnType coeff(Index row, Index col) const
Definition: ProductBase.h:135
EIGEN_STRONG_INLINE void resize(Index nbRows, Index nbCols)
const _LhsNested & lhs() const
Definition: ProductBase.h:113
void scaleAndAddTo(Dest &dst, const Scalar &a_alpha) const
Definition: ProductBase.h:257
void evalTo(Dest &dst) const
Definition: ProductBase.h:102
const unsigned int EvalBeforeAssigningBit
promote_storage_type< typename traits< Lhs >::StorageKind, typename traits< Rhs >::StorageKind >::ret StorageKind
Definition: ProductBase.h:29
void rhs(const real_t *x, real_t *f)
const unsigned int NestByRefBit
void subTo(Dest &dst) const
Definition: ProductBase.h:254
#define EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
PlainObject m_result
Definition: ProductBase.h:174
RowXpr row(Index i)
Definition: BlockMethods.h:725
traits< NestedProduct >::StorageKind StorageKind
Definition: ProductBase.h:226
Base::PlainObject PlainObject
Definition: ProductBase.h:241
void evalTo(Dest &dst) const
Definition: ProductBase.h:248
const Scalar & alpha() const
Definition: ProductBase.h:259
Expression of a diagonal/subdiagonal/superdiagonal in a matrix.
Definition: Diagonal.h:64
ScaledProduct(const NestedProduct &prod, const Scalar &x)
Definition: ProductBase.h:244
Derived & lazyAssign(const ProductBase< ProductDerived, Lhs, Rhs > &other)
Definition: ProductBase.h:270
void scaleAndAddTo(Dest &dst, const Scalar &alpha) const
Definition: ProductBase.h:111
const unsigned int EvalBeforeNestingBit
ColXpr col(Index i)
Definition: BlockMethods.h:708
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:127
#define eigen_assert(x)
const Diagonal< FullyLazyCoeffBaseProductType, Dynamic > diagonal(Index index) const
Definition: ProductBase.h:131
const Diagonal< const LazyCoeffBasedProductType, 0 > diagonal() const
const Scalar & coeffRef(Index row, Index col) const
Definition: ProductBase.h:155
const Diagonal< const FullyLazyCoeffBaseProductType, 0 > diagonal() const
Definition: ProductBase.h:124
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
Index cols() const
Definition: ProductBase.h:99
Index rows() const
Definition: ProductBase.h:98


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