Eigen  3.2.92
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 namespace Eigen {
14 
15 template<typename Lhs, typename Rhs, int Option, typename StorageKind> class ProductImpl;
16 
33 namespace internal {
34 
35 // Determine the scalar of Product<Lhs, Rhs>. This is normally the same as Lhs::Scalar times
36 // Rhs::Scalar, but product with permutation matrices inherit the scalar of the other factor.
37 template<typename Lhs, typename Rhs, typename LhsShape = typename evaluator_traits<Lhs>::Shape,
38  typename RhsShape = typename evaluator_traits<Rhs>::Shape >
39 struct product_result_scalar
40 {
41  typedef typename scalar_product_traits<typename Lhs::Scalar, typename Rhs::Scalar>::ReturnType Scalar;
42 };
43 
44 template<typename Lhs, typename Rhs, typename RhsShape>
45 struct product_result_scalar<Lhs, Rhs, PermutationShape, RhsShape>
46 {
47  typedef typename Rhs::Scalar Scalar;
48 };
49 
50 template<typename Lhs, typename Rhs, typename LhsShape>
51  struct product_result_scalar<Lhs, Rhs, LhsShape, PermutationShape>
52 {
53  typedef typename Lhs::Scalar Scalar;
54 };
55 
56 template<typename Lhs, typename Rhs, typename RhsShape>
57 struct product_result_scalar<Lhs, Rhs, TranspositionsShape, RhsShape>
58 {
59  typedef typename Rhs::Scalar Scalar;
60 };
61 
62 template<typename Lhs, typename Rhs, typename LhsShape>
63  struct product_result_scalar<Lhs, Rhs, LhsShape, TranspositionsShape>
64 {
65  typedef typename Lhs::Scalar Scalar;
66 };
67 
68 template<typename Lhs, typename Rhs, int Option>
69 struct traits<Product<Lhs, Rhs, Option> >
70 {
71  typedef typename remove_all<Lhs>::type LhsCleaned;
72  typedef typename remove_all<Rhs>::type RhsCleaned;
73  typedef traits<LhsCleaned> LhsTraits;
74  typedef traits<RhsCleaned> RhsTraits;
75 
76  typedef MatrixXpr XprKind;
77 
78  typedef typename product_result_scalar<LhsCleaned,RhsCleaned>::Scalar Scalar;
79  typedef typename product_promote_storage_type<typename LhsTraits::StorageKind,
80  typename RhsTraits::StorageKind,
81  internal::product_type<Lhs,Rhs>::ret>::ret StorageKind;
82  typedef typename promote_index_type<typename LhsTraits::StorageIndex,
83  typename RhsTraits::StorageIndex>::type StorageIndex;
84 
85  enum {
86  RowsAtCompileTime = LhsTraits::RowsAtCompileTime,
87  ColsAtCompileTime = RhsTraits::ColsAtCompileTime,
88  MaxRowsAtCompileTime = LhsTraits::MaxRowsAtCompileTime,
89  MaxColsAtCompileTime = RhsTraits::MaxColsAtCompileTime,
90 
91  // FIXME: only needed by GeneralMatrixMatrixTriangular
92  InnerSize = EIGEN_SIZE_MIN_PREFER_FIXED(LhsTraits::ColsAtCompileTime, RhsTraits::RowsAtCompileTime),
93 
94  // The storage order is somewhat arbitrary here. The correct one will be determined through the evaluator.
95  Flags = (MaxRowsAtCompileTime==1 && MaxColsAtCompileTime!=1) ? RowMajorBit
96  : (MaxColsAtCompileTime==1 && MaxRowsAtCompileTime!=1) ? 0
97  : ( ((LhsTraits::Flags&NoPreferredStorageOrderBit) && (RhsTraits::Flags&RowMajorBit))
98  || ((RhsTraits::Flags&NoPreferredStorageOrderBit) && (LhsTraits::Flags&RowMajorBit)) ) ? RowMajorBit
99  : NoPreferredStorageOrderBit
100  };
101 };
102 
103 } // end namespace internal
104 
105 
106 template<typename _Lhs, typename _Rhs, int Option>
107 class Product : public ProductImpl<_Lhs,_Rhs,Option,
108  typename internal::product_promote_storage_type<typename internal::traits<_Lhs>::StorageKind,
109  typename internal::traits<_Rhs>::StorageKind,
110  internal::product_type<_Lhs,_Rhs>::ret>::ret>
111 {
112  public:
113 
114  typedef _Lhs Lhs;
115  typedef _Rhs Rhs;
116 
117  typedef typename ProductImpl<
118  Lhs, Rhs, Option,
119  typename internal::product_promote_storage_type<typename internal::traits<Lhs>::StorageKind,
120  typename internal::traits<Rhs>::StorageKind,
121  internal::product_type<Lhs,Rhs>::ret>::ret>::Base Base;
122  EIGEN_GENERIC_PUBLIC_INTERFACE(Product)
123 
124  typedef typename internal::ref_selector<Lhs>::type LhsNested;
125  typedef typename internal::ref_selector<Rhs>::type RhsNested;
126  typedef typename internal::remove_all<LhsNested>::type LhsNestedCleaned;
127  typedef typename internal::remove_all<RhsNested>::type RhsNestedCleaned;
128 
129  EIGEN_DEVICE_FUNC Product(const Lhs& lhs, const Rhs& rhs) : m_lhs(lhs), m_rhs(rhs)
130  {
131  eigen_assert(lhs.cols() == rhs.rows()
132  && "invalid matrix product"
133  && "if you wanted a coeff-wise or a dot product use the respective explicit functions");
134  }
135 
136  EIGEN_DEVICE_FUNC inline Index rows() const { return m_lhs.rows(); }
137  EIGEN_DEVICE_FUNC inline Index cols() const { return m_rhs.cols(); }
138 
139  EIGEN_DEVICE_FUNC const LhsNestedCleaned& lhs() const { return m_lhs; }
140  EIGEN_DEVICE_FUNC const RhsNestedCleaned& rhs() const { return m_rhs; }
141 
142  protected:
143 
144  LhsNested m_lhs;
145  RhsNested m_rhs;
146 };
147 
148 namespace internal {
149 
150 template<typename Lhs, typename Rhs, int Option, int ProductTag = internal::product_type<Lhs,Rhs>::ret>
151 class dense_product_base
152  : public internal::dense_xpr_base<Product<Lhs,Rhs,Option> >::type
153 {};
154 
156 template<typename Lhs, typename Rhs, int Option>
157 class dense_product_base<Lhs, Rhs, Option, InnerProduct>
158  : public internal::dense_xpr_base<Product<Lhs,Rhs,Option> >::type
159 {
160  typedef Product<Lhs,Rhs,Option> ProductXpr;
161  typedef typename internal::dense_xpr_base<ProductXpr>::type Base;
162 public:
163  using Base::derived;
164  typedef typename Base::Scalar Scalar;
165 
166  operator const Scalar() const
167  {
168  return internal::evaluator<ProductXpr>(derived()).coeff(0,0);
169  }
170 };
171 
172 } // namespace internal
173 
174 // Generic API dispatcher
175 template<typename Lhs, typename Rhs, int Option, typename StorageKind>
176 class ProductImpl : public internal::generic_xpr_base<Product<Lhs,Rhs,Option>, MatrixXpr, StorageKind>::type
177 {
178  public:
179  typedef typename internal::generic_xpr_base<Product<Lhs,Rhs,Option>, MatrixXpr, StorageKind>::type Base;
180 };
181 
182 template<typename Lhs, typename Rhs, int Option>
183 class ProductImpl<Lhs,Rhs,Option,Dense>
184  : public internal::dense_product_base<Lhs,Rhs,Option>
185 {
186  typedef Product<Lhs, Rhs, Option> Derived;
187 
188  public:
189 
190  typedef typename internal::dense_product_base<Lhs, Rhs, Option> Base;
191  EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
192  protected:
193  enum {
194  IsOneByOne = (RowsAtCompileTime == 1 || RowsAtCompileTime == Dynamic) &&
195  (ColsAtCompileTime == 1 || ColsAtCompileTime == Dynamic),
196  EnableCoeff = IsOneByOne || Option==LazyProduct
197  };
198 
199  public:
200 
201  EIGEN_DEVICE_FUNC Scalar coeff(Index row, Index col) const
202  {
203  EIGEN_STATIC_ASSERT(EnableCoeff, THIS_METHOD_IS_ONLY_FOR_INNER_OR_LAZY_PRODUCTS);
204  eigen_assert( (Option==LazyProduct) || (this->rows() == 1 && this->cols() == 1) );
205 
206  return internal::evaluator<Derived>(derived()).coeff(row,col);
207  }
208 
209  EIGEN_DEVICE_FUNC Scalar coeff(Index i) const
210  {
211  EIGEN_STATIC_ASSERT(EnableCoeff, THIS_METHOD_IS_ONLY_FOR_INNER_OR_LAZY_PRODUCTS);
212  eigen_assert( (Option==LazyProduct) || (this->rows() == 1 && this->cols() == 1) );
213 
214  return internal::evaluator<Derived>(derived()).coeff(i);
215  }
216 
217 
218 };
219 
220 } // end namespace Eigen
221 
222 #endif // EIGEN_PRODUCT_H
Expression of the product of two arbitrary matrices or vectors.
Definition: Product.h:107
Definition: LDLT.h:16
const unsigned int RowMajorBit
Definition: Constants.h:61
Definition: Constants.h:505
Definition: Eigen_Colamd.h:54
const unsigned int NoPreferredStorageOrderBit
Definition: Constants.h:172