Eigen  3.3.3
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>::StorageIndex StorageIndex;
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 
00070     using Base::derived;
00071     using Base::const_cast_derived;
00072     using Base::rows;
00073     using Base::cols;
00074     using Base::size;
00075     using Base::coeff;
00076     using Base::coeffRef;
00077     using Base::lazyAssign;
00078     using Base::eval;
00079     using Base::operator+=;
00080     using Base::operator-=;
00081     using Base::operator*=;
00082     using Base::operator/=;
00083 
00084     typedef typename Base::CoeffReturnType CoeffReturnType;
00085     typedef typename Base::ConstTransposeReturnType ConstTransposeReturnType;
00086     typedef typename Base::RowXpr RowXpr;
00087     typedef typename Base::ColXpr ColXpr;
00088 #endif // not EIGEN_PARSED_BY_DOXYGEN
00089 
00090 
00091 
00092 #ifndef EIGEN_PARSED_BY_DOXYGEN
00093 
00094     typedef Matrix<Scalar,EIGEN_SIZE_MAX(RowsAtCompileTime,ColsAtCompileTime),
00095                           EIGEN_SIZE_MAX(RowsAtCompileTime,ColsAtCompileTime)> SquareMatrixType;
00096 #endif // not EIGEN_PARSED_BY_DOXYGEN
00097 
00100     EIGEN_DEVICE_FUNC
00101     inline Index diagonalSize() const { return (numext::mini)(rows(),cols()); }
00102 
00103     typedef typename Base::PlainObject PlainObject;
00104 
00105 #ifndef EIGEN_PARSED_BY_DOXYGEN
00106 
00107     typedef CwiseNullaryOp<internal::scalar_constant_op<Scalar>,PlainObject> ConstantReturnType;
00109     typedef typename internal::conditional<NumTraits<Scalar>::IsComplex,
00110                         CwiseUnaryOp<internal::scalar_conjugate_op<Scalar>, ConstTransposeReturnType>,
00111                         ConstTransposeReturnType
00112                      >::type AdjointReturnType;
00114     typedef Matrix<std::complex<RealScalar>, internal::traits<Derived>::ColsAtCompileTime, 1, ColMajor> EigenvaluesReturnType;
00116     typedef CwiseNullaryOp<internal::scalar_identity_op<Scalar>,PlainObject> IdentityReturnType;
00118     typedef Block<const CwiseNullaryOp<internal::scalar_identity_op<Scalar>, SquareMatrixType>,
00119                   internal::traits<Derived>::RowsAtCompileTime,
00120                   internal::traits<Derived>::ColsAtCompileTime> BasisReturnType;
00121 #endif // not EIGEN_PARSED_BY_DOXYGEN
00122 
00123 #define EIGEN_CURRENT_STORAGE_BASE_CLASS Eigen::MatrixBase
00124 #define EIGEN_DOC_UNARY_ADDONS(X,Y)
00125 #   include "../plugins/CommonCwiseUnaryOps.h"
00126 #   include "../plugins/CommonCwiseBinaryOps.h"
00127 #   include "../plugins/MatrixCwiseUnaryOps.h"
00128 #   include "../plugins/MatrixCwiseBinaryOps.h"
00129 #   ifdef EIGEN_MATRIXBASE_PLUGIN
00130 #     include EIGEN_MATRIXBASE_PLUGIN
00131 #   endif
00132 #undef EIGEN_CURRENT_STORAGE_BASE_CLASS
00133 #undef EIGEN_DOC_UNARY_ADDONS
00134 
00138     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
00139     Derived& operator=(const MatrixBase& other);
00140 
00141     // We cannot inherit here via Base::operator= since it is causing
00142     // trouble with MSVC.
00143 
00144     template <typename OtherDerived>
00145     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
00146     Derived& operator=(const DenseBase<OtherDerived>& other);
00147 
00148     template <typename OtherDerived>
00149     EIGEN_DEVICE_FUNC
00150     Derived& operator=(const EigenBase<OtherDerived>& other);
00151 
00152     template<typename OtherDerived>
00153     EIGEN_DEVICE_FUNC
00154     Derived& operator=(const ReturnByValue<OtherDerived>& other);
00155 
00156     template<typename OtherDerived>
00157     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
00158     Derived& operator+=(const MatrixBase<OtherDerived>& other);
00159     template<typename OtherDerived>
00160     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
00161     Derived& operator-=(const MatrixBase<OtherDerived>& other);
00162 
00163 #ifdef __CUDACC__
00164     template<typename OtherDerived>
00165     EIGEN_DEVICE_FUNC
00166     const Product<Derived,OtherDerived,LazyProduct>
00167     operator*(const MatrixBase<OtherDerived> &other) const
00168     { return this->lazyProduct(other); }
00169 #else
00170 
00171     template<typename OtherDerived>
00172     const Product<Derived,OtherDerived>
00173     operator*(const MatrixBase<OtherDerived> &other) const;
00174 
00175 #endif
00176 
00177     template<typename OtherDerived>
00178     EIGEN_DEVICE_FUNC
00179     const Product<Derived,OtherDerived,LazyProduct>
00180     lazyProduct(const MatrixBase<OtherDerived> &other) const;
00181 
00182     template<typename OtherDerived>
00183     Derived& operator*=(const EigenBase<OtherDerived>& other);
00184 
00185     template<typename OtherDerived>
00186     void applyOnTheLeft(const EigenBase<OtherDerived>& other);
00187 
00188     template<typename OtherDerived>
00189     void applyOnTheRight(const EigenBase<OtherDerived>& other);
00190 
00191     template<typename DiagonalDerived>
00192     EIGEN_DEVICE_FUNC
00193     const Product<Derived, DiagonalDerived, LazyProduct>
00194     operator*(const DiagonalBase<DiagonalDerived> &diagonal) const;
00195 
00196     template<typename OtherDerived>
00197     EIGEN_DEVICE_FUNC
00198     typename ScalarBinaryOpTraits<typename internal::traits<Derived>::Scalar,typename internal::traits<OtherDerived>::Scalar>::ReturnType
00199     dot(const MatrixBase<OtherDerived>& other) const;
00200 
00201     EIGEN_DEVICE_FUNC RealScalar squaredNorm() const;
00202     EIGEN_DEVICE_FUNC RealScalar norm() const;
00203     RealScalar stableNorm() const;
00204     RealScalar blueNorm() const;
00205     RealScalar hypotNorm() const;
00206     EIGEN_DEVICE_FUNC const PlainObject normalized() const;
00207     EIGEN_DEVICE_FUNC const PlainObject stableNormalized() const;
00208     EIGEN_DEVICE_FUNC void normalize();
00209     EIGEN_DEVICE_FUNC void stableNormalize();
00210 
00211     EIGEN_DEVICE_FUNC const AdjointReturnType adjoint() const;
00212     EIGEN_DEVICE_FUNC void adjointInPlace();
00213 
00214     typedef Diagonal<Derived> DiagonalReturnType;
00215     EIGEN_DEVICE_FUNC
00216     DiagonalReturnType diagonal();
00217 
00218     typedef typename internal::add_const<Diagonal<const Derived> >::type ConstDiagonalReturnType;
00219     EIGEN_DEVICE_FUNC
00220     ConstDiagonalReturnType diagonal() const;
00221 
00222     template<int Index> struct DiagonalIndexReturnType { typedef Diagonal<Derived,Index> Type; };
00223     template<int Index> struct ConstDiagonalIndexReturnType { typedef const Diagonal<const Derived,Index> Type; };
00224 
00225     template<int Index>
00226     EIGEN_DEVICE_FUNC
00227     typename DiagonalIndexReturnType<Index>::Type diagonal();
00228 
00229     template<int Index>
00230     EIGEN_DEVICE_FUNC
00231     typename ConstDiagonalIndexReturnType<Index>::Type diagonal() const;
00232 
00233     typedef Diagonal<Derived,DynamicIndex> DiagonalDynamicIndexReturnType;
00234     typedef typename internal::add_const<Diagonal<const Derived,DynamicIndex> >::type ConstDiagonalDynamicIndexReturnType;
00235 
00236     EIGEN_DEVICE_FUNC
00237     DiagonalDynamicIndexReturnType diagonal(Index index);
00238     EIGEN_DEVICE_FUNC
00239     ConstDiagonalDynamicIndexReturnType diagonal(Index index) const;
00240 
00241     template<unsigned int Mode> struct TriangularViewReturnType { typedef TriangularView<Derived, Mode> Type; };
00242     template<unsigned int Mode> struct ConstTriangularViewReturnType { typedef const TriangularView<const Derived, Mode> Type; };
00243 
00244     template<unsigned int Mode>
00245     EIGEN_DEVICE_FUNC
00246     typename TriangularViewReturnType<Mode>::Type triangularView();
00247     template<unsigned int Mode>
00248     EIGEN_DEVICE_FUNC
00249     typename ConstTriangularViewReturnType<Mode>::Type triangularView() const;
00250 
00251     template<unsigned int UpLo> struct SelfAdjointViewReturnType { typedef SelfAdjointView<Derived, UpLo> Type; };
00252     template<unsigned int UpLo> struct ConstSelfAdjointViewReturnType { typedef const SelfAdjointView<const Derived, UpLo> Type; };
00253 
00254     template<unsigned int UpLo>
00255     EIGEN_DEVICE_FUNC
00256     typename SelfAdjointViewReturnType<UpLo>::Type selfadjointView();
00257     template<unsigned int UpLo>
00258     EIGEN_DEVICE_FUNC
00259     typename ConstSelfAdjointViewReturnType<UpLo>::Type selfadjointView() const;
00260 
00261     const SparseView<Derived> sparseView(const Scalar& m_reference = Scalar(0),
00262                                          const typename NumTraits<Scalar>::Real& m_epsilon = NumTraits<Scalar>::dummy_precision()) const;
00263     EIGEN_DEVICE_FUNC static const IdentityReturnType Identity();
00264     EIGEN_DEVICE_FUNC static const IdentityReturnType Identity(Index rows, Index cols);
00265     EIGEN_DEVICE_FUNC static const BasisReturnType Unit(Index size, Index i);
00266     EIGEN_DEVICE_FUNC static const BasisReturnType Unit(Index i);
00267     EIGEN_DEVICE_FUNC static const BasisReturnType UnitX();
00268     EIGEN_DEVICE_FUNC static const BasisReturnType UnitY();
00269     EIGEN_DEVICE_FUNC static const BasisReturnType UnitZ();
00270     EIGEN_DEVICE_FUNC static const BasisReturnType UnitW();
00271 
00272     EIGEN_DEVICE_FUNC
00273     const DiagonalWrapper<const Derived> asDiagonal() const;
00274     const PermutationWrapper<const Derived> asPermutation() const;
00275 
00276     EIGEN_DEVICE_FUNC
00277     Derived& setIdentity();
00278     EIGEN_DEVICE_FUNC
00279     Derived& setIdentity(Index rows, Index cols);
00280 
00281     bool isIdentity(const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const;
00282     bool isDiagonal(const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const;
00283 
00284     bool isUpperTriangular(const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const;
00285     bool isLowerTriangular(const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const;
00286 
00287     template<typename OtherDerived>
00288     bool isOrthogonal(const MatrixBase<OtherDerived>& other,
00289                       const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const;
00290     bool isUnitary(const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const;
00291 
00296     template<typename OtherDerived>
00297     inline bool operator==(const MatrixBase<OtherDerived>& other) const
00298     { return cwiseEqual(other).all(); }
00299 
00304     template<typename OtherDerived>
00305     inline bool operator!=(const MatrixBase<OtherDerived>& other) const
00306     { return cwiseNotEqual(other).any(); }
00307 
00308     NoAlias<Derived,Eigen::MatrixBase > noalias();
00309 
00310     // TODO forceAlignedAccess is temporarily disabled
00311     // Need to find a nicer workaround.
00312     inline const Derived& forceAlignedAccess() const { return derived(); }
00313     inline Derived& forceAlignedAccess() { return derived(); }
00314     template<bool Enable> inline const Derived& forceAlignedAccessIf() const { return derived(); }
00315     template<bool Enable> inline Derived& forceAlignedAccessIf() { return derived(); }
00316 
00317     EIGEN_DEVICE_FUNC Scalar trace() const;
00318 
00319     template<int p> EIGEN_DEVICE_FUNC RealScalar lpNorm() const;
00320 
00321     EIGEN_DEVICE_FUNC MatrixBase<Derived>& matrix() { return *this; }
00322     EIGEN_DEVICE_FUNC const MatrixBase<Derived>& matrix() const { return *this; }
00323 
00326     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE ArrayWrapper<Derived> array() { return ArrayWrapper<Derived>(derived()); }
00329     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const ArrayWrapper<const Derived> array() const { return ArrayWrapper<const Derived>(derived()); }
00330 
00332 
00333     inline const FullPivLU<PlainObject> fullPivLu() const;
00334     inline const PartialPivLU<PlainObject> partialPivLu() const;
00335 
00336     inline const PartialPivLU<PlainObject> lu() const;
00337 
00338     inline const Inverse<Derived> inverse() const;
00339 
00340     template<typename ResultType>
00341     inline void computeInverseAndDetWithCheck(
00342       ResultType& inverse,
00343       typename ResultType::Scalar& determinant,
00344       bool& invertible,
00345       const RealScalar& absDeterminantThreshold = NumTraits<Scalar>::dummy_precision()
00346     ) const;
00347     template<typename ResultType>
00348     inline void computeInverseWithCheck(
00349       ResultType& inverse,
00350       bool& invertible,
00351       const RealScalar& absDeterminantThreshold = NumTraits<Scalar>::dummy_precision()
00352     ) const;
00353     Scalar determinant() const;
00354 
00356 
00357     inline const LLT<PlainObject>  llt() const;
00358     inline const LDLT<PlainObject> ldlt() const;
00359 
00361 
00362     inline const HouseholderQR<PlainObject> householderQr() const;
00363     inline const ColPivHouseholderQR<PlainObject> colPivHouseholderQr() const;
00364     inline const FullPivHouseholderQR<PlainObject> fullPivHouseholderQr() const;
00365     inline const CompleteOrthogonalDecomposition<PlainObject> completeOrthogonalDecomposition() const;
00366 
00368 
00369     inline EigenvaluesReturnType eigenvalues() const;
00370     inline RealScalar operatorNorm() const;
00371 
00373 
00374     inline JacobiSVD<PlainObject> jacobiSvd(unsigned int computationOptions = 0) const;
00375     inline BDCSVD<PlainObject>    bdcSvd(unsigned int computationOptions = 0) const;
00376 
00378 
00379     #ifndef EIGEN_PARSED_BY_DOXYGEN
00380 
00381     template<typename OtherDerived> struct cross_product_return_type {
00382       typedef typename ScalarBinaryOpTraits<typename internal::traits<Derived>::Scalar,typename internal::traits<OtherDerived>::Scalar>::ReturnType Scalar;
00383       typedef Matrix<Scalar,MatrixBase::RowsAtCompileTime,MatrixBase::ColsAtCompileTime> type;
00384     };
00385     #endif // EIGEN_PARSED_BY_DOXYGEN
00386     template<typename OtherDerived>
00387     EIGEN_DEVICE_FUNC
00388 #ifndef EIGEN_PARSED_BY_DOXYGEN
00389     inline typename cross_product_return_type<OtherDerived>::type
00390 #else
00391     inline PlainObject
00392 #endif
00393     cross(const MatrixBase<OtherDerived>& other) const;
00394 
00395     template<typename OtherDerived>
00396     EIGEN_DEVICE_FUNC
00397     inline PlainObject cross3(const MatrixBase<OtherDerived>& other) const;
00398 
00399     EIGEN_DEVICE_FUNC
00400     inline PlainObject unitOrthogonal(void) const;
00401 
00402     EIGEN_DEVICE_FUNC
00403     inline Matrix<Scalar,3,1> eulerAngles(Index a0, Index a1, Index a2) const;
00404 
00405     // put this as separate enum value to work around possible GCC 4.3 bug (?)
00406     enum { HomogeneousReturnTypeDirection = ColsAtCompileTime==1&&RowsAtCompileTime==1 ? ((internal::traits<Derived>::Flags&RowMajorBit)==RowMajorBit ? Horizontal : Vertical)
00407                                           : ColsAtCompileTime==1 ? Vertical : Horizontal };
00408     typedef Homogeneous<Derived, HomogeneousReturnTypeDirection> HomogeneousReturnType;
00409     EIGEN_DEVICE_FUNC
00410     inline HomogeneousReturnType homogeneous() const;
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 EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(ConstStartMinusOne,Scalar,quotient) HNormalizedReturnType;
00419     EIGEN_DEVICE_FUNC
00420     inline const HNormalizedReturnType hnormalized() const;
00421 
00423 
00424     void makeHouseholderInPlace(Scalar& tau, RealScalar& beta);
00425     template<typename EssentialPart>
00426     void makeHouseholder(EssentialPart& essential,
00427                          Scalar& tau, RealScalar& beta) const;
00428     template<typename EssentialPart>
00429     void applyHouseholderOnTheLeft(const EssentialPart& essential,
00430                                    const Scalar& tau,
00431                                    Scalar* workspace);
00432     template<typename EssentialPart>
00433     void applyHouseholderOnTheRight(const EssentialPart& essential,
00434                                     const Scalar& tau,
00435                                     Scalar* workspace);
00436 
00438 
00439     template<typename OtherScalar>
00440     void applyOnTheLeft(Index p, Index q, const JacobiRotation<OtherScalar>& j);
00441     template<typename OtherScalar>
00442     void applyOnTheRight(Index p, Index q, const JacobiRotation<OtherScalar>& j);
00443 
00445 
00446     template<typename OtherDerived>
00447     EIGEN_STRONG_INLINE const typename SparseMatrixBase<OtherDerived>::template CwiseProductDenseReturnType<Derived>::Type
00448     cwiseProduct(const SparseMatrixBase<OtherDerived> &other) const
00449     {
00450       return other.cwiseProduct(derived());
00451     }
00452 
00454 
00455     typedef typename internal::stem_function<Scalar>::type StemFunction;
00456     const MatrixExponentialReturnValue<Derived> exp() const;
00457     const MatrixFunctionReturnValue<Derived> matrixFunction(StemFunction f) const;
00458     const MatrixFunctionReturnValue<Derived> cosh() const;
00459     const MatrixFunctionReturnValue<Derived> sinh() const;
00460     const MatrixFunctionReturnValue<Derived> cos() const;
00461     const MatrixFunctionReturnValue<Derived> sin() const;
00462     const MatrixSquareRootReturnValue<Derived> sqrt() const;
00463     const MatrixLogarithmReturnValue<Derived> log() const;
00464     const MatrixPowerReturnValue<Derived> pow(const RealScalar& p) const;
00465     const MatrixComplexPowerReturnValue<Derived> pow(const std::complex<RealScalar>& p) const;
00466 
00467   protected:
00468     EIGEN_DEVICE_FUNC MatrixBase() : Base() {}
00469 
00470   private:
00471     EIGEN_DEVICE_FUNC explicit MatrixBase(int);
00472     EIGEN_DEVICE_FUNC MatrixBase(int,int);
00473     template<typename OtherDerived> EIGEN_DEVICE_FUNC explicit MatrixBase(const MatrixBase<OtherDerived>&);
00474   protected:
00475     // mixing arrays and matrices is not legal
00476     template<typename OtherDerived> Derived& operator+=(const ArrayBase<OtherDerived>& )
00477     {EIGEN_STATIC_ASSERT(std::ptrdiff_t(sizeof(typename OtherDerived::Scalar))==-1,YOU_CANNOT_MIX_ARRAYS_AND_MATRICES); return *this;}
00478     // mixing arrays and matrices is not legal
00479     template<typename OtherDerived> Derived& operator-=(const ArrayBase<OtherDerived>& )
00480     {EIGEN_STATIC_ASSERT(std::ptrdiff_t(sizeof(typename OtherDerived::Scalar))==-1,YOU_CANNOT_MIX_ARRAYS_AND_MATRICES); return *this;}
00481 };
00482 
00483 
00484 /***************************************************************************
00485 * Implementation of matrix base methods
00486 ***************************************************************************/
00487 
00495 template<typename Derived>
00496 template<typename OtherDerived>
00497 inline Derived&
00498 MatrixBase<Derived>::operator*=(const EigenBase<OtherDerived> &other)
00499 {
00500   other.derived().applyThisOnTheRight(derived());
00501   return derived();
00502 }
00503 
00509 template<typename Derived>
00510 template<typename OtherDerived>
00511 inline void MatrixBase<Derived>::applyOnTheRight(const EigenBase<OtherDerived> &other)
00512 {
00513   other.derived().applyThisOnTheRight(derived());
00514 }
00515 
00521 template<typename Derived>
00522 template<typename OtherDerived>
00523 inline void MatrixBase<Derived>::applyOnTheLeft(const EigenBase<OtherDerived> &other)
00524 {
00525   other.derived().applyThisOnTheLeft(derived());
00526 }
00527 
00528 } // end namespace Eigen
00529 
00530 #endif // EIGEN_MATRIXBASE_H
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends