Skip to content

Add flatter support #40274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/sage/features/flatter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
r"""
Features for testing the presence of ``flatter``
"""

from . import Executable


class flatter(Executable):
"""
A :class:`~sage.features.Feature` describing the presence of ``flatter``.

EXAMPLES::

sage: from sage.features.flatter import flatter
sage: flatter().is_present() # optional - flatter
FeatureTestResult('flatter', True)
"""
def __init__(self):
r"""
TESTS::

sage: from sage.features.flatter import flatter
sage: isinstance(flatter(), flatter)
True
"""
Executable.__init__(self, "flatter", executable="flatter")


def all_features():
return [flatter()]
40 changes: 40 additions & 0 deletions src/sage/matrix/matrix_integer_dense.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3024,6 +3024,10 @@ cdef class Matrix_integer_dense(Matrix_dense):

- ``'pari'`` -- pari's qflll

- ``'flatter'`` -- external executable ``flatter``, requires manual install (see caveats below).
Note that sufficiently new version of ``pari`` also supports FLATTER algorithm, see
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please provide Pari version info where this appeared. Also, do you know if this already is available from cypari2 ?

Copy link
Contributor Author

@user202729 user202729 Jun 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • New recursive implementation of the qflll and qflllgram functions
    using the FLATTER reduction algorithm of N. Heninger and K. Ryan.

from https://pari.math.u-bordeaux.fr/archives/pari-announce-24/msg00003.html

so, 2.17.0.

supposedly it's available

sage: pari.version()
(2, 17, 1)

but pari is still slower than flatter on my machine. Demo where LLL is used to compute (integral) kernel of a matrix:

m = random_matrix(ZZ, 25, 50, x=1, y=10^100)
assert m.rank() == 25

%time m.change_ring(QQ).right_kernel_matrix()  # 2.5s
%time m.right_kernel_matrix()  # 8s

%time m1 = (m.T*2^1200).augment(matrix.identity(50)).LLL(algorithm="pari")  # 5s
assert m1[:25,:25]==0

%time m1 = (m.T*2^1200).augment(matrix.identity(50)).LLL(algorithm="flatter")  # 3s (CPU time is small because time is spent in subprocess)
assert m1[:25,:25]==0

more:

%time m.right_kernel_matrix(algorithm='flint')  # 6s
%time m.right_kernel_matrix(algorithm='pari')   # unreasonably long
%time m.right_kernel_matrix(algorithm='padic')  # 8.5s

%time m.change_ring(QQ).right_kernel_matrix(algorithm='flint')  # 2.2s
%time m.change_ring(QQ).right_kernel_matrix(algorithm='padic')  # 2.2s

https://pari.math.u-bordeaux.fr/dochtml/html/Vectors__matrices__linear_algebra_and_sets.html#qflll.

OUTPUT: a matrix over the integers

EXAMPLES::
Expand Down Expand Up @@ -3097,6 +3101,16 @@ cdef class Matrix_integer_dense(Matrix_dense):
[0 0 0]
[0 0 0]

When ``algorithm='flatter'``, some matrices are not supported depends
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the sentence is broken. When is a Germanism, too. You want something like

The ``algorithm='flatter'`` might not support matrices supported by other algorithms.

on ``flatter``. For example::

sage: # needs flatter
sage: m = matrix.zero(3, 2)
sage: m.LLL(algorithm='flatter')
Traceback (most recent call last):
...
ValueError: ...

TESTS::

sage: matrix(ZZ, 0, 0).LLL()
Expand Down Expand Up @@ -3153,6 +3167,17 @@ cdef class Matrix_integer_dense(Matrix_dense):
Although LLL is a deterministic algorithm, the output for
different implementations and CPUs (32-bit vs. 64-bit) may
vary, while still being correct.

Check ``flatter``::

sage: # needs flatter
sage: M = matrix(ZZ, 2, 2, [-1,1,1,1])
sage: L = M.LLL(algorithm="flatter")
sage: abs(M.det()) == abs(L.det())
True
sage: L = M.LLL(algorithm="flatter", delta=0.99)
sage: abs(M.det()) == abs(L.det())
True
"""
if self.ncols() == 0 or self.nrows() == 0:
verbose("Trivial matrix, nothing to do")
Expand All @@ -3177,6 +3202,21 @@ cdef class Matrix_integer_dense(Matrix_dense):
raise TypeError("precision prec must be >= 0")
prec = int(prec)

if algorithm == 'flatter':
import subprocess
cmd = ["flatter"]
if fp is not None or early_red or use_givens or transformation or eta is not None or use_siegel:
raise TypeError("flatter does not support fp, early_red, use_givens, transformation, eta or use_siegel")
if kwds:
raise TypeError("flatter does not support additional keywords")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it be more consistent to just ignore kwds here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better practice to complain to the user about invalid data than to silently ignore them.

if delta is not None:
cmd += ["-delta", str(delta)]
stdout = subprocess.run(
cmd, input="[" + "\n".join('[' + ' '.join(str(x) for x in row) + ']' for row in self) + "]",
text=True, encoding="utf-8", stdout=subprocess.PIPE).stdout
return self.new_matrix(entries=[[ZZ(x) for x in row.strip('[] ').split()]
for row in stdout.strip('[] \n').split('\n')])

if algorithm == 'NTL:LLL':
if fp is None:
algorithm = 'NTL:LLL'
Expand Down
Loading