Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changes to add root_notation flag in mathml.py. #15901

Merged
merged 10 commits into from Feb 7, 2019
39 changes: 32 additions & 7 deletions sympy/printing/mathml.py
Expand Up @@ -8,9 +8,9 @@
from sympy.core.function import _coeff_isneg
from sympy.core.alphabets import greeks
from sympy.core.compatibility import range
from .printer import Printer
from .pretty.pretty_symbology import greek_unicode
from .conventions import split_super_sub, requires_partial
from sympy.printing.printer import Printer
from sympy.printing.pretty.pretty_symbology import greek_unicode
from sympy.printing.conventions import split_super_sub, requires_partial


class MathMLPrinterBase(Printer):
Expand All @@ -20,8 +20,11 @@ class MathMLPrinterBase(Printer):

_default_settings = {
"order": None,
"encoding": "utf-8"
"encoding": "utf-8",
"root_notation": True,
"symbol_names": {},
}

def __init__(self, settings=None):
Printer.__init__(self, settings)
from xml.dom.minidom import Document,Text
Expand Down Expand Up @@ -112,6 +115,12 @@ class MathMLContentPrinter(MathMLPrinterBase):
"""
printmethod = "_mathml_content"

def __init__(self, settings=None):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove this and rely on the parent class instead I think.

Printer.__init__(self, settings)
from xml.dom.minidom import Document,Text

self.dom = Document()

def mathml_tag(self, e):
"""Returns the MathML tag for an expression."""
translate = {
Expand Down Expand Up @@ -378,7 +387,7 @@ def translate(s):

def _print_Pow(self, e):
# Here we use root instead of power if the exponent is the reciprocal of an integer
if e.exp.is_Rational and e.exp.p == 1:
if e.exp.is_Rational and e.exp.p == 1 and self._settings['root_notation']:
x = self.dom.createElement('apply')
x.appendChild(self.dom.createElement('root'))
if e.exp.q != 2:
Expand Down Expand Up @@ -466,6 +475,12 @@ class MathMLPresentationPrinter(MathMLPrinterBase):
"""
printmethod = "_mathml_presentation"

def __init__(self, settings=None):
Printer.__init__(self, settings)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, it is better to call MathMLPrinterBase instead. However, this is just for future reference as this is already done in #15906. (Even better is super(MathMLPresentationPrinter, self).__init__(self, settings).)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay

from xml.dom.minidom import Document,Text

self.dom = Document()

def mathml_tag(self, e):
"""Returns the MathML tag for an expression."""
translate = {
Expand Down Expand Up @@ -796,7 +811,7 @@ def _print_Pow(self, e):
x.appendChild(self._print(e.exp))
return x

if e.exp.is_Rational and e.exp.p == 1:
if e.exp.is_Rational and e.exp.p == 1 and self._settings['root_notation']:
if e.exp.q == 2:
x = self.dom.createElement('msqrt')
x.appendChild(self._print(e.base))
Expand Down Expand Up @@ -905,7 +920,7 @@ def mathml(expr, printer='content', **settings):
return MathMLContentPrinter(settings).doprint(expr)


def print_mathml(expr, printer='content', **settings):
def print_mathml(expr, printer='content', symbol_names=None, root_notation=True, order=None):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be very useful if you can add all the settings from #15877 here. Starting with printer and order (to not break backwards compatibility) and then the rest in alphabetical order.

Even better if they are also added below, but here is the first priority.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay, i am adding those settings.

"""
Prints a pretty representation of the MathML code for expr. If printer is
presentation then prints Presentation MathML else prints content MathML.
Expand All @@ -930,6 +945,16 @@ def print_mathml(expr, printer='content', **settings):
</mrow>

"""

if symbol_names is None:
symbol_names = {}

settings = {
'order' : order,
'symbol_names' : symbol_names,
'root_notation' : root_notation,
}

if printer == 'presentation':
s = MathMLPresentationPrinter(settings)
else:
Expand Down