\documentclass{memoir}
\usepackage{notestemplate}

% \begin{figure}[ht]
%     \centering
%     \incfig{riemmans-theorem}
%     \caption{Riemmans theorem}
%     \label{fig:riemmans-theorem}
% \end{figure}

\begin{document}
%\section{Prototypical Examples}
%\label{sec:prototypical_examples}
%Let us consider two analytic functions on \(\R\) :
%\begin{itemize}
%	\item \(f(x) = \frac{1}{1+x^2}\)
%	\item \(g(x) = e^{-x^2}\)
%\end{itemize}
%Their graphs look similar, but they are very different. Consider their Taylor series:
%\begin{align*}
%	f(x) = 1-x^2+x^3-x^{6}+\ldots
%\end{align*}
%and
%\begin{align*}
%	g(x) = 1-x^2+\frac{x^{4}}{2!}-\frac{x^{6}}{3!}+\ldots
%\end{align*}
%Observe that the Taylor Series of \(f(x)\) only converges for \(|x|<1\), due to the denominators. However, the Taylor Seris of \(g(x)\) converges for all \(x \in \R\).\\
%The reason \(f(x)\) has issues is the complex plane. \(g(x)\) is well-defined on the complex plane, but \(f(x)\) has holes at \(i,-i\).\\
%
%Now consider \(h(x) = \begin{cases}
%	e^{\frac{-1}{x^2}}, & x\neq 0\\
%	0, & x=0
%\end{cases}\)\\
%Once again, on the real line it looks fine, and seems perfectly differentiable. But this cannot be extended to the complex plane because as you approach zero from the imaginary axis, the function blows up.
%

\subsection{Formal Definition}
\label{sub:formal_definition}

First, we construct the field \(\C\) so that the addition and multiplication operations correspond to natural geometric transformations.

\begin{defn}[Complex Field]
	We define the \textbf{complex field} \(\C\) to be the field of ordered pairs
	\begin{align*}
		(\alpha ,\beta )
	\end{align*}
	where \(\alpha ,\beta  \in \R\). The addition and multiplication operations on \(\C\) are defined by
	\begin{align*}
		(\alpha_1,\beta_1) + (\alpha_2,\beta_2) &= (\alpha_1+\alpha_2,\beta_1+\beta_2)\\
		(\alpha_1,\beta_1)\cdot (\alpha_2,\beta_2) &= (\alpha_1\beta_1 - \alpha_2\beta_2, \alpha_1\beta_2+\alpha_2\beta_1)
	\end{align*}
	Elements of \(\C\) are called \textbf{complex numbers}. We refer to the first element of the ordered pair as the \textbf{real part} (denoted \(\textrm{Re}(\alpha ,\beta )\)), and the second element as the \textbf{imaginary part} (denoted \(\textrm{Im}(\alpha ,\beta )\) of the complex number.\\

	The identities of \(\C\) are hence
	\begin{align*}
		0_{\C} &= (0_\R,0_\R)\\
		1_\C &= (1_\R,0_\R)
	\end{align*}
	and the inverses of an element are given by
	\begin{align*}
		-(\alpha,\beta ) &= (-\alpha ,-\beta )\\
		(\alpha ,\beta)^{-1} &= \left( \frac{\alpha }{\alpha^2+\beta^2}, \frac{-\beta }{\alpha^2+\beta^2} \right)
	\end{align*}
\end{defn}

\begin{anki}
TARGET DECK
Complex Qual::Complex Analysis
START
MathJaxCloze
Text: We define the **complex field** \(\C\) to be the field of ordered pairs
\(\begin{align*}
  	(\alpha ,\beta )
  \end{align*}\)
where \(\alpha ,\beta  \in \R\). The addition and multiplication operations on \(\C\) are defined by
{{c1::\(\begin{align*}
        	(\alpha_1,\beta_1) + (\alpha_2,\beta_2) &= (\alpha_1+\alpha_2,\beta_1+\beta_2)\\
        	(\alpha_1,\beta_1)\cdot (\alpha_2,\beta_2) &= (\alpha_1\beta_1 - \alpha_2\beta_2, \alpha_1\beta_2+\alpha_2\beta_1)
        \end{align*}\)}}
Elements of \(\C\) are called **complex numbers**. We refer to the first element of the ordered pair as the **real part**, and the second element as the **imaginary part** of the complex number.

The identities of \(\C\) are hence
\(\begin{align*}
  	0_{\C} &= (0_\R,0_\R)\\
  	1_\C &= (1_\R,0_\R)
  \end{align*}\)
and the inverses of an element are given by
 {{c2::\(\begin{align*}
        	-(\alpha,\beta ) &= (-\alpha ,-\beta )\\
        	(\alpha ,\beta)^{-1} &= \left( \frac{\alpha }{\alpha^2+\beta^2}, \frac{-\beta }{\alpha^2+\beta^2} \right)
        \end{align*}\)}}
Extra: Sometimes, we might write a complex number \((\alpha ,\beta )\) as the sum
\(\begin{align*}
	\alpha + i\beta .
\end{align*}\)
Tags: analysis complex_analysis complex_numbers defn
<!--ID: 1624142599825-->
END
\end{anki}

\begin{hw}
	Check that the identities and inverses above indeed satisfy the necessary conditions to be identities and inverses of a field. Furthermore, verify that the addition and multiplication operations are associative, commutative, and satisfy the distributive property.
\end{hw}

Sometimes, we might write a complex number \((\alpha ,\beta )\) as the sum
\begin{align*}
	\alpha + i\beta .
\end{align*}
In other words, we choose to define
\begin{align*}
	i:= (0,1)
\end{align*}
and will often use \(i\) for shorthand in formulas.\\

If \(\beta=0\), we say a number is \textbf{real}, and if \(\alpha=0\), we say a number is \textbf{imaginary}. There is a natural isomorphism from the set of real complex numbers to the real numbers, as well as a natural isomorphism from the set of imaginary complex numbers to the real numbers.\\

Of course, this construction does not give an intuition for why the operations above are defined so. One should first observe that the definition gives a field structure to the set, unlike the standard operations of \(\R^2\). Later, we will also show that the field is algebraically closed, and is in fact the algebraic extension of \(\R\).\\

If we graph the real part of a complex number onto an \(x\)-axis of an \(xy\)-grid, and the imaginary part to the \(y\)-axis, then the addition and multiplication operations correspond to geometric translations:
\begin{itemize}
	\item Adding a fixed complex number to a complex variable is geometrically a translation:
		\begin{align*}
			(\alpha ,\beta ) + (x,y) = (\alpha +x,\beta +y)
		\end{align*}
		% Figure
	\item Multiplying a fixed positive number to a complex variable is geometrically a dilation:
	\begin{align*}
		a(x+yi) = ax + ayi
	\end{align*}
	% Figure
	\item Multiplying by a fixed complex number of length 1 and an argument or angle \(t\) is geomerically a rotation by \(t\):
		\begin{align*}
			e^{it} = \cos(t) + i \sin(t)
		\end{align*}
	% Figure
	This definition allows us to add angles when we multiply by \(e^{it'}\)
\end{itemize}
This geometric interpretation immediately gives us new intuition when working with complex numbers. For example, a natural question arises-- is there a polar form for complex numbers in a similar sense as \(\R^2\)?\\

In fact, every non-zero complex number has a unique decomposition into a product of a positive number and a number of unit length, given by \(z = re^{it}\). Then, in a formula, complex multiplcation is defined by
\begin{align*}
	re^{it} \cdot r'e^{it'} = (r\cdot r')e^{i(t+t')}
\end{align*}
% Figure

\begin{hw}
	Show that the square root of a complex number is given by
	\begin{align*}
		(\alpha ,\beta )^{\sfrac{1}{2}} = \pm \left( \sqrt{ \frac{\alpha + \sqrt{\alpha^2+\beta^2} }{2}} , \frac{\beta }{\left| \beta  \right| } \sqrt{ \frac{-\alpha + \sqrt{\alpha^2+\beta^2} }{2}} \right) .
	\end{align*}
	where the square roots of positive numbers are taken to be the positive root. Furthermore, use this to show that the square root of any complex number always exists and has two opposite values, coinciding only if \((\alpha ,\beta )=0 \)
\end{hw}

\begin{anki}
START
MathJaxCloze
Text: The square root of a complex number is given by
 {{c1::\(\begin{align*}
         	(\alpha ,\beta )^{\sfrac{1}{2}} = \pm \left( \sqrt{ \frac{\alpha + \sqrt{\alpha^2+\beta^2} }{2}} , \frac{\beta }{\left| \beta  \right| } \sqrt{ \frac{-\alpha + \sqrt{\alpha^2+\beta^2} }{2}} \right) .
         \end{align*}\)}}
where the square roots of positive numbers are taken to be the positive root.
Extra: The square root of any complex number always exists and has two opposite values, coinciding only if \((\alpha ,\beta )=0 \)
Tags: analysis complex_analysis complex_numbers
<!--ID: 1624142599862-->
END
\end{anki}

This exercise has some subtle deep implications. Recall that in \(\R\), a polynomial equation may not have all its roots in \(\R\). However, in \(\C\) this holds-- hence if we apply the natural isomorphism on polynomials in \(\R\) to polynomials in \(\C\), the polynomials will have all its solutions in \(\C\). In fact, \(\C\) is the smallest field for which this isomorphism is possible, and hence \(\C\) is the algebraic completion of \(\R\).\\

There is much to be shown in order to demonstrate that the algebraic completion of \(\R\) corresponds to the construction of \(\C\) above. For brevity, this will be skipped, but for first-time readers, the author highly encourages one to read this construction in detail elsewhere.

\subsection{Conjugation in \(\C\)}
\label{sub:conjugation_in_c}

While there are infinitely many automorphisms of \(\C\) (of which we will discuss in detail later), there is a special automorphism that is worth discussing first.\\

\begin{defn}[Complex Conjugation]
	Let \((\alpha ,\beta ) \in \C\) be a complex number. Then we define the \textbf{complex conjugate of \((\alpha ,\beta )\)} to be the complex number given by the transformation:
	\begin{align*}
		\overline{(\alpha ,\beta )} = (\alpha ,-\beta )
	\end{align*}
	Furthermore, complex conjugation satisfies the following:
	\begin{align*}
		\overline{a+b} = \overline{a} + \overline{b}\\
		\overline{ab} = \overline{a}\cdot \overline{b}.
	\end{align*}
	Finally, complex conjugation is an \textbf{involuntary transformation}. That is, it satisfies
	\begin{align*}
		\overline{ \left( \overline{(\alpha ,\beta )} \right) } = (\alpha ,\beta )
	\end{align*}
\end{defn}
\begin{anki}
% Up to 5 consequences
START
Definition
Name: Complex Conjugate
Premise 1: Let \(a = (\alpha ,\beta ) \in \C\)
Consequence 1: The complex conjugate is \(\overline{a} := (\alpha ,-\beta )\)
Tags: analysis complex_analysis complex_numbers defn
<!--ID: 1624237478645-->
END
\end{anki}

One can check that we can obtain formulas
\begin{align*}
	\textrm{Re}(a) = \frac{a + \overline{a}}{2}, \quad \textrm{Im}(a) = \frac{a - \overline{a}}{2}
\end{align*}

\begin{defn}[Modulus]
	Let \(a = (\alpha ,\beta ) \in \C\). The \textbf{absolute value} or \textbf{modulus} of \(a\) is defined as
	\begin{align*}
		\left| a \right| := (a \overline{a})^{\sfrac{1}{2}} = (\alpha^2+\beta^2)^{\sfrac{1}{2}}.
	\end{align*}
\end{defn}
	Notice that \(\left| a \right| \geq 0\) for all \(a \in \C\), and furthermore, \(\left| a \right| = 0\) if and only if \(a=0\). It also holds that \(\left| \overline{a} \right| = \left| a \right| \) and
	\begin{align*}
		\left| ab \right| = \left| a \right| \cdot \left| b \right|
	\end{align*}
	This has the properties of a norm, and hence for sums we have
	\begin{align*}
		\left| a + b \right|^2 &= \left| a \right|^2 + \left| b \right|^2 + 2 \textrm{Re}a \overline{b}\\
		\left| a-b \right|^2 &= \left| a \right|^2 + \left| b \right|^2 - 2 \textrm{Re} a \overline{b}\\
				     &\implies \left| a+b \right|^2 + \left| a-b \right|^2 = 2\left( \left| a \right|^2 + \left| b \right|^2 \right)
	\end{align*}

\begin{anki}
START
MathJaxCloze
Text: Let \(a = (\alpha ,\beta ) \in \C\). The **absolute value** or **modulus** of \(a\) is defined as
{{c1::\(\begin{align*}
        	\left| a \right| := (a \overline{a})^{\sfrac{1}{2}} = (\alpha^2+\beta^2)^{\sfrac{1}{2}}.
        \end{align*}\)}}
Extra: Notice that \(\left| a \right| \geq 0\) for all \(a \in \C\), and furthermore, \(\left| a \right| = 0\) if and only if \(a=0\). It also holds that \(\left| \overline{a} \right| = \left| a \right| \) and
\(\begin{align*}
  	\left| ab \right| = \left| a \right| \cdot \left| b \right|
  \end{align*}\)
	This has the properties of a norm, and hence for sums we have
	\(\begin{align*}
	  	\left| a + b \right|^2 &= \left| a \right|^2 + \left| b \right|^2 + 2 \textrm{Re}a \overline{b}\\
	  	\left| a-b \right|^2 &= \left| a \right|^2 + \left| b \right|^2 - 2 \textrm{Re} a \overline{b}\\
	  			     &\implies \left| a+b \right|^2 + \left| a-b \right|^2 = 2\left( \left| a \right|^2 + \left| b \right|^2 \right)
	  \end{align*}\)
Tags: analysis complex_analysis complex_numbers defn
<!--ID: 1624237478679-->
END
\end{anki}

\begin{thm}[Triangle Inequality]
Let \(a,b \in \C\). Then
\begin{align*}
	\left| a + b \right| \leq \left| a \right| + \left| b \right|.
\end{align*}
The equation is an equality if and only if \(a \overline{b}\) is real and non-negative. In fact, this holds for extended finite sums if the ratio of any two nonzero terms is positive.
\end{thm}

\begin{anki}
% Up to 4 premises
% Up to 4 equivalences
START
Theorem
Name: Complex Triangle Inequality
Premise 1: Let \(a,b \in \C\)
Consequence 1: \(\left| a+b \right| \leq \left| a \right| + \left| b \right| \)
Consequence 2: \(\left| a+b \right| = \left| a \right| + \left| b \right| \) if and only if \(a \overline{b}\) is real and non-negative
Tags: analyis complex_analysis complex_numbers
<!--ID: 1624237478709-->
END
\end{anki}

\begin{thm}[Cauchy's Inequality]
	Let \(a_i,b_i \in \C\) for \(i \in \left\{ 1,\ldots,n \right\} \). Then
	\begin{align*}
		\left| \sum_{i=1}^{n} a_ib_i \right|^2 \leq \sum_{i=1}^{n} \left| a_i \right|^2 \sum_{i=1}^{n} \left| b_i \right|^2.
	\end{align*}
\end{thm}

\begin{anki}
% Up to 4 premises
% Up to 4 equivalences
START
Theorem
Name: Complex Cauchy's Inequality
Premise 1: Let \(a_i, b_i \in \C\) for \(i \in \left\{ 1,\ldots,n \right\} \)
Consequence 1: \(\left| \sum_{i=1}^{n} a_ib_i \right|^2 \leq \sum_{i=1}^{n} \left| a_i \right|^2 \sum_{i=1}^{n} \left| b_i \right|^2\)
Tags: analysis complex_analysis complex_numbers
<!--ID: 1624237478740-->
END
\end{anki}

\end{document}


\documentclass[12pt]{article}

\usepackage{amsmath}    % need for subequations
\usepackage{graphicx}   % need for figures
\usepackage{verbatim}   % useful for program listings
\usepackage{color}      % use if color is used in text
\usepackage{subfigure}  % use for side-by-side figures
\usepackage{hyperref}   % use for hypertext links, including those to external documents and URLs

% don't need the following. simply use defaults
\setlength{\baselineskip}{16.0pt}    % 16 pt usual spacing between lines

\setlength{\parskip}{3pt plus 2pt}
\setlength{\parindent}{20pt}
\setlength{\oddsidemargin}{0.5cm}
\setlength{\evensidemargin}{0.5cm}
\setlength{\marginparsep}{0.75cm}
\setlength{\marginparwidth}{2.5cm}
\setlength{\marginparpush}{1.0cm}
\setlength{\textwidth}{150mm}

\begin{comment}
  \pagestyle{empty} % use if page numbers not wanted
\end{comment}

% above is the preamble

\begin{document}

\begin{center}
  {\large Introduction to \LaTeX} \\ % \\ = new line
  \copyright 2006 by Harvey Gould \\
  December 5, 2006
\end{center}

\section{Introduction}
\TeX\ looks more difficult than it is. It is
almost as easy as $\pi$. See how easy it is to make special
symbols such as $\alpha$,
$\beta$, $\gamma$,
$\delta$, $\sin x$, $\hbar$, $\lambda$, $\ldots$ We also can make
subscripts
$A_{x}$, $A_{xy}$ and superscripts, $e^x$, $e^{x^2}$, and
$e^{a^b}$. We will use \LaTeX, which is based on \TeX\ and has
many higher-level commands (macros) for formatting, making
tables, etc. More information can be found in Ref.~\cite{latex}.

We just made a new paragraph. Extra lines and spaces make no
difference. Note that all formulas are enclosed by
\$ and occur in \textit{math mode}.

The default font is Computer Modern. It includes \textit{italics},
\textbf{boldface},
\textsl{slanted}, and \texttt{monospaced} fonts.

\section{Equations}
Let us see how easy it is to write equations.
\begin{equation}
  \Delta =\sum_{i=1}^N w_i (x_i - \bar{x})^2 .
\end{equation}
It is a good idea to number equations, but we can have a
equation without a number by writing
\begin{equation}
  P(x) = \frac{x - a}{b - a} , \nonumber
\end{equation}
and
\begin{equation}
  g = \frac{1}{2} \sqrt{2\pi} . \nonumber
\end{equation}

We can give an equation a label so that we can refer to it later.
\begin{equation}
  \label{eq:ising}
  E = -J \sum_{i=1}^N s_i s_{i+1} ,
\end{equation}
Equation~\eqref{eq:ising} expresses the energy of a configuration
of spins in the Ising model.\footnote{It is necessary to process (typeset) a
file twice to get the counters correct.}

We can define our own macros to save typing. For example, suppose
that we introduce the macros:
\begin{verbatim}
 \newcommand{\lb}{{\langle}}
 \newcommand{\rb}{{\rangle}}
\end{verbatim}
\newcommand{\lb}{{\langle}}
\newcommand{\rb}{{\rangle}}
Then we can write the average value of $x$ as
\begin{verbatim}
\begin{equation}
\lb x \rb = 3
\end{equation}
\end{verbatim}
The result is
\begin{equation}
  \lb x \rb = 3 .
\end{equation}

Examples of more complicated equations:
\begin{equation}
  I = \! \int_{-\infty}^\infty f(x)\,dx \label{eq:fine}.
\end{equation}
We can do some fine tuning by adding small amounts of horizontal
spacing:
\begin{verbatim}
 \, small space       \! negative space
\end{verbatim}
as is done in Eq.~\eqref{eq:fine}.

We also can align several equations:
\begin{align}
  a & = b \\
  c &= d ,
\end{align}
or number them as subequations:
\begin{subequations}
  \begin{align}
    a & = b \\
    c &= d .
  \end{align}
\end{subequations}

We can also have different cases:
\begin{equation}
  \label{eq:mdiv}
  m(T) =
  \begin{cases}
  0 & \text{$T > T_c$} \\
\bigl(1 - [\sinh 2 \beta J]^{-4} \bigr)^{\! 1/8} & \text{$T < T_c$}
\end{cases}
\end{equation}
write matrices
\begin{align}
  \textbf{T} &=
  \begin{pmatrix}
    T_{++} \hfill & T_{+-} \\
    T_{-+} & T_{--} \hfill
  \end{pmatrix} , \nonumber \\
  & =
  \begin{pmatrix}
    e^{\beta (J + B)} \hfill & e^{-\beta J} \hfill \\
    e^{-\beta J} \hfill & e^{\beta (J - B)} \hfill
  \end{pmatrix}.
\end{align}
and
\newcommand{\rv}{\textbf{r}}
\begin{equation}
  \sum_i \vec A \cdot \vec B = -P\!\int\! \rv \cdot
  \hat{\mathbf{n}}\, dA = P\!\int \! {\vec \nabla} \cdot \rv\, dV.
\end{equation}

\section{Tables}
Tables are a little more difficult. TeX
automatically calculates the width of the columns.

\begin{table}[h]
  \begin{center}
    \begin{tabular}{|l|l|r|l|}
      \hline
      lattice & $d$ & $q$ & $T_{\rm mf}/T_c$ \\
      \hline
      square & 2 & 4 & 1.763 \\
      \hline
      triangular & 2 & 6 & 1.648 \\
      \hline
      diamond & 3 & 4 & 1.479 \\
      \hline
      simple cubic & 3 & 6 & 1.330 \\
      \hline
      bcc & 3 & 8 & 1.260 \\
      \hline
      fcc & 3 & 12 & 1.225 \\
      \hline
    \end{tabular}
    \caption{\label{tab:5/tc}Comparison of the mean-field predictions
      for the critical temperature of the Ising model with exact results
      and the best known estimates for different spatial dimensions $d$
    and lattice symmetries.}
  \end{center}
\end{table}

\section{Lists}

Some example of formatted lists include the
following:

\begin{enumerate}

  \item bread

  \item cheese

\end{enumerate}

\begin{itemize}

  \item Tom

  \item Dick

\end{itemize}

\section{Figures}

We can make figures bigger or smaller by scaling them. Figure~\ref{fig:lj}
has been scaled by 60\%.

\begin{figure}[h]
  \begin{center}
    \includegraphics{figures/sine}
    \caption{\label{fig:typical}Show me a sine.}
  \end{center}
\end{figure}

\begin{figure}[h]
  \begin{center}
    \scalebox{0.6}{\includegraphics{figures/lj}}
    \caption{\label{fig:lj}Plot of the
      Lennard-Jones potential
      $u(r)$. The potential is characterized by a length
      $\sigma$ and an energy
    $\epsilon$.}
  \end{center}
\end{figure}

\section{Literal text}
It is desirable to print program code exactly as it is typed in a
monospaced font. Use \verb \begin{verbatim} and
\verb \end{verbatim} as in the following example:
\begin{verbatim}
double y0 = 10; // example of declaration and assignment statement
double v0 = 0;  // initial velocity
double t = 0;   // time
double dt = 0.01; // time step
double y = y0;
\end{verbatim}
The command \verb \verbatiminput{programs/Square.java}\ allows
you to list the file \texttt{Square.java} in the directory
programs.

\section{Special Symbols}

\subsection{Common Greek letters}

These commands may be used only in math mode. Only the most common
letters are included here.

$\alpha,
\beta, \gamma, \Gamma,
\delta,\Delta,
\epsilon, \zeta, \eta, \theta, \Theta, \kappa,
\lambda, \Lambda, \mu, \nu,
\xi, \Xi,
\pi, \Pi,
\rho,
\sigma,
\tau,
\phi, \Phi,
\chi,
\psi, \Psi,
\omega, \Omega$

\subsection{Special symbols}

The derivative is defined as
\begin{equation}
  \frac{dy}{dx} = \lim_{\Delta x \to 0} \frac{\Delta y}
  {\Delta x}
\end{equation}
\begin{equation}
  f(x) \to y \quad \mbox{as} \quad x \to
  x_{0}
\end{equation}
\begin{equation}
  f(x) \mathop {\longrightarrow}
  \limits_{x \to x_0} y
\end{equation}

\noindent Order of magnitude:
\begin{equation}
  \log_{10}f \simeq n
\end{equation}
\begin{equation}
  f(x)\sim 10^{n}
\end{equation}
Approximate equality:
\begin{equation}
  f(x)\simeq g(x)
\end{equation}
\LaTeX\ is simple if we keep everything in proportion:
\begin{equation}
  f(x) \propto x^3 .
\end{equation}

Finally we can skip some space by using commands such as
\begin{verbatim}
\bigskip    \medskip    \smallskip    \vspace{1pc}
\end{verbatim}
The space can be negative.

\section{\color{red}Use of Color}

{\color{blue}{We can change colors for emphasis}},
{\color{green}{but}} {\color{cyan}{who is going pay for the ink?}}

\section{\label{morefig}Subfigures}

As soon as many students start becoming comfortable using \LaTeX, they want
to use some of its advanced features. So we now show how to place two
figures side by side.

\begin{figure}[h!]
  \begin{center}
    \subfigure[Real and imaginary.]{
    \includegraphics[scale=0.5]{figures/reim}}
    \subfigure[Amplitude and phase.]{
    \includegraphics[scale=0.5]{figures/phase}}
    \caption{\label{fig:qm/complexfunctions} Two representations of complex
    wave functions.}
  \end{center}
\end{figure}

We first have to include the necessary package,
\verb+\usepackage{subfigure}+, which has to go in the preamble (before
\verb+\begin{document}+). It sometimes can be difficult to place a figure in
the desired place.

Your LaTeX document can be easily modified to make a poster or a screen
presentation similar to (and better than) PowerPoint. Conversion to HTML is
straightforward. Comments on this tutorial are appreciated.

\begin{thebibliography}{5}

  \bibitem{latex}Helmut Kopka and Patrick W. Daly, \textsl{A Guide to
  \LaTeX: Document Preparation for Beginners and Advanced Users},
  fourth edition, Addison-Wesley (2004).

  \bibitem{website}Some useful links are
  given at \url{}.

\end{thebibliography}

{\small \noindent Updated 5 December 2006.}
\section{Introduction}

\latex is a typesetting program; given an input file with formatting
instructions (e.g intro.tex), the program will create your document in
one of several formats (DVI, Postscript or PDF).  It is therefore not
a WYSIWYG word processor.  \latex is known as a logical markup
language, similar for example to HTML, so that you describe a piece of
text as a ``section heading'' rather than saying that it should be
formatted in a certain way.  It has excellent facilities for
typesetting mathematics, and handles large documents (such as theses)
well.  The aim of this document is not to provide an overview of
\latex, since many other guides have already been written (see
Section~\ref{sec:summary}).  Instead, it has been written primarily to
provide simple workable examples that you can cut and paste to help
you get started with \latex.  The examples have been selected to be
those most likely to be useful when writing a scientific report.  This
document is best read by comparing the source code with the resulting
output.

\section{Running \latex}

The files to accompany this paper are at:
\url{http://www.damtp.cam.ac.uk/user/eglen/texintro}.  Get the
following files and put them into a new directory.

\begin{enumerate}
  \item \url{intro.tex}: the main \latex document.
  \item \url{example.bib}: a short bibliography.
  \item \url{sigmoid.ps}: example postscript image.
  \item \url{sigmoid.pdf}: example PDF image.
\end{enumerate}

Change directory to where you stored the files and type the
following (ignoring comments placed after \#\#):

\begin{verbatim}
latex intro                     ## Run latex 1st time.
bibtex intro                    ## Extract required references
latex intro                     ## Run latex 2nd to resolve references.
latex intro                     ## Probably need to run latex a 3rd time.
xdvi intro                      ## View the DVI (device independent) file.
dvips -o intro.ps intro         ## Create a postscript file for printing.
\end{verbatim}

You will notice that you run latex several times here; this is so that
references can be resolved, and references can be extracted from your
bibtex file.  After running latex, you will be told if you need to run
it again to resolve references.  After a while, you will get the idea
of how many times you need to run latex to resolve all your
references.

If instead you would like to generate PDF files (see
Section~\ref{sec:graphics} for a discussion of file formats for
included images), you can try the following shorter sequence:

\begin{verbatim}
pdflatex intro
bibtex intro
pdflatex intro
pdflatex intro
xpdf intro.pdf                  ## View the resulting PDF
\end{verbatim}

Whether you prefer to generate DVI or PDF is up to you.  The xdvi
viewer has some nice features, such as it can reload your document
easily and has a ``magnifying glass'' that is activated by the mouse.
On the other hand, xpdf will display the document more accurately as
it will be printed.

\section{Tables}

Tables are relatively straightforward to generate.  Note that tables
and figures are not always placed exactly where you wish, as
they can \textit{float} to other parts of the document.  Rather than
trying to battle with \latex as to where they are placed, concentrate
first on getting the right content and let \latex worry about the
positioning.  Instead, use labels to your tables to refer to them.
See Table~\ref{tab:simple} and Table~\ref{tab:pars} for examples.

\begin{table}
  \centering
  \begin{tabular}{ccc}
    year & min temp (\textdegree C) & max temp (\textdegree C)\\
    \hline
    1970 & $-5$ & 35\\
    1975 & $-7$ & 29\\
    1980 & $-3$ & 30\\
    1985 & $-2$ & 32\\
  \end{tabular}
  \caption{Fictional minimal and maximal temperatures recorded in
  Cambridge over several years.}
  \label{tab:simple}
\end{table}
%% Why are the negative numbers above enclosed in math mode?
%% Hint: consider the difference between "-" in text and in math.

\begin{table}[htbp]
  \centering
  \begin{tabular}{lccc}\\ \hline
    & \multicolumn{1}{c}{$\phi$ (\micro m)}
    & \multicolumn{1}{c}{$\alpha$}
    & $\delta_{12}$ (\micro m)\\ \hline
    W81S1\\
    $h_{11}(u)$  & 67.94 & 7.81\\
    $h_{22}(u)$  & 66.27 & 5.40\\
    $h_{12}(u)$  &       &     &18\\
    \hline
    M623\\
    $h_{11}(u)$  &112.79 &  3.05\\
    $h_{22}(u)$  & 65.46 &  8.11\\
    $h_{12}(u)$  &       &      &20\\
    \hline
  \end{tabular}
  \caption{Summary of parameter estimates for the univariate
    functions $h_{11}(u)$, $h_{22}(u)$ and the bivariate function
    $h_{12}(u)$.  For the univariate fits, $\alpha$ and $\phi$ are
    least-square estimates (assuming $\delta$ was fixed at 15 \micro m).
    The final column gives the
    maximum likelihood estimate of $\delta_{12}$ assuming that the
    interaction between types is simple inhibition.
  \label{tab:pars}}
\end{table}

\section{Bibliography management}

Scientific reports normally require a section where your references
are listed.  Bibtex is an excellent system for maintaining references,
especially for large documents.  Each reference needs a unique key;
you can then refer to the reference in your \latex document by using
this key within a cite command.

Take care when formatting your references, especially when it comes to
writing authors names and the case of letters in journal titles.  In
our examples, the files are found in \url{example.bib}.  As an example
of a citation, see \citep{ihaka1996} or \citep{ihaka1996,venables1999}.

Bibtex is flexible enough to format your references in a wide number
of different styles to suit your needs.  In this file I have used the
``natbib'' package, which is suitable for the natural sciences.
Depending on the type of cite command you get (and the package that
you use for citations), you can get different styles of citation.  See
Table~\ref{tab:cite} for some examples.

\begin{table}
  \centering
  \begin{tabular}{ll}
    \hline
    command & result\\ \hline
    \verb+\citep{ihaka1996}+ & \citep{ihaka1996}\\
    \verb+\citet{ihaka1996}+ & \citet{ihaka1996}\\
    \verb+\citep[see][p. 300]{ihaka1996}+ &
    \citep[see][p. 300]{ihaka1996}
    \\
    \verb+\citeauthor{ihaka1996}+ & \citeauthor{ihaka1996}
    \\
    \verb+\citeyear{ihaka1996}+ & \citeyear{ihaka1996}
    \\
    \hline
  \end{tabular}
  \caption{Examples of different citation commands available in the
  natbib package.}
  \label{tab:cite}
\end{table}

\section{Graphics}
\label{sec:graphics}

\latex can include images in one of several format, depending on
whether you use latex (postscript format required) or pdflatex (either
jpeg, png or pdf required).  Figures can be included either at their
natural size, or you can specify e.g. the figure width.
Figure~\ref{fig:example} shows an example image which intentionally
looks slightly different depending on whether you compile the document
with latex or pdflatex.  Note that in this example the suffix of the
image file is not included so that this document compiles under both
latex and pdflatex.

\begin{figure}
  \centering
  \includegraphics[width=6cm]{sigmoid}
  \caption{Example of a sigmoidal curve generated by the R programming
    environment.  The title above the curve indicates whether you have
  included the postscript or the pdf version of the figure.}
  \label{fig:example}
\end{figure}

\section{Mathematics}

\latex can format mathematics with ease, either in line, such as
$x \times y$, or on separate lines, such as:
\[ x^2 +y^2 = z^2 \]

If you are writing several lines of equations, you can use statements
like the following:

\begin{align}
  b(t) & = s(t) - \int_{0}^{T} a(t') \cdot i(T-t') dt'
  \\
  a(t) & = \int_{0}^{T} b(t) \cdot e(T-t') dt' \label{eq:am}
  \\
  g(t) & = b(t) \ast e(t) \nonumber
\end{align}

By using labels on certain equations, we can refer to equations by
number, such as equation~(\ref{eq:am}).

\section{Summary}
\label{sec:summary}
This short guide should give you a flavour of what can be done with
\latex.  It is by no means complete, or supposed to be
self-explanatory.  It is, however, hopefully enough to get you
started!  Try experimenting by editing the source file and then
recompiling this document.  As mentioned earlier, there are many
guides for latex.  Two that I can recommend are
\url{http://www.andy-roberts.net/misc/latex/index.html} and
`` The (Not So) Short Introduction to LaTeX2e''
(\url{http://ctan.tug.org/tex-archive/info/lshort/english/lshort.pdf}).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Finally we specify the format required for our references and the
%% name of the bibtex file where our references should be taken from.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\bibliographystyle{plainnat}
\bibliography{example}

\section{Introduction}
\TeX\ looks more difficult than it is. It is
almost as easy as $\pi$. See how easy it is to make special
symbols such as $\alpha$,
$\beta$, $\gamma$,
$\delta$, $\sin x$, $\hbar$, $\lambda$, $\ldots$ We also can make
subscripts
$A_{x}$, $A_{xy}$ and superscripts, $e^x$, $e^{x^2}$, and
$e^{a^b}$. We will use \LaTeX, which is based on \TeX\ and has
many higher-level commands (macros) for formatting, making
tables, etc. More information can be found in Ref.~\cite{latex}.

We just made a new paragraph. Extra lines and spaces make no
difference. Note that all formulas are enclosed by
\$ and occur in \textit{math mode}.

The default font is Computer Modern. It includes \textit{italics},
\textbf{boldface},
\textsl{slanted}, and \texttt{monospaced} fonts.

\section{Equations}
Let us see how easy it is to write equations.
\begin{equation}
  \Delta =\sum_{i=1}^N w_i (x_i - \bar{x})^2 .
\end{equation}
It is a good idea to number equations, but we can have a
equation without a number by writing
\begin{equation}
  P(x) = \frac{x - a}{b - a} , \nonumber
\end{equation}
and
\begin{equation}
  g = \frac{1}{2} \sqrt{2\pi} . \nonumber
\end{equation}

We can give an equation a label so that we can refer to it later.
\begin{equation}
  \label{eq:ising}
  E = -J \sum_{i=1}^N s_i s_{i+1} ,
\end{equation}
Equation~\eqref{eq:ising} expresses the energy of a configuration
of spins in the Ising model.\footnote{It is necessary to process (typeset) a
file twice to get the counters correct.}

We can define our own macros to save typing. For example, suppose
that we introduce the macros:
\begin{verbatim}
 \newcommand{\lb}{{\langle}}
 \newcommand{\rb}{{\rangle}}
\end{verbatim}
\newcommand{\lb}{{\langle}}
\newcommand{\rb}{{\rangle}}
Then we can write the average value of $x$ as
\begin{verbatim}
\begin{equation}
\lb x \rb = 3
\end{equation}
\end{verbatim}
The result is
\begin{equation}
  \lb x \rb = 3 .
\end{equation}

Examples of more complicated equations:
\begin{equation}
  I = \! \int_{-\infty}^\infty f(x)\,dx \label{eq:fine}.
\end{equation}
We can do some fine tuning by adding small amounts of horizontal
spacing:
\begin{verbatim}
 \, small space       \! negative space
\end{verbatim}
as is done in Eq.~\eqref{eq:fine}.

We also can align several equations:
\begin{align}
  a & = b \\
  c &= d ,
\end{align}
or number them as subequations:
\begin{subequations}
  \begin{align}
    a & = b \\
    c &= d .
  \end{align}
\end{subequations}

We can also have different cases:
\begin{equation}
  \label{eq:mdiv}
  m(T) =
  \begin{cases}
  0 & \text{$T > T_c$} \\
\bigl(1 - [\sinh 2 \beta J]^{-4} \bigr)^{\! 1/8} & \text{$T < T_c$}
\end{cases}
\end{equation}
write matrices
\begin{align}
  \textbf{T} &=
  \begin{pmatrix}
    T_{++} \hfill & T_{+-} \\
    T_{-+} & T_{--} \hfill
  \end{pmatrix} , \nonumber \\
  & =
  \begin{pmatrix}
    e^{\beta (J + B)} \hfill & e^{-\beta J} \hfill \\
    e^{-\beta J} \hfill & e^{\beta (J - B)} \hfill
  \end{pmatrix}.
\end{align}
and
\newcommand{\rv}{\textbf{r}}
\begin{equation}
  \sum_i \vec A \cdot \vec B = -P\!\int\! \rv \cdot
  \hat{\mathbf{n}}\, dA = P\!\int \! {\vec \nabla} \cdot \rv\, dV.
\end{equation}

\section{Tables}
Tables are a little more difficult. TeX
automatically calculates the width of the columns.

\begin{table}[h]
  \begin{center}
    \begin{tabular}{|l|l|r|l|}
      \hline
      lattice & $d$ & $q$ & $T_{\rm mf}/T_c$ \\
      \hline
      square & 2 & 4 & 1.763 \\
      \hline
      triangular & 2 & 6 & 1.648 \\
      \hline
      diamond & 3 & 4 & 1.479 \\
      \hline
      simple cubic & 3 & 6 & 1.330 \\
      \hline
      bcc & 3 & 8 & 1.260 \\
      \hline
      fcc & 3 & 12 & 1.225 \\
      \hline
    \end{tabular}
    \caption{\label{tab:5/tc}Comparison of the mean-field predictions
      for the critical temperature of the Ising model with exact results
      and the best known estimates for different spatial dimensions $d$
    and lattice symmetries.}
  \end{center}
\end{table}

\section{Lists}

Some example of formatted lists include the
following:

\begin{enumerate}

  \item bread

  \item cheese

\end{enumerate}

\begin{itemize}

  \item Tom

  \item Dick

\end{itemize}

\section{Figures}

We can make figures bigger or smaller by scaling them. Figure~\ref{fig:lj}
has been scaled by 60\%.

\begin{figure}[h]
  \begin{center}
    \includegraphics{figures/sine}
    \caption{\label{fig:typical}Show me a sine.}
  \end{center}
\end{figure}

\begin{figure}[h]
  \begin{center}
    \scalebox{0.6}{\includegraphics{figures/lj}}
    \caption{\label{fig:lj}Plot of the
      Lennard-Jones potential
      $u(r)$. The potential is characterized by a length
      $\sigma$ and an energy
    $\epsilon$.}
  \end{center}
\end{figure}

\section{Literal text}
It is desirable to print program code exactly as it is typed in a
monospaced font. Use \verb \begin{verbatim} and
\verb \end{verbatim} as in the following example:
\begin{verbatim}
double y0 = 10; // example of declaration and assignment statement
double v0 = 0;  // initial velocity
double t = 0;   // time
double dt = 0.01; // time step
double y = y0;
\end{verbatim}
The command \verb \verbatiminput{programs/Square.java}\ allows
you to list the file \texttt{Square.java} in the directory
programs.

\section{Special Symbols}

\subsection{Common Greek letters}

These commands may be used only in math mode. Only the most common
letters are included here.

$\alpha,
\beta, \gamma, \Gamma,
\delta,\Delta,
\epsilon, \zeta, \eta, \theta, \Theta, \kappa,
\lambda, \Lambda, \mu, \nu,
\xi, \Xi,
\pi, \Pi,
\rho,
\sigma,
\tau,
\phi, \Phi,
\chi,
\psi, \Psi,
\omega, \Omega$

\subsection{Special symbols}

The derivative is defined as
\begin{equation}
  \frac{dy}{dx} = \lim_{\Delta x \to 0} \frac{\Delta y}
  {\Delta x}
\end{equation}
\begin{equation}
  f(x) \to y \quad \mbox{as} \quad x \to
  x_{0}
\end{equation}
\begin{equation}
  f(x) \mathop {\longrightarrow}
  \limits_{x \to x_0} y
\end{equation}

\noindent Order of magnitude:
\begin{equation}
  \log_{10}f \simeq n
\end{equation}
\begin{equation}
  f(x)\sim 10^{n}
\end{equation}
Approximate equality:
\begin{equation}
  f(x)\simeq g(x)
\end{equation}
\LaTeX\ is simple if we keep everything in proportion:
\begin{equation}
  f(x) \propto x^3 .
\end{equation}

Finally we can skip some space by using commands such as
\begin{verbatim}
\bigskip    \medskip    \smallskip    \vspace{1pc}
\end{verbatim}
The space can be negative.

\section{\color{red}Use of Color}

{\color{blue}{We can change colors for emphasis}},
{\color{green}{but}} {\color{cyan}{who is going pay for the ink?}}

\section{\label{morefig}Subfigures}

As soon as many students start becoming comfortable using \LaTeX, they want
to use some of its advanced features. So we now show how to place two
figures side by side.

\begin{figure}[h!]
  \begin{center}
    \subfigure[Real and imaginary.]{
    \includegraphics[scale=0.5]{figures/reim}}
    \subfigure[Amplitude and phase.]{
    \includegraphics[scale=0.5]{figures/phase}}
    \caption{\label{fig:qm/complexfunctions} Two representations of complex
    wave functions.}
  \end{center}
\end{figure}

We first have to include the necessary package,
\verb+\usepackage{subfigure}+, which has to go in the preamble (before
\verb+\begin{document}+). It sometimes can be difficult to place a figure in
the desired place.

Your LaTeX document can be easily modified to make a poster or a screen
presentation similar to (and better than) PowerPoint. Conversion to HTML is
straightforward. Comments on this tutorial are appreciated.

\begin{thebibliography}{5}

  \bibitem{latex}Helmut Kopka and Patrick W. Daly, \textsl{A Guide to
  \LaTeX: Document Preparation for Beginners and Advanced Users},
  fourth edition, Addison-Wesley (2004).

  \bibitem{website}Some useful links are
  given at \url{}.

\end{thebibliography}

{\small \noindent Updated 5 December 2006.}
\section{Introduction}

\latex is a typesetting program; given an input file with formatting
instructions (e.g intro.tex), the program will create your document in
one of several formats (DVI, Postscript or PDF).  It is therefore not
a WYSIWYG word processor.  \latex is known as a logical markup
language, similar for example to HTML, so that you describe a piece of
text as a ``section heading'' rather than saying that it should be
formatted in a certain way.  It has excellent facilities for
typesetting mathematics, and handles large documents (such as theses)
well.  The aim of this document is not to provide an overview of
\latex, since many other guides have already been written (see
Section~\ref{sec:summary}).  Instead, it has been written primarily to
provide simple workable examples that you can cut and paste to help
you get started with \latex.  The examples have been selected to be
those most likely to be useful when writing a scientific report.  This
document is best read by comparing the source code with the resulting
output.

\section{Running \latex}

The files to accompany this paper are at:
\url{http://www.damtp.cam.ac.uk/user/eglen/texintro}.  Get the
following files and put them into a new directory.

\begin{enumerate}
  \item \url{intro.tex}: the main \latex document.
  \item \url{example.bib}: a short bibliography.
  \item \url{sigmoid.ps}: example postscript image.
  \item \url{sigmoid.pdf}: example PDF image.
\end{enumerate}

Change directory to where you stored the files and type the
following (ignoring comments placed after \#\#):

\begin{verbatim}
latex intro                     ## Run latex 1st time.
bibtex intro                    ## Extract required references
latex intro                     ## Run latex 2nd to resolve references.
latex intro                     ## Probably need to run latex a 3rd time.
xdvi intro                      ## View the DVI (device independent) file.
dvips -o intro.ps intro         ## Create a postscript file for printing.
\end{verbatim}

You will notice that you run latex several times here; this is so that
references can be resolved, and references can be extracted from your
bibtex file.  After running latex, you will be told if you need to run
it again to resolve references.  After a while, you will get the idea
of how many times you need to run latex to resolve all your
references.

If instead you would like to generate PDF files (see
Section~\ref{sec:graphics} for a discussion of file formats for
included images), you can try the following shorter sequence:

\begin{verbatim}
pdflatex intro
bibtex intro
pdflatex intro
pdflatex intro
xpdf intro.pdf                  ## View the resulting PDF
\end{verbatim}

Whether you prefer to generate DVI or PDF is up to you.  The xdvi
viewer has some nice features, such as it can reload your document
easily and has a ``magnifying glass'' that is activated by the mouse.
On the other hand, xpdf will display the document more accurately as
it will be printed.

\section{Tables}

Tables are relatively straightforward to generate.  Note that tables
and figures are not always placed exactly where you wish, as
they can \textit{float} to other parts of the document.  Rather than
trying to battle with \latex as to where they are placed, concentrate
first on getting the right content and let \latex worry about the
positioning.  Instead, use labels to your tables to refer to them.
See Table~\ref{tab:simple} and Table~\ref{tab:pars} for examples.

\begin{table}
  \centering
  \begin{tabular}{ccc}
    year & min temp (\textdegree C) & max temp (\textdegree C)\\
    \hline
    1970 & $-5$ & 35\\
    1975 & $-7$ & 29\\
    1980 & $-3$ & 30\\
    1985 & $-2$ & 32\\
  \end{tabular}
  \caption{Fictional minimal and maximal temperatures recorded in
  Cambridge over several years.}
  \label{tab:simple}
\end{table}
%% Why are the negative numbers above enclosed in math mode?
%% Hint: consider the difference between "-" in text and in math.

\begin{table}[htbp]
  \centering
  \begin{tabular}{lccc}\\ \hline
    & \multicolumn{1}{c}{$\phi$ (\micro m)}
    & \multicolumn{1}{c}{$\alpha$}
    & $\delta_{12}$ (\micro m)\\ \hline
    W81S1\\
    $h_{11}(u)$  & 67.94 & 7.81\\
    $h_{22}(u)$  & 66.27 & 5.40\\
    $h_{12}(u)$  &       &     &18\\
    \hline
    M623\\
    $h_{11}(u)$  &112.79 &  3.05\\
    $h_{22}(u)$  & 65.46 &  8.11\\
    $h_{12}(u)$  &       &      &20\\
    \hline
  \end{tabular}
  \caption{Summary of parameter estimates for the univariate
    functions $h_{11}(u)$, $h_{22}(u)$ and the bivariate function
    $h_{12}(u)$.  For the univariate fits, $\alpha$ and $\phi$ are
    least-square estimates (assuming $\delta$ was fixed at 15 \micro m).
    The final column gives the
    maximum likelihood estimate of $\delta_{12}$ assuming that the
    interaction between types is simple inhibition.
  \label{tab:pars}}
\end{table}

\section{Bibliography management}

Scientific reports normally require a section where your references
are listed.  Bibtex is an excellent system for maintaining references,
especially for large documents.  Each reference needs a unique key;
you can then refer to the reference in your \latex document by using
this key within a cite command.

Take care when formatting your references, especially when it comes to
writing authors names and the case of letters in journal titles.  In
our examples, the files are found in \url{example.bib}.  As an example
of a citation, see \citep{ihaka1996} or \citep{ihaka1996,venables1999}.

Bibtex is flexible enough to format your references in a wide number
of different styles to suit your needs.  In this file I have used the
``natbib'' package, which is suitable for the natural sciences.
Depending on the type of cite command you get (and the package that
you use for citations), you can get different styles of citation.  See
Table~\ref{tab:cite} for some examples.

\begin{table}
  \centering
  \begin{tabular}{ll}
    \hline
    command & result\\ \hline
    \verb+\citep{ihaka1996}+ & \citep{ihaka1996}\\
    \verb+\citet{ihaka1996}+ & \citet{ihaka1996}\\
    \verb+\citep[see][p. 300]{ihaka1996}+ &
    \citep[see][p. 300]{ihaka1996}
    \\
    \verb+\citeauthor{ihaka1996}+ & \citeauthor{ihaka1996}
    \\
    \verb+\citeyear{ihaka1996}+ & \citeyear{ihaka1996}
    \\
    \hline
  \end{tabular}
  \caption{Examples of different citation commands available in the
  natbib package.}
  \label{tab:cite}
\end{table}

\section{Graphics}
\label{sec:graphics}

\latex can include images in one of several format, depending on
whether you use latex (postscript format required) or pdflatex (either
jpeg, png or pdf required).  Figures can be included either at their
natural size, or you can specify e.g. the figure width.
Figure~\ref{fig:example} shows an example image which intentionally
looks slightly different depending on whether you compile the document
with latex or pdflatex.  Note that in this example the suffix of the
image file is not included so that this document compiles under both
latex and pdflatex.

\begin{figure}
  \centering
  \includegraphics[width=6cm]{sigmoid}
  \caption{Example of a sigmoidal curve generated by the R programming
    environment.  The title above the curve indicates whether you have
  included the postscript or the pdf version of the figure.}
  \label{fig:example}
\end{figure}

\section{Mathematics}

\latex can format mathematics with ease, either in line, such as
$x \times y$, or on separate lines, such as:
\[ x^2 +y^2 = z^2 \]

If you are writing several lines of equations, you can use statements
like the following:

\begin{align}
  b(t) & = s(t) - \int_{0}^{T} a(t') \cdot i(T-t') dt'
  \\
  a(t) & = \int_{0}^{T} b(t) \cdot e(T-t') dt' \label{eq:am}
  \\
  g(t) & = b(t) \ast e(t) \nonumber
\end{align}

By using labels on certain equations, we can refer to equations by
number, such as equation~(\ref{eq:am}).

\section{Summary}
\label{sec:summary}
This short guide should give you a flavour of what can be done with
\latex.  It is by no means complete, or supposed to be
self-explanatory.  It is, however, hopefully enough to get you
started!  Try experimenting by editing the source file and then
recompiling this document.  As mentioned earlier, there are many
guides for latex.  Two that I can recommend are
\url{http://www.andy-roberts.net/misc/latex/index.html} and
`` The (Not So) Short Introduction to LaTeX2e''
(\url{http://ctan.tug.org/tex-archive/info/lshort/english/lshort.pdf}).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Finally we specify the format required for our references and the
%% name of the bibtex file where our references should be taken from.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\bibliographystyle{plainnat}
\bibliography{example}

\end{document}


\documentclass{article}
\usepackage{tocloft}
\include{common_symbols_and_format}
\renewcommand{\cfttoctitlefont}{\Large\bfseries}

\begin{document}
\logo
\rulename{Stochastic Relative Strength Index} %Argument is name of rule
\tblofcontents

\ruledescription{The Stochastic RSI (StochRSI) is an indicator used in technical analysis to identify overbought and oversold signals. It ranges between zero and one hundred and is created by applying the Stochastic oscillator formula to a set of relative strength index (RSI) values rather than to standard price data. An asset is considered oversold when the StochRSI values is below 20 and considered overbought when the StochRSI value is above 80.}
\howtotrade
The strategy is to identify overbought and oversold signals.
Bullish Momentum: when StochRSI is above 80.
Bearish Momentum: when StochRSI is below 20.

\ruleparameters %You can include however many arguments (in groups of 4) as you want!
{Time Length}{14 Days}{Time frame on which the StochRSI is calculated}{$L$}
\stoptable %must be included or Tex engine runs infinitely

\newpage
\section{Equation}
Below are the equations which govern how this specific trading rule calculates a trading position.
\begin{equation}
    RS_{\currenttime} = \frac{AvgU}{AvgD}
\end{equation}
\begin{equation}
RSI_{\currenttime} = 100-\frac{100}{(1+RS_{\currenttime})}
\end{equation}
\begin{equation}
RSI_{min} = \min\{RSI_{\currenttime-\lookbacklength},\ldots,RSI_{\currenttime}\}
\end{equation}
\begin{equation}
RSI_{max} = \max\{RSI_{\currenttime-\lookbacklength},\ldots,RSI_{\currenttime}\}
\end{equation}
\begin{equation}
    StochRSI_{\currenttime} = \frac{RSI_{\currenttime} - RSI_{min}}{RSI_{max} - RSI_{min}}
\end{equation}
\\
with:

$AvgU$ : average of all upward movements in the last $\lookbacklength$ price bars.

$AvgD$ : average of all downward movements in the last $\lookbacklength$ price bars.

$RS_{\currenttime}$: is the relative strength at time $\currenttime$.

$RSI_{\currenttime}$: is the relative strength index at time $\currenttime$.

$RSI_{min}$: Lowest RSI in last $\lookbacklength$ periods.

$RSI_{max}$: Highest RSI in last $\lookbacklength$ periods.

$StochRSI_{\currenttime}$: is the stochastic relative strength index at time $\currenttime$.

\keyterms
\furtherlinks %The footer
\end{document}


\section{Proof of Taylor's Theorem}\label{sec:TaylorsTheorem}

One of the most important uses of infinite series is
using an initial portion of the series for $f$ to approximate $f$. We
have seen, for example, that when we add up the first $n$ terms of an
alternating series with decreasing terms that the difference between
this and the true value is at most the size of the next term. A
similar result is true of many Taylor series. Recall that we stated Taylor's Theorem (Theorem \ref{thm:taylorthm} ) in terms of polynomials. Here we present a proof.

\begin{theorem*}[\ref{thm:taylorthm}, Taylor's Theorem--Again.]%\label{taylorthm2}
Suppose that $f$ is defined on some open interval $I$ around $a$ and
suppose $\ds f^{(N+1)}(x)$ exists on this interval. Then
for each $x\not=a$ in $I$ there is a value $z$ between
$x$ and $a$ so that
$$
  f(x) = \sum_{n=0}^N {f^{(n)}(a)\over n!}\,(x-a)^n +
  {f^{(N+1)}(z)\over (N+1)!}(x-a)^{N+1}.
$$
\end{theorem*}
\begin{proof}
The proof requires some cleverness to set up, but then the details are
quite elementary. We define a function $F(t)$ as follows:
$$F(t)=\sum_{n=0}^N{f^{(n)}(t)\over n!}\,(x-t)^n + B(x-t)^{N+1}.$$
Here we have replaced $a$ by $t$ in the first $N+1$ terms of the
Taylor series, and added a carefully chosen term on the end, with $B$
to be determined. Note that
we are temporarily keeping $x$ fixed, so the only variable in this
equation is $t$, and we will be interested
only in $t$ between $a$ and $x$. Now substitute $t=a$:
$$F(a)=\sum_{n=0}^N{f^{(n)}(a)\over n!}\,(x-a)^n + B(x-a)^{N+1}.$$
Set this equal to $f(x)$:
$$f(x)=\sum_{n=0}^N{f^{(n)}(a)\over n!}\,(x-a)^n + B(x-a)^{N+1}.$$
Since $x\not=a$, we can solve this for $B$, which is a
``constant''---it depends on $x$ and $a$ but those are temporarily
fixed.  Now we
have defined a function $F(t)$ with the property that
$F(a)=f(x)$. Also, all terms with a positive power of
$(x-t)$ become zero when we substitute $x$ for $t$, so
$\ds F(x)=f^{(0)}(x)/0!=f(x)$. So $F(a)=F(x)$.
By Rolle's theorem (\ref{thm:rolle}), we
know that there is a value $z\in(a,x)$ such that $F'(z)=0$. But what is $F'$?
Each term in $F(t)$, except the first term and the extra
term involving $B$, is a product, so to take the derivative we use the
product rule on each of these terms.
\begin{align*}
  F(t)=f(t)&+{f^{(1)}(t)\over 1!}(x-t)^1+{f^{(2)}(t)\over 2!}(x-t)^2+{f^{(3)}(t)\over 3!}(x-t)^3+\cdots	\\
  &+{f^{(N)}(t)\over N!}(x-t)^N+B(x-t)^{N+1}.
\end{align*}
So the derivative is
\begin{align*}
	  F'(t) = f'(t) &+\left({f^{(1)}(t)\over 1!}(x-t)^0(-1)+{f^{(2)}(t)\over 1!}(x-t)^1\right)	\\
  &+\left({f^{(2)}(t)\over 1!}(x-t)^1(-1)+{f^{(3)}(t)\over 2!}(x-t)^2\right)	\\
  &+\left({f^{(3)}(t)\over 2!}(x-t)^2(-1)+{f^{(4)}(t)\over 3!}(x-t)^3\right)+\dots+	\\
  &+\left({f^{(N)}(t)\over (N-1)!}(x-t)^{N-1}(-1)+{f^{(N+1)}(t)\over N!}(x-t)^N\right)	\\
  &+B(N+1)(x-t)^N(-1).
\end{align*}
The second term in each parenthesis cancel with the first term in the next one,
leaving just
$$F'(t) = {f^{(N+1)}(t)\over N!}(x-t)^N+B(N+1)(x-t)^N(-1).$$
At some $z$, $F'(z)=0$ so
\begin{align*}
  0&={f^{(N+1)}(z)\over N!}(x-z)^N+B(N+1)(x-z)^N(-1)	\\
  B(N+1)(x-z)^N&={f^{(N+1)}(z)\over N!}(x-z)^N	\\
  B&={f^{(N+1)}(z)\over (N+1)!}.
\end{align*}
Now we can write
$$
  F(t)=\sum_{n=0}^N{f^{(n)}(t)\over n!}\,(x-t)^n +
  {f^{(N+1)}(z)\over (N+1)!}(x-t)^{N+1}.
$$
Recalling that $F(a)=f(x)$ we get
$$
  f(x)=F(a)=\sum_{n=0}^N{f^{(n)}(a)\over n!}\,(x-a)^n +
  {f^{(N+1)}(z)\over (N+1)!}(x-a)^{N+1},
$$
which is what we wanted to show.
\end{proof}

It may not be immediately obvious that this is particularly useful;
let's look at some examples.

\begin{example}{}{}
Find a polynomial approximation for $\sin x$ accurate to $\pm 0.005$ for values of $x$ in $[-\pi/2,\pi/2]$.
\end{example}
\begin{solution}
From Taylor's theorem with $a=0$:
$$
  \sin x= \sum_{n=0}^N{f^{(n)}(a)\over n!}\,x^n +
  {f^{(N+1)}(z)\over (N+1)!}x^{N+1}.
$$
What can we say about the size of the term
$${f^{(N+1)}(z)\over (N+1)!}x^{N+1}?$$
Every derivative of $\sin x$ is $\pm\sin x$ or $\pm\cos x$, so
$\ds |f^{(N+1)}(z)|\le 1$.

So we need to pick $N$ so that
$$\left|{x^{N+1}\over (N+1)!}\right|< 0.005.$$
Since we have limited $x$ to $[-\pi/2,\pi/2]$,
$$\left|{x^{N+1}\over (N+1)!}\right|<{2^{N+1}\over (N+1)!}.$$
The quantity on the right decreases with increasing $N$, so all we
need to do is find an $N$ so that
$${2^{N+1}\over (N+1)!}<0.005.$$
A little trial and error shows that $N=8$ works,
and in fact $\ds 2^{9}/9!<0.0015$, so
\begin{align*}
  \sin x &=\sum_{n=0}^8{f^{(n)}(0)\over n!}\,x^n \pm 0.0015	\\
  &=x-{x^3\over 6}+{x^5\over 120}-{x^7\over 5040}\pm 0.0015.
\end{align*}
Figure~\ref{fig:sine approximation} shows the graphs of $\sin x$ and
and the approximation on $[0,3\pi/2]$. As $x$ gets larger, the
approximation heads to negative infinity very quickly, since it is
essentially acting like $\ds -x^7$.
\end{solution}

\begin{figure}[H]
%\texonly
\centerline{
\vbox{\beginpicture
\normalgraphs
%\ninepoint
\setcoordinatesystem units <2truecm,0.7truecm>
\setplotarea x from 0 to 5, y from -5 to 1
\axis left ticks numbered from -5 to 1 by 1 /
\axis bottom shiftedto y=0 ticks numbered from 1 to 5 by 1 /
\setquadratic
\plot 0.000 0.000 0.047 0.047 0.094 0.094 0.141 0.141 0.188 0.187
0.236 0.233 0.283 0.279 0.330 0.324 0.377 0.368 0.424 0.412
0.471 0.454 0.518 0.495 0.565 0.536 0.613 0.575 0.660 0.613
0.707 0.649 0.754 0.685 0.801 0.718 0.848 0.750 0.895 0.780
0.942 0.809 0.990 0.836 1.037 0.861 1.084 0.884 1.131 0.905
1.178 0.924 1.225 0.941 1.272 0.956 1.319 0.969 1.367 0.979
1.414 0.988 1.461 0.994 1.508 0.998 1.555 1.000 1.602 0.999
1.649 0.997 1.696 0.992 1.744 0.985 1.791 0.975 1.838 0.964
1.885 0.950 1.932 0.934 1.979 0.917 2.026 0.896 2.073 0.874
2.121 0.850 2.168 0.824 2.215 0.796 2.262 0.766 2.309 0.735
2.356 0.701 2.403 0.666 2.450 0.629 2.498 0.591 2.545 0.550
2.592 0.509 2.639 0.466 2.686 0.421 2.733 0.375 2.780 0.328
2.827 0.279 2.875 0.230 2.922 0.179 2.969 0.126 3.016 0.073
3.063 0.018 3.110 -0.037 3.157 -0.094 3.204 -0.152 3.252 -0.212
3.299 -0.272 3.346 -0.334 3.393 -0.397 3.440 -0.461 3.487 -0.527
3.534 -0.595 3.581 -0.664 3.629 -0.735 3.676 -0.808 3.723 -0.884
3.770 -0.962 3.817 -1.042 3.864 -1.125 3.911 -1.212 3.958 -1.302
4.006 -1.395 4.053 -1.493 4.100 -1.596 4.147 -1.703 4.194 -1.816
4.241 -1.936 4.288 -2.061 4.335 -2.194 4.383 -2.335 4.430 -2.484
4.477 -2.642 4.524 -2.811 4.571 -2.990 4.618 -3.181 4.665 -3.385
4.712 -3.602 /
\plot 0.000 0.000 0.047 0.047 0.094 0.094 0.141 0.141 0.188 0.187
0.236 0.233 0.283 0.279 0.330 0.324 0.377 0.368 0.424 0.412
0.471 0.454 0.518 0.495 0.565 0.536 0.613 0.575 0.660 0.613
0.707 0.649 0.754 0.685 0.801 0.718 0.848 0.750 0.895 0.780
0.942 0.809 0.990 0.836 1.037 0.861 1.084 0.884 1.131 0.905
1.178 0.924 1.225 0.941 1.272 0.956 1.319 0.969 1.367 0.979
1.414 0.988 1.461 0.994 1.508 0.998 1.555 1.000 1.602 1.000
1.649 0.997 1.696 0.992 1.744 0.985 1.791 0.976 1.838 0.965
1.885 0.951 1.932 0.935 1.979 0.918 2.026 0.898 2.073 0.876
2.121 0.853 2.168 0.827 2.215 0.800 2.262 0.771 2.309 0.740
2.356 0.707 2.403 0.673 2.450 0.637 2.498 0.600 2.545 0.562
2.592 0.522 2.639 0.482 2.686 0.440 2.733 0.397 2.780 0.353
2.827 0.309 2.875 0.264 2.922 0.218 2.969 0.172 3.016 0.125
3.063 0.078 3.110 0.031 3.157 -0.016 3.204 -0.063 3.252 -0.110
3.299 -0.156 3.346 -0.203 3.393 -0.249 3.440 -0.294 3.487 -0.339
3.534 -0.383 3.581 -0.426 3.629 -0.468 3.676 -0.509 3.723 -0.549
3.770 -0.588 3.817 -0.625 3.864 -0.661 3.911 -0.696 3.958 -0.729
4.006 -0.760 4.053 -0.790 4.100 -0.818 4.147 -0.844 4.194 -0.869
4.241 -0.891 4.288 -0.911 4.335 -0.930 4.383 -0.946 4.430 -0.960
4.477 -0.972 4.524 -0.982 4.571 -0.990 4.618 -0.996 4.665 -0.999
4.712 -1.000 /
\endpicture}}
\caption{$\sin x$ and a polynomial approximation.}
\label{fig:sine approximation}
%\endtexonly
\end{figure}

Note that we can now approximate the value of $\sin(x)$ to within 0.005 by
using simple trigonometric identities to translate $x$ into the interval $[-\pi/2,\pi/2]$.

We can extract a bit more information from this example. If we do not
limit the value of $x$, we still have
$$
  \left|{f^{(N+1)}(z)\over (N+1)!}x^{N+1}\right|\le
  \left|{x^{N+1}\over (N+1)!}\right|
$$
so that $\sin x$ is represented by
$$
  \sum_{n=0}^N{f^{(n)}(0)\over n!}\,x^n \pm
  \left|{x^{N+1}\over (N+1)!}\right|.
$$
If we can show that
$$
  \lim_{N\to\infty} \left|{x^{N+1}\over (N+1)!}\right|=0
$$
for each $x$ then
$$
  \sin x=\sum_{n=0}^\infty{f^{(n)}(0)\over n!}\,x^n
  = \sum_{n=0}^\infty (-1)^n{x^{2n+1}\over (2n+1)!},
$$
that is, the sine function is actually equal to its
Maclaurin series for all $x$. How can we prove that the limit is zero?
Suppose that $N$ is larger than $|x|$, and let $M$ be the largest
integer less than $|x|$ (if $M=0$ the following is even easier). Then
\begin{align*}
  {|x^{N+1}|\over (N+1)!} &= {|x|\over N+1}{|x|\over N}{|x|\over N-1}\cdots{|x|\over M+1}{|x|\over M}{|x|\over M-1}\cdots {|x|\over 2}{|x|\over 1}	\\
  &\le {|x|\over N+1}\cdot 1\cdot 1\cdots 1\cdot{|x|\over M}{|x|\over M-1}\cdots {|x|\over 2}{|x|\over 1}	\\
  &={|x|\over N+1}{|x|^M\over M!}.
\end{align*}
The quantity $|x|^M/ M!$ is a constant, so
$$
  \lim_{N\to\infty} {|x|\over N+1}{|x|^M\over M!} = 0
$$
and by the Squeeze Theorem (\ref{thm:SequenceSqueezeTheorem})
$$
  \lim_{N\to\infty} \left|{x^{N+1}\over (N+1)!}\right|=0
$$
as desired. Essentially the same argument works for $\cos x$ and $\ds
e^x$; unfortunately, it is more difficult to show that most functions
are equal to their Maclaurin series.

\begin{example}{}{}
Find a polynomial approximation for $\ds e^x$ near $x=2$
accurate to $\pm 0.005$.
\end{example}
\begin{solution}
From Taylor's theorem:
$$
  e^x= \sum_{n=0}^N{e^2\over n!}\,(x-2)^n +
  {e^z\over (N+1)!}(x-2)^{N+1},
$$
since $\ds f^{(n)}(x)=e^x$ for all $n$. We are interested in $x$ near 2,
and we need to keep $\ds |(x-2)^{N+1}|$ in check, so we may as well
specify that $|x-2|\le 1$, so $x\in[1,3]$. Also
$$\left|{e^z\over (N+1)!}\right|\le {e^3\over (N+1)!},$$
so we need to find an $N$ that makes $\ds e^3/(N+1)!\le 0.005$. This time
$N=5$ makes $\ds e^3/(N+1)!< 0.0015$, so the approximating polynomial is
$$
  e^x=e^2+e^2(x-2)+{e^2\over2}(x-2)^2+{e^2\over6}(x-2)^3+
  {e^2\over24}(x-2)^4+{e^2\over120}(x-2)^5
  \pm 0.0015.
$$

Note that our approximation requires that we already have a very
accurate approximation of the value $e^2$, which we shouldn't assume
we have in the context of trying to approximate $e^x$. For this reason
we typically try to center our series on values for which the derivative
of the function is easy to evaluate (e.g. $a=0$).
\end{solution}

Note well that in these examples we found polynomials of a certain
accuracy only on a small interval, even though the series for $\sin x$
and $\ds e^x$ converge for all $x$; this is typical. To get the same
accuracy on a larger interval would require more terms.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Opensolutionfile{solutions}[ex]
\section*{Exercises for \ref{sec:TaylorsTheorem}}

\begin{enumialphparenastyle}

\begin{ex}
Find a polynomial approximation for $\cos x$ on $[0,\pi]$,
accurate to $\ds \pm 10^{-3}$
\begin{sol}
$\ds 1-{x^2\over2}+{x^4\over24}-{x^6\over 720}
+\cdots+{x^{12}\over 12!}$
\end{sol}
\end{ex}

\begin{ex}
How many terms of the series for $\ln x$ centered at 1 are
required so that the guaranteed error on $[1/2,3/2]$ is
at most $\ds 10^{-3}$? What if the interval is instead $[1,3/2]$?
\begin{sol}
$1000$; $8$
\end{sol}
\end{ex}

\begin{ex}
Find the first three nonzero terms in the Taylor series for
$\tan x$ on $[-\pi/4,\pi/4]$,
and compute the guaranteed error term as given by
Taylor's theorem. (You may want to use Sage or a similar aid.)
\begin{sol}
$\ds x+{x^3\over3}+{2x^5\over15}$, error $\pm 1.27$.
\end{sol}
\end{ex}

\begin{ex}
Show that $\cos x$ is equal to its Taylor series for all $x$
by showing that the limit of the error term is zero as $N$ approaches
infinity.
\end{ex}

\begin{ex}
Show that $\ds e^x$ is equal to its Taylor series for all $x$
by showing that the limit of the error term is zero as $N$ approaches
infinity.
\end{ex}

\end{enumialphparenastyle}


\section{layman's overview}

The minimum to understand Anderson Localization is the concept of wave interference.

useful mathematical concepts: linear algebra, calculus, partial differential equations

-Emergence: whole is different from the sum of its parts
   -diffusion, localization can not be seen from the perspective of inter-molecular interactions.

localization, diffusion are processes that arise from multiple events. What's the minimum number of events to constitute diffusion, localization?
-how many people constitute a society?
-how many atoms needed to demonstrate a phase change?

Answer: as many samples as are needed to get ensemble behavior, such that adding more samples does not affect the distribution.

This is an important question computationally, as you want to be able to claim universal behavior using ensemble averages, but you want to be doing the minimum number of samples required.  For example, 100,000 realizations to yield 0.000001 accuracy. Another example, $L=5 \lambda$


% -*-latex-*-

\title{Orbiter Technical Notes: Planetary axis precession}
\author{Martin Schweiger}
\date{July 19, 2010}

\documentclass[a4paper]{article}
\usepackage[dvips]{graphicx}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{times}
\usepackage{cite}
\usepackage{wasysym}

\newcommand{\ACC}{\ensuremath\mathrm{A}}
\newcommand{\AACC}{\ensuremath\mathrm{T}}
\newcommand{\Euler}{\ensuremath\mathrm{E}}
\renewcommand{\vec}[1]{\ensuremath \mathbf{#1}}
\newcommand{\mat}[1]{\ensuremath \mathsf{#1}}

\begin{document}
\bibliographystyle{unsrt}

%\renewcommand{\vec}[1]{\ensuremath{\mathbf{#1}}}

\newcommand{\vR}[1]{\ensuremath{\vec{R}_{#1}}}
\newcommand{\nR}[1]{\ensuremath{|\vR{#1}|}}

\maketitle

\section{Introduction}
This document describes the implementation of planetary axis precession in Orbiter.

\section{Definitions}
The rotation axis of a celestial body is assumed to rotate around a \emph{precession reference axis} at constant obliquity angle and constant angular velocity. Currently, the orientation of the reference axis (OP) is considered time-invariant and is defined with respect to the ecliptic and equinox of J2000 (see Fig.~\ref{fig:prec_ref}). The axis orientation is defined by the obliquity $\varepsilon_\mathrm{ref}$ (the angle between the axis and the ecliptic north pole, $N$) and the angle from the vernal equinox  $\vernal$ to the ascending node of the ecliptic with respect to the body equator, $L_\mathrm{ref}$.
In Orbiter's left-handed system, $\vernal$ is defined as (1,0,0), and $N$ is defined as (0,1,0). The rotation matrix $\mat{R}_\mathrm{ref}$ for transforming from ecliptic to precession reference frame is then given by
\begin{equation}
\mat{R}_\mathrm{ref} = \left( \begin{array}{ccc}
\cos L_\mathrm{ref} & 0 & -\sin L_\mathrm{ref} \\
0 & 1 & 0 \\
\sin L_\mathrm{ref} & 0 & \cos L_\mathrm{ref} \end{array} \right)
\left( \begin{array}{ccc}
1 & 0 & 0 \\
0 & \cos\varepsilon_\mathrm{ref} & -\sin\varepsilon_\mathrm{ref} \\
0  & \sin\varepsilon_\mathrm{ref} & \cos\varepsilon_\mathrm{ref}
\end{array} \right).
\end{equation}
\begin{figure}[ht]
\includegraphics[width=\textwidth]{fig1.eps}
\caption{Orientation of the precession reference axis in the ecliptic frame.}
\label{fig:prec_ref}
\end{figure}
The planet's axis of rotation at some time $t$, OS, is given relative to the reference axis OP, by obliquity $\varepsilon_\mathrm{rel}$ and longitude $L_\mathrm{rel}$ (see Fig.~\ref{fig:prec_ecl}). $L_\mathrm{rel}$ is a linear function of time, and is defined as
\begin{equation}
L_\mathrm{rel}(t) = L_0 + 2\pi \frac{t-t_0}{T_p},
\end{equation}
where $t_0$ is a reference date, $L_0$ is the longitude at that date, and $T_p$ is the precession period.
The rotation from the precession reference frame to the planet's axis frame is described by
\begin{equation}
\mat{R}_\mathrm{rel}(t) = \left(\begin{array}{ccc}
\cos L_\mathrm{rel} & 0 & -\sin L_\mathrm{rel} \\
0 & 1 & 0 \\
\sin L_\mathrm{rel} & 0 & \cos L_\mathrm{rel}
\end{array} \right)
\left(\begin{array}{ccc}
1 & 0 & 0 \\
0 & \cos \varepsilon_\mathrm{rel} & -\sin \varepsilon_\mathrm{rel} \\
0 & \sin \varepsilon_\mathrm{rel} & \cos \varepsilon_\mathrm{rel}
\end{array} \right).
\end{equation}
\begin{figure}[ht]
\includegraphics[width=\textwidth]{fig2.eps}
\caption{Planet rotation axis.}
\label{fig:prec_ecl}
\end{figure}
The planet's rotation angle $\varphi(t)$ is defined via the siderial period $T_s$, and a rotation offset $\varphi_0$:
\begin{equation}\label{eq:rotation}
\varphi(t) = \varphi_0 + 2\pi \frac{t-t_0}{T_s} + [L_0 - L_\mathrm{rel}(t)] \cos \varepsilon_\mathrm{rel},
\end{equation}
where $t_0$ is a reference time (usually J2000.0). The last term in Eq.~\ref{eq:rotation} accounts for the difference between siderial and node-to-node rotation period.
The rotation is encoded in matrix $\mat{R}_\mathrm{rot}$:
\begin{equation}
\mat{R}_\mathrm{rot}(t) = \left( \begin{array}{ccc}
\cos\varphi & 0 & -\sin\varphi \\
0 & 1 & 0 \\
\sin\varphi & 0 & \cos\varphi
\end{array} \right).
\end{equation}
The full planet transformation is the combination of rotation and precession:
\begin{equation}\label{eq:fullrot}
\mat{R}(t) = \mat{R}_\mathrm{ref} \mat{R}_\mathrm{rel}(t) \mat{R}_\mathrm{rot}(t).
\end{equation}
The direction of the rotation axis is
\begin{equation}
\mathrm{OS}: \vec{s}(t) = \mat{R}(t) \left(\begin{array}{c}
0 \\ 1 \\ 0 \end{array}\right).
\end{equation}
The resulting axis obliquity and longitude of ascending node are
\begin{equation}\label{eq:eps_ecl}
\varepsilon_\mathrm{ecl}(t) = \cos^{-1} s_y(t), \qquad
L_\mathrm{ecl}(t) = \tan^{-1} \frac{-s_x(t)}{s_z(t)}.
\end{equation}
Figure \ref{fig:plots} shows examples of axis obliquity and longitude of ascending node over one precession cycle for different reference obliquities, as a function of $L_\mathrm{rel}$ (or equivalently, time).
\begin{figure}
\includegraphics[width=0.5\textwidth]{fig3a.eps}
\includegraphics[width=0.5\textwidth]{fig3b.eps}
\caption{Axis obliquity $\varepsilon_\mathrm{ecl}$ and longitude of ascending node $L_\mathrm{ecl}$ as a function of relative longitude $L_\mathrm{rel}$ over one precession cycle, for four different values of $\varepsilon_\mathrm{ref}$, and invariant parameters $\varepsilon_\mathrm{rel} = 20^\circ$, $L_\mathrm{ref}=40^\circ$.}
\label{fig:plots}
\end{figure}
Using $\varepsilon_\mathrm{ecl}$ and $L_\mathrm{ecl}$, an \emph{obliquity matrix} $\mat{R}_\mathrm{ecl}$ can be defined that rotates from ecliptic to the planet's current precession frame:
\begin{equation}\label{eq:rot_ecl}
\mat{R}_\mathrm{ecl}(t) = \left( \begin{array}{ccc}
\cos L_\mathrm{ecl} & 0 & -\sin L_\mathrm{ecl} \\
0 & 1 & 0 \\
\sin L_\mathrm{ecl} & 0 & \cos L_\mathrm{ecl} \end{array} \right)
\left( \begin{array}{ccc}
1 & 0 & 0 \\
0 & \cos\varepsilon_\mathrm{ecl} & -\sin\varepsilon_\mathrm{ecl} \\
0  & \sin\varepsilon_\mathrm{ecl} & \cos\varepsilon_\mathrm{ecl}
\end{array} \right).
\end{equation}
Note that like $\mat{R}_\mathrm{ref} \mat{R}_\mathrm{rel}$, matrix $\mat{R}_\mathrm{ecl}$ describes a rotation of the axis from ON to OS. However, there is a difference between the rotation around OS. Specifically, the reference axis for $\mat{R}_\mathrm{ecl}$ is the ascending node of the ecliptic with respect to the planet equator:
\begin{equation}
\mathrm{ON}_\mathrm{ecl}: \vec{n}(t) = \mat{R}_\mathrm{ecl}(t)
\left( \begin{array}{c} 1 \\ 0 \\ 0 \end{array} \right).
\end{equation}
The difference between $\mat{R}_\mathrm{ecl}$ and $\mat{R}_\mathrm{ref} \mat{R}_\mathrm{rel}$ can be expressed by an offset matrix $\mat{R}_\mathrm{off}$:
\begin{eqnarray}
\mat{R}_\mathrm{ecl}(t) \mat{R}_\mathrm{off}(t) &=& \mat{R}_\mathrm{ref} \mat{R}_\mathrm{rel}(t),\\
\mat{R}_\mathrm{off}(t) &=& \mat{R}^T_\mathrm{ecl}(t) \mat{R}_\mathrm{ref} \mat{R}_\mathrm{rel}(t).
\end{eqnarray}
$\mat{R}_\mathrm{off}$ describes a rotation around y, so it has the structure
\begin{equation}
\mat{R}_\mathrm{off}(t) = \left( \begin{array}{ccc}
\cos \varphi_\mathrm{off} & 0 & -\sin \varphi_\mathrm{off} \\
0 & 1 & 0 \\
\sin \varphi_\mathrm{off} & 0 & \cos \varphi_\mathrm{off} \end{array} \right),
\end{equation}
and the offset angle $\varphi_\mathrm{off}$ is given by
\begin{equation}
\varphi_\mathrm{off}(t) = \tan^{-1} \frac{-[\mat{R}_\mathrm{off}]_{13}}{[\mat{R}_\mathrm{off}]_{11}}.
\end{equation}
Including this offset into the planet's rotation angle leads to an expression for the planet's rotation angle $r(t)$ with respect to reference direction $\vec{n}(t)$:
\begin{equation}\label{eq:rotangle}
r(t) = \varphi(t) + \varphi_\mathrm{off}(t).
\end{equation}
We can now express the full rotation matrix $\mat{R}$ defined in Eq.~\ref{eq:fullrot}, using $\mat{R}_\mathrm{ecl}$ and $r$:
\begin{equation}
\mat{R}(t) = \mat{R}_\mathrm{ecl}(t) \tilde{\mat{R}}_\mathrm{rot}(t),
\end{equation}
where
\begin{equation}
\tilde{\mat{R}}_\mathrm{rot}(t) = \left( \begin{array}{ccc}
\cos r & 0 & -\sin r \\
0 & 1 & 0 \\
\sin r & 0 & \cos r
\end{array} \right).
\end{equation}

\section{Orbiter interface}
\subsection{Configuration}
The precession and rotation parameters supported in planet configuration files are listed in Table~\ref{tab:param}.
The following default assumptions apply:
\begin{itemize}
\item If PrecessionObliquity is not specified, $\varepsilon_\mathrm{ref}=0$ is assumed. (precession reference is ecliptic normal). In this case, the $L_\mathrm{ref}$ entry is ignored and $L_\mathrm{ref}=0$ is assumed.
\item If PrecessionPeriod is not specified, $T_p = \infty$ is assumed (rotation axis is stationary).
\item If LAN\_MJD is not specified, $t_0 = 51544.5$ is assumed (J2000.0).
\item If LAN is not specified, $L_0=0$ is assumed.
\item If Obliquity is not specified, $\varepsilon_\mathrm{rel}=0$ is assumed.
\item If SidRotPeriod is not specified, $T_s=\infty$ is assumed (no rotation).
\item If SidRotOffset is not specified, $\varphi_0=0$ is assumed.
\end{itemize}
For a retrograde precession of the equinoxes, a negative value of PrecessionPeriod should be used.

\begin{table}[hb]
\begin{tabular}{ll}
parameter & config entry \\ \hline
$T_s$ & SidRotPeriod [seconds] \\
$\varphi_0$ & SidRotOffset [rad] \\
$\varepsilon_\mathrm{rel}$ & Obliquity [rad] \\
$L_0$ & LAN [rad] \\
$t_0$ & LAN\_MJD [MJD] \\
$T_p$ & PrecessionPeriod [days] \\
$\varepsilon_\mathrm{ref}$ & PrecessionObliquity [rad] \\
$L_\mathrm{ref}$ & PrecessionLAN [rad]
\end{tabular}
\caption{Rotation and precession parameter entries in planet configuration files.}
\label{tab:param}
\end{table}

\subsection{API functions}
\subsubsection{void oapiGetPlanetObliquityMatrix (OBJHANDLE hPlanet, MATRIX3 *mat)}
This function returns $\mat{R}_\mathrm{ecl}(t)$ in Eq.~\ref{eq:rot_ecl} for planet \emph{hPlanet} at the current simulation time.

\subsubsection{double oapiGetPlanetObliquity (OBJHANDLE hPlanet)}
This function returns $\varepsilon_\mathrm{ecl}(t)$ in Eq.~\ref{eq:eps_ecl} for planet \emph{hPlanet} at the current simulation time.

\subsubsection{double oapiGetPlanetTheta (OBJHANDLE hPlanet)}
This function returns $L_\mathrm{ecl}(t)$ in Eq.~\ref{eq:eps_ecl} for planet \emph{hPlanet} at the current simulation time.

\subsubsection{double oapiGetPlanetCurrentRotation (OBJHANDLE hPlanet)}
This function returns the current rotation angle $r(t)$ in Eq.~\ref{eq:rotangle} for planet \emph{hPlanet} at the current simulation time.

\subsubsection{void oapiGetRotationMatrix (OBJHANDLE hPlanet, MATRIX3 *mat)}
This function returns $\mat{R}(t)$ in Eq.~\ref{eq:fullrot} for planet \emph{hPlanet} at the current simulation time.

\end{document}


\documentclass[12pt]{article}
\usepackage{cmbright}
\usepackage[margin=1in]{geometry}
\usepackage{fancyvrb}
\usepackage{amsmath}
\usepackage{graphics}

\newcommand{\keff}{k_{\text{eff}}}

\title{A trivial Monte Carlo eigenvalue calculation}
\author{James Paul Holloway}

\begin{document}

\maketitle

\section{Introduction}

Often, in order to understand a technique, we should reduce it to its simplest possible application, for if we can't understand it there, we can't understand it anywhere.  These notes outline the Monte Carlo approach to $k$-eigenvalue calculations in the simplest possible case: an infinite homogeneous medium.  Of course, the value of $\keff$ for such a system is known.  Using $\Sigma$ to denote macroscopic cross sections, and subscripts $f$ and $\gamma$ to denote fission and radiative capture, we have
\begin{equation}
\keff = k_\infty = \frac{\nu \Sigma_f}{\Sigma_a}
\end{equation}
where $\Sigma_a = \Sigma_f + \Sigma_\gamma$.

The ``transport equation'', such as it is in our simple case, is
\begin{equation}
\Sigma_t  \phi = {\nu \Sigma_f} \phi + \Sigma_s \phi \,,
\end{equation}
where $\Sigma_s$ is the scattering cross section and $\Sigma_t = \Sigma_a + \Sigma_s$.  The $k$ eigenvalue problem arises by scaling $\nu$ in such a way that this homogeneous equation supports a non-zero solution, so we consider
\begin{equation}
\Sigma_t  \phi = \frac{\nu \Sigma_f}{k} \phi + \Sigma_s \phi \,.
\end{equation}
Note that in this infinite homogeneous medium, the various ``operators'' of transport theory are just multiplication by various constants.

\section{Inverse power iteration}

Inverse power iteration for $k$ comes from isolating the $1/k$ scaled fission source
\begin{equation}
\Sigma_a  \phi = \frac{\nu \Sigma_f}{k} \phi \,.
\end{equation}
and ``inverting'' the transport operator on the left
\begin{equation}
\phi = \frac{1}{\Sigma_a} \biggl[\frac{\nu \Sigma_f}{k} \phi \biggr] = \frac{1}{k} \frac{1}{\Sigma_a} \biggl[Q \biggr]\,.  \label{eq:inverse}
\end{equation}
Note that if the fission source $Q = \nu\Sigma_f \phi$ is known, then evaluating the expression on the right simply requires solving a fixed source transport problem, a task to which Monte Carlo is well suited.

We can apply the fission operator (multiply by $\nu \Sigma_f$) to Eq.~\ref{eq:inverse} and arrive at
\begin{equation}
Q = \frac{1}{k} \frac{\nu\Sigma_f}{\Sigma_a} \biggl[Q \biggr] \label{eq:fp}
\end{equation}
This is identical to Martin's Eq. 2 in his 6/23/06 document, where his $M$ corresponds to my multiplication by $\nu\Sigma_f$, and his $A^{-1}$ to my division by $\Sigma_a$.  A Monte Carlo algorithm can evaluate $A^{-1}$, because that just means ``solve a fixed source problem,'' and it can tally the fission source that results from that solution in order to compute $MA^{-1}Q$. A Monte Carlo algorithm can also divide by $\Sigma_a$, as we will see, and such an algorithm can tally the effect of the multiplication by $\nu\Sigma_f$.

The inverse power iteration is then based on the iteration
\begin{equation}
Q^{n+1} = \frac{1}{k^n} \frac{\nu\Sigma_f}{\Sigma_a} \biggl[Q^n \biggr] \,.
\label{eq:Qiteration}
\end{equation}
To compute an update for $k$. in general we would take the inner product of Eq.~\ref{eq:fp} with some function $U$, converting it into a scalar equality, and solve for $k$.  Because we have only scalar operators it does not matter what ``$U$'' would be in this case, and we simply rearrange Eq.~\ref{eq:fp} into the form
\begin{equation}
k = \frac{\nu\Sigma_f \Sigma_a^{-1} Q}{Q} \,.
\end{equation}
Then, we can compute
\begin{equation}
k^{n+1} = \frac{\nu\Sigma_f \Sigma_a^{-1} Q^n}{Q^n} \,.
\end{equation}
This can be simplified because, by Eq.~\ref{eq:Qiteration}, $\nu\Sigma_f \Sigma_a^{-1} Q^n = k^n Q^{n+1}$, and so
\begin{equation}
k^{n+1} = k^n \frac{Q^{n+1}}{Q^n} \,.
\end{equation}
The complete iteration is thus
\begin{align}
Q^{n+1} &= \frac{1}{k^n} \frac{\nu\Sigma_f}{\Sigma_a} \biggl[Q^n \biggr] \label{eq:Qiter}\\
k^{n+1} &= k^n \frac{Q^{n+1}}{Q^n} \label{eq:kiter} \,.
\end{align}

\subsection{Convergence of the iteration}

It is easy to see that if $k_n = k_\infty$ then $Q^{n+1} = Q^n$ in Eq.~\ref{eq:Qiter} and then that $k^{n+1} = k^n = k_\infty$ in Eq.~\ref{eq:kiter}.  Thus, if the iteration converges, it converges to the correct result for $k$, and $Q$ is of course arbitrary.

Indeed, for any $Q_0, k_0$ we have
\begin{align}
Q^{1} &= \frac{1}{k^0} \frac{\nu\Sigma_f}{\Sigma_a} Q^0 = \frac{k_\infty}{k^0} Q^0\\
k^{1} &= k^0 \frac{Q^{1}}{Q^0} = k^0 \frac{k_\infty}{k^0} \frac{Q^0}{Q^0} = k_\infty \,.
\end{align}
In other words, the iteration will converge in one step! But what would a Monte Carlo implementation look like?

\subsection{Monte Carlo implementation}

At the start of each iteration we have an estimate for $Q^n$ and $k^n$.  In this infinite dimensional context $Q^n$ is just the number of fission neutrons that were released during iteration $n$.  To evaluate $Q^{n+1}$ we must compute the number of fission neutrons that are caused to be released by the known source $Q^n/k_n$.  But we must interpret this statement carefully; Eq.~\ref{eq:Qiter} says to take neutrons from the source $Q^n/k^n$, follow them until they are absorbed, and then to compute the fission source that would result from the implied flux.   The neutrons from \emph{this} source are \emph{not} transported as part of Eq.~\ref{eq:Qiter}.  We can equivalently say that we transport particles from the source $Q_n$, and when they are absorbed we compute the fission neutrons that they would release \emph{using a mean number of secondaries $\nu^n = \nu/k^n$}; we will use this latter view.

To solve Eq.~\ref{eq:Qiter} we select a number $N$ of histories to run per iteration (particles per cycle they say in the code world).  In our problem, all of our neutrons will be absorbed eventually (they have nowhere else to go).  When they are absorbed, the number, $m^n$, of fission neutrons released on average is $\nu/k^n$ times the probability of fission given that an absorption has occurred, or $m^n = (\nu/k^n) (\Sigma_f/\Sigma_a)$.  For reasons that have to do with the needs of space dependent cases, we need a pdf to sample that has this mean.  The one used is $p(s|m^n)$, where
\begin{equation}
p(s|m) = \begin{cases}
1 - (m - \lfloor m \rfloor) &s = \lfloor m \rfloor\\
(m - \lfloor m \rfloor) &s = \lfloor m \rfloor + 1\\
0&\text{otherwise}
\end{cases}
\end{equation}
where $s$ is the integer number of particles to be released in fission, and $\lfloor m \rfloor$ is the nearest integer less than or equal to $m$ (the floor function applied to $m$).  A direct calculation will show that $\langle s \rangle = m$ as desired.
To sample $p(s|m)$ for $s$, the expression
\begin{equation}
s = \bigl\lfloor m + \xi \bigr\rfloor
\end{equation}
is used, where $0 \leq \xi < 1$ is a uniformly distributed random number.

Note that the mean number of particles released per fission, $m^n$, and the pdf used to sample it, change from one iteration to the next because the mean number of secondaries $\nu^n = \nu/k_n$ varies from one iteration to the next.

\subsection{The Monte Carlo algorithm}

For each iteration (cycle) we:
\begin{enumerate}
\item Initialize a count of fission neutrons $\tilde Q^{n+1}$ to zero
\item compute
\begin{equation}
m^n = \frac{\nu \Sigma_f}{\Sigma_a k^n}
\end{equation}
\item Then for each history in the iteration we
sample $p(s|m^n)$, and add this sample $\tilde Q^{n+1}$.
\item At the end of the $N$ histories, we can compute the tally
\begin{equation}
Q^{n+1} = \frac{\tilde Q^{n+1}}{N} Q^n \,.
\end{equation}
Note in this that we compute the number of fission neutrons released per source particle (divide by the number of histories), times the magnitude of the previous source.
\item Finally, we can compute the new $k$ estimate
\begin{equation}
k^{n+1} = k^n \frac{Q^n \tilde Q^{n+1}/N}{Q^{n}} = k^n \frac{\tilde Q^{n+1}}{N} = k^n \frac{\text{number of fission neutrons banked}}{\text{particles per cycle}}
\end{equation}

\end{enumerate}

Note that the final two steps implement Eq.~\ref{eq:kiter}. We don't need Eq.~\ref{eq:Qiter} in this infinite medium case.

The algorithm can be compressed into, for each cycle,
\begin{enumerate}
\item Initialize a count of fission neutrons $\tilde Q^{n+1}$ to zero
\item For each history $\tilde Q^{n+1} = \tilde Q^{n+1} + \bigl\lfloor \nu \Sigma_f/({\Sigma_a k^n}) + \xi \bigr\rfloor$
\item At the end of the $N$ histories, we can compute the new $k$ estimate
\begin{equation}
k^{n+1} = k^n \frac{\tilde Q^{n+1}}{N} = k^n \frac{\text{number of fission neutrons banked}}{\text{particles per cycle}}
\end{equation}

\end{enumerate}

\section{The $k_\infty$ code}

The Matlab code below will implement the $k$ calculation.  It contains two methods, method 2 is what is described above.  Method 1 only contributes particles to the ``fission bank'' when there is a fission.  Because fewer particles contribute, it has a higher variance.  Here is a sample run:
\begin{Verbatim}
>> sigF = 1; sigG = 1; sigS = 0; nu = 2.5;
>> histPerCycle = 1000; cycles = 100;
>> k1 = kInfinity(histPerCycle, cycles, sigS, sigG, sigF, nu, 1);
>> k2 = kInfinity(histPerCycle, cycles, sigS, sigG, sigF, nu, 2);
>>
\end{Verbatim}

Figure~\ref{fig:k1000} shows the $k$ estimates over the first 100 cycles, using 1000 histories per cycle.  Note that the results are as good as they are going to be in about one cycle, because the deterministic iteration needs only one iteration in this trivial case.  After that. it's just fluctuations.

\begin{figure}
\begin{center}
\includegraphics{k1000.pdf}
\end{center}
\caption{$k$ estimates computed using two methods, showing 100 cycles and using 1000 histories per cycle. The exact value is $k_\infty = 2.5 \times 1 / (1 + 1) = 1.25$.  Note that Method 2 is lower variance.  The spread in Method 1 will reduce with more particles per history.}
\label{fig:k1000}
\end{figure}

Here is the code to calculate $k_\infty$ by Monte Carlo.

{\small
\VerbatimInput{kinfinity.m}
}

\end{document}