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