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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
"""

On-device verification for the cross-compiled numpy wheel (OpenBLAS).



Run after installing:

    pip install numpy-2.5.2-cp312-cp312-linux_<ABI>.whl

        where <ABI> = aarch64 (real device) or x86_64 (emulator)

    (this Android build uses the "linux" platform tag for numpy)



Usage:

    python Test_NumPy.py [--quick]



Exit code 0 = everything required PASSed.

Sections marked [SKIP] are optional (e.g. need Pillow installed).



Generated by RIMI

"""
import os
import sys
import tempfile

RESULTS = []


def test(name, fn):
    try:
        fn()
        RESULTS.append((name, "PASS", None))
    except NotImplementedError as exc:
        RESULTS.append((name, "SKIP", str(exc)))
    except Exception as exc:
        RESULTS.append((name, "FAIL", "%s: %s" % (type(exc).__name__, exc)))
        print("    ! %s -> %s: %s" % (name, type(exc).__name__, exc))


def section(title):
    print("=" * 60)
    print(title)
    print("=" * 60)


WORKDIR = None


def workdir():
    global WORKDIR
    if WORKDIR is None:
        candidates = [os.environ.get("TMPDIR") or "", tempfile.gettempdir(),
                      "/storage/emulated/0/Download", os.getcwd()]
        for base in candidates:
            if not base:
                continue
            try:
                d = os.path.join(base, "test_numpy_tmp")
                os.makedirs(d, exist_ok=True)
                with open(os.path.join(d, "_probe"), "w") as fh:
                    fh.write("ok")
                WORKDIR = d
                break
            except OSError:
                continue
        if WORKDIR is None:
            WORKDIR = "."
    return WORKDIR


# ---------------------------------------------------------------------------
# 1. import / version
# ---------------------------------------------------------------------------
def import_numpy():
    import numpy as np
    print("    numpy", np.__version__)
    assert np.__version__.split(".")[0] == "2", np.__version__
    assert callable(np.show_config)


def array_basics():
    import numpy as np
    a = np.array([[1, 2, 3], [4, 5, 6]])
    assert a.shape == (2, 3)
    assert a.ndim == 2
    assert a.size == 6
    assert a.dtype == np.dtype("int64")
    assert a.itemsize == 8
    assert a.nbytes == 48


# ---------------------------------------------------------------------------
# 2. array creation
# ---------------------------------------------------------------------------
def creation():
    import numpy as np
    assert np.array([1, 2, 3]).tolist() == [1, 2, 3]
    assert np.zeros((2, 2)).sum() == 0
    assert np.ones((2, 2)).sum() == 4
    assert np.full((2,), 7.5).tolist() == [7.5, 7.5]
    assert np.eye(3).shape == (3, 3)
    assert np.arange(5).tolist() == [0, 1, 2, 3, 4]
    assert np.linspace(0, 1, 5).shape == (5,)
    assert len(np.logspace(1, 3, 3)) == 3


def random_rng():
    import numpy as np
    rng = np.random.default_rng(42)  # seeded -> reproducible
    r1 = np.random.default_rng(42)
    r2 = np.random.default_rng(42)
    assert (r1.random(5) == r2.random(5)).all()   # same seed, same stream
    assert rng.random((3, 3)).shape == (3, 3)
    assert rng.integers(0, 10, size=(2, 5)).shape == (2, 5)
    assert rng.normal(0, 1, size=(4,)).shape == (4,)


# ---------------------------------------------------------------------------
# 3. dtypes / casting
# ---------------------------------------------------------------------------
def dtypes():
    import numpy as np
    assert np.array([1, 2, 3], dtype=np.uint8).dtype == np.dtype("uint8")
    assert np.array([1.0, 2.0]).astype(np.float32).dtype == np.dtype("float32")
    assert np.array([1, 2, 3]).astype("f4").dtype == np.dtype("float32")
    for s in ("i1", "i2", "i4", "i8", "u1", "u2", "u4", "u8", "f4", "f8"):
        assert np.dtype(s)


def overflow():
    import numpy as np
    # uint8 arithmetic wraps around
    assert (np.array([200], np.uint8) + np.array([100], np.uint8))[0] == 44
    # int division floors, true division gives float
    assert np.array([5]) // 2 == np.array([2])
    assert np.array([5]) / 2 == np.array([2.5])


# ---------------------------------------------------------------------------
# 4. indexing / slicing / masking
# ---------------------------------------------------------------------------
def indexing():
    import numpy as np
    a = np.arange(12).reshape(3, 4)
    assert a[0].tolist() == [0, 1, 2, 3]
    assert a[0, 2] == 2
    assert a[:, 1].tolist() == [1, 5, 9]
    assert a[1:, :2].tolist() == [[4, 5], [8, 9]]
    assert a[-1].tolist() == [8, 9, 10, 11]
    assert a[::2].tolist() == [[0, 1, 2, 3], [8, 9, 10, 11]]


def masking():
    import numpy as np
    a = np.arange(12).reshape(3, 4)
    assert (a[a > 5] > 5).all()
    assert len(a[(a > 2) & (a < 8)]) == 5
    assert (a[a % 2 == 0] % 2 == 0).all()
    m = a.copy()
    m[m < 5] = 0
    assert m.min() == 0
    m[:, 0] = -1
    assert (m[:, 0] == -1).all()


def fancy_indexing():
    import numpy as np
    a = np.arange(12).reshape(3, 4)
    assert a[[0, 2]].shape == (2, 4)
    assert a[:, np.array([3, 1])].shape == (3, 2)


# ---------------------------------------------------------------------------
# 5. shapes / broadcasting
# ---------------------------------------------------------------------------
def reshaping():
    import numpy as np
    a = np.arange(24)
    assert a.reshape(4, 6).shape == (4, 6)
    assert a.reshape(2, 3, 4).shape == (2, 3, 4)
    assert a.reshape(-1, 6).shape == (4, 6)
    assert a.ravel().shape == (24,)
    assert a.flatten().shape == (24,)
    assert a.reshape(4, 6).T.shape == (6, 4)
    v = np.array([1, 2, 3])
    assert v[np.newaxis, :].shape == (1, 3)
    assert v[:, np.newaxis].shape == (3, 1)


def broadcasting():
    import numpy as np
    m = np.ones((3, 4))
    assert (m + 1 == 2).all()
    assert (m * np.array([10, 20, 30, 40])).shape == (3, 4)
    assert (m + np.array([[1], [2], [3]])).shape == (3, 4)
    # (3,1) * (1,4) -> (3,4)
    out = np.array([[1], [2], [3]]) * np.array([[1, 2, 3, 4]])
    assert out.shape == (3, 4)


# ---------------------------------------------------------------------------
# 6. math / reductions
# ---------------------------------------------------------------------------
def elementwise():
    import numpy as np
    a = np.array([1., 2., 3., 4.])
    assert (a + 1).tolist() == [2., 3., 4., 5.]
    assert (a ** 2).tolist() == [1., 4., 9., 16.]
    assert np.sqrt(np.array([4., 9.])).tolist() == [2., 3.]
    assert np.clip(a, 1.5, 3.5).tolist() == [1.5, 2., 3., 3.5]
    assert np.maximum(a, 2).tolist() == [2., 2., 3., 4.]


def reductions():
    import numpy as np
    a = np.array([1., 2., 3., 4.])
    assert a.sum() == 10
    assert a.mean() == 2.5
    assert a.min() == 1 and a.max() == 4
    assert a.prod() == 24
    assert a.argmax() == 3 and a.argmin() == 0
    assert np.median(a) == 2.5
    assert np.percentile(a, 50) == 2.5
    m = np.arange(6).reshape(2, 3)
    assert m.sum(axis=0).tolist() == [3, 5, 7]
    assert m.sum(axis=1).tolist() == [3, 12]


def comparisons():
    import numpy as np
    a = np.array([1., 2., 3., 4.])
    assert (a > 2).tolist() == [False, False, True, True]
    assert bool(np.any(a > 2)) is True
    assert bool(np.all(a > 2)) is False
    assert np.count_nonzero(a > 2) == 2


# ---------------------------------------------------------------------------
# 7. linear algebra (OpenBLAS accelerated)
# ---------------------------------------------------------------------------
def matmul():
    import numpy as np
    a = np.array([[1., 2.], [3., 4.]])
    b = np.array([[5., 6.], [7., 8.]])
    assert (a @ b).tolist() == [[19., 22.], [43., 50.]]
    assert np.matmul(a, b).tolist() == (a @ b).tolist()
    assert a.dot(b).tolist() == (a @ b).tolist()


def linalg():
    import numpy as np
    a = np.array([[4., 2.], [1., 3.]])
    inv = np.linalg.inv(a)
    ident = inv @ a
    assert np.allclose(ident, np.eye(2), atol=1e-10)
    assert abs(np.linalg.det(a) - 10.0) < 1e-10
    x = np.linalg.solve(a, np.array([6., 4.]))
    assert np.allclose(a @ x, [6., 4.])
    assert np.linalg.norm(np.array([3., 4.])) == 5.0
    w, v = np.linalg.eig(a)
    assert w.shape == (2,)
    assert v.shape == (2, 2)


def point_transform():
    import numpy as np
    M = np.array([[1., 0., 10.], [0., 1., 20.], [0., 0., 1.]])
    p = np.array([5., 6., 1.])
    out = M @ p
    assert out.tolist() == [15., 26., 1.]


# ---------------------------------------------------------------------------
# 8. stacking / splitting
# ---------------------------------------------------------------------------
def stacking():
    import numpy as np
    a = np.array([1, 2, 3])
    b = np.array([4, 5, 6])
    assert np.concatenate((a, b)).tolist() == [1, 2, 3, 4, 5, 6]
    assert np.stack((a, b)).shape == (2, 3)
    assert np.vstack((a, b)).shape == (2, 3)
    assert np.hstack((a, b)).shape == (6,)
    m1 = np.ones((2, 2))
    m2 = np.zeros((2, 2))
    assert np.vstack((m1, m2)).shape == (4, 2)
    assert np.hstack((m1, m2)).shape == (2, 4)


def splitting():
    import numpy as np
    x = np.arange(10)
    parts = np.split(x, 2)
    assert len(parts) == 2 and parts[0].tolist() == [0, 1, 2, 3, 4]
    assert len(np.array_split(x, 3)) == 3
    m = np.ones((4, 4))
    assert len(np.hsplit(m, 2)) == 2
    assert len(np.vsplit(m, 2)) == 2


# ---------------------------------------------------------------------------
# 9. save / load files
# ---------------------------------------------------------------------------
def save_load_npy():
    import numpy as np
    a = np.arange(12).reshape(3, 4)
    p = os.path.join(workdir(), "a.npy")
    np.save(p, a)
    b = np.load(p)
    assert (b == a).all()


def save_load_npz():
    import numpy as np
    a = np.arange(12).reshape(3, 4)
    p = os.path.join(workdir(), "data.npz")
    np.savez(p, x=a, y=a * 2)
    d = np.load(p)
    assert (d["x"] == a).all()
    assert (d["y"] == a * 2).all()
    d.close()


def save_load_text():
    import numpy as np
    a = np.arange(12).reshape(3, 4)
    p = os.path.join(workdir(), "a.csv")
    np.savetxt(p, a, delimiter=",")
    c = np.loadtxt(p, delimiter=",")
    assert c.dtype == np.float64
    assert c.shape == (3, 4)


def save_load_binary():
    import numpy as np
    a = np.arange(12).reshape(3, 4)
    p = os.path.join(workdir(), "a.bin")
    a.tofile(p)
    b = np.fromfile(p, dtype=np.int64)
    assert b.tolist() == list(range(12))


# ---------------------------------------------------------------------------
# 11. terminal printing
# ---------------------------------------------------------------------------
def printing():
    import numpy as np
    a = np.arange(12).reshape(3, 4)
    a.tolist()  # nested python lists
    prev = np.get_printoptions()
    np.set_printoptions(precision=2, threshold=20, edgeitems=3, linewidth=120,
                        suppress=True)
    print(a)
    np.set_printoptions(**prev)


# ---------------------------------------------------------------------------
# 12. everyday snippets
# ---------------------------------------------------------------------------
def snippets():
    import numpy as np
    x = np.array([3., 1., 2., 0.])
    n = (x - x.min()) / (x.max() - x.min())
    assert n.min() == 0 and n.max() == 1
    z = (x - x.mean()) / x.std()
    assert abs(z.mean()) < 1e-12
    cats = np.array([0, 2, 1, 2, 0])
    onehot = np.eye(3)[cats]
    assert onehot.shape == (5, 3)
    assert np.diag(np.arange(9).reshape(3, 3)).tolist() == [0, 4, 8]
    rng = np.random.default_rng(7)
    values, edges = np.histogram(rng.normal(size=1000), bins=20)
    assert len(values) == 20 and len(edges) == 21
    m = rng.random((5, 8))
    assert m.argmax(axis=1).shape == (5,)
    signal = np.array([1., 2., 3., 2., 1.])
    kernel = np.ones(3) / 3
    smooth = np.convolve(signal, kernel, mode="same")
    assert smooth.shape == signal.shape


def elapsed_time():
    import numpy as np
    import time
    t0 = time.perf_counter()
    big = np.arange(1_000_000)
    out = big * 2
    elapsed = time.perf_counter() - t0
    assert out.shape == big.shape
    print("    %.4f s for 1M element multiply" % elapsed)


# ---------------------------------------------------------------------------
def main():
    quick = "--quick" in sys.argv

    section("1. numpy import / version")
    test("import numpy (2.x)", import_numpy)
    test("array basics (shape/ndim/size/dtype)", array_basics)

    section("2. array creation")
    test("creation helpers", creation)
    test("default_rng seeded random", random_rng)

    section("3. dtypes / casting")
    test("dtypes and casting", dtypes)
    test("uint8 overflow / division rules", overflow)

    section("4. indexing / masking")
    test("indexing and slicing", indexing)
    test("boolean masking + assignment", masking)
    test("fancy indexing", fancy_indexing)

    section("5. shapes / broadcasting")
    test("reshape / ravel / T / newaxis", reshaping)
    test("broadcasting rules", broadcasting)

    section("6. math / reductions")
    test("element-wise ufuncs", elementwise)
    test("reductions + axes", reductions)
    test("comparisons / any / all", comparisons)

    section("7. linear algebra")
    test("matrix multiply @", matmul)
    test("inv/det/solve/eig/norm", linalg)
    test("homography point transform", point_transform)

    section("8. stacking / splitting")
    test("concatenate / stack / vstack / hstack", stacking)
    test("split / array_split / hsplit / vsplit", splitting)

    section("9. save / load files")
    test("npy roundtrip", save_load_npy)
    test("npz roundtrip", save_load_npz)
    test("savetxt / loadtxt", save_load_text)
    test("tofile / fromfile", save_load_binary)

    section("10. terminal printing")
    test("print options + tolist", printing)

    section("11. everyday snippets")
    test("normalize / zscore / one-hot / histogram", snippets)
    test("large-array perf sanity", elapsed_time)

    print()
    print("=" * 60)
    print("SUMMARY")
    print("=" * 60)
    fails = 0
    skips = 0
    for name, status, why in RESULTS:
        mark = "  OK" if status == "PASS" else (" SKIP" if status == "SKIP" else "FAIL")
        print("%s %s" % (mark, name))
        if why:
            print("        -> %s" % why)
        if status == "FAIL":
            fails += 1
        elif status == "SKIP":
            skips += 1
    print()
    passed = len(RESULTS) - fails - skips
    print("passed=%d skipped=%d failed=%d" % (passed, skips, fails))
    if fails:
        print("RESULT: FAILED")
    elif skips and not quick:
        print("RESULT: PASSED (with informational skips)")
    else:
        print("RESULT: PASSED")
    sys.exit(1 if fails else 0)


if __name__ == "__main__":
    main()