本文整理汇总了Python中numba.numpy_support.as_dtype函数的典型用法代码示例。如果您正苦于以下问题:Python as_dtype函数的具体用法?Python as_dtype怎么用?Python as_dtype使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了as_dtype函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _build_element_wise_ufunc_wrapper
def _build_element_wise_ufunc_wrapper(cres, signature):
'''Build a wrapper for the ufunc loop entry point given by the
compilation result object, using the element-wise signature.
'''
ctx = cres.target_context
library = cres.library
llvm_func = library.get_function(cres.fndesc.llvm_func_name)
env = None
if cres.objectmode:
# Get env
env = cres.environment
assert env is not None
ll_intp = cres.target_context.get_value_type(types.intp)
ll_pyobj = cres.target_context.get_value_type(types.pyobject)
envptr = lc.Constant.int(ll_intp, id(env)).inttoptr(ll_pyobj)
else:
envptr = None
wrapper = build_ufunc_wrapper(library, ctx, llvm_func, signature,
cres.objectmode, envptr, env)
ptr = library.get_pointer_to_function(wrapper.name)
# Get dtypes
dtypenums = [as_dtype(a).num for a in signature.args]
dtypenums.append(as_dtype(signature.return_type).num)
return dtypenums, ptr, env
示例2: build
def build(self, cres, signature):
# Buider wrapper for ufunc entry point
ctx = cres.target_context
library = cres.library
llvm_func = library.get_function(cres.fndesc.llvm_func_name)
env = None
if cres.objectmode:
# Get env
env = cres.environment
assert env is not None
ll_intp = cres.target_context.get_value_type(types.intp)
ll_pyobj = cres.target_context.get_value_type(types.pyobject)
envptr = lc.Constant.int(ll_intp, id(env)).inttoptr(ll_pyobj)
else:
envptr = None
wrapper = build_ufunc_wrapper(library, ctx, llvm_func, signature,
cres.objectmode, envptr)
ptr = library.get_pointer_to_function(wrapper.name)
# Get dtypes
dtypenums = [as_dtype(a).num for a in signature.args]
dtypenums.append(as_dtype(signature.return_type).num)
return dtypenums, ptr, env
示例3: kernel_wrapper
def kernel_wrapper(values):
n = len(values)
inputs = [np.empty(n, dtype=numpy_support.as_dtype(tp))
for tp in argtypes]
output = np.empty(n, dtype=numpy_support.as_dtype(restype))
for i, vs in enumerate(values):
for v, inp in zip(vs, inputs):
inp[i] = v
args = [output] + inputs
kernel[int(math.ceil(n / 256)), 256](*args)
return list(output)
示例4: check_round
def check_round(cfunc, values, inty, outty, decimals):
# Create input and output arrays of the right type
arr = values.astype(as_dtype(inty))
out = np.zeros_like(arr).astype(as_dtype(outty))
pyout = out.copy()
_fixed_np_round(arr, decimals, pyout)
cfunc(arr, decimals, out)
np.testing.assert_allclose(out, pyout)
# Output shape mismatch
with self.assertRaises(ValueError) as raises:
cfunc(arr, decimals, out[1:])
self.assertEqual(str(raises.exception),
"invalid output shape")
示例5: _build_element_wise_ufunc_wrapper
def _build_element_wise_ufunc_wrapper(cres, signature):
'''Build a wrapper for the ufunc loop entry point given by the
compilation result object, using the element-wise signature.
'''
ctx = cres.target_context
library = cres.library
fname = cres.fndesc.llvm_func_name
with global_compiler_lock:
ptr = build_ufunc_wrapper(library, ctx, fname, signature,
cres.objectmode, cres)
# Get dtypes
dtypenums = [as_dtype(a).num for a in signature.args]
dtypenums.append(as_dtype(signature.return_type).num)
return dtypenums, ptr, cres.environment
示例6: test_hypot
def test_hypot(self, flags=enable_pyobj_flags):
pyfunc = hypot
x_types = [types.int64, types.uint64,
types.float32, types.float64]
x_values = [1, 2, 3, 4, 5, 6, .21, .34]
y_values = [x + 2 for x in x_values]
# Issue #563: precision issues with math.hypot() under Windows.
prec = 'single' if sys.platform == 'win32' else 'exact'
self.run_binary(pyfunc, x_types, x_values, y_values, flags, prec)
# Check that values that overflow in naive implementations do not
# in the numba impl
def naive_hypot(x, y):
return math.sqrt(x * x + y * y)
for fltty in (types.float32, types.float64):
cr = self.ccache.compile(pyfunc, (fltty, fltty), flags=flags)
cfunc = cr.entry_point
dt = numpy_support.as_dtype(fltty).type
val = dt(np.finfo(dt).max / 30.)
nb_ans = cfunc(val, val)
self.assertPreciseEqual(nb_ans, pyfunc(val, val), prec='single')
self.assertTrue(np.isfinite(nb_ans))
with warnings.catch_warnings():
warnings.simplefilter("error", RuntimeWarning)
self.assertRaisesRegexp(RuntimeWarning,
'overflow encountered in .*_scalars',
naive_hypot, val, val)
示例7: build
def build(self, cres):
"""
Returns (dtype numbers, function ptr, EnvironmentObject)
"""
_launch_threads()
_init()
# Build wrapper for ufunc entry point
ctx = cres.target_context
library = cres.library
signature = cres.signature
ptr, env = build_gufunc_wrapper(library, ctx, signature, self.sin,
self.sout, fndesc=cres.fndesc,
env=cres.environment)
# Get dtypes
dtypenums = []
for a in signature.args:
if isinstance(a, types.Array):
ty = a.dtype
else:
ty = a
dtypenums.append(as_dtype(ty).num)
return dtypenums, ptr, env
示例8: build
def build(self, cres):
"""
Returns (dtype numbers, function ptr, EnvironmentObject)
"""
# Buider wrapper for ufunc entry point
ctx = cres.target_context
library = cres.library
signature = cres.signature
llvm_func = library.get_function(cres.fndesc.llvm_func_name)
wrapper, env = build_gufunc_wrapper(library, ctx, llvm_func,
signature, self.sin, self.sout,
fndesc=cres.fndesc,
env=cres.environment)
ptr = library.get_pointer_to_function(wrapper.name)
# Get dtypes
dtypenums = []
for a in signature.args:
if isinstance(a, types.Array):
ty = a.dtype
else:
ty = a
dtypenums.append(as_dtype(ty).num)
return dtypenums, ptr, env
示例9: test_record_dtype_with_titles_roundtrip
def test_record_dtype_with_titles_roundtrip(self):
recdtype = np.dtype([(("title a", 'a'), np.float), ('b', np.float)])
nbtype = numpy_support.from_dtype(recdtype)
self.assertTrue(nbtype.is_title('title a'))
self.assertFalse(nbtype.is_title('a'))
self.assertFalse(nbtype.is_title('b'))
got = numpy_support.as_dtype(nbtype)
self.assertTrue(got, recdtype)
示例10: array_cumprod
def array_cumprod(context, builder, sig, args):
scalar_dtype = sig.return_type.dtype
dtype = as_dtype(scalar_dtype)
def array_cumprod_impl(arr):
size = 1
for i in arr.shape:
size = size * i
out = numpy.empty(size, dtype)
c = 1
for idx, v in enumerate(arr.flat):
c *= v
out[idx] = c
return out
res = context.compile_internal(builder, array_cumprod_impl, sig, args, locals=dict(c=scalar_dtype))
return impl_ret_new_ref(context, builder, sig.return_type, res)
示例11: roots_impl
def roots_impl(p):
# cast int vectors to float cf. numpy, this is a bit dicey as
# the roots could be complex which will fail anyway
ty = getattr(p, 'dtype', p)
if isinstance(ty, types.Integer):
cast_t = np.float64
else:
cast_t = np_support.as_dtype(ty)
def roots_impl(p):
# impl based on numpy:
# https://github.com/numpy/numpy/blob/master/numpy/lib/polynomial.py
if len(p.shape) != 1:
raise ValueError("Input must be a 1d array.")
non_zero = np.nonzero(p)[0]
if len(non_zero) == 0:
return np.zeros(0, dtype=cast_t)
tz = len(p) - non_zero[-1] - 1
# pull out the coeffs selecting between possible zero pads
p = p[int(non_zero[0]):int(non_zero[-1]) + 1]
n = len(p)
if n > 1:
# construct companion matrix, ensure fortran order
# to give to eigvals, write to upper diag and then
# transpose.
A = np.diag(np.ones((n - 2,), cast_t), 1).T
A[0, :] = -p[1:] / p[0] # normalize
roots = np.linalg.eigvals(A)
else:
roots = np.zeros(0, dtype=cast_t)
# add in additional zeros on the end if needed
if tz > 0:
return np.hstack((roots, np.zeros(tz, dtype=cast_t)))
else:
return roots
return roots_impl
示例12: build
def build(self, cres):
"""
Returns (dtype numbers, function ptr, EnvironmentObject)
"""
# Buider wrapper for ufunc entry point
signature = cres.signature
ptr, env = build_gufunc_wrapper(self.py_func, cres, self.sin, self.sout,
cache=self.cache)
# Get dtypes
dtypenums = []
for a in signature.args:
if isinstance(a, types.Array):
ty = a.dtype
else:
ty = a
dtypenums.append(as_dtype(ty).num)
return dtypenums, ptr, env
示例13: array
def array(self, shape, dtype):
dtype = numpy_support.as_dtype(dtype)
# Dynamic shared memory is requested with size 0 - this all shares the
# same underlying memory
if shape == 0:
# Count must be the maximum number of whole elements that fit in the
# buffer (Numpy complains if the buffer is not a multiple of the
# element size)
count = self._dynshared_size // dtype.itemsize
return np.frombuffer(self._dynshared.data, dtype=dtype, count=count)
# Otherwise, identify allocations by source file and line number
caller = traceback.extract_stack()[-2][0:2]
res = self._allocations.get(caller)
if res is None:
res = np.empty(shape, dtype)
self._allocations[caller] = res
return res
示例14: test_pickling_vectorize
def test_pickling_vectorize(self):
@vectorize(['intp(intp)', 'float64(float64)'], target='cuda')
def cuda_vect(x):
return x * 2
# accommodate int representations in np.arange
npty = numpy_support.as_dtype(types.intp)
# get expected result
ary = np.arange(10, dtype=npty)
expected = cuda_vect(ary)
# first pickle
foo1 = pickle.loads(pickle.dumps(cuda_vect))
del cuda_vect
got1 = foo1(ary)
np.testing.assert_equal(expected, got1)
# second pickle
foo2 = pickle.loads(pickle.dumps(foo1))
del foo1
got2 = foo2(ary)
np.testing.assert_equal(expected, got2)
示例15: array
def array(self, shape, dtype):
dtype = numpy_support.as_dtype(dtype)
return np.empty(shape, dtype)