Author: Maciej Fijalkowski <[email protected]>
Branch: numpy-refactor
Changeset: r57262:8b1436d4cc3a
Date: 2012-09-10 15:50 +0200
http://bitbucket.org/pypy/pypy/changeset/8b1436d4cc3a/

Log:    count_nonzero

diff --git a/pypy/module/micronumpy/__init__.py 
b/pypy/module/micronumpy/__init__.py
--- a/pypy/module/micronumpy/__init__.py
+++ b/pypy/module/micronumpy/__init__.py
@@ -31,6 +31,7 @@
         'concatenate': 'interp_arrayops.concatenate',
         'repeat': 'interp_arrayops.repeat',
         'where': 'interp_arrayops.where',
+        'count_nonzero': 'interp_arrayops.count_nonzero',
 
         'set_string_function': 'appbridge.set_string_function',
 
diff --git a/pypy/module/micronumpy/interp_arrayops.py 
b/pypy/module/micronumpy/interp_arrayops.py
--- a/pypy/module/micronumpy/interp_arrayops.py
+++ b/pypy/module/micronumpy/interp_arrayops.py
@@ -136,3 +136,6 @@
                                  orig_size)
             
Chunks(chunks).apply(res.implementation).implementation.setslice(space, arr)
     return res
+
+def count_nonzero(space, w_obj):
+    return space.wrap(loop.count_all_true(convert_to_array(space, w_obj)))
diff --git a/pypy/module/micronumpy/interp_boxes.py 
b/pypy/module/micronumpy/interp_boxes.py
--- a/pypy/module/micronumpy/interp_boxes.py
+++ b/pypy/module/micronumpy/interp_boxes.py
@@ -34,6 +34,8 @@
     def convert_to(self, dtype):
         return dtype.box(self.value)
 
+    def is_true(self):
+        return bool(self.value)
 
 class W_GenericBox(Wrappable):
     _attrs_ = ()
@@ -143,7 +145,6 @@
     def item(self, space):
         return self.get_dtype(space).itemtype.to_builtin_type(space, self)
 
-
 class W_BoolBox(W_GenericBox, PrimitiveBox):
     descr__new__, _get_dtype = new_dtype_getter("bool")
 
diff --git a/pypy/module/micronumpy/interp_ufuncs.py 
b/pypy/module/micronumpy/interp_ufuncs.py
--- a/pypy/module/micronumpy/interp_ufuncs.py
+++ b/pypy/module/micronumpy/interp_ufuncs.py
@@ -148,8 +148,7 @@
         obj = convert_to_array(space, w_obj)
         obj_shape = obj.get_shape()
         if obj.is_scalar():
-            raise OperationError(space.w_TypeError, space.wrap("cannot reduce "
-                "on a scalar"))
+            return obj.get_scalar_value()
         shapelen = len(obj_shape)
         axis = unwrap_axis_arg(space, shapelen, w_axis)    
         assert axis>=0
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
@@ -160,6 +160,8 @@
 
 def count_all_true(arr):
     s = 0
+    if arr.is_scalar():
+        return arr.get_scalar_value().is_true()
     iter = arr.create_iter()
     while not iter.done():
         s += iter.getitem_bool()
diff --git a/pypy/module/micronumpy/test/test_ufuncs.py 
b/pypy/module/micronumpy/test/test_ufuncs.py
--- a/pypy/module/micronumpy/test/test_ufuncs.py
+++ b/pypy/module/micronumpy/test/test_ufuncs.py
@@ -113,6 +113,7 @@
         assert (divide(array([-10]), array([2])) == array([-5])).all()
 
     def test_true_divide(self):
+        import math
         from _numpypy import array, true_divide
 
         a = array([0, 1, 2, 3, 4, 1, -1])
@@ -626,14 +627,16 @@
             ]:
                 assert ufunc(a, b) == func(a, b)
 
+
     def test_count_nonzero(self):
-        from _numpypy import where, count_nonzero, arange
-        a = arange(10)
-        assert count_nonzero(a) == 9
-        a[9] = 0
-        assert count_nonzero(a) == 8
+        from _numpypy import count_nonzero
+        assert count_nonzero(0) == 0
+        assert count_nonzero(1) == 1
+        assert count_nonzero([]) == 0
+        assert count_nonzero([1, 2, 0]) == 2
+        assert count_nonzero([[1, 2, 0], [1, 0, 2]]) == 4
 
-    def test_true_divide(self):
+    def test_true_divide_2(self):
         from _numpypy import arange, array, true_divide
         assert (true_divide(arange(3), array([2, 2, 2])) == array([0, 0.5, 
1])).all()
 
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to