• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // This Source Code Form is subject to the terms of the Mozilla
8 // Public License v. 2.0. If a copy of the MPL was not distributed
9 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 
11 #ifndef EIGEN_MATRIX_H
12 #define EIGEN_MATRIX_H
13 
14 namespace Eigen {
15 
16 /** \class Matrix
17   * \ingroup Core_Module
18   *
19   * \brief The matrix class, also used for vectors and row-vectors
20   *
21   * The %Matrix class is the work-horse for all \em dense (\ref dense "note") matrices and vectors within Eigen.
22   * Vectors are matrices with one column, and row-vectors are matrices with one row.
23   *
24   * The %Matrix class encompasses \em both fixed-size and dynamic-size objects (\ref fixedsize "note").
25   *
26   * The first three template parameters are required:
27   * \tparam _Scalar \anchor matrix_tparam_scalar Numeric type, e.g. float, double, int or std::complex<float>.
28   *                 User defined sclar types are supported as well (see \ref user_defined_scalars "here").
29   * \tparam _Rows Number of rows, or \b Dynamic
30   * \tparam _Cols Number of columns, or \b Dynamic
31   *
32   * The remaining template parameters are optional -- in most cases you don't have to worry about them.
33   * \tparam _Options \anchor matrix_tparam_options A combination of either \b #RowMajor or \b #ColMajor, and of either
34   *                 \b #AutoAlign or \b #DontAlign.
35   *                 The former controls \ref TopicStorageOrders "storage order", and defaults to column-major. The latter controls alignment, which is required
36   *                 for vectorization. It defaults to aligning matrices except for fixed sizes that aren't a multiple of the packet size.
37   * \tparam _MaxRows Maximum number of rows. Defaults to \a _Rows (\ref maxrows "note").
38   * \tparam _MaxCols Maximum number of columns. Defaults to \a _Cols (\ref maxrows "note").
39   *
40   * Eigen provides a number of typedefs covering the usual cases. Here are some examples:
41   *
42   * \li \c Matrix2d is a 2x2 square matrix of doubles (\c Matrix<double, 2, 2>)
43   * \li \c Vector4f is a vector of 4 floats (\c Matrix<float, 4, 1>)
44   * \li \c RowVector3i is a row-vector of 3 ints (\c Matrix<int, 1, 3>)
45   *
46   * \li \c MatrixXf is a dynamic-size matrix of floats (\c Matrix<float, Dynamic, Dynamic>)
47   * \li \c VectorXf is a dynamic-size vector of floats (\c Matrix<float, Dynamic, 1>)
48   *
49   * \li \c Matrix2Xf is a partially fixed-size (dynamic-size) matrix of floats (\c Matrix<float, 2, Dynamic>)
50   * \li \c MatrixX3d is a partially dynamic-size (fixed-size) matrix of double (\c Matrix<double, Dynamic, 3>)
51   *
52   * See \link matrixtypedefs this page \endlink for a complete list of predefined \em %Matrix and \em Vector typedefs.
53   *
54   * You can access elements of vectors and matrices using normal subscripting:
55   *
56   * \code
57   * Eigen::VectorXd v(10);
58   * v[0] = 0.1;
59   * v[1] = 0.2;
60   * v(0) = 0.3;
61   * v(1) = 0.4;
62   *
63   * Eigen::MatrixXi m(10, 10);
64   * m(0, 1) = 1;
65   * m(0, 2) = 2;
66   * m(0, 3) = 3;
67   * \endcode
68   *
69   * This class can be extended with the help of the plugin mechanism described on the page
70   * \ref TopicCustomizingEigen by defining the preprocessor symbol \c EIGEN_MATRIX_PLUGIN.
71   *
72   * <i><b>Some notes:</b></i>
73   *
74   * <dl>
75   * <dt><b>\anchor dense Dense versus sparse:</b></dt>
76   * <dd>This %Matrix class handles dense, not sparse matrices and vectors. For sparse matrices and vectors, see the Sparse module.
77   *
78   * Dense matrices and vectors are plain usual arrays of coefficients. All the coefficients are stored, in an ordinary contiguous array.
79   * This is unlike Sparse matrices and vectors where the coefficients are stored as a list of nonzero coefficients.</dd>
80   *
81   * <dt><b>\anchor fixedsize Fixed-size versus dynamic-size:</b></dt>
82   * <dd>Fixed-size means that the numbers of rows and columns are known are compile-time. In this case, Eigen allocates the array
83   * of coefficients as a fixed-size array, as a class member. This makes sense for very small matrices, typically up to 4x4, sometimes up
84   * to 16x16. Larger matrices should be declared as dynamic-size even if one happens to know their size at compile-time.
85   *
86   * Dynamic-size means that the numbers of rows or columns are not necessarily known at compile-time. In this case they are runtime
87   * variables, and the array of coefficients is allocated dynamically on the heap.
88   *
89   * Note that \em dense matrices, be they Fixed-size or Dynamic-size, <em>do not</em> expand dynamically in the sense of a std::map.
90   * If you want this behavior, see the Sparse module.</dd>
91   *
92   * <dt><b>\anchor maxrows _MaxRows and _MaxCols:</b></dt>
93   * <dd>In most cases, one just leaves these parameters to the default values.
94   * These parameters mean the maximum size of rows and columns that the matrix may have. They are useful in cases
95   * when the exact numbers of rows and columns are not known are compile-time, but it is known at compile-time that they cannot
96   * exceed a certain value. This happens when taking dynamic-size blocks inside fixed-size matrices: in this case _MaxRows and _MaxCols
97   * are the dimensions of the original matrix, while _Rows and _Cols are Dynamic.</dd>
98   * </dl>
99   *
100   * \see MatrixBase for the majority of the API methods for matrices, \ref TopicClassHierarchy,
101   * \ref TopicStorageOrders
102   */
103 
104 namespace internal {
105 template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
106 struct traits<Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> >
107 {
108   typedef _Scalar Scalar;
109   typedef Dense StorageKind;
110   typedef DenseIndex Index;
111   typedef MatrixXpr XprKind;
112   enum {
113     RowsAtCompileTime = _Rows,
114     ColsAtCompileTime = _Cols,
115     MaxRowsAtCompileTime = _MaxRows,
116     MaxColsAtCompileTime = _MaxCols,
117     Flags = compute_matrix_flags<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>::ret,
118     CoeffReadCost = NumTraits<Scalar>::ReadCost,
119     Options = _Options,
120     InnerStrideAtCompileTime = 1,
121     OuterStrideAtCompileTime = (Options&RowMajor) ? ColsAtCompileTime : RowsAtCompileTime
122   };
123 };
124 }
125 
126 template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
127 class Matrix
128   : public PlainObjectBase<Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> >
129 {
130   public:
131 
132     /** \brief Base class typedef.
133       * \sa PlainObjectBase
134       */
135     typedef PlainObjectBase<Matrix> Base;
136 
137     enum { Options = _Options };
138 
139     EIGEN_DENSE_PUBLIC_INTERFACE(Matrix)
140 
141     typedef typename Base::PlainObject PlainObject;
142 
143     using Base::base;
144     using Base::coeffRef;
145 
146     /**
147       * \brief Assigns matrices to each other.
148       *
149       * \note This is a special case of the templated operator=. Its purpose is
150       * to prevent a default operator= from hiding the templated operator=.
151       *
152       * \callgraph
153       */
154     EIGEN_STRONG_INLINE Matrix& operator=(const Matrix& other)
155     {
156       return Base::_set(other);
157     }
158 
159     /** \internal
160       * \brief Copies the value of the expression \a other into \c *this with automatic resizing.
161       *
162       * *this might be resized to match the dimensions of \a other. If *this was a null matrix (not already initialized),
163       * it will be initialized.
164       *
165       * Note that copying a row-vector into a vector (and conversely) is allowed.
166       * The resizing, if any, is then done in the appropriate way so that row-vectors
167       * remain row-vectors and vectors remain vectors.
168       */
169     template<typename OtherDerived>
170     EIGEN_STRONG_INLINE Matrix& operator=(const MatrixBase<OtherDerived>& other)
171     {
172       return Base::_set(other);
173     }
174 
175     /* Here, doxygen failed to copy the brief information when using \copydoc */
176 
177     /**
178       * \brief Copies the generic expression \a other into *this.
179       * \copydetails DenseBase::operator=(const EigenBase<OtherDerived> &other)
180       */
181     template<typename OtherDerived>
182     EIGEN_STRONG_INLINE Matrix& operator=(const EigenBase<OtherDerived> &other)
183     {
184       return Base::operator=(other);
185     }
186 
187     template<typename OtherDerived>
188     EIGEN_STRONG_INLINE Matrix& operator=(const ReturnByValue<OtherDerived>& func)
189     {
190       return Base::operator=(func);
191     }
192 
193     /** \brief Default constructor.
194       *
195       * For fixed-size matrices, does nothing.
196       *
197       * For dynamic-size matrices, creates an empty matrix of size 0. Does not allocate any array. Such a matrix
198       * is called a null matrix. This constructor is the unique way to create null matrices: resizing
199       * a matrix to 0 is not supported.
200       *
201       * \sa resize(Index,Index)
202       */
203     EIGEN_STRONG_INLINE Matrix() : Base()
204     {
205       Base::_check_template_params();
206       EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED
207     }
208 
209     // FIXME is it still needed
210     Matrix(internal::constructor_without_unaligned_array_assert)
211       : Base(internal::constructor_without_unaligned_array_assert())
212     { Base::_check_template_params(); EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED }
213 
214     /** \brief Constructs a vector or row-vector with given dimension. \only_for_vectors
215       *
216       * Note that this is only useful for dynamic-size vectors. For fixed-size vectors,
217       * it is redundant to pass the dimension here, so it makes more sense to use the default
218       * constructor Matrix() instead.
219       */
220     EIGEN_STRONG_INLINE explicit Matrix(Index dim)
221       : Base(dim, RowsAtCompileTime == 1 ? 1 : dim, ColsAtCompileTime == 1 ? 1 : dim)
222     {
223       Base::_check_template_params();
224       EIGEN_STATIC_ASSERT_VECTOR_ONLY(Matrix)
225       eigen_assert(dim >= 0);
226       eigen_assert(SizeAtCompileTime == Dynamic || SizeAtCompileTime == dim);
227       EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED
228     }
229 
230     #ifndef EIGEN_PARSED_BY_DOXYGEN
231     template<typename T0, typename T1>
232     EIGEN_STRONG_INLINE Matrix(const T0& x, const T1& y)
233     {
234       Base::_check_template_params();
235       Base::template _init2<T0,T1>(x, y);
236     }
237     #else
238     /** \brief Constructs an uninitialized matrix with \a rows rows and \a cols columns.
239       *
240       * This is useful for dynamic-size matrices. For fixed-size matrices,
241       * it is redundant to pass these parameters, so one should use the default constructor
242       * Matrix() instead. */
243     Matrix(Index rows, Index cols);
244     /** \brief Constructs an initialized 2D vector with given coefficients */
245     Matrix(const Scalar& x, const Scalar& y);
246     #endif
247 
248     /** \brief Constructs an initialized 3D vector with given coefficients */
249     EIGEN_STRONG_INLINE Matrix(const Scalar& x, const Scalar& y, const Scalar& z)
250     {
251       Base::_check_template_params();
252       EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Matrix, 3)
253       m_storage.data()[0] = x;
254       m_storage.data()[1] = y;
255       m_storage.data()[2] = z;
256     }
257     /** \brief Constructs an initialized 4D vector with given coefficients */
258     EIGEN_STRONG_INLINE Matrix(const Scalar& x, const Scalar& y, const Scalar& z, const Scalar& w)
259     {
260       Base::_check_template_params();
261       EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Matrix, 4)
262       m_storage.data()[0] = x;
263       m_storage.data()[1] = y;
264       m_storage.data()[2] = z;
265       m_storage.data()[3] = w;
266     }
267 
268     explicit Matrix(const Scalar *data);
269 
270     /** \brief Constructor copying the value of the expression \a other */
271     template<typename OtherDerived>
272     EIGEN_STRONG_INLINE Matrix(const MatrixBase<OtherDerived>& other)
273              : Base(other.rows() * other.cols(), other.rows(), other.cols())
274     {
275       // This test resides here, to bring the error messages closer to the user. Normally, these checks
276       // are performed deeply within the library, thus causing long and scary error traces.
277       EIGEN_STATIC_ASSERT((internal::is_same<Scalar, typename OtherDerived::Scalar>::value),
278         YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
279 
280       Base::_check_template_params();
281       Base::_set_noalias(other);
282     }
283     /** \brief Copy constructor */
284     EIGEN_STRONG_INLINE Matrix(const Matrix& other)
285             : Base(other.rows() * other.cols(), other.rows(), other.cols())
286     {
287       Base::_check_template_params();
288       Base::_set_noalias(other);
289     }
290     /** \brief Copy constructor with in-place evaluation */
291     template<typename OtherDerived>
292     EIGEN_STRONG_INLINE Matrix(const ReturnByValue<OtherDerived>& other)
293     {
294       Base::_check_template_params();
295       Base::resize(other.rows(), other.cols());
296       other.evalTo(*this);
297     }
298 
299     /** \brief Copy constructor for generic expressions.
300       * \sa MatrixBase::operator=(const EigenBase<OtherDerived>&)
301       */
302     template<typename OtherDerived>
303     EIGEN_STRONG_INLINE Matrix(const EigenBase<OtherDerived> &other)
304       : Base(other.derived().rows() * other.derived().cols(), other.derived().rows(), other.derived().cols())
305     {
306       Base::_check_template_params();
307       Base::_resize_to_match(other);
308       // FIXME/CHECK: isn't *this = other.derived() more efficient. it allows to
309       //              go for pure _set() implementations, right?
310       *this = other;
311     }
312 
313     /** \internal
314       * \brief Override MatrixBase::swap() since for dynamic-sized matrices
315       * of same type it is enough to swap the data pointers.
316       */
317     template<typename OtherDerived>
318     void swap(MatrixBase<OtherDerived> const & other)
319     { this->_swap(other.derived()); }
320 
321     inline Index innerStride() const { return 1; }
322     inline Index outerStride() const { return this->innerSize(); }
323 
324     /////////// Geometry module ///////////
325 
326     template<typename OtherDerived>
327     explicit Matrix(const RotationBase<OtherDerived,ColsAtCompileTime>& r);
328     template<typename OtherDerived>
329     Matrix& operator=(const RotationBase<OtherDerived,ColsAtCompileTime>& r);
330 
331     #ifdef EIGEN2_SUPPORT
332     template<typename OtherDerived>
333     explicit Matrix(const eigen2_RotationBase<OtherDerived,ColsAtCompileTime>& r);
334     template<typename OtherDerived>
335     Matrix& operator=(const eigen2_RotationBase<OtherDerived,ColsAtCompileTime>& r);
336     #endif
337 
338     // allow to extend Matrix outside Eigen
339     #ifdef EIGEN_MATRIX_PLUGIN
340     #include EIGEN_MATRIX_PLUGIN
341     #endif
342 
343   protected:
344     template <typename Derived, typename OtherDerived, bool IsVector>
345     friend struct internal::conservative_resize_like_impl;
346 
347     using Base::m_storage;
348 };
349 
350 /** \defgroup matrixtypedefs Global matrix typedefs
351   *
352   * \ingroup Core_Module
353   *
354   * Eigen defines several typedef shortcuts for most common matrix and vector types.
355   *
356   * The general patterns are the following:
357   *
358   * \c MatrixSizeType where \c Size can be \c 2,\c 3,\c 4 for fixed size square matrices or \c X for dynamic size,
359   * 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
360   * for complex double.
361   *
362   * For example, \c Matrix3d is a fixed-size 3x3 matrix type of doubles, and \c MatrixXf is a dynamic-size matrix of floats.
363   *
364   * There are also \c VectorSizeType and \c RowVectorSizeType which are self-explanatory. For example, \c Vector4cf is
365   * a fixed-size vector of 4 complex floats.
366   *
367   * \sa class Matrix
368   */
369 
370 #define EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, Size, SizeSuffix)   \
371 /** \ingroup matrixtypedefs */                                    \
372 typedef Matrix<Type, Size, Size> Matrix##SizeSuffix##TypeSuffix;  \
373 /** \ingroup matrixtypedefs */                                    \
374 typedef Matrix<Type, Size, 1>    Vector##SizeSuffix##TypeSuffix;  \
375 /** \ingroup matrixtypedefs */                                    \
376 typedef Matrix<Type, 1, Size>    RowVector##SizeSuffix##TypeSuffix;
377 
378 #define EIGEN_MAKE_FIXED_TYPEDEFS(Type, TypeSuffix, Size)         \
379 /** \ingroup matrixtypedefs */                                    \
380 typedef Matrix<Type, Size, Dynamic> Matrix##Size##X##TypeSuffix;  \
381 /** \ingroup matrixtypedefs */                                    \
382 typedef Matrix<Type, Dynamic, Size> Matrix##X##Size##TypeSuffix;
383 
384 #define EIGEN_MAKE_TYPEDEFS_ALL_SIZES(Type, TypeSuffix) \
385 EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 2, 2) \
386 EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 3, 3) \
387 EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 4, 4) \
388 EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, Dynamic, X) \
389 EIGEN_MAKE_FIXED_TYPEDEFS(Type, TypeSuffix, 2) \
390 EIGEN_MAKE_FIXED_TYPEDEFS(Type, TypeSuffix, 3) \
391 EIGEN_MAKE_FIXED_TYPEDEFS(Type, TypeSuffix, 4)
392 
393 EIGEN_MAKE_TYPEDEFS_ALL_SIZES(int,                  i)
394 EIGEN_MAKE_TYPEDEFS_ALL_SIZES(float,                f)
395 EIGEN_MAKE_TYPEDEFS_ALL_SIZES(double,               d)
396 EIGEN_MAKE_TYPEDEFS_ALL_SIZES(std::complex<float>,  cf)
397 EIGEN_MAKE_TYPEDEFS_ALL_SIZES(std::complex<double>, cd)
398 
399 #undef EIGEN_MAKE_TYPEDEFS_ALL_SIZES
400 #undef EIGEN_MAKE_TYPEDEFS
401 #undef EIGEN_MAKE_FIXED_TYPEDEFS
402 
403 } // end namespace Eigen
404 
405 #endif // EIGEN_MATRIX_H
406