Ask Your Question
1

subs a list with another list

asked 2018-09-04 06:33:34 +0200

MarioM gravatar image

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[0]/2, y_1 = t[1]/2, y_2 = t[2]/2, y_3 = t[3]/2)

But I want it for a general number of variables.

Thanks

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2018-09-04 07:35:59 +0200

tmonteil gravatar image

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
edit flag offensive delete link more

Comments

Thank you!

MarioM gravatar imageMarioM ( 2018-09-04 16:38:43 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2018-09-04 06:33:34 +0200

Seen: 304 times

Last updated: Sep 04 '18