# 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.

edit retag close merge delete

Sort by » oldest newest most voted

You 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.

more

One 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

more