NumPy Mathematical Functions – Trigonometric, Exponential, Hyperbolic

Free NumPy course with real-time projects Start Now!!

NumPy consists of a large number of built-in mathematical functions that can solve mathematical problems. The in-built math module is imported for mathematical calculations.

It can perform trigonometric operations, rounding functions and can also handle complex numbers. NumPy mathematical functions include methods for exponential, logarithmic, and hyperbolic functions.

Trigonometric Functions in NumPy

NumPy Trigonometric Functions

NumPy contains built-in trigonometric functions that can calculate and return the sine, cosine, and tangent. It returns the values in radian for the given angle. It also includes functions to calculate the inverse of sine, cosine, and tangent.

1. np.sin()- It performs trigonometric sine calculation element-wise.

import numpy as np  
arr = np.array([0, 30, 60, 90, 120, 150, 180]) 
#sine function  
 print(np.sin(arr * np.pi / 180))

Output

[0.00000000e+00 5.00000000e-01 8.66025404e-01 1.00000000e+00
8.66025404e-01 5.00000000e-01 1.22464680e-16]

2. np.cos()- It performs trigonometric cosine calculation element-wise.

import numpy as np  
arr = np.array([0, 30, 60, 90, 120, 150, 180])
#cosine function  
print(np.cos(arr * np.pi / 180))

Output

[ 1.00000000e+00 8.66025404e-01 5.00000000e-01 6.12323400e-17
-5.00000000e-01 -8.66025404e-01 -1.00000000e+00]

3. np.tan()- It performs trigonometric tangent calculation element-wise.

import numpy as np  
arr = np.array([0, 30, 60, 90, 120, 150, 180])
#tangent function  
print(np.tan(arr * np.pi / 180)) 

Output

[ 0.00000000e+00 5.77350269e-01 1.73205081e+00 1.63312394e+16
-1.73205081e+00 -5.77350269e-01 -1.22464680e-16]

4. np.arcsin()- It performs trigonometric inverse sine element-wise.

import numpy as np  
arr = np.array([1,2,3])
#inverse sine function
print(np.arcsin(arr * np.pi / 180)) 

Output

[0.01745418 0.03491368 0.05238383]

5.  np.arccos()- It performs trigonometric inverse of cosine element-wise.

import numpy as np  
arr = np.array([1,2,3])
#inverse cosine function
print(np.arccos(arr * np.pi / 180))

Output

[1.55334215 1.53588265 1.5184125 ]

6. np.arctan()- It performs trigonometric inverse of tangent element-wise.

import numpy as np  
arr = np.array([1,2,3])
#inverse tangent function
print(np.arctan(arr * np.pi / 180))

Output

[0.01745152 0.03489242 0.05231211]

NumPy Hyperbolic Functions

NumPy Hyperbolic Functions

There are functions for calculation of hyperbolic functions which are the analogs of the trigonometric functions. There are functions for the calculation of hyperbolic and inverse hyperbolic sine, cosine, and tangent.

1. np.sinh()- This function returns the hyperbolic sine of the array elements.

import numpy as np  
arr = np.array([30,60,90])
#hyperbolic  sine function
print(np.sinh(arr * np.pi / 180)) 

Output

[0.54785347 1.24936705 2.3012989 ]

2. np.cosh()- This function returns the hyperbolic cosine of the array elements.

import numpy as np  
arr = np.array([30,60,90])
#hyperbolic  cosine function
print(np.cosh(arr * np.pi / 180)) 

Output

[1.14023832 1.60028686 2.50917848]

3. np.tanh()- This function returns the hyperbolic tan of the array elements.

import numpy as np  
arr = np.array([30,60,90])
#hyperbolic  tangent function
print(np.tanh(arr * np.pi / 180)) 
 

Output

[0.48047278 0.78071444 0.91715234]

4. np.arcsinh()- This function returns the hyperbolic inverse sine of the array elements.

import numpy as np  
arr = np.array([150,60,90])
#hyperbolic inverse sine function
print(np.arcsinh(arr * np.pi / 180)) 

Output

[1.69018317 0.91435666 1.23340312]

5. np.arccosh()- This function returns the hyperbolic inverse cosine of the array elements.

import numpy as np  
arr = np.array([150,60,90])
#hyperbolic inverse cosine function
print(np.arccosh(arr * np.pi / 180)) 

Output

[1.61690509 0.30604211 1.02322748]

6. np.arctanh()- This function returns the hyperbolic inverse tan of the array elements.

import numpy as np  
arr = np.array([1,2,3])
#hyperbolic inverse tangent function
print(np.arctanh(arr * np.pi / 180)) 

Output

[0.01745507 0.03492077 0.05240781]

NumPy Rounding Functions

Rounding Functions in NumPy

There are various rounding functions that are used to round decimal float numbers to a particular precision of decimal numbers. Let’s discuss them below:

1. np.around()- This function is used to round off a decimal number to desired number of positions. The function takes two parameters: the input number and the precision of decimal places.

import numpy as np  
arr = np.array([20.8999,67.89899,54.63409])  
print(np.around(arr,1))

Output

[20.9 67.9 54.6]

2. np.floor()- This function returns the floor value of the input decimal value. Floor value is the largest integer number less than the input value.

import numpy as np  
arr = np.array([20.8,67.99,54.09])  
print(np.floor(arr))

Output

[20. 67. 54.]

3. np.ceil()- This function returns the ceiling value of the input decimal value. Ceiling value is the smallest integer number greater than the input value.

import numpy as np  
arr = np.array([20.8,67.99,54.09])  
print(np.ceil(arr))

Output

[21. 68. 55.]

NumPy Exponential and Logarithmic Functions

1. np.exp()- This function calculates the exponential of the input array elements.

import numpy as np  
arr = np.array([1,8,4])
#exponential function
print(np.exp(arr))

Output

[2.71828183e+00 2.98095799e+03 5.45981500e+01]

2. np.log()- This function calculates the natural log of the input array elements. Natural Logarithm of a value is the inverse of its exponential value.

import numpy as np  
arr = np.array([6,8,4])  
#logarithmic funtion
print(np.log(arr))

Output

[1.79175947 2.07944154 1.38629436]

NumPy Complex Functions

1. np.isreal()- This function returns the output of a test for real numbers.

import numpy as np  
arr = np.array([1+2j])  
#real test funtion
print(np.isreal(arr))

Output

[False]

2. np.conj()- This function is useful for calculation of conjugate of complex numbers.

import numpy as np  
arr = np.array([1+2j])  
#conjugate funtion
print(np.conj(arr))
 

Output

[1.-2.j]

Summary

The math module contains a wide range of NumPy functions functions for mathematical operations. Python supports all types of math functions. It is very useful for creating programs with complex calculations. This module further increases the capabilities of NumPy.

If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *