示例#1
0
from __future__ import division

from hep.cernlib.minuit import minimize
from hep.test import compare
from math import hypot

# -----------------------------------------------------------------------
# tests
# -----------------------------------------------------------------------


def function(x, y, z):
    return (x - y + 1) ** 2 + (x - z + 2) ** 2 + (y + z) ** 2


fit_result = minimize(function, ("x", "y", "z"))
compare(fit_result.values["x"], -3 / 2, precision=1e-6)
compare(fit_result.values["y"], -1 / 2, precision=1e-6)
compare(fit_result.values["z"], 1 / 2, precision=1e-6)

fit_result = minimize(function, (("x", 4, None), "y", "z"))
compare(fit_result.values["x"], 4, precision=1e-6)
compare(fit_result.values["y"], 4 / 3, precision=1e-6)
compare(fit_result.values["z"], 7 / 3, precision=1e-6)

fit_result = minimize(function, ("x", ("y", -1, None), "z"))
compare(fit_result.values["x"], -5 / 3, precision=1e-6)
compare(fit_result.values["y"], -1, precision=1e-6)
compare(fit_result.values["z"], 2 / 3, precision=1e-6)

fit_result = minimize(function, ("x", ("y", 5, None), ("z", 6, None)))
示例#2
0
#-----------------------------------------------------------------------
# imports
#-----------------------------------------------------------------------

from   hep.cernlib.minuit import minimize
from   hep.test import compare

#-----------------------------------------------------------------------
# tests
#-----------------------------------------------------------------------

fit_result = minimize("x**2 - x + 1", (("x", 0.0), ))
compare(fit_result.values["x"], 0.5, precision=1e-6)

fit_result = minimize("hypot(a - 1, b - 2)", (("a", 0.0), ("b", 0.0), ))
compare(fit_result.values["a"], 1.0, precision=1e-6)
compare(fit_result.values["b"], 2.0, precision=1e-6)

fit_result = minimize("x", (("x", 0, 0.1, 3, 4), ))
compare(fit_result.values["x"], 3.0, precision=1e-6)

fit_result = minimize("-x", (("x", 0, 0.1, 3, 4), ))
compare(fit_result.values["x"], 4.0, precision=1e-6)

示例#3
0
#-----------------------------------------------------------------------
# imports
#-----------------------------------------------------------------------

from   hep.cernlib.minuit import minimize
from   hep.test import compare
from   math import hypot

#-----------------------------------------------------------------------
# tests
#-----------------------------------------------------------------------

def function1(val):
    return val ** 2 - val + 1

fit_result = minimize(function1, ("val", ))
compare(fit_result.values["val"], 0.5, precision=1e-6)

def function2(a, b):
    return hypot(a - 1, b - 2)

fit_result = minimize(function2, (("b", 0.0), ("a", 0.0)))
compare(fit_result.values["a"], 1.0, precision=1e-6)
compare(fit_result.values["b"], 2.0, precision=1e-6)