$darkmode
Eigen  5.0.1-dev
Array.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2009 Gael Guennebaud <gael.guennebaud@inria.fr>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_ARRAY_H
11 #define EIGEN_ARRAY_H
12 
13 // IWYU pragma: private
14 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
18 namespace internal {
19 template <typename Scalar_, int Rows_, int Cols_, int Options_, int MaxRows_, int MaxCols_>
20 struct traits<Array<Scalar_, Rows_, Cols_, Options_, MaxRows_, MaxCols_>>
21  : traits<Matrix<Scalar_, Rows_, Cols_, Options_, MaxRows_, MaxCols_>> {
22  typedef ArrayXpr XprKind;
23  typedef ArrayBase<Array<Scalar_, Rows_, Cols_, Options_, MaxRows_, MaxCols_>> XprBase;
24 };
25 } // namespace internal
26 
47 template <typename Scalar_, int Rows_, int Cols_, int Options_, int MaxRows_, int MaxCols_>
48 class Array : public PlainObjectBase<Array<Scalar_, Rows_, Cols_, Options_, MaxRows_, MaxCols_>> {
49  public:
51  EIGEN_DENSE_PUBLIC_INTERFACE(Array)
52 
53  enum { Options = Options_ };
54  typedef typename Base::PlainObject PlainObject;
55 
56  protected:
57  template <typename Derived, typename OtherDerived, bool IsVector>
58  friend struct internal::conservative_resize_like_impl;
59 
60  using Base::m_storage;
61 
62  public:
63  using Base::base;
64  using Base::coeff;
65  using Base::coeffRef;
66 
73  template <typename OtherDerived>
74  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Array& operator=(const EigenBase<OtherDerived>& other) {
75  return Base::operator=(other);
76  }
77 
81  /* This overload is needed because the usage of
82  * using Base::operator=;
83  * fails on MSVC. Since the code below is working with GCC and MSVC, we skipped
84  * the usage of 'using'. This should be done only for operator=.
85  */
86  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Array& operator=(const Scalar& value) {
87  Base::setConstant(value);
88  return *this;
89  }
90 
100  template <typename OtherDerived>
101  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Array& operator=(const DenseBase<OtherDerived>& other) {
102  return Base::_set(other);
103  }
104 
113  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Array& operator=(const Array& other) { return Base::_set(other); }
114 
125 #ifdef EIGEN_INITIALIZE_COEFFS
126  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr Array() : Base() { EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED }
127 #else
128  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr Array() = default;
129 #endif
130 
131  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr Array(Array&&) = default;
132  EIGEN_DEVICE_FUNC Array& operator=(Array&& other) noexcept(std::is_nothrow_move_assignable<Scalar>::value) {
133  Base::operator=(std::move(other));
134  return *this;
135  }
136 
153  template <typename... ArgTypes>
154  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Array(const Scalar& a0, const Scalar& a1, const Scalar& a2, const Scalar& a3,
155  const ArgTypes&... args)
156  : Base(a0, a1, a2, a3, args...) {}
157 
181  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr Array(
182  const std::initializer_list<std::initializer_list<Scalar>>& list)
183  : Base(list) {}
184 
185 #ifndef EIGEN_PARSED_BY_DOXYGEN
186  template <typename T>
187  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE explicit Array(const T& x) {
188  Base::template _init1<T>(x);
189  }
190 
191  template <typename T0, typename T1>
192  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Array(const T0& val0, const T1& val1) {
193  this->template _init2<T0, T1>(val0, val1);
194  }
195 
196 #else
197 
198  EIGEN_DEVICE_FUNC explicit Array(const Scalar* data);
205  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE explicit Array(Index dim);
208  Array(const Scalar& value);
214  Array(Index rows, Index cols);
217  Array(const Scalar& val0, const Scalar& val1);
218 #endif // end EIGEN_PARSED_BY_DOXYGEN
219 
223  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Array(const Scalar& val0, const Scalar& val1, const Scalar& val2) {
224  EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Array, 3)
225  m_storage.data()[0] = val0;
226  m_storage.data()[1] = val1;
227  m_storage.data()[2] = val2;
228  }
232  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Array(const Scalar& val0, const Scalar& val1, const Scalar& val2,
233  const Scalar& val3) {
234  EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Array, 4)
235  m_storage.data()[0] = val0;
236  m_storage.data()[1] = val1;
237  m_storage.data()[2] = val2;
238  m_storage.data()[3] = val3;
239  }
240 
242  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr Array(const Array&) = default;
243 
244  private:
245  struct PrivateType {};
246 
247  public:
249  template <typename OtherDerived>
250  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Array(
251  const EigenBase<OtherDerived>& other,
252  std::enable_if_t<internal::is_convertible<typename OtherDerived::Scalar, Scalar>::value, PrivateType> =
253  PrivateType())
254  : Base(other.derived()) {}
255 
256  EIGEN_DEVICE_FUNC constexpr Index innerStride() const noexcept { return 1; }
257  EIGEN_DEVICE_FUNC constexpr Index outerStride() const noexcept { return this->innerSize(); }
258 
259 #ifdef EIGEN_ARRAY_PLUGIN
260 #include EIGEN_ARRAY_PLUGIN
261 #endif
262 
263  private:
264  template <typename MatrixType, typename OtherDerived, bool SwapPointers>
265  friend struct internal::matrix_swap_impl;
266 };
267 
294 #define EIGEN_MAKE_ARRAY_TYPEDEFS(Type, TypeSuffix, Size, SizeSuffix) \
295  \
296  typedef Array<Type, Size, Size> Array##SizeSuffix##SizeSuffix##TypeSuffix; \
297  \
298  typedef Array<Type, Size, 1> Array##SizeSuffix##TypeSuffix;
299 
300 #define EIGEN_MAKE_ARRAY_FIXED_TYPEDEFS(Type, TypeSuffix, Size) \
301  \
302  typedef Array<Type, Size, Dynamic> Array##Size##X##TypeSuffix; \
303  \
304  typedef Array<Type, Dynamic, Size> Array##X##Size##TypeSuffix;
305 
306 #define EIGEN_MAKE_ARRAY_TYPEDEFS_ALL_SIZES(Type, TypeSuffix) \
307  EIGEN_MAKE_ARRAY_TYPEDEFS(Type, TypeSuffix, 2, 2) \
308  EIGEN_MAKE_ARRAY_TYPEDEFS(Type, TypeSuffix, 3, 3) \
309  EIGEN_MAKE_ARRAY_TYPEDEFS(Type, TypeSuffix, 4, 4) \
310  EIGEN_MAKE_ARRAY_TYPEDEFS(Type, TypeSuffix, Dynamic, X) \
311  EIGEN_MAKE_ARRAY_FIXED_TYPEDEFS(Type, TypeSuffix, 2) \
312  EIGEN_MAKE_ARRAY_FIXED_TYPEDEFS(Type, TypeSuffix, 3) \
313  EIGEN_MAKE_ARRAY_FIXED_TYPEDEFS(Type, TypeSuffix, 4)
314 
315 EIGEN_MAKE_ARRAY_TYPEDEFS_ALL_SIZES(int, i)
316 EIGEN_MAKE_ARRAY_TYPEDEFS_ALL_SIZES(float, f)
317 EIGEN_MAKE_ARRAY_TYPEDEFS_ALL_SIZES(double, d)
318 EIGEN_MAKE_ARRAY_TYPEDEFS_ALL_SIZES(std::complex<float>, cf)
319 EIGEN_MAKE_ARRAY_TYPEDEFS_ALL_SIZES(std::complex<double>, cd)
320 
321 #undef EIGEN_MAKE_ARRAY_TYPEDEFS_ALL_SIZES
322 #undef EIGEN_MAKE_ARRAY_TYPEDEFS
323 #undef EIGEN_MAKE_ARRAY_FIXED_TYPEDEFS
324 
325 #define EIGEN_MAKE_ARRAY_TYPEDEFS(Size, SizeSuffix) \
326  \
327  \
328  template <typename Type> \
329  using Array##SizeSuffix##SizeSuffix = Array<Type, Size, Size>; \
330  \
331  \
332  template <typename Type> \
333  using Array##SizeSuffix = Array<Type, Size, 1>;
334 
335 #define EIGEN_MAKE_ARRAY_FIXED_TYPEDEFS(Size) \
336  \
337  \
338  template <typename Type> \
339  using Array##Size##X = Array<Type, Size, Dynamic>; \
340  \
341  \
342  template <typename Type> \
343  using Array##X##Size = Array<Type, Dynamic, Size>;
344 
345 EIGEN_MAKE_ARRAY_TYPEDEFS(2, 2)
346 EIGEN_MAKE_ARRAY_TYPEDEFS(3, 3)
347 EIGEN_MAKE_ARRAY_TYPEDEFS(4, 4)
348 EIGEN_MAKE_ARRAY_TYPEDEFS(Dynamic, X)
349 EIGEN_MAKE_ARRAY_FIXED_TYPEDEFS(2)
350 EIGEN_MAKE_ARRAY_FIXED_TYPEDEFS(3)
351 EIGEN_MAKE_ARRAY_FIXED_TYPEDEFS(4)
352 
353 #undef EIGEN_MAKE_ARRAY_TYPEDEFS
354 #undef EIGEN_MAKE_ARRAY_FIXED_TYPEDEFS
355 
356 #define EIGEN_USING_ARRAY_TYPEDEFS_FOR_TYPE_AND_SIZE(TypeSuffix, SizeSuffix) \
357  using Eigen::Matrix##SizeSuffix##TypeSuffix; \
358  using Eigen::Vector##SizeSuffix##TypeSuffix; \
359  using Eigen::RowVector##SizeSuffix##TypeSuffix;
360 
361 #define EIGEN_USING_ARRAY_TYPEDEFS_FOR_TYPE(TypeSuffix) \
362  EIGEN_USING_ARRAY_TYPEDEFS_FOR_TYPE_AND_SIZE(TypeSuffix, 2) \
363  EIGEN_USING_ARRAY_TYPEDEFS_FOR_TYPE_AND_SIZE(TypeSuffix, 3) \
364  EIGEN_USING_ARRAY_TYPEDEFS_FOR_TYPE_AND_SIZE(TypeSuffix, 4) \
365  EIGEN_USING_ARRAY_TYPEDEFS_FOR_TYPE_AND_SIZE(TypeSuffix, X)
366 
367 #define EIGEN_USING_ARRAY_TYPEDEFS \
368  EIGEN_USING_ARRAY_TYPEDEFS_FOR_TYPE(i) \
369  EIGEN_USING_ARRAY_TYPEDEFS_FOR_TYPE(f) \
370  EIGEN_USING_ARRAY_TYPEDEFS_FOR_TYPE(d) \
371  EIGEN_USING_ARRAY_TYPEDEFS_FOR_TYPE(cf) \
372  EIGEN_USING_ARRAY_TYPEDEFS_FOR_TYPE(cd)
373 
374 } // end namespace Eigen
375 
376 #endif // EIGEN_ARRAY_H
Array & operator=(const Scalar &value)
Definition: Array.h:86
Array & operator=(const Array &other)
Assigns arrays to each other.
Definition: Array.h:113
Namespace containing all symbols from the Eigen library.
Definition: B01_Experimental.dox:1
constexpr Array(const std::initializer_list< std::initializer_list< Scalar >> &list)
Constructs an array and initializes it from the coefficients given as initializer-lists grouped by ro...
Definition: Array.h:181
Definition: BFloat16.h:231
Array(const EigenBase< OtherDerived > &other, std::enable_if_t< internal::is_convertible< typename OtherDerived::Scalar, Scalar >::value, PrivateType >=PrivateType())
Definition: Array.h:250
constexpr Array()=default
constexpr Derived & operator=(const PlainObjectBase &other)
Definition: PlainObjectBase.h:429
constexpr const Scalar * data() const
Definition: PlainObjectBase.h:247
Base class for all dense matrices, vectors, and arrays.
Definition: DenseBase.h:38
Array & operator=(const EigenBase< OtherDerived > &other)
Definition: Array.h:74
constexpr Scalar & coeffRef(Index rowId, Index colId)
Definition: PlainObjectBase.h:191
Definition: EigenBase.h:33
Array(const Scalar &val0, const Scalar &val1, const Scalar &val2)
Definition: Array.h:223
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
Dense storage base class for matrices and arrays.
Definition: PlainObjectBase.h:95
constexpr Derived & _set(const DenseBase< OtherDerived > &other)
Copies the value of the expression other into *this with automatic resizing.
Definition: PlainObjectBase.h:717
Array(const Scalar &a0, const Scalar &a1, const Scalar &a2, const Scalar &a3, const ArgTypes &... args)
Construct a row of column vector with fixed size from an arbitrary number of coefficients.
Definition: Array.h:154
Derived & setConstant(Index size, const Scalar &val)
Definition: CwiseNullaryOp.h:363
constexpr const Scalar & coeff(Index rowId, Index colId) const
Definition: PlainObjectBase.h:172
General-purpose arrays with easy API for coefficient-wise operations.
Definition: Array.h:48
Array(const Scalar &val0, const Scalar &val1, const Scalar &val2, const Scalar &val3)
Definition: Array.h:232
const int Dynamic
Definition: Constants.h:25
Array & operator=(const DenseBase< OtherDerived > &other)
Definition: Array.h:101