Eigen  3.3.3
DenseBase.h
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2007-2010 Benoit Jacob <jacob.benoit.1@gmail.com>
00005 // Copyright (C) 2008-2010 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_DENSEBASE_H
00012 #define EIGEN_DENSEBASE_H
00013 
00014 namespace Eigen {
00015 
00016 namespace internal {
00017   
00018 // The index type defined by EIGEN_DEFAULT_DENSE_INDEX_TYPE must be a signed type.
00019 // This dummy function simply aims at checking that at compile time.
00020 static inline void check_DenseIndex_is_signed() {
00021   EIGEN_STATIC_ASSERT(NumTraits<DenseIndex>::IsSigned,THE_INDEX_TYPE_MUST_BE_A_SIGNED_TYPE); 
00022 }
00023 
00024 } // end namespace internal
00025   
00041 template<typename Derived> class DenseBase
00042 #ifndef EIGEN_PARSED_BY_DOXYGEN
00043   : public DenseCoeffsBase<Derived>
00044 #else
00045   : public DenseCoeffsBase<Derived,DirectWriteAccessors>
00046 #endif // not EIGEN_PARSED_BY_DOXYGEN
00047 {
00048   public:
00049 
00053     typedef Eigen::InnerIterator<Derived> InnerIterator;
00054 
00055     typedef typename internal::traits<Derived>::StorageKind StorageKind;
00056 
00063     typedef typename internal::traits<Derived>::StorageIndex StorageIndex;
00064 
00066     typedef typename internal::traits<Derived>::Scalar Scalar;
00067     
00071     typedef Scalar value_type;
00072     
00073     typedef typename NumTraits<Scalar>::Real RealScalar;
00074     typedef DenseCoeffsBase<Derived> Base;
00075 
00076     using Base::derived;
00077     using Base::const_cast_derived;
00078     using Base::rows;
00079     using Base::cols;
00080     using Base::size;
00081     using Base::rowIndexByOuterInner;
00082     using Base::colIndexByOuterInner;
00083     using Base::coeff;
00084     using Base::coeffByOuterInner;
00085     using Base::operator();
00086     using Base::operator[];
00087     using Base::x;
00088     using Base::y;
00089     using Base::z;
00090     using Base::w;
00091     using Base::stride;
00092     using Base::innerStride;
00093     using Base::outerStride;
00094     using Base::rowStride;
00095     using Base::colStride;
00096     typedef typename Base::CoeffReturnType CoeffReturnType;
00097 
00098     enum {
00099 
00100       RowsAtCompileTime = internal::traits<Derived>::RowsAtCompileTime,
00106       ColsAtCompileTime = internal::traits<Derived>::ColsAtCompileTime,
00113       SizeAtCompileTime = (internal::size_at_compile_time<internal::traits<Derived>::RowsAtCompileTime,
00114                                                    internal::traits<Derived>::ColsAtCompileTime>::ret),
00119       MaxRowsAtCompileTime = internal::traits<Derived>::MaxRowsAtCompileTime,
00130       MaxColsAtCompileTime = internal::traits<Derived>::MaxColsAtCompileTime,
00141       MaxSizeAtCompileTime = (internal::size_at_compile_time<internal::traits<Derived>::MaxRowsAtCompileTime,
00142                                                       internal::traits<Derived>::MaxColsAtCompileTime>::ret),
00153       IsVectorAtCompileTime = internal::traits<Derived>::MaxRowsAtCompileTime == 1
00154                            || internal::traits<Derived>::MaxColsAtCompileTime == 1,
00160       Flags = internal::traits<Derived>::Flags,
00165       IsRowMajor = int(Flags) & RowMajorBit, 
00167       InnerSizeAtCompileTime = int(IsVectorAtCompileTime) ? int(SizeAtCompileTime)
00168                              : int(IsRowMajor) ? int(ColsAtCompileTime) : int(RowsAtCompileTime),
00169 
00170       InnerStrideAtCompileTime = internal::inner_stride_at_compile_time<Derived>::ret,
00171       OuterStrideAtCompileTime = internal::outer_stride_at_compile_time<Derived>::ret
00172     };
00173     
00174     typedef typename internal::find_best_packet<Scalar,SizeAtCompileTime>::type PacketScalar;
00175 
00176     enum { IsPlainObjectBase = 0 };
00177     
00180     typedef Matrix<typename internal::traits<Derived>::Scalar,
00181                 internal::traits<Derived>::RowsAtCompileTime,
00182                 internal::traits<Derived>::ColsAtCompileTime,
00183                 AutoAlign | (internal::traits<Derived>::Flags&RowMajorBit ? RowMajor : ColMajor),
00184                 internal::traits<Derived>::MaxRowsAtCompileTime,
00185                 internal::traits<Derived>::MaxColsAtCompileTime
00186           > PlainMatrix;
00187     
00190     typedef Array<typename internal::traits<Derived>::Scalar,
00191                 internal::traits<Derived>::RowsAtCompileTime,
00192                 internal::traits<Derived>::ColsAtCompileTime,
00193                 AutoAlign | (internal::traits<Derived>::Flags&RowMajorBit ? RowMajor : ColMajor),
00194                 internal::traits<Derived>::MaxRowsAtCompileTime,
00195                 internal::traits<Derived>::MaxColsAtCompileTime
00196           > PlainArray;
00197 
00204     typedef typename internal::conditional<internal::is_same<typename internal::traits<Derived>::XprKind,MatrixXpr >::value,
00205                                  PlainMatrix, PlainArray>::type PlainObject;
00206 
00209     EIGEN_DEVICE_FUNC
00210     inline Index nonZeros() const { return size(); }
00211 
00217     EIGEN_DEVICE_FUNC
00218     Index outerSize() const
00219     {
00220       return IsVectorAtCompileTime ? 1
00221            : int(IsRowMajor) ? this->rows() : this->cols();
00222     }
00223 
00229     EIGEN_DEVICE_FUNC
00230     Index innerSize() const
00231     {
00232       return IsVectorAtCompileTime ? this->size()
00233            : int(IsRowMajor) ? this->cols() : this->rows();
00234     }
00235 
00240     EIGEN_DEVICE_FUNC
00241     void resize(Index newSize)
00242     {
00243       EIGEN_ONLY_USED_FOR_DEBUG(newSize);
00244       eigen_assert(newSize == this->size()
00245                 && "DenseBase::resize() does not actually allow to resize.");
00246     }
00251     EIGEN_DEVICE_FUNC
00252     void resize(Index rows, Index cols)
00253     {
00254       EIGEN_ONLY_USED_FOR_DEBUG(rows);
00255       EIGEN_ONLY_USED_FOR_DEBUG(cols);
00256       eigen_assert(rows == this->rows() && cols == this->cols()
00257                 && "DenseBase::resize() does not actually allow to resize.");
00258     }
00259 
00260 #ifndef EIGEN_PARSED_BY_DOXYGEN
00261 
00262     typedef CwiseNullaryOp<internal::scalar_constant_op<Scalar>,PlainObject> ConstantReturnType;
00264     typedef CwiseNullaryOp<internal::linspaced_op<Scalar,PacketScalar>,PlainObject> SequentialLinSpacedReturnType;
00266     typedef CwiseNullaryOp<internal::linspaced_op<Scalar,PacketScalar>,PlainObject> RandomAccessLinSpacedReturnType;
00268     typedef Matrix<typename NumTraits<typename internal::traits<Derived>::Scalar>::Real, internal::traits<Derived>::ColsAtCompileTime, 1> EigenvaluesReturnType;
00269 
00270 #endif // not EIGEN_PARSED_BY_DOXYGEN
00271 
00273     template<typename OtherDerived>
00274     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
00275     Derived& operator=(const DenseBase<OtherDerived>& other);
00276 
00280     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
00281     Derived& operator=(const DenseBase& other);
00282 
00283     template<typename OtherDerived>
00284     EIGEN_DEVICE_FUNC
00285     Derived& operator=(const EigenBase<OtherDerived> &other);
00286 
00287     template<typename OtherDerived>
00288     EIGEN_DEVICE_FUNC
00289     Derived& operator+=(const EigenBase<OtherDerived> &other);
00290 
00291     template<typename OtherDerived>
00292     EIGEN_DEVICE_FUNC
00293     Derived& operator-=(const EigenBase<OtherDerived> &other);
00294 
00295     template<typename OtherDerived>
00296     EIGEN_DEVICE_FUNC
00297     Derived& operator=(const ReturnByValue<OtherDerived>& func);
00298 
00302     template<typename OtherDerived>
00303     EIGEN_DEVICE_FUNC
00304     Derived& lazyAssign(const DenseBase<OtherDerived>& other);
00305 
00306     EIGEN_DEVICE_FUNC
00307     CommaInitializer<Derived> operator<< (const Scalar& s);
00308 
00310     template<unsigned int Added,unsigned int Removed>
00311     EIGEN_DEPRECATED
00312     const Derived& flagged() const
00313     { return derived(); }
00314 
00315     template<typename OtherDerived>
00316     EIGEN_DEVICE_FUNC
00317     CommaInitializer<Derived> operator<< (const DenseBase<OtherDerived>& other);
00318 
00319     typedef Transpose<Derived> TransposeReturnType;
00320     EIGEN_DEVICE_FUNC
00321     TransposeReturnType transpose();
00322     typedef typename internal::add_const<Transpose<const Derived> >::type ConstTransposeReturnType;
00323     EIGEN_DEVICE_FUNC
00324     ConstTransposeReturnType transpose() const;
00325     EIGEN_DEVICE_FUNC
00326     void transposeInPlace();
00327 
00328     EIGEN_DEVICE_FUNC static const ConstantReturnType
00329     Constant(Index rows, Index cols, const Scalar& value);
00330     EIGEN_DEVICE_FUNC static const ConstantReturnType
00331     Constant(Index size, const Scalar& value);
00332     EIGEN_DEVICE_FUNC static const ConstantReturnType
00333     Constant(const Scalar& value);
00334 
00335     EIGEN_DEVICE_FUNC static const SequentialLinSpacedReturnType
00336     LinSpaced(Sequential_t, Index size, const Scalar& low, const Scalar& high);
00337     EIGEN_DEVICE_FUNC static const RandomAccessLinSpacedReturnType
00338     LinSpaced(Index size, const Scalar& low, const Scalar& high);
00339     EIGEN_DEVICE_FUNC static const SequentialLinSpacedReturnType
00340     LinSpaced(Sequential_t, const Scalar& low, const Scalar& high);
00341     EIGEN_DEVICE_FUNC static const RandomAccessLinSpacedReturnType
00342     LinSpaced(const Scalar& low, const Scalar& high);
00343 
00344     template<typename CustomNullaryOp> EIGEN_DEVICE_FUNC
00345     static const CwiseNullaryOp<CustomNullaryOp, PlainObject>
00346     NullaryExpr(Index rows, Index cols, const CustomNullaryOp& func);
00347     template<typename CustomNullaryOp> EIGEN_DEVICE_FUNC
00348     static const CwiseNullaryOp<CustomNullaryOp, PlainObject>
00349     NullaryExpr(Index size, const CustomNullaryOp& func);
00350     template<typename CustomNullaryOp> EIGEN_DEVICE_FUNC
00351     static const CwiseNullaryOp<CustomNullaryOp, PlainObject>
00352     NullaryExpr(const CustomNullaryOp& func);
00353 
00354     EIGEN_DEVICE_FUNC static const ConstantReturnType Zero(Index rows, Index cols);
00355     EIGEN_DEVICE_FUNC static const ConstantReturnType Zero(Index size);
00356     EIGEN_DEVICE_FUNC static const ConstantReturnType Zero();
00357     EIGEN_DEVICE_FUNC static const ConstantReturnType Ones(Index rows, Index cols);
00358     EIGEN_DEVICE_FUNC static const ConstantReturnType Ones(Index size);
00359     EIGEN_DEVICE_FUNC static const ConstantReturnType Ones();
00360 
00361     EIGEN_DEVICE_FUNC void fill(const Scalar& value);
00362     EIGEN_DEVICE_FUNC Derived& setConstant(const Scalar& value);
00363     EIGEN_DEVICE_FUNC Derived& setLinSpaced(Index size, const Scalar& low, const Scalar& high);
00364     EIGEN_DEVICE_FUNC Derived& setLinSpaced(const Scalar& low, const Scalar& high);
00365     EIGEN_DEVICE_FUNC Derived& setZero();
00366     EIGEN_DEVICE_FUNC Derived& setOnes();
00367     EIGEN_DEVICE_FUNC Derived& setRandom();
00368 
00369     template<typename OtherDerived> EIGEN_DEVICE_FUNC
00370     bool isApprox(const DenseBase<OtherDerived>& other,
00371                   const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const;
00372     EIGEN_DEVICE_FUNC 
00373     bool isMuchSmallerThan(const RealScalar& other,
00374                            const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const;
00375     template<typename OtherDerived> EIGEN_DEVICE_FUNC
00376     bool isMuchSmallerThan(const DenseBase<OtherDerived>& other,
00377                            const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const;
00378 
00379     EIGEN_DEVICE_FUNC bool isApproxToConstant(const Scalar& value, const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const;
00380     EIGEN_DEVICE_FUNC bool isConstant(const Scalar& value, const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const;
00381     EIGEN_DEVICE_FUNC bool isZero(const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const;
00382     EIGEN_DEVICE_FUNC bool isOnes(const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const;
00383     
00384     inline bool hasNaN() const;
00385     inline bool allFinite() const;
00386 
00387     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
00388     Derived& operator*=(const Scalar& other);
00389     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
00390     Derived& operator/=(const Scalar& other);
00391 
00392     typedef typename internal::add_const_on_value_type<typename internal::eval<Derived>::type>::type EvalReturnType;
00400     EIGEN_DEVICE_FUNC
00401     EIGEN_STRONG_INLINE EvalReturnType eval() const
00402     {
00403       // Even though MSVC does not honor strong inlining when the return type
00404       // is a dynamic matrix, we desperately need strong inlining for fixed
00405       // size types on MSVC.
00406       return typename internal::eval<Derived>::type(derived());
00407     }
00408     
00412     template<typename OtherDerived>
00413     EIGEN_DEVICE_FUNC
00414     void swap(const DenseBase<OtherDerived>& other)
00415     {
00416       EIGEN_STATIC_ASSERT(!OtherDerived::IsPlainObjectBase,THIS_EXPRESSION_IS_NOT_A_LVALUE__IT_IS_READ_ONLY);
00417       eigen_assert(rows()==other.rows() && cols()==other.cols());
00418       call_assignment(derived(), other.const_cast_derived(), internal::swap_assign_op<Scalar>());
00419     }
00420 
00424     template<typename OtherDerived>
00425     EIGEN_DEVICE_FUNC
00426     void swap(PlainObjectBase<OtherDerived>& other)
00427     {
00428       eigen_assert(rows()==other.rows() && cols()==other.cols());
00429       call_assignment(derived(), other.derived(), internal::swap_assign_op<Scalar>());
00430     }
00431 
00432     EIGEN_DEVICE_FUNC inline const NestByValue<Derived> nestByValue() const;
00433     EIGEN_DEVICE_FUNC inline const ForceAlignedAccess<Derived> forceAlignedAccess() const;
00434     EIGEN_DEVICE_FUNC inline ForceAlignedAccess<Derived> forceAlignedAccess();
00435     template<bool Enable> EIGEN_DEVICE_FUNC
00436     inline const typename internal::conditional<Enable,ForceAlignedAccess<Derived>,Derived&>::type forceAlignedAccessIf() const;
00437     template<bool Enable> EIGEN_DEVICE_FUNC
00438     inline typename internal::conditional<Enable,ForceAlignedAccess<Derived>,Derived&>::type forceAlignedAccessIf();
00439 
00440     EIGEN_DEVICE_FUNC Scalar sum() const;
00441     EIGEN_DEVICE_FUNC Scalar mean() const;
00442     EIGEN_DEVICE_FUNC Scalar trace() const;
00443 
00444     EIGEN_DEVICE_FUNC Scalar prod() const;
00445 
00446     EIGEN_DEVICE_FUNC typename internal::traits<Derived>::Scalar minCoeff() const;
00447     EIGEN_DEVICE_FUNC typename internal::traits<Derived>::Scalar maxCoeff() const;
00448 
00449     template<typename IndexType> EIGEN_DEVICE_FUNC
00450     typename internal::traits<Derived>::Scalar minCoeff(IndexType* row, IndexType* col) const;
00451     template<typename IndexType> EIGEN_DEVICE_FUNC
00452     typename internal::traits<Derived>::Scalar maxCoeff(IndexType* row, IndexType* col) const;
00453     template<typename IndexType> EIGEN_DEVICE_FUNC
00454     typename internal::traits<Derived>::Scalar minCoeff(IndexType* index) const;
00455     template<typename IndexType> EIGEN_DEVICE_FUNC
00456     typename internal::traits<Derived>::Scalar maxCoeff(IndexType* index) const;
00457 
00458     template<typename BinaryOp>
00459     EIGEN_DEVICE_FUNC
00460     Scalar redux(const BinaryOp& func) const;
00461 
00462     template<typename Visitor>
00463     EIGEN_DEVICE_FUNC
00464     void visit(Visitor& func) const;
00465 
00473     inline const WithFormat<Derived> format(const IOFormat& fmt) const
00474     {
00475       return WithFormat<Derived>(derived(), fmt);
00476     }
00477 
00479     EIGEN_DEVICE_FUNC
00480     CoeffReturnType value() const
00481     {
00482       EIGEN_STATIC_ASSERT_SIZE_1x1(Derived)
00483       eigen_assert(this->rows() == 1 && this->cols() == 1);
00484       return derived().coeff(0,0);
00485     }
00486 
00487     bool all() const;
00488     bool any() const;
00489     Index count() const;
00490 
00491     typedef VectorwiseOp<Derived, Horizontal> RowwiseReturnType;
00492     typedef const VectorwiseOp<const Derived, Horizontal> ConstRowwiseReturnType;
00493     typedef VectorwiseOp<Derived, Vertical> ColwiseReturnType;
00494     typedef const VectorwiseOp<const Derived, Vertical> ConstColwiseReturnType;
00495 
00503     //Code moved here due to a CUDA compiler bug
00504     EIGEN_DEVICE_FUNC inline ConstRowwiseReturnType rowwise() const {
00505       return ConstRowwiseReturnType(derived());
00506     }
00507     EIGEN_DEVICE_FUNC RowwiseReturnType rowwise();
00508 
00516     EIGEN_DEVICE_FUNC inline ConstColwiseReturnType colwise() const {
00517       return ConstColwiseReturnType(derived());
00518     }
00519     EIGEN_DEVICE_FUNC ColwiseReturnType colwise();
00520 
00521     typedef CwiseNullaryOp<internal::scalar_random_op<Scalar>,PlainObject> RandomReturnType;
00522     static const RandomReturnType Random(Index rows, Index cols);
00523     static const RandomReturnType Random(Index size);
00524     static const RandomReturnType Random();
00525 
00526     template<typename ThenDerived,typename ElseDerived>
00527     const Select<Derived,ThenDerived,ElseDerived>
00528     select(const DenseBase<ThenDerived>& thenMatrix,
00529            const DenseBase<ElseDerived>& elseMatrix) const;
00530 
00531     template<typename ThenDerived>
00532     inline const Select<Derived,ThenDerived, typename ThenDerived::ConstantReturnType>
00533     select(const DenseBase<ThenDerived>& thenMatrix, const typename ThenDerived::Scalar& elseScalar) const;
00534 
00535     template<typename ElseDerived>
00536     inline const Select<Derived, typename ElseDerived::ConstantReturnType, ElseDerived >
00537     select(const typename ElseDerived::Scalar& thenScalar, const DenseBase<ElseDerived>& elseMatrix) const;
00538 
00539     template<int p> RealScalar lpNorm() const;
00540 
00541     template<int RowFactor, int ColFactor>
00542     EIGEN_DEVICE_FUNC
00543     const Replicate<Derived,RowFactor,ColFactor> replicate() const;
00552     //Code moved here due to a CUDA compiler bug
00553     EIGEN_DEVICE_FUNC
00554     const Replicate<Derived, Dynamic, Dynamic> replicate(Index rowFactor, Index colFactor) const
00555     {
00556       return Replicate<Derived, Dynamic, Dynamic>(derived(), rowFactor, colFactor);
00557     }
00558 
00559     typedef Reverse<Derived, BothDirections> ReverseReturnType;
00560     typedef const Reverse<const Derived, BothDirections> ConstReverseReturnType;
00561     EIGEN_DEVICE_FUNC ReverseReturnType reverse();
00563     //Code moved here due to a CUDA compiler bug
00564     EIGEN_DEVICE_FUNC ConstReverseReturnType reverse() const
00565     {
00566       return ConstReverseReturnType(derived());
00567     }
00568     EIGEN_DEVICE_FUNC void reverseInPlace();
00569 
00570 #define EIGEN_CURRENT_STORAGE_BASE_CLASS Eigen::DenseBase
00571 #define EIGEN_DOC_BLOCK_ADDONS_NOT_INNER_PANEL
00572 #define EIGEN_DOC_BLOCK_ADDONS_INNER_PANEL_IF(COND)
00573 #   include "../plugins/BlockMethods.h"
00574 #   ifdef EIGEN_DENSEBASE_PLUGIN
00575 #     include EIGEN_DENSEBASE_PLUGIN
00576 #   endif
00577 #undef EIGEN_CURRENT_STORAGE_BASE_CLASS
00578 #undef EIGEN_DOC_BLOCK_ADDONS_NOT_INNER_PANEL
00579 #undef EIGEN_DOC_BLOCK_ADDONS_INNER_PANEL_IF
00580 
00581     // disable the use of evalTo for dense objects with a nice compilation error
00582     template<typename Dest>
00583     EIGEN_DEVICE_FUNC
00584     inline void evalTo(Dest& ) const
00585     {
00586       EIGEN_STATIC_ASSERT((internal::is_same<Dest,void>::value),THE_EVAL_EVALTO_FUNCTION_SHOULD_NEVER_BE_CALLED_FOR_DENSE_OBJECTS);
00587     }
00588 
00589   protected:
00591     EIGEN_DEVICE_FUNC DenseBase()
00592     {
00593       /* Just checks for self-consistency of the flags.
00594        * Only do it when debugging Eigen, as this borders on paranoiac and could slow compilation down
00595        */
00596 #ifdef EIGEN_INTERNAL_DEBUGGING
00597       EIGEN_STATIC_ASSERT((EIGEN_IMPLIES(MaxRowsAtCompileTime==1 && MaxColsAtCompileTime!=1, int(IsRowMajor))
00598                         && EIGEN_IMPLIES(MaxColsAtCompileTime==1 && MaxRowsAtCompileTime!=1, int(!IsRowMajor))),
00599                           INVALID_STORAGE_ORDER_FOR_THIS_VECTOR_EXPRESSION)
00600 #endif
00601     }
00602 
00603   private:
00604     EIGEN_DEVICE_FUNC explicit DenseBase(int);
00605     EIGEN_DEVICE_FUNC DenseBase(int,int);
00606     template<typename OtherDerived> EIGEN_DEVICE_FUNC explicit DenseBase(const DenseBase<OtherDerived>&);
00607 };
00608 
00609 } // end namespace Eigen
00610 
00611 #endif // EIGEN_DENSEBASE_H
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends