\section{Maxwell's Equations}
Just a little identity on Laplacian of vector fields
\begin{proposition}
    We have
    $$\nabla^2\underline{F}=\nabla(\nabla\cdot\underline{F})-\nabla\times(\nabla\times\underline{F})$$
    where $(\nabla^2\underline{F})_i=\nabla^2\underline{F_i}$
\end{proposition}
\subsection{Introduction to Electromagnetism}
We have the electric field $\underline{E}=\underline{E}(\underline{x},t)$, the magnetic field $\underline{B}=\underline{B}(\underline{x},t)$, charge density $\rho=\rho(\underline{x},t)$ and current density $\underline{J}=\underline{J}(\underline{x},t)$.
The Maxwell's Equations state that
$$\begin{cases}
    \nabla\cdot\underline{E}=\epsilon_0^{-1}\rho\\
    \nabla\cdot\underline{B}=0\\
    \nabla\times\underline{E}+\partial\underline{B}/\partial t=0\\
    \nabla\times\underline{B}-\mu_0\epsilon_0\partial\underline{E}/\partial t=\mu_0\underline{J}
\end{cases}$$
where $\epsilon_0$ is the permitivity and $\mu_0$ is the permeability of free space with $\mu_0\epsilon_0=c^{-2}$ where $c$ is the speed of light.
Take the divergence of the fourth equation gives
$$0=\mu_0\epsilon_0\frac{\partial}{\partial t}(\nabla\cdot\underline{E})+\mu_0\nabla\cdot\underline{J}\implies 0=\frac{\partial\rho}{\partial t}+\nabla\cdot\underline{J}$$
\subsection{Integral Forms of Maxwell's Equations}
If we integrate then first equation and use divergence theorem to integrate the electric field over the flux of the boundary of a volume, we get
$$\int_{\partial V}\underline{E}\cdot\mathrm d\underline{S}=\int_V\nabla\cdot\underline{E}\,\mathrm dV=\frac{1}{\epsilon_0}\int_V\rho\,\mathrm dV=\frac{Q}{\epsilon_0}$$
where $Q$ is the total charge of the volume $V$.
Do exactly the same thing with th second equation then gives
$$\int_{\partial V}\underline{B}\cdot\mathrm d\underline{S}=\int_V\nabla\cdot\underline{B}\,\mathrm dV=0$$
which is saying that there is no magnetic monopoles since we cannot have a singular pole that emit magnetic field out of a volume.
In fact, if somewhere there exists a magnetic monopole, then charges are necessarily quantized.\\
As for the third equation, we have
$$\oint_{\partial S}\underline{E}\cdot\mathrm d\underline{x}=\int_S\nabla\times\underline{E}\cdot\mathrm d\underline{S}=-\frac{\mathrm d}{\mathrm dt}\int_S\underline{B}\,\mathrm d\underline{S}$$
So change in magnetic flux induces electric field.
Similarly, in the fourth equation,
$$\oint_{\partial S}\underline{B}\cdot\mathrm d\underline{x}=\int_S\nabla\times\underline{B}\cdot\mathrm d\underline{S}=\mu_0\epsilon_0\frac{\mathrm d}{\mathrm dt}\int_S\underline{E}\cdot\mathrm d\underline{S}+\mu_0\int_S\underline{J}\cdot\mathrm d\underline{S}$$
\subsection{Electromagnetic Waves}
In a free space, where $\rho=\underline{J}=\underline{0}$, then
\begin{align*}
    \nabla^2\underline{E}&=\nabla(\nabla\cdot\underline{E})-\nabla\times(\nabla\times\underline{E})\\
    &=0-\nabla\times\left(-\frac{\partial\underline{B}}{\partial t}\right)\\
    &=\frac{\partial}{\partial t}(\nabla\times\underline{B})\\
    &=\mu_0\epsilon_0\frac{\partial^2\underline{E}}{\partial t^2}\\
    \implies 0&=\nabla^2\underline{E}-\frac{1}{c^2}\frac{\partial^2\underline{E}}{\partial t^2}
\end{align*}
which is the wave equation of a wave with speed $c$.
If we do the same thing to $\underline{B}$, we obtain
\begin{align*}
    \nabla^2\underline{B}&=\nabla(\nabla\cdot\underline{B})-\nabla\times(\nabla\times\underline{B})\\
    &=0-\nabla\times\left(\frac{1}{c^2}\frac{\partial\underline{E}}{\partial t}\right)\\
    &=-\frac{1}{c^2}\frac{\partial}{\partial t}(\nabla\times\underline{E})\\
    &=\frac{1}{c^2}\frac{\partial^2\underline{B}}{\partial t^2}\\
    \implies 0&=\nabla^2\underline{B}-\frac{1}{c^2}\frac{\partial^2\underline{B}}{\partial t^2}
\end{align*}
\subsection{Electrostatics and Magnitostatics}
Assume that everything is time-independent, then $t$-derivatives are all $0$, which produces
$$\begin{cases}
    \nabla\cdot\underline{E}=\epsilon_0^{-1}\rho\\
    \nabla\cdot\underline{B}=0\\
    \nabla\times\underline{E}=0\\
    \nabla\times\underline{B}=\mu_0\underline{J}
\end{cases}$$
So if we work in $\mathbb R^3$ which is $2$-connected, we can write $\underline{E}=-\nabla\phi$ and $\underline{B}=\nabla\times\underline{A}$ for some $\phi,\underline{A}$.
$\phi$ is called the electric potential and $\underline{A}$ the magnetic potential.
So Maxwell's equations reduce to
$$\begin{cases}
    -\nabla^2\phi=\rho/\epsilon_0\\
    \nabla\times(\nabla\times\underline{A})=\mu_0\underline{J}
\end{cases}$$
The first one is called the Poisson's Equation.
\subsection{Gauge Invariance}
In a $2$-connected domain, we can always write $\underline{B}=\nabla\times\underline{A}$.
But note that the equation still holds by adding the gradient of some scalar function to $\underline{A}$, so $\underline{B}$ is invariant under $\underline{A}\mapsto\underline{A}+\nabla\chi$ for $\chi=\chi(\underline{x},t)$.
If we put the vector potential into the third equation,
$$\nabla\times\left( \underline{E}+\frac{\partial\underline{B}}{\partial t} \right)=0$$
So we can write $\underline{E}=-\nabla\phi-\partial\underline{B}/\partial t$, so we have
$$-\nabla^2\phi-\frac{\partial}{\partial t}(\nabla\cdot\underline{A})=\frac{\rho}{\epsilon_0}$$
And
$$\nabla\times(\nabla\times A)+\frac{1}{c^2}\nabla\left( \frac{\partial\phi}{\partial t} \right)+\frac{1}{c^2}\frac{\partial^2\underline{A}}{\partial t^2}=\mu_0\underline{J}$$
But by a known identity on curl of curl,
$$-\nabla^2\underline{A}+\frac{1}{c^2}\frac{\partial^2\underline{A}}{\partial t^2}+\nabla\left( \frac{1}{c^2}\frac{\partial\phi}{\partial t}+\nabla\cdot\underline{A} \right)=\mu_0\underline{J}$$
We now choose the scalar field $\chi$ such that
$$\frac{1}{c^2}\frac{\partial\phi}{\partial t}+\nabla\cdot\underline{A}=0$$
under $\underline{A}\mapsto\underline{A}+\nabla\chi$.
So we can get an equation similar to a wave equation
$$-\nabla^2\underline{A}+\frac{1}{c^2}\frac{\partial^2\underline{A}}{\partial t^2}=\mu_0\underline{J}$$
and
$$-\nabla^2\phi+\frac{1}{c^2}\frac{\partial^2\phi}{\partial t^2}=\frac{\rho}{\epsilon_0}$$
This trick is called the Lorenz gauge.


% AReviewofTopology.tex
% Fund Science! & Help Ernest finish his Physics Research! : quantum super-A-polynomials - a thesis by Ernest Yeung
%
% http://igg.me/at/ernestyalumni2014
%
% Facebook     : ernestyalumni
% github       : ernestyalumni
% gmail        : ernestyalumni
% google       : ernestyalumni
% linkedin     : ernestyalumni
% tumblr       : ernestyalumni
% twitter      : ernestyalumni
% youtube      : ernestyalumni
% indiegogo    : ernestyalumni
%
% Ernest Yeung was supported by Mr. and Mrs. C.W. Yeung, Prof. Robert A. Rosenstone, Michael Drown, Arvid Kingl, Mr. and Mrs. Valerie Cheng, and the Foundation for Polish Sciences, Warsaw University.

\section*{Review of Topology}

\subsection*{Topological Spaces}

Let $X$ set \\
topology on $X = $ collection $\tau = \lbrace U | U \subseteq X \rbrace$, $U$ called open subsets s.t.
\begin{enumerate}
\item[(i)] $X, \emptyset$ open
\item[(ii)] $\bigcup_{\alpha} U_{\alpha}$ is open, $\forall \, U_{\alpha}$ open subset
\item[(iii)] $\bigcap_{i=1}^n U_i$ is open
\end{enumerate}

$(X,\tau)$ topological space

\textbf{Example A.5. (Metric Spaces)}
metric space $=$ set $M$ with metric $d: M \times M \to \mathbb{R}$ s.t. $\forall \, x, y, z \in M$
\begin{enumerate}
\item[(i)] \textsc{Positivity} $d(x,y) \geq 0$, $d(x,y) =0$ iff $x=y$
\item[(ii)] \textsc{Symmetry} $d(x,y) =d(y,x)$
\item[(iii)] \textsc{Triangle inequality} $d(x,z) \leq d(x,y) + d(y,z)$
\end{enumerate}

if $M$ metric space, $x\in M$, $r>0$ \\
\phantom{\quad } \textbf{open ball of radius $r$ around $x$ }
\[
B_r(x) = \{ y \in M | d(x,y) < r \}
\]
\phantom{\quad \quad } \textbf{closed ball of radius $r$ }
\[
\underline{B}_r(x) = \{ y \in M | d(x,y) \leq r \}
\]
\textbf{metric topology on $M$ } defined by declaring $S \subseteq M$ open iff $\forall \, x \in S$, $\exists \, r >0$ s.t. $B_r(x) \subseteq S$


\documentclass[12pt]{cdblatex}
\usepackage{examples}
\usepackage{fancyhdr}
\usepackage{footer}

\begin{document}

% ============================================================================================
\bgroup
\CdbSetup{action=hide}
\begin{cadabra}
   import cdblib
   checkpoint_file = 'tests/semantic/output/example-10.json'
   cdblib.create (checkpoint_file)
   checkpoint = []
\end{cadabra}
\egroup

\clearpage

% ============================================================================================
\section*{Example 10 The determinant of the metric}

Our game here is to compute (the leading terms) in $\det g$ of the metric in RNC form
\begin{dgroup*}
   \Dmath*{g_{a b}(x) = \cdb{ex-10.gab.000}+\cdots}
\end{dgroup*}
For the sake of simplicity let's assume that we are working in 3-dimensions. The following
analysis is easily generalsied to other dimensions (and the final answers for $\det g$ and
friends are unchanged).

Define $\eps^{abc}_{ijk}$ by
\begin{align}
   \eps^{abc}_{ijk} =
        \delta^a_i \delta^b_j \delta^c_k - \delta^b_i \delta^a_j \delta^c_k
      + \delta^c_i \delta^a_j \delta^b_k - \delta^c_i \delta^b_j \delta^a_k
      + \delta^b_i \delta^c_j \delta^a_k - \delta^a_i \delta^c_j \delta^b_k
\end{align}
It is easy to see that $\eps^{abc}_{ijk}$ is anti-symmetric in both its upper and lower
indices. A trivial computation shows that for any $3{}\times{}3$ square matrix $M_{ab}$,
\begin{align}
   \eps^{abc}_{123} M_{1a} M_{2b} M_{3c}
   = \left(
          \delta^a_1 \delta^b_2 \delta^c_3 - \delta^b_1 \delta^a_2 \delta^c_3
        + \delta^c_1 \delta^a_2 \delta^b_3 - \delta^c_1 \delta^b_2 \delta^a_3
        + \delta^b_1 \delta^c_2 \delta^a_3 - \delta^a_1 \delta^c_2 \delta^b_3
     \right)M_{1a} M_{2b} M_{3c}
   = \det M
\end{align}
This can be easily generalised to
\begin{align}
   \eps^{abc}_{ijk} M_{pa} M_{qb} M_{rc}
   =
   \begin{cases}
      \pm \det M &\text{when $(ijk)$ and $(pqr)$ are permutations of $(123)$}\\
      0 & \text{otherwise}
   \end{cases}
\end{align}
The $\pm$ sign in the above depends on the particular permutations of $(ijk)$ and $(pqr)$. If
both permutations are even or both odd then the sign is $+1$ otherwise the sign is $-1$.
The same arguments can also be applied to a matrix inverse $N^{-1}$ leading to
\begin{align}
   \eps^{ijk}_{uvw} N^{pu} N^{qv} M^{rw}
   =
   \begin{cases}
      \pm \det {N^{-1}} &\text{when $(ijk)$ and $(pqr)$ are permutations of $(123)$}\\
      0 & \text{otherwise}
   \end{cases}
\end{align}
Note that the $\pm$ in this case will match exactly that for the case of $\det M$. Thus,
multiplying both expressions and summing over all choices for $(ijk)$ and $(pqr)$ leads
to
\begin{align}
   \sum_{\substack{(ijk)\\(pqr)}}\left(\det N^{-1}\right) \det M
   = \eps^{ijk}_{uvw} N^{pu} N^{qv} M^{rw} \eps^{abc}_{ijk} M_{pa} M_{qb} M_{rc}
\end{align}
where the sum on the left hand side includes just those $(ijk)$ and $(prq)$ that are
permutations of $(123)$. There are $3!$ choices for $(ijk)$ and $3!$ choices for
$(pqr)$ and thus the left hand side is easily reduced to $(3!)^2 \det M/\det N$ where
$\det N = 1/\det (N^{-1})$. For the right hand side notice that
\begin{align}
   \eps^{ijk}_{uvw} \eps^{abc}_{ijk} = 3! \eps^{abc}_{uvw}
\end{align}
which leads to
\begin{align}
   \det M = \frac{1}{3!} \det N \eps^{abc}_{uvw} M_{pa} M_{qb} M_{rc} N^{pu} N^{qv} N^{rw}
\end{align}

For our RNC metric we will set $N^{ab} = g^{ab}$ and $M_{ij} = g_{ij}(x)$. Since $g^{ab}$ is
of the form ${\rm diag}(-1,1,1,1)$ we have $\det g = -1$ and thus
\begin{align}
   \det g(x) = - \frac{1}{3!} \eps^{abc}_{ijk}\, g_{pa}(x)\, g_{qb}(x)\, g_{rc}(x)\, g^{ip} g^{jq} g^{kr}
\end{align}

The $\eps^{abc}_{ijk}$ can be constructed in Cadabra by applying the \verb|asym| algorithm
to the upper indices of $\delta^a_i \delta^b_j \delta^c_k$. Note that \verb|asym| will
include the $1/3!$ coeffcient as part of its output.

The following code computes $-\det g$ rather than $\det g$.

Note that Calzetta etal. use an opposite sign for $R_{abcd}$ so when comparing the following
results against Calzetta do take note of this flipped sign in $R_{abcd}$.

\clearpage

% ============================================================================================
\section*{The determinant of the metric}

\begin{cadabra}
   {a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u#}::Indices(position=independent).

   {a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u#}::Integer(1..3).

   \nabla{#}::Derivative.

   d{#}::KroneckerDelta.

   g^{a b}::Symmetric.
   g_{a b}::Symmetric.

   R_{a b c d}::RiemannTensor.

   x^{a}::Weight(label=num,value=1).

   def truncate (obj,n):

       ans = Ex("0")  # create a Cadabra object with value zero

       for i in range (0,n+1):
          foo := @(obj).
          bah  = Ex("num = " + str(i))
          distribute  (foo)
          keep_weight (foo, bah)
          ans = ans + foo

       return ans

   gab := g_{a b}
          - (1/3)   x^{c} x^{d} R_{a c b d}
          - (1/6)   x^{c} x^{d} x^{e} \nabla_{c}{R_{a d b e}}
          + (1/180) x^{c} x^{d} x^{e} x^{f} ( 8 g^{g h} R_{a c d g} R_{b e f h}
                                             -9 \nabla_{c d}{R_{a e b f}} ).          # cdb (ex-10.gab.000,gab)

   iab := g^{a b}
          + (1/3)  x^{c} x^{d} g^{a e} g^{b f} R_{c e d f}
          + (1/6)  x^{c} x^{d} x^{e} g^{a f} g^{b g} \nabla_{c}{R_{d f e g}}
          + (1/60) x^{c} x^{d} x^{e} x^{f} g^{a g} g^{b h}
                                 ( 4 g^{i j} R_{c g d i} R_{e h f j}
                                  +3 \nabla_{c d}{R_{e g f h}} ).                     # cdb(ex-10.iab.000,iab)

   distribute (gab)
   distribute (iab)

   gxab := gx_{a b} -> @(gab).

   eps := d^{a}_{i} d^{b}_{j} d^{c}_{k}.   # cdb (ex-10.eps.001,eps) # includes a factor of 1/3!
   asym (eps,$^{a},^{b},^{c}$)             # cdb (ex-10.eps.002,eps)

   # compute negative detg rather than det g, note 1/3! included in eps
   Ndetg := @(eps) gx_{p a} gx_{q b} gx_{r c} g^{i p} g^{j q} g^{k r}.   # cdb (ex-10.Ndetg.001,Ndetg)

   substitute       (Ndetg,gxab)                                         # cdb (ex-10.Ndetg.002,Ndetg)
   distribute       (Ndetg)                                              # cdb (ex-10.Ndetg.003,Ndetg)
   Ndetg = truncate (Ndetg,4)                                            # cdb (ex-10.Ndetg.004,Ndetg)
   substitute       (Ndetg,$g^{a b} g_{b c} -> d^{a}_{c}$,repeat=True)   # cdb (ex-10.Ndetg.005,Ndetg)
   eliminate_kronecker (Ndetg)                                           # cdb (ex-10.Ndetg.006,Ndetg)
   sort_product     (Ndetg)                                              # cdb (ex-10.Ndetg.007,Ndetg)
   rename_dummies   (Ndetg)                                              # cdb (ex-10.Ndetg.008,Ndetg)
   canonicalise     (Ndetg)                                              # cdb (ex-10.Ndetg.009,Ndetg)

   # introduce the Ricci tensor

   substitute (Ndetg,$R_{a b c d} g^{a c}               -> R_{b d}$,repeat=True)               # cdb (ex-10.Ndetg.101,Ndetg)
   substitute (Ndetg,$\nabla_{a}{R_{b c d e}} g^{b d}   -> \nabla_{a}{R_{c e}}$,repeat=True)   # cdb (ex-10.Ndetg.102,Ndetg)
   substitute (Ndetg,$\nabla_{a b}{R_{c d e f}} g^{c e} -> \nabla_{a b}{R_{d f}}$,repeat=True) # cdb (ex-10.Ndetg.103,Ndetg)

   # the following was based on sqrt-Ndetg.tex

   sqrtNdetg := 1/2 + (1/2) @(Ndetg)
               - (1/8) (1/9) R_{a b} R_{c d} x^{a} x^{b} x^{c} x^{d}
               - (1/4) (1/18) R_{a b} \nabla_{c}{R_{d e}} x^{a} x^{b} x^{c} x^{d} x^{e}.
                                                                         # cdb (ex-10.sqrtNdetg.001,sqrtNdetg)

   sort_product   (sqrtNdetg)                                            # cdb (ex-10.sqrtNdetg.002,sqrtNdetg)
   rename_dummies (sqrtNdetg)                                            # cdb (ex-10.sqrtNdetg.003,sqrtNdetg)
   canonicalise   (sqrtNdetg)                                            # cdb (ex-10.sqrtNdetg.004,sqrtNdetg)

   logNdetg := -1 + @(Ndetg)
              - (1/2) (1/9) R_{a b} R_{c d} x^{a} x^{b} x^{c} x^{d}
              - (1/18) R_{a b} \nabla_{c}{R_{d e}} x^{a} x^{b} x^{c} x^{d} x^{e}.
                                                                         # cdb (ex-10.logNdetg.001,logNdetg)

   sort_product   (logNdetg)                                             # cdb (ex-10.logNdetg.002,logNdetg)
   rename_dummies (logNdetg)                                             # cdb (ex-10.logNdetg.003,logNdetg)
   canonicalise   (logNdetg)                                             # cdb (ex-10.logNdetg.004,logNdetg)

   # =================================================================================================
   # the remaining code is just for pretty printing

   def product_sort (obj):
       substitute (obj,$ x^{a}                            -> A000^{a}               $)
       substitute (obj,$ g^{a b}                          -> A001^{a b}             $)
       substitute (obj,$ \nabla_{c}{R_{a b}}              -> A004_{a b c}           $)
       substitute (obj,$ \nabla_{c d}{R_{a b}}            -> A005_{a b c d}         $)
       substitute (obj,$ \nabla_{c d e}{R_{a b}}          -> A006_{a b c d e}       $)
       substitute (obj,$ \nabla_{c d e f}{R_{a b}}        -> A007_{a b c d e f}     $)
       substitute (obj,$ \nabla_{e}{R_{a b c d}}          -> A008_{a b c d e}       $)
       substitute (obj,$ \nabla_{e f}{R_{a b c d}}        -> A009_{a b c d e f}     $)
       substitute (obj,$ \nabla_{e f g}{R_{a b c d}}      -> A010_{a b c d e f g}   $)
       substitute (obj,$ \nabla_{e f g h}{R_{a b c d}}    -> A011_{a b c d e f g h} $)
       substitute (obj,$ R_{a b}                          -> A002_{a b}             $)
       substitute (obj,$ R_{a b c d}                      -> A003_{a b c d}         $)
       sort_product   (obj)
       rename_dummies (obj)
       substitute (obj,$ A000^{a}                 -> x^{a}                          $)
       substitute (obj,$ A001^{a b}               -> g^{a b}                        $)
       substitute (obj,$ A002_{a b}               -> R_{a b}                        $)
       substitute (obj,$ A003_{a b c d}           -> R_{a b c d}                    $)
       substitute (obj,$ A004_{a b c}             -> \nabla_{c}{R_{a b}}            $)
       substitute (obj,$ A005_{a b c d}           -> \nabla_{c d}{R_{a b}}          $)
       substitute (obj,$ A006_{a b c d e}         -> \nabla_{c d e}{R_{a b}}        $)
       substitute (obj,$ A007_{a b c d e f}       -> \nabla_{c d e f}{R_{a b}}      $)
       substitute (obj,$ A008_{a b c d e}         -> \nabla_{e}{R_{a b c d}}        $)
       substitute (obj,$ A009_{a b c d e f}       -> \nabla_{e f}{R_{a b c d}}      $)
       substitute (obj,$ A010_{a b c d e f g}     -> \nabla_{e f g}{R_{a b c d}}    $)
       substitute (obj,$ A011_{a b c d e f g h}   -> \nabla_{e f g h}{R_{a b c d}}  $)

   def get_term (obj,n):

       x^{a}::Weight(label=xnum).

       foo := @(obj).
       bah  = Ex("xnum = " + str(n))
       keep_weight (foo,bah)

       return foo

   def reformat (obj,scale):
       foo  = Ex(str(scale))
       bah := @(foo) @(obj).
       distribute     (bah)
       product_sort   (bah)
       rename_dummies (bah)
       canonicalise   (bah)
       sort_sum       (bah)
       factor_out     (bah,$x^{a?}$)
       ans := @(bah) / @(foo).
       return ans

   def rescale (obj,scale):
       foo  = Ex(str(scale))
       bah := @(foo) @(obj).
       distribute  (bah)
       factor_out  (bah,$x^{a?}$)
       return bah

   # ---------------------------------------------------------------
   # reformat Ndetg

   Rterm0 = get_term (Ndetg,0)       # cdb (ex-10.Rterm0.701,Rterm0)
   Rterm1 = get_term (Ndetg,1)       # cdb (ex-10.Rterm1.701,Rterm1)
   Rterm2 = get_term (Ndetg,2)       # cdb (ex-10.Rterm2.701,Rterm2)
   Rterm3 = get_term (Ndetg,3)       # cdb (ex-10.Rterm3.701,Rterm3)
   Rterm4 = get_term (Ndetg,4)       # cdb (ex-10.Rterm4.701,Rterm4)

   Rterm0 = reformat (Rterm0,  1)    # cdb (ex-10.Rterm0.702,Rterm0)
   Rterm1 = reformat (Rterm1,  1)    # cdb (ex-10.Rterm1.702,Rterm1)
   Rterm2 = reformat (Rterm2,  3)    # cdb (ex-10.Rterm2.702,Rterm2)
   Rterm3 = reformat (Rterm3,  6)    # cdb (ex-10.Rterm3.702,Rterm3)
   Rterm4 = reformat (Rterm4,180)    # cdb (ex-10.Rterm4.702,Rterm4)

   Ndetg := @(Rterm0) + @(Rterm1) + @(Rterm2) + @(Rterm3) + @(Rterm4).  # cdb (ex-10.Ndetg.701,Ndetg)

   # ---------------------------------------------------------------
   # reformat sqrtNdetg

   Rterm0 = get_term (sqrtNdetg,0)   # cdb (ex-10.Rterm0.801,Rterm0)
   Rterm1 = get_term (sqrtNdetg,1)   # cdb (ex-10.Rterm1.801,Rterm1)
   Rterm2 = get_term (sqrtNdetg,2)   # cdb (ex-10.Rterm2.801,Rterm2)
   Rterm3 = get_term (sqrtNdetg,3)   # cdb (ex-10.Rterm3.801,Rterm3)
   Rterm4 = get_term (sqrtNdetg,4)   # cdb (ex-10.Rterm4.801,Rterm4)

   Rterm0 = reformat (Rterm0,  1)    # cdb (ex-10.Rterm0.802,Rterm0)
   Rterm1 = reformat (Rterm1,  1)    # cdb (ex-10.Rterm1.802,Rterm1)
   Rterm2 = reformat (Rterm2,  6)    # cdb (ex-10.Rterm2.802,Rterm2)
   Rterm3 = reformat (Rterm3, 12)    # cdb (ex-10.Rterm3.802,Rterm3)
   Rterm4 = reformat (Rterm4,360)    # cdb (ex-10.Rterm4.802,Rterm4)

   sqrtNdetg := @(Rterm0) + @(Rterm1) + @(Rterm2) + @(Rterm3) + @(Rterm4).  # cdb (ex-10.sqrtNdetg.801,sqrtNdetg)

   # ---------------------------------------------------------------
   # reformat logNdetg

   Rterm0 = get_term (logNdetg,0)    # cdb (ex-10.Rterm0.801,Rterm0)
   Rterm1 = get_term (logNdetg,1)    # cdb (ex-10.Rterm1.801,Rterm1)
   Rterm2 = get_term (logNdetg,2)    # cdb (ex-10.Rterm2.801,Rterm2)
   Rterm3 = get_term (logNdetg,3)    # cdb (ex-10.Rterm3.801,Rterm3)
   Rterm4 = get_term (logNdetg,4)    # cdb (ex-10.Rterm4.801,Rterm4)

   Rterm0 = reformat (Rterm0,  1)    # cdb (ex-10.Rterm0.802,Rterm0)
   Rterm1 = reformat (Rterm1,  1)    # cdb (ex-10.Rterm1.802,Rterm1)
   Rterm2 = reformat (Rterm2,  3)    # cdb (ex-10.Rterm2.802,Rterm2)
   Rterm3 = reformat (Rterm3,  6)    # cdb (ex-10.Rterm3.802,Rterm3)
   Rterm4 = reformat (Rterm4,180)    # cdb (ex-10.Rterm4.802,Rterm4)

   logNdetg := @(Rterm0) + @(Rterm1) + @(Rterm2) + @(Rterm3) + @(Rterm4).  # cdb (ex-10.logNdetg.901,logNdetg)

   checkpoint.append (Ndetg)
   checkpoint.append (sqrtNdetg)
   checkpoint.append (logNdetg)

\end{cadabra}

\clearpage

% =================================================================================================
\section*{The metric determinant in Riemann normal coordinates}

\def\Vert{\vrule height 10pt depth 3pt width 0.5pt}
\def\LVert{\Vert\hskip 1.75pt}
\def\RVert{\hskip 1pt\Vert}

\begin{dgroup*}
   \Dmath*{-\det g(x) = \cdb{ex-10.Ndetg.701}+\cdots}
\end{dgroup*}

% =================================================================================================
\section*{The volume element in RNC}

If $-\det g(x)$ is non-negative then we also have
%
\begin{dgroup*}
   \Dmath*{\sqrt{-\det g(x)} = \cdb{ex-10.sqrtNdetg.801}+\cdots}
\end{dgroup*}

% =================================================================================================
\section*{The log of -detg in RNC}
%
\begin{dgroup*}
   \Dmath*{\log\left(-\det g(x)\right) = \cdb{ex-10.logNdetg.901}+\cdots}
\end{dgroup*}

Apart from the signs, this matches exactly the expression given by Calzetta etal. (eq. A14)

\clearpage

% ============================================================================================
% export to json format

\bgroup
\CdbSetup{action=hide}
\begin{cadabra}
   for i in range( len(checkpoint) ):
      cdblib.put ('check{:03d}'.format(i),checkpoint[i],checkpoint_file)
\end{cadabra}
\egroup

\end{document}


\documentclass{ximera}
\input{../preamble}
\title{Exercises: The Integral Test}
%%%%%\author{Philip T. Gressman}

\begin{document}
\begin{abstract}
Exercises relating to the integral test.
\end{abstract}
\maketitle

\begin{exercise}
The function
\[ \frac{\cos \pi x}{x} \]
is decreasing for $x \geq \answer{N/A}$ (if the function is not ultimately decreasing, enter N/A). The integral test \wordChoice{\choice{does}\choice[correct]{does not}} apply to the series
\[ \sum_{n=1}^\infty \frac{\cos \pi n}{n}. \]
The series \wordChoice{\choice{converges}\choice{diverges}\choice[correct]{can't be determined using this test}}.
\end{exercise}

\begin{exercise}
The function \[ \frac{\ln x}{x} \]
is decreasing for $x \geq \answer{e}$ (if the function is not ultimately decreasing, enter N/A). The integral test \wordChoice{\choice[correct]{does}\choice{does not}} apply to the series
\[ \sum_{n=3}^\infty \frac{\ln n}{n}. \]
We have
\[ \int_3^\infty \frac{\ln x}{x} dx = \answer{\infty}, \]
so the series \wordChoice{\choice{converges}\choice[correct]{diverges}\choice{can't be determined using this test}}.
\end{exercise}

\begin{exercise}
The function \[ \frac{1}{x^2+1} \]
is decreasing for $x \geq \answer{0}$ (if the function is not ultimately decreasing, enter N/A). The integral test \wordChoice{\choice[correct]{does}\choice{does not}} apply to the series
\[ \sum_{n=1}^\infty \frac{1}{n^2+1}. \]
We have that
\[ \int_1^\infty \frac{dx}{x^2 + 1} = \answer{\frac{\pi}{4}}, \]
so the series \wordChoice{\choice[correct]{converges}\choice{diverges}\choice{can't be determined using this test}}.
\end{exercise}

\begin{exercise}
The integral test
\wordChoice{\choice[correct]{does}\choice{does not}} apply to the series
\[ \sum_{n=2}^\infty \frac{1}{n (\ln n)^2}. \]
The series \wordChoice{\choice[correct]{converges}\choice{diverges}\choice{can't be determined using this test}}.
\begin{hint}
Make a substitution $u = \ln x$.
\end{hint}
\end{exercise}

\begin{exercise}
The integral test
\wordChoice{\choice[correct]{does}\choice{does not}} apply to the series
\[ \sum_{n=2}^\infty \frac{1}{n (\ln n)}. \]
The series \wordChoice{\choice{converges}\choice[correct]{diverges}\choice{can't be determined using this test}}.
\begin{hint}
Make a substitution $u = \ln x$.
\end{hint}
\end{exercise}

\begin{exercise}
The integral test
\wordChoice{\choice{does}\choice[correct]{does not}} apply to the series
\[ \sum_{n=2}^\infty e^{-n^2} \cos n \pi . \]
The series \wordChoice{\choice{converges}\choice{diverges}\choice[correct]{can't be determined using this test}}.
\end{exercise}

\begin{exercise}
The integral test
\wordChoice{\choice[correct]{does}\choice{does not}} apply to the series
\[ \sum_{n=2}^\infty n^3 e^{-n^2}. \]
The series \wordChoice{\choice[correct]{converges}\choice{diverges}\choice{can't be determined using this test}}.
\begin{hint}
Make a substitution $u = x^2$ and then integrate by parts.
\end{hint}
\end{exercise}

\begin{exercise}
The function
\[ \frac{x}{(x^2+3)^2} \]
is decreasing for $x \geq \answer{1}$.
By the integral test,
\[ \int_{\answer{8}}^\infty \frac{x}{(x^2+3)^2} dx \leq \sum_{n=8}^\infty \frac{n}{(n^2+3)^2} \leq \int_{\answer{7}}^\infty \frac{x}{(x^2+3)^2} dx. \]
We can approximate the infinite series by the sum of the first seven terms with what bounds on the error?
\[ \frac{1}{\answer{134}} + \sum_{n=1}^7 \frac{n}{(n^2+3)^2} \leq \sum_{n=1}^\infty \frac{n}{(n^2+3)^2} \leq \frac{1}{\answer{104}} + \sum_{n=1}^7 \frac{n}{(n^2+3)^2}. \]
\begin{hint}
Write
\[ \sum_{n=1}^\infty \frac{n}{(n^2+3)^2} = \sum_{n=1}^7 + \sum_{n=8}^\infty \frac{n}{(n^2+3)^2}  \]
and then use the bounds we know for the ``tail'' (i.e., the sum over $n \geq 8$).
\end{hint}
\end{exercise}

\begin{exercise}
Using the integral test, we can determine that the sum of the series
\[ \sum_{n=1}^\infty \frac{1}{n^2} \]
is \wordChoice{\choice{equal to}\choice{greater than}\choice[correct]{less than}} $2$.
\begin{hint}
Compare the series to the partial sum of the first three terms. Don't forget to include an estimate of the remainder.
\begin{hint}
\[ \sum_{n=1}^\infty \frac{1}{n^2} \leq \sum_{n=1}^3 \frac{1}{n^2} + \int_{3}^\infty \frac{dx}{x^2}. \]
\end{hint}
\end{hint}
\end{exercise}

\section*{Sample Quiz Questions}

\begin{question}%%%%%[2019IntegTestErr1]

When approximating the sum of the infinite series
\[ \sum_{n=1}^\infty \frac{1}{n^3} \]
by the sum of the first \(N\) terms, how large must \(N\) be to ensure that the approximation error is less than \(1/200\)? Choose the smallest correct bound among those listed.
\begin{multiplechoice}
\choice{\(N > 5\)}
\choice[correct]{\(N > 10\)}
\choice{\(N > 20\)}
\choice{\(N > 400\)}
\choice{\(N > 8000\)}
\choice{\(N > 160000\)}
\end{multiplechoice}
\begin{feedback}
Because the terms \(n^{-3}\) are positive and decreasing, we know that the partial sums are always less than or equal to the sum of the series. By the Integral Test, we can further say that
\[ \sum_{n=1}^\infty \frac{1}{n^3} - \sum_{n=1}^N \frac{1}{n^3} \leq \int_N^\infty \frac{1}{x^3} dx  = \frac{1}{2N^2}.\]
To be certain that the error is less than \(1/200\), we set \((2 N^2)^{-1} < 1/200\), which gives \(N > 10\).
\end{feedback}

\end{question}

\end{document}


{\bf\large Names:}

\subsection*{Scale Dependent Parametrizations}

Consider a steady flow of 10 cm s$^{-1}$ over a flat bottom in a turbulent ocean.  We will neglect impacts of stratification and rotation.  In a large scale ocean model we would parametrize the drag of the bottom using an nonlinear, quadratic drag law of the form:

\begin{equation}
\tau = C_D \rho u^2
\label{eq:drag}
\end{equation}
where $\tau$ is bottom drag or stress, $\rho \approx 1000$ kg m$^{-3}$ is the density of water, $u$ is the flow speed above the bottom and $C_D$ is the drag coefficient.  The drag coefficient is a bit of a fudge factor and depends on your problem.  Here we will use 0.01.  In SalishSeaCast I tune it to get the tides accurate.

\begin{question}
What is the drag? \\
$\tau = $\\[24pt]
\end{question}

Let's look a little closer, below is a grid diagram
(Figure~\ref{plt:grid}).  Given the flow of 10 cm s$^{^-1}$ at the
lowest grid cell $u_1$, the drag on that velocity is the value given above.

\begin{figure}[h]
\resizebox{3in}{!}{\includegraphics{grid.png}}
\caption{\protect\it{Typical grid near the bottom. $w$ is the vertical
    velocity, the black line is the bottom. No flow through the bottom
    implies $w_0 = 0$.} Lowest horizontal velocity grid point $u_1$ is
  $dz/2$ from the bottom.}
\label{plt:grid}
\end{figure}

\clearpage

In the ``real world'' we would expect a log layer profile (Figure~\ref{plt:loglayer}):

\begin{equation}
u = \max \left[ \frac {u_*}{\kappa} \log \left( \frac z z_* \right), U \right]
\end{equation}
where $u_* = (\tau/\rho)^{1/2}$ is the friction velocity, $\kappa =
0.41$ is von Karman's constant, $z$ is height above the bottom, $z_*$
is the roughness length, here 20 cm, and $U$ is the free stream (above boundary layer) velocity.

\begin{figure}[ht]
\resizebox{5.5in}{!}{\includegraphics{loglayer.png}}
\caption{\protect\it{Log layer velocity profile.}}
\label{plt:loglayer}
\end{figure}

To match the log-layer to the bottom stress parametrization above we
find that
\begin{equation}
u_* = \frac U {C_D^{1/2}}
\end{equation}

Let's assume that the model velocity at each grid point is exactly the log
layer value at the appropriate height above the bottom.  That is, you can
find $u_1$ from Figure~\ref{plt:loglayer} by reading off the graph the
value at $dz/2$.

For a large scale model a  typical bottom layer box thickness, $dz =
30$~m.  In coastal models we usually have better resolution.  What
happens if we use the parametrization (\ref{eq:drag}) but have better resolution?

\clearpage

\begin{question}
What is the drag using the quadratic drag law  (\ref{eq:drag}) for the
flow shown in Figure~\ref{plt:loglayer} for bottom grid resolutions of
$dz = $10, 5 and 2 m?\\
With $dz=10$m , $\tau  = $\\[24pt]
With $dz=5$m , $\tau  = $\\[24pt]
With $dz=2$m , $\tau  = $\\[12pt]
\end{question}

\begin{question}
Should the drag change in this way? Why or why not?
\end{question}

\vspace{1in}

\begin{question}
Noting that our bottom grid cells are not always the same size, in
shallower water they are often smaller, how should we parametrize the
bottom drag?\\[24pt]
\end{question}


\subsection{Pruning decision trees}

Training a decision tree until there is only one entry from the training set will result in overfitting.

We can use pruning to regularise trees.

\subsubsection{Pruning}

\subsubsection{Reduced error pruning}

From bottom, replace each node with a leaf of the most popular class. Accept if no reduction in accuracy.

\subsubsection{Cost complexity pruning}

Take full tree \(T_0\)

Iteratively find a subtree to replace with a leaf. Cost function is accuracy and number of leaves.

Remove this generating \(T_{i+1}\)

When we have just the root, choose a single tree using CV.

\subsubsection{Growing and pruning}

Generally we would split the data up. Grow the tree with one set and then prune with the other.

We can split our data up and iterate between growing and pruning.

When pruning, for each pair of leaves we test to see if they should be merged.

If our two sets are \(A\) and \(B\) we can do:

\begin{itemize}
\item \(A\): Grow
\item \(B\): Prune
\item \(B\): Grow
\item \(A\): Prune
\end{itemize}

And repeat this process.

\subsubsection{Partial regression trees}

Once we have built a tree, we keep a single leaf and disard the rest.


\section{Vector Space}

\subsection{Field}

\begin{definition}
    For $0$ and $1$ of a field $F$, the smallest $n$ that $\displaystyle \sum_{i=1}^n 1 = 0$ is called the \cindex{characteristic} of $F$. If no such $n$ exists, $F$ is called \cindex{characteristic zero}.
    \qed
\end{definition}

\begin{definition}
    The field $Z_2$ has characteristic of $2$ which consists of two elements $0$ and $1$:
    \begin{itemize}
        \item $0 + 0 = 0$
        \item $0 + 1 = 1 + 0 = 1$
        \item $1 + 1 = 0$
        \item $0 \times 0 = 0$
        \item $0 \times 1 = 1 \times 0 = 0$
        \item $1 \times 1 = 1$
    \end{itemize}
\end{definition}

\subsection{Vector}

Algebra is concerned with how to manipulate symbolic combinations of object and how to equate one with another.

\begin{definition}
A \cindex{vector space} vector space $V$ over a \cindex{field} field $F$ has two operation $\{+,\times\}$ with $\vec{0}$ and $1$. \qed
\end{definition}

\begin{definition}
	A \cindex{subspace} is a subset $W$ of vector space $V$ that is closed under $\{+,\times\}$. When we say a subset is a subspace of a vector space, we mean it is a vector space as well.
\end{definition}

\begin{theorem}
    $\{0\}$ is a subspace of all vector space.
    \qed
\end{theorem}

\cindex{matrix} is late Latin for \emph{womb}. The idea is that a matri is a place for holding numbers.

\begin{definition}
	a \cindex{trace} of an $n \times n$ matrix $M$, denoted $\text{tr}(M)$, is the sum of diagonal entries:
	\begin{equation}
		\text{tr}(M) = \sum_{i=1}^n M_{ii}
	\end{equation}
\end{definition}

\begin{definition}
	A \cindex{span} of a nonempty subset $S$ of a vector space $V$ is the set consisting of all linear combinations of the vectors in $S$. If $\text{span}(S) =V$, $S$ \cindex{generate} (or span) $V$.
	\qed
\end{definition}

\begin{definition}
    The span of $\emptyset$ is $\{0\}$, not $\emptyset$.
\end{definition}

A span set is useful because it allow one to describe all vectors in terms of a much smaller space.

\begin{definition}
	A subset $S$ of $V$ is \cindex{linearly dependent} if there exist a finite number of distinct vector $u_1, u_2, \dots, u_n$ in $S$ and scalars $a_1, a_2, \dots, a_n$, not all $0$, that:
	\begin{equation}
		\sum_{i=1}^n a_i u_i = 0
	\end{equation}

	$S$ is called \cindex{linearly independent} if it is not linearly dependent. $\emptyset$ is linearly independent.

	\qed
\end{definition}

\begin{theorem}
	Let $S$ be linearly independent, $v$ is not in $S$. Then $S \cup {v} $ is linearly dependent if $v \in \text{span}(S)$.
\end{theorem}

% basis
\subsection{Basis}

Basis tries to represent a infinite vector space using a finite set of vectors. So a complex structure could be understood using simplified structure. A linearly independent generating set has a very useful property that every vector has one and only one representation using basis.

\begin{definition}
	A \cindex{basis} $\beta$ for $V$ is a linearly independent subset of $V$ that generate $V$.
	\qed
\end{definition}

A vector space is usually infinite. It is desirable to describe this infinite set using a finite subset, which is  the role of basis.

\begin{theorem}
    $\emptyset$ is a basis for zero vector space $\{0\}$, so every vector space has a basis.
\end{theorem}

\begin{definition}
    The \cindex{standard basis for $F^n$} is $e_1=(1,0,0,\dots,0)$, $e_2=(0,1,0,\dots,0)$, $e_n=(0,0,\dots,1)$.
\end{definition}

\begin{definition}
    The \cindex{standard basis for $P_n(F)$} is $\{1,x,x^2,\dots,x^n\}$.
\end{definition}

\begin{theorem}
	$\beta$ is a basis of $V$ if $\forall v \in V $, $v$ has a unique representation as a linear combination of vectors of $\beta$.
\end{theorem}

\begin{theorem}
    A finite spanning set for $V$ can be reduced to a basis.
\end{theorem}

\begin{theorem}[\cindex{Replacement Theorem}]
	Let $V$ be generated by a set $G$ with $n$ vectors. Let $L$ be a linearly independent subset of $V$ with $m$ vectors. Then $m < n$ and $\exists H \subset G$ with $n-m$ vectors such that $L \cup H$ generate $V$.
	\qed
\end{theorem}

\begin{theorem}
    Let $V$ have a finite basis. Then every basis contains the same number of vectors. This number is an intrinsic property of $V$ and called the \cindex{dimension} of $V$.
\end{theorem}

\begin{theorem}
    Let $V$ be a vector space with dimension $n$:
    \begin{itemize}
        \item any finite generating set for $V$ contains at least $n$ vectors. If they contains exactly $n$ vectors, they are a basis.
        \item any linearly independent subset of $n$ vectors is a basis.
        \item every linearly independent subset could be extended to a basis.
    \end{itemize}
\end{theorem}

\begin{definition}[\cindex{Lagrange Interpolation Formula}]\label{lagrangeinterpolationformula}
let ${c_0, c_1, \dots, c_n}$ be distinct scalars in field $F$. Define  $n+1$ function $\set{f_i}$ as:
	\begin{equation}
	f_i(x) = \prod_{k=0, k \neq i}^n \frac{x - c_k}{c_i - c_k}
\end{equation}
then $\beta = \set{f_i}$ is a basis of $\mathbb{P}_n(F)$, where \cindex{$\mathbb{P}_n(F)$} is a set of all polynomials over $F$. For $\forall g \in \mathbb{P}_n(F)$, we have
	\begin{equation}
		g = \sum_{i=0}^n g(c_i) f_i
	\end{equation}

	To generate a function $g$ of degree $n$ that passes $n+1$ points $(x_i, y_i)$, first use $\set{x_i}$ to generate $\set{f_i}$, then $g = \sum\limits_{i=0}^n y_i f_i $.
\end{definition}

\begin{proof}
	since $\beta$ is a basis of $\mathbb{P}_n(F)$, $\forall g \in \mathbb{P}_n(F)$,
	\begin{equation*}
		g = \sum_{i=0}^n b_i f_i
	\end{equation*}
	it follows that
	\begin{equation*}
		g(c_j) = \sum_{i=0}^n b_i f_i(c_j) = b_j
	\end{equation*}

	so $g = \sum\limits_{i=0}^n g(c_i) f_i$.
\end{proof}

\begin{theorem}
for any two subspace $W_1$ and $W_2$ of $V$, their dimension has a relation:
\begin{equation}
	\text{dim}(W_1 + W_2) = \text{dim}(W_1) + \text{dim}(W_2) - \text{dim}(W_1 \cap W_2)
\end{equation}
\end{theorem}

\begin{definition}
    here are the definition of common terms:
    \begin{enumerate}
        \item \cindex{square matrix}: a matrix $M_{i \times j}$ that $i = j$. It is usually denoted as $M$, not $A$.
        \item \cindex{zero vector}: $\vec{0}$.
        \item \cindex{transpose}: $\left(A^\top\right)_{ij} = A_{ji}$.
        \item \cindex{symmetric matrix}: $A^\top = A$.
        \item \cindex{diagonal matrix}: for a $n \times n$ square matrix $M$ that $M_{ij} = 0$ if $i \neq j$.
        \item \cindex{upper triangular}: $A_{ij} = 0$ if $i > j$.
    \end{enumerate}
    \qed
\end{definition}

The following text discusses the result of infinite basis.

\begin{definition}
    Let $F$ be a family of sets. A member $M$ of $F$ is called \cindex{maximal} if $M$ is contained in no member of $F$ other than $M$ itself.
\end{definition}

\begin{definition}
    A collection of set $C$ is called a \cindex{chain} if for each pair of sets $A$ and $B$ in $C$, either $A \subseteq B$ or $B \subseteq A$.
\end{definition}

\begin{theorem}
    Let $F$ be a family of sets. If for each chain $C \subseteq F$, there exists a member of $F$ that contains each member of $C$, then $F$ contains a maximal member.
\end{theorem}

\begin{proof}
    use axiom of choice. Note that the maximal member may not be in $C$.
\end{proof}

\begin{definition}
    Let $S$ be a subset of a vector space $V$. A \cindex{maximal linearly independent subset} of $S$ is a subset $B$ of $S$ that:
    \begin{enumerate}
        \item $B$ is linearly independent.
        \item The only linearly independent subset of $S$ that contains $B$ is $B$.
    \end{enumerate}
\end{definition}

\begin{theorem}
    If $V$ has a basis $\beta$, $\beta$ is maximal linearly independent.
\end{theorem}
\begin{proof}
    A basis is linearly independent. Because a basis generate $V$, nothing could be added to it and still make it linearly independent.
\end{proof}

\begin{theorem}
    Let $V$ be a vector space and $S$ a subset that generate $V$. If $\beta$ is a maximal linearly independent subset of $S$, then $\beta$ is a basis $V$.
\end{theorem}
\begin{proof}
    $\beta$ is linearly independent, so only need to prove that $\beta$ generate $V$. It is easy because $\beta$ is maximal in $S$ so nothing from $S$ could be added to it.
\end{proof}

\begin{theorem}
    Let $S$ be a linearly independent subset of a vector space $V$. There exists a maximal linearly independent subset of $V$ that contains $S$.
\end{theorem}
\begin{proof}
    Let $F$ be a family of all linearly independent subsets of $V$ that contains $S$. For a chain $C$ in $F$, let $U$ be the union of all its member. This $U$ is linearly independent and belongs to $F$, so it is a maximal linearly independent subset of $F$, which is a basis of $F$.
\end{proof}

\begin{theorem}
    Every vector space has a basis.
\end{theorem}


\section{\texorpdfstring{RSA}{RSA}}
\vspace{5mm}
\large

Setup:
\begin{itemize}
	\item $p,q$ big random primes.
	\item $n:= pq$
	\item $\varphi(n) = (q - 1)(p - 1)$
	\item $(e, \varphi(n)) = 1$ is an encryption exponent
	\item select decryption exponent $d$ s.t $ed = 1 \mod(\varphi(n))$
	\item $(e, n)$ is an enc key, $(d, n)$ is an dec key
\end{itemize}

Then we encrypt by calculating $E(X) = x^e \mod n$ and decrypt $E(y) = y^d \mod n$.
Assuming $(x, n) = 1$, last equality by the Euler theorem
\[ (x^e)^d \equiv x^{ed} \equiv x^{1 + k\varphi(n)} \equiv x \cdot x^{k \varphi(n)} \equiv x \cdot (x^{\varphi(n)})^k \equiv x \]

Therefore, encryption using RSA is invertible.
Relies on the hardness of factorisation.

RSA is slow, but polynomial. Which leads to hybrid ciphers.
However, we can made some adjustments to improve computations:
\begin{itemize}
	\item choose small public exponent (3, 17, $65537 = 2^{16} + 1$)
	\item use CRT for private calculations (compute mod p, mod q and combine using CRT)
\end{itemize}

\begin{properties} RSA
\begin{itemize}
	\item Commutative: $D_2(D_1(E_2(E_1(x)))) = x$.
	\item Homomorphic: $E(x_1 x_2) = (x_1 x_2)^e = x_1^e x_2^e = E(x_1) E(x_2)$.
\end{itemize}
\end{properties}

\begin{example}
	RSA usage for \textbf{Blind signature}:

	Firstly, Alice signs some text. Bob want to sign message secretely from Alice. Steps:
	\begin{enumerate}
		\item Bob generates $b \in_R \Z_n^{\ast}$, sends $x b^d$ to Alice
		\item Alice sends back $(x b^d)^e = x^e b$
		\item Bob calculates $x^e b b^{-1} = x^e$ and gets the signature done by Alice.
	\end{enumerate}
\end{example}

\begin{definition}
	Semantic security - any properties of the plain text cannot be efficienlty computed from cipher text.
\end{definition}

\paragraph{Attacks on RSA}
\begin{enumerate}
	\item If $x < \sqrt[e]{n} \Rightarrow$ decrypt is e-th root in $\Z_n$, not that hard.
	\item If anybody knows $\varphi(n) \Rightarrow$ he can factorise $n$.
	\item If anybody knows $e,d \Rightarrow$ he can factorise $n$.
	\item Wiener. If $d < \sqrt[4]{n} \Rightarrow$ d could be obtained from $e$ probabilistically.
	\item Meet in the meedle: we know $c = m^e$, $e$ is also public. Try random small $u, v$ to get a collision.
		Then
		\[ u^e \equiv c v^{-e} \iff u^e v^e = m^e \iff (uv)^e \equiv m^e \Rightarrow ev = m \]
	\item similar massages: $m, (m + \delta)$.
		\[ c = m^e \land c^{\prime} = (m + \delta)^e \iff P(m) = m^e - c \land P^{\prime}(m) = (m + \delta)^e - c^{\prime} \]
			Therefore, $m$ is the root of 2 polynomials.
			If $e$ is small $\Rightarrow P, P^{\prime}$ have small degree and $m$ is a root of $gcd(P, P^{\prime})$.
			Which is a linear polynomial with high probability.

			Alternatively, the attacker can guess $\delta$.
	\item $p$ and $q$ are close to each other $\Rightarrow$ easier factorisation of $n$.
	\item single message encrypted with the same e but different mod.
		Ex: $e = 3, n_1, n_2, n_3$ are 3 different residuals
		\begin{gather*}
			x^3 = y_1 \mod n_1\\
			x^3 = y_2 \mod n_2\\
			x^3 = y_3 \mod n_3
		\end{gather*}
		So, \[x < \min(n_1, n_2, n_3) \Rightarrow x^3 < n_1n_2n_3\] and $x^3$ can be calculated using CRT. Then, cubic root.

		Solutions: high $e$ or randomize messages.
	\item Not 100\% Semantic secure, as RSA leaks Jacobi Symbol
		\[ \left(\frac{x}{p}\right) \left(\frac{x}{q}\right) \]
		Which is approximately 1 bit of information.

	However, computing parity of $x$ from $E(x)$ is equivalent to full decryption.
\end{enumerate}

To avoid many pitfals of RSA design, we can use Padding scheme.
\begin{itemize}
	\item PKCS v1.5: mix message and random bits:

		\includegraphics[scale=0.4]{pkcs_0.eps}

		There exists a oracle padding attack, Bleichenbacher.
	\item PKCS v2: add 2 hash functions and XORs (2 Feistel networks)

		\includegraphics[scale=0.4]{pkcs_1.eps}

		Proved to be secure against oracle padding attack.
\end{itemize}

\subsection{Diffie-Hellman key exchange protocol}
Parameters:\\
1) $p$ is a prime\\
2) $\langle g \rangle = \Z_p^{\ast}$ generator

\includegraphics[scale=0.4]{dh.eps}

Problems:
\begin{itemize}
	\item Man-in-the-middle could alter messages. Solution: sign the result
	\item Attack on parameters: replace $g$ by $g^k$ which generates only a subgroup of $H \leq \Z_p^{\ast}$
		Solution: check the parameters.
	\item Powering attack: Mallory replaces $g^x$ sent by Bob by $g^{kx}$, similarly for Alice.
		Computed signature on both sides still matches, however we are in the smaller subgroup again.
		Whick makes finding the generator much easier.

		Solution: sign the whole communication.
	\item D-H leaks whether $g^{xy}$ is a quadratic residue, $<$ 1 bit leak
	\item Common trick to fix subgroup attacks: choose safe prime $p = 2q - 1$.
		From Lagrange theorem, there are only 2 non trivial subgroups, larger of them is subgroup of quadratic residues. Easy to check.
\end{itemize}

\paragraph{El Gamal cipher based on D-H}
\includegraphics[scale=0.4]{dh_1.eps}

\begin{itemize}
	\item Params: prime $p$, $\langle g \rangle = \Z_p^{\ast}$ generator
	\item Keys: $k \in_R \{ 0, ..., p-2 \}$ secret.
		$h = g^k \mod p$ public
	\item Enc: $t \in_R \{ 0, ..., p-2 \}$
		\[ s = h^t = g^{kt} \mod p, y = xs \]
		Send $(g^t, y)$.
	\item Dec: Calculate $(g^t)^k$
		\[ x = y \cdot s^{-1} \mod p \]
\end{itemize}

\begin{note}
	We can also use different Algebraic structures with even harder descrete log, e.g. Elliptic curves.
\end{note}


% Copyright (c) 2019, Betsalel (Saul) Williamson, Jordan Henderson (the Authors)
% All rights reserved.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are met:
%     * Redistributions of source code must retain the above copyright
%       notice, this list of conditions and the following disclaimer.
%     * Redistributions in binary form must reproduce the above copyright
%       notice, this list of conditions and the following disclaimer in the
%       documentation and/or other materials provided with the distribution.
%     * Neither the names of the Authors nor the
%       names of its contributors may be used to endorse or promote products
%       derived from this software without specific prior written permission.
%
% THIS SOFTWARE IS PROVIDED BY THE Authors ``AS IS'' AND ANY
% EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
% WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
% DISCLAIMED. IN NO EVENT SHALL THE Authors BE LIABLE FOR ANY
% DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
% (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
% LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
% ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
% (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
% SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

\section{Example Math}

\subsection{Simple equation with a number}

I can reference this equation \ref{eqn:sample-dmath} by adding a unique label.

\begin{dmath}
\frac{V_{in}-V_x}{R_1}- \frac{V_x}{Z_1} + \frac{V_x - V_o}{Z_1} = 0
\label{eqn:sample-dmath}
\end{dmath}

\subsection{A dmath group with no align}
\begin{dgroup}[noalign]
\begin{dmath}
\frac{V_{in}-V_x}{R_1}- \frac{V_x}{Z_1} + \frac{V_x - V_o}{Z_1} = 0
\end{dmath}
\begin{dmath}
\frac{V_{in}}{R_1} = -\frac{V_o}{R2} - \frac{V_o Z_1}{R_2 Z_1} - \frac{V_o}{Z_1}- \frac{Z_1 V_o}{R_1 R_2}
\end{dmath}
\label{eqn:sample-dgroup-noalign}
\end{dgroup}

\subsection{A dmath group with align on}

\begin{dgroup}
\begin{dmath}
\frac{V_{in}-V_x}{R_1}- \frac{V_x}{Z_1} + \frac{V_x - V_o}{Z_1} = 0
\end{dmath}
\begin{dmath}
\frac{V_{in}}{R_1} = -\frac{V_o}{R2} - \frac{V_o Z_1}{R_2 Z_1} - \frac{V_o}{Z_1}- \frac{Z_1 V_o}{R_1 R_2}
\end{dmath}
\label{eqn:sample-dgroup}
\end{dgroup}

\subsection{In-line Math}
Sample with math included in-line with text along with units. $f_0$ at 620 \si{\hertz}.

\subsection{Symbols}

\begin{dmath}
\frac{4.3 \si{\volt}}{1 \si{\kilo\ohm}} = 4.3 \si{\milli\ampere}
\end{dmath}

\subsection{Math With Normal Text}

\begin{dmath}
\text{Average rate of speech} = \frac{\text{Words spoken}}{\text{Number of Seconds}}
\label{eqn:sample-math-with-regular-text}
\end{dmath}


\lesson{5}{Oct 18 2021 Mon (11:31:19)}{Completing the Square}{Unit 2}

\subsubsection*{Intro to the Discriminant}

The formula for the \bf{discriminant} is:

\begin{align}
    b^2 - 4ac
\end{align}

Let's try a quick example:

\begin{example}
    \begin{align}
        f(x) &= x^2 + 12x + 26 \\
             &= b^2 - 4ac \\
             &= (12)^2 - 4(1)(26) \\
             &= 144 - 104 \\
             &= 40
    \end{align}

    So, the discriminant isn't a perfect square. This tells us that there will be two irrational numbers.
\end{example}

\subsubsection*{Using Vertex Form: Minimum or Maximum}

\begin{example}
    \begin{align}
        f(x) = (x + 6)^2 - 10
    \end{align}

    From this equation, we get the vertex of the parabola, which is $(-6, -10)$
    But, it raises the question. Is it the vertex minimum, or the vertex maximum?

    To solve this, you must look at the value or the leading coefficient to make this determination.

    In this case, it would be $a = 1$, which is positive, meaning it goes upward.
\end{example}

\newpage


\subsection{Functions of integers}

\subsubsection{Addition}

Then we can define addition as:

\((a,b)+(c,d)=(a+c,b+d)\)

Integer addition can then be defined:

\(a+b=\{\{a_1\},\{a_1,a_2\}\}+\{\{b_1\},\{b_1,b_2\}\}\)

\(a+b=\{\{a_1+b_1\},\{a_1+b_1,a_2+b_2\}\}\)

Or:

\(a+b=c\)

\(c_1=a_1+b_1\)

\(c_2=a_2+b_2\)

\subsubsection{Multiplication}

Similarly, multiplication can be defined as:

\((a,b).(c,d)=(ac+bd, ad+bc)\)

\(ab=c\)

\(c_1=a_1b_1+a_2b_2\)

\(c_2=a_2b_1+a_1b_2\)

\subsubsection{Subtraction}

\(a-b=c\)

\(c_1=a_1+b_2\)

\(c_2=a_2+b_1\)


% This file was converted to LaTeX by Writer2LaTeX ver. 1.6.1
% see http://writer2latex.sourceforge.net for more info
\documentclass[letterpaper]{article}
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{amssymb,amsfonts,textcomp}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage{color}
\usepackage{array}
\usepackage{hhline}
\usepackage{hyperref}
\hypersetup{colorlinks=true, linkcolor=blue, citecolor=blue, filecolor=blue, urlcolor=blue}
% Outline numbering
\setcounter{secnumdepth}{0}
% Page layout (geometry)
\setlength\voffset{-1in}
\setlength\hoffset{-1in}
\setlength\topmargin{0.7874in}
\setlength\oddsidemargin{0.7874in}
\setlength\textheight{9.4251995in}
\setlength\textwidth{6.9251995in}
\setlength\footskip{0.0cm}
\setlength\headheight{0cm}
\setlength\headsep{0cm}
% Footnote rule
\setlength{\skip\footins}{0.0469in}
\renewcommand\footnoterule{\vspace*{-0.0071in}\setlength\leftskip{0pt}\setlength\rightskip{0pt plus 1fil}\noindent\textcolor{black}{\rule{0.25\columnwidth}{0.0071in}}\vspace*{0.0398in}}
% Pages styles
\makeatletter
\newcommand\ps@Standard{
  \renewcommand\@oddhead{}
  \renewcommand\@evenhead{}
  \renewcommand\@oddfoot{}
  \renewcommand\@evenfoot{}
  \renewcommand\thepage{\arabic{page}}
}
\makeatother
\pagestyle{Standard}
\title{}
\author{}
\date{2021-06-01}
\begin{document}
{\centering\sffamily\bfseries
Document Distance
\par}

\section{Problem Definition}
Define a document as a sequence of words (alphanumeric characters), removing all the punctuation and stuff from it all.
If we have two such documents, how similar are they (how many words do they have overlapping)?

\section{Solution}
Since we don't care about the order of the words in each document  $D$, we can think of each document as a vector, with
$D(w)$ \ being the number of occurrences of the word  $w$ \ in  $D$. We can then define distance as:

\begin{equation*}
d'(D_{1,}D_2)=D_1\cdot D_2=\sum _WD_1[W]\cdot D_2[W]
\end{equation*}
This does work as an okay start, but it does not scale well. This formula really counts the number of words we have in
common, so despite two large documents having a high percentage of words in common, this formula would yield a very
high value, because a small percentage of a large document is still a lot of words. So, we need to clamp this to a
fixed range using an alternate definition of the dot product,  $D_1\cdot D_2=|D_1||D_2|\cos (\theta )$. Defining:

\begin{equation*}
d''(D_{1,}D_2)=\frac{d'(D_{1,}D_2)}{|D_1|\cdot |D_2|}
\end{equation*}
We can see that for  $\theta \in [0,\frac{\pi } 2]$

 $\theta =\cos ^{-1}\left(\frac{d'(D_{1,}D_2)}{|D_1|\cdot |D_2|}\right)$, thus clamping it to a value. If  $\theta $ is
zero, both documents are exactly the same, and at the other end of the range, we have fully different documents.

\clearpage
\bigskip

\section{Algorithm}
\texttt{Document Distance:}

{\ttfamily
/* }

{\ttfamily
\ * 1. Split each document into words}

{\ttfamily
\ * 2. Count word frequencies (document vectors)}

{\ttfamily
\ * 3. Compute dot product}

{\ttfamily
\ */ }

\bigskip

{\ttfamily
def document\_distance(doc1, doc2):}

{\ttfamily
\ \ list1 = get\_list\_of\_words(doc1)}

{\ttfamily
\ \ list2 = get\_list\_of\_words(doc2)}

\bigskip

{\ttfamily
\ \ d1 = count\_frequencies(list1)}

{\ttfamily
\ \ d2 = count\_frequencies(list2)}

\bigskip

{\ttfamily
\ \ sum = \textrm{dot\_product(d1, d2)}}

\bigskip

{\ttfamily
\textrm{\ \ normalized = sum / (len(list1) * len(list2))}}

\bigskip

{\ttfamily
\textrm{\ \ return normalized }}

\bigskip

{\ttfamily
def get\_list\_of\_words(doc):}

{\ttfamily
\ \ start = 0}

{\ttfamily
\ \ end = 1}

{\ttfamily
\ \ A = ord(`A')}

{\ttfamily
\ \ Z = ord(`Z')}

{\ttfamily
\ \ a = ord(`a')}

{\ttfamily
\ \ z = ord(`z')}

{\ttfamily
\ \ zero = ord(`0')}

{\ttfamily
\ \ nine = ord(`9')}

{\ttfamily
\ \ space = ` `}

{\ttfamily
\ \ word\_list = []}

\bigskip

{\ttfamily
\ \ for c in doc:}

{\ttfamily
\ \ \ \ val = ord(c)}

{\ttfamily
\ \ \ \ if A {\textless} val {\textless} Z or a {\textless} val {\textless} z or zero {\textless} val {\textless} nine:}

{\ttfamily
\ \ \ \ \ \ end += 1}

{\ttfamily
\ \ \ \ elif val == space:}

{\ttfamily
\ \ \ \ \ \ word\_list.append(doc[start:end])}

{\ttfamily
\ \ \ \ \ \ start = end}

{\ttfamily
\ \ \ \ \ \ end += 1}

{\ttfamily
\ \ return word\_list}

\bigskip

{\ttfamily
def count\_frequencies(word\_list):}

{\ttfamily
\ \ count = \{\}}

\bigskip

{\ttfamily
\ \ for word in word\_list:}

{\ttfamily
\ \ \ \ if word in count:}

{\ttfamily
\ \ \ \ \ \ count[word] += 1}

{\ttfamily
\ \ \ \ else:}

{\ttfamily
\ \ \ \ \ \ count[word] = 1}

\bigskip

{\ttfamily
\ \ return count}

\bigskip

def dot\_product(d1, d2):

\ \ sum = 0

\ \ for key in d1:

\ \ \ \ if key in d2:

\ \ \ \ \ \ sum += d1[key] * d2[key]

\bigskip

\ \ return sum

\bigskip
\end{document}


\section{Induction}

\frame{
{Part 2: Induction}

\tableofcontents[currentsection,hideallsubsections, firstsection=2, sections={2-4}]
}

\subsection{Introductory Examples}

\begin{frame}{An initial induction (1/2)}
  Imagine that I want to color the natural numbers ($\mathbb{N} \geq 0$), using the following rules:\bigskip

  \begin{itemize}
    \item Number $0$ is \alert{red}
    \item Any integer next to a \alert{red} number is also \alert{red}
  \end{itemize}\bigskip

  Using these rules, can you imagine how the set $\mathbb{N}$ looks like?
\end{frame}

\begin{frame}{An initial induction (2/2)}

  \begin{center}
    Result: \alert{0,1,2,3,4,...}
  \end{center}\bigskip

  The "rule of reds" gives us a general idea of induction:

  \begin{itemize}
  \item $R(0)$ is True
  \item $R(0) \rightarrow R(1); R(1) \rightarrow R(2); R(2) \rightarrow R(3); \ldots$
  \item $R(n) \rightarrow R(n+1)$ for every $n \in \mathbb{N}$
  \end{itemize}\bigskip

  Induction can be used to prove a predicate that depends on some $n \in \mathbb{N}$ by modus ponens.
  \begin{equation*}
    \frac{R(0), R(n)\rightarrow R(n+1), n\in\mathbb{N}}{\forall n, R(n)}
  \end{equation*}
\end{frame}

\begin{frame}{Example of proof by Induction}

  Let's prove that:
  \begin{equation*}
    P(n): 1 + r + r^2 + r^3 + \ldots + r^n = \frac{r^{n+1}-1}{r-1}, r \neq 1, \forall n \in \mathbb{N}
  \end{equation*}\bigskip

  Remember the modus ponens rule for induction:
  \begin{equation*}
    \frac{P(0), P(n)\rightarrow P(n+1), n\in\mathbb{N}}{\forall n, P(n)}
  \end{equation*}\bigskip

  To prove the bottom part by induction, we need to prove the top part.

  \begin{itemize}
  \item First Step: Prove $P(0)$
  \item Second Step: Prove $P(n) \rightarrow P(n+1)$
  \end{itemize}
\end{frame}

\begin{frame}{Example of proof by Induction}

  \begin{proof}
    Proof by induction on $n$

    {\bf First Step:} Prove $P(0)$
    \begin{itemize}
    \item $P(0)$, left side: $r^0 = 1$
    \item $P(0)$, right side: $\frac{r^{0+1}-1}{r-1} = \frac{r-1}{r-1} = 1$
    \end{itemize}\medskip

    {\bf Second Step:} Prove $P(n) \rightarrow P(n+1)$
    \begin{itemize}
    \item $P(n+1)$, left side: $1 + r + r^2 + \ldots + r^n + r^{n+1}$, which is equal to $P(n)+r^{n+1}$
    \item Because $P(n)$ is True, $P(n)+r^{n+1} = \frac{r^{n+1}-1}{r-1} + r^{n+1} = \frac{(r^{n+1}-1)}{r-1} + \frac{(r^{n+1}(r-1))}{r-1}$
    \item Algebra: $\frac{(r^{n+1}-1) + (r^{n+1}(r-1))}{r-1} = \frac{r^{n+1} - 1 + r^{n+2} - r^{n+1}}{r-1} = \frac{r^{n+2} - 1 + (r^{n+1} - r^{n+1})}{r-1}$
    \item $\frac{r^{n+2} - 1}{r-1} = \frac{r^{(n+1)+1} - 1}{r-1}$, which is the right side of $P(n+1)$
    \end{itemize}
  \end{proof}
\end{frame}

\subsection{Proof by Induction}
\begin{frame}{Review: Proof Template for Induction}

  {\larger
    {\bf Proof by induction on $n$}\\
    Proof hypothesis: $P(n) = \ldots$ for all $n \in \mathbb{N}. n \geq 0$\\

    \bigskip

    First we prove $P(0)$.\\
    $\ldots$ \emph{(calculate that P(0) is True)}\\
    $\ldots$\\

    \bigskip

    Second we prove that $\forall n \geq 0, P(n) \rightarrow P(n+1)$\\
    $\ldots$ \emph{(calculate P(n+1) using P(n))}\\
    $\ldots$\\

    \bigskip

    This completes the proof that $P(n)$ for all $n\in\mathbb{N}$\hfill$\qed$
  }
\end{frame}

\subsection{The Statue Park}

\begin{frame}{The Statue Park}{A more complex proof by induction}

  The university is making a new park with the following rules:
  \begin{itemize}
    \item The park is square, with side $2^n$;
    \item In the middle of the park, there is a statue, size $1\times 1$;
    \item Other than that, the park is made of L-shaped tiles, with size $3m^2$;
  \end{itemize}\bigskip

  How can we prove that it is possible to build this park for any $n$?
\end{frame}

\begin{frame}[t]{The Statue Park}{Drawing Proof}
  Remember the rule of induction:
  \begin{itemize}
    \item Prove that $P(0)$ is true.
    \item Assume that $P(n)$ is true, then prove that $P(n) \implies P(n+1)$
  \end{itemize}
\end{frame}

\subsection{BAD induction proof}

\begin{frame}[t]{BAD Proof by induction: All horses are of the same color}

  $P(n) ::=$ for any \structure{set} with \alert{exactly n horses}, all horses have the same color.\bigskip

  \begin{itemize}

  \item<2-> {\bf Prove P(1)}: For any set with one horse, all horses have the same (one) color.

  \item<3-> {\bf Assume P(n) is true:} For any set with $n$ horses, all horses have the same color.
  \item<4-> {\bf Show that $P(n) \implies P(n+1)$:}
    \begin{itemize}
    \item <5->Consider the set of n+1 horses: $H = h_1, h_2, \ldots, h_n, h_{n+1}$
    \item <6->Subset A ($h_1, h_2, \ldots, h_n$): has $n$ horses, so all horses have the same color.
    \item <7->Subset B ($h_2, \ldots, h_n, h_{n+1}$): \alert{also} has $n$ horses, so all horses have the same color.
    \item <8->Horse $h_2$ is in subset $A$ {\bf and} in subset $B$, so subset $A$ and $B$ have the same color.
    \end{itemize}
  \item<9-> Since we showed that P(n+1) is true if P(n) is true, then all horses for any group size have the same color.
  \end{itemize}\bigskip

  \only<10>{\alert{QUIZ}: What is wrong with this proof?}
\end{frame}

\begin{frame}{What is wrong with the horse proof?}

  The second step, when we show that $P(n) \implies P(n+1)$ is not valid.\bigskip

  \begin{itemize}
    \item The implication proof depends on "$h_i$ belongs to subsets $A$ and $B$".
    \item But is this ALWAYS true?
    \begin{itemize}
      \item When $n+1 = 2$, The $n+1$ set is $\{h_1, h_2\}$, set $A = h_1$, set $B = h_2$;
      \item But in this case, {\bf there is no $h_i$ that is common to $A$ and $B$}!
    \end{itemize}
    \item So the implication proof is not valid when $P(2)$.
  \end{itemize}\bigskip

  Note that this is the only problem with the proof!
\end{frame}

\subsection{Strong Induction}

\begin{frame}
  \frametitle{Strong Induction}

  {\larger
    \begin{itemize}
    \item In regular induction, you assume P(n) to show P(n+1)

      \bigskip

    \item In strong induction, you assume P(0), P(1), P(2) \ldots
      P(n), and use all of them to show P(n+1)
    \end{itemize}
  }
\end{frame}

\begin{frame}
  \frametitle{Strong Induction Example: Stacking Game}
  {\larger
  \begin{itemize}
  \item Begin with a stack of 10 blocks
  \item Divide it in two (a,b): for example, 2 and 8 blocks.
  \item You get $a\times b$ points: 10 points
  \item Repeat with the new stacks until all stacks have 1 block.
  \end{itemize}

  \bigskip

  \alert{What is the best strategy?}
  \begin{itemize}
  \item Simple strategy: 1+9, 1+8, 1+7, 1+6... \only<2->{45 points!}
  \item CS strategy: 5+5, 2+3 and 2+3, ... \only<3->{45 points!}
  \end{itemize}
  }
\end{frame}

\begin{frame}
  \frametitle{Proof: All strategies have the same score (Part I)}

  {\larger
  Let us prove by strong inductions that all strategies for the stack
  game with ``n'' blocks have the same score:
  \begin{equation*}
    C(n) = \frac{n(n-1)}{2}
  \end{equation*}

  \bigskip

  {\bf Base Cases: 0, 1}
  \begin{itemize}
  \item When the stack has 0 blocks, I have no moves, so 0 points.
  \item When the stack has 1 block, I have no moves, so 0 points.
  \end{itemize}
  \begin{equation*}
    C(0) = \frac{0(0-1)}{2}, C(1) = \frac{1(1-1)}{2} = 0
  \end{equation*}
  }
\end{frame}

\begin{frame}
  \frametitle{Proof: All strategies have the same score (Part II)}

    {\bf Inductive Case} $C(n+1)$\\
    By strong induction, we assume that all $C(0)\ldots C(n)$ are true.

    \bigskip

    \begin{enumerate}
    \item A stack with $n+1$ blocks can be split into two: $k$ and $n+1-k$\medskip

    \item The score is: $C(n+1) = k\times(n+1-k) + C(k) + C(n+1-k)$\medskip

    \item Using the strong inductive assumption: $\forall m \leq n, C(m) = \frac{m(m-1)}{2}$\medskip

    \item Transforming (2): $C(n+1) = \frac{2k(n+1-k)}{2} + \frac{k(k-1)}{2} +
      \frac{(n+1-k)(n-k)}{2}$
  \end{enumerate}\bigskip

  ... You can finish the calculation from here ;-)
\end{frame}


\section{Vectors}\label{sec:3Dvectors}

A \dfont{vector}\index{vector}, denoted $\vect{v}$, is a quantity consisting of a
non-negative magnitude and a direction. We could represent a vector in
two dimensions as $(m,\theta)$, where $m$ is the magnitude and
$\theta$ is the direction, measured as an angle from some agreed upon
direction. For example, we might think of the vector $(5,45^\circ)$
as representing ``5 km toward the northeast''; that is,
this vector might be a \dfont{displacement vector},
indicating, say, that your grandfather walked 5 kilometers toward the
northeast to school in the snow. On the other hand, the same vector
could represent a velocity, indicating that your grandfather walked at
5 km/hr toward the northeast. What the vector does not indicate is
where this walk occurred: a vector represents a magnitude and a
direction, but not a location. Pictorially it is useful to represent a
vector as an arrow; the direction of the vector, naturally, is the
direction in which the arrow points; the magnitude of the vector is
reflected in the length of the arrow.

It turns out that many, many quantities behave as vectors, e.g.,
displacement, velocity, acceleration, force. Already we can get some
idea of their usefulness using displacement vectors. Suppose that your
grandfather walked 5 km NE and then 2 km SSE; if the terrain allows,
and perhaps armed with a compass, how could your grandfather have
walked directly to his destination? We can use vectors (and a bit of
geometry) to answer this question. We begin by noting that since
vectors do not include a specification of position, we can ``place''
them anywhere that is convenient. So we can picture your grandfather's
journey as two displacement vectors drawn head to tail:

\begin{figure}[H]
$$\vbox{\beginpicture
\normalgraphs
\setcoordinatesystem units <7truemm,7truemm>
\setplotarea x from 0 to 5, y from 0 to 3.6
\arrow <4pt> [0.35, 1] from 0 0 to 3.54 3.54
\arrow <4pt> [0.35, 1] from 3.54 3.54 to 4.3 1.69
\setdashes
\arrow <4pt> [0.35, 1] from 0 0 to 4.3 1.69
\endpicture}$$
\end{figure}

The displacement vector for the shortcut route is the vector drawn
with a dashed line, from the tail of the first to the head of the
second. With a little trigonometry, we can compute that the third
vector has magnitude approximately 4.62 and direction $21.43^\circ$,
so walking 4.62 km in the direction $21.43^\circ$
north of east (approximately ENE) would get your grandfather to
school. This sort of calculation is so common, we dignify it with a
name: we say that the third vector is the \dfont{sum}\index{vector!sum} of the other two vectors.

There is
another common way to picture the sum of two vectors. Put the vectors
tail to tail and then complete the parallelogram they indicate; the
sum of the two vectors is the diagonal of the parallelogram:

\begin{figure}[H]
$$\vbox{\beginpicture
\normalgraphs
\setcoordinatesystem units <7truemm,7truemm>
\setplotarea x from 0 to 5, y from -2 to 3.6
\arrow <4pt> [0.35, 1] from 0 0 to 3.54 3.54
\arrow <4pt> [0.35, 1] from 0 0 to 0.77 -1.85
\setdashes
\arrow <4pt> [0.35, 1] from 0 0 to 4.3 1.69
\setdots
\plot 3.54 3.54 4.3 1.69 0.77 -1.85 /
\endpicture}$$
\end{figure}

This is a more natural representation in some circumstances. For
example, if the two original vectors represent forces acting on an
object, the sum of the two vectors is the net or effective force on
the object, and it is convenient to draw all three with their tails at the
location of the object.

We also define \dfont{scalar multiplication}\index{vector!scalar multiplication} for
vectors: if $\vect{v}$ is a vector $(m,\theta)$ and $a\ge 0$ is a real
number, the vector $a\vect{v}$ is $(am,\theta)$, namely, it points in
the same direction but has $a$ times the magnitude. If $a<0$, $a\vect{v}$ is $(|a|m,\theta+\pi)$, with $|a|$ times the magnitude and
pointing in the opposite direction (unless we specify otherwise,
angles are measured in radians).

Now we can understand subtraction of vectors:
$\vect{v}-\vect{w}=\vect{v}+(-1)\vect{w}$:

\begin{figure}[H]
$$\vbox{\beginpicture
\normalgraphs
\setcoordinatesystem units <7truemm,7truemm>
\setplotarea x from 0 to 5, y from 0 to 3.6
\arrow <4pt> [0.35, 1] from 0 0 to 3.54 3.54
\arrow <4pt> [0.35, 1] from 0 0 to 4.3 1.69
\arrow <4pt> [0.35, 1] from 4.3 1.69 to 3.54 3.54
\put {$\vect{v}$} [br] <-2pt,2pt> at 1.72 1.72
\put {$\vect{w}$} [tl] <2pt,-2pt> at 2.15 0.84
\put {$\vect{v}-\vect{w}$} [bl] <2pt,2pt> at 3.92 2.615
\setcoordinatesystem units <7truemm,7truemm> point at -6 0
\setplotarea x from 0 to 5, y from 0 to 3.6
\arrow <4pt> [0.35, 1] from 0 0 to 3.54 3.54
\arrow <4pt> [0.35, 1] from 4.3 1.69 to 0 0
\arrow <4pt> [0.35, 1] from 4.3 1.69 to 3.54 3.54
\put {$\vect{v}$} [br] <-2pt,2pt> at 1.72 1.72
\put {$-\vect{w}$} [tl] <2pt,-2pt> at 2.15 0.84
\put {$\vect{v}-\vect{w}$} [bl] <2pt,2pt> at 3.92 2.615
\endpicture}$$
\end{figure}

Note that as you would expect, $\vect{w} + (\vect{v}-\vect{w}) = \vect{v}$.

We can represent a vector in ways other than $(m,\theta)$, and in fact
$(m,\theta)$ is not generally used at all. How else could we describe
a particular vector? Consider again the vector $(5,45^\circ)$. Let's
draw it again, but impose a coordinate system. If we put the tail of
the arrow at the origin, the head of the arrow ends up at
the point $(5/\sqrt2,5/\sqrt2)\approx(3.54, 3.54)$.

\begin{figure}[H]
$$\vbox{\beginpicture
\normalgraphs
\setcoordinatesystem units <7truemm,7truemm>
\setplotarea x from 0 to 5, y from 0 to 4
\axis left /
\axis bottom /
\arrow <4pt> [0.35, 1] from 0 0 to 3.54 3.54
\put {$(3.54, 3.54)$} [l] <3pt,0pt> at 3.54 3.54
\circulararc 45 degrees from 1 0 center at 0 0
\put {$5$} [br] <-2pt,2pt> at 1.72 1.72
\put {$45^\circ$} [bl] <2pt,2pt> at 1 0.3
\endpicture}$$
\end{figure}

In this picture the coordinates $(3.54,3.54)$ identify the head of the
arrow, provided we know that the tail of the arrow has been placed at
$(0,0)$. Then in fact the vector can always be identified as
$(3.54,3.54)$, no matter where it is placed; we just have to remember
that the numbers 3.54 must be interpreted as a \emph{change} from the
position of the tail, not as the actual coordinates of the arrow head;
to emphasize this we will write $\langle 3.54,3.54\rangle$ to mean the
vector and $(3.54,3.54)$ to mean the point. Then if the vector
$\langle 3.54,3.54\rangle$ is drawn with its tail at $(1,2)$ it looks
like this:

\begin{figure}[H]
$$\vbox{\beginpicture
\normalgraphs
\setcoordinatesystem units <7truemm,7truemm>
\setplotarea x from 0 to 5, y from 0 to 6
\axis left /
\axis bottom /
\arrow <4pt> [0.35, 1] from 1 2 to 4.54 5.54
\setdashes
\plot 1 2 4.54 2 4.54 5.54 /
\put {$3.54$} [t] <0pt,-3pt> at 2.72 2
\put {$3.54$} [l] <3pt,0pt> at 4.54 3.72
\put {$(4.54,5.54)$} [l] <3pt,0pt> at 4.54 5.54
\endpicture}$$
\end{figure}

Consider again the two part trip: 5 km NE and then 2 km SSE. The
vector representing the first part of the trip is $\ds \langle
5/\sqrt2,5/\sqrt2\rangle$, and the second part of the trip is
represented by $\langle 2\cos(-3\pi/8),2\sin(-3\pi/8)\rangle
\approx\langle 0.77,-1.85 \rangle$.  We can represent the sum of these
with the usual head to tail picture:

\begin{figure}[H]
$$\vbox{\beginpicture
\normalgraphs
\setcoordinatesystem units <9truemm,9truemm>
\setplotarea x from 0 to 5, y from 0 to 5
\axis bottom /
\axis left /
\arrow <4pt> [0.35, 1] from 0 0 to 3.54 3.54
\arrow <4pt> [0.35, 1] from 3.54 3.54 to 4.3 1.69
\setdashes
\arrow <4pt> [0.35, 1] from 0 0 to 4.3 1.69
\put {$3.54$} [b] <0pt,3pt> at 1.7 3.54
\put {$0.77$} [b] <0pt,3pt> at 3.9 3.54
\put {$-1.85$} [l] <3pt,0pt> at 4.3 2.69
\put {$(4.3,1.69)$} [l] <3pt,0pt> at 4.3 1.69
\plot 0 3.54 4.3 3.54 4.3 1.69 /
\endpicture}$$
\end{figure}

It is clear from the picture that the coordinates of the destination
point are $\ds (5/\sqrt2+2\cos(-3\pi/8),5/\sqrt2+2\sin(-3\pi/8))$ or
approximately $(4.3,1.69)$, so the sum of the two vectors is $\ds
\langle 5/\sqrt2+2\cos(-3\pi/8),5/\sqrt2+2\sin(-3\pi/8)\rangle \approx
\langle 4.3,1.69\rangle$. Adding the two vectors is easier in this
form than in the $(m,\theta)$ form, provided that we're willing to
have the answer in this form as well.

It is easy to see that scalar multiplication and vector subtraction
are also easy to compute in this form: $a\langle v,w\rangle=\langle
av,aw\rangle$ and $\ds \langle v_1,w_1\rangle - \langle v_2,w_2\rangle
=\langle v_1-v_2,w_1-w_2\rangle$. What about the magnitude? The
magnitude of the vector $\langle v,w\rangle$ is still the length of
the corresponding arrow representation; this is the distance from the
origin to the point $(v,w)$, namely, the distance from the tail to the
head of the arrow. Using the familiar distance formula the magnitude of
the vector is simply $\ds \sqrt{v^2+w^2}$, which we also denote with
absolute value bars: $\ds |\langle v,w\rangle|=\sqrt{v^2+w^2}$.

In three dimensions, vectors are still quantities consisting of a
magnitude and a direction, but of course there are many more possible
directions. It's not clear how we might represent the direction
explicitly, but the coordinate version of vectors makes just as much
sense in three dimensions as in two. By $\langle 1,2,3\rangle$ we mean
the vector whose head is at $(1,2,3)$ if its tail is at the origin. As
before, we can place the vector anywhere we want; if it has its tail
at $(4,5,6)$ then its head is at $(5,7,9)$. It remains true that
arithmetic is easy to do with vectors in this form.

\begin{formulabox}[Arithmetic of Vectors]
\textbf{Sum} of vectors:
\[
 \langle v_1,v_2,v_3\rangle + \langle w_1,w_2,w_3\rangle
  =\langle v_1+w_1,v_2+w_2,v_3+w_3\rangle
\]

\textbf{Scalar Multiplication} of vectors:
\[
 a\langle v_1,v_2,v_3\rangle=\langle av_1,av_2,av_3\rangle
\]

\textbf{Subtraction} of vectors:
\[
  \langle v_1,v_2,v_3\rangle - \langle w_1,w_2,w_3\rangle
  =\langle v_1-w_1,v_2-w_2,v_3-w_3\rangle
\]

The \textbf{magnitude} of the vector is the distance from the origin to
the head of the vector, or
$\ds |\langle v_1,v_2,v_3\rangle|=\sqrt{v_1^2+v_2^2+v_3^2}$.
\end{formulabox}

\begin{figure}[H]
\centerline{
\vbox{\beginpicture
\normalgraphs
\setcoordinatesystem units <6truemm,6truemm>
\setplotarea x from 0 to 5, y from 0 to 6
\axis left /
\axis bottom /
\plot 0 0 -3 -3 /
\arrow <4pt> [0.35, 1] from 0 0 to 3 4
\setdashes
\plot 4 0 3 -1 /
\plot 3 -1 -1 -1 /
\plot 3 -1 3 4 /
\setdots
\plot 0 5 4 5 3 4 -1 4 0 5 /
\plot -1 4 -1 -1 /
\plot 4 5 4 0 /
\put {$\bullet$} at 3 4
%\put {$(2,4,5)$} [tr] <-4pt,-4pt> at 3 4
\put {$y$} [l] <4pt,0pt> at 5 0
\put {$z$} [b] <0pt,4pt> at 0 6
\put {$x$} [tr] <-4pt,-4pt> at -3 -3
\put {$4$} [b] <0pt,3pt> at 4 0
\put {$2$} [br] <-2pt,2pt> at -1 -1
\put {$5$} [r] <-3pt,0pt> at 0 5
\endpicture}}
\caption{The vector $\langle 2,4,5\rangle$ with its tail at the origin. \label{fig:3d vector}}
\end{figure}

Three particularly simple vectors turn out to be quite useful:
$\vect{i}=\langle1,0,0\rangle$, $\vect{j}=\langle0,1,0\rangle$, and
$\vect{k}=\langle0,0,1\rangle$. These play much the same role for
vectors that the axes play for points. In particular, notice that
\begin{align*}
  \langle v_1,v_2,v_3\rangle &= \langle v_1,0,0\rangle + \langle
  0,v_2,0\rangle + \langle 0,0,v_3\rangle	\\
  &=v_1\langle1,0,0\rangle + v_2\langle0,1,0\rangle + v_3\langle0,0,1\rangle	\\
  &= v_1\vect{i} + v_2\vect{j} + v_3\vect{k}
\end{align*}

Thus far, we have focused our discussion on vectors which begin at the origin and end at a point. However we will frequently want to produce a vector that points from one point
to another. That is, if $P$ and $Q$ are points, we seek the vector
$\vect{x}$ such that when the tail of $\vect{x}$ is placed at $P$, its head
is at $Q$; we refer to this vector as
$\ds \longvect{\strut PQ}$.
If we know the coordinates of $P$ and $Q$,
the coordinates of the vector are easy to find.

\begin{example}{}{}
Suppose $P=(1,-2,4)$ and $Q=(-2,1,3)$. The vector
$\ds \longvect{\strut PQ}$
is
$\langle -2-1,1--2,3-4\rangle=\langle -3,3,-1\rangle$ and
$\ds \longvect{\strut QP}=\langle 3,-3,1\rangle$.
\end{example}

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

\begin{enumialphparenastyle}

\begin{ex}
Draw the vector $\langle 3,-1\rangle$ with its tail at the
origin.
\end{ex}

\begin{ex}
Draw the vector $\langle 3,-1,2\rangle$ with its tail at the
origin.
\end{ex}

\begin{ex}
Let $\vect{v}$ be the vector with tail at the origin and head
at $(1,2)$; let $\vect{w}$ be the vector with tail at the origin and head
at $(3,1)$. Draw $\vect{v}$ and $\vect{w}$ and a vector $\vect{u}$ with
tail at $(1,2)$ and head at $(3,1)$. Draw $\vect{u}$ with its tail at the origin.
\end{ex}

\begin{ex}
Let $\vect{v}$ be the vector with tail at the origin and head
at $(-1,2)$; let $\vect{ w}$ be the vector with tail at the origin and head
at $(3,3)$. Draw $\vect{v}$ and $\vect{w}$ and a vector $\vect{u}$ with
tail at $(-1,2)$ and head at $(3,3)$. Draw $\vect{u}$ with its tail at the origin.
\end{ex}

\begin{ex}
Let $\vect{v}$ be the vector with tail at the origin and head
at $(5,2)$; let $\vect{w}$ be the vector with tail at the origin and head
at $(1,5)$. Draw $\vect{v}$ and $\vect{w}$ and a vector $\vect{u}$ with
tail at $(5,2)$ and head at $(1,5)$. Draw $\vect{u}$ with its tail at the origin.
\end{ex}

\begin{ex}
Find $|\vect{v}|$, $\vect{v}+\vect{w}$, $\vect{v}-\vect{w}$,
$|\vect{v}+\vect{w}|$, $|\vect{v}-\vect{w}|$ and $-2\vect{v}$ for
$\vect{v} = \langle 1,3\rangle$ and $\vect{w} = \langle -1,-5\rangle$.
\begin{sol}
$\ds \sqrt{10}$, $\langle 0,-2\rangle$, $\langle 2,8\rangle$
2, $\ds 2\sqrt{17}$, $\langle -2,-6\rangle$
\end{sol}
\end{ex}

\begin{ex}
Find $|\vect{v}|$, $\vect{v}+\vect{w}$, $\vect{v}-\vect{w}$,
$|\vect{v}+\vect{w}|$, $|\vect{v}-\vect{w}|$ and $-2\vect{v}$ for
$\vect{v} = \langle 1,2,3\rangle$ and $\vect{w} = \langle -1,2,-3\rangle$.
\begin{sol}
$\ds \sqrt{14}$, $\langle 0,4,0\rangle$, $\langle 2,0,6\rangle$
4, $\ds 2\sqrt{10}$, $\langle -2,-4,-6\rangle$
\end{sol}
\end{ex}

\begin{ex}
Find $|\vect{v}|$, $\vect{v}+\vect{w}$, $\vect{v}-\vect{w}$,
$|\vect{v}+\vect{w}|$, $|\vect{v}-\vect{w}|$ and $-2\vect{v}$ for
$\vect{v} = \langle 1,0,1\rangle$ and $\vect{w} = \langle -1,-2,2 \rangle$.
\begin{sol}
$\ds \sqrt{2}$, $\langle 0,-2,3\rangle$, $\langle 2,2,-1\rangle$
$\ds \sqrt{13}$, $3$, $\langle -2, 0, -2\rangle$
\end{sol}
\end{ex}

\begin{ex}
Find $|\vect{v}|$, $\vect{v}+\vect{w}$, $\vect{v}-\vect{w}$,
$|\vect{v}+\vect{w}|$, $|\vect{v}-\vect{w}|$ and $-2\vect{v}$ for
$\vect{v} = \langle 1,-1,1\rangle$ and $\vect{w} = \langle 0,0,3\rangle$.
\begin{sol}
$\ds \sqrt{3}$, $\langle 1,-1,4\rangle$, $\langle 1,-1,-2\rangle$
$\ds 3\sqrt{2}$, $\ds \sqrt{6}$, $\langle -2, 2, -2\rangle$
\end{sol}
\end{ex}

\begin{ex}
Find $|\vect{v}|$, $\vect{v}+\vect{w}$, $\vect{v}-\vect{w}$,
$|\vect{v}+\vect{w}|$, $|\vect{v}-\vect{w}|$ and $-2\vect{v}$ for
$\vect{v} = \langle 3,2,1\rangle$ and $\vect{w} = \langle -1,-1,-1\rangle$.
\begin{sol}
$\ds \sqrt{14}$, $\langle 2,1,0\rangle$, $\langle 4,3,2\rangle$
$\ds \sqrt{5}$, $\ds \sqrt{29}$, $\langle -6,-4, -2\rangle$
\end{sol}
\end{ex}

\begin{ex}
Let $P=(4,5,6)$, $Q=(1,2,-5)$. Find
$\ds \longvect{\strut PQ}$. Find a vector with
the same direction as $\ds \longvect{\strut PQ}$
but with length 1. Find a vector with
the same direction as $\ds \longvect{\strut PQ}$
but with length 4.
\begin{sol}
$\langle -3, -3, -11\rangle$,
$\langle -3/\sqrt{139},-3/\sqrt{139},-11/\sqrt{139}\rangle$
$\langle -12/\sqrt{139},-12/\sqrt{139},-44/\sqrt{139}\rangle$
\end{sol}
\end{ex}

\begin{ex}
If $A, B$, and $C$ are three points, find
$\ds \longvect{\strut AB}+
\longvect{\strut BC}+
\longvect{\strut CA}$.
\begin{sol}
$\langle 0,0,0\rangle$
\end{sol}
\end{ex}

\begin{ex}
Consider the 12 vectors that have their tails at the center of a
clock and their respective heads at each of the 12 digits.  What is
the sum of these vectors?  What if we remove the vector corresponding
to 4 o'clock?  What if, instead, all vectors have their
tails at 12 o'clock, and their heads on the remaining digits?
\begin{sol}
$\vect{0}$; $\langle -r\sqrt3/2,r/2\rangle$; $\langle
0,-12r\rangle$; where $r$ is the radius of the clock
\end{sol}
\end{ex}

\begin{ex}
Let $\vect{a}$ and $\vect{b}$ be nonzero vectors in two dimensions
that are not parallel or anti-parallel.  Show, algebraically, that if
$\vect{c}$ is any two dimensional vector, there are scalars $s$ and $t$
such that $\vect{c}=s\vect{a}+t\vect{b}$.
\end{ex}

\begin{ex}
Does the statement in the previous exercise hold if the vectors
$\vect{a}$, $\vect{b}$, and $\vect{c}$ are three dimensional vectors? Explain.
\end{ex}

\end{enumialphparenastyle}


\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{float}
\usepackage{amsmath}

\usepackage[hmargin=3cm,vmargin=6.0cm]{geometry}
%\topmargin=0cm
\topmargin=-2cm
\addtolength{\textheight}{6.5cm}
\addtolength{\textwidth}{2.0cm}
%\setlength{\leftmargin}{-5cm}
\setlength{\oddsidemargin}{0.0cm}
\setlength{\evensidemargin}{0.0cm}

\begin{document}

\section*{Student Information }
%Write your full name and id number between the colon and newline
%Put one empty space character after colon and before newline
Full Name : Zeynep Özalp \\
Id Number : 2237691 \\

% Write your answers below the section tags
\section*{Answer 1}
\subsection*{1.1}
$$a_n=a_{n-1}+n^2$$
$$a_n=(a_{n-2}+(n-1)^2)+n^2$$
$$a_n=a_{n-3}+(n-2)^2+(n-1)^2+n^2$$
$$\vdots$$
$$a_n=a_1+\sum_{i=2}^n i^2=a_1+(\sum_{i=1}^n i^2)-1=\sum_{i=1}^n i^2$$
$$a_n=\frac{n(n+1)(2n+1)}{6}=\frac{2n^3+3n^2+n}{6}$$

\subsection*{1.2}
$$a_n=2a_{n-1}+2^n$$
$$a_n=2^2a_{n-2}+2(2^n)$$
$$a_n=2^3a_{n-3}+3(2^n)$$
$$\vdots$$
$$a_n=2^na_0+n2^n=2^n(n+1)$$
\section*{Answer 2}
First, put $n=1$, $f(n)=21$ and $g(n)=21$. So $f(n)\leq g(n)$ is true for $n=1$. Now assume that $f(n)\leq g(n)$ is true for $n=k$ where k is an arbitary positive integer. So, for $n=k$,
$$f(n)\leq g(n)$$
$$k^2+15k+5 \leq 21k^2$$
With this assumption, we must show that the rule is true for its successor, n = (k + 1). For $n=k+1$, we have
$$f(k+1)=k^2+17k+21=f(k)+2k+16$$
$$g(k+1)=21k^2+42k+21=g(k)+42k+21$$
Since $f(k)\leq g(k)$ from our assumption and $2k+16\leq 42k+21$ for all positive integers,
$$f(k+1)\leq g(k+1)$$
So, we have shown that if the rule is true for any specific natural number k, then it is also true for its successor, k + 1. With this information, since the rule is true for $n=1$, $f(n) \leq g(n)$ is true for all n where n is a positive integers.
\section*{Answer 3}
\subsection*{3.1}
\subsubsection*{a)}
Define a function $\theta:N^+\rightarrow \Sigma^*$ for simplicity.
$$\theta (1)=(.p_1.)$$
where . operator means the concenation of strings. Recursive steps for theta
$$\theta (2)=(.p_2.\wedge .\theta(1).)$$
$$\theta (3)=(.p_3.\wedge .\theta(2).)$$
$$\vdots$$
$$\theta (i)=(.p_i.\wedge .\theta(i-1).)$$
Then $\phi (i)$ is the concatenation of $\theta (i)$ and the string "$\rightarrow q$".
$$\phi (i)=\theta (i).\rightarrow .q$$
\\
Base step is
$$\psi (1) = (.p_1.\rightarrow . q.)$$
Recursive steps for psi
$$\psi (2) = (.p_2.\rightarrow .\psi (1).)$$
$$\psi (3) = (.p_3.\rightarrow .\psi (2).)$$
$$\vdots$$
$$\psi (i) = (.p_i.\rightarrow .\psi (i-1).)$$
\subsubsection*{b)}
\textbf{Base:}
$$\phi (1)\vdash \psi (1)$$
$$p_1\rightarrow q \vdash p_1\rightarrow q$$
\begin{table}[H]
	\centering
	\begin{tabular}{lllllll}
		1. & & & $p_1\rightarrow q$ & premise & & \\
		2. & & & $p_1\rightarrow q$ & copy 1 & & \\
	\end{tabular}
\end{table}
So, $\phi (1)\vdash \phi (1)$ for n=1.\\
\textbf{Inductive Hypothesis:}
Assume that $$\phi (k)\vdash \psi (k)$$
\textbf{Inductive Step:} We need to prove
$$\phi (k+1)\vdash \psi (k+1)$$
$$p_{k+1}\wedge \phi (k)\vdash p_{k+1}\rightarrow \psi (k)$$
\begin{table}[H]
	\centering
	\begin{tabular}{lllllll}
		1. & & & $p_{k+1}\wedge \phi (k)$ & premise & & \\ \cline{3-7}
		2. & \multicolumn{1}{c|}{} & & $p_{k+1}$ & assumed & & \multicolumn{1}{c|}{}\\
		3. & \multicolumn{1}{c|}{} & & $\phi (k)$ & $\wedge e,1$ & & \multicolumn{1}{c|}{}\\
		4. & \multicolumn{1}{c|}{} & & $\psi (k)$ & inductive hypothesis, 3& & \multicolumn{1}{c|}{}\\ \cline{3-7}
		5. &  & & $p_{k+1}\rightarrow \psi (k)$ & $\rightarrow i, 2-4$ & & \\
	\end{tabular}
\end{table}

\subsection*{3.2}
\subsubsection*{a)}
Let $H(x)$ be a function which returns the height of the binary tree $x$. Then, base case is $H(emptyTree)=-1$. Then, we can define $H(x)$ recursively as
$$H(x)=1+max(H(leftChild), H(rightChild))$$
\subsubsection*{b)}
\textbf{Definition:} A binary tree is a 223-tree if for each vertex it holds that the number of vetices in the left subtree and the number of vertices in the right subtree differ by at most 2.\\

We can find the number of nodes in a 223-tree by summing the number of vertices in the left subtree, in the right subtree and 1 for the root. \\

\textbf{$f:N\rightarrow N$}\\
To find the maximum number, the height of left and right subtree should be equal which is the (h-1) to get the height=h in total.
$$f(0)=1$$
$$f(h)=1+f(h-1)+f(h-1)\ for\ h>0$$

\textbf{$g:N\rightarrow N$}\\
One of left or right subtree should have height (h-1) and to find the minimum, other must have the minimum height whic is (h-3).
$$g(0)=1,\ g(1)=2,\ g(2)=3$$
$$g(h)=1+g(h-1)+g(h-3)\ for\ h>2$$

\subsubsection*{c) f}
\textbf{Basis Step:} Assume T is a 223-tree of h=1. Then the maximum number of vertices in T must be 3 with one root, one left vertex and one right vertex.
$$f(1)=1+f(0)+f(0)=1+1+1=3$$
\textbf{Inductive hypothesis:} Assume T's left subtree is $T_1$ and right subtree is $T_2$ with the same heights, h-1, to get the maximum number of vertices. Assume that the number of vertices in left and right subtree is
$$f(h-1)=1+f(h-2)+f(h-2)$$
\textbf{Inductive Step:} The maximum number of vertices occurs when vertices except the most bottom ones, which have the height 0, have exactly 2 child. A full binary tree of height h has maximum $2^{h+1}-1$ number of vertices as stated in theorem 2 on page 356 in our textbook. Show that $f(h)=2^{h+1}-1$.
$$f(h)=1+2f(h-1)$$
$$f(h)=1+2(1+2f(h-2))$$
$$f(h)=1+2+2^2(1+2f(h-3))$$
$$f(h)=1+2+4+2^3(1+2f(h-4))$$
$$\vdots$$
$$f(h)=\sum^{h-1}_{i=0}2^{i}+2^hf(0)$$
$$f(h)=2^h-1+2^h=2^{h+1}-1$$
\subsubsection*{c) g}
\textbf{Basis Step:} Assume T is a 223-tree of h=3. Then the minimum number of vertices in T must be 5 with one root, one left vertex and one right subtree with one root and one right subtree with one root and one right vertex.
$$g(3)=1+g(2)+g(0)=1+3+1=5$$
\textbf{Inductive hypothesis:} Assume T's left subtree is $T_1$ and right subtree is $T_2$ with the heights h-1 and h-3, respectively. Assume that the numbers of vertices in left and right subtree are
$$g(h-1)=1+g(h-2)+g(h-4)$$
$$g(h-3)=1+g(h-4)+g(h-6)$$
Therefore,
$$g(h-1)+g(h-3)=1+g(h-2)+g(h-4)+1+g(h-4)+g(h-6)$$
$$g(h-1)+g(h-3)=g(h-2)+2+2(h-4)+g(h-6)$$
\textbf{Inductive Step:}
\section*{Answer 4}
\subsection*{4.1}
\subsubsection*{a)}
The initial values of a,b are 0 and 1 is added to b each time the nested loop is traversed with a sequence of integers i, j, k such that $1\leq k\leq j\leq i\leq n$. The number of such sequences of integers is the number of ways to choose 3 integers from \{1,2,3,...,n\} with repetition allowed. Therefore, by theorem 2 on page 425 in our textbook, b=C(n+3-1,3)=C(n+2,3) and a=2C(n+2-1,2)=2C(n+1,2) by the same approach (since 2 is added to a in each time, combination is multiplied by 2).
\subsubsection*{b)}
If a=b then C(n+2,3)=2.C(n+1,2).
$$\dfrac{(n+2)!}{3!(n-1)!}=\dfrac{2(n+1)!}{2!(n-1)!}$$
$$\dfrac{(n+2)}{6}=1$$
$$n=4$$

\subsection*{4.2}
\subsubsection*{a)}
For first plate, we need to choose 2 fruits from 10 fruits. For second plate, we need to choose 2 fruits from 8 fruits. For third plate, we need to choose 2 fruits from 6 fruits.
$$C(10,2).C(8,2).C(6,2)=45.28.15=18900$$
\subsubsection*{b)}
For first plate, we need to choose 1 fruits from 10 fruits. For second plate, we need to choose 2 fruits from 9 fruits. For third plate, we need to choose 3 fruits from 7 fruits. For fourth plate, we need to chose 4 fruits from 4 fruits.
$$C(10,1).C(9,2).C(7,3).C(4,4)=10.36.35.1=12600$$
\subsubsection*{c)}
To compute this, I will use the sum of \textbf{Stirling numbers of second kind} as it is used in the example 10 on page 430 in our textbook.
$$\sum^4_{j=1}S(6,j)=1+31+90+65=187$$
\subsubsection*{d)}
We need to separate fruits into four groups. So, we need three separation operators. Let us denote the separator as "-" and fruits as "d". So, we have 6 fruits and 3 separators.
$$dddddd---$$
For instance, two distributions can be like,
$$dd-d-dd-d$$
$$dd--ddd-d$$
where the second plate is empty.
So, we can find all possible distributions of 6 fruits in 4 plates by putting 6 fruits and 3 separators in order. We can put them order in 9! way but there are repetitions, so we have divide it by the factorials of the number of repeating elements. If we do not have to use all the fruits, there are 7 cases:\\
1. 6 fruits are used.
$$\dfrac{9!}{3!6!}=84$$
2. 5 fruits are used.
$$\dfrac{8!}{3!5!}=56$$
3. 4 fruits are used.
$$\dfrac{7!}{3!4!}=35$$
4. 3 fruits are used.
$$\dfrac{6!}{3!3!}=20$$
5. 2 fruits are used.
$$\dfrac{5!}{3!2!}=10$$
6. 1 fruit is used.
$$\dfrac{4!}{3!1!}=4$$
2. 0 fruit is used.
$$\dfrac{3!}{3!0!}=1$$
Sum all the independent cases: 84+56+35+20+10+4+1=210 ways.

\end{document}