Array.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) 2009 Gael Guennebaud <gael.guennebaud@inria.fr>
5 //
6 // Eigen is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 3 of the License, or (at your option) any later version.
10 //
11 // Alternatively, you can redistribute it and/or
12 // modify it under the terms of the GNU General Public License as
13 // published by the Free Software Foundation; either version 2 of
14 // the License, or (at your option) any later version.
15 //
16 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
17 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public
22 // License and a copy of the GNU General Public License along with
23 // Eigen. If not, see <http://www.gnu.org/licenses/>.
24 
25 #ifndef EIGEN_ARRAY_H
26 #define EIGEN_ARRAY_H
27 
28 namespace Eigen {
29 
47 namespace internal {
48 template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
49 struct traits<Array<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> > : traits<Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> >
50 {
51  typedef ArrayXpr XprKind;
52  typedef ArrayBase<Array<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> > XprBase;
53 };
54 }
55 
56 template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
57 class Array
58  : public PlainObjectBase<Array<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> >
59 {
60  public:
61 
64 
65  enum { Options = _Options };
66  typedef typename Base::PlainObject PlainObject;
67 
68  protected:
69  template <typename Derived, typename OtherDerived, bool IsVector>
70  friend struct internal::conservative_resize_like_impl;
71 
72  using Base::m_storage;
73 
74  public:
75 
76  using Base::base;
77  using Base::coeff;
78  using Base::coeffRef;
79 
86  template<typename OtherDerived>
88  {
89  return Base::operator=(other);
90  }
91 
101  template<typename OtherDerived>
103  {
104  return Base::_set(other);
105  }
106 
111  {
112  return Base::_set(other);
113  }
114 
126  {
127  Base::_check_template_params();
129  }
130 
131 #ifndef EIGEN_PARSED_BY_DOXYGEN
132  // FIXME is it still needed ??
134  Array(internal::constructor_without_unaligned_array_assert)
135  : Base(internal::constructor_without_unaligned_array_assert())
136  {
137  Base::_check_template_params();
139  }
140 #endif
141 
149  : Base(dim, RowsAtCompileTime == 1 ? 1 : dim, ColsAtCompileTime == 1 ? 1 : dim)
150  {
151  Base::_check_template_params();
153  eigen_assert(dim >= 0);
156  }
157 
158  #ifndef EIGEN_PARSED_BY_DOXYGEN
159  template<typename T0, typename T1>
160  EIGEN_STRONG_INLINE Array(const T0& x, const T1& y)
161  {
162  Base::_check_template_params();
163  this->template _init2<T0,T1>(x, y);
164  }
165  #else
166 
173  Array(const Scalar& x, const Scalar& y);
174  #endif
175 
177  EIGEN_STRONG_INLINE Array(const Scalar& x, const Scalar& y, const Scalar& z)
178  {
179  Base::_check_template_params();
181  m_storage.data()[0] = x;
182  m_storage.data()[1] = y;
183  m_storage.data()[2] = z;
184  }
186  EIGEN_STRONG_INLINE Array(const Scalar& x, const Scalar& y, const Scalar& z, const Scalar& w)
187  {
188  Base::_check_template_params();
190  m_storage.data()[0] = x;
191  m_storage.data()[1] = y;
192  m_storage.data()[2] = z;
193  m_storage.data()[3] = w;
194  }
195 
196  explicit Array(const Scalar *data);
197 
199  template<typename OtherDerived>
201  : Base(other.rows() * other.cols(), other.rows(), other.cols())
202  {
203  Base::_check_template_params();
204  Base::_set_noalias(other);
205  }
208  : Base(other.rows() * other.cols(), other.rows(), other.cols())
209  {
210  Base::_check_template_params();
211  Base::_set_noalias(other);
212  }
214  template<typename OtherDerived>
216  {
217  Base::_check_template_params();
218  Base::resize(other.rows(), other.cols());
219  other.evalTo(*this);
220  }
221 
223  template<typename OtherDerived>
225  : Base(other.derived().rows() * other.derived().cols(), other.derived().rows(), other.derived().cols())
226  {
227  Base::_check_template_params();
228  Base::resize(other.rows(), other.cols());
229  *this = other;
230  }
231 
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 
271 #define EIGEN_MAKE_ARRAY_TYPEDEFS(Type, TypeSuffix, Size, SizeSuffix) \
272  \
273 typedef Array<Type, Size, Size> Array##SizeSuffix##SizeSuffix##TypeSuffix; \
274  \
275 typedef Array<Type, Size, 1> Array##SizeSuffix##TypeSuffix;
276 
277 #define EIGEN_MAKE_ARRAY_FIXED_TYPEDEFS(Type, TypeSuffix, Size) \
278  \
279 typedef Array<Type, Size, Dynamic> Array##Size##X##TypeSuffix; \
280  \
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 
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