# Smith Normal Form

After executing the following for computing the snf (or Smith normal form) of a matrix I got two different results.

In the second case I expected to get integer transformation matrices.

The code below can be run in sagecell.

sage: B = matrix([[1143, 2361, -1995, 2694],
[-342, -704, 595, -806],
[-454, -936, 791, -1070],
[-521, -1077, 910, -1228]])

sage: C = matrix(QQ, [[1143, 2361, -1995, 2694],
[-342, -704, 595, -806],
[-454, -936, 791, -1070],
[-521, -1077, 910, -1228]])

sage: print(type(C), "\n", type(B))
<class 'sage.matrix.matrix_rational_dense.Matrix_rational_dense'>
<class 'sage.matrix.matrix_integer_dense.Matrix_integer_dense'>

sage: print(C == B)
True

sage: show(B.smith_form())
([1 0 0 0]   [ 0  1  0  0]
[0 1 0 0]   [-5 -1 -3 -8]
[0 0 0 0]   [19  3 10 31]
[0 0 0 0],  [ 7  0  5 11],

[      0       0       1       0]
[   4505     -53 1549031     112]
[   5715     -60 1963950     142]
[    284       2   96814       7])

sage: show(C.smith_form())
([1 0 0 0]  [ 1/1143       0       0       0]
[0 1 0 0]  [ 19/155 127/310       0       0]
[0 0 0 0]  [   8/45  -11/15       1       0]
[0 0 0 0], [    5/9     1/3       0       1],

[       1 -787/381     7/62   -71/31]
[       0        1    49/62    -1/31]
[       0        0        1        0]
[       0        0        0        1])

edit retag close merge delete

Sort by ยป oldest newest most voted

In the second case you are explicitly defining a matrix over the rationals, so you get rational matrices as output. There is an option C.smith_form(integral=True) which will force Sage to work over the integers. That happens to give different integer matrices than B.smith_form() in this case.

more

OK. I tried this, I got different transformation matrices (for cases (1),(2)) . But the cases (1) and (3) provides exactly the same Transformation matrices.

B.smith_form() #1
C.smith_form(integral=True) #2
matrix(ZZ,C).smith_form() #3


I had in my mind that snf is defined over a PID, so I supposed even if the matrix is defined over Q, finally I get the transformation matrices over Z. Thanks.

( 2022-10-23 14:10:15 +0200 )edit

There is nothing unexpected here. While the SNF is defined uniquely, there may exist different transformation matrices (resulting in the same SNF).

( 2022-10-23 20:12:58 +0200 )edit
1

Also, the code used for integer matrices is different from that used for general matrices (even when integral=Trueis specified). So it is not an issue that the transformation matrices are different. Integer matrices: https://github.com/sagemath/sage/blob.... General matrices, including over QQ: https://github.com/sagemath/sage/blob...

( 2022-10-23 22:07:34 +0200 )edit