Skip to content

Commit

Permalink
Merge pull request #16075 from shubhamabhang77/configuration_fold_fra…
Browse files Browse the repository at this point in the history
…c_powers_and_fold_short_frac

added the fold_frac_powers and fold_short_frac flag to mathml printer
  • Loading branch information
oscargus committed Mar 2, 2019
2 parents 2dd1275 + 7fa4892 commit b786c99
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
25 changes: 25 additions & 0 deletions sympy/printing/mathml.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,8 @@ def multiply(expr, mrow):
numer, denom = fraction(expr)
if denom is not S.One:
frac = self.dom.createElement('mfrac')
if self._settings["fold_short_frac"] and len(str(expr)) < 7:
frac.setAttribute('bevelled', 'true')
xnum = self._print(numer)
xden = self._print(denom)
frac.appendChild(xnum)
Expand Down Expand Up @@ -637,6 +639,8 @@ def _print_Rational(self, e):
x.appendChild(self.dom.createTextNode(str(e.p)))
return x
x = self.dom.createElement('mfrac')
if self._settings["fold_frac_powers"]:
x.setAttribute('bevelled', 'true')
num = self.dom.createElement('mn')
num.appendChild(self.dom.createTextNode(str(e.p)))
x.appendChild(num)
Expand Down Expand Up @@ -862,6 +866,27 @@ def _print_binomial(self, expr, exp=None):

def _print_Pow(self, e):
# Here we use root instead of power if the exponent is the reciprocal of an integer
if self._settings['fold_short_frac'] and len(str(e.base)) < 7:
if e.exp.is_negative:
frac = self.dom.createElement('mfrac')
frac.setAttribute('bevelled', 'true')
frac.appendChild(self._print(1))
mrow = self.dom.createElement('mrow')
if e.base.is_symbol:
x = self.dom.createElement('mfenced')
x.appendChild(self._print(e.base))
mrow.appendChild(x)
else:
mrow.appendChild(self._print(e.base))
if e.exp.p == -1 and e.exp.q == 1:
frac.appendChild(mrow)
return frac
x = self.dom.createElement('msup')
x.appendChild(mrow)
x.appendChild(self._print(-e.exp))
frac.appendChild(x)
return frac

if e.exp.is_negative or len(str(e.base)) > 1:
mrow = self.dom.createElement('mrow')
x = self.dom.createElement('mfenced')
Expand Down
14 changes: 14 additions & 0 deletions sympy/printing/tests/test_mathml.py
Original file line number Diff line number Diff line change
Expand Up @@ -1072,6 +1072,20 @@ def test_root_notation_print():
assert mathml(x**(S(1)/3), printer='content', root_notation=False) == '<apply><power/><ci>x</ci><apply><divide/><cn>1</cn><cn>3</cn></apply></apply>'


def test_fold_frac_powers_print():
expr = x ** Rational(5, 2)
assert mathml(expr, printer='presentation') == '<msup><mi>x</mi><mfrac><mn>5</mn><mn>2</mn></mfrac></msup>'
assert mathml(expr, printer='presentation', fold_frac_powers=True) == '<msup><mi>x</mi><mfrac bevelled="true"><mn>5</mn><mn>2</mn></mfrac></msup>'
assert mathml(expr, printer='presentation', fold_frac_powers=False) == '<msup><mi>x</mi><mfrac><mn>5</mn><mn>2</mn></mfrac></msup>'


def test_fold_short_frac_print():
expr = 1 / y ** 2
assert mathml(expr, printer='presentation') == '<msup><mrow><mfenced><mi>y</mi></mfenced></mrow><mn>-2</mn></msup>'
assert mathml(expr, printer='presentation', fold_short_frac=True) == '<mfrac bevelled="true"><mn>1</mn><msup><mrow><mfenced><mi>y</mi></mfenced></mrow><mn>2</mn></msup></mfrac>'
assert mathml(expr, printer='presentation', fold_short_frac=False) == '<msup><mrow><mfenced><mi>y</mi></mfenced></mrow><mn>-2</mn></msup>'


def test_print_factorials():
assert mpp.doprint(factorial(x)) == '<mrow><mi>x</mi><mo>!</mo></mrow>'
assert mpp.doprint(factorial(x + 1)) == '<mrow><mfenced><mrow><mi>x</mi><mo>+</mo><mn>1</mn></mrow></mfenced><mo>!</mo></mrow>'
Expand Down

0 comments on commit b786c99

Please sign in to comment.