def test_mul_symbol_print(): expr = x * y assert mathml(expr, printer='presentation') == '<mrow><mi>x</mi><mo>⁢</mo><mi>y</mi></mrow>' assert mathml(expr, printer='presentation', mul_symbol=None) == '<mrow><mi>x</mi><mo>⁢</mo><mi>y</mi></mrow>' assert mathml(expr, printer='presentation', mul_symbol='dot') == '<mrow><mi>x</mi><mo>·</mo><mi>y</mi></mrow>' assert mathml(expr, printer='presentation', mul_symbol='ldot') == '<mrow><mi>x</mi><mo>․</mo><mi>y</mi></mrow>' assert mathml(expr, printer='presentation', mul_symbol='times') == '<mrow><mi>x</mi><mo>×</mo><mi>y</mi></mrow>'
def test_print_derivative(): f = Function('f') z = Symbol('z') d = Derivative(f(x, y, z), x, z, x, z, z, y) assert mathml( d ) == r'<apply><partialdiff/><bvar><ci>y</ci><ci>z</ci><degree><cn>2</cn></degree><ci>x</ci><ci>z</ci><ci>x</ci></bvar><apply><f/><ci>x</ci><ci>y</ci><ci>z</ci></apply></apply>' assert mathml( d, printer='presentation' ) == r'<mrow><mfrac><mrow><msup><mo>∂</mo><mn>6</mn></msup></mrow><mrow><mo>∂</mo><mi>y</mi><msup><mo>∂</mo><mn>2</mn></msup><mi>z</mi><mo>∂</mo><mi>x</mi><mo>∂</mo><mi>z</mi><mo>∂</mo><mi>x</mi></mrow></mfrac><mrow><mi>f</mi><mfenced><mi>x</mi><mi>y</mi><mi>z</mi></mfenced></mrow></mrow>'
def test_mat_delim_print(): expr = Matrix([[1, 2], [3, 4]]) assert mathml( expr, printer='presentation', mat_delim='[' ) == '<mfenced close="]" open="["><mtable><mtr><mtd><mn>1</mn></mtd><mtd><mn>2</mn></mtd></mtr><mtr><mtd><mn>3</mn></mtd><mtd><mn>4</mn></mtd></mtr></mtable></mfenced>' assert mathml( expr, printer='presentation', mat_delim='(' ) == '<mfenced><mtable><mtr><mtd><mn>1</mn></mtd><mtd><mn>2</mn></mtd></mtr><mtr><mtd><mn>3</mn></mtd><mtd><mn>4</mn></mtd></mtr></mtable></mfenced>' assert mathml( expr, printer='presentation', mat_delim='' ) == '<mtable><mtr><mtd><mn>1</mn></mtd><mtd><mn>2</mn></mtd></mtr><mtr><mtd><mn>3</mn></mtd><mtd><mn>4</mn></mtd></mtr></mtable>'
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>' assert mathml( x**(S(1) / 3), printer='content' ) == '<apply><root/><degree><ci>3</ci></degree><ci>x</ci></apply>' 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_mul_symbol_print(): expr = x * y assert mathml( expr, printer='presentation' ) == '<mrow><mi>x</mi><mo>⁢</mo><mi>y</mi></mrow>' assert mathml( expr, printer='presentation', mul_symbol=None ) == '<mrow><mi>x</mi><mo>⁢</mo><mi>y</mi></mrow>' assert mathml( expr, printer='presentation', mul_symbol='dot') == '<mrow><mi>x</mi><mo>·</mo><mi>y</mi></mrow>' assert mathml(expr, printer='presentation', mul_symbol='ldot' ) == '<mrow><mi>x</mi><mo>․</mo><mi>y</mi></mrow>' assert mathml(expr, printer='presentation', mul_symbol='times' ) == '<mrow><mi>x</mi><mo>×</mo><mi>y</mi></mrow>'
def rateLawToMathml(rateLaw): myMML = rateLaw.replace("^", "**") tokens = [] i = 0 for M in re.finditer(r'[a-zA-Z_]\w*', myMML): token = str(i) for j, l in enumerate( ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]): token = token.replace(str(j), l) i = i + 1 token = "T" + token + "T" tokens.append({ "o": M.group(0), "T": token, "s": M.start(), "e": M.end() }) for token in reversed(tokens): myMML = myMML[:token["s"]] + token["T"] + myMML[token["e"]:] savecopy = myMML myMML = mathml(parse_expr(myMML), printer='presentation') tokens = {T["T"]: T["o"] for T in tokens} pattern = re.compile("|".join(tokens.keys())) myMML = pattern.sub(lambda m: tokens[m.group(0)], myMML) return myMML
def _sympy_format(self, method, variables, backend, default, **kwargs): variables = variables or {} if backend in (None, math): backend = sympy variables = defaultkeydict( None if default is None else (lambda k: backend.Symbol(default(k))), { k: v if isinstance(v, Expr) else (backend.Symbol(v) if isinstance(v, str) else backend.Float(v)) for k, v in variables.items() }, ) expr = self(variables, backend=backend, **kwargs).simplify() if method == "latex": return backend.latex(expr) elif method == "str": return str(expr) elif method == "unicode": return backend.pretty(expr, use_unicode=True) elif method == "mathml": from sympy.printing.mathml import mathml return mathml(expr) else: raise NotImplementedError("Unknown method: %s" % method)
def test_mathml_constants(): mml = mp._print(I) assert mml.nodeName == 'imaginaryi' mml = mp._print(E) assert mml.nodeName == 'exponentiale' mml = mp._print(oo) assert mml.nodeName == 'infinity' mml = mp._print(pi) assert mml.nodeName == 'pi' assert mathml(GoldenRatio) == '<cn>φ</cn>' mml = mathml(EulerGamma) assert mml == '<eulergamma/>'
def test_mathml_constants(): mml = mp._print(I) assert mml.nodeName == 'imaginaryi' mml = mp._print(E) assert mml.nodeName == 'exponentiale' mml = mp._print(oo) assert mml.nodeName == 'infinity' mml = mp._print(pi) assert mml.nodeName == 'pi' assert mathml(GoldenRatio) == u'<cn>\u03c6</cn>' mml = mathml(EulerGamma) assert mml == '<eulergamma/>'
def test_mathml_constants(): mml = mp._print(I) assert mml.nodeName == "imaginaryi" mml = mp._print(E) assert mml.nodeName == "exponentiale" mml = mp._print(oo) assert mml.nodeName == "infinity" mml = mp._print(pi) assert mml.nodeName == "pi" assert mathml(GoldenRatio) == "<cn>φ</cn>" mml = mathml(EulerGamma) assert mml == "<eulergamma/>"
def test_mathml_constants(): mml = mp._print(I) assert mml.nodeName == "imaginaryi" mml = mp._print(E) assert mml.nodeName == "exponentiale" mml = mp._print(oo) assert mml.nodeName == "infinity" mml = mp._print(pi) assert mml.nodeName == "pi" # FIXME-py3k: AssertionError assert mathml(GoldenRatio) == "<cn>\xcf\x86</cn>" mml = mathml(EulerGamma) assert mml == "<eulergamma/>"
def print_gtk(x, start_viewer=True): """Print to Gtkmathview, a gtk widget capable of rendering MathML. Needs libgtkmathview-bin""" with tempfile.NamedTemporaryFile('w') as file: file.write(c2p(mathml(x), simple=True)) file.flush() if start_viewer: subprocess.check_call(('mathmlviewer', file.name))
def print_gtk(x): """Print to Gtkmathview, a gtk widget capable of rendering MathML. Needs libgtkmathview-bin""" from sympy.utilities.mathml import c2p tmp = tempfile.mktemp() # create a temp file to store the result file = open(tmp, 'wb') file.write( c2p(mathml(x), simple=True) ) file.close() os.system("mathmlviewer " + tmp)
def print_gtk(x, start_viewer=True): """Print to Gtkmathview, a gtk widget capable of rendering MathML. Needs libgtkmathview-bin""" from sympy.utilities.mathml import c2p tmp = tempfile.mktemp() # create a temp file to store the result with open(tmp, 'wb') as file: file.write( c2p(mathml(x), simple=True) ) if start_viewer: os.system("mathmlviewer " + tmp)
def print_gtk(x, start_viewer=True): """Print to Gtkmathview, a gtk widget capable of rendering MathML. Needs libgtkmathview-bin """ from sympy.utilities.mathml import c2p tmp = tempfile.mkstemp() # create a temp file to store the result with open(tmp, 'wb') as file: file.write(c2p(mathml(x), simple=True)) if start_viewer: os.system("mathmlviewer " + tmp)
def test_presentation_mathml_constants(): mml = mpp._print(I) assert mml.childNodes[0].nodeValue == 'ⅈ' mml = mpp._print(E) assert mml.childNodes[0].nodeValue == 'ⅇ' mml = mpp._print(oo) assert mml.childNodes[0].nodeValue == '∞' mml = mpp._print(pi) assert mml.childNodes[0].nodeValue == 'π' assert mathml(GoldenRatio, printer='presentation') == '<mi>φ</mi>'
def _sympy_format(self, method, variables, backend, default, **kwargs): variables = variables or {} if backend in (None, math): backend = sympy variables = defaultkeydict( None if default is None else (lambda k: backend.Symbol(default(k))), {k: v if isinstance(v, Expr) else (backend.Symbol(v) if isinstance(v, str) else backend.Float(v)) for k, v in variables.items()}) expr = self(variables, backend=backend, **kwargs).simplify() if method == 'latex': return backend.latex(expr) elif method == 'str': return str(expr) elif method == 'unicode': return backend.pretty(expr, use_unicode=True) elif method == 'mathml': from sympy.printing.mathml import mathml return mathml(expr) else: raise NotImplementedError("Unknown method: %s" % method)
def test_print_derivative(): f = Function('f') z = Symbol('z') d = Derivative(f(x, y, z), x, z, x, z, z, y) assert mathml(d) == r'<apply><partialdiff/><bvar><ci>y</ci><ci>z</ci><degree><cn>2</cn></degree><ci>x</ci><ci>z</ci><ci>x</ci></bvar><apply><f/><ci>x</ci><ci>y</ci><ci>z</ci></apply></apply>' assert mathml(d, printer='presentation') == r'<mrow><mfrac><mrow><msup><mo>∂</mo><mn>6</mn></msup></mrow><mrow><mo>∂</mo><mi>y</mi><msup><mo>∂</mo><mn>2</mn></msup><mi>z</mi><mo>∂</mo><mi>x</mi><mo>∂</mo><mi>z</mi><mo>∂</mo><mi>x</mi></mrow></mfrac><mrow><mi>f</mi><mfenced><mi>x</mi><mi>y</mi><mi>z</mi></mfenced></mrow></mrow>'
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>'
def show_val(val_x): html = mml(mathml(val_x, printer="presentation")) # html += " [" + escape(mathml(val_x, printer="presentation")) + "]" # html += " [" + str(val_x) + "]" return html
def test_presentation_settings(): raises( TypeError, lambda: mathml(Symbol("x"), printer='presentation', method="garbage"))
def test_content_settings(): raises(TypeError, lambda: mathml(Symbol("x"), method="garbage"))
def test_presentation_settings(): raises(TypeError, lambda: mathml(Symbol("x"), printer='presentation',method="garbage"))
def test_print_matrix_symbol(): A = MatrixSymbol('A', 1, 2) assert mpp.doprint(A) == '<mi>A</mi>' assert mp.doprint(A) == '<ci>A</ci>' assert mathml(A, printer='presentation', mat_symbol_style="bold" )== '<mi mathvariant="bold">A</mi>' assert mathml(A, mat_symbol_style="bold" )== '<ci>A</ci>' # No effect in content printer
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>' assert mathml(x**(S(1)/3), printer='content') == '<apply><root/><degree><ci>3</ci></degree><ci>x</ci></apply>' 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 differentSigns(request): class differentSign: a, b, c, x, f = symbols('a b c x f') def NOK(a,b): m = a*b while a != 0 and b != 0: if a > b: a %= b else: b %= a return m // (a+b) def squareEq(strName): x = symbols(strName) temp = choice((1, 2, 3)) x1 = Rational(choice([i for i in range(-10, 10, 1)]), temp) x2 = Rational(choice([i for i in range(-10, 10, 1)]), temp) nab = fraction(Rational(-x1 - x2, 1))[1] nb = fraction(Rational(-x1 - x2, 1))[0] nac = fraction(Rational(x1 * x2, 1))[1] nc = fraction(Rational(x1 * x2, 1))[0] na = NOK(nac, nab) nb = nb * (na / nab) nc = nc * (na / nac) f = expand(na*(x - x1)*(x - x2)) return f def linearEq(strName): x = symbols(strName) xMult = choice([i for i in range(1, 5)]) if (random() > 0.8): f = xMult * x else: f = xMult * x - Rational(choice([i for i in range(-10, 10, 1)]), choice((1, 2))) return f def randomEq(strName): if (random() > 0.5): return linearEq(strName) else: return squareEq(strName) def parametr1(strNameParametr='a', strNameVariable='x'): temp = differentSign() temp.a = randomEq(strNameParametr) temp.b = randomEq(strNameParametr) temp.c = randomEq(strNameParametr) temp.x = symbols(strNameVariable) temp.f = Poly(temp.a * temp.x**2 + temp.b * temp.x + temp.c, temp.x).as_expr() a = symbols('a') temp.ans = solve_poly_inequality(Poly(temp.a * temp.c, a), '<') temp.ans = tuple(temp.ans) return temp f = parametr1() fString = mathml(simplify(f.f), 'presentation') # print_mathml fAns = mathml(f.ans, 'presentation') # mathml return render( request, 'differentSign.html', context={'equation':fString,'ans':fAns}, )
def test_ln_notation_print(): expr = log(x) assert mathml(expr, printer='presentation') == '<mrow><mi>log</mi><mfenced><mi>x</mi></mfenced></mrow>' assert mathml(expr, printer='presentation', ln_notation=False) == '<mrow><mi>log</mi><mfenced><mi>x</mi></mfenced></mrow>' assert mathml(expr, printer='presentation', ln_notation=True) == '<mrow><mi>ln</mi><mfenced><mi>x</mi></mfenced></mrow>'
def test_settings(): raises(TypeError, lambda: mathml(Symbol("x"), method="garbage"))
def test_mat_delim_print(): expr = Matrix([[1, 2], [3, 4]]) assert mathml(expr, printer='presentation', mat_delim='[') == '<mfenced close="]" open="["><mtable><mtr><mtd><mn>1</mn></mtd><mtd><mn>2</mn></mtd></mtr><mtr><mtd><mn>3</mn></mtd><mtd><mn>4</mn></mtd></mtr></mtable></mfenced>' assert mathml(expr, printer='presentation', mat_delim='(') == '<mfenced><mtable><mtr><mtd><mn>1</mn></mtd><mtd><mn>2</mn></mtd></mtr><mtr><mtd><mn>3</mn></mtd><mtd><mn>4</mn></mtd></mtr></mtable></mfenced>' assert mathml(expr, printer='presentation', mat_delim='') == '<mtable><mtr><mtd><mn>1</mn></mtd><mtd><mn>2</mn></mtd></mtr><mtr><mtd><mn>3</mn></mtd><mtd><mn>4</mn></mtd></mtr></mtable>'
delta_outVar = sp.Symbol('Delta_' + math_models['#INSTRUMENT_MODEL#'][0]) delta_diffVars = [sp.Symbol('Delta_' + str(d)) for d in diffVars] subsEqn = deviceEqn.subs(effectVar, effectEqn) diffEqn = sp.sympify('0') for s, ds in zip(diffVars, delta_diffVars): diffEqn = diffEqn + sp.diff(subsEqn, s) * ds print 'diffEqn', str(diffEqn) math_models['#INSTRUMENT_ERROR_MATH#'] = [ unicode(delta_outVar), unicode(diffEqn) ] math_docx = {} for k in math_models.keys(): LHS_txt, RHS_txt = math_models[k] LHS_mml, RHS_mml = mathml(sp.sympify(LHS_txt)), mathml(sp.sympify(RHS_txt)) content_mml_tree = etree.fromstring( '<math xmlns:mml="http://www.w3.org/1998/Math/MathML" overflow="scroll"><apply><eq/>' + LHS_mml + RHS_mml + '</apply></math>') mml_tree = c2p_transform(content_mml_tree) # Частая ошибка -- знак умножения заменяется невидимым умножением ⁢, но потом интерпретируется как набор отдельных ASCII символов #for el in mml_tree.findall( '//{http://www.w3.org/1998/Math/MathML}mo' ): # if el.text!= None : el.text = el.text.replace( "⁢", u"\u2062" ) for el in mml_tree.findall('//{http://www.w3.org/1998/Math/MathML}mo'): if el.text != None: el.text = el.text.replace("⁢", '*') omml_formula = transform(mml_tree) math_docx[k] = omml_formula.getroot()