Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

python - How to modify sympy's printing order?

I am using sympy to write algebraic expressions and perform basic calculations with them. Sympy does not keep track of the order of the variables which can be a problem when it comes to printing expressions (the issue has already been raised here and here, so this is not a duplicate). e.g.

>>> from sympy import *
>>> var("p,a")
>>> l=p-1-a;
>>> print(l);
-a+p-1

However, sympy seems to print the variables in the alphabetical order. Is there a way to change the alphebetical order Python refers to and thus trick sympy into printing the variables in the desired order? Any other solution is welcome!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Some new documentation on creating custom printers is in the pipe. Maybe that will help? I would create a custom printer which -- let's say we create a custom Add printer -- sorts the args based on some property like degree or sign of a term's coefficient, and then prints the resulting Add.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...