Complex Numbers in Python

Complex Numbers in Python

Not only real numbers, Python can also handle complex numbers and its associated functions using the file “cmath”. Complex numbers have their uses in many applications related to mathematics and python provides useful tools to handle and manipulate them.

Converting real numbers to complex number

An complex number is represented by “ x + yi “. Python converts the real numbers x and y into complex using the function complex(x,y). The real part can be accessed using the function real() and imaginary part can be represented by imag().

 `# Python code to demonstrate the working of` `# complex(), real() and imag()` ` ` `# importing "cmath" for complex number operations` `import` `cmath` ` ` `# Initializing real numbers` `x ``=` `5` `y ``=` `3` ` ` `# converting x and y into complex number` `z ``=` `complex``(x,y);` ` ` `# printing real and imaginary part of complex number` `print` `(``"The real part of complex number is : "``,end``=``"")` `print` `(z.real)` ` ` `print` `(``"The imaginary part of complex number is : "``,end``=``"")` `print` `(z.imag)`

Output:

```The real part of complex number is : 5.0
The imaginary part of complex number is : 3.0
```

Phase of complex number

Geometrically, the phase of a complex number is the angle between the positive real axis and the vector representing complex number. This is also known as argument of complex number. Phase is returned using phase(), which takes complex number as argument. The range of phase lies from -pi to +pi. i.e from -3.14 to +3.14.

 `# Python code to demonstrate the working of` `# phase()` ` ` `# importing "cmath" for complex number operations` `import` `cmath` ` ` `# Initializing real numbers` `x ``=` `-``1.0` `y ``=` `0.0` ` ` `# converting x and y into complex number` `z ``=` `complex``(x,y);` ` ` `# printing phase of a complex number using phase()` `print` `(``"The phase of complex number is : "``,end``=``"")` `print` `(cmath.phase(z))`

Output:

```The phase of complex number is : 3.141592653589793
```

Converting from polar to rectangular form and vice versa

Conversion to polar is done using polar(), which returns a pair(r,ph) denoting the modulus r and phase angle ph. modulus can be displayed using abs() and phase using phase().
A complex number converts into rectangular coordinates by using rect(r, ph), where r is modulus and ph is phase angle. It returns a value numerically equal to r * (math.cos(ph) + math.sin(ph)*1j)

 `# Python code to demonstrate the working of` `# polar() and rect()` ` ` `# importing "cmath" for complex number operations` `import` `cmath` `import` `math` ` ` `# Initializing real numbers` `x ``=` `1.0` `y ``=` `1.0` ` ` `# converting x and y into complex number` `z ``=` `complex``(x,y);` ` ` `# converting complex number into polar using polar()` `w ``=` `cmath.polar(z)` ` ` `# printing modulus and argument of polar complex number` `print` `(``"The modulus and argument of polar complex number is : "``,end``=``"")` `print` `(w)` ` ` `# converting complex number into rectangular using rect()` `w ``=` `cmath.rect(``1.4142135623730951``, ``0.7853981633974483``)` ` ` `# printing rectangular form of complex number` `print` `(``"The rectangular form of complex number is : "``,end``=``"")` `print` `(w)`

Output:

```The modulus and argument of polar complex number is : (1.4142135623730951, 0.7853981633974483)
The rectangular form of complex number is : (1.0000000000000002+1j)
```

Some more important functions and constants are discussed in this article.
Operations on complex numbers :

1. exp() :- This function returns the exponent of the complex number mentioned in its argument.

2. log(x,b) :- This function returns the logarithmic value of x with the base b, both mentioned in its arguments. If base is not specified, natural log of x is returned.

 `# Python code to demonstrate the working of ` `# exp(), log()` ` ` `# importing "cmath" for complex number operations` `import` `cmath` `import` `math` ` ` `# Initializing real numbers` `x ``=` `1.0` `y ``=` `1.0` ` ` `# converting x and y into complex number` `z ``=` `complex``(x, y);` ` ` `# printing exponent of complex number` `print` `(``"The exponent of complex number is : "``, end``=``"")` `print` `(cmath.exp(z))` ` ` `# printing log form of complex number` `print` `(``"The log(base 10) of complex number is : "``, end``=``"")` `print` `(cmath.log(z,``10``))`

Output:

```The exponent of complex number is : (1.4686939399158851+2.2873552871788423j)
The log(base 10) of complex number is : (0.15051499783199057+0.3410940884604603j)
```

3. log10() :- This function returns the log base 10 of a complex number.

4. sqrt() :- This computes the square root of a complex number.

 `# Python code to demonstrate the working of ` `# log10(), sqrt()` `# importing "cmath" for complex number operations` `import` `cmath` `import` `math` ` ` `# Initializing real numbers` `x ``=` `1.0` `y ``=` `1.0` ` ` `# converting x and y into complex number` `z ``=` `complex``(x, y);` ` ` `# printing log10 of complex number` `print` `(``"The log10 of complex number is : "``, end``=``"")` `print` `(cmath.log10(z))` ` ` `# printing square root form of complex number` `print` `(``"The square root of complex number is : "``, end``=``"")` `print` `(cmath.sqrt(z))`

Output:

```The log10 of complex number is : (0.15051499783199057+0.3410940884604603j)
The square root of complex number is : (1.09868411346781+0.45508986056222733j)
```

5. isfinite() :- Returns true if both real and imaginary part of complex number are finite, else returns false.

6. isinf() :- Returns true if either real or imaginary part of complex number is/are infinite, else returns false.

7. isnan() :- Returns true if either real or imaginary part of complex number is NaN , else returns false.

 `# Python code to demonstrate the working of ` `# isnan(), isinf(), isfinite()` `  ` `# importing "cmath" for complex number operations` `import` `cmath` `import` `math` `  ` `# Initializing real numbers` `x ``=` `1.0` `y ``=` `1.0` `a ``=` `math.inf` `b ``=` `math.nan` `  ` `# converting x and y into complex number` `z ``=` `complex``(x,y);` `  ` `# converting x and a into complex number` `w ``=` `complex``(x,a);` `  ` `# converting x and b into complex number` `v ``=` `complex``(x,b);` `  ` `# checking if both numbers are finite` `if` `cmath.isfinite(z):` `       ``print` `(``"Complex number is finite"``)` `else` `: ``print` `(``"Complex number is infinite"``)   ` `      ` `# checking if either number is/are infinite` `if` `cmath.isinf(w):` `       ``print` `(``"Complex number is infinite"``)` `else` `: ``print` `(``"Complex number is finite"``)   ` `  ` `# checking if either number is/are infinite` `if` `cmath.isnan(v):` `       ``print` `(``"Complex number is NaN"``)` `else` `: ``print` `(``"Complex number is not NaN"``) `

Output:

```Complex number is finite
Complex number is infinite
Complex number is NaN
```

Constants

There are two constants defined in cmath module, “pi”, which returns the numerical value of pi. The second one is “e” which returns the numerical value of exponent.

 `# Python code to demonstrate the working of ` `# pi and e ` ` ` `# importing "cmath" for complex number operations` `import` `cmath` `import` `math` ` ` `# printing the value of pi ` `print` `(``"The value of pi is : "``, end``=``"")` `print` `(cmath.pi)` ` ` `# printing the value of e` `print` `(``"The value of exponent is : "``, end``=``"")` `print` `(cmath.e)`

Output:

```The value of pi is : 3.141592653589793
The value of exponent is : 2.718281828459045```

Last Updated on March 1, 2022 by admin

Python – Creating a button in tkinterPython – Creating a button in tkinter

Python | Creating a button in tkinter Tkinter is Python’s standard GUI (Graphical User Interface)

Python sep parameter in print()Python sep parameter in print()

Python | sep parameter in print() The separator between the arguments to print() function in

How to print an entire Pandas DataFrame in Python?How to print an entire Pandas DataFrame in Python?

Pandas is a widely-used library in Python for data manipulation and analysis. It provides a

Plotting Histogram in Python using MatplotlibPlotting Histogram in Python using Matplotlib

Plotting Histogram in Python using Matplotlib A histogram is basically used to represent data provided

Pattern matching in Python with RegexPattern matching in Python with Regex

Pattern matching in Python with Regex You may be familiar with searching for text by

Python – Multiply all numbers in the list (All different ways)Python – Multiply all numbers in the list (All different ways)

Python – Multiply all numbers in the list   Given a list, print the value

Substitution CipherSubstitution Cipher

Substitution Cipher Hiding some data is known as encryption. When plain text is encrypted it

dir() function in Pythondir() function in Python

dir() function in Python dir() is a powerful inbuilt function in Python3, which returns list of