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
85// (A * B)^T = B^T * A^T and (A * B)^H = B^H * A^H sum b * a instead of a * b, which needs a commutative scalar product.
86// Without it the transposed matrix-matrix product is not expanded.
87template <typename Lhs, typename Rhs, int RawKind>
88struct transpose_product_is_expandable : std::true_type {};
89
90template <typename Lhs, typename Rhs>
91struct transpose_product_is_expandable<Lhs, Rhs, TransposeProductEnum::MatrixMatrix>
92 : bool_constant<functor_is_commutative<
93 scalar_product_op<typename traits<Lhs>::Scalar, typename traits<Rhs>::Scalar>>::value &&
94 functor_is_commutative<
95 scalar_product_op<typename traits<Rhs>::Scalar, typename traits<Lhs>::Scalar>>::value> {};
96
97template <typename Lhs, typename Rhs>
98struct TransposeProductKind {
99 static constexpr int RawKind = (TransposeKind<Lhs>::Kind << 8) | TransposeKind<Rhs>::Kind;
100 static constexpr int Kind =
101 transpose_product_is_expandable<Lhs, Rhs, RawKind>::value ? RawKind : int(TransposeProductEnum::Default);
102};
103
104template <typename Lhs, typename Rhs, int Option, int Kind = TransposeProductKind<Lhs, Rhs>::Kind>
105struct product_transpose_helper {
106 // by default, don't optimize the transposed product
107 using Derived = Product<Lhs, Rhs, Option>;
108 using Scalar = typename Derived::Scalar;
109 using TransposeType = Transpose<const Derived>;
110 using ConjugateTransposeType = CwiseUnaryOp<scalar_conjugate_op<Scalar>, TransposeType>;
111 using AdjointType = std::conditional_t<NumTraits<Scalar>::IsComplex, ConjugateTransposeType, TransposeType>;
112
113 // return (lhs * rhs)^T
114 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TransposeType run_transpose(const Derived& derived) {
115 return TransposeType(derived);
116 }
117 // return (lhs * rhs)^H
118 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE AdjointType run_adjoint(const Derived& derived) {
119 return AdjointType(TransposeType(derived));
120 }
121};
122
123template <typename Lhs, typename Rhs, int Option>
124struct product_transpose_helper<Lhs, Rhs, Option, TransposeProductEnum::MatrixMatrix> {
125 // expand the transposed matrix-matrix product
126 using Derived = Product<Lhs, Rhs, Option>;
127
128 using LhsScalar = typename traits<Lhs>::Scalar;
129 using LhsTransposeType = typename DenseBase<Lhs>::ConstTransposeReturnType;
130 using LhsConjugateTransposeType = CwiseUnaryOp<scalar_conjugate_op<LhsScalar>, LhsTransposeType>;
131 using LhsAdjointType =
132 std::conditional_t<NumTraits<LhsScalar>::IsComplex, LhsConjugateTransposeType, LhsTransposeType>;
133
134 using RhsScalar = typename traits<Rhs>::Scalar;
135 using RhsTransposeType = typename DenseBase<Rhs>::ConstTransposeReturnType;
136 using RhsConjugateTransposeType = CwiseUnaryOp<scalar_conjugate_op<RhsScalar>, RhsTransposeType>;
137 using RhsAdjointType =
138 std::conditional_t<NumTraits<RhsScalar>::IsComplex, RhsConjugateTransposeType, RhsTransposeType>;
139
140 using TransposeType = Product<RhsTransposeType, LhsTransposeType, Option>;
141 using AdjointType = Product<RhsAdjointType, LhsAdjointType, Option>;
142
143 // return rhs^T * lhs^T
144 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TransposeType run_transpose(const Derived& derived) {
145 return TransposeType(RhsTransposeType(derived.rhs()), LhsTransposeType(derived.lhs()));
146 }
147 // return rhs^H * lhs^H
148 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE AdjointType run_adjoint(const Derived& derived) {
149 return AdjointType(RhsAdjointType(RhsTransposeType(derived.rhs())),
150 LhsAdjointType(LhsTransposeType(derived.lhs())));
151 }
152};
153template <typename Lhs, typename Rhs, int Option>
154struct product_transpose_helper<Lhs, Rhs, Option, TransposeProductEnum::PermutationMatrix> {
155 // expand the transposed permutation-matrix product
156 using Derived = Product<Lhs, Rhs, Option>;
157
158 using LhsInverseType = typename PermutationBase<Lhs>::InverseReturnType;
159
160 using RhsScalar = typename traits<Rhs>::Scalar;
161 using RhsTransposeType = typename DenseBase<Rhs>::ConstTransposeReturnType;
162 using RhsConjugateTransposeType = CwiseUnaryOp<scalar_conjugate_op<RhsScalar>, RhsTransposeType>;
163 using RhsAdjointType =
164 std::conditional_t<NumTraits<RhsScalar>::IsComplex, RhsConjugateTransposeType, RhsTransposeType>;
165
166 using TransposeType = Product<RhsTransposeType, LhsInverseType, Option>;
167 using AdjointType = Product<RhsAdjointType, LhsInverseType, Option>;
168
169 // return rhs^T * lhs^-1
170 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TransposeType run_transpose(const Derived& derived) {
171 return TransposeType(RhsTransposeType(derived.rhs()), LhsInverseType(derived.lhs()));
172 }
173 // return rhs^H * lhs^-1
174 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE AdjointType run_adjoint(const Derived& derived) {
175 return AdjointType(RhsAdjointType(RhsTransposeType(derived.rhs())), LhsInverseType(derived.lhs()));
176 }
177};
178template <typename Lhs, typename Rhs, int Option>
179struct product_transpose_helper<Lhs, Rhs, Option, TransposeProductEnum::MatrixPermutation> {
180 // expand the transposed matrix-permutation product
181 using Derived = Product<Lhs, Rhs, Option>;
182
183 using LhsScalar = typename traits<Lhs>::Scalar;
184 using LhsTransposeType = typename DenseBase<Lhs>::ConstTransposeReturnType;
185 using LhsConjugateTransposeType = CwiseUnaryOp<scalar_conjugate_op<LhsScalar>, LhsTransposeType>;
186 using LhsAdjointType =
187 std::conditional_t<NumTraits<LhsScalar>::IsComplex, LhsConjugateTransposeType, LhsTransposeType>;
188
189 using RhsInverseType = typename PermutationBase<Rhs>::InverseReturnType;
190
191 using TransposeType = Product<RhsInverseType, LhsTransposeType, Option>;
192 using AdjointType = Product<RhsInverseType, LhsAdjointType, Option>;
193
194 // return rhs^-1 * lhs^T
195 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TransposeType run_transpose(const Derived& derived) {
196 return TransposeType(RhsInverseType(derived.rhs()), LhsTransposeType(derived.lhs()));
197 }
198 // return rhs^-1 * lhs^H
199 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE AdjointType run_adjoint(const Derived& derived) {
200 return AdjointType(RhsInverseType(derived.rhs()), LhsAdjointType(LhsTransposeType(derived.lhs())));
201 }
202};
203
204} // end namespace internal
205
220template <typename Lhs_, typename Rhs_, int Option>
221class Product
222 : public ProductImpl<Lhs_, Rhs_, Option,
223 typename internal::product_promote_storage_type<
224 typename internal::traits<Lhs_>::StorageKind, typename internal::traits<Rhs_>::StorageKind,
225 internal::product_type<Lhs_, Rhs_>::value>::ret> {
226 public:
227 using Lhs = Lhs_;
228 using Rhs = Rhs_;
229
230 using Base =
231 typename ProductImpl<Lhs, Rhs, Option,
232 typename internal::product_promote_storage_type<
233 typename internal::traits<Lhs>::StorageKind, typename internal::traits<Rhs>::StorageKind,
234 internal::product_type<Lhs, Rhs>::value>::ret>::Base;
235 EIGEN_GENERIC_PUBLIC_INTERFACE(Product)
236
237 using LhsNested = typename internal::ref_selector<Lhs>::type;
238 using RhsNested = typename internal::ref_selector<Rhs>::type;
239 using LhsNestedCleaned = internal::remove_all_t<LhsNested>;
240 using RhsNestedCleaned = internal::remove_all_t<RhsNested>;
241
242 using TransposeReturnType = typename internal::product_transpose_helper<Lhs, Rhs, Option>::TransposeType;
243 using AdjointReturnType = typename internal::product_transpose_helper<Lhs, Rhs, Option>::AdjointType;
244
245 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Product(const Lhs& lhs, const Rhs& rhs) : m_lhs(lhs), m_rhs(rhs) {
246 EIGEN_INTERNAL_ASSERT_PRODUCT_SHAPE(lhs, rhs);
247 }
248
249 EIGEN_DEVICE_FUNC constexpr Index rows() const noexcept { return m_lhs.rows(); }
250 EIGEN_DEVICE_FUNC constexpr Index cols() const noexcept { return m_rhs.cols(); }
251
252 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE const LhsNestedCleaned& lhs() const { return m_lhs; }
253 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE const RhsNestedCleaned& rhs() const { return m_rhs; }
254
255 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TransposeReturnType transpose() const {
256 return internal::product_transpose_helper<Lhs, Rhs, Option>::run_transpose(*this);
257 }
258 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE AdjointReturnType adjoint() const {
259 return internal::product_transpose_helper<Lhs, Rhs, Option>::run_adjoint(*this);
260 }
261
262 protected:
263 LhsNested m_lhs;
264 RhsNested m_rhs;
265};
266
267namespace internal {
268
269template <typename Lhs, typename Rhs, int Option, int ProductTag = internal::product_type<Lhs, Rhs>::value>
270class dense_product_base : public internal::dense_xpr_base<Product<Lhs, Rhs, Option>>::type {};
271
273template <typename Lhs, typename Rhs, int Option>
274class dense_product_base<Lhs, Rhs, Option, InnerProduct>
275 : public internal::dense_xpr_base<Product<Lhs, Rhs, Option>>::type {
276 using ProductXpr = Product<Lhs, Rhs, Option>;
277 using Base = typename internal::dense_xpr_base<ProductXpr>::type;
278
279 public:
280 using Base::derived;
281 using Scalar = typename Base::Scalar;
282
283 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE operator const Scalar() const {
284 return internal::evaluator<ProductXpr>(derived()).coeff(0, 0);
285 }
286};
287
288} // namespace internal
289
290// Generic API dispatcher
291template <typename Lhs, typename Rhs, int Option, typename StorageKind>
292class ProductImpl : public internal::generic_xpr_base<Product<Lhs, Rhs, Option>, MatrixXpr, StorageKind>::type {
293 public:
294 using Base = typename internal::generic_xpr_base<Product<Lhs, Rhs, Option>, MatrixXpr, StorageKind>::type;
295};
296
297template <typename Lhs, typename Rhs, int Option>
298class ProductImpl<Lhs, Rhs, Option, Dense> : public internal::dense_product_base<Lhs, Rhs, Option> {
299 using Derived = Product<Lhs, Rhs, Option>;
300
301 public:
302 using Base = typename internal::dense_product_base<Lhs, Rhs, Option>;
303 EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
304 protected:
305 enum {
306 IsOneByOne = (RowsAtCompileTime == 1 || RowsAtCompileTime == Dynamic) &&
307 (ColsAtCompileTime == 1 || ColsAtCompileTime == Dynamic),
308 EnableCoeff = IsOneByOne || Option == LazyProduct
309 };
310
311 public:
312 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar coeff(Index row, Index col) const {
313 EIGEN_INTERNAL_CHECK_SHAPE((Option == LazyProduct) || (this->rows() == 1 && this->cols() == 1),
314 THIS_METHOD_IS_ONLY_FOR_INNER_OR_LAZY_PRODUCTS,
315 std::conditional_t<Option == LazyProduct, internal::dimension_relation<1, 1>,
316 internal::fixed_dimensions<Derived, 1, 1>>);
317
318 return internal::evaluator<Derived>(derived()).coeff(row, col);
319 }
320
321 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar coeff(Index i) const {
322 EIGEN_INTERNAL_CHECK_SHAPE((Option == LazyProduct) || (this->rows() == 1 && this->cols() == 1),
323 THIS_METHOD_IS_ONLY_FOR_INNER_OR_LAZY_PRODUCTS,
324 std::conditional_t<Option == LazyProduct, internal::dimension_relation<1, 1>,
325 internal::fixed_dimensions<Derived, 1, 1>>);
326
327 return internal::evaluator<Derived>(derived()).coeff(i);
328 }
329};
330
331} // end namespace Eigen
332
333#endif // EIGEN_PRODUCT_H
Expression of the product of two arbitrary matrices or vectors.
Definition Product.h:225
constexpr unsigned int NoPreferredStorageOrderBit
Definition Constants.h:183
constexpr unsigned int RowMajorBit
Definition Constants.h:71
Definition Constants.h:557