#!/usr/bin/env python # coding: utf-8 # # 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](http://www.sagemath.org/). Sagemath is a fantastic collection of resources build on top of the open source programming language [Python](https://www.python.org/). 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](https://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](01 - Symbolic mathematics with Sympy.ipynb) for carrying out symbolic mathematical calculations; # 2. [Numpy](02 - Linear algebra with Numpy.ipynb) for dealing with large numeric arrays: perfect for linear algebra; # 3. [Pandas](03 - Data analysis with Pandas.ipynb) 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](./img/01-00_command_prompt_on_windows.png) # # - On Mac OS or Linux open a "Terminal": # # ![Image of a terminal Window](./img/01-01_terminal_on_linux.png) # # This is a simple utility that allows you to give commands to your computer. In there type: # # ```bash # python # ``` # # This should then look something like: # # ```python # 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: # # # ```python # >>> 2 + 2 # ``` # # and press `ENTER`: # # # ```python # >>> 2 + 2 # 4 # ``` # # **This is not a very efficient and practical way of using Python.** We will instead learn to use a [Jupyter notebook](http://jupyter.org/). # # ### Jupyter notebooks # # If you are still in the Python shell (with a `>>>` waiting for you to give a command) then type: # # ```python # >>> exit() # ``` # # This will leave the prompt and you will now be back at the Command Prompt or the Terminal window. Here simply type: # # ```bash # 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](img/01-02_jupyter_home.png) # # 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](img/01-03_command_in_jupyter_cell.png) # # --- # # **EXERCISE**: Create a cell and run the following: # # ```python # 2 + 2 # ``` # --- # # #### Writing text/mathematics in a Jupyter notebook # # It is also possible to write text with a markup language called [markdown](https://daringfireball.net/projects/markdown/basics) 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 # In[2]: a / (a + b) # Calculating a / (a + b) # In[3]: a ** b # Calculating a raised to the power of b # --- # **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]: get_ipython().run_line_magic('pinfo', 'proportion') # We can **use** our function by passing it variables: # In[6]: proportion(20, 21) # In[7]: proportion(3, 1) # --- # # **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 # There are a couple of things happening there: # # 1. We're creating a variable `total` and assining it to have value `0`. # 2. We are using `range(10)` which is a Python function to get a generator of numbers from `0` (inclusive) to `10` (exclusive). # 3. The `for loop` simply repeats all the block of code that is indented. # 4. The code that is repeated is `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: # In[9]: N = 9 expected_sum = N * (N + 1) / 2 total == expected_sum # 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](01 - Symbolic mathematics with Sympy.ipynb) for carrying out symbolic mathematical calculations.