%begin-include

\section{Predicates}

\begin{para}
Until now, we have been concerned with propositions --- statements that were either true or false --- and how we could manipulate them with using logical connectives.
We will now take one step further and take into consideration the particular objects which we make statements about.
Welcome to the world of predicate logic.
\end{para}

\begin{para}[Predicates]
Just as propositional logic was all about propositions, predicate logic is all about \emph{predicates}.
An $n$-ary predicate $P(x_1,\ldots,x_n)$, where $n$ is zero or a natural number, is a statement that depends on $n$ variables $x_1,\ldots,x_n$, known as \emph{arguments}, that is either true or false depending on the values that those variables take.
The set of values the variables are allowed to take is called the \emph{domain of discourse} (or \emph{domain}, for short).
Of course, nullary predicates --- which are mere propositions --- need not be followed by parentheses since they do not take any arguments.

For example, if we were working with the set of all animals as domain, we could define a unary predicate $P(x)$ as ``$x$ can fly'' in such a way that $P(\tx{cow})$ would be false but $P(\tx{bird})$ would be true.

Notice how the proposition that arises after any assignment of values to the arguments of a predicate is written substituting the different variable symbols by the values they take, just as we did in $P(\tx{cow})$ for $P(x)$.

\end{para}

\begin{para}
Predicates, just like propositions, can be combined with the connectives from propositional logic in order to form new predicates.
Nonetheless, being able to work directly with the objects we are ``saying things'' about opens up a new world of possibilities.
One of them is the ability to incorporate functions and constants into our predicates, but the most notable of all is the use of \emph{quantifiers}.

As their name implies, quantifiers allow us to create new predicates stating the quantity of elements in the domain that verify a certain predicate. The two most fundamental are the \emph{universal} quantifier and the \emph{existential} quantifier.
The universal quantifier is used to state that a certain predicate $P(x_1,\ldots,x_n)$ is true for every value taken by a variable $x_i$ (with $i\in\{1,\ldots,n\}$) in the domain. This is written as $(\forall x_i)P(x_1,\ldots,x_n)$ and is read as ``for all $x_i$, $P(x_1,\ldots,x_n)$.''
On the other hand, we use the existential quantifier to state that a certain property\footnote{``Property'' can be used to mean ``predicate.''} $P(x_1,\ldots,x_n)$ is verified by, at least, one assignment to $x_i$ of an element in the domain. We write this as $(\exists x_i)P(x_1,\ldots,x_n)$ and read it as ``there exists an $x_i$ such that $P(x_1,\ldots,x_n)$.''
The predicate $P$ over which we are quantifying is said to be the \emph{scope} of the quantifier.

A variable $x$ appearing in the scope of a quantifier $(\forall x)$ or $(\exists x)$ is said to be a \emph{bound} variable. Variables that are not bound are called \emph{free} variables. For example, if we let $P(x)$ and $Q(x)$ be predicates, in the predicate $P(x) \land (\forall x)Q(x)$, the first occurrence of $x$ is free but the second is bound.
Nevertheless, it should be taken into account that it is a pretty poor notational choice to use the same symbol to represent a free and a bound variable in the same predicate. This last predicate could have been better written as $Q(y) \land (\forall x)Q(x)$, but, from a purely formal point of view, there is nothing wrong with the original formulation.
\end{para}

\begin{para}[Order matters]
We are now ready to explore our first application of predicate logic; isn't that exciting!?
And that application goes far beyond any mathematics that you will ever study: it is \href{https://www.youtube.com/watch?v=-mLpe7KUg9U}{love}.

Yesterday, I was scrolling through my social media feed and I found this statement: ``No matter who you are, there exists someone who will love you''.
Just when I was going to click the ``don't show me posts like this in the future'' button, I had a brilliant idea: that was a perfect sentence waiting for us --- you, my dear reader, and me --- to analyse. So let us get to it!

It should be clear that, if we take the set of all human beings as our domain and let $\heartsuit(x,y)$ denote the predicate ``$y$ loves $x$'', the statement ``no matter who you are, love has already found someone for you'' can be written as $(\forall x)(\exists y) \heartsuit(x,y)$.
Now, I have a simple question: what would happen if you changed the order of the quantifiers and wrote $(\exists y)(\forall x)\heartsuit(x,y)$?
What does this sentence mean?
It means that there exists an individual who we all happen to be in love with!
The first sentence read ``for every person $x$, there exists a person $y$ who will love them'', but now it reads ``there exists a person $x$ who is loved by every person $y$''.
Do you have the slightest idea of the mess you have made by swapping two quantifiers? You have taken a cheesy sentence and transformed its meaning to postulate the existence of a love monster!

The moral of the story is simple: do not swap quantifiers.
Nevertheless, if the same quantification is being used on two different variables consecutively, such as in $(\exists x)(\exists y)\heartsuit(x,y)$, there is obviously no harm in swapping the order of quantifiers; in fact, that last sentence would be often written as $(\exists x,y)\heartsuit(x,y)$.

Hey, I know you might be angry at me for having created hype with applications and all that stuff and having only given you a cheesy sentence. What can I say? I needed your attention! I hope you will forgive me.
\end{para}

\begin{para}[Notation]
\label{notaquan}
There is a little bit of freedom in the way that quantifiers can be written and each alternative has its own advantages. The following are just some examples of the notation that one can find in the literature:
\[ \forall x, \exists y, P(x,y),\qquad (\forall x)(\exists y) : P(x,y),\qquad \forall x : \exists y : P(x,y).\]

I see myself as a liberal person when it comes to notation, but there is something I beg you to do: do not EVER write quantifiers after their scope. Every time you write $P(x),\forall x$ a cute kitten cries immersed in sadness, so, please do not do that.
This is not a matter of taste; it is a matter of readability. When you are about to read an expression, what variables are being quantified is the first thing you should know, not the last!

Furthermore, as we have just seen, the order in which quantifiers are set is very, very important; thus, if you write them at the end\ldots what order are they in? Should they be read from left to right or from right to left? I mean, it is just inelegant and clumsy. Do not do that, please.

If you really want to do things right, you should extend this idea to your writing.
I know; saying, for instance, ``$P(x)$ is true for all $x$'' sounds natural and harmless, but as the number of quantifiers increases --- and, believe me, it will increase and not just in logic, but in ordinary mathematics --- you better have those quantifiers right at the start.
As a rule of thumb, I would only put a quantifier at the end of a predicate in written text if it is a single universal quantifier, and I would never ever write a quantifier at the end in a symbolic expression.

Now that we are dealing with notation, let me draw your attention to an important issue.
In \ref{hierarchy}, we said that, in propositional logic, there was no harm in getting rid of the parentheses that surround a full proposition, so there was no need to write $(p \land q)$ and we could just write $p\land q$.
In predicate logic, however, we need to be somewhat careful with this idea.
If, for instance, I wrote $(\forall x) P(x) \land Q(x)$, I would not mean the same as if I had written $(\forall x)(P(x) \land Q(x))$.
In the former case, the scope of the quantifier is $P(x)$, whereas in the latter it is $P(x)\land Q(x)$.

In order to save ourselves some parentheses and make everything a little bit cleaner, we will use the following convention.
If a sequence of quantifiers is followed by a dot, it will mean that its scope is the remainder of the predicate unless an opening parenthesis is present before the quantifiers; if this happens, the scope will end at the point where the matching closing parenthesis is located.
Thus, in $(\forall x)(\exists y)\qsep P(x)\land Q(y)$, the scope of the first quantifier is $(\exists y)(P(x) \land Q(y)$ and that of the second is $P(x) \land Q(y)$.
On the other hand, in $( (\forall x)\qsep P(x) ) \land Q(x)$, the scope of the quantifier is $P(x)$.
\end{para}

\begin{para}[Negating predicates]
If I told you that every single human loves mathematics, what would you have to do to prove me wrong?
You would simply need to show the existence of a person who does not like mathematics.
Therefore, the negation of the sentence ``for all $x$, $P(x)$ holds'' is ``there exists an $x$ such that $P(x)$ does not hold.''
In other words, $\lnot (\forall x) P(x)$ is the same as $(\exists x)(\lnot P(x))$.

Now, let us just say that I postulate the existence of a person who has the superpower of flying.
If you wanted to show that my statement is false, you would have to prove that no person has the superpower of flying or, equivalently, that ``for every person $x$, $x$ cannot fly''.
What is the moral of the story? That $\lnot(\exists x)P(x)$ equivalent to $(\forall x)(\lnot P(x))$.

Putting together all that we have learnt: how would you write the predicate $\lnot(\forall x)(\exists y)P(x,y)$ without having a negation connective before any quantifier?
Take a sheet of paper and write down the result.

[Spoiler alert]
If you have understood this part, you should have reasoned as follows: $\lnot(\forall x)(\exists y) P(x,y)$ is the same as $(\exists x)(\lnot (\exists y) P(x,y))$, which is equivalent to $(\exists x)(\forall y) \lnot P(x,y)$. If you got this right, congrats! You are on the right track.
If you did not, do not worry: make yourself a good cup of tea, go through this material again and give it some thinking.

As you can see, when negating an expression involving the existential and universal quantifiers, the only thing we need to do is ``swap them and negate what is inside them''.
That is a pretty easy rule, but, as always happens with this kind of shortcuts, you should only apply it if you really know what is going on underneath the hood.
\end{para}

\begin{para}[Defining quantifiers]
There is something kind of significant in our analysis of the negation of predicates. What we have shown --- probably without your noticing --- is that one of our quantifiers is redundant. The predicate $(\exists x) P(x)$ is equivalent to $\lnot (\forall x)(\lnot P(x))$, so, in a way, there was no need to introduce the existential quantifier once we had the universal one. Conversely, $(\forall x) P(x)$ is equivalent to $\lnot(\exists x)(\lnot P(x))$, hence the existential quantifier could also serve us as our only quantifier.

Despite the redundancy, we introduced both of them for an obvious reason:
for us humans, it is more natural to think ``every $x$ verifies $P(x)$'' than ``there does not exits an $x$ not verifying $P(x)$''.
\end{para}

\begin{para}[Pseudo-quantifiers]
\label{pseudoquan}
We will now introduce some ``pseudo quantifiers''.
I have given them this name --- which is, by the way, not standard whatsoever --- because they are just constructions that help us quantify over things without being proper quantifiers.
Instead, they are mere logical artefacts that are limited to a certain kind of theories.

Some theories (in fact, most theories) define a predicate $E$ that is meant to represent equality. In these theories, we can define a predicate $(\exists! x)P(x)$ meaning ``there exists a unique $x$ verifying $P(x)$''. The equivalent real predicate behind $(\exists! x)P(x)$ would be
\[(\exists x)(\forall y) (P(x) \land (P(y)\limplies E(x,y))),\]
or, in English, ``there exists an $x$ verifying $P(x)$ and such that, if any $y$ verifies $P(y)$, then $y$ is equal to $x$''.

On some occasions, one may wish to quantify only over the set of elements in the domain that verify a certain predicate $P$.
This is very easy to do. If we wanted to restrict the quantification of $(\forall x)Q(x)$ or $(\exists x)Q(x)$ only to the elements $x$ verifying $P(x)$, we would just need
\[ (\forall x)(P(x)\limplies Q(x)) \quad \tx{and} \quad  (\exists x)(P(x) \land Q(x))\]
respectively.
You see, saying ``for all $x$ verifying $P(x)$, $Q(x)$ is true'' is the same as saying ``for all $x$, if $x$ verifies $P(x)$, then $Q(x)$ is true'', and analogously for the existential quantifier.

Many theories include binary predicates $P(x,y)$ that can be written as $xPy$ --- for instance, the inclusion predicate $\in$ that we studied in our elementary treatment of set theory, --- and, given a fixed $y$, one may wish to quantify over all the elements $x$ verifying $xPy$.
Of course, one could write $(\forall x)(xPy\limplies Q(x))$ or $(\exists x)(xPy\land Q(x))$, but, instead of wasting time and ink with such lengthy expressions, it is common to simply use $(\forall x P y)Q(x)$ and $(\exists x P y) Q(x)$.
Thus, if we wanted to postulate the existence of an element $X$ inside a set $Y$ verifying a property $P(x)$, we would simply have to write $(\exists X \in Y)P(X)$.
It is important to keep in mind that this is just shorthand notation, and we should always understand what is really going on.

Just to finish with this, I have an innocent question for you: let us assume that we are in the context of set theory and, given a set $X$ and a predicate $P(x)$, we formulate the sentence $(\exists! x \in X) P(x)$.
Is this expression well-formed and unambiguous? Take your time to think about it.

[Spoiler alert] Turns out that this expression is ambiguous. What do you mean: that there exists an element $x\in X$ verifying $P(x)$ that is unique among all the elements, or unique among all the elements in $X$? As far as I know, there is no widespread convention on which of these two possible interpretations is correct, so it is better not to use this construction to avoid ambiguity.
If you wanted to say that it is unique among all the elements of $X$, you could write $(\exists! x)(x\in X \land P(x))$. If, on the other hand, you meant that it is unique amongst all the elements, you could write $(\exists x\in X)(P(x)\land (\forall y)(P(y)\limplies y =x))$.
\end{para}

\begin{para}[Higher-order logic]
Predicate logic is also known as \emph{first-order logic}. Higher-order logic is an extension of first-order logic that not only allows quantification over variables, but also over predicates about variables, over predicates about predicates about variables, and so on.

For example, the statement ``for every property $P$, there exists an element $x$ such that $P(x)$ is true'' would be a statement in second-order logic.
\end{para}

\begin{para}[Mathematical induction]
And now, let us close this chapter with a fundamental tool that we will be using extensively from now on, the principle of mathematical induction.
What this principle states is the following: if a subset $A$ of the natural numbers contains $1$ and, for every $n \in A$, $n+1 \in A$, then $A$ is the set of natural numbers.
This is kind of trivial when you think about it.
We already know that $A \subseteq \mathbb{N}$ by hypothesis, so, in order to prove that $A = \mathbb{N}$, we just need to show that $\mathbb{N} \subseteq A$.
Thus, let $n\in\mathbb{N}$ and let us prove that $n\in A$.
We know, by hypothesis that $1 \in A$ and --- since, for every $n\in A$, $n+1\in A$, --- then $1 + 1 \in A$ and, therefore, $1 + 1 + 1 \in A$. If we apply this reasoning $n$ times, we are led to
\[ n = 1 + \overset{n}{\cdots} + 1 \in A.\]

And how does this relate to predicates?
Well, let us say that we want to show that a predicate $P(n)$ is true for all natural numbers $n$.
Let $A$ be the set of natural numbers $m$ such that $P(m)$ is true.
If we show that $1 \in A$ and that, given any $m\in A$, $m+1 \in A$, we will have shown that $A = \mathbb{N}$.
In other words, if we show that $P(1)$ is true and that, assuming $P(n)$ to be true, $P(n+1)$ is true, then we will know that the predicate is true for all natural numbers.

This principle can be extended to what is known as \emph{strong induction} (we will refer to it as induction too).
If, given a subset $A\subseteq \mathbb{N}$, we know that $1\in A$ and that, assuming every natural number $i$ such that $0\leq i \leq n$ to belong to $A$, we have $n+1 \in A$, then $A = \mathbb{N}$.
The reasoning that justifies this is essentially the same as that for normal induction.
Of course, strong induction can also be applied to show that a certain property holds for every natural number.
\end{para}


\section{Integration}
\subsection{The Riemann Integral}
Our goal is the following: for a ``sufficiently nice'' function $f:[a,b]\to\mathbb R$ we want to define its integral
$$\int_a^bf(x)\,\mathrm dx$$
We would like to think of this as the area under the graph of $y=f(x)$, which motivates the Riemann integral.
\begin{definition}
    We say $f:[a,b]\in\mathbb R$ is bounded if $\exists M\in\mathbb R,|f(x)|\le M$ for all $x\in [a,b]$.
\end{definition}
\begin{definition}
    A dissection of the interval $[a,b]$ is a finite subset $D\subset [a,b]$ such that $a,b\in D$.
    If $D$ is a dissection, we can write $D=\{a_0,\ldots,a_n\}$ where $a=a_0<a_1<\cdots<a_n=b$.
\end{definition}
Note that if $D,D'$ are both dissections, so is $D\cup D'$.
\begin{definition}
    If $f:[a,b]\to\mathbb R$ is bounded and a dissection $D$ consists of $a=a_0<a_1<\cdots<a_n=b$, then we define the upper and lower Riemann sums wrt $D$ by
    $$U(f,D)=\sum_{i=1}^n(a_i-a_{i-1})\sup_{a_{i-1}<x<a_i}f(x),L(f,D)=\sum_{i=1}^n(a_i-a_{i-1})\inf_{a_{i-1}<x<a_i}f(x)$$
    respectively.
    They exists as $f$ is bounded.
\end{definition}
Since $f$ is bounded, $U,L$ are bounded over all dissections $D$.
\begin{lemma}
    If $D\subset D'$ are dissections of $[a,b]$, then $U(f,D)\ge U(f,D')$ and $L(f,D)\le L(f,D')$.
\end{lemma}
\begin{proof}
    Obvious.
\end{proof}
\begin{lemma}
    If $D_1,D_2$ are dissections of $[a,b]$, then $U(f,D_1)\ge L(f,D_2)$.
\end{lemma}
\begin{proof}
    Trivial.
\end{proof}
Consider the nonempty sets
$$U=\{U(f,D):D\text{ dissects }[a,b]\},L=\{L(f,D):D\text{ dissects }[a,b]\}$$
It then follows by the preceding lemma that $U$ is bounded below and $L$ is bounded above, so
\begin{definition}
    We define
    $$U(f)=\inf U=\inf_{D\text{ dissects }[a,b]}U(f,D),L(f)=\sup L=\sup_{D\text{ dissects }[a,b]}L(f,D)$$
    as the upper and lowe Riemann sums of $f$ over $[a,b]$.
\end{definition}
\begin{lemma}
    $U(f)\ge L(f)$.
\end{lemma}
\begin{proof}
    Follows from definition.
\end{proof}
So we will want to define
\begin{definition}
    If $L(f)=U(f)$, then we say $f$ is (Riemann) integrable on $[a,b]$ and write
    $$\int_a^bf(x)\,\mathrm dx=L(f)=U(f)$$
\end{definition}
\begin{example}
    1. Consider $f(x)=c$ for a constant $c$ for all $x\in [a,b]$, then consider $D=\{a,b\}$, then $U(f,D)=c(b-a)=L(f,D)$, hence $U(f)=c(b-a)=L(f)$, therefore $f$ is Riemann integrable and
    $$\int_a^bf(x)\,\mathrm dx=c(b-a)$$
    2. Take $f:[a,b]\to\mathbb R$ by $f(x)=0$ if $x\in\mathbb Q$ and $f(x)=1$ if $x\in\mathbb R\setminus\mathbb Q$.
    Note that if $a_{i-1}<a_i$, then $[a_{i-1},a_i]$ contains both rational and irrational numbers, so it follows that $U(f,D)=b-a$ and $L(f,D)=0$ for any dissection $D$ of $[a,b]$, therefore $U(f)=b-a\neq 0=L(f)$, hence $f$ is not Riemann integrable.
\end{example}
\subsection{Properties of the Integral}
\begin{proposition}
    $f:[a,b]\to\mathbb R$ is integrable on $[a,b]$ and
    $$\int_a^bf(x)\,\mathrm dx=I$$
    iff for every $\epsilon>0$, there is a dissection $D$ of $[a,b]$ with $U(f,D)< I+\epsilon$ and $L(f,D)>I-\epsilon$
\end{proposition}
\begin{proof}
    Immediate.
\end{proof}
\begin{proposition}
    $f:[a,b]\to\mathbb R$ is integrable on $[a,b]$ iff for any $\epsilon>0$, there is a dissection $D$ of $[a,b]$ such that $U(f,D)-L(f,D)<\epsilon$
\end{proposition}
\begin{proof}
    Consider $\inf_D U(f,D)-L(f,D)$.
\end{proof}
\begin{definition}
    If $D=\{a=a_0<a_1<\ldots<a_n=b\}$ is a dissection, then the mesh of $D$ is the difference $m(D)=\max\{a_i-a_{i-1}\}$.
\end{definition}
Observe that if $\epsilon>0$, then one can find dissection with mesh less than $\epsilon$.
\begin{theorem}
    If $f:[a,b]\to\mathbb R$ is increasing, then $f$ is integrable.
\end{theorem}
\begin{proof}
    Since $f$ is increasing, $f(a_i)\ge f(x)$ whenever $x\le a_i$.
    So
    $$\sup_{a_{i-1}<x<a_i}f(x)\le f(a_i),\inf_{a_{i-1}<x<a_i}f(x)\ge f(a_{i-1})$$
    Hence
    $$U(f,D)=\sum_{i=1}^n(a_i-a_{i-1})\sup_{a_{i-1}<x<a_i}f(x)\le\sum_{i=1}^n(a_i-a_{i-1})f(a_i)$$
    and
    $$L(f,D)=\sum_{i=1}^n(a_i-a_{i-1})\inf_{a_{i-1}<x<a_i}f(x)\ge\sum_{i=1}^n(a_i-a_{i-1})f(a_{i-1})$$
    So by substracting
    $$U(f,D)-L(f,D)\le \sum_{i=1}^n(a_i-a_{i-1})(f(a_i)-f(a_{i-1}))\le m(D)(f(b)-f(a))<\epsilon$$
    If $f(b)=f(a)$ then $f$ is constant and we know that it is integrable.
    Otherwise we take $D$ with $m(D)<\epsilon/(f(b)-f(a))$, then by the preceding lemma, $f$ is integrable on $[a,b]$.
\end{proof}
\begin{proposition}
    Suppose $f,g:[a,b]\in\mathbb R$ are integrable, then\\
    1. If $\lambda\in\mathbb R$, then $f+\lambda g$ is integrable and
    $$\int_a^bf(x)+\lambda g(x)\,\mathrm dx=\int_a^bf(x)\,\mathrm dx+\lambda\int_a^bg(x)\,\mathrm dx$$
    2. For any $c\in [a,b]$, $f|_{[a,c]},f|_{[c,b]}$ are integrable and
    $$\int_a^cf(x)\,\mathrm dx+\int_c^bf(x)\,\mathrm dx=\int_a^bf(x)\,\mathrm dx$$
    3. If $f\ge g$ on $[a,b]$, then
    $$\int_a^bf(x)\,\mathrm dx\ge\int_a^bg(x)\,\mathrm dx$$
    4. $|f|$ is integrable and
    $$\left|\int_a^bf(x)\,\mathrm dx\right|\le\int_a^b|f(x)|\,\mathrm dx$$
\end{proposition}
\begin{proof}
    Trivial but need patience.
\end{proof}
\subsection{Fundamental Theorem of Calculus}
Recall that continuous function on closed interval is bounded.
\begin{theorem}\label{cont_int}
    If $f:[a,b]\to\mathbb R$ is continuous, then $f$ is integrable.
\end{theorem}
\begin{lemma}
    Suppose $U(f,D)-L(f,D)>c>0$, then there is $a_i\in D$ such that $\exists x,y\in (a_i,a_{i+1})$ with $f(x)-f(y)\ge c/(b-a)$.
\end{lemma}
\begin{proof}
    Basically just pigeonhole principle.
\end{proof}
\begin{proof}[Proof of Theorem \ref{cont_int}]
    If $f$ is not integrable, then there is some $\epsilon>0$ such that $U(f,D)-L(f,D)>\epsilon$ for all dissections $D$ of $[a,b]$.
    Let $0<\alpha<\epsilon/(b-a)$.
    By the preceding lemma, if $D$ is a dissection of $[a,b]$, then there is some $a_i\in D$ such that $\exists x,y\in (a_i,a_{i+1}),f(x)-f(y)\ge\alpha$
    Consider the dissections $D_n$ with mesh $m(D_n)<1/n$, then we can find $a_i^{(n)}\in D_n$ and $x_n,y_n\in (a_i^{(n)},a_{i+1}^{(n)}),f(x_n)-f(y_n)\ge\alpha$.
    Since $m(D_n)<1/n$, we know that $|x_n-y_n|<1/n$, so $x_n-y_n\to 0$.
    Now by Bolzano-Weierstrass, we can find a converging subsequence $x_{n_k}\to x$ of $x_n$.
    Also $x\in [a,b]$ since $[a,b]$ is closed.
    Note also that since $x_n-y_n\to 0$, we have $y_{n_k}\to x$.
    But $f$ is continuous, so $0<\alpha\le f(x_{n_k})-f(y_{n_k})\to 0$, contradiction.
\end{proof}
\begin{theorem}[Fundamental Theorem of Calculus, Version 1]
    Suppose $f:[a,b]\to\mathbb R$ is continuous, then define
    $$G(x)=\int_a^xf(t)\,\mathrm dt$$
    for $x\in [a,b]$.
    Then $G$ is differentiable and $G^\prime=f$.
\end{theorem}
Note that given $x\in [a,b]$, since $f$ is continuous in $[a,b]$, then it is continuous and hence integrable in $[a,x]$, so $G(x)$ is always well-defined.
\begin{proof}
    We need to show that, as $h\to 0$,
    $$\frac{G(x+h)-G(x)}{h}\to f(x)$$
    for fixed $x\in[a,b]$.
    Assume for the moment that $h>0$, then
    $$G(x+h)-G(x)=\int_x^{x+h}f(t)\,\mathrm dt$$
    Given $\epsilon>0$, we can find $\delta>0$ such that $|f(t)-f(x)|<\epsilon$ whenever $|t-x|<\delta$.
    So whenever $0<h<\delta$, we have $f(x)-\epsilon<f(t)<f(x)+\epsilon$ for $t\in [x,x+h]$, so
    $$f(x)h-\epsilon h=\int_x^{x+h}f(x)-\epsilon\,\mathrm dt\le\int_{x}^{x+h}f(t)\,\mathrm dt\le\int_x^{x+h}f(x)-\epsilon\,\mathrm dt=f(x)h+\epsilon h$$
    hence
    $$\left|\frac{G(x+h)-G(x)}{h}-f(x)\right|\le\epsilon$$
    for $0<h<\delta$, hence
    $$\lim_{h\to 0^+}\frac{G(x+h)-G(x)}{h}=f(x)$$
    Using exactly the same way, we also have
    $$\lim_{h\to 0^-}\frac{G(x+h)-G(x)}{h}=f(x)$$
    Combining them gives the result.
\end{proof}
\begin{definition}
    If $F,f:[a,b]\to\mathbb R$ with $F^\prime=f$, we say $F$ is an antiderivative of $f$.
\end{definition}
So the theorem says every continuous function on closed interval has an antiderivative.
\begin{theorem}[Fundamental Theorem of Calculus, Version 2]
    If $f:[a,b]\to\mathbb R$ is continuous and $F:[a,b]\to\mathbb R$ is differentiable with $F^\prime=f$, then
    $$\int_a^bf(x)\,\mathrm dx=F(b)-F(a)$$
\end{theorem}
\begin{proof}
    Let
    $$G(x)=\int_a^xf(t)\,\mathrm dt$$
    Then $G$ is differentiable and $G^\prime=f=F^\prime$, so $(G-F)^\prime=0$, hence $G-F$ is constant hence and equals $G(a)-F(a)=-F(a)$.
    Hence
    $$\int_a^bf(x)\,\mathrm dx=G(b)=F(b)+G(b)-F(b)=F(b)-F(a)$$
    Which is what we want.
\end{proof}
Note that we have used here many theorems about the real numbers, like the mean value theorem.
\begin{corollary}
    Suppose $f:[a,b]\to\mathbb R$ is continuous and $g:[c,d]\to\mathbb R$ is $C^1$, and $g(c)=a, g(d)=b$, then
    $$\int_a^bf(t)\,\mathrm dt=\int_c^df(g(s))g^\prime(s)\,\mathrm ds$$
\end{corollary}
\begin{proof}
    Chain rule and FTC.
\end{proof}
\begin{corollary}[Integration by Part]
    Suppose $f,g:[a,b]\to\mathbb R$ are both $C^1$, then
    $$\int_a^bg^\prime(t)h(t)\,\mathrm dt=g(b)h(b)-g(a)h(a)-\int_a^bg(t)h^\prime(t)\,\mathrm dt$$
\end{corollary}
\begin{proof}
    Product rule and FTC.
\end{proof}
Recall that if $f:[a,b]\to\mathbb R$ is $C^k$, the $k^{th}$ Taylor polynomial centered at $a$ is
$$P_k(x)=\sum_{i=0}^k\frac{f^{(i)}(a)}{i!}(x-a)^i$$
\begin{theorem}[Integral Form of Taylor's Theorem]
    If $f:[a,b]\to\mathbb R$ is $C^k$ and $x\in[a,b]$, then $R_k(x)=f(x)-P_k(x)$ has
    $$R_k(x)=\int_a^x\frac{(x-t)^{k-1}}{(k-1)!}f^{(k)}(t)\,\mathrm dt$$
\end{theorem}
\begin{proof}
    Induction by using integration by part.
\end{proof}
\begin{corollary}
    If $f:[a,b]\to\mathbb R$ is $C_k$, then there is a constant $M\in\mathbb R$ such that $|f(x)-P_{k-1}(x)|\le M|x-a|^k$.
\end{corollary}
\begin{proof}
    Since $f$ is $C^k$, $|f^{(k)}|$ is bounded.
    The rest follows instantly.
\end{proof}
If $x>0,s\in\mathbb R$, we defined $x^s=\exp(s\log x)$, so $\mathrm dx^s/\mathrm dx=sx^{s-1}$.
Now consider $f(x)=(1+x)^s$.
It has Taylor polynomials (centered at $0$)
$$P_k(x)=\sum_{i=0}^k\binom{s}{i}x^i,\binom{s}{i}=\frac{1}{i!}\prod_{j=0}^{i-1}(s-j)$$
We write $P(x)$ to be the formal sum when $k\to\infty$.
By the ratio test, $P$ converges whenever $|x|<1$.
\begin{theorem}[Newton's Binomial Theorem]\label{gen_bin}
    For $x\in (-1,1)$, $P(x)$ converges to $(1+x)^s$.
\end{theorem}
\begin{lemma}
    $$k\binom{s}{k}=s\binom{s-1}{k-1},\binom{s}{k-1}+\binom{s}{k}=\binom{s+1}{k}$$
\end{lemma}
\begin{proof}
    Trivial.
\end{proof}
\begin{proof}[Proof of Theorem \ref{gen_bin}]
    By termwise differentiation,
    $$(1+x)P^\prime(x)=(1+x)\sum_{i=0}^\infty i\binom{s}{i}x^{i-1}=sP(x)$$
    by the preceding lemma.
    So consider $h(x)=(1+x)^{-s}P(x)$, so $h^\prime(x)=0$ by the differential equation we obtained above, hence $h$ is constant in $(-1,1)$, hence $h\equiv h(0)=1$.
    Therefore $P(x)=(1+x)^s$ for each $x\in (-1,1)$.
\end{proof}
\subsection{Improper Integral}
Our definition of integral
$$\int_a^bf(x)\,\mathrm dx$$
requires $a,b<\infty$ and $f$ is defined and bounded in $[a,b]$.
But we'd also like to think about things like
$$\int_1^\infty\frac{\mathrm dx}{x^2+1},\int_0^1\frac{\mathrm dx}{\sqrt{x}}$$
which are not defined in our previous definition of integral.
Suppose $f:[a,b)\to\mathbb R$ is continuous and $b\in\mathbb R\cup\{\infty\}$.
Then if $x\in [a,b)$, then
$$\int_a^xf(t)\,\mathrm dt$$
is well-defined since $f$ is continuous on $[a,x]$.
\begin{definition}
    If $f:[a,b)\to\mathbb R$ is continuous and
    $$\lim_{x\to b^-}\int_a^xf(t)\,\mathrm dt$$
    exists and equals to some $c\in\mathbb R$, we define
    $$\int_a^bf(t)\,\mathrm dt=c$$
    and say that this integral converges.
    If this limit does not exist, we say the integral diverges.\\
    Similarly, if $f:(a,b]\to\mathbb R$ is continuous and
    $$\lim_{x\to a^+}\int_x^bf(t)\,\mathrm dt$$
    exists and equals to $c$, then we define
    $$\int_a^bf(t)\,\mathrm dt=c$$
    And say that this integral converges.
    Otherwise we say this integral diverges.
\end{definition}
Note that by FTC this definition (whenever the integral converges) does coincide with our original definition of integrals.
\begin{example}
    Consider the integral
    $$\int_1^\infty t^{-s}\,\mathrm dt$$
    Then for $s>1$, then integral converges and equals $1/(s-1)$, and it diverges for $s\le 1$.
    Similarly
    $$\int_0^1 t^{-s}\,\mathrm dt$$
    converges iff $s<1$.
\end{example}
More generally,
\begin{definition}
    If $f:(a,b)\to\mathbb R$ is continuous, then choose $c\in (a,b)$, then if
    $$\int_a^cf(t)\,\mathrm dt,\int_c^bf(t)\,\mathrm dt$$
    both converge, then we say the integral of $f(t)$ over $(a,b)$ converges and
    $$\int_a^bf(t)\,\mathrm dt=\int_a^cf(t)\,\mathrm dt+\int_c^bf(t)\,\mathrm dt$$
\end{definition}
Note that this definition (both the convergence and the value of the integral) does not depend on the choice of $c$.
\begin{example}
    We have
    $$\int_{-\infty}^\infty\frac{\mathrm dx}{1+x^2}=\int_{-\infty}^0\frac{\mathrm dx}{1+x^2}+\int_{0}^\infty\frac{\mathrm dx}{1+x^2}=\pi$$
    But
    $$\int_{-\infty}^\infty t\,\mathrm dt$$
    does not converge since
    $$\int_0^\infty f(t)\,\mathrm dt$$
    does not converge.
    $$\int_0^\infty t^{-s}\,\mathrm dt=\int_0^1 t^{-s}\,\mathrm dt+\int_1^\infty t^{-s}\,\mathrm ds$$
    never converges for any value of $s$.
\end{example}
\begin{proposition}[The Integral Test]
    Suppose $f:[1,\infty)\to[0,\infty)$ is continuous and decreasing, then
    $$\sum_{n=1}^\infty f(n)$$
    converges iff
    $$\int_1^\infty f(t)\,\mathrm dt$$
    converges.
\end{proposition}
\begin{example}
    $\sum_nn^{-s}$ converges iff $s>1$ due to our discussion on the integral of $t^{-s}$ over $[1,\infty)$.
\end{example}
\begin{proof}
    Since $f(x)\ge 0$,
    $$F(x)=\int_1^x f(t)\,\mathrm dt$$
    is increasing.
    Let $A=F([1,\infty))$, then if $A$ is bounded above iff the integral converges.
    Similarly since $F(n)\ge 0$, the sequence $s_n$ of partial sums is increasing, so the series converges iff $B=\{s_n\}$ is bounded above.
    Now let $D_n=\{1,2,\ldots,n\}$ be a dissection of $[1,n]$, then
    $$s_n-f(1)=L(f,D_n)\le F(n)\le U(f,D_n)=s_n-f(n)\le s_n-c$$
    where $c=\inf_x f(x)$ (exists as $f$ is bounded below by $0$) since $f$ is decreasing.
    This implies the claim.
\end{proof}
\begin{remark}
    As we see in the proposition, improper integrals behave like series in a certain sense.
    We also have a comparison test for improper integrals.
    If $f,g:[a,b)\to\mathbb R$ satisfies $|g|\le f$ and
    $$\int_a^bf(t)\,\mathrm dt$$
    converges, then
    $$\int_a^bg(t)\,\mathrm dt$$
    converges.
\end{remark}
Using improper integrals, we can define certain interesting functions.
\begin{definition}
    The gamma function $\Gamma:(0,\infty)\to\mathbb R$ is defined by
    $$\Gamma(s)=\int_0^\infty x^{s-1}e^{-x}\,\mathrm dx$$
\end{definition}
Note that the integral always converges by whatever triviality, so this function is always well-defined.
Note also that by integration by part we know that for $s>1$, we have $\Gamma(s+1)=s\Gamma(s)$, hence $\Gamma(n+1)=n!$ for $n\in\mathbb Z_{\ge 1}$.\\
One of the motivation of defining this gamma function is because of the following (here many informal arguments are used):
Now consider an $n$-dimensional sphere $S^{n-1}=\{v\in\mathbb R^n:|v|=1\}$.
Let $V(S^{n-1})$ be the volume (or area, etc.) of $S^{n-1}$, so for example $V(S_1)=2\pi$.
Observe that
$$\int_{\mathbb R^n}f(|v|)\,\mathrm dv=\int_0^\infty V(S^{n-1})r^{n-1}f(r)\,\mathrm dr$$
Now consider $f(r)=e^{-r^2}$ and let $c_n$ be the integral as above, then
$$c_n=\prod_{i=1}^n\int_\mathbb Re^{-x^2}\,\mathrm dx=(c_1)^n$$
But on the other hand $c_n$ equals to
$$\int_0^\infty V(S^{n-1})r^{n-1}e^{-r^2}\,\mathrm dr=\frac{V(S^{n-1})}{2}\Gamma\left( \frac{n}{2} \right)$$
So
$$V(S^{n-1})=\frac{2c_1^n}{\Gamma(n/2)}$$
We know that $c_1=\sqrt{\pi}$ since we know $V(S_1)=2\pi$, and this gives the formula.


\documentclass[12pt,a4paper]{article}

\usepackage{polyglossia}
\setdefaultlanguage{english}

\usepackage{fontspec}
\setmainfont{Gentium Basic}

%\linespread{1.3}
\setlength{\parindent}{0em}
\setlength{\parskip}{5pt}

\usepackage{amssymb}
\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{mathabx}
\usepackage{eulervm}

% theorem, definition, remark
\theoremstyle{plain}
\newtheorem{mytheo} {Theorem}

\begin{document}

\title{Cycle detection problem}
\author{}
\date{}
\maketitle

\section{The problem}

Let function $ f $ maps finite set $ S $ to itself:
\begin{equation}
f: S \to S
\end{equation}

Choose initial value $ a \in S $ and build a sequence by applying $ f $ iteratively:
\begin{gather}
x_0 = a \\
x_{i+1} = f(x_i), \quad i = 0, 1, \ldots
\end{gather}

Since $ S $ is finite the sequence gets back to the older value:
\begin{equation}
x_{\nu} = x_{\mu} \quad (\nu > \mu)
\end{equation}
and then cyclically repeats values $ x_{\mu}, \ldots, x_{\nu-1} $.

Mathematical details are presented below. The problem in question is to find loop parameters: cycle start index $ \mu $ and its length $ \lambda $.

\section{Mathematical analysis}

Let $ \nu $ be \emph{the largest} index such that values

\begin{equation}
x_0, x_1, \ldots, x_{\nu-1}
\end{equation}
are all different. This means the value $ x_{\nu} $ already appeared in the sequence before, at some index $ \mu < \nu $:

\begin{equation}
x_{\nu} = x_{\mu}
\end{equation}

Let
\begin{equation}
\lambda = \nu - \mu
\end{equation}

By applying function $ f $ to both sides of equation
\begin{gather}
x_{\mu + \lambda} = x_{\mu}
\end{gather}

we have
\begin{gather}
x_{\mu + \lambda + 1} = x_{\mu + 1}
\end{gather}

Apply function $ f $ again:
\begin{gather}
x_{\mu + \lambda + 2} = x_{\mu + 2}
\end{gather}

By induction we conclude
\begin{equation}\label{eq:period}
x_{i + \lambda} = x_{i}
\end{equation}
for any index $ i \geq \mu $.

For future reference let us prove the following statement:

\begin{mytheo}
Given two indices $ i < j $ we have: $ x_i = x_j $ if and only if

\begin{equation}
i \geq \mu
\end{equation}
and
\begin{equation}
\lambda \, | \, j - i
\end{equation}

\end{mytheo}

\section{Floyd's hare and tortoise algorithm}

\section{Brent's algorithm}

\end{document}


\chapter{Rings and ideals}
\section{Some motivational metaphors about rings vs groups}
In this chapter we'll introduce the notion of
a \textbf{commutative ring} $R$.
It is a larger structure than a group:
it will have two operations addition and multiplication,
rather than just one.
We will then immediately define a \textbf{ring homomorphism}
$R \to S$ between pairs of rings.

This time, instead of having normal subgroups $H \normalin G$,
rings will instead have subsets $I \subseteq R$ called \textbf{ideals},
which are not themselves rings but satisfy some niceness conditions.
We will then show how you to define $R/I$,
in analogy to $G/H$ as before.
Finally, like with groups, we will talk a bit about how to generate ideals.

Here is a possibly helpful table of analogies to help you keep track:
\begin{center}
	\begin{tabular}[h]{lcc}
		& Group & Ring \\
		\hline
		Notation & $G$ & $R$ \\
		Operations & $\cdot$ & $+$, $\times$ \\
		Commutativity & only if abelian & for us, always \\
		Sub-structure & subgroup & (not discussed) \\
		Homomorphism & grp hom.\ $G \to H$ & ring hom.\ $R \to S$ \\
		Kernel & normal subgroup & ideal \\
		Quotient & $G/H$ & $R/I$ \\
	\end{tabular}
\end{center}

\section{(Optional) Pedagogical notes on motivation}
I wrote most of these examples with a number theoretic eye in mind;
thus if you liked elementary number theory,
a lot of your intuition will carry over.
Basically, we'll try to generalize properties of the ring $\ZZ$ to
any abelian structure in which we can also multiply.
That's why, for example, you can talk about
``irreducible polynomials in $\QQ[x]$'' in the same
way you can talk about ``primes in $\ZZ$'',
or about ``factoring polynomials modulo $p$''
in the same way we can talk ``unique factorization in $\ZZ$''.
Even if you only care about $\ZZ$
(say, you're a number theorist), this has a lot of value:
I assure you that trying to solve $x^n+y^n = z^n$ (for $n > 2$)
requires going into a ring other than $\ZZ$!

Thus for all the sections that follow, keep $\ZZ$ in mind as your prototype.

I mention this here because
commutative algebra is \emph{also} closely tied to algebraic geometry.
Lots of the ideas in commutative algebra have nice
``geometric'' interpretations that motivate the definitions,
and these connections are explored in the corresponding part later.
So, I want to admit outright that this is not
the only good way (perhaps not even the most natural one)
of motivating what is to follow.

\section{Definition and examples of rings}
\prototype{$\ZZ$ all the way! Also $R[x]$ and various fields (next section).}

Well, I guess I'll define a ring\footnote{Or,
	according to some authors, a ``ring with identity'';
	some authors don't require rings to have multiplicative identity.
	For us, ``ring'' always means ``ring with $1$''.}.

\begin{definition}
	A \vocab{ring} is a triple $(R, +, \times)$,
	the two operations usually called addition and multiplication, such that
	\begin{enumerate}[(i)]
		\ii $(R,+)$ is an abelian group, with identity $0_R$, or just $0$.
		\ii $\times$ is an associative, binary operation on $R$ with some
		identity, written $1_R$ or just $1$.
		\ii Multiplication distributes over addition.
	\end{enumerate}
	The ring $R$ is \vocab{commutative} if $\times$ is commutative.
\end{definition}
\begin{abuse}
	As usual, we will abbreviate $(R, +, \times)$ to $R$.
\end{abuse}
\begin{abuse}
	For simplicity, assume all rings are commutative
	for the rest of this chapter.
	We'll run into some noncommutative rings eventually,
	but for such rings we won't need the full theory of this chapter anyways.
\end{abuse}

These definitions are just here for completeness.
The examples are much more important.
\begin{example}[Typical rings]
	\listhack
	\begin{enumerate}[(a)]
		\ii The sets $\ZZ$, $\QQ$, $\RR$ and $\CC$ are all rings
		with the usual addition and multiplication.
		\ii The integers modulo $n$ are also a ring
		with the usual addition and multiplication.
		We also denote it by $\Zc n$.
	\end{enumerate}
\end{example}

Here is also a trivial example.
\begin{definition}
	The \vocab{zero ring} is the ring $R$ with a single element.
	We denote the zero ring by $0$.
	A ring is \vocab{nontrivial} if it is not the zero ring.
\end{definition}
\begin{exercise}
	[Comedic]
	Show that a ring is nontrivial if and only if $0_R \ne 1_R$.
\end{exercise}

Since I've defined this structure, I may as well state the obligatory facts about it.
\begin{fact}
	For any ring $R$ and $r \in R$, $r \cdot 0_R = 0_R$.
	Moreover, $r \cdot (-1_R) = -r$.
\end{fact}

Here are some more examples of rings.
\begin{example}
	[Product ring]
	\label{ex:product_ring}
	Given two rings $R$ and $S$ the \vocab{product ring},
	denoted $R \times S$, is defined as ordered pairs $(r,s)$
	with both operations done component-wise.
	For example, the Chinese remainder theorem says
	that \[ \Zc{15} \cong \Zc3 \times \Zc5 \]
	with the isomorphism $n \bmod{15} \mapsto (n \bmod 3, n \bmod 5)$.
\end{example}
\begin{remark}
	Equivalently, we can define $R \times S$ as the abelian group $R \oplus S$,
	and endow it with the multiplication where $r \cdot s = 0$
	for $r \in R$, $s \in S$.
\end{remark}
\begin{ques}
	Which $(r,s)$ is the identity element of the product ring $R \times S$?
\end{ques}

\begin{example}[Polynomial ring]
	Given any ring $R$,
	the \vocab{polynomial ring} $R[x]$ is defined as the set of polynomials
	with coefficients in $R$:
	\[ R[x] = \left\{ a_n x^n+a_{n-1}x^{n-1}+\dots+a_0
		\mid a_0, \dots, a_n \in R \right\}. \]
	This is pronounced ``$R$ adjoin $x$''.
	Addition and multiplication are done exactly in the way you would expect.
\end{example}
\begin{remark}
	[Digression on division]
	Happily, polynomial division also does what we expect:
	if $p \in R[x]$ is a polynomial, and $p(a) = 0$,
	then $(x-a)q(x) = p(x)$ for some polynomial $q$.
	Proof: do polynomial long division.

	With that, note the caveat that
	\[ x^2-1 \equiv (x-1)(x+1) \pmod 8 \]
	has \emph{four} roots $1$, $3$, $5$, $7$ in $\Zc8$.

	The problem is that $2 \cdot 4 = 0$ even though $2$ and $4$ are not zero;
	we call $2$ and $4$ \emph{zero divisors} for that reason.
	In an \emph{integral domain} (a ring without zero divisors),
	this pathology goes away,
	and just about everything you know about polynomials carries over.
	(I'll say this all again next section.)
\end{remark}
\begin{example}
	[Multi-variable polynomial ring]
	We can consider polynomials in $n$ variables with coefficients in $R$,
	denoted $R[x_1, \dots, x_n]$.
	(We can even adjoin infinitely many $x$'s if we like!)
\end{example}

\begin{example}
	[Gaussian integers are a ring]
	The \vocab{Gaussian integers} are the set of complex numbers
	with integer real and imaginary parts, that is
	\[ \ZZ[i] = \left\{ a+bi \mid a,b \in \ZZ \right\}. \]
\end{example}
\begin{abuse}
	[Liberal use of adjoinment]
	Careful readers will detect some abuse in notation here.
	$\ZZ[i]$ should officially be
	``integer-coefficient polynomials in a variable $i$''.
	However, it is understood from context that $i^2=-1$;
	and a polynomial in $i = \sqrt{-1}$ ``is'' a Gaussian integer.
\end{abuse}
\begin{example}
	[Cube root of $2$]
	As another example (using the same abuse of notation):
	\[ \ZZ[\sqrt[3]{2}] = \left\{ a + b\sqrt[3]{2} + c\sqrt[3]4
		\mid a,b,c \in \ZZ \right\}. \]
\end{example}

\section{Fields}
\prototype{$\QQ$ is a field, but $\ZZ$ is not.}

Although we won't need to know what a field is until next chapter,
they're so convenient for examples I will go ahead and introduce them now.

As you might already know, if the multiplication is invertible,
then we call the ring a field.
To be explicit, let me write the relevant definitions.

\begin{definition}
	\label{def:unit}
	A \vocab{unit} of a ring $R$
	is an element $u \in R$ which is invertible:
	for some $x \in R$ we have $ux = 1_R$.
\end{definition}

\begin{example}
	[Examples of units]
	\listhack
	\begin{enumerate}[(a)]
	\ii The units of $\ZZ$ are $\pm 1$,
	because these are the only things which ``divide $1$''
	(which is the reason for the name ``unit'').
	\ii On the other hand, in $\QQ$ everything is a unit (except $0$).
	For example, $\frac 35$ is a unit since
	$\frac 35 \cdot \frac 53 = 1$.
	\ii The Gaussian integers $\ZZ[i]$ have four units:
	$\pm 1$ and $\pm i$.
	\end{enumerate}
\end{example}

\begin{definition}
	A nontrivial (commutative) ring is a \vocab{field}
	when all its nonzero elements are units.
\end{definition}

Colloquially, we say that
\begin{moral}
	A field is a structure where you can add, subtract, multiply, and divide.
\end{moral}
Depending on context, they are often denoted
either $k$, $K$, $F$.

\begin{example}
	[First examples of fields]
	\listhack
	\begin{enumerate}[(a)]
		\ii $\QQ$, $\RR$, $\CC$ are fields,
		since the notion $\frac 1c$ makes sense in them.
		\ii If $p$ is a prime, then $\Zc p$ is a field,
		which we denote will usually denote by $\FF_p$.
	\end{enumerate}
	The trivial ring $0$ is \emph{not} considered a field,
	since we require fields to be nontrivial.
\end{example}

%\begin{remark}
%	You might say at this point that ``fields are nicer than rings'',
%	but as you'll see in this chapter, the conditions for
%	being a field are somehow ``too strong''.
%	To give an example of what I mean:
%	if you try to think about the concept of ``divisibility''
%	in $\ZZ$, you've stepped into the vast and bizarre realm of
%	number theory.  Try to do the same thing in $\QQ$ and you get nothing:
%	any nonzero $a$ ``divides'' any nonzero $b$
%	because $b = a \cdot \frac ba$.
%
%	I know at least one person who instead
%	thinks of this as an argument for why people
%	shouldn't care about number theory
%	(studying chaos rather than order).
%\end{remark}

\section{Homomorphisms}
\prototype{$\ZZ \to \Zc5$ by modding out by $5$.}
This section is going to go briskly --
it's the obvious generalization of all the stuff
we did with quotient groups.\footnote{I once found an
	abstract algebra textbook which teaches rings before groups.
	At the time I didn't understand why,
	but now I think I get it -- modding out by things in
	commutative rings is far more natural, and you can start talking
	about all the various flavors of rings and fields.
	You also have (in my opinion) more vivid first examples
	for rings than for groups.
	I actually sympathize a lot with this approach --- maybe I'll convert
	Napkin to follow it one day.}

First, we define a homomorphism and isomorphism.
\begin{definition}
	Let $R = (R, +_R, \times_R)$ and $S = (S, +_S, \times_S)$ be rings.
	A \vocab{ring homomorphism} is a map $\phi : R \to S$
	such that
	\begin{enumerate}[(i)]
		\ii $\phi(x +_R y) = \phi(x) +_S \phi(y)$ for each $x,y \in R$.
		\ii $\phi(x \times_R y) = \phi(x) \times_S \phi(y)$ for each $x,y \in R$.
		\ii $\phi(1_R) = 1_S$.
	\end{enumerate}
	If $\phi$ is a bijection then $\phi$ is an \vocab{isomorphism}
	and we say that rings $R$ and $S$ are \vocab{isomorphic}.
\end{definition}
Just what you would expect.
The only surprise is that we also demand $\phi(1_R)$ to go to $1_S$.
This condition is not extraneous:
consider the map $\ZZ \to \ZZ$ called ``multiply by zero''.
\begin{example}
	[Examples of homomorphisms]
	\listhack
	\begin{enumerate}[(a)]
		\ii The identity map, as always.
		\ii The map $\ZZ \to \Zc5$ modding out by $5$.
		\ii The map $\RR[x] \to \RR$ by $p(x) \mapsto p(0)$
		by taking the constant term.
		\ii For any ring $R$, there is a trivial ring homomorphism $R \to 0$.
	\end{enumerate}
\end{example}
\begin{example}
	[Non-examples of homomorphisms]
	Because we require $1_R$ to $1_S$, some maps that you
	might have thought were homomorphisms will fail.
	\begin{enumerate}[(a)]
		\ii The map $\ZZ \to \ZZ$ by $x \mapsto 2x$
		is not a ring homomomorphism.
		Aside from the fact it sends $1$ to $2$,
		it also does not preserve multiplication.

		\ii If $S$ is a nontrivial ring,
		the map $R \to S$ by $x \mapsto 0$ is not
		a ring homomorphism, even though it preserves multiplication.

		\ii There is no ring homomorphism $\Zc{2016} \to \ZZ$ at all.
	\end{enumerate}
	In particular, whereas for groups $G$ and $H$
	there was always a trivial group homomorphism sending
	everything in $G$ to $1_H$, this is not the case for rings.
\end{example}

\section{Ideals}
\prototype{The multiples of $5$ are an ideal of $\ZZ$.}
Now, just like we were able to mod out by groups,
we'd also like to define quotient rings.
So once again,
\begin{definition}
	The \vocab{kernel} of a ring homomorphism $\phi \colon R \to S$,
	denoted $\ker \phi$, is the set of $r \in R$ such that $\phi(r) = 0$.
\end{definition}

In group theory, we were able to characterize the ``normal'' subgroups by a few
obviously necessary conditions (namely, $gHg\inv = H$).
We can do the same thing for rings, and it's in fact easier because our operations are commutative.

First, note two obvious facts:
\begin{itemize}
	\ii If $\phi(x) = \phi(y) = 0$, then $\phi(x+y) = 0$ as well.
	So $\ker \phi$ should be closed under addition.
	\ii If $\phi(x) = 0$, then for any $r \in R$ we have
	$\phi(rx) = \phi(r)\phi(x) = 0$ too.
	So for $x \in \ker \phi$ and \emph{any} $r \in R$,
	we have $rx \in \ker\phi$.
\end{itemize}

A (nonempty) subset $I \subseteq R$ is called
an ideal if it satisfies these properties.
That is,
\begin{definition}
	A nonempty subset $I \subseteq R$ is an \vocab{ideal}
	if it is closed under addition, and for each $x \in I$,
	$rx \in I$ for all $r \in R$.
	It is \vocab{proper} if $I \neq R$.
\end{definition}

Note that in the second condition, $r$ need not be in $I$!
So this is stronger than merely saying $I$ is closed under multiplication.
\begin{remark}
	If $R$ is not commutative, we also need the condition $xr \in I$.
	That is, the ideal is \emph{two-sided}: it absorbs multiplication
	from both the left and the right.
	But since rings in Napkin are commutative
	we needn't worry with this distinction.
\end{remark}

\begin{example}
	[Prototypical example of an ideal]
	Consider the set $I = 5\ZZ = \{\dots,-10,-5,0,5,10,\dots\}$ as an ideal in $\ZZ$.
	We indeed see $I$ is the kernel of the ``take mod $5$'' homomorphism:
	\[ \ZZ \surjto \ZZ/5\ZZ. \]
	It's clearly closed under addition,
	but it absorbs multiplication from \emph{all} elements of $\ZZ$:
	given $15 \in I$, $999 \in \ZZ$, we get $15 \cdot 999 \in I$.
\end{example}

\begin{exercise}
	[Mandatory: fields have two ideals]
	If $K$ is a field, show that $K$ has exactly two ideals.
	What are they?
	\label{exer:field_ideal}
\end{exercise}

Now we claim that these conditions are sufficient.
More explicitly,
\begin{theorem}
	[Ring analog of normal subgroups]
	Let $R$ be a ring and $I \subsetneq R$.
	Then $I$ is the kernel of some homomorphism if and only if it's an ideal.
\end{theorem}
\begin{proof}
	It's quite similar to the proof for the normal subgroup thing,
	and you might try it yourself as an exercise.

	Obviously the conditions are necessary.
	To see they're sufficient, we \emph{define} a ring by ``cosets''
	\[ S = \left\{ r + I \mid r \in R \right\}. \]
	These are the equivalence where we say $r_1 \sim r_2$ if $r_1 - r_2 \in I$
	(think of this as taking ``mod $I$'').
	To see that these form a ring, we have to check that the addition
	and multiplication we put on them is well-defined.
	Specifically, we want to check that if $r_1 \sim s_1$ and $r_2 \sim s_2$,
	then $r_1 + r_2 \sim s_1 + s_2$ and $r_1r_2 \sim s_1s_2$.
	We actually already did the first part
	-- just think of $R$ and $S$ as abelian
	groups, forgetting for the moment that we can multiply.
	The multiplication is more interesting.
	\begin{exercise}
		[Recommended]
		Show that if $r_1 \sim s_1$ and $r_2 \sim s_2$, then $r_1r_2 \sim s_1s_2$.
		You will need to use the fact that $I$ absorbs multiplication
		from \emph{any} elements of $R$, not just those in $I$.
	\end{exercise}
	Anyways, since this addition and multiplication is well-defined there
	is now a surjective homomorphism $R \to S$ with kernel exactly $I$.
\end{proof}

\begin{definition}
	Given an ideal $I$, we define as above the \vocab{quotient ring}
	\[ R/I \defeq \left\{ r+I \mid r \in R \right\}. \]
	It's the ring of these equivalence classes.
	This ring is pronounced ``$R$ mod $I$''.
\end{definition}
\begin{example}[$\ZZ/5\ZZ$]
	The integers modulo $5$ formed by ``modding out additively by $5$''
	are the $\Zc 5$ we have already met.
\end{example}
But here's an important point:
just as we don't actually think of $\ZZ/5\ZZ$ as consisting of
$k + 5\ZZ$ for $k=0,\dots,4$,
we also don't really want to think about $R/I$ as elements $r+I$.
The better way to think about it is
\begin{moral}
	$R/I$ is the result when we declare that elements of $I$ are all zero;
	that is, we ``mod out by elements of $I$''.
\end{moral}
For example, modding out by $5\ZZ$ means that we consider
all elements in $\ZZ$ divisible by $5$ to be zero.
This gives you the usual modular arithmetic!

\begin{exercise}
	Earlier, we wrote $\ZZ[i]$ for the Gaussian integers,
	which was a slight abuse of notation.
	Convince yourself that this ring
	could instead be written as $\ZZ[x] / (x^2+1)$,
	if we wanted to be perfectly formal.
	(We will stick with $\ZZ[i]$ though --- it's more natural.)

	Figure out the analogous formalization of $\ZZ[\sqrt[3]{2}]$.
\end{exercise}

\section{Generating ideals}
\prototype{In $\ZZ$, the ideals are all of the form $(n)$.}

Let's give you some practice with ideals.

An important piece of intuition is that once an ideal
contains a unit, it contains $1$, and
thus must contain the entire ring.
That's why the notion of ``proper ideal''
is useful language.
To expand on that:
\begin{proposition}
	[Proper ideal $\iff$ no units]
	Let $R$ be a ring and $I \subseteq R$ an ideal.
	Then $I$ is proper (i.e.\ $I \ne R$)
	if and only if it contains no units of $R$.
\end{proposition}
\begin{proof}
	Suppose $I$ contains a unit $u$, i.e.\ an element $u$
	with an inverse $u\inv$.
	Then it contains $u \cdot u\inv = 1$, and thus $I = R$.
	Conversely, if $I$ contains no units, it is obviously proper.
\end{proof}
As a consequence, if $K$ is a field,
then its only ideals are $(0)$ and $K$
(this was \Cref{exer:field_ideal}).
So for our practice purposes, we'll be working with rings that aren't fields.

First practice: $\ZZ$.
\begin{exercise}
	Show that the only ideals of $\ZZ$ are precisely those
	sets of the form $n\ZZ$, where $n$ is a nonnegative integer.
\end{exercise}

Thus, while ideals of fields are not terribly interesting,
ideals of $\ZZ$ look eerily like elements of $\ZZ$.
Let's make this more precise.
\begin{definition}
	Let $R$ be a ring.
	The \vocab{ideal generated} by a set of elements $x_1, \dots, x_n \in R$
	is denoted by $I = (x_1, x_2, \dots, x_n)$
	and given by
	\[ I = \left\{ r_1 x_1 + \dots + r_n x_n \mid r_i \in R \right\}.  \]
	One can think of this as ``the smallest ideal containing all the $x_i$''.
\end{definition}

The analogy of putting the $\{x_i\}$ in a sealed box and shaking vigorously
kind of works here too.
\begin{remark}
	[Linear algebra digression]
	If you know linear algebra,
	you can summarize this as: an ideal is an $R$-module.
	The ideal $(x_1, \dots, x_n)$ is the submodule spanned by $x_1, \dots, x_n$.
\end{remark}

In particular, if $I = (x)$ then $I$ consists of exactly the
``multiples of $x$'', i.e.\ numbers of the form $rx$ for $r \in R$.
\begin{remark}
	We can also apply this definition to infinite generating sets,
	as long as only finitely many of the $r_i$ are not zero
	(since infinite sums don't make sense in general).
\end{remark}

\begin{example}[Examples of generated ideals]
	\listhack
	\begin{enumerate}[(a)]
		\ii As $(n) = n\ZZ$ for all $ \in \ZZ$,
		every ideal in $\ZZ$ is of the form $(n)$.
		\ii In $\ZZ[i]$, we have
		$(5) = \left\{ 5a + 5b i \mid a,b \in \ZZ \right\}$.
		\ii In $\ZZ[x]$, the ideal $(x)$ consists of polynomials
		with zero constant terms.
		\ii In $\ZZ[x,y]$, the ideal $(x,y)$ again consists
		of polynomials with zero constant terms.
		\ii In $\ZZ[x]$, the ideal $(x,5)$ consists of polynomials
		whose constant term is divisible by $5$.
	\end{enumerate}
\end{example}
\begin{ques}
	Please check that the set
	$I = \left\{ r_1 x_1 + \dots + r_n x_n \mid r_i \in R \right\}$
	is indeed always an ideal (closed under addition,
	and absorbs multiplication).
\end{ques}
Now suppose $I = (x_1, \dots, x_n)$.
What does $R/I$ look like?
According to what I said at the end of the last section,
it's what happens when we ``mod out'' by each of the elements $x_i$.
For example\dots
\begin{example}
	[Modding out by generated ideals]
	\listhack
	\begin{enumerate}[(a)]
		\ii Let $R = \ZZ$ and $I = (5)$. Then $R/I$ is literally
		$\ZZ/5\ZZ$, or the ``integers modulo $5$'':
		it is the result of declaring $5 = 0$.
		\ii Let $R = \ZZ[x]$ and $I = (x)$.
		Then $R/I$ means we send $x$ to zero; hence $R/I \cong \ZZ$
		as given any polynomial $p(x) \in R$,
		we simply get its constant term.
		\ii Let $R = \ZZ[x]$ again and now let $I = (x-3)$.
		Then $R/I$ should be thought of as the quotient when $x-3 \equiv 0$,
		that is, $x \equiv 3$.
		So given a polynomial $p(x)$ its image after
		we mod out should be thought of as $p(3)$.
		Again $R/I \cong \ZZ$, but in a different way.
		\ii Finally, let $I = (x-3,5)$.
		Then $R/I$ not only sends $x$ to three, but also $5$ to zero.
		So given $p \in R$, we get $p(3) \pmod 5$.
		Then $R/I \cong \ZZ/5\ZZ$.
	\end{enumerate}
\end{example}
\begin{remark}
	[Mod notation]
	By the way, given an ideal $I$ of a ring $R$, it's totally legit to write
	\[ x \equiv y \pmod I \]
	to mean that $x-y \in I$.
	Everything you learned about modular arithmetic carries over.
\end{remark}

\section{Principal ideal domains}
\prototype{$\ZZ$ is a PID, $\ZZ[x]$ is not.
$\CC[x]$ is a PID, $\CC[x,y]$ is not.}

What happens if we put multiple generators in an ideal,
like $(10,15) \subseteq \ZZ$?
Well, we have by definition that $(10,15)$ is given as a set by
\[ (10,15) \defeq \left\{ 10x + 15y \mid x,y \in \ZZ \right\}.  \]
If you're good at number theory you'll instantly
recognize that this as $5\ZZ = (5)$.
Surprise! In $\ZZ$, the ideal $(a,b)$ is exactly $\gcd(a,b) \ZZ$.
And that's exactly the reason you often see the GCD of two numbers denoted $(a,b)$.

We call such an ideal (one generated by a single element) a \vocab{principal ideal}.
So, in $\ZZ$, every ideal is principal.
But the same is not true in more general rings.
\begin{example}
	[A non-principal ideal]
	In $\ZZ[x]$, $I = (x,2015)$ is \emph{not} a principal ideal.

	For if $I = (f)$ for some polynomial $f \in I$
	then $f$ divides $x$ and $2015$.
	This can only occur if $f = \pm 1$,
	but then $I$ contains $\pm1$, which it does not.
\end{example}
A ring with the property that all its ideals
are principal is called a \vocab{principal ideal ring}.
We like this property because they effectively
let us take the ``greatest common factor''
in a similar way as the GCD in $\ZZ$.

In practice, we actually usually care about
so-called \textbf{principal ideal domains (PID's)}.
But we haven't defined what a domain is yet.
Nonetheless, all the examples below are actually PID's,
so we will go ahead and use this word for now,
and tell you what the additional condition is in the next chapter.

\begin{example}
	[Examples of PID's]
	To reiterate, for now you should just verify
	that these are principal ideal rings,
	even though we are using the word PID.
	\begin{enumerate}[(a)]
		\ii As we saw, $\ZZ$ is a PID.

		\ii As we also saw, $\ZZ[x]$ is not a PID,
		since $I = (x,2015)$ for example is not principal.

		\ii It turns out that for a field $k$
		the ring $k[x]$ is always a PID.
		For example, $\QQ[x]$, $\RR[x]$, $\CC[x]$ are PID's.

		If you want to try and prove this,
		first prove an analog of Bezout's lemma,
		which implies the result.

		\ii $\CC[x,y]$ is not a PID, because $(x,y)$
		is not principal.
	\end{enumerate}
\end{example}

\section{Noetherian rings}
\prototype{$\ZZ[x_1, x_2, \dots]$ is not Noetherian,
	but most reasonable rings are.
	In particular polynomial rings are.
	(Equivalently, only weirdos care about non-Noetherian rings).}

If it's too much to ask that an ideal is generated by \emph{one} element,
perhaps we can at least ask that our ideals
are generated by \emph{finitely many} elements.
Unfortunately, in certain weird rings this is also not the case.
\begin{example}
	[Non-Noetherian ring]
	Consider the ring $R = \ZZ[x_1, x_2, x_3, \dots]$
	which has \emph{infinitely} many free variables.
	Then the ideal $I = (x_1, x_2, \dots) \subseteq R$
	cannot be written with a finite generating set.
\end{example}
Nonetheless, most ``sane'' rings we work in
\emph{do} have the property that their ideals are finitely generated.
We now name such rings and give two equivalent definitions:
\begin{proposition}[The equvialent definitions of a Noetherian ring]
	For a ring $R$, the following are equivalent:
	\begin{enumerate}[(a)]
		\ii Every ideal $I$ of $R$ is finitely generated
		(i.e.\ can be written with a finite generating set).
		\ii There does \emph{not} exist an
		infinite ascending chain of ideals
		\[ I_1 \subsetneq I_2 \subsetneq I_3 \subsetneq \dots. \]
		The absence of such chains is often
		called the \vocab{ascending chain condition}.
	\end{enumerate}
	Such rings are called \vocab{Noetherian}.
\end{proposition}

\begin{example}
	[Non-Noetherian ring breaks ACC]
	In the ring $R = \ZZ[x_1, x_2, x_3, \dots]$ we have
	an infinite ascending chain
	\[ (x_1) \subsetneq (x_1, x_2) \subsetneq (x_1,x_2,x_3) \subsetneq \dots. \]
\end{example}
From the example, you can kind of see why the proposition is true:
from an infinitely generated ideal you can extract an ascending chain
by throwing elements in one at a time.
I'll leave the proof to you if you want to
do it.\footnote{On the other hand, every undergraduate
	class in this topic I've seen makes you do it as homework.
	Admittedly I haven't gone to that many such classes.}

\begin{ques}
	Why are fields Noetherian?
	Why are PID's (such as $\ZZ$) Noetherian?
\end{ques}

This leaves the question:
is our prototypical non-example of a PID,
$\ZZ[x]$, a Noetherian ring?
The answer is a glorious yes,
according to the celebrated Hilbert basis theorem.
\begin{theorem}[Hilbert basis theorem]
	Given a Noetherian ring $R$,
	the ring $R[x]$ is also Noetherian.
	Thus by induction, $R[x_1, x_2, \dots, x_n]$ is Noetherian
	for any integer $n$.
	\label{thm:hilbert_basis}
\end{theorem}
The proof of this theorem is really olympiad flavored,
so I couldn't possibly spoil it -- I've
left it as a problem at the end of this chapter.

Noetherian rings really shine in algebraic geometry,
and it's a bit hard for me to motivate them right now,
other than to say
``most rings you'll encounter are Noetherian''.
Please bear with me!

\section{\problemhead}

\begin{problem}
	The ring $R = \RR[x] / (x^2+1)$ is one that you've seen before.
	What is its name?
	\begin{hint}
		$R = \RR[i]$.
	\end{hint}
	\begin{sol}
		This is just $\RR[i] = \CC$.
		The isomorphism is given by $x \mapsto i$,
		which has kernel $(x^2+1)$.
	\end{sol}
\end{problem}

\begin{problem}
	Show that $\CC[x] / (x^2-x) \cong \CC \times \CC$.
	\begin{hint}
		The isomorphism is given by $x \mapsto (1,0)$
		and $1-x \mapsto (0,1)$.
	\end{hint}
\end{problem}

\begin{problem}
	In the ring $\ZZ$, let $I = (2016)$ and $J = (30)$.
	Show that $I \cap J$ is an ideal of $\ZZ$ and compute its elements.
\end{problem}

\begin{sproblem}
	\label{prob:inclusion_preserving}
	Let $R$ be a ring and $I$ an ideal.
	Find an inclusion-preserving bijection between
	\begin{itemize}
		\ii ideals of $R/I$, and
		\ii ideals of $R$ which contain $I$.
	\end{itemize}
\end{sproblem}

\begin{problem}
	Let $R$ be a ring.
	\begin{enumerate}[(a)]
		\ii Prove that there is exactly one ring homomorphism $\ZZ \to R$.
		\ii Prove that the number of ring homomorphisms
		$\ZZ[x] \to R$ is equal to the number of elements of $R$.
	\end{enumerate}
	\begin{hint}
		For (b) homomorphism is uniquely determined by the choice of $\psi(x) \in R$
	\end{hint}
\end{problem}

%\begin{problem}
%	[$\phi(1_R) = 1_S$ is really necessary]
%	Find two rings $R$ and $S$ and a \emph{nonzero} function
%	$f \colon R \to S$ such that
%	\begin{itemize}
%		\ii $f(x+y) = f(x) + f(y)$ for $x,y \in R$,
%		\ii $f(xy) = f(x) f(y)$ for $x,y \in R$,
%		\ii $f(1_R) \ne 1_S$ (i.e.\ $f$ is not a ring homomorphism).
%	\end{itemize}
%	\begin{hint}
%		Take $S = R \times R$ with $R$ nonzero.
%	\end{hint}
%	\begin{sol}
%		The map $R \to R \times R$ by $x \mapsto (x,0)$ will always work,
%		with $R$ nonzero.
%	\end{sol}
%\end{problem}

\begin{problem}
	\gim
	Prove the Hilbert basis theorem, \Cref{thm:hilbert_basis}.
\end{problem}

\begin{problem}
	[USA Team Selection Test 2016]
	Let $\FF_p$ denote the integers modulo a fixed prime number $p$.
	Define $\Psi \colon \FF_p[x] \to \FF_p[x]$ by
	\[ \Psi\left( \sum_{i=0}^n a_i x^i \right) = \sum_{i=0}^n a_i x^{p^i}. \]
	Let $S$ denote the image of $\Psi$.
	\begin{enumerate}[(a)]
		\ii Show that $S$ is a ring with addition
		given by polynomial addition,
		and multiplication given by \emph{function composition}.
		\ii Prove that $\Psi \colon \FF_p[x] \to S$
		is then a ring isomorphism.
	\end{enumerate}
\end{problem}

\begin{problem} % from Brian Chen
	\yod
	Let $A \subseteq B \subseteq C$ be rings.
	Suppose $C$ is a finitely generated $A$-module.
	Does it follow that $B$ is a finitely generated $A$-module?
	% Assume $A$ is Noetherian. Show that $B$ is finitely generated as an $A$-module.
	% Find a counterexample where $A$ is not Noetherian.
	\begin{hint}
		I think the result is true if you add the assumption $A$ is Noetherian,
		so look for trouble by picking $A$ not Noetherian.
	\end{hint}
	\begin{sol}
		Nope! Pick
		\begin{align*}
			A &= \ZZ[x_1, x_2, \dots] \\
			B &= \ZZ[x_1, x_2, \dots, \eps x_1, \eps x_2, \dots] \\
			C &= \ZZ[x_1, x_2, \dots, \eps].
		\end{align*}
		where $\eps \neq 0$ but $\eps^2 = 0$.
		I think the result is true if you add the assumption $A$ is Noetherian.
	\end{sol}
\end{problem}


\section{Model Description}
This module is a simulation environment module which simulates the analog voltage interface of a RW cluster.  The input is an array of voltages $V_{i}$.  The Reaction Wheel (RW) motor torque $u_{s_{i}}$ is evaluated using a linear mapping
\begin{equation}
u_{s_{i}} = V_{i} \gamma_{i} SF_{i} + b_{i}
\end{equation}
where $\gamma$ is constant value gain. SF is the scale factor error (i.e. a constant 1\% gain error SF = 1.01). $b$ is the bias term (i.e. a constant 1 Nm error on torque b = 1.)  The output of the module is an array of RW motor torques.  The deadband and saturation behavior of the RW speed is modeled inside the RW dynamics model.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% PROBLEM 1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section*{Problem 1}

Consider an infinite slab of material described by diffusion coefficient $D$ and macroscopic cross section $\Sigma_a$.
The material extends infinitely in two dimensions, but has a vacuum boundaries at $x=a$ and $x=0$.
At $x=0$ there is also a uniformly distributed source plane with strength $s''$ [neutrons per area].
Find the flux in this geometry.
(You may use the substitution $L = \sqrt{\frac{D}{\Sigma_a}}$.)


% -*- compile-command: "stack exec --package diagrams-lib --package diagrams-pgf --package diagrams-contrib --package diagrams-builder --package palette -- pdflatex --enable-write18 groups.tex" -*-
\documentclass{article}

\usepackage{hyperref}
\usepackage{url}
\usepackage{amsmath}

\usepackage[outputdir=diagrams, extension=pgf, backend=pgf, input]{diagrams-latex}
\usepackage{pgf}

\usepackage{graphicx}
\graphicspath{{images/}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\title{Operation tables}
\author{Brent Yorgey, \href{http://www.mathlesstraveled.com}{\texttt{mathlesstraveled.com}} \\ \raisebox{-0.4em}{\includegraphics[width=44px]{../CC-BY.png}} \href{http://creativecommons.org/licenses/by/4.0/}{\texttt{creativecommons.org/licenses/by/4.0/}}}

\begin{document}

\maketitle

\fontsize{16}{20}\selectfont

In this activity you're going to fill out some \emph{operation
  tables}.  Each table will have some items listed across the top and
side.  For a given operation, your job is to fill each square with the
result of that operation on the items in the corresponding row and
column.  For example, if we list $2$, $3$, and $5$ along the side and
top, and the operation is addition, then the operation table would
look like this:

\begin{center}
\begin{tabular}{l|lll}
$+$ & 2 & 3 & 5  \\ \hline
2   & 4 & 5 & 7  \\
3   & 5 & 6 & 8  \\
5   & 7 & 8 & 10
\end{tabular}
\end{center}

\section*{XOR}

Fill in each spot in the table with the XOR of the two Boolean values.

\begin{center}
\begin{tabular}{l|ll}
$\oplus$ & F & T \\ \hline
F        &   &   \\
T        &   &
\end{tabular}
\end{center}

\section*{Addition}

Fill in the table using addition.

\begin{center}
\begin{tabular}{l|lll}
$+$ & 0 & 1 & 2 \\ \hline
0        &   &   &   \\
1        &   &   &   \\
2        &   &   &
\end{tabular}
\end{center}

\section*{Addition modulo 3}

Fill in the table using \emph{addition modulo $3$}.  That is, imagine
that the number line ``wraps around'' back to $0$ when it gets to
$3$.  If you get an answer bigger than $3$, subtract $3$ from it until
you are back down under $3$.  For example, $2 +_3 2 = 1$ since $2 + 2
= 4$ and $4 - 3 = 1$.

\begin{center}
\begin{tabular}{l|lll}
$+_3$ & 0 & 1 & 2 \\ \hline
0        &   &   &   \\
1        &   &   &   \\
2        &   &   &
\end{tabular}
\end{center}

\section*{Addition modulo 5}

\begin{center}
\begin{tabular}{l|lllll}
$+_5$ & 0 & 1 & 2 & 3 & 4 \\ \hline
0        &   &   &   &   &   \\
1        &   &   &   &   &   \\
2        &   &   &   &   &   \\
3        &   &   &   &   &   \\
4        &   &   &   &   &
\end{tabular}
\end{center}

\section*{Max}

Fill in the table using the $\max$ operation, which returns the
largest of its two inputs.

\begin{center}
\begin{tabular}{l|lll}
$\max$ & 0 & 1 & 2 \\ \hline
0      &   &   &   \\
1      &   &   &   \\
2      &   &   &
\end{tabular}
\end{center}

\newpage

\section*{Multiplication modulo 5}

Multiplication modulo $5$ works just like addition modulo $5$, but
with multiplication: to compute $a \times_5 b$, first multiply $a$ and
$b$, then keep subtracting $5$ until you get something smaller than
$5$.  For example, $3 \times_5 4 = 2$ since $3 \times 4 = 12$ and $12
- 5 - 5 = 2$.

\begin{center}
\begin{tabular}{l|llll}
$\times_5$ & 1 & 2 & 3 & 4 \\ \hline
1          &   &   &   &   \\
2          &   &   &   &   \\
3          &   &   &   &   \\
4          &   &   &   &
\end{tabular}
\end{center}

\newpage

Cut out the square on this page.  Ideally, you will also be able to
see the letter F through the back of the paper.  If you can't see it
very well you are welcome to draw in a backwards F on the back that
matches up with the F on the front. \vspace{1in}

\begin{center}
\begin{diagram}[width=300]
dia = text "F" # fontSizeL 0.25 <> square 1
\end{diagram}
\end{center}

\newpage

\newcommand{\elt}[1]{\raisebox{-0.25em}{\includegraphics[height=1.2em]{#1.png}}}

Let's list all the different things we can do to the square so that it
ends up still being a square in the same orientation (that is, we want
to list all the \emph{symmetries} of the square).  Each icon on the
next page represents an operation we can do to the square.  For
example, the \elt{dot} in the first row means to do nothing; the \elt{r1} in
the second row means to rotate the square by 1/4 turn, and so on.  On
the right of the colon is a graphical depiction of the operation.  If
you start your square with the letter F facing you and perform the
operation, it should end up looking like the square at the end.  For
example, the row for \elt{r1} looks like \elt{r1row}, meaning that if
you start with \elt{f} and perform the \elt{r1} operation, the square
ends up looking like \elt{r1f}.  You should physically do all the
operations with your square to make sure you understand how they work!

\begin{center}
  \includegraphics[width=3in]{d4.png}
\end{center}

\newpage

We can also \emph{combine} two operations on the square by doing first
one and then the other.  In the end this will have the same effect as
if we had done a single operation.  For example, flipping the square
vertically and then flipping it vertically again will put it back the
way it started, so

\begin{center}
  \includegraphics[height=0.5in]{vv.png}
\end{center}

As another example,

\begin{center}
  \includegraphics[height=0.5in]{dh.png}
\end{center}

(Try this with your physical square and make sure you understand it!)

\newpage

Fill in the table below, using the combining operation explained on
the previous page.

\begin{center}
  \includegraphics[width=5in]{table.png}
\end{center}

What patterns do you notice?

\end{document}


\subsection{Subdifferentials}\label{subsec:subdifferentials}

Let \( X \) be a Hausdorff \hyperref[def:topological_vector_space]{topological vector space}, let \( D \subseteq X \) be an open set and \( f: D \to \BbbR \) be any function.

\begin{definition}\label{def:subdifferentials}
  We fix a point \( x \in D \). We define different types of \term{subgradients} and \term{subdifferentials}. Subgradients are linear functionals \( x^* \in X^* \) that approximate \( f \) at the point \( x \) in a certain way, and a subdifferential is the set of all subgradients of a given type.

  \begin{thmenum}
    \thmitem{def:subdifferentials/convex}\mcite[59]{Clarke2013}We say that \( x^* \in X^* \) is a \term{subgradient of \( f \) at \( x \)} if for every \( y \in D \) we have
    \begin{equation*}
      f(y) - f(x) \geq \inprod {x^*} {y - x}.
    \end{equation*}

    The \term{subdifferential of \( f \) at \( x \)} is denoted by \( \partial f(x) \) and is also sometimes called the \term{convex subdifferential} because of \fullref{thm:convex_iff_subdifferential_nonempty}.

    \thmitem{def:subdifferentials/clarke}\mcite[def. 10.3]{Clarke2013}We say that \( x^* \in X^* \) is a \term{Clarke (generalized) subgradient of \( f \) at \( x \)} if for every direction \( h \in X \) we have
    \begin{equation*}
      f^\circ(x)(h) \geq \inprod {x^*} h,
    \end{equation*}
    where \( f^\circ(x)(h) \) is the generalized Clarke \hyperref[def:nonsmooth_derivatives/clarke]{derivative}.

    The \term{subdifferential of \( f \) at \( x \)} is denoted by \( \partial_C f(x) \). Confusingly, the Clarke subdifferential is called the \enquote{generalized gradient} by Clarke himself with no special name for the Clarke subgradients.

    See \fullref{subsec:clarke_gradients} for properties of these subgradients.

    \thmitem{def:subdifferentials/proximal}\mcite[227]{Clarke2013}We say that \( x^* \in X^* \) is a \term{proximal subgradient of \( f \) at \( x \)} if there exist \( \sigma > 0 \) and a neighborhood \( V \subseteq X \) of \( x \) such that for every \( y \in D \cap V \) we have
    \begin{equation*}
      f(y) - f(x) + \sigma \norm{y - x}^2 \geq \inprod {x^*} {y - x}.
    \end{equation*}

    The \term{proximal subdifferential of \( f \) at \( x \)} is denoted by \( \partial_P f(x) \).

    \thmitem{def:subdifferentials/limiting}\mcite[def. 11.10]{Clarke2013}Suppose the following are satisfied:
    \begin{enumerate}
      \item \( \{ x_n \}_n \subseteq D \) is a sequence of points converging to \( x \)
      \item \( f(x_n) \to f(x) \) (redundant if \( f \) is continuous)
      \item \( x_n^* \) is a proximal subgradient for \( f \) at \( x_n \) for every \( n \in \BbbZ_{>0} \).
    \end{enumerate}

    If the limit \( x^* \coloneqq \lim_n x_n^* \) exists and is a continuous linear functional, we call \( x^* \) a \term{limiting subgradient of \( f \) at \( x \)}.

    The \term{limiting subdifferential of \( f \) at \( x \)} is denoted by \( \partial_P f(x) \).
  \end{thmenum}
\end{definition}


\documentclass{article}
\usepackage{graphicx}
\usepackage{titletoc}
\usepackage{titlesec}
\usepackage{geometry}
\usepackage{fontspec, xunicode, xltxtra}
\usepackage{float}
\usepackage{cite}
\usepackage{amsmath}
\usepackage{listings}
\usepackage{titletoc}

\geometry{left=3cm,right=3cm,top=3cm,bottom=3cm}
\DeclareMathOperator*{\argmin}{argmin}
\DeclareMathOperator*{\argmax}{argmax}
\DeclareMathOperator*{\var}{var}
\DeclareMathOperator*{\expec}{E}

\begin{document}
\title{\textsf{Homework 2 for Pattern Recognition}}
\author{Fan JIN\quad (2015011506)}
\maketitle

\section*{Question 1}
{
    \subsection*{(1)}
    {
        The error function is minimized when optimized:
        $$\frac{\partial}{\partial w_0} E(w, w_0) = \sum_{i=1}^{n} {(w^T x_i + w_0 - t_i)} $$
        $$= n w^T m + nw_0 + n_1 \frac{n}{n_1} + n_2 \frac{-n}{n_2} = n ( w^T m + w_0 ) = 0,$$
        which yields $$w_0 = -w^T m$$.
    }

    \subsection*{(2)}
    {
        Denote the observation matrix and the response matrix as
        $$x = [x_1-m, x_2-m, \cdots, x_n-m]$$ and $$t = [t_1, t_2, \cdots, t_n].$$
        Plug in the optimal $w_0 = -w^T m$, and the error function can thus be expressed as
        $$E(w) = \frac{1}{2} (w^T x - t)(w^T x - t)^T.$$

        By matrix calculus\footnote{See https://en.wikipedia.org/wiki/Matrix\_calculus}, we have
        $$\frac{\partial}{\partial w} E(w) = -x (t^T - x^T w) = 0,$$
        that is, $$x x^T w = x t^T.$$

        Note that $$x t^T = \left[ \sum_{i=1}^{n} {(x_i t_i)} \right] - m \cdot \sum_{i=1}^{n} {x_i} $$
        $$= \left[ \left(n_1 \frac{n}{n_1} m_1 + n_2 \frac{-n}{n_2} m_2 \right) \right] - m \cdot \left( n_1 \frac{n}{n_1} + n_2 \frac{-n}{n_2} \right) = n (m_1 - m_2),$$ and
        $$m = \frac{n_1}{n}m_1 + \frac{n_2}{n}m_2 = m_1 - \frac{n_2}{n}(m_1-m_2) = m_2 + \frac{n_1}{n}(m_1-m_2),$$ we have
        $$x x^T = \sum_{i\in C_1}{(x_i - m_1 + \frac{n_2}{n}(m_1-m_2))(x_i - m_1 + \frac{n_2}{n}(m_1-m_2))^T} $$$$+ \sum_{i\in C_2}{(x_i - m_2 - \frac{n_1}{n}(m_1-m_2))(x_i - m_2 - \frac{n_1}{n}(m_1-m_2))^T}$$
        $$= \sum_{i\in C_1}{(x_i - m_1)(x_i - m_1)^T} + \sum_{i\in C_2}{(x_i - m_2)(x_i - m_2)^T} + \frac{n_1 n_2}{n^2} \sum_{i=1}^{n} {(m_1 - m_2)(m_1 - m_2)^T}$$
        $$= S_w + \frac{n_1 n_2}{n} S_B.$$

        Therefore, we proved that $$\left( S_w + \frac{n_1 n_2}{n} S_B \right) w = n (m_1 - m2)$$ when $w$ is optimal.

    }

    \subsection*{(3)}
    {
        Note that $$S_B w = (m_1 - m_2) (m_1 - m_2)^T w = (m_1 - m_2) \cdot \left[ (m_1 - m_2)^T w \right],$$ and that $(m_1 - m_2)^T w$ is a scalar. Therefore, the vector $S_B w$ is proportional to $m_1-m_2$, which means $$w \propto S_w^{-1} (m_1 - m_2).$$
    }
}

\section*{Question 2}
{
    \subsection*{Data Visualization}
    {
        Since the dimension of original data is high, we apply PCA (Principal Component Analysis) and extract the first two principal compenents for a scatter plot.

        \begin{figure}[H]
            \centering
            \includegraphics[width = 0.8\linewidth]{pca.png}
            \caption{Scatter plot}
        \end{figure}

        The data seems linearly separable, although there are a few outliers.
    }

    \subsection*{Logistic Regression}
    {
        I implement the algorithm on my own, using a loss function based on cross entropy
        $$L(\theta) = - \sum_{i} {\left[ y_i \log{(h_\theta (x_i))} + (1 - y_i) \log{(1 - h_\theta (x_i))}\right]},$$
        where $$h_\theta (x_i) = \frac{1}{1 + \exp{(- \theta^T x_i)}}.$$

        Gradient descent is employed to solve the optimal parameter $\theta$. A column of ones is attached to the data matrix, in order not to write the intercept in the expression.

        The error rate is $3.33\%$ on the validation set.

    }

    \subsection*{Fisher's Discriminant}
    {
        I implement the algorithm on my own, using the formula in Question 1.

        The error rate is $1.43\%$ on the validation set.

    }

    \subsection*{Discussion}
    {
        Why does the Fisher's method have better performance than logistic regression?

        Their main difference is their loss functions. Logistic regression uses the cross entropy, while the Fisher's method adopts the ordinary quadratic loss, or the sum of squared errors (SSE). With outliers in consideration, the cross entropy tends to punish more on outliers, compared to the SSE loss function, and therefore, is more likely to result in over fitting.

        Another reason is that the Fisher's method considers the variance of two categories. It can predict the data distribution well if the positive samples have a variance different from that of the negative samples.

    }
}

\section*{Source Code}
{
    Please download the souece code from http://39.106.23.58/files/PR2\_2015011506.7z
}

\clearpage
\end{document}


\subsection{Equivalence classes}

We have already ready defined the relationship equality, between terms.

\(a=b\).

Sometimes we may wish to talk about a collection of terms which are all equal to each other. This is an equivalence class.

Though we have not yet defined them, integers are example of this. For example \(-1\) can be written as \(0-1\), \(1-2\) and so on.

\(\forall y \forall x x=y\rightarrow x\in z\)

For all sets, we can call the class of all sets equal to the set an equivalence class.

This does not necessarily exist.


\documentclass{article}
\usepackage{amsmath}
\usepackage{amsfonts}

\title{Method of Moments for Mixture of Gaussians}
\author{Yao Zhu\thanks{yzhu221@bloomberg.net}\\ Derivatives Pricing, Bloomberg LP}
\date{}

\begin{document}
\maketitle

\section{Mixture of Spherical Gaussians with the same variance parameter $\sigma^2$}
For clarity of presenting the idea of method of moments, we consider the mixture of spherical Gaussians with the same covariance. Under this setting, a random variable $X\in R^{d}$ is generated as follows:
\begin{align}
p(Z=i) &= w_i
\label{eq-mog-gen}
\\
\nonumber
X|Z=i &\sim \mathcal{N}(\mu_i,\sigma^2 I_{d})
\end{align}
for $i=1,\ldots,k$, where $k$ is a known number and $k\leq d$, $I_d$ is the $d\times d$ identity matrix, and $\sigma^2$ is the variance parameter common to all $k$ components. We further assume the following non-degeneracy condition:
\par
\textbf{Non-degeneracy Condition}: $\mu_i,i=1,\ldots,k$ are linearly independent, and $w_i>0$ for all $i=1,\ldots,k$.
\par
We target to recover the parameters $\{w_i,\mu_i\}_{i=1}^k$ and $\sigma^2$ from a sample of data points $\{x_n\}_{n=1}^N$ generated according to \eqref{eq-mog-gen}.
\section{Raw Moments}
\subsection{Tensor product}
In order to work with moments (especially moments of order higher than $2$), we need to the notion of \textit{tensor product}, denoted by $\otimes$. Let $X_1,X_2,X_3\in R^{d}$, we define $X_1\otimes X_2\otimes X_3$ such that
\begin{align}
\left(X_1\otimes X_2\otimes X_3\right)_{jlm} &= (X_1)_{j}(X_2)_{l}(X_3)_{m}
\end{align}
for $j,l,m=1,\ldots, d$. In a similar style, we define $X_1\otimes X_2=X_1 X_2^{T}$.
\subsection{Moments of Mixture of Gaussians}
Given \eqref{eq-mog-gen}, when $Z=i$ we can write and
\begin{align}
X&=\mu_i + Y \\
\nonumber
Y&\sim \mathcal{N}(0,\sigma^2 I_{d})
\end{align}
We can compute the following raw moments up to order $3$
\begin{align}
\mathbb{E}[X]&=\sum_{i=1}^k w_i \mu_i
\end{align}
We will denote $M_1 = \mathbb{E}[X]$ in the following.
\begin{align}
\mathbb{E}[X\otimes X]&=\sum_{i=1}^k w_i \mathbb{E}[X\otimes X|Z=i]\\
&= \sum_{i=1}^k w_i \mathbb{E}[(\mu_i + Y)\otimes (\mu_i + Y)]\\
&=\sum_{i=1}^k w_i \mu_i\otimes \mu_i +  \sigma^2 I_d
\end{align}
Note we have used the fact that $\mathbb{E}[Y]=0$, and $\mathbb{E}[Y\otimes Y]=\sigma^2 I_d$. Plus the fact that $\mathbb{E}[Y\otimes Y\otimes Y]=0$, we compute the $3$rd order moment
\begin{align}
\mathbb{E}[X\otimes X \otimes X]&=\sum_{i=1}^k w_i \mathbb{E}[X\otimes X\otimes X|Z=i]\\
&= \sum_{i=1}^k w_i \mathbb{E}[(\mu_i + Y)\otimes (\mu_i + Y)\otimes (\mu_i + Y)]\\
&=\sum_{i=1}^k w_i \mu_i\otimes \mu_i\otimes \mu_i + \\
\nonumber
& \sum_{i=1}^k w_i\left( \mathbb{E}[\mu_i\otimes Y\otimes Y] + \mathbb{E}[ Y\otimes\mu_i\otimes Y] + \mathbb{E}[Y\otimes Y\otimes\mu_i]\right)
\end{align}
Now let's take a closer look at the term
\begin{align*}
\sum_{i=1}^k w_i  \mathbb{E}[ Y\otimes\mu_i\otimes Y] &= \mathbb{E}[ Y\otimes(\sum_{i=1}^k w_i\mu_i)\otimes Y]\\
&= \mathbb{E}[ Y\otimes M_1\otimes Y]
\end{align*}
In order to further simplify it, we look at a particular cell
\begin{align*}
\mathbb{E}[ (Y\otimes M_1\otimes Y)_{jlm}] &= (M_1)_l \mathbb{E}[ Y_j Y_m]\\
&= (M_1)_l \sigma^2 \delta_{jm}
\end{align*}
where $\delta_{jm}$ is the Kronecker delta. Thus, in tensor form we have
\begin{align}
\mathbb{E}[ Y\otimes M_1\otimes Y] &= \sigma^2\sum_{j=1}^d e_j \otimes M_1 \otimes e_j
\end{align}
where $\{e_1,\ldots,e_d\}$ is the canonical basis of $d$ dimension. Similarly we have
\begin{align}
\mathbb{E}[ M_1 \otimes Y\otimes Y] &= \sigma^2\sum_{j=1}^d  M_1 \otimes e_j \otimes e_j\\
\mathbb{E}[Y\otimes Y\otimes M_1] &= \sigma^2\sum_{j=1}^d   e_j \otimes e_j \otimes M_1
\end{align}
Thus, in summary we have
\begin{align}
\mathbb{E}[X\otimes X \otimes X]&=\sum_{i=1}^k w_i \mu_i\otimes \mu_i\otimes \mu_i +\\
\nonumber
&\sigma^2\sum_{j=1}^d  (M_1 \otimes e_j \otimes e_j + e_j \otimes M_1 \otimes e_j + e_j \otimes e_j \otimes M_1)
\end{align}
\section{Parameter identification from the moments}
From the data sample $\{x_n\}_{n=1}^N$, we can compute the empirical moments $\widetilde{\mathbb{E}}[X]$, $\widetilde{\mathbb{E}}[X\otimes X]$, and $\widetilde{\mathbb{E}}[X\otimes X \otimes X]$ as estimates of the theoretical moments. For this reason, we say $\mathbb{E}[X]$, $\mathbb{E}[X\otimes X]$, and $\mathbb{E}[X\otimes X \otimes X]$ are observable. We want to come up with a recipe to identify the parameters $\{w_i,\mu_i\}_{i=1}^k$ and $\sigma^2$ from these observable moments.
\subsection{Identify $\sigma^2$}
Let's compute the covariance matrix of $X$
\begin{align}
cov(X)&=\mathbb{E}[(X-M_1)\otimes (X-M_1)]\\
&=\sum_{i=1}^k w_i \mathbb{E}[(X-M_1)\otimes (X-M_1)|Z=i]\\
&=\sum_{i=1}^k w_i \mathbb{E}[(\mu_i-M_1+Y)\otimes (\mu_i-M_1+Y)]\\
&=\sum_{i=1}^k w_i (\mu_i-M_1)\otimes (\mu_i-M_1) + \sigma^2 I_d
\label{eq-cov-X}
\end{align}
Note that because $\sum_{i=1}^k w_i (\mu_i-M_1)=0$, the $k$ vectors $(\mu_i-M_1)$ for $i=1,\ldots,k$ are linearly dependent. Thus, from \eqref{eq-cov-X} we know $\sigma^2=\lambda_{\min}(cov(X))$, i.e., $\sigma^2$ is the smallest eigenvalue of $cov(X)$. Note because $cov(X)=\mathbb{E}[X\otimes X]-M_1\otimes M_1$, $cov(X)$ is also observable.
\subsection{Identify $\{w_i,\mu_i\}_{i=1}^k$}
We define the following two purified moments
\begin{align}
M_2&=\mathbb{E}[X\otimes X] - \sigma^2 I_d
\label{eq-M2-obs}
\\
&=\sum_{i=1}^k w_i \mu_i\otimes \mu_i
\label{eq-M2-tensor-decomp}
\end{align}
\begin{align}
M_3&=\mathbb{E}[X\otimes X\otimes X] - \sigma^2\sum_{j=1}^d  (M_1 \otimes e_j \otimes e_j + e_j \otimes M_1 \otimes e_j + e_j \otimes e_j \otimes M_1)
\label{eq-M3-obs}
\\
&=\sum_{i=1}^k w_i \mu_i\otimes \mu_i \otimes \mu_i
\label{eq-M3-tensor-decomp}
\end{align}
Once we have identified $\sigma^2$, $M_2$ is observable thanks to equation \eqref{eq-M2-obs}, and $M_3$ is observable thanks to equation \eqref{eq-M3-obs}. In the \textbf{Non-degeneracy Condition}, we only assume $\mu_i,i=1,\ldots,k$ to be linearly independent, which is not strong enough for us to extract $\mu_i$ directly through the tensor decomposition in equation \eqref{eq-M3-tensor-decomp}. We want to cook up another tensor that admits an \textit{orthogonal tensor decomposition}, on which we can apply the \textit{tensor power method}. From \eqref{eq-M2-tensor-decomp}, we see that $M_2$ is a symmetric positive semidefinite matrix with rank $k$. Thus, it admits a thin eigendecomposition
\begin{align}
M_2 &= U\Lambda U^{T}
\label{eq-M2-thin-eigendecomp}
\end{align}
where $U=(u_1,\ldots,u_k)\in R^{d\times k}$, and $\Lambda=diag(\lambda_1,\ldots,\lambda_k)$ is a diagonal matrix with $\lambda_i>0$ for $i=1,\ldots, k$.
Now let's define whitening matrix
\begin{align}
B&=U\Lambda^{-1/2}
\label{eq-whiten-matrix}
\end{align}
and the following whitened vectors
\begin{align}
\widehat{\mu_i}&=\sqrt{w_i}B^{T}\mu_i
\label{eq-whiten-vector}
\end{align}
Also note that because $\mu_i\in span(U)$, and $w_i>0$, we recover $\mu_i$ by
\begin{align}
\mu_i&=\frac{1}{\sqrt{w_i}}(B^{T})^{\dagger}\widehat{\mu_i}
\label{eq-whiten-vector-recover}
\end{align}
where $B^{T})^{\dagger}$ is the Moore-Penrose pseudoinverse on the right of $B^{T}$ such that $B^{T}(B^{T})^{\dagger}=I_k$. From the definition of \eqref{eq-whiten-matrix}, we have
\begin{align}
I_k&=B^{T}M_2 B=M_2(B,B)\\
&=\sum_{i=1}^k w_i (B^{T}\mu_i)\otimes (B^{T}\mu_i)\\
&=\sum_{i=1}^k (\sqrt{w_i}B^{T}\mu_i)\otimes (\sqrt{w_i}B^{T}\mu_i)\\
&=\sum_{i=1}^k \widehat{\mu_i}\otimes \widehat{\mu_i}
\end{align}
Thus, the vectors $\widehat{\mu_i},i=1,\ldots,k$ are orthogonal. Now we apply the whitening to $M_3$ as follows:
\begin{align}
M_3(B,B,B)&=\sum_{i=1}^k w_i (B^{T}\mu_i)\otimes (B^{T}\mu_i)\\
&=\sum_{i=1}^k \frac{1}{\sqrt{w_i}} \widehat{\mu_i}\otimes \widehat{\mu_i}\otimes \widehat{\mu_i}
\label{eq-M3-whiten-orth-tensor-decomp}
\end{align}
Thus, the whitened tensor $M_3(B,B,B)$ admits an orthogonal decomposition \eqref{eq-M3-whiten-orth-tensor-decomp}, which is the merit we need in order to apply the tensor power method for identifying $\widehat{\mu_i}$, for $i=1,\ldots,k$. We denote $M_3(B,B,B)=\widehat{M_3}$. The tensor power method is given by the following iteration
\begin{align}
\theta_{t+1} & \leftarrow \frac{\widehat{M_3}(:,\theta_{t}, \theta_{t})}{\|\widehat{M_3}(:,\theta_{t}, \theta_{t})\|}
\label{eq-tensor-power-iter}
\end{align}
starting from an initial random vector $\theta_0$ on the unit sphere $\mathcal{S}^{k}$. It can be proved that $\theta_t$ will converge to a certain eigenvector $\widehat{\mu_i}$ of $\widehat{M_3}$. Once we have an estimate of $\widehat{\mu_i}$, we can identify $w_i$ by
\begin{align}
\frac{1}{\sqrt{w_i}}&=\widehat{M_3}(\widehat{\mu_i},\widehat{\mu_i},\widehat{\mu_i})
\label{eq-wi-estimate}
\end{align}
Now we want to find another $\widehat{\mu_j}$ different from $\widehat{\mu_i}$. For this purpose, we need to \textit{deflate} $\widehat{\mu_i}$ from $\widehat{M_3}$. Let $\mathcal{I}$ be the index set such that $i\in \mathcal{I}$ if and only if $(\widehat{\mu_i}, w_i)$ have been identified. The deflation is defined by
\begin{align}
\widehat{M_3} &\leftarrow \widehat{M_3} - \sum_{i\in \mathcal{I}}\frac{1}{\sqrt{w_i}} \widehat{\mu_i}\otimes \widehat{\mu_i} \otimes \widehat{\mu_i}
\label{eq-deflation}
\end{align}
Please see~\cite{AnandkumarG2014} for the details of orthogonal tensor decomposition and the tensor power method.
\subsection{Recipe}
In summary, our recipe using the method of moments are as follows:
\begin{enumerate}
\item Compute the empirical moments explicitly $\widetilde{M_1}=\widetilde{\mathbb{E}}[X]$ and $\widetilde{\mathbb{E}}[X\otimes X]$.
\item Identify $\sigma^2$ by extracting the smallest eigenvalue of $\widetilde{\mathbb{E}}[X\otimes X]-\widetilde{M_1}\otimes \widetilde{M_1}$.
\item Form $M_2$ explicitly by \eqref{eq-M2-obs}, and do the thin eigendecomposition \eqref{eq-M2-thin-eigendecomp} to extract the whitening matrix $B$ in \eqref{eq-whiten-matrix}.
\item Start with $\mathcal{I}=\emptyset$. For $i=1,\ldots,k$, do the tensor power iteration \eqref{eq-tensor-power-iter} using the deflated version \eqref{eq-deflation} until converge (or maximum number of iterations met). We can estimate $w_i$ by \eqref{eq-wi-estimate}. Let $\mathcal{I}=\mathcal{I}\cup {i}$.
\par
Note because in the tensor power iteration, only the action $\widehat{M_3}(:,\theta_{t}, \theta_{t})$ is needed, we don't need to explicitly form $\widehat{M_3}$. Instead, from \eqref{eq-M3-obs}, we have
\begin{align}
\widehat{M_3}(:,\theta_{t}, \theta_{t}) &= \mathbb{E}[B^{T}X (\theta_t^{T}B^{T}X)^2] -\\
\nonumber
& \sigma^2\sum_{j=1}^d  \left(B^{T}M_1 (\theta_t^{T}B^{T}e_j)^2  +  2 B^{T}e_j (\theta_t^{T}B^{T}e_j)(\theta_t^{T}B^{T}M_1) \right)
\end{align}
\item Recover $\mu_i$ from $\widehat{\mu_i}$ by \eqref{eq-whiten-vector-recover}.
\end{enumerate}
\section{More general Gaussians}
\subsection{Differing $\sigma_i^2$ for $i=1,\ldots,k$}
The method presented above can be straightforwardly extended to the case where each mixture component has as different variance parameter $\sigma_i^2$, with some tweaks to the form of the observed moments $M_2$ and $M_3$. Please see~\cite{HsuK2013} for the details.
\subsection{General covariance matrices $\Sigma_i$}
Intuitively, when we have general covariance matrices $\Sigma_i$ for $i=1,\ldots,k$, we have many more parameters to estimate (each $\Sigma_i$ have $\frac{d(d+1)}{2}$ entries) than the case of spherical Gaussians. It turns out we need the $4$th and $6$th order moments in order to approximately recover $\Sigma_i$ (with the assumption that $d=O(k^2)$). The algorithm is much more complicated and non-trivial to implement, please see~\cite{GeHK2015} for the details.

\begin{thebibliography}{6}

\bibitem{AnandkumarG2014}A. Anandkumar and R. Ge and D. Hsu and S. M. Kakade and M. Telgarsky, Tensor decompositions for learning latent variable models. \textit{Journal of Machine Learning Research}, Vol. 15, Issue 1, pp. 2773-2832, January 2014.

\bibitem{HsuK2013} D. Hsu and S. M. Kakade, Learning mixtures of spherical Gaussians: moment methods and spectral decompositions, \textit{Proceedings of the fourth Innovations in Theoretical Computer Science}, pp. 11-20, January, 2013.

\bibitem{GeHK2015} R. Ge and Q. Huang and S. M. Kakade, Learning mixtures of Gaussians in high dimensions, \textit{Proceedings of the forty-seventh annual ACM symposium on Theory of computing}, pp. 761-770, June, 2015.

\end{thebibliography}
\end{document}

\end{document}


%!TEX root = dsp_2nd_program_hw.tex

\subsection{Realization of DTMF using FFT --- dif\_fft.h}
This part is explained in the previous report.
The code is shown in section~\ref{code:fft}

See https://github.com/lzhbrian/Fast-Fourier-Transform for more information.

\subsection{Realization of DTMF using Goertzel --- goertzel.h}
Goertzel algorithm is a method by which we can only calculate the amplitude of certain frequency.
By the following functions, We can obtain the $X[k]$ we want:
\begin{equation}\label{equation:iter}
v_{k}[n] = x[n] + 2cos(\omega_{k})v_{k}[n-1]-v_{k}[n-2]
\end{equation}
\begin{equation}\label{equation:xk_calc}
X[k] = v_{k}[N-1] - W^{k}_{N}v_{k}[N-2]
\end{equation}
where
$$\omega_{k} = 2\pi k/N, W_{N}=e^{2\pi /N}, v_{k}[-2]=v_{k}[-1]=0, v_{k}[0]=x[0]$$

In this DTMF detection, we want to acquire the amplitude of
$$697Hz, 770Hz, 852Hz, 941Hz, 1209Hz, 1336Hz, 1477Hz, 1633Hz$$
Note that we get the $k$ for each frequency by the following equation:
\begin{equation}
k = ( N * f ) / SamplingRate;
\end{equation}
where $N$ is the length of the sequence, and f is the targeted frequency.

So we first iteratively calculate the value of $v_{k}[N-1]$ and $v_{k}[N-2]$ using equation~(\ref{equation:iter}), then we use them to get the value of X[k] by equation~(\ref{equation:xk_calc}).
In the real practice, we further return the amplitude of X[k] by calculating their sum of squares.

The code is shown in section~\ref{code:goertzel}