8000 PEP 791: imath --- module for integer-specific mathematics functions by skirpichev · Pull Request #4422 · python/peps · GitHub
[go: up one dir, main page]

Skip to content

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

Open
wants to merge 20 commits into
base: main
Choose a base branch
from

Conversation

skirpichev
Copy link
Member
@skirpichev skirpichev commented May 12, 2025

Basic requirements (all PEP Types)

  • Read and followed PEP 1 & PEP 12
  • File created from the latest PEP template
  • PEP has next available number, & set in filename (pep-NNNN.rst), PR title (PEP 123: <Title of PEP>) and PEP header
  • Title clearly, accurately and concisely describes the content in 79 characters or less
  • Core dev/PEP editor listed as Author or Sponsor, and formally confirmed their approval // @vstinner at https://discuss.python.org/t/91337/47
  • Author, Status (Draft), Type and Created headers filled out correctly
  • PEP-Delegate, Topic, Requires and Replaces headers completed if appropriate
  • Required sections included
    • Abstract (first section)
    • Copyright (last section; exact wording from template required)
  • Code is well-formatted (PEP 7/PEP 8) and is in code blocks, with the right lexer names if non-Python
  • PEP builds with no warnings, pre-commit checks pass and content displays as intended in the rendered HTML
  • Authors/sponsor added to .github/CODEOWNERS for the PEP

Standards Track requirements

  • PEP topic discussed in a suitable venue with general agreement that a PEP is appropriate
  • Suggested sections included (unless not applicable)
    • Motivation
    • Rationale
    • Specification
    • Backwards Compatibility
    • Security Implications
    • How to Teach This
    • Reference Implementation
    • Rejected Ideas
    • Open Issues
  • Python-Version set to valid (pre-beta) future Python version, if relevant
  • Any project stated in the PEP as supporting/endorsing/benefiting from the PEP formally confirmed such
  • Right before or after initial merging, PEP discussion thread created and linked to in Discussions-To and Post-History

📚 Documentation preview 📚: https://pep-previews--4422.org.readthedocs.build/pep-0791/

@skirpichev skirpichev added the new-pep A new draft PEP submitted for initial review label May 12, 2025
@skirpichev skirpichev marked this pull request as ready for review May 12, 2025 06:42
@skirpichev skirpichev requested a review from a team as a code owner May 12, 2025 06:42
@skirpichev skirpichev requested a review from vstinner May 12, 2025 06:42
skirpichev and others added 2 commits May 12, 2025 09:58
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
------------------------------------

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not exclude this. Primality testing was in the original proposition for imath, and I would need it more than perm() or isqrt().

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's in "Open Issues". I think we can make list more exhaustive later.

The idea is to put some rough boundary between possible content of the module and something, that not belongs to it. I'm with @pfmoore (from the 2018 thread) and think that primaility testing and factorization is outside of the stdlib scope. (Unless we will not support implementation.) Do you have a better candidate to replace that?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the scope is "number theory", then you are already saying that it's in scope. Why not just leave these questions to future PEPs and not worry about it here? Is imath going to be rejected because primality testing could be added?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm with @pfmoore (from the 2018 thread) and think that primaility testing and factorization is outside of the stdlib scope.

Actually, in that thread I supported isprime and factors. What I was against was an inefficient implementation. I don't think they necessarily have to be "best of breed" implementations (although I wouldn't object!) but I would want them to be good enough to be used in non-specialist code.

I quite often come up against problems where these functions would be useful. Not in production code (typically in "solve a puzzle in the REPL" types of situation), but certainly real life cases.

As I said elsewhere, though, I think we should keep the scope small for now. Adding functions later is fine, let's not risk the PEP failing over an argument about what's the "best" prime testing algorithm...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's close to what I meant; I did update for this section. I used "production" word, maybe it's not the best. But I think it fits "being used in real life cases" scenario, e.g. by non-professionals.

let's not risk the PEP failing over an argument about what's the "best" prime testing algorithm...

Do you think we shouldn't discuss possible extensions for the module and just restrict our attention to existing functions?

skirpichev and others added 2 commits May 12, 2025 11:27
Copy link
Member
@vstinner vstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to rename the module to "intmath" because there are already two "imath" projects, and it seems like "imath" name is more confusing than intmath: https://mail.python.org/archives/list/python-ideas@python.org/message/JV5HYIYIHAG6PQA2RXX6NCVTCNHM7IXY/

@skirpichev
Copy link
Member Author

I suggest to rename the module to "intmath" because there are already two "imath" projects

Ok, let's start with this.

@skirpichev

This comment was marked as resolved.

NeilGirdhar
< A373 /div>
NeilGirdhar

This comment was marked as outdated.

@NeilGirdhar
Copy link
NeilGirdhar commented May 12, 2025

Thanks for making the grammar changes! LGTM

@skirpichev
Copy link
Member Author

@NeilGirdhar, would you like to be added as co-author?

@NeilGirdhar
Copy link

@skirpichev If you feel my contributions have been valuable, then happily yes, but I'll leave it up to you. No hard feelings either way 😄

@mdickinson
Copy link
Member
mdickinson commented May 12, 2025

Minor observation from an (ex) number theorist: "number-theoretic" feels too specific here. E.g., I'd think of comb and perm as combinatoric rather than number-theoretic, and I'd worry that describing the module as for "for number-theoretic functions" would skew what gets added to it.

For me, the characterising properties of these functions are that (a) they typically accept and return integers, (b) they have zero dependence on the platform floating-point format or behaviour, and zero dependence on the platform math library ("libm"). So unlike most of the functions in math, they're completely deterministic and portable.

@skirpichev
Copy link
Member Author
skirpichev commented May 13, 2025

It can be helpful to note that most such functions also map to ints, but that's not their most fundamental property, or even essential.

@tim-one, the abstract highlight restrictions to both co-domain and domain:

This PEP proposes a new module for number-theoretical, combinatorial and other integer-valued functions defined for integer arguments, like math.gcd() or math.isqrt().

BTW, are you ok with title suggestion: "imath --- module for integer-specific mathematics functions"?

@skirpichev
Copy link
Member Author

How to Teach This

@hugovk, added.

Co-authored-by: Mikhail Efimov <efimov.mikhail@gmail.com>
@tim-one
Copy link
Member
tim-one commented May 13, 2025

the abstract highlight restrictions to both co-domain and domain:

And I'm saying it should not. The co-domain is irrelevant to the standard meaning of "number-theoretic function". That they're restricted to the domain of integers is the whole banana. I don't want us to box ourselves into a corner with needless restrictions. Why do it? It just so happens that most such functions in common use (and all those Python has implemented so far) return ints.

The von Mangoldt function, and the n'th harmonic number. were already given as examples of number-theoretic functions that don't return ints. I'm not saying we should add them, but am saying whatever is written now should not preclude the possibility.

skirpichev and others added 2 commits May 13, 2025 20:05
Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
@skirpichev
Copy link
Member Author

That they're restricted to the domain of integers is the whole banana. [...] The von Mangoldt function, and the n'th harmonic number. were already given as examples of number-theoretic functions that don't return ints.

Sure, but Mark suggested above something like a strict reproducibility ("deterministic and portable"). I doubt this is possible, if co-domain will be something more generic than integers/rationals or algebraic numbers (and already this case seems to be "too much for the stdlib"). I.e. that essentially excludes floating-point math.

whatever is written now should not preclude the possibility.

The pros of this seems rather hypothetical: can we have a good candidate for the sidlib right now? While cons is obvious: we can't say anymore that output should be platform-independent (and implementation-independent).

I think we can later relax this requirement in case of real need, i.e. specify in docs "All return values are integers, except when explicitly noted otherwise."

@skirpichev skirpichev changed the title PEP 791: imath --- module for number-theoretic functions PEP 791: imath --- module for integer-specific mathematics functions May 13, 2025
@tim-one
Copy link
Member
tim-one commented May 14, 2025

Well, can only say I'm baffled by why you want to handcuff future generations before the module even exists 😉. The harmonic numbers, for example, are rationals, and there are far more efficient ways to compute them exactly than via the naïve summation loop (much as there are far more efficient ways to compute factorial than naïve use of prod()). That mpmath.harmonic() returns a float doesn't mean that Python also has to.

I don't want to burn time arguing about specific functions now. I want the PEP & the docs not to create needless obstacles to whatever the future may bring.

@skirpichev
Copy link
Member Author

I want the PEP & the docs not to create needless obstacles to whatever the future may bring.

@tim-one, I worry that removing this obstacle will smash also much more useful constraint, noted by Mark. What do you think about such version of the abstract (small rewrite of this suggestion):

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.

Computations within the module expected to be exact, following
arithmetic rules for arbitrary-precision builtin
integers (:external+py3.14:class:int type).

?

@tim-one
Copy link
Member
tim-one commented May 14, 2025

I like that fine! But suggest dropping the ", following arithmetic rules ..." part. "Exact" already meant exact. and doesn't really benefit from elaboration. Maybe just:

Suitable functions can be computed exactly, given sufficient time and memory.

@skirpichev
Copy link
Member Author

But suggest dropping the ", following arithmetic rules ..." part.

Ok, done with a minor tone change (can -> must).

Copy link
@NeilGirdhar NeilGirdhar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a great improvement over the first draft! And I know it's exhausting when you're getting so many people pushing you.

vstinner and others added 3 commits May 14, 2025 17:05
Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
Co-authored-by: Neil Girdhar <mistersheik@gmail.com>
@vstinner
Copy link
Member

@skirpichev: So what do you think of renaming the module to intmath to avoid confusion and conflicts with existing projects?

@skirpichev
Copy link
Member Author

So what do you think of renaming the module to intmath to avoid confusion and conflicts with existing projects?

@vstinner, I think this should be postponed to final decision about naming.

I.e. we can do this right now if either

  1. the imath variant can be excluded,
  2. naming issue can be excluded from the "open issues" section.

Else this can introduce extra work for the implementation pr.

Copy link
Member
@vstinner vstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new-pep A new draft PEP submitted for initial review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

0