test_determinant.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. import random
  2. from sympy.core.numbers import I
  3. from sympy.core.numbers import Rational
  4. from sympy.core.symbol import (Symbol, symbols)
  5. from sympy.functions.elementary.miscellaneous import sqrt
  6. from sympy.polys.polytools import Poly
  7. from sympy.matrices import Matrix, eye, ones
  8. from sympy.abc import x, y, z
  9. from sympy.testing.pytest import raises
  10. from sympy.matrices.common import NonSquareMatrixError
  11. from sympy.functions.combinatorial.factorials import factorial, subfactorial
  12. def test_determinant():
  13. M = Matrix()
  14. assert M.det() == 1
  15. # Evaluating these directly because they are never reached via M.det()
  16. assert M._eval_det_bareiss() == 1
  17. assert M._eval_det_berkowitz() == 1
  18. assert M._eval_det_lu() == 1
  19. M = Matrix([ [0] ])
  20. assert M.det() == 0
  21. assert M._eval_det_bareiss() == 0
  22. assert M._eval_det_berkowitz() == 0
  23. assert M._eval_det_lu() == 0
  24. M = Matrix([ [5] ])
  25. assert M.det() == 5
  26. assert M._eval_det_bareiss() == 5
  27. assert M._eval_det_berkowitz() == 5
  28. assert M._eval_det_lu() == 5
  29. M = Matrix(( (-3, 2),
  30. ( 8, -5) ))
  31. assert M.det(method="domain-ge") == -1
  32. assert M.det(method="bareiss") == -1
  33. assert M.det(method="berkowitz") == -1
  34. assert M.det(method="lu") == -1
  35. M = Matrix(( (x, 1),
  36. (y, 2*y) ))
  37. assert M.det(method="domain-ge") == 2*x*y - y
  38. assert M.det(method="bareiss") == 2*x*y - y
  39. assert M.det(method="berkowitz") == 2*x*y - y
  40. assert M.det(method="lu") == 2*x*y - y
  41. M = Matrix(( (1, 1, 1),
  42. (1, 2, 3),
  43. (1, 3, 6) ))
  44. assert M.det(method="domain-ge") == 1
  45. assert M.det(method="bareiss") == 1
  46. assert M.det(method="berkowitz") == 1
  47. assert M.det(method="lu") == 1
  48. M = Matrix(( ( 3, -2, 0, 5),
  49. (-2, 1, -2, 2),
  50. ( 0, -2, 5, 0),
  51. ( 5, 0, 3, 4) ))
  52. assert M.det(method="domain-ge") == -289
  53. assert M.det(method="bareiss") == -289
  54. assert M.det(method="berkowitz") == -289
  55. assert M.det(method="lu") == -289
  56. M = Matrix(( ( 1, 2, 3, 4),
  57. ( 5, 6, 7, 8),
  58. ( 9, 10, 11, 12),
  59. (13, 14, 15, 16) ))
  60. assert M.det(method="domain-ge") == 0
  61. assert M.det(method="bareiss") == 0
  62. assert M.det(method="berkowitz") == 0
  63. assert M.det(method="lu") == 0
  64. M = Matrix(( (3, 2, 0, 0, 0),
  65. (0, 3, 2, 0, 0),
  66. (0, 0, 3, 2, 0),
  67. (0, 0, 0, 3, 2),
  68. (2, 0, 0, 0, 3) ))
  69. assert M.det(method="domain-ge") == 275
  70. assert M.det(method="bareiss") == 275
  71. assert M.det(method="berkowitz") == 275
  72. assert M.det(method="lu") == 275
  73. M = Matrix(( ( 3, 0, 0, 0),
  74. (-2, 1, 0, 0),
  75. ( 0, -2, 5, 0),
  76. ( 5, 0, 3, 4) ))
  77. assert M.det(method="domain-ge") == 60
  78. assert M.det(method="bareiss") == 60
  79. assert M.det(method="berkowitz") == 60
  80. assert M.det(method="lu") == 60
  81. M = Matrix(( ( 1, 0, 0, 0),
  82. ( 5, 0, 0, 0),
  83. ( 9, 10, 11, 0),
  84. (13, 14, 15, 16) ))
  85. assert M.det(method="domain-ge") == 0
  86. assert M.det(method="bareiss") == 0
  87. assert M.det(method="berkowitz") == 0
  88. assert M.det(method="lu") == 0
  89. M = Matrix(( (3, 2, 0, 0, 0),
  90. (0, 3, 2, 0, 0),
  91. (0, 0, 3, 2, 0),
  92. (0, 0, 0, 3, 2),
  93. (0, 0, 0, 0, 3) ))
  94. assert M.det(method="domain-ge") == 243
  95. assert M.det(method="bareiss") == 243
  96. assert M.det(method="berkowitz") == 243
  97. assert M.det(method="lu") == 243
  98. M = Matrix(( (1, 0, 1, 2, 12),
  99. (2, 0, 1, 1, 4),
  100. (2, 1, 1, -1, 3),
  101. (3, 2, -1, 1, 8),
  102. (1, 1, 1, 0, 6) ))
  103. assert M.det(method="domain-ge") == -55
  104. assert M.det(method="bareiss") == -55
  105. assert M.det(method="berkowitz") == -55
  106. assert M.det(method="lu") == -55
  107. M = Matrix(( (-5, 2, 3, 4, 5),
  108. ( 1, -4, 3, 4, 5),
  109. ( 1, 2, -3, 4, 5),
  110. ( 1, 2, 3, -2, 5),
  111. ( 1, 2, 3, 4, -1) ))
  112. assert M.det(method="domain-ge") == 11664
  113. assert M.det(method="bareiss") == 11664
  114. assert M.det(method="berkowitz") == 11664
  115. assert M.det(method="lu") == 11664
  116. M = Matrix(( ( 2, 7, -1, 3, 2),
  117. ( 0, 0, 1, 0, 1),
  118. (-2, 0, 7, 0, 2),
  119. (-3, -2, 4, 5, 3),
  120. ( 1, 0, 0, 0, 1) ))
  121. assert M.det(method="domain-ge") == 123
  122. assert M.det(method="bareiss") == 123
  123. assert M.det(method="berkowitz") == 123
  124. assert M.det(method="lu") == 123
  125. M = Matrix(( (x, y, z),
  126. (1, 0, 0),
  127. (y, z, x) ))
  128. assert M.det(method="domain-ge") == z**2 - x*y
  129. assert M.det(method="bareiss") == z**2 - x*y
  130. assert M.det(method="berkowitz") == z**2 - x*y
  131. assert M.det(method="lu") == z**2 - x*y
  132. # issue 13835
  133. a = symbols('a')
  134. M = lambda n: Matrix([[i + a*j for i in range(n)]
  135. for j in range(n)])
  136. assert M(5).det() == 0
  137. assert M(6).det() == 0
  138. assert M(7).det() == 0
  139. def test_issue_14517():
  140. M = Matrix([
  141. [ 0, 10*I, 10*I, 0],
  142. [10*I, 0, 0, 10*I],
  143. [10*I, 0, 5 + 2*I, 10*I],
  144. [ 0, 10*I, 10*I, 5 + 2*I]])
  145. ev = M.eigenvals()
  146. # test one random eigenvalue, the computation is a little slow
  147. test_ev = random.choice(list(ev.keys()))
  148. assert (M - test_ev*eye(4)).det() == 0
  149. def test_legacy_det():
  150. # Minimal support for legacy keys for 'method' in det()
  151. # Partially copied from test_determinant()
  152. M = Matrix(( ( 3, -2, 0, 5),
  153. (-2, 1, -2, 2),
  154. ( 0, -2, 5, 0),
  155. ( 5, 0, 3, 4) ))
  156. assert M.det(method="bareis") == -289
  157. assert M.det(method="det_lu") == -289
  158. assert M.det(method="det_LU") == -289
  159. M = Matrix(( (3, 2, 0, 0, 0),
  160. (0, 3, 2, 0, 0),
  161. (0, 0, 3, 2, 0),
  162. (0, 0, 0, 3, 2),
  163. (2, 0, 0, 0, 3) ))
  164. assert M.det(method="bareis") == 275
  165. assert M.det(method="det_lu") == 275
  166. assert M.det(method="Bareis") == 275
  167. M = Matrix(( (1, 0, 1, 2, 12),
  168. (2, 0, 1, 1, 4),
  169. (2, 1, 1, -1, 3),
  170. (3, 2, -1, 1, 8),
  171. (1, 1, 1, 0, 6) ))
  172. assert M.det(method="bareis") == -55
  173. assert M.det(method="det_lu") == -55
  174. assert M.det(method="BAREISS") == -55
  175. M = Matrix(( ( 3, 0, 0, 0),
  176. (-2, 1, 0, 0),
  177. ( 0, -2, 5, 0),
  178. ( 5, 0, 3, 4) ))
  179. assert M.det(method="bareiss") == 60
  180. assert M.det(method="berkowitz") == 60
  181. assert M.det(method="lu") == 60
  182. M = Matrix(( ( 1, 0, 0, 0),
  183. ( 5, 0, 0, 0),
  184. ( 9, 10, 11, 0),
  185. (13, 14, 15, 16) ))
  186. assert M.det(method="bareiss") == 0
  187. assert M.det(method="berkowitz") == 0
  188. assert M.det(method="lu") == 0
  189. M = Matrix(( (3, 2, 0, 0, 0),
  190. (0, 3, 2, 0, 0),
  191. (0, 0, 3, 2, 0),
  192. (0, 0, 0, 3, 2),
  193. (0, 0, 0, 0, 3) ))
  194. assert M.det(method="bareiss") == 243
  195. assert M.det(method="berkowitz") == 243
  196. assert M.det(method="lu") == 243
  197. M = Matrix(( (-5, 2, 3, 4, 5),
  198. ( 1, -4, 3, 4, 5),
  199. ( 1, 2, -3, 4, 5),
  200. ( 1, 2, 3, -2, 5),
  201. ( 1, 2, 3, 4, -1) ))
  202. assert M.det(method="bareis") == 11664
  203. assert M.det(method="det_lu") == 11664
  204. assert M.det(method="BERKOWITZ") == 11664
  205. M = Matrix(( ( 2, 7, -1, 3, 2),
  206. ( 0, 0, 1, 0, 1),
  207. (-2, 0, 7, 0, 2),
  208. (-3, -2, 4, 5, 3),
  209. ( 1, 0, 0, 0, 1) ))
  210. assert M.det(method="bareis") == 123
  211. assert M.det(method="det_lu") == 123
  212. assert M.det(method="LU") == 123
  213. def eye_Determinant(n):
  214. return Matrix(n, n, lambda i, j: int(i == j))
  215. def zeros_Determinant(n):
  216. return Matrix(n, n, lambda i, j: 0)
  217. def test_det():
  218. a = Matrix(2, 3, [1, 2, 3, 4, 5, 6])
  219. raises(NonSquareMatrixError, lambda: a.det())
  220. z = zeros_Determinant(2)
  221. ey = eye_Determinant(2)
  222. assert z.det() == 0
  223. assert ey.det() == 1
  224. x = Symbol('x')
  225. a = Matrix(0, 0, [])
  226. b = Matrix(1, 1, [5])
  227. c = Matrix(2, 2, [1, 2, 3, 4])
  228. d = Matrix(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 8])
  229. e = Matrix(4, 4,
  230. [x, 1, 2, 3, 4, 5, 6, 7, 2, 9, 10, 11, 12, 13, 14, 14])
  231. from sympy.abc import i, j, k, l, m, n
  232. f = Matrix(3, 3, [i, l, m, 0, j, n, 0, 0, k])
  233. g = Matrix(3, 3, [i, 0, 0, l, j, 0, m, n, k])
  234. h = Matrix(3, 3, [x**3, 0, 0, i, x**-1, 0, j, k, x**-2])
  235. # the method keyword for `det` doesn't kick in until 4x4 matrices,
  236. # so there is no need to test all methods on smaller ones
  237. assert a.det() == 1
  238. assert b.det() == 5
  239. assert c.det() == -2
  240. assert d.det() == 3
  241. assert e.det() == 4*x - 24
  242. assert e.det(method="domain-ge") == 4*x - 24
  243. assert e.det(method='bareiss') == 4*x - 24
  244. assert e.det(method='berkowitz') == 4*x - 24
  245. assert f.det() == i*j*k
  246. assert g.det() == i*j*k
  247. assert h.det() == 1
  248. raises(ValueError, lambda: e.det(iszerofunc="test"))
  249. def test_permanent():
  250. M = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
  251. assert M.per() == 450
  252. for i in range(1, 12):
  253. assert ones(i, i).per() == ones(i, i).T.per() == factorial(i)
  254. assert (ones(i, i)-eye(i)).per() == (ones(i, i)-eye(i)).T.per() == subfactorial(i)
  255. a1, a2, a3, a4, a5 = symbols('a_1 a_2 a_3 a_4 a_5')
  256. M = Matrix([a1, a2, a3, a4, a5])
  257. assert M.per() == M.T.per() == a1 + a2 + a3 + a4 + a5
  258. def test_adjugate():
  259. x = Symbol('x')
  260. e = Matrix(4, 4,
  261. [x, 1, 2, 3, 4, 5, 6, 7, 2, 9, 10, 11, 12, 13, 14, 14])
  262. adj = Matrix([
  263. [ 4, -8, 4, 0],
  264. [ 76, -14*x - 68, 14*x - 8, -4*x + 24],
  265. [-122, 17*x + 142, -21*x + 4, 8*x - 48],
  266. [ 48, -4*x - 72, 8*x, -4*x + 24]])
  267. assert e.adjugate() == adj
  268. assert e.adjugate(method='bareiss') == adj
  269. assert e.adjugate(method='berkowitz') == adj
  270. a = Matrix(2, 3, [1, 2, 3, 4, 5, 6])
  271. raises(NonSquareMatrixError, lambda: a.adjugate())
  272. def test_util():
  273. R = Rational
  274. v1 = Matrix(1, 3, [1, 2, 3])
  275. v2 = Matrix(1, 3, [3, 4, 5])
  276. assert v1.norm() == sqrt(14)
  277. assert v1.project(v2) == Matrix(1, 3, [R(39)/25, R(52)/25, R(13)/5])
  278. assert Matrix.zeros(1, 2) == Matrix(1, 2, [0, 0])
  279. assert ones(1, 2) == Matrix(1, 2, [1, 1])
  280. assert v1.copy() == v1
  281. # cofactor
  282. assert eye(3) == eye(3).cofactor_matrix()
  283. test = Matrix([[1, 3, 2], [2, 6, 3], [2, 3, 6]])
  284. assert test.cofactor_matrix() == \
  285. Matrix([[27, -6, -6], [-12, 2, 3], [-3, 1, 0]])
  286. test = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
  287. assert test.cofactor_matrix() == \
  288. Matrix([[-3, 6, -3], [6, -12, 6], [-3, 6, -3]])
  289. def test_cofactor_and_minors():
  290. x = Symbol('x')
  291. e = Matrix(4, 4,
  292. [x, 1, 2, 3, 4, 5, 6, 7, 2, 9, 10, 11, 12, 13, 14, 14])
  293. m = Matrix([
  294. [ x, 1, 3],
  295. [ 2, 9, 11],
  296. [12, 13, 14]])
  297. cm = Matrix([
  298. [ 4, 76, -122, 48],
  299. [-8, -14*x - 68, 17*x + 142, -4*x - 72],
  300. [ 4, 14*x - 8, -21*x + 4, 8*x],
  301. [ 0, -4*x + 24, 8*x - 48, -4*x + 24]])
  302. sub = Matrix([
  303. [x, 1, 2],
  304. [4, 5, 6],
  305. [2, 9, 10]])
  306. assert e.minor_submatrix(1, 2) == m
  307. assert e.minor_submatrix(-1, -1) == sub
  308. assert e.minor(1, 2) == -17*x - 142
  309. assert e.cofactor(1, 2) == 17*x + 142
  310. assert e.cofactor_matrix() == cm
  311. assert e.cofactor_matrix(method="bareiss") == cm
  312. assert e.cofactor_matrix(method="berkowitz") == cm
  313. raises(ValueError, lambda: e.cofactor(4, 5))
  314. raises(ValueError, lambda: e.minor(4, 5))
  315. raises(ValueError, lambda: e.minor_submatrix(4, 5))
  316. a = Matrix(2, 3, [1, 2, 3, 4, 5, 6])
  317. assert a.minor_submatrix(0, 0) == Matrix([[5, 6]])
  318. raises(ValueError, lambda:
  319. Matrix(0, 0, []).minor_submatrix(0, 0))
  320. raises(NonSquareMatrixError, lambda: a.cofactor(0, 0))
  321. raises(NonSquareMatrixError, lambda: a.minor(0, 0))
  322. raises(NonSquareMatrixError, lambda: a.cofactor_matrix())
  323. def test_charpoly():
  324. x, y = Symbol('x'), Symbol('y')
  325. z, t = Symbol('z'), Symbol('t')
  326. from sympy.abc import a,b,c
  327. m = Matrix(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 9])
  328. assert eye_Determinant(3).charpoly(x) == Poly((x - 1)**3, x)
  329. assert eye_Determinant(3).charpoly(y) == Poly((y - 1)**3, y)
  330. assert m.charpoly() == Poly(x**3 - 15*x**2 - 18*x, x)
  331. raises(NonSquareMatrixError, lambda: Matrix([[1], [2]]).charpoly())
  332. n = Matrix(4, 4, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  333. assert n.charpoly() == Poly(x**4, x)
  334. n = Matrix(4, 4, [45, 0, 0, 0, 0, 23, 0, 0, 0, 0, 87, 0, 0, 0, 0, 12])
  335. assert n.charpoly() == Poly(x**4 - 167*x**3 + 8811*x**2 - 173457*x + 1080540, x)
  336. n = Matrix(3, 3, [x, 0, 0, a, y, 0, b, c, z])
  337. assert n.charpoly() == Poly(t**3 - (x+y+z)*t**2 + t*(x*y+y*z+x*z) - x*y*z, t)