added some examples for ulab module - see https://micropython-ulab.readthedocs.io/en/stable/index.html for full documentation

This commit is contained in:
Sylvain PILLOT 2024-02-13 22:33:53 +01:00
parent e24765781d
commit c66299aa5d
12 changed files with 145 additions and 6 deletions

View File

@ -1,6 +1,3 @@
# PacMan par Kevin FEDYNA
# https://nsi.xyz/numapps/pac-man-en-python-numworks/
from kandinsky import fill_rect, draw_string
from ion import keydown
from math import sqrt

View File

@ -1,6 +1,3 @@
# Snake from Golem64
# https://my.numworks.com/python/golem64/snake
#Version 1.7 STABLE
#Tip: You should try to press
#some keys in the menu...

View File

@ -0,0 +1,17 @@
# code to be run in micropython
from ulab import numpy as np
x = np.linspace(0, 10, num=1024)
y = np.sin(x)
z = np.zeros(len(x))
a, b = np.fft.fft(x)
print('real part:\t', a)
print('\nimaginary part:\t', b)
c, d = np.fft.fft(x, z)
print('\nreal part:\t', c)
print('\nimaginary part:\t', d)

View File

@ -0,0 +1,19 @@
from ulab import numpy as np
array = np.array([
[3, 7, 1],
[10, 3, 2],
[5, 6, 7]
])
print(array)
print()
# Sort the whole array
print(np.sort(array, axis=None))
# Sort along each row
print(np.sort(array, axis=1))
# Sort along each column
print(np.sort(array, axis=0))

View File

@ -0,0 +1,13 @@
from ulab import numpy as np
list = [
np.array([3, 2, 8, 9]),
np.array([4, 12, 34, 25, 78]),
np.array([23, 12, 67])
]
result = []
for i in range(len(list)):
result.append(np.mean(list[i]))
print(result)

View File

@ -0,0 +1,14 @@
# code to be run in micropython
from ulab import numpy as np
a = np.array([1, 2, 3, 4], dtype=np.uint8)
b = np.array([1+1j, 2-2j, 3+3j, 4-4j], dtype=np.complex)
print('a:\t\t', a)
print('conjugate(a):\t', np.conjugate(a))
print()
print('b:\t\t', b)
print('conjugate(b):\t', np.conjugate(b))

View File

@ -0,0 +1,13 @@
# code to be run in micropython
from ulab import numpy as np
a = np.array([1, 2, 3, 4, 5])
print("a: \t", a)
print("a flipped:\t", np.flip(a))
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=np.uint8)
print("\na flipped horizontally\n", np.flip(a, axis=1))
print("\na flipped vertically\n", np.flip(a, axis=0))
print("\na flipped horizontally+vertically\n", np.flip(a))

View File

@ -0,0 +1,16 @@
# code to be run in micropython
from ulab import numpy as np
m = np.array([[1, 2, 3], [4, 5, 6], [7, 10, 9]], dtype=np.uint8)
n = np.linalg.inv(m)
print("m:\n", m)
print("\nm^-1:\n", n)
# this should be the unit matrix
print("\nm*m^-1:\n", np.dot(m, n))
m = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], dtype=np.uint8)
n = np.array([[1, 2], [3, 4], [5, 6], [7, 8]], dtype=np.uint8)
print(m)
print(n)
print(np.dot(m, n))

View File

@ -0,0 +1,13 @@
# code to be run in micropython
from ulab import numpy as np
a = np.array([1, 2, 3], dtype=np.uint16)
print("a:\t\t", a)
print("imag(a):\t", np.imag(a))
b = np.array([1, 2+1j, 3-1j], dtype=np.complex)
print("\nb:\t\t", b)
print("imag(b):\t", np.imag(b))

View File

@ -0,0 +1,18 @@
# code to be run in micropython
from ulab import numpy as np
from ulab import scipy as spy
a = np.array([[3, 2, 1, 0], [2, 1, 0, 1], [1, 0, 1, 4], [1, 2, 1, 8]])
b = np.array([4, 2, 4, 2])
print('a:\n')
print(a)
print('\nb: ', b)
x = spy.linalg.solve_triangular(a, b, lower=True)
print('='*20)
print('x: ', x)
print('\ndot(a, x): ', np.dot(a, x))

View File

@ -0,0 +1,10 @@
try:
from ulab import numpy
from ulab import scipy
except ImportError:
import numpy
import scipy.special
x = numpy.array([1, 2, 3])
scipy.special.erf(x)

View File

@ -0,0 +1,12 @@
import ulab
print('you are running ulab version', ulab.__version__)
version = ulab.__version__
version_dims = version.split('-')[1]
version_num = int(version_dims.replace('D', ''))
print('version string: ', version)
print('version dimensions: ', version_dims)
print('numerical value of dimensions: ', version_num)