[Numpy-svn] r5485 - trunk/numpy/core/tests

numpy-svn@scip... numpy-svn@scip...
Mon Jul 21 01:30:07 CDT 2008


Author: alan.mcintyre
Date: 2008-07-21 01:29:58 -0500 (Mon, 21 Jul 2008)
New Revision: 5485

Modified:
   trunk/numpy/core/tests/test_defmatrix.py
   trunk/numpy/core/tests/test_multiarray.py
   trunk/numpy/core/tests/test_numeric.py
   trunk/numpy/core/tests/test_records.py
   trunk/numpy/core/tests/test_regression.py
Log:
Added tests to improve coverage of numpy.core.
Removed unused methods from Vec class in test_numeric.py.
Renamed test in test_regression so it would be seen by nose.
Removed commented test_vector_elements test in test_defmatrix.py (same 
tems are now tested in test_matrix_element).


Modified: trunk/numpy/core/tests/test_defmatrix.py
===================================================================
--- trunk/numpy/core/tests/test_defmatrix.py	2008-07-21 00:28:13 UTC (rev 5484)
+++ trunk/numpy/core/tests/test_defmatrix.py	2008-07-21 06:29:58 UTC (rev 5485)
@@ -17,11 +17,37 @@
         assert all(B.A == D)
         assert all(C.A == D)
 
+        E = array([[5,6],[7,8]])
+        AEresult = matrix([[1,2,5,6],[3,4,7,8]])
+        assert all(bmat([A,E]) == AEresult)
+
         vec = arange(5)
         mvec = matrix(vec)
         assert mvec.shape == (1,5)
 
+    def test_bmat_nondefault_str(self):
+        A = array([[1,2],[3,4]])
+        B = array([[5,6],[7,8]])
+        Aresult = array([[1,2,1,2],
+                         [3,4,3,4],
+                         [1,2,1,2],
+                         [3,4,3,4]])
+        Bresult = array([[5,6,5,6],
+                         [7,8,7,8],
+                         [5,6,5,6],
+                         [7,8,7,8]])
+        mixresult = array([[1,2,5,6],
+                           [3,4,7,8],
+                           [5,6,1,2],
+                           [7,8,3,4]])
+        assert all(bmat("A,A;A,A") == Aresult)
+        assert all(bmat("A,A;A,A",ldict={'A':B}) == Aresult)
+        assert_raises(TypeError, bmat, "A,A;A,A",gdict={'A':B})
+        assert all(bmat("A,A;A,A",ldict={'A':A},gdict={'A':B}) == Aresult)
+        b2 = bmat("A,B;C,D",ldict={'A':A,'B':B},gdict={'C':B,'D':A})
+        assert all(b2 == mixresult)
 
+
 class TestProperties(TestCase):
     def test_sum(self):
         """Test whether matrix.sum(axis=1) preserves orientation.
@@ -38,6 +64,34 @@
         assert_array_equal(sum1, M.sum(axis=1))
         assert sumall == M.sum()
 
+
+    def test_prod(self):
+        x = matrix([[1,2,3],[4,5,6]])
+        assert x.prod() == 720
+        assert all(x.prod(0) == matrix([[4,10,18]]))
+        assert all(x.prod(1) == matrix([[6],[120]]))
+
+        y = matrix([0,1,3])
+        assert y.prod() == 0
+
+    def test_max(self):
+        x = matrix([[1,2,3],[4,5,6]])
+        assert x.max() == 6
+        assert all(x.max(0) == matrix([[4,5,6]]))
+        assert all(x.max(1) == matrix([[3],[6]]))
+
+    def test_min(self):
+        x = matrix([[1,2,3],[4,5,6]])
+        assert x.min() == 1
+        assert all(x.min(0) == matrix([[1,2,3]]))
+        assert all(x.min(1) == matrix([[1],[4]]))
+
+    def test_ptp(self):
+        x = np.arange(4).reshape((2,2))
+        assert x.ptp() == 3
+        assert all(x.ptp(0) == array([2, 2]))
+        assert all(x.ptp(1) == array([1, 1]))
+
     def test_basic(self):
         import numpy.linalg as linalg
 
@@ -55,6 +109,13 @@
         assert all(array(transpose(B) == mB.T))
         assert all(array(conjugate(transpose(B)) == mB.H))
 
+    def test_pinv(self):
+        x = matrix(arange(6).reshape(2,3))
+        xpinv = matrix([[-0.77777778,  0.27777778],
+                        [-0.11111111,  0.11111111],
+                        [ 0.55555556, -0.05555556]])
+        assert_almost_equal(x.I, xpinv)
+
     def test_comparisons(self):
         A = arange(100).reshape(10,10)
         mA = matrix(A)
@@ -241,14 +302,9 @@
         assert isinstance(x, matrix)
         assert_equal(x, matrix([[4,  3], [1,  2]]))
 
-#     def test_vector_element(self):
-#         x = matrix([[1,2,3],[4,5,6]])
-#         assert_equal(x[0][0],1)
-#         assert_equal(x[0].shape,(1,3))
-#         assert_equal(x[:,0].shape,(2,1))
-
     def test_matrix_element(self):
         x = matrix([[1,2,3],[4,5,6]])
+        assert_equal(x[0][0],matrix([[1,2,3]]))
         assert_equal(x[0][0].shape,(1,3))
         assert_equal(x[0].shape,(1,3))
         assert_equal(x[:,0].shape,(2,1))

Modified: trunk/numpy/core/tests/test_multiarray.py
===================================================================
--- trunk/numpy/core/tests/test_multiarray.py	2008-07-21 00:28:13 UTC (rev 5484)
+++ trunk/numpy/core/tests/test_multiarray.py	2008-07-21 06:29:58 UTC (rev 5485)
@@ -397,6 +397,29 @@
         # d.sort(axis=None)
         #assert_equal(d, c, "test sort with axis=None")
 
+
+    def test_sort_order(self):
+        # Test sorting an array with fields
+        x1=np.array([21,32,14])
+        x2=np.array(['my','first','name'])
+        x3=np.array([3.1,4.5,6.2])
+        r=np.rec.fromarrays([x1,x2,x3],names='id,word,number')
+
+        r.sort(order=['id'])
+        assert_equal(r.id, array([14,21,32]))
+        assert_equal(r.word, array(['name','my','first']))
+        assert_equal(r.number, array([6.2,3.1,4.5]))
+
+        r.sort(order=['word'])
+        assert_equal(r.id, array([32,21,14]))
+        assert_equal(r.word, array(['first','my','name']))
+        assert_equal(r.number, array([4.5,3.1,6.2]))
+
+        r.sort(order=['number'])
+        assert_equal(r.id, array([21,32,14]))
+        assert_equal(r.word, array(['my','first','name']))
+        assert_equal(r.number, array([3.1,4.5,6.2]))
+
     def test_argsort(self):
         # all c scalar argsorts use the same code with different types
         # so it suffices to run a quick check with one type. The number

Modified: trunk/numpy/core/tests/test_numeric.py
===================================================================
--- trunk/numpy/core/tests/test_numeric.py	2008-07-21 00:28:13 UTC (rev 5484)
+++ trunk/numpy/core/tests/test_numeric.py	2008-07-21 06:29:58 UTC (rev 5485)
@@ -23,13 +23,6 @@
         return out
     def __rmul__(self,other):
         return self*other
-    def __abs__(self):
-        out=Vec()
-        out.array=abs(self.array)
-        return out
-    def __repr__(self):
-        return "Vec("+repr(self.array.tolist())+")"
-    __str__=__repr__
 
 
 class TestDot(TestCase):
@@ -142,6 +135,59 @@
         assert_equal(zeros[1].array, zeros_test[1].array)
 
 
+class TestResize(TestCase):
+    def test_copies(self):
+        A = array([[1,2],[3,4]])
+        Ar1 = array([[1,2,3,4],[1,2,3,4]])
+        assert_equal(resize(A, (2,4)), Ar1)
+
+        Ar2 = array([[1,2],[3,4],[1,2],[3,4]])
+        assert_equal(resize(A, (4,2)), Ar2)
+
+        Ar3 = array([[1,2,3],[4,1,2],[3,4,1],[2,3,4]])
+        assert_equal(resize(A, (4,3)), Ar3)
+
+    def test_zeroresize(self):
+        A = array([[1,2],[3,4]])
+        Ar = resize(A, (0,))
+        assert_equal(Ar, array([]))
+
+
+class TestNonarrayArgs(TestCase):
+    # check that non-array arguments to functions wrap them in arrays
+    def test_squeeze(self):
+        A = [[[1,1,1],[2,2,2],[3,3,3]]]
+        assert squeeze(A).shape == (3,3)
+
+    def test_cumproduct(self):
+        A = [[1,2,3],[4,5,6]]
+        assert all(cumproduct(A) == array([1,2,6,24,120,720]))
+
+    def test_size(self):
+        A = [[1,2,3],[4,5,6]]
+        assert size(A) == 6
+        assert size(A,0) == 2
+        assert size(A,1) == 3
+
+    def test_mean(self):
+        A = [[1,2,3],[4,5,6]]
+        assert mean(A) == 3.5
+        assert all(mean(A,0) == array([2.5,3.5,4.5]))
+        assert all(mean(A,1) == array([2.,5.]))
+
+    def test_std(self):
+        A = [[1,2,3],[4,5,6]]
+        assert_almost_equal(std(A), 1.707825127659933)
+        assert_almost_equal(std(A,0), array([1.5, 1.5, 1.5]))
+        assert_almost_equal(std(A,1), array([0.81649658, 0.81649658]))
+
+    def test_var(self):
+        A = [[1,2,3],[4,5,6]]
+        assert_almost_equal(var(A), 2.9166666666666665)
+        assert_almost_equal(var(A,0), array([2.25, 2.25, 2.25]))
+        assert_almost_equal(var(A,1), array([0.66666667, 0.66666667]))
+
+
 class TestBoolScalar(TestCase):
     def test_logical(self):
         f = False_

Modified: trunk/numpy/core/tests/test_records.py
===================================================================
--- trunk/numpy/core/tests/test_records.py	2008-07-21 00:28:13 UTC (rev 5484)
+++ trunk/numpy/core/tests/test_records.py	2008-07-21 06:29:58 UTC (rev 5485)
@@ -115,6 +115,19 @@
         self.failUnlessRaises(AttributeError,assign_invalid_column,a)
 
 
+def test_find_duplicate():
+    l1 = [1,2,3,4,5,6]
+    assert np.rec.find_duplicate(l1) == []
+
+    l2 = [1,2,1,4,5,6]
+    assert np.rec.find_duplicate(l2) == [1]
+
+    l3 = [1,2,1,4,1,6,2,3]
+    assert np.rec.find_duplicate(l3) == [1,2]
+
+    l3 = [2,2,1,4,1,6,2,3]
+    assert np.rec.find_duplicate(l3) == [2,1]
+
 if __name__ == "__main__":
     run_module_suite()
 

Modified: trunk/numpy/core/tests/test_regression.py
===================================================================
--- trunk/numpy/core/tests/test_regression.py	2008-07-21 00:28:13 UTC (rev 5484)
+++ trunk/numpy/core/tests/test_regression.py	2008-07-21 06:29:58 UTC (rev 5485)
@@ -1,3 +1,4 @@
+
 from StringIO import StringIO
 import pickle
 import sys
@@ -1166,7 +1167,7 @@
             x = np.array([('a',u'b')], dtype=t)
             assert_equal(str(x), "[('a', u'b')]", err_msg=msg)
 
-    def check_sign_for_complex_nan(self, level=rlevel):
+    def test_sign_for_complex_nan(self, level=rlevel):
         """Ticket 794."""
         C = np.array([-np.inf, -2+1j, 0, 2-1j, np.inf, np.nan])
         have = np.sign(C)



More information about the Numpy-svn mailing list