#!/usr/bin/env python
# coding: utf-8
# # Introduction to scientific computing with Python
# Adapted by John Jasa (johnjasa@umich.edu) from work by J.R. Johansson (jrjohansson at gmail.com)
# ## What is Python?
# [Python](http://www.python.org/) is a modern, general-purpose, object-oriented, high-level programming language.
#
# General characteristics of Python:
#
# * **clean and simple language:** Easy-to-read and intuitive code, easy-to-learn minimalistic syntax, maintainability scales well with size of projects.
# * **expressive language:** Fewer lines of code, fewer bugs, easier to maintain.
#
# Technical details:
#
# * **dynamically typed:** No need to define the type of variables, function arguments or return types.
# * **automatic memory management:** No need to explicitly allocate and deallocate memory for variables and data arrays. No memory leak bugs.
# * **interpreted:** No need to compile the code. The Python interpreter reads and executes the python code directly.
#
# Advantages:
#
# * The main advantage is ease of programming, minimizing the time required to develop, debug and maintain the code.
# * Well designed language that encourage many good programming practices:
# * Modular and object-oriented programming, good system for packaging and re-use of code. This often results in more transparent, maintainable and bug-free code.
# * Documentation tightly integrated with the code.
# * A large standard library, and a large collection of add-on packages.
#
# Disadvantages:
#
# * Since Python is an interpreted and dynamically typed programming language, the execution of python code can be slow compared to compiled statically typed programming languages, such as C and Fortran.
# * Somewhat decentralized, with different environment, packages and documentation spread out at different places. Can make it harder to get started.
# ## What makes python suitable for scientific computing?
#
#
# * Python has a strong position in scientific computing:
# * Large community of users, easy to find help and documentation.
#
# * Extensive ecosystem of scientific libraries and environments
# * numpy: http://numpy.scipy.org - Numerical Python
# * scipy: http://www.scipy.org - Scientific Python
# * matplotlib: http://www.matplotlib.org - graphics library
#
# * Great performance due to close integration with time-tested and highly optimized codes written in C and Fortran:
# * blas, atlas blas, lapack, arpack, Intel MKL, ...
#
# * Good support for
# * Parallel processing with processes and threads
# * Interprocess communication (MPI)
# * GPU computing (OpenCL and CUDA)
#
# * Readily available and suitable for use on high-performance computing clusters.
#
# * No license costs, no unnecessary use of research budget.
#
# ### Python interpreter
# The standard way to use the Python programming language is to use the Python interpreter to run python code. The python interpreter is a program that reads and execute the python code in files passed to it as arguments. At the command prompt, the command ``python`` is used to invoke the Python interpreter.
#
# For example, to run a file ``my-program.py`` that contains python code from the command prompt, use::
#
# $ python my-program.py
#
# We can also start the interpreter by simply typing ``python`` at the command line, and interactively type python code into the interpreter.
#
#
#
#
#
# This is often how we want to work when developing scientific applications, or when doing small calculations. But the standard python interpreter is not very convenient for this kind of work, due to a number of limitations.
# ### IPython
# IPython is an interactive shell that addresses the limitation of the standard python interpreter, and it is a work-horse for scientific use of python. It provides an interactive prompt to the python interpreter with a greatly improved user-friendliness.
#
#
#
#
# Some of the many useful features of IPython includes:
#
# * Command history, which can be browsed with the up and down arrows on the keyboard.
# * Tab auto-completion.
# * In-line editing of code.
# * Object introspection, and automatic extract of documentation strings from python objects like classes and functions.
# * Good interaction with operating system shell.
# * Support for multiple parallel back-end processes, that can run on computing clusters or cloud services like Amazon EC2.
#
# ### IPython notebook
# [IPython notebook](http://ipython.org/notebook.html) is an HTML-based notebook environment for Python, similar to Mathematica or Maple. It is based on the IPython shell, but provides a cell-based environment with great interactivity, where calculations can be organized and documented in a structured way.
#
#
#
#
# Although using a web browser as graphical interface, IPython notebooks are usually run locally, from the same computer that run the browser. To start a new IPython notebook session, run the following command:
#
# $ ipython notebook
#
# from a directory where you want the notebooks to be stored. This will open a new browser window (or a new tab in an existing window) with an index page where existing notebooks are shown and from which new notebooks can be created.
# ### Spyder
# [Spyder](http://code.google.com/p/spyderlib/) is a MATLAB-like IDE for scientific computing with python. It has the many advantages of a traditional IDE environment, for example that everything from code editing, execution and debugging is carried out in a single environment, and work on different calculations can be organized as projects in the IDE environment.
#
#
#
#
# Some advantages of Spyder:
#
# * Powerful code editor, with syntax high-lighting, dynamic code introspection and integration with the python debugger.
# * Variable explorer, IPython command prompt.
# * Integrated documentation and help.
# ## Versions of Python
# There are currently two versions of python: Python 2 and Python 3. Python 3 will eventually supercede Python 2, but it is not backward-compatible with Python 2. A lot of existing python code and packages has been written for Python 2, and it is still the most wide-spread version. For these lectures either version will be fine, but it is probably easier to stick with Python 2 for now, because it is more readily available via prebuilt packages and binary installers.
#
# To see which version of Python you have, run
#
# $ python --version
# Python 2.7.3
# $ python3.2 --version
# Python 3.2.3
#
# Several versions of Python can be installed in parallel, as shown above.
#
# ## Installation
# ### Conda
# The best way set-up an scientific Python environment is to use the cross-platform package manager `conda` from Continuum Analytics. First download and install miniconda http://conda.pydata.org/miniconda.html or Anaconda (see below). Next, to install the required libraries for these notebooks, simply run:
#
# $ conda install ipython ipython-notebook spyder numpy scipy sympy matplotlib cython
#
# This should be sufficient to get a working environment on any platform supported by `conda`.
# ### Linux
# In Ubuntu Linux, to installing python and all the requirements run:
#
# $ sudo apt-get install python ipython ipython-notebook
# $ sudo apt-get install python-numpy python-scipy python-matplotlib python-sympy
# $ sudo apt-get install spyder
# ### MacOS X
# *Macports*
#
# Python is included by default in Mac OS X, but for our purposes it will be useful to install a new python environment using [Macports](http://www.macports.org/), because it makes it much easier to install all the required additional packages. Using Macports, we can install what we need with:
#
# $ sudo port install py27-ipython +pyside+notebook+parallel+scientific
# $ sudo port install py27-scipy py27-matplotlib py27-sympy
# $ sudo port install py27-spyder
#
# These will associate the commands `python` and `ipython` with the versions installed via macports (instead of the one that is shipped with Mac OS X), run the following commands:
#
# $ sudo port select python python27
# $ sudo port select ipython ipython27
#
# *Fink*
#
# Or, alternatively, you can use the [Fink](http://www.finkproject.org/) package manager. After installing Fink, use the following command to install python and the packages that we need:
#
# $ sudo fink install python27 ipython-py27 numpy-py27 matplotlib-py27 scipy-py27 sympy-py27
# $ sudo fink install spyder-mac-py27
# ### Windows
# Windows lacks a good packaging system, so the easiest way to setup a Python environment is to install a pre-packaged distribution. Some good alternatives are:
#
# * [Enthought Python Distribution](http://www.enthought.com/products/epd.php). EPD is a commercial product but is available free for academic use.
# * [Anaconda](http://continuum.io/downloads.html). The Anaconda Python distribution comes with many scientific computing and data science packages and is free, including for commercial use and redistribution. It also has add-on products such as Accelerate, IOPro, and MKL Optimizations, which have free trials and are free for academic use.
# * [Python(x,y)](http://code.google.com/p/pythonxy/). Fully open source.
#
#
#
# #### Note
#
# EPD and Anaconda are also available for Linux and Max OS X.
# ## Further reading
# * [Python](http://www.python.org). The official Python web site.
# * [Python tutorials](http://docs.python.org/2/tutorial). The official Python tutorials.
# * [Think Python](http://www.greenteapress.com/thinkpython). A free book on Python.