Esempio n. 1
0
def runtest_autowrap_twice(language, backend):
    f = autowrap((((a + b) / c)**5).expand(), language, backend)
    g = autowrap((((a + b) / c)**4).expand(), language, backend)

    # check that autowrap updates the module name.  Else, g gives the same as f
    assert f(1, -2, 1) == -1.0
    assert g(1, -2, 1) == 1.0
Esempio n. 2
0
def runtest_autowrap_twice(language, backend):
    f = autowrap((((a + b)/c)**5).expand(), language, backend)
    g = autowrap((((a + b)/c)**4).expand(), language, backend)

    # check that autowrap updates the module name.  Else, g gives the same as f
    assert f(1, -2, 1) == -1.0
    assert g(1, -2, 1) == 1.0
Esempio n. 3
0
def test_autowrap_dummy():
    # Uses DummyWrapper to test that codegen works as expected

    f = autowrap(x + y, backend='dummy')
    assert f() == str(x + y)
    assert f.args == "x, y"
    assert f.returns == "nameless"
    f = autowrap(Eq(z, x + y), backend='dummy')
    assert f() == str(x + y)
    assert f.args == "x, y"
    assert f.returns == "z"
    f = autowrap(Eq(z, x + y + z), backend='dummy')
    assert f() == str(x + y + z)
    assert f.args == "x, y, z"
    assert f.returns == "z"
Esempio n. 4
0
def test_autowrap_dummy():
    # Uses DummyWrapper to test that codegen works as expected

    f = autowrap(x + y, backend='dummy')
    assert f() == str(x + y)
    assert f.args == "x, y"
    assert f.returns == "nameless"
    f = autowrap(Eq(z, x + y), backend='dummy')
    assert f() == str(x + y)
    assert f.args == "x, y"
    assert f.returns == "z"
    f = autowrap(Eq(z, x + y + z), backend='dummy')
    assert f() == str(x + y + z)
    assert f.args == "x, y, z"
    assert f.returns == "z"
Esempio n. 5
0
def runtest_autowrap_helpers(language, backend):
    # issue sympy/sympy#10274
    expr = (a - b + c)**13
    tmp = tempfile.mkdtemp()
    f = autowrap(expr, language, backend, tempdir=tmp, helpers=[('helper', a - b + c, (a, b, c))])
    assert f(1, 1, 1) == 1

    for file in os.listdir(tmp):
        if file.startswith('wrapped_code_') and file.endswith('.c'):
            with open(tmp + '/' + file, encoding='utf-8') as f:
                assert f.read() == ('/******************************************************************************\n'
                                    ' *' + ('Code generated with diofant ' + diofant.__version__).center(76) + '*\n'
                                    ' *                                                                            *\n'
                                    ' *         See https://diofant.readthedocs.io/ for more information.          *\n'
                                    ' *                                                                            *\n'
                                    " *                      This file is part of 'autowrap'                       *\n"
                                    ' ******************************************************************************/\n'
                                    '#include ' + '"' + file[:-1] + 'h"' + '\n'
                                    '#include <math.h>\n'
                                    '\n'
                                    'double helper(double a, double b, double c) {\n'
                                    '\n'
                                    '   double helper_result;\n'
                                    '   helper_result = a - b + c;\n'
                                    '   return helper_result;\n'
                                    '\n'
                                    '}\n'
                                    '\n'
                                    'double autofunc(double a, double b, double c) {\n'
                                    '\n'
                                    '   double autofunc_result;\n'
                                    '   autofunc_result = pow(helper(a, b, c), 13);\n'
                                    '   return autofunc_result;\n'
                                    '\n'
                                    '}\n')
Esempio n. 6
0
def runtest_sympyissue_10274(language, backend):
    expr = (a - b + c)**13
    tmp = tempfile.mkdtemp()
    f = autowrap(expr, language, backend, tempdir=tmp, helpers=('helper', a - b + c, (a, b, c)))
    assert f(1, 1, 1) == 1

    for file in os.listdir(tmp):
        if file.startswith("wrapped_code_") and file.endswith(".c"):
            fil = open(tmp + '/' + file)
            assert fil.read() == ("/******************************************************************************\n"
                                  " *" + ("Code generated with diofant " + diofant.__version__).center(76) + "*\n"
                                  " *                                                                            *\n"
                                  " *         See https://diofant.readthedocs.io/ for more information.          *\n"
                                  " *                                                                            *\n"
                                  " *                      This file is part of 'autowrap'                       *\n"
                                  " ******************************************************************************/\n"
                                  "#include " + '"' + file[:-1] + 'h"' + "\n"
                                  "#include <math.h>\n"
                                  "\n"
                                  "double helper(double a, double b, double c) {\n"
                                  "\n"
                                  "   double helper_result;\n"
                                  "   helper_result = a - b + c;\n"
                                  "   return helper_result;\n"
                                  "\n"
                                  "}\n"
                                  "\n"
                                  "double autofunc(double a, double b, double c) {\n"
                                  "\n"
                                  "   double autofunc_result;\n"
                                  "   autofunc_result = pow(helper(a, b, c), 13);\n"
                                  "   return autofunc_result;\n"
                                  "\n"
                                  "}\n")
Esempio n. 7
0
def test_autowrap_args():
    x, y, z = symbols('x y z')

    pytest.raises(CodeGenArgumentListError,
                  lambda: autowrap(Eq(z, x + y), backend='dummy', args=(x,)))
    f = autowrap(Eq(z, x + y), backend='dummy', args=(y, x))
    assert f() == str(x + y)
    assert f.args == "y, x"
    assert f.returns == "z"

    pytest.raises(CodeGenArgumentListError,
                  lambda: autowrap(Eq(z, x + y + z), backend='dummy', args=(x, y)))
    f = autowrap(Eq(z, x + y + z), backend='dummy', args=(y, x, z))
    assert f() == str(x + y + z)
    assert f.args == "y, x, z"
    assert f.returns == "z"
Esempio n. 8
0
def test_autowrap_store_files():
    tmp = tempfile.mkdtemp()
    try:
        f = autowrap(x + y, backend='dummy', tempdir=tmp)
        assert f() == str(x + y)
        assert os.access(tmp, os.F_OK)
    finally:
        shutil.rmtree(tmp)
Esempio n. 9
0
def test_autowrap_store_files():
    tmp = tempfile.mkdtemp()
    try:
        f = autowrap(x + y, backend='dummy', tempdir=tmp)
        assert f() == str(x + y)
        assert os.access(tmp, os.F_OK)
    finally:
        shutil.rmtree(tmp)
Esempio n. 10
0
def runtest_autowrap_matrix_matrix(language, backend):
    expr = Eq(C[i, j], A[i, k]*B[k, j])
    matmat = autowrap(expr, language, backend)

    # compare with numpy's dot product
    M1 = numpy.random.rand(10, 20)
    M2 = numpy.random.rand(20, 15)
    M3 = numpy.dot(M1, M2)
    assert numpy.sum(numpy.abs(M3 - matmat(M1, M2))) < 1e-13
Esempio n. 11
0
def runtest_autowrap_matrix_matrix(language, backend):
    expr = Eq(C[i, j], A[i, k] * B[k, j])
    matmat = autowrap(expr, language, backend)

    # compare with numpy's dot product
    M1 = numpy.random.rand(10, 20)
    M2 = numpy.random.rand(20, 15)
    M3 = numpy.dot(M1, M2)
    assert numpy.sum(numpy.abs(M3 - matmat(M1, M2))) < 1e-13
Esempio n. 12
0
def test_autowrap_args():
    pytest.raises(CodeGenArgumentListError,
                  lambda: autowrap(Eq(z, x + y), backend='dummy', args=(x,)))
    f = autowrap(Eq(z, x + y), backend='dummy', args=(y, x))
    assert f() == str(x + y)
    assert f.args == "y, x"
    assert f.returns == "z"

    pytest.raises(CodeGenArgumentListError,
                  lambda: autowrap(Eq(z, x + y + z), backend='dummy', args=(x, y)))
    f = autowrap(Eq(z, x + y + z), backend='dummy', args=(y, x, z))
    assert f() == str(x + y + z)
    assert f.args == "y, x, z"
    assert f.returns == "z"

    f = autowrap(Eq(z, x + y + z), backend='dummy', args=(y, x, z))
    assert f() == str(x + y + z)
    assert f.args == "y, x, z"
    assert f.returns == "z"
Esempio n. 13
0
def runtest_autowrap_matrix_vector(language, backend):
    x, y = symbols('x y', cls=IndexedBase)
    expr = Eq(y[i], A[i, j]*x[j])
    mv = autowrap(expr, language, backend)

    # compare with numpy's dot product
    M = numpy.random.rand(10, 20)
    x = numpy.random.rand(20)
    y = numpy.dot(M, x)
    assert numpy.sum(numpy.abs(y - mv(M, x))) < 1e-13
Esempio n. 14
0
def test_autowrap_args():
    pytest.raises(CodeGenArgumentListError,
                  lambda: autowrap(Eq(z, x + y), backend='dummy', args=(x,)))
    f = autowrap(Eq(z, x + y), backend='dummy', args=(y, x))
    assert f() == str(x + y)
    assert f.args == 'y, x'
    assert f.returns == 'z'

    pytest.raises(CodeGenArgumentListError,
                  lambda: autowrap(Eq(z, x + y + z), backend='dummy', args=(x, y)))
    f = autowrap(Eq(z, x + y + z), backend='dummy', args=(y, x, z))
    assert f() == str(x + y + z)
    assert f.args == 'y, x, z'
    assert f.returns == 'z'

    f = autowrap(Eq(z, x + y + z), backend='dummy', args=(y, x, z))
    assert f() == str(x + y + z)
    assert f.args == 'y, x, z'
    assert f.returns == 'z'
Esempio n. 15
0
def runtest_autowrap_matrix_vector(language, backend):
    x, y = symbols('x y', cls=IndexedBase)
    expr = Eq(y[i], A[i, j] * x[j])
    mv = autowrap(expr, language, backend)

    # compare with numpy's dot product
    M = numpy.random.rand(10, 20)
    x = numpy.random.rand(20)
    y = numpy.dot(M, x)
    assert numpy.sum(numpy.abs(y - mv(M, x))) < 1e-13
Esempio n. 16
0
def runtest_sympyissue_10274(language, backend):
    expr = (a - b + c)**13
    tmp = tempfile.mkdtemp()
    f = autowrap(expr,
                 language,
                 backend,
                 tempdir=tmp,
                 helpers=('helper', a - b + c, (a, b, c)))
    assert f(1, 1, 1) == 1

    for file in os.listdir(tmp):
        if file.startswith("wrapped_code_") and file.endswith(".c"):
            fil = open(tmp + '/' + file)
            assert fil.read() == (
                "/******************************************************************************\n"
                " *" + ("Code generated with diofant " +
                        diofant.__version__).center(76) + "*\n"
                " *                                                                            *\n"
                " *         See https://diofant.readthedocs.io/ for more information.          *\n"
                " *                                                                            *\n"
                " *                      This file is part of 'autowrap'                       *\n"
                " ******************************************************************************/\n"
                "#include " + '"' + file[:-1] + 'h"' + "\n"
                "#include <math.h>\n"
                "\n"
                "double helper(double a, double b, double c) {\n"
                "\n"
                "   double helper_result;\n"
                "   helper_result = a - b + c;\n"
                "   return helper_result;\n"
                "\n"
                "}\n"
                "\n"
                "double autofunc(double a, double b, double c) {\n"
                "\n"
                "   double autofunc_result;\n"
                "   autofunc_result = pow(helper(a, b, c), 13);\n"
                "   return autofunc_result;\n"
                "\n"
                "}\n")
Esempio n. 17
0
def test_autowrap_dummy():
    # Uses DummyWrapper to test that codegen works as expected

    tempdir = tempfile.mkstemp()[-1]
    os.unlink(tempdir)
    f = autowrap(x + y, backend='dummy', tempdir=tempdir)
    assert f() == str(x + y)
    assert f.args == 'x, y'
    assert f.returns == 'nameless'
    f = autowrap(Eq(z, x + y), backend='dummy')
    assert f() == str(x + y)
    assert f.args == 'x, y'
    assert f.returns == 'z'
    f = autowrap(Eq(z, x + y + z), backend='dummy')
    assert f() == str(x + y + z)
    assert f.args == 'x, y, z'
    assert f.returns == 'z'

    e = x + y

    pytest.raises(ValueError, lambda: autowrap(e, backend='spam'))
    pytest.raises(ValueError, lambda: autowrap(e, backend='spam', language='C'))
    pytest.raises(ValueError, lambda: autowrap(e, language='spam'))
Esempio n. 18
0
from diofant import Eq, symbols
from diofant.external import import_module
from diofant.tensor import Idx, IndexedBase
from diofant.utilities.autowrap import CodeWrapError, autowrap, ufuncify


__all__ = ()

numpy = import_module('numpy', min_module_version='1.6.1')
Cython = import_module('Cython', min_module_version='0.15.1')
f2py = import_module('numpy.f2py', __import__kwargs={'fromlist': ['f2py']})

f2pyworks = False
if f2py:
    try:
        autowrap(symbols('x'), 'f95', 'f2py')
    except (CodeWrapError, ImportError, OSError):
        f2pyworks = False
    else:
        f2pyworks = True

a, b, c = symbols('a b c')
n, m, d = symbols('n m d', integer=True)
A, B, C = symbols('A B C', cls=IndexedBase)
i = Idx('i', m)
j = Idx('j', n)
k = Idx('k', d)


#
# test runners used by several language-backend combinations
Esempio n. 19
0
def runtest_autowrap_trace(language, backend):
    trace = autowrap(A[i, i], language, backend)
    assert trace(numpy.eye(100)) == 100
Esempio n. 20
0
def runtest_autowrap_trace(language, backend):
    trace = autowrap(A[i, i], language, backend)
    assert trace(numpy.eye(100)) == 100
Esempio n. 21
0
__all__ = ()

numpy = import_module('numpy', min_module_version='1.6.1')
with_numpy = pytest.mark.skipif(numpy is None, reason="Couldn't import numpy.")

Cython = import_module('Cython', min_module_version='0.15.1')
with_cython = pytest.mark.skipif(Cython is None,
                                 reason="Couldn't import Cython.")

f2py = import_module('numpy.f2py', __import__kwargs={'fromlist': ['f2py']})
with_f2py = pytest.mark.skipif(f2py is None, reason="Couldn't run f2py.")

f2pyworks = False
if f2py:
    try:
        autowrap(symbols('x'), 'f95', 'f2py')
    except (CodeWrapError, ImportError, OSError):
        f2pyworks = False
    else:
        f2pyworks = True

a, b, c = symbols('a b c')
n, m, d = symbols('n m d', integer=True)
A, B, C = symbols('A B C', cls=IndexedBase)
i = Idx('i', m)
j = Idx('j', n)
k = Idx('k', d)

#
# test runners used by several language-backend combinations
#
Esempio n. 22
0
def test_autowrap_command_err(monkeypatch):
    monkeypatch.setattr(F2PyCodeWrapper, 'command', ['/bin/false'])
    pytest.raises(CodeWrapError, lambda: autowrap(1, backend='f2py'))
Esempio n. 23
0
def test_autowrap_verbose(capsys):
    f = autowrap((((a + b) / c)**5).expand(), backend='f2py', verbose=True)

    assert capsys.readouterr().out.find('running build') == 2
    assert f(1, -2, 1) == -1.0