2

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

Netherwire
  • 2,669
  • 3
  • 31
  • 54

1 Answers1

1

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.

Abhijit
  • 62,056
  • 18
  • 131
  • 204
  • Thanks a lot, it worked just fine. What is the reason you're writing ">>>" at the beginning of the each line of your code? – Netherwire Dec 03 '13 at 17:48
  • That's what the code looks like in an interactive Python session (like if you type `python` at the command line). – asmeurer Dec 03 '13 at 22:50
  • The various options to `init_printing` that you've used here are generally not necessary. Unless you find that the default output is not optimal, the defaults are generally the best way to go. – asmeurer Dec 03 '13 at 22:51
  • Ah, I see. You're just copying what was used in the tutorial. Those options are just there because the Unicode tends to not look so good in a web-browser (and the other options are for the automatic doctesting of examples). – asmeurer Dec 03 '13 at 22:53