On Sun, 5 Mar 2023 at 08:32, Thomas Ligon <[email protected]> wrote:
>
> I have a lot of power series that look like this (but going up to t**12):
> exp1 = -2867.70035529489*t**5 + 147.938724526848*t**3 - 2.56500070531002*t
> While trying to gain some insight into the mathematics that creates them, I 
> want to print a shorter version, such as
> - 2.565 t + 147.93872 t**3- 2867.70036 t**5
> but the best I have achieved is
> - 2867.70036 t^{5} + \left(147.93872 t^{3} - 2.565 t\right)
> Rounding the numbers was easy, but I would prefer to round to 5 digits total, 
> not 5 digits after the decimal point. I was able to convert the expression to 
> a list and sort the list, but when I converted the list back to an 
> expression, I didn't succeed in producing the order I wanted.

You can get 5 digits by using exp1.evalf(5).

It is surprisingly difficult to control the order of terms in sympy's
printing functionality but it is possible:

In [1]: exp1 = -2867.70035529489*t**5 + 147.938724526848*t**3 -
2.56500070531002*t

In [2]: doprint = StrPrinter(settings={'order':'none'}).doprint

In [3]: series = Add(*sorted(exp1.evalf(5).args, key=degree), evaluate=False)

In [4]: print(doprint(series))
-2.565*t + 147.94*t**3 - 2867.7*t**5

There are two steps to controlling the order:

- Ordering the terms in the expression itself (the series variable above).
- Getting the printer to respect that ordering ('order':'none').

It should be possible to set order=None with init_printing and that
does work for the pretty printer but not for string printing (i.e.
print(expr) or str(expr)).

--
Oscar

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sympy/CAHVvXxREoxWp1Lsk%3DbPQUJ8KFkio%3DdrHV8ajcCjETjoDDMftoQ%40mail.gmail.com.

Reply via email to