# Complex Numbers in Python?

PythonServer Side ProgrammingProgramming

A complex number is created from real numbers. Python complex number can be created either using direct assignment statement or by using complex () function.

Complex numbers which are mostly used where we are using two real numbers. For instance, an electric circuit which is defined by voltage(V) and current(C) are used in geometry, scientific calculations and calculus.

## Syntax

complex([real[, imag]])

## Creating a simple complex number in python

>>> c = 3 +6j
>>> print(type(c))
<class 'complex'>
>>> print(c)
(3+6j)
>>>
>>> c1 = complex(3,6)
>>> print(type(c1))
<class 'complex'>
>>> print(c1)
(3+6j)

From above results, we can see python complex numbers are of type complex. Each complex number consist of one real part and one imaginary part.

### Python Complex Numbers- Attributes and Functions

>>> #Complex Number:
>>> c = (3 + 6j)
>>>
>>> #Real Part of complex number
>>> print('Complex Number: Real Part is = ', c. real)
Complex Number: Real Part is = 3.0
>>>
>>> #Imaginary Part of complex number
>>> print('Complex Number: Imaginary Part is = ', c. imag)
Complex Number: Imaginary Part is = 6.0
>>>
>>> #Conjugate of complex number
>>> print('Complex Number: conjugate Part = ', c. conjugate())
Complex Number: conjugate Part = (3-6j)

## Mathematical Calculations on Complex numbers

We can do simple mathematical calculations on complex numbers:

>>> #first complex number
>>> c1 = 3 + 6j
>>> #Second complex number
>>> c2 = 6 + 15j
>>>
>>> print("Addition of two complex number =", c1 + c2)
Addition of two complex number = (9+21j)
>>>
>>> #Subtraction
>>> print("Subtraction of two complex number =", c1 - c2)
Subtraction of two complex number = (-3-9j)
>>>
>>> #Multiplication
>>> print("Multiplication of two complex number =", c1 * c2)
Multiplication of two complex number = (-72+81j)
>>>
>>> #Division
>>> print("Division of two complex number =", c1 / c2)
Division of two complex number = (0.4137931034482759-0.03448275862068964j)

However, complex numbers don’t support comparison operators like <, >, <=, => and it will through TypeError message:

>>> c2 <= c2
Traceback (most recent call last):
File "<pyshell#40>", line 1, in <module>
c2 <= c2
TypeError: '<=' not supported between instances of 'complex' and 'complex'

## Python cmath module

Python cmath module provide access to mathematical functions for complex numbers. Let’s looks at some of the important features of complex numbers using math module function.

## Phase of complex number

The phase of a complex number is the angle between the real axis and the vector representing the imaginary part.

The phase returned by math and cmath modules are in radians and we use the numpy.degrees() function to convert it to degrees.

import cmath, math, numpy
c = 4+ 4j
# phase
phase = cmath.phase(c)
print('4+ 4j Phase =', phase)
print('Phase in Degrees =', numpy.degrees(phase))
print('-4-4j Phase =', cmath.phase(-4-4j), 'radians. Degrees =', numpy.degrees(cmath.phase(-4-4j)))
# we can get phase using math.atan2() function too
print('Complex number phase using math.atan2() =', math.atan2(2, 1))

## Result

4+ 4j Phase = 0.7853981633974483
Phase in Degrees = 45.0
-4-4j Phase = -2.356194490192345 radians. Degrees = -135.0
Complex number phase using math.atan2() = 1.1071487177940904

### cmath module constants

There are a couple of constans available in cmath module that are used in the complex number calculations:

import cmath
print('π =', cmath.pi)
print('e =', cmath.e)
print('tau =', cmath.tau)
print('Positive infinity =', cmath.inf)
print('Positive Complex infinity =', cmath.infj)
print('NaN =', cmath.nan)
print('NaN Complex =', cmath.nanj)

## Result

π = 3.141592653589793
e = 2.718281828459045
tau = 6.283185307179586
Positive infinity = inf
Positive Complex infinity = infj
NaN = nan
NaN Complex = nanj

### Power and Log Functions

The cmath() module provides some useful functions for logarithmic and power operations:

import cmath
c = 1 + 2j
print('e^c =', cmath.exp(c))
print('log2(c) =', cmath.log(c, 2))
print('log10(c) =', cmath.log10(c))
print('sqrt(c) =', cmath.sqrt(c))

## Result

e^c = (-1.1312043837568135+2.4717266720048188j)
log2(c) = (1.1609640474436813+1.5972779646881088j)
log10(c) = (0.3494850021680094+0.480828578784234j)
sqrt(c) = (1.272019649514069+0.7861513777574233j)

### Trigonometric Functions

import cmath
c = 2 + 4j
print('arc sine value:\n ', cmath.asin(c))
print('arc cosine value :\n', cmath.acos(c))
print('arc tangent value of complex number c :\n', cmath.atan(c))
print('sine value:\n', cmath.sin(c))
print('cosine value:\n', cmath.cos(c))
print('tangent value:\n', cmath.tan(c))

## Result

arc sine value:
(0.4538702099631225+2.198573027920936j)
arc cosine value :
(1.1169261168317741-2.198573027920936j)
arc tangent value of complex number c :
(1.4670482135772953+0.20058661813123432j)
sine value:
(24.83130584894638-11.356612711218174j)
cosine value:
(-11.36423470640106-24.814651485634187j)
tangent value:
(-0.0005079806234700387+1.0004385132020523j)

### Hyperbolic Functions

import cmath
c = 2 + 4j
print('Inverse hyperbolic sine value: \n', cmath.asinh(c))
print('Inverse hyperbolic cosine value: \n', cmath.acosh(c))
print('Inverse hyperbolic tangent value: \n', cmath.atanh(c))
print('Hyperbolic sine value: \n', cmath.sinh(c))
print('Hyperbolic cosine value: \n', cmath.cosh(c))
print('Hyperbolic tangent value: \n', cmath.tanh(c))

## Result

Inverse hyperbolic sine value:
(2.183585216564564+1.096921548830143j)
Inverse hyperbolic cosine value:
(2.198573027920936+1.1169261168317741j)
Inverse hyperbolic tangent value:
(0.09641562020299617+1.3715351039616865j)
Hyperbolic sine value:
(-2.370674169352002-2.8472390868488278j)
Hyperbolic cosine value:
(-2.4591352139173837-2.744817006792154j)
Hyperbolic tangent value:
(1.0046823121902348+0.03642336924740368j)