fn_randu.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
  2. // Copyright 2008-2016 National ICT Australia (NICTA)
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // ------------------------------------------------------------------------
  15. //! \addtogroup fn_randu
  16. //! @{
  17. arma_warn_unused
  18. inline
  19. double
  20. randu()
  21. {
  22. return double(arma_rng::randu<double>());
  23. }
  24. template<typename eT>
  25. arma_warn_unused
  26. inline
  27. typename arma_real_or_cx_only<eT>::result
  28. randu()
  29. {
  30. return eT(arma_rng::randu<eT>());
  31. }
  32. //! Generate a vector with all elements set to random values in the [0,1] interval (uniform distribution)
  33. arma_warn_unused
  34. arma_inline
  35. const Gen<vec, gen_randu>
  36. randu(const uword n_elem)
  37. {
  38. arma_extra_debug_sigprint();
  39. return Gen<vec, gen_randu>(n_elem, 1);
  40. }
  41. template<typename obj_type>
  42. arma_warn_unused
  43. arma_inline
  44. const Gen<obj_type, gen_randu>
  45. randu(const uword n_elem, const arma_empty_class junk1 = arma_empty_class(), const typename arma_Mat_Col_Row_only<obj_type>::result* junk2 = 0)
  46. {
  47. arma_extra_debug_sigprint();
  48. arma_ignore(junk1);
  49. arma_ignore(junk2);
  50. if(is_Row<obj_type>::value)
  51. {
  52. return Gen<obj_type, gen_randu>(1, n_elem);
  53. }
  54. else
  55. {
  56. return Gen<obj_type, gen_randu>(n_elem, 1);
  57. }
  58. }
  59. //! Generate a dense matrix with all elements set to random values in the [0,1] interval (uniform distribution)
  60. arma_warn_unused
  61. arma_inline
  62. const Gen<mat, gen_randu>
  63. randu(const uword n_rows, const uword n_cols)
  64. {
  65. arma_extra_debug_sigprint();
  66. return Gen<mat, gen_randu>(n_rows, n_cols);
  67. }
  68. arma_warn_unused
  69. arma_inline
  70. const Gen<mat, gen_randu>
  71. randu(const SizeMat& s)
  72. {
  73. arma_extra_debug_sigprint();
  74. return Gen<mat, gen_randu>(s.n_rows, s.n_cols);
  75. }
  76. template<typename obj_type>
  77. arma_warn_unused
  78. arma_inline
  79. const Gen<obj_type, gen_randu>
  80. randu(const uword n_rows, const uword n_cols, const typename arma_Mat_Col_Row_only<obj_type>::result* junk = 0)
  81. {
  82. arma_extra_debug_sigprint();
  83. arma_ignore(junk);
  84. if(is_Col<obj_type>::value)
  85. {
  86. arma_debug_check( (n_cols != 1), "randu(): incompatible size" );
  87. }
  88. else
  89. if(is_Row<obj_type>::value)
  90. {
  91. arma_debug_check( (n_rows != 1), "randu(): incompatible size" );
  92. }
  93. return Gen<obj_type, gen_randu>(n_rows, n_cols);
  94. }
  95. template<typename obj_type>
  96. arma_warn_unused
  97. arma_inline
  98. const Gen<obj_type, gen_randu>
  99. randu(const SizeMat& s, const typename arma_Mat_Col_Row_only<obj_type>::result* junk = 0)
  100. {
  101. arma_extra_debug_sigprint();
  102. arma_ignore(junk);
  103. return randu<obj_type>(s.n_rows, s.n_cols);
  104. }
  105. arma_warn_unused
  106. arma_inline
  107. const GenCube<cube::elem_type, gen_randu>
  108. randu(const uword n_rows, const uword n_cols, const uword n_slices)
  109. {
  110. arma_extra_debug_sigprint();
  111. return GenCube<cube::elem_type, gen_randu>(n_rows, n_cols, n_slices);
  112. }
  113. arma_warn_unused
  114. arma_inline
  115. const GenCube<cube::elem_type, gen_randu>
  116. randu(const SizeCube& s)
  117. {
  118. arma_extra_debug_sigprint();
  119. return GenCube<cube::elem_type, gen_randu>(s.n_rows, s.n_cols, s.n_slices);
  120. }
  121. template<typename cube_type>
  122. arma_warn_unused
  123. arma_inline
  124. const GenCube<typename cube_type::elem_type, gen_randu>
  125. randu(const uword n_rows, const uword n_cols, const uword n_slices, const typename arma_Cube_only<cube_type>::result* junk = 0)
  126. {
  127. arma_extra_debug_sigprint();
  128. arma_ignore(junk);
  129. return GenCube<typename cube_type::elem_type, gen_randu>(n_rows, n_cols, n_slices);
  130. }
  131. template<typename cube_type>
  132. arma_warn_unused
  133. arma_inline
  134. const GenCube<typename cube_type::elem_type, gen_randu>
  135. randu(const SizeCube& s, const typename arma_Cube_only<cube_type>::result* junk = 0)
  136. {
  137. arma_extra_debug_sigprint();
  138. arma_ignore(junk);
  139. return GenCube<typename cube_type::elem_type, gen_randu>(s.n_rows, s.n_cols, s.n_slices);
  140. }
  141. //! @}