def set_shape(self, space, orig_array, new_shape): if len(self.get_shape()) < 2 or self.size == 0: # TODO: this code could be refactored into calc_strides # but then calc_strides would have to accept a stepping factor strides = [] backstrides = [] dtype = self.dtype try: s = self.get_strides()[0] // dtype.elsize except IndexError: s = 1 if self.order == 'C': new_shape.reverse() for sh in new_shape: strides.append(s * dtype.elsize) backstrides.append(s * (sh - 1) * dtype.elsize) s *= max(1, sh) if self.order == 'C': strides.reverse() backstrides.reverse() new_shape.reverse() return self.__class__(self.start, strides, backstrides, new_shape, self, orig_array) new_strides = calc_new_strides(new_shape, self.get_shape(), self.get_strides(), self.order) if new_strides is None or len(new_strides) != len(new_shape): raise oefmt(space.w_AttributeError, "incompatible shape for a non-contiguous array") new_backstrides = [0] * len(new_shape) for nd in range(len(new_shape)): new_backstrides[nd] = (new_shape[nd] - 1) * new_strides[nd] return self.__class__(self.start, new_strides, new_backstrides, new_shape, self, orig_array)
def set_shape(self, space, orig_array, new_shape): if len(self.get_shape()) < 2 or self.size == 0: # TODO: this code could be refactored into calc_strides # but then calc_strides would have to accept a stepping factor strides = [] backstrides = [] dtype = self.dtype s = self.get_strides()[0] // dtype.get_size() if self.order == 'C': new_shape.reverse() for sh in new_shape: strides.append(s * dtype.get_size()) backstrides.append(s * (sh - 1) * dtype.get_size()) s *= max(1, sh) if self.order == 'C': strides.reverse() backstrides.reverse() new_shape.reverse() return SliceArray(self.start, strides, backstrides, new_shape, self, orig_array) new_strides = calc_new_strides(new_shape, self.get_shape(), self.get_strides(), self.order) if new_strides is None: raise OperationError( space.w_AttributeError, space.wrap("incompatible shape for a non-contiguous array")) new_backstrides = [0] * len(new_shape) for nd in range(len(new_shape)): new_backstrides[nd] = (new_shape[nd] - 1) * new_strides[nd] return SliceArray(self.start, new_strides, new_backstrides, new_shape, self, orig_array)
def set_shape(self, space, orig_array, new_shape): if len(self.get_shape()) < 2 or self.size == 0: # TODO: this code could be refactored into calc_strides # but then calc_strides would have to accept a stepping factor strides = [] backstrides = [] dtype = self.dtype s = self.get_strides()[0] // dtype.get_size() if self.order == 'C': new_shape.reverse() for sh in new_shape: strides.append(s * dtype.get_size()) backstrides.append(s * (sh - 1) * dtype.get_size()) s *= max(1, sh) if self.order == 'C': strides.reverse() backstrides.reverse() new_shape.reverse() return SliceArray(self.start, strides, backstrides, new_shape, self, orig_array) new_strides = calc_new_strides(new_shape, self.get_shape(), self.get_strides(), self.order) if new_strides is None: raise OperationError(space.w_AttributeError, space.wrap( "incompatible shape for a non-contiguous array")) new_backstrides = [0] * len(new_shape) for nd in range(len(new_shape)): new_backstrides[nd] = (new_shape[nd] - 1) * new_strides[nd] return SliceArray(self.start, new_strides, new_backstrides, new_shape, self, orig_array)
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 reshape(self, space, orig_array, new_shape): # Since we got to here, prod(new_shape) == self.size new_strides = None if self.size > 0: new_strides = calc_new_strides(new_shape, self.get_shape(), self.get_strides(), self.order) if new_strides: # We can create a view, strides somehow match up. ndims = len(new_shape) new_backstrides = [0] * ndims for nd in range(ndims): new_backstrides[nd] = (new_shape[nd] - 1) * new_strides[nd] assert isinstance(orig_array, W_NDimArray) or orig_array is None return SliceArray(self.start, new_strides, new_backstrides, new_shape, self, orig_array) else: return None
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.") if len(self.get_shape()) < 2 or self.size == 0: # TODO: this code could be refactored into calc_strides # but then calc_strides would have to accept a stepping factor strides = [] backstrides = [] dtype = self.dtype try: s = self.get_strides()[0] // dtype.elsize except IndexError: s = 1 if self.order != NPY.FORTRANORDER: new_shape.reverse() for sh in new_shape: strides.append(s * dtype.elsize) backstrides.append(s * (sh - 1) * dtype.elsize) s *= max(1, sh) if self.order != NPY.FORTRANORDER: strides.reverse() backstrides.reverse() new_shape.reverse() return self.__class__(self.start, strides, backstrides, new_shape, self, orig_array) new_strides = calc_new_strides(new_shape, self.get_shape(), self.get_strides(), self.order) if new_strides is None or len(new_strides) != len(new_shape): raise oefmt(space.w_AttributeError, "incompatible shape for a non-contiguous array") new_backstrides = [0] * len(new_shape) for nd in range(len(new_shape)): new_backstrides[nd] = (new_shape[nd] - 1) * new_strides[nd] return self.__class__(self.start, new_strides, new_backstrides, new_shape, self, orig_array)