Main MRPT website > C++ reference
MRPT logo

MatrixBase.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-2009 Benoit Jacob <jacob.benoit.1@gmail.com>
00005 // Copyright (C) 2008 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_MATRIXBASE_H
00027 #define EIGEN_MATRIXBASE_H
00028 
00029 /** \class MatrixBase
00030   * \ingroup Core_Module
00031   *
00032   * \brief Base class for all dense matrices, vectors, and expressions
00033   *
00034   * This class is the base that is inherited by all matrix, vector, and related expression
00035   * types. Most of the Eigen API is contained in this class, and its base classes. Other important
00036   * classes for the Eigen API are Matrix, and VectorwiseOp.
00037   *
00038   * Note that some methods are defined in other modules such as the \ref LU_Module LU module
00039   * for all functions related to matrix inversions.
00040   *
00041   * \param Derived is the derived type, e.g. a matrix type, or an expression, etc.
00042   *
00043   * When writing a function taking Eigen objects as argument, if you want your function
00044   * to take as argument any matrix, vector, or expression, just let it take a
00045   * MatrixBase argument. As an example, here is a function printFirstRow which, given
00046   * a matrix, vector, or expression \a x, prints the first row of \a x.
00047   *
00048   * \code
00049     template<typename Derived>
00050     void printFirstRow(const Eigen::MatrixBase<Derived>& x)
00051     {
00052       cout << x.row(0) << endl;
00053     }
00054   * \endcode
00055   *
00056   * \sa \ref TopicClassHierarchy
00057   */
00058 template<typename Derived> class MatrixBase
00059   : public DenseBase<Derived>
00060 {
00061   public:
00062 #ifndef EIGEN_PARSED_BY_DOXYGEN
00063     typedef MatrixBase StorageBaseType;
00064     typedef typename internal::traits<Derived>::StorageKind StorageKind;
00065     typedef typename internal::traits<Derived>::Index Index;
00066     typedef typename internal::traits<Derived>::Scalar Scalar;
00067     typedef typename internal::packet_traits<Scalar>::type PacketScalar;
00068     typedef typename NumTraits<Scalar>::Real RealScalar;
00069 
00070     typedef DenseBase<Derived> Base;
00071     using Base::RowsAtCompileTime;
00072     using Base::ColsAtCompileTime;
00073     using Base::SizeAtCompileTime;
00074     using Base::MaxRowsAtCompileTime;
00075     using Base::MaxColsAtCompileTime;
00076     using Base::MaxSizeAtCompileTime;
00077     using Base::IsVectorAtCompileTime;
00078     using Base::Flags;
00079     using Base::CoeffReadCost;
00080 
00081     using Base::derived;
00082     using Base::const_cast_derived;
00083     using Base::rows;
00084     using Base::cols;
00085     using Base::size;
00086     using Base::coeff;
00087     using Base::coeffRef;
00088     using Base::lazyAssign;
00089     using Base::eval;
00090     using Base::operator+=;
00091     using Base::operator-=;
00092     using Base::operator*=;
00093     using Base::operator/=;
00094 
00095     typedef typename Base::CoeffReturnType CoeffReturnType;
00096     typedef typename Base::ConstTransposeReturnType ConstTransposeReturnType;
00097     typedef typename Base::RowXpr RowXpr;
00098     typedef typename Base::ColXpr ColXpr;
00099 #endif // not EIGEN_PARSED_BY_DOXYGEN
00100 
00101 
00102 
00103 #ifndef EIGEN_PARSED_BY_DOXYGEN
00104     /** type of the equivalent square matrix */
00105     typedef Matrix<Scalar,EIGEN_SIZE_MAX(RowsAtCompileTime,ColsAtCompileTime),
00106                           EIGEN_SIZE_MAX(RowsAtCompileTime,ColsAtCompileTime)> SquareMatrixType;
00107 #endif // not EIGEN_PARSED_BY_DOXYGEN
00108 
00109     /** \returns the size of the main diagonal, which is min(rows(),cols()).
00110       * \sa rows(), cols(), SizeAtCompileTime. */
00111     inline Index diagonalSize() const { return std::min(rows(),cols()); }
00112 
00113     /** \brief The plain matrix type corresponding to this expression.
00114       *
00115       * This is not necessarily exactly the return type of eval(). In the case of plain matrices,
00116       * the return type of eval() is a const reference to a matrix, not a matrix! It is however guaranteed
00117       * that the return type of eval() is either PlainObject or const PlainObject&.
00118       */
00119     typedef Matrix<typename internal::traits<Derived>::Scalar,
00120                 internal::traits<Derived>::RowsAtCompileTime,
00121                 internal::traits<Derived>::ColsAtCompileTime,
00122                 AutoAlign | (internal::traits<Derived>::Flags&RowMajorBit ? RowMajor : ColMajor),
00123                 internal::traits<Derived>::MaxRowsAtCompileTime,
00124                 internal::traits<Derived>::MaxColsAtCompileTime
00125           > PlainObject;
00126 
00127 #ifndef EIGEN_PARSED_BY_DOXYGEN
00128     /** \internal Represents a matrix with all coefficients equal to one another*/
00129     typedef CwiseNullaryOp<internal::scalar_constant_op<Scalar>,Derived> ConstantReturnType;
00130     /** \internal the return type of MatrixBase::adjoint() */
00131     typedef typename internal::conditional<NumTraits<Scalar>::IsComplex,
00132                         CwiseUnaryOp<internal::scalar_conjugate_op<Scalar>, ConstTransposeReturnType>,
00133                         ConstTransposeReturnType
00134                      >::type AdjointReturnType;
00135     /** \internal Return type of eigenvalues() */
00136     typedef Matrix<std::complex<RealScalar>, internal::traits<Derived>::ColsAtCompileTime, 1, ColMajor> EigenvaluesReturnType;
00137     /** \internal the return type of identity */
00138     typedef CwiseNullaryOp<internal::scalar_identity_op<Scalar>,Derived> IdentityReturnType;
00139     /** \internal the return type of unit vectors */
00140     typedef Block<CwiseNullaryOp<internal::scalar_identity_op<Scalar>, SquareMatrixType>,
00141                   internal::traits<Derived>::RowsAtCompileTime,
00142                   internal::traits<Derived>::ColsAtCompileTime> BasisReturnType;
00143 #endif // not EIGEN_PARSED_BY_DOXYGEN
00144 
00145 #define EIGEN_CURRENT_STORAGE_BASE_CLASS Eigen::MatrixBase
00146 #   include "../plugins/CommonCwiseUnaryOps.h"
00147 #   include "../plugins/CommonCwiseBinaryOps.h"
00148 #   include "../plugins/MatrixCwiseUnaryOps.h"
00149 #   include "../plugins/MatrixCwiseBinaryOps.h"
00150 #   ifdef EIGEN_MATRIXBASE_PLUGIN
00151 #     include EIGEN_MATRIXBASE_PLUGIN
00152 #   endif
00153 #undef EIGEN_CURRENT_STORAGE_BASE_CLASS
00154 
00155     /** Special case of the template operator=, in order to prevent the compiler
00156       * from generating a default operator= (issue hit with g++ 4.1)
00157       */
00158     Derived& operator=(const MatrixBase& other);
00159 
00160     // We cannot inherit here via Base::operator= since it is causing
00161     // trouble with MSVC.
00162 
00163     template <typename OtherDerived>
00164     Derived& operator=(const DenseBase<OtherDerived>& other);
00165 
00166     template <typename OtherDerived>
00167     Derived& operator=(const EigenBase<OtherDerived>& other);
00168 
00169     template<typename OtherDerived>
00170     Derived& operator=(const ReturnByValue<OtherDerived>& other);
00171 
00172 #ifndef EIGEN_PARSED_BY_DOXYGEN
00173     template<typename ProductDerived, typename Lhs, typename Rhs>
00174     Derived& lazyAssign(const ProductBase<ProductDerived, Lhs,Rhs>& other);
00175 #endif // not EIGEN_PARSED_BY_DOXYGEN
00176 
00177     template<typename OtherDerived>
00178     Derived& operator+=(const MatrixBase<OtherDerived>& other);
00179     template<typename OtherDerived>
00180     Derived& operator-=(const MatrixBase<OtherDerived>& other);
00181 
00182     template<typename OtherDerived>
00183     const typename ProductReturnType<Derived,OtherDerived>::Type
00184     operator*(const MatrixBase<OtherDerived> &other) const;
00185 
00186     template<typename OtherDerived>
00187     const typename LazyProductReturnType<Derived,OtherDerived>::Type
00188     lazyProduct(const MatrixBase<OtherDerived> &other) const;
00189 
00190     template<typename OtherDerived>
00191     Derived& operator*=(const EigenBase<OtherDerived>& other);
00192 
00193     template<typename OtherDerived>
00194     void applyOnTheLeft(const EigenBase<OtherDerived>& other);
00195 
00196     template<typename OtherDerived>
00197     void applyOnTheRight(const EigenBase<OtherDerived>& other);
00198 
00199     template<typename DiagonalDerived>
00200     const DiagonalProduct<Derived, DiagonalDerived, OnTheRight>
00201     operator*(const DiagonalBase<DiagonalDerived> &diagonal) const;
00202 
00203     template<typename OtherDerived>
00204     Scalar dot(const MatrixBase<OtherDerived>& other) const;
00205     RealScalar squaredNorm() const;
00206     RealScalar norm() const;
00207     RealScalar stableNorm() const;
00208     RealScalar blueNorm() const;
00209     RealScalar hypotNorm() const;
00210     const PlainObject normalized() const;
00211     void normalize();
00212 
00213     const AdjointReturnType adjoint() const;
00214     void adjointInPlace();
00215 
00216     typedef Diagonal<Derived> DiagonalReturnType;
00217     DiagonalReturnType diagonal();
00218     typedef const Diagonal<const Derived> ConstDiagonalReturnType;
00219     ConstDiagonalReturnType diagonal() const;
00220 
00221     template<int Index> struct DiagonalIndexReturnType { typedef Diagonal<Derived,Index> Type; };
00222     template<int Index> struct ConstDiagonalIndexReturnType { typedef const Diagonal<const Derived,Index> Type; };
00223 
00224     template<int Index> typename DiagonalIndexReturnType<Index>::Type diagonal();
00225     template<int Index> typename ConstDiagonalIndexReturnType<Index>::Type diagonal() const;
00226 
00227     // Note: The "MatrixBase::" prefixes are added to help MSVC9 to match these declarations with the later implementations.
00228     typename MatrixBase::template DiagonalIndexReturnType<Dynamic>::Type diagonal(Index index);
00229     typename MatrixBase::template ConstDiagonalIndexReturnType<Dynamic>::Type diagonal(Index index) const;
00230 
00231     template<unsigned int Mode> TriangularView<Derived, Mode> part();
00232     template<unsigned int Mode> const TriangularView<Derived, Mode> part() const;
00233 
00234     template<unsigned int Mode> struct TriangularViewReturnType { typedef TriangularView<Derived, Mode> Type; };
00235     template<unsigned int Mode> struct ConstTriangularViewReturnType { typedef const TriangularView<const Derived, Mode> Type; };
00236 
00237     template<unsigned int Mode> typename TriangularViewReturnType<Mode>::Type triangularView();
00238     template<unsigned int Mode> typename ConstTriangularViewReturnType<Mode>::Type triangularView() const;
00239 
00240     template<unsigned int UpLo> struct SelfAdjointViewReturnType { typedef SelfAdjointView<Derived, UpLo> Type; };
00241     template<unsigned int UpLo> struct ConstSelfAdjointViewReturnType { typedef const SelfAdjointView<const Derived, UpLo> Type; };
00242 
00243     template<unsigned int UpLo> typename SelfAdjointViewReturnType<UpLo>::Type selfadjointView();
00244     template<unsigned int UpLo> typename ConstSelfAdjointViewReturnType<UpLo>::Type selfadjointView() const;
00245 
00246     const SparseView<Derived> sparseView(const Scalar& m_reference = Scalar(0),
00247                                          typename NumTraits<Scalar>::Real m_epsilon = NumTraits<Scalar>::dummy_precision()) const;
00248     static const IdentityReturnType Identity();
00249     static const IdentityReturnType Identity(Index rows, Index cols);
00250     static const BasisReturnType Unit(Index size, Index i);
00251     static const BasisReturnType Unit(Index i);
00252     static const BasisReturnType UnitX();
00253     static const BasisReturnType UnitY();
00254     static const BasisReturnType UnitZ();
00255     static const BasisReturnType UnitW();
00256 
00257     const DiagonalWrapper<Derived> asDiagonal() const;
00258 
00259     Derived& setIdentity();
00260     Derived& setIdentity(Index rows, Index cols);
00261 
00262     bool isIdentity(RealScalar prec = NumTraits<Scalar>::dummy_precision()) const;
00263     bool isDiagonal(RealScalar prec = NumTraits<Scalar>::dummy_precision()) const;
00264 
00265     bool isUpperTriangular(RealScalar prec = NumTraits<Scalar>::dummy_precision()) const;
00266     bool isLowerTriangular(RealScalar prec = NumTraits<Scalar>::dummy_precision()) const;
00267 
00268     template<typename OtherDerived>
00269     bool isOrthogonal(const MatrixBase<OtherDerived>& other,
00270                       RealScalar prec = NumTraits<Scalar>::dummy_precision()) const;
00271     bool isUnitary(RealScalar prec = NumTraits<Scalar>::dummy_precision()) const;
00272 
00273     /** \returns true if each coefficients of \c *this and \a other are all exactly equal.
00274       * \warning When using floating point scalar values you probably should rather use a
00275       *          fuzzy comparison such as isApprox()
00276       * \sa isApprox(), operator!= */
00277     template<typename OtherDerived>
00278     inline bool operator==(const MatrixBase<OtherDerived>& other) const
00279     { return cwiseEqual(other).all(); }
00280 
00281     /** \returns true if at least one pair of coefficients of \c *this and \a other are not exactly equal to each other.
00282       * \warning When using floating point scalar values you probably should rather use a
00283       *          fuzzy comparison such as isApprox()
00284       * \sa isApprox(), operator== */
00285     template<typename OtherDerived>
00286     inline bool operator!=(const MatrixBase<OtherDerived>& other) const
00287     { return cwiseNotEqual(other).any(); }
00288 
00289     NoAlias<Derived,Eigen::MatrixBase > noalias();
00290 
00291     inline const ForceAlignedAccess<Derived> forceAlignedAccess() const;
00292     inline ForceAlignedAccess<Derived> forceAlignedAccess();
00293     template<bool Enable> inline typename internal::add_const_on_value_type<typename internal::conditional<Enable,ForceAlignedAccess<Derived>,Derived&>::type>::type forceAlignedAccessIf() const;
00294     template<bool Enable> inline typename internal::conditional<Enable,ForceAlignedAccess<Derived>,Derived&>::type forceAlignedAccessIf();
00295 
00296     Scalar trace() const;
00297 
00298 /////////// Array module ///////////
00299 
00300     template<int p> RealScalar lpNorm() const;
00301 
00302     MatrixBase<Derived>& matrix() { return *this; }
00303     const MatrixBase<Derived>& matrix() const { return *this; }
00304 
00305     /** \returns an \link ArrayBase Array \endlink expression of this matrix
00306       * \sa ArrayBase::matrix() */
00307     ArrayWrapper<Derived> array() { return derived(); }
00308     const ArrayWrapper<Derived> array() const { return derived(); }
00309 
00310 /////////// LU module ///////////
00311 
00312     const FullPivLU<PlainObject> fullPivLu() const;
00313     const PartialPivLU<PlainObject> partialPivLu() const;
00314     const PartialPivLU<PlainObject> lu() const;
00315     const internal::inverse_impl<Derived> inverse() const;
00316     template<typename ResultType>
00317     void computeInverseAndDetWithCheck(
00318       ResultType& inverse,
00319       typename ResultType::Scalar& determinant,
00320       bool& invertible,
00321       const RealScalar& absDeterminantThreshold = NumTraits<Scalar>::dummy_precision()
00322     ) const;
00323     template<typename ResultType>
00324     void computeInverseWithCheck(
00325       ResultType& inverse,
00326       bool& invertible,
00327       const RealScalar& absDeterminantThreshold = NumTraits<Scalar>::dummy_precision()
00328     ) const;
00329     Scalar determinant() const;
00330 
00331 /////////// Cholesky module ///////////
00332 
00333     const LLT<PlainObject>  llt() const;
00334     const LDLT<PlainObject> ldlt() const;
00335 
00336 /////////// QR module ///////////
00337 
00338     const HouseholderQR<PlainObject> householderQr() const;
00339     const ColPivHouseholderQR<PlainObject> colPivHouseholderQr() const;
00340     const FullPivHouseholderQR<PlainObject> fullPivHouseholderQr() const;
00341 
00342     EigenvaluesReturnType eigenvalues() const;
00343     RealScalar operatorNorm() const;
00344 
00345 /////////// SVD module ///////////
00346 
00347     JacobiSVD<PlainObject> jacobiSvd(unsigned int computationOptions = 0) const;
00348 
00349 /////////// Geometry module ///////////
00350 
00351     template<typename OtherDerived>
00352     PlainObject cross(const MatrixBase<OtherDerived>& other) const;
00353     template<typename OtherDerived>
00354     PlainObject cross3(const MatrixBase<OtherDerived>& other) const;
00355     PlainObject unitOrthogonal(void) const;
00356     Matrix<Scalar,3,1> eulerAngles(Index a0, Index a1, Index a2) const;
00357     ScalarMultipleReturnType operator*(const UniformScaling<Scalar>& s) const;
00358     enum {
00359       SizeMinusOne = SizeAtCompileTime==Dynamic ? Dynamic : SizeAtCompileTime-1
00360     };
00361     typedef Block<const Derived,
00362                   internal::traits<Derived>::ColsAtCompileTime==1 ? SizeMinusOne : 1,
00363                   internal::traits<Derived>::ColsAtCompileTime==1 ? 1 : SizeMinusOne> ConstStartMinusOne;
00364     typedef CwiseUnaryOp<internal::scalar_quotient1_op<typename internal::traits<Derived>::Scalar>,
00365                 ConstStartMinusOne > HNormalizedReturnType;
00366 
00367     const HNormalizedReturnType hnormalized() const;
00368 
00369     // put this as separate enum value to work around possible GCC 4.3 bug (?)
00370     enum { HomogeneousReturnTypeDirection = ColsAtCompileTime==1?Vertical:Horizontal };
00371     typedef Homogeneous<Derived, HomogeneousReturnTypeDirection> HomogeneousReturnType;
00372 
00373     HomogeneousReturnType homogeneous() const;
00374 
00375 ////////// Householder module ///////////
00376 
00377     void makeHouseholderInPlace(Scalar& tau, RealScalar& beta);
00378     template<typename EssentialPart>
00379     void makeHouseholder(EssentialPart& essential,
00380                          Scalar& tau, RealScalar& beta) const;
00381     template<typename EssentialPart>
00382     void applyHouseholderOnTheLeft(const EssentialPart& essential,
00383                                    const Scalar& tau,
00384                                    Scalar* workspace);
00385     template<typename EssentialPart>
00386     void applyHouseholderOnTheRight(const EssentialPart& essential,
00387                                     const Scalar& tau,
00388                                     Scalar* workspace);
00389 
00390 ///////// Jacobi module /////////
00391 
00392     template<typename OtherScalar>
00393     void applyOnTheLeft(Index p, Index q, const JacobiRotation<OtherScalar>& j);
00394     template<typename OtherScalar>
00395     void applyOnTheRight(Index p, Index q, const JacobiRotation<OtherScalar>& j);
00396 
00397 ///////// MatrixFunctions module /////////
00398 
00399     typedef typename internal::stem_function<Scalar>::type StemFunction;
00400     const MatrixExponentialReturnValue<Derived> exp() const;
00401     const MatrixFunctionReturnValue<Derived> matrixFunction(StemFunction f) const;
00402     const MatrixFunctionReturnValue<Derived> cosh() const;
00403     const MatrixFunctionReturnValue<Derived> sinh() const;
00404     const MatrixFunctionReturnValue<Derived> cos() const;
00405     const MatrixFunctionReturnValue<Derived> sin() const;
00406 
00407 #ifdef EIGEN2_SUPPORT
00408     template<typename ProductDerived, typename Lhs, typename Rhs>
00409     Derived& operator+=(const Flagged<ProductBase<ProductDerived, Lhs,Rhs>, 0,
00410                                       EvalBeforeAssigningBit>& other);
00411 
00412     template<typename ProductDerived, typename Lhs, typename Rhs>
00413     Derived& operator-=(const Flagged<ProductBase<ProductDerived, Lhs,Rhs>, 0,
00414                                       EvalBeforeAssigningBit>& other);
00415 
00416     /** \deprecated because .lazy() is deprecated
00417       * Overloaded for cache friendly product evaluation */
00418     template<typename OtherDerived>
00419     Derived& lazyAssign(const Flagged<OtherDerived, 0, EvalBeforeAssigningBit>& other)
00420     { return lazyAssign(other._expression()); }
00421 
00422     template<unsigned int Added>
00423     const Flagged<Derived, Added, 0> marked() const;
00424     const Flagged<Derived, 0, EvalBeforeAssigningBit> lazy() const;
00425 
00426     inline const Cwise<Derived> cwise() const;
00427     inline Cwise<Derived> cwise();
00428 
00429     VectorBlock<Derived> start(Index size);
00430     const VectorBlock<Derived> start(Index size) const;
00431     VectorBlock<Derived> end(Index size);
00432     const VectorBlock<Derived> end(Index size) const;
00433     template<int Size> VectorBlock<Derived,Size> start();
00434     template<int Size> const VectorBlock<Derived,Size> start() const;
00435     template<int Size> VectorBlock<Derived,Size> end();
00436     template<int Size> const VectorBlock<Derived,Size> end() const;
00437 
00438     Minor<Derived> minor(Index row, Index col);
00439     const Minor<Derived> minor(Index row, Index col) const;
00440 #endif
00441 
00442   protected:
00443     MatrixBase() : Base() {}
00444 
00445   private:
00446     explicit MatrixBase(int);
00447     MatrixBase(int,int);
00448     template<typename OtherDerived> explicit MatrixBase(const MatrixBase<OtherDerived>&);
00449   protected:
00450     // mixing arrays and matrices is not legal
00451     template<typename OtherDerived> Derived& operator+=(const ArrayBase<OtherDerived>& array)
00452     {EIGEN_STATIC_ASSERT(sizeof(typename OtherDerived::Scalar)==-1,YOU_CANNOT_MIX_ARRAYS_AND_MATRICES);}
00453     // mixing arrays and matrices is not legal
00454     template<typename OtherDerived> Derived& operator-=(const ArrayBase<OtherDerived>& array)
00455     {EIGEN_STATIC_ASSERT(sizeof(typename OtherDerived::Scalar)==-1,YOU_CANNOT_MIX_ARRAYS_AND_MATRICES);}
00456 };
00457 
00458 #endif // EIGEN_MATRIXBASE_H



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