Example #1
0
def test_bind_C():
    if not has_fortran():
        skip("No fortran compiler found.")
    if not cython:
        skip("Cython not found.")
    if not np:
        skip("NumPy not found.")

    a = Symbol('a', real=True)
    s = Symbol('s', integer=True)
    body = [Return((sum_(a**2) / s)**.5)]
    arr = array(a, dim=[s], intent='in')
    fd = FunctionDefinition(real, 'rms', [arr, s], body, attrs=[bind_C('rms')])
    f_mod = render_as_module([fd], 'mod_rms')

    with TemporaryDirectory() as folder:
        mod, info = compile_link_import_strings(
            [('rms.f90', f_mod),
             ('_rms.pyx', ("#cython: language_level={}\n".format("3") +
                           "cdef extern double rms(double*, int*)\n"
                           "def py_rms(double[::1] x):\n"
                           "    cdef int s = x.size\n"
                           "    return rms(&x[0], &s)\n"))],
            build_dir=folder)
        assert abs(mod.py_rms(np.array([2., 4., 2., 2.])) - 7**0.5) < 1e-14
Example #2
0
def test_newtons_method_function__fcode():
    x = sp.Symbol("x", real=True)
    expr = sp.cos(x) - x**3
    func = newtons_method_function(expr, x, attrs=[bind_C(name="newton")])

    if not cython:
        skip("cython not installed.")
    if not has_fortran():
        skip("No Fortran compiler found.")

    f_mod = f_module([func], "mod_newton")
    with TemporaryDirectory() as folder:
        mod, info = compile_link_import_strings(
            [
                ("newton.f90", f_mod),
                (
                    "_newton.pyx",
                    ("#cython: language_level={}\n".format("3") +
                     "cdef extern double newton(double*)\n"
                     "def py_newton(double x):\n"
                     "    return newton(&x)\n"),
                ),
            ],
            build_dir=folder,
        )
        assert abs(mod.py_newton(0.5) - 0.865474033102) < 1e-12
Example #3
0
def test_newtons_method_function__ccode():
    x = sp.Symbol("x", real=True)
    expr = sp.cos(x) - x**3
    func = newtons_method_function(expr, x)

    if not cython:
        skip("cython not installed.")
    if not has_c():
        skip("No C compiler found.")

    compile_kw = dict(std="c99")
    with TemporaryDirectory() as folder:
        mod, info = compile_link_import_strings(
            [
                (
                    "newton.c",
                    ("#include <math.h>\n"
                     "#include <stdio.h>\n") + ccode(func),
                ),
                (
                    "_newton.pyx",
                    ("#cython: language_level={}\n".format("3") +
                     "cdef extern double newton(double)\n"
                     "def py_newton(x):\n"
                     "    return newton(x)\n"),
                ),
            ],
            build_dir=folder,
            compile_kwargs=compile_kw,
        )
        assert abs(mod.py_newton(0.5) - 0.865474033102) < 1e-12
Example #4
0
def test_newtons_method_function__ccode_parameters():
    args = x, A, k, p = sp.symbols("x A k p")
    expr = A * sp.cos(k * x) - p * x**3
    raises(ValueError, lambda: newtons_method_function(expr, x))
    use_wurlitzer = wurlitzer

    func = newtons_method_function(expr, x, args, debug=use_wurlitzer)

    if not has_c():
        skip("No C compiler found.")
    if not cython:
        skip("cython not installed.")

    compile_kw = dict(std="c99")
    with TemporaryDirectory() as folder:
        mod, info = compile_link_import_strings(
            [
                (
                    "newton_par.c",
                    ("#include <math.h>\n"
                     "#include <stdio.h>\n") + ccode(func),
                ),
                (
                    "_newton_par.pyx",
                    ("#cython: language_level={}\n".format("3") +
                     "cdef extern double newton(double, double, double, double)\n"
                     "def py_newton(x, A=1, k=1, p=1):\n"
                     "    return newton(x, A, k, p)\n"),
                ),
            ],
            compile_kwargs=compile_kw,
            build_dir=folder,
        )

        if use_wurlitzer:
            with wurlitzer.pipes() as (out, err):
                result = mod.py_newton(0.5)
        else:
            result = mod.py_newton(0.5)

        assert abs(result - 0.865474033102) < 1e-12

        if not use_wurlitzer:
            skip(
                "C-level output only tested when package 'wurlitzer' is available."
            )

        out, err = out.read(), err.read()
        assert err == ""
        assert (out == """\
x=         0.5 d_x=     0.61214
x=      1.1121 d_x=    -0.20247
x=     0.90967 d_x=   -0.042409
x=     0.86726 d_x=  -0.0017867
x=     0.86548 d_x= -3.1022e-06
x=     0.86547 d_x= -9.3421e-12
x=     0.86547 d_x=  3.6902e-17
""")  # try to run tests with LC_ALL=C if this assertion fails
def test_copying_function():
    if not np:
        skip("numpy not installed.")
    if not has_c():
        skip("No C compiler found.")
    if not cython:
        skip("Cython not found.")

    info = None
    with TemporaryDirectory() as folder:
        mod, info = _render_compile_import(_mk_func1(), build_dir=folder)
        inp = np.arange(10.0)
        out = np.empty_like(inp)
        mod._our_test_function(inp, out)
        assert np.allclose(inp, out)
Example #6
0
def test_newtons_method_function__fcode():
    x = sp.Symbol('x', real=True)
    expr = sp.cos(x) - x**3
    func = newtons_method_function(expr, x, attrs=[bind_C(name='newton')])

    if not cython:
        skip("cython not installed.")
    if not has_fortran():
        skip("No Fortran compiler found.")

    f_mod = f_module([func], 'mod_newton')
    with TemporaryDirectory() as folder:
        mod, info = compile_link_import_strings(
            [('newton.f90', f_mod),
             ('_newton.pyx', ("cdef extern double newton(double*)\n"
                              "def py_newton(double x):\n"
                              "    return newton(&x)\n"))],
            build_dir=folder)
        assert abs(mod.py_newton(0.5) - 0.865474033102) < 1e-12
Example #7
0
def test_newtons_method_function__ccode_parameters():
    args = x, A, k, p = sp.symbols('x A k p')
    expr = A * sp.cos(k * x) - p * x**3
    raises(ValueError, lambda: newtons_method_function(expr, x))
    use_wurlitzer = wurlitzer

    func = newtons_method_function(expr, x, args, debug=use_wurlitzer)

    if not has_c():
        skip("No C compiler found.")
    if not cython:
        skip("cython not installed.")

    compile_kw = dict(std='c99')
    with TemporaryDirectory() as folder:
        mod, info = compile_link_import_strings(
            [('newton_par.c', ('#include <math.h>\n'
                               '#include <stdio.h>\n') + ccode(func)),
             ('_newton_par.pyx',
              ("#cython: language_level={}\n".format("3" if PY3 else "2") +
               "cdef extern double newton(double, double, double, double)\n"
               "def py_newton(x, A=1, k=1, p=1):\n"
               "    return newton(x, A, k, p)\n"))],
            compile_kwargs=compile_kw,
            build_dir=folder)

        if use_wurlitzer:
            with wurlitzer.pipes() as (out, err):
                result = mod.py_newton(0.5)
        else:
            result = mod.py_newton(0.5)

        assert abs(result - 0.865474033102) < 1e-12

        if not use_wurlitzer:
            skip(
                "C-level output only tested when package 'wurlitzer' is available."
            )

        out, err = out.read(), err.read()
        assert err == ''
        assert out == """\
Example #8
0
def test_newtons_method_function__ccode():
    x = sp.Symbol('x', real=True)
    expr = sp.cos(x) - x**3
    func = newtons_method_function(expr, x)

    if not cython:
        skip("cython not installed.")
    if not has_c():
        skip("No C compiler found.")

    compile_kw = dict(std='c99')
    with TemporaryDirectory() as folder:
        mod, info = compile_link_import_strings(
            [('newton.c', ('#include <math.h>\n'
                           '#include <stdio.h>\n') + ccode(func)),
             ('_newton.pyx', ("cdef extern double newton(double)\n"
                              "def py_newton(x):\n"
                              "    return newton(x)\n"))],
            build_dir=folder,
            compile_kwargs=compile_kw)
        assert abs(mod.py_newton(0.5) - 0.865474033102) < 1e-12