$treeview $search $mathjax
Eigen  3.2.3
$projectbrief
$projectbrief
$searchbox

MatrixBase.h

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 // This Source Code Form is subject to the terms of the Mozilla
00008 // Public License v. 2.0. If a copy of the MPL was not distributed
00009 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
00010 
00011 #ifndef EIGEN_MATRIXBASE_H
00012 #define EIGEN_MATRIXBASE_H
00013 
00014 namespace Eigen {
00015 
00048 template<typename Derived> class MatrixBase
00049   : public DenseBase<Derived>
00050 {
00051   public:
00052 #ifndef EIGEN_PARSED_BY_DOXYGEN
00053     typedef MatrixBase StorageBaseType;
00054     typedef typename internal::traits<Derived>::StorageKind StorageKind;
00055     typedef typename internal::traits<Derived>::Index Index;
00056     typedef typename internal::traits<Derived>::Scalar Scalar;
00057     typedef typename internal::packet_traits<Scalar>::type PacketScalar;
00058     typedef typename NumTraits<Scalar>::Real RealScalar;
00059 
00060     typedef DenseBase<Derived> Base;
00061     using Base::RowsAtCompileTime;
00062     using Base::ColsAtCompileTime;
00063     using Base::SizeAtCompileTime;
00064     using Base::MaxRowsAtCompileTime;
00065     using Base::MaxColsAtCompileTime;
00066     using Base::MaxSizeAtCompileTime;
00067     using Base::IsVectorAtCompileTime;
00068     using Base::Flags;
00069     using Base::CoeffReadCost;
00070 
00071     using Base::derived;
00072     using Base::const_cast_derived;
00073     using Base::rows;
00074     using Base::cols;
00075     using Base::size;
00076     using Base::coeff;
00077     using Base::coeffRef;
00078     using Base::lazyAssign;
00079     using Base::eval;
00080     using Base::operator+=;
00081     using Base::operator-=;
00082     using Base::operator*=;
00083     using Base::operator/=;
00084 
00085     typedef typename Base::CoeffReturnType CoeffReturnType;
00086     typedef typename Base::ConstTransposeReturnType ConstTransposeReturnType;
00087     typedef typename Base::RowXpr RowXpr;
00088     typedef typename Base::ColXpr ColXpr;
00089 #endif // not EIGEN_PARSED_BY_DOXYGEN
00090 
00091 
00092 
00093 #ifndef EIGEN_PARSED_BY_DOXYGEN
00094 
00095     typedef Matrix<Scalar,EIGEN_SIZE_MAX(RowsAtCompileTime,ColsAtCompileTime),
00096                           EIGEN_SIZE_MAX(RowsAtCompileTime,ColsAtCompileTime)> SquareMatrixType;
00097 #endif // not EIGEN_PARSED_BY_DOXYGEN
00098 
00101     inline Index diagonalSize() const { return (std::min)(rows(),cols()); }
00102 
00109     typedef Matrix<typename internal::traits<Derived>::Scalar,
00110                 internal::traits<Derived>::RowsAtCompileTime,
00111                 internal::traits<Derived>::ColsAtCompileTime,
00112                 AutoAlign | (internal::traits<Derived>::Flags&RowMajorBit ? RowMajor : ColMajor),
00113                 internal::traits<Derived>::MaxRowsAtCompileTime,
00114                 internal::traits<Derived>::MaxColsAtCompileTime
00115           > PlainObject;
00116 
00117 #ifndef EIGEN_PARSED_BY_DOXYGEN
00118 
00119     typedef CwiseNullaryOp<internal::scalar_constant_op<Scalar>,Derived> ConstantReturnType;
00121     typedef typename internal::conditional<NumTraits<Scalar>::IsComplex,
00122                         CwiseUnaryOp<internal::scalar_conjugate_op<Scalar>, ConstTransposeReturnType>,
00123                         ConstTransposeReturnType
00124                      >::type AdjointReturnType;
00126     typedef Matrix<std::complex<RealScalar>, internal::traits<Derived>::ColsAtCompileTime, 1, ColMajor> EigenvaluesReturnType;
00128     typedef CwiseNullaryOp<internal::scalar_identity_op<Scalar>,Derived> IdentityReturnType;
00130     typedef Block<const CwiseNullaryOp<internal::scalar_identity_op<Scalar>, SquareMatrixType>,
00131                   internal::traits<Derived>::RowsAtCompileTime,
00132                   internal::traits<Derived>::ColsAtCompileTime> BasisReturnType;
00133 #endif // not EIGEN_PARSED_BY_DOXYGEN
00134 
00135 #define EIGEN_CURRENT_STORAGE_BASE_CLASS Eigen::MatrixBase
00136 #   include "../plugins/CommonCwiseUnaryOps.h"
00137 #   include "../plugins/CommonCwiseBinaryOps.h"
00138 #   include "../plugins/MatrixCwiseUnaryOps.h"
00139 #   include "../plugins/MatrixCwiseBinaryOps.h"
00140 #   ifdef EIGEN_MATRIXBASE_PLUGIN
00141 #     include EIGEN_MATRIXBASE_PLUGIN
00142 #   endif
00143 #undef EIGEN_CURRENT_STORAGE_BASE_CLASS
00144 
00148     Derived& operator=(const MatrixBase& other);
00149 
00150     // We cannot inherit here via Base::operator= since it is causing
00151     // trouble with MSVC.
00152 
00153     template <typename OtherDerived>
00154     Derived& operator=(const DenseBase<OtherDerived>& other);
00155 
00156     template <typename OtherDerived>
00157     Derived& operator=(const EigenBase<OtherDerived>& other);
00158 
00159     template<typename OtherDerived>
00160     Derived& operator=(const ReturnByValue<OtherDerived>& other);
00161 
00162 #ifndef EIGEN_PARSED_BY_DOXYGEN
00163     template<typename ProductDerived, typename Lhs, typename Rhs>
00164     Derived& lazyAssign(const ProductBase<ProductDerived, Lhs,Rhs>& other);
00165 
00166     template<typename MatrixPower, typename Lhs, typename Rhs>
00167     Derived& lazyAssign(const MatrixPowerProduct<MatrixPower, Lhs,Rhs>& other);
00168 #endif // not EIGEN_PARSED_BY_DOXYGEN
00169 
00170     template<typename OtherDerived>
00171     Derived& operator+=(const MatrixBase<OtherDerived>& other);
00172     template<typename OtherDerived>
00173     Derived& operator-=(const MatrixBase<OtherDerived>& other);
00174 
00175     template<typename OtherDerived>
00176     const typename ProductReturnType<Derived,OtherDerived>::Type
00177     operator*(const MatrixBase<OtherDerived> &other) const;
00178 
00179     template<typename OtherDerived>
00180     const typename LazyProductReturnType<Derived,OtherDerived>::Type
00181     lazyProduct(const MatrixBase<OtherDerived> &other) const;
00182 
00183     template<typename OtherDerived>
00184     Derived& operator*=(const EigenBase<OtherDerived>& other);
00185 
00186     template<typename OtherDerived>
00187     void applyOnTheLeft(const EigenBase<OtherDerived>& other);
00188 
00189     template<typename OtherDerived>
00190     void applyOnTheRight(const EigenBase<OtherDerived>& other);
00191 
00192     template<typename DiagonalDerived>
00193     const DiagonalProduct<Derived, DiagonalDerived, OnTheRight>
00194     operator*(const DiagonalBase<DiagonalDerived> &diagonal) const;
00195 
00196     template<typename OtherDerived>
00197     typename internal::scalar_product_traits<typename internal::traits<Derived>::Scalar,typename internal::traits<OtherDerived>::Scalar>::ReturnType
00198     dot(const MatrixBase<OtherDerived>& other) const;
00199 
00200     #ifdef EIGEN2_SUPPORT
00201       template<typename OtherDerived>
00202       Scalar eigen2_dot(const MatrixBase<OtherDerived>& other) const;
00203     #endif
00204 
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 typename internal::add_const<Diagonal<const Derived> >::type 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     typedef Diagonal<Derived,DynamicIndex> DiagonalDynamicIndexReturnType;
00228     typedef typename internal::add_const<Diagonal<const Derived,DynamicIndex> >::type ConstDiagonalDynamicIndexReturnType;
00229 
00230     DiagonalDynamicIndexReturnType diagonal(Index index);
00231     ConstDiagonalDynamicIndexReturnType diagonal(Index index) const;
00232 
00233     #ifdef EIGEN2_SUPPORT
00234     template<unsigned int Mode> typename internal::eigen2_part_return_type<Derived, Mode>::type part();
00235     template<unsigned int Mode> const typename internal::eigen2_part_return_type<Derived, Mode>::type part() const;
00236     
00237     // huuuge hack. make Eigen2's matrix.part<Diagonal>() work in eigen3. Problem: Diagonal is now a class template instead
00238     // of an integer constant. Solution: overload the part() method template wrt template parameters list.
00239     template<template<typename T, int N> class U>
00240     const DiagonalWrapper<ConstDiagonalReturnType> part() const
00241     { return diagonal().asDiagonal(); }
00242     #endif // EIGEN2_SUPPORT
00243 
00244     template<unsigned int Mode> struct TriangularViewReturnType { typedef TriangularView<Derived, Mode> Type; };
00245     template<unsigned int Mode> struct ConstTriangularViewReturnType { typedef const TriangularView<const Derived, Mode> Type; };
00246 
00247     template<unsigned int Mode> typename TriangularViewReturnType<Mode>::Type triangularView();
00248     template<unsigned int Mode> typename ConstTriangularViewReturnType<Mode>::Type triangularView() const;
00249 
00250     template<unsigned int UpLo> struct SelfAdjointViewReturnType { typedef SelfAdjointView<Derived, UpLo> Type; };
00251     template<unsigned int UpLo> struct ConstSelfAdjointViewReturnType { typedef const SelfAdjointView<const Derived, UpLo> Type; };
00252 
00253     template<unsigned int UpLo> typename SelfAdjointViewReturnType<UpLo>::Type selfadjointView();
00254     template<unsigned int UpLo> typename ConstSelfAdjointViewReturnType<UpLo>::Type selfadjointView() const;
00255 
00256     const SparseView<Derived> sparseView(const Scalar& m_reference = Scalar(0),
00257                                          const typename NumTraits<Scalar>::Real& m_epsilon = NumTraits<Scalar>::dummy_precision()) const;
00258     static const IdentityReturnType Identity();
00259     static const IdentityReturnType Identity(Index rows, Index cols);
00260     static const BasisReturnType Unit(Index size, Index i);
00261     static const BasisReturnType Unit(Index i);
00262     static const BasisReturnType UnitX();
00263     static const BasisReturnType UnitY();
00264     static const BasisReturnType UnitZ();
00265     static const BasisReturnType UnitW();
00266 
00267     const DiagonalWrapper<const Derived> asDiagonal() const;
00268     const PermutationWrapper<const Derived> asPermutation() const;
00269 
00270     Derived& setIdentity();
00271     Derived& setIdentity(Index rows, Index cols);
00272 
00273     bool isIdentity(const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const;
00274     bool isDiagonal(const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const;
00275 
00276     bool isUpperTriangular(const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const;
00277     bool isLowerTriangular(const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const;
00278 
00279     template<typename OtherDerived>
00280     bool isOrthogonal(const MatrixBase<OtherDerived>& other,
00281                       const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const;
00282     bool isUnitary(const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const;
00283 
00288     template<typename OtherDerived>
00289     inline bool operator==(const MatrixBase<OtherDerived>& other) const
00290     { return cwiseEqual(other).all(); }
00291 
00296     template<typename OtherDerived>
00297     inline bool operator!=(const MatrixBase<OtherDerived>& other) const
00298     { return cwiseNotEqual(other).any(); }
00299 
00300     NoAlias<Derived,Eigen::MatrixBase > noalias();
00301 
00302     inline const ForceAlignedAccess<Derived> forceAlignedAccess() const;
00303     inline ForceAlignedAccess<Derived> forceAlignedAccess();
00304     template<bool Enable> inline typename internal::add_const_on_value_type<typename internal::conditional<Enable,ForceAlignedAccess<Derived>,Derived&>::type>::type forceAlignedAccessIf() const;
00305     template<bool Enable> inline typename internal::conditional<Enable,ForceAlignedAccess<Derived>,Derived&>::type forceAlignedAccessIf();
00306 
00307     Scalar trace() const;
00308 
00310 
00311     template<int p> RealScalar lpNorm() const;
00312 
00313     MatrixBase<Derived>& matrix() { return *this; }
00314     const MatrixBase<Derived>& matrix() const { return *this; }
00315 
00318     ArrayWrapper<Derived> array() { return derived(); }
00319     const ArrayWrapper<const Derived> array() const { return derived(); }
00320 
00322 
00323     const FullPivLU<PlainObject> fullPivLu() const;
00324     const PartialPivLU<PlainObject> partialPivLu() const;
00325 
00326     #if EIGEN2_SUPPORT_STAGE < STAGE20_RESOLVE_API_CONFLICTS
00327     const LU<PlainObject> lu() const;
00328     #endif
00329 
00330     #ifdef EIGEN2_SUPPORT
00331     const LU<PlainObject> eigen2_lu() const;
00332     #endif
00333 
00334     #if EIGEN2_SUPPORT_STAGE > STAGE20_RESOLVE_API_CONFLICTS
00335     const PartialPivLU<PlainObject> lu() const;
00336     #endif
00337     
00338     #ifdef EIGEN2_SUPPORT
00339     template<typename ResultType>
00340     void computeInverse(MatrixBase<ResultType> *result) const {
00341       *result = this->inverse();
00342     }
00343     #endif
00344 
00345     const internal::inverse_impl<Derived> inverse() const;
00346     template<typename ResultType>
00347     void computeInverseAndDetWithCheck(
00348       ResultType& inverse,
00349       typename ResultType::Scalar& determinant,
00350       bool& invertible,
00351       const RealScalar& absDeterminantThreshold = NumTraits<Scalar>::dummy_precision()
00352     ) const;
00353     template<typename ResultType>
00354     void computeInverseWithCheck(
00355       ResultType& inverse,
00356       bool& invertible,
00357       const RealScalar& absDeterminantThreshold = NumTraits<Scalar>::dummy_precision()
00358     ) const;
00359     Scalar determinant() const;
00360 
00362 
00363     const LLT<PlainObject>  llt() const;
00364     const LDLT<PlainObject> ldlt() const;
00365 
00367 
00368     const HouseholderQR<PlainObject> householderQr() const;
00369     const ColPivHouseholderQR<PlainObject> colPivHouseholderQr() const;
00370     const FullPivHouseholderQR<PlainObject> fullPivHouseholderQr() const;
00371     
00372     #ifdef EIGEN2_SUPPORT
00373     const QR<PlainObject> qr() const;
00374     #endif
00375 
00376     EigenvaluesReturnType eigenvalues() const;
00377     RealScalar operatorNorm() const;
00378 
00380 
00381     JacobiSVD<PlainObject> jacobiSvd(unsigned int computationOptions = 0) const;
00382 
00383     #ifdef EIGEN2_SUPPORT
00384     SVD<PlainObject> svd() const;
00385     #endif
00386 
00388 
00389     #ifndef EIGEN_PARSED_BY_DOXYGEN
00391     template<typename OtherDerived> struct cross_product_return_type {
00392       typedef typename internal::scalar_product_traits<typename internal::traits<Derived>::Scalar,typename internal::traits<OtherDerived>::Scalar>::ReturnType Scalar;
00393       typedef Matrix<Scalar,MatrixBase::RowsAtCompileTime,MatrixBase::ColsAtCompileTime> type;
00394     };
00395     #endif // EIGEN_PARSED_BY_DOXYGEN
00396     template<typename OtherDerived>
00397     typename cross_product_return_type<OtherDerived>::type
00398     cross(const MatrixBase<OtherDerived>& other) const;
00399     template<typename OtherDerived>
00400     PlainObject cross3(const MatrixBase<OtherDerived>& other) const;
00401     PlainObject unitOrthogonal(void) const;
00402     Matrix<Scalar,3,1> eulerAngles(Index a0, Index a1, Index a2) const;
00403     
00404     #if EIGEN2_SUPPORT_STAGE > STAGE20_RESOLVE_API_CONFLICTS
00405     ScalarMultipleReturnType operator*(const UniformScaling<Scalar>& s) const;
00406     // put this as separate enum value to work around possible GCC 4.3 bug (?)
00407     enum { HomogeneousReturnTypeDirection = ColsAtCompileTime==1?Vertical:Horizontal };
00408     typedef Homogeneous<Derived, HomogeneousReturnTypeDirection> HomogeneousReturnType;
00409     HomogeneousReturnType homogeneous() const;
00410     #endif
00411     
00412     enum {
00413       SizeMinusOne = SizeAtCompileTime==Dynamic ? Dynamic : SizeAtCompileTime-1
00414     };
00415     typedef Block<const Derived,
00416                   internal::traits<Derived>::ColsAtCompileTime==1 ? SizeMinusOne : 1,
00417                   internal::traits<Derived>::ColsAtCompileTime==1 ? 1 : SizeMinusOne> ConstStartMinusOne;
00418     typedef CwiseUnaryOp<internal::scalar_quotient1_op<typename internal::traits<Derived>::Scalar>,
00419                 const ConstStartMinusOne > HNormalizedReturnType;
00420 
00421     const HNormalizedReturnType hnormalized() const;
00422 
00424 
00425     void makeHouseholderInPlace(Scalar& tau, RealScalar& beta);
00426     template<typename EssentialPart>
00427     void makeHouseholder(EssentialPart& essential,
00428                          Scalar& tau, RealScalar& beta) const;
00429     template<typename EssentialPart>
00430     void applyHouseholderOnTheLeft(const EssentialPart& essential,
00431                                    const Scalar& tau,
00432                                    Scalar* workspace);
00433     template<typename EssentialPart>
00434     void applyHouseholderOnTheRight(const EssentialPart& essential,
00435                                     const Scalar& tau,
00436                                     Scalar* workspace);
00437 
00439 
00440     template<typename OtherScalar>
00441     void applyOnTheLeft(Index p, Index q, const JacobiRotation<OtherScalar>& j);
00442     template<typename OtherScalar>
00443     void applyOnTheRight(Index p, Index q, const JacobiRotation<OtherScalar>& j);
00444 
00446 
00447     typedef typename internal::stem_function<Scalar>::type StemFunction;
00448     const MatrixExponentialReturnValue<Derived> exp() const;
00449     const MatrixFunctionReturnValue<Derived> matrixFunction(StemFunction f) const;
00450     const MatrixFunctionReturnValue<Derived> cosh() const;
00451     const MatrixFunctionReturnValue<Derived> sinh() const;
00452     const MatrixFunctionReturnValue<Derived> cos() const;
00453     const MatrixFunctionReturnValue<Derived> sin() const;
00454     const MatrixSquareRootReturnValue<Derived> sqrt() const;
00455     const MatrixLogarithmReturnValue<Derived> log() const;
00456     const MatrixPowerReturnValue<Derived> pow(const RealScalar& p) const;
00457 
00458 #ifdef EIGEN2_SUPPORT
00459     template<typename ProductDerived, typename Lhs, typename Rhs>
00460     Derived& operator+=(const Flagged<ProductBase<ProductDerived, Lhs,Rhs>, 0,
00461                                       EvalBeforeAssigningBit>& other);
00462 
00463     template<typename ProductDerived, typename Lhs, typename Rhs>
00464     Derived& operator-=(const Flagged<ProductBase<ProductDerived, Lhs,Rhs>, 0,
00465                                       EvalBeforeAssigningBit>& other);
00466 
00469     template<typename OtherDerived>
00470     Derived& lazyAssign(const Flagged<OtherDerived, 0, EvalBeforeAssigningBit>& other)
00471     { return lazyAssign(other._expression()); }
00472 
00473     template<unsigned int Added>
00474     const Flagged<Derived, Added, 0> marked() const;
00475     const Flagged<Derived, 0, EvalBeforeAssigningBit> lazy() const;
00476 
00477     inline const Cwise<Derived> cwise() const;
00478     inline Cwise<Derived> cwise();
00479 
00480     VectorBlock<Derived> start(Index size);
00481     const VectorBlock<const Derived> start(Index size) const;
00482     VectorBlock<Derived> end(Index size);
00483     const VectorBlock<const Derived> end(Index size) const;
00484     template<int Size> VectorBlock<Derived,Size> start();
00485     template<int Size> const VectorBlock<const Derived,Size> start() const;
00486     template<int Size> VectorBlock<Derived,Size> end();
00487     template<int Size> const VectorBlock<const Derived,Size> end() const;
00488 
00489     Minor<Derived> minor(Index row, Index col);
00490     const Minor<Derived> minor(Index row, Index col) const;
00491 #endif
00492 
00493   protected:
00494     MatrixBase() : Base() {}
00495 
00496   private:
00497     explicit MatrixBase(int);
00498     MatrixBase(int,int);
00499     template<typename OtherDerived> explicit MatrixBase(const MatrixBase<OtherDerived>&);
00500   protected:
00501     // mixing arrays and matrices is not legal
00502     template<typename OtherDerived> Derived& operator+=(const ArrayBase<OtherDerived>& )
00503     {EIGEN_STATIC_ASSERT(std::ptrdiff_t(sizeof(typename OtherDerived::Scalar))==-1,YOU_CANNOT_MIX_ARRAYS_AND_MATRICES); return *this;}
00504     // mixing arrays and matrices is not legal
00505     template<typename OtherDerived> Derived& operator-=(const ArrayBase<OtherDerived>& )
00506     {EIGEN_STATIC_ASSERT(std::ptrdiff_t(sizeof(typename OtherDerived::Scalar))==-1,YOU_CANNOT_MIX_ARRAYS_AND_MATRICES); return *this;}
00507 };
00508 
00509 
00510 /***************************************************************************
00511 * Implementation of matrix base methods
00512 ***************************************************************************/
00513 
00521 template<typename Derived>
00522 template<typename OtherDerived>
00523 inline Derived&
00524 MatrixBase<Derived>::operator*=(const EigenBase<OtherDerived> &other)
00525 {
00526   other.derived().applyThisOnTheRight(derived());
00527   return derived();
00528 }
00529 
00535 template<typename Derived>
00536 template<typename OtherDerived>
00537 inline void MatrixBase<Derived>::applyOnTheRight(const EigenBase<OtherDerived> &other)
00538 {
00539   other.derived().applyThisOnTheRight(derived());
00540 }
00541 
00547 template<typename Derived>
00548 template<typename OtherDerived>
00549 inline void MatrixBase<Derived>::applyOnTheLeft(const EigenBase<OtherDerived> &other)
00550 {
00551   other.derived().applyThisOnTheLeft(derived());
00552 }
00553 
00554 } // end namespace Eigen
00555 
00556 #endif // EIGEN_MATRIXBASE_H