How can I calculate only the imaginary part of a complex number?

asked 2019-12-14 05:02:05 +0200

andrewyg gravatar image

updated 2019-12-14 05:33:32 +0200

So I have the following function which the x will be the difference between two point:

def complexDis(x): return sqrt((x.real()^2)+((x.imag()*-i)^2))

But it seems like i can’t actually operate the .real or .imag number or at least I can’t print it out with N(conplexDis(b-a)) so im asking how can I achieve my goal?

edit retag flag offensive close merge delete

Comments

What goes wrong if you do n(complexDis(b-a).imag()), or the same with real()?

John Palmieri gravatar imageJohn Palmieri ( 2019-12-14 06:55:50 +0200 )edit

@John-Palmieri same both imag and real after squaring them they both doesn’t output real number

andrewyg gravatar imageandrewyg ( 2019-12-15 06:22:14 +0200 )edit

Hi andrewwyg

maybe there is something I'm missing, but i don't see where the problem is?

sqrt((2^2)+((3*-i)^2))= sqrt(-5) so it is not a real number ?

def complexDis(x):
    return sqrt(((x.real())^2)+(((imag_part(x))*-i)^2))
var('z')
z=2+3*I
show("complexDis(z) :\t",complexDis(z),"\t, \t z norm :\t", z.norm())
ortollj gravatar imageortollj ( 2019-12-15 11:39:41 +0200 )edit

I think we need more information: what are a and b, and what are you expecting for your output?

John Palmieri gravatar imageJohn Palmieri ( 2019-12-15 18:22:13 +0200 )edit

Note that x.imag() is a real number, so (x.imag()*-i) is purely imaginary. Its square will be negative, so your function may certainly end up computing the square root of a negative number.

John Palmieri gravatar imageJohn Palmieri ( 2019-12-15 18:24:11 +0200 )edit