aboutsummaryrefslogtreecommitdiff
path: root/circuitpython/extmod/ulab/tests/1d/numpy/poly.py
diff options
context:
space:
mode:
authorRaghuram Subramani <raghus2247@gmail.com>2022-06-19 19:47:51 +0530
committerRaghuram Subramani <raghus2247@gmail.com>2022-06-19 19:47:51 +0530
commit4fd287655a72b9aea14cdac715ad5b90ed082ed2 (patch)
tree65d393bc0e699dd12d05b29ba568e04cea666207 /circuitpython/extmod/ulab/tests/1d/numpy/poly.py
parent0150f70ce9c39e9e6dd878766c0620c85e47bed0 (diff)
add circuitpython code
Diffstat (limited to 'circuitpython/extmod/ulab/tests/1d/numpy/poly.py')
-rw-r--r--circuitpython/extmod/ulab/tests/1d/numpy/poly.py51
1 files changed, 51 insertions, 0 deletions
diff --git a/circuitpython/extmod/ulab/tests/1d/numpy/poly.py b/circuitpython/extmod/ulab/tests/1d/numpy/poly.py
new file mode 100644
index 0000000..02ce7f5
--- /dev/null
+++ b/circuitpython/extmod/ulab/tests/1d/numpy/poly.py
@@ -0,0 +1,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))