12

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?

Rutger Kassies
  • 61,630
  • 17
  • 112
  • 97

2 Answers2

10

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).

asmeurer
  • 86,894
  • 26
  • 169
  • 240
  • I opened [this](http://code.google.com/p/sympy/issues/detail?id=3613) and [this](http://code.google.com/p/sympy/issues/detail?id=3612) SymPy issue for things I mentioned in my answer. – asmeurer Feb 03 '13 at 00:06
  • Thanks a lot for answering and opening the issue. I think it would be a nice addition eventhough visualization isnt really what Sympy is about. The future of the notebook looks good and converting it to a pdf/blog or slideshow will probably be common stuff in the near future. – Rutger Kassies Feb 03 '13 at 19:39
  • 9
    We do care about visualization, though. SymPy wouldn't be nearly as useful as it is without it's printing capabilities. – asmeurer Feb 03 '13 at 23:43
5

If you want the arguments of a product to appear in a given order you can create a Mul from those args and then tell the printer that you don't want them reordered as shown here for an Add. So for your example you could do

>>> var('epsilon, sigma, T')
(epsilon, sigma, T)
>>> this = Mul(epsilon, sigma, T**4, evaluate=False)
>>> StrPrinter({'order':'none'})._print_Mul(this)
epsilon*sigma*T**4
smichr
  • 16,948
  • 2
  • 27
  • 34
  • You could also use `_print` directly as `keep_order_print = StrPrinter({"order":"none"})._print`. accordingly to the [doc point 2](https://docs.sympy.org/latest/modules/printing.html?highlight=_sympystr#which-method-is-responsible-for-printing) the method travels the tree until matching the right class otherwise use the default printer – cards Jan 17 '23 at 14:48
  • ... how do you know that the printer accepts the parameter `{'order':'none'}`? (for ex why `none` and not `None`)? I tried to look even in the source code but no hints – cards Jan 17 '23 at 14:49
  • 1
    It is the parameter that I used in the answer, so you can know it by demonstration. You can also know by looking at how the setting is used by StrPrinter: it is passed to `_as_ordered_terms` and at this [line](https://github.com/sympy/sympy/blob/c303e9e4d381f575746a8cb7290b32f6ead70a7b/sympy/printing/printer.py#L347) is handled. I agree that it is not a well documented feature. – smichr Jan 18 '23 at 16:35