From e805f9daa6c91a88fe7c0106db32369b457eda63 Mon Sep 17 00:00:00 2001 From: Prabhu Ramachandran Date: Tue, 15 May 2018 21:25:25 +0530 Subject: Add ordering to the basic material. --- scipy/basic/01_intro.tex | 325 ++++++++++++++++++ scipy/basic/02_prelims.tex | 265 ++++++++++++++ scipy/basic/03_ipython_plotting.tex | 662 +++++++++++++++++++++++++++++++++++ scipy/basic/04_saving_scripts.tex | 422 +++++++++++++++++++++++ scipy/basic/05_lists_arrays.tex | 554 ++++++++++++++++++++++++++++++ scipy/basic/06_numpy.tex | 551 +++++++++++++++++++++++++++++ scipy/basic/07_more_numpy.tex | 668 ++++++++++++++++++++++++++++++++++++ scipy/basic/08_basic_scipy.tex | 509 +++++++++++++++++++++++++++ scipy/basic/09_exercises.tex | 476 +++++++++++++++++++++++++ scipy/basic/10_mlab.tex | 523 ++++++++++++++++++++++++++++ scipy/basic/11_notebook.tex | 325 ++++++++++++++++++ scipy/basic/Makefile | 18 +- scipy/basic/basic_scipy.tex | 509 --------------------------- scipy/basic/exercises.tex | 476 ------------------------- scipy/basic/intro.tex | 325 ------------------ scipy/basic/ipython_plotting.tex | 662 ----------------------------------- scipy/basic/lists_arrays.tex | 554 ------------------------------ scipy/basic/mlab.tex | 523 ---------------------------- scipy/basic/more_numpy.tex | 668 ------------------------------------ scipy/basic/notebook.tex | 325 ------------------ scipy/basic/numpy.tex | 551 ----------------------------- scipy/basic/prelims.tex | 265 -------------- scipy/basic/saving_scripts.tex | 422 ----------------------- 23 files changed, 5289 insertions(+), 5289 deletions(-) create mode 100644 scipy/basic/01_intro.tex create mode 100644 scipy/basic/02_prelims.tex create mode 100644 scipy/basic/03_ipython_plotting.tex create mode 100644 scipy/basic/04_saving_scripts.tex create mode 100644 scipy/basic/05_lists_arrays.tex create mode 100644 scipy/basic/06_numpy.tex create mode 100644 scipy/basic/07_more_numpy.tex create mode 100644 scipy/basic/08_basic_scipy.tex create mode 100644 scipy/basic/09_exercises.tex create mode 100644 scipy/basic/10_mlab.tex create mode 100644 scipy/basic/11_notebook.tex delete mode 100644 scipy/basic/basic_scipy.tex delete mode 100644 scipy/basic/exercises.tex delete mode 100644 scipy/basic/intro.tex delete mode 100644 scipy/basic/ipython_plotting.tex delete mode 100644 scipy/basic/lists_arrays.tex delete mode 100644 scipy/basic/mlab.tex delete mode 100644 scipy/basic/more_numpy.tex delete mode 100644 scipy/basic/notebook.tex delete mode 100644 scipy/basic/numpy.tex delete mode 100644 scipy/basic/prelims.tex delete mode 100644 scipy/basic/saving_scripts.tex (limited to 'scipy/basic') diff --git a/scipy/basic/01_intro.tex b/scipy/basic/01_intro.tex new file mode 100644 index 0000000..03c0ad0 --- /dev/null +++ b/scipy/basic/01_intro.tex @@ -0,0 +1,325 @@ +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%Tutorial slides on Python. +% +% Author: FOSSEE +% Copyright (c) 2009-2016, FOSSEE, IIT Bombay +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +\documentclass[14pt,compress]{beamer} + +% Modified from: generic-ornate-15min-45min.de.tex +\mode +{ + \usetheme{Warsaw} + \useoutertheme{infolines} + \setbeamercovered{transparent} +} + +% Remove navigation symbols. +\setbeamertemplate{navigation symbols}{} + +\usepackage[english]{babel} +\usepackage[latin1]{inputenc} +%\usepackage{times} +\usepackage[T1]{fontenc} + +% Taken from Fernando's slides. +\usepackage{ae,aecompl} +\usepackage{mathpazo,courier,euler} +\usepackage[scaled=.95]{helvet} + +\definecolor{darkgreen}{rgb}{0,0.5,0} + +\usepackage{listings} +\lstset{language=Python, + basicstyle=\ttfamily\bfseries, + commentstyle=\color{red}\itshape, + stringstyle=\color{darkgreen}, + showstringspaces=false, + keywordstyle=\color{blue}\bfseries} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Macros +\setbeamercolor{emphbar}{bg=blue!20, fg=black} +\newcommand{\emphbar}[1] +{\begin{beamercolorbox}[rounded=true]{emphbar} + {#1} + \end{beamercolorbox} +} +\newcounter{time} +\setcounter{time}{0} +\newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}} + +\newcommand{\typ}[1]{\lstinline{#1}} + +\newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}} } + +\newcommand\BackgroundPicture[1]{% + \setbeamertemplate{background}{% + \parbox[c][\paperheight]{\paperwidth}{% + \vfill \hfill + \pgfimage[width=1.0\paperwidth,height=1.0\paperheight]{#1} + \hfill \vfill +}}} + +% For non-wide pictures, set the width so that the height scales +% appropriately. +\newcommand\BackgroundPictureWidth[1]{% + \setbeamertemplate{background}{% + \parbox[c][\paperheight]{\paperwidth}{% + \vfill \hfill + \pgfimage[width=1.0\paperwidth]{#1} + \hfill \vfill +}}} + +% For shorter pictures, set the height so that the width scales +% appropriately. +\newcommand\BackgroundPictureHeight[1]{% + \setbeamertemplate{background}{% + \parbox[c][\paperheight]{\paperwidth}{% + \vfill \hfill + \pgfimage[height=1.0\paperheight]{#1} + \hfill \vfill +}}} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Title page +\title[Introduction]{Introductory Scientific Computing with +Python} +\subtitle{Introduction to Python} + +\author[FOSSEE] {FOSSEE} + +\institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} +\date[] {Mumbai, India +} +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +%\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} +%\logo{\pgfuseimage{iitmlogo}} + + +%% Delete this, if you do not want the table of contents to pop up at +%% the beginning of each subsection: +\AtBeginSubsection[] +{ + \begin{frame} + \frametitle{Outline} + \tableofcontents[currentsection,currentsubsection] + \end{frame} +} + +\AtBeginSection[] +{ + \begin{frame} + \frametitle{Outline} + \tableofcontents[currentsection,currentsubsection] + \end{frame} +} + +% If you wish to uncover everything in a step-wise fashion, uncomment +% the following command: +%\beamerdefaultoverlayspecification{<+->} + +%%\includeonlyframes{current,current1,current2,current3,current4,current5,current6} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% DOCUMENT STARTS +\begin{document} + +\begin{frame} + \maketitle +\end{frame} + +\begin{frame} + \frametitle{Acknowledgement} + \Large + \begin{center} + \alert{FOSSEE group (\url{fossee.in})} \\ + based at\\ + \alert{IIT Bombay}\\ + and funded by\\ + The National Mission on Education through ICT, \\ + \alert{Ministry of HRD, India} + \end{center} +\end{frame} + + +\begin{frame}[plain] + \begin{center} + \Huge + Why Python? + \vspace*{1in} + + For Scientific Computing? + \end{center} + +\end{frame} + +\begin{frame} + \frametitle{Need: Toolkit for diversity} + \Large + \begin{itemize} + \item Numeric and Symbolic + \item Exploration and Visualization + \item High performance + \item Parallel computing + \item User interfaces, Web + \item Other tasks + \end{itemize} +\end{frame} + +\begin{frame}[plain] + \frametitle{Why Python?} + \Large + \begin{itemize} + \item Easy to read and learn + \item Powerful interactive interpreter + \item Scalable, general purpose + \item High-level, modular + \item Procedural, OO, functional + \end{itemize} +\end{frame} + +\begin{frame}[plain] + \frametitle{Why Python?} + \Large + \begin{itemize} + \item Extensive libraries + \item Rapid application development + \item Interface to C++, C and FORTRAN + \item Cross-platform + \item Open Source + \end{itemize} +\end{frame} + + +\begin{frame}[plain] + \frametitle{Requirements: numeric computation} + + \begin{center} + \pgfimage[height=3.5in]{data/intro/scipy_screenie} + + \end{center} + +\end{frame} + +\begin{frame}[plain] + \frametitle{SciPy} + \begin{itemize} + \item Linear algebra + \item Numerical integration + \item Fourier transforms + \item Signal processing + \item Special functions + \item Statistics + \item Optimization + \item Image processing + \item ODE solvers + \vspace*{0.2in} + \item Uses LAPACK, QUADPACK, ODEPACK, FFTPACK etc. from netlib + \end{itemize} +\end{frame} + +\begin{frame}[plain] + \frametitle{Requirement: Exploration/Visualization} + \begin{center} + \pgfimage[height=3.0in]{data/intro/mayavi-ipython} + \end{center} + \end{frame} + +\begin{frame}[plain] + \frametitle{3D visualization with Mayavi2} + \vspace*{-0.3in} + \begin{center} + \hspace*{-0.3in}\pgfimage[width=4.75in]{data/intro/m2_app} + \end{center} +\end{frame} + +\begin{frame}[plain] + \frametitle{Requirement: HPC, parallel computing} + \begin{center} + \includegraphics[height=1.75in]{data/intro/dam_break_poster_0} + \includegraphics[height=1.75in]{data/intro/dam_break_poster} + \end{center} +\end{frame} + +\begin{frame}[plain] + \frametitle{Requirement: UI} + + \begin{center} + \pgfimage[height=3.25in]{data/intro/wxpy_demo} + + \end{center} + +\end{frame} + +\begin{frame}[plain, fragile] + \frametitle{Super-simple UIs} +%\footnotesize +\begin{lstlisting} +from traits.api import * +class Person(HasTraits): + name = Str('name') + age = Range(0.0, 200.0) + sex = Enum('male', 'female') + +p = Person(name='Ram') +p.configure_traits() +\end{lstlisting} +\hrule +\pgfimage[width=0.5\textwidth, interpolate=true]{data/intro/traits_ui_wx} +\pgfimage[width=0.5\textwidth, interpolate=true]{data/intro/traits_ui_qt} +\begin{center}WxPython/Qt +\end{center} +\end{frame} + + + +\BackgroundPicture{data/intro/django_page} +\begin{frame}[plain] +\end{frame} +\BackgroundPicture{data/intro/blank} + +\begin{frame}[plain,fragile] + \frametitle{Easy to read and still compact?} + \footnotesize + \begin{lstlisting} +def qsort(L): + """Quick sort for given sequence, `L`.""" + if not L: return L # exit recursion if input is empty + pivot, rest = L[0], L[1:] + less_than = [ lt for lt in rest if lt < pivot ] + greater_eq = [ ge for ge in rest if ge >= pivot ] + return qsort(less_than) + [pivot] + qsort(greater_eq) + + \end{lstlisting} +\end{frame} + +\begin{frame}[plain] + \Huge + \begin{center} + + \structure{Python users?} + \end{center} +\end{frame} + +\BackgroundPictureWidth{data/intro/python-orgs} +\begin{frame}[plain] +\end{frame} +\BackgroundPicture{data/intro/blank} + +\begin{frame}[plain] + \emphbar{\Large \hfill Diverse needs, one language \hfill } + \emphbar{\Large \hfill Python! \hfill} + \begin{center} + %\structure{Python!} + \pgfimage[height=2in]{data/intro/python_only_logo} + + \url{www.python.org} + \end{center} + +\end{frame} + + +\end{document} diff --git a/scipy/basic/02_prelims.tex b/scipy/basic/02_prelims.tex new file mode 100644 index 0000000..e613082 --- /dev/null +++ b/scipy/basic/02_prelims.tex @@ -0,0 +1,265 @@ +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%Tutorial slides on Python. +% +% Author: FOSSEE +% Copyright (c) 2009-2016, FOSSEE, IIT Bombay +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +\documentclass[14pt,compress]{beamer} + +% Modified from: generic-ornate-15min-45min.de.tex +\mode +{ + \usetheme{Warsaw} + \useoutertheme{infolines} + \setbeamercovered{transparent} +} + +% Remove navigation symbols. +\setbeamertemplate{navigation symbols}{} + +\usepackage[english]{babel} +\usepackage[latin1]{inputenc} +%\usepackage{times} +\usepackage[T1]{fontenc} + +% Taken from Fernando's slides. +\usepackage{ae,aecompl} +\usepackage{mathpazo,courier,euler} +\usepackage[scaled=.95]{helvet} + +\definecolor{darkgreen}{rgb}{0,0.5,0} + +\usepackage{listings} +\lstset{language=Python, + basicstyle=\ttfamily\bfseries, + commentstyle=\color{red}\itshape, + stringstyle=\color{darkgreen}, + showstringspaces=false, + keywordstyle=\color{blue}\bfseries} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Macros +\setbeamercolor{emphbar}{bg=blue!20, fg=black} +\newcommand{\emphbar}[1] +{\begin{beamercolorbox}[rounded=true]{emphbar} + {#1} + \end{beamercolorbox} +} +\newcounter{time} +\setcounter{time}{0} +\newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}} + +\newcommand{\typ}[1]{\lstinline{#1}} + +\newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}} } + +\newcommand\BackgroundPicture[1]{% + \setbeamertemplate{background}{% + \parbox[c][\paperheight]{\paperwidth}{% + \vfill \hfill + \pgfimage[width=1.0\paperwidth,height=1.0\paperheight]{#1} + \hfill \vfill +}}} + +% For non-wide pictures, set the width so that the height scales +% appropriately. +\newcommand\BackgroundPictureWidth[1]{% + \setbeamertemplate{background}{% + \parbox[c][\paperheight]{\paperwidth}{% + \vfill \hfill + \pgfimage[width=1.0\paperwidth]{#1} + \hfill \vfill +}}} + +% For shorter pictures, set the height so that the width scales +% appropriately. +\newcommand\BackgroundPictureHeight[1]{% + \setbeamertemplate{background}{% + \parbox[c][\paperheight]{\paperwidth}{% + \vfill \hfill + \pgfimage[height=1.0\paperheight]{#1} + \hfill \vfill +}}} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Title page +\title[Preliminaries]{Introductory Scientific Computing with +Python} +\subtitle{Some preliminaries} + +\author[FOSSEE] {FOSSEE} + +\institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} +\date[] {Mumbai, India +} +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +%\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} +%\logo{\pgfuseimage{iitmlogo}} + + +%% Delete this, if you do not want the table of contents to pop up at +%% the beginning of each subsection: +\AtBeginSubsection[] +{ + \begin{frame} + \frametitle{Outline} + \tableofcontents[currentsection,currentsubsection] + \end{frame} +} + +\AtBeginSection[] +{ + \begin{frame} + \frametitle{Outline} + \tableofcontents[currentsection,currentsubsection] + \end{frame} +} + +% If you wish to uncover everything in a step-wise fashion, uncomment +% the following command: +%\beamerdefaultoverlayspecification{<+->} + +%%\includeonlyframes{current,current1,current2,current3,current4,current5,current6} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% DOCUMENT STARTS +\begin{document} + +\begin{frame} + \maketitle +\end{frame} + +\begin{frame}[plain] + \frametitle{What is Python?} + \large + \begin{itemize} + \item Python: the programming language + + \vspace*{0.5in} + \item \typ{python}: the interpreter + \end{itemize} +\end{frame} + +\begin{frame}[fragile] + \frametitle{The interpreter} + \small + \begin{lstlisting} +$ python +Python 2.7.9 (default, Feb 10 2015, 03:29:10) +Type "help", "copyright", "credits" or "license" for more information. +>>> +\end{lstlisting} %$ +\end{frame} + +\begin{frame}[fragile] + \frametitle{The interpreter} + \small +\begin{lstlisting} +$ python hello.py +hello world +$ +\end{lstlisting} +\end{frame} + +\begin{frame}[plain] + \frametitle{Python standard library} + \begin{itemize} + \item Built-in + \item ``Batteries included'' + \item Lot of important functionality + \end{itemize} +\end{frame} + +\begin{frame}[plain] + \frametitle{Python packages} + \begin{itemize} + \item Many other packages available + \item \typ{ipython} + \item \typ{numpy} + \item \typ{scipy} + \item \typ{matplotlib} + \item \typ{PyQt} + \item \typ{django} + \item \typ{mayavi} + \item About 1 lakh packages! + \end{itemize} +\end{frame} + +\begin{frame}[plain] + \frametitle{Official Python} + \begin{center} + \url{www.python.org/downloads} + + \includegraphics[height=2.75in]{data/intro/python_download} + \end{center} +\end{frame} + +\begin{frame} + \frametitle{Python distributions} + \begin{itemize} + \item GNU/Linux: easy to install + \item Usually many packages are easy to install + \item Example: \typ{apt-get}, \typ{yum} etc. + \item Not so easy on OS X and Windows + \end{itemize} +\end{frame} + +\begin{frame} + \frametitle{Python distribution: Canopy} + \begin{itemize} + \item \url{www.enthought.com/products/canopy} + \item Easy to use + \item Simple IDE + \item Cross platform: Linux, OSX, Windows + \item Free installer + \item Many packages + \end{itemize} +\end{frame} + +\begin{frame} + \frametitle{Python distribution: Anaconda} + \begin{itemize} + \item \url{www.continuum.io/downloads} + \item Cross platform: Linux, OSX, Windows + \item Free installer + \item Many packages + \end{itemize} +\end{frame} + +\begin{frame}[plain] + \frametitle{For this course} + \begin{itemize} + \item Using Canopy + \item Simple UI + \item Easy for beginners + \vspace*{0.25in} + \item Advanced users can use anything they want! + \end{itemize} +\end{frame} + +\begin{frame}[plain] + \frametitle{Setup} + \begin{itemize} + \item Download for your platform + \item Install it + \item Start Canopy + \end{itemize} +\end{frame} + +\begin{frame}[plain] + \frametitle{Summary} + \begin{itemize} + \item Python: the programming language + \item \typ{python}: the interpreter + \item Python standard library + \item Other Python packages + \item Python distributions + \end{itemize} +\end{frame} + + + + +\end{document} diff --git a/scipy/basic/03_ipython_plotting.tex b/scipy/basic/03_ipython_plotting.tex new file mode 100644 index 0000000..d0c5bf3 --- /dev/null +++ b/scipy/basic/03_ipython_plotting.tex @@ -0,0 +1,662 @@ +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%Tutorial slides on Python. +% +% Author: FOSSEE +% Copyright (c) 2009-2016, FOSSEE, IIT Bombay +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +\documentclass[14pt,compress]{beamer} + +% Modified from: generic-ornate-15min-45min.de.tex +\mode +{ + \usetheme{Warsaw} + \useoutertheme{infolines} + \setbeamercovered{transparent} +} + +\usepackage[english]{babel} +\usepackage[latin1]{inputenc} +%\usepackage{times} +\usepackage[T1]{fontenc} + +% Taken from Fernando's slides. +\usepackage{ae,aecompl} +\usepackage{mathpazo,courier,euler} +\usepackage[scaled=.95]{helvet} + +\definecolor{darkgreen}{rgb}{0,0.5,0} + +\usepackage{listings} +\lstset{language=Python, + basicstyle=\ttfamily\bfseries, + commentstyle=\color{red}\itshape, + stringstyle=\color{darkgreen}, + showstringspaces=false, + keywordstyle=\color{blue}\bfseries} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Macros +\setbeamercolor{emphbar}{bg=blue!20, fg=black} +\newcommand{\emphbar}[1] +{\begin{beamercolorbox}[rounded=true]{emphbar} + {#1} + \end{beamercolorbox} +} +\newcounter{time} +\setcounter{time}{0} +\newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}} + +\newcommand{\typ}[1]{\lstinline{#1}} + +\newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}} } + +%%% This is from Fernando's setup. +% \usepackage{color} +% \definecolor{orange}{cmyk}{0,0.4,0.8,0.2} +% % Use and configure listings package for nicely formatted code +% \usepackage{listings} +% \lstset{ +% language=Python, +% basicstyle=\small\ttfamily, +% commentstyle=\ttfamily\color{blue}, +% stringstyle=\ttfamily\color{orange}, +% showstringspaces=false, +% breaklines=true, +% postbreak = \space\dots +% } + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Title page +\title[Interactive Plotting]{Introductory Scientific Computing with +Python} +\subtitle{Introduction, IPython and Plotting} + +\author[FOSSEE] {FOSSEE} + +\institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} +\date[] {Mumbai, India +} +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +%\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} +%\logo{\pgfuseimage{iitmlogo}} + + +%% Delete this, if you do not want the table of contents to pop up at +%% the beginning of each subsection: +\AtBeginSubsection[] +{ + \begin{frame} + \frametitle{Outline} + \tableofcontents[currentsection,currentsubsection] + \end{frame} +} + +\AtBeginSection[] +{ + \begin{frame} + \frametitle{Outline} + \tableofcontents[currentsection,currentsubsection] + \end{frame} +} + +% If you wish to uncover everything in a step-wise fashion, uncomment +% the following command: +%\beamerdefaultoverlayspecification{<+->} + +%%\includeonlyframes{current,current1,current2,current3,current4,current5,current6} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% DOCUMENT STARTS +\begin{document} + +\begin{frame} + \maketitle +\end{frame} + +%% \begin{frame} +%% \frametitle{Outline} +%% \tableofcontents +%% % You might wish to add the option [pausesections] +%% \end{frame} + +\begin{frame} + \frametitle{Acknowledgement} + \Large + \begin{center} + \alert{FOSSEE group (\url{fossee.in})} \\ + based at\\ + \alert{IIT Bombay}\\ + and funded by\\ + The National Mission on Education through ICT, \\ + \alert{Ministry of HRD, India} + \end{center} +\end{frame} + +\section{Checklist} +\begin{frame} +\frametitle{Checklist} + \begin{enumerate} + \item Editor - we recommend \alert{Canopy} + \item IPython + \item Data files: + \begin{itemize} + \item \typ{pendulum.txt} + \item \typ{data.csv} + \end{itemize} + \item Images + \begin{itemize} + \item \typ{lena.png} + \end{itemize} + \end{enumerate} +\end{frame} + +\begin{frame} + \frametitle{About the Tutorial} + \begin{block}{Intended Audience} + \begin{itemize} + \item Engg., Mathematics and Science researchers with a + reasonable programming background. + \end{itemize} + \end{block} + + \begin{block}{Goal: Successful participants will be able to} + \begin{itemize} + \item Start using Python as plotting, computational tool. + \item Use the basic libraries and tools for scientific computing + with Python. + \end{itemize} + \end{block} +\end{frame} + +%% \begin{frame}[fragile] +%% \frametitle{The Python interpreter \ldots} +%% \begin{block}{} +%% \begin{lstlisting} +%% $ python +%% \end{lstlisting} %$ +%% \end{block} +%% \begin{lstlisting} +%% >>> print "Hello, World!" +%% Hello, World! +%% \end{lstlisting} +%% Exiting +%% \begin{lstlisting} +%% >>> ^D(Ctrl-D) +%% $ +%% \end{lstlisting} %$ +%% \end{frame} + +\section{Starting up IPython} +\begin{frame}[fragile] +\frametitle{Starting up \ldots} +\begin{block}{Start a terminal} + \begin{itemize} + \item Canopy command prompt (Tools menu) +\end{itemize} +\end{block} + +\begin{block}{On Terminal} +\begin{lstlisting} + $ ipython --pylab +\end{lstlisting} %$ +\end{block} +\end{frame} + +% \section{Starting up IPython} +% \begin{frame}[fragile] +% \frametitle{Starting up \ldots} +% \begin{block}{Terminal Jupyter} +% \begin{lstlisting} +% $ jupyter console + +% In [1]: %pylab +% \end{lstlisting} %$ +% \end{block} +% \end{frame} + + +\begin{frame}[fragile] +\frametitle{Running IPython} +\begin{lstlisting} + In []: print("Hello, World!") + Hello, World! +\end{lstlisting} + +Exiting on the \textbf{terminal} +\begin{lstlisting} + In []: ^D(Ctrl-D) + Do you really want to exit([y]/n)? y +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] + \frametitle{IPython? } + \begin{itemize} + \item An enhanced Python interpreter + \end{itemize} +\end{frame} + +% \begin{frame}[fragile] +% \frametitle{Pressed Ctrl-D on Canopy? } +% \begin{itemize} +% \item Pressed \verb+Ctrl-D+ inside Canopy? +% \item You suddenly lost the Python prompt? +% \item Go to \verb+View->Python+ +% \end{itemize} +% \end{frame} + + +\section{Breaking out of loops} +\begin{frame}[fragile] +\frametitle{Breaking out of Loops} +Breaking out of loops +\begin{lstlisting} + In []: while True: + ...: print("Hello, World!") + ...: + Hello, World! + Hello, World!^C(Ctrl-C) + ------------------------------------ + KeyboardInterrupt +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Exercise} + + \begin{itemize} + \item Exit the IPython interpreter + \item Close the terminal + \item Restart the terminal (Canopy tools menu) + \item Restart IPython using: + \end{itemize} +\begin{lstlisting} + $ ipython --pylab +\end{lstlisting} %$ + \inctime{10} +\end{frame} + +\section{Plotting} + +\begin{frame} + \frametitle{Important instructions} + \begin{itemize} + \item For the first session, please do not experiment + \item Follow along and type everything! + \item Case matters + \item Every character you type matters! + \end{itemize} +\end{frame} + +\subsection{Drawing plots} +\begin{frame}[fragile] +\frametitle{First Plot} +\begin{columns} + \column{0.25\textwidth} + \hspace*{-0.25in} + \includegraphics[height=2in, interpolate=true]{data/firstplot} + \column{0.8\textwidth} + \begin{block}{} + \begin{small} +\begin{lstlisting} +In []: x = linspace(0, 2*pi, 50) +In []: plot(x, sin(x)) +\end{lstlisting} + \end{small} + \end{block} +\end{columns} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Walkthrough} +\begin{block}{\typ{x = linspace(start, stop, num)} } +returns \typ{num} evenly spaced points, in the interval [\typ{start}, \typ{stop}]. +\end{block} +\vspace*{.35in} +\begin{block}{} + \small +\begin{lstlisting} +In []: x[0] +Out[]: 0.0 + +In []: x[49] +Out[]: 6.2831853071795862 +\end{lstlisting} +\end{block} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Walkthrough \ldots} +\begin{block}{\typ{plot(x, y)}} +plots \typ{x} and \typ{y} using default line style and color +\end{block} +\inctime{5} +\end{frame} + +\begin{frame} + \frametitle{Important instructions} + \begin{itemize} + \item Please do not close the plot windows or IPython + \item For the first session, please do not experiment + \item Follow along and type everything! + \item Case matters + \item Every character you type matters! + \end{itemize} +\end{frame} + +\subsection{Decoration} +\begin{frame}[fragile] +\frametitle{Adding Labels} +\begin{columns} + \column{0.25\textwidth} + \hspace*{-0.45in} + \includegraphics[height=2in, interpolate=true]{data/label} + \hspace*{0.5in} + \column{0.55\textwidth} + \begin{block}{} + \small + \begin{lstlisting} +In []: xlabel('x') + +In []: ylabel('sin(x)') + \end{lstlisting} + \small +% \end{lstlisting} +%\typ{xlabel(s)} sets the label of the \typ{x}-axis to \typ{s} + +% \begin{lstlisting} + \end{block} +%\typ{ylabel(s)} sets the label of the \typ{y}-axis to \typ{s} +\end{columns} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Another example} + \begin{lstlisting} +In []: clf() + \end{lstlisting} +\emphbar{Clears the plot area.} + \begin{lstlisting} +In []: y = linspace(0, 2*pi, 50) +In []: plot(y, sin(2*y)) +In []: xlabel('y') +In []: ylabel('sin(2y)') + \end{lstlisting} +\end{frame} + +\begin{frame}[fragile] +\frametitle{IPython tips} + +\begin{itemize} +\item Use \typ{TAB} to complete command +\item Try: +\begin{lstlisting} +In []: pl +\end{lstlisting} +\end{itemize} +\vspace*{0.5in} + +{\Large \structure{History}} +\begin{itemize} +\item Up arrow and down arrow + +\item Left / right to move and edit + +\item Type some text and press up arrow: +\begin{lstlisting} +In []: pl +\end{lstlisting} + +\end{itemize} + +\end{frame} + +\begin{frame}[fragile] + \frametitle{Advanced IPython tips \ldots} + \begin{itemize} +\item Search: \typ{Ctrl-r} and start typing + +\item \typ{Ctrl-a}: go to start of line + +\item \typ{Ctrl-e}: go to end of line + +\item \typ{Ctrl-k}: kill to end of line + \end{itemize} +\end{frame} + + +\subsection{More decoration} +\begin{frame}[fragile] +\frametitle{Title and Legends} +\vspace*{-0.15in} +% \begin{block}{} +% \small +\begin{lstlisting} +In []: title('Sinusoids') +In []: legend(['sin(2y)']) +\end{lstlisting} +% \small +% \end{block} + \vspace*{-0.1in} + \begin{center} + \includegraphics[height=2in, interpolate=true]{data/legend} + \end{center} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Legend Placement} +\begin{block}{} + \small +\begin{lstlisting} +In []: legend(['sin(2y)'], loc='center') +\end{lstlisting} +\end{block} + +\begin{columns} + \column{0.6\textwidth} + \includegraphics[height=2in, interpolate=true]{data/position} +\column{0.45\textwidth} +\vspace{-0.2in} +\begin{lstlisting} +'best' +'right' +'left' +'center' +\end{lstlisting} +\end{columns} +\inctime{15} +\end{frame} + +\begin{frame} + \frametitle{Important instructions} + \begin{itemize} + \item Please do not close the plot windows or IPython + \item For the first session, please do not experiment + \item Follow along and type everything! + \item Case matters + \item Every character you type matters! + \end{itemize} +\end{frame} + +%% \begin{frame}[fragile] +%% \frametitle{For arbitrary location} +%% \vspace*{-0.1in} +%% \begin{lstlisting} +%% In []: legend(['sin(2y)'], loc=(.8,.1)) +%% \end{lstlisting} +%% \emphbar{Specify south-east corner position} +%% %\vspace*{-0.2in} +%% \begin{center} +%% \includegraphics[height=2in, interpolate=true]{data/loc} +%% \end{center} +%% %\inctime{10} +%% \end{frame} + +\begin{frame}[fragile] +\frametitle{Showing it better} +\vspace{-0.15in} +\begin{lstlisting} +In []: plot(y, cos(y), 'r') +# See a red plot! + +In []: clf() +In []: plot(y, sin(y), 'g', linewidth=2) +\end{lstlisting} +\vspace*{-0.2in} +\begin{center} + \includegraphics[height=2.2in, interpolate=true]{data/green} +\end{center} +%\inctime{10} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Annotating} +\vspace*{-0.1in} +\begin{lstlisting} +In[]: annotate('local max', xy=(1.5, 1)) +\end{lstlisting} +\vspace*{-0.2in} +\begin{center} + \includegraphics[height=3in, interpolate=true]{data/annotate} +\end{center} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Saving \& Closing} +\begin{lstlisting} +In []: savefig('sin.png') + +In []: close() +\end{lstlisting} +Some supported formats: +\begin{itemize} +\item png +\item pdf +\item svg +\end{itemize} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Recap} + \begin{itemize} + \item \typ{linspace(start, end, num)} + \item \typ{plot(x, y)} + \item \typ{clf()} + \item \typ{xlabel, ylabel} + \item \typ{title, legend, annotate} + \item \typ{savefig} + \item \typ{close} + \end{itemize} + \inctime{10} +\end{frame} + +\section{Multiple plots} +\begin{frame}[fragile] + \frametitle{Overlaid Plots} +\begin{lstlisting} +In []: y = linspace(0, 2*pi, 50) +\end{lstlisting} + +\begin{lstlisting} +In []: clf() +In []: plot(y, sin(y)) +In []: plot(y, cos(y)) +In []: xlabel('y') +In []: ylabel('f(y)') +In []: legend(['sin(y)', 'cos(y)']) +# Note how we made two legends +\end{lstlisting} + + +\emphbar{By default plots would be overlaid!} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Plotting separate figures} +\begin{lstlisting} +In []: clf() +In []: figure(1) +In []: plot(y, sin(y)) +In []: figure(2) +In []: plot(y, cos(y)) +In []: savefig('cosine.png') +In []: figure(1) +In []: title('sin(y)') +In []: savefig('sine.png') +In []: close() +In []: close() +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Get Axes lengths} +\emphbar{Getting axes lengths} + \begin{lstlisting} +In []: xmin, xmax = xlim() +In []: ymin, ymax = ylim() +In []: print(xmin, xmax) +\end{lstlisting} +\pause +\emphbar{Set the axes limits} + \begin{lstlisting} +In []: xlim(xmin, 2*pi ) +In []: ylim(ymin-0.2, ymax+0.2) + \end{lstlisting} +\end{frame} + +\begin{frame}[plain,fragile] + \frametitle{Axes lengths} + \vspace*{-0.2in} + \begin{center} + \includegraphics[height=3.25in, interpolate=true]{data/limits} + \end{center} +\end{frame} + +\begin{frame}[fragile] +\frametitle{IPython tip: documentation} + +\begin{itemize} + + \item Try: +\begin{lstlisting} +In []: plot? +\end{lstlisting} + \vspace*{0.2in} + to get more information on \typ{plot} + + \item Use arrow keys to scroll docs + \item Note: exit help pager with ``q'' +\end{itemize} +\end{frame} + +\begin{frame}[fragile] +\frametitle{IPython tip: source} +\begin{itemize} + \item Try: +\begin{lstlisting} +In []: plot?? +\end{lstlisting} + to see the source code for \typ{plot} + +\end{itemize} +\inctime{10} +\end{frame} + + +\begin{frame}[plain,fragile] +\frametitle{Review Problem} +\begin{enumerate} +\item Plot $x, -x, \sin(x), x \sin(x)$ in range $-5\pi$ to $5\pi$ +\item Add a legend +\item Annotate the origin +\item Set axes limits to the range of x +\end{enumerate} +\vspace*{-0.15in} +\begin{center} + \includegraphics[height=2.6in, interpolate=true]{data/four_plot} +\end{center} +\end{frame} + +\end{document} diff --git a/scipy/basic/04_saving_scripts.tex b/scipy/basic/04_saving_scripts.tex new file mode 100644 index 0000000..356f10f --- /dev/null +++ b/scipy/basic/04_saving_scripts.tex @@ -0,0 +1,422 @@ +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%Tutorial slides on Python. +% +% Author: FOSSEE +% Copyright (c) 2009-2016, FOSSEE, IIT Bombay +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +\documentclass[14pt,compress]{beamer} + +% Modified from: generic-ornate-15min-45min.de.tex +\mode +{ + \usetheme{Warsaw} + \useoutertheme{infolines} + \setbeamercovered{transparent} +} + +\usepackage[english]{babel} +\usepackage[latin1]{inputenc} +%\usepackage{times} +\usepackage[T1]{fontenc} + +% Taken from Fernando's slides. +\usepackage{ae,aecompl} +\usepackage{mathpazo,courier,euler} +\usepackage[scaled=.95]{helvet} + +\definecolor{darkgreen}{rgb}{0,0.5,0} + +\usepackage{listings} +\lstset{language=Python, + basicstyle=\ttfamily\bfseries, + commentstyle=\color{red}\itshape, + stringstyle=\color{darkgreen}, + showstringspaces=false, + keywordstyle=\color{blue}\bfseries} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Macros +\setbeamercolor{emphbar}{bg=blue!20, fg=black} +\newcommand{\emphbar}[1] +{\begin{beamercolorbox}[rounded=true]{emphbar} + {#1} + \end{beamercolorbox} +} +\newcounter{time} +\setcounter{time}{0} +\newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}} + +\newcommand{\typ}[1]{\lstinline{#1}} + +\newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}} } + +%%% This is from Fernando's setup. +% \usepackage{color} +% \definecolor{orange}{cmyk}{0,0.4,0.8,0.2} +% % Use and configure listings package for nicely formatted code +% \usepackage{listings} +% \lstset{ +% language=Python, +% basicstyle=\small\ttfamily, +% commentstyle=\ttfamily\color{blue}, +% stringstyle=\ttfamily\color{orange}, +% showstringspaces=false, +% breaklines=true, +% postbreak = \space\dots +% } + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Title page +\title[Saving scripts]{Introductory Scientific Computing with +Python} +\subtitle{Saving scripts} + +\author[FOSSEE] {FOSSEE} + +\institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} +\date[] {Mumbai, India +} +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +%\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} +%\logo{\pgfuseimage{iitmlogo}} + + +%% Delete this, if you do not want the table of contents to pop up at +%% the beginning of each subsection: +\AtBeginSubsection[] +{ + \begin{frame} + \frametitle{Outline} + \tableofcontents[currentsection,currentsubsection] + \end{frame} +} + +\AtBeginSection[] +{ + \begin{frame} + \frametitle{Outline} + \tableofcontents[currentsection,currentsubsection] + \end{frame} +} + +% If you wish to uncover everything in a step-wise fashion, uncomment +% the following command: +%\beamerdefaultoverlayspecification{<+->} + +%%\includeonlyframes{current,current1,current2,current3,current4,current5,current6} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% DOCUMENT STARTS +\begin{document} + +\begin{frame} + \maketitle +\end{frame} + +%% \begin{frame} +%% \frametitle{Outline} +%% \tableofcontents +%% % You might wish to add the option [pausesections] +%% \end{frame} + +\section{Exercise} + +\begin{frame}[plain,fragile] +\frametitle{Review Problem} +\begin{enumerate} +\item Plot $x, -x, \sin(x), x \sin(x)$ in range $-5\pi$ to $5\pi$ +\item Add a legend +\item Annotate the origin +\item Set axes limits to the range of x +\end{enumerate} +\vspace*{-0.15in} +\begin{center} + \includegraphics[height=2.6in, interpolate=true]{data/four_plot} +\end{center} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Review Problem \ldots} +\alert{Plotting \ldots} +\begin{lstlisting} +In []: x = linspace(-5*pi, 5*pi, 500) +In []: plot(x, x, 'b') +In []: plot(x, -x, 'b') +In []: plot(x, sin(x), 'g', linewidth=2) +In []: plot(x, x*sin(x), 'r', + linewidth=3) +\end{lstlisting} +$\vdots$ +\end{frame} + +\begin{frame}[fragile] +\frametitle{Review Problem \ldots} +\alert{Legend \& Annotation\ldots} +\begin{lstlisting} +In []: legend(['x', '-x', 'sin(x)', + 'xsin(x)']) +In []: annotate('origin', xy = (0, 0)) +\end{lstlisting} +\alert{Setting Axes limits\ldots} +\begin{lstlisting} +In []: xlim(-5*pi, 5*pi) +In []: ylim(-5*pi, 5*pi) +\end{lstlisting} +\inctime{5} +\end{frame} + +\section{Scripts -- Saving \& Running} +\begin{frame}[fragile] +\frametitle{Command History} +Use the \typ{\%hist} \alert{magic} command of IPython +\typ{In []: \%hist}\\ +This displays the ``Command History'' +\begin{block}{Careful about errors!} + \kwrd{\%hist} will contain the errors as well.\\ +\end{block} +\pause +\begin{block}{Magic Commands?} + Magic commands are commands provided by IPython to make our life easier. +\end{block} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Saving commands into script} +Use the \typ{\%save} \alert{magic} command of IPython +\begin{block}{} +\typ{\%save script_name line_numbers} +\end{block} +Line numbers specified individually separated by spaces or as a range separated by a dash.\\ +\begin{block}{} +\typ{\%save four_plot.py} \alert{\typ{ 16 18-27}} \\ +\end{block} +Saves from history the commands entered on line numbers \alert{16, 18, 19, 20, \ldots 27} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Saving commands into a script} + \begin{itemize} + \item Save lines relevant for the review problem + \item Hint: example\\ \typ{\%save four_plot.py 16 18-27} + \item Choose the lines carefully + \item Edit \typ{four_plot.py} on Canopy + \item Make sure all the lines are correct + \item Save the script + \end{itemize} +\inctime{5} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Creating scripts: alternative} + \begin{itemize} + \item Create a new file on Canopy + \item Copy commands for assignment with your mouse + \item Save the script to \typ{four_plot.py} + \end{itemize} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Where is the script saved?} + \begin{itemize} + \item \typ{\%save} saves into the current directory + \vspace*{0.5in} + \item Use \typ{\%pwd} to print the current directory + \item Use \typ{\%cd} to change the directory + + \vspace*{0.5in} + \item Question: how do you find out more about \typ{\%cd}? + \end{itemize} +\end{frame} + + +\begin{frame}[fragile] +\frametitle{Python Scripts\ldots} +Now, \typ{four_plot.py} is called a Python Script. + \begin{itemize} + \item run the script in IPython using \typ{\%run four_plot.py}\\ + \end{itemize} +\pause +\alert{\typ{NameError: name 'linspace' is not defined}} +\begin{block}{} +To avoid this, run using \alert{\typ{\%run -i four_plot.py}}\\ +\end{block} +\pause +Where is the plot? +\begin{lstlisting} +In []: show() +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Exercise} + \begin{itemize} + \item Add the \typ{show()} command to \typ{four_plot.py} + \item Save the file + \item Test that it works + \end{itemize} + \inctime{5} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Result graph} + \begin{center} + \includegraphics[height=3in, interpolate=true]{data/four_plot} + \end{center} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Running with Python} + \begin{itemize} + \item Start a new Canopy terminal + \item Change directory to where you saved \typ{four_plot.py} + \item Run the script as: + \end{itemize} +\begin{lstlisting} + $ python four_plot.py +\end{lstlisting} + \pause + Do you see: + \begin{small} +\begin{lstlisting} +NameError: name 'linspace' is not defined +\end{lstlisting} + \end{small} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Imports} + \begin{itemize} + \item \typ{ipython --pylab} does magic + \item Import libraries using \typ{import} + \end{itemize} +\begin{lstlisting} +In []: from pylab import linspace +\end{lstlisting} +\inctime{5} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Exercise} + \begin{itemize} + \item Add \typ{from pylab import linspace} to top of \typ{four_plot.py} + \item Test that it works + \end{itemize} +\begin{lstlisting} +# four_plot.py +from pylab import linspace # <-- added +x = linspace(-5*pi, 5*pi, 500) +plot(x, x, 'b') +... +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Try again} + \begin{itemize} + \item On Canopy terminal + \item Run the script as: + \end{itemize} +\begin{lstlisting} + $ python four_plot.py +\end{lstlisting} + \pause + \vspace*{0.15in} + Do you see: + \begin{small} +\begin{lstlisting} +NameError: name 'plot' is not defined +\end{lstlisting} + \end{small} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Exercise} + \begin{itemize} + \item Change line 1 to \typ{from pylab import *} + \item Test that it works + \end{itemize} +\begin{lstlisting} +# four_plot.py +from pylab import * # <-- added +x = linspace(-5*pi, 5*pi, 500) +plot(x, x, 'b') +... +\end{lstlisting} +\inctime{5} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Solution} + \small +\begin{lstlisting} +from pylab import * +x = linspace(-5*pi, 5*pi, 500) +plot(x, x, 'b') +plot(x, -x, 'b') +plot(x, sin(x), 'g', linewidth=2) +plot(x, x*sin(x), 'r', linewidth=3) +legend(['x', '-x', 'sin(x)', 'xsin(x)']) +annotate('origin', xy = (0, 0)) +xlim(-5*pi, 5*pi) +ylim(-5*pi, 5*pi) +show() +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Note on script file names} + \begin{itemize} + \item Should start with a letter + \item Can use \typ{_} (underscore) and numbers + \item No \typ{.} allowed + \item No spaces or special characters + \end{itemize} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Test} + \begin{itemize} + \item \typ{1_script.py} + \item \typ{script_1.py} + \item \typ{one11.py} + \item \typ{one script.py} + \item \typ{one,script;xxx.py} + \item \typ{one.two.py} + \end{itemize} +\end{frame} + +\begin{frame} + \frametitle{Using Canopy} + \begin{itemize} + \item Much easier + \item Write code in the editor + \item Embedded IPython + \item Save (Ctrl-S or Cmd-S) + \item Run selection: Ctrl-Shift-R (Cmd-Shift-R on OS X) + \item Run code: Ctrl-R (Cmd-R on OS X) + \item Change directory with menu (\typ{\%cd}) + \end{itemize} + \inctime{5} +\end{frame} + + +\begin{frame}[fragile] + \frametitle{What did we learn?} + \begin{itemize} + \item Starting up IPython + \item Creating simple plots + \item Annotating: labels, legends, annotation + \item Changing the looks: color, linewidth + \item Accessing history, documentation + \item \kwrd{\%hist} - History of commands + \item Creating a Python script with \typ{\%save} + \item Running a script using \kwrd{\%run -i} + \item Importing functionality + \item Running a script with\ \typ{python script.py} + \end{itemize} +\end{frame} + + +\end{document} diff --git a/scipy/basic/05_lists_arrays.tex b/scipy/basic/05_lists_arrays.tex new file mode 100644 index 0000000..8b47bb7 --- /dev/null +++ b/scipy/basic/05_lists_arrays.tex @@ -0,0 +1,554 @@ +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%Tutorial slides on Python. +% +% Author: FOSSEE +% Copyright (c) 2009, FOSSEE, IIT Bombay +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +\documentclass[14pt,compress]{beamer} +%\documentclass[draft]{beamer} +%\documentclass[compress,handout]{beamer} +%\usepackage{pgfpages} +%\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm] + +% Modified from: generic-ornate-15min-45min.de.tex +\mode +{ + \usetheme{Warsaw} + \useoutertheme{infolines} + \setbeamercovered{transparent} +} + +\usepackage[english]{babel} +\usepackage[latin1]{inputenc} +%\usepackage{times} +\usepackage[T1]{fontenc} + +% Taken from Fernando's slides. +\usepackage{ae,aecompl} +\usepackage{mathpazo,courier,euler} +\usepackage[scaled=.95]{helvet} + +\definecolor{darkgreen}{rgb}{0,0.5,0} + +\usepackage{listings} +\lstset{language=Python, + basicstyle=\ttfamily\bfseries, + commentstyle=\color{red}\itshape, + stringstyle=\color{darkgreen}, + showstringspaces=false, + keywordstyle=\color{blue}\bfseries} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Macros +\setbeamercolor{emphbar}{bg=blue!20, fg=black} +\newcommand{\emphbar}[1] +{\begin{beamercolorbox}[rounded=true]{emphbar} + {#1} + \end{beamercolorbox} +} +\newcounter{time} +\setcounter{time}{0} +\newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}} + +\newcommand{\typ}[1]{\lstinline{#1}} + +\newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}} } + +\newcommand{\num}{\texttt{numpy}} + +%%% This is from Fernando's setup. +% \usepackage{color} +% \definecolor{orange}{cmyk}{0,0.4,0.8,0.2} +% % Use and configure listings package for nicely formatted code +% \usepackage{listings} +% \lstset{ +% language=Python, +% basicstyle=\small\ttfamily, +% commentstyle=\ttfamily\color{blue}, +% stringstyle=\ttfamily\color{orange}, +% showstringspaces=false, +% breaklines=true, +% postbreak = \space\dots +% } + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Title page +\title[Lists and Arrays]{Introductory Scientific Computing with +Python} +\subtitle{More plotting, lists and numpy arrays} + +\author[FOSSEE] {FOSSEE} + +\institute[FOSSEE -- IITB] {Department of Aerospace Engineering\\IIT Bombay} +\date[] {Mumbai, India} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +%\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} +%\logo{\pgfuseimage{iitmlogo}} + + +%% Delete this, if you do not want the table of contents to pop up at +%% the beginning of each subsection: +\AtBeginSubsection[] +{ + \begin{frame} + \frametitle{Outline} + \tableofcontents[currentsection,currentsubsection] + \end{frame} +} + +\AtBeginSection[] +{ + \begin{frame} + \frametitle{Outline} + \tableofcontents[currentsection,currentsubsection] + \end{frame} +} + +% If you wish to uncover everything in a step-wise fashion, uncomment +% the following command: +%\beamerdefaultoverlayspecification{<+->} + +%\includeonlyframes{current,current1,current2,current3,current4,current5,current6} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% DOCUMENT STARTS +\begin{document} + +\begin{frame} + \titlepage +\end{frame} + +\begin{frame} + \frametitle{Outline} + \tableofcontents + % You might wish to add the option [pausesections] +\end{frame} + +\section{Plotting Points} +\begin{frame}[fragile] +\frametitle{Why would I plot f(x)?} +Do we plot analytical functions or experimental data? +\begin{small} +\begin{lstlisting} +In []: time = [0., 1., 2, 3] + +In []: distance = [7., 11, 15, 19] + +In []: plot(time,distance) +Out[]: [] + +In []: xlabel('time') +Out[]: + +In []: ylabel('distance') +Out[]: +\end{lstlisting} +\end{small} +\end{frame} + +\begin{frame}[fragile] +\begin{figure} +\includegraphics[width=3.5in]{data/straightline.png} +\end{figure} +\alert{Is this what you have?} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Plotting points} +\begin{itemize} +\item What if we want to plot the points? +\end{itemize} +\begin{lstlisting} + In []: clf() + + In []: plot(time, distance, 'o') + Out[]: [] + + In []: clf() + In []: plot(time, distance, '.') + Out[]: [] +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] +\begin{figure} +\includegraphics[interpolate=true,width=2.35in]{data/stline_dots.png} +\includegraphics[interpolate=true,width=2.35in]{data/stline_points.png} +\end{figure} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Additional Line Styles} +\begin{itemize} + \item \typ{'o'} - Filled circles + \item \typ{'.'} - Small Dots + \item \typ{'-'} - Lines + \item \typ{'--'} - Dashed lines +\end{itemize} +\end{frame} + +\section{Lists} +\begin{frame}[fragile] + \frametitle{Lists: Introduction} + \begin{lstlisting} +In []: time = [0., 1., 2, 3] + +In []: distance = [7., 11, 15, 19] + \end{lstlisting} +What are \typ{time} and \typ{distance}?\\ +\begin{center} + \large +\alert{\typ{lists!!}} +\end{center} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Lists: Initializing \& accessing elements} +\begin{lstlisting} +In []: mtlist = [] +\end{lstlisting} +\emphbar{Empty List} +\begin{lstlisting} +In []: p = [ 2, 3, 5, 7] + +In []: p[1] +Out[]: 3 + +In []: p[0]+p[1]+p[-1] +Out[]: 12 +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] + \frametitle{List: Slicing} + \begin{block}{Remember\ldots} + \kwrd{In []: p = [ 2, 3, 5, 7]} + \end{block} +\begin{lstlisting} +In []: p[1:3] +Out[]: [3, 5] +\end{lstlisting} +\emphbar{A slice} +\begin{lstlisting} +In []: p[0:-1] +Out[]: [2, 3, 5] +In []: p[1:] +Out[]: [3, 5, 7] +\end{lstlisting} +\end{frame} + +\begin{frame}[plain,fragile] + \frametitle{List: Slicing \ldots} + \vspace*{-0.1in} + \begin{small} + \begin{block}{Remember\ldots} + \kwrd{In []: p = [ 2, 3, 5, 7]} +\end{block} +\end{small} +\begin{lstlisting} +In []: p[0:4:2] +Out[]: [2, 5] +In []: p[0::2] +Out[]: [2, 5] +In []: p[::2] +Out[]: [2, 5] +In []: p[::3] +Out[]: [2, 7] +In []: p[::-1] +Out[]: [7, 5, 3, 2] +\end{lstlisting} +\alert{\typ{list[initial:final:step]}} +\end{frame} + +\begin{frame}[fragile] + \frametitle{List: Slicing} + \begin{block}{Remember\ldots} + \kwrd{In []: p = [ 2, 3, 5, 7]} + \end{block} + What is the output of the following? +\begin{lstlisting} +In []: p[1::2] + +In []: p[1:-1:2] +\end{lstlisting} +\end{frame} + + +%% more on list slicing +\begin{frame}[fragile] +\frametitle{List operations} +\begin{lstlisting} +In []: b = [ 11, 13, 17] +In []: c = p + b + +In []: c +Out[]: [2, 3, 5, 7, 11, 13, 17] + +In []: p.append(11) +In []: p +Out[]: [ 2, 3, 5, 7, 11] +\end{lstlisting} +Question: Does \typ{c} change now that \typ{p} is changed? +\inctime{10} +\end{frame} + +\section{Simple Pendulum} +\begin{frame}[fragile] +\frametitle{Simple Pendulum - L and T} +Let us look at the Simple Pendulum experiment. +\begin{center} +\begin{small} +\begin{tabular}{| c | c | c |} +\hline +$L$ & $T$ & $T^2$ \\ \hline +0.2 & 0.90 & \\ \hline +0.3 & 1.19 & \\ \hline +0.4 & 1.30 & \\ \hline +0.5 & 1.47 & \\ \hline +0.6 & 1.58 & \\ \hline +0.7 & 1.77 & \\ \hline +0.8 & 1.83 & \\ \hline +\end{tabular} +\end{small}\\ +\alert{$L \alpha T^2$} +\end{center} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Lets use lists} +\begin{lstlisting} +In []: L = [0.2, 0.3, 0.4, 0.5, + 0.6, 0.7, 0.8] + +In []: t = [0.90, 1.19, 1.30, + 1.47, 1.58, 1.77, + 1.83] +\end{lstlisting} +\alert{Gotcha}: Make sure \typ{L} and \typ{t} have the same number +of elements + +\begin{lstlisting} +In []: print(len(L), len(t)) +\end{lstlisting} + +\end{frame} + +\begin{frame}[fragile] +\frametitle{Plotting $L$ vs $T^2$} +\begin{itemize} +\item We must square each of the values in \typ{t} +\item How do we do it? +\item We use a \kwrd{for} loop to iterate over \typ{t} +\end{itemize} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Looping with \texttt{for}} +\begin{lstlisting} +In []: for time in t: + ....: print(time*time) + ....: + ....: +\end{lstlisting} +This will print the square of each item in the list, \typ{t} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Plotting $L$ vs $T^2$} +\begin{lstlisting} +In []: tsq = [] + +In []: for time in t: + ....: tsq.append(time*time) + ....: + ....: + +\end{lstlisting} +This gives \typ{tsq} which is the list of squares of \typ{t} values. +\begin{lstlisting} +In []: print(len(L), len(t), len(tsq)) +Out[]: (7, 7, 7) + +In []: plot(L, tsq) +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] +\begin{figure} +\includegraphics[width=3.5in]{data/L-TSq-limited.png} +\end{figure} +\inctime{10} +\end{frame} + + +\begin{frame}[fragile] +\frametitle{Don't repeat yourself: functions} +\noindent Let us define a function to square the list +\begin{lstlisting} +In []: def sqr(arr): + ...: result = [] + ...: for x in arr: + ...: result.append(x*x) + ...: return result + ...: + +In []: tsq = sqr(t) + +\end{lstlisting} %$ +\end{frame} + +\begin{frame}[fragile] + \frametitle{More on defining functions} + \begin{itemize} + \item Consider the function \texttt{f(x) = x\textasciicircum{}2} + \item Let's write a Python function, equivalent to this + \end{itemize} + \begin{lstlisting} + In[]: def f(x): + ....: return x*x + ....: + + In[]: f(1) + In[]: f(2) + \end{lstlisting} + \begin{itemize} + \item \texttt{def} is a keyword + \item \texttt{f} is the name of the function + \item \texttt{x} the parameter of the function (local variable) + \item \texttt{return} is a keyword + \end{itemize} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Aside: Exercise} + \begin{itemize} + \item Write a function called \typ{mysum(a, b)} that returns sum of two + arguments. + \end{itemize} + \pause +\begin{lstlisting} +In []: def mysum(a, b): + ...: return a + b + ...: +In []: mysum(1, 2) + +In []: mysum([1, 2], [3, 4]) +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] + \frametitle{This seems tedious} + + \begin{itemize} + \item Do we have to write a function just to get the square of a list? + \item Lists +\begin{itemize} + \item Nice + \item Not too convenient for math + \item Slow +\end{itemize} +\item Enter NumPy arrays + \begin{itemize} + \item Fixed size, data type + \item Very convenient + \item Fast + \end{itemize} + \end{itemize} + \inctime{10} +\end{frame} + +\subsection{\num\ arrays} + +\begin{frame}[fragile] +\frametitle{NumPy arrays} +\begin{lstlisting} +In []: t = array(t) + +In []: tsq = t*t + +In []: print(tsq) + +In []: plot(L, tsq) # works! +\end{lstlisting} %$ +\end{frame} + +\begin{frame}[fragile] +\frametitle{Speed?} + +\noindent Lets use range to create a large list. + +\begin{lstlisting} +In []: t = range(1000000) + +In []: tsq = sqr(t) + +\end{lstlisting} %$ + +\noindent Now try it with + +\begin{lstlisting} +In []: t = array(t) + +In []: tsq = t*t +\end{lstlisting} +\ldots +\end{frame} + + +\begin{frame}[fragile] + \frametitle{IPython tip: Timing} + +Try the following: + \begin{lstlisting} +In []: %timeit sqr(t) + +In []: %timeit? + + \end{lstlisting} + + \begin{itemize} + \item \typ{\%timeit}: accurate, many measurements + \item Can also use \typ{\%time} + \item \typ{\%time}: less accurate, one measurement + \end{itemize} + +\inctime{10} +\end{frame} + + +\begin{frame}[fragile] +\frametitle{Exercise} +\begin{center} + Find out the speed difference between the \typ{sqr} function and + \typ{t*t} on the numpy array. +\end{center} + +\end{frame} + +\begin{frame}[fragile] + \frametitle{Solution} +\begin{lstlisting} +In []: t = linspace(0, 10, 100000) +In []: %timeit sqr(t) +In []: %timeit t*t +\end{lstlisting} + \inctime{5} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Summary} +\begin{itemize} +\item Plot attributes +\item plotting points +\item Lists +\item Defining simple functions +\item Introduction to \num\ arrays +\item Timing with \typ{\%timeit} +\end{itemize} +\end{frame} + +\end{document} diff --git a/scipy/basic/06_numpy.tex b/scipy/basic/06_numpy.tex new file mode 100644 index 0000000..0f5e17c --- /dev/null +++ b/scipy/basic/06_numpy.tex @@ -0,0 +1,551 @@ +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%Tutorial slides on Python. +% +% Author: FOSSEE +% Copyright (c) 2009, FOSSEE, IIT Bombay +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +\documentclass[14pt,compress]{beamer} +%\documentclass[draft]{beamer} +%\documentclass[compress,handout]{beamer} +%\usepackage{pgfpages} +%\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm] + +% Modified from: generic-ornate-15min-45min.de.tex +\mode +{ + \usetheme{Warsaw} + \useoutertheme{infolines} + \setbeamercovered{transparent} +} + +\usepackage[english]{babel} +\usepackage[latin1]{inputenc} +%\usepackage{times} +\usepackage[T1]{fontenc} + +% Taken from Fernando's slides. +\usepackage{ae,aecompl} +\usepackage{mathpazo,courier,euler} +\usepackage[scaled=.95]{helvet} + +\definecolor{darkgreen}{rgb}{0,0.5,0} + +\usepackage{listings} +\lstset{language=Python, + basicstyle=\ttfamily\bfseries, + commentstyle=\color{red}\itshape, + stringstyle=\color{darkgreen}, + showstringspaces=false, + keywordstyle=\color{blue}\bfseries} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Macros +\setbeamercolor{emphbar}{bg=blue!20, fg=black} +\newcommand{\emphbar}[1] +{\begin{beamercolorbox}[rounded=true]{emphbar} + {#1} + \end{beamercolorbox} +} +\newcounter{time} +\setcounter{time}{0} +\newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}} + +\newcommand{\typ}[1]{\lstinline{#1}} + +\newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}} } + +\newcommand{\num}{\texttt{numpy}} + +%%% This is from Fernando's setup. +% \usepackage{color} +% \definecolor{orange}{cmyk}{0,0.4,0.8,0.2} +% % Use and configure listings package for nicely formatted code +% \usepackage{listings} +% \lstset{ +% language=Python, +% basicstyle=\small\ttfamily, +% commentstyle=\ttfamily\color{blue}, +% stringstyle=\ttfamily\color{orange}, +% showstringspaces=false, +% breaklines=true, +% postbreak = \space\dots +% } + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Title page +\title[NumPy arrays]{Introductory Scientific Computing with +Python} +\subtitle{NumPy arrays} + +\author[FOSSEE] {FOSSEE} + +\institute[FOSSEE -- IITB] {Department of Aerospace Engineering\\IIT Bombay} +\date[] {Mumbai, India} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +%\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} +%\logo{\pgfuseimage{iitmlogo}} + + +%% Delete this, if you do not want the table of contents to pop up at +%% the beginning of each subsection: +\AtBeginSubsection[] +{ + \begin{frame} + \frametitle{Outline} + \tableofcontents[currentsection,currentsubsection] + \end{frame} +} + +\AtBeginSection[] +{ + \begin{frame} + \frametitle{Outline} + \tableofcontents[currentsection,currentsubsection] + \end{frame} +} + +% If you wish to uncover everything in a step-wise fashion, uncomment +% the following command: +%\beamerdefaultoverlayspecification{<+->} + +%\includeonlyframes{current,current1,current2,current3,current4,current5,current6} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% DOCUMENT STARTS +\begin{document} + +\begin{frame} + \titlepage +\end{frame} + +\begin{frame} + \frametitle{Outline} + \tableofcontents + % You might wish to add the option [pausesections] +\end{frame} + + +\section{\num\ arrays} + +\begin{frame}[fragile] + \frametitle{The \num\ module} + \begin{itemize} + \item Efficient, powerful array type + \item Abstracts out standard operations on arrays + \item Convenience functions + \item \typ{ipython --pylab} imports part of numpy + \end{itemize} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Without Pylab} +\begin{lstlisting} +In []: from numpy import * +In []: x = linspace(0, 1) +\end{lstlisting} + Note that we had done this ``import'' earlier! +\begin{lstlisting} +# Can also do this: +In []: import numpy +In []: x = numpy.linspace(0, 1) +# or +In []: import numpy as np +In []: x = np.linspace(0, 1) +\end{lstlisting} + Note the use of \typ{numpy.linspace} +\end{frame} + +\begin{frame} + \frametitle{\num\ arrays} + \begin{itemize} + \item Fixed size (\typ{arr.size}) + \item Same type (\typ{arr.dtype}) + \item Arbitrary dimensionality: \typ{arr.shape} + \item \typ{shape}: extent (size) along each dimension + \item \typ{arr.itemsize}: number of bytes per element + \item \alert{Note:} \typ{shape} can change so long as the \typ{size} + is constant + \item Indices start from 0 + \item Negative indices work like lists + \end{itemize} +\end{frame} + +\begin{frame}[fragile] + \frametitle{\num\ arrays} +\begin{lstlisting} +In []: a = array([1,2,3,4]) +In []: b = array([2,3,4,5]) + +In []: print(a[0], a[-1]) +(1, 4) + +In []: a[0] = -1 +In []: a[0] = 1 +\end{lstlisting} +Operations are elementwise +\end{frame} + +\begin{frame}[fragile] + \frametitle{Simple operations} +\begin{lstlisting} +In []: a + b +Out[]: array([3, 5, 7, 9]) +In []: a*b +Out[]: array([2, 6, 12, 20]) +In []: a/b +Out[]: array([0, 0, 0, 0]) +\end{lstlisting} + \begin{itemize} + \item Operations are \alert{element-wise} + \item Types matter (only on Python 2.x) + \end{itemize} + \inctime{10} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Data type matters} + Try again with this: +\begin{lstlisting} +In []: a = array([1.,2,3,4]) +In []: a/b +\end{lstlisting} + This can happen on Python 2.x, so beware +\end{frame} + +\begin{frame}[fragile] + \frametitle{Examples} +\noindent \typ{pi} and \typ{e} are defined. +\begin{lstlisting} +In []: x = linspace(0.0, 10.0, 200) +In []: x *= 2*pi/10 +# apply functions to array. +In []: y = sin(x) +In []: y = cos(x) +In []: x[0] = -1 +In []: print(x[0], x[-1]) +(-1.0, 10.0) +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] + \frametitle{\typ{size, shape, rank} etc.} +\vspace*{-8pt} +\begin{lstlisting} +In []: x = array([1., 2, 3, 4]) +In []: size(x) +Out[]: 4 +In []: x.dtype +dtype('float64') +In []: x.shape +Out[] (4,) +In []: rank(x) +Out[]: 1 +In []: x.itemsize +Out[]: 8 +\end{lstlisting} +\end{frame} + + +\begin{frame}[fragile] + \frametitle{Multi-dimensional arrays} +\begin{lstlisting} +In []: a = array([[ 0, 1, 2, 3], + ...: [10,11,12,13]]) +In []: a.shape # (rows, columns) +Out[]: (2, 4) + +In []: a[1,3] +Out[]: 13 + +In []: a[1,3] = -1 +In []: a[1] # The second row +array([10,11,12,-1]) +In []: a[1] = 0 # Entire row to zero. +\end{lstlisting} +\inctime{10} +\end{frame} + +\subsection{Slicing arrays} + +\begin{frame}[plain,fragile] + \frametitle{Slicing arrays} + \vspace*{-0.2in} +\begin{lstlisting} +In []: a = array([[1,2,3], [4,5,6], + ...: [7,8,9]]) +In []: a[0,1:3] +\end{lstlisting} + \pause + \vspace*{-0.1in} +\begin{lstlisting} +Out[]: array([2, 3]) + +In []: a[1:,1:] +\end{lstlisting} + \pause + \vspace*{-0.1in} +\begin{lstlisting} +Out[]: array([[5, 6], + [8, 9]]) + +In []: a[:,2] +\end{lstlisting} + \pause + \vspace*{-0.1in} +\begin{lstlisting} +Out[]: array([3, 6, 9]) +\end{lstlisting} +\end{frame} + +\begin{frame}[plain,fragile] + \frametitle{Slicing arrays ...} + \vspace*{-0.2in} +\begin{lstlisting} +In []: a = array([[1,2,3], [4,5,6], + ...: [7,8,9]]) + +In []: a[0::2,0::2] # Striding... +\end{lstlisting} + \pause + \vspace*{-0.1in} +\begin{lstlisting} +Out[]: array([[1, 3], + [7, 9]]) +# Slices refer to the same memory! +\end{lstlisting} +\end{frame} + +\subsection{Array creation} + +\begin{frame}[fragile] + \frametitle{Array creation functions} + \begin{itemize} + \item \typ{array(object)} + \item \typ{linspace(start, stop, num=50)} + \item \typ{ones(shape)} + \item \typ{zeros((d1,...,dn))} + \item \typ{empty((d1,...,dn))} + \item \typ{identity(n)} + \item \typ{ones\_like(x)}, \typ{zeros\_like(x)}, \typ{empty\_like(x)} + \end{itemize} + May pass an optional \typ{dtype=} keyword argument + + For more dtypes see: \typ{numpy.typeDict} + +\end{frame} + +\begin{frame}[fragile] + \frametitle{Creation examples} + \vspace*{-0.25in} +\begin{lstlisting} +In []: a = array([1,2,3], dtype=float) +In []: ones_like(a) +Out[]: array([ 1., 1., 1.]) + +In []: ones( (2, 3) ) +Out[]: array([[ 1., 1., 1.], + [ 1., 1., 1.]]) + +In []: identity(3) +Out[]: array([[ 1., 0., 0.], + [ 0., 1., 0.], + [ 0., 0., 1.]]) +\end{lstlisting} + \inctime{15} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Array math} + \begin{itemize} + \item Basic \alert{elementwise} math (given two arrays \typ{a, b}): + \begin{itemize} + \item \typ{a + b} $\rightarrow$ \typ{add(a, b)} + \item \typ{a - b}, $\rightarrow$ \typ{subtract(a, b)} + \item \typ{a * b}, $\rightarrow$ \typ{multiply(a, b)} + \item \typ{a / b}, $\rightarrow$ \typ{divide(a, b)} + \item \typ{a \% b}, $\rightarrow$ \typ{remainder(a, b)} + \item \typ{a ** b}, $\rightarrow$ \typ{power(a, b)} + \end{itemize} + \item Inplace operators: \typ{a += b}, or \typ{add(a, b, + a)} + \alert{What happens if \typ{a} is \typ{int} and \typ{b} is \typ{float?}} + \end{itemize} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Array math} + \begin{itemize} + \item Logical operations: \typ{==, !=, <, >}, etc. + \item \typ{sin(x), arcsin(x), sinh(x)}, + \typ{exp(x), sqrt(x)} etc. + \item \typ{sum(x, axis=0), product(x, axis=0)} + \item \typ{dot(a, b)} + \end{itemize} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Convenience functions: \typ{loadtxt}} + \begin{itemize} + \item \typ{loadtxt(file_name)}: loads a text file + \item \typ{loadtxt(file_name, unpack=True)}: loads a text file and + unpacks columns + \end{itemize} + \begin{lstlisting} +In []: x = loadtxt('pendulum.txt') +In []: x.shape +Out[]: (90, 2) + +In []: x, y = loadtxt('pendulum.txt', + ...: unpack=True) +In []: x.shape +Out[]: (90,) + \end{lstlisting} + + \inctime{10} +\end{frame} + + +\begin{frame}[fragile] + \frametitle{Advanced} + \begin{itemize} + \item Only scratched the surface of \num + \item \typ{reduce, outer} + \item Typecasting + \item More functions: \typ{take, choose, where}, \typ{compress, + concatenate} + \item Array broadcasting and \typ{None} + \item Record arrays + \end{itemize} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Learn more} + \small + \begin{itemize} + \item \url{https://docs.scipy.org/doc/numpy-dev/user/quickstart.html} + \item \url{http://numpy.org} + \end{itemize} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Recap} + \begin{itemize} + \item Basic concepts: creation, access, operations + \item 1D, multi-dimensional + \item Slicing + \item Array creation, dtypes + \item Math + \item \typ{loadtxt} + \end{itemize} + \inctime{5} +\end{frame} + +\subsection{Example: plotting data from file} + +\begin{frame}[fragile] +\frametitle{Example: plotting data from file} +\alert{Data is usually present in a file!} \\ +Lets look at the \typ{pendulum.txt} file. +\begin{lstlisting} +In []: cat pendulum.txt +1.0000e-01 6.9004e-01 +1.1000e-01 6.9497e-01 +1.2000e-01 7.4252e-01 +1.3000e-01 7.5360e-01 +\end{lstlisting} +\ldots +\end{frame} + +\begin{frame}[fragile] +\frametitle{Reading \typ{pendulum.txt}} +\begin{itemize} + \item File contains L vs.\ T values + \item First Column - L values + \item Second Column - T values + \item Let us generate a plot from the data file +\end{itemize} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Gotcha and an aside} + Ensure you are in the same directory as \typ{pendulum.txt}\\ + if not, do the following on IPython: + \begin{lstlisting} +In []: %cd directory_containing_file +# Check if pendulum.txt is there. +In []: ls +# Also try +In []: !ls + \end{lstlisting} + + \alert{Note:} \typ{\%cd} is an IPython magic command. For more information + do: + \begin{lstlisting} +In []: ? +In []: %cd? + \end{lstlisting} +\end{frame} + + +\begin{frame}[fragile] + \frametitle{Exercise} + \begin{itemize} + \item Plot L versus T square with dots + \item No line connecting points + \end{itemize} + \inctime{10} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Solution} +\begin{lstlisting} +In []: L, t = loadtxt('pendulum.txt', + ....: unpack=True) +In []: plot(L, t*t, '.') +\end{lstlisting} +or +\begin{lstlisting} +In []: x = loadtxt('pendulum.txt') +In []: L, t = x[:,0], x[:,1] +In []: plot(L, t*t, '.') +\end{lstlisting} + +\end{frame} + + +\begin{frame}[fragile] +\begin{figure} +\includegraphics[width=3.5in]{data/L-Tsq.png} +\end{figure} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Odds and ends} +\begin{lstlisting} +In []: mean(L) +Out[]: 0.54499999999999993 + +In []: std(L) +Out[]: 0.25979158313283879 +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Summary} +\begin{itemize} +\item Introduction to \num\ arrays +\item Slicing arrays +\item Multi-dimensional arrays +\item Array operations +\item Creating arrays +\item Loading data from file +\end{itemize} + +\inctime{5} +\end{frame} + +\end{document} diff --git a/scipy/basic/07_more_numpy.tex b/scipy/basic/07_more_numpy.tex new file mode 100644 index 0000000..c53cc41 --- /dev/null +++ b/scipy/basic/07_more_numpy.tex @@ -0,0 +1,668 @@ +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%Tutorial slides on Python. +% +% Author: FOSSEE +% Copyright (c) 2009, FOSSEE, IIT Bombay +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +\documentclass[14pt,compress]{beamer} +%\documentclass[draft]{beamer} +%\documentclass[compress,handout]{beamer} +%\usepackage{pgfpages} +%\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm] + +% Modified from: generic-ornate-15min-45min.de.tex +\mode +{ + \usetheme{Warsaw} + \useoutertheme{infolines} + \setbeamercovered{transparent} +} + +\usepackage[english]{babel} +\usepackage[latin1]{inputenc} +%\usepackage{times} +\usepackage[T1]{fontenc} +\usepackage{amsmath} + +% Taken from Fernando's slides. +\usepackage{ae,aecompl} +\usepackage{mathpazo,courier,euler} +\usepackage[scaled=.95]{helvet} + +\definecolor{darkgreen}{rgb}{0,0.5,0} + +\usepackage{listings} +\lstset{language=Python, + basicstyle=\ttfamily\bfseries, + commentstyle=\color{red}\itshape, + stringstyle=\color{darkgreen}, + showstringspaces=false, + keywordstyle=\color{blue}\bfseries} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Macros +\setbeamercolor{emphbar}{bg=blue!20, fg=black} +\newcommand{\emphbar}[1] +{\begin{beamercolorbox}[rounded=true]{emphbar} + {#1} + \end{beamercolorbox} +} +\newcounter{time} +\setcounter{time}{0} +\newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}} + +\newcommand{\typ}[1]{\lstinline{#1}} + +\newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}} } + +%%% This is from Fernando's setup. +% \usepackage{color} +% \definecolor{orange}{cmyk}{0,0.4,0.8,0.2} +% % Use and configure listings package for nicely formatted code +% \usepackage{listings} +% \lstset{ +% language=Python, +% basicstyle=\small\ttfamily, +% commentstyle=\ttfamily\color{blue}, +% stringstyle=\ttfamily\color{orange}, +% showstringspaces=false, +% breaklines=true, +% postbreak = \space\dots +% } + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Title page +\title[More on numpy arrays]{Introductory Scientific Computing with +Python} +\subtitle{More on numpy arrays} + +\author[FOSSEE] {FOSSEE} + +\institute[FOSSEE -- IITB] {Department of Aerospace Engineering\\IIT Bombay} +\date[] {Mumbai, India} +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +%\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} +%\logo{\pgfuseimage{iitmlogo}} + + +%% Delete this, if you do not want the table of contents to pop up at +%% the beginning of each subsection: +\AtBeginSubsection[] +{ + \begin{frame} + \frametitle{Outline} + \tableofcontents[currentsection,currentsubsection] + \end{frame} +} + +\AtBeginSection[] +{ + \begin{frame} + \frametitle{Outline} + \tableofcontents[currentsection,currentsubsection] + \end{frame} +} + +% If you wish to uncover everything in a step-wise fashion, uncomment +% the following command: +%\beamerdefaultoverlayspecification{<+->} + +%\includeonlyframes{current,current1,current2,current3,current4,current5,current6} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% DOCUMENT STARTS +\begin{document} + +\begin{frame} + \titlepage +\end{frame} + +\begin{frame} + \frametitle{Outline} + \tableofcontents +% \pausesections +\end{frame} + +\section{Matrices} + +\begin{frame} +\frametitle{Matrices: Introduction} +\alert{All matrix operations are done using \kwrd{arrays}} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Matrices: Initializing} +\begin{lstlisting} +In []: c = array([[11,12,13], + [21,22,23], + [31,32,33]]) + +In []: c +Out[]: +array([[11, 12, 13], + [21, 22, 23], + [31, 32, 33]]) +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Initializing some special matrices} +\begin{small} + \begin{lstlisting} +In []: ones((3,5)) +Out[]: +array([[ 1., 1., 1., 1., 1.], + [ 1., 1., 1., 1., 1.], + [ 1., 1., 1., 1., 1.]]) + +In []: ones_like([1, 2, 3, 4]) +Out[]: array([1, 1, 1, 1]) + +In []: identity(2) +Out[]: +array([[ 1., 0.], + [ 0., 1.]]) + \end{lstlisting} +Also available \alert{\typ{zeros, zeros_like, empty, empty_like}} +\end{small} +\end{frame} + + +\begin{frame}[fragile] + \frametitle{Accessing elements} + \begin{small} + \begin{lstlisting} +In []: c +Out[]: +array([[11, 12, 13], + [21, 22, 23], + [31, 32, 33]]) + +In []: c[1][2] +Out[]: 23 +In []: c[1,2] +Out[]: 23 + +In []: c[1] +Out[]: array([21, 22, 23]) + \end{lstlisting} + \end{small} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Changing elements} + \begin{small} + \begin{lstlisting} +In []: c[1,1] = -22 +In []: c +Out[]: +array([[ 11, 12, 13], + [ 21, -22, 23], + [ 31, 32, 33]]) + +In []: c[1] = 0 +In []: c +Out[]: +array([[11, 12, 13], + [ 0, 0, 0], + [31, 32, 33]]) + \end{lstlisting} + \end{small} +How do you access one \alert{column}? +\end{frame} + +\begin{frame}[fragile] + \frametitle{Slicing} +\begin{small} + \begin{lstlisting} +In []: c[:,1] +Out[]: array([12, 0, 32]) + +In []: c[1,:] +Out[]: array([0, 0, 0]) + +In []: c[0:2,:] +Out[]: +array([[11, 12, 13], + [ 0, 0, 0]]) + +In []: c[1:3,:] +Out[]: +array([[ 0, 0, 0], + [31, 32, 33]]) + \end{lstlisting} +\end{small} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Slicing \ldots} +\begin{small} + \begin{lstlisting} +In []: c[:2,:] +Out[]: +array([[11, 12, 13], + [ 0, 0, 0]]) + +In []: c[1:,:] +Out[]: +array([[ 0, 0, 0], + [31, 32, 33]]) + +In []: c[1:,:2] +Out[]: +array([[ 0, 0], + [31, 32]]) + \end{lstlisting} + +\end{small} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Striding} + \begin{small} + \begin{lstlisting} +In []: c[::2,:] +Out[]: +array([[11, 12, 13], + [31, 32, 33]]) + +In []: c[:,::2] +Out[]: +array([[11, 13], + [ 0, 0], + [31, 33]]) + +In []: c[::2,::2] +Out[]: +array([[11, 13], + [31, 33]]) + \end{lstlisting} + \end{small} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Shape of a matrix} + \begin{lstlisting} +In []: c +Out[]: +array([[11, 12, 13], + [ 0, 0, 0], + [31, 32, 33]]) + +In []: c.shape +Out[]: (3, 3) + \end{lstlisting} +\emphbar{Shape specifies shape or dimensions of a matrix} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Elementary image processing} +\begin{small} + \begin{lstlisting} +In []: a = imread('lena.png') + +In []: imshow(a) +Out[]: + \end{lstlisting} + \end{small} +\typ{imread} returns an array of shape (512, 512, 4) which represents an image of 512x512 pixels and 4 shades.\\ +\typ{imshow} renders the array as an image. +\end{frame} + +\begin{frame}[fragile] +\frametitle{Slicing \& Striding Exercises} + \begin{itemize} + \item Crop the image to get the top-left quarter + \item Crop the image to get only the face + \item Resize image to half by dropping alternate pixels + \end{itemize} + +\end{frame} +\begin{frame}[fragile] + \frametitle{Solutions} +\begin{small} + \begin{lstlisting} +In []: imshow(a[:256,:256]) +Out[]: + +In []: imshow(a[200:400,200:400]) +Out[]: + +In []: imshow(a[::2,::2]) +Out[]: + \end{lstlisting} +\end{small} +\inctime{15} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Transpose of a Matrix} +\begin{lstlisting} +In []: a = array([[ 1, 1, 2, -1], + ...: [ 2, 5, -1, -9], + ...: [ 2, 1, -1, 3], + ...: [ 1, -3, 2, 7]]) + +In []: a.T +Out[]: +array([[ 1, 2, 2, 1], + [ 1, 5, 1, -3], + [ 2, -1, -1, 2], + [-1, -9, 3, 7]]) +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Matrix Addition} + \begin{lstlisting} +In []: b = array([[3,2,-1,5], + [2,-2,4,9], + [-1,0.5,-1,-7], + [9,-5,7,3]]) +In []: a + b +Out[]: +array([[ 4. , 3. , 1. , 4. ], + [ 4. , 3. , 3. , 0. ], + [ 1. , 1.5, -2. , -4. ], + [ 10. , -8. , 9. , 10. ]]) + \end{lstlisting} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Elementwise Multiplication} +\begin{lstlisting} +In []: a*b +Out[]: +array([[ 3. , 2. , -2. , -5. ], + [ 4. , -10. , -4. , -81. ], + [ -2. , 0.5, 1. , -21. ], + [ 9. , 15. , 14. , 21. ]]) + +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Matrix Multiplication} +\begin{lstlisting} +In []: dot(a, b) +Out[]: +array([[ -6. , 6. , -6. , -3. ], + [-64. , 38.5, -44. , 35. ], + [ 36. , -13.5, 24. , 35. ], + [ 58. , -26. , 34. , -15. ]]) +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Inverse of a Matrix} +\begin{lstlisting} + +\end{lstlisting} +\begin{small} +\begin{lstlisting} +In []: inv(a) +Out[]: +array([[-0.5 , 0.55, -0.15, 0.7 ], + [ 0.75, -0.5 , 0.5 , -0.75], + [ 0.5 , -0.15, -0.05, -0.1 ], + [ 0.25, -0.25, 0.25, -0.25]]) +\end{lstlisting} +\end{small} +\emphbar{Try this: \typ{I = dot(a, inv(a))}} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Determinant and sum of all elements} +\begin{lstlisting} +In []: det(a) +Out[]: 80.0 +\end{lstlisting} + \begin{lstlisting} +In []: sum(a) +Out[]: 12 + \end{lstlisting} + +\end{frame} + +%%use S=array(X,Y) +\begin{frame}[fragile] +\frametitle{Eigenvalues and Eigen Vectors} +\begin{small} +\begin{lstlisting} +In []: e = array([[3,2,4],[2,0,2],[4,2,3]]) + +In []: eig(e) +Out[]: +(array([-1., 8., -1.]), + array([[-0.74535599, 0.66666667, -0.1931126 ], + [ 0.2981424 , 0.33333333, -0.78664085], + [ 0.59628479, 0.66666667, 0.58643303]])) + +In []: eigvals(e) +Out[]: array([-1., 8., -1.]) +\end{lstlisting} +\end{small} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Computing Norms} +\begin{lstlisting} +In []: norm(e) +Out[]: 8.1240384046359608 +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Singular Value Decomposition} + \begin{small} + \begin{lstlisting} +In []: svd(e) +Out[]: +(array( +[[ -6.66666667e-01, -1.23702565e-16, 7.45355992e-01], + [ -3.33333333e-01, -8.94427191e-01, -2.98142397e-01], + [ -6.66666667e-01, 4.47213595e-01, -5.96284794e-01]]), + array([ 8., 1., 1.]), + array([[-0.66666667, -0.33333333, -0.66666667], + [-0. , 0.89442719, -0.4472136 ], + [-0.74535599, 0.2981424 , 0.59628479]])) + \end{lstlisting} + \end{small} + \inctime{10} +\end{frame} + +\section{Least Squares Fit} +\begin{frame}[fragile] +\frametitle{$L$ vs. $T^2$ - Scatter} +Linear trend visible. +\vspace{-0.1in} +\begin{figure} +\includegraphics[width=4in]{data/L-Tsq-points} +\end{figure} +\end{frame} + +\begin{frame}[fragile] +\frametitle{$L$ vs. $T^2$ - Line} +This plot is not a straight line, can we do better? +\vspace{-0.1in} +\begin{figure} +\includegraphics[width=4in]{data/L-Tsq-Line} +\end{figure} +\end{frame} + +\begin{frame}[fragile] +\frametitle{$L$ vs. $T^2$ - Least Square Fit} +This is what our intention is. +\vspace{-0.1in} +\begin{figure} +\includegraphics[width=4in]{data/least-sq-fit} +\end{figure} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Matrix Formulation} +\begin{itemize} +\item We need to fit a line through points for the equation $T^2 = m \cdot L+c$ +\item In matrix form, the equation can be represented as $T_{sq} = A \cdot p$, +\item We need to find $p$ to plot the line +\end{itemize} +\end{frame} + +\begin{frame} + \begin{equation} + \begin{bmatrix} + T^2_1 \\ + T^2_2 \\ + \vdots\\ + T^2_N \\ +\end{bmatrix} += \begin{bmatrix} + L_1 & 1 \\ + L_2 & 1 \\ + \vdots & \vdots\\ + L_N & 1 \\ +\end{bmatrix} \cdot +\begin{bmatrix} + m\\ + c\\ + \end{bmatrix} +\end{equation} + +Or + +\[T_{sq} = A \cdot p \] + +\end{frame} + + +\begin{frame}[fragile] +\frametitle{Getting $L$ and $T^2$} +%If you \alert{closed} IPython after session 2 +\begin{lstlisting} +In []: L, t = loadtxt('pendulum.txt', + ...: unpack=True) +In []: tsq = t*t +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Generating $A$} +\begin{lstlisting} +In []: A = array([L, ones_like(L)]) +In []: A = A.T +\end{lstlisting} +%% \begin{itemize} +%% \item A is also called a Van der Monde matrix +%% \item It can also be generated using \typ{vander} +%% \end{itemize} +%% \begin{lstlisting} +%% In []: A = vander(L, 2) +%% \end{lstlisting} +\end{frame} + +\begin{frame}[fragile] +\frametitle{\typ{lstsq} \ldots} +\begin{itemize} +\item Now use the \typ{lstsq} function +\item Along with a lot of things, it returns the least squares solution +\end{itemize} +\begin{lstlisting} +In []: result = lstsq(A,tsq) +In []: coef = result[0] +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Least Square Fit Line \ldots} +We get the points of the line from \typ{coef} +\begin{lstlisting} +In []: Tline = coef[0]*L + coef[1] + +In []: Tline.shape +\end{lstlisting} +\begin{itemize} +\item Now plot \typ{Tline} vs. \typ{L}, to get the least squares fit line. +\end{itemize} +\begin{lstlisting} +In []: plot(L, Tline, 'r') + +In []: plot(L, tsq, 'o') +\end{lstlisting} +\inctime{10} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Least Squares Fit} +\vspace{-0.15in} +\begin{figure} +\includegraphics[width=4in]{data/least-sq-fit} +\end{figure} +\end{frame} + +\section{Random numbers} + +\begin{frame}[fragile] + \frametitle{\typ{numpy.random}} + \begin{itemize} + \item Easy random number generation + \end{itemize} + \begin{lstlisting} +In []: random? + \end{lstlisting} + Or: + \begin{lstlisting} +In []: import numpy +In []: numpy.random + \end{lstlisting} +\begin{itemize} + \item \typ{random.random}: produces uniform deviates in $(0, 1)$ + \item \typ{random.normal}: draws random samples from a Gaussian + distribution + \item Useful to create a random matrix of any shape +\end{itemize} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Using the \typ{random} module} +\begin{lstlisting} +In []: x = random.random(size=100) +In []: y = random.random(size=100) +In []: scatter(x, y) # Scatter plot it. + +In []: x,y = random.normal(size=(2,100)) +In []: clf() +In []: scatter(x, y) +\end{lstlisting} +Note that \typ{size} can be a tuple. + +\end{frame} + +\begin{frame}[fragile] + \frametitle{Using the \typ{random} module} +\begin{lstlisting} +In []: img = random.random( + ...: size=(200, 200) + ...: ) +In []: clf() +In []: imshow(img) +In []: colorbar() +\end{lstlisting} +\end{frame} + + + +\section{Summary} +\begin{frame} + \frametitle{What did we learn?} + \begin{itemize} + \item Matrices + \item Least Squares + \item Random numbers + \end{itemize} +\inctime{10} +\end{frame} + +\end{document} diff --git a/scipy/basic/08_basic_scipy.tex b/scipy/basic/08_basic_scipy.tex new file mode 100644 index 0000000..dca675d --- /dev/null +++ b/scipy/basic/08_basic_scipy.tex @@ -0,0 +1,509 @@ +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%Tutorial slides on Python. +% +% Author: FOSSEE +% Copyright (c) 2009-2016, FOSSEE, IIT Bombay +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +\documentclass[14pt,compress]{beamer} +%\documentclass[draft]{beamer} +%\documentclass[compress,handout]{beamer} +%\usepackage{pgfpages} +%\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm] + +% Modified from: generic-ornate-15min-45min.de.tex +\mode +{ + \usetheme{Warsaw} + \useoutertheme{infolines} + \setbeamercovered{transparent} +} + +\usepackage[english]{babel} +\usepackage[latin1]{inputenc} +%\usepackage{times} +\usepackage[T1]{fontenc} +\usepackage{pgf} + +% Taken from Fernando's slides. +\usepackage{ae,aecompl} +\usepackage{mathpazo,courier,euler} +\usepackage[scaled=.95]{helvet} +\usepackage{amsmath} + +\definecolor{darkgreen}{rgb}{0,0.5,0} + +\usepackage{listings} +\lstset{language=Python, + basicstyle=\ttfamily\bfseries, + commentstyle=\color{red}\itshape, + stringstyle=\color{darkgreen}, + showstringspaces=false, + keywordstyle=\color{blue}\bfseries} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Macros +\setbeamercolor{emphbar}{bg=blue!20, fg=black} +\newcommand{\emphbar}[1] +{\begin{beamercolorbox}[rounded=true]{emphbar} + {#1} + \end{beamercolorbox} +} + +\newcommand{\myemph}[1]{\structure{\emph{#1}}} +\newcommand{\PythonCode}[1]{\lstinline{#1}} + +\newcounter{time} +\setcounter{time}{0} +\newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}} + +\newcommand{\typ}[1]{\lstinline{#1}} + +\newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}} } + +%%% This is from Fernando's setup. +% \usepackage{color} +% \definecolor{orange}{cmyk}{0,0.4,0.8,0.2} +% % Use and configure listings package for nicely formatted code +% \usepackage{listings} +% \lstset{ +% language=Python, +% basicstyle=\small\ttfamily, +% commentstyle=\ttfamily\color{blue}, +% stringstyle=\ttfamily\color{orange}, +% showstringspaces=false, +% breaklines=true, +% postbreak = \space\dots +% } + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Title page +\title[Basic SciPy]{Introductory Scientific Computing with +Python} +\subtitle{Basic SciPy} + +\author[Prabhu] {FOSSEE} + +\institute[FOSSEE -- IITB] {Department of Aerospace Engineering\\IIT Bombay} +\date[] { +Mumbai, India +} +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +%\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} +%\logo{\pgfuseimage{iitmlogo}} + + +%% Delete this, if you do not want the table of contents to pop up at +%% the beginning of each subsection: +\AtBeginSubsection[] +{ + \begin{frame} + \frametitle{Outline} + \tableofcontents[currentsection,currentsubsection] + \end{frame} +} + +\AtBeginSection[] +{ + \begin{frame} + \frametitle{Outline} + \tableofcontents[currentsection,currentsubsection] + \end{frame} +} + +% If you wish to uncover everything in a step-wise fashion, uncomment +% the following command: +%\beamerdefaultoverlayspecification{<+->} + +%\includeonlyframes{current,current1,current2,current3,current4,current5,current6} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% DOCUMENT STARTS +\begin{document} + +\begin{frame} + \maketitle +\end{frame} + + +\section{Solving linear systems} + +\begin{frame}[fragile] +\frametitle{Solution of equations} +Consider, + \begin{align*} + 3x + 2y - z & = 1 \\ + 2x - 2y + 4z & = -2 \\ + -x + \frac{1}{2}y -z & = 0 + \end{align*} +Solution: + \begin{align*} + x & = 1 \\ + y & = -2 \\ + z & = -2 + \end{align*} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Solving using Matrices} +Let us now look at how to solve this using \kwrd{matrices} + \begin{lstlisting} +In []: A = array([[3,2,-1], + [2,-2,4], + [-1, 0.5, -1]]) +In []: b = array([1, -2, 0]) +In []: x = solve(A, b) + \end{lstlisting} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Solution:} +\begin{lstlisting} +In []: x +Out[]: array([ 1., -2., -2.]) +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Let's check!} +\begin{small} +\begin{lstlisting} +In []: Ax = dot(A, x) +In []: Ax +Out[]: array([ 1.00000000e+00, -2.00000000e+00, -1.11022302e-16]) +\end{lstlisting} +\end{small} +\begin{block}{} +The last term in the matrix is actually \alert{0}!\\ +We can use \kwrd{allclose()} to check. +\end{block} +\begin{lstlisting} +In []: allclose(Ax, b) +Out[]: True +\end{lstlisting} +\inctime{10} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Problem} +Solve the set of equations: +\begin{align*} + x + y + 2z -w & = 3\\ + 2x + 5y - z - 9w & = -3\\ + 2x + y -z + 3w & = -11 \\ + x - 3y + 2z + 7w & = -5\\ +\end{align*} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Solution} +Use \kwrd{solve()} +\begin{align*} + x & = -5\\ + y & = 2\\ + z & = 3\\ + w & = 0\\ +\end{align*} +\inctime{5} +\end{frame} + +\section{Finding Roots} + +\begin{frame}[fragile] +\frametitle{SciPy: \typ{roots}} +\begin{itemize} +\item Calculates the roots of polynomials +\item To calculate the roots of $x^2-5x+6$ +\end{itemize} +\begin{lstlisting} + In []: coeffs = [1, -5, 6] + In []: roots(coeffs) + Out[]: array([3., 2.]) +\end{lstlisting} +\vspace*{-.2in} +\begin{center} +\includegraphics[height=1.6in, interpolate=true]{data/roots} +\end{center} +\end{frame} + +\begin{frame}[fragile] +\frametitle{SciPy: \typ{fsolve}} +Find the root of $sin(z)+cos^2(z)$ nearest to $0$ +\vspace{-0.1in} +\begin{center} +\includegraphics[height=2.8in, interpolate=true]{data/fsolve} +\end{center} +\end{frame} + +\begin{frame}[fragile] +\frametitle{\typ{fsolve}} +\begin{small} +\begin{lstlisting} + In []: from scipy.optimize import fsolve +\end{lstlisting} +\end{small} +\begin{itemize} +\item Finds the roots of a system of non-linear equations +\item Input arguments - \alert{Function} and initial estimate +\item Returns the solution +\end{itemize} +\end{frame} + +\begin{frame}[fragile] +\frametitle{\typ{fsolve} \ldots} +\begin{lstlisting} +In []: def g(z): + ....: return sin(z)+cos(z)*cos(z) +In []: fsolve(g, 0) +Out[]: -0.66623943249251527 +\end{lstlisting} +\begin{center} +\includegraphics[height=2in, interpolate=true]{data/fsolve} +\end{center} +\inctime{10} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Exercise Problem} + Find the root of the equation $x^2 - sin(x) + cos^2(x) = tan(x)$ nearest to $0$ +\end{frame} + +\begin{frame}[fragile] + \frametitle{Solution} + \begin{small} + \begin{lstlisting} +def g(x): + return x**2 - sin(x) + cos(x)*cos(x) - tan(x) +fsolve(g, 0) + \end{lstlisting} + \end{small} + \vspace*{-0.2in} + \begin{center} +\includegraphics[height=2.5in, interpolate=true]{data/fsolve_tanx} +\end{center} +\vspace*{-0.5in} + \inctime{5} +\end{frame} + +%% \begin{frame}[fragile] +%% \frametitle{Scipy Methods \dots} +%% \begin{small} +%% \begin{lstlisting} +%% In []: from scipy.optimize import fixed_point + +%% In []: from scipy.optimize import bisect + +%% In []: from scipy.optimize import newton +%% \end{lstlisting} +%% \end{small} +%% \end{frame} + +\section{ODEs} + +\begin{frame} +\frametitle{Solving ODEs using SciPy} +\begin{itemize} +\item Consider the spread of an epidemic in a population + \vspace*{0.1in} +\item $\frac{dy}{dt} = ky(L-y)$ gives the spread of the disease + \vspace*{0.1in} +\item $L$ is the total population. +\item Use $L = 2.5E5, k = 3E-5, y(0) = 250$ +\end{itemize} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Solving ODEs using SciPy} +Define a function as below +\small +\begin{lstlisting} +In []: from scipy.integrate import odeint +In []: def epid(y, t): + ...: k = 3.0e-5 + ...: L = 2.5e5 + ...: return k*y*(L-y) + ...: +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Solving ODEs using SciPy \ldots} +\begin{lstlisting} +In []: t = linspace(0, 12, 61) + +In []: y = odeint(epid, 250, t) + +In []: plot(t, y) +\end{lstlisting} +%Insert Plot +\end{frame} + +\begin{frame}[fragile] +\frametitle{Result} +\begin{center} +\includegraphics[height=3in, interpolate=true]{data/image} +\end{center} +\vspace*{-0.5in} +\inctime{5} +\end{frame} + + +\begin{frame}[fragile] +\frametitle{ODEs - Simple Pendulum} +We shall use the simple ODE of a simple pendulum. +\begin{equation*} +\ddot{\theta} = -\frac{g}{L}sin(\theta) +\end{equation*} +\begin{itemize} +\item This equation can be written as a system of two first order ODEs +\end{itemize} +\begin{align} +\dot{\theta} &= \omega \\ +\dot{\omega} &= -\frac{g}{L}sin(\theta) \\ + \text{At}\ t &= 0 : \nonumber \\ + \theta = \theta_0(10^o)\quad & \&\quad \omega = 0\ (Initial\ values)\nonumber +\end{align} +\end{frame} + +\begin{frame}[fragile] +\frametitle{ODEs - Simple Pendulum \ldots} +\begin{itemize} +\item Use \typ{odeint} to do the integration +\end{itemize} +\begin{lstlisting} +In []: def pend_rhs(state, t): + .... theta = state[0] + .... omega = state[1] + .... g = 9.81 + .... L = 0.2 + .... F=[omega, -(g/L)*sin(theta)] + .... return F + .... +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] +\frametitle{ODEs - Simple Pendulum \ldots} +\begin{itemize} +\item \typ{t} is the time variable \\ +\item \typ{initial} has the initial values +\end{itemize} +\begin{lstlisting} +In []: t = linspace(0, 20, 101) +In []: initial = [10*2*pi/360, 0] +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] +\frametitle{ODEs - Simple Pendulum \ldots} +%%\begin{small} +\typ{In []: from scipy.integrate import odeint} +%%\end{small} +\begin{lstlisting} +In []: pend_sol = odeint(pend_rhs, + initial,t) +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Result} +\begin{center} +\includegraphics[height=2in, interpolate=true]{data/ode} +\end{center} + \inctime{10} +\end{frame} + +\section{FFTs} + +\begin{frame}[fragile] +\frametitle{The FFT} +\begin{itemize} + \item We have a simple signal $y(t)$ + \item Find the FFT and plot it +\end{itemize} +\begin{lstlisting} +In []: t = linspace(0, 2*pi, 500) +In []: y = sin(4*pi*t) + +In []: f = fft.fft(y) +In []: freq = fft.fftfreq(500, + ...: t[1] - t[0]) + +In []: plot(freq[:250], abs(f)[:250]) +In []: grid() +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] +\frametitle{FFTs cont\dots} +\begin{lstlisting} +In []: y1 = fft.ifft(f) # inverse FFT +In []: allclose(y, y1) +Out[]: True +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] +\frametitle{FFTs cont\dots} +Let us add some noise to the signal +\begin{lstlisting} +In []: yr = y + + ...: random.random(size=500)*0.2 +In []: yn = y + + ...: random.normal(size=500)*0.2 + +In []: plot(t, yr) +In []: figure() +In []: plot(freq[:250], + ...: abs(fft.fft(yr))[:250]) +\end{lstlisting} +\begin{itemize} + \item \typ{random}: produces uniform deviates in $[0, 1)$ + \item \typ{normal}: draws random samples from a Gaussian + distribution + \item Useful to create a random matrix of any shape +\end{itemize} +\end{frame} + +\begin{frame}[fragile] +\frametitle{FFTs cont\dots} +Filter the noisy signal: +\begin{lstlisting} +In []: from scipy import signal +In []: yc = signal.wiener(yn, 5) +In []: clf() +In []: plot(t, yc) +In []: figure() +In []: plot(freq[:250], + ...: abs(fft.fft(yc))[:250]) +\end{lstlisting} +Only scratched the surface here \dots + +\inctime{10} +\end{frame} + + +\begin{frame} + \frametitle{Things we have learned} + \begin{itemize} + \item Solving Linear Equations + \item Defining Functions + \item Finding Roots + \item Solving ODEs + \item FFTs and basic signal processing + \end{itemize} +\end{frame} + + +\begin{frame} + \frametitle{Further reading} + \begin{itemize} + \item \url{ipython.readthedocs.io} + \item \url{matplotlib.org/contents.html} + \item \url{docs.scipy.org/doc/numpy/user/quickstart.html} + \item \url{docs.scipy.org/doc/scipy/reference/tutorial} + \end{itemize} +\end{frame} + +\end{document} diff --git a/scipy/basic/09_exercises.tex b/scipy/basic/09_exercises.tex new file mode 100644 index 0000000..78ba335 --- /dev/null +++ b/scipy/basic/09_exercises.tex @@ -0,0 +1,476 @@ +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%Tutorial slides on Python. +% +% Author: Prabhu Ramachandran +% Copyright (c) 2005-2009, Prabhu Ramachandran +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +\documentclass[14pt,compress]{beamer} +%\documentclass[draft]{beamer} +%\documentclass[compress,handout]{beamer} +%\usepackage{pgfpages} +%\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm] + +% Modified from: generic-ornate-15min-45min.de.tex +\mode +{ + \usetheme{Warsaw} + \useoutertheme{infolines} + \setbeamercovered{transparent} +} + +\usepackage[english]{babel} +\usepackage[latin1]{inputenc} +%\usepackage{times} +\usepackage[T1]{fontenc} + +% Taken from Fernando's slides. +\usepackage{ae,aecompl} +\usepackage{mathpazo,courier,euler} +\usepackage[scaled=.95]{helvet} + +\definecolor{darkgreen}{rgb}{0,0.5,0} + +\usepackage{listings} +\lstset{language=Python, + basicstyle=\ttfamily\bfseries, + commentstyle=\color{red}\itshape, + stringstyle=\color{darkgreen}, + showstringspaces=false, + keywordstyle=\color{blue}\bfseries} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Macros +\setbeamercolor{emphbar}{bg=blue!20, fg=black} +\newcommand{\emphbar}[1] +{\begin{beamercolorbox}[rounded=true]{emphbar} + {#1} + \end{beamercolorbox} +} +\newcounter{time} +\setcounter{time}{0} +\newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}} + +\newcommand{\typ}[1]{\lstinline{#1}} + +\newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}} } + +%%% This is from Fernando's setup. +% \usepackage{color} +% \definecolor{orange}{cmyk}{0,0.4,0.8,0.2} +% % Use and configure listings package for nicely formatted code +% \usepackage{listings} +% \lstset{ +% language=Python, +% basicstyle=\small\ttfamily, +% commentstyle=\ttfamily\color{blue}, +% stringstyle=\ttfamily\color{orange}, +% showstringspaces=false, +% breaklines=true, +% postbreak = \space\dots +% } + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Title page +\title[Exercises]{Introductory Scientific Computing with Python} +\subtitle{Exercises} + +\author[FOSSEE] {FOSSEE} + +\institute[FOSSEE -- IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} +\date[] {Mumbai, India} +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +%\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} +%\logo{\pgfuseimage{iitmlogo}} + + +%% Delete this, if you do not want the table of contents to pop up at +%% the beginning of each subsection: +\AtBeginSubsection[] +{ + \begin{frame} + \frametitle{Outline} + \tableofcontents[currentsection,currentsubsection] + \end{frame} +} + + +% If you wish to uncover everything in a step-wise fashion, uncomment +% the following command: +%\beamerdefaultoverlayspecification{<+->} + +%\includeonlyframes{current,current1,current2,current3,current4,current5,current6} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% DOCUMENT STARTS +\begin{document} + +\begin{frame} + \titlepage +\end{frame} + + +\begin{frame}[fragile] + \frametitle{Problem 1} + \begin{columns} + \column{0.5\textwidth} + \hspace*{-0.5in} + \includegraphics[height=2in, interpolate=true]{data/L-Tsq.png} + \column{0.45\textwidth} + \begin{block}{Example code} + \tiny +\begin{lstlisting} +In []: l, t = loadtxt('pendulum.txt', + unpack=True) +In []: plot(l, t, '.') + \end{lstlisting} + \end{block} + \end{columns} + \begin{block}{Problem Statement} + Tweak above code to plot data in file \typ{pos.txt}. + \end{block} +\end{frame} + +\begin{frame} + \frametitle{Problem 1 cont...} + \begin{itemize} + \item Label both the axes. + \item What kind of motion is this? + \item Title the graph accordingly. + \item Annotate the position where vertical velocity is zero. + \end{itemize} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Solution} +\begin{lstlisting} +x, y = loadtxt('pos.txt', unpack=True) +plot(x, y) +xlabel('x') +ylabel('y') +title('Projectile motion') +annotate('v = 0', xy=(5, 4.75)) +# Or +annotate(r'$v_y = 0$', xy=(5, 4.75)) +\end{lstlisting} + \begin{itemize} + \item Note the \LaTeX\ syntax + \item Note the raw strings: \typ{r'$v_y = 0$'} + \end{itemize} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Problem 2} + \begin{columns} + \column{0.5\textwidth} + \hspace*{-0.5in} + \includegraphics[height=2in, interpolate=true]{data/exercises/triangle} + \column{0.45\textwidth} + \begin{block}{Plot points given x and y coordinates} + \tiny + \begin{lstlisting} +In []: x = [3, 2, -2, 3] +In []: y = [1, -3, 4, 1] +In []: plot(x, y) + \end{lstlisting} + \end{block} + \end{columns} + Line can be plotted using arrays of coordinates. + \pause + \begin{block}{Problem statement} + Write a Program that plots a regular n-gon(Let n = 5). + \end{block} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Solution} +\begin{lstlisting} +n = 5 +t = linspace(0, 2*pi, n+1) +x = cos(t) +y = sin(t) +plot(x, y) +axis('equal') +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Better Solution} +\begin{lstlisting} +def plot_ngon(n): + t = linspace(0, 2*pi, n+1) + x = cos(t) + y = sin(t) + plot(x, y) + axis('equal') + +plot_ngon(5) +clf() +plot_ngon(10) +\end{lstlisting} +\end{frame} + + +\begin{frame}[fragile] + \frametitle{Problem 3} + \begin{columns} + \column{0.5\textwidth} + \hspace*{-0.5in} + \includegraphics[height=2in, interpolate=true]{data/exercises/damp} + \column{0.45\textwidth} + \begin{block}{Damped Oscillation} + \tiny + \begin{lstlisting} +In []: t = linspace(0, 4*pi) +In []: plot(t, exp(-t/10)*sin(t)) + \end{lstlisting} + \end{block} + \end{columns} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Problem 3 cont...} +Create a sequence of images (say 10) in which the damped +oscillator($e^{-t/10}sin(t)$) slowly evolves over time $t$. +\begin{columns} +\column{0.35\textwidth} +\includegraphics[width=1.5in,height=1.5in, interpolate=true]{data/exercises/plot2} +\column{0.35\textwidth} +\includegraphics[width=1.5in,height=1.5in, interpolate=true]{data/exercises/plot4} +\column{0.35\textwidth} +\includegraphics[width=1.5in,height=1.5in, interpolate=true]{data/exercises/plot6} +\end{columns} +\begin{block}{Hint} +\small + \begin{lstlisting} +savefig('plot'+str(i)+'.png') #i is some int + \end{lstlisting} +\end{block} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Naive Solution} +\begin{lstlisting} +for i in range(1, 11): + t = linspace(0, 0.5*pi*i, 50*i) + clf() + plot(t, exp(-t/10)*sin(t)) + savefig('plot' + str(i) + '.png') +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Better Solution} +\begin{lstlisting} +for i in range(1, 11): + t = linspace(0, 0.5*pi*i, 50*i) + clf() + plot(t, exp(-t/10)*sin(t)) + xlim(0, 5*pi) + ylim(-1, 1) + savefig('plot' + str(i) + '.png') +\end{lstlisting} +\end{frame} + + +\begin{frame}[fragile] + \frametitle{Problem 4} + \begin{lstlisting} +In []: x = imread('smoothing.gif') +In []: x.shape +Out[]: (256, 256) +In []: imshow(x,cmap=cm.gray) +In []: colorbar() +\end{lstlisting} +\emphbar{Replace each pixel with mean of neighboring pixels} + \begin{center} + \includegraphics[height=1in, interpolate=true]{data/exercises/neighbour} + \end{center} +\end{frame} + +\begin{frame} + \begin{center} + \includegraphics[height=3in, interpolate=true]{data/exercises/smoothing} + \end{center} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Problem 4: Approach} + For \typ{y} being resultant image: + \begin{lstlisting} +y[1, 1] = x[0, 1]/4 + x[1, 0]/4 + + x[2, 1]/4 + x[1, 2]/4 + \end{lstlisting} + \begin{columns} + \column{0.45\textwidth} + \hspace*{-0.5in} + \includegraphics[height=1.5in, interpolate=true]{data/exercises/smoothing} + \column{0.45\textwidth} + \hspace*{-0.5in} + \includegraphics[height=1.5in, interpolate=true]{data/exercises/after-filter} + \end{columns} + \begin{block}{Hint:} + Use array Slicing. + \end{block} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Solution} + \begin{lstlisting} +In []: y = zeros_like(x) +In []: y[1:-1,1:-1] = x[:-2,1:-1]/4 + + x[2:,1:-1]/4 + + x[1:-1,2:]/4 + + x[1:-1,:-2]/4 +In []: imshow(y,cmap=cm.gray) + \end{lstlisting} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Problem 4 cont\ldots} + \begin{itemize} + \item Apply the smoothing operation repeatedly to the original + image + + \item Subtract the smoothed image from the original to obtain + the edges + \end{itemize} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Problem 5} + + What if you did the following in problem 4? +\begin{lstlisting} +In []: y1 = zeros_like(x) +In []: y1[1:-1,1:-1] = (x[:-2,1:-1] + + x[2:,1:-1] + + x[1:-1,2:] + + x[1:-1,:-2])/4 + \end{lstlisting} + + Are the answers different? + +\end{frame} + +\begin{frame}[fragile] + \frametitle{Problem 5 cont\ldots} + Why? The answer lies in the following: +\begin{lstlisting} +In []: x.dtype +Out[]: dtype('uint8') + +In []: print(x.itemsize) +1 + +In []: z = x/4.0 + +In []: print(z.dtype) +float64 + +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Problem 5 cont\ldots} +What if you did this? + +\begin{lstlisting} +x = imread('smoothing.gif') +y2 = zeros_like(x) +y2[1:-1,1:-1] = x[:-2,1:-1]/4. + \ + x[2:,1:-1]/4. + \ + x[1:-1,2:]/4. + \ + x[1:-1,:-2]/4. +\end{lstlisting} +\begin{itemize} + \item Will the answer be any different from \typ{y}? + \item What will the dtype of \typ{y2} be? + \item Discuss what is going on! +\end{itemize} + +\end{frame} + +\begin{frame}[fragile] + \frametitle{Problem 5 cont\ldots} + \begin{itemize} + \item Did you do the right thing to find the edges earlier in problem 4? + \item If not, fix it! + \end{itemize} + Note that: + \small +\begin{lstlisting} +In []: print(x.dtype) +uint8 +In []: x1 = x.astype('float64') +In []: print(x1.dtype) +float64 +In []: print(x.dtype.char) +d +In []: x.dtype. # Explore! +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Problem 6} + + Edge detection looks much nicer with \typ{lena.png}, try it! The + caveat is that it is a 4 component RGBA image with elements in the + range $[0.0, 1.0]$. + +\begin{lstlisting} +In []: x = imread('lena.png') + +In []: print(x.shape) +(512, 512, 4) + +In []: print(x.min(), x.max()) +(0.0, 1.0) +\end{lstlisting} + + Repeat the edge detection with this image. + +\end{frame} + + +\end{document} + +%% \begin{frame} +%% \frametitle{Problem 4} +%% Legendre polynomials $P_n(x)$ are defined by the following recurrence relation + +%% \center{$(n+1)P_{n+1}(x) - (2n+1)xP_n(x) + nP_{n-1}(x) = 0$}\\ + +%% with $P_0(x) = 1$, $P_1(x) = x$ and $P_2(x) = (3x^2 - 1)/2$. Compute the next three +%% Legendre polynomials and plot all 6 over the interval [-1,1]. +%% \end{frame} + +%% \begin{frame}[fragile] +%% \frametitle{Problem Set 5} +%% \begin{columns} +%% \column{0.6\textwidth} +%% \small{ +%% \begin{itemize} +%% \item[3] Consider the iteration $x_{n+1} = f(x_n)$ where $f(x) = kx(1-x)$. Plot the successive iterates of this process as explained below. +%% \end{itemize}} +%% \column{0.35\textwidth} +%% \hspace*{-0.5in} +%% \includegraphics[height=1.6in, interpolate=true]{data/cobweb} +%% \end{columns} +%% \end{frame} + +%% \begin{frame} +%% \frametitle{Problem Set 5.3} +%% Plot the cobweb plot as follows: +%% \begin{enumerate} +%% \item Start at $(x_0, 0)$ ($\implies$ i=0) +%% \item Draw a line to $(x_i, f(x_i))$ +%% \item Set $x_{i+1} = f(x_i)$ +%% \item Draw a line to $(x_{i+1}, x_{i+1})$ +%% \item $(i\implies i+1)$ +%% \item Repeat from 2 for as long as you want +%% \end{enumerate} +%% \inctime{20} +%% \end{frame} diff --git a/scipy/basic/10_mlab.tex b/scipy/basic/10_mlab.tex new file mode 100644 index 0000000..5449b1f --- /dev/null +++ b/scipy/basic/10_mlab.tex @@ -0,0 +1,523 @@ +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%Tutorial slides on Python. +% +% Author: FOSSEE +% Copyright (c) 2009-2016, FOSSEE, IIT Bombay +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +\documentclass[14pt,compress]{beamer} +%\documentclass[draft]{beamer} +%\documentclass[compress,handout]{beamer} +%\usepackage{pgfpages} +%\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm] + +% Modified from: generic-ornate-15min-45min.de.tex +\mode +{ + \usetheme{Warsaw} + \useoutertheme{infolines} + \setbeamercovered{transparent} +} + +\usepackage[english]{babel} +\usepackage[latin1]{inputenc} +%\usepackage{times} +\usepackage[T1]{fontenc} +\usepackage{pgf} + +% Taken from Fernando's slides. +\usepackage{ae,aecompl} +\usepackage{mathpazo,courier,euler} +\usepackage[scaled=.95]{helvet} +\usepackage{amsmath} + +\definecolor{darkgreen}{rgb}{0,0.5,0} + +\usepackage{listings} +\lstset{language=Python, + basicstyle=\ttfamily\bfseries, + commentstyle=\color{red}\itshape, + stringstyle=\color{darkgreen}, + showstringspaces=false, + keywordstyle=\color{blue}\bfseries} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Macros +\setbeamercolor{emphbar}{bg=blue!20, fg=black} +\newcommand{\emphbar}[1] +{\begin{beamercolorbox}[rounded=true]{emphbar} + {#1} + \end{beamercolorbox} +} + +\newcommand{\myemph}[1]{\structure{\emph{#1}}} +\newcommand{\PythonCode}[1]{\lstinline{#1}} + +\newcounter{time} +\setcounter{time}{0} +\newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}} + +\newcommand{\typ}[1]{\lstinline{#1}} + +\newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}} } + +%%% This is from Fernando's setup. +% \usepackage{color} +% \definecolor{orange}{cmyk}{0,0.4,0.8,0.2} +% % Use and configure listings package for nicely formatted code +% \usepackage{listings} +% \lstset{ +% language=Python, +% basicstyle=\small\ttfamily, +% commentstyle=\ttfamily\color{blue}, +% stringstyle=\ttfamily\color{orange}, +% showstringspaces=false, +% breaklines=true, +% postbreak = \space\dots +% } + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Title page +\title[Mayavi mlab]{Introductory Scientific Computing with +Python} +\subtitle{Simple 3D plots with Mayavi's mlab} + +\author[Prabhu] {FOSSEE} + +\institute[FOSSEE -- IITB] {Department of Aerospace Engineering\\IIT Bombay} +\date[] { +Mumbai +} +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +%\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} +%\logo{\pgfuseimage{iitmlogo}} + + +%% Delete this, if you do not want the table of contents to pop up at +%% the beginning of each subsection: +\AtBeginSubsection[] +{ + \begin{frame} + \frametitle{Outline} + \tableofcontents[currentsection,currentsubsection] + \end{frame} +} + +\AtBeginSection[] +{ + \begin{frame} + \frametitle{Outline} + \tableofcontents[currentsection,currentsubsection] + \end{frame} +} + +% If you wish to uncover everything in a step-wise fashion, uncomment +% the following command: +%\beamerdefaultoverlayspecification{<+->} + +%\includeonlyframes{current,current1,current2,current3,current4,current5,current6} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% DOCUMENT STARTS +\begin{document} + +\begin{frame} + \maketitle +\end{frame} + +\section{Introduction to Mayavi} + +\begin{frame} + \frametitle{Mayavi} + \begin{itemize} + \item 3D visualization library + \item Application + \item Support for different data formats + \item Embeddable, extensible + \item Cross-platform + \item Fully scriptable + \end{itemize} +\end{frame} + +\begin{frame} + {Overview of features} + \vspace*{-0.3in} + \begin{center} + \hspace*{-0.2in}\pgfimage[width=5in]{mlab_data/m2_app3_3} + \end{center} +\end{frame} + + +\begin{frame} + \frametitle{Mayavi in applications} + \vspace*{-0.3in} + \begin{center} + \hspace*{-0.2in}\pgfimage[width=4.5in]{mlab_data/m2_envisage} + \end{center} +\end{frame} + +\begin{frame} + \frametitle{Live in your dialogs} + \vspace*{0.1in} + \begin{center} + \hspace*{-0.2in}\pgfimage[width=2.5in]{mlab_data/mlab_tui} + \end{center} +\end{frame} + + + +\begin{frame} + \frametitle{Installation} + \begin{itemize} + \item Suggest using a Python distribution + \item Canopy's package manager: simply install mayavi + \item \typ{conda install -c menpo mayavi} + \item \typ{edm install mayavi} + \end{itemize} +\end{frame} + +\section{Getting started with mlab} + +\begin{frame} + {Overview} + \begin{itemize} + \item Simple + \item Convenient + \item Full-featured + \end{itemize} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Getting started} + + \myemph{With \typ{jupyter}:} +\begin{lstlisting}[language=bash] + $ jupyter console + In [1]: %gui qt +\end{lstlisting} %$ + \vspace*{0.15in} + + \myemph{Vanilla:} +\begin{lstlisting}[language=bash] + $ ipython --gui=qt +\end{lstlisting} %$ + \vspace*{0.15in} + +\myemph{Or:} +\begin{lstlisting}[language=bash] + $ ipython + In [1]: %gui qt +\end{lstlisting} %$ + +\end{frame} + +\begin{frame}[fragile] + \frametitle{Using notebooks} + + Make sure you have the following code first: + +\begin{lstlisting} + In [1]: from mayavi import mlab + In [2]: mlab.init_notebook() +\end{lstlisting} + + \begin{itemize} + \item Can also use \typ{\%gui qt} + \item Plots will pop-up + \end{itemize} + +\end{frame} + +\section{Basic Mayavi: \typ{mlab}} + +\begin{frame}[fragile] +\frametitle{Basic 3D visualization} +\begin{itemize} + \item Simple one liners for 3D visualization + \item Must import mlab. +\end{itemize} +\begin{lstlisting} +In []: from mayavi import mlab +\end{lstlisting} +Ready to go! +\end{frame} + +\begin{frame}[fragile] + \frametitle{Simple example} + + \begin{lstlisting} +In []: mlab.test_ + +# Let us start with this: +In []: mlab.test_contour3d() +\end{lstlisting} +Explore the UI. +\end{frame} + +\begin{frame} + {Exploring the view} + \begin{columns} + \column{0.6\textwidth} + \pgfimage[width=3in]{mlab_data/mlab_contour_window} + \column{0.4\textwidth} + \begin{itemize} + \item Mouse + \item Keyboard + \item Toolbar + \item Mayavi icon\pgfimage[width=0.2in]{mlab_data/m2_icon} + \end{itemize} + \end{columns} +\end{frame} + +\begin{frame}[fragile] + \frametitle{\typ{mlab} plotting functions} + \begin{columns} + \column{0.25\textwidth} + \myemph{\Large 0D data} + \column{0.5\textwidth} + \pgfimage[width=2in]{mlab_data/points3d_ex} + \end{columns} + + \begin{lstlisting} +In []: t = linspace(0, 2*pi, 50) +In []: u = cos(t) * pi +In []: x, y, z = sin(u), cos(u), sin(t) + \end{lstlisting} + \emphbar{\PythonCode{In []: mlab.points3d(x, y, z)}} +\end{frame} + +\begin{frame}[plain, fragile] + \frametitle{Changing how things look} + + \begin{block}{Clearing the view} + \PythonCode{>>> mlab.clf()} + \end{block} + + \pause + + \begin{block}{IPython is your friend!} + \PythonCode{>>> mlab.points3d?} + + \begin{itemize} + + \item Extra argument: Scalars + + \item Keyword arguments + + \item UI + \end{itemize} + \end{block} +\pause + \begin{lstlisting} +In []: mlab.points3d(x, y, z, t, + scale_mode='none') + \end{lstlisting} +\end{frame} + + +\begin{frame}[fragile] + \begin{columns} + \column{0.25\textwidth} + \myemph{\Large 1D data} + \column{0.5\textwidth} + \pgfimage[width=2.5in]{mlab_data/plot3d_ex} + \end{columns} + \emphbar{\PythonCode{In []: mlab.plot3d(x, y, z, t)}} + + Plots lines between the points + +\end{frame} + +\begin{frame}[fragile] + \begin{columns} + \column{0.25\textwidth} + \myemph{\Large 2D data} + \column{0.5\textwidth} + \pgfimage[width=2in]{mlab_data/surf_ex} + \end{columns} + \begin{lstlisting} +In []: x, y = mgrid[-3:3:100j,-3:3:100j] +In []: z = sin(x*x + y*y) + \end{lstlisting} + + \emphbar{\PythonCode{In []: mlab.surf(x, y, z)}} + + \alert{Assumes the points are rectilinear} + +\end{frame} + +\begin{frame}[fragile] + \frametitle{mgrid example} + \begin{lstlisting} +In []: mgrid[0:3,0:3] +Out[]: +array([[[0, 0, 0], + [1, 1, 1], + [2, 2, 2]], + + [[0, 1, 2], + [0, 1, 2], + [0, 1, 2]]]) + +In []: mgrid[-1:1:5j] +Out[]: array([-1., -0.5, 0., 0.5, 1.]) +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] + \frametitle{mgrid example} + \begin{lstlisting} +In []: x, y = mgrid[-1:1:5j, -1:1:5j] +In []: z = x*x + y*y + +In []: z +Out[]: +array([[ 2. , 1.25, 1. , 1.25, 2. ], + [ 1.25, 0.5 , 0.25, 0.5 , 1.25], + [ 1. , 0.25, 0. , 0.25, 1. ], + [ 1.25, 0.5 , 0.25, 0.5 , 1.25], + [ 2. , 1.25, 1. , 1.25, 2. ]]) +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] + \myemph{\Large 2D data: \texttt{mlab.mesh}} + \vspace*{0.25in} + + \emphbar{\PythonCode{In []: mlab.mesh(x, y, z)}} + + \alert{Points needn't be regular} + + \vspace*{0.25in} +\begin{lstlisting} +In []: phi, theta = mgrid[0:pi:20j, +... 0:2*pi:20j] +In []: x = sin(phi)*cos(theta) +In []: y = sin(phi)*sin(theta) +In []: z = cos(phi) +In []: mlab.mesh(x, y, z, +... representation= +... 'wireframe') +\end{lstlisting} + +\end{frame} + + +\begin{frame}[fragile] + + \myemph{\Large 3D data} + \vspace*{0.25in} + + \pgfimage[width=1.5in]{mlab_data/contour3d}\\ + +\begin{lstlisting} +In []: x, y, z = mgrid[-5:5:64j, + ...: -5:5:64j, + ...: -5:5:64j] +In []: mlab.contour3d(x*x*0.5 + y*y + + z*z*2) +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] + + \myemph{\Large 3D vector data: \PythonCode{mlab.quiver3d}} + \vspace*{0.25in} + + \pgfimage[width=2in]{mlab_data/quiver3d_ex}\\ + +\begin{lstlisting} +In []: mlab.test_quiver3d() +\end{lstlisting} + +\emphbar{\PythonCode{obj = mlab.quiver3d(x, y, z, u, v, w)}} +\end{frame} + + +\begin{frame}[fragile] + {Other utility functions} + \begin{itemize} + \item \PythonCode{gcf}: get current figure + \pause + \item \PythonCode{savefig}, \PythonCode{figure} + \pause + \item \PythonCode{axes}, \PythonCode{outline} + \pause + \item \PythonCode{title}, \PythonCode{xlabel, ylabel, zlabel} + \pause + \item \PythonCode{colorbar}, \PythonCode{scalarbar}, + \PythonCode{vectorbar} + \pause + \item \PythonCode{show}: Standalone mlab scripts + \pause + \item Others, see UG + \end{itemize} +\end{frame} + + +\begin{frame} + {Exploring the documentation} + \begin{center} + \pgfimage[width=4in]{mlab_data/m2_ug_doc} + \end{center} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Explore the examples} + + \myemph{\Large Try these} + +\begin{lstlisting} +In []: mlab.clf() +In []: mlab.test_points3d() +In []: mlab.clf() +In []: mlab.test_plot3d() +In []: mlab.clf() +\end{lstlisting} + Explore these later +\end{frame} + + +\begin{frame} + \frametitle{Summary} + \begin{itemize} + \item Quick introduction to Mayavi's mlab + \item Documentation: \url{github.enthought.com/mayavi/mayavi} + \end{itemize} + +\end{frame} + +\end{document} + + +\begin{frame} + {An exercise} + + \begin{block}{Exercise} + Atmospheric data of temperature over the surface of the earth. + Let temperature ($T$) vary linearly with height ($z$): + \begin{center} + $T = 288.15 - 6.5z$ + \end{center} + \end{block} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Simple solution} + + \begin{lstlisting} +lat = linspace(-89, 89, 37) +lon = linspace(0, 360, 37) +z = linspace(0, 100, 11) + \end{lstlisting} +\pause + \begin{lstlisting} +x, y, z = mgrid[0:360:37j,-89:89:37j, + 0:100:11j] +t = 288.15 - 6.5*z +mlab.contour3d(x, y, z, t) +mlab.outline() +mlab.colorbar() + \end{lstlisting} +\end{frame} diff --git a/scipy/basic/11_notebook.tex b/scipy/basic/11_notebook.tex new file mode 100644 index 0000000..4442c9f --- /dev/null +++ b/scipy/basic/11_notebook.tex @@ -0,0 +1,325 @@ +\documentclass[14pt,compress]{beamer} +%\documentclass[draft]{beamer} +%\documentclass[compress,handout]{beamer} +%\usepackage{pgfpages} +%\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm] + +% Modified from: generic-ornate-15min-45min.de.tex +\mode +{ + \usetheme{Warsaw} + \useoutertheme{infolines} + \setbeamercovered{transparent} +} + +\usepackage[english]{babel} +\usepackage[latin1]{inputenc} +%\usepackage{times} +\usepackage[T1]{fontenc} +\usepackage{pgf} + +% Taken from Fernando's slides. +\usepackage{ae,aecompl} +\usepackage{mathpazo,courier,euler} +\usepackage[scaled=.95]{helvet} +\usepackage{amsmath} + +\definecolor{darkgreen}{rgb}{0,0.5,0} + +\usepackage{listings} +\lstset{language=Python, + basicstyle=\ttfamily\bfseries, + commentstyle=\color{red}\itshape, + stringstyle=\color{darkgreen}, + showstringspaces=false, + keywordstyle=\color{blue}\bfseries} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Macros +\setbeamercolor{emphbar}{bg=blue!20, fg=black} +\newcommand{\emphbar}[1] +{\begin{beamercolorbox}[rounded=true]{emphbar} + {#1} + \end{beamercolorbox} +} + +\newcommand{\myemph}[1]{\structure{\emph{#1}}} +\newcommand{\PythonCode}[1]{\lstinline{#1}} + +\newcounter{time} +\setcounter{time}{0} +\newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}} + +\newcommand{\typ}[1]{\lstinline{#1}} + +\newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}} } + + +\newcommand\BackgroundPicture[1]{% + \setbeamertemplate{background}{% + \parbox[c][\paperheight]{\paperwidth}{% + \vfill \hfill + \pgfimage[width=1.0\paperwidth,height=1.0\paperheight]{#1} + \hfill \vfill +}}} + +% For non-wide pictures, set the width so that the height scales +% appropriately. +\newcommand\BackgroundPictureWidth[1]{% + \setbeamertemplate{background}{% + \parbox[c][\paperheight]{\paperwidth}{% + \vfill \hfill + \pgfimage[width=1.0\paperwidth]{#1} + \hfill \vfill +}}} + +% For shorter pictures, set the height so that the width scales +% appropriately. +\newcommand\BackgroundPictureHeight[1]{% + \setbeamertemplate{background}{% + \parbox[c][\paperheight]{\paperwidth}{% + \vfill \hfill + \pgfimage[height=1.0\paperheight]{#1} + \hfill \vfill +}}} + +%%% This is from Fernando's setup. +% \usepackage{color} +% \definecolor{orange}{cmyk}{0,0.4,0.8,0.2} +% % Use and configure listings package for nicely formatted code +% \usepackage{listings} +% \lstset{ +% language=Python, +% basicstyle=\small\ttfamily, +% commentstyle=\ttfamily\color{blue}, +% stringstyle=\ttfamily\color{orange}, +% showstringspaces=false, +% breaklines=true, +% postbreak = \space\dots +% } + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Title page +\title[IPython notebooks]{Introductory Scientific Computing with +Python} +\subtitle{IPython notebooks} + +\author{FOSSEE} + +\institute[FOSSEE -- IITB] {Department of Aerospace Engineering\\IIT Bombay} +\date[] { +Mumbai, India +} +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +%\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} +%\logo{\pgfuseimage{iitmlogo}} + + +%% Delete this, if you do not want the table of contents to pop up at +%% the beginning of each subsection: +\AtBeginSubsection[] +{ + \begin{frame} + \frametitle{Outline} + \tableofcontents[currentsection,currentsubsection] + \end{frame} +} + +\AtBeginSection[] +{ + \begin{frame} + \frametitle{Outline} + \tableofcontents[currentsection,currentsubsection] + \end{frame} +} + +% If you wish to uncover everything in a step-wise fashion, uncomment +% the following command: +%\beamerdefaultoverlayspecification{<+->} + +%\includeonlyframes{current,current1,current2,current3,current4,current5,current6} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% DOCUMENT STARTS +\begin{document} + +\begin{frame} + \maketitle +\end{frame} + +\begin{frame}[fragile] + \frametitle{Introduction} + \begin{itemize} + \item Have used the IPython console so far + \begin{itemize} + \item Terminal: \typ{ipython} + \item GUI: \typ{qtconsole} + \end{itemize} + \item Powerful and convenient + \item Must be installed as a package + \end{itemize} +\end{frame} + +\begin{frame} + \frametitle{IPython notebook} + \begin{itemize} + \item Create and share documents containing + \begin{itemize} + \item live code + \item equations + \item visualizations + \item interactive widgets + \item explanatory text + \end{itemize} + \item A web application + \end{itemize} +\end{frame} + + +\BackgroundPicture{data/notebook/jupyter-interface} +\begin{frame}[plain] +\end{frame} +\BackgroundPicture{data/intro/blank} + +\begin{frame} + \frametitle{Jupyter} + \begin{itemize} + \item Open source, interactive data science and computing + \item Brings IPython-like features to other languages + \item Console + \item Notebooks + \item Other tools: jupyterhub, nbviewer, etc. + \end{itemize} +\end{frame} + +\BackgroundPictureWidth{data/notebook/jupyter_logo} +\begin{frame}[plain] +\end{frame} +\BackgroundPicture{data/intro/blank} + +\begin{frame} + \frametitle{IPython and Jupyter} + \begin{itemize} + \item IPython provides Python specific functionality + \item Python \textbf{kernel} + \end{itemize} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Getting started: IPython} +Console + \begin{lstlisting} +$ ipython +\end{lstlisting} %$ +\vspace*{0.25in} +The notebook: + \begin{lstlisting} +$ ipython notebook +\end{lstlisting} %$ +\end{frame} + +\begin{frame}[fragile] + \frametitle{Getting started: Jupyter} + \begin{lstlisting} +$ jupyter console +\end{lstlisting} %$ +\vspace*{0.25in} +The notebook: + \begin{lstlisting} +$ jupyter notebook +\end{lstlisting} %$ +\end{frame} + +\begin{frame}[fragile] + \frametitle{IPython notebooks and Canopy} + \begin{itemize} + \item Can directly open \typ{*.ipynb} files from Canopy + \item Or create a new notebook using the File menu + \end{itemize} +\end{frame} + + +\begin{frame}[fragile] + \frametitle{Pylab mode} + \begin{lstlisting} +In []: %pylab + \end{lstlisting} %$ +Or: + \begin{lstlisting} +In []: %matplotlib + \end{lstlisting} %$ +\end{frame} + +\begin{frame}[fragile] + \frametitle{Using matplotlib} + \small + \begin{lstlisting} +In []: %matplotlib +In []: from matplotlib import pyplot as plt +In []: from numpy import linspace, sin +In []: x = linspace(0, 2*pi) +In []: plt.plot(x, sin(x)) + \end{lstlisting} +\end{frame} + +\begin{frame}[fragile] + \frametitle{What is the difference?} + \begin{itemize} + \item \typ{\%pylab}: imports pylab + \item \typ{\%matplotlib}: just sets up the plotting, no imports + \item Using explicit imports is cleaner + \item \typ{pyplot} provides the useful functionality + \item Could also just import \typ{pylab} + \end{itemize} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Using the IPython notebook} + + \begin{itemize} + \item Start the notebook + \item Try: + \typ{jupyter notebook} + \item If that doesn't work use \typ{ipython notebook} + \item Create a new Python 2 or Python 3 notebook + \end{itemize} +\end{frame} + +\begin{frame} + \frametitle{Basic Notebook Demo} + \begin{center} + Open the \typ{sample.ipynb} file provided + \end{center} +\end{frame} + +\begin{frame} + \frametitle{Exercise} + \begin{itemize} + \item Start the notebook + \item Create a new notebook + \item Try the interface tour (Help->Interface tour) + \item Add some markdown text, an image, and a simple equation + \item Write some simple code in multiple cells + \item Add code from \typ{four_plot.py} and run it + \end{itemize} +\end{frame} + +\begin{frame}[fragile] + \frametitle{More resources} + \begin{itemize} + \item \href{https://daringfireball.net/projects/markdown/syntax}{Markdown syntax} + \item \href{http://ipython.org}{IPython website} + \item \href{http://ipython.org/documentation.html}{IPython documentation} + \item \href{http://jupyter.org}{Jupyter website} + \item \href{https://jupyter.readthedocs.io/en/latest/index.html}{Jupyter documentation} + \item + \href{https://www.dataquest.io/blog/jupyter-notebook-tips-tricks-shortcuts/}{Jupyter + notebook tips and tricks} + \item \href{https://nbviewer.jupyter.org}{Example notebooks} + \item \href{http://tbc-python.fossee.in}{Python textbook companions} + \end{itemize} +\end{frame} + + +\end{document} diff --git a/scipy/basic/Makefile b/scipy/basic/Makefile index 94a29fe..3ffa1a3 100644 --- a/scipy/basic/Makefile +++ b/scipy/basic/Makefile @@ -4,15 +4,15 @@ pdflatex $*.tex pdflatex $*.tex -SLIDES= intro.pdf prelims.pdf ipython_plotting.pdf \ - saving_scripts.pdf \ - lists_arrays.pdf \ - numpy.pdf \ - more_numpy.pdf \ - basic_scipy.pdf \ - exercises.pdf \ - mlab.pdf \ - notebook.pdf +SLIDES= 01_intro.pdf 02_prelims.pdf 03_ipython_plotting.pdf \ + 04_saving_scripts.pdf \ + 05_lists_arrays.pdf \ + 06_numpy.pdf \ + 07_more_numpy.pdf \ + 08_basic_scipy.pdf \ + 09_exercises.pdf \ + 10_mlab.pdf \ + 11_notebook.pdf all: $(SLIDES) quickref.pdf diff --git a/scipy/basic/basic_scipy.tex b/scipy/basic/basic_scipy.tex deleted file mode 100644 index dca675d..0000000 --- a/scipy/basic/basic_scipy.tex +++ /dev/null @@ -1,509 +0,0 @@ -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%Tutorial slides on Python. -% -% Author: FOSSEE -% Copyright (c) 2009-2016, FOSSEE, IIT Bombay -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -\documentclass[14pt,compress]{beamer} -%\documentclass[draft]{beamer} -%\documentclass[compress,handout]{beamer} -%\usepackage{pgfpages} -%\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm] - -% Modified from: generic-ornate-15min-45min.de.tex -\mode -{ - \usetheme{Warsaw} - \useoutertheme{infolines} - \setbeamercovered{transparent} -} - -\usepackage[english]{babel} -\usepackage[latin1]{inputenc} -%\usepackage{times} -\usepackage[T1]{fontenc} -\usepackage{pgf} - -% Taken from Fernando's slides. -\usepackage{ae,aecompl} -\usepackage{mathpazo,courier,euler} -\usepackage[scaled=.95]{helvet} -\usepackage{amsmath} - -\definecolor{darkgreen}{rgb}{0,0.5,0} - -\usepackage{listings} -\lstset{language=Python, - basicstyle=\ttfamily\bfseries, - commentstyle=\color{red}\itshape, - stringstyle=\color{darkgreen}, - showstringspaces=false, - keywordstyle=\color{blue}\bfseries} - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Macros -\setbeamercolor{emphbar}{bg=blue!20, fg=black} -\newcommand{\emphbar}[1] -{\begin{beamercolorbox}[rounded=true]{emphbar} - {#1} - \end{beamercolorbox} -} - -\newcommand{\myemph}[1]{\structure{\emph{#1}}} -\newcommand{\PythonCode}[1]{\lstinline{#1}} - -\newcounter{time} -\setcounter{time}{0} -\newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}} - -\newcommand{\typ}[1]{\lstinline{#1}} - -\newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}} } - -%%% This is from Fernando's setup. -% \usepackage{color} -% \definecolor{orange}{cmyk}{0,0.4,0.8,0.2} -% % Use and configure listings package for nicely formatted code -% \usepackage{listings} -% \lstset{ -% language=Python, -% basicstyle=\small\ttfamily, -% commentstyle=\ttfamily\color{blue}, -% stringstyle=\ttfamily\color{orange}, -% showstringspaces=false, -% breaklines=true, -% postbreak = \space\dots -% } - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Title page -\title[Basic SciPy]{Introductory Scientific Computing with -Python} -\subtitle{Basic SciPy} - -\author[Prabhu] {FOSSEE} - -\institute[FOSSEE -- IITB] {Department of Aerospace Engineering\\IIT Bombay} -\date[] { -Mumbai, India -} -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -%\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} -%\logo{\pgfuseimage{iitmlogo}} - - -%% Delete this, if you do not want the table of contents to pop up at -%% the beginning of each subsection: -\AtBeginSubsection[] -{ - \begin{frame} - \frametitle{Outline} - \tableofcontents[currentsection,currentsubsection] - \end{frame} -} - -\AtBeginSection[] -{ - \begin{frame} - \frametitle{Outline} - \tableofcontents[currentsection,currentsubsection] - \end{frame} -} - -% If you wish to uncover everything in a step-wise fashion, uncomment -% the following command: -%\beamerdefaultoverlayspecification{<+->} - -%\includeonlyframes{current,current1,current2,current3,current4,current5,current6} - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% DOCUMENT STARTS -\begin{document} - -\begin{frame} - \maketitle -\end{frame} - - -\section{Solving linear systems} - -\begin{frame}[fragile] -\frametitle{Solution of equations} -Consider, - \begin{align*} - 3x + 2y - z & = 1 \\ - 2x - 2y + 4z & = -2 \\ - -x + \frac{1}{2}y -z & = 0 - \end{align*} -Solution: - \begin{align*} - x & = 1 \\ - y & = -2 \\ - z & = -2 - \end{align*} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Solving using Matrices} -Let us now look at how to solve this using \kwrd{matrices} - \begin{lstlisting} -In []: A = array([[3,2,-1], - [2,-2,4], - [-1, 0.5, -1]]) -In []: b = array([1, -2, 0]) -In []: x = solve(A, b) - \end{lstlisting} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Solution:} -\begin{lstlisting} -In []: x -Out[]: array([ 1., -2., -2.]) -\end{lstlisting} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Let's check!} -\begin{small} -\begin{lstlisting} -In []: Ax = dot(A, x) -In []: Ax -Out[]: array([ 1.00000000e+00, -2.00000000e+00, -1.11022302e-16]) -\end{lstlisting} -\end{small} -\begin{block}{} -The last term in the matrix is actually \alert{0}!\\ -We can use \kwrd{allclose()} to check. -\end{block} -\begin{lstlisting} -In []: allclose(Ax, b) -Out[]: True -\end{lstlisting} -\inctime{10} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Problem} -Solve the set of equations: -\begin{align*} - x + y + 2z -w & = 3\\ - 2x + 5y - z - 9w & = -3\\ - 2x + y -z + 3w & = -11 \\ - x - 3y + 2z + 7w & = -5\\ -\end{align*} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Solution} -Use \kwrd{solve()} -\begin{align*} - x & = -5\\ - y & = 2\\ - z & = 3\\ - w & = 0\\ -\end{align*} -\inctime{5} -\end{frame} - -\section{Finding Roots} - -\begin{frame}[fragile] -\frametitle{SciPy: \typ{roots}} -\begin{itemize} -\item Calculates the roots of polynomials -\item To calculate the roots of $x^2-5x+6$ -\end{itemize} -\begin{lstlisting} - In []: coeffs = [1, -5, 6] - In []: roots(coeffs) - Out[]: array([3., 2.]) -\end{lstlisting} -\vspace*{-.2in} -\begin{center} -\includegraphics[height=1.6in, interpolate=true]{data/roots} -\end{center} -\end{frame} - -\begin{frame}[fragile] -\frametitle{SciPy: \typ{fsolve}} -Find the root of $sin(z)+cos^2(z)$ nearest to $0$ -\vspace{-0.1in} -\begin{center} -\includegraphics[height=2.8in, interpolate=true]{data/fsolve} -\end{center} -\end{frame} - -\begin{frame}[fragile] -\frametitle{\typ{fsolve}} -\begin{small} -\begin{lstlisting} - In []: from scipy.optimize import fsolve -\end{lstlisting} -\end{small} -\begin{itemize} -\item Finds the roots of a system of non-linear equations -\item Input arguments - \alert{Function} and initial estimate -\item Returns the solution -\end{itemize} -\end{frame} - -\begin{frame}[fragile] -\frametitle{\typ{fsolve} \ldots} -\begin{lstlisting} -In []: def g(z): - ....: return sin(z)+cos(z)*cos(z) -In []: fsolve(g, 0) -Out[]: -0.66623943249251527 -\end{lstlisting} -\begin{center} -\includegraphics[height=2in, interpolate=true]{data/fsolve} -\end{center} -\inctime{10} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Exercise Problem} - Find the root of the equation $x^2 - sin(x) + cos^2(x) = tan(x)$ nearest to $0$ -\end{frame} - -\begin{frame}[fragile] - \frametitle{Solution} - \begin{small} - \begin{lstlisting} -def g(x): - return x**2 - sin(x) + cos(x)*cos(x) - tan(x) -fsolve(g, 0) - \end{lstlisting} - \end{small} - \vspace*{-0.2in} - \begin{center} -\includegraphics[height=2.5in, interpolate=true]{data/fsolve_tanx} -\end{center} -\vspace*{-0.5in} - \inctime{5} -\end{frame} - -%% \begin{frame}[fragile] -%% \frametitle{Scipy Methods \dots} -%% \begin{small} -%% \begin{lstlisting} -%% In []: from scipy.optimize import fixed_point - -%% In []: from scipy.optimize import bisect - -%% In []: from scipy.optimize import newton -%% \end{lstlisting} -%% \end{small} -%% \end{frame} - -\section{ODEs} - -\begin{frame} -\frametitle{Solving ODEs using SciPy} -\begin{itemize} -\item Consider the spread of an epidemic in a population - \vspace*{0.1in} -\item $\frac{dy}{dt} = ky(L-y)$ gives the spread of the disease - \vspace*{0.1in} -\item $L$ is the total population. -\item Use $L = 2.5E5, k = 3E-5, y(0) = 250$ -\end{itemize} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Solving ODEs using SciPy} -Define a function as below -\small -\begin{lstlisting} -In []: from scipy.integrate import odeint -In []: def epid(y, t): - ...: k = 3.0e-5 - ...: L = 2.5e5 - ...: return k*y*(L-y) - ...: -\end{lstlisting} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Solving ODEs using SciPy \ldots} -\begin{lstlisting} -In []: t = linspace(0, 12, 61) - -In []: y = odeint(epid, 250, t) - -In []: plot(t, y) -\end{lstlisting} -%Insert Plot -\end{frame} - -\begin{frame}[fragile] -\frametitle{Result} -\begin{center} -\includegraphics[height=3in, interpolate=true]{data/image} -\end{center} -\vspace*{-0.5in} -\inctime{5} -\end{frame} - - -\begin{frame}[fragile] -\frametitle{ODEs - Simple Pendulum} -We shall use the simple ODE of a simple pendulum. -\begin{equation*} -\ddot{\theta} = -\frac{g}{L}sin(\theta) -\end{equation*} -\begin{itemize} -\item This equation can be written as a system of two first order ODEs -\end{itemize} -\begin{align} -\dot{\theta} &= \omega \\ -\dot{\omega} &= -\frac{g}{L}sin(\theta) \\ - \text{At}\ t &= 0 : \nonumber \\ - \theta = \theta_0(10^o)\quad & \&\quad \omega = 0\ (Initial\ values)\nonumber -\end{align} -\end{frame} - -\begin{frame}[fragile] -\frametitle{ODEs - Simple Pendulum \ldots} -\begin{itemize} -\item Use \typ{odeint} to do the integration -\end{itemize} -\begin{lstlisting} -In []: def pend_rhs(state, t): - .... theta = state[0] - .... omega = state[1] - .... g = 9.81 - .... L = 0.2 - .... F=[omega, -(g/L)*sin(theta)] - .... return F - .... -\end{lstlisting} -\end{frame} - -\begin{frame}[fragile] -\frametitle{ODEs - Simple Pendulum \ldots} -\begin{itemize} -\item \typ{t} is the time variable \\ -\item \typ{initial} has the initial values -\end{itemize} -\begin{lstlisting} -In []: t = linspace(0, 20, 101) -In []: initial = [10*2*pi/360, 0] -\end{lstlisting} -\end{frame} - -\begin{frame}[fragile] -\frametitle{ODEs - Simple Pendulum \ldots} -%%\begin{small} -\typ{In []: from scipy.integrate import odeint} -%%\end{small} -\begin{lstlisting} -In []: pend_sol = odeint(pend_rhs, - initial,t) -\end{lstlisting} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Result} -\begin{center} -\includegraphics[height=2in, interpolate=true]{data/ode} -\end{center} - \inctime{10} -\end{frame} - -\section{FFTs} - -\begin{frame}[fragile] -\frametitle{The FFT} -\begin{itemize} - \item We have a simple signal $y(t)$ - \item Find the FFT and plot it -\end{itemize} -\begin{lstlisting} -In []: t = linspace(0, 2*pi, 500) -In []: y = sin(4*pi*t) - -In []: f = fft.fft(y) -In []: freq = fft.fftfreq(500, - ...: t[1] - t[0]) - -In []: plot(freq[:250], abs(f)[:250]) -In []: grid() -\end{lstlisting} -\end{frame} - -\begin{frame}[fragile] -\frametitle{FFTs cont\dots} -\begin{lstlisting} -In []: y1 = fft.ifft(f) # inverse FFT -In []: allclose(y, y1) -Out[]: True -\end{lstlisting} -\end{frame} - -\begin{frame}[fragile] -\frametitle{FFTs cont\dots} -Let us add some noise to the signal -\begin{lstlisting} -In []: yr = y + - ...: random.random(size=500)*0.2 -In []: yn = y + - ...: random.normal(size=500)*0.2 - -In []: plot(t, yr) -In []: figure() -In []: plot(freq[:250], - ...: abs(fft.fft(yr))[:250]) -\end{lstlisting} -\begin{itemize} - \item \typ{random}: produces uniform deviates in $[0, 1)$ - \item \typ{normal}: draws random samples from a Gaussian - distribution - \item Useful to create a random matrix of any shape -\end{itemize} -\end{frame} - -\begin{frame}[fragile] -\frametitle{FFTs cont\dots} -Filter the noisy signal: -\begin{lstlisting} -In []: from scipy import signal -In []: yc = signal.wiener(yn, 5) -In []: clf() -In []: plot(t, yc) -In []: figure() -In []: plot(freq[:250], - ...: abs(fft.fft(yc))[:250]) -\end{lstlisting} -Only scratched the surface here \dots - -\inctime{10} -\end{frame} - - -\begin{frame} - \frametitle{Things we have learned} - \begin{itemize} - \item Solving Linear Equations - \item Defining Functions - \item Finding Roots - \item Solving ODEs - \item FFTs and basic signal processing - \end{itemize} -\end{frame} - - -\begin{frame} - \frametitle{Further reading} - \begin{itemize} - \item \url{ipython.readthedocs.io} - \item \url{matplotlib.org/contents.html} - \item \url{docs.scipy.org/doc/numpy/user/quickstart.html} - \item \url{docs.scipy.org/doc/scipy/reference/tutorial} - \end{itemize} -\end{frame} - -\end{document} diff --git a/scipy/basic/exercises.tex b/scipy/basic/exercises.tex deleted file mode 100644 index 78ba335..0000000 --- a/scipy/basic/exercises.tex +++ /dev/null @@ -1,476 +0,0 @@ -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%Tutorial slides on Python. -% -% Author: Prabhu Ramachandran -% Copyright (c) 2005-2009, Prabhu Ramachandran -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -\documentclass[14pt,compress]{beamer} -%\documentclass[draft]{beamer} -%\documentclass[compress,handout]{beamer} -%\usepackage{pgfpages} -%\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm] - -% Modified from: generic-ornate-15min-45min.de.tex -\mode -{ - \usetheme{Warsaw} - \useoutertheme{infolines} - \setbeamercovered{transparent} -} - -\usepackage[english]{babel} -\usepackage[latin1]{inputenc} -%\usepackage{times} -\usepackage[T1]{fontenc} - -% Taken from Fernando's slides. -\usepackage{ae,aecompl} -\usepackage{mathpazo,courier,euler} -\usepackage[scaled=.95]{helvet} - -\definecolor{darkgreen}{rgb}{0,0.5,0} - -\usepackage{listings} -\lstset{language=Python, - basicstyle=\ttfamily\bfseries, - commentstyle=\color{red}\itshape, - stringstyle=\color{darkgreen}, - showstringspaces=false, - keywordstyle=\color{blue}\bfseries} - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Macros -\setbeamercolor{emphbar}{bg=blue!20, fg=black} -\newcommand{\emphbar}[1] -{\begin{beamercolorbox}[rounded=true]{emphbar} - {#1} - \end{beamercolorbox} -} -\newcounter{time} -\setcounter{time}{0} -\newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}} - -\newcommand{\typ}[1]{\lstinline{#1}} - -\newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}} } - -%%% This is from Fernando's setup. -% \usepackage{color} -% \definecolor{orange}{cmyk}{0,0.4,0.8,0.2} -% % Use and configure listings package for nicely formatted code -% \usepackage{listings} -% \lstset{ -% language=Python, -% basicstyle=\small\ttfamily, -% commentstyle=\ttfamily\color{blue}, -% stringstyle=\ttfamily\color{orange}, -% showstringspaces=false, -% breaklines=true, -% postbreak = \space\dots -% } - - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Title page -\title[Exercises]{Introductory Scientific Computing with Python} -\subtitle{Exercises} - -\author[FOSSEE] {FOSSEE} - -\institute[FOSSEE -- IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} -\date[] {Mumbai, India} -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -%\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} -%\logo{\pgfuseimage{iitmlogo}} - - -%% Delete this, if you do not want the table of contents to pop up at -%% the beginning of each subsection: -\AtBeginSubsection[] -{ - \begin{frame} - \frametitle{Outline} - \tableofcontents[currentsection,currentsubsection] - \end{frame} -} - - -% If you wish to uncover everything in a step-wise fashion, uncomment -% the following command: -%\beamerdefaultoverlayspecification{<+->} - -%\includeonlyframes{current,current1,current2,current3,current4,current5,current6} - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% DOCUMENT STARTS -\begin{document} - -\begin{frame} - \titlepage -\end{frame} - - -\begin{frame}[fragile] - \frametitle{Problem 1} - \begin{columns} - \column{0.5\textwidth} - \hspace*{-0.5in} - \includegraphics[height=2in, interpolate=true]{data/L-Tsq.png} - \column{0.45\textwidth} - \begin{block}{Example code} - \tiny -\begin{lstlisting} -In []: l, t = loadtxt('pendulum.txt', - unpack=True) -In []: plot(l, t, '.') - \end{lstlisting} - \end{block} - \end{columns} - \begin{block}{Problem Statement} - Tweak above code to plot data in file \typ{pos.txt}. - \end{block} -\end{frame} - -\begin{frame} - \frametitle{Problem 1 cont...} - \begin{itemize} - \item Label both the axes. - \item What kind of motion is this? - \item Title the graph accordingly. - \item Annotate the position where vertical velocity is zero. - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Solution} -\begin{lstlisting} -x, y = loadtxt('pos.txt', unpack=True) -plot(x, y) -xlabel('x') -ylabel('y') -title('Projectile motion') -annotate('v = 0', xy=(5, 4.75)) -# Or -annotate(r'$v_y = 0$', xy=(5, 4.75)) -\end{lstlisting} - \begin{itemize} - \item Note the \LaTeX\ syntax - \item Note the raw strings: \typ{r'$v_y = 0$'} - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Problem 2} - \begin{columns} - \column{0.5\textwidth} - \hspace*{-0.5in} - \includegraphics[height=2in, interpolate=true]{data/exercises/triangle} - \column{0.45\textwidth} - \begin{block}{Plot points given x and y coordinates} - \tiny - \begin{lstlisting} -In []: x = [3, 2, -2, 3] -In []: y = [1, -3, 4, 1] -In []: plot(x, y) - \end{lstlisting} - \end{block} - \end{columns} - Line can be plotted using arrays of coordinates. - \pause - \begin{block}{Problem statement} - Write a Program that plots a regular n-gon(Let n = 5). - \end{block} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Solution} -\begin{lstlisting} -n = 5 -t = linspace(0, 2*pi, n+1) -x = cos(t) -y = sin(t) -plot(x, y) -axis('equal') -\end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Better Solution} -\begin{lstlisting} -def plot_ngon(n): - t = linspace(0, 2*pi, n+1) - x = cos(t) - y = sin(t) - plot(x, y) - axis('equal') - -plot_ngon(5) -clf() -plot_ngon(10) -\end{lstlisting} -\end{frame} - - -\begin{frame}[fragile] - \frametitle{Problem 3} - \begin{columns} - \column{0.5\textwidth} - \hspace*{-0.5in} - \includegraphics[height=2in, interpolate=true]{data/exercises/damp} - \column{0.45\textwidth} - \begin{block}{Damped Oscillation} - \tiny - \begin{lstlisting} -In []: t = linspace(0, 4*pi) -In []: plot(t, exp(-t/10)*sin(t)) - \end{lstlisting} - \end{block} - \end{columns} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Problem 3 cont...} -Create a sequence of images (say 10) in which the damped -oscillator($e^{-t/10}sin(t)$) slowly evolves over time $t$. -\begin{columns} -\column{0.35\textwidth} -\includegraphics[width=1.5in,height=1.5in, interpolate=true]{data/exercises/plot2} -\column{0.35\textwidth} -\includegraphics[width=1.5in,height=1.5in, interpolate=true]{data/exercises/plot4} -\column{0.35\textwidth} -\includegraphics[width=1.5in,height=1.5in, interpolate=true]{data/exercises/plot6} -\end{columns} -\begin{block}{Hint} -\small - \begin{lstlisting} -savefig('plot'+str(i)+'.png') #i is some int - \end{lstlisting} -\end{block} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Naive Solution} -\begin{lstlisting} -for i in range(1, 11): - t = linspace(0, 0.5*pi*i, 50*i) - clf() - plot(t, exp(-t/10)*sin(t)) - savefig('plot' + str(i) + '.png') -\end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Better Solution} -\begin{lstlisting} -for i in range(1, 11): - t = linspace(0, 0.5*pi*i, 50*i) - clf() - plot(t, exp(-t/10)*sin(t)) - xlim(0, 5*pi) - ylim(-1, 1) - savefig('plot' + str(i) + '.png') -\end{lstlisting} -\end{frame} - - -\begin{frame}[fragile] - \frametitle{Problem 4} - \begin{lstlisting} -In []: x = imread('smoothing.gif') -In []: x.shape -Out[]: (256, 256) -In []: imshow(x,cmap=cm.gray) -In []: colorbar() -\end{lstlisting} -\emphbar{Replace each pixel with mean of neighboring pixels} - \begin{center} - \includegraphics[height=1in, interpolate=true]{data/exercises/neighbour} - \end{center} -\end{frame} - -\begin{frame} - \begin{center} - \includegraphics[height=3in, interpolate=true]{data/exercises/smoothing} - \end{center} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Problem 4: Approach} - For \typ{y} being resultant image: - \begin{lstlisting} -y[1, 1] = x[0, 1]/4 + x[1, 0]/4 - + x[2, 1]/4 + x[1, 2]/4 - \end{lstlisting} - \begin{columns} - \column{0.45\textwidth} - \hspace*{-0.5in} - \includegraphics[height=1.5in, interpolate=true]{data/exercises/smoothing} - \column{0.45\textwidth} - \hspace*{-0.5in} - \includegraphics[height=1.5in, interpolate=true]{data/exercises/after-filter} - \end{columns} - \begin{block}{Hint:} - Use array Slicing. - \end{block} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Solution} - \begin{lstlisting} -In []: y = zeros_like(x) -In []: y[1:-1,1:-1] = x[:-2,1:-1]/4 + - x[2:,1:-1]/4 + - x[1:-1,2:]/4 + - x[1:-1,:-2]/4 -In []: imshow(y,cmap=cm.gray) - \end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Problem 4 cont\ldots} - \begin{itemize} - \item Apply the smoothing operation repeatedly to the original - image - - \item Subtract the smoothed image from the original to obtain - the edges - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Problem 5} - - What if you did the following in problem 4? -\begin{lstlisting} -In []: y1 = zeros_like(x) -In []: y1[1:-1,1:-1] = (x[:-2,1:-1] + - x[2:,1:-1] + - x[1:-1,2:] + - x[1:-1,:-2])/4 - \end{lstlisting} - - Are the answers different? - -\end{frame} - -\begin{frame}[fragile] - \frametitle{Problem 5 cont\ldots} - Why? The answer lies in the following: -\begin{lstlisting} -In []: x.dtype -Out[]: dtype('uint8') - -In []: print(x.itemsize) -1 - -In []: z = x/4.0 - -In []: print(z.dtype) -float64 - -\end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Problem 5 cont\ldots} -What if you did this? - -\begin{lstlisting} -x = imread('smoothing.gif') -y2 = zeros_like(x) -y2[1:-1,1:-1] = x[:-2,1:-1]/4. + \ - x[2:,1:-1]/4. + \ - x[1:-1,2:]/4. + \ - x[1:-1,:-2]/4. -\end{lstlisting} -\begin{itemize} - \item Will the answer be any different from \typ{y}? - \item What will the dtype of \typ{y2} be? - \item Discuss what is going on! -\end{itemize} - -\end{frame} - -\begin{frame}[fragile] - \frametitle{Problem 5 cont\ldots} - \begin{itemize} - \item Did you do the right thing to find the edges earlier in problem 4? - \item If not, fix it! - \end{itemize} - Note that: - \small -\begin{lstlisting} -In []: print(x.dtype) -uint8 -In []: x1 = x.astype('float64') -In []: print(x1.dtype) -float64 -In []: print(x.dtype.char) -d -In []: x.dtype. # Explore! -\end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Problem 6} - - Edge detection looks much nicer with \typ{lena.png}, try it! The - caveat is that it is a 4 component RGBA image with elements in the - range $[0.0, 1.0]$. - -\begin{lstlisting} -In []: x = imread('lena.png') - -In []: print(x.shape) -(512, 512, 4) - -In []: print(x.min(), x.max()) -(0.0, 1.0) -\end{lstlisting} - - Repeat the edge detection with this image. - -\end{frame} - - -\end{document} - -%% \begin{frame} -%% \frametitle{Problem 4} -%% Legendre polynomials $P_n(x)$ are defined by the following recurrence relation - -%% \center{$(n+1)P_{n+1}(x) - (2n+1)xP_n(x) + nP_{n-1}(x) = 0$}\\ - -%% with $P_0(x) = 1$, $P_1(x) = x$ and $P_2(x) = (3x^2 - 1)/2$. Compute the next three -%% Legendre polynomials and plot all 6 over the interval [-1,1]. -%% \end{frame} - -%% \begin{frame}[fragile] -%% \frametitle{Problem Set 5} -%% \begin{columns} -%% \column{0.6\textwidth} -%% \small{ -%% \begin{itemize} -%% \item[3] Consider the iteration $x_{n+1} = f(x_n)$ where $f(x) = kx(1-x)$. Plot the successive iterates of this process as explained below. -%% \end{itemize}} -%% \column{0.35\textwidth} -%% \hspace*{-0.5in} -%% \includegraphics[height=1.6in, interpolate=true]{data/cobweb} -%% \end{columns} -%% \end{frame} - -%% \begin{frame} -%% \frametitle{Problem Set 5.3} -%% Plot the cobweb plot as follows: -%% \begin{enumerate} -%% \item Start at $(x_0, 0)$ ($\implies$ i=0) -%% \item Draw a line to $(x_i, f(x_i))$ -%% \item Set $x_{i+1} = f(x_i)$ -%% \item Draw a line to $(x_{i+1}, x_{i+1})$ -%% \item $(i\implies i+1)$ -%% \item Repeat from 2 for as long as you want -%% \end{enumerate} -%% \inctime{20} -%% \end{frame} diff --git a/scipy/basic/intro.tex b/scipy/basic/intro.tex deleted file mode 100644 index 03c0ad0..0000000 --- a/scipy/basic/intro.tex +++ /dev/null @@ -1,325 +0,0 @@ -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%Tutorial slides on Python. -% -% Author: FOSSEE -% Copyright (c) 2009-2016, FOSSEE, IIT Bombay -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -\documentclass[14pt,compress]{beamer} - -% Modified from: generic-ornate-15min-45min.de.tex -\mode -{ - \usetheme{Warsaw} - \useoutertheme{infolines} - \setbeamercovered{transparent} -} - -% Remove navigation symbols. -\setbeamertemplate{navigation symbols}{} - -\usepackage[english]{babel} -\usepackage[latin1]{inputenc} -%\usepackage{times} -\usepackage[T1]{fontenc} - -% Taken from Fernando's slides. -\usepackage{ae,aecompl} -\usepackage{mathpazo,courier,euler} -\usepackage[scaled=.95]{helvet} - -\definecolor{darkgreen}{rgb}{0,0.5,0} - -\usepackage{listings} -\lstset{language=Python, - basicstyle=\ttfamily\bfseries, - commentstyle=\color{red}\itshape, - stringstyle=\color{darkgreen}, - showstringspaces=false, - keywordstyle=\color{blue}\bfseries} - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Macros -\setbeamercolor{emphbar}{bg=blue!20, fg=black} -\newcommand{\emphbar}[1] -{\begin{beamercolorbox}[rounded=true]{emphbar} - {#1} - \end{beamercolorbox} -} -\newcounter{time} -\setcounter{time}{0} -\newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}} - -\newcommand{\typ}[1]{\lstinline{#1}} - -\newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}} } - -\newcommand\BackgroundPicture[1]{% - \setbeamertemplate{background}{% - \parbox[c][\paperheight]{\paperwidth}{% - \vfill \hfill - \pgfimage[width=1.0\paperwidth,height=1.0\paperheight]{#1} - \hfill \vfill -}}} - -% For non-wide pictures, set the width so that the height scales -% appropriately. -\newcommand\BackgroundPictureWidth[1]{% - \setbeamertemplate{background}{% - \parbox[c][\paperheight]{\paperwidth}{% - \vfill \hfill - \pgfimage[width=1.0\paperwidth]{#1} - \hfill \vfill -}}} - -% For shorter pictures, set the height so that the width scales -% appropriately. -\newcommand\BackgroundPictureHeight[1]{% - \setbeamertemplate{background}{% - \parbox[c][\paperheight]{\paperwidth}{% - \vfill \hfill - \pgfimage[height=1.0\paperheight]{#1} - \hfill \vfill -}}} - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Title page -\title[Introduction]{Introductory Scientific Computing with -Python} -\subtitle{Introduction to Python} - -\author[FOSSEE] {FOSSEE} - -\institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} -\date[] {Mumbai, India -} -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -%\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} -%\logo{\pgfuseimage{iitmlogo}} - - -%% Delete this, if you do not want the table of contents to pop up at -%% the beginning of each subsection: -\AtBeginSubsection[] -{ - \begin{frame} - \frametitle{Outline} - \tableofcontents[currentsection,currentsubsection] - \end{frame} -} - -\AtBeginSection[] -{ - \begin{frame} - \frametitle{Outline} - \tableofcontents[currentsection,currentsubsection] - \end{frame} -} - -% If you wish to uncover everything in a step-wise fashion, uncomment -% the following command: -%\beamerdefaultoverlayspecification{<+->} - -%%\includeonlyframes{current,current1,current2,current3,current4,current5,current6} - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% DOCUMENT STARTS -\begin{document} - -\begin{frame} - \maketitle -\end{frame} - -\begin{frame} - \frametitle{Acknowledgement} - \Large - \begin{center} - \alert{FOSSEE group (\url{fossee.in})} \\ - based at\\ - \alert{IIT Bombay}\\ - and funded by\\ - The National Mission on Education through ICT, \\ - \alert{Ministry of HRD, India} - \end{center} -\end{frame} - - -\begin{frame}[plain] - \begin{center} - \Huge - Why Python? - \vspace*{1in} - - For Scientific Computing? - \end{center} - -\end{frame} - -\begin{frame} - \frametitle{Need: Toolkit for diversity} - \Large - \begin{itemize} - \item Numeric and Symbolic - \item Exploration and Visualization - \item High performance - \item Parallel computing - \item User interfaces, Web - \item Other tasks - \end{itemize} -\end{frame} - -\begin{frame}[plain] - \frametitle{Why Python?} - \Large - \begin{itemize} - \item Easy to read and learn - \item Powerful interactive interpreter - \item Scalable, general purpose - \item High-level, modular - \item Procedural, OO, functional - \end{itemize} -\end{frame} - -\begin{frame}[plain] - \frametitle{Why Python?} - \Large - \begin{itemize} - \item Extensive libraries - \item Rapid application development - \item Interface to C++, C and FORTRAN - \item Cross-platform - \item Open Source - \end{itemize} -\end{frame} - - -\begin{frame}[plain] - \frametitle{Requirements: numeric computation} - - \begin{center} - \pgfimage[height=3.5in]{data/intro/scipy_screenie} - - \end{center} - -\end{frame} - -\begin{frame}[plain] - \frametitle{SciPy} - \begin{itemize} - \item Linear algebra - \item Numerical integration - \item Fourier transforms - \item Signal processing - \item Special functions - \item Statistics - \item Optimization - \item Image processing - \item ODE solvers - \vspace*{0.2in} - \item Uses LAPACK, QUADPACK, ODEPACK, FFTPACK etc. from netlib - \end{itemize} -\end{frame} - -\begin{frame}[plain] - \frametitle{Requirement: Exploration/Visualization} - \begin{center} - \pgfimage[height=3.0in]{data/intro/mayavi-ipython} - \end{center} - \end{frame} - -\begin{frame}[plain] - \frametitle{3D visualization with Mayavi2} - \vspace*{-0.3in} - \begin{center} - \hspace*{-0.3in}\pgfimage[width=4.75in]{data/intro/m2_app} - \end{center} -\end{frame} - -\begin{frame}[plain] - \frametitle{Requirement: HPC, parallel computing} - \begin{center} - \includegraphics[height=1.75in]{data/intro/dam_break_poster_0} - \includegraphics[height=1.75in]{data/intro/dam_break_poster} - \end{center} -\end{frame} - -\begin{frame}[plain] - \frametitle{Requirement: UI} - - \begin{center} - \pgfimage[height=3.25in]{data/intro/wxpy_demo} - - \end{center} - -\end{frame} - -\begin{frame}[plain, fragile] - \frametitle{Super-simple UIs} -%\footnotesize -\begin{lstlisting} -from traits.api import * -class Person(HasTraits): - name = Str('name') - age = Range(0.0, 200.0) - sex = Enum('male', 'female') - -p = Person(name='Ram') -p.configure_traits() -\end{lstlisting} -\hrule -\pgfimage[width=0.5\textwidth, interpolate=true]{data/intro/traits_ui_wx} -\pgfimage[width=0.5\textwidth, interpolate=true]{data/intro/traits_ui_qt} -\begin{center}WxPython/Qt -\end{center} -\end{frame} - - - -\BackgroundPicture{data/intro/django_page} -\begin{frame}[plain] -\end{frame} -\BackgroundPicture{data/intro/blank} - -\begin{frame}[plain,fragile] - \frametitle{Easy to read and still compact?} - \footnotesize - \begin{lstlisting} -def qsort(L): - """Quick sort for given sequence, `L`.""" - if not L: return L # exit recursion if input is empty - pivot, rest = L[0], L[1:] - less_than = [ lt for lt in rest if lt < pivot ] - greater_eq = [ ge for ge in rest if ge >= pivot ] - return qsort(less_than) + [pivot] + qsort(greater_eq) - - \end{lstlisting} -\end{frame} - -\begin{frame}[plain] - \Huge - \begin{center} - - \structure{Python users?} - \end{center} -\end{frame} - -\BackgroundPictureWidth{data/intro/python-orgs} -\begin{frame}[plain] -\end{frame} -\BackgroundPicture{data/intro/blank} - -\begin{frame}[plain] - \emphbar{\Large \hfill Diverse needs, one language \hfill } - \emphbar{\Large \hfill Python! \hfill} - \begin{center} - %\structure{Python!} - \pgfimage[height=2in]{data/intro/python_only_logo} - - \url{www.python.org} - \end{center} - -\end{frame} - - -\end{document} diff --git a/scipy/basic/ipython_plotting.tex b/scipy/basic/ipython_plotting.tex deleted file mode 100644 index d0c5bf3..0000000 --- a/scipy/basic/ipython_plotting.tex +++ /dev/null @@ -1,662 +0,0 @@ -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%Tutorial slides on Python. -% -% Author: FOSSEE -% Copyright (c) 2009-2016, FOSSEE, IIT Bombay -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -\documentclass[14pt,compress]{beamer} - -% Modified from: generic-ornate-15min-45min.de.tex -\mode -{ - \usetheme{Warsaw} - \useoutertheme{infolines} - \setbeamercovered{transparent} -} - -\usepackage[english]{babel} -\usepackage[latin1]{inputenc} -%\usepackage{times} -\usepackage[T1]{fontenc} - -% Taken from Fernando's slides. -\usepackage{ae,aecompl} -\usepackage{mathpazo,courier,euler} -\usepackage[scaled=.95]{helvet} - -\definecolor{darkgreen}{rgb}{0,0.5,0} - -\usepackage{listings} -\lstset{language=Python, - basicstyle=\ttfamily\bfseries, - commentstyle=\color{red}\itshape, - stringstyle=\color{darkgreen}, - showstringspaces=false, - keywordstyle=\color{blue}\bfseries} - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Macros -\setbeamercolor{emphbar}{bg=blue!20, fg=black} -\newcommand{\emphbar}[1] -{\begin{beamercolorbox}[rounded=true]{emphbar} - {#1} - \end{beamercolorbox} -} -\newcounter{time} -\setcounter{time}{0} -\newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}} - -\newcommand{\typ}[1]{\lstinline{#1}} - -\newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}} } - -%%% This is from Fernando's setup. -% \usepackage{color} -% \definecolor{orange}{cmyk}{0,0.4,0.8,0.2} -% % Use and configure listings package for nicely formatted code -% \usepackage{listings} -% \lstset{ -% language=Python, -% basicstyle=\small\ttfamily, -% commentstyle=\ttfamily\color{blue}, -% stringstyle=\ttfamily\color{orange}, -% showstringspaces=false, -% breaklines=true, -% postbreak = \space\dots -% } - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Title page -\title[Interactive Plotting]{Introductory Scientific Computing with -Python} -\subtitle{Introduction, IPython and Plotting} - -\author[FOSSEE] {FOSSEE} - -\institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} -\date[] {Mumbai, India -} -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -%\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} -%\logo{\pgfuseimage{iitmlogo}} - - -%% Delete this, if you do not want the table of contents to pop up at -%% the beginning of each subsection: -\AtBeginSubsection[] -{ - \begin{frame} - \frametitle{Outline} - \tableofcontents[currentsection,currentsubsection] - \end{frame} -} - -\AtBeginSection[] -{ - \begin{frame} - \frametitle{Outline} - \tableofcontents[currentsection,currentsubsection] - \end{frame} -} - -% If you wish to uncover everything in a step-wise fashion, uncomment -% the following command: -%\beamerdefaultoverlayspecification{<+->} - -%%\includeonlyframes{current,current1,current2,current3,current4,current5,current6} - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% DOCUMENT STARTS -\begin{document} - -\begin{frame} - \maketitle -\end{frame} - -%% \begin{frame} -%% \frametitle{Outline} -%% \tableofcontents -%% % You might wish to add the option [pausesections] -%% \end{frame} - -\begin{frame} - \frametitle{Acknowledgement} - \Large - \begin{center} - \alert{FOSSEE group (\url{fossee.in})} \\ - based at\\ - \alert{IIT Bombay}\\ - and funded by\\ - The National Mission on Education through ICT, \\ - \alert{Ministry of HRD, India} - \end{center} -\end{frame} - -\section{Checklist} -\begin{frame} -\frametitle{Checklist} - \begin{enumerate} - \item Editor - we recommend \alert{Canopy} - \item IPython - \item Data files: - \begin{itemize} - \item \typ{pendulum.txt} - \item \typ{data.csv} - \end{itemize} - \item Images - \begin{itemize} - \item \typ{lena.png} - \end{itemize} - \end{enumerate} -\end{frame} - -\begin{frame} - \frametitle{About the Tutorial} - \begin{block}{Intended Audience} - \begin{itemize} - \item Engg., Mathematics and Science researchers with a - reasonable programming background. - \end{itemize} - \end{block} - - \begin{block}{Goal: Successful participants will be able to} - \begin{itemize} - \item Start using Python as plotting, computational tool. - \item Use the basic libraries and tools for scientific computing - with Python. - \end{itemize} - \end{block} -\end{frame} - -%% \begin{frame}[fragile] -%% \frametitle{The Python interpreter \ldots} -%% \begin{block}{} -%% \begin{lstlisting} -%% $ python -%% \end{lstlisting} %$ -%% \end{block} -%% \begin{lstlisting} -%% >>> print "Hello, World!" -%% Hello, World! -%% \end{lstlisting} -%% Exiting -%% \begin{lstlisting} -%% >>> ^D(Ctrl-D) -%% $ -%% \end{lstlisting} %$ -%% \end{frame} - -\section{Starting up IPython} -\begin{frame}[fragile] -\frametitle{Starting up \ldots} -\begin{block}{Start a terminal} - \begin{itemize} - \item Canopy command prompt (Tools menu) -\end{itemize} -\end{block} - -\begin{block}{On Terminal} -\begin{lstlisting} - $ ipython --pylab -\end{lstlisting} %$ -\end{block} -\end{frame} - -% \section{Starting up IPython} -% \begin{frame}[fragile] -% \frametitle{Starting up \ldots} -% \begin{block}{Terminal Jupyter} -% \begin{lstlisting} -% $ jupyter console - -% In [1]: %pylab -% \end{lstlisting} %$ -% \end{block} -% \end{frame} - - -\begin{frame}[fragile] -\frametitle{Running IPython} -\begin{lstlisting} - In []: print("Hello, World!") - Hello, World! -\end{lstlisting} - -Exiting on the \textbf{terminal} -\begin{lstlisting} - In []: ^D(Ctrl-D) - Do you really want to exit([y]/n)? y -\end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{IPython? } - \begin{itemize} - \item An enhanced Python interpreter - \end{itemize} -\end{frame} - -% \begin{frame}[fragile] -% \frametitle{Pressed Ctrl-D on Canopy? } -% \begin{itemize} -% \item Pressed \verb+Ctrl-D+ inside Canopy? -% \item You suddenly lost the Python prompt? -% \item Go to \verb+View->Python+ -% \end{itemize} -% \end{frame} - - -\section{Breaking out of loops} -\begin{frame}[fragile] -\frametitle{Breaking out of Loops} -Breaking out of loops -\begin{lstlisting} - In []: while True: - ...: print("Hello, World!") - ...: - Hello, World! - Hello, World!^C(Ctrl-C) - ------------------------------------ - KeyboardInterrupt -\end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Exercise} - - \begin{itemize} - \item Exit the IPython interpreter - \item Close the terminal - \item Restart the terminal (Canopy tools menu) - \item Restart IPython using: - \end{itemize} -\begin{lstlisting} - $ ipython --pylab -\end{lstlisting} %$ - \inctime{10} -\end{frame} - -\section{Plotting} - -\begin{frame} - \frametitle{Important instructions} - \begin{itemize} - \item For the first session, please do not experiment - \item Follow along and type everything! - \item Case matters - \item Every character you type matters! - \end{itemize} -\end{frame} - -\subsection{Drawing plots} -\begin{frame}[fragile] -\frametitle{First Plot} -\begin{columns} - \column{0.25\textwidth} - \hspace*{-0.25in} - \includegraphics[height=2in, interpolate=true]{data/firstplot} - \column{0.8\textwidth} - \begin{block}{} - \begin{small} -\begin{lstlisting} -In []: x = linspace(0, 2*pi, 50) -In []: plot(x, sin(x)) -\end{lstlisting} - \end{small} - \end{block} -\end{columns} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Walkthrough} -\begin{block}{\typ{x = linspace(start, stop, num)} } -returns \typ{num} evenly spaced points, in the interval [\typ{start}, \typ{stop}]. -\end{block} -\vspace*{.35in} -\begin{block}{} - \small -\begin{lstlisting} -In []: x[0] -Out[]: 0.0 - -In []: x[49] -Out[]: 6.2831853071795862 -\end{lstlisting} -\end{block} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Walkthrough \ldots} -\begin{block}{\typ{plot(x, y)}} -plots \typ{x} and \typ{y} using default line style and color -\end{block} -\inctime{5} -\end{frame} - -\begin{frame} - \frametitle{Important instructions} - \begin{itemize} - \item Please do not close the plot windows or IPython - \item For the first session, please do not experiment - \item Follow along and type everything! - \item Case matters - \item Every character you type matters! - \end{itemize} -\end{frame} - -\subsection{Decoration} -\begin{frame}[fragile] -\frametitle{Adding Labels} -\begin{columns} - \column{0.25\textwidth} - \hspace*{-0.45in} - \includegraphics[height=2in, interpolate=true]{data/label} - \hspace*{0.5in} - \column{0.55\textwidth} - \begin{block}{} - \small - \begin{lstlisting} -In []: xlabel('x') - -In []: ylabel('sin(x)') - \end{lstlisting} - \small -% \end{lstlisting} -%\typ{xlabel(s)} sets the label of the \typ{x}-axis to \typ{s} - -% \begin{lstlisting} - \end{block} -%\typ{ylabel(s)} sets the label of the \typ{y}-axis to \typ{s} -\end{columns} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Another example} - \begin{lstlisting} -In []: clf() - \end{lstlisting} -\emphbar{Clears the plot area.} - \begin{lstlisting} -In []: y = linspace(0, 2*pi, 50) -In []: plot(y, sin(2*y)) -In []: xlabel('y') -In []: ylabel('sin(2y)') - \end{lstlisting} -\end{frame} - -\begin{frame}[fragile] -\frametitle{IPython tips} - -\begin{itemize} -\item Use \typ{TAB} to complete command -\item Try: -\begin{lstlisting} -In []: pl -\end{lstlisting} -\end{itemize} -\vspace*{0.5in} - -{\Large \structure{History}} -\begin{itemize} -\item Up arrow and down arrow - -\item Left / right to move and edit - -\item Type some text and press up arrow: -\begin{lstlisting} -In []: pl -\end{lstlisting} - -\end{itemize} - -\end{frame} - -\begin{frame}[fragile] - \frametitle{Advanced IPython tips \ldots} - \begin{itemize} -\item Search: \typ{Ctrl-r} and start typing - -\item \typ{Ctrl-a}: go to start of line - -\item \typ{Ctrl-e}: go to end of line - -\item \typ{Ctrl-k}: kill to end of line - \end{itemize} -\end{frame} - - -\subsection{More decoration} -\begin{frame}[fragile] -\frametitle{Title and Legends} -\vspace*{-0.15in} -% \begin{block}{} -% \small -\begin{lstlisting} -In []: title('Sinusoids') -In []: legend(['sin(2y)']) -\end{lstlisting} -% \small -% \end{block} - \vspace*{-0.1in} - \begin{center} - \includegraphics[height=2in, interpolate=true]{data/legend} - \end{center} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Legend Placement} -\begin{block}{} - \small -\begin{lstlisting} -In []: legend(['sin(2y)'], loc='center') -\end{lstlisting} -\end{block} - -\begin{columns} - \column{0.6\textwidth} - \includegraphics[height=2in, interpolate=true]{data/position} -\column{0.45\textwidth} -\vspace{-0.2in} -\begin{lstlisting} -'best' -'right' -'left' -'center' -\end{lstlisting} -\end{columns} -\inctime{15} -\end{frame} - -\begin{frame} - \frametitle{Important instructions} - \begin{itemize} - \item Please do not close the plot windows or IPython - \item For the first session, please do not experiment - \item Follow along and type everything! - \item Case matters - \item Every character you type matters! - \end{itemize} -\end{frame} - -%% \begin{frame}[fragile] -%% \frametitle{For arbitrary location} -%% \vspace*{-0.1in} -%% \begin{lstlisting} -%% In []: legend(['sin(2y)'], loc=(.8,.1)) -%% \end{lstlisting} -%% \emphbar{Specify south-east corner position} -%% %\vspace*{-0.2in} -%% \begin{center} -%% \includegraphics[height=2in, interpolate=true]{data/loc} -%% \end{center} -%% %\inctime{10} -%% \end{frame} - -\begin{frame}[fragile] -\frametitle{Showing it better} -\vspace{-0.15in} -\begin{lstlisting} -In []: plot(y, cos(y), 'r') -# See a red plot! - -In []: clf() -In []: plot(y, sin(y), 'g', linewidth=2) -\end{lstlisting} -\vspace*{-0.2in} -\begin{center} - \includegraphics[height=2.2in, interpolate=true]{data/green} -\end{center} -%\inctime{10} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Annotating} -\vspace*{-0.1in} -\begin{lstlisting} -In[]: annotate('local max', xy=(1.5, 1)) -\end{lstlisting} -\vspace*{-0.2in} -\begin{center} - \includegraphics[height=3in, interpolate=true]{data/annotate} -\end{center} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Saving \& Closing} -\begin{lstlisting} -In []: savefig('sin.png') - -In []: close() -\end{lstlisting} -Some supported formats: -\begin{itemize} -\item png -\item pdf -\item svg -\end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Recap} - \begin{itemize} - \item \typ{linspace(start, end, num)} - \item \typ{plot(x, y)} - \item \typ{clf()} - \item \typ{xlabel, ylabel} - \item \typ{title, legend, annotate} - \item \typ{savefig} - \item \typ{close} - \end{itemize} - \inctime{10} -\end{frame} - -\section{Multiple plots} -\begin{frame}[fragile] - \frametitle{Overlaid Plots} -\begin{lstlisting} -In []: y = linspace(0, 2*pi, 50) -\end{lstlisting} - -\begin{lstlisting} -In []: clf() -In []: plot(y, sin(y)) -In []: plot(y, cos(y)) -In []: xlabel('y') -In []: ylabel('f(y)') -In []: legend(['sin(y)', 'cos(y)']) -# Note how we made two legends -\end{lstlisting} - - -\emphbar{By default plots would be overlaid!} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Plotting separate figures} -\begin{lstlisting} -In []: clf() -In []: figure(1) -In []: plot(y, sin(y)) -In []: figure(2) -In []: plot(y, cos(y)) -In []: savefig('cosine.png') -In []: figure(1) -In []: title('sin(y)') -In []: savefig('sine.png') -In []: close() -In []: close() -\end{lstlisting} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Get Axes lengths} -\emphbar{Getting axes lengths} - \begin{lstlisting} -In []: xmin, xmax = xlim() -In []: ymin, ymax = ylim() -In []: print(xmin, xmax) -\end{lstlisting} -\pause -\emphbar{Set the axes limits} - \begin{lstlisting} -In []: xlim(xmin, 2*pi ) -In []: ylim(ymin-0.2, ymax+0.2) - \end{lstlisting} -\end{frame} - -\begin{frame}[plain,fragile] - \frametitle{Axes lengths} - \vspace*{-0.2in} - \begin{center} - \includegraphics[height=3.25in, interpolate=true]{data/limits} - \end{center} -\end{frame} - -\begin{frame}[fragile] -\frametitle{IPython tip: documentation} - -\begin{itemize} - - \item Try: -\begin{lstlisting} -In []: plot? -\end{lstlisting} - \vspace*{0.2in} - to get more information on \typ{plot} - - \item Use arrow keys to scroll docs - \item Note: exit help pager with ``q'' -\end{itemize} -\end{frame} - -\begin{frame}[fragile] -\frametitle{IPython tip: source} -\begin{itemize} - \item Try: -\begin{lstlisting} -In []: plot?? -\end{lstlisting} - to see the source code for \typ{plot} - -\end{itemize} -\inctime{10} -\end{frame} - - -\begin{frame}[plain,fragile] -\frametitle{Review Problem} -\begin{enumerate} -\item Plot $x, -x, \sin(x), x \sin(x)$ in range $-5\pi$ to $5\pi$ -\item Add a legend -\item Annotate the origin -\item Set axes limits to the range of x -\end{enumerate} -\vspace*{-0.15in} -\begin{center} - \includegraphics[height=2.6in, interpolate=true]{data/four_plot} -\end{center} -\end{frame} - -\end{document} diff --git a/scipy/basic/lists_arrays.tex b/scipy/basic/lists_arrays.tex deleted file mode 100644 index 8b47bb7..0000000 --- a/scipy/basic/lists_arrays.tex +++ /dev/null @@ -1,554 +0,0 @@ -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%Tutorial slides on Python. -% -% Author: FOSSEE -% Copyright (c) 2009, FOSSEE, IIT Bombay -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -\documentclass[14pt,compress]{beamer} -%\documentclass[draft]{beamer} -%\documentclass[compress,handout]{beamer} -%\usepackage{pgfpages} -%\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm] - -% Modified from: generic-ornate-15min-45min.de.tex -\mode -{ - \usetheme{Warsaw} - \useoutertheme{infolines} - \setbeamercovered{transparent} -} - -\usepackage[english]{babel} -\usepackage[latin1]{inputenc} -%\usepackage{times} -\usepackage[T1]{fontenc} - -% Taken from Fernando's slides. -\usepackage{ae,aecompl} -\usepackage{mathpazo,courier,euler} -\usepackage[scaled=.95]{helvet} - -\definecolor{darkgreen}{rgb}{0,0.5,0} - -\usepackage{listings} -\lstset{language=Python, - basicstyle=\ttfamily\bfseries, - commentstyle=\color{red}\itshape, - stringstyle=\color{darkgreen}, - showstringspaces=false, - keywordstyle=\color{blue}\bfseries} - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Macros -\setbeamercolor{emphbar}{bg=blue!20, fg=black} -\newcommand{\emphbar}[1] -{\begin{beamercolorbox}[rounded=true]{emphbar} - {#1} - \end{beamercolorbox} -} -\newcounter{time} -\setcounter{time}{0} -\newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}} - -\newcommand{\typ}[1]{\lstinline{#1}} - -\newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}} } - -\newcommand{\num}{\texttt{numpy}} - -%%% This is from Fernando's setup. -% \usepackage{color} -% \definecolor{orange}{cmyk}{0,0.4,0.8,0.2} -% % Use and configure listings package for nicely formatted code -% \usepackage{listings} -% \lstset{ -% language=Python, -% basicstyle=\small\ttfamily, -% commentstyle=\ttfamily\color{blue}, -% stringstyle=\ttfamily\color{orange}, -% showstringspaces=false, -% breaklines=true, -% postbreak = \space\dots -% } - - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Title page -\title[Lists and Arrays]{Introductory Scientific Computing with -Python} -\subtitle{More plotting, lists and numpy arrays} - -\author[FOSSEE] {FOSSEE} - -\institute[FOSSEE -- IITB] {Department of Aerospace Engineering\\IIT Bombay} -\date[] {Mumbai, India} - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -%\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} -%\logo{\pgfuseimage{iitmlogo}} - - -%% Delete this, if you do not want the table of contents to pop up at -%% the beginning of each subsection: -\AtBeginSubsection[] -{ - \begin{frame} - \frametitle{Outline} - \tableofcontents[currentsection,currentsubsection] - \end{frame} -} - -\AtBeginSection[] -{ - \begin{frame} - \frametitle{Outline} - \tableofcontents[currentsection,currentsubsection] - \end{frame} -} - -% If you wish to uncover everything in a step-wise fashion, uncomment -% the following command: -%\beamerdefaultoverlayspecification{<+->} - -%\includeonlyframes{current,current1,current2,current3,current4,current5,current6} - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% DOCUMENT STARTS -\begin{document} - -\begin{frame} - \titlepage -\end{frame} - -\begin{frame} - \frametitle{Outline} - \tableofcontents - % You might wish to add the option [pausesections] -\end{frame} - -\section{Plotting Points} -\begin{frame}[fragile] -\frametitle{Why would I plot f(x)?} -Do we plot analytical functions or experimental data? -\begin{small} -\begin{lstlisting} -In []: time = [0., 1., 2, 3] - -In []: distance = [7., 11, 15, 19] - -In []: plot(time,distance) -Out[]: [] - -In []: xlabel('time') -Out[]: - -In []: ylabel('distance') -Out[]: -\end{lstlisting} -\end{small} -\end{frame} - -\begin{frame}[fragile] -\begin{figure} -\includegraphics[width=3.5in]{data/straightline.png} -\end{figure} -\alert{Is this what you have?} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Plotting points} -\begin{itemize} -\item What if we want to plot the points? -\end{itemize} -\begin{lstlisting} - In []: clf() - - In []: plot(time, distance, 'o') - Out[]: [] - - In []: clf() - In []: plot(time, distance, '.') - Out[]: [] -\end{lstlisting} -\end{frame} - -\begin{frame}[fragile] -\begin{figure} -\includegraphics[interpolate=true,width=2.35in]{data/stline_dots.png} -\includegraphics[interpolate=true,width=2.35in]{data/stline_points.png} -\end{figure} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Additional Line Styles} -\begin{itemize} - \item \typ{'o'} - Filled circles - \item \typ{'.'} - Small Dots - \item \typ{'-'} - Lines - \item \typ{'--'} - Dashed lines -\end{itemize} -\end{frame} - -\section{Lists} -\begin{frame}[fragile] - \frametitle{Lists: Introduction} - \begin{lstlisting} -In []: time = [0., 1., 2, 3] - -In []: distance = [7., 11, 15, 19] - \end{lstlisting} -What are \typ{time} and \typ{distance}?\\ -\begin{center} - \large -\alert{\typ{lists!!}} -\end{center} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Lists: Initializing \& accessing elements} -\begin{lstlisting} -In []: mtlist = [] -\end{lstlisting} -\emphbar{Empty List} -\begin{lstlisting} -In []: p = [ 2, 3, 5, 7] - -In []: p[1] -Out[]: 3 - -In []: p[0]+p[1]+p[-1] -Out[]: 12 -\end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{List: Slicing} - \begin{block}{Remember\ldots} - \kwrd{In []: p = [ 2, 3, 5, 7]} - \end{block} -\begin{lstlisting} -In []: p[1:3] -Out[]: [3, 5] -\end{lstlisting} -\emphbar{A slice} -\begin{lstlisting} -In []: p[0:-1] -Out[]: [2, 3, 5] -In []: p[1:] -Out[]: [3, 5, 7] -\end{lstlisting} -\end{frame} - -\begin{frame}[plain,fragile] - \frametitle{List: Slicing \ldots} - \vspace*{-0.1in} - \begin{small} - \begin{block}{Remember\ldots} - \kwrd{In []: p = [ 2, 3, 5, 7]} -\end{block} -\end{small} -\begin{lstlisting} -In []: p[0:4:2] -Out[]: [2, 5] -In []: p[0::2] -Out[]: [2, 5] -In []: p[::2] -Out[]: [2, 5] -In []: p[::3] -Out[]: [2, 7] -In []: p[::-1] -Out[]: [7, 5, 3, 2] -\end{lstlisting} -\alert{\typ{list[initial:final:step]}} -\end{frame} - -\begin{frame}[fragile] - \frametitle{List: Slicing} - \begin{block}{Remember\ldots} - \kwrd{In []: p = [ 2, 3, 5, 7]} - \end{block} - What is the output of the following? -\begin{lstlisting} -In []: p[1::2] - -In []: p[1:-1:2] -\end{lstlisting} -\end{frame} - - -%% more on list slicing -\begin{frame}[fragile] -\frametitle{List operations} -\begin{lstlisting} -In []: b = [ 11, 13, 17] -In []: c = p + b - -In []: c -Out[]: [2, 3, 5, 7, 11, 13, 17] - -In []: p.append(11) -In []: p -Out[]: [ 2, 3, 5, 7, 11] -\end{lstlisting} -Question: Does \typ{c} change now that \typ{p} is changed? -\inctime{10} -\end{frame} - -\section{Simple Pendulum} -\begin{frame}[fragile] -\frametitle{Simple Pendulum - L and T} -Let us look at the Simple Pendulum experiment. -\begin{center} -\begin{small} -\begin{tabular}{| c | c | c |} -\hline -$L$ & $T$ & $T^2$ \\ \hline -0.2 & 0.90 & \\ \hline -0.3 & 1.19 & \\ \hline -0.4 & 1.30 & \\ \hline -0.5 & 1.47 & \\ \hline -0.6 & 1.58 & \\ \hline -0.7 & 1.77 & \\ \hline -0.8 & 1.83 & \\ \hline -\end{tabular} -\end{small}\\ -\alert{$L \alpha T^2$} -\end{center} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Lets use lists} -\begin{lstlisting} -In []: L = [0.2, 0.3, 0.4, 0.5, - 0.6, 0.7, 0.8] - -In []: t = [0.90, 1.19, 1.30, - 1.47, 1.58, 1.77, - 1.83] -\end{lstlisting} -\alert{Gotcha}: Make sure \typ{L} and \typ{t} have the same number -of elements - -\begin{lstlisting} -In []: print(len(L), len(t)) -\end{lstlisting} - -\end{frame} - -\begin{frame}[fragile] -\frametitle{Plotting $L$ vs $T^2$} -\begin{itemize} -\item We must square each of the values in \typ{t} -\item How do we do it? -\item We use a \kwrd{for} loop to iterate over \typ{t} -\end{itemize} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Looping with \texttt{for}} -\begin{lstlisting} -In []: for time in t: - ....: print(time*time) - ....: - ....: -\end{lstlisting} -This will print the square of each item in the list, \typ{t} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Plotting $L$ vs $T^2$} -\begin{lstlisting} -In []: tsq = [] - -In []: for time in t: - ....: tsq.append(time*time) - ....: - ....: - -\end{lstlisting} -This gives \typ{tsq} which is the list of squares of \typ{t} values. -\begin{lstlisting} -In []: print(len(L), len(t), len(tsq)) -Out[]: (7, 7, 7) - -In []: plot(L, tsq) -\end{lstlisting} -\end{frame} - -\begin{frame}[fragile] -\begin{figure} -\includegraphics[width=3.5in]{data/L-TSq-limited.png} -\end{figure} -\inctime{10} -\end{frame} - - -\begin{frame}[fragile] -\frametitle{Don't repeat yourself: functions} -\noindent Let us define a function to square the list -\begin{lstlisting} -In []: def sqr(arr): - ...: result = [] - ...: for x in arr: - ...: result.append(x*x) - ...: return result - ...: - -In []: tsq = sqr(t) - -\end{lstlisting} %$ -\end{frame} - -\begin{frame}[fragile] - \frametitle{More on defining functions} - \begin{itemize} - \item Consider the function \texttt{f(x) = x\textasciicircum{}2} - \item Let's write a Python function, equivalent to this - \end{itemize} - \begin{lstlisting} - In[]: def f(x): - ....: return x*x - ....: - - In[]: f(1) - In[]: f(2) - \end{lstlisting} - \begin{itemize} - \item \texttt{def} is a keyword - \item \texttt{f} is the name of the function - \item \texttt{x} the parameter of the function (local variable) - \item \texttt{return} is a keyword - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Aside: Exercise} - \begin{itemize} - \item Write a function called \typ{mysum(a, b)} that returns sum of two - arguments. - \end{itemize} - \pause -\begin{lstlisting} -In []: def mysum(a, b): - ...: return a + b - ...: -In []: mysum(1, 2) - -In []: mysum([1, 2], [3, 4]) -\end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{This seems tedious} - - \begin{itemize} - \item Do we have to write a function just to get the square of a list? - \item Lists -\begin{itemize} - \item Nice - \item Not too convenient for math - \item Slow -\end{itemize} -\item Enter NumPy arrays - \begin{itemize} - \item Fixed size, data type - \item Very convenient - \item Fast - \end{itemize} - \end{itemize} - \inctime{10} -\end{frame} - -\subsection{\num\ arrays} - -\begin{frame}[fragile] -\frametitle{NumPy arrays} -\begin{lstlisting} -In []: t = array(t) - -In []: tsq = t*t - -In []: print(tsq) - -In []: plot(L, tsq) # works! -\end{lstlisting} %$ -\end{frame} - -\begin{frame}[fragile] -\frametitle{Speed?} - -\noindent Lets use range to create a large list. - -\begin{lstlisting} -In []: t = range(1000000) - -In []: tsq = sqr(t) - -\end{lstlisting} %$ - -\noindent Now try it with - -\begin{lstlisting} -In []: t = array(t) - -In []: tsq = t*t -\end{lstlisting} -\ldots -\end{frame} - - -\begin{frame}[fragile] - \frametitle{IPython tip: Timing} - -Try the following: - \begin{lstlisting} -In []: %timeit sqr(t) - -In []: %timeit? - - \end{lstlisting} - - \begin{itemize} - \item \typ{\%timeit}: accurate, many measurements - \item Can also use \typ{\%time} - \item \typ{\%time}: less accurate, one measurement - \end{itemize} - -\inctime{10} -\end{frame} - - -\begin{frame}[fragile] -\frametitle{Exercise} -\begin{center} - Find out the speed difference between the \typ{sqr} function and - \typ{t*t} on the numpy array. -\end{center} - -\end{frame} - -\begin{frame}[fragile] - \frametitle{Solution} -\begin{lstlisting} -In []: t = linspace(0, 10, 100000) -In []: %timeit sqr(t) -In []: %timeit t*t -\end{lstlisting} - \inctime{5} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Summary} -\begin{itemize} -\item Plot attributes -\item plotting points -\item Lists -\item Defining simple functions -\item Introduction to \num\ arrays -\item Timing with \typ{\%timeit} -\end{itemize} -\end{frame} - -\end{document} diff --git a/scipy/basic/mlab.tex b/scipy/basic/mlab.tex deleted file mode 100644 index 5449b1f..0000000 --- a/scipy/basic/mlab.tex +++ /dev/null @@ -1,523 +0,0 @@ -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%Tutorial slides on Python. -% -% Author: FOSSEE -% Copyright (c) 2009-2016, FOSSEE, IIT Bombay -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -\documentclass[14pt,compress]{beamer} -%\documentclass[draft]{beamer} -%\documentclass[compress,handout]{beamer} -%\usepackage{pgfpages} -%\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm] - -% Modified from: generic-ornate-15min-45min.de.tex -\mode -{ - \usetheme{Warsaw} - \useoutertheme{infolines} - \setbeamercovered{transparent} -} - -\usepackage[english]{babel} -\usepackage[latin1]{inputenc} -%\usepackage{times} -\usepackage[T1]{fontenc} -\usepackage{pgf} - -% Taken from Fernando's slides. -\usepackage{ae,aecompl} -\usepackage{mathpazo,courier,euler} -\usepackage[scaled=.95]{helvet} -\usepackage{amsmath} - -\definecolor{darkgreen}{rgb}{0,0.5,0} - -\usepackage{listings} -\lstset{language=Python, - basicstyle=\ttfamily\bfseries, - commentstyle=\color{red}\itshape, - stringstyle=\color{darkgreen}, - showstringspaces=false, - keywordstyle=\color{blue}\bfseries} - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Macros -\setbeamercolor{emphbar}{bg=blue!20, fg=black} -\newcommand{\emphbar}[1] -{\begin{beamercolorbox}[rounded=true]{emphbar} - {#1} - \end{beamercolorbox} -} - -\newcommand{\myemph}[1]{\structure{\emph{#1}}} -\newcommand{\PythonCode}[1]{\lstinline{#1}} - -\newcounter{time} -\setcounter{time}{0} -\newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}} - -\newcommand{\typ}[1]{\lstinline{#1}} - -\newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}} } - -%%% This is from Fernando's setup. -% \usepackage{color} -% \definecolor{orange}{cmyk}{0,0.4,0.8,0.2} -% % Use and configure listings package for nicely formatted code -% \usepackage{listings} -% \lstset{ -% language=Python, -% basicstyle=\small\ttfamily, -% commentstyle=\ttfamily\color{blue}, -% stringstyle=\ttfamily\color{orange}, -% showstringspaces=false, -% breaklines=true, -% postbreak = \space\dots -% } - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Title page -\title[Mayavi mlab]{Introductory Scientific Computing with -Python} -\subtitle{Simple 3D plots with Mayavi's mlab} - -\author[Prabhu] {FOSSEE} - -\institute[FOSSEE -- IITB] {Department of Aerospace Engineering\\IIT Bombay} -\date[] { -Mumbai -} -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -%\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} -%\logo{\pgfuseimage{iitmlogo}} - - -%% Delete this, if you do not want the table of contents to pop up at -%% the beginning of each subsection: -\AtBeginSubsection[] -{ - \begin{frame} - \frametitle{Outline} - \tableofcontents[currentsection,currentsubsection] - \end{frame} -} - -\AtBeginSection[] -{ - \begin{frame} - \frametitle{Outline} - \tableofcontents[currentsection,currentsubsection] - \end{frame} -} - -% If you wish to uncover everything in a step-wise fashion, uncomment -% the following command: -%\beamerdefaultoverlayspecification{<+->} - -%\includeonlyframes{current,current1,current2,current3,current4,current5,current6} - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% DOCUMENT STARTS -\begin{document} - -\begin{frame} - \maketitle -\end{frame} - -\section{Introduction to Mayavi} - -\begin{frame} - \frametitle{Mayavi} - \begin{itemize} - \item 3D visualization library - \item Application - \item Support for different data formats - \item Embeddable, extensible - \item Cross-platform - \item Fully scriptable - \end{itemize} -\end{frame} - -\begin{frame} - {Overview of features} - \vspace*{-0.3in} - \begin{center} - \hspace*{-0.2in}\pgfimage[width=5in]{mlab_data/m2_app3_3} - \end{center} -\end{frame} - - -\begin{frame} - \frametitle{Mayavi in applications} - \vspace*{-0.3in} - \begin{center} - \hspace*{-0.2in}\pgfimage[width=4.5in]{mlab_data/m2_envisage} - \end{center} -\end{frame} - -\begin{frame} - \frametitle{Live in your dialogs} - \vspace*{0.1in} - \begin{center} - \hspace*{-0.2in}\pgfimage[width=2.5in]{mlab_data/mlab_tui} - \end{center} -\end{frame} - - - -\begin{frame} - \frametitle{Installation} - \begin{itemize} - \item Suggest using a Python distribution - \item Canopy's package manager: simply install mayavi - \item \typ{conda install -c menpo mayavi} - \item \typ{edm install mayavi} - \end{itemize} -\end{frame} - -\section{Getting started with mlab} - -\begin{frame} - {Overview} - \begin{itemize} - \item Simple - \item Convenient - \item Full-featured - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Getting started} - - \myemph{With \typ{jupyter}:} -\begin{lstlisting}[language=bash] - $ jupyter console - In [1]: %gui qt -\end{lstlisting} %$ - \vspace*{0.15in} - - \myemph{Vanilla:} -\begin{lstlisting}[language=bash] - $ ipython --gui=qt -\end{lstlisting} %$ - \vspace*{0.15in} - -\myemph{Or:} -\begin{lstlisting}[language=bash] - $ ipython - In [1]: %gui qt -\end{lstlisting} %$ - -\end{frame} - -\begin{frame}[fragile] - \frametitle{Using notebooks} - - Make sure you have the following code first: - -\begin{lstlisting} - In [1]: from mayavi import mlab - In [2]: mlab.init_notebook() -\end{lstlisting} - - \begin{itemize} - \item Can also use \typ{\%gui qt} - \item Plots will pop-up - \end{itemize} - -\end{frame} - -\section{Basic Mayavi: \typ{mlab}} - -\begin{frame}[fragile] -\frametitle{Basic 3D visualization} -\begin{itemize} - \item Simple one liners for 3D visualization - \item Must import mlab. -\end{itemize} -\begin{lstlisting} -In []: from mayavi import mlab -\end{lstlisting} -Ready to go! -\end{frame} - -\begin{frame}[fragile] - \frametitle{Simple example} - - \begin{lstlisting} -In []: mlab.test_ - -# Let us start with this: -In []: mlab.test_contour3d() -\end{lstlisting} -Explore the UI. -\end{frame} - -\begin{frame} - {Exploring the view} - \begin{columns} - \column{0.6\textwidth} - \pgfimage[width=3in]{mlab_data/mlab_contour_window} - \column{0.4\textwidth} - \begin{itemize} - \item Mouse - \item Keyboard - \item Toolbar - \item Mayavi icon\pgfimage[width=0.2in]{mlab_data/m2_icon} - \end{itemize} - \end{columns} -\end{frame} - -\begin{frame}[fragile] - \frametitle{\typ{mlab} plotting functions} - \begin{columns} - \column{0.25\textwidth} - \myemph{\Large 0D data} - \column{0.5\textwidth} - \pgfimage[width=2in]{mlab_data/points3d_ex} - \end{columns} - - \begin{lstlisting} -In []: t = linspace(0, 2*pi, 50) -In []: u = cos(t) * pi -In []: x, y, z = sin(u), cos(u), sin(t) - \end{lstlisting} - \emphbar{\PythonCode{In []: mlab.points3d(x, y, z)}} -\end{frame} - -\begin{frame}[plain, fragile] - \frametitle{Changing how things look} - - \begin{block}{Clearing the view} - \PythonCode{>>> mlab.clf()} - \end{block} - - \pause - - \begin{block}{IPython is your friend!} - \PythonCode{>>> mlab.points3d?} - - \begin{itemize} - - \item Extra argument: Scalars - - \item Keyword arguments - - \item UI - \end{itemize} - \end{block} -\pause - \begin{lstlisting} -In []: mlab.points3d(x, y, z, t, - scale_mode='none') - \end{lstlisting} -\end{frame} - - -\begin{frame}[fragile] - \begin{columns} - \column{0.25\textwidth} - \myemph{\Large 1D data} - \column{0.5\textwidth} - \pgfimage[width=2.5in]{mlab_data/plot3d_ex} - \end{columns} - \emphbar{\PythonCode{In []: mlab.plot3d(x, y, z, t)}} - - Plots lines between the points - -\end{frame} - -\begin{frame}[fragile] - \begin{columns} - \column{0.25\textwidth} - \myemph{\Large 2D data} - \column{0.5\textwidth} - \pgfimage[width=2in]{mlab_data/surf_ex} - \end{columns} - \begin{lstlisting} -In []: x, y = mgrid[-3:3:100j,-3:3:100j] -In []: z = sin(x*x + y*y) - \end{lstlisting} - - \emphbar{\PythonCode{In []: mlab.surf(x, y, z)}} - - \alert{Assumes the points are rectilinear} - -\end{frame} - -\begin{frame}[fragile] - \frametitle{mgrid example} - \begin{lstlisting} -In []: mgrid[0:3,0:3] -Out[]: -array([[[0, 0, 0], - [1, 1, 1], - [2, 2, 2]], - - [[0, 1, 2], - [0, 1, 2], - [0, 1, 2]]]) - -In []: mgrid[-1:1:5j] -Out[]: array([-1., -0.5, 0., 0.5, 1.]) -\end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{mgrid example} - \begin{lstlisting} -In []: x, y = mgrid[-1:1:5j, -1:1:5j] -In []: z = x*x + y*y - -In []: z -Out[]: -array([[ 2. , 1.25, 1. , 1.25, 2. ], - [ 1.25, 0.5 , 0.25, 0.5 , 1.25], - [ 1. , 0.25, 0. , 0.25, 1. ], - [ 1.25, 0.5 , 0.25, 0.5 , 1.25], - [ 2. , 1.25, 1. , 1.25, 2. ]]) -\end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \myemph{\Large 2D data: \texttt{mlab.mesh}} - \vspace*{0.25in} - - \emphbar{\PythonCode{In []: mlab.mesh(x, y, z)}} - - \alert{Points needn't be regular} - - \vspace*{0.25in} -\begin{lstlisting} -In []: phi, theta = mgrid[0:pi:20j, -... 0:2*pi:20j] -In []: x = sin(phi)*cos(theta) -In []: y = sin(phi)*sin(theta) -In []: z = cos(phi) -In []: mlab.mesh(x, y, z, -... representation= -... 'wireframe') -\end{lstlisting} - -\end{frame} - - -\begin{frame}[fragile] - - \myemph{\Large 3D data} - \vspace*{0.25in} - - \pgfimage[width=1.5in]{mlab_data/contour3d}\\ - -\begin{lstlisting} -In []: x, y, z = mgrid[-5:5:64j, - ...: -5:5:64j, - ...: -5:5:64j] -In []: mlab.contour3d(x*x*0.5 + y*y + - z*z*2) -\end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - - \myemph{\Large 3D vector data: \PythonCode{mlab.quiver3d}} - \vspace*{0.25in} - - \pgfimage[width=2in]{mlab_data/quiver3d_ex}\\ - -\begin{lstlisting} -In []: mlab.test_quiver3d() -\end{lstlisting} - -\emphbar{\PythonCode{obj = mlab.quiver3d(x, y, z, u, v, w)}} -\end{frame} - - -\begin{frame}[fragile] - {Other utility functions} - \begin{itemize} - \item \PythonCode{gcf}: get current figure - \pause - \item \PythonCode{savefig}, \PythonCode{figure} - \pause - \item \PythonCode{axes}, \PythonCode{outline} - \pause - \item \PythonCode{title}, \PythonCode{xlabel, ylabel, zlabel} - \pause - \item \PythonCode{colorbar}, \PythonCode{scalarbar}, - \PythonCode{vectorbar} - \pause - \item \PythonCode{show}: Standalone mlab scripts - \pause - \item Others, see UG - \end{itemize} -\end{frame} - - -\begin{frame} - {Exploring the documentation} - \begin{center} - \pgfimage[width=4in]{mlab_data/m2_ug_doc} - \end{center} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Explore the examples} - - \myemph{\Large Try these} - -\begin{lstlisting} -In []: mlab.clf() -In []: mlab.test_points3d() -In []: mlab.clf() -In []: mlab.test_plot3d() -In []: mlab.clf() -\end{lstlisting} - Explore these later -\end{frame} - - -\begin{frame} - \frametitle{Summary} - \begin{itemize} - \item Quick introduction to Mayavi's mlab - \item Documentation: \url{github.enthought.com/mayavi/mayavi} - \end{itemize} - -\end{frame} - -\end{document} - - -\begin{frame} - {An exercise} - - \begin{block}{Exercise} - Atmospheric data of temperature over the surface of the earth. - Let temperature ($T$) vary linearly with height ($z$): - \begin{center} - $T = 288.15 - 6.5z$ - \end{center} - \end{block} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Simple solution} - - \begin{lstlisting} -lat = linspace(-89, 89, 37) -lon = linspace(0, 360, 37) -z = linspace(0, 100, 11) - \end{lstlisting} -\pause - \begin{lstlisting} -x, y, z = mgrid[0:360:37j,-89:89:37j, - 0:100:11j] -t = 288.15 - 6.5*z -mlab.contour3d(x, y, z, t) -mlab.outline() -mlab.colorbar() - \end{lstlisting} -\end{frame} diff --git a/scipy/basic/more_numpy.tex b/scipy/basic/more_numpy.tex deleted file mode 100644 index c53cc41..0000000 --- a/scipy/basic/more_numpy.tex +++ /dev/null @@ -1,668 +0,0 @@ -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%Tutorial slides on Python. -% -% Author: FOSSEE -% Copyright (c) 2009, FOSSEE, IIT Bombay -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -\documentclass[14pt,compress]{beamer} -%\documentclass[draft]{beamer} -%\documentclass[compress,handout]{beamer} -%\usepackage{pgfpages} -%\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm] - -% Modified from: generic-ornate-15min-45min.de.tex -\mode -{ - \usetheme{Warsaw} - \useoutertheme{infolines} - \setbeamercovered{transparent} -} - -\usepackage[english]{babel} -\usepackage[latin1]{inputenc} -%\usepackage{times} -\usepackage[T1]{fontenc} -\usepackage{amsmath} - -% Taken from Fernando's slides. -\usepackage{ae,aecompl} -\usepackage{mathpazo,courier,euler} -\usepackage[scaled=.95]{helvet} - -\definecolor{darkgreen}{rgb}{0,0.5,0} - -\usepackage{listings} -\lstset{language=Python, - basicstyle=\ttfamily\bfseries, - commentstyle=\color{red}\itshape, - stringstyle=\color{darkgreen}, - showstringspaces=false, - keywordstyle=\color{blue}\bfseries} - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Macros -\setbeamercolor{emphbar}{bg=blue!20, fg=black} -\newcommand{\emphbar}[1] -{\begin{beamercolorbox}[rounded=true]{emphbar} - {#1} - \end{beamercolorbox} -} -\newcounter{time} -\setcounter{time}{0} -\newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}} - -\newcommand{\typ}[1]{\lstinline{#1}} - -\newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}} } - -%%% This is from Fernando's setup. -% \usepackage{color} -% \definecolor{orange}{cmyk}{0,0.4,0.8,0.2} -% % Use and configure listings package for nicely formatted code -% \usepackage{listings} -% \lstset{ -% language=Python, -% basicstyle=\small\ttfamily, -% commentstyle=\ttfamily\color{blue}, -% stringstyle=\ttfamily\color{orange}, -% showstringspaces=false, -% breaklines=true, -% postbreak = \space\dots -% } - - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Title page -\title[More on numpy arrays]{Introductory Scientific Computing with -Python} -\subtitle{More on numpy arrays} - -\author[FOSSEE] {FOSSEE} - -\institute[FOSSEE -- IITB] {Department of Aerospace Engineering\\IIT Bombay} -\date[] {Mumbai, India} -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -%\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} -%\logo{\pgfuseimage{iitmlogo}} - - -%% Delete this, if you do not want the table of contents to pop up at -%% the beginning of each subsection: -\AtBeginSubsection[] -{ - \begin{frame} - \frametitle{Outline} - \tableofcontents[currentsection,currentsubsection] - \end{frame} -} - -\AtBeginSection[] -{ - \begin{frame} - \frametitle{Outline} - \tableofcontents[currentsection,currentsubsection] - \end{frame} -} - -% If you wish to uncover everything in a step-wise fashion, uncomment -% the following command: -%\beamerdefaultoverlayspecification{<+->} - -%\includeonlyframes{current,current1,current2,current3,current4,current5,current6} - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% DOCUMENT STARTS -\begin{document} - -\begin{frame} - \titlepage -\end{frame} - -\begin{frame} - \frametitle{Outline} - \tableofcontents -% \pausesections -\end{frame} - -\section{Matrices} - -\begin{frame} -\frametitle{Matrices: Introduction} -\alert{All matrix operations are done using \kwrd{arrays}} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Matrices: Initializing} -\begin{lstlisting} -In []: c = array([[11,12,13], - [21,22,23], - [31,32,33]]) - -In []: c -Out[]: -array([[11, 12, 13], - [21, 22, 23], - [31, 32, 33]]) -\end{lstlisting} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Initializing some special matrices} -\begin{small} - \begin{lstlisting} -In []: ones((3,5)) -Out[]: -array([[ 1., 1., 1., 1., 1.], - [ 1., 1., 1., 1., 1.], - [ 1., 1., 1., 1., 1.]]) - -In []: ones_like([1, 2, 3, 4]) -Out[]: array([1, 1, 1, 1]) - -In []: identity(2) -Out[]: -array([[ 1., 0.], - [ 0., 1.]]) - \end{lstlisting} -Also available \alert{\typ{zeros, zeros_like, empty, empty_like}} -\end{small} -\end{frame} - - -\begin{frame}[fragile] - \frametitle{Accessing elements} - \begin{small} - \begin{lstlisting} -In []: c -Out[]: -array([[11, 12, 13], - [21, 22, 23], - [31, 32, 33]]) - -In []: c[1][2] -Out[]: 23 -In []: c[1,2] -Out[]: 23 - -In []: c[1] -Out[]: array([21, 22, 23]) - \end{lstlisting} - \end{small} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Changing elements} - \begin{small} - \begin{lstlisting} -In []: c[1,1] = -22 -In []: c -Out[]: -array([[ 11, 12, 13], - [ 21, -22, 23], - [ 31, 32, 33]]) - -In []: c[1] = 0 -In []: c -Out[]: -array([[11, 12, 13], - [ 0, 0, 0], - [31, 32, 33]]) - \end{lstlisting} - \end{small} -How do you access one \alert{column}? -\end{frame} - -\begin{frame}[fragile] - \frametitle{Slicing} -\begin{small} - \begin{lstlisting} -In []: c[:,1] -Out[]: array([12, 0, 32]) - -In []: c[1,:] -Out[]: array([0, 0, 0]) - -In []: c[0:2,:] -Out[]: -array([[11, 12, 13], - [ 0, 0, 0]]) - -In []: c[1:3,:] -Out[]: -array([[ 0, 0, 0], - [31, 32, 33]]) - \end{lstlisting} -\end{small} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Slicing \ldots} -\begin{small} - \begin{lstlisting} -In []: c[:2,:] -Out[]: -array([[11, 12, 13], - [ 0, 0, 0]]) - -In []: c[1:,:] -Out[]: -array([[ 0, 0, 0], - [31, 32, 33]]) - -In []: c[1:,:2] -Out[]: -array([[ 0, 0], - [31, 32]]) - \end{lstlisting} - -\end{small} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Striding} - \begin{small} - \begin{lstlisting} -In []: c[::2,:] -Out[]: -array([[11, 12, 13], - [31, 32, 33]]) - -In []: c[:,::2] -Out[]: -array([[11, 13], - [ 0, 0], - [31, 33]]) - -In []: c[::2,::2] -Out[]: -array([[11, 13], - [31, 33]]) - \end{lstlisting} - \end{small} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Shape of a matrix} - \begin{lstlisting} -In []: c -Out[]: -array([[11, 12, 13], - [ 0, 0, 0], - [31, 32, 33]]) - -In []: c.shape -Out[]: (3, 3) - \end{lstlisting} -\emphbar{Shape specifies shape or dimensions of a matrix} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Elementary image processing} -\begin{small} - \begin{lstlisting} -In []: a = imread('lena.png') - -In []: imshow(a) -Out[]: - \end{lstlisting} - \end{small} -\typ{imread} returns an array of shape (512, 512, 4) which represents an image of 512x512 pixels and 4 shades.\\ -\typ{imshow} renders the array as an image. -\end{frame} - -\begin{frame}[fragile] -\frametitle{Slicing \& Striding Exercises} - \begin{itemize} - \item Crop the image to get the top-left quarter - \item Crop the image to get only the face - \item Resize image to half by dropping alternate pixels - \end{itemize} - -\end{frame} -\begin{frame}[fragile] - \frametitle{Solutions} -\begin{small} - \begin{lstlisting} -In []: imshow(a[:256,:256]) -Out[]: - -In []: imshow(a[200:400,200:400]) -Out[]: - -In []: imshow(a[::2,::2]) -Out[]: - \end{lstlisting} -\end{small} -\inctime{15} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Transpose of a Matrix} -\begin{lstlisting} -In []: a = array([[ 1, 1, 2, -1], - ...: [ 2, 5, -1, -9], - ...: [ 2, 1, -1, 3], - ...: [ 1, -3, 2, 7]]) - -In []: a.T -Out[]: -array([[ 1, 2, 2, 1], - [ 1, 5, 1, -3], - [ 2, -1, -1, 2], - [-1, -9, 3, 7]]) -\end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Matrix Addition} - \begin{lstlisting} -In []: b = array([[3,2,-1,5], - [2,-2,4,9], - [-1,0.5,-1,-7], - [9,-5,7,3]]) -In []: a + b -Out[]: -array([[ 4. , 3. , 1. , 4. ], - [ 4. , 3. , 3. , 0. ], - [ 1. , 1.5, -2. , -4. ], - [ 10. , -8. , 9. , 10. ]]) - \end{lstlisting} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Elementwise Multiplication} -\begin{lstlisting} -In []: a*b -Out[]: -array([[ 3. , 2. , -2. , -5. ], - [ 4. , -10. , -4. , -81. ], - [ -2. , 0.5, 1. , -21. ], - [ 9. , 15. , 14. , 21. ]]) - -\end{lstlisting} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Matrix Multiplication} -\begin{lstlisting} -In []: dot(a, b) -Out[]: -array([[ -6. , 6. , -6. , -3. ], - [-64. , 38.5, -44. , 35. ], - [ 36. , -13.5, 24. , 35. ], - [ 58. , -26. , 34. , -15. ]]) -\end{lstlisting} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Inverse of a Matrix} -\begin{lstlisting} - -\end{lstlisting} -\begin{small} -\begin{lstlisting} -In []: inv(a) -Out[]: -array([[-0.5 , 0.55, -0.15, 0.7 ], - [ 0.75, -0.5 , 0.5 , -0.75], - [ 0.5 , -0.15, -0.05, -0.1 ], - [ 0.25, -0.25, 0.25, -0.25]]) -\end{lstlisting} -\end{small} -\emphbar{Try this: \typ{I = dot(a, inv(a))}} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Determinant and sum of all elements} -\begin{lstlisting} -In []: det(a) -Out[]: 80.0 -\end{lstlisting} - \begin{lstlisting} -In []: sum(a) -Out[]: 12 - \end{lstlisting} - -\end{frame} - -%%use S=array(X,Y) -\begin{frame}[fragile] -\frametitle{Eigenvalues and Eigen Vectors} -\begin{small} -\begin{lstlisting} -In []: e = array([[3,2,4],[2,0,2],[4,2,3]]) - -In []: eig(e) -Out[]: -(array([-1., 8., -1.]), - array([[-0.74535599, 0.66666667, -0.1931126 ], - [ 0.2981424 , 0.33333333, -0.78664085], - [ 0.59628479, 0.66666667, 0.58643303]])) - -In []: eigvals(e) -Out[]: array([-1., 8., -1.]) -\end{lstlisting} -\end{small} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Computing Norms} -\begin{lstlisting} -In []: norm(e) -Out[]: 8.1240384046359608 -\end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Singular Value Decomposition} - \begin{small} - \begin{lstlisting} -In []: svd(e) -Out[]: -(array( -[[ -6.66666667e-01, -1.23702565e-16, 7.45355992e-01], - [ -3.33333333e-01, -8.94427191e-01, -2.98142397e-01], - [ -6.66666667e-01, 4.47213595e-01, -5.96284794e-01]]), - array([ 8., 1., 1.]), - array([[-0.66666667, -0.33333333, -0.66666667], - [-0. , 0.89442719, -0.4472136 ], - [-0.74535599, 0.2981424 , 0.59628479]])) - \end{lstlisting} - \end{small} - \inctime{10} -\end{frame} - -\section{Least Squares Fit} -\begin{frame}[fragile] -\frametitle{$L$ vs. $T^2$ - Scatter} -Linear trend visible. -\vspace{-0.1in} -\begin{figure} -\includegraphics[width=4in]{data/L-Tsq-points} -\end{figure} -\end{frame} - -\begin{frame}[fragile] -\frametitle{$L$ vs. $T^2$ - Line} -This plot is not a straight line, can we do better? -\vspace{-0.1in} -\begin{figure} -\includegraphics[width=4in]{data/L-Tsq-Line} -\end{figure} -\end{frame} - -\begin{frame}[fragile] -\frametitle{$L$ vs. $T^2$ - Least Square Fit} -This is what our intention is. -\vspace{-0.1in} -\begin{figure} -\includegraphics[width=4in]{data/least-sq-fit} -\end{figure} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Matrix Formulation} -\begin{itemize} -\item We need to fit a line through points for the equation $T^2 = m \cdot L+c$ -\item In matrix form, the equation can be represented as $T_{sq} = A \cdot p$, -\item We need to find $p$ to plot the line -\end{itemize} -\end{frame} - -\begin{frame} - \begin{equation} - \begin{bmatrix} - T^2_1 \\ - T^2_2 \\ - \vdots\\ - T^2_N \\ -\end{bmatrix} -= \begin{bmatrix} - L_1 & 1 \\ - L_2 & 1 \\ - \vdots & \vdots\\ - L_N & 1 \\ -\end{bmatrix} \cdot -\begin{bmatrix} - m\\ - c\\ - \end{bmatrix} -\end{equation} - -Or - -\[T_{sq} = A \cdot p \] - -\end{frame} - - -\begin{frame}[fragile] -\frametitle{Getting $L$ and $T^2$} -%If you \alert{closed} IPython after session 2 -\begin{lstlisting} -In []: L, t = loadtxt('pendulum.txt', - ...: unpack=True) -In []: tsq = t*t -\end{lstlisting} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Generating $A$} -\begin{lstlisting} -In []: A = array([L, ones_like(L)]) -In []: A = A.T -\end{lstlisting} -%% \begin{itemize} -%% \item A is also called a Van der Monde matrix -%% \item It can also be generated using \typ{vander} -%% \end{itemize} -%% \begin{lstlisting} -%% In []: A = vander(L, 2) -%% \end{lstlisting} -\end{frame} - -\begin{frame}[fragile] -\frametitle{\typ{lstsq} \ldots} -\begin{itemize} -\item Now use the \typ{lstsq} function -\item Along with a lot of things, it returns the least squares solution -\end{itemize} -\begin{lstlisting} -In []: result = lstsq(A,tsq) -In []: coef = result[0] -\end{lstlisting} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Least Square Fit Line \ldots} -We get the points of the line from \typ{coef} -\begin{lstlisting} -In []: Tline = coef[0]*L + coef[1] - -In []: Tline.shape -\end{lstlisting} -\begin{itemize} -\item Now plot \typ{Tline} vs. \typ{L}, to get the least squares fit line. -\end{itemize} -\begin{lstlisting} -In []: plot(L, Tline, 'r') - -In []: plot(L, tsq, 'o') -\end{lstlisting} -\inctime{10} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Least Squares Fit} -\vspace{-0.15in} -\begin{figure} -\includegraphics[width=4in]{data/least-sq-fit} -\end{figure} -\end{frame} - -\section{Random numbers} - -\begin{frame}[fragile] - \frametitle{\typ{numpy.random}} - \begin{itemize} - \item Easy random number generation - \end{itemize} - \begin{lstlisting} -In []: random? - \end{lstlisting} - Or: - \begin{lstlisting} -In []: import numpy -In []: numpy.random - \end{lstlisting} -\begin{itemize} - \item \typ{random.random}: produces uniform deviates in $(0, 1)$ - \item \typ{random.normal}: draws random samples from a Gaussian - distribution - \item Useful to create a random matrix of any shape -\end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Using the \typ{random} module} -\begin{lstlisting} -In []: x = random.random(size=100) -In []: y = random.random(size=100) -In []: scatter(x, y) # Scatter plot it. - -In []: x,y = random.normal(size=(2,100)) -In []: clf() -In []: scatter(x, y) -\end{lstlisting} -Note that \typ{size} can be a tuple. - -\end{frame} - -\begin{frame}[fragile] - \frametitle{Using the \typ{random} module} -\begin{lstlisting} -In []: img = random.random( - ...: size=(200, 200) - ...: ) -In []: clf() -In []: imshow(img) -In []: colorbar() -\end{lstlisting} -\end{frame} - - - -\section{Summary} -\begin{frame} - \frametitle{What did we learn?} - \begin{itemize} - \item Matrices - \item Least Squares - \item Random numbers - \end{itemize} -\inctime{10} -\end{frame} - -\end{document} diff --git a/scipy/basic/notebook.tex b/scipy/basic/notebook.tex deleted file mode 100644 index 4442c9f..0000000 --- a/scipy/basic/notebook.tex +++ /dev/null @@ -1,325 +0,0 @@ -\documentclass[14pt,compress]{beamer} -%\documentclass[draft]{beamer} -%\documentclass[compress,handout]{beamer} -%\usepackage{pgfpages} -%\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm] - -% Modified from: generic-ornate-15min-45min.de.tex -\mode -{ - \usetheme{Warsaw} - \useoutertheme{infolines} - \setbeamercovered{transparent} -} - -\usepackage[english]{babel} -\usepackage[latin1]{inputenc} -%\usepackage{times} -\usepackage[T1]{fontenc} -\usepackage{pgf} - -% Taken from Fernando's slides. -\usepackage{ae,aecompl} -\usepackage{mathpazo,courier,euler} -\usepackage[scaled=.95]{helvet} -\usepackage{amsmath} - -\definecolor{darkgreen}{rgb}{0,0.5,0} - -\usepackage{listings} -\lstset{language=Python, - basicstyle=\ttfamily\bfseries, - commentstyle=\color{red}\itshape, - stringstyle=\color{darkgreen}, - showstringspaces=false, - keywordstyle=\color{blue}\bfseries} - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Macros -\setbeamercolor{emphbar}{bg=blue!20, fg=black} -\newcommand{\emphbar}[1] -{\begin{beamercolorbox}[rounded=true]{emphbar} - {#1} - \end{beamercolorbox} -} - -\newcommand{\myemph}[1]{\structure{\emph{#1}}} -\newcommand{\PythonCode}[1]{\lstinline{#1}} - -\newcounter{time} -\setcounter{time}{0} -\newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}} - -\newcommand{\typ}[1]{\lstinline{#1}} - -\newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}} } - - -\newcommand\BackgroundPicture[1]{% - \setbeamertemplate{background}{% - \parbox[c][\paperheight]{\paperwidth}{% - \vfill \hfill - \pgfimage[width=1.0\paperwidth,height=1.0\paperheight]{#1} - \hfill \vfill -}}} - -% For non-wide pictures, set the width so that the height scales -% appropriately. -\newcommand\BackgroundPictureWidth[1]{% - \setbeamertemplate{background}{% - \parbox[c][\paperheight]{\paperwidth}{% - \vfill \hfill - \pgfimage[width=1.0\paperwidth]{#1} - \hfill \vfill -}}} - -% For shorter pictures, set the height so that the width scales -% appropriately. -\newcommand\BackgroundPictureHeight[1]{% - \setbeamertemplate{background}{% - \parbox[c][\paperheight]{\paperwidth}{% - \vfill \hfill - \pgfimage[height=1.0\paperheight]{#1} - \hfill \vfill -}}} - -%%% This is from Fernando's setup. -% \usepackage{color} -% \definecolor{orange}{cmyk}{0,0.4,0.8,0.2} -% % Use and configure listings package for nicely formatted code -% \usepackage{listings} -% \lstset{ -% language=Python, -% basicstyle=\small\ttfamily, -% commentstyle=\ttfamily\color{blue}, -% stringstyle=\ttfamily\color{orange}, -% showstringspaces=false, -% breaklines=true, -% postbreak = \space\dots -% } - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Title page -\title[IPython notebooks]{Introductory Scientific Computing with -Python} -\subtitle{IPython notebooks} - -\author{FOSSEE} - -\institute[FOSSEE -- IITB] {Department of Aerospace Engineering\\IIT Bombay} -\date[] { -Mumbai, India -} -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -%\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} -%\logo{\pgfuseimage{iitmlogo}} - - -%% Delete this, if you do not want the table of contents to pop up at -%% the beginning of each subsection: -\AtBeginSubsection[] -{ - \begin{frame} - \frametitle{Outline} - \tableofcontents[currentsection,currentsubsection] - \end{frame} -} - -\AtBeginSection[] -{ - \begin{frame} - \frametitle{Outline} - \tableofcontents[currentsection,currentsubsection] - \end{frame} -} - -% If you wish to uncover everything in a step-wise fashion, uncomment -% the following command: -%\beamerdefaultoverlayspecification{<+->} - -%\includeonlyframes{current,current1,current2,current3,current4,current5,current6} - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% DOCUMENT STARTS -\begin{document} - -\begin{frame} - \maketitle -\end{frame} - -\begin{frame}[fragile] - \frametitle{Introduction} - \begin{itemize} - \item Have used the IPython console so far - \begin{itemize} - \item Terminal: \typ{ipython} - \item GUI: \typ{qtconsole} - \end{itemize} - \item Powerful and convenient - \item Must be installed as a package - \end{itemize} -\end{frame} - -\begin{frame} - \frametitle{IPython notebook} - \begin{itemize} - \item Create and share documents containing - \begin{itemize} - \item live code - \item equations - \item visualizations - \item interactive widgets - \item explanatory text - \end{itemize} - \item A web application - \end{itemize} -\end{frame} - - -\BackgroundPicture{data/notebook/jupyter-interface} -\begin{frame}[plain] -\end{frame} -\BackgroundPicture{data/intro/blank} - -\begin{frame} - \frametitle{Jupyter} - \begin{itemize} - \item Open source, interactive data science and computing - \item Brings IPython-like features to other languages - \item Console - \item Notebooks - \item Other tools: jupyterhub, nbviewer, etc. - \end{itemize} -\end{frame} - -\BackgroundPictureWidth{data/notebook/jupyter_logo} -\begin{frame}[plain] -\end{frame} -\BackgroundPicture{data/intro/blank} - -\begin{frame} - \frametitle{IPython and Jupyter} - \begin{itemize} - \item IPython provides Python specific functionality - \item Python \textbf{kernel} - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Getting started: IPython} -Console - \begin{lstlisting} -$ ipython -\end{lstlisting} %$ -\vspace*{0.25in} -The notebook: - \begin{lstlisting} -$ ipython notebook -\end{lstlisting} %$ -\end{frame} - -\begin{frame}[fragile] - \frametitle{Getting started: Jupyter} - \begin{lstlisting} -$ jupyter console -\end{lstlisting} %$ -\vspace*{0.25in} -The notebook: - \begin{lstlisting} -$ jupyter notebook -\end{lstlisting} %$ -\end{frame} - -\begin{frame}[fragile] - \frametitle{IPython notebooks and Canopy} - \begin{itemize} - \item Can directly open \typ{*.ipynb} files from Canopy - \item Or create a new notebook using the File menu - \end{itemize} -\end{frame} - - -\begin{frame}[fragile] - \frametitle{Pylab mode} - \begin{lstlisting} -In []: %pylab - \end{lstlisting} %$ -Or: - \begin{lstlisting} -In []: %matplotlib - \end{lstlisting} %$ -\end{frame} - -\begin{frame}[fragile] - \frametitle{Using matplotlib} - \small - \begin{lstlisting} -In []: %matplotlib -In []: from matplotlib import pyplot as plt -In []: from numpy import linspace, sin -In []: x = linspace(0, 2*pi) -In []: plt.plot(x, sin(x)) - \end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{What is the difference?} - \begin{itemize} - \item \typ{\%pylab}: imports pylab - \item \typ{\%matplotlib}: just sets up the plotting, no imports - \item Using explicit imports is cleaner - \item \typ{pyplot} provides the useful functionality - \item Could also just import \typ{pylab} - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Using the IPython notebook} - - \begin{itemize} - \item Start the notebook - \item Try: - \typ{jupyter notebook} - \item If that doesn't work use \typ{ipython notebook} - \item Create a new Python 2 or Python 3 notebook - \end{itemize} -\end{frame} - -\begin{frame} - \frametitle{Basic Notebook Demo} - \begin{center} - Open the \typ{sample.ipynb} file provided - \end{center} -\end{frame} - -\begin{frame} - \frametitle{Exercise} - \begin{itemize} - \item Start the notebook - \item Create a new notebook - \item Try the interface tour (Help->Interface tour) - \item Add some markdown text, an image, and a simple equation - \item Write some simple code in multiple cells - \item Add code from \typ{four_plot.py} and run it - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{More resources} - \begin{itemize} - \item \href{https://daringfireball.net/projects/markdown/syntax}{Markdown syntax} - \item \href{http://ipython.org}{IPython website} - \item \href{http://ipython.org/documentation.html}{IPython documentation} - \item \href{http://jupyter.org}{Jupyter website} - \item \href{https://jupyter.readthedocs.io/en/latest/index.html}{Jupyter documentation} - \item - \href{https://www.dataquest.io/blog/jupyter-notebook-tips-tricks-shortcuts/}{Jupyter - notebook tips and tricks} - \item \href{https://nbviewer.jupyter.org}{Example notebooks} - \item \href{http://tbc-python.fossee.in}{Python textbook companions} - \end{itemize} -\end{frame} - - -\end{document} diff --git a/scipy/basic/numpy.tex b/scipy/basic/numpy.tex deleted file mode 100644 index 0f5e17c..0000000 --- a/scipy/basic/numpy.tex +++ /dev/null @@ -1,551 +0,0 @@ -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%Tutorial slides on Python. -% -% Author: FOSSEE -% Copyright (c) 2009, FOSSEE, IIT Bombay -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -\documentclass[14pt,compress]{beamer} -%\documentclass[draft]{beamer} -%\documentclass[compress,handout]{beamer} -%\usepackage{pgfpages} -%\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm] - -% Modified from: generic-ornate-15min-45min.de.tex -\mode -{ - \usetheme{Warsaw} - \useoutertheme{infolines} - \setbeamercovered{transparent} -} - -\usepackage[english]{babel} -\usepackage[latin1]{inputenc} -%\usepackage{times} -\usepackage[T1]{fontenc} - -% Taken from Fernando's slides. -\usepackage{ae,aecompl} -\usepackage{mathpazo,courier,euler} -\usepackage[scaled=.95]{helvet} - -\definecolor{darkgreen}{rgb}{0,0.5,0} - -\usepackage{listings} -\lstset{language=Python, - basicstyle=\ttfamily\bfseries, - commentstyle=\color{red}\itshape, - stringstyle=\color{darkgreen}, - showstringspaces=false, - keywordstyle=\color{blue}\bfseries} - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Macros -\setbeamercolor{emphbar}{bg=blue!20, fg=black} -\newcommand{\emphbar}[1] -{\begin{beamercolorbox}[rounded=true]{emphbar} - {#1} - \end{beamercolorbox} -} -\newcounter{time} -\setcounter{time}{0} -\newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}} - -\newcommand{\typ}[1]{\lstinline{#1}} - -\newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}} } - -\newcommand{\num}{\texttt{numpy}} - -%%% This is from Fernando's setup. -% \usepackage{color} -% \definecolor{orange}{cmyk}{0,0.4,0.8,0.2} -% % Use and configure listings package for nicely formatted code -% \usepackage{listings} -% \lstset{ -% language=Python, -% basicstyle=\small\ttfamily, -% commentstyle=\ttfamily\color{blue}, -% stringstyle=\ttfamily\color{orange}, -% showstringspaces=false, -% breaklines=true, -% postbreak = \space\dots -% } - - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Title page -\title[NumPy arrays]{Introductory Scientific Computing with -Python} -\subtitle{NumPy arrays} - -\author[FOSSEE] {FOSSEE} - -\institute[FOSSEE -- IITB] {Department of Aerospace Engineering\\IIT Bombay} -\date[] {Mumbai, India} - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -%\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} -%\logo{\pgfuseimage{iitmlogo}} - - -%% Delete this, if you do not want the table of contents to pop up at -%% the beginning of each subsection: -\AtBeginSubsection[] -{ - \begin{frame} - \frametitle{Outline} - \tableofcontents[currentsection,currentsubsection] - \end{frame} -} - -\AtBeginSection[] -{ - \begin{frame} - \frametitle{Outline} - \tableofcontents[currentsection,currentsubsection] - \end{frame} -} - -% If you wish to uncover everything in a step-wise fashion, uncomment -% the following command: -%\beamerdefaultoverlayspecification{<+->} - -%\includeonlyframes{current,current1,current2,current3,current4,current5,current6} - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% DOCUMENT STARTS -\begin{document} - -\begin{frame} - \titlepage -\end{frame} - -\begin{frame} - \frametitle{Outline} - \tableofcontents - % You might wish to add the option [pausesections] -\end{frame} - - -\section{\num\ arrays} - -\begin{frame}[fragile] - \frametitle{The \num\ module} - \begin{itemize} - \item Efficient, powerful array type - \item Abstracts out standard operations on arrays - \item Convenience functions - \item \typ{ipython --pylab} imports part of numpy - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Without Pylab} -\begin{lstlisting} -In []: from numpy import * -In []: x = linspace(0, 1) -\end{lstlisting} - Note that we had done this ``import'' earlier! -\begin{lstlisting} -# Can also do this: -In []: import numpy -In []: x = numpy.linspace(0, 1) -# or -In []: import numpy as np -In []: x = np.linspace(0, 1) -\end{lstlisting} - Note the use of \typ{numpy.linspace} -\end{frame} - -\begin{frame} - \frametitle{\num\ arrays} - \begin{itemize} - \item Fixed size (\typ{arr.size}) - \item Same type (\typ{arr.dtype}) - \item Arbitrary dimensionality: \typ{arr.shape} - \item \typ{shape}: extent (size) along each dimension - \item \typ{arr.itemsize}: number of bytes per element - \item \alert{Note:} \typ{shape} can change so long as the \typ{size} - is constant - \item Indices start from 0 - \item Negative indices work like lists - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{\num\ arrays} -\begin{lstlisting} -In []: a = array([1,2,3,4]) -In []: b = array([2,3,4,5]) - -In []: print(a[0], a[-1]) -(1, 4) - -In []: a[0] = -1 -In []: a[0] = 1 -\end{lstlisting} -Operations are elementwise -\end{frame} - -\begin{frame}[fragile] - \frametitle{Simple operations} -\begin{lstlisting} -In []: a + b -Out[]: array([3, 5, 7, 9]) -In []: a*b -Out[]: array([2, 6, 12, 20]) -In []: a/b -Out[]: array([0, 0, 0, 0]) -\end{lstlisting} - \begin{itemize} - \item Operations are \alert{element-wise} - \item Types matter (only on Python 2.x) - \end{itemize} - \inctime{10} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Data type matters} - Try again with this: -\begin{lstlisting} -In []: a = array([1.,2,3,4]) -In []: a/b -\end{lstlisting} - This can happen on Python 2.x, so beware -\end{frame} - -\begin{frame}[fragile] - \frametitle{Examples} -\noindent \typ{pi} and \typ{e} are defined. -\begin{lstlisting} -In []: x = linspace(0.0, 10.0, 200) -In []: x *= 2*pi/10 -# apply functions to array. -In []: y = sin(x) -In []: y = cos(x) -In []: x[0] = -1 -In []: print(x[0], x[-1]) -(-1.0, 10.0) -\end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{\typ{size, shape, rank} etc.} -\vspace*{-8pt} -\begin{lstlisting} -In []: x = array([1., 2, 3, 4]) -In []: size(x) -Out[]: 4 -In []: x.dtype -dtype('float64') -In []: x.shape -Out[] (4,) -In []: rank(x) -Out[]: 1 -In []: x.itemsize -Out[]: 8 -\end{lstlisting} -\end{frame} - - -\begin{frame}[fragile] - \frametitle{Multi-dimensional arrays} -\begin{lstlisting} -In []: a = array([[ 0, 1, 2, 3], - ...: [10,11,12,13]]) -In []: a.shape # (rows, columns) -Out[]: (2, 4) - -In []: a[1,3] -Out[]: 13 - -In []: a[1,3] = -1 -In []: a[1] # The second row -array([10,11,12,-1]) -In []: a[1] = 0 # Entire row to zero. -\end{lstlisting} -\inctime{10} -\end{frame} - -\subsection{Slicing arrays} - -\begin{frame}[plain,fragile] - \frametitle{Slicing arrays} - \vspace*{-0.2in} -\begin{lstlisting} -In []: a = array([[1,2,3], [4,5,6], - ...: [7,8,9]]) -In []: a[0,1:3] -\end{lstlisting} - \pause - \vspace*{-0.1in} -\begin{lstlisting} -Out[]: array([2, 3]) - -In []: a[1:,1:] -\end{lstlisting} - \pause - \vspace*{-0.1in} -\begin{lstlisting} -Out[]: array([[5, 6], - [8, 9]]) - -In []: a[:,2] -\end{lstlisting} - \pause - \vspace*{-0.1in} -\begin{lstlisting} -Out[]: array([3, 6, 9]) -\end{lstlisting} -\end{frame} - -\begin{frame}[plain,fragile] - \frametitle{Slicing arrays ...} - \vspace*{-0.2in} -\begin{lstlisting} -In []: a = array([[1,2,3], [4,5,6], - ...: [7,8,9]]) - -In []: a[0::2,0::2] # Striding... -\end{lstlisting} - \pause - \vspace*{-0.1in} -\begin{lstlisting} -Out[]: array([[1, 3], - [7, 9]]) -# Slices refer to the same memory! -\end{lstlisting} -\end{frame} - -\subsection{Array creation} - -\begin{frame}[fragile] - \frametitle{Array creation functions} - \begin{itemize} - \item \typ{array(object)} - \item \typ{linspace(start, stop, num=50)} - \item \typ{ones(shape)} - \item \typ{zeros((d1,...,dn))} - \item \typ{empty((d1,...,dn))} - \item \typ{identity(n)} - \item \typ{ones\_like(x)}, \typ{zeros\_like(x)}, \typ{empty\_like(x)} - \end{itemize} - May pass an optional \typ{dtype=} keyword argument - - For more dtypes see: \typ{numpy.typeDict} - -\end{frame} - -\begin{frame}[fragile] - \frametitle{Creation examples} - \vspace*{-0.25in} -\begin{lstlisting} -In []: a = array([1,2,3], dtype=float) -In []: ones_like(a) -Out[]: array([ 1., 1., 1.]) - -In []: ones( (2, 3) ) -Out[]: array([[ 1., 1., 1.], - [ 1., 1., 1.]]) - -In []: identity(3) -Out[]: array([[ 1., 0., 0.], - [ 0., 1., 0.], - [ 0., 0., 1.]]) -\end{lstlisting} - \inctime{15} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Array math} - \begin{itemize} - \item Basic \alert{elementwise} math (given two arrays \typ{a, b}): - \begin{itemize} - \item \typ{a + b} $\rightarrow$ \typ{add(a, b)} - \item \typ{a - b}, $\rightarrow$ \typ{subtract(a, b)} - \item \typ{a * b}, $\rightarrow$ \typ{multiply(a, b)} - \item \typ{a / b}, $\rightarrow$ \typ{divide(a, b)} - \item \typ{a \% b}, $\rightarrow$ \typ{remainder(a, b)} - \item \typ{a ** b}, $\rightarrow$ \typ{power(a, b)} - \end{itemize} - \item Inplace operators: \typ{a += b}, or \typ{add(a, b, - a)} - \alert{What happens if \typ{a} is \typ{int} and \typ{b} is \typ{float?}} - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Array math} - \begin{itemize} - \item Logical operations: \typ{==, !=, <, >}, etc. - \item \typ{sin(x), arcsin(x), sinh(x)}, - \typ{exp(x), sqrt(x)} etc. - \item \typ{sum(x, axis=0), product(x, axis=0)} - \item \typ{dot(a, b)} - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Convenience functions: \typ{loadtxt}} - \begin{itemize} - \item \typ{loadtxt(file_name)}: loads a text file - \item \typ{loadtxt(file_name, unpack=True)}: loads a text file and - unpacks columns - \end{itemize} - \begin{lstlisting} -In []: x = loadtxt('pendulum.txt') -In []: x.shape -Out[]: (90, 2) - -In []: x, y = loadtxt('pendulum.txt', - ...: unpack=True) -In []: x.shape -Out[]: (90,) - \end{lstlisting} - - \inctime{10} -\end{frame} - - -\begin{frame}[fragile] - \frametitle{Advanced} - \begin{itemize} - \item Only scratched the surface of \num - \item \typ{reduce, outer} - \item Typecasting - \item More functions: \typ{take, choose, where}, \typ{compress, - concatenate} - \item Array broadcasting and \typ{None} - \item Record arrays - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Learn more} - \small - \begin{itemize} - \item \url{https://docs.scipy.org/doc/numpy-dev/user/quickstart.html} - \item \url{http://numpy.org} - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Recap} - \begin{itemize} - \item Basic concepts: creation, access, operations - \item 1D, multi-dimensional - \item Slicing - \item Array creation, dtypes - \item Math - \item \typ{loadtxt} - \end{itemize} - \inctime{5} -\end{frame} - -\subsection{Example: plotting data from file} - -\begin{frame}[fragile] -\frametitle{Example: plotting data from file} -\alert{Data is usually present in a file!} \\ -Lets look at the \typ{pendulum.txt} file. -\begin{lstlisting} -In []: cat pendulum.txt -1.0000e-01 6.9004e-01 -1.1000e-01 6.9497e-01 -1.2000e-01 7.4252e-01 -1.3000e-01 7.5360e-01 -\end{lstlisting} -\ldots -\end{frame} - -\begin{frame}[fragile] -\frametitle{Reading \typ{pendulum.txt}} -\begin{itemize} - \item File contains L vs.\ T values - \item First Column - L values - \item Second Column - T values - \item Let us generate a plot from the data file -\end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Gotcha and an aside} - Ensure you are in the same directory as \typ{pendulum.txt}\\ - if not, do the following on IPython: - \begin{lstlisting} -In []: %cd directory_containing_file -# Check if pendulum.txt is there. -In []: ls -# Also try -In []: !ls - \end{lstlisting} - - \alert{Note:} \typ{\%cd} is an IPython magic command. For more information - do: - \begin{lstlisting} -In []: ? -In []: %cd? - \end{lstlisting} -\end{frame} - - -\begin{frame}[fragile] - \frametitle{Exercise} - \begin{itemize} - \item Plot L versus T square with dots - \item No line connecting points - \end{itemize} - \inctime{10} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Solution} -\begin{lstlisting} -In []: L, t = loadtxt('pendulum.txt', - ....: unpack=True) -In []: plot(L, t*t, '.') -\end{lstlisting} -or -\begin{lstlisting} -In []: x = loadtxt('pendulum.txt') -In []: L, t = x[:,0], x[:,1] -In []: plot(L, t*t, '.') -\end{lstlisting} - -\end{frame} - - -\begin{frame}[fragile] -\begin{figure} -\includegraphics[width=3.5in]{data/L-Tsq.png} -\end{figure} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Odds and ends} -\begin{lstlisting} -In []: mean(L) -Out[]: 0.54499999999999993 - -In []: std(L) -Out[]: 0.25979158313283879 -\end{lstlisting} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Summary} -\begin{itemize} -\item Introduction to \num\ arrays -\item Slicing arrays -\item Multi-dimensional arrays -\item Array operations -\item Creating arrays -\item Loading data from file -\end{itemize} - -\inctime{5} -\end{frame} - -\end{document} diff --git a/scipy/basic/prelims.tex b/scipy/basic/prelims.tex deleted file mode 100644 index e613082..0000000 --- a/scipy/basic/prelims.tex +++ /dev/null @@ -1,265 +0,0 @@ -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%Tutorial slides on Python. -% -% Author: FOSSEE -% Copyright (c) 2009-2016, FOSSEE, IIT Bombay -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -\documentclass[14pt,compress]{beamer} - -% Modified from: generic-ornate-15min-45min.de.tex -\mode -{ - \usetheme{Warsaw} - \useoutertheme{infolines} - \setbeamercovered{transparent} -} - -% Remove navigation symbols. -\setbeamertemplate{navigation symbols}{} - -\usepackage[english]{babel} -\usepackage[latin1]{inputenc} -%\usepackage{times} -\usepackage[T1]{fontenc} - -% Taken from Fernando's slides. -\usepackage{ae,aecompl} -\usepackage{mathpazo,courier,euler} -\usepackage[scaled=.95]{helvet} - -\definecolor{darkgreen}{rgb}{0,0.5,0} - -\usepackage{listings} -\lstset{language=Python, - basicstyle=\ttfamily\bfseries, - commentstyle=\color{red}\itshape, - stringstyle=\color{darkgreen}, - showstringspaces=false, - keywordstyle=\color{blue}\bfseries} - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Macros -\setbeamercolor{emphbar}{bg=blue!20, fg=black} -\newcommand{\emphbar}[1] -{\begin{beamercolorbox}[rounded=true]{emphbar} - {#1} - \end{beamercolorbox} -} -\newcounter{time} -\setcounter{time}{0} -\newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}} - -\newcommand{\typ}[1]{\lstinline{#1}} - -\newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}} } - -\newcommand\BackgroundPicture[1]{% - \setbeamertemplate{background}{% - \parbox[c][\paperheight]{\paperwidth}{% - \vfill \hfill - \pgfimage[width=1.0\paperwidth,height=1.0\paperheight]{#1} - \hfill \vfill -}}} - -% For non-wide pictures, set the width so that the height scales -% appropriately. -\newcommand\BackgroundPictureWidth[1]{% - \setbeamertemplate{background}{% - \parbox[c][\paperheight]{\paperwidth}{% - \vfill \hfill - \pgfimage[width=1.0\paperwidth]{#1} - \hfill \vfill -}}} - -% For shorter pictures, set the height so that the width scales -% appropriately. -\newcommand\BackgroundPictureHeight[1]{% - \setbeamertemplate{background}{% - \parbox[c][\paperheight]{\paperwidth}{% - \vfill \hfill - \pgfimage[height=1.0\paperheight]{#1} - \hfill \vfill -}}} - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Title page -\title[Preliminaries]{Introductory Scientific Computing with -Python} -\subtitle{Some preliminaries} - -\author[FOSSEE] {FOSSEE} - -\institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} -\date[] {Mumbai, India -} -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -%\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} -%\logo{\pgfuseimage{iitmlogo}} - - -%% Delete this, if you do not want the table of contents to pop up at -%% the beginning of each subsection: -\AtBeginSubsection[] -{ - \begin{frame} - \frametitle{Outline} - \tableofcontents[currentsection,currentsubsection] - \end{frame} -} - -\AtBeginSection[] -{ - \begin{frame} - \frametitle{Outline} - \tableofcontents[currentsection,currentsubsection] - \end{frame} -} - -% If you wish to uncover everything in a step-wise fashion, uncomment -% the following command: -%\beamerdefaultoverlayspecification{<+->} - -%%\includeonlyframes{current,current1,current2,current3,current4,current5,current6} - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% DOCUMENT STARTS -\begin{document} - -\begin{frame} - \maketitle -\end{frame} - -\begin{frame}[plain] - \frametitle{What is Python?} - \large - \begin{itemize} - \item Python: the programming language - - \vspace*{0.5in} - \item \typ{python}: the interpreter - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{The interpreter} - \small - \begin{lstlisting} -$ python -Python 2.7.9 (default, Feb 10 2015, 03:29:10) -Type "help", "copyright", "credits" or "license" for more information. ->>> -\end{lstlisting} %$ -\end{frame} - -\begin{frame}[fragile] - \frametitle{The interpreter} - \small -\begin{lstlisting} -$ python hello.py -hello world -$ -\end{lstlisting} -\end{frame} - -\begin{frame}[plain] - \frametitle{Python standard library} - \begin{itemize} - \item Built-in - \item ``Batteries included'' - \item Lot of important functionality - \end{itemize} -\end{frame} - -\begin{frame}[plain] - \frametitle{Python packages} - \begin{itemize} - \item Many other packages available - \item \typ{ipython} - \item \typ{numpy} - \item \typ{scipy} - \item \typ{matplotlib} - \item \typ{PyQt} - \item \typ{django} - \item \typ{mayavi} - \item About 1 lakh packages! - \end{itemize} -\end{frame} - -\begin{frame}[plain] - \frametitle{Official Python} - \begin{center} - \url{www.python.org/downloads} - - \includegraphics[height=2.75in]{data/intro/python_download} - \end{center} -\end{frame} - -\begin{frame} - \frametitle{Python distributions} - \begin{itemize} - \item GNU/Linux: easy to install - \item Usually many packages are easy to install - \item Example: \typ{apt-get}, \typ{yum} etc. - \item Not so easy on OS X and Windows - \end{itemize} -\end{frame} - -\begin{frame} - \frametitle{Python distribution: Canopy} - \begin{itemize} - \item \url{www.enthought.com/products/canopy} - \item Easy to use - \item Simple IDE - \item Cross platform: Linux, OSX, Windows - \item Free installer - \item Many packages - \end{itemize} -\end{frame} - -\begin{frame} - \frametitle{Python distribution: Anaconda} - \begin{itemize} - \item \url{www.continuum.io/downloads} - \item Cross platform: Linux, OSX, Windows - \item Free installer - \item Many packages - \end{itemize} -\end{frame} - -\begin{frame}[plain] - \frametitle{For this course} - \begin{itemize} - \item Using Canopy - \item Simple UI - \item Easy for beginners - \vspace*{0.25in} - \item Advanced users can use anything they want! - \end{itemize} -\end{frame} - -\begin{frame}[plain] - \frametitle{Setup} - \begin{itemize} - \item Download for your platform - \item Install it - \item Start Canopy - \end{itemize} -\end{frame} - -\begin{frame}[plain] - \frametitle{Summary} - \begin{itemize} - \item Python: the programming language - \item \typ{python}: the interpreter - \item Python standard library - \item Other Python packages - \item Python distributions - \end{itemize} -\end{frame} - - - - -\end{document} diff --git a/scipy/basic/saving_scripts.tex b/scipy/basic/saving_scripts.tex deleted file mode 100644 index 356f10f..0000000 --- a/scipy/basic/saving_scripts.tex +++ /dev/null @@ -1,422 +0,0 @@ -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%Tutorial slides on Python. -% -% Author: FOSSEE -% Copyright (c) 2009-2016, FOSSEE, IIT Bombay -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -\documentclass[14pt,compress]{beamer} - -% Modified from: generic-ornate-15min-45min.de.tex -\mode -{ - \usetheme{Warsaw} - \useoutertheme{infolines} - \setbeamercovered{transparent} -} - -\usepackage[english]{babel} -\usepackage[latin1]{inputenc} -%\usepackage{times} -\usepackage[T1]{fontenc} - -% Taken from Fernando's slides. -\usepackage{ae,aecompl} -\usepackage{mathpazo,courier,euler} -\usepackage[scaled=.95]{helvet} - -\definecolor{darkgreen}{rgb}{0,0.5,0} - -\usepackage{listings} -\lstset{language=Python, - basicstyle=\ttfamily\bfseries, - commentstyle=\color{red}\itshape, - stringstyle=\color{darkgreen}, - showstringspaces=false, - keywordstyle=\color{blue}\bfseries} - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Macros -\setbeamercolor{emphbar}{bg=blue!20, fg=black} -\newcommand{\emphbar}[1] -{\begin{beamercolorbox}[rounded=true]{emphbar} - {#1} - \end{beamercolorbox} -} -\newcounter{time} -\setcounter{time}{0} -\newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}} - -\newcommand{\typ}[1]{\lstinline{#1}} - -\newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}} } - -%%% This is from Fernando's setup. -% \usepackage{color} -% \definecolor{orange}{cmyk}{0,0.4,0.8,0.2} -% % Use and configure listings package for nicely formatted code -% \usepackage{listings} -% \lstset{ -% language=Python, -% basicstyle=\small\ttfamily, -% commentstyle=\ttfamily\color{blue}, -% stringstyle=\ttfamily\color{orange}, -% showstringspaces=false, -% breaklines=true, -% postbreak = \space\dots -% } - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Title page -\title[Saving scripts]{Introductory Scientific Computing with -Python} -\subtitle{Saving scripts} - -\author[FOSSEE] {FOSSEE} - -\institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} -\date[] {Mumbai, India -} -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -%\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} -%\logo{\pgfuseimage{iitmlogo}} - - -%% Delete this, if you do not want the table of contents to pop up at -%% the beginning of each subsection: -\AtBeginSubsection[] -{ - \begin{frame} - \frametitle{Outline} - \tableofcontents[currentsection,currentsubsection] - \end{frame} -} - -\AtBeginSection[] -{ - \begin{frame} - \frametitle{Outline} - \tableofcontents[currentsection,currentsubsection] - \end{frame} -} - -% If you wish to uncover everything in a step-wise fashion, uncomment -% the following command: -%\beamerdefaultoverlayspecification{<+->} - -%%\includeonlyframes{current,current1,current2,current3,current4,current5,current6} - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% DOCUMENT STARTS -\begin{document} - -\begin{frame} - \maketitle -\end{frame} - -%% \begin{frame} -%% \frametitle{Outline} -%% \tableofcontents -%% % You might wish to add the option [pausesections] -%% \end{frame} - -\section{Exercise} - -\begin{frame}[plain,fragile] -\frametitle{Review Problem} -\begin{enumerate} -\item Plot $x, -x, \sin(x), x \sin(x)$ in range $-5\pi$ to $5\pi$ -\item Add a legend -\item Annotate the origin -\item Set axes limits to the range of x -\end{enumerate} -\vspace*{-0.15in} -\begin{center} - \includegraphics[height=2.6in, interpolate=true]{data/four_plot} -\end{center} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Review Problem \ldots} -\alert{Plotting \ldots} -\begin{lstlisting} -In []: x = linspace(-5*pi, 5*pi, 500) -In []: plot(x, x, 'b') -In []: plot(x, -x, 'b') -In []: plot(x, sin(x), 'g', linewidth=2) -In []: plot(x, x*sin(x), 'r', - linewidth=3) -\end{lstlisting} -$\vdots$ -\end{frame} - -\begin{frame}[fragile] -\frametitle{Review Problem \ldots} -\alert{Legend \& Annotation\ldots} -\begin{lstlisting} -In []: legend(['x', '-x', 'sin(x)', - 'xsin(x)']) -In []: annotate('origin', xy = (0, 0)) -\end{lstlisting} -\alert{Setting Axes limits\ldots} -\begin{lstlisting} -In []: xlim(-5*pi, 5*pi) -In []: ylim(-5*pi, 5*pi) -\end{lstlisting} -\inctime{5} -\end{frame} - -\section{Scripts -- Saving \& Running} -\begin{frame}[fragile] -\frametitle{Command History} -Use the \typ{\%hist} \alert{magic} command of IPython -\typ{In []: \%hist}\\ -This displays the ``Command History'' -\begin{block}{Careful about errors!} - \kwrd{\%hist} will contain the errors as well.\\ -\end{block} -\pause -\begin{block}{Magic Commands?} - Magic commands are commands provided by IPython to make our life easier. -\end{block} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Saving commands into script} -Use the \typ{\%save} \alert{magic} command of IPython -\begin{block}{} -\typ{\%save script_name line_numbers} -\end{block} -Line numbers specified individually separated by spaces or as a range separated by a dash.\\ -\begin{block}{} -\typ{\%save four_plot.py} \alert{\typ{ 16 18-27}} \\ -\end{block} -Saves from history the commands entered on line numbers \alert{16, 18, 19, 20, \ldots 27} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Saving commands into a script} - \begin{itemize} - \item Save lines relevant for the review problem - \item Hint: example\\ \typ{\%save four_plot.py 16 18-27} - \item Choose the lines carefully - \item Edit \typ{four_plot.py} on Canopy - \item Make sure all the lines are correct - \item Save the script - \end{itemize} -\inctime{5} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Creating scripts: alternative} - \begin{itemize} - \item Create a new file on Canopy - \item Copy commands for assignment with your mouse - \item Save the script to \typ{four_plot.py} - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Where is the script saved?} - \begin{itemize} - \item \typ{\%save} saves into the current directory - \vspace*{0.5in} - \item Use \typ{\%pwd} to print the current directory - \item Use \typ{\%cd} to change the directory - - \vspace*{0.5in} - \item Question: how do you find out more about \typ{\%cd}? - \end{itemize} -\end{frame} - - -\begin{frame}[fragile] -\frametitle{Python Scripts\ldots} -Now, \typ{four_plot.py} is called a Python Script. - \begin{itemize} - \item run the script in IPython using \typ{\%run four_plot.py}\\ - \end{itemize} -\pause -\alert{\typ{NameError: name 'linspace' is not defined}} -\begin{block}{} -To avoid this, run using \alert{\typ{\%run -i four_plot.py}}\\ -\end{block} -\pause -Where is the plot? -\begin{lstlisting} -In []: show() -\end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Exercise} - \begin{itemize} - \item Add the \typ{show()} command to \typ{four_plot.py} - \item Save the file - \item Test that it works - \end{itemize} - \inctime{5} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Result graph} - \begin{center} - \includegraphics[height=3in, interpolate=true]{data/four_plot} - \end{center} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Running with Python} - \begin{itemize} - \item Start a new Canopy terminal - \item Change directory to where you saved \typ{four_plot.py} - \item Run the script as: - \end{itemize} -\begin{lstlisting} - $ python four_plot.py -\end{lstlisting} - \pause - Do you see: - \begin{small} -\begin{lstlisting} -NameError: name 'linspace' is not defined -\end{lstlisting} - \end{small} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Imports} - \begin{itemize} - \item \typ{ipython --pylab} does magic - \item Import libraries using \typ{import} - \end{itemize} -\begin{lstlisting} -In []: from pylab import linspace -\end{lstlisting} -\inctime{5} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Exercise} - \begin{itemize} - \item Add \typ{from pylab import linspace} to top of \typ{four_plot.py} - \item Test that it works - \end{itemize} -\begin{lstlisting} -# four_plot.py -from pylab import linspace # <-- added -x = linspace(-5*pi, 5*pi, 500) -plot(x, x, 'b') -... -\end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Try again} - \begin{itemize} - \item On Canopy terminal - \item Run the script as: - \end{itemize} -\begin{lstlisting} - $ python four_plot.py -\end{lstlisting} - \pause - \vspace*{0.15in} - Do you see: - \begin{small} -\begin{lstlisting} -NameError: name 'plot' is not defined -\end{lstlisting} - \end{small} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Exercise} - \begin{itemize} - \item Change line 1 to \typ{from pylab import *} - \item Test that it works - \end{itemize} -\begin{lstlisting} -# four_plot.py -from pylab import * # <-- added -x = linspace(-5*pi, 5*pi, 500) -plot(x, x, 'b') -... -\end{lstlisting} -\inctime{5} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Solution} - \small -\begin{lstlisting} -from pylab import * -x = linspace(-5*pi, 5*pi, 500) -plot(x, x, 'b') -plot(x, -x, 'b') -plot(x, sin(x), 'g', linewidth=2) -plot(x, x*sin(x), 'r', linewidth=3) -legend(['x', '-x', 'sin(x)', 'xsin(x)']) -annotate('origin', xy = (0, 0)) -xlim(-5*pi, 5*pi) -ylim(-5*pi, 5*pi) -show() -\end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Note on script file names} - \begin{itemize} - \item Should start with a letter - \item Can use \typ{_} (underscore) and numbers - \item No \typ{.} allowed - \item No spaces or special characters - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Test} - \begin{itemize} - \item \typ{1_script.py} - \item \typ{script_1.py} - \item \typ{one11.py} - \item \typ{one script.py} - \item \typ{one,script;xxx.py} - \item \typ{one.two.py} - \end{itemize} -\end{frame} - -\begin{frame} - \frametitle{Using Canopy} - \begin{itemize} - \item Much easier - \item Write code in the editor - \item Embedded IPython - \item Save (Ctrl-S or Cmd-S) - \item Run selection: Ctrl-Shift-R (Cmd-Shift-R on OS X) - \item Run code: Ctrl-R (Cmd-R on OS X) - \item Change directory with menu (\typ{\%cd}) - \end{itemize} - \inctime{5} -\end{frame} - - -\begin{frame}[fragile] - \frametitle{What did we learn?} - \begin{itemize} - \item Starting up IPython - \item Creating simple plots - \item Annotating: labels, legends, annotation - \item Changing the looks: color, linewidth - \item Accessing history, documentation - \item \kwrd{\%hist} - History of commands - \item Creating a Python script with \typ{\%save} - \item Running a script using \kwrd{\%run -i} - \item Importing functionality - \item Running a script with\ \typ{python script.py} - \end{itemize} -\end{frame} - - -\end{document} -- cgit