\documentclass[12pt]{article}
\usepackage{latexsym}

\def\Def{Definition}
\def\a{for every }
\def\A{For every }
\def\e{there exists }
\def\E{There exists }
\def\pf{{\em Proof. }}
\def\eps{\varepsilon}
\def\bx{\bar{x}}
\def\by{\bar{y}}
\def\vphi{\varphi}

 \begin{document}

\newtheorem{dfn}{Definition}[section]
\newtheorem{eqn}{Equation}[section]
\newtheorem{lem}{Lemma}[section]
\newtheorem{thm}{Theorem}[section]
\newtheorem{cor}{Corollary}[section]

\title{A Brief Overview of Lyapunov Stability for Ordinary Differential Equations}
\author{David Glickenstein}
\date{May 2, 1996}
\maketitle

\abstract{Lyapunov stability is a way to characterize the qualitative
idea of stability in models of physical systems.  A stable solution to
a differential equation is one which does not change much when the
initial conditions are changed slightly.  We shall look at two methods
of checking the stability of solutions to ordinary differential
equations.  The first works by characterizing the solutions of linear
systems to study their stability and then looking at nonlinear systems
which are close to the linear case.  The second uses an auxilliary
function to find stability without having to characterize the
solutions.  Finally we look at some of the limitations of the Lyapunov
notion of stability.}

\section{Introduction}

We are going to look at general ordinary differential equations of the
following type:
\begin{equation}
	x'(t) = f(t,x(t))
	\label{gen}
\end{equation}
where $x: \Re \rightarrow \Re^n$ and $f:\Re \times \Re^n \rightarrow
\Re^n$ are functions and $t$, which usually represents time, is the
only independent variable.  The derivative $x'(t)$ is simply the
derivatives of each of the component functions, so if $x(t) =
(x_1(t),...,x_n(t))$ where $x_i(t)$ are real-valued functions on $\Re$
then
$$x'(t) = (x_1'(t),...,x_n'(t)).$$
In some cases we will be considering the autonomous case
\begin{equation}
	x'(t) = f(x(t))
	\label{aut}
\end{equation}
where $f:\Re^n \rightarrow \Re^n$.  We are going to assume the
existence of continuous solutions and uniqueness of a solution with
the initial condition $x(t_0) = x_0$ where $t_0$ is a constant and
$x_0$ is a constant n-vector.  In the simplest case, a solution will
tend to a constant as $t$ goes to infinity.  In the autonomous case,
we can think of the solution as a continuous curve through n-space
(called phase space), and when the solution tends to a constant, the
curve will converge to a point in phase space.  If a constant function
$c$ satisfies the differential equation, then the the derivative
$x'(t) = f = 0$ for $x_0 = c$ and $c$ is called an equilibrium point.
Periodic solutions satisfy $x(t) = x(t + n\tau)$ for some $\tau$ and
any integer $n$.  Other, more complicated solutions also exist which
may or may not be stable.  Nonautonomous systems have analogous types
of solutions.

We are going to be studying the stability of (\ref{gen}).  Stability
is a qualitative notion that if we perturb the initial condition
slightly, say $x(t_0) = x_0 + \eps$, then the new solution will be
close to the original system.  Stable differential equations are more
useful in practice since $x_0$ is often a  measured value and does
not have infinite precision.  A stable differential equation would
insure that our model would come up with an answer reasonably close to
the actual answer an exact initial condition.

First, we will define our notion of stability.  What we shall refer to
as {\em stable} is more correctly termed {\em Lyapunov stable} since
there are many different mathematical notions of stability.

\begin{dfn}
  	A solution $x(t)$ is {\em stable} if \a $\eps >0$
	\e $\delta > 0$ such that if $\| x_0 - \bx_0 \| < \delta$ then
	$\| x(t) - \bx(t) \| < \eps$ \a $t \ge t_0$, where $\bx(t)$ is
	the solution to
	$$\bx'(t) = f(t, \bx(t))$$
with $x(t_0) = x_0$ and $\bx(t_0) = \bx_0$.
\end{dfn}

Sometimes our model works so well that if we change the initial
condition slightly, the solution will still converge to the exact
solution under the original initial conditions.  This means that when
we use a measured value, our approximate model (with an inexact
initial condition) not only stays close to the actual solution, but
gets closer and closer to the actual solution as $t$ increases.  This
notion is called {\em asymptotic stability}.

\begin{dfn}
	A solution $x(t)$ is {\em asymptotically stable} if it is stable
	and there is a $\rho > 0$ such that if $\| x_0 - \bx_0 \| < \rho$
	then
$$\lim_{t \rightarrow \infty} \| x(t) - \bx(t) \| = 0.$$
\end{dfn}

In general, asymptotic stability is more desirable, since we know that
no matter where we place our initial condition, we will converge to
the actual answer if we are close enough to the actual initial
condition.  Unfortunately, asymptotic stability is not always
possible, as we shall see in the fourth section.

We will primarily use these two notions of stability.  There
are two main methods used to study Lyapunov stability, known
as Lyapunov's First and Second (or Direct) Methods.

\section {Lyapunov's First Method}

Lyapunov's First Method works by characterizing solutions to
differential equations of the type (\ref{gen}) and using those
characterizations to learn what we can about stability.  Thus our
approach will begin with simple linear systems and move on to certain
kinds of nonlinear systems which exhibit similar behavior to the
linear systems.

We will first look at linear differential equations

\begin{equation}
	x'(t) = A(t)x(t)
    \label{linear}
\end{equation}

where $A: \Re ^n \rightarrow \Re ^n$ is linear and $x: \Re \rightarrow \Re^n.$

When working with linear systems of the form (\ref{linear}) we will
use the following theorem, without proof, to characterize solutions.
The theorem is presented as a matrix equation where the derivative
$C'(t)$ of a matrix function $C$ is simply defined as the derivative
of each of the entries of the matrix.

\begin{thm}
	If $\Phi '(t) = A(t) \Phi (t)$ with $\Phi$ an invertible $n \times
n$ matrix then all solutions of (\ref{linear}) are linear combinations
of the columns of $\Phi$ and, in particular, we can choose some $\Phi
(t)$ such that $\Phi (t_0) = I$ where $I$ is the identity matrix, so
that $x(t) = \Phi (t) x_0$ is a solution to (\ref{linear}).  \end{thm}

\pf Omitted.

\begin{dfn}
	$\Phi$ as described in the last theorem is called the {\em
fundamental matrix} of the differential equation.
\end{dfn}

The fundamental matrix depends only on the differential equation and
thus only on the matrix function $A(t)$.

We also will need to define the operator norm for a matrix A:

\begin{dfn}
	If $A$ is an $n \times n$ real matrix then
$$\| A \| = \sum^n_{i,j=1} |a_{ij}|$$
\end{dfn}

Note that $\| Ax \| \le \| A \| \  \|x\|$ where $\| A \|$ is the operator
norm and the other two norms are the usual Euclidean vector norm in $\Re^n$.

We are now ready to prove an important theorem.  It turns out that in
the linear case the property of solutions being stable is equivalent
to the property of solutions being bounded.

\begin{thm}
	All solutions of (\ref{linear}) are stable if and only if they are
bounded.
\end{thm}

% begin proof
\pf All solutions are of the form $\Phi (t) x_0$ where $\Phi$ is the
fundamental matrix such that $\Phi (t_0) = I$.  Let $x(t)$ and
$\bx(t)$ be solutions satisfying $x(t_0) = x_0$ and $\bx(t_0) =
\bar{x}_0$ respectively.

Assume that all solutions to (\ref{linear}) are bounded.  Thus we can
find an $M$ such that $\| \Phi (t) \| < M$ for all $t > 0$.  Given
some $\eps > 0$, if $\| x_0 - \bx_0 \| < {\eps \over M}$ then
\begin{eqnarray*}
	\| x(t) - \bx(t) \| & = & \| \Phi (t)x_0 - \Phi (t) \bx_0 \| \\
	& \le & \| \Phi (t) \| \ \| x_0 - \bx_0 \| \\
	& < & \eps.
\end{eqnarray*}

Conversely, assume that all solutions are stable.  Thus the particular
solution $x(t) \equiv 0$ is stable, so \a $\eps > 0$ \e $\delta > 0$
such that if $\| \bx_0 \| < \delta$ then
$$\| \bx (t) \| = \| \Phi (t) \bx_0\| < \eps.$$
In particular, let
$$\bx_0^{(i)} = {\delta \over 2} e_i $$
where $e_i$ is the ith standard basis vector of $\Re^n$.  Then
$$\| \Phi (t) \bx_0^{(i)} \| = \| \phi_i(t) \| {\delta \over 2} <
\eps$$
where $\phi_i$ is the ith column of $\Phi$.  We can sum these over all
i and get:
$$\| \Phi (t) \| \le \sum^n_{i=1} \| \phi_i(t) \| \le {2n \eps \over
\delta} = k$$
so $$\| x(t) \| = \| \Phi (t) x_0 \| \le k \| x_0 \|$$
and $x(t)$ is bounded. $\Box$
%end proof

Note that boundedness is not necessarily true for stable systems in
general.  For instance, take the differential equation $x'(t) = 1$
whose solutions are all stable but not bounded.

Lyapunov's First Method consists mainly of showing that solutions of linear
systems, especially if $A(t)$ is a constant funtion, are bounded and
hence stable.  It then looks at functions which are nonlinear, but
simply small perturbations of linear functions.  We shall state
without proof several theorems.  For a more detailed explanation with
proofs, see [2].

Consider the constant linear equation:
\begin{equation}
	x'(t) = A \ x(t)
	\label{constlin}
\end{equation}
where $A$ is a constant matrix.  We can easily show the following.

\begin{thm}
	If all eigenvalues of A have negative real parts then every
solution to (\ref{constlin}) is asymptotically stable.
\end{thm}

\pf Omitted.

We can easily extend this to equations with slight perturbations.  So
we characterize the following nonlinear equation

\begin{equation}
	x'(t) = A \ x(t) + f(t,x(t))
	\label{nonlinear}
\end{equation}

with the following theorem.

\begin{thm}
	If f satisfies:

\begin{enumerate}
\item $f(t,x)$ is continuous for $\| x \| < a, \ 0 \le t <\infty$ and
\item $\lim_{\| x \| \rightarrow 0} {\| f(x,t) \| \over \| x \|} = 0$
\end{enumerate}
then the solution $x(t) \equiv 0$ is asymptotically stable.
\end{thm}

It turns out that to characterize the general linear case where $A$ is a
function of time, we need a slightly stronger notion of stability
called uniform stability.  The reader is again referred to [2].

\section{Lyapunov's Second Method}

Lyapunov's Second or Direct Method is unique in that it does not
require a characterization of the solutions to determine stability.
This method often allows us to determine whether a differential
equation is stable without knowing anything about what the solutions
look like, so it is ideal for dealing with nonlinear systems.

The method uses a supplementary function called a Lyapunov function to
determine properties of the asymptotic behavior of solutions to a
differential equation of the general form (\ref{gen}).  We will need
the following definitions.

\begin{dfn}
	A function $V(t,x)$ is {\em positive [negative]
definite} if \e a real-valued function $\vphi (r)$ such that:

\begin{enumerate}
\item $\vphi (r)$ is strictly increasing on $0 \le r \le a$ and
$\vphi (0) = 0$ and
\item $V(t,x) \ge \vphi ( \| x \| ) \ [V(t,x) \le - \vphi ( \| x \| )]$
for all $(t,x) \in \{ (t,x): t_0 \le t < \infty, \| x \| \le b < a \}$.
\end{enumerate}

\end{dfn}

\begin{dfn}
	$$V' = \sum^n_{i=1} {\partial V \over \partial x_i} f_i(t,x) +
{\partial V \over \partial t}$$
\end{dfn}
Note that $V'(t,x(t))$ is the time derivative of $V$.

\begin{dfn}
	A real function $V(t,x)$ is said to admit an {\em infinitesimal upper bound} if \e
$h > 0$ and a continuous, real-valued, strictly increasing function
$\psi$ with $\psi (0) = 0$ such that
$$|V(t,x)| \le \psi( \| x \|) \ {\rm for} \ \| x \| < h \ {\rm and} \
t \ge t_0.$$
\end{dfn}

Using these definitions we shall construct a function $V$.  If we can
find such a $V$, then we can find stable solutions to the differential
equation.  Unfortunately, there is no general way to construct $V$
from the differential equation (\ref{gen}).  Note that we can define
$V$ in terms of $t$ and $x$ (not $x(t)$) and since our definition of $V'$
uses the function $f$, the existence of $V$ will have implications for
the differential equation.

\begin{thm}
	If a continuous real-valued function $V(t,x)$ exists such that:
\begin{enumerate}
\item $V(t,x)$ is positive definite and
\item $V'(t,x)$ is non-positive
\end{enumerate}
then $x(t) \equiv 0$ is a stable solution of (\ref{gen}).
\end{thm}

\pf We know that $V(t,x)$ is positive definite, so \e a strictly
increasing function $\vphi (r)$ such that
$$0 < \vphi (\| x \| ) \le V(t,x)$$
for $0 < \| x \| < b$ and $t > t_0$.

Given $\eps > 0$ let
$$m_{\eps} = \min_{\| x \| = \eps} \vphi (\| x \| ) = \vphi (\eps).$$ Notice that
$m_{\eps} > 0$.  Since $V$ is continuous and $V(t,0) = 0$ for all $t$
(by positive definiteness),
we can choose a $\delta > 0$ such that
$$V(t_0, x_0) < m_{\eps}$$
if $\|x_0 \| < \delta$.  Now
$$V'(t,x(t)) \le 0$$
for $t_1 \ge t_0$ and $\| x_0 \| < \delta$ implies
$$V(t_1, x(t_1)) \le V(t_0,x(t_0)) = V(t_0,x_0) < m_{\eps}$$

Now, suppose for some $t_1 \ge t_0$ that $\| x(t_1) \| \ge \eps$
whenever $\| x_0 \| < \delta$.  Then
$$V(t_1,x(t_1)) \ge \vphi (\| x(t_1) \| ) \ge \vphi (\eps) =
m_{\eps}$$
which is a contradiction, so \a $\eps > 0$ \e $\delta > 0$ such that
$\| x(t) \| < \eps$ if $\| x_0 \| < \delta$ for $t \ge t_0$,
i.e. $x(t) \equiv 0$ is stable.  $\Box$

We call a function $V$ which satisfies the hypotheses of the theorem a
{\em Lyapunov function}.  In physical systems, the Lyapunov function
used is often an expression for energy, for instance stability for the
differential equation for a spring:
\begin{eqnarray*}
x'(t) & = & y\\
y'(t) & = & -kx
\end{eqnarray*}
can be done with $V(x,y) = {1 \over 2} y^2 + {1 \over 2} k x^2$, which is the equation
for total energy of the system.

By strengthening the requirements on $V$ we can find conditions for
asymptotic stability.

\begin{thm}
	If a continuous function $V(t,x)$ exists satisfying:
\begin{enumerate}
\item $V(t,x)$ is positive definite,
\item $V(t,x)$ admits an infinitesimal upper bound, and
\item $V'(t,x)$ is negative definite
\end{enumerate}
then the solution $x(t) \equiv 0$ of (\ref{gen}) is asymptotically
stable.
\end{thm}

\pf  By the previous theorem, the solution must be stable.  Suppose
that $x(t)$ is not asymptotically stable, so \a $\eps >0$ there exist
$\delta > 0$ and $\lambda > 0$ such that any nonzero solution $x(t)$ where
$x(t_0) = x_0$ satisfies $\lambda \le \| x(t) \| < \eps$ if $t \ge
t_0$ and $\| x_0 \| < \delta$.

Since $V'(t,x)$ is negative definite, there is a strictly increasing function
$\vphi (r)$ vanishing at the origin such that $V'(t,x) \le -
\vphi(\| x \|)$.  We know that $\| x(t) \| \ge \lambda > 0$ for $t \ge
t_0$ so there is a $d > 0$ such that $V'(t,x(t)) \le -d$.  This
implies that
\begin{eqnarray*}
	V(t,x(t)) & = & V(t_0,x_0) + \int^t_{t_0} V'(s,x(s)) ds \\
	& \le & V(t_0,x_0) - (t- t_0) d
\end{eqnarray*}
which is less less than zero for large $t$, contradicting the
assumption that $V(t,x)$ is positive definite.

Thus no such $\lambda$ exists and since $V(t,x(t))$ is positive definite and
decreasing with respect to $t$
$$\lim_{t \rightarrow \infty} V(t,x(t)) = 0$$
which implies that
$$\lim_{t \rightarrow \infty} \| x(t) \| = 0,$$
since $V(t,x(t)) \le \psi (\| x(t) \|)$ where $\psi (r)$ is strictly
increasing and vanishes at the origin (by the second hypothesis).
Thus as $V(t,x(t)) \rightarrow 0$, $\psi(\| x(t) \|) \rightarrow 0$,
and $\| x(t) \| \rightarrow 0$ (because $\psi$ is strictly increasing
and vanishes at 0).  Thus $x(t) \equiv 0$ is asymptotically stable. $\Box$

We can also use the direct method to determine instability in some cases.

\begin{thm}
	If a continuous real-valued function $V(t,x)$ exists on some set
$S = \{ (t,x) : t \ge t_1 \ and \ \| x \| < a \} $ satisfying:
\begin{enumerate}
\item $V(t,x)$ admits an infinitesimal upper bound,
\item $V'(t,x)$ is positive definite on S, and
\item \E a $T > t_1$ such that if $t_0 \ge T$ and $h > 0$ then \e $c
\in \Re^n$ such that $\| c \| < h$ and $V(t_0,c) > 0$
\end{enumerate}
then the solution $x(t) \equiv 0$ is not stable.
\end{thm}

\pf Suppose $x(t)$ is a solution not identically zero.  Then, by
uniqueness, $ \| x(t) \| \neq 0$ for all $t \ge t_1$.  If $x(t)$ is
defined at $t_0$ then for every $t \ge t_0$ for which $x(t)$ is
defined
$$V(t,x(t)) - V(t_0, x(t_0)) = \int^t_{t_0} V'(s,x(s)) ds > 0$$
since $V'(t,x(t))$ is positive definite.

Let $t_0 \ge T$ and $\eps > 0$.  There exists a $c$ such that $\| c \|
< \min(a,\eps)$ and $V(t_0,c) > 0$ by the third hypothesis.  Since $V$
admits an infinitesimal upper bound and is continuous, there is a
$\lambda \in (0,a)$ such that $\| x \| < \lambda$ for $t \ge t_1$.
Therefore,
$$| V(t,x) | < V(t_0, c).$$

Let $x(t)$ satisfy the initial condition $x(t_0) = c$.  Then $V(t,x(t)
> V(t_0, x(t_0)) > 0$ (from the positive definiteness argument), so
$\| x(t) \| \ge \lambda$.  Also by the second hypothesis we know that
there is a strictly increasing function $\vphi(r)$ such that $V'(t,x)
\ge \vphi(\| x \|)$.  Let
$$ \mu = \min_{\| x \| \in [\lambda, a]} \vphi(\| x \|) = \vphi (\lambda).$$
then
$$V'(t,x(t)) \ge \vphi(\| x(t) \|) \ge \mu.$$
Using the integral inequality, we get
$$V(t,x(t)) \ge V(t_0, x(t_0)) + (t - t_0) \mu.$$ Thus we can make
$V(t, x(t))$ arbitrarily large.  Since V(t,x) admits an infinitesimal
upper bound, there exists a strictly increasing function $\psi (r)$
such that $|V(t,x(t))| < \psi (\| x \|)$ and $\psi (0) = 0$.  So since
$V(t,x(t))$ can be arbitrarily large, $\psi(\| x(t) \|)$ can also be
arbitrarily large, and thus $\| x(t) \|$ gets arbitrarily large and the
solution is not stable. $\Box$

\section {Limitations of Lyapunov Stability}

The Lyapunov notion of stability has some problems, some of which we
shall look into here.  Asymptotic stability as we have defined it does
not always work well for periodic systems, so often other notions such
as orbital stability are used when studying differential equations
which have periodic solutions.  In addition, certain classes of
systems such as Hamiltonian Systems do not react well to a Lyapunov
analysis.  Finally, there is the question of whether Lyapunov
stability actually reflects our qualitative idea of stability.

We will first look at how Lyapunov stability works with periodic
solutions to the autonomous system (\ref{aut}).  It turns out that our
notion of asymptotic stability is incompatible with such solutions,
since the points will not get closer together, even thought two
solutions may get closer and closer to the same final state (a
periodic cycle).  To prove this we will first need a Lemma that
solutions to autonomous systems are invariant under translation.

\begin{lem}
	If $x(t)$ is a solution to (\ref{aut}) then $x(t+h)$ is also a
solution for any real number $h$.
\end{lem}

\pf Let $\bx (t) = x(t+h)$ and $s = t+h$.  Then
\begin{eqnarray*}
	\bx ' (t) & = & {dx \over ds}{ds \over dt} \\
	& = & x'(s)\\
	& = & f(x(s))\\
	& = & f(x(t+h))\\
	& = & f(\bx(t))\\
\end{eqnarray*}
so $\bx(t) = x(t+h)$ is a solution to (\ref{aut}). $\Box$

Now we can use the fact that translations of solutions are also
solutions to show that nontrivial periodic solutions cannot be
asymptotically stable.  By nontrivial periodic we mean that the
solutions do not converge to a constant vector, i.e., $x(t) \neq x(t +
\eps)$ for some small $\eps > 0$.

\begin{thm}
	If $x(t)$ is a nontrivial periodic solution of an autonomous
	system (\ref{aut}) then $x(t)$ is not asymptotically stable.
\end{thm}

\pf Suppose $x(t)$ is a nontrivial periodic solution.  Thus \e $t_0$
such that $f(x(t_0)) \neq 0$ because if not, $x'(t) = 0$ for all $t$
and the solution is constant (trivially periodic).  Suppose also that
$x(t)$ is asymptotically stable.

The stability assumption says that given $\eps > 0$ \e $\hat{\delta}
> 0$ such that $\| x(t) - \bx(t) \| < \eps$ if $ \| x_0 - \bx_0 \| <
\hat{\delta}$ where $x(t)$ satisfies $x(t_0) = x_0$ and $\bx(t)$ is a
solution to the same equation satisfying the initial condition
$\bx(t_0) = \bx_0$.

By the asymptotic stability assumption we can find a $\rho > 0$ such
that
$$\lim_{t \rightarrow \infty} \| x(t) - \bx(t) \| = 0$$
if $\| x_0 - \bx_0 \| < \rho$.

Let $\delta = \min (\hat{\delta}, \rho)$.  By the continuity of $x(t)$
we can fix some small $\bar{t}$ so that
$$\| x(t_0) - x(t_0 + \bar{t}) \| < \delta.$$
By the Lemma, $x(t + \bar{t})$ is a solution to the differential
equation, so by our asymptotic stability assumption
$$\lim_{t \rightarrow \infty} \| x(t) - x(t + \bar{t})\|  = 0.$$

We know that $x'(t_0) \neq 0$ so we can find $r > 0$ so that $\|x(t_0)
- x(t_0 + \bar{t}) \| > r$.  Also $x(t)$ is periodic, so assume it has
period $\tau$.  Then
$$\| x(t_0 + n \tau) - x(t_0 + \bar{t} + n \tau) \| > r >0$$
for all $n$, so the limit cannot go to zero, which contradicts our
assumption of asymptotic stability.  Thus $x(t)$ cannot be
asymptotically stable. $\Box$

Another class of problems which for which asymptotic stability does
not work is Time-Independent Hamiltonian systems.  They are defined as follows.

\begin{dfn}
	A {\em time-independent Hamiltontian system (TIHS)} is the 2n
dimensional system
\begin{eqnarray*}
	x'(t) & = & H_y(x(t), y(t)) \\
	y'(t) & = & -H_x(x(t), y(t))
\end{eqnarray*}
where $x(t)$ and $y(t)$ are both functions from $\Re$ to $\Re^n$ and
$H(x,y)$ is a real valued function with continuous partial
derivatives.
\end{dfn}

Hamiltonian Systems are used to model a large class of physical systems
and have many applications.  It turns out that, like nontrivial
periodic solutions to autonomous systems, no solutions to a TIHS can be
asymptotically stable.

\begin{thm}
	If $(x(t), y(t))$ is a solution to a TIHS then it is not
asymptotically stable.
\end{thm}

\pf Suppose $(x(t),y(t))$ is an asymptotically stable solution.  Then
we can find $t_0$ and $\delta > 0$ such that if $(\bx(t), \by(t))$ is
a solution and if $\| (x(t_0),y(t_0)) - (\bx(t_0), \by(t_0)) \| < \delta$ then
$$\lim_{t \rightarrow \infty} \| (x(t),y(t)) - (\bx(t), \by(t)) \| =
0.$$

But also,
$${d \over dt} H(x,y) = {\partial H \over \partial x}{dx \over dt} +
{\partial H \over \partial y}{dy \over dt} = H_x (H_y) + H_y (-H_x) =
0$$
so $H(x,y)$ is constant on any solution $(x(t),y(t))$.  By the continuity of $H$,
it must be constant on some $\delta$-neighborhood N of
$(x(t_0),y(t_0))$, so every point in N is an equilibrium point.  Thus
$(x(t),y(t))$ cannot be asymptotically stable (for initial condition
$(\bx(t_0),\by(t_0)) \in {\rm N}$, the solution
$(\bx(t),\by(t))$ will not converge to $(x(t),y(t))$. $\Box$

It turns out that although Lyapunov stability is extremely useful in
mathematically defining our qualitative view of stability, it is
neither necessary nor sufficient for something to appear stable on a
physical level.  Some examples can be found at the end of Chapter 4 of [1].

\begin{thebibliography}{99}
\bibitem{Cronan} J. Cronin. {\em Differential Equations: Introduction
and Qualitative Theory.} Marcel Dekker, New York, 1994.
\bibitem{Sanchez} D.A. Sanchez. {\em Ordinary Differential Equations
and Stability Theory: An Introduction.}  Dover Publications, New York,
1979.
\end{thebibliography}

\end{document}


\newcommand{\of}{\texttt{.o}~}

\lab{Interfacing With Other Programming Languages Using Cython}{Interfacing With Other Programming Languages Using Cython}
\label{lab:cythonwrap}

\objective{Learn to interface with object files using Cython. This lab should be worked through on a machine that has already been configured to build Cython extensions using gcc or MinGW.}

Suppose you are writing a program in Python, but would like to call code written in another language.
Perhaps this code has already been debugged and heavily optimized, so you do not want to simply re-implement the algorithm in Python or Cython.
In technical terms, you want Python to \emph{interface} (or communicate) with the other language.
For example, NumPy's linear algebra functions call functions from LAPACK and BLAS, which are written in Fortran.

One way to have Python interface with C is to write a Cython \emph{wrapper} for the C function.
The wrapper is a Cython function that calls the C function.
This is relatively easy to do because Cython compiles to C.
From Python, you can call the wrapper, which calls the C function (see Figure TODO).
In this lab you will learn how to use Cython to wrap a C function.

\section*{Wrapping with Cython: an overview}
When you use Cython to wrap a C function, you just write a Cython function that calls the C function.
To actually do this, we need to understand a little bit more about how C works.

After you write a program in C, you pass it to a compiler.
The compiler turns your C code into machine code, i.e., instructions that your computer can read.
The output of the compiler is an \emph{object file}.

In our scenario, we have C code defining a single function that we would like to wrap in Cython.
This C code has already been compiled to an object file.
The protocol for calling the C code from Cython is similar to calling it from C:
\begin{enumerate}
\item we need to \emph{include a header file} for the C code in our Cython code, and
\item we must \emph{link to the object file} compiled from the C code when we compile the Cython code.
\end{enumerate}

A \emph{header} for a file contains the declaration of the function defined in the file.
We include the header in the Cython code so that the compiler can check that the function is called with the right number and types of arguments.
Then, when we tell the compiler to \emph{link to} the object file, we simply tell it where to find the instructions defining the function declared in the header.

The Cython code will compile to a second object file.
This object file defines a module that can be imported into Python. See Figure \ref{fig:cython_flowchart}.

%TODO: make figure
\begin{figure}
\begin{tikzpicture}[dot/.style={circle,fill=black,minimum size=3pt,inner sep=0pt,
            outer sep=-1pt}, >=stealth', thick]

\node[draw, minimum height=1.5cm, minimum width=3cm, align=center](ctri1)
	[]{\Large{ctridiag.h} \\ {\small declare \texttt{ctridiag()}}};

\node[draw, minimum height=1.5cm, minimum width=3cm, align=center, node
	distance=2.5cm](ctri2)[below of=ctri1]{\Large{ctridiag.c} \\ {\small define \texttt
	{ctridiag()}}};

\node[draw, minimum height=1cm, minimum width=2cm, align=center, node
	distance=2.2cm, color=white!40!black](ctri3)[below of=ctri2]{COMPILE};

\node[draw, minimum height=.7cm, minimum width=3cm, align=center, node
	distance=1.9cm](ctri4)[below of=ctri3]{ \Large{ctridiag.o} \\{\small object file callable} \\ {\small from C}};

%\node[draw, minimum height=1cm, minimum width=3cm, align=center, node
%	distance=.85cm](ctri5)[below of=ctri4]{object file  \\ callable by C};

\draw[-, color=white!40!black](ctri2)--(ctri3);
\draw[->, shorten >=3pt,color=white!40!black](ctri3)--(ctri4);

\node[draw, minimum height=1.5cm, minimum width=3cm, align=center, node
	distance=7cm](ctri6)[right of=ctri1]{\Large{cython\_ctridiag.pyx}
	 \\ {\small define \texttt{cytridiag()}}};

\node[draw, minimum height=1cm, minimum width=2cm, align=center, node
	distance=3.3cm,color=white!40!black](ctri7)[below of=ctri6]{COMPILE};

\node[draw, minimum height=1.5cm, minimum width=3cm, align=center, node
	distance=3.3cm](ctri8)[below of=ctri7]{\Large{ctridiag.so} \\ {\small object file
	callable} \\ {\small from Python}};

\draw[-, color=white!40!black](ctri6)--(ctri7);
\draw[->, shorten >=3pt,color=white!40!black](ctri7)--(ctri8);
\draw[->, dashed, shorten >=3pt,color=white!40!black](1.5,-6.95)--(6, -3.3);
\draw[->, shorten >=3pt,color=white!40!black](ctri1)--(ctri6);

\node[draw, minimum height=.7cm, minimum width=1cm, align=center, node
	distance=3.1cm,color=white!40!black, fill=white](include)[right of=ctri1]
	{INCLUDE};

\node[draw, minimum height=.7cm, minimum width=1cm, align=center, node
	distance=5.8cm,color=white!40!black, fill=white](include)at(3.7,-5.15){LINK};

\end{tikzpicture}
\caption{This diagram shows the relationships between the files used to write a Cython wrapper for \li{ctridiag()}.
In fact, the Cython file \texttt{cython\_ctridiag.pyx} compiles first to C code and then to the shared object file, but we have omitted this step from the diagram.}
\label{fig:cython_flowchart}
\end{figure}

\section*{Wrapping with Cython: an example}
As an example, we will wrap the C function \texttt{ctridiag()} below.
This function computes the solution to a tridiagonal system $A\v = \x$.
Its parameters are pointers to four 1-D arrays, which satisfy the following:
\begin{itemize}
\item Arrays \li{a} and \li{c} have length \li{n-1} and contain the first subdiagonal and superdiagonal of $A$, respectively.
\item Arrays \li{b} and \li{x} have length \li{n} and represent the main diagonal of $A$ and $\x$, respectively.
\end{itemize}
The array \li{c} is used to store temporary values and \li{x} is transformed into the solution of the system.

\lstinputlisting[style=fromfile, language=C, name=ctridiag.c]{./ctridiag/ctridiag.c}

This terminal command tells the C-compiler gcc to compiles \texttt{ctridiag.c} to an object file \texttt{ctridiag.o}.

\begin{lstlisting}
>>> gcc -fPIC -c ctridiag.c -o ctridiag.o
\end{lstlisting}
The \texttt{-fPIC} option is required because we will later link to this object file when compiling a shared object file.
The \texttt{-c} flag prevents the compiler from raising an error even though \texttt{ctridiag.c} does not have a \li{main} function.

\subsection*{Write a header for \texttt{ctridiag.c}}

The header file essentially contains a function declaration for \li{ctridiag()}.
It tells Cython how to use the object file \li{ctridiag.o}.
\lstinputlisting[style=fromfile, language=C, name=ctridiag.h]{./ctridiag/ctridiag.h}

\subsection*{Write a Cython wrapper}
Next we write a Cython file containing a function that ``wraps'' \li{ctridiag()}.
This file must include the header we wrote in the previous step.

\lstinputlisting[style=fromfile, language=Python, name=cython_ctridiag.pyx]{./ctridiag/cython_ctridiag.pyx}

Some comments on this code are in order.
First, including the header for \li{ctridiag()} allows us to call this function even though it was not defined in this file.
This is a little like importing NumPy at the start of your Python script.

Second, the arguments of the Cython function \li{cytridiag()} are not in bijection with the arguments of \li{ctridiag()}.
In fact, Cython does not need to know the size of the NumPy arrays it accepts as inputs because these arrays are objects that carry that information with them.
We extract this information and pass it to the C function inside the Cython wrapper.

However, it is possible to unintentionally access memory outside of an array by calling this function.
For example, suppose the input arrays \li{a}, \li{b}, \li{c}, and \li{x} are of sizes 4, 5, 3, and 5.
Since \li{x} is used to determine the size of the system, the function \li{ctridiag()} will expect \li{c} to have length 4.
At some point, \li{ctridiag()} will likely try to read or even write to the 4th entry of \li{c}.
This address does exist in memory, but it does not contain an entry of \li{c}!
Therefore, this function must be called by a responsible user who knows the sizes of her arrays.
Alternatively, you could check the sizes of the arrays in the Cython wrapper before passing them to the C function.

Finally, the C function expects a parameter \li{double* a}, meaning that \li{a} is a \emph{pointer to} (i.e., the address of) a \li{double}.
The funtion \li{ctridiag()} expects this double to be the first entry of the array \li{a}.
So instead of passing the object \li{a} to \li{ctridiag()}, we find the first entry of \li{a} with \li{a[0]}, and then take its address with the \li{&} operator.

\subsection*{Compile the Cython wrapper}

Now we can compile \texttt{cython\_ctridiag.pyx} to build the Python extension.
The following setup file uses distutils to compile the Cython file, and may be run on Windows, Linux, or Macintosh-based computers.
Notice that in line 28, we link to the existing object file \texttt{ctridiag.o}.

\lstinputlisting[style=fromfile, language=Python, name=ctridiag_setup_distutils.py]{./ctridiag/ctridiag_setup_distutils.py}

This setup file can be called from the command line with the following command.
\begin{lstlisting}[style=ShellInput]
python ctridiag_setup_distutils.py build_ext --inplace
\end{lstlisting}
The \texttt{--inplace} flag tells the script to compile the extension in the current directory.
The appendix at the end of this lab contains setup files that build the Python extension by hand on various operating systems.

\subsection*{Test the Python extension}
After running the setup file, you should have a Python module called \li{cython_cytridiag} that defines a function \li{cytridiag()}.
You can import this module into IPython as usual.
However, if you modify \li{ctridiag()} and then try to recompile the extension, you may get an error if the module is currently in use.
Hence, if you are frequently recompiling your extension, it is wise to test it  with a script.

The following script tests the module \li{cython_cytridiag}.

\lstinputlisting[style=fromfile, language=Python, name=ctridiag_test.py]{./ctridiag/ctridiag_test.py}

\begin{warn}
When passing arrays as pointers to C or Fortran functions, be \emph{absolutely sure} that the array being passed is contiguous.
This means that the entries of the array are stored in \emph{adjacent} entries in memory.
Passing one of these functions a strided array will result in out of bounds memory accesses and could crash your computer.
For an example of how to check an array is contiguous in a Cython wrapper, see Problem \ref{prob:c_wrap}.
\end{warn}

\section*{Another example}
%TODO: replace the C function for this problem with one that implements Monte Carlo integration

The C function \li{cssor()} below implements the Successive Over Relaxation algorithm for solving Laplace's equation, which in two dimensions is
\[
\frac{\partial^2 F}{\partial x^2} + \frac{\partial^2 F}{\partial y^2} = 0.
\]
In the Successive Over Relaxation algorithm, an input array is iteratively modified until it converges to the solution $F(x,y)$.

\lstinputlisting[style=fromfile, language=C, name=cssor.c]{./cssor/cssor.c}

The function \li{cssor()} accepts the following inputs.
\begin{itemize}
\item $U$ is a pointer to a Fortran-contiguous 2-D array of double precision numbers.
\item $m$ is an integer storing the number of rows of $U$.
\item $n$ is an integer storing the number of columns of $U$.
\item $\omega$ is a double precision floating point value between $1$ and $2$.
The closer this value is to $2$, the faster the algorithm will converge, but if it is too close the algorithm may not converge at all.
For this lab just use $1.9$.
\item tol is a floating point number storing a tolerance used to determine when the algorithm should terminate.
\item maxiters is an integer storing the maximum allowable number of iterations.
\item info is a pointer to an integer.
This function will set info to $0$ if the algorithm converged to a solution within the given tolerance.
It will set info to $1$ if it did not.
\end{itemize}

\begin{problem}\label{prob:c_wrap}
Wrap \li{cssor()} so it can be called from Python, following these steps.
\begin{enumerate}
\item Write a C header for \li{cssor.c}.
\item Write a Cython wrapper \li{cyssor()} for the function \li{cssor()} as follows.
    \begin{enumerate}
    \item Have \li{cyssor()} accept parameters \li{tol} and \li{maxiters} that default to $10e^{-8}$ and 10,000, respectively.
    What other arguments does \li{cyssor()} need to accept?
    \item Check that the input \li{U} is a C-contiguous array (this means that the array is stored in a certain way in memory).
    You can check if \li{U} is C-contiguous with the code \li{U.is_c_contig()}, which returns a Boolean value.
    If \li{U} is not C-contiguous, raise a ValueError as follows:
    \begin{lstlisting}
    raise ValueError('Input array U is not C-contiguous')
    \end{lstlisting}
    When raising a ValueError, you may replace the string \li{'Input array U is not C-contiguous'} with any desired text.
    For more about errors and exceptions in Python, see \url{https://docs.python.org/2/tutorial/errors.html}.
    \item Raise a ValueError if \li{cssor()} fails to converge---i.e., if \li{cssor()} sets \li{info} to 1.
    \end{enumerate}
\item Compile \li{cssor.c} to an object file and compile your Cython wrapper to a Python extension.
Be aware that you may compile \li{cssor.c} correctly and still receive warnings from the compiler.
\item Write a test script for your Cython function.
    \begin{enumerate}
    \item You can run the function with the following code.
    \begin{lstlisting}
	import numpy as np
	from cython_fssor import cyssor
	resolution = 501
	U = np.zeros((resolution, resolution))
	X = np.linspace(0, 1, resolution)
	U[0] = np.sin(2 * np.pi * X)
	U[-1] = - U[0]
	cyssor(U, 1.9)
	\end{lstlisting}
    \item Have your test script run the code above and plot the modified array \li{U} on the domain $[0,1]\times[0, 1]$.
    Note that $U$ is a $501\times 501$ array.
    Your plot should look like Figure \ref{fig:solution}.
    \end{enumerate}
\end{enumerate}

%TODO: mayavify this plot.
\begin{figure}[H]
\includegraphics[width=\textwidth]{solution.png}
\caption{Correct output for the test script of Problem \ref{prob:c_wrap}.}
\label{fig:solution}
\end{figure}

\end{problem}

\section*{Wrapping a Fortran function (Optional)}
We can also use Cython to wrap Fortran.
As an example, we will wrap the Fortran function below, which implements the same algorithm as \li{ctridiag()}.

We have used the C library \li{iso_c_binding} to make the function accept pointers to native C types.
If we were wrapping a function or subroutine that we did not write ourselves, we would have to define a Fortran function that uses the \li{iso_c_binding} library to accept pointers from C and then uses the values it receives to call the original function.
\lstinputlisting[style=fromfile, language=Fortran, name=ftridiag.f90]{./ftridiag/ftridiag.f90}

\begin{warn}
When interfacing between Fortran and C, you will have to pass pointers to \emph{all} the variables you send to the Fortran function as arguments.
Passing a variable directly will probably crash Python.
\end{warn}

\subsection*{Write a header for \texttt{ftridiag.f90}}
Here is a header that tells C how to interface with the function we have just defined.
\lstinputlisting[style=fromfile, language=C, name=ftridiag.h]{./ftridiag/ftridiag.h}

To compile \li{ftridiag.f90} to an object file you can run the following command in your command line:
\begin{lstlisting}[style=ShellInput]
gfortran -fPIC -c ftridiag.f90 -o ftridiag.o
\end{lstlisting}

\subsection*{Write a Cython wrapper, compile, and test it}
The Cython wrapper for this function is analogous to \li{cython_ctridiag.pyx}, except that every variable passed to \li{ftridiag} should be a pointer.
Aside from the use of gfortran instead of gcc, the rest of the compilation and testing process is entirely the same.
The setup files specific to Windows and Linux, the setup file using distutils, and the test script are included in this lab folder.

\begin{warn}
Fortran differs from C in that the columns of arrays are stored in contiguous blocks of memory instead of the rows.
The default for NumPy and for C arrays is to have rows stored in contiguous blocks, but in NumPy this varies.
You should make sure any code passing arrays between Fortran, C, and NumPy addresses this discrepancy.
Though by default Fortran assumes arrays are arranged in Fortran-order, for many routines you can specify whether to act on an array or its transpose.
\end{warn}

\begin{info}
The function \li{ftridiag()} may seem unusual because it is a Fortran function that is callable from C.
In fact, this is not an uncommon scenario.
Many larger Fortran libraries (for example, LAPACK) provide C wrappers for all their functions.
\end{info}

\subsection*{Another example}

Here is a Fortran implementation of the Successive Over Relaxation algorithm for solving Laplace's equation.
This function is the Fortran equivalent of \li{cssor.c}.
Its parameters are the same, with the exception that all parameters are \emph{points} to the values they reference.

\lstinputlisting[style=fromfile, language=Fortran, name=fssor.f90]{./fssor/fssor.f90}

\begin{problem}
Imitate Problem \ref{prob:c_wrap} to wrap \li{fssor()} with Cython.
\end{problem}

\section*{Appendix: compiling C extensions for Python}
If you know more about compilers, you may find it enlightening to manually compile a C extension for Python.
Here we show how to manually compile \texttt{cython\_ctridiag.pyx} on Windows, Linux, and Macintosh machines.

On Windows, using the compiler MinGW (a version of gcc for windows), the compilation can be performed by running the following setup file.

\lstinputlisting[style=fromfile, language=Python, name=ctridiag_setup_windows64.py]{./ctridiag/ctridiag_setup_windows64.py}

The following file works on Linux and Macintosh machines using gcc.

\lstinputlisting[style=fromfile, language=Python, name=ctridiag_setup_linux.py]{./ctridiag/ctridiag_setup_linux.py}


%% -*- coding:utf-8 -*-
\chapter{Structure of finite K-algebras continued}

We apply the discussion from the last lecture to the case of field
extensions. We show that the separable extensions remain reduced after
a base change: the inseparability is responsible for eventual
nilpotents. As our next subject, we introduce normal and Galois
extensions and prove Artin's theorem on invariants.

\section{Structure of finite K-algebras, examples (cont'd)}

Last time we have seen that a finite $K$-algebra $A$
($\left[A:K\right] < \infty$) has only finitely many maximal ideals
$m_1, \dots, m_r$ and the following equation holds (see theorem
\ref{thm:structurefinitekalgebra}):
\[
A \cong A/m_1^{k_1} \times \dots \times A/m_r^{k_r}
\]
This is a general form of \mynameref{thm:chineseremainder}.

\begin{example}
  Let
  \[
  A = K\left[X\right]/\left(F\right)
  \]
  And the polynomial $F$ is not necessary
  irreducible so let's decompose into a product of irreducible
  factors:  $F= P_1^{k_1} \dots P_r^{k_r}$.
  Then by the  \mynameref{thm:chineseremainder}
  \footnote{
    See also remark \ref{rem:lec5_4} and theorem
    \ref{thm:structurefinitekalgebra}
  }
  one can get
  \[
  A \cong
  K\left[X\right]/\left(P_1\right)^{k_1} \times \dots
  \times K\left[X\right]/\left(P_r\right)^{k_r},
  \]
  where $K\left[X\right]/\left(P_i\right)^{k_i} = A/m_i^{k_i}$ and
  $m_i = \left( P_i \mod F \right)$
  \footnote{
    Using definition \ref{def:quotientring} one can get that
    $P_i \in K\left[X\right]$ corresponds to
    $P_i \mod F$ in $A = K\left[X\right]/\left(F\right)$ therefore
    we have $\left(P_i\right)$ is a \mynameref{def:maxideal} for
    $K\left[X\right]$ and
    \[
    A/\left(P_i\right)^{k_i} =
    K\left[X\right]/\left(P_i \mod F \right)^{k_i}.
    \]
    but $P_i \mod F  = P_i$ and as result
    \[
    K\left[X\right]/\left(F\right) \cong
    K\left[X\right]/\left(P_1\right)^{k_1} \times \dots
    \times K\left[X\right]/\left(P_r\right)^{k_r}.
    \]
  }
  - an ideal.
  \label{ex:lec5_1}
\end{example}

\begin{definition}[Nilpotent element]
  Let $A$ is a \mynameref{def:ring} than $x \in A$ is nilpotent if $x \ne 0$ but
  $\exists k: x^k = 0$.
  \footnote{
    Alternative definition from \cite{wiki:nilpotent}: An element,
    $x$, of a ring, $R$, is called nilpotent if there exists
    some positive integer, $n$, such that $x^n = 0$.
  }
  \label{def:nilpotent}
\end{definition}

\begin{definition}[reduced]
  $K$-algebra $A$ is reduced if it has no \mynameref{def:nilpotent}s.
  Or in other words
  \footnote{
    Let we have an $i$-th element of the product $\prod A/m_i^{k_i}$
    with $k_i > 1$ and $m_i  = (p)$ when
    $p \in A/m_i^{k_i}$ and $p \ne 0 = p^{k_i}$ i.e. $p$ is a
    nilpotent.
  }
  if in the decomposition
  \[
  A \cong A/m_1^{k_1} \times \dots \times A/m_r^{k_r}
  \]
  $\forall i: k_i = 1$.
  Or
  \footnote{
    $A/m_i$ is a field as soon as $m_i$ is a \mynameref{def:maxideal}
  }
  if $A$ is a product of fields.
  \label{def:reduced}
\end{definition}

\begin{definition}[local]
  \mynameref{def:ring} $A$ is called local if it has only one
  \mynameref{def:maxideal} i.e. $A \cong A/m^k$.
  \label{def:local}
\end{definition}

If $A$ is local then all elements of $A$ are nilpotents
i.e. any element of $A$ is a identity,zero or nilpotent
\footnote{
  As it was mentioned in \cite{wiki:localring}, a nonzero ring in
  which every element is either a unit or nilpotent is a local
  ring, but not reverse, as it was pointed on the lectures. There is
  also an example $A \cong A/\{0\}$ where $A$ is a \mynameref{def:field}
  and $\{0\}$ is the only maximal ideal for the fields (see example
  \ref{ex:maxideal}). In this case there are many non nilpotents
  different from identity and zero but the ring ($K$-algebra) is local.
}
\footnote{
  Comment from Staff on the issue: It looks confusing because Katya
  immediately applies this definition to the case of finite algebras
  without explicitly mentioning.

  You are right that it is not true in general. For example, discrete
  valuation rings are local and have no zero divisors at all. (Also your
  counterexample is not quite correct, because a field is exactly the
  case when every element is either invertible or nilpotent).

  But if a finite algebra has only one maximal ideal, then by the
  structural theorem it consists of nilpotents.
}
.

Most of our last examples were examples of reduced $K$-algebras such
as
\[
\mathbb{C} \otimes_{\mathbb{R}} \mathbb{C} =
\mathbb{C} \times \mathbb{C}
\]
or
\[
\mathbb{Q}\left(\sqrt{2}\right) \otimes_{\mathbb{Q}}
\mathbb{Q}\left(i\right) =
\mathbb{Q}\left(i, \sqrt{2}\right)
\]
that is a field and if we start producing similar examples then mostly
they are reduced. Well, why? Because in fact the presence of
nilpotents has to do with inseparability. The presence of nilpotents
reflects inseparability.

So let me give you one more example: tensor product of
extensions which is not reduced. Let $K$ be a field of characteristic
$p$, for instance $\mathbb{F}_p$. Consider a field of rational
functions over $K$
\footnote{
  As it was shown in example \ref{ex:lec3_1} (part
  \ref{ex:lec3_notperfect}) it's not a  \mynameref{def:perfectfield} and
  as result of theorem \ref{thm:lec3_4} is not separable.
}
: $K\left(X\right)$. We will consider
$K\left(X\right)$ as an extension of $K\left(X^p\right)$ (or with new
variable $Y = X^p$ - $K\left(Y\right)$. We will be interested in
\(
K\left(X\right)
\otimes_{K\left(Y\right)}
K\left(X\right)
\) where $X$ is a $p$th root of $Y$ so
\footnote{
  as soon as
  $K\left(X\right) = K\left[T\right]/\left(T^p - Y\right)$
}
\begin{eqnarray}
K\left(X\right)
\otimes_{K\left(Y\right)}
K\left(X\right)
\cong
\nonumber \\
\cong
K\left(X\right)
\otimes_{K\left(Y\right)}
K\left[T\right]/\left(T^p - Y\right)
\cong
\nonumber \\
\cong
K\left(X\right)\left[T\right]/\left(T^p - Y\right) =
\nonumber \\
=
K\left(X\right)\left[T\right]/\left(T^p - X^p\right) =
K\left(X\right)\left[T\right]/\left(T - X\right)^p
\nonumber
\end{eqnarray}
where $T$ is another variable. As result we have got a ring with
nilpotents for example $T - X$ and of course the reason is that our
extension $K\left(X\right)$ is pure inseparable extension (see
definition \ref{def:inseparabledegree}) of $K\left(Y\right)$.

\section{Separability and base change}
What is the reason for such a mysterious connection between
presence of nilpotents and separability? If $L$ is separable over $K$
then the number of \mynameref{def:homomorphism}s
$\left|Hom_K\left(L, \bar{K}\right)\right|$ is maximal and equal to
degree $\left[L : K\right]$ but in general it is less or equal to the
degree.  This is of course clear, because  if we have a polynomial
with distinct roots, then it's stem field for instance  has exactly
this number of  homomorphisms into the-algebraic closure and this
number is equal to the number of roots. So if some roots coincide,
then the number of homomorphisms diminishes.

Lets also recall \mynameref{thm:basechange}. If $L$ and $E$ are
extensions of $K$ and $L$ is finite over $K$ then
\[
Hom_K\left(L, E\right) \cong
Hom_E\left(
L \otimes_K E, E
\right).
\]
In the formula, $L \otimes_K E$ is a finite $E$-algebra denoted as $A$
below.
\begin{myremark}
  We have that $A = L \otimes_K E \cong E \otimes_K L$ is a free $E$
  module as soon as $L$ is a free $K$ module and with proposition
  \ref{prop:lec4_Addon} we have that $\left[A : E\right] < \infty$
  (as soon as $\left[L : K\right] < \infty$)
  and
  as result with theorem \ref{thm:structurefinitekalgebra} one can get
  that there are finitely many maximal ideals $m_i$ and
  \[
  A \cong A/m_1^{k_1} \times \dots \times A/m_r^{k_r}
  \]
  \label{rem:lec5_own1}
\end{myremark}

\begin{definition}
  With \mynameref{thm:chineseremainder} we have
  \[
  A \cong A/m_1^{k_1} \times \dots \times A/m_r^{k_r}
  \]
  Reduced algebra $A_{red}$ is defined by the following equation
  \[
  A_{red} = A/m_1 \times \dots \times A/m_r
  \]
  \label{def:reducedalgebra}
\end{definition}

We have that
\footnote{
  i.e. nilpotents become zeros in the $A_{red}$.
}
\[
A_{red} = A /\eta\left(A\right)
\]
where $\eta\left(A\right)$ is an \mynameref{def:ideal} of
nilpotents in $A$.

It is clear that
\[
Hom_E\left(A, E\right) =
Hom_E\left(A_{red}, E\right)
\]
because all homomorphism into a field must be zero on all nilpotents
\footnote{
  It requires some clarification. Consider a homomorphism
  $\phi \in Hom_E\left(A, E\right)$. $\forall x,y \in A, \phi(xy) =
  \phi(x)\phi(y)$. Let $x \in \eta\left(A\right)$ i.e. $x$ is a
  nilpotent then $x \ne 0_A, x^k = 0_A$. We have
  \[
  0_E = \phi(x^k) = \phi(x)^k
  \]
  i.e. $\phi(x) =  0_E$. Therefore all nilpotents go to zero and,
  instead of $A$ (as the set the $\phi$ acts on), we can consider
  $A_{red}$. As result, we will get that $\phi(0_{A_{red}}) = 0_E$ and all
  other properties of homomorphism are also hold, for instance
  $\forall \bar{x},\bar{y} \in A_{red}: \phi(\bar{x}+\bar{y}) =
  \phi(\bar{x}) + \phi(\bar{y})$. Really
  $\bar{x} = x + \eta(A), \bar{x} = x + \eta(A)$ and
  \[
  \phi(\bar{x}+\bar{y}) =
  \phi(x+y) = \phi(x) + \phi(y) =
  \phi(\bar{x}) + \phi(\bar{y})
  \]
  as soon as $\phi(\eta(A)) = 0_E$.
}.

So again, we see that if there are nilpotents in the tensor product,
then there is somehow fewer space for homomorphisms. Because if A is
not reduced, then the dimension
\[
\left[A_{red} : E\right] < \left[A : E\right].
\]
So the maximal number of homomorphisms, so let's say the slogan
``Maximal number of homomorphisms'' is attained when $A$ is reduced
and all quotients
\begin{equation}
  A/m_i \cong E
  \label{eq:lec5_1}
\end{equation}
because those quotients are of
course extensions of $E$.
\footnote{
  as it was mentioned above $A = L \otimes_K E$ is a finite
  $E$-algebra i.e. $A$ is a $E$-extension. The $A/m$ is also
  $E$-algebra (see remark
  \ref{rem:lec5_quotientkalgebra}) i.e. also $E$-extension.

  From other hand we consider $Hom\left(A/m_i, E\right)$ i.e. there is
  a homomorphism (injection) from $A/m_i \to E$ and as result
  $A/m_i \cong E$. These arguments are also used in the text below for
  the fact explanation.
}
In general,
those quotients are extensions of $E$ (see remark
  \ref{rem:lec5_quotientkalgebra}). We also have
\[
A \cong A/m_1 \times \dots \times A/m_r
\]
but $Hom\left(A/m_i, E\right) = \{0\}$ if $\left[A/m_i: E\right] > 1$.
This is because a field homomorphism is always injective. A field
homomorphism a homomorphism of fields which are extensions of $E$  an
$E$-homomorphism is injective. So you cannot map an $E$-vector space of
dimension greater than 1 into an $E$-vector space of dimension 1.

Lets take $E = \bar{K}$ then automatically we will get
$A/m_i \cong E$ because an algebraically closed field does not have a
non trivial finite extension.

So what have we had (see also example \ref{ex:lec5_conrad})?
\[
A = L \otimes_K \bar{K},
\]
\begin{equation}
  A_{red} = \prod_{i=1}^r \bar{K}.
  \label{eq:lec5_AredIsProd}
\end{equation}
The following one $A = A_{red}$ is the same to $r$ is maximal and
equal to $\left[L:K\right] = \left[A: \bar{K}\right]$.
\footnote{
  It is because there are $\left[L:K\right] = r$ roots of a
  polynomial and all the roots are in $\bar{K}$. Each root $\alpha_i$
  forms a polynomial $X - \alpha_i$ which creates an ideal $m_i =
  \left(X - \alpha_i\right)$. We also have
  $L = K\left(\alpha_1, \dots, \alpha_r\right)$ and
  \[
  A = L \otimes_K \bar{K} \cong \bar{K} \otimes_K L =
  \bar{K} \otimes_K K\left(\alpha_1, \dots, \alpha_r\right).
  \]
  It expands to (see example \ref{ex:lec4_1})
  \begin{eqnarray}
    A \cong \bar{K} \otimes_K \frac{K\left[X\right]}{(X-\alpha_1)
      \dots (X-\alpha_r)} \cong
    \nonumber \\
    \cong
    \frac{\bar{K}\left[X\right]}{\left(X - \alpha_1\right)}
    \times
    \dots
    \times
    \frac{\bar{K}\left[X\right]}{\left(X - \alpha_r\right)}
    \cong \bar{K} \times \dots \times \bar{K}.
    \nonumber
  \end{eqnarray}
}
In the case
\[
r = \left|Hom_{\bar{K}}\left(A, \bar{K}\right)\right| =
\left|Hom_K\left(L, \bar{K}\right)\right|
\]
So this explains why seperability is the same thing as the absence of
nilpotents. So let me formulate it as a theorem.

\begin{theorem}
  Let $L$ is a finite extension over $K$ then
  \begin{enumerate}
  \item $L$ is separable if and only if $L \otimes_K \bar{K}$
    is \mynameref{def:reduced}.
    $L$ is pure inseparable if and only if $L \otimes_K \bar{K}$
    is \mynameref{def:local}
  \item $L$ is separable if and only if for all algebraic extension
    $\Omega$, $L \otimes_K \Omega$ is reduced.
    $L$ is pure inseparable if and only if for all algebraic extension
    $\Omega$, $L \otimes_K \Omega$ is local.
  \item If $L$ is separable then the map
    \[
    \phi: L \otimes_K \bar{K} \to \bar{K}^n
    \]
    which sends
    \[
    \phi\left(l \otimes k\right) =
    \left(
    k \phi_1\left(l\right),
    \dots,
    k \phi_n\left(l\right)
    \right)
    \]
    where $\phi_i$ are distinct homomorphisms from $L$ to $\bar{K}$,
    is an isomorphism.
  \end{enumerate}
  \label{thm:lec5_1}
  \begin{proof}
    \begin{enumerate}
    \item $L$ separable is the same thing that the algebra
      $A = L\otimes_K \bar{K}$ has $\left[L:K\right]$ factors
      \footnote{
        If we have $L = K\left(\alpha\right)$
        (see theorem \ref{thm:lec3_3}) then
        there exists a minimal polynomial $P_{min}\left(\alpha,
        K\right)$ of degree $r = \left[L:K\right]$. The polynomial
        splits and has $r$ roots: $\alpha_1, \dots, \alpha_r$. Thus we
        have $r$ maximal ideals
        \(
        m_1 = \left(X - \alpha_1\right), \dots,
        m_r = \left(X - \alpha_r\right)
        \).
        For each maximal ideal we have $A/m_i \cong A$ (see claim
        \ref{claim:lec1_fieldquotionisomorphism}) thus with theorem
        \ref{thm:structurefinitekalgebra} and (\ref{eq:lec5_1}) we have
        \[
        A \cong \prod_{i=1}^r \bar{K}
        \]
      }
      $\bar{K}$ which is the same as $A$ is \mynameref{def:reduced}
      since $\dim_{\bar{K}} A = \left[L:K\right]$.
      \footnote{
        From (\ref{eq:lec5_AredIsProd}), if $A = A_{red}$, one can get
        $\dim_{\bar{K}} A = \dim_{\bar{K}} A_{red} = \dim_{\bar{K}}
        \prod_{i = 1}^r \bar{K} = r = \left[L:K\right]$.
      }

      $L$ is pure inseparable: this means that exists only one
      homomorphism of $L$ into $\bar{K}$ i.e. $A$ has only one
      $\bar{K}$-homomorphism into $\bar{K}$ thus only one factor and
      as result $A$ is \mynameref{def:local}.
    \item If $\Omega$ is an algebraic extension then
      \footnote {
        i.e. we can setup an \mynameref{def:injection} and structure
        preserving map $f: L \otimes_K \Omega \to L \otimes_K
        \bar{\Omega}$ (see definition \ref{def:embedding})
      }
      \[
      L \otimes_K \Omega \hookrightarrow L \otimes_K \bar{\Omega} =
      L \otimes_K \bar{K}.
      \]
      There is a sub-ring and so one easily checks, that a sub-ring of
      a reduced algebra is reduced and same for local.
    \item Leave as an excises
      \footnote{
        Accordingly \cite{mathstackexchange:tensorproduct} we have the
        following proof.
        Let $l_1, l_2, \dots, l_n$ is a $K$ basis in $L$.
        Then we can
        get the following elements of $\bar{K}^n$:
        $v_i = \left(\phi_1(l_i), \phi_2(l_i), \dots,
        \phi_n(l_i)\right)$. We can construct the following matrix if
        we put the $v_i$ vectors as columns:
        \[
        \begin{bmatrix}
          \phi_1(l_1) & \phi_1(l_2) & \cdots & \phi_1(l_n) \\
          \phi_2(l_1) & \phi_2(l_2) & \cdots & \phi_2(l_n) \\
          \vdots & \vdots & \ddots & \vdots \\
          \phi_n(l_1) & \phi_n(l_2) & \cdots & \phi_n(l_n) \\
        \end{bmatrix}
        \]
        Linear dependence relation between rows looks like
        \begin{eqnarray}
          \sum_{k=1} c_k \phi_k(l_i) = 0.
          \label{eq:lec5_note_isom}
        \end{eqnarray}
        Thus $\forall l \in L^\ast, \exists \{k_i\} \subset K: l =
        \sum_{i=1}^nk_i l_i$ (as soon as $\{l_i\}$ is the $K$ basis of
        $L$). Thus after multiplication (\ref{eq:lec5_note_isom}) by
        $k_i$ and summation we have
        \begin{eqnarray}
         \sum_{i=1} k_i \sum_{k=1} c_k \phi_k(l_i) =
        \sum_{k=1} c_k \phi_k(l) = 0
        \nonumber
        \end{eqnarray}
        that should hold for any $k_i$ and as result for any $l$. By
        theorem \ref{thm:dedekind} we can conclude that $\forall k:
        c_k = 0$. As result we have the set $\{v_i\}$ as linear
        independent. The size of the set is $n = \dim \bar{K}^n$
        i.e. $\{v_i\}$ is the $\bar{K}$ basis of $\bar{K}^n$.
      }
    \end{enumerate}
  \end{proof}
\end{theorem}
\begin{remark}
In general for modules $M$, $N$ and $P$ over a ring $R$ \textbf{not true} that
if $M \hookrightarrow N$ ($M$ is a sub module of $N$) then
$M \otimes_R P \hookrightarrow N \otimes_R P$. But this become the
truth if $R$ is a field and as result $M,N,P$ are
\mynameref{def:vectorspace}s. So, for my field extensions, I can say
that if I have an extension and then I take a base change, then it
remains an extension, but  you should not think that the same thing is
true for arbitrary modules over a ring.
\end{remark}

\begin{myexample}
  The example was taken from \cite{bib:KeithConradSeparability2}.
  Consider extension $\mathbb{Q}\left(\sqrt{2}\right)$ over
  $\mathbb{Q}$. Since
  \[
  \mathbb{Q}\left(\sqrt{2}\right) \cong
  \mathbb{Q}\left[X\right]/\left(X^2 - 2\right)
  \]
  tensoring with $\mathbb{Q}$ gives
  \begin{eqnarray}
    \mathbb{Q}\left(\sqrt{2}\right) \otimes_{\mathbb{Q}}
    \bar{\mathbb{Q}} \cong
    \bar{\mathbb{Q}} \otimes_{\mathbb{Q}}
    \mathbb{Q}\left(\sqrt{2}\right)
    \cong
    \nonumber \\
    \cong
    \bar{\mathbb{Q}} \otimes_{\mathbb{Q}}
    \mathbb{Q}\left[X\right]/\left(X^2 - 2\right)
    \cong
    \nonumber \\
    \cong
    \bar{\mathbb{Q}}\left[X\right]/
    \left(
    \left(X - \sqrt{2}\right)
    \left(X + \sqrt{2}\right)
    \right)
    \cong
    \nonumber \\
    \cong
    \bar{\mathbb{Q}}\left[X\right]/\left(X - \sqrt{2}\right)
    \times
    \bar{\mathbb{Q}}\left[X\right]/\left(X - \sqrt{2}\right)
    \cong
    \bar{\mathbb{Q}} \times \bar{\mathbb{Q}}
    \nonumber
  \end{eqnarray}
  We used the following fact (see claim
  \ref{claim:lec1_fieldquotionisomorphism})
  \[
  \bar{\mathbb{Q}}\left[X\right]/\left(X \pm \sqrt{2}\right)
  \cong
  \bar{\mathbb{Q}}
  \]
  \label{ex:lec5_conrad}
\end{myexample}

\section{Primitive element theorem}

\begin{definition}[Idempotent]
  The element $x$ is called idempotent if $x \cdot x = x$
  \label{def:idempotent}
\end{definition}

\begin{theorem}[Primitive element]
  Let $L$ is a finite \mynameref{def:separableextension} of $K$ then it
  has only finitely many sub extensions i.e. $E$ such that $K \subset
  E \subset L$.
  \begin{proof}
    So, let's base change to $\bar{K}$
    \footnote {
      see proof of theorem \ref{thm:lec5_1} (second part of it).
    }
    :
    \(
    E \otimes_{K} \bar{K} \hookrightarrow
    L \otimes_{K} \bar{K}
    \)
    \footnote{
      See also proof of theorem \ref{thm:lec5_1}.
    }.
    We also have (see also equation (\ref{eq:lec5_AredIsProd}))
    \[
    E \otimes_{K} \bar{K} \cong \bar{K}^m
    \]
    and
    \[
    L \otimes_{K} \bar{K} \cong \bar{K}^n
    \]
    are reduced $\bar{K}$ sub-algebras generated by
    \mynameref{def:idempotent}s namely by
    \[
    e_i = \left(0,0, \dots, 1, \dots, 0\right),
    \]
    where $1$ is in $i$-th place
    \footnote{
      this is because the typical element $k \in \bar{K}^n$ has the
      following form
      \[
      k = \left(k_1, k_2, \dots, k_n\right) = \sum_{i=1}^n k_i e_i,
      \]
      where $k_i \in \bar{K}$.
    }.

    On the other hand $L \otimes_K \bar{K} \cong \bar{K}^n$ has only
    finitely many \mynameref{def:idempotent}s because
    $\left(a_1, \dots, a_i, \dots, a_n\right)$ is an idempotent if and
    only if all $a_i$ are 0 or 1 and therefore there
    are only finitely many ways to choose $m$ idempotents out of them,
    \footnote{
      we have the following equation
      \[
      \bar{K}^m \hookrightarrow \bar{K}^n
      \]
    }
    so there is only finitely many ways to generate a subalgebra.
  \end{proof}
  \label{thm:primitiveelement}
\end{theorem}

\begin{corollary}[Primitive element theorem]
  $\exists \alpha \in L$ such that $L = K\left( \alpha \right)$
  whenever $L$ is finite and separable.
  \begin{proof}
    And this is easy to see, of course, because
    if $L$ and $K$ are infinite, then
    $L$ cannot be a union, a finite union of proper subextension.
    A vector space over an infinite field is not a finite union of
    proper subspaces.   For instance a plane is not a finite union of
    lines.
    \footnote{
      It will require some additional explanations. Took
      $\alpha \in L$ such that $P_{min}\left(\alpha, K\right)$ has
      maximal degree. If $K\left(\alpha\right) = L$ we complete and
      found the primitive element. If not then let
      $\beta \in L \setminus K\left(\alpha\right)$. Consider the
      following element $\gamma_a = \alpha + a \beta$ where $a \in
      K$. For any $a \in K$ exists $K\left(\gamma_a\right)$ such that
      $K \subset K\left(\gamma_a\right) \subset L$. We have
      $\left|K\right| = \infty$ but the
      number of sub-extensions is limited by theorem
      \ref{thm:primitiveelement} therefore $\exists a, b \in K$ such that
      $a \ne b$ and $K\left(\gamma_a\right) =
      K\left(\gamma_b\right) = K\left(\gamma\right)$ where $\gamma =
      \gamma_a$.

      We have $\gamma_a - \gamma_b = (a-b) \beta \in K\left(\gamma\right)$, i.e.
      $\beta \in K\left(\gamma\right)$. Therefore
      $\alpha = \gamma_a -a \beta = \gamma -a \beta \in
      K\left(\gamma\right)$. We also
      have (as soon as $\beta \notin
      K\left(\alpha\right)$) $K \subset K\left(\alpha\right)
      \subsetneq K\left(\gamma\right)
      \subset L$. Thus $\left[K\left(\gamma\right):K\right] >
      \left[K\left(\alpha\right):K\right]$. Therefore
      (see proposition \ref{prop:dimextension})
      \[
      \deg\left(P_{min}\left(\gamma, K\right)\right) >
      \deg\left(P_{min}\left(\alpha, K\right)\right)
      \]
      that is in contradiction with $\alpha$ choose.

      Note that \cite{bib:KenBrownPrimitiveElementTheorem} has another,
      more known, proof for the fact and prove that if
      $L = K\left(\alpha, \beta\right)$ then $\exists \lambda \in K$
      such that $\gamma = \alpha + \lambda \beta$ is the primitive
      element i.e. $L = K\left(\gamma\right) = K\left(\alpha,
      \beta\right)$.
    }

    If $L$ and $K$ are \mynameref{def:finitefield}s, then we have
    already described this situation completely. We have described all
    finite extensions and have seen that they are generated by one element.
    \footnote{
      As it was mentioned in the proof of corollary \ref{cor:lec3_1}
      we can take $\alpha = \mbox{ generator of } K^\times$. For more info
      see corollary \ref{cor:lec3_1}.
    }
  \end{proof}
  \label{col:primitiveelement}
\end{corollary}

\section{Examples. Normal extensions}

\subsection{Examples}

\begin{example}[Primitive element]
  \[
  \mathbb{Q}\left(\sqrt{2}, \sqrt{3}\right) =
  \mathbb{Q}\left(\sqrt{2} + \sqrt{3}\right).
  \]
  We have
  $\left[\mathbb{Q}\left(\sqrt{2}, \sqrt{3}\right) :
    \mathbb{Q}\right] = 4$ so all sub-extensions are quadratic.
  \footnote{
    As soon as extension has degree $4 = 2 \cdot 2$ then a
    sub-extension should have
    degree 2 and the minimal polynomial should be quadratic.
  }
  As no
  quadratic polynomial has $\alpha = \sqrt{2} + \sqrt{3}$ for a root
  \footnote{
    Quadratic polynomials have very simple formula for roots with
    only one square (discriminant) and it is not possible to get 2
    squares with it
  },
  $\alpha$ generates $\mathbb{Q}\left(\sqrt{2}, \sqrt{3}\right)$.

  This must be a primitive element, generates our field.
  It is not contained in any proper subextension.

  There is another proof (not part of the lectures) that shows that
  $\beta = \sqrt{2} + \sqrt{3}$ is a primitive element i.e.
  $\sqrt{2}, \sqrt{3}$ are generated by $\beta$. Really
  $\beta^2 = 5 + 2 \sqrt{2}\sqrt{3}$ i.e.
  \[
  \sqrt{2}\sqrt{3} = \frac{\beta^2 - 5}{2}.
  \]
  From other side
  \[
  \sqrt{2} \beta = \sqrt{2}\left(\sqrt{2} + \sqrt{3}\right) =
  2 + \sqrt{2}\sqrt{3} = \frac{\beta^2 - 1}{2}.
  \]
  Therefore
  \[
  \sqrt{2} = \frac{\beta^2 - 1}{2 \beta}
  \]
  and
  \[
  \sqrt{3} = \beta - \frac{\beta^2 - 1}{2 \beta}.
  \]
  \label{ex:lec5_primitiveelement}
\end{example}

\begin{example}[Extension which cannot be generated by a single element]
  So, take $K$ equal to $\mathbb{F}_p$ and consider
  $K\left(x,y\right)$ as an
  extension of $K\left(x^p,y^p\right)$. It has degree $p^2$
  \footnote {
    $K \subset K\left(x^p\right) \subset K\left(x^p,y^p\right)$ and
    $\left[K\left(x^p\right): K\right] = p$ as well as
    $\left[K\left(x^p, y^p\right): K\left(x^p\right)\right]$ thus with
    theorem \ref{thm:mulformuladegrees}
    \[
    \left[K\left(x^p, y^p\right): K\right] = p^2.
    \]
  }
  i.e.
  \[
  \left[
    K\left(x,y\right) : K\left(x^p,y^p\right)
    \right] = p^2.
  \]
  We have $\forall \alpha \in K\left(x,y\right) \setminus
  K\left(x^p,y^p\right)$ is of degree $p$ over
  $K\left(x^p,y^p\right)$. This is because
  $\alpha^p \in K\left(x^p,y^p\right)$
  \footnote{
    $\alpha = k_1 x + k_2 y$, where $k_1, k_2 \in K =
    \mathbb{F}_p$. Using remark \ref{rem:frobeniushomomorphism} we can
    get
    \[
    \alpha^p = k_1^p x^p + k_2^p y^p \in K\left(x^p, y^p\right).
    \]
  }
  . So, no element like these can
  generate our extension.
\end{example}

\subsection{Normal extensions}

\begin{definition}[Normal extension]
  A normal extension of $K$ is a \mynameref{def:splittingfield} of a
  family of polynomials
  \footnote {
    There is a set of polynomials (can be only one polynomial in the
    set) and the polynomials not necessary to be irreducible. Example
    $\mathbb{Q}/\mathbb{Q}$ - is a normal
    extension because there is a set of polynomials split in it :
    $\left\{X-1, X^2-1\right\}$. Note that any irreducible polynomial
    (another definition below) that has a root in it also splits, for
    instance $X - a$, where $a \in \mathbb{Q}$ is an irreducible, has
    a root $a \in \mathbb{Q}$ and splits in it. From other side an
    arbitrary polynomial ( for example $X^3-1$ ) can have a root in
    $\mathbb{Q}$ but does not necessary split in it.
  }
  in $K\left[X\right]$.
  \footnote{
    Another good definition of a normal extension
    \cite{wiki:normalextrus} can also be used. Normal extension $E/K$ is
    such algebraic extension in which every irreducible polynomial
    $P(X) \in K\left[X\right]$ that has a single root in $E$ splits in
    $E$.
  }
  \label{def:normalextension}
\end{definition}

\begin{remark}[Normal extension]
  So, take a bunch of polynomials in $K$ and we adjoin all their roots
  to $K$, and this is what is called a normal extension.
  For instance, a \mynameref{def:splittingfield} of one polynomial is
  also a normal extension.
\end{remark}

\begin{theorem}
  The following conditions are equivalent for an extension $L$ of $K$:
  \begin{enumerate}
  \item $\forall x \in L$ $P_{min}\left(x, K\right)$ splits in $L$.
  \item $L$ is \mynameref{def:normalextension}
  \item All \mynameref{def:homomorphism}s from $L$ to $\bar{K}$ have the
    same image.
    \footnote{
      And the image is $L$ as soon as $id$ is also a
      \mynameref{def:homomorphism}

      There are 2 staff's comments about the claim:
      \begin{itemize}
      \item You can't naturally identify $L$ with a particular
        subfield of $\overline{K}$ (unless $L$ is given as a
        subfield of $\overline{K}$ a priori). But from the lecture
        2.5 (see theorem \mynameref{thm:lec2_3}) we know, that there
        exists an embedding $L
        \hookrightarrow \overline{K}$ since $L$ is algebraic
        over $K$. And the theorem says that images of all such
        embeddings are equal if $L$ is normal over $K$.
      \item My previous answer was a little bit misleading. I meant,
        if you have an arbitrary algebraic extension of $K$, there
        is no canonical embedding into $\overline{K}$.

        % Was removed by the staff
        %% Consider, for
        %% example, a field $\mathbb{Q}/(x^3-2)$. It has several
        %% embeddings into $\overline{\mathbb{Q}}$,
        %% namely, $\mathbb{Q}(\sqrt[3]{2})$, $\mathbb{Q}(e^{2\pi
        %%   i/3}\sqrt[3]{2})$ and $\mathbb{Q}(e^{2\pi
        %%   i/3}\sqrt[3]{2})$. But if we consider the splitting field
        %% of $x^3-2$, then it's image in $\overline{\mathbb{Q}}$
        %% is $\mathbb{Q}(\sqrt[3]{2}, e^{2\pi i/3})$. So, yes,

        If we
        identify roots of polynomials which define $L$ with some
        elements of $\overline{K}$ (up to a permutation for each
        polynomial), then the image is $L$ itself.
      \end{itemize}

      As conclusion
      \cite{bib:baker} (p. 54),
      we can say that if $L$ is a splitting field of a
      polynomial $P$ (just another definition of normal extension
      \cite{bib:baker})
      with roots $\alpha_1, \alpha_2, \dots, \alpha_n$,  then
      $L=K\left(\alpha_1, \alpha_2, \dots, \alpha_n\right)$ and as
      soon as the homomorphism $\phi$ permutes the roots of $P$
      (if $L \subset \bar{K}$ or if we
      identify roots of polynomials which define $L$ with some
      elements of $\overline{K}$)
      :
      \begin{eqnarray}
      \phi\left(L\right) =
      \phi\left(K\left(\alpha_1, \alpha_2, \dots,
      \alpha_n\right)\right) =
      \nonumber \\
      K \left(\phi\left(\alpha_1\right),
      \phi\left(\alpha_2\right),
      \dots
      \phi\left(\alpha_n\right)
      \right) =
      K\left(\alpha_1, \alpha_2, \dots, \alpha_n\right) = L
      \nonumber
      \end{eqnarray}
    }
  \item The \mynameref{def:group} of \mynameref{def:automorphism}s
    $Aut\left(L/K\right)$ acts transitively (see definition
    \ref{def:transitive}) on this set of
    homomorphisms $Hom_K\left(L, \bar{K}\right)$.
  \end{enumerate}
  \label{thm:lec5_3}
  \begin{proof}
    1 implies 2: Take
    $\left(P_i\right)_{i \in I} = \left\{P_{min}\left(x, K\right) \mid
    x \in L\right\}$ - the set of polynomials
    (i.e. the family of polynomials).
    $L$ will be a splitting field of the set $\left(P_i\right)_{i \in
      I}$ and therefore (by definition \ref{def:normalextension}) $L$ is normal.

    2 implies 3: Let
    $S = \left\{\mbox{roots of } P_i, i \in I \mbox{ in } L\right\}$
    and
    $S' = \left\{\mbox{roots of } P_i, i \in I \mbox{ in }
    \bar{K}\right\}$
    \footnote{
      $S$ and $S'$ are 2 sets of roots
    }
    then any homomorphism
    $\phi: L \to \bar{K}$ sends $S$ to $S'$, but $S$ generates $L$
    over $K$, so $\phi\left(S\right)$ determines $\phi\left(L\right)$
    \footnote{
      As soon as $\phi(S) = S'$ then we have one set of roots and as
      result one image (that is generated by the set) for
      homomorphisms. Or more concrete, let $l \in L, S = \{s_i\}, S' =
      \{s'_i\}$. We have $l = \sum l_{ik}s_i^k$ where $l_{ik} \in K$.
      For the homomorphism $\phi$ we have
      $\phi(l) = \sum l_{ik}{s'}_i^k$ i.e. the image $\phi(L)$ consists
      of elements of the following form: $\sum l_{ik}{s'}_i^k$. For any
      homomorphism we will have the same form for the image or in
      other words - any homomorphism has the same image.
    }
    .

    3 implies 4: Let $j, j' \in Hom_K\left(L, \bar{K}\right)$
    (i.e. we took 2 homomorphisms)
    then
    they send $L$ isomorphically to its image $L'$. So, these are
    isomorphisms from $L$ to $L'$. So

    \begin{tikzpicture}[descr/.style={fill=white,inner sep=2.5pt}]
      \matrix (m) [matrix of math nodes, row sep=3em,
        column sep=3em]
              { & L' & \\
                L & & L\\ };
              \path[->,font=\scriptsize]
              (m-2-1) edge node[descr] {$ j' $} (m-1-2)
              (m-1-2) edge node[descr] {$ j^{-1} $} (m-2-3)
              (m-2-1) edge node[descr] {$ j^{-1}j' $} (m-2-3);
    \end{tikzpicture}

    take $j^{-1} \cdot j' \in Aut \left(L/K\right)$ and it sends $j$
    to $j'$
    \footnote{
      We have got that $\forall j, j' \in Hom_K\left(L,
      \bar{K}\right), \exists g \in Aut\left(L/K\right)$
      such that $g(j) = j'$, for instance $g = j^{-1}j'$ and
      \begin{equation}
        g(j) \equiv j g = j j^{-1} j' = j'.
        \label{eq:lec5_groupaction}
      \end{equation}
        Therefore group
      automorphisms acts transitively by the definition
      \ref{def:transitive}
    }.

    4 implies 1:  I have this \mynameref{def:transitive} and I have to prove
    that any minimal polynomial splits. Consider
    $P_{min}\left(x, K\right)$. $\alpha_1, \dots, \alpha_n$ - roots in
    $\bar{K}$. Then I have map $K\left(x\right) \to
    K\left(\alpha_i\right)$ that extends to
    $j_i: L \xrightarrow[x \to \alpha_i]{} \bar{K}$. This is by
    \mynameref{thm:lec2_3}.
    $\exists \theta_i \in Aut\left(L/K\right)$ such that
    $j_1 \theta_i = j_i$
    \footnote{
      as it was mentioned at the equation (\ref{eq:lec5_groupaction})
    }
    thus
    $\alpha_i \in j_1\left(L\right)$
    \footnote{
      $\theta_i: L \to L$ thus $j_1 \theta_i: L \to j_1(L)$
    }
    or all roots are in
    $j_1\left(L\right)$ and the polynomial
    $P_{min}\left(x, \bar{K}\right)$ splits over $j_1\left(L\right)$
    but this means that it splits over $L$
    \footnote{
      If $x = \alpha_1$ then $j_1 = id$ and $j_1(L) = L$
    }
  \end{proof}
\end{theorem}

\section{Galois extensions}

Now we are ready to give a definition for central object of Galois
theory

\begin{definition}[Galois extension]
  A Galois extension is a \mynameref{def:normalextension} and
  \mynameref{def:separableextension}.
  \label{def:galoisextension}
\end{definition}

\begin{theorem}
  Let $L$ be a finite over $K$ then the number of automorphisms
  $Aut\left(L/K\right)$ is less or equal to degree
  $\left[L:K\right]$:
  \[
  \left|Aut\left(L/K\right)\right| \le \left[L:K\right].
  \]
  The equality holds if and only if $L$ is
  \mynameref{def:galoisextension}.
  \label{thm:lec5_4}
  \begin{proof}
    We know that the group of automorphisms $Aut\left(L/K\right)$ acts
    freely (see definition \ref{def:freeaction}) on the set
    $Hom_K\left(L, \bar{K}\right)$,
    \footnote{
      Note that the number of distinct roots is equal to the number of
      distinct homomorphisms (see proposition \ref{prop:lec3_2}). Thus
      we can set an association between
      roots and homomorphisms. Proposition \ref{prop:stemfield}
      says that the stem
      field isomorphism (that translated to the considered
      automorphism) is unique determined by it's value on the roots that
      it exchanges. I.e. the the action of $Aut\left(L/K\right)$ on
      the set of roots (and as result on the set $Hom_K\left(L,
      \bar{K}\right)$) is exactly that one defined for
      \mynameref{def:freeaction}.

      See also first and second remarks at \ref{rem:lec5_onnormalext}.
    }
    so the number of
    automorphisms $\left|Aut\left(L/K\right)\right|$ is equal to the
    number of \mynameref{def:orbit} of this action which is less or
    equal
    \footnote{
      in ideal case each element has an unique orbit and the number of
      elements and the number of orbits are equal.
    }
    to the cardinality of the set it self:
    $\left|Hom_K\left(L, \bar{K}\right)\right|$. The equality holds
    whenever (if and only if) \mynameref{def:action} is
    \mynameref{def:transitive}.
    \footnote{
      This follows directly from the definition of
      \mynameref{def:transitive} i.e. $\forall h_1, h_2 \in
      Hom_K\left(L, \bar{K}\right)$ $\exists a \in
      Aut\left(L/K\right)$  such that $a(h_1) = h_2$.
    }
    We just seen in
    theorem \ref{thm:lec5_3} that this means that $L$ is normal over
    $K$. So we have
    \[
    \left|Aut\left(L/K\right)\right| \le
    \left|Hom_K\left(L, \bar{K}\right)\right| \le
    \left[L:K\right].
    \]
    The first inequality become equality if $L$ is normal and the
    second one if $L$ is separable
    \footnote{
      see definitions \ref{def:separabledegree} and
      \ref{def:separableextension}.
    }, thus
    \[
    \left|Aut\left(L/K\right)\right| \le \left[L:K\right]
    \]
    and equality holds if $L$ is both normal and separable i.e. if
    it's \mynameref{def:galoisextension}.
  \end{proof}
\end{theorem}

\begin{definition}[Set of invariants]
  If group $G$ acts on a set $X$ then $X^G = \left\{x
  \in X \mbox{ such that } gx = x, \forall g \in G\right\}$ - the
  set of invariants.
  \label{def:setinvariants}
\end{definition}

\begin{remark}[on normal extensions]
  \label{rem:lec5_onnormalext}
  If $L$ is normal over $K$ then
  \begin{enumerate}
  \item If we have an \mynameref{def:isomorphism} of sub-extensions
    ($K \subset L_1, L_2 \subset L$)
    $\phi: L_1 \cong L_2$ then it extends to an
    \mynameref{def:automorphism} of $L$.  To see this, we embed $L$ into
    an algebraic closure $\bar{K}$. And remark that $\phi$ extends to a
    map from $L_1$ to $\bar{K}$,
    \footnote{
      Ekaterina really said that but may be more better to say that
      $\phi$ extends to a map from $L$ to $\bar{K}$ ???
    }
    but all those maps have the same
    image (see theorem \ref{thm:lec5_3}), namely $L$
    \footnote{
      Consider $L_1 \subset L_2 \subset L \subset \bar{K}$ we have a
      homomorphism $\phi_1: L_1 \to L_2$ (our isomorphism) that
      accordingly theorem \ref{thm:lec2_3}, can be extended to
      homomorphism $L_1 \to \bar{K}$ and the last one can be extended
      to $\tilde{\phi_1}: L \to \bar{K}$. We can also consider
      $L_2 \subset L_1 \subset L \subset \bar{K}$ and homomorphism
      $\phi_2: L_2 \to L_1$ that can extended (using the same
      approach) to  $\tilde{\phi_2}: L \to \bar{K}$. Both
      $\tilde{\phi_{1,2}}$ has the same image $L$ as soon as $L$ -
      normal extension (see theorem \ref{thm:lec5_3}). Thus we have an
      \mynameref{def:automorphism}.
    }.
  \item \label{rem:item:lec5_onnormalext_2} The group of automorphisms $Aut\left(L/K\right)$ acts
    transitively on the roots of any irreducible polynomial $P \in
    K\left[X\right]$. Again, an isomorphism of stem fields extends to
    an automorphism of $L$
    \footnote{
      In the case we have \mynameref{def:transitive} i.e. for any roots
      $x,y$ we have an isomorphism of stem fields $\phi$ such that
      $\phi(x) = y$ (see proposition \ref{prop:stemfield}). This
      isomorphism extends to an automorphism accordingly the
      prev. remark.
    }.
  \item If the group $Aut\left(L/K\right)$ fixes (see definition
    \label{rem:item:lec5_onnormalext_3}
    \ref{def:fixedpoint}) some element $x \notin K$ then $x$ is pure
    inseparable (see definitions
    \ref {def:deginseppol} and \ref{def:degsepelem}) i.e.
    $P_{min}\left(x, K\right)$ has a
    single root $x$. Thus if $L$ is
    \mynameref{def:galoisextension} (i.e. is separable) then
    the set of elements which are fixed  by the automorphisms  of $L$
    over K is just K itself:
    $L^{Aut\left(L/K\right)} = K$
    (see definition \ref{def:setinvariants}).
    \footnote{
      Another explanation is the following. Let $x \in L \setminus K$
      then $g \in Aut\left(L/K\right)$ such that $g \ne id$ permutes
      roots of irreducible $P_{min}\left(x, K\right)$ and $g(x) = x'
      \ne x$. The only thing that works for
      $\forall g \in Aut\left(L/K\right)$ is $K$ i.e.
      $\forall k \in K$ and $\forall g \in Aut\left(L/K\right)$ we
      have $g(k) = k$. This is because $P_{min}\left(x, K\right) = X
      - k$ i.e. only one root possible.
    }
  \end{enumerate}
\end{remark}

\begin{definition}[Galois group]
  If $L$ is \mynameref{def:galoisextension} then Galois group $G =
  Gal\left(L/K\right)$ is the group of automorphisms $Aut\left(L/K\right)$.
  \label{def:galoisgroup}
\end{definition}

Thus we can write
\begin{equation}
  L^{Gal\left(L/K\right)} = K.
  \label{eq:lec5_2}
\end{equation}

\section{Artin's theorem}

Motivated by (\ref{eq:lec5_2}) let formulate and proof the important
theorem
\begin{theorem}[Artin]
  \label{thm:artin}
  $L$ is a field and $G \subset Aut\left(L\right)$
  \begin{enumerate}
  \item If $G$ acts with finite orbits,  so, I mean all orbits of G
    are finite (i.e. $\forall x \in L: \left|Orb(x)\right| < \infty$),
    then $L$ is a \mynameref{def:galoisextension} of $L^G$.
  \item If $\left|G\right| = n < \infty$ then
    $\left[L : L^G\right] = n$ and $G$ is a \mynameref{def:galoisgroup}
  \end{enumerate}

  \begin{remark}
    Well notice, that acting with finite orbits  and being finite is not
    the same thing. So, a short remark before giving a proof: notice
    that finite orbits does not mean finiteness because it's typical for
    Galois groups to act with finite orbits.  If we have some G, which
    is Galois of $L$ over $K$: $G = Gal\left(L/K\right)$, and
    $x \in L$, then $x$ is a root of a
    polynomial of some finite degree  and it's splitting field is
    finite over $K$, so, the orbit of $x$ is also finite
    because it's always sent to another root of the
    same polynomial and so consists of roots of  the
    $P_{min}\left(x, K\right)$. But of course the Galois group itself
    $Gal\left(L/K\right)$ can be infinite when $L$ is not finite over $K$. For
    instance, if  $K  = \mathbb{F}_p$ and $L = \overline{\mathbb{F}_p}$. It is
    very easy to compute all the Galois groups, and in fact we shall see
    shortly what is exactly this Galois group of $L$ over $K$ is infinite.
    \footnote{
      Section \ref{sec:lec6_finitefield} shows that
      $Gal\left(\bar{\mathbb{F}}_p/\mathbb{F}_p\right)$ is not cyclic,
      but theorem \ref{thm:lec3_2} says that if $G$ is finite than it
      should be cyclic. Thus we can conclude that
      $Gal\left(\bar{\mathbb{F}}_p/\mathbb{F}_p\right)$ is not finite
    }
  \end{remark}
  \begin{proof}
    \begin{enumerate}
    \item  Let me take $x$, well say, $x_1 \in L$ which is not
      $G$-invariant: $x_1 \in L \setminus L^G$ and
      $G$-\mynameref{def:orbit} of $x$
      $Orb(x) = \{x_1, x_2, \dots, x_k\}$. The
      polynomial $P\left(X\right) = \prod_{i=1}^k\left(X - x_i\right)$
      is $G$-invariant
      \footnote{
        I.e. $\forall g \in G: g\left(P\left(X\right)\right) =
        P\left(X\right)$
      }
      . $G$ just permutes  the $x_i$, it permutes the factors of these
      polynomial,
      so the polynomial is $G$-invariant. Therefore its coefficients are
      $G$-invariant and as result $P \in L^G\left[X\right]$ by
      definition \ref{def:setinvariants}. $L^G$ is a field of $G$
      invariants, and it is
      separable. $P$ is separable,
      because all $x_i$ are distinct ( there are distinct elements of the
      orbit ). And $L$ is splitting field of $P$, therefore $L$ is a
      \mynameref{def:galoisextension} over $L^G$ by the
      \mynameref{def:galoisextension} definition.
    \item We have $\left|G\right| = n$ then
      $\forall y \in L: \left|Orb\left(y\right)\right| \le n$. Take
      $x$ as above ($x \in L \setminus L^G$)
      $\left[L^G\left(x\right): L^G\right] \le n$.
      \footnote{
        $x$ is a root of the following polynomial $P\left(X\right) =
        \prod_{i=1}^k\left(X - x_i\right)$, where $x_i \in
        Orb(x)$. The polynomial has degree $\deg P =
        \left|Orb(x)\right| \le n$ and
        $\deg P_{min}\left(x, L^G\right) \le \deg P \le n$ therefore
        $\left[L^G\left(x\right): L^G\right] \le n$.
      }
      Claim that this
      implies $\left[L:L^G\right] \le n$. If I knew already, that $L$ is
      finite over $L^G$, this would be very easy, this would be just a
      direct consequence of \mynameref{thm:primitiveelement}. I
      would say that $L$ is generated by one element. I take this one element as
      my $x$ and I see that $L$ is of degree at most $n$ over $L^G$. But I
      don't know yet that $L$ is finite so I have to do some trick. So,
      proof of the claim:  take $x$ such that
      $\left[L^G\left(x\right):L^G\right]$ is maximal then take $y \in
      L$. $L^G\left(x,y\right)$ is finite over $L$
      \footnote{
        because $x,y$ are algebraic elements and using theorem
        \ref{thm:lec1_2} and proposition \ref{prop:lec1_1} we can
        conclude that $L^G\left(x,y\right)$ is finite over $L$.
      }
      and I can apply
      \mynameref{thm:primitiveelement}. Therefore
      $L^G\left(x,y\right) = L^G\left(z\right)$. But
      \[
      \left[L^G\left(x\right):L^G\right] \ge
      \left[L^G\left(z\right):L^G\right]
      \]
      thus $L^G\left(x\right) = L^G\left(z\right)$ so
      $y \in L^G\left(x\right)$ and since I can do this for any $y$, I
      eventually conclude that $L = L^G\left(x\right)$, and in
      particular, $\left[L: L^G\right] \le n$. Well, now if this is
      strictly less than $n$, then $L$ cannot have $n$ automorphisms
      over $L^G$ but $G \subset Aut\left(L/L^G\right)$ so this is a
      contradiction. Therefore $\left[L: L^G\right] = n$ and
      $G = Aut\left(L/L^G\right)$ (see theorem \ref{thm:lec5_4}).
    \end{enumerate}
  \end{proof}
\end{theorem}


\documentclass{article}
\usepackage[utf8]{inputenc}

\title{\large{\textsc{Dynamic Programming 01}}}
\date{}

\usepackage{natbib}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{mathtools}
\usepackage[a4paper, portrait, margin=0.8in]{geometry}

\usepackage{listings}

\newcommand\perm[2][n]{\prescript{#1\mkern-2.5mu}{}P_{#2}}
\newcommand\comb[2][n]{\prescript{#1\mkern-0.5mu}{}C_{#2}}
\newcommand*{\field}[1]{\mathbb{#1}}

\DeclarePairedDelimiter\ceil{\lceil}{\rceil}
\DeclarePairedDelimiter\floor{\lfloor}{\rfloor}

\newcommand{\Mod}[1]{\ (\text{mod}\ #1)}

\begin{document}

\maketitle

\subsection*{}

For the problems below, identify the subproblem, recurrence relation, and time complexity using dynamic programming. If you finish, both problems are on your homework, so you can begin coding them up.

%%%%% PROBLEMS %%%%

\begin{enumerate}

%%%%% PROBLEM 1 %%%%%
\item There are many cases in which it can be helpful to determine how different one string is from another.  Spellcheck programs use this concept to determine the most likely word attempted, biologists use it to determine how well one strand of DNA matches another, and machine translation programs use it to predict the accuracy of a generated sentence.

Given two strings $a$ and $b$, return the minimum number of operations required to convert string $a$ to string $b$. You can apply three different operations to the first string:

\begin{itemize}
    \item Insert a new character
    \item Delete and existing character
    \item Replace one character with another
\end{itemize}

For example:

\begin{lstlisting}
a = "sunday"
b = "saturday"

editDistance = 3

Steps: replace n with r, insert t, insert a
\end{lstlisting}

%%%%% PROBLEM 2 %%%%%
\item Monsters have captured Professor Nick, `N`, and imprisoned him in the bottom-right corner of the Academic Center.  The monsters also went ahead with the old remodelling plan, so the AC now consists of M x N rooms laid out in a 2D grid.  The rest of our valiant teaching team, `T`, will need to fight through the rooms of monsters in order to save Nick.

The teaching team has a health level indicated by the integer `h`.  If h drops to `0` or below at any point, everyone died, and Nick is stuck with the monsters.

Each room has either a monster or a health power up (e.g. coffee, monster fighting weapons, etc.) and will increase or decrease the health of the teaching team by the integer amount given for that room.

Given the 2D array of integers representing the health values of each room in the AC, assuming the teaching team starts in the top-left corner and Nick in the bottom-right corner, and assuming that the teaching team only moves right or down, return the **minimum initial health** required to save Professor Nick.  We need to know how much coffee to drink before attempting the rescue.

Example:

\begin{lstlisting}
-------------------------
| -2 (T) |  -3	|   3   |
-------------------------
|   -5   | -10  |   1   |
-------------------------
|   10   |  30  | -5 (S)|
-------------------------

map = {{-2, -3, 3}, {-5, -10, 1}, {10, 30, -5}}

minInitialHealth = 7, following the optimal path R -> R -> D -> D
\end{lstlisting}

%%%%% PROBLEM 3 %%%%%
\item Jingyi has a stack of coins and would like to split them up as evenly as possible with David. She is given a set of $N$ coins. Each of these coins has a given value. She must place each of these coins in one of two piles. Output the smallest possible difference between the total values of these two piles.

You are given

coins, an array of all the coins Jingyi must split
You will be filling out the function public int splitCoins(int[] coins) in $Coins.java$ and run tests in $CoinsTest.java$

For example, if she had the coins \{3,1,1,2,5,7\}, she can try to split them up into \{3,5\} and \{1,1,2,7\} and the difference between these two piles would be 3. The more optimal splitting is \{3, 2, 5\} and \{1, 1, 7\}, for a difference of 1.

\end{enumerate}

\newpage

\begin{center}
Solutions
\end{center}

%%%%% SOLUTIONS %%%%%
\begin{enumerate}

%%%%% Solution 1 %%%%%
\item
\begin{lstlisting}

Subproblem: String suffixes, cut off front letters (could also do back, but the
            suffixes concept provides a comparison to the text justification example
            from lecture)

Recurrence Relation: dp[i][j] = 1 + min(dp[i][j-1], dp[i-1][j], dp[i-1][j-1]);
                        min gets min DP of insert, remove, or replace

Runtime: O(MN), where m and n are the lengths of a and b

\end{lstlisting}

%%%%% Solution 2 %%%%%
\item
\begin{lstlisting}

Subproblem: min hp needed at position working back from target

Recurrence Relation: int down = Math.max(health[i + 1][j] - map[i][j], 1);
                     int right = Math.max(health[i][j + 1] - map[i][j], 1);
                     health[i][j] = Math.min(right, down);

Runtime: O(MN) where m and n are the dimensions of the dungeon

\end{lstlisting}

%%%%% Solution 3 %%%%%
\item
\begin{lstlisting}

Subproblem: Subproblem is DP\[i, j\], where i is the number of coins already placed
and j is the amount in the left pile

Guess: Do you put the coin in the left pile or not?

Recurrence Relation: DP\[i, j\] = whichever of (DP\[i+1, j\],
                                                DP\[i+1, j+coins\[i\]\])
                                is closer to sum(coins)/2

Runtime: O(ns) where n the number of coins and s is the sum of the coins

\end{lstlisting}

\end{enumerate}

\end{document}


\section{Feedback (informally)}
\subsection{}

\begin{frame}\mccz
\frametitleTC{Feedback and its magic}
\myPause
 \begin{columns}
  \column[T]{0.45\textwidth}
   \only<2->{\centering\includegraphics[height=6cm]{./Unit-02/img/Feedback-TheMagic_cc0.jpg}}\myPause%
  \column[T]{0.55\textwidth}
   \begin{itemize}[<+-| alert@+>]
   \item If PROPERLY used, feedback can
         \begin{itemize}[<+-| alert@+>]
         \item \vspace{2mm}counteract model errors\\
                and uncertainties,
         \item \vspace{2mm}reject a disturbance\\
                without the need\\
                for its measurement,
         \item \vspace{2mm}stabilise an unstable\\
               or ``runaway'' system.
         \end{itemize}
   \item \vspace{4mm}Let us see.
   \end{itemize}
 \end{columns}
\end{frame}

\begin{frame}
\frametitleTC{Feedback to counteract model errors/uncertainty and disturbances}
\framesubtitleTC{(to introduce this idea we do not use dynamic systems for simplicity)}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item \vspace{-8mm}Consider a controlled system such that the outcome $O$ depends on the control action $A$ and
       the disturbance $D$ as \textcolor{magenta}{$O=0.5(A+D)$.}
 \item If we want to use open-loop control and to make $O$ equal a reference $R$, we must solve for $A$ the equation
       $0.5(A+D)=R$, whence the control law \DG{$A=2R-D$.}
 \item Let us verify with some numbers:
 \item[] \begin{center}
          \includegraphics[width=0.60\columnwidth]{./Unit-02/img/Taxonomy-OpenLoop-Example-FullKnowledge.pdf}
          \hspace{6mm}\includegraphics[height=1.5cm]{./Unit-02/img/smile.png}
         \end{center}
 \end{itemize}
\end{frame}

\begin{frame}
\frametitleTC{Feedback to counteract model errors/uncertainty and disturbances}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item But what if we assume \textcolor{magenta}{$O=0.5(A+D)$} while the true system is
       \textcolor{magenta}{ $O=\mathbf{0.6}(A+D)$}?
 \item If we use the nominal control law \DG{$A=2D-R$} on the true system, we get the following:
 \item[] \begin{center}
          \includegraphics[width=0.65\columnwidth]{./Unit-02/img/Taxonomy-OpenLoop-Example-PartialKnowledge.pdf}
          \hspace{6mm}\includegraphics[height=1.5cm]{./Unit-02/img/frown.png}
         \end{center}
 \item \vfill H\'{e}las, 20\% error on a model parameter (0.6 instead of 0.5)\\
       $\Rightarrow$ 20\% error of $O$ wrt $R$.
 \item Open-loop control cannot counteract model errors.
 \item Let us try with feedback.
 \end{itemize}
\end{frame}

\begin{frame}
\frametitleTC{Feedback to counteract model errors/uncertainty and disturbances}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item \underline{EXAMPLE} feedback scheme with \TC{proportional} control (action $A$ proportional\\
       to the \TC{error} $R-O$); note that we do not compensate the disturbance:
 \item[] \begin{center}
          \includegraphics[width=0.60\columnwidth]{./Unit-02/img/Taxonomy-ClosedLoop-Example.pdf}
         \end{center}
 \item Joining the system and controller equations, for $O$ we get
       \begin{displaymath}
        \begin{array}{rcl}
         \left\{\begin{array}{rl}
          A &= K(R-O) \\
          O &= 0.6(A+D)
         \end{array}\right. &
         \Rightarrow &
          O = \frac{3K}{3K+5}R+\frac{3}{3K+5}D
         \end{array}
        \end{displaymath}
 \item Apparently, as $K \rightarrow \infty$, $O \rightarrow R$ $\forall D$.
 \item \vfill Yes, feedback can counteract model errors and disturbances;
 \item possible drawbacks on stability (improper use of feedback) later on.
 \end{itemize}
\end{frame}

\begin{frame}
\frametitleTC{Studying the stability of a system}
\framesubtitleTC{adopting here a \underline{very} informal attitude, where ``stable'' means ``left alone, does not
                 run amok''}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item Let us take our system, initialise $y(0)$ to $y_0$, let $u(k)=0$ $\forall k$ so that
       \begin{displaymath}
        y(k) = ay(k-1),
       \end{displaymath}
       and see what happens.
 \item \vfill Case 1: $|a|<1$, for example 0.5 or -0.5.\\
 \item[] For $k=0,1,2,\ldots$, we have respectively the $y$ sequences
       \begin{displaymath}
        \begin{matrix*}[r]
          y_0, &  0.5y_0, & 0.25y_0, &  0.125y_0, \ldots \\
          y_0, & -0.5y_0, & 0.25y_0, & -0.125y_0, \ldots
        \end{matrix*}
       \end{displaymath}
 \item[] For $k\rightarrow\infty$ the system's \TC{free motion} (no input) converges to zero\\
         $\Rightarrow$ we say the system is \TC{asymptotically stable.}
 \end{itemize}
\end{frame}

\begin{frame}
\frametitleTC{Studying the stability of a system}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item Case 2: $|a|=1$, i.e., $a=\pm 1$.\\
 \item[] For $k=0,1,2,\ldots$, we have respectively the $y$ sequences
       \begin{displaymath}
        \begin{matrix*}[r]
          y_0, &  y_0, & y_0, &  y_0, \ldots \\
          y_0, & -y_0, & y_0, & -y_0, \ldots
        \end{matrix*}
       \end{displaymath}
 \item[] For $k\rightarrow\infty$ the free motion neither converges to zero nor diverges\\
         $\Rightarrow$ we say the system is \TC{stable}, but not asymptotically.
 \item \vfill Case 3: $|a|>1$, for example 2 or -2.\\
 \item[] For $k=0,1,2,\ldots$, we have respectively the $y$ sequences
       \begin{displaymath}
        \begin{matrix*}[r]
          y_0, &  2y_0, & 4y_0, &  8y_0, \ldots \\
          y_0, & -2y_0, & 4y_0, & -8y_0, \ldots
        \end{matrix*}
       \end{displaymath}
 \item[] For $k\rightarrow\infty$ the free motion diverges (except for the one case $y_0=0$)\\
         $\Rightarrow$ we say the system is \TC{unstable.}
 \end{itemize}
\end{frame}

\begin{frame}
\frametitleTC{Stabilising a system by means of feedback}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item Take an unstable system:
       \begin{displaymath}
        y(k) = ay(k-1)+u(k-1), \qquad a=2.
       \end{displaymath}
 \item Feed $y$ back to $u$ proportionally, i.e., set $u(k) = Qy(k)$.
 \item Doing so we have the closed-loop system
       \begin{displaymath}
        y(k) = 2y(k-1)+Qy(k-1) = (2+Q)y(k-1)
       \end{displaymath}
       and if we chose $Q$ so that $|2+Q|<1$, this is asymptotically stable.
 \end{itemize}
\end{frame}

\begin{frame}
\frametitleTC{Stabilising a system by means of feedback}
\framesubtitleTC{and once again, tolerating model errors/uncertainties}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item Note that feedback control is also naturally \TC{robust} (for the moment,\\
       think of this as ``resilient to model errors'').
 \item For example, take $Q=-1.5$ so that $2+Q=0.5$.
 \item In this case the closed loop is asymptotically stable not only for $a=2$,\\
       but $\forall a$ s.t. $|a+Q|=|a-1.5|<1$, i.e., for $0.5<a<2.5$.
 \item \vspace{4mm} Summing up, with feedback we can impose stability even if we do not know\\
       the system (here, parameter $a$) precisely...
 \item[]\vspace{-1mm} ...but for the very same reasons, we might also \emph{destroy} stability.
 \item Feedback is a powerful weapon, use with care.
 \item \vfill And now, let us re-address feedback and dynamic systems\\
       with a formal attitude (soon focusing on the DT LTI case).
 \end{itemize}
\end{frame}