Skip to content
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

feat: IntegerMatrix should supports mpz #268

Open
alxiong opened this issue Feb 28, 2024 · 5 comments
Open

feat: IntegerMatrix should supports mpz #268

alxiong opened this issue Feb 28, 2024 · 5 comments

Comments

@alxiong
Copy link
Contributor

alxiong commented Feb 28, 2024

I was reading assign_mpz and see that even if internally the IntegerMatrix uses mpz, it doesn't accept value passed in as type mpz.

ideally I want to do:

from fpylll import IntegerMatrix
from gmpy2 import mpz

A = IntegerMatrix(5, 5)
A.gen_identity(5)
a = mpz(1000)
A[2, 3] = a

(p.s. altho I agree that SageMath support is good enough if I want to use multi-precision integers, it would requires me to go for sage's own virtualenv, unlike a simpler gmpy2 package)

@alxiong
Copy link
Contributor Author

alxiong commented Jun 24, 2024

this should do the work:

alxiong@207265d

for conveniences:

+++ b/src/fpylll/io.pyx
@@ -10,6 +10,7 @@ from .gmp.mpz cimport mpz_t, mpz_set_si, mpz_set
 from cpython.version cimport PY_MAJOR_VERSION
 from .fplll.fplll cimport FT_DEFAULT, FT_DOUBLE, FT_LONG_DOUBLE, FT_DPE, FT_MPFR
 from .fplll.fplll cimport ZT_MPZ, ZT_LONG
+cimport gmpy2
 
 # Note: this uses fpylll's numpy and not the global numpy package.
 from .numpy import is_numpy_integer
@@ -37,13 +38,17 @@ cdef int assign_Z_NR_mpz(Z_NR[mpz_t]& t, value) except -1:
 cdef int assign_mpz(mpz_t& t, value) except -1:
     if isinstance(value, long):
         mpz_set_pylong(t, value)
         return 0
+    if isinstance(value, gmpy2.mpz):
+        gmpy2.import_gmpy2()   # needed to initialize the C-API
+        mpz_set(t, gmpy2.MPZ(value))
+        return 0

tested locally, i can compile fpylll and also pass in gmpy2.mpz values.

@malb
Copy link
Collaborator

malb commented Jun 24, 2024

Is gmpy2 the mpz_t wrapper to use? I'm a bit hesitant to add more dependencies.

@cr-marcstevens
Copy link
Collaborator

I kind of agree with @malb here: are we going to support every multiprec python option by bringing it in as a requirement for fpylll?

My suggestion would be to look at a generic solution that works for any bigint type of python object by manually chopping it in limbs and reconstruct it in the fplll mpz.

@alxiong
Copy link
Contributor Author

alxiong commented Jun 24, 2024

i agree with your concern, and don't have any strong opinion on how should fpylll handles these.

feel free to close this issue.

(i was only hoping to use this issue as future reference in case anyone wants to use gmpy2 library, since sagemath couldn't be easily installed as PyPI package like gmpy2 does.
at least the 3 lines of code above, showcase how one can modify the fpylll to support that)

@malb
Copy link
Collaborator

malb commented Jun 24, 2024

I wonder if we could add Marc's idea though: an interface that takes an input and repeatedly considers it mod MAX_INT_SIZE to construct the internal integer? This then may or may not be faster than just reading in a string?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants