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)