Commit db4da8fa authored by Katter, Janike Yvonne's avatar Katter, Janike Yvonne
Browse files

Add some python examples that can be executed in build

parent 11479616
Pipeline #35101 failed with stage
in 14 seconds
import swig.pylmfit as pylm
def f(t, p):
return p[0] + p[1]*t + p[2]*t**2
def curve1():
par0 = pylm.VectorDouble([100, 0, -10])
t = pylm.VectorDouble([-4, -3, -2, -1, 0, 1, 2, 3, 4])
y = pylm.VectorDouble([16.6, 9.9, 4.4, 1.1, 0, 1.1, 4.2, 9.3, 16.4])
control = pylm.lm_control_double
pylm.set_parlen(3)
pylm.set_callback(f)
print("Fitting...")
result = pylm.fit_curve(par0, t, y, pylm.function_g, control)
print("Results:")
print(f"status after {result.status.nfev} function evaluations:")
pylm.print_infmsg(result.status.outcome)
print("Obtained parameters:")
for j in range(pylm.parlen()):
print(f"par[{j}] = {result.par[j]}")
print("Obtained norm:")
print(result.status.fnorm)
print("fitting data as follows:")
for i in range(t.size()):
fi = f(t[i], result.par)
print(f"t[{i}] = {t[i]} y = {y[i]} fit = {fi} residue = {y[i]-fi}")
if (result.status.outcome > 3):
print("FAILURE")
return 1
print("SUCCESS")
return 0
if __name__ == "__main__":
print(curve1())
import swig.pylmfit as pylm
import sys
def eval_nonlin(p, data=None):
f0 = p[0]**2 + p[1]**2 - 1
f1 = p[1] - p[0]**2
return ([f0, f1], 0)
def nonlin1(p0, p1):
par0 = pylm.VectorDouble([p0, p1])
control = pylm.lm_control_double
pylm.set_parlen(2)
pylm.set_callback(eval_nonlin)
print("Minimization:")
result = pylm.minimize(par0, None, 2, pylm.function_eval, control)
print(f"status after {result.status.nfev} function evaluations:")
pylm.print_infmsg(result.status.outcome)
print("\nSolution:")
print(f" x = {result.par[0]}")
print(f" y = {result.par[1]}")
stat_str = "valid, though not the only solution: try other starting values"
if (result.status.fnorm >= control.ftol):
stat_str = "not a valid solution, try other starting values"
print(f" d = {result.status.fnorm} => {stat_str}")
if __name__ == "__main__":
if (len(sys.argv) < 3):
print("usage: python nonlin1.py x_start y_start")
else:
p0 = float(sys.argv[1])
p1 = float(sys.argv[2])
nonlin1(p0, p1)
import swig.pylmfit as pylm
class Data_t(object):
y: list()
tx: list()
tz: list()
def __init__(self, y, tx, tz):
self.y = y
self.tx = tx
self.tz = tz
def f(self, tx, tz, p):
return p[0] + p[1]*tx + p[2]*tz
def eval_surface(p, data=None):
fvec = []
for i in range(4):
fvec.append(data.y[i] - data.f(data.tx[i], data.tz[i], p))
return (fvec, 0)
def surface1():
par0 = pylm.VectorDouble([-1, 0, 1])
tx = [-1, -1, 1, 1]
tz = [-1, 1, -1, 1]
y = [0, 1, 1, 2]
data = Data_t(y, tx, tz)
wrap = pylm.Data_Wrap(data)
pylm.set_parlen(3)
pylm.set_callback(eval_surface)
control = pylm.lm_control_double
print("Fitting:")
result = pylm.minimize(par0, pylm.to_void(wrap), 4, pylm.function_eval, control)
print("\nResults:")
print(f"status after {result.status.nfev} function evaluations:")
pylm.print_infmsg(result.status.outcome)
print("Obtained parameters:")
for j in range(par0.size()):
print(f"par[{j}] = {result.par[j]}")
print("Fitting data as follows:")
for i in range(len(tx)):
ff = data.f(tx[i], tz[i], result.par)
print(f" t[{i}]={tx[i]},{tz[i]} y={y[i]} fit={ff} residue={y[i]-ff}")
if (result.status.fnorm > 1e-14):
print(f"FAILURE (obtained norm = {result.status.fnorm} is too large)")
return 1
print(f"SUCCESS (obtained norm = {result.status.fnorm})")
return 0
if __name__ == "__main__":
print(surface1())
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment