{ "cells": [ { "cell_type": "markdown", "metadata": { "toc": "true" }, "source": [ "# Table of Contents\n", "
" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Sommaire #\n", "\n", "* Introduction\n", "\n", "* More of python\n", " \n", "* Numpy\n", "\n", "* Scipy\n", "\n", "* Matplotlib\n", "\n", "* Compiled code" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Introduction ##\n", "\n", " * Ipython\n", " * Coding rules\n", " * Existing documentation" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### ipython ###\n", "\n", " * Interactive python\n", " * Overlay on the classic python interpretor\n", " * Automatic completion\n", " * Interactive help\n", " * Support shell commands (for Unixians)\n", " * Better management of the commands history" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "notes" } }, "outputs": [], "source": [ "import math\n", "math?\n", "!ls -l" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "\n", "### Coding rules ###\n", "\n", "* Python offers much freedom\n", "* Maybe sometime too much freedom!\n", "* Important to decide on rules\n", " * Naming conventions\n", " * Always document functions/classes (docstring)\n", " * Tests\n", "\n", "Official advice: http://legacy.python.org/dev/peps/pep-0008/\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "notes" } }, "outputs": [], "source": [ "import this" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Documentation ###\n", "\n", "* Plenty of on-line documentation : https://docs.python.org/3/\n", " * Tutorials\n", " * Documentation of modules/functions\n", " * Source code of modules\n", "* Internet search\n", " * Of existing module\n", " * Of problem solving on forums\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## More of python ##\n", "\n", " * List indexing\n", " * List comprehension and lambda functions\n", " * Filter, map, reduce\n", " * Eval (maybe)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Lists indexing ###\n", "\n", "* indices begin at 0\n", "* indexing operator *[begin:end:step]*\n", "* can also be used with *range*\n", "* *end* is excluded of the selection\n", "* if the index *i* is negative, it is interpreted like *n+j* (where *n* is the size)\n", "* if the integer between the *:* is omitted, the default values are *0*, *size* and *1*\n", "\n", "Exercise: \n", "\n", "* Generate a list going from 0 to 9 (with range function and the list constructor)\n", " * Display the first, last, one before last elements\n", "* From this list generate a new one:\n", " * going from 0 to 4\n", " * going from 4 to 9\n", " * with the even numbers\n", " * with the odd number in decreasing order until 3\n", " * Generate a list of even number going from 2 to 10" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "notes" } }, "outputs": [], "source": [ "#python 2\n", "#l = range(10)\n", "\n", "#python 3\n", "l = list(range(10))\n", "print(l[0]); print(l[-1]); print(l[-2])\n", "a = l[:]; print(a)\n", "a = l[:5]; print(a)\n", "b = l[4:]; print(b)\n", "c = l[::2]; print(c)\n", "d = l[9:2:-2]; print(d)\n", "l2= list(range(10,1,-2)); print(l2)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### List comprehension and lambda function ###\n", "\n", " * Classic method to create a list: with a *for* loop and the *append* method\n", " * List comprehension : compact syntax allowing to generate list with a single line\n", " * Use a sequence or an iterator\n", " * Syntax: *[ ]* with inside it an expression followed of 0 or more *for* ol *if* statements\n", " \n", " * Lambda function : compact syntax allowing to write unnamed function in one line\n", " (useful when a function is a parameter of another one)\n", " * Syntax : *lambda args : expression(args)*\n", "\n", "Exercise:\n", "\n", " * Create, with a loop, a list with $sin(x)$ for values of $x$ going from 0 to 1 by 0.1 step\n", " * Create the same function in one line\n", " * Create a *f* function referencing a *lambda function* doing $sin(x^2)$\n", " * Create a list with $sin(x^2)$ for values of $x$ going from 0 to 1 by 0.1 step using *f*\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "notes" } }, "outputs": [], "source": [ "import math\n", "s = []\n", "for x in range(11):\n", " s.append(math.sin(x*0.1))\n", "s2 = [math.sin(x*0.1) for x in range(11)]\n", "print(s == s2)\n", "\n", "f = lambda x : math.sin(x*x)\n", "s3 = [f(x*0.1) for x in range(11)]" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### filter and map ###\n", "\n", "2 functions using as input argument a function and a list.\n", "Python 3, the return value is an object allowing to create lists.\n", "Python 2, the return value is a list object.\n", "\n", "* Filter : returns the elements of the input list for which the evaluation of the input function returns `true`\n", " -> the result of the input function is interpreted as boolean\n", "* Map : returns for each elements of the input list, the evaluation return by the input function.\n", " \n", "*Note : Generally, when there are two possibles syntax, the list comprehension is faster than,\n", "filter/map function, which are faster than the loops*\n", "\n", "Exercise: Test the 2 functions\n", "\n", "* get a list of integer all multiples of 3\n", "* get a list corresponding to $sin(x)$ where x is an other list." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "notes" } }, "outputs": [], "source": [ "l1 = list(filter(lambda x: x%3==0, range(15)))\n", "print(l1)\n", "l2 = list(map(math.sin,l1))\n", "print(l2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Zip ?\n", "No !" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Numpy ##\n", " * Introduction\n", " * Arrays: type, attributes, memory\n", " * Indexing/Slicing\n", " * View: reference/copy\n", " * Constructors, IO\n", " * Manipulation" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Introduction ###\n", "\n", "* Package to use n-dimension array\n", "* Designed for scientific computation\n", "* Efficient\n", "\n", "\n", "__Restrictions__ :\n", "\n", "* Total size of the array is constantscipy.cluster | \n", "Vector quantization / Kmeans |
scipy.constants | \n", "Mathematical and physics constants |
scipy.fftpack | \n", "Fourier Transformed |
scipy.integrate | \n", "Integrals |
scipy.interpolate | \n", "Interpolation |
scipy.io | \n", "Input/Output |
scipy.linalg | \n", "Linear Algebra |
scipy.ndimage | \n", "Image filtering (n dimension) |
scipy.odr | \n", "Orthogonal distance regression |
scipy.optimize | \n", "Optimization |
scipy.signal | \n", "Signal processing |
scipy.sparse | \n", "Sparse matrix |
scipy.spatial | \n", "Algorithm and space data structure |
scipy.special | \n", "Special mathematical functions |
scipy.stats | \n", "Statistics |