Exemplo n.º 1
0
def test_bisec():
    def f(x):
        if x < 5: return -1
        elif x >= 6: return 1
        else: return 0
    r=bisec(f, 0, 10, 1)
    print(r)
    assert r==5
Exemplo n.º 2
0
def test_result2():
    f = lambda x: x
    x = bisec(f, -2, 1)
    assert abs(x) < 1e-4
Exemplo n.º 3
0
def test_result4():
    f = lambda x: -x**3 + 1
    x = bisec(f, -2, 1.5)
    assert abs(x) < 1e-4
Exemplo n.º 4
0
def test_result3():
    f = lambda x: x**3 - 1
    x = bisec(f, -2, 1.5)
    assert abs(x) < 1e-4
Exemplo n.º 5
0
def test_result(tol):
    f = lambda x: x
    x = bisec(f, -1, 1, tol=tol)
    assert abs(x) < tol
Exemplo n.º 6
0
def test_a_equal_b_equal_root(tol):
    f = lambda x: x**2 - 1
    x = bisec(f, -1, -1, tol=tol)
    assert abs(x) < tol
Exemplo n.º 7
0
def test_a_equal_b(tol):
    f = lambda x: x**2 - 1
    with pytest.raises(ValueError):
        x = bisec(f, -2, -2, tol=tol)
Exemplo n.º 8
0
def test_discont(tol):
    f = lambda x: -1 if x < 0 else 1
    with pytest.raises(ValueError):
        x = bisec(f, -2, 1.5, tol=tol)
Exemplo n.º 9
0
def test_zero_right(tol):
    f = lambda x: x**2
    x = bisec(f, -1, 0, tol=tol)
    assert abs(x) < tol
Exemplo n.º 10
0
def test_zero_left(tol):
    f = lambda x: x**2
    x = bisec(f, 0, 1, tol=tol)
    assert abs(x) < tol
Exemplo n.º 11
0
def test_no_zero(tol):
    f = lambda x: x**2 + 1
    with pytest.raises(ValueError):
        x = bisec(f, -2, 1.5, tol=tol)
Exemplo n.º 12
0
def test_plus_minus(tol):
    f = lambda x: -x**3 + 1
    x = bisec(f, -2, 1.5, tol=tol)
    assert abs(x) < tol
Exemplo n.º 13
0
def test_zero_right():
    f = lambda x: x**2
    x = bisec(f, -1, 0)
    assert abs(x) < 1e-4
Exemplo n.º 14
0
def test_zero_left():
    f = lambda x: x**2
    x = bisec(f, 0, 1)
    assert abs(x) < 1e-4
Exemplo n.º 15
0
def test_plus_minus():
    f = lambda x: -x**3 + 1
    x = bisec(f, -2, 1.5)
    assert abs(x) < 1e-4
Exemplo n.º 16
0
def test_a_equal_b_equal_root():
    f = lambda x: x**2 - 1
    x = bisec(f, -1, -1)
    assert abs(x) < 1e-4