python / python/cpython

Improve accuracy for complex powers with small negative integer exponents

オープン
#156,695 コメント 1 件 リアクション 1 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

interpreter-core type-feature
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

Feature or enhancement

Proposal:

Currently we compute such powers as (1+0j)/z**(-n) (unless absolute value of n is too big to use specialized algorithm for integer exponents). This approach, however, introduce huge accuracy loss due to underflows in division.

For example:

>>> from gmpy2 import *                                                                                                                    
>>> import math                                                                                                                            
>>> x, y = map(float.fromhex, ['0x1.47e9c711723f5p+81',                                                                                    
...                            '0x1.38afd1168e49fp+85'])
>>> z = complex(x, y)                                                                                                                      
>>> pr = pow(z, -12); pr                                                                                                                   
0j
>>> pr2 = pow((1/z), 12); pr2                                                                                                              
(5.562684646267994e-309+5.56268464626799e-309j)
>>> gr = complex(pow(mpc(z), -12))                                                                                                         
>>> abs((pr2-gr).real)/math.ulp(gr.real)                                                                                                   
2.0
>>> abs((pr2-gr).imag)/math.ulp(gr.imag)                                                                                                   
2.0

Using instead ((1+0j)/z)**(-n) reduced error in this example from ~1e15 ULP to 2ULP. Note that, generic power algorithm is not affected by this issue:

>>> import _testcapi
>>> pr3 = _testcapi._py_c_pow(z, -12)[0]; pr3
(5.56268464626801e-309+5.56268464626799e-309j)
>>> abs((pr3-gr).real)/math.ulp(gr.real)
1.0
>>> abs((pr3-gr).imag)/math.ulp(gr.imag)
2.0
Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs
  • gh-156757
  • gh-156968

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、pow(z, -12) と _testcapi._py_c_pow で実行される複素べき乗の経路から始め、pow((1/z), 12) および reproducer に示されている mpc の結果と比較してください。完了の条件は、例における underflow に起因する精度低下を回避することです。先に進む前に、関連する PR gh-156757 と gh-156968 を確認してください。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
backend
issue の種類
機能追加
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。