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
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
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
def execute(self, interp): if self.v == 'int': dtype = get_dtype_cache(interp.space).w_int64dtype elif self.v == 'float': dtype = get_dtype_cache(interp.space).w_float64dtype else: raise BadToken('unknown v to dtype "%s"' % self.v) return dtype
def execute(self, interp): if self.v == 'int': dtype = get_dtype_cache(interp.space).w_int64dtype elif self.v == 'float': dtype = get_dtype_cache(interp.space).w_float64dtype else: raise BadToken('unknown v to dtype "%s"' % self.v) return dtype
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)
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
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)
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)
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
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
def from_shape(space, shape, dtype, order=NPY.CORDER, w_instance=None, zero=True): from pypy.module.micronumpy import concrete, descriptor, boxes from pypy.module.micronumpy.strides import calc_strides if len(shape) > NPY.MAXDIMS: raise oefmt(space.w_ValueError, "sequence too large; cannot be greater than %d", NPY.MAXDIMS) try: ovfcheck(support.product_check(shape) * dtype.elsize) except OverflowError as e: raise oefmt(space.w_ValueError, "array is too big.") 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)
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))
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
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')
def do_ufunc(space, funcs, data, types, ntypes, nin, nout, identity, name, doc, check_return, w_signature): funcs_w = [None] * ntypes dtypes_w = [None] * ntypes * (nin + nout) for i in range(ntypes): funcs_w[i] = ufuncs.W_GenericUFuncCaller( rffi.cast(gufunctype, funcs[i]), data) for i in range(ntypes * (nin + nout)): dtypes_w[i] = get_dtype_cache(space).dtypes_by_num[ord(types[i])] w_funcs = space.newlist(funcs_w) w_dtypes = space.newlist(dtypes_w) w_doc = rffi.charp2str(doc) w_name = rffi.charp2str(name) w_identity = space.wrap(identity) ufunc_generic = ufuncs.frompyfunc(space, w_funcs, nin, nout, w_dtypes, w_signature, w_identity, w_name, w_doc, stack_inputs=True) return ufunc_generic
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 = strides.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
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')
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))
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.
def PyArray_DescrFromType(space, typenum): try: dtype = get_dtype_cache(space).dtypes_by_num(typenum) return dtype except KeyError: raise oefmt(space.w_ValueError, "PyArray_DescrFromType called with invalid dtype %d", typenum)
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)
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.
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)
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)
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
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
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
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)
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)
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)
def _PyArray_FromObject(space, w_obj, typenum, min_depth, max_depth): try: dtype = get_dtype_cache(space).dtypes_by_num[typenum] except KeyError: raise OperationError(space.w_ValueError, space.wrap( '_PyArray_FromObject called with invalid dtype %d' % typenum)) try: return _PyArray_FromAny(space, w_obj, dtype, min_depth, max_depth, 0, NULL); except OperationError, e: if e.match(space, space.w_NotImplementedError): errstr = space.str_w(e.get_w_value(space)) errstr = '_PyArray_FromObject' + errstr[16:] raise OperationError(space.w_NotImplementedError, space.wrap( errstr)) raise
def _PyArray_FromObject(space, w_obj, typenum, min_depth, max_depth): try: dtype = get_dtype_cache(space).dtypes_by_num[typenum] except KeyError: raise OperationError(space.w_ValueError, space.wrap( '_PyArray_FromObject called with invalid dtype %d' % typenum)) try: return _PyArray_FromAny(space, w_obj, dtype, min_depth, max_depth, 0, NULL); except OperationError, e: if e.match(space, space.w_NotImplementedError): errstr = space.str_w(e.get_w_value(space)) errstr = '_PyArray_FromObject' + errstr[16:] raise OperationError(space.w_NotImplementedError, space.wrap( errstr)) raise
def do_ufunc(space, funcs, data, types, ntypes, nin, nout, identity, name, doc, check_return, w_signature): funcs_w = [None] * ntypes dtypes_w = [None] * ntypes * (nin + nout) for i in range(ntypes): funcs_w[i] = ufuncs.W_GenericUFuncCaller(rffi.cast(gufunctype, funcs[i]), data) for i in range(ntypes*(nin+nout)): dtypes_w[i] = get_dtype_cache(space).dtypes_by_num[ord(types[i])] w_funcs = space.newlist(funcs_w) w_dtypes = space.newlist(dtypes_w) w_doc = rffi.charp2str(doc) w_name = rffi.charp2str(name) w_identity = space.wrap(identity) ufunc_generic = ufuncs.frompyfunc(space, w_funcs, nin, nout, w_dtypes, w_signature, w_identity, w_name, w_doc, stack_inputs=True) return ufunc_generic
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.text_w(e.get_w_value(space)) raise oefmt(space.w_NotImplementedError, "_PyArray_FromObject %s", errstr[16:]) raise
def argsort(arr, space, w_axis): if w_axis is space.w_None: # note that it's fine ot pass None here as we're not going # to pass the result around (None is the link to base in slices) if arr.get_size() > 0: arr = arr.reshape(None, [arr.get_size()]) axis = 0 elif w_axis is None: axis = -1 else: axis = space.int_w(w_axis) # create array of indexes dtype = descriptor.get_dtype_cache(space).w_longdtype index_arr = W_NDimArray.from_shape(space, arr.get_shape(), dtype) with index_arr.implementation as storage, arr as arr_storage: if len(arr.get_shape()) == 1: for i in range(arr.get_size()): raw_storage_setitem(storage, i * INT_SIZE, i) r = Repr(INT_SIZE, arr.strides[0], arr.get_size(), arr_storage, storage, 0, arr.start) ArgSort(r).sort() else: shape = arr.get_shape() if axis < 0: axis = len(shape) + axis if axis < 0 or axis >= len(shape): raise oefmt(space.w_IndexError, "Wrong axis %d", axis) arr_iter = AllButAxisIter(arr, axis) arr_state = arr_iter.reset() index_impl = index_arr.implementation index_iter = AllButAxisIter(index_impl, axis) index_state = index_iter.reset() stride_size = arr.strides[axis] index_stride_size = index_impl.strides[axis] axis_size = arr.shape[axis] while not arr_iter.done(arr_state): for i in range(axis_size): raw_storage_setitem( storage, i * index_stride_size + index_state.offset, i) r = Repr(index_stride_size, stride_size, axis_size, arr_storage, storage, index_state.offset, arr_state.offset) ArgSort(r).sort() arr_state = arr_iter.next(arr_state) index_state = index_iter.next(index_state) return index_arr
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 if len(shape) > NPY.MAXDIMS: raise oefmt(space.w_ValueError, "sequence too large; must be smaller than %d", NPY.MAXDIMS) try: support.product(shape) * dtype.elsize except OverflowError as e: raise oefmt(space.w_ValueError, "array is too big") 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)
def argsort(arr, space, w_axis): if w_axis is space.w_None: # note that it's fine ot pass None here as we're not going # to pass the result around (None is the link to base in slices) if arr.get_size() > 0: arr = arr.reshape(None, [arr.get_size()]) axis = 0 elif w_axis is None: axis = -1 else: axis = space.int_w(w_axis) # create array of indexes dtype = descriptor.get_dtype_cache(space).w_longdtype index_arr = W_NDimArray.from_shape(space, arr.get_shape(), dtype) with index_arr.implementation as storage, arr as arr_storage: if len(arr.get_shape()) == 1: for i in range(arr.get_size()): raw_storage_setitem(storage, i * INT_SIZE, i) r = Repr(INT_SIZE, arr.strides[0], arr.get_size(), arr_storage, storage, 0, arr.start) ArgSort(r).sort() else: shape = arr.get_shape() if axis < 0: axis = len(shape) + axis if axis < 0 or axis >= len(shape): raise oefmt(space.w_IndexError, "Wrong axis %d", axis) arr_iter = AllButAxisIter(arr, axis) arr_state = arr_iter.reset() index_impl = index_arr.implementation index_iter = AllButAxisIter(index_impl, axis) index_state = index_iter.reset() stride_size = arr.strides[axis] index_stride_size = index_impl.strides[axis] axis_size = arr.shape[axis] while not arr_iter.done(arr_state): for i in range(axis_size): raw_storage_setitem(storage, i * index_stride_size + index_state.offset, i) r = Repr(index_stride_size, stride_size, axis_size, arr_storage, storage, index_state.offset, arr_state.offset) ArgSort(r).sort() arr_state = arr_iter.next(arr_state) index_state = index_iter.next(index_state) return index_arr
def add_ufunc(self, space, ufunc_name, op_name, argcount, extra_kwargs=None): if extra_kwargs is None: extra_kwargs = {} identity = extra_kwargs.get("identity") if identity is not None: identity = \ descriptor.get_dtype_cache(space).w_longdtype.box(identity) extra_kwargs["identity"] = identity func = ufunc_dtype_caller(space, ufunc_name, op_name, argcount, comparison_func=extra_kwargs.get("comparison_func", False), bool_result=extra_kwargs.get("bool_result", False), ) if argcount == 1: ufunc = W_Ufunc1(func, ufunc_name, **extra_kwargs) elif argcount == 2: ufunc = W_Ufunc2(func, ufunc_name, **extra_kwargs) setattr(self, ufunc_name, ufunc)
def ufunc_dtype_caller(space, ufunc_name, op_name, argcount, comparison_func, bool_result): def get_op(dtype): try: return getattr(dtype.itemtype, op_name) except AttributeError: raise oefmt(space.w_NotImplementedError, "%s not implemented for %s", ufunc_name, dtype.get_name()) dtype_cache = descriptor.get_dtype_cache(space) if argcount == 1: def impl(res_dtype, value): res = get_op(res_dtype)(value) if bool_result: return dtype_cache.w_booldtype.box(res) return res elif argcount == 2: def impl(res_dtype, lvalue, rvalue): res = get_op(res_dtype)(lvalue, rvalue) if comparison_func: return dtype_cache.w_booldtype.box(res) return res return func_with_new_name(impl, ufunc_name)
def find_dtype_for_scalar(space, w_obj, current_guess=None): bool_dtype = descriptor.get_dtype_cache(space).w_booldtype long_dtype = descriptor.get_dtype_cache(space).w_longdtype int64_dtype = descriptor.get_dtype_cache(space).w_int64dtype uint64_dtype = descriptor.get_dtype_cache(space).w_uint64dtype complex_dtype = descriptor.get_dtype_cache(space).w_complex128dtype float_dtype = descriptor.get_dtype_cache(space).w_float64dtype if isinstance(w_obj, boxes.W_GenericBox): dtype = w_obj.get_dtype(space) return find_binop_result_dtype(space, dtype, current_guess) if space.isinstance_w(w_obj, space.w_bool): return find_binop_result_dtype(space, bool_dtype, current_guess) elif space.isinstance_w(w_obj, space.w_int): return find_binop_result_dtype(space, long_dtype, current_guess) elif space.isinstance_w(w_obj, space.w_long): try: space.int_w(w_obj) except OperationError, e: if e.match(space, space.w_OverflowError): return find_binop_result_dtype(space, uint64_dtype, current_guess) raise return find_binop_result_dtype(space, int64_dtype, current_guess)
def _array(space, w_object, w_dtype=None, copy=True, w_order=None, subok=False): from pypy.module.micronumpy.boxes import W_GenericBox # numpy testing calls array(type(array([]))) and expects a ValueError if space.isinstance_w(w_object, space.w_type): raise oefmt(space.w_ValueError, "cannot create ndarray from type instance") # for anything that isn't already an array, try __array__ method first dtype = descriptor.decode_w_dtype(space, w_dtype) if not isinstance(w_object, W_NDimArray): w_array = try_array_method(space, w_object, w_dtype) if w_array is None: if (not space.isinstance_w(w_object, space.w_bytes) and not space.isinstance_w(w_object, space.w_unicode) and not isinstance(w_object, W_GenericBox)): # use buffer interface w_object = _array_from_buffer_3118(space, w_object, dtype) else: # continue with w_array, but do further operations in place w_object = w_array copy = False dtype = w_object.get_dtype() if not isinstance(w_object, W_NDimArray): w_array, _copy = try_interface_method(space, w_object, copy) if w_array is not None: w_object = w_array copy = _copy dtype = w_object.get_dtype() if isinstance(w_object, W_NDimArray): npy_order = order_converter(space, w_order, NPY.ANYORDER) if (dtype is None or w_object.get_dtype() is dtype) and ( subok or type(w_object) is W_NDimArray): flags = w_object.get_flags() must_copy = copy must_copy |= (npy_order == NPY.CORDER and not flags & NPY.ARRAY_C_CONTIGUOUS) must_copy |= (npy_order == NPY.FORTRANORDER and not flags & NPY.ARRAY_F_CONTIGUOUS) if must_copy: return w_object.descr_copy(space, space.newint(npy_order)) else: return w_object if subok and not type(w_object) is W_NDimArray: raise oefmt(space.w_NotImplementedError, "array(..., subok=True) only partially implemented") # we have a ndarray, but need to copy or change dtype if dtype is None: dtype = w_object.get_dtype() if dtype != w_object.get_dtype(): # silently reject the copy value copy = True if copy: shape = w_object.get_shape() order = support.get_order_as_CF(w_object.get_order(), npy_order) w_arr = W_NDimArray.from_shape(space, shape, dtype, order=order) if support.product(shape) == 1: w_arr.set_scalar_value( dtype.coerce(space, w_object.implementation.getitem(0))) else: loop.setslice(space, shape, w_arr.implementation, w_object.implementation) return w_arr else: imp = w_object.implementation w_base = w_object sz = w_base.get_size() * dtype.elsize if imp.base() is not None: w_base = imp.base() if type(w_base) is W_NDimArray: sz = w_base.get_size() * dtype.elsize else: # this must succeed (mmap, buffer, ...) sz = space.int_w(space.call_method(w_base, 'size')) with imp as storage: return W_NDimArray.from_shape_and_storage(space, w_object.get_shape(), storage, dtype, storage_bytes=sz, w_base=w_base, strides=imp.strides, start=imp.start) else: # not an array npy_order = order_converter(space, w_order, NPY.CORDER) shape, elems_w = find_shape_and_elems(space, w_object, dtype) if dtype is None and space.isinstance_w(w_object, space.w_buffer): dtype = descriptor.get_dtype_cache(space).w_uint8dtype if dtype is None or (dtype.is_str_or_unicode() and dtype.elsize < 1): dtype = find_dtype_for_seq(space, elems_w, dtype) w_arr = W_NDimArray.from_shape(space, shape, dtype, order=npy_order) if support.product( shape) == 1: # safe from overflow since from_shape checks w_arr.set_scalar_value(dtype.coerce(space, elems_w[0])) else: loop.assign(space, w_arr, elems_w) return w_arr
def scalar(space): dtype = get_dtype_cache(space).w_float64dtype return W_NDimArray.new_scalar(space, dtype, space.wrap(10.))
def iarray(space, shape, order=NPY.CORDER): dtype = get_dtype_cache(space).w_int64dtype return W_NDimArray.from_shape(space, shape, dtype, order=order)
def get_shape_and_dtype(space, nd, dims, typenum): shape = [] for i in range(nd): shape.append(rffi.cast(rffi.LONG, dims[i])) dtype = get_dtype_cache(space).dtypes_by_num[typenum] return shape, dtype
def _array_from_buffer_3118(space, w_object, dtype): try: w_buf = space.call_method(space.builtin, "memoryview", w_object) except OperationError as e: if e.match(space, space.w_TypeError): # object does not have buffer interface return w_object raise format = space.getattr(w_buf, space.newtext('format')) if format: descr = _descriptor_from_pep3118_format(space, space.text_w(format)) if not descr: return w_object if dtype and descr: raise oefmt( space.w_NotImplementedError, "creating an array from a memoryview while specifying dtype " "not supported") if descr.elsize != space.int_w( space.getattr(w_buf, space.newbytes('itemsize'))): msg = ("Item size computed from the PEP 3118 buffer format " "string does not match the actual item size.") space.warn(space.newtext(msg), space.w_RuntimeWarning) return w_object dtype = descr elif not dtype: dtype = descriptor.get_dtype_cache(space).w_stringdtype dtype.elsize = space.int_w( space.getattr(w_buf, space.newbytes('itemsize'))) nd = space.int_w(space.getattr(w_buf, space.newbytes('ndim'))) shape = [ space.int_w(d) for d in space.listview(space.getattr(w_buf, space.newbytes('shape'))) ] strides = [] buflen = space.len_w(w_buf) * dtype.elsize if shape: strides = [ space.int_w(d) for d in space.listview( space.getattr(w_buf, space.newbytes('strides'))) ] if not strides: d = buflen strides = [0] * nd for k in range(nd): if shape[k] > 0: d /= shape[k] strides[k] = d else: if nd == 1: shape = [ buflen / dtype.elsize, ] strides = [ dtype.elsize, ] elif nd > 1: msg = ("ndim computed from the PEP 3118 buffer format " "is greater than 1, but shape is NULL.") space.warn(space.newtext(msg), space.w_RuntimeWarning) return w_object try: w_data = rffi.cast( RAW_STORAGE_PTR, space.int_w(space.call_method(w_buf, '_pypy_raw_address'))) except OperationError as e: if e.match(space, space.w_ValueError): return w_object else: raise e writable = not space.bool_w( space.getattr(w_buf, space.newbytes('readonly'))) w_ret = W_NDimArray.from_shape_and_storage(space, shape, w_data, storage_bytes=buflen, dtype=dtype, w_base=w_object, writable=writable, strides=strides) if w_ret: return w_ret return w_object
def array(space, shape, order='C'): dtype = get_dtype_cache(space).w_float64dtype return W_NDimArray.from_shape(space, shape, dtype, order=order)
def iarray(space, shape, order=NPY.CORDER): dtype = get_dtype_cache(space).w_int64dtype return W_NDimArray.from_shape(space, shape, dtype, order=order)
def execute(self, interp): w_list = interp.space.newlist( [interp.space.wrap(float(i)) for i in range(self.v)] ) dtype = get_dtype_cache(interp.space).w_float64dtype return array(interp.space, w_list, w_dtype=dtype, w_order=None)
def execute(self, interp): arr = self.args[0].execute(interp) if not isinstance(arr, W_NDimArray): raise ArgumentNotAnArray if self.name in SINGLE_ARG_FUNCTIONS: if len(self.args) != 1 and self.name != 'sum': raise ArgumentMismatch if self.name == "sum": if len(self.args) > 1: var = self.args[1] if isinstance(var, DtypeClass): w_res = arr.descr_sum(interp.space, None, var.execute(interp)) else: w_res = arr.descr_sum(interp.space, self.args[1].execute(interp)) else: w_res = arr.descr_sum(interp.space) elif self.name == "prod": w_res = arr.descr_prod(interp.space) elif self.name == "max": w_res = arr.descr_max(interp.space) elif self.name == "min": w_res = arr.descr_min(interp.space) elif self.name == "any": w_res = arr.descr_any(interp.space) elif self.name == "all": w_res = arr.descr_all(interp.space) elif self.name == "cumsum": w_res = arr.descr_cumsum(interp.space) elif self.name == "logical_xor_reduce": logical_xor = ufuncs.get(interp.space).logical_xor w_res = logical_xor.reduce(interp.space, arr, None) elif self.name == "unegative": neg = ufuncs.get(interp.space).negative w_res = neg.call(interp.space, [arr], None, 'unsafe', None) elif self.name == "cos": cos = ufuncs.get(interp.space).cos w_res = cos.call(interp.space, [arr], None, 'unsafe', None) elif self.name == "flat": w_res = arr.descr_get_flatiter(interp.space) elif self.name == "argsort": w_res = arr.descr_argsort(interp.space) elif self.name == "tostring": arr.descr_tostring(interp.space) w_res = None else: assert False # unreachable code elif self.name in TWO_ARG_FUNCTIONS: if len(self.args) != 2: raise ArgumentMismatch arg = self.args[1].execute(interp) if not isinstance(arg, W_NDimArray): raise ArgumentNotAnArray if self.name == "dot": w_res = arr.descr_dot(interp.space, arg) elif self.name == 'multiply': w_res = arr.descr_mul(interp.space, arg) elif self.name == 'take': w_res = arr.descr_take(interp.space, arg) elif self.name == "searchsorted": w_res = arr.descr_searchsorted(interp.space, arg, interp.space.newtext('left')) else: assert False # unreachable code elif self.name in THREE_ARG_FUNCTIONS: if len(self.args) != 3: raise ArgumentMismatch arg1 = self.args[1].execute(interp) arg2 = self.args[2].execute(interp) if not isinstance(arg1, W_NDimArray): raise ArgumentNotAnArray if not isinstance(arg2, W_NDimArray): raise ArgumentNotAnArray if self.name == "where": w_res = where(interp.space, arr, arg1, arg2) else: assert False # unreachable code elif self.name in TWO_ARG_FUNCTIONS_OR_NONE: if len(self.args) != 2: raise ArgumentMismatch arg = self.args[1].execute(interp) if self.name == 'view': w_res = arr.descr_view(interp.space, arg) elif self.name == 'astype': w_res = arr.descr_astype(interp.space, arg) elif self.name == 'reshape': w_arg = self.args[1] assert isinstance(w_arg, ArrayConstant) order = -1 w_res = arr.reshape(interp.space, w_arg.wrap(interp.space), order) else: assert False else: raise WrongFunctionName if isinstance(w_res, W_NDimArray): return w_res if isinstance(w_res, FloatObject): dtype = get_dtype_cache(interp.space).w_float64dtype elif isinstance(w_res, IntObject): dtype = get_dtype_cache(interp.space).w_int64dtype elif isinstance(w_res, BoolObject): dtype = get_dtype_cache(interp.space).w_booldtype elif isinstance(w_res, boxes.W_GenericBox): dtype = w_res.get_dtype(interp.space) else: dtype = None return W_NDimArray.new_scalar(interp.space, dtype, w_res)
def execute(self, interp): w_list = interp.space.newlist( [interp.space.newfloat(float(i)) for i in range(self.v)]) dtype = get_dtype_cache(interp.space).w_float64dtype return array(interp.space, w_list, w_dtype=dtype, w_order=None)
def array(space, w_object, w_dtype=None, copy=True, w_order=None, subok=False, ndmin=0): from pypy.module.micronumpy import strides # for anything that isn't already an array, try __array__ method first if not isinstance(w_object, W_NDimArray): w___array__ = space.lookup(w_object, "__array__") if w___array__ is not None: if space.is_none(w_dtype): w_dtype = space.w_None w_array = space.get_and_call_function(w___array__, w_object, w_dtype) if isinstance(w_array, W_NDimArray): # feed w_array back into array() for other properties return array(space, w_array, w_dtype, False, w_order, subok, ndmin) else: raise oefmt(space.w_ValueError, "object __array__ method not producing an array") dtype = descriptor.decode_w_dtype(space, w_dtype) if space.is_none(w_order): order = 'C' else: order = space.str_w(w_order) if order == 'K': order = 'C' if order != 'C': # or order != 'F': raise oefmt(space.w_ValueError, "Unknown order: %s", order) # arrays with correct dtype if isinstance(w_object, W_NDimArray) and \ (space.is_none(w_dtype) or w_object.get_dtype() is dtype): shape = w_object.get_shape() if copy: w_ret = w_object.descr_copy(space) else: if ndmin <= len(shape): return w_object new_impl = w_object.implementation.set_shape(space, w_object, shape) w_ret = W_NDimArray(new_impl) if ndmin > len(shape): shape = [1] * (ndmin - len(shape)) + shape w_ret.implementation = w_ret.implementation.set_shape(space, w_ret, shape) return w_ret # not an array or incorrect dtype shape, elems_w = strides.find_shape_and_elems(space, w_object, dtype) if dtype is None or (dtype.is_str_or_unicode() and dtype.elsize < 1): dtype = strides.find_dtype_for_seq(space, elems_w, dtype) 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 ndmin > len(shape): shape = [1] * (ndmin - len(shape)) + shape w_arr = W_NDimArray.from_shape(space, shape, dtype, order=order) if len(elems_w) == 1: w_arr.set_scalar_value(dtype.coerce(space, elems_w[0])) else: loop.assign(space, w_arr, elems_w) return w_arr
def descr_all(self, space): from pypy.module.micronumpy.descriptor import get_dtype_cache value = space.is_true(self) return get_dtype_cache(space).w_booldtype.box(value)
def _array(space, w_object, w_dtype=None, copy=True, w_order=None, subok=False): from pypy.module.micronumpy import strides # for anything that isn't already an array, try __array__ method first if not isinstance(w_object, W_NDimArray): w_array = try_array_method(space, w_object, w_dtype) if w_array is not None: # continue with w_array, but do further operations in place w_object = w_array copy = False if not isinstance(w_object, W_NDimArray): w_array = try_interface_method(space, w_object) if w_array is not None: w_object = w_array copy = False dtype = descriptor.decode_w_dtype(space, w_dtype) if space.is_none(w_order): order = 'C' else: order = space.str_w(w_order) if order == 'K': order = 'C' if order != 'C': # or order != 'F': raise oefmt(space.w_ValueError, "Unknown order: %s", order) if isinstance(w_object, W_NDimArray): if (dtype is None or w_object.get_dtype() is dtype): if copy and (subok or type(w_object) is W_NDimArray): return w_object.descr_copy(space, w_order) elif not copy and (subok or type(w_object) is W_NDimArray): return w_object if subok and not type(w_object) is W_NDimArray: raise oefmt(space.w_NotImplementedError, "array(..., subok=True) only partially implemented") # we have a ndarray, but need to copy or change dtype if dtype is None: dtype = w_object.get_dtype() if dtype != w_object.get_dtype(): # silently reject the copy value copy = True if copy: shape = w_object.get_shape() elems_w = [None] * w_object.get_size() elsize = w_object.get_dtype().elsize # TODO - use w_object.implementation without copying to a list # unfortunately that causes a union error in translation for i in range(w_object.get_size()): elems_w[i] = w_object.implementation.getitem(i * elsize) else: imp = w_object.implementation with imp as storage: sz = support.product(w_object.get_shape()) * dtype.elsize return W_NDimArray.from_shape_and_storage(space, w_object.get_shape(), storage, dtype, storage_bytes=sz, w_base=w_object, start=imp.start) else: # not an array shape, elems_w = strides.find_shape_and_elems(space, w_object, dtype) if dtype is None or (dtype.is_str_or_unicode() and dtype.elsize < 1): dtype = strides.find_dtype_for_seq(space, elems_w, dtype) 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') w_arr = W_NDimArray.from_shape(space, shape, dtype, order=order) if support.product(shape) == 1: w_arr.set_scalar_value(dtype.coerce(space, elems_w[0])) else: loop.assign(space, w_arr, elems_w) return w_arr
def get_shape_and_dtype(space, nd, dims, typenum): shape = [] for i in range(nd): shape.append(rffi.cast(rffi.LONG, dims[i])) dtype = get_dtype_cache(space).dtypes_by_num[typenum] return shape, dtype
def array(space, shape, order='C'): dtype = get_dtype_cache(space).w_float64dtype return W_NDimArray.from_shape(space, shape, dtype, order=order)
def execute(self, interp): arr = self.args[0].execute(interp) if not isinstance(arr, W_NDimArray): raise ArgumentNotAnArray if self.name in SINGLE_ARG_FUNCTIONS: if len(self.args) != 1 and self.name != 'sum': raise ArgumentMismatch if self.name == "sum": if len(self.args)>1: w_res = arr.descr_sum(interp.space, self.args[1].execute(interp)) else: w_res = arr.descr_sum(interp.space) elif self.name == "prod": w_res = arr.descr_prod(interp.space) elif self.name == "max": w_res = arr.descr_max(interp.space) elif self.name == "min": w_res = arr.descr_min(interp.space) elif self.name == "any": w_res = arr.descr_any(interp.space) elif self.name == "all": w_res = arr.descr_all(interp.space) elif self.name == "cumsum": w_res = arr.descr_cumsum(interp.space) elif self.name == "logical_xor_reduce": logical_xor = ufuncs.get(interp.space).logical_xor w_res = logical_xor.reduce(interp.space, arr, None) elif self.name == "unegative": neg = ufuncs.get(interp.space).negative w_res = neg.call(interp.space, [arr], None, None, None) elif self.name == "cos": cos = ufuncs.get(interp.space).cos w_res = cos.call(interp.space, [arr], None, None, None) elif self.name == "flat": w_res = arr.descr_get_flatiter(interp.space) elif self.name == "argsort": w_res = arr.descr_argsort(interp.space) elif self.name == "tostring": arr.descr_tostring(interp.space) w_res = None else: assert False # unreachable code elif self.name in TWO_ARG_FUNCTIONS: if len(self.args) != 2: raise ArgumentMismatch arg = self.args[1].execute(interp) if not isinstance(arg, W_NDimArray): raise ArgumentNotAnArray if self.name == "dot": w_res = arr.descr_dot(interp.space, arg) elif self.name == 'take': w_res = arr.descr_take(interp.space, arg) elif self.name == "searchsorted": w_res = arr.descr_searchsorted(interp.space, arg, interp.space.wrap('left')) else: assert False # unreachable code elif self.name in THREE_ARG_FUNCTIONS: if len(self.args) != 3: raise ArgumentMismatch arg1 = self.args[1].execute(interp) arg2 = self.args[2].execute(interp) if not isinstance(arg1, W_NDimArray): raise ArgumentNotAnArray if not isinstance(arg2, W_NDimArray): raise ArgumentNotAnArray if self.name == "where": w_res = where(interp.space, arr, arg1, arg2) else: assert False elif self.name in TWO_ARG_FUNCTIONS_OR_NONE: if len(self.args) != 2: raise ArgumentMismatch arg = self.args[1].execute(interp) if self.name == 'view': w_res = arr.descr_view(interp.space, arg) elif self.name == 'astype': w_res = arr.descr_astype(interp.space, arg) else: assert False else: raise WrongFunctionName if isinstance(w_res, W_NDimArray): return w_res if isinstance(w_res, FloatObject): dtype = get_dtype_cache(interp.space).w_float64dtype elif isinstance(w_res, IntObject): dtype = get_dtype_cache(interp.space).w_int64dtype elif isinstance(w_res, BoolObject): dtype = get_dtype_cache(interp.space).w_booldtype elif isinstance(w_res, boxes.W_GenericBox): dtype = w_res.get_dtype(interp.space) else: dtype = None return W_NDimArray.new_scalar(interp.space, dtype, w_res)
def scalar(space): dtype = get_dtype_cache(space).w_float64dtype return W_NDimArray.new_scalar(space, dtype, space.wrap(10.))
def descr_zero(self, space): from pypy.module.micronumpy.descriptor import get_dtype_cache return get_dtype_cache(space).w_longdtype.box(0)