aboutsummaryrefslogtreecommitdiff
path: root/circuitpython/extmod/ulab/tests/1d/numpy/optimize.py
blob: fce86724c08e1e4be3d5b0fb7694e37e2caf57c7 (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
import math

try:
    from ulab import scipy as spy
except ImportError:
	import scipy as spy

def f(x):
    return x**2 - 2.0

ref_result = 1.4142135623715149
result = (spy.optimize.bisect(f, 1.0, 3.0))
print(math.isclose(result, ref_result, rel_tol=1E-6, abs_tol=1E-6))

ref_result = -7.105427357601002e-15
result = spy.optimize.fmin(f, 3.0, fatol=1e-15)
print(math.isclose(result, ref_result, rel_tol=1E-6, abs_tol=1E-6))

ref_result = -7.105427357601002e-15
result = spy.optimize.fmin(f, 3.0, xatol=1e-8, fatol=1e-15, maxiter=500)
print(math.isclose(result, ref_result, rel_tol=1E-6, abs_tol=1E-6))

ref_result = 1.41421826342255
result = (spy.optimize.newton(f, 3.0, tol=0.001, rtol=0.01))
print(math.isclose(result, ref_result, rel_tol=1E-6, abs_tol=1E-6))

result = (spy.optimize.newton(f, 3.0, tol=0.001, rtol=0.01, maxiter=100))
print(math.isclose(result, ref_result, rel_tol=1E-6, abs_tol=1E-6))