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.Sat, 03 Mar 2018 13:02:25 +0100Get variable of polynomial ringhttps://ask.sagemath.org/question/41374/get-variable-of-polynomial-ring/I have a univariate polynomial ring and a polynomial in this ring, e.g.
R.<x> = PolynomialRing(ZZ);
p = 3*x^2 + 7*x - 5;
My question is: If I call a function with such a polynomial how can I figure out what the original variable was?
For instance, say I have the following:
def f(q) :
return q*x;
print f(p)
Of course that is a rather stupid example but I hope the idea is clear. How can I swap x with something more general like *f.parent().variable()* or is there a better way to do such things?
Thank you very much for your help!Sat, 03 Mar 2018 11:39:27 +0100https://ask.sagemath.org/question/41374/get-variable-of-polynomial-ring/Answer by j.c. for <p>I have a univariate polynomial ring and a polynomial in this ring, e.g.</p>
<pre><code>R.<x> = PolynomialRing(ZZ);
p = 3*x^2 + 7*x - 5;
</code></pre>
<p>My question is: If I call a function with such a polynomial how can I figure out what the original variable was?
For instance, say I have the following:</p>
<pre><code>def f(q) :
return q*x;
print f(p)
</code></pre>
<p>Of course that is a rather stupid example but I hope the idea is clear. How can I swap x with something more general like <em>f.parent().variable()</em> or is there a better way to do such things?</p>
<p>Thank you very much for your help!</p>
https://ask.sagemath.org/question/41374/get-variable-of-polynomial-ring/?answer=41376#post-id-41376`p.variables()` returns a tuple which contains the variables of `p`, so your function could be written:
def f(q):
return q*q.variables()[0]Sat, 03 Mar 2018 12:41:59 +0100https://ask.sagemath.org/question/41374/get-variable-of-polynomial-ring/?answer=41376#post-id-41376Comment by philipp7 for <p><code>p.variables()</code> returns a tuple which contains the variables of <code>p</code>, so your function could be written:</p>
<pre><code>def f(q):
return q*q.variables()[0]
</code></pre>
https://ask.sagemath.org/question/41374/get-variable-of-polynomial-ring/?comment=41377#post-id-41377Perfect, exactly what I was looking for. Thank you very much!Sat, 03 Mar 2018 13:02:25 +0100https://ask.sagemath.org/question/41374/get-variable-of-polynomial-ring/?comment=41377#post-id-41377