[pypy-commit] pypy ndarray-view: implementation wip
mattip
noreply at buildbot.pypy.org
Fri Jun 28 14:40:42 CEST 2013
Author: Matti Picus <matti.picus at gmail.com>
Branch: ndarray-view
Changeset: r65059:c38b51152a62
Date: 2013-06-28 14:09 +0300
http://bitbucket.org/pypy/pypy/changeset/c38b51152a62/
Log: implementation wip
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
@@ -75,9 +75,11 @@
else:
return None
- def get_view(self, dtype, new_shape):
- pass
- #return SliceArray(self.start,
+ def get_view(self, orig_array, dtype, new_shape):
+ strides, backstrides = support.calc_strides(new_shape, dtype,
+ self.order)
+ return SliceArray(self.start, strides, backstrides, new_shape,
+ self, orig_array, dtype=dtype)
def get_real(self, orig_array):
strides = self.get_strides()
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
@@ -627,7 +627,24 @@
w_dtype))
else:
dtype = self.get_dtype()
- return W_NDimArray(self.implementation.get_view(self, dtype))
+ factor = float(dtype.get_size()) / self.get_dtype().get_size()
+ print 'new dtype',dtype,'self dtype',self.get_dtype(),'factor',factor
+ if self.get_size() % factor != 0:
+ raise OperationError(space.w_ValueError, space.wrap(
+ "new type not compatible with array."))
+ new_shape = self.get_shape()
+ impl = self.implementation
+ if impl.get_strides()[0] < impl.get_strides()[-1]:
+ new_shape[0] = int(new_shape[0] * factor)
+ if new_shape[0] == 0:
+ raise OperationError(space.w_ValueError, space.wrap(
+ "new type not compatible with array shape"))
+ else:
+ new_shape[-1] = int(new_shape[-1] * factor)
+ if new_shape[-1] == 0:
+ raise OperationError(space.w_ValueError, space.wrap(
+ "new type not compatible with array shape"))
+ return W_NDimArray(self.implementation.get_view(self, dtype, new_shape))
# --------------------- operations ----------------------------
diff --git a/pypy/module/micronumpy/test/test_numarray.py b/pypy/module/micronumpy/test/test_numarray.py
--- a/pypy/module/micronumpy/test/test_numarray.py
+++ b/pypy/module/micronumpy/test/test_numarray.py
@@ -1415,6 +1415,7 @@
from numpypy import array, int8, int16, dtype, int64
x = array([(1, 2)], dtype=[('a', int8), ('b', int8)])
y = x.view(dtype=int16)
+ print y,y.shape
assert y[0] == 513
assert y.dtype == dtype('int16')
y[0] = 670
More information about the pypy-commit
mailing list