test_regression.py 5.5 KB

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