Product.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-2011 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_PRODUCT_H
11 #define EIGEN_PRODUCT_H
12 
13 #include "./InternalHeaderCheck.h"
14 
15 namespace Eigen {
16 
17 template<typename Lhs, typename Rhs, int Option, typename StorageKind> class ProductImpl;
18 
19 namespace internal {
20 
21 template<typename Lhs, typename Rhs, int Option>
22 struct traits<Product<Lhs, Rhs, Option> >
23 {
24  typedef remove_all_t<Lhs> LhsCleaned;
25  typedef remove_all_t<Rhs> RhsCleaned;
26  typedef traits<LhsCleaned> LhsTraits;
27  typedef traits<RhsCleaned> RhsTraits;
28 
29  typedef MatrixXpr XprKind;
30 
31  typedef typename ScalarBinaryOpTraits<typename traits<LhsCleaned>::Scalar, typename traits<RhsCleaned>::Scalar>::ReturnType Scalar;
32  typedef typename product_promote_storage_type<typename LhsTraits::StorageKind,
33  typename RhsTraits::StorageKind,
34  internal::product_type<Lhs,Rhs>::ret>::ret StorageKind;
35  typedef typename promote_index_type<typename LhsTraits::StorageIndex,
36  typename RhsTraits::StorageIndex>::type StorageIndex;
37 
38  enum {
39  RowsAtCompileTime = LhsTraits::RowsAtCompileTime,
40  ColsAtCompileTime = RhsTraits::ColsAtCompileTime,
41  MaxRowsAtCompileTime = LhsTraits::MaxRowsAtCompileTime,
42  MaxColsAtCompileTime = RhsTraits::MaxColsAtCompileTime,
43 
44  // FIXME: only needed by GeneralMatrixMatrixTriangular
45  InnerSize = min_size_prefer_fixed(LhsTraits::ColsAtCompileTime, RhsTraits::RowsAtCompileTime),
46 
47  // The storage order is somewhat arbitrary here. The correct one will be determined through the evaluator.
48  Flags = (MaxRowsAtCompileTime==1 && MaxColsAtCompileTime!=1) ? RowMajorBit
49  : (MaxColsAtCompileTime==1 && MaxRowsAtCompileTime!=1) ? 0
50  : ( ((LhsTraits::Flags&NoPreferredStorageOrderBit) && (RhsTraits::Flags&RowMajorBit))
51  || ((RhsTraits::Flags&NoPreferredStorageOrderBit) && (LhsTraits::Flags&RowMajorBit)) ) ? RowMajorBit
53  };
54 };
55 
56 } // end namespace internal
57 
72 template<typename Lhs_, typename Rhs_, int Option>
73 class Product : public ProductImpl<Lhs_,Rhs_,Option,
74  typename internal::product_promote_storage_type<typename internal::traits<Lhs_>::StorageKind,
75  typename internal::traits<Rhs_>::StorageKind,
76  internal::product_type<Lhs_,Rhs_>::ret>::ret>
77 {
78  public:
79 
80  typedef Lhs_ Lhs;
81  typedef Rhs_ Rhs;
82 
83  typedef typename ProductImpl<
84  Lhs, Rhs, Option,
85  typename internal::product_promote_storage_type<typename internal::traits<Lhs>::StorageKind,
86  typename internal::traits<Rhs>::StorageKind,
87  internal::product_type<Lhs,Rhs>::ret>::ret>::Base Base;
89 
90  typedef typename internal::ref_selector<Lhs>::type LhsNested;
91  typedef typename internal::ref_selector<Rhs>::type RhsNested;
94 
95  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
96  Product(const Lhs& lhs, const Rhs& rhs) : m_lhs(lhs), m_rhs(rhs)
97  {
98  eigen_assert(lhs.cols() == rhs.rows()
99  && "invalid matrix product"
100  && "if you wanted a coeff-wise or a dot product use the respective explicit functions");
101  }
102 
103  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR
104  Index rows() const EIGEN_NOEXCEPT { return m_lhs.rows(); }
105  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR
106  Index cols() const EIGEN_NOEXCEPT { return m_rhs.cols(); }
107 
108  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
109  const LhsNestedCleaned& lhs() const { return m_lhs; }
110  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
111  const RhsNestedCleaned& rhs() const { return m_rhs; }
112 
113  protected:
114 
117 };
118 
119 namespace internal {
120 
121 template<typename Lhs, typename Rhs, int Option, int ProductTag = internal::product_type<Lhs,Rhs>::ret>
122 class dense_product_base
123  : public internal::dense_xpr_base<Product<Lhs,Rhs,Option> >::type
124 {};
125 
127 template<typename Lhs, typename Rhs, int Option>
128 class dense_product_base<Lhs, Rhs, Option, InnerProduct>
129  : public internal::dense_xpr_base<Product<Lhs,Rhs,Option> >::type
130 {
131  typedef Product<Lhs,Rhs,Option> ProductXpr;
132  typedef typename internal::dense_xpr_base<ProductXpr>::type Base;
133 public:
134  using Base::derived;
135  typedef typename Base::Scalar Scalar;
136 
137  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE operator const Scalar() const
138  {
139  return internal::evaluator<ProductXpr>(derived()).coeff(0,0);
140  }
141 };
142 
143 } // namespace internal
144 
145 // Generic API dispatcher
146 template<typename Lhs, typename Rhs, int Option, typename StorageKind>
147 class ProductImpl : public internal::generic_xpr_base<Product<Lhs,Rhs,Option>, MatrixXpr, StorageKind>::type
148 {
149  public:
150  typedef typename internal::generic_xpr_base<Product<Lhs,Rhs,Option>, MatrixXpr, StorageKind>::type Base;
151 };
152 
153 template<typename Lhs, typename Rhs, int Option>
154 class ProductImpl<Lhs,Rhs,Option,Dense>
155  : public internal::dense_product_base<Lhs,Rhs,Option>
156 {
158 
159  public:
160 
161  typedef typename internal::dense_product_base<Lhs, Rhs, Option> Base;
163  protected:
164  enum {
165  IsOneByOne = (RowsAtCompileTime == 1 || RowsAtCompileTime == Dynamic) &&
166  (ColsAtCompileTime == 1 || ColsAtCompileTime == Dynamic),
167  EnableCoeff = IsOneByOne || Option==LazyProduct
168  };
169 
170  public:
171 
172  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar coeff(Index row, Index col) const
173  {
174  EIGEN_STATIC_ASSERT(EnableCoeff, THIS_METHOD_IS_ONLY_FOR_INNER_OR_LAZY_PRODUCTS);
175  eigen_assert( (Option==LazyProduct) || (this->rows() == 1 && this->cols() == 1) );
176 
177  return internal::evaluator<Derived>(derived()).coeff(row,col);
178  }
179 
180  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar coeff(Index i) const
181  {
182  EIGEN_STATIC_ASSERT(EnableCoeff, THIS_METHOD_IS_ONLY_FOR_INNER_OR_LAZY_PRODUCTS);
183  eigen_assert( (Option==LazyProduct) || (this->rows() == 1 && this->cols() == 1) );
184 
185  return internal::evaluator<Derived>(derived()).coeff(i);
186  }
187 
188 
189 };
190 
191 } // end namespace Eigen
192 
193 #endif // EIGEN_PRODUCT_H
RowXpr row(Index i)
This is the const version of row(). *‍/.
ColXpr col(Index i)
This is the const version of col().
#define EIGEN_GENERIC_PUBLIC_INTERFACE(Derived)
Definition: Macros.h:1149
#define EIGEN_NOEXCEPT
Definition: Macros.h:1260
#define EIGEN_CONSTEXPR
Definition: Macros.h:747
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:883
#define EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
Definition: Macros.h:1168
#define eigen_assert(x)
Definition: Macros.h:902
#define EIGEN_STATIC_ASSERT(X, MSG)
Definition: StaticAssert.h:26
internal::dense_product_base< Lhs, Rhs, Option > Base
Definition: Product.h:161
Scalar coeff(Index row, Index col) const
Definition: Product.h:172
Product< Lhs, Rhs, Option > Derived
Definition: Product.h:157
internal::generic_xpr_base< Product< Lhs, Rhs, Option >, MatrixXpr, StorageKind >::type Base
Definition: Product.h:150
Expression of the product of two arbitrary matrices or vectors.
Definition: Product.h:77
internal::remove_all_t< LhsNested > LhsNestedCleaned
Definition: Product.h:92
EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition: Product.h:106
const LhsNestedCleaned & lhs() const
Definition: Product.h:109
internal::remove_all_t< RhsNested > RhsNestedCleaned
Definition: Product.h:93
ProductImpl< Lhs, Rhs, Option, typename internal::product_promote_storage_type< typename internal::traits< Lhs >::StorageKind, typename internal::traits< Rhs >::StorageKind, internal::product_type< Lhs, Rhs >::ret >::ret >::Base Base
Definition: Product.h:87
RhsNested m_rhs
Definition: Product.h:116
EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition: Product.h:104
const RhsNestedCleaned & rhs() const
Definition: Product.h:111
LhsNested m_lhs
Definition: Product.h:115
internal::ref_selector< Rhs >::type RhsNested
Definition: Product.h:91
internal::ref_selector< Lhs >::type LhsNested
Definition: Product.h:90
const unsigned int NoPreferredStorageOrderBit
Definition: Constants.h:180
const unsigned int RowMajorBit
Definition: Constants.h:68
constexpr int min_size_prefer_fixed(A a, B b)
Definition: Meta.h:553
typename remove_all< T >::type remove_all_t
Definition: Meta.h:119
: InteropHeaders
Definition: Core:139
@ LazyProduct
Definition: Constants.h:504
@ InnerProduct
Definition: Constants.h:504
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
const int Dynamic
Definition: Constants.h:24