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.Wed, 26 Sep 2018 02:33:42 +0200Keyword can't be an expression error with subs()https://ask.sagemath.org/question/43762/keyword-cant-be-an-expression-error-with-subs/ 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.Tue, 25 Sep 2018 19:49:12 +0200https://ask.sagemath.org/question/43762/keyword-cant-be-an-expression-error-with-subs/Answer by tmonteil for <p>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:</p>
<pre><code>R.<x,y> = PolynomialRing(QQ,2)
f = x^2+y^2
variables = [x,y]
f.subs(variables[0]=1)
</code></pre>
<p>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.</p>
https://ask.sagemath.org/question/43762/keyword-cant-be-an-expression-error-with-subs/?answer=43763#post-id-43763Instead 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
Tue, 25 Sep 2018 22:18:46 +0200https://ask.sagemath.org/question/43762/keyword-cant-be-an-expression-error-with-subs/?answer=43763#post-id-43763Comment by trbillin for <p>Instead of passing <code>variables[0]</code> as the name of an argument, you can pass a dictionary as an argument of <code>subs</code> method:</p>
<pre><code>sage: R.<x,y> = PolynomialRing(QQ,2)
....: f = x^2+y^2
....: variables = [x,y]
....: f.subs({variables[0]:1})
y^2 + 1
</code></pre>
https://ask.sagemath.org/question/43762/keyword-cant-be-an-expression-error-with-subs/?comment=43766#post-id-43766Thank you, this fixed the issue!Wed, 26 Sep 2018 02:33:42 +0200https://ask.sagemath.org/question/43762/keyword-cant-be-an-expression-error-with-subs/?comment=43766#post-id-43766