rumpy 0.1.1

A reimplementation of NumPy for use with rustpython in embedded interpreters
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
//! Tests for the embedded pure-Python submodules:
//! numpy.typing, numpy.exceptions, numpy.version,
//! numpy.compat, numpy.doc, numpy.core, numpy.ctypeslib,
//! numpy.char, numpy.rec, numpy.dtypes.

use rustpython_vm::Interpreter;

fn interp() -> Interpreter {
    let b = Interpreter::builder(Default::default());
    let def = rumpy::module_def(&b.ctx);
    b.add_native_module(def).build()
}

/// Register every pure-Python child of `numpy` in `sys.modules` under its
/// dotted name so `from numpy.<sub> import …` works through the import
/// machinery. Submodules that fail to initialize (e.g. because they need
/// a stdlib module we don't ship) are silently skipped — the tests that
/// actually exercise them will surface the failure directly.
fn run(source: &str) {
    let interp = interp();
    interp.enter(|vm| {
        let numpy_mod = vm.import("numpy", 0).expect("import numpy");
        let sys_modules = vm
            .sys_module
            .get_attr("modules", vm)
            .expect("sys.modules");
        for sub in [
            "typing",
            "exceptions",
            "version",
            "compat",
            "doc",
            "core",
            "ctypeslib",
            "char",
            "rec",
            "dtypes",
            "testing",
            "emath",
            "polynomial",
            "strings",
        ] {
            if let Ok(m) = numpy_mod.get_attr(sub, vm) {
                let dotted = format!("numpy.{sub}");
                let _ = sys_modules.set_item(dotted.as_str(), m, vm);
            }
        }

        let scope = vm.new_scope_with_builtins();
        let code = vm
            .compile(source, rustpython_vm::compiler::Mode::Exec, "<t>".into())
            .expect("compile");
        if let Err(e) = vm.run_code_obj(code, scope) {
            let mut s = String::new();
            let _ = vm.write_exception(&mut s, &e);
            panic!("run failed:\n{s}");
        }
    });
}

#[test]
fn typing_module_attribute_access() {
    run(
        r#"
import numpy
t = numpy.typing
assert t.NDArray is not None
assert t.ArrayLike is not None
assert t.DTypeLike is not None
"#,
    );
}

#[test]
fn typing_from_import() {
    run(
        r#"
from numpy.typing import NDArray, ArrayLike, DTypeLike
assert NDArray is not None
assert ArrayLike is not None
assert DTypeLike is not None
"#,
    );
}

#[test]
fn typing_ndarray_subscript_returns_ndarray() {
    run(
        r#"
import numpy
NDArray = numpy.typing.NDArray
# NDArray[T] returns numpy.ndarray (the real class)
assert NDArray[float] is numpy.ndarray
assert NDArray[int]   is numpy.ndarray
"#,
    );
}

#[test]
fn typing_all_export() {
    run(
        r#"
import numpy
exported = set(numpy.typing.__all__)
assert exported == {"NDArray", "ArrayLike", "DTypeLike", "NBitBase"}, exported
"#,
    );
}


#[test]
fn exceptions_axis_error_subclasses_value_and_index_error() {
    run(
        r#"
from numpy.exceptions import AxisError
e = AxisError(2, ndim=1)
assert isinstance(e, ValueError)
assert isinstance(e, IndexError)
assert e.axis == 2
assert e.ndim == 1
assert "out of bounds" in str(e)
"#,
    );
}

#[test]
fn exceptions_full_set_present() {
    run(
        r#"
from numpy.exceptions import (
    AxisError,
    ComplexWarning,
    RankWarning,
    TooHardError,
    VisibleDeprecationWarning,
    DTypePromotionError,
)
assert issubclass(ComplexWarning, RuntimeWarning)
assert issubclass(RankWarning, UserWarning)
assert issubclass(TooHardError, RuntimeError)
assert issubclass(VisibleDeprecationWarning, UserWarning)
assert issubclass(DTypePromotionError, TypeError)
"#,
    );
}


#[test]
fn version_has_strings_and_release_flag() {
    run(
        r#"
import numpy
v = numpy.version
assert isinstance(v.version, str)
assert isinstance(v.full_version, str)
assert isinstance(v.short_version, str)
assert isinstance(v.git_revision, str)
assert v.release in (True, False)
# host injects the crate version, so these three agree.
assert v.version == v.full_version == v.short_version
"#,
    );
}


#[test]
fn compat_exposes_legacy_aliases() {
    run(
        r#"
from numpy.compat import unicode, long, basestring, asbytes, asstr, asunicode
assert unicode is str
assert long is int
assert isinstance(basestring, tuple)
assert asbytes("x") == b"x"
assert asstr(b"x") == "x"
assert asunicode("x") == "x"
"#,
    );
}

#[test]
fn doc_module_is_importable() {
    run(
        r#"
import numpy.doc
assert hasattr(numpy.doc, "__all__")
assert numpy.doc.__all__ == []
"#,
    );
}

#[test]
fn core_module_is_importable() {
    run(
        r#"
import numpy.core
assert hasattr(numpy.core, "__all__")
"#,
    );
}

#[test]
fn ctypeslib_stubs_raise_not_implemented() {
    run(
        r#"
from numpy.ctypeslib import as_array, ndpointer, load_library
for fn in (as_array, ndpointer, load_library):
    try:
        fn()
    except NotImplementedError:
        pass
    else:
        raise AssertionError("expected NotImplementedError")
"#,
    );
}


#[test]
fn char_basic_string_ops() {
    run(
        r#"
import numpy.char as c
assert c.upper(["foo", "bar"]) == ["FOO", "BAR"]
assert c.lower(["FOO", "BAR"]) == ["foo", "bar"]
assert c.add(["a", "b"], ["1", "2"]) == ["a1", "b2"]
assert c.multiply(["ab", "cd"], 2) == ["abab", "cdcd"]
assert c.strip(["  foo  ", "\tbar\n"]) == ["foo", "bar"]
assert c.replace(["hello"], "l", "L") == ["heLLo"]
assert c.startswith(["abc", "xyz"], "ab") == [True, False]
assert c.count(["abcabc"], "b") == [2]
assert c.str_len(["", "abc"]) == [0, 3]
"#,
    );
}

#[test]
fn char_comparison_ops() {
    run(
        r#"
import numpy.char as c
assert c.equal(["a", "b"], ["a", "c"]) == [True, False]
assert c.not_equal(["a"], ["b"]) == [True]
assert c.less(["a"], ["b"]) == [True]
assert c.greater_equal(["b"], ["b"]) == [True]
"#,
    );
}


#[test]
fn rec_fromstring_parses_typed_buffer() {
    run(
        r#"
import numpy.rec as r

def le_int(v, n):
    return (v & ((1 << (8 * n)) - 1)).to_bytes(n, "little")

packed = (
    le_int(1, 4) + bytes.fromhex("000000000000F83F") + b"abc"
    + le_int(2, 4) + bytes.fromhex("0000000000000240") + b"de\x00"
)
arr = r.fromstring(packed, formats="i4,f8,S3", names="id,weight,tag")
assert len(arr) == 2
assert arr[0].id == 1
assert abs(arr[0].weight - 1.5) < 1e-12
assert arr[0].tag == b"abc"
assert arr[1].id == 2
assert abs(arr[1].weight - 2.25) < 1e-12
assert arr[1].tag == b"de"

be_packed = (1).to_bytes(4, "big") + bytes.fromhex("3FF8000000000000") + b"xyz"
arr2 = r.fromstring(be_packed, formats="i4,f8,S3",
                    names="id,weight,tag", byteorder=">")
assert arr2[0].id == 1
assert abs(arr2[0].weight - 1.5) < 1e-12
"#,
    );
}


#[test]
fn dtypes_classes_carry_names() {
    run(
        r#"
from numpy.dtypes import (
    BoolDType, Int8DType, Int32DType, Int64DType,
    Float32DType, Float64DType, Complex128DType,
)
assert BoolDType().name == "bool"
assert Int8DType().name == "int8"
assert Int32DType().name == "int32"
assert Int64DType().name == "int64"
assert Float32DType().name == "float32"
assert Float64DType().name == "float64"
assert Complex128DType().name == "complex128"

# repr/str/eq round-trip
assert repr(Float64DType()) == "dtype('float64')"
assert str(Int32DType()) == "int32"
assert Int32DType() == Int32DType()
assert Int32DType() == "int32"
assert hash(Int32DType()) == hash(Int32DType())
"#,
    );
}


#[test]
fn ma_count_along_axis() {
    run(
        r#"
import numpy
ma = numpy.ma

m = ma.masked_array(
    [[1, 2, 3], [4, 5, 6]],
    mask=[[False, True, False], [True, False, False]],
)
assert m.count() == 4
got0 = m.count(axis=0)
assert got0.tolist() == [1, 1, 2]
got1 = m.count(axis=1)
assert got1.tolist() == [2, 2]
"#,
    );
}


#[test]
fn testing_assert_equal_and_array_equal() {
    run(
        r#"
import numpy.testing as t
t.assert_equal(1, 1)
t.assert_equal([1, 2, 3], [1, 2, 3])
t.assert_array_equal([[1, 2], [3, 4]], [[1, 2], [3, 4]])
try:
    t.assert_equal([1, 2], [1, 3])
except AssertionError:
    pass
else:
    raise AssertionError("expected mismatch")
"#,
    );
}

#[test]
fn testing_assert_allclose_handles_tol_and_nan() {
    run(
        r#"
import numpy.testing as t
t.assert_allclose([1.0, 2.0], [1.0 + 1e-9, 2.0])
t.assert_allclose([float("nan")], [float("nan")])
try:
    t.assert_allclose([1.0], [1.5])
except AssertionError:
    pass
else:
    raise AssertionError("expected close failure")
"#,
    );
}

#[test]
fn testing_assert_raises_and_less() {
    run(
        r#"
import numpy.testing as t
t.assert_raises(ValueError, int, "abc")
t.assert_array_less([1, 2], [2, 3])
try:
    t.assert_array_less([1, 3], [2, 3])
except AssertionError:
    pass
else:
    raise AssertionError("expected less failure")
"#,
    );
}


#[test]
fn emath_sqrt_promotes_negative_reals() {
    run(
        r#"
import numpy.emath as e
# Real-domain sqrt for non-negatives.
assert e.sqrt(4) == 2.0
# Negative reals get a complex answer.
r = e.sqrt(-1)
assert isinstance(r, complex)
assert abs(r - 1j) < 1e-12
# Array variant maps element-wise.
out = e.sqrt([4, -4])
assert out[0] == 2.0
assert abs(out[1] - 2j) < 1e-12
"#,
    );
}

#[test]
fn emath_log_family() {
    run(
        r#"
import numpy.emath as e
# log of negative is complex.
r = e.log(-1)
assert isinstance(r, complex)
# Real-domain log returns float.
assert abs(e.log(1) - 0.0) < 1e-12
assert abs(e.log10(100) - 2.0) < 1e-12
assert abs(e.log2(8) - 3.0) < 1e-12
assert abs(e.logn(3, 27) - 3.0) < 1e-12
"#,
    );
}

#[test]
fn emath_inverse_trig_extends_domain() {
    run(
        r#"
import numpy.emath as e
# Inside the real domain — real answer.
inside = e.arccos(0.5)
assert isinstance(inside, float)
inside = e.arcsin(0.5)
assert isinstance(inside, float)
# Outside — complex answer.
r = e.arccos(2)
assert isinstance(r, complex)
r = e.arcsin(2)
assert isinstance(r, complex)
r = e.arctanh(2)
assert isinstance(r, complex)
"#,
    );
}


#[test]
fn polynomial_eval_and_arith() {
    run(
        r#"
from numpy.polynomial import Polynomial
# p(x) = 1 + 2x + 3x^2
p = Polynomial([1, 2, 3])
assert p(0) == 1
assert p(1) == 6
assert p(2) == 17
# Vector input.
assert p([0, 1, 2]) == [1, 6, 17]
# Arithmetic.
q = Polynomial([0, 1])  # x
assert (p + q).coef == [1, 3, 3]
assert (p - q).coef == [1, 1, 3]
assert (q * q).coef == [0, 0, 1]
assert (-p).coef == [-1, -2, -3]
assert (p + 5).coef == [6, 2, 3]
"#,
    );
}

#[test]
fn polynomial_deriv_integ_and_degree() {
    run(
        r#"
from numpy.polynomial import Polynomial
p = Polynomial([1, 2, 3])  # 1 + 2x + 3x^2
assert p.degree == 2
d = p.deriv()
assert d.coef == [2, 6]
# d/dx of the integral returns the original (up to integration constant).
i = p.integ(k=0)
# integ adds a leading constant 0.
assert i.coef[0] == 0
"#,
    );
}

#[test]
fn polynomial_roots_recover_factors() {
    run(
        r#"
from numpy.polynomial import Polynomial, polyroots
# (x - 1)(x - 2) = 2 - 3x + x^2
roots = polyroots([2, -3, 1])
roots = sorted([r.real for r in roots])
assert abs(roots[0] - 1.0) < 1e-6
assert abs(roots[1] - 2.0) < 1e-6
"#,
    );
}

#[test]
fn polynomial_fit_recovers_known_polynomial() {
    run(
        r#"
from numpy.polynomial import Polynomial
xs = [0, 1, 2, 3, 4]
# 1 + 2x + 3x^2 evaluated at xs.
ys = [1 + 2*x + 3*x*x for x in xs]
p = Polynomial.fit(xs, ys, 2)
assert abs(p.coef[0] - 1.0) < 1e-6
assert abs(p.coef[1] - 2.0) < 1e-6
assert abs(p.coef[2] - 3.0) < 1e-6
"#,
    );
}


#[test]
fn strings_basic_ops() {
    run(
        r#"
import numpy.strings as s
assert s.upper(["foo", "bar"]) == ["FOO", "BAR"]
assert s.add(["a"], ["b"]) == ["ab"]
assert s.startswith(["abc", "xyz"], "ab") == [True, False]
assert s.equal(["a"], ["a"]) == [True]
assert s.str_len(["", "abc"]) == [0, 3]
"#,
    );
}


#[test]
fn rec_construct_and_field_access() {
    run(
        r#"
import numpy.rec as r
arr = r.fromarrays([[1, 2, 3], [10.0, 20.0, 30.0]], names="id, value")
assert len(arr) == 3
assert arr.id == [1, 2, 3]
assert arr.value == [10.0, 20.0, 30.0]
assert arr.names == ("id", "value")
# Row access.
row = arr[0]
assert row.id == 1
assert row.value == 10.0
assert row["id"] == 1
assert row[0] == 1
"#,
    );
}

#[test]
fn rec_fromrecords_and_mutation() {
    run(
        r#"
import numpy.rec as r
arr = r.fromrecords([(1, "a"), (2, "b")], names="i, s")
assert arr[1].s == "b"
arr.i = [10, 20]
assert arr.i == [10, 20]
arr[0].s = "z"
assert arr[0].s == "z"
"#,
    );
}

#[test]
fn rec_helpers_and_format_parser() {
    run(
        r#"
import numpy.rec as r
assert r.find_duplicate(["a", "b", "a", "c", "b"]) == ["a", "b"]
fp = r.format_parser(["f8", "i4"], "x, y")
assert fp.names == ["x", "y"]
"#,
    );
}

#[test]
fn dtypes_all_listed() {
    run(
        r#"
import numpy.dtypes as d
expected_prefix = [
    "Bool",
    "Int8", "Int16", "Int32", "Int64",
    "UInt8", "UInt16", "UInt32", "UInt64",
    "Float16", "Float32", "Float64",
    "Complex64", "Complex128",
    "Str", "Bytes", "Object",
]
for p in expected_prefix:
    cls_name = f"{p}DType"
    assert cls_name in d.__all__, cls_name
    assert hasattr(d, cls_name), cls_name
"#,
    );
}