\documentclass[letterpaper,12pt,leqno]{article}
\usepackage{paper,math,notes}

\begin{document}

\title{Mathematical Methods for Macroeconomics: Exercises}
\author{Pascal Michaillat}
\date{}

\begin{titlepage}
\maketitle
\end{titlepage}

\section*{Dynamic Programming}

\subsection*{Exercise 1.}

Consider the following optimal growth problem: Given initial capital $k_{0}>0$, choose consumption $\bc{c_{t}} _{t =0}^{+\infty}$ to maximize utility
\begin{equation*}
\sum_{t=0}^{\infty}\b ^{t}\cdot \ln{c_{t}}
\end{equation*}
subject to the resource constraint
\begin{equation*}
k_{t+1}=A\cdot k_{t}^{\a}-c_{t}.
\end{equation*}
The parameters satisfy $0<\b<1,\;A>0,\;0<\a <1.$

\begin{enumerate}
\item Derive the optimal law of motion of consumption $c_{t}$ using a Lagrangian.
\item Identify the state variable and the control variable.
\item Write down the Bellman equation.
\item Derive the following Euler equation:
\begin{equation*}
c_{t+1}=\b\cdot  \a\cdot  A\cdot k_{t+1}^{\a -1}\cdot c_{t}.
\end{equation*}

\item Derive the first two value functions, $V_{1}(k)$ and  $V_{2}(k)$, obtained by iteration on the Bellman equation starting with the value function $V_{0}\bp{k} \equiv 0$.
\item The process of determining the value function by iterations using the Bellman equation is commonly used to solve dynamic programs numerically. The algorithm is called \textit{value function iteration}. For this optimal growth problem, one can show show using value function iteration that the value function is
\[V\bp{k} =\kappa +\frac{\ln{k^{\a}}}{1-\a\cdot \b},\]
where $\k$ is a constant. Using the Bellman equation, determine the policy function $k'(k)$ associated with this value function.
\item In light of these results, for which reasons would you prefer to use the dynamic-programming approach instead of the Lagrangian approach to solve the optimal growth problem? And for which reasons would you prefer to use the Lagrangian approach instead of the dynamic-programming approach?
\end{enumerate}

\subsection*{Exercise 2.}

Consider the problem of choosing consumption $\bc{c_{t}}_{t=0}^{+\infty}$ to maximize expected utility
\begin{equation*}
\E_{0}\sum_{t=0}^{+\infty}\b^{t}\cdot u\bp{c_{t}}
\end{equation*}
subject to the budget constraint
\begin{equation*}
c_{t}+p_{t}\cdot s_{t+1}=\bp{d_{t}+p_{t}}\cdot s_{t}.
\end{equation*}
$d_{t}$ is the dividend paid out for one share of the asset, $p_{t}$ is the price of one share of the asset, and $s_{t}$ is the number of shares of the asset held at the beginning of period $t$. In equilibrium, the price $p_{t}$ of one share is solely a function of dividends $d_{t}$. Dividends can only take two values $d_{l}$ and $d_{h}$, with $0<d_{l}<d_{h}$. Dividends follow a Markov process with transition probabilities
\begin{equation*}
\P\bp{d_{t+1}=d_{l}\mid d_{t}=d_{l}} =\P \bp{d_{t+1}=d_{h}\mid d_{t}=d_{h}} =\rho
\end{equation*}
with $1>\rho >0.5.$

\begin{enumerate}
\item Identify state and control variables.
\item Write down the Bellman equation.
\item Derive the following Euler equation:
\begin{equation*}
p_{t}\cdot u'\bp{c_{t}} =\b\cdot  \E{\bp{d_{t+1}+p_{t+1}} \cdot u'\bp{c_{t+1}} \mid d_{t}} .
\end{equation*}

\item Suppose that $u\bp{c} =c$. Show that the asset price is higher when the current dividend is high.
\end{enumerate}

\subsection*{Exercise 3.}

Consider the following optimal growth problem: Given initial capital $k_{0}>0$, choose consumption and labor $\bc{c_{t},l_{t}}_{t=0}^{+\infty}$ to maximize utility
\begin{equation*}
\sum_{t=0}^{+\infty}\b^{t}\cdot u\bp{c_{t},l_{t}}
\end{equation*}
subject to the law of motion of capital
\begin{align*}
k_{t+1}&=A_{t}\cdot f\bp{k_{t},l_{t}} -c_{t}.
\end{align*}
In addition, we impose $0\leq l_{t}\leq 1$. The discount factor $\b \in \bp{0,1} $. The function $f$ is increasing and concave in both arguments. The function $u$ is increasing and concave in $c$, decreasing and convex in $l$.

\paragraph{Deterministic case} First, suppose $A_{t}=1$ for all $t$.

\begin{enumerate}
\item What are the state and control variables?
\item  Write down the Bellman equation.
\item Derive the following optimality conditions:
\begin{align*}
\pd{u\bp{c_{t},l_{t}}}{ c_{t}} &=\b \cdot \pd{u\bp{c_{t+1},l_{t+1}}}{c_{t+1}} \cdot \pd{f\bp{k_{t+1},l_{t+1}}}{ k_{t+1}}\\
\pd{u\bp{c_{t},l_{t}}}{ c_{t}}\cdot \pd{f\bp{k_{t},l_{t}}}{l_{t}} &=-\pd{u\bp{c_{t},l_{t}}}{l_{t}}.
\end{align*}
\item Suppose that the production function $f\bp{k,l} =k^{\a}\cdot l^{1-\a}$. Determine the ratios $c/k$ and $l/k$ in steady state.
\end{enumerate}

\paragraph{Stochastic case} Now, suppose $A_{t}$ is a stochastic process that takes values $A_{1}$ and $A_{2}$ with the following probability:
\begin{equation*}
\P{A_{t+1}=A_{1}\mid A_{t}=A_{1}} =\P{A_{t+1}=A_{2}\mid A_{t}=A_{2}} =\rho .
\end{equation*}

\begin{enumerate}\setcounter{enumi}{4}
\item Write down the Bellman equation.
\item Derive the optimality conditions.
\end{enumerate}

\section*{Optimal Control}

\subsection*{Exercise 4.}

Consider the following optimal growth problem: Given initial capital $k_{0}>0$, choose a consumption path $\bc{c_{t}}_{t\geq 0}$ to maximize utility
\begin{align*}
\int_{0}^{\infty}e^{-\rho\cdot  t} \cdot \ln{c_{t}} dt
\end{align*}
subject to the law of motion of capital
\begin{align*}
\dot{k}_{t} &=f\bp{k_{t}} -c_{t}-\d \cdot k_{t}.
\end{align*}
The discount factor $\rho>0$, and the production function $f$ satisfies
 \[f\bp{k} =A\cdot k^{\a},\]
 where  $\a \in \bp{0,1}$ and $A>0$.

\begin{enumerate}
\item Write down the present-value Hamiltonian.
\item Show that the Euler equation is
\begin{align*}
\frac{\dot{c}_{t}}{c_{t}} &=\a \cdot  A \cdot k_{t}^{\a -1}-\bp{\d +\rho}.
\end{align*}

\item Solve for the steady state of the system.
\end{enumerate}

\subsection*{Exercise 5.}

Consider the following investment problem: Given initial capital $k_{0}$, choose the investment path $\bc{i_{t}} _{t \geq 0}$ to maximize profits
\begin{align*}
\int_{0}^{\infty} e^{-r\cdot t}\bs{f\bp{k_{t}} -i_{t}-\frac{\chi}{2}\cdot \bp{\frac{i_{t}^{2}}{k_{t}}}} dt
\end{align*}
subject to the law of motion of capital (we assume no capital depreciation)
\[\dot{k}_{t} =i_{t}.\]
The interest rate $r>0$, the capital adjustment cost $\chi>0$, and the production function $f$ satisfies $f'>0$ and $f''<0$.

\begin{enumerate}
\item Write down the current-value Hamiltonian.
\item Use the optimality conditions for the current-value Hamiltonian to derive the following differential equations:
\begin{align*}
\dot{k}_{t} &=\bp{\frac{q_{t}-1}{\chi}}\cdot  k_{t} \\
\dot{q}_{t} &=r\cdot q_{t}-f'\bp{k_{t}} -\frac{1}{2\cdot \chi}\bp{q_{t}-1}^{2}
\end{align*}

\item Solve for the steady state.
\end{enumerate}

\section*{Differential Equations}

\subsection*{Exercise 6.}

Find the solution of the initial value problem
\begin{align*}
\dot{a}(t) &=r\cdot a(t) +s \\
a\bp{0} &=a_{0}
\end{align*}
where both $r$ and $s$ are known constant.

\subsection*{Exercise 7.}

Find the solution of the initial value problem
\begin{align*}
\dot{a}(t) &=r(t)\cdot a(t) +s(t) \\
a\bp{0} &=a_{0}
\end{align*}
where both  $r(t)$ and $s(t)$ are known functions of $t.$

\subsection*{Exercise 8.}

Consider the linear system of FODEs given by
\begin{equation*}
\bm{\dot{x}}(t)=\bs{
\begin{array}{ll}
1 & 1 \\
4 & 1
\end{array}} \bm{x}(t).
\end{equation*}
\begin{enumerate}
\item Find the general solution of the system.
\item What would you need to find a specific solution of the system?
\item Draw the trajectories of the system.
\end{enumerate}

\subsection*{Exercise 9.}

Consider the initial value problem
\begin{align*}
\dot{k}(t) &=s\cdot f\bp{k(t)} -\d\cdot  k(t) \\
k\bp{0} &=k_{0}
\end{align*}
where the saving rate $s\in \bp{0,1} $, the capital depreciation rate $\d \in \bp{0,1}$, and the production function $f$ satisfies the \textit{Inada conditions}. That is, $f$ is continuously differentiable and
\begin{align*}
f(0)&=0\\
f'(x)&>0\\
f''(x)&<0\\
\lim_{x\to 0} f'(x)&=+\infty\\
\lim_{x\to +\infty} f'(x)&=0.
\end{align*}

\begin{enumerate}
\item Give a production function $f$ that satisfies the Inada conditions.
\item Find the steady state of the system.
\item Draw the dynamic path of $k(t) $ and show that it converges to the steady state.
\end{enumerate}

\subsection*{Exercise 10.}

The solution of the problem studied in Exercise 4 is characterized by a system of two nonlinear first-order differential equations:
\begin{align*}
\dot{k}_{t} &=f\bp{k_{t}} -c_{t}-\d \cdot k_{t}\\
\frac{\dot{c}_{t}}{c_{t}} &=\a \cdot  A \cdot k_{t}^{\a -1}-\bp{\d +\rho}.
\end{align*}
The first FODE is the law of motion of capital. The second FODE is the Euler equation, which describes the optimal path of consumption over time.

\begin{enumerate}
\item Draw the phase diagram of the system.
\item Linearize the system around its steady state.
\item Show that the steady state is a saddle point  locally.
\item Suppose the economy is in steady state at time $t_{0}$ and there is an unanticipated decrease in the discount factor $\rho$. Show on your phase diagram the transition dynamics of the model.
\end{enumerate}

\subsection*{Exercise 11.}

The solution of the investment problem studied in Exercise 5 is characterized by a system of two nonlinear first-order differential equations:
\begin{align*}
\dot{k}_{t} &=\bp{\frac{q_{t}-1}{\chi}}\cdot  k_{t} \\
\dot{q}_{t} &=r\cdot q_{t}-f'\bp{k_{t}} -\frac{1}{2\cdot \chi}\bp{q_{t}-1}^{2}.
\end{align*}
The first FODE is the law of motion of capital $k_{t}$. The second FODE is the law of motion of the co-state variable $q_{t}$.

\begin{enumerate}
\item Draw the phase diagram.
\item Show that the steady state is a saddle point locally.
\end{enumerate}

\subsection*{Exercise 12.}

Consider a discrete time version of the typical growth model:
\begin{align*}
k(t+1) &=f\bp{k(t)} -c(t) +\bp{1-\d}\cdot  k(t) \\
c(t+1) &=\b\cdot  \bs{ 1+f'\bp{k(t)} -\d }\cdot  c(t) .
\end{align*}
The discount factor $\b \in \bp{0,1}$, the rate of depreciation of capital $\d \in \bp{0,1}$, initial capital $k_{0}$ is given, and the production function $f$ satisfies the Inada conditions. These two equations are a system of first-order difference equations. Whereas a system of first-order differential equations relates $\bm{\dot{x}}(t) $ to $\bm{x}(t)$, a system of first-order difference equations relate $\bm{x}(t+1) $ to $\bm{x}(t)$.

In this exercise, we will see that we can study a system of first-order difference equations with the tools that we used to study  systems of first-order differential equations. In particular, we can use phase diagrams to understand the dynamics of the system.

\begin{enumerate}
\item Construct a phase diagram for the system. First, define
\begin{align*}
\D k & \equiv k(t+1) -k(t) , \\
\D c & \equiv c(t+1) -c(t) .
\end{align*}
Second, draw the $\D k=0$ locus and the $\D c=0$ locus on the $(k,c)$ plane. Finally, find the steady state as the intersection of the $\D k=0$ locus and the $ \D c=0$ locus.
\item Show that the steady state is a saddle point in the phase diagram.
\end{enumerate}

\subsection*{Exercise 13.}

We consider the following optimal growth problem. Given initial human capital $h_{0}$ and initial physical capital $k_{0}$, choose consumption $c(t) $ and labor $l(t) $ to maximize utility
\begin{equation*}
\int_{0}^{\infty}e^{-\rho\cdot  t}\cdot \ln{c} dt
\end{equation*}
subject to
\begin{align*}
\dot{k}_{t} &=y_{t}-c_{t}-\d\cdot  k_{t} \\
\dot{h}_{t} &=B\cdot \bp{1-l_{t}}\cdot  h_{t}.
\end{align*}
Output $y_{t}$ is defined by
\[y_{t}\equiv A\cdot k_{t}^{\a}\cdot \bp{l_{t}\cdot h_{t}} ^{\b}.\]
We also impose that $0 \leq l_{t}\leq 1$. The discount factor $\rho>0$, the rate of depreciation of physical capital $\d>0$, the constants $A>0$ and $B>0$, and the production function parameters $\a\in \bp{0,1}$ and $\b \in\bp{0,1}$.

\begin{enumerate}
\item Give state and control variables.
\item Write down the present-value Hamiltonian for this problem.
\item Derive the optimality conditions.
\item Show that the growth rate of consumption $c(t)$ is
\begin{equation*}
\frac{\dot{c}}{c}=\frac{\a\cdot  y}{k}-\bp{\d +\rho} .
\end{equation*}
\item From now on, we assume that $B=0$. Show that $l=1$.
\item Draw the phase diagram in the $(k,c)$ plane.
\item Show on the diagram that the steady state of the system is a saddle point.
\item Derive the Jacobian of the system.
\item Show that the steady state of the system is a saddle point.
\end{enumerate}

\end{document}


\subsection*{Binary boolean operators}

\subsubsection*{\href{https://source-academy.github.io/sicp/chapters/1.1.6.html\#p4}{Conjunction}}

\[
\textit{expression}_1 \ \textbf{\texttt{\&\&}} \ \textit{expression}_2
\]
stands for
\[
\textit{expression}_1 \ \textbf{\texttt{?}} \ \textit{expression}_2 \ \textbf{\texttt{:}}\ \textbf{\texttt{false}}
\]

\subsubsection*{\href{https://source-academy.github.io/sicp/chapters/1.1.6.html\#p4}{Disjunction}}

\[
\textit{expression}_1 \ \textbf{\texttt{||}} \ \textit{expression}_2
\]
stands for
\[
\textit{expression}_1 \ \textbf{\texttt{?}}\ \textbf{\texttt{true}}\  \textbf{\texttt{:}}\ \textit{expression}_2
\]


\subsection{Surfaces division}
\label{oct_sc:surface_division}
\paragraph{}
Mapping points back to NURBS surfaces in 3D can be extremely time consuming as there is no known close form mathematical solution.
Every point takes about ten to hundreds iterations before the nearest projection point on the NURBS surface is found, depending on the size of the projection surface.
However, in the problem that the proposed method is targeting, reasonably complex geometry is expected.
As a result, points projection back to such kind of NURBS surfaces may takes much more computational time than any others do and it may be necessary to find a more complicated but computational efficient algorithm other than the naive implementation.

\paragraph{}
One concept that can be utilized to improve the efficiency here is the ``divided and conquer''.
As the time complexity of the naive algorithm is $O(n^3)$ where $n$ is directly correlated to the order of the basis function and the number of control points used to describe the NURBS surfaces, dividing a surface into two generally will make the projection algorithm four times faster.
Consequently, breaking the origin NURBS surfaces into multiple smaller ones could be one of the practical practices.

\paragraph{}
Surfaces division can be performed by the help of knot insertion (Sec.~\ref{lr_sec:nurbs_knot_ins}).
Assuming a NURBS surface defined by two knot vectors\\
$
\Xi_1 = [-1, -1, -1, a_1, a_2, \dots, a_n , 1, 1, 1]
$\\
and
$
\Xi_2 = [-1, -1, -1, b_1, b_2, \dots, b_m, 1, 1, 1]
$.\\
Several knots will be inserted into these two vector so that all interior knots will repeated $p+1$ times and $p$ stands for the order of the NURBS basis function in that direction.
After knot insertion, the same NURBS surface will now be described by two new vectors\\
$
\Xi_1^\prime = [-1, -1, -1, a_1, a_1, a_1, a_2, a_2, a_2, \dots, a_n , 1, 1, 1]
$\\
and
$
\Xi_2^\prime = [-1, -1, -1, b_1, b_1, b_1, b_2, b_2, b_2, \dots, b_m, 1, 1, 1]
$.\\
Extraction then can be conducted by taking the sub-matrix from the generated control points matrix $P^\prime$ and weight matrix $w^\prime$.

\paragraph{}
Fig.~\ref{oct_fig:nurbs_division} shows a sub-division of breaking a cylinder surface into four smaller ones.

\begin{figure}[h!]
    \centering
    \begin{subfigure}[b]{0.4\linewidth}
        \centering
        \scalebox{0.27}{
            \includegraphics{octree/images/NURBSParent.png}
        }
        \caption{Original NURBS surface}
    \end{subfigure}
    \begin{subfigure}[b]{0.4\linewidth}
        \centering
        \scalebox{0.25}{
            \includegraphics{octree/images/NURBSChildren.png}
        }
        \caption{Subdivided child NURBS surfaces}
    \end{subfigure}
    \caption{NURBS surface subdivision}
    \label{oct_fig:nurbs_division}
\end{figure}


\section{Higher Order Derivatives}\label{sec:MultivariateHigherOrderDerivatives}

In single variable calculus we saw that the second derivative is often
useful: in appropriate circumstances it measures acceleration; it can
be used to identify maximum and minimum points; it tells us something
about how sharply curved a graph is. Not surprisingly, second
derivatives are also useful in the multi-variable case, but again not
surprisingly, things are a bit more complicated.

It's easy to see where some complication is going to come from: with
two variables there are four possible second derivatives. To take a
``derivative,'' we must take a partial derivative with respect to $x$
or $y$, and there are four ways to do it: $x$ then $x$, $x$ then $y$,
$y$ then $x$, $y$ then $y$.

\begin{example}{Second Derivatives}{SecondDerivatives}
Compute all four second derivatives of $f(x,y)=x^2y^2$.
\end{example}
\begin{solution}
Using an obvious notation, we get:
$$f_{xx}=2y^2\qquad f_{xy}=4xy\qquad f_{yx}=4xy\qquad f_{yy}=2x^2.$$
\end{solution}

You will have noticed that two of these are the same, the ``mixed
partials'' computed by taking partial derivatives with respect to both
variables in the two possible orders. This is not an accident---as
long as the function is reasonably nice, this will always be true.\index{mixed partials}

\begin{theorem}{Clairaut's Theorem}{clairaut}
If the mixed partial derivatives are
continuous, they are equal.\index{Clairaut's theorem}
\end{theorem}

\begin{example}{Mixed Partials}{MixedPartials}
Compute the mixed partials of $\ds f=xy/(x^2+y^2)$.
\end{example}
\begin{solution}
The mixed partial $f_{xy}$ is found by first taking the partial derivative with respect to $x$:
$$
f_x={y^3-x^2y\over(x^2+y^2)^2},
$$
then with respect to $y$:
$$
f_{xy}=-{x^4-6x^2y^2+y^4\over (x^2+y^2)^3}.
$$
We leave $f_{yx}$ as an exercise.
\end{solution}

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

\begin{enumialphparenastyle}

\begin{ex}
Let $\ds f=xy/(x^2+y^2)$; compute $f_{xx}$, $f_{yx}$, and $f_{yy}$.
\begin{sol}
$f_{xx}=(2x^3y-6xy^3)/(x^2+y^2)^3$,
$f_{yy}=(2xy^3-6x^3y)/(x^2+y^2)^3$
\end{sol}
\end{ex}

\begin{ex}
Find all first and second partial derivatives of
$x^3y^2+y^5$.
\begin{sol}
$f_x=3x^2y^2$, $f_y=2x^3y+5y^4$,
$f_{xx}=6xy^2$, $f_{yy}=2x^3+20y^3$, $f_{xy}=6x^2y$
\end{sol}
\end{ex}

\begin{ex}
Find all first and second partial derivatives of
$4x^3+xy^2+10$.
\begin{sol}
$f_x=12x^2+y^2$, $f_y=2xy$, \hfill\break
$f_{xx}=24x$, $f_{yy}=2x$, $f_{xy}=2y$
\end{sol}
\end{ex}

\begin{ex}
Find all first and second partial derivatives of
$x\sin y$.
\begin{sol}
$f_x=\sin y$, $f_y=x\cos y$, $f_{xx}=0$, $f_{yy}=-x\sin y$,
$f_{xy}=\cos y$
\end{sol}
\end{ex}

\begin{ex}
Find all first and second partial derivatives of
$\sin(3x)\cos(2y)$.
\begin{sol}
$\ds f_x=3\cos(3x)\cos(2y)$,\hfill\break
$\ds f_y=-2\sin(3x)\sin(2y)$,\hfill\break
$\ds f_{xy}=-6\cos(3x)\sin(2y)$,\hfill\break
$\ds f_{yy}=-4\sin(3x)\cos(2y)$,\hfill\break
$\ds f_{xx}=-9\sin(3x)\cos(2y)$
\end{sol}
\end{ex}

\begin{ex}
Find all first and second partial derivatives of
$e^{x+y^2}$.
\begin{sol}
$\ds f_x=e^{x+y^2}$, $\ds f_y=2ye^{x+y^2}$,\hfill\break
$\ds f_{xx}=e^{x+y^2}$,\hfill\break
$\ds f_{yy}=4y^2e^{x+y^2}+2e^{x+y^2}$,\hfill\break
$\ds f_{xy}=2ye^{x+y^2}$
\end{sol}
\end{ex}

\begin{ex}
Find all first and second partial derivatives of
$\ln\sqrt{x^3+y^4}$.
\begin{sol}
$\ds f_x={3x^2\over2(x^3+y^4)}$,
$\ds f_y={2y^3\over x^3+y^4}$,
$\ds f_{xx}={3x\over x^3+y^4}-{9x^4\over 2(x^3+y^4)^2}$,
$\ds f_{yy}={6y^2\over x^3+y^4}-{8y^6\over (x^3+y^4)^2}$,\hfill\break
$\ds f_{xy}={-6x^2y^3\over (x^3+y^4)^2}$
\end{sol}
\end{ex}

\begin{ex}
Find all first and second partial derivatives of
$z$ with respect to $x$ and $y$ if
$x^2+4y^2+16z^2-64=0$.
\begin{sol}
$\ds z_x={-x\over16z}$,
$\ds z_y={-y\over4z}$,\hfill\break
$\ds z_{xx}=-{16z^2+x^2\over16^2z^3}$,\hfill\break
$\ds z_{yy}=-{4z^2+y^2\over16z^3}$,\hfill\break
$\ds z_{xy}={-xy\over64z^3}$
\end{sol}
\end{ex}

\begin{ex}
Find all first and second partial derivatives of
$z$ with respect to $x$ and $y$ if
$xy+yz+xz=1$.
\begin{sol}
$\ds z_x=-{y+z\over x+y}$,
$\ds z_y=-{x+z\over x+y}$,\hfill\break
$\ds z_{xx}=2{y+z\over(x+y)^2}$,
$\ds z_{yy}=2{x+z\over(x+y)^2}$,\hfill\break
$\ds z_{xy}={2z\over(x+y)^2}$
\end{sol}
\end{ex}

\begin{ex}
Let $\alpha$ and $k$ be constants.  Prove that the function
$u(x,t)=e^{-\alpha^2k^2t}\sin(kx)$
is a solution to the heat equation $u_t=\alpha^2u_{xx}$
\end{ex}

\begin{ex}
Let $a$ be a constant.  Prove that $u=\sin(x-at)+\ln(x+at)$ is
  a solution to the wave equation $u_{tt}=a^2u_{xx}$.

%% \exercise Let $f(x,y)$ be a continuous differentiable function.  Analyze
%%   the level curves near a critical value if that critical value is a
%%   max or a min.  What if the level curve is a saddle point?
\end{ex}

\begin{ex}
How many third-order derivatives does a function of 2 variables
  have?  How many of these are distinct?
\end{ex}

\begin{ex}
How many $n$th order derivatives does a function of 2 variables
  have?  How many of these are distinct?
\end{ex}

\end{enumialphparenastyle}


%% Chapter 7 : Generation Forecasting using ARIMA

\section{Introduction to Time Series Analysis}
\
\
\
\
Time series of data is a time ordered sequence of data-points of a particular variable. The data-points in a time series are measured/recorded/logged at successive equally spaced points in time. The time series analysis consists of methods to extract meaningful statistical and other characteristic of the data. Forecasting and Regression are two different methods employed in time series analyses;  where forecasting is concerned with developing a model which can predict future values of time series based on its previous values, and regression is concerned with developing models which give relationships between  current values of one or time series.\\

The application of time series has a large spectrum of areas, some of the are listed below:

\begin{itemize}

\item Statistics

\item Signal Processing and Communication Engineering

\item Econometrics and Mathematical Finance

\item Weather Forecasting and Earthquake Prediction

\end{itemize}

\section{Time Series Models}
\
\
\
\
There are primarily four time series forecasting models based on the conditional mean and stationarity assumption : AR, MA, ARMA and ARIMA which are discusse in the following sections.

\subsection{AR Model}
\
\
\
\
The AR model is Auto-Regressive Model. It models a certain time-varying univariate data series such that its outputs depend linearly on its own previous values and on a stochastic term. It is a special case of the ARMA and ARIMA models. Also, it can have a unit root (that is the series becomes stationary after taking one difference) i.e. it is not always stationary. The eq (\ref{AR1},\ref{AR2},\ref{AR3},\ref{AR4}) describe the AR Model.

\begin{equation}
\label{AR1}
y_{t} = c + \Phi_{1}y_{t-1} + ... + \Phi_{p}y_{t-p} + \varepsilon_{t^{'}}
\end{equation}\\
where,\\
$ y_{t} $ = Univariate Data Series \\
$ c $ = Model Constant  \\
$ \varepsilon_{t^{'}} $ = Unpredictable Part of the Series  \\

\begin{equation}
\label{AR2}
L^{i}y_{t} = y_{t-i}
\end{equation}\\
$ L^{i} $ = Lagged Operator of Degree $ i $

\begin{equation}
\label{AR3}
\Phi(L) = ( 1 - \Phi_{1}L - ... - \Phi_{p}L^{p} )
\end{equation}\\
where,\\
$ \Phi(L) $ = Lag Operator Polynomial of the AR Process\\

\begin{equation}
\label{AR4}
\Phi(L)y_{t} = c + \varepsilon_{t^{'}}
\end{equation}

The Fig (\ref{figc7h1}) illustrates a time series of a data variable which can be modeled as an AR process.

\begin{figure}[H]
\centering
\includegraphics[scale=0.1]{ARIMAImg1}
\caption{An Data Series with AR Process}
\label{figc7h1} %% to refer use, \ref{}
\end{figure}

\subsection{MA Model}
\
\
\
\
The MA model is Moving Average Model.It models a certain time-varying univariate data series such that its outputs depend linearly on the current and previous values of the stochastic term. It is a special case of the ARMA and ARIMA models.
The eq (\ref{MA1},\ref{MA2},\ref{MA3}) describe the MA Model.

\begin{equation}
\label{MA1}
y_{t}= c + \varepsilon_{t} + \theta_{1}\varepsilon_{t-1} + ... + \theta_{q}\varepsilon_{t-q^{'}}
\end{equation}\\
where,\\
$ \varepsilon_{t}, varepsilon_{t-q} $ = Current and past errors of the series \\

\begin{equation}
\label{MA2}
\theta(L) = ( 1 + \theta_{1}L + ... + \theta_{p}L^{p} )
\end{equation}\\
where,\\
$ \theta(L) $ = Lag Operator Polynomial of the MA Process\\

\begin{equation}
\label{MA3}
y_{t} = \mu + \theta\varepsilon_{t^{'}}
\end{equation}\\
$ \mu $ = It is the unconditional mean of the MA process

The Fig (\ref{figc7h2}) illustrates a time series of a data variable which can be modeled as an MA process.

\begin{figure}[H]
\centering
\includegraphics[scale=1]{ARIMAImg2}
\caption{An Data Series with MA Process}
\label{figc7h2} %% to refer use, \ref{}
\end{figure}

\subsection{ARMA Model}
\
\
\
\
The ARMA model is the Auto-Regressive Moving Average Model. It consists of both the AR and the MA part. It is used to model more complex univariate data series. Hence, the output of the ARMA model depends on both the previous data values, and the current and previous stochastic term. AR and MA models are its special case.
The eq (\ref{ARMA1},\ref{ARMA2}) describe the ARMA Model.

\begin{equation}
\label{ARMA1}
y_{t}= c + \Phi_{1}y_{t-1} + ... + \Phi_{p}y_{t-p} + \varepsilon_{t} +  \theta_{1}\varepsilon_{t-1} + ... + \theta_{q}\varepsilon_{t-q^{'}}
\end{equation}\\

\begin{equation}
\label{ARMA2}
\Phi(L)y_{t} = c + \theta\varepsilon_{t^{'}}
\end{equation}\\

The Fig (\ref{figc7h3}) illustrates a time series of a data variable which can be modeled as an ARMA process.

\begin{figure}[H]
\centering
\includegraphics[scale=1]{ARIMAImg3}
\caption{An Data Series with ARMA Process}
\label{figc7h3} %% to refer use, \ref{}
\end{figure}

\subsection{ARIMA Model}
\
\
\
\
The ARIMA model is the Auto-Regressive Moving Average Model. it is a generalization of the ARMA model, and it can model even non-stationary univariate data series by means of using differencing to convert non-stationary series to stationary series. It consists of both the AR and the MA part similar to the ARMA model. Hence, the output of the ARIMA model depends on both the previous data values, and the current and previous stochastic term. ARMA, AR and MA models are its special case.
The eq (\ref{ARIMA1},\ref{ARIMA2}) describe the ARMA Model.

\begin{equation}
\label{ARIMA1}
\bigtriangleup^{D}y_{t}= c + \Phi_{1}\bigtriangleup^{D}y_{t-1} + ... + \Phi_{p}\bigtriangleup^{D}y_{t-p} + \varepsilon_{t} +  \theta_{1}\bigtriangleup^{D}\varepsilon_{t-1} + ... + \theta_{q}\bigtriangleup^{D}\varepsilon_{t-q^{'}}
\end{equation}\\
where,\\
$ \bigtriangleup^{D} $ = It is the difference operator of degree D

\begin{equation}
\label{ARIMA2}
\Phi(L)(1-L^{D})y_{t} = c + \theta\varepsilon_{t^{'}}
\end{equation}\\

The Fig (\ref{figc7h4}) illustrates a time series of a data variable which can be modeled as an ARIMA process.

\begin{figure}[H]
\centering
\includegraphics[scale=0.75]{ARIMAImg4}
\caption{An Data Series with ARIMA Process}
\label{figc7h4} %% to refer use, \ref{}
\end{figure}

\section{Model Identification}
\
\
\
\
The first step in time series forecasting is to identify which time series forecasting model described above would best describe the given data series. The concept of Stationarity, the ACF (Auto-Correlation Function), and the PACF (Partial Auto-Correlation Function) helps us in making an intelligent guess for the best model to be used to forecast the given time series data.

\subsection{Concept of Stationarity}
\
\
\
\
The Fig (\ref{figc7h5}) shows a stationary data time series, it can be seen that visually a stationary process seems to be like white noise with no certain pattern whatsoever. If a data time series is stationary the we can rule out ARIMA and focus on ARMA, AR and MA models which could best describe the series.

\begin{figure}[H]
\centering
\includegraphics[scale=1]{ARIMAImg5}
\caption{A Stationary Time Series}
\label{figc7h5} %% to refer use, \ref{}
\end{figure}

However, if the data time series is non-stationary i.e. it shows some kind of a pattern as illustrated in the Fig (\ref{figc7h6}), then the series has to be modeled as an ARIMA process.

\begin{figure}[H]
\centering
\includegraphics[scale=0.5]{ARIMAImg6}
\caption{Types of Non-Stationary Time Series}
\label{figc7h6} %% to refer use, \ref{}
\end{figure}

The different components of a non-stationary time series are described as follows:

\begin{itemize}

\item \textbf{Trend Component:} A trend exists when there is a long-term increase or decrease in the data. It does not have to be linear. Sometimes we will refer to a trend “changing direction” when it might go from an increasing trend to a decreasing trend. It is illustrated in the Fig (\ref{figc7h6})

\item \textbf{Seasonal Component:} A seasonal pattern exists when a series is influenced by seasonal factors (e.g., the quarter of the year, the month, or day of the week). Seasonality is always of a fixed and known period. It is illustrated in the Fig (\ref{figc7h6})

\item \textbf{Cyclic Component:} A cyclic pattern exists when data exhibit rises and falls that are not of fixed period. The duration of these fluctuations is usually of at least 2 years. It is illustrated in the Fig (\ref{figc7h6})

\end{itemize}

However, real data series may contain one or all of these  components as illustrated in the Fig (\ref{figc7h6}) with a data series having both trend and seasonal pattern.\\

The Dickey-Fuller Test and the Augmented Dickey-Fuller Test are a good measure of stationarity of a time series data.

\subsection{ACF and PACF}
\
\
\
\

Autocorrelation Function (ACF), also known as serial correlation, is the correlation of a signal with itself at different points in time. Informally, it is the similarity between observations as a function of the time lag between them. It is a mathematical tool for finding repeating patterns, such as the presence of a periodic signal obscured by noise, or identifying the missing fundamental frequency in a signal implied by its harmonic frequencies. .\\

The partial autocorrelation function (PACF) gives the partial correlation of a time series with its own lagged values, controlling for the values of the time series at all shorter lags. It contrasts with the autocorrelation function, which does not control for other lags.\\

The Fig (\ref{figc7h7}) shows the ACF and PACF plots for a first order AR and a first order MA process.

\begin{figure}[H]
\centering
\includegraphics[scale=0.5]{ARIMAImg7}
\caption{ACF and PACF Graphs for AR and MA Processes}
\label{figc7h7} %% to refer use, \ref{}
\end{figure}

Both ACF and PACF are indispensable tools in selection of the best time series model for time series data forecasting. Some basic thumb rules for interpreting the ACF and PACF plots are given in the Table (\ref{ARIMATab1})

\begin{table}[H]
  \centering
  \caption{Nature of ACF and PACF for Different Time Series Models}
    \begin{tabular}{|l|l|l|}
    \hline
    \multicolumn{1}{|c|}{\textbf{Model}} & \multicolumn{1}{c|}{\textbf{ACF}} & \multicolumn{1}{c|}{\textbf{PACF}} \bigstrut\\
    \hline
    AR(p) & Tails off gradually & Cuts off after p lags \bigstrut\\
    \hline
    MA(q) & Cuts off after q lags & Tails off gradually \bigstrut\\
    \hline
    ARMA(p,q) & Tails off gradually & Tails off gradually \bigstrut\\
    \hline
    \end{tabular}%
  \label{ARIMATab1}%
\end{table}%

The Ljung-Box Q Test is an excellent statistical tool to measure the amount of correlation present between different lags of the ACF and PACF plots. It helps us in identifying the time series models more effectively.

\section{Model Estimation and Fitness}
\
\
\
\
After completing the model identification, usually two to three variations of the model identified are created for being more certain of our choice. In estimation of the models the co-efficients of the AR, MA, ARMA and ARIMA model (depending on the forecasters choice) equations are calculated.\\

Using these complete equations, model statistics are computed for each of the estimated model. The standardized residuals are of great importance in determining the best model to be selected for forecasting. For a good estimated model its standardized residuals should represent white noise i.e. there is no correlation whatsoever in the residuals. Further more the AIC (Akalike Information Criterion) and the BIC (Bayesian Information Criterion) are quantitative measure for better model estimation, the model with least AIC and BIC values is the best esitmated model.\\

Once the decision is made using above criteria, the best model is then used for forecasting the time series data.\\

\section{Process Flow for Forecasting using ARIMA Model}
\
\
\
\
The Fig (\ref{figc7ARIMAFlow}) shows the schematic of the process flow for the development of the ARIMA model for forecasting.

\begin{figure}[H]
\centering
\includegraphics[scale=0.8]{ARIMA_Flow}
\caption{Schematic of ARIMA Model Development for Forecasting}
\label{figc7ARIMAFlow} %% to refer use, \ref{}
\end{figure}

\section{Results}
\
\
\
\
The results for generation forecasting using ARIMA have been produced for the GSEC 1MW SPVP. The data utisiled for estimating the ARIMA coefficients is the WRF weather variables generated for the same plant for the month of June 2016 (The WRF results will be discussed in Chapter 9). The forecasted weather variables form the ARIMA models have been fed as inputs to the solar energy estimation app to generate the intra-hour generation forecast.\\

\subsection{GSEC 1MW SPVP - Plant Information}
\
\
\
\
The Table (\ref{AnnAppTab1}) gives the site and PV module information of the GSEC 1MW SPVP.

\begin{table}[H]
  \centering
  \caption{GSEC 1MW SPVP Plant Information Table}
    \begin{tabular}{|l|c|c|c|c|c|}
    \hline
    \multicolumn{6}{|c|}{\textbf{GSEC 1MW SVPP}} \bigstrut\\
    \hline
    \multicolumn{6}{|c|}{\textbf{SITE INFORMATION}} \bigstrut\\
    \hline
    \textbf{LATITUDE} & \multicolumn{5}{c|}{23.275} \bigstrut\\
    \hline
    \textbf{LONGITUDE} & \multicolumn{5}{c|}{72.682} \bigstrut\\
    \hline
    \textbf{PLANT CAPACITY (MW)} & \multicolumn{5}{c|}{1} \bigstrut\\
    \hline
    \multicolumn{6}{|c|}{\textbf{PV MODULE INFORMATION}} \bigstrut\\
    \hline
    \textbf{DESCRIPTION} & A-si & Cdte & Cigs & Multi & Mono \bigstrut\\
    \hline
    \textbf{MAKE} & Du-Pont & First Solar & Q Cells & Lanco & Lanco \bigstrut\\
    \hline
    \textbf{CRYSTALLINE/THIN-FILM} & Thin-Film & Thin-Film & Thin-Film & Crystalline & Crystalline \bigstrut\\
    \hline
    \textbf{RATING (W)} & 107 & 85 & 95 & 235 & 250 \bigstrut\\
    \hline
    \textbf{Vmpp (V)} & 74 & 46.4 & 62.1 & 29.56 & 31.15 \bigstrut\\
    \hline
    \textbf{Impp (A)} & 1.44 & 1.83 & 1.53 & 7.95 & 8.034 \bigstrut\\
    \hline
    \textbf{Voc (V)} & 99 & 60.5 & 78 & 37.17 & 38.04 \bigstrut\\
    \hline
    \textbf{Isc (A)} & 1.81 & 1.94 & 1.68 & 8.4 & 8.712 \bigstrut\\
    \hline
    \textbf{TEMP COEFF OF Voc} & -0.3 & -0.27 & -0.29 & -0.31 & -0.33 \bigstrut\\
    \hline
    \textbf{TEMP COEFF OF Isc} & 0.09 & 0.04 & 0.04 & 0.06 & 0.036 \bigstrut\\
    \hline
    \textbf{TEMP COEFF OF Pmp} & -0.25 & -0.25 & -0.38 & -0.43 & -0.47 \bigstrut\\
    \hline
    \textbf{TOTAL MODULES} & 924 & 1170 & 1056 & 432 & 405 \bigstrut\\
    \hline
    \textbf{LENGTH (mm)} & 1436 & 1200 & 1196 & 1360 & 1360 \bigstrut\\
    \hline
    \textbf{BREADTH (mm)} & 1117 & 600 & 636 & 941 & 951 \bigstrut\\
    \hline
    \textbf{AREA (m2)} & 1.604012 & 0.72 & 0.760656 & 1.27976 & 1.29336 \bigstrut\\
    \hline
    \end{tabular}%
  \label{AnnAppTab1}%
\end{table}%

\subsection{ARIMA Results}
\
\
\
\
The ARIMA weather variable forecasted outputs using ARIMA models in comparison with the original data series used are given in the Fig (\ref{figc7ARIMAWs},\ref{figc7ARIMAT},\ref{figc7ARIMAIr}).

\begin{figure}[H]
\centering
\includegraphics[scale=0.8]{ARIMA_Ws}
\caption{Comparison of Wind Speed Prediction using ARIMA with Original Data Series obtained from WRF }
\label{figc7ARIMAWs} %% to refer use, \ref{}
\end{figure}

\begin{figure}[H]
\centering
\includegraphics[scale=0.8]{ARIMA_T}
\caption{Comparison of Temperature Prediction using ARIMA with Original Data Series obtained from WRF }
\label{figc7ARIMAT} %% to refer use, \ref{}
\end{figure}

\begin{figure}[H]
\centering
\includegraphics[scale=0.8]{ARIMA_Ir}
\caption{Comparison of Irradiance Prediction using ARIMA with Original Data Series obtained from WRF }
\label{figc7ARIMAIr} %% to refer use, \ref{}
\end{figure}

The energy generation forecast for the GSEC plant is computed using the ARIMA forecasted weather variables using the solar energy estimation application. The forecast is generated from 10.25 (Decimal Time), $4^{th}$ June, 2016 to 10.00 (Decimal Time), $5^{th}$ June, 2016. The weather data series of WRF used are from 0.00 (Decimal Time), $1^{st}$ June, 2016 to 10.00 (Decimal Time), $4^{th}$ June, 2016.

\begin{figure}[H]
\centering
\includegraphics[scale=0.8]{ARIMA_En}
\caption{Comparison of Energy Prediction using ARIMA with Original Data Series obtained from WRF }
\label{figc7ARIMAEn} %% to refer use, \ref{}
\end{figure}

\newpage

\subsection{Conclusion from Graphs}

All the ARIMA models used are of seasonal type with seasonality of 96 as the data series has a resolution of 15 minutes. Moreover, it has been observed that the ARIMA models with both AR and MA components forecast better. All the original data series have been differenced twice. From the Fig (\ref{figc7ARIMAWs},\ref{figc7ARIMAT},\ref{figc7ARIMAIr},\ref{figc7ARIMAEn}), we see that the ARIMA models have been able to predict the weather variables and the energy output with a good degree of accuracy.


\section{Kernel k-means, Spectral Clustering and Normalized Cut}
\label{ch:dhillon04}

\textit{Kernel k-means, Spectral Clustering and Normalized Cut} by Inderjit S. Dhillon.
Cited by 552. \textit{ACM SIGKDD international conference on Knowledge discovery and data mining}
\newline

\textbf{Main point} is that the paper \begin{inparaenum}[\itshape a\upshape)]
\item shows weighted kernel k-means formulation is very general so spectral clustering is a special case of k-means objective-function,
\item found k-means algorithm that decreases the normalized cut given weight and a kernel matrix.
\end{inparaenum}

\subsection{Weighted Kernel k-means}
Let us denote a weight for each point $a$ by $w(a)$, clusters by $\pi_j$, and a partitioning of points as $\{\pi_j\}_{j=1}^k$. Using using non-linear function $\phi$, the objective function of weighted kernel k-means is defined as :

\begin{figure}[ht]
\begin{mdframed}
$ D(\{ \pi_j \}_{j=1}^k) = \sum\limits_{j=1}^k \sum\limits_{a \in \pi_j} w(a)\| \phi(a) - m_j \|^2 $ (1) \\
where $m_j = \frac{\sum_{b \in \pi_j} w(b) \phi(b)}{ \sum_{b \in \pi_j } w(b) } $
\end{mdframed}
\caption{Objective function of weight kernel k-means}
\end{figure}

\begin{figure}[ht]
\begin{mdframed}
$ (\phi(a) - \frac{\sum_{b \in \pi_j} w(b)\phi(b)}{\sum_{b \in \pi_j} w(b) })^2$  (2)
\end{mdframed}
\caption{Distrance function from $\phi(a)$ to center $m_j$}
\end{figure}

\begin{figure}[ht]
\begin{mdframed}
WeightedKernelKMeans( $ K, k, w, C_1, \cdots, C_k $ ) \\
Input : K: kernel matrix, k: number of clusters, w: weights for each point \\
Output : $C_1, \cdots, C_k$ : partitioning of the points \\
1. Initialize the k clusters: $C_1^{(0)}, \cdots, C_k^{(0)}$. \\
2. set = $t=0$. \\
3. For each point $a$, find its new cluster index as \\
$j^{*}(a) = \operatorname*{arg\,min}_j \| \phi(a) - m_j \|^2 $ using (2) \\
4. Compute the updated clusters as \\
$C_j^{t+1} = \{ a : j^{*}(a) = j \}$ \\
5. If not converged, set $t = t + 1$ and go to Step 3, otherwise stop.
\end{mdframed}
\caption{weighted kernel k-means}
\end{figure}

\subsection{Spectral connection}
Minimization of the objective function in (1) is equivalent to the maximization of trace($Y^T W^{1/2} K W^{1/2} Y)$. We can obtain an optimal $Y$ by taking the top $k$ eigenvectors of $W^{1/2}K W^{1/2}$. It shows Kernel k-means and spectral clustering have a theoretical connection.


\documentclass[11pt]{scrartcl}
\usepackage{atonu}
\usepackage{fullpage}
\usepackage{amsmath,amsthm, amsfonts}

\begin{document}
\title{Order, Primitive Roots and Quadratic Residue}
\subtitle{BdMO National Camp 2021}
\author{\scshape{Atonu Roy Chowdhury} \\
\mailto{atonuroychowdhury@gmail.com}}
\date{\today}
\maketitle

\section{Order!}
Let's recall two of the theorems that we've seen before.
\begin{theorem}[Fermat's Little Theorem and Euler's Theoem]
Let \(p\) be a prime number and \(a\) be an integer coprime with \(p\). Then
\[a^{p-1}\equiv 1 \amod{p}\]
More generally, if \(m\) and \(a\) are positive integers with \(\gcd(a,m) =1\), then
\[a^{\varphi(m)} \equiv 1 \amod{m}\]
\end{theorem}
That is, if you take the sequence \(a^1, a^2, a^3, \ldots\) in \(\text{mod }m\), then the sequence eventually reaches \(1\) at \(a^{\varphi(m)}\), and thus it becomes periodic. But, does the sequence reach \(1\) before \(\varphi(m)\)?\\
The answer is yes. You can easily find examples. One simple example is: taking \(a=2\) and \(m=7\). Then \(\varphi(m)=\varphi(7)=6\), but \(2^3 \equiv 1 \amod{7}\). So \(6\) is not the smallest \(n\) such that \(2^n \equiv 1 \amod{7}\). We shall call this smallest such \(n\) ``order''.
\begin{defn}[Order modulo \(m\)]
Let \(a\) and \(m\) be coprime positive integers. Then the order of \(a\) modulo \(m\), denoted by \(\ord_m(a)\) is defined as follows:
\[\ord_m(a) := \min \left\{n\in\NN : a^n \equiv 1 \amod{m}\right\}\]
That means, if \(\ord_m(a) = d\), then \(a^d \equiv 1 \amod{m}\) and for every positive integer \(k\) smaller than \(d\), we have \(a^k \not\equiv 1 \amod{m}\).
\end{defn}
Now a natural question arises: is there some formula using which we can calculate order? Sadly, the answer is no. But hey, don't get frustrated. You don't really have to check every integer from \(1\) to \(\varphi(m)\). To reduce your hardwork, there comes our next theorem. Evan Chen named it ``Fundamental Theorem of Orders'', so I'm keeping the name.
\begin{theorem}[Fundamental Theorem of Orders]
\(a^N \equiv 1 \amod{m}\) if and only if \(\ord_m(a) \mid N\).
\end{theorem}
\begin{proof}
Let \(\ord_m(a)=d\). The if direction is trivial. If
\[d \mid N \implies N = dq \implies a^N \equiv a^{dq} \equiv \left(a^d\right)^q \equiv 1^q \equiv 1 \amod{m} \]
For the other direction, we just need to use the division algorithm to arrive at contradiction. Assume for the sake of contradiction that \(d \nmid N\). That means, \(N\) leaves some non-zero remainder upon division by \(d\). So \(N = dq+r\), where \(0<r<d\). Now,
\[1 \equiv a^N \equiv a^{dq+r} \equiv \left(a^d\right)^q a^r \equiv 1^q a^r\equiv a^r \amod{m} \implies \boxed{a^r \equiv 1 \amod{m}} \]
By definition of order, for every positive integer \(k\) smaller than \(d\), we have \(a^k \not\equiv 1 \amod{m}\). Here, \(r\) is a positive integer smaller than \(d\), but \(a^r \equiv 1 \amod{m}\). Thus we arrive at a contradiction. Hence \(d\) must divide \(N\).
\end{proof}
This theorem immediately gives us a corollary.
\begin{corollary}
If \(\gcd(a,m) = 1\), then \(\ord_m(a) \mid \varphi(m)\)
\end{corollary}
Now we shall see an application of the \textit{Fundamental Theorem of Orders}.
\begin{lemma}
Let \(d= \ord_m(a)\). Then \(a^x \equiv a^y \amod{m}\) if and only if \(x \equiv y \amod{d}\).
\end{lemma}
\begin{proof}
WLOG, we can assume that \(x \geq y\). Order is defined only when \(\gcd(a,m)=1\). Therefore, we can actually divide both sides of the modular equation by \(a^y\).
\[a^x \equiv a^y \amod{m} \iff a^{x-y} \equiv 1 \amod{m} \iff d \mid x-y \iff x \equiv y \amod{d}\]
Thus, we are done.
\end{proof}
Alright, time for a fun exercise.
\begin{exercise}
Let \(a\) and \(n\) be coprime integers. Show that \(n \mid \varphi(a^n - 1)\)
\end{exercise}
\begin{soln}
Let \(N = a^n - 1\). Obviously \(\gcd(a,N) = \gcd(a, a^n - 1) = 1\). Then by \textit{Corollary 1.3},
\[\ord_N(a) \mid \varphi(N)\]
If we can show that \(\ord_N(a) = n\), then we are basically done. It's actually not hard at all to show. Obviously, \(a^n \equiv 1 \amod N\). Now,
\[0<k<n \implies a^k - 1 < a^n - 1 \implies N \nmid a^k -1 \implies a^k \not\equiv 1 \amod N\]
Therefore, \(\ord_N(a) = n\) and we are done.
\end{soln}
\pagebreak

\section{Primitive Roots}
We've seen a few example in the Orders section that \(\phi(m)\) may not be the smallest positive integer to raise power such that it becomes \(1\) modulo \(m\). But occasionally it is the smallest such positive integer. That's when things start getting interesting.
\begin{defn}[Primitive Root]
An integer \(g\) is said to be a primitive root modulo \(n\) if \(\gcd(g,n) = 1\) and \[\ord_n(g) = \varphi(n)\]
\end{defn}
Notice that primitive roots might not always exist. Also, even if they do, they need not be unique. So, what's interesting about primitive roots? Well, they exist when we need them the most.
\begin{theorem}
Let \(p\) be a prime number. Then there exists a primitive root modulo \(p\).
\end{theorem}
This theorem has a stronger generalization. We don't need it now.
\begin{proof}
Before jumping into the proof, we need a lemma. I'll leave the proof of the lemma as an exercise for the reader.
\begin{lemma}
For every positive integer \(n\),
\[\sum_{d \mid n} \varphi(d) = n\]
That is, a number is exactly equal to the sum of its divisor's \(\varphi\).
\end{lemma}
We know that \(a^{p-1} \equiv 1 \amod p\), and hence \(\ord_p(a)\) is a divisor of \(p-1\) for every \(a\) with \(1\leq a \leq p-1\). Let \(d \mid p-1\). For every such \(d\), we shall consider this set
\[S_d = \left\{ a \ : \ 1\leq a \leq p-1 \text{ and } \ord_p(a) = d\right\}\]
Notice that, if we take union of \(S_d\) over all the divisors of \(p-1\), we shall get the whole reduced residue system of \(p\). Furthermore, all these \(S_d\)'s are disjoint. Therefore,
\[ \bigcup_{d \mid p-1} S_d = RRS(p) \implies \boxed{\sum_{d \mid p-1} |S_d| = p-1} \]
We shall prove in the next class that, the number of solutions (modulo \(p\)) to \(x^d \equiv 1 \amod{p}\) is at most \(d\). The elements of the set \(X = \left\{ a, a^2, a^3, \ldots, a^d \right\} \) satisfies this modular equation and there are \(d\) different elements in \(X\). Therefore, \(S_d\) must be a subset of this \(X\).\\
Now we claim that, if \(a\in S_d\), then \(a^{i} \in S_d\) if and only if \(\gcd(i,d)=1\). To prove our claim, let \(\gcd(i,d) = g >1\) and \(d = gx, i=gy\) where \(x\) and \(y\) are coprime.
\[ b = a^{i} = a^{gy} \implies b^{x} = a^{gxy} = \left(a^d\right)^y \equiv 1 \amod{p}\]
Obviously \(x\) is smaller than \(d\) because \(g>1\). Thus \(b=a^{i}\) does not have order \(d\). It can be shown easily that, if \(\gcd(i,d) = 1\), then \(a^{i}\) has order \(d\), in other words \(a^{i} \in S_d\). \\
From this, we can conclude that, \(S_d = \left\{ a^{i} \ : \ \gcd(i,d)=1 \right\} \), and there are at most \(\varphi(d)\) such elements. Therefore, \(|S_d| \leq \varphi(d)\). Putting all the pieces of the puzzle together,
\[ p-1 = \sum_{d \mid p-1} \varphi(d) \geq \sum_{d \mid p-1} |S_d| = p-1\]
Therefore, we must have \(|S_d| = \varphi(d)\) for every \(d\). Hence, \(S_{p-1} = \varphi(p-1) >0\). So such element with order \(p-1\) exists.
\end{proof}
Not only have we showed that element with order \(p-1\) exists, we've also showed that there are \(\varphi(p-1)\) such elements with order \(p-1\).
\begin{lemma}
If \(g\) is a primitive root modulo \(m\) and \(\varphi(m)\) is even, then
\[g^{\frac{\varphi(m)}{2}} \equiv -1 \pmod{m}\]
\end{lemma}
\begin{proof}
We shall prove the case of \(m\) being prime here. The composite case can be shown using the stronger generalization of Theorem 2.1. \\
When \(m\) is prime, \(\varphi(m)=m-1=2n\). \(g\) is a primitive root modulo \(m\), so definitely \(\gcd(g,m)=1\). So
\[g^{2n} \equiv 1 \amod{m} \implies m \mid g^{2n}-1 = (g^n+1)(g^n-1) \implies g^n \equiv \pm 1 \amod{m}\]
If \(g^n \equiv 1 \amod{m}\), then we get \(\ord_m(g) < \varphi(m)\). Thus we arrive at a contradiction. Therefore, \(g^n \equiv 1 \amod{m}\)
\end{proof}
\begin{lemma}
If \(g\) is a primitive root modulo \(m\), then the set \(S = \left\{ g, g^2, g^3, \ldots, g^{\varphi(m)} \right\} \) is a reduced residue system modulo \(m\).
\end{lemma}
\begin{proof}
\(\gcd(g,m) = 1 \implies \gcd(g^{i},m) = 1\). Therefore, every element of \(S\) is coprime to \(m\). So all we need to show is every element of \(S\) is distinct modulo \(m\). \\
Assume for the sake of contradiction that there exists some \(a,b\) with \(g^{a} \equiv g^b \amod{m}\). WLOG, \(a>b\). Since \(g\) is coprime with \(m\), we can actually divide both sides by \(g^b\). Then we get,
\[g^{a-b} \equiv 1 \amod{m}\]
\(a\) and \(b\) lie between \(1\) and \(\varphi(m)\). So their difference should be strictly smaller than \(\varphi(m)\). Thus we get \(g\) has order smaller than \(\varphi(m)\), which contradicts with the fact that \(g\) is a primitive root modulo \(m\).
\end{proof}
Now we shall prove Wilson's theorem, but not the proof you usually see in textbooks. We shall prove it using primitive roots.
\begin{theorem}[Wilson's Theorem]
If \(p\) is a prime number, then
\[(p-1)! \equiv -1 \amod{p}\]
\end{theorem}
\begin{proof}
\(p=2\) is trivial, so we shall consider the case of \(p\) being odd prime.\\
\(p\) is a prime, so it has a primitive root, namely \(g\). By Lemma 2.4,
\[\left\{ g, g^2, g^3, \ldots, g^{p-1} \right\} \equiv \left\{ 1,2,3,\ldots, p-1 \right\} \amod{p}\]
If we multiply all these, we shall get,
\begin{equation*}
\begin{split}
(p-1)! &\equiv 1 \cdot 2 \cdot 3 \cdots (p-1) \\
&\equiv g \cdot g^2 \cdot g^3 \cdots g^{p-1} \\
&\equiv g^{1+2+3+\cdots+(p-1)} \\
&\equiv g^{\frac{(p-1)p}{2}} \\
&\equiv \left(g^{\frac{p-1}{2}}\right)^p \\
&\equiv (-1)^p \equiv -1 \amod{p}
\end{split}
\end{equation*}
Thus, we are done.
\end{proof}
\begin{theorem}[Generalization of Theorem 2.1]
Let \(n\) be a positive integer. A primitive root modulo \(n\) exists if and only if
\[n \in \left\{ 2, 4, p^k, 2p^k \right\} \]
where \(p\) is an odd prime number.
\end{theorem}
I'm not stating the proof here. I'm gonna added it as an exercise problem.

\pagebreak
\section{Quadratic Residue}
The word ``Quadratic'' suggests that it has something to do with squares, and from the word ``Residue'' you're probably guessing that we will probably work with remainders. Yes, it is what it sounds like. Basically ``Quadratic residue'' deals with the remainders of square numbers. Let's jump into definition.
\begin{defn}[Quadratic Residue]
Let \(m\) be a positive integer. An integer \(n\) is called a \textbf{quadratic residue modulo \(m\)} if there exists some \(x\) such that \(x^2 \equiv n \amod m\).
\end{defn}
For example, \(4\) is a quadratic residue modulo \(5\), because \(7^2 \equiv 4 \amod 5\). But \(3\) is not a quadratic residue modulo \(5\), because there does not exist any integer \(x\) such that \(x^2 \equiv 3 \amod{5}\).
\begin{defn}[Quadratic Residue Class]
Let \(m\) be a positive integer. The Quadratic Residue Class of \(m\), denoted by \(\qr(m)\), is defined as follows:
\[\qr(m) = \left\{ n : n \text{ is a quadratic reisude modulo }m \right\} \]
There is an equivalent definition:
\[\qr(m) = \left\{ x^2 \text{ mod } m \ : \ 0 \leq x \leq m-1 \right\} \]
\end{defn}
It's not that hard to see that \(x^2 \equiv (m-x)^2 \amod m\). As a result, the set \(\qr(m)\) will have at most \(\frac{m}{2}+1\) elements. Because two elements contribute to the same quadratic residue. \\
Now, let's talk about a fundamental result about quadratic residue and primes.
\begin{proposition}
Let \(p\) be an odd prime. If \(-1\) is a quadratic residue modulo \(p\), then \(p \equiv 1 \amod 4\)
\end{proposition}
\begin{proof}
Assume for the sake of contradiction that \(p \equiv 3 \amod 4\), and \(-1\) is a quadratic residue modulo \(p\). That is, there exists some positive integer \(x\) such that \(x^2 \equiv -1 \amod p\). \\
As \(p \equiv 3 \amod 4\), we can express \(p\) as \(4k+3\) form. Also, \(p \mid x^2 + 1\) gives us \(\gcd(x,p)=1\). Therefore, by \textit{Fermat's Little Theorem},
\[x^{p-1} \equiv 1 \amod{p} \implies 1 \equiv x^{4k+2} \equiv \left(x^2\right)^{2k+1} \equiv \left(-1\right)^{2k+1} \equiv -1 \amod{p} \]
which leads to a contradiction. Therefore, \(p \equiv 1 \amod 4\).
\end{proof}
However, this proposition does not necessarily imply that \(-1\) is a quadratic residue for every prime of the form \(4k+1\). But it can be shown easily that for every prime of such form, you can find a positive integer \(x\) with \(x^2 \equiv -1 \amod p\).
\begin{lemma}
If \(p\equiv 1 \amod 4\) is a prime, then there exists a positive integer \(x\) with \(x^2 \equiv -1 \amod p\).
\end{lemma}
\begin{proof}
We shall prove it by constructing such \(x\). The construction is motivated by \textit{Wilson's Theorem}. Wilson's theorem says that, if \(p\) is a prime, then \((p-1)! \equiv -1 \amod{p}\). We are given that \(p\) is a prime of the form \(4k+1\). Substituting this into Wilson's theorem, we get that
\[1 \cdot 2 \cdot 3  \cdots (2k) \cdot (2k+1) \cdots (4k-1) \cdot 4k \equiv -1 \amod p\]
Our main idea is to express the LHS as a square. How can we do it? Notice that,
\begin{equation*}
\begin{split}
4k &\equiv -1 \amod{p} \\
4k-1 &\equiv -2 \amod{p} \\
4k-2 &\equiv -3 \amod{p} \\
&\cdots \\
2k+2 &\equiv -(2k-1) \amod{p} \\
2k+1 &\equiv -2k \amod{p}
\end{split}
\end{equation*}
If we multiply all these, we would get,
\[(2k+1)(2k+2)\cdots (4k-2)(4k-1)4k \equiv 1 \cdot 2 \cdot 3 \cdots (2k-1)\cdot 2k \amod{p}\]
The negative signs got canceled out because an even number of negative numbers are multiplied. Now, if we substitute this into Wilson's theorem, we get
\[\left(1 \cdot 2 \cdot 3 \cdots (2k-1)\cdot 2k\right)\left(1 \cdot 2 \cdot 3 \cdots (2k-1)\cdot 2k\right) \equiv -1 \amod{p} \implies \left((2k)!\right)^2 \equiv -1 \amod{p}\]
Thus we have successfully constructed such \(x\). So we are done.
\end{proof}

I intend to discuss about quadratic residues more in the diophantine equations note. In this note, I wanna introduce about Legendre Symbol.
\begin{defn}[Legendre Symbol]
The Legendre symbol for a positive integer \(n\) and a prime \(p\) is denoted by \(\left(\dfrac{n}{p}\right) \) and defined as:
\[
\left(\dfrac{n}{p}\right) = \left\{ \begin{array}{ll}
0 & \quad \text{if } p \mid n \\
1 & \quad \text{if } n \in \qr(p) \\
-1 & \quad \text{if } n \not\in \qr(p)
\end{array}\right.
\]
\end{defn}
The definition is basically saying that, if \(p \mid n\), then \(\left(\dfrac{n}{p}\right)\) is \(0\). When \(p\nmid a\), we have two cases. If \(n\) is a quadratic residue modulo \(p\), then \(\left(\dfrac{n}{p}\right)\) is \(1\), otherwise it's \(-1\). \\
Now you may ask, ``\(0\) is always a quadratic residue modulo \(p\). \(p \mid n\) means \(n \equiv 0 \amod{p}\), so \(n\) is a quadratic residue modulo \(p\). Why didn't we put \(1\) as the value of \(\left(\dfrac{n}{p}\right)\)? Doesn't it make more sense to have \(1\) for \textbf{all} quadratic residues?'' Well, the definition is of Legendre Symbol has a greater purpose to serve other than denoting quadratic residue. That greater purpose is our next theorem.
\begin{theorem}[Euler's Criterion]
\[ \left(\dfrac{n}{p}\right) = n^{\frac{p-1}{2}} \ \text{ mod } p\]
\end{theorem}
\begin{proof}
If \(p\mid n\), then the result is trivial. So let's assume \(p\nmid n\). \\
If \(n\) is a quadratic residue, then \(n \equiv x^2 \amod p\). By Fermat's Little Theorem,
\[x^{p-1} \equiv 1 \amod{p} \implies \left(\dfrac{n}{p}\right) = 1 \equiv \left(x^2\right)^{\frac{p-1}{2}} \equiv n^{\frac{p-1}{2}} \amod{p}\]
Now, all we are left with is, whenever \(n\) is not a quadratic residue, \(n^{\frac{p-1}{2}} \equiv -1 \amod{p}\). We shall need Wilson's theorem for this. \\
Take any integer \(x\) with \(1 \leq x \leq p-1\). Take \(y = nx^{-1}\), where \(x^{-1}\) denotes the multiplicative inverse of \(x\) modulo \(p\)\footnote{This basically means that \(xx^{-1} \equiv 1 \amod{p}\)}. Therefore, we have
\[ xy \equiv x \amod p\]
Notice that, \(x\) and \(y\) can't be equal. Because if \(x\) and \(y\) are equal, then \(n\) becomes a quadratic residue modulo \(p\). \\
If we choose a different \(x\), we shall get a different \(y\). Thus, we can divide all the integers from \(1\) to \(p-1\) in \(\frac{p-1}{2}\) pairs of \((x,y)\). Let the pairs are \((x_1, y_1), (x_2, y_2), \ldots, (x_{\frac{p-1}{2}}, y_{\frac{p-1}{2}})\). Using Wilson's theorem,
\begin{equation*}
\begin{split}
-1 &\equiv (p-1)! \equiv 1 \cdot 2 \cdot 3 \cdots (p-1) \\
&\equiv (x_1y_1)\cdot (x_2y_2) \cdots (x_{\frac{p-1}{2}} y_{\frac{p-1}{2}}) \\
&\equiv n \cdot n \cdots n \\
&\equiv n^{\frac{p-1}{2}} \amod{p}
\end{split}
\end{equation*}
Thus, we are done.
\end{proof}
Legendre Symbol has some very nice properties. I'm not proving these, you should try to prove them yourself.
\begin{lemma}
The Legendre Symbol \(\left(\dfrac{n}{p}\right) \) has the following properties:
\begin{enumerate}[i.]
\item If \(p\nmid ab\), then \(\left(\dfrac{ab}{p}\right) = \left(\dfrac{a}{p}\right) \left(\dfrac{b}{p}\right)\)
\item \(\left(\dfrac{2}{p}\right) = (-1)^{\frac{p^2-1}{8}}\)\\
\item If \(p\) and \(q\) are distinct odd primes, then
\(\left(\dfrac{q}{p}\right)\left(\dfrac{p}{q}\right) = (-1)^{\frac{p-1}{2} \frac{q-1}{2}}\)
\end{enumerate}
\end{lemma}
The last one is known as ``The Law of Quadratic Reciprocity''.
\pagebreak

\section{Exercise Problems}
\begin{problem}
Find all positive integers \(n\) such that \(n \mid 2^n -1\).
\end{problem}
\begin{problem}
Find all pairs of prime numbers \(p,q\) such that \(pq \mid (5^p-2^p)(5^q-2^q)\).
\end{problem}
\begin{problem}
Find all triplets of prime numbers \(p,q,r\) such that
\[p \mid q^r+1 , \quad q\mid r^p +1 , \quad r \mid p^q+1\]
\end{problem}
\begin{problem}
Let \(p\ge2\) be a prime number. Find all positive integer \(k\) such that \(p\) divides
\[1^k + 2^k + 3^k +\cdots +(p-1)^k\]
\end{problem}
\begin{problem}
Let \(g\) be a primitive root modulo \(n\). Then \(g^m\) is also a primitive root modulo \(n\) if and only if \(m\) is relatively prime to \(\varphi(n)\).
\end{problem}
\begin{problem}
Let \(n\) be an odd positive integer. Show that there exists a primitive root modulo \(n\) if and only if there exists a primitive root modulo \(2n\)
\end{problem}
\begin{problem}
Let \(p\) be an odd prime and \(g\) be a primitive root modulo \(p\). Then either \(g\) or \(g+p\) is a primitive root modulo \(p^k\) for every \(k\ge 1\).
\end{problem}
\begin{problem}
If any exists, there are exactly \(\varphi(\varphi(n))\) primitive roots modulo \(n\).
\end{problem}
\begin{problem}
For each non-negative integer \(m\), let \(n_{m}=101 m-100 \cdot 2^{m} \). Let \(a, b, c, d\) be integers with \(0 \leq a, b, c, d \leq 99\) such that \[n_{a}+n_{b} \equiv n_{c}+n_{d} \amod{10100}\]
\end{problem}
\begin{problem}
Find all pairs of positive integers \((a, b)\) such that \(a b(a+b)\) is not divisible by 7 , but \((a+b)^{7}-a^{7}-b^{7}\) is divisible by \(7^{7}\).
\end{problem}
\begin{problem}
Find all pairs of positive integers \(x,y\) such that \(4xy-x-y\) is a perfect square.
\end{problem}
\begin{problem}
\(a,b\) are coprime positive integers and \(p\) is an odd prime number. If \(p \mid a^2 + b^2\), show that \(p\equiv 1 \amod{4}\)
\end{problem}
\begin{problem}
Find all triplets of positive integers \(a,b,c\) such that \(a^2+1=b(2^c-1)\)
\end{problem}
\begin{problem}
Prove Lemma 3.4
\end{problem}
\begin{problem}
Prove Theorem 2.6
\end{problem}

\end{document}


\section{\module{math} ---
         Mathematical functions}

\declaremodule{builtin}{math}
\modulesynopsis{Mathematical functions (\function{sin()} etc.).}

This module is always available.  It provides access to the
mathematical functions defined by the C standard.

These functions cannot be used with complex numbers; use the functions
of the same name from the \refmodule{cmath} module if you require
support for complex numbers.  The distinction between functions which
support complex numbers and those which don't is made since most users
do not want to learn quite as much mathematics as required to
understand complex numbers.  Receiving an exception instead of a
complex result allows earlier detection of the unexpected complex
number used as a parameter, so that the programmer can determine how
and why it was generated in the first place.

The following functions provided by this module:

\begin{funcdesc}{acos}{x}
Return the arc cosine of \var{x}.
\end{funcdesc}

\begin{funcdesc}{asin}{x}
Return the arc sine of \var{x}.
\end{funcdesc}

\begin{funcdesc}{atan}{x}
Return the arc tangent of \var{x}.
\end{funcdesc}

\begin{funcdesc}{atan2}{y, x}
Return \code{atan(\var{y} / \var{x})}.
\end{funcdesc}

\begin{funcdesc}{ceil}{x}
Return the ceiling of \var{x} as a real.
\end{funcdesc}

\begin{funcdesc}{cos}{x}
Return the cosine of \var{x}.
\end{funcdesc}

\begin{funcdesc}{cosh}{x}
Return the hyperbolic cosine of \var{x}.
\end{funcdesc}

\begin{funcdesc}{exp}{x}
Return \code{e**\var{x}}.
\end{funcdesc}

\begin{funcdesc}{fabs}{x}
Return the absolute value of the real \var{x}.
\end{funcdesc}

\begin{funcdesc}{floor}{x}
Return the floor of \var{x} as a real.
\end{funcdesc}

\begin{funcdesc}{fmod}{x, y}
Return \code{fmod(\var{x}, \var{y})}, as defined by the platform C library.
Note that the Python expression \code{\var{x} \%\ \var{y}} may not return
the same result.
\end{funcdesc}

\begin{funcdesc}{frexp}{x}
% Blessed by Tim.
Return the mantissa and exponent of \var{x} as the pair
\code{(\var{m}, \var{e})}.  \var{m} is a float and \var{e} is an
integer such that \code{\var{x} == \var{m} * 2**\var{e}}.
If \var{x} is zero, returns \code{(0.0, 0)}, otherwise
\code{0.5 <= abs(\var{m}) < 1}.
\end{funcdesc}

\begin{funcdesc}{hypot}{x, y}
Return the Euclidean distance, \code{sqrt(\var{x}*\var{x} + \var{y}*\var{y})}.
\end{funcdesc}

\begin{funcdesc}{ldexp}{x, i}
Return \code{\var{x} * (2**\var{i})}.
\end{funcdesc}

\begin{funcdesc}{log}{x}
Return the natural logarithm of \var{x}.
\end{funcdesc}

\begin{funcdesc}{log10}{x}
Return the base-10 logarithm of \var{x}.
\end{funcdesc}

\begin{funcdesc}{modf}{x}
Return the fractional and integer parts of \var{x}.  Both results
carry the sign of \var{x}.  The integer part is returned as a real.
\end{funcdesc}

\begin{funcdesc}{pow}{x, y}
Return \code{\var{x}**\var{y}}.
\end{funcdesc}

\begin{funcdesc}{sin}{x}
Return the sine of \var{x}.
\end{funcdesc}

\begin{funcdesc}{sinh}{x}
Return the hyperbolic sine of \var{x}.
\end{funcdesc}

\begin{funcdesc}{sqrt}{x}
Return the square root of \var{x}.
\end{funcdesc}

\begin{funcdesc}{tan}{x}
Return the tangent of \var{x}.
\end{funcdesc}

\begin{funcdesc}{tanh}{x}
Return the hyperbolic tangent of \var{x}.
\end{funcdesc}

Note that \function{frexp()} and \function{modf()} have a different
call/return pattern than their C equivalents: they take a single
argument and return a pair of values, rather than returning their
second return value through an `output parameter' (there is no such
thing in Python).

The module also defines two mathematical constants:

\begin{datadesc}{pi}
The mathematical constant \emph{pi}.
\end{datadesc}

\begin{datadesc}{e}
The mathematical constant \emph{e}.
\end{datadesc}

\begin{seealso}
  \seemodule{cmath}{Complex number versions of many of these functions.}
\end{seealso}


\chapter{Trigonometry}

In the first half of this chapter the three trigonometric functions (sine, cosine and tangent) will be viewed as functions of angles. The second half of the chapter will approach trigonometry as functions of real numbers.

%---------------------------------------------------
% the unit circle and radians
%---------------------------------------------------
\section{The Unit Circle}
The convention for measuring angles can be seen in the diagram below, beginning from the positive $x$-axis and rotating a point from $(1,0)$ counter-clockwise through $(x,y)$. This traces out an angle, $\theta$. The relationship between the angle and the arc length defines a radian. When the arc length equals the radius, the angle $\theta=1$ radian. The convention is that the positive direction for measuring angles is anticlockwise and the negative direction for measuring angles is clockwise. This setup also defines a right-angled triangle with hypotenuse $r$. As the radius rotates, all the different right-triangles are drawn. See the \href{https://www.desmos.com/calculator/4t6zc5eucd}{link} for the animated version.

\begin{figure}[h]
	\begin{center}
		\includegraphics[width=12cm]{unitCircle}
		\caption{The unit circle has a radius of 1. When the arc length from $(1,0)$ to $(x,y)$ is equal to the radius, the angle $\theta$ is 1 radian. There are $2\pi$ radians in one complete rotation. Click \href{https://www.desmos.com/calculator/4t6zc5eucd}{here} for an animated version in Desmos.}
		\label{fig:unitCircle}
	\end{center}
\end{figure}

\subsection*{Relationship between Degrees and Radians}
The abbreviation for radians is \textit{rad}. One complete revolution is \ang{360} which is equivalent to $2 \pi $ radians. It will help to remember one of these conversion factors:
\begin{tcolorbox}\[1\text{ rev }=2\pi \text{ radians} =  \ang{360} \qquad\text{ or }\qquad\pi \text{ radians} =  \ang{180}\]
\end{tcolorbox}

\example\medskip\\
(a) Convert \ang{36} to radians\hspace{0.7cm}
(b) Convert $\displaystyle\frac{\pi }{3}$ $\mbox{rad}$ to degrees \hspace{0.7cm}
(c) Convert $1$ $\mbox{rad}$ to degrees \medskip\\

\solution Begin with the conversion factor and then modify as necessary.
\begin{tasks}[before-skip = {0ex} , after-skip={-5ex}](3)
\task \begin{align*}\ang{180}  &  =  \pi \text{ rad} \\
\ang{1}  &  =  \frac{\pi }{180}\text{ rad} \\
\ang{1} \times 36 &  =  \frac{\pi }{180} \times 36 =\frac{\pi }{5}\text{ rad}\end{align*}
\task
\begin{align*}\pi  \text{ rad} &  =  \ang{180}  \\
\frac{\pi }{3} \text{ rad} &  = \frac{\ang{180} }{3} =\ang{60} \end{align*}
\task
\begin{align*}\pi  \text{ rad} &  =  \ang{180} \\
1 \text{ rad} &  =  \frac{\ang{180} }{\pi } \\
 &  \approx   \ang{57.3} \end{align*}
\end{tasks}
Note the similarity between the answers to (b) and (c). This is because $\frac{\pi }{3} =1.0472$ so you would expect the values in degrees to be similar. Always include units with your angle whether they be degrees, radians, or even \textit{gradians}. There are 400 gradians in a circle. This unit is not often used, however, your calculator supports all three.\\
\begin{figure}[h]
\begin{center}\includegraphics[width=6cm]{calculator1}\hspace{1cm}\includegraphics[width=6.8cm]{calculator2}\end{center}
\caption{Your calculator can switch between degrees, radians, and gradians. On the right is a `D' indicating degree mode.}
\end{figure}

\subsection*{Arc Length}
Let $\theta $ be the angle subtended at the centre for the ends of an arc of any circle then the fraction of the circumference of the circle is $\frac{\theta }{2 \pi }$ if $\theta $ is measured in radians and $\frac{\theta }{\ang{360}}$ if $\theta $ is measured in degrees. The length of the circumference of any circle whose radius is $r$ is $2 \pi  r$. If $\theta $ is measured in radians
\begin{equation*}\text{length of arc} =\frac{\theta }{2 \pi } \times 2 \pi  r =\theta  r
\end{equation*}
And if $\theta $ is measured in degrees
\begin{equation*}\text{length of arc} =\frac{\theta }{\ang{360}} \times 2 \pi  r
\end{equation*}
\example Find the length of an arc that subtends an angle of \ang{45} at the centre of a circle whose radius is 9 cm.

\solution \begin{tasks}(2)
	\task Method 1:\begin{eqnarray*}\text{Length of arc} &  = & \frac{\theta }{360 \mbox{{\ensuremath{{}^\circ}}}} \times 2 \pi  r \\
	&  = & \frac{45}{360} \times 2 \pi  \times 9 \\
	&  = & 2.25 \pi \text{}\mbox{cm} \\
	&  \approx  & 7.07\text{}\mbox{cm}\end{eqnarray*}
If an exact answer is required you should\\ leave the answer as $2.25 \pi $ $\mbox{cm}$. (Or $\frac{9 \pi }{4}$ $\mbox{cm}\text{.}$)

\task Method 2: Change degrees to radians first
\begin{eqnarray*}180 \mbox{{\ensuremath{{}^\circ}}} &  = & \pi \text{}\mbox{rad} \\
	1 \mbox{{\ensuremath{{}^\circ}}} &  = & \frac{\pi }{180} \\
	45 \mbox{{\ensuremath{{}^\circ}}} &  = & \frac{\pi }{180} \times 45 \\
	&  = & \frac{\pi }{4}\end{eqnarray*}
Now, the length of arc $= r \theta = 9 \times \frac{\pi }{4} =\frac{9 \pi }{4}\text{}\mbox{cm}$.
\end{tasks}
%---------------------------------------------------
% Right-Angled Triangles: trig ratios
%---------------------------------------------------
\section{Right-Angled Triangles}
A right angled triangle is a three-sided figure (`tri-gon') with one of the interior angles being \ang{90}. The unit circle in Figure~\ref{fig:unitCircle} defines all the possible right angled triangles by rotating the point $(x,y)$ about the origin. Measuring the side-length of triangle (`metry') is where the name trigonometry comes from. If the ratio of two of the three side lengths is known then the angle must be fixed. Similarly, if the angle is known, the ratio of the side-lengths is fixed.

%---------------------------------------------------
% Pythagorean theorem
%---------------------------------------------------
\section*{Pythagoras}\label{sec:pythagoras}
Pythagoras is one of the most well known historical figures in mathematics and philosophy, primarily for his eponymous theorem. Given a right-angled triangle, the square of the hypotenuse equals the sum of the squares of the other sides.
\begin{tcolorbox}[colback=white]
	\begin{multicols}{2}
		\begin{center}
			The Pythagorean Theorem:\\
			\vspace{1cm}$a^2+b^2=c^2$
		\end{center}
		\columnbreak
		\begin{center}
			\includegraphics[width=6cm]{trigPythagoras}
		\end{center}
	\end{multicols}
\end{tcolorbox}

This theorem has many proofs, and one graphical one is shown below. The squares are all the same size. Interestingly this proof does not require any math (symbols) at all!

This is a 3-4-5 triangle which is useful in building to determine if something is square: measure sides to 3 and 4 (any units), then check that the hypotenuse is 5.

\begin{figure}
\begin{center}
	\includegraphics[width=7cm]{PythagorasProof}
\hspace{1cm}
	\includegraphics[width=7cm]{rightTriangle}
\end{center}
\end{figure}
\section*{Sine, Cosine, \& Tangent}

The longest side in a right-triangle is always the hypotenuse, with the other two sides being named with respect to the angle of interest, $\theta$. Notice that if $\theta$ moves to the other corner then the sides opposite and adjacent are \textit{reversed}. These names are helpful in defining the trigonometric ratios of sine, cosine and tan.

The phrase \textbf{SOH-CAH-TOA} is useful to remember the ratios.
\begin{tcolorbox}
\begin{equation*}\text{S-ine }  \theta  =\frac{\text{O-pposite}}{\text{H-ypotenuse}}\text{;}\qquad\text{C-osine }  \theta  =\frac{\text{A-djacent}}{\text{H-ypotenuse}}\text{;}\qquad\text{T-angent }  \theta  =\frac{\text{O-pposite}}{\text{A-djacent}}
\end{equation*}\medskip
\hspace{1.7cm}S = O/H\hspace{3.5cm}C = A/H\hspace{3.8cm}T = O/A
\end{tcolorbox}

%----------------------------------------------------
\begin{multicols}{2}
	\example Find the unknown side $x$\\
	\begin{center}
		\includegraphics[width=8cm]{trigSide1}
	\end{center}

	\columnbreak
\solution Write the sine ratio and solve the equation for $x$.\\
\begin{align*}
\sin\theta &=\frac{\text{opposite}}{\text{hypotenuse}}\\
\sin (14) &= \frac{x}{17}\\
x&=17\sin(14)\\
x&\approx4.11\end{align*}
\end{multicols}
\rule{6.8cm}{0.5pt}\\
\begin{multicols}{2}
\example Find the unknown side $x$\\
\begin{center}
	\includegraphics[width=6cm]{trigSide2}
\end{center}
	\columnbreak
\solution Write the tangent ratio and solve the equation for $x$.
\begin{align*}
\tan\theta &=\frac{\text{opposite}}{\text{adjacent}}\\
\tan(48) &= \frac{x}{15}\\
x&=15\tan(48)\\
x&\approx 16.7\end{align*}
\end{multicols}
The calculator gives approximate values of the trigonometric ratios. You must look at your question to check whether angles are in degrees or radians and ensure the calculator is first set in the right mode. Questions where degrees are to be used will give angles marked with a $^\circ$ symbol.

When solving for an angle on your calculator you select the appropriate trigonometry ratio and use the \emph{shift} button with sin, cos or tan to find $\sin ^{ -1}$, $\cos ^{ -1}$ or $\tan ^{ -1}$.
%----------------------------------------------------
\begin{multicols}{2}
	\example Find the unknown angle $\theta$\\
	\begin{center}
		\includegraphics[width=6cm]{trigAngle1}
	\end{center}

	\columnbreak
	\solution Write the sine ratio and solve the equation for $\theta$ by using the inverse-trig function on your calculator: $\sin^{-1}$.\\
	\begin{align*}
	\sin\theta &=\frac{\text{opposite}}{\text{hypotenuse}}\\
	\sin \theta &= \frac{8}{10}\\
	\sin\theta&=0.8\\
	\theta&=\sin^{-1}(0.8)\\
	\theta&=\ang{53.1}
	\end{align*}
\end{multicols}\vspace{-0.5cm}
\rule{6.8cm}{0.5pt}\\
\begin{multicols}{2}
	\example Find the unknown angle $\theta$\\
	\begin{center}
		\includegraphics[width=4cm]{trigAngle2}
	\end{center}
	\columnbreak
	\solution Write the cosine ratio and solve the equation for $\theta$ by using the inverse-trig function on your calculator: $\cos^{-1}$.
	\begin{align*}
	\cos\theta &=\frac{\text{adjacent}}{\text{hypotenuse}}\\
	\cos \theta &= \frac{7}{11}\\
	\theta&=\cos^{-1} \left(\frac{7}{11}\right)\\
	\theta&=\ang{50.5}
	\end{align*}
\end{multicols}\vspace{-0.5cm}
%\rule{6.8cm}{0.5pt}\\
%----------------------------------------------------
\example The height of a steep cliff is to be measured from a point on the opposite side of the river. The following diagram shows the measurements taken. Estimate the height of the cliff.
\begin {multicols}{2}\includegraphics[width=8cm]{L4SZ281H}
%\columnbreak\\
\solution
\begin{align*}\frac{d}{58.2} &  =  \tan  \ang{50.0}  \\
d &  =  58.2 \times \tan  \ang{50.0}  \\
\frac{h}{d} &  =  \tan  \ang{76.3}  \\
h &  =  d \times \tan  \ang{76.3}  \\
 &  =  58.2 \times \tan  \ang{50.0}  \times \tan  \ang{76.3}  \\
 &  \approx   284.5 \mbox{m}\end{align*}
\end{multicols}

%----------------------------------------------------
\example To estimate the height of a mountain above a level plane the angle of elevation of the top of the mountain is measured to be $\ang{30} $. $600 \mbox{m}$ closer to the mountain across the plane it is found that the angle of elevation is $\ang{36} $. Estimate the height of the mountain. \\
\begin{center}\includegraphics[width=11cm]{L4SZ281I}\end{center}
\solution
\begin{equation*}\frac{h}{x} =\tan  \ang{36} \text{and}\frac{h}{x +600} =\tan  \ang{30}
\end{equation*}
We want $h$ so we eliminate $x$ between these two equations
\begin{align*}x &  =  \frac{h}{\tan  \ang{36} }\text{and}x +600 =\frac{h}{\tan  \ang{30} } \\
\frac{h}{\tan  \ang{30} } &  =  \frac{h}{\tan  \ang{36} } +600 \\
\frac{h}{\tan  \ang{30} } -\frac{h}{\tan  \ang{36} } &  =  600 \\
h \left (\frac{1}{\tan  \ang{30} } -\frac{1}{\tan  \ang{36} }\right ) &  =  600 \\
h \genfrac{(}{)}{}{}{\tan  \ang{36}  -\tan  \ang{30} }{\tan  \ang{30}  \tan  \ang{36} } &  =  600 \\
h &  =  600 \times \frac{\tan  \ang{30}  \tan  \ang{36} }{\tan  \ang{36}  -\tan  \ang{30} } \\
 &  \approx   600 \times 2.811603815 \\
 &  \approx   1687 \mbox{ m}\end{align*}

%---------------------------------------------------
% identities
%---------------------------------------------------
\subsection*{Identities}\label{sec:identities}
The unit circle has equation $x^{2} +y^{2} =1$ and we define $x =\cos  \theta$ and $y =\sin  \theta$ so
\begin{equation*}x^{2} +y^{2} =1 \leadsto \left (\cos  \theta\right )^{2} +\left (\sin  \theta\right )^{2} =1
\end{equation*}
This is always written
\begin{tcolorbox}\[\sin ^{2} \theta +\cos ^{2} \theta =1\]
\end{tcolorbox}
This is an \emph{identity} which means it is true for all values of $\theta$. There are many identities in trigonometry, we will only use the Pythagorean identity (above) and one more.
Given $\sin =\frac{\text{opp}}{\text{adj}}$ we can solve for $\text{opp}=(\sin)(\text{hyp})$. Similarly from cosine:  $\text{adj}=(\cos)(\text{hyp})$. Substituting these into the tangent relationship:
\begin{tcolorbox}\[\tan=\frac{\text{opp}}{\text{adj}}=\frac{(\sin)(\cancel{\text{hyp}})}{(\cos)(\cancel{\text{hyp}})}=\frac{\sin}{\cos}\]
\end{tcolorbox}
The sine and cosine law are two more unique relationships that we will cover in section~\ref{sec:applications}.

%---------------------------------------------------
% trig functions
%---------------------------------------------------
\subsection*{All Students Take Calculus}
In the previous section the angles were between $\ang{0} $ and $\ang{90} $. In this section the angles can take any value. Initially we consider angles between $\ang{0} $ and $\ang{360} $ and relate these to the radian measure between $0$ and $2 \pi $. We remind you that angles are measured anticlockwise from the positive $x$-axis.

If the point $P (x ,y)$ is in the first quadrant, $\theta $ is the angle between $OP$ and the positive $x$-axis and we complete the right triangle then we have created the following situation.

\begin {multicols}{2}
\includegraphics[ width=3.774in, height=2.3454in,]{L4SZ281S}

Let the hypotenuse be $r$ then
\begin{equation*}r =\sqrt{x^{2} +y^{2}}
\end{equation*}
Therefore
\begin{equation*}\sin  \theta  =\frac{y}{r}\text{, }\cos  \theta  =\frac{x}{r}\text{and }\tan  \theta  =\frac{y}{x}
\end{equation*}
\end {multicols}
\begin {multicols}{2}
We now let $\theta $ be any angle and define sine, cosine and tangent in the same way. For instance if $P (x ,y)$ is in the second quadrant: $\sin  \theta  =\frac{y}{r}$ because $y$ is positive $\sin  \theta $ will be positive. ($r$ is always positive.)

$\cos  \theta  =\frac{x}{r}$ and since $x$ is negative $\cos  \theta $ will be negative. Tangent: $\tan  \theta  =\frac{y}{x}$ and negative for the same reason.\\
\includegraphics[ width=3.9868in, height=1.8161in,]{L4SZ281T}
\end{multicols}

This pattern can be extended to quadrants 3 and 4. The mnemonic (\textbf{A}ll \textbf{S}tudents \textbf{T}ake \textbf{C}alculus) might help you remember which one is positive although you can always work it out if you need to. This means all are positive in the first quadrant, only sine is positive in the second quadrant, only tangent is positive in the third quadrant and only cosine is positive in the fourth quadrant.

The value of a trigonometric function consists of two parts the numerical part and the sign. you must get both parts correct. In the previous section you related the values of a terminal point to another point in the first quadrant. A point on the unit circle could be in any one of the four quadrants.

\qquad \qquad \qquad \qquad
\begin{tabular}{cccccc}\toprule
	Quadrant  & $x$-coordinate  & $y$-coordinate  & $\cos $  & $\sin $  & $\tan $  \\\midrule
	I  & $ +$  & $ +$  & $ +$  & $ +$  & $ +$  \\\midrule
	II  & $ -$  & $ +$  & $ -$  & $ +$  & $ -$  \\\midrule
	III  & $ -$  & $ -$  & $ -$  & $ -$  & $ +$  \\\midrule
	IV  & $ +$  & $ -$  & $ +$  & $ -$  & $ -$  \\\bottomrule
\end{tabular}

\subsection*{The Area of a Triangle}
The fundamental formula for the area of a triangle is $\text{Area} =\frac{1}{2} \times \text{base} \times \text{height}$ Using the trigonometric functions the height can be replaced and the formula becomes:
\begin{equation*}\text{Area} =\frac{1}{2} \times \text{product of two sides} \times \text{sine of the included angle}
\end{equation*}
The formula is particularly easy to remember in symbolic form. Let the triangle have vertices $A$, $B$ and $C$, so the the sides opposite these angles are $a$, $b$ and $c$ respectively. Then the area can be expressed symbolically as
\begin{equation*}\text{Area} =\frac{1}{2} a b \sin  C =\frac{1}{2} b c \sin  A =\frac{1}{2} a c \sin  B
\end{equation*}

\columnsep =30pt
\begin {multicols}{2}
\includegraphics[width=8cm]{L4SZ281U}\label{fig:triangleArea}

\begin{align*}\sin  A &  = \frac{h}{c}\text{ and }h =c \sin  A \\
\text{Area} &  =  \frac{1}{2} \times \text{base} \times \text{height} \\
 &  =  \frac{1}{2} \times b \times c \sin  A =\frac{1}{2} b c \sin  A
 \end{align*}
\end {multicols}
It depends where you draw $h$ and which angle you choose to use as to which formula you finish up with. The key point to remember is $b$ and $c$ are two sides and $A$ is the angle between them. The triangle above shows $A$ as an acute angle (between $\ang{0}$ and $\ang{90} $). If the angle is obtuse (between $\ang{90} $ and $\ang{180} $) the formula still holds.
\begin{multicols}{2}
\includegraphics[ width=8cm]{L4SZ281V}
The angle is in the second quadrant and $\sin  (180 -\theta ) =\sin  \theta $, so $\sin  (180 -\theta ) =\frac{h}{c}$ can be written as $\sin  \theta  =\frac{h}{c}$ or $h =c \sin  \theta $ or $h =c \sin  A$ where $A$ is obtuse. So the area is $\frac{1}{2} b c \sin  A$
\end{multicols}

\example A triangle has two sides of 5 cm and 8 cm and the angle between them is $\ang{150} $. Find its area.\medskip\\
\solution
\begin{equation*}\text{Area} =\frac{1}{2} \times 5 \times 8 \times \sin  \ang{150}
\end{equation*}

It helps to remember that $\sin  150 =\sin  30 =\frac{1}{2}$
\begin{equation*}\text{Area} =\frac{1}{2} \times 5 \times 8 \times \frac{1}{2} =10 \text{ cm}^{2}
\end{equation*}

%---------------------------------------------------
% TRIG FUNCTIONS & GRAPHS
%---------------------------------------------------
\section{Trig Functions of Real Numbers}
In this next part of the chapter the three main trigonometric functions (sine, cosine and tangent) will be studied. They will be viewed as functions of real numbers rather than angles. The trigonometric functions defined in these two ways are identical and there is a simple rule connecting the domains. Why do we show you the two approaches? Trigonometry will be used to solve a variety of problems and these can be divided into two groups, dynamic problems and static problems. When dynamic problems (such as problems involving motion) are being solved real numbers will be used. When static problems (such as finding distances and angles for triangles) are being solved angles will be used.

%---------------------------------------------------
% trig graphs
%---------------------------------------------------
%\section{Trigonometric Functions}
You should be familiar with the fundamental graphs of $y =\sin  x$ and $y =\cos  x$. These graphs are the basis of this section. \Desmos can easily show you the shape of $y =\sin  x$ and $y =\cos  x$ so if you are asked to draw a rough sketch of these curves you should plot a few key points and draw a smooth curve between them. You will usually be given the required domain however if you are not you would choose to draw these for one complete cycle ($0 -2 \pi $). To sketch $y =\sin  x$ it is enough to select as key points $x =0$, $\frac{\pi }{2}$, $\pi $, $\frac{3 \pi }{2}$, $2 \pi $.

\begin{center}
\begin{tabular}{llllll}\toprule
	$x$  & $0$  & $\dfrac{\pi }{2}$  & $\pi $  & $\dfrac{3 \pi }{2}$  & $2 \pi $ \\\midrule
	$y =\sin  x$  & $0$  & $1$  & $0$  & $ -1$  & $0$  \\\bottomrule
\end{tabular}
\hspace{1cm}
\begin{tabular}[c]{llllll}\toprule
	$x$  & $0$  & $\dfrac{\pi }{2}$  & $\pi $  & $\dfrac{3 \pi }{2}$  & $2 \pi $ \\\midrule
	$y =\cos  x$  & $1$  & $0$  & $ -1$  & $0$  & $1$ \\\bottomrule
\end{tabular}
\end{center}

You will be aware that these curves repeat this pattern every $2 \pi $ where $x$ extends in both the positive and negative directions. $0$ to $2\pi $ represents one complete cycle. Mathematically we say
\begin{align*}\sin  \left (x +2 n \pi \right ) &  = \sin  x\text{\  for any integer }n \\
	\cos  \left (x +2 n \pi \right ) &  = \cos  x\text{\  for any integer }n\end{align*}

Aside: instead of ``for any integer $n$" we can write $ \forall n \in \mathbb{Z}$. A function that displays this characteristic is described as \emph{periodic} and for $y =\sin x$ and $y =\cos x$ the \emph{period} is $2 \pi $.

\subsection*{Transformations}
The following six transformations can be applied to any function including sine, cosine, and tangent.
% can't get bullets to show up here...future work
\begin{tasks}[style=itemize](3)
\task Vertical shift
\task Horizontal shift
\task Vertical stretch
\task Horizontal stretch
\task Reflection in the $x$-axis
\task Reflection in the $y$-axis
\end{tasks}

\example Sketch $y =\sin  \left (x -\frac{\pi }{2}\right ) +3$. This may be considered as a sine curve shifted $\frac{\pi }{2}$ units to the right. Note for periodic functions like a sine wave a horizontal shift is called a \textit{phase shift}. and $3$ units upwards.\\
\solution 1. Beginning with the special points for $\sin x$, a table shows the evolution of the points. \\
\begin{tabular}{llllllc}\cmidrule{1-6}
	$x$  & $0$  & $\frac{\pi }{2}$  & $\pi $  & $\frac{3 \pi }{2}$  & $2 \pi $ & $\leftarrow x$-values, plot these \\
	\cmidrule{1-6}
	$\sin  x$  & $0$  & $1$  & $0$  & $ -1$  & $0$ & \\
	\cmidrule{1-6}
	$\sin  \left (x -\frac{\pi }{2}\right )$  & $ -1$  & $0$  & $1$  & $0$  & $ -1$&  \\
	\cmidrule{1-6}
	$\sin  \left (x -\frac{\pi }{2}\right ) +3\qquad$  & $2$  & $3$  & $4$  & $3$  & $2$&$\leftarrow y$-values, plot these  \\
	\cmidrule{1-6}
\end{tabular}

2. Plot the transformed values with the original special points and connect with a smooth, continuous line. \Desmos confirms our transformation. The dashed plot shows $y=\sin x$ as reference.\\
\begin{center}
\includegraphics[width=8cm]{trigShift1}
\end{center}

\example Sketch $y =\cos  (x -\frac{\pi }{6})$.\medskip\\
\solution You could use a table of values however this is $y =\cos  x$ with a horizontal shift of $\frac{\pi }{6}$ to the right. Sketch the transformation on top of the graph of $y =\cos  x $.
\begin{center}
\includegraphics[width=10cm]{L4SZ270H}
\end{center}

In general $y =a \sin  x$ represents a vertical stretch of $y =\sin  x$ by $a$. If $a$ is negative the transformation can either be described as a negative stretch or (preferably) as a \emph{stretch}
of $\left \vert a\right \vert $ followed by a \emph{reflection} in the $x$-axis. Recall the reflection of $y =f \left (x\right )$ in the $x$-axis is $y = -f \left (x\right )$. The number $\left \vert a\right \vert $ is called the \emph{amplitude} for both $y =\sin  x$ and $y =\cos  x$ shown below.

If $0 <a <1$ the fractional stretch causes the curve to shrink vertically. For instance the curve
$y =\sin  x$ has a maximum value of $1$ and a minimum value of $ -1$. the curve $y =\frac{1}{2} \sin  x$ has a maximum value of $\frac{1}{2}$ and a minimum value of $ -\frac{1}{2}$.

\begin{tasks}(2)
\task\example Sketch $y =\cos  ( -t)$ \medskip\\
Notice this reflection is on top of the original. Cosine is symmetric about the $y$-axis.\\
\includegraphics[width=7.5 cm]{trigTrans1}\\

\task\example Sketch $y =\sin(x+\frac{\pi}{4})$\medskip\\
The angle is shifted to the left by 45 degrees ($\frac{\pi}{4}$ radians).\\
\includegraphics[width=7.5 cm]{trigTrans2}

\task\example Sketch $y =\cos(2x)$ \medskip\\
Here there are 2 complete cycles between 0 and $2\pi$.\\
\includegraphics[width=7.5 cm]{trigTrans3}

\task\example Sketch $y =2\sin(0.5x)$ \medskip\\
Two transformations applied together. The 2 refers to the amplitude.\\
\includegraphics[width=7.5 cm]{trigTrans4}
\end{tasks}

\subsection*{Tangent}
 There are other trig functions that we will not be covering here, for example the reciprocal functions are cosecant$=\frac{1}{\sin\theta}$, secant$=\frac{1}{\cos\theta}$, and the inverse tangent function, cotangent$=\frac{1}{\tan\theta}$. By focussing on sine, cosine and tangent the majority of problems we encounter can be solved. Tangent is the odd function out of the trio of common ones.

Previously we learnt that the period for the sine and cosine functions was $2 \pi $. Tangent is also a periodic function and it has a period of $\pi $ (not $2 \pi $). This means that it goes through one complete cycle every $\pi$. Recall $\tan  x =\frac{\sin  x}{\cos  x}$. To analyse the behaviour of the tangent function it helps if you know what you are looking for. Some key values of tangent will show the pattern.
\begin{center}
\begin{tabular}{lrrrl}\toprule
	$x$  & $\qquad\sin  x$  & $\qquad\cos  x$  &\qquad\quad& $\tan  x$  \\
	\midrule
	$ -\frac{\pi }{2}$  & $ -1$  & $0$  && $\frac{ -1}{0} = -\infty $  \\
	\midrule
	$ -\frac{\pi }{4}$  & $ -\frac{\sqrt{2}}{2}$  & $\frac{\sqrt{2}}{2}$  && $ -\frac{\sqrt{2}}{2} \div \frac{\sqrt{2}}{2} = -1$  \\
	\midrule
	$0$  & $0$  & $1$  && $\frac{0}{1} =0$  \\
	\midrule
	$\frac{\pi }{4}$  & $\frac{\sqrt{2}}{2}$  & $\frac{\sqrt{2}}{2}$  && $\frac{\sqrt{2}}{2} \div \frac{\sqrt{2}}{2} =1$  \\
	\midrule
	$\frac{\pi }{2}$  & $1$  & $0$  && $\frac{1}{0} =\infty $  \\
	\bottomrule
\end{tabular}
\end{center}
As $x$ takes values from $ -\frac{\pi }{2}$ to $\frac{\pi }{2}$, $\tan(x)$ takes values from $ -\infty $ to $\infty $. This pattern is repeated every $\pi $.
A \desmos graph can show this relationship. The dashed lines are the asymptotes.

\begin{figure}\begin{center}
\includegraphics[width=12cm]{trigTan}
\caption{The graph of $y=\tan x$. The asymptotes are shown with dashed lines, repeating every $\pi$ radians.}
\end{center}\end{figure}

The graph can be seen to have point symmetry. If you rotate the tangent curve through a half turn using the origin as axis the curve will lie on top of itself. This is a pictorial representation of an odd function.

Note: Do not confuse $y =\tan  x$ with $y =x^{3}$. While they may appear to be similar in shape the only similarities are that they both pass through the origin and continue towards $\infty $ in the first quadrant and $ -\infty $ in the third quadrant.

%---------------------------------------------------
% sine rule and cosing rule
%---------------------------------------------------
\section{Applications}\label{sec:applications}
\subsection*{The Sine Rule}
The Sine Rule is a relationship that allows you to find the sides and angles in triangles \textit{without} a right angle. In the next section we use the Cosine Rule to find sides and angles in triangles also, so as you study these two sections you need to learn which problems require the Sine Rule and which require the Cosine Rule. Previously, we met the formula for the area of a triangle given two sides and the included angle $\left ( \text{area}=\frac{1}{2} a b \sin  C\right )$. The Sine Rule and Cosine Rule also require specific combinations of sides and angles. Using standard side and angle labelling for a triangle $\rightarrow$ $\Delta A B C$ and let the sides be lowercase $a$, $b$ and $c$ where $a$ is opposite $\angle A$ etc.
\begin{center}
\includegraphics[width=7cm]{SineRuleTriangle1}
\end{center}

\begin{tcolorbox}
	The Sine Rule states that in any triangle
	\begin{equation*}\frac{\sin  A}{a} =\frac{\sin  B}{b} =\frac{\sin  C}{c}\qquad%
	\text{ or, inversely as: }%
	\qquad\frac{a}{\sin  A} =\frac{b}{\sin  B} =\frac{c}{\sin  C}
	\end{equation*}\end{tcolorbox}
Textbooks may use the term ``The Law of Sines'' meaning the same relationship.

\subsection*{Proof of the Sine Rule}
The Sine Rule is easy to prove beginning with the formula for the area of a triangle, and using the diagram above as reference with the base as $c$. (Refer to the diagram on page~\pageref{fig:triangleArea}.)
\begin{equation*}\text{Area} =\frac{1}{2}(\text{base})(\text{height})=\frac{1}{2} (c)(b\sin A)=\dots=\frac{1}{2}ab\sin C=\frac{1}{2}ac\sin B
\end{equation*}
Focussing only on the three terms on the right, multiply by 2:
\begin{equation*}b c \sin  A=ab\sin C=ac\sin B
\end{equation*}

And dividing by $a b c$
\begin{equation*}\frac{\sin A}{a}=\frac{\sin B}{b}=\frac{\sin C}{c}
\end{equation*}
\clearpage
%\rule{\textwidth}{0.5pt}\\
\begin{multicols}{2}
\example Find side lengths $a$ and $c$ in the following triangle.\\
\includegraphics[width=8cm]{SineRuleTriangle2}\\
$\phantom{1}$\\
$\phantom{1}$\\
\solution \medskip\\(1) To find $c$
\begin{align*}\frac{c}{\sin  C} &  = \frac{b}{\sin  B} \\
\frac{c}{\sin  43 } &  = \frac{5}{\sin  35 } \\
c &  = \frac{5 \sin  43 }{\sin  35 } \\
&  \approx   5.95 \mbox{cm}\text{(2 dp)}\end{align*} \\
%\clearpage
(2) To find $a$ (i) $A =\ang{180}  -(\ang{35}  +\ang{43} ) =\ang{180}  -\ang{78}  =\ang{102}$ (ii) It is usually wise to go back to the original data (i.e. use $b$ and $B$ rather than $c$ and $C$).
\begin{align*}\frac{a}{\sin  A} &  = \frac{b}{\sin  B} \\
\frac{a}{\sin  102 } &  = \frac{5}{\sin  35 } \\
a &  = \frac{5 \sin  102 }{\sin  35 } \\
&  \approx   8.53 \mbox{cm}\text{(2 dp)}\end{align*}
\end{multicols}

These two calculations illustrate the first two cases in which the Sine Rule is used. You will notice that the triangle has been completely solved in the course of this example. We started with one side and two angles and we found the other two sides and the other angle. \\
\rule{6.8cm}{0.5pt}\\
\example Given $a =\ang{30} $, $a =8$ and $b =7$ solve the triangle (i.e. find $B$, $C$ and $c$). \\
\begin{center}
	\includegraphics[width=11cm]{SineRuleTriangle3}\\
\end{center}
\solution
\begin{tasks}(3)
\task Find $B$
\begin{align*}\frac{\sin  B}{b} &  = \frac{\sin  A}{a} \\
\sin  B &  = \frac{b \sin  A}{a} \\
&  = \frac{7 \sin  30 }{8} =0.4375 \\
B &  = \sin ^{ -1} 0.4375 \approx \ang{25.94} \end{align*}

\task Find $C$
\begin{align*}C &  = \ang{180}  -(\ang{30}  +\ang{25.94} ) \\
&  = \ang{124.06} \end{align*}

\task Find $c$
\begin{align*}\frac{c}{\sin  C} &  = \frac{a}{\sin  A} \\
c &  = \frac{a \sin  C}{\sin a} \\
&  = \frac{8 \sin  124.06 }{\sin  30 } \\
&  \approx   13.3\end{align*}
\end{tasks}
%--------ambiguous case example---------------------------
The third case is not as straight forward and referred to as the ambiguous case. Given two sides and an angle there could be no triangle formed, one triangle formed or two triangles formed depending on the length of the side opposite the given angle.

\example {The Ambiguous Case:} Given $A =\ang{30} $, $a =6$ and $b =7$ solve the triangle. \\

\solution In this case $b \sin  A =3.5$ and $b =7$ so as $a$ lies between $3.5$ and $7$. This is the ambiguous case, therefore there are two solutions as the side lengths and angle can produce two different triangles.
%\columnsep =30pt
\begin {multicols}{2}
\includegraphics[width=8cm]{SineRuleAmb1}\columnbreak
\includegraphics[width=8cm]{SineRuleAmb2}
\end {multicols}
\textbf{First solution} (Proceed as before)
\begin{tasks}(3)
	\task Find $B$
\begin{align*}\frac{\sin  B}{b} &  = \frac{\sin  A}{a} \\
\sin  B &  = \frac{b \sin  A}{a} \\
&  = \frac{7 \sin  30 }{6} =0.58 \dot{3} \\
B &  = \sin ^{ -1} 0.58 \dot{3} \approx \ang{35.7} \end{align*}

\task Find $C$\\
First recognize this angle is obtuse $>\ang{90}$
\begin{align*}C &  = \ang{180}  -(\ang{30}  +\ang{35.69} ) \\
&  = \ang{114.31} \end{align*}

\task Find $c$
\begin{align*}\frac{c}{\sin  C} &  = \frac{a}{\sin  A} \\
c &  = \frac{a \sin  C}{\sin  A} \\
&  = \frac{6 \sin  114.31 }{\sin  30 } \\
&  \approx   10.9\end{align*}
\end{tasks}
\textbf{Second solution}
\begin{tasks}(2)
	\task Find the second value of $B$ \\
$B =\ang{180}  -\ang{35.69}  =\ang{144.31} $
\task Find $C$\\
$C =\ang{180} -(\ang{30}  +\ang{144.31} ) =\ang{180}  -\ang{174.31}  =\ang{5.69} $
\task*[](Or if you remember the rule that the exterior angle of a triangle is the sum of the two interior opposite angles $C +\ang{30}  =\ang{35.69} $ so $C =\ang{35.69}  -\ang{30}  =\ang{5.69} $)
\task[(3)] Find $c$
\begin{align*}\frac{c}{\sin  C} &  = \frac{a}{\sin  A} \\
c &  = \frac{a \sin  C}{\sin  A} \\
&  = \frac{6 \sin  5.69 }{\sin  30 } \\
&  \approx   1.2\end{align*}
\task[]Both answers here are correct mathematically, however, depending on the situation one answer refers to a specific triangle which should be checked with a diagram.
\end{tasks}

\subsection*{The Cosine Rule}
The Cosine Rule, sometimes called `The Law of Cosines', is useful for a non-right triangle when we know all three side lengths without an angle, or two sides and the angle between them.

\begin{tcolorbox}
	For any triangle side sides $a$, $b$, and $c$ and angle $A$ opposite side $a$:\\
	\begin{center}
		$a^{2} =b^{2} +c^{2} -2 b c \cos  A$
	\end{center}
\end{tcolorbox}
Note the similarity to the Pythagorean theorem (see page~\pageref{sec:pythagoras}).

\subsection*{Proof of The Cosine Rule}
\textbf{To prove:} For any triangle $ \Delta A B C\text{,}$ $a^{2} =b^{2} +c^{2} -2 b c \cos  A$ \\
\columnsep =30pt
\begin {multicols}{2}
\setlength\fboxrule{0in}\setlength\fboxsep{0.2in}\fcolorbox[HTML]{000000}{FFFFFF}{\includegraphics[ width=3.1021in, height=2.3549in,]{L4SZ282A}}
By Pythagoras' Theorem
\begin{align*}B C^{2} &  = C D^{2} +D B^{2} \\
a^{2} &  = \left (c -b \cos  A\right )^{2} +\left (b \sin  A\right )^{2} \\
&  = c^{2} -2 b c \cos  A +b^{2} \cos ^{2} A +b^{2} \sin ^{2} A \\
&  = c^{2} -2 b c \cos  A +b^{2} \left (\cos ^{2} A +\sin ^{2} A\right ) \\
&  = c^{2} -2 b c \cos  A +b^{2}\text{as}\cos ^{2} A +\sin ^{2} A =1\text{\ }\end{align*}
\end {multicols}
This is usually written
\begin{equation*}a^{2} =b^{2} +c^{2} -2 b c \cos  A
\end{equation*}

The diagram has been drawn to simplify the way the proof unfolds. You will see that by placing the vertex $A$ at the origin the side $a$ is found in terms of $b$, $c$, and $A$. The proof would have been the same had $A$ and $B$ been as shown and $C$ placed in the second quadrant. (Thus producing a triangle with an obtuse angle at $A$.) This rule is symmetrical. You need to be given two sides and the included angle ($b$, $c$ and $A$) and the formula allows you to calculate $a$. Most textbooks will therefore show you three equivalent formulae:
\begin{align*}a^{2} &  = b^{2} +c^{2} -2 b c \cos  A \\
b^{2} &  = c^{2} +a^{2} -2 c a \cos  B \\
c^{2} &  = a^{2} +b^{2} -2 a b \cos  C\end{align*}
\rule{6.8cm}{0.5pt}\\
\example Given $a =5$, $b =6$ and $C =\ang{130} $, find $c$.\medskip\\
\solution
\begin{align*}c^{2} &  = a^{2} +b^{2} -2 a b \cos  C \\
&  = 5^{2} +6^{2} -2 \times 5 \times 6 \times \cos  130  \\
c^2&  \approx   99.567 \\
c &  \approx   \sqrt{99.567}  \approx   9.978\end{align*}

\example Find the angle given three side lengths; given $a =5$, $b =6$ and $c =9$, find $A$. \medskip\\
\solution Because $a$ is the smallest side $A$ will most certainly be an acute angle.
\begin{align*}\cos  A &  = \frac{b^{2} +c^{2} -a^{2}}{2 b c} \\
&  = \frac{6^{2} +9^{2} -5^{2}}{2 \times 6 \times 9} \\
&  \approx 0.852 \\
A &  \approx \cos ^{ -1} \left (0.852\right ) \\
&  \approx \ang{31.6}
\end{align*}

%---------------------------------------------------
% Chapter Exercises in a separate file
%---------------------------------------------------
\section{Chapter Exercises}
\subimport{}{2TrigExercises}