Spaces:
Sleeping
Sleeping
File size: 3,213 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 |
from sympy.combinatorics import Permutation
from sympy.core.symbol import symbols
from sympy.matrices import Matrix
from sympy.matrices.expressions import (
PermutationMatrix, BlockDiagMatrix, BlockMatrix)
def test_connected_components():
a, b, c, d, e, f, g, h, i, j, k, l, m = symbols('a:m')
M = Matrix([
[a, 0, 0, 0, b, 0, 0, 0, 0, 0, c, 0, 0],
[0, d, 0, 0, 0, e, 0, 0, 0, 0, 0, f, 0],
[0, 0, g, 0, 0, 0, h, 0, 0, 0, 0, 0, i],
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[m, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, m, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, m, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
[j, 0, 0, 0, k, 0, 0, 1, 0, 0, l, 0, 0],
[0, j, 0, 0, 0, k, 0, 0, 1, 0, 0, l, 0],
[0, 0, j, 0, 0, 0, k, 0, 0, 1, 0, 0, l],
[0, 0, 0, 0, d, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, d, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0, d, 0, 0, 0, 0, 0, 1]])
cc = M.connected_components()
assert cc == [[0, 4, 7, 10], [1, 5, 8, 11], [2, 6, 9, 12], [3]]
P, B = M.connected_components_decomposition()
p = Permutation([0, 4, 7, 10, 1, 5, 8, 11, 2, 6, 9, 12, 3])
assert P == PermutationMatrix(p)
B0 = Matrix([
[a, b, 0, c],
[m, 1, 0, 0],
[j, k, 1, l],
[0, d, 0, 1]])
B1 = Matrix([
[d, e, 0, f],
[m, 1, 0, 0],
[j, k, 1, l],
[0, d, 0, 1]])
B2 = Matrix([
[g, h, 0, i],
[m, 1, 0, 0],
[j, k, 1, l],
[0, d, 0, 1]])
B3 = Matrix([[1]])
assert B == BlockDiagMatrix(B0, B1, B2, B3)
def test_strongly_connected_components():
M = Matrix([
[11, 14, 10, 0, 15, 0],
[0, 44, 0, 0, 45, 0],
[1, 4, 0, 0, 5, 0],
[0, 0, 0, 22, 0, 23],
[0, 54, 0, 0, 55, 0],
[0, 0, 0, 32, 0, 33]])
scc = M.strongly_connected_components()
assert scc == [[1, 4], [0, 2], [3, 5]]
P, B = M.strongly_connected_components_decomposition()
p = Permutation([1, 4, 0, 2, 3, 5])
assert P == PermutationMatrix(p)
assert B == BlockMatrix([
[
Matrix([[44, 45], [54, 55]]),
Matrix.zeros(2, 2),
Matrix.zeros(2, 2)
],
[
Matrix([[14, 15], [4, 5]]),
Matrix([[11, 10], [1, 0]]),
Matrix.zeros(2, 2)
],
[
Matrix.zeros(2, 2),
Matrix.zeros(2, 2),
Matrix([[22, 23], [32, 33]])
]
])
P = P.as_explicit()
B = B.as_explicit()
assert P.T * B * P == M
P, B = M.strongly_connected_components_decomposition(lower=False)
p = Permutation([3, 5, 0, 2, 1, 4])
assert P == PermutationMatrix(p)
assert B == BlockMatrix([
[
Matrix([[22, 23], [32, 33]]),
Matrix.zeros(2, 2),
Matrix.zeros(2, 2)
],
[
Matrix.zeros(2, 2),
Matrix([[11, 10], [1, 0]]),
Matrix([[14, 15], [4, 5]])
],
[
Matrix.zeros(2, 2),
Matrix.zeros(2, 2),
Matrix([[44, 45], [54, 55]])
]
])
P = P.as_explicit()
B = B.as_explicit()
assert P.T * B * P == M
|