# Python. The cmath module. Working with complex numbers. Trigonometric functions

## The cmath module. Working with complex numbers. Trigonometric functions. Hyperbolic functions. Classification functions

### Contents

Search other websites:

##### 1. Trigonometric functions

The cmath module contains 6 trigonometric functions that process complex numbers:

• cmath.acos(x) – returns the inverse cosine of the x argument;
• cmath.asin(x) – returns the arcsine of the x argument;
• cmath.atan(x) – defines the arctangent of the argument x;
• cmath.cos(x) – returns the cosine of the x argument;
• cmath.sin(x) – returns the sine of the x argument;
• cmath.tan(x) – returns the tangent of the x argument.

### ⇑

##### 1.1. Functions cmath.acos(), cmath.asin(), cmath.atan(). Get inverse cosine, arcsine, arctangent of an argument

Functions

```cmath.acos(x)
cmath.asin(x)```

return the inverse cosine and inverse sine of the argument x, respectively.

Each of the functions has two branch cuts: one extends right from 1 along the real axis to ∞, continuous from below. The other extends left from -1 along the real axis to -∞, continuous from above.

Function

`cmath.atan(x)`

allows you to get the arctangent of the argument x.

The function has two branch cuts. One extends from 1j along the imaginary axis to ∞j, continuous from the right. The other extends from -1j along the imaginary axis to -∞j, continuous from the left.

Example.

```# Trigonometric functions acos(), asin(), atan()

# 1. Include module cmath
import cmath

# 2. Get a complex number from the keyboard
re = int(input('re = '))
im = int(input('im = '))
z = complex(re, im)

# 3. Calculate the inverse cosine of argument z and display it
arc_cos = cmath.acos(z)
print('arc_cos = ', arc_cos)

# 4. Calculate the inverse sine of z
arc_sin = cmath.asin(z)
print('arc_sin = ', arc_sin)

# 5. Calculate the inverse tangent of z
arc_tan = cmath.atan(z)
print('arc_tan = ', arc_tan)```

Test example

```re = 3
im = -4
arc_cos = (0.9368124611557198+2.305509031243477j)
arc_sin = (0.6339838656391766-2.305509031243477j)
arc_tan = (1.4483069952314644-0.15899719167999918j)```

### ⇑

##### 1.2. Functions cmath.cos(), cmath.sin(), cmath.tan(). Get cosine, sine and tangent of an argument

Functions

```cmath.cos(x)
cmath.sin(x)
cmath.tan(x)```

return the cosine, sine, and tangent of the argument x.

Example.

```# Functions cos(), sin(), tan()

# Include module cmath
import cmath

# Create a complex number z = 7-8j
z = complex(7, -8)

# Calculate cosine, sine, tangent

sin_res = cmath.sin(z)
cos_res = cmath.cos(z)
tan_res = cmath.tan(z)
print('sin_res = ', sin_res)
print('cos_res = ', cos_res)
print('tan_res = ', tan_res)```

Test example

```sin_res = (979.2248346123021-1123.6753468137035j)
cos_res = (1123.675599719735+979.2246142178511j)
tan_res = (2.2295633684101687e-07-0.9999999692244822j)```

### ⇑

##### 2. Hyperbolic functions

The cmath module contains the implementation of the following hyperbolic functions that operate on complex numbers:

• cmath.acosh(x) – returns the inverse hyperbolic cosine of the x argument;
• cmath.asinh(x) – returns the hyperbolic arcsine of the argument x;
• cmath.atanh(x) – returns the hyperbolic arctangent of the argument x;
• cmath.cosh(x) – returns the hyperbolic cosine of the argument x;
• cmath.sinh(x) – returns the hyperbolic sine of the argument x;
• cmath.tanh(x) – Returns the hyperbolic tangent of the x argument.

### ⇑

##### 2.1. Functions cmath.acosh(), cmath.asinh(), cmath.atanh(). Hyperbolic inverse cosine, arcsine, arctangent

To calculate the hyperbolic arccosine of the argument x, use the function

`cmath.acosh(x)`

For this function, there is one branch cut, which extends left from 1 along the real axis to -∞, continuous from above.

To calculate the hyperbolic inverse sine of the argument x, use the function

`cmath.asinh(x)`

There are two branch cuts: One extends from 1j along the imaginary axis to ∞j, continuous from the right. The other extends from -1j along the imaginary axis to -∞j, continuous from the left.

To calculate the hyperbolic arctangent of the argument x, use the function

`cmath.atanh(x)`

There are two branch cuts. One extends from 1 along the real axis to ∞, continuous from below. The other extends from -1 along the real axis to -∞, continuous from above.

Example.

The example implements the calculation of the hyperbolic arc cosine, hyperbolic arc sine and hyperbolic arc tangent. It also demonstrates the output of the result for the hyperbolic arccosine in a convenient form with an accuracy of 2 decimal places.

```# Trigonometric functions acosh(), asinh(), atanh()

# 1. Include module cmath
import cmath

# 2. Get a complex number
re = int(input('re = ')) # real part
im = int(input('im = ')) # imaginary part
z = complex(re, im)

# 3. Calculate the inverse hyperbolic cosine of the argument z
# and display it to 2 decimal places
# 3.1. Get a value
arc_cosh = cmath.acos(z)

# 3.2. Form components of a complex number
re_str = '%.2f' % arc_cosh.real # real part of complex number arc_cosh
im_str = '%.2f' % arc_cosh.imag # imaginary part

# 3.3. Display in a convenient form
if arc_cosh.imag>0:
print('arc_cosh = ' + re_str + '+' + im_str + 'j')
elif arc_cosh.imag==0:
print('arc_cosh = ' + re_str)
else:
print('arc_cosh = ' + re_str + im_str + 'j')

# 4. Calculate and output the hyperbolic arctangent
arc_tanh = cmath.atanh(z)
print('arc_tanh = ', arc_tanh)

# 5. Calculate and output the hyperbolic arcsine
arc_sinh = cmath.asinh(z)
print('arc_sinh = ', arc_sinh)```

Test example

```re = 3
im = -4
arc_cosh = 0.94+2.31j
arc_tanh = (0.1175009073114339-1.4099210495965755j)
arc_sinh = (2.2999140408792695-0.9176168533514787j)```

### ⇑

##### 2.2. Functions cmath.cosh(), cmath.sinh(), cmath.tanh(). Hyperbolic cosine, sine, tangent

To calculate the hyperbolic cosine, sine and tangent, respectively, use the functions

```cmath.cosh(x)
cmath.sinh(x)
cmath.tanh(x)```

Example.

```# Trigonometric functions cosh(), sinh(), tanh()

# 1. Include the cmath module
import cmath

# 2. Get a complex number
re = int(input('re = ')) # real part
im = int(input('im = ')) # imaginary part
z = complex(re, im)

# 3. Calculate the hyperbolic cosine of the argument z
# and print it with precision 2 decimal places
# 3.1. Get a value
cosh = cmath.cosh(z)

# 3.2. Generate the components of a complex number
re_str = '%.2f' % cosh.real # the real part of the complex number cosh
im_str = '%.2f' % cosh.imag # imaginary part

# 3.3. Display in a convenient form
res_str = re_str
if cosh.imag>0:
res_str += '+'
res_str += im_str + 'j'
print('cosh = ' + res_str)

# 4. Calculate and output the hyperbolic tangent
tanh = cmath.tanh(z)
print('tanh = ', tanh)

# 5. Calculate and output hyperbolic sine
sinh = cmath.sinh(z)
print('sinh = ', sinh)```

Test example

```re = 4
im = -3
cosh = -27.03-3.85j
tanh = (0.999355987381473+0.0001873462046294784j)
sinh = (-27.016813258003932-3.8537380379193773j)```

### ⇑

##### 3. Classification functions

The classifications functions of the cmath module include the following:

• cmath.isinfinite(x) – determines whether there is a finite real and imaginary part of a complex number x;
• cmath.isinf(x) – determines whether there is an infinite real or imaginary part of a complex number x;
• cmath.isnan(x) – determines whether the real or imaginary parts of the complex number x are of value Nan;
• cmath.isclose (x) – determines the closeness of two values to each other.

### ⇑

##### 3.1. cmath.isfinite(). Finite complex number

Function

`cmath.isfinite(x)`

returns True if the real and imaginary parts of the complex number are finite. Otherwise, the function returns False.

Example.

```# Function isfinite()

# 1. Include the cmath module
import cmath

# 2. Get a complex number
re = int(input('re = ')) # real part
im = int(input('im = ')) # imaginary part
z = complex(re, im)

# 3. Using the isfinite() function
res = cmath.isfinite(z)

if res:
print('Both parts are finite.')
else:
print('Both parts are not finite.')```

Test example

```re = 5
im = -2
Both parts are finite.```

### ⇑

##### 3.2. cmath.isinf(). Finity of the components of a complex number

Function

`cmath.isinf(x)`

returns True if the real (x.real) or imaginary (x.imag) parts of the complex number are infinite. Otherwise, False is returned.

Example.

```# Function isinf()

# 1. Include the cmath module
import cmath

# 2. Get a complex number
re = int(input('re = ')) # real part
im = int(input('im = ')) # imaginary part
z = complex(re, im)

# 3. Using isinf() function
res = cmath.isinf(z)

if res:
print('Both parts are not finite.')
else:
print('Both parts are finite.')```

Test example

```re = 5
im = -3
Both parts are finite.```

### ⇑

##### 3.3. cmath.isnan(). Checking the components of a complex number for the value of Nan

Function

`cmath.isnan(x)`

returns True if one of the parts (real or imaginary) of the complex number is Nan. Otherwise, the function returns False.

Example.

```# Function isnan()

# 1. Include module cmath
import cmath

# 2. Get a complex number
re = int(input('re = ')) # real part
im = int(input('im = ')) # imaginary part
z = complex(re, im)

# 3. Using the isnan() function
res = cmath.isnan(z)

if res:
print('One of the parts is equal to the Nan value.')
else:
print('Both parts are not equal to Nan.')```

Test example

```re = 2
im = -7
Both parts are not equal to Nan.```

### ⇑

##### 3.4. cmath.isclose(). Determining the closeness of two values to each other

The cmath.isclose() function allows you to determine the closeness of two values to each other based on a given precision. According to the Python documentation, the syntax for declaring a function is as follows:

`cmath.isclose(a, b, rel_tol=1e-09, abs_tol = 0.0)`

here

• a, b – complex numbers that are compared with each other. If the values are close (equal with the specified precision), then the function returns True, otherwise the function returns False;
• rel_tol – optional parameter, sets the relative error during comparison;
• abs_tol – an optional parameter that defines the absolute error between the values of the elements.

Example.

```# Function isclose()

# Include the cmath module
import cmath

# Case 1. Two complex numbers are not equal
z1 = complex(2.5, 3.2) # z = 2.5+3.2j
z2 = complex(2.5000001, 3.2) # z = 2.5000001+3.2j

# Using the isclose() function
res = cmath.isclose(z1, z2) # False
if res:
print('z1 == z2')
else:
print('z1 != z2')

# Case 2. Two complex numbers are equal
z1 = complex(7.2, -1.7) # z1 = 7.2-1.7j
z2 = complex(7.2, -1.7) # z2 = 7.2-1.7j

# Use the isclose() function
res = cmath.isclose(z1, z2) # True
if res:
print('z1 == z2')
else:
print('z1 != z2')

# Case 3. Two complex numbers are equal with the specified relative precision
z1 = complex(3.001, 2.8)
z2 = complex(3.0, 2.8)

# Using the isclose() function, specifies a relative precision of 0.01
res = cmath.isclose(z1, z2, rel_tol=0.01) # z1 == z2
if res:
print('z1 == z2')
else:
print('z1 != z2')

# Case 4. Two complex numbers are equal with specified absolute precision
z1 = complex(1.0001, 5.5)
z2 = complex(1.0, 5.5)

# Function call isclose (), absolute precision 0.001 is specified
res = cmath.isclose(z1, z2, abs_tol=0.001) # z1 == z2
if res:
print('z1 == z2')
else:
print('z1 != z2')```

Test example

```z1 != z2
z1 == z2
z1 == z2
z1 == z2```