Exemple #1
0
 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
Exemple #2
0
 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
Exemple #3
0
 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
Exemple #4
0
 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)
Exemple #5
0
 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)
Exemple #6
0
 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)
Exemple #7
0
 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)
Exemple #8
0
 def astype(self, space, dtype):
     # copy the general pattern of the strides
     # but make the array storage contiguous in memory
     shape = self.get_shape()
     strides = self.get_strides()
     if len(strides) > 0:
         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)
     else:
         t_strides = []
         backstrides = []
     impl = ConcreteArray(shape, dtype, self.order, t_strides, backstrides)
     loop.setslice(space, impl.get_shape(), impl, self)
     return impl
Exemple #9
0
 def astype(self, space, dtype):
     # copy the general pattern of the strides
     # but make the array storage contiguous in memory
     shape = self.get_shape()
     strides = self.get_strides()
     if len(strides) > 0:
         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)
     else:
         t_strides = []
         backstrides = []
     impl = ConcreteArray(shape, dtype, self.order, t_strides, backstrides)
     loop.setslice(space, impl.get_shape(), impl, self)
     return impl
Exemple #10
0
 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