Commits

mattip  committed 35ce341

add test,implementation for empty_like

  • Participants
  • Parent commits b08ace6
  • Branches python-numpy

Comments (0)

Files changed (2)

File lib_pypy/numpypy/multiarray/__init__.py

     raise ValueError('not implemented yet')
 
 def count_nonzero(a):
-    if not hasattr(a,'flat'):
-        try:
+    try:
+        if not hasattr(a,'flat'):
             a = ndarray(a)
-            return sum(a.flat != 0)
-        except TypeError:
-            if isinstance(a, (tuple, list)):
-                return len(a)
-        return 1
+        return sum(a.flat != 0)
+    except TypeError:
+        if isinstance(a, (tuple, list)):
+            return len(a)
+    return 1
+
+def empty_like(a, dtype=None, order='K', subok=True):
+    if not hasattr(a,'dtype'):
+        a = ndarray(a)
+    if dtype is None:
+        dtype = a.dtype
+    #return zeros(a.shape, dtype=dtype, order=order, subok=subok)
+    return zeros(a.shape, dtype=dtype)

File lib_pypy/numpypy/test/test_multiarray.py

 def test_count_nonzero():
    a = np.array([[1, 1], [1, 1]])
    assert multiarray.count_nonzero(a) == 4
-   raises(TypeError, multiarray.count_nonzero, 'a')
+   assert multiarray.count_nonzero('a') == 1
+   assert multiarray.count_nonzero(('a',2)) == 2 
+
+def test_empty_like():
+   a = np.array([[1, 1], [1, 1]])
+   b = multiarray.empty_like(a)
+   b[0,0] = 100
+   assert b[0,0] != a[0,0]
+   assert b.shape == a.shape
+   assert b.dtype == a.dtype
+   b = multiarray.empty_like(a, dtype=float)
+   assert b.dtype == np.dtype(float)