General Applied Math#

Overview#

This section covers general applied math functions from NCL:

abs/fabs#

NCL’s abs and fabs functions returns the absolute value of numeric data

Grab and Go#

# Built-In Python
input_value = -3.14

abs_output = abs(input_value)
print(abs_output)
3.14
# Numpy
import numpy as np

input_values = [-10, -3.14, -123]

abs_output = np.abs(input_values)
print(abs_output)
[ 10.     3.14 123.  ]

avg#

NCL’s avg function calculate the average of numeric values

Grab and Go#

# Numpy
import numpy as np

input_value = [1, 2, 3]

avg_output = np.average(input_value)
print(avg_output)
2.0
# Numpy
import numpy as np

input_values = [[1, 2, 3], [4, 5, 6]]

avg_output = np.mean(input_values)
print(avg_output)
3.5
# Numpy
import numpy as np

input_values = [[1, 2, 3], [4, 5, 6]]

avg_output = np.mean(input_values, axis=1)
print(avg_output)
[2. 5.]

ceil#

NCL’s ceil function returns the smallest integer value greater than or equal to each input value

Grab and Go#

# Math
import math

input_value = 3.14

ceil_output = math.ceil(input_value)
print(ceil_output)
4
# Numpy
import numpy as np

input_values = [-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0, 3.14]
ceil_output = np.ceil(input_values)
print(ceil_output)
[-1. -1. -0.  1.  2.  2.  2.  4.]

cumsum#

NCL’s cumsum function calculates the cumulative sum

Grab and Go#

# Numpy
import numpy as np

input_values = [1, 2, 3, 4, 5]

cumsum_output = np.cumsum(input_values, axis=0)
print(cumsum_output)
[ 1  3  6 10 15]

decimalPlaces/round#

NCL’s decimalPlaces function truncates or rounds to the number of decimal places specified, while NCL’s round function rounds a float or double variable to the nearest whole number

Grab and Go#

# Built-In Python
input_value = 3.1415926

round_output = round(input_value, 3)
print(round_output)
3.142
# Numpy
import numpy as np

input_values = [-10.353535, 3.1415926, 123.4567]

round_output = np.round(input_values, 3)
print(round_output)
[-10.354   3.142 123.457]
# Math
import math

input_value = 3.1415926

decimal_values = str(input_value).split(".")
truncate_decimal = decimal_values[1][:3]
truncate_output = float(decimal_values[0] + "." + truncate_decimal)
print(truncate_output)
3.141

Differences from NCL#

NCL distinguishes between rounding a decimal value and truncating it as part of the decimalPlaces function

decimalPlaces(3.145, 2, True)  # True = rounds
>> 3.15
decimalPlaces(3.145, 2, False) # False = truncates
>> 3.14

Python includes multiple functions for rounding a value (math.ceil, math.floor, round), but does not typically handle truncating a value. To truncate a decimal value, the decimal needs to be converted to a string first.

The round function returns the value rounded to ndigits after the decimal point.

round(3.141, 2)
>> 3.14
round(3.145, 2)
>> 3.15

Important Note

Python makes use of Banker's Rounding where a value is rounded to the nearest even integer, rather than automatically rounding up or rounding down
round(3.5)
>> 4
round(4.5)
>> 4

See Floating-Point Arithmetic: Issues and Limitations and Built-in Numeric Types for more information

Use math.ceil to round up to the nearest integer

import math
math.ceil(3.14)
>> 4

Use math.floor to round down to the nearest integer

import math
math.floor(3.14)
>> 3

exp#

NCL’s exp function returns the value of e (the base of natural logarithms) raised to the power of the input

Grab and Go#

# Math
import math

input_value = -0.2

exp_output = math.exp(input_value)
print(exp_output)
0.8187307530779818
# Numpy
import numpy as np

input_values = [-0.2, 1, 1.2]

exp_output = np.exp(input_values)
print(exp_output)
[0.81873075 2.71828183 3.32011692]

floor#

NCL’s floor function returns the largest integer value less than or equal to each input value

Grab and Go#

# Math
import math

input_value = 3.14

floor_output = math.floor(input_value)
print(floor_output)
3
# Numpy
import numpy as np

input_values = [-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0, 3.14]
floor_output = np.floor(input_values)
print(floor_output)
[-2. -2. -1.  0.  1.  1.  2.  3.]

get_d2r#

NCL’s get_d2r function returns a constant to convert degrees to radians

Grab and Go#

# Math
import math

d2r = math.pi / 180
print(d2r)
0.017453292519943295
# Numpy
import numpy as np

d2r = np.pi / 180
print(d2r)
0.017453292519943295

Important Note

numpy includes the numpy.deg2rad() function to convert from degrees to radians
# Numpy
import numpy as np

input_degrees = 30  # degrees
print(np.deg2rad(input_degrees))
0.5235987755982988

get_pi#

NCL’s get_pi function returns the value of pi

Grab and Go#

# Math
import math

pi = math.pi
print(pi)
3.141592653589793
# Numpy
import numpy as np

pi = np.pi
print(pi)
3.141592653589793

get_r2d#

NCL’s get_r2d function returns a constant that converts radians to degrees

Grab and Go#

# Math
import math

r2d = 180 / math.pi
print(r2d)
57.29577951308232
# Numpy
import numpy as np

r2d = 180 / np.pi
print(r2d)
57.29577951308232

Important Note

numpy includes the numpy.rad2deg() function to convert from radians to degrees
# Numpy
import numpy as np

input_radians = 0.52  # radians
print(np.rad2deg(input_radians))
29.79380534680281

log#

NCL’s log function calculates the natural log

Grab and Go#

# Math
import math

input_value = 3.6

log_output = math.log(3.6)
print(log_output)
1.2809338454620642
# Numpy
import numpy as np

input_values = [3.5, 3.6, 3.7]

log_output = np.log(input_values)
print(log_output)
[1.25276297 1.28093385 1.30833282]

log10#

NCL’s log10 function calculate the log base 10

Grab and Go#

# Math
import math

input_value = 3.4

log10_output = math.log10(input_value)
print(log10_output)
0.5314789170422551
# Numpy
import numpy as np

input_values = [3.3, 3.4, 3.5]

log10_output = np.log10(input_values)
print(log10_output)
[0.51851394 0.53147892 0.54406804]

max#

NCL’s max function returns the maximum value in an array

Grab and Go#

# Math
import math

input_value = [2.1, 3.2, 4.3, 5.4, 6.5, 7.6, 8.7, 9.8]

max_math_output = max(input_value)
print(max_math_output)
9.8
# Numpy
import numpy as np

input_values = [2.1, 3.2, 4.3, 5.4, 6.5, 7.6, 8.7, 9.8]
max_np_output = np.max(input_values)
print(max_np_output)
9.8

min#

NCL’s min function returns the minimum value in an array

Grab and Go#

# Math
import math

input_value = [2.1, 3.2, 4.3, 5.4, 6.5, 7.6, 8.7, 9.8]

min_math_output = min(input_value)
print(min_math_output)
2.1
# Numpy
import numpy as np

input_values = [2.1, 3.2, 4.3, 5.4, 6.5, 7.6, 8.7, 9.8]
min_np_output = np.min(input_values)
print(min_np_output)
2.1

mod#

NCL’s mod function calculate the remainder of a division

Grab and Go#

# Built-In Python
mod_value = 17 % 3

print(mod_value)
2
# Numpy
import numpy as np

mod_values = np.mod([17, 4], [3, 3])
print(mod_values)
[2 1]

product#

NCL’s product function calculate the product of input values

Grab and Go#

# Math
import math

input_values = [1, 3, 4, 6]
prod_math_output = math.prod(input_values)
print(prod_math_output)
72
# Numpy
import numpy as np

input_values = [1, 3, 4, 6]
prod_np_output = np.prod(input_values)
print(prod_np_output)
72

qsort#

NCL’s qsort function sorts a numeric array

Grab and Go#

# Built-In Python
input_values = [3, 5, 1, 2]
sorted_list = sorted(input_values)
print(sorted_list)
[1, 2, 3, 5]
# Numpy
import numpy as np

input_values = [3, 5, 1, 2]
sort_np_output = np.sort(input_values)
print(sort_np_output)
[1 2 3 5]

sign_matlab#

NCL’s sign_matlab mimics the behavior of the Matlab sign function

1  if the corresponding element of X is greater than zero

0  if the corresponding element of X equals zero

-1 if the corresponding element of X is less than zero
# Numpy
import numpy as np

input_value = -5
sign_value = np.sign(input_value)
print(sign_value)
-1
# Numpy
import numpy as np

input_values = [-5, 0, 5]
sign_value = np.sign(input_values)
print(sign_value)
[-1  0  1]

sqrt#

NCL’s sqrt function calculate the square root

Grab and Go#

# Math
import math

input_value = 3.14

sqrt_output = math.sqrt(input_value)
print(sqrt_output)
1.772004514666935
# Numpy
import numpy as np

input_values = [3.14, 81, 130, 28]

sqrt_output = np.sqrt(input_values)
print(sqrt_output)
[ 1.77200451  9.         11.40175425  5.29150262]

sqsort#

NCL’s sqsort function sorts a string array

Grab and Go#

# Numpy
import numpy as np

input_values = ["mango", "egg", "taco", "apple"]
sort_np_output = np.sort(input_values)
print(sort_np_output)
['apple' 'egg' 'mango' 'taco']
# Built-In Python
input_values = ["mango", "egg", "taco", "apple"]
sorted_list = sorted(input_values)
print(sorted_list)
['apple', 'egg', 'mango', 'taco']

sum#

NCL’s sum function calculate the sum of input values

Grab and Go#

# Built-In Python
input_values = [1, 3, 4, 6]

sum_math_output = sum(input_values)
print(sum_math_output)
14
# Numpy
import numpy as np

input_values = [1, 3, 4, 6]
sum_np_output = np.sum(input_values)
print(sum_np_output)
14

Python Resources#