Angie Mitselou
Angie Mitselou

Reputation: 21

SAGEMATH: SyntaxError: keyword can't be an expression

At the end I wanted to substitute qij^2=qij, i,j=1,2,3,4 and got the error:

SyntaxError: keyword can't be an expression

The code:

sage: f1 = 2*x1 + x2 + 1
sage: f2 = -x1 + 5*x2 - 2
sage: e1 = expand(f1^2)
sage: e2 = expand(f2^2)
sage: var('q11, q12, q13, q14, q21, q22, q23, q24')
(q11, q12, q13, q14, q21, q22, q23, q24)
sage: E1 = e1.substitute(x1=q11+(2q12)-q13-(2q14), x2=q21+(2q22)-q23-(2q24))
sage: E2 = e2.substitute(x1=q11+(2q12)-q13-(2q14), x2=q21+(2q22)-q23-(2q24))
sage: E1a = expand(E1)
sage: E2a = expand(E2)
sage: E1aa = E1a.substitute(q11^2=q11, q12^2=q12, q13^2=q13, q14^2=q14,
....:                       q21^2=q21, q22^2=q22, q23^2=q23, q24^2=q24)
sage: E2aa = E2a.substitute(q11^2=q11, q12^2=q12, q13^2=q13, q14^2=q14,
....:                       q21^2=q21, q22^2=q22, q23^2=q23, q24^2=q24)
sage: E1aa, E2aa

Upvotes: 1

Views: 153

Answers (1)

John Palmieri
John Palmieri

Reputation: 1696

If you want to just substitute for a variable, you can use the = syntax: E1a.substitute(q11=(whatever)). If, though, you want to substitute for a more complicated expression, you need to use a different syntax:

E1a.substitute(q11^2=q1)    # will raise an error
E1a.substitute(q11^2==q1)   # should work
E1a.substitute({q11^2: q1}) # should work

The help message produced by E1a.substitute? gives some examples.

Upvotes: 1

Related Questions