def test_create_polynomial(): m1 = Monomial(1, ['A']) m2 = Monomial(1, ['A', 'B'], [-2, 3]) p1 = Polynomial([m1, m2]) assert isinstance(p1, Polynomial) with pytest.raises(ValueError): p2 = Polynomial(1337)
def test_str_polynomial(): p = Polynomial() assert str(p) == '1' m1 = Monomial(1, ['A', 'B'], [2, 3]) m2 = Monomial(2, ['C']) p.monomials = [m1, m2] assert str(p) == 'A^(2)*B^(3) + 2*C'
def test_add_monomial_polynomial(): m1 = Monomial(2, 'A') p1 = Polynomial() m1p1 = m1 + p1 assert isinstance(m1p1, Polynomial) assert str(m1p1) == '2*A + 1' p1m1 = p1 + m1 assert isinstance(p1m1, Polynomial) assert str(p1m1) == '1 + 2*A'
def test_add_scalar_polynomial(): s1 = 2 p1 = Polynomial() p2 = s1 + p1 assert isinstance(p2, Polynomial) assert str(p2) == '2 + 1' p3 = p1 + s1 assert isinstance(p3, Polynomial) assert str(p3) == '1 + 2'
def test_mult_scalar_polynomial(): p = Polynomial() s = 2 sp = s * p assert isinstance(sp, Polynomial) ps = p * s assert isinstance(ps, Polynomial) m1 = Monomial(1, ['A']) m2 = Monomial(2, ['B']) p2 = Polynomial([m1, m2]) sp2 = s * p2 assert isinstance(sp2, Polynomial) assert str(sp2) == '2*A + 4*B' p2s = p2 * s assert isinstance(sp2, Polynomial) assert str(sp2) == '2*A + 4*B'
def test_mult_polynomial_polynomial(): p1 = Polynomial() p2 = Polynomial() p1p2 = p1 * p2 assert isinstance(p1p2, Polynomial) m1 = Monomial(1, ['A']) m2 = Monomial(2, ['B']) m3 = Monomial(3, ['C']) m4 = Monomial(4, ['D']) p3 = Polynomial([m1, m2]) p4 = Polynomial([m3, m4]) p3p4 = p3 * p4 assert isinstance(p3p4, Polynomial) assert str(p3p4) == '3*A*C + 4*A*D + 6*B*C + 8*B*D' p4p3 = p4 * p3 assert isinstance(p4p3, Polynomial) assert str(p4p3) == '3*C*A + 6*C*B + 4*D*A + 8*D*B'
def test_mult_monomial_polynomial(): p1 = Polynomial() m1 = Monomial() m1p1 = m1 * p1 assert isinstance(m1p1, Polynomial) p1m1 = p1 * m1 assert isinstance(p1m1, Polynomial) m1 = Monomial(1, ['A']) m2 = Monomial(2, ['B']) p2 = Polynomial([m1, m2]) m3 = Monomial(3, ['C']) p2m3 = p2 * m3 assert isinstance(p2m3, Polynomial) assert str(p2m3) == '3*A*C + 6*B*C' m3p2 = m3 * p2 assert isinstance(m3p2, Polynomial) assert str(m3p2) == '3*C*A + 6*C*B'
def test_assign_polynomial(): p1 = Polynomial() assert len(p1.monomials) == 1 p1.monomials = [Monomial()] assert len(p1.monomials) == 1