test_matlib.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. from __future__ import division, absolute_import, print_function
  2. # As we are testing matrices, we ignore its PendingDeprecationWarnings
  3. try:
  4. import pytest
  5. pytestmark = pytest.mark.filterwarnings(
  6. 'ignore:the matrix subclass is not:PendingDeprecationWarning')
  7. except ImportError:
  8. pass
  9. import numpy as np
  10. import numpy.matlib
  11. from numpy.testing import assert_array_equal, assert_
  12. def test_empty():
  13. x = numpy.matlib.empty((2,))
  14. assert_(isinstance(x, np.matrix))
  15. assert_(x.shape, (1, 2))
  16. def test_ones():
  17. assert_array_equal(numpy.matlib.ones((2, 3)),
  18. np.matrix([[ 1., 1., 1.],
  19. [ 1., 1., 1.]]))
  20. assert_array_equal(numpy.matlib.ones(2), np.matrix([[ 1., 1.]]))
  21. def test_zeros():
  22. assert_array_equal(numpy.matlib.zeros((2, 3)),
  23. np.matrix([[ 0., 0., 0.],
  24. [ 0., 0., 0.]]))
  25. assert_array_equal(numpy.matlib.zeros(2), np.matrix([[ 0., 0.]]))
  26. def test_identity():
  27. x = numpy.matlib.identity(2, dtype=int)
  28. assert_array_equal(x, np.matrix([[1, 0], [0, 1]]))
  29. def test_eye():
  30. xc = numpy.matlib.eye(3, k=1, dtype=int)
  31. assert_array_equal(xc, np.matrix([[ 0, 1, 0],
  32. [ 0, 0, 1],
  33. [ 0, 0, 0]]))
  34. assert xc.flags.c_contiguous
  35. assert not xc.flags.f_contiguous
  36. xf = numpy.matlib.eye(3, 4, dtype=int, order='F')
  37. assert_array_equal(xf, np.matrix([[ 1, 0, 0, 0],
  38. [ 0, 1, 0, 0],
  39. [ 0, 0, 1, 0]]))
  40. assert not xf.flags.c_contiguous
  41. assert xf.flags.f_contiguous
  42. def test_rand():
  43. x = numpy.matlib.rand(3)
  44. # check matrix type, array would have shape (3,)
  45. assert_(x.ndim == 2)
  46. def test_randn():
  47. x = np.matlib.randn(3)
  48. # check matrix type, array would have shape (3,)
  49. assert_(x.ndim == 2)
  50. def test_repmat():
  51. a1 = np.arange(4)
  52. x = numpy.matlib.repmat(a1, 2, 2)
  53. y = np.array([[0, 1, 2, 3, 0, 1, 2, 3],
  54. [0, 1, 2, 3, 0, 1, 2, 3]])
  55. assert_array_equal(x, y)