Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- .gitattributes +1 -0
- evalkit_tf437/lib/python3.10/site-packages/pyarrow/libarrow_python.so +3 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/benchmarks/__pycache__/bench_matrix.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/__init__.py +62 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/__pycache__/_shape.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/__pycache__/applyfunc.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/__pycache__/dotproduct.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/__pycache__/factorizations.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/__pycache__/funcmatrix.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/__pycache__/kronecker.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/__pycache__/sets.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/__pycache__/slice.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/__pycache__/special.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/blockmatrix.py +980 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/companion.py +56 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/determinant.py +148 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/dotproduct.py +55 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/factorizations.py +62 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/funcmatrix.py +118 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/hadamard.py +464 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/kronecker.py +434 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/matadd.py +155 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/matexpr.py +888 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/permutation.py +303 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/sets.py +68 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/slice.py +114 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/special.py +299 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__init__.py +0 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_adjoint.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_applyfunc.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_blockmatrix.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_companion.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_derivatives.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_determinant.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_dotproduct.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_factorizations.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_funcmatrix.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_hadamard.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_indexing.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_inverse.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_kronecker.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_matadd.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_matmul.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_matpow.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_permutation.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_sets.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_slice.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_special.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_trace.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_transpose.cpython-310.pyc +0 -0
.gitattributes
CHANGED
|
@@ -550,3 +550,4 @@ falcon/lib/python3.10/site-packages/sympy/printing/tests/__pycache__/test_latex.
|
|
| 550 |
falcon/lib/python3.10/site-packages/sympy/solvers/tests/__pycache__/test_solveset.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
| 551 |
falcon/lib/python3.10/site-packages/sympy/solvers/tests/__pycache__/test_solvers.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
| 552 |
evalkit_tf437/lib/python3.10/site-packages/transformers/__pycache__/modeling_utils.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 550 |
falcon/lib/python3.10/site-packages/sympy/solvers/tests/__pycache__/test_solveset.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
| 551 |
falcon/lib/python3.10/site-packages/sympy/solvers/tests/__pycache__/test_solvers.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
| 552 |
evalkit_tf437/lib/python3.10/site-packages/transformers/__pycache__/modeling_utils.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
| 553 |
+
evalkit_tf437/lib/python3.10/site-packages/pyarrow/libarrow_python.so filter=lfs diff=lfs merge=lfs -text
|
evalkit_tf437/lib/python3.10/site-packages/pyarrow/libarrow_python.so
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:38fe206bd662faad0fa1b974536c83019976f1f3b599b100c3b482a325105172
|
| 3 |
+
size 2872424
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/benchmarks/__pycache__/bench_matrix.cpython-310.pyc
ADDED
|
Binary file (892 Bytes). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/__init__.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
""" A module which handles Matrix Expressions """
|
| 2 |
+
|
| 3 |
+
from .slice import MatrixSlice
|
| 4 |
+
from .blockmatrix import BlockMatrix, BlockDiagMatrix, block_collapse, blockcut
|
| 5 |
+
from .companion import CompanionMatrix
|
| 6 |
+
from .funcmatrix import FunctionMatrix
|
| 7 |
+
from .inverse import Inverse
|
| 8 |
+
from .matadd import MatAdd
|
| 9 |
+
from .matexpr import MatrixExpr, MatrixSymbol, matrix_symbols
|
| 10 |
+
from .matmul import MatMul
|
| 11 |
+
from .matpow import MatPow
|
| 12 |
+
from .trace import Trace, trace
|
| 13 |
+
from .determinant import Determinant, det, Permanent, per
|
| 14 |
+
from .transpose import Transpose
|
| 15 |
+
from .adjoint import Adjoint
|
| 16 |
+
from .hadamard import hadamard_product, HadamardProduct, hadamard_power, HadamardPower
|
| 17 |
+
from .diagonal import DiagonalMatrix, DiagonalOf, DiagMatrix, diagonalize_vector
|
| 18 |
+
from .dotproduct import DotProduct
|
| 19 |
+
from .kronecker import kronecker_product, KroneckerProduct, combine_kronecker
|
| 20 |
+
from .permutation import PermutationMatrix, MatrixPermute
|
| 21 |
+
from .sets import MatrixSet
|
| 22 |
+
from .special import ZeroMatrix, Identity, OneMatrix
|
| 23 |
+
|
| 24 |
+
__all__ = [
|
| 25 |
+
'MatrixSlice',
|
| 26 |
+
|
| 27 |
+
'BlockMatrix', 'BlockDiagMatrix', 'block_collapse', 'blockcut',
|
| 28 |
+
'FunctionMatrix',
|
| 29 |
+
|
| 30 |
+
'CompanionMatrix',
|
| 31 |
+
|
| 32 |
+
'Inverse',
|
| 33 |
+
|
| 34 |
+
'MatAdd',
|
| 35 |
+
|
| 36 |
+
'Identity', 'MatrixExpr', 'MatrixSymbol', 'ZeroMatrix', 'OneMatrix',
|
| 37 |
+
'matrix_symbols', 'MatrixSet',
|
| 38 |
+
|
| 39 |
+
'MatMul',
|
| 40 |
+
|
| 41 |
+
'MatPow',
|
| 42 |
+
|
| 43 |
+
'Trace', 'trace',
|
| 44 |
+
|
| 45 |
+
'Determinant', 'det',
|
| 46 |
+
|
| 47 |
+
'Transpose',
|
| 48 |
+
|
| 49 |
+
'Adjoint',
|
| 50 |
+
|
| 51 |
+
'hadamard_product', 'HadamardProduct', 'hadamard_power', 'HadamardPower',
|
| 52 |
+
|
| 53 |
+
'DiagonalMatrix', 'DiagonalOf', 'DiagMatrix', 'diagonalize_vector',
|
| 54 |
+
|
| 55 |
+
'DotProduct',
|
| 56 |
+
|
| 57 |
+
'kronecker_product', 'KroneckerProduct', 'combine_kronecker',
|
| 58 |
+
|
| 59 |
+
'PermutationMatrix', 'MatrixPermute',
|
| 60 |
+
|
| 61 |
+
'Permanent', 'per'
|
| 62 |
+
]
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/__pycache__/_shape.cpython-310.pyc
ADDED
|
Binary file (4.25 kB). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/__pycache__/applyfunc.cpython-310.pyc
ADDED
|
Binary file (5.52 kB). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/__pycache__/dotproduct.cpython-310.pyc
ADDED
|
Binary file (2.03 kB). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/__pycache__/factorizations.cpython-310.pyc
ADDED
|
Binary file (3.39 kB). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/__pycache__/funcmatrix.cpython-310.pyc
ADDED
|
Binary file (4.24 kB). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/__pycache__/kronecker.cpython-310.pyc
ADDED
|
Binary file (16.4 kB). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/__pycache__/sets.cpython-310.pyc
ADDED
|
Binary file (2.83 kB). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/__pycache__/slice.cpython-310.pyc
ADDED
|
Binary file (3.55 kB). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/__pycache__/special.cpython-310.pyc
ADDED
|
Binary file (10.7 kB). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/blockmatrix.py
ADDED
|
@@ -0,0 +1,980 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.assumptions.ask import (Q, ask)
|
| 2 |
+
from sympy.core import Basic, Add, Mul, S
|
| 3 |
+
from sympy.core.sympify import _sympify
|
| 4 |
+
from sympy.functions import adjoint
|
| 5 |
+
from sympy.functions.elementary.complexes import re, im
|
| 6 |
+
from sympy.strategies import typed, exhaust, condition, do_one, unpack
|
| 7 |
+
from sympy.strategies.traverse import bottom_up
|
| 8 |
+
from sympy.utilities.iterables import is_sequence, sift
|
| 9 |
+
from sympy.utilities.misc import filldedent
|
| 10 |
+
|
| 11 |
+
from sympy.matrices import Matrix, ShapeError
|
| 12 |
+
from sympy.matrices.exceptions import NonInvertibleMatrixError
|
| 13 |
+
from sympy.matrices.expressions.determinant import det, Determinant
|
| 14 |
+
from sympy.matrices.expressions.inverse import Inverse
|
| 15 |
+
from sympy.matrices.expressions.matadd import MatAdd
|
| 16 |
+
from sympy.matrices.expressions.matexpr import MatrixExpr, MatrixElement
|
| 17 |
+
from sympy.matrices.expressions.matmul import MatMul
|
| 18 |
+
from sympy.matrices.expressions.matpow import MatPow
|
| 19 |
+
from sympy.matrices.expressions.slice import MatrixSlice
|
| 20 |
+
from sympy.matrices.expressions.special import ZeroMatrix, Identity
|
| 21 |
+
from sympy.matrices.expressions.trace import trace
|
| 22 |
+
from sympy.matrices.expressions.transpose import Transpose, transpose
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
class BlockMatrix(MatrixExpr):
|
| 26 |
+
"""A BlockMatrix is a Matrix comprised of other matrices.
|
| 27 |
+
|
| 28 |
+
The submatrices are stored in a SymPy Matrix object but accessed as part of
|
| 29 |
+
a Matrix Expression
|
| 30 |
+
|
| 31 |
+
>>> from sympy import (MatrixSymbol, BlockMatrix, symbols,
|
| 32 |
+
... Identity, ZeroMatrix, block_collapse)
|
| 33 |
+
>>> n,m,l = symbols('n m l')
|
| 34 |
+
>>> X = MatrixSymbol('X', n, n)
|
| 35 |
+
>>> Y = MatrixSymbol('Y', m, m)
|
| 36 |
+
>>> Z = MatrixSymbol('Z', n, m)
|
| 37 |
+
>>> B = BlockMatrix([[X, Z], [ZeroMatrix(m,n), Y]])
|
| 38 |
+
>>> print(B)
|
| 39 |
+
Matrix([
|
| 40 |
+
[X, Z],
|
| 41 |
+
[0, Y]])
|
| 42 |
+
|
| 43 |
+
>>> C = BlockMatrix([[Identity(n), Z]])
|
| 44 |
+
>>> print(C)
|
| 45 |
+
Matrix([[I, Z]])
|
| 46 |
+
|
| 47 |
+
>>> print(block_collapse(C*B))
|
| 48 |
+
Matrix([[X, Z + Z*Y]])
|
| 49 |
+
|
| 50 |
+
Some matrices might be comprised of rows of blocks with
|
| 51 |
+
the matrices in each row having the same height and the
|
| 52 |
+
rows all having the same total number of columns but
|
| 53 |
+
not having the same number of columns for each matrix
|
| 54 |
+
in each row. In this case, the matrix is not a block
|
| 55 |
+
matrix and should be instantiated by Matrix.
|
| 56 |
+
|
| 57 |
+
>>> from sympy import ones, Matrix
|
| 58 |
+
>>> dat = [
|
| 59 |
+
... [ones(3,2), ones(3,3)*2],
|
| 60 |
+
... [ones(2,3)*3, ones(2,2)*4]]
|
| 61 |
+
...
|
| 62 |
+
>>> BlockMatrix(dat)
|
| 63 |
+
Traceback (most recent call last):
|
| 64 |
+
...
|
| 65 |
+
ValueError:
|
| 66 |
+
Although this matrix is comprised of blocks, the blocks do not fill
|
| 67 |
+
the matrix in a size-symmetric fashion. To create a full matrix from
|
| 68 |
+
these arguments, pass them directly to Matrix.
|
| 69 |
+
>>> Matrix(dat)
|
| 70 |
+
Matrix([
|
| 71 |
+
[1, 1, 2, 2, 2],
|
| 72 |
+
[1, 1, 2, 2, 2],
|
| 73 |
+
[1, 1, 2, 2, 2],
|
| 74 |
+
[3, 3, 3, 4, 4],
|
| 75 |
+
[3, 3, 3, 4, 4]])
|
| 76 |
+
|
| 77 |
+
See Also
|
| 78 |
+
========
|
| 79 |
+
sympy.matrices.matrixbase.MatrixBase.irregular
|
| 80 |
+
"""
|
| 81 |
+
def __new__(cls, *args, **kwargs):
|
| 82 |
+
from sympy.matrices.immutable import ImmutableDenseMatrix
|
| 83 |
+
isMat = lambda i: getattr(i, 'is_Matrix', False)
|
| 84 |
+
if len(args) != 1 or \
|
| 85 |
+
not is_sequence(args[0]) or \
|
| 86 |
+
len({isMat(r) for r in args[0]}) != 1:
|
| 87 |
+
raise ValueError(filldedent('''
|
| 88 |
+
expecting a sequence of 1 or more rows
|
| 89 |
+
containing Matrices.'''))
|
| 90 |
+
rows = args[0] if args else []
|
| 91 |
+
if not isMat(rows):
|
| 92 |
+
if rows and isMat(rows[0]):
|
| 93 |
+
rows = [rows] # rows is not list of lists or []
|
| 94 |
+
# regularity check
|
| 95 |
+
# same number of matrices in each row
|
| 96 |
+
blocky = ok = len({len(r) for r in rows}) == 1
|
| 97 |
+
if ok:
|
| 98 |
+
# same number of rows for each matrix in a row
|
| 99 |
+
for r in rows:
|
| 100 |
+
ok = len({i.rows for i in r}) == 1
|
| 101 |
+
if not ok:
|
| 102 |
+
break
|
| 103 |
+
blocky = ok
|
| 104 |
+
if ok:
|
| 105 |
+
# same number of cols for each matrix in each col
|
| 106 |
+
for c in range(len(rows[0])):
|
| 107 |
+
ok = len({rows[i][c].cols
|
| 108 |
+
for i in range(len(rows))}) == 1
|
| 109 |
+
if not ok:
|
| 110 |
+
break
|
| 111 |
+
if not ok:
|
| 112 |
+
# same total cols in each row
|
| 113 |
+
ok = len({
|
| 114 |
+
sum(i.cols for i in r) for r in rows}) == 1
|
| 115 |
+
if blocky and ok:
|
| 116 |
+
raise ValueError(filldedent('''
|
| 117 |
+
Although this matrix is comprised of blocks,
|
| 118 |
+
the blocks do not fill the matrix in a
|
| 119 |
+
size-symmetric fashion. To create a full matrix
|
| 120 |
+
from these arguments, pass them directly to
|
| 121 |
+
Matrix.'''))
|
| 122 |
+
raise ValueError(filldedent('''
|
| 123 |
+
When there are not the same number of rows in each
|
| 124 |
+
row's matrices or there are not the same number of
|
| 125 |
+
total columns in each row, the matrix is not a
|
| 126 |
+
block matrix. If this matrix is known to consist of
|
| 127 |
+
blocks fully filling a 2-D space then see
|
| 128 |
+
Matrix.irregular.'''))
|
| 129 |
+
mat = ImmutableDenseMatrix(rows, evaluate=False)
|
| 130 |
+
obj = Basic.__new__(cls, mat)
|
| 131 |
+
return obj
|
| 132 |
+
|
| 133 |
+
@property
|
| 134 |
+
def shape(self):
|
| 135 |
+
numrows = numcols = 0
|
| 136 |
+
M = self.blocks
|
| 137 |
+
for i in range(M.shape[0]):
|
| 138 |
+
numrows += M[i, 0].shape[0]
|
| 139 |
+
for i in range(M.shape[1]):
|
| 140 |
+
numcols += M[0, i].shape[1]
|
| 141 |
+
return (numrows, numcols)
|
| 142 |
+
|
| 143 |
+
@property
|
| 144 |
+
def blockshape(self):
|
| 145 |
+
return self.blocks.shape
|
| 146 |
+
|
| 147 |
+
@property
|
| 148 |
+
def blocks(self):
|
| 149 |
+
return self.args[0]
|
| 150 |
+
|
| 151 |
+
@property
|
| 152 |
+
def rowblocksizes(self):
|
| 153 |
+
return [self.blocks[i, 0].rows for i in range(self.blockshape[0])]
|
| 154 |
+
|
| 155 |
+
@property
|
| 156 |
+
def colblocksizes(self):
|
| 157 |
+
return [self.blocks[0, i].cols for i in range(self.blockshape[1])]
|
| 158 |
+
|
| 159 |
+
def structurally_equal(self, other):
|
| 160 |
+
return (isinstance(other, BlockMatrix)
|
| 161 |
+
and self.shape == other.shape
|
| 162 |
+
and self.blockshape == other.blockshape
|
| 163 |
+
and self.rowblocksizes == other.rowblocksizes
|
| 164 |
+
and self.colblocksizes == other.colblocksizes)
|
| 165 |
+
|
| 166 |
+
def _blockmul(self, other):
|
| 167 |
+
if (isinstance(other, BlockMatrix) and
|
| 168 |
+
self.colblocksizes == other.rowblocksizes):
|
| 169 |
+
return BlockMatrix(self.blocks*other.blocks)
|
| 170 |
+
|
| 171 |
+
return self * other
|
| 172 |
+
|
| 173 |
+
def _blockadd(self, other):
|
| 174 |
+
if (isinstance(other, BlockMatrix)
|
| 175 |
+
and self.structurally_equal(other)):
|
| 176 |
+
return BlockMatrix(self.blocks + other.blocks)
|
| 177 |
+
|
| 178 |
+
return self + other
|
| 179 |
+
|
| 180 |
+
def _eval_transpose(self):
|
| 181 |
+
# Flip all the individual matrices
|
| 182 |
+
matrices = [transpose(matrix) for matrix in self.blocks]
|
| 183 |
+
# Make a copy
|
| 184 |
+
M = Matrix(self.blockshape[0], self.blockshape[1], matrices)
|
| 185 |
+
# Transpose the block structure
|
| 186 |
+
M = M.transpose()
|
| 187 |
+
return BlockMatrix(M)
|
| 188 |
+
|
| 189 |
+
def _eval_adjoint(self):
|
| 190 |
+
# Adjoint all the individual matrices
|
| 191 |
+
matrices = [adjoint(matrix) for matrix in self.blocks]
|
| 192 |
+
# Make a copy
|
| 193 |
+
M = Matrix(self.blockshape[0], self.blockshape[1], matrices)
|
| 194 |
+
# Transpose the block structure
|
| 195 |
+
M = M.transpose()
|
| 196 |
+
return BlockMatrix(M)
|
| 197 |
+
|
| 198 |
+
def _eval_trace(self):
|
| 199 |
+
if self.rowblocksizes == self.colblocksizes:
|
| 200 |
+
blocks = [self.blocks[i, i] for i in range(self.blockshape[0])]
|
| 201 |
+
return Add(*[trace(block) for block in blocks])
|
| 202 |
+
|
| 203 |
+
def _eval_determinant(self):
|
| 204 |
+
if self.blockshape == (1, 1):
|
| 205 |
+
return det(self.blocks[0, 0])
|
| 206 |
+
if self.blockshape == (2, 2):
|
| 207 |
+
[[A, B],
|
| 208 |
+
[C, D]] = self.blocks.tolist()
|
| 209 |
+
if ask(Q.invertible(A)):
|
| 210 |
+
return det(A)*det(D - C*A.I*B)
|
| 211 |
+
elif ask(Q.invertible(D)):
|
| 212 |
+
return det(D)*det(A - B*D.I*C)
|
| 213 |
+
return Determinant(self)
|
| 214 |
+
|
| 215 |
+
def _eval_as_real_imag(self):
|
| 216 |
+
real_matrices = [re(matrix) for matrix in self.blocks]
|
| 217 |
+
real_matrices = Matrix(self.blockshape[0], self.blockshape[1], real_matrices)
|
| 218 |
+
|
| 219 |
+
im_matrices = [im(matrix) for matrix in self.blocks]
|
| 220 |
+
im_matrices = Matrix(self.blockshape[0], self.blockshape[1], im_matrices)
|
| 221 |
+
|
| 222 |
+
return (BlockMatrix(real_matrices), BlockMatrix(im_matrices))
|
| 223 |
+
|
| 224 |
+
def _eval_derivative(self, x):
|
| 225 |
+
return BlockMatrix(self.blocks.diff(x))
|
| 226 |
+
|
| 227 |
+
def transpose(self):
|
| 228 |
+
"""Return transpose of matrix.
|
| 229 |
+
|
| 230 |
+
Examples
|
| 231 |
+
========
|
| 232 |
+
|
| 233 |
+
>>> from sympy import MatrixSymbol, BlockMatrix, ZeroMatrix
|
| 234 |
+
>>> from sympy.abc import m, n
|
| 235 |
+
>>> X = MatrixSymbol('X', n, n)
|
| 236 |
+
>>> Y = MatrixSymbol('Y', m, m)
|
| 237 |
+
>>> Z = MatrixSymbol('Z', n, m)
|
| 238 |
+
>>> B = BlockMatrix([[X, Z], [ZeroMatrix(m,n), Y]])
|
| 239 |
+
>>> B.transpose()
|
| 240 |
+
Matrix([
|
| 241 |
+
[X.T, 0],
|
| 242 |
+
[Z.T, Y.T]])
|
| 243 |
+
>>> _.transpose()
|
| 244 |
+
Matrix([
|
| 245 |
+
[X, Z],
|
| 246 |
+
[0, Y]])
|
| 247 |
+
"""
|
| 248 |
+
return self._eval_transpose()
|
| 249 |
+
|
| 250 |
+
def schur(self, mat = 'A', generalized = False):
|
| 251 |
+
"""Return the Schur Complement of the 2x2 BlockMatrix
|
| 252 |
+
|
| 253 |
+
Parameters
|
| 254 |
+
==========
|
| 255 |
+
|
| 256 |
+
mat : String, optional
|
| 257 |
+
The matrix with respect to which the
|
| 258 |
+
Schur Complement is calculated. 'A' is
|
| 259 |
+
used by default
|
| 260 |
+
|
| 261 |
+
generalized : bool, optional
|
| 262 |
+
If True, returns the generalized Schur
|
| 263 |
+
Component which uses Moore-Penrose Inverse
|
| 264 |
+
|
| 265 |
+
Examples
|
| 266 |
+
========
|
| 267 |
+
|
| 268 |
+
>>> from sympy import symbols, MatrixSymbol, BlockMatrix
|
| 269 |
+
>>> m, n = symbols('m n')
|
| 270 |
+
>>> A = MatrixSymbol('A', n, n)
|
| 271 |
+
>>> B = MatrixSymbol('B', n, m)
|
| 272 |
+
>>> C = MatrixSymbol('C', m, n)
|
| 273 |
+
>>> D = MatrixSymbol('D', m, m)
|
| 274 |
+
>>> X = BlockMatrix([[A, B], [C, D]])
|
| 275 |
+
|
| 276 |
+
The default Schur Complement is evaluated with "A"
|
| 277 |
+
|
| 278 |
+
>>> X.schur()
|
| 279 |
+
-C*A**(-1)*B + D
|
| 280 |
+
>>> X.schur('D')
|
| 281 |
+
A - B*D**(-1)*C
|
| 282 |
+
|
| 283 |
+
Schur complement with non-invertible matrices is not
|
| 284 |
+
defined. Instead, the generalized Schur complement can
|
| 285 |
+
be calculated which uses the Moore-Penrose Inverse. To
|
| 286 |
+
achieve this, `generalized` must be set to `True`
|
| 287 |
+
|
| 288 |
+
>>> X.schur('B', generalized=True)
|
| 289 |
+
C - D*(B.T*B)**(-1)*B.T*A
|
| 290 |
+
>>> X.schur('C', generalized=True)
|
| 291 |
+
-A*(C.T*C)**(-1)*C.T*D + B
|
| 292 |
+
|
| 293 |
+
Returns
|
| 294 |
+
=======
|
| 295 |
+
|
| 296 |
+
M : Matrix
|
| 297 |
+
The Schur Complement Matrix
|
| 298 |
+
|
| 299 |
+
Raises
|
| 300 |
+
======
|
| 301 |
+
|
| 302 |
+
ShapeError
|
| 303 |
+
If the block matrix is not a 2x2 matrix
|
| 304 |
+
|
| 305 |
+
NonInvertibleMatrixError
|
| 306 |
+
If given matrix is non-invertible
|
| 307 |
+
|
| 308 |
+
References
|
| 309 |
+
==========
|
| 310 |
+
|
| 311 |
+
.. [1] Wikipedia Article on Schur Component : https://en.wikipedia.org/wiki/Schur_complement
|
| 312 |
+
|
| 313 |
+
See Also
|
| 314 |
+
========
|
| 315 |
+
|
| 316 |
+
sympy.matrices.matrixbase.MatrixBase.pinv
|
| 317 |
+
"""
|
| 318 |
+
|
| 319 |
+
if self.blockshape == (2, 2):
|
| 320 |
+
[[A, B],
|
| 321 |
+
[C, D]] = self.blocks.tolist()
|
| 322 |
+
d={'A' : A, 'B' : B, 'C' : C, 'D' : D}
|
| 323 |
+
try:
|
| 324 |
+
inv = (d[mat].T*d[mat]).inv()*d[mat].T if generalized else d[mat].inv()
|
| 325 |
+
if mat == 'A':
|
| 326 |
+
return D - C * inv * B
|
| 327 |
+
elif mat == 'B':
|
| 328 |
+
return C - D * inv * A
|
| 329 |
+
elif mat == 'C':
|
| 330 |
+
return B - A * inv * D
|
| 331 |
+
elif mat == 'D':
|
| 332 |
+
return A - B * inv * C
|
| 333 |
+
#For matrices where no sub-matrix is square
|
| 334 |
+
return self
|
| 335 |
+
except NonInvertibleMatrixError:
|
| 336 |
+
raise NonInvertibleMatrixError('The given matrix is not invertible. Please set generalized=True \
|
| 337 |
+
to compute the generalized Schur Complement which uses Moore-Penrose Inverse')
|
| 338 |
+
else:
|
| 339 |
+
raise ShapeError('Schur Complement can only be calculated for 2x2 block matrices')
|
| 340 |
+
|
| 341 |
+
def LDUdecomposition(self):
|
| 342 |
+
"""Returns the Block LDU decomposition of
|
| 343 |
+
a 2x2 Block Matrix
|
| 344 |
+
|
| 345 |
+
Returns
|
| 346 |
+
=======
|
| 347 |
+
|
| 348 |
+
(L, D, U) : Matrices
|
| 349 |
+
L : Lower Diagonal Matrix
|
| 350 |
+
D : Diagonal Matrix
|
| 351 |
+
U : Upper Diagonal Matrix
|
| 352 |
+
|
| 353 |
+
Examples
|
| 354 |
+
========
|
| 355 |
+
|
| 356 |
+
>>> from sympy import symbols, MatrixSymbol, BlockMatrix, block_collapse
|
| 357 |
+
>>> m, n = symbols('m n')
|
| 358 |
+
>>> A = MatrixSymbol('A', n, n)
|
| 359 |
+
>>> B = MatrixSymbol('B', n, m)
|
| 360 |
+
>>> C = MatrixSymbol('C', m, n)
|
| 361 |
+
>>> D = MatrixSymbol('D', m, m)
|
| 362 |
+
>>> X = BlockMatrix([[A, B], [C, D]])
|
| 363 |
+
>>> L, D, U = X.LDUdecomposition()
|
| 364 |
+
>>> block_collapse(L*D*U)
|
| 365 |
+
Matrix([
|
| 366 |
+
[A, B],
|
| 367 |
+
[C, D]])
|
| 368 |
+
|
| 369 |
+
Raises
|
| 370 |
+
======
|
| 371 |
+
|
| 372 |
+
ShapeError
|
| 373 |
+
If the block matrix is not a 2x2 matrix
|
| 374 |
+
|
| 375 |
+
NonInvertibleMatrixError
|
| 376 |
+
If the matrix "A" is non-invertible
|
| 377 |
+
|
| 378 |
+
See Also
|
| 379 |
+
========
|
| 380 |
+
sympy.matrices.expressions.blockmatrix.BlockMatrix.UDLdecomposition
|
| 381 |
+
sympy.matrices.expressions.blockmatrix.BlockMatrix.LUdecomposition
|
| 382 |
+
"""
|
| 383 |
+
if self.blockshape == (2,2):
|
| 384 |
+
[[A, B],
|
| 385 |
+
[C, D]] = self.blocks.tolist()
|
| 386 |
+
try:
|
| 387 |
+
AI = A.I
|
| 388 |
+
except NonInvertibleMatrixError:
|
| 389 |
+
raise NonInvertibleMatrixError('Block LDU decomposition cannot be calculated when\
|
| 390 |
+
"A" is singular')
|
| 391 |
+
Ip = Identity(B.shape[0])
|
| 392 |
+
Iq = Identity(B.shape[1])
|
| 393 |
+
Z = ZeroMatrix(*B.shape)
|
| 394 |
+
L = BlockMatrix([[Ip, Z], [C*AI, Iq]])
|
| 395 |
+
D = BlockDiagMatrix(A, self.schur())
|
| 396 |
+
U = BlockMatrix([[Ip, AI*B],[Z.T, Iq]])
|
| 397 |
+
return L, D, U
|
| 398 |
+
else:
|
| 399 |
+
raise ShapeError("Block LDU decomposition is supported only for 2x2 block matrices")
|
| 400 |
+
|
| 401 |
+
def UDLdecomposition(self):
|
| 402 |
+
"""Returns the Block UDL decomposition of
|
| 403 |
+
a 2x2 Block Matrix
|
| 404 |
+
|
| 405 |
+
Returns
|
| 406 |
+
=======
|
| 407 |
+
|
| 408 |
+
(U, D, L) : Matrices
|
| 409 |
+
U : Upper Diagonal Matrix
|
| 410 |
+
D : Diagonal Matrix
|
| 411 |
+
L : Lower Diagonal Matrix
|
| 412 |
+
|
| 413 |
+
Examples
|
| 414 |
+
========
|
| 415 |
+
|
| 416 |
+
>>> from sympy import symbols, MatrixSymbol, BlockMatrix, block_collapse
|
| 417 |
+
>>> m, n = symbols('m n')
|
| 418 |
+
>>> A = MatrixSymbol('A', n, n)
|
| 419 |
+
>>> B = MatrixSymbol('B', n, m)
|
| 420 |
+
>>> C = MatrixSymbol('C', m, n)
|
| 421 |
+
>>> D = MatrixSymbol('D', m, m)
|
| 422 |
+
>>> X = BlockMatrix([[A, B], [C, D]])
|
| 423 |
+
>>> U, D, L = X.UDLdecomposition()
|
| 424 |
+
>>> block_collapse(U*D*L)
|
| 425 |
+
Matrix([
|
| 426 |
+
[A, B],
|
| 427 |
+
[C, D]])
|
| 428 |
+
|
| 429 |
+
Raises
|
| 430 |
+
======
|
| 431 |
+
|
| 432 |
+
ShapeError
|
| 433 |
+
If the block matrix is not a 2x2 matrix
|
| 434 |
+
|
| 435 |
+
NonInvertibleMatrixError
|
| 436 |
+
If the matrix "D" is non-invertible
|
| 437 |
+
|
| 438 |
+
See Also
|
| 439 |
+
========
|
| 440 |
+
sympy.matrices.expressions.blockmatrix.BlockMatrix.LDUdecomposition
|
| 441 |
+
sympy.matrices.expressions.blockmatrix.BlockMatrix.LUdecomposition
|
| 442 |
+
"""
|
| 443 |
+
if self.blockshape == (2,2):
|
| 444 |
+
[[A, B],
|
| 445 |
+
[C, D]] = self.blocks.tolist()
|
| 446 |
+
try:
|
| 447 |
+
DI = D.I
|
| 448 |
+
except NonInvertibleMatrixError:
|
| 449 |
+
raise NonInvertibleMatrixError('Block UDL decomposition cannot be calculated when\
|
| 450 |
+
"D" is singular')
|
| 451 |
+
Ip = Identity(A.shape[0])
|
| 452 |
+
Iq = Identity(B.shape[1])
|
| 453 |
+
Z = ZeroMatrix(*B.shape)
|
| 454 |
+
U = BlockMatrix([[Ip, B*DI], [Z.T, Iq]])
|
| 455 |
+
D = BlockDiagMatrix(self.schur('D'), D)
|
| 456 |
+
L = BlockMatrix([[Ip, Z],[DI*C, Iq]])
|
| 457 |
+
return U, D, L
|
| 458 |
+
else:
|
| 459 |
+
raise ShapeError("Block UDL decomposition is supported only for 2x2 block matrices")
|
| 460 |
+
|
| 461 |
+
def LUdecomposition(self):
|
| 462 |
+
"""Returns the Block LU decomposition of
|
| 463 |
+
a 2x2 Block Matrix
|
| 464 |
+
|
| 465 |
+
Returns
|
| 466 |
+
=======
|
| 467 |
+
|
| 468 |
+
(L, U) : Matrices
|
| 469 |
+
L : Lower Diagonal Matrix
|
| 470 |
+
U : Upper Diagonal Matrix
|
| 471 |
+
|
| 472 |
+
Examples
|
| 473 |
+
========
|
| 474 |
+
|
| 475 |
+
>>> from sympy import symbols, MatrixSymbol, BlockMatrix, block_collapse
|
| 476 |
+
>>> m, n = symbols('m n')
|
| 477 |
+
>>> A = MatrixSymbol('A', n, n)
|
| 478 |
+
>>> B = MatrixSymbol('B', n, m)
|
| 479 |
+
>>> C = MatrixSymbol('C', m, n)
|
| 480 |
+
>>> D = MatrixSymbol('D', m, m)
|
| 481 |
+
>>> X = BlockMatrix([[A, B], [C, D]])
|
| 482 |
+
>>> L, U = X.LUdecomposition()
|
| 483 |
+
>>> block_collapse(L*U)
|
| 484 |
+
Matrix([
|
| 485 |
+
[A, B],
|
| 486 |
+
[C, D]])
|
| 487 |
+
|
| 488 |
+
Raises
|
| 489 |
+
======
|
| 490 |
+
|
| 491 |
+
ShapeError
|
| 492 |
+
If the block matrix is not a 2x2 matrix
|
| 493 |
+
|
| 494 |
+
NonInvertibleMatrixError
|
| 495 |
+
If the matrix "A" is non-invertible
|
| 496 |
+
|
| 497 |
+
See Also
|
| 498 |
+
========
|
| 499 |
+
sympy.matrices.expressions.blockmatrix.BlockMatrix.UDLdecomposition
|
| 500 |
+
sympy.matrices.expressions.blockmatrix.BlockMatrix.LDUdecomposition
|
| 501 |
+
"""
|
| 502 |
+
if self.blockshape == (2,2):
|
| 503 |
+
[[A, B],
|
| 504 |
+
[C, D]] = self.blocks.tolist()
|
| 505 |
+
try:
|
| 506 |
+
A = A**S.Half
|
| 507 |
+
AI = A.I
|
| 508 |
+
except NonInvertibleMatrixError:
|
| 509 |
+
raise NonInvertibleMatrixError('Block LU decomposition cannot be calculated when\
|
| 510 |
+
"A" is singular')
|
| 511 |
+
Z = ZeroMatrix(*B.shape)
|
| 512 |
+
Q = self.schur()**S.Half
|
| 513 |
+
L = BlockMatrix([[A, Z], [C*AI, Q]])
|
| 514 |
+
U = BlockMatrix([[A, AI*B],[Z.T, Q]])
|
| 515 |
+
return L, U
|
| 516 |
+
else:
|
| 517 |
+
raise ShapeError("Block LU decomposition is supported only for 2x2 block matrices")
|
| 518 |
+
|
| 519 |
+
def _entry(self, i, j, **kwargs):
|
| 520 |
+
# Find row entry
|
| 521 |
+
orig_i, orig_j = i, j
|
| 522 |
+
for row_block, numrows in enumerate(self.rowblocksizes):
|
| 523 |
+
cmp = i < numrows
|
| 524 |
+
if cmp == True:
|
| 525 |
+
break
|
| 526 |
+
elif cmp == False:
|
| 527 |
+
i -= numrows
|
| 528 |
+
elif row_block < self.blockshape[0] - 1:
|
| 529 |
+
# Can't tell which block and it's not the last one, return unevaluated
|
| 530 |
+
return MatrixElement(self, orig_i, orig_j)
|
| 531 |
+
for col_block, numcols in enumerate(self.colblocksizes):
|
| 532 |
+
cmp = j < numcols
|
| 533 |
+
if cmp == True:
|
| 534 |
+
break
|
| 535 |
+
elif cmp == False:
|
| 536 |
+
j -= numcols
|
| 537 |
+
elif col_block < self.blockshape[1] - 1:
|
| 538 |
+
return MatrixElement(self, orig_i, orig_j)
|
| 539 |
+
return self.blocks[row_block, col_block][i, j]
|
| 540 |
+
|
| 541 |
+
@property
|
| 542 |
+
def is_Identity(self):
|
| 543 |
+
if self.blockshape[0] != self.blockshape[1]:
|
| 544 |
+
return False
|
| 545 |
+
for i in range(self.blockshape[0]):
|
| 546 |
+
for j in range(self.blockshape[1]):
|
| 547 |
+
if i==j and not self.blocks[i, j].is_Identity:
|
| 548 |
+
return False
|
| 549 |
+
if i!=j and not self.blocks[i, j].is_ZeroMatrix:
|
| 550 |
+
return False
|
| 551 |
+
return True
|
| 552 |
+
|
| 553 |
+
@property
|
| 554 |
+
def is_structurally_symmetric(self):
|
| 555 |
+
return self.rowblocksizes == self.colblocksizes
|
| 556 |
+
|
| 557 |
+
def equals(self, other):
|
| 558 |
+
if self == other:
|
| 559 |
+
return True
|
| 560 |
+
if (isinstance(other, BlockMatrix) and self.blocks == other.blocks):
|
| 561 |
+
return True
|
| 562 |
+
return super().equals(other)
|
| 563 |
+
|
| 564 |
+
|
| 565 |
+
class BlockDiagMatrix(BlockMatrix):
|
| 566 |
+
"""A sparse matrix with block matrices along its diagonals
|
| 567 |
+
|
| 568 |
+
Examples
|
| 569 |
+
========
|
| 570 |
+
|
| 571 |
+
>>> from sympy import MatrixSymbol, BlockDiagMatrix, symbols
|
| 572 |
+
>>> n, m, l = symbols('n m l')
|
| 573 |
+
>>> X = MatrixSymbol('X', n, n)
|
| 574 |
+
>>> Y = MatrixSymbol('Y', m, m)
|
| 575 |
+
>>> BlockDiagMatrix(X, Y)
|
| 576 |
+
Matrix([
|
| 577 |
+
[X, 0],
|
| 578 |
+
[0, Y]])
|
| 579 |
+
|
| 580 |
+
Notes
|
| 581 |
+
=====
|
| 582 |
+
|
| 583 |
+
If you want to get the individual diagonal blocks, use
|
| 584 |
+
:meth:`get_diag_blocks`.
|
| 585 |
+
|
| 586 |
+
See Also
|
| 587 |
+
========
|
| 588 |
+
|
| 589 |
+
sympy.matrices.dense.diag
|
| 590 |
+
"""
|
| 591 |
+
def __new__(cls, *mats):
|
| 592 |
+
return Basic.__new__(BlockDiagMatrix, *[_sympify(m) for m in mats])
|
| 593 |
+
|
| 594 |
+
@property
|
| 595 |
+
def diag(self):
|
| 596 |
+
return self.args
|
| 597 |
+
|
| 598 |
+
@property
|
| 599 |
+
def blocks(self):
|
| 600 |
+
from sympy.matrices.immutable import ImmutableDenseMatrix
|
| 601 |
+
mats = self.args
|
| 602 |
+
data = [[mats[i] if i == j else ZeroMatrix(mats[i].rows, mats[j].cols)
|
| 603 |
+
for j in range(len(mats))]
|
| 604 |
+
for i in range(len(mats))]
|
| 605 |
+
return ImmutableDenseMatrix(data, evaluate=False)
|
| 606 |
+
|
| 607 |
+
@property
|
| 608 |
+
def shape(self):
|
| 609 |
+
return (sum(block.rows for block in self.args),
|
| 610 |
+
sum(block.cols for block in self.args))
|
| 611 |
+
|
| 612 |
+
@property
|
| 613 |
+
def blockshape(self):
|
| 614 |
+
n = len(self.args)
|
| 615 |
+
return (n, n)
|
| 616 |
+
|
| 617 |
+
@property
|
| 618 |
+
def rowblocksizes(self):
|
| 619 |
+
return [block.rows for block in self.args]
|
| 620 |
+
|
| 621 |
+
@property
|
| 622 |
+
def colblocksizes(self):
|
| 623 |
+
return [block.cols for block in self.args]
|
| 624 |
+
|
| 625 |
+
def _all_square_blocks(self):
|
| 626 |
+
"""Returns true if all blocks are square"""
|
| 627 |
+
return all(mat.is_square for mat in self.args)
|
| 628 |
+
|
| 629 |
+
def _eval_determinant(self):
|
| 630 |
+
if self._all_square_blocks():
|
| 631 |
+
return Mul(*[det(mat) for mat in self.args])
|
| 632 |
+
# At least one block is non-square. Since the entire matrix must be square we know there must
|
| 633 |
+
# be at least two blocks in this matrix, in which case the entire matrix is necessarily rank-deficient
|
| 634 |
+
return S.Zero
|
| 635 |
+
|
| 636 |
+
def _eval_inverse(self, expand='ignored'):
|
| 637 |
+
if self._all_square_blocks():
|
| 638 |
+
return BlockDiagMatrix(*[mat.inverse() for mat in self.args])
|
| 639 |
+
# See comment in _eval_determinant()
|
| 640 |
+
raise NonInvertibleMatrixError('Matrix det == 0; not invertible.')
|
| 641 |
+
|
| 642 |
+
def _eval_transpose(self):
|
| 643 |
+
return BlockDiagMatrix(*[mat.transpose() for mat in self.args])
|
| 644 |
+
|
| 645 |
+
def _blockmul(self, other):
|
| 646 |
+
if (isinstance(other, BlockDiagMatrix) and
|
| 647 |
+
self.colblocksizes == other.rowblocksizes):
|
| 648 |
+
return BlockDiagMatrix(*[a*b for a, b in zip(self.args, other.args)])
|
| 649 |
+
else:
|
| 650 |
+
return BlockMatrix._blockmul(self, other)
|
| 651 |
+
|
| 652 |
+
def _blockadd(self, other):
|
| 653 |
+
if (isinstance(other, BlockDiagMatrix) and
|
| 654 |
+
self.blockshape == other.blockshape and
|
| 655 |
+
self.rowblocksizes == other.rowblocksizes and
|
| 656 |
+
self.colblocksizes == other.colblocksizes):
|
| 657 |
+
return BlockDiagMatrix(*[a + b for a, b in zip(self.args, other.args)])
|
| 658 |
+
else:
|
| 659 |
+
return BlockMatrix._blockadd(self, other)
|
| 660 |
+
|
| 661 |
+
def get_diag_blocks(self):
|
| 662 |
+
"""Return the list of diagonal blocks of the matrix.
|
| 663 |
+
|
| 664 |
+
Examples
|
| 665 |
+
========
|
| 666 |
+
|
| 667 |
+
>>> from sympy import BlockDiagMatrix, Matrix
|
| 668 |
+
|
| 669 |
+
>>> A = Matrix([[1, 2], [3, 4]])
|
| 670 |
+
>>> B = Matrix([[5, 6], [7, 8]])
|
| 671 |
+
>>> M = BlockDiagMatrix(A, B)
|
| 672 |
+
|
| 673 |
+
How to get diagonal blocks from the block diagonal matrix:
|
| 674 |
+
|
| 675 |
+
>>> diag_blocks = M.get_diag_blocks()
|
| 676 |
+
>>> diag_blocks[0]
|
| 677 |
+
Matrix([
|
| 678 |
+
[1, 2],
|
| 679 |
+
[3, 4]])
|
| 680 |
+
>>> diag_blocks[1]
|
| 681 |
+
Matrix([
|
| 682 |
+
[5, 6],
|
| 683 |
+
[7, 8]])
|
| 684 |
+
"""
|
| 685 |
+
return self.args
|
| 686 |
+
|
| 687 |
+
|
| 688 |
+
def block_collapse(expr):
|
| 689 |
+
"""Evaluates a block matrix expression
|
| 690 |
+
|
| 691 |
+
>>> from sympy import MatrixSymbol, BlockMatrix, symbols, Identity, ZeroMatrix, block_collapse
|
| 692 |
+
>>> n,m,l = symbols('n m l')
|
| 693 |
+
>>> X = MatrixSymbol('X', n, n)
|
| 694 |
+
>>> Y = MatrixSymbol('Y', m, m)
|
| 695 |
+
>>> Z = MatrixSymbol('Z', n, m)
|
| 696 |
+
>>> B = BlockMatrix([[X, Z], [ZeroMatrix(m, n), Y]])
|
| 697 |
+
>>> print(B)
|
| 698 |
+
Matrix([
|
| 699 |
+
[X, Z],
|
| 700 |
+
[0, Y]])
|
| 701 |
+
|
| 702 |
+
>>> C = BlockMatrix([[Identity(n), Z]])
|
| 703 |
+
>>> print(C)
|
| 704 |
+
Matrix([[I, Z]])
|
| 705 |
+
|
| 706 |
+
>>> print(block_collapse(C*B))
|
| 707 |
+
Matrix([[X, Z + Z*Y]])
|
| 708 |
+
"""
|
| 709 |
+
from sympy.strategies.util import expr_fns
|
| 710 |
+
|
| 711 |
+
hasbm = lambda expr: isinstance(expr, MatrixExpr) and expr.has(BlockMatrix)
|
| 712 |
+
|
| 713 |
+
conditioned_rl = condition(
|
| 714 |
+
hasbm,
|
| 715 |
+
typed(
|
| 716 |
+
{MatAdd: do_one(bc_matadd, bc_block_plus_ident),
|
| 717 |
+
MatMul: do_one(bc_matmul, bc_dist),
|
| 718 |
+
MatPow: bc_matmul,
|
| 719 |
+
Transpose: bc_transpose,
|
| 720 |
+
Inverse: bc_inverse,
|
| 721 |
+
BlockMatrix: do_one(bc_unpack, deblock)}
|
| 722 |
+
)
|
| 723 |
+
)
|
| 724 |
+
|
| 725 |
+
rule = exhaust(
|
| 726 |
+
bottom_up(
|
| 727 |
+
exhaust(conditioned_rl),
|
| 728 |
+
fns=expr_fns
|
| 729 |
+
)
|
| 730 |
+
)
|
| 731 |
+
|
| 732 |
+
result = rule(expr)
|
| 733 |
+
doit = getattr(result, 'doit', None)
|
| 734 |
+
if doit is not None:
|
| 735 |
+
return doit()
|
| 736 |
+
else:
|
| 737 |
+
return result
|
| 738 |
+
|
| 739 |
+
def bc_unpack(expr):
|
| 740 |
+
if expr.blockshape == (1, 1):
|
| 741 |
+
return expr.blocks[0, 0]
|
| 742 |
+
return expr
|
| 743 |
+
|
| 744 |
+
def bc_matadd(expr):
|
| 745 |
+
args = sift(expr.args, lambda M: isinstance(M, BlockMatrix))
|
| 746 |
+
blocks = args[True]
|
| 747 |
+
if not blocks:
|
| 748 |
+
return expr
|
| 749 |
+
|
| 750 |
+
nonblocks = args[False]
|
| 751 |
+
block = blocks[0]
|
| 752 |
+
for b in blocks[1:]:
|
| 753 |
+
block = block._blockadd(b)
|
| 754 |
+
if nonblocks:
|
| 755 |
+
return MatAdd(*nonblocks) + block
|
| 756 |
+
else:
|
| 757 |
+
return block
|
| 758 |
+
|
| 759 |
+
def bc_block_plus_ident(expr):
|
| 760 |
+
idents = [arg for arg in expr.args if arg.is_Identity]
|
| 761 |
+
if not idents:
|
| 762 |
+
return expr
|
| 763 |
+
|
| 764 |
+
blocks = [arg for arg in expr.args if isinstance(arg, BlockMatrix)]
|
| 765 |
+
if (blocks and all(b.structurally_equal(blocks[0]) for b in blocks)
|
| 766 |
+
and blocks[0].is_structurally_symmetric):
|
| 767 |
+
block_id = BlockDiagMatrix(*[Identity(k)
|
| 768 |
+
for k in blocks[0].rowblocksizes])
|
| 769 |
+
rest = [arg for arg in expr.args if not arg.is_Identity and not isinstance(arg, BlockMatrix)]
|
| 770 |
+
return MatAdd(block_id * len(idents), *blocks, *rest).doit()
|
| 771 |
+
|
| 772 |
+
return expr
|
| 773 |
+
|
| 774 |
+
def bc_dist(expr):
|
| 775 |
+
""" Turn a*[X, Y] into [a*X, a*Y] """
|
| 776 |
+
factor, mat = expr.as_coeff_mmul()
|
| 777 |
+
if factor == 1:
|
| 778 |
+
return expr
|
| 779 |
+
|
| 780 |
+
unpacked = unpack(mat)
|
| 781 |
+
|
| 782 |
+
if isinstance(unpacked, BlockDiagMatrix):
|
| 783 |
+
B = unpacked.diag
|
| 784 |
+
new_B = [factor * mat for mat in B]
|
| 785 |
+
return BlockDiagMatrix(*new_B)
|
| 786 |
+
elif isinstance(unpacked, BlockMatrix):
|
| 787 |
+
B = unpacked.blocks
|
| 788 |
+
new_B = [
|
| 789 |
+
[factor * B[i, j] for j in range(B.cols)] for i in range(B.rows)]
|
| 790 |
+
return BlockMatrix(new_B)
|
| 791 |
+
return expr
|
| 792 |
+
|
| 793 |
+
|
| 794 |
+
def bc_matmul(expr):
|
| 795 |
+
if isinstance(expr, MatPow):
|
| 796 |
+
if expr.args[1].is_Integer and expr.args[1] > 0:
|
| 797 |
+
factor, matrices = 1, [expr.args[0]]*expr.args[1]
|
| 798 |
+
else:
|
| 799 |
+
return expr
|
| 800 |
+
else:
|
| 801 |
+
factor, matrices = expr.as_coeff_matrices()
|
| 802 |
+
|
| 803 |
+
i = 0
|
| 804 |
+
while (i+1 < len(matrices)):
|
| 805 |
+
A, B = matrices[i:i+2]
|
| 806 |
+
if isinstance(A, BlockMatrix) and isinstance(B, BlockMatrix):
|
| 807 |
+
matrices[i] = A._blockmul(B)
|
| 808 |
+
matrices.pop(i+1)
|
| 809 |
+
elif isinstance(A, BlockMatrix):
|
| 810 |
+
matrices[i] = A._blockmul(BlockMatrix([[B]]))
|
| 811 |
+
matrices.pop(i+1)
|
| 812 |
+
elif isinstance(B, BlockMatrix):
|
| 813 |
+
matrices[i] = BlockMatrix([[A]])._blockmul(B)
|
| 814 |
+
matrices.pop(i+1)
|
| 815 |
+
else:
|
| 816 |
+
i+=1
|
| 817 |
+
return MatMul(factor, *matrices).doit()
|
| 818 |
+
|
| 819 |
+
def bc_transpose(expr):
|
| 820 |
+
collapse = block_collapse(expr.arg)
|
| 821 |
+
return collapse._eval_transpose()
|
| 822 |
+
|
| 823 |
+
|
| 824 |
+
def bc_inverse(expr):
|
| 825 |
+
if isinstance(expr.arg, BlockDiagMatrix):
|
| 826 |
+
return expr.inverse()
|
| 827 |
+
|
| 828 |
+
expr2 = blockinverse_1x1(expr)
|
| 829 |
+
if expr != expr2:
|
| 830 |
+
return expr2
|
| 831 |
+
return blockinverse_2x2(Inverse(reblock_2x2(expr.arg)))
|
| 832 |
+
|
| 833 |
+
def blockinverse_1x1(expr):
|
| 834 |
+
if isinstance(expr.arg, BlockMatrix) and expr.arg.blockshape == (1, 1):
|
| 835 |
+
mat = Matrix([[expr.arg.blocks[0].inverse()]])
|
| 836 |
+
return BlockMatrix(mat)
|
| 837 |
+
return expr
|
| 838 |
+
|
| 839 |
+
|
| 840 |
+
def blockinverse_2x2(expr):
|
| 841 |
+
if isinstance(expr.arg, BlockMatrix) and expr.arg.blockshape == (2, 2):
|
| 842 |
+
# See: Inverses of 2x2 Block Matrices, Tzon-Tzer Lu and Sheng-Hua Shiou
|
| 843 |
+
[[A, B],
|
| 844 |
+
[C, D]] = expr.arg.blocks.tolist()
|
| 845 |
+
|
| 846 |
+
formula = _choose_2x2_inversion_formula(A, B, C, D)
|
| 847 |
+
if formula != None:
|
| 848 |
+
MI = expr.arg.schur(formula).I
|
| 849 |
+
if formula == 'A':
|
| 850 |
+
AI = A.I
|
| 851 |
+
return BlockMatrix([[AI + AI * B * MI * C * AI, -AI * B * MI], [-MI * C * AI, MI]])
|
| 852 |
+
if formula == 'B':
|
| 853 |
+
BI = B.I
|
| 854 |
+
return BlockMatrix([[-MI * D * BI, MI], [BI + BI * A * MI * D * BI, -BI * A * MI]])
|
| 855 |
+
if formula == 'C':
|
| 856 |
+
CI = C.I
|
| 857 |
+
return BlockMatrix([[-CI * D * MI, CI + CI * D * MI * A * CI], [MI, -MI * A * CI]])
|
| 858 |
+
if formula == 'D':
|
| 859 |
+
DI = D.I
|
| 860 |
+
return BlockMatrix([[MI, -MI * B * DI], [-DI * C * MI, DI + DI * C * MI * B * DI]])
|
| 861 |
+
|
| 862 |
+
return expr
|
| 863 |
+
|
| 864 |
+
|
| 865 |
+
def _choose_2x2_inversion_formula(A, B, C, D):
|
| 866 |
+
"""
|
| 867 |
+
Assuming [[A, B], [C, D]] would form a valid square block matrix, find
|
| 868 |
+
which of the classical 2x2 block matrix inversion formulas would be
|
| 869 |
+
best suited.
|
| 870 |
+
|
| 871 |
+
Returns 'A', 'B', 'C', 'D' to represent the algorithm involving inversion
|
| 872 |
+
of the given argument or None if the matrix cannot be inverted using
|
| 873 |
+
any of those formulas.
|
| 874 |
+
"""
|
| 875 |
+
# Try to find a known invertible matrix. Note that the Schur complement
|
| 876 |
+
# is currently not being considered for this
|
| 877 |
+
A_inv = ask(Q.invertible(A))
|
| 878 |
+
if A_inv == True:
|
| 879 |
+
return 'A'
|
| 880 |
+
B_inv = ask(Q.invertible(B))
|
| 881 |
+
if B_inv == True:
|
| 882 |
+
return 'B'
|
| 883 |
+
C_inv = ask(Q.invertible(C))
|
| 884 |
+
if C_inv == True:
|
| 885 |
+
return 'C'
|
| 886 |
+
D_inv = ask(Q.invertible(D))
|
| 887 |
+
if D_inv == True:
|
| 888 |
+
return 'D'
|
| 889 |
+
# Otherwise try to find a matrix that isn't known to be non-invertible
|
| 890 |
+
if A_inv != False:
|
| 891 |
+
return 'A'
|
| 892 |
+
if B_inv != False:
|
| 893 |
+
return 'B'
|
| 894 |
+
if C_inv != False:
|
| 895 |
+
return 'C'
|
| 896 |
+
if D_inv != False:
|
| 897 |
+
return 'D'
|
| 898 |
+
return None
|
| 899 |
+
|
| 900 |
+
|
| 901 |
+
def deblock(B):
|
| 902 |
+
""" Flatten a BlockMatrix of BlockMatrices """
|
| 903 |
+
if not isinstance(B, BlockMatrix) or not B.blocks.has(BlockMatrix):
|
| 904 |
+
return B
|
| 905 |
+
wrap = lambda x: x if isinstance(x, BlockMatrix) else BlockMatrix([[x]])
|
| 906 |
+
bb = B.blocks.applyfunc(wrap) # everything is a block
|
| 907 |
+
|
| 908 |
+
try:
|
| 909 |
+
MM = Matrix(0, sum(bb[0, i].blocks.shape[1] for i in range(bb.shape[1])), [])
|
| 910 |
+
for row in range(0, bb.shape[0]):
|
| 911 |
+
M = Matrix(bb[row, 0].blocks)
|
| 912 |
+
for col in range(1, bb.shape[1]):
|
| 913 |
+
M = M.row_join(bb[row, col].blocks)
|
| 914 |
+
MM = MM.col_join(M)
|
| 915 |
+
|
| 916 |
+
return BlockMatrix(MM)
|
| 917 |
+
except ShapeError:
|
| 918 |
+
return B
|
| 919 |
+
|
| 920 |
+
|
| 921 |
+
def reblock_2x2(expr):
|
| 922 |
+
"""
|
| 923 |
+
Reblock a BlockMatrix so that it has 2x2 blocks of block matrices. If
|
| 924 |
+
possible in such a way that the matrix continues to be invertible using the
|
| 925 |
+
classical 2x2 block inversion formulas.
|
| 926 |
+
"""
|
| 927 |
+
if not isinstance(expr, BlockMatrix) or not all(d > 2 for d in expr.blockshape):
|
| 928 |
+
return expr
|
| 929 |
+
|
| 930 |
+
BM = BlockMatrix # for brevity's sake
|
| 931 |
+
rowblocks, colblocks = expr.blockshape
|
| 932 |
+
blocks = expr.blocks
|
| 933 |
+
for i in range(1, rowblocks):
|
| 934 |
+
for j in range(1, colblocks):
|
| 935 |
+
# try to split rows at i and cols at j
|
| 936 |
+
A = bc_unpack(BM(blocks[:i, :j]))
|
| 937 |
+
B = bc_unpack(BM(blocks[:i, j:]))
|
| 938 |
+
C = bc_unpack(BM(blocks[i:, :j]))
|
| 939 |
+
D = bc_unpack(BM(blocks[i:, j:]))
|
| 940 |
+
|
| 941 |
+
formula = _choose_2x2_inversion_formula(A, B, C, D)
|
| 942 |
+
if formula is not None:
|
| 943 |
+
return BlockMatrix([[A, B], [C, D]])
|
| 944 |
+
|
| 945 |
+
# else: nothing worked, just split upper left corner
|
| 946 |
+
return BM([[blocks[0, 0], BM(blocks[0, 1:])],
|
| 947 |
+
[BM(blocks[1:, 0]), BM(blocks[1:, 1:])]])
|
| 948 |
+
|
| 949 |
+
|
| 950 |
+
def bounds(sizes):
|
| 951 |
+
""" Convert sequence of numbers into pairs of low-high pairs
|
| 952 |
+
|
| 953 |
+
>>> from sympy.matrices.expressions.blockmatrix import bounds
|
| 954 |
+
>>> bounds((1, 10, 50))
|
| 955 |
+
[(0, 1), (1, 11), (11, 61)]
|
| 956 |
+
"""
|
| 957 |
+
low = 0
|
| 958 |
+
rv = []
|
| 959 |
+
for size in sizes:
|
| 960 |
+
rv.append((low, low + size))
|
| 961 |
+
low += size
|
| 962 |
+
return rv
|
| 963 |
+
|
| 964 |
+
def blockcut(expr, rowsizes, colsizes):
|
| 965 |
+
""" Cut a matrix expression into Blocks
|
| 966 |
+
|
| 967 |
+
>>> from sympy import ImmutableMatrix, blockcut
|
| 968 |
+
>>> M = ImmutableMatrix(4, 4, range(16))
|
| 969 |
+
>>> B = blockcut(M, (1, 3), (1, 3))
|
| 970 |
+
>>> type(B).__name__
|
| 971 |
+
'BlockMatrix'
|
| 972 |
+
>>> ImmutableMatrix(B.blocks[0, 1])
|
| 973 |
+
Matrix([[1, 2, 3]])
|
| 974 |
+
"""
|
| 975 |
+
|
| 976 |
+
rowbounds = bounds(rowsizes)
|
| 977 |
+
colbounds = bounds(colsizes)
|
| 978 |
+
return BlockMatrix([[MatrixSlice(expr, rowbound, colbound)
|
| 979 |
+
for colbound in colbounds]
|
| 980 |
+
for rowbound in rowbounds])
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/companion.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.core.singleton import S
|
| 2 |
+
from sympy.core.sympify import _sympify
|
| 3 |
+
from sympy.polys.polytools import Poly
|
| 4 |
+
|
| 5 |
+
from .matexpr import MatrixExpr
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class CompanionMatrix(MatrixExpr):
|
| 9 |
+
"""A symbolic companion matrix of a polynomial.
|
| 10 |
+
|
| 11 |
+
Examples
|
| 12 |
+
========
|
| 13 |
+
|
| 14 |
+
>>> from sympy import Poly, Symbol, symbols
|
| 15 |
+
>>> from sympy.matrices.expressions import CompanionMatrix
|
| 16 |
+
>>> x = Symbol('x')
|
| 17 |
+
>>> c0, c1, c2, c3, c4 = symbols('c0:5')
|
| 18 |
+
>>> p = Poly(c0 + c1*x + c2*x**2 + c3*x**3 + c4*x**4 + x**5, x)
|
| 19 |
+
>>> CompanionMatrix(p)
|
| 20 |
+
CompanionMatrix(Poly(x**5 + c4*x**4 + c3*x**3 + c2*x**2 + c1*x + c0,
|
| 21 |
+
x, domain='ZZ[c0,c1,c2,c3,c4]'))
|
| 22 |
+
"""
|
| 23 |
+
def __new__(cls, poly):
|
| 24 |
+
poly = _sympify(poly)
|
| 25 |
+
if not isinstance(poly, Poly):
|
| 26 |
+
raise ValueError("{} must be a Poly instance.".format(poly))
|
| 27 |
+
if not poly.is_monic:
|
| 28 |
+
raise ValueError("{} must be a monic polynomial.".format(poly))
|
| 29 |
+
if not poly.is_univariate:
|
| 30 |
+
raise ValueError(
|
| 31 |
+
"{} must be a univariate polynomial.".format(poly))
|
| 32 |
+
if not poly.degree() >= 1:
|
| 33 |
+
raise ValueError(
|
| 34 |
+
"{} must have degree not less than 1.".format(poly))
|
| 35 |
+
|
| 36 |
+
return super().__new__(cls, poly)
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
@property
|
| 40 |
+
def shape(self):
|
| 41 |
+
poly = self.args[0]
|
| 42 |
+
size = poly.degree()
|
| 43 |
+
return size, size
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def _entry(self, i, j):
|
| 47 |
+
if j == self.cols - 1:
|
| 48 |
+
return -self.args[0].all_coeffs()[-1 - i]
|
| 49 |
+
elif i == j + 1:
|
| 50 |
+
return S.One
|
| 51 |
+
return S.Zero
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def as_explicit(self):
|
| 55 |
+
from sympy.matrices.immutable import ImmutableDenseMatrix
|
| 56 |
+
return ImmutableDenseMatrix.companion(self.args[0])
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/determinant.py
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.core.basic import Basic
|
| 2 |
+
from sympy.core.expr import Expr
|
| 3 |
+
from sympy.core.singleton import S
|
| 4 |
+
from sympy.core.sympify import sympify
|
| 5 |
+
from sympy.matrices.exceptions import NonSquareMatrixError
|
| 6 |
+
from sympy.matrices.matrixbase import MatrixBase
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class Determinant(Expr):
|
| 10 |
+
"""Matrix Determinant
|
| 11 |
+
|
| 12 |
+
Represents the determinant of a matrix expression.
|
| 13 |
+
|
| 14 |
+
Examples
|
| 15 |
+
========
|
| 16 |
+
|
| 17 |
+
>>> from sympy import MatrixSymbol, Determinant, eye
|
| 18 |
+
>>> A = MatrixSymbol('A', 3, 3)
|
| 19 |
+
>>> Determinant(A)
|
| 20 |
+
Determinant(A)
|
| 21 |
+
>>> Determinant(eye(3)).doit()
|
| 22 |
+
1
|
| 23 |
+
"""
|
| 24 |
+
is_commutative = True
|
| 25 |
+
|
| 26 |
+
def __new__(cls, mat):
|
| 27 |
+
mat = sympify(mat)
|
| 28 |
+
if not mat.is_Matrix:
|
| 29 |
+
raise TypeError("Input to Determinant, %s, not a matrix" % str(mat))
|
| 30 |
+
|
| 31 |
+
if mat.is_square is False:
|
| 32 |
+
raise NonSquareMatrixError("Det of a non-square matrix")
|
| 33 |
+
|
| 34 |
+
return Basic.__new__(cls, mat)
|
| 35 |
+
|
| 36 |
+
@property
|
| 37 |
+
def arg(self):
|
| 38 |
+
return self.args[0]
|
| 39 |
+
|
| 40 |
+
@property
|
| 41 |
+
def kind(self):
|
| 42 |
+
return self.arg.kind.element_kind
|
| 43 |
+
|
| 44 |
+
def doit(self, **hints):
|
| 45 |
+
arg = self.arg
|
| 46 |
+
if hints.get('deep', True):
|
| 47 |
+
arg = arg.doit(**hints)
|
| 48 |
+
|
| 49 |
+
result = arg._eval_determinant()
|
| 50 |
+
if result is not None:
|
| 51 |
+
return result
|
| 52 |
+
|
| 53 |
+
return self
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def det(matexpr):
|
| 57 |
+
""" Matrix Determinant
|
| 58 |
+
|
| 59 |
+
Examples
|
| 60 |
+
========
|
| 61 |
+
|
| 62 |
+
>>> from sympy import MatrixSymbol, det, eye
|
| 63 |
+
>>> A = MatrixSymbol('A', 3, 3)
|
| 64 |
+
>>> det(A)
|
| 65 |
+
Determinant(A)
|
| 66 |
+
>>> det(eye(3))
|
| 67 |
+
1
|
| 68 |
+
"""
|
| 69 |
+
|
| 70 |
+
return Determinant(matexpr).doit()
|
| 71 |
+
|
| 72 |
+
class Permanent(Expr):
|
| 73 |
+
"""Matrix Permanent
|
| 74 |
+
|
| 75 |
+
Represents the permanent of a matrix expression.
|
| 76 |
+
|
| 77 |
+
Examples
|
| 78 |
+
========
|
| 79 |
+
|
| 80 |
+
>>> from sympy import MatrixSymbol, Permanent, ones
|
| 81 |
+
>>> A = MatrixSymbol('A', 3, 3)
|
| 82 |
+
>>> Permanent(A)
|
| 83 |
+
Permanent(A)
|
| 84 |
+
>>> Permanent(ones(3, 3)).doit()
|
| 85 |
+
6
|
| 86 |
+
"""
|
| 87 |
+
|
| 88 |
+
def __new__(cls, mat):
|
| 89 |
+
mat = sympify(mat)
|
| 90 |
+
if not mat.is_Matrix:
|
| 91 |
+
raise TypeError("Input to Permanent, %s, not a matrix" % str(mat))
|
| 92 |
+
|
| 93 |
+
return Basic.__new__(cls, mat)
|
| 94 |
+
|
| 95 |
+
@property
|
| 96 |
+
def arg(self):
|
| 97 |
+
return self.args[0]
|
| 98 |
+
|
| 99 |
+
def doit(self, expand=False, **hints):
|
| 100 |
+
if isinstance(self.arg, MatrixBase):
|
| 101 |
+
return self.arg.per()
|
| 102 |
+
else:
|
| 103 |
+
return self
|
| 104 |
+
|
| 105 |
+
def per(matexpr):
|
| 106 |
+
""" Matrix Permanent
|
| 107 |
+
|
| 108 |
+
Examples
|
| 109 |
+
========
|
| 110 |
+
|
| 111 |
+
>>> from sympy import MatrixSymbol, Matrix, per, ones
|
| 112 |
+
>>> A = MatrixSymbol('A', 3, 3)
|
| 113 |
+
>>> per(A)
|
| 114 |
+
Permanent(A)
|
| 115 |
+
>>> per(ones(5, 5))
|
| 116 |
+
120
|
| 117 |
+
>>> M = Matrix([1, 2, 5])
|
| 118 |
+
>>> per(M)
|
| 119 |
+
8
|
| 120 |
+
"""
|
| 121 |
+
|
| 122 |
+
return Permanent(matexpr).doit()
|
| 123 |
+
|
| 124 |
+
from sympy.assumptions.ask import ask, Q
|
| 125 |
+
from sympy.assumptions.refine import handlers_dict
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
def refine_Determinant(expr, assumptions):
|
| 129 |
+
"""
|
| 130 |
+
>>> from sympy import MatrixSymbol, Q, assuming, refine, det
|
| 131 |
+
>>> X = MatrixSymbol('X', 2, 2)
|
| 132 |
+
>>> det(X)
|
| 133 |
+
Determinant(X)
|
| 134 |
+
>>> with assuming(Q.orthogonal(X)):
|
| 135 |
+
... print(refine(det(X)))
|
| 136 |
+
1
|
| 137 |
+
"""
|
| 138 |
+
if ask(Q.orthogonal(expr.arg), assumptions):
|
| 139 |
+
return S.One
|
| 140 |
+
elif ask(Q.singular(expr.arg), assumptions):
|
| 141 |
+
return S.Zero
|
| 142 |
+
elif ask(Q.unit_triangular(expr.arg), assumptions):
|
| 143 |
+
return S.One
|
| 144 |
+
|
| 145 |
+
return expr
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
handlers_dict['Determinant'] = refine_Determinant
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/dotproduct.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.core import Basic, Expr
|
| 2 |
+
from sympy.core.sympify import _sympify
|
| 3 |
+
from sympy.matrices.expressions.transpose import transpose
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class DotProduct(Expr):
|
| 7 |
+
"""
|
| 8 |
+
Dot product of vector matrices
|
| 9 |
+
|
| 10 |
+
The input should be two 1 x n or n x 1 matrices. The output represents the
|
| 11 |
+
scalar dotproduct.
|
| 12 |
+
|
| 13 |
+
This is similar to using MatrixElement and MatMul, except DotProduct does
|
| 14 |
+
not require that one vector to be a row vector and the other vector to be
|
| 15 |
+
a column vector.
|
| 16 |
+
|
| 17 |
+
>>> from sympy import MatrixSymbol, DotProduct
|
| 18 |
+
>>> A = MatrixSymbol('A', 1, 3)
|
| 19 |
+
>>> B = MatrixSymbol('B', 1, 3)
|
| 20 |
+
>>> DotProduct(A, B)
|
| 21 |
+
DotProduct(A, B)
|
| 22 |
+
>>> DotProduct(A, B).doit()
|
| 23 |
+
A[0, 0]*B[0, 0] + A[0, 1]*B[0, 1] + A[0, 2]*B[0, 2]
|
| 24 |
+
"""
|
| 25 |
+
|
| 26 |
+
def __new__(cls, arg1, arg2):
|
| 27 |
+
arg1, arg2 = _sympify((arg1, arg2))
|
| 28 |
+
|
| 29 |
+
if not arg1.is_Matrix:
|
| 30 |
+
raise TypeError("Argument 1 of DotProduct is not a matrix")
|
| 31 |
+
if not arg2.is_Matrix:
|
| 32 |
+
raise TypeError("Argument 2 of DotProduct is not a matrix")
|
| 33 |
+
if not (1 in arg1.shape):
|
| 34 |
+
raise TypeError("Argument 1 of DotProduct is not a vector")
|
| 35 |
+
if not (1 in arg2.shape):
|
| 36 |
+
raise TypeError("Argument 2 of DotProduct is not a vector")
|
| 37 |
+
|
| 38 |
+
if set(arg1.shape) != set(arg2.shape):
|
| 39 |
+
raise TypeError("DotProduct arguments are not the same length")
|
| 40 |
+
|
| 41 |
+
return Basic.__new__(cls, arg1, arg2)
|
| 42 |
+
|
| 43 |
+
def doit(self, expand=False, **hints):
|
| 44 |
+
if self.args[0].shape == self.args[1].shape:
|
| 45 |
+
if self.args[0].shape[0] == 1:
|
| 46 |
+
mul = self.args[0]*transpose(self.args[1])
|
| 47 |
+
else:
|
| 48 |
+
mul = transpose(self.args[0])*self.args[1]
|
| 49 |
+
else:
|
| 50 |
+
if self.args[0].shape[0] == 1:
|
| 51 |
+
mul = self.args[0]*self.args[1]
|
| 52 |
+
else:
|
| 53 |
+
mul = transpose(self.args[0])*transpose(self.args[1])
|
| 54 |
+
|
| 55 |
+
return mul[0]
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/factorizations.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.matrices.expressions import MatrixExpr
|
| 2 |
+
from sympy.assumptions.ask import Q
|
| 3 |
+
|
| 4 |
+
class Factorization(MatrixExpr):
|
| 5 |
+
arg = property(lambda self: self.args[0])
|
| 6 |
+
shape = property(lambda self: self.arg.shape) # type: ignore
|
| 7 |
+
|
| 8 |
+
class LofLU(Factorization):
|
| 9 |
+
@property
|
| 10 |
+
def predicates(self):
|
| 11 |
+
return (Q.lower_triangular,)
|
| 12 |
+
class UofLU(Factorization):
|
| 13 |
+
@property
|
| 14 |
+
def predicates(self):
|
| 15 |
+
return (Q.upper_triangular,)
|
| 16 |
+
|
| 17 |
+
class LofCholesky(LofLU): pass
|
| 18 |
+
class UofCholesky(UofLU): pass
|
| 19 |
+
|
| 20 |
+
class QofQR(Factorization):
|
| 21 |
+
@property
|
| 22 |
+
def predicates(self):
|
| 23 |
+
return (Q.orthogonal,)
|
| 24 |
+
class RofQR(Factorization):
|
| 25 |
+
@property
|
| 26 |
+
def predicates(self):
|
| 27 |
+
return (Q.upper_triangular,)
|
| 28 |
+
|
| 29 |
+
class EigenVectors(Factorization):
|
| 30 |
+
@property
|
| 31 |
+
def predicates(self):
|
| 32 |
+
return (Q.orthogonal,)
|
| 33 |
+
class EigenValues(Factorization):
|
| 34 |
+
@property
|
| 35 |
+
def predicates(self):
|
| 36 |
+
return (Q.diagonal,)
|
| 37 |
+
|
| 38 |
+
class UofSVD(Factorization):
|
| 39 |
+
@property
|
| 40 |
+
def predicates(self):
|
| 41 |
+
return (Q.orthogonal,)
|
| 42 |
+
class SofSVD(Factorization):
|
| 43 |
+
@property
|
| 44 |
+
def predicates(self):
|
| 45 |
+
return (Q.diagonal,)
|
| 46 |
+
class VofSVD(Factorization):
|
| 47 |
+
@property
|
| 48 |
+
def predicates(self):
|
| 49 |
+
return (Q.orthogonal,)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def lu(expr):
|
| 53 |
+
return LofLU(expr), UofLU(expr)
|
| 54 |
+
|
| 55 |
+
def qr(expr):
|
| 56 |
+
return QofQR(expr), RofQR(expr)
|
| 57 |
+
|
| 58 |
+
def eig(expr):
|
| 59 |
+
return EigenValues(expr), EigenVectors(expr)
|
| 60 |
+
|
| 61 |
+
def svd(expr):
|
| 62 |
+
return UofSVD(expr), SofSVD(expr), VofSVD(expr)
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/funcmatrix.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from .matexpr import MatrixExpr
|
| 2 |
+
from sympy.core.function import FunctionClass, Lambda
|
| 3 |
+
from sympy.core.symbol import Dummy
|
| 4 |
+
from sympy.core.sympify import _sympify, sympify
|
| 5 |
+
from sympy.matrices import Matrix
|
| 6 |
+
from sympy.functions.elementary.complexes import re, im
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class FunctionMatrix(MatrixExpr):
|
| 10 |
+
"""Represents a matrix using a function (``Lambda``) which gives
|
| 11 |
+
outputs according to the coordinates of each matrix entries.
|
| 12 |
+
|
| 13 |
+
Parameters
|
| 14 |
+
==========
|
| 15 |
+
|
| 16 |
+
rows : nonnegative integer. Can be symbolic.
|
| 17 |
+
|
| 18 |
+
cols : nonnegative integer. Can be symbolic.
|
| 19 |
+
|
| 20 |
+
lamda : Function, Lambda or str
|
| 21 |
+
If it is a SymPy ``Function`` or ``Lambda`` instance,
|
| 22 |
+
it should be able to accept two arguments which represents the
|
| 23 |
+
matrix coordinates.
|
| 24 |
+
|
| 25 |
+
If it is a pure string containing Python ``lambda`` semantics,
|
| 26 |
+
it is interpreted by the SymPy parser and casted into a SymPy
|
| 27 |
+
``Lambda`` instance.
|
| 28 |
+
|
| 29 |
+
Examples
|
| 30 |
+
========
|
| 31 |
+
|
| 32 |
+
Creating a ``FunctionMatrix`` from ``Lambda``:
|
| 33 |
+
|
| 34 |
+
>>> from sympy import FunctionMatrix, symbols, Lambda, MatPow
|
| 35 |
+
>>> i, j, n, m = symbols('i,j,n,m')
|
| 36 |
+
>>> FunctionMatrix(n, m, Lambda((i, j), i + j))
|
| 37 |
+
FunctionMatrix(n, m, Lambda((i, j), i + j))
|
| 38 |
+
|
| 39 |
+
Creating a ``FunctionMatrix`` from a SymPy function:
|
| 40 |
+
|
| 41 |
+
>>> from sympy import KroneckerDelta
|
| 42 |
+
>>> X = FunctionMatrix(3, 3, KroneckerDelta)
|
| 43 |
+
>>> X.as_explicit()
|
| 44 |
+
Matrix([
|
| 45 |
+
[1, 0, 0],
|
| 46 |
+
[0, 1, 0],
|
| 47 |
+
[0, 0, 1]])
|
| 48 |
+
|
| 49 |
+
Creating a ``FunctionMatrix`` from a SymPy undefined function:
|
| 50 |
+
|
| 51 |
+
>>> from sympy import Function
|
| 52 |
+
>>> f = Function('f')
|
| 53 |
+
>>> X = FunctionMatrix(3, 3, f)
|
| 54 |
+
>>> X.as_explicit()
|
| 55 |
+
Matrix([
|
| 56 |
+
[f(0, 0), f(0, 1), f(0, 2)],
|
| 57 |
+
[f(1, 0), f(1, 1), f(1, 2)],
|
| 58 |
+
[f(2, 0), f(2, 1), f(2, 2)]])
|
| 59 |
+
|
| 60 |
+
Creating a ``FunctionMatrix`` from Python ``lambda``:
|
| 61 |
+
|
| 62 |
+
>>> FunctionMatrix(n, m, 'lambda i, j: i + j')
|
| 63 |
+
FunctionMatrix(n, m, Lambda((i, j), i + j))
|
| 64 |
+
|
| 65 |
+
Example of lazy evaluation of matrix product:
|
| 66 |
+
|
| 67 |
+
>>> Y = FunctionMatrix(1000, 1000, Lambda((i, j), i + j))
|
| 68 |
+
>>> isinstance(Y*Y, MatPow) # this is an expression object
|
| 69 |
+
True
|
| 70 |
+
>>> (Y**2)[10,10] # So this is evaluated lazily
|
| 71 |
+
342923500
|
| 72 |
+
|
| 73 |
+
Notes
|
| 74 |
+
=====
|
| 75 |
+
|
| 76 |
+
This class provides an alternative way to represent an extremely
|
| 77 |
+
dense matrix with entries in some form of a sequence, in a most
|
| 78 |
+
sparse way.
|
| 79 |
+
"""
|
| 80 |
+
def __new__(cls, rows, cols, lamda):
|
| 81 |
+
rows, cols = _sympify(rows), _sympify(cols)
|
| 82 |
+
cls._check_dim(rows)
|
| 83 |
+
cls._check_dim(cols)
|
| 84 |
+
|
| 85 |
+
lamda = sympify(lamda)
|
| 86 |
+
if not isinstance(lamda, (FunctionClass, Lambda)):
|
| 87 |
+
raise ValueError(
|
| 88 |
+
"{} should be compatible with SymPy function classes."
|
| 89 |
+
.format(lamda))
|
| 90 |
+
|
| 91 |
+
if 2 not in lamda.nargs:
|
| 92 |
+
raise ValueError(
|
| 93 |
+
'{} should be able to accept 2 arguments.'.format(lamda))
|
| 94 |
+
|
| 95 |
+
if not isinstance(lamda, Lambda):
|
| 96 |
+
i, j = Dummy('i'), Dummy('j')
|
| 97 |
+
lamda = Lambda((i, j), lamda(i, j))
|
| 98 |
+
|
| 99 |
+
return super().__new__(cls, rows, cols, lamda)
|
| 100 |
+
|
| 101 |
+
@property
|
| 102 |
+
def shape(self):
|
| 103 |
+
return self.args[0:2]
|
| 104 |
+
|
| 105 |
+
@property
|
| 106 |
+
def lamda(self):
|
| 107 |
+
return self.args[2]
|
| 108 |
+
|
| 109 |
+
def _entry(self, i, j, **kwargs):
|
| 110 |
+
return self.lamda(i, j)
|
| 111 |
+
|
| 112 |
+
def _eval_trace(self):
|
| 113 |
+
from sympy.matrices.expressions.trace import Trace
|
| 114 |
+
from sympy.concrete.summations import Sum
|
| 115 |
+
return Trace(self).rewrite(Sum).doit()
|
| 116 |
+
|
| 117 |
+
def _eval_as_real_imag(self):
|
| 118 |
+
return (re(Matrix(self)), im(Matrix(self)))
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/hadamard.py
ADDED
|
@@ -0,0 +1,464 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from collections import Counter
|
| 2 |
+
|
| 3 |
+
from sympy.core import Mul, sympify
|
| 4 |
+
from sympy.core.add import Add
|
| 5 |
+
from sympy.core.expr import ExprBuilder
|
| 6 |
+
from sympy.core.sorting import default_sort_key
|
| 7 |
+
from sympy.functions.elementary.exponential import log
|
| 8 |
+
from sympy.matrices.expressions.matexpr import MatrixExpr
|
| 9 |
+
from sympy.matrices.expressions._shape import validate_matadd_integer as validate
|
| 10 |
+
from sympy.matrices.expressions.special import ZeroMatrix, OneMatrix
|
| 11 |
+
from sympy.strategies import (
|
| 12 |
+
unpack, flatten, condition, exhaust, rm_id, sort
|
| 13 |
+
)
|
| 14 |
+
from sympy.utilities.exceptions import sympy_deprecation_warning
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def hadamard_product(*matrices):
|
| 18 |
+
"""
|
| 19 |
+
Return the elementwise (aka Hadamard) product of matrices.
|
| 20 |
+
|
| 21 |
+
Examples
|
| 22 |
+
========
|
| 23 |
+
|
| 24 |
+
>>> from sympy import hadamard_product, MatrixSymbol
|
| 25 |
+
>>> A = MatrixSymbol('A', 2, 3)
|
| 26 |
+
>>> B = MatrixSymbol('B', 2, 3)
|
| 27 |
+
>>> hadamard_product(A)
|
| 28 |
+
A
|
| 29 |
+
>>> hadamard_product(A, B)
|
| 30 |
+
HadamardProduct(A, B)
|
| 31 |
+
>>> hadamard_product(A, B)[0, 1]
|
| 32 |
+
A[0, 1]*B[0, 1]
|
| 33 |
+
"""
|
| 34 |
+
if not matrices:
|
| 35 |
+
raise TypeError("Empty Hadamard product is undefined")
|
| 36 |
+
if len(matrices) == 1:
|
| 37 |
+
return matrices[0]
|
| 38 |
+
return HadamardProduct(*matrices).doit()
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
class HadamardProduct(MatrixExpr):
|
| 42 |
+
"""
|
| 43 |
+
Elementwise product of matrix expressions
|
| 44 |
+
|
| 45 |
+
Examples
|
| 46 |
+
========
|
| 47 |
+
|
| 48 |
+
Hadamard product for matrix symbols:
|
| 49 |
+
|
| 50 |
+
>>> from sympy import hadamard_product, HadamardProduct, MatrixSymbol
|
| 51 |
+
>>> A = MatrixSymbol('A', 5, 5)
|
| 52 |
+
>>> B = MatrixSymbol('B', 5, 5)
|
| 53 |
+
>>> isinstance(hadamard_product(A, B), HadamardProduct)
|
| 54 |
+
True
|
| 55 |
+
|
| 56 |
+
Notes
|
| 57 |
+
=====
|
| 58 |
+
|
| 59 |
+
This is a symbolic object that simply stores its argument without
|
| 60 |
+
evaluating it. To actually compute the product, use the function
|
| 61 |
+
``hadamard_product()`` or ``HadamardProduct.doit``
|
| 62 |
+
"""
|
| 63 |
+
is_HadamardProduct = True
|
| 64 |
+
|
| 65 |
+
def __new__(cls, *args, evaluate=False, check=None):
|
| 66 |
+
args = list(map(sympify, args))
|
| 67 |
+
if len(args) == 0:
|
| 68 |
+
# We currently don't have a way to support one-matrices of generic dimensions:
|
| 69 |
+
raise ValueError("HadamardProduct needs at least one argument")
|
| 70 |
+
|
| 71 |
+
if not all(isinstance(arg, MatrixExpr) for arg in args):
|
| 72 |
+
raise TypeError("Mix of Matrix and Scalar symbols")
|
| 73 |
+
|
| 74 |
+
if check is not None:
|
| 75 |
+
sympy_deprecation_warning(
|
| 76 |
+
"Passing check to HadamardProduct is deprecated and the check argument will be removed in a future version.",
|
| 77 |
+
deprecated_since_version="1.11",
|
| 78 |
+
active_deprecations_target='remove-check-argument-from-matrix-operations')
|
| 79 |
+
|
| 80 |
+
if check is not False:
|
| 81 |
+
validate(*args)
|
| 82 |
+
|
| 83 |
+
obj = super().__new__(cls, *args)
|
| 84 |
+
if evaluate:
|
| 85 |
+
obj = obj.doit(deep=False)
|
| 86 |
+
return obj
|
| 87 |
+
|
| 88 |
+
@property
|
| 89 |
+
def shape(self):
|
| 90 |
+
return self.args[0].shape
|
| 91 |
+
|
| 92 |
+
def _entry(self, i, j, **kwargs):
|
| 93 |
+
return Mul(*[arg._entry(i, j, **kwargs) for arg in self.args])
|
| 94 |
+
|
| 95 |
+
def _eval_transpose(self):
|
| 96 |
+
from sympy.matrices.expressions.transpose import transpose
|
| 97 |
+
return HadamardProduct(*list(map(transpose, self.args)))
|
| 98 |
+
|
| 99 |
+
def doit(self, **hints):
|
| 100 |
+
expr = self.func(*(i.doit(**hints) for i in self.args))
|
| 101 |
+
# Check for explicit matrices:
|
| 102 |
+
from sympy.matrices.matrixbase import MatrixBase
|
| 103 |
+
from sympy.matrices.immutable import ImmutableMatrix
|
| 104 |
+
|
| 105 |
+
explicit = [i for i in expr.args if isinstance(i, MatrixBase)]
|
| 106 |
+
if explicit:
|
| 107 |
+
remainder = [i for i in expr.args if i not in explicit]
|
| 108 |
+
expl_mat = ImmutableMatrix([
|
| 109 |
+
Mul.fromiter(i) for i in zip(*explicit)
|
| 110 |
+
]).reshape(*self.shape)
|
| 111 |
+
expr = HadamardProduct(*([expl_mat] + remainder))
|
| 112 |
+
|
| 113 |
+
return canonicalize(expr)
|
| 114 |
+
|
| 115 |
+
def _eval_derivative(self, x):
|
| 116 |
+
terms = []
|
| 117 |
+
args = list(self.args)
|
| 118 |
+
for i in range(len(args)):
|
| 119 |
+
factors = args[:i] + [args[i].diff(x)] + args[i+1:]
|
| 120 |
+
terms.append(hadamard_product(*factors))
|
| 121 |
+
return Add.fromiter(terms)
|
| 122 |
+
|
| 123 |
+
def _eval_derivative_matrix_lines(self, x):
|
| 124 |
+
from sympy.tensor.array.expressions.array_expressions import ArrayDiagonal
|
| 125 |
+
from sympy.tensor.array.expressions.array_expressions import ArrayTensorProduct
|
| 126 |
+
from sympy.matrices.expressions.matexpr import _make_matrix
|
| 127 |
+
|
| 128 |
+
with_x_ind = [i for i, arg in enumerate(self.args) if arg.has(x)]
|
| 129 |
+
lines = []
|
| 130 |
+
for ind in with_x_ind:
|
| 131 |
+
left_args = self.args[:ind]
|
| 132 |
+
right_args = self.args[ind+1:]
|
| 133 |
+
|
| 134 |
+
d = self.args[ind]._eval_derivative_matrix_lines(x)
|
| 135 |
+
hadam = hadamard_product(*(right_args + left_args))
|
| 136 |
+
diagonal = [(0, 2), (3, 4)]
|
| 137 |
+
diagonal = [e for j, e in enumerate(diagonal) if self.shape[j] != 1]
|
| 138 |
+
for i in d:
|
| 139 |
+
l1 = i._lines[i._first_line_index]
|
| 140 |
+
l2 = i._lines[i._second_line_index]
|
| 141 |
+
subexpr = ExprBuilder(
|
| 142 |
+
ArrayDiagonal,
|
| 143 |
+
[
|
| 144 |
+
ExprBuilder(
|
| 145 |
+
ArrayTensorProduct,
|
| 146 |
+
[
|
| 147 |
+
ExprBuilder(_make_matrix, [l1]),
|
| 148 |
+
hadam,
|
| 149 |
+
ExprBuilder(_make_matrix, [l2]),
|
| 150 |
+
]
|
| 151 |
+
),
|
| 152 |
+
*diagonal],
|
| 153 |
+
|
| 154 |
+
)
|
| 155 |
+
i._first_pointer_parent = subexpr.args[0].args[0].args
|
| 156 |
+
i._first_pointer_index = 0
|
| 157 |
+
i._second_pointer_parent = subexpr.args[0].args[2].args
|
| 158 |
+
i._second_pointer_index = 0
|
| 159 |
+
i._lines = [subexpr]
|
| 160 |
+
lines.append(i)
|
| 161 |
+
|
| 162 |
+
return lines
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
# TODO Implement algorithm for rewriting Hadamard product as diagonal matrix
|
| 166 |
+
# if matmul identy matrix is multiplied.
|
| 167 |
+
def canonicalize(x):
|
| 168 |
+
"""Canonicalize the Hadamard product ``x`` with mathematical properties.
|
| 169 |
+
|
| 170 |
+
Examples
|
| 171 |
+
========
|
| 172 |
+
|
| 173 |
+
>>> from sympy import MatrixSymbol, HadamardProduct
|
| 174 |
+
>>> from sympy import OneMatrix, ZeroMatrix
|
| 175 |
+
>>> from sympy.matrices.expressions.hadamard import canonicalize
|
| 176 |
+
>>> from sympy import init_printing
|
| 177 |
+
>>> init_printing(use_unicode=False)
|
| 178 |
+
|
| 179 |
+
>>> A = MatrixSymbol('A', 2, 2)
|
| 180 |
+
>>> B = MatrixSymbol('B', 2, 2)
|
| 181 |
+
>>> C = MatrixSymbol('C', 2, 2)
|
| 182 |
+
|
| 183 |
+
Hadamard product associativity:
|
| 184 |
+
|
| 185 |
+
>>> X = HadamardProduct(A, HadamardProduct(B, C))
|
| 186 |
+
>>> X
|
| 187 |
+
A.*(B.*C)
|
| 188 |
+
>>> canonicalize(X)
|
| 189 |
+
A.*B.*C
|
| 190 |
+
|
| 191 |
+
Hadamard product commutativity:
|
| 192 |
+
|
| 193 |
+
>>> X = HadamardProduct(A, B)
|
| 194 |
+
>>> Y = HadamardProduct(B, A)
|
| 195 |
+
>>> X
|
| 196 |
+
A.*B
|
| 197 |
+
>>> Y
|
| 198 |
+
B.*A
|
| 199 |
+
>>> canonicalize(X)
|
| 200 |
+
A.*B
|
| 201 |
+
>>> canonicalize(Y)
|
| 202 |
+
A.*B
|
| 203 |
+
|
| 204 |
+
Hadamard product identity:
|
| 205 |
+
|
| 206 |
+
>>> X = HadamardProduct(A, OneMatrix(2, 2))
|
| 207 |
+
>>> X
|
| 208 |
+
A.*1
|
| 209 |
+
>>> canonicalize(X)
|
| 210 |
+
A
|
| 211 |
+
|
| 212 |
+
Absorbing element of Hadamard product:
|
| 213 |
+
|
| 214 |
+
>>> X = HadamardProduct(A, ZeroMatrix(2, 2))
|
| 215 |
+
>>> X
|
| 216 |
+
A.*0
|
| 217 |
+
>>> canonicalize(X)
|
| 218 |
+
0
|
| 219 |
+
|
| 220 |
+
Rewriting to Hadamard Power
|
| 221 |
+
|
| 222 |
+
>>> X = HadamardProduct(A, A, A)
|
| 223 |
+
>>> X
|
| 224 |
+
A.*A.*A
|
| 225 |
+
>>> canonicalize(X)
|
| 226 |
+
.3
|
| 227 |
+
A
|
| 228 |
+
|
| 229 |
+
Notes
|
| 230 |
+
=====
|
| 231 |
+
|
| 232 |
+
As the Hadamard product is associative, nested products can be flattened.
|
| 233 |
+
|
| 234 |
+
The Hadamard product is commutative so that factors can be sorted for
|
| 235 |
+
canonical form.
|
| 236 |
+
|
| 237 |
+
A matrix of only ones is an identity for Hadamard product,
|
| 238 |
+
so every matrices of only ones can be removed.
|
| 239 |
+
|
| 240 |
+
Any zero matrix will make the whole product a zero matrix.
|
| 241 |
+
|
| 242 |
+
Duplicate elements can be collected and rewritten as HadamardPower
|
| 243 |
+
|
| 244 |
+
References
|
| 245 |
+
==========
|
| 246 |
+
|
| 247 |
+
.. [1] https://en.wikipedia.org/wiki/Hadamard_product_(matrices)
|
| 248 |
+
"""
|
| 249 |
+
# Associativity
|
| 250 |
+
rule = condition(
|
| 251 |
+
lambda x: isinstance(x, HadamardProduct),
|
| 252 |
+
flatten
|
| 253 |
+
)
|
| 254 |
+
fun = exhaust(rule)
|
| 255 |
+
x = fun(x)
|
| 256 |
+
|
| 257 |
+
# Identity
|
| 258 |
+
fun = condition(
|
| 259 |
+
lambda x: isinstance(x, HadamardProduct),
|
| 260 |
+
rm_id(lambda x: isinstance(x, OneMatrix))
|
| 261 |
+
)
|
| 262 |
+
x = fun(x)
|
| 263 |
+
|
| 264 |
+
# Absorbing by Zero Matrix
|
| 265 |
+
def absorb(x):
|
| 266 |
+
if any(isinstance(c, ZeroMatrix) for c in x.args):
|
| 267 |
+
return ZeroMatrix(*x.shape)
|
| 268 |
+
else:
|
| 269 |
+
return x
|
| 270 |
+
fun = condition(
|
| 271 |
+
lambda x: isinstance(x, HadamardProduct),
|
| 272 |
+
absorb
|
| 273 |
+
)
|
| 274 |
+
x = fun(x)
|
| 275 |
+
|
| 276 |
+
# Rewriting with HadamardPower
|
| 277 |
+
if isinstance(x, HadamardProduct):
|
| 278 |
+
tally = Counter(x.args)
|
| 279 |
+
|
| 280 |
+
new_arg = []
|
| 281 |
+
for base, exp in tally.items():
|
| 282 |
+
if exp == 1:
|
| 283 |
+
new_arg.append(base)
|
| 284 |
+
else:
|
| 285 |
+
new_arg.append(HadamardPower(base, exp))
|
| 286 |
+
|
| 287 |
+
x = HadamardProduct(*new_arg)
|
| 288 |
+
|
| 289 |
+
# Commutativity
|
| 290 |
+
fun = condition(
|
| 291 |
+
lambda x: isinstance(x, HadamardProduct),
|
| 292 |
+
sort(default_sort_key)
|
| 293 |
+
)
|
| 294 |
+
x = fun(x)
|
| 295 |
+
|
| 296 |
+
# Unpacking
|
| 297 |
+
x = unpack(x)
|
| 298 |
+
return x
|
| 299 |
+
|
| 300 |
+
|
| 301 |
+
def hadamard_power(base, exp):
|
| 302 |
+
base = sympify(base)
|
| 303 |
+
exp = sympify(exp)
|
| 304 |
+
if exp == 1:
|
| 305 |
+
return base
|
| 306 |
+
if not base.is_Matrix:
|
| 307 |
+
return base**exp
|
| 308 |
+
if exp.is_Matrix:
|
| 309 |
+
raise ValueError("cannot raise expression to a matrix")
|
| 310 |
+
return HadamardPower(base, exp)
|
| 311 |
+
|
| 312 |
+
|
| 313 |
+
class HadamardPower(MatrixExpr):
|
| 314 |
+
r"""
|
| 315 |
+
Elementwise power of matrix expressions
|
| 316 |
+
|
| 317 |
+
Parameters
|
| 318 |
+
==========
|
| 319 |
+
|
| 320 |
+
base : scalar or matrix
|
| 321 |
+
|
| 322 |
+
exp : scalar or matrix
|
| 323 |
+
|
| 324 |
+
Notes
|
| 325 |
+
=====
|
| 326 |
+
|
| 327 |
+
There are four definitions for the hadamard power which can be used.
|
| 328 |
+
Let's consider `A, B` as `(m, n)` matrices, and `a, b` as scalars.
|
| 329 |
+
|
| 330 |
+
Matrix raised to a scalar exponent:
|
| 331 |
+
|
| 332 |
+
.. math::
|
| 333 |
+
A^{\circ b} = \begin{bmatrix}
|
| 334 |
+
A_{0, 0}^b & A_{0, 1}^b & \cdots & A_{0, n-1}^b \\
|
| 335 |
+
A_{1, 0}^b & A_{1, 1}^b & \cdots & A_{1, n-1}^b \\
|
| 336 |
+
\vdots & \vdots & \ddots & \vdots \\
|
| 337 |
+
A_{m-1, 0}^b & A_{m-1, 1}^b & \cdots & A_{m-1, n-1}^b
|
| 338 |
+
\end{bmatrix}
|
| 339 |
+
|
| 340 |
+
Scalar raised to a matrix exponent:
|
| 341 |
+
|
| 342 |
+
.. math::
|
| 343 |
+
a^{\circ B} = \begin{bmatrix}
|
| 344 |
+
a^{B_{0, 0}} & a^{B_{0, 1}} & \cdots & a^{B_{0, n-1}} \\
|
| 345 |
+
a^{B_{1, 0}} & a^{B_{1, 1}} & \cdots & a^{B_{1, n-1}} \\
|
| 346 |
+
\vdots & \vdots & \ddots & \vdots \\
|
| 347 |
+
a^{B_{m-1, 0}} & a^{B_{m-1, 1}} & \cdots & a^{B_{m-1, n-1}}
|
| 348 |
+
\end{bmatrix}
|
| 349 |
+
|
| 350 |
+
Matrix raised to a matrix exponent:
|
| 351 |
+
|
| 352 |
+
.. math::
|
| 353 |
+
A^{\circ B} = \begin{bmatrix}
|
| 354 |
+
A_{0, 0}^{B_{0, 0}} & A_{0, 1}^{B_{0, 1}} &
|
| 355 |
+
\cdots & A_{0, n-1}^{B_{0, n-1}} \\
|
| 356 |
+
A_{1, 0}^{B_{1, 0}} & A_{1, 1}^{B_{1, 1}} &
|
| 357 |
+
\cdots & A_{1, n-1}^{B_{1, n-1}} \\
|
| 358 |
+
\vdots & \vdots &
|
| 359 |
+
\ddots & \vdots \\
|
| 360 |
+
A_{m-1, 0}^{B_{m-1, 0}} & A_{m-1, 1}^{B_{m-1, 1}} &
|
| 361 |
+
\cdots & A_{m-1, n-1}^{B_{m-1, n-1}}
|
| 362 |
+
\end{bmatrix}
|
| 363 |
+
|
| 364 |
+
Scalar raised to a scalar exponent:
|
| 365 |
+
|
| 366 |
+
.. math::
|
| 367 |
+
a^{\circ b} = a^b
|
| 368 |
+
"""
|
| 369 |
+
|
| 370 |
+
def __new__(cls, base, exp):
|
| 371 |
+
base = sympify(base)
|
| 372 |
+
exp = sympify(exp)
|
| 373 |
+
|
| 374 |
+
if base.is_scalar and exp.is_scalar:
|
| 375 |
+
return base ** exp
|
| 376 |
+
|
| 377 |
+
if isinstance(base, MatrixExpr) and isinstance(exp, MatrixExpr):
|
| 378 |
+
validate(base, exp)
|
| 379 |
+
|
| 380 |
+
obj = super().__new__(cls, base, exp)
|
| 381 |
+
return obj
|
| 382 |
+
|
| 383 |
+
@property
|
| 384 |
+
def base(self):
|
| 385 |
+
return self._args[0]
|
| 386 |
+
|
| 387 |
+
@property
|
| 388 |
+
def exp(self):
|
| 389 |
+
return self._args[1]
|
| 390 |
+
|
| 391 |
+
@property
|
| 392 |
+
def shape(self):
|
| 393 |
+
if self.base.is_Matrix:
|
| 394 |
+
return self.base.shape
|
| 395 |
+
return self.exp.shape
|
| 396 |
+
|
| 397 |
+
def _entry(self, i, j, **kwargs):
|
| 398 |
+
base = self.base
|
| 399 |
+
exp = self.exp
|
| 400 |
+
|
| 401 |
+
if base.is_Matrix:
|
| 402 |
+
a = base._entry(i, j, **kwargs)
|
| 403 |
+
elif base.is_scalar:
|
| 404 |
+
a = base
|
| 405 |
+
else:
|
| 406 |
+
raise ValueError(
|
| 407 |
+
'The base {} must be a scalar or a matrix.'.format(base))
|
| 408 |
+
|
| 409 |
+
if exp.is_Matrix:
|
| 410 |
+
b = exp._entry(i, j, **kwargs)
|
| 411 |
+
elif exp.is_scalar:
|
| 412 |
+
b = exp
|
| 413 |
+
else:
|
| 414 |
+
raise ValueError(
|
| 415 |
+
'The exponent {} must be a scalar or a matrix.'.format(exp))
|
| 416 |
+
|
| 417 |
+
return a ** b
|
| 418 |
+
|
| 419 |
+
def _eval_transpose(self):
|
| 420 |
+
from sympy.matrices.expressions.transpose import transpose
|
| 421 |
+
return HadamardPower(transpose(self.base), self.exp)
|
| 422 |
+
|
| 423 |
+
def _eval_derivative(self, x):
|
| 424 |
+
dexp = self.exp.diff(x)
|
| 425 |
+
logbase = self.base.applyfunc(log)
|
| 426 |
+
dlbase = logbase.diff(x)
|
| 427 |
+
return hadamard_product(
|
| 428 |
+
dexp*logbase + self.exp*dlbase,
|
| 429 |
+
self
|
| 430 |
+
)
|
| 431 |
+
|
| 432 |
+
def _eval_derivative_matrix_lines(self, x):
|
| 433 |
+
from sympy.tensor.array.expressions.array_expressions import ArrayTensorProduct
|
| 434 |
+
from sympy.tensor.array.expressions.array_expressions import ArrayDiagonal
|
| 435 |
+
from sympy.matrices.expressions.matexpr import _make_matrix
|
| 436 |
+
|
| 437 |
+
lr = self.base._eval_derivative_matrix_lines(x)
|
| 438 |
+
for i in lr:
|
| 439 |
+
diagonal = [(1, 2), (3, 4)]
|
| 440 |
+
diagonal = [e for j, e in enumerate(diagonal) if self.base.shape[j] != 1]
|
| 441 |
+
l1 = i._lines[i._first_line_index]
|
| 442 |
+
l2 = i._lines[i._second_line_index]
|
| 443 |
+
subexpr = ExprBuilder(
|
| 444 |
+
ArrayDiagonal,
|
| 445 |
+
[
|
| 446 |
+
ExprBuilder(
|
| 447 |
+
ArrayTensorProduct,
|
| 448 |
+
[
|
| 449 |
+
ExprBuilder(_make_matrix, [l1]),
|
| 450 |
+
self.exp*hadamard_power(self.base, self.exp-1),
|
| 451 |
+
ExprBuilder(_make_matrix, [l2]),
|
| 452 |
+
]
|
| 453 |
+
),
|
| 454 |
+
*diagonal],
|
| 455 |
+
validator=ArrayDiagonal._validate
|
| 456 |
+
)
|
| 457 |
+
i._first_pointer_parent = subexpr.args[0].args[0].args
|
| 458 |
+
i._first_pointer_index = 0
|
| 459 |
+
i._first_line_index = 0
|
| 460 |
+
i._second_pointer_parent = subexpr.args[0].args[2].args
|
| 461 |
+
i._second_pointer_index = 0
|
| 462 |
+
i._second_line_index = 0
|
| 463 |
+
i._lines = [subexpr]
|
| 464 |
+
return lr
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/kronecker.py
ADDED
|
@@ -0,0 +1,434 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.exceptions 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.matrixbase 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
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/matadd.py
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from functools import reduce
|
| 2 |
+
import operator
|
| 3 |
+
|
| 4 |
+
from sympy.core import Basic, sympify
|
| 5 |
+
from sympy.core.add import add, Add, _could_extract_minus_sign
|
| 6 |
+
from sympy.core.sorting import default_sort_key
|
| 7 |
+
from sympy.functions import adjoint
|
| 8 |
+
from sympy.matrices.matrixbase import MatrixBase
|
| 9 |
+
from sympy.matrices.expressions.transpose import transpose
|
| 10 |
+
from sympy.strategies import (rm_id, unpack, flatten, sort, condition,
|
| 11 |
+
exhaust, do_one, glom)
|
| 12 |
+
from sympy.matrices.expressions.matexpr import MatrixExpr
|
| 13 |
+
from sympy.matrices.expressions.special import ZeroMatrix, GenericZeroMatrix
|
| 14 |
+
from sympy.matrices.expressions._shape import validate_matadd_integer as validate
|
| 15 |
+
from sympy.utilities.iterables import sift
|
| 16 |
+
from sympy.utilities.exceptions import sympy_deprecation_warning
|
| 17 |
+
|
| 18 |
+
# XXX: MatAdd should perhaps not subclass directly from Add
|
| 19 |
+
class MatAdd(MatrixExpr, Add):
|
| 20 |
+
"""A Sum of Matrix Expressions
|
| 21 |
+
|
| 22 |
+
MatAdd inherits from and operates like SymPy Add
|
| 23 |
+
|
| 24 |
+
Examples
|
| 25 |
+
========
|
| 26 |
+
|
| 27 |
+
>>> from sympy import MatAdd, MatrixSymbol
|
| 28 |
+
>>> A = MatrixSymbol('A', 5, 5)
|
| 29 |
+
>>> B = MatrixSymbol('B', 5, 5)
|
| 30 |
+
>>> C = MatrixSymbol('C', 5, 5)
|
| 31 |
+
>>> MatAdd(A, B, C)
|
| 32 |
+
A + B + C
|
| 33 |
+
"""
|
| 34 |
+
is_MatAdd = True
|
| 35 |
+
|
| 36 |
+
identity = GenericZeroMatrix()
|
| 37 |
+
|
| 38 |
+
def __new__(cls, *args, evaluate=False, check=None, _sympify=True):
|
| 39 |
+
if not args:
|
| 40 |
+
return cls.identity
|
| 41 |
+
|
| 42 |
+
# This must be removed aggressively in the constructor to avoid
|
| 43 |
+
# TypeErrors from GenericZeroMatrix().shape
|
| 44 |
+
args = list(filter(lambda i: cls.identity != i, args))
|
| 45 |
+
if _sympify:
|
| 46 |
+
args = list(map(sympify, args))
|
| 47 |
+
|
| 48 |
+
if not all(isinstance(arg, MatrixExpr) for arg in args):
|
| 49 |
+
raise TypeError("Mix of Matrix and Scalar symbols")
|
| 50 |
+
|
| 51 |
+
obj = Basic.__new__(cls, *args)
|
| 52 |
+
|
| 53 |
+
if check is not None:
|
| 54 |
+
sympy_deprecation_warning(
|
| 55 |
+
"Passing check to MatAdd is deprecated and the check argument will be removed in a future version.",
|
| 56 |
+
deprecated_since_version="1.11",
|
| 57 |
+
active_deprecations_target='remove-check-argument-from-matrix-operations')
|
| 58 |
+
|
| 59 |
+
if check is not False:
|
| 60 |
+
validate(*args)
|
| 61 |
+
|
| 62 |
+
if evaluate:
|
| 63 |
+
obj = cls._evaluate(obj)
|
| 64 |
+
|
| 65 |
+
return obj
|
| 66 |
+
|
| 67 |
+
@classmethod
|
| 68 |
+
def _evaluate(cls, expr):
|
| 69 |
+
return canonicalize(expr)
|
| 70 |
+
|
| 71 |
+
@property
|
| 72 |
+
def shape(self):
|
| 73 |
+
return self.args[0].shape
|
| 74 |
+
|
| 75 |
+
def could_extract_minus_sign(self):
|
| 76 |
+
return _could_extract_minus_sign(self)
|
| 77 |
+
|
| 78 |
+
def expand(self, **kwargs):
|
| 79 |
+
expanded = super(MatAdd, self).expand(**kwargs)
|
| 80 |
+
return self._evaluate(expanded)
|
| 81 |
+
|
| 82 |
+
def _entry(self, i, j, **kwargs):
|
| 83 |
+
return Add(*[arg._entry(i, j, **kwargs) for arg in self.args])
|
| 84 |
+
|
| 85 |
+
def _eval_transpose(self):
|
| 86 |
+
return MatAdd(*[transpose(arg) for arg in self.args]).doit()
|
| 87 |
+
|
| 88 |
+
def _eval_adjoint(self):
|
| 89 |
+
return MatAdd(*[adjoint(arg) for arg in self.args]).doit()
|
| 90 |
+
|
| 91 |
+
def _eval_trace(self):
|
| 92 |
+
from .trace import trace
|
| 93 |
+
return Add(*[trace(arg) for arg in self.args]).doit()
|
| 94 |
+
|
| 95 |
+
def doit(self, **hints):
|
| 96 |
+
deep = hints.get('deep', True)
|
| 97 |
+
if deep:
|
| 98 |
+
args = [arg.doit(**hints) for arg in self.args]
|
| 99 |
+
else:
|
| 100 |
+
args = self.args
|
| 101 |
+
return canonicalize(MatAdd(*args))
|
| 102 |
+
|
| 103 |
+
def _eval_derivative_matrix_lines(self, x):
|
| 104 |
+
add_lines = [arg._eval_derivative_matrix_lines(x) for arg in self.args]
|
| 105 |
+
return [j for i in add_lines for j in i]
|
| 106 |
+
|
| 107 |
+
add.register_handlerclass((Add, MatAdd), MatAdd)
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
factor_of = lambda arg: arg.as_coeff_mmul()[0]
|
| 111 |
+
matrix_of = lambda arg: unpack(arg.as_coeff_mmul()[1])
|
| 112 |
+
def combine(cnt, mat):
|
| 113 |
+
if cnt == 1:
|
| 114 |
+
return mat
|
| 115 |
+
else:
|
| 116 |
+
return cnt * mat
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
def merge_explicit(matadd):
|
| 120 |
+
""" Merge explicit MatrixBase arguments
|
| 121 |
+
|
| 122 |
+
Examples
|
| 123 |
+
========
|
| 124 |
+
|
| 125 |
+
>>> from sympy import MatrixSymbol, eye, Matrix, MatAdd, pprint
|
| 126 |
+
>>> from sympy.matrices.expressions.matadd import merge_explicit
|
| 127 |
+
>>> A = MatrixSymbol('A', 2, 2)
|
| 128 |
+
>>> B = eye(2)
|
| 129 |
+
>>> C = Matrix([[1, 2], [3, 4]])
|
| 130 |
+
>>> X = MatAdd(A, B, C)
|
| 131 |
+
>>> pprint(X)
|
| 132 |
+
[1 0] [1 2]
|
| 133 |
+
A + [ ] + [ ]
|
| 134 |
+
[0 1] [3 4]
|
| 135 |
+
>>> pprint(merge_explicit(X))
|
| 136 |
+
[2 2]
|
| 137 |
+
A + [ ]
|
| 138 |
+
[3 5]
|
| 139 |
+
"""
|
| 140 |
+
groups = sift(matadd.args, lambda arg: isinstance(arg, MatrixBase))
|
| 141 |
+
if len(groups[True]) > 1:
|
| 142 |
+
return MatAdd(*(groups[False] + [reduce(operator.add, groups[True])]))
|
| 143 |
+
else:
|
| 144 |
+
return matadd
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
rules = (rm_id(lambda x: x == 0 or isinstance(x, ZeroMatrix)),
|
| 148 |
+
unpack,
|
| 149 |
+
flatten,
|
| 150 |
+
glom(matrix_of, factor_of, combine),
|
| 151 |
+
merge_explicit,
|
| 152 |
+
sort(default_sort_key))
|
| 153 |
+
|
| 154 |
+
canonicalize = exhaust(condition(lambda x: isinstance(x, MatAdd),
|
| 155 |
+
do_one(*rules)))
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/matexpr.py
ADDED
|
@@ -0,0 +1,888 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
from functools import wraps
|
| 3 |
+
|
| 4 |
+
from sympy.core import S, Integer, Basic, Mul, Add
|
| 5 |
+
from sympy.core.assumptions import check_assumptions
|
| 6 |
+
from sympy.core.decorators import call_highest_priority
|
| 7 |
+
from sympy.core.expr import Expr, ExprBuilder
|
| 8 |
+
from sympy.core.logic import FuzzyBool
|
| 9 |
+
from sympy.core.symbol import Str, Dummy, symbols, Symbol
|
| 10 |
+
from sympy.core.sympify import SympifyError, _sympify
|
| 11 |
+
from sympy.external.gmpy import SYMPY_INTS
|
| 12 |
+
from sympy.functions import conjugate, adjoint
|
| 13 |
+
from sympy.functions.special.tensor_functions import KroneckerDelta
|
| 14 |
+
from sympy.matrices.exceptions import NonSquareMatrixError
|
| 15 |
+
from sympy.matrices.kind import MatrixKind
|
| 16 |
+
from sympy.matrices.matrixbase import MatrixBase
|
| 17 |
+
from sympy.multipledispatch import dispatch
|
| 18 |
+
from sympy.utilities.misc import filldedent
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def _sympifyit(arg, retval=None):
|
| 22 |
+
# This version of _sympifyit sympifies MutableMatrix objects
|
| 23 |
+
def deco(func):
|
| 24 |
+
@wraps(func)
|
| 25 |
+
def __sympifyit_wrapper(a, b):
|
| 26 |
+
try:
|
| 27 |
+
b = _sympify(b)
|
| 28 |
+
return func(a, b)
|
| 29 |
+
except SympifyError:
|
| 30 |
+
return retval
|
| 31 |
+
|
| 32 |
+
return __sympifyit_wrapper
|
| 33 |
+
|
| 34 |
+
return deco
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
class MatrixExpr(Expr):
|
| 38 |
+
"""Superclass for Matrix Expressions
|
| 39 |
+
|
| 40 |
+
MatrixExprs represent abstract matrices, linear transformations represented
|
| 41 |
+
within a particular basis.
|
| 42 |
+
|
| 43 |
+
Examples
|
| 44 |
+
========
|
| 45 |
+
|
| 46 |
+
>>> from sympy import MatrixSymbol
|
| 47 |
+
>>> A = MatrixSymbol('A', 3, 3)
|
| 48 |
+
>>> y = MatrixSymbol('y', 3, 1)
|
| 49 |
+
>>> x = (A.T*A).I * A * y
|
| 50 |
+
|
| 51 |
+
See Also
|
| 52 |
+
========
|
| 53 |
+
|
| 54 |
+
MatrixSymbol, MatAdd, MatMul, Transpose, Inverse
|
| 55 |
+
"""
|
| 56 |
+
__slots__: tuple[str, ...] = ()
|
| 57 |
+
|
| 58 |
+
# Should not be considered iterable by the
|
| 59 |
+
# sympy.utilities.iterables.iterable function. Subclass that actually are
|
| 60 |
+
# iterable (i.e., explicit matrices) should set this to True.
|
| 61 |
+
_iterable = False
|
| 62 |
+
|
| 63 |
+
_op_priority = 11.0
|
| 64 |
+
|
| 65 |
+
is_Matrix: bool = True
|
| 66 |
+
is_MatrixExpr: bool = True
|
| 67 |
+
is_Identity: FuzzyBool = None
|
| 68 |
+
is_Inverse = False
|
| 69 |
+
is_Transpose = False
|
| 70 |
+
is_ZeroMatrix = False
|
| 71 |
+
is_MatAdd = False
|
| 72 |
+
is_MatMul = False
|
| 73 |
+
|
| 74 |
+
is_commutative = False
|
| 75 |
+
is_number = False
|
| 76 |
+
is_symbol = False
|
| 77 |
+
is_scalar = False
|
| 78 |
+
|
| 79 |
+
kind: MatrixKind = MatrixKind()
|
| 80 |
+
|
| 81 |
+
def __new__(cls, *args, **kwargs):
|
| 82 |
+
args = map(_sympify, args)
|
| 83 |
+
return Basic.__new__(cls, *args, **kwargs)
|
| 84 |
+
|
| 85 |
+
# The following is adapted from the core Expr object
|
| 86 |
+
|
| 87 |
+
@property
|
| 88 |
+
def shape(self) -> tuple[Expr, Expr]:
|
| 89 |
+
raise NotImplementedError
|
| 90 |
+
|
| 91 |
+
@property
|
| 92 |
+
def _add_handler(self):
|
| 93 |
+
return MatAdd
|
| 94 |
+
|
| 95 |
+
@property
|
| 96 |
+
def _mul_handler(self):
|
| 97 |
+
return MatMul
|
| 98 |
+
|
| 99 |
+
def __neg__(self):
|
| 100 |
+
return MatMul(S.NegativeOne, self).doit()
|
| 101 |
+
|
| 102 |
+
def __abs__(self):
|
| 103 |
+
raise NotImplementedError
|
| 104 |
+
|
| 105 |
+
@_sympifyit('other', NotImplemented)
|
| 106 |
+
@call_highest_priority('__radd__')
|
| 107 |
+
def __add__(self, other):
|
| 108 |
+
return MatAdd(self, other).doit()
|
| 109 |
+
|
| 110 |
+
@_sympifyit('other', NotImplemented)
|
| 111 |
+
@call_highest_priority('__add__')
|
| 112 |
+
def __radd__(self, other):
|
| 113 |
+
return MatAdd(other, self).doit()
|
| 114 |
+
|
| 115 |
+
@_sympifyit('other', NotImplemented)
|
| 116 |
+
@call_highest_priority('__rsub__')
|
| 117 |
+
def __sub__(self, other):
|
| 118 |
+
return MatAdd(self, -other).doit()
|
| 119 |
+
|
| 120 |
+
@_sympifyit('other', NotImplemented)
|
| 121 |
+
@call_highest_priority('__sub__')
|
| 122 |
+
def __rsub__(self, other):
|
| 123 |
+
return MatAdd(other, -self).doit()
|
| 124 |
+
|
| 125 |
+
@_sympifyit('other', NotImplemented)
|
| 126 |
+
@call_highest_priority('__rmul__')
|
| 127 |
+
def __mul__(self, other):
|
| 128 |
+
return MatMul(self, other).doit()
|
| 129 |
+
|
| 130 |
+
@_sympifyit('other', NotImplemented)
|
| 131 |
+
@call_highest_priority('__rmul__')
|
| 132 |
+
def __matmul__(self, other):
|
| 133 |
+
return MatMul(self, other).doit()
|
| 134 |
+
|
| 135 |
+
@_sympifyit('other', NotImplemented)
|
| 136 |
+
@call_highest_priority('__mul__')
|
| 137 |
+
def __rmul__(self, other):
|
| 138 |
+
return MatMul(other, self).doit()
|
| 139 |
+
|
| 140 |
+
@_sympifyit('other', NotImplemented)
|
| 141 |
+
@call_highest_priority('__mul__')
|
| 142 |
+
def __rmatmul__(self, other):
|
| 143 |
+
return MatMul(other, self).doit()
|
| 144 |
+
|
| 145 |
+
@_sympifyit('other', NotImplemented)
|
| 146 |
+
@call_highest_priority('__rpow__')
|
| 147 |
+
def __pow__(self, other):
|
| 148 |
+
return MatPow(self, other).doit()
|
| 149 |
+
|
| 150 |
+
@_sympifyit('other', NotImplemented)
|
| 151 |
+
@call_highest_priority('__pow__')
|
| 152 |
+
def __rpow__(self, other):
|
| 153 |
+
raise NotImplementedError("Matrix Power not defined")
|
| 154 |
+
|
| 155 |
+
@_sympifyit('other', NotImplemented)
|
| 156 |
+
@call_highest_priority('__rtruediv__')
|
| 157 |
+
def __truediv__(self, other):
|
| 158 |
+
return self * other**S.NegativeOne
|
| 159 |
+
|
| 160 |
+
@_sympifyit('other', NotImplemented)
|
| 161 |
+
@call_highest_priority('__truediv__')
|
| 162 |
+
def __rtruediv__(self, other):
|
| 163 |
+
raise NotImplementedError()
|
| 164 |
+
#return MatMul(other, Pow(self, S.NegativeOne))
|
| 165 |
+
|
| 166 |
+
@property
|
| 167 |
+
def rows(self):
|
| 168 |
+
return self.shape[0]
|
| 169 |
+
|
| 170 |
+
@property
|
| 171 |
+
def cols(self):
|
| 172 |
+
return self.shape[1]
|
| 173 |
+
|
| 174 |
+
@property
|
| 175 |
+
def is_square(self) -> bool | None:
|
| 176 |
+
rows, cols = self.shape
|
| 177 |
+
if isinstance(rows, Integer) and isinstance(cols, Integer):
|
| 178 |
+
return rows == cols
|
| 179 |
+
if rows == cols:
|
| 180 |
+
return True
|
| 181 |
+
return None
|
| 182 |
+
|
| 183 |
+
def _eval_conjugate(self):
|
| 184 |
+
from sympy.matrices.expressions.adjoint import Adjoint
|
| 185 |
+
return Adjoint(Transpose(self))
|
| 186 |
+
|
| 187 |
+
def as_real_imag(self, deep=True, **hints):
|
| 188 |
+
return self._eval_as_real_imag()
|
| 189 |
+
|
| 190 |
+
def _eval_as_real_imag(self):
|
| 191 |
+
real = S.Half * (self + self._eval_conjugate())
|
| 192 |
+
im = (self - self._eval_conjugate())/(2*S.ImaginaryUnit)
|
| 193 |
+
return (real, im)
|
| 194 |
+
|
| 195 |
+
def _eval_inverse(self):
|
| 196 |
+
return Inverse(self)
|
| 197 |
+
|
| 198 |
+
def _eval_determinant(self):
|
| 199 |
+
return Determinant(self)
|
| 200 |
+
|
| 201 |
+
def _eval_transpose(self):
|
| 202 |
+
return Transpose(self)
|
| 203 |
+
|
| 204 |
+
def _eval_trace(self):
|
| 205 |
+
return None
|
| 206 |
+
|
| 207 |
+
def _eval_power(self, exp):
|
| 208 |
+
"""
|
| 209 |
+
Override this in sub-classes to implement simplification of powers. The cases where the exponent
|
| 210 |
+
is -1, 0, 1 are already covered in MatPow.doit(), so implementations can exclude these cases.
|
| 211 |
+
"""
|
| 212 |
+
return MatPow(self, exp)
|
| 213 |
+
|
| 214 |
+
def _eval_simplify(self, **kwargs):
|
| 215 |
+
if self.is_Atom:
|
| 216 |
+
return self
|
| 217 |
+
else:
|
| 218 |
+
from sympy.simplify import simplify
|
| 219 |
+
return self.func(*[simplify(x, **kwargs) for x in self.args])
|
| 220 |
+
|
| 221 |
+
def _eval_adjoint(self):
|
| 222 |
+
from sympy.matrices.expressions.adjoint import Adjoint
|
| 223 |
+
return Adjoint(self)
|
| 224 |
+
|
| 225 |
+
def _eval_derivative_n_times(self, x, n):
|
| 226 |
+
return Basic._eval_derivative_n_times(self, x, n)
|
| 227 |
+
|
| 228 |
+
def _eval_derivative(self, x):
|
| 229 |
+
# `x` is a scalar:
|
| 230 |
+
if self.has(x):
|
| 231 |
+
# See if there are other methods using it:
|
| 232 |
+
return super()._eval_derivative(x)
|
| 233 |
+
else:
|
| 234 |
+
return ZeroMatrix(*self.shape)
|
| 235 |
+
|
| 236 |
+
@classmethod
|
| 237 |
+
def _check_dim(cls, dim):
|
| 238 |
+
"""Helper function to check invalid matrix dimensions"""
|
| 239 |
+
ok = not dim.is_Float and check_assumptions(
|
| 240 |
+
dim, integer=True, nonnegative=True)
|
| 241 |
+
if ok is False:
|
| 242 |
+
raise ValueError(
|
| 243 |
+
"The dimension specification {} should be "
|
| 244 |
+
"a nonnegative integer.".format(dim))
|
| 245 |
+
|
| 246 |
+
|
| 247 |
+
def _entry(self, i, j, **kwargs):
|
| 248 |
+
raise NotImplementedError(
|
| 249 |
+
"Indexing not implemented for %s" % self.__class__.__name__)
|
| 250 |
+
|
| 251 |
+
def adjoint(self):
|
| 252 |
+
return adjoint(self)
|
| 253 |
+
|
| 254 |
+
def as_coeff_Mul(self, rational=False):
|
| 255 |
+
"""Efficiently extract the coefficient of a product."""
|
| 256 |
+
return S.One, self
|
| 257 |
+
|
| 258 |
+
def conjugate(self):
|
| 259 |
+
return conjugate(self)
|
| 260 |
+
|
| 261 |
+
def transpose(self):
|
| 262 |
+
from sympy.matrices.expressions.transpose import transpose
|
| 263 |
+
return transpose(self)
|
| 264 |
+
|
| 265 |
+
@property
|
| 266 |
+
def T(self):
|
| 267 |
+
'''Matrix transposition'''
|
| 268 |
+
return self.transpose()
|
| 269 |
+
|
| 270 |
+
def inverse(self):
|
| 271 |
+
if self.is_square is False:
|
| 272 |
+
raise NonSquareMatrixError('Inverse of non-square matrix')
|
| 273 |
+
return self._eval_inverse()
|
| 274 |
+
|
| 275 |
+
def inv(self):
|
| 276 |
+
return self.inverse()
|
| 277 |
+
|
| 278 |
+
def det(self):
|
| 279 |
+
from sympy.matrices.expressions.determinant import det
|
| 280 |
+
return det(self)
|
| 281 |
+
|
| 282 |
+
@property
|
| 283 |
+
def I(self):
|
| 284 |
+
return self.inverse()
|
| 285 |
+
|
| 286 |
+
def valid_index(self, i, j):
|
| 287 |
+
def is_valid(idx):
|
| 288 |
+
return isinstance(idx, (int, Integer, Symbol, Expr))
|
| 289 |
+
return (is_valid(i) and is_valid(j) and
|
| 290 |
+
(self.rows is None or
|
| 291 |
+
(i >= -self.rows) != False and (i < self.rows) != False) and
|
| 292 |
+
(j >= -self.cols) != False and (j < self.cols) != False)
|
| 293 |
+
|
| 294 |
+
def __getitem__(self, key):
|
| 295 |
+
if not isinstance(key, tuple) and isinstance(key, slice):
|
| 296 |
+
from sympy.matrices.expressions.slice import MatrixSlice
|
| 297 |
+
return MatrixSlice(self, key, (0, None, 1))
|
| 298 |
+
if isinstance(key, tuple) and len(key) == 2:
|
| 299 |
+
i, j = key
|
| 300 |
+
if isinstance(i, slice) or isinstance(j, slice):
|
| 301 |
+
from sympy.matrices.expressions.slice import MatrixSlice
|
| 302 |
+
return MatrixSlice(self, i, j)
|
| 303 |
+
i, j = _sympify(i), _sympify(j)
|
| 304 |
+
if self.valid_index(i, j) != False:
|
| 305 |
+
return self._entry(i, j)
|
| 306 |
+
else:
|
| 307 |
+
raise IndexError("Invalid indices (%s, %s)" % (i, j))
|
| 308 |
+
elif isinstance(key, (SYMPY_INTS, Integer)):
|
| 309 |
+
# row-wise decomposition of matrix
|
| 310 |
+
rows, cols = self.shape
|
| 311 |
+
# allow single indexing if number of columns is known
|
| 312 |
+
if not isinstance(cols, Integer):
|
| 313 |
+
raise IndexError(filldedent('''
|
| 314 |
+
Single indexing is only supported when the number
|
| 315 |
+
of columns is known.'''))
|
| 316 |
+
key = _sympify(key)
|
| 317 |
+
i = key // cols
|
| 318 |
+
j = key % cols
|
| 319 |
+
if self.valid_index(i, j) != False:
|
| 320 |
+
return self._entry(i, j)
|
| 321 |
+
else:
|
| 322 |
+
raise IndexError("Invalid index %s" % key)
|
| 323 |
+
elif isinstance(key, (Symbol, Expr)):
|
| 324 |
+
raise IndexError(filldedent('''
|
| 325 |
+
Only integers may be used when addressing the matrix
|
| 326 |
+
with a single index.'''))
|
| 327 |
+
raise IndexError("Invalid index, wanted %s[i,j]" % self)
|
| 328 |
+
|
| 329 |
+
def _is_shape_symbolic(self) -> bool:
|
| 330 |
+
return (not isinstance(self.rows, (SYMPY_INTS, Integer))
|
| 331 |
+
or not isinstance(self.cols, (SYMPY_INTS, Integer)))
|
| 332 |
+
|
| 333 |
+
def as_explicit(self):
|
| 334 |
+
"""
|
| 335 |
+
Returns a dense Matrix with elements represented explicitly
|
| 336 |
+
|
| 337 |
+
Returns an object of type ImmutableDenseMatrix.
|
| 338 |
+
|
| 339 |
+
Examples
|
| 340 |
+
========
|
| 341 |
+
|
| 342 |
+
>>> from sympy import Identity
|
| 343 |
+
>>> I = Identity(3)
|
| 344 |
+
>>> I
|
| 345 |
+
I
|
| 346 |
+
>>> I.as_explicit()
|
| 347 |
+
Matrix([
|
| 348 |
+
[1, 0, 0],
|
| 349 |
+
[0, 1, 0],
|
| 350 |
+
[0, 0, 1]])
|
| 351 |
+
|
| 352 |
+
See Also
|
| 353 |
+
========
|
| 354 |
+
as_mutable: returns mutable Matrix type
|
| 355 |
+
|
| 356 |
+
"""
|
| 357 |
+
if self._is_shape_symbolic():
|
| 358 |
+
raise ValueError(
|
| 359 |
+
'Matrix with symbolic shape '
|
| 360 |
+
'cannot be represented explicitly.')
|
| 361 |
+
from sympy.matrices.immutable import ImmutableDenseMatrix
|
| 362 |
+
return ImmutableDenseMatrix([[self[i, j]
|
| 363 |
+
for j in range(self.cols)]
|
| 364 |
+
for i in range(self.rows)])
|
| 365 |
+
|
| 366 |
+
def as_mutable(self):
|
| 367 |
+
"""
|
| 368 |
+
Returns a dense, mutable matrix with elements represented explicitly
|
| 369 |
+
|
| 370 |
+
Examples
|
| 371 |
+
========
|
| 372 |
+
|
| 373 |
+
>>> from sympy import Identity
|
| 374 |
+
>>> I = Identity(3)
|
| 375 |
+
>>> I
|
| 376 |
+
I
|
| 377 |
+
>>> I.shape
|
| 378 |
+
(3, 3)
|
| 379 |
+
>>> I.as_mutable()
|
| 380 |
+
Matrix([
|
| 381 |
+
[1, 0, 0],
|
| 382 |
+
[0, 1, 0],
|
| 383 |
+
[0, 0, 1]])
|
| 384 |
+
|
| 385 |
+
See Also
|
| 386 |
+
========
|
| 387 |
+
as_explicit: returns ImmutableDenseMatrix
|
| 388 |
+
"""
|
| 389 |
+
return self.as_explicit().as_mutable()
|
| 390 |
+
|
| 391 |
+
def __array__(self, dtype=object, copy=None):
|
| 392 |
+
if copy is not None and not copy:
|
| 393 |
+
raise TypeError("Cannot implement copy=False when converting Matrix to ndarray")
|
| 394 |
+
from numpy import empty
|
| 395 |
+
a = empty(self.shape, dtype=object)
|
| 396 |
+
for i in range(self.rows):
|
| 397 |
+
for j in range(self.cols):
|
| 398 |
+
a[i, j] = self[i, j]
|
| 399 |
+
return a
|
| 400 |
+
|
| 401 |
+
def equals(self, other):
|
| 402 |
+
"""
|
| 403 |
+
Test elementwise equality between matrices, potentially of different
|
| 404 |
+
types
|
| 405 |
+
|
| 406 |
+
>>> from sympy import Identity, eye
|
| 407 |
+
>>> Identity(3).equals(eye(3))
|
| 408 |
+
True
|
| 409 |
+
"""
|
| 410 |
+
return self.as_explicit().equals(other)
|
| 411 |
+
|
| 412 |
+
def canonicalize(self):
|
| 413 |
+
return self
|
| 414 |
+
|
| 415 |
+
def as_coeff_mmul(self):
|
| 416 |
+
return S.One, MatMul(self)
|
| 417 |
+
|
| 418 |
+
@staticmethod
|
| 419 |
+
def from_index_summation(expr, first_index=None, last_index=None, dimensions=None):
|
| 420 |
+
r"""
|
| 421 |
+
Parse expression of matrices with explicitly summed indices into a
|
| 422 |
+
matrix expression without indices, if possible.
|
| 423 |
+
|
| 424 |
+
This transformation expressed in mathematical notation:
|
| 425 |
+
|
| 426 |
+
`\sum_{j=0}^{N-1} A_{i,j} B_{j,k} \Longrightarrow \mathbf{A}\cdot \mathbf{B}`
|
| 427 |
+
|
| 428 |
+
Optional parameter ``first_index``: specify which free index to use as
|
| 429 |
+
the index starting the expression.
|
| 430 |
+
|
| 431 |
+
Examples
|
| 432 |
+
========
|
| 433 |
+
|
| 434 |
+
>>> from sympy import MatrixSymbol, MatrixExpr, Sum
|
| 435 |
+
>>> from sympy.abc import i, j, k, l, N
|
| 436 |
+
>>> A = MatrixSymbol("A", N, N)
|
| 437 |
+
>>> B = MatrixSymbol("B", N, N)
|
| 438 |
+
>>> expr = Sum(A[i, j]*B[j, k], (j, 0, N-1))
|
| 439 |
+
>>> MatrixExpr.from_index_summation(expr)
|
| 440 |
+
A*B
|
| 441 |
+
|
| 442 |
+
Transposition is detected:
|
| 443 |
+
|
| 444 |
+
>>> expr = Sum(A[j, i]*B[j, k], (j, 0, N-1))
|
| 445 |
+
>>> MatrixExpr.from_index_summation(expr)
|
| 446 |
+
A.T*B
|
| 447 |
+
|
| 448 |
+
Detect the trace:
|
| 449 |
+
|
| 450 |
+
>>> expr = Sum(A[i, i], (i, 0, N-1))
|
| 451 |
+
>>> MatrixExpr.from_index_summation(expr)
|
| 452 |
+
Trace(A)
|
| 453 |
+
|
| 454 |
+
More complicated expressions:
|
| 455 |
+
|
| 456 |
+
>>> expr = Sum(A[i, j]*B[k, j]*A[l, k], (j, 0, N-1), (k, 0, N-1))
|
| 457 |
+
>>> MatrixExpr.from_index_summation(expr)
|
| 458 |
+
A*B.T*A.T
|
| 459 |
+
"""
|
| 460 |
+
from sympy.tensor.array.expressions.from_indexed_to_array import convert_indexed_to_array
|
| 461 |
+
from sympy.tensor.array.expressions.from_array_to_matrix import convert_array_to_matrix
|
| 462 |
+
first_indices = []
|
| 463 |
+
if first_index is not None:
|
| 464 |
+
first_indices.append(first_index)
|
| 465 |
+
if last_index is not None:
|
| 466 |
+
first_indices.append(last_index)
|
| 467 |
+
arr = convert_indexed_to_array(expr, first_indices=first_indices)
|
| 468 |
+
return convert_array_to_matrix(arr)
|
| 469 |
+
|
| 470 |
+
def applyfunc(self, func):
|
| 471 |
+
from .applyfunc import ElementwiseApplyFunction
|
| 472 |
+
return ElementwiseApplyFunction(func, self)
|
| 473 |
+
|
| 474 |
+
|
| 475 |
+
@dispatch(MatrixExpr, Expr)
|
| 476 |
+
def _eval_is_eq(lhs, rhs): # noqa:F811
|
| 477 |
+
return False
|
| 478 |
+
|
| 479 |
+
@dispatch(MatrixExpr, MatrixExpr) # type: ignore
|
| 480 |
+
def _eval_is_eq(lhs, rhs): # noqa:F811
|
| 481 |
+
if lhs.shape != rhs.shape:
|
| 482 |
+
return False
|
| 483 |
+
if (lhs - rhs).is_ZeroMatrix:
|
| 484 |
+
return True
|
| 485 |
+
|
| 486 |
+
def get_postprocessor(cls):
|
| 487 |
+
def _postprocessor(expr):
|
| 488 |
+
# To avoid circular imports, we can't have MatMul/MatAdd on the top level
|
| 489 |
+
mat_class = {Mul: MatMul, Add: MatAdd}[cls]
|
| 490 |
+
nonmatrices = []
|
| 491 |
+
matrices = []
|
| 492 |
+
for term in expr.args:
|
| 493 |
+
if isinstance(term, MatrixExpr):
|
| 494 |
+
matrices.append(term)
|
| 495 |
+
else:
|
| 496 |
+
nonmatrices.append(term)
|
| 497 |
+
|
| 498 |
+
if not matrices:
|
| 499 |
+
return cls._from_args(nonmatrices)
|
| 500 |
+
|
| 501 |
+
if nonmatrices:
|
| 502 |
+
if cls == Mul:
|
| 503 |
+
for i in range(len(matrices)):
|
| 504 |
+
if not matrices[i].is_MatrixExpr:
|
| 505 |
+
# If one of the matrices explicit, absorb the scalar into it
|
| 506 |
+
# (doit will combine all explicit matrices into one, so it
|
| 507 |
+
# doesn't matter which)
|
| 508 |
+
matrices[i] = matrices[i].__mul__(cls._from_args(nonmatrices))
|
| 509 |
+
nonmatrices = []
|
| 510 |
+
break
|
| 511 |
+
|
| 512 |
+
else:
|
| 513 |
+
# Maintain the ability to create Add(scalar, matrix) without
|
| 514 |
+
# raising an exception. That way different algorithms can
|
| 515 |
+
# replace matrix expressions with non-commutative symbols to
|
| 516 |
+
# manipulate them like non-commutative scalars.
|
| 517 |
+
return cls._from_args(nonmatrices + [mat_class(*matrices).doit(deep=False)])
|
| 518 |
+
|
| 519 |
+
if mat_class == MatAdd:
|
| 520 |
+
return mat_class(*matrices).doit(deep=False)
|
| 521 |
+
return mat_class(cls._from_args(nonmatrices), *matrices).doit(deep=False)
|
| 522 |
+
return _postprocessor
|
| 523 |
+
|
| 524 |
+
|
| 525 |
+
Basic._constructor_postprocessor_mapping[MatrixExpr] = {
|
| 526 |
+
"Mul": [get_postprocessor(Mul)],
|
| 527 |
+
"Add": [get_postprocessor(Add)],
|
| 528 |
+
}
|
| 529 |
+
|
| 530 |
+
|
| 531 |
+
def _matrix_derivative(expr, x, old_algorithm=False):
|
| 532 |
+
|
| 533 |
+
if isinstance(expr, MatrixBase) or isinstance(x, MatrixBase):
|
| 534 |
+
# Do not use array expressions for explicit matrices:
|
| 535 |
+
old_algorithm = True
|
| 536 |
+
|
| 537 |
+
if old_algorithm:
|
| 538 |
+
return _matrix_derivative_old_algorithm(expr, x)
|
| 539 |
+
|
| 540 |
+
from sympy.tensor.array.expressions.from_matrix_to_array import convert_matrix_to_array
|
| 541 |
+
from sympy.tensor.array.expressions.arrayexpr_derivatives import array_derive
|
| 542 |
+
from sympy.tensor.array.expressions.from_array_to_matrix import convert_array_to_matrix
|
| 543 |
+
|
| 544 |
+
array_expr = convert_matrix_to_array(expr)
|
| 545 |
+
diff_array_expr = array_derive(array_expr, x)
|
| 546 |
+
diff_matrix_expr = convert_array_to_matrix(diff_array_expr)
|
| 547 |
+
return diff_matrix_expr
|
| 548 |
+
|
| 549 |
+
|
| 550 |
+
def _matrix_derivative_old_algorithm(expr, x):
|
| 551 |
+
from sympy.tensor.array.array_derivatives import ArrayDerivative
|
| 552 |
+
lines = expr._eval_derivative_matrix_lines(x)
|
| 553 |
+
|
| 554 |
+
parts = [i.build() for i in lines]
|
| 555 |
+
|
| 556 |
+
from sympy.tensor.array.expressions.from_array_to_matrix import convert_array_to_matrix
|
| 557 |
+
|
| 558 |
+
parts = [[convert_array_to_matrix(j) for j in i] for i in parts]
|
| 559 |
+
|
| 560 |
+
def _get_shape(elem):
|
| 561 |
+
if isinstance(elem, MatrixExpr):
|
| 562 |
+
return elem.shape
|
| 563 |
+
return 1, 1
|
| 564 |
+
|
| 565 |
+
def get_rank(parts):
|
| 566 |
+
return sum(j not in (1, None) for i in parts for j in _get_shape(i))
|
| 567 |
+
|
| 568 |
+
ranks = [get_rank(i) for i in parts]
|
| 569 |
+
rank = ranks[0]
|
| 570 |
+
|
| 571 |
+
def contract_one_dims(parts):
|
| 572 |
+
if len(parts) == 1:
|
| 573 |
+
return parts[0]
|
| 574 |
+
else:
|
| 575 |
+
p1, p2 = parts[:2]
|
| 576 |
+
if p2.is_Matrix:
|
| 577 |
+
p2 = p2.T
|
| 578 |
+
if p1 == Identity(1):
|
| 579 |
+
pbase = p2
|
| 580 |
+
elif p2 == Identity(1):
|
| 581 |
+
pbase = p1
|
| 582 |
+
else:
|
| 583 |
+
pbase = p1*p2
|
| 584 |
+
if len(parts) == 2:
|
| 585 |
+
return pbase
|
| 586 |
+
else: # len(parts) > 2
|
| 587 |
+
if pbase.is_Matrix:
|
| 588 |
+
raise ValueError("")
|
| 589 |
+
return pbase*Mul.fromiter(parts[2:])
|
| 590 |
+
|
| 591 |
+
if rank <= 2:
|
| 592 |
+
return Add.fromiter([contract_one_dims(i) for i in parts])
|
| 593 |
+
|
| 594 |
+
return ArrayDerivative(expr, x)
|
| 595 |
+
|
| 596 |
+
|
| 597 |
+
class MatrixElement(Expr):
|
| 598 |
+
parent = property(lambda self: self.args[0])
|
| 599 |
+
i = property(lambda self: self.args[1])
|
| 600 |
+
j = property(lambda self: self.args[2])
|
| 601 |
+
_diff_wrt = True
|
| 602 |
+
is_symbol = True
|
| 603 |
+
is_commutative = True
|
| 604 |
+
|
| 605 |
+
def __new__(cls, name, n, m):
|
| 606 |
+
n, m = map(_sympify, (n, m))
|
| 607 |
+
if isinstance(name, str):
|
| 608 |
+
name = Symbol(name)
|
| 609 |
+
else:
|
| 610 |
+
if isinstance(name, MatrixBase):
|
| 611 |
+
if n.is_Integer and m.is_Integer:
|
| 612 |
+
return name[n, m]
|
| 613 |
+
name = _sympify(name) # change mutable into immutable
|
| 614 |
+
else:
|
| 615 |
+
name = _sympify(name)
|
| 616 |
+
if not isinstance(name.kind, MatrixKind):
|
| 617 |
+
raise TypeError("First argument of MatrixElement should be a matrix")
|
| 618 |
+
if not getattr(name, 'valid_index', lambda n, m: True)(n, m):
|
| 619 |
+
raise IndexError('indices out of range')
|
| 620 |
+
obj = Expr.__new__(cls, name, n, m)
|
| 621 |
+
return obj
|
| 622 |
+
|
| 623 |
+
@property
|
| 624 |
+
def symbol(self):
|
| 625 |
+
return self.args[0]
|
| 626 |
+
|
| 627 |
+
def doit(self, **hints):
|
| 628 |
+
deep = hints.get('deep', True)
|
| 629 |
+
if deep:
|
| 630 |
+
args = [arg.doit(**hints) for arg in self.args]
|
| 631 |
+
else:
|
| 632 |
+
args = self.args
|
| 633 |
+
return args[0][args[1], args[2]]
|
| 634 |
+
|
| 635 |
+
@property
|
| 636 |
+
def indices(self):
|
| 637 |
+
return self.args[1:]
|
| 638 |
+
|
| 639 |
+
def _eval_derivative(self, v):
|
| 640 |
+
|
| 641 |
+
if not isinstance(v, MatrixElement):
|
| 642 |
+
return self.parent.diff(v)[self.i, self.j]
|
| 643 |
+
|
| 644 |
+
M = self.args[0]
|
| 645 |
+
|
| 646 |
+
m, n = self.parent.shape
|
| 647 |
+
|
| 648 |
+
if M == v.args[0]:
|
| 649 |
+
return KroneckerDelta(self.args[1], v.args[1], (0, m-1)) * \
|
| 650 |
+
KroneckerDelta(self.args[2], v.args[2], (0, n-1))
|
| 651 |
+
|
| 652 |
+
if isinstance(M, Inverse):
|
| 653 |
+
from sympy.concrete.summations import Sum
|
| 654 |
+
i, j = self.args[1:]
|
| 655 |
+
i1, i2 = symbols("z1, z2", cls=Dummy)
|
| 656 |
+
Y = M.args[0]
|
| 657 |
+
r1, r2 = Y.shape
|
| 658 |
+
return -Sum(M[i, i1]*Y[i1, i2].diff(v)*M[i2, j], (i1, 0, r1-1), (i2, 0, r2-1))
|
| 659 |
+
|
| 660 |
+
if self.has(v.args[0]):
|
| 661 |
+
return None
|
| 662 |
+
|
| 663 |
+
return S.Zero
|
| 664 |
+
|
| 665 |
+
|
| 666 |
+
class MatrixSymbol(MatrixExpr):
|
| 667 |
+
"""Symbolic representation of a Matrix object
|
| 668 |
+
|
| 669 |
+
Creates a SymPy Symbol to represent a Matrix. This matrix has a shape and
|
| 670 |
+
can be included in Matrix Expressions
|
| 671 |
+
|
| 672 |
+
Examples
|
| 673 |
+
========
|
| 674 |
+
|
| 675 |
+
>>> from sympy import MatrixSymbol, Identity
|
| 676 |
+
>>> A = MatrixSymbol('A', 3, 4) # A 3 by 4 Matrix
|
| 677 |
+
>>> B = MatrixSymbol('B', 4, 3) # A 4 by 3 Matrix
|
| 678 |
+
>>> A.shape
|
| 679 |
+
(3, 4)
|
| 680 |
+
>>> 2*A*B + Identity(3)
|
| 681 |
+
I + 2*A*B
|
| 682 |
+
"""
|
| 683 |
+
is_commutative = False
|
| 684 |
+
is_symbol = True
|
| 685 |
+
_diff_wrt = True
|
| 686 |
+
|
| 687 |
+
def __new__(cls, name, n, m):
|
| 688 |
+
n, m = _sympify(n), _sympify(m)
|
| 689 |
+
|
| 690 |
+
cls._check_dim(m)
|
| 691 |
+
cls._check_dim(n)
|
| 692 |
+
|
| 693 |
+
if isinstance(name, str):
|
| 694 |
+
name = Str(name)
|
| 695 |
+
obj = Basic.__new__(cls, name, n, m)
|
| 696 |
+
return obj
|
| 697 |
+
|
| 698 |
+
@property
|
| 699 |
+
def shape(self):
|
| 700 |
+
return self.args[1], self.args[2]
|
| 701 |
+
|
| 702 |
+
@property
|
| 703 |
+
def name(self):
|
| 704 |
+
return self.args[0].name
|
| 705 |
+
|
| 706 |
+
def _entry(self, i, j, **kwargs):
|
| 707 |
+
return MatrixElement(self, i, j)
|
| 708 |
+
|
| 709 |
+
@property
|
| 710 |
+
def free_symbols(self):
|
| 711 |
+
return {self}
|
| 712 |
+
|
| 713 |
+
def _eval_simplify(self, **kwargs):
|
| 714 |
+
return self
|
| 715 |
+
|
| 716 |
+
def _eval_derivative(self, x):
|
| 717 |
+
# x is a scalar:
|
| 718 |
+
return ZeroMatrix(self.shape[0], self.shape[1])
|
| 719 |
+
|
| 720 |
+
def _eval_derivative_matrix_lines(self, x):
|
| 721 |
+
if self != x:
|
| 722 |
+
first = ZeroMatrix(x.shape[0], self.shape[0]) if self.shape[0] != 1 else S.Zero
|
| 723 |
+
second = ZeroMatrix(x.shape[1], self.shape[1]) if self.shape[1] != 1 else S.Zero
|
| 724 |
+
return [_LeftRightArgs(
|
| 725 |
+
[first, second],
|
| 726 |
+
)]
|
| 727 |
+
else:
|
| 728 |
+
first = Identity(self.shape[0]) if self.shape[0] != 1 else S.One
|
| 729 |
+
second = Identity(self.shape[1]) if self.shape[1] != 1 else S.One
|
| 730 |
+
return [_LeftRightArgs(
|
| 731 |
+
[first, second],
|
| 732 |
+
)]
|
| 733 |
+
|
| 734 |
+
|
| 735 |
+
def matrix_symbols(expr):
|
| 736 |
+
return [sym for sym in expr.free_symbols if sym.is_Matrix]
|
| 737 |
+
|
| 738 |
+
|
| 739 |
+
class _LeftRightArgs:
|
| 740 |
+
r"""
|
| 741 |
+
Helper class to compute matrix derivatives.
|
| 742 |
+
|
| 743 |
+
The logic: when an expression is derived by a matrix `X_{mn}`, two lines of
|
| 744 |
+
matrix multiplications are created: the one contracted to `m` (first line),
|
| 745 |
+
and the one contracted to `n` (second line).
|
| 746 |
+
|
| 747 |
+
Transposition flips the side by which new matrices are connected to the
|
| 748 |
+
lines.
|
| 749 |
+
|
| 750 |
+
The trace connects the end of the two lines.
|
| 751 |
+
"""
|
| 752 |
+
|
| 753 |
+
def __init__(self, lines, higher=S.One):
|
| 754 |
+
self._lines = list(lines)
|
| 755 |
+
self._first_pointer_parent = self._lines
|
| 756 |
+
self._first_pointer_index = 0
|
| 757 |
+
self._first_line_index = 0
|
| 758 |
+
self._second_pointer_parent = self._lines
|
| 759 |
+
self._second_pointer_index = 1
|
| 760 |
+
self._second_line_index = 1
|
| 761 |
+
self.higher = higher
|
| 762 |
+
|
| 763 |
+
@property
|
| 764 |
+
def first_pointer(self):
|
| 765 |
+
return self._first_pointer_parent[self._first_pointer_index]
|
| 766 |
+
|
| 767 |
+
@first_pointer.setter
|
| 768 |
+
def first_pointer(self, value):
|
| 769 |
+
self._first_pointer_parent[self._first_pointer_index] = value
|
| 770 |
+
|
| 771 |
+
@property
|
| 772 |
+
def second_pointer(self):
|
| 773 |
+
return self._second_pointer_parent[self._second_pointer_index]
|
| 774 |
+
|
| 775 |
+
@second_pointer.setter
|
| 776 |
+
def second_pointer(self, value):
|
| 777 |
+
self._second_pointer_parent[self._second_pointer_index] = value
|
| 778 |
+
|
| 779 |
+
def __repr__(self):
|
| 780 |
+
built = [self._build(i) for i in self._lines]
|
| 781 |
+
return "_LeftRightArgs(lines=%s, higher=%s)" % (
|
| 782 |
+
built,
|
| 783 |
+
self.higher,
|
| 784 |
+
)
|
| 785 |
+
|
| 786 |
+
def transpose(self):
|
| 787 |
+
self._first_pointer_parent, self._second_pointer_parent = self._second_pointer_parent, self._first_pointer_parent
|
| 788 |
+
self._first_pointer_index, self._second_pointer_index = self._second_pointer_index, self._first_pointer_index
|
| 789 |
+
self._first_line_index, self._second_line_index = self._second_line_index, self._first_line_index
|
| 790 |
+
return self
|
| 791 |
+
|
| 792 |
+
@staticmethod
|
| 793 |
+
def _build(expr):
|
| 794 |
+
if isinstance(expr, ExprBuilder):
|
| 795 |
+
return expr.build()
|
| 796 |
+
if isinstance(expr, list):
|
| 797 |
+
if len(expr) == 1:
|
| 798 |
+
return expr[0]
|
| 799 |
+
else:
|
| 800 |
+
return expr[0](*[_LeftRightArgs._build(i) for i in expr[1]])
|
| 801 |
+
else:
|
| 802 |
+
return expr
|
| 803 |
+
|
| 804 |
+
def build(self):
|
| 805 |
+
data = [self._build(i) for i in self._lines]
|
| 806 |
+
if self.higher != 1:
|
| 807 |
+
data += [self._build(self.higher)]
|
| 808 |
+
data = list(data)
|
| 809 |
+
return data
|
| 810 |
+
|
| 811 |
+
def matrix_form(self):
|
| 812 |
+
if self.first != 1 and self.higher != 1:
|
| 813 |
+
raise ValueError("higher dimensional array cannot be represented")
|
| 814 |
+
|
| 815 |
+
def _get_shape(elem):
|
| 816 |
+
if isinstance(elem, MatrixExpr):
|
| 817 |
+
return elem.shape
|
| 818 |
+
return (None, None)
|
| 819 |
+
|
| 820 |
+
if _get_shape(self.first)[1] != _get_shape(self.second)[1]:
|
| 821 |
+
# Remove one-dimensional identity matrices:
|
| 822 |
+
# (this is needed by `a.diff(a)` where `a` is a vector)
|
| 823 |
+
if _get_shape(self.second) == (1, 1):
|
| 824 |
+
return self.first*self.second[0, 0]
|
| 825 |
+
if _get_shape(self.first) == (1, 1):
|
| 826 |
+
return self.first[1, 1]*self.second.T
|
| 827 |
+
raise ValueError("incompatible shapes")
|
| 828 |
+
if self.first != 1:
|
| 829 |
+
return self.first*self.second.T
|
| 830 |
+
else:
|
| 831 |
+
return self.higher
|
| 832 |
+
|
| 833 |
+
def rank(self):
|
| 834 |
+
"""
|
| 835 |
+
Number of dimensions different from trivial (warning: not related to
|
| 836 |
+
matrix rank).
|
| 837 |
+
"""
|
| 838 |
+
rank = 0
|
| 839 |
+
if self.first != 1:
|
| 840 |
+
rank += sum(i != 1 for i in self.first.shape)
|
| 841 |
+
if self.second != 1:
|
| 842 |
+
rank += sum(i != 1 for i in self.second.shape)
|
| 843 |
+
if self.higher != 1:
|
| 844 |
+
rank += 2
|
| 845 |
+
return rank
|
| 846 |
+
|
| 847 |
+
def _multiply_pointer(self, pointer, other):
|
| 848 |
+
from ...tensor.array.expressions.array_expressions import ArrayTensorProduct
|
| 849 |
+
from ...tensor.array.expressions.array_expressions import ArrayContraction
|
| 850 |
+
|
| 851 |
+
subexpr = ExprBuilder(
|
| 852 |
+
ArrayContraction,
|
| 853 |
+
[
|
| 854 |
+
ExprBuilder(
|
| 855 |
+
ArrayTensorProduct,
|
| 856 |
+
[
|
| 857 |
+
pointer,
|
| 858 |
+
other
|
| 859 |
+
]
|
| 860 |
+
),
|
| 861 |
+
(1, 2)
|
| 862 |
+
],
|
| 863 |
+
validator=ArrayContraction._validate
|
| 864 |
+
)
|
| 865 |
+
|
| 866 |
+
return subexpr
|
| 867 |
+
|
| 868 |
+
def append_first(self, other):
|
| 869 |
+
self.first_pointer *= other
|
| 870 |
+
|
| 871 |
+
def append_second(self, other):
|
| 872 |
+
self.second_pointer *= other
|
| 873 |
+
|
| 874 |
+
|
| 875 |
+
def _make_matrix(x):
|
| 876 |
+
from sympy.matrices.immutable import ImmutableDenseMatrix
|
| 877 |
+
if isinstance(x, MatrixExpr):
|
| 878 |
+
return x
|
| 879 |
+
return ImmutableDenseMatrix([[x]])
|
| 880 |
+
|
| 881 |
+
|
| 882 |
+
from .matmul import MatMul
|
| 883 |
+
from .matadd import MatAdd
|
| 884 |
+
from .matpow import MatPow
|
| 885 |
+
from .transpose import Transpose
|
| 886 |
+
from .inverse import Inverse
|
| 887 |
+
from .special import ZeroMatrix, Identity
|
| 888 |
+
from .determinant import Determinant
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/permutation.py
ADDED
|
@@ -0,0 +1,303 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
from sympy.functions import KroneckerDelta
|
| 4 |
+
|
| 5 |
+
from .matexpr import MatrixExpr
|
| 6 |
+
from .special import ZeroMatrix, Identity, OneMatrix
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class PermutationMatrix(MatrixExpr):
|
| 10 |
+
"""A Permutation Matrix
|
| 11 |
+
|
| 12 |
+
Parameters
|
| 13 |
+
==========
|
| 14 |
+
|
| 15 |
+
perm : Permutation
|
| 16 |
+
The permutation the matrix uses.
|
| 17 |
+
|
| 18 |
+
The size of the permutation determines the matrix size.
|
| 19 |
+
|
| 20 |
+
See the documentation of
|
| 21 |
+
:class:`sympy.combinatorics.permutations.Permutation` for
|
| 22 |
+
the further information of how to create a permutation object.
|
| 23 |
+
|
| 24 |
+
Examples
|
| 25 |
+
========
|
| 26 |
+
|
| 27 |
+
>>> from sympy import Matrix, PermutationMatrix
|
| 28 |
+
>>> from sympy.combinatorics import Permutation
|
| 29 |
+
|
| 30 |
+
Creating a permutation matrix:
|
| 31 |
+
|
| 32 |
+
>>> p = Permutation(1, 2, 0)
|
| 33 |
+
>>> P = PermutationMatrix(p)
|
| 34 |
+
>>> P = P.as_explicit()
|
| 35 |
+
>>> P
|
| 36 |
+
Matrix([
|
| 37 |
+
[0, 1, 0],
|
| 38 |
+
[0, 0, 1],
|
| 39 |
+
[1, 0, 0]])
|
| 40 |
+
|
| 41 |
+
Permuting a matrix row and column:
|
| 42 |
+
|
| 43 |
+
>>> M = Matrix([0, 1, 2])
|
| 44 |
+
>>> Matrix(P*M)
|
| 45 |
+
Matrix([
|
| 46 |
+
[1],
|
| 47 |
+
[2],
|
| 48 |
+
[0]])
|
| 49 |
+
|
| 50 |
+
>>> Matrix(M.T*P)
|
| 51 |
+
Matrix([[2, 0, 1]])
|
| 52 |
+
|
| 53 |
+
See Also
|
| 54 |
+
========
|
| 55 |
+
|
| 56 |
+
sympy.combinatorics.permutations.Permutation
|
| 57 |
+
"""
|
| 58 |
+
|
| 59 |
+
def __new__(cls, perm):
|
| 60 |
+
from sympy.combinatorics.permutations import Permutation
|
| 61 |
+
|
| 62 |
+
perm = _sympify(perm)
|
| 63 |
+
if not isinstance(perm, Permutation):
|
| 64 |
+
raise ValueError(
|
| 65 |
+
"{} must be a SymPy Permutation instance.".format(perm))
|
| 66 |
+
|
| 67 |
+
return super().__new__(cls, perm)
|
| 68 |
+
|
| 69 |
+
@property
|
| 70 |
+
def shape(self):
|
| 71 |
+
size = self.args[0].size
|
| 72 |
+
return (size, size)
|
| 73 |
+
|
| 74 |
+
@property
|
| 75 |
+
def is_Identity(self):
|
| 76 |
+
return self.args[0].is_Identity
|
| 77 |
+
|
| 78 |
+
def doit(self, **hints):
|
| 79 |
+
if self.is_Identity:
|
| 80 |
+
return Identity(self.rows)
|
| 81 |
+
return self
|
| 82 |
+
|
| 83 |
+
def _entry(self, i, j, **kwargs):
|
| 84 |
+
perm = self.args[0]
|
| 85 |
+
return KroneckerDelta(perm.apply(i), j)
|
| 86 |
+
|
| 87 |
+
def _eval_power(self, exp):
|
| 88 |
+
return PermutationMatrix(self.args[0] ** exp).doit()
|
| 89 |
+
|
| 90 |
+
def _eval_inverse(self):
|
| 91 |
+
return PermutationMatrix(self.args[0] ** -1)
|
| 92 |
+
|
| 93 |
+
_eval_transpose = _eval_adjoint = _eval_inverse
|
| 94 |
+
|
| 95 |
+
def _eval_determinant(self):
|
| 96 |
+
sign = self.args[0].signature()
|
| 97 |
+
if sign == 1:
|
| 98 |
+
return S.One
|
| 99 |
+
elif sign == -1:
|
| 100 |
+
return S.NegativeOne
|
| 101 |
+
raise NotImplementedError
|
| 102 |
+
|
| 103 |
+
def _eval_rewrite_as_BlockDiagMatrix(self, *args, **kwargs):
|
| 104 |
+
from sympy.combinatorics.permutations import Permutation
|
| 105 |
+
from .blockmatrix import BlockDiagMatrix
|
| 106 |
+
|
| 107 |
+
perm = self.args[0]
|
| 108 |
+
full_cyclic_form = perm.full_cyclic_form
|
| 109 |
+
|
| 110 |
+
cycles_picks = []
|
| 111 |
+
|
| 112 |
+
# Stage 1. Decompose the cycles into the blockable form.
|
| 113 |
+
a, b, c = 0, 0, 0
|
| 114 |
+
flag = False
|
| 115 |
+
for cycle in full_cyclic_form:
|
| 116 |
+
l = len(cycle)
|
| 117 |
+
m = max(cycle)
|
| 118 |
+
|
| 119 |
+
if not flag:
|
| 120 |
+
if m + 1 > a + l:
|
| 121 |
+
flag = True
|
| 122 |
+
temp = [cycle]
|
| 123 |
+
b = m
|
| 124 |
+
c = l
|
| 125 |
+
else:
|
| 126 |
+
cycles_picks.append([cycle])
|
| 127 |
+
a += l
|
| 128 |
+
|
| 129 |
+
else:
|
| 130 |
+
if m > b:
|
| 131 |
+
if m + 1 == a + c + l:
|
| 132 |
+
temp.append(cycle)
|
| 133 |
+
cycles_picks.append(temp)
|
| 134 |
+
flag = False
|
| 135 |
+
a = m+1
|
| 136 |
+
else:
|
| 137 |
+
b = m
|
| 138 |
+
temp.append(cycle)
|
| 139 |
+
c += l
|
| 140 |
+
else:
|
| 141 |
+
if b + 1 == a + c + l:
|
| 142 |
+
temp.append(cycle)
|
| 143 |
+
cycles_picks.append(temp)
|
| 144 |
+
flag = False
|
| 145 |
+
a = b+1
|
| 146 |
+
else:
|
| 147 |
+
temp.append(cycle)
|
| 148 |
+
c += l
|
| 149 |
+
|
| 150 |
+
# Stage 2. Normalize each decomposed cycles and build matrix.
|
| 151 |
+
p = 0
|
| 152 |
+
args = []
|
| 153 |
+
for pick in cycles_picks:
|
| 154 |
+
new_cycles = []
|
| 155 |
+
l = 0
|
| 156 |
+
for cycle in pick:
|
| 157 |
+
new_cycle = [i - p for i in cycle]
|
| 158 |
+
new_cycles.append(new_cycle)
|
| 159 |
+
l += len(cycle)
|
| 160 |
+
p += l
|
| 161 |
+
perm = Permutation(new_cycles)
|
| 162 |
+
mat = PermutationMatrix(perm)
|
| 163 |
+
args.append(mat)
|
| 164 |
+
|
| 165 |
+
return BlockDiagMatrix(*args)
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
class MatrixPermute(MatrixExpr):
|
| 169 |
+
r"""Symbolic representation for permuting matrix rows or columns.
|
| 170 |
+
|
| 171 |
+
Parameters
|
| 172 |
+
==========
|
| 173 |
+
|
| 174 |
+
perm : Permutation, PermutationMatrix
|
| 175 |
+
The permutation to use for permuting the matrix.
|
| 176 |
+
The permutation can be resized to the suitable one,
|
| 177 |
+
|
| 178 |
+
axis : 0 or 1
|
| 179 |
+
The axis to permute alongside.
|
| 180 |
+
If `0`, it will permute the matrix rows.
|
| 181 |
+
If `1`, it will permute the matrix columns.
|
| 182 |
+
|
| 183 |
+
Notes
|
| 184 |
+
=====
|
| 185 |
+
|
| 186 |
+
This follows the same notation used in
|
| 187 |
+
:meth:`sympy.matrices.matrixbase.MatrixBase.permute`.
|
| 188 |
+
|
| 189 |
+
Examples
|
| 190 |
+
========
|
| 191 |
+
|
| 192 |
+
>>> from sympy import Matrix, MatrixPermute
|
| 193 |
+
>>> from sympy.combinatorics import Permutation
|
| 194 |
+
|
| 195 |
+
Permuting the matrix rows:
|
| 196 |
+
|
| 197 |
+
>>> p = Permutation(1, 2, 0)
|
| 198 |
+
>>> A = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
|
| 199 |
+
>>> B = MatrixPermute(A, p, axis=0)
|
| 200 |
+
>>> B.as_explicit()
|
| 201 |
+
Matrix([
|
| 202 |
+
[4, 5, 6],
|
| 203 |
+
[7, 8, 9],
|
| 204 |
+
[1, 2, 3]])
|
| 205 |
+
|
| 206 |
+
Permuting the matrix columns:
|
| 207 |
+
|
| 208 |
+
>>> B = MatrixPermute(A, p, axis=1)
|
| 209 |
+
>>> B.as_explicit()
|
| 210 |
+
Matrix([
|
| 211 |
+
[2, 3, 1],
|
| 212 |
+
[5, 6, 4],
|
| 213 |
+
[8, 9, 7]])
|
| 214 |
+
|
| 215 |
+
See Also
|
| 216 |
+
========
|
| 217 |
+
|
| 218 |
+
sympy.matrices.matrixbase.MatrixBase.permute
|
| 219 |
+
"""
|
| 220 |
+
def __new__(cls, mat, perm, axis=S.Zero):
|
| 221 |
+
from sympy.combinatorics.permutations import Permutation
|
| 222 |
+
|
| 223 |
+
mat = _sympify(mat)
|
| 224 |
+
if not mat.is_Matrix:
|
| 225 |
+
raise ValueError(
|
| 226 |
+
"{} must be a SymPy matrix instance.".format(perm))
|
| 227 |
+
|
| 228 |
+
perm = _sympify(perm)
|
| 229 |
+
if isinstance(perm, PermutationMatrix):
|
| 230 |
+
perm = perm.args[0]
|
| 231 |
+
|
| 232 |
+
if not isinstance(perm, Permutation):
|
| 233 |
+
raise ValueError(
|
| 234 |
+
"{} must be a SymPy Permutation or a PermutationMatrix " \
|
| 235 |
+
"instance".format(perm))
|
| 236 |
+
|
| 237 |
+
axis = _sympify(axis)
|
| 238 |
+
if axis not in (0, 1):
|
| 239 |
+
raise ValueError("The axis must be 0 or 1.")
|
| 240 |
+
|
| 241 |
+
mat_size = mat.shape[axis]
|
| 242 |
+
if mat_size != perm.size:
|
| 243 |
+
try:
|
| 244 |
+
perm = perm.resize(mat_size)
|
| 245 |
+
except ValueError:
|
| 246 |
+
raise ValueError(
|
| 247 |
+
"Size does not match between the permutation {} "
|
| 248 |
+
"and the matrix {} threaded over the axis {} "
|
| 249 |
+
"and cannot be converted."
|
| 250 |
+
.format(perm, mat, axis))
|
| 251 |
+
|
| 252 |
+
return super().__new__(cls, mat, perm, axis)
|
| 253 |
+
|
| 254 |
+
def doit(self, deep=True, **hints):
|
| 255 |
+
mat, perm, axis = self.args
|
| 256 |
+
|
| 257 |
+
if deep:
|
| 258 |
+
mat = mat.doit(deep=deep, **hints)
|
| 259 |
+
perm = perm.doit(deep=deep, **hints)
|
| 260 |
+
|
| 261 |
+
if perm.is_Identity:
|
| 262 |
+
return mat
|
| 263 |
+
|
| 264 |
+
if mat.is_Identity:
|
| 265 |
+
if axis is S.Zero:
|
| 266 |
+
return PermutationMatrix(perm)
|
| 267 |
+
elif axis is S.One:
|
| 268 |
+
return PermutationMatrix(perm**-1)
|
| 269 |
+
|
| 270 |
+
if isinstance(mat, (ZeroMatrix, OneMatrix)):
|
| 271 |
+
return mat
|
| 272 |
+
|
| 273 |
+
if isinstance(mat, MatrixPermute) and mat.args[2] == axis:
|
| 274 |
+
return MatrixPermute(mat.args[0], perm * mat.args[1], axis)
|
| 275 |
+
|
| 276 |
+
return self
|
| 277 |
+
|
| 278 |
+
@property
|
| 279 |
+
def shape(self):
|
| 280 |
+
return self.args[0].shape
|
| 281 |
+
|
| 282 |
+
def _entry(self, i, j, **kwargs):
|
| 283 |
+
mat, perm, axis = self.args
|
| 284 |
+
|
| 285 |
+
if axis == 0:
|
| 286 |
+
return mat[perm.apply(i), j]
|
| 287 |
+
elif axis == 1:
|
| 288 |
+
return mat[i, perm.apply(j)]
|
| 289 |
+
|
| 290 |
+
def _eval_rewrite_as_MatMul(self, *args, **kwargs):
|
| 291 |
+
from .matmul import MatMul
|
| 292 |
+
|
| 293 |
+
mat, perm, axis = self.args
|
| 294 |
+
|
| 295 |
+
deep = kwargs.get("deep", True)
|
| 296 |
+
|
| 297 |
+
if deep:
|
| 298 |
+
mat = mat.rewrite(MatMul)
|
| 299 |
+
|
| 300 |
+
if axis == 0:
|
| 301 |
+
return MatMul(PermutationMatrix(perm), mat)
|
| 302 |
+
elif axis == 1:
|
| 303 |
+
return MatMul(mat, PermutationMatrix(perm**-1))
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/sets.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.core.assumptions import check_assumptions
|
| 2 |
+
from sympy.core.logic import fuzzy_and
|
| 3 |
+
from sympy.core.sympify import _sympify
|
| 4 |
+
from sympy.matrices.kind import MatrixKind
|
| 5 |
+
from sympy.sets.sets import Set, SetKind
|
| 6 |
+
from sympy.core.kind import NumberKind
|
| 7 |
+
from .matexpr import MatrixExpr
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class MatrixSet(Set):
|
| 11 |
+
"""
|
| 12 |
+
MatrixSet represents the set of matrices with ``shape = (n, m)`` over the
|
| 13 |
+
given set.
|
| 14 |
+
|
| 15 |
+
Examples
|
| 16 |
+
========
|
| 17 |
+
|
| 18 |
+
>>> from sympy.matrices import MatrixSet
|
| 19 |
+
>>> from sympy import S, I, Matrix
|
| 20 |
+
>>> M = MatrixSet(2, 2, set=S.Reals)
|
| 21 |
+
>>> X = Matrix([[1, 2], [3, 4]])
|
| 22 |
+
>>> X in M
|
| 23 |
+
True
|
| 24 |
+
>>> X = Matrix([[1, 2], [I, 4]])
|
| 25 |
+
>>> X in M
|
| 26 |
+
False
|
| 27 |
+
|
| 28 |
+
"""
|
| 29 |
+
is_empty = False
|
| 30 |
+
|
| 31 |
+
def __new__(cls, n, m, set):
|
| 32 |
+
n, m, set = _sympify(n), _sympify(m), _sympify(set)
|
| 33 |
+
cls._check_dim(n)
|
| 34 |
+
cls._check_dim(m)
|
| 35 |
+
if not isinstance(set, Set):
|
| 36 |
+
raise TypeError("{} should be an instance of Set.".format(set))
|
| 37 |
+
return Set.__new__(cls, n, m, set)
|
| 38 |
+
|
| 39 |
+
@property
|
| 40 |
+
def shape(self):
|
| 41 |
+
return self.args[:2]
|
| 42 |
+
|
| 43 |
+
@property
|
| 44 |
+
def set(self):
|
| 45 |
+
return self.args[2]
|
| 46 |
+
|
| 47 |
+
def _contains(self, other):
|
| 48 |
+
if not isinstance(other, MatrixExpr):
|
| 49 |
+
raise TypeError("{} should be an instance of MatrixExpr.".format(other))
|
| 50 |
+
if other.shape != self.shape:
|
| 51 |
+
are_symbolic = any(_sympify(x).is_Symbol for x in other.shape + self.shape)
|
| 52 |
+
if are_symbolic:
|
| 53 |
+
return None
|
| 54 |
+
return False
|
| 55 |
+
return fuzzy_and(self.set.contains(x) for x in other)
|
| 56 |
+
|
| 57 |
+
@classmethod
|
| 58 |
+
def _check_dim(cls, dim):
|
| 59 |
+
"""Helper function to check invalid matrix dimensions"""
|
| 60 |
+
ok = not dim.is_Float and check_assumptions(
|
| 61 |
+
dim, integer=True, nonnegative=True)
|
| 62 |
+
if ok is False:
|
| 63 |
+
raise ValueError(
|
| 64 |
+
"The dimension specification {} should be "
|
| 65 |
+
"a nonnegative integer.".format(dim))
|
| 66 |
+
|
| 67 |
+
def _kind(self):
|
| 68 |
+
return SetKind(MatrixKind(NumberKind))
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/slice.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.matrices.expressions.matexpr import MatrixExpr
|
| 2 |
+
from sympy.core.basic import Basic
|
| 3 |
+
from sympy.core.containers import Tuple
|
| 4 |
+
from sympy.functions.elementary.integers import floor
|
| 5 |
+
|
| 6 |
+
def normalize(i, parentsize):
|
| 7 |
+
if isinstance(i, slice):
|
| 8 |
+
i = (i.start, i.stop, i.step)
|
| 9 |
+
if not isinstance(i, (tuple, list, Tuple)):
|
| 10 |
+
if (i < 0) == True:
|
| 11 |
+
i += parentsize
|
| 12 |
+
i = (i, i+1, 1)
|
| 13 |
+
i = list(i)
|
| 14 |
+
if len(i) == 2:
|
| 15 |
+
i.append(1)
|
| 16 |
+
start, stop, step = i
|
| 17 |
+
start = start or 0
|
| 18 |
+
if stop is None:
|
| 19 |
+
stop = parentsize
|
| 20 |
+
if (start < 0) == True:
|
| 21 |
+
start += parentsize
|
| 22 |
+
if (stop < 0) == True:
|
| 23 |
+
stop += parentsize
|
| 24 |
+
step = step or 1
|
| 25 |
+
|
| 26 |
+
if ((stop - start) * step < 1) == True:
|
| 27 |
+
raise IndexError()
|
| 28 |
+
|
| 29 |
+
return (start, stop, step)
|
| 30 |
+
|
| 31 |
+
class MatrixSlice(MatrixExpr):
|
| 32 |
+
""" A MatrixSlice of a Matrix Expression
|
| 33 |
+
|
| 34 |
+
Examples
|
| 35 |
+
========
|
| 36 |
+
|
| 37 |
+
>>> from sympy import MatrixSlice, ImmutableMatrix
|
| 38 |
+
>>> M = ImmutableMatrix(4, 4, range(16))
|
| 39 |
+
>>> M
|
| 40 |
+
Matrix([
|
| 41 |
+
[ 0, 1, 2, 3],
|
| 42 |
+
[ 4, 5, 6, 7],
|
| 43 |
+
[ 8, 9, 10, 11],
|
| 44 |
+
[12, 13, 14, 15]])
|
| 45 |
+
|
| 46 |
+
>>> B = MatrixSlice(M, (0, 2), (2, 4))
|
| 47 |
+
>>> ImmutableMatrix(B)
|
| 48 |
+
Matrix([
|
| 49 |
+
[2, 3],
|
| 50 |
+
[6, 7]])
|
| 51 |
+
"""
|
| 52 |
+
parent = property(lambda self: self.args[0])
|
| 53 |
+
rowslice = property(lambda self: self.args[1])
|
| 54 |
+
colslice = property(lambda self: self.args[2])
|
| 55 |
+
|
| 56 |
+
def __new__(cls, parent, rowslice, colslice):
|
| 57 |
+
rowslice = normalize(rowslice, parent.shape[0])
|
| 58 |
+
colslice = normalize(colslice, parent.shape[1])
|
| 59 |
+
if not (len(rowslice) == len(colslice) == 3):
|
| 60 |
+
raise IndexError()
|
| 61 |
+
if ((0 > rowslice[0]) == True or
|
| 62 |
+
(parent.shape[0] < rowslice[1]) == True or
|
| 63 |
+
(0 > colslice[0]) == True or
|
| 64 |
+
(parent.shape[1] < colslice[1]) == True):
|
| 65 |
+
raise IndexError()
|
| 66 |
+
if isinstance(parent, MatrixSlice):
|
| 67 |
+
return mat_slice_of_slice(parent, rowslice, colslice)
|
| 68 |
+
return Basic.__new__(cls, parent, Tuple(*rowslice), Tuple(*colslice))
|
| 69 |
+
|
| 70 |
+
@property
|
| 71 |
+
def shape(self):
|
| 72 |
+
rows = self.rowslice[1] - self.rowslice[0]
|
| 73 |
+
rows = rows if self.rowslice[2] == 1 else floor(rows/self.rowslice[2])
|
| 74 |
+
cols = self.colslice[1] - self.colslice[0]
|
| 75 |
+
cols = cols if self.colslice[2] == 1 else floor(cols/self.colslice[2])
|
| 76 |
+
return rows, cols
|
| 77 |
+
|
| 78 |
+
def _entry(self, i, j, **kwargs):
|
| 79 |
+
return self.parent._entry(i*self.rowslice[2] + self.rowslice[0],
|
| 80 |
+
j*self.colslice[2] + self.colslice[0],
|
| 81 |
+
**kwargs)
|
| 82 |
+
|
| 83 |
+
@property
|
| 84 |
+
def on_diag(self):
|
| 85 |
+
return self.rowslice == self.colslice
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
def slice_of_slice(s, t):
|
| 89 |
+
start1, stop1, step1 = s
|
| 90 |
+
start2, stop2, step2 = t
|
| 91 |
+
|
| 92 |
+
start = start1 + start2*step1
|
| 93 |
+
step = step1 * step2
|
| 94 |
+
stop = start1 + step1*stop2
|
| 95 |
+
|
| 96 |
+
if stop > stop1:
|
| 97 |
+
raise IndexError()
|
| 98 |
+
|
| 99 |
+
return start, stop, step
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
def mat_slice_of_slice(parent, rowslice, colslice):
|
| 103 |
+
""" Collapse nested matrix slices
|
| 104 |
+
|
| 105 |
+
>>> from sympy import MatrixSymbol
|
| 106 |
+
>>> X = MatrixSymbol('X', 10, 10)
|
| 107 |
+
>>> X[:, 1:5][5:8, :]
|
| 108 |
+
X[5:8, 1:5]
|
| 109 |
+
>>> X[1:9:2, 2:6][1:3, 2]
|
| 110 |
+
X[3:7:2, 4:5]
|
| 111 |
+
"""
|
| 112 |
+
row = slice_of_slice(parent.rowslice, rowslice)
|
| 113 |
+
col = slice_of_slice(parent.colslice, colslice)
|
| 114 |
+
return MatrixSlice(parent.parent, row, col)
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/special.py
ADDED
|
@@ -0,0 +1,299 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.assumptions.ask import ask, Q
|
| 2 |
+
from sympy.core.relational import Eq
|
| 3 |
+
from sympy.core.singleton import S
|
| 4 |
+
from sympy.core.sympify import _sympify
|
| 5 |
+
from sympy.functions.special.tensor_functions import KroneckerDelta
|
| 6 |
+
from sympy.matrices.exceptions import NonInvertibleMatrixError
|
| 7 |
+
from .matexpr import MatrixExpr
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class ZeroMatrix(MatrixExpr):
|
| 11 |
+
"""The Matrix Zero 0 - additive identity
|
| 12 |
+
|
| 13 |
+
Examples
|
| 14 |
+
========
|
| 15 |
+
|
| 16 |
+
>>> from sympy import MatrixSymbol, ZeroMatrix
|
| 17 |
+
>>> A = MatrixSymbol('A', 3, 5)
|
| 18 |
+
>>> Z = ZeroMatrix(3, 5)
|
| 19 |
+
>>> A + Z
|
| 20 |
+
A
|
| 21 |
+
>>> Z*A.T
|
| 22 |
+
0
|
| 23 |
+
"""
|
| 24 |
+
is_ZeroMatrix = True
|
| 25 |
+
|
| 26 |
+
def __new__(cls, m, n):
|
| 27 |
+
m, n = _sympify(m), _sympify(n)
|
| 28 |
+
cls._check_dim(m)
|
| 29 |
+
cls._check_dim(n)
|
| 30 |
+
|
| 31 |
+
return super().__new__(cls, m, n)
|
| 32 |
+
|
| 33 |
+
@property
|
| 34 |
+
def shape(self):
|
| 35 |
+
return (self.args[0], self.args[1])
|
| 36 |
+
|
| 37 |
+
def _eval_power(self, exp):
|
| 38 |
+
# exp = -1, 0, 1 are already handled at this stage
|
| 39 |
+
if (exp < 0) == True:
|
| 40 |
+
raise NonInvertibleMatrixError("Matrix det == 0; not invertible")
|
| 41 |
+
return self
|
| 42 |
+
|
| 43 |
+
def _eval_transpose(self):
|
| 44 |
+
return ZeroMatrix(self.cols, self.rows)
|
| 45 |
+
|
| 46 |
+
def _eval_adjoint(self):
|
| 47 |
+
return ZeroMatrix(self.cols, self.rows)
|
| 48 |
+
|
| 49 |
+
def _eval_trace(self):
|
| 50 |
+
return S.Zero
|
| 51 |
+
|
| 52 |
+
def _eval_determinant(self):
|
| 53 |
+
return S.Zero
|
| 54 |
+
|
| 55 |
+
def _eval_inverse(self):
|
| 56 |
+
raise NonInvertibleMatrixError("Matrix det == 0; not invertible.")
|
| 57 |
+
|
| 58 |
+
def _eval_as_real_imag(self):
|
| 59 |
+
return (self, self)
|
| 60 |
+
|
| 61 |
+
def _eval_conjugate(self):
|
| 62 |
+
return self
|
| 63 |
+
|
| 64 |
+
def _entry(self, i, j, **kwargs):
|
| 65 |
+
return S.Zero
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
class GenericZeroMatrix(ZeroMatrix):
|
| 69 |
+
"""
|
| 70 |
+
A zero matrix without a specified shape
|
| 71 |
+
|
| 72 |
+
This exists primarily so MatAdd() with no arguments can return something
|
| 73 |
+
meaningful.
|
| 74 |
+
"""
|
| 75 |
+
def __new__(cls):
|
| 76 |
+
# super(ZeroMatrix, cls) instead of super(GenericZeroMatrix, cls)
|
| 77 |
+
# because ZeroMatrix.__new__ doesn't have the same signature
|
| 78 |
+
return super(ZeroMatrix, cls).__new__(cls)
|
| 79 |
+
|
| 80 |
+
@property
|
| 81 |
+
def rows(self):
|
| 82 |
+
raise TypeError("GenericZeroMatrix does not have a specified shape")
|
| 83 |
+
|
| 84 |
+
@property
|
| 85 |
+
def cols(self):
|
| 86 |
+
raise TypeError("GenericZeroMatrix does not have a specified shape")
|
| 87 |
+
|
| 88 |
+
@property
|
| 89 |
+
def shape(self):
|
| 90 |
+
raise TypeError("GenericZeroMatrix does not have a specified shape")
|
| 91 |
+
|
| 92 |
+
# Avoid Matrix.__eq__ which might call .shape
|
| 93 |
+
def __eq__(self, other):
|
| 94 |
+
return isinstance(other, GenericZeroMatrix)
|
| 95 |
+
|
| 96 |
+
def __ne__(self, other):
|
| 97 |
+
return not (self == other)
|
| 98 |
+
|
| 99 |
+
def __hash__(self):
|
| 100 |
+
return super().__hash__()
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
class Identity(MatrixExpr):
|
| 105 |
+
"""The Matrix Identity I - multiplicative identity
|
| 106 |
+
|
| 107 |
+
Examples
|
| 108 |
+
========
|
| 109 |
+
|
| 110 |
+
>>> from sympy import Identity, MatrixSymbol
|
| 111 |
+
>>> A = MatrixSymbol('A', 3, 5)
|
| 112 |
+
>>> I = Identity(3)
|
| 113 |
+
>>> I*A
|
| 114 |
+
A
|
| 115 |
+
"""
|
| 116 |
+
|
| 117 |
+
is_Identity = True
|
| 118 |
+
|
| 119 |
+
def __new__(cls, n):
|
| 120 |
+
n = _sympify(n)
|
| 121 |
+
cls._check_dim(n)
|
| 122 |
+
|
| 123 |
+
return super().__new__(cls, n)
|
| 124 |
+
|
| 125 |
+
@property
|
| 126 |
+
def rows(self):
|
| 127 |
+
return self.args[0]
|
| 128 |
+
|
| 129 |
+
@property
|
| 130 |
+
def cols(self):
|
| 131 |
+
return self.args[0]
|
| 132 |
+
|
| 133 |
+
@property
|
| 134 |
+
def shape(self):
|
| 135 |
+
return (self.args[0], self.args[0])
|
| 136 |
+
|
| 137 |
+
@property
|
| 138 |
+
def is_square(self):
|
| 139 |
+
return True
|
| 140 |
+
|
| 141 |
+
def _eval_transpose(self):
|
| 142 |
+
return self
|
| 143 |
+
|
| 144 |
+
def _eval_trace(self):
|
| 145 |
+
return self.rows
|
| 146 |
+
|
| 147 |
+
def _eval_inverse(self):
|
| 148 |
+
return self
|
| 149 |
+
|
| 150 |
+
def _eval_as_real_imag(self):
|
| 151 |
+
return (self, ZeroMatrix(*self.shape))
|
| 152 |
+
|
| 153 |
+
def _eval_conjugate(self):
|
| 154 |
+
return self
|
| 155 |
+
|
| 156 |
+
def _eval_adjoint(self):
|
| 157 |
+
return self
|
| 158 |
+
|
| 159 |
+
def _entry(self, i, j, **kwargs):
|
| 160 |
+
eq = Eq(i, j)
|
| 161 |
+
if eq is S.true:
|
| 162 |
+
return S.One
|
| 163 |
+
elif eq is S.false:
|
| 164 |
+
return S.Zero
|
| 165 |
+
return KroneckerDelta(i, j, (0, self.cols-1))
|
| 166 |
+
|
| 167 |
+
def _eval_determinant(self):
|
| 168 |
+
return S.One
|
| 169 |
+
|
| 170 |
+
def _eval_power(self, exp):
|
| 171 |
+
return self
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
class GenericIdentity(Identity):
|
| 175 |
+
"""
|
| 176 |
+
An identity matrix without a specified shape
|
| 177 |
+
|
| 178 |
+
This exists primarily so MatMul() with no arguments can return something
|
| 179 |
+
meaningful.
|
| 180 |
+
"""
|
| 181 |
+
def __new__(cls):
|
| 182 |
+
# super(Identity, cls) instead of super(GenericIdentity, cls) because
|
| 183 |
+
# Identity.__new__ doesn't have the same signature
|
| 184 |
+
return super(Identity, cls).__new__(cls)
|
| 185 |
+
|
| 186 |
+
@property
|
| 187 |
+
def rows(self):
|
| 188 |
+
raise TypeError("GenericIdentity does not have a specified shape")
|
| 189 |
+
|
| 190 |
+
@property
|
| 191 |
+
def cols(self):
|
| 192 |
+
raise TypeError("GenericIdentity does not have a specified shape")
|
| 193 |
+
|
| 194 |
+
@property
|
| 195 |
+
def shape(self):
|
| 196 |
+
raise TypeError("GenericIdentity does not have a specified shape")
|
| 197 |
+
|
| 198 |
+
@property
|
| 199 |
+
def is_square(self):
|
| 200 |
+
return True
|
| 201 |
+
|
| 202 |
+
# Avoid Matrix.__eq__ which might call .shape
|
| 203 |
+
def __eq__(self, other):
|
| 204 |
+
return isinstance(other, GenericIdentity)
|
| 205 |
+
|
| 206 |
+
def __ne__(self, other):
|
| 207 |
+
return not (self == other)
|
| 208 |
+
|
| 209 |
+
def __hash__(self):
|
| 210 |
+
return super().__hash__()
|
| 211 |
+
|
| 212 |
+
|
| 213 |
+
class OneMatrix(MatrixExpr):
|
| 214 |
+
"""
|
| 215 |
+
Matrix whose all entries are ones.
|
| 216 |
+
"""
|
| 217 |
+
def __new__(cls, m, n, evaluate=False):
|
| 218 |
+
m, n = _sympify(m), _sympify(n)
|
| 219 |
+
cls._check_dim(m)
|
| 220 |
+
cls._check_dim(n)
|
| 221 |
+
|
| 222 |
+
if evaluate:
|
| 223 |
+
condition = Eq(m, 1) & Eq(n, 1)
|
| 224 |
+
if condition == True:
|
| 225 |
+
return Identity(1)
|
| 226 |
+
|
| 227 |
+
obj = super().__new__(cls, m, n)
|
| 228 |
+
return obj
|
| 229 |
+
|
| 230 |
+
@property
|
| 231 |
+
def shape(self):
|
| 232 |
+
return self._args
|
| 233 |
+
|
| 234 |
+
@property
|
| 235 |
+
def is_Identity(self):
|
| 236 |
+
return self._is_1x1() == True
|
| 237 |
+
|
| 238 |
+
def as_explicit(self):
|
| 239 |
+
from sympy.matrices.immutable import ImmutableDenseMatrix
|
| 240 |
+
return ImmutableDenseMatrix.ones(*self.shape)
|
| 241 |
+
|
| 242 |
+
def doit(self, **hints):
|
| 243 |
+
args = self.args
|
| 244 |
+
if hints.get('deep', True):
|
| 245 |
+
args = [a.doit(**hints) for a in args]
|
| 246 |
+
return self.func(*args, evaluate=True)
|
| 247 |
+
|
| 248 |
+
def _eval_power(self, exp):
|
| 249 |
+
# exp = -1, 0, 1 are already handled at this stage
|
| 250 |
+
if self._is_1x1() == True:
|
| 251 |
+
return Identity(1)
|
| 252 |
+
if (exp < 0) == True:
|
| 253 |
+
raise NonInvertibleMatrixError("Matrix det == 0; not invertible")
|
| 254 |
+
if ask(Q.integer(exp)):
|
| 255 |
+
return self.shape[0] ** (exp - 1) * OneMatrix(*self.shape)
|
| 256 |
+
return super()._eval_power(exp)
|
| 257 |
+
|
| 258 |
+
def _eval_transpose(self):
|
| 259 |
+
return OneMatrix(self.cols, self.rows)
|
| 260 |
+
|
| 261 |
+
def _eval_adjoint(self):
|
| 262 |
+
return OneMatrix(self.cols, self.rows)
|
| 263 |
+
|
| 264 |
+
def _eval_trace(self):
|
| 265 |
+
return S.One*self.rows
|
| 266 |
+
|
| 267 |
+
def _is_1x1(self):
|
| 268 |
+
"""Returns true if the matrix is known to be 1x1"""
|
| 269 |
+
shape = self.shape
|
| 270 |
+
return Eq(shape[0], 1) & Eq(shape[1], 1)
|
| 271 |
+
|
| 272 |
+
def _eval_determinant(self):
|
| 273 |
+
condition = self._is_1x1()
|
| 274 |
+
if condition == True:
|
| 275 |
+
return S.One
|
| 276 |
+
elif condition == False:
|
| 277 |
+
return S.Zero
|
| 278 |
+
else:
|
| 279 |
+
from sympy.matrices.expressions.determinant import Determinant
|
| 280 |
+
return Determinant(self)
|
| 281 |
+
|
| 282 |
+
def _eval_inverse(self):
|
| 283 |
+
condition = self._is_1x1()
|
| 284 |
+
if condition == True:
|
| 285 |
+
return Identity(1)
|
| 286 |
+
elif condition == False:
|
| 287 |
+
raise NonInvertibleMatrixError("Matrix det == 0; not invertible.")
|
| 288 |
+
else:
|
| 289 |
+
from .inverse import Inverse
|
| 290 |
+
return Inverse(self)
|
| 291 |
+
|
| 292 |
+
def _eval_as_real_imag(self):
|
| 293 |
+
return (self, ZeroMatrix(*self.shape))
|
| 294 |
+
|
| 295 |
+
def _eval_conjugate(self):
|
| 296 |
+
return self
|
| 297 |
+
|
| 298 |
+
def _entry(self, i, j, **kwargs):
|
| 299 |
+
return S.One
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__init__.py
ADDED
|
File without changes
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_adjoint.cpython-310.pyc
ADDED
|
Binary file (1.31 kB). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_applyfunc.cpython-310.pyc
ADDED
|
Binary file (3.65 kB). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_blockmatrix.cpython-310.pyc
ADDED
|
Binary file (16.9 kB). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_companion.cpython-310.pyc
ADDED
|
Binary file (2.37 kB). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_derivatives.cpython-310.pyc
ADDED
|
Binary file (13.3 kB). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_determinant.cpython-310.pyc
ADDED
|
Binary file (2.89 kB). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_dotproduct.cpython-310.pyc
ADDED
|
Binary file (1.91 kB). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_factorizations.cpython-310.pyc
ADDED
|
Binary file (1.33 kB). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_funcmatrix.cpython-310.pyc
ADDED
|
Binary file (3.71 kB). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_hadamard.cpython-310.pyc
ADDED
|
Binary file (5.59 kB). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_indexing.cpython-310.pyc
ADDED
|
Binary file (12 kB). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_inverse.cpython-310.pyc
ADDED
|
Binary file (3.01 kB). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_kronecker.cpython-310.pyc
ADDED
|
Binary file (5.83 kB). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_matadd.cpython-310.pyc
ADDED
|
Binary file (3.13 kB). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_matmul.cpython-310.pyc
ADDED
|
Binary file (8.06 kB). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_matpow.cpython-310.pyc
ADDED
|
Binary file (8.64 kB). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_permutation.cpython-310.pyc
ADDED
|
Binary file (5.9 kB). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_sets.cpython-310.pyc
ADDED
|
Binary file (2.34 kB). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_slice.cpython-310.pyc
ADDED
|
Binary file (3.69 kB). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_special.cpython-310.pyc
ADDED
|
Binary file (8.4 kB). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_trace.cpython-310.pyc
ADDED
|
Binary file (4.66 kB). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/sympy/matrices/expressions/tests/__pycache__/test_transpose.cpython-310.pyc
ADDED
|
Binary file (2.65 kB). View file
|
|
|