Matrix.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 // Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
6 //
7 // Eigen is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Lesser General Public
9 // License as published by the Free Software Foundation; either
10 // version 3 of the License, or (at your option) any later version.
11 //
12 // Alternatively, you can redistribute it and/or
13 // modify it under the terms of the GNU General Public License as
14 // published by the Free Software Foundation; either version 2 of
15 // the License, or (at your option) any later version.
16 //
17 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
20 // GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public
23 // License and a copy of the GNU General Public License along with
24 // Eigen. If not, see <http://www.gnu.org/licenses/>.
25 
26 #ifndef EIGEN_MATRIX_H
27 #define EIGEN_MATRIX_H
28 
29 namespace Eigen {
30 
119 namespace internal {
120 template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
121 struct traits<Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> >
122 {
123  typedef _Scalar Scalar;
124  typedef Dense StorageKind;
125  typedef DenseIndex Index;
126  typedef MatrixXpr XprKind;
127  enum {
128  RowsAtCompileTime = _Rows,
129  ColsAtCompileTime = _Cols,
130  MaxRowsAtCompileTime = _MaxRows,
131  MaxColsAtCompileTime = _MaxCols,
132  Flags = compute_matrix_flags<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>::ret,
133  CoeffReadCost = NumTraits<Scalar>::ReadCost,
134  Options = _Options,
135  InnerStrideAtCompileTime = 1,
136  OuterStrideAtCompileTime = (Options&RowMajor) ? ColsAtCompileTime : RowsAtCompileTime
137  };
138 };
139 }
140 
141 template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
142 class Matrix
143  : public PlainObjectBase<Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> >
144 {
145  public:
146 
151 
152  enum { Options = _Options };
153 
155 
156  typedef typename Base::PlainObject PlainObject;
157 
158  using Base::base;
159  using Base::coeffRef;
160 
169  EIGEN_STRONG_INLINE Matrix& operator=(const Matrix& other)
170  {
171  return Base::_set(other);
172  }
173 
184  template<typename OtherDerived>
186  {
187  return Base::_set(other);
188  }
189 
190  /* Here, doxygen failed to copy the brief information when using \copydoc */
191 
196  template<typename OtherDerived>
198  {
199  return Base::operator=(other);
200  }
201 
202  template<typename OtherDerived>
204  {
205  return Base::operator=(func);
206  }
207 
219  {
220  Base::_check_template_params();
222  }
223 
224  // FIXME is it still needed
225  Matrix(internal::constructor_without_unaligned_array_assert)
226  : Base(internal::constructor_without_unaligned_array_assert())
227  { Base::_check_template_params(); EIGEN_INITIALIZE_BY_ZERO_IF_THAT_OPTION_IS_ENABLED }
228 
236  : Base(dim, RowsAtCompileTime == 1 ? 1 : dim, ColsAtCompileTime == 1 ? 1 : dim)
237  {
238  Base::_check_template_params();
240  eigen_assert(dim >= 0);
241  eigen_assert(SizeAtCompileTime == Dynamic || SizeAtCompileTime == dim);
243  }
244 
245  #ifndef EIGEN_PARSED_BY_DOXYGEN
246  template<typename T0, typename T1>
247  EIGEN_STRONG_INLINE Matrix(const T0& x, const T1& y)
248  {
249  Base::_check_template_params();
250  Base::template _init2<T0,T1>(x, y);
251  }
252  #else
253 
258  Matrix(Index rows, Index cols);
260  Matrix(const Scalar& x, const Scalar& y);
261  #endif
262 
264  EIGEN_STRONG_INLINE Matrix(const Scalar& x, const Scalar& y, const Scalar& z)
265  {
266  Base::_check_template_params();
268  m_storage.data()[0] = x;
269  m_storage.data()[1] = y;
270  m_storage.data()[2] = z;
271  }
273  EIGEN_STRONG_INLINE Matrix(const Scalar& x, const Scalar& y, const Scalar& z, const Scalar& w)
274  {
275  Base::_check_template_params();
277  m_storage.data()[0] = x;
278  m_storage.data()[1] = y;
279  m_storage.data()[2] = z;
280  m_storage.data()[3] = w;
281  }
282 
283  explicit Matrix(const Scalar *data);
284 
286  template<typename OtherDerived>
288  : Base(other.rows() * other.cols(), other.rows(), other.cols())
289  {
290  // This test resides here, to bring the error messages closer to the user. Normally, these checks
291  // are performed deeply within the library, thus causing long and scary error traces.
292  EIGEN_STATIC_ASSERT((internal::is_same<Scalar, typename OtherDerived::Scalar>::value),
293  YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
294 
295  Base::_check_template_params();
296  Base::_set_noalias(other);
297  }
300  : Base(other.rows() * other.cols(), other.rows(), other.cols())
301  {
302  Base::_check_template_params();
303  Base::_set_noalias(other);
304  }
306  template<typename OtherDerived>
308  {
309  Base::_check_template_params();
310  Base::resize(other.rows(), other.cols());
311  other.evalTo(*this);
312  }
313 
317  template<typename OtherDerived>
319  : Base(other.derived().rows() * other.derived().cols(), other.derived().rows(), other.derived().cols())
320  {
321  Base::_check_template_params();
322  Base::resize(other.rows(), other.cols());
323  // FIXME/CHECK: isn't *this = other.derived() more efficient. it allows to
324  // go for pure _set() implementations, right?
325  *this = other;
326  }
327 
332  template<typename OtherDerived>
333  void swap(MatrixBase<OtherDerived> const & other)
334  { this->_swap(other.derived()); }
335 
336  inline Index innerStride() const { return 1; }
337  inline Index outerStride() const { return this->innerSize(); }
338 
340 
341  template<typename OtherDerived>
343  template<typename OtherDerived>
345 
346  #ifdef EIGEN2_SUPPORT
347  template<typename OtherDerived>
348  explicit Matrix(const eigen2_RotationBase<OtherDerived,ColsAtCompileTime>& r);
349  template<typename OtherDerived>
350  Matrix& operator=(const eigen2_RotationBase<OtherDerived,ColsAtCompileTime>& r);
351  #endif
352 
353  // allow to extend Matrix outside Eigen
354  #ifdef EIGEN_MATRIX_PLUGIN
355  #include EIGEN_MATRIX_PLUGIN
356  #endif
357 
358  protected:
359  template <typename Derived, typename OtherDerived, bool IsVector>
360  friend struct internal::conservative_resize_like_impl;
361 
362  using Base::m_storage;
363 };
364 
385 #define EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, Size, SizeSuffix) \
386  \
387 typedef Matrix<Type, Size, Size> Matrix##SizeSuffix##TypeSuffix; \
388  \
389 typedef Matrix<Type, Size, 1> Vector##SizeSuffix##TypeSuffix; \
390  \
391 typedef Matrix<Type, 1, Size> RowVector##SizeSuffix##TypeSuffix;
392 
393 #define EIGEN_MAKE_FIXED_TYPEDEFS(Type, TypeSuffix, Size) \
394  \
395 typedef Matrix<Type, Size, Dynamic> Matrix##Size##X##TypeSuffix; \
396  \
397 typedef Matrix<Type, Dynamic, Size> Matrix##X##Size##TypeSuffix;
398 
399 #define EIGEN_MAKE_TYPEDEFS_ALL_SIZES(Type, TypeSuffix) \
400 EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 2, 2) \
401 EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 3, 3) \
402 EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 4, 4) \
403 EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, Dynamic, X) \
404 EIGEN_MAKE_FIXED_TYPEDEFS(Type, TypeSuffix, 2) \
405 EIGEN_MAKE_FIXED_TYPEDEFS(Type, TypeSuffix, 3) \
406 EIGEN_MAKE_FIXED_TYPEDEFS(Type, TypeSuffix, 4)
407 
411 EIGEN_MAKE_TYPEDEFS_ALL_SIZES(std::complex<float>, cf)
412 EIGEN_MAKE_TYPEDEFS_ALL_SIZES(std::complex<double>, cd)
413 
414 #undef EIGEN_MAKE_TYPEDEFS_ALL_SIZES
415 #undef EIGEN_MAKE_TYPEDEFS
416 #undef EIGEN_MAKE_FIXED_TYPEDEFS
417 
418 } // end namespace Eigen
419 
420 #endif // EIGEN_MATRIX_H