{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Studying Mathematics with Python\n", "\n", "## Preface\n", "\n", "This handbook is an introduction to doing Mathematics with Open Source Software. \n", "\n", "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:\n", "\n", "1. [Sympy](01 - Symbolic mathematics with Sympy.ipynb) for carrying out symbolic mathematical calculations;\n", "2. [Numpy](02 - Linear algebra with Numpy.ipynb) for dealing with large numeric arrays: perfect for linear algebra;\n", "3. [Pandas](03 - Data analysis with Pandas.ipynb) a brilliant tool for data analysis.\n", "\n", "**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.\n", "\n", "**Note** that there is a choice between python 3 and python 2. **Python 3 is recommended and assumed in this handbook.**\n", "\n", "## Interacting with Python\n", "\n", "Once you have installed Anaconda, you will now have Python on your machine. You can interact with Python in multiple ways:\n", "\n", "### The Python shell\n", "\n", "- On Windows open a \"Command Prompt\":\n", "\n", " ![Image of a command prompt Window](./img/01-00_command_prompt_on_windows.png)\n", " \n", "- On Mac OS or Linux open a \"Terminal\":\n", "\n", " ![Image of a terminal Window](./img/01-01_terminal_on_linux.png)\n", " \n", "This is a simple utility that allows you to give commands to your computer. In there type:\n", "\n", "```bash\n", "python\n", "```\n", "\n", "This should then look something like:\n", "\n", "```python\n", "Python 3.5.2 |Anaconda 4.2.0 (64-bit)| (default, Jul 2 2016, 17:53:06) \n", "[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux\n", "Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n", ">>> \n", "```\n", "\n", "The `>>>` is a prompt for you to type a command. Let us carry out a straightforward addition:\n", "\n", "\n", "```python\n", ">>> 2 + 2\n", "```\n", "\n", "and press `ENTER`:\n", "\n", "\n", "```python\n", ">>> 2 + 2\n", "4\n", "```\n", "\n", "**This is not a very efficient and practical way of using Python.** We will instead learn to use a [Jupyter notebook](http://jupyter.org/).\n", "\n", "### Jupyter notebooks\n", "\n", "If you are still in the Python shell (with a `>>>` waiting for you to give a command) then type:\n", "\n", "```python\n", ">>> exit()\n", "```\n", "\n", "This will leave the prompt and you will now be back at the Command Prompt or the Terminal window. Here simply type:\n", "\n", "```bash\n", "jupyter notebook\n", "```\n", "\n", "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.\n", "\n", "In your browser window you should have a tab that looks like the following:\n", "\n", "![The Jupyter browser window](img/01-02_jupyter_home.png)\n", "\n", "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 ...`.\n", "\n", "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:\n", "\n", "- Click on \"play\" symbol;\n", "- Press `SHIFT + ENTER`.\n", "\n", "#### Writing code in a Jupyter cell\n", "\n", "Here is an example:\n", "\n", "![A simple Python command in a Jupyter cell](img/01-03_command_in_jupyter_cell.png)\n", "\n", "---\n", "\n", "**EXERCISE**: Create a cell and run the following:\n", "\n", "```python\n", "2 + 2\n", "```\n", "---\n", "\n", "#### Writing text/mathematics in a Jupyter notebook\n", "\n", "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:\n", "\n", "1. Markdown\n", "2. Code\n", "3. Raw NBConvert (not covered in this handbook)\n", "4. Heading (not covering in this handbook)\n", "\n", "---\n", "\n", "**EXERCISE**: Create a **markdown** cell and type the following:\n", "\n", "```markdown\n", "Here is a simple list of topics we will cover in this handbook:\n", "\n", "- Symbolic mathematics\n", "- Linear algebra\n", "- Basic data analysis\n", "\n", "Note that I can also use LaTeX:\n", "\n", "$$a x ^ 2 + b x + c$$\n", "```\n", "\n", "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We have gone through an overview of using Jupyter, we will now move on to basic Python syntax.\n", "\n", "## Basic Python\n", "\n", "Python is a fully fledged professional programming language. Used across a wide number of sectors:\n", "\n", "- Commercial products like Dropbox, Instagram and others;\n", "- Research areas like machine learning, astrophysics and others.\n", "\n", "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/\n", "\n", "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.\n", "\n", "### Variables\n", "\n", "We can assign values to variables for future use:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "420" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Anything after a `#` will be ignored. This is what we call a 'comment'\n", "a = 20 # Assigning a value to a\n", "b = 21 # Assigning a value to b\n", "a * b # Calculating the product of a and b" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.4878048780487805" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a / (a + b) # Calculating a / (a + b)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2097152000000000000000000000" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a ** b # Calculating a raised to the power of b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "**EXERCISE** For $a=15$ and $b=16$, use Python to find the values of the following:\n", "\n", "- $a\\times b$\n", "- $a + b$\n", "- $a - b$\n", "- $\\frac{a}{a + b}$\n", "- $a ^ b$\n", "\n", "---\n", "\n", "### Functions\n", "\n", "Similar to variables we can create functions that can be easily reused." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "def proportion(a, b):\n", " \"\"\"\n", " We can use triple \" to describe what our function does.\n", " \n", " Here for example: we're creating a function to calculate \n", " the proportion of a of a + b\n", " \"\"\"\n", " return a / (a + b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can read our description by typing:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "proportion?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can **use** our function by passing it variables:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.4878048780487805" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "proportion(20, 21)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.75" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "proportion(3, 1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "\n", "**EXERCISE** Create a function exponent that raises $a$ to the power of $a + b$ and use it calculate:\n", "\n", "- $3 ^ {(3 + 2)}$\n", "- $2 ^ 6$\n", "\n", "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Repeating things with for loops\n", "\n", "We can use a `for` loop to repeat bits of code with Python. For example the following will calculate:\n", "\n", "$$\\sum_{i=0}^9i$$" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "45" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "total = 0\n", "for i in range(10):\n", " total = total + i\n", "total" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are a couple of things happening there:\n", "\n", "1. We're creating a variable `total` and assining it to have value `0`.\n", "2. We are using `range(10)` which is a Python function to get a generator of numbers from `0` (inclusive) to `10` (exclusive).\n", "3. The `for loop` simply repeats all the block of code that is indented.\n", "4. The code that is repeated is `total = total + i` so we are incrementally adding `i` to `total`.\n", "\n", "We know that:\n", "\n", "$$\\sum_{i=0}^Ni=\\frac{N (N + 1)}{2}$$\n", "\n", "Let us verify this in our given case:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "N = 9\n", "expected_sum = N * (N + 1) / 2\n", "total == expected_sum" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The double `==` is used to check if two values are equal and creates what is called a `Boolean` variable.\n", "\n", "---\n", "\n", "**EXERCISE** For $N\\in\\{10, 20, 352\\}$ verify the following identity:\n", "\n", "$$\n", "\\sum_{i=0}^Ni^2=\\frac{N(N + 1)(2N + 1)}{6}\n", "$$\n", "\n", "\n", "Bonus points if you use a function (or two!) :)\n", "\n", "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Summary\n", "\n", "This section has discussed:\n", "\n", "- Using Python via the shell and Jupyter;\n", "- Writing code and markdown in Jupyter;\n", "- Some basic features of writing Python code:\n", " - Variables;\n", " - Functions;\n", " - For loops.\n", " \n", "There are a number of other things that can be done in Python but this will do for now.\n", "\n", "Let us move on to using [Sympy](01 - Symbolic mathematics with Sympy.ipynb) for carrying out symbolic mathematical calculations." ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [conda env:mwp]", "language": "python", "name": "conda-env-mwp-py" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.0" } }, "nbformat": 4, "nbformat_minor": 1 }