day2/handout.tex
changeset 78 ec1346330649
parent 72 1c1d6aaa2be3
equal deleted inserted replaced
77:3cb7e25f7c0e 78:ec1346330649
     5 \begin{document}
     5 \begin{document}
     6 \maketitle
     6 \maketitle
     7 
     7 
     8 \section{Matrices and Arrays \& 2D Plotting}
     8 \section{Matrices and Arrays \& 2D Plotting}
     9 \subsection{Matrices and Arrays}
     9 \subsection{Matrices and Arrays}
       
    10 \subsubsection{Basic Numpy}
    10 \begin{verbatim}
    11 \begin{verbatim}
    11 # Simple array math example
    12 # Simple array math example
    12 >>> import numpy as np
    13 >>> import numpy as np
    13 >>> a = np.array([1,2,3,4])
    14 >>> a = np.array([1,2,3,4])
    14 >>> b = np.arange(2,6)
    15 >>> b = np.arange(2,6)
    52 >>> np.add(a,b,a)
    53 >>> np.add(a,b,a)
    53 >>> np.sum(x,axis=1)
    54 >>> np.sum(x,axis=1)
    54 
    55 
    55 >>> np.greater(a,4)
    56 >>> np.greater(a,4)
    56 >>> np.sqrt(a)
    57 >>> np.sqrt(a)
    57 
    58 \end{verbatim}
       
    59 
       
    60 \subsubsection{Array Creation}
       
    61 \begin{verbatim}
    58 >>> np.array([2,3,4])  
    62 >>> np.array([2,3,4])  
    59 array([2, 3, 4])
    63 array([2, 3, 4])
    60 
    64 
    61 >>> np.linspace(0, 2, 4)   
    65 >>> np.linspace(0, 2, 4)   
    62 array([0.,0.6666667,1.3333333,2.])
    66 array([0.,0.6666667,1.3333333,2.])
    67 
    71 
    68 >>>a = np.array([[1,2,3],[4,5,6]])
    72 >>>a = np.array([[1,2,3],[4,5,6]])
    69 >>>np.ones_like(a)
    73 >>>np.ones_like(a)
    70 array([[1, 1, 1],
    74 array([[1, 1, 1],
    71        [1, 1, 1]])
    75        [1, 1, 1]])
    72 
    76 \end{verbatim}
       
    77 \subsubsection{Slicing, Striding Arrays}
       
    78 \begin{verbatim}
    73 >>> a = np.array([[1,2,3], [4,5,6], 
    79 >>> a = np.array([[1,2,3], [4,5,6], 
    74                [7,8,9]])
    80                [7,8,9]])
    75 >>> a[0,1:3]
    81 >>> a[0,1:3]
    76 array([2, 3])
    82 array([2, 3])
    77 >>> a[1:,1:]
    83 >>> a[1:,1:]
    85 >>> a[0::2,0::2]
    91 >>> a[0::2,0::2]
    86 array([[1, 3],
    92 array([[1, 3],
    87        [7, 9]])
    93        [7, 9]])
    88 # Slices are references to the 
    94 # Slices are references to the 
    89 # same memory!
    95 # same memory!
    90 
    96 \end{verbatim}
       
    97 \subsubsection{Random Numbers}
       
    98 \begin{verbatim}
    91 >>> np.random.rand(3,2)
    99 >>> np.random.rand(3,2)
    92 array([[ 0.96276665,  0.77174861],
   100 array([[ 0.96276665,  0.77174861],
    93        [ 0.35138557,  0.61462271],
   101        [ 0.35138557,  0.61462271],
    94        [ 0.16789255,  0.43848811]])
   102        [ 0.16789255,  0.43848811]])
    95 >>> np.random.randint(1,100)
   103 >>> np.random.randint(1,100)
    96 42
   104 42
    97 \end{verbatim}
   105 \end{verbatim}
    98 
   106 
    99 \subsection{Problem Set}
   107 \subsubsection{Problem Set}
   100 \begin{verbatim}
   108 \begin{verbatim}
   101     >>> from scipy import misc
   109     >>> from scipy import misc
   102     >>> A=misc.imread(name)
   110     >>> A=misc.imread(name)
   103     >>> misc.imshow(A)
   111     >>> misc.imshow(A)
   104 \end{verbatim}
   112 \end{verbatim}
   110   \\\small{Each element in the array is replaced by mean of all the neighbouring elements}
   118   \\\small{Each element in the array is replaced by mean of all the neighbouring elements}
   111   \\\small{How fast does your code run?}
   119   \\\small{How fast does your code run?}
   112 \end{enumerate}
   120 \end{enumerate}
   113 
   121 
   114 \subsection{2D Plotting}
   122 \subsection{2D Plotting}
       
   123 \subsubsection{Basic 2D Plotting}
   115 \begin{verbatim}
   124 \begin{verbatim}
   116 $ ipython -pylab
   125 $ ipython -pylab
   117 >>> x = linspace(0, 2*pi, 1000)
   126 >>> x = linspace(0, 2*pi, 1000)
   118 >>> plot(x, sin(x)) 
   127 >>> plot(x, sin(x)) 
   119 >>> plot(x, sin(x), 'ro')
   128 >>> plot(x, sin(x), 'ro')
   120 >>> xlabel(r'$\chi$', color='g')
   129 >>> xlabel(r'$\chi$', color='g')
   121 # LaTeX markup!
   130 # LaTeX markup!
   122 >>> ylabel(r'sin($\chi$)', color='r')
   131 >>> ylabel(r'sin($\chi$)', color='r')
   123 >>> title('Simple figure', fontsize=20)
   132 >>> title('Simple figure', fontsize=20)
   124 >>> savefig('/tmp/test.eps')
   133 >>> savefig('/tmp/test.eps')
   125 
   134 \end{verbatim}
       
   135 \subsubsection{Tweaking plots}
       
   136 \begin{verbatim}
   126 # Set properties of objects:
   137 # Set properties of objects:
   127 >>> l, = plot(x, sin(x))
   138 >>> l, = plot(x, sin(x))
   128 # Why "l,"?
   139 # Why "l,"?
   129 >>> setp(l, linewidth=2.0, color='r')
   140 >>> setp(l, linewidth=2.0, color='r')
   130 >>> l.set_linewidth(2.0)
   141 >>> l.set_linewidth(2.0)
   131 >>> draw() # Redraw.
   142 >>> draw() # Redraw.
   132 >>> setp(l) # Print properties.
   143 >>> setp(l) # Print properties.
   133 >>> clf() # Clear figure.
   144 >>> clf() # Clear figure.
   134 >>> close() # Close figure.
   145 >>> close() # Close figure.
   135 
   146 \end{verbatim}
       
   147 
       
   148 \subsubsection{Working with text}
       
   149 \begin{verbatim}
   136 >>> w = arange(-2,2,.1)
   150 >>> w = arange(-2,2,.1)
   137 >>> plot(w,exp(-(w*w))*cos)
   151 >>> plot(w,exp(-(w*w))*cos)
   138 >>> ylabel('$f(\omega)$')
   152 >>> ylabel('$f(\omega)$')
   139 >>> xlabel('$\omega$')
   153 >>> xlabel('$\omega$')
   140 >>> title(r"$f(\omega)=e^{-\omega^2}
   154 >>> title(r"$f(\omega)=e^{-\omega^2}
   142 >>> annotate('maxima',xy=(0, 1), 
   156 >>> annotate('maxima',xy=(0, 1), 
   143              xytext=(1, 0.8), 
   157              xytext=(1, 0.8), 
   144              arrowprops=dict(
   158              arrowprops=dict(
   145              facecolor='black', 
   159              facecolor='black', 
   146              shrink=0.05))
   160              shrink=0.05))
   147 
   161 \end{verbatim}
       
   162 
       
   163 \subsubsection{Legends}
       
   164 \begin{verbatim}
   148 >>> x = linspace(0, 2*np.pi, 1000)
   165 >>> x = linspace(0, 2*np.pi, 1000)
   149 >>> plot(x, cos(5*x), 'r--', 
   166 >>> plot(x, cos(5*x), 'r--', 
   150          label='cosine')
   167          label='cosine')
   151 >>> plot(x, sin(5*x), 'g--', 
   168 >>> plot(x, sin(5*x), 'g--', 
   152          label='sine')
   169          label='sine')
   153 >>> legend() 
   170 >>> legend() 
   154 # Or use:
   171 # Or use:
   155 >>> legend(['cosine', 'sine'])
   172 >>> legend(['cosine', 'sine'])
   156 
   173 \end{verbatim}
       
   174 
       
   175 \subsubsection{Multiple figures}
       
   176 \begin{verbatim}
   157 >>> figure(1)
   177 >>> figure(1)
   158 >>> plot(x, sin(x))
   178 >>> plot(x, sin(x))
   159 >>> figure(2)
   179 >>> figure(2)
   160 >>> plot(x, tanh(x))
   180 >>> plot(x, tanh(x))
   161 >>> figure(1)
   181 >>> figure(1)
   162 >>> title('Easy as 1,2,3')
   182 >>> title('Easy as 1,2,3')
   163 
   183 
   164 \end{verbatim}
   184 \end{verbatim}
   165 
   185 
   166 \subsection{Problem Set}
   186 \subsubsection{Problem Set}
   167   \begin{enumerate}
   187   \begin{enumerate}
   168     \item Write a function that plots any regular n-gon given n.
   188     \item Write a function that plots any regular n-gon given n.
   169     \item Consider the logistic map, $f(x) = kx(1-x)$, plot it for
   189     \item Consider the logistic map, $f(x) = kx(1-x)$, plot it for
   170           $k=2.5, 3.5$ and $4$ in the same plot.
   190           $k=2.5, 3.5$ and $4$ in the same plot.
   171 
   191 
   185 
   205 
   186 \subsection{Broadcasting}
   206 \subsection{Broadcasting}
   187 \begin{verbatim}
   207 \begin{verbatim}
   188 >>> a = np.arange(4)
   208 >>> a = np.arange(4)
   189 >>> b = np.arange(5)
   209 >>> b = np.arange(5)
   190 >>> a+b
   210 >>> a+b #Does this work?
   191 >>> a+3
   211 >>> a+3
   192 >>> c=np.array([3])
   212 >>> c=np.array([3])
   193 >>> a+c
   213 >>> a+c #Works!
   194 >>> b+c
   214 >>> b+c #But how?
       
   215 >>> a.shape, b.shape, c.shape
   195 
   216 
   196 >>> a = np.arange(4)
   217 >>> a = np.arange(4)
   197 >>> a+3
   218 >>> a+3
   198 array([3, 4, 5, 6])
   219 array([3, 4, 5, 6])
   199 
   220