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 // Eigen is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 3 of the License, or (at your option) any later version.
10 //
11 // Alternatively, you can redistribute it and/or
12 // modify it under the terms of the GNU General Public License as
13 // published by the Free Software Foundation; either version 2 of
14 // the License, or (at your option) any later version.
15 //
16 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
17 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public
22 // License and a copy of the GNU General Public License along with
23 // Eigen. If not, see <http://www.gnu.org/licenses/>.
24 
25 #ifndef EIGEN_PRODUCTBASE_H
26 #define EIGEN_PRODUCTBASE_H
27 
28 namespace Eigen {
29 
35 namespace internal {
36 template<typename Derived, typename _Lhs, typename _Rhs>
37 struct traits<ProductBase<Derived,_Lhs,_Rhs> >
38 {
39  typedef MatrixXpr XprKind;
40  typedef typename remove_all<_Lhs>::type Lhs;
41  typedef typename remove_all<_Rhs>::type Rhs;
42  typedef typename scalar_product_traits<typename Lhs::Scalar, typename Rhs::Scalar>::ReturnType Scalar;
43  typedef typename promote_storage_type<typename traits<Lhs>::StorageKind,
44  typename traits<Rhs>::StorageKind>::ret StorageKind;
45  typedef typename promote_index_type<typename traits<Lhs>::Index,
46  typename traits<Rhs>::Index>::type Index;
47  enum {
48  RowsAtCompileTime = traits<Lhs>::RowsAtCompileTime,
49  ColsAtCompileTime = traits<Rhs>::ColsAtCompileTime,
50  MaxRowsAtCompileTime = traits<Lhs>::MaxRowsAtCompileTime,
51  MaxColsAtCompileTime = traits<Rhs>::MaxColsAtCompileTime,
52  Flags = (MaxRowsAtCompileTime==1 ? RowMajorBit : 0)
54  // Note that EvalBeforeNestingBit and NestByRefBit
55  // are not used in practice because nested is overloaded for products
56  CoeffReadCost = 0 // FIXME why is it needed ?
57  };
58 };
59 }
60 
61 #define EIGEN_PRODUCT_PUBLIC_INTERFACE(Derived) \
62  typedef ProductBase<Derived, Lhs, Rhs > Base; \
63  EIGEN_DENSE_PUBLIC_INTERFACE(Derived) \
64  typedef typename Base::LhsNested LhsNested; \
65  typedef typename Base::_LhsNested _LhsNested; \
66  typedef typename Base::LhsBlasTraits LhsBlasTraits; \
67  typedef typename Base::ActualLhsType ActualLhsType; \
68  typedef typename Base::_ActualLhsType _ActualLhsType; \
69  typedef typename Base::RhsNested RhsNested; \
70  typedef typename Base::_RhsNested _RhsNested; \
71  typedef typename Base::RhsBlasTraits RhsBlasTraits; \
72  typedef typename Base::ActualRhsType ActualRhsType; \
73  typedef typename Base::_ActualRhsType _ActualRhsType; \
74  using Base::m_lhs; \
75  using Base::m_rhs;
76 
77 template<typename Derived, typename Lhs, typename Rhs>
78 class ProductBase : public MatrixBase<Derived>
79 {
80  public:
83 
84  typedef typename Lhs::Nested LhsNested;
85  typedef typename internal::remove_all<LhsNested>::type _LhsNested;
86  typedef internal::blas_traits<_LhsNested> LhsBlasTraits;
87  typedef typename LhsBlasTraits::DirectLinearAccessType ActualLhsType;
88  typedef typename internal::remove_all<ActualLhsType>::type _ActualLhsType;
89  typedef typename internal::traits<Lhs>::Scalar LhsScalar;
90 
91  typedef typename Rhs::Nested RhsNested;
92  typedef typename internal::remove_all<RhsNested>::type _RhsNested;
93  typedef internal::blas_traits<_RhsNested> RhsBlasTraits;
94  typedef typename RhsBlasTraits::DirectLinearAccessType ActualRhsType;
95  typedef typename internal::remove_all<ActualRhsType>::type _ActualRhsType;
96  typedef typename internal::traits<Rhs>::Scalar RhsScalar;
97 
98  // Diagonal of a product: no need to evaluate the arguments because they are going to be evaluated only once
99  typedef CoeffBasedProduct<LhsNested, RhsNested, 0> FullyLazyCoeffBaseProductType;
100 
101  public:
102 
103  typedef typename Base::PlainObject PlainObject;
104 
105  ProductBase(const Lhs& lhs, const Rhs& rhs)
106  : m_lhs(lhs), m_rhs(rhs)
107  {
108  eigen_assert(lhs.cols() == rhs.rows()
109  && "invalid matrix product"
110  && "if you wanted a coeff-wise or a dot product use the respective explicit functions");
111  }
112 
113  inline Index rows() const { return m_lhs.rows(); }
114  inline Index cols() const { return m_rhs.cols(); }
115 
116  template<typename Dest>
117  inline void evalTo(Dest& dst) const { dst.setZero(); scaleAndAddTo(dst,Scalar(1)); }
118 
119  template<typename Dest>
120  inline void addTo(Dest& dst) const { scaleAndAddTo(dst,Scalar(1)); }
121 
122  template<typename Dest>
123  inline void subTo(Dest& dst) const { scaleAndAddTo(dst,Scalar(-1)); }
124 
125  template<typename Dest>
126  inline void scaleAndAddTo(Dest& dst,Scalar alpha) const { derived().scaleAndAddTo(dst,alpha); }
127 
128  const _LhsNested& lhs() const { return m_lhs; }
129  const _RhsNested& rhs() const { return m_rhs; }
130 
131  // Implicit conversion to the nested type (trigger the evaluation of the product)
132  operator const PlainObject& () const
133  {
134  m_result.resize(m_lhs.rows(), m_rhs.cols());
135  derived().evalTo(m_result);
136  return m_result;
137  }
138 
140  { return FullyLazyCoeffBaseProductType(m_lhs, m_rhs); }
141 
142  template<int Index>
144  { return FullyLazyCoeffBaseProductType(m_lhs, m_rhs); }
145 
147  { return FullyLazyCoeffBaseProductType(m_lhs, m_rhs).diagonal(index); }
148 
149  // restrict coeff accessors to 1x1 expressions. No need to care about mutators here since this isnt a Lvalue expression
150  typename Base::CoeffReturnType coeff(Index row, Index col) const
151  {
152 #ifdef EIGEN2_SUPPORT
153  return lhs().row(row).cwiseProduct(rhs().col(col).transpose()).sum();
154 #else
156  eigen_assert(this->rows() == 1 && this->cols() == 1);
157  Matrix<Scalar,1,1> result = *this;
158  return result.coeff(row,col);
159 #endif
160  }
161 
162  typename Base::CoeffReturnType coeff(Index i) const
163  {
165  eigen_assert(this->rows() == 1 && this->cols() == 1);
166  Matrix<Scalar,1,1> result = *this;
167  return result.coeff(i);
168  }
169 
170  const Scalar& coeffRef(Index row, Index col) const
171  {
173  eigen_assert(this->rows() == 1 && this->cols() == 1);
174  return derived().coeffRef(row,col);
175  }
176 
177  const Scalar& coeffRef(Index i) const
178  {
180  eigen_assert(this->rows() == 1 && this->cols() == 1);
181  return derived().coeffRef(i);
182  }
183 
184  protected:
185 
188 
190 };
191 
192 // here we need to overload the nested rule for products
193 // such that the nested type is a const reference to a plain matrix
194 namespace internal {
195 template<typename Lhs, typename Rhs, int Mode, int N, typename PlainObject>
196 struct nested<GeneralProduct<Lhs,Rhs,Mode>, N, PlainObject>
197 {
198  typedef PlainObject const& type;
199 };
200 }
201 
202 template<typename NestedProduct>
203 class ScaledProduct;
204 
205 // Note that these two operator* functions are not defined as member
206 // functions of ProductBase, because, otherwise we would have to
207 // define all overloads defined in MatrixBase. Furthermore, Using
208 // "using Base::operator*" would not work with MSVC.
209 //
210 // Also note that here we accept any compatible scalar types
211 template<typename Derived,typename Lhs,typename Rhs>
212 const ScaledProduct<Derived>
213 operator*(const ProductBase<Derived,Lhs,Rhs>& prod, typename Derived::Scalar x)
214 { return ScaledProduct<Derived>(prod.derived(), x); }
215 
216 template<typename Derived,typename Lhs,typename Rhs>
217 typename internal::enable_if<!internal::is_same<typename Derived::Scalar,typename Derived::RealScalar>::value,
218  const ScaledProduct<Derived> >::type
219 operator*(const ProductBase<Derived,Lhs,Rhs>& prod, typename Derived::RealScalar x)
220 { return ScaledProduct<Derived>(prod.derived(), x); }
221 
222 
223 template<typename Derived,typename Lhs,typename Rhs>
224 const ScaledProduct<Derived>
225 operator*(typename Derived::Scalar x,const ProductBase<Derived,Lhs,Rhs>& prod)
226 { return ScaledProduct<Derived>(prod.derived(), x); }
227 
228 template<typename Derived,typename Lhs,typename Rhs>
229 typename internal::enable_if<!internal::is_same<typename Derived::Scalar,typename Derived::RealScalar>::value,
230  const ScaledProduct<Derived> >::type
231 operator*(typename Derived::RealScalar x,const ProductBase<Derived,Lhs,Rhs>& prod)
232 { return ScaledProduct<Derived>(prod.derived(), x); }
233 
234 namespace internal {
235 template<typename NestedProduct>
236 struct traits<ScaledProduct<NestedProduct> >
237  : traits<ProductBase<ScaledProduct<NestedProduct>,
238  typename NestedProduct::_LhsNested,
239  typename NestedProduct::_RhsNested> >
240 {
241  typedef typename traits<NestedProduct>::StorageKind StorageKind;
242 };
243 }
244 
245 template<typename NestedProduct>
247  : public ProductBase<ScaledProduct<NestedProduct>,
248  typename NestedProduct::_LhsNested,
249  typename NestedProduct::_RhsNested>
250 {
251  public:
253  typename NestedProduct::_LhsNested,
254  typename NestedProduct::_RhsNested> Base;
255  typedef typename Base::Scalar Scalar;
256  typedef typename Base::PlainObject PlainObject;
257 // EIGEN_PRODUCT_PUBLIC_INTERFACE(ScaledProduct)
258 
259  ScaledProduct(const NestedProduct& prod, Scalar x)
260  : Base(prod.lhs(),prod.rhs()), m_prod(prod), m_alpha(x) {}
261 
262  template<typename Dest>
263  inline void evalTo(Dest& dst) const { dst.setZero(); scaleAndAddTo(dst, Scalar(1)); }
264 
265  template<typename Dest>
266  inline void addTo(Dest& dst) const { scaleAndAddTo(dst, Scalar(1)); }
267 
268  template<typename Dest>
269  inline void subTo(Dest& dst) const { scaleAndAddTo(dst, Scalar(-1)); }
270 
271  template<typename Dest>
272  inline void scaleAndAddTo(Dest& dst,Scalar alpha) const { m_prod.derived().scaleAndAddTo(dst,alpha * m_alpha); }
273 
274  const Scalar& alpha() const { return m_alpha; }
275 
276  protected:
277  const NestedProduct& m_prod;
279 };
280 
283 template<typename Derived>
284 template<typename ProductDerived, typename Lhs, typename Rhs>
286 {
287  other.derived().evalTo(derived());
288  return derived();
289 }
290 
291 } // end namespace Eigen
292 
293 #endif // EIGEN_PRODUCTBASE_H