Instead of developing pre-packaged computer programs we should develop libraries of algorithms that can be shared, understood, improved, and used in ad hoc programs. This page presents a few algorithms, implemented in Python. It is perhaps fun to observe how algorithms play an increasingly large role in our moden society. Elon Musk suggests that artificial intelligence algorithms could become a danger to the public. Yuval Noah Hari discusses the role of algorithms, including the ones in your brain, in the book Homo Deus. The joke that the word “algorithm” stems from Al Gore is funny but of course not true, although he promoted the public use of ARPANET, which is now the Internet. Rather, the word algorithm stems from the Persian mathematician Muhammad bin Musa al-Khwarizmi who was born in 780 and worked at the House of Wisdom in Baghdad. Euclid’s Algorithm, probably the earliest known algorithm, is from before that time and attributed to Euclid, born in 335 BC. He devised an algorithm, i.e., systematic approach, for determining the greatest common divisor of two integers.

Python is a free scripting language, similar to Matlab, used in several courses taught by Professor Haukaas at UBC Vancouver. In contrast to compiled languages, such as C++, a Python program compiles during run-time. This makes a scripting language it slower than compiled programs, but it makes experimentation with new code easier. There are two ways to get things done with Python. You can run it as a calculator by entering statements directly in a command prompt window, or you can run input files that you prepare in a text editor. The latter is the preferred approach and several Integrated Developer Environments (IDEs) are available to help you edit and execute Python files, i.e., “.py” files. Python is only useful in engineering applications when certain external packages are added to it. In the courses taught by Professor Haukaas we use three such libraries pf functions; namely, NumPy for vectors, matrices, linear algebra, etc., SciPy for probability distributions, etc., and Matplotlib for plotting. You can install those three packages using PyCharm. To get started:

In PyCharm, select File > New Project and give it a file path and a name

In PyCharm, select Preferences to open the settings dialog box for the project

On the left-hand side of that dialog box, select the Project: (name) tab and specifically the Project Interpreter

Now on the right-hand side, make sure that some version of Python3 is selected as Project Interpreter

In the same dialog box, press + near the bottom to install NumPy, SciPy, and Matplotlib, and then close the dialog box

In PyCharm, right-click on the project at the top of the left-most pane and select New > Python file and give it a name

Select Python code from below and enter it in that file; at any time, right-click anywhere in the file to run it

The following library of algorithms written in Python may get you started. At the top of each file that you store this code in, you should typically include the following packages: “import numpy as np” / “import matplotlib.pyplot as plt” / “from scipy.stats import norm, lognorm, uniform”. Several functions are posted below, and they are called like this: fcns.steepestDescentSearchDirection(), assuming they have been stored in a separate file and imported by “import fileWithFunctions as fcns”.