day2/session2.tex
changeset 96 a749db24e73b
parent 84 338b85a9c864
child 97 555237dbce44
equal deleted inserted replaced
95:691608044ae7 96:a749db24e73b
   118 \end{frame}
   118 \end{frame}
   119 
   119 
   120 \section{Advanced Numpy}
   120 \section{Advanced Numpy}
   121 \begin{frame}[fragile]
   121 \begin{frame}[fragile]
   122   \frametitle{Broadcasting}
   122   \frametitle{Broadcasting}
   123   Try it!
       
   124   \begin{lstlisting}
   123   \begin{lstlisting}
   125     >>> a = np.arange(4)
   124     >>> a = np.arange(4)
   126     >>> b = np.arange(5)
   125     >>> b = np.arange(5)
   127     >>> a+b #Does this work?
   126     >>> a+b #Does this work?
   128     >>> a+3
   127     >>> a+3
   174     \end{figure}
   173     \end{figure}
   175 \end{frame}
   174 \end{frame}
   176 
   175 
   177 \begin{frame}[fragile]
   176 \begin{frame}[fragile]
   178   \frametitle{Copies \& Views}
   177   \frametitle{Copies \& Views}
   179   Try it!
       
   180   \vspace{-0.1in}
   178   \vspace{-0.1in}
   181   \begin{lstlisting}
   179   \begin{lstlisting}
   182     >>> a = np.arange(1,9); a.shape=3,3
   180     >>> a = np.arange(1,9); a.shape=3,3
   183     >>> b = a
   181     >>> b = a
   184     >>> b is a
   182     >>> b is a
   193   \end{lstlisting}
   191   \end{lstlisting}
   194 \end{frame}
   192 \end{frame}
   195 
   193 
   196 \begin{frame}[fragile]
   194 \begin{frame}[fragile]
   197   \frametitle{Copies \& Views}
   195   \frametitle{Copies \& Views}
   198   Try it!
       
   199   \vspace{-0.1in}
   196   \vspace{-0.1in}
   200   \begin{lstlisting}
   197   \begin{lstlisting}
   201     >>> b = a[0,1:3]
   198     >>> b = a[0,1:3]
   202     >>> c = a[0::2,0::2]
   199     >>> c = a[0::2,0::2]
   203     >>> a.flags.owndata
   200     >>> a.flags.owndata
   272 \end{frame}
   269 \end{frame}
   273 
   270 
   274 \subsection{Linear Algebra}
   271 \subsection{Linear Algebra}
   275 \begin{frame}[fragile]
   272 \begin{frame}[fragile]
   276   \frametitle{Linear Algebra}
   273   \frametitle{Linear Algebra}
   277   Try it!
       
   278   \begin{lstlisting}
   274   \begin{lstlisting}
   279     >>> import scipy as sp
   275     >>> import scipy as sp
   280     >>> from scipy import linalg
   276     >>> from scipy import linalg
   281     >>> A=sp.mat(np.arange(1,10))
   277     >>> A = sp.array(sp.arange(1,10))
   282     >>> A.shape=3,3
   278     >>> A.shape = 3,3
   283     >>> linalg.inv(A)
   279     >>> linalg.inv(A)
   284     >>> linalg.det(A)
   280     >>> linalg.det(A)
   285     >>> linalg.norm(A)
   281     >>> linalg.norm(A)
   286     >>> linalg.expm(A) #logm
   282     >>> linalg.expm(A) #logm
   287     >>> linalg.sinm(A) #cosm, tanm, ...
   283     >>> linalg.sinm(A) #cosm, tanm, ...
   288   \end{lstlisting}
   284   \end{lstlisting}
   289 \end{frame}
   285 \end{frame}
   290 
   286 
   291 \begin{frame}[fragile]
   287 \begin{frame}[fragile]
   292   \frametitle{Linear Algebra ...}
   288   \frametitle{Linear Algebra ...}
   293   Try it!
   289   \begin{lstlisting}
   294   \begin{lstlisting}
   290     >>> A = sp.array(sp.arange(1,10))
   295     >>> A = sp.mat(np.arange(1,10))
   291     >>> A.shape = 3,3
   296     >>> A.shape=3,3
       
   297     >>> linalg.lu(A)
   292     >>> linalg.lu(A)
   298     >>> linalg.eig(A)
   293     >>> linalg.eig(A)
   299     >>> linalg.eigvals(A)
   294     >>> linalg.eigvals(A)
   300   \end{lstlisting}
   295   \end{lstlisting}
   301 \end{frame}
   296 \end{frame}
   302 
   297 
   303 \begin{frame}[fragile]
   298 \begin{frame}[fragile]
   304   \frametitle{Solving Linear Equations}
   299   \frametitle{Solving Linear Equations}
       
   300   \vspace{-0.2in}
   305   \begin{align*}
   301   \begin{align*}
   306     3x + 2y - z  & = 1 \\
   302     3x + 2y - z  & = 1 \\
   307     2x - 2y + 4z  & = -2 \\
   303     2x - 2y + 4z  & = -2 \\
   308     -x + \frac{1}{2}y -z & = 0
   304     -x + \frac{1}{2}y -z & = 0
   309   \end{align*}
   305   \end{align*}
   310   To Solve this, 
   306   To Solve this, 
   311   \begin{lstlisting}
   307   \begin{lstlisting}
   312     >>> A = sp.mat([[3,2,-1],[2,-2,4]
   308     >>> A = sp.array([[3,2,-1],[2,-2,4]
   313                   ,[-1,1/2,-1]])
   309                   ,[-1,1/2,-1]])
   314     >>> B = sp.mat([[1],[-2],[0]])
   310     >>> b = sp.array([1,-2,0])
   315     >>> linalg.solve(A,B)
   311     >>> x = linalg.solve(A,b)
       
   312     >>> Ax = sp.dot(A,x)
       
   313     >>> sp.allclose(Ax, b)
   316   \end{lstlisting}
   314   \end{lstlisting}
   317 \inctime{15}
   315 \inctime{15}
   318 \end{frame}
   316 \end{frame}
   319 
   317 
   320 \subsection{Integration}
   318 \subsection{Integration}
   341            x&=2 \quad at \ t=0
   339            x&=2 \quad at \ t=0
   342   \end{align*}
   340   \end{align*}
   343   \begin{lstlisting}
   341   \begin{lstlisting}
   344 >>> def dx_dt(x,t):
   342 >>> def dx_dt(x,t):
   345         return -np.exp(-t)*x**2
   343         return -np.exp(-t)*x**2
   346 >>> t=np.linspace(0,2,100)
   344 >>> t = np.linspace(0,2,100)
   347 >>> x=integrate.odeint(dx_dt, 2, t)
   345 >>> x = integrate.odeint(dx_dt, 2, t)
   348 >>> plt.plot(x,t)
   346 >>> plt.plot(x,t)
   349   \end{lstlisting}
   347   \end{lstlisting}
   350 \inctime{10}
   348 \inctime{10}
   351 \end{frame}
   349 \end{frame}
   352 
   350 
   353 \subsection{Interpolation}
   351 \subsection{Interpolation}
   354 \begin{frame}[fragile]
   352 \begin{frame}[fragile]
   355   \frametitle{Interpolation}
   353   \frametitle{Interpolation}
   356   Try it!
       
   357   \begin{lstlisting}
   354   \begin{lstlisting}
   358 >>> from scipy import interpolate
   355 >>> from scipy import interpolate
   359 >>> interpolate.interp1d?
   356 >>> interpolate.interp1d?
   360 >>> x = np.arange(0,2*np.pi,np.pi/4)
   357 >>> x = np.arange(0,2*np.pi,np.pi/4)
   361 >>> y = np.sin(x)
   358 >>> y = np.sin(x)
   371 \begin{frame}[fragile]
   368 \begin{frame}[fragile]
   372   \frametitle{Interpolation - Splines}
   369   \frametitle{Interpolation - Splines}
   373   Plot the Cubic Spline of $sin(x)$
   370   Plot the Cubic Spline of $sin(x)$
   374   \begin{lstlisting}
   371   \begin{lstlisting}
   375 >>> tck = interpolate.splrep(x,y)
   372 >>> tck = interpolate.splrep(x,y)
   376 >>> X = np.arange(0,2*np.pi,np.pi/50)
   373 >>> xs = np.arange(0,2*np.pi,np.pi/50)
   377 >>> Y = interpolate.splev(X,tck,der=0)
   374 >>> ys = interpolate.splev(X,tck,der=0)
   378 >>> plt.plot(x,y,'o',x,y,X,Y)
   375 >>> plt.plot(x,y,'o',x,y,xs,ys)
   379 >>> plt.show()
   376 >>> plt.show()
   380   \end{lstlisting}
   377   \end{lstlisting}
   381 \inctime{10}
   378 \inctime{10}
   382 \end{frame}
   379 \end{frame}
   383 
   380 
   402   \frametitle{Signal \& Image Processing}
   399   \frametitle{Signal \& Image Processing}
   403   Applying a simple median filter
   400   Applying a simple median filter
   404   \begin{lstlisting}
   401   \begin{lstlisting}
   405 >>> from scipy import signal, ndimage
   402 >>> from scipy import signal, ndimage
   406 >>> from scipy import lena
   403 >>> from scipy import lena
   407 >>> A=lena().astype('float32')
   404 >>> A = lena().astype('float32')
   408 >>> B=signal.medfilt2d(A)
   405 >>> B = signal.medfilt2d(A)
   409 >>> imshow(B)
   406 >>> imshow(B)
   410   \end{lstlisting}
   407   \end{lstlisting}
   411   Zooming an array - uses spline interpolation
   408   Zooming an array - uses spline interpolation
   412   \begin{lstlisting}
   409   \begin{lstlisting}
   413 >>> b=ndimage.zoom(A,0.5)
   410 >>> b = ndimage.zoom(A,0.5)
   414 >>> imshow(b)
   411 >>> imshow(b)
   415   \end{lstlisting}
   412   \end{lstlisting}
   416     \inctime{5}
   413     \inctime{5}
   417 \end{frame}
   414 \end{frame}
   418 
   415