Show More
@@ -1,99 +1,103 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 | """ |
|
12 | """ | |
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 | # Copyright (C) 2008-2011 The IPython Development Team |
|
14 | # Copyright (C) 2008-2011 The IPython Development Team | |
15 | # |
|
15 | # | |
16 | # Distributed under the terms of the BSD License. The full license is in |
|
16 | # Distributed under the terms of the BSD License. The full license is in | |
17 | # the file COPYING, distributed as part of this software. |
|
17 | # the file COPYING, distributed as part of this software. | |
18 | #----------------------------------------------------------------------------- |
|
18 | #----------------------------------------------------------------------------- | |
19 |
|
19 | |||
20 | #----------------------------------------------------------------------------- |
|
20 | #----------------------------------------------------------------------------- | |
21 | # Imports |
|
21 | # Imports | |
22 | #----------------------------------------------------------------------------- |
|
22 | #----------------------------------------------------------------------------- | |
23 |
|
23 | |||
24 | from IPython.lib.latextools import latex_to_png |
|
24 | from IPython.lib.latextools import latex_to_png | |
25 | from IPython.testing import decorators as dec |
|
25 | from IPython.testing import decorators as dec | |
26 | # use @dec.skipif_not_sympy to skip tests requiring sympy |
|
26 | # use @dec.skipif_not_sympy to skip tests requiring sympy | |
27 |
|
27 | |||
28 | try: |
|
28 | try: | |
29 | from sympy import pretty, latex |
|
29 | from sympy import pretty, latex | |
30 | except ImportError: |
|
30 | except ImportError: | |
31 | pass |
|
31 | pass | |
32 |
|
32 | |||
33 |
|
33 | |||
34 | #----------------------------------------------------------------------------- |
|
34 | #----------------------------------------------------------------------------- | |
35 | # Definitions of magic functions for use with IPython |
|
35 | # Definitions of magic functions for use with IPython | |
36 | #----------------------------------------------------------------------------- |
|
36 | #----------------------------------------------------------------------------- | |
37 |
|
37 | |||
38 | def print_basic_unicode(o, p, cycle): |
|
38 | def print_basic_unicode(o, p, cycle): | |
39 | """A function to pretty print sympy Basic objects.""" |
|
39 | """A function to pretty print sympy Basic objects.""" | |
40 | if cycle: |
|
40 | if cycle: | |
41 | return p.text('Basic(...)') |
|
41 | return p.text('Basic(...)') | |
42 | out = pretty(o, use_unicode=True) |
|
42 | out = pretty(o, use_unicode=True) | |
43 | if '\n' in out: |
|
43 | if '\n' in out: | |
44 | p.text(u'\n') |
|
44 | p.text(u'\n') | |
45 | p.text(out) |
|
45 | p.text(out) | |
46 |
|
46 | |||
47 |
|
47 | |||
48 | def print_png(o): |
|
48 | def print_png(o): | |
49 | """A function to display sympy expression using LaTex -> PNG.""" |
|
49 | """A function to display sympy expression using LaTex -> PNG.""" | |
50 | s = latex(o, mode='inline') |
|
50 | s = latex(o, mode='inline') | |
51 | # mathtext does not understand certain latex flags, so we try to replace |
|
51 | # mathtext does not understand certain latex flags, so we try to replace | |
52 | # them with suitable subs. |
|
52 | # them with suitable subs. | |
53 | s = s.replace('\\operatorname','') |
|
53 | s = s.replace('\\operatorname','') | |
54 | s = s.replace('\\overline', '\\bar') |
|
54 | s = s.replace('\\overline', '\\bar') | |
55 | png = latex_to_png(s) |
|
55 | png = latex_to_png(s) | |
56 | return png |
|
56 | return png | |
57 |
|
57 | |||
58 |
|
58 | |||
59 | def print_latex(o): |
|
59 | def print_latex(o): | |
60 | """A function to generate the latex representation of sympy expressions.""" |
|
60 | """A function to generate the latex representation of sympy expressions.""" | |
61 | s = latex(o, mode='plain') |
|
61 | s = latex(o, mode='plain') | |
62 | s = s.replace('\\dag','\\dagger') |
|
62 | s = s.replace('\\dag','\\dagger') | |
63 | s = s.strip('$') |
|
63 | s = s.strip('$') | |
64 | return '$$%s$$' % s |
|
64 | return '$$%s$$' % s | |
65 |
|
65 | |||
66 |
|
66 | |||
67 | _loaded = False |
|
67 | _loaded = False | |
68 |
|
68 | |||
69 | def load_ipython_extension(ip): |
|
69 | def load_ipython_extension(ip): | |
70 | """Load the extension in IPython.""" |
|
70 | """Load the extension in IPython.""" | |
71 | global _loaded |
|
71 | global _loaded | |
72 | if not _loaded: |
|
72 | if not _loaded: | |
73 | plaintext_formatter = ip.display_formatter.formatters['text/plain'] |
|
73 | plaintext_formatter = ip.display_formatter.formatters['text/plain'] | |
74 |
|
74 | |||
75 |
for cls in (object, |
|
75 | for cls in (object, set, frozenset, str): | |
76 | plaintext_formatter.for_type(cls, print_basic_unicode) |
|
76 | plaintext_formatter.for_type(cls, print_basic_unicode) | |
77 |
|
77 | |||
78 | plaintext_formatter.for_type_by_name( |
|
78 | plaintext_formatter.for_type_by_name( | |
79 | 'sympy.core.basic', 'Basic', print_basic_unicode |
|
79 | 'sympy.core.basic', 'Basic', print_basic_unicode | |
80 | ) |
|
80 | ) | |
81 | plaintext_formatter.for_type_by_name( |
|
81 | plaintext_formatter.for_type_by_name( | |
82 | 'sympy.matrices.matrices', 'Matrix', print_basic_unicode |
|
82 | 'sympy.matrices.matrices', 'Matrix', print_basic_unicode | |
83 | ) |
|
83 | ) | |
84 |
|
84 | |||
85 | png_formatter = ip.display_formatter.formatters['image/png'] |
|
85 | png_formatter = ip.display_formatter.formatters['image/png'] | |
86 |
|
86 | |||
87 | png_formatter.for_type_by_name( |
|
87 | png_formatter.for_type_by_name( | |
88 | 'sympy.core.basic', 'Basic', print_png |
|
88 | 'sympy.core.basic', 'Basic', print_png | |
89 | ) |
|
89 | ) | |
|
90 | for cls in (list, tuple, dict, int, long, float): | |||
|
91 | png_formatter.for_type(cls, print_png) | |||
90 |
|
92 | |||
91 | latex_formatter = ip.display_formatter.formatters['text/latex'] |
|
93 | latex_formatter = ip.display_formatter.formatters['text/latex'] | |
92 | latex_formatter.for_type_by_name( |
|
94 | latex_formatter.for_type_by_name( | |
93 | 'sympy.core.basic', 'Basic', print_latex |
|
95 | 'sympy.core.basic', 'Basic', print_latex | |
94 | ) |
|
96 | ) | |
95 | latex_formatter.for_type_by_name( |
|
97 | latex_formatter.for_type_by_name( | |
96 | 'sympy.matrices.matrices', 'Matrix', print_latex |
|
98 | 'sympy.matrices.matrices', 'Matrix', print_latex | |
97 | ) |
|
99 | ) | |
98 | _loaded = True |
|
100 | for cls in (list, tuple, dict, int, long, float): | |
|
101 | latex_formatter.for_type(cls, print_latex) | |||
99 |
|
102 | |||
|
103 | _loaded = True |
General Comments 0
You need to be logged in to leave comments.
Login now