Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #1773 from asmeurer/sympy_set
Browse files Browse the repository at this point in the history
Update SymPy profile: SymPy's latex() can now print set and frozenset
  • Loading branch information
minrk committed Jun 1, 2012
2 parents 04b3974 + 955d880 commit dbad051
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions IPython/extensions/sympyprinting.py
Expand Up @@ -62,7 +62,7 @@ def can_print_latex(o):
can be printed with LaTeX. can be printed with LaTeX.
""" """
import sympy import sympy
if isinstance(o, (list, tuple)): if isinstance(o, (list, tuple, set, frozenset)):
return all(can_print_latex(i) for i in o) return all(can_print_latex(i) for i in o)
elif isinstance(o, dict): elif isinstance(o, dict):
return all((isinstance(i, basestring) or can_print_latex(i)) and can_print_latex(o[i]) for i in o) return all((isinstance(i, basestring) or can_print_latex(i)) and can_print_latex(o[i]) for i in o)
Expand All @@ -85,13 +85,22 @@ def print_latex(o):


def load_ipython_extension(ip): def load_ipython_extension(ip):
"""Load the extension in IPython.""" """Load the extension in IPython."""
import sympy
global _loaded global _loaded
if not _loaded: if not _loaded:
plaintext_formatter = ip.display_formatter.formatters['text/plain'] plaintext_formatter = ip.display_formatter.formatters['text/plain']


for cls in (object, set, frozenset, str): for cls in (object, str):
# set and frozen set are currently broken with SymPy's latex() plaintext_formatter.for_type(cls, print_basic_unicode)
# function. See http://code.google.com/p/sympy/issues/detail?id=3062.
printable_containers = [list, tuple]

# 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':
printable_containers += [set, frozenset]
else:
plaintext_formatter.for_type(cls, print_basic_unicode) plaintext_formatter.for_type(cls, print_basic_unicode)


plaintext_formatter.for_type_by_name( plaintext_formatter.for_type_by_name(
Expand All @@ -106,7 +115,7 @@ def load_ipython_extension(ip):
png_formatter.for_type_by_name( png_formatter.for_type_by_name(
'sympy.core.basic', 'Basic', print_png 'sympy.core.basic', 'Basic', print_png
) )
for cls in (list, tuple, dict, int, long, float): for cls in [dict, int, long, float] + printable_containers:
png_formatter.for_type(cls, print_png) png_formatter.for_type(cls, print_png)


latex_formatter = ip.display_formatter.formatters['text/latex'] latex_formatter = ip.display_formatter.formatters['text/latex']
Expand All @@ -117,7 +126,7 @@ def load_ipython_extension(ip):
'sympy.matrices.matrices', 'Matrix', print_latex 'sympy.matrices.matrices', 'Matrix', print_latex
) )


for cls in (list, tuple): for cls in printable_containers:
# Use LaTeX only if every element is printable by latex # Use LaTeX only if every element is printable by latex
latex_formatter.for_type(cls, print_latex) latex_formatter.for_type(cls, print_latex)


Expand Down

0 comments on commit dbad051

Please sign in to comment.