NumPy Mathematical Functions
In this tutorial, we will explore a variety of mathematical functions that Numpy offers, along with examples demonstrating their usage. Let’s dive in and see how these functions can enhance your data manipulation and analysis tasks.
Trigonometric Functions
Function | Description |
sin(x, /[, out, where, casting, …]) | Compute the sine of each element in the input array, element-wise. |
cos(x, /[, out, where, casting, …]) | Compute the cosine of each element in the input array, element-wise. |
tan(x, /[, out, where, casting, …]) | Compute the tangent of each element in the input array, element-wise. |
arcsin(x, /[, out, where, casting, …]) | Compute the inverse sine (arcsine) of each element in the input array, element-wise. |
arccos(x, /[, out, where, casting, …]) | Compute the inverse cosine (arccosine) of each element in the input array, element-wise. |
arctan(x, /[, out, where, casting, …]) | Compute the inverse tangent (arctangent) of each element in the input array, element-wise. |
hypot(x1, x2, /[, out, where, casting, …]) | Compute the hypotenuse of a right triangle given the “legs” x1 and x2. |
arctan2(x1, x2, /[, out, where, casting, …]) | Compute the arctangent of x1/x2 with the correct quadrant, element-wise. |
degrees(x, /[, out, where, casting, …]) | Convert angles from radians to degrees, element-wise. |
radians(x, /[, out, where, casting, …]) | Convert angles from degrees to radians, element-wise. |
unwrap(p[, discont, axis, period]) | Unwrap an array by complementing large deltas with respect to the specified period. |
deg2rad(x, /[, out, where, casting, …]) | Convert angles from degrees to radians, element-wise. |
rad2deg(x, /[, out, where, casting, …]) | Convert angles from radians to degrees, element-wise. |
Compute Tangent – numpy.tan()
The numpy.tan() function computes the tangent of each element in the input array.
import numpy as np # Input array arr = np.array([0, np.pi/4, np.pi/2]) # Compute tangent tan_values = np.tan(arr)
Output
tan_values: [0.00000000e+00 1.00000000e+00 1.63312394e+16]
Inverse Sine – numpy.arcsin()
The numpy.arcsin() function calculates the inverse sine (in radians) for each element in the input array.
import numpy as np # Input array arr = np.array([0, 0.5, 1]) # Compute inverse sine arcsin_values = np.arcsin(arr)
Output
arcsin_values: [0. 0.52359878 1.57079633]
Trigonometric Inverse Cosine – numpy.arccos()
The numpy.arccos() function calculates the inverse cosine (in radians) for each element in the input array.
import numpy as np # Input array arr = np.array([1, 0.5, 0]) # Compute inverse cosine arccos_values = np.arccos(arr)
Output
arccos_values: [0. 1.04719755 1.57079633]
Trigonometric Inverse Tangent – numpy.arctan()
The numpy.arctan() function calculates the inverse tangent (in radians) for each element in the input array.
import numpy as np # Input array arr = np.array([0, 1, np.inf]) # Compute inverse tangent arctan_values = np.arctan(arr)
Output
arctan_values: [0. 0.78539816 1.57079633]
Element-wise Arc Tangent – numpy.arctan2()
The numpy.arctan2() function calculates the element-wise arc tangent of x1/x2 while correctly choosing the quadrant.
import numpy as np # Input arrays x1 = np.array([1, -1, 1]) x2 = np.array([1, 1, -1]) # Compute element-wise arc tangent arctan2_values = np.arctan2(x1, x2)
Output
arctan2_values: [ 0.78539816 -0.78539816 2.35619449]
Convert Angles to Degrees – numpy.degrees()
The numpy.degrees() function converts angles from radians to degrees.
import numpy as np # Input array in radians arr_radians = np.array([0, np.pi/2, np.pi]) # Convert radians to degrees arr_degrees = np.degrees(arr_radians)
Output
arr_degrees: [ 0. 90. 180.]
Convert Angles to Radians – numpy.radians()
The numpy.radians() function converts angles from degrees to radians.
import numpy as np # Input array in degrees arr_degrees = np.array([0, 90, 180]) # Convert degrees to radians arr_radians = np.radians(arr_degrees)
Output
arr_radians: [0. 1.57079633 3.14159265]
Hyperbolic Functions
Function | Description |
sinh(x, /[, out, where, casting, …]) | Compute the hyperbolic sine (sinh) of each element in the input array, element-wise. |
cosh(x, /[, out, where, casting, …]) | Compute the hyperbolic cosine (cosh) of each element in the input array, element-wise. |
tanh(x, /[, out, where, casting, …]) | Compute the hyperbolic tangent (tanh) of each element in the input array, element-wise. |
arcsinh(x, /[, out, where, casting, …]) | Compute the inverse hyperbolic sine (arcsinh) of each element in the input array, element-wise. |
arccosh(x, /[, out, where, casting, …]) | Compute the inverse hyperbolic cosine (arccosh) of each element in the input array, element-wise. |
arctanh(x, /[, out, where, casting, …]) | Compute the inverse hyperbolic tangent (arctanh) of each element in the input array, element-wise. |
Compute Hyperbolic Tangent – numpy.tanh()
The numpy.tanh() function computes the hyperbolic tangent of each element in the input array.
import numpy as np # Input array arr = np.array([0, 0.5, 1]) # Compute hyperbolic tangent tanh_values = np.tanh(arr)
Output
tanh_values: [0. 0.46211716 0.76159416]
Inverse Hyperbolic Sine – numpy.arcsinh()
The numpy.arcsinh() function calculates the inverse hyperbolic sine for each element in the input array.
import numpy as np # Input array arr = np.array([0, 1, 2]) # Compute inverse hyperbolic sine arcsinh_values = np.arcsinh(arr)
Output
arcsinh_values: [0. 0.88137359 1.44363548]
Inverse Hyperbolic Cosine – numpy.arccosh()
The numpy.arccosh() function calculates the inverse hyperbolic cosine for each element in the input array.
import numpy as np # Input array arr = np.array([1, 2, 3]) # Compute inverse hyperbolic cosine arccosh_values = np.arccosh(arr)
Output
arccosh_values: [0. 1.3169579 1.76274717]
Inverse Hyperbolic Tangent – numpy.arctanh()
The numpy.arctanh() function calculates the inverse hyperbolic tangent for each element in the input array.
import numpy as np # Input array arr = np.array([0, 0.5, 0.9]) # Compute inverse hyperbolic tangent arctanh_values = np.arctanh(arr)
Output
arctanh_values: [0. 0.54930614 1.47221949]
Functions for Rounding
Function | Description |
round(a[, decimals, out]) | Round each element in the array ‘a’ to the given number of ‘decimals’ places. |
around(a[, decimals, out]) | Round each element in the array ‘a’ to the given number of ‘decimals’ places, using “round half to even” rounding. |
rint(x, /[, out, where, casting, order, …]) | Round elements of the array ‘x’ to the nearest integer. |
fix(x[, out]) | Round each element in the array ‘x’ to the nearest integer towards zero (truncate). |
floor(x, /[, out, where, casting, order, …]) | Compute the floor (round down) of each element in the array ‘x’. |
ceil(x, /[, out, where, casting, order, …]) | Compute the ceiling (round up) of each element in the array ‘x’. |
trunc(x, /[, out, where, casting, order, …]) | Compute the truncated value of each element in the array ‘x’, removing the decimal part. |
Round to Nearest Integer Towards Zero – numpy.rint()
The numpy.rint() function rounds the elements of the input array to the nearest integer towards zero.
import numpy as np # Input array arr = np.array([1.1, 2.5, 3.9]) # Round to nearest integer towards zero rint_values = np.rint(arr)
Output
rint_values: [1. 2. 3.]
Round to Nearest Integer Towards Zero – numpy.fix()
The numpy.fix() function rounds the elements of the input array to the nearest integer towards zero.
import numpy as np # Input array arr = np.array([1.1, 2.5, 3.9]) # Round to nearest integer towards zero fix_values = np.fix(arr)
Output
fix_values: [1. 2. 3.]
Return the Floor of the Input – numpy.floor()
The numpy.floor() function returns the largest integer not greater than the input array element-wise.
import numpy as np # Input array arr = np.array([1.1, 2.5, 3.9]) # Return floor of input floor_values = np.floor(arr)
Output
floor_values: [1. 2. 3.]
Return the Ceiling of the Input – numpy.ceil()
The numpy.ceil() function returns the smallest integer not less than the input array element-wise.
import numpy as np # Input array arr = np.array([1.1, 2.5, 3.9]) # Return ceiling of input ceil_values = np.ceil(arr)
Output
ceil_values: [2. 3. 4.]
Return the Truncated Value – numpy.trunc()
The numpy.trunc() function returns the truncated value of the input array element-wise.
import numpy as np # Input array arr = np.array([1.1, 2.5, 3.9]) # Return truncated values trunc_values = np.trunc(arr)
Output
trunc_values: [1. 2. 3.]
Exponents and Logarithms Functions
Function | Description |
exp(x, /[, out, where, casting, …]) | Compute the exponential (e^x) of each element in the input array. |
expm1(x, /[, out, where, casting, …]) | Compute e^x – 1 for each element in the input array. |
exp2(x, /[, out, where, casting, …]) | Compute 2^p for each p in the input array. |
log(x, /[, out, where, casting, …]) | Compute the natural logarithm (base e) of each element in the input array. |
log10(x, /[, out, where, casting, …]) | Compute the base 10 logarithm of each element in the input array. |
log2(x, /[, out, where, casting, …]) | Compute the base 2 logarithm of each element in the input array. |
log1p(x, /[, out, where, casting, …]) | Compute the natural logarithm of (1 + x) for each element in the input array. |
logaddexp(x1, x2, /[, out, where, casting, …]) | Compute the logarithm of the sum of exponentials of ‘x1’ and ‘x2’. |
logaddexp2(x1, x2, /[, out, where, casting, …]) | Compute the logarithm of the sum of exponentials in base-2 of ‘x1’ and ‘x2’. |
Calculate exp(x) – 1 – numpy.expm1()
The numpy.expm1() function calculates exp(x) – 1 for each element in the input array.
import numpy as np # Input array arr = np.array([0, 0.5, 1]) # Calculate exp(x) - 1 expm1_values = np.expm1(arr)
Output
expm1_values: [0. 0.64872127 1.71828183]
Calculate 2**p – numpy.exp2()
The numpy.exp2() function calculates 2**p for each element in the input array.
import numpy as np # Input array arr = np.array([0, 1, 2]) # Calculate 2**p exp2_values = np.exp2(arr)
Output
exp2_values: [1. 2. 4.]
Return the Base 10 Logarithm – numpy.log10()
The numpy.log10() function returns the base 10 logarithm of the input array element-wise.
import numpy as np # Input array arr = np.array([1, 10, 100]) # Return base 10 logarithm log10_values = np.log10(arr)
Output
log10_values: [0. 1. 2.]
Return the Base-2 Logarithm – numpy.log2()
The numpy.log2() function returns the base-2 logarithm of the input array element-wise.
import numpy as np # Input array arr = np.array([1, 2, 4]) # Return base-2 logarithm log2_values = np.log2(arr)
Output
log2_values: [0. 1. 2.]
Returns the Natural Logarithm of One Plus the Input given – numpy.log1p()
The numpy.log1p() function returns the natural logarithm of 1 + x for each element in the input array.
import numpy as np # Input array arr = np.array([0, 0.5, 1]) # Return natural logarithm of 1 + x log1p_values = np.log1p(arr)
Output
log1p_values: [0. 0.40546511 0.69314718]
Logarithm of the Sum of Exponentiations – numpy.logaddexp()
The numpy.logaddexp() function calculates the logarithm of the sum of exponentiations of the inputs.
import numpy as np # Inputs x = np.array([1, 2, 3]) y = np.array([0.5, 1, 1.5]) # Logarithm of the sum of exponentiations logaddexp_values = np.logaddexp(x, y)
Output
logaddexp_values: [1.31326169 2.12692801 3.20951501]
The logarithm of the Sum of Exponentiations in Base-2 – numpy.logaddexp2()
The numpy.logaddexp2() function calculates the logarithm of the sum of exponentiations in base-2.
import numpy as np # Inputs x = np.array([1, 2, 3]) y = np.array([0.5, 1, 1.5]) # Logarithm of the sum of exponentiations in base-2 logaddexp2_values = np.logaddexp2(x, y)
Output
logaddexp2_values: [1.5849625 2.32192809 3.169925 ]
Whether you’re a data scientist exploring trends in a massive dataset, an engineer simulating physical systems, or a researcher solving mathematical problems, NumPy’s functions have your back.
Conclusion
In this blog, we’ve only scratched the surface of the myriad mathematical functions NumPy has to offer. To fully harness their power, one must continue exploring, experimenting, and applying them to real-world challenges.
As you continue your journey in the world of Python programming and data analysis, remember that NumPy’s mathematical functions are your trusty companions, ready to assist you in unravelling the mysteries of the mathematical universe and turning data into knowledge.
Remember to visit TechVidvan for more insightful tutorials and resources on data manipulation, analysis, and more. Happy coding with Numpy!