[pypy-commit] pypy default: add a few asserts, propagate axis where it works

fijal noreply at buildbot.pypy.org
Sat Jan 21 13:12:06 CET 2012


Author: Maciej Fijalkowski <fijall at gmail.com>
Branch: 
Changeset: r51567:5a6305c3c894
Date: 2012-01-21 13:58 +0200
http://bitbucket.org/pypy/pypy/changeset/5a6305c3c894/

Log:	add a few asserts, propagate axis where it works

diff --git a/lib_pypy/numpypy/core/fromnumeric.py b/lib_pypy/numpypy/core/fromnumeric.py
--- a/lib_pypy/numpypy/core/fromnumeric.py
+++ b/lib_pypy/numpypy/core/fromnumeric.py
@@ -149,6 +149,7 @@
            [5, 6]])
 
     """
+    assert order == 'C'
     if not hasattr(a, 'reshape'):
        a = numpypy.array(a)
     return a.reshape(newshape)
@@ -690,6 +691,7 @@
     1
 
     """
+    assert axis is None
     if not hasattr(a, 'argmax'):
         a = numpypy.array(a)
     return a.argmax()
@@ -705,6 +707,7 @@
         documentation.
 
     """
+    assert axis is None
     if not hasattr(a, 'argmin'):
         a = numpypy.array(a)
     return a.argmin()
@@ -1354,9 +1357,11 @@
     -128
 
     """
+    assert dtype is None
+    assert out is None
     if not hasattr(a, "sum"):
         a = numpypy.array(a)
-    return a.sum()
+    return a.sum(axis=axis)
 
 
 def product (a, axis=None, dtype=None, out=None):
@@ -1382,6 +1387,8 @@
     any : equivalent function
 
     """
+    assert axis is None
+    assert out is None
     if not hasattr(a, 'any'):
         a = numpypy.array(a)
     return a.any()
@@ -1396,6 +1403,8 @@
     numpy.all : Equivalent function; see for details.
 
     """
+    assert axis is None
+    assert out is None
     if not hasattr(a, 'all'):
         a = numpypy.array(a)
     return a.all()
@@ -1464,6 +1473,8 @@
     (191614240, 191614240)
 
     """
+    assert axis is None
+    assert out is None
     if not hasattr(a, 'any'):
         a = numpypy.array(a)
     return a.any()
@@ -1526,6 +1537,8 @@
     (28293632, 28293632, array([ True], dtype=bool))
 
     """
+    assert axis is None
+    assert out is None
     if not hasattr(a, 'all'):
         a = numpypy.array(a)
     return a.all()
@@ -1705,6 +1718,8 @@
     4.0
 
     """
+    assert axis is None
+    assert out is None
     if not hasattr(a, "max"):
         a = numpypy.array(a)
     return a.max()
@@ -1766,6 +1781,8 @@
 
     """
     # amin() is equivalent to min()
+    assert axis is None
+    assert out is None
     if not hasattr(a, 'min'):
         a = numpypy.array(a)
     return a.min()
@@ -2214,9 +2231,11 @@
     0.55000000074505806
 
     """
+    assert dtype is None
+    assert out is None
     if not hasattr(a, "mean"):
         a = numpypy.array(a)
-    return a.mean()
+    return a.mean(axis=axis)
 
 
 def std(a, axis=None, dtype=None, out=None, ddof=0):
@@ -2305,6 +2324,10 @@
     0.44999999925552653
 
     """
+    assert axis is None
+    assert dtype is None
+    assert out is None
+    assert ddof == 0
     if not hasattr(a, "std"):
         a = numpypy.array(a)
     return a.std()
@@ -2398,6 +2421,10 @@
     0.20250000000000001
 
     """
+    assert axis is None
+    assert dtype is None
+    assert out is None
+    assert ddof == 0
     if not hasattr(a, "var"):
         a = numpypy.array(a)
     return a.var()
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
@@ -728,7 +728,7 @@
         assert d[1] == 12
 
     def test_mean(self):
-        from _numpypy import array
+        from _numpypy import array, arange
         a = array(range(5))
         assert a.mean() == 2.0
         assert a[:4].mean() == 1.5
@@ -738,6 +738,7 @@
         assert a.mean(axis=0)[0, 0] == 35
         assert (b == array(range(35, 70), dtype=float).reshape(5, 7)).all()
         assert (a.mean(2) == array(range(0, 15), dtype=float).reshape(3, 5) * 7 + 3).all()
+        assert (arange(10).reshape(5, 2).mean(axis=1) == [0.5, 2.5, 4.5, 6.5, 8.5]).all()
 
     def test_sum(self):
         from _numpypy import array
@@ -1021,11 +1022,15 @@
         assert a[0].tolist() == [17.1, 27.2]
 
     def test_var(self):
-        from _numpypy import array
+        from _numpypy import array, arange
         a = array(range(10))
         assert a.var() == 8.25
         a = array([5.0])
         assert a.var() == 0.0
+        a = arange(10).reshape(5, 2)
+        assert a.var() == 8.25
+        #assert (a.var(0) == [8, 8]).all()
+        #assert (a.var(1) == [.25] * 5).all()
 
     def test_std(self):
         from _numpypy import array
diff --git a/pypy/module/test_lib_pypy/numpypy/core/test_fromnumeric.py b/pypy/module/test_lib_pypy/numpypy/core/test_fromnumeric.py
--- a/pypy/module/test_lib_pypy/numpypy/core/test_fromnumeric.py
+++ b/pypy/module/test_lib_pypy/numpypy/core/test_fromnumeric.py
@@ -18,8 +18,8 @@
         from numpypy import array, arange, argmin
         a = arange(6).reshape((2,3))
         assert argmin(a) == 0
-        assert (argmin(a, axis=0) == array([0, 0, 0])).all()
-        assert (argmin(a, axis=1) == array([0, 0])).all()
+        #assert (argmin(a, axis=0) == array([0, 0, 0])).all()
+        #assert (argmin(a, axis=1) == array([0, 0])).all()
         b = arange(6)
         b[1] = 0
         assert argmin(b) == 0
@@ -40,8 +40,8 @@
         assert sum([0.5, 1.5])== 2.0
         assert sum([[0, 1], [0, 5]]) == 6
         # assert sum([0.5, 0.7, 0.2, 1.5], dtype=int32) == 1
-        # assert (sum([[0, 1], [0, 5]], axis=0) == array([0, 6])).all()
-        # assert (sum([[0, 1], [0, 5]], axis=1) == array([1, 5])).all()
+        assert (sum([[0, 1], [0, 5]], axis=0) == array([0, 6])).all()
+        assert (sum([[0, 1], [0, 5]], axis=1) == array([1, 5])).all()
         # If the accumulator is too small, overflow occurs:
         # assert ones(128, dtype=int8).sum(dtype=int8) == -128
 
@@ -98,20 +98,22 @@
         from numpypy import array, var
         a = array([[1,2],[3,4]])
         assert var(a) == 1.25
-        # assert (np.var(a,0) == array([ 1.,  1.])).all()
-        # assert (np.var(a,1) == array([ 0.25,  0.25])).all()
+        #assert (var(a,0) == array([ 1.,  1.])).all()
+        #assert (var(a,1) == array([ 0.25,  0.25])).all()
 
     def test_std(self):
         from numpypy import array, std
         a = array([[1, 2], [3, 4]])
         assert std(a) ==  1.1180339887498949
-        # assert (std(a, axis=0) == array([ 1.,  1.])).all()
-        # assert (std(a, axis=1) == array([ 0.5,  0.5]).all()
+        #assert (std(a, axis=0) == array([ 1.,  1.])).all()
+        #assert (std(a, axis=1) == array([ 0.5,  0.5])).all()
 
     def test_mean(self):
-        from numpypy import array, mean
+        from numpypy import array, mean, arange
         assert mean(array(range(5))) == 2.0
         assert mean(range(5)) == 2.0
+        assert (mean(arange(10).reshape(5, 2), axis=0) == [4, 5]).all()
+        assert (mean(arange(10).reshape(5, 2), axis=1) == [0.5, 2.5, 4.5, 6.5, 8.5]).all()
 
     def test_reshape(self):
         from numpypy import arange, array, dtype, reshape


More information about the pypy-commit mailing list