Trigonometric Functions#
Overview#
The trigonometric functions for sine, cosine, and tangent (and associated inverse functions) are built-in functions in Python as well as part of a common Python package numpy
Using the math
and cmath
module#
The math
and cmath
modules are part of the Python Standard Library and require no additional downloading to use. Both math modules can be used to calculate the value of trigonometric functions in Python
Both math modules expect an input value in radians:
Input: single numerical values (float, decimals, integers)
Returns: single numerical value in radians as a float
The math
function only works on real number inputs, while cmath
contains equivalent functions for for complex and imaginary numbers
However, if you are working with lists or arrays we suggest using the numpy
equivalents
Recommended: Using numpy
#
numpy
supports both real and complex values of individual numbers (floats, decimals, integers) as well as array-like values (arrays, lists, etc…)
numpy
expects an input value in radians:
Input: single or array-like numerical values (floats, integers, arrays, lists)
Returns: single or array-like numerical values in radians as floats
sin#
sin
calculates the sine of numeric values in radians
Grab and Go#
import math
input_value = 0.5 # in radians
math_sin_value = math.sin(input_value)
print(math_sin_value)
0.479425538604203
# Input: Single Value
import numpy as np
input_value = 0.5 # in radians
np_sin_value = np.sin(input_value)
print(np_sin_value)
0.479425538604203
# Input: Array
import numpy as np
input_values = np.array([0.5, 0.75, 1.0]) # in radians
np_sin_array_values = np.sin(input_values)
print(np_sin_array_values)
[0.47942554 0.68163876 0.84147098]
cos#
cos
calculates the cosine of numeric values in radians
Grab and Go#
import math
input_value = 0.5 # in radians
math_cos_value = math.cos(input_value)
print(math_cos_value)
0.8775825618903728
# Input: Single Value
import numpy as np
input_value = 0.5 # in radians
np_cos_value = np.cos(input_value)
print(np_cos_value)
0.8775825618903728
# Input: Array
import numpy as np
input_values = np.array([0.5, 0.75, 1.0]) # in radians
np_cos_array_values = np.cos(input_values)
print(np_cos_array_values)
[0.87758256 0.73168887 0.54030231]
tan#
tan
calculates the tangent of numeric values in radians
Grab and Go#
import math
input_value = 0.5 # in radians
math_tan_value = math.tan(input_value)
print(math_tan_value)
0.5463024898437905
# Input: Single Value
import numpy as np
input_value = 0.5 # in radians
np_tan_value = np.tan(input_value)
print(np_tan_value)
0.5463024898437905
# Input: Array
import numpy as np
input_values = np.array([0.5, 0.75, 1.0]) # in radians
np_tan_array_values = np.tan(input_values)
print(np_tan_array_values)
[0.54630249 0.93159646 1.55740772]
asin#
asin
calculates the arcsine of numeric values in radians
Grab and Go#
import math
input_value = 0.5 # in radians
math_asin_value = math.asin(input_value)
print(math_asin_value)
0.5235987755982989
# Input: Single Value
import numpy as np
input_value = 0.5 # in radians
np_asin_value = np.arcsin(input_value)
print(np_asin_value)
0.5235987755982989
# Input: Array
import numpy as np
input_values = np.array([0.5, 0.75, 1.0]) # in radians
np_asin_array_values = np.arcsin(input_values)
print(np_asin_array_values)
[0.52359878 0.84806208 1.57079633]
acos#
acos
calculates the arccosine of numeric values in radians
Grab and Go#
import math
input_value = 0.5 # in radians
math_acos_value = math.acos(input_value)
print(math_acos_value)
1.0471975511965979
# Input: Single Value
import numpy as np
input_value = 0.5 # in radians
np_acos_value = np.arccos(input_value)
print(np_acos_value)
1.0471975511965979
# Input: Array
import numpy as np
input_values = np.array([0.5, 0.75, 1.0]) # in radians
np_acos_array_values = np.arccos(input_values)
print(np_acos_array_values)
[1.04719755 0.72273425 0. ]
atan#
atan
calculates the arctangent of numeric values in radians
Grab and Go#
import math
input_value = 0.5 # in radians
math_atan_value = math.atan(input_value)
print(math_atan_value)
0.4636476090008061
# Input: Single Value
import numpy as np
input_value = 0.5 # in radians
np_atan_value = np.arctan(input_value)
print(np_atan_value)
0.4636476090008061
# Input: Array
import numpy as np
input_values = np.array([0.5, 0.75, 1.0]) # in radians
np_atan_array_values = np.arctan(input_value)
print(np_atan_array_values)
0.4636476090008061
atan2#
atan
calculates the arctangent of y / x
in radians
import math
input_value_x = 10.0 # in radians
input_value_y = 5.0 # in radian
math_atan2_value = math.atan2(input_value_y, input_value_x)
print(math_atan2_value)
0.4636476090008061
# Input: Single Value
import numpy as np
input_value_x = 10.0 # in radians
input_value_y = 5.0 # in radian
np_atan2_value = np.arctan2(input_value_y, input_value_x)
print(np_atan2_value)
0.4636476090008061
# Input: Array
import numpy as np
input_values_x = np.array([0.5, 0.75, 10.0]) # in radians
input_values_y = np.array([1.5, 1.75, 5.0]) # in radians
np_atan2_array_values = np.arctan2(input_values_y, input_values_x)
print(np_atan2_array_values)
[1.24904577 1.16590454 0.46364761]
cosh#
cosh
computes the hyperbolic cosine in radians
import math
input_value = -3.2 # in radians
math_cosh_value = math.cosh(input_value)
print(math_cosh_value)
12.28664620054386
# Input: Single Value
import numpy as np
input_value = -3.2 # in radians
np_cosh_value = np.cosh(input_value)
print(np_cosh_value)
12.28664620054386
# Input: Array
import numpy as np
input_values = np.array([0.5, 0.75, -3.2]) # in radians
np_cosh_array_values = np.cosh(input_values)
print(np_cosh_array_values)
[ 1.12762597 1.29468328 12.2866462 ]
sinh#
sinh
computes the hyperbolic sine in radians
import math
input_value = 1.4 # in radians
math_sinh_value = math.sinh(input_value)
print(math_sinh_value)
1.9043015014515339
# Input: Single Value
import numpy as np
input_value = 1.4 # in radians
np_sinh_value = np.sinh(input_value)
print(np_sinh_value)
1.9043015014515339
# Input: Array
import numpy as np
input_values = np.array([0.5, 0.75, 1.4]) # in radians
np_sinh_array_values = np.sinh(input_values)
print(np_sinh_array_values)
[0.52109531 0.82231673 1.9043015 ]
tanh#
tanh
computes the hyperbolic tangent in radians
import math
input_value = 0.67 # in radians
math_tanh_value = math.tanh(input_value)
print(math_tanh_value)
0.5849798828807289
import numpy as np
input_value = 0.67 # in radians
np_tanh_value = np.tanh(input_value)
print(np_tanh_value)
0.5849798828807288
# Input: Array
import numpy as np
input_values = np.array([0.5, 0.75, 0.67]) # in radians
np_tanh_array_values = np.tanh(input_values)
print(np_tanh_array_values)
[0.46211716 0.63514895 0.58497988]
Differences from NCL#
Input and Return Types#
While the math modules (math
and cmath
) only accept single numerical values—like NCL—numpy
can accept and return either single numerical values or array-like values.
Unlike NCL, cmath
and numpy
can also accept complex numbers as input values
Python Resources#
The
math
trig functions module documentationThe
cmath
trig functions module documentationThe
numpy
trig functions documentationConverting between radians and degrees with
numpy
np.rad2deg: Convert angles from radians to degrees
np.deg2rad: Convert angles from degrees to radians