\chapter{Theoretical Framework}
\section{Relativistic Electrons}
In a (linear) particle accelerator, charged particles, such as electrons, are accelerated to increase their total energy over their energy at rest.

Compared to heavier particles, such as protons ($m_p=\SI{938.27}{\mega\eV\per c\squared}$), electrons are light particles ($m_e=\SI{0.511}{\mega\eV\per c\squared}$). They are typically accelerated to speeds comparable to the speed of light to achieve kinetic energies usable for scientific experiments. For this reason, relativistic mechanics are needed to describe their movements.\footnote{As relativistic mechanics are a super set of classical mechanics, the equations also apply for slower particles.} \cite{Hinterberger1997}

With the speed of light $c=\SI{2.99792458e8}{\m\per\s}$ and the particle velocity $v$, it is common to define \cite{Wangler2008}:
\begin{align}\label{eq:theoreticalFramework_gammabeta}
\text{(normalized velocity)}\qquad\beta &= \frac{v}{c}\\
\text{(relativistic mass factor)}\qquad\gamma &= \frac{1}{\sqrt{1-\beta^2}} \\
\text{(relativistic momentum)}\qquad p &= \gamma m v
\end{align}

The total energy of a particle is \cite{Hinterberger1997}
\begin{equation}
W=\sqrt{(mc^2)^2+(pc)^2},
\end{equation}
with the rest energy $mc^2$ and the kinetic energy is
\begin{equation}
T=W-mc^2.
\end{equation}

With electrons (mass $m_e$) leaving the \gls{flute} electron gun with a momentum of $p=\SI{7}{\MeV\per c}$, this is equivalent to
\begin{align}
W=\sqrt{(m_ec^2)^2+(pc)^2} &= \SI{7.0186}{\MeV}\\
\gamma = \frac{W}{m_ec^2} &= \num{13.7351}\\
\beta = \sqrt{1-\gamma^{-2}} &= 0.9973
\end{align}

The force exerted on an electron (charge $q$) by an electric field $E$ and a magnetic flux density $B$ is given by the Lorentz force \cite[p.~19]{Hinterberger1997}
\begin{equation}\label{eq:fl}
F_L = q\left(E+v\times B\right).
\end{equation}
As the force due to the magnetic field is scaled with the velocity $v$, it is technically more viable to steer and focus a relativistic electron with magnets instead of electric fields. This is mainly because strong magnetic fields are easier to create and work with than high electric fields, which are difficult to handle. \cite[p.~27]{Krieger2013}

\autoref{eq:fl} also motivates the use need for stable electron energies. Since the velocity depends on the energy, the force on the electrons is also a function of their energy. The magnetic fluxes of electromagnets can only be changed slowly compared to the repetition rate of an accelerator, so corrections to $B$ are not possible, which is why $v$/$E$ needs to be stable for a stable trajectory/focusing.

\section{Signal Analysis}
\subsection{Auto- and Cross-Covariance}
The \textit{cross covariance} between two stochastic processes $x[n]$ and $y[n]$ is a measure of the similarity between $x[n]$ at index $n_1$ and $y[n]$ at index $n_2$ and is defined as
\begin{equation}\label{eq:crosscovariance}
r_{xy}[n_1,n_2] = \text{E}\left\{(x[n_1]-\mu_x[n_1])(y[n_2]-\mu_y[n_2])^\ast\right\}.
\end{equation}

For the special case of $y[n]:=x[n]$, $r_{xx}[n_1,n_2]$ is called \textit{auto covariance} and is a measure of self similarity of $x[n]$. \cite[p.~172]{Park2017}

The processes $x[n]$ and $y[n]$ are called \textit{\gls{wss}} if the following two properties hold. \cite[p.~167]{Park2017}
First, their means $\mu_{\xi}[n]$ are constant, i.e. they do not depend on the sample index:
\begin{align}\label{eq:wss}
\mu_{x}[n] &= \mu_x\\
\mu_{y}[n] &= \mu_y
\end{align}
Also, the auto covariance does not depend on the absolute sample indices $n_1$ and $n_2$, but merely on the difference between them:
\begin{equation}
r_{xy}[n_1,n_2] = r_{xy}[m],\qquad \text{with: } m:=n_2-n_1
\end{equation}

If both processes in \autoref{eq:crosscovariance} are \gls{wss}, \autoref{eq:crosscovariance} simplifies to
\begin{equation}
r_{xy}[m] = \text{E}\left\{(x[n]-\mu_x)(y[n-m]-\mu_y)^\ast\right\}.
\end{equation}

For the auto covariance both means are identical and can be moved outside the expectation operator:
\begin{equation}
r_{xx}[m] = \text{E}\left\{(x[n])(x[n-m])^\ast\right\}-\mu_{x}^2.
\end{equation}

When analyzing signals, the stochastic processes are often unknown and only one realization $x[n]$ is known. But if the process generating $x[n]$ is \textit{(weakly) ergodic}, then one realization is enough to determine the auto covariance of the process. \cite[p.~252]{Puente2019}
Then the auto covariance can be estimated with
\begin{equation}\label{eq:autocovarianveEstimation}
\hat{r}[m] = \frac{1}{N} \sum_{n=m+1}^{N} x[n]\,x^\ast[n-m]\qquad m \in [0,\,N-1].
\end{equation}

\subsection{Estimating the Spectrum of a Stochastic Process}
For a deterministic, time-discrete signal $x[n] \in \mathcal{L}_1$, the \gls{dft} exists (see \cite{Lapidoth2019}) and is defined as
\begin{equation}
X[k] = \sum_{n=0}^{N-1} x[n]\,\text{e}^{-j\frac{2\pi}{N}k\,n}\qquad k,n \in [0,\,N-1],
\end{equation}
using $k=\frac{N}{2\pi}\,\omega = N\,f$ as the independent, discrete frequency variable.
From the complex sequence $X[k]$, often only the magnitude (or energy) is of greater interest while the phase information are neglected.
Therefore, $S_{xx}$ is defined as
\begin{equation}
S_{xx} = \left|X[k]\right|^2,
\end{equation}
called the \textit{\gls{esd}}.

If $x[n]$ is the realization of a stochastic process, then it is of random nature rather than deterministic.
Because realizations of physical processes do not posses finite energy, they are not in the $\mathcal{L}_1$ set and their \gls{dft} is not defined. \cite[p.~5]{Stoica1997}

In this case instead of an energy spectral density, the spectrum of the average power of the process, called the \textit{\gls{psd}}, can be used instead.
To compute the \gls{psd}, there are two possibilities:
\begin{align}
\Phi_{xx}[k] &= \sum_{m=-\infty}^{\infty} r[m]\,\text{e}^{-j\frac{2\pi}{N}k\,m} \label{eq:phixx1}\\
\Phi_{xx}[k] &= \lim_{N\rightarrow\infty} \text{E}\left\{\frac{1}{N}\left|\sum_{n=0}^{N-1} x[n]\,\text{e}^{-j\frac{2\pi}{N}k\,n}\right|^2 \right\} \label{eq:phixx2}
\end{align}
When assuming $r[m]$ decays ``fast enough'', i.e.
\begin{equation}
\lim_{N\rightarrow\infty} \frac{1}{N} \sum_{m=-N}^{N} |m|\,\left|r[m]\right| = 0
\end{equation}
then \autoref{eq:phixx1} and \autoref{eq:phixx2} are equal. \cite[p.~7]{Stoica1997}

For measured data however neither equations can be used directly.
For \autoref{eq:phixx1} the auto covariance sequence $r[m]$ is unknown.
But it could be estimated with \autoref{eq:autocovarianveEstimation}. In case of \autoref{eq:phixx2} it is not possible to evaluate the limit, because only finite length data can be sampled and also the expectation can not be computed since in general there is only one realization available. Both operations can be neglected when doing an estimation.

With these practical changes in place, \autoref{eq:phixx1} and \autoref{eq:phixx2} become
\begin{align}
\hat{\Phi}_{c,\,xx}[k] &= \sum_{m=-(N-1)}^{N-1} \hat{r}[m]\,\text{e}^{-j\frac{2\pi}{N}k\,m} \label{eq:phiCxx1}\qquad\text{(Correlogram)}\\
\hat{\Phi}_{p,\,xx}[k] &= \frac{1}{N} \left| \sum_{n=0}^{N-1} x[n]\,\text{e}^{-j\frac{2\pi}{N}k\,n}\right|^2\qquad\text{(Periodogram)}\label{eq:periodogram}.
\end{align}

Both methods yield equal results, if $r[m]$ is estimated with the biased estimator $\hat{r}[m]$ in \autoref{eq:autocovarianveEstimation} in contrast to the unbiased estimator (compare \cite[p.~24]{Stoica1997})
\begin{equation}
\hat{r}_{\text{unbiased}}[m] = \frac{1}{N-m} \sum_{n=m+1}^{N} x[n]\,x^\ast[n-m]\qquad m \in [0,\,N-1].
\end{equation}

\cite{Rowell2008} shows one key weakness of the unmodified periodogram method in \autoref{eq:periodogram}: The variance does not decrease significantly with more samples $N$. Instead. the variance of the periodogram for each frequency approaches the square of the actual \gls{psd}:
\begin{equation}
\lim_{N\rightarrow\infty} \text{Var}\left\{\hat{\Phi}_{p,\,xx}[k]\right\} = \Phi_{xx}^2[k]
\end{equation}
Furthermore, the periodogram/correlogram suffer from the smearing and leakage effects because the limited length of the data samples always causes an implicit windowing, thus reducing frequency resolution.\\

There are several popular methods that improve on the periodogram/correlogram concepts:

\textbf{Blackman-Tukey:} Because of the poor accuracy of $\hat{r}[m]$ for $k\approx N$ in the definition of $\hat{\Phi}_{c,\,xx}[k]$ and the bigger the $N$, the more small errors in $\hat{r}[m]$ sum up, truncating/windowing of $\hat{r}[m]$ with $w[k]$ (length $M$) can be beneficial for the accuracy of the estimation.
\begin{equation}
\hat{\Phi}_{BT,\,xx}[k] = \sum_{m=-(M-1)}^{M-1} w[k]\hat{r}[m]\,\text{e}^{-j\frac{2\pi}{N}k\,m}
\end{equation}
The choice of the window $w[k]$ trades frequency resolution for variance and smearing for leakage reduction. \cite[p.~41]{Stoica1997}

\textbf{Barlett:} The Barlett method reduces the variance of the periodogram by splitting the $N$ data samples in $Q=\nicefrac{N}{M}$ blocks and averaging together the sub-periodograms:
\begin{align}
\hat{\Phi}_{q,\,xx}[k] &= \frac{1}{M} \left| \sum_{n=0}^{M-1} x_q[n]\,\text{e}^{-j\frac{2\pi}{M}k\,n}\right|^2\\
\hat{\Phi}_{B,\,xx}[k] &= \frac{1}{Q} \sum_{q=1}^{Q} \hat{\Phi}_{q,\,xx}[k]
\end{align}
The variance of the estimation scales with $Q$ (see \cite[p.~6]{Rowell2008}):
\begin{equation}
\text{Var}\left\{\hat{\Phi}_{B,\,xx}[k]\right\} = \frac{1}{Q}\Phi_{xx}^2[k]
\end{equation}

\textbf{Welch:} The Welch method combines splitting the data into $Q$ segments with windowing each segment and allowing the segments to overlap. With $P = \nicefrac{1}{M} \sum_{n=0}^{M-1} |w[n]|^2$ being the ``power'' of the window, the Welch method is computed as
\begin{align}
\hat{\Phi}_{s,\,xx}[k] &= \frac{1}{M P} \left| \sum_{n=0}^{M-1} x_s[n]\,\text{e}^{-j\frac{2\pi}{M}k\,n}\right|^2\\
\hat{\Phi}_{W,\,xx}[k] &= \frac{1}{Q} \sum_{s=1}^{Q} \hat{\Phi}_{s,\,xx}[k].
\end{align}
Compared to the Barlett method, the overlapping of up to \SI{50}{\percent} (see \cite{Welch1967}) allows increasing $Q$, thus reducing the variance.
\begin{equation}\label{eq:varWelch}
\text{Var}\left\{\hat{\Phi}_{W,\,xx}[k]\right\} = \frac{1}{Q}\Phi_{xx}^2[k]
\end{equation}

\paragraph{Spectogram}
If a stochastic process or a signal as a realization of that process $x[n]$ is not \gls{wss}, one possibility to analyze and display the spectral content is the use of the \gls{stft} and the spectrogram, which is a two dimensional power spectral density function mapping frequency and time to a third coordinate like height, intensity or color.

To calculate the spectrogram, the signal is split into segments with the sliding window $w[n-m]$ for which duration the signal is assumed to be stationary. For each segment at time index $m$, the periodogram is calculated according to
\begin{equation}
\hat{\Phi}_{xx}[k,m] = \frac{1}{N} \left| \sum_{n=0}^{N-1} w[n-m] x[n]\,\text{e}^{-j\frac{2\pi}{N}k\,n}\right|^2.
\end{equation}

\section{Feedback Control Systems}\label{sec:feedbackcontrol}
Feedback control systems are used to control a dynamic system (also called a plant) in such a way that its output $y(t)$ follows a certain input $x(t)$ and disturbances on the output $d(t)$ are rejected. The general structure of a closed-loop control system is shown in \autoref{fig:theoreticalFramework-feedback-architecture}. To achieve sufficient tracking of the input and stabilization of the output, a controller $G(s)$ uses the error $e(t)$ to control the plant $P(s)$ accordingly.
The error is defined as
\begin{equation}
e(t)=x(t)-r(t) = x(t)-[y(t)\ast h(t)]
\end{equation}
with $h(t)$ being the inverse Laplace transform of the filters transfer function $H(s)$. The signal $r(t)$ is the output of the measurement filter $h(t)$ or $H(s)$. It is most commonly a lowpass filter used to reject high-frequency noise on the system output $y(t)$.

Feedback control systems, or closed-loop systems, are to be differentiated from open-loop systems, in which there is no return path, so they cannot compensate for \textit{un}known disturbances. If $d(t)$ is known $\forall t$, then an open loop system would be possible and any errors could simply be compensated. But for real world application this approach is only usable for crude control tasks or if the system is very well understood or isolated from its surroundings.

\begin{figure}[tbh]
	\centering
	\includegraphics[width=\textwidth]{chap/TheoreticalFramework/img/feedbackControl/architecture.tikz}
	\caption[Control system structure]{General structure of a time-continuous feedback control system}
	\label{fig:theoreticalFramework-feedback-architecture}
\end{figure}

\subsection{Disturbance Rejection and Input Tracking}\label{sec:drandtrack}
Disturbance rejection and input tracking are two important characteristics to evaluate a stable controller. To calculate them, the block diagram in \autoref{fig:theoreticalFramework-feedback-architecture} and the Laplace transform of the inputs/outputs are used.\footnote{The Laplace transform of a function in time $f(t)$ is written as $F(s)=\mathcal{L}\left\{f(t)\right\}$.}

To calculate how the output $y(t)$ depends on the input $x(t)$, the input tracking transfer function can be used. \cite[p.~88]{Foellinger2016}
It is calculated as the transfer function $F_T=\nicefrac{Y(s)}{X(s)}$ by setting $d(t)=0$:
\begin{align}\label{eq:inputTracking}
Y(s)&=G(s)P(s)E(s)\quad\text{with:}\quad E(s)=X(s)-H(s)Y(s)\\
\Leftrightarrow Y(s)\left[1+G(s)P(s)H(s)\right]&=G(s)P(s)X(s)\\
\Leftrightarrow F_T:=\frac{Y(s)}{X(s)} &= \frac{G(s)P(s)}{1+G(s)P(s)H(s)}.
\end{align}

On the other hand, the transfer function $F_{DR}=\nicefrac{Y(s)}{D(s)}$ can be used to describe the systems response to a disturbance. \cite[p.~88]{Foellinger2016} It is defined by setting $x(t)=0$ and calculating
\begin{align}\label{eq:disturbanceRejection}
Y(s)&=G(s)P(s)E(s)+D(s)\quad\text{with:}\quad E(s)=-H(s)Y(s)\\
\Leftrightarrow Y(s)\left[1+G(s)P(s)H(s)\right]&=D(s)\\
\Leftrightarrow F_{DR}:=\frac{Y(s)}{D(s)}&=\frac{1}{1+G(s)P(s)H(s)}.
\end{align}

\subsection{Control System Stability}
The application of a controller to a system is only useful if the resulting system has a stable behavior.
One possible definition of stability is the \gls{bibo} criterion (see \cite[p.~82]{Foellinger2016}):
\begin{definition}\label{def:bibo}
(\textit{BIBO stability}) A \gls{lti} system is said to be \gls{bibo} stable if for some $M,N \in \mathbb{R}^+$, the response to a bounded input $|u(t)| \le M$ results in a bounded output $|y(t)| \le N$.
\end{definition}

For a given control system, one way to analyze its stability is to plot the locus $z=F_o(s=j2\pi f)$ of the open loop frequency response
\begin{equation}
F_o(s) = G(s)P(s)H(s)
\end{equation}
from $f=0$ to $f=\infty$ and using the Nyquist stability criterion.
For the special case of a stable open loop $F_o(s)$ \footnote{The stability of $F_o(s)$ can often easily be determined from the block diagram.} the Nyquist stability criterion can be stated as (see \cite[p.~111]{Foellinger2016})
\begin{definition}\label{def:Nyquist}
(\textit{Nyquist stability criterion}) If the open loop $F_o(s)$ is stable, then the closed loop is stable if $z=F_o(s=j2\pi f)$ does not go through or encircles $z=(-1,0j)$.
\end{definition}

\newpage
\section{Metrics to Quantify the Stability of a Signal}\label{sec:metrics}
``Stability'' can have different meanings depending on the context. In case of signal processing, a signal is usually said to be \textit{stable} if it has only little variation around its mean or some target value, i.e. the mean has to be constant and the variance stays below some threshold.
Stability is not to be confused with stationarity, which requires the mean, the variance and the autocorrelation to stay constant over time. \cite{Guthrie2020}
To express stability as a single numerical value, there are several possibilities, some are described in the following.

\paragraph{Relative Standard Deviation}
This measures the stability as the standard deviation but related to the mean value to make it comparable to other quantities with different scaling or units.

The relative, or percentual, standard deviation of the stationary stoachastic process $X$ is defined using the mean $\mu_X$ and the standard deviation $\sigma_X$ as
\begin{equation}
\op{\%STD_X} := \frac{\sigma_X}{\mu_X}.
\end{equation}
In general, especially if $X$ is non-stationary (see \autoref{eq:wss}), $\op{\%STD_X}$ depends on the absolute time $t$ and the window size $T$ for which the process is assumed to be stationary:
\begin{equation}
\op{\%STD_X} = \op{\%STD_X}(t,T)
\end{equation}
In that case, for a fixed window size $T=T_0$, a mean percentual standard deviation can be computed with
\begin{equation}\label{eq:stdp}
\op{\%STD_X}(T=T_0) = \frac{1}{N} \sum_{n=0}^{N-1} \op{\%STD_X}(t_n,T_0).
\end{equation}
This assumes discrete time steps $t_n$, $n\in[0,N-1]$.

\paragraph{Mean Squared Error}
A similar measure to the percentual standard deviation is the \gls{mse}, if, instead of the mean $\mu_X$, a fixed target $x_t$ is used.

The mean squared error sums up the squared errors $\left(x[n] - x_t[n]\right)^2$ of $x[n]$ from a set value $x_t$. To remove the effect of the length of the data sequence, the sum is devided by the length of the sequence $N$:
\begin{equation}
\op{MSE}_x := \frac{1}{N} \sum_{n=0}^{N-1} \left(x[n] - x_t\right)^2
\end{equation}

\paragraph{Relative Power of Most Prominent Noise}
This novel approach compares the power of the most prominent noise power source $P_\text{noise, max}$ of the signal $x$ with the total power $P_x$:
\begin{equation}\label{eq:mpn}
\op{MPN}_x := \frac{P_\text{noise, max}}{P_x}
\end{equation}

Compared to the relative standard deviation $\op{\%STD}(t,T)$, this method has the advantage of being time-independent and keeping periodic noise structures:\\

\paragraph{Comparison}
To make $\op{\%STD}(t,T)$ independent of time, it could be tempting to choose $T=T_\text{total}$ with $T_\text{total}$ being the time span of the data set. But because of the averaging effect of the standard deviation estimator, this washes out the maxima and thus could lead to an under-estimation of $\op{\%STD}$, so an over-estimation of the stability.

The $\op{MSE}$ uses the whole data set, but due to normalizing maxima can also be hidden.

Periodogram-based method have the advantage that for a fixed target value $x_t$, they are very easy to interpret: All power, that is not in the $f=0$ frequency bin, is definitely noise. Integrating over these noise bins yields the error power comparable to the $\op{MSE}$.
The described $\op{MPN}$ metric has the advantage of showing the largest contributor to the noise and allows for an easy validation of a controllers success on this noise component.


\subsection{Functionals of probabilities}

\(\phi (P)\in \mathbb{R} \) is a functional on \(P(X)\).

Examples include the expectation and variance.

We can define derivatives on these functionals.

\(\phi (P)\approx \phi (P^0)+D_\phi (P-P^0)\)

Where \(D_\phi \) is linear.


\section{Dynamic Programming}
  \subsection{Dynamic Convex Hull Trick}
    \code{dp/dynamic-ch-trick.cpp}
  \subsection{Divide and Conquer Optimization}
    For DP problems of the form $$dp(i,j) = min_{k \leq j} \{dp(i-1,k)+C(k,j)\}$$ where $C(k,j)$ is some cost function.
    \code{dp/divide-conquer-opt.cpp}


\Lecture{Jayalal Sarma}{Oct 19, 2020}{18}{Introduction to Ramsey Numbers}{Shivlal Gangesh}{$\alpha$}{JS}
\section{Introduction}
Till now we have seen advanced versions of the discrete mathematics topics we already know. Now we are going to get into Extremal combinatorics. Here we are interested in questions of the form
\begin{itemize}
\item \textit{If this structure appears, then what is the minimum/maximum size of the object?}
\item \textit{If the size is at least this much , then what kind of structures appear in the object?}
\item \textit{What is the minimum size of the collection such that it is guaranteed to have certain property?}
\end{itemize}
In general we are interested in the extreme behaviors in combinatorics. The classic example we start with is an extension to an example that we have done in the beginning of the course as an Application of Pigeon Hole Principle.
\section{Starting Point} \label{R(3,3)}
\begin{theorem}
Six people meet in a party. Then either there exist three people who are friends with each other or there exist three people who are strangers with each other.(Note : Any two people can either be friends or strangers)
\end{theorem}
We are interested in proving the above statement. Lets look into two different approaches
\subsection{Model 1 (Using Cliques and Independent Sets)}
\begin{description}
   \item[Model] Let us represent the problem as a $6$ vertex graph $G(V,E) $with each person corresponding to a vertex. $(u,v) \in E$ if and only if person $u$ is a friend of person $v$.
In this Model the original statement can be reformulated as
\item[Statement]
\textit{Any graph on $6$ vertices must either have a clique on $3$ vertices or an independent set on $3$ vertices.
\item}
\begin{proof}
 Consider any vertex $v$ in the graph G, without loss of generality we can assume that the degree of $v$ is greater than or equal to $3$  because suppose it is not the case then consider $\overline{G}$ ; as $\textrm{Cliques in }G \leftrightarrow \textrm{Independent Sets in } \overline{G} $.\\
 Let the $3$ neighbours of $v$ be $a$, $b$ and $c$. Consider the two exhaustive cases :
 \begin{description}
    \item[Case 1 : There are no edges among $a$, $b$ and $c$]
    $ $ \newline
    Here we have $\{a, b, c\}$ as the 3-Independent Set
    \item[Case 2 : There is at least one edge among $a$, $b$ and $c$ ]
    $ $ \newline
    Let $(a,b) \in E$ be that edge, then we have $\{v, a, b\}$ as the 3-clique
 \end{description}
Therefore the given statement holds true.
\end{proof}
\item[Proof for tightness]
To prove that this is tight we need to show there is a graph with $5$ vertices such that it does not have 3-clique and 3-Independent Set. Given below is one such  example
\begin{figure}[h!]
    \centering
    \includegraphics[width=0.2\linewidth]{images/r33counter_example.png}
    \caption{5-vertex graph with no 3-clique and no 3-Independent Set}
\end{figure}
\end{description}
\subsection{Model 2 (Using Graph Edge colouring)}
\begin{description}
   \item[Model]
   Let us represent the problem as 2-edge coloring of a $K_6$ graph with each vertex corresponding to a person. Color the edge $(u,v)$ with \textit{red} if $u$ and $v$ are friends, color it with \textit{blue} if $u$ and $v$ are strangers.
In this Model the original statement can be reformulated as
\item[Statement]
\textit{For any 2-edge colouring of $K_6$, there must exist either  a red $K_3$  or a blue $K_3$ }
\item
\begin{proof}
 Consider any Red,Blue-edge coloring of $K_6$. Consider any vertex $v$, the degree of $v$ is $5$ as the graph is a complete graph. By Pigeon Hole Principle , $v$ must have either $3$ red edges incident on it or $3$ blue edges incident on it. Consider the case when $v$ is incident on with $3$ red edges. Let the 3 neighbours of $v$ be $a$, $b$ and $c$.  Now there are 2 cases :
 \begin{description}
    \item[Case 1 : There is no red colored edge among $(a,b)$, $(b,c)$ and $(c,a)$ ]
    $ $ \newline
    Then all the three edges $(a,b)$, $(b,c)$ and $(c,a)$ are colored blue. Therefore $\{a, b, c\}$ forms a blue $K_3$
    \item[Case 2 : There is at least one red colored edge among $(a,b)$, $(b,c)$ and $(c,a)$ ]
    $ $ \newline
    Let $(a,b)$ be the red colored edge, then $\{v, a, b\}$ forms a red $K_3$
 \end{description}
 Therefore the given statement holds true.
\end{proof}
\item[Proof for tightness]
To prove that this is tight we need to show there is a 2-edge coloring of $K_5$ Such that it does not have red $K_3$ and blue $K_3$. Given below is one such  example
\begin{figure}[h!]
    \centering
    \includegraphics[width=0.2\linewidth]{images/k5counter_example.png}
    \caption{2-edge coloring of $K_5$ with no red $K_3$ and no blue $K_3$}
\end{figure}
\end{description}

Generalizing the above problem with arbitrary red $k_p$ and blue $k_q$ has been extensively studied by Ramsey and has led to the definition of Ramsey numbers.
\section{Ramsey numbers}
\begin{definition}[Ramsey number]
The Ramsey number denoted by $R(p,q)$ is the minimum number of vertices say $n$ such that any 2-edge coloring of $K_n$ must have either a red $K_p$ or a blue $K_q$.\\
(\textbf{Or equivalently as})\\
The minimum number of vertices ($n$) such that any graph on $n$ vertices must either have a clique on $p$ vertices or an independent set on $q$ vertices.
\end{definition}

\subsection{Some Observations}
\begin{property}
$R(3,3)=6$
\end{property}
This is the direct formulation of the example we have done previously in \ref{R(3,3)}
\begin{property}
$R(p,q) = R(q,p)$
\end{property}
The colors \textit{red} and \textit{blue} are just placeholders for two colors, thus swapping the colors will still preserve the Ramsey number property. Therefore $R(p,q) = R(q,p)$.
\begin{property}
$\forall l \geq 1 \quad R(l,1) = 1$
\end{property}
The existence of a blue $K_1$ is nothing but the presence of single vertex and any graph with a single vertex satisfies this property. Therefore $R(l,1) = 1$

\section{Existence of R(p,q)}
The Proof for the existence of $R(p,q)$ is due to Erdős–Szekeres. The existence was proved by providing an upper bound as a recurrence relation as follows :
\begin{theorem}
$$\forall p,q \geq 2 \quad R(p,q) \; \leq \; R(p,q-1) + R(p-1,q) $$
\end{theorem}
\begin{proof}
 Let us prove this by mathematical induction on $n$ where $n=p+q$.
 \begin{description}
    \item[Idea] To show the upper bound for $R(p,q) \leq n$ , we must argue that for any 2-edge coloring of $K_n$ there exist a red $K_p$ or blue $K_q$

   \item[Base case] $p=q=2$
$$R(2,2) \leq  R(2,1) + R(1,2)  $$
$$2 \leq 1+1$$
Hence it holds true for the base case.
   \item[Induction Hypothesis]
Assume the recurrence relation is true for $n<l$. Then we need to prove it for $n=l$. Let $n=R(p-1,q)+R(p,q-1)$. Let $w$ be any vertex in $G$ ($K_n$) and consider any $2$ -edge coloring of $G$. Let $H_1$ be the subgraph of $G$ formed from the vertices sharing a red-edge with $v$ and $H_2$ be the the subgraph of $G$ formed from the vertices sharing a blue-edge with $v$.
\begin{description}
   \item[Case 1 : There are at least $R(p-1,q)$ many red edges incident on vertex $w$]
   $ $ \newline
   $H_1$ is a complete graph on $R(p-1,q)$ vertices with 2-edge coloring. By definition and Induction Hypothesis we have that there exist a red $K_{p-1}$ or blue $K_q$ in $H_1$. So in graph $G$ (along with vertex $w$) there exist a red $K_p$ or blue $K_q$
   \item[Case 2 : There are at least $R(p,q-1)$ blue edges incident on vertex $w$]
      $ $ \newline
      $H_2$ is a complete graph on $R(p,q-1)$ vertices with 2-edge coloring. By definition and Induction Hypothesis we have that there exist a red $K_p$ or blue  $K_{q-1}$ in $H_2$. So in graph $G$ (along with vertex $w$) there exist a red $K_p$ or blue $K_q$.
\end{description}
   \end{description}

\end{proof}


\section{Sample algorithm}
\begin{algorithm}
	\caption{Kruskal's Algorithm}\label{alg:kruskal}
	\begin{algorithmic}[1]
		\Procedure{MakeSet}{$v$}
			\State Create new set containing $v$
		\EndProcedure \\

		\Function{FindSet}{$v$}
			\State\Return a set containing $v$
		\EndFunction \\

		\Procedure{Union}{$u$,$v$}
			\State Unites the set that contain $u$ and $v$ into a new set
		\EndProcedure \\

		\Function{Kruskal}{$V,E,w$}
			\State $A \gets \{\}$
			\For {each vertex $v$ in $V$}
				\State MakeSet($v$)
			\EndFor
			\State Arrange $E$ in increasing costs, ordered by $w$
			\For {each ($u$,$v$) taken from the sorted list}
				\If {FindSet($u$) $\neq$ FindSet($v$)}
					\State $A \gets A \cup \{(u,v)\}$
					\State Union($u,v$)
				\EndIf
			\EndFor
			\State \Return A
		\EndFunction
	\end{algorithmic}
\end{algorithm}

\newpage
\section{Sample Python code}
\begin{lstlisting}[language=Python]
import numpy as np

def incmatrix(genl1,genl2):
	m = len(genl1)
	n = len(genl2)
	M = None #to become the incidence matrix
	VT = np.zeros((n*m,1), int)  #dummy variable

	#compute the bitwise xor matrix
	M1 = bitxormatrix(genl1)
	M2 = np.triu(bitxormatrix(genl2),1)

	for i in range(m-1):
		for j in range(i+1, m):
			[r,c] = np.where(M2 == M1[i,j])
			for k in range(len(r)):
				VT[(i)*n + r[k]] = 1;
				VT[(i)*n + c[k]] = 1;
				VT[(j)*n + r[k]] = 1;
				VT[(j)*n + c[k]] = 1;

	if M is None:
		M = np.copy(VT)
	else:
		M = np.concatenate((M, VT), 1)

	VT = np.zeros((n*m,1), int)

	return M
\end{lstlisting}

\newpage
\section{Sample Matlab code}
\lstinputlisting[language=Octave]{codes/sample-code.m}


\subsection{Method of Undetermined Coefficients}
\noindent
The rules for undetermined coefficients are very similar for systems and single equations. Let's say we have a heterogeneous system of the form
\begin{equation*}
	\begin{cases}
		x_1' = a_{11}x_1 + \ldots + a_{1n}x_n + f_1(t) \\
		\vdots \\
		x_n' = a_{n1}x_n + \ldots + a_{nn}x_n + f_n(t)
	\end{cases}
\end{equation*}
written in a matrix form,
\begin{equation*}
	\begin{bmatrix}
		x_1' \\
		\vdots \\
		x_n'
	\end{bmatrix} = \begin{bmatrix}
		a_{11} & \ldots & a_{1n} \\
		\vdots & \ddots & \vdots \\
		a_{n1} & \ldots & a_{nn}
	\end{bmatrix} \begin{bmatrix}
		x_1 \\
		\vdots \\
		x_n
	\end{bmatrix} + \begin{bmatrix}
		f_1(t) \\
		\vdots \\
		f_n(t)
	\end{bmatrix}
\end{equation*}
or more compactly as
\begin{equation*}
	\vec{x}' = A\vec{x} + \vec{f}
\end{equation*}
All we need to do is look at each $f_i(t)$ and write in the $i^{th}$ blank the corresponding guess. This is like doing undetermined coefficients on each equation in the system.\\

\noindent
This is especially nice when all the $f_i(t)$'s have a similar form because we can write our guess as
\begin{equation*}
	\vec{g}(t) = f_c(t)\vec{v}
\end{equation*}
where $f_c(t)$ is the guess corresponding to $f$ and $\vec{c}$ is a vector of undetermined scalars.

\ifodd\includeLinearSystemsExamples\input{./linearSystems/heterogeneousSystems/undeterminedCoefficients_example.tex}\fi


\chapter{Conditional Logic and Loops}
Being able to use conditional logic and applying that logic to control the flow of your programs is an important part of programs. If there's something that computers are really good at doing, it's doing things procedurally, and loops and conditional logic are the first step to this procedural approach to computing.\par
\section{Comparison Operators}
In order to understand how to use conditional logic, we need to first understand how to test conditions. The very basics of condition testing is understanding how to use comparison operators. You've used comparison operators before in math. You know, the $=$, $<$, $>$, $\leq$, $\geq$ and $\neq$ symbols?\par
The symbols in computer science are fairly standard, but they're different than mathematical symbols. For one, the equal sign \verb|=| is already reserved for the assignment operator, and for two, we don't even have half of these keys on our keyboards! Instead, programmers use programming comparison operators. The mathematical equivalents and descriptions are provided below.\par
\vspace{5mm}
\begin{tabular}{|l|l|l|}
\hline
Operator  & Math Equivalent & Description              \\
\hline
\verb|==| & $=$             & Equal to                 \\
\hline
\verb|!=| & $\neq$          & Not equal to             \\
\hline
\verb|<|  & $<$             & Less than                \\
\hline
\verb|>|  & $>$             & Greater than             \\
\hline
\verb|<=| & $\leq$          & Less than or equal to    \\
\hline
\verb|>=| & $\geq$          & Greater than or equal to \\
\hline
\end{tabular}\par
\vspace{5mm}
We can use these symbols to test Boolean expressions in our conditional logic statements.\par
\warningtext{Variables Versus Expressions}{Do not confuse Boolean variables with Boolean expressions. They are not the same thing. A Boolean variable holds a value of type Boolean. A Boolean expression tests a condition and evaluates to true or false.}
Using our comparison operators, we can test conditions in Python. Before we get to the actual \verb|if| statements, it's important that we understand how to use these comparison operators and how they evaluate.\par
In Python, we typically use comparison operators to compare either two variables, a variable and a literal, a function result and a literal, or a function result and a variable. Let's start with the most basic (but slightly contrived) example: comparing two literals. Consider the following Boolean expression.\par
\begin{lstlisting}[style=pippython]
4 > 3
\end{lstlisting}
This expression can either evaluate to \verb|True| or \verb|False|. To manually evaluate how the Boolean expression will evaluate, we can ask the question: is this statement correct? Only consider whether the full statement is correct, not a part of the statement.\par
In the above example, we can ask the question: is 4 greater than 3? Yes, it is! Therefore, the expression will evaluate to \verb|True|.\par
Here's another example.
\begin{lstlisting}[style=pippython]
5 != 6
\end{lstlisting}
We know that the \verb|!=| is the not-equal-to comparison operator, so our question should be: is 5 not equal to 6? Yes, so the expression will evaluate to \verb|True|. Here's one that might be confusing.\par
\begin{lstlisting}[style=pippython]
5 != 5
\end{lstlisting}
Again, we're using the not-equal-to comparison operator, so the question should be: is 5 not equal to 5? No, so the expression will evaluate to \verb|False|.\par
If you want to test multiple things at once, you can also do that in Python. There are two main ways to test multiple things in programming: "and" and "or". Using "and" requires that all of the Boolean expressions evaluate to \verb|True| in order for the entire compound Boolean expression to evaluate to \verb|True|. The "or" only requires that one of the Boolean expressions evaluates to \verb|True| in order for the entire compound Boolean expression to evaluate to \verb|True|.\par
In Python, we can denote the "and" by writing \verb|and|. Similarly, we can denote the "or" by writing \verb|or|. When writing a compound Boolean expression, you can stack multiple single Boolean expressions on top of each other, as shown here.\footnote{Python is relatively special in that it uses the keywords "and" and "or". Most other languages use the symbols \&\& (two ampersands) to mean "and" and $\vert \vert$ (two vertical pipes) to mean "or."}\par
\begin{lstlisting}[style=pippython]
5 != 6 and 6 != 7
\end{lstlisting}
The statement shown would evaluate to \verb|True|, since both of the single Boolean expressions would evaluate to \verb|True|: yes, 5 is not equal to 6, and 6 is not equal to 7. You can also use more than two Boolean expressions.\par
\begin{lstlisting}[style=pippython]
5 != 6 and 7 != 7 and 8 == 8
\end{lstlisting}
In this case, the compound Boolean expression would evaluate to False, since the second condition \verb|7 != 7| evaluates to \verb|False|, so the entire expression evaluates to \verb|False|.\par
It is also possible to combine \verb|and| and \verb#or#. Consider the following expression.
\begin{lstlisting}[style=pippython]
5 != 6 and 7 != 7 or 5 != 6 and 2 != 3
\end{lstlisting}
If we evaluate the first half of the compound Boolean expression, we can see that this would evaluate to \verb|False|. However, the second half of the Boolean expression would evaluate to \verb|True|, since both of the conditions are \verb|True|. The two sides of the expression are separated by a \verb#or#, so only one side needs to evaluate to \verb|True| for the entire thing to evaluate to \verb|True|. Essentially, when you use \verb|and|, only one condition needs to be false for the whole thing to evaluate to \verb|False|. When you use \verb#or#, only one condition needs to be true for the whole thing to evaluate to \verb|True|.\par
Note that some languages have an additional operator called the strict equality operator, represented by three equal signs \verb|===|. In a strict equality, not only must the values that are being compared match, but so must the datatype.\par
In JavaScript, another programming language that has the strict equality operator, you can see how the strict equality can change the result of the Boolean expression.\footnote{In Javascript, double forward-slashes indicate a comment, not integer division.}
\begin{lstlisting}
0 == '0'  // TRUE
0 === '0'  // FALSE
0 == ''  // TRUE
0 === '0' // FALSE
\end{lstlisting}
Because the datatypes don't match exactly (the first item is an integer, the second is a string), JavaScript is evaluating a strict equality to be false, even though loose equalities might evaluate to true (in the first line, JavaScript is actually typecasting the string into an integer in order to test it).\par
While this seems useful, Python does not support the strict equality. The closest thing to this in Python is a combination of Boolean expression tests:
\begin{lstlisting}[style=pippython]
a == b and type(a) == type(b)
\end{lstlisting}
\subsubsection*{Exercise Questions}
These exercise questions cover chapter 6.1.
\begin{Exercise}
    \Question{What is a comparison operator?}
    \Question{What can a Boolean expression evaluate to?}
    \Question{List the six comparison operators in Python and what they indicate.}
\end{Exercise}
\begin{Exercise}
What do the following Boolean expressions equate to in Python?
    \Question{\verb|4 > 3|}
    \Question{\verb|4 >= 4|}
    \Question{\verb|4 > 4|}
    \Question{\verb|"Python" == "Is Awesome"|}
    \Question{\verb|4 < 3|}
    \Question{\verb|"4" == 4|}
    \Question{\verb|int("4") == 4|}
    \Question{\verb|4 != 4|}
    \Question{\verb|4 != 5|}
\end{Exercise}
\begin{Exercise}
Write the following Boolean expressions with the correct Python syntax.
    \Question{4 is equal to 8}
    \Question{4 is not equal to 9}
    \Question{The variable \verb|k| is less than 3 or greater than or equal to 5}
    \Question{The variable \verb|m| is greater than 3 and less than 20}
    \Question{The variable \verb|p| is greater than 3 or less than 0 but is not exactly 1}
\end{Exercise}
\section{If, Else, and Else If Statements}
It's one thing to just run lines of code sequentially, but one of the biggest strengths of computers is being able to make decisions based on empirical data. Using conditional logic and Boolean expressions, computers can run different pieces of code depending on whether some condition is met or not. This is the basis of the \verb|if|, \verb|else|, and \verb|elif| (else if) tools in Python.\par
The most primitive type of conditional logic is the humble \verb|if| statement. The \verb|if| statement allows you to test a condition and execute a chunk of code based on the outcome of that test. Being comfortable with how \verb|if| statements work will make learning how to use loops much easier, as the control mechanism in loops is essentially another \verb|if| statement.\par
Recall how we used indentation in chapter 4.5.3. In that chapter, we used colons and indentation to indicate which portions of code were a part of which block. The same structure is used for all of the conditional logic and loop blocks, too. Let's take a look at some code.\par
\begin{lstlisting}[style=pippython]
a = 5
b = 7
if (a < b):
  print ("a is less than b")
\end{lstlisting}
\begin{lstlisting}
a is less than b
\end{lstlisting}
Let's break down this chunk of code. In the first two lines, we're defining and initializing two integer variables, \verb|a| and \verb|b|. Then, we're creating an \verb|if| statement. We can read this \verb|if| statement as meaning "if \verb|a| is less than \verb|b|, then execute this chunk of code." In an \verb|if| statement, Python will evaluate the Boolean expression inside of the parentheses. If the Boolean expression evaluates to \verb|True|, then the code inside of the \verb|if| statement is run. If the Boolean expression evaluates to \verb|False|, then the code inside of the \verb|if| statement will not run at all. Instead, Python will skip the contents of the \verb|if| statement and resume running the code that is no longer indented.\par
We can see how this works here.\par
\begin{lstlisting}[style=pippython]
c = 7
d = 6
if (c < d):
  print ("c is less than d")
print ("This is outside of the if statement")
\end{lstlisting}
\begin{lstlisting}
This is outside of the if statement
\end{lstlisting}
How can we tell that the code outside of the \verb|if| statement will run? Notice how the second print statement (\verb|print("This is|...) is not indented at the same level as the first print statement (\verb|print("a is less|...).\par
If the Boolean expression evaluates to \verb|True|, then the code inside of the \verb|if| statement will run, then Python will resume any code outside of the \verb|if| statement. Take a look at the following code.\par
\begin{lstlisting}[style=pippython]
a = 5
b = 7
if (a < b):
  print ("a is less than b")
print ("This is outside of the if statement")
\end{lstlisting}
\begin{lstlisting}
a is less than b
This is outside of the if statement
\end{lstlisting}
The \verb|if| statement can only dictate whether the code inside of the statement can run. It cannot affect anything outside of the \verb|if| statement.\par
We can also tell Python to test a condition, then run a certain chunk of code if the condition evaluates to \verb|True| or another chunk of code if the condition evaluates to \verb|False|. In Python, we do this using the \verb|if| and \verb|else| statements. As their names suggest, \verb|if| will test the condition, and like we've seen, we must provide it with a Boolean expression to test. The \verb|else| statement will handle the code that's run if the \verb|if| statement evaluates to \verb|False|. Unlike the \verb|if| statement, the \verb|else| statement cannot take anything else. Let's take a look at some code.\par
\begin{lstlisting}[style=pippython]
a = 5
b = 7
if (a < b):
  print ("a is less than b")
else:
  print ("b is less than a")
\end{lstlisting}
\begin{lstlisting}
a is less than b
\end{lstlisting}
\begin{lstlisting}[style=pippython]
c = 7
d = 6
if (c < d):
  print ("c is less than d")
else:
  print ("d is less than c")
\end{lstlisting}
\begin{lstlisting}
d is less than c
\end{lstlisting}
In the first example, the Boolean expression in the \verb|if| statement evaluates to \verb|True|, so the code inside of the \verb|if| statement is executed. The code inside of the \verb|else| statement is ignored. Similarly, in the second example, the Boolean expression in the \verb|if| statement evaluates to \verb|False|, so the code inside of the \verb|if| statement is ignored and the code inside of the \verb|else| statement is executed.\par
But, what if we want to test more than just one condition with only two possible execution cases? Python has one additional tool for this exact case: the \verb|elif| statement. \verb|elif| is short for "else if," and it can go in between the \verb|if| statement and the \verb|else| statement. Like the \verb|if| statement, you need to provide a Boolean expression to be evaluated as part of the \verb|elif|. Take a look at the following chunk of code.\par
\begin{lstlisting}[style=pippython]
x = 3
y = 2
z = 5
if (z < x and z < y):
  print ("z is the smallest")
elif (y < x and y < z):
  print ("y is the smallest")
else:
  print ("x is the smallest")
\end{lstlisting}
\begin{lstlisting}
y is the smallest
\end{lstlisting}
This code looks complicated, but let's break it down. First, we're declaring three variables, \verb|x|, \verb|y|, and \verb|z|. Each of these variables is being initialized to a unique integer value. Next, we're testing if \verb|z| is the smallest of the three variables by comparing it to both \verb|x| and \verb|y|. If it is the smallest, then we can print that it's the smallest and exit the group of \verb|if|/\verb|else| statements. If it's not, then we can test a second condition: if \verb|y| is the smallest. If it is, then we can print that it's the smallest and exit the group of \verb|if|/\verb|else| statements. Otherwise, \verb|x| must be the smallest variable.\par
If we wanted to test more than just two, we can add multiple \verb|elif| statements. In fact, we can use as many \verb|elif| statements as we need to test all of the conditions that we need. Take a look at the following code, which works similarly to the previous example, but compares four numbers.\par
\begin{lstlisting}[style=pippython]
x = 3
y = 2
a = 1
z = 5
if (z < x and z < y and z < a):
  print ("z is the smallest")
elif (y < x and y < a and y < z):
  print ("y is the smallest")
elif (a < x and a < y and a < z):
  print ("a is the smallest")
else:
  print ("z is the smallest")
\end{lstlisting}
\begin{lstlisting}
a is the smallest
\end{lstlisting}
In this example, we're using two \verb|elif| statements inside of our \verb|if|/\verb|else| statement group. Because these \verb|elif| statements are written in between the \verb|if| and \verb|else| statements, they will be evaluated within this \verb|if|/\verb|else| statement group. In fact, it is syntactically illegal to have an \verb|elif| statement outside of the \verb|if|/\verb|else| statement group. The following code is not syntactically valid.\par
\begin{lstlisting}[style=pippython]
# WRONG
x = 3
y = 2
z = 5
if (z < x and z < y):
  print ("z is the smallest")
else:
  print ("x is the smallest")
elif (y < x and y < z):
  print ("y is the smallest")
\end{lstlisting}
\begin{lstlisting}
SyntaxError: invalid syntax
\end{lstlisting}
Remember that within your \verb|if|/\verb|else| statement group, the \verb|else| statement must be the last thing that's introduced into the group. You can think of it as the "catch all" in the group. You can have as many \verb|elif| statements as you need, but your \verb|else| statement must be the last thing.\par
We can also test whether an element is present inside of a complex datatype as covered in Chapter 5. We can directly test whether an element is in a list, tuple, or set.\par
\boxtext{Math Minded?}{Mathematically, we can represent this using the set membership symbol $\in$. If we were to ask whether $i$ is in the set {2, 3, 4}, we could represent this as $i \in {2, 3, 4}$ mathematically or "i in {2, 3, 4}" in Python.}
\subsubsection*{Exercise Questions}
These exercise questions cover chapter 6.2.
\begin{Exercise}
    \Question{What are the three new keywords discussed in this section?}
    \Question{How does Python tell what is inside of a block?}
    \Question{What does Python use to determine whether a block should be run?}
    \Question{Where must an \verb|elif| block be placed?}
\end{Exercise}
\begin{Exercise}
    \Question{Create an \verb|if| statement that executes the code inside of it if the \verb|numDogs| variable is greater  than or equal to 5.}
    \Question{Create an \verb|if| statement that executes the code inside of it if the \verb|cats| variable is exactly equivalent to the string \verb|"orange tabby"|.}
    \Question{Create an \verb|if| statement that executes the code inside of it if the \verb|isDone| variable is exactly equivalent to \verb|False|.}
\end{Exercise}
\begin{Exercise}
For this exercise, consider the \verb|if| statement that tests whether the \verb|numDogs| variable is greater than or equal to 5.
    \Question{Inside of the initial \verb|if| statement, create a print statement that outputs the following string: \verb|We have more than five dogs!|}
    \Question{Create an \verb|elif| statement that tests whether the \verb|numDogs| variable is less than or equal to 3. Inside of this \verb|elif| statement, create a print statement that outputs the following string: \verb|We have less than three dogs.|.}
    \Question{Create a final \verb|else| statement that outputs the following string and explain why this would execute in context of the other \verb|if| and \verb|elif| statements: \verb|We have exactly four dogs.|.}
\end{Exercise}
\begin{Exercise}
For this exercise, consider the \verb|if| statement that tests whether the \verb|cats| variable is exactly equivalent to the string \verb|"orange tabby"|.
    \Question{Inside of the initial \verb|if| statement, create a print statement that outputs the following string: \verb|The tabby cat is orange!|.}
    \Question{Create an \verb|elif| statement that tests whether the \verb|cats| variable is exactly equivalent to the string \verb|"grey tabby"| and if so, outputs the following string: \verb|The tabby cat is grey!|.}
    \Question{Create an \verb|elif| statement that tests whether the \verb|cats| variable is exactly equivalent to the string \verb|"brown tabby"| and if so, outputs the following string: \verb|The tabby cat is brown!|.}
    \Question{Create a final \verb|else| statement that outputs the following string and explains why this would execute in context of the other \verb|if| and \verb|elif| statements: \verb|"The tabby cat is something else"|.}
\end{Exercise}
\begin{Exercise}
Write a short script that asks the user to input a number from 1 to 100. Test whether that number is between 1 and 100, then tell the user whether the number is greater than 50, or less than or equal to 50.
\end{Exercise}
\begin{Exercise}
Cast your mind back to when we covered arithmetic. Write the Boolean expressions to evaluate the following. Write some code to prove your Boolean expressions, and for each, test the following values for \verb|i|, \verb|j|, and \verb|k|: 39, 43, 44, 50.
    \Question{Whether the variable \verb|i| is divisible by 5.}
    \Question{Whether the variable \verb|j| is a multiple of 4.}
    \Question{Whether the variable \verb|k| is a divisor of 3.}
\end{Exercise}
\section{For Loops}
Another important component of programming procedurally is the \verb|for| loop. A \verb|for| loop allows you execute a chunk of code a defined number of times.\par
A \verb|for| loop will execute exactly as many times as you specify it should run. \verb|for| loops operate through iteration, typically through numbers, though they can also iterate through complex data structures, like lists or dictionaries. Whether you pass in an integer literal or a variable, the principle between a \verb|for| loop is the same. When writing a \verb|for| loop, you need to provide three things: a starting point, an ending point, and the amount to iterate by for each run of the loop.\par
The simplest way to get started with \verb|for| loops is with integer literal values. In Python, we need to introduce another function: the \verb|range()| function. \verb|range()| can take one, two, or three arguments, and each will provide additional functionality; it returns a list of numbers.\par
Let's take a look at what \verb|range()| can produce. If we pass it only one argument, Python will assume that the list should start at 0 and be as long as you specified in that argument. That is, the list will span in intervals of 1 from $[0, n-1]$. Remember that counting starts at zero in Python!\par
\begin{lstlisting}[style=pippython]
range(5)
\end{lstlisting}
This range function would produce the list \verb|[0, 1, 2, 3, 4]|. If we pass in two arguments, then we can specify the beginning and the end of the list. Counting will begin exactly at the first number and end at one less than the second number that you specify, just like if you had only specified one argument, except if the first argument was 0. In fact, when you only pass one argument, Python assumes that the first argument \textit{is} 0! Take a look at the following code.\par
\begin{lstlisting}[style=pippython]
range(2, 6)
\end{lstlisting}
This range function would produce the list \verb|[2, 3, 4, 5]|. Notice how there are as many elements in the array as $b - a$, where $b$ is the second argument and $a$ is the first element.\par
If you do not provide a third argument, the second argument must be larger than the first argument, since the iteration direction goes up. But, what if we wanted to produce a list that counted down? Well, we could provide a third argument, the iterator. The iterator will tell Python how large the steps should be and in which direction the steps should go while generating the list. The third argument can be a positive or negative integer.\par
\begin{lstlisting}[style=pippython]
range(2, 6, 2)
\end{lstlisting}
This range function would produce the list \verb|[2, 4]|. Notice how the range of the list is the exact same as in the previous range example with two arguments. However, since we've specified that the third argument as 2, Python will only use every other element from the previous array. If we had specified the third element as 3, Python would only use every third element from the previous array.\par
If your third argument is positive, then your second element must be greater than your first element. If your third argument is negative, then your second element must be less than your first element. Consider the following code and the lists that they would produce.\par
\begin{lstlisting}[style=pippython]
range(6, 2, -1)
\end{lstlisting}
This would produce the list \verb|[6, 5, 4, 3]|.\par
\begin{lstlisting}[style=pippython]
range(6, 2, -2)
\end{lstlisting}
This would produce the list \verb|[6, 4]|.\par
Now that we can use the \verb|range()| function, we can apply it to our \verb|for| loops. \verb|for| loops always contain two keywords (\verb|for| and \verb|in| and two additional components (an iterator variable and something to iterate through). The general form of a \verb|for| loop is as follows:
\begin{lstlisting}
for variable in list:
  # code to be run
\end{lstlisting}
Let's break down this \verb|for| loop. The first thing in this statement is the keyword: \verb|for| lets Python know that we want to use a for loop. The next thing is a variable declaration. We need this variable to keep track of how many times we've run the loop. For this, just name the variable name that you want to use. Python will declare a new variable named whatever you specified, so it must follow all of our variable naming rules. The type of the variable depends on the last element inside of your loop definition, which we'll get to later. Next is another keyword that lets us know that the variable that we just declared will be iterating inside of something else. The last thing is the list that we'll be iterating through. You can also use the \verb|variable| inside of the loop. Take a look at the following loop:\par
\begin{lstlisting}[style=pippython]
for i in range(5):
  print(i)
\end{lstlisting}
\begin{lstlisting}
0
1
2
3
4
\end{lstlisting}
In this loop, we can see that we're using \verb|i| as the variable that we're using to iterate through this loop. We can then generate a list using our \verb|range()|, and according to the \verb|range()| function documentation, we know that the list that we're using is \verb|[0, 1, 2, 3, 4]|. Since we're using the \verb|range()| function, the variable type of \verb|i| (our iterator variable) will be an \verb|int|. Lastly, inside of the for loop, we are printing the iterator variable with each iteration of the loop.\par
This is the most common way to run a loop a specified number of times. This is really useful if you know exactly how many times you'll need to run a loop. For example, say you're writing a program that calculates the average of four numerical grades from 0 to 100. While you could write four \verb|input()| statements that put the values that the user inputs into a specific variable, we could also use a \verb|for| loop. This would allow us to put the value that the user inputs into a list of our naming. We could do this as shown in the following sample code.\par
\begin{lstlisting}[style=pippython]
grades = [0, 0, 0, 0] # declare a new variable of type list
for i in range(4):
  grades[i] = int(
      input("Input grade number " + str(i + 1) + " of 4: ")
      )
average = (grades[0] + grades[1] + grades[2] + grades[3]) / 4
print("Average is ", str(average))
\end{lstlisting}
Again, we can break this code down line-by-line. In line 1 (\verb|grades = [0|...), we're declaring a new variable as a list with four integers in it. Next, we're creating a \verb|for| loop that will iterate through another list of integers \verb|[0, 1, 2, 3]|, which has been created by our \verb|range()| function. The current loop iteration number is stored in the variable \verb|i|, which is an integer, as decided by the list from the \verb|range()| function. Reminder: the iteration variable (like \verb|i|) will take on whatever datatype the iteration list is. Inside of our \verb|for| loop, we're using the \verb|input()| function to take in four scores. We know that the \verb|input()| function will always return a string, so we're also \indexterm{typecast}ing the value of the user input into an integer before storing it in the \verb|grades| list. You'll also notice in the \verb|input()| function that we're asking the user for score number \verb|i + 1|, instead of \verb|i|. Again, remember that indices in Python start at 0, not 1. If we asked for \verb|i|, we'd be asking the user for score 0, 1, 2, and 3, instead of the more reasonable request of 1, 2, 3, and 4. Finally, we're using some arithmetic to calculate the average of the scores that our user gave us, storing it in the new variable \verb|average|, and printing \verb|average|. Because our division operation would almost certainly cause us to end up with a decimal, \verb|average| will probably be a float. If the average just happens to be a whole number, then Python will use the integer datatype. \par
So far, we've only used the \verb|range()| function to produce a list of integers that would be iterated over in our \verb|for| loop. However, notice how we've always referred to the result of the \verb|range()| function as a \textit{list}. So, can we also just pass in a list as our iterator for a \verb|for| loop? Yes, we can! Python allows us to iterate through a list, whether we use a list literal or a variable that contains a list. We also mentioned above that the iterator variable would take on the datatype of the iteration list, and here's where we can put that to use.\par
Let's try to create two \verb|for| loops, one using the \verb|range()| function and one with a list literal filled with integers.\par
\begin{lstlisting}[style=pippython]
for i in range(4):
  print (i)
\end{lstlisting}
\begin{lstlisting}[style=none]
0
1
2
3
\end{lstlisting}
\begin{lstlisting}[style=pippython]
for i in [0, 1, 2, 3]:
  print (i)
\end{lstlisting}
\begin{lstlisting}[style=none]
0
1
2
3
\end{lstlisting}
If it wasn't clear what \verb|range()| was doing before, it should be clear now: \verb|range()| is creating a list, and it's a shortcut to writing out each element that we want to print. This is really useful if you need to iterate a bunch of times (100, 1000, or even more!). \par
However, we're not limited to passing in integers in our lists. We can also pass in other datatypes, including floats and strings.\par
\begin{lstlisting}[style=pippython]
for i in ["Ronaldo", "Messi", "Neymar"]:
  print (i)
\end{lstlisting}
\begin{lstlisting}
Ronaldo
Messi
Neymar
\end{lstlisting}
\begin{lstlisting}[style=pippython]
for i in [1.34, 2.71, 3.14]:
  print (i)
\end{lstlisting}
\begin{lstlisting}
1.34
2.71
3.14
\end{lstlisting}
In the first example, we're iterating through three strings, and in the second example, we're iterating through three floats. Both of these are being passed in as list literals, but there's nothing preventing us from passing in a variable with a list in it.\par
\begin{lstlisting}[style=pippython]
cities = ["New York", "Atlanta", "Columbus"]
for i in cities:
  print (i)
\end{lstlisting}
\begin{lstlisting}
New York
Atlanta
Columbus
\end{lstlisting}
When iterating through an explicitly defined list (whether it's a list literal or a variable with a list), Python will always loop through the list according to the index number in ascending order. It won't sort by alphabetical order or string length, and you'll need to do some extra computing if you want your Python script to do such.
\subsubsection*{Exercise Questions}
These exercise questions cover chapter 6.3.
\begin{Exercise}
Consider the following for loop declarations. Are they syntactically correct? Why or why not?
    \Question{\verb|for i in range(5):|}
    \Question{\verb|for i in range(1, 5):|}
    \Question{\verb|for i in range(1, 6, 2):|}
    \Question{\verb|for i in 5:|}
    \Question{\verb|for (i in range(5)):|}
    \Question{\verb|for (i in 5):|}
    \Question{\verb|for i in [1, 3, 4, 6]|}
    \Question{\verb|for i in [1, 2, 3, 4]|}
    \Question{\verb|for i in ["1", "2", "3", "4"]|}
    \Question{\verb|for i in ["apple", "strawberry", "banana"]|}
    \Question{\verb|for i in fruits| where \verb|fruits| is a variable with the list of strings: \verb|["apple", "strawberry", "banana"]|}
\end{Exercise}
\begin{Exercise}
    \Question{Using exactly two lines of code, write a loop that prints the following lines:\\\verb|pear|\\\verb|papaya|\\\verb|pomelo|\\\verb|grape|}
    \Question{Using exactly two lines of code, print the numbers from 1 to 100.}
    \Question{Using exactly two lines of code and the variable \verb|k| as the iterator in your loop, print $a$ asterisks, with each set of $k$ asterisks on their own line, with the shortest line being just one asterisk and the longest line being ten asterisks. It should look like this:\\\verb|*|\\\verb|**|\\\verb|***|\\\verb|****|\\\verb|*****|\\\verb|******|\\\verb|*******|\\\verb|********|\\\verb|*********|\\\verb|**********|}
\end{Exercise}
\section{While Loops}
The last major loop structure is the \verb|while| loop. Compared to a \verb|for| loop, a \verb|while| loop is much more simple: it tests a condition and executes the code inside of that statement if the test is true and skips the code if not. \verb|for| loops are really great at doing something a specified number of times, but \verb|while| loops are really good at doing something either something a specified number of times or an indeterminate number of times. Let's look at both of these.\par
The easiest way to look at a \verb|while| loop is as a different kind of iterative loop. However, while a \verb|for| loop iterates using a list, \verb|while| loops iterate using a condition. For example, we could set a variable \verb|run| to start at 0, and each time the loop runs, the value of \verb|run| is increased by 1. Then, we could set the condition of our \verb|while| loop to be while \verb|run| is less than 100. Let's take a look at this code in practice.\par
\begin{lstlisting}[style=pippython]
run = 0
while (run < 100):
  print(run)
  run = run + 1
\end{lstlisting}
\begin{lstlisting}
0
1
2
...
97
98
99
\end{lstlisting}
As you can see, this code runs exactly 100 times, starting at 0 and ending at 99. Once \verb|run| is equal to 100, the condition is no longer met, so the code inside of the \verb|while| loop is no longer run. If we wanted to print a different range of numbers, we can just change the \verb|run| variable initialization and the condition of our \verb|while| loop. If we wanted to print from 1 to 100 instead of 0 to 99, we could run the following code instead.\par
\begin{lstlisting}[style=pippython]
run = 1
while (run < 101):
  print(run)
  run = run + 1
\end{lstlisting}
\begin{lstlisting}
1
2
3
...
98
99
100
\end{lstlisting}
Similarly, if we wanted to change how much \verb|run| increased with each iteration of our \verb|while| loop, we could change the last line in the while loop.\par
\begin{lstlisting}[style=pippython]
run = 0
while (run < 101):
  print(run)
  run = run + 2
\end{lstlisting}
\begin{lstlisting}
0
2
4
...
96
98
100
\end{lstlisting}
In each iteration of our \verb|while| loop, we're increasing the value of \verb|run| by 2 instead of 1, which ends up giving us all of the even numbers.\par
As you can see, to change the value of \verb|run|, we've been writing that the value of the variable should be the value of the variable itself plus some other number: \verb|run = run + n|, where \verb|n| is the number to increase by. Since increasing (or decreasing) the value of a variable by 1 is such a common practice across so many different applications, Python actually has a built-in mechanism for this exact purpose.\par
\begin{lstlisting}[style=pippython]
run = 0
while (run < 100):
  print(run)
  run += 1
\end{lstlisting}
In Python, we can replace the regular assignment operator \verb|=| with a different combination of symbols to do arithmetic: \verb|+=| or \verb|-=|. The first will add the value of whatever is on the right side of the to the variable on the left, while the latter will subtract. The most typical combination is \verb|variable += 1| to add 1 to the variable (equivalent to \verb|variable = variable + 1|), but you don't have to use 1. \verb|variable += 2| (equivalent to \verb|variable = variable + 2|) or \verb|variable -= 1| (equivalent to \verb|variable = variable - 1|) are both syntactically correct.\footnote{Python's method of adding and subtracting one from a variable is just one way of doing it. Other languages, like C++, C\#, Java, and JavaScript use double plus signs ++ or double negative signs -- instead (variable++ or variable- -). This is equivalent to variable += 1 or variable -= 1 in Python.}\par
However, it's more likely that you'll be using a \verb|while| loop for something other than iteration; otherwise, why would we have \verb|for| loops? As its name suggests, \verb|while| loops are great at testing conditions. It'd be a more realistic use case to test whether a specific variable is a certain value to determine if a certain chunk of code should be run again. We can use this property of \verb|while| loops to take in an indeterminate number of variables. For example, consider the example from the \verb|for| loop section where we took in four scores and computed the average. What if we didn't know how many scores the user needed to input? We could use a while loop to just keep taking in new scores until the user inputs something specific. Take a look at the following chunk of code.\par
\begin{lstlisting}[style=pippython]
grades = []
newGrade = 0
while (newGrade != -1):
  newGrade = int(input("Input another grade or -1 to stop: "))
  if (newGrade != -1):
    grades.append(newGrade)
average = sum(grades) / len(grades)
print("The average is", str(average))
\end{lstlisting}
Let's break this code down line-by-line. In the first two lines, we're creating two new variables: \verb|grades|, which is an empty list, and \verb|newGrade|, which is an integer. Next, we're creating a \verb|while| loop. We know that a grade can never be negative, so we can let our user know to use -1 to stop asking for new scores. Next, we are having our user input the new grade and are putting that value into the variable \verb|newGrade|. Then, we're testing whether \verb|newGrade| is our special -1 value to figure out whether we should add the \verb|newGrade| to the \verb|grades| list, as we don't want to add the \verb|-1| value itself. This code will continue asking the user to input another grade until they enter a -1 value. Finally, it will calculate the sum of the grades list divided by the length of the grades list, then print the result.\par
It's also worth mentioning that it's possible to create an infinite loop in Python. Infinite loops can be dangerous, since they can take up all of the processor cycles and make halting execution difficult. The difficulty of this is has to do with the specific Python interpreter that you're using, but it's a good idea to just not chance things. For example, the following example creates an infinite loop.\par
\begin{lstlisting}[style=pippython]
while True:
  print ("Hello, World!")
\end{lstlisting}
\begin{lstlisting}
Hello, World!
Hello, World!
Hello, World!
...
\end{lstlisting}
This code has nothing to ever turn the condition false, so this loop will run forever (or more specifically, until the program runs out of memory). Avoid writing infinite loops unless you have a really, really good reason to!\par
\warningtext{Infinite Loop Detection}{Some IDEs have the ability to detect an unintentional infinite loop which is uncontrollable, and they'll insert special interrupts in order to help you halt the script. However, many IDEs do not have this functionality, so don't rely on your IDE to help you halt an infinite loop.}\par
\funtext{Copy and Pasting}{"I was interviewing for a bunch of positions, and I was terrible at coding back then. Like, absolutely terrible. And they wanted me to write a function where, if there were a hundred lockers, write a function to go through each one and check something. So I wrote a function, but it was just for locker 1. And they asked me how I would repeat this a hundred times. Now, the right answer is to just write a loop, a for loop for a hundred times. And I said, 'just copy and paste it! Just copy and paste it a hundred times!'...I didn't get the job."\\\hspace*{\fill} - Jeremy Wang (aka Disguised Toast, June 2020)\footnote{\href{https://youtu.be/oiNPgJmtzVI?t=84}{https://youtube.com/watch?v=oiNPgJmtzVI\&t=84}}}\par
\subsubsection*{Exercise Questions}
These exercise questions cover chapter 6.4.
\begin{Exercise}
    \Question{What is the difference between a for loop and a while loop?}
    \Question{If a condition is never met in a while loop, what will happen?}
\end{Exercise}
\begin{Exercise}
    \Question{Write an example of an infinite while loop.}
    \Question{Write an example of an infinite for loop. Consider what needs to happen for an infinite loop to occur and replicate this in a for loop.}
\end{Exercise}
\begin{Exercise}
Create a small script that asks the user for a sequence of numbers over and over again, until they put a negative number (like -1). Store all of the results in a list with the variable name \verb|numbers|. The end list should look something like \verb|numbers = [33, 48, 9, 6, 83]|.
\end{Exercise}
\begin{Exercise}
Provide two examples of why you might want to create an infinite loop. For each example, provide a brief code snippit that demonstrates how you might use the infinite loop to your advantage, as well as how you would control the execution to make sure the loop stays controlled. In your code snippit, only provide the necessary lines to demonstrate the loop itself and how you intend on controlling the loop; everything else can just be a comment on what you would put there.
\end{Exercise}
\section{Scope}
One important concept that's difficult for many introductory programming students to grasp is scope. Scope allows us to keep our variables restricted to a certain block, and being able to effectively use scope to write clean code is an essential skill in higher levels of programming. Misusing scope or not knowing how it works is the source of a lot of frustration, and at worst, it can cause some pretty serious security issues.\par
Consider the following code.\par
\begin{lstlisting}[style=pippython]
# MARKER A
for i in range(5):
  # MARKER B
  print (i)
# MARKER C
\end{lstlisting}
We can tell what this code will do, but direct your attention to the markers in each of the comments. Where can we access the variable \verb|i|? We know that we can't use it at marker A, since the variable hasn't even been declared yet. We know that we can use it inside of the \verb|for| loop, though; we've seen this done plenty before. But what about marker C? In fact, we \textit{cannot} use \verb|i| here, since it is outside of the \newterm[scope]{functions!scope} of where the variable was first declared.\par
Scope dictates where we can use a variable. In Python, a variable can be used at the level it was declared at and anywhere deeper, but never outside, unless it has been declared to be a \newterm[global variable]{functions!scope!global}. This means that if you were to use a nested \verb|for| loop inside of an \verb|if| statement, you could use the iterator variable only inside of the \verb|for| loop, not just anywhere in the \verb|if| statement. However, if we declared a variable inside of the \verb|if| statement, it can be used anywhere inside of the \verb|if| statement, including inside of the nested \verb|for| loop. Consider the following code.\par
\begin{lstlisting}[style=pippython]
a = 1
if a > 5:
  b = 7
  while (c < 10):
    c = 3
    c += 1
\end{lstlisting}
In the above code, we can use \verb|a| anywhere inside of the program. We can use \verb|b| anywhere inside of the \verb|if| statement, but not outside of that specific \verb|if| statement. We can use \verb|c| only within the \verb|while| loop.\par
Furthermore, if two statements are at the same level, they cannot share variables.\par
\begin{lstlisting}[style=pippython]
while (a < 10):
  print(a)
  a += 1
while (b < 15):
  print(b)
  b += 3
\end{lstlisting}
In the above example, \verb|a| can only be used within the first \verb|while| loop, and \verb|b| can only be used within the second \verb|while| loop. Attempting to use a variable outside of scope may result in a \verb|NameError|, as the variable doesn't technically exist outside of its own scope.\par
\warningtext{Reinitialization Warning}{Variables are \textit{not} destroyed once execution moves beyond the loop, conditional, function, or class, even if they are not accessible. If you want to re-run the block from scratch, you \textit{must} reinitialize them to what their starting values should be.}
Another topic to discuss is the global variable: what if we want to be able to use a variable anywhere within our code? We can use a global variable instead of a local variable. Global variables can be accessed from within any class, including those in different files. You can simply make a global variable by declaring it in the outermost scope level, as shown here.\par
\begin{lstlisting}[style=pippython]
a = 0
while (a < 10):
  ...
\end{lstlisting}
Let's say that you define a variable in the outermost scope level, as shown above. Then, inside of a function (covered in the next chapter), you want to use the exact same variable and memory space. You can use the \verb|global| keyword to specify that Python should reference the exact same memory space that the variable label points to.\par
\begin{lstlisting}[style=pippython]
a = 0
def someFunction():
  global a
  # do something
  ...
\end{lstlisting}
In the above function, the scope of \verb|a| is accessible inside of the function, and its value is the exact same as outside of the function. This doesn't terribly matter for loops and conditional logic, but it does for classes and functions.
\subsubsection*{Exercise Questions}
These exercise questions cover chapter 6.5.
\begin{Exercise}
    \Question{In your own words, define scope.}
    \Question{If a variable is "out of scope," what does that mean?}
    \Question{If you try to call a variable that is out of scope, what type of error might Python throw?}
\end{Exercise}
\begin{Exercise}
    \Question{What is global scope?}
    \Question{What are some advantages of global scope?}
    \Question{What are some disadvantages of global scope?}
\end{Exercise}
\begin{Exercise}
For this exercise, consider the following code.
\begin{lstlisting}[style=pippython]
# MARKER A
i = input("Input a number")
# MARKER B
if (i > 5):
    # MARKER C
    print(i)
else:
    # MARKER D
    if (i == 5):
        # MARKER E
        k = True
    else:
        # MARKER F
        k = False
\end{lstlisting}
    \Question{At marker A, what variables are accessible, if any?}
    \Question{At marker B, what variables are accessible, if any?}
    \Question{At marker C, what variables are accessible, if any?}
    \Question{At marker D, what variables are accessible, if any?}
    \Question{At marker E, what variables are accessible, if any?}
    \Question{At marker F, what variables are accessible, if any?}
    \Question{Consider the variable \verb|i|. Is \verb|i| globally scoped? Why or why not?}
    \Question{Consider the variable \verb|k|. Is \verb|k| globally scoped? Why or why not?}
\end{Exercise}


%!TEX root =  ../main.tex

\objective{Solve arbitrary equations involving rational powers.}

Fractional exponents are nothing too special.  It is simply important to remember that in the
fraction, the numerator is what we've been thinking of as powers ($\frac{5}{1}=5$) and the
denominators are roots, i.e. $2^{\frac{1}{2}}=\sqrt{2}$.  As we will see next section, this isn't
a watertight definition, but it will do for now.

\subsection{Multiplying}\index{Exponents!rules of}
In situations where exponents abound, it may become confusing when one can and cannot
multiply, and what to do with exponents.  What do we mean when we write $5^2\cdot5^3$?
Is that $5^5$ or $5^6$?  Expanding the notation should help clear it up.  $5^2$ means
``multiply by five twice'', i.e. $5\cdot 5$.  $5^3$ means ``multiply by five three times'', $5\cdot
5\cdot 5$.  So we can see that $5^2\cdot5^3=5\cdot 5\cdot 5\cdot 5\cdot 5=5^5$.

\begin{derivation}{Multiplying with the Same Base}
$b^m\cdot b^n=b^{m+n}$.  Notice that $b$ must be consistent.
\end{derivation}

Students sometimes want to combine in impossible ways.  $2\cdot5^3$ becomes $10^3$
somehow, in their minds.  When we consider what the notation means, the contradiction
becomes clear.  $2\cdot5^3$ means ``two times this: five-times-five-times-five'', which is in no
way the same as ``ten times ten times ten''.

\subsection{Dividing}
If the exponents can be added when multiplying powers of the same base, what would you
expect when dividing?  Yes, it is subtraction.

\begin{derivation}{Dividing with the Same Base}
$\cfrac{b^m}{b^n}=b^{m-n}$.  Notice that $b$ must be consistent.
\end{derivation}

This is a good explanation for negative exponents.  For example, $\cfrac{2^3}{2^8}=2^{-5}$.
There are more two's in the denominator than the numerator.  This is the same as $\frac{1}{2^5}$,
which is a far more useful way to write the fraction.  When cancelling and simplifying are done,
it is conventional to expand the exponent, in this case writing $\frac{1}{32}$.

This also explains the origin of $b^0=1$, unless $b=0$.  Zero exponent arrises when there
are as many of the base in the denominator as there are in the numerator.  Anything
divided by itself is 1.

\subsection{Exponents}
What happens when there is an exponent on an exponent?  Easier than a dream within a
dream, the exponents continue to mean what they have always meant: ``have this many
of this bases be multiplied against themselves''.  For example, $(2^3)^4$ means ``four
groups of two-times-two-times-two'', or $2^12$.

\begin{derivation}{Distribution of Exponents over Multiplication}
$(b^m\cdot c^n)^p=b^{m\cdot p}\cdot c^{n\cdot p}$ and so for, on each element under
the power
\end{derivation}

\begin{example}
\exProblem
Solve $x^{\frac{3}{2}}=27$.

\exSolution
To get $x$ to have a simple exponent of 1, raise both sides to the two-third.\\
$\left(x^\frac{3}{2}\right)^{\frac{2}{3}}=27^{\frac{2}{3}}$
$x=\left(\sqrt[3]{27}\right)^2$\\
$x=3^2=9$
\end{example}

Notice too, there is no distributive property of exponents over addition.  $\sqrt{x^2+1}$
is irreducible, not $x+1$.  We would need to know what $x$ was to be able to proceed.

\subsection{Rational Exponents}\index{exponents!rational}
Fractional exponents must be the same as roots.  For example, we know from
the multiplication property that $4^\frac{1}{2}\cdot 4^\frac{1}{2}$ must equal $4^1$.
That must mean we are looking for a number times itself to equal 4, so $4^\frac{1}{2}$
must equal 2.  This means $\sqrt{4}=4^\frac{1}{2}$.  By the same logic, we would
find that $4^\frac{1}{3}=\sqrt[3]{4}$, $4^\frac{1}{4}=\sqrt[4]{4}$, etc.  By the power
rule above, $4^\frac{3}{2}$ must be the same as $(4^\frac{1}{2})^3$.  This could also
be written as $4^{1.5}$.

We must be careful with even rational exponents, because they can hide the sign of the base.
For example, $((-5)^2)^\frac{1}{2}=5$.  The safe answer then to $\sqrt[n]{x^n}$ for any
even $n$ is $|x|$.


\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage{amssymb,amsmath}

\usepackage{graphicx}

\usepackage[top=3cm, bottom=3cm, left=3cm, right=3cm, includefoot]{geometry}

\usepackage{amsthm}
\theoremstyle{definition}
\newtheorem{definition}{Definition}

\begin{document}

\begin{definition}
\emph{Convex polytope} $\mathbf{S}$ is the set of all convex combinations of a finite point set $ \mathcal{S}$, i.e.
\begin{displaymath}
 \mathbf{S} :=
 \left\lbrace
 v \in \mathbb{R}^n: v := \sum\limits_{i=1}^{\vert \mathcal{S} \vert} \alpha_i v_i;
	\forall i = 1,\dots,\vert \mathcal{S} \vert: \alpha_i \geq 0, v_i \in \mathcal{S},
	\sum\limits_{i=1}^{\vert \mathcal{S} \vert} \alpha_i = 1
 \right\rbrace.
\end{displaymath}
\end{definition}

Let us consider two convex polytopes $\mathbf{P}, \mathbf{Q}$ in $\mathbb{R}^d$ defined by the boundary point sets
\begin{displaymath}
 \begin{array}{rcll}
  \mathcal{P} & := & \lbrace p_1, \dots, p_{n_p} \rbrace \subset \mathbb{R}^d ~\mathrm{,} \\
  \mathcal{Q} & := & \lbrace q_1, \dots, q_{n_q} \rbrace \subset \mathbb{R}^d ~\mathrm{.}
 \end{array}
\end{displaymath}
The problem is to find the shortest distance between these two objects
\begin{equation}
 \label{eq:polytope_problem1}
 \min\limits_{p \in \mathcal{P}, q \in \mathcal{Q}} \Vert p - q \Vert ~\mathrm{.}
\end{equation}
Every interior point of the convex polytope can be expressed as a convex linear combination of given points in sets $\mathcal{P}$ and $\mathcal{Q}$
\begin{equation}
 \label{eq:polytope_linear_comb}
 \begin{array}{l}
  \forall p \in \mathbf{P} ~ \exists \alpha_1, \dots, \alpha_{n_p} \in \mathbb{R}: p = \sum\limits_{i = 1}^{n_p} \alpha_{i} p_i ~\mathcal{,} \\
	~~~~~~~~~~~~~~~~~~~~~~~ \mathrm{where} ~ \sum\limits_{i = 1}^{n_p} \alpha_i = 1 ~ \mathrm{and} ~ 0 \leq \alpha_i \leq 1 ~\forall i = 1,\dots,n_p ~\mathrm{,} \\
  \forall q \in \mathbf{Q} ~ \exists \beta_1, \dots, \beta_{n_q} \in \mathbb{R}: q = \sum\limits_{i = 1}^{n_q} \beta_{i} q_i ~\mathcal{,} \\
	~~~~~~~~~~~~~~~~~~~~~~~ \mathrm{where} ~ \sum\limits_{i = 1}^{n_q} \beta_i = 1 ~ \mathrm{and} ~ 0 \leq \beta_i \leq 1 ~\forall i = 1,\dots,n_q ~\mathrm{.}
 \end{array}
\end{equation}
We denote
\begin{displaymath}
 \begin{array}{rcl}
  y & := & [\alpha_1, \dots, \alpha_{n_p}, \beta_1, \dots, \beta_{n_q}]^T \in \mathbb{R}^{n_p + n_q} ~\mathrm{,} \\
  C & := & [p_1, \dots, p_{n_p}, -q_1, \dots, -q_{n_q}] \in \mathbb{R}^{d, n_p + n_q} ~\mathrm{,} \\
  B & := &
  \left[
   \begin{array}{cccccc}
    1 & \dots & 1 & 0 & \dots & 0 \\
    0 & \dots & 0 & 1 & \dots & 1
   \end{array}
  \right] \in \mathbb{R}^{2, n_p + n_q}  ~\mathrm{,} \\
  c & := & [1,1]^T \in \mathbb{R}^{2} \mathrm{.}
 \end{array}
\end{displaymath}
Afterwards, the cost function can be reformulated
\begin{displaymath}
 \Vert p - q \Vert  = \left\Vert \sum\limits_{i = 1}^{n_p} \alpha_{i} p_i - \sum\limits_{i = 1}^{n_q} \beta_{i} q_i \right\Vert = \Vert Cy \Vert
\end{displaymath}
and the feasible set conditions have the form
\begin{displaymath}
 By = c ~~~ \wedge ~~~ y \geq 0 ~\mathrm{.}
\end{displaymath}
After these notations, the problem \eqref{eq:polytope_problem1} can be reformulated as
\begin{equation}
 \label{eq:polytope_problem2}
 \begin{array}{rcl}
  \bar{y} & := & \arg \min\limits_{y \in \Omega_E \cap \Omega_I} y^T C^T C y ~\mathrm{,} \\
  \Omega_E & := & \left\lbrace y \in \mathbb{R}^{n_p + n_q}: By = c \right\rbrace ~\mathrm{,} \\
  \Omega_I & := & \left\lbrace y \in \mathbb{R}^{n_p + n_q}: y \geq 0 \right\rbrace ~\mathrm{.}
 \end{array}
\end{equation}
The next step consists of homogenization and orthogonalization. We introduce a substitution
\begin{equation}
 \label{eq:polytope_subs}
 x := y - y_{\mathrm{in}} ~~ \Rightarrow ~~ y = x + y_{\mathrm{in}} ~\mathrm{,}
\end{equation}
where $y_{in}$ is arbitrary point from $\Omega_E$. We can choose
\begin{displaymath}
 y_{\mathrm{in}} := \left[ \frac{1}{n_p}, \dots, \frac{1}{n_p}, \frac{1}{n_q}, \dots, \frac{1}{n_q} \right] \in \mathbb{R}^{n_p + n_q} ~\mathrm{.}
\end{displaymath}
Afterwards, the cost function and conditions have the form
\begin{displaymath}
 \begin{array}{l}
  f(x) := \frac{1}{2} \Vert C(x + y_{\mathrm{in}}) \Vert^2 = \frac{1}{2} x^T \overbrace{C^T C}^{=: A} x + x^T \overbrace{C^T C y_{\mathrm{in}}}^{=: - b} + ~c, ~~~ c =: \frac{1}{2} y_{\mathrm{in}}^T C^T C y_{\mathrm{in}} = ~\mathrm{const.} ~\mathrm{,}\\
  B(x + y_{\mathrm{in}}) = Bx + B y_{\mathrm{in}} = Bx + c ~~~ \Rightarrow ~~~ ( By = c ~ \Leftrightarrow ~ Bx = 0) ~\mathrm{,} \\
  y \geq 0 ~~ \Leftrightarrow ~~ x \geq - y_{\mathrm{in}}  ~\mathrm{.}
 \end{array}
\end{displaymath}
Moreover, the matrix $B$ can be orthonormalized using simple process
\begin{displaymath}
 \hat{B} :=
 \left[
  \begin{array}{cc}
   \frac{1}{\sqrt{n_p}} & 0 \\
   0 & \frac{1}{\sqrt{n_q}}
  \end{array}
 \right]
 B  ~\mathrm{.}
\end{displaymath}
We obtained QP with homogeneous orthogonal linear equality constraints and bound inequality constraints
\begin{equation}
 \label{eq:polytope_problem3}
 \begin{array}{rcl}
  \bar{x} & := & \arg \min\limits_{x \in \Omega_E \cap \Omega_I} \frac{1}{2} x^T A x - b^T x ~\mathrm{,} \\
  \Omega_E & := & \left\lbrace x \in \mathbb{R}^{n_p + n_q}: \hat{B}x = 0 \right\rbrace ~\mathrm{,} \\
  \Omega_I & := & \left\lbrace x \in \mathbb{R}^{n_p + n_q}: x \geq -y_{\mathrm{in}} \right\rbrace ~\mathrm{.}
 \end{array}
\end{equation}
After solving this problem, the original solution can be obtained using back substitution \eqref{eq:polytope_subs} to obtain $y$, i.e. the coeficients of linear combinations \eqref{eq:polytope_linear_comb} of the nearest points from each polytope.

%\newpage

\paragraph{Numerical example}

We consider two circles discretized by parameter $m \geq 3$, whose boundary points are defined by $P, Q \in \mathbb{R}^{2,m}$
with columns
\begin{displaymath}
 P_{\ast,i} =
 \left[
  \begin{array}{c}
   \cos (2i\pi / m ) - 2  \\
   \sin (2i\pi / m )
  \end{array}
 \right]
 , ~~~
 Q_{\ast,i} =
 \left[
  \begin{array}{c}
   \cos (\pi - 2i\pi / m ) + 2 \\
   \sin (\pi - 2i\pi / m )
  \end{array}
 \right], ~~~
 i = 0,\dots,m-1 ~\mathrm{.}
\end{displaymath}
Examples for $m = 5$ and $m=7$ can be found in Fig. \ref{fig:polytope1}.

\begin{figure}[h!]
\begin{center}
\includegraphics[width=0.45\textwidth]{polytope_n5.eps}
\includegraphics[width=0.45\textwidth]{polytope_n7.eps}
\caption[Polytope distance: testing benchmark]{Testing benchmark for polytopes distance with discretization parameter $m = 5$ \emph{(left)} and $m = 7$ \emph{(right)}.}
\label{fig:polytope1}
\end{center}
\end{figure}

The solution of the problem for any $m$ is given by
\begin{displaymath}
 \bar{y} =
 \left[
  \alpha_1, \dots, \alpha_m, \beta_1, \dots, \beta_m
 \right]^T =
 [\underbrace{1 , 0, \dots, 0}_{\in \mathbb{R}^m}, \underbrace{1 , 0, \dots, 0}_{\in \mathbb{R}^m}]^T ~\mathrm{.}
\end{displaymath}

In this problem, we can directly compute the regular condition number of Hessian matrix
\begin{displaymath}
 \kappa (A) = \kappa (C^T C) = \kappa (CC^T) = \kappa
 \left( \left[
  \begin{array}{cc}
   \sum\limits_{i = 1}^{n_p} P_{i,1}^2 + \sum\limits_{i = 1}^{n_q} Q_{i,1}^2 & \sum\limits_{i = 1}^{n_p} P_{i,1} P_{i,2} + \sum\limits_{i = 1}^{n_q} Q_{i,1} Q_{i,2} \\
   \sum\limits_{i = 1}^{n_p} P_{i,2} P_{i,1} + \sum\limits_{i = 1}^{n_q} Q_{i,2} Q_{i,1} & \sum\limits_{i = 1}^{n_p} P_{i,2}^2 + \sum\limits_{i = 1}^{n_q} Q_{i,2}^2
  \end{array}
 \right] \right) ~\mathrm{.}
\end{displaymath}
Moreover, it holds
\begin{displaymath}
 \begin{array}{rcl}
 \sum\limits_{i = 1}^{n_p} P_{i,2} P_{i,1} + \sum\limits_{i = 1}^{n_q} Q_{i,2} Q_{i,1} & = &
	\sum\limits_{i = 0}^{m-1}
		\left( \cos \left( \frac{2i\pi}{m} \right) - 2 \right)
		\sin \left( \frac{2i\pi}{m} \right) \\
  & & ~~~~~ +
 \sum\limits_{i = 0}^{m-1}
  \left( \cos \left( \pi - \frac{2i\pi}{m} \right) + 2 \right)
  \sin \left( \pi - \frac{2i\pi}{m} \right)
 = 0~\mathrm{,}
 \end{array}
\end{displaymath}
so
\begin{displaymath}
 \kappa (A) = \kappa
 \left( \left[
  \begin{array}{cc}
   \sum\limits_{i = 1}^{n_p} P_{i,1}^2 + \sum\limits_{i = 1}^{n_q} Q_{i,1}^2 & 0 \\
   0 & \sum\limits_{i = 1}^{n_p} P_{i,2}^2 + \sum\limits_{i = 1}^{n_q} Q_{i,2}^2
  \end{array}
 \right] \right) ~\mathrm{.}
\end{displaymath}
Afterwards, the condition number can be expressed
\begin{displaymath}
\kappa (A) = \frac{\sum\limits_{i = 1}^{n_p} P_{i,1}^2 + \sum\limits_{i = 1}^{n_q} Q_{i,1}^2}{\sum\limits_{i = 1}^{n_p} P_{i,2}^2 + \sum\limits_{i = 1}^{n_q} Q_{i,2}^2} =
\frac{\sum\limits_{i=0}^{m-1} \left( \cos \left( \frac{2i\pi}{m} \right) - 2 \right)^2}{\sum\limits_{i=0}^{m-1} \sin^2 \left( \frac{2i\pi}{m} \right)} ~~~~ \forall m \geq 3 ~\mathrm{.}
\end{displaymath}

\noindent Let us consider $\alpha \in \mathbb{R}$ and let us present a complex number $z \in \mathbb{C}$ by prescription
\begin{displaymath}
 z := \cos \alpha + \mathbf{i} \sin \alpha ~\mathrm{,}
\end{displaymath}
where $\mathbf{i}$ is imaginary unit. Then by De Moivre's formula we can write
\begin{displaymath}
 \sum\limits_{i=0}^{m-1} \left( \cos ( i\alpha) + \mathbf{i} \sin ( i \alpha) \right) =
 \sum\limits_{i=0}^{m-1} z^i =
 \frac{z^m - 1}{z - 1} =
 \frac{\cos (m\alpha) + \mathbf{i} \sin (m\alpha) - 1}{z-1} ~\mathrm{.}
\end{displaymath}
If we choose specific $\alpha$ in previous equality, we obtain next
\begin{equation}
 \label{eq:polytope_ne_pom}
  \begin{array}{ccccc}
   \alpha := \frac{2 \pi}{m}& ~ \Rightarrow ~ & \sum\limits_{i=0}^{m-1} \left( \cos \left( \frac{2i\pi}{m} \right) + \mathbf{i} \sin \left( \frac{2i\pi}{m} \right) \right) = 0 & ~ \Rightarrow ~ & \sum\limits_{i=0}^{m-1} \cos \left( \frac{2i\pi}{m} \right) = 0\mathrm{,}\\
   \alpha := \frac{4 \pi}{m}& ~ \Rightarrow ~ & \sum\limits_{i=0}^{m-1} \left( \cos \left( \frac{4i\pi}{m} \right) + \mathbf{i} \sin \left( \frac{4i\pi}{m} \right) \right) = 0 & ~ \Rightarrow ~ & \sum\limits_{i=0}^{m-1} \cos \left( \frac{4i\pi}{m} \right) = 0\mathrm{.}
  \end{array}
\end{equation}
Now, we return back to regular condition number and $\forall m \geq 3$ we can write (using \eqref{eq:polytope_ne_pom})
\begin{displaymath}
 \begin{array}{rl}
   \kappa (A) = & \frac{\sum\limits_{i=0}^{m-1} \left( \cos \left( \frac{2i\pi}{m} \right) - 2 \right)^2}{\sum\limits_{i=0}^{m-1} \sin^2 \left( \frac{2i\pi}{m} \right)}
= \frac{\sum\limits_{i=0}^{m-1} \cos^2 \left( \frac{2i\pi}{m} \right) - 4 \cos \left( \frac{2i\pi}{m} \right) + 4}{\sum\limits_{i=0}^{m-1} \sin^2 \left( \frac{2i\pi}{m} \right)} \\
 & = \frac{\sum\limits_{i=0}^{m-1} \left( \frac{1 + \cos \left( \frac{4i\pi}{m} \right)}{2} - 4 \cos \left( \frac{2i\pi}{m} \right) + 4 \right)}{\sum\limits_{i=0}^{m-1}
  \frac{1 - \cos \left( \frac{4i\pi}{m} \right)}{2}}
 =  \frac{ \frac{1}{2} \sum\limits_{i=0}^{m-1} \cos \left( \frac{4i\pi}{m} \right) - 4 \sum\limits_{i=0}^{m-1} \cos \left( \frac{2i\pi}{m} \right) + \sum\limits_{i=0}^{m-1} \frac{9}{2}   }{-\frac{1}{2}\sum\limits_{i=0}^{m-1} \cos \left( \frac{4i\pi}{m} \right) + \sum\limits_{i=0}^{m-1} \frac{1}{2} } = 9 ~\mathrm{.}
 \end{array}
\end{displaymath}

\end{document}


\section{Introduction}

The objective of this exercise is to study damped and driven oscillations in
mechanical systems using the Pohl resonator. For driven oscillations, we will
also observe and quantify the mechanical resonance phenomenon.

If a periodically varying external force is applied to a damped harmonic
oscillator, the resulting motion is called forced (or driven) oscillations, and
the external force is called the driving force. Assuming that the driving force
is of the form
    \[
        F=F_0(sin\omega t+\delta),
    \]
with the amplitude F 0 and angular frequency ω, the resulting steady-state
forced oscillations will be simple harmonic with the angular frequency equal to
that of the driving force. The amplitude of these steady-state oscillations
turns out to depend on the angular frequency of the the driving force, in
particular on how far it is from the natural angular frequency, and the damping
coefficient. The amplitude may become quite large, and this phenomenon is known
as the mechanical resonance.

Another interesting property of driven steady-state oscillations is the fact
that there is a phase lag between the driving force and the displacement from
the equilibrium position of the oscillating particle. This phase lag reaches
$\pi/2$ (a quarter of the cycle) when the system is driven at the natural
angular frequency.

In this experiment, forced oscillation of a balance wheel will be studied. The
corresponding quantities (such as the force and the position) will be replaced
by their angular counterparts.

The driving torque $\tau_{dr}=\tau_0cos\omega t$ and a damping torque
$\tau_f=-b\frac{d\theta}{dt}$, Also, we know the restoring torque
$\tau=-k\theta$, its equation od motion is of the form

\begin{equation}
\label{1}
I\frac{d^2\theta}{dt^2}=-k\theta-b\frac{d\theta}{dt}+\tau_0cos\omega t,
\end{equation}

where I is the moment of inertia of the balance wheel, $\tau_0$ is the amplitude
of the driving torque, and $\omega$ is the angular frequency of the driving
torque. Introducing the symbols

\[
\omega_0=\frac{k}{I},\quad 2\beta=\frac{b}{I}, \quad \mu=\frac{\tau_0}{I},
\]

Eq. \ref{1} can be rewitten as
\begin{equation}
\label{2}
\frac{d^2\theta}{dt^2}+2\beta\frac{d\theta}{dt}+\omega_0^2\theta
=\mu cos\omega t.
\end{equation}

Thw solution to Eq \ref{2} is
\[
\theta(t)=\theta_{tr}(t)+\theta_{st}cos(\omega t+\varphi),
\]
where the former term $\theta_{tr}$ denotes the transient solution that vanished
exponentially as $t\rightarrow \infty$. The steady-state  oscillation is with
the amplitude
\[
\theta_{st}=\frac{\mu}{\sqrt{(\omega_0^2-\omega^2)^2+4\beta^2\omega^2}}
\]
For small values of the damping coefficient $\beta$, the resonance angular
frequency is close to the the natural angular frequency, and the amplitude of
steady-state oscillations becomes large. The dependence of both the amplitude
and the phase shift on the driving angular frequency are shown in the left and
right Figure 1, respectively, for different values of the damping coefficient.