Skip to content

Commit dda339a

Browse files
committed
Integer conversion between python ints and PPLite::FLINT_Integer now accepts arbitary python integers.
1 parent b24009f commit dda339a

4 files changed

Lines changed: 28 additions & 53 deletions

File tree

pplite/constraint.pyx

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# distutils: libraries = gmp gmpxx pplite m flint
33

44

5-
from gmpy2 cimport GMPy_MPZ_From_mpz, import_gmpy2, mpz, mpz_t, GMPy_MPZ_From_mpz, MPZ_Check
5+
from gmpy2 cimport GMPy_MPZ_From_mpz, import_gmpy2, mpz, mpz_t, GMPy_MPZ_From_mpz, MPZ_Check, MPZ
66
from cython.operator cimport dereference as deref
77
from cpython.object cimport Py_LT, Py_LE, Py_EQ, Py_NE, Py_GT, Py_GE
88
from .linear_algebra import Variable, Affine_Expression, Linear_Expression
@@ -616,16 +616,10 @@ cdef FLINT_Integer_to_Python(FLINT_Integer& integer):
616616

617617

618618
cdef FLINT_Integer Python_int_to_FLINT_Integer(integer):
619-
cdef fmpz_t x
620-
cdef fmpz y
621-
if isinstance(integer, (int, str)):
622-
fmpz_init(x)
623-
fmpz_set_si(x, integer)
624-
return FLINT_Integer(x)
625-
if MPZ_Check(integer): # is this okay?
626-
y = <fmpz> integer
627-
return FLINT_Integer(y)
628-
raise ValueError("Integer Conversion Failed")
619+
cdef mpz x = mpz(integer)
620+
cdef mpz_t w = MPZ(x)
621+
cdef fmpz y = PTR_TO_COEFF(w)
622+
return FLINT_Integer(y)
629623

630624

631625
def FLINT_Integer_Conversion_Check(possible_integer):
@@ -640,4 +634,4 @@ def FLINT_Integer_Conversion_Check(possible_integer):
640634
return True
641635
if MPZ_Check(possible_integer):
642636
return True
643-
return False
637+
return False

pplite/integer_conversions.pyx

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
cimport cython
55

6-
from gmpy2 cimport import_gmpy2, mpz, mpz_t, GMPy_MPZ_From_mpz, MPZ_Check, mpq, MPQ_Check, GMPy_MPQ_From_mpz
6+
from gmpy2 cimport import_gmpy2, mpz, mpz_t, GMPy_MPZ_From_mpz, MPZ_Check, mpq, MPQ_Check, GMPy_MPQ_From_mpz, MPZ
77
from libcpp.vector cimport vector as cppvector
88

99
# It is assumed that the pplite environment is set up to use FLINT_integers.
@@ -12,11 +12,7 @@ from libcpp.vector cimport vector as cppvector
1212
import_gmpy2()
1313

1414
cdef FLINT_Integer_to_Python(FLINT_Integer& integer):
15-
""" Converts FLINT_Integer to python object.
16-
17-
INPUT: :class:`FLINT_Interger` (c++)
18-
19-
OUTPUT: Python object
15+
""" Converts PPlite::FLINT_Integer to python int.
2016
"""
2117
cdef mpz_t new_int
2218
mpz_init(new_int)
@@ -26,26 +22,12 @@ cdef FLINT_Integer_to_Python(FLINT_Integer& integer):
2622
return y
2723

2824
cdef FLINT_Integer Python_int_to_FLINT_Integer(integer):
29-
""" Converts a python object to a c++ FLINT_Integer
30-
31-
INPUT:
32-
33-
- integer: string or int
25+
r"""Converts python int to a PPLite::FLINT_Integer."""
26+
cdef mpz x = mpz(integer)
27+
cdef mpz_t w = MPZ(x)
28+
cdef fmpz y = PTR_TO_COEFF(w)
29+
return FLINT_Integer(y)
3430

35-
OUTPUT:
36-
37-
:class:`FLINT_Integer` (c++)
38-
"""
39-
cdef fmpz_t x
40-
cdef fmpz y
41-
if isinstance(integer, (int, str)):
42-
fmpz_init(x)
43-
fmpz_set_si(x, integer)
44-
return FLINT_Integer(x)
45-
if MPZ_Check(integer): # is this okay?
46-
y = <fmpz> integer
47-
return FLINT_Integer(y)
48-
raise ValueError("Integer Conversion Failed")
4931

5032
cdef FLINT_Rational_to_Python(FLINT_Rational& rational):
5133
"""Converts the Flint_Rational c++ class to a python object.
@@ -102,4 +84,4 @@ def FLINT_Integer_Conversion_Check(possible_integer):
10284
return True
10385
if MPZ_Check(possible_integer):
10486
return True
105-
return False
87+
return False

pplite/linear_algebra.pyx

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33

44
cimport cython
55

6-
from gmpy2 cimport import_gmpy2, mpz, mpz_t, GMPy_MPZ_From_mpz, MPZ_Check
6+
from flint import fmpz as py_fmpz
7+
#from flint.flintlib.functions.fmpz cimport fmpz_t
8+
#from flint cimport f
9+
from gmpy2 cimport import_gmpy2, mpz, mpz_t, GMPy_MPZ_From_mpz, GMPy_MPZ_New, MPZ_Check, MPZ
710
from libcpp.vector cimport vector as cppvector
811
from .constraint cimport _make_Constraint_from_richcmp
912
# from .integer_conversions cimport FLINT_Integer_to_Python, Python_int_to_FLINT_Integer
1013

1114
# TODO: Investigate why everything breaks when importing integer conversion as opposed to local definitions.
12-
15+
# TODO: Once C API is avilable from python-flint; use this to work with flint integers rather than going though gmpy2
1316
import_gmpy2()
1417

1518
cdef FLINT_Integer_to_Python(FLINT_Integer& integer):
@@ -23,16 +26,10 @@ cdef FLINT_Integer_to_Python(FLINT_Integer& integer):
2326

2427
cdef FLINT_Integer Python_int_to_FLINT_Integer(integer):
2528
r"""Converts python sting or int to a PPLite::FLINT_Integer."""
26-
cdef fmpz_t x
27-
cdef fmpz y
28-
if isinstance(integer, (int, str)):
29-
fmpz_init(x)
30-
fmpz_set_si(x, integer)
31-
return FLINT_Integer(x)
32-
if MPZ_Check(integer): # This is a little hacky...
33-
y = <fmpz> integer
34-
return FLINT_Integer(y)
35-
raise ValueError("Integer Conversion Failed")
29+
cdef mpz x = mpz(integer)
30+
cdef mpz_t w = MPZ(x)
31+
cdef fmpz y = PTR_TO_COEFF(w)
32+
return FLINT_Integer(y)
3633

3734
################
3835
### Variable ###
@@ -1512,4 +1509,4 @@ cdef class Affine_Expression(object):
15121509
15131510
>>> from pplite import Variable
15141511
"""
1515-
return _make_Constraint_from_richcmp(self, other, op)
1512+
return _make_Constraint_from_richcmp(self, other, op)

pplite/pplite_decl.pxd

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ ctypedef mp_limb_t ulong
3232
ctypedef mp_limb_signed_t slong
3333

3434
cdef extern from "flint/fmpz.h":
35-
ctypedef slong fmpz
35+
ctypedef int fmpz
3636
ctypedef fmpz fmpz_t[1]
37+
fmpz PTR_TO_COEFF(mpz_ptr ptr)
3738
void fmpz_get_mpz(mpz_t x, const fmpz_t f) noexcept
3839
void fmpz_init(fmpz_t f)
40+
void fmpz_set(fmpz_t f, const fmpz_t g)
3941
void fmpz_set_ui(fmpz_t f, ulong g)
4042
void fmpz_set_si(fmpz_t f, slong g)
4143
void fmpz_clear(fmpz_t f)
@@ -740,4 +742,4 @@ cdef extern from "pplite/pplite.hh" namespace "pplite":
740742

741743
# "pplite/mater_iterator.hh"
742744
# cdef struct[Sys, Impl] Mater_Sys:
743-
# pass
745+
# pass

0 commit comments

Comments
 (0)