def astype(self, space, dtype): strides, backstrides = calc_strides(self.get_shape(), dtype, self.order) impl = ConcreteArray(self.get_shape(), dtype, self.order, strides, backstrides) loop.setslice(space, impl.get_shape(), impl, self) return impl
def from_shape_and_storage(space, shape, storage, dtype, order='C', owning=False, w_subtype=None, w_base=None, writable=True): from pypy.module.micronumpy import concrete from pypy.module.micronumpy.strides import calc_strides strides, backstrides = calc_strides(shape, dtype, order) if w_base is not None: if owning: raise OperationError(space.w_ValueError, space.wrap("Cannot have owning=True when specifying a buffer")) if writable: impl = concrete.ConcreteArrayWithBase(shape, dtype, order, strides, backstrides, storage, w_base) else: impl = concrete.ConcreteNonWritableArrayWithBase(shape, dtype, order, strides, backstrides, storage, w_base) elif owning: # Will free storage when GCd impl = concrete.ConcreteArray(shape, dtype, order, strides, backstrides, storage=storage) else: impl = concrete.ConcreteArrayNotOwning(shape, dtype, order, strides, backstrides, storage) if w_subtype: w_ret = space.allocate_instance(W_NDimArray, w_subtype) W_NDimArray.__init__(w_ret, impl) space.call_method(w_ret, '__array_finalize__', w_subtype) return w_ret return W_NDimArray(impl)
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 astype(self, space, dtype, order, copy=True): # copy the general pattern of the strides # but make the array storage contiguous in memory shape = self.get_shape() strides = self.get_strides() if order not in (NPY.KEEPORDER, NPY.FORTRANORDER, NPY.CORDER): raise oefmt(space.w_ValueError, "Unknown order %d in astype", order) if len(strides) == 0: t_strides = [] backstrides = [] elif order in (NPY.FORTRANORDER, NPY.CORDER): t_strides, backstrides = calc_strides(shape, dtype, order) else: indx_array = range(len(strides)) list_sorter = StrideSort(indx_array, strides, self.order) list_sorter.sort() t_elsize = dtype.elsize t_strides = strides[:] base = dtype.elsize for i in indx_array: t_strides[i] = base base *= shape[i] backstrides = calc_backstrides(t_strides, shape) order = support.get_order_as_CF(self.order, order) impl = ConcreteArray(shape, dtype, order, t_strides, backstrides) if copy: loop.setslice(space, impl.get_shape(), impl, self) return impl
def astype(self, space, dtype, order): # copy the general pattern of the strides # but make the array storage contiguous in memory shape = self.get_shape() strides = self.get_strides() if order not in ('C', 'F'): raise oefmt(space.w_ValueError, "Unknown order %s in astype", order) if len(strides) == 0: t_strides = [] backstrides = [] elif order != self.order: t_strides, backstrides = calc_strides(shape, dtype, order) else: indx_array = range(len(strides)) list_sorter = StrideSort(indx_array, strides) list_sorter.sort() t_elsize = dtype.elsize t_strides = strides[:] base = dtype.elsize for i in indx_array: t_strides[i] = base base *= shape[i] backstrides = calc_backstrides(t_strides, shape) impl = ConcreteArray(shape, dtype, order, t_strides, backstrides) loop.setslice(space, impl.get_shape(), impl, self) return impl
def from_shape(space, shape, dtype, order='C', w_instance=None, zero=True): from pypy.module.micronumpy import concrete 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 w_instance: return wrap_impl(space, space.type(w_instance), w_instance, impl) return W_NDimArray(impl)
def get_view(self, space, orig_array, dtype, new_shape): strides, backstrides = calc_strides(new_shape, dtype, self.order) return SliceArray(self.start, strides, backstrides, new_shape, self, orig_array, dtype=dtype)
def from_shape_and_storage(space, shape, storage, dtype, storage_bytes=-1, order='C', owning=False, w_subtype=None, w_base=None, writable=True, strides=None, start=0): from pypy.module.micronumpy import concrete from pypy.module.micronumpy.strides import (calc_strides, calc_backstrides) isize = dtype.elsize if len(shape) > NPY.MAXDIMS: raise oefmt(space.w_ValueError, "sequence too large; must be smaller than %d", NPY.MAXDIMS) try: totalsize = ovfcheck(support.product_check(shape) * isize) except OverflowError as e: raise oefmt(space.w_ValueError, "array is too big.") if storage_bytes > 0 : if totalsize > storage_bytes: raise OperationError(space.w_TypeError, space.wrap( "buffer is too small for requested array")) else: storage_bytes = totalsize if strides is None: strides, backstrides = calc_strides(shape, dtype, order) else: if len(strides) != len(shape): raise oefmt(space.w_ValueError, 'strides, if given, must be the same length as shape') for i in range(len(strides)): if strides[i] < 0 or strides[i]*shape[i] > storage_bytes: raise oefmt(space.w_ValueError, 'strides is incompatible with shape of requested ' 'array and size of buffer') backstrides = calc_backstrides(strides, shape) if w_base is not None: if owning: raise OperationError(space.w_ValueError, space.wrap("Cannot have owning=True when specifying a buffer")) if writable: impl = concrete.ConcreteArrayWithBase(shape, dtype, order, strides, backstrides, storage, w_base, start=start) else: impl = concrete.ConcreteNonWritableArrayWithBase(shape, dtype, order, strides, backstrides, storage, w_base) elif owning: # Will free storage when GCd impl = concrete.ConcreteArray(shape, dtype, order, strides, backstrides, storage=storage) else: impl = concrete.ConcreteArrayNotOwning(shape, dtype, order, strides, backstrides, storage) if w_subtype: w_ret = space.allocate_instance(W_NDimArray, w_subtype) W_NDimArray.__init__(w_ret, impl) space.call_method(w_ret, '__array_finalize__', w_subtype) return w_ret return W_NDimArray(impl)
def from_shape_and_storage(space, shape, storage, dtype, storage_bytes=-1, order='C', owning=False, w_subtype=None, w_base=None, writable=True, strides=None, start=0): from pypy.module.micronumpy import concrete from pypy.module.micronumpy.strides import (calc_strides, calc_backstrides) isize = dtype.elsize if len(shape) > NPY.MAXDIMS: raise oefmt(space.w_ValueError, "sequence too large; must be smaller than %d", NPY.MAXDIMS) try: totalsize = support.product(shape) * isize except OverflowError as e: raise oefmt(space.w_ValueError, "array is too big") if storage_bytes > 0 : if totalsize > storage_bytes: raise OperationError(space.w_TypeError, space.wrap( "buffer is too small for requested array")) else: storage_bytes = totalsize if strides is None: strides, backstrides = calc_strides(shape, dtype, order) else: if len(strides) != len(shape): raise oefmt(space.w_ValueError, 'strides, if given, must be the same length as shape') for i in range(len(strides)): if strides[i] < 0 or strides[i]*shape[i] > storage_bytes: raise oefmt(space.w_ValueError, 'strides is incompatible with shape of requested ' 'array and size of buffer') backstrides = calc_backstrides(strides, shape) if w_base is not None: if owning: raise OperationError(space.w_ValueError, space.wrap("Cannot have owning=True when specifying a buffer")) if writable: impl = concrete.ConcreteArrayWithBase(shape, dtype, order, strides, backstrides, storage, w_base, start=start) else: impl = concrete.ConcreteNonWritableArrayWithBase(shape, dtype, order, strides, backstrides, storage, w_base) elif owning: # Will free storage when GCd impl = concrete.ConcreteArray(shape, dtype, order, strides, backstrides, storage=storage) else: impl = concrete.ConcreteArrayNotOwning(shape, dtype, order, strides, backstrides, storage) if w_subtype: w_ret = space.allocate_instance(W_NDimArray, w_subtype) W_NDimArray.__init__(w_ret, impl) space.call_method(w_ret, '__array_finalize__', w_subtype) return w_ret return W_NDimArray(impl)
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 set_shape(self, space, orig_array, new_shape): if len(new_shape) > NPY.MAXDIMS: raise oefmt(space.w_ValueError, "sequence too large; cannot be greater than %d", NPY.MAXDIMS) try: ovfcheck(support.product_check(new_shape) * self.dtype.elsize) except OverflowError as e: raise oefmt(space.w_ValueError, "array is too big.") strides, backstrides = calc_strides(new_shape, self.dtype, self.order) return SliceArray(self.start, strides, backstrides, new_shape, self, orig_array)
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 reshape(self, orig_array, new_shape): # Since we got to here, prod(new_shape) == self.size new_strides = None if self.size == 0: new_strides, _ = calc_strides(new_shape, self.dtype, self.order) else: if len(self.get_shape()) == 0: new_strides = [self.dtype.elsize] * len(new_shape) else: new_strides = calc_new_strides(new_shape, self.get_shape(), self.get_strides(), self.order) if new_strides is not None: # We can create a view, strides somehow match up. new_backstrides = calc_backstrides(new_strides, new_shape) assert isinstance(orig_array, W_NDimArray) or orig_array is None return SliceArray(self.start, new_strides, new_backstrides, new_shape, self, orig_array)
def astype(self, space, dtype, order): # copy the general pattern of the strides # but make the array storage contiguous in memory shape = self.get_shape() strides = self.get_strides() if order not in ('C', 'F'): raise oefmt(space.w_ValueError, "Unknown order %s in astype", order) if len(strides) == 0: t_strides = [] backstrides = [] elif order != self.order: t_strides, backstrides = calc_strides(shape, dtype, order) else: mins = strides[0] t_elsize = dtype.elsize for s in strides: if s < mins: mins = s t_strides = [s * t_elsize / mins for s in strides] backstrides = calc_backstrides(t_strides, shape) impl = ConcreteArray(shape, dtype, order, t_strides, backstrides) loop.setslice(space, impl.get_shape(), impl, self) return impl
def copy(self, space): strides, backstrides = calc_strides(self.get_shape(), self.dtype, self.order) impl = ConcreteArray(self.get_shape(), self.dtype, self.order, strides, backstrides) return loop.setslice(space, self.get_shape(), impl, self)
def set_shape(self, space, orig_array, new_shape): strides, backstrides = calc_strides(new_shape, self.dtype, self.order) return SliceArray(self.start, strides, backstrides, new_shape, self, orig_array)
def get_view(self, space, orig_array, dtype, new_shape, strides=None, backstrides=None): if not strides: strides, backstrides = calc_strides(new_shape, dtype, self.order) return SliceArray(self.start, strides, backstrides, new_shape, self, orig_array, dtype=dtype)