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.
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.
import numpy as np arr = np.array([0, 30, 60, 90, 120, 150, 180]) print("\nThe sin value of the angles",end = " ") print(np.sin(arr * np.pi/180)) print("\nThe cosine value of the angles",end = " ") print(np.cos(arr * np.pi/180)) print("\nThe tangent value of the angles",end = " ") print(np.tan(arr * np.pi/180))
Output:
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.
import numpy as np arr = np.array([0, 30, 60, 90]) print("printing the sin values of different angles") sinval = np.sin(arr*np.pi/180) print(sinval) print("printing the inverse of the sin") cosec = np.arcsin(sinval) print(cosec) print("printing the values in degrees") print(np.degrees(cosec)) print("\nprinting the cos values of different angles") cosval = np.cos(arr*np.pi/180) print(cosval) print("printing the inverse of the cos") sec = np.arccos(cosval) print(sec) print("\nprinting the values in degrees") print(np.degrees(sec)) print("\nprinting the tan values of different angles") tanval = np.tan(arr*np.pi/180) print(tanval) print("printing the inverse of the tan") cot = np.arctan(tanval) print(cot) print("\nprinting the values in degrees") print(np.degrees(cot))
Output:
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.
This function returns a decimal value rounded to a desired position of the decimal. The syntax of the function is given below.
numpy.around(num, decimals)
It accepts the following parameters.
SN | Parameter | Description |
---|---|---|
1 | num | It is the input number. |
2 | decimals | It 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.
import numpy as np arr = np.array([12.202, 90.23120, 123.020, 23.202]) print("printing the original array values:",end = " ") print(arr) print("Array values rounded off to 2 decimal position",np.around(arr, 2)) print("Array values rounded off to -1 decimal position",np.around(arr, -1))
Output:
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.
import numpy as np arr = np.array([12.202, 90.23120, 123.020, 23.202]) print(np.floor(arr))
Output:
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.
import numpy as np arr = np.array([12.202, 90.23120, 123.020, 23.202]) print(np.ceil(arr))
Output: