\section{Spline Fitting Method}
\label{Spline_1}

In this paper there are 2 methods of fusing IMU data and Monocular camera. First of which is spline fitting using scale factor with the data of IMU and Monocular camera depending on the reliability of the data from both sources to estimate distance. So here we did simulation of whole system with arbitrary value of data considering linear moment of robot with noisy data. Matlab simulations can be seen in the result section. Initially, theory behind curve fitting is explained to have an algebraic background of the types of curve fitting and their applications. We use spline in curve fitting because we get discrete data from sensors and camera but tracing spline helps for interpolation and extrapolation to have continuous high probability data of path.

Spline is fitted on the data points which is output of sensor in our case. Spline fitting also helps in reducing error due to noise. For improving accuracy we can increase number of spline but this will also result in an increase in computation. So we have to trade off between accuracy and computation.
Spline fitting is useful to give smooth curve. N degree spline may have N-1 curves in its shape. So if spline has $X^3$ term that means it can have 2 curves in its spline. Ideal method to cover n points in a plain is by tracing $n^{th}$ order spline which will result into zero error. So spline will pass through all the points and will have a general equation as

\begin{equation}
\ A_{0} +A_{1}X +A_{2}X^{2}+A_{3}X^{3} + ... + A_{n}X^{n} = Y
\end{equation}

This problem then converts in simple Ax = B form. we can get values of n terms by plugging in n points values.

Where

\begin{equation}
A =
\begin{pmatrix}
  1 & X_{1} & X_{1}^{2} & \cdots & X_{1}^{n} \\
  1 & X_{2} & X_{2}^{2} & \cdots & X_{2}^{n} \\
  1 & X_{3} & X_{3}^{2} & \cdots & X_{3}^{n} \\
  : & :     &     :     & \cdots &    :      \\
  1 & X_{n} & X_{n}^{2} & \cdots & X_{n}^{n} \\
 \end{pmatrix}
B =
\begin{pmatrix}
  A_{0} \\
  A_{1} \\
  A_{2} \\
  : 	\\
  A_{n} \\
 \end{pmatrix}
 C =
\begin{pmatrix}
  Y_{0} \\
  Y_{1} \\
  Y_{2} \\
  : 	\\
  Y_{n} \\
 \end{pmatrix}
\end{equation}

But for practical applications we use other methods and do not plot n degree spline as it is computationally expensive .Secondly if there is more noise in system it will give wrong results.
For practical application there are 3 kinds of spline fitting according to the application:-

\subsection{Types of curve fitting}
\subsubsection{Maximum error}
This is the method where spline is fit in a way where the point with maximum error is reduced and curve moves towards the outlier point. If there exist in order to reduce maximum error of a point. This on the other hand results into

\begin{figure}[!htb]
\includegraphics[width=\textwidth]{./figures/Maxerror.PNG}
\caption{Curve Fitting with Maximum error }
\end{figure}

\begin{equation}
E(f)=|max (f(xi)-yi)|  where\ i\ goes\ from\ 1\ to\ n
\end{equation}

Due to one outlier here, the line is shifted in order to reduce maximum error. Although not a reliable process, it is easy to compute.

\subsubsection{Average error}
This method is used to minimize average and fit the spline with minimum error conditions.

\begin{figure}[!htb]
\includegraphics[width=\textwidth]{./figures/average.PNG}
\caption{Curve Fitting with Average error }
\end{figure}

\begin{equation}
E(f)=\frac{1}{n}*\Sigma|f(x_k)-Y_k|
\end{equation}

Actually due to one outlier here line shifted in order to reduce maximum error. So this is not reliable process but is easy to compute.

\subsubsection{RMS (Least square method)}
This is the most efficient method to minimize the error called as least square method.
Hence this is mostly used in curve fitting.
We have also created an algorithm with the help of same type of curve fitting.

\begin{figure}[!htb]
\includegraphics[width=\textwidth]{./figures/RMS.PNG}
\caption{Curve Fitting with RMS error }
\end{figure}

\begin{equation}
E(f)={\lbrace\frac{1}{n}*\Sigma|f(x_k)-Y_k|\rbrace}^\frac{1}{2}
\end{equation}

\subsection{IMU Reading}
As we don’t have real IMU data we simulate virtual readings for this. We take X, Y, Z as a function on time

\begin{equation}
\label{For_x}
X_1=A_x t^2+B_x t +C_x
\end{equation}

\begin{equation}
Y_1=A_y t^2+B_y t +C_y
\end{equation}

\begin{equation}
Z_1=A_z t^2+B_z t +C_z
\end{equation}

With this treatment we will add some random noise to all the terms so that the data can be similar to what we obtain from a real IMU.

\begin{equation}
noise=A*(rand(1,n)-0.5)
\end{equation}

So our noise will range from -A/2 to A/2.
As function is rand() so it will change for every point.

\begin{equation}
X=X_1+noise
\end{equation}

\begin{equation}
Y=Y_1+noise
\end{equation}

\begin{equation}
Z=Z_1+noise
\end{equation}
We also took some random points of camera pose. And provided our system with data set.\\

Proof of Least square method in our case:
Let’s consider optimization of X first. From equation \eqref{For_x} we have

Here $A$, $B$, $C$ are known
We have data set of X with respect to t.
To calculate error in RMS we have

\begin{equation}
 Error_x={\lbrace\displaystyle\sum_{i=1}^{n}(|X_i-(A_xt_i^2+B_x t_i +C_x)|)^2\rbrace}^\frac{1}{2}
\end{equation}

When we differentiate some quantity and equate it to zero it goes to either minima or maxima
And if double derivative is positive it goes to minima.

So in this case if we take partial derivatives of error with respect to Ax , Bx , Cx

\begin{equation}
 \frac{\partial Error_x}{\partial A_x} = {\lbrace\displaystyle\sum_{i=1}^{n}2(|X_i-(A_xt_i^2+B_x t_i +C_x)|)\rbrace}({-t_i^2})
\end{equation}

\begin{equation}
\displaystyle\sum_{i=1}^{n}(|{-t_i^2}X_i+(A_x t_i^4+B_x t_i^3+C_x t_i^2)|) = 0
\end{equation}

\begin{equation}
 \frac{\partial Error_x}{\partial B_x} = {\lbrace\displaystyle\sum_{i=1}^{n}2(|X_i-(A_xt_i^2+B_x t_i +C_x)|)\rbrace}({-t_i})
\end{equation}

\begin{equation}
\displaystyle\sum_{i=1}^{n}(|{-t_i}X_i+(A_x t_i^3+B_x t_i^2+C_x t_i^1)|) = 0
\end{equation}

\begin{equation}
 \frac{\partial Error_x}{\partial C_x} = {\lbrace\displaystyle\sum_{i=1}^{n}2(|X_i-(A_xt_i^2+B_x t_i +C_x)|)\rbrace}({-1})
\end{equation}

\begin{equation}
\displaystyle\sum_{i=1}^{n}(|-X_i+(A_x t_i^2+B_x t_i+C_x)|) = 0
\end{equation}

So we can compute matrix in such a way that the problem changes to

$Ax=B$

Where,

\begin{equation}
A =
\begin{pmatrix}
  \Sigma t^4 & \Sigma t^3 & \Sigma t^2\\
  \Sigma t^3 & \Sigma t^2 & \Sigma t\\
  \Sigma t^2 & \Sigma t & \Sigma 1\\
 \end{pmatrix}
B =
\begin{pmatrix}
  A_x \\
  B_x \\
  C_x \\
 \end{pmatrix}
 C =
\begin{pmatrix}
  \Sigma X_i t ^2 \\
  \Sigma X_i t \\
  \Sigma X_i \\
 \end{pmatrix}
\end{equation}

From this equation we can calculate X matrix.Similarly,We can get X matrix of Y and Z axis.

\begin{equation}
A =
\begin{pmatrix}
  \Sigma t^4 & \Sigma t^3 & \Sigma t^2\\
  \Sigma t^3 & \Sigma t^2 & \Sigma t\\
  \Sigma t^2 & \Sigma t & \Sigma 1\\
 \end{pmatrix}
B =
\begin{pmatrix}
  A_y \\
  B_y \\
  C_y \\
 \end{pmatrix}
 C =
\begin{pmatrix}
  \Sigma Y_i t ^2 \\
  \Sigma Y_i t \\
  \Sigma Y_i \\
 \end{pmatrix}
\end{equation}

\begin{equation}
A =
\begin{pmatrix}
  \Sigma t^4 & \Sigma t^3 & \Sigma t^2\\
  \Sigma t^3 & \Sigma t^2 & \Sigma t\\
  \Sigma t^2 & \Sigma t & \Sigma 1\\
 \end{pmatrix}
B =
\begin{pmatrix}
  A_z \\
  B_z \\
  C_z \\
 \end{pmatrix}
 C =
\begin{pmatrix}
  \Sigma Z_i t ^2 \\
  \Sigma Z_i t \\
  \Sigma Z_i \\
 \end{pmatrix}
\end{equation}

By this treatment we converted given points into equation with least square method here we used condition of

\begin{equation}
min
\begin{pmatrix}
  A_x t^2 + B_x t + C_x -\lambda_i X_c \\
  A_y t^2 + B_y t + C_y -\lambda_i Y_c \\
  A_z t^2 + B_z t + C_z -\lambda_i Z_c \\
 \end{pmatrix}^2
 \end{equation}

 Here in this formula we have different value for different spline according to accuracy of camera data and IMU reading.

\subsection{Results}

 In our case we simulated the results and found following outputs.
Plotting the second order curve for given data of IMU with just least square method. We get the following graph~\ref{imures1}.

\begin{figure}[!htb]
\includegraphics[width=\textwidth,height=7cm,keepaspectratio]{./figures/lsm.jpg}
\caption{In this case Blue is actual curve traced by IMU this are points with noise.And green is plotted curve. With least square method.}
\label{imures1}
\end{figure}

Then according to the paper we fused data of camera and IMU so we get following graph~\ref{fig:multipleSpline}.

\begin{figure}[!htb]
\includegraphics[width=\textwidth,height=7cm,keepaspectratio]{./figures/AllOutput.jpg}
\caption{Multiple spline fit}
\label{fig:multipleSpline}
\end{figure}

In Figure~\ref{fig:multipleSpline} we have 3 curves fitted as shown as red which gives better results and it also uses data of camera shown with black dots. So here according to the above formula scale factor of different section is different it is according to the quality of reading by camera.
For example, in our case
\begin{equation}
\lambda_1=0.5 ,\lambda_2=0.5 ,\lambda_3=0.1
\end{equation}

In 3rd section camera readings were not accurate so taken into smaller scalar value. This camera data further improved the result. And we get minimum error and good fit. With the given spline fitting method.

So if we look into different error methods with traditional Least square method with Jung and Taylor method, it gives us good results with output similar to the research paper.

\begin{figure}[!htb]
\includegraphics[width=\textwidth,height=7cm,keepaspectratio]{./figures/ErrorC.jpg}
\caption{\textbf{a}. Red curve for method with one curve and least square method.
\textbf{b}. Green curve is using the given method by Jung and Taylor.}
\label{fig:errorc1}
\end{figure}

The graph in Figure 12 has error comparison with and without Jung and Taylor method of split curve fitting and scaling camera input.
So it is better to follow the given first method in the paper. It results into considerable reduction in error which is clearly seen in graph.

With the same approach we can optimize and plot spline in 3 dimension so when we use this algorithm for 3D space we get the following result where we get an second order optimized curve. We also included considerable noise and tested our algorithm.

\begin{figure}[H]
\includegraphics[width=\textwidth,height=7cm,keepaspectratio]{./figures/3DCurve.jpg}
\caption{Actual 3D path with noise vs Optimized spline fit}
\label{fig:errorc1}
\end{figure}


\chapter{Discussion}

\section{Complexity and optimality}
\label{sec:disc:complexity}

Generic join guarantees worst-case optimality with respect to the output size.
Our relational \ematching preserves this optimality. In particular, we have the following theorem:
\newtheorem{theorem}{Theorem}
\begin{theorem}
Relational \ematching is worst-case optimal; that is, fix a pattern $p$, let $M(p,E)$ be the set of substitutions yielded by \ematching on an \egraph $E$ with $n$ nodes, relational \ematching runs in time $\tilde O(\max_E(|M(p,E)|))$.
\end{theorem}
\begin{proof}

Notice that there is an one-to-one correspondence between an output atoms of the generated conjunctive query and the \ematching. Therefore, the worst-case bound is the same across an \ematching pattern and the conjunctive query it generated. Because generic join is worst-case optimal, relational \ematching also runs in worst-case optimal time with respect to the output size.
\end{proof}

\section{Other join algorithms}

Although we choose generic join algorithm in relational \ematching, there are also other choices in the design space. For example, traditional two-way join plans are efficient on acyclic queries, and extensive research has been done on synthesizing highly efficient query plans. Moreover, Yannakakis' algorithm \citep{yannakakis} is an optimal algorithm on ayclic queries, running in time equal to the size of the output (with possible log factors). However, both of the queries suffer on cyclic conjunctive queries. Two-way join plans spend time enumerating unsatisfying terms, while Yannakakis' algorithm are not applicable to cyclic conjunctive queries. It is possible to choose different join algorithms based on the cyclicity of the query to take advantages each algorithms. However, we currently does not implement this.

\section{Comparison to graph pattern matching}

The idea of representing graph data structure as relational databases are not new. For example, many Datalog programs represent nodes and edges as their own relations to use relational joins to support efficient queries on graph database, known as graph pattern matching. Compared to the common encoding of graphs, our relational representation of \egraphs in a relational database is slightly different and specialized for \egraphs. It is a future work to absorb researches on graph pattern matching for relational \ematching.


% !TEX root = scombinatorics.tex
\documentclass[scombinatorics.tex]{subfiles}
\begin{document}
\chapter{Set systems}
\label{sperner}

\def\medrel#1{\parbox[t]{6ex}{$\displaystyle\hfil #1$}}
\def\ceq#1#2#3{\parbox[t]{40ex}{$\displaystyle #1$}\medrel{#2}{$\displaystyle #3$}}

\def\separatore{\hfil o \rule[0.5ex]{4ex}{0.1ex} o \rule[0.5ex]{4ex}{0.1ex} o}

%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%
\section{Sperner's Theorem}\label{sperner}

We say that $\phi(A\,;b)_{b\in\V}$ is an \emph{antichain\/} if there is no pair of distinct elements $b,b'\in\V$ such that $\phi(A\,;b)\subset\phi(A\,;b')$.
Antichains are also called \emph{Sperner systems.}

If all sets in $\phi(A\,;b)_{b\in\V}$ are distinct and of equal cardinality, then we clearly have an antichain.
If $|A|=n$, the cardinality of a collection of subsets of $A$, all of cardinality $k$, is maximal when $k=\lfloor n/2\rfloor$ or $k=\lceil n/2\rceil$.
In this case\smallskip

\ceq{\hfill\big|\phi(A\,;b)_{b\in\V}\big|}
{=}
{{n\choose\lfloor n/2\rfloor}}

\ceq{}
{=}
{{n\choose\lceil n/2\rceil}.}\smallskip

By the following classical theorem, this bound holds for all antichain.
This is one of the first results of external combinatorics (though the term has been coined a few years later).

\def\ceq#1#2#3{\parbox[t]{36ex}{$\displaystyle #1$}\medrel{#2}{$\displaystyle #3$}}

\begin{void_thm}[Sperner's Theorem]
  Let $A\subseteq\U$ have cardinality $n$, finite.
  If $\phi(A\,;b)_{b\in\V}$ is an antichain then

  \ceq{\hfill\big|\phi(A\,;b)_{b\in\V}\big|}
  {\le}
  {{n\choose\lfloor n/2\rfloor}.}
\end{void_thm}
\smallskip
\begin{proof}
   Clearly, $\phi(A\,;b)_{b\in\V}$ is the disjoint union of the sets \smash{$\displaystyle{A\choose k}\cap\phi(A\,;b)_{b\in\V}$} for $k$ ranging over $\{0,\dots,n\}$.
   Then

   \ceq{\hfill\big|\phi(A\,;b)_{b\in\V}\big|}{\le}{\sum^n_{k=0}\bigg|{A\choose k}\cap\phi(A\,;b)_{b\in\V}\bigg|.}

   As for every $k\le n$

   \ceq{\hfill{n\choose k}}{\le}{{n\choose\lfloor n/2\rfloor},}

   the theorem follows immediately from the LYM inequality that we prove below.
\end{proof}

The acronym LYM stands for Lubell-Yamamoto-Meshalkin.

\begin{lemma}[(LYM inequality)]
   Let $A\subseteq\U$ have cardinality $n$, finite.
   If $\phi(A\,;b)_{b\in\V}$ is an antichain then

   % \ceq{\hfill\sum^n_{k=0}\frac{\big|\phi(A\,;b)_{b\in\V}\ \cap\  A^{(k)}\big|}{\big|A^{(k)}\big|}}{\le}{1.}

   \ceq{\hfill\sum^n_{k=0}\bigg|{A\choose k}\cap\phi(A\,;b)_{b\in\V}\bigg|\cdot{n\choose k}^{\kern-.8ex -1}\kern-.8ex}{\le}{1.}
\end{lemma}
\smallskip
\begin{proof}
   Let $\Pi$ be uniform random variable that ranges over the set of permutations of $A=\{a_1,\dots,a_n\}$.
   For any $\phi(A\,;b)$ of cardinality $k$

   \ceq{\hfill\Pr\bigg(\Pi\{a_1,\dots,a_k\}=\phi(A\,;b)\bigg)\kern-.8ex}{=}{ {n\choose k}^{\kern-.8ex -1}\kern-1.5ex.}

   The events above are disjoint for distinct sets $\phi(A\,;b)$, hence

   \ceq{\hfill\Pr\bigg(\Pi\{a_1,\dots,a_k\}\in\phi(A\,;b)_{b\in\V} \bigg)\kern-.8ex}{=}{\bigg|{A\choose k}\cap\phi(A\,;b)_{b\in\V}\bigg|\cdot{n\choose k}^{\kern-.8ex -1}\kern-1.5ex. }

   As $\phi(A\,;b)_{b\in\V}$ is an antichain, for distinct $k$ the events above are disjoint, hence

   \ceq{\hfill\Pr\bigg(\bigcup^n_{k=0}\Pi\{a_1,\dots,a_k\}\in\phi(A\,;b)_{b\in\V} \bigg)\kern-.8ex}{=}{\sum^n_{k=0}\bigg|{A\choose k}\cap\phi(A\,;b)_{b\in\V}\bigg|\cdot{n\choose k}^{\kern-.8ex-1}\kern-1.5ex. }

   Now, the inequality is evident.
\end{proof}

Let $\Pr_k$ be the probability measure on the subsets of $A$ that is concentrated and uniform on $A^{(k)}$.
Namely, for $A'\subseteq A$

\ceq{\hfill{\Pr}_k\big(\{A'\}\big)}
{=}
{\left\{
\begin{array}{ll}
   \kern1.5ex0&\textrm{if}\kern1.5ex |A'|\neq k\\
   \displaystyle{n\choose k}^{\kern-.8ex-1} &\textrm{if}\kern1.5ex |A'|= k\\
\end{array}\right.}

Then the the LYM inequality asserts that if $\phi(A\,;b)_{b\in\V}$ is an antichain then

\ceq{\hfill\sum^n_{k=0}{\Pr}_k\big(\phi(A\,;b)_{b\in\V}\big)}{\le}{1.}

This inequality is strict when $\phi(A\,;b)_{b\in\V}=A^{(k)}$ for some $k$.
In the next section we show that these are the only cases.

%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%
\section{The Erd\H{o}s-Ko-Rado Theorem}\label{ErdosKoRado}

\def\medrel#1{\parbox[t]{4ex}{$\displaystyle\hfil #1$}}
\def\ceq#1#2#3{\parbox[t]{20ex}{$\displaystyle #1$}\medrel{#2}{$\displaystyle #3$}}

\begin{lemma}[(Peter J.~Cameron)]
   Let $G$ be a $1$-transitive finite graph.
   If $G$ contains a clique of cardinality $m$, then every subgraph $H\subseteq G$ contains a clique of cardinality

   \ceq{}{\ge}{m\frac{|H|}{|G|}.}
\end{lemma}

\begin{proof}
   Let $C$ be a clique in $G$ of cardinality $m$.
   Let $k$ the cardinality of the largest clique in $H$.
   Let $n=|\Aut(G)|$.
   By $1$-transitivity, the sets $\{f\in\Aut(G): fa=b\}$, for any fixed $a\in G$ and $b$ ranging over $G$, have all the same cardinality.
   Hence, for any given pair $\<a,b\>$, they have cardinality $n/|G|$.

   Count the pairs $\<a,f\>\in C\times\Aut(G)$ such that $fa\in H$.
   For every $a\in C$ there are $n\cdot|H|$ automorphisms.
   So the number of pairs is $m\cdot n\cdot|H|/|G|$

   On the other hand for each $f\in\Aut(G)$ there are at most $k$ choices of $a\in C$.
   So $m\cdot n\cdot|H|/|G|\le k\,n$.
\end{proof}

\begin{void_thm}[Erd\H{o}s-Ko-Rado Theorem]
   Let $A\subseteq\U$ be a finite set of cardinality $n$.
   Let $k\le n/2$.
   Let $\phi(A\,;b)_{b\in\V}$ be an intersecting family of sets of cardinality $k$.
   Then

   \ceq{\hfill\Big|\phi(A\,;b)_{b\in\V}\Big|}{\le}{{n-1\choose k-1}.}
\end{void_thm}

\begin{proof}
   Let $m=\Big|\phi(A\,;b)_{b\in\V}\Big|$.
   Consider the graph

   \ceq{\hfill G}{=}{{A\choose k},}

   \ceq{\hfill E(G)}{=}{\Big\{\{A',A''\}\ :\  A'\cap A''\neq\0\Big\}.}

   Enumerate the elements of $A$, say $A=\{a_0,\dots,a_{n-1}\}$.
   Consider the following subgraph of $G$

   \ceq{\hfill H}{=}{\Big\{\{a_i,\dots,a_{i+k-1}\}\ :\  0\le i<n\Big\},}

   where the indices are intended modulo $n$.
   As $k\le n$, the largest clique in $H$ has cardinality $k$.
   As $\phi(A'\,;b)_{b\in\V}$ is a clique of $G$, by the lemma above,

   \ceq{\hfill k}{\ge}{m\frac{|H|}{|G|}}\medrel{=}$\displaystyle m\cdot n\cdot {n\choose k}^{\!-1}$

   therefore

   \ceq{\hfill m}{\le}{{n-1\choose k-1}}
\end{proof}
\end{document}


\newpage
\section{Matrix Operations}

\begin{Operator}[cofactor]{COFACTOR}
\index{matrix}
The operator \name{cofactor} returns the cofactor of the element in row
\meta{row} and column \meta{column} of a \nameref{matrix}.  Errors occur
if \meta{row} or \meta{column} do not evaluate to integer expressions or if
the matrix is not square.

\begin{Syntax}
\name{cofactor}\(\meta{matrix\_expression},\meta{row},\meta{column}\)
% COFACTOR(EXPRN:matrix_expression,ROW:integer,COLUMN:integer):algebraic
\end{Syntax}

\begin{Examples}
cofactor(mat((a,b,c),(d,e,f),(p,q,r)),2,2); & A*R - C*P \\
cofactor(mat((a,b,c),(d,e,f)),1,1); &  ***** non-square matrix
\end{Examples}
\end{Operator}

\begin{Operator}[det]{DET}
\index{matrix}\index{determinant}
The \name{det} operator returns the determinant of its
(square \nameref{matrix}) argument.

\begin{Syntax}
\name{det}\(\meta{expression}\) or \name{det} \meta{expression}
\end{Syntax}

\meta{expression} must evaluate to a square matrix.

\begin{Examples}

matrix m,n; \\

m := mat((a,b),(c,d));      &  \begin{multilineoutput}{4cm}
M(1,1) := A
M(1,2) := B
M(2,1) := C
M(2,2) := D
                               \end{multilineoutput}\\
det m;                      &      A*D - B*C \\
n := mat((1,2),(1,2));      &  \begin{multilineoutput}{4cm}
N(1,1) := 1
N(1,2) := 2
N(2,1) := 1
N(2,2) := 2
                               \end{multilineoutput} \\

det(n);                     &      0 \\

det(5);                     &      5
\end{Examples}

\begin{Comments}
Given a numerical argument, \name{det} returns the number.  However, given a
variable name that has not been declared of type matrix, or a non-square
matrix, \name{det} returns an error message.
\end{Comments}
\end{Operator}

\begin{Operator}[mat]{MAT}
\index{matrix}
The \name{mat} operator is used to represent a two-dimensional
\nameref{matrix}.
\begin{Syntax}
\name{mat}\(\(\meta{expr}\{,\meta{expr}\}\optional\)%
            \{\(\meta{expr}\{\name{,}\meta{expr}\}\optional\)\}\optional\)
\end{Syntax}

\meta{expr} may be any valid REDUCE scalar expression.

\begin{Examples}
mat((1,2),(3,4));            &
\begin{multilineoutput}{6cm}
MAT(1,1) := 1
MAT(2,3) := 2
MAT(2,1) := 3
MAT(2,2) := 4
\end{multilineoutput}\\
mat(2,1);                    & \begin{multilineoutput}{6cm}
***** Matrix mismatch
Cont? (Y or N)
\end{multilineoutput}\\
matrix qt;  \\
qt := ws;                    & \begin{multilineoutput}{6cm}
QT(1,1) := 1
QT(1,2) := 2
QT(2,1) := 3
QT(2,2) := 4
\end{multilineoutput}\\
matrix a,b; \\
a := mat((x),(y),(z));       & \begin{multilineoutput}{6cm}
A(1,1) := X
A(2,1) := Y
A(3,1) := Z
\end{multilineoutput}\\
b := mat((sin x,cos x,1));   & \begin{multilineoutput}{6cm}
B(1,1) := SIN(X)
B(1,2) := COS(X)
B(1,3) := 1
\end{multilineoutput}
\end{Examples}

\begin{Comments}
Matrices need not have a size declared (unlike arrays).  \name{mat}
redimensions a matrix variable as needed.  It is necessary, of course,
that all rows be the same length.  An anonymous matrix, as shown in the
first example, must be named before it can be referenced (note error
message).  When using \name{mat} to fill a \IFTEX{$1 \times n$}{1 x n}
matrix, the row of values must be inside a second set of parentheses, to
eliminate ambiguity.
\end{Comments}
\end{Operator}

\begin{Operator}[mateigen]{MATEIGEN}
\index{matrix}\index{eigenvalue}
The \name{mateigen} operator calculates the eigenvalue equation and the
corresponding eigenvectors of a \nameref{matrix}.
\begin{Syntax}
\name{mateigen}\(\meta{matrix-id},\meta{tag-id}\)
\end{Syntax}

\meta{matrix-id} must be a declared matrix of values, and \meta{tag-id} must be
a legal REDUCE identifier.

\begin{Examples}
aa := mat((2,5),(1,0))\$ \\
mateigen(aa,alpha);          &
\begin{multilineoutput}{2cm}
\{\{ALPHA^{2} - 2*ALPHA - 5,
  1,
  MAT(1,1) := \rfrac{5*ARBCOMPLEX(1)}{ALPHA - 2},

  MAT(2,1) := ARBCOMPLEX(1)
  \}\}
\end{multilineoutput}\\
charpoly := first first ws;  &       CHARPOLY := ALPHA^{2} - 2*ALPHA - 5 \\
bb := mat((1,0,1),(1,1,0),(0,0,1))\$ \\
mateigen(bb,lamb);          &
\begin{multilineoutput}{6cm}
\{\{LAMB - 1,3,
  [      0      ]
  [ARBCOMPLEX(2)]
  [      0      ]
  \}\}
\end{multilineoutput}
\end{Examples}

\begin{Comments}
The \name{mateigen} operator returns a list of lists of three
elements.  The first element is a square free factor of the characteristic
polynomial; the second element is its multiplicity; and the third element
is the corresponding eigenvector.  If the characteristic polynomial can be
completely factored, the product of the first elements of all the sublists
will produce the minimal polynomial.  You can access the various parts of
the answer with the usual list access operators.

If the matrix is degenerate, more than one eigenvector can be produced for
the same eigenvalue, as shown by more than one arbitrary variable in the
eigenvector.  The identification numbers of the arbitrary complex variables
shown in the examples above may not be the same as yours.  Note that since
\name{lambda} is a reserved word in REDUCE, you cannot use it as a {\it
tag-id} for this operator.
\end{Comments}
\end{Operator}

\begin{Declaration}[matrix]{MATRIX}
Identifiers are declared to be of type \name{matrix}.
\begin{Syntax}
\name{matrix} \meta{identifier} \&option \(\meta{index},\meta{index}\)\\
          \{,\meta{identifier} \&option
          \(\meta{index},\meta{index}\)\}\optional
\end{Syntax}

\meta{identifier} must not be an already-defined operator or array or
the name of a scalar variable.  Dimensions are optional, and if used appear
inside parentheses.  \meta{index} must be a positive integer.

\begin{Examples}
matrix a,b(1,4),c(4,4); \\
b(1,1);                      &          0 \\
a(1,1);                      &          ***** Matrix A not set \\
a := mat((x0,y0),(x1,y1));   & \begin{multilineoutput}{6cm}
A(1,1) := X0
A(1,2) := Y0
A(2,1) := X0
A(2,2) := X1
\end{multilineoutput}\\
length a;                    &          \{2,2\} \\
b := a**2;                   & \begin{multilineoutput}{6cm}
B(1,1) := X0^{2} + X1*Y0
B(1,2) := Y0*(X0 + Y1)
B(2,1) := X1*(X0 + Y1)
B(2,2) := X1*Y0 + Y1^{2}
\end{multilineoutput}
\end{Examples}

\begin{Comments}
When a matrix variable has not been dimensioned, matrix elements cannot be
referenced until the matrix is set by the \nameref{mat} operator.  When a
matrix is dimensioned in its declaration, matrix elements are set to 0.
Matrix elements cannot stand for themselves.  When you use \nameref{let} on
a matrix element, there is no effect unless the element contains a
constant, in which case an error message is returned.  The same behavior
occurs with \nameref{clear}.  Do \meta{not} use \nameref{clear} to try to
set a matrix element to 0. \nameref{let} statements can be applied to
matrices as a whole, if the right-hand side of the expression is a matrix
expression, and the left-hand side identifier has been declared to be a matrix.

Arithmetical operators apply to matrices of the correct dimensions.  The
operators \name{+} and \name{-} can be used with matrices of the same
dimensions.  The operator \name{*} can be used to multiply
\IFTEX{$m \times n$}{m x n} matrices by \IFTEX{$n \times p$}{n x p}
matrices.  Matrix multiplication is non-commutative.  Scalars can also be
multiplied with matrices, with the result that each element of the matrix
is multiplied by the scalar.  The operator \name{/} applied to two
matrices computes the first matrix multiplied by the inverse of the
second, if the inverse exists, and produces an error message otherwise.
Matrices can be divided by scalars, which results in dividing each element
of the matrix.  Scalars can also be divided by matrices when the matrices
are invertible, and the result is the multiplication of the scalar by the
inverse of the matrix.  Matrix inverses can by found by \name{1/A} or
\name{/A}, where \name{A} is a matrix.  Square matrices can be raised to
positive integer powers, and also to negative integer powers if they are
nonsingular.

When a matrix variable is assigned to the results of a calculation, the
matrix is redimensioned if necessary.
\end{Comments}
\end{Declaration}

\begin{Operator}[nullspace]{NULLSPACE}
\index{matrix}
\begin{Syntax}
\name{nullspace}(\meta{matrix\_expression})
\end{Syntax}

\meta{nullspace} calculates for its \nameref{matrix} argument,
\name{a}, a list of
linear independent vectors (a basis) whose linear combinations satisfy the
equation $a x = 0$.  The basis is provided in a form such that as many
upper components as possible are isolated.

\begin{Examples}
nullspace mat((1,2,3,4),(5,6,7,8)); &
\begin{multilineoutput}{6cm}
       \{
         [ 1  ]
         [    ]
         [ 0  ]
         [    ]
         [ - 3]
         [    ]
         [ 2  ]
         ,
         [ 0  ]
         [    ]
         [ 1  ]
         [    ]
         [ - 2]
         [    ]
         [ 1  ]
        \}
\end{multilineoutput}
\end{Examples}

\begin{Comments}
Note that with \name{b := nullspace a}, the expression \name{length b} is
the {\em nullity\/} of A, and that \name{second length a - length b}
calculates the {\em rank\/} of A.  The rank of a matrix expression can
also be found more directly by the \nameref{rank} operator.

In addition to the REDUCE matrix form, \name{nullspace} accepts as input a
matrix given as a \nameref{list} of lists, that is interpreted as a row matrix.  If
that form of input is chosen, the vectors in the result will be
represented by lists as well.  This additional input syntax facilitates
the use of \name{nullspace} in applications different from classical linear
algebra.
\end{Comments}

\end{Operator}

\begin{Operator}[rank]{RANK}
\index{matrix}
\begin{Syntax}
\name{rank}(\meta{matrix\_expression})
\end{Syntax}
\name{rank} calculates the rank of its matrix argument.

\begin{Examples}
rank mat((a,b,c),(d,e,f));  & 2
\end{Examples}

\begin{Comments}
The argument to \name{rank} can also be a \nameref{list} of lists, interpreted
either as a row matrix or a set of equations.  If that form of input is
chosen, the vectors in the result will be represented by lists as well.
This additional input syntax facilitates the use of \name{rank} in
applications different from classical linear algebra.
\end{Comments}

\end{Operator}

\begin{Operator}[tp]{TP}
\index{transpose}\index{matrix}
The \name{tp} operator returns the transpose of its \nameref{matrix}
 argument.
\begin{Syntax}
\name{tp} \meta{identifier} or  \name{tp}\(\meta{identifier}\)
\end{Syntax}

\meta{identifier} must be a matrix, which either has had its dimensions set
in its declaration, or has had values put into it by \name{mat}.

\begin{Examples}
matrix m,n; \\
m := mat((1,2,3),(4,5,6))\$ \\
n := tp m;                   &
\begin{multilineoutput}{6cm}
N(1,1) := 1
N(1,2) := 4
N(2,1) := 2
N(2,2) := 5
N(3,1) := 3
N(3,2) := 6
\end{multilineoutput}
\end{Examples}
\begin{Comments}
In an assignment statement involving \name{tp}, the matrix identifier on the
left-hand side is redimensioned to the correct size for the transpose.
\end{Comments}
\end{Operator}

\begin{Operator}[trace]{TRACE}
\index{matrix}
The \name{trace} operator finds the trace of its \nameref{matrix} argument.
\begin{Syntax}
\name{trace}\(\meta{expression}\) or \name{trace} \meta{simple\_expression}
\end{Syntax}

\meta{expression} or \meta{simple\_expression} must evaluate to a square
matrix.

\begin{Examples}
matrix a; \\
a := mat((x1,y1),(x2,y2))\$ \\
trace a;                     &         X1 + Y2
\end{Examples}
\begin{Comments}
The trace is the sum of the entries along the diagonal of a square matrix.
Given a non-matrix expression, or a non-square matrix, \name{trace} returns
an error message.
\end{Comments}
\end{Operator}


\chapter{CT Fourier Transform}

Recall the complex exponential $e^{st}$ for $s\in\mathbb{C}$ is the Eigenfunction of CT LTI systems. If we can decompose an input into a (possibly infinite) sum of such signals, we can easily determine the output using the superposition principle. In this section we consider the decomposition when the input is aperiodic, called the CT \emph{Fourier Transform} (CTFT).

In contrast to the CT Fourier series, in this case the complex exponent of the Eigenfunction becomes $s = j\omega$ a continuous variable, and the decomposition is an uncountably infinite sum (integral). This gives the input-output relationship for a stable LTI system as
\[
x(t) = \frac{1}{2\pi}\int\limits_{-\infty}^{\infty} X(j\omega) \, e^{j \omega t}\; d\omega \;\longrightarrow\; y(t) = \frac{1}{2\pi}\int\limits_{-\infty}^{\infty} H(j\omega) X(j\omega) \, e^{j \omega t}\; d\omega
\]
where $H(j \omega)$ are the Eigenvalues, again called the \emph{frequency response}. We now turn to determining under what circumstances the decomposition exists and how to find the function $X(j\omega)$.

\textbf{Note:} The difference in notation between $X(\omega)$ and $X(j\omega)$ is superficial. They generally are the same function. The latter just emphasizes that $s \rightarrow j\omega$. For example
\[
H(j\omega) = \frac{1}{1+(j\omega)^2} = \frac{1}{1-\omega^2} = H(\omega)
\]
are the same function since $j^2 = -1$.

\section{Synthesis and Analysis Equation}

Consider the aperiodic signal
\[
x(t) = \begin{cases}
  p(t) & A < t < B\\
  0 & \text{else}
\end{cases}
\]
and it's periodic extension with fundamental frequency $\omega_0 = \frac{2\pi}{T_0}$
\[
x_p(t) = \sum\limits_{m = -\infty}^{\infty} x(t-mT_0)
\]
where $T_0 > B-A$.
For example:
\begin{center}
  \includegraphics[scale=1]{graphics/ctfs_derivation.pdf}
\end{center}
The CT Fourier series coefficients are
\begin{align*}
  a_k &= \frac{1}{T_0} \int\limits_{T_0} x_p(t) e^{-jk\omega_0 t}\; dt\\
  &= \frac{1}{T_0} \int\limits_{-\infty}^{\infty} x(t) e^{-jk\omega_0 t}\; dt \mbox{ since } x(t) = 0 \mbox{ outside the interval } (A,B)\\
\end{align*}
Define the \emph{CT Fourier Transform} of $x(t)$ as
\[
\boxed{X(\omega) = \int\limits_{-\infty}^{\infty} x(t) e^{-j\omega t}\; dt}
\]
so that
\[
a_k = \frac{1}{T_0} X(k\omega_0)
\]
are samples of $X(\omega)$ spaced at frequencies $\omega_0$. By the CT Fourier series synthesis equation
\[
x(t) = \sum\limits_{k = -\infty}^{\infty} \frac{1}{T_0} X(k\omega_0) e^{jk\omega_0 t}
\]
Now, let $T_0 \rightarrow \infty$ so that the periodic copies move toward $\infty$ and $x_p(t) \rightarrow x(t)$. At the same time the frequency sample spacing becomes infinitesimal and
\[
X(k\omega_0) e^{jk\omega_0 t} \rightarrow X(\omega) e^{j\omega t}\; d\omega
\]
To give the \emph{Inverse Fourier Transform}
\[
\boxed{x(t) = \frac{1}{2\pi} \int\limits_{-\infty}^{\infty} X(\omega)e^{j\omega t}\; d\omega}
\]
This gives the \emph{Fourier Transform Pair}:
\[
\underbrace{X(\omega) = \mathcal{F}\{x(t)\} = \int\limits_{-\infty}^{\infty} x(t) e^{-j\omega t}\; dt}_{\text{Forward Transform / Analysis Equation}}
\hspace{3em}
\underbrace{x(t) = \mathcal{F}^{-1}\{X(\omega)\} = \frac{1}{2\pi} \int\limits_{-\infty}^{\infty} X(\omega)e^{j\omega t}\; d\omega}_{\text{Inverse Transform / Synthesis Equation}}
\]
The forward transform decomposes $x(t)$ into an infinite number of complex sinusoids. The inverse transform synthesizes a signal as an infinite sum of the sinusoids. It is an example of an \emph{Integral Transform}. Note the signal $x(t)$ and $X(\omega)$ are the same signal, just represented in different \emph{domains}, the time-domain and frequency-domain respectively.

Similar to the CT Fourier series, the function $X(\omega)$ is called the \emph{spectrum} of the signal $x(t)$. The magnitude spectrum is the function $|X(\omega)|$ and the phase spectrum is the function $\angle X(\omega)$. It is common to plot the spectrum as the combination of the magnitude and phase spectrum.

\begin{example}
  Consider the signal $x(t) = \delta(t)$. The Fourier transform is
  \begin{align*}
    X(\omega) &= \int\limits_{-\infty}^{\infty} x(t) e^{-j\omega t}\; dt\\
    &= \int\limits_{-\infty}^{\infty} \delta(t) e^{-j\omega t}\; dt\\
    &= e^{-j\omega (0)} \mbox{ by the sifting property}\\
    &= 1
  \end{align*}
  $\blacksquare$
\end{example}
\begin{example}
Consider the signal $x(t) = e^{at}u(t)$ for $a\in \mathbb{R}$.  The Fourier transform is
  \begin{align*}
    X(\omega) &= \int\limits_{-\infty}^{\infty} x(t) e^{-j\omega t}\; dt\\
    &= \int\limits_{0}^{\infty}  e^{at} \, e^{-j\omega t}\; dt\\
    &= \int\limits_{0}^{\infty}  e^{(a-j\omega) t}\; dt\\
    &= \frac{1}{a-j\omega } e^{(a-j\omega) t} \Big|_{0}^{\infty}\\
    &= \frac{1}{a-j\omega } \left[ \lim_{T\rightarrow\infty} e^{(a-j\omega) T} - \underbrace{e^{(a-j\omega) (0)}}_{1}\right]
  \end{align*}
  This example raises the question, of when does the Fourier Transform exist? Note if $a < 0$ then the limit above converges to zero, otherwise the integral diverges. In the former case we say the Fourier transform exists, and in the latter that it does not. Thus
  \[
  X(\omega) = \frac{-1}{a-j\omega } = \frac{1}{j\omega-a} \mbox{ for } a < 0\;.
  \]
  Note when $a < 0$, $x(t)$ is an energy signal. A sufficient, but not necessary condition for the Fourier transform to exist is that the signal be an energy signal. For this example, let's examine the spectrum, noting
  \[
  |X(\omega)| = \frac{1}{(a^2 + \omega^2)^\frac{1}{2}} \hspace{2em}\mbox{and}\hspace{2em} \angle X(\omega) = -\arctan\left( \frac{\omega}{-a}\right)
  \]
  plotted below for $a = -1$.
  \begin{center}
\includegraphics[scale=0.6]{graphics/ctft_example1.png}
  \end{center}
  $\blacksquare$
\end{example}
\begin{example}
Consider the signal $x(t) = e^{j\omega_0 t}$ for $\omega_0\in \mathbb{R}$.  The Fourier transform is
  \begin{align*}
    X(\omega) &= \int\limits_{-\infty}^{\infty} x(t) e^{-j\omega t}\; dt\\
    &= \int\limits_{-\infty}^{\infty}  e^{j\omega_0 t} \, e^{-j\omega t}\; dt\\
    &= \int\limits_{-\infty}^{\infty}  e^{-j(\omega_0-\omega) t}\; dt
  \end{align*}
  For $\omega \neq \omega_0$ this integral evaluates to
  \begin{align*}
    X(\omega) &= \int\limits_{-\infty}^{\infty}  \cos((\omega-\omega_0) t)\; dt + j \int\limits_{-\infty}^{\infty}  \sin((\omega-\omega_0) t)\; dt\\
    &= 0
  \end{align*}
  since the average value of a sinusoid is zero. When $\omega = \omega_0$ this integral diverges
  \[
  \int\limits_{-\infty}^{\infty}  e^{-j(\omega_0-\omega) t}dt = \int\limits_{-\infty}^{\infty}  e^{-j(0) t} dt= \int\limits_{-\infty}^{\infty} dt = \infty
  \]
  What signal is zero everywhere, but infinite at one point (I am hand-waving a bit here)? The delta function
  \[
  X(\omega) = A\delta(\omega-\omega_0) \mbox{ for some constant } A.
  \]
  To find the constant we can use the inverse transform
  \begin{align*}
    x(t) &= \frac{1}{2\pi} \int\limits_{-\infty}^{\infty} X(\omega)e^{j\omega t}\; d\omega\\
    &= \frac{1}{2\pi} \int\limits_{-\infty}^{\infty}  A\delta(\omega-\omega_0) e^{j\omega t}\; d\omega\\
    &= \frac{1}{2\pi} A e^{j\omega_0 t}\\
    &= e^{j\omega_0 t}
  \end{align*}
  which implies $A = 2\pi$.\\
  $\blacksquare$
\end{example}

\begin{example}
  Consider the signal $x(t) = \cos(\omega_0 t)$ for $\omega_0\in \mathbb{R}$.  The Fourier transform can be found using the result in the previous example by noting
  \begin{align*}
    X(\omega) &= \int\limits_{-\infty}^{\infty} x(t) e^{-j\omega t}\; dt\\
    &= \int\limits_{-\infty}^{\infty}  \cos(\omega_0 t) \, e^{-j\omega t}\; dt\\
    &= \frac{1}{2}\int\limits_{-\infty}^{\infty}   e^{j\omega_0 t} \, e^{-j\omega t}\; dt + \frac{1}{2}\int\limits_{-\infty}^{\infty} e^{-j\omega_0 t} \, e^{-j\omega t}\; dt\\
    &= \frac{1}{2} 2\pi \delta(\omega-\omega_0) + \frac{1}{2} 2\pi \delta(\omega+\omega_0)\\
    &= \pi \delta(\omega-\omega_0) + \pi \delta(\omega+\omega_0)
  \end{align*}
  This example highlights that the cosine signal is composed of exactly two frequencies.\\
  $\blacksquare$
\end{example}

\begin{example}
  Consider the signal
  \[
  X(\omega) = \begin{cases}
    1 & |\omega| < \omega_0\\
    0 & \text{else}
  \end{cases}
  \]
  The Inverse Fourier transform is
  \begin{align*}
    x(t) & = \frac{1}{2\pi} \int\limits_{-\infty}^{\infty} X(\omega)e^{j\omega t}\; d\omega\\
    &= \frac{1}{2\pi} \int\limits_{-\omega_0}^{\omega_0} e^{j\omega t}\; d\omega\\
    &= \frac{1}{2\pi} \frac{1}{jt} \left[ e^{j\omega_0 t} - e^{-j\omega_0 t}\right]\\
    &= \frac{1}{\pi t} \left[ \frac{1}{2j}e^{j\omega_0 t} - \frac{1}{2j} e^{-j\omega_0 t}\right]\\
    &= \frac{1}{\pi t} \sin(\omega_0 t)\\
    &= \frac{\omega_0}{\pi} \frac{\sin(\omega_0 t)}{\omega_0 t}\\
    &= \frac{\omega_0}{\pi} \mbox{sinc}(\omega_0 t)
  \end{align*}
  where $\mbox{sinc}()$ is the (unnormalized) \emph{sinc function}.\\
  $\blacksquare$
\end{example}

\section{Existence of the CT Fourier Transform}

The example of the real exponential above showed that for the Fourier transform to exist, the Fourier (analysis) integral must exist. Similar to the Fourier series some mild conditions, called the Dirichlet conditions, are a sufficient prerequisite for the Fourier transform of a signal $x(t)$ to exist:
\begin{itemize}
\item $x(t)$ is absolutely integrable
  \[
  x(t) = \int\limits_{-\infty}^{\infty} |x(t)|\; dt < \infty
  \]
\item $x(t)$ has a finite number of minima and maxima over any finite interval
\item $x(t)$ has a finite number of finite-valued discontinuities over any finite interval
\end{itemize}

These conditions are not necessary however, and we can extend the Fourier transform to a broader class of signals, if we allow delta functions in the transform, as in the cosine example above.

\section{Properties of the CT Fourier Transform}

There are several useful properties of the CT Fourier Transform that, when combined with a table of transforms (see Table 4.2, page 329 of OW), allow us to take the Fourier transform of  wide array of signals, and one, the convolution property, that allows us to determine the output of a system in the frequency domain easily. We state these here without proof in rough order of usefulness. See the course text for detailed derivations.

We use the notation $x(t) \stackrel{\mathcal{F}}{\longleftrightarrow} X(\omega)$ to indicate the signals are related by a Fourier Transform pair.
\begin{itemize}
\item Linearity: if $x_1(t) \stackrel{\mathcal{F}}{\longleftrightarrow} X_1(\omega)$ and $x_2(t) \stackrel{\mathcal{F}}{\longleftrightarrow} X_2(\omega)$ then
  \[
  ax_1(t) + bx_2(t) \stackrel{\mathcal{F}}{\longleftrightarrow} aX_1(\omega) + bX_2(\omega)
  \]
\item Convolution: if $x_1(t) \stackrel{\mathcal{F}}{\longleftrightarrow} X_1(\omega)$ and $x_2(t) \stackrel{\mathcal{F}}{\longleftrightarrow} X_2(\omega)$ then
  \[
  x_1(t) * x_2(t) \stackrel{\mathcal{F}}{\longleftrightarrow} X_1(\omega)X_2(\omega)
  \]
  Note in particular if one signal is the system input and the other is the impulse response, the output is the product of the Fourier transforms of each, where the Fourier transform of $h(t)$ is $H(\omega)$, the Eigenvalue or frequency response.
\item Differentiation if $x(t) \stackrel{\mathcal{F}}{\longleftrightarrow} X(\omega)$ then
  \[
  \frac{dx}{dt}(t) \stackrel{\mathcal{F}}{\longleftrightarrow} j\omega X(\omega)
  \]
  This allows us to easily determine the Eigenvalues/Frequency Response from a stable differential equation.
\item Multiplication: if $x_1(t) \stackrel{\mathcal{F}}{\longleftrightarrow} X_1(\omega)$ and $x_2(t) \stackrel{\mathcal{F}}{\longleftrightarrow} X_2(\omega)$ then
  \[
  x_1(t) \cdot x_2(t) \stackrel{\mathcal{F}}{\longleftrightarrow} \frac{1}{2\pi} X_1(\omega)*X_2(\omega)
  \]
  where $X_1(\omega)*X_2(\omega)$ is convolution in the frequency domain
  \[
  X_1(\omega)*X_2(\omega) = \int\limits_{-\infty}^{\infty} X_1(\gamma)*X_2(\omega-\gamma)\;d\gamma
  \]
\item Time-Shift: if $x(t) \stackrel{\mathcal{F}}{\longleftrightarrow} X(\omega)$ then
  \[
  x(t-t_0) \stackrel{\mathcal{F}}{\longleftrightarrow} X(\omega)e^{-j\omega t_0}
  \]
\item Conjugate Symmetry: if $x(t) \stackrel{\mathcal{F}}{\longleftrightarrow} X(\omega)$ then
  \[
  x^*(t) \stackrel{\mathcal{F}}{\longleftrightarrow} X^*(-\omega)
  \]
  This implies that if $x(t)$ is real, then the magnitude spectrum is an even function, and the phase spectrum is an odd function.

\item Integration: if $x(t) \stackrel{\mathcal{F}}{\longleftrightarrow} X(\omega)$ then
  \[
  \int\limits_{-\infty}^t x(\tau)\; d\tau \stackrel{\mathcal{F}}{\longleftrightarrow} \frac{1}{j\omega} X(\omega) + \pi X(0) \delta(\omega)
  \]
\item Time and Frequency Scaling: if $x(t) \stackrel{\mathcal{F}}{\longleftrightarrow} X(\omega)$ then if $a$ is a real constant
  \[
  x(at) \stackrel{\mathcal{F}}{\longleftrightarrow} \frac{1}{|a|} X\left(\frac{\omega}{a}\right)
  \]
\item Parseval's Relation: if $x(t) \stackrel{\mathcal{F}}{\longleftrightarrow} X(\omega)$ then
  \[
  \int\limits_{0-\infty}^{\infty} |x(t)|^2\; dt =  \frac{1}{2\pi}\int\limits_{0-\infty}^{\infty} |X(\omega)|^2\;d\omega
  \]
\end{itemize}

\section{CT Fourier Transform of a Periodic Signal}

Even though the Fourier transform was derived in the case of an a-periodic signal, the linearity property of the transform, combined with one of our examples above shows us that we can take the Fourier Transform of a periodic signal. Consider a periodic signal with Fourier series expansion
\[
x(t) = \sum\limits_{k = -\infty}^{\infty} a_k e^{jk\omega_0 t}
\]
Taking the Fourier Transform
\[
\mathcal{F}\{x(t)\} = \mathcal{F}\left\{\sum\limits_{k = -\infty}^{\infty} a_k e^{jk\omega_0 t}\right\} = \sum\limits_{k = -\infty}^{\infty} a_k \mathcal{F}\{e^{jk\omega_0 t}\} = \sum\limits_{k = -\infty}^{\infty} a_k 2\pi \delta(\omega-k\omega_0)
\]
Thus the discrete Fourier series coefficients become the weights of the corresponding delta functions centered at the harmonic frequency.


\chapter{Thermodynamics and Statistical Mechanics}

\section{Laws of Thermodynamics}

	\subsection{Zeroth Law}
	If system $A$ is in equilibrium with system $B$ and system $C$ is also in equilibrium with system $B$, then system $A$ is in equilibrium with system $C$.

	\subsection{First Law}
	The total change in the energy of the system can be expressed as,
	\begin{equation}
		\mathrm{d}E = T\mathrm{d}S - p\mathrm{d}V + \sum_i\mu_i\mathrm{d}N_i.
	\end{equation}

	\subsection{Second Law}
	\subsection{Third Law}

\section{Adiabatic Processes}
\newthought{An adiabatic process} in thermodynamics is a reversible process where no heat is added to (or removed from) the gas system by its surroundings, the system evolves \textit{slowly} enough that it evolves through a sequence of states in thermal equilibrium, and the pressure and volume satisfy $pV^{\gamma}=\text{constant}$.

In the context of MHD (or hydrodynamics), we say that each fluid element is \textit{always} in local thermal equilibrium. This is consistent with the assumption of long times scales for the MHD (fluid) equations (i.e. long compared to the collisional/thermalization time scales in this case.)

\section{Equipartition Theorem}
\newthought{The equipartition theorem} states that at temperature $T$, the average energy of any quadratic degree of freedom is $(1/2)k_BT$. Note that the equipartition theorem applies to only those systems whose energy is in the form of quadratic degrees of freedom of the form $E(q) = cq^2$, where $c$ is some constant and $q$ is the degree of freedom (e.g. $x,p_y,L_z$ etc.).

\section{Boltzmann Distribution}
\newthought{Recall that the entropy} can be expressed as $S=k_B\ln{\Omega}$, where $\Omega$ is the multiplicity. This just states that the entropy increases with the multiplicity of the system, or the number of possible accessible microstates. We regard each microstate as equally probable. Thus, the probability of being in a particular state $s^{'}$ is proportional to the multiplicity of $s^{'}$. We can write the ratio of probabilities of being in states $s_2$ and $s_1$ as
\begin{equation}
	\frac{P(s_1)}{P(s_2)} = \frac{\Omega(s_2)}{\Omega(s_1)}.
\end{equation}
We can then write this ratio in terms of the entropy,
\begin{equation}
	\frac{P(s_1)}{P(s_2)} = \exp{\left(\frac{S(s_2) - S(s_1)}{k_B}\right)}.
\end{equation}
Recalling the first law, we can write the change in entropy as, letting $\mathrm{d}N\to0$ and neglecting $p\mathrm{d}V$ as small compared to $\mathrm{d}E$, $\mathrm{d}S=\mathrm{d}E/T$. Plugging this into our above expression,
\begin{equation}
	\frac{P(s_1)}{P(s_2)} = \exp{\left(-\frac{E(s_2) - E(s_1)}{k_BT}\right)},
\end{equation}
where the negative sign comes from the fact that the change in the reservoir energy is equal and opposite of that to the change in the energy of the single atom system. We call these exponential terms Boltzmann factors,
\begin{equation}
	\text{Boltzmann factor} = \exp{\left(-\frac{E(s)}{k_BT}\right)}.
\end{equation}
Separating factors of $s_1,s_2$, it can be seen that the LHS and RHS are independent, meaning they can be set equal to a constant which we will call $Z$. Thus, the probability of state $s$ can be written as
\begin{equation}
	P(s) = \frac{1}{Z}\exp{(-E(s)/k_BT)}.
\end{equation}
Using the fact that $\sum_sP(s)=1$, we can determine $Z$ such that
\begin{equation}
	P(s) = \frac{\exp{(-E(s)/k_BT)}}{\sum_s\exp{(-E(s)/k_BT)}}.
\end{equation}
This is the so-called Boltzmann distribution, or canonical distribution.

\section{Maxwellian}
\newthought{Using Boltzmann factors,} we can express the velocity distribution in thermal equilibrium as
\begin{equation}
	f_M(v) = (\frac{m}{2\pi k_BT})^{3/2}4\pi v^2\exp{(-mv^2/2k_BT)}.
\end{equation}
This distribution goes to zero as $v\to0$ and $V\to\infty$. It peaks at the thermal velocity, $v_T=\sqrt{2k_BT/m}$.


\subsection{Indefinite (pseduo) and split orthognal groups \(O(n,m,F)\)}

\subsubsection{Recap: Metric-preserving transformations}

The bilinear form is:

\(u^TMv\)

The transformations which preserve this are:

\(P^TMP=M\)

\subsubsection{The metric}

If the metric is:

\(M=\begin{bmatrix}-1 & 0 & 0 & 0\\0 & 1 & 0 & 0\\0 & 0 & 1 & 1\\0 & 0 & 0 & 1\end{bmatrix}\)

Then we have the indefinite orthogonal group \(O(3,1)\)

\subsubsection{The split orthogonal group}

Where \(n=m\) we have the split orthogonal group.

\(O(n,n,F)\)

\subsubsection{Signatures}


\subsection{Asymptotes and Other Things to Look For}\label{sec:Asymptotes}
A vertical asymptote\index{asymptote} is a place where the function
becomes infinite, typically because the formula for the function has a
denominator that becomes zero.  For example, the reciprocal function
$f(x)=1/x$ has a vertical asymptote at $x=0$, and the function $\tan
x$ has a vertical asymptote at $x=\pi/2$ (and also at $x=-\pi/2$,
$x=3\pi/2$, etc.).  Whenever the formula for a function contains a
denominator it is worth looking for a vertical asymptote by
checking to see if the denominator can ever be zero, and then checking
the limit at such points. Note that there is not always a vertical
asymptote where the derivative is zero: $f(x)=(\sin x)/x$ has a zero
denominator at $x=0$, but since $\ds \lim_{x\to 0}(\sin x)/x=1$ there is
no asymptote there.

A horizontal asymptote is a horizontal line to which $f(x)$ gets closer and
closer as $x$ approaches $\infty$ (or as $x$ approaches $-\infty$).  For
example, the reciprocal function has the $x$-axis for a horizontal
asymptote.  Horizontal asymptotes can be identified by computing
the limits $\ds \lim_{x \to \infty}f(x)$ and $\ds \lim_{x \to -\infty}f(x)$.
Since $\ds \lim_{x \to \infty}1/x=\lim_{x \to -\infty}1/x=0$, the line
$y=0$ (that is, the $x$-axis) is a horizontal asymptote in both directions.

Some functions have asymptotes that are neither horizontal nor
vertical, but some other line. Such asymptotes are somewhat more
difficult to identify and we will ignore them.

If the domain of the function does not extend out to infinity, we should
also ask what happens as $x$ approaches the boundary of the domain.  For
example, the function $\ds y=f(x)=1/\sqrt{r^2-x^2}$ has domain $-r<x<r$, and
$y$ becomes infinite as $x$ approaches either $r$ or $-r$. In this
case we might also identify this behavior because when $x=\pm r$ the
denominator of the function is zero.

If there are any points where the derivative fails to exist (a cusp or
corner), then we should take special note of what the function does at such
a point.

Finally, it is worthwhile to notice any symmetry.  A function $f(x)$ that
has the same value for $-x$ as for $x$, i.e., $f(-x)=f(x)$, is called an
``even function.''  Its graph is symmetric with respect to the $y$-axis.
Some examples of even functions are: $\ds x^n$ when $n$ is an even number,
$\cos x$, and $\ds \sin^2x$.  On the other hand, a function that satisfies the
property $f(-x)=-f(x)$ is called an ``odd function.''  Its graph is
symmetric with respect to the origin.  Some examples of odd functions are:
$x^n$ when $n$ is an odd number, $\sin x$, and $\tan x$.  Of course, most
functions are neither even nor odd, and do not have any particular
symmetry.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CS624: Analysis of Algorithms
% Copyright 2015 Pejman Ghorbanzade <pejman@ghorbanzade.com>
% Creative Commons Attribution-ShareAlike 4.0 International License
% More info: https://github.com/ghorbanzade/beacon
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section*{Question 8}

Prove that the \textsc{Hitting Set} problem, explained below, is NP-complete.
\begin{itemize}\itemsep=0pt
\item[] \textit{Instance:} A collection $C$ of subsets of a set $S$ together with a positive integer $K$.
\item[] \textit{Question:} Does $S$ contain a hitting set for $C$ of size $K$ or less? i.e.
Is there a subset $S' \subseteq S$ with $|S'| \leq K$ such that $S'$ contains at least one element of each set $c \in C$?
\end{itemize}

\subsection*{Solution}

We will first prove \textsc{Hitting Set} is in NP and then will show that the \textsc{Vertex Cover} problem can be polynomially reduced to this problem which proves \textsc{Hitting Set} is NP-Hard.
\begin{enumerate}[label=(\alph*)]
\item To show that \textsc{Hitting Set} is in NP, we must show there as an algorithm that verifies in polynomial time if for a given finite set $S$, a collection $C$ of its subsets, a number $K$ and a final subset $S'$, intersection of $S'$ with each set $c \in C$ is non-empty.
Since $S'$ is given, we need only to check if there are at most $K$ sets $c \in C$ whose intersection with $S'$ is not empty.
Assuming there are $|C|$ elements in $C$, since finding intersection of two sets $c_1$ and $c_2$ is $\mathcal{O}(\max\{|c_1|, |c_2|\}^2)$, runtime to obtain size of $S'$ would be $\mathcal{O}(n^3)$ which is polynomial.
Therefore, \textsc{Hitting Set} is satisfiable in polynomial time and is thus in NP.
\item To show \textsc{Hitting Set} is NP-Hard we reduce the NP-Hard problem of \textsc{Vertex Cover} in polynomial time to \textsc{Hitting Set}.
Suppose $V$ is a vertex cover of size at most $K$ for graph $G(V, E)$.
Since $V$ is a vertex cover, for any edge $e = (u,v)$ either $u$ or $v$ is in $V$.
We can now think of $e$ as a collection $c \in C$ with size 2.
Since for each $c$, one of the elements is in $V$, graph $G=(V,E)$ has a hitting set of size at most $K$.
Since the reduction takes linear time to the size of the input $S$, it is a polynomial-time reduction.
Hence, \textsc{Hitting Set} is at least as hard as \textsc{Vertex Cover} which is NP-Complete.
\end{enumerate}
Therefore we have shown \textsc{Hitting Set} is in NP and is NP-hard which concludes it is NP-Complete.


\section{Model Description}

The Bore Angle Calculation model uses vector mathematics in order to calculate the miss angle from a boresight on the spacecraft to its desired target. The model also calculates the azimuth angle of the boresight.

\begin{figure}[H]
	\centerline{
		 \includegraphics[height = 3in]{Figures/missAngleImg.JPG}
	}
	\caption{All of the vectors and the miss angle are labeled here. $\bm O$ is the vector that represents the boresight of the instrument that the test is interested in. $\bm{L}$ is the direction that the spacecraft should be pointing its boresight in. $\bm M$ is the miss angle of the boresight to its desired direction. All of the $\bm r$ vectors are various position vectors designated by their subscripts.}
	\label{fig:Fig1}
\end{figure}

The model begins by creating a direction cosine martix to represent the pointing frame from the inertial frame. Then the boresight vector is projected into this frame, and the miss angle and the azimuth angle are calculated using standard trigonometry. These calculations are performed in the following way:

First, the unit relative position vector of the spacecraft is calculated relative to the celestial body that the spacecraft is supposed to be pointing at. Using Equation \ref{eq:rePos}. Where $\hat{\bm r}_{B/S}$ is the unit relative position of the spacecraft to the planet, $\bm r_{N/S}$ is the position of the celestial body in the inertial frame, and $\bm r_{N/B}$ is the position of the spacecraft in the inertial frame.

 \begin{equation}
 	\label{eq:rePos}
 	\hat{\bm r}_{B/S} = \frac{\bm r_{N/S} - \bm r_{N/B}}{\vert \bm r_{N/S} - \bm r_{N/B} \vert}
\end{equation}

Next, the unit relative velocity of the spacecraft is calculated in the same manner.

 \begin{equation}
 	\label{eq:reVel}
 	\hat{\bm v}_{B/S} = \frac{\bm v_{N/S} - \bm v_{N/B}}{\vert \bm v_{N/S} - \bm v_{N/B} \vert}
\end{equation}

Then, direction cosine matrix from the inertial frame to the pointing frame is constructed. The first row of the direction cosine matrix is set to be the unit relative position vector calculated in the first step. The last row of the direction cosine matrix is set to be the result of the cross product of the relative unit position vector with the cross product of the relative position vector with the relative velocity vector. Then, the second row of the direction cosine matrix is set to be the cross product of the first and last row of the direction cosine matrix, as show in Equation \ref{eq:dcm3}

 \begin{equation}
 	\label{eq:dcm3}
 	[\bm{PN}] =  \begin{bmatrix}
    \hat{ \bm r}_{B/S}^T \\
     ((\hat{\bm r}_{B/S}  \times  (\bm r_{B/S}  \times \bm v_{B/S})) \times \hat{ \bm r}_{B/S}) ^T\\
    (\hat{\bm r}_{B/S}  \times  (\bm r_{B/S}  \times \bm v_{B/S}))^T
    \end{bmatrix}
\end{equation}

 Once the direction cosine matrix has been created, the direction cosine matrix from the body frame to the pointing frame is created by multiplying the direction cosine matrix from the body frame to the inertial frame and the inertial frame to the pointing frame together. Then the boresight vector, $\bm O$ (which is shown in Figure \ref{fig:Fig1}), is projected into the pointing frame by dotting the new direction cosine matrix with the boresight vector in the body frame. This is shown in Equation \ref{eq:boreSight}.

 \begin{equation}
 	\label{eq:boreSight}
 	\leftexp{P}{\bm {O}} = [\bm{PB}] \cdot\leftexp{B}{\bm {O}}
\end{equation}

From there, the boresight vector is multiplied by the baseline vector in the pointing frame. Where the baseline vector in the pointing frame is defined as $\bm L = \hat{\bm i}$. Then the product of the boresight vector and the baseline vector is passed through the arccosine function to get the boresight miss angle. As shown in Equation \ref{eq:missAng}.

 \begin{equation}
 	\label{eq:missAng}
 	M = \arccos(\bm O \cdot \bm L)
\end{equation}

\begin{figure}[H]
	\centerline{
		 \includegraphics[height = 2.5in]{Figures/azi.JPG}
	}
	\caption{The azimuth angle, A is shown here with respect to the pointing coordinate frame and the vectors used to construct the azimuth angle.}
	\label{fig:Fig2}
\end{figure}

The azimuth angle is calculated by passing the product of the last two components of the borsight vector. This is shown in Equation \ref{eq:azAng} and in Figure \ref{fig:Fig2}.

 \begin{equation}
 	\label{eq:azAng}
 	A = \arctan\bigg(\frac{\bm O_{\hat k}}{\bm O_{\hat j}}\bigg)
\end{equation}


\section*{Exercises}

\begin{ex}
  Let
  \begin{equation*}
    A = \begin{mymatrix}{rr}
      5 & 7 \\
      -4 & 3 \\
    \end{mymatrix}.
  \end{equation*}
  Find the characteristic polynomial $p(\eigenvar)$, and compute
  $p(A)$.
\end{ex}

\begin{ex}
  Let
  \begin{equation*}
    A = \begin{mymatrix}{rrr}
      1 & 2 & 0 \\
      0 & 2 & -1 \\
      0 & 1 & 4 \\
    \end{mymatrix}.
  \end{equation*}
  Find the characteristic polynomial $p(\eigenvar)$, and compute
  $p(A)$.
\end{ex}

\begin{ex}
  \begin{enumerate}
  \item Let $A$ be a $2\times 2$-matrix. Prove that $A^2$ is a linear
    combination of $A$ and $I$.
  \item Given an example of a $3\times 3$-matrix $A$ such that $A^2$
    is not a linear combination of $A$ and $I$.
  \end{enumerate}
  \begin{sol}
    \begin{enumerate}
    \item The characteristic polynomial of $A$ is a quadratic
      polynomial, and therefore it is of the form
      $p(\eigenvar) = \eigenvar^2 + b\eigenvar + c$, for some
      $r,s\in\R$. By the Cayley-Hamilton theorem, $p(A)=0$, therefore
      $A^2 = -bA - cI$. This proves that $A^2$ is a linear combination
      of $A$ and $I$.
    \item $A=\begin{mymatrix}{rrr}
        0 & 1 & 0 \\
        0 & 0 & 1 \\
        0 & 0 & 0 \\
      \end{mymatrix}$.
    \end{enumerate}
  \end{sol}
\end{ex}


\documentclass[12pt]{mmalatex}
\usepackage{examples}

\begin{document}

\section*{Quadratic convergence of Newton-Raphson iterations}

This is a simple example that uses Mathematica to demonstrate the quadratic converegnce of Newton-Raphson iterations to the exact root of a non-linear equation.

\vspace{10pt}

\begin{mathematica}
   f[x_] = N[x - Exp[-x] , 200];                                (* work to 200 decimal digits *)
   a = NestList[(# - f[#]/f'[#]) &, SetPrecision[1/2,200], 6];  (* list of x values *)
   b = f /@ a;                                                  (* list of f values *)
   c = #^2 & /@ b;                                              (* list of f^2 values *)
   (* mmaBeg(table) *)
   Print[OutputForm[
         ToString[0] <> "&" <>
         ToString[NumberForm[a[[1]], 25]] <> "&" <>
         ToString[ScientificForm[b[[1]], 11, NumberFormat -> (SequenceForm[#1, "e", #3] &)]] <>
         "\\\\"
         ]]
   Do[Print[OutputForm[
            ToString[i-1] <> "&" <>
            ToString[NumberForm[a[[i]], 25]] <> "&" <>
            ToString[ScientificForm[b[[i]], 11, NumberFormat -> (SequenceForm[#1, "e", #3] &)]] <> "&" <>
            ToString[NumberForm[b[[i]]/c[[i - 1]], 5]] <>
            "\\\\"
            ]], {i, 2, 7}]
   (* mmaEnd(table) *)
\end{mathematica}

\clearpage

Note the clear quadratic convergence in the iterations -- the last column settles to approximately $-0.11546$ independent of the number of iterations. This behaviour would not be seen using normal floating point computations as they are normally limited to no more than 18 decimal digits. This computation used 200 decimal digits.

\def\eps{\epsilon}
\def\RuleA{\vrule depth0pt  width0pt height14pt}
\def\RuleB{\vrule depth8pt  width0pt height14pt}
\def\RuleC{\vrule depth10pt width0pt height16pt}

\setlength{\tabcolsep}{0.025\textwidth}%

\begin{center}
\begin{tabular}{cccc}%
   \noalign{\hrule height 1pt}
   \multicolumn{4}{c}{\RuleC\rmfamily\bfseries%
   Newton-Raphson iterations \quad%
   $x_{n+1} = x_n - f_n/f'_n\ ,\quad f(x) = x-e^{-x}$}\\
   \noalign{\hrule height 1pt}
   \RuleB$n$&$x_n$&$ \eps_{n} =  x_{n} - e^{-x_{n}}$&$\eps_{n}/\eps_{n-1}^2$\\
   \noalign{\hrule height 0.5pt}
   \mma{table}
   \noalign{\hrule height 1pt}
\end{tabular}
\end{center}

\vspace{20pt}

\begin{minipage}[t]{0.75\textwidth}
\begin{latex}
   \def\eps{\epsilon}
   \def\RuleA{\vrule depth0pt  width0pt height14pt}
   \def\RuleB{\vrule depth8pt  width0pt height14pt}
   \def\RuleC{\vrule depth10pt width0pt height16pt}

   \setlength{\tabcolsep}{0.025\textwidth}%

   \begin{center}
   \begin{tabular}{cccc}%
      \noalign{\hrule height 1pt}
      \multicolumn{4}{c}{\RuleC\rmfamily\bfseries%
      Newton-Raphson iterations \quad%
      $x_{n+1} = x_n - f_n/f'_n\ ,\quad f(x) = x-e^{-x}$}\\
      \noalign{\hrule height 1pt}
      \RuleB$n$&$ x_n$&$\eps_{n} =  x_{n} - e^{-x_{n}}$&$\eps_{n}/\eps_{n-1}^2$\\
      \noalign{\hrule height 0.5pt}
      \mma{table}
      \noalign{\hrule height 1pt}
   \end{tabular}
   \end{center}
\end{latex}
\end{minipage}

\end{document}


\section{Identity types}\label{chap:identity}

\index{identity type|(}
\index{inductive type!identity type|(}
From the perspective of types as proof-relevant propositions, how should we think of \emph{equality} in type theory? Given a type $A$, and two terms $x,y:A$, the equality $\id{x}{y}$ should again be a type. Indeed, we want to \emph{use} type theory to prove equalities. \emph{Dependent} type theory provides us with a convenient setting for this: the equality type $\id{x}{y}$ is dependent on $x,y:A$.

Then, if $\id{x}{y}$ is to be a type, how should we think of the terms of $\id{x}{y}$. A term $p:\id{x}{y}$ witnesses that $x$ and $y$ are equal terms of type $A$. In other words $p:\id{x}{y}$ is an \emph{identification} of $x$ and $y$. In a proof-relevant world, there might be many terms of type $\id{x}{y}$. I.e., there might be many identifications of $x$ and $y$. And, since $\id{x}{y}$ is itself a type, we can form the type $\id{p}{q}$ for any two identifications $p,q:\id{x}{y}$. That is, since $\id{x}{y}$ is a type, we may also use the type theory to prove things \emph{about} identifications (for instance, that two given such identifications can themselves be identified), and we may use the type theory to perform constructions with them. As we will see shortly, we can give every type a groupoidal structure.

Clearly, the equality type should not just be any type dependent on $x,y:A$. Then how do we form the equality type, and what ways are there to use identifications in constructions in type theory? The answer to both these questions is that we will form the identity type as an \emph{inductive} type, generated by just a reflexivity term providing an identification of $x$ to itself. The induction principle then provides us with a way of performing constructions with identifications, such as concatenating them, inverting them, and so on. Thus, the identity type is equipped with a reflexivity term, and further possesses the structure that are generated by its induction principle and by the type theory. This inductive construction of the identity type is elegant, beautifully simple, but far from trivial!

The situation where two terms can be identified in possibly more than one way is analogous to the situation in \emph{homotopy theory}, where two points of a space can be connected by possibly more than one \emph{path}. Indeed, for any two points $x,y$ in a space, there is a \emph{space of paths} from $x$ to $y$. Moreover, between any two paths from $x$ to $y$ there is a space of \emph{homotopies} between them, and so on. This leads to the homotopy interpretation of type theory, outlined in \cref{tab:homotopy_interpretation}. The connection between homotopy theory and type theory been made precise by the construction of homotopical models of type theory, and it has led to the fruitful research area of \emph{synthetic homotopy theory}, the subfield of \emph{homotopy type theory} that is the topic of this course.

\begin{table}
\begin{center}
\caption{\label{tab:homotopy_interpretation}The homotopy interpretation\index{Homotopy interpretation}}
\begin{tabular}{ll}
\toprule
\emph{Type theory} &  \emph{Homotopy theory} \\
\midrule
Types  & Spaces \\
Dependent types & Fibrations \\
Terms & Points \\
Dependent pair type & Total space \\
Identity type & Path fibration\\
\bottomrule
\end{tabular}
\end{center}
\end{table}

\subsection{The inductive definition of identity types}

\begin{defn}
  Consider a type $A$ and let $a:A$. Then we define the \define{identity type} of $A$ at $a$ as an inductive family of types $a =_A x$\index{a = x@{$a = x$}|see {identity type}} indexed by $x:A$, of which the constructor is\index{refl@{$\refl{}$}}\index{identity type!refl@{$\refl{}$}}
  \begin{equation*}
    \refl{a}:a=_Aa.
  \end{equation*}
  The induction principle of the identity type\index{identity type!induction principle}\index{induction principle!of the identity type} postulates that for any family of types $P(x,p)$ indexed by $x:A$ and $p:a=_A x$, there is a function\index{path-ind@{$\pathind$}}\index{identity type!path-ind@{$\pathind$}}
  \begin{equation*}
    \pathind_a:P(a,\refl{a}) \to \prd{x:A}\prd{p:a=_A x} P(x,p)
  \end{equation*}
  that satisfies $\pathind_a(p,a,\refl{a})\jdeq p$.

  A term of type $a=_A x$ is also called an \define{identification}\index{identification}\index{identity type!identification} of $a$ with $x$, and sometimes it is called a \define{path}\index{path}\index{identity type!path} from $a$ to $x$.
The induction principle for identity types is sometimes called \define{identification elimination}\index{identification elimination}\index{induction principle!identification elimination}\index{identity type!identification elimination} or \define{path induction}\index{path induction}\index{identity type!path induction}\index{induction principle!path induction}. We also write $\idtypevar{A}$\index{Id A@{$\idtypevar{A}$}|see {identity type}} for the identity type on $A$, and often we write $a=x$ for the type of identifications of $a$ with $x$, omitting reference to the ambient type $A$.
\end{defn}

\begin{rmk}
  We see that the identity type is not just an inductive type, like the inductive types $\N$, $\emptyt$, and $\unit$ for example, but it is and inductive \emph{family} of types. Even though we have a type $a=_A x$ for any $x:A$, the constructor only provides a term $\refl{a}:a=_A a$, identifying $a$ with itself. The induction principle then asserts that in order to prove something about all identifications of $a$ with some $x:A$, it suffices to prove this assertion about $\refl{a}$ only. We will see in the next sections that this induction principle is strong enough to derive many familiar facts about equality, namely that it is a symmetric and transitive relation, and that all functions preserve equality.
\end{rmk}

\begin{rmk}
  \index{rules!identity type|(}\index{identity type!rules|(}
  Since the identity types require getting used to, we provide the formal rules
  for identity types. The identity type is formed by the formation rule:
  \begin{prooftree}
    \AxiomC{$\Gamma\vdash a:A$}
    \UnaryInfC{$\Gamma,x:A\vdash a=_A x~\type$}
  \end{prooftree}
  The constructor of the identity type is then given by the introduction rule:
  \begin{prooftree}
    \AxiomC{$\Gamma\vdash a:A$}
    \UnaryInfC{$\Gamma\vdash \refl{a}:a=_A a$}
  \end{prooftree}
  The induction principle is now given by the elimination rule:
  \begin{prooftree}
    \AxiomC{$\Gamma\vdash a:A$}
    \AxiomC{$\Gamma,x:A,p:a=_A x\vdash P(x,p)~\type$}
    \BinaryInfC{$\Gamma\vdash \pathind_a:P(a,\refl{a})\to\prd{x:A}\prd{p:a=_A x}P(x,p)$}
  \end{prooftree}
  And finally the computation rule is:
  \begin{prooftree}
    \AxiomC{$\Gamma\vdash a:A$}
    \AxiomC{$\Gamma,x:A,p:a=_A x\vdash P(x,p)~\type$}
    \BinaryInfC{$\Gamma\vdash \pathind_a(p,a,\refl{a})\jdeq p : P(a,\refl{a})$}
  \end{prooftree}
  \index{rules!identity type|)}\index{identity type!rules|)}
\end{rmk}

\begin{rmk}
  One might wonder whether it is also possible to form the identity type at a \emph{variable} of type $A$, rather than at a term. This is certainly possible: since we can form the identity type in \emph{any} context, we can form the identity type at a variable $x:A$ as follows:
  \begin{prooftree}
    \AxiomC{$\Gamma,x:A\vdash x:A$}
    \UnaryInfC{$\Gamma,x:A,y:A\vdash x=_A y~\type$}
  \end{prooftree}
  In this way we obtain the `binary' identity type. Its constructor is then also indexed by $x:A$. We have the following introduction rule
  \begin{prooftree}
    \AxiomC{$\Gamma,x:A\vdash x:A$}
    \UnaryInfC{$\Gamma,x:A\vdash \refl{x}:x=_A x$}
  \end{prooftree}
  and similarly we have elimination and computation rules.
\end{rmk}

\subsection{The groupoidal structure of types}\label{sec:groupoid}
\index{groupoid laws!of identifications|(}
We show that identifications can be \emph{concatenated} and \emph{inverted}, which corresponds to the transitivity and symmetry of the identity type.

\begin{defn}\label{defn:id_concat}
Let $A$ be a type. We define the \define{concatenation}\index{concatenation!for identifications}\index{concat@{$\concat$}} operation
\begin{equation*}
\concat : \prd{x,y,z:A} (\id{x}{y})\to(\id{y}{z})\to (\id{x}{z}).
\end{equation*}
We will write $\ct{p}{q}$ for $\concat(p,q)$.
\end{defn}

\begin{constr}
We construct the concatenation operation by path induction. It suffices to construct
\begin{equation*}
\concat(\refl{x}):\prd{z:A} (x=z)\to(x=z).
\end{equation*}
Here we take $\concat(\refl{x})_z \jdeq \idfunc[(x=z)]$.
Explicitly, the term we have constructed is
\begin{equation*}
\lam{x}\pathind_x(\lam{z}\idfunc[(\id{x}{z})]):\prd{x,y:A} (x=y)\to \prd{z:A} (y=z)\to (x=z).
\end{equation*}
To obtain a term of the asserted type we need to swap the order of the arguments $p:x=y$ and $z:A$, using \cref{ex:swap}.
\end{constr}

\begin{defn}\label{defn:id_inv}
Let $A$ be a type. We define the \define{inverse operation}\index{inverse operation!for identifications}\index{inv@{$\invfunc$}}
\begin{equation*}
\invfunc:\prd{x,y:A} (x=y)\to (y=x).
\end{equation*}
Most of the time we will write $p^{-1}$ for $\invfunc(p)$.
\end{defn}

\begin{constr}
We construct the inverse operation by path induction. It suffices to construct
\begin{equation*}
\invfunc(\refl{x}): x=x,
\end{equation*}
for any $x:A$. Here we take $\invfunc(\refl{x})\defeq \refl{x}$.
\end{constr}

The next question is whether the concatenation and inverting operations on paths behave as expected. More concretely, is path concatenation associative, does it satisfy the unit laws, and is the inverse of a path indeed a two-sided inverse?

For example, in the case of associativity we are asking to compare the paths
\begin{equation*}
  \ct{(\ct{p}{q})}{r}\qquad\text{and}\qquad\ct{p}{(\ct{q}{r})}
\end{equation*}
for any $p:x=y$, $q:y=z$, and $r:z=w$ in a type $A$. The computation rules of path induction are not strong enough to conclude that $\ct{(\ct{p}{q})}{r}$ and $\ct{p}{(\ct{q}{r})}$ are judgmentally equal. However, both $\ct{(\ct{p}{q})}{r}$ and $\ct{p}{(\ct{q}{r})}$ are terms of the same type: they are identifications of type $x=w$. Since the identity type is a type like any other, we can ask whether there is an \emph{identification}
\begin{equation*}
\ct{(\ct{p}{q})}{r}=\ct{p}{(\ct{q}{r})}.
\end{equation*}
This is a very useful idea: while it is often impossible to show that two terms of the same type are judgmentally equal, it may be the case that those two terms can be \emph{identified}. Indeed, we identify two terms by constructing a term of the identity type, and we can use all the type theory at our disposal in order to construct such a term. In this way we can show, for example, that addition on the natural numbers or on the integers is associative and satisfies the unit laws. And indeed, here we will show that path concatenation is associative and satisfies the unit laws.

\begin{defn}\label{defn:id_assoc}
  Let $A$ be a type and consider three consecutive paths
  \begin{equation*}
    \begin{tikzcd}
      x \arrow[r,equals,"p"] & y \arrow[r,equals,"q"] & z \arrow[r,equals,"r"] & w
    \end{tikzcd}
  \end{equation*}
  in $A$. We define the \define{associator}\index{associativity!of path concatenation}
  \begin{equation*}
    \assoc(p,q,r) : \ct{(\ct{p}{q})}{r}=\ct{p}{(\ct{q}{r})}.
  \end{equation*}
\end{defn}

\begin{constr}
By path induction it suffices to show that
\begin{equation*}
\prd{z:A}\prd{q:x=z}\prd{w:A}\prd{r:z=w} \ct{(\ct{\refl{x}}{q})}{r}= \ct{\refl{x}}{(\ct{q}{r})}.
\end{equation*}
Let $q:x=z$ and $r:z=w$. Note that by the computation rule of the path induction principle we have a judgmental equality $\ct{\refl{x}}{q}\jdeq q$. Therefore we conclude that
\begin{equation*}
  \ct{(\ct{\refl{x}}{q})}{r}\jdeq \ct{q}{r}.
\end{equation*}
Similarly we have a judgmental equality $\ct{\refl{x}}{(\ct{q}{r})}\jdeq \ct{q}{r}$. Thus we see that the left-hand side and the right-hand side in
\begin{equation*}
  \ct{(\ct{\refl{x}}{q})}{r}=\ct{\refl{x}}{(\ct{q}{r})}
\end{equation*}
are judgmentally equal, so we can simply define $\assoc(\refl{x},q,r)\defeq\refl{\ct{q}{r}}$.
\end{constr}

\begin{defn}\label{defn:id_unit}
Let $A$ be a type. We define the left and right \define{unit law operations}\index{unit law operations!for identifications}, which assigns to each $p:x=y$ the terms\index{left unit@{$\leftunit$}}\index{right unit@{$\rightunit$}}
\begin{align*}
\leftunit(p) & : \ct{\refl{x}}{p}=p \\
\rightunit(p) & : \ct{p}{\refl{y}}=p,
\end{align*}
respectively.
\end{defn}

\begin{constr}
By identification elimination it suffices to construct
\begin{align*}
\leftunit(\refl{x}) & : \ct{\refl{x}}{\refl{x}} = \refl{x} \\
\rightunit(\refl{x}) & : \ct{\refl{x}}{\refl{x}} = \refl{x}.
\end{align*}
In both cases we take $\refl{\refl{x}}$.
\end{constr}

\begin{defn}\label{defn:id_invlaw}
Let $A$ be a type. We define left and right \define{inverse law operations}\index{inverse law operations!for identifications}\index{left inv@{$\leftinv$}}\index{right inv@{$\rightinv$}}
\begin{align*}
\leftinv(p) & : \ct{p^{-1}}{p} = \refl{y} \\
\rightinv(p) & : \ct{p}{p^{-1}} = \refl{x}.
\end{align*}
\end{defn}

\begin{constr}
By identification elimination it suffices to construct
\begin{align*}
\leftinv(\refl{x}) & : \ct{\refl{x}^{-1}}{\refl{x}} = \refl{x} \\
\rightinv(\refl{x}) & : \ct{\refl{x}}{\refl{x}^{-1}} = \refl{x}.
\end{align*}
Using the computation rules we see that
\begin{equation*}
\ct{\refl{x}^{-1}}{\refl{x}}\jdeq \ct{\refl{x}}{\refl{x}}\jdeq\refl{x},
\end{equation*}
so we define $\leftinv(\refl{x})\defeq \refl{\refl{x}}$. Similarly it follows from the computation rules that
\begin{equation*}
\ct{\refl{x}}{\refl{x}^{-1}} \jdeq \refl{x}^{-1}\jdeq \refl{x}
\end{equation*}
so we again define $\rightinv(\refl{x})\defeq\refl{\refl{x}}$.
\end{constr}

\begin{rmk}
  We have seen that the associator, the unit laws, and the inverse laws, are all proven by constructing an identification of identifications. And indeed, there is nothing that would stop us from considering identifications of those identifications of identifications. We can go up as far as we like in the \emph{tower of identity types}\index{tower of identity types}\index{identity type!tower of identity types}, which is obtained by iteratively taking identity types.

  The iterated identity types give types in homotopy type theory a very intricate structure. One important way of studying this structure is via the homotopy groups of types, a subject that we will gradually be working towards.
\end{rmk}
\index{groupoid laws!of identifications|)}

\subsection{The action on paths of functions}

\index{action on paths|(}
\index{identity type!action on paths|(}
Using the induction principle of the identity type we can show that every function preserves identifications.
In other words, every function sends identified terms to identified terms.
Note that this is a form of continuity for functions in type theory: if there is a path that identifies two points $x$ and $y$ of a type $A$, then there also is a path that identifies the values $f(x)$ and $f(y)$ in the codomain of $f$.

\begin{defn}\label{defn:ap}
Let $f:A\to B$ be a map. We define the \define{action on paths}\index{function!action on paths} of $f$ as an operation\index{ap f@{$\apfunc{f}$}|see {action on paths}}
\begin{equation*}
\apfunc{f} : \prd{x,y:A} (\id{x}{y})\to(\id{f(x)}{f(y)}).
\end{equation*}
Moreover, there are operations\index{ap-id@{$\apid$}}\index{action on paths!ap-id@{$\apid$}}\index{ap-comp@{$\apcomp$}}\index{action on paths!ap-comp@{$\apcomp$}}
\begin{align*}
\apid_A & : \prd{x,y:A}\prd{p:\id{x}{y}} \id{p}{\ap{\idfunc[A]}{p}} \\
\apcomp(f,g) & : \prd{x,y:A}\prd{p:\id{x}{y}} \id{\ap{g}{\ap{f}{p}}}{\ap{g\circ f}{p}}.
\end{align*}
\end{defn}

\begin{constr}
First we define $\apfunc{f}$ by identity elimination, taking
\begin{equation*}
\apfunc{f}(\refl{x})\defeq \refl{f(x)}.
\end{equation*}
Next, we construct $\apid_A$ by identity elimination, taking
\begin{equation*}
\apid_A(\refl{x}) \defeq \refl{\refl{x}}.
\end{equation*}
Finally, we construct $\apcomp(f,g)$ by identity elimination, taking
\begin{equation*}
\apcomp(f,g,\refl{x}) \defeq \refl{g(f(x))}.\qedhere
\end{equation*}
\end{constr}

\begin{defn}\label{defn:ap-preserve}
Let $f:A\to B$ be a map. Then there are identifications\index{ap-refl@{$\aprefl$}}\index{ap-inv@{$\apinv$}}\index{ap-concat@{$\apconcat$}}\index{action on paths!ap-refl@{$\aprefl$}}\index{action on paths!ap-inv@{$\apinv$}}\index{action on paths!ap-concat@{$\apconcat$}}
\begin{align*}
\aprefl(f,x) & : \id{\ap{f}{\refl{x}}}{\refl{f}(x)} \\
\apinv(f,p) & : \id{\ap{f}{p^{-1}}}{\ap{f}{p}^{-1}} \\
\apconcat(f,p,q) & : \id{\ap{f}{\ct{p}{q}}}{\ct{\ap{f}{p}}{\ap{f}{q}}}
\end{align*}
for every $p:\id{x}{y}$ and $q:\id{x}{y}$.
\end{defn}

\begin{constr}
To construct $\aprefl(f,x)$ we simply observe that ${\ap{f}{\refl{x}}}\jdeq {\refl{f}(x)}$, so we take
\begin{equation*}
\aprefl(f,x)\defeq\refl{\refl{f(x)}}.
\end{equation*}
We construct $\apinv(f,p)$ by identification elimination on $p$, taking
\begin{equation*}
\apinv(f,\refl{x}) \defeq \refl{\ap{f}{\refl{x}}}.
\end{equation*}
Finally we construct $\apconcat(f,p,q)$ by identification elimination on $p$, taking
\begin{equation*}
\apconcat(f,\refl{x},q)  \defeq \refl{\ap{f}{q}}.\qedhere
\end{equation*}
\end{constr}
\index{action on paths|)}
\index{identity type!action on paths|)}

\subsection{Transport}

Dependent types also come with an action on paths: the \emph{transport} functions.
Given an identification $p:\id{x}{y}$ in the base type $A$, we can transport any term $b:B(x)$ to the fiber $B(y)$.
The transport functions have many applications, which we will encounter throughout this course.

\begin{defn}
Let $A$ be a type, and let $B$ be a type family over $A$.
We will construct a \define{transport}\index{transport}\index{family!transport}\index{identity type!transport} operation\index{tr B@{$\tr_B$}}
\begin{equation*}
\tr_B:\prd{x,y:A} (\id{x}{y})\to (B(x)\to B(y)).
\end{equation*}
\end{defn}

\begin{constr}
We construct $\tr_B(p)$ by induction on $p:x=_A y$, taking
\begin{equation*}
\tr_B(\refl{x}) \defeq \idfunc[B(x)].\qedhere
\end{equation*}
\end{constr}

Thus we see that type theory cannot distinguish between identified terms $x$ and $y$, because for any type family $B$ over $A$ one gets a term of $B(y)$ as soon as $B(x)$ has a term.

As an application of the transport function we construct the \emph{dependent} action on paths\index{dependent action on paths}\index{dependent function!dependent action on paths} of a dependent function $f:\prd{x:A}B(x)$. Note that for such a dependent function $f$, and an identification $p:\id[A]{x}{y}$, it does not make sense to directly compare $f(x)$ and $f(y)$, since the type of $f(x)$ is $B(x)$ whereas the type of $f(y)$ is $B(y)$, which might not be exactly the same type. However, we can first \emph{transport} $f(x)$ along $p$, so that we obtain the term $\tr_B(p,f(x))$ which is of type $B(y)$. Now we can ask whether it is the case that $\tr_B(p,f(x))=f(y)$. The dependent action on paths of $f$ establishes this identification.

\begin{defn}\label{defn:apd}
Given a dependent function $f:\prd{a:A}B(a)$ and a path $p:\id{x}{y}$ in $A$, we construct a path\index{apd f@{$\apdfunc{f}$}}
\begin{equation*}
\apd{f}{p} : \id{\tr_B(p,f(x))}{f(y)}.
\end{equation*}
\end{defn}

\begin{constr}
The path $\apd{f}{p}$ is constructed by path induction on $p$. Thus, it suffices to construct a path
\begin{equation*}
\apd{f}{\refl{x}}:\id{\tr_B(\refl{x},f(x))}{f(x)}.
\end{equation*}
Since transporting along $\refl{x}$ is the identity function on $B(x)$, we simply take $\apd{f}{\refl{x}}\defeq\refl{f(x)}$.
\end{constr}

\begin{exercises}
\exercise
  \begin{subexenum}
  \item State Goldbach's Conjecture\index{Goldbach's Conjecture} in type theory.
  \item State the Twin Prime Conjecture\index{Twin Prime Conjecture} in type theory.
  \end{subexenum}
\exercise \label{ex:inv_assoc}Show that the operation inverting paths distributes over the concatenation operation, i.e., construct an identification
  \index{distributivity!of inv over concat@{of $\invfunc$ over $\concat$}}
  \index{identity type!distributive-inv-concat@{$\distributiveinvconcat$}}
  \begin{align*}
    \distributiveinvconcat(p,q):\id{(\ct{p}{q})^{-1}}{\ct{q^{-1}}{p^{-1}}}.
  \end{align*}
  for any $p:\id{x}{y}$ and $q:\id{y}{z}$.
\exercise \label{ex:inv_con}For any $p:x=y$, $q:y=z$, and $r:x=z$, construct maps
  \index{identity type!inv-con@{$\invcon$}}
  \index{inv-con@{$\invcon$}}
  \index{identity type!con-inv@{$\coninv$}}
  \index{con-inv@{$\coninv$}}
  \begin{align*}
    \invcon(p,q,r) & : (\ct{p}{q}=r)\to (q=\ct{p^{-1}}{r}) \\
    \coninv(p,q,r) & : (\ct{p}{q}=r)\to (p=\ct{r}{q^{-1}}).
  \end{align*}
\exercise Let $B$ be a type family over $A$, and consider a path $p:\id{x}{x'}$ in $A$. Construct for any $y:B(x)$ a path\index{lift@{$\lift$}}\index{identity type!lift@{$\lift$}}
  \begin{equation*}
    \lift_B(p,y) : \id{(x,y)}{(x',\tr_B(p,y))}.
  \end{equation*}
  In other words, a path in the \emph{base type} $A$ \emph{lifts} to a path in the total space $\sm{x:A}B(x)$ for every term over the domain, analogous to the path lifting property for fibrations in homotopy theory.
\exercise \label{ex:semi-ring-laws-N}In this exercise we show that the operations of addition and multiplication on the natural numbers satisfy the laws of a commutative \define{semi-ring}.%
  \index{semi-ring laws!for N@{for $\N$}}%
  \index{natural numbers!semi-ring laws}%
  \index{associativity!of addition on N@{of addition on $\N$}}%
  \index{unit laws!for addition on N@{for addition on $\N$}}%
  \index{commutativity!of addition on N@{of addition on $\N$}}%
  \index{associativity!of multiplication on N@{of multiplication on $\N$}}%
  \index{unit laws!for multiplication on N@{for multiplication on $\N$}}%
  \index{commutativity!of multiplication on N@{of multiplication on $\N$}}%
  \index{distributivity!of mulN over addN@{of $\mulN$ over $\addN$}}%
  \begin{subexenum}
  \item Show that addition satisfies the following laws:
    \begin{align*}
      m+0 & = m & m+\succN(n) & = \succN(m+n) \\
      0+m & = m & \succN(m)+n & = \succN(m+n).
    \end{align*}
  \item Show that addition is associative and commutative, i.e., show that we have identifications
    \begin{align*}
      (m+n)+k & = m+(n+k) \\
      m+n & = n+m.
    \end{align*}
  \item Show that multiplication satisfies the following laws:
    \begin{align*}
      m\cdot 0 & = 0 & m\cdot 1 & = m & m\cdot \succN(n) & = m+m\cdot n \\
      0\cdot m & = 0 & 1\cdot m & = m & \succN(m)\cdot n & = m\cdot n+n.
    \end{align*}
  \item Show that multiplication on $\N$ is commutative:
    \begin{equation*}
      m\cdot n=n\cdot m.
    \end{equation*}
  \item Show that multiplication on $\N$ distributes over addition from the left and from the right, i.e., show that we have identifications
    \begin{align*}
      m\cdot (n+k) & = m\cdot n + m\cdot k \\
      (m+n)\cdot k & = m\cdot k + n\cdot k.
    \end{align*}
  \item Show that multiplication on $\N$ is associative:
    \begin{align*}
      (m\cdot n)\cdot k & = m\cdot (n\cdot k).
    \end{align*}
  \end{subexenum}
\exercise Consider four consecutive identifications
  \begin{equation*}
    \begin{tikzcd}
      a \arrow[r,equals,"p"] & b \arrow[r,equals,"q"] & c \arrow[r,equals,"r"] & d \arrow[r,equals,"s"] & e
    \end{tikzcd}
  \end{equation*}
  in a type $A$. In this exercise we will show that the \define{Mac Lane pentagon}\index{Mac Lane pentagon}\index{identity type!Mac Lane pentagon} for identifications commutes.
  \begin{subexenum}
  \item Construct the five identifications $\alpha_1,\ldots,\alpha_5$ in the pentagon
    \begin{equation*}
      \begin{tikzcd}[column sep=-1.5em]
        &[-2em] \ct{(\ct{(\ct{p}{q})}{r})}{s} \arrow[rr,equals,"\alpha_4"] \arrow[dl,equals,swap,"\alpha_1"] & & \ct{(\ct{p}{q})}{(\ct{r}{s})} \arrow[dr,equals,"\alpha_5"] &[-2em] \\
        \ct{(\ct{p}{(\ct{q}{r})})}{s} \arrow[drr,equals,swap,"\alpha_2"] & & & & \ct{p}{(\ct{q}{(\ct{r}{s})})}, \\
        & & \ct{p}{(\ct{(\ct{q}{r})}{s})} \arrow[urr,equals,swap,"\alpha_3"]
      \end{tikzcd}
    \end{equation*}
    where $\alpha_1$, $\alpha_2$, and $\alpha_3$ run counter-clockwise, and $\alpha_4$ and $\alpha_5$ run clockwise.
  \item Show that
    \begin{equation*}
      \ct{(\ct{\alpha_1}{\alpha_2})}{\alpha_3} = \ct{\alpha_4}{\alpha_5}.
    \end{equation*}
  \end{subexenum}
\end{exercises}

%\item In this exercise we show that the action on paths of a function preserves the groupoid-structure of a type.
%\begin{subexenum}
%\item Construct an identification
%\begin{equation*}
%\mathsf{ap.assoc}(f,p,q,r)
%\end{equation*}
%witnessing that the diagram
%\begin{equation*}
%\begin{tikzcd}[column sep=large]
%\ap{f}{\ct{(\ct{p}{q})}{r}} \arrow[r,equals,"\ap{\apfunc{f}}{\assoc(p,q,r)}"] \arrow[d,swap,equals,"{\mathsf{ap.ct}(f,%\ct{p}{q},r)}"] & \ap{f}{\ct{p}{(\ct{q}{r})}} \arrow[d,equals,"{\mathsf{ap.ct}(f,p,\ct{q}{r})}"] \\
%\ct{\ap{f}{\ct{p}{q}}}{\ap{f}{r}} \arrow[dd,equals,near start,"{\mathsf{whisk\usc{}r}(\mathsf{ap.ct}(f,p,q),\ap{f}{r})}"]   & %\ct{\ap{f}{p}}{\ap{f}{\ct{q}{r}}} \arrow[dd,equals,swap,near end,"{\mathsf{whisk\usc{}l}(\ap{f}{p},\mathsf{ap.ct}(f,q,r))}"]  %\\
%\\
%\ct{(\ct{\ap{f}{p}}{\ap{f}{q}})}{\ap{f}{r}} \arrow[r,equals,swap,"{\assoc(\ap{f}{p},\ap{f}{q},\ap{f}{r})}"yshift=-1em] & \ct{\ap{f}{p}}{(\ct{\ap{f}{q}}{\ap{f}{r}})}
%\end{tikzcd}
%\end{equation*}
%commutes.
%\end{subexenum}

\index{identity type|)}
\index{inductive type!identity type|)}
\index{inductive type|)}


% how to compile: pdflatex Template.tex. This will create a Template.pdf file.
\documentclass[11pt,fleqn]{article}
\usepackage{cite}
\usepackage{microtype}                  % improved microtypography
\usepackage[utf8]{inputenc}             % utf8 encoding
\usepackage{newtxtext}
\usepackage{newtxmath}
\gdef\ttdefault{cmtt}%
\usepackage{graphicx}                   % graphics
\usepackage{xcolor}                     % colors
\usepackage{amsmath}                    % ams math commands
\usepackage[margin=3cm]{geometry}       % page layout
\usepackage[english]{babel}             % english typographic rules
\usepackage{listings,xcolor}
\usepackage{booktabs}

%----------------------------------------------
% title page
\title{FEM Solver with Matlab}
\author{John Lian, Orginally written in April 2015}

%-------------------------------------------
\begin{document}
\maketitle                              % creates title page

\section{The exercise}

This document was part of a homework assignment originally submitted for MECH 546 - Finite Element Methods in Solid Mechanics at McGill University.
The aim of the exercise was to build the stiffness matrix of the system depicted in Figure \ref{fig:exercise}. The mesh was to be generated using FreeFem++ and imported into Matlab using the linear triangular T3 finite  elements and assuming plane strains and a standard steel. The external force stems from (1) the vertical gravity with $g$ = 1000 m/s\textsuperscript{2} and (2) a constant pressure $P = 10^4$ N/m\textsuperscript{2}.

\begin{figure}[!h]
    \centering
    \includegraphics{exercise.png}
    \caption{System of interest. Distances are expressed in meters}
    \label{fig:exercise}
\end{figure}

\section{The solver and the methods}

The system depicted was meshed in FreeFem++ with, essentially, a difference between a circle and an ellipse with a cut-off on the right. The circle has a radius of $R = 13$ and the ellipse is given by

\begin{equation}
	\left(\frac{x-12}{12}\right)^2 + \frac{y^2}{5^2} = 1,
\end{equation}

thus, the ellipse is described with $a = 12$ and $b = 5$. The mesh was implemented in FreeFem++ using

\begin{verbatim}
border aa(t=0,2*sqrt(R^2-9^2)){x=-9;y=ya-t;label=1;};
border bb(t=0,21){x=-9+t;y=-1*sqrt(R^2-x^2);label=2;};
border cc(t=0,12){x=12-t;y=-b*sqrt(1-(t/a)^2);label=3;};
border dd(t=0,12){x=t;y=b*sqrt(1-((t-12)/a)^2);label=4;};
border ee(t=0,21){x=12-t;y=sqrt(R^2-x^2);label=5;};
\end{verbatim}

A FEM solver was then implemented in MATLAB. In order to make the implementation more efficient (fewer for loops), some \emph{tricks} were employed. To begin, let $[T^e]$ be the triangle geometric matrix defined as

\begin{equation}
	[T^e] =
\left[
\begin{array}{ccc}
1 & 1 & 1 \\
x^e_1 & x^e_2 & x^e_3 \\
y^e_1 & y^e_2 & y^e_3
\end{array}
\right].
\end{equation}

Thus, the area of the triangle is given as $|T^e|$. With that, the elemental stiffness matrix is given as

\begin{equation}
	[K^e] = \dfrac{1}{4|T^e|}[B]^T[D][B]
\end{equation}

for a T3 element. It can be computed simultaneously for all indices using the fact that

\begin{equation}
\left[
\begin{array}{cc}
\frac{\partial N_1^{4Q}}{\partial x} & \frac{\partial N_1^{4Q}}{\partial y} \\
\frac{\partial N_2^{4Q}}{\partial x} & \frac{\partial N_2^{4Q}}{\partial y} \\
\frac{\partial N_3^{4Q}}{\partial x} & \frac{\partial N_3^{4Q}}{\partial y}
\end{array}
\right]
=
\left[
\begin{array}{ccc}
1 & 1 & 1 \\
x^e_1 & x^e_2 & x^e_3 \\
y^e_1 & y^e_2 & y^e_3
\end{array}
\right]^{-1}
\left[
\begin{array}{cc}
0 & 0 \\
1 & 0 \\
0 & 1
\end{array}
\right]
= \frac{1}{2|T^e|}
\left[
\begin{array}{ccc}
y_2^e-y_3^e & x_3^e-x_2^e \\
y_3^e-y_1^e & x_1^e-x_3^e \\
y_1^e-y_2^e & x_2^e-x_1^e
\end{array}
\right]
\end{equation}

Then, the assembly operation for the global stiffness matrix is given by

\begin{equation}
	[K_{ij}] = \sum_{T \in \Omega} [K^e_{ij}],
\end{equation}

where $T$ denotes T3 elements.

Using similar logic, the force vector is given by

\begin{equation}
	(F_i) = \sum_{T \in \Omega} (F_{\Omega i}^e) + \sum_{E \in \partial\Omega} (F_{\partial \Omega i}^e),
\end{equation}

where $E$ are edges of the domain.

Assuming body forces $f_{\Omega} = (f_1, f_2)$ are given at the mesh nodes, the integral can be approximated by

\begin{equation}
\int_\Omega [N^e]^T f_{\Omega} d\Omega = \frac{1}{6}
\left|
\begin{array}{ccc}
x_2^e - x_1^e & x_3^e - x_1^e \\
y_2^e - y_1^e & y_3^e - y_1^e
\end{array}
\right|
f_i(x_c,y_c), \quad j=\mod(i-1,2)+1
\end{equation}

where $(x_c,y_c)$ is the centre of mass of the triangle $T$. With the assumption on $f$,

\begin{equation}
	f_j(x_s, y_s) = (f_j(x_1, y_1) + f_j(x_2, y_2) + f_j(x_3, y_3))/3,
\end{equation}

we can compute the body force using vectorization techniques given in \cite{koko2007vectorized}.

Integrals involving Neumann conditions can be approximated using the value of $f_{\partial \Omega}$ at the centre of the edge $E$

\begin{equation}
	\int_{\partial \Omega} [N^e]^T f_{\partial \Omega} d\Omega = \frac{1}{2} |E| g_j(x_c,y_c), \quad j=\mod(i-1,2)+1
\end{equation}

with $f_{\partial\Omega} = (g_1, g_2)$. Since the pressure force in this system acts normally on the edge of the domain, the external force could not be easily generalized in MATLAB. Each border was examined separately and the pressure force was applied more or less with \emph{brute force}. Additionally, the mesh edge data needed to be extracted from the FreeFem++ .msh file deliberately. Please see the attached files.

Lastly, the nodal displacement can be solved by solving the linear system of equations like in Exercise 1.  The stresses and the von Mises stresses can be obtained from the displacement solution via

\begin{equation}
\left[
\begin{array}{c}
	\sigma_{1}^e \\
	\sigma_{2}^e \\
	\sigma_{12}^e
\end{array}
\right]
= [D][B^e](d^e)
\end{equation}

and

\begin{equation}
	\sigma_v = \sqrt{\sigma_1^2- \sigma_1\sigma_2+ \sigma_2^2+3\sigma_{12}^2}
\end{equation}

\subsection{Number of elements needed to ensure convergence in stress}

A convergence of stress was sought. After trial and error, it was determined that about 33053 elements are needed for the maximum von Mises stress value to converge. Interestingly, increasing the number of elements causes the maximum von Mises stress value to fluctuate semi-randomly, but the increased computational time for each cycle prevented further analysis to be performed. Please see Table~\ref{table:convergence} for details.

\begin{table}[h]
\centering
\begin{tabular}{@{}lllll@{}}
\toprule
Number of Elements & FreeFem++ $\sigma_v$ & MATLAB $\sigma_v$ & FreeFem++ $v$  & MATLAB $v$  \\ \midrule
220                & $4.63664\times 10^8$     &$ 4.7143\times 10^8$   & -0.0464092    & -0.0463    \\
1393               & $7.66613\times 10^8$     &$ 7.7227\times 10^8$   & -0.0505296    & -0.0506    \\
5592               & $1.10681\times 10^9$     &$ 1.1077\times 10^9$   & -0.0516523    & -0.0517    \\
12541              & $1.43700\times 10^9$     &$ 1.4367\times 10^9$   & -0.0520871    & -0.0521    \\
15018              & $1.49578\times 10^9$     &$ 1.5015\times 10^9$   & -0.0521518    & -0.0522    \\
22486              & $1.71684\times 10^9$     &$ 1.7169\times 10^9$   & -0.0521216    & -0.0521    \\
33053              & $1.82630\times 10^9$     &$ 1.8262\times 10^9$   & -0.0523231    & -0.0523    \\
\bottomrule
\end{tabular}
\caption{Relationship between number of elements used and resulting stresses}
\label{table:convergence}
\end{table}

\subsection{Highest von Mises stress}

The highest von Mises stress is located at the bottom left corner of the system, as expected. Please see Figure~\ref{fig:matlab}. The point is highlighted on the figure. On the FreeFem++ result it is located at the same place. Please see Figure~\ref{fig:freefem}.

\begin{figure}[!htb]
\centering
\includegraphics[width=0.7\textwidth]{freefem.eps}
\caption{Converged von Mises stress field superimposed over deformation visualization from FreeFem++ for 33053 elements}
\label{fig:freefem}
\end{figure}

\begin{figure}[!htb]
\centering
\includegraphics[width=0.7\textwidth]{matlab.png}
\caption{Converged von Mises stress field superimposed over deformation visualization from MATLAB for 33053 elements}
\label{fig:matlab}
\end{figure}

\subsection{Largest displacement}

The largest displacement is located at the bottom right tip of the system, as expected. Please see Figure~\ref{fig:matlab}. The point is highlighted on the figure. On the FreeFem++ result it is located at the same place. Please see Figure~\ref{fig:freefem}.

\clearpage

\bibliographystyle{apalike}
\bibliography{references}

\end{document}


\documentclass[notitlepage]{simple}

\author{Matt McCarthy\\\href{mailto:matthew.mccarthy.12@cnu.edu}{matthew.mccarthy.12@cnu.edu}}
\title{Derivatives of Arctan at 0}
\date{Februrary, 2016}

\begin{document}
\maketitle

\section{Statement of Problem}

Define $f(x):=\arctan x$.
Find $f^{(n)}(0)$.

\section{Background}

For this problem we will need two theorems.
Firstly, we need Taylor's Theorem.
\begin{thm}[Taylor's Theorem on a disk in $\CC$]\label{t-thm}
	Let $f:\CC\rightarrow\CC$ be analytic on a disk of radius $r$ about $z_0$.
	Then there exists a unique power series such that
	\[
		f(z)=\sum_{k=0}^\infty \frac{f^{(k)}(z_0)}{k!}(z-z_0)^k
	\]
	for all $z$ such that $|z-z_0|<r$.
\end{thm}
Furthermore, we need an additional Taylor expansion.
\begin{thm}\label{t-exp}
	Define
	\[
		f(z):=\frac{1}{1-z}.
	\]
	Then,
	\[
		f(z)=\sum_{k=0}^\infty z^k
	\]
	for any $z\in\CC$ with $|z|<1$.
\end{thm}

\section{Solution}

Define $g(x):=f'(x)$.
We know that
\[
	g(x)=f'(x)=\frac{1}{1+x^2} = \frac{1}{1-(-x^2)}.
\]
From here we can invoke \autoref{t-exp} to get
\[
	g(x) = \sum_{n=0}^\infty (-x^2)^n = \sum_{n=0}^\infty (-1)^n (x-0)^{2n}.
\]
Furthermore, we know that this power series representation is unique by \autoref{t-thm}.
Thus,
\[
	g(x)=\sum_{n=0}^\infty \frac{g^{(n)}(0)}{n!}(x-0)^n=\sum_{n=0}^\infty (-1)^n (x-0)^{2n}.
\]
Using \autoref{t-thm}, we notice that $g^{(2n+1)}(0)=0$ since no odd terms appead in the Taylor expansion.
Moreover, by \autoref{t-thm} we get
\[
	\frac{g^{(2n)}(0)}{(2n)!}=(-1)^n.
\]
Therefore, $g^{(2n)}(0)=(-1)^n(2n)!$.
However, by definition of $g=f'$, we have $f^{(2n+2)}(0)=0$ and $f^{(2n+1)}(0)=(-1)^n(2n)!$.
\end{document}