NumPy Mathematical Functions

Numpy contains a large number of mathematical functions which can be used to perform various mathematical operations. The mathematical functions include trigonometric functions, arithmetic functions, and functions for handling complex numbers. Let's discuss the mathematical functions.

Trigonometric functions

Numpy contains the trigonometric functions which are used to calculate the sine, cosine, and tangent of the different angles in radian.

The sin, cos, and tan functions return the trigonometric ratio for the specified angles. Consider the following example.

Example

Output:

The sin value of the angles 
[0.00000000e+00 5.00000000e-01 8.66025404e-01 1.00000000e+00
 8.66025404e-01 5.00000000e-01 1.22464680e-16]

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

The tangent value of the angles [ 0.00000000e+00  5.77350269e-01  1.73205081e+00  1.63312394e+16
 -1.73205081e+00 -5.77350269e-01 -1.22464680e-16]

On the other hand, arcsin(), arccos(), and arctan() functions return the trigonometric inverse of the specified angles.

The numpy.degrees() function can be used to verify the result of these trigonometric functions. Consider the following example.

Example

Output:

printing the sin values of different angles
[0.        0.5       0.8660254 1.       ]
printing the inverse of the sin
[0.         0.52359878 1.04719755 1.57079633]
printing the values in degrees
[ 0. 30. 60. 90.]

printing the cos values of different angles
[1.00000000e+00 8.66025404e-01 5.00000000e-01 6.12323400e-17]
printing the inverse of the cos
[0.         0.52359878 1.04719755 1.57079633]

printing the values in degrees
[ 0. 30. 60. 90.]

printing the tan values of different angles
[0.00000000e+00 5.77350269e-01 1.73205081e+00 1.63312394e+16]
printing the inverse of the tan
[0.         0.52359878 1.04719755 1.57079633]

printing the values in degrees
[ 0. 30. 60. 90.]

Rounding Functions

The numpy provides various functions that can be used to truncate the value of a decimal float number rounded to a particular precision of decimal numbers. Let's discuss the rounding functions.

The numpy.around() function

This function returns a decimal value rounded to a desired position of the decimal. The syntax of the function is given below.

It accepts the following parameters.

SNParameterDescription
1numIt is the input number.
2decimalsIt is the number of decimals which to which the number is to be rounded. The default value is 0. If this value is negative, then the decimal will be moved to the left.

Consider the following example.

Example

Output:

printing the original array values: [ 12.202   90.2312 123.02    23.202 ]
Array values rounded off to 2 decimal position [ 12.2   90.23 123.02  23.2 ]
Array values rounded off to -2 decimal position [ 10.  90. 120.  20.]

The numpy.floor() function

This function is used to return the floor value of the input data which is the largest integer not greater than the input value. Consider the following example.

Example

Output:

[ 12.  90. 123.  23.]

The numpy.ceil() function

This function is used to return the ceiling value of the array values which is the smallest integer value greater than the array element. Consider the following example.

Example

Output:

[ 13.  91. 124.  24.]





Latest Courses