Eigen  3.4.90 (git rev 5a9f66fb35d03a4da9ef8976e67a61b30aa16dcf)
 
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 <[email protected]>
5// Copyright (C) 2006-2008 Benoit Jacob <[email protected]>
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
11#ifndef EIGEN_XPRHELPER_H
12#define EIGEN_XPRHELPER_H
13
14// IWYU pragma: private
15#include "../InternalHeaderCheck.h"
16
17namespace Eigen {
18
19namespace internal {
20
21// useful for unsigned / signed integer comparisons when idx is intended to be non-negative
22template <typename IndexType>
23EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE typename make_unsigned<IndexType>::type returnUnsignedIndexValue(
24 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 = typename make_unsigned<IndexType>::type;
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 T can be considered as an integral index (i.e., and integral type or enum)
68template <typename T>
69struct is_valid_index_type {
70 enum { value = internal::is_integral<T>::value || std::is_enum<T>::value };
71};
72
73// true if both types are not valid index types
74template <typename RowIndices, typename ColIndices>
75struct valid_indexed_view_overload {
76 enum {
77 value = !(internal::is_valid_index_type<RowIndices>::value && internal::is_valid_index_type<ColIndices>::value)
78 };
79};
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 typedef T type;
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 = internal::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 typedef PromotedType type;
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 private:
136 no_assignment_operator& operator=(const no_assignment_operator&);
137
138 protected:
139 EIGEN_DEFAULT_COPY_CONSTRUCTOR(no_assignment_operator)
140 EIGEN_DEFAULT_EMPTY_CONSTRUCTOR_AND_DESTRUCTOR(no_assignment_operator)
141};
142
144template <typename I1, typename I2>
145struct promote_index_type {
146 typedef std::conditional_t<(sizeof(I1) < sizeof(I2)), I2, I1> type;
147};
148
153template <typename T, int Value>
154class variable_if_dynamic {
155 public:
156 EIGEN_DEFAULT_EMPTY_CONSTRUCTOR_AND_DESTRUCTOR(variable_if_dynamic)
157 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE explicit variable_if_dynamic(T v) {
158 EIGEN_ONLY_USED_FOR_DEBUG(v);
159 eigen_assert(v == T(Value));
160 }
161 EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE EIGEN_CONSTEXPR T value() { return T(Value); }
162 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR operator T() const { return T(Value); }
163 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void setValue(T v) const {
164 EIGEN_ONLY_USED_FOR_DEBUG(v);
165 eigen_assert(v == T(Value));
166 }
167};
168
169template <typename T>
170class variable_if_dynamic<T, Dynamic> {
171 T m_value;
172
173 public:
174 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE explicit variable_if_dynamic(T value = 0) EIGEN_NO_THROW : m_value(value) {}
175 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T value() const { return m_value; }
176 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE operator T() const { return m_value; }
177 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void setValue(T value) { m_value = value; }
178};
179
182template <typename T, int Value>
183class variable_if_dynamicindex {
184 public:
185 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE explicit variable_if_dynamicindex(T v) {
186 EIGEN_ONLY_USED_FOR_DEBUG(v);
187 eigen_assert(v == T(Value));
188 }
189 EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE EIGEN_CONSTEXPR T value() { return T(Value); }
190 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void setValue(T) {}
191};
192
193template <typename T>
194class variable_if_dynamicindex<T, DynamicIndex> {
195 T m_value;
196 EIGEN_DEVICE_FUNC variable_if_dynamicindex() { eigen_assert(false); }
197
198 public:
199 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE explicit variable_if_dynamicindex(T value) : m_value(value) {}
200 EIGEN_DEVICE_FUNC T EIGEN_STRONG_INLINE value() const { return m_value; }
201 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void setValue(T value) { m_value = value; }
202};
203
204template <typename T>
205struct functor_traits {
206 enum { Cost = 10, PacketAccess = false, IsRepeatable = false };
207};
208
209// estimates the cost of lazily evaluating a generic functor by unwinding the expression
210template <typename Xpr>
211struct nested_functor_cost {
212 static constexpr Index Cost = static_cast<Index>(functor_traits<Xpr>::Cost);
213};
214
215template <typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
216struct nested_functor_cost<Matrix<Scalar, Rows, Cols, Options, MaxRows, MaxCols>> {
217 static constexpr Index Cost = 1;
218};
219
220template <typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
221struct nested_functor_cost<Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols>> {
222 static constexpr Index Cost = 1;
223};
224
225// TODO: assign a cost to the stride type?
226template <typename PlainObjectType, int MapOptions, typename StrideType>
227struct nested_functor_cost<Map<PlainObjectType, MapOptions, StrideType>> : nested_functor_cost<PlainObjectType> {};
228
229template <typename Func, typename Xpr>
230struct nested_functor_cost<CwiseUnaryOp<Func, Xpr>> {
231 using XprCleaned = remove_all_t<Xpr>;
232 using FuncCleaned = remove_all_t<Func>;
233 static constexpr Index Cost = nested_functor_cost<FuncCleaned>::Cost + nested_functor_cost<XprCleaned>::Cost;
234};
235
236template <typename Func, typename Xpr>
237struct nested_functor_cost<CwiseNullaryOp<Func, Xpr>> {
238 using XprCleaned = remove_all_t<Xpr>;
239 using FuncCleaned = remove_all_t<Func>;
240 static constexpr Index Cost = nested_functor_cost<FuncCleaned>::Cost + nested_functor_cost<XprCleaned>::Cost;
241};
242
243template <typename Func, typename LhsXpr, typename RhsXpr>
244struct nested_functor_cost<CwiseBinaryOp<Func, LhsXpr, RhsXpr>> {
245 using LhsXprCleaned = remove_all_t<LhsXpr>;
246 using RhsXprCleaned = remove_all_t<RhsXpr>;
247 using FuncCleaned = remove_all_t<Func>;
248 static constexpr Index Cost = nested_functor_cost<FuncCleaned>::Cost + nested_functor_cost<LhsXprCleaned>::Cost +
249 nested_functor_cost<RhsXprCleaned>::Cost;
250};
251
252template <typename Func, typename LhsXpr, typename MidXpr, typename RhsXpr>
253struct nested_functor_cost<CwiseTernaryOp<Func, LhsXpr, MidXpr, RhsXpr>> {
254 using LhsXprCleaned = remove_all_t<LhsXpr>;
255 using MidXprCleaned = remove_all_t<MidXpr>;
256 using RhsXprCleaned = remove_all_t<RhsXpr>;
257 using FuncCleaned = remove_all_t<Func>;
258 static constexpr Index Cost = nested_functor_cost<FuncCleaned>::Cost + nested_functor_cost<LhsXprCleaned>::Cost +
259 nested_functor_cost<MidXprCleaned>::Cost + nested_functor_cost<RhsXprCleaned>::Cost;
260};
261
262template <typename Xpr>
263struct functor_cost {
264 static constexpr Index Cost = plain_enum_max(nested_functor_cost<Xpr>::Cost, 1);
265};
266
267template <typename T>
268struct packet_traits;
269
270template <typename T>
271struct unpacket_traits;
272
273template <int Size, typename PacketType,
274 bool Stop = Size == Dynamic || (Size % unpacket_traits<PacketType>::size) == 0 ||
275 is_same<PacketType, typename unpacket_traits<PacketType>::half>::value>
276struct find_best_packet_helper;
277
278template <int Size, typename PacketType>
279struct find_best_packet_helper<Size, PacketType, true> {
280 typedef PacketType type;
281};
282
283template <int Size, typename PacketType>
284struct find_best_packet_helper<Size, PacketType, false> {
285 typedef typename find_best_packet_helper<Size, typename unpacket_traits<PacketType>::half>::type type;
286};
287
288template <typename T, int Size>
289struct find_best_packet {
290 typedef typename find_best_packet_helper<Size, typename packet_traits<T>::type>::type type;
291};
292
293template <int Size, typename PacketType,
294 bool Stop = (Size == unpacket_traits<PacketType>::size) ||
295 is_same<PacketType, typename unpacket_traits<PacketType>::half>::value>
296struct find_packet_by_size_helper;
297template <int Size, typename PacketType>
298struct find_packet_by_size_helper<Size, PacketType, true> {
299 using type = PacketType;
300};
301template <int Size, typename PacketType>
302struct find_packet_by_size_helper<Size, PacketType, false> {
303 using type = typename find_packet_by_size_helper<Size, typename unpacket_traits<PacketType>::half>::type;
304};
305
306template <typename T, int Size>
307struct find_packet_by_size {
308 using type = typename find_packet_by_size_helper<Size, typename packet_traits<T>::type>::type;
309 static constexpr bool value = (Size == unpacket_traits<type>::size);
310};
311template <typename T>
312struct find_packet_by_size<T, 1> {
313 using type = typename unpacket_traits<T>::type;
314 static constexpr bool value = (unpacket_traits<type>::size == 1);
315};
316
317#if EIGEN_MAX_STATIC_ALIGN_BYTES > 0
318constexpr inline int compute_default_alignment_helper(int ArrayBytes, int AlignmentBytes) {
319 if ((ArrayBytes % AlignmentBytes) == 0) {
320 return AlignmentBytes;
321 } else if (EIGEN_MIN_ALIGN_BYTES < AlignmentBytes) {
322 return compute_default_alignment_helper(ArrayBytes, AlignmentBytes / 2);
323 } else {
324 return 0;
325 }
326}
327#else
328// If static alignment is disabled, no need to bother.
329// This also avoids a division by zero
330constexpr inline int compute_default_alignment_helper(int ArrayBytes, int AlignmentBytes) {
331 EIGEN_UNUSED_VARIABLE(ArrayBytes);
332 EIGEN_UNUSED_VARIABLE(AlignmentBytes);
333 return 0;
334}
335#endif
336
337template <typename T, int Size>
338struct compute_default_alignment {
339 enum { value = compute_default_alignment_helper(Size * sizeof(T), EIGEN_MAX_STATIC_ALIGN_BYTES) };
340};
341
342template <typename T>
343struct compute_default_alignment<T, Dynamic> {
344 enum { value = EIGEN_MAX_ALIGN_BYTES };
345};
346
347template <typename Scalar_, int Rows_, int Cols_,
348 int Options_ = AutoAlign | ((Rows_ == 1 && Cols_ != 1) ? RowMajor
349 : (Cols_ == 1 && Rows_ != 1) ? ColMajor
350 : EIGEN_DEFAULT_MATRIX_STORAGE_ORDER_OPTION),
351 int MaxRows_ = Rows_, int MaxCols_ = Cols_>
352class make_proper_matrix_type {
353 enum {
354 IsColVector = Cols_ == 1 && Rows_ != 1,
355 IsRowVector = Rows_ == 1 && Cols_ != 1,
356 Options = IsColVector ? (Options_ | ColMajor) & ~RowMajor
357 : IsRowVector ? (Options_ | RowMajor) & ~ColMajor
358 : Options_
359 };
360
361 public:
362 typedef Matrix<Scalar_, Rows_, Cols_, Options, MaxRows_, MaxCols_> type;
363};
364
365constexpr inline unsigned compute_matrix_flags(int Options) {
366 unsigned row_major_bit = Options & RowMajor ? RowMajorBit : 0;
367 // FIXME currently we still have to handle DirectAccessBit at the expression level to handle DenseCoeffsBase<>
368 // and then propagate this information to the evaluator's flags.
369 // However, I (Gael) think that DirectAccessBit should only matter at the evaluation stage.
370 return DirectAccessBit | LvalueBit | NestByRefBit | row_major_bit;
371}
372
373constexpr inline int size_at_compile_time(int rows, int cols) {
374 if (rows == 0 || cols == 0) return 0;
375 if (rows == Dynamic || cols == Dynamic) return Dynamic;
376 return rows * cols;
377}
378
379template <typename XprType>
380struct size_of_xpr_at_compile_time {
381 enum { ret = size_at_compile_time(traits<XprType>::RowsAtCompileTime, traits<XprType>::ColsAtCompileTime) };
382};
383
384/* plain_matrix_type : the difference from eval is that plain_matrix_type is always a plain matrix type,
385 * whereas eval is a const reference in the case of a matrix
386 */
387
388template <typename T, typename StorageKind = typename traits<T>::StorageKind>
389struct plain_matrix_type;
390template <typename T, typename BaseClassType, int Flags>
391struct plain_matrix_type_dense;
392template <typename T>
393struct plain_matrix_type<T, Dense> {
394 typedef typename plain_matrix_type_dense<T, typename traits<T>::XprKind, traits<T>::Flags>::type type;
395};
396template <typename T>
397struct plain_matrix_type<T, DiagonalShape> {
398 typedef typename T::PlainObject type;
399};
400
401template <typename T>
402struct plain_matrix_type<T, SkewSymmetricShape> {
403 typedef typename T::PlainObject type;
404};
405
406template <typename T, int Flags>
407struct plain_matrix_type_dense<T, MatrixXpr, Flags> {
408 typedef Matrix<typename traits<T>::Scalar, traits<T>::RowsAtCompileTime, traits<T>::ColsAtCompileTime,
409 AutoAlign | (Flags & RowMajorBit ? RowMajor : ColMajor), traits<T>::MaxRowsAtCompileTime,
410 traits<T>::MaxColsAtCompileTime>
411 type;
412};
413
414template <typename T, int Flags>
415struct plain_matrix_type_dense<T, ArrayXpr, Flags> {
416 typedef Array<typename traits<T>::Scalar, traits<T>::RowsAtCompileTime, traits<T>::ColsAtCompileTime,
417 AutoAlign | (Flags & RowMajorBit ? RowMajor : ColMajor), traits<T>::MaxRowsAtCompileTime,
418 traits<T>::MaxColsAtCompileTime>
419 type;
420};
421
422/* eval : the return type of eval(). For matrices, this is just a const reference
423 * in order to avoid a useless copy
424 */
425
426template <typename T, typename StorageKind = typename traits<T>::StorageKind>
427struct eval;
428
429template <typename T>
430struct eval<T, Dense> {
431 typedef typename plain_matrix_type<T>::type type;
432 // typedef typename T::PlainObject type;
433 // typedef T::Matrix<typename traits<T>::Scalar,
434 // traits<T>::RowsAtCompileTime,
435 // traits<T>::ColsAtCompileTime,
436 // AutoAlign | (traits<T>::Flags&RowMajorBit ? RowMajor : ColMajor),
437 // traits<T>::MaxRowsAtCompileTime,
438 // traits<T>::MaxColsAtCompileTime
439 // > type;
440};
441
442template <typename T>
443struct eval<T, DiagonalShape> {
444 typedef typename plain_matrix_type<T>::type type;
445};
446
447template <typename T>
448struct eval<T, SkewSymmetricShape> {
449 typedef typename plain_matrix_type<T>::type type;
450};
451
452// for matrices, no need to evaluate, just use a const reference to avoid a useless copy
453template <typename Scalar_, int Rows_, int Cols_, int Options_, int MaxRows_, int MaxCols_>
454struct eval<Matrix<Scalar_, Rows_, Cols_, Options_, MaxRows_, MaxCols_>, Dense> {
455 typedef const Matrix<Scalar_, Rows_, Cols_, Options_, MaxRows_, MaxCols_>& type;
456};
457
458template <typename Scalar_, int Rows_, int Cols_, int Options_, int MaxRows_, int MaxCols_>
459struct eval<Array<Scalar_, Rows_, Cols_, Options_, MaxRows_, MaxCols_>, Dense> {
460 typedef const Array<Scalar_, Rows_, Cols_, Options_, MaxRows_, MaxCols_>& type;
461};
462
463/* similar to plain_matrix_type, but using the evaluator's Flags */
464template <typename T, typename StorageKind = typename traits<T>::StorageKind>
465struct plain_object_eval;
466
467template <typename T>
468struct plain_object_eval<T, Dense> {
469 typedef typename plain_matrix_type_dense<T, typename traits<T>::XprKind, evaluator<T>::Flags>::type type;
470};
471
472/* plain_matrix_type_column_major : same as plain_matrix_type but guaranteed to be column-major
473 */
474template <typename T>
475struct plain_matrix_type_column_major {
476 enum {
477 Rows = traits<T>::RowsAtCompileTime,
478 Cols = traits<T>::ColsAtCompileTime,
479 MaxRows = traits<T>::MaxRowsAtCompileTime,
480 MaxCols = traits<T>::MaxColsAtCompileTime
481 };
482 typedef Matrix<typename traits<T>::Scalar, Rows, Cols, (MaxRows == 1 && MaxCols != 1) ? RowMajor : ColMajor, MaxRows,
483 MaxCols>
484 type;
485};
486
487/* plain_matrix_type_row_major : same as plain_matrix_type but guaranteed to be row-major
488 */
489template <typename T>
490struct plain_matrix_type_row_major {
491 enum {
492 Rows = traits<T>::RowsAtCompileTime,
493 Cols = traits<T>::ColsAtCompileTime,
494 MaxRows = traits<T>::MaxRowsAtCompileTime,
495 MaxCols = traits<T>::MaxColsAtCompileTime
496 };
497 typedef Matrix<typename traits<T>::Scalar, Rows, Cols, (MaxCols == 1 && MaxRows != 1) ? ColMajor : RowMajor, MaxRows,
498 MaxCols>
499 type;
500};
501
505template <typename T>
506struct ref_selector {
507 typedef std::conditional_t<bool(traits<T>::Flags& NestByRefBit), T const&, const T> type;
508
509 typedef std::conditional_t<bool(traits<T>::Flags& NestByRefBit), T&, T> non_const_type;
510};
511
513template <typename T1, typename T2>
514struct transfer_constness {
515 typedef std::conditional_t<bool(internal::is_const<T1>::value), add_const_on_value_type_t<T2>, T2> type;
516};
517
518// However, we still need a mechanism to detect whether an expression which is evaluated multiple time
519// has to be evaluated into a temporary.
520// That's the purpose of this new nested_eval helper:
532template <typename T, int n, typename PlainObject = typename plain_object_eval<T>::type>
533struct nested_eval {
534 enum {
535 ScalarReadCost = NumTraits<typename traits<T>::Scalar>::ReadCost,
536 CoeffReadCost =
537 evaluator<T>::CoeffReadCost, // NOTE What if an evaluator evaluate itself into a temporary?
538 // Then CoeffReadCost will be small (e.g., 1) but we still have to evaluate,
539 // especially if n>1. This situation is already taken care by the
540 // EvalBeforeNestingBit flag, which is turned ON for all evaluator creating a
541 // temporary. This flag is then propagated by the parent evaluators. Another
542 // solution could be to count the number of temps?
543 NAsInteger = n == Dynamic ? HugeCost : n,
544 CostEval = (NAsInteger + 1) * ScalarReadCost + CoeffReadCost,
545 CostNoEval = int(NAsInteger) * int(CoeffReadCost),
546 Evaluate = (int(evaluator<T>::Flags) & EvalBeforeNestingBit) || (int(CostEval) < int(CostNoEval))
547 };
548
549 typedef std::conditional_t<Evaluate, PlainObject, typename ref_selector<T>::type> type;
550};
551
552template <typename T>
553EIGEN_DEVICE_FUNC inline T* const_cast_ptr(const T* ptr) {
554 return const_cast<T*>(ptr);
555}
556
557template <typename Derived, typename XprKind = typename traits<Derived>::XprKind>
558struct dense_xpr_base {
559 /* dense_xpr_base should only ever be used on dense expressions, thus falling either into the MatrixXpr or into the
560 * ArrayXpr cases */
561};
562
563template <typename Derived>
564struct dense_xpr_base<Derived, MatrixXpr> {
565 typedef MatrixBase<Derived> type;
566};
567
568template <typename Derived>
569struct dense_xpr_base<Derived, ArrayXpr> {
570 typedef ArrayBase<Derived> type;
571};
572
573template <typename Derived, typename XprKind = typename traits<Derived>::XprKind,
574 typename StorageKind = typename traits<Derived>::StorageKind>
575struct generic_xpr_base;
576
577template <typename Derived, typename XprKind>
578struct generic_xpr_base<Derived, XprKind, Dense> {
579 typedef typename dense_xpr_base<Derived, XprKind>::type type;
580};
581
582template <typename XprType, typename CastType>
583struct cast_return_type {
584 typedef typename XprType::Scalar CurrentScalarType;
585 typedef remove_all_t<CastType> CastType_;
586 typedef typename CastType_::Scalar NewScalarType;
587 typedef std::conditional_t<is_same<CurrentScalarType, NewScalarType>::value, const XprType&, CastType> type;
588};
589
590template <typename A, typename B>
591struct promote_storage_type;
592
593template <typename A>
594struct promote_storage_type<A, A> {
595 typedef A ret;
596};
597template <typename A>
598struct promote_storage_type<A, const A> {
599 typedef A ret;
600};
601template <typename A>
602struct promote_storage_type<const A, A> {
603 typedef A ret;
604};
605
619template <typename A, typename B, typename Functor>
620struct cwise_promote_storage_type;
621
622template <typename A, typename Functor>
623struct cwise_promote_storage_type<A, A, Functor> {
624 typedef A ret;
625};
626template <typename Functor>
627struct cwise_promote_storage_type<Dense, Dense, Functor> {
628 typedef Dense ret;
629};
630template <typename A, typename Functor>
631struct cwise_promote_storage_type<A, Dense, Functor> {
632 typedef Dense ret;
633};
634template <typename B, typename Functor>
635struct cwise_promote_storage_type<Dense, B, Functor> {
636 typedef Dense ret;
637};
638template <typename Functor>
639struct cwise_promote_storage_type<Sparse, Dense, Functor> {
640 typedef Sparse ret;
641};
642template <typename Functor>
643struct cwise_promote_storage_type<Dense, Sparse, Functor> {
644 typedef Sparse ret;
645};
646
647template <typename LhsKind, typename RhsKind, int LhsOrder, int RhsOrder>
648struct cwise_promote_storage_order {
649 enum { value = LhsOrder };
650};
651
652template <typename LhsKind, int LhsOrder, int RhsOrder>
653struct cwise_promote_storage_order<LhsKind, Sparse, LhsOrder, RhsOrder> {
654 enum { value = RhsOrder };
655};
656template <typename RhsKind, int LhsOrder, int RhsOrder>
657struct cwise_promote_storage_order<Sparse, RhsKind, LhsOrder, RhsOrder> {
658 enum { value = LhsOrder };
659};
660template <int Order>
661struct cwise_promote_storage_order<Sparse, Sparse, Order, Order> {
662 enum { value = Order };
663};
664
679template <typename A, typename B, int ProductTag>
680struct product_promote_storage_type;
681
682template <typename A, int ProductTag>
683struct product_promote_storage_type<A, A, ProductTag> {
684 typedef A ret;
685};
686template <int ProductTag>
687struct product_promote_storage_type<Dense, Dense, ProductTag> {
688 typedef Dense ret;
689};
690template <typename A, int ProductTag>
691struct product_promote_storage_type<A, Dense, ProductTag> {
692 typedef Dense ret;
693};
694template <typename B, int ProductTag>
695struct product_promote_storage_type<Dense, B, ProductTag> {
696 typedef Dense ret;
697};
698
699template <typename A, int ProductTag>
700struct product_promote_storage_type<A, DiagonalShape, ProductTag> {
701 typedef A ret;
702};
703template <typename B, int ProductTag>
704struct product_promote_storage_type<DiagonalShape, B, ProductTag> {
705 typedef B ret;
706};
707template <int ProductTag>
708struct product_promote_storage_type<Dense, DiagonalShape, ProductTag> {
709 typedef Dense ret;
710};
711template <int ProductTag>
712struct product_promote_storage_type<DiagonalShape, Dense, ProductTag> {
713 typedef Dense ret;
714};
715
716template <typename A, int ProductTag>
717struct product_promote_storage_type<A, SkewSymmetricShape, ProductTag> {
718 typedef A ret;
719};
720template <typename B, int ProductTag>
721struct product_promote_storage_type<SkewSymmetricShape, B, ProductTag> {
722 typedef B ret;
723};
724template <int ProductTag>
725struct product_promote_storage_type<Dense, SkewSymmetricShape, ProductTag> {
726 typedef Dense ret;
727};
728template <int ProductTag>
729struct product_promote_storage_type<SkewSymmetricShape, Dense, ProductTag> {
730 typedef Dense ret;
731};
732template <int ProductTag>
733struct product_promote_storage_type<SkewSymmetricShape, SkewSymmetricShape, ProductTag> {
734 typedef Dense ret;
735};
736
737template <typename A, int ProductTag>
738struct product_promote_storage_type<A, PermutationStorage, ProductTag> {
739 typedef A ret;
740};
741template <typename B, int ProductTag>
742struct product_promote_storage_type<PermutationStorage, B, ProductTag> {
743 typedef B ret;
744};
745template <int ProductTag>
746struct product_promote_storage_type<Dense, PermutationStorage, ProductTag> {
747 typedef Dense ret;
748};
749template <int ProductTag>
750struct product_promote_storage_type<PermutationStorage, Dense, ProductTag> {
751 typedef Dense ret;
752};
753
757template <typename ExpressionType, typename Scalar = typename ExpressionType::Scalar>
758struct plain_row_type {
759 typedef Matrix<Scalar, 1, ExpressionType::ColsAtCompileTime,
760 int(ExpressionType::PlainObject::Options) | int(RowMajor), 1, ExpressionType::MaxColsAtCompileTime>
761 MatrixRowType;
762 typedef Array<Scalar, 1, ExpressionType::ColsAtCompileTime, int(ExpressionType::PlainObject::Options) | int(RowMajor),
763 1, ExpressionType::MaxColsAtCompileTime>
764 ArrayRowType;
765
766 typedef std::conditional_t<is_same<typename traits<ExpressionType>::XprKind, MatrixXpr>::value, MatrixRowType,
767 ArrayRowType>
768 type;
769};
770
771template <typename ExpressionType, typename Scalar = typename ExpressionType::Scalar>
772struct plain_col_type {
773 typedef Matrix<Scalar, ExpressionType::RowsAtCompileTime, 1, ExpressionType::PlainObject::Options & ~RowMajor,
774 ExpressionType::MaxRowsAtCompileTime, 1>
775 MatrixColType;
776 typedef Array<Scalar, ExpressionType::RowsAtCompileTime, 1, ExpressionType::PlainObject::Options & ~RowMajor,
777 ExpressionType::MaxRowsAtCompileTime, 1>
778 ArrayColType;
779
780 typedef std::conditional_t<is_same<typename traits<ExpressionType>::XprKind, MatrixXpr>::value, MatrixColType,
781 ArrayColType>
782 type;
783};
784
785template <typename ExpressionType, typename Scalar = typename ExpressionType::Scalar>
786struct plain_diag_type {
787 enum {
788 diag_size = internal::min_size_prefer_dynamic(ExpressionType::RowsAtCompileTime, ExpressionType::ColsAtCompileTime),
789 max_diag_size = min_size_prefer_fixed(ExpressionType::MaxRowsAtCompileTime, ExpressionType::MaxColsAtCompileTime)
790 };
791 typedef Matrix<Scalar, diag_size, 1, ExpressionType::PlainObject::Options & ~RowMajor, max_diag_size, 1>
792 MatrixDiagType;
793 typedef Array<Scalar, diag_size, 1, ExpressionType::PlainObject::Options & ~RowMajor, max_diag_size, 1> ArrayDiagType;
794
795 typedef std::conditional_t<is_same<typename traits<ExpressionType>::XprKind, MatrixXpr>::value, MatrixDiagType,
796 ArrayDiagType>
797 type;
798};
799
800template <typename Expr, typename Scalar = typename Expr::Scalar>
801struct plain_constant_type {
802 enum { Options = (traits<Expr>::Flags & RowMajorBit) ? RowMajor : 0 };
803
804 typedef Array<Scalar, traits<Expr>::RowsAtCompileTime, traits<Expr>::ColsAtCompileTime, Options,
805 traits<Expr>::MaxRowsAtCompileTime, traits<Expr>::MaxColsAtCompileTime>
806 array_type;
807
808 typedef Matrix<Scalar, traits<Expr>::RowsAtCompileTime, traits<Expr>::ColsAtCompileTime, Options,
809 traits<Expr>::MaxRowsAtCompileTime, traits<Expr>::MaxColsAtCompileTime>
810 matrix_type;
811
812 typedef CwiseNullaryOp<
813 scalar_constant_op<Scalar>,
814 const std::conditional_t<is_same<typename traits<Expr>::XprKind, MatrixXpr>::value, matrix_type, array_type>>
815 type;
816};
817
818template <typename ExpressionType>
819struct is_lvalue {
820 enum { value = (!bool(is_const<ExpressionType>::value)) && bool(traits<ExpressionType>::Flags & LvalueBit) };
821};
822
823template <typename T>
824struct is_diagonal {
825 enum { ret = false };
826};
827
828template <typename T>
829struct is_diagonal<DiagonalBase<T>> {
830 enum { ret = true };
831};
832
833template <typename T>
834struct is_diagonal<DiagonalWrapper<T>> {
835 enum { ret = true };
836};
837
838template <typename T, int S>
839struct is_diagonal<DiagonalMatrix<T, S>> {
840 enum { ret = true };
841};
842
843template <typename T>
844struct is_identity {
845 enum { value = false };
846};
847
848template <typename T>
849struct is_identity<CwiseNullaryOp<internal::scalar_identity_op<typename T::Scalar>, T>> {
850 enum { value = true };
851};
852
853template <typename S1, typename S2>
854struct glue_shapes;
855template <>
856struct glue_shapes<DenseShape, TriangularShape> {
857 typedef TriangularShape type;
858};
859
860template <typename T1, typename T2>
861struct possibly_same_dense {
862 enum {
863 value = has_direct_access<T1>::ret && has_direct_access<T2>::ret &&
864 is_same<typename T1::Scalar, typename T2::Scalar>::value
865 };
866};
867
868template <typename T1, typename T2>
869EIGEN_DEVICE_FUNC bool is_same_dense(const T1& mat1, const T2& mat2,
870 std::enable_if_t<possibly_same_dense<T1, T2>::value>* = 0) {
871 return (mat1.data() == mat2.data()) && (mat1.innerStride() == mat2.innerStride()) &&
872 (mat1.outerStride() == mat2.outerStride());
873}
874
875template <typename T1, typename T2>
876EIGEN_DEVICE_FUNC bool is_same_dense(const T1&, const T2&, std::enable_if_t<!possibly_same_dense<T1, T2>::value>* = 0) {
877 return false;
878}
879
880// Internal helper defining the cost of a scalar division for the type T.
881// The default heuristic can be specialized for each scalar type and architecture.
882template <typename T, bool Vectorized = false, typename EnableIf = void>
883struct scalar_div_cost {
884 enum { value = 8 * NumTraits<T>::MulCost };
885};
886
887template <typename T, bool Vectorized>
888struct scalar_div_cost<std::complex<T>, Vectorized> {
889 enum { value = 2 * scalar_div_cost<T>::value + 6 * NumTraits<T>::MulCost + 3 * NumTraits<T>::AddCost };
890};
891
892template <bool Vectorized>
893struct scalar_div_cost<signed long, Vectorized, std::conditional_t<sizeof(long) == 8, void, false_type>> {
894 enum { value = 24 };
895};
896template <bool Vectorized>
897struct scalar_div_cost<unsigned long, Vectorized, std::conditional_t<sizeof(long) == 8, void, false_type>> {
898 enum { value = 21 };
899};
900
901#ifdef EIGEN_DEBUG_ASSIGN
902std::string demangle_traversal(int t) {
903 if (t == DefaultTraversal) return "DefaultTraversal";
904 if (t == LinearTraversal) return "LinearTraversal";
905 if (t == InnerVectorizedTraversal) return "InnerVectorizedTraversal";
906 if (t == LinearVectorizedTraversal) return "LinearVectorizedTraversal";
907 if (t == SliceVectorizedTraversal) return "SliceVectorizedTraversal";
908 return "?";
909}
910std::string demangle_unrolling(int t) {
911 if (t == NoUnrolling) return "NoUnrolling";
912 if (t == InnerUnrolling) return "InnerUnrolling";
913 if (t == CompleteUnrolling) return "CompleteUnrolling";
914 return "?";
915}
916std::string demangle_flags(int f) {
917 std::string res;
918 if (f & RowMajorBit) res += " | RowMajor";
919 if (f & PacketAccessBit) res += " | Packet";
920 if (f & LinearAccessBit) res += " | Linear";
921 if (f & LvalueBit) res += " | Lvalue";
922 if (f & DirectAccessBit) res += " | Direct";
923 if (f & NestByRefBit) res += " | NestByRef";
924 if (f & NoPreferredStorageOrderBit) res += " | NoPreferredStorageOrderBit";
925
926 return res;
927}
928#endif
929
930template <typename XprType>
931struct is_block_xpr : std::false_type {};
932
933template <typename XprType, int BlockRows, int BlockCols, bool InnerPanel>
934struct is_block_xpr<Block<XprType, BlockRows, BlockCols, InnerPanel>> : std::true_type {};
935
936template <typename XprType, int BlockRows, int BlockCols, bool InnerPanel>
937struct is_block_xpr<const Block<XprType, BlockRows, BlockCols, InnerPanel>> : std::true_type {};
938
939// Helper utility for constructing non-recursive block expressions.
940template <typename XprType>
941struct block_xpr_helper {
942 using BaseType = XprType;
943
944 // For regular block expressions, simply forward along the InnerPanel argument,
945 // which is set when calling row/column expressions.
946 static constexpr bool is_inner_panel(bool inner_panel) { return inner_panel; }
947
948 // Only enable non-const base function if XprType is not const (otherwise we get a duplicate definition).
949 template <typename T = XprType, typename EnableIf = std::enable_if_t<!std::is_const<T>::value>>
950 static EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE BaseType& base(XprType& xpr) {
951 return xpr;
952 }
953 static EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE const BaseType& base(const XprType& xpr) { return xpr; }
954 static constexpr EIGEN_ALWAYS_INLINE Index row(const XprType& /*xpr*/, Index r) { return r; }
955 static constexpr EIGEN_ALWAYS_INLINE Index col(const XprType& /*xpr*/, Index c) { return c; }
956};
957
958template <typename XprType, int BlockRows, int BlockCols, bool InnerPanel>
959struct block_xpr_helper<Block<XprType, BlockRows, BlockCols, InnerPanel>> {
960 using BlockXprType = Block<XprType, BlockRows, BlockCols, InnerPanel>;
961 // Recursive helper in case of explicit block-of-block expression.
962 using NestedXprHelper = block_xpr_helper<XprType>;
963 using BaseType = typename NestedXprHelper::BaseType;
964
965 // For block-of-block expressions, we need to combine the InnerPannel trait
966 // with that of the block subexpression.
967 static constexpr bool is_inner_panel(bool inner_panel) { return InnerPanel && inner_panel; }
968
969 // Only enable non-const base function if XprType is not const (otherwise we get a duplicates definition).
970 template <typename T = XprType, typename EnableIf = std::enable_if_t<!std::is_const<T>::value>>
971 static EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE BaseType& base(BlockXprType& xpr) {
972 return NestedXprHelper::base(xpr.nestedExpression());
973 }
974 static EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE const BaseType& base(const BlockXprType& xpr) {
975 return NestedXprHelper::base(xpr.nestedExpression());
976 }
977 static constexpr EIGEN_ALWAYS_INLINE Index row(const BlockXprType& xpr, Index r) {
978 return xpr.startRow() + NestedXprHelper::row(xpr.nestedExpression(), r);
979 }
980 static constexpr EIGEN_ALWAYS_INLINE Index col(const BlockXprType& xpr, Index c) {
981 return xpr.startCol() + NestedXprHelper::col(xpr.nestedExpression(), c);
982 }
983};
984
985template <typename XprType, int BlockRows, int BlockCols, bool InnerPanel>
986struct block_xpr_helper<const Block<XprType, BlockRows, BlockCols, InnerPanel>>
987 : block_xpr_helper<Block<XprType, BlockRows, BlockCols, InnerPanel>> {};
988
989template <typename XprType>
990struct is_matrix_base_xpr : std::is_base_of<MatrixBase<remove_all_t<XprType>>, remove_all_t<XprType>> {};
991
992template <typename XprType>
993struct is_permutation_base_xpr : std::is_base_of<PermutationBase<remove_all_t<XprType>>, remove_all_t<XprType>> {};
994
995} // end namespace internal
996
1037template <typename ScalarA, typename ScalarB, typename BinaryOp = internal::scalar_product_op<ScalarA, ScalarB>>
1039#ifndef EIGEN_PARSED_BY_DOXYGEN
1040 // for backward compatibility, use the hints given by the (deprecated) internal::scalar_product_traits class.
1041 : internal::scalar_product_traits<ScalarA, ScalarB>
1042#endif // EIGEN_PARSED_BY_DOXYGEN
1043{
1044};
1045
1046template <typename T, typename BinaryOp>
1047struct ScalarBinaryOpTraits<T, T, BinaryOp> {
1048 typedef T ReturnType;
1049};
1050
1051template <typename T, typename BinaryOp>
1052struct ScalarBinaryOpTraits<T, typename NumTraits<std::enable_if_t<NumTraits<T>::IsComplex, T>>::Real, BinaryOp> {
1053 typedef T ReturnType;
1054};
1055template <typename T, typename BinaryOp>
1056struct ScalarBinaryOpTraits<typename NumTraits<std::enable_if_t<NumTraits<T>::IsComplex, T>>::Real, T, BinaryOp> {
1057 typedef T ReturnType;
1058};
1059
1060// For Matrix * Permutation
1061template <typename T, typename BinaryOp>
1062struct ScalarBinaryOpTraits<T, void, BinaryOp> {
1063 typedef T ReturnType;
1064};
1065
1066// For Permutation * Matrix
1067template <typename T, typename BinaryOp>
1068struct ScalarBinaryOpTraits<void, T, BinaryOp> {
1069 typedef T ReturnType;
1070};
1071
1072// for Permutation*Permutation
1073template <typename BinaryOp>
1074struct ScalarBinaryOpTraits<void, void, BinaryOp> {
1075 typedef void ReturnType;
1076};
1077
1078// We require Lhs and Rhs to have "compatible" scalar types.
1079// It is tempting to always allow mixing different types but remember that this is often impossible in the vectorized
1080// paths. So allowing mixing different types gives very unexpected errors when enabling vectorization, when the user
1081// tries to add together a float matrix and a double matrix.
1082#define EIGEN_CHECK_BINARY_COMPATIBILIY(BINOP, LHS, RHS) \
1083 EIGEN_STATIC_ASSERT( \
1084 (Eigen::internal::has_ReturnType<ScalarBinaryOpTraits<LHS, RHS, BINOP>>::value), \
1085 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
1086
1087} // end namespace Eigen
1088
1089#endif // EIGEN_XPRHELPER_H
@ ColMajor
Definition Constants.h:318
@ RowMajor
Definition Constants.h:320
@ AutoAlign
Definition Constants.h:322
const unsigned int EvalBeforeNestingBit
Definition Constants.h:74
const unsigned int DirectAccessBit
Definition Constants.h:159
const unsigned int LvalueBit
Definition Constants.h:148
const unsigned int RowMajorBit
Definition Constants.h:70
Namespace containing all symbols from the Eigen library.
Definition Core:137
const int HugeCost
Definition Constants.h:48
const int DynamicIndex
Definition Constants.h:30
const int Dynamic
Definition Constants.h:25
Determines whether the given binary operation of two numeric types is allowed and what the scalar ret...
Definition XprHelper.h:1043