NumPy Tutorial NumPy Statistics NumPy References

NumPy - Mathematical Functions



The NumPy package contains a number of mathematical functions which provides all the functionality required for various mathematical operations. It includes trigonometric functions, arithmetic functions, and functions for handling complex numbers. Below mentioned are the most frequently used mathematical functions.

FunctionDescription
sin() Returns the trigonometric sine of an angle in radians.
cos() Returns the trigonometric cosine of an angle in radians.
tan() Returns the trigonometric tangent of an angle in radians.
arcsin() Returns the arc sine of a value.
arccos() Returns the arc cosine of a value.
arctan() Returns the arc tangent of a value.
around() Rounds to the given number of decimals.
floor() Rounds the given number down to the nearest integer.
ceil() Rounds the given number up to the nearest integer.

Lets discuss these functions in detail:

Trigonometric Functions

The numpy package provides trigonometric functions which can be used to calculate trigonometric ratios for a given angle in radians. Consider the following example.

import numpy as np

Arr = np.array([0, 30, 60, 90])
#converting the angles in radians
Arr = Arr*np.pi/180

print("\nThe sin value of angles:")
print(np.sin(Arr))

print("\nThe cos value of angles:")
print(np.cos(Arr))

print("\nThe tan value of angles:")
print(np.tan(Arr))

The output of the above code will be:

The sin value of angles:
[ 0.         0.5        0.8660254  1.       ]

The cos value of angles:
[  1.00000000e+00   8.66025404e-01   5.00000000e-01   6.12323400e-17]

The tan value of angles:
[  0.00000000e+00   5.77350269e-01   1.73205081e+00   1.63312394e+16]

Similarly, arcsin, arcos, and arctan functions can be used to calculate trigonometric inverse of sin, cos, and tan of the given angles. These functions returns angle values in radians which can be converted into degrees using numpy.degrees() function.

import numpy as np

Arr = np.array([0, 30, 60, 90])
#converting the angles in radians
Arr = Arr*np.pi/180

sinArr = np.sin(Arr)
inv_sinArr = np.arcsin(sinArr)
print("The sin value of angles:")
print(sinArr)
print("The inverse of the sin value (in radians):")
print(inv_sinArr)
print("The inverse of the sin value (in degrees):")
print(np.degrees(inv_sinArr))

cosArr = np.cos(Arr)
inv_cosArr = np.arccos(cosArr)
print("\nThe cos value of angles:")
print(cosArr)
print("The inverse of the cos value (in radians):")
print(inv_cosArr)
print("The inverse of the cos value (in degrees):")
print(np.degrees(inv_cosArr))

tanArr = np.tan(Arr)
inv_tanArr = np.arctan(tanArr)
print("\nThe tan value of angles:")
print(tanArr)
print("The inverse of the tan value (in radians):")
print(inv_tanArr)
print("The inverse of the tan value (in degrees):")
print(np.degrees(inv_tanArr))

The output of the above code will be:

The sin value of angles:
[ 0.         0.5        0.8660254  1.       ]
The inverse of the sin value (in radians):
[ 0.          0.52359878  1.04719755  1.57079633]
The inverse of the sin value (in degrees):
[  0.  30.  60.  90.]

The cos value of angles:
[  1.00000000e+00   8.66025404e-01   5.00000000e-01   6.12323400e-17]
The inverse of the cos value (in radians):
[ 0.          0.52359878  1.04719755  1.57079633]
The inverse of the cos value (in degrees):
[  0.  30.  60.  90.]

The tan value of angles:
[  0.00000000e+00   5.77350269e-01   1.73205081e+00   1.63312394e+16]
The inverse of the tan value (in radians):
[ 0.          0.52359878  1.04719755  1.57079633]
The inverse of the tan value (in degrees):
[  0.  30.  60.  90.]

Rounding Functions

numpy.around() function

The numpy.around() function returns the value rounded to the desired precision. The syntax for using this function is given below:

numpy.around(a, decimals)

a Required. Specify the input array [array_like].
decimals Optional. Specify the decimal places to which the number is to be rounded. The default is 0. If negative integer is provided, then the integer is rounded to position to the left of the decimal point.

The example below shows how to use the numpy.around() function.

import numpy as np

Arr = np.array([0.655, 6.125, 52.978, 167.23])

#rounding the array to 2 decimal points
print("Rounded to 2 decimal places:")
print(np.around(Arr, 2))

#rounding the array to 0 decimal points
print("\nRounded to 0 decimal places:")
print(np.around(Arr))

#rounding the array to -1 decimal points
print("\nRounded to -1 decimal places:")
print(np.around(Arr, -1))

The output of the above code will be:

Rounded to 2 decimal places:
[   0.66    6.12   52.98  167.23]

Rounded to 0 decimal places:
[   1.    6.   53.  167.]

Rounded to -1 decimal places:
[   0.   10.   50.  170.]

numpy.floor() function

The numpy.floor() function returns the floor value of the input data. The floor of the scalar x is the largest integer i, such that i <= x. See the example below:

import numpy as np

Arr = np.array([0.65, 6.56, 52.67, 167.23])
print("Original Array:")
print(Arr)

print("\nFloor value array:")
print(np.floor(Arr))

The output of the above code will be:

Original Array:
[   0.65    6.56   52.67  167.23]

Floor value array:
[   0.    6.   52.  167.]

numpy.ceil() function

The numpy.floor() function returns the ceiling value of the input data. The ceiling of the scalar x is the smallest integer i, such that i >= x. Consider the example below.

import numpy as np

Arr = np.array([0.65, 6.56, 52.67, 167.23])
print("Original Array:")
print(Arr)

print("\nCeiling value array:")
print(np.ceil(Arr))

The output of the above code will be:

Original Array:
[   0.65    6.56   52.67  167.23]

Ceiling value array:
[   1.    7.   53.  168.]