Ask Your Question
0

SyntaxError: keyword can't be an expression

asked 2022-04-24 17:27:52 +0200

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 flag offensive close merge delete

Comments

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);
E1aaDic={q11:q11^(1/2),q12:q12^(1/2),q13:q13^(1/2),q14:q14^(1/2),q21:q21^(1/2),q22:q22^(1/2),q23:q23^(1/2),q24:q24^(1/2)}
E1aa=E1a.substitute(E1aaDic)
show(E1aa)
ortollj gravatar imageortollj ( 2022-04-24 18:21:21 +0200 )edit
John Palmieri gravatar imageJohn Palmieri ( 2022-04-24 22:07:14 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-04-24 19:44:37 +0200

Emmanuel Charpentier gravatar image

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,

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2022-04-24 17:27:52 +0200

Seen: 46 times

Last updated: Apr 24