% \documentclass[serif]{beamer} % Serif for Computer Modern math font.
\documentclass[serif, handout]{beamer} % Handout to ignore pause statements

\hypersetup{colorlinks,linkcolor=,urlcolor=red}


\usefonttheme{serif} % Looks like Computer Modern for non-math text -- nice!
\setbeamertemplate{navigation symbols}{} % Suppress navigation symbols
\usetheme{AnnArbor}  % CambridgeUS
% \usetheme{Frankfurt}  % Displays section titles on top: Fairly thin but still swallows some material at bottom of crowded slides
% \usetheme{Berlin} % Displays sections on top
% \usetheme{Berkeley}

\usepackage[english]{babel}
\usepackage{amsmath} % for binom
% \usepackage{graphicx} % To include pdf files!
% \definecolor{links}{HTML}{2A1B81}
% \definecolor{links}{red}
\setbeamertemplate{footline}[frame number] 

\mode<presentation>

\title{\texttt{SageMath}: Open source software for symbolic mathematics\footnote{ This slide show is an open-source document. See last slide for copyright information.}}
\subtitle{Jerry Brunner, Department of Statistical Sciences}
\date{} % To suppress date

\begin{document}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
  \titlepage
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
\frametitle{Symbolic calculation}
%\framesubtitle{} 
\begin{itemize}
    \item Lots of software can carry out numerical calculations, and so can \texttt{SageMath}. \pause
    \item What makes \texttt{SageMath} special is that it can also do \emph{symbolic} computation. \pause
    \item That is, it is able to manipulate symbols as well as numbers. \pause
    \item[]
    \item How much of the ``math" we do is actually a set of clerical tasks?
    
\end{itemize}
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
\frametitle{Free and open source} \pause
%\framesubtitle{} 

The \texttt{SageMath} website is \href{http://www.sagemath.org}{\texttt{http://www.sagemath.org}}. \pause

\begin{itemize}
    \item There are commercial versions like 
        \begin{itemize}
            \item Mathematica (\href{http://www.wolfram.com}{http://www.wolfram.com})
            \item Maple (\href{http://www.maplesoft.com} {http://www.maplesoft.com})
        \end{itemize}\pause
    \item \texttt{SageMath} is not the only free alternative. \pause
    \item It includes quite a few of the others. \pause
    \item Python based. \pause
    \item On your computer, or use \texttt{SageMathCloud}. 
\end{itemize}
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
\frametitle{\texttt{SageMath} on your computer: A free download} \pause
%\framesubtitle{} 
\begin{itemize}
    \item It's a big download. \pause
    \item Native versions for Mac and linux. \pause
    \item Windows version lives in a virtual linux machine. \pause
    \item First download Oracle's VirtualBox.
\end{itemize}
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
\frametitle{Web browser interface} \pause 
%\framesubtitle{} 
When you start up for the first time you will see something like this: \pause 
\begin{center}
\includegraphics[width=4in]{Graphics/shot1}
\end{center} \pause 

\vspace{4mm}

Click on New Worksheet.
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
\frametitle{Give it a name} 
%\framesubtitle{} 
\begin{center}
\includegraphics[width=4in]{Graphics/shot2}
\end{center} 
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
\frametitle{Check Typeset} 
%\framesubtitle{} 
\begin{center}
\includegraphics[width=4in]{Graphics/shot3}
\end{center} \pause
Click in the box.
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
\frametitle{Click in the box: \underline{evaluate} appears} 
%\framesubtitle{} 
\begin{center}
\includegraphics[width=4in]{Graphics/shot4}
\end{center} \pause 
\vspace{3mm}
Type code in the box and click \underline{evaluate}.
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
\frametitle{Click \underline{evaluate}} 
%\framesubtitle{} 
\begin{center}
\includegraphics[width=4in]{Graphics/shot5}
\end{center} \pause
%\vspace{3mm}

Type more code in the next box.
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
\frametitle{Keep going} 
%\framesubtitle{} 
\begin{center}
\includegraphics[width=4in]{Graphics/shot6}
\end{center} 
\vspace{3mm}
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
\frametitle{Integer arithmetic} 
%\framesubtitle{} 
\begin{center}
\includegraphics[width=4in]{Graphics/shot7}
\end{center} 
\vspace{3mm}
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}[fragile]
\frametitle{Same calculation with R}
\framesubtitle{\texttt{SageMath}'s answer was 11652140094042840181048771286026412160}  \pause
{\color{blue}
\begin{verbatim}
> prod(1:100)/(prod(1:60)*prod(1:30)*prod(1:10))
\end{verbatim}
} % End color
\begin{verbatim}
[1] 1.165214e+37
\end{verbatim} 
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
\frametitle{Charlotte's homework problem} \pause
%\framesubtitle{} 
{\LARGE
\begin{displaymath}
    \frac{4x}{4x^2-8x+7} + \frac{3x}{4x^2-10x+7} = 1
\end{displaymath} \pause

{\footnotesize Note $16 \times 7 = 112$.}  \pause

\begin{displaymath}
    16x^4 - 100x^3 + 200x^2 - 175x + 49 = 0
\end{displaymath}
} % End size
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}[fragile]
\frametitle{Use \texttt{SageMath}}
\framesubtitle{Solve $16x^4 - 100x^3 + 200x^2 - 175x + 49 = 0$} \pause
%{\small % or footnotesize or scriptsize
\begin{tabular}{|l|}  \hline 
\begin{minipage}{4in}
\begin{verbatim}

f = 16*x^4 - 100*x^3 + 200*x^2 - 175*x + 49
factor(f)

\end{verbatim}
\end{minipage}
 \\     \hline
\end{tabular} 
\vspace{3mm}

% \noindent
{\color{blue}\underline{evaluate}} \pause

\vspace{3mm}
{\color{blue}
${\left(4 \, x^{2} - 9 \, x + 7\right)} {\left(2 \, x - 1\right)}
{\left(2 \, x - 7\right)}$
} % End colour
%} % End size
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}[fragile]
\frametitle{Solve directly}
\framesubtitle{$ \frac{4x}{4x^2-8x+7} + \frac{3x}{4x^2-10x+7} = 1$} \pause
%{\small % or footnotesize or scriptsize
\begin{tabular}{|l|}  \hline 
\begin{minipage}{4in}
\begin{verbatim}

eq = 4*x/(4*x^2-8*x+7) + 3*x/(4*x^2-10*x+7) == 1
solve(eq,x) # Solve eq for x

\end{verbatim}
\end{minipage}
 \\     \hline
\end{tabular} 
\vspace{3mm}

% \noindent
{\color{blue}\underline{evaluate}} \pause

\vspace{3mm}
{\color{blue}
$\left[x = \left(\frac{1}{2}\right), x = \left(\frac{7}{2}\right), x =
-\frac{1}{8} i \, \sqrt{31} + \frac{9}{8}, x = \frac{1}{8} i \,
\sqrt{31} + \frac{9}{8}\right]$
} % End colour
%} % End size
\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}[fragile]
\frametitle{Derivatives}
\framesubtitle{} \pause

Calculate $\frac{\partial^3}{\partial x^3} \left(\frac{e^{4x}}{1+e^{4x}}\right)$. \pause This is something you could do by hand, but would you want to? \pause

{\small % or footnotesize or scriptsize
\begin{tabular}{|l|}  \hline 
\begin{minipage}{4.5in}
\begin{verbatim}

f(x) = exp(4*x)/(1+exp(4*x))
derivative(f(x),x,3) # Derivative with respect to x, 3 times  

\end{verbatim}
\end{minipage}
 \\     \hline
\end{tabular} 
\vspace{3mm}

% \noindent
{\color{blue}\underline{evaluate}} \pause

\vspace{3mm}
{\color{blue}
$\frac{64 \, e^{\left(4 \, x\right)}}{e^{\left(4 \, x\right)} + 1} -
\frac{448 \, e^{\left(8 \, x\right)}}{{\left(e^{\left(4 \, x\right)} +
1\right)}^{2}} + \frac{768 \, e^{\left(12 \,
x\right)}}{{\left(e^{\left(4 \, x\right)} + 1\right)}^{3}} - \frac{384
\, e^{\left(16 \, x\right)}}{{\left(e^{\left(4 \, x\right)} +
1\right)}^{4}}$
} % End colour
\vspace{3mm} \pause

% Another box
\begin{tabular}{|l|}  \hline 
\begin{minipage}{4in}
\begin{verbatim}

factor(_) # Factor the preceding expression  

\end{verbatim}
\end{minipage}
 \\     \hline
\end{tabular} 
\vspace{3mm}

% \noindent
{\color{blue}\underline{evaluate}} \pause

\vspace{3mm}
{\color{blue}
$\frac{64 \, {\left(e^{\left(8 \, x\right)} - 4 \, e^{\left(4 \,
x\right)} + 1\right)} e^{\left(4 \, x\right)}}{{\left(e^{\left(4 \,
x\right)} + 1\right)}^{4}}$
} % End colour
} % End size
\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\begin{frame}[fragile] % A SageMath frame
\frametitle{Look at $f(x)$ again} \pause
% \framesubtitle{} \pause
%{\small % or footnotesize or scriptsize
\begin{tabular}{|l|}  \hline 
\begin{minipage}{4in}
\begin{verbatim}

f(x)

\end{verbatim}
\end{minipage}
 \\     \hline
\end{tabular} 
\vspace{3mm}

% \noindent
{\color{blue}\underline{evaluate}} \pause

\vspace{3mm}
{\color{blue}
$	\frac{e^{\left(4 \, x\right)}}{e^{\left(4 \, x\right)} + 1}$ \pause
} % End colour
%} % End size

\vspace{5mm}
It looks like a cumulative distribution function. Is it?
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\begin{frame}[fragile] % A SageMath frame
\frametitle{Check $\displaystyle{\lim_{x \rightarrow -\infty}f(x)}$ and 
              $\displaystyle{\lim_{x \rightarrow \infty}f(x)}$}\pause
% \framesubtitle{} 

\begin{tabular}{|l|}  \hline
\verb:limit(f(x),x=-Infinity);limit(f(x),x=Infinity)     :  \\ \hline
\end{tabular} 

\vspace{1mm}
{\color{blue}\underline{evaluate}}  \pause

{\color{blue}
\noindent
\begin{tabular}{l}  
$0$  \\ 
$1$
\end{tabular} \\  } \pause 
\vspace{3mm}

Or,

\vspace{3mm}
\noindent
\begin{tabular}{|l|}  \hline
\verb:show(limit(f(x),x=-Infinity))                      :  \\ 
\verb:show(limit(f(x),x=Infinity))                       :  \\ \hline
\end{tabular} 
\vspace{1mm}

{\color{blue}\underline{evaluate} \vspace{1mm} \pause

\noindent
\begin{tabular}{l}  
$0$  \\ \\
$1$
\end{tabular} \\    }
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}[fragile]
\frametitle{The (single) derivative of $f(x)$ is a density. }
\framesubtitle{Since $f(x)$ is a cumulative distribution function} \pause

\begin{tabular}{|l|}  \hline
\verb:derivative(f(x),x)                                 :  \\ \hline
\end{tabular}

\vspace{3mm}
\noindent
{\color{blue}\underline{evaluate} \pause

\vspace{3mm}

$  4 \, \frac{e^{\left(4 \, x\right)}}{{\left(e^{\left(4 \, x\right)} +
1\right)}} - 4 \, \frac{e^{\left(8 \, x\right)}}{{\left(e^{\left(4 \,
x\right)} + 1\right)}^{2}}  
$   } \pause

\vspace{3mm}
\noindent
\begin{tabular}{|l|}  \hline
\verb:# Another way                                      :  \\ 
\verb:f(x).derivative(x)                                 :  \\ \hline
\end{tabular}

\vspace{3mm}
\noindent
{\color{blue}\underline{evaluate}} \pause

\vspace{3mm}
{\color{blue}$\frac{4 \, e^{\left(4 \, x\right)}}{e^{\left(4 \, x\right)} + 1} -
\frac{4 \, e^{\left(8 \, x\right)}}{{\left(e^{\left(4 \, x\right)} +
1\right)}^{2}}$}
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]
\frametitle{Simplify and save the density} \pause
%\framesubtitle{} 
\begin{tabular}{|l|}  \hline
\verb:g(x) = factor(f(x).derivative(x)); g(x)            :  \\ \hline
\end{tabular} 

\vspace{3mm}
\noindent
{\color{blue}\underline{evaluate}} \pause
\vspace{3mm}

{\color{blue}$ \frac{4 \, e^{\left(4 \, x\right)}}{{\left(e^{\left(4 \, x\right)} +
1\right)}^{2}}$ }
\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]
\frametitle{Plot $g(x) = \frac{4 \, e^{\left(4 \, x\right)}}{{\left(e^{\left(4 \, x\right)} +
1\right)}^{2}}$} \pause
%\framesubtitle{} 

% kurve = plot(g(x),x,-5,5);
% kurve.save(filename='kurve.pdf')

\vspace{3mm}
\noindent
\begin{tabular}{|l|}  \hline
\verb:plot(g(x),x,-5,5)                                  :  \\ \hline
\end{tabular} 

\vspace{3mm}
\noindent
{\color{blue}\underline{evaluate}} \pause

\begin{center}
\includegraphics[width=3.5in]{Graphics/logisticdensity}
\end{center}
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]
\frametitle{Is $g(x)$ symmetric around zero?} \pause
%\framesubtitle{} 
If so, then expected value = median = 0. \pause
\vspace{5mm}
\vspace{3mm}
\noindent
\begin{tabular}{|l|}  \hline
\verb:f(0)                                              :  \\ \hline
\end{tabular} 

\vspace{3mm}
\noindent
{\color{blue}\underline{evaluate} \pause

\vspace{3mm}
$\frac{1}{2}$}
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]
\frametitle{Symmetry about zero means $g(x)=g(-x)$} \pause
%\framesubtitle{} 
\begin{tabular}{|l|}  \hline
\verb:g(x)-g(-x)                                     :  \\ \hline
\end{tabular} 

\vspace{3mm}
\noindent
{\color{blue}\underline{evaluate} \pause

\vspace{3mm}
$  \frac{4 \, e^{\left(4 \, x\right)}}{{\left(e^{\left(4 \, x\right)} +
1\right)}^{2}} - \frac{4 \, e^{\left(-4 \, x\right)}}{{\left(e^{\left(-4
\, x\right)} + 1\right)}^{2}}$   } \pause

\vspace{3mm}
\noindent
\begin{tabular}{|l|}  \hline
\verb:factor(g(x)-g(-x))                              :  \\ \hline
\end{tabular} 

\vspace{3mm}
\noindent
{\color{blue}\underline{evaluate} \pause

\vspace{3mm}
$0$ }
\vspace{3mm} \pause

\noindent
Is this right? \pause Yes. To see it, \pause just multiply numerator and denominator of 
$g(-x) = \frac{4 \, e^{-4 \, x}}{{\left(e^{-4 \, x} + 1\right)}^{2}}$ by $e^{8x}$\pause, 
obtaining $g(x) = \frac{4 \, e^{4 \, x}}{{\left(e^{4 \, x} + 1\right)}^{2}}$.
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]
\frametitle{Replace 4 with $\theta$ in $f(x) = \frac{e^{4 \, x}}{e^{4 \, x} + 1}$} \pause
%\framesubtitle{} 
Need to declare any symbolic variable other than $x$. \pause
\vspace{12mm}

\noindent
\begin{tabular}{|l|}  \hline
\verb:var('theta')                                      :  \\ 
\verb:F(x) = exp(theta*x)/(1+exp(theta*x)); F(x)        :  \\ \hline
\end{tabular} 

\vspace{3mm}
\noindent
{\color{blue}\underline{evaluate}} \pause

\vspace{3mm}
{\color{blue}$\frac{e^{\left(\theta x\right)}}{e^{\left(\theta x\right)} + 1}$}
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]
\frametitle{Is $F(x) = \frac{e^{\theta \, x}}{e^{\theta \, x} + 1}$ a distribution function?} \pause
%\framesubtitle{$F(x) = $} 

\vspace{3mm}
\noindent
\begin{tabular}{|l|}  \hline
\verb:limit(F(x),x=-Infinity)                              :  \\  \hline
\end{tabular} 

\vspace{3mm}
\noindent
{\color{blue}\underline{evaluate} \pause
\begin{verbatim}
Traceback (click to the left of this block for traceback)
...
Is  theta  positive, negative, or zero?
\end{verbatim}  } \pause

\begin{itemize}
    \item Good question! \pause
    \item Actually, the question is asked by the excellent open-source calculus program \texttt{Maxima}\pause, and \texttt{Sage} relays the question. \pause
    \item  In \texttt{Maxima}, you could answer the question interactively. \pause
    \item In \texttt{SageMath}, click in the box and edit the code.
\end{itemize}
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]
\frametitle{Assume $\theta>0$} \pause
%\framesubtitle{} 
\begin{tabular}{|l|}  \hline
\verb:assume(theta>0)                                   :  \\ 
\verb:F(x).limit(x=-oo); F(x).limit(x=oo)               :  \\ \hline
\end{tabular} 

\vspace{3mm}
\noindent
{\color{blue}\underline{evaluate} \pause

\noindent
\begin{tabular}{l}  
$0$  \\ 
$1$ 
\end{tabular} }
\vspace{5mm} \pause

Differentiate to get the density. \pause
\vspace{3mm}

\begin{tabular}{|l|}  \hline
\verb:# New f(x) will replace the old one.              :  \\
\verb:f(x) = factor(F(x).derivative(x)); f(x)           :  \\ \hline
\end{tabular} 

\vspace{3mm}
\noindent
{\color{blue}\underline{evaluate} } \pause

\vspace{3mm}
{\color{blue}$\frac{\theta e^{\left(\theta x\right)}}{{\left(e^{\left(\theta x\right)}
+ 1\right)}^{2}}$}
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]
\frametitle{$f(x)$ is symmetric about zero.}
\framesubtitle{So the expected value must be zero.}  \pause

\begin{tabular}{|l|}  \hline
\verb:factor(f(x)-f(-x)) # Checking symmetry            :  \\ \hline
\end{tabular} 

\vspace{3mm}
\noindent
{\color{blue}\underline{evaluate} \pause

$0$} \vspace{6mm} \pause

\begin{tabular}{|l|}  \hline
\verb:# Expected value                                  :  \\ 
\verb:integrate(x*f(x),x,-oo,oo)                        :  \\ \hline
\end{tabular} 

\vspace{3mm}
\noindent
{\color{blue}\underline{evaluate} \pause

$0$ } \vspace{3mm} \pause

The variance emerges in terms of an obscure function called the \texttt{polylog}. The calculation will not be shown.
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}[fragile]
\frametitle{Add a location parameter} \pause
%\framesubtitle{} 
\begin{tabular}{|l|}  \hline
\verb:var('mu')                                            :  \\ 
\verb:F(x) = exp(theta*(x-mu))/(1+exp(theta*(x-mu))); F(x) :  \\ \hline
\end{tabular} 

\vspace{3mm}
\noindent
{\color{blue}\underline{evaluate}} \pause

\vspace{3mm}
{\color{blue}$\frac{e^{\left(-{\left(\mu - x\right)}
\theta\right)}}{e^{\left(-{\left(\mu - x\right)} \theta\right)} + 1}$} \pause
\vspace{3mm}

I can't control the order of variables in \texttt{SageMath} output. It looks alphabetical, with the \texttt{m} in \texttt{mu} coming before $x$.
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}[fragile]
\frametitle{The density is tasteless} \pause
\framesubtitle{As \texttt{SageMath} writes it} 

\begin{tabular}{|l|}  \hline
\verb:f(x) = factor( F(x).derivative(x) ); f(x) :  \\ \hline
\end{tabular} 

\vspace{3mm}
\noindent
{\color{blue}\underline{evaluate}} \pause

\vspace{3mm}
{\color{blue}$\frac{\theta e^{\left(\mu \theta + \theta x\right)}}{{\left(e^{\left(\mu
\theta\right)} + e^{\left(\theta x\right)}\right)}^{2}}$} \pause
\vspace{5mm}

By hand, \pause

\begin{eqnarray*}
    f(x) & = & \frac{\theta e^{\left(\mu \theta + \theta x\right)}} 
                    {{\left(e^{\mu\theta} + e^{\theta x}\right)}^{2}} \pause
                    ~ \cdot ~ \frac{e^{-2\mu\theta}}{e^{-2\mu\theta}} \\ \pause
        & = & \cdots \\ \pause
        & = &  \frac{\theta e^{\theta\left(x-\mu\right)}}
                    {{\left(e^{\theta\left(x-\mu\right)} + 1\right)}^{2}} 
\end{eqnarray*} 
\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
\frametitle{Simulating from the distribution}
\framesubtitle{} 
\begin{itemize}
    \item A good source of cute homework problems.   \pause
    \item Give students the density $f(x) = \frac{\theta e^{\theta\left(x+\mu\right)}} 
                    {{\left(e^{\theta x} + e^{\theta \mu}\right)}^{2}} $.  \pause
    %\item Even showing it's a density is no joke. \pause
    \item $\mu$ can be estimated with $\overline{x}$ but it's not obvious.  \pause
    \item $\theta$ has to be estimated numerically. \pause
    \item Need to generate some data from this distribution.
\end{itemize}
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
\frametitle{Simulation by the inverse CDF method}
\framesubtitle{A well-known rule} \pause 
\begin{itemize}
    \item Let $F(x)$ be the CDF of a continuous random variable \pause with $F \uparrow$ on its support\pause, so that $F^{-1}$ exists. \pause
    \item If $U \sim U(0,1)$\pause, then $X = F^{-1}(U) \sim F(x)$. \pause 
    \item In words, \pause if $U$ is a random variable with a uniform density on the interval $(0,1)$ \pause and $F(x)$ is the cumulative distribution function of a continuous random variable\pause, then if you transform $U$ with the \emph{inverse}  of $F(x)$\pause, the result is a random variable with distribution function $F(x)$. 
\end{itemize}
\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]
\frametitle{Of course you could do this by hand, but \ldots} \pause
%\framesubtitle{} 
\begin{tabular}{|l|}  \hline
\verb:# Inverse of cdf                                     :  \\ 
\verb:var('X U')                                           :  \\ 
\verb:solve(F(X)==U,X) # Solve F(X)=U for X                :  \\ \hline
\end{tabular} 

\vspace{3mm}
\noindent
{\color{blue}\underline{evaluate}} \pause

\vspace{3mm}
{\color{blue}$\left[X = \frac{\mu \theta + \log\left(-\frac{U}{U -
1}\right)}{\theta}\right]$} \pause
\vspace{5mm}

It might be a bit better to write this as \pause
\begin{displaymath}
    X = \mu + \frac{1}{\theta} \log \left(\frac{U}{1-U}\right),
\end{displaymath} \pause
but what \texttt{Sage} gives us is quite nice.
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}[fragile]
\frametitle{Simulate data from $F(x)$ using \texttt{R}}
\framesubtitle{$X = \mu + \frac{1}{\theta} \log \left(\frac{U}{1-U}\right)$}  \pause
{\footnotesize % or scriptsize
{\color{blue}
\begin{verbatim}
> n = 20; mu = -2; theta = 4
> U = runif(n)
> X = mu + log(U/(1-U))/theta; X
\end{verbatim} \pause
} % End color
\begin{verbatim}
 [1] -1.994528 -2.455775 -2.389822 -2.996261 -1.477381 -2.422011 -1.855653
 [8] -2.855570 -2.358733 -1.712423 -2.075641 -1.908347 -2.018621 -2.019441
[15] -1.956178 -2.015682 -2.846583 -1.727180 -1.726458 -2.207717
\end{verbatim} 
} % End size
\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]
\frametitle{Some symbolic variables do not need to be declared} \pause
%\framesubtitle{} 
{\footnotesize
\noindent
\begin{tabular}{|l|}  \hline
\verb:pi                                                :  \\ \hline
\end{tabular} 

\vspace{3mm}
\noindent
{\color{blue}\underline{evaluate} \pause

\vspace{2mm}
$\pi$ } \pause

\vspace{2mm}
Is that really the ratio of a circle's circumference to its diameter, or just the Greek letter? \pause

\vspace{3mm}
\noindent
\begin{tabular}{|l|}  \hline
\verb:cos(pi)                                           :  \\ \hline
\end{tabular} 

\vspace{3mm}
\noindent
{\color{blue}\underline{evaluate} \pause

\vspace{2mm}
$-1$ }
\vspace{2mm}

That's pretty promising. Evaluate it numerically. \pause
\vspace{3mm}

\begin{tabular}{|l|}  \hline
\verb:n(pi) # Could also say pi.n()                     :  \\ \hline
\end{tabular} 

\vspace{3mm}
\noindent
{\color{blue}\underline{evaluate} \pause

\vspace{2mm}
$3.14159265358979$ }
\vspace{2mm}

}% End size
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]
\frametitle{Try using \texttt{pi} in the normal distribution} \pause
%\framesubtitle{} 
{\footnotesize
\begin{tabular}{|l|}  \hline 
\verb:# Normal density                                              :  \\ 
\verb:var('mu, sigma')                                              :  \\   \pause
\verb:assume(sigma>0)                                               :  \\  \pause
\verb:f(x) = 1/(sigma*sqrt(2*pi)) * exp(-(x-mu)^2/(2*sigma^2)); f(x):  \\ \hline
\end{tabular} 

\vspace{3mm}
\noindent
{\color{blue}\underline{evaluate}} \pause

\vspace{3mm}
{\color{blue}$\frac{\sqrt{2} e^{\left(-\frac{{\left(\mu - x\right)}^{2}}{2 \,
\sigma^{2}}\right)}}{2 \, \sqrt{\pi} \sigma}$}
\vspace{3mm} \pause

\begin{tabular}{|l|}  \hline
\verb:# Integrate the density                               :  \\ 
\verb:integrate(f(x),x,-oo,oo)                              :  \\ \hline                            
\end{tabular} 

\vspace{3mm}
\noindent
{\color{blue} \underline{evaluate} \pause

\vspace{2mm}
$1$ }
}% End size
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]
\frametitle{Expected value and variance of the normal} \pause
\framesubtitle{Direct calculation} 
{\footnotesize
\begin{tabular}{|l|}  \hline
\verb:# E(X)                                                :  \\ 
\verb:integrate(x*f(x),x,-oo,oo)                            :  \\ \hline 
\end{tabular} 

\vspace{3mm}
\noindent
{\color{blue} \underline{evaluate} \pause

\vspace{2mm}
$\mu$ } \pause
\vspace{2mm}

\vspace{3mm}
\noindent
\begin{tabular}{|l|}  \hline
\verb:# E(X-mu)^2 is the variance                           :  \\ 
\verb:integrate((x-mu)^2*f(x),x,-oo,oo)                     :  \\ \hline 
\end{tabular} 

\vspace{3mm}
\noindent
{\color{blue} \underline{evaluate} \pause

\vspace{2mm}
$\sigma^2$ }
}% End size
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]
\frametitle{Moment-generating function} \pause
%\framesubtitle{} 
{\footnotesize
\begin{tabular}{|l|}  \hline 
\verb:# Moment-generating function M(t) = E(e^{Xt})            :  \pause \\ 
\verb:var('t')                                                 :  \pause \\ 
\verb:M(t) = integrate(exp(x*t)*f(x),x,-oo,oo); M(t)           :  \\ \hline
\end{tabular} 

\vspace{3mm}
\noindent
{\color{blue}\underline{evaluate}} \pause

\vspace{3mm}
{\color{blue}$e^{\left(\frac{1}{2} \, \sigma^{2} t^{2} + \mu t\right)}$} \pause
\vspace{3mm}


\vspace{3mm}
\noindent
\begin{tabular}{|l|}  \hline 
\verb:# Differentiate four times, set t=0 to get E(X^4)        :  \pause \\ 
\verb:derivative(M(t),t,4)(t=0)                                :  \\ \hline
\end{tabular} 

\vspace{3mm}
\noindent
{\color{blue}\underline{evaluate}} \pause

\vspace{3mm}
{\color{blue}$\mu^{4} + 6 \, \mu^{2} \sigma^{2} + 3 \, \sigma^{4}$}

}% End size
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]
\frametitle{Geometric distribution} \pause
%\framesubtitle{} 
A coin with $Pr\{\mbox{Head}\} = \theta$ is tossed repeatedly, and $X$ is the number of tosses required to get the first head. \pause \vspace{5mm}

{\footnotesize
\begin{tabular}{|l|}  \hline 
\verb:# Geometric                                              :  \\ 
\verb:var('theta')                                             :  \\ 
\verb:assume(0<theta); assume(theta<1)  # Note two statements  :  \\  \pause
\verb:p(x) = theta*(1-theta)^(x-1); p(x)                       :  \\  \pause
\verb:p(x).sum(x,1,oo)                 # Sum the pmf           :  \\ \hline
\end{tabular} 

\vspace{3mm}
\noindent
{\color{blue}\underline{evaluate}}  \pause

\vspace{3mm}
{\color{blue}$1$}
}% End size
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]
\frametitle{Expected value and variance of the Geometric} \pause
%\framesubtitle{} 

{\footnotesize
\begin{tabular}{|l|}  \hline 
\verb:(x*p(x)).sum(x,1,oo)             # Expected value        : \\  \hline
\end{tabular} 

\vspace{3mm}
\noindent
{\color{blue}\underline{evaluate}}

\vspace{3mm}
{\color{blue}$\frac{1}{\theta}$ \pause
} % End colour
\vspace{5mm}

\begin{tabular}{|l|}  \hline 
\verb:((x-1/theta)^2*p(x)).sum(x,1,oo) # Variance              :  \\  \hline
\end{tabular} 

\vspace{3mm}
\noindent
{\color{blue}\underline{evaluate}}

\vspace{3mm}
{\color{blue}$-\frac{\theta - 1}{\theta^{2}}$
} % End colour
} % End size
\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}
\frametitle{Linear algebra}
\framesubtitle{Not even scratching the surface}  \pause
\begin{itemize}
    \item The algorithm that Sage uses for a particular task will depend on the \emph{ring} (a concept from Algebra) to which the matrix belongs. \pause
    \item When the contents of a matrix are symbols, the matrix belongs to the symbolic ring, abbreviated \texttt{SR}. \pause
    \item As in Python, a matrix is a list of rows, and the rows are lists of matrix elements.
\end{itemize}
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]
\frametitle{A symbolic matrix} \pause
%\framesubtitle{} 
{\footnotesize
\begin{tabular}{|l|}  \hline 
\begin{minipage}{4in}
\begin{verbatim}

var('alpha beta gamma delta')
A = matrix( SR, [[alpha, beta],[gamma, delta]] ); A

\end{verbatim}
\end{minipage}
 \\     \hline
\end{tabular} 

\vspace{3mm}
\noindent
{\color{blue}\underline{evaluate}} \pause

\vspace{3mm}
{\color{blue}
$\left(\begin{array}{rr}
\alpha & \beta \\
\gamma & \delta
\end{array}\right)$} \pause

\vspace{2mm}
As in Python, index numbering begins with zero, not one. \pause
\vspace{2mm}

\begin{tabular}{|l|}  \hline 
\begin{minipage}{4in}
\begin{verbatim}

A[0,1]

\end{verbatim}
\end{minipage}
 \\     \hline
\end{tabular} 

\vspace{3mm}
\noindent
{\color{blue}\underline{evaluate}} \pause

\vspace{3mm}
{\color{blue}$\beta$}
}% End size
\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]
\frametitle{Multiplication by a scalar does what you would hope} \pause
%\framesubtitle{} 
{\footnotesize
\begin{tabular}{|l|}  \hline 
\begin{minipage}{6in}
\begin{verbatim}

a=2
a*A

\end{verbatim}
\end{minipage}
 \\     \hline
\end{tabular} 

\vspace{3mm}
\noindent
{\color{blue}\underline{evaluate}} \pause

\vspace{3mm}
{\color{blue}
$\left(\begin{array}{rr}
2 \, \alpha & 2 \, \beta \\
2 \, \gamma & 2 \, \delta
\end{array}\right)$}

}% End size
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]
\frametitle{Matrix multiplication} \pause
%\framesubtitle{} 
{\footnotesize
\begin{tabular}{|l|}  \hline 
\begin{minipage}{4in}
\begin{verbatim}

var('x11 x12 x13 x21 x22 x23')
B = matrix(SR, [[x11, x12, x13], [x21, x22, x23]])
B

\end{verbatim}
\end{minipage}
 \\     \hline
\end{tabular} 

\vspace{3mm}
\noindent
{\color{blue}\underline{evaluate}} \pause

\vspace{3mm}
{\color{blue}
$\left(\begin{array}{rrr}
x_{11} & x_{12} & x_{13} \\
x_{21} & x_{22} & x_{23}
\end{array}\right)$} \pause

\vspace{5mm}

\begin{tabular}{|l|}  \hline 
\begin{minipage}{4in}
\begin{verbatim}

C = A*B; C

\end{verbatim}
\end{minipage}
 \\     \hline
\end{tabular} 

\vspace{3mm}
\noindent
{\color{blue}\underline{evaluate}} \pause

\vspace{3mm}
{\color{blue}
$\left(\begin{array}{rrr}
\alpha x_{11} + \beta x_{21} & \alpha x_{12} + \beta x_{22} &
\alpha x_{13} + \beta x_{23} \\
\gamma x_{11} + \delta x_{21} & \gamma x_{12} + \delta x_{22} &
\gamma x_{13} + \delta x_{23}
\end{array}\right)$}
}% End size
\vspace{3mm} \pause

Of course the matrices must be the right size or \texttt{SageMath} raises an error.
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]
\frametitle{Matrix transpose etc.}
\framesubtitle{Using a notation that quickly becomes natural if it is not already}  \pause
{\footnotesize
\begin{tabular}{|l|}  \hline 
\begin{minipage}{6in}
\begin{verbatim}

show(A)
A.transpose()

\end{verbatim}
\end{minipage}
 \\     \hline
\end{tabular} 

\vspace{3mm}
\noindent
{\color{blue}\underline{evaluate}} \pause

\vspace{3mm}
{\color{blue}
$\left(\begin{array}{rr}
\alpha & \beta \\
\gamma & \delta
\end{array}\right)$

\vspace{3mm}

$\left(\begin{array}{rr}
\alpha & \gamma \\
\beta & \delta
\end{array}\right)$}
}% End size
\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]
\frametitle{Trace and determinant}
\framesubtitle{$A = \left(\begin{array}{rr}
\alpha & \beta \\
\gamma & \delta
\end{array}\right)$}  \pause
{\footnotesize
\begin{tabular}{|l|}  \hline 
\begin{minipage}{4in}
\begin{verbatim}

A.trace()

\end{verbatim}
\end{minipage}
 \\     \hline
\end{tabular} 

\vspace{3mm}
\noindent
{\color{blue}\underline{evaluate}} \pause

\vspace{3mm}
{\color{blue}
$\alpha + \delta$} \pause
\vspace{3mm}

\vspace{3mm}
\noindent
\begin{tabular}{|l|}  \hline 
\begin{minipage}{4in}
\begin{verbatim}

A.determinant()

\end{verbatim}
\end{minipage}
 \\     \hline
\end{tabular} 

\vspace{3mm}
\noindent
{\color{blue}\underline{evaluate}} \pause

\vspace{3mm}
{\color{blue}
$\alpha \delta - \beta \gamma$}

}% End size
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]
\frametitle{\texttt{SageMath} has a scrollbar} \pause
%\framesubtitle{} 
{\scriptsize

\begin{tabular}{|l|}  \hline 
\begin{minipage}{4.5in}
\begin{verbatim}

D = C.transpose() # C is 2x3, D is 3x2
E = (C*D).inverse()   # Inverse of C*D
factor(E) # E is HUGE! This is not as bad. Factor is a good way to simplify.

\end{verbatim}
\end{minipage}
 \\     \hline
\end{tabular} 

\vspace{3mm}
\noindent
{\color{blue}\underline{evaluate}} \pause

\vspace{3mm}
{\color{blue}
$\left(\begin{array}{rr}
\frac{\gamma^{2} x_{11}^{2} + \gamma^{2} x_{12}^{2} + \gamma^{2}
x_{13}^{2} + 2 \, \delta \gamma x_{11} x_{21} + \delta^{2} x_{21}^{2} +
2 \, \delta \gamma x_{12} x_{22} + \delta^{2} x_{22}^{2} + 2 \, \delta
\gamma x_{13} x_{23} + \delta^{2} x_{23}^{2}}{{\left(x_{12}^{2}
x_{21}^{2} + x_{13}^{2} x_{21}^{2} - 2 \, x_{11} x_{12} x_{21} x_{22} +
x_{11}^{2} x_{22}^{2} + x_{13}^{2} x_{22}^{2} - 2 \, x_{11} x_{13}
x_{21} x_{23} - 2 \, x_{12} x_{13} x_{22} x_{23} + x_{11}^{2} x_{23}^{2}
+ x_{12}^{2} x_{23}^{2}\right)} {\left(\alpha \delta - \beta
\gamma\right)}^{2}} & -\frac{\alpha \gamma x_{11}^{2} + \alpha
\gamma x_{12}^{2} + \alpha \gamma x_{13}^{2} + \alpha \delta x_{11}
x_{21} + \beta \gamma x_{11} x_{21} + \beta \delta x_{21}^{2} + \alpha
\delta x_{12} x_{22} + \beta \gamma x_{12} x_{22} + \beta \delta
x_{22}^{2} + \alpha \delta x_{13} x_{23} + \beta \gamma x_{13} x_{23} +
\beta \delta x_{23}^{2}}{{\left(x_{12}^{2} x_{21}^{2} + x_{13}^{2}
x_{21}^{2} - 2 \, x_{11} x_{12} x_{21} x_{22} + x_{11}^{2} x_{22}^{2} +
x_{13}^{2} x_{22}^{2} - 2 \, x_{11} x_{13} x_{21} x_{23} - 2 \, x_{12}
x_{13} x_{22} x_{23} + x_{11}^{2} x_{23}^{2} + x_{12}^{2}
x_{23}^{2}\right)} {\left(\alpha \delta - \beta \gamma\right)}^{2}} \\
-\frac{\alpha \gamma x_{11}^{2} + \alpha \gamma x_{12}^{2} + \alpha
\gamma x_{13}^{2} + \alpha \delta x_{11} x_{21} + \beta \gamma x_{11}
x_{21} + \beta \delta x_{21}^{2} + \alpha \delta x_{12} x_{22} + \beta
\gamma x_{12} x_{22} + \beta \delta x_{22}^{2} + \alpha \delta x_{13}
x_{23} + \beta \gamma x_{13} x_{23} + \beta \delta
x_{23}^{2}}{{\left(x_{12}^{2} x_{21}^{2} + x_{13}^{2} x_{21}^{2} - 2 \,
x_{11} x_{12} x_{21} x_{22} + x_{11}^{2} x_{22}^{2} + x_{13}^{2}
x_{22}^{2} - 2 \, x_{11} x_{13} x_{21} x_{23} - 2 \, x_{12} x_{13}
x_{22} x_{23} + x_{11}^{2} x_{23}^{2} + x_{12}^{2} x_{23}^{2}\right)}
{\left(\alpha \delta - \beta \gamma\right)}^{2}} & \frac{\alpha^{2}
x_{11}^{2} + \alpha^{2} x_{12}^{2} + \alpha^{2} x_{13}^{2} + 2 \, \alpha
\beta x_{11} x_{21} + \beta^{2} x_{21}^{2} + 2 \, \alpha \beta x_{12}
x_{22} + \beta^{2} x_{22}^{2} + 2 \, \alpha \beta x_{13} x_{23} +
\beta^{2} x_{23}^{2}}{{\left(x_{12}^{2} x_{21}^{2} + x_{13}^{2}
x_{21}^{2} - 2 \, x_{11} x_{12} x_{21} x_{22} + x_{11}^{2} x_{22}^{2} +
x_{13}^{2} x_{22}^{2} - 2 \, x_{11} x_{13} x_{21} x_{23} - 2 \, x_{12}
x_{13} x_{22} x_{23} + x_{11}^{2} x_{23}^{2} + x_{12}^{2}
x_{23}^{2}\right)} {\left(\alpha \delta - \beta \gamma\right)}^{2}}
\end{array}\right)$
} % End colour
}% End size
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]
\frametitle{A smaller example}
\framesubtitle{Recall $\mathbf{A} = 
\left(\begin{array}{rr}
\alpha & \beta \\
\gamma & \delta
\end{array}\right)$}  \pause
{\scriptsize
\begin{tabular}{|l|}  \hline 
\begin{minipage}{4.5in}
\begin{verbatim}

A.inverse() # Here is something we can look at without a scrollbar.

\end{verbatim}
\end{minipage}
 \\     \hline
\end{tabular} 

\vspace{3mm}
\noindent
{\color{blue}\underline{evaluate}} \pause

\vspace{3mm}
{\color{blue}
$\left(\begin{array}{rr}
\frac{1}{\alpha} + \frac{\beta \gamma}{\alpha^{2} {\left(\delta -
\frac{\beta \gamma}{\alpha}\right)}} & -\frac{\beta}{\alpha
{\left(\delta - \frac{\beta \gamma}{\alpha}\right)}} \\
-\frac{\gamma}{\alpha {\left(\delta - \frac{\beta
\gamma}{\alpha}\right)}} & \frac{1}{\delta - \frac{\beta
\gamma}{\alpha}}
\end{array}\right)$} \pause
\vspace{3mm}

\vspace{3mm}
\noindent
\begin{tabular}{|l|}  \hline 
\begin{minipage}{4.5in}
\begin{verbatim}

Ainverse = factor(_) # Factor the last expression.
Ainverse

\end{verbatim}
\end{minipage}
 \\     \hline
\end{tabular} 

\vspace{3mm}
\noindent
{\color{blue}\underline{evaluate}} \pause

\vspace{3mm}
{\color{blue}
$\left(\begin{array}{rr}
\frac{\delta}{\alpha \delta - \beta \gamma} & -\frac{\beta}{\alpha
\delta - \beta \gamma} \\
-\frac{\gamma}{\alpha \delta - \beta \gamma} & \frac{\alpha}{\alpha
\delta - \beta \gamma}
\end{array}\right)$}
\vspace{3mm}

Notice the assumption that $\alpha\delta \neq \beta\gamma$. This is typical behaviour, and usually what you want.
}% End size
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]
\frametitle{It's easy to get at parts of an expression.} \pause
%\framesubtitle{} 
Recall $A^{-1} = \left(\begin{array}{rr}
\frac{\delta}{\alpha \delta - \beta \gamma} & -\frac{\beta}{\alpha
\delta - \beta \gamma} \\
-\frac{\gamma}{\alpha \delta - \beta \gamma} & \frac{\alpha}{\alpha
\delta - \beta \gamma}
\end{array}\right)$ \pause
\vspace{10mm}

{\footnotesize
%%%%%%%%%%%%%%%%%%%%%% Begin Sage display %%%%%%%%%%%%%%%%%%%%%%%%%%%
\vspace{3mm}
\noindent
\begin{tabular}{|l|}  \hline 
\begin{minipage}{4in}
\begin{verbatim}

denominator(Ainverse[0,1])

\end{verbatim}
\end{minipage}
 \\     \hline
\end{tabular} 

\vspace{3mm}
\noindent
{\color{blue}\underline{evaluate}}\pause

\vspace{3mm}
{\color{blue}
$\alpha \delta - \beta \gamma$
} % End colour
}% End size
\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]
\frametitle{You can treat the matrix as a function} \pause
%\framesubtitle{} 
{\footnotesize
\begin{tabular}{|l|}  \hline 
\begin{minipage}{6in}
\begin{verbatim}

Ainverse(alpha=1,gamma=2)

\end{verbatim}
\end{minipage}
 \\     \hline
\end{tabular} 

\vspace{3mm}
\noindent
{\color{blue}\underline{evaluate}} \pause

\vspace{3mm}
{\color{blue}
$\left(\begin{array}{rr}
-\frac{\delta}{2 \, \beta - \delta} & \frac{\beta}{2 \, \beta -
\delta} \\
\frac{2}{2 \, \beta - \delta} & -\frac{1}{2 \, \beta - \delta}
\end{array}\right)$}
\vspace{3mm}

This will work with any \texttt{SageMath} expression.
}% End size
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]
\frametitle{Rank} 
\framesubtitle{Minimum of number linearly independent rows, number of linearly independent columns} \pause 
Recall from earlier,\vspace{3mm}

{\footnotesize
\begin{tabular}{|l|}  \hline
\verb:C                                                          :  \\ \hline
\end{tabular} 

\vspace{3mm}
\noindent
{\color{blue}\underline{evaluate}} \pause

\vspace{3mm}
{\color{blue}
$\left(\begin{array}{rrr}
\alpha x_{11} + \beta x_{21} & \alpha x_{12} + \beta x_{22} &
\alpha x_{13} + \beta x_{23} \\
\gamma x_{11} + \delta x_{21} & \gamma x_{12} + \delta x_{22} &
\gamma x_{13} + \delta x_{23}
\end{array}\right)$
} % End colour
\vspace{3mm} \pause

\begin{tabular}{|l|}  \hline
\verb:rank(C)                                                    :  \\ \hline
\end{tabular} 

\vspace{3mm}
\noindent
{\color{blue}\underline{evaluate}} \pause
\vspace{3mm}

{\color{blue}$ 2$ }
}% End size
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]
\frametitle{Rank of a product is the minimum of ranks} \pause
\framesubtitle{$\mathbf{C} = \left(\begin{array}{rrr}
\alpha x_{11} + \beta x_{21} & \alpha x_{12} + \beta x_{22} &
\alpha x_{13} + \beta x_{23} \\
\gamma x_{11} + \delta x_{21} & \gamma x_{12} + \delta x_{22} &
\gamma x_{13} + \delta x_{23}
\end{array}\right)$} 
The matrix $\mathbf{C}^\top \mathbf{C}$ is awful to look at\pause, but since the rank of a product is the minimum of the rank of the matrices being multiplied\pause, its rank must be two \pause (with \texttt{SageMath}'s usual optimistic assumptions). \pause

\vspace{3mm}

\begin{tabular}{|l|}  \hline
\verb:rank( C.transpose()*C )                                               :  \\ \hline
\end{tabular} 

\vspace{3mm}
\noindent
{\color{blue}\underline{evaluate}} \pause
\vspace{3mm}

{\color{blue}$ 2$ }
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}[fragile] 
\frametitle{Eigenvalues}
\framesubtitle{Recall $\mathbf{A} = 
\left(\begin{array}{rr}
\alpha & \beta \\
\gamma & \delta
\end{array}\right)$}  \pause
{\footnotesize
\begin{tabular}{|l|}  \hline
\verb:A.eigenvalues() # Returns a list               :  \\ \hline
\end{tabular} 

\vspace{3mm}
\noindent
{\color{blue}\underline{evaluate}} \pause
\vspace{3mm}

{\color{blue}$\left[\frac{1}{2} \, \alpha + \frac{1}{2} \, \delta - \frac{1}{2} \,
\sqrt{\alpha^{2} - 2 \, \alpha \delta + \delta^{2} + 4 \, \beta \gamma},
\frac{1}{2} \, \alpha + \frac{1}{2} \, \delta + \frac{1}{2} \,
\sqrt{\alpha^{2} - 2 \, \alpha \delta + \delta^{2} + 4 \, \beta
\gamma}\right]$ }\pause
} % End size
\vspace{4mm}

\begin{itemize}
    \item The eigenvalues of a real symmetric matrix are real.  \pause
    \item The expression under the square root sign will be non-negative if $\mathbf{A}$ is symmetric \pause --- that is, if $\beta=\gamma$. \pause
    \item Sage doesn't care about this; imaginary numbers are fine.
\end{itemize}
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}[fragile] 
\frametitle{Sum of eigenvalues equals the trace}
\framesubtitle{For real symmetric matrices} \pause
{\small % or footnotesize or scriptsize
\begin{tabular}{|l|}  \hline
\verb:eigenA = A.eigenvalues()                           :  \\ 
\verb:eigenA                                             :  \\ \hline
\end{tabular} 

\vspace{3mm}
\noindent
{\color{blue}\underline{evaluate}} \pause
\vspace{3mm}

{\color{blue}$\left[\frac{1}{2} \, \alpha + \frac{1}{2} \, \delta - \frac{1}{2} \,
\sqrt{\alpha^{2} - 2 \, \alpha \delta + \delta^{2} + 4 \, \beta \gamma},
\frac{1}{2} \, \alpha + \frac{1}{2} \, \delta + \frac{1}{2} \,
\sqrt{\alpha^{2} - 2 \, \alpha \delta + \delta^{2} + 4 \, \beta
\gamma}\right]$  \pause
} % End colour
\vspace{4mm}

\begin{tabular}{|l|}  \hline
\verb:sum(eigenA)                                           :  \\ \hline
\end{tabular} 

\vspace{3mm}
\noindent
{\color{blue}\underline{evaluate}} \pause
\vspace{3mm}

{\color{blue}$\alpha+\delta$ 
} % End colour

} % End size
\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}
\frametitle{The story continues. This is just a taste.} \pause
%\framesubtitle{} 
\begin{itemize}
    \item Sage can save a lot of effort.  \pause
    \item It's great for working out examples that might be too painful otherwise. \pause
    \item There are functions for many areas of math. \pause
    \item If you know the math, looking at the documentation and getting \texttt{SageMath} to do it is pretty easy.
\end{itemize}
\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
\frametitle{Copyright Information}

This slide show was prepared by  \href{http://www.utstat.toronto.edu/~brunner}{Jerry Brunner},
Department of Statistical Sciences, University of Toronto. It is licensed under a 
\href{http://creativecommons.org/licenses/by-sa/3.0/deed.en_US}
     {Creative Commons Attribution - ShareAlike 3.0 Unported License}. Use any part of it as you like and share the result freely. The \LaTeX~source code is available from 
\href{http://www.utstat.toronto.edu/~brunner/workshops/sagemath} {\footnotesize \texttt{http://www.utstat.toronto.edu/$^\sim$brunner/workshops/sagemath}}

\end{frame}


\end{document}

\begin{frame}
\frametitle{}
%\framesubtitle{} 
\begin{itemize}
    \item 
    \item 
    \item 
\end{itemize}
\end{frame}
