Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
XprHelper.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
5// Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
6//
7// This Source Code Form is subject to the terms of the Mozilla
8// Public License v. 2.0. If a copy of the MPL was not distributed
9// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10// SPDX-License-Identifier: MPL-2.0
11
12#ifndef EIGEN_XPRHELPER_H
13#define EIGEN_XPRHELPER_H
14
15// IWYU pragma: private
16#include "../InternalHeaderCheck.h"
17
18namespace Eigen {
19
20namespace internal {
21
22// useful for unsigned / signed integer comparisons when idx is intended to be non-negative
23template <typename IndexType>
24EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::make_unsigned_t<IndexType> returnUnsignedIndexValue(const IndexType& idx) {
25 EIGEN_STATIC_ASSERT((NumTraits<IndexType>::IsInteger), THIS FUNCTION IS FOR INTEGER TYPES)
26 eigen_internal_assert(idx >= 0 && "Index value is negative and target type is unsigned");
27 using UnsignedType = std::make_unsigned_t<IndexType>;
28 return static_cast<UnsignedType>(idx);
29}
30
31template <typename IndexDest, typename IndexSrc, bool IndexDestIsInteger = NumTraits<IndexDest>::IsInteger,
32 bool IndexDestIsSigned = NumTraits<IndexDest>::IsSigned,
33 bool IndexSrcIsInteger = NumTraits<IndexSrc>::IsInteger,
34 bool IndexSrcIsSigned = NumTraits<IndexSrc>::IsSigned>
35struct convert_index_impl {
36 static inline EIGEN_DEVICE_FUNC IndexDest run(const IndexSrc& idx) {
37 eigen_internal_assert(idx <= NumTraits<IndexDest>::highest() && "Index value is too big for target type");
38 return static_cast<IndexDest>(idx);
39 }
40};
41template <typename IndexDest, typename IndexSrc>
42struct convert_index_impl<IndexDest, IndexSrc, true, true, true, false> {
43 // IndexDest is a signed integer
44 // IndexSrc is an unsigned integer
45 static inline EIGEN_DEVICE_FUNC IndexDest run(const IndexSrc& idx) {
46 eigen_internal_assert(idx <= returnUnsignedIndexValue(NumTraits<IndexDest>::highest()) &&
47 "Index value is too big for target type");
48 return static_cast<IndexDest>(idx);
49 }
50};
51template <typename IndexDest, typename IndexSrc>
52struct convert_index_impl<IndexDest, IndexSrc, true, false, true, true> {
53 // IndexDest is an unsigned integer
54 // IndexSrc is a signed integer
55 static inline EIGEN_DEVICE_FUNC IndexDest run(const IndexSrc& idx) {
56 eigen_internal_assert(returnUnsignedIndexValue(idx) <= NumTraits<IndexDest>::highest() &&
57 "Index value is too big for target type");
58 return static_cast<IndexDest>(idx);
59 }
60};
61
62template <typename IndexDest, typename IndexSrc>
63EIGEN_DEVICE_FUNC inline IndexDest convert_index(const IndexSrc& idx) {
64 return convert_index_impl<IndexDest, IndexSrc>::run(idx);
65}
66
67// true if both types are not valid index types
68template <typename RowIndices, typename ColIndices>
69struct valid_indexed_view_overload : bool_constant<!(internal::is_valid_index_type<RowIndices>::value &&
70 internal::is_valid_index_type<ColIndices>::value)> {};
71
72template <typename T, typename = void>
73struct is_eigen_expression : std::false_type {};
74// The conversion check may instantiate EigenBase<X> (MSVC 14.29 does), which is a hard error unless
75// traits<X>::StorageKind exists. Every class derived from EigenBase<X> has it, so requiring it rejects no expression.
76template <typename T>
77struct is_eigen_expression<T, void_t<decltype(std::declval<const T&>().derived()),
78 typename traits<typename shape_expression<T>::type>::StorageKind>>
79 : std::is_convertible<const remove_all_t<T>*, const EigenBase<typename shape_expression<T>::type>*> {};
80
81// promote_scalar_arg is an helper used in operation between an expression and a scalar, like:
82// expression * scalar
83// Its role is to determine how the type T of the scalar operand should be promoted given the scalar type ExprScalar of
84// the given expression. The IsSupported template parameter must be provided by the caller as:
85// internal::has_ReturnType<ScalarBinaryOpTraits<ExprScalar,T,op> >::value using the proper order for ExprScalar and T.
86// Then the logic is as follows:
87// - if the operation is natively supported as defined by IsSupported, then the scalar type is not promoted, and T is
88// returned.
89// - otherwise, NumTraits<ExprScalar>::Literal is returned if T is implicitly convertible to
90// NumTraits<ExprScalar>::Literal AND that this does not imply a float to integer conversion.
91// - otherwise, ExprScalar is returned if T is implicitly convertible to ExprScalar AND that this does not imply a
92// float to integer conversion.
93// - In all other cases, the promoted type is not defined, and the respective operation is thus invalid and not
94// available (SFINAE).
95template <typename ExprScalar, typename T, bool IsSupported>
96struct promote_scalar_arg;
97
98template <typename S, typename T>
99struct promote_scalar_arg<S, T, true> {
100 using type = T;
101};
102
103// Recursively check safe conversion to PromotedType, and then ExprScalar if they are different.
104template <typename ExprScalar, typename T, typename PromotedType,
105 bool ConvertibleToLiteral = std::is_convertible<T, PromotedType>::value,
106 bool IsSafe = NumTraits<T>::IsInteger || !NumTraits<PromotedType>::IsInteger>
107struct promote_scalar_arg_unsupported;
108
109// Start recursion with NumTraits<ExprScalar>::Literal
110template <typename S, typename T>
111struct promote_scalar_arg<S, T, false> : promote_scalar_arg_unsupported<S, T, typename NumTraits<S>::Literal> {};
112
113// We found a match!
114template <typename S, typename T, typename PromotedType>
115struct promote_scalar_arg_unsupported<S, T, PromotedType, true, true> {
116 using type = PromotedType;
117};
118
119// No match, but no real-to-integer issues, and ExprScalar and current PromotedType are different,
120// so let's try to promote to ExprScalar
121template <typename ExprScalar, typename T, typename PromotedType>
122struct promote_scalar_arg_unsupported<ExprScalar, T, PromotedType, false, true>
123 : promote_scalar_arg_unsupported<ExprScalar, T, ExprScalar> {};
124
125// Unsafe real-to-integer, let's stop.
126template <typename S, typename T, typename PromotedType, bool ConvertibleToLiteral>
127struct promote_scalar_arg_unsupported<S, T, PromotedType, ConvertibleToLiteral, false> {};
128
129// T is not even convertible to ExprScalar, let's stop.
130template <typename S, typename T>
131struct promote_scalar_arg_unsupported<S, T, S, false, true> {};
132
133// classes inheriting no_assignment_operator don't generate a default operator=.
134class no_assignment_operator {
135 no_assignment_operator& operator=(const no_assignment_operator&) = delete;
136
137 protected:
138 EIGEN_DEFAULT_COPY_CONSTRUCTOR(no_assignment_operator)
139 EIGEN_DEFAULT_EMPTY_CONSTRUCTOR_AND_DESTRUCTOR(no_assignment_operator)
140};
141
143template <typename I1, typename I2>
144struct promote_index_type {
145 using type = std::conditional_t<(sizeof(I1) < sizeof(I2)), I2, I1>;
146};
147
152template <typename T, int Value>
153class variable_if_dynamic {
154 public:
155 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE explicit variable_if_dynamic(T v) noexcept {
156 EIGEN_ONLY_USED_FOR_DEBUG(v);
157 eigen_plain_assert(v == T(Value));
158 }
159 EIGEN_DEVICE_FUNC static constexpr T value() { return T(Value); }
160 EIGEN_DEVICE_FUNC constexpr operator T() const { return T(Value); }
161 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void setValue(T v) const {
162 EIGEN_ONLY_USED_FOR_DEBUG(v);
163 eigen_assert(v == T(Value));
164 }
165};
166
167template <typename T>
168class variable_if_dynamic<T, Dynamic> {
169 T m_value;
170
171 public:
172 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE explicit variable_if_dynamic(T value = 0) noexcept : m_value(value) {}
173 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE T value() const { return m_value; }
174 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE operator T() const { return m_value; }
175 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void setValue(T value) { m_value = value; }
176};
177
180template <typename T, int Value>
181class variable_if_dynamicindex {
182 public:
183 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE explicit variable_if_dynamicindex(T v) {
184 EIGEN_ONLY_USED_FOR_DEBUG(v);
185 eigen_assert(v == T(Value));
186 }
187 EIGEN_DEVICE_FUNC static constexpr T value() { return T(Value); }
188 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr void setValue(T) {}
189};
190
191template <typename T>
192class variable_if_dynamicindex<T, DynamicIndex> {
193 T m_value;
194 EIGEN_DEVICE_FUNC variable_if_dynamicindex() { eigen_assert(false); }
195
196 public:
197 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE explicit variable_if_dynamicindex(T value) : m_value(value) {}
198 EIGEN_DEVICE_FUNC constexpr T EIGEN_STRONG_INLINE value() const { return m_value; }
199 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void setValue(T value) { m_value = value; }
200};
201
202template <typename T>
203struct functor_traits {
204 enum { Cost = 10, PacketAccess = false, IsRepeatable = false };
205};
206
207// Marks a binary functor as commutative: f(a, b) == f(b, a). Reductions may then reorder
208// operands, not merely re-associate them, which enables faster accumulation. Deliberately a
209// separate trait rather than a functor_traits member: user code specializes functor_traits
210// wholesale, so a new member there would break every existing specialization. The default is
211// conservative; operand order is preserved unless a functor opts in.
212template <typename Func>
213struct functor_is_commutative : std::false_type {};
214
215// estimates the cost of lazily evaluating a generic functor by unwinding the expression
216template <typename Xpr>
217struct nested_functor_cost {
218 static constexpr Index Cost = static_cast<Index>(functor_traits<Xpr>::Cost);
219};
220
221template <typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
222struct nested_functor_cost<Matrix<Scalar, Rows, Cols, Options, MaxRows, MaxCols>> {
223 static constexpr Index Cost = 1;
224};
225
226template <typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
227struct nested_functor_cost<Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols>> {
228 static constexpr Index Cost = 1;
229};
230
231// TODO: assign a cost to the stride type?
232template <typename PlainObjectType, int MapOptions, typename StrideType>
233struct nested_functor_cost<Map<PlainObjectType, MapOptions, StrideType>> : nested_functor_cost<PlainObjectType> {};
234
235template <typename Func, typename Xpr>
236struct nested_functor_cost<CwiseUnaryOp<Func, Xpr>> {
237 using XprCleaned = remove_all_t<Xpr>;
238 using FuncCleaned = remove_all_t<Func>;
239 static constexpr Index Cost = nested_functor_cost<FuncCleaned>::Cost + nested_functor_cost<XprCleaned>::Cost;
240};
241
242template <typename Func, typename Xpr>
243struct nested_functor_cost<CwiseNullaryOp<Func, Xpr>> {
244 using XprCleaned = remove_all_t<Xpr>;
245 using FuncCleaned = remove_all_t<Func>;
246 static constexpr Index Cost = nested_functor_cost<FuncCleaned>::Cost + nested_functor_cost<XprCleaned>::Cost;
247};
248
249template <typename Func, typename LhsXpr, typename RhsXpr>
250struct nested_functor_cost<CwiseBinaryOp<Func, LhsXpr, RhsXpr>> {
251 using LhsXprCleaned = remove_all_t<LhsXpr>;
252 using RhsXprCleaned = remove_all_t<RhsXpr>;
253 using FuncCleaned = remove_all_t<Func>;
254 static constexpr Index Cost = nested_functor_cost<FuncCleaned>::Cost + nested_functor_cost<LhsXprCleaned>::Cost +
255 nested_functor_cost<RhsXprCleaned>::Cost;
256};
257
258template <typename Func, typename LhsXpr, typename MidXpr, typename RhsXpr>
259struct nested_functor_cost<CwiseTernaryOp<Func, LhsXpr, MidXpr, RhsXpr>> {
260 using LhsXprCleaned = remove_all_t<LhsXpr>;
261 using MidXprCleaned = remove_all_t<MidXpr>;
262 using RhsXprCleaned = remove_all_t<RhsXpr>;
263 using FuncCleaned = remove_all_t<Func>;
264 static constexpr Index Cost = nested_functor_cost<FuncCleaned>::Cost + nested_functor_cost<LhsXprCleaned>::Cost +
265 nested_functor_cost<MidXprCleaned>::Cost + nested_functor_cost<RhsXprCleaned>::Cost;
266};
267
268template <typename Xpr>
269struct functor_cost {
270 static constexpr Index Cost = plain_enum_max(nested_functor_cost<Xpr>::Cost, 1);
271};
272
273template <typename T>
274struct packet_traits;
275
276template <typename T>
277struct unpacket_traits;
278
279template <int Size, typename PacketType,
280 bool Stop = Size == Dynamic || (Size % unpacket_traits<PacketType>::size) == 0 ||
281 std::is_same<PacketType, typename unpacket_traits<PacketType>::half>::value>
282struct find_best_packet_helper;
283
284template <int Size, typename PacketType>
285struct find_best_packet_helper<Size, PacketType, true> {
286 using type = PacketType;
287};
288
289template <int Size, typename PacketType>
290struct find_best_packet_helper<Size, PacketType, false> {
291 using type = typename find_best_packet_helper<Size, typename unpacket_traits<PacketType>::half>::type;
292};
293
294template <typename T, int Size>
295struct find_best_packet {
296 using type = typename find_best_packet_helper<Size, typename packet_traits<T>::type>::type;
297};
298
299// Like find_best_packet, but picks the widest packet whose size is <= Size
300// rather than the widest that exactly divides Size. The caller handles any tail.
301template <int Size, typename PacketType,
302 bool Stop = Size == Dynamic || Size >= unpacket_traits<PacketType>::size ||
303 std::is_same<PacketType, typename unpacket_traits<PacketType>::half>::value>
304struct find_largest_packet_helper;
305
306template <int Size, typename PacketType>
307struct find_largest_packet_helper<Size, PacketType, true> {
308 using type = PacketType;
309};
310
311template <int Size, typename PacketType>
312struct find_largest_packet_helper<Size, PacketType, false> {
313 using type = typename find_largest_packet_helper<Size, typename unpacket_traits<PacketType>::half>::type;
314};
315
316template <typename T, int Size>
317struct find_largest_packet {
318 using type = typename find_largest_packet_helper<Size, typename packet_traits<T>::type>::type;
319};
320
321// Pick the packet type for a linear-traversal assignment: the widest packet
322// whose <full-packet count + scalar-tail count> is minimal.
323//
324// find_best_packet picks the widest exact divisor (no scalar tail). That
325// overshoots when no exact divisor exists -- it falls through to the smallest
326// packet, e.g. Packet4f at N=9 float on AVX2 emits 2*SSE + 1 scalar where
327// 1*Packet8f + 1 scalar would do. We prefer find_largest_packet whenever it
328// strictly cuts the op count; otherwise we keep find_best_packet so kernels
329// like LLT/LDLT that rely on exact-fit narrow packets are not disturbed
330// (e.g. 3*Packet2d == 1*Packet4d + 2 scalars at N=6 double on AVX-512, both
331// 3 ops -- keep Packet2d).
332//
333// Only used in LinearVectorizedTraversal, whose tail handling already accepts
334// a partial-packet remainder. InnerVectorized / SliceVectorized still require
335// exact divisibility, so they continue to use find_best_packet.
336template <typename T, int Size>
337struct find_assign_linear_packet {
338 private:
339 using best_type = typename find_best_packet<T, Size>::type;
340 using largest_type = typename find_largest_packet<T, Size>::type;
341 // Op count = full packets + scalar-tail elements (one scalar emit per tail
342 // element under CompleteUnrolling). Both helpers return the max packet for
343 // Dynamic, so the op-count tie there harmlessly resolves to find_best.
344 template <typename P>
345 static constexpr int ops() {
346 constexpr int sz = unpacket_traits<P>::size;
347 return Size == Dynamic ? 0 : Size / sz + Size % sz;
348 }
349
350 public:
351 using type = std::conditional_t<(ops<largest_type>() < ops<best_type>()), largest_type, best_type>;
352};
353
354template <int Size, typename PacketType,
355 bool Stop = (Size == unpacket_traits<PacketType>::size) ||
356 std::is_same<PacketType, typename unpacket_traits<PacketType>::half>::value>
357struct find_packet_by_size_helper;
358template <int Size, typename PacketType>
359struct find_packet_by_size_helper<Size, PacketType, true> {
360 using type = PacketType;
361};
362template <int Size, typename PacketType>
363struct find_packet_by_size_helper<Size, PacketType, false> {
364 using type = typename find_packet_by_size_helper<Size, typename unpacket_traits<PacketType>::half>::type;
365};
366
367template <typename T, int Size>
368struct find_packet_by_size {
369 using type = typename find_packet_by_size_helper<Size, typename packet_traits<T>::type>::type;
370 static constexpr bool value = (Size == unpacket_traits<type>::size);
371};
372template <typename T>
373struct find_packet_by_size<T, 1> {
374 using type = typename unpacket_traits<T>::type;
375 static constexpr bool value = (unpacket_traits<type>::size == 1);
376};
377
378#if EIGEN_MAX_STATIC_ALIGN_BYTES > 0
379constexpr int compute_default_alignment_helper(int ArrayBytes, int AlignmentBytes) {
380 if ((ArrayBytes % AlignmentBytes) == 0) {
381 return AlignmentBytes;
382 } else if (EIGEN_MIN_ALIGN_BYTES < AlignmentBytes) {
383 return compute_default_alignment_helper(ArrayBytes, AlignmentBytes / 2);
384 } else {
385 return 0;
386 }
387}
388#else
389// If static alignment is disabled, no need to bother.
390// This also avoids a division by zero
391constexpr int compute_default_alignment_helper(int ArrayBytes, int AlignmentBytes) {
392 EIGEN_UNUSED_VARIABLE(ArrayBytes);
393 EIGEN_UNUSED_VARIABLE(AlignmentBytes);
394 return 0;
395}
396#endif
397
398template <typename T, int Size>
399struct compute_default_alignment
400 : std::integral_constant<int, compute_default_alignment_helper(Size * sizeof(T), EIGEN_MAX_STATIC_ALIGN_BYTES)> {};
401
402template <typename T>
403struct compute_default_alignment<T, Dynamic> : std::integral_constant<int, EIGEN_MAX_ALIGN_BYTES> {};
404
405template <typename Scalar_, int Rows_, int Cols_,
406 int Options_ = AutoAlign | ((Rows_ == 1 && Cols_ != 1) ? RowMajor
407 : (Cols_ == 1 && Rows_ != 1) ? ColMajor
408 : EIGEN_DEFAULT_MATRIX_STORAGE_ORDER_OPTION),
409 int MaxRows_ = Rows_, int MaxCols_ = Cols_>
410struct make_proper_matrix_type {
411 private:
412 static constexpr bool IsColVector = Cols_ == 1 && Rows_ != 1;
413 static constexpr bool IsRowVector = Rows_ == 1 && Cols_ != 1;
414 static constexpr int Options = IsColVector ? (Options_ | ColMajor) & ~RowMajor
415 : IsRowVector ? (Options_ | RowMajor) & ~ColMajor
416 : Options_;
417
418 public:
419 using type = Matrix<Scalar_, Rows_, Cols_, Options, MaxRows_, MaxCols_>;
420};
421
422constexpr unsigned compute_matrix_flags(int Options) {
423 unsigned row_major_bit = Options & RowMajor ? RowMajorBit : 0;
424 // FIXME currently we still have to handle DirectAccessBit at the expression level to handle DenseCoeffsBase<>
425 // and then propagate this information to the evaluator's flags.
426 // However, I (Gael) think that DirectAccessBit should only matter at the evaluation stage.
427 return DirectAccessBit | LvalueBit | NestByRefBit | row_major_bit;
428}
429
430template <typename XprType>
431struct size_of_xpr_at_compile_time
432 : std::integral_constant<int, size_at_compile_time(traits<XprType>::RowsAtCompileTime,
433 traits<XprType>::ColsAtCompileTime)> {};
434
435/* plain_matrix_type : the difference from eval is that plain_matrix_type is always a plain matrix type,
436 * whereas eval is a const reference in the case of a matrix
437 */
438
439template <typename T, typename StorageKind = typename traits<T>::StorageKind>
440struct plain_matrix_type;
441
442/* plain_object_options : the Options template argument (storage order and alignment) of the plain object that a
443 * decomposition's MatrixType stores: the type's own for a Matrix or Array, the referenced type's for a Ref, whose own
444 * Options is a pointer-alignment requirement instead.
445 */
446template <typename T>
447struct plain_object_options {
448 static constexpr int value = int(traits<T>::Options);
449};
450template <typename PlainObjectType, int Options, typename StrideType>
451struct plain_object_options<Ref<PlainObjectType, Options, StrideType>> : plain_object_options<PlainObjectType> {};
452
453/* is_ref : whether T is a Ref<>, i.e. a decomposition instantiated on it works in the referenced memory. */
454template <typename T>
455struct is_ref : std::false_type {};
456template <typename PlainObjectType, int Options, typename StrideType>
457struct is_ref<Ref<PlainObjectType, Options, StrideType>> : std::true_type {};
458
459template <typename T, typename BaseClassType, int Flags>
460struct plain_matrix_type_dense;
461template <typename T>
462struct plain_matrix_type<T, Dense> {
463 using type = typename plain_matrix_type_dense<T, typename traits<T>::XprKind, traits<T>::Flags>::type;
464};
465template <typename T>
466struct plain_matrix_type<T, DiagonalShape> {
467 using type = typename T::PlainObject;
468};
469
470template <typename T>
471struct plain_matrix_type<T, SkewSymmetricShape> {
472 using type = typename T::PlainObject;
473};
474
475template <typename T, int Flags>
476struct plain_matrix_type_dense<T, MatrixXpr, Flags> {
477 using type = Matrix<typename traits<T>::Scalar, traits<T>::RowsAtCompileTime, traits<T>::ColsAtCompileTime,
478 AutoAlign | (Flags & RowMajorBit ? RowMajor : ColMajor), traits<T>::MaxRowsAtCompileTime,
479 traits<T>::MaxColsAtCompileTime>;
480};
481
482template <typename T, int Flags>
483struct plain_matrix_type_dense<T, ArrayXpr, Flags> {
484 using type = Array<typename traits<T>::Scalar, traits<T>::RowsAtCompileTime, traits<T>::ColsAtCompileTime,
485 AutoAlign | (Flags & RowMajorBit ? RowMajor : ColMajor), traits<T>::MaxRowsAtCompileTime,
486 traits<T>::MaxColsAtCompileTime>;
487};
488
489/* eval : the return type of eval(). For matrices, this is just a const reference
490 * in order to avoid a useless copy
491 */
492
493template <typename T, typename StorageKind = typename traits<T>::StorageKind>
494struct eval;
495
496template <typename T>
497struct eval<T, Dense> {
498 using type = typename plain_matrix_type<T>::type;
499};
500
501template <typename T>
502struct eval<T, DiagonalShape> {
503 using type = typename plain_matrix_type<T>::type;
504};
505
506template <typename T>
507struct eval<T, SkewSymmetricShape> {
508 using type = typename plain_matrix_type<T>::type;
509};
510
511// for matrices, no need to evaluate, just use a const reference to avoid a useless copy
512template <typename Scalar_, int Rows_, int Cols_, int Options_, int MaxRows_, int MaxCols_>
513struct eval<Matrix<Scalar_, Rows_, Cols_, Options_, MaxRows_, MaxCols_>, Dense> {
514 using type = const Matrix<Scalar_, Rows_, Cols_, Options_, MaxRows_, MaxCols_>&;
515};
516
517template <typename Scalar_, int Rows_, int Cols_, int Options_, int MaxRows_, int MaxCols_>
518struct eval<Array<Scalar_, Rows_, Cols_, Options_, MaxRows_, MaxCols_>, Dense> {
519 using type = const Array<Scalar_, Rows_, Cols_, Options_, MaxRows_, MaxCols_>&;
520};
521
522/* similar to plain_matrix_type, but using the evaluator's Flags */
523template <typename T, typename StorageKind = typename traits<T>::StorageKind>
524struct plain_object_eval;
525
526template <typename T>
527struct plain_object_eval<T, Dense> {
528 using type = typename plain_matrix_type_dense<T, typename traits<T>::XprKind, evaluator<T>::Flags>::type;
529};
530
531/* plain_matrix_type_column_major : same as plain_matrix_type but guaranteed to be column-major
532 */
533template <typename T>
534struct plain_matrix_type_column_major {
535 static constexpr int Rows = traits<T>::RowsAtCompileTime;
536 static constexpr int Cols = traits<T>::ColsAtCompileTime;
537 static constexpr int MaxRows = traits<T>::MaxRowsAtCompileTime;
538 static constexpr int MaxCols = traits<T>::MaxColsAtCompileTime;
539 using type = Matrix<typename traits<T>::Scalar, Rows, Cols, (MaxRows == 1 && MaxCols != 1) ? RowMajor : ColMajor,
540 MaxRows, MaxCols>;
541};
542
543/* plain_matrix_type_row_major : same as plain_matrix_type but guaranteed to be row-major
544 */
545template <typename T>
546struct plain_matrix_type_row_major {
547 static constexpr int Rows = traits<T>::RowsAtCompileTime;
548 static constexpr int Cols = traits<T>::ColsAtCompileTime;
549 static constexpr int MaxRows = traits<T>::MaxRowsAtCompileTime;
550 static constexpr int MaxCols = traits<T>::MaxColsAtCompileTime;
551 using type = Matrix<typename traits<T>::Scalar, Rows, Cols, (MaxCols == 1 && MaxRows != 1) ? ColMajor : RowMajor,
552 MaxRows, MaxCols>;
553};
554
558template <typename T>
559struct ref_selector {
560 using type = std::conditional_t<bool(traits<T>::Flags& NestByRefBit), T const&, const T>;
561
562 using non_const_type = std::conditional_t<bool(traits<T>::Flags& NestByRefBit), T&, T>;
563};
564
565// However, we still need a mechanism to detect whether an expression which is evaluated multiple time
566// has to be evaluated into a temporary.
567// That's the purpose of this new nested_eval helper:
579template <typename T, int n, typename PlainObject = typename plain_object_eval<T>::type>
580struct nested_eval {
581 enum {
582 ScalarReadCost = NumTraits<typename traits<T>::Scalar>::ReadCost,
583 CoeffReadCost =
584 evaluator<T>::CoeffReadCost, // NOTE What if an evaluator evaluate itself into a temporary?
585 // Then CoeffReadCost will be small (e.g., 1) but we still have to evaluate,
586 // especially if n>1. This situation is already taken care by the
587 // EvalBeforeNestingBit flag, which is turned ON for all evaluator creating a
588 // temporary. This flag is then propagated by the parent evaluators. Another
589 // solution could be to count the number of temps?
590 NAsInteger = n == Dynamic ? HugeCost : n,
591 CostEval = (NAsInteger + 1) * ScalarReadCost + CoeffReadCost,
592 CostNoEval = int(NAsInteger) * int(CoeffReadCost),
593 Evaluate = (int(evaluator<T>::Flags) & EvalBeforeNestingBit) || (int(CostEval) < int(CostNoEval))
594 };
595
596 using type = std::conditional_t<Evaluate, PlainObject, typename ref_selector<T>::type>;
597};
598
599template <typename Derived, typename XprKind = typename traits<Derived>::XprKind>
600struct dense_xpr_base {
601 /* dense_xpr_base should only ever be used on dense expressions, thus falling either into the MatrixXpr or into the
602 * ArrayXpr cases */
603};
604
605template <typename Derived>
606struct dense_xpr_base<Derived, MatrixXpr> {
607 using type = MatrixBase<Derived>;
608};
609
610template <typename Derived>
611struct dense_xpr_base<Derived, ArrayXpr> {
612 using type = ArrayBase<Derived>;
613};
614
615template <typename Derived, typename XprKind = typename traits<Derived>::XprKind,
616 typename StorageKind = typename traits<Derived>::StorageKind>
617struct generic_xpr_base;
618
619template <typename Derived, typename XprKind>
620struct generic_xpr_base<Derived, XprKind, Dense> {
621 using type = typename dense_xpr_base<Derived, XprKind>::type;
622};
623
624template <typename XprType, typename CastType>
625struct cast_return_type {
626 using CurrentScalarType = typename XprType::Scalar;
627 using CastType_ = remove_all_t<CastType>;
628 using NewScalarType = typename CastType_::Scalar;
629 using type = std::conditional_t<std::is_same<CurrentScalarType, NewScalarType>::value, const XprType&, CastType>;
630};
631
632template <typename A, typename B>
633struct promote_storage_type;
634
635template <typename A>
636struct promote_storage_type<A, A> {
637 using ret = A;
638};
639template <typename A>
640struct promote_storage_type<A, const A> {
641 using ret = A;
642};
643template <typename A>
644struct promote_storage_type<const A, A> {
645 using ret = A;
646};
647
661template <typename A, typename B, typename Functor>
662struct cwise_promote_storage_type;
663
664template <typename A, typename Functor>
665struct cwise_promote_storage_type<A, A, Functor> {
666 using ret = A;
667};
668template <typename Functor>
669struct cwise_promote_storage_type<Dense, Dense, Functor> {
670 using ret = Dense;
671};
672template <typename A, typename Functor>
673struct cwise_promote_storage_type<A, Dense, Functor> {
674 using ret = Dense;
675};
676template <typename B, typename Functor>
677struct cwise_promote_storage_type<Dense, B, Functor> {
678 using ret = Dense;
679};
680template <typename Functor>
681struct cwise_promote_storage_type<Sparse, Dense, Functor> {
682 using ret = Sparse;
683};
684template <typename Functor>
685struct cwise_promote_storage_type<Dense, Sparse, Functor> {
686 using ret = Sparse;
687};
688
689template <typename LhsKind, typename RhsKind, int LhsOrder, int RhsOrder>
690struct cwise_promote_storage_order : std::integral_constant<int, LhsOrder> {};
691
692template <typename LhsKind, int LhsOrder, int RhsOrder>
693struct cwise_promote_storage_order<LhsKind, Sparse, LhsOrder, RhsOrder> : std::integral_constant<int, RhsOrder> {};
694template <typename RhsKind, int LhsOrder, int RhsOrder>
695struct cwise_promote_storage_order<Sparse, RhsKind, LhsOrder, RhsOrder> : std::integral_constant<int, LhsOrder> {};
696template <int Order>
697struct cwise_promote_storage_order<Sparse, Sparse, Order, Order> : std::integral_constant<int, Order> {};
698
713template <typename A, typename B, int ProductTag>
714struct product_promote_storage_type;
715
716template <typename A, int ProductTag>
717struct product_promote_storage_type<A, A, ProductTag> {
718 using ret = A;
719};
720template <int ProductTag>
721struct product_promote_storage_type<Dense, Dense, ProductTag> {
722 using ret = Dense;
723};
724template <typename A, int ProductTag>
725struct product_promote_storage_type<A, Dense, ProductTag> {
726 using ret = Dense;
727};
728template <typename B, int ProductTag>
729struct product_promote_storage_type<Dense, B, ProductTag> {
730 using ret = Dense;
731};
732
733template <typename A, int ProductTag>
734struct product_promote_storage_type<A, DiagonalShape, ProductTag> {
735 using ret = A;
736};
737template <typename B, int ProductTag>
738struct product_promote_storage_type<DiagonalShape, B, ProductTag> {
739 using ret = B;
740};
741template <int ProductTag>
742struct product_promote_storage_type<Dense, DiagonalShape, ProductTag> {
743 using ret = Dense;
744};
745template <int ProductTag>
746struct product_promote_storage_type<DiagonalShape, Dense, ProductTag> {
747 using ret = Dense;
748};
749
750template <typename A, int ProductTag>
751struct product_promote_storage_type<A, SkewSymmetricShape, ProductTag> {
752 using ret = A;
753};
754template <typename B, int ProductTag>
755struct product_promote_storage_type<SkewSymmetricShape, B, ProductTag> {
756 using ret = B;
757};
758template <int ProductTag>
759struct product_promote_storage_type<Dense, SkewSymmetricShape, ProductTag> {
760 using ret = Dense;
761};
762template <int ProductTag>
763struct product_promote_storage_type<SkewSymmetricShape, Dense, ProductTag> {
764 using ret = Dense;
765};
766template <int ProductTag>
767struct product_promote_storage_type<SkewSymmetricShape, SkewSymmetricShape, ProductTag> {
768 using ret = Dense;
769};
770// A skew-symmetric matrix scaled by a diagonal is neither, and without these the <A, DiagonalShape> and
771// <SkewSymmetricShape, B> rules are ambiguous.
772template <int ProductTag>
773struct product_promote_storage_type<SkewSymmetricShape, DiagonalShape, ProductTag> {
774 using ret = Dense;
775};
776template <int ProductTag>
777struct product_promote_storage_type<DiagonalShape, SkewSymmetricShape, ProductTag> {
778 using ret = Dense;
779};
780
781template <typename A, int ProductTag>
782struct product_promote_storage_type<A, PermutationStorage, ProductTag> {
783 using ret = A;
784};
785template <typename B, int ProductTag>
786struct product_promote_storage_type<PermutationStorage, B, ProductTag> {
787 using ret = B;
788};
789template <int ProductTag>
790struct product_promote_storage_type<Dense, PermutationStorage, ProductTag> {
791 using ret = Dense;
792};
793template <int ProductTag>
794struct product_promote_storage_type<PermutationStorage, Dense, ProductTag> {
795 using ret = Dense;
796};
797
801template <typename ExpressionType, typename Scalar = typename ExpressionType::Scalar>
802struct plain_row_type {
803 using MatrixRowType =
804 Matrix<Scalar, 1, ExpressionType::ColsAtCompileTime, int(ExpressionType::PlainObject::Options) | int(RowMajor), 1,
805 ExpressionType::MaxColsAtCompileTime>;
806 using ArrayRowType =
807 Array<Scalar, 1, ExpressionType::ColsAtCompileTime, int(ExpressionType::PlainObject::Options) | int(RowMajor), 1,
808 ExpressionType::MaxColsAtCompileTime>;
809
810 using type = std::conditional_t<std::is_same<typename traits<ExpressionType>::XprKind, MatrixXpr>::value,
811 MatrixRowType, ArrayRowType>;
812};
813
814template <typename ExpressionType, typename Scalar = typename ExpressionType::Scalar>
815struct plain_col_type {
816 using MatrixColType =
817 Matrix<Scalar, ExpressionType::RowsAtCompileTime, 1, ExpressionType::PlainObject::Options & ~RowMajor,
818 ExpressionType::MaxRowsAtCompileTime, 1>;
819 using ArrayColType = Array<Scalar, ExpressionType::RowsAtCompileTime, 1,
820 ExpressionType::PlainObject::Options & ~RowMajor, ExpressionType::MaxRowsAtCompileTime, 1>;
821
822 using type = std::conditional_t<std::is_same<typename traits<ExpressionType>::XprKind, MatrixXpr>::value,
823 MatrixColType, ArrayColType>;
824};
825
826template <typename ExpressionType, typename Scalar = typename ExpressionType::Scalar>
827struct plain_diag_type {
828 static constexpr int diag_size =
829 internal::min_size_prefer_dynamic(ExpressionType::RowsAtCompileTime, ExpressionType::ColsAtCompileTime);
830 static constexpr int max_diag_size =
831 min_size_prefer_fixed(ExpressionType::MaxRowsAtCompileTime, ExpressionType::MaxColsAtCompileTime);
832 using MatrixDiagType =
833 Matrix<Scalar, diag_size, 1, ExpressionType::PlainObject::Options & ~RowMajor, max_diag_size, 1>;
834 using ArrayDiagType = Array<Scalar, diag_size, 1, ExpressionType::PlainObject::Options & ~RowMajor, max_diag_size, 1>;
835
836 using type = std::conditional_t<std::is_same<typename traits<ExpressionType>::XprKind, MatrixXpr>::value,
837 MatrixDiagType, ArrayDiagType>;
838};
839
840template <typename Expr, typename Scalar = typename Expr::Scalar>
841struct plain_constant_type {
842 static constexpr int Options = (traits<Expr>::Flags & RowMajorBit) ? RowMajor : 0;
843
844 using array_type = Array<Scalar, traits<Expr>::RowsAtCompileTime, traits<Expr>::ColsAtCompileTime, Options,
845 traits<Expr>::MaxRowsAtCompileTime, traits<Expr>::MaxColsAtCompileTime>;
846
847 using matrix_type = Matrix<Scalar, traits<Expr>::RowsAtCompileTime, traits<Expr>::ColsAtCompileTime, Options,
848 traits<Expr>::MaxRowsAtCompileTime, traits<Expr>::MaxColsAtCompileTime>;
849
850 using type = CwiseNullaryOp<scalar_constant_op<Scalar>,
851 const std::conditional_t<std::is_same<typename traits<Expr>::XprKind, MatrixXpr>::value,
852 matrix_type, array_type>>;
853};
854
855template <typename ExpressionType>
856struct is_lvalue : bool_constant<(!bool(std::is_const<ExpressionType>::value)) &&
857 bool((traits<ExpressionType>::Flags & LvalueBit))> {};
858
859template <typename T>
860struct is_diagonal : std::false_type {};
861
862template <typename T>
863struct is_diagonal<DiagonalBase<T>> : std::true_type {};
864
865template <typename T>
866struct is_diagonal<DiagonalWrapper<T>> : std::true_type {};
867
868template <typename T, int S>
869struct is_diagonal<DiagonalMatrix<T, S>> : std::true_type {};
870
871template <typename T>
872struct is_identity : std::false_type {};
873
874template <typename T>
875struct is_identity<CwiseNullaryOp<internal::scalar_identity_op<typename T::Scalar>, T>> : std::true_type {};
876
877template <typename S1, typename S2>
878struct glue_shapes;
879template <>
880struct glue_shapes<DenseShape, TriangularShape> {
881 using type = TriangularShape;
882};
883
884template <typename T1, typename T2>
885struct possibly_same_dense : bool_constant<has_direct_access<T1>::value && has_direct_access<T2>::value &&
886 std::is_same<typename T1::Scalar, typename T2::Scalar>::value> {};
887
888template <typename T1, typename T2, std::enable_if_t<possibly_same_dense<T1, T2>::value, int> = 0>
889EIGEN_DEVICE_FUNC bool is_same_dense(const T1& mat1, const T2& mat2) {
890 return (mat1.data() == mat2.data()) && (mat1.innerStride() == mat2.innerStride()) &&
891 (mat1.outerStride() == mat2.outerStride());
892}
893
894template <typename T1, typename T2, std::enable_if_t<!possibly_same_dense<T1, T2>::value, int> = 0>
895EIGEN_DEVICE_FUNC bool is_same_dense(const T1&, const T2&) {
896 return false;
897}
898
899// Internal helper defining the cost of a scalar division for the type T.
900// The default heuristic can be specialized for each scalar type and architecture.
901template <typename T, bool Vectorized = false, typename EnableIf = void>
902struct scalar_div_cost : std::integral_constant<int, 8 * NumTraits<T>::MulCost> {};
903
904template <typename T, bool Vectorized>
905struct scalar_div_cost<T, Vectorized, std::enable_if_t<NumTraits<T>::IsComplex>>
906 : std::integral_constant<int, 2 * scalar_div_cost<typename NumTraits<T>::Real>::value +
907 6 * NumTraits<typename NumTraits<T>::Real>::MulCost +
908 3 * NumTraits<typename NumTraits<T>::Real>::AddCost> {};
909
910template <bool Vectorized>
911struct scalar_div_cost<signed long, Vectorized, std::conditional_t<sizeof(long) == 8, void, std::false_type>>
912 : std::integral_constant<int, 24> {};
913template <bool Vectorized>
914struct scalar_div_cost<unsigned long, Vectorized, std::conditional_t<sizeof(long) == 8, void, std::false_type>>
915 : std::integral_constant<int, 21> {};
916
917#ifdef EIGEN_DEBUG_ASSIGN
918std::string demangle_traversal(int t) {
919 if (t == DefaultTraversal) return "DefaultTraversal";
920 if (t == LinearTraversal) return "LinearTraversal";
921 if (t == InnerVectorizedTraversal) return "InnerVectorizedTraversal";
922 if (t == LinearVectorizedTraversal) return "LinearVectorizedTraversal";
923 if (t == SliceVectorizedTraversal) return "SliceVectorizedTraversal";
924 return "?";
925}
926std::string demangle_unrolling(int t) {
927 if (t == NoUnrolling) return "NoUnrolling";
928 if (t == InnerUnrolling) return "InnerUnrolling";
929 if (t == CompleteUnrolling) return "CompleteUnrolling";
930 return "?";
931}
932std::string demangle_flags(int f) {
933 std::string res;
934 if (f & RowMajorBit) res += " | RowMajor";
935 if (f & PacketAccessBit) res += " | Packet";
936 if (f & LinearAccessBit) res += " | Linear";
937 if (f & LvalueBit) res += " | Lvalue";
938 if (f & DirectAccessBit) res += " | Direct";
939 if (f & NestByRefBit) res += " | NestByRef";
940 if (f & NoPreferredStorageOrderBit) res += " | NoPreferredStorageOrderBit";
941
942 return res;
943}
944#endif
945
946template <typename XprType>
947struct is_block_xpr : std::false_type {};
948
949template <typename XprType, int BlockRows, int BlockCols, bool InnerPanel>
950struct is_block_xpr<Block<XprType, BlockRows, BlockCols, InnerPanel>> : std::true_type {};
951
952template <typename XprType, int BlockRows, int BlockCols, bool InnerPanel>
953struct is_block_xpr<const Block<XprType, BlockRows, BlockCols, InnerPanel>> : std::true_type {};
954
955// Helper utility for constructing non-recursive block expressions.
956template <typename XprType>
957struct block_xpr_helper {
958 using BaseType = XprType;
959
960 // For regular block expressions, simply forward along the InnerPanel argument,
961 // which is set when calling row/column expressions.
962 static constexpr bool is_inner_panel(bool inner_panel) { return inner_panel; }
963
964 // Only enable non-const base function if XprType is not const (otherwise we get a duplicate definition).
965 template <typename T = XprType, typename EnableIf = std::enable_if_t<!std::is_const<T>::value>>
966 static EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE BaseType& base(XprType& xpr) {
967 return xpr;
968 }
969 static EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE const BaseType& base(const XprType& xpr) { return xpr; }
970 static constexpr EIGEN_ALWAYS_INLINE Index row(const XprType& /*xpr*/, Index r) { return r; }
971 static constexpr EIGEN_ALWAYS_INLINE Index col(const XprType& /*xpr*/, Index c) { return c; }
972};
973
974template <typename XprType, int BlockRows, int BlockCols, bool InnerPanel>
975struct block_xpr_helper<Block<XprType, BlockRows, BlockCols, InnerPanel>> {
976 using BlockXprType = Block<XprType, BlockRows, BlockCols, InnerPanel>;
977 // Recursive helper in case of explicit block-of-block expression.
978 using NestedXprHelper = block_xpr_helper<XprType>;
979 using BaseType = typename NestedXprHelper::BaseType;
980
981 // For block-of-block expressions, we need to combine the InnerPannel trait
982 // with that of the block subexpression.
983 static constexpr bool is_inner_panel(bool inner_panel) {
984 return NestedXprHelper::is_inner_panel(InnerPanel && inner_panel);
985 }
986
987 // Only enable non-const base function if XprType is not const (otherwise we get a duplicates definition).
988 template <typename T = XprType, typename EnableIf = std::enable_if_t<!std::is_const<T>::value>>
989 static EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE BaseType& base(BlockXprType& xpr) {
990 return NestedXprHelper::base(xpr.nestedExpression());
991 }
992 static EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE const BaseType& base(const BlockXprType& xpr) {
993 return NestedXprHelper::base(xpr.nestedExpression());
994 }
995 static constexpr EIGEN_ALWAYS_INLINE Index row(const BlockXprType& xpr, Index r) {
996 return xpr.startRow() + NestedXprHelper::row(xpr.nestedExpression(), r);
997 }
998 static constexpr EIGEN_ALWAYS_INLINE Index col(const BlockXprType& xpr, Index c) {
999 return xpr.startCol() + NestedXprHelper::col(xpr.nestedExpression(), c);
1000 }
1001};
1002
1003template <typename XprType, int BlockRows, int BlockCols, bool InnerPanel>
1004struct block_xpr_helper<const Block<XprType, BlockRows, BlockCols, InnerPanel>>
1005 : block_xpr_helper<Block<XprType, BlockRows, BlockCols, InnerPanel>> {};
1006
1007template <typename XprType>
1008struct is_matrix_base_xpr : std::is_base_of<MatrixBase<remove_all_t<XprType>>, remove_all_t<XprType>> {};
1009
1010template <typename XprType>
1011struct is_permutation_base_xpr : std::is_base_of<PermutationBase<remove_all_t<XprType>>, remove_all_t<XprType>> {};
1012
1013} // end namespace internal
1014
1074template <typename ScalarA, typename ScalarB, typename BinaryOp = internal::scalar_product_op<ScalarA, ScalarB>>
1076#ifndef EIGEN_PARSED_BY_DOXYGEN
1077 // for backward compatibility, use the hints given by the (deprecated) internal::scalar_product_traits class.
1078 : internal::scalar_product_traits<ScalarA, ScalarB>
1079#endif // EIGEN_PARSED_BY_DOXYGEN
1080{
1081};
1082
1083template <typename T, typename BinaryOp>
1084struct ScalarBinaryOpTraits<T, T, BinaryOp> {
1085 using ReturnType = T;
1086};
1087
1088template <typename T, typename BinaryOp>
1089struct ScalarBinaryOpTraits<T, typename NumTraits<std::enable_if_t<NumTraits<T>::IsComplex, T>>::Real, BinaryOp> {
1090 using ReturnType = T;
1091};
1092template <typename T, typename BinaryOp>
1093struct ScalarBinaryOpTraits<typename NumTraits<std::enable_if_t<NumTraits<T>::IsComplex, T>>::Real, T, BinaryOp> {
1094 using ReturnType = T;
1095};
1096
1097// For Matrix * Permutation
1098template <typename T, typename BinaryOp>
1099struct ScalarBinaryOpTraits<T, void, BinaryOp> {
1100 using ReturnType = T;
1101};
1102
1103// For Permutation * Matrix
1104template <typename T, typename BinaryOp>
1105struct ScalarBinaryOpTraits<void, T, BinaryOp> {
1106 using ReturnType = T;
1107};
1108
1109// for Permutation*Permutation
1110template <typename BinaryOp>
1111struct ScalarBinaryOpTraits<void, void, BinaryOp> {
1112 using ReturnType = void;
1113};
1114
1115// We require Lhs and Rhs to have "compatible" scalar types.
1116// It is tempting to always allow mixing different types but remember that this is often impossible in the vectorized
1117// paths. So allowing mixing different types gives very unexpected errors when enabling vectorization, when the user
1118// tries to add together a float matrix and a double matrix.
1119#define EIGEN_CHECK_BINARY_COMPATIBILITY(BINOP, LHS, RHS) \
1120 EIGEN_STATIC_ASSERT( \
1121 (Eigen::internal::has_ReturnType<ScalarBinaryOpTraits<LHS, RHS, BINOP>>::value), \
1122 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
1123
1124namespace internal {
1125
1126// EigenBase alone does not supply the generic scalar operators used here.
1127template <typename T, typename Derived, typename XprKind>
1128struct has_generic_scalar_binary_ops
1129 : bool_constant<std::is_same<XprKind, ArrayXpr>::value
1130 ? std::is_convertible<const remove_all_t<T>*, const ArrayBase<Derived>*>::value
1131 : (std::is_convertible<const remove_all_t<T>*, const MatrixBase<Derived>*>::value ||
1132 std::is_convertible<const remove_all_t<T>*, const SparseMatrixBase<Derived>*>::value)> {};
1133
1134namespace scalar_pow_adl {
1135using std::pow;
1136
1137template <bool Inspect, typename Lhs, typename Rhs, typename = void>
1138struct is_supported : std::false_type {};
1139template <typename Lhs, typename Rhs>
1140struct is_supported<true, Lhs, Rhs,
1141 void_t<typename ScalarBinaryOpTraits<Lhs, Rhs, scalar_pow_op<Lhs, Rhs>>::ReturnType,
1142 decltype(pow(std::declval<const Lhs&>(), std::declval<const Rhs&>()))>>
1143 : std::is_convertible<decltype(pow(std::declval<const Lhs&>(), std::declval<const Rhs&>())),
1144 typename ScalarBinaryOpTraits<Lhs, Rhs, scalar_pow_op<Lhs, Rhs>>::ReturnType> {};
1145} // namespace scalar_pow_adl
1146
1147template <typename Scalar, typename T, template <typename, typename> class BinaryOp, bool ScalarOnLeft>
1148using scalar_expression_binary_traits =
1149 std::conditional_t<ScalarOnLeft, ScalarBinaryOpTraits<T, Scalar, BinaryOp<T, Scalar>>,
1150 ScalarBinaryOpTraits<Scalar, T, BinaryOp<Scalar, T>>>;
1151
1152template <typename Expr, typename T, template <typename, typename> class BinaryOp, bool ScalarOnLeft, typename = void>
1153struct has_competing_expression_binary_op : std::false_type {};
1154
1155template <typename Expr, typename T, template <typename, typename> class BinaryOp, bool ScalarOnLeft>
1156struct has_competing_expression_binary_op<
1157 Expr, T, BinaryOp, ScalarOnLeft,
1158 void_t<decltype(std::declval<const T&>().derived()), typename traits<typename shape_expression<T>::type>::Scalar,
1159 typename traits<typename shape_expression<T>::type>::XprKind,
1160 decltype(traits<typename shape_expression<T>::type>::RowsAtCompileTime),
1161 decltype(traits<typename shape_expression<T>::type>::ColsAtCompileTime),
1162 typename scalar_expression_binary_traits<typename traits<Expr>::Scalar, T, BinaryOp,
1163 ScalarOnLeft>::ReturnType>> {
1164 using Other = typename shape_expression<T>::type;
1165 using Scalar = typename traits<Expr>::Scalar;
1166 using XprKind = typename traits<Expr>::XprKind;
1167 using Lhs = std::conditional_t<ScalarOnLeft, Other, Expr>;
1168 using Rhs = std::conditional_t<ScalarOnLeft, Expr, Other>;
1169 using LhsScalar = typename traits<Lhs>::Scalar;
1170 using RhsScalar = typename traits<Rhs>::Scalar;
1171 using LhsShape = compile_time_shape<Lhs>;
1172 using RhsShape = compile_time_shape<Rhs>;
1173 using Op = BinaryOp<LhsScalar, RhsScalar>;
1174 static constexpr bool IsArray = std::is_same<XprKind, ArrayXpr>::value;
1175 static constexpr bool HasExpressionOverload = has_generic_scalar_binary_ops<Expr, Expr, XprKind>::value &&
1176 has_generic_scalar_binary_ops<T, Other, XprKind>::value;
1177 static constexpr bool ProductSizeMatches =
1178 dimension_relation<LhsShape::Cols, RhsShape::Rows, LhsShape::MaxCols, RhsShape::MaxRows>::Compatible;
1179 static constexpr bool ArraySizeMatches =
1180 dimension_relation<LhsShape::Rows, RhsShape::Rows, LhsShape::MaxRows, RhsShape::MaxRows>::Compatible &&
1181 dimension_relation<LhsShape::Cols, RhsShape::Cols, LhsShape::MaxCols, RhsShape::MaxCols>::Compatible;
1182 using OppositeLhs = std::conditional_t<ScalarOnLeft, typename traits<Other>::Scalar, Expr>;
1183 using OppositeRhs = std::conditional_t<ScalarOnLeft, Expr, typename traits<Other>::Scalar>;
1184 // Preserve a one-sided scalar registration. Only broad traits admitting both
1185 // expression-as-scalar interpretations need to give way to an expression operation.
1186 static constexpr bool HasOppositeScalarOverload =
1187 has_ReturnType<ScalarBinaryOpTraits<OppositeLhs, OppositeRhs, BinaryOp<OppositeLhs, OppositeRhs>>>::value;
1188 // Matrix division has no expression overload; matrix products also require compatible coefficient types.
1189 static constexpr bool Competing = HasExpressionOverload && !is_eigen_expression<Scalar>::value &&
1190 std::is_same<XprKind, typename traits<Other>::XprKind>::value &&
1191 (IsArray ? ArraySizeMatches : ProductSizeMatches) &&
1192 (IsArray || std::is_same<Op, scalar_product_op<LhsScalar, RhsScalar>>::value) &&
1193 has_ReturnType<ScalarBinaryOpTraits<LhsScalar, RhsScalar, Op>>::value;
1194 static constexpr bool IsPow = std::is_same<Op, scalar_pow_op<LhsScalar, RhsScalar>>::value;
1195 using ScalarLhs = std::conditional_t<ScalarOnLeft, T, Scalar>;
1196 using ScalarRhs = std::conditional_t<ScalarOnLeft, Scalar, T>;
1197 using ScalarPower = scalar_pow_adl::is_supported<(Competing && IsPow), ScalarLhs, ScalarRhs>;
1198 static constexpr bool value = Competing && (IsPow ? !ScalarPower::value : HasOppositeScalarOverload);
1199};
1200
1201template <typename T>
1202using expression_binary_base_t = typename shape_expression<T>::type::StorageBaseType;
1203
1204// Preserve base-reference bindings; only concrete operands get a better match
1205// when broad traits admit both expression-as-scalar interpretations.
1206template <typename Lhs, typename Rhs, template <typename, typename> class BinaryOp, typename OwnerBase, typename = void>
1207struct has_ambiguous_expression_binary_op : std::false_type {};
1208
1209template <typename Lhs, typename Rhs, template <typename, typename> class BinaryOp, typename OwnerBase>
1210struct has_ambiguous_expression_binary_op<Lhs, Rhs, BinaryOp, OwnerBase,
1211 void_t<expression_binary_base_t<Lhs>, expression_binary_base_t<Rhs>>>
1212 : bool_constant<
1213 std::is_same<OwnerBase, expression_binary_base_t<Rhs>>::value &&
1214 std::is_convertible<const Rhs*, const OwnerBase*>::value &&
1215 !std::is_same<remove_all_t<Lhs>, expression_binary_base_t<Lhs>>::value &&
1216 !std::is_same<remove_all_t<Rhs>, expression_binary_base_t<Rhs>>::value &&
1217 has_competing_expression_binary_op<typename shape_expression<Lhs>::type, Rhs, BinaryOp, false>::value &&
1218 has_competing_expression_binary_op<typename shape_expression<Rhs>::type, Lhs, BinaryOp, true>::value> {
1219 static constexpr bool DenseSparse =
1220 std::is_same<typename traits<typename shape_expression<Lhs>::type>::StorageKind, Dense>::value &&
1221 std::is_same<typename traits<typename shape_expression<Rhs>::type>::StorageKind, Sparse>::value;
1222};
1223
1224template <template <typename, typename> class BinaryOp, bool DenseSparse>
1225struct expression_binary_op {
1226 template <typename Lhs, typename Rhs>
1227 EIGEN_DEVICE_FUNC static constexpr EIGEN_STRONG_INLINE auto run(const Lhs& lhs, const Rhs& rhs)
1228 -> CwiseBinaryOp<BinaryOp<typename Lhs::Scalar, typename Rhs::Scalar>, const typename shape_expression<Lhs>::type,
1229 const typename shape_expression<Rhs>::type> {
1230 return {lhs.derived(), rhs.derived()};
1231 }
1232};
1233
1234template <>
1235struct expression_binary_op<scalar_product_op, false> {
1236 template <typename Lhs, typename Rhs>
1237 EIGEN_DEVICE_FUNC static constexpr EIGEN_STRONG_INLINE auto run(const Lhs& lhs, const Rhs& rhs)
1238 -> decltype(lhs.operator*(rhs)) {
1239 return lhs.operator*(rhs);
1240 }
1241};
1242
1243template <>
1244struct expression_binary_op<scalar_product_op, true> {
1245 template <typename Lhs, typename Rhs>
1246 EIGEN_DEVICE_FUNC static constexpr EIGEN_STRONG_INLINE auto run(const Lhs& lhs, const Rhs& rhs)
1247 -> decltype(operator*(lhs, rhs)) {
1248 return operator*(lhs, rhs);
1249 }
1250};
1251
1252} // namespace internal
1253
1254} // end namespace Eigen
1255
1256#endif // EIGEN_XPRHELPER_H
@ ColMajor
Definition Constants.h:319
@ RowMajor
Definition Constants.h:321
@ AutoAlign
Definition Constants.h:323
constexpr unsigned int NoPreferredStorageOrderBit
Definition Constants.h:183
constexpr unsigned int PacketAccessBit
Definition Constants.h:98
constexpr unsigned int DirectAccessBit
Definition Constants.h:160
constexpr unsigned int EvalBeforeNestingBit
Definition Constants.h:75
constexpr unsigned int LinearAccessBit
Definition Constants.h:134
constexpr unsigned int LvalueBit
Definition Constants.h:149
constexpr unsigned int RowMajorBit
Definition Constants.h:71
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition NumTraits.h:267
Determines whether the given binary operation of two numeric types is allowed and what the scalar ret...
Definition XprHelper.h:1080