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
Changes from 2 commits
128853d
066f9b3
032c1c6
741742e
e43f214
7be4d2b
6581e26
5dcfec5
a4489d7
ed2a659
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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): | ||
|
@@ -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 | ||
|
@@ -112,6 +115,12 @@ class MathMLContentPrinter(MathMLPrinterBase): | |
""" | ||
printmethod = "_mathml_content" | ||
|
||
def __init__(self, settings=None): | ||
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 = { | ||
|
@@ -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: | ||
|
@@ -466,6 +475,12 @@ class MathMLPresentationPrinter(MathMLPrinterBase): | |
""" | ||
printmethod = "_mathml_presentation" | ||
|
||
def __init__(self, settings=None): | ||
Printer.__init__(self, settings) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = { | ||
|
@@ -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)) | ||
|
@@ -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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Even better if they are also added below, but here is the first priority. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
@@ -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: | ||
|
There was a problem hiding this comment.
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.