\documentclass[12pt]{article}

%\usepackage{fullpage}
\usepackage{amsmath,amssymb}

\begin{document}

\title{Two Dimensional Euler Simulation}

\author{
S.J. Montgomery-Smith\\
Department of Mathematics\\
University of Missouri\\
Columbia, MO 65211, U.S.A.\\
stephen@math.missouri.edu\\
http://www.math.missouri.edu/\~{}stephen}

\date{September 10, 2000}

\maketitle

This document describes a program I wrote to simulate the
two dimensional Euler Equation --- a program that is part
of the {\tt xlock} screensaver as the {\tt euler2d}
mode.  A similar explanation may also be found in the
book by Chorin \cite{C}.

\section{The Euler Equation}

The Euler Equation describes the motion of an incompressible
fluid that has no viscosity.  If the fluid is contained
in a domain $\Omega$ with boundary $\partial \Omega$, then
the equation is in the vector field $u$ (the velocity)
and the
scalar field $p$ (the pressure):
\begin{eqnarray*}
\frac{\partial}{\partial t} u &=& -u \cdot \nabla u + \nabla p \\
\nabla \cdot u &=& 0 \\
u \cdot n &=& 0 \quad \text{on $\partial \Omega$}
\end{eqnarray*}
where $n$ is the unit normal to $\partial \Omega$.

\section{Vorticity}

It turns out that it can be easier write these equations
in terms of the vorticity.  In two dimensions the vorticity
is the scalar $w = \partial u_2/\partial x - \partial u_1/\partial y$.
The equation for vorticity becomes
\[ \frac{\partial}{\partial t} w = -u \cdot \nabla w .\]
A solution to this equation can be written as follows.  The velocity
$u$ causes a flow, that is, a function $\varphi(t,x)$ that tells where
the particle initially at $x$ ends up at time $t$, that is
\[
\frac\partial{\partial t} \varphi(t,x)
= u(t,\varphi(t,x)) .\]
Then the equation
for $w$ tells us that the vorticity is ``pushed'' around by the flow,
that is, $w(t,\varphi(t,x)) = w(0,x)$.

\section{The Biot-Savart Kernel}

Now, once we have the vorticity, we can recover the velocity $u$ by
solving the equation
\begin{eqnarray*}
\partial u_2/\partial x - \partial u_1/\partial y &=& w \\
\nabla \cdot u &=& 0 \\
u \cdot n &=& 0 \quad \text{on $\partial \Omega$}.
\end{eqnarray*}
This equation is solved by using a Biot-Savart kernel $K(x,y)$:
$$ u(x) = \int_\Omega K(x,y) w(y) \, dy .$$
The function $K$ depends upon the choice of domain.  First let us consider
the case when $\Omega$ is the whole plane (in which case the boundary
condition $u \cdot n = 0$ is replaced by saying that $u$ decays at infinity).
Then
\begin{equation*}
K(x,y) = K_1(x,y) = c \frac{(x-y)^\perp}{|x-y|^2} .
\end{equation*}
Here $x^\perp = (-x_2,x_1)$, and $c$ is a constant, probably something
like $1/2\pi$.  In any case we will set it to be one, which in effect
is rescaling the time variable, so we don't need to worry about it.

We can use this as a basis to find $K$ on the unit disk
$\Omega = \Delta = \{x:|x|<1\}$.  It turns out to be
\begin{equation*}
K_2(x,y) = K_1(x,y) - K_1(x,y^*) ,
\end{equation*}
where $y^* = y/|y|^2$ is called the reflection of $y$ about the
boundary of the unit disk.

Another example is if we have a bijective analytic function
$p:\Delta \to {\mathbb C}$, and we let $\Omega = p(\Delta)$.
(Here we think of $\Delta$ as a subset of $\mathbb C$, that is,
we are identifying the plane with the set of complex numbers.)
In that case we get
\[ K_p(p(x),p(y)) = K_2(x,y)/|p'(x)|^2 .\]
Our simulation considers the last case.  Examples of such
analytic functions include series
$p(x) = x + \sum_{n=2}^\infty c_n x^n$, where
$\sum_{n=2}^\infty n |c_n| \le 1$.
(Thanks to David Ullrich for pointing this out to me.)

\section{The Simulation}

Now let's get to decribing the simulation.  We assume a rather
unusual initial distribution for the vorticity --- that the
vorticity is a finite sum of dirac delta masses.
\[ w(0,x) = \sum_{k=1}^N w_k \delta(x-x_k(0)) .\]
Here $x_k(0)$ is the initial place where the points
of vorticity are concentrated, with values $w_k$.
Then at time $t$, the vorticity becomes
\[ w(t,x) = \sum_{k=1}^N w_k \delta(x-x_k(t)) .\]
The points of fluid $x_k(t)$ are pushed by the
flow, that is, $x_k(t) = \varphi(t,x_k(0))$, or
\[ \frac{\partial}{\partial t} x_k(t) = u(t,x_k(t)) .\]
Putting this all together, we finally obtain the equations
\[ \frac{\partial}{\partial t} x_k = \alpha_k \]
where
\[ \alpha_k   = \sum_{l=1}^N w_l K(x_k,x_l) .\]
This is the equation that our simulation solves.

In fact, in our case, where the domain is $p(\Delta)$,
the points are described by points
$\tilde x_k$, where $x_k = p(\tilde x_k)$.  Then
the equations become
\begin{eqnarray}
\label{tildex-p1}
\frac{\partial}{\partial t} \tilde x_k &=& \tilde\alpha_k \\
\label{tildex-p2}
\tilde\alpha_k &=& \frac1{|p'(\tilde x_k)|^2}
     \sum_{l=1}^N w_l K_2(\tilde x_k,\tilde x_l) .
\end{eqnarray}

We solve this $2N$ system of equations using standard
numerical methods, in our case, using the second order midpoint method
for the first step, and thereafter using the second order Adams-Bashforth
method.  (See for example the book
by Burden and Faires \cite{BF}).

\section{The Program - Data Structures}

The computer program solves equation (\ref{tildex-p1}), and displays
the results on the screen, with a boundary.  All the information
for solving the equation and displaying the output is countained
in the structure {\tt euler2dstruct}.  Let us describe some of
the fields in {\tt euler2dstruct}.
The points $\tilde x_k$ are contained
in {\tt double *x}: with the coordinates of
$\tilde x_k$ being the two numbers
{\tt x[2*k+0]}, {\tt x[2*k+1]}.  The values $w_k$ are contained
in {\tt double *w}.  The total number of points is
{\tt int N}.  (But only the first {\tt int Nvortex} points
have $w_k \ne 0$.)  The coefficients of the analytic function
(in our case a polynomial) $p$
are contained in {\tt double p\_coef[2*(deg\_p-1)]} --- here
{\tt deg\_p} is the degree of $p$, and the real and imaginary
parts of the coefficient
$c_n$ is contained in {\tt p\_coef[2*(n-2)+0]} and {\tt p\_coef[2*(n-2)+1]}.

\section{Data Initialization}

The program starts in the function {\tt init\_euler2d}.  After allocating
the memory for the data, and initialising some of the temporary variables
required for the numerical solving program, it randomly assigns the
coefficients of $p$, making sure that $\sum_{n=2}^{\tt deg\_p} n |c_n| = 1$.
Then the program figures out how to draw the boundary, and what rescaling
of the data is required to draw it on the screen.  (This uses the
function {\tt calc\_p} which calculates $p(x)$.)

Next, it randomly assigns the initial values of $\tilde x_k$.  We want
to do this in such a way so that the points are uniformly spread over the
domain.  Let us first consider the case when the domain is the unit circle
$\Delta$.  In that case the proportion of points that we would expect
inside the circle of radius $r$ would be proportional to $r^2$.  So
we do it as follows:
\[ r = \sqrt{R_{0,1}},\quad \theta = R_{-\pi,\pi}, \quad
   \tilde x_k = r (\cos \theta, \sin \theta) .\]
Here, and in the rest of this discussion, $R_{a,b}$ is a function
that returns a random variable uniformly distributed over the interval
$[a,b]$.

This works fine for $\Delta$, but for $p(\Delta)$, the points
$p(\tilde x_k)$ are not uniformly distributed over $p(\Delta)$,
but are distributed with a density proportional to
$1/|p'(\tilde x_k)|^2$.  So to restore the uniform density we need
to reject this value of $\tilde x_k$ with probability proportional
to $|p'(\tilde x_k)|^2$.  Noticing that the condition
$\sum_{n=2}^{\tt deg\_p} n |c_n| = 1$ implies that
$|p'(\tilde x_k)| \le 2$, we
do this by rejecting if $|p'(\tilde x_k)|^2 < R_{0,4}$.
(This makes use of the function {\tt calc\_mod\_dp2} which calculates
$|p'(x)|^2$.)

\section{Solving the Equation}

The main loop of the program is in the function {\tt draw\_euler2d}.
Most of the drawing operations are contained in this function, and
the numerical aspects are sent to the function {\tt ode\_solve}.
But there is an aspect of this that I would like
to discuss in the next section, and so we will look at a simple method for
numerically solving differential equations.

The Euler Method
(nothing to do with the Euler Equation), is as
follows.  Pick a small number $h$ --- the time step (in
the program call {\tt delta\_t}).  Then we approximate
the solution of the equation:
\begin{equation}
\label{method-simple}
\tilde x_k(t+h) = \tilde x_k(t) + h \tilde\alpha_k(t) .
\end{equation}
The more sophisticated methods we use are variations of
the Euler Method, and so the discussion in the following section
still applies.

In the program, the quantities $\tilde\alpha_k$, given by
equations (\ref{tildex-p2}) are calculated by the function
{\tt derivs}
(which in turns calls {\tt calc\_all\_mod\_dp2} to
calculate $|p'(\tilde x_k)|^2$ at all the points).

\section{Subtle Perturbation}

Added later: the scheme described here seems to not be that effective,
so now it is not used.

One problem using a numerical scheme such as the Euler Method occurs
when the points $\tilde x_k$ get close to the boundary
of $\Delta$.  In that case, it is possible that the new
points will be pushed outside of the boundary.  Even if they
are not pushed out of the boundary, they may be much closer
or farther from the boundary than they should be.
Our system of equations is very sensitive to how close points
are to the boundary --- points with non-zero vorticity
(``vortex points'') that are close to the boundary travel
at great speed alongside the boundary, with speed that is
inversely proportional to the distance from the boundary.

A way to try to mitigate this problem is something that I call
``subtle perturbation.''
We map the points in
the unit disk to points in the plane using the map
\begin{equation*}
F(x) = f(|x|) \frac x{|x|} ,
\end{equation*}
where $f:[0,1]\to[0,\infty]$ is an increasing continuous
bijection.  It turns out that a good choice is
\begin{equation*}
f(t) = -\log(1-t) .
\end{equation*}
(The reason for this is that points close to each other
that are a distance
about $r$ from the boundary will be pushed around so that
their distance from each other is about multiplied by the
derivative of $\log r$, that is, $1/r$.)
Note that the inverse of this function is given by
\begin{equation*}
F^{-1}(x) = f^{-1}(|x|) \frac x{|x|} ,
\end{equation*}
where
\begin{equation*}
f^{-1}(t) = 1-e^{-t} .
\end{equation*}

So what we could do is the following: instead of working with
the points $\tilde x_k$, we could work instead with the points
$y_k = F(\tilde x_k)$.  In effect this is what we do.
Instead of performing the computation (\ref{method-simple}),
we do the calculation
\begin{equation*}
y_k = F(\tilde x_k(t)) + h {\cal A}(\tilde x_k) \tilde\alpha_k(t)
\end{equation*}
where
${\cal A}(x)$ is the matrix of partial derivatives of $F$:
\begin{equation*}
{\cal A}(x) =
\frac{f(|x|)}{|x|}
\left[
\begin{matrix}
1 & 0\\
0 & 1
\end{matrix}
\right]
+ \frac1{|x|}
  \left(\frac{f'(|x|)}{|x|} - \frac{f(|x|)}{|x|^2}\right)
\left[
\begin{matrix}
x_{1}^2   & x_{1} x_{2}\\
x_{1} x_{2} & x_{2}^2
\end{matrix}
\right],
\end{equation*}
and then compute
\begin{equation*}
\tilde x_k(t+h) = F^{-1}(y_k).
\end{equation*}
These calculations are done in the function {\tt perturb}, if
the quantity {\tt SUBTLE\_PERTURB} is set.

\section{Drawing the Points}

As we stated earlier, most of the drawing functions are contained
in the function {\tt draw\_euler2d}.  If the variable
{\tt hide\_vortex} is set (and the function {\tt init\_euler2d}
will set this with probability $3/4$), then we only display
the points $\tilde x_k$ for ${\tt Nvortex} < k \le N$.  If
{\tt hide\_vortex} is not set, then the ``vortex points''
$\tilde x_k$ ($1 \le k \le {\tt Nvortex}$) are displayed in white.
In fact the points $p(\tilde x_k)$ are what are put onto the screen,
and for this we make use of the function {\tt calc\_all\_p}.

\section{Addition to Program: Changing the Power Law}

A later addition to the program adds an option {\tt eulerpower},
which allows one to change the power law that describes how
the vortex points influence other points.  In effect, if this
option is set with the value $m$, then the Biot-Savart Kernel
is replace by
$$ K_1(x,y) = \frac{(x-y)^\perp}{|x-y|^{m+1}}, $$
and
$$ K_2(x,y) = K_1(x,y) - |y|^{1-m} K_1(x,y) .$$
So for example, setting $m=2$ corresponds to the
quasi-geostrophic equation.  (I haven't yet figured out
what $K_p$ should be, so if $m \ne 1$ we use the unit circle
as the boundary.)

\begin{thebibliography}{9}

\bibitem{BF} Richard L. Burden, J. Douglas Faires, Numerical Analysis,
sixth edition, Brooks/Cole, 1996.

\bibitem{C} Alexandre J. Chorin, Vorticity and Turbulence,
Applied Mathematical Sciences, Vol 103, Springer Verlag, 1994.

\end{thebibliography}

\end{document}


\chapter{Derivation of Pipe-flow Process Model}
\label{AppendixB}
Since the derivation of the model is based on physical relationships and conservation principles it would be convenient to study at least once the consecutive steps performed in order to obtain it. This may provide reader with better understanding of the process as well as present assumptions and approximations that are utilized. The model is derived starting from two equations: equation of continuity and equation of motion. The derivation provided below is based on %\cite{billmann_isermann,keerthi_phd}.

\section{Equation of Continuity}

We start from definition of the mass:

\begin{equation}
\label{eq:mass}
m = \rho A \Delta z
\end{equation}

Considering mass change in time, i. e. mass-flow

\begin{equation}
\label{eq:massflow}
q = \frac{\partial m}{\partial t} = \rho A  w
\end{equation}

and then, introducing principle of conservation for elementary pipe segment we obtain

\begin{equation}
\label{eq:massflow2}
\frac{\partial m}{\partial t} = \rho A w - A\left(w + \frac{\partial w}{\partial z}\Delta z\right) \left( \rho + \frac{\partial \rho}{\partial z}\Delta z\right)
\end{equation}

Substituting (\ref{eq:mass}) to  (\ref{eq:massflow2}) we obtain

\begin{equation}
\label{eq:massflow3}
\frac{\partial \left( \rho A \Delta z \right)}{\partial t} = \rho A  w - A\left( \rho w + \Delta z \left( \rho \frac{\partial w}{\partial z} +w \frac{\partial \rho}{\partial z} \right)+\frac{\partial w}{\partial z} \Delta z  \frac{\partial \rho}{\partial z} \Delta z  \right)
\end{equation}

where the last term $\frac{\partial w}{\partial z} \Delta z  \frac{\partial \rho}{\partial z} \Delta z$ is small enough to be neglected. Then, substituting

\begin{equation}
\label{eq:ms4}
\rho \frac{\partial w}{\partial z} +w \frac{\partial \rho}{\partial z} = \frac{\partial}{\partial z} \left( \rho w \right)
\end{equation}

as

\begin{equation}
\label{eq:ms5}
\frac{\partial}{\partial z} \left( \rho w \right) + \frac{\partial \rho}{\partial t} = 0
\end{equation}

Assuming, that the process is isothermal we can introduce

\begin{equation}
\label{eq:ms6}
\nu = \sqrt{\frac{p}{\rho}}
\end{equation}

thus, substituting (\ref{eq:massflow}) and (\ref{eq:ms6}) to (\ref{eq:ms5}) we obtain first equation of our model:

\begin{equation}
\label{eq:cont_fin3}
\frac{A}{\nu^2} \frac{\partial p}{\partial t} + \frac{\partial q}{\partial z} = 0
\end{equation}

\section{Equation of Motion}

Here we start from definition of momentum:

\begin{equation}
\label{eq:mm1}
M = \rho A \Delta z w
\end{equation}

then,using principle of conservation:

\begin{equation}
\label{eq:mm2}
\frac{\partial \left( \rho A w \Delta z \right)}{\partial t} = A \left(p + \frac{\rho w^2}{2} \right) - A\left(p + \frac{\partial p}{\partial z} \Delta z + \frac{\rho w^2}{2} + \frac{\partial}{\partial z} \left( \frac{\rho w^2}{2} \right) \Delta z \right) - F - Y
\end{equation}

By rearranging equation presented above, we obtain:

\begin{equation}
\label{eq:mm3}
\frac{\partial \left( \rho A w \Delta z \right)}{\partial t} = A \left(p + \frac{\rho w^2}{2} \right) - A\left(\left(p + \frac{\rho w^2}{2} \right) + \Delta z \left( \frac{\partial p}{\partial z} +  \frac{\partial}{\partial z} \left( \frac{\rho w^2}{2} \right) \right)\right) - F - Y
\end{equation}

Substituting

\begin{equation}
\label{eq:mm4}
 \frac{\partial}{\partial z} \left(p +  \frac{\rho w^2}{2} \right) = \frac{\partial p}{\partial z} +  \frac{\partial}{\partial z} \left( \frac{\rho w^2}{2} \right)
\end{equation}

into (\ref{eq:mm3}) we will obtain
\begin{equation}
\label{eq:mm5}
\frac{\partial \left( \rho  w  \right)}{\partial t} = -  \frac{\partial}{\partial z} \left(p +  \frac{\rho w^2}{2} \right) - F- Y
\end{equation}

Again, assuming that the process is isothermal we obtain:

\begin{equation}
\label{eq:mm_fin}
\frac{1}{A} \frac{\partial q}{\partial t} + \left( 1 - \frac{q^2 \nu^2}{2 A^2 p}\right) \frac{\partial p}{\partial z} + \frac{q \nu^2}{A^2 p} \frac{\partial q}{\partial z}= - \frac{\lambda \nu^2}{2DA^2} \frac{q|q|}{p} - \frac{g sin \alpha}{\nu^2} p
\end{equation}

Taking into account, that if $w^2 \ll \nu^2$, i. e. velocity of the fluid is significantly lower than velocity of the sound in this fluid, we may neglect term $ \frac{q^2 \nu^2}{2 A^2 p}$. Moreover, if the pipeline is long, then we may neglect term $ \frac{q \nu^2}{A^2 p}$. Thus, we obtain second equation that is included in physical, continuous time model:

\begin{equation}
\label{eq:momen_fin3}
\frac{1}{A} \frac{\partial q}{\partial t} + \frac{\partial p}{\partial z} = - \frac{\lambda \nu^2}{2DA^2} \frac{q|q|}{p} - \frac{g sin \alpha}{\nu^2} p
\end{equation}


\documentclass[12pt]{article}

\title{Tic-Tac-Toe with SAT}
\author{William John Holden}
\date{\today}

\usepackage{amsmath,amsthm}
\usepackage{mathtools}
%\usepackage{parskip}
\usepackage{amssymb}
\usepackage[margin=1.0in]{geometry}
\usepackage[hyphens]{url}
\usepackage[hidelinks]{hyperref}
\usepackage{listings}
\lstset{basicstyle=\ttfamily,columns=flexible,frame=single,breaklines=true,linewidth=1\textwidth}

\newcommand{\tictactoe}[9]{\begin{center}\begin{tabular}{ c | c | c }{$#1$} & {$#2$} & {$#3$} \\ \hline {$#4$} & {$#5$} & {$#6$} \\ \hline {$#7$} & {$#8$} & {$#9$} \end{tabular}\end{center}}

\begin{document}

\maketitle

\section{Introduction}

In this exercise we will construct a series of boolean satisfiability clauses to find a winning move for the game Tic-Tac-Toe.
Throughout this exercise, assume that we are playing as $x$ and that it is our turn to play for any game shown.
We treat the SAT solver as a ``black box'' and will not worry about how it works.
A SAT solver accepts a \textit{formula} of \textit{clauses} containing boolean \textit{variables} in \textit{conjunctive normal form} (CNF).
An example of a formula in CNF is

\begin{equation}(A \vee \neg B)(C)(C \vee D \vee E)(B \vee \neg C).\end{equation}

This formula is \textit{satisfiable} with the literals $A=T$, $B=T$, $C=T$, $D=F$, and $E=F$.
This is not the only satisfying assignment.
$D$ and $F$ may each take the values $T$ or $F$, but we are constrained in $C=T$.
If $C=F$ then the clause $(C)$ cannot be satisfied, and any unsatisfied clause falsifies the entire formula.

Some formulas cannot be satisfied under any assignment of literals to variables. An obvious example is $(A)(\neg A)$.
A less obvious example is

\begin{equation}(A \vee \neg B)(B \vee \neg C)(C)(\neg A \vee \neg B \vee \neg C).\end{equation}

In this case, the $(C)$ clause implies $C=T$, which means $B=T$, which then implies $A=T$, but now the final clause is $(F \vee F \vee F)$ which falsifies the formula.

A \textit{SAT solver} is a computer program for finding a set of satisfying literals or proving if no such satisfying assignment exists.
Though the boolean satisfiability problem is NP-complete, state of the art SAT solvers are very efficient in practice.
Martin Horenovsky has a very nice article at \url{https://codingnest.com/modern-sat-solvers-fast-neat-underused-part-1-of-n/} on this subject.
I originally learned of the satisfiability problem and SAT solvers attending a course on NP-Complete Problems from Alexander Kulikov (\url{https://www.edx.org/course/np-complete-problems-uc-san-diegox-algs203x}).

Others have successfully solved Sudoku puzzles using SAT solvers.
In this exercise, we will do the same for tic-tac-toe.
Of course, tic-tac-toe is much less computationally demanding than Sudoku.
Tic-tac-toe has a smaller board ($3 \times 3$ instead of $9 \times 9$) and it has fewer constraints.
If we consider the size of the tic-tac-toe puzzle to be $n$ for dimensions $\sqrt{n} \textrm{ rows} \times \sqrt{n} \textrm{ columns} = x \times y$ then we can iterate over every row and column to find a winning move in

\begin{align}O(x \times y) + O(y \times x ) + O(2 \textrm{ diagonals} \times \sqrt{n}) &= \\
O(xy)+O(xy)+O(2\sqrt{n})
&=O(n).
\end{align}

Using an exponential time tool for a linear time problem may seem crazy, but the goal here is to learn to \textit{reduce} problems to SAT.

\section{Reduction to SAT}

\subsection{Variables}

We need to express the positions of our tic-tac-toe board as boolean variables. This is pretty easy. We assign a variable letter to each position in reading order:

\tictactoe{a}{b}{c}{d}{e}{f}{g}{h}{i}

There are actually \textit{three} possible states for each position: X, O, and unassigned. However, in this problem we do not need to concern ourselves with the third state.
We will consider a variable to be ``true'' if it is marked wtih X and false otherwise.

\subsection{Contraint 1: Winning Combinations}

There are eight total winning combinations in tic-tac-toe: satisfy any of the three rows, any of the columns, or either of the diagonals.
I should note that the word \textit{or} in the previous sentence is an inclusive or; winning combinations allow for both a row and a column, both a row and a diagonal, and both a column and a diagonal. For example, the assignment

\tictactoe{o}{o}{x}{o}{o}{x}{x}{x}{}

can be won by setting $i=x$.  We can express the eight winning combinations as

\begin{equation}(a \wedge b \wedge c) \vee (d \wedge e \wedge f) \vee (g \wedge h \wedge i)
\vee
(a \wedge d \wedge g) \vee (b \wedge e \wedge h) \vee (c \wedge f \wedge i)
\vee
(a \wedge e \wedge i) \vee (c \wedge e \wedge g).\end{equation}

This long formula is in \textit{disjunctive normal form} (DNF) and not the CNF we want.
We could use rules of discrete mathematics, such as DeMorgan's Law and Distributivity, but in this paper on computational tools let us instead use \textit{Mathematica's} \texttt{BooleanConvert} function (\url{https://reference.wolfram.com/language/ref/BooleanConvert.html}).
\texttt{BooleanConvert} accepts an expression and converts it to the desired form.
(\textit{Mathematica} functionality is available at \url{https://www.wolframalpha.com}).

\begin{lstlisting}
In[1] := BooleanConvert[((a && b && c) || (d && e && f) || (g && h && i) || (a && d && g) || (b && e && h) || (c && f && i) || (a && e && i) || (c && e && f)),"CNF"]
Out[1] := (a || b || c || d || g) && (a || b || f || g) && (a || b || f ||  i) && (a || c || d || h) && (a || c || e || h) && (a || e || f || g) && (a || e || i) && (a || f || h) && (b || c || d || i) && (b || d || e || f || h) && (b || d || e || i) && (b || d || f ||  i) && (b || e || f || g) && (b || e || g || i) && (b || f || g || i) && (c || d || e || h) && (c || d || e || i) && (c || d || h || i) && (c || e || g) && (c || f || g || h || i)
\end{lstlisting}

Now we have our first set of constraints that we can provide to a SAT solver.
An assignment of literals to the variables $a$ through $i$ is a winning move if all three variables in any row, column, or diagonal are $x$.

\subsection{Contraint 2: Board Configuration}

The game will be shown with some configuration of $x$ and $o$ settings on the board.
For each position with an $x$ we will create a clause of unit length where that variable must be true.
All positions marked $o$ induce a clause where that variable must be false.
For example, given the game

\tictactoe{x}{o}{x}{o}{x}{o}{o}{}{}

we will set $a=T$, $b=F$, $c=T$, $d=F$, $e=T$, $f=F$, $g=F$ with the clauses

\begin{equation}(a)(\neg b)(c)(\neg d)(e)(\neg f)(\neg g).\end{equation}

These clauses must be generated based on game state.

\subsection{Constraint 3: One Move}

We assume that it is our turn to play, and we only get to make one move.
One might be tempted to construct an ``exactly-one-of'' clause using the \textit{exclusive or} operation.
However, this is not correct.
Consider $A \oplus B \oplus C$ for $A = B = C = T$. Then $A \oplus B = F$, but $(A \oplus B) \oplus C = (F) \oplus T = T$.
We need a different ``gadget'' to constrain our SAT solver to mark only one square of our tic-tac-toe board with $x$.

\end{document}


\subsection{Fermat's method}

Identify the integer as the difference of two squares, and use this.

\(x=a.b\)

We use the midpoint of the two as \(c=\dfrac{a+b}{2}\)

This only works for odd numbers. If we have

The we have:

\begin{itemize}
\item \(a=c+d\)
\item \(b=c-d\)
\item \(x=(c+d)(c-d)\)
\item \(x=c^2-d^2\)
\end{itemize}

We can test this by trying \(a\) to get \(a^2-x\), and seeing if this is a square number.


\documentclass[a4paper]{article}

\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
\usepackage{amsmath}
\usepackage{graphicx}

\title{MATH 542 Homework 10}
\author{Saket Choudhary\\skchoudh@usc.edu}

\begin{document}
\maketitle
\subsection*{Problem 3c.1}

\subsubsection*{Problem 3c.1.a}
\begin{align*}
var[S^2]  &= Var[\frac{Y'(I_n-P)Y}{n-p}]\\
&= \frac{1}{(n-p)^2}Var[Y'(I_n-P)Y]\\
&= \frac{1}{(n-p)^2}\times (2\sigma^4(n-p))\\
&= \frac{2\sigma^4}{n-p}
\end{align*}

\subsubsection*{Problem 3c.1.b}
\begin{align*}
A_1 &= \frac{1}{n-p+2}[I_n-X(X'X)^{-1}X']\\
&= \frac{R}{n-p+2}\\
E[(Y'A_1Y-\sigma^2)^2] &= Var(Y'A_1Y-\sigma^2) + (E[Y'A_1Y-\sigma^2])^2\\
&= Var(Y'A_1Y) + (E[Y'A_1Y]-\sigma^2)^2\\
&= \frac{Var(Y'RY)}{(n-p+2)^2} + ( \frac{E[Y'RY]}{(n-p+2)}-\sigma^2)^2\\
&= \frac{2\sigma^4(n-p)}{(n-p+2)^2} + (\frac{\sigma^2(n-p)}{n-p+2}-\sigma^2)^2 \text{ using 3.12 from textbook}\\
&= \frac{2\sigma^4(n-p)}{(n-p+2)^2} + \frac{4\sigma^4}{(n-p+2)^2}\\
&= \frac{2\sigma^4}{n-p+2}
\end{align*}

\subsubsection*{Problem 3c.1.c}
\begin{align*}
E[Y'A_1Y] &= \frac{E[Y'RY]}{n-p+2}\\
&= \frac{\sigma^2(n-p)}{n-p+2}\text{ using 3.12 from textbook}\\
MSE[Y'A_1Y] &= E[(Y'A_1Y-\sigma^2)^2]\\
&= \frac{2\sigma^4}{n-p+2}\\
MSE[S^2] &= E[S^2-(E[S^2])^2]\\
&= Var(S^2)\\
&= \frac{2\sigma^4}{n-p}\\
&<\frac{2\sigma^4}{n-p+2}\\
&\leq MSE[Y'A_1Y]
\end{align*}

\section*{Problem 3d.1}
\subsubsection*{Problem 3d.1.a}
Given $Y_i \sim N(\theta, \sigma^2)$ or $Y_i = \theta + \epsilon_i$ where $\epsilon_i \sim N(0, \sigma^2)$ \\
$\mathbf{Y} = \mathbf{1_n}\theta + \mathbf{\epsilon}$ thus $\hat{\theta} = (\mathbf{1_n}'\mathbf{1_n})^{-1}\mathbf{1_n'}Y = \frac{1}{n}\mathbf{1_n'}Y  = \bar{Y}$

Thus,  using theorem  $3.5(ii)$ $\bar{Y}$ and $S^2=\sum_i(Y_i-\bar{Y})^2$ are independent

\subsubsection*{Problem 3d.1.b}
Borrowing from part (a)  we have: $RSS=Q=\sum_i(Y_i-\bar{Y})^2$ $\implies$ using  theorem $3.5(iii)$:

$RSS/\sigma^2 \sim \chi^2_{n-1}$

\subsection*{Problem 3d.2}
\begin{align*}
RSS &= Y'(I_n-P)Y\\
&= Y'(I_n-P)Y-\beta'X'(I-P)(Y-X\beta)+Y'(I-P)(-X\beta) \text{ both terms are zero using PX=P and P=P'} \\
&= (Y-X\beta)'(I_n-P)(Y-X\beta)\\
&= \epsilon'(I_n-P)\epsilon
\end{align*}

\begin{align*}
(\hat{\beta}-\beta)'X'X(\hat{\beta}-\beta) &= Z'Z\\
Z &= X(\hat{\beta}-\beta)\\
&= X((X'X)^{-1}X'Y-(X'X)^{-1}X'X\beta)\\
&= P(Y-X\beta)\\
&= P\epsilon\\
(\hat{\beta}-\beta)'X'X(\hat{\beta}-\beta) &= \epsilon'P'P\epsilon
\end{align*}

\begin{align*}
Cov[RSS, (\hat{\beta}-\beta)'X'X(\hat{\beta}-\beta)] &= Cov[\epsilon'(I_n-P)\epsilon, \epsilon'P'P\epsilon]\\
&=  Cov[\epsilon'(I_n-P)\epsilon, \epsilon'PP\epsilon] \text{ using } P'=P\\
&=  Cov[\epsilon'(I_n-P)\epsilon, \epsilon' P\epsilon] \text{ using } PP = P\\
&= \sigma^2(I-P)P \\
&= 0
\end{align*}
Thus, $RSS$ and $(\hat{\beta}-\beta)'X'X(\hat{\beta}-\beta)$ are indepedent

\subsection*{Problem 3.12}

\begin{align*}
Y &= X\beta + \epsilon\\
\bar{Y} &= \frac{1}{n}\mathbf{1_n}Y\\
\sum_i(Y_i-\hat{Y_i})^2  &= (Y-X\hat{\beta})'(Y-X\hat{\beta})\\
&= (Y-X(X'X)^{-1}X'Y)'(Y-X(X'X)^{-1}X'Y)\\
&= (Y-PY)'(Y-PY)\\
&= Y'(I-P)'(I-P)Y\\
&= Y'(I-P)Y\text{ using idempotency of } I-P\\
Cov[\frac{1}{n}\mathbf{1_n}Y, (I-P)Y]\\
&= \frac{1}{n}\mathbf{1_n}Cov[Y](I-P)'\\
&= \sigma^2(n-p)\frac{1}{n}\mathbf{1_n}(I-P)'
\end{align*}
Since the first column of the design matrix is all 1, $1_n$ belongs to the column space of $X$  and is orthogonal to $(I-P)'$ (P being the projection matrix) $\implies Cov[\frac{1}{n}\mathbf{1_n}Y, (I-P)Y] = 0$
\end{document}


\lab{Poisson's equation}{Poisson's equation}
\label{lab:poisson2d}

Suppose that we want to describe the distribution of heat throughout a region $\Omega$.
Let $h(x)$ represent the temperature on the boundary of $\Omega$ ($\partial \Omega$), and let $g(x)$ represent the initial heat distribution at time $t = 0$.
If we let $f(x,t)$ represent any heat sources/sinks in $\Omega$, then the flow of heat can be described by the boundary value problem (BVP)
\begin{align}
	\begin{split}
		& { } u_t = \triangle u + f(x,t), \quad x \in \Omega, \quad t >0,\\
		& { }u(x,t) = h(x), \quad x \in \partial \Omega, \\
		& { }u(x,0) = g(x).
	\end{split}
\end{align}
When the source term $f$ does not depend on time, there is often a steady-state heat distribution $u_{\infty}$ that is approached as $t \to \infty$.
This steady state $u_{\infty}$ is a solution of the BVP
\begin{align}
	\begin{split}
		& { }  \triangle u + f(x) = 0, \quad x \in \Omega,\\
		& { }u(x,t) = h(x), \quad x \in \partial \Omega.
	\end{split}
\end{align}

This last partial differential equation, $\triangle u = -f$, is called Poisson's equation.
This equation is satisfied by the steady-state solutions of many other evolutionary processes.
Poisson's equation is often used in electrostatics, image processing, surface reconstruction, computational fluid dynamics, and other areas.

\section*{Poisson's equation in two dimensions}

 Consider Poisson's equation together with Dirichlet boundary conditions on a square domain $R = [a,b]\times [c,d]$:
 \begin{align}
	\begin{split}
 	u_{xx} + u_{yy} &= f,\quad x \text{ in } R \subset \mathbb{R}^2,\\
 	u &= g, \quad x \text{ on } \partial R.
	\end{split}\label{eqn:2d_poisson}
\end{align}
Let $a = x_{-1}, x_0, \ldots, x_{N-1} = b$ be a partition of $[a,b]$, and let $c = y_{-1}, y_0, \ldots, y_{N-1} = d$ be a partition of $[c,d]$.
Suppose that there are $N+1$ evenly spaced points, so that $N$ is the number of subintervals in each dimension, and $x_i, y_j$ are given by
\begin{align*}
	x_i &= a + (i+1)h, \\
	y_j &= c + (j+1)h,
\end{align*}
for $i,j = 0, \ldots, N-2$, where $h = x_i-x_{i-1} = y_i-y_{i-1}$.
We look for an approximation $U_{i,\,j}$ on the grid $\{(x_i,y_j)\}_{i,j=-1}^{N-1}$.

Recall that
 \begin{align*}
 \triangle u &= u_{xx}(x,y) + u_{yy}(x,y) \\
&= \frac{u(x+h,y) - 2u(x,y)+ u(x-h,y)}{h^2} \\
 & \qquad{}+
 \frac{u(x,y+h) - 2u(x,y)+ u(x,y-h)}{h^2} + \mathcal{O}(h^2).
 \end{align*}
 We replace $\triangle $ with the finite difference operator $\triangle_h$, defined by
 \begin{align*}
 \triangle_h U_{ij} &= \frac{U_{i+1,\,j} - 2U_{i,\,j} + U_{i-1,\,j}}{h^2} + \frac{U_{i,\,j+1} - 2U_{i,\,j}+ U_{i,\,j-1}}{h^2},\\
&= \frac{1}{h^2}(U_{i-1,\,j} + U_{i+1,\,j} + U_{i,\,j-1} + U_{i,\,j+1}-4U_{i,\,j}).
 \end{align*}

 Then the set of equations
\[
\triangle_h U_{ij} = f_{ij}, \quad i,j = 0,\ldots,N-2,
\]% $i,j = 1,\ldots,m$
can be written in matrix form as
 \[AU + p +  q  = f.\]

$A$ is a block tridiagonal matrix, given by
\begin{align}
	\frac{1}{h^2}
\begin{bmatrix}
T & I & &  &\\
I &T & I & &\\
&\ddots  & \ddots & \ddots & \\
&  & I & T & I \\
&  &  & I & T\end{bmatrix}\label{poisson2d:matrixA}
\end{align}

where $I$ is the $N-1\times N-1$ % $m\times m$
identity matrix, and $T$ is the tridiagonal matrix
\[\begin{bmatrix}
-4 & 1 & &  &\\
1 &-4 & 1 & &\\
&\ddots  & \ddots & \ddots & \\
&  & 1 & -4 & 1 \\
&  &  & 1 & -4 \end{bmatrix}.\]

The vector $U$ is given by
\[U = \begin{bmatrix} U^0 \\ U^1 \\ \\ U^{N-2} \end{bmatrix} \text{ where } U^j =
\begin{bmatrix} U_{0,\,j} \\ U_{1,\,j} \\ \\ U_{N-2,\,j} \end{bmatrix} \text{ for each } j, \text{ }0\leq j \leq N-2.\]

% \[U = \begin{bmatrix} U^1 \\ U^2 \\ \\ U^m \end{bmatrix} \text{ where } U^j =
% \begin{bmatrix} U_{1,\,j} \\ U_{2,\,j} \\ \\ U_{m,\,j} \end{bmatrix} \text{ for each } j, 1\leq j \leq m\]
% So $U^j$ represents the $j$th row of interior points in our grid, where $y_j = jh$

The vectors $p$ and $q$ come from the boundary conditions of \eqref{eqn:2d_poisson}, and are given by
\[p = \begin{bmatrix} p^0 \\ \ldots \\ \\ p^{N-2} \end{bmatrix}, \quad  q = \begin{bmatrix} q^0 \\ \ldots \\ \\ q^{N-2} \end{bmatrix},\]
where
\[p^j = \frac{1}{h^2} \begin{bmatrix} g_{-1,\,j} \\ 0 \\ \vdots \\0\\ g_{N-1,\,j} \end{bmatrix} ,\,\,\, 0 \leq j \leq N-2,\]
and
\[q^0 = \frac{1}{h^2}\begin{bmatrix} g_{0,-1}  \\ g_{1,-1} \\ \vdots \\ g_{N-3,-1}\\ g_{N-2,-1} \end{bmatrix}, \quad q^{N-2} = \frac{1}{h^2}\begin{bmatrix} g_{0,N-1} \\ g_{1,N-1} \\ \vdots \\ g_{N-3,N-1}\\ g_{N-2,N-1} \end{bmatrix}, \quad q^{j} = \begin{bmatrix} 0 \\ 0 \\ \vdots \\ 0 \\ 0 \end{bmatrix} ,\,\,\, 1 \leq j \leq N-3.\]

% The vector $q$ is given by $u = [q^0 \ldots q^{N-2}]^T$, %   $u = [q^1 \ldots q^m]^T$,
% where
% \[q^j = \frac{1}{h^2} \begin{bmatrix} g_{0,\,j} \\ 0 \\ \vdots \\0\\ g_{m+1,\,j} \end{bmatrix} , \,\,\, 2 \leq j \leq m-1\]
% and
% \[q^1 = \frac{1}{h^2}\begin{bmatrix} g_{1,0} + g_{0,1} \\ g_{2,0} \\ \vdots \\ g_{m-1,0}\\ g_{m,0} + g_{m+1,1}\end{bmatrix}, \quad q^m = \frac{1}{h^2}\begin{bmatrix} g_{1,m+1} + g_{0,m}\\ g_{2,m+1} \\ \vdots \\ g_{m-1,m+1}\\ g_{m,m+1} + g_{m+1,m}\end{bmatrix}\]

% \begin{problem}
% Find the solution $u$ of the 2D Poisson equation with the given Dirichlet boundary conditions:
% \begin{align*}
% 	\Delta u &= -\pi^2 \sin(\pi x)\sin(\pi y), \quad (x,y) \in [0,1]\times [0,1], \\
% 	u(x,0) &= 1-x, \\
% 	u(x,1) &= 1-2x, \\
% 	u(0,y) &= 1, \\
% 	u(1,y) &= -y.
% \end{align*}
%
% Graph your solution, and demonstrate convergence of the numerical approximation by
% creating a log-log plot of the error $E(h).$
% \end{problem}

% The matrix $A$ is sparse, and so we can use several functions from the package \texttt{scipy.sparse.linalg}.
% In particular, we use the functions \texttt{spdiags} and \texttt{spsolve}.
%
%  \begin{verbatim}
% D1,D2,D3 = -4*np.ones((1,m**2)), np.ones((1,m**2)), np.ones((1,m**2))
% Dm1, Dm2 = np.ones((1,m**2)), np.ones((1,m**2))
% for j in range(0,D2.shape[1]):
% 	if (j%m)==m-1:
% 		D2[0,j]=0
% 	if (j%m)==0:
% 		D3[0,j]=0
% diags = np.array([0,-1,1,-m,m])
% data = np.concatenate((D1,D2,D3,Dm1,Dm2),axis=0) # This stacks up rows
% A = 1./h**2.*spdiags(data, diags, m**2,m**2).asformat('csr') # This ap
%  \end{verbatim}

\begin{comment}
\subsection{2D Heat Equation}
Recall that the collection of finite difference equations
\[\nabla^2_h U_{ij} = 0, \quad 1 \leq i,j\leq m\]
can be written in matrix form as
\[AU + q  = 0\]

The Crank-Nicolson method for the 2D heat equation is given by
\[U_{i,\,j}^{n+1}- U_{i,\,j}^{n} = \frac{\Delta t}{2}(\nabla_h^2 U_{i,\,j}^{n} + \nabla_h^2 U_{i,\,j}^{n+1}) \text{ for each } 1 \leq i,j \leq m\]
is a second order accurate in both space and time. Basically we're using a midpoint scheme in time,
and a trapezoidal scheme in space. The resulting method is implicit, and can be written in matrix form as
\begin{align*}
	IU^{n+1} &= IU^n + \frac{\Delta t}{2}(AU^n + q + AU^{n+1} + q)\\
	(I - \frac{\Delta t}{2}A)U^{n+1}&= (I + \frac{\Delta t}{2}A)U^n + \Delta t q
\end{align*}

% TODO: What size must the time step be to ensure stability?

We will need to take many time steps, where many equations must be solved with the matrix $(I - \frac{\Delta t}{2}A)$.
The function \texttt{factorized} from \texttt{scipy.sparse.linalg} computes the LU decomposition of the matrix.
This decomposition reduces the time required for solving consecutive time steps.
\end{comment}

The following code implements the greater portion of the finite difference method; it leaves the construction of the matrix $A$ in \eqref{poisson2d:matrixA} to problem \ref{poisson2d:laplace}.

\begin{lstlisting}
from __future__ import division
from scipy.sparse import spdiags
from scipy.sparse.linalg import spsolve

def poisson_square(a1,b1,c1,d1,n,bcs, source):
	# n = number of subintervals
	# We discretize in the x dimension by
	# a1 = x_0 < x_1< ... < x_n=b1, and
	# We discretize in the y dimension by
	# c1 = y_0 < y_1< ... < y_n=d1.
	# This means that we have interior points
	# {x_1, ..., x_{n-1}}\times {y_1, ..., y_{n-1}}
	# or {x_1, ..., x_m}\times {y_1, ..., y_m} where m = n-1.
	# In Python, this is indexed as
	# {x_0, ..., x_{m-1}}\times {y_0, ..., y_{m-1}}
	# We will have m**2 pairs of interior points, and
	# m**2 corresponding equations.
	# We will organize these equations by their
	# y coordinates: all equations centered
	# at (x_i, y_0) will be listed first,
	# then (x_i, y_1), and so on till (x_i, y_{m-1})
	delta_x, delta_y, h, m = (b1-a1)/n, (d1-c1)/n, (b1-a1)/n, n-1

	####    Construct the matrix A    ####

	####    Here we construct the vector b    ####
	b, Array = np.zeros(m**2), np.linspace(0.,1.,m+2)[1:-1]
	# In the next line, source represents
	# the inhomogenous part of Poisson's equation
	for j in xrange(m):
		b[j*m:(j+1)*m] = source(a1+(b1-a1)*Array, c1+(j+1)*h*np.ones(m) )

    # In the next four lines, bcs represents the
	# Dirichlet conditions on the boundary
	# y = c1+h, d1-h
	b[0:m] -= h**(-2.)*bcs(a1+(b1-a1)*Array,c1*np.ones(m))
	b[(m-1)*m:m**2] -= h**(-2.)*bcs(a1+(b1-a1)*Array,d1*np.ones(m))
	# x = a1+h, b1-h
	b[0::m] -= h**(-2.)*bcs(a1*np.ones(m),c1+(d1-c1)*Array)
	b[(m-1)::m] -= h**(-2.)*bcs(b1*np.ones(m),c1+(d1-c1)*Array)

    ####    Here we solve the system A*soln = b    ####
	soln = spsolve(A,b)

	# We return the solution, and the boundary values,
	# in the array z.
	z = np.zeros((m+2,m+2) )
	for j in xrange(m):
		z[1:-1,j+1] = soln[j*m:(j+1)*m]

	x, y = np.linspace(a1,b1,m+2), np.linspace(c1,d1,m+2)
	z[:,0], z[:,m+1]  = bcs(x,c1*np.ones(len(x)) ), bcs(x,d1*np.ones(len(x)) )
	z[0,:], z[m+1,:] = bcs(a1*np.ones(len(x)),y), bcs(b1*np.ones(len(x)),y)
	return z

\end{lstlisting}

\begin{problem}
Construct the matrix $A$ in \eqref{poisson2d:matrixA}. Make sure your matrix is sparse. Then use the code
above to solve the boundary value problem
\begin{align}
	\begin{split}
	\Delta u = 0, &{}\quad x \in [0,1]\times [0,1],\\
	u(x,y) = x^3, &{}\quad (x,y) \in \partial ([0,1]\times [0,1]).
	\end{split}
	\label{poisson2d:laplace}
\end{align}
Plot the 3D solution with $n=100$.
\end{problem}

\begin{figure}
\includegraphics[width=\textwidth]{Laplace.png}
\caption{The solution of \eqref{poisson2d:laplace}.}
\end{figure}

\section*{Poisson's equation and conservative forces}
In physics Poisson's equation is used to describe the scalar potential of a conservative force.
In general
\[ \Delta V = - f\]
where $V$ is the scalar potential of the force, or the potential energy a particle would have at that point, and $f$ is a source term.
Examples of conservative forces include Newton's Law of Gravity (where matter become the source term) and Coulomb's Law, which gives the force between two charge particles (where charge is the source term).

In electrostatics the electric potential is also known as the voltage, and is denoted by $V.$
From Maxwell's equations it can be shown that that the voltage obeys Poisson's equation with the electric charge density (like a continuous cloud of electrons) being the source term:
\[
 \Delta V = -\frac{\rho}{\epsilon_0},
\]
where $\rho$ is the charge density and $\epsilon_0$ is the permissivity of
free space, which is a constant that we'll leave as $1$.

Usually a non zero $V$ at a point will cause a charged particle to move to a lower potential, changing $\rho$ and the solution to $V$.
However, in this analysis we'll assume that the charges are fixed in place.

Suppose we have 3 nested pipes.
The outer pipe is attached to "ground," which usually we define to be $V=0$, and the inner two have opposite relative charges.
Physically the two inner pipes would function like a capacitor.

The following code will plot the charge distribution of this setup.
\begin{lstlisting}
import matplotlib.colors as mcolors

def source(X,Y):
    """
    Takes arbitrary arrays of coordinates X and Y and returns an array of the same shape
    representing the charge density of nested charged squares
    """
    src = np.zeros(X.shape)
    src[ np.logical_or(
        np.logical_and( np.logical_or(abs(X-1.5) < .1,abs(X+1.5) < .1) ,abs(Y) < 1.6),
        np.logical_and( np.logical_or(abs(Y-1.5) < .1,abs(Y+1.5) < .1) ,abs(X) < 1.6))] = 1
    src[ np.logical_or(
        np.logical_and( np.logical_or(abs(X-0.9) < .1,abs(X+0.9) < .1) ,abs(Y) < 1.0),
        np.logical_and( np.logical_or(abs(Y-0.9) < .1,abs(Y+0.9) < .1) ,abs(X) < 1.0))] = -1
    return src

#Generate a color dictionary for use with LinearSegmentedColormap
#that places red and blue at the min and max values of data
#and white when data is zero

def genDict(data):
    zero = 1/(1 - np.max(data)/np.min(data))
    cdict = {'red':   [(0.0,  1.0, 1.0),
                   (zero,  1.0, 1.0),
                   (1.0,  0.0, 0.0)],
         'green': [(0.0,  0.0, 0.0),
                   (zero,  1.0, 1.0),
                   (1.0,  0.0, 0.0)],
         'blue':  [(0.0,  0.0, 0.0),
                   (zero,  1.0, 1.0),
                   (1.0,  1.0, 1.0)]}
    return cdict

a1 = -2.
b1 = 2.
c1 = -2.
d1 = 2.
n =100
X = np.linspace(a1,b1,n)
Y = np.linspace(c1,d1,n)
X,Y = np.meshgrid(X,Y)

plt.imshow(source(X,Y),cmap =  mcolors.LinearSegmentedColormap('cmap', genDict(source(X,Y))))
plt.colorbar(label="Relative Charge")
plt.show()
\end{lstlisting}

\begin{figure}
\includegraphics[width=\textwidth]{pipesRho.pdf}
\caption{The charge density of the 3 nested pipes.}
\end{figure}

The function \li{genDict} scales the color values to be white when the charge density is zero.
This is mostly to help visualize where there are neutrally charged zones by forcing them to be white.
You may find it useful to also apply it when you solve for the electric  potential.

With this definition of the charge density, we can solve Poisson's equation for the potential field.

\begin{problem}
Solve
% \[\Delta V = -\rho(x,y)\]
\begin{align}
	\begin{split}
	\Delta V = -\rho(x,y), &{}\quad x \in [-2,2]\times [-2,2],\\
	u(x,y) = 0, &{}\quad (x,y) \in \partial ([-2,2]\times [-2,2]).
	\end{split}
	\label{poisson2d:source}
\end{align}
%
for the electric potential $V.$
Use the source function ($-\rho$) defined above. % and grid sizes defined above.
% Use $V=0$ for the boundary conditions on all sides.
% \textit{(Due to the size of $A$, this is best done using sparse matrices)}
Plot the 2D solution using $n=100$.
\end{problem}

\begin{figure}
\includegraphics[width=\textwidth]{pipesV.png}
\caption{The electric potential of the 3 nested pipes.}
\end{figure}


\section{Application of our previous calculation of $ H_\ast(S^n)$ and the ``locality principle''}
\begin{theorem}
Let $n\geq 1$. There is a surjective monoid homomorphism $[S^n,S^n]\to \Z_\times$, where $\Z_\times$ is the multiplicative monoid of $\Z$. $[S^n,S^n]$ is a monoid under composition. (This is basically the degree...)
\end{theorem}
\begin{proof}
Given $f:S^n\to S^n$, take the homology, which is just a homomorphism $\Z\to \Z$, all of which are simply multiplication by an integer. The integer by which you're multiplying to get this homomorphism is the integer associated to $f$.

Construction. If $n=1$, this is just the winding number. Suppose I've constructed this in dimension $n-1$. We have:
	\begin{equation*}
	\xymatrix{ H_{n-1}(S^{n-1})\ar[d]^n & \ar[l] H_n(D^n,S^{n-1})\ar[r]\ar[d] & H_n(S^n)\ar[d]\\
	 H_{n-1}(S^{n-1}) & \ar[l] H_n(D^n,S^{n-1})\ar[r] & H_n(S^n)}
	\end{equation*}
So we're basically suspending $f:S^{n-1}\to S^{n-1}$. More explicitly, if you have $f:S^{n-1}\to S^{n-1}$. We can extend to $\overline{f}:D^n\to D^n$ by sending $tx\mapsto tf(x)$ where $tx$ denotes the ray connecting $x\in S^{n-1}$ to the origin, and we can then quotient out by $S^{n-1}$ to get the map $S^n\to S^n$ as required.
\end{proof}
\subsection{Addendum to the ES axioms}
There's a further axiom, which isn't due to ES, but rather due to Milnor. It's this.
\begin{itemize}
\item Suppose $I$ a set. For each $\alpha\in I$ there's have a space $X_\alpha\in\mathbf{Top}$. I can consider $\coprod_\alpha X_\alpha$. There are inclusion maps $X_\alpha\to\coprod_\alpha X_\alpha$. Then $\bigoplus_\alpha h_n(X_\alpha)\cong h_n\left(\coprod_\alpha X_\alpha\right)$.
\end{itemize}
This is known for ordinary singular homology.
\subsection{Homological algebra}
\begin{enumerate}
\item Suppose $A,B\subseteq C$ are abelian groups. Then $A+C\subseteq C$. You have a sexseq $0\to A\cap B\to A\oplus B\to A+C\to 0$ where the map $c\mapsto (c,-c)$ is how the map $A\cap B\to A\oplus B$ is defined.
\item ``Fundamental isomorphism for abelian groups'' says the following. We have two sexseqs.
	\begin{equation*}
	\xymatrix{0\ar[r] & A\cap B\ar[r]\ar[d] & B\ar[r]\ar[d] & B/A\cap B\ar[r]\ar@{-->}[d]^\cong & 0\\
	0\ar[r] & A\ar[r] & A+B\ar[r] & (A+B)/A\ar[r] & 0}
	\end{equation*}
	I'm not going to write out the diagram chase that we did.
\item ``Snake lemma''. Suppose I have\footnote{``It's my Turn'', Jill Clayburgh}:
	\begin{equation*}
	\xymatrix{ & \ker f^\prime\ar[r]\ar[d] & \ker f\ar[r]\ar[d] & \ker f^{\prime\prime}\ar[d]\\
	0\ar[r] & A^\prime\ar[r]\ar[d]^{f^\prime} & A\ar[r]\ar[d]^f & A^{\prime\prime}\ar[r]\ar[d]^{f^{\prime\prime}} & 0\\
	0\ar[r] & B^\prime\ar[r]\ar[d] & B\ar[r]\ar[d] & B^{\prime\prime}\ar[r]\ar[d] & 0\\
	 & \coker f^\prime\ar[r] & \coker f\ar[r] & \coker f^{\prime\prime}}
	\end{equation*}
	Claim is that there's a map $\ker f^{\prime\prime}\to\coker f$ so that $0\to \ker f^\prime\to \ker f\to \ker f^{\prime\prime}\to\coker f^\prime\to\coker f\to \coker f^{\prime\prime}\to 0$. This is basically the lexseq in homology associated to the sexseqs of the following three chain complexes: $0\to A^\prime\to B^\prime\to 0$, $0\to A\to B\to 0$, and $0\to A^{\prime\prime}\to B^{\prime\prime}\to 0$. Work this out yourself.
\end{enumerate}
\subsection{Locality}
\begin{definition}
The (not necessarily open) cover of a topological space. Won't write this.
\end{definition}
\begin{definition}
Let ${\mathscr{A}}$ be a cover of $X$. An $n$-simplex $\sigma$ is ${\mathscr{A}}$-small if there is $A\in \mathscr{A}$ such that the image of $\sigma$ is entirely in $A$.
\end{definition}
Notice that if $\sigma:\Delta^n\to X$ is ${\mathscr{A}}$-small, then so is $d^i\sigma$. Let's denote by $\Sin^{\mathscr{A}}_n(X)$ the set of ${\mathscr{A}}$-small $n$-simplices. This means that we get a map $\Sin^{\mathscr{A}}_n(X)\to \Sin^{\mathscr{A}}_{n-1}(X)$. Let $S^{\mathscr{A}}_n(X)=\Z[\Sin^{\mathscr{A}}_n(X)]$. Then there's a subchain complex $S^{\mathscr{A}}_\ast(X)$.
\begin{theorem}
The inclusion $S^\mathscr{A}_\ast(X)\subseteq S_\ast(X)$ is a chain homotopy equivalence.
\end{theorem}
\begin{corollary}
If $ H^\mathscr{A}_\ast(X):= H(S^\mathscr{A}_\ast(X))$, then $ H^\mathscr{A}_\ast(X)\cong H_\ast(X)$.
\end{corollary}
We'll do this on Monday.
\begin{example}
If $\mathscr{A}=\{A,B\}$, then $\overline{X-B}=X-\mathrm{Int}(B)\subseteq\mathrm{Int}(A)$. Let $X-B=U$. Then $U\subseteq \overline{U}\subseteq \mathrm{Int}(A)\subseteq A\subseteq X$. This is an excision! So $U\subseteq A\subseteq X$ is an excision. But now, $(X-U,A-U)\to (X,A)$ is an excision, but $(X-U,A-U)=(B,A\cap B)$, so we have $(B,A\cap B)\to (X,A)$ is an excision. Now, also, $S^\mathscr{A}_\ast(X)=S_\ast(A)+S_\ast(B)$. Says he got off track, let me just write things out and explain in a moment.
\begin{equation*}
	\xymatrix{0\ar[r] & S_n(A)\cap S_n(B)=S_n(A\cap B)\ar[r]\ar[d] & S_n(B)\ar[r]\ar[d] & S_n(B,A\cap B)\ar[r]\ar@{-->}[d]^\cong & 0\\
	0\ar[r] & S_n(A)\ar[r] & S_n(A)+S_n(B)=S^\mathscr{A}_n(X)\ar[r] & S^\mathscr{A}_n(X)/S_n(A)\ar[r] & 0}
	\end{equation*}
But we can consider $S_\ast(X)/S_\ast(A)=S_\ast(X,A)$. By the lexseq + 5 lemma, this thing is isomorphic to $S^\mathscr{A}_n(X)/S_n(A)$, so $S_\ast(B,A\cap B)\cong S_\ast(X,A)$ in homology. This is precisely the excision theorem. QED.
\end{example}