fn_randi.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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_randi
  16. //! @{
  17. template<typename obj_type>
  18. arma_warn_unused
  19. inline
  20. obj_type
  21. randi(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. typedef typename obj_type::elem_type eT;
  26. if(is_Col<obj_type>::value)
  27. {
  28. arma_debug_check( (n_cols != 1), "randi(): incompatible size" );
  29. }
  30. else
  31. if(is_Row<obj_type>::value)
  32. {
  33. arma_debug_check( (n_rows != 1), "randi(): incompatible size" );
  34. }
  35. obj_type out(n_rows, n_cols);
  36. int a;
  37. int b;
  38. if(param.state == 0)
  39. {
  40. a = 0;
  41. b = arma_rng::randi<eT>::max_val();
  42. }
  43. else
  44. if(param.state == 1)
  45. {
  46. a = param.a_int;
  47. b = param.b_int;
  48. }
  49. else
  50. {
  51. a = int(param.a_double);
  52. b = int(param.b_double);
  53. }
  54. arma_debug_check( (a > b), "randi(): incorrect distribution parameters: a must be less than b" );
  55. arma_rng::randi<eT>::fill(out.memptr(), out.n_elem, a, b);
  56. return out;
  57. }
  58. template<typename obj_type>
  59. arma_warn_unused
  60. inline
  61. obj_type
  62. randi(const SizeMat& s, const distr_param& param = distr_param(), const typename arma_Mat_Col_Row_only<obj_type>::result* junk = 0)
  63. {
  64. arma_extra_debug_sigprint();
  65. arma_ignore(junk);
  66. return randi<obj_type>(s.n_rows, s.n_cols, param);
  67. }
  68. template<typename obj_type>
  69. arma_warn_unused
  70. inline
  71. obj_type
  72. randi(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)
  73. {
  74. arma_extra_debug_sigprint();
  75. arma_ignore(junk1);
  76. arma_ignore(junk2);
  77. if(is_Row<obj_type>::value)
  78. {
  79. return randi<obj_type>(1, n_elem, param);
  80. }
  81. else
  82. {
  83. return randi<obj_type>(n_elem, 1, param);
  84. }
  85. }
  86. arma_warn_unused
  87. inline
  88. imat
  89. randi(const uword n_rows, const uword n_cols, const distr_param& param = distr_param())
  90. {
  91. arma_extra_debug_sigprint();
  92. return randi<imat>(n_rows, n_cols, param);
  93. }
  94. arma_warn_unused
  95. inline
  96. imat
  97. randi(const SizeMat& s, const distr_param& param = distr_param())
  98. {
  99. arma_extra_debug_sigprint();
  100. return randi<imat>(s.n_rows, s.n_cols, param);
  101. }
  102. arma_warn_unused
  103. inline
  104. ivec
  105. randi(const uword n_elem, const distr_param& param = distr_param())
  106. {
  107. arma_extra_debug_sigprint();
  108. return randi<ivec>(n_elem, uword(1), param);
  109. }
  110. arma_warn_unused
  111. inline
  112. sword
  113. randi(const distr_param& param)
  114. {
  115. return as_scalar( randi<ivec>(uword(1), uword(1), param) );
  116. }
  117. template<typename eT>
  118. arma_warn_unused
  119. inline
  120. typename arma_scalar_only<eT>::result
  121. randi(const distr_param& param)
  122. {
  123. return eT( as_scalar( randi< Col<eT> >(uword(1), uword(1), param) ) );
  124. }
  125. arma_warn_unused
  126. inline
  127. sword
  128. randi()
  129. {
  130. return sword( arma_rng::randi<sword>() );
  131. }
  132. template<typename eT>
  133. arma_warn_unused
  134. inline
  135. typename arma_scalar_only<eT>::result
  136. randi()
  137. {
  138. return eT( arma_rng::randi<eT>() );
  139. }
  140. template<typename cube_type>
  141. arma_warn_unused
  142. inline
  143. cube_type
  144. randi(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)
  145. {
  146. arma_extra_debug_sigprint();
  147. arma_ignore(junk);
  148. typedef typename cube_type::elem_type eT;
  149. cube_type out(n_rows, n_cols, n_slices);
  150. int a;
  151. int b;
  152. if(param.state == 0)
  153. {
  154. a = 0;
  155. b = arma_rng::randi<eT>::max_val();
  156. }
  157. else
  158. if(param.state == 1)
  159. {
  160. a = param.a_int;
  161. b = param.b_int;
  162. }
  163. else
  164. {
  165. a = int(param.a_double);
  166. b = int(param.b_double);
  167. }
  168. arma_debug_check( (a > b), "randi(): incorrect distribution parameters: a must be less than b" );
  169. arma_rng::randi<eT>::fill(out.memptr(), out.n_elem, a, b);
  170. return out;
  171. }
  172. template<typename cube_type>
  173. arma_warn_unused
  174. inline
  175. cube_type
  176. randi(const SizeCube& s, const distr_param& param = distr_param(), const typename arma_Cube_only<cube_type>::result* junk = 0)
  177. {
  178. arma_extra_debug_sigprint();
  179. arma_ignore(junk);
  180. return randi<cube_type>(s.n_rows, s.n_cols, s.n_slices, param);
  181. }
  182. arma_warn_unused
  183. inline
  184. icube
  185. randi(const uword n_rows, const uword n_cols, const uword n_slices, const distr_param& param = distr_param())
  186. {
  187. arma_extra_debug_sigprint();
  188. return randi<icube>(n_rows, n_cols, n_slices, param);
  189. }
  190. arma_warn_unused
  191. inline
  192. icube
  193. randi(const SizeCube& s, const distr_param& param = distr_param())
  194. {
  195. arma_extra_debug_sigprint();
  196. return randi<icube>(s.n_rows, s.n_cols, s.n_slices, param);
  197. }
  198. //! @}