From 6bbafe089e0ba29c38c1c63da59633ab7b6db73e Mon Sep 17 00:00:00 2001 From: Prabhu Ramachandran Date: Thu, 19 Jan 2017 00:04:28 +0530 Subject: Reorganize files with better names. Add a little more content to the mlab slides. --- scipy/basic/Makefile | 12 +- scipy/basic/README.txt | 15 + scipy/basic/basic_scipy.tex | 509 +++++++++++++++++++++++++++++ scipy/basic/ipython_plotting.tex | 662 ++++++++++++++++++++++++++++++++++++++ scipy/basic/lists_arrays.tex | 554 ++++++++++++++++++++++++++++++++ scipy/basic/mlab.tex | 78 ++++- scipy/basic/more_numpy.tex | 668 +++++++++++++++++++++++++++++++++++++++ scipy/basic/numpy.tex | 550 ++++++++++++++++++++++++++++++++ scipy/basic/saving_scripts.tex | 422 +++++++++++++++++++++++++ scipy/basic/session1a.tex | 662 -------------------------------------- scipy/basic/session1b.tex | 422 ------------------------- scipy/basic/session2a.tex | 554 -------------------------------- scipy/basic/session2b.tex | 550 -------------------------------- scipy/basic/session3.tex | 668 --------------------------------------- scipy/basic/session4.tex | 509 ----------------------------- 15 files changed, 3463 insertions(+), 3372 deletions(-) create mode 100644 scipy/basic/basic_scipy.tex create mode 100644 scipy/basic/ipython_plotting.tex create mode 100644 scipy/basic/lists_arrays.tex create mode 100644 scipy/basic/more_numpy.tex create mode 100644 scipy/basic/numpy.tex create mode 100644 scipy/basic/saving_scripts.tex delete mode 100644 scipy/basic/session1a.tex delete mode 100644 scipy/basic/session1b.tex delete mode 100644 scipy/basic/session2a.tex delete mode 100644 scipy/basic/session2b.tex delete mode 100644 scipy/basic/session3.tex delete mode 100644 scipy/basic/session4.tex (limited to 'scipy') diff --git a/scipy/basic/Makefile b/scipy/basic/Makefile index bd43507..94a29fe 100644 --- a/scipy/basic/Makefile +++ b/scipy/basic/Makefile @@ -4,12 +4,12 @@ pdflatex $*.tex pdflatex $*.tex -SLIDES= intro.pdf prelims.pdf session1a.pdf \ - session1b.pdf \ - session2a.pdf \ - session2b.pdf \ - session3.pdf \ - session4.pdf \ +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 diff --git a/scipy/basic/README.txt b/scipy/basic/README.txt index 35d9c22..9495118 100644 --- a/scipy/basic/README.txt +++ b/scipy/basic/README.txt @@ -38,6 +38,7 @@ Basic Tutorial: introduce the Python language in the context of these common tasks. Here is a rough outline of what we propose to cover: + - Introduction and Preliminaries. - Introduction to IPython. - Creating basic plots with matplotlib. - NumPy array basics: 1D arrays. @@ -47,6 +48,20 @@ Basic Tutorial: - Using SciPy for Linear Algebra, FFT's, root finding and integrating ODEs. +Session wise slide breakup: + +1. intro.pdf +2. prelims.pdf +3. ipython_plotting.pdf +4. saving_scripts.pdf +5. lists_arrays.pdf +6. numpy.pdf +7. more_numpy.pdf +8. scipy.pdf +9. exercises.pdf +10. notebook.pdf +11. mlab.pdf + * Attendee Requirements: diff --git a/scipy/basic/basic_scipy.tex b/scipy/basic/basic_scipy.tex new file mode 100644 index 0000000..4c65d24 --- /dev/null +++ b/scipy/basic/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.github.com/ipython-doc} + \item \url{matplotlib.sf.net/contents.html} + \item \url{scipy.org/Tentative_NumPy_Tutorial} + \item \url{docs.scipy.org/doc/scipy/reference/tutorial} + \end{itemize} +\end{frame} + +\end{document} diff --git a/scipy/basic/ipython_plotting.tex b/scipy/basic/ipython_plotting.tex new file mode 100644 index 0000000..d0c5bf3 --- /dev/null +++ b/scipy/basic/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/lists_arrays.tex b/scipy/basic/lists_arrays.tex new file mode 100644 index 0000000..d5e936f --- /dev/null +++ b/scipy/basic/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[Interactive Plotting]{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 index 1d7fe45..bb2e4eb 100644 --- a/scipy/basic/mlab.tex +++ b/scipy/basic/mlab.tex @@ -80,7 +80,7 @@ % Title page \title[Basic SciPy and Mayavi]{Introductory Scientific Computing with Python} -\subtitle{Simple 3D plots with mlab} +\subtitle{Simple 3D plots with Mayavi's mlab} \author[Prabhu] {FOSSEE} @@ -126,6 +126,82 @@ Mumbai \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} + \frametitle{Installation} + \begin{itemize} + \item Suggest using a Python distribution + \item Canopy's package manager + \item \typ{conda} + \item \typ{edm} + \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}[plain,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,plain] + \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] diff --git a/scipy/basic/more_numpy.tex b/scipy/basic/more_numpy.tex new file mode 100644 index 0000000..c53cc41 --- /dev/null +++ b/scipy/basic/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/numpy.tex b/scipy/basic/numpy.tex new file mode 100644 index 0000000..6180ea2 --- /dev/null +++ b/scipy/basic/numpy.tex @@ -0,0 +1,550 @@ +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%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[Interactive Plotting]{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 + \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} +\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/saving_scripts.tex b/scipy/basic/saving_scripts.tex new file mode 100644 index 0000000..04bba5a --- /dev/null +++ b/scipy/basic/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[Interactive Plotting]{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/session1a.tex b/scipy/basic/session1a.tex deleted file mode 100644 index d0c5bf3..0000000 --- a/scipy/basic/session1a.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/session1b.tex b/scipy/basic/session1b.tex deleted file mode 100644 index 04bba5a..0000000 --- a/scipy/basic/session1b.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[Interactive Plotting]{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/session2a.tex b/scipy/basic/session2a.tex deleted file mode 100644 index d5e936f..0000000 --- a/scipy/basic/session2a.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[Interactive Plotting]{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/session2b.tex b/scipy/basic/session2b.tex deleted file mode 100644 index 6180ea2..0000000 --- a/scipy/basic/session2b.tex +++ /dev/null @@ -1,550 +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[Interactive Plotting]{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 - \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} -\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/session3.tex b/scipy/basic/session3.tex deleted file mode 100644 index c53cc41..0000000 --- a/scipy/basic/session3.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/session4.tex b/scipy/basic/session4.tex deleted file mode 100644 index 4c65d24..0000000 --- a/scipy/basic/session4.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.github.com/ipython-doc} - \item \url{matplotlib.sf.net/contents.html} - \item \url{scipy.org/Tentative_NumPy_Tutorial} - \item \url{docs.scipy.org/doc/scipy/reference/tutorial} - \end{itemize} -\end{frame} - -\end{document} -- cgit