Trigonometry is a branch of mathematics that establishes relations between different angles and their directions.
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:
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.
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:
import mathn = 1 #assigning a value to n in radiansresult = math.sin(n) #computing the resultprint('The sin value of', n, 'is: ', result)
math.sin(n)
on the input variable n
.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)
, wherex
is an input in degrees.
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) |
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))
n
and computed the required values while printing outputs.Note: The range of inputs in
asin()
,acos()
andatan()
functions is -1 to 1.
To usecmath
module:
import cmath
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.
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))
In the code provided, we've called the hyperbolic trigonometric functions as mentioned in the table provided.
x
as the real part and y
as the imaginary part. z
by calling the complex()
function on the variables x
and y
. 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.
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)
numpy.tan(n)
on the input variable n
.result
variable.Note: We can also pass
math.pi
ornumpy.pi
as our inputs in the trigonometric functions.
Let's have a look at this example:
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)
n
and m
, and stored them in the variables result_1
and result_2
.result_1
and result_2
.