Mostly by providing a "numpy shim" for CircuitPython, try to make the numpy tests run on all three systems. (a "scipy shim" might also be useful?) However, there are test failures. Is it worth working through them and getting this to a point where it could be included?
56 lines
1.6 KiB
Python
56 lines
1.6 KiB
Python
import math
|
|
try:
|
|
import numpy as np
|
|
except:
|
|
import ulab as np
|
|
|
|
try:
|
|
from math import isclose
|
|
except:
|
|
def isclose(a, b, *, rel_tol=1e-9, abs_tol=0):
|
|
return abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)
|
|
|
|
p = [1, 1, 1, 0]
|
|
x = [0, 1, 2, 3, 4]
|
|
result = np.polyval(p, x)
|
|
ref_result = np.array([0, 3, 14, 39, 84])
|
|
for i in range(len(x)):
|
|
print(isclose(result[i], ref_result[i], rel_tol=1E-9, abs_tol=1E-9))
|
|
|
|
a = np.array(x)
|
|
result = np.polyval(p, a)
|
|
ref_result = np.array([0, 3, 14, 39, 84])
|
|
for i in range(len(x)):
|
|
print(isclose(result[i], ref_result[i], rel_tol=1E-9, abs_tol=1E-9))
|
|
|
|
# linear fit
|
|
x = np.linspace(-10, 10, 20)
|
|
y = 1.5*x + 3
|
|
result = np.polyfit(x, y, 1)
|
|
ref_result = np.array([ 1.5, 3.0])
|
|
for i in range(2):
|
|
print(isclose(result[i], ref_result[i], rel_tol=1E-9, abs_tol=1E-9))
|
|
|
|
# 2nd degree fit
|
|
x = np.linspace(-10, 10, 20)
|
|
y = x*x*2.5 - x*0.5 + 1.2
|
|
result = np.polyfit(x, y, 2)
|
|
ref_result = np.array([2.5, -0.5, 1.2])
|
|
for i in range(3):
|
|
print(isclose(result[i], ref_result[i], rel_tol=1E-9, abs_tol=1E-9))
|
|
|
|
# 3rd degree fit
|
|
x = np.linspace(-10, 10, 20)
|
|
y = x*x*x*1.255 + x*x*1.0 - x*0.75 + 0.0
|
|
result = np.polyfit(x, y, 3)
|
|
ref_result = np.array([1.255, 1.0, -0.75, 0.0])
|
|
for i in range(4):
|
|
print(isclose(result[i], ref_result[i], rel_tol=1E-9, abs_tol=1E-9))
|
|
|
|
# 4th degree fit
|
|
x = np.linspace(-10, 10, 20)
|
|
y = x*x*x*x + x*x*x*1.255 + x*x*1.0 - x*0.75 + 0.0
|
|
result = np.polyfit(x, y, 4)
|
|
ref_result = np.array([1.0, 1.255, 1.0, -0.75, 0.0])
|
|
for i in range(5):
|
|
print(isclose(result[i], ref_result[i], rel_tol=1E-9, abs_tol=1E-9))
|