Python Complex Number Arithmetic

by Shelley Walsh ©2001

Python has complex number operations built into it, so you can use it to check your homework or get some extra practice if you want.

Python does square roots by raising to the 1/2 power, but if you want something that looks more familiar, you can simply define sqrt as a function.

def sqrt(x):
... return x**.5
...
>>> sqrt(4)
2.0

I did this, and then since I had heard that it will work with complex numbers I tried this.

>>> sqrt(-4)

But it didn't work.

Traceback (most recent call last):
File "<input>", line 1, in ?
File "<input>", line 2, in sqrt
ValueError: negative number cannot be raised to a fractional power

It turns out you can take square roots of negative numbers, but you have to let it know that you want to work with complex number. To do that you write -4 as -4+0j.

>>> sqrt(-4+0j)
2j

This brings us to another difference that you have to watch out for, which is that it using j instead of i like electronics people do.

I tried a few other things with it to see what would happen.

>>> 1j*2
2j
>>> j*2
Traceback (most recent call last):
File "<input>", line 1, in ?
NameError: name 'j' is not defined

You don't have to put in a 0 for the real part if it is 0, but you do need a coefficient on the j. I won't recognize j alone.

Other than that the operations pretty much work like normal.

>>> (4+3j)+(2+6j)
(6+9j)
>>> (3+2j)*(7+1j)
(19+17j)
>>> 2j*1j
(-2+0j)
>>> (3+2j)/(3+1j)
(1.0999999999999999+0.29999999999999999j)
>>> 1j**17
1j
>>> 1j**22
(-1+0j)
>>> 1j**1234
(-1+9.5923269327613525e-14j)

Some round off error here, this should be -1, but notice that the imaginary part here is in scientific notation and represents a very small number, something times 10-14.
>>>
>>> sqrt(-49+0j)+3
(3+7j)
>>> -sqrt(-100+0j)
-10j
>>> 7-(3-2j)
(4+2j)
>>> (-3-5j)*(-3+5j)
(34+0j)
>>> abs(3+4j)
5.0

Application

Complex numbers have an interesting history. There were originally merely of interest to pure mathematicians, but later many applications were found for them. Most of them are quite complicated, but one that isn't too complicated is that you can use them to rotate coordinates. Suppose you have a point (x,y) in the plane and you want to find the point you get when you rotate by a given angle. You can do this by multiplying x+yi by a complex number whose angle with the positive x axis is that angle.

To find the point one unit from the origin making a given angle with the x axis you need a little trigonometry. It turns out that the point is (cos(angle), sin(angle)). If you don't know any trigonometry, you can just take my word for that. Since I can't yet find the mathematical functions modules for my Python I created my own trigonometry module using a few terms of the sine series and a little fiddling.To use my rotate module you need my trig module. You can just copy it and paste it into a module and name it trig.py.

facts=[1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800, 479001600]
pi=3.1415926535897932384
def sin(x):
if 0<=x<=(pi/2):
y=0
for k in range(6):
m=2*k+1
y=y+(-1)**k*x**m/facts[m]
return y
elif x>=0:
x=((x/(2*pi))%1)*2*pi
if x<=pi:
return sin(pi-x)
else:
return -sin(x-pi)
else:
return -sin(-x)

def cos(x):
return sin(pi/2-x)

def tan(x):
return sin(x)/cos(x)

def cot(x):
return cos(x)/sin(x)

def sec(x):
return 1/cos(x)
def csc(x):
return 1/sin(x)

def rad(x):
return x*pi/180

In order to use this module in the IDE all you need to do is type

from trig import *

Do that if you want to be able to evaluate any of the six trig functions, but you don't have to do that if you import the rotate module, since it will do it for you.

To do the rotating, just copy and paste this into a module and name it rotate.py. You must have the trig module in the same directory or you will get an error when you try to import the rotate module, because it imports the trig module.

from trig import *
def rotate(P,angle):
theta=rad(angle)
a=cos(theta)
b=sin(theta)
x=P[0]
y=P[1]
z=(x+y*1j)*(a+b*1j)
return z.real,z.imag

Then you can type

from rotate import *

in the IDE and do things like this.

>>> rotate((1,1),90)
(-0.99999994374105117, 0.99999994374105072)
>>> rotate((1,0),90)
(-2.2204460492503131e-16, 0.99999994374105095)
>>> rotate((1,1),45)
(-2.2204460492503131e-16, 1.4142135623593119)
>>> rotate((3,4),-53)
(4.9999871094116557, 0.011353562638369397)
>>> rotate((5,0),37)
(3.9931775499463029, 3.0090751157576894)
>>>

Note: You will probably be able to make more sense out of these numbers if you round to the nearest thousandth or so.

Links

Python Home Page

Mathematics Articles
Complex Number Arithmetic

Python Articles

Home Page
MathHelp
Math 107

Questions and Comments