Ejemplo n.º 1
0
def test_MnMigrad_np():
    fcn = FCN(
        lambda xy: 10 + xy[0]**2 + ((xy[1] - 1) / 2)**2,
        lambda xy: [2 * xy[0], (xy[1] - 1)],
        True,
        1,
    )
    state = MnUserParameterState()
    state.add("x", 5, 0.1)
    state.add("😁", 3, 0.2, -5, 5)
    assert len(state) == 2
    str = MnStrategy(2)
    migrad = MnMigrad(fcn, state, str)
    fmin = migrad(0, 0.1)
    state = fmin.state
    assert len(state) == 2
    assert state[0].number == 0
    assert state[0].name == "x"
    assert state[0].value == approx(0, abs=1e-2)
    assert state[0].error == approx(1, abs=1e-2)
    assert state[1].number == 1
    assert state[1].name == "😁"
    assert state[1].value == approx(1, abs=1e-2)
    assert state[1].error == approx(2, abs=6e-2)
    assert fcn._nfcn > 0
    assert fcn._ngrad > 0
Ejemplo n.º 2
0
def test_MnMigrad_cfunc(npar):
    nb = pytest.importorskip("numba")

    c_sig = nb.types.double(nb.types.uintc, nb.types.CPointer(nb.types.double))

    @nb.cfunc(c_sig)
    def fcn(n, x):
        x = nb.carray(x, (n, ))
        r = 0.0
        for i in range(n):
            r += (x[i] - i)**2
        return r

    fcn = FCN(fcn, None, True, 1)
    state = MnUserParameterState()
    for i in range(npar):
        state.add(f"x{i}", 5, 0.1)
    migrad = MnMigrad(fcn, state, 1)
    fmin = migrad(0, 0.1)
    state = fmin.state
    assert len(state) == npar
    for i, p in enumerate(state):
        assert p.number == i
        assert p.value == approx(i, abs=1e-3)
        assert p.error == approx(1, abs=1e-3)
Ejemplo n.º 3
0
def test_MnSimplex():
    fcn = FCN(lambda x: 10 + x**2, None, False, 1)
    state = MnUserParameterState()
    state.add("x", 2, 5)
    simplex = MnSimplex(fcn, state, 1)
    fmin = simplex(0, 0.1)
    assert fmin.is_valid
    state = fmin.state
    assert len(state) == 1
    assert state[0].value == approx(0, abs=5e-2)
Ejemplo n.º 4
0
def test_FunctionMinimum():
    fcn = FCN(lambda x: 10 + x**2, None, False, 1)
    st = MnUserParameterState()
    st.add("x", 0.01, 5)
    str = MnStrategy(1)
    fm1 = FunctionMinimum(fcn, st, str, 0.2)
    assert fm1.is_valid
    assert len(fm1.state) == 1
    assert fm1.fval == 10.0001
    fm2 = FunctionMinimum(fcn, st, str, 0)
    assert not fm2.is_valid
Ejemplo n.º 5
0
def test_MnMigrad_grad():
    fcn = FCN(lambda x: 10 + x**2, lambda x: [2 * x], False, 1)
    state = MnUserParameterState()
    state.add("x", 5, 0.1)
    migrad = MnMigrad(fcn, state, 1)
    fmin = migrad(0, 0.1)
    state = fmin.state
    assert len(state) == 1
    assert state[0].number == 0
    assert state[0].name == "x"
    assert state[0].value == approx(0, abs=1e-3)
    assert state[0].error == approx(1, abs=1e-3)
    assert fcn._nfcn > 0
    assert fcn._ngrad > 0
Ejemplo n.º 6
0
def test_MnMigrad():
    fcn = FCN(fn, None, False, 1)
    state = MnUserParameterState()
    state.add("x", 5, 0.1)
    state.add("y", 3, 0.2, -5, 5)
    migrad = MnMigrad(fcn, state, 1)
    fmin = migrad(0, 0.1)
    assert fmin.is_valid
    state = fmin.state
    assert state[0].value == approx(0, abs=5e-3)
    assert state[0].error == approx(1, abs=5e-3)
    assert state[1].value == approx(1, abs=5e-3)
    assert state[1].error == approx(2, abs=6e-2)
    assert fcn._nfcn > 0
    assert fcn._ngrad == 0
Ejemplo n.º 7
0
def test_FunctionMinimum_pickle():
    st = MnUserParameterState()
    st.add("x", 1, 0.1)
    st.add("y", 2, 0.1, 1, 3)
    fm = FunctionMinimum(FCN(fn, None, False, 1), st, 1, 0.1)

    pkl = pickle.dumps(fm)
    fm2 = pickle.loads(pkl)

    assert len(fm.state) == len(fm2.state)
    assert fm.state == fm2.state
    assert fm.edm == fm2.edm
    assert fm.fval == fm2.fval
    assert fm.is_valid == fm2.is_valid
    assert fm.has_valid_parameters == fm2.has_valid_parameters
    assert fm.has_accurate_covar == fm2.has_accurate_covar
    assert fm.has_posdef_covar == fm2.has_posdef_covar
    assert fm.has_made_posdef_covar == fm2.has_made_posdef_covar
    assert fm.hesse_failed == fm2.hesse_failed
    assert fm.has_covariance == fm2.has_covariance
    assert fm.is_above_max_edm == fm2.is_above_max_edm
    assert fm.has_reached_call_limit == fm2.has_reached_call_limit
    assert fm.errordef == fm2.errordef