Equations

Equations

class ruffini.Equation(first, second)

Bases: object

You know what an equation is. Well, if you’re reading this docs I hope you know. Anyway.

The sides of the equations are polynomials.

__init__(first, second)

Initialize the Equation by giving the two sides.

>>> Equation(Monomial(2, x=1), 4)
2x - 4 = 0

As you may have noticed, it moves all the terms to the first side. It calculates the equation’s variable and degree.

>>> equation = Equation(Monomial(2, x=1), 4)
>>> equation.first
2x - 4
>>> equation.second
0
>>> equation.degree
1
>>> equation.variable
'x'

NB: It raises a NotImplementedError if you try to create an equation with more than one variable

>>> Equation(Monomial(3, x=1), Monomial(2, y=1))
Traceback (most recent call last):
...
NotImplementedError: Too many variables
Raise:NotImplementedError
__str__()

Return the equation formatted as a string

>>> print(Equation(Monomial(2, x=1), 4))
2x - 4 = 0
Return type:str
__repr__()

Return the equation formatted as a string

>>> Equation(Monomial(2, x=1), 4)
2x - 4 = 0

For more informations, see Equation.__str__().

Return type:str
solve()

Solve the equation.

>>> Equation(Monomial(x=2), 4).solve()
(Fraction(2, 1), Fraction(-2, 1))

It works only with equations of degree 1 or 2; if it’s higher, it raises a NotImplementedError.

>>> Equation(Monomial(x=3), 27).solve()
Traceback (most recent call last):
...
NotImplementedError: Can't solve equations with degree higher than 2

If the result is impossible or indeterminate it raises a ValueError

>>> Equation(Monomial(0, x=1), 0).solve()
Traceback (most recent call last):
...
ValueError: Equation impossible or indeterminate
Raise:ValueError, NotImplementedError