Python have five built-in Data Types – Numbers, Sequence, Dictionaries, Boolean and Sets. Here in this article, I will be talking about Numbers Data Type subcategory – Complex Numbers Type, you can see information about other Number Types here – Python Data Type Numbers.
Mathematically Complex Numbers are those which have one real and other imaginary part and can in general be written as a + bj where j is iota (iota equals to √-1). Like 3 + 1j is a complex number.
As Complex Numbers have an imaginary part that’s why these need to be treated differently as compared to simple numbers like Integers/Floats. That’s why Python’s Core Developers have built-in two modules of mathematical functions one for Reals/Integers/Floats(Math Python Module) and other one for Complex Numbers(cMath Python Module).
How to declare complex number in Python?
Complex number in Python can be declared by direct assignment method or using complex() function. For direct assignment just do x = a + bj or do x = complex(a, b) both ways will create a complex number x where a is Real Part, b is Imaginary part and j is iota.
Below are some code examples showing methods for declaring Complex Numbers in Python.
x = 3 + 28j # A complex number with 3, 28 as Real and Imaginary parts respectively y = complex(3, 28) # A complex number with 3, 28 as Real and Imaginary parts respectively
What is complex() function in Python?
complex() is a Python’s Built-in function which returns a Complex Number when passed in two real numbers. For example – complex(18, 2) will return 18 + 2j Complex Number Object.
How to add or subtract complex numbers?
For adding/subtracting complex numbers Python will add/subtract reals, imaginary parts and then will make a new Complex number with resulted real, imaginary parts.
For example – If you have two complex numbers x = 28 + 29j and y = 27 + 12j. Then in order to add these together x + y. Python will take real, imaginary parts of x, y and add those together respectively and then make a new Complex number with resulted parts. So x + y = (28+27) + (29+12)j results in x + y = 55 + 42j.
Similar to this way, Python deals with Subtraction as well. Like if we have x = 27 + 29j and y = 22 + 12j and we want to do x – y. Then Python will take real, imaginary parts of x, y respectively subtract these and then make a new real number with resulted parts. So x – y = (27-22) + (29 -12)j will results in x – y = 5 + 17j.
Below are some Python Code examples showing How to add/subtract complex numbers?
x = 37 + 8j y = 19 + 12j x + y // Returns 56 + 20j x - y // Returns 16 - 4j
How to multiply complex numbers in Python?
Well multiplying two complex numbers is a bit tricky as compared to multiplication of just two real numbers(like 28 X 10 = 280, this is so simple). But as Complex numbers have imaginary parts doing multiplication is a bit different. Let me explain through an example, like if we have x = 3 + 2j and y = 5 + 8j and we want to do xy(Multiply two complex numbers).
This in terms of basic mathematics can be done as xy = (3 + 2j)(5 + 8j) = (3*5 + 3 *8j + 2j*5 + 2j*8j) = (15 + 24j + 10j + 18j2) = (15 + 24j + 10j – 18) = (-3 + 34j).
So xy = -3 + 34j
j2 is -1, read this – Powers of iota for knowing more about why j2 is -1.
But if your writing Python and want to multiply two or more complex numbers, then you don’t need to worry about underlining stuff. Just doing x*y where both x, y are complex numbers will return result of multiplication of these complex numbers.
For multiplying two complex numbers x,y do not write xy this will raise NameError: name ‘xy’ is not defined.
Rather write x * y this will multiply two complex numbers and return resulting complex numer.
Below is a code example showing How to multiply two complex numbers in Python?
x = 8 + 28j y = 28 + 6j x * y // Returns 56 + 832j
How to divide complex numbers in Python?
Complex numbers in Python can be divided using slash(\). Like if you have two complex numbers x, y then x/y will return division of these two. In mathematics, its kind of tricky to divide two complex numbers and in order to keep things clear and focus more about Python itself, I’m not showing you How two complex numbers can be divided in simple mathematics? as I did above for multiplication of complex numbers.
Below are some code examples showing How to divide two complex numbers in Python?
x = 2 + 5j y = 3 + 7j x/y // Returns (1 + 0j)
How to find modulus of Complex Numbers in Python?
For finding modulus of a complex numbers in Python, there exist three approaches – using sqrt(), abs() or polar() function. I’ve put together a complete article explaining each of these method for finding Complex Number Modulus in Python. You can see that article here – Complex Number Modulus Calculating in Python?
These are some simple Mathematical Operations which can be performed upon Complex Numbers in Python Programming Language. Just one personal advice from me While dealing with Complex Numbers keep in mind that these are not Real Numbers.
Below are some links pointing to articles about Complex Numbers containing information about specific operations on Complex Numbers in Python.