NumPy: Trigonometric functions (sin, cos, tan, arcsin, arccos, arctan)
In NumPy, you can calculate trigonometric functions (sin, cos, tan) and inverse trigonometric functions (arcsin, arccos, arctan) for each element in the array (ndarray
).
- Pi (π):
np.pi
- Common topics for trigonometric functions in NumPy
- Angle conversion between radians and degrees
- Sine and inverse sine:
np.sin()
,np.arcsin()
- Cosine and inverse cosine:
np.cos()
,np.arccos()
- Tangent and inverse tangent:
np.tan()
,np.arctan()
,np.arctan2()
- Difference between
np.arctan()
andnp.arctan2()
Trigonometric functions can also be calculated using the standard library math
module in Python.
In addition to trigonometric functions, NumPy also provides hyperbolic functions such as np.sinh()
, np.cosh()
, np.tanh()
, np.arcsinh()
, np.arccosh()
, and np.arctanh()
.
The NumPy version used in this article is as follows. Note that functionality may vary between versions.
import numpy as np
print(np.__version__)
# 1.26.1
Pi (π): np.pi
Pi (π) is defined as np.pi
.
print(np.pi)
# 3.141592653589793
Common topics for trigonometric functions in NumPy
Target types
While np.radians()
is used here as an example, the same applies to other functions such as np.sin()
.
Specifying a scalar value as an argument returns a scalar value.
print(np.radians(180))
# 3.141592653589793
print(type(np.radians(180)))
# <class 'numpy.float64'>
When a NumPy array (ndarray
) is specified, it returns an ndarray
with operations applied element-wise.
a = np.array([0, 90, 180])
print(type(a))
# <class 'numpy.ndarray'>
print(np.radians(a))
# [0. 1.57079633 3.14159265]
print(type(np.radians(a)))
# <class 'numpy.ndarray'>
Array-like objects such as lists, as well as ndarray
, can be specified as inputs. It is important to note that the return type is always an ndarray
.
l = [0, 90, 180]
print(type(l))
# <class 'list'>
print(np.radians(l))
# [0. 1.57079633 3.14159265]
print(type(np.radians(l)))
# <class 'numpy.ndarray'>
For details on converting between ndarray
and lists, see the following article.
Round values
Errors may occur in the calculation of trigonometric functions due to the approximation of the irrational number pi.
To round values, use np.round()
. Specify the number of decimal places as the second argument (default is 0
).
a = np.array([0, 90, 180])
print(np.radians(a))
# [0. 1.57079633 3.14159265]
print(np.round(np.radians(a)))
# [0. 2. 3.]
print(np.round(np.radians(a), 2))
# [0. 1.57 3.14]
Here is an example illustrating errors in the calculation of trigonometric functions.
print(np.sin(np.radians(30)))
# 0.49999999999999994
print(np.round(np.sin(np.radians(30)), 1))
# 0.5
While an ndarray
output may display rounded values, the actual values are unchanged.
print(np.sin(np.radians([0, 30, 90])))
# [0. 0.5 1. ]
print(np.sin(np.radians([0, 30, 90]))[1])
# 0.49999999999999994
Settings such as the number of digits output by print()
can be changed with np.set_printoptions()
.
np.set_printoptions(precision=20)
print(np.sin(np.radians([0, 30, 90])))
# [0. 0.49999999999999994 1. ]
np.set_printoptions(precision=8) # reset to default
The following sample codes are executed with the default setting (precision=8
).
Angle conversion between radians and degrees
NumPy expresses angles in radians for trigonometric and inverse trigonometric functions, such as np.sin()
and np.arcsin()
.
Degrees to radians: np.radians()
, np.deg2rad()
You can convert degrees to radians with np.radians()
or np.deg2rad()
. Either method returns the same result.
print(np.radians([0, 90, 180]))
# [0. 1.57079633 3.14159265]
print(np.deg2rad([0, 90, 180]))
# [0. 1.57079633 3.14159265]
Radians to degrees: np.degrees()
, np.rad2deg()
You can convert radians to degrees with np.degrees()
or np.rad2deg()
. Either method returns the same result.
print(np.degrees([0, np.pi / 2, np.pi]))
# [ 0. 90. 180.]
print(np.rad2deg([0, np.pi / 2, np.pi]))
# [ 0. 90. 180.]
Sine and inverse sine: np.sin()
, np.arcsin()
You can calculate sine with np.sin()
, and inverse sine with np.arcsin()
.
print(np.sin(np.radians([0, 30, 90])))
# [0. 0.5 1. ]
print(np.degrees(np.arcsin([0, 0.5, 1])))
# [ 0. 30. 90.]
Cosine and inverse cosine: np.cos()
, np.arccos()
You can calculate cosine with np.cos()
, and inverse cosine with np.arccos()
.
print(np.cos(np.radians([0, 60, 90])))
# [1.000000e+00 5.000000e-01 6.123234e-17]
print(np.round(np.cos(np.radians([0, 60, 90])), 1))
# [1. 0.5 0. ]
print(np.degrees(np.arccos([0, 0.5, 1])))
# [90. 60. 0.]
Tangent and inverse tangent: np.tan()
, np.arctan()
, np.arctan2()
You can calculate tangent with np.tan()
, and inverse tangent with np.arctan()
and np.arctan2()
. More about np.arctan2()
is mentioned later.
print(np.tan(np.radians([0, 45, 90])))
# [0.00000000e+00 1.00000000e+00 1.63312394e+16]
print(np.degrees(np.arctan([0, 1, np.inf])))
# [ 0. 45. 90.]
np.inf
represents infinity.
Difference between np.arctan()
and np.arctan2()
Both np.arctan()
and np.arctan2()
return the inverse tangent, but they differ in the number of arguments and the range of the returned values.
np.arctan(x)
takes one argument and returns "arctan(x)" in radians. The return value ranges from -pi / 2
to pi / 2
(-90 degrees to 90 degrees).
print(np.degrees(np.arctan([-np.inf, -1, 0, 1, np.inf])))
# [-90. -45. 0. 45. 90.]
np.arctan2(y, x)
takes two arguments and returns "arctan(y / x)" in radians. This angle represents the polar angle, defined as the angle between the positive x-axis and the vector from the origin to the point (x, y)
on the polar coordinate plane. The return value ranges from -pi
to pi
(-180 degrees to 180 degrees).
Since angles in the second and third quadrants can be correctly obtained, np.arctan2()
is more appropriate than np.arctan()
when considering the polar coordinate plane. Note that the argument order is y, x
, not x, y
.
print(np.degrees(np.arctan2(-1, 1)))
# -45.0
print(np.degrees(np.arctan2(1, -1)))
# 135.0
You can specify array-like objects such as ndarray
and lists as the first and second arguments. The values are calculated for each corresponding pair of elements.
print(np.degrees(np.arctan2([0, 1, 1, 1, 0],
[0, 1, 0, -1, -1])))
# [ 0. 45. 90. 135. 180.]
print(np.degrees(np.arctan2([0, -1, -1, -1, 0],
[0, 1, 0, -1, -1])))
# [ 0. -45. -90. -135. 180.]
If y
is 0
and x
is negative (indicating the negative x-axis), np.arctan2()
returns pi
(180 degrees). However, when y
is negative zero (-0
), the function returns -pi
(-180 degrees). This distinction is important for precise handling of signs.
print(np.degrees(np.arctan2(0, -1)))
# 180.0
print(np.degrees(np.arctan2(-0.0, -1.0)))
# -180.0
Negative zero can be specified using the floating-point number (float
). The integer (int
) cannot handle negative zero, so -0
is treated as 0
.
print(np.degrees(np.arctan2(-0, -1)))
# 180.0
There are other cases where the sign of the result changes depending on the sign of zero. It might be easier to understand the results by imagining negative zero as a very small negative value.
print(np.degrees(np.arctan2([0.0, -0.0, 0.0, -0.0],
[0.0, 0.0, -0.0, -0.0])))
# [ 0. -0. 180. -180.]
print(np.sin(-0.0))
# -0.0
print(np.arcsin(-0.0))
# -0.0
print(np.tan(-0.0))
# -0.0
print(np.arctan(-0.0))
# -0.0