fmpq_poly – polynomials over rational numbers¶
- class flint.fmpq_poly(*args)¶
The fmpq_poly type represents dense univariate polynomials over the rational numbers. For efficiency reasons, an fmpq_poly is structurally an integer polynomial with a single common denominator.
>>> from flint import ctx >>> fmpq_poly([1,2,3],5) ** 3 27/125*x^6 + 54/125*x^5 + 63/125*x^4 + 44/125*x^3 + 21/125*x^2 + 6/125*x + 1/125 >>> ctx.pretty = False >>> fmpq_poly([1,2,3],5) ** 3 fmpq_poly([1, 6, 21, 44, 63, 54, 27], 125) >>> divmod(fmpq_poly([2,0,1,1,6]), fmpq_poly([3,5,7])) (fmpq_poly([38, -161, 294], 343), fmpq_poly([572, 293], 343)) >>> ctx.pretty = True
- static bernoulli_poly(n)¶
Returns the Bernoulli polynomial \(B_n(x)\) as an fmpq_poly.
>>> fmpq_poly.bernoulli_poly(2) x^2 + (-1)*x + 1/6
- coeffs(self)¶
Returns the coefficients of
selfas a list>>> from flint import fmpz_poly >>> f = fmpz_poly([1,2,3,4,5]) >>> f.coeffs() [1, 2, 3, 4, 5]
- complex_roots(self, **kwargs)¶
Computes the complex roots of this polynomial. See
fmpz_poly.roots().>>> from flint import fmpq, ctx >>> ctx.prec = 53 >>> fmpq_poly([fmpq(2,3),1]).complex_roots() [([-0.666666666666667 +/- 3.34e-16], 1)]
- deflation(self)¶
- degree(self) long¶
- denom(self)¶
- derivative(self)¶
- static euler_poly(n)¶
Returns the Euler polynomial \(E_n(x)\) as an fmpq_poly.
>>> fmpq_poly.euler_poly(3) x^3 + (-3/2)*x^2 + 1/4
- factor(self, *, monic=False)¶
Factors self into irreducible polynomials. Returns (c, factors) where c is the leading coefficient and factors is a list of (poly, exp).
>>> fmpq_poly.legendre_p(5).factor() (1/8, [(x, 1), (63*x^4 + (-70)*x^2 + 15, 1)]) >>> (fmpq_poly([1,-1],10) ** 5 * fmpq_poly([1,2,3],7)).factor() (-1/700000, [(3*x^2 + 2*x + 1, 1), (x + (-1), 5)])
Since python-flint 0.7.0 this returns primitive denominator-free factors consistent with
fmpq_mpoly.factor(). In previous versions of python-flint all factors were made monic. Passmonic=Trueto get monic factors instead.>>> fmpq_poly.legendre_p(5).factor(monic=True) (63/8, [(x, 1), (x^4 + (-10/9)*x^2 + 5/21, 1)]) >>> (fmpq_poly([1,-1],10) ** 5 * fmpq_poly([1,2,3],7)).factor(monic=True) (-3/700000, [(x^2 + 2/3*x + 1/3, 1), (x + (-1), 5)])
- factor_squarefree(self)¶
Factors self into square-free polynomials. Returns (c, factors) where c is the leading coefficient and factors is a list of (poly, exp).
>>> x = fmpq_poly([0, 1]) >>> p = x**2 * (x/2 - 1)**2 * (x + 1)**3 >>> p 1/4*x^7 + (-1/4)*x^6 + (-5/4)*x^5 + 1/4*x^4 + 2*x^3 + x^2 >>> p.factor_squarefree() (1/4, [(x^2 + (-2)*x, 2), (x + 1, 3)]) >>> p.factor() (1/4, [(x, 2), (x + (-2), 2), (x + 1, 3)])
- gcd(self, other)¶
Returns the greatest common divisor of self and other.
>>> A = fmpq_poly([1,2,6],6); B = fmpq_poly([4,2,1],12) >>> (A * B).gcd(B) x^2 + 2*x + 4
- integral(self)¶
- is_constant(self)¶
Returns True if this polynomial is a scalar (constant).
>>> f = fmpq_poly([0, 1]) >>> f x >>> f.is_constant() False
- is_gen(self)¶
Return
Trueif the polynomial is the generator of the polynomial, \(x\), andFalseotherwise>>> x = fmpq_poly([0, 1]) >>> x x >>> x.is_gen() True >>> (x + 1).is_gen() False
- is_one(self)¶
Returns True if this polynomial is equal to 1.
- is_zero(self)¶
Returns True if this is the zero polynomial.
- leading_coefficient(self)¶
Returns the leading coefficient of the polynomial.
>>> f = fmpq_poly([1, 2, 3]) >>> f 3*x^2 + 2*x + 1 >>> f.leading_coefficient() 3
- left_shift(self, slong n)¶
Returns
selfshifted left byncoefficients by inserting zero coefficients. This is equivalent to multiplying the polynomial by x^n>>> f = fmpq_poly([1,2,3]) >>> f.left_shift(0) 3*x^2 + 2*x + 1 >>> f.left_shift(1) 3*x^3 + 2*x^2 + x >>> f.left_shift(4) 3*x^6 + 2*x^5 + x^4
- static legendre_p(n)¶
Returns the Legendre polynomial \(P_n(x)\) as an fmpq_poly.
>>> fmpq_poly.legendre_p(3) 5/2*x^3 + (-3/2)*x
- length(self) long¶
- numer(self)¶
- property p¶
fmpq_poly.numer(self)
- property q¶
fmpq_poly.denom(self)
- real_roots(self)¶
- repr(self)¶
- resultant(self, other)¶
Returns the resultant of self and other.
>>> A = fmpq_poly([1, 0, -1]); B = fmpq_poly([1, -1]) >>> A.resultant(B) 0 >>> C = fmpq_poly([1, 0, 0, 0, 0, -1, 1]) >>> D = fmpq_poly([1, 0, 0, -1, 0, 0, 1]) >>> C.resultant(D) 3 >>> f = fmpq_poly([1, -1] + [0] * 98 + [1]) >>> g = fmpq_poly([1] + [0] * 50 + [-1] + [0] * 48 + [1]) >>> f.resultant(g) 1125899906842623
- right_shift(self, slong n)¶
Returns
selfshifted right byncoefficients. This is equivalent to the floor division of the polynomial by x^n>>> f = fmpq_poly([1,2,3]) >>> f.right_shift(0) 3*x^2 + 2*x + 1 >>> f.right_shift(1) 3*x + 2 >>> f.right_shift(4) 0
- roots(self)¶
Computes all the roots in the base ring of the polynomial. Returns a list of all pairs (v, m) where v is the integer root and m is the multiplicity of the root.
To compute complex roots of a polynomial, instead use the
.complex_roots()method, which is available on certain polynomial rings.>>> from flint import fmpz_poly >>> fmpz_poly([1, 2]).roots() [] >>> fmpz_poly([2, 1]).roots() [(-2, 1)] >>> fmpz_poly([12, 7, 1]).roots() [(-3, 1), (-4, 1)] >>> (fmpz_poly([-5,1]) * fmpz_poly([-5,1]) * fmpz_poly([-3,1])).roots() [(3, 1), (5, 2)]
- sqrt(self)¶
Return the exact square root of this polynomial or
None.>>> p = fmpq_poly([1,2,1],4) >>> p 1/4*x^2 + 1/2*x + 1/4 >>> p.sqrt() 1/2*x + 1/2
- str(self, bool ascending=False, var='x', *args, **kwargs)¶
Convert to a human-readable string (generic implementation for all polynomial types).
If ascending is True, the monomials are output from low degree to high, otherwise from high to low.
- truncate(self, slong n)¶
Notionally truncate the polynomial to have length
n. Ifnis larger than the length of the input, then a copy ofselfis returned. Ifnis not positive, then the zero polynomial is returned.Effectively returns this polynomial \(\mod x^n\).
>>> f = fmpq_poly([1,2,3]) >>> f.truncate(3) == f True >>> f.truncate(2) 2*x + 1 >>> f.truncate(1) 1 >>> f.truncate(0) 0 >>> f.truncate(-1) 0
- xgcd(self, other)¶