[Tutor] imaginary number class

alan.gauld@bt.com alan.gauld@bt.com
Tue, 25 Jun 2002 17:34:35 +0100


> writing the code itself, but don't really have a comprehensive 
> understanding of imaginary numbers. I'm at a bit of a loss as 
> to how to perform common operations on them.

One thing to do is play with Python complex numbers

>>> a = 3+4j
>>> b = 5+6j
>>> a+b
8+10j

pretty straightforward add the components...
>>> a-b
-2-2j
yep just subtract the components...

>>> a*b
-9+38j

Hmmm, bit strange.
Think of the numbers like algebraic expressions:

(3+4j)(5+6j) = 3*5+4j*5+3*6j+4j*6j

And recalling that j*j = -1:

= 15 + 20j + 18j + -1(24)

= 15-24 + 38j

= -9+38j

voila

>>> a/b
0.6393...+0.0327...j

Uh oh. This is a bit harder.

Here we have to multiply top and bottom by the 
inverse of the denomoninator: (which mathematically 
is like multiplying by one!)

(a+bj)/(c+dj) = (a+bj)(c-dj)/(c+dj)(c-dj)

The reason is that the inverse multiplier turns 
the bottom line into a real number:

(c+dj)(c-dj) = c*c + c*dj - c*dj - dj*dj

= c*c - -1(d*d) = c*c + d*d  = X which is real.

So now we get:
(a+bj)(c-dj)/X

So do the multiplication then divide each term by X.

(3+4j)(5-j6)/(25+36) = (15 + 20j - 18j - -1(24))/61

= 39/61 + 2j/61 = 0.6393...+0.0327...j

Conversion between complex and polar is done by thinking 
of the real axis as horizontal, the imaginary one as vertical 
and using trigonometry... Magnitude is determined by pythagorus.

HTH

Alan G.