[pypy-commit] pypy python-numpy: add test, implementation for empty_like

mattip noreply at buildbot.pypy.org
Fri Aug 10 17:37:48 CEST 2012


Author: mattip <matti.picus at gmail.com>
Branch: python-numpy
Changeset: r56697:35ce3419261f
Date: 2012-08-10 18:36 +0300
http://bitbucket.org/pypy/pypy/changeset/35ce3419261f/

Log:	add test,implementation for empty_like

diff --git a/lib_pypy/numpypy/multiarray/__init__.py b/lib_pypy/numpypy/multiarray/__init__.py
--- a/lib_pypy/numpypy/multiarray/__init__.py
+++ b/lib_pypy/numpypy/multiarray/__init__.py
@@ -40,11 +40,19 @@
     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)
diff --git a/lib_pypy/numpypy/test/test_multiarray.py b/lib_pypy/numpypy/test/test_multiarray.py
--- a/lib_pypy/numpypy/test/test_multiarray.py
+++ b/lib_pypy/numpypy/test/test_multiarray.py
@@ -10,4 +10,15 @@
 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)


More information about the pypy-commit mailing list