CwiseNullaryOp.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2008-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
  5. //
  6. // This Source Code Form is subject to the terms of the Mozilla
  7. // Public License v. 2.0. If a copy of the MPL was not distributed
  8. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. #ifndef EIGEN_CWISE_NULLARY_OP_H
  10. #define EIGEN_CWISE_NULLARY_OP_H
  11. namespace Eigen {
  12. namespace internal {
  13. template<typename NullaryOp, typename PlainObjectType>
  14. struct traits<CwiseNullaryOp<NullaryOp, PlainObjectType> > : traits<PlainObjectType>
  15. {
  16. enum {
  17. Flags = traits<PlainObjectType>::Flags & RowMajorBit
  18. };
  19. };
  20. } // namespace internal
  21. /** \class CwiseNullaryOp
  22. * \ingroup Core_Module
  23. *
  24. * \brief Generic expression of a matrix where all coefficients are defined by a functor
  25. *
  26. * \tparam NullaryOp template functor implementing the operator
  27. * \tparam PlainObjectType the underlying plain matrix/array type
  28. *
  29. * This class represents an expression of a generic nullary operator.
  30. * It is the return type of the Ones(), Zero(), Constant(), Identity() and Random() methods,
  31. * and most of the time this is the only way it is used.
  32. *
  33. * However, if you want to write a function returning such an expression, you
  34. * will need to use this class.
  35. *
  36. * The functor NullaryOp must expose one of the following method:
  37. <table class="manual">
  38. <tr ><td>\c operator()() </td><td>if the procedural generation does not depend on the coefficient entries (e.g., random numbers)</td></tr>
  39. <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>
  40. <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>
  41. </table>
  42. * It is also possible to expose the last two operators if the generation makes sense for matrices but can be optimized for vectors.
  43. *
  44. * See DenseBase::NullaryExpr(Index,const CustomNullaryOp&) for an example binding
  45. * C++11 random number generators.
  46. *
  47. * A nullary expression can also be used to implement custom sophisticated matrix manipulations
  48. * that cannot be covered by the existing set of natively supported matrix manipulations.
  49. * See this \ref TopicCustomizing_NullaryExpr "page" for some examples and additional explanations
  50. * on the behavior of CwiseNullaryOp.
  51. *
  52. * \sa class CwiseUnaryOp, class CwiseBinaryOp, DenseBase::NullaryExpr
  53. */
  54. template<typename NullaryOp, typename PlainObjectType>
  55. class CwiseNullaryOp : public internal::dense_xpr_base< CwiseNullaryOp<NullaryOp, PlainObjectType> >::type, internal::no_assignment_operator
  56. {
  57. public:
  58. typedef typename internal::dense_xpr_base<CwiseNullaryOp>::type Base;
  59. EIGEN_DENSE_PUBLIC_INTERFACE(CwiseNullaryOp)
  60. EIGEN_DEVICE_FUNC
  61. CwiseNullaryOp(Index rows, Index cols, const NullaryOp& func = NullaryOp())
  62. : m_rows(rows), m_cols(cols), m_functor(func)
  63. {
  64. eigen_assert(rows >= 0
  65. && (RowsAtCompileTime == Dynamic || RowsAtCompileTime == rows)
  66. && cols >= 0
  67. && (ColsAtCompileTime == Dynamic || ColsAtCompileTime == cols));
  68. }
  69. EIGEN_DEVICE_FUNC
  70. EIGEN_STRONG_INLINE Index rows() const { return m_rows.value(); }
  71. EIGEN_DEVICE_FUNC
  72. EIGEN_STRONG_INLINE Index cols() const { return m_cols.value(); }
  73. /** \returns the functor representing the nullary operation */
  74. EIGEN_DEVICE_FUNC
  75. const NullaryOp& functor() const { return m_functor; }
  76. protected:
  77. const internal::variable_if_dynamic<Index, RowsAtCompileTime> m_rows;
  78. const internal::variable_if_dynamic<Index, ColsAtCompileTime> m_cols;
  79. const NullaryOp m_functor;
  80. };
  81. /** \returns an expression of a matrix defined by a custom functor \a func
  82. *
  83. * The parameters \a rows and \a cols are the number of rows and of columns of
  84. * the returned matrix. Must be compatible with this MatrixBase type.
  85. *
  86. * This variant is meant to be used for dynamic-size matrix types. For fixed-size types,
  87. * it is redundant to pass \a rows and \a cols as arguments, so Zero() should be used
  88. * instead.
  89. *
  90. * The template parameter \a CustomNullaryOp is the type of the functor.
  91. *
  92. * \sa class CwiseNullaryOp
  93. */
  94. template<typename Derived>
  95. template<typename CustomNullaryOp>
  96. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const CwiseNullaryOp<CustomNullaryOp, typename DenseBase<Derived>::PlainObject>
  97. DenseBase<Derived>::NullaryExpr(Index rows, Index cols, const CustomNullaryOp& func)
  98. {
  99. return CwiseNullaryOp<CustomNullaryOp, PlainObject>(rows, cols, func);
  100. }
  101. /** \returns an expression of a matrix defined by a custom functor \a func
  102. *
  103. * The parameter \a size is the size of the returned vector.
  104. * Must be compatible with this MatrixBase type.
  105. *
  106. * \only_for_vectors
  107. *
  108. * This variant is meant to be used for dynamic-size vector types. For fixed-size types,
  109. * it is redundant to pass \a size as argument, so Zero() should be used
  110. * instead.
  111. *
  112. * The template parameter \a CustomNullaryOp is the type of the functor.
  113. *
  114. * Here is an example with C++11 random generators: \include random_cpp11.cpp
  115. * Output: \verbinclude random_cpp11.out
  116. *
  117. * \sa class CwiseNullaryOp
  118. */
  119. template<typename Derived>
  120. template<typename CustomNullaryOp>
  121. EIGEN_STRONG_INLINE const CwiseNullaryOp<CustomNullaryOp, typename DenseBase<Derived>::PlainObject>
  122. DenseBase<Derived>::NullaryExpr(Index size, const CustomNullaryOp& func)
  123. {
  124. EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
  125. if(RowsAtCompileTime == 1) return CwiseNullaryOp<CustomNullaryOp, PlainObject>(1, size, func);
  126. else return CwiseNullaryOp<CustomNullaryOp, PlainObject>(size, 1, func);
  127. }
  128. /** \returns an expression of a matrix defined by a custom functor \a func
  129. *
  130. * This variant is only for fixed-size DenseBase types. For dynamic-size types, you
  131. * need to use the variants taking size arguments.
  132. *
  133. * The template parameter \a CustomNullaryOp is the type of the functor.
  134. *
  135. * \sa class CwiseNullaryOp
  136. */
  137. template<typename Derived>
  138. template<typename CustomNullaryOp>
  139. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const CwiseNullaryOp<CustomNullaryOp, typename DenseBase<Derived>::PlainObject>
  140. DenseBase<Derived>::NullaryExpr(const CustomNullaryOp& func)
  141. {
  142. return CwiseNullaryOp<CustomNullaryOp, PlainObject>(RowsAtCompileTime, ColsAtCompileTime, func);
  143. }
  144. /** \returns an expression of a constant matrix of value \a value
  145. *
  146. * The parameters \a rows and \a cols are the number of rows and of columns of
  147. * the returned matrix. Must be compatible with this DenseBase type.
  148. *
  149. * This variant is meant to be used for dynamic-size matrix types. For fixed-size types,
  150. * it is redundant to pass \a rows and \a cols as arguments, so Zero() should be used
  151. * instead.
  152. *
  153. * The template parameter \a CustomNullaryOp is the type of the functor.
  154. *
  155. * \sa class CwiseNullaryOp
  156. */
  157. template<typename Derived>
  158. EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
  159. DenseBase<Derived>::Constant(Index rows, Index cols, const Scalar& value)
  160. {
  161. return DenseBase<Derived>::NullaryExpr(rows, cols, internal::scalar_constant_op<Scalar>(value));
  162. }
  163. /** \returns an expression of a constant matrix of value \a value
  164. *
  165. * The parameter \a size is the size of the returned vector.
  166. * Must be compatible with this DenseBase type.
  167. *
  168. * \only_for_vectors
  169. *
  170. * This variant is meant to be used for dynamic-size vector types. For fixed-size types,
  171. * it is redundant to pass \a size as argument, so Zero() should be used
  172. * instead.
  173. *
  174. * The template parameter \a CustomNullaryOp is the type of the functor.
  175. *
  176. * \sa class CwiseNullaryOp
  177. */
  178. template<typename Derived>
  179. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
  180. DenseBase<Derived>::Constant(Index size, const Scalar& value)
  181. {
  182. return DenseBase<Derived>::NullaryExpr(size, internal::scalar_constant_op<Scalar>(value));
  183. }
  184. /** \returns an expression of a constant matrix of value \a value
  185. *
  186. * This variant is only for fixed-size DenseBase types. For dynamic-size types, you
  187. * need to use the variants taking size arguments.
  188. *
  189. * The template parameter \a CustomNullaryOp is the type of the functor.
  190. *
  191. * \sa class CwiseNullaryOp
  192. */
  193. template<typename Derived>
  194. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
  195. DenseBase<Derived>::Constant(const Scalar& value)
  196. {
  197. EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
  198. return DenseBase<Derived>::NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, internal::scalar_constant_op<Scalar>(value));
  199. }
  200. /** \deprecated because of accuracy loss. In Eigen 3.3, it is an alias for LinSpaced(Index,const Scalar&,const Scalar&)
  201. *
  202. * \sa LinSpaced(Index,Scalar,Scalar), setLinSpaced(Index,const Scalar&,const Scalar&)
  203. */
  204. template<typename Derived>
  205. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
  206. DenseBase<Derived>::LinSpaced(Sequential_t, Index size, const Scalar& low, const Scalar& high)
  207. {
  208. EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
  209. return DenseBase<Derived>::NullaryExpr(size, internal::linspaced_op<Scalar,PacketScalar>(low,high,size));
  210. }
  211. /** \deprecated because of accuracy loss. In Eigen 3.3, it is an alias for LinSpaced(const Scalar&,const Scalar&)
  212. *
  213. * \sa LinSpaced(Scalar,Scalar)
  214. */
  215. template<typename Derived>
  216. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
  217. DenseBase<Derived>::LinSpaced(Sequential_t, const Scalar& low, const Scalar& high)
  218. {
  219. EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
  220. EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
  221. return DenseBase<Derived>::NullaryExpr(Derived::SizeAtCompileTime, internal::linspaced_op<Scalar,PacketScalar>(low,high,Derived::SizeAtCompileTime));
  222. }
  223. /**
  224. * \brief Sets a linearly spaced vector.
  225. *
  226. * The function generates 'size' equally spaced values in the closed interval [low,high].
  227. * When size is set to 1, a vector of length 1 containing 'high' is returned.
  228. *
  229. * \only_for_vectors
  230. *
  231. * Example: \include DenseBase_LinSpaced.cpp
  232. * Output: \verbinclude DenseBase_LinSpaced.out
  233. *
  234. * For integer scalar types, an even spacing is possible if and only if the length of the range,
  235. * i.e., \c high-low is a scalar multiple of \c size-1, or if \c size is a scalar multiple of the
  236. * number of values \c high-low+1 (meaning each value can be repeated the same number of time).
  237. * If one of these two considions is not satisfied, then \c high is lowered to the largest value
  238. * satisfying one of this constraint.
  239. * Here are some examples:
  240. *
  241. * Example: \include DenseBase_LinSpacedInt.cpp
  242. * Output: \verbinclude DenseBase_LinSpacedInt.out
  243. *
  244. * \sa setLinSpaced(Index,const Scalar&,const Scalar&), CwiseNullaryOp
  245. */
  246. template<typename Derived>
  247. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
  248. DenseBase<Derived>::LinSpaced(Index size, const Scalar& low, const Scalar& high)
  249. {
  250. EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
  251. return DenseBase<Derived>::NullaryExpr(size, internal::linspaced_op<Scalar,PacketScalar>(low,high,size));
  252. }
  253. /**
  254. * \copydoc DenseBase::LinSpaced(Index, const Scalar&, const Scalar&)
  255. * Special version for fixed size types which does not require the size parameter.
  256. */
  257. template<typename Derived>
  258. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
  259. DenseBase<Derived>::LinSpaced(const Scalar& low, const Scalar& high)
  260. {
  261. EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
  262. EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
  263. return DenseBase<Derived>::NullaryExpr(Derived::SizeAtCompileTime, internal::linspaced_op<Scalar,PacketScalar>(low,high,Derived::SizeAtCompileTime));
  264. }
  265. /** \returns true if all coefficients in this matrix are approximately equal to \a val, to within precision \a prec */
  266. template<typename Derived>
  267. EIGEN_DEVICE_FUNC bool DenseBase<Derived>::isApproxToConstant
  268. (const Scalar& val, const RealScalar& prec) const
  269. {
  270. typename internal::nested_eval<Derived,1>::type self(derived());
  271. for(Index j = 0; j < cols(); ++j)
  272. for(Index i = 0; i < rows(); ++i)
  273. if(!internal::isApprox(self.coeff(i, j), val, prec))
  274. return false;
  275. return true;
  276. }
  277. /** This is just an alias for isApproxToConstant().
  278. *
  279. * \returns true if all coefficients in this matrix are approximately equal to \a value, to within precision \a prec */
  280. template<typename Derived>
  281. EIGEN_DEVICE_FUNC bool DenseBase<Derived>::isConstant
  282. (const Scalar& val, const RealScalar& prec) const
  283. {
  284. return isApproxToConstant(val, prec);
  285. }
  286. /** Alias for setConstant(): sets all coefficients in this expression to \a val.
  287. *
  288. * \sa setConstant(), Constant(), class CwiseNullaryOp
  289. */
  290. template<typename Derived>
  291. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void DenseBase<Derived>::fill(const Scalar& val)
  292. {
  293. setConstant(val);
  294. }
  295. /** Sets all coefficients in this expression to value \a val.
  296. *
  297. * \sa fill(), setConstant(Index,const Scalar&), setConstant(Index,Index,const Scalar&), setZero(), setOnes(), Constant(), class CwiseNullaryOp, setZero(), setOnes()
  298. */
  299. template<typename Derived>
  300. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setConstant(const Scalar& val)
  301. {
  302. return derived() = Constant(rows(), cols(), val);
  303. }
  304. /** Resizes to the given \a size, and sets all coefficients in this expression to the given value \a val.
  305. *
  306. * \only_for_vectors
  307. *
  308. * Example: \include Matrix_setConstant_int.cpp
  309. * Output: \verbinclude Matrix_setConstant_int.out
  310. *
  311. * \sa MatrixBase::setConstant(const Scalar&), setConstant(Index,Index,const Scalar&), class CwiseNullaryOp, MatrixBase::Constant(const Scalar&)
  312. */
  313. template<typename Derived>
  314. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
  315. PlainObjectBase<Derived>::setConstant(Index size, const Scalar& val)
  316. {
  317. resize(size);
  318. return setConstant(val);
  319. }
  320. /** Resizes to the given size, and sets all coefficients in this expression to the given value \a val.
  321. *
  322. * \param rows the new number of rows
  323. * \param cols the new number of columns
  324. * \param val the value to which all coefficients are set
  325. *
  326. * Example: \include Matrix_setConstant_int_int.cpp
  327. * Output: \verbinclude Matrix_setConstant_int_int.out
  328. *
  329. * \sa MatrixBase::setConstant(const Scalar&), setConstant(Index,const Scalar&), class CwiseNullaryOp, MatrixBase::Constant(const Scalar&)
  330. */
  331. template<typename Derived>
  332. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
  333. PlainObjectBase<Derived>::setConstant(Index rows, Index cols, const Scalar& val)
  334. {
  335. resize(rows, cols);
  336. return setConstant(val);
  337. }
  338. /**
  339. * \brief Sets a linearly spaced vector.
  340. *
  341. * The function generates 'size' equally spaced values in the closed interval [low,high].
  342. * When size is set to 1, a vector of length 1 containing 'high' is returned.
  343. *
  344. * \only_for_vectors
  345. *
  346. * Example: \include DenseBase_setLinSpaced.cpp
  347. * Output: \verbinclude DenseBase_setLinSpaced.out
  348. *
  349. * For integer scalar types, do not miss the explanations on the definition
  350. * of \link LinSpaced(Index,const Scalar&,const Scalar&) even spacing \endlink.
  351. *
  352. * \sa LinSpaced(Index,const Scalar&,const Scalar&), CwiseNullaryOp
  353. */
  354. template<typename Derived>
  355. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setLinSpaced(Index newSize, const Scalar& low, const Scalar& high)
  356. {
  357. EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
  358. return derived() = Derived::NullaryExpr(newSize, internal::linspaced_op<Scalar,PacketScalar>(low,high,newSize));
  359. }
  360. /**
  361. * \brief Sets a linearly spaced vector.
  362. *
  363. * The function fills \c *this with equally spaced values in the closed interval [low,high].
  364. * When size is set to 1, a vector of length 1 containing 'high' is returned.
  365. *
  366. * \only_for_vectors
  367. *
  368. * For integer scalar types, do not miss the explanations on the definition
  369. * of \link LinSpaced(Index,const Scalar&,const Scalar&) even spacing \endlink.
  370. *
  371. * \sa LinSpaced(Index,const Scalar&,const Scalar&), setLinSpaced(Index, const Scalar&, const Scalar&), CwiseNullaryOp
  372. */
  373. template<typename Derived>
  374. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setLinSpaced(const Scalar& low, const Scalar& high)
  375. {
  376. EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
  377. return setLinSpaced(size(), low, high);
  378. }
  379. // zero:
  380. /** \returns an expression of a zero matrix.
  381. *
  382. * The parameters \a rows and \a cols are the number of rows and of columns of
  383. * the returned matrix. Must be compatible with this MatrixBase type.
  384. *
  385. * This variant is meant to be used for dynamic-size matrix types. For fixed-size types,
  386. * it is redundant to pass \a rows and \a cols as arguments, so Zero() should be used
  387. * instead.
  388. *
  389. * Example: \include MatrixBase_zero_int_int.cpp
  390. * Output: \verbinclude MatrixBase_zero_int_int.out
  391. *
  392. * \sa Zero(), Zero(Index)
  393. */
  394. template<typename Derived>
  395. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
  396. DenseBase<Derived>::Zero(Index rows, Index cols)
  397. {
  398. return Constant(rows, cols, Scalar(0));
  399. }
  400. /** \returns an expression of a zero vector.
  401. *
  402. * The parameter \a size is the size of the returned vector.
  403. * Must be compatible with this MatrixBase type.
  404. *
  405. * \only_for_vectors
  406. *
  407. * This variant is meant to be used for dynamic-size vector types. For fixed-size types,
  408. * it is redundant to pass \a size as argument, so Zero() should be used
  409. * instead.
  410. *
  411. * Example: \include MatrixBase_zero_int.cpp
  412. * Output: \verbinclude MatrixBase_zero_int.out
  413. *
  414. * \sa Zero(), Zero(Index,Index)
  415. */
  416. template<typename Derived>
  417. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
  418. DenseBase<Derived>::Zero(Index size)
  419. {
  420. return Constant(size, Scalar(0));
  421. }
  422. /** \returns an expression of a fixed-size zero matrix or vector.
  423. *
  424. * This variant is only for fixed-size MatrixBase types. For dynamic-size types, you
  425. * need to use the variants taking size arguments.
  426. *
  427. * Example: \include MatrixBase_zero.cpp
  428. * Output: \verbinclude MatrixBase_zero.out
  429. *
  430. * \sa Zero(Index), Zero(Index,Index)
  431. */
  432. template<typename Derived>
  433. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
  434. DenseBase<Derived>::Zero()
  435. {
  436. return Constant(Scalar(0));
  437. }
  438. /** \returns true if *this is approximately equal to the zero matrix,
  439. * within the precision given by \a prec.
  440. *
  441. * Example: \include MatrixBase_isZero.cpp
  442. * Output: \verbinclude MatrixBase_isZero.out
  443. *
  444. * \sa class CwiseNullaryOp, Zero()
  445. */
  446. template<typename Derived>
  447. EIGEN_DEVICE_FUNC bool DenseBase<Derived>::isZero(const RealScalar& prec) const
  448. {
  449. typename internal::nested_eval<Derived,1>::type self(derived());
  450. for(Index j = 0; j < cols(); ++j)
  451. for(Index i = 0; i < rows(); ++i)
  452. if(!internal::isMuchSmallerThan(self.coeff(i, j), static_cast<Scalar>(1), prec))
  453. return false;
  454. return true;
  455. }
  456. /** Sets all coefficients in this expression to zero.
  457. *
  458. * Example: \include MatrixBase_setZero.cpp
  459. * Output: \verbinclude MatrixBase_setZero.out
  460. *
  461. * \sa class CwiseNullaryOp, Zero()
  462. */
  463. template<typename Derived>
  464. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setZero()
  465. {
  466. return setConstant(Scalar(0));
  467. }
  468. /** Resizes to the given \a size, and sets all coefficients in this expression to zero.
  469. *
  470. * \only_for_vectors
  471. *
  472. * Example: \include Matrix_setZero_int.cpp
  473. * Output: \verbinclude Matrix_setZero_int.out
  474. *
  475. * \sa DenseBase::setZero(), setZero(Index,Index), class CwiseNullaryOp, DenseBase::Zero()
  476. */
  477. template<typename Derived>
  478. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
  479. PlainObjectBase<Derived>::setZero(Index newSize)
  480. {
  481. resize(newSize);
  482. return setConstant(Scalar(0));
  483. }
  484. /** Resizes to the given size, and sets all coefficients in this expression to zero.
  485. *
  486. * \param rows the new number of rows
  487. * \param cols the new number of columns
  488. *
  489. * Example: \include Matrix_setZero_int_int.cpp
  490. * Output: \verbinclude Matrix_setZero_int_int.out
  491. *
  492. * \sa DenseBase::setZero(), setZero(Index), class CwiseNullaryOp, DenseBase::Zero()
  493. */
  494. template<typename Derived>
  495. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
  496. PlainObjectBase<Derived>::setZero(Index rows, Index cols)
  497. {
  498. resize(rows, cols);
  499. return setConstant(Scalar(0));
  500. }
  501. // ones:
  502. /** \returns an expression of a matrix where all coefficients equal one.
  503. *
  504. * The parameters \a rows and \a cols are the number of rows and of columns of
  505. * the returned matrix. Must be compatible with this MatrixBase type.
  506. *
  507. * This variant is meant to be used for dynamic-size matrix types. For fixed-size types,
  508. * it is redundant to pass \a rows and \a cols as arguments, so Ones() should be used
  509. * instead.
  510. *
  511. * Example: \include MatrixBase_ones_int_int.cpp
  512. * Output: \verbinclude MatrixBase_ones_int_int.out
  513. *
  514. * \sa Ones(), Ones(Index), isOnes(), class Ones
  515. */
  516. template<typename Derived>
  517. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
  518. DenseBase<Derived>::Ones(Index rows, Index cols)
  519. {
  520. return Constant(rows, cols, Scalar(1));
  521. }
  522. /** \returns an expression of a vector where all coefficients equal one.
  523. *
  524. * The parameter \a newSize is the size of the returned vector.
  525. * Must be compatible with this MatrixBase type.
  526. *
  527. * \only_for_vectors
  528. *
  529. * This variant is meant to be used for dynamic-size vector types. For fixed-size types,
  530. * it is redundant to pass \a size as argument, so Ones() should be used
  531. * instead.
  532. *
  533. * Example: \include MatrixBase_ones_int.cpp
  534. * Output: \verbinclude MatrixBase_ones_int.out
  535. *
  536. * \sa Ones(), Ones(Index,Index), isOnes(), class Ones
  537. */
  538. template<typename Derived>
  539. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
  540. DenseBase<Derived>::Ones(Index newSize)
  541. {
  542. return Constant(newSize, Scalar(1));
  543. }
  544. /** \returns an expression of a fixed-size matrix or vector where all coefficients equal one.
  545. *
  546. * This variant is only for fixed-size MatrixBase types. For dynamic-size types, you
  547. * need to use the variants taking size arguments.
  548. *
  549. * Example: \include MatrixBase_ones.cpp
  550. * Output: \verbinclude MatrixBase_ones.out
  551. *
  552. * \sa Ones(Index), Ones(Index,Index), isOnes(), class Ones
  553. */
  554. template<typename Derived>
  555. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
  556. DenseBase<Derived>::Ones()
  557. {
  558. return Constant(Scalar(1));
  559. }
  560. /** \returns true if *this is approximately equal to the matrix where all coefficients
  561. * are equal to 1, within the precision given by \a prec.
  562. *
  563. * Example: \include MatrixBase_isOnes.cpp
  564. * Output: \verbinclude MatrixBase_isOnes.out
  565. *
  566. * \sa class CwiseNullaryOp, Ones()
  567. */
  568. template<typename Derived>
  569. EIGEN_DEVICE_FUNC bool DenseBase<Derived>::isOnes
  570. (const RealScalar& prec) const
  571. {
  572. return isApproxToConstant(Scalar(1), prec);
  573. }
  574. /** Sets all coefficients in this expression to one.
  575. *
  576. * Example: \include MatrixBase_setOnes.cpp
  577. * Output: \verbinclude MatrixBase_setOnes.out
  578. *
  579. * \sa class CwiseNullaryOp, Ones()
  580. */
  581. template<typename Derived>
  582. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setOnes()
  583. {
  584. return setConstant(Scalar(1));
  585. }
  586. /** Resizes to the given \a newSize, and sets all coefficients in this expression to one.
  587. *
  588. * \only_for_vectors
  589. *
  590. * Example: \include Matrix_setOnes_int.cpp
  591. * Output: \verbinclude Matrix_setOnes_int.out
  592. *
  593. * \sa MatrixBase::setOnes(), setOnes(Index,Index), class CwiseNullaryOp, MatrixBase::Ones()
  594. */
  595. template<typename Derived>
  596. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
  597. PlainObjectBase<Derived>::setOnes(Index newSize)
  598. {
  599. resize(newSize);
  600. return setConstant(Scalar(1));
  601. }
  602. /** Resizes to the given size, and sets all coefficients in this expression to one.
  603. *
  604. * \param rows the new number of rows
  605. * \param cols the new number of columns
  606. *
  607. * Example: \include Matrix_setOnes_int_int.cpp
  608. * Output: \verbinclude Matrix_setOnes_int_int.out
  609. *
  610. * \sa MatrixBase::setOnes(), setOnes(Index), class CwiseNullaryOp, MatrixBase::Ones()
  611. */
  612. template<typename Derived>
  613. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
  614. PlainObjectBase<Derived>::setOnes(Index rows, Index cols)
  615. {
  616. resize(rows, cols);
  617. return setConstant(Scalar(1));
  618. }
  619. // Identity:
  620. /** \returns an expression of the identity matrix (not necessarily square).
  621. *
  622. * The parameters \a rows and \a cols are the number of rows and of columns of
  623. * the returned matrix. Must be compatible with this MatrixBase type.
  624. *
  625. * This variant is meant to be used for dynamic-size matrix types. For fixed-size types,
  626. * it is redundant to pass \a rows and \a cols as arguments, so Identity() should be used
  627. * instead.
  628. *
  629. * Example: \include MatrixBase_identity_int_int.cpp
  630. * Output: \verbinclude MatrixBase_identity_int_int.out
  631. *
  632. * \sa Identity(), setIdentity(), isIdentity()
  633. */
  634. template<typename Derived>
  635. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::IdentityReturnType
  636. MatrixBase<Derived>::Identity(Index rows, Index cols)
  637. {
  638. return DenseBase<Derived>::NullaryExpr(rows, cols, internal::scalar_identity_op<Scalar>());
  639. }
  640. /** \returns an expression of the identity matrix (not necessarily square).
  641. *
  642. * This variant is only for fixed-size MatrixBase types. For dynamic-size types, you
  643. * need to use the variant taking size arguments.
  644. *
  645. * Example: \include MatrixBase_identity.cpp
  646. * Output: \verbinclude MatrixBase_identity.out
  647. *
  648. * \sa Identity(Index,Index), setIdentity(), isIdentity()
  649. */
  650. template<typename Derived>
  651. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::IdentityReturnType
  652. MatrixBase<Derived>::Identity()
  653. {
  654. EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
  655. return MatrixBase<Derived>::NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, internal::scalar_identity_op<Scalar>());
  656. }
  657. /** \returns true if *this is approximately equal to the identity matrix
  658. * (not necessarily square),
  659. * within the precision given by \a prec.
  660. *
  661. * Example: \include MatrixBase_isIdentity.cpp
  662. * Output: \verbinclude MatrixBase_isIdentity.out
  663. *
  664. * \sa class CwiseNullaryOp, Identity(), Identity(Index,Index), setIdentity()
  665. */
  666. template<typename Derived>
  667. bool MatrixBase<Derived>::isIdentity
  668. (const RealScalar& prec) const
  669. {
  670. typename internal::nested_eval<Derived,1>::type self(derived());
  671. for(Index j = 0; j < cols(); ++j)
  672. {
  673. for(Index i = 0; i < rows(); ++i)
  674. {
  675. if(i == j)
  676. {
  677. if(!internal::isApprox(self.coeff(i, j), static_cast<Scalar>(1), prec))
  678. return false;
  679. }
  680. else
  681. {
  682. if(!internal::isMuchSmallerThan(self.coeff(i, j), static_cast<RealScalar>(1), prec))
  683. return false;
  684. }
  685. }
  686. }
  687. return true;
  688. }
  689. namespace internal {
  690. template<typename Derived, bool Big = (Derived::SizeAtCompileTime>=16)>
  691. struct setIdentity_impl
  692. {
  693. EIGEN_DEVICE_FUNC
  694. static EIGEN_STRONG_INLINE Derived& run(Derived& m)
  695. {
  696. return m = Derived::Identity(m.rows(), m.cols());
  697. }
  698. };
  699. template<typename Derived>
  700. struct setIdentity_impl<Derived, true>
  701. {
  702. EIGEN_DEVICE_FUNC
  703. static EIGEN_STRONG_INLINE Derived& run(Derived& m)
  704. {
  705. m.setZero();
  706. const Index size = numext::mini(m.rows(), m.cols());
  707. for(Index i = 0; i < size; ++i) m.coeffRef(i,i) = typename Derived::Scalar(1);
  708. return m;
  709. }
  710. };
  711. } // end namespace internal
  712. /** Writes the identity expression (not necessarily square) into *this.
  713. *
  714. * Example: \include MatrixBase_setIdentity.cpp
  715. * Output: \verbinclude MatrixBase_setIdentity.out
  716. *
  717. * \sa class CwiseNullaryOp, Identity(), Identity(Index,Index), isIdentity()
  718. */
  719. template<typename Derived>
  720. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity()
  721. {
  722. return internal::setIdentity_impl<Derived>::run(derived());
  723. }
  724. /** \brief Resizes to the given size, and writes the identity expression (not necessarily square) into *this.
  725. *
  726. * \param rows the new number of rows
  727. * \param cols the new number of columns
  728. *
  729. * Example: \include Matrix_setIdentity_int_int.cpp
  730. * Output: \verbinclude Matrix_setIdentity_int_int.out
  731. *
  732. * \sa MatrixBase::setIdentity(), class CwiseNullaryOp, MatrixBase::Identity()
  733. */
  734. template<typename Derived>
  735. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity(Index rows, Index cols)
  736. {
  737. derived().resize(rows, cols);
  738. return setIdentity();
  739. }
  740. /** \returns an expression of the i-th unit (basis) vector.
  741. *
  742. * \only_for_vectors
  743. *
  744. * \sa MatrixBase::Unit(Index), MatrixBase::UnitX(), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW()
  745. */
  746. template<typename Derived>
  747. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::Unit(Index newSize, Index i)
  748. {
  749. EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
  750. return BasisReturnType(SquareMatrixType::Identity(newSize,newSize), i);
  751. }
  752. /** \returns an expression of the i-th unit (basis) vector.
  753. *
  754. * \only_for_vectors
  755. *
  756. * This variant is for fixed-size vector only.
  757. *
  758. * \sa MatrixBase::Unit(Index,Index), MatrixBase::UnitX(), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW()
  759. */
  760. template<typename Derived>
  761. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::Unit(Index i)
  762. {
  763. EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
  764. return BasisReturnType(SquareMatrixType::Identity(),i);
  765. }
  766. /** \returns an expression of the X axis unit vector (1{,0}^*)
  767. *
  768. * \only_for_vectors
  769. *
  770. * \sa MatrixBase::Unit(Index,Index), MatrixBase::Unit(Index), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW()
  771. */
  772. template<typename Derived>
  773. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitX()
  774. { return Derived::Unit(0); }
  775. /** \returns an expression of the Y axis unit vector (0,1{,0}^*)
  776. *
  777. * \only_for_vectors
  778. *
  779. * \sa MatrixBase::Unit(Index,Index), MatrixBase::Unit(Index), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW()
  780. */
  781. template<typename Derived>
  782. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitY()
  783. { return Derived::Unit(1); }
  784. /** \returns an expression of the Z axis unit vector (0,0,1{,0}^*)
  785. *
  786. * \only_for_vectors
  787. *
  788. * \sa MatrixBase::Unit(Index,Index), MatrixBase::Unit(Index), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW()
  789. */
  790. template<typename Derived>
  791. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitZ()
  792. { return Derived::Unit(2); }
  793. /** \returns an expression of the W axis unit vector (0,0,0,1)
  794. *
  795. * \only_for_vectors
  796. *
  797. * \sa MatrixBase::Unit(Index,Index), MatrixBase::Unit(Index), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW()
  798. */
  799. template<typename Derived>
  800. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitW()
  801. { return Derived::Unit(3); }
  802. } // end namespace Eigen
  803. #endif // EIGEN_CWISE_NULLARY_OP_H