示例#1
0
 def test_ctypes_sine(self):
     quad(LowLevelCallable(sine_ctypes), 0, 1)
示例#2
0
import numpy as np
from numpy.testing import assert_allclose

from scipy import ndimage
from scipy.ndimage import _ctest
from scipy.ndimage import _ctest_oldapi
from scipy.ndimage import _cytest
from scipy._lib._ccallback import LowLevelCallable

FILTER1D_FUNCTIONS = [
    lambda filter_size: _ctest.filter1d(filter_size),
    lambda filter_size: _ctest_oldapi.filter1d(filter_size),
    lambda filter_size: _cytest.filter1d(filter_size, with_signature=False),
    lambda filter_size: LowLevelCallable(_cytest.filter1d(filter_size, with_signature=True)),
    lambda filter_size: LowLevelCallable.from_cython(_cytest, "_filter1d",
                                                     _cytest.filter1d_capsule(filter_size)),
]

FILTER2D_FUNCTIONS = [
    lambda weights: _ctest.filter2d(weights),
    lambda weights: _ctest_oldapi.filter2d(weights),
    lambda weights: _cytest.filter2d(weights, with_signature=False),
    lambda weights: LowLevelCallable(_cytest.filter2d(weights, with_signature=True)),
    lambda weights: LowLevelCallable.from_cython(_cytest, "_filter2d", _cytest.filter2d_capsule(weights)),
]

TRANSFORM_FUNCTIONS = [
    lambda shift: _ctest.transform(shift),
    lambda shift: _ctest_oldapi.transform(shift),
    lambda shift: _cytest.transform(shift, with_signature=False),
    lambda shift: LowLevelCallable(_cytest.transform(shift, with_signature=True)),
CALLERS = {
    'simple': _test_ccallback.test_call_simple,
    'nodata': _test_ccallback.test_call_nodata,
    'nonlocal': _test_ccallback.test_call_nonlocal,
    'cython': _test_ccallback_cython.test_call_cython,
}

# These functions have signatures known to the callers
FUNCS = {
    'python':
    lambda: callback_python,
    'capsule':
    lambda: _test_ccallback.test_get_plus1_capsule(),
    'cython':
    lambda: LowLevelCallable.from_cython(_test_ccallback_cython, "plus1_cython"
                                         ),
    'ctypes':
    lambda: _test_ccallback_cython.plus1_ctypes,
    'cffi':
    lambda: _get_cffi_func(_test_ccallback_cython.plus1_ctypes,
                           'double (*)(double, int *, void *)'),
    'capsule_b':
    lambda: _test_ccallback.test_get_plus1b_capsule(),
    'cython_b':
    lambda: LowLevelCallable.from_cython(_test_ccallback_cython,
                                         "plus1b_cython"),
    'ctypes_b':
    lambda: _test_ccallback_cython.plus1b_ctypes,
    'cffi_b':
    lambda: _get_cffi_func(_test_ccallback_cython.plus1b_ctypes,
                           'double (*)(double, double, int *, void *)'),
示例#4
0
a, b = 0.01, 100


@contextlib.contextmanager
def timing():
    s = time.time()
    yield
    print('Time: {}s'.format(time.time() - s))


def func(x):
    return sin(1 / x)


optimized_func = cfunc("float64(float64)")(func)

print('Optimized version')
with timing():
    out1 = quad(LowLevelCallable(optimized_func.ctypes), a, b)
    print(out1)

print('Optimized version')
with timing():
    out1 = quad(LowLevelCallable(optimized_func.ctypes), a, b)
    print(out1)

print('Standard version')
with timing():
    out2 = quad(func, a, b)
    print(out2)
示例#5
0
    ffi = cffi.FFI()
    return ffi.new('double *', 2.0)


CALLERS = {
    'simple': _test_ccallback.test_call_simple,
    'nodata': _test_ccallback.test_call_nodata,
    'nonlocal': _test_ccallback.test_call_nonlocal,
    'cython': _test_ccallback_cython.test_call_cython,
}

# These functions have signatures known to the callers
FUNCS = {
    'python': lambda: callback_python,
    'capsule': lambda: _test_ccallback.test_get_plus1_capsule(),
    'cython': lambda: LowLevelCallable.from_cython(_test_ccallback_cython, "plus1_cython"),
    'ctypes': lambda: _test_ccallback_cython.plus1_ctypes,
    'cffi': lambda: _get_cffi_func(_test_ccallback_cython.plus1_ctypes,
                                   'double (*)(double, int *, void *)'),
    'capsule_b': lambda: _test_ccallback.test_get_plus1b_capsule(),
    'cython_b': lambda: LowLevelCallable.from_cython(_test_ccallback_cython, "plus1b_cython"),
    'ctypes_b': lambda: _test_ccallback_cython.plus1b_ctypes,
    'cffi_b': lambda: _get_cffi_func(_test_ccallback_cython.plus1b_ctypes,
                                     'double (*)(double, double, int *, void *)'),
}

# These functions have signatures the callers don't know
BAD_FUNCS = {
    'capsule_bc': lambda: _test_ccallback.test_get_plus1bc_capsule(),
    'cython_bc': lambda: LowLevelCallable.from_cython(_test_ccallback_cython, "plus1bc_cython"),
    'ctypes_bc': lambda: _test_ccallback_cython.plus1bc_ctypes,
示例#6
0
from __future__ import division, print_function, absolute_import

import numpy as np
from numpy.testing import assert_allclose
from scipy import ndimage
from scipy._lib._ccallback import LowLevelCallable
from scipy.ndimage import _ctest
from scipy.ndimage import _ctest_oldapi
from scipy.ndimage import _cytest

FILTER1D_FUNCTIONS = [
    lambda filter_size: _ctest.filter1d(filter_size),
    lambda filter_size: _ctest_oldapi.filter1d(filter_size),
    lambda filter_size: _cytest.filter1d(filter_size, with_signature=False),
    lambda filter_size: LowLevelCallable(_cytest.filter1d(filter_size, with_signature=True)),
    lambda filter_size: LowLevelCallable.from_cython(_cytest, "_filter1d",
                                                     _cytest.filter1d_capsule(filter_size)),
]

FILTER2D_FUNCTIONS = [
    lambda weights: _ctest.filter2d(weights),
    lambda weights: _ctest_oldapi.filter2d(weights),
    lambda weights: _cytest.filter2d(weights, with_signature=False),
    lambda weights: LowLevelCallable(_cytest.filter2d(weights, with_signature=True)),
    lambda weights: LowLevelCallable.from_cython(_cytest, "_filter2d", _cytest.filter2d_capsule(weights)),
]

TRANSFORM_FUNCTIONS = [
    lambda shift: _ctest.transform(shift),
    lambda shift: _ctest_oldapi.transform(shift),
    lambda shift: _cytest.transform(shift, with_signature=False),
示例#7
0
# -*- coding: utf-8 -*-
"""
Created on Mon May  3 18:18:10 2021

@author: swallan
"""

import srd
import ctypes
from scipy._lib._ccallback import LowLevelCallable
import numpy as np
import scipy.integrate as integrate

usr_data = np.array([4.577, 10, 10], float).ctypes.data_as(ctypes.c_void_p)
# usr_data = np.array([3.77, 3, 12], float).ctypes.data_as(ctypes.c_void_p)
llc = LowLevelCallable.from_cython(srd, "genstudentized_range_cdf", usr_data)


#%%
def cython_cdf(dps):
    atol = 10**-(dps)

    ranges = [(-np.inf, np.inf), (0, np.inf)]
    opts = dict(epsabs=atol)
    res = integrate.nquad(llc, ranges=ranges, opts=opts)[0]
    print(atol)
    print(res)
    return res
    # return integrate.dblquad(llc, -np.inf, np.inf, gfun=0, hfun=np.inf,
    #                                 epsabs=atol)