def polynomial(self, variable='x'): ans = Polynomial() for mon, coeff in self.items(): ans += mon.polynomial(variable) * coeff return ans
def test_mult(a, b, prod): assert Polynomial(a) * Polynomial(b) == Polynomial(prod)
def test_call(a, b, fx): assert Polynomial(a).__call__(b) == fx
def test_coefficients(): #Get-function p = Polynomial([1,2,3,4]) assert Polynomial.coefficients(p) == [1,2,3,4]
def test_mul(): #Multiplies p = Polynomial([1,2,3]) c = 2 assert Polynomial.__mul__(p,c) == [2,4,6]
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
def test_evaluation(): p = Polynomial([3, 2, 2]) # 2x**2 + 2x + 3 assert p(3) == 27 assert p(7) == 115 assert p(209) == 87783
from polynomials import Polynomial, deriv import pytest @pytest.mark.parametrize("a, d", ((Polynomial((0, )), Polynomial( (0, ))), (Polynomial((2, 0, 3)), (Polynomial((0, 6)))), (Polynomial( (4, 2)), Polynomial((2, ))))) def test_deriv(a, d): assert deriv(a) == d
def test_equality(): assert Polynomial((0, 1)) == Polynomial((0, 1))
def __rsub__(self, other): new = self - other new2 = tuple(-a for a in new.coefficients) return Polynomial(new2)
def setUp(self): self.a_polynomial = Polynomial("2x**4 + 4x**3 - 5x**2 - 9") self.b_polynomial = Polynomial("2a**2 + 4a**4 - 2") self.c_polynomial = Polynomial("1/2z**4 - 1/2") self.d_polynomial = Polynomial("4x**5 + x**4 - 2x**3 + 3")
def test_degree(): #check if class Polynomial returns correct polynomial degree p = Polynomial([1, 2, 3, 0]) assert p.degree() == 2
def test_sub(): #check if polynomial p and q is subtracted correctly by class polynomial p = Polynomial([0, 3, 0, 5]) q = Polynomial([1, 2, 4, 0]) assert p - q != Polynomial([-1, 1, -4, 5])
def test_add(): #check if polynomial p and q is added together correctly by class polynomial p = Polynomial([0, 3, 0, 5]) q = Polynomial([1, 2, 4, 0]) r = Polynomial([1, 5, 4, 5]) assert p + q != r def test_sub(): #check if polynomial p and q is subtracted correctly by class polynomial p = Polynomial([0, 3, 0, 5]) q = Polynomial([1, 2, 4, 0]) assert p - q != Polynomial([-1, 1, -4, 5]) def test_degree(): #check if class Polynomial returns correct polynomial degree p = Polynomial([1, 2, 3, 0]) assert p.degree() == 2 p = Polynomial([1, 2, 0, 4]) #print p.__repr__() #test_add() r = p.__mul__(2.2) print r.__repr__()
def test_adding(): p1 = Polynomial([3, 2, 4, 1]) # x**3 + 4*x**2 + 2*x + 3 p2 = Polynomial([7, 2, 3]) # 3*x**2 + 2*x + 7 assert p1 + p2 == Polynomial([10, 4, 7, 1])
def test_add(a, b, sum): assert Polynomial(a) + Polynomial(b) == Polynomial(sum)
def test_subtracting(): p1 = Polynomial([7, 4, 5, 6]) # 6*x**3 + 5*x**2 + 4*x + 7 p2 = Polynomial([2, 2, 3, 2]) # 2*x**3 + 3*x**2 + 2*x + 2 assert p1 - p2 == Polynomial([5, 2, 2, 4])
def test_add_scalar(): assert Polynomial((2, 1)) + 3 == Polynomial((5, 1))
def test_multiplication(): p1 = Polynomial([2, 2, 2, 4]) p2 = Polynomial([1, 2, 0, 0, 0]) assert p1 * 2 == Polynomial([4, 4, 4, 8]) assert p2 * 5 == Polynomial([5, 10, 0, 0, 0])
def test_reverse_add_scalar(): assert 3 + Polynomial((2, 1)) == Polynomial((5, 1))
def test_repr(): p1 = Polynomial([2, 2, 2, 4]) p2 = Polynomial([1, 2, 0, 0, 0]) assert p1 == '2 + 2x + 2x^2 + 4x^3' assert p2 == '1 + 2x'
def test_add_unknown(): with pytest.raises(TypeError): Polynomial((1, )) + "frog"
def test_eq(): #Checking if they are the same. p = Polynomial([1,2,3,4]) q = Polynomial([1,2,3,4]) assert Polynomial.__eq__(p,q) == True
def test_mult(f, g, fg): assert Polynomial(f) * Polynomial(g) == Polynomial(fg)
def test_sub(a, b, sub): assert Polynomial(a) - Polynomial(b) == Polynomial(sub)
def test_mult_scalar(a, f, af): assert Polynomial(f) * a == Polynomial(af)
def test_pow(a, b, pow): assert Polynomial(a)**b == Polynomial(pow)
def test_reverse_mult_scalar(): assert 3 * Polynomial((2, 1)) == Polynomial((6, 3))
def test_print(): p = Polynomial((2, 1, 0, 3)) assert str(p) == "3x^3 + x + 2"
def test_call(f, x, val): assert Polynomial(f)(x) == val