Skip to content

Commit aaa1600

Browse files
committed
Merge remote-tracking branch 'upstream/experimental' into experimental
2 parents 377b802 + 413ca6d commit aaa1600

23 files changed

+1048
-409
lines changed

README.MD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
<br />
1515
<!-- when we create docs -->
1616
<a href="https://github.com/Mojo-Numerics-and-Algorithms-group/NuMojo-Examples-and-Benchmarks/blob/main/docs/README.md"><strong>Explore the docs» </strong></a>
17+
<br>
18+
<a href="https://discord.com/channels/1149778565756366939/1149778566603620455"><strong>Check out our Discord» </strong></a>
1719
<br />
1820
<!-- <br /> -->
1921
<!-- <a href="">View Demo</a>

new_tests/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Testing guide
2+
3+
Inorder to ensure that all of our code works and that a new code doesn't break old code we need to instate a testing policy (which we can hopefully implement a CI/CD process for running).
4+
5+
Due to the similiarity with NumPy, a Numpy equivelent comparison will be our method for confirming validity.
6+
7+
The functions contained in utils_for_test.mojo `check` and `check_is_close` will be used to compare NuMojo results with NumPy equivelents.
8+
9+
Each test must be in it's own def function and begin with the word test example `test_arange`. This allows the `mojo test` command to find it. A single function can cover multiple similiar tests but they should have unique strings to identify which check failed.
10+
11+
12+

new_tests/test_array_creation.mojo

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import numojo as nm
2+
from time import now
3+
from python import Python, PythonObject
4+
from utils_for_test import check, check_is_close
5+
6+
def test_arange():
7+
var np = Python.import_module("numpy")
8+
check(nm.arange[nm.i64](0,100),np.arange(0,100,dtype=np.int64),"Arange is broken")
9+
check(nm.arange[nm.f64](0,100),np.arange(0,100,dtype=np.float64),"Arange is broken")
10+
11+
def test_linspace():
12+
var np = Python.import_module("numpy")
13+
check(nm.linspace[nm.f64](0,100),np.linspace(0,100,dtype=np.float64),"Linspace is broken")
14+
15+
def test_logspace():
16+
var np = Python.import_module("numpy")
17+
check_is_close(nm.logspace[nm.f64](0,100,5),np.logspace(0,100,5,dtype=np.float64),"Logspace is broken")
18+
19+
def test_geomspace():
20+
var np = Python.import_module("numpy")
21+
check_is_close(nm.geomspace[nm.f64](1,100,5),np.geomspace(1,100,5,dtype=np.float64),"Logspace is broken")
22+
23+
def test_zeros():
24+
var np = Python.import_module("numpy")
25+
check(nm.zeros[nm.f64](10,10,10,10),np.zeros((10,10,10,10),dtype=np.float64),"Zeros is broken")
26+
27+
def test_ones():
28+
var np = Python.import_module("numpy")
29+
check(nm.ones[nm.f64](10,10,10,10),np.ones((10,10,10,10),dtype=np.float64),"Ones is broken")
30+
31+
def test_full():
32+
var np = Python.import_module("numpy")
33+
check(nm.full[nm.f64](10,10,10,10,fill_value=10),np.full((10,10,10,10),10,dtype=np.float64),"Full is broken")
34+
35+
def test_identity():
36+
var np = Python.import_module("numpy")
37+
check(nm.identity[nm.i64](100),np.identity(100,dtype=np.int64),"Identity is broken")
38+
39+
def test_eye():
40+
var np = Python.import_module("numpy")
41+
check(nm.eye[nm.i64](100,100),np.eye(100,100,dtype=np.int64),"Eye is broken")
42+
def main():
43+
var np = Python.import_module("numpy")
44+
var arr = nm.arange[nm.f64](0,100)
45+
arr.reshape(10,10)
46+
var np_arr = np.arange(0,100).reshape(10,10)
47+
# Arange like flat arrays
48+
# check(nm.arange[nm.i64](0,100),np.arange(0,100,dtype=np.int64),"Arange is broken")
49+
# check(nm.linspace[nm.i64](0,100),np.linspace(0,100,dtype=np.float64),"Linspace is broken")
50+
# check_is_close(nm.logspace[nm.i64](0,100,5),np.logspace(0,100,5,dtype=np.float64),"Logspace is broken")
51+
# check_is_close(nm.geomspace[nm.i64](1,100,5),np.geomspace(1,100,5,dtype=np.float64),"Logspace is broken")
52+
# print((arr@arr).to_numpy()-np.matmul(np_arr,np_arr))
53+
print(nm.matmul_naive[nm.f64](arr,arr).to_numpy())#-np.matmul(np_arr,np_arr))
54+
print(np.matmul(np_arr,np_arr))
55+
# # Basic ND arrays
56+
# print(nm.sin[nm.f64](nm.arange[nm.f64](0,15)))
57+
# print( np.sin(np.arange(0,15, dtype=np.float64)))
58+
# check(nm.zeros[nm.f64](10,10,10,10),np.zeros((10,10,10,10),dtype=np.float64),"Zeros is broken")
59+
# check(nm.ones[nm.f64](10,10,10,10),np.ones((10,10,10,10),dtype=np.float64),"Ones is broken")
60+
# check(nm.full[nm.f64](10,10,10,10,fill_value=10),np.full((10,10,10,10),10,dtype=np.float64),"Full is broken")
61+
62+
# # 2d Linalg related arrays
63+
64+
# check(nm.identity[nm.i64](100),np.identity(100,dtype=np.int64),"Identity is broken")
65+
# check(nm.eye[nm.i64](100,100),np.eye(100,100,dtype=np.int64),"Eye is broken")
66+
67+
68+

new_tests/test_math.mojo

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import numojo as nm
2+
from time import now
3+
from python import Python, PythonObject
4+
from utils_for_test import check, check_is_close
5+
6+
7+
def test_add_array():
8+
var np = Python.import_module("numpy")
9+
var arr = nm.arange[nm.f64](0,15)
10+
11+
check(nm.add[nm.f64](arr,5.0),np.arange(0,15)+5,"Add array + scalar")
12+
check(nm.add[nm.f64](arr,arr),np.arange(0,15)+np.arange(0,15),"Add array + array")
13+
14+
def test_add_array_par():
15+
var np = Python.import_module("numpy")
16+
var arr = nm.arange[nm.f64](0,500)
17+
18+
check(nm.add[nm.f64,backend=nm.math.math_funcs.VectorizedParallelizedNWorkers[6]](arr,5.0),np.arange(0,500)+5,"Add array + scalar")
19+
check(nm.add[nm.f64,nm.math.math_funcs.VectorizedParallelizedNWorkers[6]](arr,arr),np.arange(0,500)+np.arange(0,500),"Add array + array")
20+
21+
def test_sin():
22+
var np = Python.import_module("numpy")
23+
var arr = nm.arange[nm.f64](0,15)
24+
25+
check_is_close(nm.sin[nm.f64](arr),np.sin(np.arange(0,15)),"Add array + scalar")
26+
27+
def test_sin_par():
28+
var np = Python.import_module("numpy")
29+
var arr = nm.arange[nm.f64](0,15)
30+
31+
check_is_close(nm.sin[nm.f64, backend=nm .math.math_funcs.VectorizedParallelizedNWorkers[6]](arr),np.sin(np.arange(0,15)),"Add array + scalar")
32+
33+
def test_matmul():
34+
var np = Python.import_module("numpy")
35+
var arr = nm.arange[nm.f64](0,100)
36+
arr.reshape(10,10)
37+
var np_arr = np.arange(0,100).reshape(10,10)
38+
check_is_close(arr@arr,np.matmul(np_arr,np_arr),"Dunder matmul is broken")
39+
# The only matmul that currently works is par (__matmul__)
40+
# check_is_close(nm.matmul_tiled_unrolled_parallelized(arr,arr),np.matmul(np_arr,np_arr),"TUP matmul is broken")
41+

new_tests/utils_for_test.mojo

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from python import Python, PythonObject
2+
from testing.testing import assert_true
3+
import numojo as nm
4+
5+
6+
fn check(array:nm.NDArray,np_sol:PythonObject,st:String)raises:
7+
var np = Python.import_module("numpy")
8+
assert_true(np.all(np.equal(array.to_numpy(),np_sol)),st)
9+
10+
fn check_is_close(array:nm.NDArray,np_sol:PythonObject,st:String)raises:
11+
var np = Python.import_module("numpy")
12+
assert_true(np.all(np.isclose(array.to_numpy(),np_sol)),st)

0 commit comments

Comments
 (0)