Polynomials¶
Polynomials¶
-
class
ruffini.
Polynomial
(terms=(), *args)¶ Bases:
tuple
A Polynomial object is the sum of two or more monomials and/or numbers.
You can sum, subtract and multiplicate instances of Polynomial.
You can assign a value to the variables and calculate the value of that polynomial with the value you assigned.
NB The Polynomial class is a subclass of tuple, so all the methods of tuple are automatically inherited from Polynomial; many of these methods are not in this docs.
-
static
__new__
(cls, terms=(), *args)¶ Create the polynomial by giving it a list of terms (a term can be a Monomial or a number); if two or more terms have the same variables, it will sum them toghether
>>> Polynomial(Monomial(2, x=2, y=2), Monomial(3, x=2, y=2)) 5x**2y**2
Raise: TypeError
-
__init__
(terms=(), *args)¶ Initialize the polynomial, then calculate its degree (the highest degree between terms’ ones)
>>> p = Polynomial(Monomial(a=1), Monomial(3)) >>> p a + 3 >>> p.degree 1
-
term_coefficient
(variables=None, **kwargs)¶ Return the coefficient of the term with the given variables
>>> x = Variable('x') >>> y = Variable('y') >>> >>> p = 2*x*y - 6*x*y**3 + 8*x*y >>> p 10xy - 6xy**3 >>> >>> p.term_coefficient(x=1, y=1) Fraction(10, 1)
If none is found, the result will be 0
>>> p.term_coefficient(k=1, b=2) 0
You can also give directly the variables as an argument
>>> p.term_coefficient(x*y) Fraction(10, 1)
Return type: int, float
-
factorize
()¶ With this method you can factorize the polynomial.
For more informations, see
factorize()
docs.Return type: FPolynomial
-
zeros
¶ Return a set of zeros for the polynomial
>>> x = Variable('x') >>> p = 3*x**3 + 2*x**2 - 3*x - 2 >>> p.zeros {Fraction(1, 1), Fraction(-2, 3), Fraction(-1, 1)}
It works only with polynomials with only a variable and a constant term
>>> Polynomial(3*x, Monomial(2, y=1)).zeros Traceback (most recent call last): ... ValueError: Can't calculate zeros for polynomials with more than a variable
>>> Polynomial(3*x, 5*x**2).zeros Traceback (most recent call last): ... ValueError: Can't calculate zeros for polynomials without a constant term
Return type: set Raises: ValueError
-
eval
(values={}, **kwargs)¶ Evaluates the polynomial, giving values for each variable
>>> p = Polynomial(Monomial(5, x=1), Monomial(3, y=1)) >>> p.eval(x=2, y=3) 19
For more informations, see
Monomial.eval()
.Return type: int, float, Monomial, Polynomial
-
__add__
(other)¶ Sum the polynomial with another polynomial, a monomial or a number, too.
>>> x = Variable('x') >>> y = Variable('y') >>> p = 3*x + 2*y >>> >>> p + (3*y + 2) 3x + 5y + 2 >>> >>> p + 2*x 5x + 2y >>> >>> p + 1 3x + 2y + 1
Return type: Polynomial Raise: TypeError
-
__sub__
(other)¶ Subtract the polynomial from another polynomial, a monomial or a number.
>>> x = Variable('x') >>> y = Variable('y') >>> >>> p = 3*x + 2*y >>> >>> p - (3*y + 2) 3x - y - 2 >>> >>> p - 2*x x + 2y >>> >>> p - 1 3x + 2y - 1
Return type: Polynomial Raise: TypeError
-
__mul__
(other)¶ This method is used to multiply a polynomial by a polynomial, a monomial or a number:
>>> x = Variable('x') >>> y = Variable('y') >>> >>> p = 3*x + 2*y >>> >>> p * (3*y + 2) 9xy + 6x + 6y**2 + 4y >>> >>> p * x 3x**2 + 2xy >>> >>> p * 4 12x + 8y
Return type: Polynomial Raise: TypeError
-
__radd__
(other)¶ This method is the reverse for
Polynomial.__add__()
. With this method, you can swap the two operands of the addition:>>> 8 + Polynomial(Monomial(4, a=2)) 4a**2 + 8
For more informations, see
Polynomial.__add__()
docs.Return type: Polynomial Raise: TypeError
-
__rsub__
(other)¶ This method is the reverse for
Polynomial.__sub__()
. With this method, you can swap the two operands of the addition:>>> 5 - Polynomial(Monomial(7, k=1)) -7k + 5
For more informations, see
Polynomial.__sub__ docs()
.Return type: Polynomial Raise: TypeError
-
__rmul__
(other)¶ This method is the reverse for
Polynomial.__mul__()
. With this method, you can swap the two operands of the addition:>>> 10 * Polynomial(Monomial(3.5, b=3)) 35b**3
For more informations, see
Polynomial.__mul__()
docs.Return type: Polynomial, NotImplemented Raise: TypeError
-
__str__
()¶ Return the polynomial as a string. Powers are indicated with **.
>>> str(Polynomial(Monomial(4, a=4, b=1))) '4a**4b' >>> str(Polynomial(Monomial(a=2), Monomial(-2, c=2))) 'a**2 - 2c**2' >>> str(Polynomial(Monomial(3, x=2), Monomial(6, y=3))) '3x**2 + 6y**3'
To see how the single terms are printed, see the
Monomial.__str__()
docs.Return type: str
-
__repr__
()¶ Return the polynomial as a string.
>>> repr(Polynomial(Monomial(4, a=4, b=1))) '4a**4b'
For more informations, see
Polynomial.__str__()
.Return type: str
-
__eq__
(other)¶ Check if two polynomials are equivalent, comparing each term
>>> p0 = Polynomial(Monomial(4, a=4, b=1)) >>> p1 = Polynomial(Monomial(1, a=2), Monomial(-2, c=2)) >>> p2 = Polynomial(Monomial(-2, c=2), Monomial(1, a=2)) >>> >>> p0 == p1 False >>> p0 == p0 True >>> p1 == p2 True
If a polynomial has a single term, it can also be compared to a monomial
>>> Polynomial(Monomial(3, f=2)) == Monomial(3, f=2) True
Since a monomial with no variables can be compared to a number, if a polynomial has only a term - which is a monomial with no variables - it can be compared to a number
>>> Polynomial(Monomial(7)) == 7 True
In any other case, the result will be False.
>>> Polynomial() == {1, 2, 3} False
Return type: bool
-
__neg__
()¶ Return the opposite of the polynomial, changing the sign of each term of the polynomial
>>> -Polynomial(Monomial(4, x=1), Monomial(2, y=2)) -4x - 2y**2
Return type: Polynomial
-
static