Random.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2008 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_RANDOM_H
  10. #define EIGEN_RANDOM_H
  11. namespace Eigen {
  12. namespace internal {
  13. template<typename Scalar> struct scalar_random_op {
  14. EIGEN_EMPTY_STRUCT_CTOR(scalar_random_op)
  15. inline const Scalar operator() () const { return random<Scalar>(); }
  16. };
  17. template<typename Scalar>
  18. struct functor_traits<scalar_random_op<Scalar> >
  19. { enum { Cost = 5 * NumTraits<Scalar>::MulCost, PacketAccess = false, IsRepeatable = false }; };
  20. } // end namespace internal
  21. /** \returns a random matrix expression
  22. *
  23. * Numbers are uniformly spread through their whole definition range for integer types,
  24. * and in the [-1:1] range for floating point scalar types.
  25. *
  26. * The parameters \a rows and \a cols are the number of rows and of columns of
  27. * the returned matrix. Must be compatible with this MatrixBase type.
  28. *
  29. * \not_reentrant
  30. *
  31. * This variant is meant to be used for dynamic-size matrix types. For fixed-size types,
  32. * it is redundant to pass \a rows and \a cols as arguments, so Random() should be used
  33. * instead.
  34. *
  35. *
  36. * Example: \include MatrixBase_random_int_int.cpp
  37. * Output: \verbinclude MatrixBase_random_int_int.out
  38. *
  39. * This expression has the "evaluate before nesting" flag so that it will be evaluated into
  40. * a temporary matrix whenever it is nested in a larger expression. This prevents unexpected
  41. * behavior with expressions involving random matrices.
  42. *
  43. * See DenseBase::NullaryExpr(Index, const CustomNullaryOp&) for an example using C++11 random generators.
  44. *
  45. * \sa DenseBase::setRandom(), DenseBase::Random(Index), DenseBase::Random()
  46. */
  47. template<typename Derived>
  48. inline const typename DenseBase<Derived>::RandomReturnType
  49. DenseBase<Derived>::Random(Index rows, Index cols)
  50. {
  51. return NullaryExpr(rows, cols, internal::scalar_random_op<Scalar>());
  52. }
  53. /** \returns a random vector expression
  54. *
  55. * Numbers are uniformly spread through their whole definition range for integer types,
  56. * and in the [-1:1] range for floating point scalar types.
  57. *
  58. * The parameter \a size is the size of the returned vector.
  59. * Must be compatible with this MatrixBase type.
  60. *
  61. * \only_for_vectors
  62. * \not_reentrant
  63. *
  64. * This variant is meant to be used for dynamic-size vector types. For fixed-size types,
  65. * it is redundant to pass \a size as argument, so Random() should be used
  66. * instead.
  67. *
  68. * Example: \include MatrixBase_random_int.cpp
  69. * Output: \verbinclude MatrixBase_random_int.out
  70. *
  71. * This expression has the "evaluate before nesting" flag so that it will be evaluated into
  72. * a temporary vector whenever it is nested in a larger expression. This prevents unexpected
  73. * behavior with expressions involving random matrices.
  74. *
  75. * \sa DenseBase::setRandom(), DenseBase::Random(Index,Index), DenseBase::Random()
  76. */
  77. template<typename Derived>
  78. inline const typename DenseBase<Derived>::RandomReturnType
  79. DenseBase<Derived>::Random(Index size)
  80. {
  81. return NullaryExpr(size, internal::scalar_random_op<Scalar>());
  82. }
  83. /** \returns a fixed-size random matrix or vector expression
  84. *
  85. * Numbers are uniformly spread through their whole definition range for integer types,
  86. * and in the [-1:1] range for floating point scalar types.
  87. *
  88. * This variant is only for fixed-size MatrixBase types. For dynamic-size types, you
  89. * need to use the variants taking size arguments.
  90. *
  91. * Example: \include MatrixBase_random.cpp
  92. * Output: \verbinclude MatrixBase_random.out
  93. *
  94. * This expression has the "evaluate before nesting" flag so that it will be evaluated into
  95. * a temporary matrix whenever it is nested in a larger expression. This prevents unexpected
  96. * behavior with expressions involving random matrices.
  97. *
  98. * \not_reentrant
  99. *
  100. * \sa DenseBase::setRandom(), DenseBase::Random(Index,Index), DenseBase::Random(Index)
  101. */
  102. template<typename Derived>
  103. inline const typename DenseBase<Derived>::RandomReturnType
  104. DenseBase<Derived>::Random()
  105. {
  106. return NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, internal::scalar_random_op<Scalar>());
  107. }
  108. /** Sets all coefficients in this expression to random values.
  109. *
  110. * Numbers are uniformly spread through their whole definition range for integer types,
  111. * and in the [-1:1] range for floating point scalar types.
  112. *
  113. * \not_reentrant
  114. *
  115. * Example: \include MatrixBase_setRandom.cpp
  116. * Output: \verbinclude MatrixBase_setRandom.out
  117. *
  118. * \sa class CwiseNullaryOp, setRandom(Index), setRandom(Index,Index)
  119. */
  120. template<typename Derived>
  121. inline Derived& DenseBase<Derived>::setRandom()
  122. {
  123. return *this = Random(rows(), cols());
  124. }
  125. /** Resizes to the given \a newSize, and sets all coefficients in this expression to random values.
  126. *
  127. * Numbers are uniformly spread through their whole definition range for integer types,
  128. * and in the [-1:1] range for floating point scalar types.
  129. *
  130. * \only_for_vectors
  131. * \not_reentrant
  132. *
  133. * Example: \include Matrix_setRandom_int.cpp
  134. * Output: \verbinclude Matrix_setRandom_int.out
  135. *
  136. * \sa DenseBase::setRandom(), setRandom(Index,Index), class CwiseNullaryOp, DenseBase::Random()
  137. */
  138. template<typename Derived>
  139. EIGEN_STRONG_INLINE Derived&
  140. PlainObjectBase<Derived>::setRandom(Index newSize)
  141. {
  142. resize(newSize);
  143. return setRandom();
  144. }
  145. /** Resizes to the given size, and sets all coefficients in this expression to random values.
  146. *
  147. * Numbers are uniformly spread through their whole definition range for integer types,
  148. * and in the [-1:1] range for floating point scalar types.
  149. *
  150. * \not_reentrant
  151. *
  152. * \param rows the new number of rows
  153. * \param cols the new number of columns
  154. *
  155. * Example: \include Matrix_setRandom_int_int.cpp
  156. * Output: \verbinclude Matrix_setRandom_int_int.out
  157. *
  158. * \sa DenseBase::setRandom(), setRandom(Index), class CwiseNullaryOp, DenseBase::Random()
  159. */
  160. template<typename Derived>
  161. EIGEN_STRONG_INLINE Derived&
  162. PlainObjectBase<Derived>::setRandom(Index rows, Index cols)
  163. {
  164. resize(rows, cols);
  165. return setRandom();
  166. }
  167. } // end namespace Eigen
  168. #endif // EIGEN_RANDOM_H