\lab{Lorenz Equations}{Lorenz Equations}
% \epigraph{\textit{Chaos: When the present determines the future, but the approximate present does not approximately determine the future.}}{Edward Lorenz}
\objective{Investigate the behavior of a system that exhibits chaotic behavior.
Demonstrate methods for visualizing the evolution of a system.}

Chaos is everywhere.
It can crop up in unexpected places and in remarkably simple systems, and a great deal of work has been done to describe the behavior of chaotic systems.
One primary characteristic of chaos is that small changes in initial conditions result in large changes over time in the solution curves.

\section*{The Lorenz System}
One of the earlier examples of chaotic behavior was discovered by Edward Lorenz.
In 1963, while working to study atmospheric dynamics, he derived the simple system of equations
\begin{align*}
\frac{\partial x}{\partial t} &= \sigma \left(y - x\right) \\
\frac{\partial y}{\partial t} &= \rho x - y - x z \\
\frac{\partial z}{\partial t} &= x y - \beta z
\end{align*}
where $\sigma$, $\rho$, and $\beta$ are all constants.
After deriving these equations, he plotted the solutions and observed some unexpected behavior.
For appropriately chosen values of $\sigma$, $\rho$, and $\beta$, the solutions did not tend toward any steady fixed points, nor did the system permit any stable cycles.
The solutions did not tend off toward infinity either.
With further work, he began the study of what was called a \textit{strange attractor}.
This system, though relatively simple, exhibits chaotic behavior.

\begin{comment}
\subsection*{Plotting and Interacting}
We will be making interactive 3D plots using \li{matplotlib}.
Please refer back to the \textit{Introduction to Matplotlib} lab for tips and tricks to graphing 3D.
Below is some sample code to help you out.

\begin{lstlisting}
from matplotlib import rcParams, pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
rcParams['figure.figsize'] = (16,10)     #Affects output size of graphs.
'''
Code up your X, Y, Z values
'''
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot( X, Y, Z )    #Make sure X, Y, Z are same length.
                      #Connect points (X[i], Y[i], Z[i]) for i in len(X)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

ax.set_xlim3d([min(X), max(X)])    #Bounds the axes nicely
ax.set_ylim3d([min(Y), max(Y)])
ax.set_zlim3d([min(Z), max(Z)])

plt.show()
\end{lstlisting}

Because we are plotting in 3D, we are also interested in being able to interact with the plot and rotate it to analyze what our solutions are actually doing.
To facilitate this, \li{matplotlib} has different backends which allow for various levels of control over what our graphs do.
These backends are accessed using \li{matplotlib}'s \li{switch_backend()} function with a call to whichever backend we want.
Your computer may default to a backend which you can already interact with (try clicking your plot and rotating it to see).
If not, consider using 'qt5agg', 'qt4agg', nbagg', or 'tkagg' in the function call.

\begin{lstlisting}
plt.switch_backend('qt5agg') # This backend opens the graph in a new window
\end{lstlisting}

\begin{warn}
Do not run \li{\%matplotlib inline} with this code. You will crash your kernel.
\end{warn}
\end{comment}

\begin{problem}
Write a function that implements the Lorenz equations. Let $\sigma=10$, $\rho=28$, $\beta=\frac{8}{3}$. Make a 3D plot of a solution to the Lorenz equations, where the initial conditions $x_0,y_0,z_0$ are each drawn randomly from a uniform distribution on $[-15,15]$. As usual, use \li{scipy.integrate.odeint} to compute an approximate solution. Compare your results with Figure \ref{fig:Single_Lorenz}.
\end{problem}

\begin{figure}
\includegraphics[width=\textwidth]{figures/Single_Lorenz.pdf}
\caption{Approximate solution to the Lorenz equation with random initial conditions}
\label{fig:Single_Lorenz}
\end{figure}

\section*{Basin of Attraction}
Notice in the first problem that the solution tended to a 'nice' region. This region is a basin of attraction, and the set of numerical values towards which a system will converge to is an \textbf{attractor}. Consider what happens when we change up the initial conditions.
\begin{problem}
To better visualize the Lorenz attractor, produce a single 3D plot displaying three solutions to the Lorenz equations, each with random initial conditions (as before). Compare your results with Figure \ref{fig:Multiple_Lorenz}.
\end{problem}

\begin{figure}
\includegraphics[width=\textwidth]{figures/Multiple_Lorenz.pdf}
\caption{Multiple solutions to the Lorenz equation with random initial conditions}
\label{fig:Multiple_Lorenz}
\end{figure}

\section*{Chaos}
Chaotic systems exhibit high sensitivity to initial conditions. This means that a small difference in initial conditions may result in solutions that diverge significantly from each other. However, chaotic systems are not \textit{random}. According to Lorenz, chaos is "when the present determines the future, but the approximate present does not approximately determine the future."

\begin{problem}
Use \li{matplotlib.animation.FuncAnimation} to produce a 3D animation of two solutions to the Lorenz equations with nearly identical initial conditions. To make the initial conditions, draw $x_0,y_0,z_0$ as before, and then make a second initial condition by adding a small perturbation to the first (Hint: try using \li{np.random.randn(3)*(1e-10)} for the perturbation). Note that it may take several seconds before the separation between the two solutions will be noticeable.

The animation should display a point marker as well as the past trajectory curve for each solution. Save your animation as \li{lorenz_animation.mp4}.
\end{problem}

In a chaotic system, round-off error implicit in a numerical method can also cause divergent solutions. For example, using a Runge-Kutta method with two different values for the stepsize $h$ on identical initial conditions will still result in approximations that differ in a chaotic fashion.

\begin{comment}
\begin{figure}
\includegraphics[width=\textwidth]{figures/perturbed_lorenz.pdf}
\caption{Two solutions to the Lorenz equation with perturbed initial conditions}
\label{fig:perturbed_lorenz}
\end{figure}

You may be wondering if your code was even correct.
You could distinctly see the two curves because there was some separation of color, but who is to say that the two solutions moved away from each other only slightly as time went on.
Our next task will be to \textit{animate} this beast.

\section*{Intro to Animation}
Let's get you a degree in Computer Animation.
You will be provided with some code that helps with animation.
Please feel free to play around and try to animate different things.
Again, refer to the \textit{Introduction to Matplotlib} lab for additional help.

We will call on \li{matplotlib.animate} to assist in animation. Import the following:
\begin{lstlisting}
from matplotlib.animation import FuncAnimation
\end{lstlisting}
Follow these steps for setting up an animation.
\begin{itemize}
\item Calculate all data needed for the animation (not necessary in some cases, but it simplifies things).
\item Define a figure explicitly with \li{plt.figure()} and set its window boundaries.
\item Draw empty objects that can be altered dynamically.
\item Define a function to update the drawing objects.
\item make a call to \li{FuncAnimation()} which will start the animation.
\end{itemize}
\li{FuncAnimation()} accepts the figure to be animated, the function that updates the figure, the number of frames to show before repeating, and how fast to run the animation (lower number $=$ faster).

Check out the sample code for animating two 2D objects simultaneously.
\begin{lstlisting}
from matplotlib.animation import FuncAnimation

def sine_cos_animation():
	#Calculate the data to be animated
	x = np.linspace(0, 2*np.pi, 200)[:-1]
	y1, y2 = np.sin(x), np.cos(x)

	#Create a figure and set the window boundaries
	fig = plt.figure()
	plt.xlim(0, 2*np.pi)
	plt.ylim(-1.2, 1.2)

	#Initiate empty lines of the correct dimension
	sin_drawing, = plt.plot([], [])
	cos_drawing, = plt.plot([], [])	#note the comma after the variable name

	#Define a function that updates each line
	def update(index):
		sin_drawing.set_data(x[:index], y1[:index])
		cos_drawing.set_data(x[:index], y2[:index])
		return sin_drawing, cos_drawing,

	a = FuncAnimation(fig, update, frames=len(x), interval=10)
	plt.show()
\end{lstlisting}

\begin{warn}
The above animation code works great for 2D animations, but \li{set_data()} is only good for 2D. When you want to animate in 3D, you will need an extra empty list in \li{plt.plot()} and a call to \li{set_3d_properties()} in the \li{update(index)} function.
\end{warn}

\section*{Animate}
You now know how to plot the Lorenz equation, how to plot multiple equations, and even how to animate (yay you, you're a star) simple plots.
It's time to put all your know-how to good use.
\end{comment}

\begin{problem}
The \li{odeint} function allows user to specify error tolerances (similar to setting a value of $h$ in a Runge-Kutta method). Using a single initial condition, produce two approximations by using the \li{odeint} arguments (\li{atol=1e-15, rtol=1e-13}) for the first approximation and (\li{atol=1e-12, rtol=1e-10}) for the second.

As in the previous problem, use \li{FuncAnimation} to animation both solutions. Save the animation as \li{lorenz_animation2.mp4}.
\end{problem}

\begin{comment}
If our system is very chaotic, then even small round off errors in your computer can lead to drastically different solutions.

\begin{problem}
Now set one initial condition.
Use \li{odeint} to solve the system, but use the arguments \li{atol=1E-14}, \li{rtol=1E-12}, and then again with \li{atol=1E-15}, \li{rtol=1E-13}.
Animate both solutions on the same plot.
\end{problem}
\end{comment}

\section*{Lyapunov Exponents}
The \textit{Lyapunov exponent} of a dynamical system is one measure of how chaotic a system is.
While there are more conditions for a system to be considered chaotic, one of the primary indicators of a chaotic system is \textit{extreme sensitivity to initial conditions}.
Strictly speaking, this is saying that a chaotic system is poorly conditioned.
In a chaotic system, the sensitivity to changes in initial conditions depends expoentially on the time the system is allowed to evolve.
If $\delta(t)$ represents the difference between two solution curves, when $\delta(t)$ is small, the following approximation holds.
\[\|\delta(t)\| \sim \|\delta(0)\| e^{\lambda t}\]
where $\lambda$ is a constant called the Lyapunov exponent. In other words, $\log(\|\delta(t)\|)$ is approximately linear as a function of time, with slope $\lambda$.
For the Lorenz system (and for the parameter values specified in this lab), experimentally it can be verified that $\lambda \approx .9$.

\begin{figure}
\includegraphics[width=\textwidth]{figures/semilog.pdf}
\caption{A semilog plot of the separation between two solutions to the Lorenz equations together with a fitted line that gives a rough estimate of the Lyapunov exponent of the system.}
\label{fig:lyapunov_exponent}
\end{figure}

\begin{comment}
Suppose we want tol find the Lyapunov exponent for the Lorenz System.
Start by importing \li{linalg} and \li{linregress} modules from \li{scipy}.
\begin{lstlisting}
from scipy import linalg as la
from scipy.stats import linregress
\end{lstlisting}
To get a good estimate of the Lyapunov exponent, we want to make sure our initial solution already lies on the attractor.
To do this, choose some random initial conditions, run your \li{solve_lorenz} function, then pick out the final coordinates.
Let these coordinates be the starting point for our next system.

Next perturb the conditions slightly.
Find the solution curve using these two sets of initial coordinates and then calculate the \li{norm} between the solution at each point.
Plot the norm using \li{plt.semilogy(time, norm)}.

Finally we want to calculate the exponential line fitted to the data.
Because we are using the semilog plot, we can find this line as follows:
\begin{itemize}
\item take the \li{log} of the norms.
\item use \li{linregress()} to compute the linear regression of the log of the norms.
\item take the \li{exp} of the linear regression to turn back into an exponential regression.
\item plot the exponential regression using \li{plt.semilogy()} (your solution will be a line)
\end{itemize}
\end{comment}

\begin{problem}
Estimate the Lyapunov exponent of the Lorenz equations by doing the following:
\begin{itemize}
	\item Produce an initial condition that already lies in the attractor. This can be done by using a random "dummy" initial condition, approximating the resulting solution to the Lorenz system for a short time, and then using the endpoint of that solution (which is now in the attractor) as the desired initial condition.
	\item Produce a second initial condition by adding a small perturbation to the first (as before).
	\item For both initial conditions, use \li{odeint} to produce approximate solutions for $0 \leq t \leq 10$
	\item Compute $\|\delta(t)\|$ by taking the norm of the vector difference between the two solutions for each value of $t$.
	\item Use \li{scipy.stats.linregress} to calculate a best-fit line for $\log(\|\delta(t)\|)$ against $t$.
	\item The slope of the best-fit line is an approximation for the Lyapunov exponent $\lambda$

\end{itemize}
Produce a plot similar to Figure \ref{fig:lyapunov_exponent} by using \li{plt.semilogy}.

Hint: Remember that the best-fit line you calculated corresponds to a best-fit exponential for $\|\delta(t)\|$. If \li{a} and \li{b} are the slope and intercept of the best-fit line, the best-fit exponential can be plotted using \li{plt.semilogy(t,np.exp(a*t+b)}.

\end{problem}


\documentclass[12pt]{article}
\usepackage{tensor}
\usepackage{amsmath}
\usepackage{amsfonts}
\begin{document}

\section{Newtonian Curvature}
The equations that describe Newtonian Curvature can be derived by imagining the difference in acceleration between different particles
on a sphere. In geometric units, the acceleration due to gravity is

\[
    a = \frac{Gm_c}{c^2 r^2}
\]

For two particles separated by $\xi$ perpendicular to the radial direction, their acceleration lines converge with an angle $\xi / r$.
For a separation parallel to the radial direction, consider the difference in acceleration for a distance $r$ and $r + \xi$.

Space components of geodesic deviation:

\begin{equation}
    \frac{d^2\xi}{d\tau^2} + \tensor{R}{^k_0_j_0}\xi^j = 0
\end{equation}

where for Newtonian Curvature,

\begin{equation}
    \begin{Vmatrix}
        \tensor{R}{^{\hat{x}}_{\hat{0}}_{\hat{x}}_{\hat{0}}} &
        \tensor{R}{^{\hat{y}}_{\hat{0}}_{\hat{x}}_{\hat{0}}} &
        \tensor{R}{^{\hat{z}}_{\hat{0}}_{\hat{x}}_{\hat{0}}} \\
        \tensor{R}{^{\hat{x}}_{\hat{0}}_{\hat{y}}_{\hat{0}}} &
        \tensor{R}{^{\hat{y}}_{\hat{0}}_{\hat{y}}_{\hat{0}}} &
        \tensor{R}{^{\hat{z}}_{\hat{0}}_{\hat{y}}_{\hat{0}}} \\
        \tensor{R}{^{\hat{x}}_{\hat{0}}_{\hat{z}}_{\hat{0}}} &
        \tensor{R}{^{\hat{y}}_{\hat{0}}_{\hat{z}}_{\hat{0}}} &
        \tensor{R}{^{\hat{z}}_{\hat{0}}_{\hat{z}}_{\hat{0}}} \\
    \end{Vmatrix}
    =
    \begin{Vmatrix}
        m/r^3 & 0 & 0 \\
        0 & m/r^3 & 0 \\
        0 & 0 & -2m/r^3 \\
    \end{Vmatrix}
\end{equation}

\end{document}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Copyright (c) 2003-2018 by The University of Queensland
% http://www.uq.edu.au
%
% Primary Business: Queensland, Australia
% Licensed under the Apache License, version 2.0
% http://www.apache.org/licenses/LICENSE-2.0
%
% Development until 2012 by Earth Systems Science Computational Center (ESSCC)
% Development 2012-2013 by School of Earth Sciences
% Development from 2014 by Centre for Geoscience Computing (GeoComp)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\chapter{The Einstein Summation Convention}

The Einstein Summation Convention (ESC) is a notational convention that is preferred by the \esc developers. It is a condensed and practical way to deal with multi-dimensional and convoluted PDEs. By suppressing the need to write out the many terms of each problem it is possible to increase efficiency and reduce the number of errors created through poor working. According to the convention, when an index variable appears twice in a single term, it implies that we are summing over all of its possible values.
So we have;
\begin{equation}
a_{1}\frac{\partial^2 f}{\partial x_{1}^2} + a_{2}\frac{\partial^2 f}{\partial x_{2}^2} = a_{i}\frac{\partial^2 f}{\partial x_{i}^2}
\end{equation}

For a scalar function $f(x_{1},x_{2},..x_{i})$ and a vector $\mathbf{u}(u_{1},u_{2},..u_{i})$ with $u_{i}(x_{1},x_{2},..x_{i})$, we have the following notation:
\begin{equation}
\mathbf{u}=\sum_{i}u_{i}e^i = u_{i}e^i
\end{equation}
\begin{equation}
\mathbf{grad}(f) = \mathbf{\nabla}(f) = \sum_{i}\frac{\partial f}{\partial x_{i}}e^i = (\partial_{i} f)e^i = f_{,i}e^i
\end{equation}
\begin{equation}
div(\mathbf{u}) = \mathbf{\nabla}.\mathbf{u} = \sum_{i}\frac{\partial u_{i}}{\partial x_{i}} = \partial_{i} u_{i} = u_{i,i}
\end{equation}
\begin{equation}
div(\mathbf{grad}(f)) = \nabla^2 f = \Delta f = \sum_{i}\frac{\partial^2 f}{\partial x_{i}^2} = f_{,ii}
\end{equation}


\section{Range Maximum Query} \label{sec:parallelRMQ}

In this section, we will describe our approach to address the
challenges in the second stage of Algorithm~\ref{alg:parallel-VGLCS},
i.e., an efficient {\em range maximum query}.  The range maximum query
problem is more complicated than the previous incremental suffix
maximum query problem because the suffix maximum query is a special
case of the range maximum query.

The operations to support range maximum query is similar to those for
suffix maximum query.  A {\sc Make} operation creates an empty array
$A$, an {\sc Append}$(V)$ operation appends a value $V$ to the end of an
array $A$.  Finally, a {\sc Query}$(L, R)$ operation finds the {\em
maximum} value among the $L$-th value to the $R$-th value of an array
$A$.

%% We can use a parallel sparse table implementation to answer range
%% maximum queries with $p$ processors, so that it requires $O(n \log n /
%% p + \log n)$ in preprocessing, and takes only $O(1)$ time to answer a
%% query.  On the other hand, it is difficult to efficiently parallelize
%% the querying on tree-like data structures, e.g., disjoint sets.

\subsection{Blocked Sparse Table} \label{sec:blocked-sparse-table}

\begin{figure}[!thb]
  \centering \subfigure[Array] {
    \includegraphics[width=0.65\linewidth]{\GraphicPath/fig-interval-decomposition.pdf}
  } \subfigure[Blocked sparse table] {
    \includegraphics[width=0.25\linewidth]{\GraphicPath/fig-sparse-table.pdf}
  }
  \caption{A Blocked Sparse Table}
  \label{fig:block-interval-decomposition}
\end{figure}

We improve the efficiency of our parallel VGLCS algorithm with a {\em
  blocked} sparse table proposed by
Fischer~\cite{Fischer2006TheoreticalAP}.  The blocked approach first
partitions the data into blocks of size $s$, then it computes the
maximum of each block, and compute a sparse table $T_s$ for these
maximums.  Recall that the {\em unblocked} sparse table in
Section~\ref{sec:sparse-table} does {\em not} partition data into
blocks, but builds a table of $\log n$ rows of maximum for different
lengths of intervals.

The blocked approach answers a range maximum query as follows.  We
consider two types of queries -- {\em super block} query and {\em
  in-block} query.  A super block query queries the answer for {\em
  consecutive} blocks, and an in-block query queries a segment {\em
  within} a block.  It is easy to see that we can answer a super block
query by querying $T_s$ at most {\em twice}, as described in
Section~\ref{sec:sparse-table}.  We can also answer an in-block
queries answered by a {\em single} lookup into an {\em least common
  ancestor table}.  We will provide more details on this table later.
Since we can split {\em any} range query into at most {\em two}
queries into $T_s$ and {\em two} in-block queries, we need at most
{\em four} memory access to answer any range maximum query.  Also
since the super block query is easy to answer with $T_s$, we will now
focus on in-block query.

Fischer's algorithm~\cite{Fischer2006TheoreticalAP} scans through the
data within a block and places them into a {\em Cartesian tree}.  Each
node of this Cartesian tree stores a data and the index of this data
within the block.  One can think of this Cartesian tree as a {\em heap}
where the data are in heap order, and the indexes of the data are in
{\em sorted binary search tree order}.  Please refer to
Figure~\ref{fig:ancesstor-cartesian} for an illustration.  Note that in
Figure~\ref{fig:ancesstor-cartesian} the horizontal position of tree
nodes reflects their position in the data block.  As a result, the
answer to an in-block range maximum query from the $i$-th to the $j$-th
element of a block is located at their {\em least common ancestor} in
the Cartesian tree.  For example, the maximum from the fourth (with data
3) to the six elements (with data 2) is located at the fifth element
(with data 7).

\begin{figure}[!thb]
  \centering
  \includegraphics[width=0.9\linewidth]{\GraphicPath/fig-interval-cartesian.pdf}
  \caption{Least common ancestor tables}
  \label{fig:ancesstor-cartesian}
\end{figure}

To answer in-block queries, Fischer's algorithm computes a {\em least
common ancestor table} for every block.  After scanning the data in a
block, Fischer's algorithm builds a Cartesian tree and its least common
ancestor (LCA) table.  An LCA table stores all $((i, j), k)$'s where the
$k$-th data is the common ancestor for the $i$-th and $j$-th data in a
block.  Please refer to Figure~\ref{fig:ancesstor-cartesian} for an
illustration.  Now we can answer an in-block range maximum query from
the $i$-th to the $j$-th element simply by look into the LCA table and
return $k$,  least common ancestor table of this block.  One can think
of the LCA table as a mapping table from an in-block query $(i, j)$ to
its answer $k$.

Note that the algorithm does {\em not} maintain the value of the $k$-th
element.  Instead, it stores the {\em index}, i.e., $k$, of the least
common ancestor so that two blocks with the {\em same relative key
order} can {\em share} a least common ancestor table.  For example, the
first three blocks in Figure~\ref{fig:ancesstor-cartesian} can share the
same LCA table because they have the same Cartesian tree.  Consequently,
an in-block range maximum query $(1, 3)$ to {\em any} of these three
blocks will return the {\em same} answer $2$.

The main idea of Fischer's algorithm is to compute an LCA table for
every block, and answer an in-block query directly by looking into its
LCA table.  It is easy to see that there are ${\cal C}_s$ different Cartesian
trees, where ${\cal C}_s$ is number of different binary trees of $s$ nodes. It
is also easy to see that each block can be identified by the shape of
its Cartesian tree, so it can be represented by an index.  For ease of
notation we will refer to this index as its {\em Catalan index}. By
knowing the Catalan index of a block, we can answer an in-block range
maximum query by looking into its corresponding LCA table. Please refer
to Figure~\ref{fig:ancesstor-cartesian} for an illustration.

Fischer's algorithm~\cite{Fischer2006TheoreticalAP} builds least
ancestor tables by choosing $s = \frac{\log n}{4}$ as the block size for
performance reason.  Recall that ${\cal C}_s =
\frac{1}{s+1}\binom{2s}{s} = O(\frac{4^s}{s^{\nicefrac{3}{2}}})$.  As a
result the time to scan data and to build Cartesian tree and LCA tables
is $O(n)$, and the space requirement is $O(s^2
\frac{4^s}{s^{\nicefrac{3}{2}}}) = O(n)$.  That is, a sequential
Fischer's algorithm requires $O(n)$ time in preprocessing, and $O(1)$
time to answer a query.  It is easy to see that both preprocessing time
and query answering are optimal.

One drawback of Fischer's algorithm is that it causes {\em serious
  cache miss} when the number of data is large.  Fischer's algorithm
will construct LCA tables for blocks {\em on-demand}.  When the
algorithm finds that the corresponding LCA table is {\em not} in
memory, it will build the LCA table, which will be cached and this may
{\em evict} other LCA tables from cache.  This LCA table building
process repeats for as many times as the number of blocks, and may
cause serious cache misses.

In order to reduce cache miss, Demaine~\cite{Demaine2009OnCT} proposed
{\em cache-aware} operations on Cartesian
tree~\cite{Vuillemin1980AUL}.  Demaine's algorithm does not check if
an LCA table is in memory -- instead it builds LCA table for {\em
  every possible} block.  To do so, Demaine's algorithm uses a binary
string of length $2s$ to identify a block and its Cartesian tree.  The
binary string is encoded in such a way that one can answer an in-block
range maximum query by examining this binary string only.  However,
this examination requires counting the number of 1's {\em between} the
last two 0's, which is {\em hard} to implement efficiently in a modern
computer.

% We replace lookup operation to naive operation.  The naive operation
% is find the maximum value by comparing each element on the compressed
% data. On the other hand, the lookup operation find the index of
% maximum value from the ancestor table. When loading a element from index
% table, it also bring some useless data to caches.   In order to use
% cache efficiently, the naive operation is better than the lookup
% operation because the access pattern is almost one by one in our VGLCS
% algorithm.

\subsection{Rightmost-pops Encoding} \label{sec:cct}

We propose a new encoding for blocks, called {\em rightmost-pops},
instead of the binary string by Demaine~\cite{Demaine2009OnCT}, in order
to improve the performance of range maximum query.  This rightmost-pops
encoding is inspired by Demaine's algorithm and Cartesian tree.

The rightmost-pops encoding encodes a Cartesian tree by keeping only
the {\em rightmost} path of the Cartesian tree in a {\em stack}, and
keeping track of the {\em number of pops} from the stack when we add
data into the Cartesian tree.  Please refer to
Figure~\ref{fig:interval-cartesian} for an illustration.  To maintain
the heap property of the Cartesian tree, when we add the $i$-th data
$a_i$ into the Cartesian tree, we need to {\em pop} the data in the
stack, which stores the rightmost path of the Cartesian tree, that are
{\em smaller} than $a_i$.  We keep popping data until the top of stack
is no less than $a_i$, then we push $a_i$ into the stack.  Let $t_i$
be the number of nodes that need to be popped, and it is easy to see
that $0 \le t_i < s$, where $s$ is the block size.  We use these
$t_i$, the number of pops on the right most path, to encode a
Cartesian tree.

Consider the example in Figure~\ref{fig:interval-cartesian}.  When we
insert $a_1 = 0$, we just insert it into the stack since $a_0 = 1$, no
data is popped, and $t_1$ is $0$.  When we insert $a_2 = 4$ we need to
pop both $a_0$ and $a_1$ out of the stack, since they are smaller than
$a_2$, so $t_2$ is $2$.  As defined earlier, the contents of the stack
is exactly the rightmost path of the Cartesian tree, and we keep these
$t_i$'s to represent a Cartesian tree.

\begin{figure}[!thb]
  \centering
  \includegraphics[width=\linewidth]{\GraphicPath/fig-cartesian-encoding-stack.pdf}
  \caption{Rightmost path in stack}
  \label{fig:interval-cartesian}
\end{figure}

The key idea of rightmost-pops encoding is that we can use $t_i$'s to
{\em implicitly identify} the Cartesian tree of this block of data, and
that we can answer in-block range queries simply by examining these
$t_i$'s.  Suppose we want to answer an in-block maximum query that
ranges from $l$ to $r$ with these $t_i$'s.  We maintain the number of
times data are {\em popped} from the stack in a variable $x$, and
initialize $x$ to 0.  We then loop through $t_l$ to $t_r$ and let index
$j$ run from $l$ to $r$.  Every iteration adds 1 to $x$ then subtracts
$t_j$ from $x$.  We need to remember the index $j$ when $x$ becomes
smaller or equal to 0.  Finally, we report the {\em last} index $j$ when
$x$ becomes smaller or equal to 0, as the answer.  The pseudo code of
the query answering algorithm is in
Algorithm~\ref{alg:cartesian64bits-query}.  All the operations of
Algorithm~\ref{alg:cartesian64bits-query} map directly to machine
instructions so that unlike Demaine's algorithm,
Algorithm~\ref{alg:cartesian64bits-query} is extremely intuitive to
implement.

\input{\AlgoPath/alg-cartesian64bits-query-2e}

The correctness proof of Algorithm~\ref{alg:cartesian64bits-query} is
in Theorem~\ref{thm:correctness}.  The intuition is that we only need
to know the root of the tree when the last element in range was
inserted.

\begin{theorem} \label{thm:correctness}
  Algorithm~\ref{alg:cartesian64bits-query} correctly answers an
  in-block range maximum query.
\end{theorem}
\begin{proof}
When $x$, the number of poppings, is {\em smaller than or equal} to 0,
it means the added data has become the {\em root} of the tree of the
queried range.  As a result, when we add a data and it became the root
of the tree for the {\em last} time, the added data is indeed the
maximum among this interval, because according to the heap property, the
root is the maximum among all nodes within this tree.
\end{proof}

We also note that since all $t_i$'s are small than 16, we can represent
each of them as a 4-bit integer.  We then concatenate sixteen of these
4-bit integers into a 64-bit integer to present a Cartesian tree for a
block of sixteen data.  The pseudo code on how to build a 64-bit integer
to represent a block of 16 data is in
Algorithm~\ref{alg:cartesian-to-64bits}, which runs in time $O(s)$.
where $s$ is the block size.  Note that all the operations, e.g., shift,
addition, subtraction, in Algorithm~\ref{alg:cartesian-to-64bits} map
directly to machine instructions and are straightforward to implement.

\input{\AlgoPath/alg-cartesian-to-64bits-2e}

We choose the block size as $s = \frac{\log n}{4} = 16$ for performance
reason.  Modern CPUs support 64-bit register and fast operation on them.
When we pack 16 $t_i$ in to a 64-bit integer, we can leverage fast
64-bit instructions and improve performance.  In addition, we do {\em
not} build least common ancestor tables {\em explicitly} since we
implicitly maintain the Cartesian tree information within these 64-bit
$t_i$.  This approach reduces memory usage and improves cache
performance, it can also efficiently answer one-dimension range maximum
query for up to $n = 2^{64}$ data.

Note that our rightmost-pops encoding does improve cache performance,
but will increase the time complexity in answering queries.
Algorithm~\ref{alg:cartesian64bits-query} accesses data in a very
regular manner and has a better data locality than Fischer's algorithm.
The preprocessing time is $O(n)$, as same as in Fischer's algorithm.
However, a single query now needs $O(s)$ time, where $s$ is the block
size $\frac{\log n}{4}$.  This is acceptable in practice since we set
the block size $s$ to $16$ for $n$ up to $2^{64}$, so $s$ is a small
constant.  The space complexity is $O(n)$ as in Fischer's algorithm. The
overall time complexity of the parallel version of our VGLCS algorithm
becomes $O(n^2 \log{n} / p + n \log n)$.  Note that the $\log n$ comes
from the block size $s = O(\log n)$


\newsection{Data Types}

\subsection{Motivation}
Now that we know how to compile and run programs, and print to the screen, we will bump up the complexity of our programs a little. In this chapter, we will be exploring data types, and how to do basic calculations.

\subsection{Types}
There are 8 ``primitive" (fundamental) data types in Java (we will explain constraints that they have later):
\begin{itemize}
\item \texttt{byte} - an integer with a small range.
\item \texttt{short} - same as \texttt{byte}, but a larger range.
\item \verb|int| - same as \verb|short|, but even larger range.
\item \verb|long| - same as \verb|int|, but even still larger range.
\item \verb|float| - a ``real" value, that has some precision.
\item \verb|double| - same as \verb|float| but with more precision.
\item \verb|boolean| - a type that takes one of two possible values: \verb|true|, or \verb|false|.
\item \verb|char| - a Unicode character. Think of \verb|a| or \verb|b| as \verb|char|s - any single character is a \verb|char|.
\end{itemize}

\noindent Now, we give the range for each of the primitive data types (all inclusive on both ends). These are all of the values that they can hold:
\begin{itemize}
\item \verb|byte|: -128 to +127.
\item \verb|short|: -32768 to +32767.
\item \verb|int|: $-2^{31}$ to $+2^{31}-1$.
\item \verb|long|: $-2^{63}$ to $+2^{63}-1$.
\item \verb|float|: $1.4 \times 10^{-45}$ to $3.4 \times 10^{38}$.
\item \verb|double|: $4.9 \times 10^{-324}$ to $1.7 \times 10^{308}$.
\item \verb|boolean|: \verb|true| or \verb|false|.
\item \verb|char|: 0 to 65535.
\end{itemize}
Now that we know the 8 fundamental data types, we want to know how to use them. To do that, we have to $declare$ a $variable$ with a type.

\subsection{Variable Declaration}
Like classes in the previous chapter, declaring variables follow the same basic structure, which is:
\begin{center}
$<Type>$ $<VariableName> = $ $<Value>;$
\end{center}
For example, if we want a \verb|int| variable called \verb|val| to have a value of 2, we would write the line of code as:

\begin{lstlisting}
int val = 2; // assign val to a value of 2
\end{lstlisting}

\noindent Also, we can declare several variables of the same type in the same line using a comma between the names:
\begin{lstlisting}
int val = 2, otherVal = 3;
\end{lstlisting}
Notes:
\begin{itemize}
\item In a list of declarations, there is no need to declare the type twice (in fact, there will be a compiler error if this is done)
\item The convention in Java names is to follow camel casing: the first ``word" is lowercase, and the subsequent ``words" in the same variable are capitalized (see example above).
\end{itemize}

\noindent Now, there are only certain names that our Java variables can have. The rules are:
\begin{itemize}
\item The name cannot be a ``keyword" (any of the words in this document that are colored blue, such as \verb|int|, \verb|public|, etc.).
\item The first character must be a letter (either upper or lowercase), the underscore symbol, or a dollar sign (``\$"). However, a variable name cannot start with a number.
\item Any character other than the first may be letters, underscores, the dollar sign, or also numbers.
\end{itemize}

\noindent Here are some variable declarations using all primitive types:
\begin{lstlisting}
byte b = 1;
short s = 2;
int i = 300;
long l = 1000L; // note the 'L'
float f = 0.56f; // note the 'f'
double d = 523.6; // no suffix
boolean b = true;
char c = 'a'; // chars are surrounded in single quotes
\end{lstlisting}

\noindent After we have initialized a variable, we can also modify it without having to declare it again. Here is an example:
\begin{lstlisting}
int i = 300;
i = 301; // modifies i's value to be 301
// int i = 301; // this causes a compiler error
		//(declare once)
\end{lstlisting}

\subsection{Primitive Operators}
Now that we know how to declare and modify values, we need to learn how to do various operations on them, such as addition, subtraction, multiplication, and division. An example showing all of them is the following (with comments):
\begin{lstlisting}
/* Addition */
// int works the same as byte and short
int i1 = 0, i2 = 3;
int i3 = i1 + i2; // adds i1's value (0) and i2's (3)

// doubles and floats work the same way:
double d1 = 2.5, d2 = 3.0;
float d3 = d1 + d2;
float f1 = 2.5f, f2 = 3.0f;
float f3 = f1 + f2;

// Can add across types, but is promoted to "most precise"
// If we add double + int, the result is a double
int i4 = 4;
double d4 = 5.3;
double d5 = d4 + i4;
// But we can also "cast" (with parentheses) to the type we want if it is less "precise" - this truncates the fractional part:
int casted = (int)(d4 + i4); // error without (int)

// cannot do operations on booleans (compiler error)
// boolean b1 = true + false;

/* Subtraction */
// Works with primitive types the same way as addition
int i5 = 0, i6 = 3;
int i7 = i5 - i6; // i6 has value -3

/* Multiplication */
// Multiplication is the same as addition in the same type
// However, we promote to "most precise" value if multiple types
int i8 = 5;
double d6 = 5.3;
double d7 = d6*i8; // promote i8 to a double valued 5.0

/* Division */
// Division is easy to understand except for when the numerator and denominator are both int/short/byte.
int i9 = 10, i10 = 3;
// What is i9 / i10? We expect 3.33333..., but we get 3 instead. Java does "integer division", where the numerator and denominator are both ints, which is truncating all the fractional parts.
int i11 = i9 / i10; // i11 has value 3.
// However, if we "cast" one of the values to a double, then we do not have this problem
double d8 = (double)i9 / i10; // cast i9 to a double of value 10.0
// d8 now has value 3.333333...
\end{lstlisting}

Another operation is called the mod operator (or ``remainder"):
\begin{lstlisting}
int i1 = 10, i2 = 3;
int i3 = i1 % i2; // i3 has value 1, since 10 remainder 3 = 1.
\end{lstlisting}

We can also ``shorten" times when we are modifying the same value with another. For example:
\begin{lstlisting}
int i = 5;
i += 3; // equivalent to: i = i + 3;
i -= 3; // equivalent to: i = i - 3;
i *= 3; // equivalent to: i = i * 3;
i /= 3; // equivalent to: i = i / 3;
\end{lstlisting}

For \verb|int| variables, there are 2 operations that are done very often, so they were put into the language: increment and decrement:
\begin{lstlisting}
int i = 2;
i++; // equivalent to: i += 1;
i--; // equivalent to: i -= 1;
\end{lstlisting}

\subsection{Boolean}

So how do we compare values? There are various operators (parentheses added for clarity - they are not needed):
\begin{lstlisting}
int i1 = 5, i2 = 6;
boolean larger = (i1 > i2); // false, since 5 < 6
boolean largerOrEqual = (i1 >= i2); // also false
boolean smaller = (i1 < i2); // true
boolean smallerOrEqual = (i1 <= i2); // true
boolean notEqual = (i1 != i2); // true
boolean equal = (i1 == i2); // false
// Note that == means compare value, = means assign
\end{lstlisting}

There are also operators to work with booleans: ``and", ``or", and ``not".
\begin{lstlisting}
/* And = && */
boolean t = true, f = false;
boolean andTrueTrue = t && t; // true
boolean andTrueFalse = t && f; // false
boolean andFalseTrue = f && t; // false
boolean andFalseFalse = f && f; // false
/* Or = || */
boolean orTrueTrue = t || t; // true
boolean orTrueFalse = t || f; // true
boolean orFalseTrue = f || t; // true
boolean orFalseFalse = f || f; // false
/* Not = ! */
boolean notTrue = !t; // false
boolean notFalse = !f; // true

// We can use multiple operators at once:
boolean myst = !(true && (true || false)); // false
\end{lstlisting}

What if we do not want our variable values to change? There is a way, with the \verb|final| keyword:
\begin{lstlisting}
final int NUM = 100; // convention: all capital letters for final variables
// NUM++; // compilation error
\end{lstlisting}

\subsection{String}
Now that we fully understand how primitive data types work, we can move on to using classes provided by the Java API (Application Programmer Interface). One such class is provided in the Java package \verb|java.lang|, called \verb|String|. This package is so often that Java automatically imports it for you, so the programmer does not have to do extra work.

\par A \verb|String| is a set of characters in between double quotes. Any character can be inside a string, except for a double quote. Examples are:
\begin{lstlisting}
String str1 = "I love";
String str2 = "Java";
String str3 = str1 + str2; // is now "I loveJava"
// str3 is called the "concatenation" of str1 and str2
int i1 = 3;
str3 = str1 + i1; // has value "I love3"
\end{lstlisting}

\noindent However, there is another way to declare a \verb|String| variable, and that is done using the Java keyword \verb|new|:
\begin{lstlisting}
String str1 = new String("I love"); // same as above
\end{lstlisting}

\verb|String| is the only Java class that can declare (also called ``instantiate") a variable without the \verb|new| keyword. All other Java classes must be instantiated with this keyword.

\par Now we need to see what we can do with these \verb|String| variables. As we covered in the last chapter, every Java class has one or more methods. The syntax for calling a method is:

\begin{center}
$variable.methodname(optional parameters)$
\end{center}

Note: \textbf{we cannot call methods on primitive data types} (see later chapters on classes and methods). Examples of various methods in Java for the \verb|String| class are:
\begin{lstlisting}
String str1 = "I love Java";
/*String.toUpperCase() and toLowerCase*/
String upper = str1.toUpperCase(); // "I LOVE JAVA"
String lower = str1.toLowerCase(); // "i love java"

/*String.replace(char c, char d)*/
// Replaces all instances of c with d
String repl = str1.replace('a', 'p'); // "I love Jpvp"

// We can even chain method calls
String myst = str1.replace('a', 'p').toUpperCase(); // "I LOVE JPVP"

/*String.length() - int which is # of characters*/
int len = str1.length(); // 11, since there are 11 characters

/*String.charAt(int n) - gets the nth character*/
// Strings are 0-indexed, so the first character is at index 0
// Error at runtime if input >= length()
char firstChar = str1.charAt(0); // has value 'I'
char secondChar = str1.charAt(1); // has value ' '
char lastChar = str1.charAt(str.length()-1); // 'a'

/*String.substring(int n, int m)*/
// gives a String that consists of index n through m, not inclusive on m
String substr1 = str1.substring(0, 3); // "I lo"
String substr2 = str1.substring(2, 6); // "love"

/*String.equals(String other)*/
// gives true (boolean) if the first String is exactly the same as the other
boolean equal1 = substr1.equals(substr2); // false
boolean equal2 = "Equal".equals("Equal"); // true
// We can also check for equality ignoring the case
boolean equal3 = "Equal".equalsIgnoreCase("eqUal"); // true

/*String.indexOf(char c)*/
// Returns index (int) of the first instance of c; otherwise, -1
int found1 = str1.indexOf('o'); // 4
int found2 = str1.indexOf('f'); // -1

/*String.compareTo(String other)*/
// Returns int showing lexicographic ordering of strings according to ASCII table (negative if first < second, 0 if equal, positive if first > second)
int c1 = "A".compareTo("B"); // negative
int c2 = "A".compareTo("b"); // more negative, lowercase has higher ASCII value
int c3 = "A".compareTo("A"); // 0
int c4 = "B".compareTo("A"); // positive
\end{lstlisting}

\subsection{Scanner}
Now that we know how to create and modify modify \verb|String| variables, it is important to know how to get input from the user so that we can make our programs usable. For that, we use the \verb|Scanner| class in the \verb|java.util| package.

\par However, since this class is not in the package \verb|java.lang|, we must $import$ it from the Java library, by putting an import statement at the beginning of our file:
\begin{lstlisting}
import java.util.Scanner; // import the Scanner class from the java.util package
// Equivalent: import java.util.*; // import everything from this package
public class SomeClass {
	// ...
}
\end{lstlisting}

\noindent So how do we use this class? We must initialize it with the \verb|new| keyword:
\begin{lstlisting}
Scanner s = new Scanner(System.in); // System.in means from the keyboard
\end{lstlisting}
\noindent This code creates an variable (often called ``object" in this case, since it is a class) in which we can call various methods on it to get input from the user. There are various ways of getting input from the user:

\begin{lstlisting}
Scanner s = new Scanner(System.in);
int x = s.nextInt(); // the first integer entered by the user (after pushing the enter/return key) will be put into x
// If an integer is not entered, then a run-time error will occur.
double y = s.nextDouble(); // if a user enters a double
String str1 = s.next(); // this is the next "word" (does not include spaces)
String str2 = s.nextLine(); // this is the entire line of characters before pressing the enter/return key
\end{lstlisting}

%\subsection{Example: Putting it all together}
%Suppose we were given the task of having a menu at a restaurant, with a menu of:
%\begin{itemize}
%\item Hamburgers = \$2.75 each
%\item Cheeseburgers = \$3.25 each
%\item Drinks = \$2.00 each
%\end{itemize}
%And we allowed as many choices for each as the user wanted. Our task is to take in the user's input of how much of each item he/she wants, and calculate the total. Suppose the user asked for 2 hamburgers, 1 cheeseburger, and 3 drinks. Therefore, the total will be: $2.75 \times 2 + 3.25 \times 1 + 2.00 \times 3 = 14.75$.
%\\ \\
%First, we need to set up our class. Let's call it RestaurantMenu:
%\begin{lstlisting}
%public class RestaurantMenu { // begin class
%     public static void main(String[] args) { // main
%     }
%}
%\end{lstlisting}
%
%Now, we know we need to take input from the user. Therefore, we need to create a Scanner variable (and do not forget to import the \verb|java.util| package!):
%
%\begin{lstlisting}
%import java.util.Scanner; // get user input
%public class RestaurantMenu { // begin class
%     public static void main(String[] args) { // main
%          Scanner input = new Scanner(System.in);
%     }
%}
%\end{lstlisting}
%
%\noindent We know that there are 3 items on the board, and that the user will enter three integers. Therefore, we need to get these integers from the user. It may be helpful to print to the user how much of each item to input:
%
%\begin{lstlisting}
%import java.util.Scanner; // get user input
%public class RestaurantMenu { // begin class
%     public static void main(String[] args) { // main
%          // Menu
%          System.out.println("Menu");
%          System.out.println("Hamburger - $2.75");
%          System.out.println("Cheeseburger - $3.25");
%          System.out.println("Drink - $2.00");
%          // Get Inputs from user
%          Scanner input = new Scanner(System.in);
%          System.out.println("# hamburgers?");
%          int numHamburgers = input.nextInt();
%          System.out.println("# cheeseburgers?");
%          int numCheeseburgers = input.nextInt();
%          System.out.println("# drinks?");
%          int numDrinks = input.nextInt();
%     }
%}
%\end{lstlisting}
%
%\noindent Now we need to include the price of each item in some way. It would be preferable to use \verb|final| variables to store the prices, since we can guarantee that the prices will never change (also preferable to put them near the beginning of the main method):
%\begin{lstlisting}
%import java.util.Scanner; // get user input
%public class RestaurantMenu { // begin class
%     public static void main(String[] args) { // main
%          // Prices
%          final double HAM_PRICE = 2.75;
%          final double CHEESE_PRICE = 3.25;
%          final double DRINK_PRICE = 2.00;
%          // Menu
%          System.out.println("Menu");
%          System.out.println("Hamburger - $2.75");
%          System.out.println("Cheeseburger - $3.25");
%          System.out.println("Drink - $2.00");
%          // Get inputs from user
%          Scanner input = new Scanner(System.in);
%          System.out.println("# hamburgers?");
%          int numHamburgers = input.nextInt();
%          System.out.println("# cheeseburgers?");
%          int numCheeseburgers = input.nextInt();
%          System.out.println("# drinks?");
%          int numDrinks = input.nextInt();
%     }
%}
%\end{lstlisting}
%
%\noindent The last two parts are to compute the total price based on the user input, and to print out the result to the user:
%\begin{lstlisting}
%import java.util.Scanner; // get user input
%public class RestaurantMenu { // begin class
%     public static void main(String[] args) { // main
%          // Prices
%          final double HAM_PRICE = 2.75;
%          final double CHEESE_PRICE = 3.25;
%          final double DRINK_PRICE = 2.00;
%          // Menu
%          System.out.println("Menu");
%          System.out.println("Hamburger - $2.75");
%          System.out.println("Cheeseburger - $3.25");
%          System.out.println("Drink - $2.00");
%          // Get Inputs from user
%          Scanner input = new Scanner(System.in);
%          System.out.println("# hamburgers?");
%          int numHamburgers = input.nextInt();
%          System.out.println("# cheeseburgers?");
%          int numCheeseburgers = input.nextInt();
%          System.out.println("# drinks?");
%          int numDrinks = input.nextInt();
%          // Calculate total price
%          double totalPrice = numHamburgers*HAM_PRICE;
%          totalPrice += numCheeseburgers*CHEESE_PRICE;
%          totalPrice += numDrinks*DRINK_PRICE;
%          System.out.println("Total Price: " + totalPrice);
%     }
%}
%\end{lstlisting}

\subsection{Math}
There is another class, called \verb|Math| (in the \verb|java.lang| package) that provides some useful functionalities:
\begin{lstlisting}
/* Square root */
double num1 = 105.0;
double d1 = Math.sqrt(num1);

/* PI - constant */
double pi = Math.PI;

/* Math.{min, max} - minimum or maximum of 2 ints */
int num1 = 5, num2 = 3;
int max = Math.max(num1, num2); // 5
// also can be: Math.max(num2, num1);
int min = Math.min(num1, num2); // 3

/* Pow - take first double to power of second */
double d2 = 3.5, d3 = 4.3;
double d4 = Math.pow(d2, d3); // = 3.5^4.3
\end{lstlisting}

\subsection{Written Exercises}

\begin{enumerate}
\item Give the output of the following program:
\begin{lstlisting}
public class Example {
     public static void main(String[] args) {
          int y = 2, z = 1;
          z = y * 2;
          System.out.print(y + z);
     }
}
\end{lstlisting}

\item What will be the output of the following program?
\begin{lstlisting}
public class Example {
     public static void main(String[] args) {
          String s = new String("Arizona state university");
          char ch1 = s.toLowerCase().toUpperCase().charAt(0);
          char ch2 = s.toUpperCase().charAt(8);
          char ch3 = s.toUpperCase().charAt(s.length() - 1);
          System.out.println("ch 1 is: " + ch1);
          System.out.println("ch 2 is: " + ch2);
          System.out.println("ch 3 is: " + ch3);
     }
}
\end{lstlisting}

\item What will be the output of the following program?
\begin{lstlisting}
public class Example {
     public static void main(String[] args) {
          int num1 = 4, num2 = 5;
          System.out.println("4"+"5");
          System.out.println(num1+num2);
          System.out.println("num1"+"num2");
          System.out.println(4+5);
     }
}
\end{lstlisting}

\item Which of the following correctly invokes the method \verb|length()| of the \verb|String| variable \verb|str| and stores the result in \verb|val| of type \verb|int|?
\begin{lstlisting}
int val = str.length();
int val = length.str();
int val = length().str;
int val = length(str);
\end{lstlisting}

\item Evaluate each of the following expressions.
\begin{lstlisting}
String s = "Programming is Fun";
String t = "Workshop is cool";
System.out.println(s.charAt(0)+t.substring(3, 4));
System.out.println(t.substring(7));
\end{lstlisting}

\item Evaluate each of the following expressions, assuming \verb|j| is an \verb|int| with value 11, \verb|k| is an \verb|int| with value 3, and \verb|s| is a \verb|String| with value ``Ford Rivers".
\begin{lstlisting}
j / k
j % k
s.substring(1, 5)
s.length()
s.charAt(3)
\end{lstlisting}

\item True or False? The type \verb|String| is a primitive data type.

\item True or False? The type \verb|char| is a primitive data type.

\item Write the output of the following program:
\begin{lstlisting}
public class Question {
     public static void main(String[] args) {
          String str = "hello";
          System.out.println("abcdef".substring(1, 3));
          System.out.println("pizza".length());
          System.out.println(str.replace('h', 'm'));
          System.out.println("hamburger".substring(0, 3));
          System.out.println(str.charAt(1));
          System.out.println(str.equals("hello"));
          System.out.println("pizza".toUpperCase());
          System.out.println(Math.pow(2, 4));
          double num4 = Math.sqrt(16.0);
          System.out.println(num4);
     }
}
\end{lstlisting}

\item Write the output of the following program:
\begin{lstlisting}
public class Question {
     public static void main(String[] args) {
          String s1 = "Clinton, Hillary";
          String s2 = new String("Obama, Barack");
          System.out.println(s1.charAt(2));
          System.out.println(s1.charAt(s1.length()-1));
          System.out.println(s2.toUpperCase());
          System.out.println(s2.substring(
               s2.indexOf(",")+2, s2.length());
     }
}
\end{lstlisting}

\item What value is contained in the \verb|int| variable \verb|length| after the following statements are executed?
\begin{lstlisting}
length = 2;
length *= 9;
length -= 6;
\end{lstlisting}

\item What is the result of \verb|2/4| when evaluated in Java? Why?

\end{enumerate}

\subsection{Programming Exercises}

\begin{enumerate}

\item Write a Java program that asks the user for the radius of a circle and finds the area of the circle.

\item Write a Java program that prompts the user to enter 2 integers. Print the smaller of the 2 integers.

\end{enumerate}