Main MRPT website > C++ reference
MRPT logo

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 
00028 /** \class CwiseNullaryOp
00029   * \ingroup Core_Module
00030   *
00031   * \brief Generic expression of a matrix where all coefficients are defined by a functor
00032   *
00033   * \param NullaryOp template functor implementing the operator
00034   * \param PlainObjectType the underlying plain matrix/array type
00035   *
00036   * This class represents an expression of a generic nullary operator.
00037   * It is the return type of the Ones(), Zero(), Constant(), Identity() and Random() methods,
00038   * and most of the time this is the only way it is used.
00039   *
00040   * However, if you want to write a function returning such an expression, you
00041   * will need to use this class.
00042   *
00043   * \sa class CwiseUnaryOp, class CwiseBinaryOp, DenseBase::NullaryExpr()
00044   */
00045 
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 
00111 /** \returns an expression of a matrix defined by a custom functor \a func
00112   *
00113   * The parameters \a rows and \a cols are the number of rows and of columns of
00114   * the returned matrix. Must be compatible with this MatrixBase type.
00115   *
00116   * This variant is meant to be used for dynamic-size matrix types. For fixed-size types,
00117   * it is redundant to pass \a rows and \a cols as arguments, so Zero() should be used
00118   * instead.
00119   *
00120   * The template parameter \a CustomNullaryOp is the type of the functor.
00121   *
00122   * \sa class CwiseNullaryOp
00123   */
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 
00132 /** \returns an expression of a matrix defined by a custom functor \a func
00133   *
00134   * The parameter \a size is the size of the returned vector.
00135   * Must be compatible with this MatrixBase type.
00136   *
00137   * \only_for_vectors
00138   *
00139   * This variant is meant to be used for dynamic-size vector types. For fixed-size types,
00140   * it is redundant to pass \a size as argument, so Zero() should be used
00141   * instead.
00142   *
00143   * The template parameter \a CustomNullaryOp is the type of the functor.
00144   *
00145   * \sa class CwiseNullaryOp
00146   */
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 
00157 /** \returns an expression of a matrix defined by a custom functor \a func
00158   *
00159   * This variant is only for fixed-size DenseBase types. For dynamic-size types, you
00160   * need to use the variants taking size arguments.
00161   *
00162   * The template parameter \a CustomNullaryOp is the type of the functor.
00163   *
00164   * \sa class CwiseNullaryOp
00165   */
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 
00174 /** \returns an expression of a constant matrix of value \a value
00175   *
00176   * The parameters \a rows and \a cols are the number of rows and of columns of
00177   * the returned matrix. Must be compatible with this DenseBase type.
00178   *
00179   * This variant is meant to be used for dynamic-size matrix types. For fixed-size types,
00180   * it is redundant to pass \a rows and \a cols as arguments, so Zero() should be used
00181   * instead.
00182   *
00183   * The template parameter \a CustomNullaryOp is the type of the functor.
00184   *
00185   * \sa class CwiseNullaryOp
00186   */
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 
00194 /** \returns an expression of a constant matrix of value \a value
00195   *
00196   * The parameter \a size is the size of the returned vector.
00197   * Must be compatible with this DenseBase type.
00198   *
00199   * \only_for_vectors
00200   *
00201   * This variant is meant to be used for dynamic-size vector types. For fixed-size types,
00202   * it is redundant to pass \a size as argument, so Zero() should be used
00203   * instead.
00204   *
00205   * The template parameter \a CustomNullaryOp is the type of the functor.
00206   *
00207   * \sa class CwiseNullaryOp
00208   */
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 
00216 /** \returns an expression of a constant matrix of value \a value
00217   *
00218   * This variant is only for fixed-size DenseBase types. For dynamic-size types, you
00219   * need to use the variants taking size arguments.
00220   *
00221   * The template parameter \a CustomNullaryOp is the type of the functor.
00222   *
00223   * \sa class CwiseNullaryOp
00224   */
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 
00233 /**
00234   * \brief Sets a linearly space vector.
00235   *
00236   * The function generates 'size' equally spaced values in the closed interval [low,high].
00237   * This particular version of LinSpaced() uses sequential access, i.e. vector access is
00238   * assumed to be a(0), a(1), ..., a(size). This assumption allows for better vectorization
00239   * and yields faster code than the random access version.
00240   *
00241   * \only_for_vectors
00242   *
00243   * Example: \include DenseBase_LinSpaced_seq.cpp
00244   * Output: \verbinclude DenseBase_LinSpaced_seq.out
00245   *
00246   * \sa setLinSpaced(Index,const Scalar&,const Scalar&), LinSpaced(Index,Scalar,Scalar), CwiseNullaryOp
00247   */
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 
00256 /**
00257   * \copydoc DenseBase::LinSpaced(Sequential_t, Index, const Scalar&, const Scalar&)
00258   * Special version for fixed size types which does not require the size parameter.
00259   */
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 
00269 /**
00270   * \brief Sets a linearly space vector.
00271   *
00272   * The function generates 'size' equally spaced values in the closed interval [low,high].
00273   *
00274   * \only_for_vectors
00275   *
00276   * Example: \include DenseBase_LinSpaced.cpp
00277   * Output: \verbinclude DenseBase_LinSpaced.out
00278   *
00279   * \sa setLinSpaced(Index,const Scalar&,const Scalar&), LinSpaced(Sequential_t,Index,const Scalar&,const Scalar&,Index), CwiseNullaryOp
00280   */
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 
00289 /**
00290   * \copydoc DenseBase::LinSpaced(Index, const Scalar&, const Scalar&)
00291   * Special version for fixed size types which does not require the size parameter.
00292   */
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 
00302 /** \returns true if all coefficients in this matrix are approximately equal to \a value, to within precision \a prec */
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 
00314 /** This is just an alias for isApproxToConstant().
00315   *
00316   * \returns true if all coefficients in this matrix are approximately equal to \a value, to within precision \a prec */
00317 template<typename Derived>
00318 bool DenseBase<Derived>::isConstant
00319 (const Scalar& value, RealScalar prec) const
00320 {
00321   return isApproxToConstant(value, prec);
00322 }
00323 
00324 /** Alias for setConstant(): sets all coefficients in this expression to \a value.
00325   *
00326   * \sa setConstant(), Constant(), class CwiseNullaryOp
00327   */
00328 template<typename Derived>
00329 EIGEN_STRONG_INLINE void DenseBase<Derived>::fill(const Scalar& value)
00330 {
00331   setConstant(value);
00332 }
00333 
00334 /** Sets all coefficients in this expression to \a value.
00335   *
00336   * \sa fill(), setConstant(Index,const Scalar&), setConstant(Index,Index,const Scalar&), setZero(), setOnes(), Constant(), class CwiseNullaryOp, setZero(), setOnes()
00337   */
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 
00344 /** Resizes to the given \a size, and sets all coefficients in this expression to the given \a value.
00345   *
00346   * \only_for_vectors
00347   *
00348   * Example: \include Matrix_setConstant_int.cpp
00349   * Output: \verbinclude Matrix_setConstant_int.out
00350   *
00351   * \sa MatrixBase::setConstant(const Scalar&), setConstant(Index,Index,const Scalar&), class CwiseNullaryOp, MatrixBase::Constant(const Scalar&)
00352   */
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 
00361 /** Resizes to the given size, and sets all coefficients in this expression to the given \a value.
00362   *
00363   * \param rows the new number of rows
00364   * \param cols the new number of columns
00365   * \param value the value to which all coefficients are set
00366   *
00367   * Example: \include Matrix_setConstant_int_int.cpp
00368   * Output: \verbinclude Matrix_setConstant_int_int.out
00369   *
00370   * \sa MatrixBase::setConstant(const Scalar&), setConstant(Index,const Scalar&), class CwiseNullaryOp, MatrixBase::Constant(const Scalar&)
00371   */
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 
00380 /**
00381   * \brief Sets a linearly space vector.
00382   *
00383   * The function generates 'size' equally spaced values in the closed interval [low,high].
00384   *
00385   * \only_for_vectors
00386   *
00387   * Example: \include DenseBase_setLinSpaced.cpp
00388   * Output: \verbinclude DenseBase_setLinSpaced.out
00389   *
00390   * \sa CwiseNullaryOp
00391   */
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 
00401 /** \returns an expression of a zero matrix.
00402   *
00403   * The parameters \a rows and \a cols are the number of rows and of columns of
00404   * the returned matrix. Must be compatible with this MatrixBase type.
00405   *
00406   * This variant is meant to be used for dynamic-size matrix types. For fixed-size types,
00407   * it is redundant to pass \a rows and \a cols as arguments, so Zero() should be used
00408   * instead.
00409   *
00410   * Example: \include MatrixBase_zero_int_int.cpp
00411   * Output: \verbinclude MatrixBase_zero_int_int.out
00412   *
00413   * \sa Zero(), Zero(Index)
00414   */
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 
00422 /** \returns an expression of a zero vector.
00423   *
00424   * The parameter \a size is the size of the returned vector.
00425   * Must be compatible with this MatrixBase type.
00426   *
00427   * \only_for_vectors
00428   *
00429   * This variant is meant to be used for dynamic-size vector types. For fixed-size types,
00430   * it is redundant to pass \a size as argument, so Zero() should be used
00431   * instead.
00432   *
00433   * Example: \include MatrixBase_zero_int.cpp
00434   * Output: \verbinclude MatrixBase_zero_int.out
00435   *
00436   * \sa Zero(), Zero(Index,Index)
00437   */
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 
00445 /** \returns an expression of a fixed-size zero matrix or vector.
00446   *
00447   * This variant is only for fixed-size MatrixBase types. For dynamic-size types, you
00448   * need to use the variants taking size arguments.
00449   *
00450   * Example: \include MatrixBase_zero.cpp
00451   * Output: \verbinclude MatrixBase_zero.out
00452   *
00453   * \sa Zero(Index), Zero(Index,Index)
00454   */
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 
00462 /** \returns true if *this is approximately equal to the zero matrix,
00463   *          within the precision given by \a prec.
00464   *
00465   * Example: \include MatrixBase_isZero.cpp
00466   * Output: \verbinclude MatrixBase_isZero.out
00467   *
00468   * \sa class CwiseNullaryOp, Zero()
00469   */
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 
00480 /** Sets all coefficients in this expression to zero.
00481   *
00482   * Example: \include MatrixBase_setZero.cpp
00483   * Output: \verbinclude MatrixBase_setZero.out
00484   *
00485   * \sa class CwiseNullaryOp, Zero()
00486   */
00487 template<typename Derived>
00488 EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setZero()
00489 {
00490   return setConstant(Scalar(0));
00491 }
00492 
00493 /** Resizes to the given \a size, and sets all coefficients in this expression to zero.
00494   *
00495   * \only_for_vectors
00496   *
00497   * Example: \include Matrix_setZero_int.cpp
00498   * Output: \verbinclude Matrix_setZero_int.out
00499   *
00500   * \sa DenseBase::setZero(), setZero(Index,Index), class CwiseNullaryOp, DenseBase::Zero()
00501   */
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 
00510 /** Resizes to the given size, and sets all coefficients in this expression to zero.
00511   *
00512   * \param rows the new number of rows
00513   * \param cols the new number of columns
00514   *
00515   * Example: \include Matrix_setZero_int_int.cpp
00516   * Output: \verbinclude Matrix_setZero_int_int.out
00517   *
00518   * \sa DenseBase::setZero(), setZero(Index), class CwiseNullaryOp, DenseBase::Zero()
00519   */
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 
00530 /** \returns an expression of a matrix where all coefficients equal one.
00531   *
00532   * The parameters \a rows and \a cols are the number of rows and of columns of
00533   * the returned matrix. Must be compatible with this MatrixBase type.
00534   *
00535   * This variant is meant to be used for dynamic-size matrix types. For fixed-size types,
00536   * it is redundant to pass \a rows and \a cols as arguments, so Ones() should be used
00537   * instead.
00538   *
00539   * Example: \include MatrixBase_ones_int_int.cpp
00540   * Output: \verbinclude MatrixBase_ones_int_int.out
00541   *
00542   * \sa Ones(), Ones(Index), isOnes(), class Ones
00543   */
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 
00551 /** \returns an expression of a vector where all coefficients equal one.
00552   *
00553   * The parameter \a size is the size of the returned vector.
00554   * Must be compatible with this MatrixBase type.
00555   *
00556   * \only_for_vectors
00557   *
00558   * This variant is meant to be used for dynamic-size vector types. For fixed-size types,
00559   * it is redundant to pass \a size as argument, so Ones() should be used
00560   * instead.
00561   *
00562   * Example: \include MatrixBase_ones_int.cpp
00563   * Output: \verbinclude MatrixBase_ones_int.out
00564   *
00565   * \sa Ones(), Ones(Index,Index), isOnes(), class Ones
00566   */
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 
00574 /** \returns an expression of a fixed-size matrix or vector where all coefficients equal one.
00575   *
00576   * This variant is only for fixed-size MatrixBase types. For dynamic-size types, you
00577   * need to use the variants taking size arguments.
00578   *
00579   * Example: \include MatrixBase_ones.cpp
00580   * Output: \verbinclude MatrixBase_ones.out
00581   *
00582   * \sa Ones(Index), Ones(Index,Index), isOnes(), class Ones
00583   */
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 
00591 /** \returns true if *this is approximately equal to the matrix where all coefficients
00592   *          are equal to 1, within the precision given by \a prec.
00593   *
00594   * Example: \include MatrixBase_isOnes.cpp
00595   * Output: \verbinclude MatrixBase_isOnes.out
00596   *
00597   * \sa class CwiseNullaryOp, Ones()
00598   */
00599 template<typename Derived>
00600 bool DenseBase<Derived>::isOnes
00601 (RealScalar prec) const
00602 {
00603   return isApproxToConstant(Scalar(1), prec);
00604 }
00605 
00606 /** Sets all coefficients in this expression to one.
00607   *
00608   * Example: \include MatrixBase_setOnes.cpp
00609   * Output: \verbinclude MatrixBase_setOnes.out
00610   *
00611   * \sa class CwiseNullaryOp, Ones()
00612   */
00613 template<typename Derived>
00614 EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setOnes()
00615 {
00616   return setConstant(Scalar(1));
00617 }
00618 
00619 /** Resizes to the given \a size, and sets all coefficients in this expression to one.
00620   *
00621   * \only_for_vectors
00622   *
00623   * Example: \include Matrix_setOnes_int.cpp
00624   * Output: \verbinclude Matrix_setOnes_int.out
00625   *
00626   * \sa MatrixBase::setOnes(), setOnes(Index,Index), class CwiseNullaryOp, MatrixBase::Ones()
00627   */
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 
00636 /** Resizes to the given size, and sets all coefficients in this expression to one.
00637   *
00638   * \param rows the new number of rows
00639   * \param cols the new number of columns
00640   *
00641   * Example: \include Matrix_setOnes_int_int.cpp
00642   * Output: \verbinclude Matrix_setOnes_int_int.out
00643   *
00644   * \sa MatrixBase::setOnes(), setOnes(Index), class CwiseNullaryOp, MatrixBase::Ones()
00645   */
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 
00656 /** \returns an expression of the identity matrix (not necessarily square).
00657   *
00658   * The parameters \a rows and \a cols are the number of rows and of columns of
00659   * the returned matrix. Must be compatible with this MatrixBase type.
00660   *
00661   * This variant is meant to be used for dynamic-size matrix types. For fixed-size types,
00662   * it is redundant to pass \a rows and \a cols as arguments, so Identity() should be used
00663   * instead.
00664   *
00665   * Example: \include MatrixBase_identity_int_int.cpp
00666   * Output: \verbinclude MatrixBase_identity_int_int.out
00667   *
00668   * \sa Identity(), setIdentity(), isIdentity()
00669   */
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 
00677 /** \returns an expression of the identity matrix (not necessarily square).
00678   *
00679   * This variant is only for fixed-size MatrixBase types. For dynamic-size types, you
00680   * need to use the variant taking size arguments.
00681   *
00682   * Example: \include MatrixBase_identity.cpp
00683   * Output: \verbinclude MatrixBase_identity.out
00684   *
00685   * \sa Identity(Index,Index), setIdentity(), isIdentity()
00686   */
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 
00695 /** \returns true if *this is approximately equal to the identity matrix
00696   *          (not necessarily square),
00697   *          within the precision given by \a prec.
00698   *
00699   * Example: \include MatrixBase_isIdentity.cpp
00700   * Output: \verbinclude MatrixBase_isIdentity.out
00701   *
00702   * \sa class CwiseNullaryOp, Identity(), Identity(Index,Index), setIdentity()
00703   */
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 
00753 /** Writes the identity expression (not necessarily square) into *this.
00754   *
00755   * Example: \include MatrixBase_setIdentity.cpp
00756   * Output: \verbinclude MatrixBase_setIdentity.out
00757   *
00758   * \sa class CwiseNullaryOp, Identity(), Identity(Index,Index), isIdentity()
00759   */
00760 template<typename Derived>
00761 EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity()
00762 {
00763   return internal::setIdentity_impl<Derived>::run(derived());
00764 }
00765 
00766 /** \brief Resizes to the given size, and writes the identity expression (not necessarily square) into *this.
00767   *
00768   * \param rows the new number of rows
00769   * \param cols the new number of columns
00770   *
00771   * Example: \include Matrix_setIdentity_int_int.cpp
00772   * Output: \verbinclude Matrix_setIdentity_int_int.out
00773   *
00774   * \sa MatrixBase::setIdentity(), class CwiseNullaryOp, MatrixBase::Identity()
00775   */
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 
00783 /** \returns an expression of the i-th unit (basis) vector.
00784   *
00785   * \only_for_vectors
00786   *
00787   * \sa MatrixBase::Unit(Index), MatrixBase::UnitX(), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW()
00788   */
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 
00796 /** \returns an expression of the i-th unit (basis) vector.
00797   *
00798   * \only_for_vectors
00799   *
00800   * This variant is for fixed-size vector only.
00801   *
00802   * \sa MatrixBase::Unit(Index,Index), MatrixBase::UnitX(), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW()
00803   */
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 
00811 /** \returns an expression of the X axis unit vector (1{,0}^*)
00812   *
00813   * \only_for_vectors
00814   *
00815   * \sa MatrixBase::Unit(Index,Index), MatrixBase::Unit(Index), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW()
00816   */
00817 template<typename Derived>
00818 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitX()
00819 { return Derived::Unit(0); }
00820 
00821 /** \returns an expression of the Y axis unit vector (0,1{,0}^*)
00822   *
00823   * \only_for_vectors
00824   *
00825   * \sa MatrixBase::Unit(Index,Index), MatrixBase::Unit(Index), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW()
00826   */
00827 template<typename Derived>
00828 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitY()
00829 { return Derived::Unit(1); }
00830 
00831 /** \returns an expression of the Z axis unit vector (0,0,1{,0}^*)
00832   *
00833   * \only_for_vectors
00834   *
00835   * \sa MatrixBase::Unit(Index,Index), MatrixBase::Unit(Index), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW()
00836   */
00837 template<typename Derived>
00838 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitZ()
00839 { return Derived::Unit(2); }
00840 
00841 /** \returns an expression of the W axis unit vector (0,0,0,1)
00842   *
00843   * \only_for_vectors
00844   *
00845   * \sa MatrixBase::Unit(Index,Index), MatrixBase::Unit(Index), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW()
00846   */
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



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN: at Sat Mar 26 06:16:28 UTC 2011