test_matlib.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import numpy as np
  2. import numpy.matlib
  3. from numpy.testing import assert_array_equal, assert_
  4. def test_empty():
  5. x = numpy.matlib.empty((2,))
  6. assert_(isinstance(x, np.matrix))
  7. assert_(x.shape, (1, 2))
  8. def test_ones():
  9. assert_array_equal(numpy.matlib.ones((2, 3)),
  10. np.matrix([[ 1., 1., 1.],
  11. [ 1., 1., 1.]]))
  12. assert_array_equal(numpy.matlib.ones(2), np.matrix([[ 1., 1.]]))
  13. def test_zeros():
  14. assert_array_equal(numpy.matlib.zeros((2, 3)),
  15. np.matrix([[ 0., 0., 0.],
  16. [ 0., 0., 0.]]))
  17. assert_array_equal(numpy.matlib.zeros(2), np.matrix([[ 0., 0.]]))
  18. def test_identity():
  19. x = numpy.matlib.identity(2, dtype=int)
  20. assert_array_equal(x, np.matrix([[1, 0], [0, 1]]))
  21. def test_eye():
  22. xc = numpy.matlib.eye(3, k=1, dtype=int)
  23. assert_array_equal(xc, np.matrix([[ 0, 1, 0],
  24. [ 0, 0, 1],
  25. [ 0, 0, 0]]))
  26. assert xc.flags.c_contiguous
  27. assert not xc.flags.f_contiguous
  28. xf = numpy.matlib.eye(3, 4, dtype=int, order='F')
  29. assert_array_equal(xf, np.matrix([[ 1, 0, 0, 0],
  30. [ 0, 1, 0, 0],
  31. [ 0, 0, 1, 0]]))
  32. assert not xf.flags.c_contiguous
  33. assert xf.flags.f_contiguous
  34. def test_rand():
  35. x = numpy.matlib.rand(3)
  36. # check matrix type, array would have shape (3,)
  37. assert_(x.ndim == 2)
  38. def test_randn():
  39. x = np.matlib.randn(3)
  40. # check matrix type, array would have shape (3,)
  41. assert_(x.ndim == 2)
  42. def test_repmat():
  43. a1 = np.arange(4)
  44. x = numpy.matlib.repmat(a1, 2, 2)
  45. y = np.array([[0, 1, 2, 3, 0, 1, 2, 3],
  46. [0, 1, 2, 3, 0, 1, 2, 3]])
  47. assert_array_equal(x, y)