matrixlatticesage

sagemath: How to get matrix LLL axis transformation?


I tried using the LLL method on matrices in sagemath to obtain an LLL-reduced basis for my lattice. I noticed, that the resulting lattice switched around some coordinate axis and I'm trying to figure out how to get the coordinate transformation necessary for twisting the rest of my vectors into the same direction.

I noticed that there is a parameter called "transformation". According to the documentation it should return some transformation matrix, but all I get is an error:

m = matrix(QQ, 2, 2, [[5,0], [0,2]])
m.LLL()

Output:
[0 2]
[5 0]
m = matrix(QQ, 2, 2, [[5,0], [0,2]])
m.LLL(transformation=True)

Output:
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In [186], line 2
      1 m = matrix(QQ, Integer(2), Integer(2), [[Integer(5),Integer(0)], [Integer(0),Integer(2)]])
----> 2 m.LLL(transformation=True)

File /private/var/tmp/sage-9.8-current/local/var/lib/sage/venv-python3.11.1/lib/python3.11/site-packages/sage/matrix/matrix_rational_dense.pyx:2964, in sage.matrix.matrix_rational_dense.Matrix_rational_dense.LLL (build/cythonized/sage/matrix/matrix_rational_dense.cpp:25855)()
   2962 """
   2963 A, d = self._clear_denom()
-> 2964 return A.LLL(*args, **kwargs) / d
   2965 
   2966 

File /private/var/tmp/sage-9.8-current/local/var/lib/sage/venv-python3.11.1/lib/python3.11/site-packages/sage/rings/integer.pyx:2034, in sage.rings.integer.Integer.__truediv__ (build/cythonized/sage/rings/integer.c:14116)()
   2032         return y
   2033 
-> 2034     return coercion_model.bin_op(left, right, operator.truediv)
   2035 
   2036 cpdef _div_(self, right):

File /private/var/tmp/sage-9.8-current/local/var/lib/sage/venv-python3.11.1/lib/python3.11/site-packages/sage/structure/coerce.pyx:1248, in sage.structure.coerce.CoercionModel.bin_op (build/cythonized/sage/structure/coerce.c:11847)()
   1246     # We should really include the underlying error.
   1247     # This causes so much headache.
-> 1248     raise bin_op_exception(op, x, y)
   1249 
   1250 cpdef canonical_coercion(self, x, y):

TypeError: unsupported operand parent(s) for /: '<class 'tuple'>' and 'Integer Ring'

Edit: It seems like this issue only comes up over rational matrices. As I need to support rational matrices, this doesn't help unfortunately ...


Solution

  • There is no axis transformation, the algorithm just uses the rows of the matrix to be the basis vectors, not the columns.

    The transformation obtained from the parameter "transformation" does not return a matrix representing the axis transformation, just the unimodular matrix for getting from the basis to the lll-reduced basis. This transformation matrix can be calculated using transformation = basis.solve_left(lllBasis) and solves the equation transformation * basis = lllBasis.