rapydscript-ns 0.8.3 → 0.8.4

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.
Files changed (116) hide show
  1. package/.agignore +1 -1
  2. package/.github/workflows/ci.yml +38 -38
  3. package/=template.pyj +5 -5
  4. package/CHANGELOG.md +8 -0
  5. package/HACKING.md +103 -103
  6. package/LICENSE +24 -24
  7. package/PYTHON_DIFFERENCES_REPORT.md +2 -2
  8. package/PYTHON_FEATURE_COVERAGE.md +13 -13
  9. package/README.md +670 -6
  10. package/TODO.md +5 -6
  11. package/add-toc-to-readme +2 -2
  12. package/bin/export +75 -75
  13. package/bin/rapydscript +70 -70
  14. package/bin/web-repl-export +102 -102
  15. package/build +2 -2
  16. package/language-service/index.js +155 -6
  17. package/package.json +1 -1
  18. package/publish.py +37 -37
  19. package/release/baselib-plain-pretty.js +2006 -229
  20. package/release/baselib-plain-ugly.js +70 -3
  21. package/release/compiler.js +11554 -3870
  22. package/release/signatures.json +31 -29
  23. package/session.vim +4 -4
  24. package/setup.cfg +2 -2
  25. package/src/ast.pyj +93 -1
  26. package/src/baselib-builtins.pyj +22 -1
  27. package/src/baselib-containers.pyj +99 -0
  28. package/src/baselib-errors.pyj +44 -0
  29. package/src/baselib-internal.pyj +94 -4
  30. package/src/baselib-itertools.pyj +97 -97
  31. package/src/baselib-str.pyj +24 -0
  32. package/src/compiler.pyj +36 -36
  33. package/src/errors.pyj +30 -30
  34. package/src/lib/aes.pyj +646 -646
  35. package/src/lib/copy.pyj +120 -0
  36. package/src/lib/elementmaker.pyj +83 -83
  37. package/src/lib/encodings.pyj +126 -126
  38. package/src/lib/gettext.pyj +569 -569
  39. package/src/lib/itertools.pyj +580 -580
  40. package/src/lib/math.pyj +193 -193
  41. package/src/lib/operator.pyj +11 -11
  42. package/src/lib/pythonize.pyj +20 -20
  43. package/src/lib/random.pyj +118 -118
  44. package/src/lib/re.pyj +470 -470
  45. package/src/lib/react.pyj +74 -0
  46. package/src/lib/traceback.pyj +63 -63
  47. package/src/lib/uuid.pyj +77 -77
  48. package/src/monaco-language-service/builtins.js +5 -0
  49. package/src/monaco-language-service/diagnostics.js +25 -3
  50. package/src/monaco-language-service/dts.js +550 -550
  51. package/src/output/classes.pyj +108 -8
  52. package/src/output/codegen.pyj +16 -2
  53. package/src/output/comments.pyj +45 -45
  54. package/src/output/exceptions.pyj +201 -105
  55. package/src/output/functions.pyj +9 -0
  56. package/src/output/jsx.pyj +164 -0
  57. package/src/output/literals.pyj +28 -2
  58. package/src/output/modules.pyj +1 -1
  59. package/src/output/operators.pyj +8 -2
  60. package/src/output/statements.pyj +2 -2
  61. package/src/output/stream.pyj +1 -0
  62. package/src/output/treeshake.pyj +182 -182
  63. package/src/output/utils.pyj +72 -72
  64. package/src/parse.pyj +417 -113
  65. package/src/string_interpolation.pyj +72 -72
  66. package/src/tokenizer.pyj +29 -0
  67. package/src/unicode_aliases.pyj +576 -576
  68. package/src/utils.pyj +192 -192
  69. package/test/_import_one.pyj +37 -37
  70. package/test/_import_two/__init__.pyj +11 -11
  71. package/test/_import_two/level2/deep.pyj +4 -4
  72. package/test/_import_two/other.pyj +6 -6
  73. package/test/_import_two/sub.pyj +13 -13
  74. package/test/aes_vectors.pyj +421 -421
  75. package/test/annotations.pyj +80 -80
  76. package/test/decorators.pyj +77 -77
  77. package/test/docstrings.pyj +39 -39
  78. package/test/elementmaker_test.pyj +45 -45
  79. package/test/functions.pyj +151 -151
  80. package/test/generators.pyj +41 -41
  81. package/test/generic.pyj +370 -370
  82. package/test/imports.pyj +72 -72
  83. package/test/internationalization.pyj +73 -73
  84. package/test/lint.pyj +164 -164
  85. package/test/loops.pyj +85 -85
  86. package/test/numpy.pyj +734 -734
  87. package/test/omit_function_metadata.pyj +20 -20
  88. package/test/python_features.pyj +19 -6
  89. package/test/regexp.pyj +55 -55
  90. package/test/repl.pyj +121 -121
  91. package/test/scoped_flags.pyj +76 -76
  92. package/test/unit/index.js +2177 -64
  93. package/test/unit/language-service-dts.js +543 -543
  94. package/test/unit/language-service-hover.js +455 -455
  95. package/test/unit/language-service.js +590 -4
  96. package/test/unit/web-repl.js +303 -0
  97. package/tools/cli.js +547 -547
  98. package/tools/compile.js +219 -219
  99. package/tools/completer.js +131 -131
  100. package/tools/embedded_compiler.js +251 -251
  101. package/tools/gettext.js +185 -185
  102. package/tools/ini.js +65 -65
  103. package/tools/msgfmt.js +187 -187
  104. package/tools/repl.js +223 -223
  105. package/tools/test.js +118 -118
  106. package/tools/utils.js +128 -128
  107. package/tools/web_repl.js +95 -95
  108. package/try +41 -41
  109. package/web-repl/env.js +196 -74
  110. package/web-repl/index.html +163 -163
  111. package/web-repl/main.js +252 -254
  112. package/web-repl/prism.css +139 -139
  113. package/web-repl/prism.js +113 -113
  114. package/web-repl/rapydscript.js +224 -102
  115. package/web-repl/sha1.js +25 -25
  116. package/hack_demo.pyj +0 -112
package/test/numpy.pyj CHANGED
@@ -1,734 +1,734 @@
1
- # globals: assrt
2
- # vim:fileencoding=utf-8
3
- import numpy as np
4
-
5
- eq = assrt.equal
6
- de = assrt.deepEqual
7
- ok = assrt.ok
8
-
9
- def close(a, b, tol=1e-9):
10
- return Math.abs(a - b) < tol
11
-
12
- # -------------------------------------------------------
13
- # Array creation
14
- # -------------------------------------------------------
15
- z = np.zeros([3])
16
- ok(isinstance(z, np.ndarray))
17
- eq(z.shape[0], 3)
18
- eq(z[0], 0)
19
- eq(z[1], 0)
20
- eq(z.size, 3)
21
- eq(z.ndim, 1)
22
-
23
- o = np.ones([2, 3])
24
- ok(isinstance(o, np.ndarray))
25
- eq(o.shape[0], 2)
26
- eq(o.shape[1], 3)
27
- eq(o[0][0], 1)
28
- eq(o[1][2], 1)
29
- eq(o.ndim, 2)
30
-
31
- a = np.arange(5)
32
- eq(a[0], 0)
33
- eq(a[1], 1)
34
- eq(a[4], 4)
35
- eq(a.size, 5)
36
-
37
- a = np.arange(1, 6)
38
- eq(a[0], 1)
39
- eq(a[4], 5)
40
-
41
- a = np.arange(0, 10, 2)
42
- eq(a[0], 0)
43
- eq(a[1], 2)
44
- eq(a[2], 4)
45
- eq(a.size, 5)
46
-
47
- ls = np.linspace(0, 1, 5)
48
- eq(ls.size, 5)
49
- ok(close(ls[0], 0.0))
50
- ok(close(ls[4], 1.0))
51
- ok(close(ls[2], 0.5))
52
-
53
- e = np.eye(3)
54
- eq(e.shape[0], 3)
55
- eq(e.shape[1], 3)
56
- eq(e[0][0], 1)
57
- eq(e[1][1], 1)
58
- eq(e[2][2], 1)
59
- eq(e[0][1], 0)
60
- eq(e[1][0], 0)
61
-
62
- e2 = np.eye(3, k=1)
63
- eq(e2[0][1], 1)
64
- eq(e2[1][2], 1)
65
- eq(e2[0][0], 0)
66
-
67
- f = np.full([2, 3], 7.0)
68
- eq(f[0][0], 7)
69
- eq(f[1][2], 7)
70
-
71
- a = np.array([1.0, 2.0, 3.0])
72
- eq(a[0], 1)
73
- eq(a[2], 3)
74
-
75
- a2d = np.array([[1.0, 2.0], [3.0, 4.0]])
76
- eq(a2d[0][0], 1)
77
- eq(a2d[0][1], 2)
78
- eq(a2d[1][0], 3)
79
- eq(a2d[1][1], 4)
80
-
81
- zl = np.zeros_like(a2d)
82
- eq(zl.shape[0], 2)
83
- eq(zl.shape[1], 2)
84
- eq(zl[0][0], 0)
85
-
86
- ol = np.ones_like(a)
87
- eq(ol.size, 3)
88
- eq(ol[0], 1)
89
-
90
- # -------------------------------------------------------
91
- # Element access and item/itemset
92
- # -------------------------------------------------------
93
- arr1d = np.array([10.0, 20.0, 30.0])
94
- eq(arr1d.item(0), 10)
95
- eq(arr1d.item(2), 30)
96
-
97
- arr2d = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
98
- eq(arr2d.item(0, 0), 1)
99
- eq(arr2d.item(0, 2), 3)
100
- eq(arr2d.item(1, 1), 5)
101
-
102
- # tolist
103
- tl = np.array([1.0, 2.0, 3.0]).tolist()
104
- de(tl, [1, 2, 3])
105
-
106
- # -------------------------------------------------------
107
- # Math operations
108
- # -------------------------------------------------------
109
- a = np.array([1.0, 4.0, 9.0])
110
- s = np.sqrt(a)
111
- ok(close(s[0], 1.0))
112
- ok(close(s[1], 2.0))
113
- ok(close(s[2], 3.0))
114
-
115
- a = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
116
- ok(close(a.sum(), 15.0))
117
- ok(close(a.mean(), 3.0))
118
- ok(close(np.sum(a), 15.0))
119
- ok(close(np.mean(a), 3.0))
120
-
121
- ok(close(a.std(), Math.sqrt(2.0)))
122
- ok(close(a.min(), 1.0))
123
- ok(close(a.max(), 5.0))
124
-
125
- # clip
126
- c = a.clip(2, 4)
127
- eq(c[0], 2)
128
- eq(c[2], 3)
129
- eq(c[4], 4)
130
-
131
- # cumsum
132
- cs = a.cumsum()
133
- eq(cs[0], 1)
134
- eq(cs[1], 3)
135
- eq(cs[4], 15)
136
-
137
- # cumprod
138
- a2 = np.array([1.0, 2.0, 3.0])
139
- cp = a2.cumprod()
140
- eq(cp[0], 1)
141
- eq(cp[1], 2)
142
- eq(cp[2], 6)
143
-
144
- # argmin / argmax
145
- eq(a.argmin(), 0)
146
- eq(a.argmax(), 4)
147
-
148
- # square, exp, log
149
- sq = np.square(np.array([2.0, 3.0]))
150
- eq(sq[0], 4)
151
- eq(sq[1], 9)
152
-
153
- ex = np.exp(np.array([0.0]))
154
- ok(close(ex[0], 1.0))
155
-
156
- lg = np.log(np.array([1.0, Math.E]))
157
- ok(close(lg[0], 0.0))
158
- ok(close(lg[1], 1.0))
159
-
160
- # trig
161
- sn = np.sin(np.array([0.0]))
162
- ok(close(sn[0], 0.0))
163
- cs2 = np.cos(np.array([0.0]))
164
- ok(close(cs2[0], 1.0))
165
-
166
- # degrees/radians
167
- deg = np.degrees(np.array([Math.PI]))
168
- ok(close(deg[0], 180.0))
169
- rad = np.radians(np.array([180.0]))
170
- ok(close(rad[0], Math.PI))
171
-
172
- # ceil / floor
173
- ok(close(np.ceil(np.array([1.2]))[0], 2.0))
174
- ok(close(np.floor(np.array([1.8]))[0], 1.0))
175
-
176
- # sign
177
- sg = np.sign(np.array([-3.0, 0.0, 5.0]))
178
- eq(sg[0], -1)
179
- eq(sg[1], 0)
180
- eq(sg[2], 1)
181
-
182
- # around
183
- ar = np.around(np.array([1.567]), 2)
184
- ok(close(ar[0], 1.57))
185
-
186
- # abs / absolute
187
- ab = np.abs(np.array([-1.0, 2.0, -3.0]))
188
- eq(ab[0], 1)
189
- eq(ab[1], 2)
190
- eq(ab[2], 3)
191
-
192
- # negative
193
- ng = np.negative(np.array([1.0, -2.0]))
194
- eq(ng[0], -1)
195
- eq(ng[1], 2)
196
-
197
- # log2 / log10
198
- l2 = np.log2(np.array([8.0]))
199
- ok(close(l2[0], 3.0))
200
- l10 = np.log10(np.array([1000.0]))
201
- ok(close(l10[0], 3.0))
202
-
203
- # -------------------------------------------------------
204
- # Broadcasting
205
- # -------------------------------------------------------
206
- a1 = np.array([1.0, 2.0, 3.0])
207
- r = np.add(a1, 10)
208
- eq(r[0], 11)
209
- eq(r[1], 12)
210
- eq(r[2], 13)
211
-
212
- r2 = np.multiply(a1, 2)
213
- eq(r2[0], 2)
214
- eq(r2[1], 4)
215
- eq(r2[2], 6)
216
-
217
- r3 = np.add(np.array([1.0, 2.0, 3.0]), np.array([10.0, 20.0, 30.0]))
218
- eq(r3[0], 11)
219
- eq(r3[1], 22)
220
- eq(r3[2], 33)
221
-
222
- # subtract / divide
223
- r4 = np.subtract(np.array([5.0, 6.0]), np.array([1.0, 2.0]))
224
- eq(r4[0], 4)
225
- eq(r4[1], 4)
226
-
227
- r5 = np.divide(np.array([6.0, 9.0]), np.array([2.0, 3.0]))
228
- eq(r5[0], 3)
229
- eq(r5[1], 3)
230
-
231
- # power
232
- pw = np.power(np.array([2.0, 3.0]), 3)
233
- eq(pw[0], 8)
234
- eq(pw[1], 27)
235
-
236
- # maximum / minimum
237
- mx = np.maximum(np.array([1.0, 5.0, 3.0]), np.array([4.0, 2.0, 3.0]))
238
- eq(mx[0], 4)
239
- eq(mx[1], 5)
240
- eq(mx[2], 3)
241
-
242
- mn = np.minimum(np.array([1.0, 5.0, 3.0]), np.array([4.0, 2.0, 3.0]))
243
- eq(mn[0], 1)
244
- eq(mn[1], 2)
245
- eq(mn[2], 3)
246
-
247
- # -------------------------------------------------------
248
- # Linear algebra
249
- # -------------------------------------------------------
250
- v1 = np.array([1.0, 2.0, 3.0])
251
- v2 = np.array([4.0, 5.0, 6.0])
252
- ok(close(np.dot(v1, v2), 32.0))
253
-
254
- A = np.array([[1.0, 2.0], [3.0, 4.0]])
255
- B = np.array([[5.0, 6.0], [7.0, 8.0]])
256
- C = np.matmul(A, B)
257
- eq(C[0][0], 19)
258
- eq(C[0][1], 22)
259
- eq(C[1][0], 43)
260
- eq(C[1][1], 50)
261
-
262
- C2 = np.dot(A, B)
263
- eq(C2[0][0], 19)
264
- eq(C2[1][1], 50)
265
-
266
- tr = np.trace(A)
267
- eq(tr, 5)
268
-
269
- A3 = np.eye(3)
270
- ok(close(np.det(A3), 1.0))
271
-
272
- A2 = np.array([[1.0, 2.0], [3.0, 4.0]])
273
- ok(close(np.det(A2), -2.0))
274
-
275
- # outer product
276
- ov = np.outer(np.array([1.0, 2.0]), np.array([3.0, 4.0]))
277
- eq(ov[0][0], 3)
278
- eq(ov[0][1], 4)
279
- eq(ov[1][0], 6)
280
- eq(ov[1][1], 8)
281
-
282
- # norm
283
- ok(close(np.norm(np.array([3.0, 4.0])), 5.0))
284
-
285
- # cross product
286
- cr = np.cross(np.array([1.0, 0.0, 0.0]), np.array([0.0, 1.0, 0.0]))
287
- eq(cr[0], 0)
288
- eq(cr[1], 0)
289
- eq(cr[2], 1)
290
-
291
- # linalg.inv
292
- M = np.array([[1.0, 2.0], [3.0, 4.0]])
293
- Minv = np.linalg.inv(M)
294
- ok(close(Minv[0][0], -2.0))
295
- ok(close(Minv[0][1], 1.0))
296
- ok(close(Minv[1][0], 1.5))
297
- ok(close(Minv[1][1], -0.5))
298
-
299
- # linalg.solve
300
- a_mat = np.array([[2.0, 1.0], [1.0, 3.0]])
301
- b_vec = np.array([5.0, 10.0])
302
- x = np.linalg.solve(a_mat, b_vec)
303
- ok(close(x[0], 1.0))
304
- ok(close(x[1], 3.0))
305
-
306
- # -------------------------------------------------------
307
- # Sorting
308
- # -------------------------------------------------------
309
- unsorted = np.array([3.0, 1.0, 4.0, 1.0, 5.0, 9.0, 2.0])
310
- sorted_arr = np.sort(unsorted)
311
- eq(sorted_arr[0], 1)
312
- eq(sorted_arr[1], 1)
313
- eq(sorted_arr[6], 9)
314
-
315
- indices = np.argsort(np.array([3.0, 1.0, 2.0]))
316
- eq(indices[0], 1)
317
- eq(indices[1], 2)
318
- eq(indices[2], 0)
319
-
320
- ss = np.searchsorted(np.array([1.0, 2.0, 3.0, 4.0, 5.0]), 3)
321
- eq(ss, 2)
322
-
323
- nz = np.nonzero(np.array([0.0, 1.0, 0.0, 2.0]))
324
- eq(nz[0][0], 1)
325
- eq(nz[0][1], 3)
326
-
327
- # where
328
- cond = np.array([1.0, 0.0, 1.0])
329
- xv = np.array([10.0, 20.0, 30.0])
330
- yv = np.array([100.0, 200.0, 300.0])
331
- wh = np.where(cond, xv, yv)
332
- eq(wh[0], 10)
333
- eq(wh[1], 200)
334
- eq(wh[2], 30)
335
-
336
- # -------------------------------------------------------
337
- # Statistics
338
- # -------------------------------------------------------
339
- med = np.median(np.array([3.0, 1.0, 2.0]))
340
- ok(close(med, 2.0))
341
-
342
- med2 = np.median(np.array([1.0, 2.0, 3.0, 4.0]))
343
- ok(close(med2, 2.5))
344
-
345
- pct = np.percentile(np.array([0.0, 25.0, 50.0, 75.0, 100.0]), 50)
346
- ok(close(pct, 50.0))
347
-
348
- # diff
349
- d = np.diff(np.array([1.0, 3.0, 6.0, 10.0]))
350
- eq(d[0], 2)
351
- eq(d[1], 3)
352
- eq(d[2], 4)
353
- eq(d.size, 3)
354
-
355
- # -------------------------------------------------------
356
- # Logic
357
- # -------------------------------------------------------
358
- ok(np.all(np.array([1.0, 1.0, 1.0])))
359
- ok(not np.all(np.array([1.0, 0.0, 1.0])))
360
- ok(np.any(np.array([0.0, 1.0, 0.0])))
361
- ok(not np.any(np.array([0.0, 0.0, 0.0])))
362
-
363
- nan_arr = np.array([1.0, NaN, 3.0])
364
- isn = np.isnan(nan_arr)
365
- eq(isn[0], 0)
366
- ok(isn[1])
367
- eq(isn[2], 0)
368
-
369
- inf_arr = np.array([1.0, Infinity, 3.0])
370
- isi = np.isinf(inf_arr)
371
- eq(isi[0], 0)
372
- ok(isi[1])
373
-
374
- ok(np.array_equal(np.array([1.0, 2.0]), np.array([1.0, 2.0])))
375
- ok(not np.array_equal(np.array([1.0, 2.0]), np.array([1.0, 3.0])))
376
-
377
- isf = np.isfinite(np.array([1.0, Infinity, NaN]))
378
- ok(isf[0])
379
- eq(isf[1], 0)
380
- eq(isf[2], 0)
381
-
382
- # comparison ufuncs
383
- ge = np.greater(np.array([3.0, 1.0]), np.array([2.0, 2.0]))
384
- ok(ge[0])
385
- eq(ge[1], 0)
386
-
387
- le = np.less_equal(np.array([1.0, 2.0]), np.array([2.0, 2.0]))
388
- ok(le[0])
389
- ok(le[1])
390
-
391
- # -------------------------------------------------------
392
- # Shape manipulation
393
- # -------------------------------------------------------
394
- a = np.arange(6)
395
- r = a.reshape([2, 3])
396
- eq(r.shape[0], 2)
397
- eq(r.shape[1], 3)
398
- eq(r[0][0], 0)
399
- eq(r[0][2], 2)
400
- eq(r[1][0], 3)
401
- eq(r[1][2], 5)
402
-
403
- r2 = np.reshape(a, [3, 2])
404
- eq(r2.shape[0], 3)
405
- eq(r2.shape[1], 2)
406
-
407
- flat = r.ravel()
408
- eq(flat.size, 6)
409
- eq(flat[0], 0)
410
- eq(flat[5], 5)
411
-
412
- A = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
413
- At = A.transpose()
414
- eq(At.shape[0], 3)
415
- eq(At.shape[1], 2)
416
- eq(At[0][0], 1)
417
- eq(At[0][1], 4)
418
- eq(At[2][0], 3)
419
- eq(At[2][1], 6)
420
-
421
- At2 = np.transpose(A)
422
- eq(At2[0][0], 1)
423
- eq(At2[2][1], 6)
424
-
425
- # concatenate
426
- a1 = np.array([1.0, 2.0, 3.0])
427
- a2 = np.array([4.0, 5.0, 6.0])
428
- cat = np.concatenate([a1, a2])
429
- eq(cat.size, 6)
430
- eq(cat[0], 1)
431
- eq(cat[5], 6)
432
-
433
- # 2D concatenate axis=0
434
- m1 = np.array([[1.0, 2.0], [3.0, 4.0]])
435
- m2 = np.array([[5.0, 6.0], [7.0, 8.0]])
436
- cat2 = np.concatenate([m1, m2], 0)
437
- eq(cat2.shape[0], 4)
438
- eq(cat2[2][0], 5)
439
-
440
- # 2D concatenate axis=1
441
- cat3 = np.concatenate([m1, m2], 1)
442
- eq(cat3.shape[1], 4)
443
- eq(cat3[0][2], 5)
444
-
445
- # hstack / vstack
446
- hs = np.hstack([a1, a2])
447
- eq(hs.size, 6)
448
- eq(hs[3], 4)
449
-
450
- vs = np.vstack([a1, a2])
451
- eq(vs.shape[0], 2)
452
- eq(vs.shape[1], 3)
453
- eq(vs[0][0], 1)
454
- eq(vs[1][0], 4)
455
-
456
- # expand_dims / squeeze
457
- ex = np.expand_dims(np.array([1.0, 2.0, 3.0]), 0)
458
- eq(ex.shape[0], 1)
459
- eq(ex.shape[1], 3)
460
-
461
- sq2 = np.squeeze(ex)
462
- eq(sq2.shape[0], 3)
463
-
464
- # flip
465
- fl = np.flip(np.array([1.0, 2.0, 3.0]))
466
- eq(fl[0], 3)
467
- eq(fl[2], 1)
468
-
469
- # roll
470
- rl = np.roll(np.array([1.0, 2.0, 3.0, 4.0]), 1)
471
- eq(rl[0], 4)
472
- eq(rl[1], 1)
473
-
474
- # repeat
475
- rp = np.repeat(np.array([1.0, 2.0, 3.0]), 2)
476
- eq(rp.size, 6)
477
- eq(rp[0], 1)
478
- eq(rp[1], 1)
479
- eq(rp[2], 2)
480
-
481
- # -------------------------------------------------------
482
- # Set operations
483
- # -------------------------------------------------------
484
- u = np.union1d(np.array([1.0, 2.0, 3.0]), np.array([2.0, 3.0, 4.0]))
485
- eq(u.size, 4)
486
- eq(u[0], 1)
487
- eq(u[3], 4)
488
-
489
- i = np.intersect1d(np.array([1.0, 2.0, 3.0]), np.array([2.0, 3.0, 4.0]))
490
- eq(i.size, 2)
491
- eq(i[0], 2)
492
- eq(i[1], 3)
493
-
494
- sd = np.setdiff1d(np.array([1.0, 2.0, 3.0]), np.array([2.0, 3.0]))
495
- eq(sd.size, 1)
496
- eq(sd[0], 1)
497
-
498
- i1d = np.in1d(np.array([1.0, 2.0, 3.0]), np.array([2.0, 4.0]))
499
- eq(i1d[0], 0)
500
- ok(i1d[1])
501
- eq(i1d[2], 0)
502
-
503
- # -------------------------------------------------------
504
- # Additional shape helpers
505
- # -------------------------------------------------------
506
- # diag: extract from matrix
507
- dg = np.diag(np.array([[1.0, 2.0], [3.0, 4.0]]))
508
- eq(dg[0], 1)
509
- eq(dg[1], 4)
510
-
511
- # diag: create matrix from vector
512
- dm = np.diag(np.array([1.0, 2.0, 3.0]))
513
- eq(dm.shape[0], 3)
514
- eq(dm[0][0], 1)
515
- eq(dm[1][1], 2)
516
- eq(dm[2][2], 3)
517
- eq(dm[0][1], 0)
518
-
519
- # tril / triu
520
- tri_m = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]])
521
- tl_m = np.tril(tri_m)
522
- eq(tl_m[0][1], 0)
523
- eq(tl_m[1][0], 4)
524
- eq(tl_m[2][2], 9)
525
-
526
- tu_m = np.triu(tri_m)
527
- eq(tu_m[0][0], 1)
528
- eq(tu_m[1][0], 0)
529
- eq(tu_m[2][2], 9)
530
-
531
- # broadcast_to
532
- bt = np.broadcast_to(np.array([1.0, 2.0, 3.0]), [2, 3])
533
- eq(bt.shape[0], 2)
534
- eq(bt.shape[1], 3)
535
- eq(bt[0][0], 1)
536
- eq(bt[1][2], 3)
537
-
538
- # -------------------------------------------------------
539
- # Additional numeric
540
- # -------------------------------------------------------
541
- # nan_to_num
542
- nn = np.nan_to_num(np.array([1.0, NaN, 3.0]))
543
- eq(nn[0], 1)
544
- eq(nn[1], 0)
545
- eq(nn[2], 3)
546
-
547
- # fix (truncate toward zero)
548
- fx = np.fix(np.array([2.7, -2.7]))
549
- eq(fx[0], 2)
550
- eq(fx[1], -2)
551
-
552
- # ptp
553
- pt = np.ptp(np.array([1.0, 5.0, 2.0, 8.0]))
554
- eq(pt, 7)
555
-
556
- # count_nonzero
557
- cnz = np.count_nonzero(np.array([0.0, 1.0, 0.0, 2.0, 3.0]))
558
- eq(cnz, 3)
559
-
560
- # ediff1d
561
- ed = np.ediff1d(np.array([1.0, 2.0, 4.0, 7.0]))
562
- eq(ed[0], 1)
563
- eq(ed[1], 2)
564
- eq(ed[2], 3)
565
-
566
- # average (weighted)
567
- avg = np.average(np.array([1.0, 2.0, 3.0]), weights=np.array([1.0, 2.0, 1.0]))
568
- ok(close(avg, 2.0))
569
-
570
- # interp
571
- xi = np.interp(0.5, np.array([0.0, 1.0]), np.array([0.0, 10.0]))
572
- ok(close(xi, 5.0))
573
-
574
- # trapz
575
- tx = np.trapz(np.array([1.0, 2.0, 1.0]), dx=1.0)
576
- ok(close(tx, 3.0))
577
-
578
- # convolve
579
- cv = np.convolve(np.array([1.0, 2.0, 3.0]), np.array([0.0, 1.0, 0.5]))
580
- eq(cv.size, 5)
581
- ok(close(cv[0], 0))
582
- ok(close(cv[1], 1))
583
- ok(close(cv[2], 2.5))
584
-
585
- # polyval
586
- pv = np.polyval(np.array([1.0, 0.0, -1.0]), 2.0)
587
- ok(close(pv, 3.0))
588
-
589
- # shape / ndim / size utilities
590
- ok(close(np.shape(np.array([[1.0, 2.0]]))[0], 1))
591
- eq(np.ndim(np.array([1.0, 2.0, 3.0])), 1)
592
- eq(np.size(np.array([[1.0, 2.0], [3.0, 4.0]])), 4)
593
-
594
- # prod
595
- pr = np.prod(np.array([1.0, 2.0, 3.0, 4.0]))
596
- eq(pr, 24)
597
-
598
- # amin / amax / nanmin / nanmax / nanmean
599
- eq(np.amin(np.array([3.0, 1.0, 2.0])), 1)
600
- eq(np.amax(np.array([3.0, 1.0, 2.0])), 3)
601
- eq(np.nanmin(np.array([3.0, NaN, 1.0])), 1)
602
- eq(np.nanmax(np.array([3.0, NaN, 5.0])), 5)
603
- ok(close(np.nanmean(np.array([1.0, NaN, 3.0])), 2.0))
604
-
605
- # var / std
606
- ok(close(np.variance(np.array([2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0])), 4.0))
607
- ok(close(np.std(np.array([2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0])), 2.0))
608
-
609
- # mod
610
- mo = np.mod(np.array([10.0, 11.0, 12.0]), 3)
611
- eq(mo[0], 1)
612
- eq(mo[1], 2)
613
- eq(mo[2], 0)
614
-
615
- # hypot
616
- hp = np.hypot(np.array([3.0]), np.array([4.0]))
617
- ok(close(hp[0], 5.0))
618
-
619
- # arctan2
620
- at2 = np.arctan2(np.array([1.0]), np.array([1.0]))
621
- ok(close(at2[0], Math.PI / 4.0))
622
-
623
- # -------------------------------------------------------
624
- # 2D reduction operations
625
- # -------------------------------------------------------
626
- m = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
627
- row_sum = m.sum(1)
628
- eq(row_sum[0], 6)
629
- eq(row_sum[1], 15)
630
-
631
- col_sum = m.sum(0)
632
- eq(col_sum[0], 5)
633
- eq(col_sum[1], 7)
634
- eq(col_sum[2], 9)
635
-
636
- # -------------------------------------------------------
637
- # astype / copy
638
- # -------------------------------------------------------
639
- float_arr = np.array([1.5, 2.7, 3.9])
640
- int_arr = float_arr.astype('int32')
641
- eq(int_arr[0], 1)
642
- eq(int_arr[1], 2)
643
- eq(int_arr[2], 3)
644
-
645
- cp = float_arr.copy()
646
- ok(np.array_equal(cp, float_arr))
647
-
648
- # -------------------------------------------------------
649
- # random sub-module
650
- # -------------------------------------------------------
651
- np.random.seed(42)
652
- r1 = np.random.rand(3)
653
- ok(isinstance(r1, np.ndarray))
654
- eq(r1.shape[0], 3)
655
- ok(r1[0] >= 0.0)
656
- ok(r1[0] < 1.0)
657
- ok(r1[1] >= 0.0)
658
- ok(r1[1] < 1.0)
659
-
660
- r2d = np.random.rand(2, 3)
661
- eq(r2d.shape[0], 2)
662
- eq(r2d.shape[1], 3)
663
-
664
- # seeded reproducibility
665
- np.random.seed(123)
666
- v1 = np.random.rand(1)[0]
667
- np.random.seed(123)
668
- v2 = np.random.rand(1)[0]
669
- ok(close(v1, v2))
670
-
671
- rn = np.random.randn(5)
672
- eq(rn.shape[0], 5)
673
-
674
- ri = np.random.randint(0, 10, 5)
675
- eq(ri.size, 5)
676
- ok(ri[0] >= 0)
677
- ok(ri[0] < 10)
678
-
679
- ru = np.random.uniform(1.0, 2.0, 4)
680
- eq(ru.size, 4)
681
- ok(ru[0] >= 1.0)
682
- ok(ru[0] < 2.0)
683
-
684
- rno = np.random.normal(0.0, 1.0, 3)
685
- eq(rno.size, 3)
686
-
687
- perm = np.random.permutation(5)
688
- eq(perm.size, 5)
689
-
690
- rc = np.random.choice(np.array([10.0, 20.0, 30.0]), 2)
691
- eq(rc.size, 2)
692
-
693
- # -------------------------------------------------------
694
- # bincount / histogram
695
- # -------------------------------------------------------
696
- bc = np.bincount(np.array([0, 1, 1, 2, 2, 2]))
697
- eq(bc[0], 1)
698
- eq(bc[1], 2)
699
- eq(bc[2], 3)
700
-
701
- hist = np.histogram(np.array([1.0, 2.0, 3.0, 4.0, 5.0]), 5)
702
- eq(hist[0].size, 5)
703
- eq(hist[1].size, 6)
704
-
705
- # -------------------------------------------------------
706
- # searchsorted and diff
707
- # -------------------------------------------------------
708
- eq(np.searchsorted(np.array([1.0, 3.0, 5.0]), 2.0), 1)
709
- eq(np.searchsorted(np.array([1.0, 3.0, 5.0]), 3.0), 1)
710
- eq(np.searchsorted(np.array([1.0, 3.0, 5.0]), 3.0, 'right'), 2)
711
-
712
- # -------------------------------------------------------
713
- # linspace / logspace
714
- # -------------------------------------------------------
715
- ls2 = np.linspace(0.0, 100.0, 11)
716
- eq(ls2.size, 11)
717
- ok(close(ls2[0], 0.0))
718
- ok(close(ls2[10], 100.0))
719
- ok(close(ls2[5], 50.0))
720
-
721
- log_s = np.logspace(0, 2, 3)
722
- ok(close(log_s[0], 1.0))
723
- ok(close(log_s[1], 10.0))
724
- ok(close(log_s[2], 100.0))
725
-
726
- # -------------------------------------------------------
727
- # polyfit / polyval roundtrip
728
- # -------------------------------------------------------
729
- xp = np.array([0.0, 1.0, 2.0])
730
- yp = np.array([0.0, 1.0, 4.0])
731
- coeffs = np.polyfit(xp, yp, 2)
732
- ok(close(np.polyval(coeffs, 0.0), 0.0, 1e-6))
733
- ok(close(np.polyval(coeffs, 1.0), 1.0, 1e-6))
734
- ok(close(np.polyval(coeffs, 2.0), 4.0, 1e-6))
1
+ # globals: assrt
2
+ # vim:fileencoding=utf-8
3
+ import numpy as np
4
+
5
+ eq = assrt.equal
6
+ de = assrt.deepEqual
7
+ ok = assrt.ok
8
+
9
+ def close(a, b, tol=1e-9):
10
+ return Math.abs(a - b) < tol
11
+
12
+ # -------------------------------------------------------
13
+ # Array creation
14
+ # -------------------------------------------------------
15
+ z = np.zeros([3])
16
+ ok(isinstance(z, np.ndarray))
17
+ eq(z.shape[0], 3)
18
+ eq(z[0], 0)
19
+ eq(z[1], 0)
20
+ eq(z.size, 3)
21
+ eq(z.ndim, 1)
22
+
23
+ o = np.ones([2, 3])
24
+ ok(isinstance(o, np.ndarray))
25
+ eq(o.shape[0], 2)
26
+ eq(o.shape[1], 3)
27
+ eq(o[0][0], 1)
28
+ eq(o[1][2], 1)
29
+ eq(o.ndim, 2)
30
+
31
+ a = np.arange(5)
32
+ eq(a[0], 0)
33
+ eq(a[1], 1)
34
+ eq(a[4], 4)
35
+ eq(a.size, 5)
36
+
37
+ a = np.arange(1, 6)
38
+ eq(a[0], 1)
39
+ eq(a[4], 5)
40
+
41
+ a = np.arange(0, 10, 2)
42
+ eq(a[0], 0)
43
+ eq(a[1], 2)
44
+ eq(a[2], 4)
45
+ eq(a.size, 5)
46
+
47
+ ls = np.linspace(0, 1, 5)
48
+ eq(ls.size, 5)
49
+ ok(close(ls[0], 0.0))
50
+ ok(close(ls[4], 1.0))
51
+ ok(close(ls[2], 0.5))
52
+
53
+ e = np.eye(3)
54
+ eq(e.shape[0], 3)
55
+ eq(e.shape[1], 3)
56
+ eq(e[0][0], 1)
57
+ eq(e[1][1], 1)
58
+ eq(e[2][2], 1)
59
+ eq(e[0][1], 0)
60
+ eq(e[1][0], 0)
61
+
62
+ e2 = np.eye(3, k=1)
63
+ eq(e2[0][1], 1)
64
+ eq(e2[1][2], 1)
65
+ eq(e2[0][0], 0)
66
+
67
+ f = np.full([2, 3], 7.0)
68
+ eq(f[0][0], 7)
69
+ eq(f[1][2], 7)
70
+
71
+ a = np.array([1.0, 2.0, 3.0])
72
+ eq(a[0], 1)
73
+ eq(a[2], 3)
74
+
75
+ a2d = np.array([[1.0, 2.0], [3.0, 4.0]])
76
+ eq(a2d[0][0], 1)
77
+ eq(a2d[0][1], 2)
78
+ eq(a2d[1][0], 3)
79
+ eq(a2d[1][1], 4)
80
+
81
+ zl = np.zeros_like(a2d)
82
+ eq(zl.shape[0], 2)
83
+ eq(zl.shape[1], 2)
84
+ eq(zl[0][0], 0)
85
+
86
+ ol = np.ones_like(a)
87
+ eq(ol.size, 3)
88
+ eq(ol[0], 1)
89
+
90
+ # -------------------------------------------------------
91
+ # Element access and item/itemset
92
+ # -------------------------------------------------------
93
+ arr1d = np.array([10.0, 20.0, 30.0])
94
+ eq(arr1d.item(0), 10)
95
+ eq(arr1d.item(2), 30)
96
+
97
+ arr2d = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
98
+ eq(arr2d.item(0, 0), 1)
99
+ eq(arr2d.item(0, 2), 3)
100
+ eq(arr2d.item(1, 1), 5)
101
+
102
+ # tolist
103
+ tl = np.array([1.0, 2.0, 3.0]).tolist()
104
+ de(tl, [1, 2, 3])
105
+
106
+ # -------------------------------------------------------
107
+ # Math operations
108
+ # -------------------------------------------------------
109
+ a = np.array([1.0, 4.0, 9.0])
110
+ s = np.sqrt(a)
111
+ ok(close(s[0], 1.0))
112
+ ok(close(s[1], 2.0))
113
+ ok(close(s[2], 3.0))
114
+
115
+ a = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
116
+ ok(close(a.sum(), 15.0))
117
+ ok(close(a.mean(), 3.0))
118
+ ok(close(np.sum(a), 15.0))
119
+ ok(close(np.mean(a), 3.0))
120
+
121
+ ok(close(a.std(), Math.sqrt(2.0)))
122
+ ok(close(a.min(), 1.0))
123
+ ok(close(a.max(), 5.0))
124
+
125
+ # clip
126
+ c = a.clip(2, 4)
127
+ eq(c[0], 2)
128
+ eq(c[2], 3)
129
+ eq(c[4], 4)
130
+
131
+ # cumsum
132
+ cs = a.cumsum()
133
+ eq(cs[0], 1)
134
+ eq(cs[1], 3)
135
+ eq(cs[4], 15)
136
+
137
+ # cumprod
138
+ a2 = np.array([1.0, 2.0, 3.0])
139
+ cp = a2.cumprod()
140
+ eq(cp[0], 1)
141
+ eq(cp[1], 2)
142
+ eq(cp[2], 6)
143
+
144
+ # argmin / argmax
145
+ eq(a.argmin(), 0)
146
+ eq(a.argmax(), 4)
147
+
148
+ # square, exp, log
149
+ sq = np.square(np.array([2.0, 3.0]))
150
+ eq(sq[0], 4)
151
+ eq(sq[1], 9)
152
+
153
+ ex = np.exp(np.array([0.0]))
154
+ ok(close(ex[0], 1.0))
155
+
156
+ lg = np.log(np.array([1.0, Math.E]))
157
+ ok(close(lg[0], 0.0))
158
+ ok(close(lg[1], 1.0))
159
+
160
+ # trig
161
+ sn = np.sin(np.array([0.0]))
162
+ ok(close(sn[0], 0.0))
163
+ cs2 = np.cos(np.array([0.0]))
164
+ ok(close(cs2[0], 1.0))
165
+
166
+ # degrees/radians
167
+ deg = np.degrees(np.array([Math.PI]))
168
+ ok(close(deg[0], 180.0))
169
+ rad = np.radians(np.array([180.0]))
170
+ ok(close(rad[0], Math.PI))
171
+
172
+ # ceil / floor
173
+ ok(close(np.ceil(np.array([1.2]))[0], 2.0))
174
+ ok(close(np.floor(np.array([1.8]))[0], 1.0))
175
+
176
+ # sign
177
+ sg = np.sign(np.array([-3.0, 0.0, 5.0]))
178
+ eq(sg[0], -1)
179
+ eq(sg[1], 0)
180
+ eq(sg[2], 1)
181
+
182
+ # around
183
+ ar = np.around(np.array([1.567]), 2)
184
+ ok(close(ar[0], 1.57))
185
+
186
+ # abs / absolute
187
+ ab = np.abs(np.array([-1.0, 2.0, -3.0]))
188
+ eq(ab[0], 1)
189
+ eq(ab[1], 2)
190
+ eq(ab[2], 3)
191
+
192
+ # negative
193
+ ng = np.negative(np.array([1.0, -2.0]))
194
+ eq(ng[0], -1)
195
+ eq(ng[1], 2)
196
+
197
+ # log2 / log10
198
+ l2 = np.log2(np.array([8.0]))
199
+ ok(close(l2[0], 3.0))
200
+ l10 = np.log10(np.array([1000.0]))
201
+ ok(close(l10[0], 3.0))
202
+
203
+ # -------------------------------------------------------
204
+ # Broadcasting
205
+ # -------------------------------------------------------
206
+ a1 = np.array([1.0, 2.0, 3.0])
207
+ r = np.add(a1, 10)
208
+ eq(r[0], 11)
209
+ eq(r[1], 12)
210
+ eq(r[2], 13)
211
+
212
+ r2 = np.multiply(a1, 2)
213
+ eq(r2[0], 2)
214
+ eq(r2[1], 4)
215
+ eq(r2[2], 6)
216
+
217
+ r3 = np.add(np.array([1.0, 2.0, 3.0]), np.array([10.0, 20.0, 30.0]))
218
+ eq(r3[0], 11)
219
+ eq(r3[1], 22)
220
+ eq(r3[2], 33)
221
+
222
+ # subtract / divide
223
+ r4 = np.subtract(np.array([5.0, 6.0]), np.array([1.0, 2.0]))
224
+ eq(r4[0], 4)
225
+ eq(r4[1], 4)
226
+
227
+ r5 = np.divide(np.array([6.0, 9.0]), np.array([2.0, 3.0]))
228
+ eq(r5[0], 3)
229
+ eq(r5[1], 3)
230
+
231
+ # power
232
+ pw = np.power(np.array([2.0, 3.0]), 3)
233
+ eq(pw[0], 8)
234
+ eq(pw[1], 27)
235
+
236
+ # maximum / minimum
237
+ mx = np.maximum(np.array([1.0, 5.0, 3.0]), np.array([4.0, 2.0, 3.0]))
238
+ eq(mx[0], 4)
239
+ eq(mx[1], 5)
240
+ eq(mx[2], 3)
241
+
242
+ mn = np.minimum(np.array([1.0, 5.0, 3.0]), np.array([4.0, 2.0, 3.0]))
243
+ eq(mn[0], 1)
244
+ eq(mn[1], 2)
245
+ eq(mn[2], 3)
246
+
247
+ # -------------------------------------------------------
248
+ # Linear algebra
249
+ # -------------------------------------------------------
250
+ v1 = np.array([1.0, 2.0, 3.0])
251
+ v2 = np.array([4.0, 5.0, 6.0])
252
+ ok(close(np.dot(v1, v2), 32.0))
253
+
254
+ A = np.array([[1.0, 2.0], [3.0, 4.0]])
255
+ B = np.array([[5.0, 6.0], [7.0, 8.0]])
256
+ C = np.matmul(A, B)
257
+ eq(C[0][0], 19)
258
+ eq(C[0][1], 22)
259
+ eq(C[1][0], 43)
260
+ eq(C[1][1], 50)
261
+
262
+ C2 = np.dot(A, B)
263
+ eq(C2[0][0], 19)
264
+ eq(C2[1][1], 50)
265
+
266
+ tr = np.trace(A)
267
+ eq(tr, 5)
268
+
269
+ A3 = np.eye(3)
270
+ ok(close(np.det(A3), 1.0))
271
+
272
+ A2 = np.array([[1.0, 2.0], [3.0, 4.0]])
273
+ ok(close(np.det(A2), -2.0))
274
+
275
+ # outer product
276
+ ov = np.outer(np.array([1.0, 2.0]), np.array([3.0, 4.0]))
277
+ eq(ov[0][0], 3)
278
+ eq(ov[0][1], 4)
279
+ eq(ov[1][0], 6)
280
+ eq(ov[1][1], 8)
281
+
282
+ # norm
283
+ ok(close(np.norm(np.array([3.0, 4.0])), 5.0))
284
+
285
+ # cross product
286
+ cr = np.cross(np.array([1.0, 0.0, 0.0]), np.array([0.0, 1.0, 0.0]))
287
+ eq(cr[0], 0)
288
+ eq(cr[1], 0)
289
+ eq(cr[2], 1)
290
+
291
+ # linalg.inv
292
+ M = np.array([[1.0, 2.0], [3.0, 4.0]])
293
+ Minv = np.linalg.inv(M)
294
+ ok(close(Minv[0][0], -2.0))
295
+ ok(close(Minv[0][1], 1.0))
296
+ ok(close(Minv[1][0], 1.5))
297
+ ok(close(Minv[1][1], -0.5))
298
+
299
+ # linalg.solve
300
+ a_mat = np.array([[2.0, 1.0], [1.0, 3.0]])
301
+ b_vec = np.array([5.0, 10.0])
302
+ x = np.linalg.solve(a_mat, b_vec)
303
+ ok(close(x[0], 1.0))
304
+ ok(close(x[1], 3.0))
305
+
306
+ # -------------------------------------------------------
307
+ # Sorting
308
+ # -------------------------------------------------------
309
+ unsorted = np.array([3.0, 1.0, 4.0, 1.0, 5.0, 9.0, 2.0])
310
+ sorted_arr = np.sort(unsorted)
311
+ eq(sorted_arr[0], 1)
312
+ eq(sorted_arr[1], 1)
313
+ eq(sorted_arr[6], 9)
314
+
315
+ indices = np.argsort(np.array([3.0, 1.0, 2.0]))
316
+ eq(indices[0], 1)
317
+ eq(indices[1], 2)
318
+ eq(indices[2], 0)
319
+
320
+ ss = np.searchsorted(np.array([1.0, 2.0, 3.0, 4.0, 5.0]), 3)
321
+ eq(ss, 2)
322
+
323
+ nz = np.nonzero(np.array([0.0, 1.0, 0.0, 2.0]))
324
+ eq(nz[0][0], 1)
325
+ eq(nz[0][1], 3)
326
+
327
+ # where
328
+ cond = np.array([1.0, 0.0, 1.0])
329
+ xv = np.array([10.0, 20.0, 30.0])
330
+ yv = np.array([100.0, 200.0, 300.0])
331
+ wh = np.where(cond, xv, yv)
332
+ eq(wh[0], 10)
333
+ eq(wh[1], 200)
334
+ eq(wh[2], 30)
335
+
336
+ # -------------------------------------------------------
337
+ # Statistics
338
+ # -------------------------------------------------------
339
+ med = np.median(np.array([3.0, 1.0, 2.0]))
340
+ ok(close(med, 2.0))
341
+
342
+ med2 = np.median(np.array([1.0, 2.0, 3.0, 4.0]))
343
+ ok(close(med2, 2.5))
344
+
345
+ pct = np.percentile(np.array([0.0, 25.0, 50.0, 75.0, 100.0]), 50)
346
+ ok(close(pct, 50.0))
347
+
348
+ # diff
349
+ d = np.diff(np.array([1.0, 3.0, 6.0, 10.0]))
350
+ eq(d[0], 2)
351
+ eq(d[1], 3)
352
+ eq(d[2], 4)
353
+ eq(d.size, 3)
354
+
355
+ # -------------------------------------------------------
356
+ # Logic
357
+ # -------------------------------------------------------
358
+ ok(np.all(np.array([1.0, 1.0, 1.0])))
359
+ ok(not np.all(np.array([1.0, 0.0, 1.0])))
360
+ ok(np.any(np.array([0.0, 1.0, 0.0])))
361
+ ok(not np.any(np.array([0.0, 0.0, 0.0])))
362
+
363
+ nan_arr = np.array([1.0, NaN, 3.0])
364
+ isn = np.isnan(nan_arr)
365
+ eq(isn[0], 0)
366
+ ok(isn[1])
367
+ eq(isn[2], 0)
368
+
369
+ inf_arr = np.array([1.0, Infinity, 3.0])
370
+ isi = np.isinf(inf_arr)
371
+ eq(isi[0], 0)
372
+ ok(isi[1])
373
+
374
+ ok(np.array_equal(np.array([1.0, 2.0]), np.array([1.0, 2.0])))
375
+ ok(not np.array_equal(np.array([1.0, 2.0]), np.array([1.0, 3.0])))
376
+
377
+ isf = np.isfinite(np.array([1.0, Infinity, NaN]))
378
+ ok(isf[0])
379
+ eq(isf[1], 0)
380
+ eq(isf[2], 0)
381
+
382
+ # comparison ufuncs
383
+ ge = np.greater(np.array([3.0, 1.0]), np.array([2.0, 2.0]))
384
+ ok(ge[0])
385
+ eq(ge[1], 0)
386
+
387
+ le = np.less_equal(np.array([1.0, 2.0]), np.array([2.0, 2.0]))
388
+ ok(le[0])
389
+ ok(le[1])
390
+
391
+ # -------------------------------------------------------
392
+ # Shape manipulation
393
+ # -------------------------------------------------------
394
+ a = np.arange(6)
395
+ r = a.reshape([2, 3])
396
+ eq(r.shape[0], 2)
397
+ eq(r.shape[1], 3)
398
+ eq(r[0][0], 0)
399
+ eq(r[0][2], 2)
400
+ eq(r[1][0], 3)
401
+ eq(r[1][2], 5)
402
+
403
+ r2 = np.reshape(a, [3, 2])
404
+ eq(r2.shape[0], 3)
405
+ eq(r2.shape[1], 2)
406
+
407
+ flat = r.ravel()
408
+ eq(flat.size, 6)
409
+ eq(flat[0], 0)
410
+ eq(flat[5], 5)
411
+
412
+ A = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
413
+ At = A.transpose()
414
+ eq(At.shape[0], 3)
415
+ eq(At.shape[1], 2)
416
+ eq(At[0][0], 1)
417
+ eq(At[0][1], 4)
418
+ eq(At[2][0], 3)
419
+ eq(At[2][1], 6)
420
+
421
+ At2 = np.transpose(A)
422
+ eq(At2[0][0], 1)
423
+ eq(At2[2][1], 6)
424
+
425
+ # concatenate
426
+ a1 = np.array([1.0, 2.0, 3.0])
427
+ a2 = np.array([4.0, 5.0, 6.0])
428
+ cat = np.concatenate([a1, a2])
429
+ eq(cat.size, 6)
430
+ eq(cat[0], 1)
431
+ eq(cat[5], 6)
432
+
433
+ # 2D concatenate axis=0
434
+ m1 = np.array([[1.0, 2.0], [3.0, 4.0]])
435
+ m2 = np.array([[5.0, 6.0], [7.0, 8.0]])
436
+ cat2 = np.concatenate([m1, m2], 0)
437
+ eq(cat2.shape[0], 4)
438
+ eq(cat2[2][0], 5)
439
+
440
+ # 2D concatenate axis=1
441
+ cat3 = np.concatenate([m1, m2], 1)
442
+ eq(cat3.shape[1], 4)
443
+ eq(cat3[0][2], 5)
444
+
445
+ # hstack / vstack
446
+ hs = np.hstack([a1, a2])
447
+ eq(hs.size, 6)
448
+ eq(hs[3], 4)
449
+
450
+ vs = np.vstack([a1, a2])
451
+ eq(vs.shape[0], 2)
452
+ eq(vs.shape[1], 3)
453
+ eq(vs[0][0], 1)
454
+ eq(vs[1][0], 4)
455
+
456
+ # expand_dims / squeeze
457
+ ex = np.expand_dims(np.array([1.0, 2.0, 3.0]), 0)
458
+ eq(ex.shape[0], 1)
459
+ eq(ex.shape[1], 3)
460
+
461
+ sq2 = np.squeeze(ex)
462
+ eq(sq2.shape[0], 3)
463
+
464
+ # flip
465
+ fl = np.flip(np.array([1.0, 2.0, 3.0]))
466
+ eq(fl[0], 3)
467
+ eq(fl[2], 1)
468
+
469
+ # roll
470
+ rl = np.roll(np.array([1.0, 2.0, 3.0, 4.0]), 1)
471
+ eq(rl[0], 4)
472
+ eq(rl[1], 1)
473
+
474
+ # repeat
475
+ rp = np.repeat(np.array([1.0, 2.0, 3.0]), 2)
476
+ eq(rp.size, 6)
477
+ eq(rp[0], 1)
478
+ eq(rp[1], 1)
479
+ eq(rp[2], 2)
480
+
481
+ # -------------------------------------------------------
482
+ # Set operations
483
+ # -------------------------------------------------------
484
+ u = np.union1d(np.array([1.0, 2.0, 3.0]), np.array([2.0, 3.0, 4.0]))
485
+ eq(u.size, 4)
486
+ eq(u[0], 1)
487
+ eq(u[3], 4)
488
+
489
+ i = np.intersect1d(np.array([1.0, 2.0, 3.0]), np.array([2.0, 3.0, 4.0]))
490
+ eq(i.size, 2)
491
+ eq(i[0], 2)
492
+ eq(i[1], 3)
493
+
494
+ sd = np.setdiff1d(np.array([1.0, 2.0, 3.0]), np.array([2.0, 3.0]))
495
+ eq(sd.size, 1)
496
+ eq(sd[0], 1)
497
+
498
+ i1d = np.in1d(np.array([1.0, 2.0, 3.0]), np.array([2.0, 4.0]))
499
+ eq(i1d[0], 0)
500
+ ok(i1d[1])
501
+ eq(i1d[2], 0)
502
+
503
+ # -------------------------------------------------------
504
+ # Additional shape helpers
505
+ # -------------------------------------------------------
506
+ # diag: extract from matrix
507
+ dg = np.diag(np.array([[1.0, 2.0], [3.0, 4.0]]))
508
+ eq(dg[0], 1)
509
+ eq(dg[1], 4)
510
+
511
+ # diag: create matrix from vector
512
+ dm = np.diag(np.array([1.0, 2.0, 3.0]))
513
+ eq(dm.shape[0], 3)
514
+ eq(dm[0][0], 1)
515
+ eq(dm[1][1], 2)
516
+ eq(dm[2][2], 3)
517
+ eq(dm[0][1], 0)
518
+
519
+ # tril / triu
520
+ tri_m = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]])
521
+ tl_m = np.tril(tri_m)
522
+ eq(tl_m[0][1], 0)
523
+ eq(tl_m[1][0], 4)
524
+ eq(tl_m[2][2], 9)
525
+
526
+ tu_m = np.triu(tri_m)
527
+ eq(tu_m[0][0], 1)
528
+ eq(tu_m[1][0], 0)
529
+ eq(tu_m[2][2], 9)
530
+
531
+ # broadcast_to
532
+ bt = np.broadcast_to(np.array([1.0, 2.0, 3.0]), [2, 3])
533
+ eq(bt.shape[0], 2)
534
+ eq(bt.shape[1], 3)
535
+ eq(bt[0][0], 1)
536
+ eq(bt[1][2], 3)
537
+
538
+ # -------------------------------------------------------
539
+ # Additional numeric
540
+ # -------------------------------------------------------
541
+ # nan_to_num
542
+ nn = np.nan_to_num(np.array([1.0, NaN, 3.0]))
543
+ eq(nn[0], 1)
544
+ eq(nn[1], 0)
545
+ eq(nn[2], 3)
546
+
547
+ # fix (truncate toward zero)
548
+ fx = np.fix(np.array([2.7, -2.7]))
549
+ eq(fx[0], 2)
550
+ eq(fx[1], -2)
551
+
552
+ # ptp
553
+ pt = np.ptp(np.array([1.0, 5.0, 2.0, 8.0]))
554
+ eq(pt, 7)
555
+
556
+ # count_nonzero
557
+ cnz = np.count_nonzero(np.array([0.0, 1.0, 0.0, 2.0, 3.0]))
558
+ eq(cnz, 3)
559
+
560
+ # ediff1d
561
+ ed = np.ediff1d(np.array([1.0, 2.0, 4.0, 7.0]))
562
+ eq(ed[0], 1)
563
+ eq(ed[1], 2)
564
+ eq(ed[2], 3)
565
+
566
+ # average (weighted)
567
+ avg = np.average(np.array([1.0, 2.0, 3.0]), weights=np.array([1.0, 2.0, 1.0]))
568
+ ok(close(avg, 2.0))
569
+
570
+ # interp
571
+ xi = np.interp(0.5, np.array([0.0, 1.0]), np.array([0.0, 10.0]))
572
+ ok(close(xi, 5.0))
573
+
574
+ # trapz
575
+ tx = np.trapz(np.array([1.0, 2.0, 1.0]), dx=1.0)
576
+ ok(close(tx, 3.0))
577
+
578
+ # convolve
579
+ cv = np.convolve(np.array([1.0, 2.0, 3.0]), np.array([0.0, 1.0, 0.5]))
580
+ eq(cv.size, 5)
581
+ ok(close(cv[0], 0))
582
+ ok(close(cv[1], 1))
583
+ ok(close(cv[2], 2.5))
584
+
585
+ # polyval
586
+ pv = np.polyval(np.array([1.0, 0.0, -1.0]), 2.0)
587
+ ok(close(pv, 3.0))
588
+
589
+ # shape / ndim / size utilities
590
+ ok(close(np.shape(np.array([[1.0, 2.0]]))[0], 1))
591
+ eq(np.ndim(np.array([1.0, 2.0, 3.0])), 1)
592
+ eq(np.size(np.array([[1.0, 2.0], [3.0, 4.0]])), 4)
593
+
594
+ # prod
595
+ pr = np.prod(np.array([1.0, 2.0, 3.0, 4.0]))
596
+ eq(pr, 24)
597
+
598
+ # amin / amax / nanmin / nanmax / nanmean
599
+ eq(np.amin(np.array([3.0, 1.0, 2.0])), 1)
600
+ eq(np.amax(np.array([3.0, 1.0, 2.0])), 3)
601
+ eq(np.nanmin(np.array([3.0, NaN, 1.0])), 1)
602
+ eq(np.nanmax(np.array([3.0, NaN, 5.0])), 5)
603
+ ok(close(np.nanmean(np.array([1.0, NaN, 3.0])), 2.0))
604
+
605
+ # var / std
606
+ ok(close(np.variance(np.array([2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0])), 4.0))
607
+ ok(close(np.std(np.array([2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0])), 2.0))
608
+
609
+ # mod
610
+ mo = np.mod(np.array([10.0, 11.0, 12.0]), 3)
611
+ eq(mo[0], 1)
612
+ eq(mo[1], 2)
613
+ eq(mo[2], 0)
614
+
615
+ # hypot
616
+ hp = np.hypot(np.array([3.0]), np.array([4.0]))
617
+ ok(close(hp[0], 5.0))
618
+
619
+ # arctan2
620
+ at2 = np.arctan2(np.array([1.0]), np.array([1.0]))
621
+ ok(close(at2[0], Math.PI / 4.0))
622
+
623
+ # -------------------------------------------------------
624
+ # 2D reduction operations
625
+ # -------------------------------------------------------
626
+ m = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
627
+ row_sum = m.sum(1)
628
+ eq(row_sum[0], 6)
629
+ eq(row_sum[1], 15)
630
+
631
+ col_sum = m.sum(0)
632
+ eq(col_sum[0], 5)
633
+ eq(col_sum[1], 7)
634
+ eq(col_sum[2], 9)
635
+
636
+ # -------------------------------------------------------
637
+ # astype / copy
638
+ # -------------------------------------------------------
639
+ float_arr = np.array([1.5, 2.7, 3.9])
640
+ int_arr = float_arr.astype('int32')
641
+ eq(int_arr[0], 1)
642
+ eq(int_arr[1], 2)
643
+ eq(int_arr[2], 3)
644
+
645
+ cp = float_arr.copy()
646
+ ok(np.array_equal(cp, float_arr))
647
+
648
+ # -------------------------------------------------------
649
+ # random sub-module
650
+ # -------------------------------------------------------
651
+ np.random.seed(42)
652
+ r1 = np.random.rand(3)
653
+ ok(isinstance(r1, np.ndarray))
654
+ eq(r1.shape[0], 3)
655
+ ok(r1[0] >= 0.0)
656
+ ok(r1[0] < 1.0)
657
+ ok(r1[1] >= 0.0)
658
+ ok(r1[1] < 1.0)
659
+
660
+ r2d = np.random.rand(2, 3)
661
+ eq(r2d.shape[0], 2)
662
+ eq(r2d.shape[1], 3)
663
+
664
+ # seeded reproducibility
665
+ np.random.seed(123)
666
+ v1 = np.random.rand(1)[0]
667
+ np.random.seed(123)
668
+ v2 = np.random.rand(1)[0]
669
+ ok(close(v1, v2))
670
+
671
+ rn = np.random.randn(5)
672
+ eq(rn.shape[0], 5)
673
+
674
+ ri = np.random.randint(0, 10, 5)
675
+ eq(ri.size, 5)
676
+ ok(ri[0] >= 0)
677
+ ok(ri[0] < 10)
678
+
679
+ ru = np.random.uniform(1.0, 2.0, 4)
680
+ eq(ru.size, 4)
681
+ ok(ru[0] >= 1.0)
682
+ ok(ru[0] < 2.0)
683
+
684
+ rno = np.random.normal(0.0, 1.0, 3)
685
+ eq(rno.size, 3)
686
+
687
+ perm = np.random.permutation(5)
688
+ eq(perm.size, 5)
689
+
690
+ rc = np.random.choice(np.array([10.0, 20.0, 30.0]), 2)
691
+ eq(rc.size, 2)
692
+
693
+ # -------------------------------------------------------
694
+ # bincount / histogram
695
+ # -------------------------------------------------------
696
+ bc = np.bincount(np.array([0, 1, 1, 2, 2, 2]))
697
+ eq(bc[0], 1)
698
+ eq(bc[1], 2)
699
+ eq(bc[2], 3)
700
+
701
+ hist = np.histogram(np.array([1.0, 2.0, 3.0, 4.0, 5.0]), 5)
702
+ eq(hist[0].size, 5)
703
+ eq(hist[1].size, 6)
704
+
705
+ # -------------------------------------------------------
706
+ # searchsorted and diff
707
+ # -------------------------------------------------------
708
+ eq(np.searchsorted(np.array([1.0, 3.0, 5.0]), 2.0), 1)
709
+ eq(np.searchsorted(np.array([1.0, 3.0, 5.0]), 3.0), 1)
710
+ eq(np.searchsorted(np.array([1.0, 3.0, 5.0]), 3.0, 'right'), 2)
711
+
712
+ # -------------------------------------------------------
713
+ # linspace / logspace
714
+ # -------------------------------------------------------
715
+ ls2 = np.linspace(0.0, 100.0, 11)
716
+ eq(ls2.size, 11)
717
+ ok(close(ls2[0], 0.0))
718
+ ok(close(ls2[10], 100.0))
719
+ ok(close(ls2[5], 50.0))
720
+
721
+ log_s = np.logspace(0, 2, 3)
722
+ ok(close(log_s[0], 1.0))
723
+ ok(close(log_s[1], 10.0))
724
+ ok(close(log_s[2], 100.0))
725
+
726
+ # -------------------------------------------------------
727
+ # polyfit / polyval roundtrip
728
+ # -------------------------------------------------------
729
+ xp = np.array([0.0, 1.0, 2.0])
730
+ yp = np.array([0.0, 1.0, 4.0])
731
+ coeffs = np.polyfit(xp, yp, 2)
732
+ ok(close(np.polyval(coeffs, 0.0), 0.0, 1e-6))
733
+ ok(close(np.polyval(coeffs, 1.0), 1.0, 1e-6))
734
+ ok(close(np.polyval(coeffs, 2.0), 4.0, 1e-6))