##// END OF EJS Templates
Remove utils.autoattr
Remove utils.autoattr

File last commit:

r8574:0986e716
r9472:e2b7db05
Show More
sympyprinting.py
171 lines | 5.5 KiB | text/x-python | PythonLexer
Pauli Virtanen
DOC: extensions: add documentation for the bundled extensions
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
Display system is fully working now....
r3278
Sean Vig
Add deprecation warnings for sympyprinting...
r8574 As of SymPy 0.7.2, maintenance of this extension has moved to SymPy under
sympy.interactive.ipythonprinting, any modifications to account for changes to
SymPy should be submitted to SymPy rather than changed here. This module is
maintained here for backwards compatablitiy with old SymPy versions.
Brian Granger
Display system is fully working now....
r3278 """
#-----------------------------------------------------------------------------
Takafumi Arakaki
Copyright fix on sympyprinting.py and latextools.py...
r7311 # Copyright (C) 2008 The IPython Development Team
Brian Granger
Display system is fully working now....
r3278 #
# 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
More improvements to the display system....
r3279 from IPython.lib.latextools import latex_to_png
Paul Ivanov
skip sympy tests if sympy not installed
r3504 from IPython.testing import decorators as dec
Paul Ivanov
removed skipif decorators from non-tests
r3507 # use @dec.skipif_not_sympy to skip tests requiring sympy
Paul Ivanov
skip sympy tests if sympy not installed
r3504
try:
from sympy import pretty, latex
except ImportError:
pass
Brian Granger
More improvements to the display system....
r3279
Sean Vig
Add deprecation warnings for sympyprinting...
r8574 import warnings
Brian Granger
Display system is fully working now....
r3278 #-----------------------------------------------------------------------------
Fernando Perez
Fix simple comment.
r6934 # Definitions of special display functions for use with IPython
Brian Granger
Display system is fully working now....
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
More improvements to the display system....
r3279 def print_png(o):
Takafumi Arakaki
Document print_png and print_display_png
r7310 """
A function to display sympy expression using inline style LaTeX in PNG.
"""
Brian Granger
Using inline mode for rendering sympy Expr to latex.
r3283 s = latex(o, mode='inline')
Brian Granger
Fixing Matrix/overbar printing for sympy.
r3285 # mathtext does not understand certain latex flags, so we try to replace
# them with suitable subs.
Brian Granger
Small fixes for latex/png printing....
r3284 s = s.replace('\\operatorname','')
Brian Granger
Fixing Matrix/overbar printing for sympy.
r3285 s = s.replace('\\overline', '\\bar')
Brian Granger
Misc updates the display system....
r3880 png = latex_to_png(s)
Brian Granger
More improvements to the display system....
r3279 return png
Takafumi Arakaki
Use dvipng to format sympy.Matrix to PNG display
r7307
def print_display_png(o):
Takafumi Arakaki
Document print_png and print_display_png
r7310 """
A function to display sympy expression using display style LaTeX in PNG.
"""
Takafumi Arakaki
Use dvipng to format sympy.Matrix to PNG display
r7307 s = latex(o, mode='plain')
s = s.strip('$')
Takafumi Arakaki
Document print_png and print_display_png
r7310 # As matplotlib does not support display style, dvipng backend is
# used here.
Takafumi Arakaki
Use breqn.sty in dvipng backend if possible
r7852 png = latex_to_png(s, backend='dvipng', wrap=True)
Takafumi Arakaki
Use dvipng to format sympy.Matrix to PNG display
r7307 return png
Aaron Meurer
Only print container types with LaTeX in the notebook if all the elements can...
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
Aaron Meurer
Update SymPy profile: SymPy's latex() can now print set and frozenset...
r7039 if isinstance(o, (list, tuple, set, frozenset)):
Aaron Meurer
Only print container types with LaTeX in the notebook if all the elements can...
r6297 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
Initial latex printing for sympy and fixes to autogrow.
r4316
def print_latex(o):
Aaron Meurer
Only print container types with LaTeX in the notebook if all the elements can...
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
Initial latex printing for sympy and fixes to autogrow.
r4316
Brian Granger
Display system is fully working now....
r3278 _loaded = False
def load_ipython_extension(ip):
"""Load the extension in IPython."""
Aaron Meurer
Update SymPy profile: SymPy's latex() can now print set and frozenset...
r7039 import sympy
Sean Vig
Add deprecation warnings for sympyprinting...
r8574
# sympyprinting extension has been moved to SymPy as of 0.7.2, if it
# exists there, warn the user and import it
try:
import sympy.interactive.ipythonprinting
except ImportError:
pass
else:
warnings.warn("The sympyprinting extension in IPython is deprecated, "
"use sympy.interactive.ipythonprinting")
ip.extension_manager.load_extension('sympy.interactive.ipythonprinting')
return
Brian Granger
Display system is fully working now....
r3278 global _loaded
if not _loaded:
plaintext_formatter = ip.display_formatter.formatters['text/plain']
Brian E. Granger
Adding basic types (list, dict ,etc) to the sympy profile....
r4270
Aaron Meurer
Update SymPy profile: SymPy's latex() can now print set and frozenset...
r7039 for cls in (object, str):
plaintext_formatter.for_type(cls, print_basic_unicode)
printable_containers = [list, tuple]
Aaron Meurer
Test against SymPy version 0.7.1, not 0.7.1-git
r7092 # set and frozen set were broken with SymPy's latex() function, but
# was fixed in the 0.7.1-git development version. See
# http://code.google.com/p/sympy/issues/detail?id=3062.
if sympy.__version__ > '0.7.1':
Aaron Meurer
Update SymPy profile: SymPy's latex() can now print set and frozenset...
r7039 printable_containers += [set, frozenset]
Aaron Meurer
Test against SymPy version 0.7.1, not 0.7.1-git
r7092 else:
plaintext_formatter.for_type(cls, print_basic_unicode)
Brian E. Granger
Adding basic types (list, dict ,etc) to the sympy profile....
r4270
Brian Granger
Display system is fully working now....
r3278 plaintext_formatter.for_type_by_name(
'sympy.core.basic', 'Basic', print_basic_unicode
)
Brian Granger
Fixing Matrix/overbar printing for sympy.
r3285 plaintext_formatter.for_type_by_name(
'sympy.matrices.matrices', 'Matrix', print_basic_unicode
)
Brian Granger
More improvements to the display system....
r3279 png_formatter = ip.display_formatter.formatters['image/png']
Brian E. Granger
Adding basic types (list, dict ,etc) to the sympy profile....
r4270
Brian Granger
More improvements to the display system....
r3279 png_formatter.for_type_by_name(
'sympy.core.basic', 'Basic', print_png
)
Takafumi Arakaki
Use dvipng to format sympy.Matrix to PNG display
r7307 png_formatter.for_type_by_name(
'sympy.matrices.matrices', 'Matrix', print_display_png
)
Aaron Meurer
Update SymPy profile: SymPy's latex() can now print set and frozenset...
r7039 for cls in [dict, int, long, float] + printable_containers:
Aaron Meurer
Use LaTeX to print various built-in types with the SymPy printing extension...
r6110 png_formatter.for_type(cls, print_png)
Brian Granger
Initial latex printing for sympy and fixes to autogrow.
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
Only print container types with LaTeX in the notebook if all the elements can...
r6297
Aaron Meurer
Update SymPy profile: SymPy's latex() can now print set and frozenset...
r7039 for cls in printable_containers:
Aaron Meurer
Only print container types with LaTeX in the notebook if all the elements can...
r6297 # Use LaTeX only if every element is printable by latex
Aaron Meurer
Use LaTeX to print various built-in types with the SymPy printing extension...
r6110 latex_formatter.for_type(cls, print_latex)
Brian Granger
Display system is fully working now....
r3278
Aaron Meurer
Use LaTeX to print various built-in types with the SymPy printing extension...
r6110 _loaded = True