def test_degree():
    #Assuming this is what you actually ment: Return the index of the highest nonzero coefficient.
    p = Polynomial([1,2,5,4])
    assert Polynomial.degree(p) == 3

    p = Polynomial([1])
    assert Polynomial.degree(p) == -1
示例#2
0
def test_poly_degree():
    p = Polynomial([5, 3, 2])
    q = Polynomial([4, 3])
    test = Polynomial([3, 0, 1, 0])  # several zeros might give issues?
    assert p.degree() == 2
    assert q.degree() == 1
    assert test.degree() == 2
示例#3
0
def test_degree():

    # Testing that the degree of both of these polynomials is 2
    p = Polynomial([1, 2, 3])
    q = Polynomial([1, 2, 3, 0, 0])

    deg_ex = 2
    assert p.degree() == deg_ex
    assert q.degree() == deg_ex
示例#4
0
def test_degree():
    p1 = Polynomial([2, 2, 2, 4])  # 4*x**3 + 2*x**2 + 2*x + 2
    p2 = Polynomial([1, 0, 2, 4])  # 4*x**3 + 2*x**2 + 0*x + 1
    p3 = Polynomial([1, 2, 0, 0, 0])  # 0*x**4 + 0*x**3 + 0*x**2 + 2*x + 1
    p4 = Polynomial([0, 0, 0, 0])
    assert p1.degree() == 3
    assert p2.degree() == 3
    assert p3.degree() == 1
    assert p4.degree() == -1
示例#5
0
def test_degree():
    a = Polynomial([1, 2, 1])  #The polynomial x**2 + 2x + 1
    b = Polynomial([5, 3, 0, 1])  #The polynomial x**3 + 3x + 5
    c = Polynomial([3, 1, 0, 0, 2])  #The polynomial 2x**4 + x + 3
    d = Polynomial([1, 0, 1, 0])  #The polynomial x**2 + 1
    z = Polynomial([])  #The zero-polynomial
    assert a.degree() == 2
    assert b.degree() == 3
    assert c.degree() == 4
    assert d.degree() == 2
    assert z.degree() == -1
    print("test_degree() finished successfully.")
class test_polynomials(unittest.TestCase):
    def setUp(self):
        self.p = Polynomial([2, 3, 1])
        self.q = Polynomial([0, 1, 2])

    def poly_ev(self):
        #evaluate a polynom
        corr_poly = dict({0: 2, 1: 6, 2: 12})
        [self.assertEqual(self.q(i), j) for i, j in corr_poly.items()]

    def poly_add(self):
        #Add two polynoms
        add = Polynomial([2, 4, 3])
        res = self.p + self.q  #Add
        self.assertEqual(add, res)

    def poly_add(self):
        #Substitute two polynoms
        sub = Polynomial([-1, 2, -1])
        res = self.p - self.q  #sub
        self.assertEqual(sub, res)

    def degree_exists(self):
        #verifying degree exist
        method = 'degree'  #get a little hint from stack overflow
        self.assertTrue(
            hasattr(Polynomial, 'method')
            and callable(getattr(Polynomial, 'method')))
        #(if('method' in dir(Polynomial) and inspect.isfunction(Polynomial.method)))
        #check if it returns the degree of my poly
        self.assertEqual(self.p.degree(), 2)

    def repr_test(self):
        poly = 'x^2 + 3x + 2'
        self.assertEqual(repr(self.p), poly)  #tests the method

    def mul_test():
        numb = 5
        poly = [0, 5, 10]
        mul = self.q * numb
        self.assertEqual(mul, Polynomial(poly))
示例#7
0
def test_polynomial():
    """ Testing if a Polynomial assetion at a given point is correct"""
    tol = 1e-8
    poly1 = Polynomial([1, 2, 2])
    evaluation_at_4, expected_value_4 = poly1(4), 41
    msg_value = "Expected value at x=%g: %f, Calcualted value at x=%g: %f" % (
        4, expected_value_4, 4, evaluation_at_4)
    assert (abs(evaluation_at_4 - expected_value_4) < tol), msg_value
    """Testing function for adding and subtracting two Polynomials"""
    poly2 = Polynomial([2, 5])
    expected_new_poly_sum = [3, 7, 2]
    expected_new_poly_sub = [-1, -3, 2]
    sum_poly = (poly1 + poly2)
    sub_poly = (poly1 - poly2)
    msg_sum_poly = "Expected new polynom sum: %s, Calculated new polynom sum: %s" % (
        expected_new_poly_sum, sum_poly)
    msg_sub_poly = "Expected new polynom subtraction: %s, Calculated new polynom subtraction: %s" % (
        expected_new_poly_sub, sub_poly)
    assert (expected_new_poly_sum == sum_poly), msg_sum_poly
    assert (expected_new_poly_sub == sub_poly), msg_sub_poly
    """ Testing if the class returns the corrct degree of the polynom"""
    expected_degree = 2
    poly1 = Polynomial([1, 2, 3, 0])
    returned_degree = poly1.degree()
    msg_degree = "Returned degree %i Expected Degree: %i" % (returned_degree,
                                                             expected_degree)
    assert (expected_degree == returned_degree), msg_degree
    """ Testing if __repr__ works correctly"""
    expected_string = "1 + 2x + 3x^2"
    print_msg = "Retuned string do nat match expected string"
    assert (poly1.__repr__() == expected_string), print_msg
    """ Testing __mul__ method"""
    expected_new_poly_mul = [2, 4, 6, 0]
    poly1 = Polynomial([1, 2, 3, 0])
    new_mul_pol = poly1 * 2
    msg_mul = "Multiplaction did not return the expected polynimal. Expected: ", expected_new_poly_mul, " Calculated: ", new_mul_pol
    assert (expected_new_poly_mul == new_mul_pol), msg_mul
示例#8
0
def test_degree_all_zeroes():
    p = Polynomial([0, 0, 0, 0, 0])
    assert p.degree() == -1
示例#9
0
def test_degree_highest_zero():
    p = Polynomial([1, 2, 3, 4, 0])
    assert p.degree() == 3
示例#10
0
def test_degree_no_zeroes():
    p = Polynomial([1, 2, 3, 4, 5])
    assert p.degree() == 4
示例#11
0
def test_degree():
    q = Polynomial([0, 1, 0, 3])
    assert q.degree() == 3
示例#12
0
def test_degree():

    #check if class Polynomial returns correct polynomial degree
    p = Polynomial([1, 2, 3, 0])
    assert p.degree() == 2