-
Notifications
You must be signed in to change notification settings - Fork 0
Copy all information from thread into PEP #9
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
Changes from all commits
969e13e
4da81c7
d04bfd5
a2d8b70
469823a
1552bea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| PEP: 9999 | ||
| Title: imath --- module for number-theoretic functions | ||
| Author: Sergey B Kirpichev <skirpichev@gmail.com> | ||
| Author: Neil Girdhar <misterhsheik@gmail.com>, | ||
| Sergey B Kirpichev <skirpichev@gmail.com> | ||
| Sponsor: Victor Stinner <vstinner@python.org> | ||
| Discussions-To: Pending | ||
| Status: Draft | ||
|
|
@@ -14,41 +15,38 @@ Post-History: 02-Jun-2019, | |
| 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`. | ||
| This PEP proposes a new module for mathematical functions related to number | ||
| theory and combinatorics. | ||
|
|
||
|
|
||
| Motivation | ||
| ========== | ||
|
|
||
| The :external+py3.14:mod:`math` documentaton says: "This module provides access | ||
| to the mathematical functions defined by the C standard." But as a state of | ||
| 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). | ||
|
|
||
| For example, the :external+py3.14:mod:`math` module documentation says: "Except | ||
| when explicitly noted otherwise, all return values are floats." This is not | ||
| 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 | ||
| can simplify description of accepted arguments for both the | ||
| :external+py3.14:mod:`math` and the new module. | ||
|
|
||
| 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 | ||
| 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. | ||
| The :external+py3.14:mod:`math` module documentation says: "This module provides | ||
| access to the mathematical functions defined by the C standard." But, over time | ||
| the module was populated with functions that aren't related to those | ||
| mathematical functions defined in the C standard. | ||
|
|
||
| Also, the :external+py3.14:mod:`math` module documentation says: "Except when | ||
| explicitly noted otherwise, all return values are floats." This is no 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 ``imath`` module, where they belong, a similar | ||
| catch-all: "Except when explicitly noted otherwise, all return values are | ||
| integers." could tell the truth once. | ||
|
|
||
| The ``imath`` module would parallel the :external+py3.14:mod:`math` module's | ||
| other siblings: :external+py3.14:mod:`cmath` and the | ||
| :external+py3.14:mod:`statistics`. 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. | ||
|
Comment on lines
+25
to
+43
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. First paragraph:
Second para:
Third para:
|
||
|
|
||
|
|
||
| Specification | ||
| ============= | ||
|
|
||
| The PEP proposes moving the following integer-related functions [1]_ in a new | ||
| The PEP proposes exposing the following integer-related functions [1]_ in a new | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, "moving" is a correct word. We move functions, period.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're not proposing moving the functions though. We're proposing leaving them in Are you talking about the implementations? |
||
| module, called ``imath``: | ||
|
|
||
| * :external+py3.14:func:`~math.comb` | ||
|
|
@@ -72,8 +70,20 @@ proposal. | |
| Backwards Compatibility | ||
| ======================= | ||
|
|
||
| As aliases in :external+py3.14:mod:`math` will be kept for indefinite time | ||
| (their use would be discouraged), there are no anticipated code breaks. | ||
| As aliases in :external+py3.14:mod:`math` will be kept indefinitely, there are | ||
| no anticipated code breaks. (The use of these aliases is discouraged.) | ||
|
|
||
| Rationale | ||
| ========= | ||
|
|
||
| Why not simply update the documentation to broaden math's scope | ||
| --------------------------------------------------------------- | ||
|
|
||
| Broadening ``math``'s scope would solve the documentation issue, but it would | ||
| neglect the motivation of providing a shared context. As mentioned, shared | ||
| context reduces verbosity in the documentation and conceptual load. It also | ||
| aids discoverability through grouping related functions and IDEs suggesting | ||
| helpful completions. | ||
|
Comment on lines
+75
to
+86
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For me it looks like you repeat stuff from the "Motivation" section. I hope, that with a small improvement of the former we can just remove this section.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I'm happy to remove the Rationale. I thought you wanted it. Should I remove it now? |
||
|
|
||
|
|
||
| Reference Implementation | ||
|
|
@@ -105,27 +115,83 @@ interval math or numerical integration. | |
| Other proposed names include ``ntheory`` (like SymPy's submodule), | ||
| ``integermath`` and ``imaths``. | ||
|
|
||
| Module scope and possible library members | ||
| ========================================= | ||
|
Comment on lines
+118
to
+119
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should be part of the "Open Issues" section.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's not an open issue because we don't want the SC deciding which library members will be added. This is just a way of illustrating scope. We're not looking to start a debate about the choices in this section. Open issues are for things that the SC needs to decide: Like the name of the library. |
||
|
|
||
| This PEP proposes a new module with only the six existing functions listed | ||
| above. However, in order to support the module's possible future utility, and | ||
| to illustrate its scope, we provide an exploratory list of functions that could | ||
| one day find a home in the ``imath`` library: | ||
|
Comment on lines
+121
to
+124
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, but you again miss my point. The purpose of this section is to show what can be included eventually to the module and what can't, not classification of the
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
But I'm not suggesting the point is classification? Let's go point-by-point: Do you agree that this section should:
Also, you want his section to show "what can be included eventually". What do you mean by this exactly? Is it one of the two things above, or something else? |
||
|
|
||
| * Number theory | ||
|
|
||
| * Divisibility & Modular Arithmetic (see also [5]_) | ||
|
|
||
| * ``gcd(a, b)``: Part of proposal. | ||
| * ``lcm(a, b)``: Part of proposal. | ||
| * ``powmod(a, b, m)``: Modular exponentiation (already in `pow(a, b, m)`, | ||
| but renamed for clarity). | ||
| * ``is_coprime(a, b)`` | ||
| * ``modinv(a, m)``: Modular inverse. | ||
| * ``gcdext(a, b)``: Solve linear Diophantine equations in two variables (the | ||
| :external+py3.14:class:`int` implementation actually includes the extended | ||
| Euclidean algorithm). | ||
|
|
||
| * Integer Arithmetic and Sequences | ||
|
|
||
| * ``isqrt(n)``: Part of proposal. | ||
| * ``c_div(a, b)``: Ceiling divide, see [2]_, [3]_. | ||
| * ``isqrt_rem(n)``: The integer square root and its remainder. | ||
| * ``sqrtmod(n, modulo)``: Square root within a modular group. | ||
| * ``ilog(n, base)``: Integer logarihm; currently | ||
| :external+py3.14:func:`math.log` has a special handling for integer | ||
| arguments. It's unique (wrt other module functions) and undocumented. | ||
| See [4]_. | ||
| * ``fibonacci(n)`` | ||
|
|
||
| * Primality & Factorization | ||
|
|
||
| * ``is_prime(n)`` | ||
| * ``is_power_of_two(x)`` | ||
| * ``prime_factors(n)``: Trial division or Pollard’s rho for small inputs. | ||
| * ``totient(n)``: Euler’s totient function. | ||
|
|
||
| * Combinatorics | ||
|
|
||
| * ``factorial(n)``: Part of proposal. | ||
| * ``comb(n, k)``: Part of proposal. | ||
| * ``perm(n, k)``: Part of proposal. | ||
|
|
||
|
|
||
| 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). | ||
|
|
||
|
|
||
|
skirpichev marked this conversation as resolved.
|
||
| Arguments against | ||
|
NeilGirdhar marked this conversation as resolved.
|
||
| ================= | ||
|
|
||
| Discoverability | ||
| --------------- | ||
|
|
||
| Module scope and possible extensions | ||
| ------------------------------------ | ||
| Beginners looking for a math function may instinctively look for it in the math | ||
| module. Rather than creating a new module, change the documentation for the | ||
| math module to read not mention the C standard or floating-point numbers. | ||
|
|
||
| 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 | ||
| testing and factorization. | ||
| Countering this, complex math is still math, but we have a separate module for | ||
| that. Just as with complex numbers, integers should have their own module too. | ||
|
|
||
| There are possible additions, among proposed in the initial discussion thread | ||
| (see also [5]_): | ||
| Limited benefit | ||
| --------------- | ||
|
Comment on lines
+189
to
+190
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see counter arguments here.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we add one? I thought that our counterargument is that it is useful enough for all the reasons we've already given. Or should we remove this section? Happy with whatever. |
||
|
|
||
| * ``c_div()`` --- for integer ceiling divide, see [2]_, [3]_. | ||
| * ``gcdext()`` --- to solve linear Diophantine equation in two variables (the | ||
| :external+py3.14:class:`int` implementation actually include extended | ||
| Euclidean algorithm) | ||
| * ``isqrt_rem()`` --- to return both integer square root and a remainder (if | ||
| integer isn't a perfect square) | ||
| * ``ilog()`` --- integer logarithm, currently :external+py3.14:func:`math.log` | ||
| has a special handling for integer arguments. It's unique (wrt other module | ||
| functions) and not documented so far, see [4]_ | ||
| * ``fibonacci()``. | ||
| Whereas the ``cmath`` module is justified by its prevention of a lot of slow | ||
| type checks, the ``imath`` module has limited benefit in this regard. The | ||
|
NeilGirdhar marked this conversation as resolved.
|
||
| example given: ``sin`` has a place in ``cmath``, but not ``imath``. | ||
|
|
||
|
|
||
| Rejected ideas | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you argue why this is better?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to Wikipedia:
Therefore, I think our desired scope should be "number theory and combinatorics".
I also think that abstracts should be as concise as possible with details presented in papers/proposals. So I prefer one sentence without examples.