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
580 views
in Technique[技术] by (71.8m points)

python - Prevent Sympy from rearranging the equation

Perhaps im overlooking the obvious but how do you prevent sympy from rearranging equations?

Im using Sympy in the iPython notebook so i can easily copy-paste the Latex code to Lyx, but i want the equations in the same order as i defined them.

For example, the formula for grey-body radiation as a function of its temperature:

enter image description here

Sympy automatically places the Temperature component to the front, which gives a very unusual representation of the formula. Is there anyway to prevent this?

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

Currently, there is no way in SymPy to print things exactly as they are entered, because that information isn't even saved anywhere.

I believe in a multiplication, symbols are ordered alphabetically, with capital letters coming before lowercase letters (basically, the order from ord). The best trick I can come up with is to use the symbol_names option to latex, which lets you change the name used for a Symbol in its LaTeX representation. The ordering will still be based on the original symbol's name, so you can trick it:

>>> from sympy.abc import epsilon, omega, t
>>> latex(epsilon*sigma*t**4, symbol_names={t:"T"})
epsilon sigma T^{4}

If you want this to pretty print nicely in the notebook, you'll have to write a custom version of the printing extension that passes your symbol_names dict to latex. See https://github.com/sympy/sympy/blob/master/sympy/interactive/printing.py (ideally one could just pass the latex options to init_printing, I'll open an issue in the SymPy bug tracker for that).


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

...