Commits

Brian Kearns committed a0190dd

add multiarray.empty_like()

Comments (0)

Files changed (3)

pypy/module/micronumpy/__init__.py

         'zeros': 'interp_numarray.zeros',
         'empty': 'interp_numarray.zeros',
         'ones': 'interp_numarray.ones',
+        'empty_like': 'interp_numarray.empty_like',
         '_reconstruct' : 'interp_numarray._reconstruct',
         'scalar' : 'interp_numarray.build_scalar',
         'dot': 'interp_arrayops.dot',

pypy/module/micronumpy/interp_numarray.py

     w_arr.fill(space, one)
     return space.wrap(w_arr)
 
+@unwrap_spec(subok=bool)
+def empty_like(space, w_a, w_dtype=None, w_order=None, subok=True):
+    w_a = convert_to_array(space, w_a)
+    if subok and type(w_a) is not W_NDimArray:
+        raise OperationError(space.w_NotImplementedError, space.wrap(
+            "subtypes not implemented"))
+    if w_dtype is None:
+        w_dtype = w_a.get_dtype()
+    return zeros(space, w_a.descr_get_shape(space), w_dtype)
+
 def _reconstruct(space, w_subtype, w_shape, w_dtype):
     return descr_new_array(space, w_subtype, w_shape, w_dtype)
 

pypy/module/micronumpy/test/test_numarray.py

         # TypeError
         raises((TypeError, AttributeError), 'x.ndim = 3')
 
-    def test_init(self):
+    def test_zeros(self):
         from numpypy import zeros
         a = zeros(15)
         # Check that storage was actually zero'd.
         assert a[13] == 5.3
         assert zeros(()).shape == ()
 
+    def test_empty_like(self):
+        import numpy as np
+        a = np.zeros((2, 3))
+        assert a.shape == (2, 3)
+        a[0,0] = 1
+        b = np.empty_like(a)
+        assert b.shape == a.shape
+        assert b.dtype == a.dtype
+        assert b[0,0] != 1
+        b = np.empty_like(a, dtype='i4')
+        assert b.shape == a.shape
+        assert b.dtype == np.dtype('i4')
+        assert b[0,0] != 1
+        b = np.empty_like([1,2,3])
+        assert b.shape == (3,)
+        assert b.dtype == np.int_
+
+        class A(np.ndarray):
+            pass
+        import sys
+        if '__pypy__' not in sys.builtin_module_names:
+            b = np.empty_like(A((2, 3)))
+            assert b.shape == (2, 3)
+            assert type(b) is A
+        else:
+            raises(NotImplementedError, np.empty_like, A((2, 3)))
+
     def test_size(self):
         from numpypy import array,arange,cos
         assert array(3).size == 1