[PATCH] Add support for printing tuples with the mathml backend

1 view
Skip to first unread message

fabian...@gmail.com

unread,
Feb 3, 2009, 7:06:08 PM2/3/09
to sympy-...@googlegroups.com, Fabian Seoane
From: Fabian Seoane <fab...@fseoane.net>

---
sympy/printing/mathml.py | 20 ++++++++++++++++++++
sympy/printing/tests/test_mathml.py | 14 +++++++++++++-
2 files changed, 33 insertions(+), 1 deletions(-)

diff --git a/sympy/printing/mathml.py b/sympy/printing/mathml.py
index ec6c88c..ddae019 100644
--- a/sympy/printing/mathml.py
+++ b/sympy/printing/mathml.py
@@ -6,6 +6,12 @@ from sympy import Basic, sympify
from printer import Printer

class MathMLPrinter(Printer):
+ """Prints an expression to the MathML markup language
+
+ Whenever possible tries to use Content markup and not Presentation markup.
+
+ References: http://www.w3.org/TR/MathML2/
+ """
printmethod = "_mathml_"

def __init__(self, *args, **kwargs):
@@ -23,6 +29,7 @@ class MathMLPrinter(Printer):
'Mul': 'times',
'Derivative': 'diff',
'Number': 'cn',
+ 'int': 'cn',
'Pow': 'power',
'Symbol': 'ci',
'Integral': 'int'
@@ -138,6 +145,19 @@ class MathMLPrinter(Printer):
x.appendChild(self._print(arg))
return x

+ def _print_list(self, seq):
+ """MathML reference for the <list> element:
+ http://www.w3.org/TR/MathML2/chapter4.html#contm.list"""
+ dom_element = self.dom.createElement('list')
+ for item in seq:
+ dom_element.appendChild(self._print(item))
+ return dom_element
+
+ def _print_int(self, p):
+ dom_element = self.dom.createElement(self.mathml_tag(p))
+ dom_element.appendChild(self.dom.createTextNode(str(p)))
+ return dom_element
+
def mathml(expr):
"""Returns the MathML representation of expr"""
s = MathMLPrinter()
diff --git a/sympy/printing/tests/test_mathml.py b/sympy/printing/tests/test_mathml.py
index 6d7f55c..4e7890a 100644
--- a/sympy/printing/tests/test_mathml.py
+++ b/sympy/printing/tests/test_mathml.py
@@ -1,4 +1,4 @@
-from sympy import diff, Integral, Limit, sin, Symbol
+from sympy import diff, Integral, Limit, sin, Symbol, Integer
from sympy.printing.mathml import mathml, MathMLPrinter
from xml.dom.minidom import parseString

@@ -67,6 +67,18 @@ def test_mathml_integrals():
assert mml_1.childNodes[3].nodeName == 'uplimit'
assert mml_1.childNodes[4].toxml() == mp._print(integrand).toxml()

+def test_mathml_tuples():
+ mml_1 = mp._print([2])
+ assert mml_1.nodeName == 'list'
+ assert mml_1.childNodes[0].nodeName == 'cn'
+ assert len(mml_1.childNodes) == 1
+
+ mml_2 = mp._print([2, Integer(1)])
+ assert mml_2.nodeName == 'list'
+ assert mml_2.childNodes[0].nodeName == 'cn'
+ assert mml_2.childNodes[1].nodeName == 'cn'
+ assert len(mml_2.childNodes) == 2
+
def test_mathml_matrices():
pass #TODO

--
1.6.0.2

Ondrej Certik

unread,
Feb 3, 2009, 8:31:57 PM2/3/09
to sympy-...@googlegroups.com
+1 thanks
Reply all
Reply to author
Forward
0 new messages