What are trigonometric functions in Python?
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:
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
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
We'll then use it in the following way:
Code
import mathn = 1 #assigning a value to n in radiansresult = math.sin(n) #computing the resultprint('The sin value of', n, 'is: ', result)
Explanation
- Line 4: We called the function
math.sin(n)on the input variablen. - Line 5: Print the output stored in the
resultvariable.
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), wherexis 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 mathn = 0.67 #input specifiedprint("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))
Explanation
- Line 4–9: We called the trigonometric functions for the variable
nand computed the required values while printing outputs.
Note: The range of inputs in
asin(),acos()andatan()functions is -1 to 1.
Hyperbolic trigonometric functions
To usecmath module:
import cmath
Syntax
The syntax for calling math module functions.
cmath.function_name(value)
The syntax of some commonly used
| 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 cmathx = 2 #input specifiedy = 1z = complex(x,y) #making the input a complex numberprint("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))
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
xas the real part andyas the imaginary part. - Line 5: We have created a complex number
zby calling thecomplex()function on the variablesxandy. - Line 6–11: We have called the hyperbolic functions for the complex number
zand we computed the required values.
Note: We can also use the
numpymodule 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 npn = 1 #assigning a value to n in radiansresult = np.tan(n) #computing the resultprint('The tan value of', n, 'is: ', result)
Explanation
- Line 4: We called the function
numpy.tan(n)on the input variablen. - Line 5: Print the output stored in the
resultvariable.
Note: We can also pass
math.piornumpy.pias our inputs in the trigonometric functions.
Let's have a look at this example:
Code
import mathimport numpy as npn = math.pi/4 #assigning a value using math modulem = np.pi/4 #assigning a value using numpy module#computing the resultsresult_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)
Explanation
- Line 8–9: We have computed the cosine value for the inputs
nandm, and stored them in the variablesresult_1andresult_2. - Line 11–12: Print the output variables
result_1andresult_2.
Free Resources