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 // IWYU pragma: private
14 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
18 template <typename Lhs, typename Rhs, int Option, typename StorageKind>
19 class ProductImpl;
20 
21 namespace internal {
22 
23 template <typename Lhs, typename Rhs, int Option>
24 struct traits<Product<Lhs, Rhs, Option>> {
29 
30  typedef MatrixXpr XprKind;
31 
33  typename traits<RhsCleaned>::Scalar>::ReturnType Scalar;
34  typedef typename product_promote_storage_type<typename LhsTraits::StorageKind, typename RhsTraits::StorageKind,
38 
39  enum {
40  RowsAtCompileTime = LhsTraits::RowsAtCompileTime,
41  ColsAtCompileTime = RhsTraits::ColsAtCompileTime,
42  MaxRowsAtCompileTime = LhsTraits::MaxRowsAtCompileTime,
43  MaxColsAtCompileTime = RhsTraits::MaxColsAtCompileTime,
44 
45  // FIXME: only needed by GeneralMatrixMatrixTriangular
46  InnerSize = min_size_prefer_fixed(LhsTraits::ColsAtCompileTime, RhsTraits::RowsAtCompileTime),
47 
48  // The storage order is somewhat arbitrary here. The correct one will be determined through the evaluator.
49  Flags = (MaxRowsAtCompileTime == 1 && MaxColsAtCompileTime != 1) ? RowMajorBit
50  : (MaxColsAtCompileTime == 1 && MaxRowsAtCompileTime != 1) ? 0
51  : (((LhsTraits::Flags & NoPreferredStorageOrderBit) && (RhsTraits::Flags & RowMajorBit)) ||
52  ((RhsTraits::Flags & NoPreferredStorageOrderBit) && (LhsTraits::Flags & RowMajorBit)))
53  ? RowMajorBit
55  };
56 };
57 
59  // convenience enumerations to specialize transposed products
60  enum : int {
61  Default = 0x00,
62  Matrix = 0x01,
63  Permutation = 0x02,
67  };
68 };
69 template <typename Xpr>
70 struct TransposeKind {
74 };
75 
76 template <typename Lhs, typename Rhs>
78  static constexpr int Kind = (TransposeKind<Lhs>::Kind << 8) | TransposeKind<Rhs>::Kind;
79 };
80 
81 template <typename Lhs, typename Rhs, int Option, int Kind = TransposeProductKind<Lhs, Rhs>::Kind>
83  // by default, don't optimize the transposed product
85  using Scalar = typename Derived::Scalar;
89 
90  // return (lhs * rhs)^T
92  return TransposeType(derived);
93  }
94  // return (lhs * rhs)^H
96  return AdjointType(TransposeType(derived));
97  }
98 };
99 
100 template <typename Lhs, typename Rhs, int Option>
101 struct product_transpose_helper<Lhs, Rhs, Option, TransposeProductEnum::MatrixMatrix> {
102  // expand the transposed matrix-matrix product
104 
105  using LhsScalar = typename traits<Lhs>::Scalar;
110 
111  using RhsScalar = typename traits<Rhs>::Scalar;
116 
119 
120  // return rhs^T * lhs^T
122  return TransposeType(RhsTransposeType(derived.rhs()), LhsTransposeType(derived.lhs()));
123  }
124  // return rhs^H * lhs^H
126  return AdjointType(RhsAdjointType(RhsTransposeType(derived.rhs())),
127  LhsAdjointType(LhsTransposeType(derived.lhs())));
128  }
129 };
130 template <typename Lhs, typename Rhs, int Option>
132  // expand the transposed permutation-matrix product
134 
136 
137  using RhsScalar = typename traits<Rhs>::Scalar;
142 
145 
146  // return rhs^T * lhs^-1
148  return TransposeType(RhsTransposeType(derived.rhs()), LhsInverseType(derived.lhs()));
149  }
150  // return rhs^H * lhs^-1
152  return AdjointType(RhsAdjointType(RhsTransposeType(derived.rhs())), LhsInverseType(derived.lhs()));
153  }
154 };
155 template <typename Lhs, typename Rhs, int Option>
156 struct product_transpose_helper<Lhs, Rhs, Option, TransposeProductEnum::MatrixPermutation> {
157  // expand the transposed matrix-permutation product
159 
160  using LhsScalar = typename traits<Lhs>::Scalar;
165 
167 
170 
171  // return rhs^-1 * lhs^T
173  return TransposeType(RhsInverseType(derived.rhs()), LhsTransposeType(derived.lhs()));
174  }
175  // return rhs^-1 * lhs^H
177  return AdjointType(RhsInverseType(derived.rhs()), LhsAdjointType(LhsTransposeType(derived.lhs())));
178  }
179 };
180 
181 } // end namespace internal
182 
197 template <typename Lhs_, typename Rhs_, int Option>
198 class Product
199  : public ProductImpl<Lhs_, Rhs_, Option,
200  typename internal::product_promote_storage_type<
201  typename internal::traits<Lhs_>::StorageKind, typename internal::traits<Rhs_>::StorageKind,
202  internal::product_type<Lhs_, Rhs_>::ret>::ret> {
203  public:
204  typedef Lhs_ Lhs;
205  typedef Rhs_ Rhs;
206 
207  typedef
208  typename ProductImpl<Lhs, Rhs, Option,
213 
214  typedef typename internal::ref_selector<Lhs>::type LhsNested;
215  typedef typename internal::ref_selector<Rhs>::type RhsNested;
218 
219  using TransposeReturnType = typename internal::product_transpose_helper<Lhs, Rhs, Option>::TransposeType;
220  using AdjointReturnType = typename internal::product_transpose_helper<Lhs, Rhs, Option>::AdjointType;
221 
223  eigen_assert(lhs.cols() == rhs.rows() && "invalid matrix product" &&
224  "if you wanted a coeff-wise or a dot product use the respective explicit functions");
225  }
226 
229 
232 
235  }
238  }
239 
240  protected:
243 };
244 
245 namespace internal {
246 
248 class dense_product_base : public internal::dense_xpr_base<Product<Lhs, Rhs, Option>>::type {};
249 
251 template <typename Lhs, typename Rhs, int Option>
253  : public internal::dense_xpr_base<Product<Lhs, Rhs, Option>>::type {
256 
257  public:
258  using Base::derived;
259  typedef typename Base::Scalar Scalar;
260 
262  return internal::evaluator<ProductXpr>(derived()).coeff(0, 0);
263  }
264 };
265 
266 } // namespace internal
267 
268 // Generic API dispatcher
269 template <typename Lhs, typename Rhs, int Option, typename StorageKind>
270 class ProductImpl : public internal::generic_xpr_base<Product<Lhs, Rhs, Option>, MatrixXpr, StorageKind>::type {
271  public:
273 };
274 
275 template <typename Lhs, typename Rhs, int Option>
276 class ProductImpl<Lhs, Rhs, Option, Dense> : public internal::dense_product_base<Lhs, Rhs, Option> {
278 
279  public:
282  protected:
283  enum {
284  IsOneByOne = (RowsAtCompileTime == 1 || RowsAtCompileTime == Dynamic) &&
285  (ColsAtCompileTime == 1 || ColsAtCompileTime == Dynamic),
286  EnableCoeff = IsOneByOne || Option == LazyProduct
287  };
288 
289  public:
291  EIGEN_STATIC_ASSERT(EnableCoeff, THIS_METHOD_IS_ONLY_FOR_INNER_OR_LAZY_PRODUCTS);
292  eigen_assert((Option == LazyProduct) || (this->rows() == 1 && this->cols() == 1));
293 
294  return internal::evaluator<Derived>(derived()).coeff(row, col);
295  }
296 
298  EIGEN_STATIC_ASSERT(EnableCoeff, THIS_METHOD_IS_ONLY_FOR_INNER_OR_LAZY_PRODUCTS);
299  eigen_assert((Option == LazyProduct) || (this->rows() == 1 && this->cols() == 1));
300 
301  return internal::evaluator<Derived>(derived()).coeff(i);
302  }
303 };
304 
305 } // end namespace Eigen
306 
307 #endif // EIGEN_PRODUCT_H
int i
Definition: BiCGSTAB_step_by_step.cpp:9
#define EIGEN_GENERIC_PUBLIC_INTERFACE(Derived)
Definition: Macros.h:1149
#define EIGEN_NOEXCEPT
Definition: Macros.h:1267
#define EIGEN_CONSTEXPR
Definition: Macros.h:758
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:892
#define EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
Definition: Macros.h:1171
#define eigen_assert(x)
Definition: Macros.h:910
#define EIGEN_STRONG_INLINE
Definition: Macros.h:834
m col(1)
m row(1)
#define EIGEN_STATIC_ASSERT(X, MSG)
Definition: StaticAssert.h:26
int rows
Definition: Tutorial_commainit_02.cpp:1
int cols
Definition: Tutorial_commainit_02.cpp:1
SCALAR Scalar
Definition: bench_gemm.cpp:45
Generic expression where a coefficient-wise unary operator is applied to an expression.
Definition: CwiseUnaryOp.h:53
Expression of the inverse of another expression.
Definition: Inverse.h:43
The matrix class, also used for vectors and row-vectors.
Definition: Eigen/Eigen/src/Core/Matrix.h:186
Permutation matrix.
Definition: PermutationMatrix.h:280
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar coeff(Index i) const
Definition: Product.h:297
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar coeff(Index row, Index col) const
Definition: Product.h:290
internal::dense_product_base< Lhs, Rhs, Option > Base
Definition: Product.h:280
Product< Lhs, Rhs, Option > Derived
Definition: Product.h:277
Definition: Product.h:270
internal::generic_xpr_base< Product< Lhs, Rhs, Option >, MatrixXpr, StorageKind >::type Base
Definition: Product.h:272
Expression of the product of two arbitrary matrices or vectors.
Definition: Product.h:202
Lhs_ Lhs
Definition: Product.h:204
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition: Product.h:227
internal::remove_all_t< LhsNested > LhsNestedCleaned
Definition: Product.h:216
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE AdjointReturnType adjoint() const
Definition: Product.h:236
typename internal::product_transpose_helper< Lhs, Rhs, Option >::AdjointType AdjointReturnType
Definition: Product.h:220
internal::remove_all_t< RhsNested > RhsNestedCleaned
Definition: Product.h:217
RhsNested m_rhs
Definition: Product.h:242
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const LhsNestedCleaned & lhs() const
Definition: Product.h:230
Rhs_ Rhs
Definition: Product.h:205
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const RhsNestedCleaned & rhs() const
Definition: Product.h:231
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TransposeReturnType transpose() const
Definition: Product.h:233
LhsNested m_lhs
Definition: Product.h:241
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:211
internal::ref_selector< Rhs >::type RhsNested
Definition: Product.h:215
internal::ref_selector< Lhs >::type LhsNested
Definition: Product.h:214
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition: Product.h:228
typename internal::product_transpose_helper< Lhs, Rhs, Option >::TransposeType TransposeReturnType
Definition: Product.h:219
Expression of the transpose of a matrix.
Definition: Transpose.h:56
internal::dense_xpr_base< ProductXpr >::type Base
Definition: Product.h:255
Product< Lhs, Rhs, Option > ProductXpr
Definition: Product.h:254
Definition: Product.h:248
@ IsComplex
Definition: common.h:73
@ LazyProduct
Definition: Constants.h:504
@ InnerProduct
Definition: Constants.h:509
const unsigned int NoPreferredStorageOrderBit
Definition: Constants.h:182
const unsigned int RowMajorBit
Definition: Constants.h:70
Eigen::DenseIndex ret
Definition: level1_cplx_impl.h:43
@ Lhs
Definition: TensorContractionMapper.h:20
@ Rhs
Definition: TensorContractionMapper.h:20
constexpr int min_size_prefer_fixed(A a, B b)
Definition: Meta.h:683
typename remove_all< T >::type remove_all_t
Definition: Meta.h:142
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
const int Dynamic
Definition: Constants.h:25
Extend namespace for flags.
Definition: fsi_chan_precond_driver.cc:56
type
Definition: compute_granudrum_aor.py:141
Definition: Eigen_Colamd.h:49
Definition: Constants.h:519
Definition: Constants.h:534
Determines whether the given binary operation of two numeric types is allowed and what the scalar ret...
Definition: XprHelper.h:1043
Definition: Product.h:70
static constexpr int Kind
Definition: Product.h:71
@ Permutation
Definition: Product.h:63
@ Default
Definition: Product.h:61
@ MatrixMatrix
Definition: Product.h:64
@ MatrixPermutation
Definition: Product.h:65
@ Matrix
Definition: Product.h:62
static constexpr int Kind
Definition: Product.h:78
Definition: XprHelper.h:558
Definition: CoreEvaluators.h:104
Definition: XprHelper.h:575
Definition: XprHelper.h:990
Definition: XprHelper.h:993
typename DenseBase< Rhs >::ConstTransposeReturnType RhsTransposeType
Definition: Product.h:112
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE AdjointType run_adjoint(const Derived &derived)
Definition: Product.h:125
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TransposeType run_transpose(const Derived &derived)
Definition: Product.h:121
std::conditional_t< NumTraits< RhsScalar >::IsComplex, RhsConjugateTransposeType, RhsTransposeType > RhsAdjointType
Definition: Product.h:115
typename DenseBase< Lhs >::ConstTransposeReturnType LhsTransposeType
Definition: Product.h:106
std::conditional_t< NumTraits< LhsScalar >::IsComplex, LhsConjugateTransposeType, LhsTransposeType > LhsAdjointType
Definition: Product.h:109
typename PermutationBase< Rhs >::InverseReturnType RhsInverseType
Definition: Product.h:166
std::conditional_t< NumTraits< LhsScalar >::IsComplex, LhsConjugateTransposeType, LhsTransposeType > LhsAdjointType
Definition: Product.h:164
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE AdjointType run_adjoint(const Derived &derived)
Definition: Product.h:176
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TransposeType run_transpose(const Derived &derived)
Definition: Product.h:172
typename DenseBase< Lhs >::ConstTransposeReturnType LhsTransposeType
Definition: Product.h:161
std::conditional_t< NumTraits< RhsScalar >::IsComplex, RhsConjugateTransposeType, RhsTransposeType > RhsAdjointType
Definition: Product.h:141
typename PermutationBase< Lhs >::InverseReturnType LhsInverseType
Definition: Product.h:135
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE AdjointType run_adjoint(const Derived &derived)
Definition: Product.h:151
typename DenseBase< Rhs >::ConstTransposeReturnType RhsTransposeType
Definition: Product.h:138
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TransposeType run_transpose(const Derived &derived)
Definition: Product.h:147
std::conditional_t< NumTraits< Scalar >::IsComplex, ConjugateTransposeType, TransposeType > AdjointType
Definition: Product.h:88
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TransposeType run_transpose(const Derived &derived)
Definition: Product.h:91
Transpose< const Derived > TransposeType
Definition: Product.h:86
typename Derived::Scalar Scalar
Definition: Product.h:85
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE AdjointType run_adjoint(const Derived &derived)
Definition: Product.h:95
CwiseUnaryOp< scalar_conjugate_op< Scalar >, TransposeType > ConjugateTransposeType
Definition: Product.h:87
Definition: GeneralProduct.h:52
std::conditional_t<(sizeof(I1)< sizeof(I2)), I2, I1 > type
Definition: XprHelper.h:146
remove_all_t< Rhs > RhsCleaned
Definition: Product.h:26
traits< LhsCleaned > LhsTraits
Definition: Product.h:27
traits< RhsCleaned > RhsTraits
Definition: Product.h:28
ScalarBinaryOpTraits< typename traits< LhsCleaned >::Scalar, typename traits< RhsCleaned >::Scalar >::ReturnType Scalar
Definition: Product.h:33
remove_all_t< Lhs > LhsCleaned
Definition: Product.h:25
promote_index_type< typename LhsTraits::StorageIndex, typename RhsTraits::StorageIndex >::type StorageIndex
Definition: Product.h:37
product_promote_storage_type< typename LhsTraits::StorageKind, typename RhsTraits::StorageKind, internal::product_type< Lhs, Rhs >::ret >::ret StorageKind
Definition: Product.h:35
Definition: ForwardDeclarations.h:21