Sympy integrate() does not produce output in the natural form of the common fraction

981 views Asked by At

This code is taken from the Sympy tutorial:

init_printing(use_unicode=False, wrap_line=False, no_global=True)
x = Symbol('x')
r = integrate(x**2 + x + 1, x)

print(r)

The output is: x**3/3 + x**2/2 + x

It's correct, but in tutorial the output was:

 3    2
x    x
-- + -- + x
3    2 

How can I reach this form of output?

If necessary: IDE is pyCharm, python ver. is 3.3

1

There are 1 answers

4
Abhijit On BEST ANSWER

You need a pretty printer pprint instead of normal print. Refer the Sympy Tutorial section Printing

>>> from sympy import *
>>> init_printing(use_unicode=False, wrap_line=False, no_global=True)
>>> x = Symbol('x')
>>> r = integrate(x**2 + x + 1, x)
>>> print(r)
x**3/3 + x**2/2 + x
>>> pprint(r)
 3    2    
x    x     
-- + -- + x
3    2

Note For non string objects, print statement (Python 2.X) or the print function (Python 3.X) converts the object to string using the rules for string conversion.