Curriculum
Course: NumPy
Login

Curriculum

NumPy

Text lesson

ufunc Trigonometric

Trigonometric Functions

NumPy offers the ufuncs sin(), cos(), and tan() that accept values in radians and return the corresponding sine, cosine, and tangent values.

Example

Calculate the sine value of π/2.

import numpy as np

x = np.sin(np.pi/2)

print(x)

Example

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)

Convert Degrees Into Radians

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}.

Example

Convert all the values in the given array arr to radians.

import numpy as np

arr = np.array([90180270360])

x = np.deg2rad(arr)

print(x)

Radians to Degrees

Example

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)