File size: 3,730 Bytes
ac2f8e9 |
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 |
from sympy.testing.pytest import warns_deprecated_sympy
from sympy.core.symbol import Symbol
from sympy.polys.polytools import Poly
from sympy.matrices import Matrix, randMatrix
from sympy.matrices.normalforms import (
invariant_factors,
smith_normal_form,
smith_normal_decomp,
hermite_normal_form,
is_smith_normal_form,
)
from sympy.polys.domains import ZZ, QQ
from sympy.core.numbers import Integer
import random
def test_smith_normal():
m = Matrix([[12,6,4,8],[3,9,6,12],[2,16,14,28],[20,10,10,20]])
smf = Matrix([[1, 0, 0, 0], [0, 10, 0, 0], [0, 0, 30, 0], [0, 0, 0, 0]])
assert smith_normal_form(m) == smf
a, s, t = smith_normal_decomp(m)
assert a == s * m * t
x = Symbol('x')
with warns_deprecated_sympy():
m = Matrix([[Poly(x-1), Poly(1, x),Poly(-1,x)],
[0, Poly(x), Poly(-1,x)],
[Poly(0,x),Poly(-1,x),Poly(x)]])
invs = 1, x - 1, x**2 - 1
assert invariant_factors(m, domain=QQ[x]) == invs
m = Matrix([[2, 4]])
smf = Matrix([[2, 0]])
assert smith_normal_form(m) == smf
prng = random.Random(0)
for i in range(6):
for j in range(6):
for _ in range(10 if i*j else 1):
m = randMatrix(i, j, max=5, percent=50, prng=prng)
a, s, t = smith_normal_decomp(m)
assert a == s * m * t
assert is_smith_normal_form(a)
s.inv().to_DM(ZZ)
t.inv().to_DM(ZZ)
a, s, t = smith_normal_decomp(m, QQ)
assert a == s * m * t
assert is_smith_normal_form(a)
s.inv()
t.inv()
def test_smith_normal_deprecated():
from sympy.polys.solvers import RawMatrix as Matrix
with warns_deprecated_sympy():
m = Matrix([[12, 6, 4,8],[3,9,6,12],[2,16,14,28],[20,10,10,20]])
setattr(m, 'ring', ZZ)
with warns_deprecated_sympy():
smf = Matrix([[1, 0, 0, 0], [0, 10, 0, 0], [0, 0, 30, 0], [0, 0, 0, 0]])
assert smith_normal_form(m) == smf
x = Symbol('x')
with warns_deprecated_sympy():
m = Matrix([[Poly(x-1), Poly(1, x),Poly(-1,x)],
[0, Poly(x), Poly(-1,x)],
[Poly(0,x),Poly(-1,x),Poly(x)]])
setattr(m, 'ring', QQ[x])
invs = (Poly(1, x, domain='QQ'), Poly(x - 1, domain='QQ'), Poly(x**2 - 1, domain='QQ'))
assert invariant_factors(m) == invs
with warns_deprecated_sympy():
m = Matrix([[2, 4]])
setattr(m, 'ring', ZZ)
with warns_deprecated_sympy():
smf = Matrix([[2, 0]])
assert smith_normal_form(m) == smf
def test_hermite_normal():
m = Matrix([[2, 7, 17, 29, 41], [3, 11, 19, 31, 43], [5, 13, 23, 37, 47]])
hnf = Matrix([[1, 0, 0], [0, 2, 1], [0, 0, 1]])
assert hermite_normal_form(m) == hnf
tr_hnf = Matrix([[37, 0, 19], [222, -6, 113], [48, 0, 25], [0, 2, 1], [0, 0, 1]])
assert hermite_normal_form(m.transpose()) == tr_hnf
m = Matrix([[8, 28, 68, 116, 164], [3, 11, 19, 31, 43], [5, 13, 23, 37, 47]])
hnf = Matrix([[4, 0, 0], [0, 2, 1], [0, 0, 1]])
assert hermite_normal_form(m) == hnf
assert hermite_normal_form(m, D=8) == hnf
assert hermite_normal_form(m, D=ZZ(8)) == hnf
assert hermite_normal_form(m, D=Integer(8)) == hnf
m = Matrix([[10, 8, 6, 30, 2], [45, 36, 27, 18, 9], [5, 4, 3, 2, 1]])
hnf = Matrix([[26, 2], [0, 9], [0, 1]])
assert hermite_normal_form(m) == hnf
m = Matrix([[2, 7], [0, 0], [0, 0]])
hnf = Matrix([[1], [0], [0]])
assert hermite_normal_form(m) == hnf
def test_issue_23410():
A = Matrix([[1, 12], [0, 8], [0, 5]])
H = Matrix([[1, 0], [0, 8], [0, 5]])
assert hermite_normal_form(A) == H
|