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.Mon, 28 May 2012 04:37:19 +0200Polynomial identityhttps://ask.sagemath.org/question/9006/polynomial-identity/I'm pretty interested in solving the following kind of problem using Sage: ``Let R be a polynomial ring in, say, x,y,z as variables over a field k. I'd like to find field-elements a,b,c such that
a(x^2+y^2)+b(xy+zx)+c(xyz)==0, if they exist (I know they do)''
so that Sage returns (a,b,c)=(0,0,0). That seems to be an easy matter if one can traduce the polynomial identity into a vector space identity. I proved to be unable to do so.
I've to say that my polynomial identities are quite more cumbersome and include up to 7 variables so working them by-hand is almost impossible in a finite ammount of time or patience.Sun, 27 May 2012 11:53:59 +0200https://ask.sagemath.org/question/9006/polynomial-identity/Answer by Mike Hansen for <p>I'm pretty interested in solving the following kind of problem using Sage: ``Let R be a polynomial ring in, say, x,y,z as variables over a field k. I'd like to find field-elements a,b,c such that</p>
<p>a(x^2+y^2)+b(xy+zx)+c(xyz)==0, if they exist (I know they do)''</p>
<p>so that Sage returns (a,b,c)=(0,0,0). That seems to be an easy matter if one can traduce the polynomial identity into a vector space identity. I proved to be unable to do so.</p>
<p>I've to say that my polynomial identities are quite more cumbersome and include up to 7 variables so working them by-hand is almost impossible in a finite ammount of time or patience.</p>
https://ask.sagemath.org/question/9006/polynomial-identity/?answer=13619#post-id-13619Here is some code which "linearizes" your problem:
def polynomial_identity(polys):
monomials = set()
for p in polys:
monomials.update(p.monomials())
base_ring = p.base_ring()
monomial_order = list(monomials)
m = []
for p in polys:
row = []
for monomial in monomial_order:
try:
row.append(base_ring(p.coefficient(monomial)))
except TypeError:
row.append(base_ring(0))
m.append(row)
m = matrix(m)
return m.kernel()
The result is a vector space which gives you all of your solutions. Your example
sage: polynomial_identity([x^2 + y^2, x*y + x*z, x*y*z])
Vector space of degree 3 and dimension 0 over Rational Field
Basis matrix:
[]
shows that (0,0,0) is the only answer. Another example:
sage: polynomial_identity([x+y, 2*x+2*y, 3*(x+y)])
Vector space of degree 3 and dimension 2 over Rational Field
Basis matrix:
[ 1 0 -1/3]
[ 0 1 -2/3]
Sun, 27 May 2012 18:35:53 +0200https://ask.sagemath.org/question/9006/polynomial-identity/?answer=13619#post-id-13619Comment by Angel for <p>Here is some code which "linearizes" your problem:</p>
<pre><code>def polynomial_identity(polys):
monomials = set()
for p in polys:
monomials.update(p.monomials())
base_ring = p.base_ring()
monomial_order = list(monomials)
m = []
for p in polys:
row = []
for monomial in monomial_order:
try:
row.append(base_ring(p.coefficient(monomial)))
except TypeError:
row.append(base_ring(0))
m.append(row)
m = matrix(m)
return m.kernel()
</code></pre>
<p>The result is a vector space which gives you all of your solutions. Your example</p>
<pre><code>sage: polynomial_identity([x^2 + y^2, x*y + x*z, x*y*z])
Vector space of degree 3 and dimension 0 over Rational Field
Basis matrix:
[]
</code></pre>
<p>shows that (0,0,0) is the only answer. Another example:</p>
<pre><code>sage: polynomial_identity([x+y, 2*x+2*y, 3*(x+y)])
Vector space of degree 3 and dimension 2 over Rational Field
Basis matrix:
[ 1 0 -1/3]
[ 0 1 -2/3]
</code></pre>
https://ask.sagemath.org/question/9006/polynomial-identity/?comment=19732#post-id-19732Cool! I actually managed myself to write a slightly different version of your code. Great. I really thank you for your help ^^'.Mon, 28 May 2012 04:37:19 +0200https://ask.sagemath.org/question/9006/polynomial-identity/?comment=19732#post-id-19732Comment by Mike Hansen for <p>Here is some code which "linearizes" your problem:</p>
<pre><code>def polynomial_identity(polys):
monomials = set()
for p in polys:
monomials.update(p.monomials())
base_ring = p.base_ring()
monomial_order = list(monomials)
m = []
for p in polys:
row = []
for monomial in monomial_order:
try:
row.append(base_ring(p.coefficient(monomial)))
except TypeError:
row.append(base_ring(0))
m.append(row)
m = matrix(m)
return m.kernel()
</code></pre>
<p>The result is a vector space which gives you all of your solutions. Your example</p>
<pre><code>sage: polynomial_identity([x^2 + y^2, x*y + x*z, x*y*z])
Vector space of degree 3 and dimension 0 over Rational Field
Basis matrix:
[]
</code></pre>
<p>shows that (0,0,0) is the only answer. Another example:</p>
<pre><code>sage: polynomial_identity([x+y, 2*x+2*y, 3*(x+y)])
Vector space of degree 3 and dimension 2 over Rational Field
Basis matrix:
[ 1 0 -1/3]
[ 0 1 -2/3]
</code></pre>
https://ask.sagemath.org/question/9006/polynomial-identity/?comment=19733#post-id-19733You can put this in ~/.sage/init.sage and it will be loaded each time you start Sage. You can also run "%cpaste" from the command line, paste the code in, and then put in "--" to finish the paste.Sun, 27 May 2012 23:32:42 +0200https://ask.sagemath.org/question/9006/polynomial-identity/?comment=19733#post-id-19733Comment by Angel for <p>Here is some code which "linearizes" your problem:</p>
<pre><code>def polynomial_identity(polys):
monomials = set()
for p in polys:
monomials.update(p.monomials())
base_ring = p.base_ring()
monomial_order = list(monomials)
m = []
for p in polys:
row = []
for monomial in monomial_order:
try:
row.append(base_ring(p.coefficient(monomial)))
except TypeError:
row.append(base_ring(0))
m.append(row)
m = matrix(m)
return m.kernel()
</code></pre>
<p>The result is a vector space which gives you all of your solutions. Your example</p>
<pre><code>sage: polynomial_identity([x^2 + y^2, x*y + x*z, x*y*z])
Vector space of degree 3 and dimension 0 over Rational Field
Basis matrix:
[]
</code></pre>
<p>shows that (0,0,0) is the only answer. Another example:</p>
<pre><code>sage: polynomial_identity([x+y, 2*x+2*y, 3*(x+y)])
Vector space of degree 3 and dimension 2 over Rational Field
Basis matrix:
[ 1 0 -1/3]
[ 0 1 -2/3]
</code></pre>
https://ask.sagemath.org/question/9006/polynomial-identity/?comment=19734#post-id-19734First of all I thank you for your interest. I knew nothing about Sage till today's evening. I tried to type your code directly into the console, but something happens at line 8. Is any way I can include this function in the core of the program so that I don't need to define it everytime from console (just in case I am able to do so...). I mainly understand your code even I know nothing about this language nor its functions. Any comment that lead to understanding would be really appreciated.Sun, 27 May 2012 20:22:30 +0200https://ask.sagemath.org/question/9006/polynomial-identity/?comment=19734#post-id-19734