Eigen  3.4.90 (git rev 5a9f66fb35d03a4da9ef8976e67a61b30aa16dcf)
 
Loading...
Searching...
No Matches
DenseCoeffsBase.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2006-2010 Benoit Jacob <[email protected]>
5//
6// This Source Code Form is subject to the terms of the Mozilla
7// Public License v. 2.0. If a copy of the MPL was not distributed
8// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9
10#ifndef EIGEN_DENSECOEFFSBASE_H
11#define EIGEN_DENSECOEFFSBASE_H
12
13// IWYU pragma: private
14#include "./InternalHeaderCheck.h"
15
16namespace Eigen {
17
18namespace internal {
19template <typename T>
20struct add_const_on_value_type_if_arithmetic {
21 typedef std::conditional_t<is_arithmetic<T>::value, T, add_const_on_value_type_t<T>> type;
22};
23} // namespace internal
24
37template <typename Derived>
38class DenseCoeffsBase<Derived, ReadOnlyAccessors> : public EigenBase<Derived> {
39 public:
40 typedef typename internal::traits<Derived>::StorageKind StorageKind;
41 typedef typename internal::traits<Derived>::Scalar Scalar;
42 typedef typename internal::packet_traits<Scalar>::type PacketScalar;
43
44 // Explanation for this CoeffReturnType typedef.
45 // - This is the return type of the coeff() method.
46 // - The LvalueBit means exactly that we can offer a coeffRef() method, which means exactly that we can get references
47 // to coeffs, which means exactly that we can have coeff() return a const reference (as opposed to returning a value).
48 // - The is_arithmetic check is required since "const int", "const double", etc. will cause warnings on some systems
49 // while the declaration of "const T", where T is a non arithmetic type does not. Always returning "const Scalar&" is
50 // not possible, since the underlying expressions might not offer a valid address the reference could be referring to.
51 typedef std::conditional_t<bool(internal::traits<Derived>::Flags& LvalueBit), const Scalar&,
52 std::conditional_t<internal::is_arithmetic<Scalar>::value, Scalar, const Scalar>>
53 CoeffReturnType;
54
55 typedef typename internal::add_const_on_value_type_if_arithmetic<typename internal::packet_traits<Scalar>::type>::type
56 PacketReturnType;
57
59 using Base::cols;
60 using Base::derived;
61 using Base::rows;
62 using Base::size;
63
64 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index rowIndexByOuterInner(Index outer, Index inner) const {
65 return int(Derived::RowsAtCompileTime) == 1 ? 0
66 : int(Derived::ColsAtCompileTime) == 1 ? inner
67 : int(Derived::Flags) & RowMajorBit ? outer
68 : inner;
69 }
70
71 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index colIndexByOuterInner(Index outer, Index inner) const {
72 return int(Derived::ColsAtCompileTime) == 1 ? 0
73 : int(Derived::RowsAtCompileTime) == 1 ? inner
74 : int(Derived::Flags) & RowMajorBit ? inner
75 : outer;
76 }
77
92 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR CoeffReturnType coeff(Index row, Index col) const {
93 eigen_internal_assert(row >= 0 && row < rows() && col >= 0 && col < cols());
94 return internal::evaluator<Derived>(derived()).coeff(row, col);
95 }
96
97 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR CoeffReturnType coeffByOuterInner(Index outer,
98 Index inner) const {
99 return coeff(rowIndexByOuterInner(outer, inner), colIndexByOuterInner(outer, inner));
100 }
101
106 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR CoeffReturnType operator()(Index row, Index col) const {
107 eigen_assert(row >= 0 && row < rows() && col >= 0 && col < cols());
108 return coeff(row, col);
109 }
110
126 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR CoeffReturnType coeff(Index index) const {
127 EIGEN_STATIC_ASSERT(internal::evaluator<Derived>::Flags & LinearAccessBit,
128 THIS_COEFFICIENT_ACCESSOR_TAKING_ONE_ACCESS_IS_ONLY_FOR_EXPRESSIONS_ALLOWING_LINEAR_ACCESS)
129 eigen_internal_assert(index >= 0 && index < size());
130 return internal::evaluator<Derived>(derived()).coeff(index);
131 }
132
141 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR CoeffReturnType operator[](Index index) const {
142 EIGEN_STATIC_ASSERT(Derived::IsVectorAtCompileTime,
143 THE_BRACKET_OPERATOR_IS_ONLY_FOR_VECTORS__USE_THE_PARENTHESIS_OPERATOR_INSTEAD)
144 eigen_assert(index >= 0 && index < size());
145 return coeff(index);
146 }
147
158 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR CoeffReturnType operator()(Index index) const {
159 eigen_assert(index >= 0 && index < size());
160 return coeff(index);
161 }
162
165 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR CoeffReturnType x() const { return (*this)[0]; }
166
169 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR CoeffReturnType y() const {
170 EIGEN_STATIC_ASSERT(Derived::SizeAtCompileTime == -1 || Derived::SizeAtCompileTime >= 2, OUT_OF_RANGE_ACCESS);
171 return (*this)[1];
172 }
173
176 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR CoeffReturnType z() const {
177 EIGEN_STATIC_ASSERT(Derived::SizeAtCompileTime == -1 || Derived::SizeAtCompileTime >= 3, OUT_OF_RANGE_ACCESS);
178 return (*this)[2];
179 }
180
183 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR CoeffReturnType w() const {
184 EIGEN_STATIC_ASSERT(Derived::SizeAtCompileTime == -1 || Derived::SizeAtCompileTime >= 4, OUT_OF_RANGE_ACCESS);
185 return (*this)[3];
186 }
187
198 template <int LoadMode>
199 EIGEN_STRONG_INLINE PacketReturnType packet(Index row, Index col) const {
200 typedef typename internal::packet_traits<Scalar>::type DefaultPacketType;
201 eigen_internal_assert(row >= 0 && row < rows() && col >= 0 && col < cols());
202 return internal::evaluator<Derived>(derived()).template packet<LoadMode, DefaultPacketType>(row, col);
203 }
204
206 template <int LoadMode>
207 EIGEN_STRONG_INLINE PacketReturnType packetByOuterInner(Index outer, Index inner) const {
208 return packet<LoadMode>(rowIndexByOuterInner(outer, inner), colIndexByOuterInner(outer, inner));
209 }
210
221 template <int LoadMode>
222 EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const {
223 EIGEN_STATIC_ASSERT(internal::evaluator<Derived>::Flags & LinearAccessBit,
224 THIS_COEFFICIENT_ACCESSOR_TAKING_ONE_ACCESS_IS_ONLY_FOR_EXPRESSIONS_ALLOWING_LINEAR_ACCESS)
225 typedef typename internal::packet_traits<Scalar>::type DefaultPacketType;
226 eigen_internal_assert(index >= 0 && index < size());
227 return internal::evaluator<Derived>(derived()).template packet<LoadMode, DefaultPacketType>(index);
228 }
229
230 protected:
231 // explanation: DenseBase is doing "using ..." on the methods from DenseCoeffsBase.
232 // But some methods are only available in the DirectAccess case.
233 // So we add dummy methods here with these names, so that "using... " doesn't fail.
234 // It's not private so that the child class DenseBase can access them, and it's not public
235 // either since it's an implementation detail, so has to be protected.
236 void coeffRef();
237 void coeffRefByOuterInner();
238 void writePacket();
239 void writePacketByOuterInner();
240 void copyCoeff();
241 void copyCoeffByOuterInner();
242 void copyPacket();
243 void copyPacketByOuterInner();
244 void stride();
245 void innerStride();
246 void outerStride();
247 void rowStride();
248 void colStride();
249};
250
263template <typename Derived>
264class DenseCoeffsBase<Derived, WriteAccessors> : public DenseCoeffsBase<Derived, ReadOnlyAccessors> {
265 public:
266 typedef DenseCoeffsBase<Derived, ReadOnlyAccessors> Base;
267
268 typedef typename internal::traits<Derived>::StorageKind StorageKind;
269 typedef typename internal::traits<Derived>::Scalar Scalar;
270 typedef typename internal::packet_traits<Scalar>::type PacketScalar;
271 typedef typename NumTraits<Scalar>::Real RealScalar;
272
273 using Base::coeff;
274 using Base::colIndexByOuterInner;
275 using Base::cols;
276 using Base::derived;
277 using Base::rowIndexByOuterInner;
278 using Base::rows;
279 using Base::size;
280 using Base::operator[];
281 using Base::operator();
282 using Base::w;
283 using Base::x;
284 using Base::y;
285 using Base::z;
286
301 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(Index row, Index col) {
302 eigen_internal_assert(row >= 0 && row < rows() && col >= 0 && col < cols());
303 return internal::evaluator<Derived>(derived()).coeffRef(row, col);
304 }
305
306 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRefByOuterInner(Index outer, Index inner) {
307 return coeffRef(rowIndexByOuterInner(outer, inner), colIndexByOuterInner(outer, inner));
308 }
309
315 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& operator()(Index row, Index col) {
316 eigen_assert(row >= 0 && row < rows() && col >= 0 && col < cols());
317 return coeffRef(row, col);
318 }
319
335 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(Index index) {
336 EIGEN_STATIC_ASSERT(internal::evaluator<Derived>::Flags & LinearAccessBit,
337 THIS_COEFFICIENT_ACCESSOR_TAKING_ONE_ACCESS_IS_ONLY_FOR_EXPRESSIONS_ALLOWING_LINEAR_ACCESS)
338 eigen_internal_assert(index >= 0 && index < size());
339 return internal::evaluator<Derived>(derived()).coeffRef(index);
340 }
341
349 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& operator[](Index index) {
350 EIGEN_STATIC_ASSERT(Derived::IsVectorAtCompileTime,
351 THE_BRACKET_OPERATOR_IS_ONLY_FOR_VECTORS__USE_THE_PARENTHESIS_OPERATOR_INSTEAD)
352 eigen_assert(index >= 0 && index < size());
353 return coeffRef(index);
354 }
355
365 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Scalar& operator()(Index index) {
366 eigen_assert(index >= 0 && index < size());
367 return coeffRef(index);
368 }
369
372 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Scalar& x() { return (*this)[0]; }
373
376 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Scalar& y() {
377 EIGEN_STATIC_ASSERT(Derived::SizeAtCompileTime == -1 || Derived::SizeAtCompileTime >= 2, OUT_OF_RANGE_ACCESS);
378 return (*this)[1];
379 }
380
383 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Scalar& z() {
384 EIGEN_STATIC_ASSERT(Derived::SizeAtCompileTime == -1 || Derived::SizeAtCompileTime >= 3, OUT_OF_RANGE_ACCESS);
385 return (*this)[2];
386 }
387
390 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Scalar& w() {
391 EIGEN_STATIC_ASSERT(Derived::SizeAtCompileTime == -1 || Derived::SizeAtCompileTime >= 4, OUT_OF_RANGE_ACCESS);
392 return (*this)[3];
393 }
394};
395
408template <typename Derived>
409class DenseCoeffsBase<Derived, DirectAccessors> : public DenseCoeffsBase<Derived, ReadOnlyAccessors> {
410 public:
411 typedef DenseCoeffsBase<Derived, ReadOnlyAccessors> Base;
412 typedef typename internal::traits<Derived>::Scalar Scalar;
413 typedef typename NumTraits<Scalar>::Real RealScalar;
414
415 using Base::cols;
416 using Base::derived;
417 using Base::rows;
418 using Base::size;
419
424 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR inline Index innerStride() const { return derived().innerStride(); }
425
431 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR inline Index outerStride() const { return derived().outerStride(); }
432
433 // FIXME shall we remove it ?
434 EIGEN_CONSTEXPR inline Index stride() const { return Derived::IsVectorAtCompileTime ? innerStride() : outerStride(); }
435
440 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR inline Index rowStride() const {
441 return Derived::IsRowMajor ? outerStride() : innerStride();
442 }
443
448 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR inline Index colStride() const {
449 return Derived::IsRowMajor ? innerStride() : outerStride();
450 }
451};
452
465template <typename Derived>
466class DenseCoeffsBase<Derived, DirectWriteAccessors> : public DenseCoeffsBase<Derived, WriteAccessors> {
467 public:
468 typedef DenseCoeffsBase<Derived, WriteAccessors> Base;
469 typedef typename internal::traits<Derived>::Scalar Scalar;
470 typedef typename NumTraits<Scalar>::Real RealScalar;
471
472 using Base::cols;
473 using Base::derived;
474 using Base::rows;
475 using Base::size;
476
481 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR inline Index innerStride() const EIGEN_NOEXCEPT { return derived().innerStride(); }
482
488 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR inline Index outerStride() const EIGEN_NOEXCEPT { return derived().outerStride(); }
489
490 // FIXME shall we remove it ?
491 EIGEN_CONSTEXPR inline Index stride() const EIGEN_NOEXCEPT {
492 return Derived::IsVectorAtCompileTime ? innerStride() : outerStride();
493 }
494
499 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR inline Index rowStride() const EIGEN_NOEXCEPT {
500 return Derived::IsRowMajor ? outerStride() : innerStride();
501 }
502
507 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR inline Index colStride() const EIGEN_NOEXCEPT {
508 return Derived::IsRowMajor ? innerStride() : outerStride();
509 }
510};
511
512namespace internal {
513
514template <int Alignment, typename Derived, bool JustReturnZero>
515struct first_aligned_impl {
516 static EIGEN_CONSTEXPR inline Index run(const Derived&) EIGEN_NOEXCEPT { return 0; }
517};
518
519template <int Alignment, typename Derived>
520struct first_aligned_impl<Alignment, Derived, false> {
521 static inline Index run(const Derived& m) { return internal::first_aligned<Alignment>(m.data(), m.size()); }
522};
523
532template <int Alignment, typename Derived>
533static inline Index first_aligned(const DenseBase<Derived>& m) {
534 enum { ReturnZero = (int(evaluator<Derived>::Alignment) >= Alignment) || !(Derived::Flags & DirectAccessBit) };
535 return first_aligned_impl<Alignment, Derived, ReturnZero>::run(m.derived());
536}
537
538template <typename Derived>
539static inline Index first_default_aligned(const DenseBase<Derived>& m) {
540 typedef typename Derived::Scalar Scalar;
541 typedef typename packet_traits<Scalar>::type DefaultPacketType;
542 return internal::first_aligned<int(unpacket_traits<DefaultPacketType>::alignment), Derived>(m);
543}
544
545template <typename Derived, bool HasDirectAccess = has_direct_access<Derived>::ret>
546struct inner_stride_at_compile_time {
547 enum { ret = traits<Derived>::InnerStrideAtCompileTime };
548};
549
550template <typename Derived>
551struct inner_stride_at_compile_time<Derived, false> {
552 enum { ret = 0 };
553};
554
555template <typename Derived, bool HasDirectAccess = has_direct_access<Derived>::ret>
556struct outer_stride_at_compile_time {
557 enum { ret = traits<Derived>::OuterStrideAtCompileTime };
558};
559
560template <typename Derived>
561struct outer_stride_at_compile_time<Derived, false> {
562 enum { ret = 0 };
563};
564
565} // end namespace internal
566
567} // end namespace Eigen
568
569#endif // EIGEN_DENSECOEFFSBASE_H
EIGEN_CONSTEXPR Index innerStride() const
Definition DenseCoeffsBase.h:424
EIGEN_CONSTEXPR Index rowStride() const
Definition DenseCoeffsBase.h:440
EIGEN_CONSTEXPR Index outerStride() const
Definition DenseCoeffsBase.h:431
EIGEN_CONSTEXPR Index colStride() const
Definition DenseCoeffsBase.h:448
EIGEN_CONSTEXPR Index innerStride() const EIGEN_NOEXCEPT
Definition DenseCoeffsBase.h:481
EIGEN_CONSTEXPR Index rowStride() const EIGEN_NOEXCEPT
Definition DenseCoeffsBase.h:499
EIGEN_CONSTEXPR Index outerStride() const EIGEN_NOEXCEPT
Definition DenseCoeffsBase.h:488
EIGEN_CONSTEXPR Index colStride() const EIGEN_NOEXCEPT
Definition DenseCoeffsBase.h:507
Base class providing read-only coefficient access to matrices and arrays.
Definition DenseCoeffsBase.h:38
EIGEN_CONSTEXPR CoeffReturnType operator[](Index index) const
Definition DenseCoeffsBase.h:141
EIGEN_CONSTEXPR CoeffReturnType y() const
Definition DenseCoeffsBase.h:169
EIGEN_CONSTEXPR CoeffReturnType w() const
Definition DenseCoeffsBase.h:183
EIGEN_CONSTEXPR CoeffReturnType x() const
Definition DenseCoeffsBase.h:165
EIGEN_CONSTEXPR CoeffReturnType operator()(Index row, Index col) const
Definition DenseCoeffsBase.h:106
EIGEN_CONSTEXPR CoeffReturnType operator()(Index index) const
Definition DenseCoeffsBase.h:158
EIGEN_CONSTEXPR CoeffReturnType coeff(Index index) const
Definition DenseCoeffsBase.h:126
EIGEN_CONSTEXPR CoeffReturnType z() const
Definition DenseCoeffsBase.h:176
EIGEN_CONSTEXPR CoeffReturnType coeff(Index row, Index col) const
Definition DenseCoeffsBase.h:92
Base class providing read/write coefficient access to matrices and arrays.
Definition DenseCoeffsBase.h:264
EIGEN_CONSTEXPR Scalar & x()
Definition DenseCoeffsBase.h:372
EIGEN_CONSTEXPR Scalar & z()
Definition DenseCoeffsBase.h:383
EIGEN_CONSTEXPR Scalar & w()
Definition DenseCoeffsBase.h:390
Scalar & operator[](Index index)
Definition DenseCoeffsBase.h:349
Scalar & operator()(Index row, Index col)
Definition DenseCoeffsBase.h:315
Scalar & coeffRef(Index row, Index col)
Definition DenseCoeffsBase.h:301
EIGEN_CONSTEXPR Scalar & operator()(Index index)
Definition DenseCoeffsBase.h:365
Scalar & coeffRef(Index index)
Definition DenseCoeffsBase.h:335
EIGEN_CONSTEXPR Scalar & y()
Definition DenseCoeffsBase.h:376
@ DirectAccessors
Definition Constants.h:376
@ ReadOnlyAccessors
Definition Constants.h:372
@ WriteAccessors
Definition Constants.h:374
@ DirectWriteAccessors
Definition Constants.h:378
const unsigned int LinearAccessBit
Definition Constants.h:133
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
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:83
Definition EigenBase.h:33
Eigen::Index Index
The interface type of indices.
Definition EigenBase.h:43