Numpy | Mathematical Function - GeeksforGeeks

15 Nov, 2018
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 has standard trigonometric functions which return trigonometric ratios for a given angle in radians.
numpy.sin(x[, out]) = ufunc ‘sin’) : This mathematical function helps user to calculate trignmetric sine for all x(being the array elements).
Output :
Input array : 
 [0, 1.5707963267948966, 1.0471975511965976, 3.141592653589793]

Sine values : 
 [  0.00000000e+00   1.00000000e+00   8.66025404e-01   1.22464680e-16]

 numpy.cos(x[, out]) = ufunc ‘cos’) : This mathematical function helps user to calculate trignmetric cosine for all x(being the array elements).
Output :
Input array : 
 [0, 1.5707963267948966, 1.0471975511965976, 3.141592653589793]

Cosine values : 
 [  1.00000000e+00   6.12323400e-17   5.00000000e-01  -1.00000000e+00]

 
Hyperbolic Functions –numpy.sinh(x[, out]) = ufunc ‘sin’) : This mathematical function helps user to calculate hyperbolic sine for all x(being the array elements).
Equivalent to 1/2 * (np.exp(x) - np.exp(-x)) or -1j * np.sin(1j*x).
Output :
Input array : 
 [0, 1.5707963267948966, 1.0471975511965976, 3.141592653589793]

Sine Hyperbolic values : 
 [  0.           2.3012989    1.24936705  11.54873936]

 numpy.cosh(x[, out]) = ufunc ‘cos’) : This mathematical function helps user to calculate hyperbolic cosine for all x(being the array elements).
Equivalent to 1/2 * (np.exp(x) - np.exp(-x)) and np.cos(1j*x)
Output :
Input array : 
 [0, 1.5707963267948966, 1.0471975511965976, 3.141592653589793]

cosine Hyperbolic values : 
 [  1.           2.50917848   1.60028686  11.59195328]

 
Functions for Rounding –numpy.around(arr, decimals = 0, out = None) : This mathematical function helps user to evenly round array elements to the given number of decimals.
Output :
Input array : 
 [0.5, 1.5, 2.5, 3.5, 4.5, 10.1]

Rounded values : 
 [  0.   2.   2.   4.   4.  10.]

Input array : 
 [0.53, 1.54, 0.71]

Rounded values : 
 [ 1.  2.  1.]

Input array : 
 [0.5538, 1.33354, 0.71445]

Rounded values : 
 [ 0.554  1.334  0.714]
 numpy.round_(arr, decimals = 0, out = None) : This mathematical function round an array to the given number of decimals.
Output :
Input array : 
 [0.5, 1.5, 2.5, 3.5, 4.5, 10.1]

Rounded values : 
 [  0.   2.   2.   4.   4.  10.]

Input array : 
 [0.53, 1.54, 0.71]

Rounded values : 
 [ 1.  2.  1.]

Input array : 
 [0.5538, 1.33354, 0.71445]

Rounded values : 
 [ 0.554  1.334  0.714]
 
Exponents and logarithms Functions –numpy.exp(array, out = None, where = True, casting = ‘same_kind’, order = ‘K’, dtype = None) : This mathematical function helps user to calculate exponential of all the elements in the input array.
Output :
Input array :  [1, 3, 5]
Output array :  [   2.71828183   20.08553692  148.4131591 ]

 numpy.log(x[, out] = ufunc ‘log1p’) : This mathematical function helps user to calculate Natural logarithm of x where x belongs to all the input array elements.Natural logarithm log is the inverse of the exp(), so that log(exp(x)) = x. The natural logarithm is log in base e.
Output :
Input array :  [1, 3, 5, 256]
Output array :  [ 0.          1.09861229  1.60943791  5.54517744]

np.log(4**4) :  5.54517744448
np.log(2**8) :  5.54517744448

 
Arithmetic Functions –numpy.reciprocal(x, /, out=None, *, where=True) : This mathematical function is used to calculate reciprocal of all the elements in the input array.
Note: For integer arguments with absolute value larger than 1, the result is always zero because of the way Python handles integer division. For integer zero the result is an overflow.
Output :
Input  number :  2.0
Output number :  0.5
 numpy.divide(arr1, arr2, out = None, where = True, casting = ‘same_kind’, order = ‘K’, dtype = None) : Array element from first array is divided by elements from second element (all happens element-wise). Both arr1 and arr2 must have same shape and element in arr2 must not be zero; otherwise it will raise an error.
Output :
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 4, 5, 6]

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]

 
Complex number Function –numpy.isreal(array) : 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.
Output :
Is Real :  [False  True] 

Is Real :  [ True  True] 

numpy.conj(x[, out] = ufunc ‘conjugate’) : This function helps the user to conjugate any complex number.The conjugate of a complex number is obtained by changing the sign of its imaginary part. If the complex number is 2+5j then its conjugate is 2-5j.
Output :
Output conjugated complex number of  2+4j :  (2-4j)
Output conjugated complex number of 5-8j:  (5+8j)

 Special functions –numpy.cbrt(arr, out = None, ufunc ‘cbrt’) : This mathematical function helps user to calculate cube root of x for all x being the array elements.
Output :
cbrt Value of arr1 : 
 [  1.  30.   4. -10.]

cbrt Value of arr2 :  [ 10.0793684  -5.0396842]

 numpy.clip() : This function is used to Clip (limit) the values in an array.Given an interval, values outside the interval are clipped to the interval edges. For example, if an interval of [0, 1] is specified, values smaller than 0 become 0, and values larger than 1 become 1.
Output :
Input array :  [1, 2, 3, 4, 5, 6, 7, 8]
Output array :  [2 2 3 4 5 6 6 6]
 
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.