How to multiply two complex numbers in python ?

How to multiply two complex numbers in python ?

Daidalos April 15, 2020


Example of how to multiply two complex numbers in python

Create two complex numbers in python

Let's import the module python cmath that is used to work with complex numbers

>>> import cmath

Create a first complex number z1:

>>> z1 = 1.0 + 2.0j
>>> z1
(1+2j)

of real part

>>> z1.real
1.0

and imaginary part

 >>> z1.imag
2.0

Let's also create another complex number z2:

>>> z2 = 3.0 + 5.0j
>>> z2 
(3+5j)

Multiply the two complex numbers

To multiply z1 by z2, a solution is to use the operator *, example:

>>> z3 = z1 * z2
>>> z3
(-7+11j)

Use the polar representation

Transform z1 in polar representation:

>>> r1,theta1 = cmath.polar(z1)
>>> r1,theta1
(2.23606797749979, 1.1071487177940904)

Transform z2 in polar representation:

>>> r2,theta2 = cmath.polar(z2)
>>> r2,theta2
(5.830951894845301, 1.0303768265243125)

Then, multiply z1 by z2 using:

>>> r3 = r1 * r2
>>> theta3 = theta1 + theta2
>>> r3,theta3
(13.038404810405298, 2.137525544318403)

Going back to cartesian representation:

>>> cmath.rect(r3,theta3)
(-6.999999999999999+11.000000000000002j)

References

Links Site
Comment calculer les coordonnées polaires d'un nombre complexe en python ? moonbooks.org
Représentation géométrique d'un nombre complexe bibmath.net
cmath — Mathematical functions for complex numbers python doc