Eigen  3.4.90 (git rev 67eeba6e720c5745abc77ae6c92ce0a44aa7b7ae)
Product.h
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;
88  EIGEN_GENERIC_PUBLIC_INTERFACE(Product)
89 
90  typedef typename internal::ref_selector<Lhs>::type LhsNested;
91  typedef typename internal::ref_selector<Rhs>::type RhsNested;
92  typedef internal::remove_all_t<LhsNested> LhsNestedCleaned;
93  typedef internal::remove_all_t<RhsNested> RhsNestedCleaned;
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 
115  LhsNested m_lhs;
116  RhsNested m_rhs;
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 {
157  typedef Product<Lhs, Rhs, Option> Derived;
158 
159  public:
160 
161  typedef typename internal::dense_product_base<Lhs, Rhs, Option> Base;
162  EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
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
Expression of the product of two arbitrary matrices or vectors.
Definition: Product.h:77
const unsigned int NoPreferredStorageOrderBit
Definition: Constants.h:180
const unsigned int RowMajorBit
Definition: Constants.h:68
Namespace containing all symbols from the Eigen library.
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:59
const int Dynamic
Definition: Constants.h:24