aboutsummaryrefslogtreecommitdiff
path: root/circuitpython/extmod/ulab/tests/2d/scipy/cho_solve.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/2d/scipy/cho_solve.py
parent0150f70ce9c39e9e6dd878766c0620c85e47bed0 (diff)
add circuitpython code
Diffstat (limited to 'circuitpython/extmod/ulab/tests/2d/scipy/cho_solve.py')
-rw-r--r--circuitpython/extmod/ulab/tests/2d/scipy/cho_solve.py29
1 files changed, 29 insertions, 0 deletions
diff --git a/circuitpython/extmod/ulab/tests/2d/scipy/cho_solve.py b/circuitpython/extmod/ulab/tests/2d/scipy/cho_solve.py
new file mode 100644
index 0000000..57643c8
--- /dev/null
+++ b/circuitpython/extmod/ulab/tests/2d/scipy/cho_solve.py
@@ -0,0 +1,29 @@
+import math
+
+try:
+ from ulab import scipy, numpy as np
+except ImportError:
+ import scipy
+ import numpy as np
+
+## test cholesky solve
+L = np.array([[3, 0, 0, 0], [2, 1, 0, 0], [1, 0, 1, 0], [1, 2, 1, 8]])
+b = np.array([4, 2, 4, 2])
+
+# L needs to be a lower triangular matrix
+result = scipy.linalg.cho_solve(L, b)
+ref_result = np.array([-0.01388888888888906, -0.6458333333333331, 2.677083333333333, -0.01041666666666667])
+
+for i in range(4):
+ print(math.isclose(result[i], ref_result[i], rel_tol=1E-6, abs_tol=1E-6))
+
+## test cholesky and cho_solve together
+C = np.array([[18, 22, 54, 42], [22, 70, 86, 62], [54, 86, 174, 134], [42, 62, 134, 106]])
+L = np.linalg.cholesky(C)
+
+# L is a lower triangular matrix obtained by performing cholesky of positive-definite linear system
+result = scipy.linalg.cho_solve(L, b)
+ref_result = np.array([6.5625, 1.1875, -2.9375, 0.4375])
+
+for i in range(4):
+ print(math.isclose(result[i], ref_result[i], rel_tol=1E-6, abs_tol=1E-6))