Main MRPT website > C++ reference
MRPT logo

Matrix.h

Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2006-2010 Benoit Jacob <jacob.benoit.1@gmail.com>
00005 // Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
00006 //
00007 // Eigen is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU Lesser General Public
00009 // License as published by the Free Software Foundation; either
00010 // version 3 of the License, or (at your option) any later version.
00011 //
00012 // Alternatively, you can redistribute it and/or
00013 // modify it under the terms of the GNU General Public License as
00014 // published by the Free Software Foundation; either version 2 of
00015 // the License, or (at your option) any later version.
00016 //
00017 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00018 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00019 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00020 // GNU General Public License for more details.
00021 //
00022 // You should have received a copy of the GNU Lesser General Public
00023 // License and a copy of the GNU General Public License along with
00024 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00025 
00026 #ifndef EIGEN_MATRIX_H
00027 #define EIGEN_MATRIX_H
00028 
00029 /** \class Matrix
00030   * \ingroup Core_Module
00031   *
00032   * \brief The matrix class, also used for vectors and row-vectors
00033   *
00034   * The %Matrix class is the work-horse for all \em dense (\ref dense "note") matrices and vectors within Eigen.
00035   * Vectors are matrices with one column, and row-vectors are matrices with one row.
00036   *
00037   * The %Matrix class encompasses \em both fixed-size and dynamic-size objects (\ref fixedsize "note").
00038   *
00039   * The first three template parameters are required:
00040   * \tparam _Scalar \anchor matrix_tparam_scalar Numeric type, e.g. float, double, int or std::complex<float>.
00041   *                 User defined sclar types are supported as well (see \ref user_defined_scalars "here").
00042   * \tparam _Rows Number of rows, or \b Dynamic
00043   * \tparam _Cols Number of columns, or \b Dynamic
00044   *
00045   * The remaining template parameters are optional -- in most cases you don't have to worry about them.
00046   * \tparam _Options \anchor matrix_tparam_options A combination of either \b RowMajor or \b ColMajor, and of either
00047   *                 \b AutoAlign or \b DontAlign.
00048   *                 The former controls storage order, and defaults to column-major. The latter controls alignment, which is required
00049   *                 for vectorization. It defaults to aligning matrices except for fixed sizes that aren't a multiple of the packet size.
00050   * \tparam _MaxRows Maximum number of rows. Defaults to \a _Rows (\ref maxrows "note").
00051   * \tparam _MaxCols Maximum number of columns. Defaults to \a _Cols (\ref maxrows "note").
00052   *
00053   * Eigen provides a number of typedefs covering the usual cases. Here are some examples:
00054   *
00055   * \li \c Matrix2d is a 2x2 square matrix of doubles (\c Matrix<double, 2, 2>)
00056   * \li \c Vector4f is a vector of 4 floats (\c Matrix<float, 4, 1>)
00057   * \li \c RowVector3i is a row-vector of 3 ints (\c Matrix<int, 1, 3>)
00058   *
00059   * \li \c MatrixXf is a dynamic-size matrix of floats (\c Matrix<float, Dynamic, Dynamic>)
00060   * \li \c VectorXf is a dynamic-size vector of floats (\c Matrix<float, Dynamic, 1>)
00061   *
00062   * \li \c Matrix2Xf is a partially fixed-size (dynamic-size) matrix of floats (\c Matrix<float, 2, Dynamic>)
00063   * \li \c MatrixX3d is a partially dynamic-size (fixed-size) matrix of double (\c Matrix<double, Dynamic, 3>)
00064   *
00065   * See \link matrixtypedefs this page \endlink for a complete list of predefined \em %Matrix and \em Vector typedefs.
00066   *
00067   * You can access elements of vectors and matrices using normal subscripting:
00068   *
00069   * \code
00070   * Eigen::VectorXd v(10);
00071   * v[0] = 0.1;
00072   * v[1] = 0.2;
00073   * v(0) = 0.3;
00074   * v(1) = 0.4;
00075   *
00076   * Eigen::MatrixXi m(10, 10);
00077   * m(0, 1) = 1;
00078   * m(0, 2) = 2;
00079   * m(0, 3) = 3;
00080   * \endcode
00081   *
00082   * <i><b>Some notes:</b></i>
00083   *
00084   * <dl>
00085   * <dt><b>\anchor dense Dense versus sparse:</b></dt>
00086   * <dd>This %Matrix class handles dense, not sparse matrices and vectors. For sparse matrices and vectors, see the Sparse module.
00087   *
00088   * Dense matrices and vectors are plain usual arrays of coefficients. All the coefficients are stored, in an ordinary contiguous array.
00089   * This is unlike Sparse matrices and vectors where the coefficients are stored as a list of nonzero coefficients.</dd>
00090   *
00091   * <dt><b>\anchor fixedsize Fixed-size versus dynamic-size:</b></dt>
00092   * <dd>Fixed-size means that the numbers of rows and columns are known are compile-time. In this case, Eigen allocates the array
00093   * of coefficients as a fixed-size array, as a class member. This makes sense for very small matrices, typically up to 4x4, sometimes up
00094   * to 16x16. Larger matrices should be declared as dynamic-size even if one happens to know their size at compile-time.
00095   *
00096   * Dynamic-size means that the numbers of rows or columns are not necessarily known at compile-time. In this case they are runtime
00097   * variables, and the array of coefficients is allocated dynamically on the heap.
00098   *
00099   * 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.
00100   * If you want this behavior, see the Sparse module.</dd>
00101   *
00102   * <dt><b>\anchor maxrows _MaxRows and _MaxCols:</b></dt>
00103   * <dd>In most cases, one just leaves these parameters to the default values.
00104   * These parameters mean the maximum size of rows and columns that the matrix may have. They are useful in cases
00105   * when the exact numbers of rows and columns are not known are compile-time, but it is known at compile-time that they cannot
00106   * exceed a certain value. This happens when taking dynamic-size blocks inside fixed-size matrices: in this case _MaxRows and _MaxCols
00107   * are the dimensions of the original matrix, while _Rows and _Cols are Dynamic.</dd>
00108   * </dl>
00109   *
00110   * \see MatrixBase for the majority of the API methods for matrices, \ref TopicClassHierarchy
00111   */
00112 
00113 namespace internal {
00114 template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
00115 struct traits<Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> >
00116 {
00117   typedef _Scalar Scalar;
00118   typedef Dense StorageKind;
00119   typedef DenseIndex Index;
00120   typedef MatrixXpr XprKind;
00121   enum {
00122     RowsAtCompileTime = _Rows,
00123     ColsAtCompileTime = _Cols,
00124     MaxRowsAtCompileTime = _MaxRows,
00125     MaxColsAtCompileTime = _MaxCols,
00126     Flags = compute_matrix_flags<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>::ret,
00127     CoeffReadCost = NumTraits<Scalar>::ReadCost,
00128     Options = _Options,
00129     InnerStrideAtCompileTime = 1,
00130     OuterStrideAtCompileTime = (Options&RowMajor) ? ColsAtCompileTime : RowsAtCompileTime
00131   };
00132 };
00133 }
00134 
00135 template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
00136 class Matrix
00137   : public PlainObjectBase<Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> >
00138 {
00139   public:
00140 
00141     /** \brief Base class typedef.
00142       * \sa PlainObjectBase
00143       */
00144     typedef PlainObjectBase<Matrix> Base;
00145 
00146     enum { Options = _Options };
00147 
00148     EIGEN_DENSE_PUBLIC_INTERFACE(Matrix)
00149 
00150     typedef typename Base::PlainObject PlainObject;
00151 
00152     enum { NeedsToAlign = (!(Options&DontAlign))
00153                           && SizeAtCompileTime!=Dynamic && ((static_cast<int>(sizeof(Scalar))*SizeAtCompileTime)%16)==0 };
00154     EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)
00155 
00156     using Base::base;
00157     using Base::coeffRef;
00158 
00159     /**
00160       * \brief Assigns matrices to each other.
00161       *
00162       * \note This is a special case of the templated operator=. Its purpose is
00163       * to prevent a default operator= from hiding the templated operator=.
00164       *
00165       * \callgraph
00166       */
00167     EIGEN_STRONG_INLINE Matrix& operator=(const Matrix& other)
00168     {
00169       return Base::_set(other);
00170     }
00171 
00172     /** \internal
00173       * \brief Copies the value of the expression \a other into \c *this with automatic resizing.
00174       *
00175       * *this might be resized to match the dimensions of \a other. If *this was a null matrix (not already initialized),
00176       * it will be initialized.
00177       *
00178       * Note that copying a row-vector into a vector (and conversely) is allowed.
00179       * The resizing, if any, is then done in the appropriate way so that row-vectors
00180       * remain row-vectors and vectors remain vectors.
00181       */
00182     template<typename OtherDerived>
00183     EIGEN_STRONG_INLINE Matrix& operator=(const MatrixBase<OtherDerived>& other)
00184     {
00185       return Base::_set(other);
00186     }
00187 
00188     /* Here, doxygen failed to copy the brief information when using \copydoc */
00189 
00190     /**
00191       * \brief Copies the generic expression \a other into *this.
00192       * \copydetails DenseBase::operator=(const EigenBase<OtherDerived> &other)
00193       */
00194     template<typename OtherDerived>
00195     EIGEN_STRONG_INLINE Matrix& operator=(const EigenBase<OtherDerived> &other)
00196     {
00197       return Base::operator=(other);
00198     }
00199 
00200     template<typename OtherDerived>
00201     EIGEN_STRONG_INLINE Matrix& operator=(const ReturnByValue<OtherDerived>& func)
00202     {
00203       return Base::operator=(func);
00204     }
00205 
00206     /** \brief Default constructor.
00207       *
00208       * For fixed-size matrices, does nothing.
00209       *
00210       * For dynamic-size matrices, creates an empty matrix of size 0. Does not allocate any array. Such a matrix
00211       * is called a null matrix. This constructor is the unique way to create null matrices: resizing
00212       * a matrix to 0 is not supported.
00213       *
00214       * \sa resize(Index,Index)
00215       */
00216     EIGEN_STRONG_INLINE explicit Matrix() : Base()
00217     {
00218       Base::_check_template_params();
00219       EIGEN_INITIALIZE_BY_ZERO_IF_THAT_OPTION_IS_ENABLED
00220     }
00221 
00222     // FIXME is it still needed
00223     Matrix(internal::constructor_without_unaligned_array_assert)
00224       : Base(internal::constructor_without_unaligned_array_assert())
00225     { Base::_check_template_params(); EIGEN_INITIALIZE_BY_ZERO_IF_THAT_OPTION_IS_ENABLED }
00226 
00227     /** \brief Constructs a vector or row-vector with given dimension. \only_for_vectors
00228       *
00229       * Note that this is only useful for dynamic-size vectors. For fixed-size vectors,
00230       * it is redundant to pass the dimension here, so it makes more sense to use the default
00231       * constructor Matrix() instead.
00232       */
00233     EIGEN_STRONG_INLINE explicit Matrix(Index dim)
00234       : Base(dim, RowsAtCompileTime == 1 ? 1 : dim, ColsAtCompileTime == 1 ? 1 : dim)
00235     {
00236       Base::_check_template_params();
00237       EIGEN_STATIC_ASSERT_VECTOR_ONLY(Matrix)
00238       eigen_assert(dim > 0);
00239       eigen_assert(SizeAtCompileTime == Dynamic || SizeAtCompileTime == dim);
00240       EIGEN_INITIALIZE_BY_ZERO_IF_THAT_OPTION_IS_ENABLED
00241     }
00242 
00243     #ifndef EIGEN_PARSED_BY_DOXYGEN
00244     template<typename T0, typename T1>
00245     EIGEN_STRONG_INLINE Matrix(const T0& x, const T1& y)
00246     {
00247       Base::_check_template_params();
00248       Base::template _init2<T0,T1>(x, y);
00249     }
00250     #else
00251     /** \brief Constructs an uninitialized matrix with \a rows rows and \a cols columns.
00252       *
00253       * This is useful for dynamic-size matrices. For fixed-size matrices,
00254       * it is redundant to pass these parameters, so one should use the default constructor
00255       * Matrix() instead. */
00256     Matrix(Index rows, Index cols);
00257     /** \brief Constructs an initialized 2D vector with given coefficients */
00258     Matrix(const Scalar& x, const Scalar& y);
00259     #endif
00260 
00261     /** \brief Constructs an initialized 3D vector with given coefficients */
00262     EIGEN_STRONG_INLINE Matrix(const Scalar& x, const Scalar& y, const Scalar& z)
00263     {
00264       Base::_check_template_params();
00265       EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Matrix, 3)
00266       m_storage.data()[0] = x;
00267       m_storage.data()[1] = y;
00268       m_storage.data()[2] = z;
00269     }
00270     /** \brief Constructs an initialized 4D vector with given coefficients */
00271     EIGEN_STRONG_INLINE Matrix(const Scalar& x, const Scalar& y, const Scalar& z, const Scalar& w)
00272     {
00273       Base::_check_template_params();
00274       EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Matrix, 4)
00275       m_storage.data()[0] = x;
00276       m_storage.data()[1] = y;
00277       m_storage.data()[2] = z;
00278       m_storage.data()[3] = w;
00279     }
00280 
00281     explicit Matrix(const Scalar *data);
00282 
00283     /** \brief Constructor copying the value of the expression \a other */
00284     template<typename OtherDerived>
00285     EIGEN_STRONG_INLINE Matrix(const MatrixBase<OtherDerived>& other)
00286              : Base(other.rows() * other.cols(), other.rows(), other.cols())
00287     {
00288       Base::_check_template_params();
00289       Base::_set_noalias(other);
00290     }
00291     /** \brief Copy constructor */
00292     EIGEN_STRONG_INLINE Matrix(const Matrix& other)
00293             : Base(other.rows() * other.cols(), other.rows(), other.cols())
00294     {
00295       Base::_check_template_params();
00296       Base::_set_noalias(other);
00297     }
00298     /** \brief Copy constructor with in-place evaluation */
00299     template<typename OtherDerived>
00300     EIGEN_STRONG_INLINE Matrix(const ReturnByValue<OtherDerived>& other)
00301     {
00302       Base::_check_template_params();
00303       Base::resize(other.rows(), other.cols());
00304       other.evalTo(*this);
00305     }
00306 
00307     /** \brief Copy constructor for generic expressions.
00308       * \sa MatrixBase::operator=(const EigenBase<OtherDerived>&)
00309       */
00310     template<typename OtherDerived>
00311     EIGEN_STRONG_INLINE Matrix(const EigenBase<OtherDerived> &other)
00312       : Base(other.derived().rows() * other.derived().cols(), other.derived().rows(), other.derived().cols())
00313     {
00314       Base::_check_template_params();
00315       Base::resize(other.rows(), other.cols());
00316       // FIXME/CHECK: isn't *this = other.derived() more efficient. it allows to
00317       //              go for pure _set() implementations, right?
00318       *this = other;
00319     }
00320 
00321     /** \internal
00322       * \brief Override MatrixBase::swap() since for dynamic-sized matrices
00323       * of same type it is enough to swap the data pointers.
00324       */
00325     template<typename OtherDerived>
00326     void swap(MatrixBase<OtherDerived> const & other)
00327     { this->_swap(other.derived()); }
00328 
00329     inline Index innerStride() const { return 1; }
00330     inline Index outerStride() const { return this->innerSize(); }
00331 
00332     /////////// Geometry module ///////////
00333 
00334     template<typename OtherDerived>
00335     explicit Matrix(const RotationBase<OtherDerived,ColsAtCompileTime>& r);
00336     template<typename OtherDerived>
00337     Matrix& operator=(const RotationBase<OtherDerived,ColsAtCompileTime>& r);
00338 
00339     // allow to extend Matrix outside Eigen
00340     #ifdef EIGEN_MATRIX_PLUGIN
00341     #include EIGEN_MATRIX_PLUGIN
00342     #endif
00343 
00344   protected:
00345     template <typename Derived, typename OtherDerived, bool IsVector>
00346     friend struct internal::conservative_resize_like_impl;
00347 
00348     using Base::m_storage;
00349 };
00350 
00351 /** \defgroup matrixtypedefs Global matrix typedefs
00352   *
00353   * \ingroup Core_Module
00354   *
00355   * Eigen defines several typedef shortcuts for most common matrix and vector types.
00356   *
00357   * The general patterns are the following:
00358   *
00359   * \c MatrixSizeType where \c Size can be \c 2,\c 3,\c 4 for fixed size square matrices or \c X for dynamic size,
00360   * 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
00361   * for complex double.
00362   *
00363   * For example, \c Matrix3d is a fixed-size 3x3 matrix type of doubles, and \c MatrixXf is a dynamic-size matrix of floats.
00364   *
00365   * There are also \c VectorSizeType and \c RowVectorSizeType which are self-explanatory. For example, \c Vector4cf is
00366   * a fixed-size vector of 4 complex floats.
00367   *
00368   * \sa class Matrix
00369   */
00370 
00371 #define EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, Size, SizeSuffix)   \
00372 /** \ingroup matrixtypedefs */                                    \
00373 typedef Matrix<Type, Size, Size> Matrix##SizeSuffix##TypeSuffix;  \
00374 /** \ingroup matrixtypedefs */                                    \
00375 typedef Matrix<Type, Size, 1>    Vector##SizeSuffix##TypeSuffix;  \
00376 /** \ingroup matrixtypedefs */                                    \
00377 typedef Matrix<Type, 1, Size>    RowVector##SizeSuffix##TypeSuffix;
00378 
00379 #define EIGEN_MAKE_FIXED_TYPEDEFS(Type, TypeSuffix, Size)         \
00380 /** \ingroup matrixtypedefs */                                    \
00381 typedef Matrix<Type, Size, Dynamic> Matrix##Size##X##TypeSuffix;  \
00382 /** \ingroup matrixtypedefs */                                    \
00383 typedef Matrix<Type, Dynamic, Size> Matrix##X##Size##TypeSuffix;
00384 
00385 #define EIGEN_MAKE_TYPEDEFS_ALL_SIZES(Type, TypeSuffix) \
00386 EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 2, 2) \
00387 EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 3, 3) \
00388 EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 4, 4) \
00389 EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, Dynamic, X) \
00390 EIGEN_MAKE_FIXED_TYPEDEFS(Type, TypeSuffix, 2) \
00391 EIGEN_MAKE_FIXED_TYPEDEFS(Type, TypeSuffix, 3) \
00392 EIGEN_MAKE_FIXED_TYPEDEFS(Type, TypeSuffix, 4)
00393 
00394 EIGEN_MAKE_TYPEDEFS_ALL_SIZES(int,                  i)
00395 EIGEN_MAKE_TYPEDEFS_ALL_SIZES(float,                f)
00396 EIGEN_MAKE_TYPEDEFS_ALL_SIZES(double,               d)
00397 EIGEN_MAKE_TYPEDEFS_ALL_SIZES(std::complex<float>,  cf)
00398 EIGEN_MAKE_TYPEDEFS_ALL_SIZES(std::complex<double>, cd)
00399 
00400 #undef EIGEN_MAKE_TYPEDEFS_ALL_SIZES
00401 #undef EIGEN_MAKE_TYPEDEFS
00402 
00403 #undef EIGEN_MAKE_TYPEDEFS_LARGE
00404 
00405 #define EIGEN_USING_MATRIX_TYPEDEFS_FOR_TYPE_AND_SIZE(TypeSuffix, SizeSuffix) \
00406 using Eigen::Matrix##SizeSuffix##TypeSuffix; \
00407 using Eigen::Vector##SizeSuffix##TypeSuffix; \
00408 using Eigen::RowVector##SizeSuffix##TypeSuffix;
00409 
00410 #define EIGEN_USING_MATRIX_TYPEDEFS_FOR_TYPE(TypeSuffix) \
00411 EIGEN_USING_MATRIX_TYPEDEFS_FOR_TYPE_AND_SIZE(TypeSuffix, 2) \
00412 EIGEN_USING_MATRIX_TYPEDEFS_FOR_TYPE_AND_SIZE(TypeSuffix, 3) \
00413 EIGEN_USING_MATRIX_TYPEDEFS_FOR_TYPE_AND_SIZE(TypeSuffix, 4) \
00414 EIGEN_USING_MATRIX_TYPEDEFS_FOR_TYPE_AND_SIZE(TypeSuffix, X) \
00415 
00416 #define EIGEN_USING_MATRIX_TYPEDEFS \
00417 EIGEN_USING_MATRIX_TYPEDEFS_FOR_TYPE(i) \
00418 EIGEN_USING_MATRIX_TYPEDEFS_FOR_TYPE(f) \
00419 EIGEN_USING_MATRIX_TYPEDEFS_FOR_TYPE(d) \
00420 EIGEN_USING_MATRIX_TYPEDEFS_FOR_TYPE(cf) \
00421 EIGEN_USING_MATRIX_TYPEDEFS_FOR_TYPE(cd)
00422 
00423 #endif // EIGEN_MATRIX_H



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN:exported at Tue Jan 25 21:56:31 UTC 2011