├── images ├── edit_mode.png ├── command_mode.png └── menubar_toolbar.png ├── LICENSE ├── README.md ├── CITING.md ├── example.css ├── Aliasing.ipynb ├── finite_difference_lab.ipynb ├── Introduction.ipynb ├── Styling_notebooks.ipynb ├── Plotting in the notebook.ipynb ├── Multigrid.ipynb └── Introducing the IPython Notebook.ipynb /images/edit_mode.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ketch/teaching-numerics-with-notebooks/HEAD/images/edit_mode.png -------------------------------------------------------------------------------- /images/command_mode.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ketch/teaching-numerics-with-notebooks/HEAD/images/command_mode.png -------------------------------------------------------------------------------- /images/menubar_toolbar.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ketch/teaching-numerics-with-notebooks/HEAD/images/menubar_toolbar.png -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | These notebooks by Aron Ahmadia and David Ketcheson are licensed under a 2 | Creative Commons Attribution 4.0 International License. All code examples 3 | are also licensed under the [MIT license](http://opensource.org/licenses/MIT). 4 | 5 | You are welcome to use and adapt these materials provided that you give attribution. 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Teaching numerical methods with IPython notebooks 2 | 3 | #### A tutorial prepared for SciPy 2014 4 | 5 | Instructors: David Ketcheson and Aron Ahmadia 6 | Monday, July 7 1-5 p.m. 7 | 8 | https://conference.scipy.org/scipy2014/schedule/presentation/1563/ 9 | 10 | See http://ketch.github.io/teaching-numerics-with-notebooks/. 11 | -------------------------------------------------------------------------------- /CITING.md: -------------------------------------------------------------------------------- 1 | Please use the following to cite this material: 2 | 3 | A. Ahmadia and D. I. Ketcheson. *Teaching Numerical Methods with IPython Notebooks*, 4 | http://github.com/ketch/tnmwin/, SciPy tutorials, 2014. 5 | 6 | 7 | Bibtex: 8 | 9 | 10 | @misc{teaching-numerics, 11 | Author = {A. Ahmadia and D. I. Ketcheson}, 12 | Howpublished = {http://github.com/ketch/tnmwin/}, 13 | Title = {Teaching Numerical Methods with IPython Notebooks}, 14 | Annote = {SciPy tutorials.}, 15 | Year = {2014}} 16 | -------------------------------------------------------------------------------- /example.css: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 117 | -------------------------------------------------------------------------------- /Aliasing.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "metadata": { 3 | "name": "", 4 | "signature": "sha256:569e19645f494b174516af445bf98d214069d54c0140047482476fc7965214cc" 5 | }, 6 | "nbformat": 3, 7 | "nbformat_minor": 0, 8 | "worksheets": [ 9 | { 10 | "cells": [ 11 | { 12 | "cell_type": "code", 13 | "collapsed": false, 14 | "input": [ 15 | "from IPython.core.display import HTML\n", 16 | "css_file = './example.css'\n", 17 | "HTML(open(css_file, \"r\").read())" 18 | ], 19 | "language": "python", 20 | "metadata": {}, 21 | "outputs": [ 22 | { 23 | "html": [ 24 | "\n", 25 | "\n", 26 | "\n", 27 | "\n", 28 | "\n", 29 | "\n", 30 | "\n" 140 | ], 141 | "metadata": {}, 142 | "output_type": "pyout", 143 | "prompt_number": 1, 144 | "text": [ 145 | "" 146 | ] 147 | } 148 | ], 149 | "prompt_number": 1 150 | }, 151 | { 152 | "cell_type": "markdown", 153 | "metadata": {}, 154 | "source": [ 155 | "\"Creative
This example by Aron Ahmadia and David Ketcheson is licensed under a Creative Commons Attribution 4.0 International License. All code examples are also licensed under the [MIT license](http://opensource.org/licenses/MIT)." 156 | ] 157 | }, 158 | { 159 | "cell_type": "heading", 160 | "level": 1, 161 | "metadata": {}, 162 | "source": [ 163 | "Aliasing" 164 | ] 165 | }, 166 | { 167 | "cell_type": "markdown", 168 | "metadata": {}, 169 | "source": [ 170 | "A numerical grid has a limited resolution. If you try to represent a rapidly-oscillating function with relatively few grid points, you will observe an effect known as **aliasing**. This naturally limits the range of frequencies that can be modelled on a given grid. It can also lead to instabilities in pseudospectral simulations, when generation of high frequencies leads to buildup of lower-frequency energy due to aliasing." 171 | ] 172 | }, 173 | { 174 | "cell_type": "markdown", 175 | "metadata": {}, 176 | "source": [ 177 | "The code below plots a sine wave of a given frequency, along with its representation on a grid with $m$ points. Try changing $p$ and notice how for $m<2p$ the function looks like a lower-frequency mode." 178 | ] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "collapsed": false, 183 | "input": [ 184 | "%matplotlib inline\n", 185 | "import numpy as np\n", 186 | "import matplotlib.pyplot as plt\n", 187 | "\n", 188 | "from IPython.display import display, HTML\n", 189 | "from IPython.html import widgets\n", 190 | "from IPython.html.widgets import interact, interactive\n", 191 | "\n", 192 | "def plot_sine(wavenumber=4,grid_points=12,plot_sine='On'):\n", 193 | " \"Plot sin(2*pi*p), sampled at m equispaced points.\"\n", 194 | " x = np.linspace(0,1,grid_points+2); # grid\n", 195 | " xf = np.linspace(0,1,1000) # fine grid\n", 196 | " y = np.sin(wavenumber*np.pi*x)\n", 197 | " yf = np.sin(wavenumber*np.pi*xf)\n", 198 | " fig = plt.figure(figsize = (8, 6));\n", 199 | " ax = fig.add_subplot(1,1,1);\n", 200 | " if plot_sine == 'On':\n", 201 | " ax.plot(xf, yf, 'r-', linewidth=2);\n", 202 | " ax.plot(x, y, 'o-', lw=2)\n", 203 | "\n", 204 | "interact(plot_sine, wavenumber=(1,44,1), grid_points=(10, 16, 1), plot_sine=(('On','Off')));" 205 | ], 206 | "language": "python", 207 | "metadata": {}, 208 | "outputs": [] 209 | }, 210 | { 211 | "cell_type": "markdown", 212 | "metadata": {}, 213 | "source": [ 214 | "Try to answer the questions below with pencil and paper; then check them by modifying the code above.\n", 215 | "\n", 216 | "1. For a given number of grid points $m$, which wavenumbers $p$ will be aliased to the $p=0$ mode? Which will be aliased to $p=1$? Can you explain why?\n", 217 | "2. What is the highest frequency mode that can be represented on a given grid?" 218 | ] 219 | } 220 | ], 221 | "metadata": {} 222 | } 223 | ] 224 | } -------------------------------------------------------------------------------- /finite_difference_lab.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "metadata": { 3 | "name": "", 4 | "signature": "sha256:cf22c7ef5c8506e7823efd50f804355e8583f5631a998725c8ed2f08dabb8b40" 5 | }, 6 | "nbformat": 3, 7 | "nbformat_minor": 0, 8 | "worksheets": [ 9 | { 10 | "cells": [ 11 | { 12 | "cell_type": "heading", 13 | "level": 1, 14 | "metadata": {}, 15 | "source": [ 16 | "Finite Differences Lab Example" 17 | ] 18 | }, 19 | { 20 | "cell_type": "heading", 21 | "level": 3, 22 | "metadata": {}, 23 | "source": [ 24 | "Aron Ahmadia (US Army ERDC) and David Ketcheson (KAUST)" 25 | ] 26 | }, 27 | { 28 | "cell_type": "heading", 29 | "level": 3, 30 | "metadata": {}, 31 | "source": [ 32 | "Teaching Numerical Methods with IPython Notebooks, SciPy 2014" 33 | ] 34 | }, 35 | { 36 | "cell_type": "markdown", 37 | "metadata": {}, 38 | "source": [ 39 | "\"Creative
This example by Aron Ahmadia and David Ketcheson is licensed under a Creative Commons Attribution 4.0 International License. All code examples are also licensed under the [MIT license](http://opensource.org/licenses/MIT)." 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "collapsed": false, 45 | "input": [ 46 | "from IPython.core.display import HTML\n", 47 | "css_file = './example.css'\n", 48 | "HTML(open(css_file, \"r\").read())" 49 | ], 50 | "language": "python", 51 | "metadata": {}, 52 | "outputs": [ 53 | { 54 | "html": [ 55 | "\n", 56 | "\n", 57 | "\n", 58 | "\n", 59 | "\n", 60 | "\n", 61 | "\n" 171 | ], 172 | "metadata": {}, 173 | "output_type": "pyout", 174 | "prompt_number": 1, 175 | "text": [ 176 | "" 177 | ] 178 | } 179 | ], 180 | "prompt_number": 1 181 | }, 182 | { 183 | "cell_type": "markdown", 184 | "metadata": {}, 185 | "source": [ 186 | "Together with your teammate, write a Python script or function to do the following:\n", 187 | "\n", 188 | "1) Generate a set of $m+2$ equally-spaced points $0=x_0,x_1,x_2,\\dots,x_m+1=1$. \n", 189 | "2) Evaluate the following function at those points: \n", 190 | "\n", 191 | "$$f(x) = 1 + x + 3x^2 + 2\\sin(5x)$$\n", 192 | "\n", 193 | "3) Evaluate the finite difference approximation\n", 194 | "\n", 195 | "$$f''(x) \\approx \\frac{f(x_{i-1}) - 2 f(x_i) + f(x_{i+1})}{h^2}$$\n", 196 | "\n", 197 | "for $i=2,3,\\dots,m$, where $h$ is the spacing between successive points.\n", 198 | "\n", 199 | "4) Plot the finite difference approximation and the exact values of the second derivative.\n", 200 | "\n", 201 | "Try running your function with different values of $m$ and check that its output tends to the exact second derivative as $m$ is increased.\n", 202 | "\n", 203 | "There are at least 3 different ways to program step 3:\n", 204 | "\n", 205 | "- Using a loop\n", 206 | "- Using matrix-vector multiplication\n", 207 | "- Using array slicing\n", 208 | "\n", 209 | "Can you code all 3? Which is the fastest? You can time any line of code in IPython using %timeit." 210 | ] 211 | }, 212 | { 213 | "cell_type": "code", 214 | "collapsed": false, 215 | "input": [], 216 | "language": "python", 217 | "metadata": {}, 218 | "outputs": [] 219 | }, 220 | { 221 | "cell_type": "markdown", 222 | "metadata": {}, 223 | "source": [ 224 | "Now add the following to your function:\n", 225 | "\n", 226 | "1) Compute the error of your approximation in the following grid-function norms:\n", 227 | "\n", 228 | "- $L_1: h\\sum_i |E(x_i)|$\n", 229 | "- $L_2: \\sqrt{h\\sum_i \\left(E(x_i)\\right)^2}$\n", 230 | "- $L_\\infty$: $\\max_i |E(x_i)|$\n", 231 | "\n", 232 | "2) Loop over the following values of $m$: $1,2,4,8,16$. \n", 233 | "Compute the three error norms for each $m$-value.\n", 234 | "\n", 235 | "3) Estimate the convergence rate using the formula $E(h) \\approx Ch^p$. *Hint: take the absolute value of both sides and then take the log. What does the resulting equation describe, and what is the role of $p$?*" 236 | ] 237 | }, 238 | { 239 | "cell_type": "code", 240 | "collapsed": false, 241 | "input": [], 242 | "language": "python", 243 | "metadata": {}, 244 | "outputs": [] 245 | } 246 | ], 247 | "metadata": {} 248 | } 249 | ] 250 | } -------------------------------------------------------------------------------- /Introduction.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "metadata": { 3 | "name": "", 4 | "signature": "sha256:72affc010a2aa23af8053c93488a06c74e7eaf7808b6d29cde74c3ba9fb3c6b4" 5 | }, 6 | "nbformat": 3, 7 | "nbformat_minor": 0, 8 | "worksheets": [ 9 | { 10 | "cells": [ 11 | { 12 | "cell_type": "heading", 13 | "level": 1, 14 | "metadata": {}, 15 | "source": [ 16 | "Teaching Numerical Methods with IPython Notebooks" 17 | ] 18 | }, 19 | { 20 | "cell_type": "heading", 21 | "level": 3, 22 | "metadata": { 23 | "slideshow": { 24 | "slide_type": "fragment" 25 | } 26 | }, 27 | "source": [ 28 | "Aron Ahmadia (US Army ERDC) and David Ketcheson (KAUST)" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": {}, 34 | "source": [ 35 | "\"Creative
This lecture by Aron Ahmadia and David Ketcheson is licensed under a Creative Commons Attribution 4.0 International License. All code examples are also licensed under the [MIT license](http://opensource.org/licenses/MIT)." 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "collapsed": false, 41 | "input": [ 42 | "from IPython.core.display import HTML\n", 43 | "css_file = './example.css'\n", 44 | "HTML(open(css_file, \"r\").read())" 45 | ], 46 | "language": "python", 47 | "metadata": { 48 | "slideshow": { 49 | "slide_type": "skip" 50 | } 51 | }, 52 | "outputs": [ 53 | { 54 | "html": [ 55 | "\n", 56 | "\n", 57 | "\n", 58 | "\n", 59 | "\n", 60 | "\n", 61 | "\n" 171 | ], 172 | "metadata": {}, 173 | "output_type": "pyout", 174 | "prompt_number": 1, 175 | "text": [ 176 | "" 177 | ] 178 | } 179 | ], 180 | "prompt_number": 1 181 | }, 182 | { 183 | "cell_type": "heading", 184 | "level": 2, 185 | "metadata": {}, 186 | "source": [ 187 | "Scope" 188 | ] 189 | }, 190 | { 191 | "cell_type": "markdown", 192 | "metadata": { 193 | "slideshow": { 194 | "slide_type": "slide" 195 | } 196 | }, 197 | "source": [ 198 | "This course is about how to use IPython notebooks in teaching. It's based on our experience teaching semester-length university courses as well as short courses for academia and industry. Although the examples will be drawn from courses on numerical methods, many of the ideas may be useful for other courses in which Python is used." 199 | ] 200 | }, 201 | { 202 | "cell_type": "markdown", 203 | "metadata": { 204 | "slideshow": { 205 | "slide_type": "slide" 206 | } 207 | }, 208 | "source": [ 209 | "A course in numerical methods should enable students to:\n", 210 | "\n", 211 | "1. **Understand** numerical algorithms and related mathematical concepts like\n", 212 | " complexity, stability, and convergence\n", 213 | "2. **Select** an appropriate method for a given problem\n", 214 | "3. **Implement** the selected numerical algorithm\n", 215 | "4. **Test** and debug the numerical implementation" 216 | ] 217 | }, 218 | { 219 | "cell_type": "markdown", 220 | "metadata": { 221 | "slideshow": { 222 | "slide_type": "fragment" 223 | } 224 | }, 225 | "source": [ 226 | "Many courses focus almost entirely on the first objective, but all four are essential. It's worth devoting significant time in class to each of them. That means that students should code *in class*. The IPython notebook is a great tool for this.\n", 227 | "\n", 228 | "Notebooks can also be used in a more traditional course; for instance\n", 229 | "\n", 230 | "- to distribute and collect homework exercises;\n", 231 | "- for occasional laboratory sessions;\n", 232 | "- as a textbook or course notes." 233 | ] 234 | }, 235 | { 236 | "cell_type": "markdown", 237 | "metadata": { 238 | "slideshow": { 239 | "slide_type": "slide" 240 | } 241 | }, 242 | "source": [ 243 | "## The IPython notebook as a teaching medium\n", 244 | "\n", 245 | "The notebook combines in a single document\n", 246 | "\n", 247 | "- Mathematics (using LaTeX)\n", 248 | "- Text (using Markdown)\n", 249 | "- Code (in Python or other languages)\n", 250 | "- Figures and animations\n", 251 | "\n", 252 | "We'll go into this in more detail in a few minutes." 253 | ] 254 | }, 255 | { 256 | "cell_type": "markdown", 257 | "metadata": { 258 | "slideshow": { 259 | "slide_type": "fragment" 260 | } 261 | }, 262 | "source": [ 263 | "[Here's an example.](http://nbviewer.ipython.org/github/ketch/HyperPython/blob/master/Lesson_02_Traffic.ipynb)" 264 | ] 265 | }, 266 | { 267 | "cell_type": "heading", 268 | "level": 2, 269 | "metadata": { 270 | "slideshow": { 271 | "slide_type": "slide" 272 | } 273 | }, 274 | "source": [ 275 | "More Examples" 276 | ] 277 | }, 278 | { 279 | "cell_type": "markdown", 280 | "metadata": { 281 | "slideshow": { 282 | "slide_type": "fragment" 283 | } 284 | }, 285 | "source": [ 286 | "The IPython notebook is already being used as a medium of instruction for courses in numerical methods or scientific computing:\n", 287 | "\n", 288 | "- [AeroPython](http://lorenabarba.com/blog/announcing-aeropython/) (Lorena Barba)\n", 289 | "- [12 Steps to Navier-Stokes](http://lorenabarba.com/blog/cfd-python-12-steps-to-navier-stokes/) (Lorena Barba)\n", 290 | "- [Numerical analysis of DEs](https://github.com/ketch/AMCS252) (DK) ([see also notebooks from previous years](https://github.com/ketch/finite-difference-course))\n", 291 | "- [HyperPython](https://github.com/ketch/HyperPython) (DK)\n", 292 | "\n", 293 | "More course materials (in many subjects!), can be found in this curated list: \n", 294 | "\n", 295 | "[A gallery of interesting IPython Notebooks](https://github.com/ipython/ipython/wiki/A-gallery-of-interesting-IPython-Notebooks)" 296 | ] 297 | }, 298 | { 299 | "cell_type": "heading", 300 | "level": 2, 301 | "metadata": { 302 | "slideshow": { 303 | "slide_type": "slide" 304 | } 305 | }, 306 | "source": [ 307 | "Other resources" 308 | ] 309 | }, 310 | { 311 | "cell_type": "markdown", 312 | "metadata": { 313 | "slideshow": { 314 | "slide_type": "fragment" 315 | } 316 | }, 317 | "source": [ 318 | "This tutorial is primarily focused on the technical aspects of using IPython notebooks for teaching. For more discussion of pedagogical aspects, see also:\n", 319 | "\n", 320 | "- [Teaching with the IPython Notebook](http://nbviewer.ipython.org/gist/jiffyclub/5165431) by Matt Davis\n", 321 | "- [How IPython Notebook and Github have changed the way I teach Python](http://peak5390.wordpress.com/2013/09/22/how-ipython-notebook-and-github-have-changed-the-way-i-teach-python/) by Eric Matthes\n", 322 | "- [Using the IPython Notebook as a Teaching Tool](http://www.software-carpentry.org/blog/2013/03/using-notebook-as-a-teaching-tool.html) by Greg Wilson\n", 323 | "- [Teaching with ipython notebooks -- a progress report](http://ivory.idyll.org/blog/teaching-with-ipynb-2.html) by C. Titus Brown" 324 | ] 325 | }, 326 | { 327 | "cell_type": "heading", 328 | "level": 2, 329 | "metadata": { 330 | "slideshow": { 331 | "slide_type": "slide" 332 | } 333 | }, 334 | "source": [ 335 | "Running notebooks in the cloud" 336 | ] 337 | }, 338 | { 339 | "cell_type": "markdown", 340 | "metadata": { 341 | "slideshow": { 342 | "slide_type": "fragment" 343 | } 344 | }, 345 | "source": [ 346 | "In courses using Python, achieving a working installation of all necessary libraries for every student used to be difficult. No longer! The major scientific Python packages are now easy to install on all major platforms (e.g., using [Canopy](https://www.enthought.com/products/canopy/) or [Anaconda](https://store.continuum.io/cshop/anaconda/)). " 347 | ] 348 | }, 349 | { 350 | "cell_type": "markdown", 351 | "metadata": { 352 | "slideshow": { 353 | "slide_type": "fragment" 354 | } 355 | }, 356 | "source": [ 357 | "An even more painless solution is to use a **cloud service**. Two cloud services for running IPython notebooks are currently available:\n", 358 | "\n", 359 | "- [Sage Math Cloud](http://cloud.sagemath.org>)\n", 360 | "- [Wakari](http://wakari.io)\n", 361 | "\n", 362 | "Both allow **free accounts**, and include all the most common scientific Python packages by default." 363 | ] 364 | }, 365 | { 366 | "cell_type": "heading", 367 | "level": 2, 368 | "metadata": {}, 369 | "source": [ 370 | "Distributing notebooks" 371 | ] 372 | }, 373 | { 374 | "cell_type": "markdown", 375 | "metadata": {}, 376 | "source": [ 377 | "If you keep your materials in a Github repository, students can easily get them using Sage Math Cloud (without knowing git) or by cloning (with knowing git)." 378 | ] 379 | }, 380 | { 381 | "cell_type": "markdown", 382 | "metadata": {}, 383 | "source": [ 384 | "A dead simple way to distribute a notebook is by using Dropbox and nbviewer. Just put the notebook in your public Dropbox folder, get the public link to it, and paste that into [nbviewer](http://nbviewer.ipython.org/). Give the resulting URL to your students, and they can both view and download the notebook. For example, [here is a link to this notebook on nbviewer through Dropbox](http://nbviewer.ipython.org/urls/dl.dropboxusercontent.com/u/656693/Introduction.ipynb)." 385 | ] 386 | }, 387 | { 388 | "cell_type": "markdown", 389 | "metadata": {}, 390 | "source": [ 391 | "Another very useful tool for distributing notebooks is [gist](http://gist.github.com/)." 392 | ] 393 | }, 394 | { 395 | "cell_type": "heading", 396 | "level": 2, 397 | "metadata": {}, 398 | "source": [ 399 | "Collecting notebooks" 400 | ] 401 | }, 402 | { 403 | "cell_type": "markdown", 404 | "metadata": {}, 405 | "source": [ 406 | "- Have each student/group add you as a collaborator to their SMC project. Then you have direct access to their notebooks. It is helpful to have them put their name or ID number in the project name.\n", 407 | "- Have students post their notebook on the web (using dropbox or Github) and send you the link.\n", 408 | "- In small courses, evaluation can be done in-class, with students showing their work to the class. This can also be done in somewhat larger classes if students work in groups." 409 | ] 410 | } 411 | ], 412 | "metadata": {} 413 | } 414 | ] 415 | } -------------------------------------------------------------------------------- /Styling_notebooks.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "metadata": { 3 | "name": "", 4 | "signature": "sha256:00b33d93b2202bf96955eb36dc6e663425769213dc607eebcdac797193d75988" 5 | }, 6 | "nbformat": 3, 7 | "nbformat_minor": 0, 8 | "worksheets": [ 9 | { 10 | "cells": [ 11 | { 12 | "cell_type": "heading", 13 | "level": 1, 14 | "metadata": {}, 15 | "source": [ 16 | "Adding style to the notebook" 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "### Teaching Numerical Methods with IPython Notebooks, SciPy 2014 \n", 24 | "\n", 25 | "### Aron Ahmadia (US Army ERDC) and David Ketcheson (KAUST)" 26 | ] 27 | }, 28 | { 29 | "cell_type": "markdown", 30 | "metadata": {}, 31 | "source": [ 32 | "\"Creative
This lecture by Aron Ahmadia and David Ketcheson is licensed under a Creative Commons Attribution 4.0 International License. All code examples are also licensed under the [MIT license](http://opensource.org/licenses/MIT)." 33 | ] 34 | }, 35 | { 36 | "cell_type": "markdown", 37 | "metadata": {}, 38 | "source": [ 39 | "You can give your notebooks a distinctive flair, and perhaps make them more useful for your students, by modifying the way they look.\n", 40 | "\n", 41 | "The appearance of the IPython notebook is managed through **Cascading Style Sheets** (CSS), a widely-used web technology. It's possible to make huge modifications to the notebook with CSS; we will just cover the basics." 42 | ] 43 | }, 44 | { 45 | "cell_type": "heading", 46 | "level": 2, 47 | "metadata": {}, 48 | "source": [ 49 | "Applying a stylesheet" 50 | ] 51 | }, 52 | { 53 | "cell_type": "markdown", 54 | "metadata": {}, 55 | "source": [ 56 | "You can apply a particular style locally to all notebooks you run by adding a `custom.css` file to your ipython profile directory. This is fun, but not so useful for teaching, since it will only change the look of notebooks on your own machine. What's needed is a way to distribute styling information with the notebook itself, so that anyone viewing it sees it the way you intended." 57 | ] 58 | }, 59 | { 60 | "cell_type": "markdown", 61 | "metadata": {}, 62 | "source": [ 63 | "We can apply a CSS style to the notebook dynamically like this:" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "collapsed": false, 69 | "input": [ 70 | "from IPython.core.display import HTML\n", 71 | "css_file = './example.css'\n", 72 | "HTML(open(css_file, \"r\").read())" 73 | ], 74 | "language": "python", 75 | "metadata": {}, 76 | "outputs": [ 77 | { 78 | "html": [ 79 | "\n", 80 | "\n", 81 | "\n", 82 | "\n", 83 | "\n", 84 | "\n", 85 | "\n" 195 | ], 196 | "metadata": {}, 197 | "output_type": "pyout", 198 | "prompt_number": 3, 199 | "text": [ 200 | "" 201 | ] 202 | } 203 | ], 204 | "prompt_number": 3 205 | }, 206 | { 207 | "cell_type": "markdown", 208 | "metadata": {}, 209 | "source": [ 210 | "This is slightly intrusive -- ideally, the styling would be applied without the user needing to know or do anything about it. [Such a solution is in the works](https://github.com/ipython/ipython/issues/5921), but for now the code above is the best approach available. It simply injects the contents of `css_file` in the header of the notebook's HTML file. Any file can be used just by replacing the path stored in `css_file`." 211 | ] 212 | }, 213 | { 214 | "cell_type": "heading", 215 | "level": 2, 216 | "metadata": {}, 217 | "source": [ 218 | "Using CSS with the Notebook" 219 | ] 220 | }, 221 | { 222 | "cell_type": "markdown", 223 | "metadata": {}, 224 | "source": [ 225 | "Let's take a look at the contents of that CSS file, using IPython's `%cat` magic function:" 226 | ] 227 | }, 228 | { 229 | "cell_type": "code", 230 | "collapsed": false, 231 | "input": [ 232 | "%cat 'example.css'" 233 | ], 234 | "language": "python", 235 | "metadata": {}, 236 | "outputs": [] 237 | }, 238 | { 239 | "cell_type": "markdown", 240 | "metadata": {}, 241 | "source": [ 242 | "We can use `%load` and `%%html` to load the file into a cell and actually apply it:" 243 | ] 244 | }, 245 | { 246 | "cell_type": "code", 247 | "collapsed": false, 248 | "input": [ 249 | "%load 'example.css'" 250 | ], 251 | "language": "python", 252 | "metadata": {}, 253 | "outputs": [] 254 | }, 255 | { 256 | "cell_type": "markdown", 257 | "metadata": {}, 258 | "source": [ 259 | "There's a lot there (notice that the notebook created a scrollable section because the output was so long). Let's walk through this custom stylesheet one piece at a time.\n", 260 | "\n", 261 | "---\n", 262 | "\n", 263 | "The first section is actually pure HTML, not CSS:\n", 264 | "\n", 265 | "```html\n", 266 | "\n", 267 | "\n", 268 | "\n", 269 | "\n", 270 | "\n", 271 | "```\n", 272 | "\n", 273 | "This loads a bunch of [Google fonts]() that we are going to use. If you use fonts that are usually installed on most OS's, you don't need a section like this." 274 | ] 275 | }, 276 | { 277 | "cell_type": "markdown", 278 | "metadata": {}, 279 | "source": [ 280 | "---\n", 281 | "\n", 282 | "```css\n", 283 | "\n", 544 | "```" 545 | ] 546 | }, 547 | { 548 | "cell_type": "markdown", 549 | "metadata": {}, 550 | "source": [ 551 | "## More resources\n", 552 | "\n", 553 | "If you've never used CSS, there are a number of good tutorials available on the web. Once you understand a bit of CSS, all you need to know are the names that IPython uses for the different sections of the notebook. Unfortunately, these are not well documented at the moment. The best place to start with styling your own notebooks are the many examples out there:\n", 554 | "\n", 555 | "- [HyperPython styling]() ([sample notebook]())\n", 556 | "- [AeroPython styling]() ([sample notebook]())\n", 557 | "- [Louic Vermeer's custom CSS](http://blog.louic.nl/?p=683)\n", 558 | "- [48 themes for your IPython notebook](http://www.damian.oquanta.info/posts/48-themes-for-your-ipython-notebook.html) from Damian Avila\n", 559 | "- [Somebody's custom.css from Github](https://github.com/panditarevolution/ipythonNotebook_customs/blob/master/monokai/custom.css)" 560 | ] 561 | }, 562 | { 563 | "cell_type": "code", 564 | "collapsed": false, 565 | "input": [], 566 | "language": "python", 567 | "metadata": {}, 568 | "outputs": [] 569 | } 570 | ], 571 | "metadata": {} 572 | } 573 | ] 574 | } -------------------------------------------------------------------------------- /Plotting in the notebook.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "metadata": { 3 | "name": "", 4 | "signature": "sha256:b5f0a23736d263f47764a97687577b2528579133e254af4771d5c9c1889c0f21" 5 | }, 6 | "nbformat": 3, 7 | "nbformat_minor": 0, 8 | "worksheets": [ 9 | { 10 | "cells": [ 11 | { 12 | "cell_type": "heading", 13 | "level": 1, 14 | "metadata": {}, 15 | "source": [ 16 | "Plotting in the notebook" 17 | ] 18 | }, 19 | { 20 | "cell_type": "heading", 21 | "level": 3, 22 | "metadata": {}, 23 | "source": [ 24 | "Aron Ahmadia (US Army ERDC) and David Ketcheson (KAUST)" 25 | ] 26 | }, 27 | { 28 | "cell_type": "heading", 29 | "level": 3, 30 | "metadata": {}, 31 | "source": [ 32 | "Teaching Numerical Methods with IPython Notebooks, SciPy 2014" 33 | ] 34 | }, 35 | { 36 | "cell_type": "markdown", 37 | "metadata": {}, 38 | "source": [ 39 | "\"Creative
This lecture by Aron Ahmadia and David Ketcheson is licensed under a Creative Commons Attribution 4.0 International License. All code examples are also licensed under the [MIT license](http://opensource.org/licenses/MIT)." 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "collapsed": false, 45 | "input": [ 46 | "from IPython.core.display import HTML\n", 47 | "css_file = './example.css'\n", 48 | "HTML(open(css_file, \"r\").read())" 49 | ], 50 | "language": "python", 51 | "metadata": {}, 52 | "outputs": [ 53 | { 54 | "html": [ 55 | "\n", 56 | "\n", 57 | "\n", 58 | "\n", 59 | "\n", 60 | "\n", 61 | "\n" 171 | ], 172 | "metadata": {}, 173 | "output_type": "pyout", 174 | "prompt_number": 12, 175 | "text": [ 176 | "" 177 | ] 178 | } 179 | ], 180 | "prompt_number": 12 181 | }, 182 | { 183 | "cell_type": "markdown", 184 | "metadata": {}, 185 | "source": [ 186 | "One of the major advantages of the notebook over a traditional textbook is that **plots** of numerical solutions can be included with the **code** that produces them -- and the student can modify and execute that code, to see how the result changes! Better yet, **animations** and **interactive widgets** can be included to show the evolution of time-dependent solutions, or the dependence of results on some parameter(s). Here we'll explore some of the tools for producing such plots and animations. Some of these are very new and are still evolving rapidly." 187 | ] 188 | }, 189 | { 190 | "cell_type": "markdown", 191 | "metadata": {}, 192 | "source": [ 193 | "In order to make things more interesting and relevant, we'll demonstrate these options in the context of a PDE solution." 194 | ] 195 | }, 196 | { 197 | "cell_type": "markdown", 198 | "metadata": {}, 199 | "source": [ 200 | "# The model\n", 201 | "\n", 202 | "We'll solve a system of reaction-diffusion PDEs in two dimensions:\n", 203 | "\n", 204 | "\\begin{align}\n", 205 | "u_t & = \\delta D_1 \\nabla^2 u + f(u,v) \\\\\n", 206 | "v_t & = \\delta D_2 \\nabla^2 v + g(u,v)\n", 207 | "\\end{align}\n", 208 | "\n", 209 | "where $\\nabla^2 u = u_{xx} + u_{yy}$ denotes the Laplacian and $f,g$ represent reaction terms.\n", 210 | "\n", 211 | "For simplicity, we'll consider the square domain $[-1,1]\\times[-1,1]$ with periodic boundary conditions; i.e., the conditions on $u(x,y,t)$ are\n", 212 | "\n", 213 | "\\begin{align}\n", 214 | "u(-1,y,t) & = u(1,y,t) \\\\\n", 215 | "u(x,-1,t) & = u(x,1,t)\n", 216 | "\\end{align}\n", 217 | "\n", 218 | "with corresponding conditions on $v$. The reaction terms we will use are\n", 219 | "\n", 220 | "\\begin{align}\n", 221 | "f(u,v) & = \\alpha u (1-\\tau_1 v^2) + v(1-\\tau_2 u) \\\\\n", 222 | "g(u,v) & = \\beta v + \\alpha \\tau_1 u v^2 + u (\\gamma + \\tau_2 v).\n", 223 | "\\end{align}" 224 | ] 225 | }, 226 | { 227 | "cell_type": "code", 228 | "collapsed": false, 229 | "input": [ 230 | "import numpy as np\n", 231 | "import scipy.optimize\n", 232 | "import scipy.sparse\n", 233 | "\n", 234 | "def f(u,v):\n", 235 | " return alpha*u*(1-tau1*v**2) + v*(1-tau2*u);\n", 236 | "\n", 237 | "def g(u,v):\n", 238 | " return beta*v*(1+alpha*tau1/beta*u*v) + u*(gamma+tau2*v);\n", 239 | "\n", 240 | "def five_pt_laplacian_sparse_periodic(m,a,b):\n", 241 | " \"\"\"Construct a sparse matrix that applies the 5-point laplacian discretization\n", 242 | " with periodic BCs on all sides.\"\"\"\n", 243 | " e=np.ones(m**2)\n", 244 | " e2=([1]*(m-1)+[0])*m\n", 245 | " e3=([0]+[1]*(m-1))*m\n", 246 | " h=(b-a)/(m+1)\n", 247 | " A=scipy.sparse.spdiags([-4*e,e2,e3,e,e],[0,-1,1,-m,m],m**2,m**2)\n", 248 | " # Top & bottom BCs:\n", 249 | " A_periodic = scipy.sparse.spdiags([e,e],[m-m**2,m**2-m],m**2,m**2).tolil()\n", 250 | " # Left & right BCs:\n", 251 | " for i in range(m):\n", 252 | " A_periodic[i*m,(i+1)*m-1] = 1.\n", 253 | " A_periodic[(i+1)*m-1,i*m] = 1.\n", 254 | " A = A + A_periodic\n", 255 | " A/=h**2\n", 256 | " A = A.todia()\n", 257 | " return A\n", 258 | "\n", 259 | "def one_step(u,v,k,A,delta,D1=0.5,D2=1.0):\n", 260 | " u_new = u + k * (delta*D1*A*u + f(u,v))\n", 261 | " v_new = v + k * (delta*D2*A*v + g(u,v))\n", 262 | " \n", 263 | " return u_new, v_new\n", 264 | "\n", 265 | "def step_size(h,delta):\n", 266 | " return h**2/(5.*delta)" 267 | ], 268 | "language": "python", 269 | "metadata": {}, 270 | "outputs": [] 271 | }, 272 | { 273 | "cell_type": "heading", 274 | "level": 2, 275 | "metadata": {}, 276 | "source": [ 277 | "Matplotlib: static plots" 278 | ] 279 | }, 280 | { 281 | "cell_type": "markdown", 282 | "metadata": {}, 283 | "source": [ 284 | "As a first example of a plot, let's just look at the sparsity structure of the numerical laplacian matrix produced by the provided function above. By default, matplotlib plots will open in a separate window, just as they would if we were plotting from an IPython command line. To get them to appear in the notebook, we use an IPython magic function" 285 | ] 286 | }, 287 | { 288 | "cell_type": "code", 289 | "collapsed": false, 290 | "input": [ 291 | "%matplotlib inline" 292 | ], 293 | "language": "python", 294 | "metadata": {}, 295 | "outputs": [] 296 | }, 297 | { 298 | "cell_type": "code", 299 | "collapsed": false, 300 | "input": [ 301 | "A = five_pt_laplacian_sparse_periodic(4,-1.,1.)\n", 302 | "import matplotlib.pyplot as plt\n", 303 | "plt.spy(A)" 304 | ], 305 | "language": "python", 306 | "metadata": {}, 307 | "outputs": [] 308 | }, 309 | { 310 | "cell_type": "markdown", 311 | "metadata": {}, 312 | "source": [ 313 | "Now let's solve the reaction-diffusion PDE and plot the solution (note that the methods we're using here are not very accurate or efficient, but they're good enough to give a qualitatively correct solution in reasonable time on a small grid)." 314 | ] 315 | }, 316 | { 317 | "cell_type": "code", 318 | "collapsed": false, 319 | "input": [ 320 | "delta=0.0021; tau1=3.5; tau2=0; alpha=0.899; beta=-0.91; gamma=-alpha;\n", 321 | "\n", 322 | "def set_up(m, T, a=-1., b=1.):\n", 323 | " # Set up the grid\n", 324 | " a=-1.; b=1.\n", 325 | " h=(b-a)/m; # Grid spacing\n", 326 | " x = np.linspace(a,b,m) # Coordinates\n", 327 | " y = np.linspace(a,b,m)\n", 328 | "\n", 329 | " # Initial data\n", 330 | " u=np.random.randn(m,m)/2.;\n", 331 | " v=np.random.randn(m,m)/2.;\n", 332 | "\n", 333 | " plt.clf(); plt.hold(False)\n", 334 | " plt.pcolormesh(x,y,u); plt.colorbar(); plt.axis('image');\n", 335 | " plt.draw()\n", 336 | " \n", 337 | " u=u.reshape(-1)\n", 338 | " v=v.reshape(-1)\n", 339 | "\n", 340 | " A=five_pt_laplacian_sparse_periodic(m,-1.,1.)\n", 341 | "\n", 342 | " k = step_size(h,delta) # Time step size\n", 343 | " N = int(round(T/k)) # Number of steps to take\n", 344 | " \n", 345 | " return x, y, u, v, A, k, N\n", 346 | " \n", 347 | "def pattern_formation(m,T):\n", 348 | " r\"\"\"Model pattern formation by solving a reaction-diffusion PDE on a periodic\n", 349 | " square domain with an m x m grid.\"\"\"\n", 350 | " x, y, u, v, A, k, N = set_up(m,T)\n", 351 | " t=0. # Initial time\n", 352 | " \n", 353 | " #Now step forward in time\n", 354 | " for j in range(N):\n", 355 | "\n", 356 | " u,v = one_step(u,v,k,A,delta) \n", 357 | " t = t+k;\n", 358 | "\n", 359 | " # Plot the final solution\n", 360 | " U=u.reshape((m,m))\n", 361 | "\n", 362 | " plt.pcolormesh(x,y,U)\n", 363 | " plt.colorbar()\n", 364 | " plt.axis('image')\n", 365 | " plt.show()" 366 | ], 367 | "language": "python", 368 | "metadata": {}, 369 | "outputs": [] 370 | }, 371 | { 372 | "cell_type": "code", 373 | "collapsed": false, 374 | "input": [ 375 | "pattern_formation(m=100,T=200)" 376 | ], 377 | "language": "python", 378 | "metadata": {}, 379 | "outputs": [] 380 | }, 381 | { 382 | "cell_type": "markdown", 383 | "metadata": {}, 384 | "source": [ 385 | "Having the plot in the notebook is nice, but there are major drawbacks:\n", 386 | "\n", 387 | "- We can't zoom or pan the plot interactively\n", 388 | "- If a single cell produces multiple plots, only the last one appears (try it)\n", 389 | "- Thus we only see the final state of a time-dependent solution\n", 390 | "\n", 391 | "There are straightforward ways to get multiple plots from one cell, and even to plot several snapshots of a time-dependent solution. But it would be much nicer if the solution plot actually evolved in time. Here's a crude way to accomplish that:" 392 | ] 393 | }, 394 | { 395 | "cell_type": "code", 396 | "collapsed": false, 397 | "input": [ 398 | "import time\n", 399 | "from IPython.display import display, clear_output\n", 400 | "\n", 401 | "def pattern_formation(m,T):\n", 402 | " x, y, u, v, A, k, N = set_up(m,T)\n", 403 | " t=0. # Initial time\n", 404 | " \n", 405 | " #Now step forward in time\n", 406 | " next_plot = 0\n", 407 | " for j in range(N):\n", 408 | "\n", 409 | " u,v = one_step(u,v,k,A,delta) \n", 410 | " t = t+k;\n", 411 | "\n", 412 | " #Plot every t=5 units\n", 413 | " if t>next_plot:\n", 414 | " clear_output(wait=True)\n", 415 | " next_plot = next_plot + 5\n", 416 | " U=u.reshape((m,m))\n", 417 | " time.sleep(0.2)\n", 418 | " plt.pcolormesh(x,y,U); plt.axis('image'); plt.title(str(t))\n", 419 | " fig=plt.gcf(); display(fig)\n", 420 | " \n", 421 | " return U" 422 | ], 423 | "language": "python", 424 | "metadata": {}, 425 | "outputs": [] 426 | }, 427 | { 428 | "cell_type": "code", 429 | "collapsed": false, 430 | "input": [ 431 | "U = pattern_formation(m=100,T=50)\n", 432 | "plt.close('all')" 433 | ], 434 | "language": "python", 435 | "metadata": {}, 436 | "outputs": [] 437 | }, 438 | { 439 | "cell_type": "markdown", 440 | "metadata": {}, 441 | "source": [ 442 | "See [this notebook for more examples of using `clear_output`](http://nbviewer.ipython.org/github/ipython/ipython/blob/2.x/examples/Notebook/Animations%20Using%20clear_output.ipynb)." 443 | ] 444 | }, 445 | { 446 | "cell_type": "heading", 447 | "level": 2, 448 | "metadata": {}, 449 | "source": [ 450 | "Interactive plots" 451 | ] 452 | }, 453 | { 454 | "cell_type": "markdown", 455 | "metadata": {}, 456 | "source": [ 457 | "What if we want to be able to zoom and pan? That's a bit harder, but there are multiple solutions either just developed or in the works. " 458 | ] 459 | }, 460 | { 461 | "cell_type": "heading", 462 | "level": 3, 463 | "metadata": {}, 464 | "source": [ 465 | "Plotly" 466 | ] 467 | }, 468 | { 469 | "cell_type": "markdown", 470 | "metadata": {}, 471 | "source": [ 472 | "One is [Plotly](https://plot.ly/plot), a web-based service that generates interactive plots and allows them to be embedded in the notebook. To use it, you should set up an account. But for this tutorial, you can just run the following code using my account." 473 | ] 474 | }, 475 | { 476 | "cell_type": "code", 477 | "collapsed": false, 478 | "input": [ 479 | "import plotly\n", 480 | "import plotly.plotly as py \n", 481 | "py.sign_in('DavidKetcheson','mgs2lgb203')\n", 482 | "\n", 483 | "py.iplot([plotly.graph_objs.Heatmap(z=U)],width=500,height=500)" 484 | ], 485 | "language": "python", 486 | "metadata": {}, 487 | "outputs": [] 488 | }, 489 | { 490 | "cell_type": "markdown", 491 | "metadata": {}, 492 | "source": [ 493 | "Notice that you can get actual data values by hovering over the plot. You can also adjust the plot's look interactively on the Plotly website. For many more examples of using Plotly in the IPython notebook, see [this notebook](http://nbviewer.ipython.org/gist/chriddyp/7628933) and [this notebook](http://nbviewer.ipython.org/github/plotly/IPython-plotly/blob/master/Plotly%20Quickstart.ipynb), both of which are introductions to Plotly. For a few more examples of plotting numerical simulation results with Plotly, look at [my notebook on Stegotons](http://nbviewer.ipython.org/gist/ketch/8554686).\n", 494 | "\n", 495 | "Importantly, usage of Plotly requires an internet connection." 496 | ] 497 | }, 498 | { 499 | "cell_type": "heading", 500 | "level": 3, 501 | "metadata": {}, 502 | "source": [ 503 | "Bokeh" 504 | ] 505 | }, 506 | { 507 | "cell_type": "markdown", 508 | "metadata": {}, 509 | "source": [ 510 | "Another package that can produce interactive plots in the notebook (this time without needing an internet connection) is [Bokeh](http://bokeh.pydata.org/). I couldn't get a `pcolor` plot example working with Bokeh (I think it's possible, but it's in beta and changing rapidly). Here is a very simple line plot example." 511 | ] 512 | }, 513 | { 514 | "cell_type": "code", 515 | "collapsed": false, 516 | "input": [ 517 | "from bokeh.plotting import output_notebook, scatter, show, line\n", 518 | "output_notebook()" 519 | ], 520 | "language": "python", 521 | "metadata": {}, 522 | "outputs": [] 523 | }, 524 | { 525 | "cell_type": "code", 526 | "collapsed": false, 527 | "input": [ 528 | "x = np.linspace(0, 4*np.pi, 100)\n", 529 | "y = np.sin(x)\n", 530 | "line(x,y, color=\"#FF00FF\", tools=\"pan,wheel_zoom,box_zoom,reset,resize\")\n", 531 | "show()" 532 | ], 533 | "language": "python", 534 | "metadata": {}, 535 | "outputs": [] 536 | }, 537 | { 538 | "cell_type": "markdown", 539 | "metadata": {}, 540 | "source": [ 541 | "Animated plots are also possible with Bokeh, but we won't spend more time on it because the interface will change in the near future (they plan to integrate with IPython widgets)." 542 | ] 543 | }, 544 | { 545 | "cell_type": "heading", 546 | "level": 3, 547 | "metadata": {}, 548 | "source": [ 549 | "Other prospects" 550 | ] 551 | }, 552 | { 553 | "cell_type": "markdown", 554 | "metadata": {}, 555 | "source": [ 556 | "Some other projects that allow (or may soon allow) for interactive plots in the browser include:\n", 557 | "\n", 558 | "- [mpld3](http://mpld3.github.io/): a combination of matplotlib and [D3js](http://d3js.org/). See [examples here](http://mpld3.github.io/examples/index.html#example-gallery).\n", 559 | "- [Vincent](http://vincent.readthedocs.org/en/latest/index.html)\n", 560 | "- [Vispy](http://vispy.org/)\n", 561 | "\n", 562 | "The last two are still too new to go into in this tutorial, but may be useful soon." 563 | ] 564 | }, 565 | { 566 | "cell_type": "heading", 567 | "level": 2, 568 | "metadata": {}, 569 | "source": [ 570 | "JSAnimation: animated plots" 571 | ] 572 | }, 573 | { 574 | "cell_type": "markdown", 575 | "metadata": {}, 576 | "source": [ 577 | "Thanks to Jake Vanderplas' [JSAnimation](http://nbviewer.ipython.org/github/jakevdp/JSAnimation/blob/master/animation_example.ipynb) library, we have a much better way to include animations of time-dependent solutions. I've been using JSAnimation heavily in my teaching notebooks, but it will soon be replaced by tools based on IPython widgets." 578 | ] 579 | }, 580 | { 581 | "cell_type": "heading", 582 | "level": 3, 583 | "metadata": {}, 584 | "source": [ 585 | "Installing JSAnimation" 586 | ] 587 | }, 588 | { 589 | "cell_type": "markdown", 590 | "metadata": {}, 591 | "source": [ 592 | "If you're running on SageMathCloud, you already have access to JSAnimation through Clawpack, via\n", 593 | "\n", 594 | " from clawpack.visclaw.JSAnimation import IPython_display\n", 595 | " \n", 596 | "If you're working locally, the easiest way to get it is\n", 597 | "\n", 598 | " git clone https://github.com/jakevdp/JSAnimation.git\n", 599 | " cd JSAnimation\n", 600 | " python setup.py install\n", 601 | " \n", 602 | "after which you can\n", 603 | "\n", 604 | " from JSAnimation import IPython_display" 605 | ] 606 | }, 607 | { 608 | "cell_type": "code", 609 | "collapsed": false, 610 | "input": [ 611 | "def pattern_formation(m=10,T=1000):\n", 612 | " # Set up the grid\n", 613 | " a=-1.; b=1.\n", 614 | " h=(b-a)/m; # Grid spacing\n", 615 | " x = np.linspace(a,b,m) # Coordinates\n", 616 | " y = np.linspace(a,b,m)\n", 617 | " Y,X = np.meshgrid(y,x)\n", 618 | "\n", 619 | " # Initial data\n", 620 | " u=np.random.randn(m,m)/2.;\n", 621 | " v=np.random.randn(m,m)/2.;\n", 622 | "\n", 623 | " frames = [u]\n", 624 | " \n", 625 | " u=u.reshape(-1)\n", 626 | " v=v.reshape(-1)\n", 627 | "\n", 628 | " A=five_pt_laplacian_sparse_periodic(m,-1.,1.)\n", 629 | "\n", 630 | " t=0. # Initial time\n", 631 | " k = step_size(h,delta) # Time step size\n", 632 | " N = int(round(T/k)) # Number of steps to take\n", 633 | " \n", 634 | " #Now step forward in time\n", 635 | " next_plot = 0\n", 636 | " for j in range(N):\n", 637 | " #Plot every t=5 units\n", 638 | " if t>=next_plot:\n", 639 | " next_plot = next_plot + 5\n", 640 | " U=u.reshape((m,m))\n", 641 | " frames.append(U)\n", 642 | " \n", 643 | " u,v = one_step(u,v,k,A,delta)\n", 644 | " t = t+k;\n", 645 | " \n", 646 | " return x,y,frames" 647 | ], 648 | "language": "python", 649 | "metadata": {}, 650 | "outputs": [] 651 | }, 652 | { 653 | "cell_type": "code", 654 | "collapsed": false, 655 | "input": [ 656 | "x, y, frames = pattern_formation(m=100,T=200)" 657 | ], 658 | "language": "python", 659 | "metadata": {}, 660 | "outputs": [] 661 | }, 662 | { 663 | "cell_type": "code", 664 | "collapsed": false, 665 | "input": [ 666 | "from matplotlib import animation\n", 667 | "import matplotlib.pyplot as plt\n", 668 | "#from clawpack.visclaw.JSAnimation import IPython_display # Works on SMC\n", 669 | "from JSAnimation import IPython_display\n", 670 | "import numpy as np\n", 671 | "\n", 672 | "fig = plt.figure(figsize=[4,4])\n", 673 | "\n", 674 | "U = frames[0]\n", 675 | "\n", 676 | "# This essentially does a pcolor plot, but it returns the appropriate object\n", 677 | "# for use in animation. See http://matplotlib.org/examples/pylab_examples/pcolor_demo.html.\n", 678 | "# Note that it's necessary to transpose the data array because of the way imshow works.\n", 679 | "plot_handle = plt.imshow(U.T, vmin=U.min(), vmax=U.max(),\n", 680 | " extent=[x.min(), x.max(), y.min(), y.max()],\n", 681 | " interpolation='nearest', origin='lower')\n", 682 | "\n", 683 | "def fplot(frame_number):\n", 684 | " U = frames[frame_number]\n", 685 | " plot_handle.set_data(U.T)\n", 686 | " return plot_handle,\n", 687 | "\n", 688 | "animation.FuncAnimation(fig, fplot, frames=len(frames), interval=20)" 689 | ], 690 | "language": "python", 691 | "metadata": {}, 692 | "outputs": [] 693 | }, 694 | { 695 | "cell_type": "markdown", 696 | "metadata": {}, 697 | "source": [ 698 | "Notice that `FuncAnimation` takes three arguments:\n", 699 | "\n", 700 | "- A matplotlib figure\n", 701 | "- A plotting function\n", 702 | "- A list of frame indices\n", 703 | "\n", 704 | "Using JSAnimation can be a little tricky, because we need to provide a function (called `fplot` here) that returns a handle to a plot of one frame of the solution. It's not enough for `fplot` to simply plot the solution. That's why we first get a handle to the plot and then use `set_data` to modify the plot each time `fplot` is called.\n", 705 | "\n", 706 | "Also, notice that the `frames` argument can be any list. Typically, it would be a list of numbers, but more generally it is just the list of argument values that need to be passed to `fplot`." 707 | ] 708 | }, 709 | { 710 | "cell_type": "markdown", 711 | "metadata": {}, 712 | "source": [ 713 | "See also: IPython widgets (up next)" 714 | ] 715 | } 716 | ], 717 | "metadata": {} 718 | } 719 | ] 720 | } -------------------------------------------------------------------------------- /Multigrid.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "metadata": { 3 | "name": "", 4 | "signature": "sha256:c334619ad18feb0b04faa57eab495e48215ad6b014d4ab8c09697baee5c35f56" 5 | }, 6 | "nbformat": 3, 7 | "nbformat_minor": 0, 8 | "worksheets": [ 9 | { 10 | "cells": [ 11 | { 12 | "cell_type": "heading", 13 | "level": 1, 14 | "metadata": {}, 15 | "source": [ 16 | "Multigrid Lecture and Homework Example" 17 | ] 18 | }, 19 | { 20 | "cell_type": "heading", 21 | "level": 3, 22 | "metadata": {}, 23 | "source": [ 24 | "Aron Ahmadia (US Army ERDC) and David Ketcheson (KAUST)" 25 | ] 26 | }, 27 | { 28 | "cell_type": "heading", 29 | "level": 3, 30 | "metadata": {}, 31 | "source": [ 32 | "Teaching Numerical Methods with IPython Notebooks, SciPy 2014" 33 | ] 34 | }, 35 | { 36 | "cell_type": "markdown", 37 | "metadata": {}, 38 | "source": [ 39 | "\"Creative
This example by Aron Ahmadia and David Ketcheson is licensed under a Creative Commons Attribution 4.0 International License. All code examples are also licensed under the [MIT license](http://opensource.org/licenses/MIT)." 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "collapsed": false, 45 | "input": [ 46 | "from IPython.core.display import HTML\n", 47 | "css_file = './example.css'\n", 48 | "HTML(open(css_file, \"r\").read())" 49 | ], 50 | "language": "python", 51 | "metadata": {}, 52 | "outputs": [ 53 | { 54 | "html": [ 55 | "\n", 56 | "\n", 57 | "\n", 58 | "\n", 59 | "\n", 60 | "\n", 61 | "\n" 171 | ], 172 | "metadata": {}, 173 | "output_type": "pyout", 174 | "prompt_number": 6, 175 | "text": [ 176 | "" 177 | ] 178 | } 179 | ], 180 | "prompt_number": 6 181 | }, 182 | { 183 | "cell_type": "markdown", 184 | "metadata": {}, 185 | "source": [ 186 | "# Multigrid\n", 187 | "\n", 188 | "Multigrid is one of the great algorithms for numerically solving PDEs. It is also one of the few essentially optimal algorithms for solving a linear system of equations, since it computes the solution of an $N\\times N$ system in ${\\mathcal O}(N\\log(N))$ -- or even just ${\\mathcal O}(N)$ -- operations. \n", 189 | "\n", 190 | "This notebook is meant to accompany a reading of [Section 4.6 of Randall LeVeque's text on finite difference methods](http://0-epubs.siam.org.library.kaust.edu.sa/doi/abs/10.1137/1.9780898717839.ch4). Other good resources are cited there." 191 | ] 192 | }, 193 | { 194 | "cell_type": "code", 195 | "collapsed": false, 196 | "input": [ 197 | "%matplotlib inline\n", 198 | "import numpy as np\n", 199 | "import matplotlib.pyplot as plt\n", 200 | "from matplotlib import animation\n", 201 | "from clawpack.visclaw.JSAnimation import IPython_display" 202 | ], 203 | "language": "python", 204 | "metadata": {}, 205 | "outputs": [] 206 | }, 207 | { 208 | "cell_type": "markdown", 209 | "metadata": {}, 210 | "source": [ 211 | "## A two-point boundary value problem\n", 212 | "Let's use Jacobi's method to solve the one-dimensional boundary value problem \n", 213 | "\n", 214 | "\\begin{align}\n", 215 | "u''(x) & = f(x) & 0\"Creative
This lecture by Aron Ahmadia and David Ketcheson is licensed under a Creative Commons Attribution 4.0 International License. All code examples are also licensed under the [MIT license](http://opensource.org/licenses/MIT)." 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "collapsed": false, 54 | "input": [ 55 | "from IPython.core.display import HTML\n", 56 | "css_file = './example.css'\n", 57 | "HTML(open(css_file, \"r\").read())" 58 | ], 59 | "language": "python", 60 | "metadata": {}, 61 | "outputs": [ 62 | { 63 | "html": [ 64 | "\n", 65 | "\n", 66 | "\n", 67 | "\n", 68 | "\n", 69 | "\n", 70 | "\n" 180 | ], 181 | "metadata": {}, 182 | "output_type": "pyout", 183 | "prompt_number": 1, 184 | "text": [ 185 | "" 186 | ] 187 | } 188 | ], 189 | "prompt_number": 1 190 | }, 191 | { 192 | "cell_type": "heading", 193 | "level": 2, 194 | "metadata": { 195 | "slideshow": { 196 | "slide_type": "notes" 197 | } 198 | }, 199 | "source": [ 200 | "What is this?" 201 | ] 202 | }, 203 | { 204 | "cell_type": "markdown", 205 | "metadata": { 206 | "slideshow": { 207 | "slide_type": "notes" 208 | } 209 | }, 210 | "source": [ 211 | "This is a gentle introduction to the IPython Notebook aimed at lecturers who wish to incorporate it in their teaching, written in an IPython Notebook. This presentation adapts material from the [IPython official documentation](http://nbviewer.ipython.org/github/ipython/ipython/blob/2.x/examples/Notebook)." 212 | ] 213 | }, 214 | { 215 | "cell_type": "heading", 216 | "level": 2, 217 | "metadata": { 218 | "slideshow": { 219 | "slide_type": "slide" 220 | } 221 | }, 222 | "source": [ 223 | "What is an IPython Notebook?" 224 | ] 225 | }, 226 | { 227 | "cell_type": "markdown", 228 | "metadata": { 229 | "slideshow": { 230 | "slide_type": "fragment" 231 | } 232 | }, 233 | "source": [ 234 | "An IPython Notebook is a:\n", 235 | "\n", 236 | "**[A]** Interactive environment for writing and running code \n", 237 | "**[B]** Weave of code, data, prose, equations, analysis, and visualization \n", 238 | "**[C]** Tool for prototyping new code and analysis \n", 239 | "**[D]** Reproducible workflow for scientific research \n" 240 | ] 241 | }, 242 | { 243 | "cell_type": "markdown", 244 | "metadata": { 245 | "slideshow": { 246 | "slide_type": "fragment" 247 | } 248 | }, 249 | "source": [ 250 | "**[E]** **All of the above**" 251 | ] 252 | }, 253 | { 254 | "cell_type": "heading", 255 | "level": 3, 256 | "metadata": { 257 | "slideshow": { 258 | "slide_type": "slide" 259 | } 260 | }, 261 | "source": [ 262 | "Writing and Running Code" 263 | ] 264 | }, 265 | { 266 | "cell_type": "markdown", 267 | "metadata": { 268 | "slideshow": { 269 | "slide_type": "fragment" 270 | } 271 | }, 272 | "source": [ 273 | "The IPython Notebook consists of an ordered list of cells. \n", 274 | "\n", 275 | "There are four important cell types:\n", 276 | "\n", 277 | "* **Code**\n", 278 | "* **Markdown**\n", 279 | "* **Heading**\n", 280 | "* **Raw**" 281 | ] 282 | }, 283 | { 284 | "cell_type": "markdown", 285 | "metadata": { 286 | "slideshow": { 287 | "slide_type": "fragment" 288 | } 289 | }, 290 | "source": [ 291 | "We briefly introduce how Code Cells work here. We will return to the other three cell types later." 292 | ] 293 | }, 294 | { 295 | "cell_type": "heading", 296 | "level": 3, 297 | "metadata": { 298 | "slideshow": { 299 | "slide_type": "slide" 300 | } 301 | }, 302 | "source": [ 303 | "Code Cells" 304 | ] 305 | }, 306 | { 307 | "cell_type": "code", 308 | "collapsed": false, 309 | "input": [ 310 | "# This is a code cell made up of Python comments\n", 311 | "# We can execute it by clicking on it with the mouse\n", 312 | "# then clicking the \"Run Cell\" button" 313 | ], 314 | "language": "python", 315 | "metadata": { 316 | "slideshow": { 317 | "slide_type": "fragment" 318 | } 319 | }, 320 | "outputs": [], 321 | "prompt_number": 1 322 | }, 323 | { 324 | "cell_type": "code", 325 | "collapsed": false, 326 | "input": [ 327 | "# A comment is a pretty boring piece of code\n", 328 | "# This code cell generates \"Hello, World\" when executed\n", 329 | "\n", 330 | "print \"Hello, World\"" 331 | ], 332 | "language": "python", 333 | "metadata": { 334 | "slideshow": { 335 | "slide_type": "fragment" 336 | } 337 | }, 338 | "outputs": [ 339 | { 340 | "output_type": "stream", 341 | "stream": "stdout", 342 | "text": [ 343 | "Hello, World\n" 344 | ] 345 | } 346 | ], 347 | "prompt_number": 2 348 | }, 349 | { 350 | "cell_type": "code", 351 | "collapsed": false, 352 | "input": [ 353 | "# Code cells can also generate graphical output\n", 354 | "%matplotlib inline\n", 355 | "import matplotlib\n", 356 | "matplotlib.pyplot.hist([0, 1, 2, 2, 3, 3, 3, 4, 4, 4, 10]);" 357 | ], 358 | "language": "python", 359 | "metadata": { 360 | "slideshow": { 361 | "slide_type": "subslide" 362 | } 363 | }, 364 | "outputs": [ 365 | { 366 | "metadata": {}, 367 | "output_type": "display_data", 368 | "png": "iVBORw0KGgoAAAANSUhEUgAAAXUAAAEACAYAAABMEua6AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAADYRJREFUeJzt3G+MHPV9x/H3cGfyR6eLayE5wb7kKgNRkKpgiByH0nrb\npBJYqfOEB0StqOiDIBQESaSEECH5njWpVKWiCGJVATlNC41IhExq2pKIBaQobhTbF/O3tksU/1FM\nFGNkbKmxYfLgN+dbr9fe2fV4Z/zd90sa3W92frf71Xnvs7/7zoxBkiRJkiRJkiRJkiRJkiSp8d4N\nbAN2Ai8Bf3eWefcDu4F5YPVoSpMkDeO9xddJ4KfADV3H1wNbi/HHizmSpBpcUmLO8eLrpcAEcLjr\n+AZgczHeBiwFlldSnSRpIGVC/RJS++UQ8AypDdNpBbCvY38/sLKS6iRJAykT6u8A15CC+k+BVo85\nWdd+fn5lSZKGMTnA3DeB/wA+BrQ7Hj8AzHTsryweO82qVavyvXv3DlGiJI21vcAVZSf3W6lfRuqR\nA7wH+AtgR9ecLcCtxXgtcITUqjm9qr17yfPcLc/ZuHFj7TUkeQO2JtRB7f8eTXlfNGXzZ7G4Aau6\n8/Rc+q3UP0A6CXpJsf0L8GPg9uL4JtKVL+uBPcAx4LZBCpAkVadfqO8Cru3x+Kau/TurKUeSdD7K\nnChVxVqtVt0lqIF8XyzyZzG87qtWLqS86A+pAbIsoxkXKTWhjgzfm2qq9LtaPqtdqUtSIIa6JAVi\nqEtSIIa6JAViqEtSIIa6JAViqEtSIIa6JAViqEtSIIa6JAViqEtSIIa6JAViqEtSIIa6JAViqEtS\nIIa6JAViqEtSIIa6JAViqEtSIIa6JAViqEtSIIa6JAViqEtSIIa6JAViqEtSIIa6JAXSL9RngGeA\nF4EXgLt6zGkBbwI7iu2+CuuTJA1gss/xE8AXgZ3AFPBz4Gng5a55zwIbKq9OkjSQfiv1X5MCHeAt\nUphf3mNeVmVRkqThDNJTnwVWA9u6Hs+B64F5YCtwdSWVSZIG1q/9smAKeBy4m7Ri77Sd1Hs/DtwE\nPAFcVVWBkqTyyoT6EuD7wHdJgd3taMf4KeBBYBlwuHvi3NzcqXGr1aLVapWvVJLGQLvdpt1uD/39\n/XrhGbAZ+C3phGkvy4HXSW2YNcD3SK2abnme58NVqcplWUb6J6tbE+rI8L2ppkq/q+XPW/Zbqf8x\n8NfAL0iXKwJ8DfhgMd4E3AzcAZwktWBuKV+uJKlKo7xqxZV6g7hSP70G35tqqkFX6t5RKkmBGOqS\nFIihLkmBGOqSFIihLkmBGOqSFIihLkmBGOqSFIihLkmBGOqSFIihLkmBGOqSFIihLkmBGOqSFIih\nLkmBGOqSFIihLkmBGOqSFIihLkmBGOqSFIihLkmBGOqSFIihLkmBGOqSFIihLkmBGOqSFIihLkmB\nGOqSFIihLkmB9Av1GeAZ4EXgBeCus8y7H9gNzAOrK6tOkjSQyT7HTwBfBHYCU8DPgaeBlzvmrAeu\nAK4EPg48BKytvFJJUl/9Vuq/JgU6wFukML+8a84GYHMx3gYsBZZXVaAkqbxBeuqzpNbKtq7HVwD7\nOvb3AyvPryxJ0jD6tV8WTAGPA3eTVuzdsq79vNeTzM3NnRq3Wi1arVbJl49lenoZR4++UXcZkhqo\n3W7TbreH/v7uMO5lCfBD4CngH3sc/xbQBh4r9l8B1gGHuubled4z68dOlmWc5XNvlFU0oAZoRh0Z\nvjfVVCkvSmU10L/9kgHfBl6id6ADbAFuLcZrgSOcGeiSpBHol/43AM8Bv2BxOfU14IPFeFPx9QHg\nRuAYcBuwvcdzuVIvuFLv1IQ6XKmruQZdqZeeWAFDvWCod2pCHYa6mqvq9osk6SJiqEtSIIa6JAVi\nqEtSIIa6JAViqEtSIIa6JAViqEtSIIa6JAViqEtSIIa6JAViqEtSIIa6JAViqEtSIIa6JAViqEtS\nIIa6JAViqEtSIIa6JAViqEtSIIa6JAViqEtSIIa6JAViqEtSIIa6JAViqEtSIIa6JAViqEtSIGVC\n/WHgELDrLMdbwJvAjmK7r5LKJEkDmywx5xHgn4DvnGPOs8CGSiqSJA2tzEr9eeCNPnOyCmqRJJ2n\nKnrqOXA9MA9sBa6u4DklSUMo037pZzswAxwHbgKeAK7qNXFubu7UuNVq0Wq1Knh5SYqj3W7TbreH\n/v6ybZNZ4Engj0rMfQ24Djjc9Xie53n5ygLLsoz0B06tVTSgBmhGHRm+N9VUKS/Kt7iraL8s73jB\nNcW4O9AlSSNQpv3yKLAOuAzYB2wElhTHNgE3A3cAJ0ktmFuqL1OSVMYor1qx/VKw/dKpCXXYflFz\n1dF+kSQ1hKEuSYEY6pIUiKEuSYEY6pIUiKEuSYEY6pIUiKEuSYEY6pIUiKEuSYEY6pIUiKEuSYEY\n6pIUiKEuSYEY6pIUiKEuSYEY6pIUiKEuSYEY6pIUiKEuSYEY6pIUiKEuSYEY6pIUiKEuSYEY6pIU\niKEuSYEY6pIUiKEuSYGUCfWHgUPArnPMuR/YDcwDqyuoS5I0hDKh/ghw4zmOrweuAK4EPgc8VEFd\nkqQhlAn154E3znF8A7C5GG8DlgLLz7MuSdIQJit4jhXAvo79/cBKUsvmNAcPHqzg5YY3PT3N1NRU\nrTVI0oVURagDZF37ea9JH/rQh0+NJyYuZWLiXRW9fH9vv/07Tpx4i3fe+f+Rvaaki9f09DKOHj1X\nk6KZqgj1A8BMx/7K4rEznDx5tGNcwSsPZDtwHWf5vBmx7s9ASU2TAv3iy4sqLmncAtxajNcCR+jR\nepEkXXhlVuqPAuuAy0i9843AkuLYJmAr6QqYPcAx4Lbqy5QklTHKPkBe758yTWu/1F1HE2qAZtSR\nked116CmybImvDehiOnSWe0dpZIUiKEuSYEY6pIUiKEuSYEY6pIUiKEuSYEY6pIUiKEuSYEY6pIU\niKEuSYEY6pIUiKEuSYEY6pIUiKEuSYEY6pIUiKEuSYEY6pIUiKEuSYEY6pIUiKEuSYEY6pIUiKEu\nSYEY6pIUiKEuSYEY6pIUiKEuSYEY6pIUiKEuSYGUCfUbgVeA3cA9PY63gDeBHcV2X1XFSZIGM9nn\n+ATwAPAp4ADwM2AL8HLXvGeBDZVXJ0kaSL+V+hpgD/BL4ATwGPCZHvOyasuSJA2jX6ivAPZ17O8v\nHuuUA9cD88BW4OrKqpMkDaRf+yUv8RzbgRngOHAT8ARw1XnWJUkaQr9QP0AK7AUzpNV6p6Md46eA\nB4FlwOEzn26uY9wqNknSonaxDadfL3wSeBX4JHAQ+B/gs5x+onQ58DppVb8G+B4w2+O58nIL/wtl\nO3Ad9dawIKP+OppQAzSjjow8r7sGNU2WNeG9CUVMlz5v2W+lfhK4E/gv0pUw3yYF+u3F8U3AzcAd\nxdzjwC0D1StJqswor1pxpX5KE1YATagBmlGHK3Wd6WJdqXtHqSQFYqhLUiCGuiQFYqhLUiCGuiQF\nYqhLUiCGuiQFYqhLUiCGuiQFYqhLUiCGuiQFYqhLUiCGuiQFYqhLUiCGuiQFYqhLUiCGuiQFYqhL\nUiCGuiQFYqhLUiCGuiQFYqhLUiCGuiQFYqhLUiCGuiQFYqhLUiCGuiQFYqhLUiBlQv1G4BVgN3DP\nWebcXxyfB1ZXU5okaVD9Qn0CeIAU7FcDnwU+0jVnPXAFcCXwOeChimuUxkK73a67hMbwZzG8fqG+\nBtgD/BI4ATwGfKZrzgZgczHeBiwFlldXojQeDLJF/iyG1y/UVwD7Ovb3F4/1m7Py/EuTJA1qss/x\nvOTzZGW+b3r6L0s+XfXefvsIx47V9vKSNBLdYdxtLTBH6qkD3Au8A3yjY863gDapNQPppOo64FDX\nc+0BVg1fqiSNpb2k85aVmCyecBa4FNhJ7xOlW4vxWuCnVb24JKl6NwGvklba9xaP3V5sCx4ojs8D\n1460OkmSJEnDKXPz0jiYAZ4BXgReAO6qt5xGmAB2AE/WXUjNlgKPAy8DL5HamOPqXtLvyC7g34B3\n1VvOSD1MOhe5q+OxZcDTwP8C/016r9RqgtSWmQWW0LsnPy7eD1xTjKdILa1x/Vks+BLwr8CWugup\n2Wbgb4vxJPC+Gmup0yzwfywG+b8Df1NbNaP3J6Q78jtD/e+BrxTje4Cvj7qobp8A/rNj/6vFJngC\n+GTdRdRoJfAj4M8Y75X6+0hBprQqfRX4A9KH25PAp2qtaPRmOT3UX2HxZs73F/vndKH/Q68yNy+N\no1nSJ/K2muuo0zeBL5MukR1nfwj8BngE2A78M/DeWiuqz2HgH4BfAQeBI6QP/nG2nMXLww9R4m79\nCx3qZW9eGidTpP7p3cBbNddSl08Dr5P66f3ulYhuknTF2IPF12OM71+zq4AvkBY9l5N+V/6qzoIa\nJqdEpl7oUD9AOkG4YIa0Wh9XS4DvA98ltV/G1fWk/zPoNeBR4M+B79RaUX32F9vPiv3HGd/Lgj8G\n/AT4LXAS+AHpvTLODpHaLgAfIC2GalXm5qVxkZGC65t1F9Iw6xjvnjrAc8BVxXiO0+/YHicfJV0Z\n9h7S78tm4PO1VjR6s5x5onThqsGv0oATpdD75qVxdAOpf7yT1HbYweJ/vzDO1uHVLx8lrdTnSavT\ncb36BdKVHguXNG4m/XU7Lh4lnUv4Helc5G2kk8c/okGXNEqSJEmSJEmSJEmSJEmSJEmSJEm6SP0e\nKj+Dtwgqq5YAAAAASUVORK5CYII=\n", 369 | "text": [ 370 | "" 371 | ] 372 | } 373 | ], 374 | "prompt_number": 3 375 | }, 376 | { 377 | "cell_type": "heading", 378 | "level": 2, 379 | "metadata": { 380 | "slideshow": { 381 | "slide_type": "slide" 382 | } 383 | }, 384 | "source": [ 385 | "Modal editor" 386 | ] 387 | }, 388 | { 389 | "cell_type": "markdown", 390 | "metadata": {}, 391 | "source": [ 392 | "Starting with IPython 2.0, the IPython Notebook has a modal user interface. This means that the keyboard does different things depending on which mode the Notebook is in. There are two modes: edit mode and command mode." 393 | ] 394 | }, 395 | { 396 | "cell_type": "heading", 397 | "level": 3, 398 | "metadata": { 399 | "slideshow": { 400 | "slide_type": "subslide" 401 | } 402 | }, 403 | "source": [ 404 | "Edit mode" 405 | ] 406 | }, 407 | { 408 | "cell_type": "markdown", 409 | "metadata": {}, 410 | "source": [ 411 | "Edit mode is indicated by a green cell border and a prompt showing in the editor area:\n", 412 | "\n", 413 | "\n", 414 | "\n", 415 | "When a cell is in edit mode, you can type into the cell, like a normal text editor." 416 | ] 417 | }, 418 | { 419 | "cell_type": "markdown", 420 | "metadata": { 421 | "slideshow": { 422 | "slide_type": "fragment" 423 | } 424 | }, 425 | "source": [ 426 | "
\n", 427 | "Enter edit mode by pressing `enter` or using the mouse to click on a cell's editor area.\n", 428 | "
" 429 | ] 430 | }, 431 | { 432 | "cell_type": "markdown", 433 | "metadata": { 434 | "slideshow": { 435 | "slide_type": "fragment" 436 | } 437 | }, 438 | "source": [ 439 | "
\n", 440 | "While in edit mode, tab-completion works for variables the kernel knows about from executing previous cells.\n", 441 | "
" 442 | ] 443 | }, 444 | { 445 | "cell_type": "heading", 446 | "level": 3, 447 | "metadata": { 448 | "slideshow": { 449 | "slide_type": "subslide" 450 | } 451 | }, 452 | "source": [ 453 | "Command mode" 454 | ] 455 | }, 456 | { 457 | "cell_type": "markdown", 458 | "metadata": {}, 459 | "source": [ 460 | "Command mode is indicated by a grey cell border:\n", 461 | "\n", 462 | "\n", 463 | "\n", 464 | "When you are in command mode, you are able to edit the notebook as a whole, but not type into individual cells. Most importantly, in command mode, the keyboard is mapped to a set of shortcuts that let you perform notebook and cell actions efficiently. For example, if you are in command mode and you press `c`, you will copy the current cell - no modifier is needed." 465 | ] 466 | }, 467 | { 468 | "cell_type": "markdown", 469 | "metadata": { 470 | "slideshow": { 471 | "slide_type": "fragment" 472 | } 473 | }, 474 | "source": [ 475 | "
\n", 476 | "Don't try to type into a cell in command mode; unexpected things will happen!\n", 477 | "
" 478 | ] 479 | }, 480 | { 481 | "cell_type": "markdown", 482 | "metadata": { 483 | "slideshow": { 484 | "slide_type": "fragment" 485 | } 486 | }, 487 | "source": [ 488 | "
\n", 489 | "Enter command mode by pressing `esc` or using the mouse to click *outside* a cell's editor area.\n", 490 | "
" 491 | ] 492 | }, 493 | { 494 | "cell_type": "heading", 495 | "level": 2, 496 | "metadata": { 497 | "slideshow": { 498 | "slide_type": "slide" 499 | } 500 | }, 501 | "source": [ 502 | "Mouse navigation" 503 | ] 504 | }, 505 | { 506 | "cell_type": "markdown", 507 | "metadata": {}, 508 | "source": [ 509 | "All navigation and actions in the Notebook are available using the mouse through the menubar and toolbar, which are both above the main Notebook area:\n", 510 | "\n", 511 | "" 512 | ] 513 | }, 514 | { 515 | "cell_type": "markdown", 516 | "metadata": { 517 | "slideshow": { 518 | "slide_type": "notes" 519 | } 520 | }, 521 | "source": [ 522 | "The first idea of mouse based navigation is that **cells can be selected by clicking on them.** The currently selected cell gets a grey or green border depending on whether the notebook is in edit or command mode. If you click inside a cell's editor area, you will enter edit mode. If you click on the prompt or output area of a cell you will enter command mode.\n", 523 | "\n", 524 | "If you are running this notebook in a live session (not on http://nbviewer.ipython.org) try selecting different cells and going between edit and command mode. Try typing into a cell." 525 | ] 526 | }, 527 | { 528 | "cell_type": "markdown", 529 | "metadata": { 530 | "slideshow": { 531 | "slide_type": "notes" 532 | } 533 | }, 534 | "source": [ 535 | "The second idea of mouse based navigation is that **cell actions usually apply to the currently selected cell**. Thus if you want to run the code in a cell, you would select it and click the \"Play\" button in the toolbar or the \"Cell:Run\" menu item. Similarly, to copy a cell you would select it and click the \"Copy\" button in the toolbar or the \"Edit:Copy\" menu item. With this simple pattern, you should be able to do most everything you need with the mouse.\n", 536 | "\n", 537 | "Markdown and heading cells have one other state that can be modified with the mouse. These cells can either be rendered or unrendered. When they are rendered, you will see a nice formatted representation of the cell's contents. When they are unrendered, you will see the raw text source of the cell. To render the selected cell with the mouse, click the \"Play\" button in the toolbar or the \"Cell:Run\" menu item. To unrender the selected cell, double click on the cell." 538 | ] 539 | }, 540 | { 541 | "cell_type": "heading", 542 | "level": 2, 543 | "metadata": { 544 | "slideshow": { 545 | "slide_type": "slide" 546 | } 547 | }, 548 | "source": [ 549 | "Keyboard Navigation" 550 | ] 551 | }, 552 | { 553 | "cell_type": "markdown", 554 | "metadata": { 555 | "slideshow": { 556 | "slide_type": "fragment" 557 | } 558 | }, 559 | "source": [ 560 | "The modal user interface of the IPython Notebook has been optimized for efficient keyboard usage. This is made possible by having two different sets of keyboard shortcuts: one set that is active in edit mode and another in command mode." 561 | ] 562 | }, 563 | { 564 | "cell_type": "markdown", 565 | "metadata": { 566 | "slideshow": { 567 | "slide_type": "fragment" 568 | } 569 | }, 570 | "source": [ 571 | "The most important keyboard shortcuts are `enter`, which enters edit mode, and `esc`, which enters command mode.\n", 572 | "\n", 573 | "In edit mode, most of the keyboard is dedicated to typing into the cell's editor. Thus, in edit mode there are relatively few shortcuts:" 574 | ] 575 | }, 576 | { 577 | "cell_type": "markdown", 578 | "metadata": {}, 579 | "source": [ 580 | "In command mode, the entire keyboard is available for shortcuts:" 581 | ] 582 | }, 583 | { 584 | "cell_type": "markdown", 585 | "metadata": { 586 | "slideshow": { 587 | "slide_type": "slide" 588 | } 589 | }, 590 | "source": [ 591 | "Here the rough order in which the IPython Developers recommend learning the command mode shortcuts:\n", 592 | "\n", 593 | "1. Basic navigation: `enter`, `shift-enter`, `up/k`, `down/j`\n", 594 | "2. Saving the notebook: `s`\n", 595 | "2. Cell types: `y`, `m`, `1-6`, `t`\n", 596 | "3. Cell creation and movement: `a`, `b`, `ctrl+k`, `ctrl+j`\n", 597 | "4. Cell editing: `x`, `c`, `v`, `d`, `z`, `shift+=`\n", 598 | "5. Kernel operations: `i`, `0`" 599 | ] 600 | }, 601 | { 602 | "cell_type": "markdown", 603 | "metadata": { 604 | "slideshow": { 605 | "slide_type": "fragment" 606 | } 607 | }, 608 | "source": [ 609 | "Aron and David humbly suggest learning `h` first!" 610 | ] 611 | }, 612 | { 613 | "cell_type": "heading", 614 | "level": 2, 615 | "metadata": { 616 | "slideshow": { 617 | "slide_type": "slide" 618 | } 619 | }, 620 | "source": [ 621 | "The IPython Notebook Architecture" 622 | ] 623 | }, 624 | { 625 | "cell_type": "markdown", 626 | "metadata": { 627 | "slideshow": { 628 | "slide_type": "notes" 629 | } 630 | }, 631 | "source": [ 632 | "So far, we have learned the basics of using IPython Notebooks.\n", 633 | "\n", 634 | "For simple demonstrations, the typical user doesn't need to understand how the computations are being handled, but to successfully write and present computational notebooks, **you** will need to understand how the notebook architecture works." 635 | ] 636 | }, 637 | { 638 | "cell_type": "markdown", 639 | "metadata": { 640 | "slideshow": { 641 | "slide_type": "notes" 642 | } 643 | }, 644 | "source": [ 645 | "A *live* notebook is composed of an interactive web page (the front end), a running IPython session (the kernel or back end), and a web server responsible for handling communication between the two (the, err..., middle-end)" 646 | ] 647 | }, 648 | { 649 | "cell_type": "markdown", 650 | "metadata": { 651 | "slideshow": { 652 | "slide_type": "notes" 653 | } 654 | }, 655 | "source": [ 656 | "A *static* notebook, as for example seen on NBViewer, is a static view of the notebook's content. The default format is HTML, but a notebook can also be output in PDF or other formats." 657 | ] 658 | }, 659 | { 660 | "cell_type": "markdown", 661 | "metadata": {}, 662 | "source": [ 663 | "The centerpiece of an IPython Notebook is the \"kernel\", the IPython instance responsible for executing all code. Your IPython kernel maintains its state between executed cells." 664 | ] 665 | }, 666 | { 667 | "cell_type": "code", 668 | "collapsed": false, 669 | "input": [ 670 | "x = 0\n", 671 | "print x" 672 | ], 673 | "language": "python", 674 | "metadata": { 675 | "slideshow": { 676 | "slide_type": "subslide" 677 | } 678 | }, 679 | "outputs": [ 680 | { 681 | "output_type": "stream", 682 | "stream": "stdout", 683 | "text": [ 684 | "0\n" 685 | ] 686 | } 687 | ], 688 | "prompt_number": 4 689 | }, 690 | { 691 | "cell_type": "code", 692 | "collapsed": false, 693 | "input": [ 694 | "x += 1\n", 695 | "print x" 696 | ], 697 | "language": "python", 698 | "metadata": { 699 | "slideshow": { 700 | "slide_type": "fragment" 701 | } 702 | }, 703 | "outputs": [ 704 | { 705 | "output_type": "stream", 706 | "stream": "stdout", 707 | "text": [ 708 | "1\n" 709 | ] 710 | } 711 | ], 712 | "prompt_number": 5 713 | }, 714 | { 715 | "cell_type": "markdown", 716 | "metadata": { 717 | "slideshow": { 718 | "slide_type": "notes" 719 | } 720 | }, 721 | "source": [ 722 | "There are two important actions for interacting with the kernel. The first is to interrupt it. This is the same as sending a Control-C from the command line. The second is to restart it. This completely terminates the kernel and starts it anew. None of the kernel state is saved across a restart. " 723 | ] 724 | }, 725 | { 726 | "cell_type": "heading", 727 | "level": 2, 728 | "metadata": { 729 | "slideshow": { 730 | "slide_type": "slide" 731 | } 732 | }, 733 | "source": [ 734 | "Markdown cells" 735 | ] 736 | }, 737 | { 738 | "cell_type": "markdown", 739 | "metadata": {}, 740 | "source": [ 741 | "Text can be added to IPython Notebooks using Markdown cells. Markdown is a popular markup language that is a superset of HTML. Its specification can be found here:\n", 742 | "\n", 743 | "" 744 | ] 745 | }, 746 | { 747 | "cell_type": "heading", 748 | "level": 2, 749 | "metadata": { 750 | "slideshow": { 751 | "slide_type": "slide" 752 | } 753 | }, 754 | "source": [ 755 | "Markdown basics" 756 | ] 757 | }, 758 | { 759 | "cell_type": "heading", 760 | "level": 3, 761 | "metadata": { 762 | "slideshow": { 763 | "slide_type": "subslide" 764 | } 765 | }, 766 | "source": [ 767 | "Text formatting" 768 | ] 769 | }, 770 | { 771 | "cell_type": "markdown", 772 | "metadata": { 773 | "slideshow": { 774 | "slide_type": "fragment" 775 | } 776 | }, 777 | "source": [ 778 | "You can make text *italic* or **bold**." 779 | ] 780 | }, 781 | { 782 | "cell_type": "heading", 783 | "level": 3, 784 | "metadata": { 785 | "slideshow": { 786 | "slide_type": "subslide" 787 | } 788 | }, 789 | "source": [ 790 | "Itemized Lists" 791 | ] 792 | }, 793 | { 794 | "cell_type": "markdown", 795 | "metadata": {}, 796 | "source": [ 797 | "* One\n", 798 | " - Sublist\n", 799 | " - This\n", 800 | " - Sublist\n", 801 | " - That\n", 802 | " - The other thing\n", 803 | "* Two\n", 804 | " - Sublist\n", 805 | "* Three\n", 806 | " - Sublist" 807 | ] 808 | }, 809 | { 810 | "cell_type": "heading", 811 | "level": 3, 812 | "metadata": { 813 | "slideshow": { 814 | "slide_type": "subslide" 815 | } 816 | }, 817 | "source": [ 818 | "Enumerated Lists" 819 | ] 820 | }, 821 | { 822 | "cell_type": "markdown", 823 | "metadata": {}, 824 | "source": [ 825 | "1. Here we go\n", 826 | " 1. Sublist\n", 827 | " 2. Sublist\n", 828 | "2. There we go\n", 829 | "3. Now this" 830 | ] 831 | }, 832 | { 833 | "cell_type": "heading", 834 | "level": 3, 835 | "metadata": { 836 | "slideshow": { 837 | "slide_type": "subslide" 838 | } 839 | }, 840 | "source": [ 841 | "Horizontal Rules" 842 | ] 843 | }, 844 | { 845 | "cell_type": "markdown", 846 | "metadata": {}, 847 | "source": [ 848 | "---\n", 849 | "\n", 850 | "---\n", 851 | "\n", 852 | "---" 853 | ] 854 | }, 855 | { 856 | "cell_type": "heading", 857 | "level": 3, 858 | "metadata": { 859 | "slideshow": { 860 | "slide_type": "subslide" 861 | } 862 | }, 863 | "source": [ 864 | "Blockquotes" 865 | ] 866 | }, 867 | { 868 | "cell_type": "markdown", 869 | "metadata": {}, 870 | "source": [ 871 | "> To me programming is more than an important practical art. It is also a gigantic undertaking in the foundations of knowledge. -- Rear Admiral Grace Hopper" 872 | ] 873 | }, 874 | { 875 | "cell_type": "heading", 876 | "level": 3, 877 | "metadata": { 878 | "slideshow": { 879 | "slide_type": "subslide" 880 | } 881 | }, 882 | "source": [ 883 | "Links" 884 | ] 885 | }, 886 | { 887 | "cell_type": "markdown", 888 | "metadata": {}, 889 | "source": [ 890 | "[IPython's website](http://ipython.org)" 891 | ] 892 | }, 893 | { 894 | "cell_type": "heading", 895 | "level": 3, 896 | "metadata": { 897 | "slideshow": { 898 | "slide_type": "subslide" 899 | } 900 | }, 901 | "source": [ 902 | "Code" 903 | ] 904 | }, 905 | { 906 | "cell_type": "markdown", 907 | "metadata": {}, 908 | "source": [ 909 | " def f(x):\n", 910 | " \"\"\"a docstring\"\"\"\n", 911 | " return x**2" 912 | ] 913 | }, 914 | { 915 | "cell_type": "markdown", 916 | "metadata": { 917 | "slideshow": { 918 | "slide_type": "fragment" 919 | } 920 | }, 921 | "source": [ 922 | "You can also use triple-backticks to denote code blocks.\n", 923 | "This also allows you to choose the appropriate syntax highlighter.\n", 924 | "\n", 925 | "```C\n", 926 | "if (i=0; i\n", 1011 | " " 1012 | ], 1013 | "metadata": {}, 1014 | "output_type": "pyout", 1015 | "prompt_number": 6, 1016 | "text": [ 1017 | "" 1018 | ] 1019 | } 1020 | ], 1021 | "prompt_number": 6 1022 | }, 1023 | { 1024 | "cell_type": "heading", 1025 | "level": 3, 1026 | "metadata": { 1027 | "slideshow": { 1028 | "slide_type": "slide" 1029 | } 1030 | }, 1031 | "source": [ 1032 | "Other HTML" 1033 | ] 1034 | }, 1035 | { 1036 | "cell_type": "markdown", 1037 | "metadata": {}, 1038 | "source": [ 1039 | " Be Bold! " 1040 | ] 1041 | }, 1042 | { 1043 | "cell_type": "heading", 1044 | "level": 2, 1045 | "metadata": { 1046 | "slideshow": { 1047 | "slide_type": "slide" 1048 | } 1049 | }, 1050 | "source": [ 1051 | "Mathematical Equations" 1052 | ] 1053 | }, 1054 | { 1055 | "cell_type": "markdown", 1056 | "metadata": { 1057 | "slideshow": { 1058 | "slide_type": "fragment" 1059 | } 1060 | }, 1061 | "source": [ 1062 | "Courtesy of MathJax, you can beautifully render mathematical expressions, both inline: \n", 1063 | "$e^{i\\pi} + 1 = 0$, and displayed:\n", 1064 | "\n", 1065 | "$$e^x=\\sum_{i=0}^\\infty \\frac{1}{i!}x^i$$" 1066 | ] 1067 | }, 1068 | { 1069 | "cell_type": "heading", 1070 | "level": 3, 1071 | "metadata": { 1072 | "slideshow": { 1073 | "slide_type": "subslide" 1074 | } 1075 | }, 1076 | "source": [ 1077 | "Equation Environments" 1078 | ] 1079 | }, 1080 | { 1081 | "cell_type": "markdown", 1082 | "metadata": {}, 1083 | "source": [ 1084 | "You can also use a number of equation environments, such as `align`:\n", 1085 | "\n", 1086 | "\\begin{align}\n", 1087 | " x &= 4 \\\\\n", 1088 | "y+z &= x\n", 1089 | "\\end{align}\n", 1090 | "\n", 1091 | "[A full list of available TeX and LaTeX commands is maintained by Dr. Carol Burns.](http://www.onemathematicalcat.org/MathJaxDocumentation/TeXSyntax.htm)" 1092 | ] 1093 | }, 1094 | { 1095 | "cell_type": "heading", 1096 | "level": 3, 1097 | "metadata": { 1098 | "slideshow": { 1099 | "slide_type": "subslide" 1100 | } 1101 | }, 1102 | "source": [ 1103 | "Other Useful MathJax Notes" 1104 | ] 1105 | }, 1106 | { 1107 | "cell_type": "markdown", 1108 | "metadata": {}, 1109 | "source": [ 1110 | "* inline math is demarcated by `$ $`, or `\\( \\)`\n", 1111 | "* displayed math is demarcated by `$$ $$` or `\\[ \\]`\n", 1112 | "* displayed math environments can also be directly demarcated by `\\begin` and `\\end`\n", 1113 | "* `\\newcommand` and `\\def` are supported, *within* areas MathJax processes (such as in a `\\[ \\]` block)\n", 1114 | "* equation numbering is not officially supported, but it can be indirectly enabled" 1115 | ] 1116 | }, 1117 | { 1118 | "cell_type": "heading", 1119 | "level": 2, 1120 | "metadata": { 1121 | "slideshow": { 1122 | "slide_type": "slide" 1123 | } 1124 | }, 1125 | "source": [ 1126 | "A Note about Notebook Security" 1127 | ] 1128 | }, 1129 | { 1130 | "cell_type": "markdown", 1131 | "metadata": {}, 1132 | "source": [ 1133 | "By default, a notebook downloaded to a new computer is *untrusted*\n", 1134 | "\n", 1135 | "* HTML and Javascript in Markdown cells is now *never* executed\n", 1136 | "* HTML and Javascript code outputs must be explicitly *re-executed*\n", 1137 | "* Some of these restrictions can be mitigrated through shared accounts (Sage MathCloud) and secrets" 1138 | ] 1139 | }, 1140 | { 1141 | "cell_type": "markdown", 1142 | "metadata": { 1143 | "slideshow": { 1144 | "slide_type": "notes" 1145 | } 1146 | }, 1147 | "source": [ 1148 | "More information on notebook security is in the [IPython Notebook documentation](http://ipython.org/ipython-doc/stable/notebook/security.html)" 1149 | ] 1150 | }, 1151 | { 1152 | "cell_type": "heading", 1153 | "level": 2, 1154 | "metadata": { 1155 | "slideshow": { 1156 | "slide_type": "slide" 1157 | } 1158 | }, 1159 | "source": [ 1160 | "Magics" 1161 | ] 1162 | }, 1163 | { 1164 | "cell_type": "markdown", 1165 | "metadata": { 1166 | "slideshow": { 1167 | "slide_type": "-" 1168 | } 1169 | }, 1170 | "source": [ 1171 | "IPython kernels execute a superset of the Python language. The extension functions, commonly referred to as *magics*, come in two variants. " 1172 | ] 1173 | }, 1174 | { 1175 | "cell_type": "heading", 1176 | "level": 3, 1177 | "metadata": { 1178 | "slideshow": { 1179 | "slide_type": "subslide" 1180 | } 1181 | }, 1182 | "source": [ 1183 | "Line Magics" 1184 | ] 1185 | }, 1186 | { 1187 | "cell_type": "markdown", 1188 | "metadata": { 1189 | "slideshow": { 1190 | "slide_type": "-" 1191 | } 1192 | }, 1193 | "source": [ 1194 | "* A *line magic* looks like a command line call. The most important of these is `%matplotlib inline`, which embeds all matplotlib plot output as images in the notebook itself." 1195 | ] 1196 | }, 1197 | { 1198 | "cell_type": "code", 1199 | "collapsed": false, 1200 | "input": [ 1201 | "%matplotlib inline" 1202 | ], 1203 | "language": "python", 1204 | "metadata": { 1205 | "slideshow": { 1206 | "slide_type": "fragment" 1207 | } 1208 | }, 1209 | "outputs": [], 1210 | "prompt_number": 7 1211 | }, 1212 | { 1213 | "cell_type": "code", 1214 | "collapsed": false, 1215 | "input": [ 1216 | "%whos" 1217 | ], 1218 | "language": "python", 1219 | "metadata": { 1220 | "slideshow": { 1221 | "slide_type": "subslide" 1222 | } 1223 | }, 1224 | "outputs": [ 1225 | { 1226 | "output_type": "stream", 1227 | "stream": "stdout", 1228 | "text": [ 1229 | "Variable Type Data/Info\n", 1230 | "----------------------------------\n", 1231 | "YouTubeVideo type \n", 1232 | "matplotlib module matplotlib/__init__.pyc'>\n", 1233 | "x int 1\n" 1234 | ] 1235 | } 1236 | ], 1237 | "prompt_number": 8 1238 | }, 1239 | { 1240 | "cell_type": "heading", 1241 | "level": 3, 1242 | "metadata": { 1243 | "slideshow": { 1244 | "slide_type": "subslide" 1245 | } 1246 | }, 1247 | "source": [ 1248 | "Cell Magics" 1249 | ] 1250 | }, 1251 | { 1252 | "cell_type": "markdown", 1253 | "metadata": { 1254 | "slideshow": { 1255 | "slide_type": "-" 1256 | } 1257 | }, 1258 | "source": [ 1259 | "* A *cell magic* takes its entire cell as an argument. Although there are a number of useful cell magics, you may find `%%timeit` to be useful for exploring code performance." 1260 | ] 1261 | }, 1262 | { 1263 | "cell_type": "code", 1264 | "collapsed": false, 1265 | "input": [ 1266 | "%%timeit\n", 1267 | "\n", 1268 | "import numpy as np\n", 1269 | "np.sum(np.random.rand(1000))" 1270 | ], 1271 | "language": "python", 1272 | "metadata": { 1273 | "slideshow": { 1274 | "slide_type": "fragment" 1275 | } 1276 | }, 1277 | "outputs": [ 1278 | { 1279 | "output_type": "stream", 1280 | "stream": "stdout", 1281 | "text": [ 1282 | "10000 loops, best of 3: 23.2 \u00b5s per loop\n" 1283 | ] 1284 | } 1285 | ], 1286 | "prompt_number": 9 1287 | }, 1288 | { 1289 | "cell_type": "heading", 1290 | "level": 3, 1291 | "metadata": { 1292 | "slideshow": { 1293 | "slide_type": "slide" 1294 | } 1295 | }, 1296 | "source": [ 1297 | "Interacting with the Command Line" 1298 | ] 1299 | }, 1300 | { 1301 | "cell_type": "markdown", 1302 | "metadata": { 1303 | "slideshow": { 1304 | "slide_type": "subslide" 1305 | } 1306 | }, 1307 | "source": [ 1308 | "IPython supports one final trick, the ability to interact directly with your shell by using the `!` operator." 1309 | ] 1310 | }, 1311 | { 1312 | "cell_type": "code", 1313 | "collapsed": false, 1314 | "input": [ 1315 | "!ls" 1316 | ], 1317 | "language": "python", 1318 | "metadata": { 1319 | "slideshow": { 1320 | "slide_type": "fragment" 1321 | } 1322 | }, 1323 | "outputs": [ 1324 | { 1325 | "output_type": "stream", 1326 | "stream": "stdout", 1327 | "text": [ 1328 | "Aliasing.ipynb Markdown Cells.ipynb README.md finite_difference_lab.ipynb\r\n", 1329 | "Introducing the IPython Notebook.ipynb Multigrid.ipynb example.css \u001b[34mimages\u001b[m\u001b[m\r\n" 1330 | ] 1331 | } 1332 | ], 1333 | "prompt_number": 10 1334 | }, 1335 | { 1336 | "cell_type": "code", 1337 | "collapsed": false, 1338 | "input": [ 1339 | "x = !ls" 1340 | ], 1341 | "language": "python", 1342 | "metadata": { 1343 | "slideshow": { 1344 | "slide_type": "fragment" 1345 | } 1346 | }, 1347 | "outputs": [], 1348 | "prompt_number": 11 1349 | }, 1350 | { 1351 | "cell_type": "code", 1352 | "collapsed": false, 1353 | "input": [ 1354 | "print x" 1355 | ], 1356 | "language": "python", 1357 | "metadata": { 1358 | "slideshow": { 1359 | "slide_type": "fragment" 1360 | } 1361 | }, 1362 | "outputs": [ 1363 | { 1364 | "output_type": "stream", 1365 | "stream": "stdout", 1366 | "text": [ 1367 | "['Aliasing.ipynb', 'Introducing the IPython Notebook.ipynb', 'Markdown Cells.ipynb', 'Multigrid.ipynb', 'README.md', 'example.css', 'finite_difference_lab.ipynb', 'images']\n" 1368 | ] 1369 | } 1370 | ], 1371 | "prompt_number": 12 1372 | }, 1373 | { 1374 | "cell_type": "heading", 1375 | "level": 2, 1376 | "metadata": { 1377 | "slideshow": { 1378 | "slide_type": "slide" 1379 | } 1380 | }, 1381 | "source": [ 1382 | "A Note about Notebook Version Control" 1383 | ] 1384 | }, 1385 | { 1386 | "cell_type": "markdown", 1387 | "metadata": { 1388 | "slideshow": { 1389 | "slide_type": "-" 1390 | } 1391 | }, 1392 | "source": [ 1393 | "The IPython Notebook is stored using canonicalized JSON for ease of use with version control systems.\n", 1394 | "\n", 1395 | "There are two things to be aware of:\n", 1396 | "\n", 1397 | "* By default, IPython embeds all content and saves kernel execution numbers. You may want to get in the habit of clearing all cells before committing.\n", 1398 | "\n", 1399 | "* As of IPython 2.0, all notebooks are signed on save. This increases the chances of a commit collision during merge, forcing a manual resolution. Either signature can be safely deleted in this situation." 1400 | ] 1401 | } 1402 | ], 1403 | "metadata": {} 1404 | } 1405 | ] 1406 | } --------------------------------------------------------------------------------