def test_poly_multiplication(t1, t2, t3): assert Poly(Term(1, Factor("t1", 1))) * Poly(Term(2, Factor("t2", 1))) == Poly( Term(2, Factor("t1", 1), Factor("t2", 1)) ) assert t1 * 1 is t1 assert 1 * t1 is t1 assert t1 * 2 == t1 * const(2) assert 2 * t1 == t1 * const(2)
def test_poly_addition(t1, t2, t3): assert Poly(Term(1, Factor("t1", 1))) + Poly(Term(1, Factor("t2", 1))) == Poly( Term(1, Factor("t1", 1)), Term(1, Factor("t2", 1)) ) assert t1 + 0 is t1 assert 0 + t1 is t1 assert t1 + 1 == t1 + const(1) assert 1 + t1 == t1 + const(1) assert -t1 == Poly(Term(-1, Factor("t1", 1)))
def test_term_multiplication(): result = Term(1, Factor("t1", 1)) * Term(2, Factor("t2", 2)) expected = Term(2, Factor("t1", 1), Factor("t2", 2)) assert result == expected t = Term(1, Factor("t", 1)) assert t * 1 is t assert 1 * t is t with pytest.raises(RuntimeError): t * 2
def test_merge_common_terms(): result = set( _merge_common_terms( Term(1, Factor("t1", 1)), Term(2, Factor("t2", 1)), Term(3, Factor("t3", 1)), Term(4, Factor("t2", 1)), Term(5, Factor("t1", 1)), ) ) assert result == { Term(6, Factor("t1", 1)), Term(6, Factor("t2", 1)), Term(3, Factor("t3", 1)), }
def test_term_addition(): assert Term(1, Factor("t", 2)) + Term(2, Factor("t", 2)) == Term(3, Factor("t", 2)) t = Term(1, Factor("t", 1)) assert t + 0 is t assert 0 + t is t with pytest.raises(RuntimeError): Term(1, Factor("t1", 1)) + Term(1, Factor("t2", 1)) with pytest.raises(RuntimeError): t + 1
def test_term_equality(f): x = Term(2, Factor("t1", 1), Factor("t2", 2)) y = Term(2, Factor("t2", 2), Factor("t1", 1)) assert f(x) == f(y) x = Term(2, Factor("t1", 1), Factor("t2", 2)) y = Term(2, Factor("t1", 2), Factor("t2", 2)) assert f(x) != f(y) x = Term(1, Factor("t1", 1), Factor("t2", 2)) y = Term(2, Factor("t1", 1), Factor("t2", 2)) assert f(x) != f(y)
def test_poly_subtraction(t1, t2): assert t1 - t1 == Poly(Term(0)) assert 1 - t1 == Poly(Term(1), Term(-1, Factor("t1", 1))) assert t1 - 1 == Poly(Term(-1), Term(1, Factor("t1", 1)))
def test_poly_constructor(): assert Poly( Term(1, Factor("t1", 1)), Term(2, Factor("t1", 1)), Term(2, Factor("t2", 2)) ) == Poly(Term(3, Factor("t1", 1)), Term(2, Factor("t2", 2)))
def test_term_str(): t = Term(2, Factor("t2", 2), Factor("t1", 1)) assert str(t) == "2 * t1^1 t2^2" assert str(t) == repr(t) assert str(Term(1)) == "1"
def test_term_highest_power(): t = Term(2, Factor("t1", 2), Factor("t2", 3)) assert t.highest_power("t1") == 2 assert t.highest_power("t2") == 3 assert t.highest_power("t3") == 0
def test_term_substitute(t1, t2): result = Term(2, Factor("t1", 1), Factor("t2", 2)).substitute("t2", t1 - t2) expected = 2 * t1**3 - 4 * t1**2 * t2 + 2 * t1 * t2**2 assert result == expected
def test_term_is_constant(): assert Term(2).is_constant() assert not Term(2, Factor("t", 1)).is_constant()
def test_term_eval(): assert Term(2, Factor("t", 3)).eval(t=4) == 2 * 4**3
def test_term_collect_for(): t = Term(2, Factor("t1", 1), Factor("t2", 2)) assert t.collect_for(Factor("t1", 1)) == Term(2, Factor("t2", 2)) assert t.collect_for(Factor("t2", 2)) == Term(2, Factor("t1", 1)) with pytest.raises(RuntimeError): t.collect_for(Factor("t3", 2))
def test_term_is_function_of(): t = Term(2, Factor("t1", 1), Factor("t2", 1)) assert t.is_function_of("t1") assert t.is_function_of("t2") assert not t.is_function_of("t3")
def test_term_constructor(): assert Term(Factor("t", 2)) == Term(1, Factor("t", 2)) assert Term(Factor("t", 2), Factor("t", 3)) == Term(Factor("t", 5)) assert Term(0, Factor("t", 2)) == Term(0)