[pypy-commit] pypy numpy-refactor: reshape
fijal
noreply at buildbot.pypy.org
Thu Aug 30 19:45:15 CEST 2012
Author: Maciej Fijalkowski <fijall at gmail.com>
Branch: numpy-refactor
Changeset: r57022:acbda6aaf5e3
Date: 2012-08-30 17:44 +0200
http://bitbucket.org/pypy/pypy/changeset/acbda6aaf5e3/
Log: reshape
diff --git a/pypy/module/micronumpy/arrayimpl/concrete.py b/pypy/module/micronumpy/arrayimpl/concrete.py
--- a/pypy/module/micronumpy/arrayimpl/concrete.py
+++ b/pypy/module/micronumpy/arrayimpl/concrete.py
@@ -1,6 +1,7 @@
from pypy.module.micronumpy.arrayimpl import base
from pypy.module.micronumpy import support, loop
+from pypy.module.micronumpy.strides import calc_new_strides
from pypy.module.micronumpy.iter import Chunk, Chunks, NewAxisChunk, RecordChunk
from pypy.interpreter.error import OperationError, operationerrfmt
from pypy.rlib import jit
@@ -80,6 +81,24 @@
def get_size(self):
return self.size // self.dtype.itemtype.get_element_size()
+
+ def reshape(self, space, 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.shape,
+ self.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]
+ return SliceArray(self.start, new_strides, new_backstrides,
+ new_shape, self)
+ else:
+ return None
+
# -------------------- applevel get/setitem -----------------------
@jit.unroll_safe
@@ -90,7 +109,7 @@
raise IndexError
idx = int_w(space, w_index)
if idx < 0:
- idx = self.shape[i] + id
+ idx = self.shape[i] + idx
if idx < 0 or idx >= self.shape[0]:
raise operationerrfmt(space.w_IndexError,
"index (%d) out of range (0<=index<%d", i, self.shape[i],
diff --git a/pypy/module/micronumpy/interp_numarray.py b/pypy/module/micronumpy/interp_numarray.py
--- a/pypy/module/micronumpy/interp_numarray.py
+++ b/pypy/module/micronumpy/interp_numarray.py
@@ -5,7 +5,8 @@
from pypy.interpreter.gateway import interp2app, unwrap_spec
from pypy.module.micronumpy import interp_dtype, interp_ufuncs, support
from pypy.module.micronumpy.arrayimpl import create_implementation, create_slice
-from pypy.module.micronumpy.strides import find_shape_and_elems
+from pypy.module.micronumpy.strides import find_shape_and_elems,\
+ get_shape_from_iterable
from pypy.module.micronumpy.interp_support import unwrap_axis_arg
from pypy.tool.sourcetools import func_with_new_name
from pypy.rlib import jit
@@ -101,6 +102,34 @@
arr.implementation = self.implementation.copy()
return arr
+ def descr_reshape(self, space, args_w):
+ """reshape(...)
+ a.reshape(shape)
+
+ Returns an array containing the same data with a new shape.
+
+ Refer to `numpypy.reshape` for full documentation.
+
+ See Also
+ --------
+ numpypy.reshape : equivalent function
+ """
+ if len(args_w) == 1:
+ w_shape = args_w[0]
+ else:
+ w_shape = space.newtuple(args_w)
+ new_shape = get_shape_from_iterable(space, self.get_size(), w_shape)
+ new_impl = self.implementation.reshape(space, new_shape)
+ if new_impl is not None:
+ self.implementation = new_impl
+ return self
+ else:
+ # Create copy with contiguous data
+ arr = self.descr_copy(space)
+ arr.implementation = arr.implementation.reshape(space, new_shape)
+ return arr
+
+
# --------------------- binary operations ----------------------------
def _binop_impl(ufunc_name):
@@ -210,6 +239,7 @@
#std = interp2app(W_NDimArray.descr_std),
copy = interp2app(W_NDimArray.descr_copy),
+ reshape = interp2app(W_NDimArray.descr_reshape),
)
def decode_w_dtype(space, w_dtype):
More information about the pypy-commit
mailing list