fn_randg.hpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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_randg
  16. //! @{
  17. template<typename obj_type>
  18. arma_warn_unused
  19. inline
  20. obj_type
  21. randg(const uword n_rows, const uword n_cols, const distr_param& param = distr_param(), const typename arma_Mat_Col_Row_only<obj_type>::result* junk = 0)
  22. {
  23. arma_extra_debug_sigprint();
  24. arma_ignore(junk);
  25. #if defined(ARMA_USE_CXX11)
  26. {
  27. if(is_Col<obj_type>::value)
  28. {
  29. arma_debug_check( (n_cols != 1), "randg(): incompatible size" );
  30. }
  31. else
  32. if(is_Row<obj_type>::value)
  33. {
  34. arma_debug_check( (n_rows != 1), "randg(): incompatible size" );
  35. }
  36. obj_type out(n_rows, n_cols);
  37. double a;
  38. double b;
  39. if(param.state == 0)
  40. {
  41. a = double(1);
  42. b = double(1);
  43. }
  44. else
  45. if(param.state == 1)
  46. {
  47. a = double(param.a_int);
  48. b = double(param.b_int);
  49. }
  50. else
  51. {
  52. a = param.a_double;
  53. b = param.b_double;
  54. }
  55. arma_debug_check( ((a <= double(0)) || (b <= double(0))), "randg(): a and b must be greater than zero" );
  56. #if defined(ARMA_USE_EXTERN_CXX11_RNG)
  57. {
  58. arma_rng_cxx11_instance.randg_fill(out.memptr(), out.n_elem, a, b);
  59. }
  60. #else
  61. {
  62. arma_rng_cxx11 local_arma_rng_cxx11_instance;
  63. typedef typename arma_rng_cxx11::seed_type seed_type;
  64. local_arma_rng_cxx11_instance.set_seed( seed_type(arma_rng::randi<seed_type>()) );
  65. local_arma_rng_cxx11_instance.randg_fill(out.memptr(), out.n_elem, a, b);
  66. }
  67. #endif
  68. return out;
  69. }
  70. #else
  71. {
  72. arma_ignore(n_rows);
  73. arma_ignore(n_cols);
  74. arma_ignore(param);
  75. arma_stop_logic_error("randg(): C++11 compiler required");
  76. return obj_type();
  77. }
  78. #endif
  79. }
  80. template<typename obj_type>
  81. arma_warn_unused
  82. inline
  83. obj_type
  84. randg(const SizeMat& s, const distr_param& param = distr_param(), const typename arma_Mat_Col_Row_only<obj_type>::result* junk = 0)
  85. {
  86. arma_extra_debug_sigprint();
  87. arma_ignore(junk);
  88. return randg<obj_type>(s.n_rows, s.n_cols, param);
  89. }
  90. template<typename obj_type>
  91. arma_warn_unused
  92. inline
  93. obj_type
  94. randg(const uword n_elem, const distr_param& param = distr_param(), const arma_empty_class junk1 = arma_empty_class(), const typename arma_Mat_Col_Row_only<obj_type>::result* junk2 = 0)
  95. {
  96. arma_extra_debug_sigprint();
  97. arma_ignore(junk1);
  98. arma_ignore(junk2);
  99. if(is_Row<obj_type>::value)
  100. {
  101. return randg<obj_type>(1, n_elem, param);
  102. }
  103. else
  104. {
  105. return randg<obj_type>(n_elem, 1, param);
  106. }
  107. }
  108. arma_warn_unused
  109. inline
  110. mat
  111. randg(const uword n_rows, const uword n_cols, const distr_param& param = distr_param())
  112. {
  113. arma_extra_debug_sigprint();
  114. return randg<mat>(n_rows, n_cols, param);
  115. }
  116. arma_warn_unused
  117. inline
  118. mat
  119. randg(const SizeMat& s, const distr_param& param = distr_param())
  120. {
  121. arma_extra_debug_sigprint();
  122. return randg<mat>(s.n_rows, s.n_cols, param);
  123. }
  124. arma_warn_unused
  125. inline
  126. vec
  127. randg(const uword n_elem, const distr_param& param = distr_param())
  128. {
  129. arma_extra_debug_sigprint();
  130. return randg<vec>(n_elem, uword(1), param);
  131. }
  132. arma_warn_unused
  133. inline
  134. double
  135. randg(const distr_param& param = distr_param())
  136. {
  137. arma_extra_debug_sigprint();
  138. return as_scalar( randg<vec>(uword(1), uword(1), param) );
  139. }
  140. template<typename eT>
  141. arma_warn_unused
  142. inline
  143. typename arma_real_or_cx_only<eT>::result
  144. randg(const distr_param& param = distr_param())
  145. {
  146. return eT( as_scalar( randg< Col<eT> >(uword(1), uword(1), param) ) );
  147. }
  148. template<typename cube_type>
  149. arma_warn_unused
  150. inline
  151. cube_type
  152. randg(const uword n_rows, const uword n_cols, const uword n_slices, const distr_param& param = distr_param(), const typename arma_Cube_only<cube_type>::result* junk = 0)
  153. {
  154. arma_extra_debug_sigprint();
  155. arma_ignore(junk);
  156. #if defined(ARMA_USE_CXX11)
  157. {
  158. cube_type out(n_rows, n_cols, n_slices);
  159. double a;
  160. double b;
  161. if(param.state == 0)
  162. {
  163. a = double(1);
  164. b = double(1);
  165. }
  166. else
  167. if(param.state == 1)
  168. {
  169. a = double(param.a_int);
  170. b = double(param.b_int);
  171. }
  172. else
  173. {
  174. a = param.a_double;
  175. b = param.b_double;
  176. }
  177. arma_debug_check( ((a <= double(0)) || (b <= double(0))), "randg(): a and b must be greater than zero" );
  178. #if defined(ARMA_USE_EXTERN_CXX11_RNG)
  179. {
  180. arma_rng_cxx11_instance.randg_fill(out.memptr(), out.n_elem, a, b);
  181. }
  182. #else
  183. {
  184. arma_rng_cxx11 local_arma_rng_cxx11_instance;
  185. typedef typename arma_rng_cxx11::seed_type seed_type;
  186. local_arma_rng_cxx11_instance.set_seed( seed_type(arma_rng::randi<seed_type>()) );
  187. local_arma_rng_cxx11_instance.randg_fill(out.memptr(), out.n_elem, a, b);
  188. }
  189. #endif
  190. return out;
  191. }
  192. #else
  193. {
  194. arma_ignore(n_rows);
  195. arma_ignore(n_cols);
  196. arma_ignore(n_slices);
  197. arma_ignore(param);
  198. arma_stop_logic_error("randg(): C++11 compiler required");
  199. return cube_type();
  200. }
  201. #endif
  202. }
  203. template<typename cube_type>
  204. arma_warn_unused
  205. inline
  206. cube_type
  207. randg(const SizeCube& s, const distr_param& param = distr_param(), const typename arma_Cube_only<cube_type>::result* junk = 0)
  208. {
  209. arma_extra_debug_sigprint();
  210. arma_ignore(junk);
  211. return randg<cube_type>(s.n_rows, s.n_cols, s.n_slices, param);
  212. }
  213. arma_warn_unused
  214. inline
  215. cube
  216. randg(const uword n_rows, const uword n_cols, const uword n_slices, const distr_param& param = distr_param())
  217. {
  218. arma_extra_debug_sigprint();
  219. return randg<cube>(n_rows, n_cols, n_slices, param);
  220. }
  221. arma_warn_unused
  222. inline
  223. cube
  224. randg(const SizeCube& s, const distr_param& param = distr_param())
  225. {
  226. arma_extra_debug_sigprint();
  227. return randg<cube>(s.n_rows, s.n_cols, s.n_slices, param);
  228. }
  229. //! @}