##// END OF EJS Templates
SymPy has changed its recommended way to initialize printing
Aaron Meurer -
Show More
@@ -1,171 +1,171 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 As of SymPy 0.7.2, maintenance of this extension has moved to SymPy under
13 13 sympy.interactive.ipythonprinting, any modifications to account for changes to
14 14 SymPy should be submitted to SymPy rather than changed here. This module is
15 15 maintained here for backwards compatablitiy with old SymPy versions.
16 16
17 17 """
18 18 #-----------------------------------------------------------------------------
19 19 # Copyright (C) 2008 The IPython Development Team
20 20 #
21 21 # Distributed under the terms of the BSD License. The full license is in
22 22 # the file COPYING, distributed as part of this software.
23 23 #-----------------------------------------------------------------------------
24 24
25 25 #-----------------------------------------------------------------------------
26 26 # Imports
27 27 #-----------------------------------------------------------------------------
28 28
29 29 from IPython.lib.latextools import latex_to_png
30 30 from IPython.testing import decorators as dec
31 31 # use @dec.skipif_not_sympy to skip tests requiring sympy
32 32
33 33 try:
34 34 from sympy import pretty, latex
35 35 except ImportError:
36 36 pass
37 37
38 38 import warnings
39 39
40 40 #-----------------------------------------------------------------------------
41 41 # Definitions of special display functions for use with IPython
42 42 #-----------------------------------------------------------------------------
43 43
44 44 def print_basic_unicode(o, p, cycle):
45 45 """A function to pretty print sympy Basic objects."""
46 46 if cycle:
47 47 return p.text('Basic(...)')
48 48 out = pretty(o, use_unicode=True)
49 49 if '\n' in out:
50 50 p.text(u'\n')
51 51 p.text(out)
52 52
53 53
54 54 def print_png(o):
55 55 """
56 56 A function to display sympy expression using inline style LaTeX in PNG.
57 57 """
58 58 s = latex(o, mode='inline')
59 59 # mathtext does not understand certain latex flags, so we try to replace
60 60 # them with suitable subs.
61 61 s = s.replace('\\operatorname','')
62 62 s = s.replace('\\overline', '\\bar')
63 63 png = latex_to_png(s)
64 64 return png
65 65
66 66
67 67 def print_display_png(o):
68 68 """
69 69 A function to display sympy expression using display style LaTeX in PNG.
70 70 """
71 71 s = latex(o, mode='plain')
72 72 s = s.strip('$')
73 73 # As matplotlib does not support display style, dvipng backend is
74 74 # used here.
75 75 png = latex_to_png(s, backend='dvipng', wrap=True)
76 76 return png
77 77
78 78
79 79 def can_print_latex(o):
80 80 """
81 81 Return True if type o can be printed with LaTeX.
82 82
83 83 If o is a container type, this is True if and only if every element of o
84 84 can be printed with LaTeX.
85 85 """
86 86 import sympy
87 87 if isinstance(o, (list, tuple, set, frozenset)):
88 88 return all(can_print_latex(i) for i in o)
89 89 elif isinstance(o, dict):
90 90 return all((isinstance(i, basestring) or can_print_latex(i)) and can_print_latex(o[i]) for i in o)
91 91 elif isinstance(o,(sympy.Basic, sympy.matrices.Matrix, int, long, float)):
92 92 return True
93 93 return False
94 94
95 95 def print_latex(o):
96 96 """A function to generate the latex representation of sympy
97 97 expressions."""
98 98 if can_print_latex(o):
99 99 s = latex(o, mode='plain')
100 100 s = s.replace('\\dag','\\dagger')
101 101 s = s.strip('$')
102 102 return '$$%s$$' % s
103 103 # Fallback to the string printer
104 104 return None
105 105
106 106 _loaded = False
107 107
108 108 def load_ipython_extension(ip):
109 109 """Load the extension in IPython."""
110 110 import sympy
111 111
112 112 # sympyprinting extension has been moved to SymPy as of 0.7.2, if it
113 113 # exists there, warn the user and import it
114 114 try:
115 115 import sympy.interactive.ipythonprinting
116 116 except ImportError:
117 117 pass
118 118 else:
119 119 warnings.warn("The sympyprinting extension in IPython is deprecated, "
120 "use sympy.interactive.ipythonprinting")
120 "use 'from sympy import init_printing; init_printing()'")
121 121 ip.extension_manager.load_extension('sympy.interactive.ipythonprinting')
122 122 return
123 123
124 124 global _loaded
125 125 if not _loaded:
126 126 plaintext_formatter = ip.display_formatter.formatters['text/plain']
127 127
128 128 for cls in (object, str):
129 129 plaintext_formatter.for_type(cls, print_basic_unicode)
130 130
131 131 printable_containers = [list, tuple]
132 132
133 133 # set and frozen set were broken with SymPy's latex() function, but
134 134 # was fixed in the 0.7.1-git development version. See
135 135 # http://code.google.com/p/sympy/issues/detail?id=3062.
136 136 if sympy.__version__ > '0.7.1':
137 137 printable_containers += [set, frozenset]
138 138 else:
139 139 plaintext_formatter.for_type(cls, print_basic_unicode)
140 140
141 141 plaintext_formatter.for_type_by_name(
142 142 'sympy.core.basic', 'Basic', print_basic_unicode
143 143 )
144 144 plaintext_formatter.for_type_by_name(
145 145 'sympy.matrices.matrices', 'Matrix', print_basic_unicode
146 146 )
147 147
148 148 png_formatter = ip.display_formatter.formatters['image/png']
149 149
150 150 png_formatter.for_type_by_name(
151 151 'sympy.core.basic', 'Basic', print_png
152 152 )
153 153 png_formatter.for_type_by_name(
154 154 'sympy.matrices.matrices', 'Matrix', print_display_png
155 155 )
156 156 for cls in [dict, int, long, float] + printable_containers:
157 157 png_formatter.for_type(cls, print_png)
158 158
159 159 latex_formatter = ip.display_formatter.formatters['text/latex']
160 160 latex_formatter.for_type_by_name(
161 161 'sympy.core.basic', 'Basic', print_latex
162 162 )
163 163 latex_formatter.for_type_by_name(
164 164 'sympy.matrices.matrices', 'Matrix', print_latex
165 165 )
166 166
167 167 for cls in printable_containers:
168 168 # Use LaTeX only if every element is printable by latex
169 169 latex_formatter.for_type(cls, print_latex)
170 170
171 171 _loaded = True
General Comments 0
You need to be logged in to leave comments. Login now