SHARE
TWEET

Untitled

a guest Jun 15th, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import sys
  2. sys.path.append('/home/vagrant/.local/lib/python3.4/site-packages')
  3. from sympy import *
  4. from sympy.printing.mathml import mathml
  5.  
  6. def returnMathML(value):
  7.     str = "<math>"
  8.     if '=' in value:#文字列中に=(イコール)が含まれている場合
  9.         str += mathml(Eq(*map(sympify, value.split('='))),printer='presentation')
  10.     else:
  11.         str += mathml(sympify(value),printer='presentation')
  12.     str += "</math>"
  13.     return str
  14.  
  15. print(returnMathML("A"))
  16.      
  17. <math><mi>A</mi></math>
  18.      
  19. print(returnMathML("y=a*x**2+b*x+c"))
  20.      
  21. $ python test001.py
  22. <math><mrow><mi>y</mi><mo>=</mo><mrow><mrow><mi>a</mi><mo>&InvisibleTimes;</mo><msup><mi>x</mi><mn>2</mn></msup></mrow><mo>+</mo><mrow><mi>b</mi><mo>&InvisibleTimes;</mo><mi>x</mi></mrow><mo>+</mo><mi>c</mi></mrow></mrow></math>
  23.      
  24. print(returnMathML("S"))
  25.      
  26. $ python test001.py
  27. Traceback (most recent call last):
  28.   File "test001.py", line 15, in <module>
  29.     print(returnMathML("S"))
  30.   File "test001.py", line 11, in returnMathML
  31.     str += mathml(sympify(value),printer='presentation')
  32.   File "/home/vagrant/.pyenv/versions/3.6.7/lib/python3.6/site-packages/sympy/printing/mathml.py", line 1904, in mathml
  33.     return MathMLPresentationPrinter(settings).doprint(expr)
  34.   File "/home/vagrant/.pyenv/versions/3.6.7/lib/python3.6/site-packages/sympy/printing/mathml.py", line 68, in doprint
  35.     unistr = mathML.toxml()
  36. AttributeError: 'str' object has no attribute 'toxml'
  37.      
  38. print(returnMathML("S=abs(a)*(β-α)**3/6"))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top