Permalink
Browse files

TST: Add tests for matlib. Closes #1242.

(From Ralf Gommers.)
  • Loading branch information...
1 parent d7bf2b5 commit 62ebd8eb6a761fc7fc8e28f16b4c7a4a3d0465ac @pv pv committed Jul 31, 2010
Showing with 57 additions and 4 deletions.
  1. +4 −4 numpy/matlib.py
  2. +53 −0 numpy/tests/test_matlib.py
View
@@ -160,10 +160,10 @@ def identity(n,dtype=None):
Examples
--------
>>> import numpy.matlib
- >>> np.identity(3, dtype=int)
- array([[1, 0, 0],
- [0, 1, 0],
- [0, 0, 1]])
+ >>> np.matlib.identity(3, dtype=int)
+ matrix([[1, 0, 0],
+ [0, 1, 0],
+ [0, 0, 1]])
"""
a = array([1]+n*[0],dtype=dtype)
@@ -0,0 +1,53 @@
+import numpy as np
+import numpy.matlib
+from numpy.testing import assert_array_equal, assert_
+
+def test_empty():
+ x = np.matlib.empty((2,))
+ assert_(isinstance(x, np.matrix))
+ assert_(x.shape, (1,2))
+
+def test_ones():
+ assert_array_equal(np.matlib.ones((2, 3)),
+ np.matrix([[ 1., 1., 1.],
+ [ 1., 1., 1.]]))
+
+ assert_array_equal(np.matlib.ones(2), np.matrix([[ 1., 1.]]))
+
+def test_zeros():
+ assert_array_equal(np.matlib.zeros((2, 3)),
+ np.matrix([[ 0., 0., 0.],
+ [ 0., 0., 0.]]))
+
+ assert_array_equal(np.matlib.zeros(2), np.matrix([[ 0., 0.]]))
+
+def test_identity():
+ x = np.matlib.identity(2, dtype=np.int)
+ assert_array_equal(x, np.matrix([[1, 0], [0, 1]]))
+
+def test_eye():
+ x = np.matlib.eye(3, k=1, dtype=int)
+ assert_array_equal(x, np.matrix([[ 0, 1, 0],
+ [ 0, 0, 1],
+ [ 0, 0, 0]]))
+
+def test_rand():
+ x = np.matlib.rand(3)
+ # check matrix type, array would have shape (3,)
+ assert_(x.ndim == 2)
+
+def test_randn():
+ x = np.matlib.randn(3)
+ # check matrix type, array would have shape (3,)
+ assert_(x.ndim == 2)
+
+def test_repmat():
+ a1 = np.arange(4)
+ x = np.matlib.repmat(a1, 2, 2)
+ y = np.array([[0, 1, 2, 3, 0, 1, 2, 3],
+ [0, 1, 2, 3, 0, 1, 2, 3]])
+ assert_array_equal(x, y)
+
+
+if __name__ == "__main__":
+ run_module_suite()

0 comments on commit 62ebd8e

Please sign in to comment.