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.Tue, 21 Jul 2020 10:46:56 +0200how to get the coefficient of a multivariate polynomial with respect to a specific variable and degree, in a quotient ring ?https://ask.sagemath.org/question/52594/how-to-get-the-coefficient-of-a-multivariate-polynomial-with-respect-to-a-specific-variable-and-degree-in-a-quotient-ring/Here is what I tried.
sage: F = ZZ.quo(3*ZZ); F
sage: A.<X, Y, Z> = PolynomialRing(F); A
sage: R.<x, y, z> = A.quotient(ideal(X^2 - 1, Y^2 - 1, Z^2 - 1))
sage: f = x*z + x*y*z + y + 1
sage: f.coefficient(z, 1)
sage: f.coefficient({z: 1})
sage: f.coeffcient(z)andriamTue, 21 Jul 2020 10:46:56 +0200https://ask.sagemath.org/question/52594/Computing a Basis of Polynomials (As a Vector Space)https://ask.sagemath.org/question/49610/computing-a-basis-of-polynomials-as-a-vector-space/Hello everyone! I'm fairly new to sage, so this is probably super simple but I can't quite figure it out.
So $S_n$ acts on the multivariate polynomial ring $M = \mathbb{C} [x_1,x_2,...,x_n,y_1,y_2,...y_n]$ by simply $\sigma*x_i = x_{\sigma(i)}$ (and same for the $y_i$'s). This turns $M$ into a $S_n$-Module, and by extension of linearity a module of the group ring $\mathbb{C}S_n$.
This isn't the important part however; I'm studying submodules of the thing above, and the problem right now is I have a bunch of polynomials (hundreds) of considerable length, but I want a minimal generating set (only additively). Is there a command that does this quickly without having to convert everything into a giant $(n+1)^{n+1}$-dimensional vector space?
Thanks so much in advance!RaymondChouMon, 20 Jan 2020 06:23:50 +0100https://ask.sagemath.org/question/49610/Groebner basis computationhttps://ask.sagemath.org/question/45340/groebner-basis-computation/ How we can use SageMath for computing Groebner basis? e.g. we have 35 multivariate polynomial equations, is it possible to compute groebner basis at a time?MMumtazWed, 06 Feb 2019 07:49:28 +0100https://ask.sagemath.org/question/45340/get the coefficients of polynomial of several variables?https://ask.sagemath.org/question/41526/get-the-coefficients-of-polynomial-of-several-variables/ Consider that I have a polynomial with n variables x1,x2,...,xn and want to get the coefficient of that polynomial. For example I can have:
x1+2*x1*x5^2+3*x1^2*x4-5*x2*x5^2+1/2*x2*x3+x6^2*x7*x8+9*x3*x9^3
I would like to ask Sage to give coefficient for the polynomial with variables x1..x9 and the result should be something like this:
coefficient,(exponent of x1..x9);
1,(1,0,0,0,0,0,0,0,0);
2,(1,0,0,0,2,0,0,0,0);
3,(2,0,0,1,0,0,0,0,0);
-5,(0,1,0,0,2,0,0,0,0);
1/2,(0,1,1,0,0,0,0,0,0);
1,(0,0,0,0,0,2,1,1);
9,(0,0,1,0,0,0,0,0,3)
How can I achieve this?
DanialBaghWed, 14 Mar 2018 03:12:52 +0100https://ask.sagemath.org/question/41526/Is there a way to get the homogeneous part of certain degree of a (multivariate) polynomial?https://ask.sagemath.org/question/39177/is-there-a-way-to-get-the-homogeneous-part-of-certain-degree-of-a-multivariate-polynomial/Every multivariate polynomial $f\in\Bbb F[x_1,\ldots,x_n]$ of degree $d$ can be written as $f = f_0+f_1+\cdots+f_d$, where $f_i$ is a **homogeneous** polynomial of degree $i$. Is there a *direct* way to get each $f_i$ given $f$ in SageMath? For a specific application I have where I only need $f_d$ I am homogenizing and then setting $h=0$, and based on this I wrote an ugly script that recursively finds $f_i$.
Is there a cleaner (and more efficient) way to do this?
Thanks for the help!
**EDIT:**
This is the code I'm using to obtain $f_d$ from $f$
fd = R( f.homogenize()(h=0) )
where R is the multivariate polynomial ring (parent of $f$). If I want $f_{d-1}$ for example, I can define $g$ as $f - f_d$ and apply the line to $g$. This recursive definition is not satisfactory since to get $f_i$ I need to have all $f_{i+1},\ldots,f_d$ first, which is inefficient. Also, that trick of homogenizing, evaluating $h=0$ and coercing the result back to the original polynomial ring is not very neat.descuderoSun, 15 Oct 2017 19:37:26 +0200https://ask.sagemath.org/question/39177/Remove a variable from a polynomial ring k(a,b)[x1,x2,x0] where a,b are parametershttps://ask.sagemath.org/question/38710/remove-a-variable-from-a-polynomial-ring-kabx1x2x0-where-ab-are-parameters/ I am trying to homogenize polynomials using variable `x0` in a polynomial ring `k(a,b)[x1,x2]` defined as follows:
R.<a,b> = PolynomialRing( QQ, order='degrevlex' )
K = FractionField( R )
RK.<x1,x2> = PolynomialRing( K, order='degrevlex' )
After homogenization, I define the new polynomial ring with block order:
RKH.<x1,x2,x0>=PolynomialRing(K,order='degrevlex(2),degrevlex(1)')
Then in my program, I need to dehomogenize my polynomials by setting `x0=1`, and remove the variable `x0` from the polynomial ring. This works fine in a polynomial ring without the parameter fraction field. For example
P.<x,y,z>=PolynomialRing(QQ,order='degrevlex(2),degrevlex(1)')
fp=x^2+x*y+4*z^2
R=P.remove_var(z,order='degrevlex');R
R(fp(z=1)).parent()
Multivariate Polynomial Ring in x, y, z over Rational Field
Multivariate Polynomial Ring in x, y over Rational Field
Multivariate Polynomial Ring in x, y over Rational Field
However, with the fraction field `k(a,b)`, the same method does not work any more:
R.<a,b> = PolynomialRing( QQ, order='degrevlex' )
K = FractionField( R )
RK.<x1,x2> = PolynomialRing( K, order='degrevlex' )
RKH.<x1,x2,x0>=PolynomialRing(K,order='degrevlex(2),degrevlex(1)')
pf=a*x1^2-b*x1*x2+x0^2
RKHn=RKH.remove_var(x0,order='degrevlex')
pfn=pf(x0=1)
RKHn(pfn)
Multivariate Polynomial Ring in x1, x2, x0 over Fraction Field of Multivariate Polynomial Ring in a, b over Rational Field
Error in lines 11-11
TypeError: not a constant polynomial
I didn't copy down the whole error message so it doesn't look so long. Is there a way to fix this? Thank you for your help!KittyLFri, 01 Sep 2017 21:09:49 +0200https://ask.sagemath.org/question/38710/Multivariate symbolicshttps://ask.sagemath.org/question/37292/multivariate-symbolics/Consider this piece of code:
sage: n=4 # dimension of state-space
sage: x = polygens(QQ, ['x'+str(i) for i in [1..n]]); x # vector of state variables
(x1, x2, x3, x4)
There is a handy keyword with formal power series, the number of generators (`num_gens`), and it works like:
sage: PowerSeriesRing(QQ, 'x', num_gens=5)
Multivariate Power Series Ring in x0, x1, x2, x3, x4 over Rational Field
There is a kind of analogue with polynomials, but not keyworded:
sage: PolynomialRing(QQ, 'x', 5)
Multivariate Polynomial Ring in x0, x1, x2, x3, x4 over Rational Field
Is it possible to use any of these to define a vector of polynomial variables in the object `x` as the beginning (but without list comprehension)?
---
As a side note, i think the `var` constructor also lacks a vector form?
sage: var(['x'+str(i) for i in [1..n]])
(x1, x2, x3, x4)
This could be useful to simplify constructions as:
sage: A = matrix(SR, [['a'+str(i)+str(j) for j in [1..n]] for i in [1..n]]); A
[a11 a12 a13 a14]
[a21 a22 a23 a24]
[a31 a32 a33 a34]
[a41 a42 a43 a44]
In Matlab this is `A = sym('a', n)`, but i didn't find a similar shortcut in Sage.mforetsFri, 14 Apr 2017 08:59:58 +0200https://ask.sagemath.org/question/37292/Division algorithm in a polynomial ring with variable coefficientshttps://ask.sagemath.org/question/37098/division-algorithm-in-a-polynomial-ring-with-variable-coefficients/ I am working on an algorithm to divide a polynomial `f` by a list of polynomials `[g1, g2, ..., gm]`. The following is my algorithm:
def div(f,g): # Division algorithm on Page 11 of Using AG by Cox;
# f is the dividend;
# g is a list of ordered divisors;
# The output consists of a list of coefficients for g and the remainder;
# p is the intermediate dividend;
n = len(g)
p, r, q = f, 0, [0 for x in range(0,n)]
while p != 0:
i, divisionoccured = 0, False
print(p,r,q);
while i < n and divisionoccured == False:
if g[i].lt().divides(p.lt()):
q[i] = q[i] + p.lt()//g[i].lt()
p = p - (p.lt()//g[i].lt())*g[i]
divisionoccured = True
else:
i = i + 1
if divisionoccured == False:
r = r + p.lt()
p = p - p.lt()
return q, r
Here is an example of implementing the algorithm:
K.<a,b> = FractionField(PolynomialRing(QQ,'a, b'))
P.<x,y,z> = PolynomialRing(K,order='lex')
f=a*x^2*y^3+x*y+2*b
g1=a^2*x+2
g2=x*y-b
div(f,[g1,g2])
Here is the result:
(a*x^2*y^3 + x*y + 2*b, 0, [0, 0])
(((-2)/a)*x*y^3 + x*y + 2*b, 0, [1/a*x*y^3, 0])
(x*y + 4/a^3*y^3 + 2*b, 0, [1/a*x*y^3 + ((-2)/a^3)*y^3, 0])
(4/a^3*y^3 + ((-2)/a^2)*y + 2*b, 0, [1/a*x*y^3 + ((-2)/a^3)*y^3 + 1/a^2*y, 0])
(((-2)/a^2)*y + 2*b, 4/a^3*y^3, [1/a*x*y^3 + ((-2)/a^3)*y^3 + 1/a^2*y, 0])
(2*b, 4/a^3*y^3 + ((-2)/a^2)*y, [1/a*x*y^3 + ((-2)/a^3)*y^3 + 1/a^2*y, 0])
Error in lines 6-6
Traceback (most recent call last):
and some other error messages.
We can see that it worked well until the leading term is `2b`. it does not recognize the `2b` as a term. I tried:
(x).lt().divides(1)
It gives the answer `False`. But I tried
(x).lt().divides(a)
It gives error message. Is there a way to solve this? Thank you for your help!
KittyLMon, 27 Mar 2017 21:26:48 +0200https://ask.sagemath.org/question/37098/Computing square free part of a multivariate polynomialhttps://ask.sagemath.org/question/36768/computing-square-free-part-of-a-multivariate-polynomial/In one of the algorithms I am working on, there is a part asking for the square-free part of a multivariate polynomial. I can find it using a complicated way, namely,
I = ideal(f)
IRad = I.radical().groebner_basis()
But I think this must be a very inefficient way to do it. Is there a better way? Thank you!KittyLTue, 28 Feb 2017 21:49:14 +0100https://ask.sagemath.org/question/36768/Find the kernel of a matrix $A$ and make it a matrix.https://ask.sagemath.org/question/33533/find-the-kernel-of-a-matrix-a-and-make-it-a-matrix/ I am trying to write a function that computes the monic genrator of an ideal $I\in k[x_1,\dots,x_n]$, i.e., the generator of $I\cap k[x_i]$ for each $i$. For this I need to use linear algebra for the set
$$\{1, x_i, x_i^2,\dots\}$$
I write each one of them in terms of the basis for the quotient ring $k[x_1,\dots,x_n]/I$, and see if they are linearly dependent.
Since I add in one more power a time, when I find a linearly dependent set, it should have nullity $1$. So if I can get the one element in basis of the kernel, I am done. But the $A.kernel()$ command in Sage gives me this:
N=M.kernel();N
Vector space of degree 2 and dimension 1 over Rational Field
Basis matrix:
[0 1]
Is there a way to assign it as a vector using the kernel command? Or do I have to write my own function to implement it? Thank you for your help!KittyLTue, 24 May 2016 13:47:57 +0200https://ask.sagemath.org/question/33533/Pass a list of variable names as parameter to a polynomial ringhttps://ask.sagemath.org/question/33526/pass-a-list-of-variable-names-as-parameter-to-a-polynomial-ring/ I am trying to write a function that compute a vector space basis $B$ for the quotient ring $k[x_1,\dots,x_n]/I$. I want to make the list of variables as the input parameter.
I tried this:
var("x,y")
Vlist=[x,y]
P.<Vlist>=PolynomialRing(QQ,order='degrevlex')
f=x^2+y^3
f.lm()
It gave me error message. I also tried
Vlist=['x,y']
or
Vlist=["x,y"]
None of them works.
I know that
P.<x,y>=PolynomialRing(QQ,order='degrevlex')
f=x^2+y^3
f.lm()
works. So I can just type this before I run my function. But is there a way that I can make this as input of the function?KittyLMon, 23 May 2016 19:17:44 +0200https://ask.sagemath.org/question/33526/How to flatten polynomial rings?https://ask.sagemath.org/question/26969/how-to-flatten-polynomial-rings/ It is possible in Sage to create a polynomial ring over a polynomial ring. But Sage stores this as a two-tiered structure that is sometimes inconvenient. I would like to flatten it.
For example,
sage: P = PolynomialRing(QQ, 'c', 6)
sage: S = PolynomialRing(P, 't', 6)
creates a polynomial ring in t0, t1, t2, ...., t5 over a polynomial ring in c0, c1, ..., c5. But then
sage: clist = list( P.gens()[i] for i in range(6)) # get names for variables
sage: tlist = list( S.gens()[i] for i in range(6))
sage: poly = clist[0]*tlist[0] # produces polynomial c0*t0 in S
sage: poly.polynomial(clist[0]) # asks for poly as a polynomial in c0
fails with "var must be one of the generators of the parent polynomial
ring."
I would like to `flatten' S so it is simply a single polynomial ring over QQ in 12 variables.
You may ask, why don't I just construct a single ring in the first place? In this instance, because I want to create a polynomial ring in c0, ...., cn and t0, ...., tn where n is a variable, and I don't know how to do this (I'd prefer to avoid just making c0, ..., c2n and trying to keep track of which are actually t's). I can imagine other situations where one would like to be able to flatten also.Kate StangeThu, 28 May 2015 17:44:37 +0200https://ask.sagemath.org/question/26969/multivariate polynomial root-findinghttps://ask.sagemath.org/question/10331/multivariate-polynomial-root-finding/I have a series of large, high-degree, bivariate polynomials in two variables, p and q. For example, one of these polynomials is:
$p^7 (25/4 q^6 - 75 q^5 + 525/2 q^4 - 400 q^3 + 300 q^2 - 120 q + 20) + p^6 (-175/8 q^6 + 525/2 q^5 - 3675/4 q^4 + 1400 q^3 - 1050 q^2 + 420 q - 70) + p^5 (255/8 q^6 - 765/2 q^5 + 2655/2 q^4 - 1980 q^3 + 1425 q^2 - 540 q + 84) + p^4 (-25 q^6 + 300 q^5 - 8175/8 q^4 + 1450 q^3 - 1875/2 q^2 + 300 q - 35) + p^3 (45/4 q^6 - 135 q^5 + 1785/4 q^4 - 580 q^3 + 300 q^2 - 60 q) + p^2 (-45/16 q^6 + 135/4 q^5 - 855/8 q^4 + 120 q^3 - 75/2 q^2) + p (5/16 q^6 - 15/4 q^5 + 45/4 q^4 - 10 q^3) + 1$
I would like to find all values of $q$ for which this polynomial is equal to $1-p$. The following code block illustrates my problem:
sage: fid7tof
5/4*(5*q^6 - 60*q^5 + 210*q^4 - 320*q^3 + 240*q^2 - 96*q + 16)*p^7 - 35/8*(5*q^6 - 60*q^5 + 210*q^4 - 320*q^3 + 240*q^2 - 96*q + 16)*p^6 + 3/8*(85*q^6 - 1020*q^5 + 3540*q^4 - 5280*q^3 + 3800*q^2 - 1440*q + 224)*p^5 - 5/8*(40*q^6 - 480*q^5 + 1635*q^4 - 2320*q^3 + 1500*q^2 - 480*q + 56)*p^4 + 5/4*(9*q^6 - 108*q^5 + 357*q^4 - 464*q^3 + 240*q^2 - 48*q)*p^3 - 15/16*(3*q^6 - 36*q^5 + 114*q^4 - 128*q^3 + 40*q^2)*p^2 + 5/16*(q^6 - 12*q^5 + 36*q^4 - 32*q^3)*p + 1
sage: solve([fid7tof == 1-p], q)
[0 == 5*(10*p^4 - 20*p^3 + 16*p^2 - 6*p + 1)*q^6 - 60*(10*p^4 - 20*p^3 + 16*p^2 - 6*p + 1)*q^5 + 30*(70*p^4 - 140*p^3 + 109*p^2 - 39*p + 6)*q^4 - 160*(20*p^4 - 40*p^3 + 29*p^2 - 9*p + 1)*q^3 + 160*p^4 + 600*(4*p^4 - 8*p^3 + 5*p^2 - p)*q^2 - 320*p^3 - 960*(p^4 - 2*p^3 + p^2)*q + 112*p^2 + 48*p + 16]
All Sage returns is another multivariate polynomial, which doubtless has many solutions $\bar{q}(p)$. Is there something I can do to get these solutions?bcrigerMon, 08 Jul 2013 14:10:36 +0200https://ask.sagemath.org/question/10331/Turning an expression into a multivariate polynomial?https://ask.sagemath.org/question/10198/turning-an-expression-into-a-multivariate-polynomial/Suppose I have the expression
-a^3x^2 - a^2xy^2 + axy + bx^2 + 2bxy + xy^2
I want to turn this into a polynomial in x and y:
(1-a^2)xy^2+(a+2*b)xy+(b-a^3)x^2
so that I can then extract the coefficients (by setting x=1, y=1 in the list of operands of the new expression). How do I tell Sage which of the four variables will be the polynomial variables?AlasdairThu, 06 Jun 2013 00:57:36 +0200https://ask.sagemath.org/question/10198/Polynomial as a sum of simply factored expressions?https://ask.sagemath.org/question/9761/polynomial-as-a-sum-of-simply-factored-expressions/The polynomial
p^9 + p^8 + 7*p^6 + 6*p^4 + 3*p^3 + 4*p^2 + 2
can't be factored (over the rationals). However, it can be expressed in simpler form as
(p^3+1)^3 + (p^2+1)^4
Is there any way (other than trial and error) of finding such a sum, for a given (multivariate) polynomial?AlasdairTue, 19 Feb 2013 04:56:58 +0100https://ask.sagemath.org/question/9761/Explicit representation of element of idealhttps://ask.sagemath.org/question/8636/explicit-representation-of-element-of-ideal/ P.<x,y,z,t>=PolynomialRing(QQ,4)
C1= 17*x^2 + 7*y^2 - 26*y*z + 7*z^2
C2= 13*y^2 - 7*y*z + 13*z^2 - 51*t^2
x^4+y^4+z^4-18*t^4 in ideal(C1,C2)
So there exist \alpha, \beta in P with <latex>\alpha C1 + \beta C2 = x^4+y^4+z^4-18*t^4</latex>
What's the command to find \alpha and \beta explicitly ?fivemackTue, 17 Jan 2012 19:40:59 +0100https://ask.sagemath.org/question/8636/