Author: Maciej Fijalkowski <[email protected]>
Branch: missing-ndarray-attributes
Changeset: r58525:b0e0c785b8dc
Date: 2012-10-28 00:05 +0200
http://bitbucket.org/pypy/pypy/changeset/b0e0c785b8dc/

Log:    implement astype

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
@@ -262,6 +262,11 @@
     def argsort(self, space, w_axis):
         return argsort_array(self, space, w_axis)
 
+    def astype(self, space, dtype):
+        new_arr = W_NDimArray.from_shape(self.get_shape(), dtype)
+        loop.copy_from_to(self, new_arr.implementation, dtype)
+        return new_arr
+
 class SliceArray(BaseConcreteArray):
     def __init__(self, start, strides, backstrides, shape, parent, dtype=None):
         self.strides = strides
diff --git a/pypy/module/micronumpy/arrayimpl/scalar.py 
b/pypy/module/micronumpy/arrayimpl/scalar.py
--- a/pypy/module/micronumpy/arrayimpl/scalar.py
+++ b/pypy/module/micronumpy/arrayimpl/scalar.py
@@ -98,3 +98,6 @@
 
     def argsort(self, space, w_axis):
         return space.wrap(0)
+
+    def astype(self, space, dtype):
+        return W_NDimArray.new_scalar(space, dtype, self.value)
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
@@ -384,9 +384,10 @@
         contig = self.descr_copy(space)
         return contig.implementation.argsort(space, w_axis)
 
-    def descr_astype(self, space, w_type):
-        raise OperationError(space.w_NotImplementedError, space.wrap(
-            "astype not implemented yet"))
+    def descr_astype(self, space, w_dtype):
+        dtype = space.interp_w(interp_dtype.W_Dtype,
+          space.call_function(space.gettypefor(interp_dtype.W_Dtype), w_dtype))
+        return self.implementation.astype(space, dtype)
 
     def descr_base(self, space):
         raise OperationError(space.w_NotImplementedError, space.wrap(
@@ -788,6 +789,7 @@
     imag = GetSetProperty(W_NDimArray.descr_get_imag),
 
     argsort = interp2app(W_NDimArray.descr_argsort),
+    astype = interp2app(W_NDimArray.descr_astype),
 
     __array_interface__ = GetSetProperty(W_NDimArray.descr_array_iface),
 )
diff --git a/pypy/module/micronumpy/loop.py b/pypy/module/micronumpy/loop.py
--- a/pypy/module/micronumpy/loop.py
+++ b/pypy/module/micronumpy/loop.py
@@ -460,3 +460,16 @@
                           val_arr.descr_getitem(space, w_idx))
         iter.next()
 
+copy_from_to_driver = jit.JitDriver(greens = ['dtype'],
+                                    reds = ['from_iter', 'to_iter'])
+
+def copy_from_to(from_, to, dtype):
+    from_iter = from_.create_iter(from_.get_shape())
+    to_iter = to.create_iter(to.get_shape())
+    while not from_iter.done():
+        copy_from_to_driver.jit_merge_point(dtype=dtype, from_iter=from_iter,
+                                            to_iter=to_iter)
+        to_iter.setitem(from_iter.getitem().convert_to(dtype))
+        to_iter.next()
+        from_iter.next()
+
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
@@ -1567,6 +1567,15 @@
             a = arange(100)
             assert (a.argsort() == a).all()
 
+    def test_astype(self):
+        from _numpypy import array
+        b = array(1).astype(float)
+        assert b == 1
+        assert b.dtype == float
+        b = array([1, 2]).astype(float)
+        assert (b == [1, 2]).all()
+        assert b.dtype == 'float'
+
 class AppTestMultiDim(BaseNumpyAppTest):
     def test_init(self):
         import _numpypy
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to