{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import theme\n", "theme.load_style()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Lesson 2: Introduction to Python\n", "## Using Python in Scientific Computing\n", "\n", "\n", "\n", "\"Creative\n", "\n", "This lecture by Tim Fuller is licensed under the\n", "Creative Commons Attribution 4.0 International License. All code examples are also licensed under the [MIT license](http://opensource.org/licenses/MIT)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This book is intended for students in science/engineering/math and presumes an introductory knowledge of computer programming (be it whatever language). This book *is not* an introduction to computer programming.\n", "\n", "For a complete introduction to IPython Notebooks, see the [IPython Notebook Tutorial](IPython Notebook Tutorial.ipynb)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "# Topics\n", "\n", "- [What is Python](#what_is) \n", "- [How do I use Python?](#how_to_py)\n", "- [Minimal Python Stack](#minimal_stack)\n", " - [numpy](#minimal_py_numpy)\n", " - [matplotlib](#minimal_py_matplotlib)\n", " - [sympy](#minimal_py_sympy)\n", "- [Dive in to Python](#dive_in_to_py)\n", " - [Variable Assignment](#var_assign)\n", " - [Variable Types](#var_types)\n", " - [Program Flow Control](#prog_flow)\n", " - [Indentation](#indentation)\n", " - [Branching](#branching)\n", " - [Looping](#looping)\n", " - [Functions](#functions)\n", " - [Modules](#modules)\n", " - [Numpy](#numpy)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# What is Python?[](#top)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As your read through each section, you will see cells labeled with\n", "\n", "
Try it
\n", "\n", "which are places meant for you to try the new concept introduced. Keep in mind, that these Notebooks are interactive, fee free to modify values, move things, test things out.\n", "\n", "[Python](www.python.org) is an open source, general-purpose language, with hundreds of modules geared toward scientific computing. Some main features of Python are:\n", "\n", " - Object Oriented, Procedural, and Functional programming styles\n", " - Easy to interface with C/C++/Fortran/Java/etc\n", " - Interactive environment\n", " - Becoming the de-facto standard interfacing language for many commercial FE codes\n", " - Clean and simple\n", " - [Duck typed](http://en.wikipedia.org/wiki/Duck_typing)\n", " - Interpretive (no compiling necessary)\n", " - Automatic memory management\n", " \n", "Some relative advantages of Python\n", "\n", " - Ease of programming\n", " - Fast prototyping\n", " - Interfacing with other languages\n", " - Large library of modules and add-on packages\n", " - Open source\n", " \n", "Some relative disadvantages\n", "\n", " - Speed of execution compared to compiled languages\n", " - Programs can become platform/environment dependent\n", " \n", "\n", "There is no way a short tutorial can describe everything in Python (I've been using/developing Python for 10+ years and learn new stuff all the time). But, it will introduce enough concepts to complete the first homework assignment and provide a foundation for future learning." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# How do I use Python?[](#top)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python is synonomous with the programming language, the interactive Python shell, and the interpreter under the hood that actually reads and processes the code. There are several ways to write/interact with Python: the interactive python shell, the IPython notebook, and by interpreting program files containing python statemtns. This is one of Python's relative strengths: it allows users to select the environment that best suits their needs." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The Interactive Python Shell" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The Python shell is a program that reads and executes Python statements as you enter them. It is opened by invoking the following at a command prompt\n", "\n", " $ python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "Try it!
\n", "Before moving, enter a python shell and execute the following\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Interactive Python sessions are fantastic for quick calculations, or testing snippets of code." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## IPython Notebook" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[Ipython Notebooks](http://ipython.org/notebook.html) provide an interactive environment for Python similar to Maple's worksheets or Mathematica's notebooks. It is based on the IPython shell (a modified interactive Python shell). IPython notebook sessions are stored locally as `json` files with a `.ipynb` file extension and are interpreted graphically as a web session using IPython's notebook server. This file is an example of an IPython Notebook file.\n", "\n", "The IPython Notebook server is launched locally by executing the following at the command prompt:\n", "\n", " $ ipython notebook\n", " \n", "This will open a new web browser (or tab in an existing window) in which notebooks can be created/edited/deleted. The fact that you are reading this means you have already launched the notebook server.\n", "\n", "IPython notebooks consist of a collection of text cells and code cells. `code` cells execute Python code in the notebook's kernel. Text cells are labeled `markdown`, `Raw NBConvert`, and `Heading1-6`. Different text cell types display their contents differently. Looking at the toolbar above, can you tell what type of cell this is? (`Markdown`).\n", "\n", "See the full [IPython Tutorial](Lesson01_IPythonNotebookTutorial.ipynb) for more details." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Python Program Files" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Often, it is not convenient to work in an interactive or notebook environment. In those situations, Python statements can be saved in Python program files that are later interpreted and executed by the Python interpreter (this is not strictly true, but is true enough for now). Python program files (usually) have the file extension \"`.py`\", e.g. `baz.py`. \n", "\n", "With the exception of comment lines, every line in a Python program file is considered to be a Python statement and statements are executed in order from the top of the file to the bottom. Any statement proceding a \"`#`\" (until the end of the line) is considered a comment. Python program files can be executed individually by executing the following at a command prompt:\n", "\n", " python filename.py" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "CAUTION
python files that you intend to later be imported should be valid python variable names. i.e., do not use special characters in Python program file names (+, -, /, *, @, $, etc.). More on this later.\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Minimal Software Stack for Scientific Computing in Python[](#top)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The strength of Python for scientific computing lies in the large number of add-on modules on which users can build applications. Many of the most important modules, namely \n", "\n", " - [`numpy`](http://www.numpy.org/‎)\n", " - [`scipy`](http://www.scipy.org/‎)\n", " - [`sympy`](http://www.sympy.org/‎)\n", " - [`matplotlib`](http://www.matplotlib.org/‎)\n", " - [`ipython notebook`](http://www.ipython.org/notebook.html)\n", " \n", "are not part of the standard Python distribution. Historically, users had to install each module (and their dependencies) individually. This painful process can, in large part, be bypassed by using one of the fantastic commercially available Python distributions. I recommend [annoconda](https://store.continuum.io/cshop/anaconda/) or [enthought](https://www.enthought.com/products/epd/‎). Both offer free and paid versions. The free versions offer all the utility that we will need in this book." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## numpy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `numpy` package is the foundation of most scientific computations performed in Python. It is implemented in C/Fortran, so performance is greatly improved over native Python data types. `numpy` provides:\n", "\n", " - the `ndarray` (n dimensinal array) is `numpy`'s primary object\n", " - fast array operations\n", " - large library of linear algebra procedures\n", " - and much, much, more...\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## matplotlib" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`matplotlib` is a 2D and 3D graphics library for generating scientific figures. `matplotlib`:\n", "\n", " - easy to use interface very similar to Matlabs\n", " - support for $\\LaTeX$ labels and text\n", " - publication quality output in most popular formats\n", " - GUI and batch modes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## sympy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`sympy` provides a Computer Algebra System (CAS) for Python. `sympy` is a regular Python module and integrates very well with IPython notebooks." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Dive in to Python[](#top)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Variable Assignment" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Since python is a dynamically interpreted language, a variable's type need not be declared - its type is inferred at the time of assignment. Variables are assigned a value by the assignment operateor \"`=`\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "spam = 4\n", "spam" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "An assignment statement assigns to the variable name on the left of the assignment operator the expression on the right." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", " Reminder: To execute the cell above move focus to the cell and press \n", "
\n", "
shift enter
\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Variable Types" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python supports many `type`s of variables. The most commonly used in this course are `str`, `float`, `int`, `bool`, `list`, `tuple`, and `dict`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Strings" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Strings are groups of one or more characters of type `str` enclosed by single or double quotes" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "string = \"foo\"\n", "type(string)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "String concatenation is by the \"+\" operator" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "post = \" bar\"\n", "string + post" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python's support for string parsing, manipulation, etc. is, in my opinion, one of its greatest strengths." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Numbers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python supports many types of numbers, the most common being reals and integers\n", "\n", "Real numbers are of type `float`" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a = 4." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a = float(4)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a = float(\"4.\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "type(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Integers have type `int`" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "b = 4" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "b = int(4.)\n", "type(b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Be careful when converting strings to integers" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "b = int(\"4.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The above code \"raised\" a `ValueError`. Instead, first convert to float, then integer" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "b = int(float(\"4.\"))\n", "b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Arithmetic is through the standard arithmetic operators" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a = 3.\n", "b = 2.\n", "a + b # addition" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a - b # subtraction" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a * b # multiplication" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a / b # division" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\"**Warning!** by default \"/\" is integer division\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "1 / 2" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a ** 2 # raising to power (note, do not use ^)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Booleans" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python supports a boolean type `bool` with two members `True` and `False`. Python also supports the related `None`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Lists" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lists are mutable container objects of type `list`. Lists are composed of comma separated members enclosed by paired brackets" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a = [1, 2, 3]\n", "type(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lists can be appended to and extended by other lists" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a.append(4)\n", "a.extend([5, 6, 7])\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", " Important: In the cell above a is an object of type list. Put differently, a is an \"instance\" of the list class. The dot . following a above, is a special operator that accesses \"methods\" associated with a. Simplistically, methods are functions that are owned by a class and are accessible only by instances of the class through the dot operator. Classes will be covered more in depth later, it is sufficient for now to recognize the action of the dot operator.\n", "\n", "
\n", "\n", "List members need not be of the same type" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "b = [1, 2, \"spam\", True, [3, 4]]\n", "b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " Attention: matlab and fortran users: in Python, indexing is 0 based\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "first = b[0]\n", "first" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "last = b[-1]\n", "last" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Slices of lists can be accessed" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "c = a[0:3]\n", "c" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Tuples" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Tuples are container objects like lists, but are immutable (not modifiable after creation) and use parenthesis in place of brackets" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a = (1, 2, 3, 4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Once created, immutable objects cannot be modified. For instance, attempting a number to the end of `a` will result in an error" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# a.append(4) # tuples are immutable, so this will result in an error" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Dictionaries" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Like lists and tuples, dictionaries are yet another container object (type: `dict`). Dictionaries consist of `key:value` pairs enclosed by braces. Valid keys are any immutable object (other items can be used, but for now immutable objects will suffice). For example, in the cell below a dictionary `d` is created with key `a` and value `5`. A new `key:value` pair is then added to `d`. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "d = {\"a\": 5}\n", "d[\"b\"] = \"spam\"\n", "d" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "d[\"b\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A `KeyError` is raised when attempting to access a non-existent key" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# d[12]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `dict` constructor can be used to create a dictionary from a list of tuples" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "d = dict([('a', 5), ('b', 'spam'), (0, (1,2,3))])\n", "d" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", " Try it!
Add key:value pairs \"c\": [1,2,3] and \"d\": (3,4,5) to d.\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " Note: dictionaries do not preserve order.\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Program Flow Control" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Indentation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Unlike many other languages that explicitly define the scope of code blocks (opening/closing braces, begin/end keywords, etc.), Python implicitly defines code block scope through indentation\n", "\n", " compound_statement \":\"\n", " code block\n", " \n", "The Python language does not define a standard indentation depth, but the following advice should be followed:\n", "\n", " - Don't mix tabs and spaces\n", " - Spaces are preferred in new code\n", " - Be consistent with indentation\n", " - Use 4 spaces in new code (4 spaces will be used in this class)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "Branching (if, else, etc.)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Branching within a code is handled by `if`, `elif`, and `else` clauses" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "if True:\n", " print \"I'm True!\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a = 4\n", "if a < 3:\n", " print \"Less than 3\"\n", "elif a < 6:\n", " print \"Between 3 and 6\"\n", "else:\n", " print \"Greater than 6\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice that the indentation of the above code defines the scope of each block." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Looping" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python provides two forms of looping: `for` and `while`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### `for` Loops" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`for` loops in Python is one area that took me some time to become accustomed to, in comparison to C or Fortran. In C, Fortran, and many other languages, loops are constructed by incrementing an integer value from a starting value to an ending value. For example, in C\n", "\n", " for(int i=0; i<10; i++){\n", " std::cout << i << \"\\n\";\n", " }\n", " \n", "In Python, `for` loops are performed by iterating through an \"iterable\". Iterables are objects, such as `lists`, `tuples`, and `dict`s, that support iteration. In python, the previous loop could be written:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "for i in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:\n", " print i" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "or," ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "for i in range(10):\n", " print i" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Strings also support iteration. To loop through the values of a string," ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "for letter in \"a very long string\":\n", " print letter" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "Try It!
\n", "Insert a new cell below and write a block of code that loops through integers 1-20 and prints those that are even.\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### `while` Loops" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Similar to `for` loops, while loops iterate until a condition is met." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a = 1\n", "while a < 5:\n", " a += a # lhs += rhs is a shortcut for lhs = lhs + rhs\n", "print a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### List Comprehension" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A list comprehension is a concise way of creating a `list`. Consider the following " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a = []\n", "for i in range(4):\n", " a.append(i ** 2)\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "The range function creates a list, starting from 0 by default, with n elements\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "range(9)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The above code can be simplified to" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a = [i ** 2 for i in range(4)]\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "List comprehensions consist of an opening bracket, expression, a for loop, followed by 1 or more for loops and/or if statements and are ended with a closing bracket." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "Try It!
\n", "Open a cell below and create a list of the cubes of the first 5 integers using 1) a for loop and append statement and 2) a list comprehension.\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Functions contain collections of related Python code. Functions have the following syntax\n", "\n", " def function_name \"(\" [parameter_list] \")\" \":\"\n", " suite\n", "\n", "For example" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def print_hello():\n", " print \"Hello, World!\"\n", "print_hello()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def print_hello_with_args(a, b):\n", " print \"{0} says hello to {1}!\".format(a, b)\n", "print_hello_with_args(\"Fred\", \"Sue\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "The .format string method, in its simplest form, replaces occurrences of {n} with its nth argument. For example\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "\"Mary {0} {3} {2} {1}\".format(\"had\", \"lambs\", \"little\", 5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Functions can return 1 or more objects" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def add(a, b):\n", " c = a + b\n", " return c\n", "add(4, 5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Lambda Functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Another type of function is the `lambda` function, or \"anonymous\" function. `lambda`s allow the creation of functions on the fly" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "f = lambda x: x ** 2\n", "f(4.)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`lambda` functions are useful in many situations and we'll have a chance to use them often throughtout this book.\n", "\n", "
\n", "Try It!
\n", "Write a function that multiplies 2 numbers and returns the result.\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Modules" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python modules are importable files containing Python code. Importing a module in to the current file exposes that modules contents to the current file. The syntax for importing a module takes one of several forms, demonstrated below." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import math\n", "math.pi" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "math.cos(0.)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Modules names can be aliased for convenience with the ``as`` designator" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import math as m\n", "m.pi" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Alternatively, all contents of a module can be imported in to the current namespace" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from math import *\n", "pi" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "However, this can lead to pollution of the current namespace and is not recommended - unless the module was designed to be imported in this way. For example, suppose you do\n", "\n", " from spam import *\n", " from eggs import *\n", " \n", " baz = foo\n", " \n", "from which module did `foo` come from?\n", "\n", "The Python standard library comes with many useful modules. In this course we will also use heavily the nonstandard `numpy`, `sympy`, and `matplotlib` modules.\n", "\n", "
\n", "Try It!
\n", "Import the `os` module and determine your current working directory with its `cwd` method.\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## `numpy`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`numpy` is a powerful Python module that is the defacto standard for scientific computing in Python. It is customary to import `numpy` as" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The power of `numpy` lies in the array object that it provides. `numpy` arrays are similar to Python lists, but are statically typed and contain only one object type. Arrays can be instantiated in a number of ways, \n", "\n", "- from an existing list" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a = np.array([1., 2., 3.])\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- the `numpy.linspace` function" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a = np.linspace(0, 10, 5) # np.linspace takes (start, stop, number of elements)\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- the `numpy.arange` function" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a = np.arange(0, 5, 1.5) # np.arange takes (start, stop, step size)\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And still others." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Linear Algebra with `numpy`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`numpy` provides many common operations from linear algebra to operate on n dimensional arrays. Consider the following arrays" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Define 1 and 2D arrays. 2D arrays are similar to matrices in matlab\n", "a = np.array([3, 2, 6])\n", "b = np.array([1, 1, 7])\n", "M = np.array([[1, 5, 4], [5, 5, 3], [8, 2, 9]])\n", "L = np.array([[3, 1, 5], [5, 7, 2], [1, 0, 3]])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### The transpose of an array" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "np.transpose(M)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `*` operator operates element wise on arrays, so `a * b` will not yield a scalar" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a * b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### The `dot` Product" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Scalar product of two vectors" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "np.dot(a, b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Matrix/Vector multiplication is also through the `dot` method" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "np.dot(M, a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Think of the `dot` method in terms of the indicial summation representation of matrix operations, e.g. matrix/vector multiplication\n", "\n", "$\\{c\\} = [A]\\{b\\} \\Rightarrow c_i = \\sum_jA_{ij}b_{j}$\n", "\n", "would be written\n", "\n", " c = np.dot(A, b)\n", " \n", "Or matrix multiplication,\n", "\n", "$[C] = [A][B] \\Rightarrow C_{ij} = \\sum_m A_{im}B_{mj}$\n", "\n", "is\n", "\n", " C = np.dot(A, B)\n", " \n", "Or the vector/vector (scalar) product\n", "\n", "$c = \\{v\\}\\cdot\\{w\\} = \\sum_i v_i w_i$\n", "\n", "is\n", "\n", " c = np.dot(v, w)\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Inverse of a square matrix" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "np.linalg.inv(M)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Determinant of a matrix" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Determinant of a 2D array\n", "np.linalg.det(M)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Solution of linear systems:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "solve for $\\{x\\}$: $[M]\\{x\\} = \\{b\\}$" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Solution to system of equations (using inv is *very* inefficient)\n", "x = np.dot(np.linalg.inv(M), b)\n", "x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Better to use the `solve` method" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "x = np.linalg.solve(M, b)\n", "x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## `sympy`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`sympy` is a Computer Algebra System implemented in python. `sympy` allows the creation of symbols and functions (among many other objects) and allows algebraic manipulation of those objects." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import sympy as sp\n", "sp.init_printing() # allow pretty math output" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Variables can be designated as `Symbol`s and used in symbolic computation" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "x = sp.Symbol(\"x\")\n", "y, z = sp.symbols(\"y z\")\n", "f = x + y * sp.pi\n", "f" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Values can be substituted in place of symbols" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "f.subs({x: 2, y: 3})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Evaluate the value numerically" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "f.subs({x: 2, y: 3}).evalf()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Calculus with `sympy`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Evaluate the following integral: $\\int_{0}^{2} x \\,dx$" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "expr = sp.integrate(\"x\", (\"x\", 0, 2))\n", "expr" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lets compare with `numpy`'s built in `trapz` method" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "func = lambda x: x\n", "xvals = np.linspace(0, 2, 5)\n", "func_vals = np.array([func(x) for x in xvals])\n", "a = np.trapz(func_vals, x=xvals)\n", "a" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "expr.evalf() == a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Differential Equations with Sympy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Find $u(x)$ for $u'(x) + u(x) = x$, with $u(0) = 3$. Plot the solution on $x\\in[0,10]$" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "x = sp.Symbol(\"x\")\n", "u = sp.Function(\"u\")\n", "\n", "# recast equation so that all terms appear on left hand side\n", "ics = {u(0): 3}\n", "de = sp.diff(u(x), x) + u(x) - x\n", "\n", "gen_sol = sp.dsolve(de, u(x)).rhs\n", "spec_sol = sp.dsolve(de, u(x), ics=ics, simplify=False)\n", "gen_sol" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "spec_sol" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Find the coefficients" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "coeffs = sp.solve([gen_sol.subs(x, 0) - 3], \"C1\")\n", "sol = gen_sol.subs(coeffs)\n", "sol" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we plot with `matplotlib`. `matplotlib` offers plotting functionality very similar to Matlab" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# \"magic\" command below allows inline printing of plots\n", "import matplotlib.pyplot as plt\n", "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "dx = .25\n", "xvals = np.arange(0, 10, dx)\n", "sol_vals = [float(sol.subs({x: xval}).evalf()) for xval in xvals]\n", "plt.plot(xvals, sol_vals)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Numeric Solution" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's now find $u(x)$ numerically using finite differencing.\n", "\n", "The forward difference operator is\n", "\n", "$\\frac{df}{dx} \\approx \\frac{f(x + \\Delta{x}) - f(x)}{\\Delta{x}}$\n", "\n", "Substituting the finite difference relation for $u'$ gives\n", "\n", "$u(x + \\Delta{x}) = u(x)(1 - \\Delta{x}) + x \\Delta{x}$\n", "\n", "Since $u(x=0)$ is known ($u(0) = 3$), we are now ready to solve for $u(x + \\Delta{x})$ iteratively" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# dx and xvals defined above\n", "\n", "u = [3.]\n", "for x in xvals[1:]:\n", " u.append(u[-1] * (1 - dx) + x * dx)\n", "u = np.array(u)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### Comparison" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [], "source": [ "# Evaluate solution over interval [0, 10] and plot\n", "plt.plot(xvals, sol_vals, label=\"Analytic\")\n", "plt.plot(xvals, u, \"r+\", label=\"Approximate\")\n", "plt.xlabel(\"x\")\n", "plt.ylabel(\"u(x)\")\n", "plt.legend(loc=\"best\");" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.9" } }, "nbformat": 4, "nbformat_minor": 0 }