You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Imaginary type and IEC 60559-compatible complex arithmetic
"Generally, mixed-mode arithmetic combining real and complex variables
should be performed directly, not by first coercing the real to complex,
lest the sign of zero be rendered uninformative; the same goes for
combinations of pure imaginary quantities with complex variables." (c)
Kahan, W: Branch cuts for complex elementary functions.
That's why C standards since C99 introduce imaginary types. This patch
implements similar extension to the Python language.
Lets consider (actually interrelated) problems, which will be solved on
this way.
1) Now complex arithmetic could be used for implementation of
mathematical functions without special "corner cases", with textbooks
formulae. Take the inverse tangent as an example:
>>> z = complex(-0.0, 2)
>>> cmath.atan(z)
(-1.5707963267948966+0.5493061443340549j)
>>> atan = lambda z: 1j*(cmath.log(1 - 1j*z) - cmath.log(1 + 1j*z))/2
>>> atan(z) # real part had wrong sign before
(-1.5707963267948966+0.5493061443340549j)
2) Previously, we have unsigned imaginary literals with the following
semantics:
a±bj = complex(a ± 0.0, ±b)
complex(a, ±b)*cj = complex(a*0.0 ∓ b*c, a*c ± b*0.0)
While this behaviour was well documented, most users would expect
instead here:
a±bj = complex(a, ±b)
complex(a, ±b)*cj = complex(∓b*c, a*c)
i.e. that it follows to the rectangular notation for complex numbers.
For example:
>>> -0.0+1j # was 1j
(-0.0+1j)
>>> float('inf')*1j # was (nan+infj)
infj
>>> -0.0+1j # was 1j
(-0.0+1j)
>>> complex(-0.0, 1) # was (-0+1j), not funny signed integer zero
(-0.0+1j)
3) The ``eval(repr(x)) == x`` invariant now holds for the complex type.
What's changed:
* Added a new subtype (imaginary) of the complex type with
few overloaded methods (conjugate() and __getnewargs__()).
* Complex and imaginary types implement IEC 60559-compatible complex
arithmetic (as specified by C11 Annex G).
* Imaginary literals now produce instances of imaginary type.
* cmath.infj/nanj were changed to be of imaginary type.
* Modules ast, code, copy, marshal got support for imaginary type.
* Few tests adapted to use complex, instead of imaginary literals
* Print dot for signed zeros in the real part of repr(complex)
* repr(complex) now prints real part even if it's zero.
0 commit comments