-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
PEP 791: imath --- module for integer-specific mathematics functions #4422
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
849338f
PEP 791: imath --- module for number-theoretic functions
skirpichev 8e04e48
Apply suggestions from code review
skirpichev 0245fda
address review: disband References (inline them)
skirpichev 13f82ad
Apply suggestions from code review
skirpichev f92dc1b
address review: add old discussion link
skirpichev 549ca1a
address review: c_div naming
skirpichev b5ff41b
address review: grammar nits
skirpichev 1013832
Apply suggestions from code review
skirpichev 1b19066
address review: re-arrange Imath note
skirpichev 5054afb
address review: rework note about primality testing, etc
skirpichev d7b3aec
address review: add "How to Teach This"
skirpichev 37e3001
Apply suggestions from code review
skirpichev eac5a4a
Apply suggestions from code review
skirpichev c4857e3
address review: link to imath
skirpichev 341e1fc
address review: change title
skirpichev 01017e3
address review by tim-one (and maybe mdickinson)
skirpichev 11b5d7f
Include argument from the d.p.o thread
skirpichev cc29b81
Update .github/CODEOWNERS
vstinner ffe9e8e
Apply suggestions from code review
skirpichev ad587ab
address review: move part of Abstract to the Specification
skirpichev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,165 @@ | ||
| PEP: 791 | ||
| Title: imath --- module for number-theoretic functions | ||
| Author: Sergey B Kirpichev <skirpichev@gmail.com> | ||
| Sponsor: Victor Stinner <vstinner@python.org> | ||
| Discussions-To: Pending | ||
| Status: Draft | ||
| Type: Standards Track | ||
| Created: 10-May-2025 | ||
|
skirpichev marked this conversation as resolved.
Outdated
|
||
| Python-Version: 3.15 | ||
| Post-History: 02-Jun-2019, | ||
|
skirpichev marked this conversation as resolved.
Outdated
|
||
| `09-May-2025 <https://discuss.python.org/t/91337>`__, | ||
|
|
||
|
|
||
| Abstract | ||
| ======== | ||
|
|
||
| This PEP proposes a new module for number-theoretical, combinatorial and other | ||
| integer-valued functions defined for integer arguments, like | ||
| :external+py3.14:func:`math.gcd` or :external+py3.14:func:`math.isqrt`. | ||
|
skirpichev marked this conversation as resolved.
skirpichev marked this conversation as resolved.
|
||
|
|
||
|
|
||
| Motivation | ||
| ========== | ||
|
|
||
| The :external+py3.14:mod:`math` documentation says: "This module provides access | ||
| to the mathematical functions defined by the C standard." But as a state of | ||
|
skirpichev marked this conversation as resolved.
Outdated
|
||
| art, over time the module was populated with functions that aren't related to | ||
| the C standard or floating-point arithmetics. Now it's much harder to describe | ||
| module scope, content and interfaces (returned values or accepted arguments). | ||
|
skirpichev marked this conversation as resolved.
|
||
|
|
||
| For example, the :external+py3.14:mod:`math` module documentation says: "Except | ||
| when explicitly noted otherwise, all return values are floats." This is not | ||
|
skirpichev marked this conversation as resolved.
Outdated
|
||
| longer true: *None* of the functions listed in the "Number-theoretic | ||
| functions" [1]_ subsection of the documentation returns a float, but the | ||
| documentation doesn't say so. In the proposed module a similar sentence "All | ||
| return values are integers." could tell the truth once. In a similar way we | ||
|
skirpichev marked this conversation as resolved.
Outdated
|
||
| can simplify description of accepted arguments for both the | ||
|
skirpichev marked this conversation as resolved.
Outdated
|
||
| :external+py3.14:mod:`math` and the new module. | ||
|
skirpichev marked this conversation as resolved.
|
||
|
|
||
| Apparently, the :external+py3.14:mod:`math` can't serve as a catch-all place | ||
| for mathematical functions: we have also the :external+py3.14:mod:`cmath` and | ||
|
skirpichev marked this conversation as resolved.
Outdated
skirpichev marked this conversation as resolved.
Outdated
|
||
| the :external+py3.14:mod:`statistics`. Let's make same for integer-related | ||
| functions. It would provide shared context, which reduces verbosity in the | ||
| documentation and conceptual load. It also aids discoverability through | ||
| grouping related functions and IDEs suggesting helpful completions. | ||
|
skirpichev marked this conversation as resolved.
Outdated
|
||
|
|
||
|
|
||
| Specification | ||
| ============= | ||
|
|
||
| The PEP proposes moving the following integer-related functions [1]_ in a new | ||
| module, called ``imath``: | ||
|
|
||
| * :external+py3.14:func:`~math.comb` | ||
| * :external+py3.14:func:`~math.factorial` | ||
| * :external+py3.14:func:`~math.gcd` | ||
| * :external+py3.14:func:`~math.isqrt` | ||
| * :external+py3.14:func:`~math.lcm` | ||
| * :external+py3.14:func:`~math.perm` | ||
|
|
||
| Their aliases in :external+py3.14:mod:`math` will be :term:`soft deprecated`. | ||
|
|
||
| Modules functions will accept integers and objects that implement the | ||
|
skirpichev marked this conversation as resolved.
Outdated
|
||
| :external+py3.14:meth:`~object.__index__` method which is used to convert the | ||
|
skirpichev marked this conversation as resolved.
Outdated
|
||
| object to an integer number. | ||
|
|
||
| Possible extensions for the new module and it's scope are discussed in the | ||
|
skirpichev marked this conversation as resolved.
Outdated
|
||
| `Open Issues <Open Issues_>`_ section. New functions are not part of the | ||
|
skirpichev marked this conversation as resolved.
Outdated
|
||
| proposal. | ||
|
|
||
|
|
||
| Backwards Compatibility | ||
| ======================= | ||
|
|
||
| As aliases in :external+py3.14:mod:`math` will be kept for indefinite time | ||
|
skirpichev marked this conversation as resolved.
Outdated
|
||
| (their use would be discouraged), there are no anticipated code breaks. | ||
|
|
||
|
|
||
| Reference Implementation | ||
| ======================== | ||
|
|
||
| https://github.com/python/cpython/pull/133909 | ||
|
skirpichev marked this conversation as resolved.
Outdated
|
||
|
|
||
|
|
||
| Open Issues | ||
| =========== | ||
|
|
||
| Module name | ||
| ----------- | ||
|
|
||
| Chosen name seems consistent with other domain-specific mathematical module: | ||
|
skirpichev marked this conversation as resolved.
Outdated
|
||
| :external+py3.14:mod:`cmath` (for complex numbers). | ||
|
skirpichev marked this conversation as resolved.
|
||
|
|
||
| There is already an ``imath`` project on PyPI, but only with two releases, with | ||
| the most recent one 4 years ago. Its repository is no longer accessible. | ||
|
skirpichev marked this conversation as resolved.
Outdated
|
||
| The `Imath <https://github.com/AcademySoftwareFoundation/Imath>`_ C++ library | ||
| include Python bindings with same name. | ||
|
skirpichev marked this conversation as resolved.
Outdated
|
||
|
|
||
| `Polling shows <https://discuss.python.org/t/91337/35>`_ ``intmath`` as another | ||
|
skirpichev marked this conversation as resolved.
Outdated
skirpichev marked this conversation as resolved.
Outdated
|
||
| popular name. The argument made was that the normal mathematical spelling of | ||
| the imaginary unit is ``i``, which makes imath ambiguous. It also has no conflict | ||
|
skirpichev marked this conversation as resolved.
Outdated
|
||
| with any PyPI module. On the other hand, ``intmath`` may be confused with | ||
| interval math or numerical integration. | ||
|
|
||
| Other proposed names include ``ntheory`` (like SymPy's submodule), | ||
| ``integermath`` and ``imaths``. | ||
|
|
||
|
|
||
| Module scope and possible extensions | ||
| ------------------------------------ | ||
|
|
||
| Unless we can just provide bindings to some well supported mathematical library | ||
| like the GMP, the module scope should be limited. For example, no primality | ||
|
skirpichev marked this conversation as resolved.
|
||
| testing and factorization. | ||
|
skirpichev marked this conversation as resolved.
Outdated
skirpichev marked this conversation as resolved.
Outdated
|
||
|
|
||
| There are possible additions, among proposed in the initial discussion thread | ||
|
skirpichev marked this conversation as resolved.
Outdated
|
||
| (see also [5]_): | ||
|
skirpichev marked this conversation as resolved.
Outdated
|
||
|
|
||
| * ``c_div()`` --- for integer ceiling divide, see [2]_, [3]_. | ||
| * ``gcdext()`` --- to solve linear Diophantine equation in two variables (the | ||
|
skirpichev marked this conversation as resolved.
Outdated
|
||
| :external+py3.14:class:`int` implementation actually include extended | ||
|
skirpichev marked this conversation as resolved.
Outdated
|
||
| Euclidean algorithm) | ||
| * ``isqrt_rem()`` --- to return both integer square root and a remainder (if | ||
|
skirpichev marked this conversation as resolved.
Outdated
|
||
| integer isn't a perfect square) | ||
|
skirpichev marked this conversation as resolved.
Outdated
|
||
| * ``ilog()`` --- integer logarithm, currently :external+py3.14:func:`math.log` | ||
|
skirpichev marked this conversation as resolved.
Outdated
|
||
| has a special handling for integer arguments. It's unique (wrt other module | ||
|
skirpichev marked this conversation as resolved.
Outdated
|
||
| functions) and not documented so far, see [4]_ | ||
| * ``fibonacci()``. | ||
|
|
||
|
vstinner marked this conversation as resolved.
|
||
|
|
||
| Rejected ideas | ||
| ============== | ||
|
|
||
| There was a brief discussion about exposing :external+py3.14:func:`math.isqrt` | ||
| as ``imath.sqrt`` in the same way that :external+py3.14:func:`cmath.sqrt` is | ||
| the complex version of :external+py3.14:func:`math.sqrt`. However, ``isqrt`` | ||
| is ultimately a different function: it is the floor of the square root. It | ||
| would be confusing to give it the same name (under a different module). | ||
|
|
||
|
|
||
| Acknowledgements | ||
| ================ | ||
|
|
||
| Thanks to Tim Peters for reviving the idea of the :external+py3.14:mod:`math` | ||
| splitting. Thanks to Neil Girdhar for substantial improvements of | ||
|
skirpichev marked this conversation as resolved.
Outdated
|
||
| the initial draft. | ||
|
|
||
|
|
||
| Footnotes | ||
|
skirpichev marked this conversation as resolved.
Outdated
|
||
| ========= | ||
|
|
||
| .. [1] Number-theoretic functions | ||
| (https://docs.python.org/3.14/library/math.html#number-theoretic-functions) | ||
| .. [2] Integer ceiling divide | ||
| (https://discuss.python.org/t/91269) | ||
| .. [3] https://gmpy2.readthedocs.io/en/stable/mpz.html#gmpy2.c_div | ||
| .. [4] https://github.com/python/cpython/issues/120950 | ||
| .. [5] https://github.com/python/cpython/issues/81313 | ||
|
|
||
|
|
||
| Copyright | ||
| ========= | ||
|
|
||
| This document is placed in the public domain or under the | ||
| CC0-1.0-Universal license, whichever is more permissive. | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.