def x_minus_y() -> Polynomial: x = Symbol('x') y = Symbol('y') term_x = PolynomialTerm(Monomial({MonomialFactor(x, 1)}), 1.0) term_minus_y = PolynomialTerm(Monomial({MonomialFactor(y, 1)}), -1.0) return Polynomial({term_x, term_minus_y})
def x_sq_minus_y_sq() -> Polynomial: x = Symbol('x') y = Symbol('y') term_x_sq = PolynomialTerm(Monomial({MonomialFactor(x, 2)}), 1.0) term_minus_y_sq = PolynomialTerm(Monomial({MonomialFactor(y, 2)}), -1.0) return Polynomial({term_x_sq, term_minus_y_sq})
def two_x_plus_two_y() -> Polynomial: x = Symbol('x') y = Symbol('y') term_two_x = PolynomialTerm(Monomial({MonomialFactor(x, 1)}), 2.0) term_two_y = PolynomialTerm(Monomial({MonomialFactor(y, 1)}), 2.0) return Polynomial({term_two_x, term_two_y})
def x_sq_plus_two_xy_plus_y_sq() -> Polynomial: x = Symbol('x') y = Symbol('y') term_2xy = PolynomialTerm(Monomial({MonomialFactor(x, 1), MonomialFactor(y, 1)}), 2.0) term_x_sq = PolynomialTerm(Monomial({MonomialFactor(x, 2)}), 1.0) term_y_sq = PolynomialTerm(Monomial({MonomialFactor(y, 2)}), 1.0) return Polynomial({term_x_sq, term_y_sq, term_2xy})
def test_monomial_multiplication_positive_powers() -> None: x = Symbol('x') y = Symbol('y') z = Symbol('z') first_monomial = Monomial({MonomialFactor(x, 2), MonomialFactor(y, 3), MonomialFactor(z, 4)}) second_monomial = Monomial({MonomialFactor(x, 1), MonomialFactor(y, 1), MonomialFactor(z, 1)}) assert first_monomial * second_monomial == \ Monomial({MonomialFactor(x, 3), MonomialFactor(y, 4), MonomialFactor(z, 5)})
def test_monomial_multiplication_to_trivial() -> None: x = Symbol('x') y = Symbol('y') z = Symbol('z') first_monomial = Monomial({MonomialFactor(x, 2), MonomialFactor(y, -3), MonomialFactor(z, 4)}) second_monomial = Monomial({MonomialFactor(x, -2), MonomialFactor(z, -4), MonomialFactor(y, 3)}) assert first_monomial * second_monomial == Monomial({MonomialFactor(TrivialMulSymbol(), 0)}) assert second_monomial * first_monomial == TrivialMonomial()
def test_monomial_symbols() -> None: x = Symbol('x') y = Symbol('y') z = Symbol('z') Monomial({MonomialFactor(x, 2), MonomialFactor(y, 3), MonomialFactor(z, 4)}) assert Monomial({MonomialFactor(x, 2), MonomialFactor(y, 3), MonomialFactor(z, 4)}).symbols == \ {x, y, z} assert Monomial({MonomialFactor(x, 2), MonomialFactor(y, 3)}).symbols == {x, y}
def first_lotke_volterra_ode() -> ODE: # return dx/dt = x - x*y x = Symbol('x') y = Symbol('y') rhs = Polynomial({ PolynomialTerm(Monomial({MonomialFactor(x, 1)}), 1), PolynomialTerm(Monomial({MonomialFactor(x, 1), MonomialFactor(y, 1)}), -1) }) return ODE(x, rhs)
def second_lotke_volterra_ode() -> ODE: # return dy/dt = x*y - y x = Symbol('x') y = Symbol('y') rhs = Polynomial({ PolynomialTerm(Monomial({MonomialFactor(y, 1)}), -1), PolynomialTerm(Monomial({MonomialFactor(x, 1), MonomialFactor(y, 1)}), 1) }) return ODE(y, rhs)
def one_x() -> Polynomial: x = Symbol('x') one_x = PolynomialTerm(Monomial({MonomialFactor(x, 1)}), 1.0) return Polynomial({one_x})
def const_one() -> Polynomial: x = TrivialMulSymbol() one = PolynomialTerm(Monomial({MonomialFactor(x, 0)}), 1.0) return Polynomial({one})
def two_x() -> Polynomial: x = Symbol('x') term_two_x = PolynomialTerm(Monomial({MonomialFactor(x, 1)}), 2.0) return Polynomial({term_two_x})
def one_y() -> Polynomial: y = Symbol('y') one_y = PolynomialTerm(Monomial({MonomialFactor(y, 1)}), 1.0) return Polynomial({one_y})