test_regression.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. """ Test functions for linalg module
  2. """
  3. import pytest
  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. w_svdvals = linalg.svdvals(a)
  75. assert_array_almost_equal(w, w_svdvals)
  76. def test_norm_object_array(self):
  77. # gh-7575
  78. testvector = np.array([np.array([0, 1]), 0, 0], dtype=object)
  79. norm = linalg.norm(testvector)
  80. assert_array_equal(norm, [0, 1])
  81. assert_(norm.dtype == np.dtype('float64'))
  82. norm = linalg.norm(testvector, ord=1)
  83. assert_array_equal(norm, [0, 1])
  84. assert_(norm.dtype != np.dtype('float64'))
  85. norm = linalg.norm(testvector, ord=2)
  86. assert_array_equal(norm, [0, 1])
  87. assert_(norm.dtype == np.dtype('float64'))
  88. assert_raises(ValueError, linalg.norm, testvector, ord='fro')
  89. assert_raises(ValueError, linalg.norm, testvector, ord='nuc')
  90. assert_raises(ValueError, linalg.norm, testvector, ord=np.inf)
  91. assert_raises(ValueError, linalg.norm, testvector, ord=-np.inf)
  92. assert_raises(ValueError, linalg.norm, testvector, ord=0)
  93. assert_raises(ValueError, linalg.norm, testvector, ord=-1)
  94. assert_raises(ValueError, linalg.norm, testvector, ord=-2)
  95. testmatrix = np.array([[np.array([0, 1]), 0, 0],
  96. [0, 0, 0]], dtype=object)
  97. norm = linalg.norm(testmatrix)
  98. assert_array_equal(norm, [0, 1])
  99. assert_(norm.dtype == np.dtype('float64'))
  100. norm = linalg.norm(testmatrix, ord='fro')
  101. assert_array_equal(norm, [0, 1])
  102. assert_(norm.dtype == np.dtype('float64'))
  103. assert_raises(TypeError, linalg.norm, testmatrix, ord='nuc')
  104. assert_raises(ValueError, linalg.norm, testmatrix, ord=np.inf)
  105. assert_raises(ValueError, linalg.norm, testmatrix, ord=-np.inf)
  106. assert_raises(ValueError, linalg.norm, testmatrix, ord=0)
  107. assert_raises(ValueError, linalg.norm, testmatrix, ord=1)
  108. assert_raises(ValueError, linalg.norm, testmatrix, ord=-1)
  109. assert_raises(TypeError, linalg.norm, testmatrix, ord=2)
  110. assert_raises(TypeError, linalg.norm, testmatrix, ord=-2)
  111. assert_raises(ValueError, linalg.norm, testmatrix, ord=3)
  112. def test_lstsq_complex_larger_rhs(self):
  113. # gh-9891
  114. size = 20
  115. n_rhs = 70
  116. G = np.random.randn(size, size) + 1j * np.random.randn(size, size)
  117. u = np.random.randn(size, n_rhs) + 1j * np.random.randn(size, n_rhs)
  118. b = G.dot(u)
  119. # This should work without segmentation fault.
  120. u_lstsq, res, rank, sv = linalg.lstsq(G, b, rcond=None)
  121. # check results just in case
  122. assert_array_almost_equal(u_lstsq, u)
  123. @pytest.mark.parametrize("upper", [True, False])
  124. def test_cholesky_empty_array(self, upper):
  125. # gh-25840 - upper=True hung before.
  126. res = np.linalg.cholesky(np.zeros((0, 0)), upper=upper)
  127. assert res.size == 0
  128. @pytest.mark.parametrize("rtol", [0.0, [0.0] * 4, np.zeros((4,))])
  129. def test_matrix_rank_rtol_argument(self, rtol):
  130. # gh-25877
  131. x = np.zeros((4, 3, 2))
  132. res = np.linalg.matrix_rank(x, rtol=rtol)
  133. assert res.shape == (4,)
  134. def test_openblas_threading(self):
  135. # gh-27036
  136. # Test whether matrix multiplication involving a large matrix always
  137. # gives the same (correct) answer
  138. x = np.arange(500000, dtype=np.float64)
  139. src = np.vstack((x, -10*x)).T
  140. matrix = np.array([[0, 1], [1, 0]])
  141. expected = np.vstack((-10*x, x)).T # src @ matrix
  142. for i in range(200):
  143. result = src @ matrix
  144. mismatches = (~np.isclose(result, expected)).sum()
  145. if mismatches != 0:
  146. assert False, ("unexpected result from matmul, "
  147. "probably due to OpenBLAS threading issues")