MOAB  4.9.3pre
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 <[email protected]>
00005 // Copyright (C) 2008 Gael Guennebaud <[email protected]>
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     using Base::operator*;
00084     using Base::operator/;
00085 
00086     typedef typename Base::CoeffReturnType CoeffReturnType;
00087     typedef typename Base::ConstTransposeReturnType ConstTransposeReturnType;
00088     typedef typename Base::RowXpr RowXpr;
00089     typedef typename Base::ColXpr ColXpr;
00090 #endif // not EIGEN_PARSED_BY_DOXYGEN
00091 
00092 
00093 
00094 #ifndef EIGEN_PARSED_BY_DOXYGEN
00095 
00096     typedef Matrix<Scalar,EIGEN_SIZE_MAX(RowsAtCompileTime,ColsAtCompileTime),
00097                           EIGEN_SIZE_MAX(RowsAtCompileTime,ColsAtCompileTime)> SquareMatrixType;
00098 #endif // not EIGEN_PARSED_BY_DOXYGEN
00099 
00102     EIGEN_DEVICE_FUNC
00103     inline Index diagonalSize() const { return (std::min)(rows(),cols()); }
00104 
00105     typedef typename Base::PlainObject PlainObject;
00106 
00107 #ifndef EIGEN_PARSED_BY_DOXYGEN
00108 
00109     typedef CwiseNullaryOp<internal::scalar_constant_op<Scalar>,PlainObject> ConstantReturnType;
00111     typedef typename internal::conditional<NumTraits<Scalar>::IsComplex,
00112                         CwiseUnaryOp<internal::scalar_conjugate_op<Scalar>, ConstTransposeReturnType>,
00113                         ConstTransposeReturnType
00114                      >::type AdjointReturnType;
00116     typedef Matrix<std::complex<RealScalar>, internal::traits<Derived>::ColsAtCompileTime, 1, ColMajor> EigenvaluesReturnType;
00118     typedef CwiseNullaryOp<internal::scalar_identity_op<Scalar>,PlainObject> IdentityReturnType;
00120     typedef Block<const CwiseNullaryOp<internal::scalar_identity_op<Scalar>, SquareMatrixType>,
00121                   internal::traits<Derived>::RowsAtCompileTime,
00122                   internal::traits<Derived>::ColsAtCompileTime> BasisReturnType;
00123 #endif // not EIGEN_PARSED_BY_DOXYGEN
00124 
00125 #define EIGEN_CURRENT_STORAGE_BASE_CLASS Eigen::MatrixBase
00126 #   include "../plugins/CommonCwiseUnaryOps.h"
00127 #   include "../plugins/CommonCwiseBinaryOps.h"
00128 #   include "../plugins/MatrixCwiseUnaryOps.h"
00129 #   include "../plugins/MatrixCwiseBinaryOps.h"
00130 #   ifdef EIGEN_MATRIXBASE_PLUGIN
00131 #     include EIGEN_MATRIXBASE_PLUGIN
00132 #   endif
00133 #undef EIGEN_CURRENT_STORAGE_BASE_CLASS
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 internal::scalar_product_traits<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     EIGEN_DEVICE_FUNC
00334     inline const FullPivLU<PlainObject> fullPivLu() const;
00335     EIGEN_DEVICE_FUNC
00336     inline const PartialPivLU<PlainObject> partialPivLu() const;
00337 
00338     EIGEN_DEVICE_FUNC
00339     inline const PartialPivLU<PlainObject> lu() const;
00340 
00341     EIGEN_DEVICE_FUNC
00342     inline const Inverse<Derived> inverse() const;
00343 
00344     template<typename ResultType>
00345     inline void computeInverseAndDetWithCheck(
00346       ResultType& inverse,
00347       typename ResultType::Scalar& determinant,
00348       bool& invertible,
00349       const RealScalar& absDeterminantThreshold = NumTraits<Scalar>::dummy_precision()
00350     ) const;
00351     template<typename ResultType>
00352     inline void computeInverseWithCheck(
00353       ResultType& inverse,
00354       bool& invertible,
00355       const RealScalar& absDeterminantThreshold = NumTraits<Scalar>::dummy_precision()
00356     ) const;
00357     Scalar determinant() const;
00358 
00360 
00361     inline const LLT<PlainObject>  llt() const;
00362     inline const LDLT<PlainObject> ldlt() const;
00363 
00365 
00366     inline const HouseholderQR<PlainObject> householderQr() const;
00367     inline const ColPivHouseholderQR<PlainObject> colPivHouseholderQr() const;
00368     inline const FullPivHouseholderQR<PlainObject> fullPivHouseholderQr() const;
00369     inline const CompleteOrthogonalDecomposition<PlainObject> completeOrthogonalDecomposition() const;
00370 
00372 
00373     inline EigenvaluesReturnType eigenvalues() const;
00374     inline RealScalar operatorNorm() const;
00375 
00377 
00378     inline JacobiSVD<PlainObject> jacobiSvd(unsigned int computationOptions = 0) const;
00379     inline BDCSVD<PlainObject>    bdcSvd(unsigned int computationOptions = 0) const;
00380 
00382 
00383     #ifndef EIGEN_PARSED_BY_DOXYGEN
00384 
00385     template<typename OtherDerived> struct cross_product_return_type {
00386       typedef typename internal::scalar_product_traits<typename internal::traits<Derived>::Scalar,typename internal::traits<OtherDerived>::Scalar>::ReturnType Scalar;
00387       typedef Matrix<Scalar,MatrixBase::RowsAtCompileTime,MatrixBase::ColsAtCompileTime> type;
00388     };
00389     #endif // EIGEN_PARSED_BY_DOXYGEN
00390     template<typename OtherDerived>
00391     EIGEN_DEVICE_FUNC
00392 #ifndef EIGEN_PARSED_BY_DOXYGEN
00393     inline typename cross_product_return_type<OtherDerived>::type
00394 #else
00395     inline PlainObject
00396 #endif
00397     cross(const MatrixBase<OtherDerived>& other) const;
00398 
00399     template<typename OtherDerived>
00400     EIGEN_DEVICE_FUNC
00401     inline PlainObject cross3(const MatrixBase<OtherDerived>& other) const;
00402 
00403     EIGEN_DEVICE_FUNC
00404     inline PlainObject unitOrthogonal(void) const;
00405 
00406     inline Matrix<Scalar,3,1> eulerAngles(Index a0, Index a1, Index a2) const;
00407 
00408     inline ScalarMultipleReturnType operator*(const UniformScaling<Scalar>& s) const;
00409     // put this as separate enum value to work around possible GCC 4.3 bug (?)
00410     enum { HomogeneousReturnTypeDirection = ColsAtCompileTime==1&&RowsAtCompileTime==1 ? ((internal::traits<Derived>::Flags&RowMajorBit)==RowMajorBit ? Horizontal : Vertical)
00411                                           : ColsAtCompileTime==1 ? Vertical : Horizontal };
00412     typedef Homogeneous<Derived, HomogeneousReturnTypeDirection> HomogeneousReturnType;
00413     inline HomogeneousReturnType homogeneous() const;
00414 
00415     enum {
00416       SizeMinusOne = SizeAtCompileTime==Dynamic ? Dynamic : SizeAtCompileTime-1
00417     };
00418     typedef Block<const Derived,
00419                   internal::traits<Derived>::ColsAtCompileTime==1 ? SizeMinusOne : 1,
00420                   internal::traits<Derived>::ColsAtCompileTime==1 ? 1 : SizeMinusOne> ConstStartMinusOne;
00421     typedef CwiseUnaryOp<internal::scalar_quotient1_op<typename internal::traits<Derived>::Scalar>,
00422                 const ConstStartMinusOne > HNormalizedReturnType;
00423 
00424     inline const HNormalizedReturnType hnormalized() const;
00425 
00427 
00428     void makeHouseholderInPlace(Scalar& tau, RealScalar& beta);
00429     template<typename EssentialPart>
00430     void makeHouseholder(EssentialPart& essential,
00431                          Scalar& tau, RealScalar& beta) const;
00432     template<typename EssentialPart>
00433     void applyHouseholderOnTheLeft(const EssentialPart& essential,
00434                                    const Scalar& tau,
00435                                    Scalar* workspace);
00436     template<typename EssentialPart>
00437     void applyHouseholderOnTheRight(const EssentialPart& essential,
00438                                     const Scalar& tau,
00439                                     Scalar* workspace);
00440 
00442 
00443     template<typename OtherScalar>
00444     void applyOnTheLeft(Index p, Index q, const JacobiRotation<OtherScalar>& j);
00445     template<typename OtherScalar>
00446     void applyOnTheRight(Index p, Index q, const JacobiRotation<OtherScalar>& j);
00447 
00449 
00450     template<typename OtherDerived>
00451     EIGEN_STRONG_INLINE const typename SparseMatrixBase<OtherDerived>::template CwiseProductDenseReturnType<Derived>::Type
00452     cwiseProduct(const SparseMatrixBase<OtherDerived> &other) const
00453     {
00454       return other.cwiseProduct(derived());
00455     }
00456 
00458 
00459     typedef typename internal::stem_function<Scalar>::type StemFunction;
00460     const MatrixExponentialReturnValue<Derived> exp() const;
00461     const MatrixFunctionReturnValue<Derived> matrixFunction(StemFunction f) const;
00462     const MatrixFunctionReturnValue<Derived> cosh() const;
00463     const MatrixFunctionReturnValue<Derived> sinh() const;
00464     const MatrixFunctionReturnValue<Derived> cos() const;
00465     const MatrixFunctionReturnValue<Derived> sin() const;
00466     const MatrixSquareRootReturnValue<Derived> sqrt() const;
00467     const MatrixLogarithmReturnValue<Derived> log() const;
00468     const MatrixPowerReturnValue<Derived> pow(const RealScalar& p) const;
00469     const MatrixComplexPowerReturnValue<Derived> pow(const std::complex<RealScalar>& p) const;
00470 
00471   protected:
00472     EIGEN_DEVICE_FUNC MatrixBase() : Base() {}
00473 
00474   private:
00475     EIGEN_DEVICE_FUNC explicit MatrixBase(int);
00476     EIGEN_DEVICE_FUNC MatrixBase(int,int);
00477     template<typename OtherDerived> EIGEN_DEVICE_FUNC explicit MatrixBase(const MatrixBase<OtherDerived>&);
00478   protected:
00479     // mixing arrays and matrices is not legal
00480     template<typename OtherDerived> Derived& operator+=(const ArrayBase<OtherDerived>& )
00481     {EIGEN_STATIC_ASSERT(std::ptrdiff_t(sizeof(typename OtherDerived::Scalar))==-1,YOU_CANNOT_MIX_ARRAYS_AND_MATRICES); return *this;}
00482     // mixing arrays and matrices is not legal
00483     template<typename OtherDerived> Derived& operator-=(const ArrayBase<OtherDerived>& )
00484     {EIGEN_STATIC_ASSERT(std::ptrdiff_t(sizeof(typename OtherDerived::Scalar))==-1,YOU_CANNOT_MIX_ARRAYS_AND_MATRICES); return *this;}
00485 };
00486 
00487 
00488 /***************************************************************************
00489 * Implementation of matrix base methods
00490 ***************************************************************************/
00491 
00499 template<typename Derived>
00500 template<typename OtherDerived>
00501 inline Derived&
00502 MatrixBase<Derived>::operator*=(const EigenBase<OtherDerived> &other)
00503 {
00504   other.derived().applyThisOnTheRight(derived());
00505   return derived();
00506 }
00507 
00513 template<typename Derived>
00514 template<typename OtherDerived>
00515 inline void MatrixBase<Derived>::applyOnTheRight(const EigenBase<OtherDerived> &other)
00516 {
00517   other.derived().applyThisOnTheRight(derived());
00518 }
00519 
00525 template<typename Derived>
00526 template<typename OtherDerived>
00527 inline void MatrixBase<Derived>::applyOnTheLeft(const EigenBase<OtherDerived> &other)
00528 {
00529   other.derived().applyThisOnTheLeft(derived());
00530 }
00531 
00532 } // end namespace Eigen
00533 
00534 #endif // EIGEN_MATRIXBASE_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines