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)