NumPy offers the ufuncs sin(), cos(), and tan() that accept values in radians and return the corresponding sine, cosine, and tangent values.
Calculate the sine value of π/2.
import numpy as np x = np.sin(np.pi/2) print(x) |
Calculate the sine values for all elements in the array arr.
import numpy as np arr = np.array([np.pi/2, np.pi/3, np.pi/4, np.pi/5]) x = np.sin(arr) print(x) |
In NumPy, trigonometric functions use radians by default, but we can also convert between radians and degrees.
Note: Radian values are calculated as π/180×degree values\pi / 180 \times \text{degree values}. |
Convert all the values in the given array arr to radians.
import numpy as np arr = np.array([90, 180, 270, 360]) x = np.deg2rad(arr) print(x) |
Convert all the values in the given array arr to degrees.
import numpy as np arr = np.array([np.pi/2, np.pi, 1.5*np.pi, 2*np.pi]) x = np.rad2deg(arr) print(x) |