m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

461 lines
14 KiB

6 months ago
  1. from sympy.core.function import expand_mul
  2. from sympy.core.numbers import (I, Rational)
  3. from sympy.core.singleton import S
  4. from sympy.functions.elementary.miscellaneous import sqrt
  5. from sympy.simplify.simplify import simplify
  6. from sympy.matrices.matrices import NonSquareMatrixError
  7. from sympy.matrices import Matrix, zeros, eye, SparseMatrix
  8. from sympy.abc import x, y, z
  9. from sympy.testing.pytest import raises, slow
  10. from sympy.testing.matrices import allclose
  11. def test_LUdecomp():
  12. testmat = Matrix([[0, 2, 5, 3],
  13. [3, 3, 7, 4],
  14. [8, 4, 0, 2],
  15. [-2, 6, 3, 4]])
  16. L, U, p = testmat.LUdecomposition()
  17. assert L.is_lower
  18. assert U.is_upper
  19. assert (L*U).permute_rows(p, 'backward') - testmat == zeros(4)
  20. testmat = Matrix([[6, -2, 7, 4],
  21. [0, 3, 6, 7],
  22. [1, -2, 7, 4],
  23. [-9, 2, 6, 3]])
  24. L, U, p = testmat.LUdecomposition()
  25. assert L.is_lower
  26. assert U.is_upper
  27. assert (L*U).permute_rows(p, 'backward') - testmat == zeros(4)
  28. # non-square
  29. testmat = Matrix([[1, 2, 3],
  30. [4, 5, 6],
  31. [7, 8, 9],
  32. [10, 11, 12]])
  33. L, U, p = testmat.LUdecomposition(rankcheck=False)
  34. assert L.is_lower
  35. assert U.is_upper
  36. assert (L*U).permute_rows(p, 'backward') - testmat == zeros(4, 3)
  37. # square and singular
  38. testmat = Matrix([[1, 2, 3],
  39. [2, 4, 6],
  40. [4, 5, 6]])
  41. L, U, p = testmat.LUdecomposition(rankcheck=False)
  42. assert L.is_lower
  43. assert U.is_upper
  44. assert (L*U).permute_rows(p, 'backward') - testmat == zeros(3)
  45. M = Matrix(((1, x, 1), (2, y, 0), (y, 0, z)))
  46. L, U, p = M.LUdecomposition()
  47. assert L.is_lower
  48. assert U.is_upper
  49. assert (L*U).permute_rows(p, 'backward') - M == zeros(3)
  50. mL = Matrix((
  51. (1, 0, 0),
  52. (2, 3, 0),
  53. ))
  54. assert mL.is_lower is True
  55. assert mL.is_upper is False
  56. mU = Matrix((
  57. (1, 2, 3),
  58. (0, 4, 5),
  59. ))
  60. assert mU.is_lower is False
  61. assert mU.is_upper is True
  62. # test FF LUdecomp
  63. M = Matrix([[1, 3, 3],
  64. [3, 2, 6],
  65. [3, 2, 2]])
  66. P, L, Dee, U = M.LUdecompositionFF()
  67. assert P*M == L*Dee.inv()*U
  68. M = Matrix([[1, 2, 3, 4],
  69. [3, -1, 2, 3],
  70. [3, 1, 3, -2],
  71. [6, -1, 0, 2]])
  72. P, L, Dee, U = M.LUdecompositionFF()
  73. assert P*M == L*Dee.inv()*U
  74. M = Matrix([[0, 0, 1],
  75. [2, 3, 0],
  76. [3, 1, 4]])
  77. P, L, Dee, U = M.LUdecompositionFF()
  78. assert P*M == L*Dee.inv()*U
  79. # issue 15794
  80. M = Matrix(
  81. [[1, 2, 3],
  82. [4, 5, 6],
  83. [7, 8, 9]]
  84. )
  85. raises(ValueError, lambda : M.LUdecomposition_Simple(rankcheck=True))
  86. def test_singular_value_decompositionD():
  87. A = Matrix([[1, 2], [2, 1]])
  88. U, S, V = A.singular_value_decomposition()
  89. assert U * S * V.T == A
  90. assert U.T * U == eye(U.cols)
  91. assert V.T * V == eye(V.cols)
  92. B = Matrix([[1, 2]])
  93. U, S, V = B.singular_value_decomposition()
  94. assert U * S * V.T == B
  95. assert U.T * U == eye(U.cols)
  96. assert V.T * V == eye(V.cols)
  97. C = Matrix([
  98. [1, 0, 0, 0, 2],
  99. [0, 0, 3, 0, 0],
  100. [0, 0, 0, 0, 0],
  101. [0, 2, 0, 0, 0],
  102. ])
  103. U, S, V = C.singular_value_decomposition()
  104. assert U * S * V.T == C
  105. assert U.T * U == eye(U.cols)
  106. assert V.T * V == eye(V.cols)
  107. D = Matrix([[Rational(1, 3), sqrt(2)], [0, Rational(1, 4)]])
  108. U, S, V = D.singular_value_decomposition()
  109. assert simplify(U.T * U) == eye(U.cols)
  110. assert simplify(V.T * V) == eye(V.cols)
  111. assert simplify(U * S * V.T) == D
  112. def test_QR():
  113. A = Matrix([[1, 2], [2, 3]])
  114. Q, S = A.QRdecomposition()
  115. R = Rational
  116. assert Q == Matrix([
  117. [ 5**R(-1, 2), (R(2)/5)*(R(1)/5)**R(-1, 2)],
  118. [2*5**R(-1, 2), (-R(1)/5)*(R(1)/5)**R(-1, 2)]])
  119. assert S == Matrix([[5**R(1, 2), 8*5**R(-1, 2)], [0, (R(1)/5)**R(1, 2)]])
  120. assert Q*S == A
  121. assert Q.T * Q == eye(2)
  122. A = Matrix([[1, 1, 1], [1, 1, 3], [2, 3, 4]])
  123. Q, R = A.QRdecomposition()
  124. assert Q.T * Q == eye(Q.cols)
  125. assert R.is_upper
  126. assert A == Q*R
  127. A = Matrix([[12, 0, -51], [6, 0, 167], [-4, 0, 24]])
  128. Q, R = A.QRdecomposition()
  129. assert Q.T * Q == eye(Q.cols)
  130. assert R.is_upper
  131. assert A == Q*R
  132. def test_QR_non_square():
  133. # Narrow (cols < rows) matrices
  134. A = Matrix([[9, 0, 26], [12, 0, -7], [0, 4, 4], [0, -3, -3]])
  135. Q, R = A.QRdecomposition()
  136. assert Q.T * Q == eye(Q.cols)
  137. assert R.is_upper
  138. assert A == Q*R
  139. A = Matrix([[1, -1, 4], [1, 4, -2], [1, 4, 2], [1, -1, 0]])
  140. Q, R = A.QRdecomposition()
  141. assert Q.T * Q == eye(Q.cols)
  142. assert R.is_upper
  143. assert A == Q*R
  144. A = Matrix(2, 1, [1, 2])
  145. Q, R = A.QRdecomposition()
  146. assert Q.T * Q == eye(Q.cols)
  147. assert R.is_upper
  148. assert A == Q*R
  149. # Wide (cols > rows) matrices
  150. A = Matrix([[1, 2, 3], [4, 5, 6]])
  151. Q, R = A.QRdecomposition()
  152. assert Q.T * Q == eye(Q.cols)
  153. assert R.is_upper
  154. assert A == Q*R
  155. A = Matrix([[1, 2, 3, 4], [1, 4, 9, 16], [1, 8, 27, 64]])
  156. Q, R = A.QRdecomposition()
  157. assert Q.T * Q == eye(Q.cols)
  158. assert R.is_upper
  159. assert A == Q*R
  160. A = Matrix(1, 2, [1, 2])
  161. Q, R = A.QRdecomposition()
  162. assert Q.T * Q == eye(Q.cols)
  163. assert R.is_upper
  164. assert A == Q*R
  165. def test_QR_trivial():
  166. # Rank deficient matrices
  167. A = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
  168. Q, R = A.QRdecomposition()
  169. assert Q.T * Q == eye(Q.cols)
  170. assert R.is_upper
  171. assert A == Q*R
  172. A = Matrix([[1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4]])
  173. Q, R = A.QRdecomposition()
  174. assert Q.T * Q == eye(Q.cols)
  175. assert R.is_upper
  176. assert A == Q*R
  177. A = Matrix([[1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4]]).T
  178. Q, R = A.QRdecomposition()
  179. assert Q.T * Q == eye(Q.cols)
  180. assert R.is_upper
  181. assert A == Q*R
  182. # Zero rank matrices
  183. A = Matrix([[0, 0, 0]])
  184. Q, R = A.QRdecomposition()
  185. assert Q.T * Q == eye(Q.cols)
  186. assert R.is_upper
  187. assert A == Q*R
  188. A = Matrix([[0, 0, 0]]).T
  189. Q, R = A.QRdecomposition()
  190. assert Q.T * Q == eye(Q.cols)
  191. assert R.is_upper
  192. assert A == Q*R
  193. A = Matrix([[0, 0, 0], [0, 0, 0]])
  194. Q, R = A.QRdecomposition()
  195. assert Q.T * Q == eye(Q.cols)
  196. assert R.is_upper
  197. assert A == Q*R
  198. A = Matrix([[0, 0, 0], [0, 0, 0]]).T
  199. Q, R = A.QRdecomposition()
  200. assert Q.T * Q == eye(Q.cols)
  201. assert R.is_upper
  202. assert A == Q*R
  203. # Rank deficient matrices with zero norm from beginning columns
  204. A = Matrix([[0, 0, 0], [1, 2, 3]]).T
  205. Q, R = A.QRdecomposition()
  206. assert Q.T * Q == eye(Q.cols)
  207. assert R.is_upper
  208. assert A == Q*R
  209. A = Matrix([[0, 0, 0, 0], [1, 2, 3, 4], [0, 0, 0, 0]]).T
  210. Q, R = A.QRdecomposition()
  211. assert Q.T * Q == eye(Q.cols)
  212. assert R.is_upper
  213. assert A == Q*R
  214. A = Matrix([[0, 0, 0, 0], [1, 2, 3, 4], [0, 0, 0, 0], [2, 4, 6, 8]]).T
  215. Q, R = A.QRdecomposition()
  216. assert Q.T * Q == eye(Q.cols)
  217. assert R.is_upper
  218. assert A == Q*R
  219. A = Matrix([[0, 0, 0], [0, 0, 0], [0, 0, 0], [1, 2, 3]]).T
  220. Q, R = A.QRdecomposition()
  221. assert Q.T * Q == eye(Q.cols)
  222. assert R.is_upper
  223. assert A == Q*R
  224. def test_QR_float():
  225. A = Matrix([[1, 1], [1, 1.01]])
  226. Q, R = A.QRdecomposition()
  227. assert allclose(Q * R, A)
  228. assert allclose(Q * Q.T, Matrix.eye(2))
  229. assert allclose(Q.T * Q, Matrix.eye(2))
  230. A = Matrix([[1, 1], [1, 1.001]])
  231. Q, R = A.QRdecomposition()
  232. assert allclose(Q * R, A)
  233. assert allclose(Q * Q.T, Matrix.eye(2))
  234. assert allclose(Q.T * Q, Matrix.eye(2))
  235. def test_LUdecomposition_Simple_iszerofunc():
  236. # Test if callable passed to matrices.LUdecomposition_Simple() as iszerofunc keyword argument is used inside
  237. # matrices.LUdecomposition_Simple()
  238. magic_string = "I got passed in!"
  239. def goofyiszero(value):
  240. raise ValueError(magic_string)
  241. try:
  242. lu, p = Matrix([[1, 0], [0, 1]]).LUdecomposition_Simple(iszerofunc=goofyiszero)
  243. except ValueError as err:
  244. assert magic_string == err.args[0]
  245. return
  246. assert False
  247. def test_LUdecomposition_iszerofunc():
  248. # Test if callable passed to matrices.LUdecomposition() as iszerofunc keyword argument is used inside
  249. # matrices.LUdecomposition_Simple()
  250. magic_string = "I got passed in!"
  251. def goofyiszero(value):
  252. raise ValueError(magic_string)
  253. try:
  254. l, u, p = Matrix([[1, 0], [0, 1]]).LUdecomposition(iszerofunc=goofyiszero)
  255. except ValueError as err:
  256. assert magic_string == err.args[0]
  257. return
  258. assert False
  259. def test_LDLdecomposition():
  260. raises(NonSquareMatrixError, lambda: Matrix((1, 2)).LDLdecomposition())
  261. raises(ValueError, lambda: Matrix(((1, 2), (3, 4))).LDLdecomposition())
  262. raises(ValueError, lambda: Matrix(((5 + I, 0), (0, 1))).LDLdecomposition())
  263. raises(ValueError, lambda: Matrix(((1, 5), (5, 1))).LDLdecomposition())
  264. raises(ValueError, lambda: Matrix(((1, 2), (3, 4))).LDLdecomposition(hermitian=False))
  265. A = Matrix(((1, 5), (5, 1)))
  266. L, D = A.LDLdecomposition(hermitian=False)
  267. assert L * D * L.T == A
  268. A = Matrix(((25, 15, -5), (15, 18, 0), (-5, 0, 11)))
  269. L, D = A.LDLdecomposition()
  270. assert L * D * L.T == A
  271. assert L.is_lower
  272. assert L == Matrix([[1, 0, 0], [ Rational(3, 5), 1, 0], [Rational(-1, 5), Rational(1, 3), 1]])
  273. assert D.is_diagonal()
  274. assert D == Matrix([[25, 0, 0], [0, 9, 0], [0, 0, 9]])
  275. A = Matrix(((4, -2*I, 2 + 2*I), (2*I, 2, -1 + I), (2 - 2*I, -1 - I, 11)))
  276. L, D = A.LDLdecomposition()
  277. assert expand_mul(L * D * L.H) == A
  278. assert L.expand() == Matrix([[1, 0, 0], [I/2, 1, 0], [S.Half - I/2, 0, 1]])
  279. assert D.expand() == Matrix(((4, 0, 0), (0, 1, 0), (0, 0, 9)))
  280. raises(NonSquareMatrixError, lambda: SparseMatrix((1, 2)).LDLdecomposition())
  281. raises(ValueError, lambda: SparseMatrix(((1, 2), (3, 4))).LDLdecomposition())
  282. raises(ValueError, lambda: SparseMatrix(((5 + I, 0), (0, 1))).LDLdecomposition())
  283. raises(ValueError, lambda: SparseMatrix(((1, 5), (5, 1))).LDLdecomposition())
  284. raises(ValueError, lambda: SparseMatrix(((1, 2), (3, 4))).LDLdecomposition(hermitian=False))
  285. A = SparseMatrix(((1, 5), (5, 1)))
  286. L, D = A.LDLdecomposition(hermitian=False)
  287. assert L * D * L.T == A
  288. A = SparseMatrix(((25, 15, -5), (15, 18, 0), (-5, 0, 11)))
  289. L, D = A.LDLdecomposition()
  290. assert L * D * L.T == A
  291. assert L.is_lower
  292. assert L == Matrix([[1, 0, 0], [ Rational(3, 5), 1, 0], [Rational(-1, 5), Rational(1, 3), 1]])
  293. assert D.is_diagonal()
  294. assert D == Matrix([[25, 0, 0], [0, 9, 0], [0, 0, 9]])
  295. A = SparseMatrix(((4, -2*I, 2 + 2*I), (2*I, 2, -1 + I), (2 - 2*I, -1 - I, 11)))
  296. L, D = A.LDLdecomposition()
  297. assert expand_mul(L * D * L.H) == A
  298. assert L == Matrix(((1, 0, 0), (I/2, 1, 0), (S.Half - I/2, 0, 1)))
  299. assert D == Matrix(((4, 0, 0), (0, 1, 0), (0, 0, 9)))
  300. def test_pinv_succeeds_with_rank_decomposition_method():
  301. # Test rank decomposition method of pseudoinverse succeeding
  302. As = [Matrix([
  303. [61, 89, 55, 20, 71, 0],
  304. [62, 96, 85, 85, 16, 0],
  305. [69, 56, 17, 4, 54, 0],
  306. [10, 54, 91, 41, 71, 0],
  307. [ 7, 30, 10, 48, 90, 0],
  308. [0,0,0,0,0,0]])]
  309. for A in As:
  310. A_pinv = A.pinv(method="RD")
  311. AAp = A * A_pinv
  312. ApA = A_pinv * A
  313. assert simplify(AAp * A) == A
  314. assert simplify(ApA * A_pinv) == A_pinv
  315. assert AAp.H == AAp
  316. assert ApA.H == ApA
  317. def test_rank_decomposition():
  318. a = Matrix(0, 0, [])
  319. c, f = a.rank_decomposition()
  320. assert f.is_echelon
  321. assert c.cols == f.rows == a.rank()
  322. assert c * f == a
  323. a = Matrix(1, 1, [5])
  324. c, f = a.rank_decomposition()
  325. assert f.is_echelon
  326. assert c.cols == f.rows == a.rank()
  327. assert c * f == a
  328. a = Matrix(3, 3, [1, 2, 3, 1, 2, 3, 1, 2, 3])
  329. c, f = a.rank_decomposition()
  330. assert f.is_echelon
  331. assert c.cols == f.rows == a.rank()
  332. assert c * f == a
  333. a = Matrix([
  334. [0, 0, 1, 2, 2, -5, 3],
  335. [-1, 5, 2, 2, 1, -7, 5],
  336. [0, 0, -2, -3, -3, 8, -5],
  337. [-1, 5, 0, -1, -2, 1, 0]])
  338. c, f = a.rank_decomposition()
  339. assert f.is_echelon
  340. assert c.cols == f.rows == a.rank()
  341. assert c * f == a
  342. @slow
  343. def test_upper_hessenberg_decomposition():
  344. A = Matrix([
  345. [1, 0, sqrt(3)],
  346. [sqrt(2), Rational(1, 2), 2],
  347. [1, Rational(1, 4), 3],
  348. ])
  349. H, P = A.upper_hessenberg_decomposition()
  350. assert simplify(P * P.H) == eye(P.cols)
  351. assert simplify(P.H * P) == eye(P.cols)
  352. assert H.is_upper_hessenberg
  353. assert (simplify(P * H * P.H)) == A
  354. B = Matrix([
  355. [1, 2, 10],
  356. [8, 2, 5],
  357. [3, 12, 34],
  358. ])
  359. H, P = B.upper_hessenberg_decomposition()
  360. assert simplify(P * P.H) == eye(P.cols)
  361. assert simplify(P.H * P) == eye(P.cols)
  362. assert H.is_upper_hessenberg
  363. assert simplify(P * H * P.H) == B
  364. C = Matrix([
  365. [1, sqrt(2), 2, 3],
  366. [0, 5, 3, 4],
  367. [1, 1, 4, sqrt(5)],
  368. [0, 2, 2, 3]
  369. ])
  370. H, P = C.upper_hessenberg_decomposition()
  371. assert simplify(P * P.H) == eye(P.cols)
  372. assert simplify(P.H * P) == eye(P.cols)
  373. assert H.is_upper_hessenberg
  374. assert simplify(P * H * P.H) == C
  375. D = Matrix([
  376. [1, 2, 3],
  377. [-3, 5, 6],
  378. [4, -8, 9],
  379. ])
  380. H, P = D.upper_hessenberg_decomposition()
  381. assert simplify(P * P.H) == eye(P.cols)
  382. assert simplify(P.H * P) == eye(P.cols)
  383. assert H.is_upper_hessenberg
  384. assert simplify(P * H * P.H) == D
  385. E = Matrix([
  386. [1, 0, 0, 0],
  387. [0, 1, 0, 0],
  388. [1, 1, 0, 1],
  389. [1, 1, 1, 0]
  390. ])
  391. H, P = E.upper_hessenberg_decomposition()
  392. assert simplify(P * P.H) == eye(P.cols)
  393. assert simplify(P.H * P) == eye(P.cols)
  394. assert H.is_upper_hessenberg
  395. assert simplify(P * H * P.H) == E