test_matmul.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. from sympy.core import I, symbols, Basic, Mul, S
  2. from sympy.core.mul import mul
  3. from sympy.functions import adjoint, transpose
  4. from sympy.matrices.common import ShapeError
  5. from sympy.matrices import (Identity, Inverse, Matrix, MatrixSymbol, ZeroMatrix,
  6. eye, ImmutableMatrix)
  7. from sympy.matrices.expressions import Adjoint, Transpose, det, MatPow
  8. from sympy.matrices.expressions.special import GenericIdentity
  9. from sympy.matrices.expressions.matmul import (factor_in_front, remove_ids,
  10. MatMul, combine_powers, any_zeros, unpack, only_squares)
  11. from sympy.strategies import null_safe
  12. from sympy.assumptions.ask import Q
  13. from sympy.assumptions.refine import refine
  14. from sympy.core.symbol import Symbol
  15. from sympy.testing.pytest import XFAIL, raises
  16. n, m, l, k = symbols('n m l k', integer=True)
  17. x = symbols('x')
  18. A = MatrixSymbol('A', n, m)
  19. B = MatrixSymbol('B', m, l)
  20. C = MatrixSymbol('C', n, n)
  21. D = MatrixSymbol('D', n, n)
  22. E = MatrixSymbol('E', m, n)
  23. def test_evaluate():
  24. assert MatMul(C, C, evaluate=True) == MatMul(C, C).doit()
  25. def test_adjoint():
  26. assert adjoint(A*B) == Adjoint(B)*Adjoint(A)
  27. assert adjoint(2*A*B) == 2*Adjoint(B)*Adjoint(A)
  28. assert adjoint(2*I*C) == -2*I*Adjoint(C)
  29. M = Matrix(2, 2, [1, 2 + I, 3, 4])
  30. MA = Matrix(2, 2, [1, 3, 2 - I, 4])
  31. assert adjoint(M) == MA
  32. assert adjoint(2*M) == 2*MA
  33. assert adjoint(MatMul(2, M)) == MatMul(2, MA).doit()
  34. def test_transpose():
  35. assert transpose(A*B) == Transpose(B)*Transpose(A)
  36. assert transpose(2*A*B) == 2*Transpose(B)*Transpose(A)
  37. assert transpose(2*I*C) == 2*I*Transpose(C)
  38. M = Matrix(2, 2, [1, 2 + I, 3, 4])
  39. MT = Matrix(2, 2, [1, 3, 2 + I, 4])
  40. assert transpose(M) == MT
  41. assert transpose(2*M) == 2*MT
  42. assert transpose(x*M) == x*MT
  43. assert transpose(MatMul(2, M)) == MatMul(2, MT).doit()
  44. def test_factor_in_front():
  45. assert factor_in_front(MatMul(A, 2, B, evaluate=False)) ==\
  46. MatMul(2, A, B, evaluate=False)
  47. def test_remove_ids():
  48. assert remove_ids(MatMul(A, Identity(m), B, evaluate=False)) == \
  49. MatMul(A, B, evaluate=False)
  50. assert null_safe(remove_ids)(MatMul(Identity(n), evaluate=False)) == \
  51. MatMul(Identity(n), evaluate=False)
  52. def test_combine_powers():
  53. assert combine_powers(MatMul(D, Inverse(D), D, evaluate=False)) == \
  54. MatMul(Identity(n), D, evaluate=False)
  55. assert combine_powers(MatMul(B.T, Inverse(E*A), E, A, B, evaluate=False)) == \
  56. MatMul(B.T, Identity(m), B, evaluate=False)
  57. assert combine_powers(MatMul(A, E, Inverse(A*E), D, evaluate=False)) == \
  58. MatMul(Identity(n), D, evaluate=False)
  59. def test_any_zeros():
  60. assert any_zeros(MatMul(A, ZeroMatrix(m, k), evaluate=False)) == \
  61. ZeroMatrix(n, k)
  62. def test_unpack():
  63. assert unpack(MatMul(A, evaluate=False)) == A
  64. x = MatMul(A, B)
  65. assert unpack(x) == x
  66. def test_only_squares():
  67. assert only_squares(C) == [C]
  68. assert only_squares(C, D) == [C, D]
  69. assert only_squares(C, A, A.T, D) == [C, A*A.T, D]
  70. def test_determinant():
  71. assert det(2*C) == 2**n*det(C)
  72. assert det(2*C*D) == 2**n*det(C)*det(D)
  73. assert det(3*C*A*A.T*D) == 3**n*det(C)*det(A*A.T)*det(D)
  74. def test_doit():
  75. assert MatMul(C, 2, D).args == (C, 2, D)
  76. assert MatMul(C, 2, D).doit().args == (2, C, D)
  77. assert MatMul(C, Transpose(D*C)).args == (C, Transpose(D*C))
  78. assert MatMul(C, Transpose(D*C)).doit(deep=True).args == (C, C.T, D.T)
  79. def test_doit_drills_down():
  80. X = ImmutableMatrix([[1, 2], [3, 4]])
  81. Y = ImmutableMatrix([[2, 3], [4, 5]])
  82. assert MatMul(X, MatPow(Y, 2)).doit() == X*Y**2
  83. assert MatMul(C, Transpose(D*C)).doit().args == (C, C.T, D.T)
  84. def test_doit_deep_false_still_canonical():
  85. assert (MatMul(C, Transpose(D*C), 2).doit(deep=False).args ==
  86. (2, C, Transpose(D*C)))
  87. def test_matmul_scalar_Matrix_doit():
  88. # Issue 9053
  89. X = Matrix([[1, 2], [3, 4]])
  90. assert MatMul(2, X).doit() == 2*X
  91. def test_matmul_sympify():
  92. assert isinstance(MatMul(eye(1), eye(1)).args[0], Basic)
  93. def test_collapse_MatrixBase():
  94. A = Matrix([[1, 1], [1, 1]])
  95. B = Matrix([[1, 2], [3, 4]])
  96. assert MatMul(A, B).doit() == ImmutableMatrix([[4, 6], [4, 6]])
  97. def test_refine():
  98. assert refine(C*C.T*D, Q.orthogonal(C)).doit() == D
  99. kC = k*C
  100. assert refine(kC*C.T, Q.orthogonal(C)).doit() == k*Identity(n)
  101. assert refine(kC* kC.T, Q.orthogonal(C)).doit() == (k**2)*Identity(n)
  102. def test_matmul_no_matrices():
  103. assert MatMul(1) == 1
  104. assert MatMul(n, m) == n*m
  105. assert not isinstance(MatMul(n, m), MatMul)
  106. def test_matmul_args_cnc():
  107. assert MatMul(n, A, A.T).args_cnc() == [[n], [A, A.T]]
  108. assert MatMul(A, A.T).args_cnc() == [[], [A, A.T]]
  109. @XFAIL
  110. def test_matmul_args_cnc_symbols():
  111. # Not currently supported
  112. a, b = symbols('a b', commutative=False)
  113. assert MatMul(n, a, b, A, A.T).args_cnc() == [[n], [a, b, A, A.T]]
  114. assert MatMul(n, a, A, b, A.T).args_cnc() == [[n], [a, A, b, A.T]]
  115. def test_issue_12950():
  116. M = Matrix([[Symbol("x")]]) * MatrixSymbol("A", 1, 1)
  117. assert MatrixSymbol("A", 1, 1).as_explicit()[0]*Symbol('x') == M.as_explicit()[0]
  118. def test_construction_with_Mul():
  119. assert Mul(C, D) == MatMul(C, D)
  120. assert Mul(D, C) == MatMul(D, C)
  121. def test_construction_with_mul():
  122. assert mul(C, D) == MatMul(C, D)
  123. assert mul(D, C) == MatMul(D, C)
  124. assert mul(C, D) != MatMul(D, C)
  125. def test_generic_identity():
  126. assert MatMul.identity == GenericIdentity()
  127. assert MatMul.identity != S.One
  128. def test_issue_23519():
  129. N = Symbol("N", integer=True)
  130. M1 = MatrixSymbol("M1", N, N)
  131. M2 = MatrixSymbol("M2", N, N)
  132. I = Identity(N)
  133. z = (M2 + 2 * (M2 + I) * M1 + I)
  134. assert z.coeff(M1) == 2*I + 2*M2
  135. def test_shape_error():
  136. A = MatrixSymbol('A', 2, 2)
  137. B = MatrixSymbol('B', 3, 3)
  138. raises(ShapeError, lambda: MatMul(A, B))