test_reductions.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. from sympy.core.numbers import I
  2. from sympy.core.symbol import symbols
  3. from sympy.matrices.common import _MinimalMatrix, _CastableMatrix
  4. from sympy.matrices.matrices import MatrixReductions
  5. from sympy.testing.pytest import raises
  6. from sympy.matrices import Matrix, zeros
  7. from sympy.core.symbol import Symbol
  8. from sympy.core.numbers import Rational
  9. from sympy.functions.elementary.miscellaneous import sqrt
  10. from sympy.simplify.simplify import simplify
  11. from sympy.abc import x
  12. class ReductionsOnlyMatrix(_MinimalMatrix, _CastableMatrix, MatrixReductions):
  13. pass
  14. def eye_Reductions(n):
  15. return ReductionsOnlyMatrix(n, n, lambda i, j: int(i == j))
  16. def zeros_Reductions(n):
  17. return ReductionsOnlyMatrix(n, n, lambda i, j: 0)
  18. # ReductionsOnlyMatrix tests
  19. def test_row_op():
  20. e = eye_Reductions(3)
  21. raises(ValueError, lambda: e.elementary_row_op("abc"))
  22. raises(ValueError, lambda: e.elementary_row_op())
  23. raises(ValueError, lambda: e.elementary_row_op('n->kn', row=5, k=5))
  24. raises(ValueError, lambda: e.elementary_row_op('n->kn', row=-5, k=5))
  25. raises(ValueError, lambda: e.elementary_row_op('n<->m', row1=1, row2=5))
  26. raises(ValueError, lambda: e.elementary_row_op('n<->m', row1=5, row2=1))
  27. raises(ValueError, lambda: e.elementary_row_op('n<->m', row1=-5, row2=1))
  28. raises(ValueError, lambda: e.elementary_row_op('n<->m', row1=1, row2=-5))
  29. raises(ValueError, lambda: e.elementary_row_op('n->n+km', row1=1, row2=5, k=5))
  30. raises(ValueError, lambda: e.elementary_row_op('n->n+km', row1=5, row2=1, k=5))
  31. raises(ValueError, lambda: e.elementary_row_op('n->n+km', row1=-5, row2=1, k=5))
  32. raises(ValueError, lambda: e.elementary_row_op('n->n+km', row1=1, row2=-5, k=5))
  33. raises(ValueError, lambda: e.elementary_row_op('n->n+km', row1=1, row2=1, k=5))
  34. # test various ways to set arguments
  35. assert e.elementary_row_op("n->kn", 0, 5) == Matrix([[5, 0, 0], [0, 1, 0], [0, 0, 1]])
  36. assert e.elementary_row_op("n->kn", 1, 5) == Matrix([[1, 0, 0], [0, 5, 0], [0, 0, 1]])
  37. assert e.elementary_row_op("n->kn", row=1, k=5) == Matrix([[1, 0, 0], [0, 5, 0], [0, 0, 1]])
  38. assert e.elementary_row_op("n->kn", row1=1, k=5) == Matrix([[1, 0, 0], [0, 5, 0], [0, 0, 1]])
  39. assert e.elementary_row_op("n<->m", 0, 1) == Matrix([[0, 1, 0], [1, 0, 0], [0, 0, 1]])
  40. assert e.elementary_row_op("n<->m", row1=0, row2=1) == Matrix([[0, 1, 0], [1, 0, 0], [0, 0, 1]])
  41. assert e.elementary_row_op("n<->m", row=0, row2=1) == Matrix([[0, 1, 0], [1, 0, 0], [0, 0, 1]])
  42. assert e.elementary_row_op("n->n+km", 0, 5, 1) == Matrix([[1, 5, 0], [0, 1, 0], [0, 0, 1]])
  43. assert e.elementary_row_op("n->n+km", row=0, k=5, row2=1) == Matrix([[1, 5, 0], [0, 1, 0], [0, 0, 1]])
  44. assert e.elementary_row_op("n->n+km", row1=0, k=5, row2=1) == Matrix([[1, 5, 0], [0, 1, 0], [0, 0, 1]])
  45. # make sure the matrix doesn't change size
  46. a = ReductionsOnlyMatrix(2, 3, [0]*6)
  47. assert a.elementary_row_op("n->kn", 1, 5) == Matrix(2, 3, [0]*6)
  48. assert a.elementary_row_op("n<->m", 0, 1) == Matrix(2, 3, [0]*6)
  49. assert a.elementary_row_op("n->n+km", 0, 5, 1) == Matrix(2, 3, [0]*6)
  50. def test_col_op():
  51. e = eye_Reductions(3)
  52. raises(ValueError, lambda: e.elementary_col_op("abc"))
  53. raises(ValueError, lambda: e.elementary_col_op())
  54. raises(ValueError, lambda: e.elementary_col_op('n->kn', col=5, k=5))
  55. raises(ValueError, lambda: e.elementary_col_op('n->kn', col=-5, k=5))
  56. raises(ValueError, lambda: e.elementary_col_op('n<->m', col1=1, col2=5))
  57. raises(ValueError, lambda: e.elementary_col_op('n<->m', col1=5, col2=1))
  58. raises(ValueError, lambda: e.elementary_col_op('n<->m', col1=-5, col2=1))
  59. raises(ValueError, lambda: e.elementary_col_op('n<->m', col1=1, col2=-5))
  60. raises(ValueError, lambda: e.elementary_col_op('n->n+km', col1=1, col2=5, k=5))
  61. raises(ValueError, lambda: e.elementary_col_op('n->n+km', col1=5, col2=1, k=5))
  62. raises(ValueError, lambda: e.elementary_col_op('n->n+km', col1=-5, col2=1, k=5))
  63. raises(ValueError, lambda: e.elementary_col_op('n->n+km', col1=1, col2=-5, k=5))
  64. raises(ValueError, lambda: e.elementary_col_op('n->n+km', col1=1, col2=1, k=5))
  65. # test various ways to set arguments
  66. assert e.elementary_col_op("n->kn", 0, 5) == Matrix([[5, 0, 0], [0, 1, 0], [0, 0, 1]])
  67. assert e.elementary_col_op("n->kn", 1, 5) == Matrix([[1, 0, 0], [0, 5, 0], [0, 0, 1]])
  68. assert e.elementary_col_op("n->kn", col=1, k=5) == Matrix([[1, 0, 0], [0, 5, 0], [0, 0, 1]])
  69. assert e.elementary_col_op("n->kn", col1=1, k=5) == Matrix([[1, 0, 0], [0, 5, 0], [0, 0, 1]])
  70. assert e.elementary_col_op("n<->m", 0, 1) == Matrix([[0, 1, 0], [1, 0, 0], [0, 0, 1]])
  71. assert e.elementary_col_op("n<->m", col1=0, col2=1) == Matrix([[0, 1, 0], [1, 0, 0], [0, 0, 1]])
  72. assert e.elementary_col_op("n<->m", col=0, col2=1) == Matrix([[0, 1, 0], [1, 0, 0], [0, 0, 1]])
  73. assert e.elementary_col_op("n->n+km", 0, 5, 1) == Matrix([[1, 0, 0], [5, 1, 0], [0, 0, 1]])
  74. assert e.elementary_col_op("n->n+km", col=0, k=5, col2=1) == Matrix([[1, 0, 0], [5, 1, 0], [0, 0, 1]])
  75. assert e.elementary_col_op("n->n+km", col1=0, k=5, col2=1) == Matrix([[1, 0, 0], [5, 1, 0], [0, 0, 1]])
  76. # make sure the matrix doesn't change size
  77. a = ReductionsOnlyMatrix(2, 3, [0]*6)
  78. assert a.elementary_col_op("n->kn", 1, 5) == Matrix(2, 3, [0]*6)
  79. assert a.elementary_col_op("n<->m", 0, 1) == Matrix(2, 3, [0]*6)
  80. assert a.elementary_col_op("n->n+km", 0, 5, 1) == Matrix(2, 3, [0]*6)
  81. def test_is_echelon():
  82. zro = zeros_Reductions(3)
  83. ident = eye_Reductions(3)
  84. assert zro.is_echelon
  85. assert ident.is_echelon
  86. a = ReductionsOnlyMatrix(0, 0, [])
  87. assert a.is_echelon
  88. a = ReductionsOnlyMatrix(2, 3, [3, 2, 1, 0, 0, 6])
  89. assert a.is_echelon
  90. a = ReductionsOnlyMatrix(2, 3, [0, 0, 6, 3, 2, 1])
  91. assert not a.is_echelon
  92. x = Symbol('x')
  93. a = ReductionsOnlyMatrix(3, 1, [x, 0, 0])
  94. assert a.is_echelon
  95. a = ReductionsOnlyMatrix(3, 1, [x, x, 0])
  96. assert not a.is_echelon
  97. a = ReductionsOnlyMatrix(3, 3, [0, 0, 0, 1, 2, 3, 0, 0, 0])
  98. assert not a.is_echelon
  99. def test_echelon_form():
  100. # echelon form is not unique, but the result
  101. # must be row-equivalent to the original matrix
  102. # and it must be in echelon form.
  103. a = zeros_Reductions(3)
  104. e = eye_Reductions(3)
  105. # we can assume the zero matrix and the identity matrix shouldn't change
  106. assert a.echelon_form() == a
  107. assert e.echelon_form() == e
  108. a = ReductionsOnlyMatrix(0, 0, [])
  109. assert a.echelon_form() == a
  110. a = ReductionsOnlyMatrix(1, 1, [5])
  111. assert a.echelon_form() == a
  112. # now we get to the real tests
  113. def verify_row_null_space(mat, rows, nulls):
  114. for v in nulls:
  115. assert all(t.is_zero for t in a_echelon*v)
  116. for v in rows:
  117. if not all(t.is_zero for t in v):
  118. assert not all(t.is_zero for t in a_echelon*v.transpose())
  119. a = ReductionsOnlyMatrix(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 9])
  120. nulls = [Matrix([
  121. [ 1],
  122. [-2],
  123. [ 1]])]
  124. rows = [a[i, :] for i in range(a.rows)]
  125. a_echelon = a.echelon_form()
  126. assert a_echelon.is_echelon
  127. verify_row_null_space(a, rows, nulls)
  128. a = ReductionsOnlyMatrix(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 8])
  129. nulls = []
  130. rows = [a[i, :] for i in range(a.rows)]
  131. a_echelon = a.echelon_form()
  132. assert a_echelon.is_echelon
  133. verify_row_null_space(a, rows, nulls)
  134. a = ReductionsOnlyMatrix(3, 3, [2, 1, 3, 0, 0, 0, 2, 1, 3])
  135. nulls = [Matrix([
  136. [Rational(-1, 2)],
  137. [ 1],
  138. [ 0]]),
  139. Matrix([
  140. [Rational(-3, 2)],
  141. [ 0],
  142. [ 1]])]
  143. rows = [a[i, :] for i in range(a.rows)]
  144. a_echelon = a.echelon_form()
  145. assert a_echelon.is_echelon
  146. verify_row_null_space(a, rows, nulls)
  147. # this one requires a row swap
  148. a = ReductionsOnlyMatrix(3, 3, [2, 1, 3, 0, 0, 0, 1, 1, 3])
  149. nulls = [Matrix([
  150. [ 0],
  151. [ -3],
  152. [ 1]])]
  153. rows = [a[i, :] for i in range(a.rows)]
  154. a_echelon = a.echelon_form()
  155. assert a_echelon.is_echelon
  156. verify_row_null_space(a, rows, nulls)
  157. a = ReductionsOnlyMatrix(3, 3, [0, 3, 3, 0, 2, 2, 0, 1, 1])
  158. nulls = [Matrix([
  159. [1],
  160. [0],
  161. [0]]),
  162. Matrix([
  163. [ 0],
  164. [-1],
  165. [ 1]])]
  166. rows = [a[i, :] for i in range(a.rows)]
  167. a_echelon = a.echelon_form()
  168. assert a_echelon.is_echelon
  169. verify_row_null_space(a, rows, nulls)
  170. a = ReductionsOnlyMatrix(2, 3, [2, 2, 3, 3, 3, 0])
  171. nulls = [Matrix([
  172. [-1],
  173. [1],
  174. [0]])]
  175. rows = [a[i, :] for i in range(a.rows)]
  176. a_echelon = a.echelon_form()
  177. assert a_echelon.is_echelon
  178. verify_row_null_space(a, rows, nulls)
  179. def test_rref():
  180. e = ReductionsOnlyMatrix(0, 0, [])
  181. assert e.rref(pivots=False) == e
  182. e = ReductionsOnlyMatrix(1, 1, [1])
  183. a = ReductionsOnlyMatrix(1, 1, [5])
  184. assert e.rref(pivots=False) == a.rref(pivots=False) == e
  185. a = ReductionsOnlyMatrix(3, 1, [1, 2, 3])
  186. assert a.rref(pivots=False) == Matrix([[1], [0], [0]])
  187. a = ReductionsOnlyMatrix(1, 3, [1, 2, 3])
  188. assert a.rref(pivots=False) == Matrix([[1, 2, 3]])
  189. a = ReductionsOnlyMatrix(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 9])
  190. assert a.rref(pivots=False) == Matrix([
  191. [1, 0, -1],
  192. [0, 1, 2],
  193. [0, 0, 0]])
  194. a = ReductionsOnlyMatrix(3, 3, [1, 2, 3, 1, 2, 3, 1, 2, 3])
  195. b = ReductionsOnlyMatrix(3, 3, [1, 2, 3, 0, 0, 0, 0, 0, 0])
  196. c = ReductionsOnlyMatrix(3, 3, [0, 0, 0, 1, 2, 3, 0, 0, 0])
  197. d = ReductionsOnlyMatrix(3, 3, [0, 0, 0, 0, 0, 0, 1, 2, 3])
  198. assert a.rref(pivots=False) == \
  199. b.rref(pivots=False) == \
  200. c.rref(pivots=False) == \
  201. d.rref(pivots=False) == b
  202. e = eye_Reductions(3)
  203. z = zeros_Reductions(3)
  204. assert e.rref(pivots=False) == e
  205. assert z.rref(pivots=False) == z
  206. a = ReductionsOnlyMatrix([
  207. [ 0, 0, 1, 2, 2, -5, 3],
  208. [-1, 5, 2, 2, 1, -7, 5],
  209. [ 0, 0, -2, -3, -3, 8, -5],
  210. [-1, 5, 0, -1, -2, 1, 0]])
  211. mat, pivot_offsets = a.rref()
  212. assert mat == Matrix([
  213. [1, -5, 0, 0, 1, 1, -1],
  214. [0, 0, 1, 0, 0, -1, 1],
  215. [0, 0, 0, 1, 1, -2, 1],
  216. [0, 0, 0, 0, 0, 0, 0]])
  217. assert pivot_offsets == (0, 2, 3)
  218. a = ReductionsOnlyMatrix([[Rational(1, 19), Rational(1, 5), 2, 3],
  219. [ 4, 5, 6, 7],
  220. [ 8, 9, 10, 11],
  221. [ 12, 13, 14, 15]])
  222. assert a.rref(pivots=False) == Matrix([
  223. [1, 0, 0, Rational(-76, 157)],
  224. [0, 1, 0, Rational(-5, 157)],
  225. [0, 0, 1, Rational(238, 157)],
  226. [0, 0, 0, 0]])
  227. x = Symbol('x')
  228. a = ReductionsOnlyMatrix(2, 3, [x, 1, 1, sqrt(x), x, 1])
  229. for i, j in zip(a.rref(pivots=False),
  230. [1, 0, sqrt(x)*(-x + 1)/(-x**Rational(5, 2) + x),
  231. 0, 1, 1/(sqrt(x) + x + 1)]):
  232. assert simplify(i - j).is_zero
  233. def test_issue_17827():
  234. C = Matrix([
  235. [3, 4, -1, 1],
  236. [9, 12, -3, 3],
  237. [0, 2, 1, 3],
  238. [2, 3, 0, -2],
  239. [0, 3, 3, -5],
  240. [8, 15, 0, 6]
  241. ])
  242. # Tests for row/col within valid range
  243. D = C.elementary_row_op('n<->m', row1=2, row2=5)
  244. E = C.elementary_row_op('n->n+km', row1=5, row2=3, k=-4)
  245. F = C.elementary_row_op('n->kn', row=5, k=2)
  246. assert(D[5, :] == Matrix([[0, 2, 1, 3]]))
  247. assert(E[5, :] == Matrix([[0, 3, 0, 14]]))
  248. assert(F[5, :] == Matrix([[16, 30, 0, 12]]))
  249. # Tests for row/col out of range
  250. raises(ValueError, lambda: C.elementary_row_op('n<->m', row1=2, row2=6))
  251. raises(ValueError, lambda: C.elementary_row_op('n->kn', row=7, k=2))
  252. raises(ValueError, lambda: C.elementary_row_op('n->n+km', row1=-1, row2=5, k=2))
  253. def test_rank():
  254. m = Matrix([[1, 2], [x, 1 - 1/x]])
  255. assert m.rank() == 2
  256. n = Matrix(3, 3, range(1, 10))
  257. assert n.rank() == 2
  258. p = zeros(3)
  259. assert p.rank() == 0
  260. def test_issue_11434():
  261. ax, ay, bx, by, cx, cy, dx, dy, ex, ey, t0, t1 = \
  262. symbols('a_x a_y b_x b_y c_x c_y d_x d_y e_x e_y t_0 t_1')
  263. M = Matrix([[ax, ay, ax*t0, ay*t0, 0],
  264. [bx, by, bx*t0, by*t0, 0],
  265. [cx, cy, cx*t0, cy*t0, 1],
  266. [dx, dy, dx*t0, dy*t0, 1],
  267. [ex, ey, 2*ex*t1 - ex*t0, 2*ey*t1 - ey*t0, 0]])
  268. assert M.rank() == 4
  269. def test_rank_regression_from_so():
  270. # see:
  271. # https://stackoverflow.com/questions/19072700/why-does-sympy-give-me-the-wrong-answer-when-i-row-reduce-a-symbolic-matrix
  272. nu, lamb = symbols('nu, lambda')
  273. A = Matrix([[-3*nu, 1, 0, 0],
  274. [ 3*nu, -2*nu - 1, 2, 0],
  275. [ 0, 2*nu, (-1*nu) - lamb - 2, 3],
  276. [ 0, 0, nu + lamb, -3]])
  277. expected_reduced = Matrix([[1, 0, 0, 1/(nu**2*(-lamb - nu))],
  278. [0, 1, 0, 3/(nu*(-lamb - nu))],
  279. [0, 0, 1, 3/(-lamb - nu)],
  280. [0, 0, 0, 0]])
  281. expected_pivots = (0, 1, 2)
  282. reduced, pivots = A.rref()
  283. assert simplify(expected_reduced - reduced) == zeros(*A.shape)
  284. assert pivots == expected_pivots
  285. def test_issue_15872():
  286. A = Matrix([[1, 1, 1, 0], [-2, -1, 0, -1], [0, 0, -1, -1], [0, 0, 2, 1]])
  287. B = A - Matrix.eye(4) * I
  288. assert B.rank() == 3
  289. assert (B**2).rank() == 2
  290. assert (B**3).rank() == 2