Exemplo n.º 1
0
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)
Exemplo n.º 2
0
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)))
Exemplo n.º 3
0
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
Exemplo n.º 4
0
def test_merge_common_factors():
    result = set(
        _merge_common_factors(
            Factor("t1", 1),
            Factor("t2", 2),
            Factor("t3", 3),
            Factor("t2", 4),
            Factor("t1", 5),
        )
    )
    expected = {Factor("t1", 6), Factor("t2", 6), Factor("t3", 3)}
    assert result == expected
Exemplo n.º 5
0
def test_factor_multiply():
    assert Factor("t", 1) * Factor("t", 2) == Factor("t", 3)

    f = Factor("t", 1)
    assert 1 * f is f
    assert f * 1 is f

    with pytest.raises(RuntimeError):
        Factor("t1", 1) * Factor("t2", 1)
    with pytest.raises(RuntimeError):
        Factor("t", 1) * 2
Exemplo n.º 6
0
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)),
    }
Exemplo n.º 7
0
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
Exemplo n.º 8
0
def test_factor_equality(f):
    assert f(Factor("t", 2)) == f(Factor("t", 2))
    assert f(Factor("t", 2)) != f(Factor("t2", 2))
    assert f(Factor("t", 1)) != f(Factor("t", 2))
Exemplo n.º 9
0
def test_factor_eval():
    assert Factor("t", 2).eval(t=2) == 4
Exemplo n.º 10
0
def test_factor_str():
    f = Factor("t", 2)
    assert str(f) == "t^2"
    assert str(f) == repr(f)
Exemplo n.º 11
0
def test_factor_constructor():
    assert Factor("t") == Factor("t", 1)

    with pytest.raises(ValueError):
        Factor("t", 0)
Exemplo n.º 12
0
def test_term_eval():
    assert Term(2, Factor("t", 3)).eval(t=4) == 2 * 4**3
Exemplo n.º 13
0
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)))
Exemplo n.º 14
0
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"
Exemplo n.º 15
0
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)))
Exemplo n.º 16
0
def test_poly_collect_for(t1, t2, t3):
    result = (t1**2 * t2 * t3**3 + 2 * t1**2).collect_for(Factor("t1", 2))
    expected = 2 + t2 * t3**3
    assert result == expected
Exemplo n.º 17
0
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")
Exemplo n.º 18
0
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
Exemplo n.º 19
0
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)
Exemplo n.º 20
0
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))
Exemplo n.º 21
0
def test_term_is_constant():
    assert Term(2).is_constant()
    assert not Term(2, Factor("t", 1)).is_constant()
Exemplo n.º 22
0
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)
Exemplo n.º 23
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