Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:56:12

0001 // This file is part of Eigen, a lightweight C++ template library
0002 // for linear algebra.
0003 //
0004 // Copyright (C) 2008-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
0005 //
0006 // This Source Code Form is subject to the terms of the Mozilla
0007 // Public License v. 2.0. If a copy of the MPL was not distributed
0008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
0009 
0010 #ifndef EIGEN_CWISE_NULLARY_OP_H
0011 #define EIGEN_CWISE_NULLARY_OP_H
0012 
0013 namespace Eigen {
0014 
0015 namespace internal {
0016 template<typename NullaryOp, typename PlainObjectType>
0017 struct traits<CwiseNullaryOp<NullaryOp, PlainObjectType> > : traits<PlainObjectType>
0018 {
0019   enum {
0020     Flags = traits<PlainObjectType>::Flags & RowMajorBit
0021   };
0022 };
0023 
0024 } // namespace internal
0025 
0026 /** \class CwiseNullaryOp
0027   * \ingroup Core_Module
0028   *
0029   * \brief Generic expression of a matrix where all coefficients are defined by a functor
0030   *
0031   * \tparam NullaryOp template functor implementing the operator
0032   * \tparam PlainObjectType the underlying plain matrix/array type
0033   *
0034   * This class represents an expression of a generic nullary operator.
0035   * It is the return type of the Ones(), Zero(), Constant(), Identity() and Random() methods,
0036   * and most of the time this is the only way it is used.
0037   *
0038   * However, if you want to write a function returning such an expression, you
0039   * will need to use this class.
0040   *
0041   * The functor NullaryOp must expose one of the following method:
0042     <table class="manual">
0043     <tr            ><td>\c operator()() </td><td>if the procedural generation does not depend on the coefficient entries (e.g., random numbers)</td></tr>
0044     <tr class="alt"><td>\c operator()(Index i)</td><td>if the procedural generation makes sense for vectors only and that it depends on the coefficient index \c i (e.g., linspace) </td></tr>
0045     <tr            ><td>\c operator()(Index i,Index j)</td><td>if the procedural generation depends on the matrix coordinates \c i, \c j (e.g., to generate a checkerboard with 0 and 1)</td></tr>
0046     </table>
0047   * It is also possible to expose the last two operators if the generation makes sense for matrices but can be optimized for vectors.
0048   *
0049   * See DenseBase::NullaryExpr(Index,const CustomNullaryOp&) for an example binding
0050   * C++11 random number generators.
0051   *
0052   * A nullary expression can also be used to implement custom sophisticated matrix manipulations
0053   * that cannot be covered by the existing set of natively supported matrix manipulations.
0054   * See this \ref TopicCustomizing_NullaryExpr "page" for some examples and additional explanations
0055   * on the behavior of CwiseNullaryOp.
0056   *
0057   * \sa class CwiseUnaryOp, class CwiseBinaryOp, DenseBase::NullaryExpr
0058   */
0059 template<typename NullaryOp, typename PlainObjectType>
0060 class CwiseNullaryOp : public internal::dense_xpr_base< CwiseNullaryOp<NullaryOp, PlainObjectType> >::type, internal::no_assignment_operator
0061 {
0062   public:
0063 
0064     typedef typename internal::dense_xpr_base<CwiseNullaryOp>::type Base;
0065     EIGEN_DENSE_PUBLIC_INTERFACE(CwiseNullaryOp)
0066 
0067     EIGEN_DEVICE_FUNC
0068     CwiseNullaryOp(Index rows, Index cols, const NullaryOp& func = NullaryOp())
0069       : m_rows(rows), m_cols(cols), m_functor(func)
0070     {
0071       eigen_assert(rows >= 0
0072             && (RowsAtCompileTime == Dynamic || RowsAtCompileTime == rows)
0073             &&  cols >= 0
0074             && (ColsAtCompileTime == Dynamic || ColsAtCompileTime == cols));
0075     }
0076 
0077     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR
0078     Index rows() const { return m_rows.value(); }
0079     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR
0080     Index cols() const { return m_cols.value(); }
0081 
0082     /** \returns the functor representing the nullary operation */
0083     EIGEN_DEVICE_FUNC
0084     const NullaryOp& functor() const { return m_functor; }
0085 
0086   protected:
0087     const internal::variable_if_dynamic<Index, RowsAtCompileTime> m_rows;
0088     const internal::variable_if_dynamic<Index, ColsAtCompileTime> m_cols;
0089     const NullaryOp m_functor;
0090 };
0091 
0092 
0093 /** \returns an expression of a matrix defined by a custom functor \a func
0094   *
0095   * The parameters \a rows and \a cols are the number of rows and of columns of
0096   * the returned matrix. Must be compatible with this MatrixBase type.
0097   *
0098   * This variant is meant to be used for dynamic-size matrix types. For fixed-size types,
0099   * it is redundant to pass \a rows and \a cols as arguments, so Zero() should be used
0100   * instead.
0101   *
0102   * The template parameter \a CustomNullaryOp is the type of the functor.
0103   *
0104   * \sa class CwiseNullaryOp
0105   */
0106 template<typename Derived>
0107 template<typename CustomNullaryOp>
0108 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
0109 #ifndef EIGEN_PARSED_BY_DOXYGEN
0110 const CwiseNullaryOp<CustomNullaryOp,typename DenseBase<Derived>::PlainObject>
0111 #else
0112 const CwiseNullaryOp<CustomNullaryOp,PlainObject>
0113 #endif
0114 DenseBase<Derived>::NullaryExpr(Index rows, Index cols, const CustomNullaryOp& func)
0115 {
0116   return CwiseNullaryOp<CustomNullaryOp, PlainObject>(rows, cols, func);
0117 }
0118 
0119 /** \returns an expression of a matrix defined by a custom functor \a func
0120   *
0121   * The parameter \a size is the size of the returned vector.
0122   * Must be compatible with this MatrixBase type.
0123   *
0124   * \only_for_vectors
0125   *
0126   * This variant is meant to be used for dynamic-size vector types. For fixed-size types,
0127   * it is redundant to pass \a size as argument, so Zero() should be used
0128   * instead.
0129   *
0130   * The template parameter \a CustomNullaryOp is the type of the functor.
0131   *
0132   * Here is an example with C++11 random generators: \include random_cpp11.cpp
0133   * Output: \verbinclude random_cpp11.out
0134   *
0135   * \sa class CwiseNullaryOp
0136   */
0137 template<typename Derived>
0138 template<typename CustomNullaryOp>
0139 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
0140 #ifndef EIGEN_PARSED_BY_DOXYGEN
0141 const CwiseNullaryOp<CustomNullaryOp, typename DenseBase<Derived>::PlainObject>
0142 #else
0143 const CwiseNullaryOp<CustomNullaryOp, PlainObject>
0144 #endif
0145 DenseBase<Derived>::NullaryExpr(Index size, const CustomNullaryOp& func)
0146 {
0147   EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
0148   if(RowsAtCompileTime == 1) return CwiseNullaryOp<CustomNullaryOp, PlainObject>(1, size, func);
0149   else return CwiseNullaryOp<CustomNullaryOp, PlainObject>(size, 1, func);
0150 }
0151 
0152 /** \returns an expression of a matrix defined by a custom functor \a func
0153   *
0154   * This variant is only for fixed-size DenseBase types. For dynamic-size types, you
0155   * need to use the variants taking size arguments.
0156   *
0157   * The template parameter \a CustomNullaryOp is the type of the functor.
0158   *
0159   * \sa class CwiseNullaryOp
0160   */
0161 template<typename Derived>
0162 template<typename CustomNullaryOp>
0163 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
0164 #ifndef EIGEN_PARSED_BY_DOXYGEN
0165 const CwiseNullaryOp<CustomNullaryOp, typename DenseBase<Derived>::PlainObject>
0166 #else
0167 const CwiseNullaryOp<CustomNullaryOp, PlainObject>
0168 #endif
0169 DenseBase<Derived>::NullaryExpr(const CustomNullaryOp& func)
0170 {
0171   return CwiseNullaryOp<CustomNullaryOp, PlainObject>(RowsAtCompileTime, ColsAtCompileTime, func);
0172 }
0173 
0174 /** \returns an expression of a constant matrix of value \a value
0175   *
0176   * The parameters \a rows and \a cols are the number of rows and of columns of
0177   * the returned matrix. Must be compatible with this DenseBase type.
0178   *
0179   * This variant is meant to be used for dynamic-size matrix types. For fixed-size types,
0180   * it is redundant to pass \a rows and \a cols as arguments, so Zero() should be used
0181   * instead.
0182   *
0183   * The template parameter \a CustomNullaryOp is the type of the functor.
0184   *
0185   * \sa class CwiseNullaryOp
0186   */
0187 template<typename Derived>
0188 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
0189 DenseBase<Derived>::Constant(Index rows, Index cols, const Scalar& value)
0190 {
0191   return DenseBase<Derived>::NullaryExpr(rows, cols, internal::scalar_constant_op<Scalar>(value));
0192 }
0193 
0194 /** \returns an expression of a constant matrix of value \a value
0195   *
0196   * The parameter \a size is the size of the returned vector.
0197   * Must be compatible with this DenseBase type.
0198   *
0199   * \only_for_vectors
0200   *
0201   * This variant is meant to be used for dynamic-size vector types. For fixed-size types,
0202   * it is redundant to pass \a size as argument, so Zero() should be used
0203   * instead.
0204   *
0205   * The template parameter \a CustomNullaryOp is the type of the functor.
0206   *
0207   * \sa class CwiseNullaryOp
0208   */
0209 template<typename Derived>
0210 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
0211 DenseBase<Derived>::Constant(Index size, const Scalar& value)
0212 {
0213   return DenseBase<Derived>::NullaryExpr(size, internal::scalar_constant_op<Scalar>(value));
0214 }
0215 
0216 /** \returns an expression of a constant matrix of value \a value
0217   *
0218   * This variant is only for fixed-size DenseBase types. For dynamic-size types, you
0219   * need to use the variants taking size arguments.
0220   *
0221   * The template parameter \a CustomNullaryOp is the type of the functor.
0222   *
0223   * \sa class CwiseNullaryOp
0224   */
0225 template<typename Derived>
0226 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
0227 DenseBase<Derived>::Constant(const Scalar& value)
0228 {
0229   EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
0230   return DenseBase<Derived>::NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, internal::scalar_constant_op<Scalar>(value));
0231 }
0232 
0233 /** \deprecated because of accuracy loss. In Eigen 3.3, it is an alias for LinSpaced(Index,const Scalar&,const Scalar&)
0234   *
0235   * \only_for_vectors
0236   *
0237   * Example: \include DenseBase_LinSpaced_seq_deprecated.cpp
0238   * Output: \verbinclude DenseBase_LinSpaced_seq_deprecated.out
0239   *
0240   * \sa LinSpaced(Index,const Scalar&, const Scalar&), setLinSpaced(Index,const Scalar&,const Scalar&)
0241   */
0242 template<typename Derived>
0243 EIGEN_DEPRECATED EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
0244 DenseBase<Derived>::LinSpaced(Sequential_t, Index size, const Scalar& low, const Scalar& high)
0245 {
0246   EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
0247   return DenseBase<Derived>::NullaryExpr(size, internal::linspaced_op<Scalar>(low,high,size));
0248 }
0249 
0250 /** \deprecated because of accuracy loss. In Eigen 3.3, it is an alias for LinSpaced(const Scalar&,const Scalar&)
0251   *
0252   * \sa LinSpaced(const Scalar&, const Scalar&)
0253   */
0254 template<typename Derived>
0255 EIGEN_DEPRECATED EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
0256 DenseBase<Derived>::LinSpaced(Sequential_t, const Scalar& low, const Scalar& high)
0257 {
0258   EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
0259   EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
0260   return DenseBase<Derived>::NullaryExpr(Derived::SizeAtCompileTime, internal::linspaced_op<Scalar>(low,high,Derived::SizeAtCompileTime));
0261 }
0262 
0263 /**
0264   * \brief Sets a linearly spaced vector.
0265   *
0266   * The function generates 'size' equally spaced values in the closed interval [low,high].
0267   * When size is set to 1, a vector of length 1 containing 'high' is returned.
0268   *
0269   * \only_for_vectors
0270   *
0271   * Example: \include DenseBase_LinSpaced.cpp
0272   * Output: \verbinclude DenseBase_LinSpaced.out
0273   *
0274   * For integer scalar types, an even spacing is possible if and only if the length of the range,
0275   * i.e., \c high-low is a scalar multiple of \c size-1, or if \c size is a scalar multiple of the
0276   * number of values \c high-low+1 (meaning each value can be repeated the same number of time).
0277   * If one of these two considions is not satisfied, then \c high is lowered to the largest value
0278   * satisfying one of this constraint.
0279   * Here are some examples:
0280   *
0281   * Example: \include DenseBase_LinSpacedInt.cpp
0282   * Output: \verbinclude DenseBase_LinSpacedInt.out
0283   *
0284   * \sa setLinSpaced(Index,const Scalar&,const Scalar&), CwiseNullaryOp
0285   */
0286 template<typename Derived>
0287 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
0288 DenseBase<Derived>::LinSpaced(Index size, const Scalar& low, const Scalar& high)
0289 {
0290   EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
0291   return DenseBase<Derived>::NullaryExpr(size, internal::linspaced_op<Scalar>(low,high,size));
0292 }
0293 
0294 /**
0295   * \copydoc DenseBase::LinSpaced(Index, const Scalar&, const Scalar&)
0296   * Special version for fixed size types which does not require the size parameter.
0297   */
0298 template<typename Derived>
0299 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
0300 DenseBase<Derived>::LinSpaced(const Scalar& low, const Scalar& high)
0301 {
0302   EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
0303   EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
0304   return DenseBase<Derived>::NullaryExpr(Derived::SizeAtCompileTime, internal::linspaced_op<Scalar>(low,high,Derived::SizeAtCompileTime));
0305 }
0306 
0307 /** \returns true if all coefficients in this matrix are approximately equal to \a val, to within precision \a prec */
0308 template<typename Derived>
0309 EIGEN_DEVICE_FUNC bool DenseBase<Derived>::isApproxToConstant
0310 (const Scalar& val, const RealScalar& prec) const
0311 {
0312   typename internal::nested_eval<Derived,1>::type self(derived());
0313   for(Index j = 0; j < cols(); ++j)
0314     for(Index i = 0; i < rows(); ++i)
0315       if(!internal::isApprox(self.coeff(i, j), val, prec))
0316         return false;
0317   return true;
0318 }
0319 
0320 /** This is just an alias for isApproxToConstant().
0321   *
0322   * \returns true if all coefficients in this matrix are approximately equal to \a value, to within precision \a prec */
0323 template<typename Derived>
0324 EIGEN_DEVICE_FUNC bool DenseBase<Derived>::isConstant
0325 (const Scalar& val, const RealScalar& prec) const
0326 {
0327   return isApproxToConstant(val, prec);
0328 }
0329 
0330 /** Alias for setConstant(): sets all coefficients in this expression to \a val.
0331   *
0332   * \sa setConstant(), Constant(), class CwiseNullaryOp
0333   */
0334 template<typename Derived>
0335 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void DenseBase<Derived>::fill(const Scalar& val)
0336 {
0337   setConstant(val);
0338 }
0339 
0340 /** Sets all coefficients in this expression to value \a val.
0341   *
0342   * \sa fill(), setConstant(Index,const Scalar&), setConstant(Index,Index,const Scalar&), setZero(), setOnes(), Constant(), class CwiseNullaryOp, setZero(), setOnes()
0343   */
0344 template<typename Derived>
0345 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setConstant(const Scalar& val)
0346 {
0347   return derived() = Constant(rows(), cols(), val);
0348 }
0349 
0350 /** Resizes to the given \a size, and sets all coefficients in this expression to the given value \a val.
0351   *
0352   * \only_for_vectors
0353   *
0354   * Example: \include Matrix_setConstant_int.cpp
0355   * Output: \verbinclude Matrix_setConstant_int.out
0356   *
0357   * \sa MatrixBase::setConstant(const Scalar&), setConstant(Index,Index,const Scalar&), class CwiseNullaryOp, MatrixBase::Constant(const Scalar&)
0358   */
0359 template<typename Derived>
0360 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
0361 PlainObjectBase<Derived>::setConstant(Index size, const Scalar& val)
0362 {
0363   resize(size);
0364   return setConstant(val);
0365 }
0366 
0367 /** Resizes to the given size, and sets all coefficients in this expression to the given value \a val.
0368   *
0369   * \param rows the new number of rows
0370   * \param cols the new number of columns
0371   * \param val the value to which all coefficients are set
0372   *
0373   * Example: \include Matrix_setConstant_int_int.cpp
0374   * Output: \verbinclude Matrix_setConstant_int_int.out
0375   *
0376   * \sa MatrixBase::setConstant(const Scalar&), setConstant(Index,const Scalar&), class CwiseNullaryOp, MatrixBase::Constant(const Scalar&)
0377   */
0378 template<typename Derived>
0379 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
0380 PlainObjectBase<Derived>::setConstant(Index rows, Index cols, const Scalar& val)
0381 {
0382   resize(rows, cols);
0383   return setConstant(val);
0384 }
0385 
0386 /** Resizes to the given size, changing only the number of columns, and sets all
0387   * coefficients in this expression to the given value \a val. For the parameter
0388   * of type NoChange_t, just pass the special value \c NoChange.
0389   *
0390   * \sa MatrixBase::setConstant(const Scalar&), setConstant(Index,const Scalar&), class CwiseNullaryOp, MatrixBase::Constant(const Scalar&)
0391   */
0392 template<typename Derived>
0393 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
0394 PlainObjectBase<Derived>::setConstant(NoChange_t, Index cols, const Scalar& val)
0395 {
0396   return setConstant(rows(), cols, val);
0397 }
0398 
0399 /** Resizes to the given size, changing only the number of rows, and sets all
0400   * coefficients in this expression to the given value \a val. For the parameter
0401   * of type NoChange_t, just pass the special value \c NoChange.
0402   *
0403   * \sa MatrixBase::setConstant(const Scalar&), setConstant(Index,const Scalar&), class CwiseNullaryOp, MatrixBase::Constant(const Scalar&)
0404   */
0405 template<typename Derived>
0406 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
0407 PlainObjectBase<Derived>::setConstant(Index rows, NoChange_t, const Scalar& val)
0408 {
0409   return setConstant(rows, cols(), val);
0410 }
0411 
0412 
0413 /**
0414   * \brief Sets a linearly spaced vector.
0415   *
0416   * The function generates 'size' equally spaced values in the closed interval [low,high].
0417   * When size is set to 1, a vector of length 1 containing 'high' is returned.
0418   *
0419   * \only_for_vectors
0420   *
0421   * Example: \include DenseBase_setLinSpaced.cpp
0422   * Output: \verbinclude DenseBase_setLinSpaced.out
0423   *
0424   * For integer scalar types, do not miss the explanations on the definition
0425   * of \link LinSpaced(Index,const Scalar&,const Scalar&) even spacing \endlink.
0426   *
0427   * \sa LinSpaced(Index,const Scalar&,const Scalar&), CwiseNullaryOp
0428   */
0429 template<typename Derived>
0430 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setLinSpaced(Index newSize, const Scalar& low, const Scalar& high)
0431 {
0432   EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
0433   return derived() = Derived::NullaryExpr(newSize, internal::linspaced_op<Scalar>(low,high,newSize));
0434 }
0435 
0436 /**
0437   * \brief Sets a linearly spaced vector.
0438   *
0439   * The function fills \c *this with equally spaced values in the closed interval [low,high].
0440   * When size is set to 1, a vector of length 1 containing 'high' is returned.
0441   *
0442   * \only_for_vectors
0443   *
0444   * For integer scalar types, do not miss the explanations on the definition
0445   * of \link LinSpaced(Index,const Scalar&,const Scalar&) even spacing \endlink.
0446   *
0447   * \sa LinSpaced(Index,const Scalar&,const Scalar&), setLinSpaced(Index, const Scalar&, const Scalar&), CwiseNullaryOp
0448   */
0449 template<typename Derived>
0450 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setLinSpaced(const Scalar& low, const Scalar& high)
0451 {
0452   EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
0453   return setLinSpaced(size(), low, high);
0454 }
0455 
0456 // zero:
0457 
0458 /** \returns an expression of a zero matrix.
0459   *
0460   * The parameters \a rows and \a cols are the number of rows and of columns of
0461   * the returned matrix. Must be compatible with this MatrixBase type.
0462   *
0463   * This variant is meant to be used for dynamic-size matrix types. For fixed-size types,
0464   * it is redundant to pass \a rows and \a cols as arguments, so Zero() should be used
0465   * instead.
0466   *
0467   * Example: \include MatrixBase_zero_int_int.cpp
0468   * Output: \verbinclude MatrixBase_zero_int_int.out
0469   *
0470   * \sa Zero(), Zero(Index)
0471   */
0472 template<typename Derived>
0473 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
0474 DenseBase<Derived>::Zero(Index rows, Index cols)
0475 {
0476   return Constant(rows, cols, Scalar(0));
0477 }
0478 
0479 /** \returns an expression of a zero vector.
0480   *
0481   * The parameter \a size is the size of the returned vector.
0482   * Must be compatible with this MatrixBase type.
0483   *
0484   * \only_for_vectors
0485   *
0486   * This variant is meant to be used for dynamic-size vector types. For fixed-size types,
0487   * it is redundant to pass \a size as argument, so Zero() should be used
0488   * instead.
0489   *
0490   * Example: \include MatrixBase_zero_int.cpp
0491   * Output: \verbinclude MatrixBase_zero_int.out
0492   *
0493   * \sa Zero(), Zero(Index,Index)
0494   */
0495 template<typename Derived>
0496 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
0497 DenseBase<Derived>::Zero(Index size)
0498 {
0499   return Constant(size, Scalar(0));
0500 }
0501 
0502 /** \returns an expression of a fixed-size zero matrix or vector.
0503   *
0504   * This variant is only for fixed-size MatrixBase types. For dynamic-size types, you
0505   * need to use the variants taking size arguments.
0506   *
0507   * Example: \include MatrixBase_zero.cpp
0508   * Output: \verbinclude MatrixBase_zero.out
0509   *
0510   * \sa Zero(Index), Zero(Index,Index)
0511   */
0512 template<typename Derived>
0513 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
0514 DenseBase<Derived>::Zero()
0515 {
0516   return Constant(Scalar(0));
0517 }
0518 
0519 /** \returns true if *this is approximately equal to the zero matrix,
0520   *          within the precision given by \a prec.
0521   *
0522   * Example: \include MatrixBase_isZero.cpp
0523   * Output: \verbinclude MatrixBase_isZero.out
0524   *
0525   * \sa class CwiseNullaryOp, Zero()
0526   */
0527 template<typename Derived>
0528 EIGEN_DEVICE_FUNC bool DenseBase<Derived>::isZero(const RealScalar& prec) const
0529 {
0530   typename internal::nested_eval<Derived,1>::type self(derived());
0531   for(Index j = 0; j < cols(); ++j)
0532     for(Index i = 0; i < rows(); ++i)
0533       if(!internal::isMuchSmallerThan(self.coeff(i, j), static_cast<Scalar>(1), prec))
0534         return false;
0535   return true;
0536 }
0537 
0538 /** Sets all coefficients in this expression to zero.
0539   *
0540   * Example: \include MatrixBase_setZero.cpp
0541   * Output: \verbinclude MatrixBase_setZero.out
0542   *
0543   * \sa class CwiseNullaryOp, Zero()
0544   */
0545 template<typename Derived>
0546 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setZero()
0547 {
0548   return setConstant(Scalar(0));
0549 }
0550 
0551 /** Resizes to the given \a size, and sets all coefficients in this expression to zero.
0552   *
0553   * \only_for_vectors
0554   *
0555   * Example: \include Matrix_setZero_int.cpp
0556   * Output: \verbinclude Matrix_setZero_int.out
0557   *
0558   * \sa DenseBase::setZero(), setZero(Index,Index), class CwiseNullaryOp, DenseBase::Zero()
0559   */
0560 template<typename Derived>
0561 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
0562 PlainObjectBase<Derived>::setZero(Index newSize)
0563 {
0564   resize(newSize);
0565   return setConstant(Scalar(0));
0566 }
0567 
0568 /** Resizes to the given size, and sets all coefficients in this expression to zero.
0569   *
0570   * \param rows the new number of rows
0571   * \param cols the new number of columns
0572   *
0573   * Example: \include Matrix_setZero_int_int.cpp
0574   * Output: \verbinclude Matrix_setZero_int_int.out
0575   *
0576   * \sa DenseBase::setZero(), setZero(Index), class CwiseNullaryOp, DenseBase::Zero()
0577   */
0578 template<typename Derived>
0579 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
0580 PlainObjectBase<Derived>::setZero(Index rows, Index cols)
0581 {
0582   resize(rows, cols);
0583   return setConstant(Scalar(0));
0584 }
0585 
0586 /** Resizes to the given size, changing only the number of columns, and sets all
0587   * coefficients in this expression to zero. For the parameter of type NoChange_t,
0588   * just pass the special value \c NoChange.
0589   *
0590   * \sa DenseBase::setZero(), setZero(Index), setZero(Index, Index), setZero(Index, NoChange_t), class CwiseNullaryOp, DenseBase::Zero()
0591   */
0592 template<typename Derived>
0593 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
0594 PlainObjectBase<Derived>::setZero(NoChange_t, Index cols)
0595 {
0596   return setZero(rows(), cols);
0597 }
0598 
0599 /** Resizes to the given size, changing only the number of rows, and sets all
0600   * coefficients in this expression to zero. For the parameter of type NoChange_t,
0601   * just pass the special value \c NoChange.
0602   *
0603   * \sa DenseBase::setZero(), setZero(Index), setZero(Index, Index), setZero(NoChange_t, Index), class CwiseNullaryOp, DenseBase::Zero()
0604   */
0605 template<typename Derived>
0606 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
0607 PlainObjectBase<Derived>::setZero(Index rows, NoChange_t)
0608 {
0609   return setZero(rows, cols());
0610 }
0611 
0612 // ones:
0613 
0614 /** \returns an expression of a matrix where all coefficients equal one.
0615   *
0616   * The parameters \a rows and \a cols are the number of rows and of columns of
0617   * the returned matrix. Must be compatible with this MatrixBase type.
0618   *
0619   * This variant is meant to be used for dynamic-size matrix types. For fixed-size types,
0620   * it is redundant to pass \a rows and \a cols as arguments, so Ones() should be used
0621   * instead.
0622   *
0623   * Example: \include MatrixBase_ones_int_int.cpp
0624   * Output: \verbinclude MatrixBase_ones_int_int.out
0625   *
0626   * \sa Ones(), Ones(Index), isOnes(), class Ones
0627   */
0628 template<typename Derived>
0629 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
0630 DenseBase<Derived>::Ones(Index rows, Index cols)
0631 {
0632   return Constant(rows, cols, Scalar(1));
0633 }
0634 
0635 /** \returns an expression of a vector where all coefficients equal one.
0636   *
0637   * The parameter \a newSize is the size of the returned vector.
0638   * Must be compatible with this MatrixBase type.
0639   *
0640   * \only_for_vectors
0641   *
0642   * This variant is meant to be used for dynamic-size vector types. For fixed-size types,
0643   * it is redundant to pass \a size as argument, so Ones() should be used
0644   * instead.
0645   *
0646   * Example: \include MatrixBase_ones_int.cpp
0647   * Output: \verbinclude MatrixBase_ones_int.out
0648   *
0649   * \sa Ones(), Ones(Index,Index), isOnes(), class Ones
0650   */
0651 template<typename Derived>
0652 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
0653 DenseBase<Derived>::Ones(Index newSize)
0654 {
0655   return Constant(newSize, Scalar(1));
0656 }
0657 
0658 /** \returns an expression of a fixed-size matrix or vector where all coefficients equal one.
0659   *
0660   * This variant is only for fixed-size MatrixBase types. For dynamic-size types, you
0661   * need to use the variants taking size arguments.
0662   *
0663   * Example: \include MatrixBase_ones.cpp
0664   * Output: \verbinclude MatrixBase_ones.out
0665   *
0666   * \sa Ones(Index), Ones(Index,Index), isOnes(), class Ones
0667   */
0668 template<typename Derived>
0669 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
0670 DenseBase<Derived>::Ones()
0671 {
0672   return Constant(Scalar(1));
0673 }
0674 
0675 /** \returns true if *this is approximately equal to the matrix where all coefficients
0676   *          are equal to 1, within the precision given by \a prec.
0677   *
0678   * Example: \include MatrixBase_isOnes.cpp
0679   * Output: \verbinclude MatrixBase_isOnes.out
0680   *
0681   * \sa class CwiseNullaryOp, Ones()
0682   */
0683 template<typename Derived>
0684 EIGEN_DEVICE_FUNC bool DenseBase<Derived>::isOnes
0685 (const RealScalar& prec) const
0686 {
0687   return isApproxToConstant(Scalar(1), prec);
0688 }
0689 
0690 /** Sets all coefficients in this expression to one.
0691   *
0692   * Example: \include MatrixBase_setOnes.cpp
0693   * Output: \verbinclude MatrixBase_setOnes.out
0694   *
0695   * \sa class CwiseNullaryOp, Ones()
0696   */
0697 template<typename Derived>
0698 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setOnes()
0699 {
0700   return setConstant(Scalar(1));
0701 }
0702 
0703 /** Resizes to the given \a newSize, and sets all coefficients in this expression to one.
0704   *
0705   * \only_for_vectors
0706   *
0707   * Example: \include Matrix_setOnes_int.cpp
0708   * Output: \verbinclude Matrix_setOnes_int.out
0709   *
0710   * \sa MatrixBase::setOnes(), setOnes(Index,Index), class CwiseNullaryOp, MatrixBase::Ones()
0711   */
0712 template<typename Derived>
0713 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
0714 PlainObjectBase<Derived>::setOnes(Index newSize)
0715 {
0716   resize(newSize);
0717   return setConstant(Scalar(1));
0718 }
0719 
0720 /** Resizes to the given size, and sets all coefficients in this expression to one.
0721   *
0722   * \param rows the new number of rows
0723   * \param cols the new number of columns
0724   *
0725   * Example: \include Matrix_setOnes_int_int.cpp
0726   * Output: \verbinclude Matrix_setOnes_int_int.out
0727   *
0728   * \sa MatrixBase::setOnes(), setOnes(Index), class CwiseNullaryOp, MatrixBase::Ones()
0729   */
0730 template<typename Derived>
0731 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
0732 PlainObjectBase<Derived>::setOnes(Index rows, Index cols)
0733 {
0734   resize(rows, cols);
0735   return setConstant(Scalar(1));
0736 }
0737 
0738 /** Resizes to the given size, changing only the number of rows, and sets all
0739   * coefficients in this expression to one. For the parameter of type NoChange_t,
0740   * just pass the special value \c NoChange.
0741   *
0742  * \sa MatrixBase::setOnes(), setOnes(Index), setOnes(Index, Index), setOnes(NoChange_t, Index), class CwiseNullaryOp, MatrixBase::Ones()
0743   */
0744 template<typename Derived>
0745 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
0746 PlainObjectBase<Derived>::setOnes(Index rows, NoChange_t)
0747 {
0748   return setOnes(rows, cols());
0749 }
0750 
0751 /** Resizes to the given size, changing only the number of columns, and sets all
0752   * coefficients in this expression to one. For the parameter of type NoChange_t,
0753   * just pass the special value \c NoChange.
0754   *
0755  * \sa MatrixBase::setOnes(), setOnes(Index), setOnes(Index, Index), setOnes(Index, NoChange_t) class CwiseNullaryOp, MatrixBase::Ones()
0756   */
0757 template<typename Derived>
0758 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
0759 PlainObjectBase<Derived>::setOnes(NoChange_t, Index cols)
0760 {
0761   return setOnes(rows(), cols);
0762 }
0763 
0764 // Identity:
0765 
0766 /** \returns an expression of the identity matrix (not necessarily square).
0767   *
0768   * The parameters \a rows and \a cols are the number of rows and of columns of
0769   * the returned matrix. Must be compatible with this MatrixBase type.
0770   *
0771   * This variant is meant to be used for dynamic-size matrix types. For fixed-size types,
0772   * it is redundant to pass \a rows and \a cols as arguments, so Identity() should be used
0773   * instead.
0774   *
0775   * Example: \include MatrixBase_identity_int_int.cpp
0776   * Output: \verbinclude MatrixBase_identity_int_int.out
0777   *
0778   * \sa Identity(), setIdentity(), isIdentity()
0779   */
0780 template<typename Derived>
0781 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::IdentityReturnType
0782 MatrixBase<Derived>::Identity(Index rows, Index cols)
0783 {
0784   return DenseBase<Derived>::NullaryExpr(rows, cols, internal::scalar_identity_op<Scalar>());
0785 }
0786 
0787 /** \returns an expression of the identity matrix (not necessarily square).
0788   *
0789   * This variant is only for fixed-size MatrixBase types. For dynamic-size types, you
0790   * need to use the variant taking size arguments.
0791   *
0792   * Example: \include MatrixBase_identity.cpp
0793   * Output: \verbinclude MatrixBase_identity.out
0794   *
0795   * \sa Identity(Index,Index), setIdentity(), isIdentity()
0796   */
0797 template<typename Derived>
0798 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::IdentityReturnType
0799 MatrixBase<Derived>::Identity()
0800 {
0801   EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
0802   return MatrixBase<Derived>::NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, internal::scalar_identity_op<Scalar>());
0803 }
0804 
0805 /** \returns true if *this is approximately equal to the identity matrix
0806   *          (not necessarily square),
0807   *          within the precision given by \a prec.
0808   *
0809   * Example: \include MatrixBase_isIdentity.cpp
0810   * Output: \verbinclude MatrixBase_isIdentity.out
0811   *
0812   * \sa class CwiseNullaryOp, Identity(), Identity(Index,Index), setIdentity()
0813   */
0814 template<typename Derived>
0815 bool MatrixBase<Derived>::isIdentity
0816 (const RealScalar& prec) const
0817 {
0818   typename internal::nested_eval<Derived,1>::type self(derived());
0819   for(Index j = 0; j < cols(); ++j)
0820   {
0821     for(Index i = 0; i < rows(); ++i)
0822     {
0823       if(i == j)
0824       {
0825         if(!internal::isApprox(self.coeff(i, j), static_cast<Scalar>(1), prec))
0826           return false;
0827       }
0828       else
0829       {
0830         if(!internal::isMuchSmallerThan(self.coeff(i, j), static_cast<RealScalar>(1), prec))
0831           return false;
0832       }
0833     }
0834   }
0835   return true;
0836 }
0837 
0838 namespace internal {
0839 
0840 template<typename Derived, bool Big = (Derived::SizeAtCompileTime>=16)>
0841 struct setIdentity_impl
0842 {
0843   EIGEN_DEVICE_FUNC
0844   static EIGEN_STRONG_INLINE Derived& run(Derived& m)
0845   {
0846     return m = Derived::Identity(m.rows(), m.cols());
0847   }
0848 };
0849 
0850 template<typename Derived>
0851 struct setIdentity_impl<Derived, true>
0852 {
0853   EIGEN_DEVICE_FUNC
0854   static EIGEN_STRONG_INLINE Derived& run(Derived& m)
0855   {
0856     m.setZero();
0857     const Index size = numext::mini(m.rows(), m.cols());
0858     for(Index i = 0; i < size; ++i) m.coeffRef(i,i) = typename Derived::Scalar(1);
0859     return m;
0860   }
0861 };
0862 
0863 } // end namespace internal
0864 
0865 /** Writes the identity expression (not necessarily square) into *this.
0866   *
0867   * Example: \include MatrixBase_setIdentity.cpp
0868   * Output: \verbinclude MatrixBase_setIdentity.out
0869   *
0870   * \sa class CwiseNullaryOp, Identity(), Identity(Index,Index), isIdentity()
0871   */
0872 template<typename Derived>
0873 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity()
0874 {
0875   return internal::setIdentity_impl<Derived>::run(derived());
0876 }
0877 
0878 /** \brief Resizes to the given size, and writes the identity expression (not necessarily square) into *this.
0879   *
0880   * \param rows the new number of rows
0881   * \param cols the new number of columns
0882   *
0883   * Example: \include Matrix_setIdentity_int_int.cpp
0884   * Output: \verbinclude Matrix_setIdentity_int_int.out
0885   *
0886   * \sa MatrixBase::setIdentity(), class CwiseNullaryOp, MatrixBase::Identity()
0887   */
0888 template<typename Derived>
0889 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity(Index rows, Index cols)
0890 {
0891   derived().resize(rows, cols);
0892   return setIdentity();
0893 }
0894 
0895 /** \returns an expression of the i-th unit (basis) vector.
0896   *
0897   * \only_for_vectors
0898   *
0899   * \sa MatrixBase::Unit(Index), MatrixBase::UnitX(), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW()
0900   */
0901 template<typename Derived>
0902 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::Unit(Index newSize, Index i)
0903 {
0904   EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
0905   return BasisReturnType(SquareMatrixType::Identity(newSize,newSize), i);
0906 }
0907 
0908 /** \returns an expression of the i-th unit (basis) vector.
0909   *
0910   * \only_for_vectors
0911   *
0912   * This variant is for fixed-size vector only.
0913   *
0914   * \sa MatrixBase::Unit(Index,Index), MatrixBase::UnitX(), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW()
0915   */
0916 template<typename Derived>
0917 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::Unit(Index i)
0918 {
0919   EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
0920   return BasisReturnType(SquareMatrixType::Identity(),i);
0921 }
0922 
0923 /** \returns an expression of the X axis unit vector (1{,0}^*)
0924   *
0925   * \only_for_vectors
0926   *
0927   * \sa MatrixBase::Unit(Index,Index), MatrixBase::Unit(Index), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW()
0928   */
0929 template<typename Derived>
0930 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitX()
0931 { return Derived::Unit(0); }
0932 
0933 /** \returns an expression of the Y axis unit vector (0,1{,0}^*)
0934   *
0935   * \only_for_vectors
0936   *
0937   * \sa MatrixBase::Unit(Index,Index), MatrixBase::Unit(Index), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW()
0938   */
0939 template<typename Derived>
0940 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitY()
0941 { return Derived::Unit(1); }
0942 
0943 /** \returns an expression of the Z axis unit vector (0,0,1{,0}^*)
0944   *
0945   * \only_for_vectors
0946   *
0947   * \sa MatrixBase::Unit(Index,Index), MatrixBase::Unit(Index), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW()
0948   */
0949 template<typename Derived>
0950 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitZ()
0951 { return Derived::Unit(2); }
0952 
0953 /** \returns an expression of the W axis unit vector (0,0,0,1)
0954   *
0955   * \only_for_vectors
0956   *
0957   * \sa MatrixBase::Unit(Index,Index), MatrixBase::Unit(Index), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW()
0958   */
0959 template<typename Derived>
0960 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitW()
0961 { return Derived::Unit(3); }
0962 
0963 /** \brief Set the coefficients of \c *this to the i-th unit (basis) vector
0964   *
0965   * \param i index of the unique coefficient to be set to 1
0966   *
0967   * \only_for_vectors
0968   *
0969   * \sa MatrixBase::setIdentity(), class CwiseNullaryOp, MatrixBase::Unit(Index,Index)
0970   */
0971 template<typename Derived>
0972 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setUnit(Index i)
0973 {
0974   EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
0975   eigen_assert(i<size());
0976   derived().setZero();
0977   derived().coeffRef(i) = Scalar(1);
0978   return derived();
0979 }
0980 
0981 /** \brief Resizes to the given \a newSize, and writes the i-th unit (basis) vector into *this.
0982   *
0983   * \param newSize the new size of the vector
0984   * \param i index of the unique coefficient to be set to 1
0985   *
0986   * \only_for_vectors
0987   *
0988   * \sa MatrixBase::setIdentity(), class CwiseNullaryOp, MatrixBase::Unit(Index,Index)
0989   */
0990 template<typename Derived>
0991 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setUnit(Index newSize, Index i)
0992 {
0993   EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
0994   eigen_assert(i<newSize);
0995   derived().resize(newSize);
0996   return setUnit(i);
0997 }
0998 
0999 } // end namespace Eigen
1000 
1001 #endif // EIGEN_CWISE_NULLARY_OP_H