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 | namespace Eigen {
|
---|
14 |
|
---|
15 | /** \class Array
|
---|
16 | * \ingroup Core_Module
|
---|
17 | *
|
---|
18 | * \brief General-purpose arrays with easy API for coefficient-wise operations
|
---|
19 | *
|
---|
20 | * The %Array class is very similar to the Matrix class. It provides
|
---|
21 | * general-purpose one- and two-dimensional arrays. The difference between the
|
---|
22 | * %Array and the %Matrix class is primarily in the API: the API for the
|
---|
23 | * %Array class provides easy access to coefficient-wise operations, while the
|
---|
24 | * API for the %Matrix class provides easy access to linear-algebra
|
---|
25 | * operations.
|
---|
26 | *
|
---|
27 | * This class can be extended with the help of the plugin mechanism described on the page
|
---|
28 | * \ref TopicCustomizingEigen by defining the preprocessor symbol \c EIGEN_ARRAY_PLUGIN.
|
---|
29 | *
|
---|
30 | * \sa \ref TutorialArrayClass, \ref TopicClassHierarchy
|
---|
31 | */
|
---|
32 | namespace internal {
|
---|
33 | template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
|
---|
34 | struct traits<Array<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> > : traits<Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> >
|
---|
35 | {
|
---|
36 | typedef ArrayXpr XprKind;
|
---|
37 | typedef ArrayBase<Array<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> > XprBase;
|
---|
38 | };
|
---|
39 | }
|
---|
40 |
|
---|
41 | template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
|
---|
42 | class Array
|
---|
43 | : public PlainObjectBase<Array<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> >
|
---|
44 | {
|
---|
45 | public:
|
---|
46 |
|
---|
47 | typedef PlainObjectBase<Array> Base;
|
---|
48 | EIGEN_DENSE_PUBLIC_INTERFACE(Array)
|
---|
49 |
|
---|
50 | enum { Options = _Options };
|
---|
51 | typedef typename Base::PlainObject PlainObject;
|
---|
52 |
|
---|
53 | protected:
|
---|
54 | template <typename Derived, typename OtherDerived, bool IsVector>
|
---|
55 | friend struct internal::conservative_resize_like_impl;
|
---|
56 |
|
---|
57 | using Base::m_storage;
|
---|
58 |
|
---|
59 | public:
|
---|
60 |
|
---|
61 | using Base::base;
|
---|
62 | using Base::coeff;
|
---|
63 | using Base::coeffRef;
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * The usage of
|
---|
67 | * using Base::operator=;
|
---|
68 | * fails on MSVC. Since the code below is working with GCC and MSVC, we skipped
|
---|
69 | * the usage of 'using'. This should be done only for operator=.
|
---|
70 | */
|
---|
71 | template<typename OtherDerived>
|
---|
72 | EIGEN_STRONG_INLINE Array& operator=(const EigenBase<OtherDerived> &other)
|
---|
73 | {
|
---|
74 | return Base::operator=(other);
|
---|
75 | }
|
---|
76 |
|
---|
77 | /** Copies the value of the expression \a other into \c *this with automatic resizing.
|
---|
78 | *
|
---|
79 | * *this might be resized to match the dimensions of \a other. If *this was a null matrix (not already initialized),
|
---|
80 | * it will be initialized.
|
---|
81 | *
|
---|
82 | * Note that copying a row-vector into a vector (and conversely) is allowed.
|
---|
83 | * The resizing, if any, is then done in the appropriate way so that row-vectors
|
---|
84 | * remain row-vectors and vectors remain vectors.
|
---|
85 | */
|
---|
86 | template<typename OtherDerived>
|
---|
87 | EIGEN_STRONG_INLINE Array& operator=(const ArrayBase<OtherDerived>& other)
|
---|
88 | {
|
---|
89 | return Base::_set(other);
|
---|
90 | }
|
---|
91 |
|
---|
92 | /** This is a special case of the templated operator=. Its purpose is to
|
---|
93 | * prevent a default operator= from hiding the templated operator=.
|
---|
94 | */
|
---|
95 | EIGEN_STRONG_INLINE Array& operator=(const Array& other)
|
---|
96 | {
|
---|
97 | return Base::_set(other);
|
---|
98 | }
|
---|
99 |
|
---|
100 | /** Default constructor.
|
---|
101 | *
|
---|
102 | * For fixed-size matrices, does nothing.
|
---|
103 | *
|
---|
104 | * For dynamic-size matrices, creates an empty matrix of size 0. Does not allocate any array. Such a matrix
|
---|
105 | * is called a null matrix. This constructor is the unique way to create null matrices: resizing
|
---|
106 | * a matrix to 0 is not supported.
|
---|
107 | *
|
---|
108 | * \sa resize(Index,Index)
|
---|
109 | */
|
---|
110 | EIGEN_STRONG_INLINE Array() : Base()
|
---|
111 | {
|
---|
112 | Base::_check_template_params();
|
---|
113 | EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED
|
---|
114 | }
|
---|
115 |
|
---|
116 | #ifndef EIGEN_PARSED_BY_DOXYGEN
|
---|
117 | // FIXME is it still needed ??
|
---|
118 | /** \internal */
|
---|
119 | Array(internal::constructor_without_unaligned_array_assert)
|
---|
120 | : Base(internal::constructor_without_unaligned_array_assert())
|
---|
121 | {
|
---|
122 | Base::_check_template_params();
|
---|
123 | EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED
|
---|
124 | }
|
---|
125 | #endif
|
---|
126 |
|
---|
127 | #ifdef EIGEN_HAVE_RVALUE_REFERENCES
|
---|
128 | Array(Array&& other)
|
---|
129 | : Base(std::move(other))
|
---|
130 | {
|
---|
131 | Base::_check_template_params();
|
---|
132 | if (RowsAtCompileTime!=Dynamic && ColsAtCompileTime!=Dynamic)
|
---|
133 | Base::_set_noalias(other);
|
---|
134 | }
|
---|
135 | Array& operator=(Array&& other)
|
---|
136 | {
|
---|
137 | other.swap(*this);
|
---|
138 | return *this;
|
---|
139 | }
|
---|
140 | #endif
|
---|
141 |
|
---|
142 | /** Constructs a vector or row-vector with given dimension. \only_for_vectors
|
---|
143 | *
|
---|
144 | * Note that this is only useful for dynamic-size vectors. For fixed-size vectors,
|
---|
145 | * it is redundant to pass the dimension here, so it makes more sense to use the default
|
---|
146 | * constructor Matrix() instead.
|
---|
147 | */
|
---|
148 | EIGEN_STRONG_INLINE explicit Array(Index dim)
|
---|
149 | : Base(dim, RowsAtCompileTime == 1 ? 1 : dim, ColsAtCompileTime == 1 ? 1 : dim)
|
---|
150 | {
|
---|
151 | Base::_check_template_params();
|
---|
152 | EIGEN_STATIC_ASSERT_VECTOR_ONLY(Array)
|
---|
153 | eigen_assert(dim >= 0);
|
---|
154 | eigen_assert(SizeAtCompileTime == Dynamic || SizeAtCompileTime == dim);
|
---|
155 | EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED
|
---|
156 | }
|
---|
157 |
|
---|
158 | #ifndef EIGEN_PARSED_BY_DOXYGEN
|
---|
159 | template<typename T0, typename T1>
|
---|
160 | EIGEN_STRONG_INLINE Array(const T0& val0, const T1& val1)
|
---|
161 | {
|
---|
162 | Base::_check_template_params();
|
---|
163 | this->template _init2<T0,T1>(val0, val1);
|
---|
164 | }
|
---|
165 | #else
|
---|
166 | /** constructs an uninitialized matrix with \a rows rows and \a cols columns.
|
---|
167 | *
|
---|
168 | * This is useful for dynamic-size matrices. For fixed-size matrices,
|
---|
169 | * it is redundant to pass these parameters, so one should use the default constructor
|
---|
170 | * Matrix() instead. */
|
---|
171 | Array(Index rows, Index cols);
|
---|
172 | /** constructs an initialized 2D vector with given coefficients */
|
---|
173 | Array(const Scalar& val0, const Scalar& val1);
|
---|
174 | #endif
|
---|
175 |
|
---|
176 | /** constructs an initialized 3D vector with given coefficients */
|
---|
177 | EIGEN_STRONG_INLINE Array(const Scalar& val0, const Scalar& val1, const Scalar& val2)
|
---|
178 | {
|
---|
179 | Base::_check_template_params();
|
---|
180 | EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Array, 3)
|
---|
181 | m_storage.data()[0] = val0;
|
---|
182 | m_storage.data()[1] = val1;
|
---|
183 | m_storage.data()[2] = val2;
|
---|
184 | }
|
---|
185 | /** constructs an initialized 4D vector with given coefficients */
|
---|
186 | EIGEN_STRONG_INLINE Array(const Scalar& val0, const Scalar& val1, const Scalar& val2, const Scalar& val3)
|
---|
187 | {
|
---|
188 | Base::_check_template_params();
|
---|
189 | EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Array, 4)
|
---|
190 | m_storage.data()[0] = val0;
|
---|
191 | m_storage.data()[1] = val1;
|
---|
192 | m_storage.data()[2] = val2;
|
---|
193 | m_storage.data()[3] = val3;
|
---|
194 | }
|
---|
195 |
|
---|
196 | explicit Array(const Scalar *data);
|
---|
197 |
|
---|
198 | /** Constructor copying the value of the expression \a other */
|
---|
199 | template<typename OtherDerived>
|
---|
200 | EIGEN_STRONG_INLINE Array(const ArrayBase<OtherDerived>& other)
|
---|
201 | : Base(other.rows() * other.cols(), other.rows(), other.cols())
|
---|
202 | {
|
---|
203 | Base::_check_template_params();
|
---|
204 | Base::_set_noalias(other);
|
---|
205 | }
|
---|
206 | /** Copy constructor */
|
---|
207 | EIGEN_STRONG_INLINE Array(const Array& other)
|
---|
208 | : Base(other.rows() * other.cols(), other.rows(), other.cols())
|
---|
209 | {
|
---|
210 | Base::_check_template_params();
|
---|
211 | Base::_set_noalias(other);
|
---|
212 | }
|
---|
213 | /** Copy constructor with in-place evaluation */
|
---|
214 | template<typename OtherDerived>
|
---|
215 | EIGEN_STRONG_INLINE Array(const ReturnByValue<OtherDerived>& other)
|
---|
216 | {
|
---|
217 | Base::_check_template_params();
|
---|
218 | Base::resize(other.rows(), other.cols());
|
---|
219 | other.evalTo(*this);
|
---|
220 | }
|
---|
221 |
|
---|
222 | /** \sa MatrixBase::operator=(const EigenBase<OtherDerived>&) */
|
---|
223 | template<typename OtherDerived>
|
---|
224 | EIGEN_STRONG_INLINE Array(const EigenBase<OtherDerived> &other)
|
---|
225 | : Base(other.derived().rows() * other.derived().cols(), other.derived().rows(), other.derived().cols())
|
---|
226 | {
|
---|
227 | Base::_check_template_params();
|
---|
228 | Base::_resize_to_match(other);
|
---|
229 | *this = other;
|
---|
230 | }
|
---|
231 |
|
---|
232 | /** Override MatrixBase::swap() since for dynamic-sized matrices of same type it is enough to swap the
|
---|
233 | * data pointers.
|
---|
234 | */
|
---|
235 | template<typename OtherDerived>
|
---|
236 | void swap(ArrayBase<OtherDerived> const & other)
|
---|
237 | { this->_swap(other.derived()); }
|
---|
238 |
|
---|
239 | inline Index innerStride() const { return 1; }
|
---|
240 | inline Index outerStride() const { return this->innerSize(); }
|
---|
241 |
|
---|
242 | #ifdef EIGEN_ARRAY_PLUGIN
|
---|
243 | #include EIGEN_ARRAY_PLUGIN
|
---|
244 | #endif
|
---|
245 |
|
---|
246 | private:
|
---|
247 |
|
---|
248 | template<typename MatrixType, typename OtherDerived, bool SwapPointers>
|
---|
249 | friend struct internal::matrix_swap_impl;
|
---|
250 | };
|
---|
251 |
|
---|
252 | /** \defgroup arraytypedefs Global array typedefs
|
---|
253 | * \ingroup Core_Module
|
---|
254 | *
|
---|
255 | * Eigen defines several typedef shortcuts for most common 1D and 2D array types.
|
---|
256 | *
|
---|
257 | * The general patterns are the following:
|
---|
258 | *
|
---|
259 | * \c ArrayRowsColsType where \c Rows and \c Cols can be \c 2,\c 3,\c 4 for fixed size square matrices or \c X for dynamic size,
|
---|
260 | * and where \c Type can be \c i for integer, \c f for float, \c d for double, \c cf for complex float, \c cd
|
---|
261 | * for complex double.
|
---|
262 | *
|
---|
263 | * For example, \c Array33d is a fixed-size 3x3 array type of doubles, and \c ArrayXXf is a dynamic-size matrix of floats.
|
---|
264 | *
|
---|
265 | * There are also \c ArraySizeType which are self-explanatory. For example, \c Array4cf is
|
---|
266 | * a fixed-size 1D array of 4 complex floats.
|
---|
267 | *
|
---|
268 | * \sa class Array
|
---|
269 | */
|
---|
270 |
|
---|
271 | #define EIGEN_MAKE_ARRAY_TYPEDEFS(Type, TypeSuffix, Size, SizeSuffix) \
|
---|
272 | /** \ingroup arraytypedefs */ \
|
---|
273 | typedef Array<Type, Size, Size> Array##SizeSuffix##SizeSuffix##TypeSuffix; \
|
---|
274 | /** \ingroup arraytypedefs */ \
|
---|
275 | typedef Array<Type, Size, 1> Array##SizeSuffix##TypeSuffix;
|
---|
276 |
|
---|
277 | #define EIGEN_MAKE_ARRAY_FIXED_TYPEDEFS(Type, TypeSuffix, Size) \
|
---|
278 | /** \ingroup arraytypedefs */ \
|
---|
279 | typedef Array<Type, Size, Dynamic> Array##Size##X##TypeSuffix; \
|
---|
280 | /** \ingroup arraytypedefs */ \
|
---|
281 | typedef Array<Type, Dynamic, Size> Array##X##Size##TypeSuffix;
|
---|
282 |
|
---|
283 | #define EIGEN_MAKE_ARRAY_TYPEDEFS_ALL_SIZES(Type, TypeSuffix) \
|
---|
284 | EIGEN_MAKE_ARRAY_TYPEDEFS(Type, TypeSuffix, 2, 2) \
|
---|
285 | EIGEN_MAKE_ARRAY_TYPEDEFS(Type, TypeSuffix, 3, 3) \
|
---|
286 | EIGEN_MAKE_ARRAY_TYPEDEFS(Type, TypeSuffix, 4, 4) \
|
---|
287 | EIGEN_MAKE_ARRAY_TYPEDEFS(Type, TypeSuffix, Dynamic, X) \
|
---|
288 | EIGEN_MAKE_ARRAY_FIXED_TYPEDEFS(Type, TypeSuffix, 2) \
|
---|
289 | EIGEN_MAKE_ARRAY_FIXED_TYPEDEFS(Type, TypeSuffix, 3) \
|
---|
290 | EIGEN_MAKE_ARRAY_FIXED_TYPEDEFS(Type, TypeSuffix, 4)
|
---|
291 |
|
---|
292 | EIGEN_MAKE_ARRAY_TYPEDEFS_ALL_SIZES(int, i)
|
---|
293 | EIGEN_MAKE_ARRAY_TYPEDEFS_ALL_SIZES(float, f)
|
---|
294 | EIGEN_MAKE_ARRAY_TYPEDEFS_ALL_SIZES(double, d)
|
---|
295 | EIGEN_MAKE_ARRAY_TYPEDEFS_ALL_SIZES(std::complex<float>, cf)
|
---|
296 | EIGEN_MAKE_ARRAY_TYPEDEFS_ALL_SIZES(std::complex<double>, cd)
|
---|
297 |
|
---|
298 | #undef EIGEN_MAKE_ARRAY_TYPEDEFS_ALL_SIZES
|
---|
299 | #undef EIGEN_MAKE_ARRAY_TYPEDEFS
|
---|
300 |
|
---|
301 | #undef EIGEN_MAKE_ARRAY_TYPEDEFS_LARGE
|
---|
302 |
|
---|
303 | #define EIGEN_USING_ARRAY_TYPEDEFS_FOR_TYPE_AND_SIZE(TypeSuffix, SizeSuffix) \
|
---|
304 | using Eigen::Matrix##SizeSuffix##TypeSuffix; \
|
---|
305 | using Eigen::Vector##SizeSuffix##TypeSuffix; \
|
---|
306 | using Eigen::RowVector##SizeSuffix##TypeSuffix;
|
---|
307 |
|
---|
308 | #define EIGEN_USING_ARRAY_TYPEDEFS_FOR_TYPE(TypeSuffix) \
|
---|
309 | EIGEN_USING_ARRAY_TYPEDEFS_FOR_TYPE_AND_SIZE(TypeSuffix, 2) \
|
---|
310 | EIGEN_USING_ARRAY_TYPEDEFS_FOR_TYPE_AND_SIZE(TypeSuffix, 3) \
|
---|
311 | EIGEN_USING_ARRAY_TYPEDEFS_FOR_TYPE_AND_SIZE(TypeSuffix, 4) \
|
---|
312 | EIGEN_USING_ARRAY_TYPEDEFS_FOR_TYPE_AND_SIZE(TypeSuffix, X) \
|
---|
313 |
|
---|
314 | #define EIGEN_USING_ARRAY_TYPEDEFS \
|
---|
315 | EIGEN_USING_ARRAY_TYPEDEFS_FOR_TYPE(i) \
|
---|
316 | EIGEN_USING_ARRAY_TYPEDEFS_FOR_TYPE(f) \
|
---|
317 | EIGEN_USING_ARRAY_TYPEDEFS_FOR_TYPE(d) \
|
---|
318 | EIGEN_USING_ARRAY_TYPEDEFS_FOR_TYPE(cf) \
|
---|
319 | EIGEN_USING_ARRAY_TYPEDEFS_FOR_TYPE(cd)
|
---|
320 |
|
---|
321 | } // end namespace Eigen
|
---|
322 |
|
---|
323 | #endif // EIGEN_ARRAY_H
|
---|