本文整理汇总了Python中pypy.module.micronumpy.descriptor.get_dtype_cache函数的典型用法代码示例。如果您正苦于以下问题:Python get_dtype_cache函数的具体用法?Python get_dtype_cache怎么用?Python get_dtype_cache使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get_dtype_cache函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: execute
def execute(self, interp):
w_lhs = self.lhs.execute(interp)
if isinstance(self.rhs, SliceConstant):
w_rhs = self.rhs.wrap(interp.space)
else:
w_rhs = self.rhs.execute(interp)
if not isinstance(w_lhs, W_NDimArray):
# scalar
dtype = get_dtype_cache(interp.space).w_float64dtype
w_lhs = W_NDimArray.new_scalar(interp.space, dtype, w_lhs)
assert isinstance(w_lhs, W_NDimArray)
if self.name == '+':
w_res = w_lhs.descr_add(interp.space, w_rhs)
elif self.name == '*':
w_res = w_lhs.descr_mul(interp.space, w_rhs)
elif self.name == '-':
w_res = w_lhs.descr_sub(interp.space, w_rhs)
elif self.name == '**':
w_res = w_lhs.descr_pow(interp.space, w_rhs)
elif self.name == '->':
if isinstance(w_rhs, FloatObject):
w_rhs = IntObject(int(w_rhs.floatval))
assert isinstance(w_lhs, W_NDimArray)
w_res = w_lhs.descr_getitem(interp.space, w_rhs)
else:
raise NotImplementedError
if (not isinstance(w_res, W_NDimArray) and
not isinstance(w_res, boxes.W_GenericBox)):
dtype = get_dtype_cache(interp.space).w_float64dtype
w_res = W_NDimArray.new_scalar(interp.space, dtype, w_res)
return w_res
示例2: find_unaryop_result_dtype
def find_unaryop_result_dtype(space, dt, promote_to_float=False,
promote_bools=False, promote_to_largest=False):
if promote_to_largest:
if dt.kind == NPY.GENBOOLLTR or dt.kind == NPY.SIGNEDLTR:
if dt.elsize * 8 < LONG_BIT:
return descriptor.get_dtype_cache(space).w_longdtype
elif dt.kind == NPY.UNSIGNEDLTR:
if dt.elsize * 8 < LONG_BIT:
return descriptor.get_dtype_cache(space).w_ulongdtype
else:
assert dt.kind == NPY.FLOATINGLTR or dt.kind == NPY.COMPLEXLTR
return dt
if promote_bools and (dt.kind == NPY.GENBOOLLTR):
return descriptor.get_dtype_cache(space).w_int8dtype
if promote_to_float:
if dt.kind == NPY.FLOATINGLTR or dt.kind == NPY.COMPLEXLTR:
return dt
if dt.num >= NPY.INT:
return descriptor.get_dtype_cache(space).w_float64dtype
for bytes, dtype in descriptor.get_dtype_cache(space).float_dtypes_by_num_bytes:
if (dtype.kind == NPY.FLOATINGLTR and
dtype.itemtype.get_element_size() >
dt.itemtype.get_element_size()):
return dtype
return dt
示例3: call
def call(self, space, args_w):
w_obj = args_w[0]
out = None
if len(args_w) > 1:
out = args_w[1]
if space.is_w(out, space.w_None):
out = None
w_obj = convert_to_array(space, w_obj)
dtype = w_obj.get_dtype()
if dtype.is_flexible():
raise OperationError(space.w_TypeError,
space.wrap('Not implemented for this type'))
if (self.int_only and not dtype.is_int() or
not self.allow_bool and dtype.is_bool() or
not self.allow_complex and dtype.is_complex()):
raise oefmt(space.w_TypeError,
"ufunc %s not supported for the input type", self.name)
calc_dtype = find_unaryop_result_dtype(space,
w_obj.get_dtype(),
promote_to_float=self.promote_to_float,
promote_bools=self.promote_bools)
if out is not None:
if not isinstance(out, W_NDimArray):
raise oefmt(space.w_TypeError, 'output must be an array')
res_dtype = out.get_dtype()
#if not w_obj.get_dtype().can_cast_to(res_dtype):
# raise oefmt(space.w_TypeError,
# "Cannot cast ufunc %s output from dtype('%s') to dtype('%s') with casting rule 'same_kind'", self.name, w_obj.get_dtype().name, res_dtype.name)
elif self.bool_result:
res_dtype = descriptor.get_dtype_cache(space).w_booldtype
else:
res_dtype = calc_dtype
if self.complex_to_float and calc_dtype.is_complex():
if calc_dtype.num == NPY.CFLOAT:
res_dtype = descriptor.get_dtype_cache(space).w_float32dtype
else:
res_dtype = descriptor.get_dtype_cache(space).w_float64dtype
if w_obj.is_scalar():
w_val = self.func(calc_dtype,
w_obj.get_scalar_value().convert_to(space, calc_dtype))
if out is None:
return w_val
w_val = res_dtype.coerce(space, w_val)
if out.is_scalar():
out.set_scalar_value(w_val)
else:
out.fill(space, w_val)
return out
shape = shape_agreement(space, w_obj.get_shape(), out,
broadcast_down=False)
return loop.call1(space, shape, self.func, calc_dtype, res_dtype,
w_obj, out)
示例4: test_binops
def test_binops(self, space):
bool_dtype = get_dtype_cache(space).w_booldtype
int8_dtype = get_dtype_cache(space).w_int8dtype
int32_dtype = get_dtype_cache(space).w_int32dtype
float64_dtype = get_dtype_cache(space).w_float64dtype
c64_dtype = get_dtype_cache(space).w_complex64dtype
c128_dtype = get_dtype_cache(space).w_complex128dtype
cld_dtype = get_dtype_cache(space).w_complexlongdtype
fld_dtype = get_dtype_cache(space).w_floatlongdtype
# Basic pairing
assert find_binop_result_dtype(space, bool_dtype, bool_dtype) is bool_dtype
assert find_binop_result_dtype(space, bool_dtype, float64_dtype) is float64_dtype
assert find_binop_result_dtype(space, float64_dtype, bool_dtype) is float64_dtype
assert find_binop_result_dtype(space, int32_dtype, int8_dtype) is int32_dtype
assert find_binop_result_dtype(space, int32_dtype, bool_dtype) is int32_dtype
assert find_binop_result_dtype(space, c64_dtype, float64_dtype) is c128_dtype
assert find_binop_result_dtype(space, c64_dtype, fld_dtype) is cld_dtype
assert find_binop_result_dtype(space, c128_dtype, fld_dtype) is cld_dtype
# With promote bool (happens on div), the result is that the op should
# promote bools to int8
assert find_binop_result_dtype(space, bool_dtype, bool_dtype, promote_bools=True) is int8_dtype
assert find_binop_result_dtype(space, bool_dtype, float64_dtype, promote_bools=True) is float64_dtype
# Coerce to floats
assert find_binop_result_dtype(space, bool_dtype, float64_dtype, promote_to_float=True) is float64_dtype
示例5: test_allowed_types
def test_allowed_types(self, space):
dt_bool = get_dtype_cache(space).w_booldtype
dt_float16 = get_dtype_cache(space).w_float16dtype
dt_int32 = get_dtype_cache(space).w_int32dtype
ufunc = unary_ufunc(space, None, "x", int_only=True)
assert ufunc._calc_dtype(space, dt_bool, out=None) == (dt_bool, dt_bool)
assert ufunc.dtypes # XXX: shouldn't contain too much stuff
ufunc = unary_ufunc(space, None, "x", promote_to_float=True)
assert ufunc._calc_dtype(space, dt_bool, out=None) == (dt_float16, dt_float16)
assert ufunc._calc_dtype(space, dt_bool, casting="same_kind") == (dt_float16, dt_float16)
raises(OperationError, ufunc._calc_dtype, space, dt_bool, casting="no")
ufunc = unary_ufunc(space, None, "x")
assert ufunc._calc_dtype(space, dt_int32, out=None) == (dt_int32, dt_int32)
示例6: _PyArray_DescrFromType
def _PyArray_DescrFromType(space, typenum):
try:
dtype = get_dtype_cache(space).dtypes_by_num[typenum]
return dtype
except KeyError:
raise OperationError(space.w_ValueError, space.wrap(
'_PyArray_DescrFromType called with invalid dtype %d' % typenum))
示例7: test_can_cast_same_type
def test_can_cast_same_type(space):
dt_bool = get_dtype_cache(space).w_booldtype
assert can_cast_type(space, dt_bool, dt_bool, 'no')
assert can_cast_type(space, dt_bool, dt_bool, 'equiv')
assert can_cast_type(space, dt_bool, dt_bool, 'safe')
assert can_cast_type(space, dt_bool, dt_bool, 'same_kind')
assert can_cast_type(space, dt_bool, dt_bool, 'unsafe')
示例8: argmin_argmax
def argmin_argmax(space, w_arr, w_out, axis):
from pypy.module.micronumpy.descriptor import get_dtype_cache
dtype = w_arr.get_dtype()
shapelen = len(w_arr.get_shape())
axis_flags = [False] * shapelen
axis_flags[axis] = True
inner_iter, outer_iter = split_iter(w_arr.implementation, axis_flags)
outer_state = outer_iter.reset()
out_iter, out_state = w_out.create_iter()
while not outer_iter.done(outer_state):
inner_state = inner_iter.reset()
inner_state.offset = outer_state.offset
cur_best = inner_iter.getitem(inner_state)
inner_state = inner_iter.next(inner_state)
result = 0
idx = 1
while not inner_iter.done(inner_state):
arg_driver.jit_merge_point(shapelen=shapelen, dtype=dtype)
w_val = inner_iter.getitem(inner_state)
new_best = getattr(dtype.itemtype, op_name)(cur_best, w_val)
if dtype.itemtype.ne(new_best, cur_best):
result = idx
cur_best = new_best
inner_state = inner_iter.next(inner_state)
idx += 1
result = get_dtype_cache(space).w_longdtype.box(result)
out_iter.setitem(out_state, result)
out_state = out_iter.next(out_state)
outer_state = outer_iter.next(outer_state)
return w_out
示例9: numpify
def numpify(space, w_object):
"""Convert the object to a W_NumpyObject"""
# XXX: code duplication with _array()
from pypy.module.micronumpy import strides
if isinstance(w_object, W_NumpyObject):
return w_object
# for anything that isn't already an array, try __array__ method first
w_array = try_array_method(space, w_object)
if w_array is not None:
return w_array
shape, elems_w = strides.find_shape_and_elems(space, w_object, None)
dtype = find_dtype_for_seq(space, elems_w, None)
if dtype is None:
dtype = descriptor.get_dtype_cache(space).w_float64dtype
elif dtype.is_str_or_unicode() and dtype.elsize < 1:
# promote S0 -> S1, U0 -> U1
dtype = descriptor.variable_dtype(space, dtype.char + '1')
if len(elems_w) == 1:
return dtype.coerce(space, elems_w[0])
else:
w_arr = W_NDimArray.from_shape(space, shape, dtype)
loop.assign(space, w_arr, elems_w)
return w_arr
示例10: test_SimpleNew_scalar
def test_SimpleNew_scalar(self, space, api):
ptr_s = lltype.nullptr(rffi.LONGP.TO)
a = api._PyArray_SimpleNew(0, ptr_s, 12)
dtype = get_dtype_cache(space).w_float64dtype
a.set_scalar_value(dtype.itemtype.box(10.))
assert a.get_scalar_value().value == 10.
示例11: __init__
def __init__(self, index_stride_size, stride_size, size):
start = 0
dtype = descriptor.get_dtype_cache(space).w_longdtype
indexes = dtype.itemtype.malloc(size * dtype.elsize)
values = alloc_raw_storage(size * stride_size,
track_allocation=False)
Repr.__init__(self, dtype.elsize, stride_size,
size, values, indexes, start, start)
示例12: from_shape
def from_shape(space, shape, dtype, order='C', w_instance=None, zero=True):
from pypy.module.micronumpy import concrete, descriptor, boxes
from pypy.module.micronumpy.strides import calc_strides
strides, backstrides = calc_strides(shape, dtype.base, order)
impl = concrete.ConcreteArray(shape, dtype.base, order, strides,
backstrides, zero=zero)
if dtype == descriptor.get_dtype_cache(space).w_objectdtype:
impl.fill(space, boxes.W_ObjectBox(space.w_None))
if w_instance:
return wrap_impl(space, space.type(w_instance), w_instance, impl)
return W_NDimArray(impl)
示例13: _PyArray_FromObject
def _PyArray_FromObject(space, w_obj, typenum, min_depth, max_depth):
try:
dtype = get_dtype_cache(space).dtypes_by_num[typenum]
except KeyError:
raise oefmt(space.w_ValueError, "_PyArray_FromObject called with invalid dtype %d", typenum)
try:
return _PyArray_FromAny(space, w_obj, dtype, min_depth, max_depth, 0, NULL)
except OperationError as e:
if e.match(space, space.w_NotImplementedError):
errstr = space.str_w(e.get_w_value(space))
raise oefmt(space.w_NotImplementedError, "_PyArray_FromObject %s", errstr[16:])
raise
示例14: find_dtype_for_seq
def find_dtype_for_seq(space, elems_w, dtype):
if len(elems_w) == 1:
w_elem = elems_w[0]
return _dtype_guess(space, dtype, w_elem)
for w_elem in elems_w:
dtype = _dtype_guess(space, dtype, w_elem)
if dtype is None:
dtype = descriptor.get_dtype_cache(space).w_float64dtype
elif dtype.is_str_or_unicode() and dtype.elsize < 1:
# promote S0 -> S1, U0 -> U1
dtype = descriptor.variable_dtype(space, dtype.char + '1')
return dtype
示例15: test_type_resolver
def test_type_resolver(self, space):
c128_dtype = get_dtype_cache(space).w_complex128dtype
c64_dtype = get_dtype_cache(space).w_complex64dtype
f64_dtype = get_dtype_cache(space).w_float64dtype
f32_dtype = get_dtype_cache(space).w_float32dtype
u32_dtype = get_dtype_cache(space).w_uint32dtype
b_dtype = get_dtype_cache(space).w_booldtype
ufunc = W_UfuncGeneric(
space,
[None, None, None],
"eigenvals",
None,
1,
1,
[f32_dtype, c64_dtype, f64_dtype, c128_dtype, c128_dtype, c128_dtype],
"",
)
f32_array = W_NDimArray(VoidBoxStorage(0, f32_dtype))
index, dtypes = ufunc.type_resolver(space, [f32_array], [None], "d->D", ufunc.dtypes)
# needs to cast input type, create output type
assert index == 1
assert dtypes == [f64_dtype, c128_dtype]
index, dtypes = ufunc.type_resolver(space, [f32_array], [None], "", ufunc.dtypes)
assert index == 0
assert dtypes == [f32_dtype, c64_dtype]
raises(OperationError, ufunc.type_resolver, space, [f32_array], [None], "u->u", ufunc.dtypes)
exc = raises(OperationError, ufunc.type_resolver, space, [f32_array], [None], "i->i", ufunc.dtypes)