\pagebreak
\chapter{Algorithms}
\label{appndx:algorithms}

\begin{algorithm}[!ht]
\SetAlgoLined
\SetKwInOut{KwIn}{Input}
\SetKwInOut{KwOut}{Output}

\KwIn{Two lists $[e^1_{ij}]$ and $[e^2_{ij}]$, $i = 1, 2, \cdots, n$ and $j = 1, 2, 3$, where each element $i$ is a \texttt{float} of the respective electric field at the $j$ coordinate.}
\KwOut{A list of \texttt{float} size $n$ containing the maximum envelope amplitude of the electric field at each point of input.}
Set a list $[e^{am}_i]$, $i = 1, 2, \cdots, n$, of type \texttt{float} to save calculated result\;
Calculate the dot product of $[e^1_{ij}]$ and $[e^2_{ij}]$ across $j$ and save it to a list $[dot_i$], $i = 1, 2, \cdots, n$, of type \texttt{float}\;
Calculate the cross product of $[e^1_{ij}]$ and $[e^2_{ij}]$ across $j$ and save it to a list $[cross_i]$, $i = 1, 2, \cdots, n$, of type \texttt{float}\;
\ForEach{$i \in [dot_i], [cross_i]$}{
    Calculate the \textit{arctan} of $\dfrac{[cross_i]}{[dot_i]}$ and save it to a list $[angles_i]$, $i = 1, 2, \cdots, n$, of type \texttt{float}\;
}
\ForEach{$i,j \in [e^2_{ij}]$}{
    \If{$[angles_i] \geq\pi/2$}{
        $e^2_{ij} \longleftarrow -e^2_{ij}$\;
    }
}
Execute the code section from lines $\boldsymbol{2 - 6}$ again\;
$[cosangs_i] \longleftarrow \cos{[angles_i]}$\;
Set a list $[e^{minus}_{ij}]$ as $[e^1_{ij}] - [e^2_{ij}]$, for all $\forall i,j$\;
\ForEach{$i,j \in [e^2_{ij}]\;\text{across}\; j$}{
    $[mag\_e^1_i] \longleftarrow$ Magnitude of $[e^1_{ij}]$\;
    $[mag\_e^2_i] \longleftarrow$ Magnitude of $[e^2_{ij}]$\;
    \uIf{$[e^2_{ij}] < [e^1_{ij}]\cdot[cosangs_i]$}{
       $[e^{am}_i] \longleftarrow 2\cdot[e^2_{ij}]$\;
    }
    \ElseIf{$[e^1_{ij}] > [e^2_{ij}]$}{
       $[e^{am}_i] \longleftarrow 2\dfrac{\big|\big|\vec{E_2}\big|\times\big(\vec{E_1} - \vec{E_2}\big)\big|}{\big|\vec{E_1} - \vec{E_2}\big|}$\;
    }
    \uIf{$[e^1_{ij}] < [e^2_{ij}]\cdot[cosangs_i]$}{
       $[e^{am}_i] \longleftarrow 2\cdot[e^1_{ij}]$\;
    }
    \ElseIf{$[e^2_{ij}] > [e^1_{ij}]$}{
       $[e^{am}_i] \longleftarrow 2\dfrac{\big|\big|\vec{E_1}\big|\times\big(\vec{E_2} - \vec{E_1}\big)\big|}{\big|\vec{E_2} - \vec{E_1}\big|}$\;
    }
}
\caption{Calculate the maximum envelope modulation amplitude at all directions}
\label{alg:max_modulation_amplitude}
\end{algorithm}

\pagebreak

\begin{algorithm}[!ht]
\SetAlgoLined
\SetKwInOut{KwIn}{Input}
\SetKwInOut{KwOut}{Output}

\KwIn{$msh$ \gls{FE} mesh and $[init\_point_i]\;, i = 1..3$ as the initial point of the vector.}
\KwOut{The unit normal vector of the electrode surface with respect to the \gls{FE} mesh.}
$adj\_face\longleftarrow$ First adjacent face to $[init\_point_i]\in msh$\;
$[end\_point^1_i]\longleftarrow$ Vertex corresponding to $adj\_face\;\neq [init\_point_i]$\;
$[end\_point^2_i]\longleftarrow$ Vertex corresponding to $adj\_face\;\neq [init\_point_i]$ and different than $[end\_point^1_i]$\;
$[normal_i]\longleftarrow ([end\_point^1_i] - [init\_point_i])\times([end\_point^2_i] - [init\_point_i])$\;
$[normal_i]\longleftarrow [normal_i]/\big|[normal_i]\big|$\;
\caption{Calculate the normal vector coordinates}
\label{alg:electrode_orientation}
\end{algorithm}


\externaldocument{paper.tex}

\section{Proposed algorithm - Intrinsic Isometric Manifold Learning}
	\label{sec:Intrinsic-isometric-manifold-learning}

	\subsection{Algorithm high-level overview}
	\label{ssec:Algorithm-high-level-overview}

	First we will describe an Intrinsic Isometric manifold learning algorithm which operates assuming that the values of the matrix $\mathbf{M}\left({\bf y}_{i}\right)=\frac{df}{dx}\left(f^{-1}\left({\bf y}_{i}\right)\right)\frac{df}{dx}\left(f^{-1}\left({\bf y}_{i}\right)\right)^{T}$ are given for all $y_{i}\in{\cal Y}_{s}$, where $\frac{df}{dx}\left(\mathbf{x}\right)$ is the Jacobian of the observation function w.r.t the intrinsic variable at the intrinsic point ${\bf x}_{i}$. Although this is a very restrictive requirement, we show in the \cref{sec:Intrinsic-Metric-Estimation} how it can, in some scenarios, be robustly approximated from the accessible sample subset ${\cal Y}_{s}$ without explicit knowledge of $f$.

	Our approach then for achieving the goal described is \cref{sec:setting} is implemented by first recognizing that $\mathbf{M}\left({\bf y}_{i}\right)^{\dagger}=\left[\frac{df}{dx}\left(f^{-1}\left({\bf y}_{i}\right)\right)\frac{df}{dx}\left(f^{-1}\left({\bf y}_{i}\right)\right)^{T}\right]^{\dagger}$ is in fact the push-forward metric tensor and the observation function $f$ creates a isomorphism between the intrinsic and observed manifolds when using this push-forward metric as the Riemannian metric on the tangent planes of the observed manifold $\mathcal{Y}$. This isomorphism allows us to calculate approximation of short range intrinsic Euclidean distances on the observed manifold in a way that is invariant to the observation function $f$, as if they were calculated directly on the latent intrinsic manifold $\text{\ensuremath{\mathcal{X}}}$. We then construct an embedding of the observed manifold into $n$-dimensional Euclidean space by performing \ac{LS-MDS} using the approximations of the short-range intrinsic distances as the target distances. Since only short-range distances are approximated, we use a weighted version of the stress function (presented in Equation \cref{eq:w-intrinisc_stress}) which can ``ignore'' un-approximated inter-point distances and call it the \textit{partial stress function}. As this defines a non-convex cost function we are required to provide the optimization process with a good initial solution, to do so we recognize that using intrinsic short-range distances we can calculate approximations of all intrinsic geodesic distances on the manifold. With this we define an intrinsic version of the well known Isomap algorithm \cite{tenenbaum2000global}, which attempts to embed the sampled points in an Euclidean space so that they respect the approximated geodesic distances. The resulting embedding is used as an initial embedding for the minimization of the partial stress function \cref{eq:w-intrinisc_stress}. Due to the low-dimension constraint imposed on the embedding, the minimization of the partial stress function leads also to minimization of the full unweighted stress function in \cref{eq:stress}. The flow of this algorithm is illustrated in \cref{fig:Intrinsic-isometric-manifold-lea}.

	This approach harnesses the ability of eigen-decomposition to give a globally optimal solution to the embedding problem given all inter-point distances and the ability of the \ac{LS-MDS} optimization approach to only consider local intrinsic Euclidean structure. Convergence of the optimization process is achieved provided that the embedding received via eigen-decomposition is ``close enough'' to the true intrinsic structure. Since, as we will show, this might not be the case for highly non-convex intrinsic manifolds we also devise an iterative multi-scale optimization scheme to avoid such occurrences.

	\begin{figure}[h]
		\begin{centering}
			\includegraphics[width=1\textwidth]{figures/Chapter_3/flow}
			\par\end{centering}
		\caption{Intrinsic-isometric manifold learning flow graph\label{fig:Intrinsic-isometric-manifold-lea}}
	\end{figure}

	\subsection{Local intrinsic geometry approximation}
	\label{ssec:Intrinsic-geometry-approximation}

	Minimization of the cost function given in \cref{sec:setting} first requires an approximation of the intrinsic Euclidean inter-point distances $d_{i,j}=d_{euc}\left(\text{\ensuremath{\mathbf{x}}}_{i},\text{\ensuremath{\mathbf{x}}}_{j}\right)=\left\Vert {\bf x}_{i}-{\bf x}_{j}\right\Vert $ without having direct access to the latent intrinsic variables themselves. To overcome this obstacle we recognize that ${\cal X}$ can be viewed as a Riemannian manifold by endowing it with the dot-product inherited from its $n$-dimensional intrinsic Euclidean space, and that $\mathbf{M}\left({\bf y}_{i}\right)^{\dagger}=\left[\frac{df}{dx}\left(f^{-1}\left({\bf y}_{i}\right)\right)\frac{df}{dx}\left(f^{-1}\left({\bf y}_{i}\right)\right)^{T}\right]^{\dagger}$ gives us the push-forward metric tensor on $\mathcal{Y}$ with respect to the transformation between $\mathcal{X}$ and $\mathcal{Y}$ (the observation function). With this, an isomorphism is established between the two manifolds $\mathcal{X}$ and $\mathcal{Y}$ which enables
	us to use $\mathcal{Y}$ as a proxy for making intrinsic geometric calculations on the latent manifold $\mathcal{X}$. A similar observation was made in \cite{singer2008non}, where the following approximation was suggested for calculating intrinsic inter-point distances:
	\begin{equation}
		\begin{aligned}
			\tilde{d}_{i,j}^{2}=&\frac{1}{2}\left[\text{\ensuremath{\mathbf{y}}}_{i}-\mathbf{y}_{j}\right]^{T}\mathbf{M}\left({\bf y}_{i}\right)^{\dagger}\left[\text{\ensuremath{\mathbf{y}}}_{i}-\mathbf{y}_{j}\right]+\frac{1}{2}\left[\text{\ensuremath{\mathbf{y}}}_{i}-\mathbf{y}_{j}\right]^{T}\mathbf{M}\left({\bf y}_{j}\right)^{\dagger}\left[\text{\ensuremath{\mathbf{y}}}_{i}-\mathbf{y}_{j}\right] \\
			\approx & \left\Vert \text{{\bf x}}_{i}-\text{{\bf x}}_{j}\right\Vert^{2} = d_{i,j}^{2}\label{eq:int_dist_approx}
		\end{aligned}
	\end{equation}
	This approximation has been used is several papers \cite{dsilva2013nonlinear,dsilva2015data,dsilva2015parsimonious,duncan2013identifying,mishne2015graph,singer2008non,talmon2012parametrization,talmon2013empirical,talmon2015intrinsic,talmon2015manifold},
	error analysis for it was presented in \cite{dsilva2015data,singer2008non} and it was found to be:
	\[
	d_{i,j}^{2}=\frac{1}{2}\left[\text{\ensuremath{\mathbf{y}}}_{i}-\mathbf{y}_{j}\right]^{T}\mathbf{M}\left({\bf y}_{i}\right)^{\dagger}\left[\text{\ensuremath{\mathbf{y}}}_{i}-\mathbf{y}_{j}\right]+\frac{1}{2}\left[\text{\ensuremath{\mathbf{y}}}_{i}-\mathbf{y}_{j}\right]^{T}\mathbf{M}\left({\bf y}_{j}\right)^{\dagger}\left[\text{\ensuremath{\mathbf{y}}}_{i}-\mathbf{y}_{j}\right]+\ensuremath{\mathcal{O}}\left(\left\Vert \text{\ensuremath{\mathbf{y}}}_{i}-\mathbf{y}_{j}\right\Vert ^{4}\right)
	\]
	We show the nature of this approximation on an artificial data set in \cref{ssec:simulated_data_Intrinsic_isometric_embedding}. It is evident from both the empirical results from the given example and the rigorous error analysis given in \cite{dsilva2015data} that this approximation is valid only for points of the observed manifold which are close to each other, i.e. when $\left\Vert\text{\ensuremath{\mathbf{y}}}_{i}-\mathbf{y}_{j}\right\Vert ^{4}$ is small with respect to the higher derivatives of the observation function. In order to generate an embedding from the calculated intrinsic inter-point distance estimations we formulate a \ac{LS-MDS} optimization problem using the weighted version of the stress function :
	\begin{equation}
	\sigma\left(\widetilde{\mathbf{X}}\right)=\sum_{i<j}w_{i,j}\left(\left\Vert \mathbf{\widetilde{x}}_{i}-\mathbf{\widetilde{x}}_{j}\right\Vert -\tilde{d_{i,j}}\right)^{2}\label{eq:w-intrinisc_stress}
	\end{equation}
	Where unknown (or unreliable) distances estimations are given zero weights. Determining the exact scale at which we can ``trust'' the approximation \cref{eq:int_dist_approx} is still an open issue but this is a common problem in non-linear dimensionality reduction and dealing with this is out of the scope of this work. We will say that use of between 5 and 15 nearest neighbors around each sample point has proven to be a good rule-of-thumb since this gives good graph connectivity while using mostly reliable distance approximations. We remark that in \cite{singer2008non} this approximation was used in order to create an intrinsic variant of the diffusion maps method. There, the locality of the approximation did not pose a problem due to the exponentially decaying diffusion kernel in which it was used, which made longer distances and their approximation errors insignificant. However, in our work, we aim to retrieve the actual Euclidean geometry of the intrinsic space and it is not immediately clear if this locally approximated intrinsic geometry uncovers enough information about the intrinsic manifold in order to fully recover it. The possibility of retrieving the intrinsic structure only given a subset of known inter-point distances can be reduced to the question of whether a not a inter-point \ac{EDM} originating from points known to reside in an $n$-dimensional Euclidean space can be uniquely completed given only a subset of the distances. This problem is known as the \ac{EDMCP} \cite{alfakih1999solving,fang2012euclidean}. It is known that under cretin conditions it is possible to uniquely determine all missing distances from a subset of of distance, this is usually done using semi-definite programming, which makes it untraceable for problems involving large numbers of points (which are the case of interest in most manifold learning application), however this motivates us to believe that given a number of known distance constrains coupled with the requirement that the points lay in a $n$-dimensional Euclidean space might be enough in order to fully constrain the problem allowing for a unique solution which represents the true latent structure.

	\subsection{Intrinsic geodesic distance approximation and intrinsic Isomap}
	\label{ssec:Global-geometry-approximation}

	A problem common with \ac{LS-MDS} it is that due to it being a non-convex optimization problem which convergence to a local (as opposed to global) minima. In order to improve the chances of converging to a good final solution one must provide in with a good initial point. In this case this means a good initial embedding into $n$-dimensional space. This initial embedding is then tweaked during the optimization until complete convergence. In order to construct this initial embedding we create an intrinsic variant of the Isomap algorithm.

	To do so we harness the ability to calculate local geometry in an intrinsic fashion and use the local distances approximated using \cref{eq:int_dist_approx} in order to approximate all intrinsic geodesic distances directly from the observed manifold. To understand why this is possible we look look at the definition of path lengths:
	\[
	L_{\mathcal{X},h}\left[\gamma\right]=L_{\mathcal{Y},f_{*}}\left[f\left(\gamma\right)\right]=\int_{a}^{b}\sqrt{f_{*f\left(\gamma(t)\right)}\left(f\left(\gamma\left(t\right)\right)',f\left(\gamma\left(t\right)\right)'\right)}dt
	\]
	This equation states that all length calculations for corresponding paths on the isometric manifolds $\mathcal{X}$ and $\mathcal{Y}$ will be identical if one uses the push-forward metric as the metric on the observed manifold $\mathcal{Y}$. This gives us a way to calculate intrinsic path lengths using only observed data assuming that $\left(\frac{df}{dx}\left({\bf x}_{i}\right)\frac{df}{dx}\left({\bf x}_{i}\right)^{T}\right)^{\dagger}$is known everywhere on the manifold. The push-forward metric accounts for local stretching or contraction of the manifold due to the observation function and changes the way we measure distances in order to compensate for this. This is similar to the role the transformation Jacobian determinant has when performing a change of variables when calculating integrals or transforming probability density functions. Using these intrinsic curve length calculation one can also calculate geodesic distances as the infimum over all paths in $\mathcal{Y}$ which connect $f\left({\bf x}_{i}\right)$ and $f\left({\bf x}_{j}\right)$ where the push-forward metric is used as a metric on the observed manifold. Since only a final set of points sampled from these manifold is given, this minimization over all contentious paths can be approximated by a minimization between all paths passing through a limited set of sample point. This presents a problem which is equivalent to the shortest-path problem where only distances for which we have proper distance approximations are represented by edges on the graph.

	Once all intrinsic inter-point geodesic distances are approximated one can use \ac{MDS} to create an $n$-dimensional embedding in which Euclidean distances best respect these distances. This can be seen as an intrinsic variant of the well known Isomap method. The main difference is that instead of assuming local isometry (as is the case in Isomap) we use the push-forward metric instead of the Euclidean metric on the observed manifold in order to explicitly guarantee local isometry. Since geodesic and Euclidean distances are equal to each other for convex spaces, this intrinsic Isomap method gives an intrinsic-isometric embedding for convex intrinsic manifolds as seen in \cref{fig:Intrinsic-isomap-convex}

	\begin{figure}[h]
		\begin{centering}
			\begin{minipage}[b][1\totalheight][c]{0.3\columnwidth}%
				\begin{center}
					\includegraphics[width=1\textwidth]{figures/Chapter_3/convex/intrinsic}
					\par\end{center}%
			\end{minipage}\hfill{}%
			\begin{minipage}[b][1\totalheight][c]{0.3\columnwidth}%
				\begin{center}
					\includegraphics[width=1\textwidth]{figures/Chapter_3/convex/observed}
					\par\end{center}%
			\end{minipage}\hfill{}%
			\begin{minipage}[b][1\totalheight][c]{0.3\columnwidth}%
				\begin{center}
					\includegraphics[width=1\textwidth]{figures/Chapter_3/convex/embeddingpng}
					\par\end{center}%
			\end{minipage}
			\par\end{centering}

			\begin{centering}
				\begin{minipage}[b]{0.3\columnwidth}%
					\begin{center}
						\includegraphics[width=1\textwidth]{figures/Chapter_3/non_convex/intrinsic}
						\par\end{center}%
				\end{minipage}\hfill{}%
				\begin{minipage}[b]{0.3\columnwidth}%
					\begin{center}
						\includegraphics[width=1\textwidth]{figures/Chapter_3/non_convex/observed}
						\par\end{center}%
				\end{minipage}\hfill{}%
				\begin{minipage}[b]{0.3\columnwidth}%
					\begin{center}
						\includegraphics[width=1\textwidth]{figures/Chapter_3/non_convex/embedding}
						\par\end{center}%
				\end{minipage}
				\par\end{centering}
		\caption{Intrinsic Isomap applied to a convex manifold\label{fig:Intrinsic-isomap-convex}}
	\end{figure}

	However this is not true in general and as was discussed in \cite{rosman2010nonlinear} for non-convex manifolds, Euclidean and geodesic distances are not the same and the embedding produced will not respect the intrinsic Euclidean geometry. Indeed on a non-convex data set we see that this method does not recover the structure of the intrinsic space as seen in \cref{fig:punctured_severed_sphere_intrinsic_isomap_embedding}. This shows that simply making the Isomap algorithm intrinsic does not suffice in order to produce an intrinsic-isometric embedding in general however resulting embedding might be ``close-enough'' to an embedding which minimizes the stress function in \cref{eq:w-intrinisc_stress} so that further optimization will converge to a good local minima resulting in an embedding which is intrinsic and isometric.

	With this strategy we use a combined approach. Using the embedding generated by our intrinsic variant of Isomap as a initial point for the optimization gives us the advantage of the global optimality of a similar embedding problems, then further optimization of the desired cost function lead to a convergence to a local minima of our exact	problem.This combines the benefits of both eigen-decomposition and non-convex optimization.Eigenvalue decomposition provides a globally optimal embedding given a full inter-point distance matrix however the bedimming generated might be distorted due to to non-convexity of the intrinsic manifold, on the other hand non-convex optimization of the stress function is not effected by non convexity however it will not converge to a good local minimum unless it has a good initial embedding.

	\subsection{Stress minimization and final embedding}
	\label{ssec:Embedding-correction-for}

	Once an initial embedding is built using our intrinsic variant of the Isomap method, further minimization of stress function is performed iteratively with \ac{SMACOF}.

	It is important to remark that our initial goal, as detailed in \cref{sec:setting}, was to minimize the full, unweighted stress function with respect to the true intrinsic Euclidean distances. Instead we then minimize a weighted stress function where only short intrinsic distances (which we were able to approximate from the observed data) appear. This is similar to common setting in machine learning where a partially known and approximated cost function is minimized with the hope that the true cost function, corresponding in our case to the full and exact stress, in also minimized. It seem that we might suffer from over-fitting if the problem is not constrained enough by the existing distance and low-rank restrictions on the embedding. To see if this is the case we plot the observed partial stress function along the full true stress function. As can be seen in \cref{fig:Stress-Plot-Full} both these function exhibit similar behaviors during the optimization process.
	\begin{figure}[h]
		\begin{centering}
			\includegraphics[width=0.7\textwidth]{figures/Chapter_3/stress_real_vs_observed}
			\par\end{centering}
		\caption{Observed partial stress and true full stress during \ac{LS-MDS} optimization\label{fig:Stress-Plot-Full}}
	\end{figure}
	At first it might seem surprising that the real stress is lower then the observed stress estimated intrinsic Euclidean distances. To explain this we remember that the intrinsic distance estimations used for the cost function are only noisy approximation of the true intrinsic distances and as such might disagree with each other and might not necessarily be exactly embeddable into a low-dimensional space. As a result a certain amount of stress will exist in the embedding with respect to the noisy distance approximations. The \ac{LS-MDS} process aggregates these noisy intrinsic distance estimations into a single embedding, this has a denoising effect which reduces the stress of the final embedding with respect to the true distances.

	\subsection{Multi-scale scheme}

	When the non-convexity of the intrinsic manifold is not very significant, the initial embedding received using our intrinsic variant of the Isomap method (described in \cref{ssec:Global-geometry-approximation}) is, in many cases, close enough to the actual embedding as to allow convergence of the weighted \ac{LS-MDS} minimization to a good solution as described in \cref{ssec:Embedding-correction-for}. However, when the non-convexity of the intrinsic manifold i significant, this initial embedding might not be close enough to the actual intrinsic structure and the \ac{LS-MDS} optimization will not converge to a quality local minimum. To avoid this situation we suggest a multi-scale optimization scheme.

	We start from a small scale on the observed manifold, applying this method to small patches of the manifold which are convex or approximately-convex, thus improving the chance of the \ac{LS-MDS} optimization to converge to a good solution. Assuming that the algorithm works properly on such a small scale and a good embedding into Euclidean space is received, a straightforward calculation of approximate intrinsic Euclidean distances from the embedding itself is possible and we approximate all inter-point distances in this patch via simple Euclidean distance measurement in the received embedding. This provides an approximation for the previously unknown or missing distances. This approximation does not relay on convexity of the intrinsic manifold and avoids the distortions induced by non-convexity when these distances were previously approximated using shortest-path algorithm. With intrinsic distances now approximated on a larger scale than before we can now increase the scale on which the algorithm operates, again allowing for the introduction of some additional slight non-convexity. At the subsequent iterations of the algorithm, the scale is increased iteratively and the non-convexity induces by the new points is again tackled in the same way. This process is repeated until an embedding of all points into Euclidean space is attained which respect the Euclidean intrinsic distance. The definition of operation scales and their increment can be easily done using the intrinsic geodesic distances which were already approximated as described in \cref{ssec:Global-geometry-approximation}. This gradual increase in scale is visualized in \cref{fig:Increasing-operation-scales}. We remark that although the visualization is in the intrinsic space, the estimation of the geodesic distances and hence the scale selection is performed directly on the observed manifold.

	\begin{figure}[h]
		\begin{minipage}[t]{0.19\columnwidth}%
			\begin{center}
				\includegraphics[width=1\textwidth]{figures/Chapter_3/scale_1}
				\par\end{center}%
		\end{minipage}\hfill{}%
		\begin{minipage}[t]{0.19\columnwidth}%
			\begin{center}
				\includegraphics[width=1\textwidth]{figures/Chapter_3/scale_2}
				\par\end{center}%
		\end{minipage}\hfill{}%
		\begin{minipage}[t]{0.19\columnwidth}%
			\begin{center}
				\includegraphics[width=1\textwidth]{figures/Chapter_3/scale_3}
				\par\end{center}%
		\end{minipage}\hfill{}%
		\begin{minipage}[t]{0.19\columnwidth}%
			\begin{center}
				\includegraphics[width=1\textwidth]{figures/Chapter_3/scale_4}
				\par\end{center}%
		\end{minipage}\hfill{}%
		\begin{minipage}[t]{0.19\columnwidth}%
			\begin{center}
				\includegraphics[width=1\textwidth]{figures/Chapter_3/scale_5}
				\par\end{center}%
		\end{minipage}

		\caption[Increasing operation scales]{Increasing operation scales - 200 points are operated on on the first
			scale and an additional 200 points are added at each scale \label{fig:Increasing-operation-scales}}
	\end{figure}

	A pitfall that might occur in this process is that the increase of the scale of the algorithm is too large resulting in introduction of major non-convexity and poor distance estimation, luckily we can monitor the process of scale increase by calculating the observed partial stress of the generated embedding with respect to the approximated short-intrinsic distances and different scales can be attempted till a good stress value is reached.

	Another way in which this process can be made more robust is by starting this multi-scale schema form multiple different starting points and then choosing the embedding which results in the least amount of partial observed stress. The selection of stating point can be done by randomly selecting sample points or preferably by clustering the data using the already approximated inter-point intrinsic geodesic distances, which leads to starting points that are distributed across the manifold and can lead to different final results.

	\begin{figure}[h]
		\begin{centering}
			\includegraphics[width=0.7\textwidth]{figures/Chapter_3/center_point_clustering}
			\par\end{centering}
		\caption[Clustering and starting point selection]{Clustering and starting point selection using approximated intrinsic
			geodesic distances. Different colors represent different clusters.
			The bigger red points represent the cluster centers and will be used
			as different starting points of the multi-scale schema\label{fig:Clustering-by-geodesic}}
	\end{figure}


\documentclass{article}
\usepackage{amsmath}
\usepackage{amssymb}

\newcommand*{\mvec}{\mathbf{\mathrm{vec}}}
\newcommand*{\abcd}[4]{\left( \begin{array}{cc} #1 & #2 \\ #3 & #4 \end{array} \right)}
\newcommand*{\mat}[1]{\tilde{#1}}

\begin{document}

\title{Optimal measurement network for pairwise differences}

\section{Cone programming for A-optimal}

The A-optimal is cast as the following SDP.

\begin{equation}
\min \sum_{i=1}^K u_i
\label{eq:SDP-objective}
\end{equation}
subject to
\begin{equation}
n_{ij} \geq 0  \ \forall \{ (i,j)|1 \leq i \leq j \leq K\},
\label{eq:SDP-positive-n}
\end{equation}
\begin{equation}
\sum_i n_{ii} + \sum_{i<j} n_{ij} = N,
\label{eq:SDP-sum-n}
\end{equation}
and
\begin{equation}
  \left(
\begin{array}{cc}
 F(\{n_{ab}\}) & \vec{e}_i \\
 \vec{e}_i^t & u_i
\end{array} \right) \geq 0  \ \text{for } i=1,2, \dots, K
\label{eq:SDP-Fu}
\end{equation}
where
\begin{equation}
F( \{n_{ab}\} ) = \sum_{a=1}^K V_{aa} n_{aa}
   + \sum_{a<b} V_{ab} n_{ab}
\end{equation}
with sparse matrices $V_{aa}$ and $V_{ab}$ defined by
\begin{eqnarray}
V_{aa;\alpha\beta} &=& s_{aa}^{-2} \delta_{a\alpha}\delta_{a\beta}
\nonumber \\
V_{a\neq b;\alpha\beta} &=& s_{ab}^{-2} (\delta_{a\alpha}\delta_{a\beta} + \delta_{b\alpha}\delta_{b\beta} - \delta_{a\alpha}\delta_{b\beta} - \delta_{a\beta}\delta_{b\alpha})
\end{eqnarray}

In the notation of the cone programming, there are $K(K+1)/2 + K$ variables for
minimization:
\begin{eqnarray}
  \vec{x} &=& ( \vec{n}^t; \vec{u}^t )^t
  \nonumber \\
    &=& ( n_{11}, n_{12}, \dots, n_{1K}, n_{22}, n_{23}, \dots, n_{K-1\, K}, n_{KK},
          u_1, u_2, \dots, u_K )^t
\end{eqnarray}

The coefficients are
\begin{equation}
\vec{c} = (0, 0, \dots, 0, 1, 1, \dots, 1 )^t
\end{equation}
with $K(K+1)/2$ entries of 0s and $K$ entries of 1s.
\begin{equation}
A = \left( 1, 1, \dots, 1, 0, 0, \dots, 0 \right)
\end{equation}
with $K(K+1)/2$ entries of 1s and $K$ entries of 0s.

\begin{equation}
G = - \left(
\begin{array}{ccccccc}
 & I_{K(K+1)/2} & & & 0_{K(K+1)/2, K} & \\
\mvec(V_{11}^+) & \mvec(V_{12}^+) & \cdots
\mvec(V_{KK}^+) & \mvec{\abcd{0}{0}{0}{1}} & 0 & \cdots \\
\mvec(V_{11}^+) & \mvec(V_{12}^+) & \cdots
\mvec(V_{KK}^+) & 0 & \mvec{\abcd{0}{0}{0}{1}} & \cdots \\
& & & \cdots & & & \\
\mvec(V_{11}^+) & \mvec(V_{12}^+) & \cdots
\mvec(V_{KK}^+) & 0 & \cdots & \mvec{\abcd{0}{0}{0}{1}}
\end{array}
\right)
\end{equation}
where
\begin{equation}
V_{ij}^{+} = \abcd{V_{ij}}{0}{0}{0}
\end{equation}

\begin{equation}
h = \left(
\begin{array}{c}
\vec{0}_{K(K+1)/2}  \\
  \mvec{\abcd{F(\{ n^{(0)} \})}{\vec{e}_1}{\vec{e}_1^t}{0}} \\
  \mvec{\abcd{F(\{ n^{(0)} \})}{\vec{e}_2}{\vec{e}_2^t}{0}} \\
  \vdots \\
  \mvec{\abcd{F(\{ n^{(0)} \})}{\vec{e}_K}{\vec{e}_K^t}{0}}
\end{array}
\right)
\end{equation}

The dimensions of the cone programming is
\begin{equation}
\mathrm{dims} = \left\{
\begin{array}{rl}
  \text{'l'}: & K(K+1)/2 \\
  \text{'q'}: & \text{[ ]} \\
  \text{'s'}: & [ K+1, K+1, \dots, K+1 ]
\end{array}
\right.
\end{equation}

$G$ is a VERY sparse matrix of $K(K+1)/2 + K(K+1)^2$ rows and
$K(K+1)/2 + K$ columns, with highly structured entries.  Solving the
cone programming for large $K$ requires exploiting the structure in
the problem.

We need to solve the KKT equations
\begin{equation}
\left(
\begin{array}{ccc}
0 & A^t & G^t \\
A & 0 & 0 \\
G & 0 & -W^t W
\end{array} \right)
\left(
\begin{array}{c}
\vec{x} \\ y \\ \vec{z}
\end{array} \right)
= \left(
\begin{array}{c}
\vec{p} \\ q \\ \vec{l}
\end{array} \right)
\end{equation}

$\vec{x}$ and $\vec{p}$ are vectors of length $K(K+1)/2 + K$. We will write
\begin{eqnarray}
\vec{x} &=& ( n_{11}, n_{12}, \dots, n_{KK} ; u_1, u_2, \dots, u_K )^t
\nonumber \\
\vec{p} &=& ( p_{11}, p_{12}, \dots, p_{KK} ; \pi_1, \pi_2, \dots, \pi_K )^t
\end{eqnarray}

$y$ and $q$ are two scalar numbers.

$\vec{z}$ and $\vec{l}$ are vectors of length $K(K+1)/2 + K(K+1)^2$. We denote
\begin{eqnarray}
\vec{z} &=& ( z_{11}, z_{12}, \dots, z_{KK}, \mvec( \mat{z}_1)^t, \mvec( \mat{z}_2)^t, \dots, \mvec( \mat{z}_K)^t)^t
\nonumber \\
\vec{l} &=& ( l_{11}, l_{12}, \dots, l_{KK}, \mvec( \mat{l}_1)^t, \mvec( \mat{l}_2)^t, \dots, \mvec( \mat{l}_K)^t)^t
\end{eqnarray}
where each $\mat{z}_i$ and $\mat{l}_i$ is a $K+1$ by $K+1$ square
symmetric matrix.  We will write $\mat{z}_i$ in a block form
\begin{equation}
\mat{z}_i = \left(
\begin{array}{cc}
\mat{z}_i^\ast & \zeta_i \\
\zeta_i^t & z_{i,K+1,K+1}
\end{array} \right)
\end{equation}

$W$ is a block diagonal matrix, satisfying
\begin{equation}
W^t W \vec{z} = \left(
\begin{array}{c}
d_{11}^2 z_{11} \\
d_{12}^2 z_{12} \\
\vdots \\
d_{KK}^2 z_{KK} \\
\mvec( r_1 r_1^t \mat{z}_1 r_1 r_1^t) \\
\mvec( r_2 r_2^t \mat{z}_2 r_2 r_2^t) \\
\vdots \\
\mvec( r_K r_K^t \mat{z}_K r_K r_K^t)
\end{array} \right)
\end{equation}

The first group of KKT equations, $A^t y + G^t \vec{x} = \vec{p}$, are
\begin{equation}
y - z_{ab} - \mvec(V_{ab})^t\cdot\mvec( \sum_i \mat{z}_i^\ast ) = p_{ab}
\text{ for } 1\leq a \leq b \leq K
\label{eq:KKT-1a}
\end{equation}
and
\begin{equation}
-z_{i,K+1,K+1} = \pi_i  \text{ for } i=1,2,\dots, K
\label{eq:KKT-1b}
\end{equation}

There is only one equation for the second group $A \vec{x} = q$:
\begin{equation}
\sum_a n_{aa} + \sum_{a<b} n_{ab} = q
\label{eq:KKT-n-sum}
\end{equation}

The third group of KKT equations, $G\vec{x} - W^t W \vec{z} = \vec{s}$, are
\begin{equation}
-n_{ab} - d_{ab}^2 z_{ab} = l_{ab} \text{ for } 1\leq a \leq b \leq K
\label{eq:KKT-3a}
\end{equation}
and
\begin{equation}
-\abcd{F( \{ n_{ab} \})}{0}{0}{u_i} - r_i r_i^t \mat{z}_i r_i r_i^t = \mat{l}_i
\label{eq:KKT-3b}
\end{equation}
Eq.~\ref{eq:KKT-3b} represents $K(K+1)^2$ equations.

We will first eliminate the variables in $\mat{z}_i^\ast$. Denoting
\begin{equation}
R_i = r_i^{-t} r_i^{-1} = \left(
  \begin{array}{cc}
    R_i^\ast & \gamma_i \\
    \gamma_i^t & R_{i,K+1,K+1}
  \end{array}\right)
\end{equation}
Eq.~\ref{eq:KKT-3b} can be rewritten as
\begin{equation}
R_i \abcd{F(\{n_{ab}\})}{0}{0}{u_i} R_i + \mat{z}_i = - R_i \mat{l}_i R_i
\label{eq:KKT-3b-R}
\end{equation}
Writing $L_i \equiv R_i \mat{l}_i R_i$ in block form
\begin{equation}
L_i = \left(
\begin{array}{cc}
  L_i^\ast & \lambda_i \\
  \lambda_i^t & L_{i,K+1,K+1}
\end{array} \right)
\end{equation}
Eq.~\ref{eq:KKT-3b-R} becomes
\begin{equation}
R_i^\ast F( \{n_{ab}\}) R_i^\ast + \gamma_i \gamma_i^t u_i + \mat{z}_i^\ast = - L_i^\ast
\label{eq:KKT-Fuz}
\end{equation}
\begin{equation}
R_i^\ast F( \{n_{ab}\}) \gamma_i + \gamma_i R_{i,K+1,K+1} u_i + \zeta_i = - \lambda_i
\label{eq:KKT-n-u-zeta}
\end{equation}
and
\begin{equation}
\gamma_i^t F( \{ n_{ab}\})\gamma_i + R_{i,K+1,K+1}^2 u_i + z_{i,K+1,K+1} = -L_{i,K+1,K+1}
\end{equation}
Because $z_{i,K+1,K+1} = -\pi_i$, the last equation is
\begin{equation}
\gamma_i^t F( \{ n_{ab}\})\gamma_i + R_{i,K+1,K+1}^2 u_i = \pi_i - L_{i,K+1,K+1}
\label{eq:KKT-n-u}
\end{equation}

Eq.~\ref{eq:KKT-Fuz} and Eq.~\ref{eq:KKT-1a} together lead to
\begin{equation}
z_{\alpha\beta} = y - p_{\alpha\beta} + \mvec( V_{\alpha\beta})^t\cdot \mvec\left(\sum_i R_i^\ast F(\{ n_{ab}\}) R_i^\ast + \sum_i \gamma_i \gamma_i^t u_i\right) + \mvec(V_{\alpha\beta})^t\cdot \mvec\left(\sum_i L_i^\ast\right)
\end{equation}

Plugging the above in Eq.~\ref{eq:KKT-3a}, we also eliminate $z_{ab}$.
\begin{eqnarray}
&&d_{\alpha\beta}^{-2} n_{\alpha\beta} + \mvec(V_{\alpha\beta})^t\cdot
\mvec\left(\sum_i R_i^\ast F(\{ n_{ab} \}) R_i^\ast + \sum_i \gamma_i \gamma_i^t u_i\right) + y
\nonumber \\
&=& p_{\alpha\beta} - \mvec( V_{\alpha\beta})^t\cdot
\mvec\left( \sum_i L_i^\ast \right) - d_{\alpha\beta}^{-2} l_{\alpha\beta}
\label{eq:KKT-n-u-y}
\end{eqnarray}

Eq.~\ref{eq:KKT-n-u-y} ($K(K+1)/2$ equations), Eq.~\ref{eq:KKT-n-sum}
(1 equation), and Eq.~\ref{eq:KKT-n-u} ($K$ equations) together
determine the variables $\{ n_{ab} \}$ ($K(K+1)/2$ variables), $\{ u_i
\}$ ($K$ variables), and $y$ (1 variable).

The matrix of the form $ R F(\{ n_{ab} \}) R$ is
\begin{eqnarray}
R F( \{ n_{ab} \}) R &=& \sum_a R V_{aa} R n_{aa} + \sum_{a<b} R V_{ab} R n_{ab}
\nonumber \\
&=& \sum_a s_{aa}^{-2} R_a R_a^t n_{aa}
 + \sum_{a<b} s_{ab}^{-2} (R_a R_a^t + R_b R_b^t - R_a R_b^t - R_b R_a^t) n_{ab}
\nonumber \\
\end{eqnarray}
where $R_a$ is the column vector of the $a$th column of $R$.

The vector of the form $ R F(\{ n_{ab} \}) \gamma $ is
\begin{equation}
R F( \{ n_{ab} \}) \gamma = \sum_a s_{aa}^{-2} R_a \gamma_a n_{aa}
  + \sum_{a<b} s_{ab}^{-2} ( R_a \gamma_a + R_b \gamma_b - R_a \gamma_b - R_b \gamma_a) n_{ab}
\end{equation}

The scalar of the form $\gamma^t F(\{ n_{ab} \})$ is
\begin{equation}
\gamma^t F(\{ n_{ab} \}) \gamma = \sum_a s_{aa}^{-2} \gamma_a^2 n_{aa}
 + \sum_{a<b} s_{ab}^{-2} ( \gamma_a^2 + \gamma_b^2 - 2\gamma_a\gamma_b ) n_{ab}
\end{equation}

The inner product $\mvec( V_{\alpha\beta})^t \cdot \mvec( X )$ is
\begin{equation}
\mvec( V_{\alpha\beta})^t \cdot \mvec( X ) = \left\{
\begin{array}{cl}
s_{\alpha\alpha}^{-2} X_{\alpha\alpha}   & \text{ if } \alpha = \beta \\
s_{\alpha\beta}^{-2} \left(X_{\alpha\alpha} + X_{\beta\beta} - X_{\alpha\beta} - X_{\beta\alpha}\right) & \text{ if } \alpha \neq \beta
\end{array} \right.
\end{equation}

The equation for $\{n_{ab}\}$, $\{u_i\}$, and $y$, summarizing Eq.~\ref{eq:KKT-n-u-y}, ~\ref{eq:KKT-n-sum}, and ~\ref{eq:KKT-n-u}, is
\begin{equation}
\left(
\begin{array}{cc}
B & \vec{\eta} \\
\vec{\eta}^t & 0
\end{array}\right)
\left(
\begin{array}{c}
\vec{x} \\
y
\end{array} \right) = \left(
\begin{array}{c}
\vec{x}_0 \\
y_0
\end{array}
\right)
\label{eq:KKT-x-y}
\end{equation}
where $\vec{x} = (n_{11}, n_{12}, \dots, n_{22}, n_{23}, \dots, n_{KK}, u_1, u_2,
\dots u_K)^t$, and $B$ is a symmetric matrix

\begin{equation}
B = \left(
\begin{array}{cc}
A & \vec{\eta} \\
\vec{\eta}^t & \mathrm{diag}( R_{i,K+1,K+1}^2)
\end{array}
\right)
\end{equation}
where $\vec{\eta} = ( 1, 1, ... 1, 0, 0, ... 0 )^t$ is $K(K+1)/2$ 1's
followed by $K$ 0's.

Eq.~\ref{eq:KKT-x-y} can be solved by
\begin{eqnarray}
y &=& \frac{\vec{\eta}^t\cdot B^{-1}\cdot\vec{x}_0 - y_0}{\vec{\eta}^t\cdot B^{-1} \vec{\eta}} \nonumber \\
\vec{x} &=& B^{-1}\vec{x}_0 - y B^{-1}\vec{\eta}
\end{eqnarray}

Some algebra shows that the first $K(K+1)/2$-by-$K(K+1)/2$ submatrix of $A$ is given by
\begin{equation}
A_{\alpha\beta,ab} =
s_{ab}^{-2}s_{\alpha\beta}^{-2} \cdot \left\{
\begin{array}{cl}
\sum_i R_{i,a,\alpha}^2 & \text{ if } a=b, \alpha=\beta \\
\sum_i (R_{i,a,\alpha} - R_{i,b,\alpha})^2 & \text{ if } a\neq b, \alpha=\beta \\
\sum_i (R_{i,a,\alpha} - R_{i,a,\beta})^2 & \text{ if } a=b, \alpha\neq\beta \\
\sum_i ((R_{i,a,\alpha} - R_{i,b,\alpha}) - (R_{i,a,\beta} - R_{i,b,\beta}))^2 & \text{ if } a\neq b, \alpha\neq\beta
\end{array}\right.
\end{equation}

The computation of this submatrix $A_{\alpha\beta,ab}$ is $O(K^5)$ in
time complexity and $O(K^4)$ in memory complexity; it is the most
intensive computation in computing the A-optimal by cone programming.

\section{Including reference input}

Often, some of the quantities, say $\{x_a|a\in Q\}$, are measured by
other experiments to within certain statistical error $\{\tilde{x}_a
\pm \delta_a|a\in Q\}$. The likelihood that our new measurements
produce the set of values $\{ \hat{x}_e \}$ and the other experiments
produce the set of values $\{ \tilde{x}_a | a\in Q\}$, given the true
values of the quantities of $\{ x_i \}$, is
\begin{eqnarray}
L &=& \prod_i (\sqrt{2\pi \sigma_i})^{-1}
  \exp\left( -\frac{(x_i - \hat{x}_i)^2}{2\sigma_i^2} \right)
\nonumber \\
  &\cdot& \prod_{a\in Q} (\sqrt{2\pi \delta_a})^{-1}
  \exp\left( -\frac{(x_a - \tilde{x}_a)^2}{2\delta_a^2} \right)
\nonumber \\
  &\cdot& \prod_{i<j} (\sqrt{2\pi \sigma_{ij}})^{-1}
  \exp\left( -\frac{(x_i - x_j - \hat{x}_{ij})^2}{2\sigma_{ij}^2}\right)
\label{eq:likelihood}
\end{eqnarray}

Maximizing $\ln L$ with respect to $\{ x_i \}$ yields

\begin{equation}
F \vec{x} = \vec{z}
\end{equation}

where
\begin{equation}
F_{ij} = \left\{
\begin{array}{cl}
\delta_i^{-2} + \sigma_i^{-2} + \sum_{k\neq i} \sigma_{ik}^{-2} & \text{ if } i = j \text{ and } i\in Q \\
\sigma_i^{-2} + \sum_{k\neq i} \sigma_{ki}^{-2} & \text{ if } i=j \text{ and } i\notin Q \\
-\sigma_{ij}^{-2} & \text{ if } i\neq j
\end{array}
\right.
\label{eq:Fisher-information-matrix}
\end{equation}
and
\begin{equation}
z_i = \left\{
\begin{array}{cl}
\sigma_i^{-2} \hat{x}_i + \delta_i^{-2} \tilde{x}_i + \sum_{j\neq i}\sigma_{ij}^{-2} \hat{x}_{ij} & \text{ if } i \in Q \\
\sigma_i^{-2} \hat{x}_i + \sum_{j\neq i}\sigma_{ij}^{-2} & \text{ if } i \notin Q
\end{array}
\right.
\end{equation}

The A-optimal can be determined by the same SDP of
Eqs.~\ref{eq:SDP-objective} to~\ref{eq:SDP-Fu}, but the Fisher
information matrix $F$ now includes diagonal terms of $\delta_i^{-2}$.

\end{document}


\chapter{Numbers and Numerals}
\section{Introduction}\label{intro:numbers_and_numerals}
Back in the early 1970s when I first began to struggle with the basics of arithmetic, I was introduced to something called ``The New Math.''  The New Math was well-meant like all of the subsequent efforts to improve the instruction of mathematics have been.  It attempted many things, but the thing I am thinking of now is that it made a big deal about separating the concepts of ``numeral'' and number.  A numeral is a means of denoting a number, but a number is the thing itself.

At the time, being about 10 years old, I thought this was stupid, and to be sure I do so now, but for different reasons.

At the time I thought that I already knew the difference between a numeral and a number.  Now I am no longer sure I know what a number is.

In those days, they segued from teaching this distinction to teaching us about Roman Numerals.  They might've also taught us about putting numbers into different bases, but I cannot recall.

My reason for bringing this all up now is that the purpose of this chapter is to show--to a certain degree of precision--how computers add numbers.  The first step is to represent numbers in a way they can be encoded into the computer, i.e. in binary. The next step is to describe the addition algorithm on a macro level.  The final step, which will be quite protracted, will be to describe how this can be implemented electronically via half-adders, full-adders, logic gates, and mathematically simplified transistors.

This will be done from the point of view of mathematics, not electrical engineering, but giving due respect to the electrical engineers. May God bless them.

\section{Number Representation}\label{section:number_representation}
In \S \ref{intro:numbers_and_numerals}, I spoke about the distinction between numbers and numerals.  I am interested in representing numbers in a way computers can deal with them.  In a certain sense, the most basic idea of a number is that something is either there or it is not; there is either something or there is nothing; something is either True or it is False.  When there is nothing we can denote it as 0; when there is something we can denote it as 1.  When call this a \emph{boolean} number. \index{boolean}

That first step was a little philosophical and I apologize for that.  The next step is more down to earth: counting.  When we count, something even little children can do, we create the counting numbers which mathematicians refer to as \emph{positive integers} or \emph{natural numbers}: \index{positive integers} \index{natural numbers} 1, 2, 3, 4,\dots. We are giving names to positions in an order. You can think of laying out coins or peanuts or corn nuts or a mixture of all of them while giving them names.

Over the ages, we've learned to be systematic in assigning the names so that we always know how to write down the next number even if we don't know how to pronounce it.  We do this by using the so-called Hindu-Arabic numbers which we more generically refer to as base-ten numbers or decimal numbers. (Note how quickly we become careless about the numeral number distinction in ordinary speech.)

In working with computers, we use base-two numbers which are better known as \emph{binary} numbers: \index{binary} 1, 10, 11, 100 \dots Note that we can go on naming our numbers forever while we very quickly lose the ability to read them in their common names.

It now becomes useful for us to write this in the language of mathematics.  When we have a positive integer $b$, there is an integer $n\ge 0$ and a sequence of boolean numbers $b_0, b_1, \dots, b_n$ such that
\[
b = 2^n\cdot b_n + \dots + 2 \cdot b_1 + b_0.
\]
For the sake of brevity, we would typically suppress the $2^i$ in the expansion and write this number as $b=(b_nb_{n-1}\dots b_1b_0)_\text{bin}$. At times when no confusion is possible, we will write $b=b_nb_{n-1}\dots b_1b_0$.

\section{Numbers in a Computer}
As mentioned before, the New Math made a big deal about separating the concepts of numeral and number.  I am now going to make a big deal out of separating a number and its encoding in a computer. Representing the number in binary is just the first step to encoding it.  You may have read somewhere that we use binary numbers because 1 denotes a switch being on and 0 represents a switch being off.  That is not so much wrong as it is incomplete.  Having a 1 can also mean that the voltage is at or above a certain threshhold while having a 0 means that voltage is below that threshhold.  It can also mean other things.  Viewed on paper it is an abstraction.  Abstract thinking is wonderful, but we do need to keep in mind that our abstractions refer to physical entities.  We live in a physical world.

Speaking of living in a physical world, we are limited to the size of number that we can encode.  When we represent a number in binary as $b=b_nb_{n-1}\dots b_1b_0$, each $b_i$ is referred to as a \emph{bit}.  The number $b$ have have just named has $n+1$ bits.  At the time of this writing, computers which can handle numbers 64-bit numbers are quite common.

In this treatment, we will generically talk about $N$-bit numbers and in our examples we will set $N=4$\footnote{Using $N=64$ would add nothing to our understanding and make our exposition impossible to read.  The fact that it would make the writer's life hell is just a coincidence.}.  This will limit us to using only the numbers between 0 and $2^4-1 = 15$, inclusive, in examples but this will not hinder the process we are learning about.

In the language of computer science, we will be dealing with 4-bit unsigned integers.  Dealing with signed integers is only slightly more complicated, but will be deferred to another time.  We will now list all of the 4-bit unsigned integers in Table~\ref{table:4-bit_unsigned_integers}.

\begin{table}\label{table:4-bit_unsigned_integers}
	\begin{center}
		\begin{tabular}{cc}
			$b$ & $(b)_\text{bin}$ \\  \hline
			0 & 0000 \\
			1 & 0001  \\
			2 & 0010  \\
			3 & 0011  \\
			4 & 0100 \\
			5 & 0101 \\
			6 & 0110  \\
			7 & 0111  \\
			8 & 1000  \\
			9 & 1001  \\
			10 & 1010  \\
			11 & 1011  \\
			12 & 1100  \\
			13 & 1101  \\
			14 & 1110  \\
			15 & 1111  \\
		\end{tabular}
	\end{center}
\caption{4-bit unsigned integers}
\end{table}

\section{Binary Addition}
Though most of you already know how to do binary addition by hand or could at least take a creditable stab at it, I would like to start this section with a simple example.  Consider 11 plus 5 being represented in binary as
\begin{center}
	\begin{tabular}{ccccc}
		& 1 & 0 & 1 & 1 \\
	  + & 0 & 1 & 0 & 1
	\end{tabular}
\end{center}

We begin the addition algorithm by adding the rightmost bits.  In this case $1 + 1 = 10$.  In our process we will write down the number $s=0$ ($s$ stands for sum) and $c=1$ ($c$ stands for carry).  This will appear as
\begin{center}
	\begin{tabular}{ccccc}
		& 1 & 0 & $1^1$ & 1 \\
		+ & 0 & 1 & 0 & 1 \\ \hline
		&   &   &       & 0
	\end{tabular}
\end{center}

Let us note that the first step requires only two inputs: one bit from each of the summands.  There are two outputs: sum and carry.

In the next step, we go to the rightmost but one column. We take the carry from the previous step, which you recall was 1 in this case, and add it to the 1 from the top summand.  This gives us $1+1 = 10$.  We now add the 0 from the lower summand this gives us $10 + 0 = 10$. So we again put down an $s=0$ and carry $c= 1$.  This will appear as
\begin{center}
	\begin{tabular}{ccccc}
		& 1 & $0^1$ & $1^1$ & 1 \\
		+ & 0 & 1 & 0 & 1 \\ \hline
		&   &   &   0    & 0
	\end{tabular}
\end{center}

What we need to know from this step is that there are actually three inputs: one bit from each summand as before and the carry value $c$ from the previous step.

Continuing in this way until we are done will give us

\begin{center}
	\begin{tabular}{ccccc}
		& $1^1$ & $0^1$ & $1^1$ & 1 \\
		+ & 0 & 1 & 0 & 1 \\ \hline
		&   & 0  &     0  & 0
	\end{tabular}
\end{center}
and
\begin{center}
	\begin{tabular}{ccccc}
		& $1^1$ & $0^1$ & $1^1$ & 1 \\
		+ & 0 & 1 & 0 & 1 \\ \hline
	1	&  0 & 0  &     0  & 0
	\end{tabular}
\end{center}

The final sum of 11 and 5 is 16.  This would result in an overflow error on a 4-bit machine.

In the sequel, we will ape this process and create two black box devices: the half-adder and the full-adder.  We will then shed light on what goes on inside these black boxes.  This will take a while.

\subsection{The Half-Adder}
The \emph{half-adder}\index{half-adder} is an electronic device\footnote{Think of it as a chip.} that will take in two bits and return their sum in the form of two bits called the sum (denotde by $s$) and the carry (denoted by $c$).  How it works is illustrated in Table~\ref{table:half-adder}.

\begin{table}
			\begin{center}
		\begin{tabular}{ccccc}
			\multicolumn{2}{c}{Inputs}& & \multicolumn{2}{c}{Outputs} \\
			$b_1$ & $b_2$ &             & $s$ & $c_{out}$ \\ \hline
			0   &   0   & $\rightarrow$ & 0   & 0  \\
			0   &   1   & $\rightarrow$ & 1   & 0  \\
			1   &   0   & $\rightarrow$ & 1   & 0  \\
			1   &   1   & $\rightarrow$ & 0   & 1  \\
		\end{tabular}
	\end{center}
\caption{Half-Adder}\label{table:half-adder}
\end{table}
The table describes how the half-adder works in a mathematical way.  This is fine, as far as it goes, but when time comes to design a machine we will need to think the half-adder as being a chip that we will plug in to a printed circuit board(PCB).  To aid in this, we have created the diagram in Figure~\ref{figure:half-adder}.
\begin{figure}
	\begin{center}
		\includegraphics[width = 4.0in]{half_adder.png}
	\end{center}
	\caption{Half-Adder}\label{figure:half-adder}
\end{figure}
In the figure we have inputs labeled $b_1$ and $b_2$.  These will have current or voltage (or the lack thereof) associated with them according to whether they are 1s or 0s.   There is an outflow of current or voltage to $s$ and $c$.

The phrase ``black box'' is meant to evoke mystery.  We know what the black box does, but we do not know how.  Our aim is to dispell this mystery to a certain degree. We will push to the boundary of physics, but we will not step over.

\subsection{Full-Adder}
We will present a \emph{full-adder}\index{full-adder} in a black box in a way similar to what we did for the half-adder above.  See Table~\ref{table:full-adder}
\begin{table}
	\begin{center}
	\begin{tabular}{cccccc}
		\multicolumn{3}{c}{Inputs} &  & \multicolumn{2}{c}{Outputs} \\
		$c_\text{in}$ & $b_1$ & $b_2$ &             & $s$ & $c_\text{out}$ \\ \hline
		0 & 0   &   0   & $\rightarrow$ & 0   & 0  \\
		0 & 0   &   1   & $\rightarrow$ & 1   & 0  \\
		0 & 1   &   0   & $\rightarrow$ & 1   & 0  \\
		0 & 1   &   1   & $\rightarrow$ & 0   & 1  \\
		1 & 0   &   0   & $\rightarrow$ & 1   & 0  \\
		1 & 0   &   1   & $\rightarrow$ & 0   & 1  \\
		1 & 1   &   0   & $\rightarrow$ & 0   & 1  \\
		1 & 1   &   1   & $\rightarrow$ & 1   & 1  \\
	\end{tabular}
\end{center}
	\caption{Full-Adder}\label{table:full-adder}
\end{table}
Note that we have three inputs the bits $b_1$ and $b_2$ from our summands and the carry from our previous step that we denote as $c_\text{in}$.  We have outputs: our sum $s$ and our carry that we denote as $c_\text{out}$.

As before, we will want to think of this as a chip that will be plugged into a PCB.  We do this in Figure~\ref{figure:full-adder}.
\begin{figure}
	\begin{center}
		\includegraphics[width = 4.0in]{full_adder.png}
	\end{center}
	\caption{Full-Adder}\label{figure:full-adder}
\end{figure}
Again the lines are standing in for electrical connections, with $c$, $b_1$, and $b_2$ denoting inputs from the bit carried in and the bits from the summands. The outputs $c$ and $s$ denote the bit we carry out and the sum that we write down at this step, respectively.

We wish to justify the terminology half-adder and full-adder by showing that the full-adder is composed of two half-adders.  This is, in fact, ``almost'' true.  We need one more item.  This appears to be a small thing at first, but it will open the world of logic gates to us.

\section{Our First Taste of Logic}
In \S~\ref{section:number_representation}, we mentioned boolean numbers.  there we said we could use 1 to denote if something was present or a statement was true and 0 if a statement was false or something was absent. This is of use in Boolean Algebra and logic.  These are broad topics whos surface we will only begin to skim. In order to get our toes wet, while finding a way to demystify the full-adder, we will now introduce \emph{logical OR}.

The logical OR operation is an binary operation on bits.  In this context, binary means it has two operands like the plus operation does.  We denote logical OR by the symbol $\vee $.  Let $p$ and $q$ be boolean values or bits.  Then $p\vee  q$ (read as $p$ or $q$) is $0$ exactly when both $p$ and $q$ are $0$ and is equal to $1$ all other times.  We make this specific in Table~\ref{table:logical_or}.
\begin{table}
			\begin{center}
		\begin{tabular}{ccc}
			\multicolumn{2}{c}{Inputs} & Output \\
			$b_1$ & $b_2$ & $b_1\vee b_2$  \\ \hline
			0     & 0     &    0 \\
			0     & 1     &    1  \\
			1     & 0     &    1  \\
			1     & 1     &   1
		\end{tabular}
	\end{center}
\caption{Logical OR}\label{table:logical_or}
\end{table}

As with all of our operation, we are interested in ``creating a chip'' to implement this operation and in plugging that chip into a PCB.  As logical OR is a basic operation, there is a standard wiring diagram symbol for it.  We call the symbol an \emph{OR-gate}\index{OR-gate}, and it is shown in Figure~\ref{figure:or_gate}.
\begin{figure}
	\begin{center}
		\includegraphics[width = 3.0in]{OR_Gate.png}
	\end{center}
\caption{An OR-gate}\label{figure:or_gate}
\end{figure}

Note that the OR-gate is shaped vaguely like an arrowhead.  We can think of the arrowhead as indicating the direction of motion of current through it.  The two horizontal segments on the lefthand side are inputs where the electrical manifestations of $p$ and $q$ are connected.  Let horizontal segment on the right is where the current associated with $p\vee  q$ is output.

This is just the first of many logic gates.

\section{Full-Adder, Revisited}
Let us now revisit the process of adding binary numbers.  In every step but the first one, we have three inputs: $c$, carry-in; $b_1$, a bit from the first summand; $b_2$, a bit from the second summand.  While using the full-adder to do this creates the impression that this is done in one seemless step, to anyone doing this by hand, it is absolutely clear that we are combining two operations.

First we perform the addition $c + b_1$ on a half-adder, call it $HA_1$. This will yield a carry-out bit that we will call $c_1$ and a sum bit that we will call $s_1$.

After doing this we perform the second operation on another half-adder that we will call $HA_2$.  this will yield a carry-out of $c_2$ and a sum of $s$.  This $s$ is the sum of the full-adder.

The carry-out of the full-adder is obtained by combining $c_1$ and $c2$.  This could be done simply by adding them together, and indeed, this is what we do when we do the work by hand.  However, our purpose here is to try to get something simpler.

Let us first note that \emph{at most} one of the $c_1$ and $c_2$ is equal to 1.  If they are both 0, then $c_1 + c_2 = 0$ and $c_1 \vee  c_2 =0$.  If exactly one of $c_1$ and $c_2$, then $c_1+c_2 = 1$ and $c_1 \vee  c_2 = 1$. Therefore, the final carry-out of our full-adder is $c_1\vee c_2$.  We can therefore diagram a full-adder in term of half-adders as in Figure~\ref{figure:full-adder_in_terms_of_half_adders}.
\begin{figure}
		\begin{center}
		\includegraphics[width = 5.0in]{full_adder_diagram.png}
	\end{center}
\caption{Full-Adder in Terms of Half-Adders}\label{figure:full-adder_in_terms_of_half_adders}
\end{figure}

We are still in a position where we've put something we don't know how to design in terms of transistors into other things we don't know how to design in terms of transistors, but at least we've dealt with the full-adder.  To deal with the half-adder and the OR-gate we will have to learn some more logic.

\section{Other Logical Operators}

In this section, we will learn about two more basic logical operators in addition to OR: AND and NOT. Besides these, we will learn about the composite operators NAND and exclusive OR (XOR).  We will also learn about the corresponding logic gates.

We will denote the binary logical operator AND by the symbol $\wedge$. Given the boolean values $p$ and $q$, $p\wedge q$ will be 1 exactly when both $p$ and $q$ are equal to 1 and for all other combinations $p\wedge q$.  This gives us the Table~\ref{table:logical_and}.
\begin{table}
		\begin{center}
	\begin{tabular}{ccc}
		\multicolumn{2}{c}{Inputs} & Output \\
		$p$ & $q$ & $p\wedge q$  \\ \hline
		0     & 0     &    0 \\
		0     & 1     &    0  \\
		1     & 0     &    0 \\
		1     & 1     &   1
	\end{tabular}
\end{center}
\caption{Logical AND}\label{table:logical_and}
\end{table}
The AND-gate is shown in Figure~\ref{figure:and_gate}.
\begin{figure}
		\begin{center}
		\includegraphics[width = 1.5in]{AND_Gate.png}
		\end{center}
		\caption{AND Gate}\label{figure:and_gate}
\end{figure}

In contrast to all of the other operators we've encountered, the NOT operator is not a binary operator but a unitary operator.  We denote it by $\neg$.  Given a boolean value $p$, $\neg p$ has the opposite value, that is $\neg 1 = 0$ and $\neg 0 = 1$. We display this in Table~\ref{table:not_operator}.
\begin{table}
		\begin{center}
	\begin{tabular}{cc}
		$p$ & $\neg p$ \\ \hline
		0   & 1  \\
		1   & 0
	\end{tabular}
\end{center}
\caption{The NOT operator}\label{table:not_operator}
\end{table}
It is denoted in electronic diagrams by the symbol in Figure~\ref{figure:not_gate}.
\begin{figure}
		\begin{center}
		\includegraphics[width= 3.0in]{NOT_Gate.png}
		\end{center}
	\caption{The NOT gate}\label{figure:not_gate}
\end{figure}

The NAND operator, as its name would suggest, is a composite of the AND and NOT operators.  We denote this binary operator by $\uparrow$.  If $p$ and $q$ are boolean values, then $p\uparrow q = \neg (p \wedge q)$.  Note that $p\uparrow q$ is 1 except when $p$ and $q$ are both~1.  We build up in this operator in Table~\ref{table:logical_nand}.
\begin{table}
		\begin{center}
	\begin{tabular}{ccccc}
		$p$ & $q$ & $p\wedge q$ & $\neg (p\wedge q)$ & $p\uparrow q$   \\ \hline
		0  &   0   &    0          &        1        & 1            \\
		0  &   1   &    0          &        1        & 1            \\
		1  &   0   &    0          &        1        & 1            \\
		1  &   1   &    1          &        0        & 0            \\
	\end{tabular}
\caption{Logical NAND}\label{table:logical_nand}
\end{center}
\end{table}
The NAND gate is in Figure~\ref{figure:nand_gate}.
\begin{figure}
		\begin{center}
		\includegraphics[width = 3.00in]{NAND_Gate.png}
		\end{center}
	\caption{NAND Gate}\label{figure:nand_gate}
\end{figure}

Our final composite operator is \emph{exclusive OR}\index{exclusive OR}, i.e. XOR.  We denote XOR by $\bigoplus$.  Given boolean values $p$ and $q$, $p\bigoplus q = 1$ exactly when one of $p$ and $q$ is equal to 1 and is 0 for all other combinations of $p$ and $q$.  We can write $p\bigoplus q$ in terms of the other operators as
\[
p\bigoplus q = (p\vee q)\wedge \neg(p\wedge q).
\]
This is justified in Table~\ref{table:logical_xor}.
\begin{table}
			\begin{center}
			\begin{tabular}{cccccc}
				$p$ & $q$ & $p\vee q$ & $p\wedge q$ & $(p\vee q)\wedge \neg (p\wedge q)$ & $p\bigoplus q$  \\ \hline
				0  &   0   &    0          &        0        & 0   &  0         \\
				0  &   1   &    1          &        0        & 1   &  1         \\
				1  &   0   &    1          &        0        & 1   &  1         \\
				1  &   1   &    1          &        1        & 0   &  0         \\
			\end{tabular}
			\end{center}\caption{Logical XOR}\label{table:logical_xor}
\end{table}
The XOR gate is in Figure~\ref{figure:xor_gate}.
\begin{figure}
	\begin{center}
	\includegraphics[width = 3.00in]{XOR_Gate.png}
	\end{center}\caption{XOR-gate}\label{figure:xor_gate}
\end{figure}

For those who've worked with modular arithmetic, it is quite natural to identify the boolean values 0 and 1 with the elements of $\mathbb{Z}_2 = \{0, 1\}$ of the fields of integers modulo 2.  This is actually quite useful in the case of AND and XOR the operations denoted as $\wedge$ and $\bigoplus$ are equivalent to multiplication and addition in that setting.

\section{The Half-Adder, Revisited}

In this section, we will obtain a diagram for the half-adder in terms of basic logic gates.  This will be made easy with the help of Table~\ref{table:xor_and_half-adder}, which is quite evocative.
\begin{table}
	\begin{center}
		\begin{tabular}{cccccc}
			$b_1$ & $b_2$ & $b_1\bigoplus b_2$ & $b_1\wedge b_2$ & $s$ & $c_\text{out}$  \\ \hline
			0  &   0   &    0          &        0        & 0   &  0         \\
			0  &   1   &    1          &        0        & 1   &  0         \\
			1  &   0   &    1          &        0        & 1   &  0         \\
			1  &   1   &    0          &        1        & 0   &  1         \\
		\end{tabular}
	\end{center}\caption{A Comparison of XOR, AND, and the outputs of the half-adder}\label{table:xor_and_half-adder}
\end{table}
Note that $b_1\bigoplus b_2$ is exactly the same as $s$, the so-called sum of the half-adder.  Also note that $b_1\wedge b_2$ is exactly the same as $c_\text{out}$, the carry-out of the half-adder.

Using this information and the associated logic gates, we can wire up our half-adder as in Figure~\ref{figure:half-adder_logic_gates}
\begin{figure}
	\begin{center}
		\includegraphics[width = 4.00in]{half_adder_diagram.png}
	\end{center}\caption{Half-Adder in terms of Logic Gates}\label{figure:half-adder_logic_gates}
\end{figure}

Note that in diagrams such as these, we assume that crossing line segments do not interact unless there is a thick dot at the point of intersection.  There are no such thick dots in this diagram.

This how leaves us with the task of writing the logic gates in terms of transistors.  This is a two-fold process: First we will show that AND, OR, and XOR can be written in terms of NAND; then we will show that NAND can be written in terms of transistors.  For the time being, we will hide the second of these behind a veil as would a skilled exotic dancer.  The first requires a little more logic.

\section{Just a Little More Logic}

We will now present some formulas form logic that will be of great use to us.  Their truth can be verified by creating truth tables for them.  We will do this ourselves in one case and leave the rest to the interested reader.

Absorbtion:
\begin{eqnarray}
	p \vee p & = & p \\
	p \wedge p & = & p
\end{eqnarray}

Involution:
\begin{equation}
	\neg (\neg p) = p.
\end{equation}

De Morgan's Laws:
\begin{eqnarray}
	\neg (p\vee q) & = & \neg p \wedge \neg q  \\ \label{eqnarray:demorgan_1}
	\neg (p\wedge q) & = & \neg p \vee \neg q \label{eqnarry:demorgan_2}
\end{eqnarray}

To verify Equation~\ref{eqnarry:demorgan_2}, consider the truth table in Table~\ref{table:demorgan_2}.

\begin{table}
	\begin{center}
	\begin{tabular}{ccccccc}
		$p$ & $q$ & $\neg p$ & $\neg q$ & $p \wedge q$ & $\neg(p \wedge q)$ & $\neg p \vee \neg q$  \\ \hline
		 0  & 0   &    1     &  1       &   0          &  1                 &    1                  \\
		 0  & 1   &    1     &  0       &   0          &  1                 &    1                  \\
		 1  & 0   &    0     &  1       &   0          &  1                 &    1                  \\
		 1  & 1   &    0     &  0       &   1          &  0                 &    0                  \\
	\end{tabular}
	\end{center} \caption{Truth Table for Equation~\ref{eqnarry:demorgan_2}}\label{table:demorgan_2}
\end{table}

We will now talk about the following identities where we put NOT, AND, OR, and XOR in terms of NAND:
\begin{enumerate}
	\item $\neg p = p\uparrow p$ \label{identity:not}
	\item $p\wedge q = (p\uparrow q)\uparrow (p\uparrow q)$ \label{identity:and}
	\item $p\vee q = (p\uparrow p)\uparrow (q\uparrow q)$	\label{identity:or}
	\item $p\bigoplus q = (p\uparrow(p\uparrow q))\uparrow (q\uparrow (p\uparrow q))$ \label{identity:xor}
\end{enumerate}

We will demonstrate these identities.  All of our demostrations are straight-forward calculations with the exception of (\ref{identity:xor}).

To show (\ref{identity:not})  we make use of absorbtion to show:
\begin{eqnarray*}
 \neg p & = & \neg(p\wedge p) \\
 		& = & p \uparrow p
\end{eqnarray*}

We use involution and (\ref{identity:not}) to show:
\begin{eqnarray*}
	p \wedge q & = & \neg \neg (p\wedge q) \\
	           & = & \neg (p\uparrow q) \\
	           & = & (p\uparrow q) \uparrow (p\uparrow q)
\end{eqnarray*}

We can now apply absorbtion, (\ref{eqnarray:demorgan_1}), and (\ref{identity:or}) to show:
\begin{eqnarray}
	p \vee q &=& \neg (\neg p \wedge \neg q) \\
	         &=& \neg p \uparrow \neg q \\
	         &=& (p\uparrow p) \uparrow (q\uparrow q)
\end{eqnarray}

Making a truth table is the easiest way to verify (\ref{identity:xor}), and we leave this to the reader.  Using the forumlas (\ref{identity:not}) - (\ref{identity:xor}), we can create diagrams for AND, OR, and XOR in terms of NAND in Figures \ref{figure:and_in_terms_of_nand}-\ref{figure:xor_in_terms_of_nand}.
\begin{figure}
	\begin{center}
	\includegraphics[width = 4in]{and_diagram.png}
\end{center}\caption{AND in terms of NAND with inputs $b_1$ and $b_2$}\label{figure:and_in_terms_of_nand}
\end{figure}
\begin{figure}
	\begin{center}
		\includegraphics[width = 4in]{or_diagram.png}
	\end{center}\caption{OR in terms of NAND with inputs $A$ and $B$}\label{figure:or_in_terms_of_nand}
\end{figure}
\begin{figure}
	\begin{center}
		\includegraphics[width = 4in]{xor_diagram.png}
	\end{center}\caption{XOR in terms of NAND with inputs $A$ and $B$}\label{figure:xor_in_terms_of_nand}
\end{figure}

\section{Transistors and NAND}

We now come to the last section which is the least mathematical part of the chapter.  We will talk about transistors.  To be precise, we are not talking about specific transistors, but about mathematically idealized transisitors.  To talk about specific transistors would require discussion of specific voltages, specific resistances, etc.  These details belong properly to the world of electronics or, dare we say, electrical engineering.  We won't go to that world--at least in this chapter.

Indeed, we are going to limit ourselves to exactly one type of transistor: a mathematically idealized npn transistor.  See Figure~\ref{figure:npn_transistor}.
\begin{figure}
	\begin{center}
		\includegraphics[width = 4in]{npn_transistor_labeled.png}
	\end{center}\caption{Mathematical npn Transistor}\label{figure:npn_transistor}
\end{figure}
In the figure, there are three distringuished points: $C$, $B$, and $E$.  We will call $C$ the \emph{collector}, $E$ the \emph{emitter}, and $B$ the \emph{base}.

For pedagogical purposes, the collector and the emitter are well-named.  The collector is the place where electricity goes in, the place that collects it.  The emitter is the place where electricity goes out, the place that emits it.

Pedagogically, it would have been better to refer to the base as the button.  If you apply a particular voltage to the base, the transistor will then allow current to pass from the collector to the emittor.  A voltage at $B$ allows current to go from $C$  and pass out of the transistor through $E$.  A transistor is an electronic switch.

Now consider Figure~\ref{figure:nand_diagram}.
\begin{figure}
	\begin{center}
		\includegraphics[width = 4in]{nand_diagram.png}
	\end{center}\caption{A NAND gate in terms of npn Transistors}\label{figure:nand_diagram}
\end{figure}
There is voltage applied to point $V_\text{in}$.  In the diagram, proceeding downward from $V_\text{in}$, there is a branch point where our diagram separates into two paths: one horizonal; one vertical. At this point the current could go either horizontally to $C$ or it could proceed down vertically through two transistors that are connected in series.

We will imagine the resistance from the branch point to $C$ to be a bit greater than that of the series of transistors when both of them are activated. \footnote{I \emph{resisted} the temptation to talk about resistors.}

If $A$ and $B$ are both 1, then the current from $V_\text{in}$ will be allowed to go to ground.  This means there will be insufficient voltage at $C$, i.e. $C=0$.  Otherwise, $C=1$ because if either $A$ or $B$ is equal to 0 current will not be allows to pass through the series of transistors.  This is written in detail in Table~\ref{table:c_is_a_nand_b}.
\begin{table}
	\begin{center}
		\begin{tabular}{cccc}
			$A$ & $B$ & $C$ & $A\uparrow B$ \\ \hline
			0   & 0   &  1  & 1 \\
			0   & 1   &  1  & 1 \\
			1   & 0   &  1  & 1 \\
			1   & 1   &  0  & 0
		\end{tabular}
	\end{center}\caption{$C$ is $A\uparrow B$}\label{table:c_is_a_nand_b}
\end{table}
We see that the value at $C$ is $A\uparrow B$.

\section{Epilog}

We are at the end of our journey.  In principle, you could build an electronic device to add numbers.  This would require using transistors to make NAND gates: NAND gates to make the various other logic gates; and the various other logic gates to make half-adders and full-adders.  When it is all put together, it is a very complicated device.

There have been sturdy individuals who've done just that.  I doff my hat to them.  For my part, I am in a state of awe to those who created the means of tackling such complexity.


%auto-ignore
\providecommand{\MainFolder}{..}
\documentclass[\MainFolder/Text.tex]{subfiles}

\begin{document}
\section{Uniqueness of Hodge propagator}\label{Sec:TZ}
\allowdisplaybreaks
\begin{Proposition}[Uniqueness of Hodge homotopy]\label{Prop:UniHO}
Let $M$ be a closed oriented manifold, and let $\DR(M)=\Im\Dd\oplus\Im\CoDd\oplus\Harm$ be its Hodge decomposition. For any linear map $\Htp:\DR(M)\rightarrow\DR(M)$ satisfying
\begin{equation}\label{Eq:GreenOp}
\Dd\circ\Htp + \Htp\circ\Dd = \iota_\Harm\circ\pi_\Harm - \Id,
\end{equation}
there exist linear maps $R_1: \Im \Dd \rightarrow \Im \Dd$, $R_2: \Im \CoDd \rightarrow \Ker \Dd$ and $R_3: \Harm \rightarrow \Ker \Dd$ such that
\begin{equation} \label{Eq:GreenOpForm}
\Htp = \begin{cases}
 -(\Restr{\Dd}{\Im\CoDd})^{-1} + R_1 & \text{on }\Im \Dd, \\
 (\Restr{\Dd}{\Im\CoDd})^{-1} R_1 \Dd + R_2 & \text{on } \Im \CoDd, \\
 R_3 & \text{on }\Harm.
\end{cases}
\end{equation}
Note that $\Restr{\Dd}{\Im \CoDd} : \Im \CoDd \rightarrow \Im \Dd$ is an isomorphism.
Moreover, the following facts are equivalent for an operator $\Htp$ satisfying \eqref{Eq:GreenOp}:
\begin{enumerate}[label=(\arabic*)]
\item $\Htp = \StdHtp$ is the standard Hodge homotopy,
\item $R_1 = R_2 = R_3 = 0$,
\item $\Im\Htp\subset\Im\CoDd$,
\end{enumerate}
\end{Proposition}
\begin{proof}
Suppose first that $\omega \in \Im\Dd$. Plugging $\omega$ in \eqref{Eq:GreenOp}, we get that $\Htp \omega$ has to satisfy
$$ \Dd \Htp \omega = \omega. $$
We see that
$$ \Htp\omega \in \eta + \Ker \Dd, $$
where $\eta$ is the unique coexact form with $\Dd \eta = \omega$. In other words, $\eta$ is the preimage of $\omega$ under the isomorphism $\Dd: \Im \CoDd \rightarrow \Im \Dd$, and we can write
\begin{equation}\label{Eq:GOpOnClosed}
\Htp \omega = (\Restr{\Dd}{\Im \CoDd})^{-1} \omega + R_1 \omega,
\end{equation}
where $R_1: \Im \Dd \rightarrow \Ker \Dd$.

Suppose now that $\omega \in \Im\CoDd$. Using \eqref{Eq:GreenOp} and \eqref{Eq:GOpOnClosed}, we obtain
$$ \Dd \Htp \omega = \omega - \Htp \Dd \omega = \underbrace{\omega - (\Restr{\Dd}{\Im \CoDd})^{-1} \Dd \omega}_{=0}- R_1 \Dd \omega, $$
where the two terms cancel because $\omega$ is the unique coexact primitive to $\Dd \omega$. Notice that this equation restricts $R_1$ to $R_1: \Im \Dd \rightarrow \Im \Dd$. Similarly as in the first case, we obtain
$$ \Htp \omega = - (\Restr{\Dd}{\Im\CoDd})^{-1} R_1 \Dd \omega + R_2 \omega, $$
where $R_2: \Im\CoDd \rightarrow \Ker \Dd$.

If $\omega\in \Harm$, then \eqref{Eq:GreenOp} gives
$$ \Dd \Htp \omega = 0, $$
and hence $\Htp \omega = R_3 \omega$ for some $R_3: \Harm \rightarrow \Ker \Dd$.

Therefore, \eqref{Eq:GreenOp} implies \eqref{Eq:GreenOpForm}. The other direction clearly holds as well.

As for the equivalent facts, because $\Ker \Dd \perp \Im \CoDd$ (with respect to the $L^2$-inner product), it is clear from \eqref{Eq:GreenOpForm} that $\Im(\Htp)\subset \Im(\CoDd)$ is equivalent to $R_1 = R_2 = R_3 = 0$. Therefore, if there is a $\Htp$ satisfying \eqref{Eq:GreenOp} and (3), then it is unique; it must be $\HtpStd = -\CoDd\GOp$.
\end{proof}

We see that the standard Hodge homotopy $\HtpStd$ can be characterized as the unique Hodge homotopy with coexact image.

We would like to use the equation
\begin{equation}\label{Eq:DFGDFG}
\Dd \StdPrpg = \pm \HKer \quad\text{on }(M\times M)\backslash\Diag
\end{equation}
and characterize $\StdPrpg$ as its unique coexact solution. This is probably not enough and additional assumptions on the asymptotic behavior near $\Diag$ are needed.

Another idea is to lift the equation \eqref{Eq:DFGDFG} to the blow-up and study primitives to $\pi^*\HKer$ on $\Bl_\Diag(M\times M)$. The advantage is that $\Bl_\Diag(M\times M)$ is a compact manifold with boundary, and hence for a given Riemannian metric, we have the Hodge decomposition with boundary conditions.  However, the pull-back metric $g$ along $\pi : \Bl_\Diag(M\times M)\rightarrow M\times M$ is singular at the boundary. We would have to approximate it with a family of metrics~$g_t$ in a correct way, use Hodge theory to find a $\CoDd$-primitive $\eta_t$ to a unique coexact $\Dd$-primitive~$\Prpg_t$ to $\pi^*\HKer$ with certain boundary conditions, and finally prove that $\Prpg_t$ converges to a solution of \eqref{Eq:DFGDFG} on the interior and $\eta_t$ to its $\CoDd$-primitive.

Natural differential operators on $\DR(M\times M)$ do not always pull-back to differential operators on $\DR(\Bl_\Diag(M\times M))$ via the blow-down map. The total differential $\Dd$ does, but the operators $\Id \otimes \Dd_y$ and $\Dd_x \otimes \Id$ do not. We suppose that none of $\CoDd$, $\Id \otimes \CoDd_y$ and $\CoDd_x \otimes \Id$ does. We illustrate the consequences on the following seemingly pathological example.
%
%if the $n-1$ homology of configuration space is zero
%$$ \GKer_1 - \GKer_2 \in \Im\Dd \cap \Im \CoDd $$
%or
%$$ \GKer_1 - \GKer_2 \in \Ker\Dd \cap \Im\CoDd $$
%if it is non-zero. Study asymptotics of such forms. They are in particular all harmonic. Maybe the condition that they smoothly extend to blow-up imply that the difference must be zero by the maximum principle.
\begin{Proposition}[Pathological example]\label{Prop:PatEm}
For any $n\in \N$, there exists a smooth form $\eta \in \DR(\Bl_\Diag(\R^n\times \R^n))$ with compact vertical support with respect to the fiber bundle $\tilde{\pi}_2 = \Pr_2 \circ \pi : \Bl_\Diag(\R^n\times \R^n) \rightarrow \R^n$ such that $\Dd_y \eta = 0$ on $(\R^n\times \R^n)\backslash \Diag$ but $\Dd \tilde{\pi}_{2*}\eta \neq 0$.

If $\eta \in \DR(\Bl_\Diag(\R\times \R))$ is as above and satisfies in addition $\tau^* \eta = \pm \eta$, then $\Dd_y \eta = 0$ on $\R\times \R \backslash \Diag$ implies $\Dd \tilde{\pi}_{2*}\eta = 0$.
\end{Proposition}
\begin{proof}
Let us start with $n=1$. Let $f: \R\times \R\backslash\Diag\rightarrow \R$ be a function such that
\begin{enumerate}
 \item $f: \R\times \R\backslash\Diag\rightarrow \R$ is smooth.
 \item It holds $\frac{\partial f}{\partial y}(x,y) = 0$ for all $(x,y)\in \R\times \R\backslash \Diag$.
 \item For every $y\in \R$, the function $f(\cdot,y)$ defined on $\R\backslash\{y\}$ has compact support in $\R\backslash\{y\}$.
\end{enumerate}
Clearly, (1) and (2) implies
\begin{equation}\label{Eq:PartialConst}
 f(x,y) = \begin{cases} f^-(x) & \text{for }x<y, \\ f^+(x) & \text{for }x>y, \end{cases}\quad\text{for all }(x,y)\in \R\times \R\backslash \Diag,
\end{equation}
where $f^+$, $f^-: \R \rightarrow \R$ are smooth functions. It is easy to see that (3) implies the existence of $x^+$, $x^-\in \R$ such that
$$ f^+(x) = 0\quad\text{for all }x>x^+ \quad\text{and}\quad f^-(x) = 0\quad\text{for all }x<x^-. $$
We consider the form
\begin{equation}
\eta(x,y) = f(x,y) \Diff{x}\quad\text{on }\R\times\R\backslash\Diag.
\end{equation}
In general, a form $\eta(x,y)$ on $\R^n\times\R^n\backslash\Diag$ is a restriction of a smooth form on $\Bl_\Diag(\R^n\times \R^n)$ if and only if the form $(\Phi^*\eta)(r,\omega,u)$ for the diffeomorphism $\Phi: (0,\infty) \times \Sph{n-1} \times \R^n \rightarrow \R^n \times \R^n \backslash \Diag$ given by $\Phi(r,\omega,u) = (u+r\omega,u)$ is a restriction of a smooth form on $[0,\infty) \times \Sph{n-1}\times \R^n$. In our case, we have
$$ (\Phi^*\eta)(r,u) = \begin{cases}
 f^+(u+r)\bigl(\Diff{u}+\Diff{r}\bigr) & \text{on } D^+:= [0,\infty) \times \{1\}\times \R, \\
 f^-(u-r)\bigl(\Diff{u}-\Diff{r}\bigr) & \text{on }D^-:=[0,\infty) \times \{-1\} \times \R,
\end{cases} $$
where we used the fact that $\Sph{0} = \{\pm 1\}$ and splitted the domain in two connected components. We see that $\Phi^* \eta$ extends smoothly to $[0,\infty) \times \Sph{0}\times \R$ in the obvious way. We denote the extension by $\widetilde{\Phi^* \eta}$ and the induced extension of $\eta$ to $\Bl_\Diag(\R\times \R)$ by $\tilde{\eta}$. It holds $\tilde{\Phi}^* \tilde{\eta} = \widetilde{\Phi^* \eta}$ under the extended diffeomorphism $\tilde{\Phi}: [0,\infty) \times \Sph{0} \times \R \rightarrow \Bl_\Diag(\R\times\R)$. The fiberwise integral along the smooth oriented fiber bundle $\tilde{\pi}_2: \Bl_\Diag(\R\times \R) \rightarrow \R$ transforms under $\tilde{\Phi}$ to the fiberwise integral along $\tilde{p}_3: [0,\infty) \times \Sph{0} \times \R \rightarrow \R$, and we get for all $y=u \in \R$ the following:
$$ (\tilde{\pi}_{2*}\tilde{\eta})(y) = \tilde{p}_{3*}(\widetilde{\Phi^*\eta})(u) = \int_0^\infty \bigl(f^+(u+r) + f^-(u-r) \bigr)\Diff{r}. $$
The algebraic sign of the second term was canceled by the geometric sign coming from different orientations of $[0,\infty)$ in $[0,\infty)\times\{0\}$ and in $[0,\infty)\times \{1\}$. We think of $\tilde{\Phi}$ as of an isomorphism of fiber bundles $\tilde{p}_3$ and $\tilde{\pi}_2$ covering the identity, which explains the notation $y=u$. The fiberwise integration along $\tilde{p}_3: [0,\infty) \times \Sph{0} \times
 \R \rightarrow \R$ reduces to the Lebesgue integration of a smooth function $g$ on $[0,\infty) \times \Sph{\omega} \times \R$ with respect to $(r,\omega)$. Clearly, we can permute any differential operator acting on $u$ with the integral.
 In our case, we use $\Dd$ and obtain
\begin{align*}
(\Dd \tilde{\pi}_{2*}\tilde{\eta})(y) &= (\Dd \tilde{p}_{3*}\widetilde{\Phi^*\eta})(u) \\
 &= \frac{\partial }{\partial u}\Bigl(\int_{0}^\infty \bigl(f^+(u+r)+f^-(u-r)\bigr) \Diff{r}\Bigr) \Diff{u} \\
&= \Bigl(\int_0^\infty (f^+)'(u+r) + (f^-)'(u-r) \Diff{r}\Bigr)\Diff{u} \\
& = \Bigl(\int_u^\infty (f^+)'(z) \Diff{z} + \int_{-\infty}^u (f^-)'(z) \Diff{z}\Bigr) \Diff{u} \\
& = \bigl(f^-(u)- f^+(u)\bigr) \Diff{u}
\end{align*}
This is zero precisely when $f$ extends continuously to $\R \times \R$.

A general form on $\R\times \R\backslash \Diag$ is
$$ \eta(x,y) = f_0(x,y) + f_1(x,y) \Diff{x} + f_2(x,y) \Diff{y} + f_3(x,y) \Diff{x}\Diff{y}. $$
Imposing the condition $\Dd_y\eta= 0$, we get \eqref{Eq:PartialConst} for both $f_0$ and $f_1$.
Imposing $\tau^* \eta = - \eta$, we get $f_3(x,y) = f_3(y,x)$, $f_2(x,y) = - f_1(y,x)$ and $f_0(x,y) = - f_0(y,x)$.
Now, requiring that $\eta$ has compact vertical support in $x$ implies $f_0 = f_1 \equiv 0$ because of \eqref{Eq:PartialConst}, and hence also $f_2 \equiv 0$.
However, the form $\eta = f_3(x,y) \Diff{x} \Diff{y}$ satisfies $\Dd_y \eta = 0$ and $\Dd\tilde{\pi}_{2*}\eta = 0$ trivially.

As for the higher dimensions, let $n\ge 1$, and consider
\begin{equation*}
\eta(x,y) := \begin{multlined}[t]\frac{\lambda(x)}{\Abs{x-y}^n} \sum_{i=1}^n (-1)^{i+1}(x^i - y^i) \Diff{x}^1\dotsb\Diff{x}^n\Diff{y}^1\dotsb \widehat{\Diff{y}^i}\dotsb\Diff{y}^n\end{multlined}
\end{equation*}
for $(x,y)\in \R^n\times\R^n\backslash\Diag$, where $\lambda: \R^n \rightarrow \R$ is a smooth bump function. Then $\eta$ extends smoothly to $\Bl_\Diag(\R^n\times \R^n)$, it holds $\Dd_y \eta(x,y) = 0$, but
$$ \Dd \int_x \eta(x,y) = \Vol(\Sph{n}) \lambda(y) \Diff{y}^1 \dotsb \Diff{y}^n. $$
This finishes the proof.
\end{proof}
\end{document}