Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
remove compile-time dependency on flint from integer
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Kliem committed Aug 28, 2021
1 parent 9986723 commit c08a8b5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
16 changes: 16 additions & 0 deletions src/sage/libs/flint/ulong_extras.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def n_factor_to_list(unsigned long n, int proved):
"""
A wrapper around ``n_factor``.
EXAMPLES::
sage: from sage.libs.flint.ulong_extras import n_factor_to_list
sage: n_factor_to_list(60, 20)
[(2, 2), (3, 1), (5, 1)]
sage: n_factor_to_list((10**6).next_prime() + 1, 0)
[(2, 2), (53, 2), (89, 1)]
"""
cdef n_factor_t f
n_factor_init(&f)
n_factor(&f, n, proved)
return [(f.p[i], int(f.exp[i])) for i in range(f.num)]
24 changes: 12 additions & 12 deletions src/sage/rings/integer.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,6 @@ from sage.libs.gmp.pylong cimport *
from sage.libs.gmp.mpq cimport mpq_neg
from sage.libs.gmp.binop cimport mpq_add_z, mpq_mul_z, mpq_div_zz

from sage.libs.flint.ulong_extras cimport *

import sage.rings.infinity

from sage.structure.coerce cimport is_numpy_type
Expand Down Expand Up @@ -3884,7 +3882,6 @@ cdef class Integer(sage.structure.element.EuclideanDomainElement):

cdef Integer n, p, unit
cdef int i
cdef n_factor_t f

if mpz_sgn(self.value) == 0:
raise ArithmeticError("factorization of 0 is not defined")
Expand All @@ -3907,15 +3904,18 @@ cdef class Integer(sage.structure.element.EuclideanDomainElement):
return factor_trial_division(self, limit)

if mpz_fits_slong_p(n.value):
if proof is None:
from sage.structure.proof.proof import get_flag
proof = get_flag(proof, "arithmetic")
n_factor_init(&f)
n_factor(&f, mpz_get_ui(n.value), proof)
F = [(Integer(f.p[i]), int(f.exp[i])) for i from 0 <= i < f.num]
F.sort()
return IntegerFactorization(F, unit=unit, unsafe=True,
sort=False, simplify=False)
try:
from sage.libs.flint.ulong_extras import n_factor_to_list
if proof is None:
from sage.structure.proof.proof import get_flag
proof = get_flag(proof, "arithmetic")
F = n_factor_to_list(mpz_get_ui(n.value), proof)
F = [(smallInteger(a), smallInteger(b)) for a, b in F]
F.sort()
return IntegerFactorization(F, unit=unit, unsafe=True,
sort=False, simplify=False)
except ImportError:
pass

if mpz_sizeinbase(n.value, 2) < 40:
from sage.rings.factorint import factor_trial_division
Expand Down

0 comments on commit c08a8b5

Please sign in to comment.