applied-ai-018 commited on
Commit
373249e
·
verified ·
1 Parent(s): 8931e57

Add files using upload-large-folder tool

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. ckpts/universal/global_step120/zero/4.mlp.dense_h_to_4h_swiglu.weight/exp_avg.pt +3 -0
  2. ckpts/universal/global_step120/zero/4.mlp.dense_h_to_4h_swiglu.weight/fp32.pt +3 -0
  3. ckpts/universal/global_step120/zero/5.mlp.dense_4h_to_h.weight/exp_avg.pt +3 -0
  4. ckpts/universal/global_step120/zero/7.mlp.dense_h_to_4h.weight/exp_avg_sq.pt +3 -0
  5. ckpts/universal/global_step120/zero/7.mlp.dense_h_to_4h.weight/fp32.pt +3 -0
  6. venv/lib/python3.10/site-packages/sympy/matrices/__pycache__/common.cpython-310.pyc +0 -0
  7. venv/lib/python3.10/site-packages/sympy/matrices/__pycache__/decompositions.cpython-310.pyc +0 -0
  8. venv/lib/python3.10/site-packages/sympy/matrices/__pycache__/dense.cpython-310.pyc +0 -0
  9. venv/lib/python3.10/site-packages/sympy/matrices/__pycache__/eigen.cpython-310.pyc +0 -0
  10. venv/lib/python3.10/site-packages/sympy/matrices/__pycache__/graph.cpython-310.pyc +0 -0
  11. venv/lib/python3.10/site-packages/sympy/matrices/__pycache__/immutable.cpython-310.pyc +0 -0
  12. venv/lib/python3.10/site-packages/sympy/matrices/__pycache__/inverse.cpython-310.pyc +0 -0
  13. venv/lib/python3.10/site-packages/sympy/matrices/__pycache__/normalforms.cpython-310.pyc +0 -0
  14. venv/lib/python3.10/site-packages/sympy/matrices/__pycache__/reductions.cpython-310.pyc +0 -0
  15. venv/lib/python3.10/site-packages/sympy/matrices/__pycache__/solvers.cpython-310.pyc +0 -0
  16. venv/lib/python3.10/site-packages/sympy/matrices/__pycache__/sparse.cpython-310.pyc +0 -0
  17. venv/lib/python3.10/site-packages/sympy/matrices/__pycache__/sparsetools.cpython-310.pyc +0 -0
  18. venv/lib/python3.10/site-packages/sympy/matrices/__pycache__/subspaces.cpython-310.pyc +0 -0
  19. venv/lib/python3.10/site-packages/sympy/matrices/__pycache__/utilities.cpython-310.pyc +0 -0
  20. venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_adjoint.cpython-310.pyc +0 -0
  21. venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_applyfunc.cpython-310.pyc +0 -0
  22. venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_blockmatrix.cpython-310.pyc +0 -0
  23. venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_derivatives.cpython-310.pyc +0 -0
  24. venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_determinant.cpython-310.pyc +0 -0
  25. venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_dotproduct.cpython-310.pyc +0 -0
  26. venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_factorizations.cpython-310.pyc +0 -0
  27. venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_fourier.cpython-310.pyc +0 -0
  28. venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_funcmatrix.cpython-310.pyc +0 -0
  29. venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_hadamard.cpython-310.pyc +0 -0
  30. venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_indexing.cpython-310.pyc +0 -0
  31. venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_inverse.cpython-310.pyc +0 -0
  32. venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_kronecker.cpython-310.pyc +0 -0
  33. venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_matadd.cpython-310.pyc +0 -0
  34. venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_matexpr.cpython-310.pyc +0 -0
  35. venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_matpow.cpython-310.pyc +0 -0
  36. venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_permutation.cpython-310.pyc +0 -0
  37. venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_sets.cpython-310.pyc +0 -0
  38. venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_slice.cpython-310.pyc +0 -0
  39. venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_transpose.cpython-310.pyc +0 -0
  40. venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/test_blockmatrix.py +445 -0
  41. venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/test_derivatives.py +477 -0
  42. venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/test_determinant.py +62 -0
  43. venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/test_diagonal.py +156 -0
  44. venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/test_dotproduct.py +35 -0
  45. venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/test_factorizations.py +29 -0
  46. venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/test_funcmatrix.py +56 -0
  47. venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/test_indexing.py +299 -0
  48. venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/test_inverse.py +62 -0
  49. venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/test_kronecker.py +150 -0
  50. venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/test_matadd.py +58 -0
ckpts/universal/global_step120/zero/4.mlp.dense_h_to_4h_swiglu.weight/exp_avg.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9d8482768118ba69c44357b1bf36bef5f8616e7bbd205f619363d7704846dc2d
3
+ size 33555612
ckpts/universal/global_step120/zero/4.mlp.dense_h_to_4h_swiglu.weight/fp32.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0ded136b07ab79eeaeebe5ca9f43b7943fafdf53f9f1ac9f6f903432ec9686e6
3
+ size 33555533
ckpts/universal/global_step120/zero/5.mlp.dense_4h_to_h.weight/exp_avg.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ba883ae15fda29f7dee2dd791763d96a7a34fc9dcaf284afda4692eebc196242
3
+ size 33555612
ckpts/universal/global_step120/zero/7.mlp.dense_h_to_4h.weight/exp_avg_sq.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5b0645a25322010737954b1f4dd9836f4c5bc31eed3b7c79c592f460a13ee585
3
+ size 33555627
ckpts/universal/global_step120/zero/7.mlp.dense_h_to_4h.weight/fp32.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:45a589ec2dbc155011808308467c1c267bd965e3279f048c7de5a85b2ab82d59
3
+ size 33555533
venv/lib/python3.10/site-packages/sympy/matrices/__pycache__/common.cpython-310.pyc ADDED
Binary file (99.9 kB). View file
 
venv/lib/python3.10/site-packages/sympy/matrices/__pycache__/decompositions.cpython-310.pyc ADDED
Binary file (41.4 kB). View file
 
venv/lib/python3.10/site-packages/sympy/matrices/__pycache__/dense.cpython-310.pyc ADDED
Binary file (31.2 kB). View file
 
venv/lib/python3.10/site-packages/sympy/matrices/__pycache__/eigen.cpython-310.pyc ADDED
Binary file (36.9 kB). View file
 
venv/lib/python3.10/site-packages/sympy/matrices/__pycache__/graph.cpython-310.pyc ADDED
Binary file (9.11 kB). View file
 
venv/lib/python3.10/site-packages/sympy/matrices/__pycache__/immutable.cpython-310.pyc ADDED
Binary file (6.24 kB). View file
 
venv/lib/python3.10/site-packages/sympy/matrices/__pycache__/inverse.cpython-310.pyc ADDED
Binary file (11.4 kB). View file
 
venv/lib/python3.10/site-packages/sympy/matrices/__pycache__/normalforms.cpython-310.pyc ADDED
Binary file (4.39 kB). View file
 
venv/lib/python3.10/site-packages/sympy/matrices/__pycache__/reductions.cpython-310.pyc ADDED
Binary file (9.83 kB). View file
 
venv/lib/python3.10/site-packages/sympy/matrices/__pycache__/solvers.cpython-310.pyc ADDED
Binary file (21.5 kB). View file
 
venv/lib/python3.10/site-packages/sympy/matrices/__pycache__/sparse.cpython-310.pyc ADDED
Binary file (13.8 kB). View file
 
venv/lib/python3.10/site-packages/sympy/matrices/__pycache__/sparsetools.cpython-310.pyc ADDED
Binary file (8.52 kB). View file
 
venv/lib/python3.10/site-packages/sympy/matrices/__pycache__/subspaces.cpython-310.pyc ADDED
Binary file (4.36 kB). View file
 
venv/lib/python3.10/site-packages/sympy/matrices/__pycache__/utilities.cpython-310.pyc ADDED
Binary file (2.72 kB). View file
 
venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_adjoint.cpython-310.pyc ADDED
Binary file (1.32 kB). View file
 
venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_applyfunc.cpython-310.pyc ADDED
Binary file (3.66 kB). View file
 
venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_blockmatrix.cpython-310.pyc ADDED
Binary file (15.8 kB). View file
 
venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_derivatives.cpython-310.pyc ADDED
Binary file (13.3 kB). View file
 
venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_determinant.cpython-310.pyc ADDED
Binary file (2.82 kB). View file
 
venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_dotproduct.cpython-310.pyc ADDED
Binary file (1.92 kB). View file
 
venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_factorizations.cpython-310.pyc ADDED
Binary file (1.34 kB). View file
 
venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_fourier.cpython-310.pyc ADDED
Binary file (2.48 kB). View file
 
venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_funcmatrix.cpython-310.pyc ADDED
Binary file (3.83 kB). View file
 
venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_hadamard.cpython-310.pyc ADDED
Binary file (5.6 kB). View file
 
venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_indexing.cpython-310.pyc ADDED
Binary file (12 kB). View file
 
venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_inverse.cpython-310.pyc ADDED
Binary file (2.74 kB). View file
 
venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_kronecker.cpython-310.pyc ADDED
Binary file (5.84 kB). View file
 
venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_matadd.cpython-310.pyc ADDED
Binary file (3.13 kB). View file
 
venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_matexpr.cpython-310.pyc ADDED
Binary file (21.8 kB). View file
 
venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_matpow.cpython-310.pyc ADDED
Binary file (8.65 kB). View file
 
venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_permutation.cpython-310.pyc ADDED
Binary file (5.91 kB). View file
 
venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_sets.cpython-310.pyc ADDED
Binary file (2.35 kB). View file
 
venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_slice.cpython-310.pyc ADDED
Binary file (3.7 kB). View file
 
venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_transpose.cpython-310.pyc ADDED
Binary file (2.66 kB). View file
 
venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/test_blockmatrix.py ADDED
@@ -0,0 +1,445 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.matrices.expressions.trace import Trace
2
+ from sympy.testing.pytest import raises, slow
3
+ from sympy.matrices.expressions.blockmatrix import (
4
+ block_collapse, bc_matmul, bc_block_plus_ident, BlockDiagMatrix,
5
+ BlockMatrix, bc_dist, bc_matadd, bc_transpose, bc_inverse,
6
+ blockcut, reblock_2x2, deblock)
7
+ from sympy.matrices.expressions import (MatrixSymbol, Identity,
8
+ Inverse, trace, Transpose, det, ZeroMatrix, OneMatrix)
9
+ from sympy.matrices.common import NonInvertibleMatrixError
10
+ from sympy.matrices import (
11
+ Matrix, ImmutableMatrix, ImmutableSparseMatrix)
12
+ from sympy.core import Tuple, symbols, Expr, S
13
+ from sympy.functions import transpose, im, re
14
+
15
+ i, j, k, l, m, n, p = symbols('i:n, p', integer=True)
16
+ A = MatrixSymbol('A', n, n)
17
+ B = MatrixSymbol('B', n, n)
18
+ C = MatrixSymbol('C', n, n)
19
+ D = MatrixSymbol('D', n, n)
20
+ G = MatrixSymbol('G', n, n)
21
+ H = MatrixSymbol('H', n, n)
22
+ b1 = BlockMatrix([[G, H]])
23
+ b2 = BlockMatrix([[G], [H]])
24
+
25
+ def test_bc_matmul():
26
+ assert bc_matmul(H*b1*b2*G) == BlockMatrix([[(H*G*G + H*H*H)*G]])
27
+
28
+ def test_bc_matadd():
29
+ assert bc_matadd(BlockMatrix([[G, H]]) + BlockMatrix([[H, H]])) == \
30
+ BlockMatrix([[G+H, H+H]])
31
+
32
+ def test_bc_transpose():
33
+ assert bc_transpose(Transpose(BlockMatrix([[A, B], [C, D]]))) == \
34
+ BlockMatrix([[A.T, C.T], [B.T, D.T]])
35
+
36
+ def test_bc_dist_diag():
37
+ A = MatrixSymbol('A', n, n)
38
+ B = MatrixSymbol('B', m, m)
39
+ C = MatrixSymbol('C', l, l)
40
+ X = BlockDiagMatrix(A, B, C)
41
+
42
+ assert bc_dist(X+X).equals(BlockDiagMatrix(2*A, 2*B, 2*C))
43
+
44
+ def test_block_plus_ident():
45
+ A = MatrixSymbol('A', n, n)
46
+ B = MatrixSymbol('B', n, m)
47
+ C = MatrixSymbol('C', m, n)
48
+ D = MatrixSymbol('D', m, m)
49
+ X = BlockMatrix([[A, B], [C, D]])
50
+ Z = MatrixSymbol('Z', n + m, n + m)
51
+ assert bc_block_plus_ident(X + Identity(m + n) + Z) == \
52
+ BlockDiagMatrix(Identity(n), Identity(m)) + X + Z
53
+
54
+ def test_BlockMatrix():
55
+ A = MatrixSymbol('A', n, m)
56
+ B = MatrixSymbol('B', n, k)
57
+ C = MatrixSymbol('C', l, m)
58
+ D = MatrixSymbol('D', l, k)
59
+ M = MatrixSymbol('M', m + k, p)
60
+ N = MatrixSymbol('N', l + n, k + m)
61
+ X = BlockMatrix(Matrix([[A, B], [C, D]]))
62
+
63
+ assert X.__class__(*X.args) == X
64
+
65
+ # block_collapse does nothing on normal inputs
66
+ E = MatrixSymbol('E', n, m)
67
+ assert block_collapse(A + 2*E) == A + 2*E
68
+ F = MatrixSymbol('F', m, m)
69
+ assert block_collapse(E.T*A*F) == E.T*A*F
70
+
71
+ assert X.shape == (l + n, k + m)
72
+ assert X.blockshape == (2, 2)
73
+ assert transpose(X) == BlockMatrix(Matrix([[A.T, C.T], [B.T, D.T]]))
74
+ assert transpose(X).shape == X.shape[::-1]
75
+
76
+ # Test that BlockMatrices and MatrixSymbols can still mix
77
+ assert (X*M).is_MatMul
78
+ assert X._blockmul(M).is_MatMul
79
+ assert (X*M).shape == (n + l, p)
80
+ assert (X + N).is_MatAdd
81
+ assert X._blockadd(N).is_MatAdd
82
+ assert (X + N).shape == X.shape
83
+
84
+ E = MatrixSymbol('E', m, 1)
85
+ F = MatrixSymbol('F', k, 1)
86
+
87
+ Y = BlockMatrix(Matrix([[E], [F]]))
88
+
89
+ assert (X*Y).shape == (l + n, 1)
90
+ assert block_collapse(X*Y).blocks[0, 0] == A*E + B*F
91
+ assert block_collapse(X*Y).blocks[1, 0] == C*E + D*F
92
+
93
+ # block_collapse passes down into container objects, transposes, and inverse
94
+ assert block_collapse(transpose(X*Y)) == transpose(block_collapse(X*Y))
95
+ assert block_collapse(Tuple(X*Y, 2*X)) == (
96
+ block_collapse(X*Y), block_collapse(2*X))
97
+
98
+ # Make sure that MatrixSymbols will enter 1x1 BlockMatrix if it simplifies
99
+ Ab = BlockMatrix([[A]])
100
+ Z = MatrixSymbol('Z', *A.shape)
101
+ assert block_collapse(Ab + Z) == A + Z
102
+
103
+ def test_block_collapse_explicit_matrices():
104
+ A = Matrix([[1, 2], [3, 4]])
105
+ assert block_collapse(BlockMatrix([[A]])) == A
106
+
107
+ A = ImmutableSparseMatrix([[1, 2], [3, 4]])
108
+ assert block_collapse(BlockMatrix([[A]])) == A
109
+
110
+ def test_issue_17624():
111
+ a = MatrixSymbol("a", 2, 2)
112
+ z = ZeroMatrix(2, 2)
113
+ b = BlockMatrix([[a, z], [z, z]])
114
+ assert block_collapse(b * b) == BlockMatrix([[a**2, z], [z, z]])
115
+ assert block_collapse(b * b * b) == BlockMatrix([[a**3, z], [z, z]])
116
+
117
+ def test_issue_18618():
118
+ A = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
119
+ assert A == Matrix(BlockDiagMatrix(A))
120
+
121
+ def test_BlockMatrix_trace():
122
+ A, B, C, D = [MatrixSymbol(s, 3, 3) for s in 'ABCD']
123
+ X = BlockMatrix([[A, B], [C, D]])
124
+ assert trace(X) == trace(A) + trace(D)
125
+ assert trace(BlockMatrix([ZeroMatrix(n, n)])) == 0
126
+
127
+ def test_BlockMatrix_Determinant():
128
+ A, B, C, D = [MatrixSymbol(s, 3, 3) for s in 'ABCD']
129
+ X = BlockMatrix([[A, B], [C, D]])
130
+ from sympy.assumptions.ask import Q
131
+ from sympy.assumptions.assume import assuming
132
+ with assuming(Q.invertible(A)):
133
+ assert det(X) == det(A) * det(X.schur('A'))
134
+
135
+ assert isinstance(det(X), Expr)
136
+ assert det(BlockMatrix([A])) == det(A)
137
+ assert det(BlockMatrix([ZeroMatrix(n, n)])) == 0
138
+
139
+ def test_squareBlockMatrix():
140
+ A = MatrixSymbol('A', n, n)
141
+ B = MatrixSymbol('B', n, m)
142
+ C = MatrixSymbol('C', m, n)
143
+ D = MatrixSymbol('D', m, m)
144
+ X = BlockMatrix([[A, B], [C, D]])
145
+ Y = BlockMatrix([[A]])
146
+
147
+ assert X.is_square
148
+
149
+ Q = X + Identity(m + n)
150
+ assert (block_collapse(Q) ==
151
+ BlockMatrix([[A + Identity(n), B], [C, D + Identity(m)]]))
152
+
153
+ assert (X + MatrixSymbol('Q', n + m, n + m)).is_MatAdd
154
+ assert (X * MatrixSymbol('Q', n + m, n + m)).is_MatMul
155
+
156
+ assert block_collapse(Y.I) == A.I
157
+
158
+ assert isinstance(X.inverse(), Inverse)
159
+
160
+ assert not X.is_Identity
161
+
162
+ Z = BlockMatrix([[Identity(n), B], [C, D]])
163
+ assert not Z.is_Identity
164
+
165
+
166
+ def test_BlockMatrix_2x2_inverse_symbolic():
167
+ A = MatrixSymbol('A', n, m)
168
+ B = MatrixSymbol('B', n, k - m)
169
+ C = MatrixSymbol('C', k - n, m)
170
+ D = MatrixSymbol('D', k - n, k - m)
171
+ X = BlockMatrix([[A, B], [C, D]])
172
+ assert X.is_square and X.shape == (k, k)
173
+ assert isinstance(block_collapse(X.I), Inverse) # Can't invert when none of the blocks is square
174
+
175
+ # test code path where only A is invertible
176
+ A = MatrixSymbol('A', n, n)
177
+ B = MatrixSymbol('B', n, m)
178
+ C = MatrixSymbol('C', m, n)
179
+ D = ZeroMatrix(m, m)
180
+ X = BlockMatrix([[A, B], [C, D]])
181
+ assert block_collapse(X.inverse()) == BlockMatrix([
182
+ [A.I + A.I * B * X.schur('A').I * C * A.I, -A.I * B * X.schur('A').I],
183
+ [-X.schur('A').I * C * A.I, X.schur('A').I],
184
+ ])
185
+
186
+ # test code path where only B is invertible
187
+ A = MatrixSymbol('A', n, m)
188
+ B = MatrixSymbol('B', n, n)
189
+ C = ZeroMatrix(m, m)
190
+ D = MatrixSymbol('D', m, n)
191
+ X = BlockMatrix([[A, B], [C, D]])
192
+ assert block_collapse(X.inverse()) == BlockMatrix([
193
+ [-X.schur('B').I * D * B.I, X.schur('B').I],
194
+ [B.I + B.I * A * X.schur('B').I * D * B.I, -B.I * A * X.schur('B').I],
195
+ ])
196
+
197
+ # test code path where only C is invertible
198
+ A = MatrixSymbol('A', n, m)
199
+ B = ZeroMatrix(n, n)
200
+ C = MatrixSymbol('C', m, m)
201
+ D = MatrixSymbol('D', m, n)
202
+ X = BlockMatrix([[A, B], [C, D]])
203
+ assert block_collapse(X.inverse()) == BlockMatrix([
204
+ [-C.I * D * X.schur('C').I, C.I + C.I * D * X.schur('C').I * A * C.I],
205
+ [X.schur('C').I, -X.schur('C').I * A * C.I],
206
+ ])
207
+
208
+ # test code path where only D is invertible
209
+ A = ZeroMatrix(n, n)
210
+ B = MatrixSymbol('B', n, m)
211
+ C = MatrixSymbol('C', m, n)
212
+ D = MatrixSymbol('D', m, m)
213
+ X = BlockMatrix([[A, B], [C, D]])
214
+ assert block_collapse(X.inverse()) == BlockMatrix([
215
+ [X.schur('D').I, -X.schur('D').I * B * D.I],
216
+ [-D.I * C * X.schur('D').I, D.I + D.I * C * X.schur('D').I * B * D.I],
217
+ ])
218
+
219
+
220
+ def test_BlockMatrix_2x2_inverse_numeric():
221
+ """Test 2x2 block matrix inversion numerically for all 4 formulas"""
222
+ M = Matrix([[1, 2], [3, 4]])
223
+ # rank deficient matrices that have full rank when two of them combined
224
+ D1 = Matrix([[1, 2], [2, 4]])
225
+ D2 = Matrix([[1, 3], [3, 9]])
226
+ D3 = Matrix([[1, 4], [4, 16]])
227
+ assert D1.rank() == D2.rank() == D3.rank() == 1
228
+ assert (D1 + D2).rank() == (D2 + D3).rank() == (D3 + D1).rank() == 2
229
+
230
+ # Only A is invertible
231
+ K = BlockMatrix([[M, D1], [D2, D3]])
232
+ assert block_collapse(K.inv()).as_explicit() == K.as_explicit().inv()
233
+ # Only B is invertible
234
+ K = BlockMatrix([[D1, M], [D2, D3]])
235
+ assert block_collapse(K.inv()).as_explicit() == K.as_explicit().inv()
236
+ # Only C is invertible
237
+ K = BlockMatrix([[D1, D2], [M, D3]])
238
+ assert block_collapse(K.inv()).as_explicit() == K.as_explicit().inv()
239
+ # Only D is invertible
240
+ K = BlockMatrix([[D1, D2], [D3, M]])
241
+ assert block_collapse(K.inv()).as_explicit() == K.as_explicit().inv()
242
+
243
+
244
+ @slow
245
+ def test_BlockMatrix_3x3_symbolic():
246
+ # Only test one of these, instead of all permutations, because it's slow
247
+ rowblocksizes = (n, m, k)
248
+ colblocksizes = (m, k, n)
249
+ K = BlockMatrix([
250
+ [MatrixSymbol('M%s%s' % (rows, cols), rows, cols) for cols in colblocksizes]
251
+ for rows in rowblocksizes
252
+ ])
253
+ collapse = block_collapse(K.I)
254
+ assert isinstance(collapse, BlockMatrix)
255
+
256
+
257
+ def test_BlockDiagMatrix():
258
+ A = MatrixSymbol('A', n, n)
259
+ B = MatrixSymbol('B', m, m)
260
+ C = MatrixSymbol('C', l, l)
261
+ M = MatrixSymbol('M', n + m + l, n + m + l)
262
+
263
+ X = BlockDiagMatrix(A, B, C)
264
+ Y = BlockDiagMatrix(A, 2*B, 3*C)
265
+
266
+ assert X.blocks[1, 1] == B
267
+ assert X.shape == (n + m + l, n + m + l)
268
+ assert all(X.blocks[i, j].is_ZeroMatrix if i != j else X.blocks[i, j] in [A, B, C]
269
+ for i in range(3) for j in range(3))
270
+ assert X.__class__(*X.args) == X
271
+ assert X.get_diag_blocks() == (A, B, C)
272
+
273
+ assert isinstance(block_collapse(X.I * X), Identity)
274
+
275
+ assert bc_matmul(X*X) == BlockDiagMatrix(A*A, B*B, C*C)
276
+ assert block_collapse(X*X) == BlockDiagMatrix(A*A, B*B, C*C)
277
+ #XXX: should be == ??
278
+ assert block_collapse(X + X).equals(BlockDiagMatrix(2*A, 2*B, 2*C))
279
+ assert block_collapse(X*Y) == BlockDiagMatrix(A*A, 2*B*B, 3*C*C)
280
+ assert block_collapse(X + Y) == BlockDiagMatrix(2*A, 3*B, 4*C)
281
+
282
+ # Ensure that BlockDiagMatrices can still interact with normal MatrixExprs
283
+ assert (X*(2*M)).is_MatMul
284
+ assert (X + (2*M)).is_MatAdd
285
+
286
+ assert (X._blockmul(M)).is_MatMul
287
+ assert (X._blockadd(M)).is_MatAdd
288
+
289
+ def test_BlockDiagMatrix_nonsquare():
290
+ A = MatrixSymbol('A', n, m)
291
+ B = MatrixSymbol('B', k, l)
292
+ X = BlockDiagMatrix(A, B)
293
+ assert X.shape == (n + k, m + l)
294
+ assert X.shape == (n + k, m + l)
295
+ assert X.rowblocksizes == [n, k]
296
+ assert X.colblocksizes == [m, l]
297
+ C = MatrixSymbol('C', n, m)
298
+ D = MatrixSymbol('D', k, l)
299
+ Y = BlockDiagMatrix(C, D)
300
+ assert block_collapse(X + Y) == BlockDiagMatrix(A + C, B + D)
301
+ assert block_collapse(X * Y.T) == BlockDiagMatrix(A * C.T, B * D.T)
302
+ raises(NonInvertibleMatrixError, lambda: BlockDiagMatrix(A, C.T).inverse())
303
+
304
+ def test_BlockDiagMatrix_determinant():
305
+ A = MatrixSymbol('A', n, n)
306
+ B = MatrixSymbol('B', m, m)
307
+ assert det(BlockDiagMatrix()) == 1
308
+ assert det(BlockDiagMatrix(A)) == det(A)
309
+ assert det(BlockDiagMatrix(A, B)) == det(A) * det(B)
310
+
311
+ # non-square blocks
312
+ C = MatrixSymbol('C', m, n)
313
+ D = MatrixSymbol('D', n, m)
314
+ assert det(BlockDiagMatrix(C, D)) == 0
315
+
316
+ def test_BlockDiagMatrix_trace():
317
+ assert trace(BlockDiagMatrix()) == 0
318
+ assert trace(BlockDiagMatrix(ZeroMatrix(n, n))) == 0
319
+ A = MatrixSymbol('A', n, n)
320
+ assert trace(BlockDiagMatrix(A)) == trace(A)
321
+ B = MatrixSymbol('B', m, m)
322
+ assert trace(BlockDiagMatrix(A, B)) == trace(A) + trace(B)
323
+
324
+ # non-square blocks
325
+ C = MatrixSymbol('C', m, n)
326
+ D = MatrixSymbol('D', n, m)
327
+ assert isinstance(trace(BlockDiagMatrix(C, D)), Trace)
328
+
329
+ def test_BlockDiagMatrix_transpose():
330
+ A = MatrixSymbol('A', n, m)
331
+ B = MatrixSymbol('B', k, l)
332
+ assert transpose(BlockDiagMatrix()) == BlockDiagMatrix()
333
+ assert transpose(BlockDiagMatrix(A)) == BlockDiagMatrix(A.T)
334
+ assert transpose(BlockDiagMatrix(A, B)) == BlockDiagMatrix(A.T, B.T)
335
+
336
+ def test_issue_2460():
337
+ bdm1 = BlockDiagMatrix(Matrix([i]), Matrix([j]))
338
+ bdm2 = BlockDiagMatrix(Matrix([k]), Matrix([l]))
339
+ assert block_collapse(bdm1 + bdm2) == BlockDiagMatrix(Matrix([i + k]), Matrix([j + l]))
340
+
341
+ def test_blockcut():
342
+ A = MatrixSymbol('A', n, m)
343
+ B = blockcut(A, (n/2, n/2), (m/2, m/2))
344
+ assert B == BlockMatrix([[A[:n/2, :m/2], A[:n/2, m/2:]],
345
+ [A[n/2:, :m/2], A[n/2:, m/2:]]])
346
+
347
+ M = ImmutableMatrix(4, 4, range(16))
348
+ B = blockcut(M, (2, 2), (2, 2))
349
+ assert M == ImmutableMatrix(B)
350
+
351
+ B = blockcut(M, (1, 3), (2, 2))
352
+ assert ImmutableMatrix(B.blocks[0, 1]) == ImmutableMatrix([[2, 3]])
353
+
354
+ def test_reblock_2x2():
355
+ B = BlockMatrix([[MatrixSymbol('A_%d%d'%(i,j), 2, 2)
356
+ for j in range(3)]
357
+ for i in range(3)])
358
+ assert B.blocks.shape == (3, 3)
359
+
360
+ BB = reblock_2x2(B)
361
+ assert BB.blocks.shape == (2, 2)
362
+
363
+ assert B.shape == BB.shape
364
+ assert B.as_explicit() == BB.as_explicit()
365
+
366
+ def test_deblock():
367
+ B = BlockMatrix([[MatrixSymbol('A_%d%d'%(i,j), n, n)
368
+ for j in range(4)]
369
+ for i in range(4)])
370
+
371
+ assert deblock(reblock_2x2(B)) == B
372
+
373
+ def test_block_collapse_type():
374
+ bm1 = BlockDiagMatrix(ImmutableMatrix([1]), ImmutableMatrix([2]))
375
+ bm2 = BlockDiagMatrix(ImmutableMatrix([3]), ImmutableMatrix([4]))
376
+
377
+ assert bm1.T.__class__ == BlockDiagMatrix
378
+ assert block_collapse(bm1 - bm2).__class__ == BlockDiagMatrix
379
+ assert block_collapse(Inverse(bm1)).__class__ == BlockDiagMatrix
380
+ assert block_collapse(Transpose(bm1)).__class__ == BlockDiagMatrix
381
+ assert bc_transpose(Transpose(bm1)).__class__ == BlockDiagMatrix
382
+ assert bc_inverse(Inverse(bm1)).__class__ == BlockDiagMatrix
383
+
384
+ def test_invalid_block_matrix():
385
+ raises(ValueError, lambda: BlockMatrix([
386
+ [Identity(2), Identity(5)],
387
+ ]))
388
+ raises(ValueError, lambda: BlockMatrix([
389
+ [Identity(n), Identity(m)],
390
+ ]))
391
+ raises(ValueError, lambda: BlockMatrix([
392
+ [ZeroMatrix(n, n), ZeroMatrix(n, n)],
393
+ [ZeroMatrix(n, n - 1), ZeroMatrix(n, n + 1)],
394
+ ]))
395
+ raises(ValueError, lambda: BlockMatrix([
396
+ [ZeroMatrix(n - 1, n), ZeroMatrix(n, n)],
397
+ [ZeroMatrix(n + 1, n), ZeroMatrix(n, n)],
398
+ ]))
399
+
400
+ def test_block_lu_decomposition():
401
+ A = MatrixSymbol('A', n, n)
402
+ B = MatrixSymbol('B', n, m)
403
+ C = MatrixSymbol('C', m, n)
404
+ D = MatrixSymbol('D', m, m)
405
+ X = BlockMatrix([[A, B], [C, D]])
406
+
407
+ #LDU decomposition
408
+ L, D, U = X.LDUdecomposition()
409
+ assert block_collapse(L*D*U) == X
410
+
411
+ #UDL decomposition
412
+ U, D, L = X.UDLdecomposition()
413
+ assert block_collapse(U*D*L) == X
414
+
415
+ #LU decomposition
416
+ L, U = X.LUdecomposition()
417
+ assert block_collapse(L*U) == X
418
+
419
+ def test_issue_21866():
420
+ n = 10
421
+ I = Identity(n)
422
+ O = ZeroMatrix(n, n)
423
+ A = BlockMatrix([[ I, O, O, O ],
424
+ [ O, I, O, O ],
425
+ [ O, O, I, O ],
426
+ [ I, O, O, I ]])
427
+ Ainv = block_collapse(A.inv())
428
+ AinvT = BlockMatrix([[ I, O, O, O ],
429
+ [ O, I, O, O ],
430
+ [ O, O, I, O ],
431
+ [ -I, O, O, I ]])
432
+ assert Ainv == AinvT
433
+
434
+
435
+ def test_adjoint_and_special_matrices():
436
+ A = Identity(3)
437
+ B = OneMatrix(3, 2)
438
+ C = ZeroMatrix(2, 3)
439
+ D = Identity(2)
440
+ X = BlockMatrix([[A, B], [C, D]])
441
+ X2 = BlockMatrix([[A, S.ImaginaryUnit*B], [C, D]])
442
+ assert X.adjoint() == BlockMatrix([[A, ZeroMatrix(3, 2)], [OneMatrix(2, 3), D]])
443
+ assert re(X) == X
444
+ assert X2.adjoint() == BlockMatrix([[A, ZeroMatrix(3, 2)], [-S.ImaginaryUnit*OneMatrix(2, 3), D]])
445
+ assert im(X2) == BlockMatrix([[ZeroMatrix(3, 3), OneMatrix(3, 2)], [ZeroMatrix(2, 3), ZeroMatrix(2, 2)]])
venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/test_derivatives.py ADDED
@@ -0,0 +1,477 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Some examples have been taken from:
3
+
4
+ http://www.math.uwaterloo.ca/~hwolkowi//matrixcookbook.pdf
5
+ """
6
+ from sympy import KroneckerProduct
7
+ from sympy.combinatorics import Permutation
8
+ from sympy.concrete.summations import Sum
9
+ from sympy.core.numbers import Rational
10
+ from sympy.core.singleton import S
11
+ from sympy.core.symbol import symbols
12
+ from sympy.functions.elementary.exponential import (exp, log)
13
+ from sympy.functions.elementary.miscellaneous import sqrt
14
+ from sympy.functions.elementary.trigonometric import (cos, sin, tan)
15
+ from sympy.functions.special.tensor_functions import KroneckerDelta
16
+ from sympy.matrices.expressions.determinant import Determinant
17
+ from sympy.matrices.expressions.diagonal import DiagMatrix
18
+ from sympy.matrices.expressions.hadamard import (HadamardPower, HadamardProduct, hadamard_product)
19
+ from sympy.matrices.expressions.inverse import Inverse
20
+ from sympy.matrices.expressions.matexpr import MatrixSymbol
21
+ from sympy.matrices.expressions.special import OneMatrix
22
+ from sympy.matrices.expressions.trace import Trace
23
+ from sympy.matrices.expressions.matadd import MatAdd
24
+ from sympy.matrices.expressions.matmul import MatMul
25
+ from sympy.matrices.expressions.special import (Identity, ZeroMatrix)
26
+ from sympy.tensor.array.array_derivatives import ArrayDerivative
27
+ from sympy.matrices.expressions import hadamard_power
28
+ from sympy.tensor.array.expressions.array_expressions import ArrayAdd, ArrayTensorProduct, PermuteDims
29
+
30
+ i, j, k = symbols("i j k")
31
+ m, n = symbols("m n")
32
+
33
+ X = MatrixSymbol("X", k, k)
34
+ x = MatrixSymbol("x", k, 1)
35
+ y = MatrixSymbol("y", k, 1)
36
+
37
+ A = MatrixSymbol("A", k, k)
38
+ B = MatrixSymbol("B", k, k)
39
+ C = MatrixSymbol("C", k, k)
40
+ D = MatrixSymbol("D", k, k)
41
+
42
+ a = MatrixSymbol("a", k, 1)
43
+ b = MatrixSymbol("b", k, 1)
44
+ c = MatrixSymbol("c", k, 1)
45
+ d = MatrixSymbol("d", k, 1)
46
+
47
+
48
+ KDelta = lambda i, j: KroneckerDelta(i, j, (0, k-1))
49
+
50
+
51
+ def _check_derivative_with_explicit_matrix(expr, x, diffexpr, dim=2):
52
+ # TODO: this is commented because it slows down the tests.
53
+ return
54
+
55
+ expr = expr.xreplace({k: dim})
56
+ x = x.xreplace({k: dim})
57
+ diffexpr = diffexpr.xreplace({k: dim})
58
+
59
+ expr = expr.as_explicit()
60
+ x = x.as_explicit()
61
+ diffexpr = diffexpr.as_explicit()
62
+
63
+ assert expr.diff(x).reshape(*diffexpr.shape).tomatrix() == diffexpr
64
+
65
+
66
+ def test_matrix_derivative_by_scalar():
67
+ assert A.diff(i) == ZeroMatrix(k, k)
68
+ assert (A*(X + B)*c).diff(i) == ZeroMatrix(k, 1)
69
+ assert x.diff(i) == ZeroMatrix(k, 1)
70
+ assert (x.T*y).diff(i) == ZeroMatrix(1, 1)
71
+ assert (x*x.T).diff(i) == ZeroMatrix(k, k)
72
+ assert (x + y).diff(i) == ZeroMatrix(k, 1)
73
+ assert hadamard_power(x, 2).diff(i) == ZeroMatrix(k, 1)
74
+ assert hadamard_power(x, i).diff(i).dummy_eq(
75
+ HadamardProduct(x.applyfunc(log), HadamardPower(x, i)))
76
+ assert hadamard_product(x, y).diff(i) == ZeroMatrix(k, 1)
77
+ assert hadamard_product(i*OneMatrix(k, 1), x, y).diff(i) == hadamard_product(x, y)
78
+ assert (i*x).diff(i) == x
79
+ assert (sin(i)*A*B*x).diff(i) == cos(i)*A*B*x
80
+ assert x.applyfunc(sin).diff(i) == ZeroMatrix(k, 1)
81
+ assert Trace(i**2*X).diff(i) == 2*i*Trace(X)
82
+
83
+ mu = symbols("mu")
84
+ expr = (2*mu*x)
85
+ assert expr.diff(x) == 2*mu*Identity(k)
86
+
87
+
88
+ def test_one_matrix():
89
+ assert MatMul(x.T, OneMatrix(k, 1)).diff(x) == OneMatrix(k, 1)
90
+
91
+
92
+ def test_matrix_derivative_non_matrix_result():
93
+ # This is a 4-dimensional array:
94
+ I = Identity(k)
95
+ AdA = PermuteDims(ArrayTensorProduct(I, I), Permutation(3)(1, 2))
96
+ assert A.diff(A) == AdA
97
+ assert A.T.diff(A) == PermuteDims(ArrayTensorProduct(I, I), Permutation(3)(1, 2, 3))
98
+ assert (2*A).diff(A) == PermuteDims(ArrayTensorProduct(2*I, I), Permutation(3)(1, 2))
99
+ assert MatAdd(A, A).diff(A) == ArrayAdd(AdA, AdA)
100
+ assert (A + B).diff(A) == AdA
101
+
102
+
103
+ def test_matrix_derivative_trivial_cases():
104
+ # Cookbook example 33:
105
+ # TODO: find a way to represent a four-dimensional zero-array:
106
+ assert X.diff(A) == ArrayDerivative(X, A)
107
+
108
+
109
+ def test_matrix_derivative_with_inverse():
110
+
111
+ # Cookbook example 61:
112
+ expr = a.T*Inverse(X)*b
113
+ assert expr.diff(X) == -Inverse(X).T*a*b.T*Inverse(X).T
114
+
115
+ # Cookbook example 62:
116
+ expr = Determinant(Inverse(X))
117
+ # Not implemented yet:
118
+ # assert expr.diff(X) == -Determinant(X.inv())*(X.inv()).T
119
+
120
+ # Cookbook example 63:
121
+ expr = Trace(A*Inverse(X)*B)
122
+ assert expr.diff(X) == -(X**(-1)*B*A*X**(-1)).T
123
+
124
+ # Cookbook example 64:
125
+ expr = Trace(Inverse(X + A))
126
+ assert expr.diff(X) == -(Inverse(X + A)).T**2
127
+
128
+
129
+ def test_matrix_derivative_vectors_and_scalars():
130
+
131
+ assert x.diff(x) == Identity(k)
132
+ assert x[i, 0].diff(x[m, 0]).doit() == KDelta(m, i)
133
+
134
+ assert x.T.diff(x) == Identity(k)
135
+
136
+ # Cookbook example 69:
137
+ expr = x.T*a
138
+ assert expr.diff(x) == a
139
+ assert expr[0, 0].diff(x[m, 0]).doit() == a[m, 0]
140
+ expr = a.T*x
141
+ assert expr.diff(x) == a
142
+
143
+ # Cookbook example 70:
144
+ expr = a.T*X*b
145
+ assert expr.diff(X) == a*b.T
146
+
147
+ # Cookbook example 71:
148
+ expr = a.T*X.T*b
149
+ assert expr.diff(X) == b*a.T
150
+
151
+ # Cookbook example 72:
152
+ expr = a.T*X*a
153
+ assert expr.diff(X) == a*a.T
154
+ expr = a.T*X.T*a
155
+ assert expr.diff(X) == a*a.T
156
+
157
+ # Cookbook example 77:
158
+ expr = b.T*X.T*X*c
159
+ assert expr.diff(X) == X*b*c.T + X*c*b.T
160
+
161
+ # Cookbook example 78:
162
+ expr = (B*x + b).T*C*(D*x + d)
163
+ assert expr.diff(x) == B.T*C*(D*x + d) + D.T*C.T*(B*x + b)
164
+
165
+ # Cookbook example 81:
166
+ expr = x.T*B*x
167
+ assert expr.diff(x) == B*x + B.T*x
168
+
169
+ # Cookbook example 82:
170
+ expr = b.T*X.T*D*X*c
171
+ assert expr.diff(X) == D.T*X*b*c.T + D*X*c*b.T
172
+
173
+ # Cookbook example 83:
174
+ expr = (X*b + c).T*D*(X*b + c)
175
+ assert expr.diff(X) == D*(X*b + c)*b.T + D.T*(X*b + c)*b.T
176
+ assert str(expr[0, 0].diff(X[m, n]).doit()) == \
177
+ 'b[n, 0]*Sum((c[_i_1, 0] + Sum(X[_i_1, _i_3]*b[_i_3, 0], (_i_3, 0, k - 1)))*D[_i_1, m], (_i_1, 0, k - 1)) + Sum((c[_i_2, 0] + Sum(X[_i_2, _i_4]*b[_i_4, 0], (_i_4, 0, k - 1)))*D[m, _i_2]*b[n, 0], (_i_2, 0, k - 1))'
178
+
179
+ # See https://github.com/sympy/sympy/issues/16504#issuecomment-1018339957
180
+ expr = x*x.T*x
181
+ I = Identity(k)
182
+ assert expr.diff(x) == KroneckerProduct(I, x.T*x) + 2*x*x.T
183
+
184
+
185
+ def test_matrix_derivatives_of_traces():
186
+
187
+ expr = Trace(A)*A
188
+ I = Identity(k)
189
+ assert expr.diff(A) == ArrayAdd(ArrayTensorProduct(I, A), PermuteDims(ArrayTensorProduct(Trace(A)*I, I), Permutation(3)(1, 2)))
190
+ assert expr[i, j].diff(A[m, n]).doit() == (
191
+ KDelta(i, m)*KDelta(j, n)*Trace(A) +
192
+ KDelta(m, n)*A[i, j]
193
+ )
194
+
195
+ ## First order:
196
+
197
+ # Cookbook example 99:
198
+ expr = Trace(X)
199
+ assert expr.diff(X) == Identity(k)
200
+ assert expr.rewrite(Sum).diff(X[m, n]).doit() == KDelta(m, n)
201
+
202
+ # Cookbook example 100:
203
+ expr = Trace(X*A)
204
+ assert expr.diff(X) == A.T
205
+ assert expr.rewrite(Sum).diff(X[m, n]).doit() == A[n, m]
206
+
207
+ # Cookbook example 101:
208
+ expr = Trace(A*X*B)
209
+ assert expr.diff(X) == A.T*B.T
210
+ assert expr.rewrite(Sum).diff(X[m, n]).doit().dummy_eq((A.T*B.T)[m, n])
211
+
212
+ # Cookbook example 102:
213
+ expr = Trace(A*X.T*B)
214
+ assert expr.diff(X) == B*A
215
+
216
+ # Cookbook example 103:
217
+ expr = Trace(X.T*A)
218
+ assert expr.diff(X) == A
219
+
220
+ # Cookbook example 104:
221
+ expr = Trace(A*X.T)
222
+ assert expr.diff(X) == A
223
+
224
+ # Cookbook example 105:
225
+ # TODO: TensorProduct is not supported
226
+ #expr = Trace(TensorProduct(A, X))
227
+ #assert expr.diff(X) == Trace(A)*Identity(k)
228
+
229
+ ## Second order:
230
+
231
+ # Cookbook example 106:
232
+ expr = Trace(X**2)
233
+ assert expr.diff(X) == 2*X.T
234
+
235
+ # Cookbook example 107:
236
+ expr = Trace(X**2*B)
237
+ assert expr.diff(X) == (X*B + B*X).T
238
+ expr = Trace(MatMul(X, X, B))
239
+ assert expr.diff(X) == (X*B + B*X).T
240
+
241
+ # Cookbook example 108:
242
+ expr = Trace(X.T*B*X)
243
+ assert expr.diff(X) == B*X + B.T*X
244
+
245
+ # Cookbook example 109:
246
+ expr = Trace(B*X*X.T)
247
+ assert expr.diff(X) == B*X + B.T*X
248
+
249
+ # Cookbook example 110:
250
+ expr = Trace(X*X.T*B)
251
+ assert expr.diff(X) == B*X + B.T*X
252
+
253
+ # Cookbook example 111:
254
+ expr = Trace(X*B*X.T)
255
+ assert expr.diff(X) == X*B.T + X*B
256
+
257
+ # Cookbook example 112:
258
+ expr = Trace(B*X.T*X)
259
+ assert expr.diff(X) == X*B.T + X*B
260
+
261
+ # Cookbook example 113:
262
+ expr = Trace(X.T*X*B)
263
+ assert expr.diff(X) == X*B.T + X*B
264
+
265
+ # Cookbook example 114:
266
+ expr = Trace(A*X*B*X)
267
+ assert expr.diff(X) == A.T*X.T*B.T + B.T*X.T*A.T
268
+
269
+ # Cookbook example 115:
270
+ expr = Trace(X.T*X)
271
+ assert expr.diff(X) == 2*X
272
+ expr = Trace(X*X.T)
273
+ assert expr.diff(X) == 2*X
274
+
275
+ # Cookbook example 116:
276
+ expr = Trace(B.T*X.T*C*X*B)
277
+ assert expr.diff(X) == C.T*X*B*B.T + C*X*B*B.T
278
+
279
+ # Cookbook example 117:
280
+ expr = Trace(X.T*B*X*C)
281
+ assert expr.diff(X) == B*X*C + B.T*X*C.T
282
+
283
+ # Cookbook example 118:
284
+ expr = Trace(A*X*B*X.T*C)
285
+ assert expr.diff(X) == A.T*C.T*X*B.T + C*A*X*B
286
+
287
+ # Cookbook example 119:
288
+ expr = Trace((A*X*B + C)*(A*X*B + C).T)
289
+ assert expr.diff(X) == 2*A.T*(A*X*B + C)*B.T
290
+
291
+ # Cookbook example 120:
292
+ # TODO: no support for TensorProduct.
293
+ # expr = Trace(TensorProduct(X, X))
294
+ # expr = Trace(X)*Trace(X)
295
+ # expr.diff(X) == 2*Trace(X)*Identity(k)
296
+
297
+ # Higher Order
298
+
299
+ # Cookbook example 121:
300
+ expr = Trace(X**k)
301
+ #assert expr.diff(X) == k*(X**(k-1)).T
302
+
303
+ # Cookbook example 122:
304
+ expr = Trace(A*X**k)
305
+ #assert expr.diff(X) == # Needs indices
306
+
307
+ # Cookbook example 123:
308
+ expr = Trace(B.T*X.T*C*X*X.T*C*X*B)
309
+ assert expr.diff(X) == C*X*X.T*C*X*B*B.T + C.T*X*B*B.T*X.T*C.T*X + C*X*B*B.T*X.T*C*X + C.T*X*X.T*C.T*X*B*B.T
310
+
311
+ # Other
312
+
313
+ # Cookbook example 124:
314
+ expr = Trace(A*X**(-1)*B)
315
+ assert expr.diff(X) == -Inverse(X).T*A.T*B.T*Inverse(X).T
316
+
317
+ # Cookbook example 125:
318
+ expr = Trace(Inverse(X.T*C*X)*A)
319
+ # Warning: result in the cookbook is equivalent if B and C are symmetric:
320
+ assert expr.diff(X) == - X.inv().T*A.T*X.inv()*C.inv().T*X.inv().T - X.inv().T*A*X.inv()*C.inv()*X.inv().T
321
+
322
+ # Cookbook example 126:
323
+ expr = Trace((X.T*C*X).inv()*(X.T*B*X))
324
+ assert expr.diff(X) == -2*C*X*(X.T*C*X).inv()*X.T*B*X*(X.T*C*X).inv() + 2*B*X*(X.T*C*X).inv()
325
+
326
+ # Cookbook example 127:
327
+ expr = Trace((A + X.T*C*X).inv()*(X.T*B*X))
328
+ # Warning: result in the cookbook is equivalent if B and C are symmetric:
329
+ assert expr.diff(X) == B*X*Inverse(A + X.T*C*X) - C*X*Inverse(A + X.T*C*X)*X.T*B*X*Inverse(A + X.T*C*X) - C.T*X*Inverse(A.T + (C*X).T*X)*X.T*B.T*X*Inverse(A.T + (C*X).T*X) + B.T*X*Inverse(A.T + (C*X).T*X)
330
+
331
+
332
+ def test_derivatives_of_complicated_matrix_expr():
333
+ expr = a.T*(A*X*(X.T*B + X*A) + B.T*X.T*(a*b.T*(X*D*X.T + X*(X.T*B + A*X)*D*B - X.T*C.T*A)*B + B*(X*D.T + B*A*X*A.T - 3*X*D))*B + 42*X*B*X.T*A.T*(X + X.T))*b
334
+ result = (B*(B*A*X*A.T - 3*X*D + X*D.T) + a*b.T*(X*(A*X + X.T*B)*D*B + X*D*X.T - X.T*C.T*A)*B)*B*b*a.T*B.T + B**2*b*a.T*B.T*X.T*a*b.T*X*D + 42*A*X*B.T*X.T*a*b.T + B*D*B**3*b*a.T*B.T*X.T*a*b.T*X + B*b*a.T*A*X + a*b.T*(42*X + 42*X.T)*A*X*B.T + b*a.T*X*B*a*b.T*B.T**2*X*D.T + b*a.T*X*B*a*b.T*B.T**3*D.T*(B.T*X + X.T*A.T) + 42*b*a.T*X*B*X.T*A.T + A.T*(42*X + 42*X.T)*b*a.T*X*B + A.T*B.T**2*X*B*a*b.T*B.T*A + A.T*a*b.T*(A.T*X.T + B.T*X) + A.T*X.T*b*a.T*X*B*a*b.T*B.T**3*D.T + B.T*X*B*a*b.T*B.T*D - 3*B.T*X*B*a*b.T*B.T*D.T - C.T*A*B**2*b*a.T*B.T*X.T*a*b.T + X.T*A.T*a*b.T*A.T
335
+ assert expr.diff(X) == result
336
+
337
+
338
+ def test_mixed_deriv_mixed_expressions():
339
+
340
+ expr = 3*Trace(A)
341
+ assert expr.diff(A) == 3*Identity(k)
342
+
343
+ expr = k
344
+ deriv = expr.diff(A)
345
+ assert isinstance(deriv, ZeroMatrix)
346
+ assert deriv == ZeroMatrix(k, k)
347
+
348
+ expr = Trace(A)**2
349
+ assert expr.diff(A) == (2*Trace(A))*Identity(k)
350
+
351
+ expr = Trace(A)*A
352
+ I = Identity(k)
353
+ assert expr.diff(A) == ArrayAdd(ArrayTensorProduct(I, A), PermuteDims(ArrayTensorProduct(Trace(A)*I, I), Permutation(3)(1, 2)))
354
+
355
+ expr = Trace(Trace(A)*A)
356
+ assert expr.diff(A) == (2*Trace(A))*Identity(k)
357
+
358
+ expr = Trace(Trace(Trace(A)*A)*A)
359
+ assert expr.diff(A) == (3*Trace(A)**2)*Identity(k)
360
+
361
+
362
+ def test_derivatives_matrix_norms():
363
+
364
+ expr = x.T*y
365
+ assert expr.diff(x) == y
366
+ assert expr[0, 0].diff(x[m, 0]).doit() == y[m, 0]
367
+
368
+ expr = (x.T*y)**S.Half
369
+ assert expr.diff(x) == y/(2*sqrt(x.T*y))
370
+
371
+ expr = (x.T*x)**S.Half
372
+ assert expr.diff(x) == x*(x.T*x)**Rational(-1, 2)
373
+
374
+ expr = (c.T*a*x.T*b)**S.Half
375
+ assert expr.diff(x) == b*a.T*c/sqrt(c.T*a*x.T*b)/2
376
+
377
+ expr = (c.T*a*x.T*b)**Rational(1, 3)
378
+ assert expr.diff(x) == b*a.T*c*(c.T*a*x.T*b)**Rational(-2, 3)/3
379
+
380
+ expr = (a.T*X*b)**S.Half
381
+ assert expr.diff(X) == a/(2*sqrt(a.T*X*b))*b.T
382
+
383
+ expr = d.T*x*(a.T*X*b)**S.Half*y.T*c
384
+ assert expr.diff(X) == a/(2*sqrt(a.T*X*b))*x.T*d*y.T*c*b.T
385
+
386
+
387
+ def test_derivatives_elementwise_applyfunc():
388
+
389
+ expr = x.applyfunc(tan)
390
+ assert expr.diff(x).dummy_eq(
391
+ DiagMatrix(x.applyfunc(lambda x: tan(x)**2 + 1)))
392
+ assert expr[i, 0].diff(x[m, 0]).doit() == (tan(x[i, 0])**2 + 1)*KDelta(i, m)
393
+ _check_derivative_with_explicit_matrix(expr, x, expr.diff(x))
394
+
395
+ expr = (i**2*x).applyfunc(sin)
396
+ assert expr.diff(i).dummy_eq(
397
+ HadamardProduct((2*i)*x, (i**2*x).applyfunc(cos)))
398
+ assert expr[i, 0].diff(i).doit() == 2*i*x[i, 0]*cos(i**2*x[i, 0])
399
+ _check_derivative_with_explicit_matrix(expr, i, expr.diff(i))
400
+
401
+ expr = (log(i)*A*B).applyfunc(sin)
402
+ assert expr.diff(i).dummy_eq(
403
+ HadamardProduct(A*B/i, (log(i)*A*B).applyfunc(cos)))
404
+ _check_derivative_with_explicit_matrix(expr, i, expr.diff(i))
405
+
406
+ expr = A*x.applyfunc(exp)
407
+ # TODO: restore this result (currently returning the transpose):
408
+ # assert expr.diff(x).dummy_eq(DiagMatrix(x.applyfunc(exp))*A.T)
409
+ _check_derivative_with_explicit_matrix(expr, x, expr.diff(x))
410
+
411
+ expr = x.T*A*x + k*y.applyfunc(sin).T*x
412
+ assert expr.diff(x).dummy_eq(A.T*x + A*x + k*y.applyfunc(sin))
413
+ _check_derivative_with_explicit_matrix(expr, x, expr.diff(x))
414
+
415
+ expr = x.applyfunc(sin).T*y
416
+ # TODO: restore (currently returning the transpose):
417
+ # assert expr.diff(x).dummy_eq(DiagMatrix(x.applyfunc(cos))*y)
418
+ _check_derivative_with_explicit_matrix(expr, x, expr.diff(x))
419
+
420
+ expr = (a.T * X * b).applyfunc(sin)
421
+ assert expr.diff(X).dummy_eq(a*(a.T*X*b).applyfunc(cos)*b.T)
422
+ _check_derivative_with_explicit_matrix(expr, X, expr.diff(X))
423
+
424
+ expr = a.T * X.applyfunc(sin) * b
425
+ assert expr.diff(X).dummy_eq(
426
+ DiagMatrix(a)*X.applyfunc(cos)*DiagMatrix(b))
427
+ _check_derivative_with_explicit_matrix(expr, X, expr.diff(X))
428
+
429
+ expr = a.T * (A*X*B).applyfunc(sin) * b
430
+ assert expr.diff(X).dummy_eq(
431
+ A.T*DiagMatrix(a)*(A*X*B).applyfunc(cos)*DiagMatrix(b)*B.T)
432
+ _check_derivative_with_explicit_matrix(expr, X, expr.diff(X))
433
+
434
+ expr = a.T * (A*X*b).applyfunc(sin) * b.T
435
+ # TODO: not implemented
436
+ #assert expr.diff(X) == ...
437
+ #_check_derivative_with_explicit_matrix(expr, X, expr.diff(X))
438
+
439
+ expr = a.T*A*X.applyfunc(sin)*B*b
440
+ assert expr.diff(X).dummy_eq(
441
+ HadamardProduct(A.T * a * b.T * B.T, X.applyfunc(cos)))
442
+
443
+ expr = a.T * (A*X.applyfunc(sin)*B).applyfunc(log) * b
444
+ # TODO: wrong
445
+ # assert expr.diff(X) == A.T*DiagMatrix(a)*(A*X.applyfunc(sin)*B).applyfunc(Lambda(k, 1/k))*DiagMatrix(b)*B.T
446
+
447
+ expr = a.T * (X.applyfunc(sin)).applyfunc(log) * b
448
+ # TODO: wrong
449
+ # assert expr.diff(X) == DiagMatrix(a)*X.applyfunc(sin).applyfunc(Lambda(k, 1/k))*DiagMatrix(b)
450
+
451
+
452
+ def test_derivatives_of_hadamard_expressions():
453
+
454
+ # Hadamard Product
455
+
456
+ expr = hadamard_product(a, x, b)
457
+ assert expr.diff(x) == DiagMatrix(hadamard_product(b, a))
458
+
459
+ expr = a.T*hadamard_product(A, X, B)*b
460
+ assert expr.diff(X) == HadamardProduct(a*b.T, A, B)
461
+
462
+ # Hadamard Power
463
+
464
+ expr = hadamard_power(x, 2)
465
+ assert expr.diff(x).doit() == 2*DiagMatrix(x)
466
+
467
+ expr = hadamard_power(x.T, 2)
468
+ assert expr.diff(x).doit() == 2*DiagMatrix(x)
469
+
470
+ expr = hadamard_power(x, S.Half)
471
+ assert expr.diff(x) == S.Half*DiagMatrix(hadamard_power(x, Rational(-1, 2)))
472
+
473
+ expr = hadamard_power(a.T*X*b, 2)
474
+ assert expr.diff(X) == 2*a*a.T*X*b*b.T
475
+
476
+ expr = hadamard_power(a.T*X*b, S.Half)
477
+ assert expr.diff(X) == a/(2*sqrt(a.T*X*b))*b.T
venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/test_determinant.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.core import S, symbols
2
+ from sympy.matrices import eye, ones, Matrix, ShapeError
3
+ from sympy.matrices.expressions import (
4
+ Identity, MatrixExpr, MatrixSymbol, Determinant,
5
+ det, per, ZeroMatrix, Transpose,
6
+ Permanent
7
+ )
8
+ from sympy.matrices.expressions.special import OneMatrix
9
+ from sympy.testing.pytest import raises
10
+ from sympy.assumptions.ask import Q
11
+ from sympy.assumptions.refine import refine
12
+
13
+ n = symbols('n', integer=True)
14
+ A = MatrixSymbol('A', n, n)
15
+ B = MatrixSymbol('B', n, n)
16
+ C = MatrixSymbol('C', 3, 4)
17
+
18
+
19
+ def test_det():
20
+ assert isinstance(Determinant(A), Determinant)
21
+ assert not isinstance(Determinant(A), MatrixExpr)
22
+ raises(ShapeError, lambda: Determinant(C))
23
+ assert det(eye(3)) == 1
24
+ assert det(Matrix(3, 3, [1, 3, 2, 4, 1, 3, 2, 5, 2])) == 17
25
+ _ = A / det(A) # Make sure this is possible
26
+
27
+ raises(TypeError, lambda: Determinant(S.One))
28
+
29
+ assert Determinant(A).arg is A
30
+
31
+ def test_eval_determinant():
32
+ assert det(Identity(n)) == 1
33
+ assert det(ZeroMatrix(n, n)) == 0
34
+ assert det(OneMatrix(n, n)) == Determinant(OneMatrix(n, n))
35
+ assert det(OneMatrix(1, 1)) == 1
36
+ assert det(OneMatrix(2, 2)) == 0
37
+ assert det(Transpose(A)) == det(A)
38
+
39
+
40
+ def test_refine():
41
+ assert refine(det(A), Q.orthogonal(A)) == 1
42
+ assert refine(det(A), Q.singular(A)) == 0
43
+ assert refine(det(A), Q.unit_triangular(A)) == 1
44
+ assert refine(det(A), Q.normal(A)) == det(A)
45
+
46
+
47
+ def test_commutative():
48
+ det_a = Determinant(A)
49
+ det_b = Determinant(B)
50
+ assert det_a.is_commutative
51
+ assert det_b.is_commutative
52
+ assert det_a * det_b == det_b * det_a
53
+
54
+ def test_permanent():
55
+ assert isinstance(Permanent(A), Permanent)
56
+ assert not isinstance(Permanent(A), MatrixExpr)
57
+ assert isinstance(Permanent(C), Permanent)
58
+ assert Permanent(ones(3, 3)).doit() == 6
59
+ _ = C / per(C)
60
+ assert per(Matrix(3, 3, [1, 3, 2, 4, 1, 3, 2, 5, 2])) == 103
61
+ raises(TypeError, lambda: Permanent(S.One))
62
+ assert Permanent(A).arg is A
venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/test_diagonal.py ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.matrices.expressions import MatrixSymbol
2
+ from sympy.matrices.expressions.diagonal import DiagonalMatrix, DiagonalOf, DiagMatrix, diagonalize_vector
3
+ from sympy.assumptions.ask import (Q, ask)
4
+ from sympy.core.symbol import Symbol
5
+ from sympy.functions.special.tensor_functions import KroneckerDelta
6
+ from sympy.matrices.dense import Matrix
7
+ from sympy.matrices.expressions.matmul import MatMul
8
+ from sympy.matrices.expressions.special import Identity
9
+ from sympy.testing.pytest import raises
10
+
11
+
12
+ n = Symbol('n')
13
+ m = Symbol('m')
14
+
15
+
16
+ def test_DiagonalMatrix():
17
+ x = MatrixSymbol('x', n, m)
18
+ D = DiagonalMatrix(x)
19
+ assert D.diagonal_length is None
20
+ assert D.shape == (n, m)
21
+
22
+ x = MatrixSymbol('x', n, n)
23
+ D = DiagonalMatrix(x)
24
+ assert D.diagonal_length == n
25
+ assert D.shape == (n, n)
26
+ assert D[1, 2] == 0
27
+ assert D[1, 1] == x[1, 1]
28
+ i = Symbol('i')
29
+ j = Symbol('j')
30
+ x = MatrixSymbol('x', 3, 3)
31
+ ij = DiagonalMatrix(x)[i, j]
32
+ assert ij != 0
33
+ assert ij.subs({i:0, j:0}) == x[0, 0]
34
+ assert ij.subs({i:0, j:1}) == 0
35
+ assert ij.subs({i:1, j:1}) == x[1, 1]
36
+ assert ask(Q.diagonal(D)) # affirm that D is diagonal
37
+
38
+ x = MatrixSymbol('x', n, 3)
39
+ D = DiagonalMatrix(x)
40
+ assert D.diagonal_length == 3
41
+ assert D.shape == (n, 3)
42
+ assert D[2, m] == KroneckerDelta(2, m)*x[2, m]
43
+ assert D[3, m] == 0
44
+ raises(IndexError, lambda: D[m, 3])
45
+
46
+ x = MatrixSymbol('x', 3, n)
47
+ D = DiagonalMatrix(x)
48
+ assert D.diagonal_length == 3
49
+ assert D.shape == (3, n)
50
+ assert D[m, 2] == KroneckerDelta(m, 2)*x[m, 2]
51
+ assert D[m, 3] == 0
52
+ raises(IndexError, lambda: D[3, m])
53
+
54
+ x = MatrixSymbol('x', n, m)
55
+ D = DiagonalMatrix(x)
56
+ assert D.diagonal_length is None
57
+ assert D.shape == (n, m)
58
+ assert D[m, 4] != 0
59
+
60
+ x = MatrixSymbol('x', 3, 4)
61
+ assert [DiagonalMatrix(x)[i] for i in range(12)] == [
62
+ x[0, 0], 0, 0, 0, 0, x[1, 1], 0, 0, 0, 0, x[2, 2], 0]
63
+
64
+ # shape is retained, issue 12427
65
+ assert (
66
+ DiagonalMatrix(MatrixSymbol('x', 3, 4))*
67
+ DiagonalMatrix(MatrixSymbol('x', 4, 2))).shape == (3, 2)
68
+
69
+
70
+ def test_DiagonalOf():
71
+ x = MatrixSymbol('x', n, n)
72
+ d = DiagonalOf(x)
73
+ assert d.shape == (n, 1)
74
+ assert d.diagonal_length == n
75
+ assert d[2, 0] == d[2] == x[2, 2]
76
+
77
+ x = MatrixSymbol('x', n, m)
78
+ d = DiagonalOf(x)
79
+ assert d.shape == (None, 1)
80
+ assert d.diagonal_length is None
81
+ assert d[2, 0] == d[2] == x[2, 2]
82
+
83
+ d = DiagonalOf(MatrixSymbol('x', 4, 3))
84
+ assert d.shape == (3, 1)
85
+ d = DiagonalOf(MatrixSymbol('x', n, 3))
86
+ assert d.shape == (3, 1)
87
+ d = DiagonalOf(MatrixSymbol('x', 3, n))
88
+ assert d.shape == (3, 1)
89
+ x = MatrixSymbol('x', n, m)
90
+ assert [DiagonalOf(x)[i] for i in range(4)] ==[
91
+ x[0, 0], x[1, 1], x[2, 2], x[3, 3]]
92
+
93
+
94
+ def test_DiagMatrix():
95
+ x = MatrixSymbol('x', n, 1)
96
+ d = DiagMatrix(x)
97
+ assert d.shape == (n, n)
98
+ assert d[0, 1] == 0
99
+ assert d[0, 0] == x[0, 0]
100
+
101
+ a = MatrixSymbol('a', 1, 1)
102
+ d = diagonalize_vector(a)
103
+ assert isinstance(d, MatrixSymbol)
104
+ assert a == d
105
+ assert diagonalize_vector(Identity(3)) == Identity(3)
106
+ assert DiagMatrix(Identity(3)).doit() == Identity(3)
107
+ assert isinstance(DiagMatrix(Identity(3)), DiagMatrix)
108
+
109
+ # A diagonal matrix is equal to its transpose:
110
+ assert DiagMatrix(x).T == DiagMatrix(x)
111
+ assert diagonalize_vector(x.T) == DiagMatrix(x)
112
+
113
+ dx = DiagMatrix(x)
114
+ assert dx[0, 0] == x[0, 0]
115
+ assert dx[1, 1] == x[1, 0]
116
+ assert dx[0, 1] == 0
117
+ assert dx[0, m] == x[0, 0]*KroneckerDelta(0, m)
118
+
119
+ z = MatrixSymbol('z', 1, n)
120
+ dz = DiagMatrix(z)
121
+ assert dz[0, 0] == z[0, 0]
122
+ assert dz[1, 1] == z[0, 1]
123
+ assert dz[0, 1] == 0
124
+ assert dz[0, m] == z[0, m]*KroneckerDelta(0, m)
125
+
126
+ v = MatrixSymbol('v', 3, 1)
127
+ dv = DiagMatrix(v)
128
+ assert dv.as_explicit() == Matrix([
129
+ [v[0, 0], 0, 0],
130
+ [0, v[1, 0], 0],
131
+ [0, 0, v[2, 0]],
132
+ ])
133
+
134
+ v = MatrixSymbol('v', 1, 3)
135
+ dv = DiagMatrix(v)
136
+ assert dv.as_explicit() == Matrix([
137
+ [v[0, 0], 0, 0],
138
+ [0, v[0, 1], 0],
139
+ [0, 0, v[0, 2]],
140
+ ])
141
+
142
+ dv = DiagMatrix(3*v)
143
+ assert dv.args == (3*v,)
144
+ assert dv.doit() == 3*DiagMatrix(v)
145
+ assert isinstance(dv.doit(), MatMul)
146
+
147
+ a = MatrixSymbol("a", 3, 1).as_explicit()
148
+ expr = DiagMatrix(a)
149
+ result = Matrix([
150
+ [a[0, 0], 0, 0],
151
+ [0, a[1, 0], 0],
152
+ [0, 0, a[2, 0]],
153
+ ])
154
+ assert expr.doit() == result
155
+ expr = DiagMatrix(a.T)
156
+ assert expr.doit() == result
venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/test_dotproduct.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.core.expr import unchanged
2
+ from sympy.core.mul import Mul
3
+ from sympy.matrices import Matrix
4
+ from sympy.matrices.expressions.matexpr import MatrixSymbol
5
+ from sympy.matrices.expressions.dotproduct import DotProduct
6
+ from sympy.testing.pytest import raises
7
+
8
+
9
+ A = Matrix(3, 1, [1, 2, 3])
10
+ B = Matrix(3, 1, [1, 3, 5])
11
+ C = Matrix(4, 1, [1, 2, 4, 5])
12
+ D = Matrix(2, 2, [1, 2, 3, 4])
13
+
14
+ def test_docproduct():
15
+ assert DotProduct(A, B).doit() == 22
16
+ assert DotProduct(A.T, B).doit() == 22
17
+ assert DotProduct(A, B.T).doit() == 22
18
+ assert DotProduct(A.T, B.T).doit() == 22
19
+
20
+ raises(TypeError, lambda: DotProduct(1, A))
21
+ raises(TypeError, lambda: DotProduct(A, 1))
22
+ raises(TypeError, lambda: DotProduct(A, D))
23
+ raises(TypeError, lambda: DotProduct(D, A))
24
+
25
+ raises(TypeError, lambda: DotProduct(B, C).doit())
26
+
27
+ def test_dotproduct_symbolic():
28
+ A = MatrixSymbol('A', 3, 1)
29
+ B = MatrixSymbol('B', 3, 1)
30
+
31
+ dot = DotProduct(A, B)
32
+ assert dot.is_scalar == True
33
+ assert unchanged(Mul, 2, dot)
34
+ # XXX Fix forced evaluation for arithmetics with matrix expressions
35
+ assert dot * A == (A[0, 0]*B[0, 0] + A[1, 0]*B[1, 0] + A[2, 0]*B[2, 0])*A
venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/test_factorizations.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.matrices.expressions.factorizations import lu, LofCholesky, qr, svd
2
+ from sympy.assumptions.ask import (Q, ask)
3
+ from sympy.core.symbol import Symbol
4
+ from sympy.matrices.expressions.matexpr import MatrixSymbol
5
+
6
+ n = Symbol('n')
7
+ X = MatrixSymbol('X', n, n)
8
+
9
+ def test_LU():
10
+ L, U = lu(X)
11
+ assert L.shape == U.shape == X.shape
12
+ assert ask(Q.lower_triangular(L))
13
+ assert ask(Q.upper_triangular(U))
14
+
15
+ def test_Cholesky():
16
+ LofCholesky(X)
17
+
18
+ def test_QR():
19
+ Q_, R = qr(X)
20
+ assert Q_.shape == R.shape == X.shape
21
+ assert ask(Q.orthogonal(Q_))
22
+ assert ask(Q.upper_triangular(R))
23
+
24
+ def test_svd():
25
+ U, S, V = svd(X)
26
+ assert U.shape == S.shape == V.shape == X.shape
27
+ assert ask(Q.orthogonal(U))
28
+ assert ask(Q.orthogonal(V))
29
+ assert ask(Q.diagonal(S))
venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/test_funcmatrix.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.core import symbols, Lambda
2
+ from sympy.functions import KroneckerDelta
3
+ from sympy.matrices import Matrix
4
+ from sympy.matrices.expressions import FunctionMatrix, MatrixExpr, Identity
5
+ from sympy.testing.pytest import raises, warns
6
+ from sympy.utilities.exceptions import SymPyDeprecationWarning
7
+
8
+
9
+ def test_funcmatrix_creation():
10
+ i, j, k = symbols('i j k')
11
+ assert FunctionMatrix(2, 2, Lambda((i, j), 0))
12
+ assert FunctionMatrix(0, 0, Lambda((i, j), 0))
13
+
14
+ raises(ValueError, lambda: FunctionMatrix(-1, 0, Lambda((i, j), 0)))
15
+ raises(ValueError, lambda: FunctionMatrix(2.0, 0, Lambda((i, j), 0)))
16
+ raises(ValueError, lambda: FunctionMatrix(2j, 0, Lambda((i, j), 0)))
17
+ raises(ValueError, lambda: FunctionMatrix(0, -1, Lambda((i, j), 0)))
18
+ raises(ValueError, lambda: FunctionMatrix(0, 2.0, Lambda((i, j), 0)))
19
+ raises(ValueError, lambda: FunctionMatrix(0, 2j, Lambda((i, j), 0)))
20
+
21
+ raises(ValueError, lambda: FunctionMatrix(2, 2, Lambda(i, 0)))
22
+ with warns(SymPyDeprecationWarning, test_stacklevel=False):
23
+ # This raises a deprecation warning from sympify()
24
+ raises(ValueError, lambda: FunctionMatrix(2, 2, lambda i, j: 0))
25
+ raises(ValueError, lambda: FunctionMatrix(2, 2, Lambda((i,), 0)))
26
+ raises(ValueError, lambda: FunctionMatrix(2, 2, Lambda((i, j, k), 0)))
27
+ raises(ValueError, lambda: FunctionMatrix(2, 2, i+j))
28
+ assert FunctionMatrix(2, 2, "lambda i, j: 0") == \
29
+ FunctionMatrix(2, 2, Lambda((i, j), 0))
30
+
31
+ m = FunctionMatrix(2, 2, KroneckerDelta)
32
+ assert m.as_explicit() == Identity(2).as_explicit()
33
+ assert m.args[2].dummy_eq(Lambda((i, j), KroneckerDelta(i, j)))
34
+
35
+ n = symbols('n')
36
+ assert FunctionMatrix(n, n, Lambda((i, j), 0))
37
+ n = symbols('n', integer=False)
38
+ raises(ValueError, lambda: FunctionMatrix(n, n, Lambda((i, j), 0)))
39
+ n = symbols('n', negative=True)
40
+ raises(ValueError, lambda: FunctionMatrix(n, n, Lambda((i, j), 0)))
41
+
42
+
43
+ def test_funcmatrix():
44
+ i, j = symbols('i,j')
45
+ X = FunctionMatrix(3, 3, Lambda((i, j), i - j))
46
+ assert X[1, 1] == 0
47
+ assert X[1, 2] == -1
48
+ assert X.shape == (3, 3)
49
+ assert X.rows == X.cols == 3
50
+ assert Matrix(X) == Matrix(3, 3, lambda i, j: i - j)
51
+ assert isinstance(X*X + X, MatrixExpr)
52
+
53
+
54
+ def test_replace_issue():
55
+ X = FunctionMatrix(3, 3, KroneckerDelta)
56
+ assert X.replace(lambda x: True, lambda x: x) == X
venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/test_indexing.py ADDED
@@ -0,0 +1,299 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.concrete.summations import Sum
2
+ from sympy.core.symbol import symbols, Symbol, Dummy
3
+ from sympy.functions.elementary.miscellaneous import sqrt
4
+ from sympy.functions.special.tensor_functions import KroneckerDelta
5
+ from sympy.matrices.dense import eye
6
+ from sympy.matrices.expressions.blockmatrix import BlockMatrix
7
+ from sympy.matrices.expressions.hadamard import HadamardPower
8
+ from sympy.matrices.expressions.matexpr import (MatrixSymbol,
9
+ MatrixExpr, MatrixElement)
10
+ from sympy.matrices.expressions.matpow import MatPow
11
+ from sympy.matrices.expressions.special import (ZeroMatrix, Identity,
12
+ OneMatrix)
13
+ from sympy.matrices.expressions.trace import Trace, trace
14
+ from sympy.matrices.immutable import ImmutableMatrix
15
+ from sympy.tensor.array.expressions.array_expressions import ArrayTensorProduct
16
+ from sympy.testing.pytest import XFAIL, raises
17
+
18
+ k, l, m, n = symbols('k l m n', integer=True)
19
+ i, j = symbols('i j', integer=True)
20
+
21
+ W = MatrixSymbol('W', k, l)
22
+ X = MatrixSymbol('X', l, m)
23
+ Y = MatrixSymbol('Y', l, m)
24
+ Z = MatrixSymbol('Z', m, n)
25
+
26
+ X1 = MatrixSymbol('X1', m, m)
27
+ X2 = MatrixSymbol('X2', m, m)
28
+ X3 = MatrixSymbol('X3', m, m)
29
+ X4 = MatrixSymbol('X4', m, m)
30
+
31
+ A = MatrixSymbol('A', 2, 2)
32
+ B = MatrixSymbol('B', 2, 2)
33
+ x = MatrixSymbol('x', 1, 2)
34
+ y = MatrixSymbol('x', 2, 1)
35
+
36
+
37
+ def test_symbolic_indexing():
38
+ x12 = X[1, 2]
39
+ assert all(s in str(x12) for s in ['1', '2', X.name])
40
+ # We don't care about the exact form of this. We do want to make sure
41
+ # that all of these features are present
42
+
43
+
44
+ def test_add_index():
45
+ assert (X + Y)[i, j] == X[i, j] + Y[i, j]
46
+
47
+
48
+ def test_mul_index():
49
+ assert (A*y)[0, 0] == A[0, 0]*y[0, 0] + A[0, 1]*y[1, 0]
50
+ assert (A*B).as_mutable() == (A.as_mutable() * B.as_mutable())
51
+ X = MatrixSymbol('X', n, m)
52
+ Y = MatrixSymbol('Y', m, k)
53
+
54
+ result = (X*Y)[4,2]
55
+ expected = Sum(X[4, i]*Y[i, 2], (i, 0, m - 1))
56
+ assert result.args[0].dummy_eq(expected.args[0], i)
57
+ assert result.args[1][1:] == expected.args[1][1:]
58
+
59
+
60
+ def test_pow_index():
61
+ Q = MatPow(A, 2)
62
+ assert Q[0, 0] == A[0, 0]**2 + A[0, 1]*A[1, 0]
63
+ n = symbols("n")
64
+ Q2 = A**n
65
+ assert Q2[0, 0] == 2*(
66
+ -sqrt((A[0, 0] + A[1, 1])**2 - 4*A[0, 0]*A[1, 1] +
67
+ 4*A[0, 1]*A[1, 0])/2 + A[0, 0]/2 + A[1, 1]/2
68
+ )**n * \
69
+ A[0, 1]*A[1, 0]/(
70
+ (sqrt(A[0, 0]**2 - 2*A[0, 0]*A[1, 1] + 4*A[0, 1]*A[1, 0] +
71
+ A[1, 1]**2) + A[0, 0] - A[1, 1])*
72
+ sqrt(A[0, 0]**2 - 2*A[0, 0]*A[1, 1] + 4*A[0, 1]*A[1, 0] + A[1, 1]**2)
73
+ ) - 2*(
74
+ sqrt((A[0, 0] + A[1, 1])**2 - 4*A[0, 0]*A[1, 1] +
75
+ 4*A[0, 1]*A[1, 0])/2 + A[0, 0]/2 + A[1, 1]/2
76
+ )**n * A[0, 1]*A[1, 0]/(
77
+ (-sqrt(A[0, 0]**2 - 2*A[0, 0]*A[1, 1] + 4*A[0, 1]*A[1, 0] +
78
+ A[1, 1]**2) + A[0, 0] - A[1, 1])*
79
+ sqrt(A[0, 0]**2 - 2*A[0, 0]*A[1, 1] + 4*A[0, 1]*A[1, 0] + A[1, 1]**2)
80
+ )
81
+
82
+
83
+ def test_transpose_index():
84
+ assert X.T[i, j] == X[j, i]
85
+
86
+
87
+ def test_Identity_index():
88
+ I = Identity(3)
89
+ assert I[0, 0] == I[1, 1] == I[2, 2] == 1
90
+ assert I[1, 0] == I[0, 1] == I[2, 1] == 0
91
+ assert I[i, 0].delta_range == (0, 2)
92
+ raises(IndexError, lambda: I[3, 3])
93
+
94
+
95
+ def test_block_index():
96
+ I = Identity(3)
97
+ Z = ZeroMatrix(3, 3)
98
+ B = BlockMatrix([[I, I], [I, I]])
99
+ e3 = ImmutableMatrix(eye(3))
100
+ BB = BlockMatrix([[e3, e3], [e3, e3]])
101
+ assert B[0, 0] == B[3, 0] == B[0, 3] == B[3, 3] == 1
102
+ assert B[4, 3] == B[5, 1] == 0
103
+
104
+ BB = BlockMatrix([[e3, e3], [e3, e3]])
105
+ assert B.as_explicit() == BB.as_explicit()
106
+
107
+ BI = BlockMatrix([[I, Z], [Z, I]])
108
+
109
+ assert BI.as_explicit().equals(eye(6))
110
+
111
+
112
+ def test_block_index_symbolic():
113
+ # Note that these matrices may be zero-sized and indices may be negative, which causes
114
+ # all naive simplifications given in the comments to be invalid
115
+ A1 = MatrixSymbol('A1', n, k)
116
+ A2 = MatrixSymbol('A2', n, l)
117
+ A3 = MatrixSymbol('A3', m, k)
118
+ A4 = MatrixSymbol('A4', m, l)
119
+ A = BlockMatrix([[A1, A2], [A3, A4]])
120
+ assert A[0, 0] == MatrixElement(A, 0, 0) # Cannot be A1[0, 0]
121
+ assert A[n - 1, k - 1] == A1[n - 1, k - 1]
122
+ assert A[n, k] == A4[0, 0]
123
+ assert A[n + m - 1, 0] == MatrixElement(A, n + m - 1, 0) # Cannot be A3[m - 1, 0]
124
+ assert A[0, k + l - 1] == MatrixElement(A, 0, k + l - 1) # Cannot be A2[0, l - 1]
125
+ assert A[n + m - 1, k + l - 1] == MatrixElement(A, n + m - 1, k + l - 1) # Cannot be A4[m - 1, l - 1]
126
+ assert A[i, j] == MatrixElement(A, i, j)
127
+ assert A[n + i, k + j] == MatrixElement(A, n + i, k + j) # Cannot be A4[i, j]
128
+ assert A[n - i - 1, k - j - 1] == MatrixElement(A, n - i - 1, k - j - 1) # Cannot be A1[n - i - 1, k - j - 1]
129
+
130
+
131
+ def test_block_index_symbolic_nonzero():
132
+ # All invalid simplifications from test_block_index_symbolic() that become valid if all
133
+ # matrices have nonzero size and all indices are nonnegative
134
+ k, l, m, n = symbols('k l m n', integer=True, positive=True)
135
+ i, j = symbols('i j', integer=True, nonnegative=True)
136
+ A1 = MatrixSymbol('A1', n, k)
137
+ A2 = MatrixSymbol('A2', n, l)
138
+ A3 = MatrixSymbol('A3', m, k)
139
+ A4 = MatrixSymbol('A4', m, l)
140
+ A = BlockMatrix([[A1, A2], [A3, A4]])
141
+ assert A[0, 0] == A1[0, 0]
142
+ assert A[n + m - 1, 0] == A3[m - 1, 0]
143
+ assert A[0, k + l - 1] == A2[0, l - 1]
144
+ assert A[n + m - 1, k + l - 1] == A4[m - 1, l - 1]
145
+ assert A[i, j] == MatrixElement(A, i, j)
146
+ assert A[n + i, k + j] == A4[i, j]
147
+ assert A[n - i - 1, k - j - 1] == A1[n - i - 1, k - j - 1]
148
+ assert A[2 * n, 2 * k] == A4[n, k]
149
+
150
+
151
+ def test_block_index_large():
152
+ n, m, k = symbols('n m k', integer=True, positive=True)
153
+ i = symbols('i', integer=True, nonnegative=True)
154
+ A1 = MatrixSymbol('A1', n, n)
155
+ A2 = MatrixSymbol('A2', n, m)
156
+ A3 = MatrixSymbol('A3', n, k)
157
+ A4 = MatrixSymbol('A4', m, n)
158
+ A5 = MatrixSymbol('A5', m, m)
159
+ A6 = MatrixSymbol('A6', m, k)
160
+ A7 = MatrixSymbol('A7', k, n)
161
+ A8 = MatrixSymbol('A8', k, m)
162
+ A9 = MatrixSymbol('A9', k, k)
163
+ A = BlockMatrix([[A1, A2, A3], [A4, A5, A6], [A7, A8, A9]])
164
+ assert A[n + i, n + i] == MatrixElement(A, n + i, n + i)
165
+
166
+
167
+ @XFAIL
168
+ def test_block_index_symbolic_fail():
169
+ # To make this work, symbolic matrix dimensions would need to be somehow assumed nonnegative
170
+ # even if the symbols aren't specified as such. Then 2 * n < n would correctly evaluate to
171
+ # False in BlockMatrix._entry()
172
+ A1 = MatrixSymbol('A1', n, 1)
173
+ A2 = MatrixSymbol('A2', m, 1)
174
+ A = BlockMatrix([[A1], [A2]])
175
+ assert A[2 * n, 0] == A2[n, 0]
176
+
177
+
178
+ def test_slicing():
179
+ A.as_explicit()[0, :] # does not raise an error
180
+
181
+
182
+ def test_errors():
183
+ raises(IndexError, lambda: Identity(2)[1, 2, 3, 4, 5])
184
+ raises(IndexError, lambda: Identity(2)[[1, 2, 3, 4, 5]])
185
+
186
+
187
+ def test_matrix_expression_to_indices():
188
+ i, j = symbols("i, j")
189
+ i1, i2, i3 = symbols("i_1:4")
190
+
191
+ def replace_dummies(expr):
192
+ repl = {i: Symbol(i.name) for i in expr.atoms(Dummy)}
193
+ return expr.xreplace(repl)
194
+
195
+ expr = W*X*Z
196
+ assert replace_dummies(expr._entry(i, j)) == \
197
+ Sum(W[i, i1]*X[i1, i2]*Z[i2, j], (i1, 0, l-1), (i2, 0, m-1))
198
+ assert MatrixExpr.from_index_summation(expr._entry(i, j)) == expr
199
+
200
+ expr = Z.T*X.T*W.T
201
+ assert replace_dummies(expr._entry(i, j)) == \
202
+ Sum(W[j, i2]*X[i2, i1]*Z[i1, i], (i1, 0, m-1), (i2, 0, l-1))
203
+ assert MatrixExpr.from_index_summation(expr._entry(i, j), i) == expr
204
+
205
+ expr = W*X*Z + W*Y*Z
206
+ assert replace_dummies(expr._entry(i, j)) == \
207
+ Sum(W[i, i1]*X[i1, i2]*Z[i2, j], (i1, 0, l-1), (i2, 0, m-1)) +\
208
+ Sum(W[i, i1]*Y[i1, i2]*Z[i2, j], (i1, 0, l-1), (i2, 0, m-1))
209
+ assert MatrixExpr.from_index_summation(expr._entry(i, j)) == expr
210
+
211
+ expr = 2*W*X*Z + 3*W*Y*Z
212
+ assert replace_dummies(expr._entry(i, j)) == \
213
+ 2*Sum(W[i, i1]*X[i1, i2]*Z[i2, j], (i1, 0, l-1), (i2, 0, m-1)) +\
214
+ 3*Sum(W[i, i1]*Y[i1, i2]*Z[i2, j], (i1, 0, l-1), (i2, 0, m-1))
215
+ assert MatrixExpr.from_index_summation(expr._entry(i, j)) == expr
216
+
217
+ expr = W*(X + Y)*Z
218
+ assert replace_dummies(expr._entry(i, j)) == \
219
+ Sum(W[i, i1]*(X[i1, i2] + Y[i1, i2])*Z[i2, j], (i1, 0, l-1), (i2, 0, m-1))
220
+ assert MatrixExpr.from_index_summation(expr._entry(i, j)) == expr
221
+
222
+ expr = A*B**2*A
223
+ #assert replace_dummies(expr._entry(i, j)) == \
224
+ # Sum(A[i, i1]*B[i1, i2]*B[i2, i3]*A[i3, j], (i1, 0, 1), (i2, 0, 1), (i3, 0, 1))
225
+
226
+ # Check that different dummies are used in sub-multiplications:
227
+ expr = (X1*X2 + X2*X1)*X3
228
+ assert replace_dummies(expr._entry(i, j)) == \
229
+ Sum((Sum(X1[i, i2] * X2[i2, i1], (i2, 0, m - 1)) + Sum(X1[i3, i1] * X2[i, i3], (i3, 0, m - 1))) * X3[
230
+ i1, j], (i1, 0, m - 1))
231
+
232
+
233
+ def test_matrix_expression_from_index_summation():
234
+ from sympy.abc import a,b,c,d
235
+ A = MatrixSymbol("A", k, k)
236
+ B = MatrixSymbol("B", k, k)
237
+ C = MatrixSymbol("C", k, k)
238
+ w1 = MatrixSymbol("w1", k, 1)
239
+
240
+ i0, i1, i2, i3, i4 = symbols("i0:5", cls=Dummy)
241
+
242
+ expr = Sum(W[a,b]*X[b,c]*Z[c,d], (b, 0, l-1), (c, 0, m-1))
243
+ assert MatrixExpr.from_index_summation(expr, a) == W*X*Z
244
+ expr = Sum(W.T[b,a]*X[b,c]*Z[c,d], (b, 0, l-1), (c, 0, m-1))
245
+ assert MatrixExpr.from_index_summation(expr, a) == W*X*Z
246
+ expr = Sum(A[b, a]*B[b, c]*C[c, d], (b, 0, k-1), (c, 0, k-1))
247
+ assert MatrixSymbol.from_index_summation(expr, a) == A.T*B*C
248
+ expr = Sum(A[b, a]*B[c, b]*C[c, d], (b, 0, k-1), (c, 0, k-1))
249
+ assert MatrixSymbol.from_index_summation(expr, a) == A.T*B.T*C
250
+ expr = Sum(C[c, d]*A[b, a]*B[c, b], (b, 0, k-1), (c, 0, k-1))
251
+ assert MatrixSymbol.from_index_summation(expr, a) == A.T*B.T*C
252
+ expr = Sum(A[a, b] + B[a, b], (a, 0, k-1), (b, 0, k-1))
253
+ assert MatrixExpr.from_index_summation(expr, a) == OneMatrix(1, k)*A*OneMatrix(k, 1) + OneMatrix(1, k)*B*OneMatrix(k, 1)
254
+ expr = Sum(A[a, b]**2, (a, 0, k - 1), (b, 0, k - 1))
255
+ assert MatrixExpr.from_index_summation(expr, a) == Trace(A * A.T)
256
+ expr = Sum(A[a, b]**3, (a, 0, k - 1), (b, 0, k - 1))
257
+ assert MatrixExpr.from_index_summation(expr, a) == Trace(HadamardPower(A.T, 2) * A)
258
+ expr = Sum((A[a, b] + B[a, b])*C[b, c], (b, 0, k-1))
259
+ assert MatrixExpr.from_index_summation(expr, a) == (A+B)*C
260
+ expr = Sum((A[a, b] + B[b, a])*C[b, c], (b, 0, k-1))
261
+ assert MatrixExpr.from_index_summation(expr, a) == (A+B.T)*C
262
+ expr = Sum(A[a, b]*A[b, c]*A[c, d], (b, 0, k-1), (c, 0, k-1))
263
+ assert MatrixExpr.from_index_summation(expr, a) == A**3
264
+ expr = Sum(A[a, b]*A[b, c]*B[c, d], (b, 0, k-1), (c, 0, k-1))
265
+ assert MatrixExpr.from_index_summation(expr, a) == A**2*B
266
+
267
+ # Parse the trace of a matrix:
268
+
269
+ expr = Sum(A[a, a], (a, 0, k-1))
270
+ assert MatrixExpr.from_index_summation(expr, None) == trace(A)
271
+ expr = Sum(A[a, a]*B[b, c]*C[c, d], (a, 0, k-1), (c, 0, k-1))
272
+ assert MatrixExpr.from_index_summation(expr, b) == trace(A)*B*C
273
+
274
+ # Check wrong sum ranges (should raise an exception):
275
+
276
+ ## Case 1: 0 to m instead of 0 to m-1
277
+ expr = Sum(W[a,b]*X[b,c]*Z[c,d], (b, 0, l-1), (c, 0, m))
278
+ raises(ValueError, lambda: MatrixExpr.from_index_summation(expr, a))
279
+ ## Case 2: 1 to m-1 instead of 0 to m-1
280
+ expr = Sum(W[a,b]*X[b,c]*Z[c,d], (b, 0, l-1), (c, 1, m-1))
281
+ raises(ValueError, lambda: MatrixExpr.from_index_summation(expr, a))
282
+
283
+ # Parse nested sums:
284
+ expr = Sum(A[a, b]*Sum(B[b, c]*C[c, d], (c, 0, k-1)), (b, 0, k-1))
285
+ assert MatrixExpr.from_index_summation(expr, a) == A*B*C
286
+
287
+ # Test Kronecker delta:
288
+ expr = Sum(A[a, b]*KroneckerDelta(b, c)*B[c, d], (b, 0, k-1), (c, 0, k-1))
289
+ assert MatrixExpr.from_index_summation(expr, a) == A*B
290
+
291
+ expr = Sum(KroneckerDelta(i1, m)*KroneckerDelta(i2, n)*A[i, i1]*A[j, i2], (i1, 0, k-1), (i2, 0, k-1))
292
+ assert MatrixExpr.from_index_summation(expr, m) == ArrayTensorProduct(A.T, A)
293
+
294
+ # Test numbered indices:
295
+ expr = Sum(A[i1, i2]*w1[i2, 0], (i2, 0, k-1))
296
+ assert MatrixExpr.from_index_summation(expr, i1) == MatrixElement(A*w1, i1, 0)
297
+
298
+ expr = Sum(A[i1, i2]*B[i2, 0], (i2, 0, k-1))
299
+ assert MatrixExpr.from_index_summation(expr, i1) == MatrixElement(A*B, i1, 0)
venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/test_inverse.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.core import symbols, S
2
+ from sympy.matrices.expressions import MatrixSymbol, Inverse, MatPow, ZeroMatrix, OneMatrix
3
+ from sympy.matrices.common import NonInvertibleMatrixError, NonSquareMatrixError
4
+ from sympy.matrices import eye, Identity
5
+ from sympy.testing.pytest import raises
6
+ from sympy.assumptions.ask import Q
7
+ from sympy.assumptions.refine import refine
8
+
9
+ n, m, l = symbols('n m l', integer=True)
10
+ A = MatrixSymbol('A', n, m)
11
+ B = MatrixSymbol('B', m, l)
12
+ C = MatrixSymbol('C', n, n)
13
+ D = MatrixSymbol('D', n, n)
14
+ E = MatrixSymbol('E', m, n)
15
+
16
+
17
+ def test_inverse():
18
+ assert Inverse(C).args == (C, S.NegativeOne)
19
+ assert Inverse(C).shape == (n, n)
20
+ assert Inverse(A*E).shape == (n, n)
21
+ assert Inverse(E*A).shape == (m, m)
22
+ assert Inverse(C).inverse() == C
23
+ assert Inverse(Inverse(C)).doit() == C
24
+ assert isinstance(Inverse(Inverse(C)), Inverse)
25
+
26
+ assert Inverse(*Inverse(E*A).args) == Inverse(E*A)
27
+
28
+ assert C.inverse().inverse() == C
29
+
30
+ assert C.inverse()*C == Identity(C.rows)
31
+
32
+ assert Identity(n).inverse() == Identity(n)
33
+ assert (3*Identity(n)).inverse() == Identity(n)/3
34
+
35
+ # Simplifies Muls if possible (i.e. submatrices are square)
36
+ assert (C*D).inverse() == D.I*C.I
37
+ # But still works when not possible
38
+ assert isinstance((A*E).inverse(), Inverse)
39
+ assert Inverse(C*D).doit(inv_expand=False) == Inverse(C*D)
40
+
41
+ assert Inverse(eye(3)).doit() == eye(3)
42
+ assert Inverse(eye(3)).doit(deep=False) == eye(3)
43
+
44
+ assert OneMatrix(1, 1).I == Identity(1)
45
+ assert isinstance(OneMatrix(n, n).I, Inverse)
46
+
47
+ def test_inverse_non_invertible():
48
+ raises(NonInvertibleMatrixError, lambda: ZeroMatrix(n, n).I)
49
+ raises(NonInvertibleMatrixError, lambda: OneMatrix(2, 2).I)
50
+
51
+ def test_refine():
52
+ assert refine(C.I, Q.orthogonal(C)) == C.T
53
+
54
+
55
+ def test_inverse_matpow_canonicalization():
56
+ A = MatrixSymbol('A', 3, 3)
57
+ assert Inverse(MatPow(A, 3)).doit() == MatPow(Inverse(A), 3).doit()
58
+
59
+
60
+ def test_nonsquare_error():
61
+ A = MatrixSymbol('A', 3, 4)
62
+ raises(NonSquareMatrixError, lambda: Inverse(A))
venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/test_kronecker.py ADDED
@@ -0,0 +1,150 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.core.mod import Mod
2
+ from sympy.core.numbers import I
3
+ from sympy.core.symbol import symbols
4
+ from sympy.functions.elementary.integers import floor
5
+ from sympy.matrices.dense import (Matrix, eye)
6
+ from sympy.matrices import MatrixSymbol, Identity
7
+ from sympy.matrices.expressions import det, trace
8
+
9
+ from sympy.matrices.expressions.kronecker import (KroneckerProduct,
10
+ kronecker_product,
11
+ combine_kronecker)
12
+
13
+
14
+ mat1 = Matrix([[1, 2 * I], [1 + I, 3]])
15
+ mat2 = Matrix([[2 * I, 3], [4 * I, 2]])
16
+
17
+ i, j, k, n, m, o, p, x = symbols('i,j,k,n,m,o,p,x')
18
+ Z = MatrixSymbol('Z', n, n)
19
+ W = MatrixSymbol('W', m, m)
20
+ A = MatrixSymbol('A', n, m)
21
+ B = MatrixSymbol('B', n, m)
22
+ C = MatrixSymbol('C', m, k)
23
+
24
+
25
+ def test_KroneckerProduct():
26
+ assert isinstance(KroneckerProduct(A, B), KroneckerProduct)
27
+ assert KroneckerProduct(A, B).subs(A, C) == KroneckerProduct(C, B)
28
+ assert KroneckerProduct(A, C).shape == (n*m, m*k)
29
+ assert (KroneckerProduct(A, C) + KroneckerProduct(-A, C)).is_ZeroMatrix
30
+ assert (KroneckerProduct(W, Z) * KroneckerProduct(W.I, Z.I)).is_Identity
31
+
32
+
33
+ def test_KroneckerProduct_identity():
34
+ assert KroneckerProduct(Identity(m), Identity(n)) == Identity(m*n)
35
+ assert KroneckerProduct(eye(2), eye(3)) == eye(6)
36
+
37
+
38
+ def test_KroneckerProduct_explicit():
39
+ X = MatrixSymbol('X', 2, 2)
40
+ Y = MatrixSymbol('Y', 2, 2)
41
+ kp = KroneckerProduct(X, Y)
42
+ assert kp.shape == (4, 4)
43
+ assert kp.as_explicit() == Matrix(
44
+ [
45
+ [X[0, 0]*Y[0, 0], X[0, 0]*Y[0, 1], X[0, 1]*Y[0, 0], X[0, 1]*Y[0, 1]],
46
+ [X[0, 0]*Y[1, 0], X[0, 0]*Y[1, 1], X[0, 1]*Y[1, 0], X[0, 1]*Y[1, 1]],
47
+ [X[1, 0]*Y[0, 0], X[1, 0]*Y[0, 1], X[1, 1]*Y[0, 0], X[1, 1]*Y[0, 1]],
48
+ [X[1, 0]*Y[1, 0], X[1, 0]*Y[1, 1], X[1, 1]*Y[1, 0], X[1, 1]*Y[1, 1]]
49
+ ]
50
+ )
51
+
52
+
53
+ def test_tensor_product_adjoint():
54
+ assert KroneckerProduct(I*A, B).adjoint() == \
55
+ -I*KroneckerProduct(A.adjoint(), B.adjoint())
56
+ assert KroneckerProduct(mat1, mat2).adjoint() == \
57
+ kronecker_product(mat1.adjoint(), mat2.adjoint())
58
+
59
+
60
+ def test_tensor_product_conjugate():
61
+ assert KroneckerProduct(I*A, B).conjugate() == \
62
+ -I*KroneckerProduct(A.conjugate(), B.conjugate())
63
+ assert KroneckerProduct(mat1, mat2).conjugate() == \
64
+ kronecker_product(mat1.conjugate(), mat2.conjugate())
65
+
66
+
67
+ def test_tensor_product_transpose():
68
+ assert KroneckerProduct(I*A, B).transpose() == \
69
+ I*KroneckerProduct(A.transpose(), B.transpose())
70
+ assert KroneckerProduct(mat1, mat2).transpose() == \
71
+ kronecker_product(mat1.transpose(), mat2.transpose())
72
+
73
+
74
+ def test_KroneckerProduct_is_associative():
75
+ assert kronecker_product(A, kronecker_product(
76
+ B, C)) == kronecker_product(kronecker_product(A, B), C)
77
+ assert kronecker_product(A, kronecker_product(
78
+ B, C)) == KroneckerProduct(A, B, C)
79
+
80
+
81
+ def test_KroneckerProduct_is_bilinear():
82
+ assert kronecker_product(x*A, B) == x*kronecker_product(A, B)
83
+ assert kronecker_product(A, x*B) == x*kronecker_product(A, B)
84
+
85
+
86
+ def test_KroneckerProduct_determinant():
87
+ kp = kronecker_product(W, Z)
88
+ assert det(kp) == det(W)**n * det(Z)**m
89
+
90
+
91
+ def test_KroneckerProduct_trace():
92
+ kp = kronecker_product(W, Z)
93
+ assert trace(kp) == trace(W)*trace(Z)
94
+
95
+
96
+ def test_KroneckerProduct_isnt_commutative():
97
+ assert KroneckerProduct(A, B) != KroneckerProduct(B, A)
98
+ assert KroneckerProduct(A, B).is_commutative is False
99
+
100
+
101
+ def test_KroneckerProduct_extracts_commutative_part():
102
+ assert kronecker_product(x * A, 2 * B) == x * \
103
+ 2 * KroneckerProduct(A, B)
104
+
105
+
106
+ def test_KroneckerProduct_inverse():
107
+ kp = kronecker_product(W, Z)
108
+ assert kp.inverse() == kronecker_product(W.inverse(), Z.inverse())
109
+
110
+
111
+ def test_KroneckerProduct_combine_add():
112
+ kp1 = kronecker_product(A, B)
113
+ kp2 = kronecker_product(C, W)
114
+ assert combine_kronecker(kp1*kp2) == kronecker_product(A*C, B*W)
115
+
116
+
117
+ def test_KroneckerProduct_combine_mul():
118
+ X = MatrixSymbol('X', m, n)
119
+ Y = MatrixSymbol('Y', m, n)
120
+ kp1 = kronecker_product(A, X)
121
+ kp2 = kronecker_product(B, Y)
122
+ assert combine_kronecker(kp1+kp2) == kronecker_product(A+B, X+Y)
123
+
124
+
125
+ def test_KroneckerProduct_combine_pow():
126
+ X = MatrixSymbol('X', n, n)
127
+ Y = MatrixSymbol('Y', n, n)
128
+ assert combine_kronecker(KroneckerProduct(
129
+ X, Y)**x) == KroneckerProduct(X**x, Y**x)
130
+ assert combine_kronecker(x * KroneckerProduct(X, Y)
131
+ ** 2) == x * KroneckerProduct(X**2, Y**2)
132
+ assert combine_kronecker(
133
+ x * (KroneckerProduct(X, Y)**2) * KroneckerProduct(A, B)) == x * KroneckerProduct(X**2 * A, Y**2 * B)
134
+ # cannot simplify because of non-square arguments to kronecker product:
135
+ assert combine_kronecker(KroneckerProduct(A, B.T) ** m) == KroneckerProduct(A, B.T) ** m
136
+
137
+
138
+ def test_KroneckerProduct_expand():
139
+ X = MatrixSymbol('X', n, n)
140
+ Y = MatrixSymbol('Y', n, n)
141
+
142
+ assert KroneckerProduct(X + Y, Y + Z).expand(kroneckerproduct=True) == \
143
+ KroneckerProduct(X, Y) + KroneckerProduct(X, Z) + \
144
+ KroneckerProduct(Y, Y) + KroneckerProduct(Y, Z)
145
+
146
+ def test_KroneckerProduct_entry():
147
+ A = MatrixSymbol('A', n, m)
148
+ B = MatrixSymbol('B', o, p)
149
+
150
+ assert KroneckerProduct(A, B)._entry(i, j) == A[Mod(floor(i/o), n), Mod(floor(j/p), m)]*B[Mod(i, o), Mod(j, p)]
venv/lib/python3.10/site-packages/sympy/matrices/expressions/tests/test_matadd.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.matrices.expressions import MatrixSymbol, MatAdd, MatPow, MatMul
2
+ from sympy.matrices.expressions.special import GenericZeroMatrix, ZeroMatrix
3
+ from sympy.matrices.common import ShapeError
4
+ from sympy.matrices import eye, ImmutableMatrix
5
+ from sympy.core import Add, Basic, S
6
+ from sympy.core.add import add
7
+ from sympy.testing.pytest import XFAIL, raises
8
+
9
+ X = MatrixSymbol('X', 2, 2)
10
+ Y = MatrixSymbol('Y', 2, 2)
11
+
12
+ def test_evaluate():
13
+ assert MatAdd(X, X, evaluate=True) == add(X, X, evaluate=True) == MatAdd(X, X).doit()
14
+
15
+ def test_sort_key():
16
+ assert MatAdd(Y, X).doit().args == add(Y, X).doit().args == (X, Y)
17
+
18
+
19
+ def test_matadd_sympify():
20
+ assert isinstance(MatAdd(eye(1), eye(1)).args[0], Basic)
21
+ assert isinstance(add(eye(1), eye(1)).args[0], Basic)
22
+
23
+
24
+ def test_matadd_of_matrices():
25
+ assert MatAdd(eye(2), 4*eye(2), eye(2)).doit() == ImmutableMatrix(6*eye(2))
26
+ assert add(eye(2), 4*eye(2), eye(2)).doit() == ImmutableMatrix(6*eye(2))
27
+
28
+
29
+ def test_doit_args():
30
+ A = ImmutableMatrix([[1, 2], [3, 4]])
31
+ B = ImmutableMatrix([[2, 3], [4, 5]])
32
+ assert MatAdd(A, MatPow(B, 2)).doit() == A + B**2
33
+ assert MatAdd(A, MatMul(A, B)).doit() == A + A*B
34
+ assert (MatAdd(A, X, MatMul(A, B), Y, MatAdd(2*A, B)).doit() ==
35
+ add(A, X, MatMul(A, B), Y, add(2*A, B)).doit() ==
36
+ MatAdd(3*A + A*B + B, X, Y))
37
+
38
+
39
+ def test_generic_identity():
40
+ assert MatAdd.identity == GenericZeroMatrix()
41
+ assert MatAdd.identity != S.Zero
42
+
43
+
44
+ def test_zero_matrix_add():
45
+ assert Add(ZeroMatrix(2, 2), ZeroMatrix(2, 2)) == ZeroMatrix(2, 2)
46
+
47
+ @XFAIL
48
+ def test_matrix_Add_with_scalar():
49
+ raises(TypeError, lambda: Add(0, ZeroMatrix(2, 2)))
50
+
51
+
52
+ def test_shape_error():
53
+ A = MatrixSymbol('A', 2, 3)
54
+ B = MatrixSymbol('B', 3, 3)
55
+ raises(ShapeError, lambda: MatAdd(A, B))
56
+
57
+ A = MatrixSymbol('A', 3, 2)
58
+ raises(ShapeError, lambda: MatAdd(A, B))