DenseCoeffsBase.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2006-2010 Benoit Jacob <jacob.benoit.1@gmail.com>
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 
16 namespace Eigen {
17 
18 namespace internal {
19 template <typename T>
22 };
23 } // namespace internal
24 
37 template <typename Derived>
38 class DenseCoeffsBase<Derived, ReadOnlyAccessors> : public EigenBase<Derived> {
39  public:
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&,
54 
57 
59  using Base::cols;
60  using Base::derived;
61  using Base::rows;
62  using Base::size;
63 
65  return int(Derived::RowsAtCompileTime) == 1 ? 0
66  : int(Derived::ColsAtCompileTime) == 1 ? inner
67  : int(Derived::Flags) & RowMajorBit ? outer
68  : inner;
69  }
70 
72  return int(Derived::ColsAtCompileTime) == 1 ? 0
73  : int(Derived::RowsAtCompileTime) == 1 ? inner
74  : int(Derived::Flags) & RowMajorBit ? inner
75  : outer;
76  }
77 
93  eigen_internal_assert(row >= 0 && row < rows() && col >= 0 && col < cols());
94  return internal::evaluator<Derived>(derived()).coeff(row, col);
95  }
96 
98  Index inner) const {
99  return coeff(rowIndexByOuterInner(outer, inner), colIndexByOuterInner(outer, inner));
100  }
101 
107  eigen_assert(row >= 0 && row < rows() && col >= 0 && col < cols());
108  return coeff(row, col);
109  }
110 
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 
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 
159  eigen_assert(index >= 0 && index < size());
160  return coeff(index);
161  }
162 
166 
170  EIGEN_STATIC_ASSERT(Derived::SizeAtCompileTime == -1 || Derived::SizeAtCompileTime >= 2, OUT_OF_RANGE_ACCESS);
171  return (*this)[1];
172  }
173 
177  EIGEN_STATIC_ASSERT(Derived::SizeAtCompileTime == -1 || Derived::SizeAtCompileTime >= 3, OUT_OF_RANGE_ACCESS);
178  return (*this)[2];
179  }
180 
184  EIGEN_STATIC_ASSERT(Derived::SizeAtCompileTime == -1 || Derived::SizeAtCompileTime >= 4, OUT_OF_RANGE_ACCESS);
185  return (*this)[3];
186  }
187 
198  template <int LoadMode>
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>
208  return packet<LoadMode>(rowIndexByOuterInner(outer, inner), colIndexByOuterInner(outer, inner));
209  }
210 
221  template <int LoadMode>
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();
238  void writePacket();
240  void copyCoeff();
242  void copyPacket();
244  void stride();
245  void innerStride();
246  void outerStride();
247  void rowStride();
248  void colStride();
249 };
250 
263 template <typename Derived>
264 class DenseCoeffsBase<Derived, WriteAccessors> : public DenseCoeffsBase<Derived, ReadOnlyAccessors> {
265  public:
267 
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 
302  eigen_internal_assert(row >= 0 && row < rows() && col >= 0 && col < cols());
303  return internal::evaluator<Derived>(derived()).coeffRef(row, col);
304  }
305 
307  return coeffRef(rowIndexByOuterInner(outer, inner), colIndexByOuterInner(outer, inner));
308  }
309 
316  eigen_assert(row >= 0 && row < rows() && col >= 0 && col < cols());
317  return coeffRef(row, col);
318  }
319 
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 
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 
366  eigen_assert(index >= 0 && index < size());
367  return coeffRef(index);
368  }
369 
373 
377  EIGEN_STATIC_ASSERT(Derived::SizeAtCompileTime == -1 || Derived::SizeAtCompileTime >= 2, OUT_OF_RANGE_ACCESS);
378  return (*this)[1];
379  }
380 
384  EIGEN_STATIC_ASSERT(Derived::SizeAtCompileTime == -1 || Derived::SizeAtCompileTime >= 3, OUT_OF_RANGE_ACCESS);
385  return (*this)[2];
386  }
387 
391  EIGEN_STATIC_ASSERT(Derived::SizeAtCompileTime == -1 || Derived::SizeAtCompileTime >= 4, OUT_OF_RANGE_ACCESS);
392  return (*this)[3];
393  }
394 };
395 
408 template <typename Derived>
409 class DenseCoeffsBase<Derived, DirectAccessors> : public DenseCoeffsBase<Derived, ReadOnlyAccessors> {
410  public:
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 
441  return Derived::IsRowMajor ? outerStride() : innerStride();
442  }
443 
449  return Derived::IsRowMajor ? innerStride() : outerStride();
450  }
451 };
452 
465 template <typename Derived>
466 class DenseCoeffsBase<Derived, DirectWriteAccessors> : public DenseCoeffsBase<Derived, WriteAccessors> {
467  public:
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 ?
492  return Derived::IsVectorAtCompileTime ? innerStride() : outerStride();
493  }
494 
500  return Derived::IsRowMajor ? outerStride() : innerStride();
501  }
502 
508  return Derived::IsRowMajor ? innerStride() : outerStride();
509  }
510 };
511 
512 namespace internal {
513 
514 template <int Alignment, typename Derived, bool JustReturnZero>
516  static EIGEN_CONSTEXPR inline Index run(const Derived&) EIGEN_NOEXCEPT { return 0; }
517 };
518 
519 template <int Alignment, typename Derived>
520 struct 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 
532 template <int Alignment, typename Derived>
533 static inline Index first_aligned(const DenseBase<Derived>& m) {
534  enum { ReturnZero = (int(evaluator<Derived>::Alignment) >= Alignment) || !(Derived::Flags & DirectAccessBit) };
536 }
537 
538 template <typename Derived>
540  typedef typename Derived::Scalar Scalar;
541  typedef typename packet_traits<Scalar>::type DefaultPacketType;
543 }
544 
548 };
549 
550 template <typename Derived>
551 struct inner_stride_at_compile_time<Derived, false> {
552  enum { ret = 0 };
553 };
554 
558 };
559 
560 template <typename Derived>
561 struct 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::Triplet< double > T
Definition: EigenUnitTest.cpp:11
#define eigen_internal_assert(x)
Definition: Macros.h:916
#define EIGEN_NOEXCEPT
Definition: Macros.h:1267
#define EIGEN_CONSTEXPR
Definition: Macros.h:758
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:892
#define eigen_assert(x)
Definition: Macros.h:910
#define EIGEN_STRONG_INLINE
Definition: Macros.h:834
m col(1)
m row(1)
RowVector3d w
Definition: Matrix_resize_int.cpp:3
#define EIGEN_STATIC_ASSERT(X, MSG)
Definition: StaticAssert.h:26
int rows
Definition: Tutorial_commainit_02.cpp:1
int cols
Definition: Tutorial_commainit_02.cpp:1
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
SCALAR Scalar
Definition: bench_gemm.cpp:45
Base class for all dense matrices, vectors, and arrays.
Definition: DenseBase.h:44
EIGEN_CONSTEXPR Index stride() const
Definition: DenseCoeffsBase.h:434
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index colStride() const
Definition: DenseCoeffsBase.h:448
DenseCoeffsBase< Derived, ReadOnlyAccessors > Base
Definition: DenseCoeffsBase.h:411
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index outerStride() const
Definition: DenseCoeffsBase.h:431
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index rowStride() const
Definition: DenseCoeffsBase.h:440
NumTraits< Scalar >::Real RealScalar
Definition: DenseCoeffsBase.h:413
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index innerStride() const
Definition: DenseCoeffsBase.h:424
internal::traits< Derived >::Scalar Scalar
Definition: DenseCoeffsBase.h:412
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index innerStride() const EIGEN_NOEXCEPT
Definition: DenseCoeffsBase.h:481
NumTraits< Scalar >::Real RealScalar
Definition: DenseCoeffsBase.h:470
DenseCoeffsBase< Derived, WriteAccessors > Base
Definition: DenseCoeffsBase.h:468
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index rowStride() const EIGEN_NOEXCEPT
Definition: DenseCoeffsBase.h:499
internal::traits< Derived >::Scalar Scalar
Definition: DenseCoeffsBase.h:469
EIGEN_CONSTEXPR Index stride() const EIGEN_NOEXCEPT
Definition: DenseCoeffsBase.h:491
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index outerStride() const EIGEN_NOEXCEPT
Definition: DenseCoeffsBase.h:488
EIGEN_DEVICE_FUNC 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
internal::packet_traits< Scalar >::type PacketScalar
Definition: DenseCoeffsBase.h:42
internal::add_const_on_value_type_if_arithmetic< typename internal::packet_traits< Scalar >::type >::type PacketReturnType
Definition: DenseCoeffsBase.h:56
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR CoeffReturnType coeff(Index index) const
Definition: DenseCoeffsBase.h:126
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR CoeffReturnType operator()(Index row, Index col) const
Definition: DenseCoeffsBase.h:106
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR CoeffReturnType w() const
Definition: DenseCoeffsBase.h:183
internal::traits< Derived >::StorageKind StorageKind
Definition: DenseCoeffsBase.h:40
std::conditional_t< bool(internal::traits< Derived >::Flags &LvalueBit), const Scalar &, std::conditional_t< internal::is_arithmetic< Scalar >::value, Scalar, const Scalar > > CoeffReturnType
Definition: DenseCoeffsBase.h:53
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR CoeffReturnType x() const
Definition: DenseCoeffsBase.h:165
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR CoeffReturnType y() const
Definition: DenseCoeffsBase.h:169
EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
Definition: DenseCoeffsBase.h:222
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR CoeffReturnType coeffByOuterInner(Index outer, Index inner) const
Definition: DenseCoeffsBase.h:97
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index colIndexByOuterInner(Index outer, Index inner) const
Definition: DenseCoeffsBase.h:71
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR CoeffReturnType z() const
Definition: DenseCoeffsBase.h:176
EIGEN_STRONG_INLINE PacketReturnType packetByOuterInner(Index outer, Index inner) const
Definition: DenseCoeffsBase.h:207
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR CoeffReturnType operator()(Index index) const
Definition: DenseCoeffsBase.h:158
EIGEN_STRONG_INLINE PacketReturnType packet(Index row, Index col) const
Definition: DenseCoeffsBase.h:199
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR CoeffReturnType operator[](Index index) const
Definition: DenseCoeffsBase.h:141
internal::traits< Derived >::Scalar Scalar
Definition: DenseCoeffsBase.h:41
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR CoeffReturnType coeff(Index row, Index col) const
Definition: DenseCoeffsBase.h:92
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index rowIndexByOuterInner(Index outer, Index inner) const
Definition: DenseCoeffsBase.h:64
EigenBase< Derived > Base
Definition: DenseCoeffsBase.h:58
Base class providing read/write coefficient access to matrices and arrays.
Definition: DenseCoeffsBase.h:264
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & coeffRefByOuterInner(Index outer, Index inner)
Definition: DenseCoeffsBase.h:306
NumTraits< Scalar >::Real RealScalar
Definition: DenseCoeffsBase.h:271
EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Scalar & operator()(Index row, Index col)
Definition: DenseCoeffsBase.h:315
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Scalar & operator()(Index index)
Definition: DenseCoeffsBase.h:365
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Scalar & x()
Definition: DenseCoeffsBase.h:372
EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Scalar & coeffRef(Index row, Index col)
Definition: DenseCoeffsBase.h:301
DenseCoeffsBase< Derived, ReadOnlyAccessors > Base
Definition: DenseCoeffsBase.h:266
EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Scalar & coeffRef(Index index)
Definition: DenseCoeffsBase.h:335
internal::packet_traits< Scalar >::type PacketScalar
Definition: DenseCoeffsBase.h:270
EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Scalar & operator[](Index index)
Definition: DenseCoeffsBase.h:349
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Scalar & z()
Definition: DenseCoeffsBase.h:383
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Scalar & w()
Definition: DenseCoeffsBase.h:390
internal::traits< Derived >::Scalar Scalar
Definition: DenseCoeffsBase.h:269
internal::traits< Derived >::StorageKind StorageKind
Definition: DenseCoeffsBase.h:268
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Scalar & y()
Definition: DenseCoeffsBase.h:376
Definition: ForwardDeclarations.h:63
@ 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
Scalar * y
Definition: level1_cplx_impl.h:128
return int(ret)+1
Eigen::DenseIndex ret
Definition: level1_cplx_impl.h:43
int * m
Definition: level2_cplx_impl.h:294
static Index first_default_aligned(const DenseBase< Derived > &m)
Definition: DenseCoeffsBase.h:539
static Index first_aligned(const DenseBase< Derived > &m)
Definition: DenseCoeffsBase.h:533
typename add_const_on_value_type< T >::type add_const_on_value_type_t
Definition: Meta.h:274
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
squared absolute value
Definition: GlobalFunctions.h:87
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
type
Definition: compute_granudrum_aor.py:141
Definition: Eigen_Colamd.h:49
list x
Definition: plotDoE.py:28
Definition: EigenBase.h:33
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:43
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:217
std::conditional_t< is_arithmetic< T >::value, T, add_const_on_value_type_t< T > > type
Definition: DenseCoeffsBase.h:21
Definition: CoreEvaluators.h:104
static Index run(const Derived &m)
Definition: DenseCoeffsBase.h:521
Definition: DenseCoeffsBase.h:515
static EIGEN_CONSTEXPR Index run(const Derived &) EIGEN_NOEXCEPT
Definition: DenseCoeffsBase.h:516
Definition: DenseCoeffsBase.h:546
@ ret
Definition: DenseCoeffsBase.h:547
Definition: DenseCoeffsBase.h:556
@ ret
Definition: DenseCoeffsBase.h:557
Definition: GenericPacketMath.h:108
Definition: ForwardDeclarations.h:21
Definition: GenericPacketMath.h:134