NumPy contains a large number of various mathematical operations. NumPy provides standard trigonometric functions, functions for arithmetic operations, handling complex numbers, etc.
Trigonometric Functions
NumPy provides functions like sin(), cos() and tan() to compute trigonometric ratios element-wise for angles in radians.
| FUNCTION | DESCRIPTION |
|---|---|
Computes sine element-wise | |
Computes cosine element-wise | |
| tan() | Compute tangent element-wise. |
| arcsin() | Inverse sine, element-wise. |
| arccos() | Trigonometric inverse cosine, element-wise. |
| arctan() | Trigonometric inverse tangent, element-wise. |
| arctan2() | Element-wise arc tangent of x1/x2 choosing the quadrant correctly. |
| degrees() | Convert angles from radians to degrees. |
| rad2deg() | Convert angles from radians to degrees. |
| deg2rad | Convert angles from degrees to radians. |
| radians() | Convert angles from degrees to radians. |
| hypot() | Given the “legs” of a right triangle, return its hypotenuse. |
| unwrap() | Unwrap by changing deltas between values to 2*pi complement. |
Sine Function
The sine function returns the y-coordinate of a point on the unit circle for a given angle (in radians).
import numpy as np
arr = [0, np.pi/2, np.pi/3, np.pi]
s1 = np.sin(arr)
print("Sine values:\n", s1)
Output
Sine values: [0.00000000e+00 1.00000000e+00 8.66025404e-01 1.22464680e-16]
Explanation:
- arr stores angles in radians.
- np.sin(arr): calculates sine for each angle element-wise.
Cosine Function
The cosine function returns the x-coordinate of a point on the unit circle for a given angle (in radians).
import numpy as np
arr = [0, np.pi/2, np.pi/3, np.pi]
c1 = np.cos(arr)
print("Cosine values:\n", c1)
Output
Cosine values: [ 1.000000e+00 6.123234e-17 5.000000e-01 -1.000000e+00]
Explanation: np.cos(arr): computes cosine values element-wise for all angles.
Hyperbolic Functions
Used to calculate hyperbolic sine, cosine, and tangent.
| FUNCTION | DESCRIPTION |
|---|---|
Hyperbolic sine | |
Hyperbolic cosine | |
| tanh() | Compute hyperbolic tangent element-wise. |
| arcsinh() | Inverse hyperbolic sine element-wise. |
| arccosh() | Inverse hyperbolic cosine, element-wise. |
| arctanh() | Inverse hyperbolic tangent element-wise. |
Hyperbolic Sine
The hyperbolic sine (sinh) function returns the value of the hyperbola-based analogue of the sine function, defined as (e^x-e^(-x))/2.
import numpy as np
arr = [0, np.pi/2, np.pi/3, np.pi]
sh = np.sinh(arr)
print("Hyperbolic sine values:\n", sh)
Output
Hyperbolic sine values: [ 0. 2.3012989 1.24936705 11.54873936]
Explanation: np.sinh(arr) computes the hyperbolic sine for each value element-wise.
Rounding Functions
Used to round numbers to the nearest integer or specified decimals.
| FUNCTION | DESCRIPTION |
|---|---|
| rint() | Round to nearest integer towards zero. |
| fix() | Round to nearest integer towards zero. |
| floor() | Return the floor of the input, element-wise. |
| ceil() | Return the ceiling of the input, element-wise. |
| trunc() | Return the truncated value of the input, element-wise. |
rint() Function
np.rint() rounds each element in an array to the nearest integer, returning a new array with the rounded values.
import numpy as np
arr = [1.2, 2.7, -3.4, -4.6]
print(np.rint(arr))
Output
[ 1. 3. -3. -5.]
Exponents and logarithms Functions
NumPy supports exponential, logarithmic, and power operations element-wise.
| FUNCTION | DESCRIPTION |
|---|---|
e^x element-wise | |
| expm1() | Calculate exp(x) – 1 for all elements in the array. |
| exp2() | Calculate 2**p for all p in the input array. |
| log10() | Return the base 10 logarithm of the input array, element-wise. |
| log2() | Base-2 logarithm of x. |
| log1p() | Return the natural logarithm of one plus the input array, element-wise. |
| logaddexp() | Logarithm of the sum of exponentiations of the inputs. |
| logaddexp2() | Logarithm of the sum of exponentiations of the inputs in base-2. |
Exponential
np.exp() function calculates e^x for each element in an array, where e is approximately equal to 2.718 is the base of natural logarithms.
import numpy as np
arr = [1, 3, 5]
print(np.exp(arr))
Output
[ 2.71828183 20.08553692 148.4131591 ]
Natural Logarithm
The natural logarithm (np.log) computes the logarithm of each element in the array with base e. where 'e' is approximately equal to 2.718.
import numpy as np
arr = [1, 3, 5, 256]
print(np.log(arr))
Output
[0. 1.09861229 1.60943791 5.54517744]
Arithmetic Functions
Arithmetic functions perform basic mathematical operations in Python, such as addition, subtraction, multiplication, and division etc.
| FUNCTION | DESCRIPTION |
|---|---|
| add() | Add arguments element-wise. |
| positive() | Numerical positive, element-wise. |
| negative() | Numerical negative, element-wise. |
| multiply() | Multiply arguments element-wise. |
| power() | First array elements raised to powers from second array, element-wise. |
| subtract() | Subtract arguments, element-wise. |
| true_divide() | Returns a true division of the inputs, element-wise. |
| floor_divide() | Return the largest integer smaller or equal to the division of the inputs. |
| float_power() | First array elements raised to powers from second array, element-wise. |
| mod() | Return the element-wise remainder of division. |
| remainder() | Return element-wise remainder of division. |
| divmod() | Return element-wise quotient and remainder simultaneously. |
Returns the reciprocal (1/x) of each element in an array. | |
performs element-wise division between arrays or numbers. |
Reciprocal Function
np.reciprocal() computes the reciprocal (1/x) of each element in the input array element-wise.
import numpy as np
print(np.reciprocal(2.0))
Output
0.5
Divide Function
numpy.divide(arr1, arr2) performs element-wise division of the first array by the second array.
import numpy as np
arr1 = [2, 27, 2, 21, 23]
arr2 = [2, 3, 4, 5, 6]
print(np.divide(arr1, arr2))
Output
[1. 9. 0.5 4.2 3.83333333]
Complex Number Functions
isreal() Function
numpy.isreal() test element-wise whether it is a real number or not(not infinity or not Not a Number) and return the result as a boolean array.
import numpy as geek
print("Is Real : ", geek.isreal([1+1j, 0j]))
print("Is Real : ", geek.isreal([1, 0]))
Output
Is Real : [False True] Is Real : [ True True]
conj() Function
numpy.conj(x) gives the complex conjugate of a number by changing the sign of its imaginary part.
import numpy as np
c1 = 2+4j
o1 = np.conj(c1)
print ("Output conjugated complex number of 2+4j : ", o1)
c2 =5-8j
o2 = np.conj(c2)
print ("Output conjugated complex number of 5-8j: ", o2)
Output
Output conjugated complex number of 2+4j : (2-4j) Output conjugated complex number of 5-8j: (5+8j)
Special functions
Special functions perform advanced mathematical and numerical operations, such as convolution, element-wise calculations, interpolation, and handling NaN or complex values.
| FUNCTION | DESCRIPTION |
|---|---|
| convolve() | Returns the discrete, linear convolution of two one-dimensional sequences. |
| sqrt() | Return the non-negative square-root of an array, element-wise. |
| square() | Return the element-wise square of the input. |
| absolute() | Calculate the absolute value element-wise. |
| fabs() | Compute the absolute values element-wise. |
| sign() | Returns an element-wise indication of the sign of a number. |
| interp() | One-dimensional linear interpolation. |
| maximum() | Element-wise maximum of array elements. |
| minimum() | Element-wise minimum of array elements. |
| real_if_close() | If complex input returns a real array if complex parts are close to zero. |
| nan_to_num() | Replace NaN with zero and infinity with large finite numbers. |
| heaviside() | Compute the Heaviside step function. |
cbrt() | computes the cube root of each element in an array. |
clip() | limits array values to a min and max. |
Cube Root
numpy.cbrt(x) function helps user to calculate cube root of x for all x being the array elements.
import numpy as np
arr = [1, 27000, 64, -1000]
print(np.cbrt(arr))
Output
[ 1. 30. 4. -10.]
clip() Function
numpy.clip(x, a_min, a_max) this function is used to Clip (limit) the values in an array.
import numpy as np
arr = [1, 2, 3, 4, 5, 6, 7, 8]
print(np.clip(arr, 2, 6))