%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Alex Clemesha, May 25, 2006
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\documentclass{beamer}
\usepackage{beamerthemesplit}
\usepackage{amsfonts}
\usepackage{epsfig}
\usepackage{color}
\usepackage{xspace}
\definecolor{dbluecolor}{rgb}{0,0,0.6}
\definecolor{dredcolor}{rgb}{.5,0.0,0.0}
\definecolor{dgreencolor}{rgb}{0,0.4,0}
\definecolor{blue}{rgb}{.02, .02, .908}
\newcommand{\dred}{\color{dredcolor}\bf}
\newcommand{\dblue}{\color{dbluecolor}\bf}
\newcommand{\dgreen}{\color{dgreencolor}\bf}
\newcommand{\field}[1]{\mathbb{#1}}

\title{Visualization of Number Theory with SAGE}
\author{Alex Clemesha}
\date{May 25 2006}
\institute{University of Washington}

\begin{document}

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

%%%

\begin{frame}[fragile]
\frametitle{Outline}

\begin{enumerate}
\item{\dblue Implementation}\\
Wrapping matplotlib classes

\item{\dblue Basic Functions}\\
Functions provided to users

\item{\dblue Plotting}\\
Making image files from SAGE functions

\item{\dblue Visualizing Number Theory (the fun part)}\\
Using SAGE mathematical and graphics functions together

\end{enumerate}

\end{frame}

%%%

\begin{frame}
\begin{itemize}
\item{\dblue Desire:}\\
 We want {\bf SAGE} to have excellent graphics capabilities\\
\item{\dblue Solution:}\\
 {\bf Gnuplot} license says: `` Permission to modify the software is granted, but not the right to
 distribute the complete modified source code.''\\
\vspace{1ex} 
{\bf PyX}, still considering, 3D capabilities look good, but no pngs, svg, but {\em not} ruled out.\\
\vspace{1ex} 
 Use {\bf matplotlib} by John Hunter (http://matplotlib.sourceforge.net)!\\
\vspace{1ex} 
...but {\bf matplotlib} provides users a very ``Matlab-like'' interface, because after all
that's what it was designed to do. So instead we wrap matplotlib's classes with our own more
``Mathematica-like'' interface.
\end{itemize}
\end{frame} 

%%%

\begin{frame}[fragile]

\begin{itemize}
\item{\dblue class that handles all the data of a graphic}
\begin{verbatim}
from matplotlib.figure import Figure 
\end{verbatim}

\item{\dblue classes that handle the generation of the image files}\\
{\dblue for png files:}
\begin{verbatim}
FigureCanvasAgg
\end{verbatim}
{\dblue for ps or eps files:}
\begin{verbatim}
FigureCanvasPS
\end{verbatim}
{\dblue for svg files (svg is an XML graphics format):}
\begin{verbatim}
FigureCanvasSVG
\end{verbatim}

\end{itemize}
\end{frame}

%%%

\begin{frame}[fragile]

\frametitle{Basics functions}
\begin{itemize}
\item{plotting}
\end{itemize}
 \begin{verbatim}
   plot(f, xmin, xmin, **options)
   parametric_plot((fx, fy), xmin, xmax, **options)
   list_plot(L, **options)
 \end{verbatim}

\begin{itemize}
\item Graphics objects
\end{itemize}
 \begin{verbatim}
  circle((x, y), radius)
  disk((x, y), radius, theta1, theta2)
  line(xydata)
  point((x, y))
  polygon(xydata)
  text(string, (x, y))
 \end{verbatim}

\end{frame}

%%%

\begin{frame}[fragile]

\frametitle{Making the image files}
\begin{itemize}
\item Once you have Graphics object {\tt g} you can:
\end{itemize}
 \begin{verbatim}
 g.show(**options)
 g.save(**options) 
 \end{verbatim}
\begin{itemize}
\item If you have several Graphics objects {\tt g1,...,gn} you can:
\end{itemize}
 \begin{verbatim}
 ga = graphics_array([[g1, g2],[g3, g4]])
 ga.show(**options)
 ga.save(**options) 
 \end{verbatim}

\end{frame}

%%%

\begin{frame}[fragile]

\frametitle{User defined functions}

{\bf Two (equivalent) ways of creating user defined functions:}

\begin{itemize}
\item {Regular functions:}
\end{itemize}
 \begin{verbatim}
def zrf(t):
    return zeta(1/2 + I*t).real()

def zif(t):
    return zeta(1/2 + I*t).imag()
 \end{verbatim}
\begin{itemize}
\item {Lambda functions:}
\end{itemize}

 \begin{verbatim}
zrl = lambda t: zeta(1/2 + I*t).real()
zil = lambda t: zeta(1/2 + I*t).imag()
 \end{verbatim}

\end{frame}

%%%

\begin{frame}[fragile]

 \begin{verbatim}
p1 = plot(zrf,-25,25,rgbcolor=(0,0,1))
p2 = plot(zif,-25,25,rgbcolor=(1,0,0))
(p1 + p2).save('zeta.png',figsize=[4,4])
 \end{verbatim}

\begin{figure}[hbp]
\includegraphics[width=8cm,height=6cm]{zeta}
\end{figure}

\end{frame}

%%%

\begin{frame}[fragile]
It is conjectured that all values $s = \sigma + it$ such 
that $\zeta(s) = 0$, $\sigma > 0$, have the form $s = 1/2 + it$.  Here
we look at the real part of $\zeta(s)$ versus the imaginary part.
 \begin{verbatim}
p3 = parametric_plot((zrl, zil),-25,25, \
     rgbcolor=hue(0.6),plot_points=1000)
p3.save('zetap.png',ymin=-2,ymax=2,figsize=[3,3])
 \end{verbatim}

\begin{figure}[hbp]
\includegraphics[width=6cm,height=5cm]{zetap}
\end{figure}

\end{frame}

%%%

\begin{frame}[fragile]
Proposition: Let p be a prime number congruent to 1 mod 4.  There exists
a right triangle with integer sides such that the
length of the hypotenuse is p (e.g., 5, 13, 17).  
In $\mathbb{Z}[i] = \{a + bi | a,b \in \mathbb{Z}\}$ the numbers p
lose their irreducibility, so we can factor them there. 
For example: $13^{2} = (3 + 2i)^{2}(3 - 2i)^{2} = 5^{2} + 12^{2}$
\begin{verbatim}
#prime number congruent to 1 mod 4:
L = [p for p in primes(30) if (p-1)%4 == 0]; L
[5,13,17,29]

#construct the Number Field
K.<I> = NumberField(x^2 + 1) 
K.factor_integer(13)[0][0].gens_reduced()[0]
3*I - 2
\end{verbatim}
\end{frame}

%%%

\begin{frame}[fragile]
\small
\begin{verbatim}
gg = Graphics() #empty Graphics object
gl = [] #empty list

for p in L:
    z = K.factor_integer(p)[1][0].gens_reduced()[0]
    zz = z^2
    a,b = abs(zz[0]),abs(zz[1])
    lv = [[0, 0], [a, 0], [a, b], [0, 0]]
    l = line(lv, rgbcolor=(0,0,1))
    sv = (a, b, sqrt(a^2 + b^2))
    s = "$(%s,\ %s,\ %s)$"%sv
    t = text(s, (2*a/3, b/4), fontsize=8)
    gg += l
    gl.append((l + t))
\end{verbatim}
\end{frame}

%%%

\begin{frame}
Here is the result of connecting the points we
found from the above code which factors 
prime numbers in  $\mathbb{Z}[i]$.
{\tt gg.save('triples1.png',figsize=[6,4])}
\begin{figure}[hbp]
\includegraphics[width=8cm, height=6cm]{triples1}
\end{figure}
\end{frame}

%%%

\begin{frame}
Here is a graphics\_array of the above triangles.\\
{\tt graphics\_array([gl[0:2],gl[2:4]]).save('triples2.png')}
\begin{figure}[hbp]
\includegraphics[width=11cm, height=8cm]{triples2}
\end{figure}
\end{frame}


\begin{frame}[fragile]
\small
Here we take the first 1000 coefficients of the $q-$expansion of the modular
form corresponding to the elliptic curve $y^{2} + y = x^{3} + x^{2} - 2x$.
\begin{verbatim}
    E = EllipticCurve("37a")
    ans = E.anlist(1000)
    g,h = Graphics(),Graphics()
    m = abs(max(ans))
    for i,an in zip(range(len(ans)),ans):
        c = (0,0,1) #blue
        if is_prime(i):
            c = (1,0,0) #if prime color point red
            h += point((i,an), pointsize=2, rgbcolor=c)
        g += point((i,an), pointsize=2, rgbcolor=c)
    g += plot(lambda x: 2*sqrt(x), 2, n, rgbcolor=(0,1,0))
    h += plot(lambda x: 2*sqrt(x), 2, n, rgbcolor=(0,1,0))
    g.save("ec37an.eps",figsize=[3,3])
    h.save("ec37ap.eps",figsize=[3,3])
\end{verbatim}
\end{frame}

\begin{frame}
It is a theorem by Hasse that $|a_{p}| \leq 2\sqrt{p}$ for all primes $p$. 
Prime points are red, non-prime points are blue and the line $2\sqrt{p}$ is green.
\begin{figure}[hbp]
\includegraphics[width=5cm, height=6cm]{ec37an}
\includegraphics[width=5cm, height=6cm]{ec37ap}
\end{figure}
\end{frame}

\begin{frame}[fragile]
Here is some SAGE code that draws spirals for a given constant c.
\begin{verbatim}

    c = 37/41
    g = Graphics()
    for k in range(1000):
        xr = k*cos(2*pi*c*k)
        yr = k*sin(2*pi*c*k)
        g += point((xr, yr), rgbcolor=hue(0.4+0.2*(k%2)))
    g.save('spiral.png', figsize=[6,6], draw_axis=False)

\end{verbatim}
\end{frame}


\begin{frame}
If you have any $c \in \field{Q}$ you will always
eventually see 'arms' appear, i.e., the point $(cos(2\pi\/c\/k),sin(2\pi\/c\/k))$
will repeat as $k$ runs through the integers.  For the below examples $k = 0,...,2000$.
\begin{figure}[hbp]
\includegraphics[width=5cm, height=5cm]{rspiral1}
\includegraphics[width=5cm, height=5cm]{rspiral2}
\end{figure}
first with $c = 1/5$ then with $c = 37/41$.
\end{frame}

\begin{frame}
Above code with c = $\sqrt{2}.$  Note that $\sqrt{2}$ has the continued fraction
$1+\frac{1}{2+\frac{1}{2+\frac{1}{2+..}}}$.
\begin{figure}[hbp]
\includegraphics[width=8cm, height=8cm]{sqrt2}
\end{figure}
\end{frame}

\begin{frame}
c = golden ratio. Note that the golden ratio has continued fraction.
$1+\frac{1}{1+\frac{1}{1+\frac{1}{1+..}}}$.
\begin{figure}[hbp]
\includegraphics[width=8cm, height=8cm]{golden_ratio}
\end{figure}
\end{frame}

\end{document}




