Skip to content

Commit

Permalink
Merge pull request #252 from fplll/numpy_arrays
Browse files Browse the repository at this point in the history
Add support for converting (integer) numpy arrays to IntegerMatrix.
  • Loading branch information
malb committed Aug 17, 2023
2 parents 6595410 + b8c8e17 commit dc0f213
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/fpylll/fplll/integer_matrix.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@ cdef class IntegerMatrix:
>>> B[0,0]
1
IntegerMatrix also supports numpy's integer types, if numpy is supported.
See tests/test_numpy.py for example usage.
"""
self._type = check_int_type(int_type)

Expand Down
12 changes: 11 additions & 1 deletion src/fpylll/io.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ 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

# Note: this uses fpylll's numpy and not the global numpy package.
IF HAVE_NUMPY:
from .numpy import is_numpy_integer

IF HAVE_QD:
from fpylll.fplll.fplll cimport FT_DD, FT_QD

Expand All @@ -36,7 +40,7 @@ cdef int assign_Z_NR_mpz(Z_NR[mpz_t]& t, value) except -1:
cdef int assign_mpz(mpz_t& t, value) except -1:
"""
Assign Python integer to Z_NR[mpz_t]
"""
"""
if isinstance(value, int) and PY_MAJOR_VERSION == 2:
mpz_set_si(t, PyInt_AS_LONG(value))
return 0
Expand All @@ -49,6 +53,12 @@ cdef int assign_mpz(mpz_t& t, value) except -1:
mpz_set_pylong(t, value)
return 0

IF HAVE_NUMPY:
if is_numpy_integer(value):
value = long(value)
mpz_set_pylong(t, value)
return 0

raise NotImplementedError("Type '%s' not supported"%type(value))

cdef object mpz_get_python(mpz_srcptr z):
Expand Down
9 changes: 9 additions & 0 deletions src/fpylll/numpy.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ IF not HAVE_NUMPY:

import numpy
from numpy.__init__ cimport ndarray # TODO: that __init__ shouldn't be needed
from numpy.__init__ cimport integer as np_integer

def _dump_mu(ndarray[double, ndim=2, mode="c"] mu not None, MatGSO M, int kappa, int block_size):
u"""
Expand Down Expand Up @@ -130,3 +131,11 @@ def dump_r(MatGSO M, int kappa, int block_size):
r = ndarray(dtype='float64', shape=block_size)
_dump_r(r, M, kappa, block_size)
return r

def is_numpy_integer(value):
"""
Return true if value is a numpy integer, false otherwise.
:param value: the value to be checked.
:returns: True if value is a numpy integer, false otherwise.
"""
return isinstance(value, np_integer)
11 changes: 11 additions & 0 deletions tests/test_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,14 @@ def test_dump_r(nrows=10):

for i in range(nrows):
assert abs(M.get_r(i, i) - r[i]) < 0.001


def test_is_numpy_integer(nrows=10):
if not have_numpy:
return

import numpy as np
B = np.eye(nrows, dtype=np.int32)
Bfpy = IntegerMatrix.from_matrix(B)
for i in range(nrows):
assert Bfpy[i][i] == 1

0 comments on commit dc0f213

Please sign in to comment.