\section{Elementary Number Theory}
Let $n$ be an natural number.
The multiples of $n$ are all integers $kn$ where $k\in\mathbb Z$.
For example, $2n,5n,n,-4n,0$ are all multiplies of $n$.
\begin{definition}
    If $m$ is a multiple of $n$, we say $n$ divides $m$, or $n$ is a divisor of $m$, written as $n|m$.
\end{definition}
\begin{definition}
    We say a natural number $n\ge 2$ is a prime if it has no divisors apart from $1$ and $n$.\\
    Otherwise, we call it to be composite.
\end{definition}
\begin{example}
    $2,3,5,7,11,13,17,\ldots$ are prime.\\
    $10,25,34,44,57$ are composite.
\end{example}
Our aim now is to break up a number into primes, for example $63=3\time 3\time 7$, and hopefully it would be unique.
\begin{proposition}
    Every natural number $n\ge 2$ is expressible as a product of primes.
\end{proposition}
\begin{proof}
    Strong induction.\\
    The statement is obvious true for $n=2$.
    Given an $n>2$, if it is prime, then it is done.
    Otherwise, we can write $n=ab$ where $n>a,b>1$.
    By induction hypothesis, we can write $a,b$ as a product of primes.
    $$a=p_1p_2\cdots p_k, b=q_1q_2\cdots q_l$$
    where $p_i,q_i$ are primes.
    So $n=p_1p_2\cdots p_kq_1q_2\cdots q_l$, therefore the statement is true for $n$.
    And it's done.
\end{proof}
\begin{remark}
    1. Although we start at $2$, sometimes you can regard $1$ as a product of no primes.\\
    2. There is no nice pattern of primes.
    There is no algebraic formula for the $n^{th}$ prime.
\end{remark}
\begin{theorem}
    There are infinitely many primes.
\end{theorem}
\begin{proof}
    Suppose for the same of contradiction that there is only finitely many, let
    $$p_1,p_2,\ldots, p_n$$
    be the primes.
    So consider $k=p_1p_2\cdots p_n+1$, it does not have any prime factors as none of the primes divide $k$.
    This is a contradiction to the fact that $k$ has a prime factorisation.
\end{proof}
\begin{theorem}
    The prime factorization of any positive integer $n\ge 2$ is unique.
\end{theorem}
The uniqueness is taken up to re-ordering.
Why is it true?
Why can't $41\times 101=67\times 73$?
We need $p|ab\implies p|a\lor p|b$ where $p$ is a prime number.
We need $p$ to be prime since $6|8\times3$ but $6\nmid 8$ and $6\nmid 3$.
This has to be hard to prove since it is about prime dividing things instead of things dividing primes, as in the definition.
\subsection{Highest Common Factors}
\begin{definition}
    For integers $a,b$ we say the positive integer $c$ is the HCF (Highest Common Factor) of $a,b$ if\\
    1. $c|a$ and $c|b$. ($c$ is a common factor of $a,b$)\\
    2. For any positive integers $d$ such that $d|a$ and $d|b$, then $d|c$. (every common factor of $a,b$ divides c).
\end{definition}
\begin{example}
    The HCF of $18$ and $12$ is $6$.
\end{example}
We want to show that an HCF always exists
\begin{proposition}[Division Algorithm]
    For natural numbers $n,k$, we can write $n=qk+r$, for some integers $q,r$ with $0\le r<k$.
\end{proposition}
\begin{proof}
    Induction on $n$.
    $n=1$ is trivial.\\
    Given $n>1$, we have $n-1=qk+r$ for some integers $q,r$ with $0\le r<k$.\\
    If $r<k-1$, then we have $n=qk+(r+1)$.
    If $r=k-1$, then $n=(q+1)k+0$.
\end{proof}
We can find HCF by Euclid's Algorithm.
\begin{definition}
    The HCF of $a$ and $b$ where $a\ge b$.\\
    Write $q_1b+r_1$ (where $0\le r_1<b$).\\
    Then write $b=q_2r_1+r_2$ (where $0\le r_2<r_1$).\\
    Then write $r_1=q_3r_2+r_3$ (where $0\le r_3<r_2$).\\
    Continue this process until some remainder $r_{n-1}=q_{n+1}r_n+r_{n+1}$ where $r_{n+1}=0$.
    (If necessary, take $a=r_{-1}, b=r_0$)
    That is, some remainder goes to $0$.
\end{definition}
\begin{example}
    $372,162$\\
    $372=2\times 162+48$.\\
    $162=3\times 48+18$.\\
    $48=2\times 18+12$.\\
    $18=1\times 12+6$.\\
    $12=2\times 6+0$.\\
    So the HCF of $372,162$ is $6$.
\end{example}
\begin{theorem}
    The Euclid's Algorithm works.\\
    That is, the HCF exists and can always be found by Euclid's Algorithm.
\end{theorem}
\begin{proof}
    Let $r_n$ be the output.
    Firstly, it is a common factor of $a,b$, since $r_n|r_{n-1},r_n|r_{n-2}|\cdots,r_n|b,r_n|a$ inductively.\\
    Then, for any other common factor $d$ of $a,b$, we know that $d|r_1$, inductively $d|r_i$ for every $i$, then $d|r_n$.\\
    Note that the algorithm always terminate, since the sequence $r_i$ is strictly decreasing.
    In fact, there are at most $b$ steps before it terminates.
\end{proof}
\begin{example}
    $82,57$\\
    $82=57\times 1+25$.\\
    $57=25\times 2+7$.\\
    $25=7\times 3+4$.\\
    $4=3\times 1+1$.\\
    $3=3\times 1+0$.\\
    So the HCF of $82,57$ is $1$.
\end{example}
If two positive integers have HCF $1$, we say that they are coprime.\\
Can we write $1=82x+57y$ for some integers $x,y\in\mathbb Z$?
So we have $1=4-3=4-(7-4)=2\times 4-7=2\times (25-3\times 7)-7=2\times 25-7\times 7=2\times 25-7\times(57-2\times 25)=-7\times 57+16\times 25=-7\times 57+16\times(82-57)=-23\times 57+16\times 82$.
In fact, this algorithm provides a proof that it always works that for any coprime $p,q$ we have $x,y\in\mathbb Z$ with $1=xp+yq$.
\begin{theorem}
    $\forall a,b\in\mathbb N,\exists x,y\in\mathbb Z,\operatorname{HCF}(a,b)=xa+yb$.
\end{theorem}
\begin{proof}
    Run the Euclid's Algorithm on $a,b$ to $r_n$.
    We have $r_n$ written as an integral combination of $r_{n-1},r_{n-2}$.
    Then substitute for $r_{n-1}$ to obtain $r_n$ as an integral combination of $r_{n-2},r_{n-3}$.
    Inductively, we can write $\operatorname{HCF}(a,b)=r_n$ as an integral combination of $a,b$.
\end{proof}
\begin{remark}
    Euclid is telling us that such $x,y$ exists and how to find them in practice.
\end{remark}
There is a second proof to this statement.
\begin{proof}[Alternative Proof]
    Consider the set $\{ax+by:x,y\in\mathbb Z\}$, and let $h$ be the least positive integer in this set.\\
    We claim that $h$ is the HCF of $a$ and $b$.
    If $d|a$ and $d|b$ then $d|ax+yb\implies d|h$.\\
    Now $h$ must be a common factor of $a,b$.
    Suppose that $h\nmid a$, then $a=qh+r$ where $q\in\mathbb Z, 0<r<h$.
    However, $r$ would be an integral combination of $a,b$, but this contradicts the minimality of $h$.\\
    So $h|a$ and similarly $h|b$, so $h$ is an HCF of $a,b$.
\end{proof}
\begin{remark}
    The alternative proof is abstract, nice, and more concise, but it is non-constructive.
    It does not tell us how to find such an integral combination.
\end{remark}
One of the applications of this fact is to solve (linear) Diophantine Equations.\\
Fix $a,b\in\mathbb N$, when can we solve $ax=b,x\in\mathbb Z$?
Obviously we have an solution if and only if $a|b$ and the solution is $b/a$.
But how about $2$ variables?
Suppose $a,b,c\in\mathbb N$, when can we solve $ax+by=c$ in $\mathbb Z$?
We cannot solve $116x+212y=13$ due to parity problems, but we can solve $82x+57y=13$ by multiplying $13$ to the integral combination of $1$ by $82,57$
\begin{corollary}[Bezout Theorem]
    $ax+by=c$ is solvable in $\mathbb Z$ if and only if $\operatorname{HCF}(a,b)|c$.
\end{corollary}
\begin{proof}
    $h$ the HCF of $a,b$.\\
    If there is a solution, we have $c=ax+by$ for some $x,y\in Z$ so $h|ax+by=c$.
    Conversely, if $h|c\implies c=qh,q\in\mathbb N$, since we can write $h=ax+by$ for some $x,y\in Z$, $c=qh=a(qx)+b(qy)$.
\end{proof}
We are now ready to prove
\begin{proposition}
    Let $p$ be prime, $a,b\in\mathbb N$, then $p|ab\implies p|a\lor p|b$.
\end{proposition}
\begin{proof}
    Given that $p|ab$, suppose that $p\nmid a,p\nmid b$.
    So the HCF of $p,a$ is $1$, therefore $\exists x,y\in\mathbb Z, px+ay=1$.
    Note that here we have produced a positive statement here.
    So $pbx+aby=b$, but since $p|ab,p|pbx+aby=b$, which is a contradiction.
\end{proof}
This establishes the statement we have claimed in the preceding section.
\begin{remark}
    Immediately, if $p|a_1a_2\ldots a_n$, then $p|a_i$ for some $i$.
\end{remark}
\subsection{Fundamental Theorem of Arithmetic}
When we have established the theory so far, we are ready to show
\begin{theorem}[Fundamental Theorem of Arithmetic]
    Any positive integer $n\ge 2$ can be written as a product of primes uniquely up to reordering.
\end{theorem}
\begin{proof}
    We already know that any positive integer $n\ge 2$ can be written as such a product, so the rest is to prove its uniqueness up to reordering.\\
    Uniqueness can be proved by (strong) induction on $n$. $n=2$ is obvious.
    Given $n>2$, suppose that
    $$p_1p_2\cdots p_k=n=q_1q_2\cdots q_l$$
    where $p_i,q_i$ are primes, we want to show $k=l$ and after reordering $p_i=q_i,\forall i$.\\
    We have $p_1|q_1q_2\cdots q_l$, so $p_1|q_i$ for some $i$.
    We can reorder such that $q_i\mapsto q_1$.
    Note that due to primity, $p_1=q_1$, thus
    $$p_2p_3\cdots p_k=q_2q_3\cdots q_l$$
    By induction hypothesis, $k-1=l-1\implies k=l$ and we can reorder such that $p_i=q_i, i\ge 2$.
    So the theorem is proved.
\end{proof}
What ideas are involved?
We took the things that cannot be broken up (i.e. primes) and we break everything up into the product of those `unbreakables' (irreducibles).\\
However, this may not be the case.
Despite the fact that we are used to it, unique factorization really isn't obvious.
\begin{example}
    Consider the set $\mathbb Z(\sqrt{-3})=\{a+b\sqrt{-3}:a,b\in\mathbb Z\}$.\\
    We can do addition and multiplication in it in the obvious way, and the set is closed under both.
    So we can define `divides' and `factor of' etc.
    One can show that everything can be broken into irreducibles.
    But $4=2\times 2=(1+\sqrt{-3})(1-\sqrt{-3})$, and $2,1\pm\sqrt{-3}$ are all irreducibles.\\
    Therefore in this funny set, the fundamental theorem of arithmetic doesn't work.
\end{example}
There are a few applications of unique factorization.
First of all, we want to look into factors.
Consider $n=2^3\cdot 3^7\cdot 5\cdot 11$, we can spot factors of the form $2^a\cdot 3^b\cdot 5^c\cdot 11^d, 0\le a\le 3, 0\le b\le 7, 0\le c\le 1, 0\le d\le 1$.
We do not have others because of unique factorization.
In general, the factors of $n=p_1^{a_1}\cdots p_k^{a_k}$ where $p_i$ are distinct primes are of the form $p_1^{b_1}\cdots p_k^{b_k}$ where $0\le b_i\le a_i$.\\
Also, we can find HCF easily.
For example, the common factors of $2^3\cdot 3^2\cdot 5\cdot 11$, $2^2\cdot 3^6\cdot 5\cdot 11$ are of the form $2^a\cdot 3^b\cdot 11^c$ where $0\le a\le 2, 0\le b\le 2, 0\le c\le 1$, so the HCF is $2^2\times 3^2\times 11$.
In general, the HCF of $p_1^{a_1}\cdots p_k^{a_k}$ and $p_1^{b_1}\cdots p_k^{b_k}$ is $p_1^{\min\{a_1,b_1\}}\cdots p_k^{\min\{a_k,b_k\}}$.\\
We can find LCM as well.
The LCM of the numbers in the last example would be $2^3\times 3^6\times 5\times 7\times 11$
In general, the LCM of $p_1^{a_1}\cdots p_k^{a_k}$ and $p_1^{b_1}\cdots p_k^{b_k}$ is $p_1^{\max\{a_1,b_1\}}\cdots p_k^{\max\{a_k,b_k\}}$.\\
Note that $\operatorname{HCF(a,b)}\times\operatorname{LCM(a,b)}=a\times b$.
\subsection{Modular Arithmetic}
\begin{definition}
    Let $n\ge 2$ be a positive integer, then $\mathbb Z_n$ consists of integers with two of them regarded as the same if their difference is a multiple of $n$.
\end{definition}
For example, in $\mathbb Z_7$, $2$ and $16$ are the same.\\
If $a$ and $b$ are the same in $\mathbb Z_n$, we write $a\equiv b\pmod{n}$.\\
In this world, we only care about the remainder when we divide it by $n$.
In $\mathbb Z_n$, $0,1,\ldots ,n-1$ are distinct and every $k\in\mathbb Z_n$ is one of them by division algorithm.
So we can view $\mathbb Z_n$ as an $n$-clock.\\
We can do addition and multiplication in $\mathbb Z_n$.
Note that parity does not make sense in $\mathbb Z_{2n-1}$.
So we would have to check that they are well-defined.
\begin{proposition}
    Suppose $a\equiv a'\pmod n,b\equiv b'\pmod n$, then $a+b\equiv a'+b'\pmod n, ab\equiv a'b'\pmod n$.
\end{proposition}
\begin{proof}
    Trivial.
\end{proof}
All the usual laws of arithmetic applies as $\mathbb Z_n$ inherited them from $\mathbb Z$.\\
Something we have done so far can already be expressed in terms of modular arithmetic.
For example, $ab\equiv 0\pmod{p}\implies a\equiv 0\pmod{p}\lor b\equiv 0\pmod{p}$ if $p$ is a prime number.
Or equivalently, there is no zero divisor in $\mathbb Z_p$.\\
The structure of $\mathbb Z_n$ under addition is boring enough (just a cyclic group), but how about it under multiplication?
\begin{definition}
    In $\mathbb Z_n$, we say $a$ is the inverse of $b$ if $ab\equiv 1\pmod{n}$.
\end{definition}
\begin{example}
    1. In $\mathbb Z_{10}$, $3\times 7\equiv 1\pmod{10}$ so $7$ is the inverse of $3$.\\
    2. (non-example) There is no inverse of $4$ in $\mathbb Z_{10}$ since $4b$ is even for all $b\in\mathbb Z$ so we can never have $4b\equiv 1\pmod{10}$.
\end{example}
If $a$ has an inverse $b$, we write $b=a^{-1}$ given that the $n$ in $\mathbb Z_n$ is understood.
\begin{remark}
    1. If inverse exists, it is unique (in $\mathbb Z_n$).
    Indeed, suppose $ab\equiv ac\equiv 1\pmod{n}$, but then $b\equiv bab\equiv bac\equiv c\pmod{n}$.\\
    2. If $ab\equiv ac\pmod{n}$ and $a$ has an inverse, then $b\equiv c\pmod n$ by multiplying both sides by $a^{-1}$.
    However, if $a$ does not have an inverse, you cannot really cancel it.
    For example $4\times 5\equiv 4\times 0\pmod{10}$, but $5\not\equiv 0\pmod{10}$.
\end{remark}
$\mathbb Z_n$ is quite nice if $n$ is prime.
\begin{proposition}
    $\forall a\not\equiv 0\pmod{p},\exists b\in\mathbb Z_p,ab\equiv 1\pmod{p}$.
\end{proposition}
\begin{proof}
    We know that $(a,p)=1$, so there are some $x,y\in\mathbb Z,ax+py=1\implies ax\equiv 1\pmod{p}$.
    We can take $b=x$.
\end{proof}
\begin{proof}[Alternative proof]
    IN $\mathbb Z_p$, consider $0a,1a,2a,\ldots,(p-1)a$.
    Our task is to show that one of these equals $1$.
    Note that no two of them are equal, since $ia\equiv ja\pmod{p}\implies (i-j)a\equiv 0\pmod{p}\implies i\equiv j\pmod{p}$.\\
    Therefore $\{ka:k\in\{0,1,\ldots,p-1\}\}=\{0,1,\ldots,p-1\}$.
    so there is some $b$ such that $ba\equiv 1\pmod{p}$.
\end{proof}
How about the case in $\mathbb Z_n$ when $n$ is composite?
\begin{proposition}
    $\forall a\in\mathbb Z$, there is some $b$ such that $ab\equiv 1\pmod{n}$ if and only if $(a,n)=1$.
\end{proposition}
\begin{proof}
    If $a$ is invertible, then there is some $b,y\in\mathbb Z$ such that $ab+ny=1$, which means that $(a,n)=1$.\\
    Conversely, if $(a,n)=1$, then there is some $x,y\in\mathbb Z, ax+ny=1\implies ax\equiv 1\pmod{n}$, so we can take $b=x$.
\end{proof}
\begin{definition}
    The Euler $\phi$ function is defined by
    $$\phi(n)=|\{0<a<n:(a,n)=1\}|$$
\end{definition}
Equivalently, $\phi(n)$ is the number of invertible elements in $\mathbb Z_n$.
\begin{example}
    1. $\phi(p)=p-1$ for any prime $p$.\\
    2. $\phi(p^2)=p^2-p=p(p-1)$ for any prime $p$.\\
    3. $\phi(pq)=pq-p-q+1=(p-1)(q-1)=\phi(p)\phi(q)$ for any distinct primes $p,q$.\\
    4. $\phi(p^n)=p^n-p^{n-1}=p^{n-1}(p-1)$ for any prime $p$ and positive integer $n$.
\end{example}
We now introduce the order of an element by a few observations:
In $\mathbb Z_7$, $2^1\equiv 2, 2^2\equiv 4, 2^3\equiv 1$ and things go around again.
In $\mathbb Z_{11}$, $2^1\equiv 2, 2^2\equiv 4, 2^3\equiv 8, 2^4\equiv 5, 2^5\equiv 10, \ldots, 2^{10}\equiv 1\pmod 11$ and things repeat.
\begin{theorem}[Fermat's Little Theorem (F$\ell$T)]
    Let $p$ be a prime, then in $\mathbb Z_p$, every $a\not\equiv 0$ has $a^{p-1}\equiv 1$.
\end{theorem}
\begin{proof}
    Note that $\{ka:k\in\{1,\ldots,p-1\}\}=\{1,\ldots,p-1\}$.\\
    So we have
    $$\prod_{k=1}^{p-1}ak\equiv\prod_{k=1}^{p-1}k\pmod{p}\implies (a^{p-1}-1)(p-1)!\equiv 0\pmod{p}$$
    Now $(p-1)!\neq 0\pmod{p}$ since all the terms in the product is invertible.
    Therefore the theorem.
\end{proof}
For composite $n$, we have a similar proposition.
\begin{theorem}[Fermat-Euler Theorem]
    Let $n\ge 2$ be a positive integer, then in $\mathbb Z_n$, every invertible $a$ has $a^{\phi(n)}\equiv 1$.
\end{theorem}
\begin{proof}
    Note that $\{ka:0<k<n,(k,n)=1\}=\{k:0<k<n,(k,n)=1\}$.
    Since $a$ is invertible, we have $ia\equiv ja\pmod{n}\implies i\equiv j\pmod{n}$\\
    So we have
    \begin{align*}
        \prod_{0<k<n,(k,n)=1}ak\equiv\prod_{0<k<n,(k,n)=1}k\pmod{n}\\
        \implies(a^{\phi(n)}-1)\prod_{0<k<n,(k,n)=1}k\equiv 0\pmod{n}
    \end{align*}
    Now $\prod_{0<k<n,(k,n)=1}k\neq 0\pmod{n}$ since all the terms in the product is invertible.
    Therefore $a^{\phi(n)}\equiv 1\pmod{n}$.
\end{proof}
\begin{lemma}
    Let $p$ be prime, then in $\mathbb Z_p$, the solutions to $x^2\equiv 1\pmod{p}$ are $x=\pm 1$.
\end{lemma}
Note that it is not true when $p$ is not prime.
For example, $1^2\equiv 3^2\equiv 5^2\equiv 7^2\equiv 1$.
\begin{proof}
    $x^2\equiv 1\pmod{p}\iff (x-1)(x+1)\equiv 0\pmod{p}\iff x\equiv\pm 1\pmod{p}$ since $p$ is prime.
\end{proof}
\begin{remark}
    Any nonzero polynomial of degree $d$ in $\mathbb Z_p$ has at most $d$ solutions.
\end{remark}
Note that in the proof of F$\ell$T, there is the expression $(p-1)!$.
It looks like as if it is some interesting thing.
Note that $(3-1)!\equiv -1\pmod{3},(5-1)!\equiv -1\pmod{5},(7-1)!\equiv -1\pmod{7}$.
So it is natural to state the following, which is in fact true:
\begin{theorem}[Wilson's Theorem]
    Let $p$ be a prime, then $(p-1)!\equiv -1\pmod{p}$
\end{theorem}
\begin{proof}
    When $p=2$ it is trivial, so we assume henceforth that $p>2$.\\
    Note that all of $1,2,\ldots ,p-1$ are invertible.
    So we can pair up the elements with their inverses, and they have a product $1$.
    We have the pairs as long as we do not have $x^2\equiv 1\pmod{p}\implies x\equiv 1\lor x\equiv p-1\pmod{p}$.
    Hence $(p-1)!\equiv 1(p-1)\equiv -1\pmod{p}$.
\end{proof}
When is $-1$ a square modulo $p$?
Is there $x\in\mathbb Z$ with $x^2\equiv -1\pmod{p}$?
\begin{example}
    When $p=5$, we notice that $2^2\equiv -1\pmod{5}$
    When $p=7$, by trying, we know that such $x$ does not exist.
    When $p=13$, $5^2=25\equiv -1\pmod{13}$.
    when $p=19$, by trying, we know that such $x$ does not exist.
\end{example}
By looking at the pattern, we can try to prove the following theorem
\begin{theorem}
    For a prime number $p>2$, then $x^2\equiv -1\pmod{p}$ is solvable if and only if $p\equiv 1\pmod{4}$.
\end{theorem}
\begin{proof}
    If $p=4k+3$ for some $k\in\mathbb N$ but $x^2\equiv -1\pmod{p}$ for some $x\in\mathbb Z$.
    But we also have $-1\equiv (x^2)^{2k+1}=x^{4k+2}=x^{p-1}\equiv 1\pmod{p}$, which is a contradiction.\\
    Conversely, if $p=4k+1$, Wilson's theorem tells us that $(4k)!\equiv -1\pmod{p}$.
    Note that $4k-r\equiv -r-1\pmod{p}$, therefore $((2k)!)^2=((2k)!)^2(-1)^{2k}\equiv (4k)!\equiv -1\pmod{p}$.
\end{proof}
Now we go back solving linear congruences.
\begin{example}
    If we want to solve $7x\equiv 4\pmod{30}$.
    Firstly we can find $7\cdot 13\equiv 1\pmod{30}$, we knew we can do this since $(7,30)=1$.
    So $7x\equiv 4\pmod{30}\iff 13\cdot 7x\equiv 13\cdot 4\pmod{30}\iff x\equiv 22\pmod{30}$.
\end{example}
\begin{example}
    Solve $10x\equiv 12\pmod{34}$.
    Note that in this case $(10,34)\neq 1$ so we cannot do the same thing again.
    However, we can throw it back to $\mathbb Z$, so $10x\equiv 12\pmod{34}\iff\exists y\in\mathbb Z, 10x=12+34y\iff\exists y\in\mathbb Z,5x=6+17y\iff 5x\equiv 6\pmod{17}$.
    So from here on we can do the same thing again.
    $5x\equiv 6\pmod{17}\iff x\equiv 7\times 6\equiv 8\pmod{17}$.
\end{example}
Now we want to try simultaneous ones.
\begin{example}
    \[
        \begin{cases}
            x\equiv 3\pmod{17}\\
            x\equiv 5\pmod{19}
        \end{cases}
    \]
    Can we solve it?
    We would expect the answer being yes, since modulo $17,19$ should not ``intervene each other'' since $(17,19)=1$.
    That is not a proof, but that is our intuition.
\end{example}
\begin{example}
    \[
        \begin{cases}
            x\equiv 5\pmod{30}\\
            x\equiv 8\pmod{34}
        \end{cases}
    \]
    It then becomes immediate that this is not solvable due to parity problem.
    What has gone wrong looks like that two modulos are related as $(30,34)=2$.
\end{example}
\begin{theorem}[Chinese Remainder Theorem]
    The system of linear equations
    \[
        \begin{cases}
            x\equiv a\pmod{u}\\
            x\equiv b\pmod{v}
        \end{cases}
    \]
    is solvable if $(u,v)=1$.
    Moreover, the solution is unique modulo $uv$.
\end{theorem}
\begin{proof}
    Existence: Since they are coprime, we have $su+tv=1$ for some $s,t\in\mathbb Z$, then $x=bsu+atv$ solves the system.\\
    Uniqueness: If $x,x'$ are solutions, then $x-x'\equiv 0\pmod{u}$ and $x-x'\pmod{v}$, hence $x-x'\equiv 0\pmod{uv}$.
    Conversely it is obvious that if $x\equiv x'\pmod{uv}$, then if $x$ is a solution so is $x'$.
\end{proof}
The exact same thing would work if you have more than $2$ equations in the system modulo pairwisely coprime numbers.
This can be proved by CRT and induction.
\subsection{Application of number theory}
The RSA code is an example of an application of the Fermat-Euler theorem.
Consider the following scenario: we want to send an encoded message to the receiver who is supposed to have a way to decode it.\\
It seems ``obvious'' that knowing how to decode equals knowing how to encode.
However, there is a way such that even if you know how to encode, it is still ``very hard'' to decode.
That is, the process of finding an inverse function will takes a long period of time for the message to expire.\\
We encode in the following way:
Pick two large primes $p,q$, say a hundred digits each and take their product.
A message is just a sequence of digits, so we can decompose it into not-so-long blocks such that the number represented by each block is less than both of the primes.
Say one of these blocks is $x$, and we take an exponent $e\ge 2$ such that $(e,\phi(pq))=1$, and the encoding message would be the smallest positive integer with $x^e\equiv m\pmod{pq}$
So to decode, we can find some $d$ with $de\equiv 1\pmod{\phi(pq)}$, so $m^d\equiv x\pmod{pq}$ by Fermat-Euler.
This is the way to decode the message, so we just need to find such $d$, which is easy and fast by Euclid given that we do know $\phi(pq)$.\\
Now we only need to know $pq$ and $e$ to encode, and we need to know $d$ (or $e$ and $\phi(pq)=(p-1)(q-1)$, since we can run Euclid) and $pq$ to decode.
Observe here that if the decoder only know all eise information but $\phi(pq)$ explicitly, he will have to factorize $pq$.
Thus, we can publish $pq$ and $e$, then anyone else can send a message to us, however, since only us know $d$, no one else can decode the message quickly since they will have to run a slow algorithm to try factorizing $pq$.


[missed first half]
\section{Cylinder-plane conformal mapping}
Consider the mapping $w\to z$ defined by
\begin{align}
    w &= \ln\frac{z + a}{z - a}
\end{align}

Let $w=u + iv$ and $z = x + iy$.
We want to answer these two questions:
(a) What region of the $z$-plane corresponds to the region bewteen
$u=0$ and $u=u_0$?

(b) what region of the $z$-plane corresponds to the region of the $w$-plane
between $v=0$ and $v=v_0$,
where $v_0$ is a constant that lies between $0$ and $\pi$.

(a)
\begin{align}
    w &= u + iv = \ln \frac{z + a}{z - a}
\end{align}
where $z = x + iy$.
So
\begin{align}
    e^{w} &= \frac{z + a}{z - a}
\end{align}
and the modulus is
\begin{align}
    \left| e^{u} e^{iv} \right|
    &=
    \left|
        \frac{(x + a) + iy}{(x - a) + iy}
    \right|
\end{align}
and so
\begin{align}
    e^{2 u} &=
    \frac{(x + a)^2 + y^2}{(x - a)^2 + y^2}
\end{align}

For $u=0$, $e^{2 u} = 1$.
\begin{align}
    (x + a)^2 + y^2 &=
\end{align}

[missed some lines]

For $u=u_0$,
$e^{2 u_0} =: C^2$,
we have
$C^2 > 1$ for $u_0 > 0$.
Along $v=u_0$,
\begin{align}
    C^2\left[
        (x - a)^2 + y^2
    \right] &=
    (x + a)^2 + y^2
\end{align}
and after some algebra you can find in the notes,
you find that
\begin{align}
    x^2 + y^2 + a^2 *=
    \underbrace{\left(\frac{c^2 + 1}{c^2 - 1}\right)}_{\lambda} 2ax
\end{align}
So call that thing $\lambda$ becuase it's the only place where $C^2$ appears.
\begin{align}
    \lambda &:= \frac{c^2 + 1}{c^2 - 1}
\end{align}
It's possible to rewrite the equation
\begin{align}
    (x - a\lambda)^2 + y^2 = a^2 (\lambda^2 - 1)
\end{align}
and you can convince yourself that $\lambda > 1$.
Because $C^2$ is a positive number bigger than 1,
so $C^2 - 1$ is a positive number,
and $C^2 + 1$ is a bigger positive number,
so $\lambda > 1$.
This equation here is an equation of a circle
centred at $(a\lambda, 0)$.
So the centre of the circle is at $x=a\lambda$.
So it's on the $x$-axis.
The radius of the circle is
$R= a\sqrt{\lambda^2 - 1}$.

Let me continue for a couple more minutes.
Here's what we're finding.

We started with 2 straight lines in the $u-v$ plane.
$u=u_0$ and $u=u_0$.
Those are 2 straight lines.
The $u=0$ line maps to the $y$-axis
and the $u=u_0$ line maps to a circle with centre sitting on the $x$ axis.
The region between $u=0$ and $u=u_0$ is mapped into the region between the
$y$-axis and the circle.

So let's draw that.

[picture here]

This is a fairly standard map.
We don't have enough time in the class to go thorugh all the standard maps.
If you're familiar with all the standard maps you can go very ar.
It's just like how in integration we hvae standar contours.
That's why it's important I did almost all of the standard contours.

Maybe 30 to 40 years ago,
conformal mapping was improtant,
but these days we can solve things using more powerful computers and this is
becoming less important.
30 years ago,
a big chunk of the course would have been dedicated to conformal mapping
just like how I did contour integrals in the course.

\begin{question}
    At the bottom of the $y$-axis is $u=0$?
\end{question}
The entire $y$-axis is mapped from the line $u=0$.
Sorry about my handwriting.
This whole line is $u=0$.

Any other questions?

(b)
Let's do problem b now.
So we start with what we had before
\begin{align}
    e^{u}e^{iv}
    &=
    \frac{(x + a) _+ iy}{(x - a) + iy}
    &=
    \frac{(x^2 + y^2 - a^2) - 2 i ay}{(x - a)^2 + y^2}
\end{align}
and then skipping a few steps you can find in the notes,
you get
\begin{align}
    e^{iv} &=
    e^{-u}\left\{
        \frac{(x^2 + y^2 - a^2) - 2 iay}{(x - a)^2 + y^2}
    \right\}
\end{align}
But from the previous problem, we know
\begin{align}
    e^{-u} &= \cdots
\end{align}
and with a little bit of algebra gain which I'm not going to show you,
you get
\begin{align}
    e^{iv} &=
    \frac{(x^2 + y^2 - a^2) - 2iay}{\sqrt{
        [(x - a)^2 + y^2]
        [(x + a)^2 + y^2]
    }}
\end{align}
So now we have a direct relation between $v$ and $x,y$.
But you can see this is a messy expression.
Not straightforward to handle,
but let's see how to make progress.

So then let me notice that
\begin{align}
    \tan v &=
    \frac{-2ay}{x^2 + y^2 - a^2}
\end{align}
and if $v$ is a constant, then $\tan v$ is also a constant.
For reasons that will be clear in a moment,
let me also write
\begin{align}
    \sin v &=
    \frac{-2ay}{\sqrt{
        [(x - a)^2 + y^2]
        [(x + a)^2 + y^2]
    }}
\end{align}
So why am I writing both tan and sin?
It's because the tan is not unique,
it's the same for both $v$ and $v+\pi$.
If I tell you the tanget and the sign,
then I specify $v$ uniquely.
If I only specify $\tan v$,
there is a degeneracy and it's not clear what $v$ is.

Let's go back to the quesiton.
For $v=0$, $\tan v =0 $ and $y=0$.
So $v=0$ maps onto the $x$ axis.

Let $\tan v_0 = k$ be a constant.
Then
\begin{align}
    \frac{-2ay}{x^2 + y^2 - a^2} = k
\end{align}
With some algebra I'm skipping,
if you do it,
you can rewrite this as
\begin{align}
    x^2 = \left( y + \frac{a}{k} \right)^2
    &=
    a^2 \left( 1 + \frac{1}{k^2} \right)
\end{align}
and this is the circle centred at
$\left( 0, \frac{-a}{k} \right)$
with readius
$a\sqrt{1 + \frac{1}{k^2}}$.

Alright,
so let's draw it.
This is the $x$-axis and this is the $y$-axis.
Let's draw a circle.
This is the centre of the circle.
I've drawn part of the circle dotted.
Well you're wondering this is the equatino for the full circle,
but why have I drawn part of it solid and part of it dotted.
Well here's why.
Remember what I did was set $\tan v = k$.
That's how I got a circle.
But remember $\tan v$ is the same for both $v$ and $v+\pi$.
I'm going to argue that the dotted part corresponds to $v+\pi$,
and the solid part is the circle that maps to what we want.
It's actually quite tricky.

[picture here]

Recall the the denominator of the $\sin v$ expression is positive.
But how we were asked to look at the problem for $0\le v_0< \pi$.
So if $v < \pi$, then $\sin v$ > 0 and $y <0$,
so only the portion of the circle below the $y=0$ $x$-axis is what we want.
The part of the circle above the $x$-axis corresponds to when
$\sin v < 0$.
Let me write a couple of things and then I can ask questions.

Since we restricted $0< v_0 < \pi$,
$\sin v_0 > 0$.
Then only the part of the circle with $y<0$
corresponds to $v_0$.

Now you can convince yourself that the circle crosses the $x$-axis at
$x=-a$,
which maps to $u-\infty$,
and at $x=a$,
which maps to $u=+\infty$.
The lowest point of the circle,
which is at $x=0$,
corresponds to $u=0$.

So as you change $u$ holding $v$ fixed,
you trace out an arc.
Convince yourself that this is true.

Are there any questions about this?

We have a grand total of 5 minutes to solve this part C.
Alright.

(c) Consider the geometry below in the $w$-plane.
You have two infinite plates,
one at $u=0$ and one at $u=u_0$.
And this is the line $v=0$.
This is the direction of increasing $v$.
Now we're going to set this $u=0$ plane to potential
$V=0$
and this other plate to potential
$V=V_0$.

The solution for region between the two plates is
\begin{align}
    V &= V_0\left\{
        \frac{u}{u_0}
    \right\}
\end{align}
And remember this mapping maps from plane to cylinder.
We already showed that.

This maps into the geometry below,
a plane at $x=0$
and cylinder at $(\lambda a, 0)$
with radius $a\sqrt{\lambda^2 - 1}$.
But you remember in the formulation of the question,
the cylinder is centred distance $d$ from the plane
and has radius $R$.
So we equate
\begin{align}
    d &= a \lambda\\
    R &= a\sqrt{\lambda^2 - 1}
\end{align}
So then after some algebra I have in the notes,
you find that
\begin{align}
    a &=
    R \sqrt{\frac{d^2}{R^2} - 1}\\
    \lambda &=
    \frac{d/R}{\sqrt{\frac{d^2}{R^2} - 1}}
\end{align}
To solve the problem,
all we have to do is rewrite $V=V_0 u/u_0$,
rewriting $u$ in terms of $x$ and $y$.
But we know what that is because we have this thing from (a),
and here's what you end up with
\begin{align}
    V &= \frac{V_0}{u_0} \frac{1}{2}
    \log\left\{
        \frac{(x + a)^2 + y^2}{(x - a)^2 + y^2}
    \right\}
\end{align}
and we have to rewrite $u_0$ and $a$ in terms of
$d$ and $R$ using the expressions above.
It's given in the notes.
This is the solution
and we just have to rewrite the constants in terms of $d$ and $R$.
You can find the closed-form expression in the notes.
You can check by direct substitution into Laplace's euqation,
and you find that it satisfied Laplace's equation and also
that it satisfies the boundary conditions.

Let me stop here.
I know that was rather quick.

The equations are all in the notes,
but the only thing not in the notes are the explanations,
which I give in the lectures but are too long to write out.
Let me end the lecture here and stop the recording.
See you Thursday.


%\paragraph{Notations}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{table}[t]
	\centering
	\begin{footnotesize}
	\begin{tabular}{ll}\\
\toprule
		{\bf Symbol}  & {\bf Meaning}\\
\midrule
$\intint{H}$  & set of integers from $1$ to $H$\\
$\|\cdot\|$ & $L_2$-norm\\
$\|\cdot\|_F$ &    Frobenius norm  \\
$\|\cdot\|_0$ & $L_0$-norm\\
$\|\cdot\|_2$    &    spectral norm  \\
$\rmD_\rvv$ & diagonal matrix with vector $\rvv$ on the diagonal\\
% $N$           & number of data points\\
$D$           & data dimension\\
$H$           & number of layers in a network\\
$Q$           & number of sparse factors\\
$\mathcal{X}_i$ & input space of $f_{i+1}$ and output space of $f_{i}$\\
$\rvx_1,\ldots, \rvx_N $        &    data points\\
$\rmX \in\mathbb{R}^{N\times D}$&    data matrix\\
$\rmS_1, \ldots, \rmS_Q$        &    sparse matrices\\
$\mathcal{E}_1, \ldots, \mathcal{E}_Q$ & sparsity constraint sets\\
$\delta_{\mathcal{E}}$ & 		indicator functions for set $\mathcal{E}$\\
$\floor{\rvx}$ & round $\rvx$ to the flooring integer\\
% $\tau$  & current iteration \\
\bottomrule
	\end{tabular}
	\end{footnotesize}
	\caption{Notation used in this paper.}
	\label{tab:notation}
\end{table}
%\begin{table}[t]
%	\centering
%	\begin{footnotesize}
%	\begin{tabular}{cllcl}\\
%		\cline{1-2}\cline{4-5}\vspace*{1mm}
%		{\bf Symbol}  & {\bf Meaning}                      &  &    {\bf Symbol}          & {\bf Meaning}                    \\ 		\cline{1-2}\cline{4-5}
%		$N$           & number of data points              &  &    $\rvx_1,\ldots, \rvx_N $        &    data points            \\
%		$D$           & data dimension &  &    $\rmX \in\mathbb{R}^{N\times D}$&    data matrix            \\
%		$K$           & number of clusters                 &  &    $\rvu_1,\ldots, \rvu_K $        &    \kmeans centroids        \\
%		$\rvt$        &  cluster assignment vector           &  &    $\rmU\in\mathbb{R}^{K\times D}$ &    \kmeans centroid matrix  \\
%		&                 &  &    $\rvv_1,\ldots, \rvv_K $        &    \qkmeans centroids        \\
%		&          &  &    $\rmV\in\mathbb{R}^{K\times D}$ &    \qkmeans centroid matrix  \\
%		$Q$           & number of sparse factors    &  &    $\rmS_1, \ldots, \rmS_Q$        &    sparse matrices        \\
%		$\|\cdot\|$, & $L_2$-norm&  &    $\|\cdot\|_F$, &    Frobenius norm  \\
%		$\|\cdot\|_0$ & $L_0$-norm&  &    $\|\cdot\|_2$    &    spectral norm  \\
%		$\mathcal{E}_1, \ldots, \mathcal{E}_Q$ & sparsity constraint sets           &  & $\delta_{\mathcal{E}}$ & 		indicator functions for set $\mathcal{E}$\\
%		$\intint{M}$  & set of integers from $1$ to $M$ &  & $\tau$  &                       		current iteration  \\
%
%		$\rmD_\rvv$ & diagonal matrix with vector $\rvv$ on the diagonal\\                                                          		\cline{1-2}\cline{4-5}        \\
%	\end{tabular}
%	\end{footnotesize}
%	\caption{Notation used in this paper.}
%	\label{tab:notation}
%\end{table}
%\addtocounter{footnote}{0}
%\footnotetext{We also use the standard notations such as $\mathbb{R}^n$ and $\mathbb{M}_n$.}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{table}[t]
%	\centering
%	\begin{tabular}{|r|c|l|}
%		\hline
%		indices &  $i$, $j$, $m$, $n$, $p$, $q$ &  small  Latin characters  \\
%		other integers &  $K$, $Q$, $N$, $\ldots$ &  capital  Latin characters \\
%	%	vector spaces\footnotemark & $\mathcal{X}$, $\mathcal{Y}$, $\mathcal{H}$, $\ldots$ & Calligraphic letters \\
%		vectors (or functions) & $\rvx$, $\rvt$, $\rvk$, $\ldots$ & small bold Latin characters \\
%		matrices  & $\rmX$, $\rmU$, $\rmK$, $\ldots$ & capital bold Latin characters \\
%		transpose & $\top$ & $\rmX^\top$ transpose of  $\rmX$ \\
%		\hline
%	\end{tabular}
%	\caption{Notations used in this paper.}
%	\label{tab:notation}
%\end{table}
%\addtocounter{footnote}{0}
%\footnotetext{We also use the standard notations such as $\mathbb{R}^n$ and $\mathbb{M}_n$.}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%The notations frequently used in the paper are summarized in Table~\ref{tab:notation}.
%%
%Throughout the paper we use $\nexamples$ as the number of data samples and $\datadim$ the dimensionality of a data point.
%$\rmX \in \R^{\nexamples \times \datadim}$ is the data matrix.
%For $K \in \sN$, we define $\intint{K}=\left \lbrace k\in \sN: 1 \leq k \leq K\right \rbrace$.
%%
%For a given vector $\rvv$, $\rvv[i]$ is the $i$th component of $\rvv$.
%%
%For a given matrix $\rmM$, the notation $\rmM_{[i]}$ (resp. $\rmM^{[i]}$) refers to the $i$th row (column) of $\rmM$, the entry at the $i$th row and the $j$th column is denoted by $\rmM[i,j]$, and $\|\rmM\|_F$ denotes the Frobenius norm, $\|\rmM\|_2$ the spectral norm and $\|\rmM\|_0$ counts the number of non-zero entries in $\rmM$. \addHK{other norms?}
%
%
%
%
%\todo[inline]{The text is redundant with the table. In addition, we should remove the "small Latin character0", "capital Latin characters" as they do not provide any meaning. We should prefer the trick with the transpose.}


\documentclass{article}
\usepackage{mathrsfs}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage{graphicx}
\usepackage{amsfonts}
\DeclarePairedDelimiter{\ceil}{\lceil}{\rceil}
\DeclarePairedDelimiter{\floor}{\lfloor}{\rfloor}

\begin{document}
\begin{center}
\textbf{\huge{Week 9}}
\end{center}

\section{Lemma}
If $\mathscr{C}$ is a linear code, then
$$ d_{min}(\mathscr{C})= \text{min}_{\underbar{c} \neq 0}w_H (\underbar{c})$$
where, Hamming weight, $w_H(\underbar{c})=$ number of non zero positions in $\underbar{c}$.
\\ \\
Proof:

By definition,

    \begin{align*}
        d_{min}(\mathscr{C})&=\text{min} (d_H(\underbar{c}_1, \underbar{c}_2)) \qquad \underbar{c}_1, \underbar{c}_2 \in \mathscr{C} ; \underbar{c}_1 \neq \underbar{c}_2 \\
        \text{min} (d_H(\underbar{c}_1, \underbar{c}_2))& = \text{min} (w_H(\underbar{c}_1 - \underbar{c}_2) \qquad \underbar{c}_1, \underbar{c}_2 \in \mathscr{C} ; \underbar{c}_1 \neq \underbar{c}_2 \\
        &= \text{min}(w_H(\underbar{c})) \qquad\qquad    \underbar{c}\neq 0 ; \underbar{c} \in \mathscr{C}
    \end{align*}
    Hence proved.

\section{Examples}
We know that every subspace of a vector space has a basis, i.e a set of linearly independent vectors from the subspace which span the subspace.
\\
\begin{enumerate}
    \item Suppose $\mathscr{C}= \mathbb{F}_2^n$,
    \begin{itemize}
        \item Then any set of n linearly independent vectors from $\mathbb{F}_2^n$ will be a basis of $\mathscr{C}$.
        \item In particular we can choose the standard basis, $\underbar{c}_1= (1,0,\cdots,0),\underbar{c}_2= (0,1,0,\cdots,0),\cdots, \underbar{c}_n=(0,\cdots,0,1)$
    \end{itemize}
    \item Suppose $\mathscr{C}= \{ (0, \cdots,0), (1,\cdots,1)\}$
    \begin{itemize}
        \item As this code is closed under addition, this is a valid linear code.
        \item The basis for $\mathscr{C}$ will be $\{ (1,\cdots,1)\}$.
        \item This code encodes 1 bit.
    \end{itemize}
    \item Suppose $B= \{ g_1,\cdots, g_k \}$, $k<n$ are a set of linearly independent vectors in $\mathbb{F}_2^n$. What is linear code $\mathscr{C}$ for which B is a basis?
    \begin{itemize}
        \item Set of all linear combinations of vectors in B, i.e. $$ \mathscr{C}= \text{span}(B) = \left\{ \sum_{i=1}^{k} \alpha_i g_i: \alpha_i \in \mathbb{F}_2 \right\}$$
        \item $ |\mathscr{C}|= 2^k$, k is called the dimension of the subspace.

         Hence, $k= \log_2 |\mathscr{C}| $.
         \item Rate of  the code $= k/n $.
         \item This code encodes $k$ bits.
         \item Encoding is a linear operator, hence implementation is simple. $$ ( \alpha_1 , \alpha_2, \cdots, \alpha_k ) \xrightarrow{\text{encoded}} \sum_{i=1}^{k} \alpha_i g_i$$
         $$ ( \alpha_1 , \alpha_2, \cdots, \alpha_k ) \xrightarrow{\text{linear}} ( \alpha_1 , \alpha_2, \cdots, \alpha_k )_{1\times k} G_{k \times n}$$
         where,
         $ G_{k \times n}=
\begin{pmatrix}
  g_1 \\
  g_2 \\
  \vdots \\
  g_k \\
\end{pmatrix}$
    \end{itemize}

\end{enumerate}

\subsection{Generator matrix}

Pick any collection of $k$ linearly independent from $\mathbb{F}_2^n$ $\{ g_1,\cdots, g_k\}$.

$ G_{k \times n}=
\begin{pmatrix}
g_1 \\
g_2 \\
\vdots \\
g_k \\
\end{pmatrix}$
\\
\\
Rowspace(G) = span(rows of G) = k dimensional subspace of $\mathbb{F}_2^n$

$$ d_{min}(\mathscr{C})= \text{min}_{\underbar{c} \neq 0}w_H (\underbar{c})$$

Encoding is the operation of mapping $2^nR$ length messages to the n-length codewords in a unique manner. It is the mapping from k-length vectors over $\mathbb{F}_2$ to $\mathscr{C}$.

For linear codes, we can do this encoding as a linear mapping.
Encoding operation for linear codes requires polynomial in n, unlike non-linear codes require exponential complexity.

\subsection{Example}

\begin{itemize}
    \item Repetition code (eg. 2):
    $$ G=   [1,1,\cdots,1]_{1 \times n}$$
    $$ \text{Rowspace}(G)= \{ (0,0,\cdots,0),(1,1,\cdots,1)\}$$
    $$ d_{min}(\mathscr{C})= n \qquad dim(\mathscr{C})=1 \qquad R= \frac{k}{n}= \frac{1}{n}$$

    How can we implement minimum distance decoding more efficiently?
    $$ \hat{\underbar{c}}= argmin_{\underbar{c} \in \mathscr{C}} d_H(y,\underbar{c})$$
    For n=5:
    Suppose $y=(1\,1\,1\,0\,0)$, then minimum distance decoder output is $\hat{c}=(1\,1\,1\,1\,1)$.

    $$ MDD(y)= \begin{cases}
      \underbar{0}=(0,\cdots,0) & w_H(y)<\frac{n}{2} \\
      \underbar{1}=(1,\cdots,1) & w_H(y) > \frac{n}{2}
   \end{cases}$$
   This is the majority decoding rule.
\end{itemize}

\section{Binary Hamming code}

This is a class of codes, we take up a particular example, let:
$$ n=2^r -1 \qquad k=2^r-1-r \qquad d=3 \quad \forall \quad r \geq 3$$
$$ \text{if }r=3  \Rightarrow\qquad n=7,k=4,d=3$$

$$ G_{4 \times 7}= \left[ I_4 : \begin{bmatrix}
    1       & 0 & 1  \\
    0       & 1 & 1  \\
    1       & 1 & 0  \\
    1       & 1 & 1
\end{bmatrix}\right]$$
(we are appending $I_4$ with 3 columns to the right)

Note: The 4 rows of G are linearly independent vectors of $\mathbb{F}_2^7$. Rank(G) = number of linearly independent vectors in rows or columns = 4.

$\mathscr{C}$=Rowspace(G) is a 4-dim linear code. Rate= 4/7.

$|\mathscr{C}|= 2^k= 2^4=16$

$d_{min}(\mathscr{C}) =3$

\end{document}


\section{Asymptotics}

%%%%%%%%%%%%%%%%%%%%
\begin{frame}{$\Omega\;(\omega), \Theta, O\;(o)$}
  \[
    O(g(n)) = \set{f(n) \mid \exists c > 0, \exists n_0, \forall n \ge n_0: 0 \le f(n) \le c g(n)}
  \]

  \pause

  \[
    \Omega(g(n)) = \set{f(n) \mid \exists c > 0, \exists n_0, \forall n \ge n_0: 0 \le c g(n) \le f(n)}
  \]

  \pause

  \begin{align*}
    \Theta(g(n)) &= \set{f(n) \mid \exists c_1 > 0, \exists c_2 >0, \exists n_0, \forall n \ge n_0: \\
		& 0 \le c_1 g(n) \le f(n) \le c_2 g(n)}
  \end{align*}

  \pause

  \[
	  o(g(n)) = \set{f(n) \mid \textcolor{red}{\forall c > 0}, \exists n_0, \forall n \ge n_0: 0 \le f(n) \le c g(n)}
  \]

  \pause

  \[
	  \omega(g(n)) = \set{f(n) \mid \textcolor{red}{\forall c > 0}, \exists n_0, \forall n \ge n_0: 0 \le c g(n) \le f(n)}
  \]
\end{frame}
%%%%%%%%%%%%%%%%%%%%
\begin{frame}{(Problem 1.2.6)}
  \begin{exampleblock}{Problem 1.2.6 (4)}
	\[
		f(n) = \Theta(g(n)) \iff f(n) = O(g(n)) \land f(n) = \Omega(g(n))
	\]
  \end{exampleblock}

  \pause

  \begin{exampleblock}{Problem 1.2.6 (5)}
	\begin{align*}
	  f(n) = O(g(n)) &\iff g(n) = \Omega(f(n)) \\
	  f(n) = o(g(n)) &\iff g(n) = \omega(f(n))
	\end{align*}
  \end{exampleblock}

  \pause

  \begin{alertblock}{$f(n) = O(g(n)) \lor g(n) = \Omega(f(n)) ?$}
	\pause
	\[
		f(n) = n, \quad g(n) = n^{1 + \sin n}
	\]
  \end{alertblock}

  \pause

  \begin{exampleblock}{Problem 1.2.6 (6)}
	\[
		\Theta(g(n)) \cap o(g(n)) = \emptyset
	\]
  \end{exampleblock}
\end{frame}
%%%%%%%%%%%%%%%%%%%%
\begin{frame}{$\Omega\;(\omega), \Theta, O\;(o)$}
  \begin{alertblock}{Reference}
	``Big Omicron and Big Omega and Big Theta'' by Donald E. Knuth, 1976.
  \end{alertblock}
\end{frame}
%%%%%%%%%%%%%%%%%%%%
\begin{frame}{(Problem 1.2.10)}
  \[
	\log(n!) = \Theta(n \log n)
  \]

  \pause
  \vspace{0.30cm}
  \centerline{Prove by definition.}

  \pause
  \vspace{0.50cm}

  \centerline{Exercise: Prove it by Mathematical Induction.}
\end{frame}
%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Horner's rule (Problem 1.1.6)}
  \[
	P(x) = a_0 + a_1 x + a_2 x^2 + \cdots + a_{n-1} x^{n-1} + a_n x^n
  \]

  \pause
  \vspace{0.50cm}

  \centerline{Loop invariant (after the $k$-th loop):}

  \[
	 \sum_{i = n}^{i = n-k} a_i x^{k-(n-i)}
  \]
\end{frame}
%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%


\documentclass[12pt]{article}

\usepackage[left=1cm,right=1cm,
    top=2cm,bottom=2cm,bindingoffset=0cm]{geometry}
\usepackage{braket}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{mathrsfs}
\usepackage[T2A]{fontenc}
\usepackage[utf8x]{inputenc}
\usepackage[english]{babel}
\usepackage{graphicx}
\parindent=0.5cm

\usepackage{hyperref}
\usepackage{indentfirst}

\numberwithin{equation}{section}
%\usepackage{showkeys}

\begin{document}
\section*{MSAI Statistics \& Probability – Week 2 Seminar \& HW}\\

\textbf{Problem 1:} There are $R$ red and $G$ green balls in a box. Alice and Bob pick balls from the box in turns (one ball at a time). The one who is the first to pick a red ball is the winner. Find the probability that Alice wins if she is the first to make a move and 1) they do not put balls back in the box 2) they do put them back. Write a numeric answer for $R=4$ and $G=5$.
\\

\textbf{Problem 2:} Are the following statements true?
\begin{enumerate}
    \item $p(B|A)+p(B|\bar{A})=1$
    \item $p(B|A)+p(\bar{B}|\bar{A})=1$
\end{enumerate}
\\

\textbf{Problem 3:} Two subsets, $A_1$ and $A_2$ (they may coincide) are chosen randomly from $\{1,\dots,n\}$. Find the probability that $|A_1|=l_1$ and $|A_2|=l_2$ under the condition that $A_1\cap A_2=\varnothing.$ Give a numeric answer for $n=6,~l_1=2,~l_2=3.$
\\

\textbf{Problem 4:} There are $R$ red and $G$ green balls in a box. Alice is picking balls from the box, one at a time. $A_k$ is the event that, at time $k$ ($k$-th pick), she picks a red ball. Are the events $A_1,\dots,A_n$ independent? Alice may be picking balls with replacement or without replacement – if Alice chooses to pick, say, without replacement – then she's making all the picks without replacement. Same for with replacement – provide the answer for both cases. \\
\textit{You may (or may not) need the following notion in your solution: A set of more than 2 events is called mutually (or collectively) independent, if all pairs of events in it are pairwise independent.}
\\

\textbf{Problem 5:} Every edge (independently of all others) of $K_n$ – a complete graph on $n$ vertices, is colored in one of $k$
colors. For a set of vertices $S$ of the graph, let $A_S$ be the event that all the edges having both vertices in $S$ have the same color. What are the conditions on $S,T$ for the events $A_S$ and $A_T$ to be independent?

\end{document}


\documentclass[letterpaper,10pt]{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{siunitx}
\usepackage{physics}
\usepackage[left=1in,right=1in,top=1in,bottom=1in]{geometry}

\DeclareSIUnit \inch {in}
\DeclareSIUnit \fahrenheit {\degree F}
\DeclareSIUnit \rankine {R}

\title{Offset Unit Conversions}
\author{C.D. Clark III}

\begin{document}
\maketitle

An offset unit is a unit that contains an offset from zero (a bias). The most common examples are the Celsius and Fahrenheit
scales. Converting from Celsius to Kelvin is simple because they have the same
scaling factor, you just add or subtract the offset. However, converting from
Celsius to Fahrenheit requires correct conversion of the scaling factor and offset.

Any non-base unit ($\hat{v}$) can be expressed in terms of a base unit ($\hat{u}$) with a scaling factor ($\alpha$) and offset ($\beta$).
$$
\label{eq:offset_unit}
\hat{v} = \alpha \hat{u} + \beta
$$
Consider two offset units, $\hat{v}_1$ and $\hat{v}_2$, and assume our base unit $\hat{u}$ is an \emph{abolute} unit.
\begin{align}
  \hat{v}_1 &= \alpha_1 \hat{u} + \beta_1 \\
  \hat{v}_2 &= \alpha_2 \hat{u} + \beta_2
\end{align}

We want to know how to convert quantities expressed in one offset unit to another offset units.

\section{Incorrect Way}
What follows is not correct, but it illustrates the difficulty in deriving a formula to convert from on offset unit to the other.

To convert \emph{from} $\hat{v}_1$ \emph{to} $\hat{v}_2$,
\begin{align}
  \hat{u} &= \frac{\hat{v}_1 - \beta_1}{\alpha_1} \\
  \hat{v}_2 &= \alpha_2 \left( \frac{\hat{v}_1 - \beta_1}{\alpha_1} \right)  + \beta_2 \\
  \label{eq:v1_to_v2}
  \hat{v}_2 &= \frac{\alpha_2}{\alpha_1}\hat{v}_1  + \left( \beta_2- \frac{\alpha_2}{\alpha_1}\beta_1 \right)
\end{align}
If $\beta_1 = \beta_2 = 0$ this just gives the usual unit convertion formula.

Now, given a quantity $x = v_1 \hat{v}_1$, what is the numerical value for the quantity when expressed in terms of $\hat{v}_2$?

\textbf{tangent}
Note that \ref{eq:v1_to_v2} gives us a way to replace $\hat{v}_2$ with
$\hat{v}_1$. Given a units $\hat{v}_1$, we can use Equation \ref{eq:v1_to_v2}
to convert it to $\hat{v}_2$. For example, let $\hat{v}_1 = \si{\centi\meter}$ and $\hat{v}_2 = \si{\inch}$.
Then:
\begin{align}
  \si{\centi\meter} &= \SI{0.01}{\meter} \\
  \si{\inch} &= \SI{0.0254}{\meter}
\end{align}
and
\begin{align}
  \si{\inch} &= 0.0254\times \SI{100}{\centi\meter}  = \SI{2.54}{\centi\meter}
\end{align}
Whic is a conversion \emph{from} \si{\centi\meter} \emph{to} \si{\inch}. But if we want to convert a quantity expressed in \si{\centi\meter}
to \si{\inch}, say \SI{10}{\centi\meter} for example, we would do:
\begin{align}
  \SI{10}{\centi\meter} = \SI{10}{\centi\meter} \frac{\si{\inch}}{\SI{2.54}{\centi\meter}} = \frac{10}{2.54} \si{\inch}
\end{align}
So the numerical value of a quantity expressed in a given unit transforms in the opposite way as the unit. This is similar to the difference
between covariant and contravariant vectors, where the components of contravariant vector transform in the opposte way of the basis vectors.

To convert $x = v_1 \hat{v}_1$ to $\hat{v}_2$, we need to replace $\hat{v}_1$.
\begin{align}
  \hat{v}_1 &= \frac{\alpha_1}{\alpha_2}\hat{v}_2  + \left( \beta_1- \frac{\alpha_1}{\alpha_2}\beta_2 \right) \\
  v_1 \hat{v}_1 &= v_1 \left(\frac{\alpha_1}{\alpha_2}\hat{v}_2  + \left( \beta_1- \frac{\alpha_1}{\alpha_2}\beta_2 \right)\right) \\
  v_1 \hat{v}_1 &= v_1 \frac{\alpha_1}{\alpha_2}\hat{v}_2  + v_1 \left( \beta_1- \frac{\alpha_1}{\alpha_2}\beta_2 \right)
\end{align}
So what is $v_2$? For absolute units, we would just have $v_2 = v_1\frac{\alpha_1}{\alpha_2}$. But here we have the second term
with no unit to deal with. First, consider the cases when of the units is an absolute unit. Let $\hat{v}_1$ be an absolute unit, so
that $\beta_1 = 0$. Then we have
\begin{align}
  v_1 \hat{v}_1 &= v_1 \frac{\alpha_1}{\alpha_2}\hat{v}_2  - v_1 \frac{\alpha_1}{\alpha_2}\beta_2
\end{align}
The $v_1$ in the second term does not make any sense here. If we consider the case when $\hat{v}_1 = \si{\kelvin}$ and $\hat{v}_2 = \si{\celsius}$,
then this says that
\begin{align}
  \SI{100}{\kelvin} &= 100 \si{\celsius}  - 100 \times 273
\end{align}
which is not correct. The correct equation is of course
\begin{align}
  \SI{100}{\kelvin} &= 100 \si{\celsius}  - 273,
\end{align}
but this doesn't even appear to be dimensionally correct. The issue is that units don't obey the normal algebra. It does not make sense to multiply
an offset unit by a scaling factor (what is \SI{100}{\celsius} times 2?), so we cannot do it.

\section{Correct Way}

The correct way to convert offset units is to first subract off the bias,
convert the scale, and add the new bias on.  Note that the $\beta$ term in
Equation \ref{eq:offset_unit} is a numerical value expressed in the \emph{scale
of the unit being defined}. When we convert from the absolute unit, to the offset unit,
the $\beta$ term is \emph{not} scaled. i.e.
\begin{align}
  100\hat{u} \rightarrow \alpha (100\hat{u}) + \beta \ne \alpha (100\hat{u}) + 100 \beta
\end{align}

Note that, while $\hat{v}$ is an offset unit, $\hat{v}-\beta$ is \emph{not}. This
illustrates the breakdown in our notation, it looks like we are adding two different things.

Reformulating our conversion then
\begin{align}
  \hat{v}_1  - \beta_1 &= \alpha_1 \hat{u} \\
  \hat{v}_2  - \beta_2 &= \alpha_2 \hat{u}.
\end{align}
Since $\hat{v}_i  - \beta_i$ is an absolute unit, it will obey our usual algebra, as long as we keep it together. Let $\hat{v}^\prime_i = \hat{v}_i - \beta_i$. Then
\begin{align}
  \hat{v}_1  - \beta_1 &= \alpha_1 \hat{u} = \hat{v}^\prime_1 \\
  \hat{v}_2  - \beta_2 &= \alpha_2 \hat{u} = \hat{v}^\prime_2 ,
\end{align}
and
\begin{align}
  \hat{v}^\prime_1 &= \alpha_1 \hat{u} \\
  \hat{v}^\prime_2 &= \alpha_2 \hat{u} = \frac{\alpha_2}{\alpha_1} \hat{v}^\prime_1 \\
  \hat{v}^\prime_1 &= \frac{\alpha_1}{\alpha_2} \hat{v}^\prime_2 \\
\end{align}
Note that $\frac{\alpha_1}{\alpha_2}$ (expressed in terms of each units scaling factor for the base unit of the dimension)
is the conversion factor from $\hat{v}_2$ to $\hat{v}_1$ but will convert quantities expressed
in $\hat{v}_1$ to $\hat{v}_2$.
$$
v^\prime_1 \hat{v}^\prime_1 = v^\prime_1 \frac{\alpha_1}{\alpha_2} \hat{v}^\prime_2 = v^\prime_2 \hat{v}^\prime_2
$$
Now we just need a rule to convert between quantities expressed in terms of the primed and unprimed units:
\begin{align}
  v \hat{v} &= (v - \beta)\hat{v}^\prime = v^\prime \hat{v}^\prime \\
  v^\prime \hat{v}^\prime &= (v^\prime + \beta)\hat{v} = v\hat{v}
\end{align}
Now given $x = v_1\hat{v}_1$
\begin{align}
  v_1 \hat{v}_1 &= (v_1 - \beta_1) \hat{v}^\prime_1 = (v_1 - \beta_1) \frac{\alpha_1}{\alpha_2} \hat{v}^\prime_2 = \left((v_1 - \beta_1) \frac{\alpha_1}{\alpha_2} + \beta_2\right) \hat{v}_2 \\
  v_2 &= \left((v_1 - \beta_1) \frac{\alpha_1}{\alpha_2} + \beta_2\right)
\end{align}

\subsection{Final Note}

The discussion about covariant and contravariant vectors above is fun, but it does have some practical consequenses. In fact, it is the reason
I started writing this document.

Take the well known conversion forula for Celsius to Fahrenheit:
$$
\si{\fahrenheit} = \frac{9}{5} \si{\celsius} + 32
$$

The Rankine scale is an aboslute temperature scale that uses the Fahrenheit degree size. That is, Rankine is zero at absolute zero, but
the size of one degree is the same as Fahrenheit. So, to convert from Kelvin to Rankine, we have
\begin{equation}
  \label{eq:rankine_quant}
\si{\rankine} = \frac{9}{5} \si{\kelvin}
\end{equation}
Now, since a Kelvin degree is larger than a Rankine degree, we also have
$$
\SI{900}{\rankine} = \SI{500}{\kelvin}
$$
but this means that
\begin{equation}
  \label{eq:rankine_unit}
\si{\rankine} = \frac{5}{9}\si{\kelvin}.
\end{equation}
So which is correct?

The answer is that they are both correct, but Equation \ref{eq:rankine_quant} is a conversion for the numerical values of the quantity, and
Equation \ref{eq:rankine_unit} is a conversion for the units. Perhaps Equation \ref{eq:rankine_quant} should be written as
\begin{equation}
  T_R = \frac{9}{5} T_K,
\end{equation}
i.e. a relationships between the \emph{components} of a quantity. The point here is that we must be careful when defining unit conversions
in the library. The library expects conversions for the units, \emph{not} the components, so Equation \ref{eq:rankine_unit} is the correct way
to specify the unit.

\section{General Case}

In general, a unit may be defined as an arbitrary transformation. For example, the American Wire Gauge unit is defined as
$$
d_n = \SI{0.127}{\milli\meter} \times 92^{\frac{36 - n}{39}}
$$
where $n$ is the wire gauge. To perform arbitrary unit conversions, including conversions between non-linear units, we just need the
function that relates unit to a linear, absolute base unit. Given two units:
\begin{align}
\hat{v}_1 &= f(\hat{u}) \\
\hat{v}_2 &= g(\hat{u})
\end{align}
%then
%\begin{align}
  %\hat{u} &= f^{-1}(\hat{v}_1) \\
%\hat{v}_2 &= g(f^{-1}(\hat{v}_1)).
%\end{align}
To convert quantities, be just convert to the base unit first, then to the second unit.
\begin{align}
v_1 \hat{v}_1 &= g(f^{-1}(v_1)) \hat{v}_2 = v_2 \hat{v}_2 \\
v_2 &= g(f^{-1}(v_1))
\end{align}

\end{document}


This appendix describes the analytical solution used in Section \ref{sec:tf-ver}.

\section{Verification of the thermal-fluids model}
\label{appendix:ver}

The analytical solution of the problem is
\begin{align}
    T_c (r, z) &= T_{in} + \frac{q_{ave} R_f^2 L}{2 \rho c_p v \pi R_c^2} \left[ 1 + cos \left( \frac{\pi}{L} z \right) \right] \\
    T_3(z) &= T_c(z) + \frac{q_{ave} \pi}{2} sin \left( \frac{\pi}{L} z \right) R_f^2 \frac{ln(R_i/R_m)}{2 k_i} \\
    T_2(z) &= T_3(z) + \frac{q_{ave} \pi}{2} sin \left( \frac{\pi}{L} z \right) R_f^2 \frac{ln(R_m/R_g)}{2 k_m} \\
    T_1(z) &= T_2(z) + \frac{q_{ave} \pi}{2} sin \left( \frac{\pi}{L} z \right) R_f^2 \frac{ln(R_g/R_f)}{2 k_g} \\
    T_f (r=0, z) &= T_1(z) + \frac{q_{ave} \pi}{2} sin \left( \frac{\pi}{L} z \right) R_f^2 \frac{1}{4 k_f} \\
    T_f (r, z=L/2) &= \frac{q_{ave}}{4 k_f} \left(R_f^2 - r^2\right) + T_1 (z=L/2) \\
    T_g (r, z=L/2) &= \frac{T_1 (z=L/2)-T_2 (z=L/2)}{ln (R_f/R_g)} ln (r/R_g) + T_1(z=L/2) \\
    T_m (r, z=L/2) &= \frac{T_2 (z=L/2)-T_3 (z=L/2)}{ln (R_g/R_m)} ln (r/R_m) + T_2(z=L/2) \\
    T_i (r, z=L/2) &= \frac{T_3 (z=L/2)-T_c (z=L/2)}{ln (R_m/R_i)} ln (r/R_i) + T_3(z=L/2) \\
    T_c (r, z=L/2) &= T_c(z=L/2)
    \intertext{where}
    T_{c} &= \mbox{bulk coolant temperature } [^{\circ}C] \notag \\
    T_{in} &= \mbox{inlet coolant temperature } [^{\circ}C] \notag \\
    q_{ave} &= \mbox{average power density } [W \cdot cm^{-3}] \notag \\
    R_f &= \mbox{fuel compact radius } [cm] \notag \\
    L &= \mbox{fuel column height } [cm] \notag \\
    \rho &= \mbox{helium density } [kg \cdot cm^{-3}] \notag \\
    c_p &= \mbox{helium heat capacity } [J \cdot kg^{-1} \cdot K^{-1}] \notag \\
    v &= \mbox{average helium velocity } [cm \cdot s^{-1}] \notag \\
    R_c &= \mbox{coolant channel radius } [cm] \notag \\
    R_g &= \mbox{gap radius } [cm] \notag \\
    R_m &= \mbox{moderator radius } [cm] \notag \\
    R_i &= \mbox{film radius } [cm] \notag \\
    k_f &= \mbox{fuel compact thermal conductivity } [W \cdot cm^{-1} \cdot K^{-1}] \notag \\
    k_g &= \mbox{gap thermal conductivity } [W \cdot cm^{-1} \cdot K^{-1}] \notag \\
    k_m &= \mbox{moderator thermal conductivity } [W \cdot cm^{-1} \cdot K^{-1}] \notag \\
    k_i &= \mbox{film thermal conductivity } [W \cdot cm^{-1} \cdot K^{-1}]. \notag
\end{align}


\section{Math IV - Numerical Methods}
  \subsection{Fast Square Testing}
    An optimized test for square integers.
    \code{math/numerical/is_square.cpp}
  \subsection{Simpson Integration}
    Use to numerically calculate integrals
    \code{math/numerical/simpson_integration.cpp}


\section{Model}

\begin{equation}
    \textbf{State space: }\statespace = \R^{2} \times \{0, 1, 2, 3, 4, 5\} \times \{ 18, 19, \cdots , 84, 85\}
\end{equation}

\begin{equation}
    \textbf{Action space: }\actionspace  = \{0, 15, 25, 37, 45\}
\end{equation}

\begin{equation}
    \textbf{States: }\{A, Z, K, Q\}, \qquad \textbf{Actions: } \{H\}
\end{equation}

\begin{equation}
    \textbf{Variables: }\{L, W, Y, C, B, U\},  \qquad \textbf{Parameters: } \{\kappa, \mu_\rho, \sigma_\rho, p_\psi, \sigma_\epsilon, \zeta, a, b, c, d\}
\end{equation}

The variables are normalized such that their values correspond to a single week. I.e. the agent can choose to work between 0 and 45 hours a week. The agents consumption will be normalized to weekly levels. The state $A$ represents assets. $Z$ is the idiosyncratic wage level. $K$ is the number of kids and $Q$ is the age of the agent. In this formulation time $(t)$ and age $(Q)$ are explicitly modelled separately, such to clearly separate what is a function of age and what is a function of time. f.x. The wage level is modelled to follow a certain pattern as a function of age, not of time. Importantly for solving the model, I do not need to integrate out the age since it is perfectly deterministic. The action space contains a single discrete variable $H$ that denotes the number of working hours.

Model dynamics:

\begin{align}
    U_t (C_t, L_t, B_t) &= C_t^{1-B_t}L_t^{B_t} \label{eq:utility_v1}\\
    B_t (K_t) &= f^{B}(K_t) \label{eq:alpha_v1}\\
    Y_t ( H_t, W_t) &= H_t \cdot W_t \label{eq:salary_v1}\\
    C_t (A_t, Y_t) &= \kappa \cdot (A_t + Y_t) \label{eq:consumption_v1}\\
    L_t(H_t) &= 7 \cdot 24 - H_t \label{eq:leisure_v1}\\
    W_t(Z_t, Q_t) &= f^{W_t}(Q_t) + Z_t \label{eq:wage_v1}
\end{align}

Law of motion (transitionsligninger XXX):

\begin{align}
    A_{t+1}(A_t, Y_t, C_t) &= \rho_{t+1} \cdot (A_t + Y_t - C_t), \qquad \rho_{t+1} \sim \ndist(\mu_{\rho}, \sigma_{\rho}) \label{eq:assets_v1} \\
    K_{t+1} (K_t) &= K_t + \psi_{t+1}, \qquad \psi_{t+1} \sim Bernoulli(p_{\psi}) \label{eq:kids_v1}\\
    Z_{t+1} (Z_{t}) &=  \zeta \cdot Z_t \cdot \epsilon_{t+1} \qquad \log (\epsilon_{t+1} ) \sim \ndist(0, \sigma_{\epsilon}) \label{eq:wage_level_v1}\\
    Q_{t+1} (Q_t)&= Q_t + 1 \label{eq:age_v1}
\end{align}

\begin{figure}
    \centering
    \includegraphics[scale=0.09, angle=90]{figures/modeldynamic_tmp.jpg}
    \caption{Model dynamics (temporary figure) XXX}
    \label{fig:modeldynamics}
\end{figure}

Other variables in the model are: Utility as modelled in equation \eqref{eq:utility_v1} that follow the Cobb-Douglas formulation. Equation \eqref{eq:alpha_v1} denotes the utility dynamic of having kids. Later the functional form of $f^{B}$ is discussed. Equation \eqref{eq:salary_v1} models salary as being the product of the number of working hours and the wage level. \eqref{eq:consumption_v1} is assumed to be fraction of the sum of salary and assets (sometimes referred \textit{cash-on-hand}). \eqref{eq:leisure_v1} shows leisure being the total number of hours in a week - minus the number of hours worked. Equation \eqref{eq:wage_v1} governs how the wage level at time $t$ can be decomposed into an idiosyncratic component and a deterministic component that are deterministic as a function of age.

4 equations govern the law of motion: Equation \eqref{eq:assets_v1} governs how the assets in period $t$ grows with a normally distribution return, adding the salary and subtracting the consumption of period $t$. Equation \eqref{eq:kids_v1} denotes the growth of children. The idiosyncratic wage level is governed by equation \eqref{eq:wage_level_v1}. Where it's assumed that the previous period has influence on the wage level. Equation \eqref{eq:age_v1} shows how the age of the agent follows a deterministic process.

Finally two functions in the formulation should be clearly explicited: $f^{B}, f^{W}$. Starting with $f^{B}$ a couple of things should be noted, about how the function should behave. First and foremost:

\begin{equation}
    f^{B}: \N_{0,+} \mapsto [0,1]
\end{equation}

This is due to the fact that the utility function is of the Cobb-Douglas formulation, where the $B$ parameter should be contained in the interval [0,1]. Second it should be noted that it should not be perfectly determined whether or not:

\begin{equation}
    \frac{d}{d K}f^{B} > 0 \qquad \textbf{or} \qquad \frac{d}{d K}f^{B} < 0
\end{equation}

Therefore the formulation:

\begin{equation}
    f^{B} (K_t)= a + b \frac{\exp{c + d K_t}}{1 + \exp{c + d K_t}}\qquad  \text{s.t. } a + b < 1, a \land b > 0
\end{equation}

The function $f^{W}$ is not parametric, and determined by data from Dansk Statistik XXX.

Parameters of the model being: $\kappa, \mu_\rho, \sigma_\rho, p_\psi, \sigma_\epsilon, \zeta$. and $a, b, c, d$, where the latter govern the dynamic of the utility of leisure conditioned on the number of kids. $\kappa$ governs the consumption profile, $\mu_\rho$ denotes the expected return of the assets, $\sigma_\rho$ the variance. $p_\psi$ is the probability of a new child. $\sigma_{\epsilon}$ is the variance (before the log transformation) of the log-normal shock to the income process. $\zeta$ is growth constant on the wage level evolution.

\textbf{MULIGE UDVIDELSER}: 1) function for $p_\psi (Q_t, K_t)$.  Forbrugs kvotienten $\kappa (Q_t, K_t)$ kan være en funktion af alderen og børn. Man kan også overveje at tilføje transfers. Uddannelses niveayu og dets indflydelse på wage er også en mulighed.


\textbf{Find a way to modify your program of Exercise 8.4, as in Exercise 3.8 but now for a second order problem, to make use of matrix exponentials rather than time discratization. What effect does this have on the computation time?}
\newline

In this case, just for $N=24$ it took $t=  25.3899480~s$ to complete the simulation, a severe increase for such a small number of grid points.

\subsection*{Matlab code for this problem}
\begin{verbatim}
%% Problem 5 - 8.5 Trefethen

% Grid and initial data:

Nvector = [24];
time = zeros(length(Nvector),1);
for k = 1:length(Nvector)
    N=Nvector(k);
    [D,x] = cheb(N);  y = x;
    D2 = D^2;
    D2 = D2(2:end-1,2:end-1);
    L = kron(eye(N-1),D2)+kron(D2,eye(N-1));
    A = [zeros(size(L)) eye(size(L)); L zeros(size(L))];
    dt = 6/N^2;
    [xx,yy] = meshgrid(x(2:N),y(2:N));
    x = xx(:); y = yy(:);

    plotgap = round((1/3)/dt); dt = (1/3)/plotgap;
    u0 = exp(-40*((x-.4).^2 + y.^2));
    u = u0;
    % Time-stepping by leap frog formula:
    [ay,ax] = meshgrid([.56 .06],[.1 .55]); clf
    tic
    for n = 0:3*plotgap
        t = n*dt;
%         if rem(n+.5,plotgap)<1     % plots at multiples of t=1/3
%           uu = reshape(u,N-1,N-1);
%           i = n/plotgap+1;
%           subplot('position',[ax(i) ay(i) .36 .36])
%           [xxx,yyy] = meshgrid(-1:1/16:1,-1:1/16:1);
%           uuu = interp2(xx,yy,uu,xxx,yyy,'cubic');
%           mesh(xxx,yyy,uuu), axis([-1 1 -1 1 -0.15 1])
%           colormap(1e-6*[1 1 1]); title(['t = ' num2str(t)]), drawnow
%         end
    % ------------------------------ %
    % -- Using matrix exponential -- %
    % ------------------------------ %
        v = expm(A*t)*[u0;zeros(size(u0))];
        u = v(1:length(u0));
    end
    time(k) = toc;
end
\end{verbatim}


\chapter{Maximum and minimum algorithms}
\Label{cha:maxmin}

In this chapter we discuss the formal specification of algorithms
in the \cxx Standard Library \cite[\S 28.7.8]{cxx-17-draft}
that compute the maximum or minimum values of their arguments.
As the algorithms in Chapter~\ref{cha:non-mutating}, they also do not modify
any memory locations outside their scope.
The most important new feature of the algorithms in this chapter
is that they compare values using binary operators such as \inl{<}.

We consider in this chapter the following algorithms.

\begin{itemize}

\item
We discuss some properties of relations operators in \S\ref{sec:relationaloperators}.

\item
We introduce in \S\ref{sec:arraybounds} various predicates
that describe basic order properties for arrays whose elements are of~\valuetype.

\item
\clamp, which is discussed in \S\ref{sec:clamp},
is a very simple algorithms that ``clamps'' (or ``clips'') a value between a
pair of boundary values.

\item \maxelement
returns an index to
a maximum element in a range. Similar to \find it also
returns the smallest of all possible indices.
This algorithm is discussed in \S\ref{sec:maxelement}.
In \S\ref{sec:maxelementii}, we introduce an alternative
specification \maxelementii which relies on user-defined predicates.

\item \maxseq  in \S\ref{sec:maxseq}
is very similar to \maxelement and will serve as an
example of \emph{modular verification}.
It returns the maximum value itself rather than an index to
it.

\item \minelement in \S\ref{sec:minelement}
can be used to find the smallest element in an array.

\item \minmaxelement in \S\ref{sec:minmaxelement}
is used to find simultaneously the smallest and largest element in a given range.
This algorithms relies on the auxiliary function
\makepair (\S\ref{sec:makepair}).
\end{itemize}

First, however, we discuss in \S\ref{sec:relationaloperators}
general properties that must be satisfied by the relational operators.

%\clearpage

\section{A note on relational operators}
\Label{sec:relationaloperators}

Note that in order to compare values, algorithms in the
\cxx Standard Library \cite[\S 28.7.8]{cxx-17-draft} usually rely solely on
the \emph{less than} operator~\inl{<} or special function
objects.
To be precise, the operator~\inl{<} must be a \emph{partial
order},\footnote{
    See~\url{http://en.wikipedia.org/wiki/Partially_ordered_set}
}
which means that the following rules must hold.
%
\begin{alignat*}{5}
&\text{irreflexivity} &&\qquad  \forall x     &&: \neg(x < x)         \\
&\text{asymmetry}     &&\qquad  \forall x,y   &&: x < y             &&\implies \neg(y < x)\\
&\text{transitivity}  &&\qquad  \forall x,y,z &&: x < y \wedge y < z &&\implies x < z
\end{alignat*}

If you wish to check that the operator~\inl{<} of our \valuetype\footnote{
    See \S\ref{sec:frequentPattern}
}  satisfies these properties
one can formulate the lemmas of \logicref{Less} and verify them with \framac.

\input{Listings/LessThanComparable.acsl.tex}

It is of course possible to specify and implement the algorithms
of this chapter by only using operator~\inl{<}.
For example, \inl{a <= b} can be written as \texttt{a < b || a == b},
or, for our particular ordering on \valuetype, as \inl{!(b < a)}.
Listing~\logicref{Less} therefor also contains lemmas on representing
the operator~\inl{>}, \inl{<=}, and~\inl{>=} through operator \inl{<}.

\section{Predicates for bounds and extrema of arrays}
\label{sec:arraybounds}

We define in the following listing the predicates \logicref{MaxElement}
and \logicref{MinElement} that we will use for the specification of various algorithms.
We will discuss these predicates in more detail in \S\ref{sec:maxelementii}
and \S\ref{sec:minelement}.

\input{Listings/ArrayExtrema.acsl.tex}

\clearpage

The aforementioned predicates rely on the predicates
\logicref{LowerBound} and \logicref{UpperBound} which
are shown in the following listing together with
the related predicates \logicref{StrictUpperBound} and \logicref{StrictLowerBound}.

\input{Listings/ArrayBounds.acsl.tex}

These predicates concisely express the
comparison of the elements in an array (segment) with a given value.
We will heavily rely on these predicates both in this chapter and
in Chapter~\ref{cha:binary-search}.

\clearpage

\input{maxmin/clamp}
\input{maxmin/make_pair}
\input{maxmin/max_element}
\input{maxmin/max_element2}
\input{maxmin/max_seq}
\input{maxmin/min_element}
\input{maxmin/minmax_element}