When I pointed out " This gives an error. You have to split the ordered
terms into coefficients and factors first with `as_coeff_Mul()`" it
identified why the previous answer was incorrect and gave this modified
line:
expList = [(round(coeff, 5), t.as_poly()) for coeff, t in
(term.as_coeff_Mul() for term in expList)]
On Monday, March 6, 2023 at 9:08:45 PM UTC-6 Chris Smith wrote:
> I thought I would try this in ChatGPT since one of the answers sounded
> gpt-ish. Quite surprised by the results in terms of level of detail about
> what is going on in the routine that it presented. I marked the line that
> was needed to make the function of the gpt-answer work:
>
> def printSeries(expIn):
> expOut = expIn
> expOut = collect(expOut, t)
> expList = expOut.as_ordered_terms() # convert expression to list
> expList = [i.as_coeff_Mul() for i in expList] # <---- needed this
> line
> expList = [(t, round(coeff, 5)) for coeff, t in expList] # round
> coefficients to 5 digits
> expList.sort(key=lambda term: term[0].as_poly().degree(),
> reverse=True) # sort terms by degree of t
> expStr = ' '.join([f"{coeff:.5g} {t}" for t, coeff in expList]) #
> format terms as strings
> print(expStr)
>
> /c
> On Monday, March 6, 2023 at 10:45:51 AM UTC-6 Oscar wrote:
>
>> The degree function can be imported like
>>
>> from sympy import degree
>>
>> It is used to get the degree of a polynomial expression.
>>
>> On Mon, 6 Mar 2023 at 14:53, Thomas Ligon <[email protected]> wrote:
>> >
>> > Thanks! That's a big help, but I am not finished yet. Here is where I
>> am now:
>> >
>> > Step 1: Use your code.
>> > name 'StrPrinter' is not defined
>> > changed StrPrinter to sympy.printing.str.StrPrinter
>> > https://docs.sympy.org/latest/modules/printing.html
>> > name 'degree' is not defined
>> > I tried key=sympy.printing.str.degree, but that gave me
>> > module 'sympy.printing.str' has no attribute 'degree'
>> > Then I tried key=sympy.degree and that worked.
>> > Now the two series are being printed in the desired order.
>> >
>> > Step 2: Use latex
>> > I tried changing print(doprint(series)) to
>> print(latex(doprint(series)))
>> > and the result is
>> > \mathtt{\text{-2.565*t + 147.94*t**3 - 2867.7*t**5}}
>> > which doesn't format correctly in Word's equation editor or in
>> www.overleaf.com,
>> > where it formats correctly, but issues an error
>> > Undefined control sequence.
>> > LaTeX Error: \mathtt allowed only in math mode.
>> > I tried changing the string to
>> > -2.565*t + 147.94*t**3 - 2867.7*t**5
>> > and this formats correctly in Word and in Overleaf.
>> > Then I tried
>> > doprint =
>> sympy.printing.latex.LatexPrinter(settings={'order':'none'}).doprint
>> > but that throws an exception
>> > '_PrintFunction' object has no attribute 'LatexPrinter'
>> > even though the class
>> > class sympy.printing.latex.LatexPrinter(settings=None)
>> > is defined in
>> >
>> https://docs.sympy.org/latest/modules/printing.html#module-sympy.printing.latex
>>
>> >
>> > Still to do:
>> > Learn how this works.
>> > Learn why evalf(5) works. The documentation at
>> > https://docs.sympy.org/latest/modules/evalf.html
>> > doesn't make it clear to me what the possible arguments are and what
>> they mean.
>> > Find documentation for sorted and key=degree.
>> > I searched for sympy degree and found polynomials and angles, but still
>> don't understand this one.
>> >
>> >
>> > On Sunday, March 5, 2023 at 8:53:32 PM UTC+1 Oscar wrote:
>> >
>> > 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/d48be44a-e979-41fd-aa37-1fabadde9b7an%40googlegroups.com.
>>
>>
>>
>
--
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/48fc1fb2-de3a-44b3-aeb1-c756e9df14c8n%40googlegroups.com.