\documentclass[svgnames]{beamer}
%\usepackage{beamerarticle}
\usepackage{comment}
\usepackage{sagetex}

%\usepackage[utf8x]{inputenc}
\mode<presentation>
{
  \usetheme{Rochester}
 %\usecolortheme{crane}
 \usecolortheme{default}
 \setbeamercovered{transparent}
  % or whatever (possibly just delete it)
}%\usepackage{ngerman}

\newcommand{\page}[1]{\frame{#1}}

\usepackage{tikz}
\usepackage{tkz-graph}
\usepackage{tkz-berge}
\usetikzlibrary{arrows,shapes} 

\usepackage{graphicx}
\usepackage{pstricks}
\usepackage{color}
\usepackage{listings}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{listings}
\usepackage{xspace}  % to allow for text macros that don't eat space 
\newcommand{\field}[1]{\mathbb{#1}}

\newtheorem{thm}{Theorem}[section]

%\definecolor{dblackcolor}{rgb}{0.0,0.0,0.0}
%\definecolor{dbluecolor}{rgb}{.01,.02,0.29}
%\definecolor{dredcolor}{rgb}{0,0,0}
%\definecolor{dgraycolor}{rgb}{0.50,0.50,0.50}
%\newcommand{\dblue}{\color{dbluecolor}}
%\newcommand{\dred}{\color{dredcolor}}
%\newcommand{\dblack}{\color{dblackcolor}}

%\renewcommand{\emph}[1]{{\dblack{#1}}}

%\newcommand{\SAGE}{{\color{dbluecolor}\sf Sage}\xspace}
%\newcommand{\sage}{\Sage}
\newcommand{\ftitle}[1]{\frametitle{#1}}

\newenvironment{VerbExample}
{\example\semiverbatim}
{\endsemiverbatim\endexample}


\newcommand{\codedivider}{\hrule\medskip}

\title{$\text{Sage\TeX}=\text{Sage}+\text{\TeX}$}
%\subtitle{\url{http://www.sagemath.org}\\
%\url{http://www.ctan.org/tex-archive/help/Catalogue/entries/sagetex.html}}
\date{}
\author[Jason Grout]{Jason Grout\\Drake University\\\url{jason.grout@drake.edu}}
\titlegraphic{\includegraphics[width=0.2\textwidth]{sagelogo.png}}


\lstdefinelanguage{Sage}[]{Python}
{morekeywords={True,False,sage},
sensitive=true}

\lstset{frame=none,
          showtabs=False,
          showspaces=False,
          showstringspaces=False,
          commentstyle={\color{dredcolor}},
          keywordstyle={\color{dbluecolor}\bfseries},
          stringstyle ={\color{dgraycolor}\bfseries},
          language = Sage,
          basicstyle=\small
          }

\begin{document}

\begin{sagesilent}
from sage.graphs.graph_latex import setup_latex_preamble
setup_latex_preamble()
\end{sagesilent}

\begin{frame}[plain]
\titlepage
\end{frame}
%
%\begin{frame}{Outline}
%\tableofcontents
%\end{frame}


\begin{frame} %wstein
\ftitle{What is Sage?}
\begin{block}{Sage: Mission Statement}
Create a viable free open source alternative to Magma, Maple, Mathematica and Matlab. 
\end{block}

\end{frame}


\begin{frame}
  \ftitle{Advantages of Sage}

  \begin{itemize}
  \item Free
  \item Open-source
  \item Web notebook
  \item Industry-standard Python Language
  \item Integrates many standard open-source packages
  \item Interfaces to many commercial packages
  \end{itemize}
\end{frame}


\begin{frame}
  \ftitle{Online Notebook}
  \begin{itemize}
  \item Access Sage totally via the web
    \begin{itemize}
    \item standard web browser
    \item any operating system
    \item even cell phones!
    \end{itemize}
  \item \emph{Nothing} to install
  \item One click to collaborate and share worksheets
  \item Typesetting, 2d graphics, interactive 3d graphics
  \item Buttons, sliders, etc., to explore problems
 \end{itemize}
\end{frame}


  \begin{frame}[fragile]
\frametitle{Sage\TeX}
\TeX\ document:
\begin{verbatim}
The number 2010 factors into $\sage{factor(2010)}$.

\end{verbatim}
\codedivider
PDF Output:

   The number 2010 factors into $\sage{factor(2010)}$.
\end{frame}


\begin{frame}[plain,fragile]
\TeX\ document:
\small
\begin{verbatim}
\sageplot{plot(-x^3+7*x,(x,-5,5))}
\end{verbatim}
\normalsize
\codedivider
PDF Output: 
\begin{center}
    \sageplot{plot(-x^3+3*x^2+7*x-4,(x,-5,5))}
\end{center}
\end{frame}


\begin{frame}[fragile]
  \ftitle{How does it work?}
  
  \begin{enumerate}
  \item Make \TeX\ see the \verb|sagetex.sty| file (e.g., copy it to
    your \TeX\ file directory)
  \item \verb|pdflatex example.tex| -- Makes an \verb|example.sage|
    file containing the Sage source in your document
  \item \verb|sage example.sage| -- Runs the Sage code and creates
    output for inclusion in the document
  \item \verb|pdflatex example.tex| -- Inserts results of Sage code in PDF
  \end{enumerate}
\end{frame}



\begin{frame}
  \ftitle{Other ways to use Sage\TeX}

  \begin{itemize}
  \item An option to include the Sage output with the tex file, to
    ``freeze'' things so people don't need Sage to \TeX\ your file
  \item An option to use a remote Sage server to do the computations,
    so you don't have to have Sage installed locally
 \item Use Sage\TeX\ as a scripting language for \TeX
  \end{itemize}
\end{frame}

 \begin{frame}[fragile]
\frametitle{Write explanations}
\small
\begin{verbatim}
\begin{sagesilent}
f(x,y)=x*sin(y); grad_f=f.gradient()
\end{sagesilent}  
Let $f(x,y)=\sage{f(x,y)}$. Then $\nabla f=\sage{grad_f(x,y)}$.
\sageplot{plot_vector_field(grad_f, (x,-3,3), (y,0,3)), 
          frame=True, aspect_ratio=1}
\end{verbatim}

\codedivider

\begin{sagesilent}
f(x,y)=x*sin(y); grad_f=f.gradient()
\end{sagesilent}  
Let $f(x,y)=\sage{f(x,y)}$.  
Then $\nabla f=\sage{grad_f(x,y)}$.
\sageplot{plot_vector_field(grad_f, (x,-3,3), (y,0,3)), 
          frame=True, aspect_ratio=1}
\end{frame}

\begin{frame}[fragile]
\frametitle{Write questions}
\small
\begin{verbatim}
\begin{sagesilent}
m=identity_matrix(QQ,3)
m[0]=m[0]+m[1]
m[1]=m[1]-m[2]
m[2]=m[2]-2*m[1]
m[1]=m[1]+3*m[0]
m[0]=2*m[0]
\end{sagesilent}
Compute the rref of $\sage{m}$.
\end{verbatim}
\codedivider

\begin{sagesilent}
m=identity_matrix(QQ,3)
m[0]=m[0]+m[1]
m[1]=m[1]-m[2]
m[2]=m[2]-2*m[1]
m[1]=m[1]+3*m[0]
m[0]=2*m[0]
\end{sagesilent}
Compute the rref of $\sage{m}$.
\end{frame}


\begin{frame}[fragile]
\frametitle{Write answers}
\small 
\begin{verbatim}
\begin{sagesilent}
M=random_matrix(QQ,3,4,algorithm='echelonizable', 
    rank=3, upper_bound=10)
\end{sagesilent}
Compute the rref of $M=\sage{M}$.\\
Solution: $\mathrm{rref}(M)=\sage{M.rref()}$
\end{verbatim}
\codedivider

\begin{sagesilent}
M=random_matrix(QQ,3,4,algorithm='echelonizable', 
    rank=2, upper_bound=10)
\end{sagesilent}
Compute the rref of $M=\sage{M}$.\\
Solution: $\mathrm{rref}(M)=\sage{M.rref()}$
\end{frame}



%% Diagonalizable question example
% \begin{frame}[fragile]
% \small 
% \begin{verbatim}
% \begin{sagesilent}
% M=random_matrix(QQ,3,algorithm='diagonalizable', 
%     eigenvalues=[1,-2],dimensions=[2,1])
% D,P=M.eigenmatrix_right(); Pinv=P.inverse()
% \end{sagesilent}
% Compute the diagonalization of $M=\sage{M}$.\\
% Solution: $\sage{P*D*Pinv}=\sage{P}\sage{D}\sage{Pinv}$
% \end{verbatim}
% \codedivider

% \begin{sagesilent}
% M=random_matrix(QQ,3,algorithm='diagonalizable', 
%     eigenvalues=[1,-2],dimensions=[2,1])
% D,P=M.eigenmatrix_right(); Pinv=P.inverse()
% \end{sagesilent}
% Compute the diagonalization of $M=\sage{M}$.\\
% $\sage{P*D*Pinv}=\sage{P}\sage{D}\sage{Pinv}$
% \end{frame}




\begin{frame}[plain,fragile]
\begin{verbatim}
\begin{sagesilent}
f(x,y)=2*x^2*y+x*sec(y)+e^(-2*y)
resolution = 10
slope_field=plot_slope_field(-diff(f,x)/diff(f,y), 
  (x,-3,3),(y,-2,3.5), plot_points=resolution)
phase=sum([implicit_plot(f(x,y)+i,(x,-3,3),(y,-2,3.5), 
           plot_points=resolution*20, cmap='bone') 
          for i in [-22..22,step=2]])
\end{sagesilent}

\sageplot{slope_field+phase, aspect_ratio=1, 
  figsize=(5,5)}
\end{verbatim}

\end{frame}


\begin{frame}[plain,fragile]
    \begin{sagesilent}
   f(x,y)=2*x^2*y+x*sec(y)+e^(-2*y)
   resolution = 10
   slope_field=plot_slope_field(-diff(f,x)/diff(f,y), (x,-3,3),(y,-2,3.5),
      plot_points=resolution)
   phase=sum([implicit_plot(f(x,y)+i,(x,-3,3),(y,-2,3.5), 
      plot_points=resolution*20, cmap='bone') for i in [-22..22,step=2]])
  \end{sagesilent}

  \begin{center}
    \sageplot{slope_field+phase, aspect_ratio=1, figsize=(5,5)}
  \end{center}
\end{frame}

\begin{frame}[fragile,plain,fragile]

\begin{verbatim}
(8 pts) Solve the differential equation.  Show all work.  You may
  express the answer implicitly (i.e., you don't have to solve for
  $y$).

  \begin{equation*}
    \sage{diff(f(x,y),x)} 
    + (\sage{diff(f(x,y),y)})\frac{dy}{dx} 
    = 0
  \end{equation*}
\end{verbatim}
\codedivider
(8 pts) Solve the differential equation.  Show all work.  You may
  express the answer implicitly (i.e., you don't have to solve for
  $y$).


  \begin{equation*}
    \sage{diff(f(x,y),x)} + (\sage{diff(f(x,y),y)})\frac{dy}{dx} = 0
  \end{equation*}
  

\end{frame}




\begin{frame}[fragile]
\frametitle{3d plots}
\small
\begin{verbatim}
\begin{sagesilent}
f(x,y)=x*sin(y)+y*cos(x)
\end{sagesilent}
\sageplot[width=2.4in]{plot3d(f,(x,-2,2),(y,-2,2))}
\end{verbatim}

\codedivider

\begin{sagesilent}
f(x,y)=x*sin(y)+y*cos(x)
\end{sagesilent}
\sageplot[width=2.4in]{plot3d(f,(x,-2,2),(y,-2,2))}
\end{frame}

\begin{frame}[plain,fragile]
\small
\begin{verbatim}
\begin{sagesilent}
mathematica('myplot=Plot3D[x^2-y^2,{x,-1,1},{y,-1,1}]')
mathematica('Export["%s/graphicsfile.png", myplot]'\
 %os.getcwd())
\end{sagesilent}
\includegraphics[width=3in]{graphicsfile}
\end{verbatim}

\codedivider

\begin{sagesilent}
mathematica('myplot=Plot3D[x^2-y^2,{x,-1,1},{y,-1,1}]')
mathematica('Export["%s/graphicsfile.pdf", myplot]' % os.getcwd())
\end{sagesilent}
\includegraphics[width=3in]{graphicsfile}
\end{frame}


%\sagetexunpause

\begin{frame}[plain,fragile]
\begin{verbatim}
\begin{sagesilent}
  G=graphs.PetersenGraph(); P=G.coloring()    
\end{sagesilent}

A famous graph can be colored with 
$\sage{G.chromatic_number()}$ colors:

\sageplot[width=1in]{G.plot(partition=P)}
\end{verbatim}
\codedivider

\begin{sagesilent}
  G=graphs.PetersenGraph()
  P=G.coloring()    
\end{sagesilent}

% This will have extra space in it, unless using a version of sagetex with
% this fix: http://bitbucket.org/ddrake/sagetex/changeset/b428595d62c0
A famous graph can be colored with \sage{G.chromatic_number()} colors:

\sageplot[width=1in]{G.plot(partition=P)}
\end{frame}


\begin{frame}[plain,fragile]
\begin{verbatim}
  \begin{sagesilent}
    g = graphs.PetersenGraph() 
    g.set_latex_options(tkz_style = 'Shade')
  \end{sagesilent}
  \sage{g}
\end{verbatim}
\codedivider

  \begin{sagesilent}
    g = graphs.PetersenGraph() 
    g.set_latex_options(tkz_style = 'Shade')
  \end{sagesilent}
  \sage{g}
\footnotesize Uses yet-to-be-released options

\end{frame}


\begin{frame}[plain,fragile]
\begin{minipage}[b]{0.4\linewidth}
\begin{verbatim}
\begin{sagesilent}
H=graphs.HeawoodGraph()
H.set_latex_options(
 graphic_size=(4,4),
 vertex_size=0.2,
 edge_thickness=0.04,
 edge_color='green',
 vertex_color='green',
 vertex_label_color='red')
\end{sagesilent}

\sage{H}
 \end{verbatim}
\end{minipage}
\hfill
\begin{minipage}[b]{0.4\linewidth}
\begin{sagesilent}
H=graphs.HeawoodGraph()
H.set_latex_options(
 graphic_size=(4,4),
 vertex_size=0.2,
 edge_thickness=0.04,
 edge_color='green',
 vertex_color='green',
 vertex_label_color='red')
\end{sagesilent}

\sage{H}
\\

\footnotesize Uses yet-to-be-released options
\end{minipage}
\end{frame}


\begin{frame}[fragile]
  \ftitle{A Cayley Graph}
\begin{verbatim}
\begin{sagesilent}
G=CyclicPermutationGroup(4)
C=G.cayley_graph(generators=[G((1,2,3,4)), G((1,4,3,2))])
C.set_pos(C.layout_circular())
C.set_latex_options(graphic_size=(8,6),
vertex_shape="rectangle",
edge_labels=True)
\end{sagesilent}
\sage{C}
\end{verbatim}
\end{frame}

\begin{frame}[fragile]
  \ftitle{A Cayley Graph}
  \begin{sagesilent}
G=CyclicPermutationGroup(4)
C=G.cayley_graph(generators=[G((1,2,3,4)), G((1,4,3,2))])
C.set_pos(C.layout_circular())
C.set_latex_options(graphic_size=(8,6),vertex_shape="rectangle",
edge_labels=True)
\end{sagesilent}
\begin{center}
\sage{C}
\end{center}
\hfill\footnotesize Uses yet-to-be-released options
\end{frame}


%For when we can treat Sage as a scripting language for Latex:

\begin{frame}[fragile]
\frametitle{Generate \TeX\ using Sage}
\small
\begin{verbatim}
\begin{sagesilent}
var('x,y,n')
funcs=[x^2, x^n, sin(x),cos(x)] 
table=r"\begin{table}  \centering  \begin{tabular}{cc}"
table+=r"$f(x)$ & $f'(x)$ \\\hline" 
for f in funcs:
    table+=r"$%s$ & $%s$ \\"%(latex(f), latex(diff(f,x)))
table+=r"\end{tabular}\end{table}"
\end{sagesilent}
\sagestr{table}
\end{verbatim}

\codedivider
\begin{sagesilent}
var('x,y,n')
funcs=[x^2, x^n, sin(x),cos(x)] 
table=r"\begin{table}  \centering  \begin{tabular}{cc}"
table+=r"$f(x)$ & $f'(x)$ \\\hline" 
for f in funcs:
    table+=r"$%s$ & $%s$ \\"%(latex(f), latex(diff(f,x)))
table+=r"\end{tabular}\end{table}"
\end{sagesilent}
\sagestr{table}
\end{frame}




% \begin{frame}[fragile]
% \frametitle{Using \TeX\ counters}
% \small 
% \begin{verbatim}
% \begin{enumerate}
% \item $x^{\arabic{enumi}}\to\sage{diff(x^\arabic{enumi}, x)}$
% \item $x^{\arabic{enumi}}\to\sage{diff(x^\arabic{enumi}, x)}$
% \item $x^{\arabic{enumi}}\to\sage{diff(x^\arabic{enumi}, x)}$
% \item $x^{\arabic{enumi}}\to\sage{diff(x^\arabic{enumi}, x)}$
% \end{enumerate}
% \end{verbatim}
% \codedivider
% \begin{center}
% \begin{enumerate}
% \item $x^{\arabic{enumi}}\to\sage{diff(x^\arabic{enumi}, x)}$
% \item $x^{\arabic{enumi}}\to\sage{diff(x^\arabic{enumi}, x)}$
% \item $x^{\arabic{enumi}}\to\sage{diff(x^\arabic{enumi}, x)}$
% \item $x^{\arabic{enumi}}\to\sage{diff(x^\arabic{enumi}, x)}$
% \end{enumerate}
% \end{center}
% \end{frame}



\begin{frame}[fragile,plain]
\small
\begin{verbatim}
\begin{sagesilent}
t=Tachyon(camera_center=(0,-4,1), xres = 800, yres = 600, 
    raydepth = 12, aspectratio=.75, antialiasing = True)
t.light((0.02,0.012,0.001), 0.01, (1,0,0))
t.light((0,0,10), 0.01, (0,0,1))
t.texture('s', color=(.8,1,1), opacity=.9, specular=.95, 
    diffuse=.3, ambient=0.05)
t.texture('p', color=(0,0,1), opacity=1, specular=.2, 
    texfunc=1)
t.sphere((-1,-.57735,-0.7071),1,'s')
t.sphere((1,-.57735,-0.7071),1,'s')
t.sphere((0,1.15465,-0.7071),1,'s')
t.sphere((0,0,0.9259),1,'s')
t.plane((0,0,-1.9259),(0,0,1),'p')
\end{sagesilent}
\sageplot{t}
\end{verbatim}

\end{frame}



\begin{frame}[plain,fragile]
% \begin{sagesilent}
% t=Tachyon(camera_center=(0,-4,1), xres = 800, yres = 600, 
%     raydepth = 12, aspectratio=.75, antialiasing = True)
% t.light((0.02,0.012,0.001), 0.01, (1,0,0))
% t.light((0,0,10), 0.01, (0,0,1))
% t.texture('s', color=(.8,1,1), opacity=.9, specular=.95, 
%     diffuse=.3, ambient=0.05)
% t.texture('p', color=(0,0,1), opacity=1, specular=.2, 
%     texfunc=1)
% t.sphere((-1,-.57735,-0.7071),1,'s')
% t.sphere((1,-.57735,-0.7071),1,'s')
% t.sphere((0,1.15465,-0.7071),1,'s')
% t.sphere((0,0,0.9259),1,'s')
% t.plane((0,0,-1.9259),(0,0,1),'p')
% t.save('spheres.png')
% \end{sagesilent}
%\sageplot{t}

\includegraphics[width=0.8\textwidth]{spheres}

\end{frame}


\begin{frame}
  \frametitle{Credits and License}
  Special thanks to Dan Drake for the current Sage\TeX\ package.
\bigskip

Thanks also to Gonzalo Tornaria, Joe Wetherell, and Harald Schilly for previous
versions of Sage\TeX.  Thanks to Rob Beezer for the last three ``fancy
graph'' examples.
\bigskip

This presentation is licensed CC-by-sa.  See
\url{http://creativecommons.org/licenses/by-sa/3.0/}.

\end{frame}

\begin{frame}
\frametitle{$\text{Sage\TeX}=\text{Sage}+\text{\TeX}$}
\begin{center}  
\vfill
{ \Huge Thanks! }
\vfill
Jason Grout\\
\url{jason.grout@drake.edu}\\
\smallskip

Sage: \url{www.sagemath.org}\\
Sage\TeX: on CTAN

\end{center}
\end{frame}

\end{document}




  % \begin{frame}[fragile]
  %   \ftitle{SageTeX}

  %   \begin{example}
  %     asdf
  %   \end{example}

  %   The number 2010 factors into \sage{2010.factor()}.
    
  % \end{frame}


%   \begin{frame}
%     \ftitle{SageTeX}
    
    
%   \end{frame}
