Trigonometric Functions#

Warning

This is not meant to be a standalone notebook. This notebook is part of the process we have for adding entries to the NCL Index and is not meant to be used as tutorial or example code.

Functions covered#

NCL code#

; sin
; Adapted from https://www.ncl.ucar.edu/Document/Functions/Built-in/sin.shtml
f = 0.5
sin_f = sin(f)  ; sin_f = 0.4794255
print(sin_f)

; cos
; Adapted from https://www.ncl.ucar.edu/Document/Functions/Built-in/cos.shtml
f = -1.0
cos_f = cos(f)  ; cos_f = 0.5403023
print(cos_f)

; tan
; Adapted from https://www.ncl.ucar.edu/Document/Functions/Built-in/tan.shtml
f = 5.6
tan_f = tan(f)  ; tan_f = -0.8139434
print(tan_f)

; asin
; Adapted from https://www.ncl.ucar.edu/Document/Functions/Built-in/asin.shtml
f = 0.5
asin_f = asin(f) ; asin_f = 0.5235988
print(asin_f)

; acos
; Adapted from https://www.ncl.ucar.edu/Document/Functions/Built-in/acos.shtml
f = -1.0
acos_f = acos(f) ; acos_f = 3.141593
print(acos_f)

; atan
; Adapted from https://www.ncl.ucar.edu/Document/Functions/Built-in/atan.shtml
f = 2.7
atan_f = atan(f) ; atan_f = 1.216091
print(atan_f)

; atan2
; Adapted from https://www.ncl.ucar.edu/Document/Functions/Built-in/atan2.shtml
x = 10
y = 5
atan2_f = atan2(y, x) ; atan2_f = 0.4636476
print(atan2_f)

; cosh
; Adapted from https://www.ncl.ucar.edu/Document/Functions/Built-in/cosh.shtml
f = -3.2
cosh_f = cosh(f) ; cosh_f = 12.28665
print(cosh_f)

; sinh
; Adapted from https://www.ncl.ucar.edu/Document/Functions/Built-in/sinh.shtml
f = 1.4
sinh_f = sinh(f) ; sinh_f = 1.904301
print(sinh_f)

; tanh
; Adapted from https://www.ncl.ucar.edu/Document/Functions/Built-in/tanh.shtml
f = 0.67
tanh_f = tanh(f) ; tanh_f = 0.5849799
print(tanh_f)

Python Functionality#

import math
import numpy as np

results_math = {}
results_np = {}

# sin
f = 0.5
sin_math_f = math.sin(f)
sin_np_f = np.sin(f)
results_math["sin"] = sin_math_f
results_np["sin"] = sin_np_f

# cos
f = -1.0
cos_math_f = math.cos(f)
cos_np_f = np.cos(f)
results_math["cos"] = cos_math_f
results_np["cos"] = cos_np_f

# tan
f = 5.6
tan_math_f = math.tan(f)
tan_np_f = np.tan(f)
results_math["tan"] = tan_math_f
results_np["tan"] = tan_np_f

# asin
f = 0.5
asin_math_f = math.asin(f)
asin_np_f = np.arcsin(f)
results_math["asin"] = asin_math_f
results_np["asin"] = asin_np_f

# acos
f = -1.0
acos_math_f = math.acos(f)
acos_np_f = np.arccos(f)
results_math["acos"] = acos_math_f
results_np["acos"] = acos_np_f

# atan
f = 2.7
atan_math_f = math.atan(f)
atan_np_f = np.arctan(f)
results_math["atan"] = atan_math_f
results_np["atan"] = atan_np_f

# atan2
x = 10.0
y = 5.0
atan2_math_f = math.atan2(y, x)
atan2_np_f = np.arctan2(y, x)
results_math["atan2"] = atan2_math_f
results_np["atan2"] = atan2_np_f

# cosh
f = -3.2
cosh_math_f = math.cosh(f)
cosh_np_f = np.cosh(f)
results_math["cosh"] = cosh_math_f
results_np["cosh"] = cosh_np_f

# sinh
f = 1.4
sinh_math_f = math.sinh(f)
sinh_np_f = np.sinh(f)
results_math["sinh"] = sinh_math_f
results_np["sinh"] = sinh_np_f

# tanh
f = 0.67
tanh_math_f = math.tanh(f)
tanh_np_f = np.tanh(f)
results_math["tanh"] = tanh_math_f
results_np["tanh"] = tanh_np_f

Comparison#

# dictionary of trig functions results
ncl_results = {
    "sin": 0.4794255,
    "cos": 0.5403023,
    "tan": -0.8139434,
    "asin": 0.5235988,
    "acos": 3.141593,
    "atan": 1.216091,
    "atan2": 0.4636476,
    "cosh": 12.28665,
    "sinh": 1.904301,
    "tanh": 0.5849799,
}

for c in ncl_results.keys() & results_math.keys() & results_np.keys():
    print(
        f"{c}: \n\tpython (math):\t{results_math[c]}\n\tpython (numpy):\t{results_np[c]}\n\tncl:\t\t{ncl_results[c]}\n"
    )
    assert math.isclose(
        results_np[c], results_math[c], rel_tol=1e-15
    )  # within 15 decimal points
    assert math.isclose(
        ncl_results[c], results_math[c], rel_tol=1e-06
    )  # within 6 decimal points
    assert math.isclose(
        ncl_results[c], results_np[c], rel_tol=1e-06
    )  # within 6 decimal points
atan: 
	python (math):	1.2160906747839564
	python (numpy):	1.2160906747839564
	ncl:		1.216091

asin: 
	python (math):	0.5235987755982989
	python (numpy):	0.5235987755982989
	ncl:		0.5235988

cosh: 
	python (math):	12.28664620054386
	python (numpy):	12.28664620054386
	ncl:		12.28665

sin: 
	python (math):	0.479425538604203
	python (numpy):	0.479425538604203
	ncl:		0.4794255

tan: 
	python (math):	-0.8139432836897027
	python (numpy):	-0.8139432836897027
	ncl:		-0.8139434

cos: 
	python (math):	0.5403023058681398
	python (numpy):	0.5403023058681398
	ncl:		0.5403023

sinh: 
	python (math):	1.9043015014515339
	python (numpy):	1.9043015014515339
	ncl:		1.904301

atan2: 
	python (math):	0.4636476090008061
	python (numpy):	0.4636476090008061
	ncl:		0.4636476

acos: 
	python (math):	3.141592653589793
	python (numpy):	3.141592653589793
	ncl:		3.141593

tanh: 
	python (math):	0.5849798828807289
	python (numpy):	0.5849798828807288
	ncl:		0.5849799

Differences#

for c in ncl_results.keys() & results_math.keys() & results_np.keys():
    print(f"{c}:")
    print(f"\t{results_math[c] - ncl_results[c]}")
    print(f"\t{results_np[c] - ncl_results[c]}")
atan:
	-3.252160436506557e-07
	-3.252160436506557e-07
asin:
	-2.440170110418194e-08
	-2.440170110418194e-08
cosh:
	-3.7994561399301574e-06
	-3.7994561399301574e-06
sin:
	3.8604203000947024e-08
	3.8604203000947024e-08
tan:
	1.1631029728231823e-07
	1.1631029728231823e-07
cos:
	5.868139751896706e-09
	5.868139751896706e-09
sinh:
	5.014515338519487e-07
	5.014515338519487e-07
atan2:
	9.000806100445402e-09
	9.000806100445402e-09
acos:
	-3.4641020674186507e-07
	-3.4641020674186507e-07
tanh:
	-1.7119271111809553e-08
	-1.7119271222831856e-08