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