numpy 2.3.5__cp313-cp313-macosx_14_0_arm64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of numpy might be problematic. Click here for more details.

Files changed (897) hide show
  1. numpy/__config__.py +170 -0
  2. numpy/__config__.pyi +102 -0
  3. numpy/__init__.cython-30.pxd +1241 -0
  4. numpy/__init__.pxd +1154 -0
  5. numpy/__init__.py +945 -0
  6. numpy/__init__.pyi +6147 -0
  7. numpy/_array_api_info.py +346 -0
  8. numpy/_array_api_info.pyi +207 -0
  9. numpy/_configtool.py +39 -0
  10. numpy/_configtool.pyi +1 -0
  11. numpy/_core/__init__.py +186 -0
  12. numpy/_core/__init__.pyi +2 -0
  13. numpy/_core/_add_newdocs.py +6967 -0
  14. numpy/_core/_add_newdocs.pyi +3 -0
  15. numpy/_core/_add_newdocs_scalars.py +390 -0
  16. numpy/_core/_add_newdocs_scalars.pyi +16 -0
  17. numpy/_core/_asarray.py +134 -0
  18. numpy/_core/_asarray.pyi +41 -0
  19. numpy/_core/_dtype.py +366 -0
  20. numpy/_core/_dtype.pyi +58 -0
  21. numpy/_core/_dtype_ctypes.py +120 -0
  22. numpy/_core/_dtype_ctypes.pyi +83 -0
  23. numpy/_core/_exceptions.py +162 -0
  24. numpy/_core/_exceptions.pyi +55 -0
  25. numpy/_core/_internal.py +958 -0
  26. numpy/_core/_internal.pyi +72 -0
  27. numpy/_core/_machar.py +355 -0
  28. numpy/_core/_machar.pyi +55 -0
  29. numpy/_core/_methods.py +255 -0
  30. numpy/_core/_methods.pyi +22 -0
  31. numpy/_core/_multiarray_tests.cpython-313-darwin.so +0 -0
  32. numpy/_core/_multiarray_umath.cpython-313-darwin.so +0 -0
  33. numpy/_core/_operand_flag_tests.cpython-313-darwin.so +0 -0
  34. numpy/_core/_rational_tests.cpython-313-darwin.so +0 -0
  35. numpy/_core/_simd.cpython-313-darwin.so +0 -0
  36. numpy/_core/_simd.pyi +25 -0
  37. numpy/_core/_string_helpers.py +100 -0
  38. numpy/_core/_string_helpers.pyi +12 -0
  39. numpy/_core/_struct_ufunc_tests.cpython-313-darwin.so +0 -0
  40. numpy/_core/_type_aliases.py +119 -0
  41. numpy/_core/_type_aliases.pyi +97 -0
  42. numpy/_core/_ufunc_config.py +491 -0
  43. numpy/_core/_ufunc_config.pyi +78 -0
  44. numpy/_core/_umath_tests.cpython-313-darwin.so +0 -0
  45. numpy/_core/arrayprint.py +1775 -0
  46. numpy/_core/arrayprint.pyi +238 -0
  47. numpy/_core/cversions.py +13 -0
  48. numpy/_core/defchararray.py +1427 -0
  49. numpy/_core/defchararray.pyi +1135 -0
  50. numpy/_core/einsumfunc.py +1498 -0
  51. numpy/_core/einsumfunc.pyi +184 -0
  52. numpy/_core/fromnumeric.py +4269 -0
  53. numpy/_core/fromnumeric.pyi +1750 -0
  54. numpy/_core/function_base.py +545 -0
  55. numpy/_core/function_base.pyi +278 -0
  56. numpy/_core/getlimits.py +748 -0
  57. numpy/_core/getlimits.pyi +3 -0
  58. numpy/_core/include/numpy/__multiarray_api.c +376 -0
  59. numpy/_core/include/numpy/__multiarray_api.h +1628 -0
  60. numpy/_core/include/numpy/__ufunc_api.c +54 -0
  61. numpy/_core/include/numpy/__ufunc_api.h +341 -0
  62. numpy/_core/include/numpy/_neighborhood_iterator_imp.h +90 -0
  63. numpy/_core/include/numpy/_numpyconfig.h +33 -0
  64. numpy/_core/include/numpy/_public_dtype_api_table.h +86 -0
  65. numpy/_core/include/numpy/arrayobject.h +7 -0
  66. numpy/_core/include/numpy/arrayscalars.h +196 -0
  67. numpy/_core/include/numpy/dtype_api.h +480 -0
  68. numpy/_core/include/numpy/halffloat.h +70 -0
  69. numpy/_core/include/numpy/ndarrayobject.h +304 -0
  70. numpy/_core/include/numpy/ndarraytypes.h +1950 -0
  71. numpy/_core/include/numpy/npy_2_compat.h +249 -0
  72. numpy/_core/include/numpy/npy_2_complexcompat.h +28 -0
  73. numpy/_core/include/numpy/npy_3kcompat.h +374 -0
  74. numpy/_core/include/numpy/npy_common.h +977 -0
  75. numpy/_core/include/numpy/npy_cpu.h +124 -0
  76. numpy/_core/include/numpy/npy_endian.h +78 -0
  77. numpy/_core/include/numpy/npy_math.h +602 -0
  78. numpy/_core/include/numpy/npy_no_deprecated_api.h +20 -0
  79. numpy/_core/include/numpy/npy_os.h +42 -0
  80. numpy/_core/include/numpy/numpyconfig.h +182 -0
  81. numpy/_core/include/numpy/random/LICENSE.txt +21 -0
  82. numpy/_core/include/numpy/random/bitgen.h +20 -0
  83. numpy/_core/include/numpy/random/distributions.h +209 -0
  84. numpy/_core/include/numpy/random/libdivide.h +2079 -0
  85. numpy/_core/include/numpy/ufuncobject.h +343 -0
  86. numpy/_core/include/numpy/utils.h +37 -0
  87. numpy/_core/lib/libnpymath.a +0 -0
  88. numpy/_core/lib/npy-pkg-config/mlib.ini +12 -0
  89. numpy/_core/lib/npy-pkg-config/npymath.ini +20 -0
  90. numpy/_core/lib/pkgconfig/numpy.pc +7 -0
  91. numpy/_core/memmap.py +363 -0
  92. numpy/_core/memmap.pyi +3 -0
  93. numpy/_core/multiarray.py +1762 -0
  94. numpy/_core/multiarray.pyi +1285 -0
  95. numpy/_core/numeric.py +2760 -0
  96. numpy/_core/numeric.pyi +882 -0
  97. numpy/_core/numerictypes.py +633 -0
  98. numpy/_core/numerictypes.pyi +197 -0
  99. numpy/_core/overrides.py +183 -0
  100. numpy/_core/overrides.pyi +48 -0
  101. numpy/_core/printoptions.py +32 -0
  102. numpy/_core/printoptions.pyi +28 -0
  103. numpy/_core/records.py +1089 -0
  104. numpy/_core/records.pyi +333 -0
  105. numpy/_core/shape_base.py +998 -0
  106. numpy/_core/shape_base.pyi +175 -0
  107. numpy/_core/strings.py +1829 -0
  108. numpy/_core/strings.pyi +511 -0
  109. numpy/_core/tests/_locales.py +72 -0
  110. numpy/_core/tests/_natype.py +205 -0
  111. numpy/_core/tests/data/astype_copy.pkl +0 -0
  112. numpy/_core/tests/data/generate_umath_validation_data.cpp +170 -0
  113. numpy/_core/tests/data/recarray_from_file.fits +0 -0
  114. numpy/_core/tests/data/umath-validation-set-README.txt +15 -0
  115. numpy/_core/tests/data/umath-validation-set-arccos.csv +1429 -0
  116. numpy/_core/tests/data/umath-validation-set-arccosh.csv +1429 -0
  117. numpy/_core/tests/data/umath-validation-set-arcsin.csv +1429 -0
  118. numpy/_core/tests/data/umath-validation-set-arcsinh.csv +1429 -0
  119. numpy/_core/tests/data/umath-validation-set-arctan.csv +1429 -0
  120. numpy/_core/tests/data/umath-validation-set-arctanh.csv +1429 -0
  121. numpy/_core/tests/data/umath-validation-set-cbrt.csv +1429 -0
  122. numpy/_core/tests/data/umath-validation-set-cos.csv +1375 -0
  123. numpy/_core/tests/data/umath-validation-set-cosh.csv +1429 -0
  124. numpy/_core/tests/data/umath-validation-set-exp.csv +412 -0
  125. numpy/_core/tests/data/umath-validation-set-exp2.csv +1429 -0
  126. numpy/_core/tests/data/umath-validation-set-expm1.csv +1429 -0
  127. numpy/_core/tests/data/umath-validation-set-log.csv +271 -0
  128. numpy/_core/tests/data/umath-validation-set-log10.csv +1629 -0
  129. numpy/_core/tests/data/umath-validation-set-log1p.csv +1429 -0
  130. numpy/_core/tests/data/umath-validation-set-log2.csv +1629 -0
  131. numpy/_core/tests/data/umath-validation-set-sin.csv +1370 -0
  132. numpy/_core/tests/data/umath-validation-set-sinh.csv +1429 -0
  133. numpy/_core/tests/data/umath-validation-set-tan.csv +1429 -0
  134. numpy/_core/tests/data/umath-validation-set-tanh.csv +1429 -0
  135. numpy/_core/tests/examples/cython/checks.pyx +373 -0
  136. numpy/_core/tests/examples/cython/meson.build +43 -0
  137. numpy/_core/tests/examples/cython/setup.py +39 -0
  138. numpy/_core/tests/examples/limited_api/limited_api1.c +17 -0
  139. numpy/_core/tests/examples/limited_api/limited_api2.pyx +11 -0
  140. numpy/_core/tests/examples/limited_api/limited_api_latest.c +19 -0
  141. numpy/_core/tests/examples/limited_api/meson.build +59 -0
  142. numpy/_core/tests/examples/limited_api/setup.py +24 -0
  143. numpy/_core/tests/test__exceptions.py +90 -0
  144. numpy/_core/tests/test_abc.py +54 -0
  145. numpy/_core/tests/test_api.py +654 -0
  146. numpy/_core/tests/test_argparse.py +92 -0
  147. numpy/_core/tests/test_array_api_info.py +113 -0
  148. numpy/_core/tests/test_array_coercion.py +911 -0
  149. numpy/_core/tests/test_array_interface.py +222 -0
  150. numpy/_core/tests/test_arraymethod.py +84 -0
  151. numpy/_core/tests/test_arrayobject.py +75 -0
  152. numpy/_core/tests/test_arrayprint.py +1328 -0
  153. numpy/_core/tests/test_casting_floatingpoint_errors.py +154 -0
  154. numpy/_core/tests/test_casting_unittests.py +817 -0
  155. numpy/_core/tests/test_conversion_utils.py +206 -0
  156. numpy/_core/tests/test_cpu_dispatcher.py +49 -0
  157. numpy/_core/tests/test_cpu_features.py +432 -0
  158. numpy/_core/tests/test_custom_dtypes.py +315 -0
  159. numpy/_core/tests/test_cython.py +351 -0
  160. numpy/_core/tests/test_datetime.py +2734 -0
  161. numpy/_core/tests/test_defchararray.py +825 -0
  162. numpy/_core/tests/test_deprecations.py +454 -0
  163. numpy/_core/tests/test_dlpack.py +190 -0
  164. numpy/_core/tests/test_dtype.py +1995 -0
  165. numpy/_core/tests/test_einsum.py +1317 -0
  166. numpy/_core/tests/test_errstate.py +131 -0
  167. numpy/_core/tests/test_extint128.py +217 -0
  168. numpy/_core/tests/test_function_base.py +503 -0
  169. numpy/_core/tests/test_getlimits.py +205 -0
  170. numpy/_core/tests/test_half.py +568 -0
  171. numpy/_core/tests/test_hashtable.py +35 -0
  172. numpy/_core/tests/test_indexerrors.py +125 -0
  173. numpy/_core/tests/test_indexing.py +1455 -0
  174. numpy/_core/tests/test_item_selection.py +167 -0
  175. numpy/_core/tests/test_limited_api.py +102 -0
  176. numpy/_core/tests/test_longdouble.py +369 -0
  177. numpy/_core/tests/test_machar.py +30 -0
  178. numpy/_core/tests/test_mem_overlap.py +930 -0
  179. numpy/_core/tests/test_mem_policy.py +452 -0
  180. numpy/_core/tests/test_memmap.py +246 -0
  181. numpy/_core/tests/test_multiarray.py +10577 -0
  182. numpy/_core/tests/test_multithreading.py +292 -0
  183. numpy/_core/tests/test_nditer.py +3498 -0
  184. numpy/_core/tests/test_nep50_promotions.py +287 -0
  185. numpy/_core/tests/test_numeric.py +4247 -0
  186. numpy/_core/tests/test_numerictypes.py +651 -0
  187. numpy/_core/tests/test_overrides.py +791 -0
  188. numpy/_core/tests/test_print.py +200 -0
  189. numpy/_core/tests/test_protocols.py +46 -0
  190. numpy/_core/tests/test_records.py +544 -0
  191. numpy/_core/tests/test_regression.py +2670 -0
  192. numpy/_core/tests/test_scalar_ctors.py +207 -0
  193. numpy/_core/tests/test_scalar_methods.py +246 -0
  194. numpy/_core/tests/test_scalarbuffer.py +153 -0
  195. numpy/_core/tests/test_scalarinherit.py +105 -0
  196. numpy/_core/tests/test_scalarmath.py +1176 -0
  197. numpy/_core/tests/test_scalarprint.py +403 -0
  198. numpy/_core/tests/test_shape_base.py +891 -0
  199. numpy/_core/tests/test_simd.py +1341 -0
  200. numpy/_core/tests/test_simd_module.py +103 -0
  201. numpy/_core/tests/test_stringdtype.py +1814 -0
  202. numpy/_core/tests/test_strings.py +1499 -0
  203. numpy/_core/tests/test_ufunc.py +3313 -0
  204. numpy/_core/tests/test_umath.py +4928 -0
  205. numpy/_core/tests/test_umath_accuracy.py +124 -0
  206. numpy/_core/tests/test_umath_complex.py +626 -0
  207. numpy/_core/tests/test_unicode.py +368 -0
  208. numpy/_core/umath.py +60 -0
  209. numpy/_core/umath.pyi +197 -0
  210. numpy/_distributor_init.py +15 -0
  211. numpy/_distributor_init.pyi +1 -0
  212. numpy/_expired_attrs_2_0.py +79 -0
  213. numpy/_expired_attrs_2_0.pyi +62 -0
  214. numpy/_globals.py +96 -0
  215. numpy/_globals.pyi +17 -0
  216. numpy/_pyinstaller/__init__.py +0 -0
  217. numpy/_pyinstaller/__init__.pyi +0 -0
  218. numpy/_pyinstaller/hook-numpy.py +36 -0
  219. numpy/_pyinstaller/hook-numpy.pyi +13 -0
  220. numpy/_pyinstaller/tests/__init__.py +16 -0
  221. numpy/_pyinstaller/tests/pyinstaller-smoke.py +32 -0
  222. numpy/_pyinstaller/tests/test_pyinstaller.py +35 -0
  223. numpy/_pytesttester.py +201 -0
  224. numpy/_pytesttester.pyi +18 -0
  225. numpy/_typing/__init__.py +148 -0
  226. numpy/_typing/_add_docstring.py +153 -0
  227. numpy/_typing/_array_like.py +106 -0
  228. numpy/_typing/_char_codes.py +213 -0
  229. numpy/_typing/_dtype_like.py +114 -0
  230. numpy/_typing/_extended_precision.py +15 -0
  231. numpy/_typing/_nbit.py +19 -0
  232. numpy/_typing/_nbit_base.py +94 -0
  233. numpy/_typing/_nbit_base.pyi +40 -0
  234. numpy/_typing/_nested_sequence.py +79 -0
  235. numpy/_typing/_scalars.py +20 -0
  236. numpy/_typing/_shape.py +8 -0
  237. numpy/_typing/_ufunc.py +7 -0
  238. numpy/_typing/_ufunc.pyi +941 -0
  239. numpy/_utils/__init__.py +95 -0
  240. numpy/_utils/__init__.pyi +30 -0
  241. numpy/_utils/_convertions.py +18 -0
  242. numpy/_utils/_convertions.pyi +4 -0
  243. numpy/_utils/_inspect.py +192 -0
  244. numpy/_utils/_inspect.pyi +71 -0
  245. numpy/_utils/_pep440.py +486 -0
  246. numpy/_utils/_pep440.pyi +121 -0
  247. numpy/char/__init__.py +2 -0
  248. numpy/char/__init__.pyi +111 -0
  249. numpy/conftest.py +258 -0
  250. numpy/core/__init__.py +33 -0
  251. numpy/core/__init__.pyi +0 -0
  252. numpy/core/_dtype.py +10 -0
  253. numpy/core/_dtype.pyi +0 -0
  254. numpy/core/_dtype_ctypes.py +10 -0
  255. numpy/core/_dtype_ctypes.pyi +0 -0
  256. numpy/core/_internal.py +27 -0
  257. numpy/core/_multiarray_umath.py +57 -0
  258. numpy/core/_utils.py +21 -0
  259. numpy/core/arrayprint.py +10 -0
  260. numpy/core/defchararray.py +10 -0
  261. numpy/core/einsumfunc.py +10 -0
  262. numpy/core/fromnumeric.py +10 -0
  263. numpy/core/function_base.py +10 -0
  264. numpy/core/getlimits.py +10 -0
  265. numpy/core/multiarray.py +25 -0
  266. numpy/core/numeric.py +12 -0
  267. numpy/core/numerictypes.py +10 -0
  268. numpy/core/overrides.py +10 -0
  269. numpy/core/overrides.pyi +7 -0
  270. numpy/core/records.py +10 -0
  271. numpy/core/shape_base.py +10 -0
  272. numpy/core/umath.py +10 -0
  273. numpy/ctypeslib/__init__.py +13 -0
  274. numpy/ctypeslib/__init__.pyi +33 -0
  275. numpy/ctypeslib/_ctypeslib.py +603 -0
  276. numpy/ctypeslib/_ctypeslib.pyi +245 -0
  277. numpy/doc/ufuncs.py +138 -0
  278. numpy/dtypes.py +41 -0
  279. numpy/dtypes.pyi +631 -0
  280. numpy/exceptions.py +247 -0
  281. numpy/exceptions.pyi +27 -0
  282. numpy/f2py/__init__.py +86 -0
  283. numpy/f2py/__init__.pyi +6 -0
  284. numpy/f2py/__main__.py +5 -0
  285. numpy/f2py/__version__.py +1 -0
  286. numpy/f2py/__version__.pyi +1 -0
  287. numpy/f2py/_backends/__init__.py +9 -0
  288. numpy/f2py/_backends/__init__.pyi +5 -0
  289. numpy/f2py/_backends/_backend.py +44 -0
  290. numpy/f2py/_backends/_backend.pyi +46 -0
  291. numpy/f2py/_backends/_distutils.py +76 -0
  292. numpy/f2py/_backends/_distutils.pyi +13 -0
  293. numpy/f2py/_backends/_meson.py +231 -0
  294. numpy/f2py/_backends/_meson.pyi +63 -0
  295. numpy/f2py/_backends/meson.build.template +55 -0
  296. numpy/f2py/_isocbind.py +62 -0
  297. numpy/f2py/_isocbind.pyi +13 -0
  298. numpy/f2py/_src_pyf.py +247 -0
  299. numpy/f2py/_src_pyf.pyi +29 -0
  300. numpy/f2py/auxfuncs.py +1004 -0
  301. numpy/f2py/auxfuncs.pyi +264 -0
  302. numpy/f2py/capi_maps.py +811 -0
  303. numpy/f2py/capi_maps.pyi +33 -0
  304. numpy/f2py/cb_rules.py +665 -0
  305. numpy/f2py/cb_rules.pyi +17 -0
  306. numpy/f2py/cfuncs.py +1563 -0
  307. numpy/f2py/cfuncs.pyi +31 -0
  308. numpy/f2py/common_rules.py +143 -0
  309. numpy/f2py/common_rules.pyi +9 -0
  310. numpy/f2py/crackfortran.py +3725 -0
  311. numpy/f2py/crackfortran.pyi +258 -0
  312. numpy/f2py/diagnose.py +149 -0
  313. numpy/f2py/diagnose.pyi +1 -0
  314. numpy/f2py/f2py2e.py +786 -0
  315. numpy/f2py/f2py2e.pyi +76 -0
  316. numpy/f2py/f90mod_rules.py +269 -0
  317. numpy/f2py/f90mod_rules.pyi +16 -0
  318. numpy/f2py/func2subr.py +329 -0
  319. numpy/f2py/func2subr.pyi +7 -0
  320. numpy/f2py/rules.py +1629 -0
  321. numpy/f2py/rules.pyi +43 -0
  322. numpy/f2py/setup.cfg +3 -0
  323. numpy/f2py/src/fortranobject.c +1436 -0
  324. numpy/f2py/src/fortranobject.h +173 -0
  325. numpy/f2py/symbolic.py +1516 -0
  326. numpy/f2py/symbolic.pyi +221 -0
  327. numpy/f2py/tests/__init__.py +16 -0
  328. numpy/f2py/tests/src/abstract_interface/foo.f90 +34 -0
  329. numpy/f2py/tests/src/abstract_interface/gh18403_mod.f90 +6 -0
  330. numpy/f2py/tests/src/array_from_pyobj/wrapmodule.c +235 -0
  331. numpy/f2py/tests/src/assumed_shape/.f2py_f2cmap +1 -0
  332. numpy/f2py/tests/src/assumed_shape/foo_free.f90 +34 -0
  333. numpy/f2py/tests/src/assumed_shape/foo_mod.f90 +41 -0
  334. numpy/f2py/tests/src/assumed_shape/foo_use.f90 +19 -0
  335. numpy/f2py/tests/src/assumed_shape/precision.f90 +4 -0
  336. numpy/f2py/tests/src/block_docstring/foo.f +6 -0
  337. numpy/f2py/tests/src/callback/foo.f +62 -0
  338. numpy/f2py/tests/src/callback/gh17797.f90 +7 -0
  339. numpy/f2py/tests/src/callback/gh18335.f90 +17 -0
  340. numpy/f2py/tests/src/callback/gh25211.f +10 -0
  341. numpy/f2py/tests/src/callback/gh25211.pyf +18 -0
  342. numpy/f2py/tests/src/callback/gh26681.f90 +18 -0
  343. numpy/f2py/tests/src/cli/gh_22819.pyf +6 -0
  344. numpy/f2py/tests/src/cli/hi77.f +3 -0
  345. numpy/f2py/tests/src/cli/hiworld.f90 +3 -0
  346. numpy/f2py/tests/src/common/block.f +11 -0
  347. numpy/f2py/tests/src/common/gh19161.f90 +10 -0
  348. numpy/f2py/tests/src/crackfortran/accesstype.f90 +13 -0
  349. numpy/f2py/tests/src/crackfortran/common_with_division.f +17 -0
  350. numpy/f2py/tests/src/crackfortran/data_common.f +8 -0
  351. numpy/f2py/tests/src/crackfortran/data_multiplier.f +5 -0
  352. numpy/f2py/tests/src/crackfortran/data_stmts.f90 +20 -0
  353. numpy/f2py/tests/src/crackfortran/data_with_comments.f +8 -0
  354. numpy/f2py/tests/src/crackfortran/foo_deps.f90 +6 -0
  355. numpy/f2py/tests/src/crackfortran/gh15035.f +16 -0
  356. numpy/f2py/tests/src/crackfortran/gh17859.f +12 -0
  357. numpy/f2py/tests/src/crackfortran/gh22648.pyf +7 -0
  358. numpy/f2py/tests/src/crackfortran/gh23533.f +5 -0
  359. numpy/f2py/tests/src/crackfortran/gh23598.f90 +4 -0
  360. numpy/f2py/tests/src/crackfortran/gh23598Warn.f90 +11 -0
  361. numpy/f2py/tests/src/crackfortran/gh23879.f90 +20 -0
  362. numpy/f2py/tests/src/crackfortran/gh27697.f90 +12 -0
  363. numpy/f2py/tests/src/crackfortran/gh2848.f90 +13 -0
  364. numpy/f2py/tests/src/crackfortran/operators.f90 +49 -0
  365. numpy/f2py/tests/src/crackfortran/privatemod.f90 +11 -0
  366. numpy/f2py/tests/src/crackfortran/publicmod.f90 +10 -0
  367. numpy/f2py/tests/src/crackfortran/pubprivmod.f90 +10 -0
  368. numpy/f2py/tests/src/crackfortran/unicode_comment.f90 +4 -0
  369. numpy/f2py/tests/src/f2cmap/.f2py_f2cmap +1 -0
  370. numpy/f2py/tests/src/f2cmap/isoFortranEnvMap.f90 +9 -0
  371. numpy/f2py/tests/src/isocintrin/isoCtests.f90 +34 -0
  372. numpy/f2py/tests/src/kind/foo.f90 +20 -0
  373. numpy/f2py/tests/src/mixed/foo.f +5 -0
  374. numpy/f2py/tests/src/mixed/foo_fixed.f90 +8 -0
  375. numpy/f2py/tests/src/mixed/foo_free.f90 +8 -0
  376. numpy/f2py/tests/src/modules/gh25337/data.f90 +8 -0
  377. numpy/f2py/tests/src/modules/gh25337/use_data.f90 +6 -0
  378. numpy/f2py/tests/src/modules/gh26920/two_mods_with_no_public_entities.f90 +21 -0
  379. numpy/f2py/tests/src/modules/gh26920/two_mods_with_one_public_routine.f90 +21 -0
  380. numpy/f2py/tests/src/modules/module_data_docstring.f90 +12 -0
  381. numpy/f2py/tests/src/modules/use_modules.f90 +20 -0
  382. numpy/f2py/tests/src/negative_bounds/issue_20853.f90 +7 -0
  383. numpy/f2py/tests/src/parameter/constant_array.f90 +45 -0
  384. numpy/f2py/tests/src/parameter/constant_both.f90 +57 -0
  385. numpy/f2py/tests/src/parameter/constant_compound.f90 +15 -0
  386. numpy/f2py/tests/src/parameter/constant_integer.f90 +22 -0
  387. numpy/f2py/tests/src/parameter/constant_non_compound.f90 +23 -0
  388. numpy/f2py/tests/src/parameter/constant_real.f90 +23 -0
  389. numpy/f2py/tests/src/quoted_character/foo.f +14 -0
  390. numpy/f2py/tests/src/regression/AB.inc +1 -0
  391. numpy/f2py/tests/src/regression/assignOnlyModule.f90 +25 -0
  392. numpy/f2py/tests/src/regression/datonly.f90 +17 -0
  393. numpy/f2py/tests/src/regression/f77comments.f +26 -0
  394. numpy/f2py/tests/src/regression/f77fixedform.f95 +5 -0
  395. numpy/f2py/tests/src/regression/f90continuation.f90 +9 -0
  396. numpy/f2py/tests/src/regression/incfile.f90 +5 -0
  397. numpy/f2py/tests/src/regression/inout.f90 +9 -0
  398. numpy/f2py/tests/src/regression/lower_f2py_fortran.f90 +5 -0
  399. numpy/f2py/tests/src/regression/mod_derived_types.f90 +23 -0
  400. numpy/f2py/tests/src/return_character/foo77.f +45 -0
  401. numpy/f2py/tests/src/return_character/foo90.f90 +48 -0
  402. numpy/f2py/tests/src/return_complex/foo77.f +45 -0
  403. numpy/f2py/tests/src/return_complex/foo90.f90 +48 -0
  404. numpy/f2py/tests/src/return_integer/foo77.f +56 -0
  405. numpy/f2py/tests/src/return_integer/foo90.f90 +59 -0
  406. numpy/f2py/tests/src/return_logical/foo77.f +56 -0
  407. numpy/f2py/tests/src/return_logical/foo90.f90 +59 -0
  408. numpy/f2py/tests/src/return_real/foo77.f +45 -0
  409. numpy/f2py/tests/src/return_real/foo90.f90 +48 -0
  410. numpy/f2py/tests/src/routines/funcfortranname.f +5 -0
  411. numpy/f2py/tests/src/routines/funcfortranname.pyf +11 -0
  412. numpy/f2py/tests/src/routines/subrout.f +4 -0
  413. numpy/f2py/tests/src/routines/subrout.pyf +10 -0
  414. numpy/f2py/tests/src/size/foo.f90 +44 -0
  415. numpy/f2py/tests/src/string/char.f90 +29 -0
  416. numpy/f2py/tests/src/string/fixed_string.f90 +34 -0
  417. numpy/f2py/tests/src/string/gh24008.f +8 -0
  418. numpy/f2py/tests/src/string/gh24662.f90 +7 -0
  419. numpy/f2py/tests/src/string/gh25286.f90 +14 -0
  420. numpy/f2py/tests/src/string/gh25286.pyf +12 -0
  421. numpy/f2py/tests/src/string/gh25286_bc.pyf +12 -0
  422. numpy/f2py/tests/src/string/scalar_string.f90 +9 -0
  423. numpy/f2py/tests/src/string/string.f +12 -0
  424. numpy/f2py/tests/src/value_attrspec/gh21665.f90 +9 -0
  425. numpy/f2py/tests/test_abstract_interface.py +26 -0
  426. numpy/f2py/tests/test_array_from_pyobj.py +678 -0
  427. numpy/f2py/tests/test_assumed_shape.py +50 -0
  428. numpy/f2py/tests/test_block_docstring.py +20 -0
  429. numpy/f2py/tests/test_callback.py +263 -0
  430. numpy/f2py/tests/test_character.py +641 -0
  431. numpy/f2py/tests/test_common.py +23 -0
  432. numpy/f2py/tests/test_crackfortran.py +421 -0
  433. numpy/f2py/tests/test_data.py +71 -0
  434. numpy/f2py/tests/test_docs.py +64 -0
  435. numpy/f2py/tests/test_f2cmap.py +17 -0
  436. numpy/f2py/tests/test_f2py2e.py +964 -0
  437. numpy/f2py/tests/test_isoc.py +56 -0
  438. numpy/f2py/tests/test_kind.py +53 -0
  439. numpy/f2py/tests/test_mixed.py +35 -0
  440. numpy/f2py/tests/test_modules.py +83 -0
  441. numpy/f2py/tests/test_parameter.py +129 -0
  442. numpy/f2py/tests/test_pyf_src.py +43 -0
  443. numpy/f2py/tests/test_quoted_character.py +18 -0
  444. numpy/f2py/tests/test_regression.py +187 -0
  445. numpy/f2py/tests/test_return_character.py +48 -0
  446. numpy/f2py/tests/test_return_complex.py +67 -0
  447. numpy/f2py/tests/test_return_integer.py +55 -0
  448. numpy/f2py/tests/test_return_logical.py +65 -0
  449. numpy/f2py/tests/test_return_real.py +109 -0
  450. numpy/f2py/tests/test_routines.py +29 -0
  451. numpy/f2py/tests/test_semicolon_split.py +75 -0
  452. numpy/f2py/tests/test_size.py +45 -0
  453. numpy/f2py/tests/test_string.py +100 -0
  454. numpy/f2py/tests/test_symbolic.py +495 -0
  455. numpy/f2py/tests/test_value_attrspec.py +15 -0
  456. numpy/f2py/tests/util.py +442 -0
  457. numpy/f2py/use_rules.py +99 -0
  458. numpy/f2py/use_rules.pyi +9 -0
  459. numpy/fft/__init__.py +215 -0
  460. numpy/fft/__init__.pyi +43 -0
  461. numpy/fft/_helper.py +235 -0
  462. numpy/fft/_helper.pyi +45 -0
  463. numpy/fft/_pocketfft.py +1693 -0
  464. numpy/fft/_pocketfft.pyi +138 -0
  465. numpy/fft/_pocketfft_umath.cpython-313-darwin.so +0 -0
  466. numpy/fft/helper.py +17 -0
  467. numpy/fft/helper.pyi +22 -0
  468. numpy/fft/tests/__init__.py +0 -0
  469. numpy/fft/tests/test_helper.py +167 -0
  470. numpy/fft/tests/test_pocketfft.py +589 -0
  471. numpy/lib/__init__.py +97 -0
  472. numpy/lib/__init__.pyi +44 -0
  473. numpy/lib/_array_utils_impl.py +62 -0
  474. numpy/lib/_array_utils_impl.pyi +26 -0
  475. numpy/lib/_arraypad_impl.py +890 -0
  476. numpy/lib/_arraypad_impl.pyi +89 -0
  477. numpy/lib/_arraysetops_impl.py +1260 -0
  478. numpy/lib/_arraysetops_impl.pyi +468 -0
  479. numpy/lib/_arrayterator_impl.py +224 -0
  480. numpy/lib/_arrayterator_impl.pyi +46 -0
  481. numpy/lib/_datasource.py +700 -0
  482. numpy/lib/_datasource.pyi +31 -0
  483. numpy/lib/_format_impl.py +1036 -0
  484. numpy/lib/_format_impl.pyi +26 -0
  485. numpy/lib/_function_base_impl.py +5844 -0
  486. numpy/lib/_function_base_impl.pyi +1164 -0
  487. numpy/lib/_histograms_impl.py +1085 -0
  488. numpy/lib/_histograms_impl.pyi +50 -0
  489. numpy/lib/_index_tricks_impl.py +1067 -0
  490. numpy/lib/_index_tricks_impl.pyi +208 -0
  491. numpy/lib/_iotools.py +900 -0
  492. numpy/lib/_iotools.pyi +114 -0
  493. numpy/lib/_nanfunctions_impl.py +2024 -0
  494. numpy/lib/_nanfunctions_impl.pyi +52 -0
  495. numpy/lib/_npyio_impl.py +2596 -0
  496. numpy/lib/_npyio_impl.pyi +301 -0
  497. numpy/lib/_polynomial_impl.py +1465 -0
  498. numpy/lib/_polynomial_impl.pyi +318 -0
  499. numpy/lib/_scimath_impl.py +642 -0
  500. numpy/lib/_scimath_impl.pyi +93 -0
  501. numpy/lib/_shape_base_impl.py +1301 -0
  502. numpy/lib/_shape_base_impl.pyi +235 -0
  503. numpy/lib/_stride_tricks_impl.py +549 -0
  504. numpy/lib/_stride_tricks_impl.pyi +74 -0
  505. numpy/lib/_twodim_base_impl.py +1201 -0
  506. numpy/lib/_twodim_base_impl.pyi +438 -0
  507. numpy/lib/_type_check_impl.py +699 -0
  508. numpy/lib/_type_check_impl.pyi +350 -0
  509. numpy/lib/_ufunclike_impl.py +207 -0
  510. numpy/lib/_ufunclike_impl.pyi +67 -0
  511. numpy/lib/_user_array_impl.py +299 -0
  512. numpy/lib/_user_array_impl.pyi +225 -0
  513. numpy/lib/_utils_impl.py +784 -0
  514. numpy/lib/_utils_impl.pyi +10 -0
  515. numpy/lib/_version.py +154 -0
  516. numpy/lib/_version.pyi +17 -0
  517. numpy/lib/array_utils.py +7 -0
  518. numpy/lib/array_utils.pyi +12 -0
  519. numpy/lib/format.py +24 -0
  520. numpy/lib/format.pyi +66 -0
  521. numpy/lib/introspect.py +95 -0
  522. numpy/lib/introspect.pyi +3 -0
  523. numpy/lib/mixins.py +180 -0
  524. numpy/lib/mixins.pyi +77 -0
  525. numpy/lib/npyio.py +1 -0
  526. numpy/lib/npyio.pyi +9 -0
  527. numpy/lib/recfunctions.py +1681 -0
  528. numpy/lib/recfunctions.pyi +435 -0
  529. numpy/lib/scimath.py +13 -0
  530. numpy/lib/scimath.pyi +30 -0
  531. numpy/lib/stride_tricks.py +1 -0
  532. numpy/lib/stride_tricks.pyi +6 -0
  533. numpy/lib/tests/__init__.py +0 -0
  534. numpy/lib/tests/data/py2-np0-objarr.npy +0 -0
  535. numpy/lib/tests/data/py2-objarr.npy +0 -0
  536. numpy/lib/tests/data/py2-objarr.npz +0 -0
  537. numpy/lib/tests/data/py3-objarr.npy +0 -0
  538. numpy/lib/tests/data/py3-objarr.npz +0 -0
  539. numpy/lib/tests/data/python3.npy +0 -0
  540. numpy/lib/tests/data/win64python2.npy +0 -0
  541. numpy/lib/tests/test__datasource.py +352 -0
  542. numpy/lib/tests/test__iotools.py +360 -0
  543. numpy/lib/tests/test__version.py +64 -0
  544. numpy/lib/tests/test_array_utils.py +32 -0
  545. numpy/lib/tests/test_arraypad.py +1415 -0
  546. numpy/lib/tests/test_arraysetops.py +1074 -0
  547. numpy/lib/tests/test_arrayterator.py +46 -0
  548. numpy/lib/tests/test_format.py +1054 -0
  549. numpy/lib/tests/test_function_base.py +4573 -0
  550. numpy/lib/tests/test_histograms.py +855 -0
  551. numpy/lib/tests/test_index_tricks.py +573 -0
  552. numpy/lib/tests/test_io.py +2848 -0
  553. numpy/lib/tests/test_loadtxt.py +1101 -0
  554. numpy/lib/tests/test_mixins.py +215 -0
  555. numpy/lib/tests/test_nanfunctions.py +1438 -0
  556. numpy/lib/tests/test_packbits.py +376 -0
  557. numpy/lib/tests/test_polynomial.py +320 -0
  558. numpy/lib/tests/test_recfunctions.py +1052 -0
  559. numpy/lib/tests/test_regression.py +231 -0
  560. numpy/lib/tests/test_shape_base.py +813 -0
  561. numpy/lib/tests/test_stride_tricks.py +656 -0
  562. numpy/lib/tests/test_twodim_base.py +559 -0
  563. numpy/lib/tests/test_type_check.py +473 -0
  564. numpy/lib/tests/test_ufunclike.py +97 -0
  565. numpy/lib/tests/test_utils.py +80 -0
  566. numpy/lib/user_array.py +1 -0
  567. numpy/lib/user_array.pyi +1 -0
  568. numpy/linalg/__init__.py +98 -0
  569. numpy/linalg/__init__.pyi +73 -0
  570. numpy/linalg/_linalg.py +3682 -0
  571. numpy/linalg/_linalg.pyi +475 -0
  572. numpy/linalg/_umath_linalg.cpython-313-darwin.so +0 -0
  573. numpy/linalg/_umath_linalg.pyi +61 -0
  574. numpy/linalg/lapack_lite.cpython-313-darwin.so +0 -0
  575. numpy/linalg/lapack_lite.pyi +141 -0
  576. numpy/linalg/linalg.py +17 -0
  577. numpy/linalg/linalg.pyi +69 -0
  578. numpy/linalg/tests/__init__.py +0 -0
  579. numpy/linalg/tests/test_deprecations.py +20 -0
  580. numpy/linalg/tests/test_linalg.py +2443 -0
  581. numpy/linalg/tests/test_regression.py +181 -0
  582. numpy/ma/API_CHANGES.txt +135 -0
  583. numpy/ma/LICENSE +24 -0
  584. numpy/ma/README.rst +236 -0
  585. numpy/ma/__init__.py +53 -0
  586. numpy/ma/__init__.pyi +458 -0
  587. numpy/ma/core.py +8933 -0
  588. numpy/ma/core.pyi +1462 -0
  589. numpy/ma/extras.py +2344 -0
  590. numpy/ma/extras.pyi +138 -0
  591. numpy/ma/mrecords.py +773 -0
  592. numpy/ma/mrecords.pyi +96 -0
  593. numpy/ma/tests/__init__.py +0 -0
  594. numpy/ma/tests/test_arrayobject.py +40 -0
  595. numpy/ma/tests/test_core.py +5886 -0
  596. numpy/ma/tests/test_deprecations.py +87 -0
  597. numpy/ma/tests/test_extras.py +1998 -0
  598. numpy/ma/tests/test_mrecords.py +497 -0
  599. numpy/ma/tests/test_old_ma.py +942 -0
  600. numpy/ma/tests/test_regression.py +100 -0
  601. numpy/ma/tests/test_subclassing.py +469 -0
  602. numpy/ma/testutils.py +294 -0
  603. numpy/matlib.py +380 -0
  604. numpy/matlib.pyi +582 -0
  605. numpy/matrixlib/__init__.py +12 -0
  606. numpy/matrixlib/__init__.pyi +5 -0
  607. numpy/matrixlib/defmatrix.py +1119 -0
  608. numpy/matrixlib/defmatrix.pyi +17 -0
  609. numpy/matrixlib/tests/__init__.py +0 -0
  610. numpy/matrixlib/tests/test_defmatrix.py +455 -0
  611. numpy/matrixlib/tests/test_interaction.py +360 -0
  612. numpy/matrixlib/tests/test_masked_matrix.py +240 -0
  613. numpy/matrixlib/tests/test_matrix_linalg.py +105 -0
  614. numpy/matrixlib/tests/test_multiarray.py +17 -0
  615. numpy/matrixlib/tests/test_numeric.py +18 -0
  616. numpy/matrixlib/tests/test_regression.py +31 -0
  617. numpy/polynomial/__init__.py +187 -0
  618. numpy/polynomial/__init__.pyi +25 -0
  619. numpy/polynomial/_polybase.py +1191 -0
  620. numpy/polynomial/_polybase.pyi +285 -0
  621. numpy/polynomial/_polytypes.pyi +892 -0
  622. numpy/polynomial/chebyshev.py +2003 -0
  623. numpy/polynomial/chebyshev.pyi +181 -0
  624. numpy/polynomial/hermite.py +1740 -0
  625. numpy/polynomial/hermite.pyi +107 -0
  626. numpy/polynomial/hermite_e.py +1642 -0
  627. numpy/polynomial/hermite_e.pyi +107 -0
  628. numpy/polynomial/laguerre.py +1675 -0
  629. numpy/polynomial/laguerre.pyi +100 -0
  630. numpy/polynomial/legendre.py +1605 -0
  631. numpy/polynomial/legendre.pyi +100 -0
  632. numpy/polynomial/polynomial.py +1616 -0
  633. numpy/polynomial/polynomial.pyi +89 -0
  634. numpy/polynomial/polyutils.py +759 -0
  635. numpy/polynomial/polyutils.pyi +423 -0
  636. numpy/polynomial/tests/__init__.py +0 -0
  637. numpy/polynomial/tests/test_chebyshev.py +623 -0
  638. numpy/polynomial/tests/test_classes.py +618 -0
  639. numpy/polynomial/tests/test_hermite.py +558 -0
  640. numpy/polynomial/tests/test_hermite_e.py +559 -0
  641. numpy/polynomial/tests/test_laguerre.py +540 -0
  642. numpy/polynomial/tests/test_legendre.py +571 -0
  643. numpy/polynomial/tests/test_polynomial.py +669 -0
  644. numpy/polynomial/tests/test_polyutils.py +128 -0
  645. numpy/polynomial/tests/test_printing.py +555 -0
  646. numpy/polynomial/tests/test_symbol.py +217 -0
  647. numpy/py.typed +0 -0
  648. numpy/random/LICENSE.md +71 -0
  649. numpy/random/__init__.pxd +14 -0
  650. numpy/random/__init__.py +213 -0
  651. numpy/random/__init__.pyi +124 -0
  652. numpy/random/_bounded_integers.cpython-313-darwin.so +0 -0
  653. numpy/random/_bounded_integers.pxd +29 -0
  654. numpy/random/_bounded_integers.pyi +1 -0
  655. numpy/random/_common.cpython-313-darwin.so +0 -0
  656. numpy/random/_common.pxd +107 -0
  657. numpy/random/_common.pyi +16 -0
  658. numpy/random/_examples/cffi/extending.py +44 -0
  659. numpy/random/_examples/cffi/parse.py +53 -0
  660. numpy/random/_examples/cython/extending.pyx +77 -0
  661. numpy/random/_examples/cython/extending_distributions.pyx +118 -0
  662. numpy/random/_examples/cython/meson.build +53 -0
  663. numpy/random/_examples/numba/extending.py +86 -0
  664. numpy/random/_examples/numba/extending_distributions.py +67 -0
  665. numpy/random/_generator.cpython-313-darwin.so +0 -0
  666. numpy/random/_generator.pyi +861 -0
  667. numpy/random/_mt19937.cpython-313-darwin.so +0 -0
  668. numpy/random/_mt19937.pyi +25 -0
  669. numpy/random/_pcg64.cpython-313-darwin.so +0 -0
  670. numpy/random/_pcg64.pyi +44 -0
  671. numpy/random/_philox.cpython-313-darwin.so +0 -0
  672. numpy/random/_philox.pyi +39 -0
  673. numpy/random/_pickle.py +88 -0
  674. numpy/random/_pickle.pyi +43 -0
  675. numpy/random/_sfc64.cpython-313-darwin.so +0 -0
  676. numpy/random/_sfc64.pyi +28 -0
  677. numpy/random/bit_generator.cpython-313-darwin.so +0 -0
  678. numpy/random/bit_generator.pxd +35 -0
  679. numpy/random/bit_generator.pyi +124 -0
  680. numpy/random/c_distributions.pxd +119 -0
  681. numpy/random/lib/libnpyrandom.a +0 -0
  682. numpy/random/mtrand.cpython-313-darwin.so +0 -0
  683. numpy/random/mtrand.pyi +703 -0
  684. numpy/random/tests/__init__.py +0 -0
  685. numpy/random/tests/data/__init__.py +0 -0
  686. numpy/random/tests/data/generator_pcg64_np121.pkl.gz +0 -0
  687. numpy/random/tests/data/generator_pcg64_np126.pkl.gz +0 -0
  688. numpy/random/tests/data/mt19937-testset-1.csv +1001 -0
  689. numpy/random/tests/data/mt19937-testset-2.csv +1001 -0
  690. numpy/random/tests/data/pcg64-testset-1.csv +1001 -0
  691. numpy/random/tests/data/pcg64-testset-2.csv +1001 -0
  692. numpy/random/tests/data/pcg64dxsm-testset-1.csv +1001 -0
  693. numpy/random/tests/data/pcg64dxsm-testset-2.csv +1001 -0
  694. numpy/random/tests/data/philox-testset-1.csv +1001 -0
  695. numpy/random/tests/data/philox-testset-2.csv +1001 -0
  696. numpy/random/tests/data/sfc64-testset-1.csv +1001 -0
  697. numpy/random/tests/data/sfc64-testset-2.csv +1001 -0
  698. numpy/random/tests/data/sfc64_np126.pkl.gz +0 -0
  699. numpy/random/tests/test_direct.py +592 -0
  700. numpy/random/tests/test_extending.py +127 -0
  701. numpy/random/tests/test_generator_mt19937.py +2809 -0
  702. numpy/random/tests/test_generator_mt19937_regressions.py +207 -0
  703. numpy/random/tests/test_random.py +1757 -0
  704. numpy/random/tests/test_randomstate.py +2130 -0
  705. numpy/random/tests/test_randomstate_regression.py +217 -0
  706. numpy/random/tests/test_regression.py +152 -0
  707. numpy/random/tests/test_seed_sequence.py +79 -0
  708. numpy/random/tests/test_smoke.py +819 -0
  709. numpy/rec/__init__.py +2 -0
  710. numpy/rec/__init__.pyi +23 -0
  711. numpy/strings/__init__.py +2 -0
  712. numpy/strings/__init__.pyi +97 -0
  713. numpy/testing/__init__.py +22 -0
  714. numpy/testing/__init__.pyi +102 -0
  715. numpy/testing/_private/__init__.py +0 -0
  716. numpy/testing/_private/__init__.pyi +0 -0
  717. numpy/testing/_private/extbuild.py +250 -0
  718. numpy/testing/_private/extbuild.pyi +25 -0
  719. numpy/testing/_private/utils.py +2752 -0
  720. numpy/testing/_private/utils.pyi +499 -0
  721. numpy/testing/overrides.py +84 -0
  722. numpy/testing/overrides.pyi +11 -0
  723. numpy/testing/print_coercion_tables.py +207 -0
  724. numpy/testing/print_coercion_tables.pyi +27 -0
  725. numpy/testing/tests/__init__.py +0 -0
  726. numpy/testing/tests/test_utils.py +1917 -0
  727. numpy/tests/__init__.py +0 -0
  728. numpy/tests/test__all__.py +10 -0
  729. numpy/tests/test_configtool.py +48 -0
  730. numpy/tests/test_ctypeslib.py +377 -0
  731. numpy/tests/test_lazyloading.py +38 -0
  732. numpy/tests/test_matlib.py +59 -0
  733. numpy/tests/test_numpy_config.py +46 -0
  734. numpy/tests/test_numpy_version.py +54 -0
  735. numpy/tests/test_public_api.py +806 -0
  736. numpy/tests/test_reloading.py +74 -0
  737. numpy/tests/test_scripts.py +49 -0
  738. numpy/tests/test_warnings.py +78 -0
  739. numpy/typing/__init__.py +201 -0
  740. numpy/typing/mypy_plugin.py +195 -0
  741. numpy/typing/tests/__init__.py +0 -0
  742. numpy/typing/tests/data/fail/arithmetic.pyi +126 -0
  743. numpy/typing/tests/data/fail/array_constructors.pyi +34 -0
  744. numpy/typing/tests/data/fail/array_like.pyi +15 -0
  745. numpy/typing/tests/data/fail/array_pad.pyi +6 -0
  746. numpy/typing/tests/data/fail/arrayprint.pyi +16 -0
  747. numpy/typing/tests/data/fail/arrayterator.pyi +14 -0
  748. numpy/typing/tests/data/fail/bitwise_ops.pyi +17 -0
  749. numpy/typing/tests/data/fail/char.pyi +65 -0
  750. numpy/typing/tests/data/fail/chararray.pyi +62 -0
  751. numpy/typing/tests/data/fail/comparisons.pyi +27 -0
  752. numpy/typing/tests/data/fail/constants.pyi +3 -0
  753. numpy/typing/tests/data/fail/datasource.pyi +15 -0
  754. numpy/typing/tests/data/fail/dtype.pyi +17 -0
  755. numpy/typing/tests/data/fail/einsumfunc.pyi +12 -0
  756. numpy/typing/tests/data/fail/flatiter.pyi +20 -0
  757. numpy/typing/tests/data/fail/fromnumeric.pyi +148 -0
  758. numpy/typing/tests/data/fail/histograms.pyi +12 -0
  759. numpy/typing/tests/data/fail/index_tricks.pyi +14 -0
  760. numpy/typing/tests/data/fail/lib_function_base.pyi +62 -0
  761. numpy/typing/tests/data/fail/lib_polynomial.pyi +29 -0
  762. numpy/typing/tests/data/fail/lib_utils.pyi +3 -0
  763. numpy/typing/tests/data/fail/lib_version.pyi +6 -0
  764. numpy/typing/tests/data/fail/linalg.pyi +48 -0
  765. numpy/typing/tests/data/fail/ma.pyi +143 -0
  766. numpy/typing/tests/data/fail/memmap.pyi +5 -0
  767. numpy/typing/tests/data/fail/modules.pyi +17 -0
  768. numpy/typing/tests/data/fail/multiarray.pyi +52 -0
  769. numpy/typing/tests/data/fail/ndarray.pyi +11 -0
  770. numpy/typing/tests/data/fail/ndarray_misc.pyi +36 -0
  771. numpy/typing/tests/data/fail/nditer.pyi +8 -0
  772. numpy/typing/tests/data/fail/nested_sequence.pyi +16 -0
  773. numpy/typing/tests/data/fail/npyio.pyi +24 -0
  774. numpy/typing/tests/data/fail/numerictypes.pyi +5 -0
  775. numpy/typing/tests/data/fail/random.pyi +62 -0
  776. numpy/typing/tests/data/fail/rec.pyi +17 -0
  777. numpy/typing/tests/data/fail/scalars.pyi +87 -0
  778. numpy/typing/tests/data/fail/shape.pyi +6 -0
  779. numpy/typing/tests/data/fail/shape_base.pyi +8 -0
  780. numpy/typing/tests/data/fail/stride_tricks.pyi +9 -0
  781. numpy/typing/tests/data/fail/strings.pyi +52 -0
  782. numpy/typing/tests/data/fail/testing.pyi +28 -0
  783. numpy/typing/tests/data/fail/twodim_base.pyi +32 -0
  784. numpy/typing/tests/data/fail/type_check.pyi +13 -0
  785. numpy/typing/tests/data/fail/ufunc_config.pyi +21 -0
  786. numpy/typing/tests/data/fail/ufunclike.pyi +21 -0
  787. numpy/typing/tests/data/fail/ufuncs.pyi +17 -0
  788. numpy/typing/tests/data/fail/warnings_and_errors.pyi +5 -0
  789. numpy/typing/tests/data/misc/extended_precision.pyi +9 -0
  790. numpy/typing/tests/data/mypy.ini +9 -0
  791. numpy/typing/tests/data/pass/arithmetic.py +612 -0
  792. numpy/typing/tests/data/pass/array_constructors.py +137 -0
  793. numpy/typing/tests/data/pass/array_like.py +43 -0
  794. numpy/typing/tests/data/pass/arrayprint.py +37 -0
  795. numpy/typing/tests/data/pass/arrayterator.py +27 -0
  796. numpy/typing/tests/data/pass/bitwise_ops.py +131 -0
  797. numpy/typing/tests/data/pass/comparisons.py +315 -0
  798. numpy/typing/tests/data/pass/dtype.py +57 -0
  799. numpy/typing/tests/data/pass/einsumfunc.py +36 -0
  800. numpy/typing/tests/data/pass/flatiter.py +19 -0
  801. numpy/typing/tests/data/pass/fromnumeric.py +272 -0
  802. numpy/typing/tests/data/pass/index_tricks.py +60 -0
  803. numpy/typing/tests/data/pass/lib_user_array.py +22 -0
  804. numpy/typing/tests/data/pass/lib_utils.py +19 -0
  805. numpy/typing/tests/data/pass/lib_version.py +18 -0
  806. numpy/typing/tests/data/pass/literal.py +51 -0
  807. numpy/typing/tests/data/pass/ma.py +174 -0
  808. numpy/typing/tests/data/pass/mod.py +149 -0
  809. numpy/typing/tests/data/pass/modules.py +45 -0
  810. numpy/typing/tests/data/pass/multiarray.py +76 -0
  811. numpy/typing/tests/data/pass/ndarray_conversion.py +87 -0
  812. numpy/typing/tests/data/pass/ndarray_misc.py +203 -0
  813. numpy/typing/tests/data/pass/ndarray_shape_manipulation.py +47 -0
  814. numpy/typing/tests/data/pass/nditer.py +4 -0
  815. numpy/typing/tests/data/pass/numeric.py +95 -0
  816. numpy/typing/tests/data/pass/numerictypes.py +17 -0
  817. numpy/typing/tests/data/pass/random.py +1497 -0
  818. numpy/typing/tests/data/pass/recfunctions.py +161 -0
  819. numpy/typing/tests/data/pass/scalars.py +248 -0
  820. numpy/typing/tests/data/pass/shape.py +19 -0
  821. numpy/typing/tests/data/pass/simple.py +168 -0
  822. numpy/typing/tests/data/pass/simple_py3.py +6 -0
  823. numpy/typing/tests/data/pass/ufunc_config.py +64 -0
  824. numpy/typing/tests/data/pass/ufunclike.py +47 -0
  825. numpy/typing/tests/data/pass/ufuncs.py +16 -0
  826. numpy/typing/tests/data/pass/warnings_and_errors.py +6 -0
  827. numpy/typing/tests/data/reveal/arithmetic.pyi +720 -0
  828. numpy/typing/tests/data/reveal/array_api_info.pyi +70 -0
  829. numpy/typing/tests/data/reveal/array_constructors.pyi +249 -0
  830. numpy/typing/tests/data/reveal/arraypad.pyi +22 -0
  831. numpy/typing/tests/data/reveal/arrayprint.pyi +25 -0
  832. numpy/typing/tests/data/reveal/arraysetops.pyi +74 -0
  833. numpy/typing/tests/data/reveal/arrayterator.pyi +27 -0
  834. numpy/typing/tests/data/reveal/bitwise_ops.pyi +167 -0
  835. numpy/typing/tests/data/reveal/char.pyi +224 -0
  836. numpy/typing/tests/data/reveal/chararray.pyi +137 -0
  837. numpy/typing/tests/data/reveal/comparisons.pyi +264 -0
  838. numpy/typing/tests/data/reveal/constants.pyi +14 -0
  839. numpy/typing/tests/data/reveal/ctypeslib.pyi +81 -0
  840. numpy/typing/tests/data/reveal/datasource.pyi +23 -0
  841. numpy/typing/tests/data/reveal/dtype.pyi +136 -0
  842. numpy/typing/tests/data/reveal/einsumfunc.pyi +39 -0
  843. numpy/typing/tests/data/reveal/emath.pyi +54 -0
  844. numpy/typing/tests/data/reveal/fft.pyi +37 -0
  845. numpy/typing/tests/data/reveal/flatiter.pyi +47 -0
  846. numpy/typing/tests/data/reveal/fromnumeric.pyi +347 -0
  847. numpy/typing/tests/data/reveal/getlimits.pyi +51 -0
  848. numpy/typing/tests/data/reveal/histograms.pyi +25 -0
  849. numpy/typing/tests/data/reveal/index_tricks.pyi +70 -0
  850. numpy/typing/tests/data/reveal/lib_function_base.pyi +213 -0
  851. numpy/typing/tests/data/reveal/lib_polynomial.pyi +144 -0
  852. numpy/typing/tests/data/reveal/lib_utils.pyi +17 -0
  853. numpy/typing/tests/data/reveal/lib_version.pyi +20 -0
  854. numpy/typing/tests/data/reveal/linalg.pyi +132 -0
  855. numpy/typing/tests/data/reveal/ma.pyi +369 -0
  856. numpy/typing/tests/data/reveal/matrix.pyi +73 -0
  857. numpy/typing/tests/data/reveal/memmap.pyi +19 -0
  858. numpy/typing/tests/data/reveal/mod.pyi +179 -0
  859. numpy/typing/tests/data/reveal/modules.pyi +51 -0
  860. numpy/typing/tests/data/reveal/multiarray.pyi +194 -0
  861. numpy/typing/tests/data/reveal/nbit_base_example.pyi +21 -0
  862. numpy/typing/tests/data/reveal/ndarray_assignability.pyi +77 -0
  863. numpy/typing/tests/data/reveal/ndarray_conversion.pyi +85 -0
  864. numpy/typing/tests/data/reveal/ndarray_misc.pyi +247 -0
  865. numpy/typing/tests/data/reveal/ndarray_shape_manipulation.pyi +39 -0
  866. numpy/typing/tests/data/reveal/nditer.pyi +49 -0
  867. numpy/typing/tests/data/reveal/nested_sequence.pyi +25 -0
  868. numpy/typing/tests/data/reveal/npyio.pyi +83 -0
  869. numpy/typing/tests/data/reveal/numeric.pyi +134 -0
  870. numpy/typing/tests/data/reveal/numerictypes.pyi +16 -0
  871. numpy/typing/tests/data/reveal/polynomial_polybase.pyi +220 -0
  872. numpy/typing/tests/data/reveal/polynomial_polyutils.pyi +219 -0
  873. numpy/typing/tests/data/reveal/polynomial_series.pyi +138 -0
  874. numpy/typing/tests/data/reveal/random.pyi +1546 -0
  875. numpy/typing/tests/data/reveal/rec.pyi +171 -0
  876. numpy/typing/tests/data/reveal/scalars.pyi +191 -0
  877. numpy/typing/tests/data/reveal/shape.pyi +13 -0
  878. numpy/typing/tests/data/reveal/shape_base.pyi +52 -0
  879. numpy/typing/tests/data/reveal/stride_tricks.pyi +27 -0
  880. numpy/typing/tests/data/reveal/strings.pyi +196 -0
  881. numpy/typing/tests/data/reveal/testing.pyi +198 -0
  882. numpy/typing/tests/data/reveal/twodim_base.pyi +145 -0
  883. numpy/typing/tests/data/reveal/type_check.pyi +67 -0
  884. numpy/typing/tests/data/reveal/ufunc_config.pyi +30 -0
  885. numpy/typing/tests/data/reveal/ufunclike.pyi +31 -0
  886. numpy/typing/tests/data/reveal/ufuncs.pyi +123 -0
  887. numpy/typing/tests/data/reveal/warnings_and_errors.pyi +11 -0
  888. numpy/typing/tests/test_isfile.py +32 -0
  889. numpy/typing/tests/test_runtime.py +102 -0
  890. numpy/typing/tests/test_typing.py +205 -0
  891. numpy/version.py +11 -0
  892. numpy/version.pyi +18 -0
  893. numpy-2.3.5.dist-info/LICENSE.txt +971 -0
  894. numpy-2.3.5.dist-info/METADATA +1093 -0
  895. numpy-2.3.5.dist-info/RECORD +897 -0
  896. numpy-2.3.5.dist-info/WHEEL +6 -0
  897. numpy-2.3.5.dist-info/entry_points.txt +13 -0
@@ -0,0 +1,2809 @@
1
+ import hashlib
2
+ import os.path
3
+ import sys
4
+
5
+ import pytest
6
+
7
+ import numpy as np
8
+ from numpy.exceptions import AxisError
9
+ from numpy.linalg import LinAlgError
10
+ from numpy.random import MT19937, Generator, RandomState, SeedSequence
11
+ from numpy.testing import (
12
+ IS_WASM,
13
+ assert_,
14
+ assert_allclose,
15
+ assert_array_almost_equal,
16
+ assert_array_equal,
17
+ assert_equal,
18
+ assert_no_warnings,
19
+ assert_raises,
20
+ assert_warns,
21
+ suppress_warnings,
22
+ )
23
+
24
+ random = Generator(MT19937())
25
+
26
+ JUMP_TEST_DATA = [
27
+ {
28
+ "seed": 0,
29
+ "steps": 10,
30
+ "initial": {"key_sha256": "bb1636883c2707b51c5b7fc26c6927af4430f2e0785a8c7bc886337f919f9edf", "pos": 9}, # noqa: E501
31
+ "jumped": {"key_sha256": "ff682ac12bb140f2d72fba8d3506cf4e46817a0db27aae1683867629031d8d55", "pos": 598}, # noqa: E501
32
+ },
33
+ {
34
+ "seed": 384908324,
35
+ "steps": 312,
36
+ "initial": {"key_sha256": "16b791a1e04886ccbbb4d448d6ff791267dc458ae599475d08d5cced29d11614", "pos": 311}, # noqa: E501
37
+ "jumped": {"key_sha256": "a0110a2cf23b56be0feaed8f787a7fc84bef0cb5623003d75b26bdfa1c18002c", "pos": 276}, # noqa: E501
38
+ },
39
+ {
40
+ "seed": [839438204, 980239840, 859048019, 821],
41
+ "steps": 511,
42
+ "initial": {"key_sha256": "d306cf01314d51bd37892d874308200951a35265ede54d200f1e065004c3e9ea", "pos": 510}, # noqa: E501
43
+ "jumped": {"key_sha256": "0e00ab449f01a5195a83b4aee0dfbc2ce8d46466a640b92e33977d2e42f777f8", "pos": 475}, # noqa: E501
44
+ },
45
+ ]
46
+
47
+
48
+ @pytest.fixture(scope='module', params=[True, False])
49
+ def endpoint(request):
50
+ return request.param
51
+
52
+
53
+ class TestSeed:
54
+ def test_scalar(self):
55
+ s = Generator(MT19937(0))
56
+ assert_equal(s.integers(1000), 479)
57
+ s = Generator(MT19937(4294967295))
58
+ assert_equal(s.integers(1000), 324)
59
+
60
+ def test_array(self):
61
+ s = Generator(MT19937(range(10)))
62
+ assert_equal(s.integers(1000), 465)
63
+ s = Generator(MT19937(np.arange(10)))
64
+ assert_equal(s.integers(1000), 465)
65
+ s = Generator(MT19937([0]))
66
+ assert_equal(s.integers(1000), 479)
67
+ s = Generator(MT19937([4294967295]))
68
+ assert_equal(s.integers(1000), 324)
69
+
70
+ def test_seedsequence(self):
71
+ s = MT19937(SeedSequence(0))
72
+ assert_equal(s.random_raw(1), 2058676884)
73
+
74
+ def test_invalid_scalar(self):
75
+ # seed must be an unsigned 32 bit integer
76
+ assert_raises(TypeError, MT19937, -0.5)
77
+ assert_raises(ValueError, MT19937, -1)
78
+
79
+ def test_invalid_array(self):
80
+ # seed must be an unsigned integer
81
+ assert_raises(TypeError, MT19937, [-0.5])
82
+ assert_raises(ValueError, MT19937, [-1])
83
+ assert_raises(ValueError, MT19937, [1, -2, 4294967296])
84
+
85
+ def test_noninstantized_bitgen(self):
86
+ assert_raises(ValueError, Generator, MT19937)
87
+
88
+
89
+ class TestBinomial:
90
+ def test_n_zero(self):
91
+ # Tests the corner case of n == 0 for the binomial distribution.
92
+ # binomial(0, p) should be zero for any p in [0, 1].
93
+ # This test addresses issue #3480.
94
+ zeros = np.zeros(2, dtype='int')
95
+ for p in [0, .5, 1]:
96
+ assert_(random.binomial(0, p) == 0)
97
+ assert_array_equal(random.binomial(zeros, p), zeros)
98
+
99
+ def test_p_is_nan(self):
100
+ # Issue #4571.
101
+ assert_raises(ValueError, random.binomial, 1, np.nan)
102
+
103
+
104
+ class TestMultinomial:
105
+ def test_basic(self):
106
+ random.multinomial(100, [0.2, 0.8])
107
+
108
+ def test_zero_probability(self):
109
+ random.multinomial(100, [0.2, 0.8, 0.0, 0.0, 0.0])
110
+
111
+ def test_int_negative_interval(self):
112
+ assert_(-5 <= random.integers(-5, -1) < -1)
113
+ x = random.integers(-5, -1, 5)
114
+ assert_(np.all(-5 <= x))
115
+ assert_(np.all(x < -1))
116
+
117
+ def test_size(self):
118
+ # gh-3173
119
+ p = [0.5, 0.5]
120
+ assert_equal(random.multinomial(1, p, np.uint32(1)).shape, (1, 2))
121
+ assert_equal(random.multinomial(1, p, np.uint32(1)).shape, (1, 2))
122
+ assert_equal(random.multinomial(1, p, np.uint32(1)).shape, (1, 2))
123
+ assert_equal(random.multinomial(1, p, [2, 2]).shape, (2, 2, 2))
124
+ assert_equal(random.multinomial(1, p, (2, 2)).shape, (2, 2, 2))
125
+ assert_equal(random.multinomial(1, p, np.array((2, 2))).shape,
126
+ (2, 2, 2))
127
+
128
+ assert_raises(TypeError, random.multinomial, 1, p,
129
+ float(1))
130
+
131
+ def test_invalid_prob(self):
132
+ assert_raises(ValueError, random.multinomial, 100, [1.1, 0.2])
133
+ assert_raises(ValueError, random.multinomial, 100, [-.1, 0.9])
134
+
135
+ def test_invalid_n(self):
136
+ assert_raises(ValueError, random.multinomial, -1, [0.8, 0.2])
137
+ assert_raises(ValueError, random.multinomial, [-1] * 10, [0.8, 0.2])
138
+
139
+ def test_p_non_contiguous(self):
140
+ p = np.arange(15.)
141
+ p /= np.sum(p[1::3])
142
+ pvals = p[1::3]
143
+ random = Generator(MT19937(1432985819))
144
+ non_contig = random.multinomial(100, pvals=pvals)
145
+ random = Generator(MT19937(1432985819))
146
+ contig = random.multinomial(100, pvals=np.ascontiguousarray(pvals))
147
+ assert_array_equal(non_contig, contig)
148
+
149
+ def test_multinomial_pvals_float32(self):
150
+ x = np.array([9.9e-01, 9.9e-01, 1.0e-09, 1.0e-09, 1.0e-09, 1.0e-09,
151
+ 1.0e-09, 1.0e-09, 1.0e-09, 1.0e-09], dtype=np.float32)
152
+ pvals = x / x.sum()
153
+ random = Generator(MT19937(1432985819))
154
+ match = r"[\w\s]*pvals array is cast to 64-bit floating"
155
+ with pytest.raises(ValueError, match=match):
156
+ random.multinomial(1, pvals)
157
+
158
+
159
+ class TestMultivariateHypergeometric:
160
+
161
+ def setup_method(self):
162
+ self.seed = 8675309
163
+
164
+ def test_argument_validation(self):
165
+ # Error cases...
166
+
167
+ # `colors` must be a 1-d sequence
168
+ assert_raises(ValueError, random.multivariate_hypergeometric,
169
+ 10, 4)
170
+
171
+ # Negative nsample
172
+ assert_raises(ValueError, random.multivariate_hypergeometric,
173
+ [2, 3, 4], -1)
174
+
175
+ # Negative color
176
+ assert_raises(ValueError, random.multivariate_hypergeometric,
177
+ [-1, 2, 3], 2)
178
+
179
+ # nsample exceeds sum(colors)
180
+ assert_raises(ValueError, random.multivariate_hypergeometric,
181
+ [2, 3, 4], 10)
182
+
183
+ # nsample exceeds sum(colors) (edge case of empty colors)
184
+ assert_raises(ValueError, random.multivariate_hypergeometric,
185
+ [], 1)
186
+
187
+ # Validation errors associated with very large values in colors.
188
+ assert_raises(ValueError, random.multivariate_hypergeometric,
189
+ [999999999, 101], 5, 1, 'marginals')
190
+
191
+ int64_info = np.iinfo(np.int64)
192
+ max_int64 = int64_info.max
193
+ max_int64_index = max_int64 // int64_info.dtype.itemsize
194
+ assert_raises(ValueError, random.multivariate_hypergeometric,
195
+ [max_int64_index - 100, 101], 5, 1, 'count')
196
+
197
+ @pytest.mark.parametrize('method', ['count', 'marginals'])
198
+ def test_edge_cases(self, method):
199
+ # Set the seed, but in fact, all the results in this test are
200
+ # deterministic, so we don't really need this.
201
+ random = Generator(MT19937(self.seed))
202
+
203
+ x = random.multivariate_hypergeometric([0, 0, 0], 0, method=method)
204
+ assert_array_equal(x, [0, 0, 0])
205
+
206
+ x = random.multivariate_hypergeometric([], 0, method=method)
207
+ assert_array_equal(x, [])
208
+
209
+ x = random.multivariate_hypergeometric([], 0, size=1, method=method)
210
+ assert_array_equal(x, np.empty((1, 0), dtype=np.int64))
211
+
212
+ x = random.multivariate_hypergeometric([1, 2, 3], 0, method=method)
213
+ assert_array_equal(x, [0, 0, 0])
214
+
215
+ x = random.multivariate_hypergeometric([9, 0, 0], 3, method=method)
216
+ assert_array_equal(x, [3, 0, 0])
217
+
218
+ colors = [1, 1, 0, 1, 1]
219
+ x = random.multivariate_hypergeometric(colors, sum(colors),
220
+ method=method)
221
+ assert_array_equal(x, colors)
222
+
223
+ x = random.multivariate_hypergeometric([3, 4, 5], 12, size=3,
224
+ method=method)
225
+ assert_array_equal(x, [[3, 4, 5]] * 3)
226
+
227
+ # Cases for nsample:
228
+ # nsample < 10
229
+ # 10 <= nsample < colors.sum()/2
230
+ # colors.sum()/2 < nsample < colors.sum() - 10
231
+ # colors.sum() - 10 < nsample < colors.sum()
232
+ @pytest.mark.parametrize('nsample', [8, 25, 45, 55])
233
+ @pytest.mark.parametrize('method', ['count', 'marginals'])
234
+ @pytest.mark.parametrize('size', [5, (2, 3), 150000])
235
+ def test_typical_cases(self, nsample, method, size):
236
+ random = Generator(MT19937(self.seed))
237
+
238
+ colors = np.array([10, 5, 20, 25])
239
+ sample = random.multivariate_hypergeometric(colors, nsample, size,
240
+ method=method)
241
+ if isinstance(size, int):
242
+ expected_shape = (size,) + colors.shape
243
+ else:
244
+ expected_shape = size + colors.shape
245
+ assert_equal(sample.shape, expected_shape)
246
+ assert_((sample >= 0).all())
247
+ assert_((sample <= colors).all())
248
+ assert_array_equal(sample.sum(axis=-1),
249
+ np.full(size, fill_value=nsample, dtype=int))
250
+ if isinstance(size, int) and size >= 100000:
251
+ # This sample is large enough to compare its mean to
252
+ # the expected values.
253
+ assert_allclose(sample.mean(axis=0),
254
+ nsample * colors / colors.sum(),
255
+ rtol=1e-3, atol=0.005)
256
+
257
+ def test_repeatability1(self):
258
+ random = Generator(MT19937(self.seed))
259
+ sample = random.multivariate_hypergeometric([3, 4, 5], 5, size=5,
260
+ method='count')
261
+ expected = np.array([[2, 1, 2],
262
+ [2, 1, 2],
263
+ [1, 1, 3],
264
+ [2, 0, 3],
265
+ [2, 1, 2]])
266
+ assert_array_equal(sample, expected)
267
+
268
+ def test_repeatability2(self):
269
+ random = Generator(MT19937(self.seed))
270
+ sample = random.multivariate_hypergeometric([20, 30, 50], 50,
271
+ size=5,
272
+ method='marginals')
273
+ expected = np.array([[ 9, 17, 24],
274
+ [ 7, 13, 30],
275
+ [ 9, 15, 26],
276
+ [ 9, 17, 24],
277
+ [12, 14, 24]])
278
+ assert_array_equal(sample, expected)
279
+
280
+ def test_repeatability3(self):
281
+ random = Generator(MT19937(self.seed))
282
+ sample = random.multivariate_hypergeometric([20, 30, 50], 12,
283
+ size=5,
284
+ method='marginals')
285
+ expected = np.array([[2, 3, 7],
286
+ [5, 3, 4],
287
+ [2, 5, 5],
288
+ [5, 3, 4],
289
+ [1, 5, 6]])
290
+ assert_array_equal(sample, expected)
291
+
292
+
293
+ class TestSetState:
294
+ def setup_method(self):
295
+ self.seed = 1234567890
296
+ self.rg = Generator(MT19937(self.seed))
297
+ self.bit_generator = self.rg.bit_generator
298
+ self.state = self.bit_generator.state
299
+ self.legacy_state = (self.state['bit_generator'],
300
+ self.state['state']['key'],
301
+ self.state['state']['pos'])
302
+
303
+ def test_gaussian_reset(self):
304
+ # Make sure the cached every-other-Gaussian is reset.
305
+ old = self.rg.standard_normal(size=3)
306
+ self.bit_generator.state = self.state
307
+ new = self.rg.standard_normal(size=3)
308
+ assert_(np.all(old == new))
309
+
310
+ def test_gaussian_reset_in_media_res(self):
311
+ # When the state is saved with a cached Gaussian, make sure the
312
+ # cached Gaussian is restored.
313
+
314
+ self.rg.standard_normal()
315
+ state = self.bit_generator.state
316
+ old = self.rg.standard_normal(size=3)
317
+ self.bit_generator.state = state
318
+ new = self.rg.standard_normal(size=3)
319
+ assert_(np.all(old == new))
320
+
321
+ def test_negative_binomial(self):
322
+ # Ensure that the negative binomial results take floating point
323
+ # arguments without truncation.
324
+ self.rg.negative_binomial(0.5, 0.5)
325
+
326
+
327
+ class TestIntegers:
328
+ rfunc = random.integers
329
+
330
+ # valid integer/boolean types
331
+ itype = [bool, np.int8, np.uint8, np.int16, np.uint16,
332
+ np.int32, np.uint32, np.int64, np.uint64]
333
+
334
+ def test_unsupported_type(self, endpoint):
335
+ assert_raises(TypeError, self.rfunc, 1, endpoint=endpoint, dtype=float)
336
+
337
+ def test_bounds_checking(self, endpoint):
338
+ for dt in self.itype:
339
+ lbnd = 0 if dt is bool else np.iinfo(dt).min
340
+ ubnd = 2 if dt is bool else np.iinfo(dt).max + 1
341
+ ubnd = ubnd - 1 if endpoint else ubnd
342
+ assert_raises(ValueError, self.rfunc, lbnd - 1, ubnd,
343
+ endpoint=endpoint, dtype=dt)
344
+ assert_raises(ValueError, self.rfunc, lbnd, ubnd + 1,
345
+ endpoint=endpoint, dtype=dt)
346
+ assert_raises(ValueError, self.rfunc, ubnd, lbnd,
347
+ endpoint=endpoint, dtype=dt)
348
+ assert_raises(ValueError, self.rfunc, 1, 0, endpoint=endpoint,
349
+ dtype=dt)
350
+
351
+ assert_raises(ValueError, self.rfunc, [lbnd - 1], ubnd,
352
+ endpoint=endpoint, dtype=dt)
353
+ assert_raises(ValueError, self.rfunc, [lbnd], [ubnd + 1],
354
+ endpoint=endpoint, dtype=dt)
355
+ assert_raises(ValueError, self.rfunc, [ubnd], [lbnd],
356
+ endpoint=endpoint, dtype=dt)
357
+ assert_raises(ValueError, self.rfunc, 1, [0],
358
+ endpoint=endpoint, dtype=dt)
359
+ assert_raises(ValueError, self.rfunc, [ubnd + 1], [ubnd],
360
+ endpoint=endpoint, dtype=dt)
361
+
362
+ def test_bounds_checking_array(self, endpoint):
363
+ for dt in self.itype:
364
+ lbnd = 0 if dt is bool else np.iinfo(dt).min
365
+ ubnd = 2 if dt is bool else np.iinfo(dt).max + (not endpoint)
366
+
367
+ assert_raises(ValueError, self.rfunc, [lbnd - 1] * 2, [ubnd] * 2,
368
+ endpoint=endpoint, dtype=dt)
369
+ assert_raises(ValueError, self.rfunc, [lbnd] * 2,
370
+ [ubnd + 1] * 2, endpoint=endpoint, dtype=dt)
371
+ assert_raises(ValueError, self.rfunc, ubnd, [lbnd] * 2,
372
+ endpoint=endpoint, dtype=dt)
373
+ assert_raises(ValueError, self.rfunc, [1] * 2, 0,
374
+ endpoint=endpoint, dtype=dt)
375
+
376
+ def test_rng_zero_and_extremes(self, endpoint):
377
+ for dt in self.itype:
378
+ lbnd = 0 if dt is bool else np.iinfo(dt).min
379
+ ubnd = 2 if dt is bool else np.iinfo(dt).max + 1
380
+ ubnd = ubnd - 1 if endpoint else ubnd
381
+ is_open = not endpoint
382
+
383
+ tgt = ubnd - 1
384
+ assert_equal(self.rfunc(tgt, tgt + is_open, size=1000,
385
+ endpoint=endpoint, dtype=dt), tgt)
386
+ assert_equal(self.rfunc([tgt], tgt + is_open, size=1000,
387
+ endpoint=endpoint, dtype=dt), tgt)
388
+
389
+ tgt = lbnd
390
+ assert_equal(self.rfunc(tgt, tgt + is_open, size=1000,
391
+ endpoint=endpoint, dtype=dt), tgt)
392
+ assert_equal(self.rfunc(tgt, [tgt + is_open], size=1000,
393
+ endpoint=endpoint, dtype=dt), tgt)
394
+
395
+ tgt = (lbnd + ubnd) // 2
396
+ assert_equal(self.rfunc(tgt, tgt + is_open, size=1000,
397
+ endpoint=endpoint, dtype=dt), tgt)
398
+ assert_equal(self.rfunc([tgt], [tgt + is_open],
399
+ size=1000, endpoint=endpoint, dtype=dt),
400
+ tgt)
401
+
402
+ def test_rng_zero_and_extremes_array(self, endpoint):
403
+ size = 1000
404
+ for dt in self.itype:
405
+ lbnd = 0 if dt is bool else np.iinfo(dt).min
406
+ ubnd = 2 if dt is bool else np.iinfo(dt).max + 1
407
+ ubnd = ubnd - 1 if endpoint else ubnd
408
+
409
+ tgt = ubnd - 1
410
+ assert_equal(self.rfunc([tgt], [tgt + 1],
411
+ size=size, dtype=dt), tgt)
412
+ assert_equal(self.rfunc(
413
+ [tgt] * size, [tgt + 1] * size, dtype=dt), tgt)
414
+ assert_equal(self.rfunc(
415
+ [tgt] * size, [tgt + 1] * size, size=size, dtype=dt), tgt)
416
+
417
+ tgt = lbnd
418
+ assert_equal(self.rfunc([tgt], [tgt + 1],
419
+ size=size, dtype=dt), tgt)
420
+ assert_equal(self.rfunc(
421
+ [tgt] * size, [tgt + 1] * size, dtype=dt), tgt)
422
+ assert_equal(self.rfunc(
423
+ [tgt] * size, [tgt + 1] * size, size=size, dtype=dt), tgt)
424
+
425
+ tgt = (lbnd + ubnd) // 2
426
+ assert_equal(self.rfunc([tgt], [tgt + 1],
427
+ size=size, dtype=dt), tgt)
428
+ assert_equal(self.rfunc(
429
+ [tgt] * size, [tgt + 1] * size, dtype=dt), tgt)
430
+ assert_equal(self.rfunc(
431
+ [tgt] * size, [tgt + 1] * size, size=size, dtype=dt), tgt)
432
+
433
+ def test_full_range(self, endpoint):
434
+ # Test for ticket #1690
435
+
436
+ for dt in self.itype:
437
+ lbnd = 0 if dt is bool else np.iinfo(dt).min
438
+ ubnd = 2 if dt is bool else np.iinfo(dt).max + 1
439
+ ubnd = ubnd - 1 if endpoint else ubnd
440
+
441
+ try:
442
+ self.rfunc(lbnd, ubnd, endpoint=endpoint, dtype=dt)
443
+ except Exception as e:
444
+ raise AssertionError("No error should have been raised, "
445
+ "but one was with the following "
446
+ "message:\n\n%s" % str(e))
447
+
448
+ def test_full_range_array(self, endpoint):
449
+ # Test for ticket #1690
450
+
451
+ for dt in self.itype:
452
+ lbnd = 0 if dt is bool else np.iinfo(dt).min
453
+ ubnd = 2 if dt is bool else np.iinfo(dt).max + 1
454
+ ubnd = ubnd - 1 if endpoint else ubnd
455
+
456
+ try:
457
+ self.rfunc([lbnd] * 2, [ubnd], endpoint=endpoint, dtype=dt)
458
+ except Exception as e:
459
+ raise AssertionError("No error should have been raised, "
460
+ "but one was with the following "
461
+ "message:\n\n%s" % str(e))
462
+
463
+ def test_in_bounds_fuzz(self, endpoint):
464
+ # Don't use fixed seed
465
+ random = Generator(MT19937())
466
+
467
+ for dt in self.itype[1:]:
468
+ for ubnd in [4, 8, 16]:
469
+ vals = self.rfunc(2, ubnd - endpoint, size=2 ** 16,
470
+ endpoint=endpoint, dtype=dt)
471
+ assert_(vals.max() < ubnd)
472
+ assert_(vals.min() >= 2)
473
+
474
+ vals = self.rfunc(0, 2 - endpoint, size=2 ** 16, endpoint=endpoint,
475
+ dtype=bool)
476
+ assert_(vals.max() < 2)
477
+ assert_(vals.min() >= 0)
478
+
479
+ def test_scalar_array_equiv(self, endpoint):
480
+ for dt in self.itype:
481
+ lbnd = 0 if dt is bool else np.iinfo(dt).min
482
+ ubnd = 2 if dt is bool else np.iinfo(dt).max + 1
483
+ ubnd = ubnd - 1 if endpoint else ubnd
484
+
485
+ size = 1000
486
+ random = Generator(MT19937(1234))
487
+ scalar = random.integers(lbnd, ubnd, size=size, endpoint=endpoint,
488
+ dtype=dt)
489
+
490
+ random = Generator(MT19937(1234))
491
+ scalar_array = random.integers([lbnd], [ubnd], size=size,
492
+ endpoint=endpoint, dtype=dt)
493
+
494
+ random = Generator(MT19937(1234))
495
+ array = random.integers([lbnd] * size, [ubnd] *
496
+ size, size=size, endpoint=endpoint, dtype=dt)
497
+ assert_array_equal(scalar, scalar_array)
498
+ assert_array_equal(scalar, array)
499
+
500
+ def test_repeatability(self, endpoint):
501
+ # We use a sha256 hash of generated sequences of 1000 samples
502
+ # in the range [0, 6) for all but bool, where the range
503
+ # is [0, 2). Hashes are for little endian numbers.
504
+ tgt = {'bool': '053594a9b82d656f967c54869bc6970aa0358cf94ad469c81478459c6a90eee3', # noqa: E501
505
+ 'int16': '54de9072b6ee9ff7f20b58329556a46a447a8a29d67db51201bf88baa6e4e5d4', # noqa: E501
506
+ 'int32': 'd3a0d5efb04542b25ac712e50d21f39ac30f312a5052e9bbb1ad3baa791ac84b', # noqa: E501
507
+ 'int64': '14e224389ac4580bfbdccb5697d6190b496f91227cf67df60989de3d546389b1', # noqa: E501
508
+ 'int8': '0e203226ff3fbbd1580f15da4621e5f7164d0d8d6b51696dd42d004ece2cbec1', # noqa: E501
509
+ 'uint16': '54de9072b6ee9ff7f20b58329556a46a447a8a29d67db51201bf88baa6e4e5d4', # noqa: E501
510
+ 'uint32': 'd3a0d5efb04542b25ac712e50d21f39ac30f312a5052e9bbb1ad3baa791ac84b', # noqa: E501
511
+ 'uint64': '14e224389ac4580bfbdccb5697d6190b496f91227cf67df60989de3d546389b1', # noqa: E501
512
+ 'uint8': '0e203226ff3fbbd1580f15da4621e5f7164d0d8d6b51696dd42d004ece2cbec1'} # noqa: E501
513
+
514
+ for dt in self.itype[1:]:
515
+ random = Generator(MT19937(1234))
516
+
517
+ # view as little endian for hash
518
+ if sys.byteorder == 'little':
519
+ val = random.integers(0, 6 - endpoint, size=1000, endpoint=endpoint,
520
+ dtype=dt)
521
+ else:
522
+ val = random.integers(0, 6 - endpoint, size=1000, endpoint=endpoint,
523
+ dtype=dt).byteswap()
524
+
525
+ res = hashlib.sha256(val).hexdigest()
526
+ assert_(tgt[np.dtype(dt).name] == res)
527
+
528
+ # bools do not depend on endianness
529
+ random = Generator(MT19937(1234))
530
+ val = random.integers(0, 2 - endpoint, size=1000, endpoint=endpoint,
531
+ dtype=bool).view(np.int8)
532
+ res = hashlib.sha256(val).hexdigest()
533
+ assert_(tgt[np.dtype(bool).name] == res)
534
+
535
+ def test_repeatability_broadcasting(self, endpoint):
536
+ for dt in self.itype:
537
+ lbnd = 0 if dt in (bool, np.bool) else np.iinfo(dt).min
538
+ ubnd = 2 if dt in (bool, np.bool) else np.iinfo(dt).max + 1
539
+ ubnd = ubnd - 1 if endpoint else ubnd
540
+
541
+ # view as little endian for hash
542
+ random = Generator(MT19937(1234))
543
+ val = random.integers(lbnd, ubnd, size=1000, endpoint=endpoint,
544
+ dtype=dt)
545
+
546
+ random = Generator(MT19937(1234))
547
+ val_bc = random.integers([lbnd] * 1000, ubnd, endpoint=endpoint,
548
+ dtype=dt)
549
+
550
+ assert_array_equal(val, val_bc)
551
+
552
+ random = Generator(MT19937(1234))
553
+ val_bc = random.integers([lbnd] * 1000, [ubnd] * 1000,
554
+ endpoint=endpoint, dtype=dt)
555
+
556
+ assert_array_equal(val, val_bc)
557
+
558
+ @pytest.mark.parametrize(
559
+ 'bound, expected',
560
+ [(2**32 - 1, np.array([517043486, 1364798665, 1733884389, 1353720612,
561
+ 3769704066, 1170797179, 4108474671])),
562
+ (2**32, np.array([517043487, 1364798666, 1733884390, 1353720613,
563
+ 3769704067, 1170797180, 4108474672])),
564
+ (2**32 + 1, np.array([517043487, 1733884390, 3769704068, 4108474673,
565
+ 1831631863, 1215661561, 3869512430]))]
566
+ )
567
+ def test_repeatability_32bit_boundary(self, bound, expected):
568
+ for size in [None, len(expected)]:
569
+ random = Generator(MT19937(1234))
570
+ x = random.integers(bound, size=size)
571
+ assert_equal(x, expected if size is not None else expected[0])
572
+
573
+ def test_repeatability_32bit_boundary_broadcasting(self):
574
+ desired = np.array([[[1622936284, 3620788691, 1659384060],
575
+ [1417365545, 760222891, 1909653332],
576
+ [3788118662, 660249498, 4092002593]],
577
+ [[3625610153, 2979601262, 3844162757],
578
+ [ 685800658, 120261497, 2694012896],
579
+ [1207779440, 1586594375, 3854335050]],
580
+ [[3004074748, 2310761796, 3012642217],
581
+ [2067714190, 2786677879, 1363865881],
582
+ [ 791663441, 1867303284, 2169727960]],
583
+ [[1939603804, 1250951100, 298950036],
584
+ [1040128489, 3791912209, 3317053765],
585
+ [3155528714, 61360675, 2305155588]],
586
+ [[ 817688762, 1335621943, 3288952434],
587
+ [1770890872, 1102951817, 1957607470],
588
+ [3099996017, 798043451, 48334215]]])
589
+ for size in [None, (5, 3, 3)]:
590
+ random = Generator(MT19937(12345))
591
+ x = random.integers([[-1], [0], [1]],
592
+ [2**32 - 1, 2**32, 2**32 + 1],
593
+ size=size)
594
+ assert_array_equal(x, desired if size is not None else desired[0])
595
+
596
+ def test_int64_uint64_broadcast_exceptions(self, endpoint):
597
+ configs = {np.uint64: ((0, 2**65), (-1, 2**62), (10, 9), (0, 0)),
598
+ np.int64: ((0, 2**64), (-(2**64), 2**62), (10, 9), (0, 0),
599
+ (-2**63 - 1, -2**63 - 1))}
600
+ for dtype in configs:
601
+ for config in configs[dtype]:
602
+ low, high = config
603
+ high = high - endpoint
604
+ low_a = np.array([[low] * 10])
605
+ high_a = np.array([high] * 10)
606
+ assert_raises(ValueError, random.integers, low, high,
607
+ endpoint=endpoint, dtype=dtype)
608
+ assert_raises(ValueError, random.integers, low_a, high,
609
+ endpoint=endpoint, dtype=dtype)
610
+ assert_raises(ValueError, random.integers, low, high_a,
611
+ endpoint=endpoint, dtype=dtype)
612
+ assert_raises(ValueError, random.integers, low_a, high_a,
613
+ endpoint=endpoint, dtype=dtype)
614
+
615
+ low_o = np.array([[low] * 10], dtype=object)
616
+ high_o = np.array([high] * 10, dtype=object)
617
+ assert_raises(ValueError, random.integers, low_o, high,
618
+ endpoint=endpoint, dtype=dtype)
619
+ assert_raises(ValueError, random.integers, low, high_o,
620
+ endpoint=endpoint, dtype=dtype)
621
+ assert_raises(ValueError, random.integers, low_o, high_o,
622
+ endpoint=endpoint, dtype=dtype)
623
+
624
+ def test_int64_uint64_corner_case(self, endpoint):
625
+ # When stored in Numpy arrays, `lbnd` is casted
626
+ # as np.int64, and `ubnd` is casted as np.uint64.
627
+ # Checking whether `lbnd` >= `ubnd` used to be
628
+ # done solely via direct comparison, which is incorrect
629
+ # because when Numpy tries to compare both numbers,
630
+ # it casts both to np.float64 because there is
631
+ # no integer superset of np.int64 and np.uint64. However,
632
+ # `ubnd` is too large to be represented in np.float64,
633
+ # causing it be round down to np.iinfo(np.int64).max,
634
+ # leading to a ValueError because `lbnd` now equals
635
+ # the new `ubnd`.
636
+
637
+ dt = np.int64
638
+ tgt = np.iinfo(np.int64).max
639
+ lbnd = np.int64(np.iinfo(np.int64).max)
640
+ ubnd = np.uint64(np.iinfo(np.int64).max + 1 - endpoint)
641
+
642
+ # None of these function calls should
643
+ # generate a ValueError now.
644
+ actual = random.integers(lbnd, ubnd, endpoint=endpoint, dtype=dt)
645
+ assert_equal(actual, tgt)
646
+
647
+ def test_respect_dtype_singleton(self, endpoint):
648
+ # See gh-7203
649
+ for dt in self.itype:
650
+ lbnd = 0 if dt is bool else np.iinfo(dt).min
651
+ ubnd = 2 if dt is bool else np.iinfo(dt).max + 1
652
+ ubnd = ubnd - 1 if endpoint else ubnd
653
+ dt = np.bool if dt is bool else dt
654
+
655
+ sample = self.rfunc(lbnd, ubnd, endpoint=endpoint, dtype=dt)
656
+ assert_equal(sample.dtype, dt)
657
+
658
+ for dt in (bool, int):
659
+ lbnd = 0 if dt is bool else np.iinfo(dt).min
660
+ ubnd = 2 if dt is bool else np.iinfo(dt).max + 1
661
+ ubnd = ubnd - 1 if endpoint else ubnd
662
+
663
+ # gh-7284: Ensure that we get Python data types
664
+ sample = self.rfunc(lbnd, ubnd, endpoint=endpoint, dtype=dt)
665
+ assert not hasattr(sample, 'dtype')
666
+ assert_equal(type(sample), dt)
667
+
668
+ def test_respect_dtype_array(self, endpoint):
669
+ # See gh-7203
670
+ for dt in self.itype:
671
+ lbnd = 0 if dt is bool else np.iinfo(dt).min
672
+ ubnd = 2 if dt is bool else np.iinfo(dt).max + 1
673
+ ubnd = ubnd - 1 if endpoint else ubnd
674
+ dt = np.bool if dt is bool else dt
675
+
676
+ sample = self.rfunc([lbnd], [ubnd], endpoint=endpoint, dtype=dt)
677
+ assert_equal(sample.dtype, dt)
678
+ sample = self.rfunc([lbnd] * 2, [ubnd] * 2, endpoint=endpoint,
679
+ dtype=dt)
680
+ assert_equal(sample.dtype, dt)
681
+
682
+ def test_zero_size(self, endpoint):
683
+ # See gh-7203
684
+ for dt in self.itype:
685
+ sample = self.rfunc(0, 0, (3, 0, 4), endpoint=endpoint, dtype=dt)
686
+ assert sample.shape == (3, 0, 4)
687
+ assert sample.dtype == dt
688
+ assert self.rfunc(0, -10, 0, endpoint=endpoint,
689
+ dtype=dt).shape == (0,)
690
+ assert_equal(random.integers(0, 0, size=(3, 0, 4)).shape,
691
+ (3, 0, 4))
692
+ assert_equal(random.integers(0, -10, size=0).shape, (0,))
693
+ assert_equal(random.integers(10, 10, size=0).shape, (0,))
694
+
695
+ def test_error_byteorder(self):
696
+ other_byteord_dt = '<i4' if sys.byteorder == 'big' else '>i4'
697
+ with pytest.raises(ValueError):
698
+ random.integers(0, 200, size=10, dtype=other_byteord_dt)
699
+
700
+ # chi2max is the maximum acceptable chi-squared value.
701
+ @pytest.mark.slow
702
+ @pytest.mark.parametrize('sample_size,high,dtype,chi2max',
703
+ [(5000000, 5, np.int8, 125.0), # p-value ~4.6e-25
704
+ (5000000, 7, np.uint8, 150.0), # p-value ~7.7e-30
705
+ (10000000, 2500, np.int16, 3300.0), # p-value ~3.0e-25
706
+ (50000000, 5000, np.uint16, 6500.0), # p-value ~3.5e-25
707
+ ])
708
+ def test_integers_small_dtype_chisquared(self, sample_size, high,
709
+ dtype, chi2max):
710
+ # Regression test for gh-14774.
711
+ samples = random.integers(high, size=sample_size, dtype=dtype)
712
+
713
+ values, counts = np.unique(samples, return_counts=True)
714
+ expected = sample_size / high
715
+ chi2 = ((counts - expected)**2 / expected).sum()
716
+ assert chi2 < chi2max
717
+
718
+
719
+ class TestRandomDist:
720
+ # Make sure the random distribution returns the correct value for a
721
+ # given seed
722
+
723
+ def setup_method(self):
724
+ self.seed = 1234567890
725
+
726
+ def test_integers(self):
727
+ random = Generator(MT19937(self.seed))
728
+ actual = random.integers(-99, 99, size=(3, 2))
729
+ desired = np.array([[-80, -56], [41, 37], [-83, -16]])
730
+ assert_array_equal(actual, desired)
731
+
732
+ def test_integers_masked(self):
733
+ # Test masked rejection sampling algorithm to generate array of
734
+ # uint32 in an interval.
735
+ random = Generator(MT19937(self.seed))
736
+ actual = random.integers(0, 99, size=(3, 2), dtype=np.uint32)
737
+ desired = np.array([[9, 21], [70, 68], [8, 41]], dtype=np.uint32)
738
+ assert_array_equal(actual, desired)
739
+
740
+ def test_integers_closed(self):
741
+ random = Generator(MT19937(self.seed))
742
+ actual = random.integers(-99, 99, size=(3, 2), endpoint=True)
743
+ desired = np.array([[-80, -56], [41, 38], [-83, -15]])
744
+ assert_array_equal(actual, desired)
745
+
746
+ def test_integers_max_int(self):
747
+ # Tests whether integers with closed=True can generate the
748
+ # maximum allowed Python int that can be converted
749
+ # into a C long. Previous implementations of this
750
+ # method have thrown an OverflowError when attempting
751
+ # to generate this integer.
752
+ actual = random.integers(np.iinfo('l').max, np.iinfo('l').max,
753
+ endpoint=True)
754
+
755
+ desired = np.iinfo('l').max
756
+ assert_equal(actual, desired)
757
+
758
+ def test_random(self):
759
+ random = Generator(MT19937(self.seed))
760
+ actual = random.random((3, 2))
761
+ desired = np.array([[0.096999199829214, 0.707517457682192],
762
+ [0.084364834598269, 0.767731206553125],
763
+ [0.665069021359413, 0.715487190596693]])
764
+ assert_array_almost_equal(actual, desired, decimal=15)
765
+
766
+ random = Generator(MT19937(self.seed))
767
+ actual = random.random()
768
+ assert_array_almost_equal(actual, desired[0, 0], decimal=15)
769
+
770
+ def test_random_float(self):
771
+ random = Generator(MT19937(self.seed))
772
+ actual = random.random((3, 2))
773
+ desired = np.array([[0.0969992 , 0.70751746], # noqa: E203
774
+ [0.08436483, 0.76773121],
775
+ [0.66506902, 0.71548719]])
776
+ assert_array_almost_equal(actual, desired, decimal=7)
777
+
778
+ def test_random_float_scalar(self):
779
+ random = Generator(MT19937(self.seed))
780
+ actual = random.random(dtype=np.float32)
781
+ desired = 0.0969992
782
+ assert_array_almost_equal(actual, desired, decimal=7)
783
+
784
+ @pytest.mark.parametrize('dtype, uint_view_type',
785
+ [(np.float32, np.uint32),
786
+ (np.float64, np.uint64)])
787
+ def test_random_distribution_of_lsb(self, dtype, uint_view_type):
788
+ random = Generator(MT19937(self.seed))
789
+ sample = random.random(100000, dtype=dtype)
790
+ num_ones_in_lsb = np.count_nonzero(sample.view(uint_view_type) & 1)
791
+ # The probability of a 1 in the least significant bit is 0.25.
792
+ # With a sample size of 100000, the probability that num_ones_in_lsb
793
+ # is outside the following range is less than 5e-11.
794
+ assert 24100 < num_ones_in_lsb < 25900
795
+
796
+ def test_random_unsupported_type(self):
797
+ assert_raises(TypeError, random.random, dtype='int32')
798
+
799
+ def test_choice_uniform_replace(self):
800
+ random = Generator(MT19937(self.seed))
801
+ actual = random.choice(4, 4)
802
+ desired = np.array([0, 0, 2, 2], dtype=np.int64)
803
+ assert_array_equal(actual, desired)
804
+
805
+ def test_choice_nonuniform_replace(self):
806
+ random = Generator(MT19937(self.seed))
807
+ actual = random.choice(4, 4, p=[0.4, 0.4, 0.1, 0.1])
808
+ desired = np.array([0, 1, 0, 1], dtype=np.int64)
809
+ assert_array_equal(actual, desired)
810
+
811
+ def test_choice_uniform_noreplace(self):
812
+ random = Generator(MT19937(self.seed))
813
+ actual = random.choice(4, 3, replace=False)
814
+ desired = np.array([2, 0, 3], dtype=np.int64)
815
+ assert_array_equal(actual, desired)
816
+ actual = random.choice(4, 4, replace=False, shuffle=False)
817
+ desired = np.arange(4, dtype=np.int64)
818
+ assert_array_equal(actual, desired)
819
+
820
+ def test_choice_nonuniform_noreplace(self):
821
+ random = Generator(MT19937(self.seed))
822
+ actual = random.choice(4, 3, replace=False, p=[0.1, 0.3, 0.5, 0.1])
823
+ desired = np.array([0, 2, 3], dtype=np.int64)
824
+ assert_array_equal(actual, desired)
825
+
826
+ def test_choice_noninteger(self):
827
+ random = Generator(MT19937(self.seed))
828
+ actual = random.choice(['a', 'b', 'c', 'd'], 4)
829
+ desired = np.array(['a', 'a', 'c', 'c'])
830
+ assert_array_equal(actual, desired)
831
+
832
+ def test_choice_multidimensional_default_axis(self):
833
+ random = Generator(MT19937(self.seed))
834
+ actual = random.choice([[0, 1], [2, 3], [4, 5], [6, 7]], 3)
835
+ desired = np.array([[0, 1], [0, 1], [4, 5]])
836
+ assert_array_equal(actual, desired)
837
+
838
+ def test_choice_multidimensional_custom_axis(self):
839
+ random = Generator(MT19937(self.seed))
840
+ actual = random.choice([[0, 1], [2, 3], [4, 5], [6, 7]], 1, axis=1)
841
+ desired = np.array([[0], [2], [4], [6]])
842
+ assert_array_equal(actual, desired)
843
+
844
+ def test_choice_exceptions(self):
845
+ sample = random.choice
846
+ assert_raises(ValueError, sample, -1, 3)
847
+ assert_raises(ValueError, sample, 3., 3)
848
+ assert_raises(ValueError, sample, [], 3)
849
+ assert_raises(ValueError, sample, [1, 2, 3, 4], 3,
850
+ p=[[0.25, 0.25], [0.25, 0.25]])
851
+ assert_raises(ValueError, sample, [1, 2], 3, p=[0.4, 0.4, 0.2])
852
+ assert_raises(ValueError, sample, [1, 2], 3, p=[1.1, -0.1])
853
+ assert_raises(ValueError, sample, [1, 2], 3, p=[0.4, 0.4])
854
+ assert_raises(ValueError, sample, [1, 2, 3], 4, replace=False)
855
+ # gh-13087
856
+ assert_raises(ValueError, sample, [1, 2, 3], -2, replace=False)
857
+ assert_raises(ValueError, sample, [1, 2, 3], (-1,), replace=False)
858
+ assert_raises(ValueError, sample, [1, 2, 3], (-1, 1), replace=False)
859
+ assert_raises(ValueError, sample, [1, 2, 3], 2,
860
+ replace=False, p=[1, 0, 0])
861
+
862
+ def test_choice_return_shape(self):
863
+ p = [0.1, 0.9]
864
+ # Check scalar
865
+ assert_(np.isscalar(random.choice(2, replace=True)))
866
+ assert_(np.isscalar(random.choice(2, replace=False)))
867
+ assert_(np.isscalar(random.choice(2, replace=True, p=p)))
868
+ assert_(np.isscalar(random.choice(2, replace=False, p=p)))
869
+ assert_(np.isscalar(random.choice([1, 2], replace=True)))
870
+ assert_(random.choice([None], replace=True) is None)
871
+ a = np.array([1, 2])
872
+ arr = np.empty(1, dtype=object)
873
+ arr[0] = a
874
+ assert_(random.choice(arr, replace=True) is a)
875
+
876
+ # Check 0-d array
877
+ s = ()
878
+ assert_(not np.isscalar(random.choice(2, s, replace=True)))
879
+ assert_(not np.isscalar(random.choice(2, s, replace=False)))
880
+ assert_(not np.isscalar(random.choice(2, s, replace=True, p=p)))
881
+ assert_(not np.isscalar(random.choice(2, s, replace=False, p=p)))
882
+ assert_(not np.isscalar(random.choice([1, 2], s, replace=True)))
883
+ assert_(random.choice([None], s, replace=True).ndim == 0)
884
+ a = np.array([1, 2])
885
+ arr = np.empty(1, dtype=object)
886
+ arr[0] = a
887
+ assert_(random.choice(arr, s, replace=True).item() is a)
888
+
889
+ # Check multi dimensional array
890
+ s = (2, 3)
891
+ p = [0.1, 0.1, 0.1, 0.1, 0.4, 0.2]
892
+ assert_equal(random.choice(6, s, replace=True).shape, s)
893
+ assert_equal(random.choice(6, s, replace=False).shape, s)
894
+ assert_equal(random.choice(6, s, replace=True, p=p).shape, s)
895
+ assert_equal(random.choice(6, s, replace=False, p=p).shape, s)
896
+ assert_equal(random.choice(np.arange(6), s, replace=True).shape, s)
897
+
898
+ # Check zero-size
899
+ assert_equal(random.integers(0, 0, size=(3, 0, 4)).shape, (3, 0, 4))
900
+ assert_equal(random.integers(0, -10, size=0).shape, (0,))
901
+ assert_equal(random.integers(10, 10, size=0).shape, (0,))
902
+ assert_equal(random.choice(0, size=0).shape, (0,))
903
+ assert_equal(random.choice([], size=(0,)).shape, (0,))
904
+ assert_equal(random.choice(['a', 'b'], size=(3, 0, 4)).shape,
905
+ (3, 0, 4))
906
+ assert_raises(ValueError, random.choice, [], 10)
907
+
908
+ def test_choice_nan_probabilities(self):
909
+ a = np.array([42, 1, 2])
910
+ p = [None, None, None]
911
+ assert_raises(ValueError, random.choice, a, p=p)
912
+
913
+ def test_choice_p_non_contiguous(self):
914
+ p = np.ones(10) / 5
915
+ p[1::2] = 3.0
916
+ random = Generator(MT19937(self.seed))
917
+ non_contig = random.choice(5, 3, p=p[::2])
918
+ random = Generator(MT19937(self.seed))
919
+ contig = random.choice(5, 3, p=np.ascontiguousarray(p[::2]))
920
+ assert_array_equal(non_contig, contig)
921
+
922
+ def test_choice_return_type(self):
923
+ # gh 9867
924
+ p = np.ones(4) / 4.
925
+ actual = random.choice(4, 2)
926
+ assert actual.dtype == np.int64
927
+ actual = random.choice(4, 2, replace=False)
928
+ assert actual.dtype == np.int64
929
+ actual = random.choice(4, 2, p=p)
930
+ assert actual.dtype == np.int64
931
+ actual = random.choice(4, 2, p=p, replace=False)
932
+ assert actual.dtype == np.int64
933
+
934
+ def test_choice_large_sample(self):
935
+ choice_hash = '4266599d12bfcfb815213303432341c06b4349f5455890446578877bb322e222'
936
+ random = Generator(MT19937(self.seed))
937
+ actual = random.choice(10000, 5000, replace=False)
938
+ if sys.byteorder != 'little':
939
+ actual = actual.byteswap()
940
+ res = hashlib.sha256(actual.view(np.int8)).hexdigest()
941
+ assert_(choice_hash == res)
942
+
943
+ def test_choice_array_size_empty_tuple(self):
944
+ random = Generator(MT19937(self.seed))
945
+ assert_array_equal(random.choice([1, 2, 3], size=()), np.array(1),
946
+ strict=True)
947
+ assert_array_equal(random.choice([[1, 2, 3]], size=()), [1, 2, 3])
948
+ assert_array_equal(random.choice([[1]], size=()), [1], strict=True)
949
+ assert_array_equal(random.choice([[1]], size=(), axis=1), [1],
950
+ strict=True)
951
+
952
+ def test_bytes(self):
953
+ random = Generator(MT19937(self.seed))
954
+ actual = random.bytes(10)
955
+ desired = b'\x86\xf0\xd4\x18\xe1\x81\t8%\xdd'
956
+ assert_equal(actual, desired)
957
+
958
+ def test_shuffle(self):
959
+ # Test lists, arrays (of various dtypes), and multidimensional versions
960
+ # of both, c-contiguous or not:
961
+ for conv in [lambda x: np.array([]),
962
+ lambda x: x,
963
+ lambda x: np.asarray(x).astype(np.int8),
964
+ lambda x: np.asarray(x).astype(np.float32),
965
+ lambda x: np.asarray(x).astype(np.complex64),
966
+ lambda x: np.asarray(x).astype(object),
967
+ lambda x: [(i, i) for i in x],
968
+ lambda x: np.asarray([[i, i] for i in x]),
969
+ lambda x: np.vstack([x, x]).T,
970
+ # gh-11442
971
+ lambda x: (np.asarray([(i, i) for i in x],
972
+ [("a", int), ("b", int)])
973
+ .view(np.recarray)),
974
+ # gh-4270
975
+ lambda x: np.asarray([(i, i) for i in x],
976
+ [("a", object, (1,)),
977
+ ("b", np.int32, (1,))])]:
978
+ random = Generator(MT19937(self.seed))
979
+ alist = conv([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])
980
+ random.shuffle(alist)
981
+ actual = alist
982
+ desired = conv([4, 1, 9, 8, 0, 5, 3, 6, 2, 7])
983
+ assert_array_equal(actual, desired)
984
+
985
+ def test_shuffle_custom_axis(self):
986
+ random = Generator(MT19937(self.seed))
987
+ actual = np.arange(16).reshape((4, 4))
988
+ random.shuffle(actual, axis=1)
989
+ desired = np.array([[ 0, 3, 1, 2],
990
+ [ 4, 7, 5, 6],
991
+ [ 8, 11, 9, 10],
992
+ [12, 15, 13, 14]])
993
+ assert_array_equal(actual, desired)
994
+ random = Generator(MT19937(self.seed))
995
+ actual = np.arange(16).reshape((4, 4))
996
+ random.shuffle(actual, axis=-1)
997
+ assert_array_equal(actual, desired)
998
+
999
+ def test_shuffle_custom_axis_empty(self):
1000
+ random = Generator(MT19937(self.seed))
1001
+ desired = np.array([]).reshape((0, 6))
1002
+ for axis in (0, 1):
1003
+ actual = np.array([]).reshape((0, 6))
1004
+ random.shuffle(actual, axis=axis)
1005
+ assert_array_equal(actual, desired)
1006
+
1007
+ def test_shuffle_axis_nonsquare(self):
1008
+ y1 = np.arange(20).reshape(2, 10)
1009
+ y2 = y1.copy()
1010
+ random = Generator(MT19937(self.seed))
1011
+ random.shuffle(y1, axis=1)
1012
+ random = Generator(MT19937(self.seed))
1013
+ random.shuffle(y2.T)
1014
+ assert_array_equal(y1, y2)
1015
+
1016
+ def test_shuffle_masked(self):
1017
+ # gh-3263
1018
+ a = np.ma.masked_values(np.reshape(range(20), (5, 4)) % 3 - 1, -1)
1019
+ b = np.ma.masked_values(np.arange(20) % 3 - 1, -1)
1020
+ a_orig = a.copy()
1021
+ b_orig = b.copy()
1022
+ for i in range(50):
1023
+ random.shuffle(a)
1024
+ assert_equal(
1025
+ sorted(a.data[~a.mask]), sorted(a_orig.data[~a_orig.mask]))
1026
+ random.shuffle(b)
1027
+ assert_equal(
1028
+ sorted(b.data[~b.mask]), sorted(b_orig.data[~b_orig.mask]))
1029
+
1030
+ def test_shuffle_exceptions(self):
1031
+ random = Generator(MT19937(self.seed))
1032
+ arr = np.arange(10)
1033
+ assert_raises(AxisError, random.shuffle, arr, 1)
1034
+ arr = np.arange(9).reshape((3, 3))
1035
+ assert_raises(AxisError, random.shuffle, arr, 3)
1036
+ assert_raises(TypeError, random.shuffle, arr, slice(1, 2, None))
1037
+ arr = [[1, 2, 3], [4, 5, 6]]
1038
+ assert_raises(NotImplementedError, random.shuffle, arr, 1)
1039
+
1040
+ arr = np.array(3)
1041
+ assert_raises(TypeError, random.shuffle, arr)
1042
+ arr = np.ones((3, 2))
1043
+ assert_raises(AxisError, random.shuffle, arr, 2)
1044
+
1045
+ def test_shuffle_not_writeable(self):
1046
+ random = Generator(MT19937(self.seed))
1047
+ a = np.zeros(5)
1048
+ a.flags.writeable = False
1049
+ with pytest.raises(ValueError, match='read-only'):
1050
+ random.shuffle(a)
1051
+
1052
+ def test_permutation(self):
1053
+ random = Generator(MT19937(self.seed))
1054
+ alist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
1055
+ actual = random.permutation(alist)
1056
+ desired = [4, 1, 9, 8, 0, 5, 3, 6, 2, 7]
1057
+ assert_array_equal(actual, desired)
1058
+
1059
+ random = Generator(MT19937(self.seed))
1060
+ arr_2d = np.atleast_2d([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]).T
1061
+ actual = random.permutation(arr_2d)
1062
+ assert_array_equal(actual, np.atleast_2d(desired).T)
1063
+
1064
+ bad_x_str = "abcd"
1065
+ assert_raises(AxisError, random.permutation, bad_x_str)
1066
+
1067
+ bad_x_float = 1.2
1068
+ assert_raises(AxisError, random.permutation, bad_x_float)
1069
+
1070
+ random = Generator(MT19937(self.seed))
1071
+ integer_val = 10
1072
+ desired = [3, 0, 8, 7, 9, 4, 2, 5, 1, 6]
1073
+
1074
+ actual = random.permutation(integer_val)
1075
+ assert_array_equal(actual, desired)
1076
+
1077
+ def test_permutation_custom_axis(self):
1078
+ a = np.arange(16).reshape((4, 4))
1079
+ desired = np.array([[ 0, 3, 1, 2],
1080
+ [ 4, 7, 5, 6],
1081
+ [ 8, 11, 9, 10],
1082
+ [12, 15, 13, 14]])
1083
+ random = Generator(MT19937(self.seed))
1084
+ actual = random.permutation(a, axis=1)
1085
+ assert_array_equal(actual, desired)
1086
+ random = Generator(MT19937(self.seed))
1087
+ actual = random.permutation(a, axis=-1)
1088
+ assert_array_equal(actual, desired)
1089
+
1090
+ def test_permutation_exceptions(self):
1091
+ random = Generator(MT19937(self.seed))
1092
+ arr = np.arange(10)
1093
+ assert_raises(AxisError, random.permutation, arr, 1)
1094
+ arr = np.arange(9).reshape((3, 3))
1095
+ assert_raises(AxisError, random.permutation, arr, 3)
1096
+ assert_raises(TypeError, random.permutation, arr, slice(1, 2, None))
1097
+
1098
+ @pytest.mark.parametrize("dtype", [int, object])
1099
+ @pytest.mark.parametrize("axis, expected",
1100
+ [(None, np.array([[3, 7, 0, 9, 10, 11],
1101
+ [8, 4, 2, 5, 1, 6]])),
1102
+ (0, np.array([[6, 1, 2, 9, 10, 11],
1103
+ [0, 7, 8, 3, 4, 5]])),
1104
+ (1, np.array([[ 5, 3, 4, 0, 2, 1],
1105
+ [11, 9, 10, 6, 8, 7]]))])
1106
+ def test_permuted(self, dtype, axis, expected):
1107
+ random = Generator(MT19937(self.seed))
1108
+ x = np.arange(12).reshape(2, 6).astype(dtype)
1109
+ random.permuted(x, axis=axis, out=x)
1110
+ assert_array_equal(x, expected)
1111
+
1112
+ random = Generator(MT19937(self.seed))
1113
+ x = np.arange(12).reshape(2, 6).astype(dtype)
1114
+ y = random.permuted(x, axis=axis)
1115
+ assert y.dtype == dtype
1116
+ assert_array_equal(y, expected)
1117
+
1118
+ def test_permuted_with_strides(self):
1119
+ random = Generator(MT19937(self.seed))
1120
+ x0 = np.arange(22).reshape(2, 11)
1121
+ x1 = x0.copy()
1122
+ x = x0[:, ::3]
1123
+ y = random.permuted(x, axis=1, out=x)
1124
+ expected = np.array([[0, 9, 3, 6],
1125
+ [14, 20, 11, 17]])
1126
+ assert_array_equal(y, expected)
1127
+ x1[:, ::3] = expected
1128
+ # Verify that the original x0 was modified in-place as expected.
1129
+ assert_array_equal(x1, x0)
1130
+
1131
+ def test_permuted_empty(self):
1132
+ y = random.permuted([])
1133
+ assert_array_equal(y, [])
1134
+
1135
+ @pytest.mark.parametrize('outshape', [(2, 3), 5])
1136
+ def test_permuted_out_with_wrong_shape(self, outshape):
1137
+ a = np.array([1, 2, 3])
1138
+ out = np.zeros(outshape, dtype=a.dtype)
1139
+ with pytest.raises(ValueError, match='same shape'):
1140
+ random.permuted(a, out=out)
1141
+
1142
+ def test_permuted_out_with_wrong_type(self):
1143
+ out = np.zeros((3, 5), dtype=np.int32)
1144
+ x = np.ones((3, 5))
1145
+ with pytest.raises(TypeError, match='Cannot cast'):
1146
+ random.permuted(x, axis=1, out=out)
1147
+
1148
+ def test_permuted_not_writeable(self):
1149
+ x = np.zeros((2, 5))
1150
+ x.flags.writeable = False
1151
+ with pytest.raises(ValueError, match='read-only'):
1152
+ random.permuted(x, axis=1, out=x)
1153
+
1154
+ def test_beta(self):
1155
+ random = Generator(MT19937(self.seed))
1156
+ actual = random.beta(.1, .9, size=(3, 2))
1157
+ desired = np.array(
1158
+ [[1.083029353267698e-10, 2.449965303168024e-11],
1159
+ [2.397085162969853e-02, 3.590779671820755e-08],
1160
+ [2.830254190078299e-04, 1.744709918330393e-01]])
1161
+ assert_array_almost_equal(actual, desired, decimal=15)
1162
+
1163
+ def test_binomial(self):
1164
+ random = Generator(MT19937(self.seed))
1165
+ actual = random.binomial(100.123, .456, size=(3, 2))
1166
+ desired = np.array([[42, 41],
1167
+ [42, 48],
1168
+ [44, 50]])
1169
+ assert_array_equal(actual, desired)
1170
+
1171
+ random = Generator(MT19937(self.seed))
1172
+ actual = random.binomial(100.123, .456)
1173
+ desired = 42
1174
+ assert_array_equal(actual, desired)
1175
+
1176
+ def test_chisquare(self):
1177
+ random = Generator(MT19937(self.seed))
1178
+ actual = random.chisquare(50, size=(3, 2))
1179
+ desired = np.array([[32.9850547060149, 39.0219480493301],
1180
+ [56.2006134779419, 57.3474165711485],
1181
+ [55.4243733880198, 55.4209797925213]])
1182
+ assert_array_almost_equal(actual, desired, decimal=13)
1183
+
1184
+ def test_dirichlet(self):
1185
+ random = Generator(MT19937(self.seed))
1186
+ alpha = np.array([51.72840233779265162, 39.74494232180943953])
1187
+ actual = random.dirichlet(alpha, size=(3, 2))
1188
+ desired = np.array([[[0.5439892869558927, 0.45601071304410745],
1189
+ [0.5588917345860708, 0.4411082654139292 ]], # noqa: E202
1190
+ [[0.5632074165063435, 0.43679258349365657],
1191
+ [0.54862581112627, 0.45137418887373015]],
1192
+ [[0.49961831357047226, 0.5003816864295278 ], # noqa: E202
1193
+ [0.52374806183482, 0.47625193816517997]]])
1194
+ assert_array_almost_equal(actual, desired, decimal=15)
1195
+ bad_alpha = np.array([5.4e-01, -1.0e-16])
1196
+ assert_raises(ValueError, random.dirichlet, bad_alpha)
1197
+
1198
+ random = Generator(MT19937(self.seed))
1199
+ alpha = np.array([51.72840233779265162, 39.74494232180943953])
1200
+ actual = random.dirichlet(alpha)
1201
+ assert_array_almost_equal(actual, desired[0, 0], decimal=15)
1202
+
1203
+ def test_dirichlet_size(self):
1204
+ # gh-3173
1205
+ p = np.array([51.72840233779265162, 39.74494232180943953])
1206
+ assert_equal(random.dirichlet(p, np.uint32(1)).shape, (1, 2))
1207
+ assert_equal(random.dirichlet(p, np.uint32(1)).shape, (1, 2))
1208
+ assert_equal(random.dirichlet(p, np.uint32(1)).shape, (1, 2))
1209
+ assert_equal(random.dirichlet(p, [2, 2]).shape, (2, 2, 2))
1210
+ assert_equal(random.dirichlet(p, (2, 2)).shape, (2, 2, 2))
1211
+ assert_equal(random.dirichlet(p, np.array((2, 2))).shape, (2, 2, 2))
1212
+
1213
+ assert_raises(TypeError, random.dirichlet, p, float(1))
1214
+
1215
+ def test_dirichlet_bad_alpha(self):
1216
+ # gh-2089
1217
+ alpha = np.array([5.4e-01, -1.0e-16])
1218
+ assert_raises(ValueError, random.dirichlet, alpha)
1219
+
1220
+ # gh-15876
1221
+ assert_raises(ValueError, random.dirichlet, [[5, 1]])
1222
+ assert_raises(ValueError, random.dirichlet, [[5], [1]])
1223
+ assert_raises(ValueError, random.dirichlet, [[[5], [1]], [[1], [5]]])
1224
+ assert_raises(ValueError, random.dirichlet, np.array([[5, 1], [1, 5]]))
1225
+
1226
+ def test_dirichlet_alpha_non_contiguous(self):
1227
+ a = np.array([51.72840233779265162, -1.0, 39.74494232180943953])
1228
+ alpha = a[::2]
1229
+ random = Generator(MT19937(self.seed))
1230
+ non_contig = random.dirichlet(alpha, size=(3, 2))
1231
+ random = Generator(MT19937(self.seed))
1232
+ contig = random.dirichlet(np.ascontiguousarray(alpha),
1233
+ size=(3, 2))
1234
+ assert_array_almost_equal(non_contig, contig)
1235
+
1236
+ def test_dirichlet_small_alpha(self):
1237
+ eps = 1.0e-9 # 1.0e-10 -> runtime x 10; 1e-11 -> runtime x 200, etc.
1238
+ alpha = eps * np.array([1., 1.0e-3])
1239
+ random = Generator(MT19937(self.seed))
1240
+ actual = random.dirichlet(alpha, size=(3, 2))
1241
+ expected = np.array([
1242
+ [[1., 0.],
1243
+ [1., 0.]],
1244
+ [[1., 0.],
1245
+ [1., 0.]],
1246
+ [[1., 0.],
1247
+ [1., 0.]]
1248
+ ])
1249
+ assert_array_almost_equal(actual, expected, decimal=15)
1250
+
1251
+ @pytest.mark.slow
1252
+ def test_dirichlet_moderately_small_alpha(self):
1253
+ # Use alpha.max() < 0.1 to trigger stick breaking code path
1254
+ alpha = np.array([0.02, 0.04, 0.03])
1255
+ exact_mean = alpha / alpha.sum()
1256
+ random = Generator(MT19937(self.seed))
1257
+ sample = random.dirichlet(alpha, size=20000000)
1258
+ sample_mean = sample.mean(axis=0)
1259
+ assert_allclose(sample_mean, exact_mean, rtol=1e-3)
1260
+
1261
+ # This set of parameters includes inputs with alpha.max() >= 0.1 and
1262
+ # alpha.max() < 0.1 to exercise both generation methods within the
1263
+ # dirichlet code.
1264
+ @pytest.mark.parametrize(
1265
+ 'alpha',
1266
+ [[5, 9, 0, 8],
1267
+ [0.5, 0, 0, 0],
1268
+ [1, 5, 0, 0, 1.5, 0, 0, 0],
1269
+ [0.01, 0.03, 0, 0.005],
1270
+ [1e-5, 0, 0, 0],
1271
+ [0.002, 0.015, 0, 0, 0.04, 0, 0, 0],
1272
+ [0.0],
1273
+ [0, 0, 0]],
1274
+ )
1275
+ def test_dirichlet_multiple_zeros_in_alpha(self, alpha):
1276
+ alpha = np.array(alpha)
1277
+ y = random.dirichlet(alpha)
1278
+ assert_equal(y[alpha == 0], 0.0)
1279
+
1280
+ def test_exponential(self):
1281
+ random = Generator(MT19937(self.seed))
1282
+ actual = random.exponential(1.1234, size=(3, 2))
1283
+ desired = np.array([[0.098845481066258, 1.560752510746964],
1284
+ [0.075730916041636, 1.769098974710777],
1285
+ [1.488602544592235, 2.49684815275751 ]]) # noqa: E202
1286
+ assert_array_almost_equal(actual, desired, decimal=15)
1287
+
1288
+ def test_exponential_0(self):
1289
+ assert_equal(random.exponential(scale=0), 0)
1290
+ assert_raises(ValueError, random.exponential, scale=-0.)
1291
+
1292
+ def test_f(self):
1293
+ random = Generator(MT19937(self.seed))
1294
+ actual = random.f(12, 77, size=(3, 2))
1295
+ desired = np.array([[0.461720027077085, 1.100441958872451],
1296
+ [1.100337455217484, 0.91421736740018 ], # noqa: E202
1297
+ [0.500811891303113, 0.826802454552058]])
1298
+ assert_array_almost_equal(actual, desired, decimal=15)
1299
+
1300
+ def test_gamma(self):
1301
+ random = Generator(MT19937(self.seed))
1302
+ actual = random.gamma(5, 3, size=(3, 2))
1303
+ desired = np.array([[ 5.03850858902096, 7.9228656732049 ], # noqa: E202
1304
+ [18.73983605132985, 19.57961681699238],
1305
+ [18.17897755150825, 18.17653912505234]])
1306
+ assert_array_almost_equal(actual, desired, decimal=14)
1307
+
1308
+ def test_gamma_0(self):
1309
+ assert_equal(random.gamma(shape=0, scale=0), 0)
1310
+ assert_raises(ValueError, random.gamma, shape=-0., scale=-0.)
1311
+
1312
+ def test_geometric(self):
1313
+ random = Generator(MT19937(self.seed))
1314
+ actual = random.geometric(.123456789, size=(3, 2))
1315
+ desired = np.array([[1, 11],
1316
+ [1, 12],
1317
+ [11, 17]])
1318
+ assert_array_equal(actual, desired)
1319
+
1320
+ def test_geometric_exceptions(self):
1321
+ assert_raises(ValueError, random.geometric, 1.1)
1322
+ assert_raises(ValueError, random.geometric, [1.1] * 10)
1323
+ assert_raises(ValueError, random.geometric, -0.1)
1324
+ assert_raises(ValueError, random.geometric, [-0.1] * 10)
1325
+ with np.errstate(invalid='ignore'):
1326
+ assert_raises(ValueError, random.geometric, np.nan)
1327
+ assert_raises(ValueError, random.geometric, [np.nan] * 10)
1328
+
1329
+ def test_gumbel(self):
1330
+ random = Generator(MT19937(self.seed))
1331
+ actual = random.gumbel(loc=.123456789, scale=2.0, size=(3, 2))
1332
+ desired = np.array([[ 4.688397515056245, -0.289514845417841],
1333
+ [ 4.981176042584683, -0.633224272589149],
1334
+ [-0.055915275687488, -0.333962478257953]])
1335
+ assert_array_almost_equal(actual, desired, decimal=15)
1336
+
1337
+ def test_gumbel_0(self):
1338
+ assert_equal(random.gumbel(scale=0), 0)
1339
+ assert_raises(ValueError, random.gumbel, scale=-0.)
1340
+
1341
+ def test_hypergeometric(self):
1342
+ random = Generator(MT19937(self.seed))
1343
+ actual = random.hypergeometric(10.1, 5.5, 14, size=(3, 2))
1344
+ desired = np.array([[ 9, 9],
1345
+ [ 9, 9],
1346
+ [10, 9]])
1347
+ assert_array_equal(actual, desired)
1348
+
1349
+ # Test nbad = 0
1350
+ actual = random.hypergeometric(5, 0, 3, size=4)
1351
+ desired = np.array([3, 3, 3, 3])
1352
+ assert_array_equal(actual, desired)
1353
+
1354
+ actual = random.hypergeometric(15, 0, 12, size=4)
1355
+ desired = np.array([12, 12, 12, 12])
1356
+ assert_array_equal(actual, desired)
1357
+
1358
+ # Test ngood = 0
1359
+ actual = random.hypergeometric(0, 5, 3, size=4)
1360
+ desired = np.array([0, 0, 0, 0])
1361
+ assert_array_equal(actual, desired)
1362
+
1363
+ actual = random.hypergeometric(0, 15, 12, size=4)
1364
+ desired = np.array([0, 0, 0, 0])
1365
+ assert_array_equal(actual, desired)
1366
+
1367
+ def test_laplace(self):
1368
+ random = Generator(MT19937(self.seed))
1369
+ actual = random.laplace(loc=.123456789, scale=2.0, size=(3, 2))
1370
+ desired = np.array([[-3.156353949272393, 1.195863024830054],
1371
+ [-3.435458081645966, 1.656882398925444],
1372
+ [ 0.924824032467446, 1.251116432209336]])
1373
+ assert_array_almost_equal(actual, desired, decimal=15)
1374
+
1375
+ def test_laplace_0(self):
1376
+ assert_equal(random.laplace(scale=0), 0)
1377
+ assert_raises(ValueError, random.laplace, scale=-0.)
1378
+
1379
+ def test_logistic(self):
1380
+ random = Generator(MT19937(self.seed))
1381
+ actual = random.logistic(loc=.123456789, scale=2.0, size=(3, 2))
1382
+ desired = np.array([[-4.338584631510999, 1.890171436749954],
1383
+ [-4.64547787337966 , 2.514545562919217], # noqa: E203
1384
+ [ 1.495389489198666, 1.967827627577474]])
1385
+ assert_array_almost_equal(actual, desired, decimal=15)
1386
+
1387
+ def test_lognormal(self):
1388
+ random = Generator(MT19937(self.seed))
1389
+ actual = random.lognormal(mean=.123456789, sigma=2.0, size=(3, 2))
1390
+ desired = np.array([[ 0.0268252166335, 13.9534486483053],
1391
+ [ 0.1204014788936, 2.2422077497792],
1392
+ [ 4.2484199496128, 12.0093343977523]])
1393
+ assert_array_almost_equal(actual, desired, decimal=13)
1394
+
1395
+ def test_lognormal_0(self):
1396
+ assert_equal(random.lognormal(sigma=0), 1)
1397
+ assert_raises(ValueError, random.lognormal, sigma=-0.)
1398
+
1399
+ def test_logseries(self):
1400
+ random = Generator(MT19937(self.seed))
1401
+ actual = random.logseries(p=.923456789, size=(3, 2))
1402
+ desired = np.array([[14, 17],
1403
+ [3, 18],
1404
+ [5, 1]])
1405
+ assert_array_equal(actual, desired)
1406
+
1407
+ def test_logseries_zero(self):
1408
+ random = Generator(MT19937(self.seed))
1409
+ assert random.logseries(0) == 1
1410
+
1411
+ @pytest.mark.parametrize("value", [np.nextafter(0., -1), 1., np.nan, 5.])
1412
+ def test_logseries_exceptions(self, value):
1413
+ random = Generator(MT19937(self.seed))
1414
+ with np.errstate(invalid="ignore"):
1415
+ with pytest.raises(ValueError):
1416
+ random.logseries(value)
1417
+ with pytest.raises(ValueError):
1418
+ # contiguous path:
1419
+ random.logseries(np.array([value] * 10))
1420
+ with pytest.raises(ValueError):
1421
+ # non-contiguous path:
1422
+ random.logseries(np.array([value] * 10)[::2])
1423
+
1424
+ def test_multinomial(self):
1425
+ random = Generator(MT19937(self.seed))
1426
+ actual = random.multinomial(20, [1 / 6.] * 6, size=(3, 2))
1427
+ desired = np.array([[[1, 5, 1, 6, 4, 3],
1428
+ [4, 2, 6, 2, 4, 2]],
1429
+ [[5, 3, 2, 6, 3, 1],
1430
+ [4, 4, 0, 2, 3, 7]],
1431
+ [[6, 3, 1, 5, 3, 2],
1432
+ [5, 5, 3, 1, 2, 4]]])
1433
+ assert_array_equal(actual, desired)
1434
+
1435
+ @pytest.mark.skipif(IS_WASM, reason="fp errors don't work in wasm")
1436
+ @pytest.mark.parametrize("method", ["svd", "eigh", "cholesky"])
1437
+ def test_multivariate_normal(self, method):
1438
+ random = Generator(MT19937(self.seed))
1439
+ mean = (.123456789, 10)
1440
+ cov = [[1, 0], [0, 1]]
1441
+ size = (3, 2)
1442
+ actual = random.multivariate_normal(mean, cov, size, method=method)
1443
+ desired = np.array([[[-1.747478062846581, 11.25613495182354 ], # noqa: E202
1444
+ [-0.9967333370066214, 10.342002097029821]],
1445
+ [[ 0.7850019631242964, 11.181113712443013],
1446
+ [ 0.8901349653255224, 8.873825399642492]],
1447
+ [[ 0.7130260107430003, 9.551628690083056],
1448
+ [ 0.7127098726541128, 11.991709234143173]]])
1449
+
1450
+ assert_array_almost_equal(actual, desired, decimal=15)
1451
+
1452
+ # Check for default size, was raising deprecation warning
1453
+ actual = random.multivariate_normal(mean, cov, method=method)
1454
+ desired = np.array([0.233278563284287, 9.424140804347195])
1455
+ assert_array_almost_equal(actual, desired, decimal=15)
1456
+ # Check that non symmetric covariance input raises exception when
1457
+ # check_valid='raises' if using default svd method.
1458
+ mean = [0, 0]
1459
+ cov = [[1, 2], [1, 2]]
1460
+ assert_raises(ValueError, random.multivariate_normal, mean, cov,
1461
+ check_valid='raise')
1462
+
1463
+ # Check that non positive-semidefinite covariance warns with
1464
+ # RuntimeWarning
1465
+ cov = [[1, 2], [2, 1]]
1466
+ assert_warns(RuntimeWarning, random.multivariate_normal, mean, cov)
1467
+ assert_warns(RuntimeWarning, random.multivariate_normal, mean, cov,
1468
+ method='eigh')
1469
+ assert_raises(LinAlgError, random.multivariate_normal, mean, cov,
1470
+ method='cholesky')
1471
+
1472
+ # and that it doesn't warn with RuntimeWarning check_valid='ignore'
1473
+ assert_no_warnings(random.multivariate_normal, mean, cov,
1474
+ check_valid='ignore')
1475
+
1476
+ # and that it raises with RuntimeWarning check_valid='raises'
1477
+ assert_raises(ValueError, random.multivariate_normal, mean, cov,
1478
+ check_valid='raise')
1479
+ assert_raises(ValueError, random.multivariate_normal, mean, cov,
1480
+ check_valid='raise', method='eigh')
1481
+
1482
+ # check degenerate samples from singular covariance matrix
1483
+ cov = [[1, 1], [1, 1]]
1484
+ if method in ('svd', 'eigh'):
1485
+ samples = random.multivariate_normal(mean, cov, size=(3, 2),
1486
+ method=method)
1487
+ assert_array_almost_equal(samples[..., 0], samples[..., 1],
1488
+ decimal=6)
1489
+ else:
1490
+ assert_raises(LinAlgError, random.multivariate_normal, mean, cov,
1491
+ method='cholesky')
1492
+
1493
+ cov = np.array([[1, 0.1], [0.1, 1]], dtype=np.float32)
1494
+ with suppress_warnings() as sup:
1495
+ random.multivariate_normal(mean, cov, method=method)
1496
+ w = sup.record(RuntimeWarning)
1497
+ assert len(w) == 0
1498
+
1499
+ mu = np.zeros(2)
1500
+ cov = np.eye(2)
1501
+ assert_raises(ValueError, random.multivariate_normal, mean, cov,
1502
+ check_valid='other')
1503
+ assert_raises(ValueError, random.multivariate_normal,
1504
+ np.zeros((2, 1, 1)), cov)
1505
+ assert_raises(ValueError, random.multivariate_normal,
1506
+ mu, np.empty((3, 2)))
1507
+ assert_raises(ValueError, random.multivariate_normal,
1508
+ mu, np.eye(3))
1509
+
1510
+ @pytest.mark.parametrize('mean, cov', [([0], [[1 + 1j]]), ([0j], [[1]])])
1511
+ def test_multivariate_normal_disallow_complex(self, mean, cov):
1512
+ random = Generator(MT19937(self.seed))
1513
+ with pytest.raises(TypeError, match="must not be complex"):
1514
+ random.multivariate_normal(mean, cov)
1515
+
1516
+ @pytest.mark.parametrize("method", ["svd", "eigh", "cholesky"])
1517
+ def test_multivariate_normal_basic_stats(self, method):
1518
+ random = Generator(MT19937(self.seed))
1519
+ n_s = 1000
1520
+ mean = np.array([1, 2])
1521
+ cov = np.array([[2, 1], [1, 2]])
1522
+ s = random.multivariate_normal(mean, cov, size=(n_s,), method=method)
1523
+ s_center = s - mean
1524
+ cov_emp = (s_center.T @ s_center) / (n_s - 1)
1525
+ # these are pretty loose and are only designed to detect major errors
1526
+ assert np.all(np.abs(s_center.mean(-2)) < 0.1)
1527
+ assert np.all(np.abs(cov_emp - cov) < 0.2)
1528
+
1529
+ def test_negative_binomial(self):
1530
+ random = Generator(MT19937(self.seed))
1531
+ actual = random.negative_binomial(n=100, p=.12345, size=(3, 2))
1532
+ desired = np.array([[543, 727],
1533
+ [775, 760],
1534
+ [600, 674]])
1535
+ assert_array_equal(actual, desired)
1536
+
1537
+ def test_negative_binomial_exceptions(self):
1538
+ with np.errstate(invalid='ignore'):
1539
+ assert_raises(ValueError, random.negative_binomial, 100, np.nan)
1540
+ assert_raises(ValueError, random.negative_binomial, 100,
1541
+ [np.nan] * 10)
1542
+
1543
+ def test_negative_binomial_p0_exception(self):
1544
+ # Verify that p=0 raises an exception.
1545
+ with assert_raises(ValueError):
1546
+ x = random.negative_binomial(1, 0)
1547
+
1548
+ def test_negative_binomial_invalid_p_n_combination(self):
1549
+ # Verify that values of p and n that would result in an overflow
1550
+ # or infinite loop raise an exception.
1551
+ with np.errstate(invalid='ignore'):
1552
+ assert_raises(ValueError, random.negative_binomial, 2**62, 0.1)
1553
+ assert_raises(ValueError, random.negative_binomial, [2**62], [0.1])
1554
+
1555
+ def test_noncentral_chisquare(self):
1556
+ random = Generator(MT19937(self.seed))
1557
+ actual = random.noncentral_chisquare(df=5, nonc=5, size=(3, 2))
1558
+ desired = np.array([[ 1.70561552362133, 15.97378184942111],
1559
+ [13.71483425173724, 20.17859633310629],
1560
+ [11.3615477156643 , 3.67891108738029]]) # noqa: E203
1561
+ assert_array_almost_equal(actual, desired, decimal=14)
1562
+
1563
+ actual = random.noncentral_chisquare(df=.5, nonc=.2, size=(3, 2))
1564
+ desired = np.array([[9.41427665607629e-04, 1.70473157518850e-04],
1565
+ [1.14554372041263e+00, 1.38187755933435e-03],
1566
+ [1.90659181905387e+00, 1.21772577941822e+00]])
1567
+ assert_array_almost_equal(actual, desired, decimal=14)
1568
+
1569
+ random = Generator(MT19937(self.seed))
1570
+ actual = random.noncentral_chisquare(df=5, nonc=0, size=(3, 2))
1571
+ desired = np.array([[0.82947954590419, 1.80139670767078],
1572
+ [6.58720057417794, 7.00491463609814],
1573
+ [6.31101879073157, 6.30982307753005]])
1574
+ assert_array_almost_equal(actual, desired, decimal=14)
1575
+
1576
+ def test_noncentral_f(self):
1577
+ random = Generator(MT19937(self.seed))
1578
+ actual = random.noncentral_f(dfnum=5, dfden=2, nonc=1,
1579
+ size=(3, 2))
1580
+ desired = np.array([[0.060310671139 , 0.23866058175939], # noqa: E203
1581
+ [0.86860246709073, 0.2668510459738 ], # noqa: E202
1582
+ [0.23375780078364, 1.88922102885943]])
1583
+ assert_array_almost_equal(actual, desired, decimal=14)
1584
+
1585
+ def test_noncentral_f_nan(self):
1586
+ random = Generator(MT19937(self.seed))
1587
+ actual = random.noncentral_f(dfnum=5, dfden=2, nonc=np.nan)
1588
+ assert np.isnan(actual)
1589
+
1590
+ def test_normal(self):
1591
+ random = Generator(MT19937(self.seed))
1592
+ actual = random.normal(loc=.123456789, scale=2.0, size=(3, 2))
1593
+ desired = np.array([[-3.618412914693162, 2.635726692647081],
1594
+ [-2.116923463013243, 0.807460983059643],
1595
+ [ 1.446547137248593, 2.485684213886024]])
1596
+ assert_array_almost_equal(actual, desired, decimal=15)
1597
+
1598
+ def test_normal_0(self):
1599
+ assert_equal(random.normal(scale=0), 0)
1600
+ assert_raises(ValueError, random.normal, scale=-0.)
1601
+
1602
+ def test_pareto(self):
1603
+ random = Generator(MT19937(self.seed))
1604
+ actual = random.pareto(a=.123456789, size=(3, 2))
1605
+ desired = np.array([[1.0394926776069018e+00, 7.7142534343505773e+04],
1606
+ [7.2640150889064703e-01, 3.4650454783825594e+05],
1607
+ [4.5852344481994740e+04, 6.5851383009539105e+07]])
1608
+ # For some reason on 32-bit x86 Ubuntu 12.10 the [1, 0] entry in this
1609
+ # matrix differs by 24 nulps. Discussion:
1610
+ # https://mail.python.org/pipermail/numpy-discussion/2012-September/063801.html
1611
+ # Consensus is that this is probably some gcc quirk that affects
1612
+ # rounding but not in any important way, so we just use a looser
1613
+ # tolerance on this test:
1614
+ np.testing.assert_array_almost_equal_nulp(actual, desired, nulp=30)
1615
+
1616
+ def test_poisson(self):
1617
+ random = Generator(MT19937(self.seed))
1618
+ actual = random.poisson(lam=.123456789, size=(3, 2))
1619
+ desired = np.array([[0, 0],
1620
+ [0, 0],
1621
+ [0, 0]])
1622
+ assert_array_equal(actual, desired)
1623
+
1624
+ def test_poisson_exceptions(self):
1625
+ lambig = np.iinfo('int64').max
1626
+ lamneg = -1
1627
+ assert_raises(ValueError, random.poisson, lamneg)
1628
+ assert_raises(ValueError, random.poisson, [lamneg] * 10)
1629
+ assert_raises(ValueError, random.poisson, lambig)
1630
+ assert_raises(ValueError, random.poisson, [lambig] * 10)
1631
+ with np.errstate(invalid='ignore'):
1632
+ assert_raises(ValueError, random.poisson, np.nan)
1633
+ assert_raises(ValueError, random.poisson, [np.nan] * 10)
1634
+
1635
+ def test_power(self):
1636
+ random = Generator(MT19937(self.seed))
1637
+ actual = random.power(a=.123456789, size=(3, 2))
1638
+ desired = np.array([[1.977857368842754e-09, 9.806792196620341e-02],
1639
+ [2.482442984543471e-10, 1.527108843266079e-01],
1640
+ [8.188283434244285e-02, 3.950547209346948e-01]])
1641
+ assert_array_almost_equal(actual, desired, decimal=15)
1642
+
1643
+ def test_rayleigh(self):
1644
+ random = Generator(MT19937(self.seed))
1645
+ actual = random.rayleigh(scale=10, size=(3, 2))
1646
+ desired = np.array([[4.19494429102666, 16.66920198906598],
1647
+ [3.67184544902662, 17.74695521962917],
1648
+ [16.27935397855501, 21.08355560691792]])
1649
+ assert_array_almost_equal(actual, desired, decimal=14)
1650
+
1651
+ def test_rayleigh_0(self):
1652
+ assert_equal(random.rayleigh(scale=0), 0)
1653
+ assert_raises(ValueError, random.rayleigh, scale=-0.)
1654
+
1655
+ def test_standard_cauchy(self):
1656
+ random = Generator(MT19937(self.seed))
1657
+ actual = random.standard_cauchy(size=(3, 2))
1658
+ desired = np.array([[-1.489437778266206, -3.275389641569784],
1659
+ [ 0.560102864910406, -0.680780916282552],
1660
+ [-1.314912905226277, 0.295852965660225]])
1661
+ assert_array_almost_equal(actual, desired, decimal=15)
1662
+
1663
+ def test_standard_exponential(self):
1664
+ random = Generator(MT19937(self.seed))
1665
+ actual = random.standard_exponential(size=(3, 2), method='inv')
1666
+ desired = np.array([[0.102031839440643, 1.229350298474972],
1667
+ [0.088137284693098, 1.459859985522667],
1668
+ [1.093830802293668, 1.256977002164613]])
1669
+ assert_array_almost_equal(actual, desired, decimal=15)
1670
+
1671
+ def test_standard_expoential_type_error(self):
1672
+ assert_raises(TypeError, random.standard_exponential, dtype=np.int32)
1673
+
1674
+ def test_standard_gamma(self):
1675
+ random = Generator(MT19937(self.seed))
1676
+ actual = random.standard_gamma(shape=3, size=(3, 2))
1677
+ desired = np.array([[0.62970724056362, 1.22379851271008],
1678
+ [3.899412530884 , 4.12479964250139], # noqa: E203
1679
+ [3.74994102464584, 3.74929307690815]])
1680
+ assert_array_almost_equal(actual, desired, decimal=14)
1681
+
1682
+ def test_standard_gammma_scalar_float(self):
1683
+ random = Generator(MT19937(self.seed))
1684
+ actual = random.standard_gamma(3, dtype=np.float32)
1685
+ desired = 2.9242148399353027
1686
+ assert_array_almost_equal(actual, desired, decimal=6)
1687
+
1688
+ def test_standard_gamma_float(self):
1689
+ random = Generator(MT19937(self.seed))
1690
+ actual = random.standard_gamma(shape=3, size=(3, 2))
1691
+ desired = np.array([[0.62971, 1.2238],
1692
+ [3.89941, 4.1248],
1693
+ [3.74994, 3.74929]])
1694
+ assert_array_almost_equal(actual, desired, decimal=5)
1695
+
1696
+ def test_standard_gammma_float_out(self):
1697
+ actual = np.zeros((3, 2), dtype=np.float32)
1698
+ random = Generator(MT19937(self.seed))
1699
+ random.standard_gamma(10.0, out=actual, dtype=np.float32)
1700
+ desired = np.array([[10.14987, 7.87012],
1701
+ [ 9.46284, 12.56832],
1702
+ [13.82495, 7.81533]], dtype=np.float32)
1703
+ assert_array_almost_equal(actual, desired, decimal=5)
1704
+
1705
+ random = Generator(MT19937(self.seed))
1706
+ random.standard_gamma(10.0, out=actual, size=(3, 2), dtype=np.float32)
1707
+ assert_array_almost_equal(actual, desired, decimal=5)
1708
+
1709
+ def test_standard_gamma_unknown_type(self):
1710
+ assert_raises(TypeError, random.standard_gamma, 1.,
1711
+ dtype='int32')
1712
+
1713
+ def test_out_size_mismatch(self):
1714
+ out = np.zeros(10)
1715
+ assert_raises(ValueError, random.standard_gamma, 10.0, size=20,
1716
+ out=out)
1717
+ assert_raises(ValueError, random.standard_gamma, 10.0, size=(10, 1),
1718
+ out=out)
1719
+
1720
+ def test_standard_gamma_0(self):
1721
+ assert_equal(random.standard_gamma(shape=0), 0)
1722
+ assert_raises(ValueError, random.standard_gamma, shape=-0.)
1723
+
1724
+ def test_standard_normal(self):
1725
+ random = Generator(MT19937(self.seed))
1726
+ actual = random.standard_normal(size=(3, 2))
1727
+ desired = np.array([[-1.870934851846581, 1.25613495182354 ], # noqa: E202
1728
+ [-1.120190126006621, 0.342002097029821],
1729
+ [ 0.661545174124296, 1.181113712443012]])
1730
+ assert_array_almost_equal(actual, desired, decimal=15)
1731
+
1732
+ def test_standard_normal_unsupported_type(self):
1733
+ assert_raises(TypeError, random.standard_normal, dtype=np.int32)
1734
+
1735
+ def test_standard_t(self):
1736
+ random = Generator(MT19937(self.seed))
1737
+ actual = random.standard_t(df=10, size=(3, 2))
1738
+ desired = np.array([[-1.484666193042647, 0.30597891831161],
1739
+ [ 1.056684299648085, -0.407312602088507],
1740
+ [ 0.130704414281157, -2.038053410490321]])
1741
+ assert_array_almost_equal(actual, desired, decimal=15)
1742
+
1743
+ def test_triangular(self):
1744
+ random = Generator(MT19937(self.seed))
1745
+ actual = random.triangular(left=5.12, mode=10.23, right=20.34,
1746
+ size=(3, 2))
1747
+ desired = np.array([[ 7.86664070590917, 13.6313848513185 ], # noqa: E202
1748
+ [ 7.68152445215983, 14.36169131136546],
1749
+ [13.16105603911429, 13.72341621856971]])
1750
+ assert_array_almost_equal(actual, desired, decimal=14)
1751
+
1752
+ def test_uniform(self):
1753
+ random = Generator(MT19937(self.seed))
1754
+ actual = random.uniform(low=1.23, high=10.54, size=(3, 2))
1755
+ desired = np.array([[2.13306255040998 , 7.816987531021207], # noqa: E203
1756
+ [2.015436610109887, 8.377577533009589],
1757
+ [7.421792588856135, 7.891185744455209]])
1758
+ assert_array_almost_equal(actual, desired, decimal=15)
1759
+
1760
+ def test_uniform_range_bounds(self):
1761
+ fmin = np.finfo('float').min
1762
+ fmax = np.finfo('float').max
1763
+
1764
+ func = random.uniform
1765
+ assert_raises(OverflowError, func, -np.inf, 0)
1766
+ assert_raises(OverflowError, func, 0, np.inf)
1767
+ assert_raises(OverflowError, func, fmin, fmax)
1768
+ assert_raises(OverflowError, func, [-np.inf], [0])
1769
+ assert_raises(OverflowError, func, [0], [np.inf])
1770
+
1771
+ # (fmax / 1e17) - fmin is within range, so this should not throw
1772
+ # account for i386 extended precision DBL_MAX / 1e17 + DBL_MAX >
1773
+ # DBL_MAX by increasing fmin a bit
1774
+ random.uniform(low=np.nextafter(fmin, 1), high=fmax / 1e17)
1775
+
1776
+ def test_uniform_zero_range(self):
1777
+ func = random.uniform
1778
+ result = func(1.5, 1.5)
1779
+ assert_allclose(result, 1.5)
1780
+ result = func([0.0, np.pi], [0.0, np.pi])
1781
+ assert_allclose(result, [0.0, np.pi])
1782
+ result = func([[2145.12], [2145.12]], [2145.12, 2145.12])
1783
+ assert_allclose(result, 2145.12 + np.zeros((2, 2)))
1784
+
1785
+ def test_uniform_neg_range(self):
1786
+ func = random.uniform
1787
+ assert_raises(ValueError, func, 2, 1)
1788
+ assert_raises(ValueError, func, [1, 2], [1, 1])
1789
+ assert_raises(ValueError, func, [[0, 1], [2, 3]], 2)
1790
+
1791
+ def test_scalar_exception_propagation(self):
1792
+ # Tests that exceptions are correctly propagated in distributions
1793
+ # when called with objects that throw exceptions when converted to
1794
+ # scalars.
1795
+ #
1796
+ # Regression test for gh: 8865
1797
+
1798
+ class ThrowingFloat(np.ndarray):
1799
+ def __float__(self):
1800
+ raise TypeError
1801
+
1802
+ throwing_float = np.array(1.0).view(ThrowingFloat)
1803
+ assert_raises(TypeError, random.uniform, throwing_float,
1804
+ throwing_float)
1805
+
1806
+ class ThrowingInteger(np.ndarray):
1807
+ def __int__(self):
1808
+ raise TypeError
1809
+
1810
+ throwing_int = np.array(1).view(ThrowingInteger)
1811
+ assert_raises(TypeError, random.hypergeometric, throwing_int, 1, 1)
1812
+
1813
+ def test_vonmises(self):
1814
+ random = Generator(MT19937(self.seed))
1815
+ actual = random.vonmises(mu=1.23, kappa=1.54, size=(3, 2))
1816
+ desired = np.array([[ 1.107972248690106, 2.841536476232361],
1817
+ [ 1.832602376042457, 1.945511926976032],
1818
+ [-0.260147475776542, 2.058047492231698]])
1819
+ assert_array_almost_equal(actual, desired, decimal=15)
1820
+
1821
+ def test_vonmises_small(self):
1822
+ # check infinite loop, gh-4720
1823
+ random = Generator(MT19937(self.seed))
1824
+ r = random.vonmises(mu=0., kappa=1.1e-8, size=10**6)
1825
+ assert_(np.isfinite(r).all())
1826
+
1827
+ def test_vonmises_nan(self):
1828
+ random = Generator(MT19937(self.seed))
1829
+ r = random.vonmises(mu=0., kappa=np.nan)
1830
+ assert_(np.isnan(r))
1831
+
1832
+ @pytest.mark.parametrize("kappa", [1e4, 1e15])
1833
+ def test_vonmises_large_kappa(self, kappa):
1834
+ random = Generator(MT19937(self.seed))
1835
+ rs = RandomState(random.bit_generator)
1836
+ state = random.bit_generator.state
1837
+
1838
+ random_state_vals = rs.vonmises(0, kappa, size=10)
1839
+ random.bit_generator.state = state
1840
+ gen_vals = random.vonmises(0, kappa, size=10)
1841
+ if kappa < 1e6:
1842
+ assert_allclose(random_state_vals, gen_vals)
1843
+ else:
1844
+ assert np.all(random_state_vals != gen_vals)
1845
+
1846
+ @pytest.mark.parametrize("mu", [-7., -np.pi, -3.1, np.pi, 3.2])
1847
+ @pytest.mark.parametrize("kappa", [1e-9, 1e-6, 1, 1e3, 1e15])
1848
+ def test_vonmises_large_kappa_range(self, mu, kappa):
1849
+ random = Generator(MT19937(self.seed))
1850
+ r = random.vonmises(mu, kappa, 50)
1851
+ assert_(np.all(r > -np.pi) and np.all(r <= np.pi))
1852
+
1853
+ def test_wald(self):
1854
+ random = Generator(MT19937(self.seed))
1855
+ actual = random.wald(mean=1.23, scale=1.54, size=(3, 2))
1856
+ desired = np.array([[0.26871721804551, 3.2233942732115 ], # noqa: E202
1857
+ [2.20328374987066, 2.40958405189353],
1858
+ [2.07093587449261, 0.73073890064369]])
1859
+ assert_array_almost_equal(actual, desired, decimal=14)
1860
+
1861
+ def test_wald_nonnegative(self):
1862
+ random = Generator(MT19937(self.seed))
1863
+ samples = random.wald(mean=1e9, scale=2.25, size=1000)
1864
+ assert_(np.all(samples >= 0.0))
1865
+
1866
+ def test_weibull(self):
1867
+ random = Generator(MT19937(self.seed))
1868
+ actual = random.weibull(a=1.23, size=(3, 2))
1869
+ desired = np.array([[0.138613914769468, 1.306463419753191],
1870
+ [0.111623365934763, 1.446570494646721],
1871
+ [1.257145775276011, 1.914247725027957]])
1872
+ assert_array_almost_equal(actual, desired, decimal=15)
1873
+
1874
+ def test_weibull_0(self):
1875
+ random = Generator(MT19937(self.seed))
1876
+ assert_equal(random.weibull(a=0, size=12), np.zeros(12))
1877
+ assert_raises(ValueError, random.weibull, a=-0.)
1878
+
1879
+ def test_zipf(self):
1880
+ random = Generator(MT19937(self.seed))
1881
+ actual = random.zipf(a=1.23, size=(3, 2))
1882
+ desired = np.array([[ 1, 1],
1883
+ [ 10, 867],
1884
+ [354, 2]])
1885
+ assert_array_equal(actual, desired)
1886
+
1887
+
1888
+ class TestBroadcast:
1889
+ # tests that functions that broadcast behave
1890
+ # correctly when presented with non-scalar arguments
1891
+ def setup_method(self):
1892
+ self.seed = 123456789
1893
+
1894
+ def test_uniform(self):
1895
+ random = Generator(MT19937(self.seed))
1896
+ low = [0]
1897
+ high = [1]
1898
+ uniform = random.uniform
1899
+ desired = np.array([0.16693771389729, 0.19635129550675, 0.75563050964095])
1900
+
1901
+ random = Generator(MT19937(self.seed))
1902
+ actual = random.uniform(low * 3, high)
1903
+ assert_array_almost_equal(actual, desired, decimal=14)
1904
+
1905
+ random = Generator(MT19937(self.seed))
1906
+ actual = random.uniform(low, high * 3)
1907
+ assert_array_almost_equal(actual, desired, decimal=14)
1908
+
1909
+ def test_normal(self):
1910
+ loc = [0]
1911
+ scale = [1]
1912
+ bad_scale = [-1]
1913
+ random = Generator(MT19937(self.seed))
1914
+ desired = np.array([-0.38736406738527, 0.79594375042255, 0.0197076236097])
1915
+
1916
+ random = Generator(MT19937(self.seed))
1917
+ actual = random.normal(loc * 3, scale)
1918
+ assert_array_almost_equal(actual, desired, decimal=14)
1919
+ assert_raises(ValueError, random.normal, loc * 3, bad_scale)
1920
+
1921
+ random = Generator(MT19937(self.seed))
1922
+ normal = random.normal
1923
+ actual = normal(loc, scale * 3)
1924
+ assert_array_almost_equal(actual, desired, decimal=14)
1925
+ assert_raises(ValueError, normal, loc, bad_scale * 3)
1926
+
1927
+ def test_beta(self):
1928
+ a = [1]
1929
+ b = [2]
1930
+ bad_a = [-1]
1931
+ bad_b = [-2]
1932
+ desired = np.array([0.18719338682602, 0.73234824491364, 0.17928615186455])
1933
+
1934
+ random = Generator(MT19937(self.seed))
1935
+ beta = random.beta
1936
+ actual = beta(a * 3, b)
1937
+ assert_array_almost_equal(actual, desired, decimal=14)
1938
+ assert_raises(ValueError, beta, bad_a * 3, b)
1939
+ assert_raises(ValueError, beta, a * 3, bad_b)
1940
+
1941
+ random = Generator(MT19937(self.seed))
1942
+ actual = random.beta(a, b * 3)
1943
+ assert_array_almost_equal(actual, desired, decimal=14)
1944
+
1945
+ def test_exponential(self):
1946
+ scale = [1]
1947
+ bad_scale = [-1]
1948
+ desired = np.array([0.67245993212806, 0.21380495318094, 0.7177848928629])
1949
+
1950
+ random = Generator(MT19937(self.seed))
1951
+ actual = random.exponential(scale * 3)
1952
+ assert_array_almost_equal(actual, desired, decimal=14)
1953
+ assert_raises(ValueError, random.exponential, bad_scale * 3)
1954
+
1955
+ def test_standard_gamma(self):
1956
+ shape = [1]
1957
+ bad_shape = [-1]
1958
+ desired = np.array([0.67245993212806, 0.21380495318094, 0.7177848928629])
1959
+
1960
+ random = Generator(MT19937(self.seed))
1961
+ std_gamma = random.standard_gamma
1962
+ actual = std_gamma(shape * 3)
1963
+ assert_array_almost_equal(actual, desired, decimal=14)
1964
+ assert_raises(ValueError, std_gamma, bad_shape * 3)
1965
+
1966
+ def test_gamma(self):
1967
+ shape = [1]
1968
+ scale = [2]
1969
+ bad_shape = [-1]
1970
+ bad_scale = [-2]
1971
+ desired = np.array([1.34491986425611, 0.42760990636187, 1.4355697857258])
1972
+
1973
+ random = Generator(MT19937(self.seed))
1974
+ gamma = random.gamma
1975
+ actual = gamma(shape * 3, scale)
1976
+ assert_array_almost_equal(actual, desired, decimal=14)
1977
+ assert_raises(ValueError, gamma, bad_shape * 3, scale)
1978
+ assert_raises(ValueError, gamma, shape * 3, bad_scale)
1979
+
1980
+ random = Generator(MT19937(self.seed))
1981
+ gamma = random.gamma
1982
+ actual = gamma(shape, scale * 3)
1983
+ assert_array_almost_equal(actual, desired, decimal=14)
1984
+ assert_raises(ValueError, gamma, bad_shape, scale * 3)
1985
+ assert_raises(ValueError, gamma, shape, bad_scale * 3)
1986
+
1987
+ def test_f(self):
1988
+ dfnum = [1]
1989
+ dfden = [2]
1990
+ bad_dfnum = [-1]
1991
+ bad_dfden = [-2]
1992
+ desired = np.array([0.07765056244107, 7.72951397913186, 0.05786093891763])
1993
+
1994
+ random = Generator(MT19937(self.seed))
1995
+ f = random.f
1996
+ actual = f(dfnum * 3, dfden)
1997
+ assert_array_almost_equal(actual, desired, decimal=14)
1998
+ assert_raises(ValueError, f, bad_dfnum * 3, dfden)
1999
+ assert_raises(ValueError, f, dfnum * 3, bad_dfden)
2000
+
2001
+ random = Generator(MT19937(self.seed))
2002
+ f = random.f
2003
+ actual = f(dfnum, dfden * 3)
2004
+ assert_array_almost_equal(actual, desired, decimal=14)
2005
+ assert_raises(ValueError, f, bad_dfnum, dfden * 3)
2006
+ assert_raises(ValueError, f, dfnum, bad_dfden * 3)
2007
+
2008
+ def test_noncentral_f(self):
2009
+ dfnum = [2]
2010
+ dfden = [3]
2011
+ nonc = [4]
2012
+ bad_dfnum = [0]
2013
+ bad_dfden = [-1]
2014
+ bad_nonc = [-2]
2015
+ desired = np.array([2.02434240411421, 12.91838601070124, 1.24395160354629])
2016
+
2017
+ random = Generator(MT19937(self.seed))
2018
+ nonc_f = random.noncentral_f
2019
+ actual = nonc_f(dfnum * 3, dfden, nonc)
2020
+ assert_array_almost_equal(actual, desired, decimal=14)
2021
+ assert np.all(np.isnan(nonc_f(dfnum, dfden, [np.nan] * 3)))
2022
+
2023
+ assert_raises(ValueError, nonc_f, bad_dfnum * 3, dfden, nonc)
2024
+ assert_raises(ValueError, nonc_f, dfnum * 3, bad_dfden, nonc)
2025
+ assert_raises(ValueError, nonc_f, dfnum * 3, dfden, bad_nonc)
2026
+
2027
+ random = Generator(MT19937(self.seed))
2028
+ nonc_f = random.noncentral_f
2029
+ actual = nonc_f(dfnum, dfden * 3, nonc)
2030
+ assert_array_almost_equal(actual, desired, decimal=14)
2031
+ assert_raises(ValueError, nonc_f, bad_dfnum, dfden * 3, nonc)
2032
+ assert_raises(ValueError, nonc_f, dfnum, bad_dfden * 3, nonc)
2033
+ assert_raises(ValueError, nonc_f, dfnum, dfden * 3, bad_nonc)
2034
+
2035
+ random = Generator(MT19937(self.seed))
2036
+ nonc_f = random.noncentral_f
2037
+ actual = nonc_f(dfnum, dfden, nonc * 3)
2038
+ assert_array_almost_equal(actual, desired, decimal=14)
2039
+ assert_raises(ValueError, nonc_f, bad_dfnum, dfden, nonc * 3)
2040
+ assert_raises(ValueError, nonc_f, dfnum, bad_dfden, nonc * 3)
2041
+ assert_raises(ValueError, nonc_f, dfnum, dfden, bad_nonc * 3)
2042
+
2043
+ def test_noncentral_f_small_df(self):
2044
+ random = Generator(MT19937(self.seed))
2045
+ desired = np.array([0.04714867120827, 0.1239390327694])
2046
+ actual = random.noncentral_f(0.9, 0.9, 2, size=2)
2047
+ assert_array_almost_equal(actual, desired, decimal=14)
2048
+
2049
+ def test_chisquare(self):
2050
+ df = [1]
2051
+ bad_df = [-1]
2052
+ desired = np.array([0.05573640064251, 1.47220224353539, 2.9469379318589])
2053
+
2054
+ random = Generator(MT19937(self.seed))
2055
+ actual = random.chisquare(df * 3)
2056
+ assert_array_almost_equal(actual, desired, decimal=14)
2057
+ assert_raises(ValueError, random.chisquare, bad_df * 3)
2058
+
2059
+ def test_noncentral_chisquare(self):
2060
+ df = [1]
2061
+ nonc = [2]
2062
+ bad_df = [-1]
2063
+ bad_nonc = [-2]
2064
+ desired = np.array([0.07710766249436, 5.27829115110304, 0.630732147399])
2065
+
2066
+ random = Generator(MT19937(self.seed))
2067
+ nonc_chi = random.noncentral_chisquare
2068
+ actual = nonc_chi(df * 3, nonc)
2069
+ assert_array_almost_equal(actual, desired, decimal=14)
2070
+ assert_raises(ValueError, nonc_chi, bad_df * 3, nonc)
2071
+ assert_raises(ValueError, nonc_chi, df * 3, bad_nonc)
2072
+
2073
+ random = Generator(MT19937(self.seed))
2074
+ nonc_chi = random.noncentral_chisquare
2075
+ actual = nonc_chi(df, nonc * 3)
2076
+ assert_array_almost_equal(actual, desired, decimal=14)
2077
+ assert_raises(ValueError, nonc_chi, bad_df, nonc * 3)
2078
+ assert_raises(ValueError, nonc_chi, df, bad_nonc * 3)
2079
+
2080
+ def test_standard_t(self):
2081
+ df = [1]
2082
+ bad_df = [-1]
2083
+ desired = np.array([-1.39498829447098, -1.23058658835223, 0.17207021065983])
2084
+
2085
+ random = Generator(MT19937(self.seed))
2086
+ actual = random.standard_t(df * 3)
2087
+ assert_array_almost_equal(actual, desired, decimal=14)
2088
+ assert_raises(ValueError, random.standard_t, bad_df * 3)
2089
+
2090
+ def test_vonmises(self):
2091
+ mu = [2]
2092
+ kappa = [1]
2093
+ bad_kappa = [-1]
2094
+ desired = np.array([2.25935584988528, 2.23326261461399, -2.84152146503326])
2095
+
2096
+ random = Generator(MT19937(self.seed))
2097
+ actual = random.vonmises(mu * 3, kappa)
2098
+ assert_array_almost_equal(actual, desired, decimal=14)
2099
+ assert_raises(ValueError, random.vonmises, mu * 3, bad_kappa)
2100
+
2101
+ random = Generator(MT19937(self.seed))
2102
+ actual = random.vonmises(mu, kappa * 3)
2103
+ assert_array_almost_equal(actual, desired, decimal=14)
2104
+ assert_raises(ValueError, random.vonmises, mu, bad_kappa * 3)
2105
+
2106
+ def test_pareto(self):
2107
+ a = [1]
2108
+ bad_a = [-1]
2109
+ desired = np.array([0.95905052946317, 0.2383810889437, 1.04988745750013])
2110
+
2111
+ random = Generator(MT19937(self.seed))
2112
+ actual = random.pareto(a * 3)
2113
+ assert_array_almost_equal(actual, desired, decimal=14)
2114
+ assert_raises(ValueError, random.pareto, bad_a * 3)
2115
+
2116
+ def test_weibull(self):
2117
+ a = [1]
2118
+ bad_a = [-1]
2119
+ desired = np.array([0.67245993212806, 0.21380495318094, 0.7177848928629])
2120
+
2121
+ random = Generator(MT19937(self.seed))
2122
+ actual = random.weibull(a * 3)
2123
+ assert_array_almost_equal(actual, desired, decimal=14)
2124
+ assert_raises(ValueError, random.weibull, bad_a * 3)
2125
+
2126
+ def test_power(self):
2127
+ a = [1]
2128
+ bad_a = [-1]
2129
+ desired = np.array([0.48954864361052, 0.19249412888486, 0.51216834058807])
2130
+
2131
+ random = Generator(MT19937(self.seed))
2132
+ actual = random.power(a * 3)
2133
+ assert_array_almost_equal(actual, desired, decimal=14)
2134
+ assert_raises(ValueError, random.power, bad_a * 3)
2135
+
2136
+ def test_laplace(self):
2137
+ loc = [0]
2138
+ scale = [1]
2139
+ bad_scale = [-1]
2140
+ desired = np.array([-1.09698732625119, -0.93470271947368, 0.71592671378202])
2141
+
2142
+ random = Generator(MT19937(self.seed))
2143
+ laplace = random.laplace
2144
+ actual = laplace(loc * 3, scale)
2145
+ assert_array_almost_equal(actual, desired, decimal=14)
2146
+ assert_raises(ValueError, laplace, loc * 3, bad_scale)
2147
+
2148
+ random = Generator(MT19937(self.seed))
2149
+ laplace = random.laplace
2150
+ actual = laplace(loc, scale * 3)
2151
+ assert_array_almost_equal(actual, desired, decimal=14)
2152
+ assert_raises(ValueError, laplace, loc, bad_scale * 3)
2153
+
2154
+ def test_gumbel(self):
2155
+ loc = [0]
2156
+ scale = [1]
2157
+ bad_scale = [-1]
2158
+ desired = np.array([1.70020068231762, 1.52054354273631, -0.34293267607081])
2159
+
2160
+ random = Generator(MT19937(self.seed))
2161
+ gumbel = random.gumbel
2162
+ actual = gumbel(loc * 3, scale)
2163
+ assert_array_almost_equal(actual, desired, decimal=14)
2164
+ assert_raises(ValueError, gumbel, loc * 3, bad_scale)
2165
+
2166
+ random = Generator(MT19937(self.seed))
2167
+ gumbel = random.gumbel
2168
+ actual = gumbel(loc, scale * 3)
2169
+ assert_array_almost_equal(actual, desired, decimal=14)
2170
+ assert_raises(ValueError, gumbel, loc, bad_scale * 3)
2171
+
2172
+ def test_logistic(self):
2173
+ loc = [0]
2174
+ scale = [1]
2175
+ bad_scale = [-1]
2176
+ desired = np.array([-1.607487640433, -1.40925686003678, 1.12887112820397])
2177
+
2178
+ random = Generator(MT19937(self.seed))
2179
+ actual = random.logistic(loc * 3, scale)
2180
+ assert_array_almost_equal(actual, desired, decimal=14)
2181
+ assert_raises(ValueError, random.logistic, loc * 3, bad_scale)
2182
+
2183
+ random = Generator(MT19937(self.seed))
2184
+ actual = random.logistic(loc, scale * 3)
2185
+ assert_array_almost_equal(actual, desired, decimal=14)
2186
+ assert_raises(ValueError, random.logistic, loc, bad_scale * 3)
2187
+ assert_equal(random.logistic(1.0, 0.0), 1.0)
2188
+
2189
+ def test_lognormal(self):
2190
+ mean = [0]
2191
+ sigma = [1]
2192
+ bad_sigma = [-1]
2193
+ desired = np.array([0.67884390500697, 2.21653186290321, 1.01990310084276])
2194
+
2195
+ random = Generator(MT19937(self.seed))
2196
+ lognormal = random.lognormal
2197
+ actual = lognormal(mean * 3, sigma)
2198
+ assert_array_almost_equal(actual, desired, decimal=14)
2199
+ assert_raises(ValueError, lognormal, mean * 3, bad_sigma)
2200
+
2201
+ random = Generator(MT19937(self.seed))
2202
+ actual = random.lognormal(mean, sigma * 3)
2203
+ assert_raises(ValueError, random.lognormal, mean, bad_sigma * 3)
2204
+
2205
+ def test_rayleigh(self):
2206
+ scale = [1]
2207
+ bad_scale = [-1]
2208
+ desired = np.array(
2209
+ [1.1597068009872629,
2210
+ 0.6539188836253857,
2211
+ 1.1981526554349398]
2212
+ )
2213
+
2214
+ random = Generator(MT19937(self.seed))
2215
+ actual = random.rayleigh(scale * 3)
2216
+ assert_array_almost_equal(actual, desired, decimal=14)
2217
+ assert_raises(ValueError, random.rayleigh, bad_scale * 3)
2218
+
2219
+ def test_wald(self):
2220
+ mean = [0.5]
2221
+ scale = [1]
2222
+ bad_mean = [0]
2223
+ bad_scale = [-2]
2224
+ desired = np.array([0.38052407392905, 0.50701641508592, 0.484935249864])
2225
+
2226
+ random = Generator(MT19937(self.seed))
2227
+ actual = random.wald(mean * 3, scale)
2228
+ assert_array_almost_equal(actual, desired, decimal=14)
2229
+ assert_raises(ValueError, random.wald, bad_mean * 3, scale)
2230
+ assert_raises(ValueError, random.wald, mean * 3, bad_scale)
2231
+
2232
+ random = Generator(MT19937(self.seed))
2233
+ actual = random.wald(mean, scale * 3)
2234
+ assert_array_almost_equal(actual, desired, decimal=14)
2235
+ assert_raises(ValueError, random.wald, bad_mean, scale * 3)
2236
+ assert_raises(ValueError, random.wald, mean, bad_scale * 3)
2237
+
2238
+ def test_triangular(self):
2239
+ left = [1]
2240
+ right = [3]
2241
+ mode = [2]
2242
+ bad_left_one = [3]
2243
+ bad_mode_one = [4]
2244
+ bad_left_two, bad_mode_two = right * 2
2245
+ desired = np.array([1.57781954604754, 1.62665986867957, 2.30090130831326])
2246
+
2247
+ random = Generator(MT19937(self.seed))
2248
+ triangular = random.triangular
2249
+ actual = triangular(left * 3, mode, right)
2250
+ assert_array_almost_equal(actual, desired, decimal=14)
2251
+ assert_raises(ValueError, triangular, bad_left_one * 3, mode, right)
2252
+ assert_raises(ValueError, triangular, left * 3, bad_mode_one, right)
2253
+ assert_raises(ValueError, triangular, bad_left_two * 3, bad_mode_two,
2254
+ right)
2255
+
2256
+ random = Generator(MT19937(self.seed))
2257
+ triangular = random.triangular
2258
+ actual = triangular(left, mode * 3, right)
2259
+ assert_array_almost_equal(actual, desired, decimal=14)
2260
+ assert_raises(ValueError, triangular, bad_left_one, mode * 3, right)
2261
+ assert_raises(ValueError, triangular, left, bad_mode_one * 3, right)
2262
+ assert_raises(ValueError, triangular, bad_left_two, bad_mode_two * 3,
2263
+ right)
2264
+
2265
+ random = Generator(MT19937(self.seed))
2266
+ triangular = random.triangular
2267
+ actual = triangular(left, mode, right * 3)
2268
+ assert_array_almost_equal(actual, desired, decimal=14)
2269
+ assert_raises(ValueError, triangular, bad_left_one, mode, right * 3)
2270
+ assert_raises(ValueError, triangular, left, bad_mode_one, right * 3)
2271
+ assert_raises(ValueError, triangular, bad_left_two, bad_mode_two,
2272
+ right * 3)
2273
+
2274
+ assert_raises(ValueError, triangular, 10., 0., 20.)
2275
+ assert_raises(ValueError, triangular, 10., 25., 20.)
2276
+ assert_raises(ValueError, triangular, 10., 10., 10.)
2277
+
2278
+ def test_binomial(self):
2279
+ n = [1]
2280
+ p = [0.5]
2281
+ bad_n = [-1]
2282
+ bad_p_one = [-1]
2283
+ bad_p_two = [1.5]
2284
+ desired = np.array([0, 0, 1])
2285
+
2286
+ random = Generator(MT19937(self.seed))
2287
+ binom = random.binomial
2288
+ actual = binom(n * 3, p)
2289
+ assert_array_equal(actual, desired)
2290
+ assert_raises(ValueError, binom, bad_n * 3, p)
2291
+ assert_raises(ValueError, binom, n * 3, bad_p_one)
2292
+ assert_raises(ValueError, binom, n * 3, bad_p_two)
2293
+
2294
+ random = Generator(MT19937(self.seed))
2295
+ actual = random.binomial(n, p * 3)
2296
+ assert_array_equal(actual, desired)
2297
+ assert_raises(ValueError, binom, bad_n, p * 3)
2298
+ assert_raises(ValueError, binom, n, bad_p_one * 3)
2299
+ assert_raises(ValueError, binom, n, bad_p_two * 3)
2300
+
2301
+ def test_negative_binomial(self):
2302
+ n = [1]
2303
+ p = [0.5]
2304
+ bad_n = [-1]
2305
+ bad_p_one = [-1]
2306
+ bad_p_two = [1.5]
2307
+ desired = np.array([0, 2, 1], dtype=np.int64)
2308
+
2309
+ random = Generator(MT19937(self.seed))
2310
+ neg_binom = random.negative_binomial
2311
+ actual = neg_binom(n * 3, p)
2312
+ assert_array_equal(actual, desired)
2313
+ assert_raises(ValueError, neg_binom, bad_n * 3, p)
2314
+ assert_raises(ValueError, neg_binom, n * 3, bad_p_one)
2315
+ assert_raises(ValueError, neg_binom, n * 3, bad_p_two)
2316
+
2317
+ random = Generator(MT19937(self.seed))
2318
+ neg_binom = random.negative_binomial
2319
+ actual = neg_binom(n, p * 3)
2320
+ assert_array_equal(actual, desired)
2321
+ assert_raises(ValueError, neg_binom, bad_n, p * 3)
2322
+ assert_raises(ValueError, neg_binom, n, bad_p_one * 3)
2323
+ assert_raises(ValueError, neg_binom, n, bad_p_two * 3)
2324
+
2325
+ def test_poisson(self):
2326
+
2327
+ lam = [1]
2328
+ bad_lam_one = [-1]
2329
+ desired = np.array([0, 0, 3])
2330
+
2331
+ random = Generator(MT19937(self.seed))
2332
+ max_lam = random._poisson_lam_max
2333
+ bad_lam_two = [max_lam * 2]
2334
+ poisson = random.poisson
2335
+ actual = poisson(lam * 3)
2336
+ assert_array_equal(actual, desired)
2337
+ assert_raises(ValueError, poisson, bad_lam_one * 3)
2338
+ assert_raises(ValueError, poisson, bad_lam_two * 3)
2339
+
2340
+ def test_zipf(self):
2341
+ a = [2]
2342
+ bad_a = [0]
2343
+ desired = np.array([1, 8, 1])
2344
+
2345
+ random = Generator(MT19937(self.seed))
2346
+ zipf = random.zipf
2347
+ actual = zipf(a * 3)
2348
+ assert_array_equal(actual, desired)
2349
+ assert_raises(ValueError, zipf, bad_a * 3)
2350
+ with np.errstate(invalid='ignore'):
2351
+ assert_raises(ValueError, zipf, np.nan)
2352
+ assert_raises(ValueError, zipf, [0, 0, np.nan])
2353
+
2354
+ def test_geometric(self):
2355
+ p = [0.5]
2356
+ bad_p_one = [-1]
2357
+ bad_p_two = [1.5]
2358
+ desired = np.array([1, 1, 3])
2359
+
2360
+ random = Generator(MT19937(self.seed))
2361
+ geometric = random.geometric
2362
+ actual = geometric(p * 3)
2363
+ assert_array_equal(actual, desired)
2364
+ assert_raises(ValueError, geometric, bad_p_one * 3)
2365
+ assert_raises(ValueError, geometric, bad_p_two * 3)
2366
+
2367
+ def test_hypergeometric(self):
2368
+ ngood = [1]
2369
+ nbad = [2]
2370
+ nsample = [2]
2371
+ bad_ngood = [-1]
2372
+ bad_nbad = [-2]
2373
+ bad_nsample_one = [-1]
2374
+ bad_nsample_two = [4]
2375
+ desired = np.array([0, 0, 1])
2376
+
2377
+ random = Generator(MT19937(self.seed))
2378
+ actual = random.hypergeometric(ngood * 3, nbad, nsample)
2379
+ assert_array_equal(actual, desired)
2380
+ assert_raises(ValueError, random.hypergeometric, bad_ngood * 3, nbad, nsample)
2381
+ assert_raises(ValueError, random.hypergeometric, ngood * 3, bad_nbad, nsample)
2382
+ assert_raises(ValueError, random.hypergeometric, ngood * 3, nbad, bad_nsample_one) # noqa: E501
2383
+ assert_raises(ValueError, random.hypergeometric, ngood * 3, nbad, bad_nsample_two) # noqa: E501
2384
+
2385
+ random = Generator(MT19937(self.seed))
2386
+ actual = random.hypergeometric(ngood, nbad * 3, nsample)
2387
+ assert_array_equal(actual, desired)
2388
+ assert_raises(ValueError, random.hypergeometric, bad_ngood, nbad * 3, nsample)
2389
+ assert_raises(ValueError, random.hypergeometric, ngood, bad_nbad * 3, nsample)
2390
+ assert_raises(ValueError, random.hypergeometric, ngood, nbad * 3, bad_nsample_one) # noqa: E501
2391
+ assert_raises(ValueError, random.hypergeometric, ngood, nbad * 3, bad_nsample_two) # noqa: E501
2392
+
2393
+ random = Generator(MT19937(self.seed))
2394
+ hypergeom = random.hypergeometric
2395
+ actual = hypergeom(ngood, nbad, nsample * 3)
2396
+ assert_array_equal(actual, desired)
2397
+ assert_raises(ValueError, hypergeom, bad_ngood, nbad, nsample * 3)
2398
+ assert_raises(ValueError, hypergeom, ngood, bad_nbad, nsample * 3)
2399
+ assert_raises(ValueError, hypergeom, ngood, nbad, bad_nsample_one * 3)
2400
+ assert_raises(ValueError, hypergeom, ngood, nbad, bad_nsample_two * 3)
2401
+
2402
+ assert_raises(ValueError, hypergeom, -1, 10, 20)
2403
+ assert_raises(ValueError, hypergeom, 10, -1, 20)
2404
+ assert_raises(ValueError, hypergeom, 10, 10, -1)
2405
+ assert_raises(ValueError, hypergeom, 10, 10, 25)
2406
+
2407
+ # ValueError for arguments that are too big.
2408
+ assert_raises(ValueError, hypergeom, 2**30, 10, 20)
2409
+ assert_raises(ValueError, hypergeom, 999, 2**31, 50)
2410
+ assert_raises(ValueError, hypergeom, 999, [2**29, 2**30], 1000)
2411
+
2412
+ def test_logseries(self):
2413
+ p = [0.5]
2414
+ bad_p_one = [2]
2415
+ bad_p_two = [-1]
2416
+ desired = np.array([1, 1, 1])
2417
+
2418
+ random = Generator(MT19937(self.seed))
2419
+ logseries = random.logseries
2420
+ actual = logseries(p * 3)
2421
+ assert_array_equal(actual, desired)
2422
+ assert_raises(ValueError, logseries, bad_p_one * 3)
2423
+ assert_raises(ValueError, logseries, bad_p_two * 3)
2424
+
2425
+ def test_multinomial(self):
2426
+ random = Generator(MT19937(self.seed))
2427
+ actual = random.multinomial([5, 20], [1 / 6.] * 6, size=(3, 2))
2428
+ desired = np.array([[[0, 0, 2, 1, 2, 0],
2429
+ [2, 3, 6, 4, 2, 3]],
2430
+ [[1, 0, 1, 0, 2, 1],
2431
+ [7, 2, 2, 1, 4, 4]],
2432
+ [[0, 2, 0, 1, 2, 0],
2433
+ [3, 2, 3, 3, 4, 5]]], dtype=np.int64)
2434
+ assert_array_equal(actual, desired)
2435
+
2436
+ random = Generator(MT19937(self.seed))
2437
+ actual = random.multinomial([5, 20], [1 / 6.] * 6)
2438
+ desired = np.array([[0, 0, 2, 1, 2, 0],
2439
+ [2, 3, 6, 4, 2, 3]], dtype=np.int64)
2440
+ assert_array_equal(actual, desired)
2441
+
2442
+ random = Generator(MT19937(self.seed))
2443
+ actual = random.multinomial([5, 20], [[1 / 6.] * 6] * 2)
2444
+ desired = np.array([[0, 0, 2, 1, 2, 0],
2445
+ [2, 3, 6, 4, 2, 3]], dtype=np.int64)
2446
+ assert_array_equal(actual, desired)
2447
+
2448
+ random = Generator(MT19937(self.seed))
2449
+ actual = random.multinomial([[5], [20]], [[1 / 6.] * 6] * 2)
2450
+ desired = np.array([[[0, 0, 2, 1, 2, 0],
2451
+ [0, 0, 2, 1, 1, 1]],
2452
+ [[4, 2, 3, 3, 5, 3],
2453
+ [7, 2, 2, 1, 4, 4]]], dtype=np.int64)
2454
+ assert_array_equal(actual, desired)
2455
+
2456
+ @pytest.mark.parametrize("n", [10,
2457
+ np.array([10, 10]),
2458
+ np.array([[[10]], [[10]]])
2459
+ ]
2460
+ )
2461
+ def test_multinomial_pval_broadcast(self, n):
2462
+ random = Generator(MT19937(self.seed))
2463
+ pvals = np.array([1 / 4] * 4)
2464
+ actual = random.multinomial(n, pvals)
2465
+ n_shape = () if isinstance(n, int) else n.shape
2466
+ expected_shape = n_shape + (4,)
2467
+ assert actual.shape == expected_shape
2468
+ pvals = np.vstack([pvals, pvals])
2469
+ actual = random.multinomial(n, pvals)
2470
+ expected_shape = np.broadcast_shapes(n_shape, pvals.shape[:-1]) + (4,)
2471
+ assert actual.shape == expected_shape
2472
+
2473
+ pvals = np.vstack([[pvals], [pvals]])
2474
+ actual = random.multinomial(n, pvals)
2475
+ expected_shape = np.broadcast_shapes(n_shape, pvals.shape[:-1])
2476
+ assert actual.shape == expected_shape + (4,)
2477
+ actual = random.multinomial(n, pvals, size=(3, 2) + expected_shape)
2478
+ assert actual.shape == (3, 2) + expected_shape + (4,)
2479
+
2480
+ with pytest.raises(ValueError):
2481
+ # Ensure that size is not broadcast
2482
+ actual = random.multinomial(n, pvals, size=(1,) * 6)
2483
+
2484
+ def test_invalid_pvals_broadcast(self):
2485
+ random = Generator(MT19937(self.seed))
2486
+ pvals = [[1 / 6] * 6, [1 / 4] * 6]
2487
+ assert_raises(ValueError, random.multinomial, 1, pvals)
2488
+ assert_raises(ValueError, random.multinomial, 6, 0.5)
2489
+
2490
+ def test_empty_outputs(self):
2491
+ random = Generator(MT19937(self.seed))
2492
+ actual = random.multinomial(np.empty((10, 0, 6), "i8"), [1 / 6] * 6)
2493
+ assert actual.shape == (10, 0, 6, 6)
2494
+ actual = random.multinomial(12, np.empty((10, 0, 10)))
2495
+ assert actual.shape == (10, 0, 10)
2496
+ actual = random.multinomial(np.empty((3, 0, 7), "i8"),
2497
+ np.empty((3, 0, 7, 4)))
2498
+ assert actual.shape == (3, 0, 7, 4)
2499
+
2500
+
2501
+ @pytest.mark.skipif(IS_WASM, reason="can't start thread")
2502
+ class TestThread:
2503
+ # make sure each state produces the same sequence even in threads
2504
+ def setup_method(self):
2505
+ self.seeds = range(4)
2506
+
2507
+ def check_function(self, function, sz):
2508
+ from threading import Thread
2509
+
2510
+ out1 = np.empty((len(self.seeds),) + sz)
2511
+ out2 = np.empty((len(self.seeds),) + sz)
2512
+
2513
+ # threaded generation
2514
+ t = [Thread(target=function, args=(Generator(MT19937(s)), o))
2515
+ for s, o in zip(self.seeds, out1)]
2516
+ [x.start() for x in t]
2517
+ [x.join() for x in t]
2518
+
2519
+ # the same serial
2520
+ for s, o in zip(self.seeds, out2):
2521
+ function(Generator(MT19937(s)), o)
2522
+
2523
+ # these platforms change x87 fpu precision mode in threads
2524
+ if np.intp().dtype.itemsize == 4 and sys.platform == "win32":
2525
+ assert_array_almost_equal(out1, out2)
2526
+ else:
2527
+ assert_array_equal(out1, out2)
2528
+
2529
+ def test_normal(self):
2530
+ def gen_random(state, out):
2531
+ out[...] = state.normal(size=10000)
2532
+
2533
+ self.check_function(gen_random, sz=(10000,))
2534
+
2535
+ def test_exp(self):
2536
+ def gen_random(state, out):
2537
+ out[...] = state.exponential(scale=np.ones((100, 1000)))
2538
+
2539
+ self.check_function(gen_random, sz=(100, 1000))
2540
+
2541
+ def test_multinomial(self):
2542
+ def gen_random(state, out):
2543
+ out[...] = state.multinomial(10, [1 / 6.] * 6, size=10000)
2544
+
2545
+ self.check_function(gen_random, sz=(10000, 6))
2546
+
2547
+
2548
+ # See Issue #4263
2549
+ class TestSingleEltArrayInput:
2550
+ def setup_method(self):
2551
+ self.argOne = np.array([2])
2552
+ self.argTwo = np.array([3])
2553
+ self.argThree = np.array([4])
2554
+ self.tgtShape = (1,)
2555
+
2556
+ def test_one_arg_funcs(self):
2557
+ funcs = (random.exponential, random.standard_gamma,
2558
+ random.chisquare, random.standard_t,
2559
+ random.pareto, random.weibull,
2560
+ random.power, random.rayleigh,
2561
+ random.poisson, random.zipf,
2562
+ random.geometric, random.logseries)
2563
+
2564
+ probfuncs = (random.geometric, random.logseries)
2565
+
2566
+ for func in funcs:
2567
+ if func in probfuncs: # p < 1.0
2568
+ out = func(np.array([0.5]))
2569
+
2570
+ else:
2571
+ out = func(self.argOne)
2572
+
2573
+ assert_equal(out.shape, self.tgtShape)
2574
+
2575
+ def test_two_arg_funcs(self):
2576
+ funcs = (random.uniform, random.normal,
2577
+ random.beta, random.gamma,
2578
+ random.f, random.noncentral_chisquare,
2579
+ random.vonmises, random.laplace,
2580
+ random.gumbel, random.logistic,
2581
+ random.lognormal, random.wald,
2582
+ random.binomial, random.negative_binomial)
2583
+
2584
+ probfuncs = (random.binomial, random.negative_binomial)
2585
+
2586
+ for func in funcs:
2587
+ if func in probfuncs: # p <= 1
2588
+ argTwo = np.array([0.5])
2589
+
2590
+ else:
2591
+ argTwo = self.argTwo
2592
+
2593
+ out = func(self.argOne, argTwo)
2594
+ assert_equal(out.shape, self.tgtShape)
2595
+
2596
+ out = func(self.argOne[0], argTwo)
2597
+ assert_equal(out.shape, self.tgtShape)
2598
+
2599
+ out = func(self.argOne, argTwo[0])
2600
+ assert_equal(out.shape, self.tgtShape)
2601
+
2602
+ def test_integers(self, endpoint):
2603
+ itype = [np.bool, np.int8, np.uint8, np.int16, np.uint16,
2604
+ np.int32, np.uint32, np.int64, np.uint64]
2605
+ func = random.integers
2606
+ high = np.array([1])
2607
+ low = np.array([0])
2608
+
2609
+ for dt in itype:
2610
+ out = func(low, high, endpoint=endpoint, dtype=dt)
2611
+ assert_equal(out.shape, self.tgtShape)
2612
+
2613
+ out = func(low[0], high, endpoint=endpoint, dtype=dt)
2614
+ assert_equal(out.shape, self.tgtShape)
2615
+
2616
+ out = func(low, high[0], endpoint=endpoint, dtype=dt)
2617
+ assert_equal(out.shape, self.tgtShape)
2618
+
2619
+ def test_three_arg_funcs(self):
2620
+ funcs = [random.noncentral_f, random.triangular,
2621
+ random.hypergeometric]
2622
+
2623
+ for func in funcs:
2624
+ out = func(self.argOne, self.argTwo, self.argThree)
2625
+ assert_equal(out.shape, self.tgtShape)
2626
+
2627
+ out = func(self.argOne[0], self.argTwo, self.argThree)
2628
+ assert_equal(out.shape, self.tgtShape)
2629
+
2630
+ out = func(self.argOne, self.argTwo[0], self.argThree)
2631
+ assert_equal(out.shape, self.tgtShape)
2632
+
2633
+
2634
+ @pytest.mark.parametrize("config", JUMP_TEST_DATA)
2635
+ def test_jumped(config):
2636
+ # Each config contains the initial seed, a number of raw steps
2637
+ # the sha256 hashes of the initial and the final states' keys and
2638
+ # the position of the initial and the final state.
2639
+ # These were produced using the original C implementation.
2640
+ seed = config["seed"]
2641
+ steps = config["steps"]
2642
+
2643
+ mt19937 = MT19937(seed)
2644
+ # Burn step
2645
+ mt19937.random_raw(steps)
2646
+ key = mt19937.state["state"]["key"]
2647
+ if sys.byteorder == 'big':
2648
+ key = key.byteswap()
2649
+ sha256 = hashlib.sha256(key)
2650
+ assert mt19937.state["state"]["pos"] == config["initial"]["pos"]
2651
+ assert sha256.hexdigest() == config["initial"]["key_sha256"]
2652
+
2653
+ jumped = mt19937.jumped()
2654
+ key = jumped.state["state"]["key"]
2655
+ if sys.byteorder == 'big':
2656
+ key = key.byteswap()
2657
+ sha256 = hashlib.sha256(key)
2658
+ assert jumped.state["state"]["pos"] == config["jumped"]["pos"]
2659
+ assert sha256.hexdigest() == config["jumped"]["key_sha256"]
2660
+
2661
+
2662
+ def test_broadcast_size_error():
2663
+ mu = np.ones(3)
2664
+ sigma = np.ones((4, 3))
2665
+ size = (10, 4, 2)
2666
+ assert random.normal(mu, sigma, size=(5, 4, 3)).shape == (5, 4, 3)
2667
+ with pytest.raises(ValueError):
2668
+ random.normal(mu, sigma, size=size)
2669
+ with pytest.raises(ValueError):
2670
+ random.normal(mu, sigma, size=(1, 3))
2671
+ with pytest.raises(ValueError):
2672
+ random.normal(mu, sigma, size=(4, 1, 1))
2673
+ # 1 arg
2674
+ shape = np.ones((4, 3))
2675
+ with pytest.raises(ValueError):
2676
+ random.standard_gamma(shape, size=size)
2677
+ with pytest.raises(ValueError):
2678
+ random.standard_gamma(shape, size=(3,))
2679
+ with pytest.raises(ValueError):
2680
+ random.standard_gamma(shape, size=3)
2681
+ # Check out
2682
+ out = np.empty(size)
2683
+ with pytest.raises(ValueError):
2684
+ random.standard_gamma(shape, out=out)
2685
+
2686
+ # 2 arg
2687
+ with pytest.raises(ValueError):
2688
+ random.binomial(1, [0.3, 0.7], size=(2, 1))
2689
+ with pytest.raises(ValueError):
2690
+ random.binomial([1, 2], 0.3, size=(2, 1))
2691
+ with pytest.raises(ValueError):
2692
+ random.binomial([1, 2], [0.3, 0.7], size=(2, 1))
2693
+ with pytest.raises(ValueError):
2694
+ random.multinomial([2, 2], [.3, .7], size=(2, 1))
2695
+
2696
+ # 3 arg
2697
+ a = random.chisquare(5, size=3)
2698
+ b = random.chisquare(5, size=(4, 3))
2699
+ c = random.chisquare(5, size=(5, 4, 3))
2700
+ assert random.noncentral_f(a, b, c).shape == (5, 4, 3)
2701
+ with pytest.raises(ValueError, match=r"Output size \(6, 5, 1, 1\) is"):
2702
+ random.noncentral_f(a, b, c, size=(6, 5, 1, 1))
2703
+
2704
+
2705
+ def test_broadcast_size_scalar():
2706
+ mu = np.ones(3)
2707
+ sigma = np.ones(3)
2708
+ random.normal(mu, sigma, size=3)
2709
+ with pytest.raises(ValueError):
2710
+ random.normal(mu, sigma, size=2)
2711
+
2712
+
2713
+ def test_ragged_shuffle():
2714
+ # GH 18142
2715
+ seq = [[], [], 1]
2716
+ gen = Generator(MT19937(0))
2717
+ assert_no_warnings(gen.shuffle, seq)
2718
+ assert seq == [1, [], []]
2719
+
2720
+
2721
+ @pytest.mark.parametrize("high", [-2, [-2]])
2722
+ @pytest.mark.parametrize("endpoint", [True, False])
2723
+ def test_single_arg_integer_exception(high, endpoint):
2724
+ # GH 14333
2725
+ gen = Generator(MT19937(0))
2726
+ msg = 'high < 0' if endpoint else 'high <= 0'
2727
+ with pytest.raises(ValueError, match=msg):
2728
+ gen.integers(high, endpoint=endpoint)
2729
+ msg = 'low > high' if endpoint else 'low >= high'
2730
+ with pytest.raises(ValueError, match=msg):
2731
+ gen.integers(-1, high, endpoint=endpoint)
2732
+ with pytest.raises(ValueError, match=msg):
2733
+ gen.integers([-1], high, endpoint=endpoint)
2734
+
2735
+
2736
+ @pytest.mark.parametrize("dtype", ["f4", "f8"])
2737
+ def test_c_contig_req_out(dtype):
2738
+ # GH 18704
2739
+ out = np.empty((2, 3), order="F", dtype=dtype)
2740
+ shape = [1, 2, 3]
2741
+ with pytest.raises(ValueError, match="Supplied output array"):
2742
+ random.standard_gamma(shape, out=out, dtype=dtype)
2743
+ with pytest.raises(ValueError, match="Supplied output array"):
2744
+ random.standard_gamma(shape, out=out, size=out.shape, dtype=dtype)
2745
+
2746
+
2747
+ @pytest.mark.parametrize("dtype", ["f4", "f8"])
2748
+ @pytest.mark.parametrize("order", ["F", "C"])
2749
+ @pytest.mark.parametrize("dist", [random.standard_normal, random.random])
2750
+ def test_contig_req_out(dist, order, dtype):
2751
+ # GH 18704
2752
+ out = np.empty((2, 3), dtype=dtype, order=order)
2753
+ variates = dist(out=out, dtype=dtype)
2754
+ assert variates is out
2755
+ variates = dist(out=out, dtype=dtype, size=out.shape)
2756
+ assert variates is out
2757
+
2758
+
2759
+ def test_generator_ctor_old_style_pickle():
2760
+ rg = np.random.Generator(np.random.PCG64DXSM(0))
2761
+ rg.standard_normal(1)
2762
+ # Directly call reduce which is used in pickling
2763
+ ctor, (bit_gen, ), _ = rg.__reduce__()
2764
+ # Simulate unpickling an old pickle that only has the name
2765
+ assert bit_gen.__class__.__name__ == "PCG64DXSM"
2766
+ print(ctor)
2767
+ b = ctor(*("PCG64DXSM",))
2768
+ print(b)
2769
+ b.bit_generator.state = bit_gen.state
2770
+ state_b = b.bit_generator.state
2771
+ assert bit_gen.state == state_b
2772
+
2773
+
2774
+ def test_pickle_preserves_seed_sequence():
2775
+ # GH 26234
2776
+ # Add explicit test that bit generators preserve seed sequences
2777
+ import pickle
2778
+
2779
+ rg = np.random.Generator(np.random.PCG64DXSM(20240411))
2780
+ ss = rg.bit_generator.seed_seq
2781
+ rg_plk = pickle.loads(pickle.dumps(rg))
2782
+ ss_plk = rg_plk.bit_generator.seed_seq
2783
+ assert_equal(ss.state, ss_plk.state)
2784
+ assert_equal(ss.pool, ss_plk.pool)
2785
+
2786
+ rg.bit_generator.seed_seq.spawn(10)
2787
+ rg_plk = pickle.loads(pickle.dumps(rg))
2788
+ ss_plk = rg_plk.bit_generator.seed_seq
2789
+ assert_equal(ss.state, ss_plk.state)
2790
+
2791
+
2792
+ @pytest.mark.parametrize("version", [121, 126])
2793
+ def test_legacy_pickle(version):
2794
+ # Pickling format was changes in 1.22.x and in 2.0.x
2795
+ import gzip
2796
+ import pickle
2797
+
2798
+ base_path = os.path.split(os.path.abspath(__file__))[0]
2799
+ pkl_file = os.path.join(
2800
+ base_path, "data", f"generator_pcg64_np{version}.pkl.gz"
2801
+ )
2802
+ with gzip.open(pkl_file) as gz:
2803
+ rg = pickle.load(gz)
2804
+ state = rg.bit_generator.state['state']
2805
+
2806
+ assert isinstance(rg, Generator)
2807
+ assert isinstance(rg.bit_generator, np.random.PCG64)
2808
+ assert state['state'] == 35399562948360463058890781895381311971
2809
+ assert state['inc'] == 87136372517582989555478159403783844777