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