test_eigen.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. from mpmath import mp
  4. from mpmath import libmp
  5. xrange = libmp.backend.xrange
  6. def run_hessenberg(A, verbose = 0):
  7. if verbose > 1:
  8. print("original matrix (hessenberg):\n", A)
  9. n = A.rows
  10. Q, H = mp.hessenberg(A)
  11. if verbose > 1:
  12. print("Q:\n",Q)
  13. print("H:\n",H)
  14. B = Q * H * Q.transpose_conj()
  15. eps = mp.exp(0.8 * mp.log(mp.eps))
  16. err0 = 0
  17. for x in xrange(n):
  18. for y in xrange(n):
  19. err0 += abs(A[y,x] - B[y,x])
  20. err0 /= n * n
  21. err1 = 0
  22. for x in xrange(n):
  23. for y in xrange(x + 2, n):
  24. err1 += abs(H[y,x])
  25. if verbose > 0:
  26. print("difference (H):", err0, err1)
  27. if verbose > 1:
  28. print("B:\n", B)
  29. assert err0 < eps
  30. assert err1 == 0
  31. def run_schur(A, verbose = 0):
  32. if verbose > 1:
  33. print("original matrix (schur):\n", A)
  34. n = A.rows
  35. Q, R = mp.schur(A)
  36. if verbose > 1:
  37. print("Q:\n", Q)
  38. print("R:\n", R)
  39. B = Q * R * Q.transpose_conj()
  40. C = Q * Q.transpose_conj()
  41. eps = mp.exp(0.8 * mp.log(mp.eps))
  42. err0 = 0
  43. for x in xrange(n):
  44. for y in xrange(n):
  45. err0 += abs(A[y,x] - B[y,x])
  46. err0 /= n * n
  47. err1 = 0
  48. for x in xrange(n):
  49. for y in xrange(n):
  50. if x == y:
  51. C[y,x] -= 1
  52. err1 += abs(C[y,x])
  53. err1 /= n * n
  54. err2 = 0
  55. for x in xrange(n):
  56. for y in xrange(x + 1, n):
  57. err2 += abs(R[y,x])
  58. if verbose > 0:
  59. print("difference (S):", err0, err1, err2)
  60. if verbose > 1:
  61. print("B:\n", B)
  62. assert err0 < eps
  63. assert err1 < eps
  64. assert err2 == 0
  65. def run_eig(A, verbose = 0):
  66. if verbose > 1:
  67. print("original matrix (eig):\n", A)
  68. n = A.rows
  69. E, EL, ER = mp.eig(A, left = True, right = True)
  70. if verbose > 1:
  71. print("E:\n", E)
  72. print("EL:\n", EL)
  73. print("ER:\n", ER)
  74. eps = mp.exp(0.8 * mp.log(mp.eps))
  75. err0 = 0
  76. for i in xrange(n):
  77. B = A * ER[:,i] - E[i] * ER[:,i]
  78. err0 = max(err0, mp.mnorm(B))
  79. B = EL[i,:] * A - EL[i,:] * E[i]
  80. err0 = max(err0, mp.mnorm(B))
  81. err0 /= n * n
  82. if verbose > 0:
  83. print("difference (E):", err0)
  84. assert err0 < eps
  85. #####################
  86. def test_eig_dyn():
  87. v = 0
  88. for i in xrange(5):
  89. n = 1 + int(mp.rand() * 5)
  90. if mp.rand() > 0.5:
  91. # real
  92. A = 2 * mp.randmatrix(n, n) - 1
  93. if mp.rand() > 0.5:
  94. A *= 10
  95. for x in xrange(n):
  96. for y in xrange(n):
  97. A[x,y] = int(A[x,y])
  98. else:
  99. A = (2 * mp.randmatrix(n, n) - 1) + 1j * (2 * mp.randmatrix(n, n) - 1)
  100. if mp.rand() > 0.5:
  101. A *= 10
  102. for x in xrange(n):
  103. for y in xrange(n):
  104. A[x,y] = int(mp.re(A[x,y])) + 1j * int(mp.im(A[x,y]))
  105. run_hessenberg(A, verbose = v)
  106. run_schur(A, verbose = v)
  107. run_eig(A, verbose = v)
  108. def test_eig():
  109. v = 0
  110. AS = []
  111. A = mp.matrix([[2, 1, 0], # jordan block of size 3
  112. [0, 2, 1],
  113. [0, 0, 2]])
  114. AS.append(A)
  115. AS.append(A.transpose())
  116. A = mp.matrix([[2, 0, 0], # jordan block of size 2
  117. [0, 2, 1],
  118. [0, 0, 2]])
  119. AS.append(A)
  120. AS.append(A.transpose())
  121. A = mp.matrix([[2, 0, 1], # jordan block of size 2
  122. [0, 2, 0],
  123. [0, 0, 2]])
  124. AS.append(A)
  125. AS.append(A.transpose())
  126. A= mp.matrix([[0, 0, 1], # cyclic
  127. [1, 0, 0],
  128. [0, 1, 0]])
  129. AS.append(A)
  130. AS.append(A.transpose())
  131. for A in AS:
  132. run_hessenberg(A, verbose = v)
  133. run_schur(A, verbose = v)
  134. run_eig(A, verbose = v)