This handbook is an introduction to doing Mathematics with Open Source Software.
One alternative to matlab, mathematica and maple is to use sagemath. Sagemath is a fantastic collection of resources build on top of the open source programming language Python. However it is quite a large piece of software that can be difficult to install on Windows. There is a cloud based system cloud.sagemath.com but this requires a connection to the internet. Instead this notebook will describe the use of 3 lightweight Python libraries:
Python and the above libraries are all free to use (and potentially change if you needed to!). The recommended distribution of Python that includes a number of libraries for scientific work is Anaconda. If you have not yet installed Python on your machine go to the following link: https://www.continuum.io/downloads and follow the download instructions.
Note that there is a choice between python 3 and python 2. Python 3 is recommended and assumed in this handbook.
Once you have installed Anaconda, you will now have Python on your machine. You can interact with Python in multiple ways:
On Windows open a "Command Prompt":
On Mac OS or Linux open a "Terminal":
This is a simple utility that allows you to give commands to your computer. In there type:
python
This should then look something like:
Python 3.5.2 |Anaconda 4.2.0 (64-bit)| (default, Jul 2 2016, 17:53:06)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
The >>>
is a prompt for you to type a command. Let us carry out a straightforward addition:
>>> 2 + 2
and press ENTER
:
>>> 2 + 2
4
This is not a very efficient and practical way of using Python. We will instead learn to use a Jupyter notebook.
If you are still in the Python shell (with a >>>
waiting for you to give a command) then type:
>>> exit()
This will leave the prompt and you will now be back at the Command Prompt or the Terminal window. Here simply type:
jupyter notebook
After a little moment this will open a Jupyter notebook page in a browser. Note that this is all just running on your computer and you do not need to be connected to the internet to use a Jupyter notebook.
In your browser window you should have a tab that looks like the following:
You can use that to navigate your directories/folders. When you are in a location that you want to use, create a new notebook by clicking on New
(on the top right) and Python ...
.
These notebook are very versatil and powerful. You can write blocks of code in each cell and see the output. To execute code in a cell:
SHIFT + ENTER
.Here is an example:
EXERCISE: Create a cell and run the following:
2 + 2
It is also possible to write text with a markup language called markdown which can also include LaTeX. To do this you need to change the mode of a cell. There is a dropdown menu that lets you choose between:
EXERCISE: Create a markdown cell and type the following:
Here is a simple list of topics we will cover in this handbook:
- Symbolic mathematics
- Linear algebra
- Basic data analysis
Note that I can also use LaTeX:
$$a x ^ 2 + b x + c$$
We have gone through an overview of using Jupyter, we will now move on to basic Python syntax.
Python is a fully fledged professional programming language. Used across a wide number of sectors:
It is not the intention of this handbook to give a strong grounding in Python as a programming language. There are a number of great resources for this which you can find here http://docs.python-guide.org/en/latest/intro/learning/
We will go over some of the very basics here which if you have never used Python before will be enough to get you started.
We can assign values to variables for future use:
# Anything after a `#` will be ignored. This is what we call a 'comment'
a = 20 # Assigning a value to a
b = 21 # Assigning a value to b
a * b # Calculating the product of a and b
420
a / (a + b) # Calculating a / (a + b)
0.4878048780487805
a ** b # Calculating a raised to the power of b
2097152000000000000000000000
EXERCISE For $a=15$ and $b=16$, use Python to find the values of the following:
Similar to variables we can create functions that can be easily reused.
def proportion(a, b):
"""
We can use triple " to describe what our function does.
Here for example: we're creating a function to calculate
the proportion of a of a + b
"""
return a / (a + b)
We can read our description by typing:
proportion?
We can use our function by passing it variables:
proportion(20, 21)
0.4878048780487805
proportion(3, 1)
0.75
EXERCISE Create a function exponent that raises $a$ to the power of $a + b$ and use it calculate:
We can use a for
loop to repeat bits of code with Python. For example the following will calculate:
total = 0
for i in range(10):
total = total + i
total
45
There are a couple of things happening there:
total
and assining it to have value 0
.range(10)
which is a Python function to get a generator of numbers from 0
(inclusive) to 10
(exclusive).for loop
simply repeats all the block of code that is indented.total = total + i
so we are incrementally adding i
to total
.We know that:
$$\sum_{i=0}^Ni=\frac{N (N + 1)}{2}$$Let us verify this in our given case:
N = 9
expected_sum = N * (N + 1) / 2
total == expected_sum
True
The double ==
is used to check if two values are equal and creates what is called a Boolean
variable.
EXERCISE For $N\in\{10, 20, 352\}$ verify the following identity:
$$ \sum_{i=0}^Ni^2=\frac{N(N + 1)(2N + 1)}{6} $$Bonus points if you use a function (or two!) :)
This section has discussed:
There are a number of other things that can be done in Python but this will do for now.
Let us move on to using [Sympy](01 - Symbolic mathematics with Sympy.ipynb) for carrying out symbolic mathematical calculations.