Esempio n. 1
0
def _test_to_pymbolic(mapper, sym, use_symengine):
    x, y = sym.symbols("x,y")

    assert mapper(sym.Rational(3, 4)) == prim.Quotient(3, 4)
    assert mapper(sym.Integer(6)) == 6

    if not use_symengine:
        assert mapper(sym.Subs(x**2, (x,), (y,))) == \
            prim.Substitution(x_**2, ("x",), (y_,))
        deriv = sym.Derivative(x**2, x)
        assert mapper(deriv) == prim.Derivative(x_**2, ("x", ))
    else:
        assert mapper(sym.Subs(x**2, (x,), (y,))) == \
            y_**2
        deriv = sym.Derivative(x**2, x)
        assert mapper(deriv) == 2 * x_

    # functions
    assert mapper(sym.Function("f")(x)) == prim.Variable("f")(x_)
    assert mapper(sym.exp(x)) == prim.Variable("exp")(x_)

    # indexed accesses
    if not use_symengine:
        i, j = sym.symbols("i,j")
        assert mapper(sym.Indexed(x, i, j)) == x_[i_, j_]

    # constants
    import math
    # FIXME: Why isn't this exact?
    assert abs(mapper(sym.pi) - math.pi) < 1e-14
    assert abs(mapper(sym.E) - math.e) < 1e-14
    assert mapper(sym.I) == 1j
Esempio n. 2
0
def test_latex_mapper():
    from pymbolic import parse
    from pymbolic.mapper.stringifier import LaTeXMapper, StringifyMapper

    tm = LaTeXMapper()
    sm = StringifyMapper()

    equations = []

    def add(expr):
        # Add an equation to the list of tests.
        equations.append(r"\[%s\] %% from: %s" % (tm(expr), sm(expr)))

    add(parse("a * b + c"))
    add(parse("f(a,b,c)"))
    add(parse("a ** b ** c"))
    add(parse("(a | b) ^ ~c"))
    add(parse("a << b"))
    add(parse("a >> b"))
    add(parse("a[i,j,k]"))
    add(parse("a[1:3]"))
    add(parse("a // b"))
    add(parse("not (a or b) and c"))
    add(parse("(a % b) % c"))
    add(parse("(a >= b) or (b <= c)"))
    add(prim.Min((1,)) + prim.Max((1, 2)))
    add(prim.Substitution(prim.Variable("x") ** 2, ("x",), (2,)))
    add(prim.Derivative(parse("x**2"), ("x",)))

    # Run LaTeX and ensure the file compiles.
    import os
    import tempfile
    import subprocess
    import shutil

    latex_dir = tempfile.mkdtemp("pymbolic")

    try:
        tex_file_path = os.path.join(latex_dir, "input.tex")

        with open(tex_file_path, "w") as tex_file:
            contents = LATEX_TEMPLATE % "\n".join(equations)
            tex_file.write(contents)

        try:
            subprocess.check_output(
                    ["latex",
                     "-interaction=nonstopmode",
                     "-output-directory=%s" % latex_dir,
                     tex_file_path],
                    universal_newlines=True)
        except OSError:  # FIXME: Should be FileNotFoundError on Py3
            pytest.skip("latex command not found")
        except subprocess.CalledProcessError as err:
            assert False, str(err.output)

    finally:
        shutil.rmtree(latex_dir)
Esempio n. 3
0
def _test_from_pymbolic(mapper, sym, use_symengine):
    x, y = sym.symbols("x,y")

    assert mapper(x_ + y_) == x + y
    assert mapper(x_ * y_) == x * y
    assert mapper(x_**2) == x**2

    assert mapper(prim.Substitution(x_**2, ("x",), (y_,))) == \
        sym.Subs(x**2, (x,), (y,))
    deriv = sym.Derivative(x**2, x)
    assert mapper(prim.Derivative(x_**2, ("x", ))) == deriv

    if use_symengine:
        assert mapper(x_[0]) == sym.Symbol("x_0")
    else:
        i, j = sym.symbols("i,j")
        assert mapper(x_[i_, j_]) == sym.Indexed(x, i, j)

    assert mapper(prim.Variable("f")(x_)) == sym.Function("f")(x)
Esempio n. 4
0
 def map_Subs(self, expr):
     return prim.Substitution(
         self.rec(expr.expr),
         tuple(v.name for v in expr.variables),
         tuple(self.rec(v) for v in expr.point),
     )