##// END OF EJS Templates
Merge pull request #1399 from asmeurer/sympyprinting...
Fernando Perez -
r6482:ba882bf7 merge
parent child Browse files
Show More
@@ -1,99 +1,124 b''
1 1 """
2 2 A print function that pretty prints sympy Basic objects.
3 3
4 4 :moduleauthor: Brian Granger
5 5
6 6 Usage
7 7 =====
8 8
9 9 Once the extension is loaded, Sympy Basic objects are automatically
10 10 pretty-printed.
11 11
12 12 """
13 13 #-----------------------------------------------------------------------------
14 14 # Copyright (C) 2008-2011 The IPython Development Team
15 15 #
16 16 # Distributed under the terms of the BSD License. The full license is in
17 17 # the file COPYING, distributed as part of this software.
18 18 #-----------------------------------------------------------------------------
19 19
20 20 #-----------------------------------------------------------------------------
21 21 # Imports
22 22 #-----------------------------------------------------------------------------
23 23
24 24 from IPython.lib.latextools import latex_to_png
25 25 from IPython.testing import decorators as dec
26 26 # use @dec.skipif_not_sympy to skip tests requiring sympy
27 27
28 28 try:
29 29 from sympy import pretty, latex
30 30 except ImportError:
31 31 pass
32 32
33
34 33 #-----------------------------------------------------------------------------
35 34 # Definitions of magic functions for use with IPython
36 35 #-----------------------------------------------------------------------------
37 36
38 37 def print_basic_unicode(o, p, cycle):
39 38 """A function to pretty print sympy Basic objects."""
40 39 if cycle:
41 40 return p.text('Basic(...)')
42 41 out = pretty(o, use_unicode=True)
43 42 if '\n' in out:
44 43 p.text(u'\n')
45 44 p.text(out)
46 45
47 46
48 47 def print_png(o):
49 48 """A function to display sympy expression using LaTex -> PNG."""
50 49 s = latex(o, mode='inline')
51 50 # mathtext does not understand certain latex flags, so we try to replace
52 51 # them with suitable subs.
53 52 s = s.replace('\\operatorname','')
54 53 s = s.replace('\\overline', '\\bar')
55 54 png = latex_to_png(s)
56 55 return png
57 56
57 def can_print_latex(o):
58 """
59 Return True if type o can be printed with LaTeX.
60
61 If o is a container type, this is True if and only if every element of o
62 can be printed with LaTeX.
63 """
64 import sympy
65 if isinstance(o, (list, tuple)):
66 return all(can_print_latex(i) for i in o)
67 elif isinstance(o, dict):
68 return all((isinstance(i, basestring) or can_print_latex(i)) and can_print_latex(o[i]) for i in o)
69 elif isinstance(o,(sympy.Basic, sympy.matrices.Matrix, int, long, float)):
70 return True
71 return False
58 72
59 73 def print_latex(o):
60 """A function to generate the latex representation of sympy expressions."""
61 s = latex(o, mode='plain')
62 s = s.replace('\\dag','\\dagger')
63 s = s.strip('$')
64 return '$$%s$$' % s
65
74 """A function to generate the latex representation of sympy
75 expressions."""
76 if can_print_latex(o):
77 s = latex(o, mode='plain')
78 s = s.replace('\\dag','\\dagger')
79 s = s.strip('$')
80 return '$$%s$$' % s
81 # Fallback to the string printer
82 return None
66 83
67 84 _loaded = False
68 85
69 86 def load_ipython_extension(ip):
70 87 """Load the extension in IPython."""
71 88 global _loaded
72 89 if not _loaded:
73 90 plaintext_formatter = ip.display_formatter.formatters['text/plain']
74 91
75 for cls in (object, tuple, list, set, frozenset, dict, str):
92 for cls in (object, set, frozenset, str):
93 # set and frozen set are currently broken with SymPy's latex()
94 # function. See http://code.google.com/p/sympy/issues/detail?id=3062.
76 95 plaintext_formatter.for_type(cls, print_basic_unicode)
77 96
78 97 plaintext_formatter.for_type_by_name(
79 98 'sympy.core.basic', 'Basic', print_basic_unicode
80 99 )
81 100 plaintext_formatter.for_type_by_name(
82 101 'sympy.matrices.matrices', 'Matrix', print_basic_unicode
83 102 )
84 103
85 104 png_formatter = ip.display_formatter.formatters['image/png']
86 105
87 106 png_formatter.for_type_by_name(
88 107 'sympy.core.basic', 'Basic', print_png
89 108 )
109 for cls in (list, tuple, dict, int, long, float):
110 png_formatter.for_type(cls, print_png)
90 111
91 112 latex_formatter = ip.display_formatter.formatters['text/latex']
92 113 latex_formatter.for_type_by_name(
93 114 'sympy.core.basic', 'Basic', print_latex
94 115 )
95 116 latex_formatter.for_type_by_name(
96 117 'sympy.matrices.matrices', 'Matrix', print_latex
97 118 )
98 _loaded = True
99 119
120 for cls in (list, tuple):
121 # Use LaTeX only if every element is printable by latex
122 latex_formatter.for_type(cls, print_latex)
123
124 _loaded = True
General Comments 0
You need to be logged in to leave comments. Login now