day2/session3.tex
branchscipyin2010
changeset 452 f9417abb23a6
parent 451 db7b23465572
equal deleted inserted replaced
451:db7b23465572 452:f9417abb23a6
    49 }
    49 }
    50 \newcounter{time}
    50 \newcounter{time}
    51 \setcounter{time}{0}
    51 \setcounter{time}{0}
    52 \newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}}
    52 \newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}}
    53 
    53 
    54 \newcommand{\typ}[1]{\lstinline{#1}}
    54 \newcommand{\typ}[1]{\textbf{\texttt{#1}}}
    55 
    55 
    56 \newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}}  }
    56 \newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}}  }
    57 
    57 
    58 %%% This is from Fernando's setup.
    58 %%% This is from Fernando's setup.
    59 % \usepackage{color}
    59 % \usepackage{color}
   191         n /= 2
   191         n /= 2
   192     return z
   192     return z
   193   \end{lstlisting}
   193   \end{lstlisting}
   194 \end{frame} 
   194 \end{frame} 
   195 
   195 
       
   196 \subsection{Exercises}
       
   197 \begin{frame}{Problem 3.1}
       
   198   Write a function to return the gcd of two numbers.
       
   199 \end{frame}
       
   200 
       
   201 \begin{frame}{Problem 3.2}
       
   202 Write a program to print all primitive pythagorean triads (a, b, c) where a, b are in the range 1---100 \\
       
   203 A pythagorean triad $(a,b,c)$ has the property $a^2 + b^2 = c^2$.\\By primitive we mean triads that do not `depend' on others. For example, (4,3,5) is a variant of (3,4,5) and hence is not primitive. And (10,24,26) is easily derived from (5,12,13) and is also not primitive.
       
   204 \end{frame}
       
   205 
       
   206 \begin{frame}{Problem 3.3}
       
   207   Write a program that generates a list of all four digit numbers that have all their digits even and are perfect squares.\newline\\\emph{For example, the output should include 6400 but not 8100 (one digit is odd) or 4248 (not a perfect square).}
       
   208 
       
   209 % \inctime{15}
       
   210 \end{frame}
       
   211 
   196 \subsection{Default arguments}
   212 \subsection{Default arguments}
   197 \begin{frame}[fragile]
   213 \begin{frame}[fragile]
   198   \frametitle{Functions: default arguments}
   214   \frametitle{Functions: default arguments}
   199   \begin{lstlisting}
   215   \begin{lstlisting}
   200 In []: greet = 'hello world'
   216 In []: greet = 'hello world'
   265       \item \typ{abs, any, all, len, max, min}
   281       \item \typ{abs, any, all, len, max, min}
   266       \item \typ{pow, range, sum, type}
   282       \item \typ{pow, range, sum, type}
   267       \item Refer here:
   283       \item Refer here:
   268           \url{http://docs.python.org/library/functions.html}
   284           \url{http://docs.python.org/library/functions.html}
   269   \end{itemize}
   285   \end{itemize}
   270   \inctime{10} 
   286 %  \inctime{10} 
   271 \end{frame}
       
   272 
       
   273 \subsection{Exercises}
       
   274 \begin{frame}{Problem set 3: Problem 3.1}
       
   275   Write a function to return the gcd of two numbers.
       
   276 \end{frame}
       
   277 
       
   278 \begin{frame}{Problem 3.2}
       
   279 Write a program to print all primitive pythagorean triads (a, b, c) where a, b are in the range 1---100 \\
       
   280 A pythagorean triad $(a,b,c)$ has the property $a^2 + b^2 = c^2$.\\By primitive we mean triads that do not `depend' on others. For example, (4,3,5) is a variant of (3,4,5) and hence is not primitive. And (10,24,26) is easily derived from (5,12,13) and is also not primitive.
       
   281 \end{frame}
       
   282 
       
   283 \begin{frame}{Problem 3.3}
       
   284   Write a program that generates a list of all four digit numbers that have all their digits even and are perfect squares.\newline\\\emph{For example, the output should include 6400 but not 8100 (one digit is odd) or 4248 (not a perfect square).}
       
   285 
       
   286 \inctime{15}
       
   287 \end{frame}
   287 \end{frame}
   288 
   288 
   289 \section{Modules}
   289 \section{Modules}
   290 \begin{frame}[fragile]
   290 \begin{frame}[fragile]
   291   \frametitle{\texttt{from} \ldots \texttt{import} magic}
   291   \frametitle{\typ{from} \ldots \typ{import} magic}
   292   \begin{lstlisting}
   292   \begin{lstlisting}
   293 from scipy.integrate import odeint
   293 from scipy.integrate import odeint
   294 
   294 
   295 from scipy.optimize import fsolve
   295 from scipy.optimize import fsolve
   296   \end{lstlisting}
   296   \end{lstlisting}
   382 from scipy.optimize import fsolve
   382 from scipy.optimize import fsolve
   383   \end{lstlisting}
   383   \end{lstlisting}
   384 \end{frame}
   384 \end{frame}
   385 
   385 
   386 \begin{frame}[fragile]
   386 \begin{frame}[fragile]
   387   \frametitle{\texttt{from} \ldots \texttt{import} - conventional way!}
   387   \frametitle{\typ{from} \ldots \typ{import} - conventional way!}
   388   \small
   388   \small
   389   \begin{lstlisting}
   389   \begin{lstlisting}
   390 from scipy import linspace, pi, sin
   390 from scipy import linspace, pi, sin
   391 from pylab import plot, legend, annotate
   391 from pylab import plot, legend, annotate
   392 from pylab import xlim, ylim
   392 from pylab import xlim, ylim
   402 ylim(-5*pi, 5*pi)
   402 ylim(-5*pi, 5*pi)
   403   \end{lstlisting}
   403   \end{lstlisting}
   404 \end{frame}
   404 \end{frame}
   405 
   405 
   406 \begin{frame}[fragile]
   406 \begin{frame}[fragile]
   407   \frametitle{\texttt{from} \ldots \texttt{import} - conventional way!}
   407   \frametitle{\typ{from} \ldots \typ{import} - conventional way!}
   408   \small
   408   \small
   409   \begin{lstlisting}
   409   \begin{lstlisting}
   410 import scipy
   410 import scipy
   411 import pylab
   411 import pylab
   412 
   412 
   421 pylab.ylim(-5*scipy.pi, 5*scipy.pi)
   421 pylab.ylim(-5*scipy.pi, 5*scipy.pi)
   422   \end{lstlisting}
   422   \end{lstlisting}
   423 \end{frame}
   423 \end{frame}
   424 
   424 
   425 \begin{frame}[fragile]
   425 \begin{frame}[fragile]
   426   \frametitle{\texttt{import} - the community convention}
   426   \frametitle{\typ{import} - the community convention}
   427   \begin{lstlisting}
   427   \begin{lstlisting}
   428 
   428 
   429 import numpy as np
   429 import numpy as np
   430 np.linspace(-5 * np.pi, 5 * np.pi, 500)
   430 np.linspace(-5 * np.pi, 5 * np.pi, 500)
   431 
   431 
   450     \item And a whole lot more!
   450     \item And a whole lot more!
   451     \end{itemize}
   451     \end{itemize}
   452   \item Check out the Python Library reference:
   452   \item Check out the Python Library reference:
   453     \url{http://docs.python.org/library/}
   453     \url{http://docs.python.org/library/}
   454   \end{itemize}
   454   \end{itemize}
   455 \inctime{5}
   455 % \inctime{5}
   456 \end{frame}
   456 \end{frame}
   457 
   457 
   458 \begin{frame}[fragile]
   458 \begin{frame}[fragile]
   459   \frametitle{Modules of special interest}
   459   \frametitle{Modules of special interest}
   460   \begin{description}[matplotlibfor2d]
   460   \begin{description}[matplotlibfor2d]
   461     \item[\texttt{pylab}] Easy, interactive, 2D plotting
   461     \item[\typ{pylab}] Easy, interactive, 2D plotting
   462 
   462 
   463     \item[\texttt{scipy}] arrays, statistics, optimization, integration, linear
   463     \item[\typ{scipy}] arrays, statistics, optimization, integration, linear
   464             algebra, Fourier transforms, signal and image processing,
   464             algebra, Fourier transforms, signal and image processing,
   465             genetic algorithms, ODE solvers, special functions, and more
   465             genetic algorithms, ODE solvers, special functions, and more
   466 
   466 
   467     \item[\texttt{Mayavi}] Easy, interactive, 3D plotting
   467     \item[\typ{mayavi}] Easy, interactive, 3D plotting
   468   \end{description}
   468   \end{description}
   469 \end{frame}
   469 \end{frame}
   470 
   470 
   471 \begin{frame}[fragile]
   471 \begin{frame}[fragile]
   472   \frametitle{From which module?}
   472   \frametitle{From which module?}
   541 for line in open("myfile.txt"):
   541 for line in open("myfile.txt"):
   542     print line
   542     print line
   543 for line in urllib2.urlopen('http://site.com'):
   543 for line in urllib2.urlopen('http://site.com'):
   544     print line
   544     print line
   545   \end{lstlisting}
   545   \end{lstlisting}
   546   \inctime{10}
   546 %  \inctime{10}
   547 \end{frame}
   547 \end{frame}
   548 
   548 
   549 \begin{frame}
   549 \begin{frame}
   550   \frametitle{What did we learn?}
   550   \frametitle{What did we learn?}
   551   \begin{itemize}
   551   \begin{itemize}
       
   552     \item Functions: Definition
       
   553     \item Functions: Docstrings
   552     \item Functions: Default and Keyword arguments
   554     \item Functions: Default and Keyword arguments
   553     \item Modules
   555     \item Modules
   554     \item Objects
   556     \item Objects
   555   \end{itemize}
   557   \end{itemize}
   556 \end{frame}
   558 \end{frame}