CwiseNullaryOp.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) 2008-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 //
00006 // This Source Code Form is subject to the terms of the Mozilla
00007 // Public License v. 2.0. If a copy of the MPL was not distributed
00008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
00009 
00010 #ifndef EIGEN_CWISE_NULLARY_OP_H
00011 #define EIGEN_CWISE_NULLARY_OP_H
00012 
00013 namespace Eigen {
00014 
00033 namespace internal {
00034 template<typename NullaryOp, typename PlainObjectType>
00035 struct traits<CwiseNullaryOp<NullaryOp, PlainObjectType> > : traits<PlainObjectType>
00036 {
00037   enum {
00038     Flags = (traits<PlainObjectType>::Flags
00039       & (  HereditaryBits
00040          | (functor_has_linear_access<NullaryOp>::ret ? LinearAccessBit : 0)
00041          | (functor_traits<NullaryOp>::PacketAccess ? PacketAccessBit : 0)))
00042       | (functor_traits<NullaryOp>::IsRepeatable ? 0 : EvalBeforeNestingBit),
00043     CoeffReadCost = functor_traits<NullaryOp>::Cost
00044   };
00045 };
00046 }
00047 
00048 template<typename NullaryOp, typename PlainObjectType>
00049 class CwiseNullaryOp : internal::no_assignment_operator,
00050   public internal::dense_xpr_base< CwiseNullaryOp<NullaryOp, PlainObjectType> >::type
00051 {
00052   public:
00053 
00054     typedef typename internal::dense_xpr_base<CwiseNullaryOp>::type Base;
00055     EIGEN_DENSE_PUBLIC_INTERFACE(CwiseNullaryOp)
00056 
00057     CwiseNullaryOp(Index nbRows, Index nbCols, const NullaryOp& func = NullaryOp())
00058       : m_rows(nbRows), m_cols(nbCols), m_functor(func)
00059     {
00060       eigen_assert(nbRows >= 0
00061             && (RowsAtCompileTime == Dynamic || RowsAtCompileTime == nbRows)
00062             &&  nbCols >= 0
00063             && (ColsAtCompileTime == Dynamic || ColsAtCompileTime == nbCols));
00064     }
00065 
00066     EIGEN_STRONG_INLINE Index rows() const { return m_rows.value(); }
00067     EIGEN_STRONG_INLINE Index cols() const { return m_cols.value(); }
00068 
00069     EIGEN_STRONG_INLINE const Scalar coeff(Index rowId, Index colId) const
00070     {
00071       return m_functor(rowId, colId);
00072     }
00073 
00074     template<int LoadMode>
00075     EIGEN_STRONG_INLINE PacketScalar packet(Index rowId, Index colId) const
00076     {
00077       return m_functor.packetOp(rowId, colId);
00078     }
00079 
00080     EIGEN_STRONG_INLINE const Scalar coeff(Index index) const
00081     {
00082       return m_functor(index);
00083     }
00084 
00085     template<int LoadMode>
00086     EIGEN_STRONG_INLINE PacketScalar packet(Index index) const
00087     {
00088       return m_functor.packetOp(index);
00089     }
00090 
00092     const NullaryOp& functor() const { return m_functor; }
00093 
00094   protected:
00095     const internal::variable_if_dynamic<Index, RowsAtCompileTime> m_rows;
00096     const internal::variable_if_dynamic<Index, ColsAtCompileTime> m_cols;
00097     const NullaryOp m_functor;
00098 };
00099 
00100 
00114 template<typename Derived>
00115 template<typename CustomNullaryOp>
00116 EIGEN_STRONG_INLINE const CwiseNullaryOp<CustomNullaryOp, Derived>
00117 DenseBase<Derived>::NullaryExpr(Index rows, Index cols, const CustomNullaryOp& func)
00118 {
00119   return CwiseNullaryOp<CustomNullaryOp, Derived>(rows, cols, func);
00120 }
00121 
00137 template<typename Derived>
00138 template<typename CustomNullaryOp>
00139 EIGEN_STRONG_INLINE const CwiseNullaryOp<CustomNullaryOp, Derived>
00140 DenseBase<Derived>::NullaryExpr(Index size, const CustomNullaryOp& func)
00141 {
00142   EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
00143   if(RowsAtCompileTime == 1) return CwiseNullaryOp<CustomNullaryOp, Derived>(1, size, func);
00144   else return CwiseNullaryOp<CustomNullaryOp, Derived>(size, 1, func);
00145 }
00146 
00156 template<typename Derived>
00157 template<typename CustomNullaryOp>
00158 EIGEN_STRONG_INLINE const CwiseNullaryOp<CustomNullaryOp, Derived>
00159 DenseBase<Derived>::NullaryExpr(const CustomNullaryOp& func)
00160 {
00161   return CwiseNullaryOp<CustomNullaryOp, Derived>(RowsAtCompileTime, ColsAtCompileTime, func);
00162 }
00163 
00177 template<typename Derived>
00178 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
00179 DenseBase<Derived>::Constant(Index nbRows, Index nbCols, const Scalar& value)
00180 {
00181   return DenseBase<Derived>::NullaryExpr(nbRows, nbCols, internal::scalar_constant_op<Scalar>(value));
00182 }
00183 
00199 template<typename Derived>
00200 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
00201 DenseBase<Derived>::Constant(Index size, const Scalar& value)
00202 {
00203   return DenseBase<Derived>::NullaryExpr(size, internal::scalar_constant_op<Scalar>(value));
00204 }
00205 
00215 template<typename Derived>
00216 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
00217 DenseBase<Derived>::Constant(const Scalar& value)
00218 {
00219   EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
00220   return DenseBase<Derived>::NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, internal::scalar_constant_op<Scalar>(value));
00221 }
00222 
00240 template<typename Derived>
00241 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::SequentialLinSpacedReturnType
00242 DenseBase<Derived>::LinSpaced(Sequential_t, Index size, const Scalar& low, const Scalar& high)
00243 {
00244   EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
00245   return DenseBase<Derived>::NullaryExpr(size, internal::linspaced_op<Scalar,false>(low,high,size));
00246 }
00247 
00252 template<typename Derived>
00253 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::SequentialLinSpacedReturnType
00254 DenseBase<Derived>::LinSpaced(Sequential_t, const Scalar& low, const Scalar& high)
00255 {
00256   EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
00257   EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
00258   return DenseBase<Derived>::NullaryExpr(Derived::SizeAtCompileTime, internal::linspaced_op<Scalar,false>(low,high,Derived::SizeAtCompileTime));
00259 }
00260 
00274 template<typename Derived>
00275 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
00276 DenseBase<Derived>::LinSpaced(Index size, const Scalar& low, const Scalar& high)
00277 {
00278   EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
00279   return DenseBase<Derived>::NullaryExpr(size, internal::linspaced_op<Scalar,true>(low,high,size));
00280 }
00281 
00286 template<typename Derived>
00287 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
00288 DenseBase<Derived>::LinSpaced(const Scalar& low, const Scalar& high)
00289 {
00290   EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
00291   EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
00292   return DenseBase<Derived>::NullaryExpr(Derived::SizeAtCompileTime, internal::linspaced_op<Scalar,true>(low,high,Derived::SizeAtCompileTime));
00293 }
00294 
00296 template<typename Derived>
00297 bool DenseBase<Derived>::isApproxToConstant
00298 (const Scalar& val, const RealScalar& prec) const
00299 {
00300   for(Index j = 0; j < cols(); ++j)
00301     for(Index i = 0; i < rows(); ++i)
00302       if(!internal::isApprox(this->coeff(i, j), val, prec))
00303         return false;
00304   return true;
00305 }
00306 
00310 template<typename Derived>
00311 bool DenseBase<Derived>::isConstant
00312 (const Scalar& val, const RealScalar& prec) const
00313 {
00314   return isApproxToConstant(val, prec);
00315 }
00316 
00321 template<typename Derived>
00322 EIGEN_STRONG_INLINE void DenseBase<Derived>::fill(const Scalar& val)
00323 {
00324   setConstant(val);
00325 }
00326 
00331 template<typename Derived>
00332 EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setConstant(const Scalar& val)
00333 {
00334   return derived() = Constant(rows(), cols(), val);
00335 }
00336 
00346 template<typename Derived>
00347 EIGEN_STRONG_INLINE Derived&
00348 PlainObjectBase<Derived>::setConstant(Index size, const Scalar& val)
00349 {
00350   resize(size);
00351   return setConstant(val);
00352 }
00353 
00365 template<typename Derived>
00366 EIGEN_STRONG_INLINE Derived&
00367 PlainObjectBase<Derived>::setConstant(Index nbRows, Index nbCols, const Scalar& val)
00368 {
00369   resize(nbRows, nbCols);
00370   return setConstant(val);
00371 }
00372 
00386 template<typename Derived>
00387 EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setLinSpaced(Index newSize, const Scalar& low, const Scalar& high)
00388 {
00389   EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
00390   return derived() = Derived::NullaryExpr(newSize, internal::linspaced_op<Scalar,false>(low,high,newSize));
00391 }
00392 
00403 template<typename Derived>
00404 EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setLinSpaced(const Scalar& low, const Scalar& high)
00405 {
00406   EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
00407   return setLinSpaced(size(), low, high);
00408 }
00409 
00410 // zero:
00411 
00426 template<typename Derived>
00427 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
00428 DenseBase<Derived>::Zero(Index nbRows, Index nbCols)
00429 {
00430   return Constant(nbRows, nbCols, Scalar(0));
00431 }
00432 
00449 template<typename Derived>
00450 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
00451 DenseBase<Derived>::Zero(Index size)
00452 {
00453   return Constant(size, Scalar(0));
00454 }
00455 
00466 template<typename Derived>
00467 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
00468 DenseBase<Derived>::Zero()
00469 {
00470   return Constant(Scalar(0));
00471 }
00472 
00481 template<typename Derived>
00482 bool DenseBase<Derived>::isZero(const RealScalar& prec) const
00483 {
00484   for(Index j = 0; j < cols(); ++j)
00485     for(Index i = 0; i < rows(); ++i)
00486       if(!internal::isMuchSmallerThan(this->coeff(i, j), static_cast<Scalar>(1), prec))
00487         return false;
00488   return true;
00489 }
00490 
00498 template<typename Derived>
00499 EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setZero()
00500 {
00501   return setConstant(Scalar(0));
00502 }
00503 
00513 template<typename Derived>
00514 EIGEN_STRONG_INLINE Derived&
00515 PlainObjectBase<Derived>::setZero(Index newSize)
00516 {
00517   resize(newSize);
00518   return setConstant(Scalar(0));
00519 }
00520 
00531 template<typename Derived>
00532 EIGEN_STRONG_INLINE Derived&
00533 PlainObjectBase<Derived>::setZero(Index nbRows, Index nbCols)
00534 {
00535   resize(nbRows, nbCols);
00536   return setConstant(Scalar(0));
00537 }
00538 
00539 // ones:
00540 
00555 template<typename Derived>
00556 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
00557 DenseBase<Derived>::Ones(Index nbRows, Index nbCols)
00558 {
00559   return Constant(nbRows, nbCols, Scalar(1));
00560 }
00561 
00578 template<typename Derived>
00579 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
00580 DenseBase<Derived>::Ones(Index newSize)
00581 {
00582   return Constant(newSize, Scalar(1));
00583 }
00584 
00595 template<typename Derived>
00596 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
00597 DenseBase<Derived>::Ones()
00598 {
00599   return Constant(Scalar(1));
00600 }
00601 
00610 template<typename Derived>
00611 bool DenseBase<Derived>::isOnes
00612 (const RealScalar& prec) const
00613 {
00614   return isApproxToConstant(Scalar(1), prec);
00615 }
00616 
00624 template<typename Derived>
00625 EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setOnes()
00626 {
00627   return setConstant(Scalar(1));
00628 }
00629 
00639 template<typename Derived>
00640 EIGEN_STRONG_INLINE Derived&
00641 PlainObjectBase<Derived>::setOnes(Index newSize)
00642 {
00643   resize(newSize);
00644   return setConstant(Scalar(1));
00645 }
00646 
00657 template<typename Derived>
00658 EIGEN_STRONG_INLINE Derived&
00659 PlainObjectBase<Derived>::setOnes(Index nbRows, Index nbCols)
00660 {
00661   resize(nbRows, nbCols);
00662   return setConstant(Scalar(1));
00663 }
00664 
00665 // Identity:
00666 
00681 template<typename Derived>
00682 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::IdentityReturnType
00683 MatrixBase<Derived>::Identity(Index nbRows, Index nbCols)
00684 {
00685   return DenseBase<Derived>::NullaryExpr(nbRows, nbCols, internal::scalar_identity_op<Scalar>());
00686 }
00687 
00698 template<typename Derived>
00699 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::IdentityReturnType
00700 MatrixBase<Derived>::Identity()
00701 {
00702   EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
00703   return MatrixBase<Derived>::NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, internal::scalar_identity_op<Scalar>());
00704 }
00705 
00715 template<typename Derived>
00716 bool MatrixBase<Derived>::isIdentity
00717 (const RealScalar& prec) const
00718 {
00719   for(Index j = 0; j < cols(); ++j)
00720   {
00721     for(Index i = 0; i < rows(); ++i)
00722     {
00723       if(i == j)
00724       {
00725         if(!internal::isApprox(this->coeff(i, j), static_cast<Scalar>(1), prec))
00726           return false;
00727       }
00728       else
00729       {
00730         if(!internal::isMuchSmallerThan(this->coeff(i, j), static_cast<RealScalar>(1), prec))
00731           return false;
00732       }
00733     }
00734   }
00735   return true;
00736 }
00737 
00738 namespace internal {
00739 
00740 template<typename Derived, bool Big = (Derived::SizeAtCompileTime>=16)>
00741 struct setIdentity_impl
00742 {
00743   static EIGEN_STRONG_INLINE Derived& run(Derived& m)
00744   {
00745     return m = Derived::Identity(m.rows(), m.cols());
00746   }
00747 };
00748 
00749 template<typename Derived>
00750 struct setIdentity_impl<Derived, true>
00751 {
00752   typedef typename Derived::Index Index;
00753   static EIGEN_STRONG_INLINE Derived& run(Derived& m)
00754   {
00755     m.setZero();
00756     const Index size = (std::min)(m.rows(), m.cols());
00757     for(Index i = 0; i < size; ++i) m.coeffRef(i,i) = typename Derived::Scalar(1);
00758     return m;
00759   }
00760 };
00761 
00762 } // end namespace internal
00763 
00771 template<typename Derived>
00772 EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity()
00773 {
00774   return internal::setIdentity_impl<Derived>::run(derived());
00775 }
00776 
00787 template<typename Derived>
00788 EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity(Index nbRows, Index nbCols)
00789 {
00790   derived().resize(nbRows, nbCols);
00791   return setIdentity();
00792 }
00793 
00800 template<typename Derived>
00801 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::Unit(Index newSize, Index i)
00802 {
00803   EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
00804   return BasisReturnType(SquareMatrixType::Identity(newSize,newSize), i);
00805 }
00806 
00815 template<typename Derived>
00816 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::Unit(Index i)
00817 {
00818   EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
00819   return BasisReturnType(SquareMatrixType::Identity(),i);
00820 }
00821 
00828 template<typename Derived>
00829 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitX()
00830 { return Derived::Unit(0); }
00831 
00838 template<typename Derived>
00839 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitY()
00840 { return Derived::Unit(1); }
00841 
00848 template<typename Derived>
00849 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitZ()
00850 { return Derived::Unit(2); }
00851 
00858 template<typename Derived>
00859 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitW()
00860 { return Derived::Unit(3); }
00861 
00862 } // end namespace Eigen
00863 
00864 #endif // EIGEN_CWISE_NULLARY_OP_H


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Sat Jun 8 2019 19:36:56