spherical.recursions.complex_powers
complex_powers
complex_powers(z, M, zpowers=None)
Source: spherical/recursions/complex_powers.py
Compute integer powers of z=exp(iθ) recursively
Parameters
-
z:complexComplex number to take integer powers of. Note that this is assumed to be normalized, but can be an array of such numbers.
-
M:intHighest power to compute
-
zpowers:None (defaults to None),arrayofcomplexIf present, the output will be written into this array, which must have shape z.shape+(M+1,). Pre-allocating this argument can reduce the time spent in this function by up to 60%.
Returns
-
zpowers:arrayofcomplexPowers z⁰, z¹, ..., zᴹ. The last dimension has size M+1, any preceding dimensions are the same as the input
zarray.
Note
This algorithm is mostly due to Stoer and Bulirsch in "Introduction to Numerical Analysis" (page 24) — with a little help from de Moivre's formula, which is essentially exp(iθ)ⁿ = exp(inθ), as well as my own alterations to deal with different behaviors in different quadrants. There isn't usually a huge advantage to using this specialized function. If you just need a particular power, it will generally be far more efficient and just as accurate to compute either exp(iθ)ⁿ or exp(inθ) explicitly. However, if you need all powers from 0 to M, this function is about 10 or 5 times faster than those options, respectively. Like those options, this function is numerically stable, in the sense that its errors are usually smaller than the error from machine-precision errors in the input argument — or at worst about 30% larger, around π/2.