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