% -----------------------------*- LaTeX -*------------------------------
\documentclass[12pt]{report}
\usepackage{scribe_hgen486}
\def \baselinestretch{1.2}
\def\ni{\noindent}
\def\Be{\text{Be}}

\usepackage{amsmath,url,enumerate,graphicx}

\usepackage{chngcntr}
\counterwithout{table}{section}
\begin{document}

\scribe{Alan Hutchison}		% required
\lecturenumber{5}			% required, must be a number
\lecturedate{January 19}		% required, omit year
\lecturer{Matthew Stephens}

\maketitle

% please leave this comment
\framebox[.95\textwidth]{\parbox{.93\textwidth}{ {{\bf Note:}} These
lecture notes are still rough, and have only have been mildly
proofread.  }}
\vspace*{.1in}

% feel free to delete content below this line
% ----------------------------------------------------------------------

\section{Introduction to Bayesian Inference}

\emph{See github.com/stephens999/stat302 for further notes by M. Stephens on Bayesian Inference (Lects. 1-4)}

From last class:\
Posterior Odds = Prior Odds * Likelihood Ratio (aka Bayes Factor)

\[
\frac{Pr(M_1|x)}{Pr(M_0|x)} = \frac{P(M_1)}{P(M_0)} * \frac{P(x|M_1}{P(x|M_0)}
\]

This is the two model $M_1$ vs. $M_0$ case. We saw two examples of this in the previous lecture, regarding 1) Identifying Elephant Tusks and 2) Protein measurements in the blood. In these cases we were using likelihood ratios (LRs) for imagining a population that we're screening to tell us the conditional probability of a state (elephant type of tusk / disease state of patient) given data.\\

What if we only have 1 tusk and a LR of 1.8? We can think of the LR as a measure of the uncertainty of event (tusk is Savannah, tusk is Forest) based on everything we know. Instead of thinking in terms of a `measure of frequency' we can think in terms of a `measure of uncertainty'.\\

\begin{enumerate}
\item ``50\% chance of snow tomorrow'' This is simplest understood as the uncertainty of snow, not as a prediction of a hypothetical population of days.
\item The answer to ``Did it snow in Denver?'' can be expressed as an uncertainty, even though the event `snowed in Denver' is not a random variable.
\end{enumerate}

One way of thinking about uncertainty is by ``comparing an event to a standard'', such as a flip of a (fair) coin or a roll of a (fair) dice. It is not unreasonable to start with these standards (usually uniformly distributed probabilities). Heuristically, if one's prior odds are off by a factor of 10, then it starts to be more problematic.

\section{Comparison of more than two models}

Now, we will look at $k$ different models instead of 2 models, as we have done previously.\\

In the elephant case, for example, instead of thinking of just Forest vs. Savannah elephants, we could think about further subdividing these categories by genetic differences amongst the different cardinal directions (N E S W). \\

Let's refer to the different models as $Z_i \in \{1,2,...,k\}$, the likelihood of data $P(x_i|Z_i=k)$ as $L_{ik}$ and the prior probability $P(Z_i=k)$ as $\Pi_k$. We are using subscript $i$ to denote a series of tusks, but we could just as easily drop the $i's$.\\

Using the Law of Total Probability (LTP), we can write

\[
P(x_i) = \sum_{k'} P(x_i,Z_i=k') = \sum_{k'} P(x_i | Z_i=k') P(Z_i = k') = \sum_{k'} L_{ik'} \Pi_{k'}
\]
\[
P(Z_i=k|x_i) = \frac{P(x_i|Z_i=k)P(Z_i=k)}{P(x_i)} = \frac{L_{ik}\Pi_k}{\sum_{k'} L_{ik'} \Pi_{k'}}.
\]

Note that the denominator is simply the sum over $k$ of all possible numerators. We use the symbol $k'$ to denote the same set of states $k$, only for the sum across all $k$ of them. This means we can simply compute $L_{ik}\Pi_k$ for each $k$, then divide by the sum of them at the end. If all $k$ models are equally likely, then we end up just normalizing by the sum of the likelihoods.\\

Instead of using equalities, we can use proportions to avoid thinking about the normalizing factor. We can think about this as informing our posterior uncertainty by what we learn from the data (the likelihood) with what we knew or believed beforehand (the prior).
\[
Posterior \propto Likelihood * Prior
\]

Remember that the likelihoods themselves don't really matter, it's the ratios that give you information about the different models. In ratios the normalizing constant ends up canceling out altogether.

\section{Continuum of models case: Parametric model}
\subsection{Estimating an allele frequency}

As previously, sample 100 alleles, see 40 of type `1' and 60 of type `0'. $D$: Data. $M_q$: ``freq. of allele `1' is q''\\

Originally we used,
\[
L(q) = q^{40} (1-q)^{60}
\]

Maximizing this provides a Maximum Likelihood Estimate (MLE) point estimate for $q$, but we may be interested incorporating prior information or belief for $q$. We can compute the posterior distribution of $q$ using Bayes Theorem. Let our prior distribution on $q$ be $P(q)$.

\[
P(q|D) \propto q^{40} (1-q)^{60} * p(q)
\]
Assuming that all values of $q$ are equally probable $P(q)=1$

\[
P(q|D) \propto q^{40} (1-q)^{60}
\]
 now we need to normalize by $\int q'^{40}(1-q')^{60} dq'$. This normalization step can be a complicated integral, and there are some shortcuts that can be used here. Instead of solving out the integral, it is easier if we can recognize it as a standard distribution whose behavior we know.

 Since we are used the assumption that $P(q)=1$, i.e. all values of $q$ are equally probable, we can also write this uniform distribution as a Beta distribution of $q$, $Be(q;\alpha,\beta)=q^{1-\alpha} (1-q)^{1-\beta}$ with parameters $\alpha=1$ and $\beta=1$, $P(q) = Be(q;1,1) = q^0 (1-q)^0 = 1$.

\[
P(q|D) \propto q^{40} (1-q)^{60} * p(q) =  q^{40} (1-q)^{60} q^0 (1-q)^0 = q^{40} (1-q)^{60}
\]

Rewritten as a Beta distribution, we have

\[
P(q|D) \propto q^{40} (1-q)^{60} = q^{\alpha -1 = 40} (1-q)^{\beta -1 = 60} = Be(q;\alpha,\beta) = Be(q;41,61)
\]

Having an expression proportional to the posterior distribution $P(q|D)$, we have the means for summarizing $q$ as either a point estimate or as an interval. For point estimates, common choices include the \emph{posterior mean}, \emph{posterior median}, and \emph{posterior mode}. For an interval estimate, \emph{posterior quantiles} allow us to describe the spread of likely values of $q$. Giving the 2.5\% quantile and 97.5\% quantile provides the interval for which there is a 95\% probability that $q$ lies in it.

There are two takeaways from this section

\begin{enumerate}
\item For computing the posterior distribution in the continuous case, we use the same procedure as the the discrete case but we have the trick of using conjugate prior distributions to make the math simpler.
\item Once we have the posterior distribution, we can summarize it with both point estimates and interval estimates.
\end{enumerate}

\subsection{Conjugacy}

 For many types of likelihood functions there are corresponding prior \emph{conjugate} distributions such that when the two are multiplied together the resulting distribution is also in the same family of conjugate distributions. More formally, \emph{conjugacy means that the posterior distribution comes from the same family as the prior distribution}. These priors may not be realistic interpretations of the underlying mechanism of the prior distribution, but they are powerful and useful. For the Beta distributions in particular, any distribution with support of [0,1] can be approximated as a mixture of Beta distributions.

A question in class asked if there were any properties of likelihood functions that would indicate whether a conjugate prior distribution exists. The short answer is no, though it has been proven that exponential likelihood families have conjugate priors. Many common distributions have been identified as having conjugate priors, and they are summarized in Table \ref{tab:test}.

\begin{table}
\begin{center}
\begin{tabular}{|c|c|}\hline
Observed Likelihood Function & Conjugate Prior \\ \hline \hline
Binomial & Beta \\ \hline
Multinomial & Dirichlet \\ \hline
Mean of Normal & Normal \\ \hline
Variance of Normal & Inverse Normal \\ \hline
Mean of Poisson & Gamma \\ \hline
\end{tabular}
\caption{Observed Likelihood Functions and their Conjugate Priors}
\label{tab:test}
\end{center}
\end{table}

Let's return to the above example. Instead of assuming the uniform prior distribution $P(q) = Be(q;1,1)$, let's leave it as a general Beta distribution.

\[
P(q|D) \propto q^{40} (1-q)^{60} * p(q) =  q^{40} (1-q)^{60} q^{\alpha-1} (1-q)^{\beta-1} = q^{40+\alpha-1} (1-q)^{60+\beta-1} = Be(q;40+\alpha,60+\beta)
\]

Here we can see exactly how the choice of the prior distribution parameters $\alpha$ and $\beta$ can include the resulting posterior distribution. Figure \ref{fig:2015-01-19_beta} shows the Beta distribution for a variety of $\alpha,\beta$ combinations.

\begin{figure}
\includegraphics[width=\textwidth]{Betas.pdf}
\label{fig:2015-01-19_beta}
\caption{Beta distributions for different $\alpha$ and $\beta$ values. Black refers to the density, red to the distribution function, and green the quantile function.}
\end{figure}

 Given that it is reasonable to assume that the prior distribution of allele frequencies in population genetics is U-shaped, we can use a prior distribution of $Be(q;0.5,0.5)$.

 \[
P(q|D) \propto  q^{40} (1-q)^{60} q^{1/2-1} (1-q)^{1/2-1} = q^{40-1/2} (1-q)^{60-1/2} = Be(q;40.5,60.5)
\]
As you can see, this slightly changes our posterior distribution with a uniform prior, from $Be(q;41,61)$ to $Be(q;40.5,60.5)$

More generally, we can think of the observed alleles in our sample as $n_1$ of type ``1'' and $n_0$ of type ``0''. This means our posterior distribution will be

\[
P(q|D) \sim\  Be(q;n_1+\alpha,n_0+\beta)
\]

and the resulting posterior mean estimate for $q$ would be

\[
E[P(q|D)] = \frac{n_1+\alpha}{n_0+n_1+\alpha+\beta}
\]

Using a Maximum Likelihood Estimate (MLE) approach instead, our result would be independent of the prior distribution

\[
E[L(q|D)] = \frac{n_1}{n_0+n_1}
\]

Likewise, estimating $q$ solely off the prior distribution would be independent of observed data $n_0$ or $n_1$.

\[
E[Be(q;\alpha,\beta)] = \frac{\alpha}{\alpha+\beta}
\]

What this comparison of point estimates of $q$ shows is that the posterior mean of $q$ is a combination or tradeoff or compromise of the MLE estimate and the prior estimate: the results is `weighted' by the evidence presented by $n_0$ and $n_1$, but is balanced against the prior information or uncertainty about the population encoded in $\alpha$ and $\beta$. If $\alpha$ and $\beta$ are big relative to $n_0$ and $n_1$, then the posterior mean will be close to the prior mean. If $n_0$ and $n_1$ are big relative to $\alpha$ and $\beta$, then the posterior mean will be close to the MLE. ``In sufficient quantities, data can overwhelm belief.''

Note: in some situations, $\alpha$ and $\beta$ can be thought of as pseudo-counts. This can be useful in a situation where you want a prior that strongly discounts the possibility of an event but you don't want to make it zero. In that case, you could use a prior of $Be(1,100)$ to indicate the prior belief that it is extremely unlikely and then let the data determine whether the posterior probability of the event increases.

\subsection{Example: Estimating Normal Mean}

1 observation: data $X \sim\ N(\theta,\sigma^2)$

assuming $\sigma^2$ is known (e.g. measurement error).

Prior on $\theta$, $\theta \sim\ N(\mu_0,\sigma_0^2)$. We use the subscript `0' to denote that these are prior parameters.

\[
P(\theta|X=x) \propto\ prior * likelihood
\]
\[
P(\theta|X=x) \propto\ \exp{[\frac{-(\theta - \mu_0)^2}{2 \sigma_0^2}]} \exp{[\frac{-(x - \theta)^2}{2 \sigma^2}]}
\]

\[
P(\theta|X=x) \propto\ \exp{[\theta^2 [-\frac{1}{2\sigma_0^2}+\frac{1}{2\sigma^2}] + \theta[\frac{\mu_0}{\sigma_0^2}+\frac{x}{\sigma^2}]]}
\]

Given we are solving for the normal mean using a normal conjugate prior, we know our posterior distribution is going to have the form
\[
P(\theta|X=x) \propto\ N(\mu_1,\sigma_1^2) = \exp{[\frac{-(\theta - \mu_1)^2}{2 \sigma_1^2}]}
\]

which can be re-written without the constants as

\[
P(\theta|X=x) \propto\  \exp{[\theta^2\frac{-1}{2 \sigma_1^2}+\theta \frac{-\mu_1^2}{2 \sigma_1^2}]}
\]

Comparing this with the above equation, we can determine that

\[
\frac{1}{\sigma_1^2} = \frac{1}{\sigma_0^2} + \frac{1}{\sigma^2}
\]
The inverse of the variance $\frac{1}{\sigma^2}$ is referred to as the precision, which is mathematically sometimes more tractable. This equation means that our posterior precision is the sum of our prior precision and our precision from our data. Our precision gets bigger, and our variance gets smaller.

\[
\frac{\mu}{\sigma_1^2} = \frac{\mu_0}{\sigma_0^2} + \frac{x}{\sigma^2}
\]

This means that the product of our posterior mean and our posterior precision is the sum of the product of our prior mean and prior precision with product of our observed $x$ and observed precision.

\end{document}


\documentclass[a4paper,12pt]{article}

\begin{document}

\title{wassopies fififiifif}
\author{mr poops}
\date{\today}
\maketitle

\section{the poopings}
this is a chapter where i will teach you some math. let's go.
\subsection{start}
we must find out that $\frac{1}{2}$ is equivalent to $\frac{4}{8}.$
let's find it out.
$$\frac{1}{2} = \frac{1 \times 4}{2 \times 4} = \frac{4}{8}$$
\section{milk}
$\omega$ is $\Omega$ but lowercase
BUT!!!!!!!! DOES 9 + 77 = NOTHING?!!?!
we can find out by multiplying $77 \times 0$. haha jk. ok goodbye.
REMEBER THIS:
$$9 + 7 = Nothing$$
\section{I LIKE BIG TRUCKS AND I CAN NOT LIE}
\subsection{idk}
\subsubsection{O RELLY}
\end{document}


\input{includes/lab_preamble}

\def\LabCourse{AP Computer Science A}
\def\LabNumber{04}
\def\LabTitle{PRNG Lab}

\begin{document}
	\begin{coverpages}
		\ \\[2cm]
		\begin{center}
			\huge
			\textbf{\LabTitle}

			\Large
			\LabCourse
		\end{center}

		\vspace{1.5cm}

		\begin{center}
			\includegraphics[scale=0.45]{graphics/logo_black}

			\vspace{2.5cm}

			\Large
			Name: \rule{11.5cm}{0.1pt}
		\end{center}
	\end{coverpages}

	\thispagestyle{empty}
	\tableofcontents

	\pagebreak

	\section{Background}
		Within a computer system or program, a pseudo-random number generator (PRNG) is used to generate sequences of seemingly random numbers. Each PRNG is an algorithm to generate a sequence of numbers that mimic the properties of truly random numbers. They are called ``pseudo''-random because these algorithms are actually deterministic; that is, if you knew the starting value (called a generator's \emph{seed}) and the algorithm in use, you could easily predict all future numbers generated by that algorithm. Nonetheless, PRNGs in wide use today benefit from being completely mathematical in nature, meaning they are easily programmed in software and generally very fast. Their deterministic nature can be combated by choosing algorithms and seed values that result in long \emph{periods}. A \emph{period} of a PRNG is the length of the sequence of numbers before repetitions occur.\\[\baselineskip]
		This lab was designed to give you some exposure to the generation of random numbers. The following example shows you how a pseudo-random number generator works to generate a sequence of seemingly random numbers. The two activities that follow will ask you to implement your own pseudo-random number generators based on historic and well-known algorithms.

		\subsection{Example}
			\EBox{A bit of chaos...}{
					Work with basic chaos theory can provide for an interesting pseudo-random number generator.\\[\baselineskip]Given $0 < x_n < 1$ and $r > 0$, the following generating formula can create a sequence of numbers that seem to jump randomly in some subinterval of $[0, 1]$ after the first few iterations.
					\[ x_{n + 1} = rx_n(1 - x_n) \]

					Here is a sequence of values for $r = 3.63$ and $x_0 = 0.47$:
					\begin{center}
						\scalebox{0.7}{
							$\{0.470000000,\ 0.904233000,\ 0.314342325,\ 0.782378356,\ 0.618052744,\ 0.856910685,\ 0.445091590,\ 0.896555791,\ \ldots\}$
						}
					\end{center}
					Because of the way that this particular algorithm works, the seed value, $x_n$ is not nearly as important as the choice in $r$. Each will generate pretty distinct sequences, regardless of the initial seed value, $x_n$.

					For instance, here is a sequence of values for $r = 3.64$ and $x_0 = 0.47$:
					\begin{center}
						\scalebox{0.7}{
							$\{0.470000000,\ 0.944089000,\ 0.200054999,\ 0.606525056,\ 0.904492644,\ 0.327401808,\ 0.834595385,\ 0.523194068,\ \ldots\}$
						}
					\end{center}
					There will be, however, some values for $r$ which do not produce good sequences of seemingly random number. Here is a sequence of values for $r = 1.3$ with, again, $x_0 = 0.47$:
					\begin{center}
						\scalebox{0.7}{
							$\{0.470000000,\ 0.323830000,\ 0.284653370,\ 0.264713578,\ 0.253032389,\ 0.245709099,\ 0.240936979,\ 0.237752257,\ 0.235593957,\ 0.234116278,\ \ldots\}$
						}
					\end{center}
					Notice that the random numbers begin exhibiting ``stable'' (non-changing) values in certain positions, with $0.23\ldots$ beginning each of the random numbers towards the end of the printed sequence. In fact, no future generated number will begin with anything else, with an increasing number of decimal positions becoming stable as the sequence progresses. By the 92$^{\text{nd}}$ iteration, each value generated will be identical to at least $15$ decimal places..
			}

	\pagebreak

	\section{Applications}
		\QBox{Why are random numbers important to the study and application of computer science?}{4cm}
		\ \\[9pt]
		\QBox{Although hardware based ``true'' random number generators are available, software-based pseudo-random number generators still remain the predominant method for generating random numbers in use today. Why do you think developers continue to rely on pseudo-random number generators?}{4cm}
		\ \\[9pt]
		\QBox{Do you think a software based ``true'' random number generator will ever be created? Explain why or why not.}{4cm}
		\ \\[9pt]
		\QBox{Why is understanding the period of a sequence of random numbers important for the study, implementation, and use of a pseudo-random number generator?}{4cm}

	\pagebreak

	\section{Activity \#1}
		\subsection{Introduction}
			In 1949, John von Neumann proposed a method for generating pseudo-random numbers known as the ``Middle-Square Method''. This method works to generate numbers in the following way:
		  \begin{enumerate}
		    \item Beginning with an $n$-digit seed number, square the current number.
		    \item Select the middle $n$-digits of the resulting number. This is the new, pseudo-random number.
		    \item Repeat the process from Step \#1, using the new number.
		  \end{enumerate}
		  Although this method does appear to generate sequences of pseudo-random numbers, it quickly fell to criticism due to relatively short periods. Nonetheless, it is an academically interesting way of generating random numbers. In this activity, you will be implementing a number of methods related to generating pseudo-random numbers using the middle-square method.
			\ \\[18pt]
		  \EBox{Example}{
			  Here is an example of using the middle-square method to generate a sequence of pseudo-random numbers. We will begin with the $4$-digit seed value $5678$.
			  \[\begin{aligned}
			    (5678)^2 &= 32\underline{2396}84\\
			    (2396)^2 &= 05\underline{7408}16\\
			    (7408)^2 &= 54\underline{8784}64\\
			    &\ldots
			  \end{aligned}\]
			  In this case, the sequence of pseudo-random numbers would be: $\{5678, 2396, 7408, 8784, \ldots\}$, continuing until a repetition of a four-digit number would cause the sequence, or a subsequence of it, to begin again.\\[\baselineskip]
			  Note that because a sequence of $n$-digits is not always exactly centered in the resultant square, a choice needs to be made as to which $n$-digits should be selected. This occurred with the selection of $7408$ in the example above. Additionally, in order to form an $n$-digit number, numbers prefixed with as many as $n$ zeroes can be considered as part of the sequence of $n$-digit numbers (e.g., $0032$ can be considered a $4$-digit number).
			}

	  \subsection{Exercises}
	  %Implement middlesquares method...
	    \begin{enumerate}
	      \item Implement the \code{getMiddleSquare} method which takes as its parameters \code{x}, the current number in a sequence of pseudo-random numbers and \code{n}, the number of digits required for the generated number. This method should return the next number in the sequence using the middle-square method described above.
	      \item Overload \code{getMiddleSquare} to additionally take as a parameter, \code{N}, the number of pseudo-random numbers to generate. The method should return an array of integer values generated successively using the middle-square method.
	      \item Implement the \code{getMiddleSquarePeriod} method which will take as a parameter the beginning seed number, \code{x}, for a sequence of pseudo-random numbers generated using the middle-square method and \code{n}, the number of digits for each pseudo-random number in the sequence. This method should return the length of the sequence before any repetition occurs.
	    \end{enumerate}

  \pagebreak

	  \subsection{Questions}
	    \QBox{Find a four-digit, non-trivial seed number with a period of less than $10$. How did you conduct your search?}{6cm}
	    \ \\[9pt]
	    \QBox{Why does the existence of numbers such as those you discovered in Question \#5 mean that the middle-square method for generating pseudo-random number generators is not of practical use?}{6cm}

  \pagebreak

	\section{Activity \#2}
		\subsection{Introduction}
			The PRNG built into many programming languages and/or their libraries (including those accessible by Java's \code{Random} and \code{Math}) use a method known as a \emph{Linear Congruential Generator}. This technique generates the next pseudo-random number in a sequence of seemingly random numbers using the following formula:
			\[	x_{n + 1} = (kx_{n} + c)\ \mathbf{mod}\ m \]
			This method requires the selection of three parameters: $k$, $c$, and $m$ as well as an initial seed value, $x_0$. As with most pseudo-random number generators, the selection of these parameters becomes an important part of the algorithm implementation in order to generate a convincingly ``random'' sequence of numbers.
			\ \\[18pt]
			\EBox{Example}{
				Here is an example using a linear congruential generator with the following values:
				\begin{center}
					\begin{tabular}{l l l l}
						$k = 17$ & $c = 42$ & $m = 217$ & $x_0 = 72$
					\end{tabular}
				\end{center}
				These values yield the following sequence of numbers:
				\[\{72,\ 181,\ 81,\ 117,\ 78,\ 66,\ 79,\ 83,\ 151,\ 5,\ 127,\ 31,\ 135,\ 167,\ 60,\ \ldots\}\]
				Notice, however, what happens with a small change to the parameter $m$:
				\begin{center}
					\begin{tabular}{l l l l}
						$k = 17$ & $c = 42$ & $m = 216$ & $x_0 = 72$
					\end{tabular}
				\end{center}
				These values now yield the following sequence of numbers:
				\[\{72,\ 186,\ 180,\ 78,\ 72,\ 186,\ 180,\ 78,\ \ldots\}\]
				Once a repetition has occurred the sequence of numbers will continue indefinitely, indicating a very short period.
			}

		\subsection{Exercises}
			\begin{enumerate}
				\item Implement the \code{getLCG} method that will take as parameters \code{k}, \code{c}, \code{m} and a current value, \code{x}, and returns the next value in the sequence generated by the linear congruential generator method described in the introduction.
				\item Overload the \code{getLCG} method to additionally take as a parameter \code{N}, the number of pseudo-random numbers to generate. This method should return an array of \code{N} integer values.
	      \item Implement the \code{getLCGPeriod} method which will take as parameters \code{k}, \code{c}, \code{m} and an initial seed value, \code{x}, and returns the length of the sequence before any repetition occurs.
			\end{enumerate}

	\pagebreak

		\subsection{Questions}
			\QBox{Language implementations of a linear congruential generator use large powers of $2$ as the $m$ parameter (Java, for instance, uses $2^{48}$). This allows for the generation of numbers with $n-$bit precision (say, $32$-bit integers) by truncating the generated number to the first $n$-bits. Which part of the LCG formula does this make simpler? Explain why.}{6cm}
			\ \\[9pt]
			\QBox{Explain how you would generate random, floating-point numbers using a linear congruential generator.}{6cm}

	\pagebreak

	\section{Final Analysis}
		\QBox{Many language-implemented pseudo-random number generators will use a different seed value each time they are called. This seed value is usually derived from some system factors, such as the current system time. What are the benefits and drawbacks to this approach?}{3.5cm}
		\ \\[9pt]
		\QBox{Languages that use the linear congruential generator method for generating pseudo-random numbers, such as Java, usually do not vary the parameters other than the initial seed value. Java, for instance, has values: $k = 25214903917$, $c = 11$, and $m = 2^{48}$. Why do you think these languages use established parameters for their LGC rather than generate them at the time of use?}{3.5cm}
		\ \\[9pt]
		\QBox{What part of implementing either the middle-square method or the linear congruential generator method for generating pseudo-random numbers did you find most challenging? How did you overcome this challenge?}{3.5cm}
		\ \\[9pt]
	  \QBox{What new programming techniques or knowledge did you learn as a result of this lab?}{3.5cm}

	\pagebreak
	\blankpage
	\pagebreak

	\section{Template Class \& Test Cases}
		\lstinputlisting[basicstyle=\small\ttfamily,tabsize=2]{files/PRNG.java}

	\pagebreak
	\blankpage

	%Scoring Matrix
	\section*{Scoring Matrix}
	\vspace{0.25cm}

	\renewcommand{\arraystretch}{2}
	\begin{tabular} {*{4}{*{3}{| >{\bfseries\centering}p{0.0575\textwidth}}}|}
		\hline
		\multicolumn{12}{| c |}{\bfseries\Large\LabTitle}\\
		\hline
		\multicolumn{3}{| c |}{\bfseries Applications} & \multicolumn{3}{| c |}{\bfseries Activity \#1} & \multicolumn{3}{| c |}{\bfseries Activity \#2} & \multicolumn{3}{| c |}{\bfseries Final Analysis}\\
		\hline
		Q01 & 1 & \ & EX1 & 3 & \ & EX1 & 3 & \ & Q09 & 1 & \ \tabularnewline
		\hline
		Q02 & 1 & \ & EX2 & 2 & \ & EX2 & 2 & \ & Q10 & 1 & \ \tabularnewline
		\hline
		Q03 & 1 & \ & EX3 & 4 & \ & EX3 & 4 & \ & Q11 & 1 & \ \tabularnewline
		\hline
		Q04 & 1 & \ & Q05 & 1 & \ & Q07 & 1 & \ & Q12 & 1 & \ \tabularnewline
		\hline
		\multicolumn{3}{ c |}{\ } & Q06 & 1 & \ & Q08 & 1 & \ & \multicolumn{3}{| c}{\ }\tabularnewline
		\cline{4-9}
	\end{tabular}

	\vspace{0.25cm}
	\textbf{Comments:}
\end{document}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% fphw Assignment
% LaTeX Template
% Version 1.0 (27/04/2019)
%
% This template originates from:
% https://www.LaTeXTemplates.com
%
% Authors:
% Class by Felipe Portales-Oliva (f.portales.oliva@gmail.com) with template
% content and modifications by Vel (vel@LaTeXTemplates.com)
%
% Template (this file) License:
% CC BY-NC-SA 3.0 (http://creativecommons.org/licenses/by-nc-sa/3.0/)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%----------------------------------------------------------------------------------------
%    PACKAGES AND OTHER DOCUMENT CONFIGURATIONS
%----------------------------------------------------------------------------------------

\documentclass[
    12pt, % Default font size, values between 10pt-12pt are allowed
    %letterpaper, % Uncomment for US letter paper size
    %spanish, % Uncomment for Spanish
]{fphw}

% Template-specific packages
\usepackage[utf8]{inputenc} % Required for inputting international characters
\usepackage[T1]{fontenc} % Output font encoding for international characters
\usepackage{fontspec,unicode-math} % Required for using utf8 characters in math mode
\usepackage{parskip}  % To add extra space between paragraphs
% \usepackage{mathpazo} % Use the Palatino font
\usepackage{graphicx} % Required for including images
\usepackage{booktabs} % Better horizontal rules in tables
\usepackage{hyperref} % For links (both internal and external)
% \usepackage{listings} % Required for insertion of code
\usepackage{enumerate}% To modify the enumerate environment
\usepackage{cleveref} % Better \ref command -> \cref
\usepackage{import}   % This 4 packages and the command allow importing pdf
\usepackage{xifthen}  % figures generated with inkscape
\usepackage{pdfpages} % Source: https://castel.dev/post/lecture-notes-2/
\usepackage{mathtools}
\usepackage{wrapfig}
\usepackage{cancel}
\usepackage{transparent}
\newcommand{\incfig}[1]{%
    \def\svgwidth{0.95\columnwidth}
    \small
        \import{./images/}{#1.pdf_tex}
}

\setlength{\parindent}{15pt}
\setlength{\headheight}{22.66pt}

%----------------------------------------------------------------------------------------
%    ASSIGNMENT INFORMATION
%----------------------------------------------------------------------------------------

\title{Task 5 \\ Stereographic Projection} % Assignment title

\author{Emilio Domínguez Sánchez} % Student name

\date{November 4th, 2020} % Due date

\institute{University of Murcia \\ Faculty of Mathematics} % Institute or school name

\class{Geometría de Superficies} % Course or class name

\professor{Dr. Pascual Lucas Saorin} % Professor or teacher in charge of the assignment

%----------------------------------------------------------------------------------------
%    Definitions
%----------------------------------------------------------------------------------------

\usepackage{physics}
\newcommand{\R}{\mathbb{R}}
\newcommand{\sphere}{{\mathbb{S}^2}}
\newcommand{\inner}[2]{\left\langle #1, \; #2 \right\rangle}

\begin{document}

\maketitle % Output the assignment title, created automatically using the information in the custom commands above

%----------------------------------------------------------------------------------------
%    ASSIGNMENT CONTENT
%----------------------------------------------------------------------------------------

\section*{Problem}

\begin{problem}

The stereographic projection is a function that projects the sphere given by the equation

\begin{equation*}
    \sphere = \qty{\mqty(x & y & z) : x^2 + y^2 + (z-1)^2 = 1}
\end{equation*}

onto the plane $z = 0$, which we identify with $\R^2$.
The projection; $π$; maps the point $p = \mqty(x & y & z) \in \sphere - \qty{N}$,
where $N = \mqty(0 & 0 & 2)$,
to the insersection of the plane $z = 0$ with the line that connects $N$ and $p$.

\begin{enumerate}
    \item Prove that $X = π^{-1} : \R^2 \to \sphere - \qty{N}$
    is a parametrization of the sphere.

    \item Find the coefficients of the first fundamental form of $X$.
\end{enumerate}
\end{problem}

%----------------------------------------------------------------------------------------

\subsection*{Answer}

    The statement talks about $π$ and gives a valid definition for it because
the intersection of a line and a plane is at most one point and
the lines that join $N$ and a different point are never parallel to the plane $z=0$.
However, all the questions are related to $X ≔ π^{-1}$.
Out approach finds the expression for $X$ without computing the expression of $π$.
Nevertheless, we can use the fact that we know that $π$ is well defined
to define $X$ in a way that it is $π^{-1}$ (by construction)
and is therefore biyective (hence inyective),
which comes in handy to prove that it is a parametrization
without proving that the inverse is also continuos.
That is, without finding an expression for $π$.

    Take a point $\mqty(u & v)$ of $\R^2$.
$X(u, v)$ is the image of a point $\mqty(x & y & z)$ if and only if
$\mqty(u & v & 0)$ is the intersection of the plane $z = 0$ and
the line $l ≔ \qty{N + λ(\mqty(x & y & z)-N)}_{λ \in \R}$.
Hence, $l$ is also the line that passes through $N$ and $\mqty(u & v & 0)$,
and $π^{-1}(\qty{\mqty(u & v)})$ is, by definition, $l \cap (\sphere - N)$.

\begin{equation*}
    l =
    \qty{\mqty(u & v & 0) + λ\qty(N-\mqty(u & v & 0))}_{λ \in \R} =
    \qty{\mqty(u(1-λ) & v(1-λ) & 2λ)}_{λ \in \R}. \\
\end{equation*}
\begin{equation*}
    l \cap \sphere = \\
    \qty{\mqty(u(1-λ) & v(1-λ) & 2λ)}_{λ \in \R} \cap \qty{x^2 + y^2 + (z-1)^2 = 1},
\end{equation*}

\pagebreak

    Substituting the parametric equations of $l$ into the implicit equation of $\sphere$
leaves us with the equation\footnote{
Note that we know $l(1) = N \in \sphere$,
which means that $λ = 1$ is a solution of the equation.}

\begin{gather*}
    \begin{aligned}
        u^2(1-λ)^2 + v^2(1-λ)^2 + (2λ-1)^2 &= 1 \\
        (1-λ)\qty[(u^2+v^2)(1-λ) - 4λ] &= 0,
    \end{aligned} \\
\end{gather*}

\noindent
of solutions $λ = 1$ and $λ = \frac{u^2+v^2}{u^2+v^2+4} = 1 - \frac{4}{u^2+v^2+4}$.
That gives

\begin{align*}
    l \cap \sphere &=
    \qty{
        \mqty(\frac{4u}{u^2+v^2+4} & \frac{4v}{u^2+v^2+4} & 2-\frac{8}{u^2+v^2+4}),
        N
    } \qq{and} \\
    l \cap (\sphere - N) &=
    \qty{
        \mqty(\frac{4u}{u^2+v^2+4} & \frac{4v}{u^2+v^2+4} & 2-\frac{8}{u^2+v^2+4})
    }.
\end{align*}

    The fact that the intersection is defined for all $\mqty(u & v)$ and is unique
proves that $π$ is a biyection between $\sphere - N$ and $\R^2$.
And the expression for $π^{-1}$ would be

\begin{equation*}
    X(u, v) =
    \mqty(\dfrac{4u}{u^2+v^2+4} & \dfrac{4v}{u^2+v^2+4} & 2-\dfrac{8}{u^2+v^2+4}).
\end{equation*}

    Now we foucs on the properties of $X$. Its differential is

\begin{equation*}
    %TODO \mqty was not handled as I expected in a subindex
    \dd{X}_{(u \; v)} =
    \pmqty{
    -4u^2 + 4v^2 + 16 & -8uv \\
    -8uv & 4u^2 - 4v^2 + 16 \\
    16u & 16v \\
    } \times \frac{1}{(u^2+v^2+4)^2}
\end{equation*}

\noindent
which is of maximum range because

\begin{multline*}
    \mqty|
    -4u^2 + 4v^2 + 16 & -8uv \\
    -8uv & 4u^2 - 4v^2 + 16
    | = \\
    16^2 - (4u^2-4v^2)^2 - (8uv)^2 =
    16^2 - 16(u^4+v^4) + 32u^2v^2 - 64u^2v^2 = \\
    16^2 - (4(u^2+v^2))^2,
\end{multline*}
\begin{multline*}
    \mqty|
    -4u^2 + 4v^2 + 16 & -8uv \\
    16u & 16v \\
    | = \\
    -64u^2v + 64v^3 + 256v + 128u^2v =
    v(64(u^2 + v^2) + 256)
\end{multline*}
\noindent
and
\begin{multline*}
    \mqty|
    -8uv & 4u^2 - 4v^2 + 16 \\
    16u & 16v \\
    | = \\
    -128uv^2 - 64u^3 + 64uv^2 - 256u =
    u(-64(u^2 + v^2) - 256)
\end{multline*}

\noindent
are $0$ when $u^2+v^2 = 4$, $v = 0$ and $u = 0$, respectively,
which cannot happen at the same time.
All the work up until now,
plus the fact that $X$ in inyective because it is the inverse of $π$,
is enough to apply the technical result that proves that
$X^{-1}$ exists and is continuous.
In other words, to show that $X$ is a parametrization of $\sphere$.

\subsubsection*{Coefficients}

\newcommand{\dX}[1]{{\pdv{X_{(u \; v)}}{#1}}}

Writing

\begin{align*}
    \dX{u} &= \frac{\mqty(-4u^2 + 4v^2 + 16 & -8uv & 16u)}{(u^2+v^2+4)^2}, \\
    \dX{v} &= \frac{\mqty(-8uv & 4u^2 - 4v^2 + 16 & 16v)}{(u^2+v^2+4)^2};
\end{align*}

\noindent
we get that the coefficients are

\begin{multline*}
    {E = \inner{\dX{u}}{\dX{u}} = } \\
    \frac{1}{(u^2+v^2+4)^4}
        \qty(16u^4+16v^4+256-32u^2v^2-128u^2+128v^2 + 64u^2v^2 + 256u^2) = \\
    \frac{1}{(u^2+v^2+4)^4}
        \qty(16u^4+16v^4 +32u^2v^2 + 128u^2 + 128v^2 + 256) = \\
    \frac{16}{(u^2+v^2+4)^4}
        \qty(u^4+v^4 +2u^2v^2 + 8u^2 + 8v^2 + 16) =
    \frac{16}{(u^2+v^2+4)^2},
\end{multline*}
\begin{multline*}
    {G = \inner{\dX{v}}{\dX{v}} = }
    \hfill \text{(by symmetry)} \hfill =
    \frac{16}{(u^2+v^2+4)^2}
\end{multline*}
and
\begin{multline*}
    {F = \inner{\dX{u}}{\dX{v}} = }
    \hfill \frac{1}{(u^2+v^2+4)^4} \qty(-128uv - 128uv + 256uv) \hfill =
    0.
\end{multline*}

(Note that $X$ is an orthogonal parametrization because $F = 0$.)

%----------------------------------------------------------------------------------------

\end{document}


\chapter{Arrays and references}

Up to this point, the only variables we have used were for individual values such as numbers or strings.
In this chapter, we'll learn how to store multiple values of the same type using a single variable.
This language feature will enable you to write programs that manipulate larger amounts of data.

For example, Exercise~\ref{doubloon} asked you to check whether every letter in a string appears exactly twice.
One algorithm (which hopefully you already discovered) is to loop through the string 26 times, once for each lowercase letter:

\begin{code}
for (char c = 'a'; c <= 'z'; c++) {
    // count how many times the letter appears
    // if the count is not 0 or 2, return false
}
\end{code}

This ``nested loops'' approach is inefficient, especially when the string is long (e.g., one billion characters).
Another algorithm would initialize 26 variables to zero, loop through the string once, and use a giant \java{if} statement update the variable for each letter.
But who wants to declare 26 variables?

That's where arrays come in.
We can declare a single variable that stores 26 integers.
Rather than use an \java{if} statement to update each value, we can use arithmetic to update the $n$th value directly.
We will present this algorithm at the end of the chapter.

\section{Creating arrays}

\index{array}
\index{element}

An {\bf array} is a sequence of values; the values in the array are called {\bf elements}.
You can make an array of \java{int}s, \java{double}s, \java{String}s, or any other type, but all the values in an array must have the same type.

\index{type!array}
\index{[ ] square brackets}
\index{brackets!square}

To create an array, you have to declare a variable with an {\em array type} and then create the array itself.
Array types look like other Java types, except they are followed by square brackets (\java{[]}).
For example, the following lines declare that \java{counts} is an ``integer array'' and \java{values} is a ``double array'':

\begin{code}
int[] counts;
double[] values;
\end{code}

\index{new}
\index{operator!new}
\index{allocate}

To create the array itself, you have to use the \java{new} operator, which we first saw in Section~\ref{scanner}.
The \java{new} operator {\bf allocates} memory for the array and automatically initializes all of its elements to zero.

\begin{code}
counts = new int[4];
values = new double[size];
\end{code}

The first assignment makes \java{counts} refer to an array of four integers.
The second makes \java{values} refer to an array of \java{double}s, but the number of elements depends on the value of \java{size} (at the time the array is created).

Of course, you can also declare the variable and create the array with a single line of code:

\begin{code}
int[] counts = new int[4];
double[] values = new double[size];
\end{code}

\index{NegativeArraySizeException}
\index{exception!NegativeArraySize}

You can use any integer expression for the size of an array, as long as the value is nonnegative.
If you try to create an array with \java{-4} elements, for example, you will get a \java{NegativeArraySizeException}.
An array with zero elements is allowed, and there are special uses for such arrays that we'll see later on.

\section{Accessing elements}
\label{elements}

When you create an array with the \java{new} operator, the elements are initialized to zero.
Figure~\ref{fig.array} shows a memory diagram of the \java{counts} array so far.

\begin{figure}[!ht]
\begin{center}
\includegraphics{figs/array.pdf}
\caption{Memory diagram of an \java{int} array.}
\label{fig.array}
\end{center}
\end{figure}

\index{reference}

The arrow indicates that the value of \java{counts} is a {\bf reference} to the array.
You should think of {\em the array} and {\em the variable} that refers to it as two different things.
As we'll soon see, we can assign a different variable to refer to the same array, and we can change the value of \java{counts} to refer to a different array.

\index{element}
\index{index}
\index{array!element}
\index{array!index}

The large numbers inside the boxes are the elements of the array.
The small numbers outside the boxes are the {\bf indexes} used to identify each location in the array.
As with strings, the index of the first element is 0, not 1.
For this reason, we sometimes refer to the first element as the ``zeroth'' element.

The \java{[]} operator selects elements from an array:

\begin{code}
System.out.println("The zeroth element is " + counts[0]);
\end{code}

You can use the \java{[]} operator anywhere in an expression:

\begin{code}
counts[0] = 7;
counts[1] = counts[0] * 2;
counts[2]++;
counts[3] -= 60;
\end{code}

Figure~\ref{fig.array2} shows the result of these statements.

\begin{figure}[!ht]
\begin{center}
\includegraphics{figs/array2.pdf}
\caption{Memory diagram after several assignment statements.}
\label{fig.array2}
\end{center}
\end{figure}

You can use any expression as an index, as long as it has type \java{int}.
One of the most common ways to index an array is with a loop variable.
For example:

\begin{code}
int i = 0;
while (i < 4) {
    System.out.println(counts[i]);
    i++;
}
\end{code}

This \java{while} loop counts up from 0 to 4.
When \java{i} is 4, the condition fails and the loop terminates.
So the body of the loop is only executed when \java{i} is 0, 1, 2, and 3.
In this context, the variable name \java{i} is short for ``index''.

\index{loop variable}
\index{variable!loop}

Each time through the loop we use \java{i} as an index into the array, displaying the \java{i}th element.
This type of array processing is usually written as a \java{for} loop.

\begin{code}
for (int i = 0; i < 4; i++) {
    System.out.println(counts[i]);
}
\end{code}

\index{ArrayIndexOutOfBoundsException}
\index{exception!ArrayIndexOutOfBounds}

For the \java{counts} array, the only legal indexes are 0, 1, 2, and 3.
If the index is negative or greater than 3, the result is an \java{ArrayIndexOutOfBoundsException}.

\section{Displaying arrays}
\label{printarray}

\index{array!printing}

You can use \java{println} to display an array, but it probably doesn't do what you would like.
%slr:  8-21-19
For example, the following trinket program (1) declares an array variable, (2) makes it refer to an array of four elements with an \textbf{array initializer}, and (3) attempts to display the contents of the array using \java{println}:
%
%\begin{code}
%int[] a = {1, 2, 3, 4};
%System.out.println(a);
%\end{code}

% height = 130 + 11 * num_lines
\begin{trinket} [230] {PrintArrayReference.java}
public class PrintArrayReference {

    public static void main(String[] args) {
       int[] a = {1, 2, 3, 4}; //array initializer
       /* above statement is equivalent to the following
       int[] a = new int[4];
       a[0] = 1;
       a[1] = 2;
       a[2] = 3;
       a[3] = 4;  */
       System.out.println(a);  //a is a *refernce* to an array
    }
}
\end{trinket}

Run the program to see what you get. Unfortunately, the output is something like:
%slr: end 8-21-19

\begin{stdout}
[I@bf3f7e0
\end{stdout}

The bracket indicates that the value is an array, \java{I} stands for ``integer'', and the rest represents the address of the array in memory.
If we want to display the elements of the array, we could do it ourselves:

%slr:  8-21-19
%\begin{code}
%public static void printArray(int[] a) {
%    System.out.print("{" + a[0]);
%    for (int i = 1; i < a.length; i++) {
%        System.out.print(", " + a[i]);
%    }
%    System.out.println("}");
%}
%\end{code}
% height = 130 + 11 * num_lines
\begin{trinket} [190] {PrintArrayValues.java}
public class PrintArrayValues {

    public static void main(String[] args) {
       int[] a = {1, 2, 3, 4}; //array initializer
        for (int i = 0; i < a.length; i++) {
           System.out.println(a[i]);
       }
    }
}
\end{trinket}

%Given the previous array, the output of \java{printArray} is:
%
%\begin{stdout}
%{1, 2, 3, 4}
%\end{stdout}
Run the program and see what you get.

In the example above, we see that \java{a.length} gives the length of the array.  The pattern of the for loop is important to understand.  The \textbf{length} of \java{a} is \textbf{4}, but the \textbf{indexes} are \java{0, 1, 2} and \java{3}.  The for loop starts the loop index \java{i} at \java{0}, and continues as long as \java{i < a.length} giving \java{i} four values, namely \java{0, 1, 2} and \java{3}, precisely what we want.
%slr: end 8-21-19

\index{utility class}
\index{Arrays class}

The Java library provides a utility class \java{java.util.Arrays} that has methods for working with arrays.
One of them, \java{toString}, returns a string representation of an array.
We can invoke it like this:

\begin{code}
System.out.println(Arrays.toString(a));
\end{code}

And the output is:

\begin{stdout}
[1, 2, 3, 4]
\end{stdout}

As usual, we have to \java{import java.util.Arrays} before we can use it.
Notice that the string format is slightly different: it uses square brackets instead of curly braces.
But it beats having to write your own \java{printArray} method.

%slr: 8-21-19
\textbf{Section Exercises}
\begin{enumerate}
\item Modify the \java{PrintArrayValues} program in the trinket above so as to use the \java{Arrays.toSting()} method to print the values of \java{a}.
\item Change the initializer on line 4.  Intialize with more, different values and see what happens.
\item Change the intiializer again, this time adding a \java{double} literal, say 3.5.  What happens?
\end{enumerate}
%slr: end 8-16-19

\section{Copying arrays}

\index{array!copying}

As explained in Section~\ref{elements}, array variables contain {\em references} to arrays.
When you make an assignment to an array variable, it simply copies the reference.
But it doesn't copy the array itself.
For example:

%slr: 8-21-19
%\begin{code}
%double[] a = new double[3];
%double[] b = a;
%\end{code}
% height = 130 + 11 * num_lines
\begin{trinket} [250] {ArrayAliases.java}
import java.util.Arrays;
public class ArrayAliases {

    public static void main(String[] args) {
       double[] a = new double[3];
       double[] b = a;
       System.out.printf("a : %s\n", Arrays.toString(a));
       System.out.printf("b : %s\n", Arrays.toString(b));
       a[0] = 17.0;
       System.out.printf("a : %s\n", Arrays.toString(a));
       System.out.printf("b : %s\n", Arrays.toString(b));
    }
}
\end{trinket}

Run the program to see what happens.  Changing \java{a[0]} also changed \java{b[0]}!
%slr:  end 8-21-19

These statements create an array of three \java{double}s and make two different variables refer to it, as shown in Figure~\ref{fig.array3}.

\index{memory diagram}
\index{diagram!memory}

\begin{figure}[!ht]
\begin{center}
\includegraphics[scale=0.75]{figs/array3.pdf}
\caption{Memory diagram of two variables referring to the same array.}
\label{fig.array3}
\end{center}
\end{figure}

\index{alias}

Any changes made through either variable will be seen by the other.
Above, we set \java{a[0] = 17.0}, and then displayed \java{b[0]}, and got {\tt 17.0}.
Because \java{a} and \java{b} are different names for the same thing, they are sometimes called {\bf aliases}.

If you actually want to copy the array, not just the reference, you have to create a new array and copy the elements from one to the other, like this:

%slr:  8-21-19
%\begin{code}
%double[] b = new double[3];
%for (int i = 0; i < 3; i++) {
%    b[i] = a[i];
%}
%\end{code}
% height = 130 + 11 * num_lines
\begin{trinket} [320] {CopyArray.java}
import java.util.Arrays;
public class CopyArray {

    public static void main(String[] args) {
       double[] a = {17, 0, 0};
       double[] b = new double[a.length];
       for (int i = 0; i < a.length; i++){
          b[i] = a[i];
       }
       System.out.printf("a : %s\n", Arrays.toString(a));
       System.out.printf("b : %s\n", Arrays.toString(b));
       a[1] = Math.PI;
       System.out.printf("a : %s\n", Arrays.toString(a));
       System.out.printf("b : %s\n", Arrays.toString(b));
    }
}
\end{trinket}

Run the program to see what you get.  Now the arrays are not aliases, but are independent arrays, each of length \java{3}.

\index{Arrays class}

\java{java.util.Arrays} provides a method named \java{copyOf} that performs this task for you.
So you can replace the previous code with one line:

\begin{code}
double[] b = Arrays.copyOf(a, a.length);
\end{code}

The second parameter is the number of elements you want to copy, so \java{copyOf} can also be used to copy part of an array.
Figure~\ref{fig.array4} shows the state of the array variables after copying the arrays, either iteratively or by invoking \java{Arrays.copyOf}.

\begin{figure}[!ht]
\begin{center}
\includegraphics[scale=0.75]{figs/array4.pdf}
\caption{Memory diagram of two variables referring to different arrays.}
\label{fig.array4}
\end{center}
\end{figure}

When the program above changed the value of \java{a[1]}, the corresponding value \java{b[1]} was unchanged.
%\section{Array length}

\index{length!array}
\index{array!length}

%slr: 8-21-19
%The examples so far only work if the array has three elements.
%It is better to generalize the code to work with arrays of any size.
%We can do that by replacing the magic number, \java{3}, with \java{a.length}:
%
%\begin{code}
%double[] b = new double[a.length];
%for (int i = 0; i < a.length; i++) {
%    b[i] = a[i];
%}
%\end{code}
%slr:  end 8-21-19

All arrays have a built-in constant, \java{length}, that stores the number of elements.
In contrast to \java{String.length()}, which is a method, \java{a.length} is a constant.
The expression \java{a.length} may look like a method invocation, but there are no parentheses and no arguments.

The last time the loop gets executed, \java{i} is \java{a.length - 1}, which is the index of the last element.
When \java{i} is equal to \java{a.length}, the condition fails and the body is not executed -- which is a good thing, because trying to access \java{a[a.length]} would throw an exception.

Of course we can replace the loop altogether by using \java{Arrays.copyOf} and \java{a.length} for the second argument.
The following line produces the same result shown in Figure~\ref{fig.array4}.

\begin{code}
double[] b = Arrays.copyOf(a, a.length);
\end{code}

%slr: 8-21-19
\textbf{Section Exercises}
\begin{enumerate}
\item Modify the \java{CopyArray} program in the trinket above so as to use the \java{Arrays.copyOf()} method to make the copy of \java{a}.
\item Modify the call to copy only the first two values of a into b, making \java{b.length} equal to 2.
\end{enumerate}
%slr: end 8-21-19

\section{Array traversal}
\label{traversal}

\index{traversal}

Many computations can be implemented by looping through the elements of an array and performing an operation on each element.
Looping through the elements of an array is called a {\bf traversal}.

\begin{code}
int[] a = {1, 2, 3, 4, 5};
for (int i = 0; i < a.length; i++) {
    a[i] *= a[i];
}
\end{code}

This example traverses an array and squares each element.
At the end of the loop, the array has the values \java{{1, 4, 9, 16, 25}}.

\index{search}

Another common pattern is a {\bf search}, which involves traversing an array and ``searching'' for a particular element.
For example, the following program invokes a method that takes an array and a value, and returns the index where the value appears:

%slr:  8-21-19
\begin{code}
public static int search(double[] array, double target) {
    for (int i = 0; i < array.length; i++) {
        if (array[i] == target) {
            return i;
        }
    }
    return -1;  // not found
}
\end{code}
% height = 130 + 11 * num_lines
\begin{trinket} [370] {SearchArray.java}
public class SearchArray {

    public static int search( double[] array, double target) {
       for (int i = 0; i < array.length; i++) {
           if (array[i] == target) {
               return i;
           }
       }
       return -1;  // not found
    }

    public static void main(String[] args) {
       double[] a = {17.5, -3.2, 8, Math.E};
       double x = -3.2;
       int index = search(a, x);
       if (index >= 0) {
          System.out.printf("%f found in array at index %d\n", x, index);
       } else {
          System.out.printf("%f not round in array\n");
       }
    }
}
\end{trinket}
%slr: end 8-21-19

If we find the target value in the array, the \java{search} method returns its index immediately.
If the loop exits without finding the target, it returns \java{-1}, a special value chosen to indicate a failed search.
(This code is essentially what the \java{String.indexOf} method does.)

Run the program with a few different values of \java{x} to see how it behaves when it does and does not find the value in the array.

%slr:  8-21-19
%The following code searches an array for the value \java{1.23}, which is the third element.
%Because array indexes start at zero, the output is \java{2}.
%
%\begin{code}
%double[] array = {3.14, -55.0, 1.23, -0.8};
%int index = search(array, 1.23);
%System.out.println(index);
%\end{code}
%slr: end 8-21-19

\index{reduce}

Another common traversal is a {\bf reduce} operation, which ``reduces'' an array of values down to a single value.
Examples include the sum or product of the elements, the minimum, and the maximum.
The following program uses a method named \java{sum} that takes an array and returns the sum of its elements:

%slr: 8-21-19
%\begin{code}
%public static double sum(double[] array) {
%    double total = 0.0;
%    for (int i = 0; i < array.length; i++) {
%        total += array[i];
%    }
%    return total;
%}
%\end{code}
% height = 130 + 11 * num_lines
\begin{trinket} [320] {SumArray.java}
public class SumArray {

    public static double sum(double[] array) {
        double total = 0.0;  // 'accumulator'
        for (int i = 0; i < array.length; i++) {
            total += array[i];
        }
        return total;
    }

    public static void main(String[] args) {
       double[] a = {17.5, -3.2, 8, Math.E};
       System.out.println("The sum of the array elements is " + sum(a));
    }
}
\end{trinket}
%slr: end 8-21-19

Run the program and see how it works.

\index{accumulator}

In the method \java{sum}, before the loop, we initialize \java{total} to zero.
Each time through the loop, we update \java{total} by adding one element from the array.
At the end of the loop, \java{total} contains the sum of the elements.
A variable used this way is sometimes called an {\bf accumulator}, because it ``accumulates'' the running total.

%slr: 8-21-19
\index{programming idiom}
Note that each of these array traversals employs the same for loop structure:

\begin{code}
for (int i = 0; i < array.length; i++) {
   //...
}
\end{code}

This is a \textbf{very} common Java \href{https://en.wikipedia.org/wiki/Programming_idiom}{programming idiom}.
%slr:  end 8-21-19

\section{Random numbers}
\label{random}

\index{deterministic}

Most computer programs do the same thing every time they run; programs like that are called {\bf deterministic}.
Usually determinism is a good thing, since we expect the same calculation to yield the same result.
But for some applications, we want the computer to be unpredictable.
Games are an obvious example, but there are many others, like scientific simulations.

%Technically speaking, all computer programs are deterministic: they simply execute the source code.

\index{nondeterministic}
\index{pseudorandom}

Making a program {\bf nondeterministic} turns out to be hard, because it's impossible for a computer to generate truly random numbers.
But there are algorithms that generate unpredictable sequences called {\bf pseudorandom} numbers.
For most applications, they are as good as random.

%Nondeterminism is a theoretical concept for analyzing the complexity of algorithms.

\index{Random}
\index{nextInt!Random}

If you did Exercise~\ref{guess}, you have already seen \java{java.util.Random}, which generates pseudorandom numbers.
The method \java{nextInt} takes an integer argument, \java{n}, and returns a random integer between \java{0} and \java{n - 1} (inclusive).

If you generate a long series of random numbers, every value should appear, at least approximately, the same number of times.
One way to test this behavior of \java{nextInt} is to generate a large number of values, store them in an array, and count the number of times each value occurs.

The following method creates an \java{int} array and fills it with random numbers between 0 and 99.
The argument specifies the desired size of the array, and the return value is a reference to the new array.

\begin{code}
public static int[] randomArray(int size) {
    Random random = new Random();
    int[] a = new int[size];
    for (int i = 0; i < a.length; i++) {
        a[i] = random.nextInt(100);
    }
    return a;
}
\end{code}

%slr:  8-21-19

%The following \java{main} method generates an array and displays it using \java{printArray} from Section~\ref{printarray}.
%We could have used \java{Arrays.toString}, but we like seeing curly braces instead of square brackets.
%
%\begin{code}
%public static void main(String[] args) {
%    int[] array = randomArray(8);
%    printArray(array);
%}
%\end{code}
%
%Each time you run the program, you should get different values.
%The output will look something like this:
%
%\begin{stdout}
%{15, 62, 46, 74, 67, 52, 51, 10}
%\end{stdout}
The following program generates an array of pseudo-random numbers using the method \java{randomArray} and displays it:

% height = 130 + 11 * num_lines
\begin{trinket} [320] {RandomArray.java}
import java.util.Arrays;
import java.util.Random;
public class RandomArray {

    public static int[] randomArray(int size) {
        Random random = new Random();
        int[] a = new int[size];
        for (int i = 0; i < a.length; i++) {
            a[i] = random.nextInt(100);
        }
        return a;
    }

    public static void main(String[] args) {
       int[] array = randomArray(8);
       System.out.println(Arrays.toString(array));
    }
}
\end{trinket}
%slr: end 8-21-19

Run it a few times to get a feel for how the values change and what their range is.
%slr:  end 8-21-19

\section{Building a histogram}
\label{singlepass}

\index{histogram}
\index{counter}

If these values were exam scores -- and they would be pretty bad exam scores in that case -- the teacher might present them to the class in the form of a {\bf histogram}.
In statistics, a histogram is a set of counters that keeps track of the number of times each value appears.

For exam scores, we might have ten counters to keep track of how many students scored in the 90s, the 80s, etc.
To do that, we can traverse the array and count the number of elements that fall in a given range.

The following method takes an array and two integers.
It returns the number of elements that fall in the range from \java{low} to \java{high - 1}.

\begin{code}
public static int inRange(int[] a, int low, int high) {
    int count = 0;
    for (int i = 0; i < a.length; i++) {
        if (a[i] >= low && a[i] < high) {
            count++;
        }
    }
    return count;
}
\end{code}

\index{reduce}

This pattern should look familiar: it is another reduce operation.
Notice that \java{low} is included in the range (\java{>=}), but \java{high} is excluded (\java{<}).
This design keeps us from counting any scores twice.

Now we can count the number of scores in each grade range.
We add the following code to our \java{main} method:

\begin{code}
int[] scores = randomArray(30);
int a = inRange(scores, 90, 100);
int b = inRange(scores, 80, 90);
int c = inRange(scores, 70, 80);
int d = inRange(scores, 60, 70);
int f = inRange(scores, 0, 60);
\end{code}

This code is repetitive, but it is acceptable as long as the number of ranges is small.
Suppose we wanted to keep track of the number of times each individual score appears.
Then we would have to write 100 lines of code:

\begin{code}
int count0 = inRange(scores, 0, 1);
int count1 = inRange(scores, 1, 2);
int count2 = inRange(scores, 2, 3);
...
int count99 = inRange(scores, 99, 100);
\end{code}

What we need is a way to store 100 counters, preferably so we can use an index to access them.
Wait a minute, that's exactly what an array does.

The following fragment creates an array of 100 counters, one for each possible score.
It loops through the scores and uses \java{inRange} to count how many times each score appears.
Then it stores the results in the \java{counts} array:

\begin{code}
int[] counts = new int[100];
for (int i = 0; i < counts.length; i++) {
    counts[i] = inRange(scores, i, i + 1);
}
\end{code}

Notice that we are using the loop variable \java{i} three times: as an index into the \java{counts} array, and in the last two arguments of \java{inRange}.

%slr:  8-21-19
Lets put this together and see how it works.  Remember we are trying to see how well our pseudo-random number generator performs - each value should appear around once - maybe 0 or two times here and there.  But with 100 values generated in the interval [0, 99] we would not expect to see, say, the value 7 repeated 25 times.

% height = 130 + 11 * num_lines
\begin{trinket} [500] {Histogram1.java}
import java.util.Random;
import java.util.Arrays;
public class Histogram1 {

    public static int[] randomArray(int size) {
        Random random = new Random();
        int[] a = new int[size];
        for (int i = 0; i < a.length; i++) {
            a[i] = random.nextInt(100);
        }
        return a;
    }

    public static int inRange(int[] a, int low, int high) {
        int count = 0;
        for (int i = 0; i < a.length; i++) {
           if (a[i] >= low && a[i] < high) {
               count++;
           }
        }
        return count;
    }

    public static void main(String[] args) {
       int[] scores = randomArray(100); //100 values in range [0,99]
       int[] counts = new int[100];
       for (int i = 0; i < counts.length; i++) {
          counts[i] = inRange(scores, i, i+1);
       }
       System.out.println(Arrays.toString(counts));
    }
}
\end{trinket}

Run it and see how well the pseudo-random number generator seems to be working.
%slr: end 8-21-19

\index{efficiency}

The code works, but it is not as efficient as it could be.
Every time the loop invokes \java{inRange}, it traverses the entire array.
It would be better to make a single pass through the \java{scores} array.

For each score, we already know which range it falls in -- the score itself.
We can use that value to increment the corresponding counter, and discard the method \java{inRange}.
This code traverses the array of scores {\em only once} to generate the histogram:

%slr:  8-21-19

%\begin{code}
%int[] counts = new int[100];
%for (int i = 0; i < scores.length; i++) {
%    int index = scores[i];
%    counts[index]++;
%}
%\end{code}
% height = 130 + 11 * num_lines
\begin{trinket} [400] {Histogram2.java}
import java.util.Random;
import java.util.Arrays;

public class Histogram2 {

    public static int[] randomArray(int size) {
        Random random = new Random();
        int[] a = new int[size];
        for (int i = 0; i < a.length; i++) {
            a[i] = random.nextInt(100);
        }
        return a;
    }

    public static void main(String[] args) {
       int[] scores = randomArray(100); //100 values in range [0,99]
       int[] counts = new int[100];
       for (int i = 0; i < counts.length; i++) {
          int index = scores[i];
          counts[index]++;
       }
       System.out.println(Arrays.toString(counts));
    }
}
\end{trinket}

Each time through the loop, it selects one element from \java{scores} and uses it as an index to increment the corresponding element of \java{counts}.
Because this code traverses the array of scores only once, it is much more efficient.

%slr: 8-21-19
\textbf{Section Exercises}
\begin{enumerate}
\item Modify the \java{Histogram2} program in the trinket above so as to generate 1000 values in the range [0, 99] and build a histogram.
\item Modify the \java{Histogram1} program (the one further up that uses the \java{inRange} method) to likewise generate 1000 values in the range [0, 99] and count how many values ended up in the 10 intervals [0, 10), [10, 20), ..., [90, 100).  Does the pseudo-random number generator seem to be working well?
\end{enumerate}
%slr: end 8-21-19

\section{The enhanced for loop}
\label{enhanced}

Since traversing arrays is so common, Java provides an alternative syntax that makes the code more compact.
Consider a \java{for} loop that displays the elements of an array on separate lines:

\begin{code}
for (int i = 0; i < values.length; i++) {
    int value = values[i];
    System.out.println(value);
}
\end{code}

We could rewrite the loop like this:

\begin{code}
for (int value : values) {
    System.out.println(value);
}
\end{code}

\index{enhanced for loop}
\index{for!enhanced}

This statement is called an {\bf enhanced for loop}, also known as the ``for each'' loop.
You can read the code as, ``for each \java{value} in \java{values}''.
It's conventional to use plural nouns for array variables and singular nouns for element variables.

Notice how the single line \java{for (int value : values)} replaces the first two lines of the standard \java{for} loop.
It hides the details of iterating each index of the array, and instead, focuses on the values themselves.

Using the enhanced \java{for} loop, and removing the temporary variable, we can write the histogram code from the previous section more concisely:

\begin{code}
int[] counts = new int[100];
for (int score : scores) {
    counts[score]++;
}
\end{code}

Enhanced \java{for} loops often make the code more readable, especially for accumulating values.
But they are not helpful when you need to refer to the index, as in search operations.

\begin{code}
for (double d : array) {
    if (d == target) {
        // array contains d, but we don't know where
    }
}
\end{code}

%slr: 8-21-19
\textbf{Section Exercises}
\begin{enumerate}
\item Modify the \java{Histogram2} program in the trinket above so as to use the for each loop instead of a for loop.  It should work the same.  Does it?
\end{enumerate}
%slr: end 8-21-19

\section{Counting characters}

We now return to the example from the beginning of the chapter and present a solution to Exercise~\ref{doubloon} using arrays.
Here is the problem again:

\begin{quote}
A word is said to be a ``doubloon'' if every letter that appears in the word appears exactly twice.
Write a method called \java{isDoubloon} that takes a string and checks whether it is a doubloon.
To ignore case, invoke the \java{toLowerCase} method before checking.
\end{quote}

Based on the approach from Section~\ref{singlepass}, we will create an array of 26 integers to count how many times each letter appears.
We convert the string to lowercase, so that we can treat \java{'A'} and \java{'a'} (for example) as the same latter.

\begin{code}
int[] counts = new int[26];
String lower = s.toLowerCase();
\end{code}

We can use a \java{for} loop to iterate each character in the string.
To update the \java{counts} array, we need to compute the index that corresponds to each character.
Fortunately, Java allows you to perform arithmetic on characters.

\begin{code}
for (int i = 0; i < lower.length(); i++) {
    char letter = lower.charAt(i);
    int index = letter - 'a';
    counts[index]++;
}
\end{code}

\index{toCharArray}

To simplify the code, it would be nice to use an enhanced \java{for} loop.
The enhanced \java{for} loop does not work with strings directly, but you can convert any string to a character array and iterate that instead:

\begin{code}
for (char letter : lower.toCharArray()) {
    int index = letter - 'a';
    counts[index]++;
}
\end{code}

After counting all the characters in the \java{lower} string, we need one last \java{for} loop to determine whether each letter appears 0 or 2 times.

\begin{code}
for (int count : counts) {
    if (count != 0 && count != 2) {
        return false;  // not a doubloon
    }
}
return true;  // is a doubloon
\end{code}

Like in Section~\ref{traversal}, we can return immediately if the inner condition is true (which, in this example, means that the word is not a doubloon).
If we make it all the way through the \java{for} loop, we know that all counts are 0 or 2.

Pulling together the code fragments, and adding some comments and test cases, here is an entire program.
%This example uses \java{if} statements, \java{for} loops, methods, strings, and arrays.
It's amazing to think about how much you've learned in just seven chapters!

\index{Doubloon.java}

\begin{trinket}{Doubloon.java}
public class Doubloon {

    public static boolean isDoubloon(String s) {
        // count the number of times each letter appears
        int[] counts = new int[26];
        String lower = s.toLowerCase();
        for (char letter : lower.toCharArray()) {
            int index = letter - 'a';
            counts[index]++;
        }
        // determine whether the given word is a doubloon
        for (int count : counts) {
            if (count != 0 && count != 2) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        System.out.println(isDoubloon("Mama"));  // true
        System.out.println(isDoubloon("Lama"));  // false
    }
}
\end{trinket}

\section{Vocabulary}

\begin{description}

\term{array}
A collection of values, where all the values have the same type, and each value is identified by an index.

\term{element}
One of the values in an array.
The \java{[]} operator selects elements.

\term{index}
An integer variable or value used to indicate an element of an array.

\term{allocate}
To reserve memory for an array or other object.
In Java, the \java{new} operator allocates memory.

\term{reference}
A value that indicates a storage location.
In a memory diagram, a reference appears as an arrow.

\term{alias}
A variable that refers to the same object as another variable.

\term{traversal}
Looping through the elements of an array (or other collection).

\term{search}
A traversal pattern used to find a particular element of an array.

\term{reduce}
A traversal pattern that combines the elements of an array into a single value.

\term{accumulator}
A variable used to accumulate results during a traversal.

\term{deterministic}
A program that does the same thing every time it is run.

\term{nondeterministic}
A program that always behaves differently, even when run multiple times with the same input.

\term{pseudorandom}
A sequence of numbers that appear to be random, but which are actually the product of a deterministic computation.

\term{histogram}
An array of integers where each integer counts the number of values that fall into a certain range.

\term{enhanced for loop}
An alternative syntax for traversing the elements of an array (or other collection).

\end{description}

\section{Exercises}

The code for this chapter is in the {\tt ch07} directory of {\tt ThinkJavaCode2}.
See page~\pageref{code} for instructions on how to download the repository.
Before you start the exercises, we recommend that you compile and run the examples.

If you haven't already, take a look at Appendix~\ref{debugging} where we've collected some of our favorite debugging advice.
It refers to language features we haven't yet covered, but it's good for you to know what's available when you need it.

\begin{exercise}  %%V6 Ex8.2

The purpose of this exercise is to practice reading code and recognizing the traversal patterns in this chapter.
The following methods are hard to read, because instead of using meaningful names for the variables and methods, they use names of fruit.

For each method, write one sentence that describes what the method does, without getting into the details of how it works.
And for each variable, identify the role it plays.

\begin{code}
public static int banana(int[] a) {
    int kiwi = 1;
    int i = 0;
    while (i < a.length) {
        kiwi = kiwi * a[i];
        i++;
    }
    return kiwi;
}
\end{code}

\begin{code}
public static int grapefruit(int[] a, int grape) {
    for (int i = 0; i < a.length; i++) {
        if (a[i] == grape) {
            return i;
        }
    }
    return -1;
}
\end{code}

\begin{code}
public static int pineapple(int[] a, int apple) {
    int pear = 0;
    for (int pine: a) {
        if (pine == apple) {
            pear++;
        }
    }
    return pear;
}
\end{code}

\end{exercise}

\begin{exercise}  %%V6 Ex8.3

What is the output of the following program?
Describe in a few words what \java{mus} does.
Draw a stack diagram just before \java{mus} returns.
%that shows the state of the program

\begin{code}
public static int[] make(int n) {
    int[] a = new int[n];
    for (int i = 0; i < n; i++) {
        a[i] = i + 1;
    }
    return a;
}
\end{code}

\begin{code}
public static void dub(int[] jub) {
    for (int i = 0; i < jub.length; i++) {
        jub[i] *= 2;
    }
}
\end{code}

\begin{code}
public static int mus(int[] zoo) {
    int fus = 0;
    for (int i = 0; i < zoo.length; i++) {
        fus += zoo[i];
    }
    return fus;
}
\end{code}

\begin{code}
public static void main(String[] args) {
    int[] bob = make(5);
    dub(bob);
    System.out.println(mus(bob));
}
\end{code}

\end{exercise}

\begin{exercise}  %%V6 Ex8.4

Write a method called \java{indexOfMax} that takes an array of integers and returns the index of the largest element.
Can you write this method using an enhanced \java{for} loop?
Why or why not?

\end{exercise}

\begin{exercise}  %%V6 Ex8.5

The Sieve of Eratosthenes is ``a simple, ancient algorithm for finding all prime numbers up to any given limit,'' which you can read about at \url{https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes}.

Write a method called \java{sieve} that takes an integer parameter, \java{n}, and returns a \java{boolean} array that indicates, for each number from \java{0} to \java{n - 1}, whether the number is prime.

\end{exercise}

\begin{exercise}  %%V6 Ex8.6

Write a method named \java{areFactors} that takes an integer \java{n} and an array of integers, and that returns \java{true} if the numbers in the array are all factors of \java{n} (which is to say that \java{n} is divisible by all of them).

\end{exercise}

\begin{exercise}  %%V6 Ex8.7

Write a method named \java{arePrimeFactors} that takes an integer \java{n} and an array of integers, and that returns \java{true} if the numbers in the array are all prime {\it and} their product is \java{n}.

\end{exercise}

\begin{exercise}  %%V6 Ex9.2

Write a method called \java{letterHist} that takes a string as a parameter and returns a histogram of the letters in the string.
The zeroth element of the histogram should contain the number of a's in the string (upper- and lowercase); the 25th element should contain the number of z's.
Your solution should only traverse the string once.

\end{exercise}

\begin{exercise}  %%V6 Ex9.7

\index{anagram}

Two words are anagrams if they contain the same letters and the same number of each letter.
For example, ``stop'' is an anagram of ``pots'' and ``allen downey'' is an anagram of ``well annoyed''.
Write a method that takes two strings and checks whether they are anagrams of each other.

\end{exercise}