ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 18 May 2022 14:53:38 +0200Simplify rational expression to polynomialhttps://ask.sagemath.org/question/62495/simplify-rational-expression-to-polynomial/ I'd like to check my computations for rational polynomial equations with sage. Here is an equation that gives the general form of a parabola with respect to a directrix and focus:
(ax+by+c)^2/(a^2+b^2) = (x-f1)^2 + (y-f2)^2
I'd like to obtain from this rational expression a polynomial expression in x,y. Is there a way to do this kind of computation automatically in Sage?Rodrigo RayaWed, 18 May 2022 14:53:38 +0200https://ask.sagemath.org/question/62495/Series Reversionhttps://ask.sagemath.org/question/23500/series-reversion/I have a rational polynomial f(z) = P(z)/Q(z) which I want to revert (I'm trying to find the inverse of f).
I'll pick a simple example, the function f(z) = (1/z-1)^4
sage: z = var('z')
sage: z = FractionField(PolynomialRing(QQ, 'z')).objgen()
sage: f(z) = (1/z-1)^4
sage: g = f(z).taylor(x,2,4); #I expand f in a Taylor series just to make sure it's a series
sage: f.expand().reversion()
AttributeError: 'sage.symbolic.expression.Expression' object has no attribute 'reversion'
sage: g1 = g.power_series(QQ)
TypeError: denominator must be a unit
sage: g.reversion()
AttributeError: 'sage.symbolic.expression.Expression' object has no attribute 'reversion'
A power series P(z) is not a rational function P(z)/Q(z). That's why the `TypeError: denominator must be a unit` appears.
Is it possible that Sage cannot invert rational polynomials? In the [Sage help text](http://www.sagemath.org/doc/reference/power_series/sage/rings/power_series_poly.html) the computation is passed to pari first, before using Lagrangian inversion.
I thought Sage uses FLINT. Why is it not used for computing the inverse of rational polynomials?
----------
EDIT (after answer by slelievre):
My problem now is how to avoid using the Symbolic Ring. I'm trying to do the following:
I want to invert an approximation formula for the Gamma-function. I am using [Spouge's approximation](https://en.wikipedia.org/wiki/Spouge's_approximation). It has terms in the numerator and the denominator. Ithink I can compute them separetely per se, but what if I wanted to do a Pade approximation of the function
sage: f = (z+13)^(z+1/2)*exp(z)
sage: g = f.pade(5,5)
(it's the term ahead of the sum term in Spouge's approximation)
How do I get separate polynomials for the numerator and the denominator that are defined in the right algebraic structure?
jjackSun, 20 Jul 2014 12:43:52 +0200https://ask.sagemath.org/question/23500/`Fraction must have unit denominator`when using polynomial.numerator()https://ask.sagemath.org/question/44347/fraction-must-have-unit-denominatorwhen-using-polynomialnumerator/I am trying to implement a factorization algorithm for polynomials with integer coefficients, based on Kronecker's principle.
# Kronecker Factorization Algorithm
def kronecker_splitting(f):
"""
INPUT:
a polynomial f in ZZ[x]
OUTPUT:
a non trivial factor of f, or f if it is irreducible
"""
ZZX.<x> = ZZ[]
if f.parent() != ZZ[x]: raise ValueError("f must be in ZZ[x]")
if f.degree() == 1: return f
a = []
a.append(ZZ.random_element())
# Check if a0 is a root of f
if f.subs(a[0])==0: return x-a[0]
div = f.subs(a[0]).divisors() # compute positive divisors
M = [(d,) for d in div] # Turn m into a collection of tuples
for e in range(1,ceil(f.degree()/2)+1):
# Choose a not previously chosen integer
while(True):
aux = ZZ.random_element()
if aux not in a:
a.append(aux)
break
# Check that a[e] is not a root
if f.subs(a[e])==0: return x-a[e]
# compute divisors of a[e]
pos_div = f.subs(a[e]).divisors()
neg_div = [-d for d in pos_div]
div = pos_div + neg_div
Mp = M = [c_prev + (d,) for c_prev in M for d in div]
while len(Mp)>0:
c = Mp.pop()
# solve linear system
A = matrix.vandermonde(a).transpose()
b = A.solve_right(vector(c))
vector2poly = lambda h: sum([ci*x^i for i,ci in enumerate(h)])
g = vector2poly(b)
if b[e] != 0 and f % g == 0:
print("found non trivial factor {}".format(g))
print(g.parent())
return g.numerator()
return f # f is irreducible
def kronecker_factorization(f):
"""
INPUT:
f :- monic element of ZZ[x]
OUTPUT:
[f_1,...,f_s] :- monic irreducible factorization of f
"""
ZZX = ZZ[x]
factors = [f]; irreducible_factors = []
while len(factors) > 0:
g = factors.pop()
print("splitting {}".format(g))
h = kronecker_splitting(g)
# if g was already irreducible
if h == g: irreducible_factors.append(g)
else: factors.extend([h, ZZX(g/h)])
return irreducible_factors
ZZX.<x> = ZZ[x]
f = (x+1)*(x+2)*(x+3)
factors = kronecker_factorization(f)
print("{} factorizes as {}".format(f, factors))
assert(f == reduce(lambda a,b:a*b, factors))
for g in factors:
assert(g.parent() == ZZX)
assert(g.is_irreducible())
When I run the snippet above, I often get this output (try running it a few times to reproduce it, bcs of the randomness):
splitting x^3 + 6*x^2 + 11*x + 6
splitting x^2 + 3*x + 2
found non trivial factor 2*x + 2
Univariate Polynomial Ring in x over Rational Field
Error in lines 46-46
Traceback (most recent call last):
File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1188, in execute
flags=compile_flags) in namespace, locals
File "", line 1, in <module>
File "", line 15, in kronecker_factorization
File "sage/structure/parent.pyx", line 921, in sage.structure.parent.Parent.__call__ (build/cythonized/sage/structure/parent.c:9679)
return mor._call_(x)
File "sage/categories/map.pyx", line 790, in sage.categories.map.Map._call_ (build/cythonized/sage/categories/map.c:7325)
cpdef Element _call_(self, x):
File "/ext/sage/sage-8.4_1804/local/lib/python2.7/site-packages/sage/rings/fraction_field.py", line 1155, in _call_
raise TypeError("fraction must have unit denominator")
TypeError: fraction must have unit denominator
This confuses me, since if I try reproducing the mistake with the following snippet:
QQX.<x> = QQ[x]
g = 3*x + 3
g.parent()
g.numerator()
I get the output: I expected, no errors in sight:
Univariate Polynomial Ring in x over Rational Field
3*x + 3
What is going on?JsevillamolTue, 20 Nov 2018 14:44:51 +0100https://ask.sagemath.org/question/44347/Checking an ideal over $\overline{\mathbb Q}$ for primalityhttps://ask.sagemath.org/question/25104/checking-an-ideal-over-overlinemathbb-q-for-primality/I'm trying to check an ideal in a multivariate polynomial ring over $\overline{\mathbb Q}$ for primality.
Using just a single variable, I can type
R.<x> = PolynomialRing(QQ.algebraic_closure())
print Ideal([x^2+2]).is_prime()
and Sage tells me that $\langle x^2+2\rangle$ is not prime in $\overline{\mathbb Q}[x]$, as expected.
However, as soon as I add another variable, e.g. `R.<x,y> = ...` I get the following output:
<pre>
verbose 0 (3539: multi_polynomial_ideal.py, groebner_basis) Warning: falling back to very slow toy implementation.
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-8-c0542a5be0f8> in <module>()
----> 1 print Ideal([x**Integer(2)+Integer(2)]).is_prime()
/opt/sage-6.4.beta4/local/lib/python2.7/site-packages/sage/rings/polynomial/multi_polynomial_ideal.pyc in is_prime(self, **kwds)
1265 if not self.ring().base_ring().is_field():
1266 raise NotImplementedError
-> 1267 CPD = self.complete_primary_decomposition(**kwds)
1268 if len(CPD) != 1:
1269 return False
/opt/sage-6.4.beta4/local/lib/python2.7/site-packages/sage/rings/polynomial/multi_polynomial_ideal.pyc in __call__(self, *args, **kwds)
602 if not R.base_ring().is_field():
603 raise ValueError("Coefficient ring must be a field for function '%s'."%(self.f.__name__))
--> 604 return self.f(self._instance, *args, **kwds)
605
606 require_field = RequireField
/opt/sage-6.4.beta4/local/lib/python2.7/site-packages/sage/rings/polynomial/multi_polynomial_ideal.pyc in wrapper(*args, **kwds)
503 """
504 with LibSingularDefaultContext():
--> 505 return func(*args, **kwds)
506 return wrapper
507
/opt/sage-6.4.beta4/local/lib/python2.7/site-packages/sage/rings/polynomial/multi_polynomial_ideal.pyc in complete_primary_decomposition(self, algorithm)
1079 if algorithm == 'sy':
1080 primdecSY = sage.libs.singular.ff.primdec__lib.primdecSY
-> 1081 P = primdecSY(self)
1082 elif algorithm == 'gtz':
1083 primdecGTZ = sage.libs.singular.ff.primdec__lib.primdecGTZ
/opt/sage-6.4.beta4/local/lib/python2.7/site-packages/sage/libs/singular/function.so in sage.libs.singular.function.SingularFunction.__call__ (build/cythonized/sage/libs/singular/function.cpp:13788)()
TypeError: Cannot call Singular function 'primdecSY' with ring parameter of type '<class 'sage.rings.polynomial.multi_polynomial_ring.MPolynomialRing_polydict_domain_with_category'>'
</pre>
Is there a way to work around this or is there really no implementation for primality checks in $\overline{\mathbb Q}[x_1,\dots,x_n]$?pegelWed, 03 Dec 2014 23:28:13 +0100https://ask.sagemath.org/question/25104/