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

added the fold_frac_powers and fold_short_frac flag to mathml printer #16075

Merged
merged 6 commits into from
Mar 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions sympy/printing/mathml.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,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 @@ -636,6 +638,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 @@ -861,6 +865,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 @@ -982,6 +982,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