summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--day1/scipy-quickref.tex231
-rw-r--r--day2/python-quickref.tex241
2 files changed, 472 insertions, 0 deletions
diff --git a/day1/scipy-quickref.tex b/day1/scipy-quickref.tex
new file mode 100644
index 0000000..618f2c9
--- /dev/null
+++ b/day1/scipy-quickref.tex
@@ -0,0 +1,231 @@
+\documentclass{article}
+\usepackage{graphicx}
+\usepackage[landscape]{geometry}
+\usepackage[pdftex]{color}
+\usepackage{url}
+\usepackage{multicol}
+\usepackage{amsmath}
+\usepackage{amsfonts}
+\newcommand{\ex}{\color{blue}}
+\pagestyle{empty}
+\advance\topmargin-.9in
+\advance\textheight2in
+\advance\textwidth3.0in
+\advance\oddsidemargin-1.45in
+\advance\evensidemargin-1.45in
+\parindent0pt
+\parskip2pt
+
+\newcommand{\hr}{\centerline{\rule{3.5in}{1pt}}}
+\newcommand{\skipin}{\hspace*{12pt}}
+
+\usepackage{color}
+\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}
+
+\begin{document}
+\begin{multicols*}{3}
+\begin{center}
+\textbf{Python for Scientific Computing}\\
+\textbf{Quick Reference}\\
+FOSSEE -- Dec 1, 2010\\
+\end{center}
+\vspace{-2ex}
+
+%*********************************************
+ \hr\textbf{Starting up}
+
+To start \lstinline|ipython| with \lstinline|pylab|:\\
+\lstinline| $ ipython -pylab| %$
+
+To exit: \lstinline|^D| (Ctrl-D)
+
+%*********************************************
+\hr\textbf{Plotting}
+
+Creating a linear array:\\
+{\ex \lstinline| x = linspace(0, 2*pi, 50)|}
+
+Plotting two variables:\\
+{\ex \lstinline| plot(x, sin(x))|}
+
+Plotting two lists of equal length x, y:\\
+{\ex \lstinline| plot(x, y)|}
+
+Plots with colors:\\
+{\ex \lstinline| plot(x, sin(x), 'b')|} gives a blue line
+
+Line style and markers:\\
+{\ex \lstinline| plot(x, sin(x), '--')|} gives a dashed line\\
+{\ex \lstinline|'.'|} -- a point marker, {\ex \lstinline|'o'|} -- a circle marker
+
+Labels:\\
+{\ex \lstinline| xlabel('x')|} and {\ex \lstinline|ylabel('sin(x)')|}
+
+Title (\lstinline|pylab| accepts \TeX~in any text expression):\\
+{\ex \lstinline| title(r'$\sigma$ vs. $\sin(\sigma)$')|}
+
+Legend:\\
+Use standard placement:\\
+{\ex \lstinline| legend('sin(x)', loc=center)|}\\
+Or explicitly specify location:\\
+{\ex \lstinline| legend(['sin(x)'], loc=(.8,.1))|}
+
+Annotate:\\
+{\ex \lstinline| legend('annotation string', xy=(1.5, 1))|}
+
+Saving figures:\\
+{\ex \lstinline| savefig('sinusoids.png')|}
+
+Miscellaneous:\\
+{\ex \lstinline| clf()|} to clear the plot area\\
+{\ex \lstinline| close()|} to close the figure
+
+%*********************************************
+\textbf{Other plots}
+
+Pie Charts:\\
+{\ex \lstinline| pie(science.values(), labels=science.keys())|},
+where {\ex \lstinline|science|} is a dictionary.
+
+%*********************************************
+\hr\textbf{Saving and Running scripts}
+
+{\ex \lstinline| %hist|} returns history of commands used. \\
+To save a set of lines, say 14-18, 20, 22, to \lstinline|sample.py| \\
+{\ex \lstinline| %save sample.py 14-18 20 22|}\\
+To run \lstinline|sample.py| \\
+{\ex \lstinline| %run -i sample.py|}
+
+%*********************************************
+ \hr\textbf{Reading from files}
+
+\lstinline|filename.txt| is a file with float data.
+
+Using a for loop: (reading line by line)
+\vspace{-8pt}
+\begin{lstlisting}
+for line in open('filename.txt'):
+ # some processing
+\end{lstlisting}
+\vspace{-8pt}
+Using loadtxt: (reading all the data at once)\\
+{\ex \lstinline|X = loadtxt('filename.txt')|} \\
+X is an array with all the data from \lstinline|filename.txt|\\
+{\ex \lstinline|X = loadtxt('filename.txt', delimiter=';')|} \\
+when ';' delmits the columns of data
+
+%*********************************************
+\hr\textbf{Statistical operations}
+
+{\ex \lstinline|mean|, \lstinline|median|, \lstinline|std|}
+
+%*********************************************
+\hr\textbf{Array Creation}
+
+{\ex \lstinline|C = array([[11,12,13], [21,22,23], [31,32,33]])|}\\
+{\ex \lstinline|C.shape|} shape--- rows \& cols\\
+{\ex \lstinline|C.dtype|} data type\\
+{\ex \lstinline|B = ones_like(C)|} array of ones; same shape, dtype as C\\
+\skipin similarly \lstinline|zeros_like, empty_like| \\
+{\ex \lstinline|A = ones((3,2))|} array of ones of shape (3,2)\\
+\skipin similarly \lstinline|zeros, empty|\\
+{\ex \lstinline|I = identity(3)|} identity matrix of size 3x3
+
+%*********************************************
+\hr\textbf{Accessing \& Changing elements}
+
+{\ex \lstinline|C[1, 2]|} gets third element of second row\\
+\skipin \textbf{Note:} Indexing starts from 0. \\
+{\ex \lstinline|C[1]|} gets the second row \\
+{\ex \lstinline|C[1,:]|} same as above (`:' implies all columns)\\
+{\ex \lstinline|C[:,1]|} gets the second column (`:' implies all rows)\\
+{\ex \lstinline|C[0:2,:]|} or {\ex \lstinline|C[:2,:]|} gets $1^{st}, 2^{nd}$ rows; all cols\\
+{\ex \lstinline|C[1:3,:]|} or {\ex \lstinline|C[1:,:]|} gets $2^{nd}, 3^{rd}$
+rows; all cols\\
+{\ex \lstinline|C[0:3:2,:]|} or {\ex \lstinline|C[::2,:]|} gets $1^{st}, 3^{rd}$
+rows; all cols
+
+%*********************************************
+\hr\textbf{Matrix Operations}
+
+For a Matrix A and B of equal shapes:\\
+{\ex \lstinline|A.T|} transpose\\
+{\ex \lstinline|sum(A)|} sum of all elements\\
+{\ex \lstinline|A+B|} addition\\
+{\ex \lstinline|A*B|} element wise product\\
+{\ex \lstinline|dot(A, B)|} Matrix multiplication\\
+{\ex \lstinline|inv(A)|} inverse, {\ex \lstinline|det(A)|} determinant\\
+{\ex \lstinline|eig(A)|} eigen values and vectors\\
+{\ex \lstinline|norm(A)|} norm\\
+{\ex \lstinline|svd(A)|} singular value decomposition
+
+%*********************************************
+\hr\textbf{Solving Linear Equations}
+
+{\ex \lstinline|A = array([[3,2,-1], [2,-2,4], [-1, 0.5, -1]])|}\\
+\skipin coefficient array\\
+{\ex \lstinline|b = array([1, -2, 0])|} constant array\\
+{\ex \lstinline|x = solve(A, b)|} the required solution
+
+Checking the solution:\\
+{\ex \lstinline|Ax = dot(A,x)|} matrix multiplication of A and x\\
+{\ex \lstinline|allclose(Ax, b)|} check the closeness of Ax, b
+
+%*********************************************
+\hr\textbf{Roots of Polynomials}
+
+{\ex \lstinline|coeffs = [1, 6, 13]|} coefficients in descending order\\
+{\ex \lstinline|roots(coeffs)|} returns complex roots of the polynomial
+
+%*********************************************
+\hr\textbf{Roots of non-linear equations}
+
+{\ex \lstinline|from scipy.optimize import fsolve|}\\
+\skipin \lstinline|fsolve| is not in \lstinline|pylab|\\
+\skipin we import from \lstinline|scipy.optimize| \\
+We wish to find the roots of $f(x)=sin(x)+cos(x)^2$
+\vspace{-8pt}
+\begin{lstlisting}
+def f(x):
+ return sin(x)+cos(x)**2
+\end{lstlisting}
+\vspace{-8pt}
+{\ex \lstinline|fsolve(f, 0)|} \\
+\skipin arguments are function name and initial estimate
+
+%*********************************************
+\hr\textbf{ODE}
+
+To solve the ODE below:\\
+$\frac{dy}{dt} = ky(L-y)$, L = 25000, k = 0.00003, y(0) = 250\\
+\vspace{-8pt}
+\begin{lstlisting}
+def f(y, t):
+ k, L = 0.00003, 25000
+ return k*y*(L-y)
+\end{lstlisting}
+\vspace{-8pt}
+{\ex \lstinline|t = linspace(0, 12, 60)|} time over which to solve ODE\\
+{\ex \lstinline|y0 = 250|} initial conditions\\
+{\ex \lstinline|from scipy.integrate import odeint|}\\
+{\ex \lstinline|y = odeint(f, y0, t)|}
+
+\hr\textbf{FFT}
+
+{\ex \lstinline|t = linspace(0, 2*pi, 500)|}\\
+{\ex \lstinline|y = sin(4*pi*t)|} a sinusoidal signal\\
+{\ex \lstinline|f = fft(y)|}\\
+{\ex \lstinline|freq = fftfreq(500, t[1] - t[0])|}\\
+{\ex \lstinline|plot(freq[:250], abs(f)[:250])|}\\
+
+\end{multicols*}
+
+\end{document}
diff --git a/day2/python-quickref.tex b/day2/python-quickref.tex
new file mode 100644
index 0000000..b464a5e
--- /dev/null
+++ b/day2/python-quickref.tex
@@ -0,0 +1,241 @@
+\documentclass{article}
+\usepackage{graphicx}
+\usepackage[landscape]{geometry}
+\usepackage[pdftex]{color}
+\usepackage{url}
+\usepackage{multicol}
+\usepackage{amsmath}
+\usepackage{amsfonts}
+\newcommand{\ex}{\color{blue}}
+\pagestyle{empty}
+\advance\topmargin-.9in
+\advance\textheight2in
+\advance\textwidth3.0in
+\advance\oddsidemargin-1.45in
+\advance\evensidemargin-1.45in
+\parindent0pt
+\parskip2pt
+
+\newcommand{\hr}{\centerline{\rule{3.5in}{1pt}}}
+\newcommand{\skipin}{\hspace*{12pt}}
+
+\usepackage{color}
+\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}
+
+\begin{document}
+\begin{multicols*}{3}
+\begin{center}
+\textbf{Python}\\
+\textbf{Quick Reference}\\
+FOSSEE -- Dec 1, 2010\\
+\end{center}
+\vspace{-2ex}
+
+%*********************************************
+ \hr\textbf{Basic Data types}
+
+Declaring an Integer:\\
+{\ex \lstinline| b=9999999999999999999 |}
+
+Declaring a float:\\
+{\ex \lstinline| p=3.141592 |}
+
+Declaring a Complex number:\\
+{\ex \lstinline| c = 3.2+4.6j |}
+
+%*********************************************
+\hr\textbf{Arithmetic Operators}
+
+\begin{tabular}{lll}
+Addition: {\ex \lstinline|+|} &
+Subtraction: {\ex \lstinline|-|} &
+Multiplication: {\ex \lstinline|*|} \\
+Division: {\ex \lstinline|/|} &
+Modulo: {\ex \lstinline|%|} &
+Exponentiation: {\ex \lstinline|**|}
+\end{tabular}
+
+%*********************************************
+\hr\textbf{Strings} (immutable)
+
+Declaring a string:\\
+{\ex \lstinline|k='Single quote' |}\\
+{\ex \lstinline|l="Double quote contain's single quote" |}\\
+{\ex \lstinline|m='''"Contain's both"''' |}\\
+{\ex \lstinline|s[5]|} -- Elements can be accessed with their index
+
+%*********************************************
+ \hr\textbf{Manipulating Strings}
+
+String indexing starts from 0, like lists.\\
+\lstinline|s = `Hello World'|
+
+\begin{tabular}{lll}
+\lstinline|s[0:5]| -- \texttt{Hello} &
+\lstinline|s[6:]| -- \texttt{World} &
+\lstinline|s[6::2]| -- \texttt{Wrd}
+\end{tabular}
+\lstinline|s.replace('e', 'a')| new string; `e' replaced by `a'
+
+%*********************************************
+\hr\textbf{Input-Output}
+
+Output:\\
+{\ex \lstinline| print x|} -- print variable \texttt{x} (with new line)\\
+{\ex \lstinline| print x, |} -- print with out new line\\
+{\ex \lstinline| print "a is %d b is %f" %(a, b)|} -- formatting
+
+Input:\\
+{\ex \lstinline| x = raw_input()|} -- take input from user\\
+{\ex \lstinline| x = raw_input("Age: ")|} -- take input with prompt
+
+%*********************************************
+\hr\textbf{Control Flow}
+
+Writing an if/elif/else block:
+\vspace{-8pt}
+\begin{lstlisting}
+if condition1:
+ # do A, B, C
+elif condition2:
+ # do D, E
+else:
+ # do Y, Z
+\end{lstlisting}
+\vspace{-8pt}
+The ternary operator:\\
+{\ex \lstinline|C if X else Y|} -- Do C if X is True else do Y.
+
+For loop:\\
+{\ex \lstinline|for i in range(1,11,2):|}\\
+{\ex \lstinline| s = s + i|}
+
+%*********************************************
+\hr\textbf{Lists}
+
+{\ex \lstinline|empty=[]|} -- empty list\\
+{\ex \lstinline|nonempty = ['spam', 'eggs', 100, 1.234]|} -- filled list\\
+{\ex \lstinline|nonempty[0]|} -- accessing elements\\
+{\ex \lstinline|nonempty[-1]|} -- accessing elements: negative indexing\\
+{\ex \lstinline|len(nonempty)|} -- obtain length of list\\
+{\ex \lstinline|nonempty.append('python')|} -- append element to list\\
+{\ex \lstinline|del(nonempty[1])|} -- delete elements\\
+{\ex \lstinline|nonempty.remove(100)|} -- remove elements\\
+{\ex \lstinline|A[start:stop:step]|} -- slicing a list \lstinline|stop| excluded
+{\ex \lstinline|sorted(A)|} -- returns a new sorted list\\
+{\ex \lstinline|A.sort()|} -- in-place sort\\
+{\ex \lstinline|A[::-1]|} -- returns new reversed list\\
+{\ex \lstinline|A.reverse()|} -- in-place reverse\\
+{\ex \lstinline|x in A|} -- check for container-ship
+
+%*********************************************
+\hr\textbf{Tuples}
+
+{\ex \lstinline|t = (1, "hello", 2.5)|} -- creating a tuple\\
+{\ex \lstinline|t[index] Ex: t[2]|} -- accessing elements\\
+{\ex \lstinline|t[start:stop:step]|} -- accessing slices\\
+{\ex \lstinline|a, b = b, a|} -- swapping: tuple packing, unpacking
+
+%*********************************************
+\hr\textbf{Dictionaries} (key value pairs)
+
+{\ex \lstinline|d = {'k1':'v1', 'k2':'v2'}|} -- creating \\
+{\ex \lstinline|print d['k1']|} -- accessing values using keys\\
+{\ex \lstinline|d['k3'] = 'v3'|} -- adding a key-value pair\\
+{\ex \lstinline|del d['k1']|} -- deleting a key-value pair
+
+%*********************************************
+\hr\textbf{Sets}
+
+{\ex \lstinline|p10 = set([2, 3, 5, 7]) |} -- creating a set
+
+Operators:\\
+{\ex \lstinline+s1 | s2 +} -- Union
+Other operations available:\\
+{\ex \lstinline|&|} -- Intersection, {\ex \lstinline|-|} -- Difference,
+{\ex \lstinline|^|} -- Symmetric Difference, {\ex \lstinline|<|} -- Subset
+
+%*********************************************
+\hr\textbf{Functions}
+
+Defining a function:
+\vspace{-8pt}
+\begin{lstlisting}
+def name([arguments]):
+ code
+ [return values]
+\end{lstlisting}
+\vspace{-8pt}
+
+Arguments of functions can have default arguments. Arguments with default arguments are at end of definition
+
+Functions can be called with keyword arguments. All keyword arguments
+should be at the end of the argument list.
+
+%*********************************************
+\hr\textbf{Modules}
+
+Import all definitions to current name-space:\\
+{\ex \lstinline| from scipy import *|}
+
+Import few definitions to current name-space:\\
+{\ex \lstinline| from scipy import linspace, pi, sin|}
+
+Import a module as a new name-space:\\
+{\ex \lstinline| import scipy|}
+
+%*********************************************
+\hr\textbf{Writing python scripts}
+
+See where python searches for modules:\\
+{\ex \lstinline|sys.path|}
+
+Include our own path in PYTHONPATH:\\
+{\ex \lstinline|sys.path.append(our_path)|}
+
+Run certian code only if executed and not if imported:\\
+{\ex \lstinline|if __name__=="__main__": #do something|}
+
+%*********************************************
+\hr\textbf{Skeleton of a Test}
+
+{\ex \lstinline| if __name__ == '__main__':|}\\
+{\ex \lstinline| result = gcd(48, 64) |}\\
+{\ex \lstinline| if result != 16: |}\\
+{\ex \lstinline| print ``Test Failed'' |}\\
+{\ex \lstinline| print ``Test Passed'' |}
+
+%*********************************************
+\hr\textbf{Testing}
+
+Get results from function or unit of code being tested. \\
+Compare it to original output. \\
+Test passed if they match else failed.
+
+%*********************************************
+\hr\textbf{Try-Catch} (Skeleton)
+
+{\ex \lstinline| try: |}\\
+{\ex \lstinline| num = int(a) |}\\
+{\ex \lstinline| except: |}\\
+{\ex \lstinline| print ``Wrong input...'' |}
+
+\hr\textbf{Code Style}
+
+Four Space Indentation\\
+79 character limit on a line\\
+Funtions should be seperated by blank line\\
+Use Docstrings\\
+White space around operators
+
+\end{multicols*}
+
+\end{document}