Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
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// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_PRODUCT_H
12#define EIGEN_PRODUCT_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
19template <typename Lhs, typename Rhs, int Option, typename StorageKind>
20class ProductImpl;
21
22namespace internal {
23
24template <typename Lhs, typename Rhs, int Option>
25struct traits<Product<Lhs, Rhs, Option>> {
26 using LhsCleaned = remove_all_t<Lhs>;
27 using RhsCleaned = remove_all_t<Rhs>;
28 using LhsTraits = traits<LhsCleaned>;
29 using RhsTraits = traits<RhsCleaned>;
30
31 using XprKind = MatrixXpr;
32
33 using Scalar = typename ScalarBinaryOpTraits<typename LhsTraits::Scalar, typename RhsTraits::Scalar>::ReturnType;
34 using StorageKind =
35 typename product_promote_storage_type<typename LhsTraits::StorageKind, typename RhsTraits::StorageKind,
36 internal::product_type<Lhs, Rhs>::value>::ret;
37 using StorageIndex =
38 typename promote_index_type<typename LhsTraits::StorageIndex, typename RhsTraits::StorageIndex>::type;
39
40 enum {
41 RowsAtCompileTime = LhsTraits::RowsAtCompileTime,
42 ColsAtCompileTime = RhsTraits::ColsAtCompileTime,
43 MaxRowsAtCompileTime = LhsTraits::MaxRowsAtCompileTime,
44 MaxColsAtCompileTime = RhsTraits::MaxColsAtCompileTime,
45
46 // FIXME: only needed by GeneralMatrixMatrixTriangular
47 InnerSize = min_size_prefer_fixed(LhsTraits::ColsAtCompileTime, RhsTraits::RowsAtCompileTime),
48
49 // Products served by the coefficient-based evaluator are evaluated in the storage order reported below, which
50 // Reverse, Redux, Block, FindCoeff and others use to index their packets and coefficients. Row-major * row-major
51 // is row-major there, so that the rhs rows can be vectorized. A dense product of inner size 1 is served by it in
52 // LazyCoeffBasedProductMode, and materialized otherwise; unlike product_type, this test agrees between host and
53 // device code.
54 CoeffBasedEval = Option == LazyProduct || (std::is_same<StorageKind, Dense>::value && InnerSize == 1),
55
56 // For other products the storage order is somewhat arbitrary: their evaluators materialize a PlainObject of it.
57 Flags = (MaxRowsAtCompileTime == 1 && MaxColsAtCompileTime != 1) ? RowMajorBit
58 : (MaxColsAtCompileTime == 1 && MaxRowsAtCompileTime != 1) ? 0
59 : (((LhsTraits::Flags & NoPreferredStorageOrderBit) && (RhsTraits::Flags & RowMajorBit)) ||
60 ((RhsTraits::Flags & NoPreferredStorageOrderBit) && (LhsTraits::Flags & RowMajorBit)) ||
61 (CoeffBasedEval && (LhsTraits::Flags & RowMajorBit) && (RhsTraits::Flags & RowMajorBit)))
64 };
65};
66
67struct TransposeProductEnum {
68 // convenience enumerations to specialize transposed products
69 enum : int {
70 Default = 0x00,
71 Matrix = 0x01,
72 Permutation = 0x02,
73 MatrixMatrix = (Matrix << 8) | Matrix,
74 MatrixPermutation = (Matrix << 8) | Permutation,
75 PermutationMatrix = (Permutation << 8) | Matrix
76 };
77};
78template <typename Xpr>
79struct TransposeKind {
80 static constexpr int Kind = is_matrix_base_xpr<Xpr>::value ? TransposeProductEnum::Matrix
81 : is_permutation_base_xpr<Xpr>::value ? TransposeProductEnum::Permutation
82 : TransposeProductEnum::Default;
83};
84
85template <typename Lhs, typename Rhs>
86struct TransposeProductKind {
87 static constexpr int Kind = (TransposeKind<Lhs>::Kind << 8) | TransposeKind<Rhs>::Kind;
88};
89
90template <typename Lhs, typename Rhs, int Option, int Kind = TransposeProductKind<Lhs, Rhs>::Kind>
91struct product_transpose_helper {
92 // by default, don't optimize the transposed product
93 using Derived = Product<Lhs, Rhs, Option>;
94 using Scalar = typename Derived::Scalar;
95 using TransposeType = Transpose<const Derived>;
96 using ConjugateTransposeType = CwiseUnaryOp<scalar_conjugate_op<Scalar>, TransposeType>;
97 using AdjointType = std::conditional_t<NumTraits<Scalar>::IsComplex, ConjugateTransposeType, TransposeType>;
98
99 // return (lhs * rhs)^T
100 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TransposeType run_transpose(const Derived& derived) {
101 return TransposeType(derived);
102 }
103 // return (lhs * rhs)^H
104 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE AdjointType run_adjoint(const Derived& derived) {
105 return AdjointType(TransposeType(derived));
106 }
107};
108
109template <typename Lhs, typename Rhs, int Option>
110struct product_transpose_helper<Lhs, Rhs, Option, TransposeProductEnum::MatrixMatrix> {
111 // expand the transposed matrix-matrix product
112 using Derived = Product<Lhs, Rhs, Option>;
113
114 using LhsScalar = typename traits<Lhs>::Scalar;
115 using LhsTransposeType = typename DenseBase<Lhs>::ConstTransposeReturnType;
116 using LhsConjugateTransposeType = CwiseUnaryOp<scalar_conjugate_op<LhsScalar>, LhsTransposeType>;
117 using LhsAdjointType =
118 std::conditional_t<NumTraits<LhsScalar>::IsComplex, LhsConjugateTransposeType, LhsTransposeType>;
119
120 using RhsScalar = typename traits<Rhs>::Scalar;
121 using RhsTransposeType = typename DenseBase<Rhs>::ConstTransposeReturnType;
122 using RhsConjugateTransposeType = CwiseUnaryOp<scalar_conjugate_op<RhsScalar>, RhsTransposeType>;
123 using RhsAdjointType =
124 std::conditional_t<NumTraits<RhsScalar>::IsComplex, RhsConjugateTransposeType, RhsTransposeType>;
125
126 using TransposeType = Product<RhsTransposeType, LhsTransposeType, Option>;
127 using AdjointType = Product<RhsAdjointType, LhsAdjointType, Option>;
128
129 // return rhs^T * lhs^T
130 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TransposeType run_transpose(const Derived& derived) {
131 return TransposeType(RhsTransposeType(derived.rhs()), LhsTransposeType(derived.lhs()));
132 }
133 // return rhs^H * lhs^H
134 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE AdjointType run_adjoint(const Derived& derived) {
135 return AdjointType(RhsAdjointType(RhsTransposeType(derived.rhs())),
136 LhsAdjointType(LhsTransposeType(derived.lhs())));
137 }
138};
139template <typename Lhs, typename Rhs, int Option>
140struct product_transpose_helper<Lhs, Rhs, Option, TransposeProductEnum::PermutationMatrix> {
141 // expand the transposed permutation-matrix product
142 using Derived = Product<Lhs, Rhs, Option>;
143
144 using LhsInverseType = typename PermutationBase<Lhs>::InverseReturnType;
145
146 using RhsScalar = typename traits<Rhs>::Scalar;
147 using RhsTransposeType = typename DenseBase<Rhs>::ConstTransposeReturnType;
148 using RhsConjugateTransposeType = CwiseUnaryOp<scalar_conjugate_op<RhsScalar>, RhsTransposeType>;
149 using RhsAdjointType =
150 std::conditional_t<NumTraits<RhsScalar>::IsComplex, RhsConjugateTransposeType, RhsTransposeType>;
151
152 using TransposeType = Product<RhsTransposeType, LhsInverseType, Option>;
153 using AdjointType = Product<RhsAdjointType, LhsInverseType, Option>;
154
155 // return rhs^T * lhs^-1
156 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TransposeType run_transpose(const Derived& derived) {
157 return TransposeType(RhsTransposeType(derived.rhs()), LhsInverseType(derived.lhs()));
158 }
159 // return rhs^H * lhs^-1
160 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE AdjointType run_adjoint(const Derived& derived) {
161 return AdjointType(RhsAdjointType(RhsTransposeType(derived.rhs())), LhsInverseType(derived.lhs()));
162 }
163};
164template <typename Lhs, typename Rhs, int Option>
165struct product_transpose_helper<Lhs, Rhs, Option, TransposeProductEnum::MatrixPermutation> {
166 // expand the transposed matrix-permutation product
167 using Derived = Product<Lhs, Rhs, Option>;
168
169 using LhsScalar = typename traits<Lhs>::Scalar;
170 using LhsTransposeType = typename DenseBase<Lhs>::ConstTransposeReturnType;
171 using LhsConjugateTransposeType = CwiseUnaryOp<scalar_conjugate_op<LhsScalar>, LhsTransposeType>;
172 using LhsAdjointType =
173 std::conditional_t<NumTraits<LhsScalar>::IsComplex, LhsConjugateTransposeType, LhsTransposeType>;
174
175 using RhsInverseType = typename PermutationBase<Rhs>::InverseReturnType;
176
177 using TransposeType = Product<RhsInverseType, LhsTransposeType, Option>;
178 using AdjointType = Product<RhsInverseType, LhsAdjointType, Option>;
179
180 // return rhs^-1 * lhs^T
181 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TransposeType run_transpose(const Derived& derived) {
182 return TransposeType(RhsInverseType(derived.rhs()), LhsTransposeType(derived.lhs()));
183 }
184 // return rhs^-1 * lhs^H
185 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE AdjointType run_adjoint(const Derived& derived) {
186 return AdjointType(RhsInverseType(derived.rhs()), LhsAdjointType(LhsTransposeType(derived.lhs())));
187 }
188};
189
190} // end namespace internal
191
206template <typename Lhs_, typename Rhs_, int Option>
207class Product
208 : public ProductImpl<Lhs_, Rhs_, Option,
209 typename internal::product_promote_storage_type<
210 typename internal::traits<Lhs_>::StorageKind, typename internal::traits<Rhs_>::StorageKind,
211 internal::product_type<Lhs_, Rhs_>::value>::ret> {
212 public:
213 using Lhs = Lhs_;
214 using Rhs = Rhs_;
215
216 using Base =
217 typename ProductImpl<Lhs, Rhs, Option,
218 typename internal::product_promote_storage_type<
219 typename internal::traits<Lhs>::StorageKind, typename internal::traits<Rhs>::StorageKind,
220 internal::product_type<Lhs, Rhs>::value>::ret>::Base;
221 EIGEN_GENERIC_PUBLIC_INTERFACE(Product)
222
223 using LhsNested = typename internal::ref_selector<Lhs>::type;
224 using RhsNested = typename internal::ref_selector<Rhs>::type;
225 using LhsNestedCleaned = internal::remove_all_t<LhsNested>;
226 using RhsNestedCleaned = internal::remove_all_t<RhsNested>;
227
228 using TransposeReturnType = typename internal::product_transpose_helper<Lhs, Rhs, Option>::TransposeType;
229 using AdjointReturnType = typename internal::product_transpose_helper<Lhs, Rhs, Option>::AdjointType;
230
231 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Product(const Lhs& lhs, const Rhs& rhs) : m_lhs(lhs), m_rhs(rhs) {
232 EIGEN_INTERNAL_ASSERT_PRODUCT_SHAPE(lhs, rhs);
233 }
234
235 EIGEN_DEVICE_FUNC constexpr Index rows() const noexcept { return m_lhs.rows(); }
236 EIGEN_DEVICE_FUNC constexpr Index cols() const noexcept { return m_rhs.cols(); }
237
238 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE const LhsNestedCleaned& lhs() const { return m_lhs; }
239 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE const RhsNestedCleaned& rhs() const { return m_rhs; }
240
241 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TransposeReturnType transpose() const {
242 return internal::product_transpose_helper<Lhs, Rhs, Option>::run_transpose(*this);
243 }
244 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE AdjointReturnType adjoint() const {
245 return internal::product_transpose_helper<Lhs, Rhs, Option>::run_adjoint(*this);
246 }
247
248 protected:
249 LhsNested m_lhs;
250 RhsNested m_rhs;
251};
252
253namespace internal {
254
255template <typename Lhs, typename Rhs, int Option, int ProductTag = internal::product_type<Lhs, Rhs>::value>
256class dense_product_base : public internal::dense_xpr_base<Product<Lhs, Rhs, Option>>::type {};
257
259template <typename Lhs, typename Rhs, int Option>
260class dense_product_base<Lhs, Rhs, Option, InnerProduct>
261 : public internal::dense_xpr_base<Product<Lhs, Rhs, Option>>::type {
262 using ProductXpr = Product<Lhs, Rhs, Option>;
263 using Base = typename internal::dense_xpr_base<ProductXpr>::type;
264
265 public:
266 using Base::derived;
267 using Scalar = typename Base::Scalar;
268
269 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE operator const Scalar() const {
270 return internal::evaluator<ProductXpr>(derived()).coeff(0, 0);
271 }
272};
273
274} // namespace internal
275
276// Generic API dispatcher
277template <typename Lhs, typename Rhs, int Option, typename StorageKind>
278class ProductImpl : public internal::generic_xpr_base<Product<Lhs, Rhs, Option>, MatrixXpr, StorageKind>::type {
279 public:
280 using Base = typename internal::generic_xpr_base<Product<Lhs, Rhs, Option>, MatrixXpr, StorageKind>::type;
281};
282
283template <typename Lhs, typename Rhs, int Option>
284class ProductImpl<Lhs, Rhs, Option, Dense> : public internal::dense_product_base<Lhs, Rhs, Option> {
285 using Derived = Product<Lhs, Rhs, Option>;
286
287 public:
288 using Base = typename internal::dense_product_base<Lhs, Rhs, Option>;
289 EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
290 protected:
291 enum {
292 IsOneByOne = (RowsAtCompileTime == 1 || RowsAtCompileTime == Dynamic) &&
293 (ColsAtCompileTime == 1 || ColsAtCompileTime == Dynamic),
294 EnableCoeff = IsOneByOne || Option == LazyProduct
295 };
296
297 public:
298 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar coeff(Index row, Index col) const {
299 EIGEN_INTERNAL_CHECK_SHAPE((Option == LazyProduct) || (this->rows() == 1 && this->cols() == 1),
300 THIS_METHOD_IS_ONLY_FOR_INNER_OR_LAZY_PRODUCTS,
301 std::conditional_t<Option == LazyProduct, internal::dimension_relation<1, 1>,
302 internal::fixed_dimensions<Derived, 1, 1>>);
303
304 return internal::evaluator<Derived>(derived()).coeff(row, col);
305 }
306
307 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar coeff(Index i) const {
308 EIGEN_INTERNAL_CHECK_SHAPE((Option == LazyProduct) || (this->rows() == 1 && this->cols() == 1),
309 THIS_METHOD_IS_ONLY_FOR_INNER_OR_LAZY_PRODUCTS,
310 std::conditional_t<Option == LazyProduct, internal::dimension_relation<1, 1>,
311 internal::fixed_dimensions<Derived, 1, 1>>);
312
313 return internal::evaluator<Derived>(derived()).coeff(i);
314 }
315};
316
317} // end namespace Eigen
318
319#endif // EIGEN_PRODUCT_H
Expression of the product of two arbitrary matrices or vectors.
Definition Product.h:211
constexpr unsigned int NoPreferredStorageOrderBit
Definition Constants.h:183
constexpr unsigned int RowMajorBit
Definition Constants.h:71
Definition Constants.h:557