Polynomials Factorization¶
FPolynomials¶
-
class
ruffini.
FPolynomial
¶ Bases:
tuple
A FPolynomial (factorized polynomial) object is a multiplication of two or more polynomials.
When you factor a polynomial, an FPolynomial istance is returned. On the other hand, you can multiply the polynomials of the fpolynomial and obtain the starting polynomial.
You can’t perform any math operation with a factorized polynomial
NB The FPolynomial class is a subclass of tuple, so all the methods of tuple are automatically inherited from FPolynomial; many of these methods are not in this docs.
-
static
__new__
(cls, *factors)¶ Create the factorized polynomial giving it a list of factors (int, float, Fraction, Monomial or Polynomial).
>>> p = Polynomial(Monomial(2, x=2, y=2)) >>> FPolynomial(5, p) 5(2x**2y**2)
Every factor that is equal to 1 is not inserted in the fpolynomial
>>> len(FPolynomial(5, p, 1)) 2
In the factors must be present at least a polynomial
>>> FPolynomial(5) Traceback (most recent call last): ... TypeError: There must be at least a polynomial
It converts all the factors to Polynomial and sort them by frequency.
Raise: TypeError
-
eval
()¶ Return the starting polynomial multiplying all the factors
>>> f = FPolynomial(5, Polynomial(Monomial(2, x=1), 3)) >>> f 5(2x + 3) >>> f.eval() 10x + 15
The result will always be a Polynomial
>>> type(f.eval()) <class 'ruffini.polynomials.Polynomial'>
Return type: Polynomial
-
__str__
()¶ Return the factorized polynomial as a string.
>>> p1 = Polynomial(2, Monomial(3, x=1)) >>> p2 = Polynomial(Monomial(2, y=1), 17) >>> print(FPolynomial(p1, p2)) (2 + 3x)(2y + 17)
If the first element is a monomial, an integer or a float, its brackets will be omitted
>>> print(FPolynomial(5, p1)) 5(2 + 3x)
Otherwise, if a factor appears two times, the result will be like this
>>> print(FPolynomial(p2, p2)) (2y + 17)**2
>>> print(FPolynomial(p2, p2, 5)) 5(2y + 17)**2
Return type: str
-
__repr__
()¶ Return the factorized polynomial as a string.
>>> p1 = Polynomial(2, Monomial(3, x=1)) >>> p2 = Polynomial(Monomial(2, y=1), 17) >>> repr(FPolynomial(p1, p2)) '(2 + 3x)(2y + 17)'
For more informations, see :func:FPolynomial.__str__()`.
Return type: str
-
__eq__
(other)¶ Compare two factorized polynomials to see if they’re equivalent.
>>> p = Polynomial(Monomial(5, x=2), 3) >>> m = Monomial(2, y=1) >>> FPolynomial(p, m) == FPolynomial(p, m) True
If we swap factors, the result doesn’t change
>>> FPolynomial(p, m) == FPolynomial(m, p) True
If we compare two factorized polynomials with different factors, but which - once evaluated - they are equivalent, the result is true. For example:
>>> x = Variable("x") >>> y = Variable("y") >>> >>> fp1 = FPolynomial(2*x - 3*y**2, 2*x - 3*y**2) >>> fp2 = FPolynomial(3*y**2 - 2*x, 3*y**2 - 2*x) >>> >>> fp1.eval() == fp2.eval() True >>> fp1 == fp2 True
Return type: bool
-
static
factorize()¶
-
ruffini.
factorize
(polynomial)¶ Factorize the given polynomial using some algorythms (sub functions), such as
gcf()
, group [todo], squares difference [todo], cubes sum [todo], cubes difference [todo], binomial square [todo],factorize_binomia_square()
, trinomial square [todo].It works in recursive mode.
>>> factorize(Polynomial(Monomial(10, x=1), 15)) 5(2x + 3)
If polynomial isn’t a polynomial, it will raise a TypeError
>>> factorize('John') Traceback (most recent call last): ... TypeError: Can't factorize object of type 'str'
Return type: FPolynomial Raises: TypeError
gcf()¶
-
ruffini.
gcf
(polynomial)¶ Factorize a polynomial with the gcf (greates common facor) method. It works like this:
AX + AY + … = A(X + Y + …)
for example:
>>> gcf(Polynomial(Monomial(10, x=1), 15)) (5, 2x + 3)
If there isn’t a gcf, it will return the starting polynomial
>>> gcf(Polynomial(Monomial(11, x=1), 15)) (11x + 15,)
The function returns a tuple.
Return type: tuple Raise: TypeError
binomial_square()¶
-
ruffini.
binomial_square
(polynomial)¶ Check if the polynomial is a binomial square. It works like this:
A*2 + B**2 + 2AB = (A + B)**2
for example:
>>> p = Polynomial(Monomial(4, x=2), Monomial(9, y=4), Monomial(-12, x=1, y=2)) >>> p 4x**2 + 9y**4 - 12xy**2 >>> binomial_square(p) (2x - 3y**2, 2x - 3y**2)
It can raise ValueError in three cases:
- When polynomial’s length isn’t 3:
>>> binomial_square(Polynomial(1)) Traceback (most recent call last): ... ValueError: Not a binomial square
- When there aren’t at least two squares:
>>> p = Polynomial(Monomial(4, x=2), Monomial(9, y=5), Monomial(3)) >>> binomial_square(p) Traceback (most recent call last): ... ValueError: Not a binomial square
- When the third term isn’t the product of the firsts:
>>> p = Polynomial(Monomial(4, x=2), Monomial(9, y=4), Monomial(3)) >>> binomial_square(p) Traceback (most recent call last): ... ValueError: Not a binomial square
Return type: tuple Raise: TypeError, ValueError