|
| 1 | +PEP: 9999 |
| 2 | +Title: Imaginary type and IEC 60559-compatible complex arithmetic |
| 3 | +Author: Sergey B Kirpichev <skirpichev@gmail.com> |
| 4 | +Sponsor: Pending |
| 5 | +Discussions-To: Pending |
| 6 | +Status: Draft |
| 7 | +Type: Standards Track |
| 8 | +Created: 05-Dec-2023 |
| 9 | +Python-Version: 3.15 |
| 10 | +Post-History: `10-Sep-2023 <https://discuss.python.org/t/33433>`__, |
| 11 | + `14-Jan-2025 <https://discuss.python.org/t/77073>`__ |
| 12 | + |
| 13 | + |
| 14 | +Abstract |
| 15 | +======== |
| 16 | + |
| 17 | +Introduce new imaginary type and mixed rules for complex/imaginary/real |
| 18 | +arithmetics. |
| 19 | + |
| 20 | + |
| 21 | +Motivation |
| 22 | +========== |
| 23 | + |
| 24 | +Too much special casing, rectangular notation, brain split wrt repr() & str |
| 25 | +input, fix eval-repr round-trip, fix simple invariants in docs. |
| 26 | + |
| 27 | +* C standard since C99 |
| 28 | +* GSL |
| 29 | +* MPC has special cases for real operand |
| 30 | + |
| 31 | +Examples: |
| 32 | + |
| 33 | +at z = ±2±0j |
| 34 | + |
| 35 | +* atan = lambda z: 1j*(cmath.log(1 - 1j*z) - cmath.log(1 + 1j*z))/2 |
| 36 | +* asin = lambda z: -1j*cmath.asinh(1j*z) |
| 37 | + |
| 38 | +at z = +2±0j |
| 39 | + |
| 40 | +* asin = lambda z: -1j*cmath.log(1j*z + cmath.sqrt(1 - z*z)) |
| 41 | + |
| 42 | +at z = -2±0j |
| 43 | + |
| 44 | +* asin = lambda z: 1j*cmath.log(cmath.sqrt(1 - z*z) - 1j*z) |
| 45 | + |
| 46 | + |
| 47 | +Not a replacement to limit() in CAS! |
| 48 | + |
| 49 | +.. code-block:: pycon |
| 50 | +
|
| 51 | + >>> log1p = lambda z: +z - z*z/2 # ln(1+z) up to O(z^3) |
| 52 | + >>> log1m = lambda z: -z - z*z/2 # ln(1-z) up to O(z^3) |
| 53 | + >>> log1p(0.25-0j) # wrong sign! |
| 54 | + (0.21875+0j) |
| 55 | + >>> log1p(0.25-1e-10j) |
| 56 | + (0.21875-7.5e-11j) |
| 57 | + >>> log1p(0.25-1e-30j) |
| 58 | + (0.21875-7.500000000000001e-31j) |
| 59 | + >>> log1p(0.25-1e-300j) |
| 60 | + (0.21875-7.5e-301j) |
| 61 | + >>> log1m(0.25-0j) # correct sign |
| 62 | + (-0.28125+0j) |
| 63 | + >>> log1m(0.25-1e-300j) |
| 64 | + (-0.28125+1.25e-300j) |
| 65 | +
|
| 66 | +
|
| 67 | +Rationale |
| 68 | +========= |
| 69 | + |
| 70 | +C17 Annex G |
| 71 | + |
| 72 | +Note that removing from C2y is irrelevant for us. |
| 73 | + |
| 74 | + |
| 75 | +Specification |
| 76 | +============= |
| 77 | + |
| 78 | ++----------+------------+--------------+--------------------+ |
| 79 | +| lhs\/rhs | u | iv | u + iv | |
| 80 | ++----------+------------+--------------+--------------------+ |
| 81 | +| x | x ± u | x ± iv | (x ± u) ± iv | |
| 82 | ++----------+------------+--------------+--------------------+ |
| 83 | +| iy | ±u + iy | i(y ± v) | ±u + i(y ± v) | |
| 84 | ++----------+------------+--------------+--------------------+ |
| 85 | +| x + iy | x ± u + iy | x + i(y ± v) | (x ± u) + i(y ± v) | |
| 86 | ++----------+------------+--------------+--------------------+ |
| 87 | + |
| 88 | ++----------+----------------+-----------------+----------------------------+ |
| 89 | +| lhs\/rhs | u | iv | u + iv | |
| 90 | ++----------+----------------+-----------------+----------------------------+ |
| 91 | +| x | x * u | i(x*v) | (x*u) + i(x*v) | |
| 92 | ++----------+----------------+-----------------+----------------------------+ |
| 93 | +| iy | i(y*u) | -y*v | (-y*v) + i(y*u) | |
| 94 | ++----------+----------------+-----------------+----------------------------+ |
| 95 | +| x + iy | (x*u) + i(y*u) | (-y*v) + i(x*v) | (x*u - y*v) + i(y*u + x*v) | |
| 96 | ++----------+----------------+-----------------+----------------------------+ |
| 97 | + |
| 98 | ++----------+----------------+-----------------+ |
| 99 | +| lhs\/rhs | u | iv | |
| 100 | ++----------+----------------+-----------------+ |
| 101 | +| x | x / u | i(-x/v) | |
| 102 | ++----------+----------------+-----------------+ |
| 103 | +| iy | i(y/u) | y/v | |
| 104 | ++----------+----------------+-----------------+ |
| 105 | +| x + iy | (x/u) + i(y/u) | (y/v) + i(-x/v) | |
| 106 | ++----------+----------------+-----------------+ |
| 107 | + |
| 108 | + |
| 109 | +Backwards Compatibility |
| 110 | +======================= |
| 111 | + |
| 112 | +[Describe potential impact and severity on pre-existing code.] |
| 113 | + |
| 114 | + |
| 115 | +How to Teach This |
| 116 | +================= |
| 117 | + |
| 118 | +[How to teach users, new and experienced, how to apply the PEP to their work.] |
| 119 | + |
| 120 | + |
| 121 | +Reference Implementation |
| 122 | +======================== |
| 123 | + |
| 124 | +https://github.com/skirpichev/cpython/pull/1 |
| 125 | + |
| 126 | + |
| 127 | +Rejected Ideas |
| 128 | +============== |
| 129 | + |
| 130 | +[Why certain ideas that were brought while discussing this PEP were not |
| 131 | +ultimately pursued.] |
| 132 | + |
| 133 | + |
| 134 | +Footnotes |
| 135 | +========= |
| 136 | + |
| 137 | +.. [1] https://www.gnu.org/software/gsl/doc/html/complex.html#complex-arithmetic-operators |
| 138 | +
|
| 139 | +.. [2] /home/sk/ToDo/n3215.pdf |
| 140 | +
|
| 141 | +.. [3] /home/sk/ToDo/n3274.pdf |
| 142 | +
|
| 143 | +.. [4] https://github.com/llvm/llvm-project/issues/60269 |
| 144 | +
|
| 145 | +
|
| 146 | +Copyright |
| 147 | +========= |
| 148 | + |
| 149 | +This document is placed in the public domain or under the |
| 150 | +CC0-1.0-Universal license, whichever is more permissive. |
0 commit comments