Beispiel #1
0
def testQuadratic():
    # Exception if not quadratic
    assert_raises(ValueError, QuadraticEquation, *(0, 1, 1))
    # Real roots
    r1, r2 = QuadraticEquation(1, 0, -2)
    assert_equal(r1, -r2)
    assert_equal(abs(r1), math.sqrt(2))
    # Complex roots
    r1, r2 = QuadraticEquation(1, 0, 2)
    assert_equal(r1, -r2)
    assert_equal(r1, cmath.sqrt(-2))
    # Constant term 0
    r1, r2 = QuadraticEquation(1, -1, 0)
    assert_equal(r1, 1)
    assert_equal(r2, 0j)
    # Real, distinct
    r1, r2 = QuadraticEquation(1, 4, -21)
    assert(r1 == 3)
    assert(r2 == -7)
    # Real coefficients, complex roots
    r1, r2 = QuadraticEquation(1, -4, 5)
    assert_equal(r1, 2 + 1j)
    assert_equal(r2, 2 - 1j)
    # Complex coefficients, complex roots
    r1, r2 = QuadraticEquation(1, 3 - 3j, 10 - 54j)
    assert_equal(r1, (3 + 7j))
    assert_equal(r2, (-6 - 4j))
Beispiel #2
0
def testBracketRoots():
    '''The polynomial p(x) = (x-1)*(x-10)*(x+10) has
    three roots.  Thus f(x) = exp(p(x)) - 1 will be zero when
    p(x) is zero.  Use BracketRoots() to find the x = 1 root.
    Also demonstrate that it will exceed the iteration limit
    if the interval doesn't include any of the roots.
    '''
    r1, r2, r3  = 1000, -500, 500
    f = lambda x: (x - r1)*(x - r2)*(x + r3)
    r = BracketRoots(f, -2, -1)
    assert((r[0] <= r1 <= r[1]) or
                    (r[0] <= r2 <= r[1]) or
                    (r[0] <= r3 <= r[1]))
    # Demonstate iteration limit can be reached
    f = lambda x: x - 1000000
    assert_raises(TooManyIterations, BracketRoots, f, -2, -1, maxit=10)
Beispiel #3
0
def testQuartic():
    # Exception if not cubic
    assert_raises(ValueError, QuarticEquation, *(0, 1, 1, 1, 1))
    # Basic equation
    r = QuarticEquation(1, 0, 0, 0, 0)
    assert(r == (0, 0, 0, 0))
    # Fourth roots of 1
    for r in QuarticEquation(1, 0, 0, 0, -1):
        assert_equal(r**4, 1)
    # Fourth roots of -1
    for r in QuarticEquation(1, 0, 0, 0, 1):
        assert_equal(Pound(r**4, eps=eps), -1)
    # The equation (x-1)*(x-2)*(x-3)*(x-4)
    for i,j in zip(QuarticEquation(1, -10, 35, -50, 24), range(1, 5)):
        assert_equal(i, j)
    # Two real roots: x*(x-1)*(x-j)*(x+j)
    for i, j in zip(QuarticEquation(1, -1, 1, -1, 0), (-1j, 1j, 0j, 1)):
        assert_equal(i, j)
Beispiel #4
0
def testCubic():
    # Exception if not cubic
    assert_raises(ValueError, CubicEquation, *(0, 1, 1, 1))
    # Basic equation
    r = CubicEquation(1, 0, 0, 0)
    assert(r == (0, 0, 0))
    # Cube roots of 1
    for r in CubicEquation(1, 0, 0, -1):
        assert_equal(Pound(r**3, eps=eps), 1)
    # Cube roots of -1
    for r in CubicEquation(1, 0, 0, 1):
        assert_equal(r**3, -1)
    # Three real roots:  (x-1)*(x-2)*(x-3)
    for i, j in zip(CubicEquation(1, -6, 11, -6), (3, 1, 2)):
        assert_equal(i, j)
    # One real root:  (x-1)*(x-j)*(x+j)
    for i, j in zip(CubicEquation(1, -1, 1, -1), (1, 1j, -1j)):
        assert_equal(i, j)
Beispiel #5
0
def testBisection():
    # Root of x = cos(x); it's 0.739085133215161 as can be found easily
    # by iteration on a calculator.
    f = lambda x: x - math.cos(x)
    tol = 1e-14
    root, numit = Bisection(f, 0.7, 0.8, tol=tol)
    assert abs(root - 0.739085133215161) <= tol
    # Eighth root of 2:  root of x**8 = 2
    f = lambda x: x**8 - 2
    tol = 1e-14
    root, numit = Bisection(f, 1, 2, tol=tol)
    assert abs(root - math.pow(2, 1/8)) <= tol
    # Simple quadratic equation
    t = 100001
    f = lambda x: (x - t)*(x + 100)
    tol = 1e-10
    root, numit = Bisection(f, 0, 2.1*t, tol=tol)
    assert abs(root - t) <= tol
    # Note setting switch to True will cause an exception for this
    # case.
    assert_raises(ValueError, Bisection, f, 0, 2.1*t, tol=tol, switch=True)