Trusted answers to developer questions

Fatima Numan

Grokking Modern System Design Interview for Engineers & Managers

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.

**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:

$Sine$ $Cosine$ $Tangent$

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.

The following syntax is used to call any `math`

function:

math.function_name(value)

Let's find the ** **value of a number

We'll then use it in the following way:

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

**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.

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 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

**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.

To use`cmath`

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 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

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.

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

**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:

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

**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

Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

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.

Keep Exploring

Related Courses