{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "This lecture is about using **Python for numerical computing**. Python is a high-level, general-purpose **interpreted programming language** that is widely used in scientific computing and engineering. As a general-purpose language, Python was not specifically designed for numerical computing, but many of its characteristics make it well suited for this task. First and foremost, Python is well known for its clean and easy-to-read code syntax." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Types of programming language\n", "\n", "## Compiled languages\n", "\n", "These\n", "* Require a translation, i.e, the **Compilation**, of the code to machine code\n", "* Usually strict data types\n", "* Typically faster than the interpreted languages" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Interpreted languages\n", "These\n", "* Are translated during the execution\n", "* Usually not strictly typed\n", "* Usually slower (but more easy to read)\n", "\n", "**Python** roughly falls into the second category with potential for speed up (see more below)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Why did I choose Python?\n", "\n", "* Good code readability improves maintainability\n", "\n", "* For high performance cases it may be necessary to use a low-level program language, such as C or Fortran, to obtain the best performance out of the hardware that runs the code. \n", "\n", "* While the best possible runtime performance can be achieved in a low-level programming language, working in a high-level language such as Python usually **reduces the development time**, and often results in more flexible and extensible code." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "## Python versions\n", "\n", "Many implementations, but two major versions\n", "\n", " Python 2\n", " Python 3 (this course)\n", "\n", "further version like `PyPy` are not mentioned her." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Why Python and Jupyter Notebooks?\n", "### Jupyter Notebooks\n", "One of the most significant advances in the scientific computing arena is underway with the explosion of interest in **Jupyter (formerly, IPython) Notebook** technology. The scientific publication Nature recently featured an article on the benefits of **Jupyter Notebooks for scientific research**. There are now Jupyter Notebooks on numerous topics in many scientific disciplines. Here are a few examples of IPython Notebooks for science:\n", "\n", "* LIGO Gravitational Wave Data\n", "* Satellite Imagery Analysis\n", "* 12 Steps to Navier-Stokes\n", "* Computer Vision\n", "* Machine Learning" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "The reason for **Jupyter’s immense success** is it excels in a form of programming called **“literate programming”.**\n", "\n", "Literate programming is a software development style pioneered by Stanford computer scientist, Donald Knuth. This type of programming emphasizes a prose first approach where exposition with human-friendly text is punctuated with code blocks. It excels at demonstration, research, and teaching objectives especially for science. Literate programming allows users to formulate and describe their thoughts with prose, supplemented by mathematical equations, as they prepare to write code blocks. " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### History\n", "\n", "For a bit of history, IPython notebooks quickly gained popularity and in 2013 the IPython team won a Sloan Foundation Grant to accelerate development of this technology. The IPython Notebook concept was expanded upon to allow for additional programming languages and was therefore renamed “Jupyter”. “Jupyter” is a loose acronym meaning Julia, Python and R, but today, the notebook technology supports many programming languages. For more information on Jupyter notebooks see the How to Start and Run a Jupyter Notebook section." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## What Is Python?\n", "Python is a “batteries included” computer programming language. More concretely, Python is a programming language that, in contrast to other programming languages such as C, Fortran, or Java, allows users to more readily focus and solve domain problems instead of dealing with the complexity of how a computer operates. Python achieves this goal by having the following attributes:" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Python is a **high-level language**, meaning that it **abstracts underlying computer-related technical details.** For example, Python does **not** make its users **think too much about computer memory management** or proper declaration of variables and uses safe assumptions about what the programmer is trying to convey. In addition, **a high-level language can be expressed in a manner closer to English prose or mathematical equations.** \n", "\n", "Python is a general-purpose language meaning that it can be used for all problems that a computer is capable of rather than specializing in a specific area such as statistical analysis." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Python is an **interpreted language** meaning that **evaluation of code** to obtain results can happen **immediately** rather than having to go through a time-consuming, **compile and run cycle**, which thereby speeds up the thinking and experimentation processes. \n", "\n", "Python has **many, many users** which means that programmers can quickly find solutions and example code to problems with the help of Google and Stackoverflow.\n", "These features, perhaps, come with a minor cost of reduced language performance, but this is a trade-off the vast majority of users are willing to make in order to gain all the advantages Python has to offer." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "In addition, Python has a rich ecosystem for scientific inquiry in the form of many proven, and popular open-source packages including:\n", "\n", "* numpy,scipy a Python package for scientific computing\n", "* matplotlib, 2D plotting library which produces publication quality figures\n", "\n", "The style of python is found [here](https://www.python.org/dev/peps/pep-0008/)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# How to run python?\n", "* You could run via Python console\n", "* There are IDE (Integrated Development Engine) like Spyder\n", "* Via a webbrowser and the [Jupyter Notebook](https://jupyter.org/) system\n", "\n", "An individual description will be given on the webpage how to set up your system." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Let's start having fun!" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This algorithm will compute the exponential of a number a\n" ] } ], "source": [ "print(\"This algorithm will compute the exponential of a number a\")" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "2.6666666666666665" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2+2/3" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "We see that on the left of the so-called **cell** in this notebook we see the field **IN** and **OUT**. Both the **In and Out** Variables can later be accessed. Let's see!" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "ename": "KeyError", "evalue": "22", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mOut\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m22\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mKeyError\u001b[0m: 22" ] } ], "source": [ "print(Out[22])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "Out[30]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "print(Out)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "We can also rely on auto-completion using the `` key, which then provides a list of possible completions of the started command" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "pow(3,3)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "We can also use **object inspection** to learn more about variables, functions, or classes. For example, os. produces a list of the variables, functions, and classes in the os module, and pressing after having typed os.w results in a list of symbols in the os module that starts with w:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "import os" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "os.d" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "os.wait" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "os.abc?" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "We can also call commands from the shell via the Jupyter notebook. Using the exclamation point." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "!mkdir blabla" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "rm: cannot remove 'blabla': No such file or directory\r\n" ] } ], "source": [ "!rm -r blabla" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 11_Monte-Carlo-Maxwell-Boltzmann-Distributions.ipynb\r\n", "'2. Automatic Differentiation.ipynb'\r\n", " dog_inheritance.py\r\n", " dog_isinstance.py\r\n", " dog.py\r\n", " euler1.ipynb\r\n", " euler2.ipynb\r\n", " ExAutomaticDiff.ipynb\r\n", " facebook_combined.txt\r\n", " face.png\r\n", " fib.py\r\n", " gaussian_processes.ipynb\r\n", " GaussianProcesses.ipynb\r\n", " igraph.ipynb\r\n", "'Introduction to Jupyter notebooks and Python.ipynb'\r\n", " JuliaSciComp\r\n", " Labs\r\n", "'Lecture 0311.ipynb'\r\n", " lecture0.ipynb\r\n", " lecture0.slides.html\r\n", " lecture10.ipynb\r\n", " lecture11.ipynb\r\n", " lecture12.ipynb\r\n", " lecture1.ipynb\r\n", " lecture1_lab.ipynb\r\n", " lecture1.slides.html\r\n", " lecture2.ipynb\r\n", " lecture2.slides.html\r\n", " lecture3.ipynb\r\n", " lecture4.ipynb\r\n", " lecture5.ipynb\r\n", " lecture6.ipynb\r\n", " lecture7.ipynb\r\n", " lecture8.ipynb\r\n", " lecture9.ipynb\r\n", " mandril.jpg\r\n", " material\r\n", " Monte-Carlo-Calculating-Pi.ipynb\r\n", " MoreBasics.ipynb\r\n", " Network_old.ipynb\r\n", " output\r\n", " Overview.ipynb\r\n", " peppers.png\r\n", " rbf1d.png\r\n", " scipro-primer\r\n", " seg_ex_05_chalk.ipynb\r\n", " Social_Network_Ads.csv\r\n", " stuff\r\n", " Videos\r\n" ] } ], "source": [ "!ls" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "We can also call and run python scripts that live outside of the Jupyter notebook like the following function that returns Fibonacci numbers\n", "```python\n", "def fib(n):\n", " '''\n", " Return a list of the first n Fibonacci numbers.\n", " '''\n", " f0, f1 = 0, 1\n", " f = [1] * n\n", " for i in range(1, n):\n", " f[i] = f0 + f1\n", " f0, f1 = f1, f[i]\n", " return f\n", "```" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "%run fib.py" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765]\n" ] } ], "source": [ "print(fib(20))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Timing and profiling code\n", "\n", "We have now seen some fairly basic things and want to discuss who codes can be timed when you want to show your results in a presentation, a thesis, a sales pitch, and so on.\n", "\n", "We can use the **%time** and **%timeit** commands for simple benchmarking facilities. If we want to know more we do" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "%timeit?" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.05 µs ± 31.5 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)\n" ] } ], "source": [ "# Some statistics of the timings\n", "%timeit fib(10)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 12.3 ms, sys: 3 µs, total: 12.3 ms\n", "Wall time: 12.6 ms\n" ] } ], "source": [ "# Time for a single execution\n", "result = %time fib(10000)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Variables\n", "\n", "Variables are used to store and retrieve data. Every variable has a **value** (which, in Python, can be undefined) and a **type** (partly hidden in Python).\n", "\n", "### Simple types\n", "\n", "* **Number** -- generally, what you'd consider an integer or a real number in mathematics.\n", " For example: 17, -19, 17.19, 0, 2e3, ...\n", "* **String**-- a text.\n", " For example: \"word\", \"This is the solution to all your questions.\", \"ŞƿҿÇïåĿ sɹǝʇɔɐɹɐɥɔ\", \"17\", ...\n", " The so-called empty string, \"\", has no characters (its length is zero).\n", "* **Boolean** -- truth values (`True` and `False`).\n", "* **NoneType** -- the type of a special constant None that means \"no value\". This is not a zero (a number), nor an empty string, nor anything else! None is different from any other constant and any other value that a variable can get." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Note that `42` is a number, while `\"42\"` is a string!" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Some not-so-simple types\n", "\n", "* Lists and tuples -- most languages have only lists (usually called arrays)\n", "* Dictionaries\n", "* Sets\n", "* Objects\n", "* Functions, which can be saved in variables as well" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n", "The value of x is 4\n" ] } ], "source": [ "x = input()\n", "print(\"The value of x is\", x)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Whatever is on the right-hand side of the assignment = gets computed first. Then the result is assigned to the variable on the left-hand side.\n", "Having done this the next line of code is executed.\n", "\n", "\n", "\n", "In our example this means:\n", "* The function `input()` reads in a sequence of characters from the standard input our keyboard and returns it as a `string`.\n", "* That value is then assigned to the variable `x` (on the left-hand side of the assignment operator =).\n", " Now x holds - as a string - whatever we have typed up to the first newline, i.e., up to the first Enter key (the newline itself is not part of the string).\n", "* The function print() now outputs its arguments to the standard output (usually the user's screen), in order in which they were given, separated by a single space character.\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Conversion \n", "\n", "We can also convert between simple data types. " ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n", "7\n", "x = 4\n", "y = 7.0\n", "x+y = 11.0\n", "This is a string: \"11.0\"\n" ] } ], "source": [ "x = int(input())\n", "y = float(input())\n", "print(\"x = \", x)\n", "print(\"y = \", y)\n", "print(\"x+y = \", x+y)\n", "z = 'This is a string: \"' + str(x+y) + '\"'\n", "print(z)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# What is Scientific Computing ?\n", "\n", "Scientific Computing is nowadays the **third pillar of science**, standing right next to theoretical analysis and experiments for scientific discovery.\n", "\n", "Computation becomes crucially important in situations such as:\n", "\n", "* The problem at hand cannot be solved by traditional experimental or theoretical means, such as attempting to predict climate change\n", "* Experimentation may be dangerous, e.g., characterization of toxic materials\n", "* The problem would be too expensive or time-consuming to try to solve by other avenues, e.g. determination of the structure of proteins\n", "\n", "Another characteristic of **Scientific Computing** is that it is a **multidisciplinary activity.** \n", "\n", "It involves application experts, applied mathematicians and computer scientists that help to implement computational solution." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## But what is Scientific Computing ?\n", "\n", "Let us follow the SIAM Journal of Scientific Computing and their categorization of the journal papers into\n", "three categories:\n", "\n", "> **Methods and Algorithms for Scientific Computing:**\n", "Papers in this category may include theoretical analysis, provided that the relevance to applications in science and engineering is demonstrated. They should contain meaningful computational results and theoretical results or strong heuristics supporting the performance of new algorithms." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ ">**Computational Methods in Science and Engineering:**\n", "Papers in this section will typically describe novel methodologies for solving a specific problem in computational science or engineering. They should contain enough information about the application to orient other computational scientists but should omit details of interest mainly to the applications specialist." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ ">**Software and High-Performance Computing:**\n", "Papers in this category should concern the novel design and development of computational methods and high-quality software, parallel algorithms, high-performance computing issues, new architectures, data analysis, or visualization. The primary focus should be on computational methods that have potentially large impact for an important class of scientific or engineering problems." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Following Prof Nico Gauger from TU Kaiserslautern we also use the following quote of [Golub&Ortega]* as follows:\n", "\n", ">… **Scientific Computing** is the collection of **tools, techniques, and theories** required to solve on a computer mathematical models of problems in **Science and Engineering**.\n", "A majority of these tools, techniques, and theories originally **developed in Mathematics**, many of them having their genesis long before the advent of electronic computers. This set of mathematical theories and techniques is called **Numerical Analysis** (or Numerical Mathematics) and constitutes a major part of scientific computing. The development of the electronic computer, however, signaled a new era in the approach to the solution of scientific problems. Many of the numerical methods that had been developed for the purpose of hand calculation (including the use of desk calculators for the actual arithmetic) had to be revised and sometimes abandoned. Considerations that where irrelevant or unimportant for hand calculation now became of utmost importance for the efficient and correct use of a large Computer System. Many of these considerations –**programming languages, operating systems, " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ ">management of large quantities of data, correctness of programs** – were subsumed under the new discipline of Computer Science, on which scientific computing now depends heavily. But mathematics itself continues to play a major role in scientific computing: it provides the language of the mathematical models that are to be solved and information about the suitability of a model **(Does it have a solution? Is the solution unique?)** and it provides the theoretical foundation for the numerical methos and, increasingly, many of the tools from computer science.\n", "In summary, then, scientific computing draws on mathematics and computer science to develop the best way to use computer systems to solve problems from science and engineering." ] } ], "metadata": { "celltoolbar": "Slideshow", "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.7" } }, "nbformat": 4, "nbformat_minor": 4 }