##// END OF EJS Templates
Fixing Matrix/overbar printing for sympy.
Brian Granger -
Show More
@@ -1,60 +1,65 b''
1 """A print function that pretty prints sympy Basic objects.
1 """A print function that pretty prints sympy Basic objects.
2
2
3 Authors:
3 Authors:
4 * Brian Granger
4 * Brian Granger
5 """
5 """
6 #-----------------------------------------------------------------------------
6 #-----------------------------------------------------------------------------
7 # Copyright (C) 2008-2011 The IPython Development Team
7 # Copyright (C) 2008-2011 The IPython Development Team
8 #
8 #
9 # Distributed under the terms of the BSD License. The full license is in
9 # Distributed under the terms of the BSD License. The full license is in
10 # the file COPYING, distributed as part of this software.
10 # the file COPYING, distributed as part of this software.
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12
12
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 # Imports
14 # Imports
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16
16
17 from IPython.lib.latextools import latex_to_png
17 from IPython.lib.latextools import latex_to_png
18
18
19 from sympy import pretty, latex
19 from sympy import pretty, latex
20
20
21 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
22 # Definitions of magic functions for use with IPython
22 # Definitions of magic functions for use with IPython
23 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
24
24
25 def print_basic_unicode(o, p, cycle):
25 def print_basic_unicode(o, p, cycle):
26 """A function to pretty print sympy Basic objects."""
26 """A function to pretty print sympy Basic objects."""
27 if cycle:
27 if cycle:
28 return p.text('Basic(...)')
28 return p.text('Basic(...)')
29 out = pretty(o, use_unicode=True)
29 out = pretty(o, use_unicode=True)
30 if '\n' in out:
30 if '\n' in out:
31 p.text(u'\n')
31 p.text(u'\n')
32 p.text(out)
32 p.text(out)
33
33
34
34
35 def print_png(o):
35 def print_png(o):
36 """A funciton to display sympy expression using LaTex -> PNG."""
36 """A funciton to display sympy expression using LaTex -> PNG."""
37 s = latex(o, mode='inline')
37 s = latex(o, mode='inline')
38 # mathtext does not understand \\operatorname to we remove it so functions
38 # mathtext does not understand certain latex flags, so we try to replace
39 # like sin, cos can print. We should possible replace it with mathrm.
39 # them with suitable subs.
40 s = s.replace('\\operatorname','')
40 s = s.replace('\\operatorname','')
41 s = s.replace('\\overline', '\\bar')
41 png = latex_to_png(s, encode=True)
42 png = latex_to_png(s, encode=True)
42 return png
43 return png
43
44
44 _loaded = False
45 _loaded = False
45
46
46
47
47 def load_ipython_extension(ip):
48 def load_ipython_extension(ip):
48 """Load the extension in IPython."""
49 """Load the extension in IPython."""
49 global _loaded
50 global _loaded
50 if not _loaded:
51 if not _loaded:
51 plaintext_formatter = ip.display_formatter.formatters['text/plain']
52 plaintext_formatter = ip.display_formatter.formatters['text/plain']
52 plaintext_formatter.for_type_by_name(
53 plaintext_formatter.for_type_by_name(
53 'sympy.core.basic', 'Basic', print_basic_unicode
54 'sympy.core.basic', 'Basic', print_basic_unicode
54 )
55 )
56 plaintext_formatter.for_type_by_name(
57 'sympy.matrices.matrices', 'Matrix', print_basic_unicode
58 )
59
55 png_formatter = ip.display_formatter.formatters['image/png']
60 png_formatter = ip.display_formatter.formatters['image/png']
56 png_formatter.for_type_by_name(
61 png_formatter.for_type_by_name(
57 'sympy.core.basic', 'Basic', print_png
62 'sympy.core.basic', 'Basic', print_png
58 )
63 )
59 _loaded = True
64 _loaded = True
60
65
General Comments 0
You need to be logged in to leave comments. Login now