sympyprinting.py
124 lines
| 4.0 KiB
| text/x-python
|
PythonLexer
Pauli Virtanen
|
r4888 | """ | ||
A print function that pretty prints sympy Basic objects. | ||||
:moduleauthor: Brian Granger | ||||
Usage | ||||
===== | ||||
Once the extension is loaded, Sympy Basic objects are automatically | ||||
pretty-printed. | ||||
Brian Granger
|
r3278 | |||
""" | ||||
#----------------------------------------------------------------------------- | ||||
# Copyright (C) 2008-2011 The IPython Development Team | ||||
# | ||||
# Distributed under the terms of the BSD License. The full license is in | ||||
# the file COPYING, distributed as part of this software. | ||||
#----------------------------------------------------------------------------- | ||||
#----------------------------------------------------------------------------- | ||||
# Imports | ||||
#----------------------------------------------------------------------------- | ||||
Brian Granger
|
r3279 | from IPython.lib.latextools import latex_to_png | ||
Paul Ivanov
|
r3504 | from IPython.testing import decorators as dec | ||
Paul Ivanov
|
r3507 | # use @dec.skipif_not_sympy to skip tests requiring sympy | ||
Paul Ivanov
|
r3504 | |||
try: | ||||
from sympy import pretty, latex | ||||
except ImportError: | ||||
pass | ||||
Brian Granger
|
r3279 | |||
Brian Granger
|
r3278 | #----------------------------------------------------------------------------- | ||
Fernando Perez
|
r6934 | # Definitions of special display functions for use with IPython | ||
Brian Granger
|
r3278 | #----------------------------------------------------------------------------- | ||
def print_basic_unicode(o, p, cycle): | ||||
"""A function to pretty print sympy Basic objects.""" | ||||
if cycle: | ||||
return p.text('Basic(...)') | ||||
out = pretty(o, use_unicode=True) | ||||
if '\n' in out: | ||||
p.text(u'\n') | ||||
p.text(out) | ||||
Brian Granger
|
r3279 | def print_png(o): | ||
Brian Granger
|
r3878 | """A function to display sympy expression using LaTex -> PNG.""" | ||
Brian Granger
|
r3283 | s = latex(o, mode='inline') | ||
Brian Granger
|
r3285 | # mathtext does not understand certain latex flags, so we try to replace | ||
# them with suitable subs. | ||||
Brian Granger
|
r3284 | s = s.replace('\\operatorname','') | ||
Brian Granger
|
r3285 | s = s.replace('\\overline', '\\bar') | ||
Brian Granger
|
r3880 | png = latex_to_png(s) | ||
Brian Granger
|
r3279 | return png | ||
Aaron Meurer
|
r6297 | def can_print_latex(o): | ||
""" | ||||
Return True if type o can be printed with LaTeX. | ||||
If o is a container type, this is True if and only if every element of o | ||||
can be printed with LaTeX. | ||||
""" | ||||
import sympy | ||||
if isinstance(o, (list, tuple)): | ||||
return all(can_print_latex(i) for i in o) | ||||
elif isinstance(o, dict): | ||||
return all((isinstance(i, basestring) or can_print_latex(i)) and can_print_latex(o[i]) for i in o) | ||||
elif isinstance(o,(sympy.Basic, sympy.matrices.Matrix, int, long, float)): | ||||
return True | ||||
return False | ||||
Brian Granger
|
r4316 | |||
def print_latex(o): | ||||
Aaron Meurer
|
r6297 | """A function to generate the latex representation of sympy | ||
expressions.""" | ||||
if can_print_latex(o): | ||||
s = latex(o, mode='plain') | ||||
s = s.replace('\\dag','\\dagger') | ||||
s = s.strip('$') | ||||
return '$$%s$$' % s | ||||
# Fallback to the string printer | ||||
return None | ||||
Brian Granger
|
r4316 | |||
Brian Granger
|
r3278 | _loaded = False | ||
def load_ipython_extension(ip): | ||||
"""Load the extension in IPython.""" | ||||
global _loaded | ||||
if not _loaded: | ||||
plaintext_formatter = ip.display_formatter.formatters['text/plain'] | ||||
Brian E. Granger
|
r4270 | |||
Aaron Meurer
|
r6110 | for cls in (object, set, frozenset, str): | ||
Aaron Meurer
|
r6297 | # set and frozen set are currently broken with SymPy's latex() | ||
# function. See http://code.google.com/p/sympy/issues/detail?id=3062. | ||||
Brian E. Granger
|
r4270 | plaintext_formatter.for_type(cls, print_basic_unicode) | ||
Brian Granger
|
r3278 | plaintext_formatter.for_type_by_name( | ||
'sympy.core.basic', 'Basic', print_basic_unicode | ||||
) | ||||
Brian Granger
|
r3285 | plaintext_formatter.for_type_by_name( | ||
'sympy.matrices.matrices', 'Matrix', print_basic_unicode | ||||
) | ||||
Brian Granger
|
r3279 | png_formatter = ip.display_formatter.formatters['image/png'] | ||
Brian E. Granger
|
r4270 | |||
Brian Granger
|
r3279 | png_formatter.for_type_by_name( | ||
'sympy.core.basic', 'Basic', print_png | ||||
) | ||||
Aaron Meurer
|
r6110 | for cls in (list, tuple, dict, int, long, float): | ||
png_formatter.for_type(cls, print_png) | ||||
Brian Granger
|
r4316 | |||
latex_formatter = ip.display_formatter.formatters['text/latex'] | ||||
latex_formatter.for_type_by_name( | ||||
'sympy.core.basic', 'Basic', print_latex | ||||
) | ||||
latex_formatter.for_type_by_name( | ||||
'sympy.matrices.matrices', 'Matrix', print_latex | ||||
) | ||||
Aaron Meurer
|
r6297 | |||
for cls in (list, tuple): | ||||
# Use LaTeX only if every element is printable by latex | ||||
Aaron Meurer
|
r6110 | latex_formatter.for_type(cls, print_latex) | ||
Brian Granger
|
r3278 | |||
Aaron Meurer
|
r6110 | _loaded = True | ||