aboutsummaryrefslogtreecommitdiff
path: root/circuitpython/extmod/ulab/tests/1d/numpy/poly.py
blob: 02ce7f5bee254fc659ed429fbcfd1e1a5a71775a (plain)
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
import math

try:
    from ulab import numpy as np
except ImportError:
    import numpy as np
    
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(math.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(math.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(math.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(math.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(math.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(math.isclose(result[i], ref_result[i], rel_tol=1E-9, abs_tol=1E-9))