Skip to content

Commit 4f3daf4

Browse files
Add NuMojo v0.4 (#36)
* added latest README * updated recipe.yaml * added numojo logo * updated recipe with tests * added test file * updated numojo to v0.4 * updated max version to 24.6 * updated max version to 24.6 * removed duplicates in tests * removed too many test functions * update recipe * removed trailing whitespace --------- Co-authored-by: Caroline Frasca <42614552+carolinefrasca@users.noreply.github.com>
1 parent 0e9c2d0 commit 4f3daf4

File tree

4 files changed

+361
-0
lines changed

4 files changed

+361
-0
lines changed

recipes/numojo/README.md

Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
<a name="readme-top"></a>
2+
<!-- add these later -->
3+
<!-- [![MIT License][license-shield]][] -->
4+
5+
<div align="center">
6+
<a href="">
7+
<img src="./numojo_logo.png" alt="Logo" width="350" height="350">
8+
</a>
9+
10+
<h1 align="center" style="font-size: 3em; color: white; font-family: 'Avenir'; text-shadow: 1px 1px orange;">NuMojo</h1>
11+
12+
<p align="center">
13+
NuMojo is a library for numerical computing in Mojo 🔥 similar to NumPy, SciPy in Python.
14+
<br />
15+
<!-- when we create docs -->
16+
<div style="font-family: 'Arial'; border: 1px solid black; padding: 5px;">
17+
<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> &nbsp; &nbsp;
18+
<a href="./docs/changelog.md"><strong>Changelog» </strong></a> &nbsp; &nbsp;
19+
<a href="https://discord.com/channels/1149778565756366939/1149778566603620455" ><strong>Check out our Discord» </strong></a>
20+
</div>
21+
<br />
22+
<div style="font-family: 'Arial'; border: 1px solid black; padding: 5px;">
23+
<a href="./docs/readme_zhs.md"><strong>中文·简» </strong></a> &nbsp;
24+
<a href="./docs/readme_zht.md"><strong>中文·繁» </strong></a> &nbsp;
25+
<a href="./docs/readme_jp.md"><strong>日本語» </strong></a>
26+
</div>
27+
<!-- <a href="./docs/readme_kr.md"><strong>한국어 문서» </strong></a> -->
28+
<!-- <br /> -->
29+
<!-- <br /> -->
30+
<!-- <a href="">View Demo</a>
31+
·
32+
<a href="">Report Bug</a>
33+
·
34+
<a href="">Request Feature</a> -->
35+
</p>
36+
</div>
37+
38+
<details>
39+
<summary>Table of Contents</summary>
40+
<ol>
41+
<li>
42+
<a href="#about-the-project">About The Project</a>
43+
<ul>
44+
<li><a href="#what-numojo-is"> What NuMojo is </a></li>
45+
<li><a href="#what-numojo-is-not">What NuMojo is not</a></li>
46+
</ul>
47+
</li>
48+
<li><a href="#goals">Goals</a></li>
49+
<li><a href="#usage">Usage</a></li>
50+
<li><a href="#how-to-install">How to install</a></li>
51+
<li><a href="#contributing">Contributing</a></li>
52+
<li><a href="#warnings">Warnings</a></li>
53+
<li><a href="#license">License</a></li>
54+
<li><a href="#acknowledgments">Acknowledgments</a></li>
55+
<li><a href="#Contributors">Contributors</a></li>
56+
</ol>
57+
</details>
58+
59+
## About the project
60+
61+
### What NuMojo is
62+
63+
NuMojo intends to capture a wide swath of numerics capability present in the Python packages NumPy, SciPy, and Scikit.
64+
65+
NuMojo intends to try and get the most out of the capabilities of Mojo including vectorization, parallelization, and GPU acceleration (once available). Currently, NuMojo extends (most of) the standard library math functions to work on array inputs.
66+
67+
We intend NuMojo to be a building block for other Mojo packages that need fast math under the hood without the added weight of a ML back and forward propagation system
68+
69+
### What NuMojo is not
70+
71+
NuMojo is not a machine learning library, it will never include back-propagation in the base library.
72+
73+
## Goals and features
74+
75+
Our main goal is to implement a fast, comprehensive numerics library in Mojo.
76+
77+
Following are some brief long-term goals. Some of them have already been implemented or partially implemented.
78+
79+
Core data types:
80+
81+
- Native n-dimensional array (`numojo.core.ndarray.NDArray`).
82+
- Native 2-dimensional array, i.e., matrix (`numojo.mat.matrix.Matrix`).
83+
- Native fixed-dimension array (to be implemented when trait parameterization is available).
84+
85+
Routines and objects:
86+
87+
- Array creation routines (`numojo.routines.creation`)
88+
- Array manipulation routines (`numojo.routines.manipulation`)
89+
- Bit-wise operations (`numojo.routines.bitwise`)
90+
- Constants (`numojo.routines.constants`)
91+
- Input and output (`numojo.routines.io`)
92+
- Text files (`numojo.routines.files`)
93+
- Text formatting options (`numojo.routines.formatting`)
94+
- Linear algebra (`numojo.routines.linalg`)
95+
- Decompositions (`numojo.routines.decompositions`)
96+
- Products of matrices and vectors (`numojo.routines.products`)
97+
- Solving (`numojo.routines.solving`)
98+
- Logic functions (`numojo.routines.logic`)
99+
- Comparison (`numojo.routines.comparison`)
100+
- Array contents (`numojo.routines.contents`)
101+
- Truth value testing (`numojo.routines.truth`)
102+
- Mathematical functions (`numojo.routines.math`)
103+
- Arithmetic operations (`numojo.routines.arithmetic`)
104+
- Exponents and logarithms (`numojo.routines.exponents`)
105+
- Extrema finding (`numojo.routines.extrema`)
106+
- Floating point routines (`numojo.routines.floating`)
107+
- Hyperbolic functions (`numojo.routines.hyper`)
108+
- Indexing (`numojo.routines.indexing`)
109+
- Miscellaneous (`numojo.routines.misc`)
110+
- Rounding (`numojo.routines.rounding`)
111+
- Sums, products, differences (`numojo.routines.sums`, `numojo.routines.products`, `numojo.routines.differences`)
112+
- Trigonometric functions (`numojo.routines.trig`)
113+
- Random sampling (`numojo.routines.random`)
114+
- Sorting, searching, and counting (`numojo.routines.sorting`, `numojo.routines.searching`)
115+
- Statistics (`numojo.routines.statistics`)
116+
- Averages and variances (`numojo.routines.averages`)
117+
- Calculus, Integration & Derivatives etc
118+
- Optimizers
119+
- Function approximators
120+
121+
Please find all the available functions and objects [here](docs/features.md).
122+
123+
For a detailed roadmap, please refer to the [docs/roadmap.md](docs/roadmap.md) file.
124+
125+
## Usage
126+
127+
An example of n-dimensional array (`NDArray` type) goes as follows.
128+
129+
```mojo
130+
import numojo as nm
131+
from numojo.prelude import *
132+
133+
134+
fn main() raises:
135+
# Generate two 1000x1000 matrices with random float64 values
136+
var A = nm.random.randn(shape=Shape(1000, 1000))
137+
var B = nm.random.randn(shape=Shape(1000, 1000))
138+
139+
# Generate a 3x2 matrix from string representation
140+
var X = nm.fromstring[f32]("[[1.1, -0.32, 1], [0.1, -3, 2.124]]")
141+
142+
# Print array
143+
print(A)
144+
145+
# Array multiplication
146+
var C = A @ B
147+
148+
# Array inversion
149+
var I = nm.inv(A)
150+
151+
# Array slicing
152+
var A_slice = A[1:3, 4:19]
153+
154+
# Get scalar from array
155+
var A_item = A[Idx(291, 141)]
156+
```
157+
158+
An example of matrix (`Matrix` type) goes as follows.
159+
160+
```mojo
161+
from numojo import mat
162+
from numojo.prelude import *
163+
164+
165+
fn main() raises:
166+
# Generate two 1000x1000 matrices with random float64 values
167+
var A = mat.rand(shape=(1000, 1000))
168+
var B = mat.rand(shape=(1000, 1000))
169+
170+
# Generate 1000x1 matrix (column vector) with random float64 values
171+
var C = mat.rand(shape=(1000, 1))
172+
173+
# Generate a 4x3 matrix from string representation
174+
var F = mat.fromstring[i8](
175+
"[[12,11,10],[9,8,7],[6,5,4],[3,2,1]]", shape=(4, 3)
176+
)
177+
178+
# Matrix slicing
179+
var A_slice = A[1:3, 4:19]
180+
var B_slice = B[255, 103:241:2]
181+
182+
# Get scalar from matrix
183+
var A_item = A[291, 141]
184+
185+
# Flip the column vector
186+
print(C[::-1, :])
187+
188+
# Sort and argsort along axis
189+
print(mat.sort(A, axis=1))
190+
print(mat.argsort(A, axis=0))
191+
192+
# Sum the matrix
193+
print(mat.sum(B))
194+
print(mat.sum(B, axis=1))
195+
196+
# Matrix multiplication
197+
print(A @ B)
198+
199+
# Matrix inversion
200+
print(A.inv())
201+
202+
# Solve linear algebra
203+
print(mat.solve(A, B))
204+
205+
# Least square
206+
print(mat.lstsq(A, C))
207+
```
208+
209+
## How to install
210+
211+
There are two approach to install and use the Numojo package.
212+
213+
### Build package
214+
215+
This approach invovles building a standalone package file `mojopkg`.
216+
217+
1. Clone the repository.
218+
2. Build the package using `mojo package numojo`
219+
3. Move the numojo.mojopkg into the directory containing the your code.
220+
221+
### Include NuMojo's path for compiler and LSP
222+
223+
This approach does not require buiding a package file. Instead, when you compile your code, you can include the path of NuMojo reporsitory with the following command:
224+
225+
```console
226+
mojo run -I "../NuMojo" example.mojo
227+
```
228+
229+
This is more flexible as you are able to edit the NuMojo source files when testing your code.
230+
231+
In order to allow VSCode LSP to resolve the imported `numojo` package, you can:
232+
233+
1. Go to preference page of VSCode.
234+
2. Go to `Mojo › Lsp: Include Dirs`
235+
3. Click `add item` and write the path where the Numojo repository is located, e.g. `/Users/Name/Programs/NuMojo`.
236+
4. Restart the Mojo LSP server.
237+
238+
Now VSCode can show function hints for the Numojo package!
239+
240+
## Contributing
241+
242+
Any contributions you make are **greatly appreciated**. For more details and guidelines on contributions, please check [here](CONTRIBUTING.md)
243+
244+
## Warnings
245+
246+
This library is still very much a work in progress and may change at any time.
247+
248+
## License
249+
250+
Distributed under the Apache 2.0 License with LLVM Exceptions. See [LICENSE](https://github.com/Mojo-Numerics-and-Algorithms-group/NuMojo/blob/main/LICENSE) and the LLVM [License](https://llvm.org/LICENSE.txt) for more information.
251+
252+
## Acknowledgements
253+
254+
Built in native [Mojo](https://github.com/modularml/mojo) which was created by [Modular](https://github.com/modularml).
255+
256+
## Contributors
257+
258+
<a href="https://github.com/Mojo-Numerics-and-Algorithms-group/NuMojo/graphs/contributors">
259+
<img src="https://contrib.rocks/image?repo=Mojo-Numerics-and-Algorithms-group/NuMojo" />
260+
</a>

recipes/numojo/numojo_logo.png

782 KB
Loading

recipes/numojo/recipe.yaml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
context:
2+
version: "0.4.0"
3+
4+
package:
5+
name: "numojo"
6+
version: ${{ version }}
7+
8+
source:
9+
- git: https://github.com/Mojo-Numerics-and-Algorithms-group/NuMojo.git
10+
rev: 13bc54b65a47ba31306dae510e61787284469332
11+
12+
build:
13+
number: 0
14+
script:
15+
- mojo package numojo -o ${{ PREFIX }}/lib/mojo/numojo.mojopkg
16+
requirements:
17+
host:
18+
- max=24.6
19+
run:
20+
- ${{ pin_compatible('max') }}
21+
22+
tests:
23+
- script:
24+
- if: unix
25+
then:
26+
- mojo run tests.mojo
27+
requirements:
28+
run:
29+
- max=24.6
30+
- numpy
31+
files:
32+
recipe:
33+
- tests.mojo
34+
35+
about:
36+
homepage: https://github.com/Mojo-Numerics-and-Algorithms-group/NuMojo
37+
license: Apache-2.0
38+
license_file: LICENSE
39+
summary: NuMojo is a library for numerical computing in Mojo 🔥 similar to NumPy, SciPy in Python.
40+
repository: https://github.com/Mojo-Numerics-and-Algorithms-group/NuMojo
41+
42+
extra:
43+
project_name: NuMojo
44+
maintainers:
45+
- shivasankarka
46+
- MadAlex1997
47+
- forFudan
48+
- sandstromviktor

recipes/numojo/tests.mojo

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import numojo as nm
2+
from numojo.prelude import *
3+
from numojo import mat
4+
from python import Python, PythonObject
5+
from testing.testing import assert_raises, assert_equal, assert_true, assert_almost_equal
6+
7+
8+
fn check[
9+
dtype: DType
10+
](array: nm.NDArray[dtype], np_sol: PythonObject, st: String) raises:
11+
var np = Python.import_module("numpy")
12+
assert_true(np.all(np.equal(array.to_numpy(), np_sol)), st)
13+
14+
15+
fn check_is_close[
16+
dtype: DType
17+
](array: nm.NDArray[dtype], np_sol: PythonObject, st: String) raises:
18+
var np = Python.import_module("numpy")
19+
assert_true(np.all(np.isclose(array.to_numpy(), np_sol, atol=0.1)), st)
20+
21+
22+
fn check_values_close[
23+
dtype: DType
24+
](value: Scalar[dtype], np_sol: PythonObject, st: String) raises:
25+
var np = Python.import_module("numpy")
26+
assert_true(np.isclose(value, np_sol, atol=0.01), st)
27+
28+
29+
def test_arange():
30+
var np = Python.import_module("numpy")
31+
check(
32+
nm.arange[nm.i64](0, 100),
33+
np.arange(0, 100, dtype=np.int64),
34+
"Arange is broken",
35+
)
36+
check(
37+
nm.arange[nm.f64](0, 100),
38+
np.arange(0, 100, dtype=np.float64),
39+
"Arange is broken",
40+
)
41+
42+
def test_linspace():
43+
var np = Python.import_module("numpy")
44+
check(
45+
nm.linspace[nm.f64](0, 100),
46+
np.linspace(0, 100, dtype=np.float64),
47+
"Linspace is broken",
48+
)
49+
50+
def main():
51+
test_arange()
52+
test_linspace()
53+

0 commit comments

Comments
 (0)