-
-
Notifications
You must be signed in to change notification settings - Fork 503
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gh-35502:
sage.rings.factorint
: Modularization fixes
<!-- Please provide a concise, informative and self-explanatory title. --> <!-- Don't put issue numbers in the title. Put it in the Description below. --> <!-- For example, instead of "Fixes #12345", use "Add a new method to multiply two integers" --> ### 📚 Description <!-- Describe your changes here in detail. --> Spliiting `sage.rings.factorint` into separate modules, per library used. <!-- Why is this change required? What problem does it solve? --> <!-- If this PR resolves an open issue, please link to it here. For example "Fixes #12345". --> Part of: - #29705 <!-- If your change requires a documentation PR, please link it appropriately. --> ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. It should be `[x]` not `[x ]`. --> - [x] The title is concise, informative, and self-explanatory. - [x] The description explains in detail what this PR is about. - [x] I have linked a relevant issue or discussion. - [ ] I have created tests covering the changes. - [x] I have updated the documentation accordingly. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on - #12345: short description why this is a dependency - #34567: ... --> <!-- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> URL: #35502 Reported by: Matthias Köppe Reviewer(s): Kwankyu Lee
- Loading branch information
Showing
6 changed files
with
208 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
# sage.doctest: optional - sage.libs.flint | ||
r""" | ||
Integer factorization using FLINT | ||
AUTHORS: | ||
- Michael Orlitzky (2023) | ||
""" | ||
|
||
#***************************************************************************** | ||
# Copyright (C) 2023 Michael Orlitzky | ||
# | ||
# Distributed under the terms of the GNU General Public License (GPL) | ||
# as published by the Free Software Foundation; either version 2 of | ||
# the License, or (at your option) any later version. | ||
# http://www.gnu.org/licenses/ | ||
#***************************************************************************** | ||
|
||
from cysignals.signals cimport sig_on, sig_off | ||
|
||
from sage.libs.flint.fmpz cimport fmpz_t, fmpz_init, fmpz_set_mpz | ||
from sage.libs.flint.fmpz_factor cimport * | ||
from sage.rings.integer cimport Integer | ||
|
||
|
||
def factor_using_flint(Integer n): | ||
r""" | ||
Factor the nonzero integer ``n`` using FLINT. | ||
This function returns a list of (factor, exponent) pairs. The | ||
factors will be of type ``Integer``, and the exponents will be of | ||
type ``int``. | ||
INPUT: | ||
- ``n`` -- a nonzero sage Integer; the number to factor. | ||
OUTPUT: | ||
A list of ``(Integer, int)`` pairs representing the factors and | ||
their exponents. | ||
EXAMPLES:: | ||
sage: from sage.rings.factorint_flint import factor_using_flint | ||
sage: n = ZZ(9962572652930382) | ||
sage: factors = factor_using_flint(n) | ||
sage: factors | ||
[(2, 1), (3, 1), (1660428775488397, 1)] | ||
sage: prod( f^e for (f,e) in factors ) == n | ||
True | ||
Negative numbers will have a leading factor of ``(-1)^1``:: | ||
sage: n = ZZ(-1 * 2 * 3) | ||
sage: factor_using_flint(n) | ||
[(-1, 1), (2, 1), (3, 1)] | ||
The factorization of unity is empty:: | ||
sage: factor_using_flint(ZZ.one()) | ||
[] | ||
While zero has a single factor, of... zero:: | ||
sage: factor_using_flint(ZZ.zero()) | ||
[(0, 1)] | ||
TESTS: | ||
Check that the integers [-10,000, 10,000] are factored correctly:: | ||
sage: all( | ||
....: prod( f^e for (f,e) in factor_using_flint(ZZ(c*k)) ) == c*k | ||
....: for k in range(10000) | ||
....: for c in [-1, 1] | ||
....: ) | ||
True | ||
""" | ||
if n.is_zero(): | ||
return [(n, int(1))] | ||
|
||
cdef fmpz_t p | ||
fmpz_init(p) | ||
fmpz_set_mpz(p, (<Integer>n).value) | ||
|
||
cdef fmpz_factor_t factors | ||
fmpz_factor_init(factors) | ||
|
||
sig_on() | ||
fmpz_factor(factors, p) | ||
sig_off() | ||
|
||
pairs = fmpz_factor_to_pairlist(factors) | ||
|
||
fmpz_factor_clear(factors) | ||
return pairs |
Oops, something went wrong.