# Reduced form of symbolic expressions

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

Sort by ยป oldest newest most voted

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

more

1

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

( 2023-07-27 10:26:01 +0200 )edit

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

( 2023-07-27 11:48:15 +0200 )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]

( 2023-07-28 08:22:22 +0200 )edit