TExcelTAD
Home › NumPy › ufunc Trigonometric

ufunc Trigonometric

NumPy has ufuncs for all the trigonometric functions: np.sin, np.cos, np.tan and their inverses np.arcsin, np.arccos, np.arctan. They are essential for geometry, physics, signal processing, graphics and navigation.

All of them work in radians, not degrees. A full circle is 2π radians (360°), so π radians = 180°. NumPy provides the constant np.pi.

Sine, cosine and tangent

python · try it yourself
import numpy as np

print(np.sin(np.pi / 2))                 # sin(90 degrees)
angles = np.array([0, np.pi / 6, np.pi / 4, np.pi / 3, np.pi / 2])
print(np.sin(angles).round(4))
print(np.cos(angles).round(4))
print(np.tan(np.pi / 4).round(4))
1.0
[0.     0.5    0.7071 0.866  1.    ]
[1.     0.866  0.7071 0.5    0.    ]
1.0

Converting degrees and radians

np.deg2rad (alias np.radians) and np.rad2deg (alias np.degrees) convert between the two units:

python · try it yourself
import numpy as np

degrees = np.array([0, 30, 90, 180, 270, 360])
radians = np.deg2rad(degrees)
print(radians.round(4))
print(np.rad2deg(radians))
print(np.sin(np.deg2rad(30)).round(4))     # sin(30 degrees) = 0.5
[0.     0.5236 1.5708 3.1416 4.7124 6.2832]
[  0.  30.  90. 180. 270. 360.]
0.5

Inverse functions: finding angles

np.arcsin, np.arccos and np.arctan go the other way: given a ratio, they return an angle (in radians):

python · try it yourself
import numpy as np

ratios = np.array([1, -1, 0.5])
print(np.rad2deg(np.arcsin(ratios)))

# A ramp rises 1 m over 12 m horizontally. What is its angle?
print("ramp angle:", np.rad2deg(np.arctan(1 / 12)).round(2), "degrees")
[ 90. -90.  30.]
ramp angle: 4.76 degrees

For angles from coordinates, prefer np.arctan2(y, x): unlike arctan(y / x), it knows which quadrant the point is in and handles x = 0:

python · try it yourself
import numpy as np

x = np.array([1, -1, -1, 0])
y = np.array([1, 1, -1, 2])
print(np.rad2deg(np.arctan2(y, x)))
[  45.  135. -135.   90.]

Hypotenuse

np.hypot(a, b) returns √(a² + b²), the hypotenuse of a right triangle or the length of a 2-D vector:

python · try it yourself
import numpy as np

print(np.hypot(3, 4))
legs_a = np.array([5, 8, 1])
legs_b = np.array([12, 15, 1])
print(np.hypot(legs_a, legs_b).round(4))
5.0
[13.     17.      1.4142]

Drawing a wave

Because the functions work on whole arrays, plotting a smooth curve takes just a few lines: create many x-values with linspace and apply the function:

python · try it yourself
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 2 * np.pi, 200)
plt.figure(figsize=(6, 3.2))
plt.plot(np.rad2deg(x), np.sin(x), label="sin", color="#4dabcf")
plt.plot(np.rad2deg(x), np.cos(x), label="cos", color="#e07a5f")
plt.axhline(0, color="gray", linewidth=0.5)
plt.xticks([0, 90, 180, 270, 360])
plt.xlabel("angle (degrees)")
plt.legend()
plt.tight_layout()
plt.show()
print("Plotted sin and cos over one full turn")
Plotted sin and cos over one full turn

Example: points on a circle

Sine and cosine turn an angle into a position on a circle: x = r·cos(θ), y = r·sin(θ). Here are the corners of a regular hexagon with radius 2:

python · try it yourself
import numpy as np

theta = np.deg2rad(np.arange(0, 360, 60))
r = 2
points = np.column_stack((r * np.cos(theta), r * np.sin(theta)))
print(points.round(3) + 0.0)       # + 0.0 turns -0.0 into 0.0
[[ 2.     0.   ]
 [ 1.     1.732]
 [-1.     1.732]
 [-2.     0.   ]
 [-1.    -1.732]
 [ 1.    -1.732]]

Note: Results such as np.cos(np.pi / 2) print as 6.123233995736766e-17 instead of 0, because π itself can't be stored exactly. Round before displaying, or compare with np.isclose.

Test yourself

Which unit do NumPy's trigonometric functions expect?

  1. Degrees
  2. Radians
  3. Gradians
  4. Turns

What is np.rad2deg(np.pi)?

  1. 90.0
  2. 180.0
  3. 360.0
  4. 3.14

Why prefer np.arctan2(y, x) over np.arctan(y / x)?

  1. It is faster
  2. It returns degrees
  3. It uses the signs of both arguments to find the correct quadrant
  4. It works only on integers