Skip to content

Commit

Permalink
Merge pull request #15901 from shiksha11/newbranch2
Browse files Browse the repository at this point in the history
changes to add root_notation flag in mathml.py.
  • Loading branch information
oscargus committed Feb 7, 2019
2 parents a9e9aa7 + ed2a659 commit d05637a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 30 deletions.
47 changes: 18 additions & 29 deletions sympy/printing/mathml.py
Expand Up @@ -7,9 +7,9 @@
from sympy import sympify, S, Mul
from sympy.core.function import _coeff_isneg
from sympy.core.compatibility import range
from .conventions import split_super_sub, requires_partial
from .pretty.pretty_symbology import greek_unicode
from .printer import Printer
from sympy.printing.conventions import split_super_sub, requires_partial
from sympy.printing.pretty.pretty_symbology import greek_unicode
from sympy.printing.printer import Printer


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

_default_settings = {
"order": None,
"encoding": "utf-8"
"encoding": "utf-8",
"fold_frac_powers": False,
"fold_func_brackets": False,
"fold_short_frac": None,
"inv_trig_style": "abbreviated",
"ln_notation": False,
"long_frac_ratio": None,
"mat_delim": "[",
"mat_symbol_style": "plain",
"mul_symbol": None,
"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 @@ -376,7 +388,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 self._settings['root_notation'] and e.exp.is_Rational and e.exp.p == 1:
x = self.dom.createElement('apply')
x.appendChild(self.dom.createElement('root'))
if e.exp.q != 2:
Expand Down Expand Up @@ -464,29 +476,6 @@ class MathMLPresentationPrinter(MathMLPrinterBase):
"""
printmethod = "_mathml_presentation"

def __init__(self, settings=None):
MathMLPrinterBase.__init__(self, settings)

_default_settings = {
"fold_frac_powers": False,
"fold_func_brackets": False,
"fold_short_frac": None,
"inv_trig_style": "abbreviated",
"ln_notation": False,
"long_frac_ratio": None,
"mat_delim": "[",
"mat_symbol_style": "plain",
"mul_symbol": None,
"root_notation": True,
"symbol_names": {},
"order": None
}

self._settings = _default_settings

if settings is not None:
self._settings.update(settings)

def mathml_tag(self, e):
"""Returns the MathML tag for an expression."""
translate = {
Expand Down Expand Up @@ -816,7 +805,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
8 changes: 7 additions & 1 deletion sympy/printing/tests/test_mathml.py
@@ -1,6 +1,6 @@
from sympy import diff, Integral, Limit, sin, Symbol, Integer, Rational, cos, \
tan, asin, acos, atan, sinh, cosh, tanh, asinh, acosh, atanh, E, I, oo, \
pi, GoldenRatio, EulerGamma, Sum, Eq, Ne, Ge, Lt, Float, Matrix, Basic
pi, GoldenRatio, EulerGamma, Sum, Eq, Ne, Ge, Lt, Float, Matrix, Basic, S
from sympy.printing.mathml import mathml, MathMLContentPrinter, MathMLPresentationPrinter, \
MathMLPrinter

Expand Down Expand Up @@ -934,7 +934,13 @@ def test_toprettyxml_hooking():
assert prettyxml_old1 == doc1.toprettyxml()
assert prettyxml_old2 == doc2.toprettyxml()


def test_print_basic():
expr = Basic(1, 2)
assert mpp.doprint(expr) == '<mrow><mi>basic</mi><mfenced><mn>1</mn><mn>2</mn></mfenced></mrow>'
assert mp.doprint(expr) == '<basic><cn>1</cn><cn>2</cn></basic>'


def test_root_notation_print():
assert mathml(x**(S(1)/3), printer='presentation') == '<mroot><mi>x</mi><mn>3</mn></mroot>'
assert mathml(x**(S(1)/3), printer='presentation', root_notation=False) == '<msup><mi>x</mi><mfrac><mn>1</mn><mn>3</mn></mfrac></msup>'

0 comments on commit d05637a

Please sign in to comment.