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.
Here is the code:
global t
t = symbols('t')
def sortSeries(valIn):
valOut = valIn
for num in (valOut.atoms(sympy.Float)):
valOut = valOut.xreplace({num: 1}) # remove coefficient
if valOut == 1:
return 1 # set t**0 to 1
else:
valOut = valOut.subs(t, 2) # change t**n to 2**n, which is a
sortable number
return valOut
def printSeries(expIn):
expOut = expIn
expOut = collect(expOut, t)
for num in (expOut.atoms(Number)):
expOut = expOut.xreplace({num: num.round(5)})
expList = expOut.as_ordered_terms() # convert expression to list
expList.sort(key=sortSeries) # sort list by values of t**n
for ind in range(0, len(expList)): # convert list to expression
term = expList[ind]
if ind == 0:
exp = term
else:
# exp = exp + term
exp = Add(exp, term, evaluate=False)
#exp = Add(term, exp, evaluate=False)
expOut = exp
print(latex(expOut))
#return expOut
def test():
#exp1 = 6.14979599379575*t**3 - 1.69088473154467*t
#exp2 = 0.331732200302894 - 2.35512815419867*t**2
exp1 = -2867.70035529489*t**5 + 147.938724526848*t**3 -
2.56500070531002*t
exp2 = 701.066644698213*t**4 - 23.5908063706474*t**2 + 0.13771861708229
printSeries(exp1)
printSeries(exp2)
print('end of test')
--
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/f553a5c3-0464-4aa0-a169-56ef0bdac8c1n%40googlegroups.com.