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 // Eigen is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 3 of the License, or (at your option) any later version.
00010 //
00011 // Alternatively, you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License as
00013 // published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00017 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00018 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License and a copy of the GNU General Public License along with
00023 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00024 
00025 #ifndef EIGEN_CWISE_NULLARY_OP_H
00026 #define EIGEN_CWISE_NULLARY_OP_H
00027 
00046 namespace internal {
00047 template<typename NullaryOp, typename PlainObjectType>
00048 struct traits<CwiseNullaryOp<NullaryOp, PlainObjectType> > : traits<PlainObjectType>
00049 {
00050   enum {
00051     Flags = (traits<PlainObjectType>::Flags
00052       & (  HereditaryBits
00053          | (functor_has_linear_access<NullaryOp>::ret ? LinearAccessBit : 0)
00054          | (functor_traits<NullaryOp>::PacketAccess ? PacketAccessBit : 0)))
00055       | (functor_traits<NullaryOp>::IsRepeatable ? 0 : EvalBeforeNestingBit),
00056     CoeffReadCost = functor_traits<NullaryOp>::Cost
00057   };
00058 };
00059 }
00060 
00061 template<typename NullaryOp, typename PlainObjectType>
00062 class CwiseNullaryOp : internal::no_assignment_operator,
00063   public internal::dense_xpr_base< CwiseNullaryOp<NullaryOp, PlainObjectType> >::type
00064 {
00065   public:
00066 
00067     typedef typename internal::dense_xpr_base<CwiseNullaryOp>::type Base;
00068     EIGEN_DENSE_PUBLIC_INTERFACE(CwiseNullaryOp)
00069 
00070     CwiseNullaryOp(Index rows, Index cols, const NullaryOp& func = NullaryOp())
00071       : m_rows(rows), m_cols(cols), m_functor(func)
00072     {
00073       eigen_assert(rows >= 0
00074             && (RowsAtCompileTime == Dynamic || RowsAtCompileTime == rows)
00075             &&  cols >= 0
00076             && (ColsAtCompileTime == Dynamic || ColsAtCompileTime == cols));
00077     }
00078 
00079     EIGEN_STRONG_INLINE Index rows() const { return m_rows.value(); }
00080     EIGEN_STRONG_INLINE Index cols() const { return m_cols.value(); }
00081 
00082     EIGEN_STRONG_INLINE const Scalar coeff(Index rows, Index cols) const
00083     {
00084       return m_functor(rows, cols);
00085     }
00086 
00087     template<int LoadMode>
00088     EIGEN_STRONG_INLINE PacketScalar packet(Index row, Index col) const
00089     {
00090       return m_functor.packetOp(row, col);
00091     }
00092 
00093     EIGEN_STRONG_INLINE const Scalar coeff(Index index) const
00094     {
00095       return m_functor(index);
00096     }
00097 
00098     template<int LoadMode>
00099     EIGEN_STRONG_INLINE PacketScalar packet(Index index) const
00100     {
00101       return m_functor.packetOp(index);
00102     }
00103 
00104   protected:
00105     const internal::variable_if_dynamic<Index, RowsAtCompileTime> m_rows;
00106     const internal::variable_if_dynamic<Index, ColsAtCompileTime> m_cols;
00107     const NullaryOp m_functor;
00108 };
00109 
00110 
00124 template<typename Derived>
00125 template<typename CustomNullaryOp>
00126 EIGEN_STRONG_INLINE const CwiseNullaryOp<CustomNullaryOp, Derived>
00127 DenseBase<Derived>::NullaryExpr(Index rows, Index cols, const CustomNullaryOp& func)
00128 {
00129   return CwiseNullaryOp<CustomNullaryOp, Derived>(rows, cols, func);
00130 }
00131 
00147 template<typename Derived>
00148 template<typename CustomNullaryOp>
00149 EIGEN_STRONG_INLINE const CwiseNullaryOp<CustomNullaryOp, Derived>
00150 DenseBase<Derived>::NullaryExpr(Index size, const CustomNullaryOp& func)
00151 {
00152   EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
00153   if(RowsAtCompileTime == 1) return CwiseNullaryOp<CustomNullaryOp, Derived>(1, size, func);
00154   else return CwiseNullaryOp<CustomNullaryOp, Derived>(size, 1, func);
00155 }
00156 
00166 template<typename Derived>
00167 template<typename CustomNullaryOp>
00168 EIGEN_STRONG_INLINE const CwiseNullaryOp<CustomNullaryOp, Derived>
00169 DenseBase<Derived>::NullaryExpr(const CustomNullaryOp& func)
00170 {
00171   return CwiseNullaryOp<CustomNullaryOp, Derived>(RowsAtCompileTime, ColsAtCompileTime, func);
00172 }
00173 
00187 template<typename Derived>
00188 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
00189 DenseBase<Derived>::Constant(Index rows, Index cols, const Scalar& value)
00190 {
00191   return DenseBase<Derived>::NullaryExpr(rows, cols, internal::scalar_constant_op<Scalar>(value));
00192 }
00193 
00209 template<typename Derived>
00210 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
00211 DenseBase<Derived>::Constant(Index size, const Scalar& value)
00212 {
00213   return DenseBase<Derived>::NullaryExpr(size, internal::scalar_constant_op<Scalar>(value));
00214 }
00215 
00225 template<typename Derived>
00226 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
00227 DenseBase<Derived>::Constant(const Scalar& value)
00228 {
00229   EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
00230   return DenseBase<Derived>::NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, internal::scalar_constant_op<Scalar>(value));
00231 }
00232 
00248 template<typename Derived>
00249 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::SequentialLinSpacedReturnType
00250 DenseBase<Derived>::LinSpaced(Sequential_t, Index size, const Scalar& low, const Scalar& high)
00251 {
00252   EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
00253   return DenseBase<Derived>::NullaryExpr(size, internal::linspaced_op<Scalar,false>(low,high,size));
00254 }
00255 
00260 template<typename Derived>
00261 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::SequentialLinSpacedReturnType
00262 DenseBase<Derived>::LinSpaced(Sequential_t, const Scalar& low, const Scalar& high)
00263 {
00264   EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
00265   EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
00266   return DenseBase<Derived>::NullaryExpr(Derived::SizeAtCompileTime, internal::linspaced_op<Scalar,false>(low,high,Derived::SizeAtCompileTime));
00267 }
00268 
00281 template<typename Derived>
00282 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
00283 DenseBase<Derived>::LinSpaced(Index size, const Scalar& low, const Scalar& high)
00284 {
00285   EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
00286   return DenseBase<Derived>::NullaryExpr(size, internal::linspaced_op<Scalar,true>(low,high,size));
00287 }
00288 
00293 template<typename Derived>
00294 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
00295 DenseBase<Derived>::LinSpaced(const Scalar& low, const Scalar& high)
00296 {
00297   EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
00298   EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
00299   return DenseBase<Derived>::NullaryExpr(Derived::SizeAtCompileTime, internal::linspaced_op<Scalar,true>(low,high,Derived::SizeAtCompileTime));
00300 }
00301 
00303 template<typename Derived>
00304 bool DenseBase<Derived>::isApproxToConstant
00305 (const Scalar& value, RealScalar prec) const
00306 {
00307   for(Index j = 0; j < cols(); ++j)
00308     for(Index i = 0; i < rows(); ++i)
00309       if(!internal::isApprox(this->coeff(i, j), value, prec))
00310         return false;
00311   return true;
00312 }
00313 
00317 template<typename Derived>
00318 bool DenseBase<Derived>::isConstant
00319 (const Scalar& value, RealScalar prec) const
00320 {
00321   return isApproxToConstant(value, prec);
00322 }
00323 
00328 template<typename Derived>
00329 EIGEN_STRONG_INLINE void DenseBase<Derived>::fill(const Scalar& value)
00330 {
00331   setConstant(value);
00332 }
00333 
00338 template<typename Derived>
00339 EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setConstant(const Scalar& value)
00340 {
00341   return derived() = Constant(rows(), cols(), value);
00342 }
00343 
00353 template<typename Derived>
00354 EIGEN_STRONG_INLINE Derived&
00355 PlainObjectBase<Derived>::setConstant(Index size, const Scalar& value)
00356 {
00357   resize(size);
00358   return setConstant(value);
00359 }
00360 
00372 template<typename Derived>
00373 EIGEN_STRONG_INLINE Derived&
00374 PlainObjectBase<Derived>::setConstant(Index rows, Index cols, const Scalar& value)
00375 {
00376   resize(rows, cols);
00377   return setConstant(value);
00378 }
00379 
00392 template<typename Derived>
00393 EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setLinSpaced(Index size, const Scalar& low, const Scalar& high)
00394 {
00395   EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
00396   return derived() = Derived::NullaryExpr(size, internal::linspaced_op<Scalar,false>(low,high,size));
00397 }
00398 
00399 // zero:
00400 
00415 template<typename Derived>
00416 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
00417 DenseBase<Derived>::Zero(Index rows, Index cols)
00418 {
00419   return Constant(rows, cols, Scalar(0));
00420 }
00421 
00438 template<typename Derived>
00439 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
00440 DenseBase<Derived>::Zero(Index size)
00441 {
00442   return Constant(size, Scalar(0));
00443 }
00444 
00455 template<typename Derived>
00456 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
00457 DenseBase<Derived>::Zero()
00458 {
00459   return Constant(Scalar(0));
00460 }
00461 
00470 template<typename Derived>
00471 bool DenseBase<Derived>::isZero(RealScalar prec) const
00472 {
00473   for(Index j = 0; j < cols(); ++j)
00474     for(Index i = 0; i < rows(); ++i)
00475       if(!internal::isMuchSmallerThan(this->coeff(i, j), static_cast<Scalar>(1), prec))
00476         return false;
00477   return true;
00478 }
00479 
00487 template<typename Derived>
00488 EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setZero()
00489 {
00490   return setConstant(Scalar(0));
00491 }
00492 
00502 template<typename Derived>
00503 EIGEN_STRONG_INLINE Derived&
00504 PlainObjectBase<Derived>::setZero(Index size)
00505 {
00506   resize(size);
00507   return setConstant(Scalar(0));
00508 }
00509 
00520 template<typename Derived>
00521 EIGEN_STRONG_INLINE Derived&
00522 PlainObjectBase<Derived>::setZero(Index rows, Index cols)
00523 {
00524   resize(rows, cols);
00525   return setConstant(Scalar(0));
00526 }
00527 
00528 // ones:
00529 
00544 template<typename Derived>
00545 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
00546 DenseBase<Derived>::Ones(Index rows, Index cols)
00547 {
00548   return Constant(rows, cols, Scalar(1));
00549 }
00550 
00567 template<typename Derived>
00568 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
00569 DenseBase<Derived>::Ones(Index size)
00570 {
00571   return Constant(size, Scalar(1));
00572 }
00573 
00584 template<typename Derived>
00585 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
00586 DenseBase<Derived>::Ones()
00587 {
00588   return Constant(Scalar(1));
00589 }
00590 
00599 template<typename Derived>
00600 bool DenseBase<Derived>::isOnes
00601 (RealScalar prec) const
00602 {
00603   return isApproxToConstant(Scalar(1), prec);
00604 }
00605 
00613 template<typename Derived>
00614 EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setOnes()
00615 {
00616   return setConstant(Scalar(1));
00617 }
00618 
00628 template<typename Derived>
00629 EIGEN_STRONG_INLINE Derived&
00630 PlainObjectBase<Derived>::setOnes(Index size)
00631 {
00632   resize(size);
00633   return setConstant(Scalar(1));
00634 }
00635 
00646 template<typename Derived>
00647 EIGEN_STRONG_INLINE Derived&
00648 PlainObjectBase<Derived>::setOnes(Index rows, Index cols)
00649 {
00650   resize(rows, cols);
00651   return setConstant(Scalar(1));
00652 }
00653 
00654 // Identity:
00655 
00670 template<typename Derived>
00671 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::IdentityReturnType
00672 MatrixBase<Derived>::Identity(Index rows, Index cols)
00673 {
00674   return DenseBase<Derived>::NullaryExpr(rows, cols, internal::scalar_identity_op<Scalar>());
00675 }
00676 
00687 template<typename Derived>
00688 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::IdentityReturnType
00689 MatrixBase<Derived>::Identity()
00690 {
00691   EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
00692   return MatrixBase<Derived>::NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, internal::scalar_identity_op<Scalar>());
00693 }
00694 
00704 template<typename Derived>
00705 bool MatrixBase<Derived>::isIdentity
00706 (RealScalar prec) const
00707 {
00708   for(Index j = 0; j < cols(); ++j)
00709   {
00710     for(Index i = 0; i < rows(); ++i)
00711     {
00712       if(i == j)
00713       {
00714         if(!internal::isApprox(this->coeff(i, j), static_cast<Scalar>(1), prec))
00715           return false;
00716       }
00717       else
00718       {
00719         if(!internal::isMuchSmallerThan(this->coeff(i, j), static_cast<RealScalar>(1), prec))
00720           return false;
00721       }
00722     }
00723   }
00724   return true;
00725 }
00726 
00727 namespace internal {
00728 
00729 template<typename Derived, bool Big = (Derived::SizeAtCompileTime>=16)>
00730 struct setIdentity_impl
00731 {
00732   static EIGEN_STRONG_INLINE Derived& run(Derived& m)
00733   {
00734     return m = Derived::Identity(m.rows(), m.cols());
00735   }
00736 };
00737 
00738 template<typename Derived>
00739 struct setIdentity_impl<Derived, true>
00740 {
00741   typedef typename Derived::Index Index;
00742   static EIGEN_STRONG_INLINE Derived& run(Derived& m)
00743   {
00744     m.setZero();
00745     const Index size = std::min(m.rows(), m.cols());
00746     for(Index i = 0; i < size; ++i) m.coeffRef(i,i) = typename Derived::Scalar(1);
00747     return m;
00748   }
00749 };
00750 
00751 } // end namespace internal
00752 
00760 template<typename Derived>
00761 EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity()
00762 {
00763   return internal::setIdentity_impl<Derived>::run(derived());
00764 }
00765 
00776 template<typename Derived>
00777 EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity(Index rows, Index cols)
00778 {
00779   derived().resize(rows, cols);
00780   return setIdentity();
00781 }
00782 
00789 template<typename Derived>
00790 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::Unit(Index size, Index i)
00791 {
00792   EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
00793   return BasisReturnType(SquareMatrixType::Identity(size,size), i);
00794 }
00795 
00804 template<typename Derived>
00805 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::Unit(Index i)
00806 {
00807   EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
00808   return BasisReturnType(SquareMatrixType::Identity(),i);
00809 }
00810 
00817 template<typename Derived>
00818 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitX()
00819 { return Derived::Unit(0); }
00820 
00827 template<typename Derived>
00828 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitY()
00829 { return Derived::Unit(1); }
00830 
00837 template<typename Derived>
00838 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitZ()
00839 { return Derived::Unit(2); }
00840 
00847 template<typename Derived>
00848 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitW()
00849 { return Derived::Unit(3); }
00850 
00851 #endif // EIGEN_CWISE_NULLARY_OP_H


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:31:00