##// END OF EJS Templates
Document print_png and print_display_png
Takafumi Arakaki -
Show More
@@ -1,145 +1,151
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 # Definitions of special display functions for use with IPython
34 # Definitions of special display functions for use with IPython
35 #-----------------------------------------------------------------------------
35 #-----------------------------------------------------------------------------
36
36
37 def print_basic_unicode(o, p, cycle):
37 def print_basic_unicode(o, p, cycle):
38 """A function to pretty print sympy Basic objects."""
38 """A function to pretty print sympy Basic objects."""
39 if cycle:
39 if cycle:
40 return p.text('Basic(...)')
40 return p.text('Basic(...)')
41 out = pretty(o, use_unicode=True)
41 out = pretty(o, use_unicode=True)
42 if '\n' in out:
42 if '\n' in out:
43 p.text(u'\n')
43 p.text(u'\n')
44 p.text(out)
44 p.text(out)
45
45
46
46
47 def print_png(o):
47 def print_png(o):
48 """A function to display sympy expression using LaTex -> PNG."""
48 """
49 A function to display sympy expression using inline style LaTeX in PNG.
50 """
49 s = latex(o, mode='inline')
51 s = latex(o, mode='inline')
50 # mathtext does not understand certain latex flags, so we try to replace
52 # mathtext does not understand certain latex flags, so we try to replace
51 # them with suitable subs.
53 # them with suitable subs.
52 s = s.replace('\\operatorname','')
54 s = s.replace('\\operatorname','')
53 s = s.replace('\\overline', '\\bar')
55 s = s.replace('\\overline', '\\bar')
54 png = latex_to_png(s)
56 png = latex_to_png(s)
55 return png
57 return png
56
58
57
59
58 def print_display_png(o):
60 def print_display_png(o):
59 """A function to display sympy expression using LaTex -> PNG."""
61 """
62 A function to display sympy expression using display style LaTeX in PNG.
63 """
60 s = latex(o, mode='plain')
64 s = latex(o, mode='plain')
61 s = s.strip('$')
65 s = s.strip('$')
66 # As matplotlib does not support display style, dvipng backend is
67 # used here.
62 png = latex_to_png('$$%s$$' % s, backend='dvipng')
68 png = latex_to_png('$$%s$$' % s, backend='dvipng')
63 return png
69 return png
64
70
65
71
66 def can_print_latex(o):
72 def can_print_latex(o):
67 """
73 """
68 Return True if type o can be printed with LaTeX.
74 Return True if type o can be printed with LaTeX.
69
75
70 If o is a container type, this is True if and only if every element of o
76 If o is a container type, this is True if and only if every element of o
71 can be printed with LaTeX.
77 can be printed with LaTeX.
72 """
78 """
73 import sympy
79 import sympy
74 if isinstance(o, (list, tuple, set, frozenset)):
80 if isinstance(o, (list, tuple, set, frozenset)):
75 return all(can_print_latex(i) for i in o)
81 return all(can_print_latex(i) for i in o)
76 elif isinstance(o, dict):
82 elif isinstance(o, dict):
77 return all((isinstance(i, basestring) or can_print_latex(i)) and can_print_latex(o[i]) for i in o)
83 return all((isinstance(i, basestring) or can_print_latex(i)) and can_print_latex(o[i]) for i in o)
78 elif isinstance(o,(sympy.Basic, sympy.matrices.Matrix, int, long, float)):
84 elif isinstance(o,(sympy.Basic, sympy.matrices.Matrix, int, long, float)):
79 return True
85 return True
80 return False
86 return False
81
87
82 def print_latex(o):
88 def print_latex(o):
83 """A function to generate the latex representation of sympy
89 """A function to generate the latex representation of sympy
84 expressions."""
90 expressions."""
85 if can_print_latex(o):
91 if can_print_latex(o):
86 s = latex(o, mode='plain')
92 s = latex(o, mode='plain')
87 s = s.replace('\\dag','\\dagger')
93 s = s.replace('\\dag','\\dagger')
88 s = s.strip('$')
94 s = s.strip('$')
89 return '$$%s$$' % s
95 return '$$%s$$' % s
90 # Fallback to the string printer
96 # Fallback to the string printer
91 return None
97 return None
92
98
93 _loaded = False
99 _loaded = False
94
100
95 def load_ipython_extension(ip):
101 def load_ipython_extension(ip):
96 """Load the extension in IPython."""
102 """Load the extension in IPython."""
97 import sympy
103 import sympy
98 global _loaded
104 global _loaded
99 if not _loaded:
105 if not _loaded:
100 plaintext_formatter = ip.display_formatter.formatters['text/plain']
106 plaintext_formatter = ip.display_formatter.formatters['text/plain']
101
107
102 for cls in (object, str):
108 for cls in (object, str):
103 plaintext_formatter.for_type(cls, print_basic_unicode)
109 plaintext_formatter.for_type(cls, print_basic_unicode)
104
110
105 printable_containers = [list, tuple]
111 printable_containers = [list, tuple]
106
112
107 # set and frozen set were broken with SymPy's latex() function, but
113 # set and frozen set were broken with SymPy's latex() function, but
108 # was fixed in the 0.7.1-git development version. See
114 # was fixed in the 0.7.1-git development version. See
109 # http://code.google.com/p/sympy/issues/detail?id=3062.
115 # http://code.google.com/p/sympy/issues/detail?id=3062.
110 if sympy.__version__ > '0.7.1':
116 if sympy.__version__ > '0.7.1':
111 printable_containers += [set, frozenset]
117 printable_containers += [set, frozenset]
112 else:
118 else:
113 plaintext_formatter.for_type(cls, print_basic_unicode)
119 plaintext_formatter.for_type(cls, print_basic_unicode)
114
120
115 plaintext_formatter.for_type_by_name(
121 plaintext_formatter.for_type_by_name(
116 'sympy.core.basic', 'Basic', print_basic_unicode
122 'sympy.core.basic', 'Basic', print_basic_unicode
117 )
123 )
118 plaintext_formatter.for_type_by_name(
124 plaintext_formatter.for_type_by_name(
119 'sympy.matrices.matrices', 'Matrix', print_basic_unicode
125 'sympy.matrices.matrices', 'Matrix', print_basic_unicode
120 )
126 )
121
127
122 png_formatter = ip.display_formatter.formatters['image/png']
128 png_formatter = ip.display_formatter.formatters['image/png']
123
129
124 png_formatter.for_type_by_name(
130 png_formatter.for_type_by_name(
125 'sympy.core.basic', 'Basic', print_png
131 'sympy.core.basic', 'Basic', print_png
126 )
132 )
127 png_formatter.for_type_by_name(
133 png_formatter.for_type_by_name(
128 'sympy.matrices.matrices', 'Matrix', print_display_png
134 'sympy.matrices.matrices', 'Matrix', print_display_png
129 )
135 )
130 for cls in [dict, int, long, float] + printable_containers:
136 for cls in [dict, int, long, float] + printable_containers:
131 png_formatter.for_type(cls, print_png)
137 png_formatter.for_type(cls, print_png)
132
138
133 latex_formatter = ip.display_formatter.formatters['text/latex']
139 latex_formatter = ip.display_formatter.formatters['text/latex']
134 latex_formatter.for_type_by_name(
140 latex_formatter.for_type_by_name(
135 'sympy.core.basic', 'Basic', print_latex
141 'sympy.core.basic', 'Basic', print_latex
136 )
142 )
137 latex_formatter.for_type_by_name(
143 latex_formatter.for_type_by_name(
138 'sympy.matrices.matrices', 'Matrix', print_latex
144 'sympy.matrices.matrices', 'Matrix', print_latex
139 )
145 )
140
146
141 for cls in printable_containers:
147 for cls in printable_containers:
142 # Use LaTeX only if every element is printable by latex
148 # Use LaTeX only if every element is printable by latex
143 latex_formatter.for_type(cls, print_latex)
149 latex_formatter.for_type(cls, print_latex)
144
150
145 _loaded = True
151 _loaded = True
General Comments 0
You need to be logged in to leave comments. Login now