I'm trying to make a matplotlib plot of the function logi(x) (an i-based log). Here is my code:
from pylab import *
import cmath
x = linspace(-8,8,500)
z = cmath.log(x,1j)
plot(x,real(z),label='real')
plot(x,imag(z),label='imag')
legend()
show()
The problem is that it throws TypeError: only length-1 arrays can be converted to Python scalars.
I've searched a bit and found answers such as TypeError: only length-1 arrays can be converted to Python scalars while trying to exponentially fit data :
Non-numpy functions like math.abs() or math.log10() don't play nicely with numpy arrays.
And some advice that says to use Numpy's log() function instead. Unfortunately Numpy does not support i-based logarithms so I'm forced to use Cmath. How can I fix it?
If the array has complex dtype, then
np.logwill return complex results:At -8:
We can shift the base with the standard:
Compare that with
cmathresult:So either iterate and call
cmath.logfor each element of the array, or use this complex array and base shift.