Data Science and Computing with Python for Pilots and Flight Test Engineers
Basic Mathematics
In this lesson we learn how to represent basic mathematical operations, which you know from high school, in Python code.
import numpy as np
Simple Arithmetic Operations on Real Numbers
# Simple arithmetic operations on real numbers:
# Adding two numbers:
a = 2.0 + 3.0
# Subtracting two numbers:
b = 3.0 - 1.0
# Multiplying two numbers:
c = 2.0 * 3.0
# Dividing two numbers:
d = 5.0 / 2.0
# Whole number division:
e = 5 // 2
# Remainder of whole number division:
f = 5 % 2
print(a, b, c, d, e, f)
Exponentiation, Roots, and Logarithms
# Exponentiation, e.g. squaring two numbers (and analogously for higher exponents):
a = 10.0**2
# Taking the square root:
b = np.sqrt(9.0)
# n-th root:
n = 3.0
c = 16.0**(1.0/n)
# Exponential function e^x (where e is the Euler number):
d = np.exp(5.0)
# Natural logarithm ln (base e) (this is the inverse function of the exponential function, i.e. x = log(exp(x)):
e = np.log(5.0)
# Logarithm base 10:
f = np.log10(5.0)
# Logarithm base 2:
g = np.log2(5.0)
# Number pi:
h = np.pi
print(a, b, c, d, e, f, g, h)
Trigonometric Functions
# Trigonometric functions (angle in radians, i.e. 360 degrees is represented by 2*np.pi):
a = np.sin(np.pi/2.0)
b = np.cos(np.pi)
c = np.tan(np.pi/4.0)
# Inverse trigonometric funtions:
d = np.arcsin(1.0)
e = np.arccos(1.0)
# Regular arctan:
f_a = np.arctan(1.0)
f_b = np.arctan(-1.0)
# "Four quadrant arctan":
f1 = np.arctan2(1.0, 1.0)
f2 = np.arctan2(-1.0, 1.0)
f3 = np.arctan2(1.0, -1.0)
f4 = np.arctan2(-1.0, -1.0)
# Note: np.arctan() takes only one argument x/y, while np.arctan2() takes two arguments (x, y),
# the second being the denominator of the definition of the tangent.
# arctan2() has the benefit of returning the proper value throughout all 360 deg. This is not the case for the
# regular tangent function arctan(), which takes only one argument, x/y, and can thus not distinguish,
# for instance, between -1/1 and 1/-1, as well as 1/1 and -1/-1.
print(a, b, c)
print(d, e)
print(f_a, f_b)
print(f1, f2, f3, f4)
Copy and paste the above code cells into your Jupyter notebook on your computer and run them.