Sympy Forced Print Order

SymPyperfectly keeps track of all the operations that I perform with my symbolic expressions. But at the time of printing the result for latex output, I would like to put in order a specific term. This is just for convention, and unfortunately this convention is not alphabetical for the symbol name (as reasonable sympy)

import sympy as sp
sp.init_printing()
U,tp, z, d = sp.symbols('U t_\perp z d')
# do many operations with those symbols
# the final expression is:

z+tp**2+U+U/(z-3*tp)+d

My problem is what the SymPyexpression is ordered as

U + U/(-3*t_\perp + z) + d + t_\perp**2 + z

But this order is not an agreement in my area. For us it zshould be the leftmost expression, then tp, then U, even if it is written in capital letters, it dis the most inconsequential and placed on the right. All these variables have special meaning, and that is why we write them in this order, and the reason for the code variables is named in this way.

I do not want to rename zto aand, as suggested in , does not allow Sympy to rearrange the equation , and then at the time of printing convert it ato z. In Force SymPy, there is a hint to maintain order of terms that I can write a sort function, but could not find documentation about this.

+5

Source: https://habr.com/ru/post/1678708/


All Articles