Skip to content

gh-117999: fixed small nonnegative integer powers of complex numbers - #118000

Draft
skirpichev wants to merge 13 commits into
python:mainfrom
skirpichev:fix-c_powi-117999
Draft

gh-117999: fixed small nonnegative integer powers of complex numbers#118000
skirpichev wants to merge 13 commits into
python:mainfrom
skirpichev:fix-c_powi-117999

Conversation

@skirpichev

@skirpichevskirpichev commented Apr 17, 2024

Copy link
Copy Markdown
Member

Before, handling of numbers with special values in components (infinities, nans, signed zero) was invalid. Simple example:

 >>> z = complex(1, -0.0)
>>> z*z
(1-0j)
>>> z**2
(1+0j)

Now:

 >>> z**2
(1-0j)

@skirpichev
skirpichevforce-pushed the fix-c_powi-117999 branch 2 times, most recently from 2a6babf to 5ee43eeCompareApril 18, 2024 08:49
@skirpichevskirpichev changed the title gh-117999: fixed invalid small integer powers of real±0jgh-117999: fixed small nonnegative integer powers of complex numbers with special componentsApr 24, 2024
@skirpichev
skirpichevforce-pushed the fix-c_powi-117999 branch 2 times, most recently from 51c6cad to cd3e11eCompareMay 12, 2024 17:00
@skirpichevskirpichev changed the title gh-117999: fixed small nonnegative integer powers of complex numbers with special componentsgh-117999: fixed small nonnegative integer powers of complex numbersMay 12, 2024
…mbers
Before, handling of numbers with special values in components
(infinities, nans, signed zero) was invalid. Simple example:
>>> z = complex(1, -0.0)
>>> z*z
(1-0j)
>>> z**2
(1+0j)
Now:
>>> z**2
(1-0j)
@skirpichev

Copy link
Copy Markdown
MemberAuthor

@picnixz, I would appreciate your review on this pr. Or your opinion in the issue thread.

@picnixz

Copy link
Copy Markdown
Member

I'll do it tomorrow! (Monday, Paris time)

@picnixz
picnixz self-requested a review August 18, 2024 13:54
Comment threadLib/test/test_complex.py Outdated
Comment threadLib/test/test_complex.py Outdated
Comment threadLib/test/test_complex.py Outdated
Comment threadLib/test/test_complex.py Outdated
Comment threadLib/test/test_complex.py Outdated
Comment threadObjects/complexobject.c Outdated
Comment threadLib/test/test_complex.py Outdated
@picnixz

Copy link
Copy Markdown
Member

I need to think a bit more on the issue. I'll try to have something by the end of the day or tomorrow. Ideally, I would like to have no inconsistency between the generic algorithm and the non-generic one (namely, the result should be as if we were using the generic algorithm).

@skirpichev

skirpichev commented Aug 19, 2024

Copy link
Copy Markdown
MemberAuthor

I would like to have no inconsistency between the generic algorithm and the non-generic one

I'm not sure if it's possible without too much code, that affects performance severely. BTW, I think that numpy code has no such special version for integer exponents. I'll double check.

Edit: Ah, no. numpy mimics CPython here, at least in npy_math_complex.c.src.

Edit2:

JFR, some simple benchmarks.

With specialized code (main):

$ python -m pyperf timeit -q -s 'z=1+1j;e=20' 'pow(z,e)'
Mean +- std dev: 237 ns +- 5 ns
$ python -m pyperf timeit -q -s 'z=1+1j;e=90' 'pow(z,e)'
Mean +- std dev: 259 ns +- 4 ns
$ python -m pyperf timeit -q -s 'z=1+1j;e=110' 'pow(z,e)'
Mean +- std dev: 579 ns +- 5 ns
$ python -m pyperf timeit -q -s 'z=1+1j;e=200' 'pow(z,e)'
Mean +- std dev: 571 ns +- 3 ns

Without:

$ python -m pyperf timeit -q -s 'z=1+1j;e=2' 'pow(z,e)'
Mean +- std dev: 562 ns +- 3 ns
$ python -m pyperf timeit -q -s 'z=1+1j;e=20' 'pow(z,e)'
Mean +- std dev: 576 ns +- 2 ns
$ python -m pyperf timeit -q -s 'z=1+1j;e=90' 'pow(z,e)'
Mean +- std dev: 576 ns +- 3 ns
$ python -m pyperf timeit -q -s 'z=1+1j;e=110' 'pow(z,e)'
Mean +- std dev: 578 ns +- 4 ns
$ python -m pyperf timeit -q -s 'z=1+1j;e=200' 'pow(z,e)'
Mean +- std dev: 573 ns +- 3 ns

Comment threadLib/test/test_complex.py Outdated
Comment threadObjects/complexobject.c Outdated
Comment threadLib/test/test_complex.py Outdated
Comment threadLib/test/test_complex.py Outdated
skirpichevand others added 3 commits August 19, 2024 11:55
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
@skirpichev
skirpichev deleted the fix-c_powi-117999 branch August 20, 2024 03:29
@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actionsgithub-actionsBot added the stale Stale PR or inactive for long period of time. label Apr 16, 2026
@skirpichevskirpichev self-assigned this May 24, 2026
@github-actionsgithub-actionsBot removed the stale Stale PR or inactive for long period of time. label May 30, 2026
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@skirpichev@picnixz