Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Numerical computing

Computers evolved from calculators to solve complicated math problems. Unsurprisingly, the relationship between computers and math has stayed to this day. Computers are simply incredibly good at solving problems that would take ages to do by hand. This is the field of numerical computing: a field of programming centered around doing math on a computer as efficiently as possible.

In this chapter, we will primarily be using the Python language, for its simplicity and its ubiquity in numerical computing. In fact, Python is so frequently used in numerical computing that the “Scientific Python stack” is well-understood to be composed of a set of Python libraries specialized for numerical computing. In this chapter, we will explain how to use these libraries to implement mathematical calculations, solve scientific problems, and make visualizations and graphs.

Using the NumPy library

First, import NumPy, which is often abbreviated as np:

import numpy as np

NumPy is used when working with arrays or lists. These lists can contain any type of object and can be any size. Using NumPy makes creating and manipulating arrays much easier because it provides plenty of prebuilt functionality.

arr_1 = np.array([1, 2, 3])
arr_2 = np.array(['a', 'b', 'c'])

Numpy arrays can also nest arrays within arrays. These shapes can become very complicated so using array.shape can give you the ovaral dimentions of a numpy array.

arr_1 = np.array([1, 2, 3])
arr_2 = np.array([[1, 2, 3]])
arr_3 = np.array([[[1, 2, 3],[4, 5, 6]],[[7, 8, 9], [10, 11, 12]]])
arr_4 = np.array([[1], [2], [3]])
print(f"arr_1 shape: {arr_1.shape}")
print(f"arr_2 shape: {arr_2.shape}")
print(f"arr_3 shape: {arr_3.shape}")
print(f"arr_4 shape: {arr_4.shape}")

NumPy arrays have all the same attributes as normal arrays. Like normal arrays they can be indexed starting at 0.

arr_1 = np.array([1, 2, 3])
arr_2 = np.array([[1, 2, 3]])
arr_3 = np.array([[[1, 2, 3],[4, 5, 6]],[[7, 8, 9], [10, 11, 12]]])
print(f"arr_1 1st index: {arr_1[0]}")
print(f"arr_1 2th index: {arr_1[2]}")
print(f"arr_3 multiIndexed: {arr_3[1][0][1]}")
print(f"arr_3 listIndex: {arr_3[0][1]}")

You can use NumPy to create default arrays, such as an array of zeros or ones.

arr_zeros = np.zeros(4)
arr_ones = np.ones(1)
arr_random_trinary = np.random.randint(0, 3, size=10)
print(f"arr_zeros: {arr_zeros}")
print(f"arr_ones: {arr_ones}")
print(f"arr_random_trinary: {arr_random_trinary}")

NumPy has built-in functions that save the user from having to implement them. Some of the more useful functions include np.max(), np.min(), np.mean(), np.median(), and np.std().

arr_1 = np.array([1, -2, 3, 7, 8, 0, 20, 5, 3])
print(f"arr_1 maxValue: {arr_1.max()}")
print(f"arr_1 minValue: {arr_1.min()}")
print(f"Mean of array: {arr_1.mean()}") 
print(f"Median of array: {np.median(arr_1)}") 
print(f"Standard Deviation of array: {arr_1.std()}")

You can apply basic operations to arrays of similar sizes, such as addition, subtraction, and other arithmetic operations.

arr_1 = np.array([1, 2, 4])
arr_2 = np.array([2, 2, 3])
arr_3 = np.array([[1, 2],[3, 4]])
print(f"(arr_1 + arr_2): {np.add(arr_1, arr_2)}")
print(f"(arr_1 - arr_2): {np.subtract(arr_1, arr_2)}")
print(f"(arr_1 * arr_2): {np.multiply(arr_1, arr_2)}")
print(f"(arr_1 / arr_2): {np.divide(arr_1, arr_2)}")
print(f"(arr_1) + (arr_2): {np.concatenate((arr_1, arr_2), axis=0)}")
print(f"sqrt(arr_1): {np.sqrt(arr_1)}\n")
print(f"Transposed arr_3: \n{arr_3.T}")