applied-ai-018 commited on
Commit
6e19154
·
verified ·
1 Parent(s): 2c43f46

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. env-llmeval/lib/python3.10/site-packages/sympy/matrices/__pycache__/__init__.cpython-310.pyc +0 -0
  2. env-llmeval/lib/python3.10/site-packages/sympy/matrices/__pycache__/common.cpython-310.pyc +0 -0
  3. env-llmeval/lib/python3.10/site-packages/sympy/matrices/__pycache__/decompositions.cpython-310.pyc +0 -0
  4. env-llmeval/lib/python3.10/site-packages/sympy/matrices/__pycache__/dense.cpython-310.pyc +0 -0
  5. env-llmeval/lib/python3.10/site-packages/sympy/matrices/__pycache__/determinant.cpython-310.pyc +0 -0
  6. env-llmeval/lib/python3.10/site-packages/sympy/matrices/__pycache__/eigen.cpython-310.pyc +0 -0
  7. env-llmeval/lib/python3.10/site-packages/sympy/matrices/__pycache__/graph.cpython-310.pyc +0 -0
  8. env-llmeval/lib/python3.10/site-packages/sympy/matrices/__pycache__/immutable.cpython-310.pyc +0 -0
  9. env-llmeval/lib/python3.10/site-packages/sympy/matrices/__pycache__/inverse.cpython-310.pyc +0 -0
  10. env-llmeval/lib/python3.10/site-packages/sympy/matrices/__pycache__/matrices.cpython-310.pyc +0 -0
  11. env-llmeval/lib/python3.10/site-packages/sympy/matrices/__pycache__/normalforms.cpython-310.pyc +0 -0
  12. env-llmeval/lib/python3.10/site-packages/sympy/matrices/__pycache__/reductions.cpython-310.pyc +0 -0
  13. env-llmeval/lib/python3.10/site-packages/sympy/matrices/__pycache__/repmatrix.cpython-310.pyc +0 -0
  14. env-llmeval/lib/python3.10/site-packages/sympy/matrices/__pycache__/solvers.cpython-310.pyc +0 -0
  15. env-llmeval/lib/python3.10/site-packages/sympy/matrices/__pycache__/sparse.cpython-310.pyc +0 -0
  16. env-llmeval/lib/python3.10/site-packages/sympy/matrices/__pycache__/sparsetools.cpython-310.pyc +0 -0
  17. env-llmeval/lib/python3.10/site-packages/sympy/matrices/__pycache__/subspaces.cpython-310.pyc +0 -0
  18. env-llmeval/lib/python3.10/site-packages/sympy/matrices/__pycache__/utilities.cpython-310.pyc +0 -0
  19. env-llmeval/lib/python3.10/site-packages/sympy/matrices/benchmarks/__init__.py +0 -0
  20. env-llmeval/lib/python3.10/site-packages/sympy/matrices/benchmarks/__pycache__/__init__.cpython-310.pyc +0 -0
  21. env-llmeval/lib/python3.10/site-packages/sympy/matrices/benchmarks/__pycache__/bench_matrix.cpython-310.pyc +0 -0
  22. env-llmeval/lib/python3.10/site-packages/sympy/matrices/benchmarks/bench_matrix.py +21 -0
  23. env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/__pycache__/kronecker.cpython-310.pyc +0 -0
  24. env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/__pycache__/sets.cpython-310.pyc +0 -0
  25. env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/__pycache__/special.cpython-310.pyc +0 -0
  26. env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/fourier.py +91 -0
  27. env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/kronecker.py +434 -0
  28. env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/__init__.cpython-310.pyc +0 -0
  29. env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_blockmatrix.cpython-310.pyc +0 -0
  30. env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_companion.cpython-310.pyc +0 -0
  31. env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_derivatives.cpython-310.pyc +0 -0
  32. env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_determinant.cpython-310.pyc +0 -0
  33. env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_dotproduct.cpython-310.pyc +0 -0
  34. env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_factorizations.cpython-310.pyc +0 -0
  35. env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_funcmatrix.cpython-310.pyc +0 -0
  36. env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_hadamard.cpython-310.pyc +0 -0
  37. env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_matexpr.cpython-310.pyc +0 -0
  38. env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_matmul.cpython-310.pyc +0 -0
  39. env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_permutation.cpython-310.pyc +0 -0
  40. env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_slice.cpython-310.pyc +0 -0
  41. env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_special.cpython-310.pyc +0 -0
  42. env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_trace.cpython-310.pyc +0 -0
  43. env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_transpose.cpython-310.pyc +0 -0
  44. env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/tests/test_adjoint.py +34 -0
  45. env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/tests/test_blockmatrix.py +445 -0
  46. env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/tests/test_inverse.py +62 -0
  47. env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/tests/test_kronecker.py +150 -0
  48. env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/tests/test_matexpr.py +567 -0
  49. env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/tests/test_matpow.py +217 -0
  50. env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/tests/test_slice.py +65 -0
env-llmeval/lib/python3.10/site-packages/sympy/matrices/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (2.48 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/matrices/__pycache__/common.cpython-310.pyc ADDED
Binary file (99.9 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/matrices/__pycache__/decompositions.cpython-310.pyc ADDED
Binary file (41.4 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/matrices/__pycache__/dense.cpython-310.pyc ADDED
Binary file (31.2 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/matrices/__pycache__/determinant.cpython-310.pyc ADDED
Binary file (24 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/matrices/__pycache__/eigen.cpython-310.pyc ADDED
Binary file (36.9 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/matrices/__pycache__/graph.cpython-310.pyc ADDED
Binary file (9.11 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/matrices/__pycache__/immutable.cpython-310.pyc ADDED
Binary file (6.24 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/matrices/__pycache__/inverse.cpython-310.pyc ADDED
Binary file (11.4 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/matrices/__pycache__/matrices.cpython-310.pyc ADDED
Binary file (70.7 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/matrices/__pycache__/normalforms.cpython-310.pyc ADDED
Binary file (4.39 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/matrices/__pycache__/reductions.cpython-310.pyc ADDED
Binary file (9.82 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/matrices/__pycache__/repmatrix.cpython-310.pyc ADDED
Binary file (22 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/matrices/__pycache__/solvers.cpython-310.pyc ADDED
Binary file (21.5 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/matrices/__pycache__/sparse.cpython-310.pyc ADDED
Binary file (13.8 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/matrices/__pycache__/sparsetools.cpython-310.pyc ADDED
Binary file (8.52 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/matrices/__pycache__/subspaces.cpython-310.pyc ADDED
Binary file (4.36 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/matrices/__pycache__/utilities.cpython-310.pyc ADDED
Binary file (2.72 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/matrices/benchmarks/__init__.py ADDED
File without changes
env-llmeval/lib/python3.10/site-packages/sympy/matrices/benchmarks/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (190 Bytes). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/matrices/benchmarks/__pycache__/bench_matrix.cpython-310.pyc ADDED
Binary file (898 Bytes). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/matrices/benchmarks/bench_matrix.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.core.numbers import Integer
2
+ from sympy.matrices.dense import (eye, zeros)
3
+
4
+ i3 = Integer(3)
5
+ M = eye(100)
6
+
7
+
8
+ def timeit_Matrix__getitem_ii():
9
+ M[3, 3]
10
+
11
+
12
+ def timeit_Matrix__getitem_II():
13
+ M[i3, i3]
14
+
15
+
16
+ def timeit_Matrix__getslice():
17
+ M[:, :]
18
+
19
+
20
+ def timeit_Matrix_zeronm():
21
+ zeros(100, 100)
env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/__pycache__/kronecker.cpython-310.pyc ADDED
Binary file (16.4 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/__pycache__/sets.cpython-310.pyc ADDED
Binary file (2.81 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/__pycache__/special.cpython-310.pyc ADDED
Binary file (10.7 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/fourier.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.core.sympify import _sympify
2
+ from sympy.matrices.expressions import MatrixExpr
3
+ from sympy.core.numbers import I
4
+ from sympy.core.singleton import S
5
+ from sympy.functions.elementary.exponential import exp
6
+ from sympy.functions.elementary.miscellaneous import sqrt
7
+
8
+
9
+ class DFT(MatrixExpr):
10
+ r"""
11
+ Returns a discrete Fourier transform matrix. The matrix is scaled
12
+ with :math:`\frac{1}{\sqrt{n}}` so that it is unitary.
13
+
14
+ Parameters
15
+ ==========
16
+
17
+ n : integer or Symbol
18
+ Size of the transform.
19
+
20
+ Examples
21
+ ========
22
+
23
+ >>> from sympy.abc import n
24
+ >>> from sympy.matrices.expressions.fourier import DFT
25
+ >>> DFT(3)
26
+ DFT(3)
27
+ >>> DFT(3).as_explicit()
28
+ Matrix([
29
+ [sqrt(3)/3, sqrt(3)/3, sqrt(3)/3],
30
+ [sqrt(3)/3, sqrt(3)*exp(-2*I*pi/3)/3, sqrt(3)*exp(2*I*pi/3)/3],
31
+ [sqrt(3)/3, sqrt(3)*exp(2*I*pi/3)/3, sqrt(3)*exp(-2*I*pi/3)/3]])
32
+ >>> DFT(n).shape
33
+ (n, n)
34
+
35
+ References
36
+ ==========
37
+
38
+ .. [1] https://en.wikipedia.org/wiki/DFT_matrix
39
+
40
+ """
41
+
42
+ def __new__(cls, n):
43
+ n = _sympify(n)
44
+ cls._check_dim(n)
45
+
46
+ obj = super().__new__(cls, n)
47
+ return obj
48
+
49
+ n = property(lambda self: self.args[0]) # type: ignore
50
+ shape = property(lambda self: (self.n, self.n)) # type: ignore
51
+
52
+ def _entry(self, i, j, **kwargs):
53
+ w = exp(-2*S.Pi*I/self.n)
54
+ return w**(i*j) / sqrt(self.n)
55
+
56
+ def _eval_inverse(self):
57
+ return IDFT(self.n)
58
+
59
+
60
+ class IDFT(DFT):
61
+ r"""
62
+ Returns an inverse discrete Fourier transform matrix. The matrix is scaled
63
+ with :math:`\frac{1}{\sqrt{n}}` so that it is unitary.
64
+
65
+ Parameters
66
+ ==========
67
+
68
+ n : integer or Symbol
69
+ Size of the transform
70
+
71
+ Examples
72
+ ========
73
+
74
+ >>> from sympy.matrices.expressions.fourier import DFT, IDFT
75
+ >>> IDFT(3)
76
+ IDFT(3)
77
+ >>> IDFT(4)*DFT(4)
78
+ I
79
+
80
+ See Also
81
+ ========
82
+
83
+ DFT
84
+
85
+ """
86
+ def _entry(self, i, j, **kwargs):
87
+ w = exp(-2*S.Pi*I/self.n)
88
+ return w**(-i*j) / sqrt(self.n)
89
+
90
+ def _eval_inverse(self):
91
+ return DFT(self.n)
env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/kronecker.py ADDED
@@ -0,0 +1,434 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Implementation of the Kronecker product"""
2
+ from functools import reduce
3
+ from math import prod
4
+
5
+ from sympy.core import Mul, sympify
6
+ from sympy.functions import adjoint
7
+ from sympy.matrices.common import ShapeError
8
+ from sympy.matrices.expressions.matexpr import MatrixExpr
9
+ from sympy.matrices.expressions.transpose import transpose
10
+ from sympy.matrices.expressions.special import Identity
11
+ from sympy.matrices.matrices import MatrixBase
12
+ from sympy.strategies import (
13
+ canon, condition, distribute, do_one, exhaust, flatten, typed, unpack)
14
+ from sympy.strategies.traverse import bottom_up
15
+ from sympy.utilities import sift
16
+
17
+ from .matadd import MatAdd
18
+ from .matmul import MatMul
19
+ from .matpow import MatPow
20
+
21
+
22
+ def kronecker_product(*matrices):
23
+ """
24
+ The Kronecker product of two or more arguments.
25
+
26
+ This computes the explicit Kronecker product for subclasses of
27
+ ``MatrixBase`` i.e. explicit matrices. Otherwise, a symbolic
28
+ ``KroneckerProduct`` object is returned.
29
+
30
+
31
+ Examples
32
+ ========
33
+
34
+ For ``MatrixSymbol`` arguments a ``KroneckerProduct`` object is returned.
35
+ Elements of this matrix can be obtained by indexing, or for MatrixSymbols
36
+ with known dimension the explicit matrix can be obtained with
37
+ ``.as_explicit()``
38
+
39
+ >>> from sympy import kronecker_product, MatrixSymbol
40
+ >>> A = MatrixSymbol('A', 2, 2)
41
+ >>> B = MatrixSymbol('B', 2, 2)
42
+ >>> kronecker_product(A)
43
+ A
44
+ >>> kronecker_product(A, B)
45
+ KroneckerProduct(A, B)
46
+ >>> kronecker_product(A, B)[0, 1]
47
+ A[0, 0]*B[0, 1]
48
+ >>> kronecker_product(A, B).as_explicit()
49
+ Matrix([
50
+ [A[0, 0]*B[0, 0], A[0, 0]*B[0, 1], A[0, 1]*B[0, 0], A[0, 1]*B[0, 1]],
51
+ [A[0, 0]*B[1, 0], A[0, 0]*B[1, 1], A[0, 1]*B[1, 0], A[0, 1]*B[1, 1]],
52
+ [A[1, 0]*B[0, 0], A[1, 0]*B[0, 1], A[1, 1]*B[0, 0], A[1, 1]*B[0, 1]],
53
+ [A[1, 0]*B[1, 0], A[1, 0]*B[1, 1], A[1, 1]*B[1, 0], A[1, 1]*B[1, 1]]])
54
+
55
+ For explicit matrices the Kronecker product is returned as a Matrix
56
+
57
+ >>> from sympy import Matrix, kronecker_product
58
+ >>> sigma_x = Matrix([
59
+ ... [0, 1],
60
+ ... [1, 0]])
61
+ ...
62
+ >>> Isigma_y = Matrix([
63
+ ... [0, 1],
64
+ ... [-1, 0]])
65
+ ...
66
+ >>> kronecker_product(sigma_x, Isigma_y)
67
+ Matrix([
68
+ [ 0, 0, 0, 1],
69
+ [ 0, 0, -1, 0],
70
+ [ 0, 1, 0, 0],
71
+ [-1, 0, 0, 0]])
72
+
73
+ See Also
74
+ ========
75
+ KroneckerProduct
76
+
77
+ """
78
+ if not matrices:
79
+ raise TypeError("Empty Kronecker product is undefined")
80
+ if len(matrices) == 1:
81
+ return matrices[0]
82
+ else:
83
+ return KroneckerProduct(*matrices).doit()
84
+
85
+
86
+ class KroneckerProduct(MatrixExpr):
87
+ """
88
+ The Kronecker product of two or more arguments.
89
+
90
+ The Kronecker product is a non-commutative product of matrices.
91
+ Given two matrices of dimension (m, n) and (s, t) it produces a matrix
92
+ of dimension (m s, n t).
93
+
94
+ This is a symbolic object that simply stores its argument without
95
+ evaluating it. To actually compute the product, use the function
96
+ ``kronecker_product()`` or call the ``.doit()`` or ``.as_explicit()``
97
+ methods.
98
+
99
+ >>> from sympy import KroneckerProduct, MatrixSymbol
100
+ >>> A = MatrixSymbol('A', 5, 5)
101
+ >>> B = MatrixSymbol('B', 5, 5)
102
+ >>> isinstance(KroneckerProduct(A, B), KroneckerProduct)
103
+ True
104
+ """
105
+ is_KroneckerProduct = True
106
+
107
+ def __new__(cls, *args, check=True):
108
+ args = list(map(sympify, args))
109
+ if all(a.is_Identity for a in args):
110
+ ret = Identity(prod(a.rows for a in args))
111
+ if all(isinstance(a, MatrixBase) for a in args):
112
+ return ret.as_explicit()
113
+ else:
114
+ return ret
115
+
116
+ if check:
117
+ validate(*args)
118
+ return super().__new__(cls, *args)
119
+
120
+ @property
121
+ def shape(self):
122
+ rows, cols = self.args[0].shape
123
+ for mat in self.args[1:]:
124
+ rows *= mat.rows
125
+ cols *= mat.cols
126
+ return (rows, cols)
127
+
128
+ def _entry(self, i, j, **kwargs):
129
+ result = 1
130
+ for mat in reversed(self.args):
131
+ i, m = divmod(i, mat.rows)
132
+ j, n = divmod(j, mat.cols)
133
+ result *= mat[m, n]
134
+ return result
135
+
136
+ def _eval_adjoint(self):
137
+ return KroneckerProduct(*list(map(adjoint, self.args))).doit()
138
+
139
+ def _eval_conjugate(self):
140
+ return KroneckerProduct(*[a.conjugate() for a in self.args]).doit()
141
+
142
+ def _eval_transpose(self):
143
+ return KroneckerProduct(*list(map(transpose, self.args))).doit()
144
+
145
+ def _eval_trace(self):
146
+ from .trace import trace
147
+ return Mul(*[trace(a) for a in self.args])
148
+
149
+ def _eval_determinant(self):
150
+ from .determinant import det, Determinant
151
+ if not all(a.is_square for a in self.args):
152
+ return Determinant(self)
153
+
154
+ m = self.rows
155
+ return Mul(*[det(a)**(m/a.rows) for a in self.args])
156
+
157
+ def _eval_inverse(self):
158
+ try:
159
+ return KroneckerProduct(*[a.inverse() for a in self.args])
160
+ except ShapeError:
161
+ from sympy.matrices.expressions.inverse import Inverse
162
+ return Inverse(self)
163
+
164
+ def structurally_equal(self, other):
165
+ '''Determine whether two matrices have the same Kronecker product structure
166
+
167
+ Examples
168
+ ========
169
+
170
+ >>> from sympy import KroneckerProduct, MatrixSymbol, symbols
171
+ >>> m, n = symbols(r'm, n', integer=True)
172
+ >>> A = MatrixSymbol('A', m, m)
173
+ >>> B = MatrixSymbol('B', n, n)
174
+ >>> C = MatrixSymbol('C', m, m)
175
+ >>> D = MatrixSymbol('D', n, n)
176
+ >>> KroneckerProduct(A, B).structurally_equal(KroneckerProduct(C, D))
177
+ True
178
+ >>> KroneckerProduct(A, B).structurally_equal(KroneckerProduct(D, C))
179
+ False
180
+ >>> KroneckerProduct(A, B).structurally_equal(C)
181
+ False
182
+ '''
183
+ # Inspired by BlockMatrix
184
+ return (isinstance(other, KroneckerProduct)
185
+ and self.shape == other.shape
186
+ and len(self.args) == len(other.args)
187
+ and all(a.shape == b.shape for (a, b) in zip(self.args, other.args)))
188
+
189
+ def has_matching_shape(self, other):
190
+ '''Determine whether two matrices have the appropriate structure to bring matrix
191
+ multiplication inside the KroneckerProdut
192
+
193
+ Examples
194
+ ========
195
+ >>> from sympy import KroneckerProduct, MatrixSymbol, symbols
196
+ >>> m, n = symbols(r'm, n', integer=True)
197
+ >>> A = MatrixSymbol('A', m, n)
198
+ >>> B = MatrixSymbol('B', n, m)
199
+ >>> KroneckerProduct(A, B).has_matching_shape(KroneckerProduct(B, A))
200
+ True
201
+ >>> KroneckerProduct(A, B).has_matching_shape(KroneckerProduct(A, B))
202
+ False
203
+ >>> KroneckerProduct(A, B).has_matching_shape(A)
204
+ False
205
+ '''
206
+ return (isinstance(other, KroneckerProduct)
207
+ and self.cols == other.rows
208
+ and len(self.args) == len(other.args)
209
+ and all(a.cols == b.rows for (a, b) in zip(self.args, other.args)))
210
+
211
+ def _eval_expand_kroneckerproduct(self, **hints):
212
+ return flatten(canon(typed({KroneckerProduct: distribute(KroneckerProduct, MatAdd)}))(self))
213
+
214
+ def _kronecker_add(self, other):
215
+ if self.structurally_equal(other):
216
+ return self.__class__(*[a + b for (a, b) in zip(self.args, other.args)])
217
+ else:
218
+ return self + other
219
+
220
+ def _kronecker_mul(self, other):
221
+ if self.has_matching_shape(other):
222
+ return self.__class__(*[a*b for (a, b) in zip(self.args, other.args)])
223
+ else:
224
+ return self * other
225
+
226
+ def doit(self, **hints):
227
+ deep = hints.get('deep', True)
228
+ if deep:
229
+ args = [arg.doit(**hints) for arg in self.args]
230
+ else:
231
+ args = self.args
232
+ return canonicalize(KroneckerProduct(*args))
233
+
234
+
235
+ def validate(*args):
236
+ if not all(arg.is_Matrix for arg in args):
237
+ raise TypeError("Mix of Matrix and Scalar symbols")
238
+
239
+
240
+ # rules
241
+
242
+ def extract_commutative(kron):
243
+ c_part = []
244
+ nc_part = []
245
+ for arg in kron.args:
246
+ c, nc = arg.args_cnc()
247
+ c_part.extend(c)
248
+ nc_part.append(Mul._from_args(nc))
249
+
250
+ c_part = Mul(*c_part)
251
+ if c_part != 1:
252
+ return c_part*KroneckerProduct(*nc_part)
253
+ return kron
254
+
255
+
256
+ def matrix_kronecker_product(*matrices):
257
+ """Compute the Kronecker product of a sequence of SymPy Matrices.
258
+
259
+ This is the standard Kronecker product of matrices [1].
260
+
261
+ Parameters
262
+ ==========
263
+
264
+ matrices : tuple of MatrixBase instances
265
+ The matrices to take the Kronecker product of.
266
+
267
+ Returns
268
+ =======
269
+
270
+ matrix : MatrixBase
271
+ The Kronecker product matrix.
272
+
273
+ Examples
274
+ ========
275
+
276
+ >>> from sympy import Matrix
277
+ >>> from sympy.matrices.expressions.kronecker import (
278
+ ... matrix_kronecker_product)
279
+
280
+ >>> m1 = Matrix([[1,2],[3,4]])
281
+ >>> m2 = Matrix([[1,0],[0,1]])
282
+ >>> matrix_kronecker_product(m1, m2)
283
+ Matrix([
284
+ [1, 0, 2, 0],
285
+ [0, 1, 0, 2],
286
+ [3, 0, 4, 0],
287
+ [0, 3, 0, 4]])
288
+ >>> matrix_kronecker_product(m2, m1)
289
+ Matrix([
290
+ [1, 2, 0, 0],
291
+ [3, 4, 0, 0],
292
+ [0, 0, 1, 2],
293
+ [0, 0, 3, 4]])
294
+
295
+ References
296
+ ==========
297
+
298
+ .. [1] https://en.wikipedia.org/wiki/Kronecker_product
299
+ """
300
+ # Make sure we have a sequence of Matrices
301
+ if not all(isinstance(m, MatrixBase) for m in matrices):
302
+ raise TypeError(
303
+ 'Sequence of Matrices expected, got: %s' % repr(matrices)
304
+ )
305
+
306
+ # Pull out the first element in the product.
307
+ matrix_expansion = matrices[-1]
308
+ # Do the kronecker product working from right to left.
309
+ for mat in reversed(matrices[:-1]):
310
+ rows = mat.rows
311
+ cols = mat.cols
312
+ # Go through each row appending kronecker product to.
313
+ # running matrix_expansion.
314
+ for i in range(rows):
315
+ start = matrix_expansion*mat[i*cols]
316
+ # Go through each column joining each item
317
+ for j in range(cols - 1):
318
+ start = start.row_join(
319
+ matrix_expansion*mat[i*cols + j + 1]
320
+ )
321
+ # If this is the first element, make it the start of the
322
+ # new row.
323
+ if i == 0:
324
+ next = start
325
+ else:
326
+ next = next.col_join(start)
327
+ matrix_expansion = next
328
+
329
+ MatrixClass = max(matrices, key=lambda M: M._class_priority).__class__
330
+ if isinstance(matrix_expansion, MatrixClass):
331
+ return matrix_expansion
332
+ else:
333
+ return MatrixClass(matrix_expansion)
334
+
335
+
336
+ def explicit_kronecker_product(kron):
337
+ # Make sure we have a sequence of Matrices
338
+ if not all(isinstance(m, MatrixBase) for m in kron.args):
339
+ return kron
340
+
341
+ return matrix_kronecker_product(*kron.args)
342
+
343
+
344
+ rules = (unpack,
345
+ explicit_kronecker_product,
346
+ flatten,
347
+ extract_commutative)
348
+
349
+ canonicalize = exhaust(condition(lambda x: isinstance(x, KroneckerProduct),
350
+ do_one(*rules)))
351
+
352
+
353
+ def _kronecker_dims_key(expr):
354
+ if isinstance(expr, KroneckerProduct):
355
+ return tuple(a.shape for a in expr.args)
356
+ else:
357
+ return (0,)
358
+
359
+
360
+ def kronecker_mat_add(expr):
361
+ args = sift(expr.args, _kronecker_dims_key)
362
+ nonkrons = args.pop((0,), None)
363
+ if not args:
364
+ return expr
365
+
366
+ krons = [reduce(lambda x, y: x._kronecker_add(y), group)
367
+ for group in args.values()]
368
+
369
+ if not nonkrons:
370
+ return MatAdd(*krons)
371
+ else:
372
+ return MatAdd(*krons) + nonkrons
373
+
374
+
375
+ def kronecker_mat_mul(expr):
376
+ # modified from block matrix code
377
+ factor, matrices = expr.as_coeff_matrices()
378
+
379
+ i = 0
380
+ while i < len(matrices) - 1:
381
+ A, B = matrices[i:i+2]
382
+ if isinstance(A, KroneckerProduct) and isinstance(B, KroneckerProduct):
383
+ matrices[i] = A._kronecker_mul(B)
384
+ matrices.pop(i+1)
385
+ else:
386
+ i += 1
387
+
388
+ return factor*MatMul(*matrices)
389
+
390
+
391
+ def kronecker_mat_pow(expr):
392
+ if isinstance(expr.base, KroneckerProduct) and all(a.is_square for a in expr.base.args):
393
+ return KroneckerProduct(*[MatPow(a, expr.exp) for a in expr.base.args])
394
+ else:
395
+ return expr
396
+
397
+
398
+ def combine_kronecker(expr):
399
+ """Combine KronekeckerProduct with expression.
400
+
401
+ If possible write operations on KroneckerProducts of compatible shapes
402
+ as a single KroneckerProduct.
403
+
404
+ Examples
405
+ ========
406
+
407
+ >>> from sympy.matrices.expressions import combine_kronecker
408
+ >>> from sympy import MatrixSymbol, KroneckerProduct, symbols
409
+ >>> m, n = symbols(r'm, n', integer=True)
410
+ >>> A = MatrixSymbol('A', m, n)
411
+ >>> B = MatrixSymbol('B', n, m)
412
+ >>> combine_kronecker(KroneckerProduct(A, B)*KroneckerProduct(B, A))
413
+ KroneckerProduct(A*B, B*A)
414
+ >>> combine_kronecker(KroneckerProduct(A, B)+KroneckerProduct(B.T, A.T))
415
+ KroneckerProduct(A + B.T, B + A.T)
416
+ >>> C = MatrixSymbol('C', n, n)
417
+ >>> D = MatrixSymbol('D', m, m)
418
+ >>> combine_kronecker(KroneckerProduct(C, D)**m)
419
+ KroneckerProduct(C**m, D**m)
420
+ """
421
+ def haskron(expr):
422
+ return isinstance(expr, MatrixExpr) and expr.has(KroneckerProduct)
423
+
424
+ rule = exhaust(
425
+ bottom_up(exhaust(condition(haskron, typed(
426
+ {MatAdd: kronecker_mat_add,
427
+ MatMul: kronecker_mat_mul,
428
+ MatPow: kronecker_mat_pow})))))
429
+ result = rule(expr)
430
+ doit = getattr(result, 'doit', None)
431
+ if doit is not None:
432
+ return doit()
433
+ else:
434
+ return result
env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (197 Bytes). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_blockmatrix.cpython-310.pyc ADDED
Binary file (15.8 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_companion.cpython-310.pyc ADDED
Binary file (2.38 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_derivatives.cpython-310.pyc ADDED
Binary file (13.3 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_determinant.cpython-310.pyc ADDED
Binary file (2.82 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_dotproduct.cpython-310.pyc ADDED
Binary file (1.92 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_factorizations.cpython-310.pyc ADDED
Binary file (1.34 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_funcmatrix.cpython-310.pyc ADDED
Binary file (3.82 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_hadamard.cpython-310.pyc ADDED
Binary file (5.59 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_matexpr.cpython-310.pyc ADDED
Binary file (21.8 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_matmul.cpython-310.pyc ADDED
Binary file (8.06 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_permutation.cpython-310.pyc ADDED
Binary file (5.9 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_slice.cpython-310.pyc ADDED
Binary file (3.7 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_special.cpython-310.pyc ADDED
Binary file (8.41 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_trace.cpython-310.pyc ADDED
Binary file (4.66 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_transpose.cpython-310.pyc ADDED
Binary file (2.66 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/tests/test_adjoint.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.core import symbols, S
2
+ from sympy.functions import adjoint, conjugate, transpose
3
+ from sympy.matrices.expressions import MatrixSymbol, Adjoint, trace, Transpose
4
+ from sympy.matrices import eye, Matrix
5
+
6
+ n, m, l, k, p = symbols('n m l k p', integer=True)
7
+ A = MatrixSymbol('A', n, m)
8
+ B = MatrixSymbol('B', m, l)
9
+ C = MatrixSymbol('C', n, n)
10
+
11
+
12
+ def test_adjoint():
13
+ Sq = MatrixSymbol('Sq', n, n)
14
+
15
+ assert Adjoint(A).shape == (m, n)
16
+ assert Adjoint(A*B).shape == (l, n)
17
+ assert adjoint(Adjoint(A)) == A
18
+ assert isinstance(Adjoint(Adjoint(A)), Adjoint)
19
+
20
+ assert conjugate(Adjoint(A)) == Transpose(A)
21
+ assert transpose(Adjoint(A)) == Adjoint(Transpose(A))
22
+
23
+ assert Adjoint(eye(3)).doit() == eye(3)
24
+
25
+ assert Adjoint(S(5)).doit() == S(5)
26
+
27
+ assert Adjoint(Matrix([[1, 2], [3, 4]])).doit() == Matrix([[1, 3], [2, 4]])
28
+
29
+ assert adjoint(trace(Sq)) == conjugate(trace(Sq))
30
+ assert trace(adjoint(Sq)) == conjugate(trace(Sq))
31
+
32
+ assert Adjoint(Sq)[0, 1] == conjugate(Sq[1, 0])
33
+
34
+ assert Adjoint(A*B).doit() == Adjoint(B) * Adjoint(A)
env-llmeval/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)]])
env-llmeval/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))
env-llmeval/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)]
env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/tests/test_matexpr.py ADDED
@@ -0,0 +1,567 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.concrete.summations import Sum
2
+ from sympy.core.exprtools import gcd_terms
3
+ from sympy.core.function import (diff, expand)
4
+ from sympy.core.relational import Eq
5
+ from sympy.core.symbol import (Dummy, Symbol, Str)
6
+ from sympy.functions.special.tensor_functions import KroneckerDelta
7
+ from sympy.matrices.dense import zeros
8
+ from sympy.polys.polytools import factor
9
+
10
+ from sympy.core import (S, symbols, Add, Mul, SympifyError, Rational,
11
+ Function)
12
+ from sympy.functions import sin, cos, tan, sqrt, cbrt, exp
13
+ from sympy.simplify import simplify
14
+ from sympy.matrices import (ImmutableMatrix, Inverse, MatAdd, MatMul,
15
+ MatPow, Matrix, MatrixExpr, MatrixSymbol,
16
+ SparseMatrix, Transpose, Adjoint, MatrixSet)
17
+ from sympy.matrices.common import NonSquareMatrixError
18
+ from sympy.matrices.expressions.determinant import Determinant, det
19
+ from sympy.matrices.expressions.matexpr import MatrixElement
20
+ from sympy.matrices.expressions.special import ZeroMatrix, Identity
21
+ from sympy.testing.pytest import raises, XFAIL
22
+
23
+
24
+ n, m, l, k, p = symbols('n m l k p', integer=True)
25
+ x = symbols('x')
26
+ A = MatrixSymbol('A', n, m)
27
+ B = MatrixSymbol('B', m, l)
28
+ C = MatrixSymbol('C', n, n)
29
+ D = MatrixSymbol('D', n, n)
30
+ E = MatrixSymbol('E', m, n)
31
+ w = MatrixSymbol('w', n, 1)
32
+
33
+
34
+ def test_matrix_symbol_creation():
35
+ assert MatrixSymbol('A', 2, 2)
36
+ assert MatrixSymbol('A', 0, 0)
37
+ raises(ValueError, lambda: MatrixSymbol('A', -1, 2))
38
+ raises(ValueError, lambda: MatrixSymbol('A', 2.0, 2))
39
+ raises(ValueError, lambda: MatrixSymbol('A', 2j, 2))
40
+ raises(ValueError, lambda: MatrixSymbol('A', 2, -1))
41
+ raises(ValueError, lambda: MatrixSymbol('A', 2, 2.0))
42
+ raises(ValueError, lambda: MatrixSymbol('A', 2, 2j))
43
+
44
+ n = symbols('n')
45
+ assert MatrixSymbol('A', n, n)
46
+ n = symbols('n', integer=False)
47
+ raises(ValueError, lambda: MatrixSymbol('A', n, n))
48
+ n = symbols('n', negative=True)
49
+ raises(ValueError, lambda: MatrixSymbol('A', n, n))
50
+
51
+
52
+ def test_matexpr_properties():
53
+ assert A.shape == (n, m)
54
+ assert (A * B).shape == (n, l)
55
+ assert A[0, 1].indices == (0, 1)
56
+ assert A[0, 0].symbol == A
57
+ assert A[0, 0].symbol.name == 'A'
58
+
59
+
60
+ def test_matexpr():
61
+ assert (x*A).shape == A.shape
62
+ assert (x*A).__class__ == MatMul
63
+ assert 2*A - A - A == ZeroMatrix(*A.shape)
64
+ assert (A*B).shape == (n, l)
65
+
66
+
67
+ def test_matexpr_subs():
68
+ A = MatrixSymbol('A', n, m)
69
+ B = MatrixSymbol('B', m, l)
70
+ C = MatrixSymbol('C', m, l)
71
+
72
+ assert A.subs(n, m).shape == (m, m)
73
+ assert (A*B).subs(B, C) == A*C
74
+ assert (A*B).subs(l, n).is_square
75
+
76
+ W = MatrixSymbol("W", 3, 3)
77
+ X = MatrixSymbol("X", 2, 2)
78
+ Y = MatrixSymbol("Y", 1, 2)
79
+ Z = MatrixSymbol("Z", n, 2)
80
+ # no restrictions on Symbol replacement
81
+ assert X.subs(X, Y) == Y
82
+ # it might be better to just change the name
83
+ y = Str('y')
84
+ assert X.subs(Str("X"), y).args == (y, 2, 2)
85
+ # it's ok to introduce a wider matrix
86
+ assert X[1, 1].subs(X, W) == W[1, 1]
87
+ # but for a given MatrixExpression, only change
88
+ # name if indexing on the new shape is valid.
89
+ # Here, X is 2,2; Y is 1,2 and Y[1, 1] is out
90
+ # of range so an error is raised
91
+ raises(IndexError, lambda: X[1, 1].subs(X, Y))
92
+ # here, [0, 1] is in range so the subs succeeds
93
+ assert X[0, 1].subs(X, Y) == Y[0, 1]
94
+ # and here the size of n will accept any index
95
+ # in the first position
96
+ assert W[2, 1].subs(W, Z) == Z[2, 1]
97
+ # but not in the second position
98
+ raises(IndexError, lambda: W[2, 2].subs(W, Z))
99
+ # any matrix should raise if invalid
100
+ raises(IndexError, lambda: W[2, 2].subs(W, zeros(2)))
101
+
102
+ A = SparseMatrix([[1, 2], [3, 4]])
103
+ B = Matrix([[1, 2], [3, 4]])
104
+ C, D = MatrixSymbol('C', 2, 2), MatrixSymbol('D', 2, 2)
105
+
106
+ assert (C*D).subs({C: A, D: B}) == MatMul(A, B)
107
+
108
+
109
+ def test_addition():
110
+ A = MatrixSymbol('A', n, m)
111
+ B = MatrixSymbol('B', n, m)
112
+
113
+ assert isinstance(A + B, MatAdd)
114
+ assert (A + B).shape == A.shape
115
+ assert isinstance(A - A + 2*B, MatMul)
116
+
117
+ raises(TypeError, lambda: A + 1)
118
+ raises(TypeError, lambda: 5 + A)
119
+ raises(TypeError, lambda: 5 - A)
120
+
121
+ assert A + ZeroMatrix(n, m) - A == ZeroMatrix(n, m)
122
+ raises(TypeError, lambda: ZeroMatrix(n, m) + S.Zero)
123
+
124
+
125
+ def test_multiplication():
126
+ A = MatrixSymbol('A', n, m)
127
+ B = MatrixSymbol('B', m, l)
128
+ C = MatrixSymbol('C', n, n)
129
+
130
+ assert (2*A*B).shape == (n, l)
131
+ assert (A*0*B) == ZeroMatrix(n, l)
132
+ assert (2*A).shape == A.shape
133
+
134
+ assert A * ZeroMatrix(m, m) * B == ZeroMatrix(n, l)
135
+
136
+ assert C * Identity(n) * C.I == Identity(n)
137
+
138
+ assert B/2 == S.Half*B
139
+ raises(NotImplementedError, lambda: 2/B)
140
+
141
+ A = MatrixSymbol('A', n, n)
142
+ B = MatrixSymbol('B', n, n)
143
+ assert Identity(n) * (A + B) == A + B
144
+
145
+ assert A**2*A == A**3
146
+ assert A**2*(A.I)**3 == A.I
147
+ assert A**3*(A.I)**2 == A
148
+
149
+
150
+ def test_MatPow():
151
+ A = MatrixSymbol('A', n, n)
152
+
153
+ AA = MatPow(A, 2)
154
+ assert AA.exp == 2
155
+ assert AA.base == A
156
+ assert (A**n).exp == n
157
+
158
+ assert A**0 == Identity(n)
159
+ assert A**1 == A
160
+ assert A**2 == AA
161
+ assert A**-1 == Inverse(A)
162
+ assert (A**-1)**-1 == A
163
+ assert (A**2)**3 == A**6
164
+ assert A**S.Half == sqrt(A)
165
+ assert A**Rational(1, 3) == cbrt(A)
166
+ raises(NonSquareMatrixError, lambda: MatrixSymbol('B', 3, 2)**2)
167
+
168
+
169
+ def test_MatrixSymbol():
170
+ n, m, t = symbols('n,m,t')
171
+ X = MatrixSymbol('X', n, m)
172
+ assert X.shape == (n, m)
173
+ raises(TypeError, lambda: MatrixSymbol('X', n, m)(t)) # issue 5855
174
+ assert X.doit() == X
175
+
176
+
177
+ def test_dense_conversion():
178
+ X = MatrixSymbol('X', 2, 2)
179
+ assert ImmutableMatrix(X) == ImmutableMatrix(2, 2, lambda i, j: X[i, j])
180
+ assert Matrix(X) == Matrix(2, 2, lambda i, j: X[i, j])
181
+
182
+
183
+ def test_free_symbols():
184
+ assert (C*D).free_symbols == {C, D}
185
+
186
+
187
+ def test_zero_matmul():
188
+ assert isinstance(S.Zero * MatrixSymbol('X', 2, 2), MatrixExpr)
189
+
190
+
191
+ def test_matadd_simplify():
192
+ A = MatrixSymbol('A', 1, 1)
193
+ assert simplify(MatAdd(A, ImmutableMatrix([[sin(x)**2 + cos(x)**2]]))) == \
194
+ MatAdd(A, Matrix([[1]]))
195
+
196
+
197
+ def test_matmul_simplify():
198
+ A = MatrixSymbol('A', 1, 1)
199
+ assert simplify(MatMul(A, ImmutableMatrix([[sin(x)**2 + cos(x)**2]]))) == \
200
+ MatMul(A, Matrix([[1]]))
201
+
202
+
203
+ def test_invariants():
204
+ A = MatrixSymbol('A', n, m)
205
+ B = MatrixSymbol('B', m, l)
206
+ X = MatrixSymbol('X', n, n)
207
+ objs = [Identity(n), ZeroMatrix(m, n), A, MatMul(A, B), MatAdd(A, A),
208
+ Transpose(A), Adjoint(A), Inverse(X), MatPow(X, 2), MatPow(X, -1),
209
+ MatPow(X, 0)]
210
+ for obj in objs:
211
+ assert obj == obj.__class__(*obj.args)
212
+
213
+
214
+ def test_matexpr_indexing():
215
+ A = MatrixSymbol('A', n, m)
216
+ A[1, 2]
217
+ A[l, k]
218
+ A[l + 1, k + 1]
219
+ A = MatrixSymbol('A', 2, 1)
220
+ for i in range(-2, 2):
221
+ for j in range(-1, 1):
222
+ A[i, j]
223
+
224
+
225
+ def test_single_indexing():
226
+ A = MatrixSymbol('A', 2, 3)
227
+ assert A[1] == A[0, 1]
228
+ assert A[int(1)] == A[0, 1]
229
+ assert A[3] == A[1, 0]
230
+ assert list(A[:2, :2]) == [A[0, 0], A[0, 1], A[1, 0], A[1, 1]]
231
+ raises(IndexError, lambda: A[6])
232
+ raises(IndexError, lambda: A[n])
233
+ B = MatrixSymbol('B', n, m)
234
+ raises(IndexError, lambda: B[1])
235
+ B = MatrixSymbol('B', n, 3)
236
+ assert B[3] == B[1, 0]
237
+
238
+
239
+ def test_MatrixElement_commutative():
240
+ assert A[0, 1]*A[1, 0] == A[1, 0]*A[0, 1]
241
+
242
+
243
+ def test_MatrixSymbol_determinant():
244
+ A = MatrixSymbol('A', 4, 4)
245
+ assert A.as_explicit().det() == A[0, 0]*A[1, 1]*A[2, 2]*A[3, 3] - \
246
+ A[0, 0]*A[1, 1]*A[2, 3]*A[3, 2] - A[0, 0]*A[1, 2]*A[2, 1]*A[3, 3] + \
247
+ A[0, 0]*A[1, 2]*A[2, 3]*A[3, 1] + A[0, 0]*A[1, 3]*A[2, 1]*A[3, 2] - \
248
+ A[0, 0]*A[1, 3]*A[2, 2]*A[3, 1] - A[0, 1]*A[1, 0]*A[2, 2]*A[3, 3] + \
249
+ A[0, 1]*A[1, 0]*A[2, 3]*A[3, 2] + A[0, 1]*A[1, 2]*A[2, 0]*A[3, 3] - \
250
+ A[0, 1]*A[1, 2]*A[2, 3]*A[3, 0] - A[0, 1]*A[1, 3]*A[2, 0]*A[3, 2] + \
251
+ A[0, 1]*A[1, 3]*A[2, 2]*A[3, 0] + A[0, 2]*A[1, 0]*A[2, 1]*A[3, 3] - \
252
+ A[0, 2]*A[1, 0]*A[2, 3]*A[3, 1] - A[0, 2]*A[1, 1]*A[2, 0]*A[3, 3] + \
253
+ A[0, 2]*A[1, 1]*A[2, 3]*A[3, 0] + A[0, 2]*A[1, 3]*A[2, 0]*A[3, 1] - \
254
+ A[0, 2]*A[1, 3]*A[2, 1]*A[3, 0] - A[0, 3]*A[1, 0]*A[2, 1]*A[3, 2] + \
255
+ A[0, 3]*A[1, 0]*A[2, 2]*A[3, 1] + A[0, 3]*A[1, 1]*A[2, 0]*A[3, 2] - \
256
+ A[0, 3]*A[1, 1]*A[2, 2]*A[3, 0] - A[0, 3]*A[1, 2]*A[2, 0]*A[3, 1] + \
257
+ A[0, 3]*A[1, 2]*A[2, 1]*A[3, 0]
258
+
259
+ B = MatrixSymbol('B', 4, 4)
260
+ assert Determinant(A + B).doit() == det(A + B) == (A + B).det()
261
+
262
+
263
+ def test_MatrixElement_diff():
264
+ assert (A[3, 0]*A[0, 0]).diff(A[0, 0]) == A[3, 0]
265
+
266
+
267
+ def test_MatrixElement_doit():
268
+ u = MatrixSymbol('u', 2, 1)
269
+ v = ImmutableMatrix([3, 5])
270
+ assert u[0, 0].subs(u, v).doit() == v[0, 0]
271
+
272
+
273
+ def test_identity_powers():
274
+ M = Identity(n)
275
+ assert MatPow(M, 3).doit() == M**3
276
+ assert M**n == M
277
+ assert MatPow(M, 0).doit() == M**2
278
+ assert M**-2 == M
279
+ assert MatPow(M, -2).doit() == M**0
280
+ N = Identity(3)
281
+ assert MatPow(N, 2).doit() == N**n
282
+ assert MatPow(N, 3).doit() == N
283
+ assert MatPow(N, -2).doit() == N**4
284
+ assert MatPow(N, 2).doit() == N**0
285
+
286
+
287
+ def test_Zero_power():
288
+ z1 = ZeroMatrix(n, n)
289
+ assert z1**4 == z1
290
+ raises(ValueError, lambda:z1**-2)
291
+ assert z1**0 == Identity(n)
292
+ assert MatPow(z1, 2).doit() == z1**2
293
+ raises(ValueError, lambda:MatPow(z1, -2).doit())
294
+ z2 = ZeroMatrix(3, 3)
295
+ assert MatPow(z2, 4).doit() == z2**4
296
+ raises(ValueError, lambda:z2**-3)
297
+ assert z2**3 == MatPow(z2, 3).doit()
298
+ assert z2**0 == Identity(3)
299
+ raises(ValueError, lambda:MatPow(z2, -1).doit())
300
+
301
+
302
+ def test_matrixelement_diff():
303
+ dexpr = diff((D*w)[k,0], w[p,0])
304
+
305
+ assert w[k, p].diff(w[k, p]) == 1
306
+ assert w[k, p].diff(w[0, 0]) == KroneckerDelta(0, k, (0, n-1))*KroneckerDelta(0, p, (0, 0))
307
+ _i_1 = Dummy("_i_1")
308
+ assert dexpr.dummy_eq(Sum(KroneckerDelta(_i_1, p, (0, n-1))*D[k, _i_1], (_i_1, 0, n - 1)))
309
+ assert dexpr.doit() == D[k, p]
310
+
311
+
312
+ def test_MatrixElement_with_values():
313
+ x, y, z, w = symbols("x y z w")
314
+ M = Matrix([[x, y], [z, w]])
315
+ i, j = symbols("i, j")
316
+ Mij = M[i, j]
317
+ assert isinstance(Mij, MatrixElement)
318
+ Ms = SparseMatrix([[2, 3], [4, 5]])
319
+ msij = Ms[i, j]
320
+ assert isinstance(msij, MatrixElement)
321
+ for oi, oj in [(0, 0), (0, 1), (1, 0), (1, 1)]:
322
+ assert Mij.subs({i: oi, j: oj}) == M[oi, oj]
323
+ assert msij.subs({i: oi, j: oj}) == Ms[oi, oj]
324
+ A = MatrixSymbol("A", 2, 2)
325
+ assert A[0, 0].subs(A, M) == x
326
+ assert A[i, j].subs(A, M) == M[i, j]
327
+ assert M[i, j].subs(M, A) == A[i, j]
328
+
329
+ assert isinstance(M[3*i - 2, j], MatrixElement)
330
+ assert M[3*i - 2, j].subs({i: 1, j: 0}) == M[1, 0]
331
+ assert isinstance(M[i, 0], MatrixElement)
332
+ assert M[i, 0].subs(i, 0) == M[0, 0]
333
+ assert M[0, i].subs(i, 1) == M[0, 1]
334
+
335
+ assert M[i, j].diff(x) == Matrix([[1, 0], [0, 0]])[i, j]
336
+
337
+ raises(ValueError, lambda: M[i, 2])
338
+ raises(ValueError, lambda: M[i, -1])
339
+ raises(ValueError, lambda: M[2, i])
340
+ raises(ValueError, lambda: M[-1, i])
341
+
342
+
343
+ def test_inv():
344
+ B = MatrixSymbol('B', 3, 3)
345
+ assert B.inv() == B**-1
346
+
347
+ # https://github.com/sympy/sympy/issues/19162
348
+ X = MatrixSymbol('X', 1, 1).as_explicit()
349
+ assert X.inv() == Matrix([[1/X[0, 0]]])
350
+
351
+ X = MatrixSymbol('X', 2, 2).as_explicit()
352
+ detX = X[0, 0]*X[1, 1] - X[0, 1]*X[1, 0]
353
+ invX = Matrix([[ X[1, 1], -X[0, 1]],
354
+ [-X[1, 0], X[0, 0]]]) / detX
355
+ assert X.inv() == invX
356
+
357
+
358
+ @XFAIL
359
+ def test_factor_expand():
360
+ A = MatrixSymbol("A", n, n)
361
+ B = MatrixSymbol("B", n, n)
362
+ expr1 = (A + B)*(C + D)
363
+ expr2 = A*C + B*C + A*D + B*D
364
+ assert expr1 != expr2
365
+ assert expand(expr1) == expr2
366
+ assert factor(expr2) == expr1
367
+
368
+ expr = B**(-1)*(A**(-1)*B**(-1) - A**(-1)*C*B**(-1))**(-1)*A**(-1)
369
+ I = Identity(n)
370
+ # Ideally we get the first, but we at least don't want a wrong answer
371
+ assert factor(expr) in [I - C, B**-1*(A**-1*(I - C)*B**-1)**-1*A**-1]
372
+
373
+
374
+ def test_issue_2749():
375
+ A = MatrixSymbol("A", 5, 2)
376
+ assert (A.T * A).I.as_explicit() == Matrix([[(A.T * A).I[0, 0], (A.T * A).I[0, 1]], \
377
+ [(A.T * A).I[1, 0], (A.T * A).I[1, 1]]])
378
+
379
+
380
+ def test_issue_2750():
381
+ x = MatrixSymbol('x', 1, 1)
382
+ assert (x.T*x).as_explicit()**-1 == Matrix([[x[0, 0]**(-2)]])
383
+
384
+
385
+ def test_issue_7842():
386
+ A = MatrixSymbol('A', 3, 1)
387
+ B = MatrixSymbol('B', 2, 1)
388
+ assert Eq(A, B) == False
389
+ assert Eq(A[1,0], B[1, 0]).func is Eq
390
+ A = ZeroMatrix(2, 3)
391
+ B = ZeroMatrix(2, 3)
392
+ assert Eq(A, B) == True
393
+
394
+
395
+ def test_issue_21195():
396
+ t = symbols('t')
397
+ x = Function('x')(t)
398
+ dx = x.diff(t)
399
+ exp1 = cos(x) + cos(x)*dx
400
+ exp2 = sin(x) + tan(x)*(dx.diff(t))
401
+ exp3 = sin(x)*sin(t)*(dx.diff(t)).diff(t)
402
+ A = Matrix([[exp1], [exp2], [exp3]])
403
+ B = Matrix([[exp1.diff(x)], [exp2.diff(x)], [exp3.diff(x)]])
404
+ assert A.diff(x) == B
405
+
406
+
407
+ def test_MatMul_postprocessor():
408
+ z = zeros(2)
409
+ z1 = ZeroMatrix(2, 2)
410
+ assert Mul(0, z) == Mul(z, 0) in [z, z1]
411
+
412
+ M = Matrix([[1, 2], [3, 4]])
413
+ Mx = Matrix([[x, 2*x], [3*x, 4*x]])
414
+ assert Mul(x, M) == Mul(M, x) == Mx
415
+
416
+ A = MatrixSymbol("A", 2, 2)
417
+ assert Mul(A, M) == MatMul(A, M)
418
+ assert Mul(M, A) == MatMul(M, A)
419
+ # Scalars should be absorbed into constant matrices
420
+ a = Mul(x, M, A)
421
+ b = Mul(M, x, A)
422
+ c = Mul(M, A, x)
423
+ assert a == b == c == MatMul(Mx, A)
424
+ a = Mul(x, A, M)
425
+ b = Mul(A, x, M)
426
+ c = Mul(A, M, x)
427
+ assert a == b == c == MatMul(A, Mx)
428
+ assert Mul(M, M) == M**2
429
+ assert Mul(A, M, M) == MatMul(A, M**2)
430
+ assert Mul(M, M, A) == MatMul(M**2, A)
431
+ assert Mul(M, A, M) == MatMul(M, A, M)
432
+
433
+ assert Mul(A, x, M, M, x) == MatMul(A, Mx**2)
434
+
435
+
436
+ @XFAIL
437
+ def test_MatAdd_postprocessor_xfail():
438
+ # This is difficult to get working because of the way that Add processes
439
+ # its args.
440
+ z = zeros(2)
441
+ assert Add(z, S.NaN) == Add(S.NaN, z)
442
+
443
+
444
+ def test_MatAdd_postprocessor():
445
+ # Some of these are nonsensical, but we do not raise errors for Add
446
+ # because that breaks algorithms that want to replace matrices with dummy
447
+ # symbols.
448
+
449
+ z = zeros(2)
450
+
451
+ assert Add(0, z) == Add(z, 0) == z
452
+
453
+ a = Add(S.Infinity, z)
454
+ assert a == Add(z, S.Infinity)
455
+ assert isinstance(a, Add)
456
+ assert a.args == (S.Infinity, z)
457
+
458
+ a = Add(S.ComplexInfinity, z)
459
+ assert a == Add(z, S.ComplexInfinity)
460
+ assert isinstance(a, Add)
461
+ assert a.args == (S.ComplexInfinity, z)
462
+
463
+ a = Add(z, S.NaN)
464
+ # assert a == Add(S.NaN, z) # See the XFAIL above
465
+ assert isinstance(a, Add)
466
+ assert a.args == (S.NaN, z)
467
+
468
+ M = Matrix([[1, 2], [3, 4]])
469
+ a = Add(x, M)
470
+ assert a == Add(M, x)
471
+ assert isinstance(a, Add)
472
+ assert a.args == (x, M)
473
+
474
+ A = MatrixSymbol("A", 2, 2)
475
+ assert Add(A, M) == Add(M, A) == A + M
476
+
477
+ # Scalars should be absorbed into constant matrices (producing an error)
478
+ a = Add(x, M, A)
479
+ assert a == Add(M, x, A) == Add(M, A, x) == Add(x, A, M) == Add(A, x, M) == Add(A, M, x)
480
+ assert isinstance(a, Add)
481
+ assert a.args == (x, A + M)
482
+
483
+ assert Add(M, M) == 2*M
484
+ assert Add(M, A, M) == Add(M, M, A) == Add(A, M, M) == A + 2*M
485
+
486
+ a = Add(A, x, M, M, x)
487
+ assert isinstance(a, Add)
488
+ assert a.args == (2*x, A + 2*M)
489
+
490
+
491
+ def test_simplify_matrix_expressions():
492
+ # Various simplification functions
493
+ assert type(gcd_terms(C*D + D*C)) == MatAdd
494
+ a = gcd_terms(2*C*D + 4*D*C)
495
+ assert type(a) == MatAdd
496
+ assert a.args == (2*C*D, 4*D*C)
497
+
498
+
499
+ def test_exp():
500
+ A = MatrixSymbol('A', 2, 2)
501
+ B = MatrixSymbol('B', 2, 2)
502
+ expr1 = exp(A)*exp(B)
503
+ expr2 = exp(B)*exp(A)
504
+ assert expr1 != expr2
505
+ assert expr1 - expr2 != 0
506
+ assert not isinstance(expr1, exp)
507
+ assert not isinstance(expr2, exp)
508
+
509
+
510
+ def test_invalid_args():
511
+ raises(SympifyError, lambda: MatrixSymbol(1, 2, 'A'))
512
+
513
+
514
+ def test_matrixsymbol_from_symbol():
515
+ # The label should be preserved during doit and subs
516
+ A_label = Symbol('A', complex=True)
517
+ A = MatrixSymbol(A_label, 2, 2)
518
+
519
+ A_1 = A.doit()
520
+ A_2 = A.subs(2, 3)
521
+ assert A_1.args == A.args
522
+ assert A_2.args[0] == A.args[0]
523
+
524
+
525
+ def test_as_explicit():
526
+ Z = MatrixSymbol('Z', 2, 3)
527
+ assert Z.as_explicit() == ImmutableMatrix([
528
+ [Z[0, 0], Z[0, 1], Z[0, 2]],
529
+ [Z[1, 0], Z[1, 1], Z[1, 2]],
530
+ ])
531
+ raises(ValueError, lambda: A.as_explicit())
532
+
533
+
534
+ def test_MatrixSet():
535
+ M = MatrixSet(2, 2, set=S.Reals)
536
+ assert M.shape == (2, 2)
537
+ assert M.set == S.Reals
538
+ X = Matrix([[1, 2], [3, 4]])
539
+ assert X in M
540
+ X = ZeroMatrix(2, 2)
541
+ assert X in M
542
+ raises(TypeError, lambda: A in M)
543
+ raises(TypeError, lambda: 1 in M)
544
+ M = MatrixSet(n, m, set=S.Reals)
545
+ assert A in M
546
+ raises(TypeError, lambda: C in M)
547
+ raises(TypeError, lambda: X in M)
548
+ M = MatrixSet(2, 2, set={1, 2, 3})
549
+ X = Matrix([[1, 2], [3, 4]])
550
+ Y = Matrix([[1, 2]])
551
+ assert (X in M) == S.false
552
+ assert (Y in M) == S.false
553
+ raises(ValueError, lambda: MatrixSet(2, -2, S.Reals))
554
+ raises(ValueError, lambda: MatrixSet(2.4, -1, S.Reals))
555
+ raises(TypeError, lambda: MatrixSet(2, 2, (1, 2, 3)))
556
+
557
+
558
+ def test_matrixsymbol_solving():
559
+ A = MatrixSymbol('A', 2, 2)
560
+ B = MatrixSymbol('B', 2, 2)
561
+ Z = ZeroMatrix(2, 2)
562
+ assert -(-A + B) - A + B == Z
563
+ assert (-(-A + B) - A + B).simplify() == Z
564
+ assert (-(-A + B) - A + B).expand() == Z
565
+ assert (-(-A + B) - A + B - Z).simplify() == Z
566
+ assert (-(-A + B) - A + B - Z).expand() == Z
567
+ assert (A*(A + B) + B*(A.T + B.T)).expand() == A**2 + A*B + B*A.T + B*B.T
env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/tests/test_matpow.py ADDED
@@ -0,0 +1,217 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.functions.elementary.miscellaneous import sqrt
2
+ from sympy.simplify.powsimp import powsimp
3
+ from sympy.testing.pytest import raises
4
+ from sympy.core.expr import unchanged
5
+ from sympy.core import symbols, S
6
+ from sympy.matrices import Identity, MatrixSymbol, ImmutableMatrix, ZeroMatrix, OneMatrix, Matrix
7
+ from sympy.matrices.common import NonSquareMatrixError
8
+ from sympy.matrices.expressions import MatPow, MatAdd, MatMul
9
+ from sympy.matrices.expressions.inverse import Inverse
10
+ from sympy.matrices.expressions.matexpr import MatrixElement
11
+
12
+ n, m, l, k = symbols('n m l k', integer=True)
13
+ A = MatrixSymbol('A', n, m)
14
+ B = MatrixSymbol('B', m, l)
15
+ C = MatrixSymbol('C', n, n)
16
+ D = MatrixSymbol('D', n, n)
17
+ E = MatrixSymbol('E', m, n)
18
+
19
+
20
+ def test_entry_matrix():
21
+ X = ImmutableMatrix([[1, 2], [3, 4]])
22
+ assert MatPow(X, 0)[0, 0] == 1
23
+ assert MatPow(X, 0)[0, 1] == 0
24
+ assert MatPow(X, 1)[0, 0] == 1
25
+ assert MatPow(X, 1)[0, 1] == 2
26
+ assert MatPow(X, 2)[0, 0] == 7
27
+
28
+
29
+ def test_entry_symbol():
30
+ from sympy.concrete import Sum
31
+ assert MatPow(C, 0)[0, 0] == 1
32
+ assert MatPow(C, 0)[0, 1] == 0
33
+ assert MatPow(C, 1)[0, 0] == C[0, 0]
34
+ assert isinstance(MatPow(C, 2)[0, 0], Sum)
35
+ assert isinstance(MatPow(C, n)[0, 0], MatrixElement)
36
+
37
+
38
+ def test_as_explicit_symbol():
39
+ X = MatrixSymbol('X', 2, 2)
40
+ assert MatPow(X, 0).as_explicit() == ImmutableMatrix(Identity(2))
41
+ assert MatPow(X, 1).as_explicit() == X.as_explicit()
42
+ assert MatPow(X, 2).as_explicit() == (X.as_explicit())**2
43
+ assert MatPow(X, n).as_explicit() == ImmutableMatrix([
44
+ [(X ** n)[0, 0], (X ** n)[0, 1]],
45
+ [(X ** n)[1, 0], (X ** n)[1, 1]],
46
+ ])
47
+
48
+ a = MatrixSymbol("a", 3, 1)
49
+ b = MatrixSymbol("b", 3, 1)
50
+ c = MatrixSymbol("c", 3, 1)
51
+
52
+ expr = (a.T*b)**S.Half
53
+ assert expr.as_explicit() == Matrix([[sqrt(a[0, 0]*b[0, 0] + a[1, 0]*b[1, 0] + a[2, 0]*b[2, 0])]])
54
+
55
+ expr = c*(a.T*b)**S.Half
56
+ m = sqrt(a[0, 0]*b[0, 0] + a[1, 0]*b[1, 0] + a[2, 0]*b[2, 0])
57
+ assert expr.as_explicit() == Matrix([[c[0, 0]*m], [c[1, 0]*m], [c[2, 0]*m]])
58
+
59
+ expr = (a*b.T)**S.Half
60
+ denom = sqrt(a[0, 0]*b[0, 0] + a[1, 0]*b[1, 0] + a[2, 0]*b[2, 0])
61
+ expected = (a*b.T).as_explicit()/denom
62
+ assert expr.as_explicit() == expected
63
+
64
+ expr = X**-1
65
+ det = X[0, 0]*X[1, 1] - X[1, 0]*X[0, 1]
66
+ expected = Matrix([[X[1, 1], -X[0, 1]], [-X[1, 0], X[0, 0]]])/det
67
+ assert expr.as_explicit() == expected
68
+
69
+ expr = X**m
70
+ assert expr.as_explicit() == X.as_explicit()**m
71
+
72
+
73
+ def test_as_explicit_matrix():
74
+ A = ImmutableMatrix([[1, 2], [3, 4]])
75
+ assert MatPow(A, 0).as_explicit() == ImmutableMatrix(Identity(2))
76
+ assert MatPow(A, 1).as_explicit() == A
77
+ assert MatPow(A, 2).as_explicit() == A**2
78
+ assert MatPow(A, -1).as_explicit() == A.inv()
79
+ assert MatPow(A, -2).as_explicit() == (A.inv())**2
80
+ # less expensive than testing on a 2x2
81
+ A = ImmutableMatrix([4])
82
+ assert MatPow(A, S.Half).as_explicit() == A**S.Half
83
+
84
+
85
+ def test_doit_symbol():
86
+ assert MatPow(C, 0).doit() == Identity(n)
87
+ assert MatPow(C, 1).doit() == C
88
+ assert MatPow(C, -1).doit() == C.I
89
+ for r in [2, S.Half, S.Pi, n]:
90
+ assert MatPow(C, r).doit() == MatPow(C, r)
91
+
92
+
93
+ def test_doit_matrix():
94
+ X = ImmutableMatrix([[1, 2], [3, 4]])
95
+ assert MatPow(X, 0).doit() == ImmutableMatrix(Identity(2))
96
+ assert MatPow(X, 1).doit() == X
97
+ assert MatPow(X, 2).doit() == X**2
98
+ assert MatPow(X, -1).doit() == X.inv()
99
+ assert MatPow(X, -2).doit() == (X.inv())**2
100
+ # less expensive than testing on a 2x2
101
+ assert MatPow(ImmutableMatrix([4]), S.Half).doit() == ImmutableMatrix([2])
102
+ X = ImmutableMatrix([[0, 2], [0, 4]]) # det() == 0
103
+ raises(ValueError, lambda: MatPow(X,-1).doit())
104
+ raises(ValueError, lambda: MatPow(X,-2).doit())
105
+
106
+
107
+ def test_nonsquare():
108
+ A = MatrixSymbol('A', 2, 3)
109
+ B = ImmutableMatrix([[1, 2, 3], [4, 5, 6]])
110
+ for r in [-1, 0, 1, 2, S.Half, S.Pi, n]:
111
+ raises(NonSquareMatrixError, lambda: MatPow(A, r))
112
+ raises(NonSquareMatrixError, lambda: MatPow(B, r))
113
+
114
+
115
+ def test_doit_equals_pow(): #17179
116
+ X = ImmutableMatrix ([[1,0],[0,1]])
117
+ assert MatPow(X, n).doit() == X**n == X
118
+
119
+
120
+ def test_doit_nested_MatrixExpr():
121
+ X = ImmutableMatrix([[1, 2], [3, 4]])
122
+ Y = ImmutableMatrix([[2, 3], [4, 5]])
123
+ assert MatPow(MatMul(X, Y), 2).doit() == (X*Y)**2
124
+ assert MatPow(MatAdd(X, Y), 2).doit() == (X + Y)**2
125
+
126
+
127
+ def test_identity_power():
128
+ k = Identity(n)
129
+ assert MatPow(k, 4).doit() == k
130
+ assert MatPow(k, n).doit() == k
131
+ assert MatPow(k, -3).doit() == k
132
+ assert MatPow(k, 0).doit() == k
133
+ l = Identity(3)
134
+ assert MatPow(l, n).doit() == l
135
+ assert MatPow(l, -1).doit() == l
136
+ assert MatPow(l, 0).doit() == l
137
+
138
+
139
+ def test_zero_power():
140
+ z1 = ZeroMatrix(n, n)
141
+ assert MatPow(z1, 3).doit() == z1
142
+ raises(ValueError, lambda:MatPow(z1, -1).doit())
143
+ assert MatPow(z1, 0).doit() == Identity(n)
144
+ assert MatPow(z1, n).doit() == z1
145
+ raises(ValueError, lambda:MatPow(z1, -2).doit())
146
+ z2 = ZeroMatrix(4, 4)
147
+ assert MatPow(z2, n).doit() == z2
148
+ raises(ValueError, lambda:MatPow(z2, -3).doit())
149
+ assert MatPow(z2, 2).doit() == z2
150
+ assert MatPow(z2, 0).doit() == Identity(4)
151
+ raises(ValueError, lambda:MatPow(z2, -1).doit())
152
+
153
+
154
+ def test_OneMatrix_power():
155
+ o = OneMatrix(3, 3)
156
+ assert o ** 0 == Identity(3)
157
+ assert o ** 1 == o
158
+ assert o * o == o ** 2 == 3 * o
159
+ assert o * o * o == o ** 3 == 9 * o
160
+
161
+ o = OneMatrix(n, n)
162
+ assert o * o == o ** 2 == n * o
163
+ # powsimp necessary as n ** (n - 2) * n does not produce n ** (n - 1)
164
+ assert powsimp(o ** (n - 1) * o) == o ** n == n ** (n - 1) * o
165
+
166
+
167
+ def test_transpose_power():
168
+ from sympy.matrices.expressions.transpose import Transpose as TP
169
+
170
+ assert (C*D).T**5 == ((C*D)**5).T == (D.T * C.T)**5
171
+ assert ((C*D).T**5).T == (C*D)**5
172
+
173
+ assert (C.T.I.T)**7 == C**-7
174
+ assert (C.T**l).T**k == C**(l*k)
175
+
176
+ assert ((E.T * A.T)**5).T == (A*E)**5
177
+ assert ((A*E).T**5).T**7 == (A*E)**35
178
+ assert TP(TP(C**2 * D**3)**5).doit() == (C**2 * D**3)**5
179
+
180
+ assert ((D*C)**-5).T**-5 == ((D*C)**25).T
181
+ assert (((D*C)**l).T**k).T == (D*C)**(l*k)
182
+
183
+
184
+ def test_Inverse():
185
+ assert Inverse(MatPow(C, 0)).doit() == Identity(n)
186
+ assert Inverse(MatPow(C, 1)).doit() == Inverse(C)
187
+ assert Inverse(MatPow(C, 2)).doit() == MatPow(C, -2)
188
+ assert Inverse(MatPow(C, -1)).doit() == C
189
+
190
+ assert MatPow(Inverse(C), 0).doit() == Identity(n)
191
+ assert MatPow(Inverse(C), 1).doit() == Inverse(C)
192
+ assert MatPow(Inverse(C), 2).doit() == MatPow(C, -2)
193
+ assert MatPow(Inverse(C), -1).doit() == C
194
+
195
+
196
+ def test_combine_powers():
197
+ assert (C ** 1) ** 1 == C
198
+ assert (C ** 2) ** 3 == MatPow(C, 6)
199
+ assert (C ** -2) ** -3 == MatPow(C, 6)
200
+ assert (C ** -1) ** -1 == C
201
+ assert (((C ** 2) ** 3) ** 4) ** 5 == MatPow(C, 120)
202
+ assert (C ** n) ** n == C ** (n ** 2)
203
+
204
+
205
+ def test_unchanged():
206
+ assert unchanged(MatPow, C, 0)
207
+ assert unchanged(MatPow, C, 1)
208
+ assert unchanged(MatPow, Inverse(C), -1)
209
+ assert unchanged(Inverse, MatPow(C, -1), -1)
210
+ assert unchanged(MatPow, MatPow(C, -1), -1)
211
+ assert unchanged(MatPow, MatPow(C, 1), 1)
212
+
213
+
214
+ def test_no_exponentiation():
215
+ # if this passes, Pow.as_numer_denom should recognize
216
+ # MatAdd as exponent
217
+ raises(NotImplementedError, lambda: 3**(-2*C))
env-llmeval/lib/python3.10/site-packages/sympy/matrices/expressions/tests/test_slice.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.matrices.expressions.slice import MatrixSlice
2
+ from sympy.matrices.expressions import MatrixSymbol
3
+ from sympy.abc import a, b, c, d, k, l, m, n
4
+ from sympy.testing.pytest import raises, XFAIL
5
+ from sympy.functions.elementary.integers import floor
6
+ from sympy.assumptions import assuming, Q
7
+
8
+
9
+ X = MatrixSymbol('X', n, m)
10
+ Y = MatrixSymbol('Y', m, k)
11
+
12
+ def test_shape():
13
+ B = MatrixSlice(X, (a, b), (c, d))
14
+ assert B.shape == (b - a, d - c)
15
+
16
+ def test_entry():
17
+ B = MatrixSlice(X, (a, b), (c, d))
18
+ assert B[0,0] == X[a, c]
19
+ assert B[k,l] == X[a+k, c+l]
20
+ raises(IndexError, lambda : MatrixSlice(X, 1, (2, 5))[1, 0])
21
+
22
+ assert X[1::2, :][1, 3] == X[1+2, 3]
23
+ assert X[:, 1::2][3, 1] == X[3, 1+2]
24
+
25
+ def test_on_diag():
26
+ assert not MatrixSlice(X, (a, b), (c, d)).on_diag
27
+ assert MatrixSlice(X, (a, b), (a, b)).on_diag
28
+
29
+ def test_inputs():
30
+ assert MatrixSlice(X, 1, (2, 5)) == MatrixSlice(X, (1, 2), (2, 5))
31
+ assert MatrixSlice(X, 1, (2, 5)).shape == (1, 3)
32
+
33
+ def test_slicing():
34
+ assert X[1:5, 2:4] == MatrixSlice(X, (1, 5), (2, 4))
35
+ assert X[1, 2:4] == MatrixSlice(X, 1, (2, 4))
36
+ assert X[1:5, :].shape == (4, X.shape[1])
37
+ assert X[:, 1:5].shape == (X.shape[0], 4)
38
+
39
+ assert X[::2, ::2].shape == (floor(n/2), floor(m/2))
40
+ assert X[2, :] == MatrixSlice(X, 2, (0, m))
41
+ assert X[k, :] == MatrixSlice(X, k, (0, m))
42
+
43
+ def test_exceptions():
44
+ X = MatrixSymbol('x', 10, 20)
45
+ raises(IndexError, lambda: X[0:12, 2])
46
+ raises(IndexError, lambda: X[0:9, 22])
47
+ raises(IndexError, lambda: X[-1:5, 2])
48
+
49
+ @XFAIL
50
+ def test_symmetry():
51
+ X = MatrixSymbol('x', 10, 10)
52
+ Y = X[:5, 5:]
53
+ with assuming(Q.symmetric(X)):
54
+ assert Y.T == X[5:, :5]
55
+
56
+ def test_slice_of_slice():
57
+ X = MatrixSymbol('x', 10, 10)
58
+ assert X[2, :][:, 3][0, 0] == X[2, 3]
59
+ assert X[:5, :5][:4, :4] == X[:4, :4]
60
+ assert X[1:5, 2:6][1:3, 2] == X[2:4, 4]
61
+ assert X[1:9:2, 2:6][1:3, 2] == X[3:7:2, 4]
62
+
63
+ def test_negative_index():
64
+ X = MatrixSymbol('x', 10, 10)
65
+ assert X[-1, :] == X[9, :]