test_regression.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. """ Test functions for linalg module
  2. """
  3. import warnings
  4. import numpy as np
  5. from numpy import linalg, arange, float64, array, dot, transpose
  6. from numpy.testing import (
  7. assert_, assert_raises, assert_equal, assert_array_equal,
  8. assert_array_almost_equal, assert_array_less
  9. )
  10. class TestRegression:
  11. def test_eig_build(self):
  12. # Ticket #652
  13. rva = array([1.03221168e+02 + 0.j,
  14. -1.91843603e+01 + 0.j,
  15. -6.04004526e-01 + 15.84422474j,
  16. -6.04004526e-01 - 15.84422474j,
  17. -1.13692929e+01 + 0.j,
  18. -6.57612485e-01 + 10.41755503j,
  19. -6.57612485e-01 - 10.41755503j,
  20. 1.82126812e+01 + 0.j,
  21. 1.06011014e+01 + 0.j,
  22. 7.80732773e+00 + 0.j,
  23. -7.65390898e-01 + 0.j,
  24. 1.51971555e-15 + 0.j,
  25. -1.51308713e-15 + 0.j])
  26. a = arange(13 * 13, dtype=float64)
  27. a.shape = (13, 13)
  28. a = a % 17
  29. va, ve = linalg.eig(a)
  30. va.sort()
  31. rva.sort()
  32. assert_array_almost_equal(va, rva)
  33. def test_eigh_build(self):
  34. # Ticket 662.
  35. rvals = [68.60568999, 89.57756725, 106.67185574]
  36. cov = array([[77.70273908, 3.51489954, 15.64602427],
  37. [3.51489954, 88.97013878, -1.07431931],
  38. [15.64602427, -1.07431931, 98.18223512]])
  39. vals, vecs = linalg.eigh(cov)
  40. assert_array_almost_equal(vals, rvals)
  41. def test_svd_build(self):
  42. # Ticket 627.
  43. a = array([[0., 1.], [1., 1.], [2., 1.], [3., 1.]])
  44. m, n = a.shape
  45. u, s, vh = linalg.svd(a)
  46. b = dot(transpose(u[:, n:]), a)
  47. assert_array_almost_equal(b, np.zeros((2, 2)))
  48. def test_norm_vector_badarg(self):
  49. # Regression for #786: Frobenius norm for vectors raises
  50. # ValueError.
  51. assert_raises(ValueError, linalg.norm, array([1., 2., 3.]), 'fro')
  52. def test_lapack_endian(self):
  53. # For bug #1482
  54. a = array([[5.7998084, -2.1825367],
  55. [-2.1825367, 9.85910595]], dtype='>f8')
  56. b = array(a, dtype='<f8')
  57. ap = linalg.cholesky(a)
  58. bp = linalg.cholesky(b)
  59. assert_array_equal(ap, bp)
  60. def test_large_svd_32bit(self):
  61. # See gh-4442, 64bit would require very large/slow matrices.
  62. x = np.eye(1000, 66)
  63. np.linalg.svd(x)
  64. def test_svd_no_uv(self):
  65. # gh-4733
  66. for shape in (3, 4), (4, 4), (4, 3):
  67. for t in float, complex:
  68. a = np.ones(shape, dtype=t)
  69. w = linalg.svd(a, compute_uv=False)
  70. c = np.count_nonzero(np.absolute(w) > 0.5)
  71. assert_equal(c, 1)
  72. assert_equal(np.linalg.matrix_rank(a), 1)
  73. assert_array_less(1, np.linalg.norm(a, ord=2))
  74. def test_norm_object_array(self):
  75. # gh-7575
  76. testvector = np.array([np.array([0, 1]), 0, 0], dtype=object)
  77. norm = linalg.norm(testvector)
  78. assert_array_equal(norm, [0, 1])
  79. assert_(norm.dtype == np.dtype('float64'))
  80. norm = linalg.norm(testvector, ord=1)
  81. assert_array_equal(norm, [0, 1])
  82. assert_(norm.dtype != np.dtype('float64'))
  83. norm = linalg.norm(testvector, ord=2)
  84. assert_array_equal(norm, [0, 1])
  85. assert_(norm.dtype == np.dtype('float64'))
  86. assert_raises(ValueError, linalg.norm, testvector, ord='fro')
  87. assert_raises(ValueError, linalg.norm, testvector, ord='nuc')
  88. assert_raises(ValueError, linalg.norm, testvector, ord=np.inf)
  89. assert_raises(ValueError, linalg.norm, testvector, ord=-np.inf)
  90. with warnings.catch_warnings():
  91. warnings.simplefilter("error", DeprecationWarning)
  92. assert_raises((AttributeError, DeprecationWarning),
  93. linalg.norm, testvector, ord=0)
  94. assert_raises(ValueError, linalg.norm, testvector, ord=-1)
  95. assert_raises(ValueError, linalg.norm, testvector, ord=-2)
  96. testmatrix = np.array([[np.array([0, 1]), 0, 0],
  97. [0, 0, 0]], dtype=object)
  98. norm = linalg.norm(testmatrix)
  99. assert_array_equal(norm, [0, 1])
  100. assert_(norm.dtype == np.dtype('float64'))
  101. norm = linalg.norm(testmatrix, ord='fro')
  102. assert_array_equal(norm, [0, 1])
  103. assert_(norm.dtype == np.dtype('float64'))
  104. assert_raises(TypeError, linalg.norm, testmatrix, ord='nuc')
  105. assert_raises(ValueError, linalg.norm, testmatrix, ord=np.inf)
  106. assert_raises(ValueError, linalg.norm, testmatrix, ord=-np.inf)
  107. assert_raises(ValueError, linalg.norm, testmatrix, ord=0)
  108. assert_raises(ValueError, linalg.norm, testmatrix, ord=1)
  109. assert_raises(ValueError, linalg.norm, testmatrix, ord=-1)
  110. assert_raises(TypeError, linalg.norm, testmatrix, ord=2)
  111. assert_raises(TypeError, linalg.norm, testmatrix, ord=-2)
  112. assert_raises(ValueError, linalg.norm, testmatrix, ord=3)
  113. def test_lstsq_complex_larger_rhs(self):
  114. # gh-9891
  115. size = 20
  116. n_rhs = 70
  117. G = np.random.randn(size, size) + 1j * np.random.randn(size, size)
  118. u = np.random.randn(size, n_rhs) + 1j * np.random.randn(size, n_rhs)
  119. b = G.dot(u)
  120. # This should work without segmentation fault.
  121. u_lstsq, res, rank, sv = linalg.lstsq(G, b, rcond=None)
  122. # check results just in case
  123. assert_array_almost_equal(u_lstsq, u)