I just realized that doing
x.real*x.real+x.imag*x.imag
is three times faster than doing
abs(x)**2
where x is a numpy array of complex numbers. For code readability, I could define a function like
def abs2(x):
return x.real*x.real+x.imag*x.imag
which is still far faster than abs(x)**2, but it is at the cost of a function call. Is it possible to inline such a function, as I would do in C using macro or using inline keyword?
x*x.conj()
, by the way? – Unmask