|
1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
2 %Tutorial slides on Python. |
|
3 % |
|
4 % Author: FOSSEE |
|
5 % Copyright (c) 2009, FOSSEE, IIT Bombay |
|
6 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
7 |
|
8 \documentclass[14pt,compress]{beamer} |
|
9 %\documentclass[draft]{beamer} |
|
10 %\documentclass[compress,handout]{beamer} |
|
11 %\usepackage{pgfpages} |
|
12 %\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm] |
|
13 |
|
14 % Modified from: generic-ornate-15min-45min.de.tex |
|
15 \mode<presentation> |
|
16 { |
|
17 \usetheme{Warsaw} |
|
18 \useoutertheme{infolines} |
|
19 \setbeamercovered{transparent} |
|
20 } |
|
21 |
|
22 \usepackage[english]{babel} |
|
23 \usepackage[latin1]{inputenc} |
|
24 %\usepackage{times} |
|
25 \usepackage[T1]{fontenc} |
|
26 |
|
27 % Taken from Fernando's slides. |
|
28 \usepackage{ae,aecompl} |
|
29 \usepackage{mathpazo,courier,euler} |
|
30 \usepackage[scaled=.95]{helvet} |
|
31 \usepackage{amsmath} |
|
32 |
|
33 \definecolor{darkgreen}{rgb}{0,0.5,0} |
|
34 |
|
35 \usepackage{listings} |
|
36 \lstset{language=Python, |
|
37 basicstyle=\ttfamily\bfseries, |
|
38 commentstyle=\color{red}\itshape, |
|
39 stringstyle=\color{darkgreen}, |
|
40 showstringspaces=false, |
|
41 keywordstyle=\color{blue}\bfseries} |
|
42 |
|
43 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
44 % Macros |
|
45 \setbeamercolor{emphbar}{bg=blue!20, fg=black} |
|
46 \newcommand{\emphbar}[1] |
|
47 {\begin{beamercolorbox}[rounded=true]{emphbar} |
|
48 {#1} |
|
49 \end{beamercolorbox} |
|
50 } |
|
51 \newcounter{time} |
|
52 \setcounter{time}{0} |
|
53 \newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}} |
|
54 |
|
55 \newcommand{\typ}[1]{\lstinline{#1}} |
|
56 |
|
57 \newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}} } |
|
58 |
|
59 %%% This is from Fernando's setup. |
|
60 % \usepackage{color} |
|
61 % \definecolor{orange}{cmyk}{0,0.4,0.8,0.2} |
|
62 % % Use and configure listings package for nicely formatted code |
|
63 % \usepackage{listings} |
|
64 % \lstset{ |
|
65 % language=Python, |
|
66 % basicstyle=\small\ttfamily, |
|
67 % commentstyle=\ttfamily\color{blue}, |
|
68 % stringstyle=\ttfamily\color{orange}, |
|
69 % showstringspaces=false, |
|
70 % breaklines=true, |
|
71 % postbreak = \space\dots |
|
72 % } |
|
73 |
|
74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
75 % Title page |
|
76 \title[Solving Equations \& ODEs]{Python for Science and Engg:\\Solving Equations \& ODEs} |
|
77 |
|
78 \author[FOSSEE] {FOSSEE} |
|
79 |
|
80 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} |
|
81 \date[] {SciPy 2010, Introductory tutorials\\Day 1, Session 6} |
|
82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
83 |
|
84 %\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} |
|
85 %\logo{\pgfuseimage{iitmlogo}} |
|
86 |
|
87 |
|
88 %% Delete this, if you do not want the table of contents to pop up at |
|
89 %% the beginning of each subsection: |
|
90 \AtBeginSubsection[] |
|
91 { |
|
92 \begin{frame}<beamer> |
|
93 \frametitle{Outline} |
|
94 \tableofcontents[currentsection,currentsubsection] |
|
95 \end{frame} |
|
96 } |
|
97 |
|
98 \AtBeginSection[] |
|
99 { |
|
100 \begin{frame}<beamer> |
|
101 \frametitle{Outline} |
|
102 \tableofcontents[currentsection,currentsubsection] |
|
103 \end{frame} |
|
104 } |
|
105 |
|
106 % If you wish to uncover everything in a step-wise fashion, uncomment |
|
107 % the following command: |
|
108 %\beamerdefaultoverlayspecification{<+->} |
|
109 |
|
110 %\includeonlyframes{current,current1,current2,current3,current4,current5,current6} |
|
111 |
|
112 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
113 % DOCUMENT STARTS |
|
114 \begin{document} |
|
115 |
|
116 \begin{frame} |
|
117 \maketitle |
|
118 \end{frame} |
|
119 |
|
120 %% \begin{frame} |
|
121 %% \frametitle{Outline} |
|
122 %% \tableofcontents |
|
123 %% % You might wish to add the option [pausesections] |
|
124 %% \end{frame} |
|
125 |
|
126 \section{Solving linear equations} |
|
127 |
|
128 \begin{frame}[fragile] |
|
129 \frametitle{Solution of equations} |
|
130 Consider, |
|
131 \begin{align*} |
|
132 3x + 2y - z & = 1 \\ |
|
133 2x - 2y + 4z & = -2 \\ |
|
134 -x + \frac{1}{2}y -z & = 0 |
|
135 \end{align*} |
|
136 Solution: |
|
137 \begin{align*} |
|
138 x & = 1 \\ |
|
139 y & = -2 \\ |
|
140 z & = -2 |
|
141 \end{align*} |
|
142 \end{frame} |
|
143 |
|
144 \begin{frame}[fragile] |
|
145 \frametitle{Solving using Matrices} |
|
146 Let us now look at how to solve this using \kwrd{matrices} |
|
147 \begin{lstlisting} |
|
148 In []: A = array([[3,2,-1], |
|
149 [2,-2,4], |
|
150 [-1, 0.5, -1]]) |
|
151 In []: b = array([1, -2, 0]) |
|
152 In []: x = solve(A, b) |
|
153 \end{lstlisting} |
|
154 \end{frame} |
|
155 |
|
156 \begin{frame}[fragile] |
|
157 \frametitle{Solution:} |
|
158 \begin{lstlisting} |
|
159 In []: x |
|
160 Out[]: array([ 1., -2., -2.]) |
|
161 \end{lstlisting} |
|
162 \end{frame} |
|
163 |
|
164 \begin{frame}[fragile] |
|
165 \frametitle{Let's check!} |
|
166 \begin{small} |
|
167 \begin{lstlisting} |
|
168 In []: Ax = dot(A, x) |
|
169 In []: Ax |
|
170 Out[]: array([ 1.00000000e+00, -2.00000000e+00, -1.11022302e-16]) |
|
171 \end{lstlisting} |
|
172 \end{small} |
|
173 \begin{block}{} |
|
174 The last term in the matrix is actually \alert{0}!\\ |
|
175 We can use \kwrd{allclose()} to check. |
|
176 \end{block} |
|
177 \begin{lstlisting} |
|
178 In []: allclose(Ax, b) |
|
179 Out[]: True |
|
180 \end{lstlisting} |
|
181 \inctime{10} |
|
182 \end{frame} |
|
183 |
|
184 \begin{frame}[fragile] |
|
185 \frametitle{Problem} |
|
186 Solve the set of equations: |
|
187 \begin{align*} |
|
188 x + y + 2z -w & = 3\\ |
|
189 2x + 5y - z - 9w & = -3\\ |
|
190 2x + y -z + 3w & = -11 \\ |
|
191 x - 3y + 2z + 7w & = -5\\ |
|
192 \end{align*} |
|
193 \inctime{5} |
|
194 \end{frame} |
|
195 |
|
196 \begin{frame}[fragile] |
|
197 \frametitle{Solution} |
|
198 Use \kwrd{solve()} |
|
199 \begin{align*} |
|
200 x & = -5\\ |
|
201 y & = 2\\ |
|
202 z & = 3\\ |
|
203 w & = 0\\ |
|
204 \end{align*} |
|
205 \end{frame} |
|
206 |
|
207 \section{Finding Roots} |
|
208 |
|
209 \begin{frame}[fragile] |
|
210 \frametitle{SciPy: \typ{roots}} |
|
211 \begin{itemize} |
|
212 \item Calculates the roots of polynomials |
|
213 \item To calculate the roots of $x^2-5x+6$ |
|
214 \end{itemize} |
|
215 \begin{lstlisting} |
|
216 In []: coeffs = [1, -5, 6] |
|
217 In []: roots(coeffs) |
|
218 Out[]: array([3., 2.]) |
|
219 \end{lstlisting} |
|
220 \vspace*{-.2in} |
|
221 \begin{center} |
|
222 \includegraphics[height=1.6in, interpolate=true]{data/roots} |
|
223 \end{center} |
|
224 \end{frame} |
|
225 |
|
226 \begin{frame}[fragile] |
|
227 \frametitle{SciPy: \typ{fsolve}} |
|
228 \begin{small} |
|
229 \begin{lstlisting} |
|
230 In []: from scipy.optimize import fsolve |
|
231 \end{lstlisting} |
|
232 \end{small} |
|
233 \begin{itemize} |
|
234 \item Finds the roots of a system of non-linear equations |
|
235 \item Input arguments - Function and initial estimate |
|
236 \item Returns the solution |
|
237 \end{itemize} |
|
238 \end{frame} |
|
239 |
|
240 \begin{frame}[fragile] |
|
241 \frametitle{\typ{fsolve}} |
|
242 Find the root of $sin(z)+cos^2(z)$ nearest to $0$ |
|
243 \vspace{-0.1in} |
|
244 \begin{center} |
|
245 \includegraphics[height=2.8in, interpolate=true]{data/fsolve} |
|
246 \end{center} |
|
247 \end{frame} |
|
248 |
|
249 \begin{frame}[fragile] |
|
250 \frametitle{\typ{fsolve}} |
|
251 Root of $sin(z)+cos^2(z)$ nearest to $0$ |
|
252 \begin{lstlisting} |
|
253 In []: fsolve(sin(z)+cos(z)*cos(z), 0) |
|
254 NameError: name 'z' is not defined |
|
255 \end{lstlisting} |
|
256 \end{frame} |
|
257 |
|
258 \begin{frame}[fragile] |
|
259 \frametitle{\typ{fsolve}} |
|
260 \begin{lstlisting} |
|
261 In []: z = linspace(-pi, pi) |
|
262 In []: fsolve(sin(z)+cos(z)*cos(z), 0) |
|
263 \end{lstlisting} |
|
264 \begin{small} |
|
265 \alert{\typ{TypeError:}} |
|
266 \typ{'numpy.ndarray' object is not callable} |
|
267 \end{small} |
|
268 \end{frame} |
|
269 |
|
270 \begin{frame}[fragile] |
|
271 \frametitle{Functions - Definition} |
|
272 We have been using them all along. Now let's see how to define them. |
|
273 \begin{lstlisting} |
|
274 In []: def g(z): |
|
275 ....: return sin(z)+cos(z)*cos(z) |
|
276 \end{lstlisting} |
|
277 \begin{itemize} |
|
278 \item \typ{def} -- keyword |
|
279 \item name: \typ{g} |
|
280 \item arguments: \typ{z} |
|
281 \item \typ{return} -- keyword |
|
282 \end{itemize} |
|
283 \end{frame} |
|
284 |
|
285 \begin{frame}[fragile] |
|
286 \frametitle{Functions - Calling them} |
|
287 \begin{lstlisting} |
|
288 In []: g() |
|
289 --------------------------------------- |
|
290 \end{lstlisting} |
|
291 \alert{\typ{TypeError:}}\typ{g() takes exactly 1 argument} |
|
292 \typ{(0 given)} |
|
293 \begin{lstlisting} |
|
294 In []: g(0) |
|
295 Out[]: 1.0 |
|
296 In []: g(1) |
|
297 Out[]: 1.1333975665343254 |
|
298 \end{lstlisting} |
|
299 More on Functions later \ldots |
|
300 \end{frame} |
|
301 |
|
302 \begin{frame}[fragile] |
|
303 \frametitle{\typ{fsolve} \ldots} |
|
304 Find the root of $sin(z)+cos^2(z)$ nearest to $0$ |
|
305 \begin{lstlisting} |
|
306 In []: fsolve(g, 0) |
|
307 Out[]: -0.66623943249251527 |
|
308 \end{lstlisting} |
|
309 \begin{center} |
|
310 \includegraphics[height=2in, interpolate=true]{data/fsolve} |
|
311 \end{center} |
|
312 \end{frame} |
|
313 |
|
314 \begin{frame}[fragile] |
|
315 \frametitle{Exercise Problem} |
|
316 Find the root of the equation $x^2 - sin(x) + cos^2(x) = tan(x)$ nearest to $0$ |
|
317 \end{frame} |
|
318 |
|
319 \begin{frame}[fragile] |
|
320 \frametitle{Solution} |
|
321 \begin{small} |
|
322 \begin{lstlisting} |
|
323 def g(x): |
|
324 return x**2 - sin(x) + cos(x)*cos(x) - tan(x) |
|
325 fsolve(g, 0) |
|
326 \end{lstlisting} |
|
327 \end{small} |
|
328 \begin{center} |
|
329 \includegraphics[height=2in, interpolate=true]{data/fsolve_tanx} |
|
330 \end{center} |
|
331 \end{frame} |
|
332 |
|
333 %% \begin{frame}[fragile] |
|
334 %% \frametitle{Scipy Methods \dots} |
|
335 %% \begin{small} |
|
336 %% \begin{lstlisting} |
|
337 %% In []: from scipy.optimize import fixed_point |
|
338 |
|
339 %% In []: from scipy.optimize import bisect |
|
340 |
|
341 %% In []: from scipy.optimize import newton |
|
342 %% \end{lstlisting} |
|
343 %% \end{small} |
|
344 %% \end{frame} |
|
345 |
|
346 \section{ODEs} |
|
347 |
|
348 \begin{frame}[fragile] |
|
349 \frametitle{Solving ODEs using SciPy} |
|
350 \begin{itemize} |
|
351 \item Consider the spread of an epidemic in a population |
|
352 \item $\frac{dy}{dt} = ky(L-y)$ gives the spread of the disease |
|
353 \item $L$ is the total population. |
|
354 \item Use $L = 2.5E5, k = 3E-5, y(0) = 250$ |
|
355 \item Define a function as below |
|
356 \end{itemize} |
|
357 \begin{lstlisting} |
|
358 In []: from scipy.integrate import odeint |
|
359 In []: def epid(y, t): |
|
360 .... k = 3.0e-5 |
|
361 .... L = 2.5e5 |
|
362 .... return k*y*(L-y) |
|
363 .... |
|
364 \end{lstlisting} |
|
365 \end{frame} |
|
366 |
|
367 \begin{frame}[fragile] |
|
368 \frametitle{Solving ODEs using SciPy \ldots} |
|
369 \begin{lstlisting} |
|
370 In []: t = linspace(0, 12, 61) |
|
371 |
|
372 In []: y = odeint(epid, 250, t) |
|
373 |
|
374 In []: plot(t, y) |
|
375 \end{lstlisting} |
|
376 %Insert Plot |
|
377 \end{frame} |
|
378 |
|
379 \begin{frame}[fragile] |
|
380 \frametitle{Result} |
|
381 \begin{center} |
|
382 \includegraphics[height=2in, interpolate=true]{data/image} |
|
383 \end{center} |
|
384 \end{frame} |
|
385 |
|
386 |
|
387 \begin{frame}[fragile] |
|
388 \frametitle{ODEs - Simple Pendulum} |
|
389 We shall use the simple ODE of a simple pendulum. |
|
390 \begin{equation*} |
|
391 \ddot{\theta} = -\frac{g}{L}sin(\theta) |
|
392 \end{equation*} |
|
393 \begin{itemize} |
|
394 \item This equation can be written as a system of two first order ODEs |
|
395 \end{itemize} |
|
396 \begin{align} |
|
397 \dot{\theta} &= \omega \\ |
|
398 \dot{\omega} &= -\frac{g}{L}sin(\theta) \\ |
|
399 \text{At}\ t &= 0 : \nonumber \\ |
|
400 \theta = \theta_0(10^o)\quad & \&\quad \omega = 0\ (Initial\ values)\nonumber |
|
401 \end{align} |
|
402 \end{frame} |
|
403 |
|
404 \begin{frame}[fragile] |
|
405 \frametitle{ODEs - Simple Pendulum \ldots} |
|
406 \begin{itemize} |
|
407 \item Use \typ{odeint} to do the integration |
|
408 \end{itemize} |
|
409 \begin{lstlisting} |
|
410 In []: def pend_int(initial, t): |
|
411 .... theta = initial[0] |
|
412 .... omega = initial[1] |
|
413 .... g = 9.81 |
|
414 .... L = 0.2 |
|
415 .... F=[omega, -(g/L)*sin(theta)] |
|
416 .... return F |
|
417 .... |
|
418 \end{lstlisting} |
|
419 \end{frame} |
|
420 |
|
421 \begin{frame}[fragile] |
|
422 \frametitle{ODEs - Simple Pendulum \ldots} |
|
423 \begin{itemize} |
|
424 \item \typ{t} is the time variable \\ |
|
425 \item \typ{initial} has the initial values |
|
426 \end{itemize} |
|
427 \begin{lstlisting} |
|
428 In []: t = linspace(0, 20, 101) |
|
429 In []: initial = [10*2*pi/360, 0] |
|
430 \end{lstlisting} |
|
431 \end{frame} |
|
432 |
|
433 \begin{frame}[fragile] |
|
434 \frametitle{ODEs - Simple Pendulum \ldots} |
|
435 %%\begin{small} |
|
436 \typ{In []: from scipy.integrate import odeint} |
|
437 %%\end{small} |
|
438 \begin{lstlisting} |
|
439 In []: pend_sol = odeint(pend_int, |
|
440 initial,t) |
|
441 \end{lstlisting} |
|
442 \end{frame} |
|
443 |
|
444 \begin{frame}[fragile] |
|
445 \frametitle{Result} |
|
446 \begin{center} |
|
447 \includegraphics[height=2in, interpolate=true]{data/ode} |
|
448 \end{center} |
|
449 \end{frame} |
|
450 |
|
451 \section{FFTs} |
|
452 |
|
453 \begin{frame}[fragile] |
|
454 \frametitle{The FFT} |
|
455 \begin{itemize} |
|
456 \item We have a simple signal $y(t)$ |
|
457 \item Find the FFT and plot it |
|
458 \end{itemize} |
|
459 \begin{lstlisting} |
|
460 In []: t = linspace(0, 2*pi, 500) |
|
461 In []: y = sin(4*pi*t) |
|
462 |
|
463 In []: f = fft(y) |
|
464 In []: freq = fftfreq(500, t[1] - t[0]) |
|
465 |
|
466 In []: plot(freq[:250], abs(f)[:250]) |
|
467 In []: grid() |
|
468 \end{lstlisting} |
|
469 \end{frame} |
|
470 |
|
471 \begin{frame}[fragile] |
|
472 \frametitle{FFTs cont\dots} |
|
473 \begin{lstlisting} |
|
474 In []: y1 = ifft(f) # inverse FFT |
|
475 In []: allclose(y, y1) |
|
476 Out[]: True |
|
477 \end{lstlisting} |
|
478 \end{frame} |
|
479 |
|
480 \begin{frame}[fragile] |
|
481 \frametitle{FFTs cont\dots} |
|
482 Let us add some noise to the signal |
|
483 \begin{lstlisting} |
|
484 In []: yr = y + random(size=500)*0.2 |
|
485 In []: yn = y + normal(size=500)*0.2 |
|
486 |
|
487 In []: plot(t, yr) |
|
488 In []: figure() |
|
489 In []: plot(freq[:250], |
|
490 ...: abs(fft(yn))[:250]) |
|
491 \end{lstlisting} |
|
492 \begin{itemize} |
|
493 \item \typ{random}: produces uniform deviates in $[0, 1)$ |
|
494 \item \typ{normal}: draws random samples from a Gaussian |
|
495 distribution |
|
496 \item Useful to create a random matrix of any shape |
|
497 \end{itemize} |
|
498 \end{frame} |
|
499 |
|
500 \begin{frame}[fragile] |
|
501 \frametitle{FFTs cont\dots} |
|
502 Filter the noisy signal: |
|
503 \begin{lstlisting} |
|
504 In []: from scipy import signal |
|
505 In []: yc = signal.wiener(yn, 5) |
|
506 In []: clf() |
|
507 In []: plot(t, yc) |
|
508 In []: figure() |
|
509 In []: plot(freq[:250], |
|
510 ...: abs(fft(yc))[:250]) |
|
511 \end{lstlisting} |
|
512 Only scratched the surface here \dots |
|
513 \end{frame} |
|
514 |
|
515 |
|
516 \begin{frame} |
|
517 \frametitle{Things we have learned} |
|
518 \begin{itemize} |
|
519 \item Solving Linear Equations |
|
520 \item Defining Functions |
|
521 \item Finding Roots |
|
522 \item Solving ODEs |
|
523 \item Random number generation |
|
524 \item FFTs and basic signal processing |
|
525 \end{itemize} |
|
526 \end{frame} |
|
527 |
|
528 \end{document} |
|
529 |
|
530 %% Questions for Quiz %% |
|
531 %% ------------------ %% |
|
532 |
|
533 \begin{frame} |
|
534 \frametitle{\incqno } |
|
535 Given a 4x4 matrix \texttt{A} and a 4-vector \texttt{b}, what command do |
|
536 you use to solve for the equation \\ |
|
537 \texttt{Ax = b}? |
|
538 \end{frame} |
|
539 |
|
540 \begin{frame} |
|
541 \frametitle{\incqno } |
|
542 What command will you use if you wish to integrate a system of ODEs? |
|
543 \end{frame} |
|
544 |
|
545 \begin{frame} |
|
546 \frametitle{\incqno } |
|
547 How do you calculate the roots of the polynomial, $y = 1 + 6x + 8x^2 + |
|
548 x^3$? |
|
549 \end{frame} |
|
550 |
|
551 \begin{frame} |
|
552 \frametitle{\incqno } |
|
553 Two arrays \lstinline+a+ and \lstinline+b+ are numerically almost equal, what command |
|
554 do you use to check if this is true? |
|
555 \end{frame} |
|
556 |
|
557 %% \begin{frame}[fragile] |
|
558 %% \frametitle{\incqno } |
|
559 %% \begin{lstlisting} |
|
560 %% In []: x = arange(0, 1, 0.25) |
|
561 %% In []: print x |
|
562 %% \end{lstlisting} |
|
563 %% What will be printed? |
|
564 %% \end{frame} |
|
565 |
|
566 |
|
567 %% \begin{frame}[fragile] |
|
568 %% \frametitle{\incqno } |
|
569 %% \begin{lstlisting} |
|
570 %% from scipy.integrate import quad |
|
571 %% def f(x): |
|
572 %% res = x*cos(x) |
|
573 %% quad(f, 0, 1) |
|
574 %% \end{lstlisting} |
|
575 %% What changes will you make to the above code to make it work? |
|
576 %% \end{frame} |
|
577 |
|
578 %% \begin{frame} |
|
579 %% \frametitle{\incqno } |
|
580 %% What two commands will you use to create and evaluate a spline given |
|
581 %% some data? |
|
582 %% \end{frame} |
|
583 |
|
584 %% \begin{frame}[fragile] |
|
585 %% \frametitle{\incqno } |
|
586 %% What would be the result? |
|
587 %% \begin{lstlisting} |
|
588 %% In []: x |
|
589 %% array([[0, 1, 2], |
|
590 %% [3, 4, 5], |
|
591 %% [6, 7, 8]]) |
|
592 %% In []: x[::-1,:] |
|
593 %% \end{lstlisting} |
|
594 %% Hint: |
|
595 %% \begin{lstlisting} |
|
596 %% In []: x = arange(9) |
|
597 %% In []: x[::-1] |
|
598 %% array([8, 7, 6, 5, 4, 3, 2, 1, 0]) |
|
599 %% \end{lstlisting} |
|
600 %% \end{frame} |
|
601 |
|
602 %% \begin{frame}[fragile] |
|
603 %% \frametitle{\incqno } |
|
604 %% What would be the result? |
|
605 %% \begin{lstlisting} |
|
606 %% In []: y = arange(3) |
|
607 %% In []: x = linspace(0,3,3) |
|
608 %% In []: x-y |
|
609 %% \end{lstlisting} |
|
610 %% \end{frame} |
|
611 |
|
612 %% \begin{frame}[fragile] |
|
613 %% \frametitle{\incqno } |
|
614 %% \begin{lstlisting} |
|
615 %% In []: x |
|
616 %% array([[ 0, 1, 2, 3], |
|
617 %% [ 4, 5, 6, 7], |
|
618 %% [ 8, 9, 10, 11], |
|
619 %% [12, 13, 14, 15]]) |
|
620 %% \end{lstlisting} |
|
621 %% How will you get the following \lstinline+x+? |
|
622 %% \begin{lstlisting} |
|
623 %% array([[ 5, 7], |
|
624 %% [ 9, 11]]) |
|
625 %% \end{lstlisting} |
|
626 %% \end{frame} |
|
627 |
|
628 %% \begin{frame}[fragile] |
|
629 %% \frametitle{\incqno } |
|
630 %% What would be the output? |
|
631 %% \begin{lstlisting} |
|
632 %% In []: y = arange(4) |
|
633 %% In []: x = array(([1,2,3,2],[1,3,6,0])) |
|
634 %% In []: x + y |
|
635 %% \end{lstlisting} |
|
636 %% \end{frame} |
|
637 |
|
638 %% \begin{frame}[fragile] |
|
639 %% \frametitle{\incqno } |
|
640 %% \begin{lstlisting} |
|
641 %% In []: line = plot(x, sin(x)) |
|
642 %% \end{lstlisting} |
|
643 %% Use the \lstinline+set_linewidth+ method to set width of \lstinline+line+ to 2. |
|
644 %% \end{frame} |
|
645 |
|
646 %% \begin{frame}[fragile] |
|
647 %% \frametitle{\incqno } |
|
648 %% What would be the output? |
|
649 %% \begin{lstlisting} |
|
650 %% In []: x = arange(9) |
|
651 %% In []: y = arange(9.) |
|
652 %% In []: x == y |
|
653 %% \end{lstlisting} |
|
654 %% \end{frame} |
|
655 |