# subs a list with another list

I define

x = [var('x_%d' % k) for k in range(p)]
y = [var('y_%d' % k) for k in range(p)]
z =x + y
PRz = PolynomialRing(QQ,z)


with $p$ arbitrary, then I need new variables, to comare polynomials, define as:

t = [z[k] + 1/z[k] for k in range(p)]


Is there a way to substitute the list of variables $z$ with the list of varialbes $t$? The only way that I could do it was as

polynomial.subs(y_0 = t/2, y_1 = t/2, y_2 = t/2, y_3 = t/2)


But I want it for a general number of variables.

Thanks

edit retag close merge delete

Sort by » oldest newest most voted

You can use a Pyton dicttionary to define your substitution, and you can define your dictionary with comprehension:

For example, with p=5:

sage: d = {PRz(y[i]):t[i]/2 for i in range(p)}


You get the dictionary:

sage: d
{y_4: 1/2*x_4 + 1/2/x_4,
y_3: 1/2*x_3 + 1/2/x_3,
y_2: 1/2*x_2 + 1/2/x_2,
y_1: 1/2*x_1 + 1/2/x_1,
y_0: 1/2*x_0 + 1/2/x_0}


Then:

sage: polynomial = PRz.random_element()
sage: polynomial
2*x_0*x_1 - x_2*x_3 - x_1*y_2 - y_1*y_2 + 1/2*y_4

sage: polynomial.subs(d)
2*x_0*x_1 - 1/4*(x_1 + 1/x_1)*(x_2 + 1/x_2) - 1/2*x_1*(x_2 + 1/x_2) - x_2*x_3 + 1/4*x_4 + 1/4/x_4

more