Spaces:
Running
Running
File size: 5,607 Bytes
6a86ad5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
from sympy.combinatorics import Permutation
from sympy.core.expr import unchanged
from sympy.matrices import Matrix
from sympy.matrices.expressions import \
MatMul, BlockDiagMatrix, Determinant, Inverse
from sympy.matrices.expressions.matexpr import MatrixSymbol
from sympy.matrices.expressions.special import ZeroMatrix, OneMatrix, Identity
from sympy.matrices.expressions.permutation import \
MatrixPermute, PermutationMatrix
from sympy.testing.pytest import raises
from sympy.core.symbol import Symbol
def test_PermutationMatrix_basic():
p = Permutation([1, 0])
assert unchanged(PermutationMatrix, p)
raises(ValueError, lambda: PermutationMatrix((0, 1, 2)))
assert PermutationMatrix(p).as_explicit() == Matrix([[0, 1], [1, 0]])
assert isinstance(PermutationMatrix(p)*MatrixSymbol('A', 2, 2), MatMul)
def test_PermutationMatrix_matmul():
p = Permutation([1, 2, 0])
P = PermutationMatrix(p)
M = Matrix([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
assert (P*M).as_explicit() == P.as_explicit()*M
assert (M*P).as_explicit() == M*P.as_explicit()
P1 = PermutationMatrix(Permutation([1, 2, 0]))
P2 = PermutationMatrix(Permutation([2, 1, 0]))
P3 = PermutationMatrix(Permutation([1, 0, 2]))
assert P1*P2 == P3
def test_PermutationMatrix_matpow():
p1 = Permutation([1, 2, 0])
P1 = PermutationMatrix(p1)
p2 = Permutation([2, 0, 1])
P2 = PermutationMatrix(p2)
assert P1**2 == P2
assert P1**3 == Identity(3)
def test_PermutationMatrix_identity():
p = Permutation([0, 1])
assert PermutationMatrix(p).is_Identity
p = Permutation([1, 0])
assert not PermutationMatrix(p).is_Identity
def test_PermutationMatrix_determinant():
P = PermutationMatrix(Permutation([0, 1, 2]))
assert Determinant(P).doit() == 1
P = PermutationMatrix(Permutation([0, 2, 1]))
assert Determinant(P).doit() == -1
P = PermutationMatrix(Permutation([2, 0, 1]))
assert Determinant(P).doit() == 1
def test_PermutationMatrix_inverse():
P = PermutationMatrix(Permutation(0, 1, 2))
assert Inverse(P).doit() == PermutationMatrix(Permutation(0, 2, 1))
def test_PermutationMatrix_rewrite_BlockDiagMatrix():
P = PermutationMatrix(Permutation([0, 1, 2, 3, 4, 5]))
P0 = PermutationMatrix(Permutation([0]))
assert P.rewrite(BlockDiagMatrix) == \
BlockDiagMatrix(P0, P0, P0, P0, P0, P0)
P = PermutationMatrix(Permutation([0, 1, 3, 2, 4, 5]))
P10 = PermutationMatrix(Permutation(0, 1))
assert P.rewrite(BlockDiagMatrix) == \
BlockDiagMatrix(P0, P0, P10, P0, P0)
P = PermutationMatrix(Permutation([1, 0, 3, 2, 5, 4]))
assert P.rewrite(BlockDiagMatrix) == \
BlockDiagMatrix(P10, P10, P10)
P = PermutationMatrix(Permutation([0, 4, 3, 2, 1, 5]))
P3210 = PermutationMatrix(Permutation([3, 2, 1, 0]))
assert P.rewrite(BlockDiagMatrix) == \
BlockDiagMatrix(P0, P3210, P0)
P = PermutationMatrix(Permutation([0, 4, 2, 3, 1, 5]))
P3120 = PermutationMatrix(Permutation([3, 1, 2, 0]))
assert P.rewrite(BlockDiagMatrix) == \
BlockDiagMatrix(P0, P3120, P0)
P = PermutationMatrix(Permutation(0, 3)(1, 4)(2, 5))
assert P.rewrite(BlockDiagMatrix) == BlockDiagMatrix(P)
def test_MartrixPermute_basic():
p = Permutation(0, 1)
P = PermutationMatrix(p)
A = MatrixSymbol('A', 2, 2)
raises(ValueError, lambda: MatrixPermute(Symbol('x'), p))
raises(ValueError, lambda: MatrixPermute(A, Symbol('x')))
assert MatrixPermute(A, P) == MatrixPermute(A, p)
raises(ValueError, lambda: MatrixPermute(A, p, 2))
pp = Permutation(0, 1, size=3)
assert MatrixPermute(A, pp) == MatrixPermute(A, p)
pp = Permutation(0, 1, 2)
raises(ValueError, lambda: MatrixPermute(A, pp))
def test_MatrixPermute_shape():
p = Permutation(0, 1)
A = MatrixSymbol('A', 2, 3)
assert MatrixPermute(A, p).shape == (2, 3)
def test_MatrixPermute_explicit():
p = Permutation(0, 1, 2)
A = MatrixSymbol('A', 3, 3)
AA = A.as_explicit()
assert MatrixPermute(A, p, 0).as_explicit() == \
AA.permute(p, orientation='rows')
assert MatrixPermute(A, p, 1).as_explicit() == \
AA.permute(p, orientation='cols')
def test_MatrixPermute_rewrite_MatMul():
p = Permutation(0, 1, 2)
A = MatrixSymbol('A', 3, 3)
assert MatrixPermute(A, p, 0).rewrite(MatMul).as_explicit() == \
MatrixPermute(A, p, 0).as_explicit()
assert MatrixPermute(A, p, 1).rewrite(MatMul).as_explicit() == \
MatrixPermute(A, p, 1).as_explicit()
def test_MatrixPermute_doit():
p = Permutation(0, 1, 2)
A = MatrixSymbol('A', 3, 3)
assert MatrixPermute(A, p).doit() == MatrixPermute(A, p)
p = Permutation(0, size=3)
A = MatrixSymbol('A', 3, 3)
assert MatrixPermute(A, p).doit().as_explicit() == \
MatrixPermute(A, p).as_explicit()
p = Permutation(0, 1, 2)
A = Identity(3)
assert MatrixPermute(A, p, 0).doit().as_explicit() == \
MatrixPermute(A, p, 0).as_explicit()
assert MatrixPermute(A, p, 1).doit().as_explicit() == \
MatrixPermute(A, p, 1).as_explicit()
A = ZeroMatrix(3, 3)
assert MatrixPermute(A, p).doit() == A
A = OneMatrix(3, 3)
assert MatrixPermute(A, p).doit() == A
A = MatrixSymbol('A', 4, 4)
p1 = Permutation(0, 1, 2, 3)
p2 = Permutation(0, 2, 3, 1)
expr = MatrixPermute(MatrixPermute(A, p1, 0), p2, 0)
assert expr.as_explicit() == expr.doit().as_explicit()
expr = MatrixPermute(MatrixPermute(A, p1, 1), p2, 1)
assert expr.as_explicit() == expr.doit().as_explicit()
|