예제 #1
0
def test_mul_symbol_print():
    expr = x * y
    assert mathml(expr, printer='presentation') == '<mrow><mi>x</mi><mo>&InvisibleTimes;</mo><mi>y</mi></mrow>'
    assert mathml(expr, printer='presentation', mul_symbol=None) == '<mrow><mi>x</mi><mo>&InvisibleTimes;</mo><mi>y</mi></mrow>'
    assert mathml(expr, printer='presentation', mul_symbol='dot') == '<mrow><mi>x</mi><mo>&#xB7;</mo><mi>y</mi></mrow>'
    assert mathml(expr, printer='presentation', mul_symbol='ldot') == '<mrow><mi>x</mi><mo>&#x2024;</mo><mi>y</mi></mrow>'
    assert mathml(expr, printer='presentation', mul_symbol='times') == '<mrow><mi>x</mi><mo>&#xD7;</mo><mi>y</mi></mrow>'
예제 #2
0
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>&#x2202;</mo><mn>6</mn></msup></mrow><mrow><mo>&#x2202;</mo><mi>y</mi><msup><mo>&#x2202;</mo><mn>2</mn></msup><mi>z</mi><mo>&#x2202;</mo><mi>x</mi><mo>&#x2202;</mo><mi>z</mi><mo>&#x2202;</mo><mi>x</mi></mrow></mfrac><mrow><mi>f</mi><mfenced><mi>x</mi><mi>y</mi><mi>z</mi></mfenced></mrow></mrow>'
예제 #3
0
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>'
예제 #4
0
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>'
예제 #5
0
def test_mul_symbol_print():
    expr = x * y
    assert mathml(
        expr, printer='presentation'
    ) == '<mrow><mi>x</mi><mo>&InvisibleTimes;</mo><mi>y</mi></mrow>'
    assert mathml(
        expr, printer='presentation', mul_symbol=None
    ) == '<mrow><mi>x</mi><mo>&InvisibleTimes;</mo><mi>y</mi></mrow>'
    assert mathml(
        expr, printer='presentation',
        mul_symbol='dot') == '<mrow><mi>x</mi><mo>&#xB7;</mo><mi>y</mi></mrow>'
    assert mathml(expr, printer='presentation', mul_symbol='ldot'
                  ) == '<mrow><mi>x</mi><mo>&#x2024;</mo><mi>y</mi></mrow>'
    assert mathml(expr, printer='presentation', mul_symbol='times'
                  ) == '<mrow><mi>x</mi><mo>&#xD7;</mo><mi>y</mi></mrow>'
예제 #6
0
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
예제 #7
0
    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)
예제 #8
0
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>&#966;</cn>'

    mml = mathml(EulerGamma)
    assert mml == '<eulergamma/>'
예제 #9
0
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/>'
예제 #10
0
파일: test_mathml.py 프로젝트: latot/sympy
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>&#966;</cn>"

    mml = mathml(EulerGamma)
    assert mml == "<eulergamma/>"
예제 #11
0
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/>"
예제 #12
0
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))
예제 #13
0
파일: gtk.py 프로젝트: certik/sympy-oldcore
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)
예제 #14
0
파일: gtk.py 프로젝트: Visheshk/sympy
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)
예제 #15
0
파일: gtk.py 프로젝트: asmeurer/sympy
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)
예제 #16
0
def test_presentation_mathml_constants():
    mml = mpp._print(I)
    assert mml.childNodes[0].nodeValue == '&ImaginaryI;'

    mml = mpp._print(E)
    assert mml.childNodes[0].nodeValue == '&ExponentialE;'

    mml = mpp._print(oo)
    assert mml.childNodes[0].nodeValue == '&#x221E;'

    mml = mpp._print(pi)
    assert mml.childNodes[0].nodeValue == '&pi;'

    assert mathml(GoldenRatio, printer='presentation') == '<mi>&#966;</mi>'
예제 #17
0
def test_presentation_mathml_constants():
    mml = mpp._print(I)
    assert mml.childNodes[0].nodeValue == '&ImaginaryI;'

    mml = mpp._print(E)
    assert mml.childNodes[0].nodeValue == '&ExponentialE;'

    mml = mpp._print(oo)
    assert mml.childNodes[0].nodeValue == '&#x221E;'

    mml = mpp._print(pi)
    assert mml.childNodes[0].nodeValue == '&pi;'

    assert mathml(GoldenRatio, printer='presentation') == '<mi>&#966;</mi>'
예제 #18
0
파일: _expr.py 프로젝트: bjodah/chempy
 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)
예제 #19
0
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>&#x2202;</mo><mn>6</mn></msup></mrow><mrow><mo>&#x2202;</mo><mi>y</mi><msup><mo>&#x2202;</mo><mn>2</mn></msup><mi>z</mi><mo>&#x2202;</mo><mi>x</mi><mo>&#x2202;</mo><mi>z</mi><mo>&#x2202;</mo><mi>x</mi></mrow></mfrac><mrow><mi>f</mi><mfenced><mi>x</mi><mi>y</mi><mi>z</mi></mfenced></mrow></mrow>'
예제 #20
0
파일: test_mathml.py 프로젝트: cklb/sympy
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>'
예제 #21
0
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
예제 #22
0
def test_presentation_settings():
    raises(
        TypeError,
        lambda: mathml(Symbol("x"), printer='presentation', method="garbage"))
예제 #23
0
def test_content_settings():
    raises(TypeError, lambda: mathml(Symbol("x"), method="garbage"))
예제 #24
0
def test_presentation_settings():
    raises(TypeError, lambda: mathml(Symbol("x"), printer='presentation',method="garbage"))
예제 #25
0
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
예제 #26
0
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>'
예제 #27
0
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},
    )
예제 #28
0
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>'
예제 #29
0
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
예제 #30
0
def test_settings():
    raises(TypeError, lambda: mathml(Symbol("x"), method="garbage"))
예제 #31
0
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>'
예제 #32
0
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)

    # Частая ошибка -- знак умножения заменяется невидимым умножением &#x2062;, но потом интерпретируется как набор отдельных ASCII символов
    #for el in mml_tree.findall( '//{http://www.w3.org/1998/Math/MathML}mo' ):
    #    if el.text!= None : el.text = el.text.replace( "&#x2062;", 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("&#x2062;", '*')

    omml_formula = transform(mml_tree)
    math_docx[k] = omml_formula.getroot()
예제 #33
0
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>'