day2/handout.tex
author Madhusudan.C.S <madhusudancs@gmail.com>
Tue, 29 Dec 2009 19:25:11 +0530
changeset 334 2214b5dba4d4
parent 78 ec1346330649
permissions -rw-r--r--
Merged the branches.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
72
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     1
\documentclass[12pt]{article}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     2
\usepackage{amsmath}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     3
\title{Python Workshop\\Problems and Exercises}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     4
\author{Asokan Pichai\\Prabhu Ramachandran}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     5
\begin{document}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     6
\maketitle
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     7
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     8
\section{Matrices and Arrays \& 2D Plotting}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     9
\subsection{Matrices and Arrays}
78
ec1346330649 Minor edits to Day2 handout.
Puneeth Chaganti <punchagan@fossee.in>
parents: 72
diff changeset
    10
\subsubsection{Basic Numpy}
72
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    11
\begin{verbatim}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    12
# Simple array math example
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    13
>>> import numpy as np
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    14
>>> a = np.array([1,2,3,4])
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    15
>>> b = np.arange(2,6)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    16
>>> b
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    17
array([2,3,4,5])
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    18
>>> a*2 + b + 1 # Basic math!
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    19
array([5, 8, 11, 14])
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    20
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    21
# Pi and e are defined.
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    22
>>> x = np.linspace(0.0, 10.0, 1000)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    23
>>> x *= 2*np.pi/10 # inplace.
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    24
# apply functions to array.
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    25
>>> y = np.sin(x)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    26
>>> z = np.exp(y)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    27
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    28
>>> x = np.array([1., 2, 3, 4])
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    29
>>> np.size(x)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    30
4
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    31
>>> x.dtype # What is a.dtype?
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    32
dtype('float64')
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    33
>>> x.shape
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    34
(4,)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    35
>>> print np.rank(x), x.itemsize
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    36
1 8
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    37
>>> x[0] = 10
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    38
>>> print x[0], x[-1]
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    39
10.0 4.0
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    40
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    41
>>> a = np.array([[ 0, 1, 2, 3],
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    42
...            [10,11,12,13]])
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    43
>>> a.shape # (rows, columns)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    44
(2, 4)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    45
# Accessing and setting values
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    46
>>> a[1,3] 
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    47
13
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    48
>>> a[1,3] = -1
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    49
>>> a[1] # The second row
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    50
array([10,11,12,-1])
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    51
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    52
>>> b=np.array([[0,2,4,2],[1,2,3,4]])
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    53
>>> np.add(a,b,a)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    54
>>> np.sum(x,axis=1)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    55
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    56
>>> np.greater(a,4)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    57
>>> np.sqrt(a)
78
ec1346330649 Minor edits to Day2 handout.
Puneeth Chaganti <punchagan@fossee.in>
parents: 72
diff changeset
    58
\end{verbatim}
72
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    59
78
ec1346330649 Minor edits to Day2 handout.
Puneeth Chaganti <punchagan@fossee.in>
parents: 72
diff changeset
    60
\subsubsection{Array Creation}
ec1346330649 Minor edits to Day2 handout.
Puneeth Chaganti <punchagan@fossee.in>
parents: 72
diff changeset
    61
\begin{verbatim}
72
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    62
>>> np.array([2,3,4])  
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    63
array([2, 3, 4])
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    64
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    65
>>> np.linspace(0, 2, 4)   
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    66
array([0.,0.6666667,1.3333333,2.])
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    67
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    68
>>>np.ones([2,2])
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    69
array([[ 1.,  1.],
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    70
     [ 1.,  1.]])
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    71
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    72
>>>a = np.array([[1,2,3],[4,5,6]])
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    73
>>>np.ones_like(a)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    74
array([[1, 1, 1],
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    75
       [1, 1, 1]])
78
ec1346330649 Minor edits to Day2 handout.
Puneeth Chaganti <punchagan@fossee.in>
parents: 72
diff changeset
    76
\end{verbatim}
ec1346330649 Minor edits to Day2 handout.
Puneeth Chaganti <punchagan@fossee.in>
parents: 72
diff changeset
    77
\subsubsection{Slicing, Striding Arrays}
ec1346330649 Minor edits to Day2 handout.
Puneeth Chaganti <punchagan@fossee.in>
parents: 72
diff changeset
    78
\begin{verbatim}
72
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    79
>>> a = np.array([[1,2,3], [4,5,6], 
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    80
               [7,8,9]])
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    81
>>> a[0,1:3]
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    82
array([2, 3])
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    83
>>> a[1:,1:]
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    84
array([[5, 6],
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    85
       [8, 9]])
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    86
>>> a[:,2]
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    87
array([3, 6, 9])
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    88
>>> a[...,2]
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    89
array([3, 6, 9])
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    90
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    91
>>> a[0::2,0::2]
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    92
array([[1, 3],
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    93
       [7, 9]])
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    94
# Slices are references to the 
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    95
# same memory!
78
ec1346330649 Minor edits to Day2 handout.
Puneeth Chaganti <punchagan@fossee.in>
parents: 72
diff changeset
    96
\end{verbatim}
ec1346330649 Minor edits to Day2 handout.
Puneeth Chaganti <punchagan@fossee.in>
parents: 72
diff changeset
    97
\subsubsection{Random Numbers}
ec1346330649 Minor edits to Day2 handout.
Puneeth Chaganti <punchagan@fossee.in>
parents: 72
diff changeset
    98
\begin{verbatim}
72
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    99
>>> np.random.rand(3,2)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   100
array([[ 0.96276665,  0.77174861],
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   101
       [ 0.35138557,  0.61462271],
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   102
       [ 0.16789255,  0.43848811]])
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   103
>>> np.random.randint(1,100)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   104
42
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   105
\end{verbatim}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   106
78
ec1346330649 Minor edits to Day2 handout.
Puneeth Chaganti <punchagan@fossee.in>
parents: 72
diff changeset
   107
\subsubsection{Problem Set}
72
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   108
\begin{verbatim}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   109
    >>> from scipy import misc
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   110
    >>> A=misc.imread(name)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   111
    >>> misc.imshow(A)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   112
\end{verbatim}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   113
\begin{enumerate}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   114
  \item Convert an RGB image to Grayscale. $ Y = 0.5R + 0.25G + 0.25B $.
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   115
  \item Scale the image to 50\%.
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   116
  \item Introduce some random noise.
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   117
  \item Smooth the image using a mean filter.
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   118
  \\\small{Each element in the array is replaced by mean of all the neighbouring elements}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   119
  \\\small{How fast does your code run?}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   120
\end{enumerate}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   121
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   122
\subsection{2D Plotting}
78
ec1346330649 Minor edits to Day2 handout.
Puneeth Chaganti <punchagan@fossee.in>
parents: 72
diff changeset
   123
\subsubsection{Basic 2D Plotting}
72
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   124
\begin{verbatim}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   125
$ ipython -pylab
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   126
>>> x = linspace(0, 2*pi, 1000)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   127
>>> plot(x, sin(x)) 
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   128
>>> plot(x, sin(x), 'ro')
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   129
>>> xlabel(r'$\chi$', color='g')
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   130
# LaTeX markup!
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   131
>>> ylabel(r'sin($\chi$)', color='r')
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   132
>>> title('Simple figure', fontsize=20)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   133
>>> savefig('/tmp/test.eps')
78
ec1346330649 Minor edits to Day2 handout.
Puneeth Chaganti <punchagan@fossee.in>
parents: 72
diff changeset
   134
\end{verbatim}
ec1346330649 Minor edits to Day2 handout.
Puneeth Chaganti <punchagan@fossee.in>
parents: 72
diff changeset
   135
\subsubsection{Tweaking plots}
ec1346330649 Minor edits to Day2 handout.
Puneeth Chaganti <punchagan@fossee.in>
parents: 72
diff changeset
   136
\begin{verbatim}
72
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   137
# Set properties of objects:
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   138
>>> l, = plot(x, sin(x))
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   139
# Why "l,"?
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   140
>>> setp(l, linewidth=2.0, color='r')
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   141
>>> l.set_linewidth(2.0)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   142
>>> draw() # Redraw.
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   143
>>> setp(l) # Print properties.
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   144
>>> clf() # Clear figure.
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   145
>>> close() # Close figure.
78
ec1346330649 Minor edits to Day2 handout.
Puneeth Chaganti <punchagan@fossee.in>
parents: 72
diff changeset
   146
\end{verbatim}
72
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   147
78
ec1346330649 Minor edits to Day2 handout.
Puneeth Chaganti <punchagan@fossee.in>
parents: 72
diff changeset
   148
\subsubsection{Working with text}
ec1346330649 Minor edits to Day2 handout.
Puneeth Chaganti <punchagan@fossee.in>
parents: 72
diff changeset
   149
\begin{verbatim}
72
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   150
>>> w = arange(-2,2,.1)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   151
>>> plot(w,exp(-(w*w))*cos)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   152
>>> ylabel('$f(\omega)$')
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   153
>>> xlabel('$\omega$')
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   154
>>> title(r"$f(\omega)=e^{-\omega^2}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   155
            cos({\omega^2})$")
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   156
>>> annotate('maxima',xy=(0, 1), 
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   157
             xytext=(1, 0.8), 
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   158
             arrowprops=dict(
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   159
             facecolor='black', 
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   160
             shrink=0.05))
78
ec1346330649 Minor edits to Day2 handout.
Puneeth Chaganti <punchagan@fossee.in>
parents: 72
diff changeset
   161
\end{verbatim}
72
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   162
78
ec1346330649 Minor edits to Day2 handout.
Puneeth Chaganti <punchagan@fossee.in>
parents: 72
diff changeset
   163
\subsubsection{Legends}
ec1346330649 Minor edits to Day2 handout.
Puneeth Chaganti <punchagan@fossee.in>
parents: 72
diff changeset
   164
\begin{verbatim}
72
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   165
>>> x = linspace(0, 2*np.pi, 1000)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   166
>>> plot(x, cos(5*x), 'r--', 
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   167
         label='cosine')
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   168
>>> plot(x, sin(5*x), 'g--', 
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   169
         label='sine')
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   170
>>> legend() 
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   171
# Or use:
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   172
>>> legend(['cosine', 'sine'])
78
ec1346330649 Minor edits to Day2 handout.
Puneeth Chaganti <punchagan@fossee.in>
parents: 72
diff changeset
   173
\end{verbatim}
72
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   174
78
ec1346330649 Minor edits to Day2 handout.
Puneeth Chaganti <punchagan@fossee.in>
parents: 72
diff changeset
   175
\subsubsection{Multiple figures}
ec1346330649 Minor edits to Day2 handout.
Puneeth Chaganti <punchagan@fossee.in>
parents: 72
diff changeset
   176
\begin{verbatim}
72
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   177
>>> figure(1)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   178
>>> plot(x, sin(x))
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   179
>>> figure(2)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   180
>>> plot(x, tanh(x))
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   181
>>> figure(1)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   182
>>> title('Easy as 1,2,3')
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   183
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   184
\end{verbatim}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   185
78
ec1346330649 Minor edits to Day2 handout.
Puneeth Chaganti <punchagan@fossee.in>
parents: 72
diff changeset
   186
\subsubsection{Problem Set}
72
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   187
  \begin{enumerate}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   188
    \item Write a function that plots any regular n-gon given n.
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   189
    \item Consider the logistic map, $f(x) = kx(1-x)$, plot it for
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   190
          $k=2.5, 3.5$ and $4$ in the same plot.
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   191
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   192
  \item Consider the iteration $x_{n+1} = f(x_n)$ where $f(x) =
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   193
        kx(1-x)$.  Plot the successive iterates of this process.
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   194
  \item Plot this using a cobweb plot as follows:
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   195
  \begin{enumerate}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   196
    \item Start at $(x_0, 0)$
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   197
    \item Draw line to $(x_i, f(x_i))$; 
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   198
    \item Set $x_{i+1} = f(x_i)$
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   199
    \item Draw line to $(x_i, x_i)$
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   200
    \item Repeat from 2 for as long as you want 
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   201
  \end{enumerate}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   202
\end{enumerate}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   203
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   204
\section{Advanced Numpy}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   205
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   206
\subsection{Broadcasting}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   207
\begin{verbatim}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   208
>>> a = np.arange(4)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   209
>>> b = np.arange(5)
78
ec1346330649 Minor edits to Day2 handout.
Puneeth Chaganti <punchagan@fossee.in>
parents: 72
diff changeset
   210
>>> a+b #Does this work?
72
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   211
>>> a+3
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   212
>>> c=np.array([3])
78
ec1346330649 Minor edits to Day2 handout.
Puneeth Chaganti <punchagan@fossee.in>
parents: 72
diff changeset
   213
>>> a+c #Works!
ec1346330649 Minor edits to Day2 handout.
Puneeth Chaganti <punchagan@fossee.in>
parents: 72
diff changeset
   214
>>> b+c #But how?
ec1346330649 Minor edits to Day2 handout.
Puneeth Chaganti <punchagan@fossee.in>
parents: 72
diff changeset
   215
>>> a.shape, b.shape, c.shape
72
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   216
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   217
>>> a = np.arange(4)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   218
>>> a+3
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   219
array([3, 4, 5, 6])
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   220
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   221
>>> x = np.ones((3, 5))
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   222
>>> y = np.ones(8)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   223
>>> (x[..., None] + y).shape
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   224
(3, 5, 8)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   225
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   226
\end{verbatim}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   227
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   228
\subsection{Copies \& Views}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   229
\begin{verbatim}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   230
>>> a = np.array([[1,2,3],[4,5,6]])
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   231
>>> b = a
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   232
>>> b is a
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   233
>>> b[0,0]=0; print a
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   234
>>> c = a.view()
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   235
>>> c is a
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   236
>>> c.base is a
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   237
>>> c.flags.owndata
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   238
>>> d = a.copy()
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   239
>>> d.base is a
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   240
>>> d.flags.owndata
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   241
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   242
>>> a = np.arange(1,9)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   243
>>> a.shape=3,3
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   244
>>> b = a[0,1:3]
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   245
>>> c = a[0::2,0::2]
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   246
>>> a.flags.owndata
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   247
>>> b.flags.owndata
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   248
>>> b.base
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   249
>>> c.base is a
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   250
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   251
>>> b = a[np.array([0,1,2])]
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   252
array([[1, 2, 3],
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   253
       [4, 5, 6],
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   254
       [7, 8, 9]])
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   255
>>> b.flags.owndata
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   256
>>> abool=np.greater(a,2)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   257
>>> c = a[abool]
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   258
>>> c.flags.owndata
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   259
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   260
\end{verbatim}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   261
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   262
\section{Scipy}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   263
\subsection{Linear Algebra}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   264
\begin{verbatim}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   265
>>> import scipy as sp
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   266
>>> from scipy import linalg
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   267
>>> A=sp.mat(np.arange(1,10))
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   268
>>> A.shape=3,3
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   269
>>> linalg.inv(A)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   270
>>> linalg.det(A)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   271
>>> linalg.norm(A)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   272
>>> linalg.expm(A) #logm
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   273
>>> linalg.sinm(A) #cosm, tanm, ...
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   274
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   275
>>> A = sp.mat(np.arange(1,10))
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   276
>>> A.shape=3,3
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   277
>>> linalg.lu(A)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   278
>>> linalg.eig(A)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   279
>>> linalg.eigvals(A)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   280
\end{verbatim}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   281
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   282
\subsection{Solving Linear Equations}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   283
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   284
\begin{align*}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   285
  3x + 2y - z  & = 1 \\
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   286
  2x - 2y + 4z  & = -2 \\
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   287
  -x + \frac{1}{2}y -z & = 0
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   288
\end{align*}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   289
To Solve this, 
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   290
\begin{verbatim}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   291
>>> A = sp.mat([[3,2,-1],[2,-2,4]
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   292
              ,[-1,1/2,-1]])
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   293
>>> B = sp.mat([[1],[-2],[0]])
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   294
>>> linalg.solve(A,B)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   295
\end{verbatim}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   296
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   297
\subsection{Integrate}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   298
\subsubsection{Quadrature}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   299
Calculate the area under $(sin(x) + x^2)$ in the range $(0,1)$
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   300
\begin{verbatim}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   301
>>> def f(x):
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   302
        return np.sin(x)+x**2
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   303
>>> integrate.quad(f, 0, 1)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   304
\end{verbatim}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   305
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   306
\subsubsection{ODE Integration}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   307
Numerically solve ODEs\\
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   308
\begin{align*}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   309
\frac{dx}{dt} &=-e^{-t}x^2\\ 
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   310
         x(0) &=2    
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   311
\end{align*}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   312
\begin{verbatim}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   313
>>> def dx_dt(x,t):
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   314
...     return -np.exp(-t)*x**2
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   315
>>> x=integrate.odeint(dx_dt, 2, t)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   316
>>> plt.plot(x,t)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   317
\end{verbatim}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   318
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   319
\subsection{Interpolation}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   320
\subsubsection{1D Interpolation}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   321
\begin{verbatim}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   322
>>> from scipy import interpolate
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   323
>>> interpolate.interp1d?
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   324
>>> x = np.arange(0,2*np.pi,np.pi/4)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   325
>>> y = np.sin(x)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   326
>>> fl = interpolate.interp1d(
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   327
            x,y,kind='linear')
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   328
>>> fc = interpolate.interp1d(
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   329
             x,y,kind='cubic')
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   330
>>> fl(np.pi/3)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   331
>>> fc(np.pi/3)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   332
\end{verbatim}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   333
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   334
\subsubsection{Splines}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   335
Plot the Cubic Spline of $sin(x)$
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   336
\begin{verbatim}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   337
>>> x = np.arange(0,2*np.pi,np.pi/4)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   338
>>> y = np.sin(x)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   339
>>> tck = interpolate.splrep(x,y)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   340
>>> X = np.arange(0,2*np.pi,np.pi/50)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   341
>>> Y = interpolate.splev(X,tck,der=0)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   342
>>> plt.plot(x,y,'o',x,y,X,Y)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   343
>>> plt.show()
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   344
\end{verbatim}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   345
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   346
\subsection{Signal \& Image Processing}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   347
Applying a simple median filter
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   348
\begin{verbatim}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   349
>>> from scipy import signal, ndimage
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   350
>>> from scipy import lena
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   351
>>> A=lena().astype('float32')
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   352
>>> B=signal.medfilt2d(A)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   353
>>> imshow(B)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   354
\end{verbatim}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   355
Zooming an array - uses spline interpolation
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   356
\begin{verbatim}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   357
>>> b=ndimage.zoom(A,0.5)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   358
>>> imshow(b)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   359
\end{verbatim}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   360
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   361
\section{3D Data Visualization}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   362
\subsection{Using mlab}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   363
\begin{verbatim}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   364
>>> from enthought.mayavi import mlab
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   365
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   366
>>> mlab.test_<TAB>
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   367
>>> mlab.test_contour3d()
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   368
>>> mlab.test_contour3d??
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   369
\end{verbatim}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   370
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   371
\subsubsection{Plotting Functions}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   372
\begin{verbatim}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   373
>>> from numpy import *
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   374
>>> t = linspace(0, 2*pi, 50)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   375
>>> u = cos(t)*pi
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   376
>>> x, y, z = sin(u), cos(u), sin(t)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   377
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   378
>>> x = mgrid[-3:3:100j,-3:3:100j]
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   379
>>> z = sin(x*x + y*y)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   380
>>> mlab.surf(x, y, z)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   381
\end{verbatim}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   382
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   383
\subsubsection{Large 2D Data}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   384
\begin{verbatim}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   385
>>> mlab.mesh(x, y, z)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   386
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   387
>>> phi, theta = numpy.mgrid[0:pi:20j, 
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   388
...                         0:2*pi:20j]
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   389
>>> x = sin(phi)*cos(theta)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   390
>>> y = sin(phi)*sin(theta)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   391
>>> z = cos(phi)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   392
>>> mlab.mesh(x, y, z, 
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   393
...           representation=
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   394
...           'wireframe')
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   395
\end{verbatim}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   396
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   397
\subsubsection{Large 3D Data}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   398
\begin{verbatim}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   399
>>> x, y, z = ogrid[-5:5:64j, 
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   400
...                -5:5:64j, 
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   401
...                -5:5:64j]
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   402
>>> mlab.contour3d(x*x*0.5 + y*y + 
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   403
                   z*z*2)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   404
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   405
>>> mlab.test_quiver3d()
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   406
\end{verbatim}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   407
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   408
\subsection{Motivational Problem}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   409
Atmospheric data of temperature over the surface of the earth. Let temperature ($T$) vary linearly with height ($z$)\\
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   410
$T = 288.15 - 6.5z$
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   411
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   412
\begin{verbatim}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   413
lat = linspace(-89, 89, 37)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   414
lon = linspace(0, 360, 37)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   415
z = linspace(0, 100, 11)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   416
\end{verbatim}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   417
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   418
\begin{verbatim}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   419
x, y, z = mgrid[0:360:37j,-89:89:37j,
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   420
                0:100:11j]
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   421
t = 288.15 - 6.5*z
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   422
mlab.contour3d(x, y, z, t)
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   423
mlab.outline()
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   424
mlab.colorbar()
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   425
\end{verbatim}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   426
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   427
\subsection{Lorenz equation}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   428
\begin{eqnarray*}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   429
\frac{d x}{dt} &=& s (y-x)\\
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   430
\frac{d y}{d t} &=& rx -y -xz\\
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   431
\frac{d z}{d t} &=& xy - bz\\
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   432
\end{eqnarray*}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   433
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   434
Let $s=10,$
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   435
$r=28,$ 
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   436
$b=8./3.$
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   437
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   438
\begin{verbatim}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   439
x, y, z = mgrid[-50:50:20j,-50:50:20j,
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   440
                -10:60:20j]
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   441
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   442
\end{verbatim}
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   443
1c1d6aaa2be3 Added first cut of Day2 handout; Minor edits to sessions 2 and 3.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   444
\end{document}