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 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
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
-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))
Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!
Output
-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
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
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
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
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
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
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
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
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
NumPy Rounding Functions
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
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
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
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. 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
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
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
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.
Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google