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, 19 Sep 2020 19:21:22 +0200Functions in polynomials ringshttps://ask.sagemath.org/question/53502/functions-in-polynomials-rings/ I want to define a function in a polynomial ring in several variables.
R.<x1,x3,x5>=PolynomialRing(QQ)
I am trying to define a function that takes $(i,j)$ to $x_i^j$.
I tried
def f(i,j):
return xi^j
This does not work.
I tried replacing xi with x[i], that doesn't work. Can someone please tell me what I am doing wrong and how to fix it?
If instead of taking 3 variables I take only 1 variable then the method works.Sat, 19 Sep 2020 06:59:12 +0200https://ask.sagemath.org/question/53502/functions-in-polynomials-rings/Answer by rburing for <p>I want to define a function in a polynomial ring in several variables. </p>
<pre><code>R.<x1,x3,x5>=PolynomialRing(QQ)
</code></pre>
<p>I am trying to define a function that takes $(i,j)$ to $x_i^j$.</p>
<p>I tried</p>
<pre><code>def f(i,j):
return xi^j
</code></pre>
<p>This does not work.
I tried replacing xi with x[i], that doesn't work. Can someone please tell me what I am doing wrong and how to fix it?
If instead of taking 3 variables I take only 1 variable then the method works.</p>
https://ask.sagemath.org/question/53502/functions-in-polynomials-rings/?answer=53503#post-id-53503You want to access the generators of `R` as a tuple:
sage: R = PolynomialRing(QQ, 3, names='x'); R
Multivariate Polynomial Ring in x0, x1, x2 over Rational Field
sage: x = R.gens(); x
(x0, x1, x2)
sage: x[0]
x0
If you want to use some strange alternative indexing, then you can achieve it with a function.Sat, 19 Sep 2020 10:18:47 +0200https://ask.sagemath.org/question/53502/functions-in-polynomials-rings/?answer=53503#post-id-53503Answer by slelievre for <p>I want to define a function in a polynomial ring in several variables. </p>
<pre><code>R.<x1,x3,x5>=PolynomialRing(QQ)
</code></pre>
<p>I am trying to define a function that takes $(i,j)$ to $x_i^j$.</p>
<p>I tried</p>
<pre><code>def f(i,j):
return xi^j
</code></pre>
<p>This does not work.
I tried replacing xi with x[i], that doesn't work. Can someone please tell me what I am doing wrong and how to fix it?
If instead of taking 3 variables I take only 1 variable then the method works.</p>
https://ask.sagemath.org/question/53502/functions-in-polynomials-rings/?answer=53513#post-id-53513One can produce strings and have the polynomial ring eat them.
String formatting is easy thanks to Python.
Define a polynomial ring as in the question:
R.<x1, x3, x5> = PolynomialRing(QQ)
Define a "generator power" function as follows:
def f(i, j):
r"""
Return the polynomial variable xi raised to the j-th power.
"""
return R('x{}^{}'.format(i, j))
Example:
sage: f(3, 2)
x3^2
Sat, 19 Sep 2020 19:21:22 +0200https://ask.sagemath.org/question/53502/functions-in-polynomials-rings/?answer=53513#post-id-53513