On Thursday, 5 May 2022 at 02:03:30 UTC-7 Ha wrote:

> For Example:  I tried this:
>
> f(X[1] = 1, X[2] = 5) 
>
> and got this error:
>
> File "<ipython-input-6-3b58a4eab255>", line 10 f(X[Integer(1)] = 
> Integer(1), X[Integer(2)] = Integer(5)) 
> ^ SyntaxError: keyword can't be an expression
>
> Indeed, that does not work. The syntax f(x0=1,x1=5) works via python's 
keyword parameter mechanism. For it to work. the keyword argument used (x0 
and x1 in the example) must match the print names of the variables of the 
polynomial ring. Those print names are x0,x1,...,x9 in your example. The 
names X[1] and X[2] do not match. What's worse: they are not valid python 
keywords, as the error says! So you don't even get to the matching phase.

What you would need is the *value* of X[1], X[2] instead (which is 'x1' and 
'x2' respectively). 

Python has some magic that allows you to specify the keyword names through 
expressions:

f(**{X[1]: 1, X[2]: 5})

should do the trick. It basically evaluates to f(**{'x1': 1, 'x2': 5}), and 
f(x1=1,x2=5) basically translates to f(**{'x1':1,'x2':5}) as well.

The keyword trick is really just there for convenience. The workarounds 
above may reduce convenience by quite a bit, so perhaps you prefer to just 
use "full evaluation" instead. For instance, for your example, you could do

v=list(X)
v[1]=1
v[2]=5
f(v)

(as presented here, it's less compact, but in your actual application it 
may be more direct)

-- 
You received this message because you are subscribed to the Google Groups 
"sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-support+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sage-support/2b14d6bd-f4e6-4f42-97dc-4109a7f605a0n%40googlegroups.com.

Reply via email to