Ask Your Question
1

Keyword can't be an expression error with subs()

asked 2018-09-25 19:49:12 +0200

trbillin gravatar image

I want to be able to iterate through a list of variables for function and make assignments to each of them. Here's an example that shows the issue I'm having:

R.<x,y> = PolynomialRing(QQ,2)
f = x^2+y^2
variables = [x,y]
f.subs(variables[0]=1)

This gives "SyntaxError: keyword can't be an expression." How can I get around this? The goal is to use a loop to make substitutions for some variables, look at what happens, then in the next iteration different substitutions will be made.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2018-09-25 22:18:46 +0200

tmonteil gravatar image

Instead of passing variables[0] as the name of an argument, you can pass a dictionary as an argument of subs method:

sage: R.<x,y> = PolynomialRing(QQ,2)
....: f = x^2+y^2
....: variables = [x,y]
....: f.subs({variables[0]:1})
y^2 + 1
edit flag offensive delete link more

Comments

Thank you, this fixed the issue!

trbillin gravatar imagetrbillin ( 2018-09-26 02:33:42 +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

Stats

Asked: 2018-09-25 19:49:12 +0200

Seen: 1,242 times

Last updated: Sep 25 '18