Ask Your Question
1

Reduced form of symbolic expressions

asked 2023-07-27 08:00:27 +0100

I am working in symbolic ring with two symbols: q and h. The relation between them is $q=e^h$. I want to do some algebra of matrices with symbolic entries. The entries of the input matrices consist of both q and h. I want that the entries of the output matrix to only contain q i.e. any expression in h should be converted into expressions in q. The following is my code:

q = SR.var('q')
h = SR.var('h')
relation = q == exp(h)

Q = matrix(SR,[[0,0,0,e^(-h)+e^(h)],[1,0,1,0],[-e^h,0,e^(-h),0],[0,1,0,0]]) 
Qi= Q.inverse() 
R=matrix(SR,[[q,0,0,0],[0,q-q^-1,1,0],[0,1,0,0],[0,0,0,q]])
print(Qi*R.subs({e^h:q},{e^-h:q^-1}).simplify_full())

Here is the output:

[                                                       0 -(q^2 - 1)*(e^h/(e^(-h) + e^h) - 1)/q - 1/(e^(-h) + e^h)                                  -e^h/(e^(-h) + e^h) + 1                                                        0]
[                                                       0                                                        0                                                        0                                                        q]
[                                                       0      (q^2 - 1)*e^h/(q*(e^(-h) + e^h)) + 1/(e^(-h) + e^h)                                       e^h/(e^(-h) + e^h)                                                        0]
[                                        q/(e^(-h) + e^h)                                                        0                                                        0                                                        0]

How can I fix this? If I add .subs(..) at the end of both the matrices then all h expressions are changed into expressions of q. However they are still not reduced, for example I get expressions like below

-q/(q^2 + 1) + (q^2 - 1)/((q^2 + 1)*q)
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-07-27 10:15:26 +0100

tolga gravatar image

Maybe it is trivial but can (Qi*R).subs(h==log(q)).simplify_full() solve your problem?

edit flag offensive delete link more

Comments

1

Keeping everything in h is easier: (Qi*R).subs(relation).simplify_full()

tolga gravatar imagetolga ( 2023-07-27 10:26:01 +0100 )edit

Adding brackets and using h==log(q) worked. Thank you!

Shruti Barapatre gravatar imageShruti Barapatre ( 2023-07-27 11:48:15 +0100 )edit

Can be shortened to ;

sage: var("q, h")
(q, h)
sage: rel=q==e^h
sage: Q=matrix([[0,0,0,e^(-h)+e^(h)],[1,0,1,0],[-e^h,0,e^(-h),0],[0,1,0,0]])
sage: (Q^-1).apply_map(lambda u:u.subs(solve(rel,h)[0]))
[               0 -q/(q + 1/q) + 1     -1/(q + 1/q)                0]
[               0                0                0                1]
[               0      q/(q + 1/q)      1/(q + 1/q)                0]
[     1/(q + 1/q)                0                0                0]
Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2023-07-28 08:22:22 +0100 )edit

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: 2023-07-27 08:00:27 +0100

Seen: 181 times

Last updated: Jul 27 '23