# SyntaxError: keyword can't be an expression

At the end I wanted to substitute qij^2=qij, i,j=1,2,3,4 the error: SyntaxError: keyword can't be an expression

the code: f1=(2*x1)+x2+1;

f2=-x1+(5*x2)-2;

e1=expand(f1^2);

e2=expand(f2^2);

var('q11,q12,q13,q14,q21,q22,q23,q24')

(q11,q12,q13,q14,q21,q22,q23,q24)

E1=e1.substitute(x1=q11+(2q12)-q13-(2q14), x2=q21+(2q22)-q23-(2q24));

E2=e2.substitute(x1=q11+(2q12)-q13-(2q14), x2=q21+(2q22)-q23-(2q24));

E1a=expand(E1);

E2a=expand(E2);

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);

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);

E1aa,E2aa

edit retag close merge delete

I would try this , but I'm not sure it's what you want, it's up to you to modify sorry if it is not what you want

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

( 2022-04-24 18:21:21 +0200 )edit
( 2022-04-24 22:07:14 +0200 )edit

Sort by ยป oldest newest most voted

The syntax ex.substitute(a=b, c=d) is read by binding the a and c identifiers to b and d respectively in a list of keyword arguments passed to the body of the subs method. This is possible if and only if a and c are acceptable python identifiers. Something like q12^2 is not.

Using either ex.subs([a==b, c==d]) or ex.subs({a:b, c:d}) syntaxes do not have this limitation ; BTW, the latter is the only one accepted by sympy ; standardizing on it might be a good idea.

HTH,

more