Hong-Phuc Bui
5 days ago 01eda97ff64ad2570b7105cc5b734f488ce0d47a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from polynomial import Polynomial
EPSILON = 0.001
 
def test_polynomial_representation():
    c = Polynomial(5, 6, 0, -7)
    r = repr(c)
    s = str(c)
    print(r, s, c)
 
 
def test_evaluate():
    l = Polynomial(-12.5, 3.6)
    x = 0.0
    (y, rest) = l.evaluate(x)
    assert abs(y + 12.5) < EPSILON
 
 
def test_evaluate_2():
    l = Polynomial(11, 7, -5, -4, 2)
    x = 2
    q_expected = [-3, -5, 0, 2]
    y_expected = 5
    (y, q) = l.evaluate(x)
 
    assert abs(y - y_expected) < EPSILON
    for (i, r) in enumerate( q_expected ):
        assert abs(r - q[i]) < EPSILON
 
 
def test_polynomial_add():
    p = Polynomial(-3, 4.5, 6)      # ~$p(x) = -3 + 4.5x + 6x^2$~
    q = Polynomial( 1, 0,   3, -4)  # ~$q(x) = 1 + 3x^2 - 4x^3$~
    s = p + q                       # ~$s(x) = -2 + 4.5x + 9x^2 - 4x^3$~
    expected = [-2, 4.5, 9, -4]
    for (i, ec) in enumerate( expected ):
        pc = s[i]
        assert abs(pc - ec) < EPSILON
 
def test_polynomial_add_2():
    p = Polynomial(1, 0, 3, -4)  # ~$q(x) = 1 + 3x^2 - 4x^3$~
    q = Polynomial(-3, 4.5, 6)      # ~$p(x) = -3 + 4.5x + 6x^2$~
    s = p + q                       # ~$s(x) = -2 + 4.5x + 9x^2 - 4x^3$~
    expected = [-2, 4.5, 9, -4]
    for (i, ec) in enumerate( expected ):
        pc = s[i]
        assert abs(pc - ec) < EPSILON
 
 
if __name__ == "__main__":
    #test_evaluate_2()
    test_polynomial_add()