\documentclass{slides}
\usepackage[landscape,pdftex]{geometry}
\parindent=0pt \parskip=0pt

\usepackage{amssymb,amsmath,amsthm}
\input{../tex/isomath}
\usepackage{graphicx}

\newcommand{\hatvecx}{\hat{\mathbf{x}}}
\newcommand{\hatvecy}{\hat{\mathbf{y}}}
\newcommand{\hatvecz}{\hat{\mathbf{z}}}

%% ================================================================
\begin{document}

%% ----------------------------------------------------------------
\begin{slide}
\begin{center}
\textbf{Perl for the working (or aspiring) mathematician} \\
\vspace{1.0in}
John Kerl \\
University of Arizona \\
Department of Mathematics \\
Software Interest Group \\
October 11, 2006
\end{center}
\end{slide}

%% ----------------------------------------------------------------
\begin{slide}
\begin{center}
\textbf{Abstract}
\end{center}

What can one more language do for you?

\begin{itemize}

\vspace{-0.25in} \item Why people use Perl; why some people love it and why others hate it.

\vspace{-0.25in} \item The minimum you need to know to write and run simple
Perl programs, even if you haven't used such languages before.

\vspace{-0.25in} \item Text manipulation and regular expressions.

\vspace{-0.25in} \item Numerics.

\end{itemize}

\end{slide}
%% ----------------------------------------------------------------
\begin{slide}
\begin{center}
\textbf{Why Perl?}
\end{center}

\begin{itemize}

\vspace{-0.25in} \item
Perl programs are fast to develop:  no compilation phase.

\vspace{-0.25in} \item
Perl is the de facto standard for CGI scripting.

\vspace{-0.25in} \item
Unlike monolithic programs such as Matlab, Mathematica, etc., it's easy to
connect (pipe) Perl scripts to other programs.

\vspace{-0.25in} \item
Perl has built-in support for text manipulation.

\vspace{-0.25in} \item
It works the same on Unix/Linux or Windows.  (In fact, this is why I started
using Perl back in 2000.)

\end{itemize}

\end{slide}
%% ----------------------------------------------------------------
\begin{slide}

\begin{itemize}

\vspace{-0.25in} \item It's not the best language for doing math, but it's good
for quick-and-dirty programs.  Perl does math as well as many other things (a
general-purpose language).

\vspace{-0.25in} \item Perl is good for automating all those miscellaneous
tedious time-consuming tasks --- times when you think \emph{I am really sick
and tired of this miscellaneous tedious time-consuming task which is keeping me
from what I'd rather be doing} --- grade calcuations, turning data into nicely
formatting LaTeX output, making bulk changes to many files at once, making a
nice index for LaTeX documents, etc.  etc.

\end{itemize}

\end{slide}
%% ----------------------------------------------------------------
\begin{slide}
\begin{center}
\textbf{Love/hate}
\end{center}

Perl motto:  There's more than one way to do it (TMTOWTDI).  Larry Wall took
favorite features from many languages and put them into Perl.  It's powerful
and maddeningly eclectic.

\end{slide}
%% ----------------------------------------------------------------
\begin{slide}
\begin{center}
\textbf{Reading proficiency}
\end{center}

The department has a \textbf{foreign language} requirement for PhD students.
Why?  Well, not all important results are published in your native language!

By the same token, if you're doing compuational work of any sort, you may find
that your collaborators' favorite programming languages aren't the same as
yours.

Learn some Python, Perl, C, Fortran, Matlab.

\end{slide}
%% ----------------------------------------------------------------
\begin{slide}
\begin{center}
\textbf{Magic}
\end{center}

There are many pre-existing software tools.  In this presentation, I'll give
examples of an ODE solver and a data formatter.  Matlab et al. already do these
things for you.  Why should you listen to me reinventing the wheel?

\begin{itemize}

\vspace{-0.25in} \item
Pre-existing packages don't always have what you want --- be ready to roll your
own when necessary.

\vspace{-0.25in} \item
Computers appear to be magical.  When you learn to program, you learn to create
your own wizardry and find out how things really work.  Perl is a good starter
language.  (Python is another good choice!)

\end{itemize}

\end{slide}
%% ----------------------------------------------------------------
\begin{slide}
\begin{center}
\textbf{Installation}
\end{center}

Unix/Linux:  It's already there.

Mac:  It's there too --- Perl is included in Mac OS X.

Windows:  Download it from the web.

\end{slide}
%% ----------------------------------------------------------------
\begin{slide}
\begin{center}
\textbf{Running Perl programs}
\end{center}

Put the following text in a file, say \texttt{hello.pl}:

\begin{verbatim}
print "Hello, world!\n";
\end{verbatim}

Then at the command prompt, type:

\begin{verbatim}
[shell prompt]$ perl hello.pl
\end{verbatim}

You should see:

\begin{verbatim}
Hello, world!
\end{verbatim}

\end{slide}
%% ----------------------------------------------------------------
\begin{slide}
\begin{center}
\textbf{More about running Perl programs}
\end{center}

For simplicity of presentation, I suggested typing \texttt{perl hello.pl}.

You can also just type \texttt{hello.pl}: Use \texttt{\#!} (shebang) for
Unix/Linux; \texttt{.pl} association for Windows.  (The \texttt{.pl} suffix is
a mere convention for Linux, and can matter on Windows.)  I can give you more
info about this topic but it's a technical detail.

\end{slide}
%% ----------------------------------------------------------------
\begin{slide}
\begin{center}
\textbf{Numerics by example}
\end{center}

Here is a naive Euler (first-order) integrator for the Lorenz equations.  (Feel
free to implement higher-order techniques such as Runge-Kutta --- it would
simply occupy another slide or two.)

Derivation of Euler scheme:
\begin{eqnarray*}
	x &=& x(t) \\
	x' &\approx& \frac{x(t+h) - x(t)}{h} \\
	x(t+h) &\approx& x(t) + h x' \\
	x_{k+1} &:=& x_k + h x'
\end{eqnarray*}
Let's turn that into instructions for a computer to carry out,
using the Lorenz equations.

\end{slide}
%% ----------------------------------------------------------------
\begin{slide}
\begin{center}
\textbf{Lorenz equations}
\end{center}

System of non-linear ODEs:
\begin{eqnarray*}
	x' &=& s(y-x) \\
	y' &=& rx - y - xz \\
	z' &=& xy -bz. \\
\end{eqnarray*}
Constants:
$$
	s = 10.0, \qquad
	r = 28.0, \qquad
	b = 2.6666667.
$$
Initial conditions:
$$
	x_0 = 10, \qquad
	y_0 =  0, \qquad
	z_0 = 10.
$$

\end{slide}
%% ----------------------------------------------------------------
\begin{slide}
\begin{center}
\textbf{First half of numerical script}
\end{center}

\begin{verbatim}
$tmax = 10.0;                  # Simulate 10 seconds
$h    = 0.002;                 # with step of 0.002 sec
$N    = int($tmax/$h);

$x0 = 10; $y0 = 0; $z0 = 10;   # Initial conditions

$s = 10.0; $r = 28.0; $b = 2.666667; # Constants
\end{verbatim}

\end{slide}
%% ----------------------------------------------------------------
\begin{slide}
\begin{center}
\textbf{Second half of numerical script}
\end{center}

\begin{verbatim}

$x = $x0; $y = $y0; $z = $z0;  # Start integration
for ($t = 0; $t < $tmax; $t = $t + $h) {
    printf "%11.8f\t%11.8f\t%11.8f\n", $x, $y, $z;

    $xp = $s * ($y - $x);      # Cheesy Euler integrator.
    $yp = $r * $x - $y - $x * $z;
    $zp = $x * $y - $b * $z;

    $x = $x + $h * $xp;
    $y = $y + $h * $yp;
    $z = $z + $h * $zp;
}
\end{verbatim}

\end{slide}
%% ----------------------------------------------------------------
\begin{slide}
\begin{center}
\textbf{Language points}
\end{center}

\begin{itemize}

\vspace{-0.25in} \item Scalar (think ``non-array'') variables begin with
a dollar sign.

\vspace{-0.25in} \item Variable assignments, arithmetic expressions, etc.  are
about what you'd expect.

\vspace{-0.25in} \item Statements are terminated by semicolons.

\vspace{-0.25in} \item Control structures (for, if, while, etc.) are delimited
using curly braces.

\end{itemize}

\end{slide}
%% ----------------------------------------------------------------
\begin{slide}
\begin{center}
\textbf{Output of numerical script}
\end{center}

\begin{verbatim}
[shell prompt]$ perl lor2.pl
10.00000000	 0.00000000	10.00000000
 9.80000000	 0.36000000	 9.94666666
 9.61120000	 0.71312533	 9.90067376
 9.43323851	 1.05961157	 9.86157814
 9.26576597	 1.39970047	 9.82897419
 9.10844466	 1.73363801	 9.80249158
 8.96094853	 2.06167273	 9.78179311
 ...         ...         ...
\end{verbatim}

OK, so we're seeing the estimated $x$'s, $y$'s, and $z$'s.  But how to plot
three variables on a two-dimensional screen?

\end{slide}
%% ----------------------------------------------------------------
\begin{slide}
\begin{center}
\textbf{Text manipulation by example}
\end{center}

Answer:  Projection from 3D to 2D is just a linear transformation.  Write down
a 2x3 matrix $P$ whose columns are where you want $\hatvecx$, $\hatvecy$, and
$\hatvecz$ to go.  Pre-multiply each data
triple by that:
$$
	\begin{bmatrix}
	u \\ v
	\end{bmatrix}
	=
	\begin{bmatrix}
	(P\hatvecx)_0 & (P\hatvecy)_0 & (P\hatvecz)_0 \\
	(P\hatvecx)_1 & (P\hatvecy)_1 & (P\hatvecz)_1 \\
	\end{bmatrix}
	\begin{bmatrix}
	x \\ y \\ z
	\end{bmatrix}
$$
Then plot $(u,v)$ pairs on the two-dimensional display.

\setlength{\unitlength}{1.0in}
\begin{picture}(6,1.5)
%\put(0.0,0.0){\framebox(6.0,1.5)}
\thicklines
\put(0.2,0.35){\vector(1,0){1.0}}
\put(0.2,0.35){\vector(0,1){1.0}}
\put(0.2,0.35){\vector(-1,-1){0.25}}
\put(1.4,0.35){$\hatvecy$ looks like $(1,0)$ in 2D}
\put(0.4,1.00){$\hatvecz$ looks like $(0,1)$ in 2D}
\put(0.2,0.00){$\hatvecx$ looks like $(-0.25,-0.25)$ in 2D}
\put(5.2,1.00){$P=\begin{bmatrix} -0.25 & 1 & 0 \\ -0.25 & 0 & 1 \\\end{bmatrix}$}
\thinlines
\end{picture}

\end{slide}
%% ----------------------------------------------------------------
\begin{slide}
\begin{center}
\textbf{Outline of projection script}
\end{center}

We'll make a separate (re-usable!) script that simply reads three-column data
--- from our Lorenz integrator \emph{or anything else} --- and writes
two-column data, modified as described above:

\begin{itemize}

\vspace{-0.25in} \item \textbf{Read} lines of input (triples of numbers,
delimited by whitespace) one line at a time, splitting the line into
three numbers $x$, $y$, and $z$.

\vspace{-0.25in} \item For each triple $x$, $y$, $z$, \textbf{compute}
$u$ and $v$.

\vspace{-0.25in} \item \textbf{Write} $u$ and $v$.

\end{itemize}

\end{slide}
%% ----------------------------------------------------------------
\begin{slide}
\begin{center}
\textbf{Subroutines}
\end{center}

Here's the computation part.  I'll use this as an example of subroutines.
Assume we've already read in a triple of $x$, $y$, and $z$.  We'll call this
routine to find what $u$ and $v$ are.

\begin{verbatim}
@Pxhat = (-0.25, -0.25); @Pyhat = (1, 0); @Pzhat = (0, 1);
sub do_triple
{
    my ($x, $y, $z) = @_; # Use "my" to keep them local &
    my ($u, $v);          #   not clash w/ outer x, y, z.
    $u = $Pxhat[0]*$x + $Pyhat[0]*$y + $Pzhat[0]*$z;
    $v = $Pxhat[1]*$x + $Pyhat[1]*$y + $Pzhat[1]*$z;
    return ($u, $v);
}
\end{verbatim}
\end{slide}
%% ----------------------------------------------------------------
\begin{slide}
\begin{center}
\textbf{Language points}
\end{center}

\begin{itemize}

\vspace{-0.25in} \item Subroutines start with the \texttt{sub} keyword, and
they return values (scalars, arrays, etc.) using the \texttt{return} keyword.

\vspace{-0.25in} \item The argument list is spelled \texttt{@\_}.  Keep local
variables private to your subroutine using \texttt{my}.

\vspace{-0.25in} \item You can have an array of scalars on the left-hand side
of an assignment.

\vspace{-0.25in} \item Arrays are indexed (from zero up, math folks!)
using square brackets.

\end{itemize}

\end{slide}
%% ----------------------------------------------------------------
\begin{slide}
\begin{center}
\textbf{Reading lines of standard input}
\end{center}
\begin{verbatim}
$lno = 0;             # Remember line number so we can
while ($line = <>) {  # print informative error messages.
    chomp $line;
    $line =~ s/^\s+//; # Strip leading whitespace.
    $line =~ s/\s+$//; # Strip trailing whitespace.
    $lno++;
    @flds = split /\s+/, $line;
    $num_flds = @flds;
    if (@flds == 3) {
        ($u, $v) = do_triple($flds[0], $flds[1], $flds[2]);
        printf "%11.7f %11.7f\n", $u, $v;  # Output
    } else {
        die "$0:  Need 3 columns; got $n at line $lno.\n";
    }
}
\end{verbatim}

\end{slide}
%% ----------------------------------------------------------------
\begin{slide}
\begin{center}
\textbf{Language points}

\begin{itemize}

\vspace{-0.25in} \item \texttt{<>} on the right-hand side of an assignment
gets the next line of input.

\vspace{-0.25in} \item \texttt{s/old/new/} is a substitution.  The ``old part''
is a \textbf{regular expression}.  Here, \texttt{s/\^{\textbackslash}s+//}
means replace one or more whitespace characters at the beginning of the line
with nothing.

\vspace{-0.25in} \item The \texttt{split} keyword splits a string into pieces,
with a delimiter specified by a regular expression of your choice.  It hands
back an array of pieces.

\vspace{-0.25in} \item The \texttt{printf} syntax (along with \texttt{for}) is
taken straight from C.

\end{itemize}

\end{center}
\end{slide}
%% ----------------------------------------------------------------
\begin{slide}
\begin{center}
\textbf{Command-line arguments to the projection script}
\end{center}
\begin{verbatim}
@Pxhat = (-0.25, -0.25); @Pyhat = (1, 0); @Pzhat = (0, 1);
while (@ARGV && ($ARGV[0] =~ m/^-/)) {
    if ($ARGV[0] eq "-xy") {
        @Pxhat = (1, 0);
        @Pyhat = (0, 1);
        @Pzhat = (0, 0);
    }
    elsif (...) {
        ...
    }
    else {
        last
    }
    shift @ARGV;
}
\end{verbatim}
\end{slide}
%% ----------------------------------------------------------------
\begin{slide}
\begin{center}
\textbf{Language points}
\end{center}

\begin{itemize}

\vspace{-0.25in} \item Command-line arguments to a Perl script are useful for
allowing the user (you!) to specify initial values, options, etc. instead of
typing all parameters into the script.

\vspace{-0.25in} \item \texttt{@ARGV} is an array of the command-line arguments
to the script.  (E.g. \texttt{perl projdown.pl -xy}.)

\vspace{-0.25in} \item \texttt{m/pattern/} is another use of regular
expressions:  here, for matching.  Take the specified action if the
command-line argument begins with a minus sign.

\vspace{-0.25in} \item \texttt{shift} removes the zeroth element of an array,
shortening it by one.

\end{itemize}

\end{slide}
%% ----------------------------------------------------------------
\begin{slide}
\begin{center}
\textbf{Output of numerical and projection scripts}
\end{center}

\begin{verbatim}
[shell prompt]$ perl lor2.pl | perl projdown.pl
 -2.5000000   7.5000000
 -2.0900000   7.4966667
 -1.6896747   7.4978738
 -1.2986981   7.5032685
 -0.9167410   7.5125327
 ...          ...
\end{verbatim}
Or:
\begin{verbatim}
[shell prompt]$ perl lor2.pl > xyz.txt
[shell prompt]$ perl projdown.pl < xyz.txt > uv.txt
\end{verbatim}

These data can be sent to a 2D display.

\begin{center}
\includegraphics{figures/lor.eps}

\includegraphics{figures/lor2.eps}
\end{center}

%\texttt{http://math.arizona.edu/{\textasciitilde}kerl/doc/perl-talk/lorplot.jpg}

%\texttt{http://math.arizona.edu/{\textasciitilde}kerl/doc/perl-talk/lorplot2.jpg}

Also here are the full scripts:

\texttt{http://math.arizona.edu/{\textasciitilde}kerl/doc/perl-talk/lor2.pl.txt}

\texttt{http://math.arizona.edu/{\textasciitilde}kerl/doc/perl-talk/projdown.pl.txt}

%\includegraphics*{lorplot.jpg}
%\includegraphics{lorplot.jpg}
%\includegraphics[length=1.0in,width=1.0in]{lorplot.jpg}
%\includegraphics*[length=1.0in,width=1.0in]{lorplot.jpg}
%\includegraphics*[bb=0 0 6 6,scale=1.0]{lorplot.eps}
%\scalebox{1.0}{ \includegraphics*{lorplot.jpg} }

\end{slide}
%% ----------------------------------------------------------------
\begin{slide}
\begin{center}
\textbf{More text processing}
\end{center}

Some raw data (a 3x3 matrix):

\begin{verbatim}
  0.8550000   0.1900000  -0.0450000
  0.0550000  -0.2100000   1.1550000
  3.2550000  -8.6100000   6.3550000
\end{verbatim}

Run it through the Perl program \texttt{mattex}
(available at \texttt{http://math.arizona.edu/{\textasciitilde}kerl}):

\begin{verbatim}
[shell prompt]$ mattex myfile.txt
\left[\begin{array}{rrr}
	0.8550000 &  0.1900000 & -0.0450000 \\
	0.0550000 & -0.2100000 &  1.1550000 \\
	3.2550000 & -8.6100000 &  6.3550000 \\
\end{array}\right]
\end{verbatim}

\end{slide}
%% ----------------------------------------------------------------
\begin{slide}
\begin{center}
\textbf{More text processing}
\end{center}

See the Wikipedia article on ``How Perl saved the Human Genome Project'' ---
merging of huge databases from different labs.

\end{slide}
%% ----------------------------------------------------------------
\begin{slide}
\begin{center}
\textbf{Modules}
\end{center}

There are a bajillion modules at CPAN (\texttt{cpan.perl.org}).  You might
check for pre-existing modules before inventing something from scratch.

There are interfaces for complex arithmetic, numerical linear algebra (e.g.
LAPACK/BLAS), database interfaces, etc. etc.

\end{slide}
%% ----------------------------------------------------------------
\begin{slide}
\begin{center}
\textbf{References}
\end{center}

\begin{itemize}

\vspace{-0.25in} \item
\emph{Programming Perl} (``The Camel''), O'Reilly.

\vspace{-0.25in} \item
\texttt{www.perl.org}

\vspace{-0.25in} \item
\texttt{cpan.perl.org}

\end{itemize}

\end{slide}
%% ================================================================
\end{document}
