00-Studying-Mathematics-with-Python

Studying Mathematics with Python

Preface

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:

  1. Sympy for carrying out symbolic mathematical calculations;
  2. Numpy for dealing with large numeric arrays: perfect for linear algebra;
  3. Pandas a brilliant tool for data analysis.

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.

Interacting with Python

Once you have installed Anaconda, you will now have Python on your machine. You can interact with Python in multiple ways:

The Python shell

  • On Windows open a "Command Prompt":

    Image of a command prompt Window

  • On Mac OS or Linux open a "Terminal":

    Image of a terminal Window

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.

Jupyter notebooks

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:

The Jupyter browser window

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:

  • Click on "play" symbol;
  • Press SHIFT + ENTER.

Writing code in a Jupyter cell

Here is an example:

A simple Python command in a Jupyter cell


EXERCISE: Create a cell and run the following:

2 + 2

Writing text/mathematics in a Jupyter notebook

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:

  1. Markdown
  2. Code
  3. Raw NBConvert (not covered in this handbook)
  4. Heading (not covering in this handbook)

EXERCISE: Create a markdown cell and type the following:

markdown
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.

Basic Python

Python is a fully fledged professional programming language. Used across a wide number of sectors:

  • Commercial products like Dropbox, Instagram and others;
  • Research areas like machine learning, astrophysics and others.

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.

Variables

We can assign values to variables for future use:

In [1]:
# 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
Out[1]:
420
In [2]:
a / (a + b)  # Calculating a / (a + b)
Out[2]:
0.4878048780487805
In [3]:
a ** b  # Calculating a raised to the power of b
Out[3]:
2097152000000000000000000000

EXERCISE For $a=15$ and $b=16$, use Python to find the values of the following:

  • $a\times b$
  • $a + b$
  • $a - b$
  • $\frac{a}{a + b}$
  • $a ^ b$

Functions

Similar to variables we can create functions that can be easily reused.

In [4]:
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:

In [5]:
proportion?

We can use our function by passing it variables:

In [6]:
proportion(20, 21)
Out[6]:
0.4878048780487805
In [7]:
proportion(3, 1)
Out[7]:
0.75

EXERCISE Create a function exponent that raises $a$ to the power of $a + b$ and use it calculate:

  • $3 ^ {(3 + 2)}$
  • $2 ^ 6$

Repeating things with for loops

We can use a for loop to repeat bits of code with Python. For example the following will calculate:

$$\sum_{i=0}^9i$$
In [8]:
total = 0
for i in range(10):
    total = total + i
total
Out[8]:
45

There are a couple of things happening there:

We know that:

$$\sum_{i=0}^Ni=\frac{N (N + 1)}{2}$$

Let us verify this in our given case:

In [9]:
N = 9
expected_sum = N * (N + 1) / 2
total == expected_sum
Out[9]:
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!) :)


Summary

This section has discussed:

  • Using Python via the shell and Jupyter;
  • Writing code and markdown in Jupyter;
  • Some basic features of writing Python code:
    • Variables;
    • Functions;
    • For loops.

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 for carrying out symbolic mathematical calculations.