[Numpy-discussion] complex numbers
Tim Hochberg
tim.hochberg at ieee.org
Tue Oct 3 16:34:54 EDT 2006
Kenny Ortmann wrote:
> excuse my laziness for not looking this up, I googled it but could not find
> a solution.
> matlab has a
> isreal(array)
> where if the array is full of real numbers the value returned is 1.
> I'm translating matlab code and ran across
>
> if ~isreal(array)
> array = abs(array)
>
> Is there a way to check to see if a number is real or complex?
There may be a better way, but::
alltrue(isreal(x))
Would work. As would:
not sometrue(x.imag)
In the above test you are already negating the test, so you could just
drop the not.
> and if so is
> there a way to extract the(a + ib) because the absolute value of a complex
> number is like the pythagorean therom on a and b?
>
I'm not entirely sure what you are looking for here, but x.imag and
x.real will give you the real and imaginary parts. abs(x) will return
the magnitude of x whether x is real or complex. x.conj() will return
complex conjugate.
I'm somewhat suspicious of that matlab code. The code given is
discontinuous as you cross the negative real axis. Does the result
subsequently get squared or something? I'm guessing that either the
matlab code is doing extra work, or there are some hidden assumptions
(all values are in the positive real half-plane). Or some such. In
either case, you'd probably be OK just skipping the check for realness
and always taking the absolute of array. I can't say for sure without
more context though.
-tim
More information about the NumPy-Discussion
mailing list