Related Tags

python3

What are trigonometric functions in Python?

Fatima Numan

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

Trigonometry is a branch of mathematics that establishes relations between different angles and their directions.

Trigonometric functions

We use trigonometric functions to compute certain angles, which help us understand the geometry involved in our day-to-day lives.

The most commonly used functions include:

• $Sine$
• $Cosine$
• $Tangent$

Trigonometric functions in Python

Python has an entire module dedicated solely to basic and advanced mathematical operations involving arithmetic (addition, subtraction, multiplication, and division), exponential, logarithmic, and trigonometric functions. That library is known as math.

To use the math module, we can simply import it at the start of our code using:

import math
Syntax to add math library in Python

We're ready to use any trigonometric functions in our code.

Syntax

The following syntax is used to call any math function:

math.function_name(value)

Let's find the $sine$ value of a number $n$ to understand how these functions work.

We'll then use it in the following way:

Code

import math

n = 1 #assigning a value to n in radians
result = math.sin(n) #computing the result
print('The sin value of', n, 'is: ', result) 
How to use the sine function in the math module

Explanation

• Line 4: We called the function math.sin(n) on the input variable n.
• Line 5: Print the output stored in the result variable.

Note: The input in any of the trigonometric functions used in Python is in radians.

So, if we want to convert an angle from degrees to radians, we can use the math.radians(x), where x is an input in degrees.

Syntax for different trigonometric functions

The table below provides the syntax for the math module's most commonly used trigonometric functions.

Function Name Purpose Syntax
Sine Returns sine value of a number math.sin(input)
Cosine Returns cosine value of a number math.cos(input)
Tangent Returns tangent value of a number math.tan(input)
Arc Sine Returns inverse of sine math.asin(input)
Arc Cosine Returns inverse of cosine math.acos(input)
Arc Tangent Returns inverse of tangent math.atan(input)

Code

You may run these functions in the code provided below:

import math

n = 0.67 #input specified
print("math.sin(n) = " , math.sin(n))
print("math.cos(n) = " , math.cos(n))
print("math.tan(n) = " , math.tan(n))
print("math.asin(n) = " , math.asin(n))
print("math.acos(n) = " , math.acos(n))
print("math.atan(n) = " , math.atan(n))
How to use all of the trigonometric functions mentioned in the above table

Explanation

• Line 4–9: We called the trigonometric functions for the variable n and computed the required values while printing outputs.

Note: The range of inputs in asin(), acos()and atan()functions is -1 to 1.

Hyperbolic trigonometric functions

To use$hyperbolic$trigonometric functions, we'll have to import the cmath module:

import cmath

Syntax

The syntax for calling $hyperbolic$ trigonometric functions is the same as that of math module functions.

cmath.function_name(value)

The syntax of some commonly used $hyperbolic$ functions is provided in the table below:

Function Name Syntax
Hyberbolic Sine cmath.sinh(input)
Hyberbolic Cosine cmath.cosh(input)
Hyberbolic Tangent cmath.tanh(input)
Hyberbolic Arc Sine cmath.asinh(input)
Hyberbolic Arc Cosine cmath.acosh(input)
Hyberbolic Arc Tangent cmath.atanh(input)

The inputs for the functions mentioned above can be integers or complex numbers, depending on the function we choose to use. Let's now run these functions with complex inputs. We'll use the complex() function to convert our real and imaginary numbers into complex numbers.

Code

import cmath

x = 2 #input specified
y = 1
z = complex(x,y) #making the input a complex number
print("cmath.sinh(z) = " , cmath.sinh(z))
print("cmath.cosh(z) = " , cmath.cosh(z))
print("cmath.tanh(z) = " , cmath.tanh(z))
print("cmath.asinh(z) = " , cmath.asinh(z))
print("cmath.acosh(z) = " , cmath.acosh(z))
print("cmath.atanh(z) = " , cmath.atanh(z))
How to use the cmath library for the hyperbolic functions mentioned in the above table

Explanation

In the code provided, we've called the hyperbolic trigonometric functions as mentioned in the table provided.

• Lines 3–4: We assigned the value x as the real part and y as the imaginary part.
• Line 5: We have created a complex number z by calling the complex() function on the variables x and y.
• Line 6–11: We have called the hyperbolic functions for the complex number z and we computed the required values.

Note: We can also use the numpy module to call trigonometric functions.

Here's an example that shows how we can use the numpy module for the trigonometric functions.

Code

import numpy as np

n = 1 #assigning a value to n in radians
result = np.tan(n) #computing the result
print('The tan value of', n, 'is: ', result) 
How to use the numpy module to find the tangent value of number

Explanation

• Line 4: We called the function numpy.tan(n) on the input variable n.
• Line 5: Print the output stored in the result variable.

Note: We can also pass math.pi or numpy.pi as our inputs in the trigonometric functions.

Let's have a look at this example:

Code

import math
import numpy as np

n = math.pi/4 #assigning a value using math module
m = np.pi/4 #assigning a value using numpy module

#computing the results
result_1 = math.cos(n)
result_2 = np.cos(m)

print("The cos value of n = math.pi/4 is: ", result_1)
print("The cos value of m = numpy.pi/4 is: ", result_2) 
How to use the pi value in math and the numpy modules for trigonometric functions

Explanation

• Line 8–9: We have computed the cosine value for the inputs n and m, and stored them in the variables result_1 and result_2.
• Line 11–12: Print the output variables result_1 and result_2.

RELATED TAGS

python3

CONTRIBUTOR

Fatima Numan