Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
CwiseNullaryOp.h
00001 // This file is part of Eigen, a lightweight C++ template library 00002 // for linear algebra. 00003 // 00004 // Copyright (C) 2008-2010 Gael Guennebaud <gael.guennebaud@inria.fr> 00005 // 00006 // This Source Code Form is subject to the terms of the Mozilla 00007 // Public License v. 2.0. If a copy of the MPL was not distributed 00008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 00009 00010 #ifndef EIGEN_CWISE_NULLARY_OP_H 00011 #define EIGEN_CWISE_NULLARY_OP_H 00012 00013 namespace Eigen { 00014 00015 /** \class CwiseNullaryOp 00016 * \ingroup Core_Module 00017 * 00018 * \brief Generic expression of a matrix where all coefficients are defined by a functor 00019 * 00020 * \param NullaryOp template functor implementing the operator 00021 * \param PlainObjectType the underlying plain matrix/array type 00022 * 00023 * This class represents an expression of a generic nullary operator. 00024 * It is the return type of the Ones(), Zero(), Constant(), Identity() and Random() methods, 00025 * and most of the time this is the only way it is used. 00026 * 00027 * However, if you want to write a function returning such an expression, you 00028 * will need to use this class. 00029 * 00030 * \sa class CwiseUnaryOp, class CwiseBinaryOp, DenseBase::NullaryExpr() 00031 */ 00032 00033 namespace internal { 00034 template<typename NullaryOp, typename PlainObjectType> 00035 struct traits<CwiseNullaryOp<NullaryOp, PlainObjectType> > : traits<PlainObjectType> 00036 { 00037 enum { 00038 Flags = (traits<PlainObjectType>::Flags 00039 & ( HereditaryBits 00040 | (functor_has_linear_access<NullaryOp>::ret ? LinearAccessBit : 0) 00041 | (functor_traits<NullaryOp>::PacketAccess ? PacketAccessBit : 0))) 00042 | (functor_traits<NullaryOp>::IsRepeatable ? 0 : EvalBeforeNestingBit), 00043 CoeffReadCost = functor_traits<NullaryOp>::Cost 00044 }; 00045 }; 00046 } 00047 00048 template<typename NullaryOp, typename PlainObjectType> 00049 class CwiseNullaryOp : internal::no_assignment_operator, 00050 public internal::dense_xpr_base< CwiseNullaryOp<NullaryOp, PlainObjectType> >::type 00051 { 00052 public: 00053 00054 typedef typename internal::dense_xpr_base<CwiseNullaryOp>::type Base; 00055 EIGEN_DENSE_PUBLIC_INTERFACE(CwiseNullaryOp) 00056 00057 CwiseNullaryOp(Index nbRows, Index nbCols, const NullaryOp& func = NullaryOp()) 00058 : m_rows(nbRows), m_cols(nbCols), m_functor(func) 00059 { 00060 eigen_assert(nbRows >= 0 00061 && (RowsAtCompileTime == Dynamic || RowsAtCompileTime == nbRows) 00062 && nbCols >= 0 00063 && (ColsAtCompileTime == Dynamic || ColsAtCompileTime == nbCols)); 00064 } 00065 00066 EIGEN_STRONG_INLINE Index rows() const { return m_rows.value(); } 00067 EIGEN_STRONG_INLINE Index cols() const { return m_cols.value(); } 00068 00069 EIGEN_STRONG_INLINE const Scalar coeff(Index rowId, Index colId) const 00070 { 00071 return m_functor(rowId, colId); 00072 } 00073 00074 template<int LoadMode> 00075 EIGEN_STRONG_INLINE PacketScalar packet(Index rowId, Index colId) const 00076 { 00077 return m_functor.packetOp(rowId, colId); 00078 } 00079 00080 EIGEN_STRONG_INLINE const Scalar coeff(Index index) const 00081 { 00082 return m_functor(index); 00083 } 00084 00085 template<int LoadMode> 00086 EIGEN_STRONG_INLINE PacketScalar packet(Index index) const 00087 { 00088 return m_functor.packetOp(index); 00089 } 00090 00091 /** \returns the functor representing the nullary operation */ 00092 const NullaryOp& functor() const { return m_functor; } 00093 00094 protected: 00095 const internal::variable_if_dynamic<Index, RowsAtCompileTime> m_rows; 00096 const internal::variable_if_dynamic<Index, ColsAtCompileTime> m_cols; 00097 const NullaryOp m_functor; 00098 }; 00099 00100 00101 /** \returns an expression of a matrix defined by a custom functor \a func 00102 * 00103 * The parameters \a rows and \a cols are the number of rows and of columns of 00104 * the returned matrix. Must be compatible with this MatrixBase type. 00105 * 00106 * This variant is meant to be used for dynamic-size matrix types. For fixed-size types, 00107 * it is redundant to pass \a rows and \a cols as arguments, so Zero() should be used 00108 * instead. 00109 * 00110 * The template parameter \a CustomNullaryOp is the type of the functor. 00111 * 00112 * \sa class CwiseNullaryOp 00113 */ 00114 template<typename Derived> 00115 template<typename CustomNullaryOp> 00116 EIGEN_STRONG_INLINE const CwiseNullaryOp<CustomNullaryOp, Derived> 00117 DenseBase<Derived>::NullaryExpr(Index rows, Index cols, const CustomNullaryOp& func) 00118 { 00119 return CwiseNullaryOp<CustomNullaryOp, Derived>(rows, cols, func); 00120 } 00121 00122 /** \returns an expression of a matrix defined by a custom functor \a func 00123 * 00124 * The parameter \a size is the size of the returned vector. 00125 * Must be compatible with this MatrixBase type. 00126 * 00127 * \only_for_vectors 00128 * 00129 * This variant is meant to be used for dynamic-size vector types. For fixed-size types, 00130 * it is redundant to pass \a size as argument, so Zero() should be used 00131 * instead. 00132 * 00133 * The template parameter \a CustomNullaryOp is the type of the functor. 00134 * 00135 * \sa class CwiseNullaryOp 00136 */ 00137 template<typename Derived> 00138 template<typename CustomNullaryOp> 00139 EIGEN_STRONG_INLINE const CwiseNullaryOp<CustomNullaryOp, Derived> 00140 DenseBase<Derived>::NullaryExpr(Index size, const CustomNullaryOp& func) 00141 { 00142 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) 00143 if(RowsAtCompileTime == 1) return CwiseNullaryOp<CustomNullaryOp, Derived>(1, size, func); 00144 else return CwiseNullaryOp<CustomNullaryOp, Derived>(size, 1, func); 00145 } 00146 00147 /** \returns an expression of a matrix defined by a custom functor \a func 00148 * 00149 * This variant is only for fixed-size DenseBase types. For dynamic-size types, you 00150 * need to use the variants taking size arguments. 00151 * 00152 * The template parameter \a CustomNullaryOp is the type of the functor. 00153 * 00154 * \sa class CwiseNullaryOp 00155 */ 00156 template<typename Derived> 00157 template<typename CustomNullaryOp> 00158 EIGEN_STRONG_INLINE const CwiseNullaryOp<CustomNullaryOp, Derived> 00159 DenseBase<Derived>::NullaryExpr(const CustomNullaryOp& func) 00160 { 00161 return CwiseNullaryOp<CustomNullaryOp, Derived>(RowsAtCompileTime, ColsAtCompileTime, func); 00162 } 00163 00164 /** \returns an expression of a constant matrix of value \a value 00165 * 00166 * The parameters \a nbRows and \a nbCols are the number of rows and of columns of 00167 * the returned matrix. Must be compatible with this DenseBase type. 00168 * 00169 * This variant is meant to be used for dynamic-size matrix types. For fixed-size types, 00170 * it is redundant to pass \a nbRows and \a nbCols as arguments, so Zero() should be used 00171 * instead. 00172 * 00173 * The template parameter \a CustomNullaryOp is the type of the functor. 00174 * 00175 * \sa class CwiseNullaryOp 00176 */ 00177 template<typename Derived> 00178 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType 00179 DenseBase<Derived>::Constant(Index nbRows, Index nbCols, const Scalar& value) 00180 { 00181 return DenseBase<Derived>::NullaryExpr(nbRows, nbCols, internal::scalar_constant_op<Scalar>(value)); 00182 } 00183 00184 /** \returns an expression of a constant matrix of value \a value 00185 * 00186 * The parameter \a size is the size of the returned vector. 00187 * Must be compatible with this DenseBase type. 00188 * 00189 * \only_for_vectors 00190 * 00191 * This variant is meant to be used for dynamic-size vector types. For fixed-size types, 00192 * it is redundant to pass \a size as argument, so Zero() should be used 00193 * instead. 00194 * 00195 * The template parameter \a CustomNullaryOp is the type of the functor. 00196 * 00197 * \sa class CwiseNullaryOp 00198 */ 00199 template<typename Derived> 00200 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType 00201 DenseBase<Derived>::Constant(Index size, const Scalar& value) 00202 { 00203 return DenseBase<Derived>::NullaryExpr(size, internal::scalar_constant_op<Scalar>(value)); 00204 } 00205 00206 /** \returns an expression of a constant matrix of value \a value 00207 * 00208 * This variant is only for fixed-size DenseBase types. For dynamic-size types, you 00209 * need to use the variants taking size arguments. 00210 * 00211 * The template parameter \a CustomNullaryOp is the type of the functor. 00212 * 00213 * \sa class CwiseNullaryOp 00214 */ 00215 template<typename Derived> 00216 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType 00217 DenseBase<Derived>::Constant(const Scalar& value) 00218 { 00219 EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived) 00220 return DenseBase<Derived>::NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, internal::scalar_constant_op<Scalar>(value)); 00221 } 00222 00223 /** 00224 * \brief Sets a linearly space vector. 00225 * 00226 * The function generates 'size' equally spaced values in the closed interval [low,high]. 00227 * This particular version of LinSpaced() uses sequential access, i.e. vector access is 00228 * assumed to be a(0), a(1), ..., a(size). This assumption allows for better vectorization 00229 * and yields faster code than the random access version. 00230 * 00231 * When size is set to 1, a vector of length 1 containing 'high' is returned. 00232 * 00233 * \only_for_vectors 00234 * 00235 * Example: \include DenseBase_LinSpaced_seq.cpp 00236 * Output: \verbinclude DenseBase_LinSpaced_seq.out 00237 * 00238 * \sa setLinSpaced(Index,const Scalar&,const Scalar&), LinSpaced(Index,Scalar,Scalar), CwiseNullaryOp 00239 */ 00240 template<typename Derived> 00241 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::SequentialLinSpacedReturnType 00242 DenseBase<Derived>::LinSpaced(Sequential_t, Index size, const Scalar& low, const Scalar& high) 00243 { 00244 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) 00245 return DenseBase<Derived>::NullaryExpr(size, internal::linspaced_op<Scalar,false>(low,high,size)); 00246 } 00247 00248 /** 00249 * \copydoc DenseBase::LinSpaced(Sequential_t, Index, const Scalar&, const Scalar&) 00250 * Special version for fixed size types which does not require the size parameter. 00251 */ 00252 template<typename Derived> 00253 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::SequentialLinSpacedReturnType 00254 DenseBase<Derived>::LinSpaced(Sequential_t, const Scalar& low, const Scalar& high) 00255 { 00256 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) 00257 EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived) 00258 return DenseBase<Derived>::NullaryExpr(Derived::SizeAtCompileTime, internal::linspaced_op<Scalar,false>(low,high,Derived::SizeAtCompileTime)); 00259 } 00260 00261 /** 00262 * \brief Sets a linearly space vector. 00263 * 00264 * The function generates 'size' equally spaced values in the closed interval [low,high]. 00265 * When size is set to 1, a vector of length 1 containing 'high' is returned. 00266 * 00267 * \only_for_vectors 00268 * 00269 * Example: \include DenseBase_LinSpaced.cpp 00270 * Output: \verbinclude DenseBase_LinSpaced.out 00271 * 00272 * \sa setLinSpaced(Index,const Scalar&,const Scalar&), LinSpaced(Sequential_t,Index,const Scalar&,const Scalar&,Index), CwiseNullaryOp 00273 */ 00274 template<typename Derived> 00275 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType 00276 DenseBase<Derived>::LinSpaced(Index size, const Scalar& low, const Scalar& high) 00277 { 00278 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) 00279 return DenseBase<Derived>::NullaryExpr(size, internal::linspaced_op<Scalar,true>(low,high,size)); 00280 } 00281 00282 /** 00283 * \copydoc DenseBase::LinSpaced(Index, const Scalar&, const Scalar&) 00284 * Special version for fixed size types which does not require the size parameter. 00285 */ 00286 template<typename Derived> 00287 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType 00288 DenseBase<Derived>::LinSpaced(const Scalar& low, const Scalar& high) 00289 { 00290 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) 00291 EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived) 00292 return DenseBase<Derived>::NullaryExpr(Derived::SizeAtCompileTime, internal::linspaced_op<Scalar,true>(low,high,Derived::SizeAtCompileTime)); 00293 } 00294 00295 /** \returns true if all coefficients in this matrix are approximately equal to \a val, to within precision \a prec */ 00296 template<typename Derived> 00297 bool DenseBase<Derived>::isApproxToConstant 00298 (const Scalar& val, const RealScalar& prec) const 00299 { 00300 for(Index j = 0; j < cols(); ++j) 00301 for(Index i = 0; i < rows(); ++i) 00302 if(!internal::isApprox(this->coeff(i, j), val, prec)) 00303 return false; 00304 return true; 00305 } 00306 00307 /** This is just an alias for isApproxToConstant(). 00308 * 00309 * \returns true if all coefficients in this matrix are approximately equal to \a value, to within precision \a prec */ 00310 template<typename Derived> 00311 bool DenseBase<Derived>::isConstant 00312 (const Scalar& val, const RealScalar& prec) const 00313 { 00314 return isApproxToConstant(val, prec); 00315 } 00316 00317 /** Alias for setConstant(): sets all coefficients in this expression to \a val. 00318 * 00319 * \sa setConstant(), Constant(), class CwiseNullaryOp 00320 */ 00321 template<typename Derived> 00322 EIGEN_STRONG_INLINE void DenseBase<Derived>::fill(const Scalar& val) 00323 { 00324 setConstant(val); 00325 } 00326 00327 /** Sets all coefficients in this expression to \a value. 00328 * 00329 * \sa fill(), setConstant(Index,const Scalar&), setConstant(Index,Index,const Scalar&), setZero(), setOnes(), Constant(), class CwiseNullaryOp, setZero(), setOnes() 00330 */ 00331 template<typename Derived> 00332 EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setConstant(const Scalar& val) 00333 { 00334 return derived() = Constant(rows(), cols(), val); 00335 } 00336 00337 /** Resizes to the given \a size, and sets all coefficients in this expression to the given \a value. 00338 * 00339 * \only_for_vectors 00340 * 00341 * Example: \include Matrix_setConstant_int.cpp 00342 * Output: \verbinclude Matrix_setConstant_int.out 00343 * 00344 * \sa MatrixBase::setConstant(const Scalar&), setConstant(Index,Index,const Scalar&), class CwiseNullaryOp, MatrixBase::Constant(const Scalar&) 00345 */ 00346 template<typename Derived> 00347 EIGEN_STRONG_INLINE Derived& 00348 PlainObjectBase<Derived>::setConstant(Index size, const Scalar& val) 00349 { 00350 resize(size); 00351 return setConstant(val); 00352 } 00353 00354 /** Resizes to the given size, and sets all coefficients in this expression to the given \a value. 00355 * 00356 * \param nbRows the new number of rows 00357 * \param nbCols the new number of columns 00358 * \param val the value to which all coefficients are set 00359 * 00360 * Example: \include Matrix_setConstant_int_int.cpp 00361 * Output: \verbinclude Matrix_setConstant_int_int.out 00362 * 00363 * \sa MatrixBase::setConstant(const Scalar&), setConstant(Index,const Scalar&), class CwiseNullaryOp, MatrixBase::Constant(const Scalar&) 00364 */ 00365 template<typename Derived> 00366 EIGEN_STRONG_INLINE Derived& 00367 PlainObjectBase<Derived>::setConstant(Index nbRows, Index nbCols, const Scalar& val) 00368 { 00369 resize(nbRows, nbCols); 00370 return setConstant(val); 00371 } 00372 00373 /** 00374 * \brief Sets a linearly space vector. 00375 * 00376 * The function generates 'size' equally spaced values in the closed interval [low,high]. 00377 * When size is set to 1, a vector of length 1 containing 'high' is returned. 00378 * 00379 * \only_for_vectors 00380 * 00381 * Example: \include DenseBase_setLinSpaced.cpp 00382 * Output: \verbinclude DenseBase_setLinSpaced.out 00383 * 00384 * \sa CwiseNullaryOp 00385 */ 00386 template<typename Derived> 00387 EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setLinSpaced(Index newSize, const Scalar& low, const Scalar& high) 00388 { 00389 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) 00390 return derived() = Derived::NullaryExpr(newSize, internal::linspaced_op<Scalar,false>(low,high,newSize)); 00391 } 00392 00393 /** 00394 * \brief Sets a linearly space vector. 00395 * 00396 * The function fill *this with equally spaced values in the closed interval [low,high]. 00397 * When size is set to 1, a vector of length 1 containing 'high' is returned. 00398 * 00399 * \only_for_vectors 00400 * 00401 * \sa setLinSpaced(Index, const Scalar&, const Scalar&), CwiseNullaryOp 00402 */ 00403 template<typename Derived> 00404 EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setLinSpaced(const Scalar& low, const Scalar& high) 00405 { 00406 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) 00407 return setLinSpaced(size(), low, high); 00408 } 00409 00410 // zero: 00411 00412 /** \returns an expression of a zero matrix. 00413 * 00414 * The parameters \a rows and \a cols are the number of rows and of columns of 00415 * the returned matrix. Must be compatible with this MatrixBase type. 00416 * 00417 * This variant is meant to be used for dynamic-size matrix types. For fixed-size types, 00418 * it is redundant to pass \a rows and \a cols as arguments, so Zero() should be used 00419 * instead. 00420 * 00421 * Example: \include MatrixBase_zero_int_int.cpp 00422 * Output: \verbinclude MatrixBase_zero_int_int.out 00423 * 00424 * \sa Zero(), Zero(Index) 00425 */ 00426 template<typename Derived> 00427 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType 00428 DenseBase<Derived>::Zero(Index nbRows, Index nbCols) 00429 { 00430 return Constant(nbRows, nbCols, Scalar(0)); 00431 } 00432 00433 /** \returns an expression of a zero vector. 00434 * 00435 * The parameter \a size is the size of the returned vector. 00436 * Must be compatible with this MatrixBase type. 00437 * 00438 * \only_for_vectors 00439 * 00440 * This variant is meant to be used for dynamic-size vector types. For fixed-size types, 00441 * it is redundant to pass \a size as argument, so Zero() should be used 00442 * instead. 00443 * 00444 * Example: \include MatrixBase_zero_int.cpp 00445 * Output: \verbinclude MatrixBase_zero_int.out 00446 * 00447 * \sa Zero(), Zero(Index,Index) 00448 */ 00449 template<typename Derived> 00450 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType 00451 DenseBase<Derived>::Zero(Index size) 00452 { 00453 return Constant(size, Scalar(0)); 00454 } 00455 00456 /** \returns an expression of a fixed-size zero matrix or vector. 00457 * 00458 * This variant is only for fixed-size MatrixBase types. For dynamic-size types, you 00459 * need to use the variants taking size arguments. 00460 * 00461 * Example: \include MatrixBase_zero.cpp 00462 * Output: \verbinclude MatrixBase_zero.out 00463 * 00464 * \sa Zero(Index), Zero(Index,Index) 00465 */ 00466 template<typename Derived> 00467 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType 00468 DenseBase<Derived>::Zero() 00469 { 00470 return Constant(Scalar(0)); 00471 } 00472 00473 /** \returns true if *this is approximately equal to the zero matrix, 00474 * within the precision given by \a prec. 00475 * 00476 * Example: \include MatrixBase_isZero.cpp 00477 * Output: \verbinclude MatrixBase_isZero.out 00478 * 00479 * \sa class CwiseNullaryOp, Zero() 00480 */ 00481 template<typename Derived> 00482 bool DenseBase<Derived>::isZero(const RealScalar& prec) const 00483 { 00484 for(Index j = 0; j < cols(); ++j) 00485 for(Index i = 0; i < rows(); ++i) 00486 if(!internal::isMuchSmallerThan(this->coeff(i, j), static_cast<Scalar>(1), prec)) 00487 return false; 00488 return true; 00489 } 00490 00491 /** Sets all coefficients in this expression to zero. 00492 * 00493 * Example: \include MatrixBase_setZero.cpp 00494 * Output: \verbinclude MatrixBase_setZero.out 00495 * 00496 * \sa class CwiseNullaryOp, Zero() 00497 */ 00498 template<typename Derived> 00499 EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setZero() 00500 { 00501 return setConstant(Scalar(0)); 00502 } 00503 00504 /** Resizes to the given \a size, and sets all coefficients in this expression to zero. 00505 * 00506 * \only_for_vectors 00507 * 00508 * Example: \include Matrix_setZero_int.cpp 00509 * Output: \verbinclude Matrix_setZero_int.out 00510 * 00511 * \sa DenseBase::setZero(), setZero(Index,Index), class CwiseNullaryOp, DenseBase::Zero() 00512 */ 00513 template<typename Derived> 00514 EIGEN_STRONG_INLINE Derived& 00515 PlainObjectBase<Derived>::setZero(Index newSize) 00516 { 00517 resize(newSize); 00518 return setConstant(Scalar(0)); 00519 } 00520 00521 /** Resizes to the given size, and sets all coefficients in this expression to zero. 00522 * 00523 * \param nbRows the new number of rows 00524 * \param nbCols the new number of columns 00525 * 00526 * Example: \include Matrix_setZero_int_int.cpp 00527 * Output: \verbinclude Matrix_setZero_int_int.out 00528 * 00529 * \sa DenseBase::setZero(), setZero(Index), class CwiseNullaryOp, DenseBase::Zero() 00530 */ 00531 template<typename Derived> 00532 EIGEN_STRONG_INLINE Derived& 00533 PlainObjectBase<Derived>::setZero(Index nbRows, Index nbCols) 00534 { 00535 resize(nbRows, nbCols); 00536 return setConstant(Scalar(0)); 00537 } 00538 00539 // ones: 00540 00541 /** \returns an expression of a matrix where all coefficients equal one. 00542 * 00543 * The parameters \a nbRows and \a nbCols are the number of rows and of columns of 00544 * the returned matrix. Must be compatible with this MatrixBase type. 00545 * 00546 * This variant is meant to be used for dynamic-size matrix types. For fixed-size types, 00547 * it is redundant to pass \a rows and \a cols as arguments, so Ones() should be used 00548 * instead. 00549 * 00550 * Example: \include MatrixBase_ones_int_int.cpp 00551 * Output: \verbinclude MatrixBase_ones_int_int.out 00552 * 00553 * \sa Ones(), Ones(Index), isOnes(), class Ones 00554 */ 00555 template<typename Derived> 00556 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType 00557 DenseBase<Derived>::Ones(Index nbRows, Index nbCols) 00558 { 00559 return Constant(nbRows, nbCols, Scalar(1)); 00560 } 00561 00562 /** \returns an expression of a vector where all coefficients equal one. 00563 * 00564 * The parameter \a newSize is the size of the returned vector. 00565 * Must be compatible with this MatrixBase type. 00566 * 00567 * \only_for_vectors 00568 * 00569 * This variant is meant to be used for dynamic-size vector types. For fixed-size types, 00570 * it is redundant to pass \a size as argument, so Ones() should be used 00571 * instead. 00572 * 00573 * Example: \include MatrixBase_ones_int.cpp 00574 * Output: \verbinclude MatrixBase_ones_int.out 00575 * 00576 * \sa Ones(), Ones(Index,Index), isOnes(), class Ones 00577 */ 00578 template<typename Derived> 00579 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType 00580 DenseBase<Derived>::Ones(Index newSize) 00581 { 00582 return Constant(newSize, Scalar(1)); 00583 } 00584 00585 /** \returns an expression of a fixed-size matrix or vector where all coefficients equal one. 00586 * 00587 * This variant is only for fixed-size MatrixBase types. For dynamic-size types, you 00588 * need to use the variants taking size arguments. 00589 * 00590 * Example: \include MatrixBase_ones.cpp 00591 * Output: \verbinclude MatrixBase_ones.out 00592 * 00593 * \sa Ones(Index), Ones(Index,Index), isOnes(), class Ones 00594 */ 00595 template<typename Derived> 00596 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType 00597 DenseBase<Derived>::Ones() 00598 { 00599 return Constant(Scalar(1)); 00600 } 00601 00602 /** \returns true if *this is approximately equal to the matrix where all coefficients 00603 * are equal to 1, within the precision given by \a prec. 00604 * 00605 * Example: \include MatrixBase_isOnes.cpp 00606 * Output: \verbinclude MatrixBase_isOnes.out 00607 * 00608 * \sa class CwiseNullaryOp, Ones() 00609 */ 00610 template<typename Derived> 00611 bool DenseBase<Derived>::isOnes 00612 (const RealScalar& prec) const 00613 { 00614 return isApproxToConstant(Scalar(1), prec); 00615 } 00616 00617 /** Sets all coefficients in this expression to one. 00618 * 00619 * Example: \include MatrixBase_setOnes.cpp 00620 * Output: \verbinclude MatrixBase_setOnes.out 00621 * 00622 * \sa class CwiseNullaryOp, Ones() 00623 */ 00624 template<typename Derived> 00625 EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setOnes() 00626 { 00627 return setConstant(Scalar(1)); 00628 } 00629 00630 /** Resizes to the given \a newSize, and sets all coefficients in this expression to one. 00631 * 00632 * \only_for_vectors 00633 * 00634 * Example: \include Matrix_setOnes_int.cpp 00635 * Output: \verbinclude Matrix_setOnes_int.out 00636 * 00637 * \sa MatrixBase::setOnes(), setOnes(Index,Index), class CwiseNullaryOp, MatrixBase::Ones() 00638 */ 00639 template<typename Derived> 00640 EIGEN_STRONG_INLINE Derived& 00641 PlainObjectBase<Derived>::setOnes(Index newSize) 00642 { 00643 resize(newSize); 00644 return setConstant(Scalar(1)); 00645 } 00646 00647 /** Resizes to the given size, and sets all coefficients in this expression to one. 00648 * 00649 * \param nbRows the new number of rows 00650 * \param nbCols the new number of columns 00651 * 00652 * Example: \include Matrix_setOnes_int_int.cpp 00653 * Output: \verbinclude Matrix_setOnes_int_int.out 00654 * 00655 * \sa MatrixBase::setOnes(), setOnes(Index), class CwiseNullaryOp, MatrixBase::Ones() 00656 */ 00657 template<typename Derived> 00658 EIGEN_STRONG_INLINE Derived& 00659 PlainObjectBase<Derived>::setOnes(Index nbRows, Index nbCols) 00660 { 00661 resize(nbRows, nbCols); 00662 return setConstant(Scalar(1)); 00663 } 00664 00665 // Identity: 00666 00667 /** \returns an expression of the identity matrix (not necessarily square). 00668 * 00669 * The parameters \a nbRows and \a nbCols are the number of rows and of columns of 00670 * the returned matrix. Must be compatible with this MatrixBase type. 00671 * 00672 * This variant is meant to be used for dynamic-size matrix types. For fixed-size types, 00673 * it is redundant to pass \a rows and \a cols as arguments, so Identity() should be used 00674 * instead. 00675 * 00676 * Example: \include MatrixBase_identity_int_int.cpp 00677 * Output: \verbinclude MatrixBase_identity_int_int.out 00678 * 00679 * \sa Identity(), setIdentity(), isIdentity() 00680 */ 00681 template<typename Derived> 00682 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::IdentityReturnType 00683 MatrixBase<Derived>::Identity(Index nbRows, Index nbCols) 00684 { 00685 return DenseBase<Derived>::NullaryExpr(nbRows, nbCols, internal::scalar_identity_op<Scalar>()); 00686 } 00687 00688 /** \returns an expression of the identity matrix (not necessarily square). 00689 * 00690 * This variant is only for fixed-size MatrixBase types. For dynamic-size types, you 00691 * need to use the variant taking size arguments. 00692 * 00693 * Example: \include MatrixBase_identity.cpp 00694 * Output: \verbinclude MatrixBase_identity.out 00695 * 00696 * \sa Identity(Index,Index), setIdentity(), isIdentity() 00697 */ 00698 template<typename Derived> 00699 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::IdentityReturnType 00700 MatrixBase<Derived>::Identity() 00701 { 00702 EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived) 00703 return MatrixBase<Derived>::NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, internal::scalar_identity_op<Scalar>()); 00704 } 00705 00706 /** \returns true if *this is approximately equal to the identity matrix 00707 * (not necessarily square), 00708 * within the precision given by \a prec. 00709 * 00710 * Example: \include MatrixBase_isIdentity.cpp 00711 * Output: \verbinclude MatrixBase_isIdentity.out 00712 * 00713 * \sa class CwiseNullaryOp, Identity(), Identity(Index,Index), setIdentity() 00714 */ 00715 template<typename Derived> 00716 bool MatrixBase<Derived>::isIdentity 00717 (const RealScalar& prec) const 00718 { 00719 for(Index j = 0; j < cols(); ++j) 00720 { 00721 for(Index i = 0; i < rows(); ++i) 00722 { 00723 if(i == j) 00724 { 00725 if(!internal::isApprox(this->coeff(i, j), static_cast<Scalar>(1), prec)) 00726 return false; 00727 } 00728 else 00729 { 00730 if(!internal::isMuchSmallerThan(this->coeff(i, j), static_cast<RealScalar>(1), prec)) 00731 return false; 00732 } 00733 } 00734 } 00735 return true; 00736 } 00737 00738 namespace internal { 00739 00740 template<typename Derived, bool Big = (Derived::SizeAtCompileTime>=16)> 00741 struct setIdentity_impl 00742 { 00743 static EIGEN_STRONG_INLINE Derived& run(Derived& m) 00744 { 00745 return m = Derived::Identity(m.rows(), m.cols()); 00746 } 00747 }; 00748 00749 template<typename Derived> 00750 struct setIdentity_impl<Derived, true> 00751 { 00752 typedef typename Derived::Index Index; 00753 static EIGEN_STRONG_INLINE Derived& run(Derived& m) 00754 { 00755 m.setZero(); 00756 const Index size = (std::min)(m.rows(), m.cols()); 00757 for(Index i = 0; i < size; ++i) m.coeffRef(i,i) = typename Derived::Scalar(1); 00758 return m; 00759 } 00760 }; 00761 00762 } // end namespace internal 00763 00764 /** Writes the identity expression (not necessarily square) into *this. 00765 * 00766 * Example: \include MatrixBase_setIdentity.cpp 00767 * Output: \verbinclude MatrixBase_setIdentity.out 00768 * 00769 * \sa class CwiseNullaryOp, Identity(), Identity(Index,Index), isIdentity() 00770 */ 00771 template<typename Derived> 00772 EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity() 00773 { 00774 return internal::setIdentity_impl<Derived>::run(derived()); 00775 } 00776 00777 /** \brief Resizes to the given size, and writes the identity expression (not necessarily square) into *this. 00778 * 00779 * \param nbRows the new number of rows 00780 * \param nbCols the new number of columns 00781 * 00782 * Example: \include Matrix_setIdentity_int_int.cpp 00783 * Output: \verbinclude Matrix_setIdentity_int_int.out 00784 * 00785 * \sa MatrixBase::setIdentity(), class CwiseNullaryOp, MatrixBase::Identity() 00786 */ 00787 template<typename Derived> 00788 EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity(Index nbRows, Index nbCols) 00789 { 00790 derived().resize(nbRows, nbCols); 00791 return setIdentity(); 00792 } 00793 00794 /** \returns an expression of the i-th unit (basis) vector. 00795 * 00796 * \only_for_vectors 00797 * 00798 * \sa MatrixBase::Unit(Index), MatrixBase::UnitX(), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW() 00799 */ 00800 template<typename Derived> 00801 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::Unit(Index newSize, Index i) 00802 { 00803 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) 00804 return BasisReturnType(SquareMatrixType::Identity(newSize,newSize), i); 00805 } 00806 00807 /** \returns an expression of the i-th unit (basis) vector. 00808 * 00809 * \only_for_vectors 00810 * 00811 * This variant is for fixed-size vector only. 00812 * 00813 * \sa MatrixBase::Unit(Index,Index), MatrixBase::UnitX(), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW() 00814 */ 00815 template<typename Derived> 00816 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::Unit(Index i) 00817 { 00818 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) 00819 return BasisReturnType(SquareMatrixType::Identity(),i); 00820 } 00821 00822 /** \returns an expression of the X axis unit vector (1{,0}^*) 00823 * 00824 * \only_for_vectors 00825 * 00826 * \sa MatrixBase::Unit(Index,Index), MatrixBase::Unit(Index), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW() 00827 */ 00828 template<typename Derived> 00829 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitX() 00830 { return Derived::Unit(0); } 00831 00832 /** \returns an expression of the Y axis unit vector (0,1{,0}^*) 00833 * 00834 * \only_for_vectors 00835 * 00836 * \sa MatrixBase::Unit(Index,Index), MatrixBase::Unit(Index), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW() 00837 */ 00838 template<typename Derived> 00839 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitY() 00840 { return Derived::Unit(1); } 00841 00842 /** \returns an expression of the Z axis unit vector (0,0,1{,0}^*) 00843 * 00844 * \only_for_vectors 00845 * 00846 * \sa MatrixBase::Unit(Index,Index), MatrixBase::Unit(Index), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW() 00847 */ 00848 template<typename Derived> 00849 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitZ() 00850 { return Derived::Unit(2); } 00851 00852 /** \returns an expression of the W axis unit vector (0,0,0,1) 00853 * 00854 * \only_for_vectors 00855 * 00856 * \sa MatrixBase::Unit(Index,Index), MatrixBase::Unit(Index), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW() 00857 */ 00858 template<typename Derived> 00859 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitW() 00860 { return Derived::Unit(3); } 00861 00862 } // end namespace Eigen 00863 00864 #endif // EIGEN_CWISE_NULLARY_OP_H
Generated on Thu Nov 17 2022 22:01:28 by
1.7.2