Python

Python cmath.exp() Method with Examples

cmath.exp() Method in Python:

The cmath.exp() method takes a complex number as an argument and returns the exponential value. If the number is x, it returns e**x, where e is the natural logarithm base.

Syntax:

cmath.exp(x)

Parameters

x: This is Required. A number whose exponential value is to be determined.

Return Value:

Returns a  complex value that represents a complex number’s exponential.

Examples:

Example1:

Input:

Given Complex Number = 3+4j

Output:

The given complex number's (3+4j) exponential value = 
(-13.128783081462158-15.200784463067954j)

Example2:

Input:

Given realpart = 5
Given imaginary part = 2

Output:

The given complex number's (5+2j) exponential value = 
(-61.761666662504986+134.9517036790434j)

Note: The above input format is for dynamic input.

cmath.exp() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the complex number as static input and store it in a variable.
  • Pass the given complex number as an argument to the cmath.exp() method that returns the given complex number’s exponential value.
  • Store it in another variable.
  • Print the exponential value of the given complex number.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the complex number as static input and store it in a variable.
complexnumb = 3+4j
# Pass the given complex number as an argument to the cmath.exp() method that
# returns the the given complex number's exponential value.
# Store it in another variable.
rslt = cmath.exp(complexnumb)
# Print the exponential value of the given complex number.
print("The given complex number's", complexnumb,
      "exponential value = ")
print(rslt)

Output:

The given complex number's (3+4j) exponential value = 
(-13.128783081462158-15.200784463067954j)

Similarly, try for the other examples

import cmath
complexnumb = -1-2j
rslt = cmath.exp(complexnumb)
print("The given complex number's", complexnumb,
      "exponential value = ")
print(rslt)

Output:

The given complex number's (-1-2j) exponential value = 
(-0.1530918656742263-0.33451182923926226j)

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the real part and imaginary part of the complex number as user input using map(), int(), split().
  • Store it in two variables.
  • Using a complex() function convert those two variables into a complex number and store them in a variable.
  • Pass the given complex number as an argument to the cmath.exp() method that returns the given complex number’s exponential value.
  • Store it in another variable.
  • Print the exponential value of the given complex number.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the real part and imaginary part of the complex number as user input
# using map(), int(), split().
# Store it in two variables.
realnumb, imaginarynumb = map(int, input(
    'Enter real part and complex part of the complex number = ').split())
# Using a complex() function convert those two variables into a complex number.
complexnumb = complex(realnumb, imaginarynumb)

# Pass the given complex number as an argument to the cmath.exp() method that
# returns the the given complex number's exponential value.
# Store it in another variable.
rslt = cmath.exp(complexnumb)
# Print the exponential value of the given complex number.
print("The given complex number's", complexnumb,
      "exponential value = ")
print(rslt)

Output:

Enter real part and complex part of the complex number = 5 2
The given complex number's (5+2j) exponential value = 
(-61.761666662504986+134.9517036790434j)

Python cmath.exp() Method with Examples Read More »

Python cmath.polar() Method with Examples

cmath.polar() Method in Python:

A complex number is converted to polar coordinates using the cmath.polar() method. It returns a tuple consisting of modulus and phase.

A complex number in polar coordinates is defined by modulus r and phase angle phi.

Syntax:

cmath.polar(x)

Parameters

x: This is Required. A number used to calculate the polar coordinates of

Return Value:

Returns a tuple value containing polar coordinates.

Examples:

Example1:

Input:

Given Complex Number = 3+4j

Output:

The polar coordinates of given complex number (3+4j)  = 
(5.0, 0.9272952180016122)

Example2:

Input:

Given realpart = 5
Given imaginary part = 2

Output:

The polar coordinates of given complex number (5+2j) = 
(5.385164807134504, 0.3805063771123649)

Note: The above input format is for dynamic input.

cmath.polar() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the complex number as static input and store it in a variable.
  • Pass the given complex number as an argument to the cmath.polar() method that converts the given complex number to polar coordinates and returns a tuple consisting of modulus and phase.
  • Store it in another variable.
  • Print the result tuple consisting of modulus and phase.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the complex number as static input and store it in a variable.
complexnumb = 3+4j
# Pass the given complex number as an argument to the cmath.polar() method
# that converts the given complex number to polar coordinates and returns a
# tuple consisting of modulus and phase.
# Store it in another variable.
rslt = cmath.polar(complexnumb)
# Print the result tuple consisting of modulus and phase.
print("The polar coordinates of given complex number", complexnumb,
      " = ")
print(rslt)

Output:

The polar coordinates of given complex number (3+4j)  = 
(5.0, 0.9272952180016122)

Similarly, try for the other examples

import cmath
complexnumb = -1-2j
rslt = cmath.polar(complexnumb)
print("The polar coordinates of given complex number", complexnumb,
      " = ")
print(rslt)

Output:

The polar coordinates of given complex number (-1-2j)  = 
(2.23606797749979, -2.0344439357957027)

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the real part and imaginary part of the complex number as user input using map(), int(), split().
  • Store it in two variables.
  • Using a complex() function convert those two variables into a complex number and store them in a variable.
  • Pass the given complex number as an argument to the cmath.polar() method that converts the given complex number to polar coordinates and returns a tuple consisting of modulus and phase.
  • Store it in another variable.
  • Print the result tuple consisting of modulus and phase.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the real part and imaginary part of the complex number as user input
# using map(), int(), split().
# Store it in two variables.
realnumb, imaginarynumb = map(int, input(
    'Enter real part and complex part of the complex number = ').split())
# Using a complex() function convert those two variables into a complex number.
complexnumb = complex(realnumb, imaginarynumb)

# Pass the given complex number as an argument to the cmath.polar() method
# that converts the given complex number to polar coordinates and returns a
# tuple consisting of modulus and phase.
# Store it in another variable.
rslt = cmath.polar(complexnumb)
# Print the above result tuple consisting of modulus and phase.
print("The polar coordinates of given complex number", complexnumb,
      " = ")
print(rslt)

Output:

Enter real part and complex part of the complex number = 5 2
The polar coordinates of given complex number (5+2j) = 
(5.385164807134504, 0.3805063771123649)

Python cmath.polar() Method with Examples Read More »

Python cmath.sin() Method with Examples

cmath.sin() Method in Python:

The sine of a number is returned by the cmath.sin() method.

Sine is a trigonometric function that represents the ratio of a right triangle’s opposite side to its hypotenuse.

Syntax:

cmath.sin(x)

Parameters

x: This is Required. It is a number that can be used to calculate the sine of

Return Value:

Returns a complex value that denotes the sine of a complex number.

Examples:

Example1:

Input:

Given Complex Number = 3+4j

Output:

The given complex number's (3+4j) sine value = 
(3.853738037919377-27.016813258003932j)

Example2:

Input:

Given realpart = 5
Given imaginary part = 2

Output:

The given complex number's (5+2j) sine value = 
(-3.6076607742131563+1.0288031496599335j)

Note: The above input format is for dynamic input.

cmath.sin() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the complex number as static input and store it in a variable.
  • Pass the given complex number as an argument to the cmath.sin() method that returns the given complex number’s sine value.
  • Store it in another variable.
  • Print the sine value of the given complex number.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the complex number as static input and store it in a variable.
complexnumb = 3+4j
# Pass the given complex number as an argument to the cmath.sin() method that
# returns the given complex number's sine value.
# Store it in another variable.
rslt = cmath.sin(complexnumb)
# Print the sine value of the given complex number.
print("The given complex number's", complexnumb,
      "sine value = ")
print(rslt)

Output:

The given complex number's (3+4j) sine value = 
(3.853738037919377-27.016813258003932j)

Similarly, try for the other examples

import cmath
complexnumb = -1-2j
rslt = cmath.sin(complexnumb)
print("The given complex number's", complexnumb,
      "sine value = ")
print(rslt)

Output:

The given complex number's (-1-2j) sine value = 
(-3.165778513216168-1.9596010414216063j)

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the real part and imaginary part of the complex number as user input using map(), int(), split().
  • Store it in two variables.
  • Using a complex() function convert those two variables into a complex number and store them in a variable.
  • Pass the given complex number as an argument to the cmath.sin() method that returns the given complex number’s sine value.
  • Store it in another variable.
  • Print the sine value of the given complex number.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the real part and imaginary part of the complex number as user input
# using map(), int(), split().
# Store it in two variables.
realnumb, imaginarynumb = map(int, input(
    'Enter real part and complex part of the complex number = ').split())
# Using a complex() function convert those two variables into a complex number.
complexnumb = complex(realnumb, imaginarynumb)

# Pass the given complex number as an argument to the cmath.sin() method that
# returns the given complex number's sine value.
# Store it in another variable.
rslt = cmath.sin(complexnumb)
# Print the sine value of the given complex number.
print("The given complex number's", complexnumb,
      "sine value = ")
print(rslt)

Output:

Enter real part and complex part of the complex number = 5 2
The given complex number's (5+2j) sine value = 
(-3.6076607742131563+1.0288031496599335j)

Python cmath.sin() Method with Examples Read More »

Python cmath.sinh() Method with Examples

cmath.sinh() Method in Python:

The cmath.sinh() method returns the given complex number’s hyperbolic sine.

Syntax:

cmath.sinh(x)

Parameters

x: This is Required. It is a number used to calculate the hyperbolic sine of

Return Value:

Returns a complex value that represents the complex number’s hyperbolic sine.

Examples:

Example1:

Input:

Given Complex Number = 3+4j

Output:

The given complex number's (3+4j) hyperbolic sine value = 
(-6.5481200409110025-7.61923172032141j)

Example2:

Input:

Given realpart = 5
Given imaginary part = 2

Output:

The given complex number's (5+2j) hyperbolic sine value = 
(-30.879431343588244+67.47891523845588j)

Note: The above input format is for dynamic input.

cmath.sinh() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the complex number as static input and store it in a variable.
  • Pass the given complex number as an argument to the cmath.sinh() method that returns the given complex number’s hyperbolic sine value.
  • Store it in another variable.
  • Print the hyperbolic sine value of the given complex number.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the complex number as static input and store it in a variable.
complexnumb = 3+4j
# Pass the given complex number as an argument to the cmath.sinh() method that
# returns the given complex number's hyperbolic sine value.
# Store it in another variable.
rslt = cmath.sinh(complexnumb)
# Print the hyperbolic sine value of the given complex number.
print("The given complex number's", complexnumb,
      "hyperbolic sine value = ")
print(rslt)

Output:

The given complex number's (3+4j) hyperbolic sine value = 
(-6.5481200409110025-7.61923172032141j)

Similarly, try for the other examples

import cmath
complexnumb = -1-3j
rslt = cmath.sinh(complexnumb)
print("The given complex number's", complexnumb,
      "hyperbolic sine value = ")
print(rslt)

Output:

The given complex number's (-1-3j) hyperbolic sine value = 
(1.1634403637032504-0.21775955162215221j)

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the real part and imaginary part of the complex number as user input using map(), int(), split().
  • Store it in two variables.
  • Using a complex() function convert those two variables into a complex number and store them in a variable.
  • Pass the given complex number as an argument to the cmath.sinh() method that returns the given complex number’s hyperbolic sine value.
  • Store it in another variable.
  • Print the hyperbolic sine value of the given complex number.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the real part and imaginary part of the complex number as user input
# using map(), int(), split().
# Store it in two variables.
realnumb, imaginarynumb = map(int, input(
    'Enter real part and complex part of the complex number = ').split())
# Using a complex() function convert those two variables into a complex number.
complexnumb = complex(realnumb, imaginarynumb)

# Pass the given complex number as an argument to the cmath.sinh() method that
# returns the given complex number's hyperbolic sine value.
# Store it in another variable.
rslt = cmath.sinh(complexnumb)
# Print the hyperbolic sine value of the given complex number.
print("The given complex number's", complexnumb,
      "hyperbolic sine value = ")
print(rslt)

Output:

Enter real part and complex part of the complex number = 5 2
The given complex number's (5+2j) hyperbolic sine value = 
(-30.879431343588244+67.47891523845588j)

Python cmath.sinh() Method with Examples Read More »

Python cmath.cosh() Method with Examples

cmath.cosh() Method in Python:

The cmath.cosh() method returns the given complex number’s hyperbolic cosine.

Syntax:

cmath.cosh(x)

Parameters

x: This is Required. It is a number for calculating the hyperbolic cosine of

Return Value:

Returns a complex value that represents the complex number’s hyperbolic cosine.

Examples:

Example1:

Input:

Given Complex Number = 3+4j

Output:

The given complex number's (3+4j) hyperbolic cosine value = 
(-6.580663040551157-7.581552742746545j)

Example2:

Input:

Given realpart = 5
Given imaginary part = 2

Output:

The given complex number's (5+2j) hyperbolic cosine value = 
(-30.88223531891674+67.47278844058752j)

Note: The above input format is for dynamic input.

cmath.cosh() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the complex number as static input and store it in a variable.
  • Pass the given complex number as an argument to the cmath.cosh() method that returns the given complex number’s hyperbolic cosine value.
  • Store it in another variable.
  • Print the hyperbolic cosine value of the given complex number.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the complex number as static input and store it in a variable.
complexnumb = 3+4j
# Pass the given complex number as an argument to the cmath.cosh() method that
# returns the given complex number's hyperbolic cosine value.
# Store it in another variable.
rslt = cmath.cosh(complexnumb)
# Print the hyperbolic cosine value of the given complex number.
print("The given complex number's", complexnumb,
      "hyperbolic cosine value. = ")
print(rslt)

Output:

The given complex number's (3+4j) hyperbolic cosine value. = 
(-6.580663040551157-7.581552742746545j)

Similarly, try for the other examples

import cmath
complexnumb = -5-1j
rslt = cmath.cosh(complexnumb)
print("The given complex number's", complexnumb,
      "hyperbolic cosine value = ")
print(rslt)

Output:

The given complex number's (-5-1j) hyperbolic cosine value = 
(40.09580630629883+62.43984868079963j)

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the real part and imaginary part of the complex number as user input using map(), int(), split().
  • Store it in two variables.
  • Using a complex() function convert those two variables into a complex number and store them in a variable.
  • Pass the given complex number as an argument to the cmath.cosh() method that returns the given complex number’s hyperbolic cosine value.
  • Store it in another variable.
  • Print the hyperbolic cosine value of the given complex number.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the real part and imaginary part of the complex number as user input
# using map(), int(), split().
# Store it in two variables.
realnumb, imaginarynumb = map(int, input(
    'Enter real part and complex part of the complex number = ').split())
# Using a complex() function convert those two variables into a complex number.
complexnumb = complex(realnumb, imaginarynumb)

# Pass the given complex number as an argument to the cmath.cosh() method that
# returns the given complex number's hyperbolic cosine value.
# Store it in another variable.
rslt = cmath.cosh(complexnumb)
# Print the hyperbolic cosine value of the given complex number.
print("The given complex number's", complexnumb,
      "hyperbolic cosine value = ")
print(rslt)

Output:

Enter real part and complex part of the complex number = 5 2
The given complex number's (5+2j) hyperbolic cosine value = 
(-30.88223531891674+67.47278844058752j)

Python cmath.cosh() Method with Examples Read More »

Python cmath.cos() Method with Examples

cmath.cos() Method in Python:

The cosine of a complex number is returned by the cmath.cos() method.

Syntax:

cmath.cos(x)

Parameters

x: This is Required. It is a number that can be used to calculate the cosine of

Return Value:

Returns a complex value that denotes the cosine of a complex number.

Examples:

Example1:

Input:

Given Complex Number = 3+4j

Output:

The given complex number's (3+4j) cosine value = 
(-27.034945603074224-3.851153334811777j)

Example2:

Input:

Given realpart = 5
Given imaginary part = 2

Output:

The given complex number's (5+2j) cosine value = 
(1.0671926518731156+3.4778844858991573j)

Note: The above input format is for dynamic input.

cmath.cos() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the complex number as static input and store it in a variable.
  • Pass the given complex number as an argument to the cmath.cos() method that returns the given complex number’s cosine value.
  • Store it in another variable.
  • Print the cosine value of the given complex number.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the complex number as static input and store it in a variable.
complexnumb = 3+4j
# Pass the given complex number as an argument to the cmath.cos() method that
# returns the given complex number's cosine value.
# Store it in another variable.
rslt = cmath.cos(complexnumb)
# Print the cosine value of the given complex number.
print("The given complex number's", complexnumb,
      "cosine value = ")
print(rslt)

Output:

The given complex number's (3+4j) cosine value = 
(-27.034945603074224-3.851153334811777j)

Similarly, try for the other examples

import cmath
complexnumb = -1-2j
rslt = cmath.cos(complexnumb)
print("The given complex number's", complexnumb,
      "cosine value = ")
print(rslt)

Output:

The given complex number's (-1-2j) cosine value = 
(2.0327230070196656-3.0518977991518j)

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the real part and imaginary part of the complex number as user input using map(), int(), split().
  • Store it in two variables.
  • Using a complex() function convert those two variables into a complex number and store them in a variable.
  • Pass the given complex number as an argument to the cmath.cos() method that returns the given complex number’s cosine value.
  • Store it in another variable.
  • Print the cosine value of the given complex number.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the real part and imaginary part of the complex number as user input
# using map(), int(), split().
# Store it in two variables.
realnumb, imaginarynumb = map(int, input(
    'Enter real part and complex part of the complex number = ').split())
# Using a complex() function convert those two variables into a complex number.
complexnumb = complex(realnumb, imaginarynumb)

# Pass the given complex number as an argument to the cmath.cos() method that
# returns the given complex number's cosine value.
# Store it in another variable.
rslt = cmath.cos(complexnumb)
# Print the cosine value of the given complex number.
print("The given complex number's", complexnumb,
      "cosine value = ")
print(rslt)

Output:

Enter real part and complex part of the complex number = 5 2
The given complex number's (5+2j) cosine value = 
(1.0671926518731156+3.4778844858991573j)

Python cmath.cos() Method with Examples Read More »

Python cmath.atanh() Method with Examples

cmath.atanh() Method in Python:

The cmath.atanh() method returns the complex number’s inverse hyperbolic tangent.

There are two types of branch cuts:

  1. Extends along the real axis from 1 to ∞, and is continuous from below.
  2. Extends along the real axis from -1 to -∞, and is continuous from above.

Syntax:

cmath.atanh(x)

Parameters

x: This is Required. It is a number used to calculate the inverse hyperbolic arctangent of

Return Value:

Returns a complex value that represents the complex number’s inverse hyperbolic tangent.

Examples:

Example1:

Input:

Given Complex Number = 3-4j

Output:

The given complex number's (3-4j) inverse hyperbolic tangent value = 
(0.1175009073114339-1.4099210495965755j)

Example2:

Input:

Given realpart = 5
Given imaginary part = 3

Output:

The given complex number's (5+3j) inverse hyperbolic tangent value = 
(0.14694666622552977+1.4808695768986575j)

Note: The above input format is for dynamic input.

cmath.atanh() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the complex number as static input and store it in a variable.
  • Pass the given complex number as an argument to the cmath.atanh() method that returns the given complex number’s inverse hyperbolic tangent value.
  • Store it in another variable.
  • Print the inverse hyperbolic tangent value of the given complex number.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the complex number as static input and store it in a variable.
complexnumb = 3-4j
# Pass the given complex number as an argument to the cmath.atanh() method that
# returns the given complex number's inverse hyperbolic tangent value.
# Store it in another variable.
rslt = cmath.atanh(complexnumb)
# Print the inverse hyperbolic tangent value of the given complex number.
print("The given complex number's", complexnumb,
      "inverse hyperbolic tangent value = ")
print(rslt)

Output:

The given complex number's (3-4j) inverse hyperbolic tangent value = 
(0.1175009073114339-1.4099210495965755j)

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the real part and imaginary part of the complex number as user input using map(), int(), split().
  • Store it in two variables.
  • Using a complex() function convert those two variables into a complex number and store it in a variable.
  • Pass the given complex number as an argument to the cmath.atanh() method that returns the given complex number’s inverse hyperbolic tangent value.
  • Store it in another variable.
  • Print the inverse hyperbolic tangent value of the given complex number.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the real part and imaginary part of the complex number as user input
# using map(), int(), split().
# Store it in two variables.
realnumb, imaginarynumb = map(int, input(
    'Enter real part and complex part of the complex number = ').split())
# Using a complex() function convert those two variables into a complex number.
complexnumb = complex(realnumb, imaginarynumb)

# Pass the given complex number as an argument to the cmath.atanh() method that
# returns the given complex number's inverse hyperbolic tangent value.
# Store it in another variable.
rslt = cmath.atanh(complexnumb)
# Print the inverse hyperbolic tangent value of the given complex number.
print("The given complex number's", complexnumb,
      "inverse hyperbolic tangent value = ")
print(rslt)

Output:

Enter real part and complex part of the complex number = 5 3
The given complex number's (5+3j) inverse hyperbolic tangent value = 
(0.14694666622552977+1.4808695768986575j)

Python cmath.atanh() Method with Examples Read More »

Python cmath.rect() Method with Examples

cmath.rect() Method in Python:

The cmath.rect() method converts polar coordinates to the complex number’s rectangular form. It generates a complex number that includes phase and modulus.

This method equals r * (math.cos(phi) + math.sin(phi)*1j).

The radius r is the vector’s length, and phi (phase angle) is the angle formed with the real axis.

Syntax:

cmath.rect(r, phi)

Parameters

r: This is Required. The modulus of a complex number is represented by this symbol.

phi: This is Required. It represents a complex number’s phase.

Return Value:

Returns a complex value that represents a complex number in its rectangular form.

Examples:

Example1:

Input:

Given modulus = 2.135667
Given phase = 6.1111116

Output:

The rectangular form of the complex number =  (2.104127071172476-0.3656812864341552j)

Example2:

Input:

Given modulus = 5
Given phase = 2

Output:

The rectangular form of the complex number =  (-2.080734182735712+4.546487134128409j)

cmath.rect() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the modulus of a complex number (r) as static input and store it in a variable.
  • Give the phase of a complex number (phi) as static input and store it in another variable.
  • Pass the given modulus, phase of a complex number as the arguments to the cmath.rect() method which returns a complex value that represents the complex number in its rectangular form.
  • Store it in another variable.
  • Print a complex value that represents the complex number in its rectangular form.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the modulus of a complex number (r) as static input and store it in a variable.
gvn_moduls_val = 2.135667
# Give the phase of a complex number (phi) as static input and store it
# in another variable.
gvn_phasee_val = 6.1111116
# Pass the given modulus, phase of a complex number as the arguments to the
# cmath.rect() method which returns a complex value that represents the
# complex number in its rectangular form.
# Store it in another variable.
rslt = cmath.rect(gvn_moduls_val, gvn_phasee_val)
# Print a complex value that represents the complex number in its rectangular form.
print("The rectangular form of the complex number = ", rslt)

Output:

The rectangular form of the complex number =  (2.104127071172476-0.3656812864341552j)

Similarly, try for the other examples

import cmath
gvn_moduls_val = 5
gvn_phasee_val = 2
rslt = cmath.rect(gvn_moduls_val, gvn_phasee_val)
print("The rectangular form of the complex number = ", rslt)

Output:

The rectangular form of the complex number =  (-2.080734182735712+4.546487134128409j)

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the modulus of a complex number (r) as user input using the float(input()) function and store it in a variable.
  • Give the phase of a complex number (phi) as user input using the float(input()) function and store it in another variable.
  • Pass the given modulus, phase of a complex number as the arguments to the cmath.rect() method which returns a complex value that represents the complex number in its rectangular form.
  • Store it in another variable.
  • Print a complex value that represents the complex number in its rectangular form.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the modulus of a complex number (r) as user input using the float(input()) function
# and store it in a variable.
gvn_moduls_val = float(input("Enter some random number = "))
# Give the phase of a complex number (phi) as user input using the float(input()) function
# and store it in another variable.
gvn_phasee_val = float(input("Enter some random number = "))
# Pass the given modulus, phase of a complex number as the arguments to the
# cmath.rect() method which returns a complex value that represents the
# complex number in its rectangular form.
# Store it in another variable.
rslt = cmath.rect(gvn_moduls_val, gvn_phasee_val)
# Print a complex value that represents the complex number in its rectangular form.
print("The rectangular form of the complex number = ", rslt)

Output:

Enter some random number = 1.56
Enter some random number = 2.34546
The rectangular form of the complex number = (-1.0911821806705588+1.1148638699801174j)

Python cmath.rect() Method with Examples Read More »

Python cmath.isfinite() Method with Examples

cmath.isfinite() Method in Python:

The cmath.isfinite() method determines whether or not a given complex value is finite.

This method gives the following Boolean value: If the value is finite, True; otherwise, False.

Syntax:

cmath.isfinite(x)

Parameters

x: This is Required. It is the value that is used to determine whether it is finite or not.

Return Value:

Returns a boolean value that is True if the value is finite and False otherwise.

Examples:

Example1:

Input:

Given Complex Number = float('inf')+ 7j

Output:

Check if the given complex number (inf+7j) is finite or not =  False

Example2:

Input:

Given realpart = 5
Given imaginary part = 2

Output:

Check if the given complex number (5+2j) is finite or not = True

Note: The above input format is for dynamic input.

cmath.isfinite() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the complex number as static input and store it in a variable.
  • Pass the given complex number as an argument to the cmath.isfinite() method that returns a boolean value that is True if the given complex number is finite and False otherwise.
  • Store it in another variable.
  • Print the above result after checking if the given complex number is finite or not.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the complex number as static input and store it in a variable.
complexnumb = 3+4j
# Pass the given complex number as an argument to the cmath.isfinite() method
# that returns a boolean value that is True if the given complex number is
# finite and False otherwise.
# Store it in another variable.
rslt = cmath.isfinite(complexnumb)
# Print the above result after checking if the given complex number is finite or not.
print("Check if the given complex number",
      complexnumb, "is finite or not = ", rslt)

Output:

Check if the given complex number (3+4j) is finite or not =  True

Similarly, try for the other examples

import cmath
complexnumb = complex(3, float('inf'))
rslt = cmath.isfinite(complexnumb)
print("Check if the given complex number",
      complexnumb, "is finite or not = ", rslt)

Output:

Check if the given complex number (3+infj) is finite or not =  False
import cmath
complexnumb = float('inf')+ 7j
rslt = cmath.isfinite(complexnumb)
print("Check if the given complex number",
      complexnumb, "is finite or not = ", rslt)

Output:

Check if the given complex number (inf+7j) is finite or not =  False

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the real part and imaginary part of the complex number as user input using map(), int(), split().
  • Store it in two variables.
  • Using a complex() function convert those two variables into a complex number and store them in a variable.
  • Pass the given complex number as an argument to the cmath.isfinite() method that returns a boolean value that is True if the given complex number is finite and False otherwise.
  • Store it in another variable.
  • Print the above result after checking if the given complex number is finite or not.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the real part and imaginary part of the complex number as user input
# using map(), int(), split().
# Store it in two variables.
realnumb, imaginarynumb = map(int, input(
    'Enter real part and complex part of the complex number = ').split())
# Using a complex() function convert those two variables into a complex number.
complexnumb = complex(realnumb, imaginarynumb)

# Pass the given complex number as an argument to the cmath.isfinite() method
# that returns a boolean value that is True if the given complex number is
# finite and False otherwise.
# Store it in another variable.
rslt = cmath.isfinite(complexnumb)
# Print the above result after checking if the given complex number is finite or not.
print("Check if the given complex number",
      complexnumb, "is finite or not = ", rslt)

Output:

Enter real part and complex part of the complex number = 5 2
Check if the given complex number (5+2j) is finite or not = True

Python cmath.isfinite() Method with Examples Read More »

Python cmath.isclose() Method with Examples

cmath.isclose() Method in Python:

The cmath.isclose() method determines whether or not two complex values are close. This method gives the following Boolean value: If the values are close, True; otherwise, False.

To determine whether the values are close, this method employs either a relative tolerance or an absolute tolerance.

It compares the values using the following formula:

abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)

Syntax:

cmath.isclose(a, b, rel_tol= value, abs_tol= value)

Parameters

a: This is Required. The first value used to determine closeness

b: This is Required. The second value used to determine closeness

rel_tol: This is Optional. It is relative tolerance. It is the greatest (maximum) possible difference between values a and b. 1e-09 is the default value.

abs_tol: This is Optional. The absolute minimum tolerance. It is used to compare values close to zero. The value must be greater than zero.

Return Value:

Returns a true or false value. If the values are close, True; otherwise, False.

Examples:

Example1:

Input:

Given first Complex Number = 3+4j
Given second Complex Number = 3+4j

Output:

Checking if the given two complex values are close or not = True

Example2:

Input:

Given first Complex Number = 5+2j
Given second Complex Number = 5+26j

Output:

Checking if the given two complex values are close or not = False

cmath.isclose() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the first complex number as static input and store it in a variable.
  • Give the second complex number as static input and store it in another variable.
  • Pass the given two complex numbers as the arguments to the cmath.isclose() method that determines whether or not given two complex values is close.
  • Store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the complex number as static input and store it in a variable.
complexnumb1 = 3+4j
# Give the second complex number as static input and store it in another variable.
complexnumb2 = 3+4j
# Pass the given two complex numbers as the arguments to the cmath.isclose()
# method that determines whether or not given two complex values are close.
# Store it in another variable.
rslt = cmath.isclose(complexnumb1, complexnumb2)
# Print the above result
print("Checking if the given two complex values are close or not =", rslt)

Output:

Checking if the given two complex values are close or not = True

With giving absolute tolerance

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the first complex number as static input and store it in a variable.
  • Give the second complex number as static input and store it in another variable.
  • Pass the given two complex numbers and absolute tolerance as some random number as the arguments to the cmath.isclose() method that determines whether or not given two complex values is close.
  • Store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the complex number as static input and store it in a variable.
complexnumb1 = 5+2j
# Give the second complex number as static input and store it in another variable.
complexnumb2 = 5+2j
# Pass the given two complex numbers and absolute tolerance as some random number
# as the arguments to the cmath.isclose() method that determines whether or
# not given two complex values are close.
# Store it in another variable.
rslt = cmath.isclose(complexnumb1, complexnumb2, abs_tol=0.005)
# Print the above result
print("Checking if the given two complex values are close or not =", rslt)

Output:

Checking if the given two complex values are close or not = True

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the real part and imaginary part of the first complex number as user input using map(), int(), split().
  • Store it in two variables.
  • Using a complex() function convert those two variables into a complex number and store them in a variable.
  • Give the real part and imaginary part of the second complex number as user input using map(), int(), split().
  • Store it in two variables.
  • Using a complex() function convert those two variables into a complex number and store them in a variable.
  • Pass the given two complex numbers as the arguments to the cmath.isclose() method that determines whether or not given two complex values is close.
  • Store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the real part and imaginary part of the first complex number as user input
# using map(), int(), split().
# Store it in two variables.
realnumb1, imaginarynumb1 = map(int, input(
    'Enter real part and complex part of the complex number = ').split())
# Using a complex() function convert those two variables into a complex number.
complexnumb1 = complex(realnumb1, imaginarynumb1)
# Give the real part and imaginary part of the second complex number as user input
# using map(), int(), split().
# Store it in two variables.
realnumb2, imaginarynumb2 = map(int, input(
    'Enter real part and complex part of the complex number = ').split())
# Using a complex() function convert those two variables into a complex number.
complexnumb2 = complex(realnumb2, imaginarynumb2)

# Pass the given two complex numbers as the arguments to the cmath.isclose()
# method that determines whether or not given two complex values are close.
# Store it in another variable.
rslt = cmath.isclose(complexnumb1, complexnumb2)
# Print the above result
print("Checking if the given two complex values are close or not =", rslt)

Output:

Enter real part and complex part of the complex number = 5 2
Enter real part and complex part of the complex number = 3 2
Checking if the given two complex values are close or not = False

Python cmath.isclose() Method with Examples Read More »