%!TEX root = main.tex
\chapter{Filters}
\label{chap:filters}

\begin{figure}[H]
	\begin{center}
		\includegraphics[width = 14cm]{img/Narciss_Caravaggio.jpg}
		\caption{Caravaggio, Narziss. ad Feedback: Echo liebt Narziss etc.}
		\label{fig:narziss}
	\end{center}
\end{figure}

\begin{center}
\begin{figure}[h!]
\tikzset{concept/.append style={fill={none}}}
\begin{tikzpicture}
  \path[mindmap,concept color=black,text=black]
    node[concept] {Filters}
    [clockwise from=0]
    child[concept color=red!50!black] {
      node[concept] {Systems}
      % }
      [clockwise from=90]
      child { node[concept] {Impulse Response} }
      child { node[concept] {Difference Equation} }
      child { node[concept] {Transfer Function} }
      % child { node[concept] {pro\-gramming languages} }
      % child { node[concept] {software engineer\-ing} }
    }
    child[concept color=blue] {
      node[concept] (IIR) {IIR}
      [clockwise from=-30]
    }
    child[concept color=red] { node[concept] (FIR){FIR} }
    child[concept color=orange] { node[concept] (sd) {Sound design Challenge} };

\begin{pgfonlayer}{background}
    \draw [circle connection bar]
      (fm) edge (sd)
      (fm) edge (pm);
  \end{pgfonlayer}

\end{tikzpicture}
\caption{Lecture Contents}
\end{figure}
\end{center}

What is a filter? We use filters a lot. We often need to shape the spectrum of a signal. Be it for technical reasons (DC-Offset removal, Anti-aliasing filters, crossovers, interpolation, ...) or for aesthetic ones (EQ-ing, subtractive synthesis)\\
Please take a moment and seriously ask yourself, knowing what you know about signals, knowing what we can do with a computer: How do we actually do this? What is a digital Filter? This is what this chapter will try to explain.\\

\section{Seeing Frequency Content in the Time Domain}

Let's first try to get a feel for how signals \textit{look}. It's a lot easier to understand how a filter works afterwards. This might be obvious but let's state clearly:

\begin{framed}
	High frequency signals have a short \textit{period}. That means the signal ``moves'' fast, or a lot in short time. Therefore: The more movement is in a signal or the more fluctuation or the faster the signal changes the more high frequency content it has.
\end{framed}

Three Questions in order to make you think about signals:

\begin{question}
	Please look at the image below. Can you draw an approximation of its spectrum? What signals could have been mixed to obtain this result? To help you think about this: How would you explain the graph to somebody who doesn't see it?
% subsection raetsel (end)
\begin{figure}[H]
	\begin{center}
		\includegraphics[width = 14cm]{raetsel_original.png}
		\caption{Original Signal}
		\label{fig:originalSignal}
	\end{center}
\end{figure}
\end{question}

\begin{Answer}
You should be able to see that there is a sinusoidal component in the signal. This sinusoid has a period of 10 ms, so a frequency of 100 Hz. But also you should see that it is not a clean sine wave, there is some noisy component in there. This is what you should have been able to infer from the image. In fact it is an (attenuated) 100Hz sinusoid mixed with (attenuated) white noise.
\end{Answer}

\begin{question}
	The signal in figure \ref{fig:originalSignal} was filtered. As a result we obtained the signal in figure \ref{fig:filtered1}. What kind of filter could have been used?
	\begin{figure}[H]
	\begin{center}
		\includegraphics[width = 14cm]{raetsel_highpass.png}
		\caption{Filtered Signal 1}
		\label{fig:filtered1}
	\end{center}
	\end{figure}
\end{question}

\begin{Answer}
	It could have been a notch or stopband filter at 100 Hz or a highpass. In fact, it was a highpass.
\end{Answer}

\begin{question}
	Again, the signal in figure \ref{fig:originalSignal} (so the original signal again) was filtered. As a result we obtained the signal in figure \ref{fig:raetsel_2} . What kind of filter could have been used?
\begin{figure}[H]
	\begin{center}
		\includegraphics[width = 14cm]{raetsel_lowpass.png}
		\caption{Filtered Signal 2}
		\label{fig:raetsel_2}
	\end{center}
\end{figure}

\end{question}

\begin{Answer}
	It could have been a bandpass at 100Hz, or a lowpass. In fact, it was a lowpass. We see that the signal got a lot smoother.
\end{Answer}

\section{Ways of Describing a Filter}
There are a couple of ways to \textit{fully} describe a linear time invariant system (LTI), filters typically\footnote{filters can be augmented with non-linear or time-varying elements, for example distortion or modulation. In sound design, this is actually very common. But describing such a system mathematically is out of the scope of this work.} are such systems.\\
Below is an (incomplete) list of methods that are all able to describe a filter. They offer very different possibilities of understanding and manipulating a filter.
\begin{itemize}
	\item Difference Equation
	\item Magnitude and Phase Response
	\item Impulse Response
	\item Code (graphical, such as Max/MSP, pure data, simulink)
	\item Code (text, such as C++)
	\item Block Diagram
	\item Transfer Function (Rational Function)
	\item Unit Step Response
\end{itemize}

\begin{figure}[htb]
  \centering
  \label{fig:conversions}

  \begin{tikzpicture}[auto, thick, node distance=2.3cm, >=triangle 45]

  % \draw node at (0,0) [input] (excitation) {};
  \draw node at (0,0) [block] (de) {difference Equation};
  \draw node [block, right of=de, node distance = 8cm] (tf) {transfer function};
  \draw node [block, below of= de] (ir) {impulse Response};
  \draw node [block, below of= ir] (sr) {step Response};
  \draw node [block, below of= tf] (fr) {frequency Response};
  \draw node [block, above of= de] (bd) {block diagram};

  % \draw node [block, below of=delay, node distance = 2cm] (loss) {\Large$G(z)$};
  % \draw node at (6.,-0.001) {\textbullet};

  % \draw node [output, right of=delay,node distance = 3cm] (out) {};
  \draw[<->] (de) -- node {by inspection}(bd);
  \draw[->] (de) -- node {\Large$z transform$}(tf);
  \draw[->] (tf) -- node {evaluate on unit circle}(fr);
  \draw[->] (de) -- node {set $x[n]=\delta[n]$}(ir);
  % \draw[->] (sum1) -- node {}(delay);
  % \draw[->] (loss.west) -| node {}(sum1.south);

  % \draw[->] (6., -0.001) |- node {}(loss.east);

  \draw[->] (ir) -- node {\Large$\int x dt$}(sr);
  \draw[->] (ir) -- node {\Large$\mathcal{F} $}(fr);
  % \draw[<-] (ir) -- node {\Large$\mathcal{F^{-1}} $}(fr);

  \end{tikzpicture}
  \caption{Ways of describing LTI systems and how to get from one to the other}
\end{figure}

While in practice, the transfer function is very important, we will mostly work with block diagrams, pd programs and difference equations.\\
It is an important skill to be able to switch between these representations, for example to calculate the impulse response from a difference equation or block diagram.

\subsection{Difference Equations}
\label{sub:diff}

A difference equation is of the form mentioned in the introduction. So for example
\begin{equation}
	y(n) = x(n)+x(n-1)
	\label{eq:simple}
\end{equation}
Here, $y$ is the output $x$ is the input and $n$ denotes indexing those two signals.\\
$y(n)=x(n)$ means: take the incoming sample and make it the output. $x(n-1)$ denotes a delay by one sample. Please note that the above equation describes a \textit{system}, a filter in this case. But it can be seen as using an input array (the input signal), indexed by $n$ and creating an output array.\\
Let's visualize this idea. Given the input signal in figure \ref{fig:diffImpResp}, we can create the output using the equation.

\begin{figure}[h]
	\centering
	\includegraphics[width=\textwidth]{diffEqViz1}
	\caption[shortCaption]
	{The input and corresponding output of the equation given above.}
	\label{fig:diffImpResp}
\end{figure}
 Also it becomes obvious that the $n-1$ expression is a delay, since it makes us look up the 3rd index for creating the 4th sample at the output in this case, or $y(4) = x(4)+x(4-1)$.\footnote{The impulse in this example input signal is at position $n=3$ so the third sample. This was chosen at random and has no meaning. It could just as well sit at $n=5$.}\\
 So for more clarity, let's see all the values at all $n$s (compare this with figure \ref{fig:diffImpResp}):

\begin{center}

\begin{tabular}{l|l|l|l}
	$n$ & $y(n)$ & $x(n)$ &$x(n-1)$\\
	\hline
	0 & 0 & 0 &0\\
	1 & 0 & 0 &0\\
	2 & 0 & 0 &0\\
	3 & 1 & 1 &0\\
	4 & 1 & 0 &1\\
	5 & 0 & 0 &0\\
\end{tabular}
\end{center}

\subsection{Block Diagrams} % (fold)
\label{sub:block}

Block diagrams are nice because they are very visual and sometimes easier to understand. Also, if we work in pure data, or a different graphical programming language (such as Max/MSP or MATLAB Simulink) it's very straight forward to implement a block diagram. Translating from a block diagram to text code or difference equations or vice versa can be hard sometimes.\\
In figure \ref{fig:diffImpResp}, we can see a block diagram representation of equation \ref{eq:simple}. This \textit{is} the same thing.

% subsection subsection_name (end)

\begin{figure}[H]
  \centering
  \label{fig:simpleLPdiag}
  \begin{tikzpicture}[auto, thick, node distance=2.5cm, >=triangle 45]
  \draw node at (0,0) [input] (inp) {};
  \draw node at (2.5,-0.001) (junct) {\textbullet};
  \draw node at (4,2) [block] (delay) {\Large$z^{-1}$};
  \draw node [sum, right of=inp, node distance = 5.cm] (sum1) {\Large$+$};

  \draw node [output, right of=sum1,node distance = 3cm] (out) {};
  \draw[->] (inp) -- node {}(sum1);
  % \draw[->] (junct) |- node {}(delay);
  \draw[->] (delay.east) -| node {}(sum1.north);

  \draw[->] (2.5, -0.001) |- node {}(delay.west);

  \draw[->] (sum1) -- node {\Large$y[n]$}(out);

  \node[text width=3cm] at (1.7,0.4)
    {\Large$x[n]$};

  \end{tikzpicture}
  \caption{A very simple lowpass filter.}
  \label{fig:blockDiagr}
\end{figure}

Don't be irritated by the $z^{-1}$ block. This notation stands for a delay by one sample. A block $z^{-m}$ would stand for a delay by $m$ samples. This is a convention we need to remember. This way of notating a delay might seem unnecessarily complex, but it has its reasons.

\bgInfo{This notation, $z^{-1}$, comes from the fact that analyzing the frequency response of a system is very easy when working with complex numbers (so compounds of real and imaginary numbers). Complex numbers are often denoted by $Z$, which is another convention. Not only do we work with complex numbers, but with complex sinusoids when analyzing such systems. We imagine complex sinusoids coming into our system. A multiplication by another complex sinusoid can cause a phase shift (a delay). Dividing ($z^{-1} = \frac{1}{z}$) an incoming complex sinusoid by the complex sinusoid at the same frequency causes a phase shift that is equals to a delay by 1 sample. If you want to understand all this and stop with mickey-mouse-explanations I can recommend reading \cite{miller_puckette_theory_2006} and \cite{smith_introduction_2007}. Both of these works can be found on-line for free.\

But let's quickly calculate the frequency response, so you've seen it once:
We take our difference equation (we could also start from the block diagram):
\begin{equation}
	y(n) = x(n)+x(n-1)
\end{equation}
We then do a process called the z-transform, that is, we get rid of all the indexing. We now think about applying the process of filtering to the whole signal at once and we denote the delay by a multipliaction with a complex sinusoid that causes a phase shift:
\begin{equation}
	Y = X + X \cdot Z^{-1}
\end{equation}

We then solve the equation for $Y/X$:

\begin{equation}
	Y = X \cdot (1 + Z^{-1})
\end{equation}
\begin{equation}
	\frac{Y}{X} = 1+Z^{-1}
\end{equation}

Typically we substitute $\frac{Y}{X}$ with a new name, typically $H$ and now make this a function of $Z$.

\begin{equation}
	H(Z) = 1 + Z^{-1}
\end{equation}
This is called a system's transfer function. This is the actual standard for talking about systems. Typically we do two things with this, either, we draw a so called \textit{Pole-Zero-Plot}.
\begin{figure}[H]
	\centering
	\includegraphics[width=\textwidth]{poleZero.png}
	\caption[pole zero plot]
	{a pole zero plot. This indicates where the function $H(z)$ has zeros(where its value is zero) or poles(where its value is infinity). Actually there is also a pole at $0+0i$ which is not shown here. This is because a pole at this position has no frequency dependent effect.}
	\label{fig:poleZero}
\end{figure}

We can also evaluate the function $H(z)$for complex sinusoids.\\
The absolute value of $H$ is the output amplitude, if we just plot this value for all complex numbers we see this:

\begin{figure}[H]
	\centering
	\includegraphics[width=\textwidth]{poleZero3dPlot.png}
	\caption[poles and zeros, 3D in the complex plane]
	{Evaluation of the Transfer function in the complex plane. The unit circle is visible as a dotted line. This plot is not too easy to read but if you look at the coordinate $-1+0i$, you can see that the value there is very low. This seems to indicate a lowpass. You can basically ignore the huge mountain in the middle. It sits at $0+0i$ and has therefore equal effect on all frequencies.}
	\label{fig:poleZero3d}
\end{figure}

It's OK if this plot doesn't tell you much. The really important thing is to evaluate this surface using complex sinusoids:
$cos(\omega) + i\cdot sin(\omega)$. This is the same as looking at the values along the dotted line. We can now plot the magnitude response over $\omega$. So we effectively evaluate the formula
\begin{equation}
	|H(\omega)| = |1 + (cos(\omega)+i \cdot sin(\omega))^{-1}|
\end{equation}
Where $\omega$ ranges from $0$ to $\pi$(sic!). This is the same as going along the dotted line, starting at $1+0i$ (lowest frequency) and ending at $-1+0i$ (the highest frequency). Then we finally get the magnitude response:

\begin{figure}[H]
	\centering
	\includegraphics[width=\textwidth]{magResp.png}
	\caption[Filter magnitude response]
	{Magnitude response, so $|H(\omega)| = |1 + (cos(\omega)+i \cdot sin(\omega))^{-1}|$}
	\label{fig:magResp}
\end{figure}

}

\subsection{pd Code} % (fold)
\label{sub:pd_code}
If we program the filter in pd (or any other programming language) we obviously also fully defined the filter. Try to build the filter from above in pd! Start from the difference equation or the block diagram, what ever is harder to understand for you.\\
In figure \ref{fig:pdSimpleFir} you can see the translation to pd.

\begin{figure}[H]
	\centering
	\includegraphics{simpleFir}
	\caption[FIR filter in pd]
	{A simple filter in pure data.}
	\label{fig:pdSimpleFir}
\end{figure}

% subsection pd_code (end)

\subsection{Impulse Response}

An impulse has the advantage that it contains all frequencies. It has a flat spectrum.  So recording how a filter reacts to the impulse gives us the magnitude of the spectrum at all frequencies.\\
We can see the impulse response of the filter we are describing in this section in  multiple ways in figure \ref{fig:diffImpResp}.

\bgInfo{
\subsection{Text Oriented Code} % (fold)
\label{sub:tcode}
Below, we can see a code implementation in the language MATLAB.

\lstinputlisting[label=code,
captionpos=b,
caption=A script that creates an impulse implements a vectorized filter and plots the result.]{code/FIRvectorized.m}
}

\section{Ways of Getting an Intuitive Understanding}

So now we learned some ways to define a filter, to talk about a filter. But what is this filter actually doing? It is a very simple lowpass filter. It cuts away high frequencies. But why is adding a signal to itself but one sample delayed making a lowpass? This certainly is a bit surprising, so let's try to understand what's happening.

\subsection{Combfilter to Lowpass}
First let's view the problem from another angle. We know what this structure from above is: It's a combfilter, right? It's taking a signal and adding a delayed version of it. Let's quickly review what a combfilter is:
The combfilter effect can come up if we record something with a microphone and we get the direct signal and a delayed (e.g. via a reflection) signal also. These two signals mix together and this is what we get in our recording.\\
The result is that some frequencies cancel out.\\
We can simply simulate this situation in Max using a delay and an addition.

\begin{figure}[H]
	\centering
	\includegraphics{simpleComb}
	\caption[simple comb filter]
	{A very simple comb filter}
	\label{fig:simpleComb}
\end{figure}

This canceling out of frequencies \footnote{also called destructive interference} can be imagined if we look at figure \ref{fig:destIntereference}.
\begin{figure}[H]
	\centering
	\includegraphics[width=\textwidth]{destInterference}
	\caption[Destructive Interference]
	{Destructive Interference. The two waves would cancel out completely if mixed together(=added).}
	\label{fig:destIntereference}
\end{figure}

Please note that the signal is a sine wave with a frequency of 100 Hz, so a period of 10 milliseconds. The delay is set to be 5 ms, so half of the period of the input signal in this case. It it was set to be equals the period, the two waves would \textit{interfere constructively}, so we would get out a higher amplitude. \\

\important{In a combfilter, the first frequency that cancels out is the one whose period is double the delay time. Or, to put it differently
\begin{equation}
	f_c = \frac{1}{2d}
\end{equation}
If $f_c$ is the first frequency that cancels out and d is the delay time in seconds. \\
How to calculate this fast, if you are given the delay time:
just calculate the frequency for the given delay, and then use half of that. For example, if given delay 1ms, the corresponding frequency would be 1000 Hz. Half of that (=double the period) is 500 Hz, and that's where the first trough in the spectrum would be.

}

In figure \ref{fig:combToLowpass} we can see the \textit{frequency response}\footnote{The impulse response is recorded a couple of times and the output's spectrum has been analyzed} of our combfilter. We see a couple of plots, it's always the same filter but the delay is different. We see that for delay times >1 sample, we observe our typical comb pattern. But at Delay = 1 sample, there is just a ramp left, leaving us with a lowpass.

\begin{figure}[H]
	\centering
	\includegraphics[width=\textwidth]{FromCombfilterToLowpass}
	\caption[From comb filter to lowpass]
	{Different amounts of delays result in different magnitude responses. The extreme case of a comb filter, a delay of 1 sample, is looking like a lowpass.}
	\label{fig:combToLowpass}
\end{figure}

\bgInfo{
	We can also calculate what's happening with our equation from above:
	The delay is $1/f_s$, so 1 over samplerate. This is called the sampling intervall, let's call it $I_s$, just to get rid of the fraction. The frequency that cancels out is then $1/2I$. By inspection we find that this means the frequency that has double the wavelength of the sampling frequency, so half the sampling frequency, so \textit{Nyquist}.
}

\subsection{Prolonging an Impulse}
We can also take a different route to understand why this is a lowpass. Let's recall what the spectrum of an impulse looks like and what the spectrum of DC offset looks like. Taking a look at figure \ref{fig:impToDc}, we see an impulse, the impulse response of our filter and a DC-Offset signal. Recalling from the introduction, and looking at the figure, we know that the DC-offset signal contains only energy at 0Hz. Now look at the time domain signals: Isn't making our impulse signal last for one sample longer a small step towards generating a DC-offset signal?

\begin{figure}[H]
	\centering
	\includegraphics[width=\textwidth]{impulseToDC}
	\caption[Spectrum Impulse, Lowpass, DC]
	{Impulse, impulse response and DC offset signal and their spectra. Note that there are very few samples here and the DC-Offset signal's spectrum seems to ramp down from 0Hz. In fact, there is a single high value at 0Hz.}
	\label{fig:impToDc}
\end{figure}

\subsection{A moving Average}
How is \textit{average} defined? There are a lot of different ways to compute an average, but typically we mean the \textit{arithmetic mean}, so adding all numbers and dividing by the count of items:
\begin{equation}
	A = \frac{1}{N} \sum_{n=1}^N x(n)
\end{equation}

Looking back at our filter, we will notice that we are adding up the current sample and the last sample. If we were to divide that sum by two (so the number of elements, since it's two samples we are adding up), we would arrive at the definition above. So we could say, that our lowpass is always outputting the average of the current and the last sample. It is therefore called a moving average filter. \\
Taking the average of something always reduces information and we get something that is fluctuating less than its input. If we would make a website or something that would always show the average temperature in Vienna of the last two weeks, this display wouldn't start jumping around if there was a single hot day in winter. It would remove high frequency data (jumps). It would be some kind of lowpass filter.

\subsection{A sine at Nyquist}
Here is yet another way of understanding why the structure we saw is a lowpass. Let's imagine a sine wave at Nyquist frequency, the highest frequency we can work with. We can see what such a sine looks like in Figure~\ref{fig:nyqSine}. If we apply this moving average filter, so that lowpass we have been talking about, what do you think comes out? Go ahead and calculate the average for each sample and its previous sample. We add $1 + -1 = 0$, $-1 + 1 = 0$, $1 + -1 = 0$, ... So you can see, if we feed a sine at nyquist into our lowpass, what we get is silence.

\begin{figure}[H]
	\centering
	\includegraphics[width=\textwidth]{sineWaveNyq.png}
	\caption[Sine wave at nyquist frequency]
	{Sine wave at nyquist frequency}
	\label{fig:nyqSine}
\end{figure}

\subsection{Highpass} % (fold)
\label{sub:Highpass}

You should already be familiar with the basic types of filters, so lowpass, highpass, bandpass etc. We now saw how we can build a lowpass filter, but what about a highpass, what about notching, peak, shelving filters? This is getting pretty involved quickly, but let's have a look at a highpass. Highpass filtering is the opposite of lowpass filtering. It emphasizes movement in the signal, since everything that moves fast must contain high frequencies. One simple form of a highpass is taking the difference between the current and the last sample, so:

\begin{figure}[H]
  \centering
  \label{fig:sipleHP}
  \begin{tikzpicture}[auto, thick, node distance=2.5cm, >=triangle 45]
  \draw node at (0,0) [input] (inp) {};
  \draw node at (2.5,-0.001) (junct) {\textbullet};
  \draw node at (4,2) [block] (delay) {\Large$z^{-1}$};
  \draw node [sum, right of=inp, node distance = 5.cm] (sum1) {\Large$+$};

  \draw node [output, right of=sum1,node distance = 3cm] (out) {};
  \draw[->] (inp) -- node {}(sum1);
  % \draw[->] (junct) |- node {}(delay);
  \draw[->] (delay.east) -| node {}(sum1.north);

  \draw[->] (2.5, -0.001) |- node {}(delay.west);

  \draw[->] (sum1) -- node {\Large$y[n]$}(out);

  \node[text width=3cm] at (1.7,0.4)
    {\Large$x[n]$};

\node[text width=3cm] at (6.7,0.9)
    {\Large$-$};

  \end{tikzpicture}
  \caption{A very simple highpass filter. Please compare to \ref{fig:blockDiagr}. }
  \label{fig:blockDiagrHigh}
\end{figure}

You will notice that figure \ref{fig:blockDiagrHigh} and figure \ref{fig:blockDiagr} are identical with oly one little exception. In this highpass filter we \textbf{subtract} the delayed signal from the input. This is denoted by the little $-$ sign next to one line leading to the addition. One might think, ``Why don't we just use a minus sign instead of the addition sign?'' Because order matters with subtraction, it makes a difference if we subtract 3 from 5 or 5 from 3.\\
The structure above is also called a differentiator, since it is a way of approximating the derivative of a signal.

% subsection filter_types (end)

\subsection{Video Filters}

\video{
	Filters in video work the same way and are as common as in audio. But in video we have another question to answer before we start filtering: Does our operation (adding two neighboring samples) apply to time, or to space? In other words: Do we want to add neighboring pixels to produce an output pixel or do we want to add whole frames over time to produce an output frame?\\
	Have a look at figure \ref{fig:v_origianl}. We will take this still from a video as a reference. We send the video through a couple of filters (one at a time, not in series) and watch the output. \\
	For each of the output frames, try to think about how this could have been generated from the input, what kind of a filter could have been used!?

	\begin{figure}[H]
		\begin{center}
			\includegraphics[width = 14cm]{vidFilters_o.png}
			\caption[Video Filters: Original]{Frame from the original video}
			\label{fig:v_origianl}
		\end{center}
	\end{figure}

\begin{figure}[H]
	\begin{center}
		\includegraphics[width = 14cm]{vidFiltersFIRs.png}
		\caption[Video Filters: Blur]{This effect is known as a \textit{blur} It is generalted by using a FIR lowpass in the space domain. That means, in the simplest case, each output pixel is the average of the input pixel at the same location and its neighbors. In practice, weighting functions are used to control, how much the input pixel and how much its neighbors contribute. These weighting functions are called kernels, (since this FIR filtering is the same as convolution). Typical kernels are: Gaussian, Box, the sinc function, etc. }
		\label{fig:v_blur}
	\end{center}
\end{figure}

\begin{figure}[H]
	\begin{center}
		\includegraphics[width = 14cm]{vidFilters_IIRt.png}
		\caption[Video Filters: IIR Lowpass, time domain]
		{Here we can see a kind of lowpass (IIR) in the time domain. The current input-frame and a mixture of past (output-)frames are added up to generate this output frame.}
		\label{fig:v_TLP}
	\end{center}
\end{figure}

\begin{figure}[H]
	\begin{center}
		\includegraphics[width = 14cm]{vidFiltersHPFIRs.png}
		\caption[Video Filters: FIR Highpass, space domain]
		{We can hardly see anything here, but if you look closely, edges from the original are still visible. What happened here is a FIR filtering in the space domain, so the opposite of blurring: Edge detection.}
		\label{fig:vidFiltHP}
	\end{center}
\end{figure}

\begin{figure}[H]
	\begin{center}
		\includegraphics[width = 14cm]{vidFiltersHPFIRt.png}
		\caption[Video Filters: FIR Highpass, time domain]
		{What we can see here is another highpass, but in the time domain. It computes the difference between the current and the last frame.}
		\label{fig:vidFilHPt}
	\end{center}
\end{figure}

}

\section{Filters in Max/MSP}
Please note that what we did so far was filtering signals, but we \textbf{built} the filters with very low level operations (Addition, multiplication and time shift = delay). This was in order to understand how filters work. Sometimes we need to build a filter, but often we can just \textit{use} filters, which are built in to Max or any other environment.\\
\subsection{Lowpass}
In pd, we can use \pd{lop\textasciitilde}, \pd{onepole\textasciitilde}, \pd{lores\textasciitilde} and \pd{vcf\textasciitilde}. \pd{lores\textasciitilde } has a resonance parameter. This emphasizes frequencies at the cutoff frequency of the filter. \pd{vcf\textasciitilde} also has this parameter and an additional bandpass output.

\subsection{Highpass}
There is \pd{hip\textasciitilde}, which is a highpass filter. But we can just use a structure that subtracts a lowpassed version from our original signal also. For example:
\begin{figure}[H]
	\begin{center}
		\includegraphics{pdhighpass.png}
		\caption{Highpass filter}
		\label{fig:pdHP}
	\end{center}
\end{figure}

\subsection{Bandpass} % (fold)
There are two bandpass filters in Max, \pd{reson\textasciitilde} and \pd{svf\textasciitilde}. Additionally \pd{biquad\textasciitilde} can be used in bandpass mode.
We can also make our own custom bandpass using a lowpass and a highpass in series:
\begin{figure}[H]
	\centering
	\includegraphics{bandpass}
	\caption[bandpass]
	{Bandpass Filter in pd}
	\label{fig:filterInPd}
\end{figure}

% subsection subsection_name (end)

More basic building blocks that can be used to realize a filter are \pd{rpole\textasciitilde }, \pd{rzero\textasciitilde }, \pd{cpole\textasciitilde }. \pd{czero\textasciitilde }. \pd{biquad\textasciitilde } is a very powerful filter that allows more or less arbitrary responses.\\

\section{Types of Digital Filters}

It is important to understand that there are two types of filters\footnote{We are going to ignore that one can also filter by using FFT approaches}.
\begin{itemize}
	\item Finite Impulse Response (FIR) Filters
	\item Infinite Impulse Response (IIR) Filters
\end{itemize}

We have been dealing with a FIR filter so far. An infinite impulse response filter does not have an infinite impulse response in practice, but it \textit{can} have one. How is this achieved? By using feedback. A FIR filter does not have feedback, an IIR filter does.

\begin{figure}[htb]
  \centering
  \label{fig:IIRlowpass}

  \begin{tikzpicture}[auto, thick, node distance=2.3cm, >=triangle 45]

  \draw node at (0,0) [input] (excitation) {};
  \draw node [sum, right of=excitation, node distance = 2.cm] (sum1) {\Large$+$};
  \draw node at (5,-2) [block ] (delay) {\Large$z^{-1}$};
  \draw node [mult,shape border rotate=180, left of=delay, node distance = 2cm] (loss) {$0.5$};
  \draw node at (6.,-0.001) {\textbullet};

  \draw node [output, right of=sum1,node distance = 5cm] (out) {};
  \draw[->] (excitation) -- node {\Large$x[n]$}(sum1);
  % \draw[->] (sum1) -- node {}(6., -0.001);
  \draw[->] (loss.west) -| node {}(sum1.south);

  \draw[->] (6., -0.001) |- node {}(delay.east);
  \draw[->] (delay) -- node {}(loss);

  \draw[->] (sum1) -- node {\Large$y[n]$}(out);

  \end{tikzpicture}
  \caption{A simple IIR filter. Note that the arrows are circling, there is feedback involved.}
\end{figure}

If we look at the impulse Response, we can see that it is  ``longer'' than what we had before. If you look closely, we can see that the output sample at which the impulse arrives(n=3), just has the value of the input(1). After that, we can see that the next value is always the sample before times 0.5, so the impulse respopnse is a \textit{geometric series}: $\{1,0.5,0.25,0.125,..\}$ or $\{1, \frac{1}{2}, \frac{1}{4}, \frac{1}{8}, ...\}$. This is the case since the coefficient in the feedback path is 0.5, so $\frac{1}{2}$.

\begin{figure}[H]
	\centering
	\includegraphics[width=\textwidth]{IIR1}
	\caption[Impulse Response of $y(n)=x(n)+\frac{1}{2}y(n-1)$]
	{Impulse Response of $y(n)=x(n)+\frac{1}{2}y(n-1)$}
	\label{fig:iir2}
\end{figure}

Let's translate the above diagram to a difference equation, so we see another representation of the same system.
\begin{equation}
 	y(n) = x(n)+y(n-1) \cdot 0.5
 \end{equation}

Finally, let's quickly summarize some of the differences between the two filter types:
\begin{center}

\begin{tabular}{l|l|l}

& \textbf{IIR} & \textbf{FIR} \\
\hline
stability & possibly unstable & always stable \\
Real time Efficiency & Very efficient & inefficient \\
topology & Feedback & no feedback \\

\end{tabular}
\end{center}

\subsection{IIR Filters in Max}
There are several ways to use IIR filters in Max (\pd{biquad\textasciitilde}, \pd{onepole\textasciitilde}, \pd{cascade\textasciitilde}, ... ). If we want to \textit{create} an IIR filter from scratch, we should use \pd{gen\textasciitilde}. After creating a \pd{gen\textasciitilde} object, we can double click it to edit it. In there we are allowed to do signal processing on a sample-by-sample level. This is necessary in order to use feedback with a single sample delay. A simple IIR lowpass filter would look like depicted in Figure \ref{fig:iirGen}.

\begin{figure}[H]
	\centering
	\includegraphics{img/genIIR.PNG}
	\caption[IIR lowpass filter made in gen.]
	{IIR lowpass filter made in gen. $y(n)=x(n)+0.99 \cdot y(n-1)$}
	\label{fig:iirGen}
\end{figure}

% \section{Convolution}
% Convolution is a very important concept in DSP. We already encountered it in chapter \ref{Modulation}. We know that convolution reverbs use impulse responses of rooms to simulate these rooms. But how is this done? It \textit{can} be done using a FIR filter.

\section{Key Points}
\begin{itemize}
	\item Make sure you understood the differences between a FIR and an IIR filter.
	\item Make sure you can make an educated guess what kind of a filter we see if you encounter a block diagram.
	\item Make sure you can do simple translations between Max, blockdiagrams and difference equations.
	\item Make sure you know when a combfilter turns into a lowpass.
	\item Make sure you know and understand the formula to calculate the first phase cancellation in a combfilter.
\end{itemize}

\section{Homework}

Build the structure depicted in Figure~\ref{fig:genFx} in Max/MSP.

\begin{figure}[H]
	\centering
	\includegraphics[width=\textwidth]{img/effect.png}
	\caption[Generalized Effects Structure proposed by Dattoro]
	{Effects Structure proposed by Dattoro}
	\label{fig:genFx}
\end{figure}

% Notizen aus erster UE:\\
% Viellt. mit IR anfangen?
% Vielleicht davor noch: das beispiel mit noise + sinus.

% IR, bypass system, hall system.

% garnicht faltung. Moving average zu long FIR.
% Impuls antworten anhand beispielen besprechen.

% \begin{enumerate}
% 	\item FIR!

% kammfilter(FF)

% dann FIR kammfilter

% chorus
% flanger

% LFO

% ->index/tiefe, offset, frequenz wdh.

% -frequenzgang
% ausl\"oschungen, rechnen..

% \item
% FIR one sample delay\\

% FIR bauen, vllt. an tafel blockdiagramm.\\

% FIR hipass\\

% \item

% mehrere Samples delay
% IR
% differenzen gleichung

% eventuell Faltung(faltungs hall connection herstellen)

% faltung als multiplikation d. spektra
% multiplikation als faltung d spektra.

% M4L faltungs hall herzeigen

% LTI?

% differentiator, akkumulator?

% \item
% IIR\\

% notizen POlyphonie
% was separat, was einmal

% \end{enumerate}

% \section{einfuehrung} % (fold)
% \label{sub:einfuehrung}

% Ziel der LV:\\
% \begin{enumerate}
% 	\item Gefühl dafür bekommen was die häufigsten Operationen der DSV eigentlich tun.
% 	\item Gefühl dafür bekommen, dass es günstig ist, zwischen verschiedenen repräsentationen hin und her zu wechseln (time und frequency domain, aber auch system repräsentationen)
% 	\item Sich in professioneller literatur(DSP lehrbücher) nicht völlig verloren fühlen.
% 	\item schönheit zu sehen dass alles das \glqq{}gleiche\grqq{} ist.(Delay, filter, reverb, chorus, flanger etc)
% \end{enumerate}

% Wozu?:\\
% \begin{enumerate}
% 	\item Wir arbeiten ständig mit Operationen der DSV. Filter sind nicht nur in synthesizern. Ein tieferes verständnis erleichtert die arbeit. Wie funktioniert zum beispiel ein blur?
% 	\item Was ist ein FeedForward compressor?
% \end{enumerate}

% Nachdenken über filter allgemein. Was ist ein filter im gespür d. studenten. \\
% Bekannt was ein LTI system ist?\\

% \newpage
% -Wie könnte ein filter realisiert werden?\\
% -Ein digitales signal ist eine reihe von zahlen, eine sequenz von werten. (über bildliche darstellungen sprechen)\\
% -Echtzeit fall: ich bekomme in ein system ein signal(also ständig werte) und soll ein zb. lowpass gefilteretes signal ausgeben.\\
% \textbf{Was kann ich mit dem signal machen? Zum beispiel addieren, multiplizieren, verzögern.}\\

% Einfacher moving average. Zunächst nur an der tafel!
% Impulsantowrt ausrechnen!
% Was ist ein impuls.\\
% Was ist eine impulsantwort? Beispiele.. untit impulse ( = dirac impuls, dirac gunktion, delta function)\\

% Diagramm aufmalen:

% impuls \(\rightarrow\) Raum \(\rightarrow\) Impulsantwort\\
% oderr \\
% impuls \(\rightarrow\) beliebiges system \(\rightarrow\) Impulsantwort\\
% \textbf{anwendugsbeispiel:} tontechnik bei okto; mischpult ausmessen, dbmax ausmessen(fragwürdige sache! Wieso?)

% \textbf{FRAGE}\\
% Ein system(zB. ein Filter) hat folgende Eigenschft:\\
% Wenn zwei signale getrennt (d.h. unabhängig voneinander, zB. nacheinander.) in das system geschickt werden, und die Ergebnisse summiert werden, entsteht das signal \(P\). Wenn andererseits die signale zuvor summiert werden und dann in das system geschickt werden entsteht ebenfalls \(P\). Welche gleichung etspricht einer beschreibung dieser eigenschaft?

% \begin{enumerate}
% 	\item \(f(x)+f(y) = f (x * y) = P\)
% 	\item \(f(x)+f(y) = f (x + y) = P\)
% 	\item \(f(x)+f(x) = f (x + y) = P\)
% 	\item \(f(x)+f(y) = f (x + x) = P\)
% 	\item nichts von alledem.
% \end{enumerate}

% \newpage
% richtig : nr.2 \\
% \\

% Erklärung eines LTI systems:

% \begin{itemize}
% 	\item Linear:
% 		\begin{itemize}
% 			\item Superpositionsprinzip \(f(x)+f(y) = f (x + y)\)
% 			\item Homogenität \(a*f(x) = f(a*x)\)
% 		\end{itemize}
% 	\item Time Invariant (zeit-invariant)
% \end{itemize}

% Zurück zu filter:
% was glauben sie wie er funktioniert. \\
% \\

% \section {kammfilter}

% Wiederum fragen was sich die studenten vorstellen.\\

% \glqq{}Wir bauen ein delay.\grqq{}
% nicht vergessen: block~, subpatcher.

% \begin{figure}[h]
% 	\begin{center}
% 		\includegraphics[width = 14cm]{simpleDelay.png}
% 		\caption{SimpleDelay}
% 		\label{fig:simpleDelay}
% 	\end{center}
% \end{figure}

% Studenten sollen ausprobieren. Verschiedene inputs f. kammfilter. \\

% Modulation des kammfilters. Studenten sollen LFO an den kammfilter dran bauen.\\
% \(\rightarrow\) Flager \\
% \(\rightarrow\) Chorus \\

% \textbf{index/tiefe, offset, frequenz wiederholen.}

% \begin{figure}[h]
% 	\begin{center}
% 		\includegraphics[width = 14cm]{generalizedEffect.png}
% 		\caption{generalized Effect structure}
% 		\label{fig:genEffect}
% 	\end{center}
% \end{figure}

% über name sprechen, sowohl spectrum als auch Impulsantwort(IIR variante) sind kammförmig. \\
% Eigentlich einfach ein delay, ein delay mit feedback im falle von IIR.\\

% \begin{figure}[h]
% 	\begin{center}
% 		\includegraphics[width = 14cm]{img/combFIR_patch.png}
% 		\caption{Der Patch 01\_combFilter.pd}
% 		\label{fig:01_combFilter}
% 	\end{center}
% \end{figure}

% Zunächst nicht wichtig die versch. representationen zu verstehen. \\
% Patch representation kurz durchbesprechen. Sollte verständlich sein.\\

% Unterschiedliche representationen durchbesprechen.
% Fragen, abstimmen?, was die studenten für die angenehmste representation halten.

% \begin{itemize}
% 	\item Patch: vorteile: interaktiv, allgemein, reinfolge der events nachvollziehbar. Nachteil: nicht sehr übersichtlich. Nicht sehr konzis, nicht kompakt.
% 	\item Block Diagramm: übersichtlich, allgemein. Reinfolge der events klar, pfeile! Fluss, richtung, eindeutig. Nachteile: Nicht kompakt.
% 	\item differenzen gleichung: Vorteil: kompakt, konzis. Nachteil: reinfolge der events nicht klar ersichtlich: kein rezept, eher eine beschreibung. (nicht imperativ sondern deklarativ, va. im Fall von Feedback)
% 	\item Impulsantwort: \textbf{Frage: was ist ein Impuls?}  Vor/Nachteil: Nicht allgemein, nur ein spezieller zustand des systems beschrieben. Nicht kompakt, aber kann sehr intuitiv sein. Beispiele an tafel: IR von bypass-system, IR mit viel hall, IR von bandpass filter mit hoher resonanz, eventuell: differentiator, accumulator.
% 	\item andere representationen: text-code, transfer funktion, frequency response(magnitude + phase response/group delay)
% \end{itemize}

% \newpage

% \section {Moving Average}

% \textbf{Wo hat der kammfilter immer sein erstes Tal im spectrum?}
% Sinus an tafel malen.
% Antwort:
% bei

% \(\lambda =  (2* \Delta t) \)
% (wobei \(\Delta t\) die delay zweit in sekunden. und \(\lambda\)  die periodendauer der gesuchten frequenz.) Daher:\\
% \(
% f_c = 1/(2* \Delta t)
% \)

% Nun wird das delay auf ein sample reduziert. Ein lowpass entsteht der seine grenzfrequenz bei sr/2 hat.\\

% Frage: was macht ein lowpass filter in der timedomain?

% \begin{figure}[h]
% 	\begin{center}
% 		\includegraphics[width = 14cm]{movingAverage.png}
% 		\caption{movingAverage}
% 		\label{fig:movingAverage}
% 	\end{center}
% \end{figure}

% \textbf{EXKURS: VIDEO FILTER, lowpass, time domain erkennen.}

% \newpage
% \textbf{Frage:} Vergleiche original und die gefilterte variante (01.mov) . Was für ein filter könnte angewandt worden sein:
% \begin{enumerate}
%  	\item lowpass
%  	\item bandpass
%  	\item hipass
%  	\item notch
%  	\item nichts von alledem
%  \end{enumerate}

% wie könnte ein highpass realisiert werden? Wie schaut der effekt eines highpass filters in der frequenz- und wie in der timedomain aus?

% \section{convolution, Faltung}

% Video (unten) zeigen, vorher erklären: \\

% conv theorem.

% http://youtu.be/\_vyke3vF4Nk?t=25m16s \\
% start bei min 25.

% \textbf{maxpatch}

% %%--------------------------------------------------------------------------
% %% IIR/FIR
% %%--------------------------------------------------------------------------
% \section {IIR/FIR}

% Je steiler die Filterflanke sein, je komplexer der filter sein soll, desto mehr delays werden benötigt.\\

% Wieso: einfache Erklärung: Um aus einem signal, das alle frequenzen enthält (zB dirac impuls) ein signal zu machen, das hauptsächlich sehr tiefe frequenzen enthält wird ein system benötigt das \glqq{}lange wellen\grqq{} zu produzieren im stande ist.

% An tafel zeichnen: dirac impuls und unit step/ DC. Frage nach spectrum.\\

% frage nach FIR der diesen IR haben könnte.

% \textbf{Patch: lomgSimpleFir}

% Grenzfall: integrator macht aus unit impulse, \(delta [n]\), unit step signal\(u[n]\)(DC).

% Integrator an tafel malen.\\

% Daher:
% Es kann gezeigt werden dass ein feedback pfad einer unendlichen menge an delays gleichkommt, siehe IR.

% \textbf{Patch: 02\_combFilterIIR}

% nebenbei:
% linear phase = symmetrischer IR, immer FIR, daher weniger performant.
% Linear phase filter sind notwendig wenn die timedomain wellenform möglichst unbeeinträchtigt bleiben soll.

% \subsection{Onepole} % (fold)
% \label{ssub:onepole}

% % subsubsection subsubsection_name (end)
% onepole an tafel beschreiben. Block diagramm, malen, nach differenzengleichung fragen. Patch herzeigen.\\

% \textbf{patch: IIRtest} \\
% danach:\\
% \textbf{patch: IIRtest2} \\

% Fragwürdig aber falls interesse:
% onepole coeff berechnung zB:\\

% \begin{equation}
% a_0 = \frac{2 \pi f_c} {sr}
% \end{equation}

% oder

% \begin{equation}
% a_0 = sin(\frac{2 \pi f_c} {sr})
% \end{equation}

% \section{Hausübung}

% hausübung: Baue einen einen chorus. TESTSIGNAL AUSBESSERN! nicht noise!!


\documentclass{spec}

\usepackage{amsfonts,bm,amsmath}
\usepackage{verbatim}
\usepackage{algorithm, algpseudocode}
\usepackage{caption}

\input{nlls_header.tex}
\newcommand{\scalarinteger}[1]{\itt{int #1}}
\newcommand{\scalarreal}[1]{\itt{double #1}}
\newcommand{\scalarlogical}[1]{\itt{bool #1}}
\newcommand{\ronearrayreal}[2]{\itt{double *#1}}
\newcommand{\scalarcharacter}[2]{
  % need to advance the size by one for the c version compared to the fortran version...
  \newcount\charsize
  \charsize=#2
  \advance\charsize by 1 %
  \itt{character #1[\the\charsize]}
}
\newcommand{\mainroutine}{{\tt nlls\_solve}}
\newcommand{\onestep}{{\tt ral\_nlls\_iterate}}
\newcommand{\ct}{.}
\newcommand{\vecentry}[2]{\texttt{#1[#2]}}
\newcommand{\fortranonly}[1]{}
\newcommand{\conly}[1]{#1}
\begin{document}

\hslheader

\begin{center}
\huge \sc  C Interface
\end{center}

\hslsummary

\input{summary.tex}

%!!!!!!!!!!!!!!!!!!!!!!!!!!!!
\hslattributes
\hslversions{\versionum\ (\versiondate)}.
\hslIRDCZ Real (single, double).
\hsllanguage Fortran 2003 subset (F95+TR155581).
\hsldate January 2016.
\hslorigin The Numerical Analysis Group, Rutherford Appleton Laboratory.
\hslremark The development of this package was
partially supported by EPSRC grant EP/M025179/1.

%!!!!!!!!!!!!!!!!!!!!!!!!!!!
\newpage
\hslhowto

\subsection{Calling sequences}

Functions signatures are defined in a header file
\begin{verbatim}
   #include "ral_nlls.h"
\end{verbatim}
\medskip

\noindent The user callable subroutines are:
\vspace{-0.1cm}
\begin{description}
   \item[\texttt{ral\_nlls\_default\_options()}] initializes solver options to default values.
   \item[\texttt{nlls\_solve()}]  solves the non-linear least squares problem (Section~\ref{eq:nlls_problem}).
   \item[\texttt{ral\_nlls\_init\_workspace()}] initialises a workspace for use with \texttt{ral\_nlls\_iterate()}.
   \item[\texttt{ral\_nlls\_iterate()}] performs a single iteration of the solver algorithm.
   \item[\texttt{ral\_nlls\_free\_workspace()}] frees memory allocated by a call to \texttt{ral\_nlls\_init\_workspace()}.
\end{description}

%%%%%%%%%%%%%%%%%%%%%% derived types %%%%%%%%%%%%%%%%%%%%%%%%

\hsltypes
\label{derived types}
For each problem, the user must employ the derived types defined by the
module to declare scalars of the types {\tt struct ral\_nlls\_options}, and
{\tt ral\_nlls\_inform}.
The following pseudocode illustrates this.
\begin{verbatim}
   #include "ral_nlls.h"
   ...
   struct ral_nlls_options options;
   struct ral_nlls_inform inform;
   ...
\end{verbatim}
The members of these structs are explained
in Sections~\ref{typeoptions} and \ref{typeinform}.

%%%%%%%%%%%%%%%%%%%%%% argument lists %%%%%%%%%%%%%%%%%%%%%%%%
\hslarguments

\subsubsection{Integer and package types}
%{\tt INTEGER} denotes default {\tt INTEGER} and
%{\tt INTEGER(long)} denotes {\tt INTEGER(kind=selected\_int\_kind(18))}.
The term {\bf package type} is used to mean \texttt{float}
if the single precision version is being used and
\texttt{double} for the double precision version.

\subsubsection{To initialise members of \texttt{struct nlls\_options} to default values}

To initialise the value of \texttt{struct nlls\_options}, the user \textbf{must} make a
call to the following suboutine (failure to do so will result in undefined behaviour):
\begin{verbatim}
   void ral_nlls_default_options(struct ral_nlls_options *options);
\end{verbatim}

\begin{description}
   \itt{*options} will be initialised to default values on return.
\end{description}

\subsubsection{To solve the non-linear least squares problem}
\label{sec:solve}

To solve the non-linear least squares problem make a call of the following
subroutine:

\begin{verbatim}
   void nlls_solve( int n, int m, double X[], ral_nlls_eval_r_type eval_r,
      ral_nlls_eval_j_type eval_j, ral_nlls_eval_hf_type eval_hf,
      void* params, struct nlls_options const* options, struct nlls_inform* inform,
      double weights[])
\end{verbatim}

\begin{description}
\itt{n} holds the number $n$ of
variables to be fitted; i.e., $n$ is the length of the unknown vector $\bm x$.

\itt{m} holds the number $m$ of
data points available; i.e., $m$ is the number of functions $f_i$.
\textbf{Restriction:} \texttt{m},\texttt{n}$>$\texttt{0}

\itt{x} must hold the initial guess for $\bm x$, and on
successful exit it holds the solution to the non-linear least squares problem.

\itt{eval\_r} specifies a callback function that, given a point $\iter{\vx}$,
returns the vector $\vr(\iter{\vx})$. Details of the function signature and
requirements are are given in Section~\ref{sec::function_eval}.

\itt{eval\_j} specifies a callback function that, given a point $\iter{\vx}$,
returns the $m \times n$ Jacobian matrix, $\iter{\vJ}$, of $\vr$ at $\iter{\vx}$. Details of the function signature and requirements are are given in
Section~\ref{sec::function_eval}.

\itt{eval\_hf} is a {\tt PROCEDURE} that, given a point $\iter{\vx}$
and function $\vr(\iter{\vx})$, returns the second-order terms of the Hessian at $\iter{\vx}$.
Further details of the format required are given in Section~\ref{sec::function_eval}.

\itt{params} is a pointer to user data that is passed unaltered to the callback
functions {\tt eval\_r}, {\tt eval\_J}, and {\tt eval\_Hf}.

\itt{inform} provides information about the execution
of the subroutine, as explained in Section~\ref{typeinform}.

\itt{options} specifies options that control the execution of the subroutine,
see Section~\ref{typeoptions}.

\itt{weights} may be {\tt NULL}, otherwise it is a rank-1 array of size {\tt m}. If present, {\tt weights} holds the square-roots of the
diagonal entries of the weighting matrix, $\vW$, in (\ref{eq:nlls_problem}).  If absent, then the norm in (\ref{eq:nlls_problem}) is taken to be the 2-norm, that is, $\vW = I$.
\end{description}

\subsubsection{To initialise a workspace for use with \texttt{ral\_nlls\_iterate()}}

Prior to the first call of \texttt{ral\_nlls\_iterate()}, the workspace must be
initialised by a call to the following subroutine:
\begin{verbatim}
   void ral_nlls_init_workspace(void **workspace);
\end{verbatim}

\begin{description}
   \itt{*workspace} will, on return, be allocated and initialised using Fortran intrinsics.
      To avoid a memory leak, it must be freed through a call to \texttt{ral\_nlls\_free\_workspace()}.
\end{description}

\subsubsection{To iterate once}
\label{sec:iterate}
Alternatively, the user may step through the solution process one iteration at
a time by making a call of the following form:

\begin{verbatim}
   void ral_nlls_iterate( int n, int m, double X[], void* workspace,
      ral_nlls_eval_r_type eval_r, ral_nlls_eval_j_type eval_j, ral_nlls_eval_hf_type eval_hf,
      void* params, struct nlls_options const* options, struct nlls_inform* inform,
      double weights[])
\end{verbatim}

\begin{description}

\item[\normalfont \texttt{n}, \texttt{m}, \texttt{eval\_F}, \texttt{eval\_J}, \texttt{eval\_HF}, \texttt{params}, \texttt{options}, \texttt{inform}, and \texttt{weights}] are as described in Section~\ref{sec:solve}.

\itt{X} is an array of size {\tt n}. On the first call, it must hold the initial guess for
$\bm x$. On return it holds the value of $\bm x$ at the current iterate, and
must be passed unaltered to any subsequent call to \texttt{ral\_nlls\_iterate()}.

\itt{w} is workspace allocated and initialised through a previous call to
\texttt{ral\_nlls\_init\_workspace()}.

\end{description}

\subsubsection{To free a workspace when it is no longer required}

Memory allocated during the call to \texttt{ral\_nlls\_init\_workspace()} may be freed
by a call to the following subroutine:
\begin{verbatim}
   void ral_nlls_free_workspace(void **workspace);
\end{verbatim}

\begin{description}
   \itt{*workspace} is the workspace to be freed. On exit it will be set to \texttt{NULL}.
\end{description}

\subsection{User-supplied function evaluation routines}
\label{sec::function_eval}
In order to evaluate the function, Jacobian and Hessian at a point, the user
must supply callback functions that perform this operation that the code
{\tt ral\_nlls} will call internally.

In order to pass user-defined data into the evaluation calls, the parameter
\texttt{params} is passed unaltered to the callback functions. Typically this
will be a pointer to a user defined structure that stores the data to be fitted.

\subsubsection{For evaluating the function $\vr(\vx)$}

A subroutine must be supplied to calculate $\vr(\vx)$ for a given vector $\vx$.
It must have the following signature:
\begin{verbatim}
  int eval_r (int n, int m, void const* params, double const* x, double* r);
\end{verbatim}

\input{eval_r.tex}

\subsubsection{For evaluating the function $J = \nabla \vr(\iter{\vx})$}
A subroutine must be supplied to calculate $J = \nabla \vr(\iter{\vx})$ for a given vector $\vx$. It must have the following signature:

\begin{verbatim}
   int eval_j (int n, int m, void const* params, double const* x, double* J);
\end{verbatim}

\input{eval_j.tex}

\subsubsection{For evaluating the function $Hf = \sum_{i=1}^m r_i(\vx) \vW  \nabla^2 \vr_i(x)$}
A subroutine must be supplied to calculate $Hf = \sum_{i=1}^m (\vr)_i \nabla^2 r_i(\vx)$ for given vectors $\vx \in \mathbb{R}^n$ and $\vr \in \mathbb{R}^m$; here \((\vr)_i\) denotes the $i$th component of the vector $\vr$.
It must have the following signature

\begin{verbatim}
int eval_hf (int n, int m, void const* params, double const* x, double const* r, double* Hf);
\end{verbatim}

\input{eval_hf.tex}

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

\subsection{The options derived data type}
\label{typeoptions}

\input{options_type.tex}

\subsection{The derived data type for holding information}
\label{typeinform}

\input{info_type.tex}

%%%%%%%%%%%%%%%%%%%%%% Warning and error messages %%%%%%%%%%%%%%%%%%%%%%%%

\hslerrors

A successful return from a subroutine in the package is indicated by
{\tt inform.status} having the value zero.
A non-zero value is associated with an error message that by default will
be output on the Fortran unit {\tt inform.error}.  This string is also passed to the
calling routine in {\tt inform.error\_message}.

\input{errors.tex}

\hslgeneral

\hslrestrictions {\tt m$\ge$n$\ge$1}.

\hslmethod
\label{method}

\input{method.tex}

\hslexample

\input{example_text.tex}

\verbatiminput{../example/C/nlls_example.c}

\end{document}


\documentclass[preprint]{sigplanconf}

\usepackage{graphicx}
\usepackage{comment}
\usepackage{amsmath}
\usepackage{xspace}
\usepackage{amssymb}
\usepackage{stmaryrd}
\usepackage{proof}
\usepackage{multicol}
\usepackage[nodayofweek]{datetime}
\usepackage{etex}
\usepackage[all, cmtip]{xy}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{multicol}
\newcommand\hmmax{0} % default \newcommand\bmmax{0} % default 4
\usepackage{bm}
\usepackage{cmll}

\newcommand{\fname}[1]{\ulcorner #1 \urcorner}
\newcommand{\fconame}[1]{\llcorner #1 \lrcorner}

\newcommand{\xcomment}[2]{\textbf{#1:~\textsl{#2}}}
\newcommand{\amr}[1]{\xcomment{Amr}{#1}}
\newcommand{\roshan}[1]{\xcomment{Roshan}{#1}}

\newcommand{\asterix}[0]{*}

\newcommand{\ie}{\textit{i.e.}\xspace}
\newcommand{\eg}{\textit{e.g.}\xspace}

\newcommand{\lcal}{\ensuremath{\lambda}-calculus\xspace}
\newcommand{\G}{\ensuremath{\mathcal{G}}\xspace}

\newcommand{\code}[1]{\lstinline[basicstyle=\small]{#1}\xspace}
\newcommand{\name}[1]{\code{#1}}

\def\newblock{}

\newenvironment{floatrule}
    {\hrule width \hsize height .33pt \vspace{.5pc}}
    {\par\addvspace{.5pc}}

\newtheorem{theorem}{Theorem}[section]
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{definition}[theorem]{Definition}
\newtheorem{proposition}[theorem]{Proposition}
\newenvironment{proof}[1][Proof.]{\begin{trivlist}\item[\hskip \labelsep {\bfseries #1}]}{\end{trivlist}}

\newcommand{\arrow}[1]{\mathtt{#1}}

\newcommand{\dgm}[2][0.95]{
\begin{center}
\scalebox{#1}{
\includegraphics{diagrams/#2.pdf}
}
\end{center}
}

%subcode-inline{bnf-inline} name langRev
%! swap+ = \mathit{swap}^+
%! swap* = \mathit{swap}^*
%! dagger =  ^{\dagger}
%! assocl+ = \mathit{assocl}^+
%! assocr+ = \mathit{assocr}^+
%! assocl* = \mathit{assocl}^*
%! assocr* = \mathit{assocr}^*
%! identr* = \mathit{uniti}
%! identl* = \mathit{unite}
%! dist = \mathit{distrib}
%! factor = \mathit{factor}
%! eta = \eta
%! eps = \epsilon
%! eta+ = \eta^+
%! eps+ = \epsilon^+
%! eta* = \eta^{\times}
%! eps* = \epsilon^{\times}
%! trace+ = trace^+
%! trace* = trace^{\times}
%! ^^^ = ^{-1}
%! (o) = \circ
%! (;) = \fatsemi
%! (*) = \times
%! (+) = +
%! LeftP = L^+
%! RightP = R^+
%! LeftT = L^{\times}
%! RightT = R^{\times}
%! alpha = \alpha
%! bool = \textit{bool}
%! color = \textit{color}
%! Gr = G

%subcode-inline{bnf-inline} regex \{\{(((\}[^\}])|[^\}])*)\}\} name main include langRev
%! Gx = \Gamma^{\times}
%! G = \Gamma
%! [] = \Box
%! |-->* = \mapsto^{\asterix}
%! |-->> = \mapsto_{\ggg}
%! |--> = \mapsto
%! <--| = \mapsfrom
%! |- = \vdash
%! <><> = \approx
%! ==> = \Longrightarrow
%! <== = \Longleftarrow
%! <=> = \Longleftrightarrow
%! <-> = \leftrightarrow
%! ~> = \leadsto
%! -o+ = \multimap^{+}
%! -o* = \multimap^{\times}
%! -o = \multimap
%! ::= = &::=&
%! /= = \neq
%! @@ = \mu
%! [^ = \lceil
%! ^] = \rceil
%! forall = \forall
%! exists = \exists
%! empty = \epsilon
%! Pi = \Pi
%! Pi0 = \Pi^{o}
%! PiEE* = \Pi^{\eta\epsilon}_{*}
%! PiEE+ = \Pi^{\eta\epsilon}_{+}
%! PiEE = \Pi^{\eta\epsilon}
%! CatSet = \textbf{Set}
%! theseus = Theseus
%! sqrt(x) = \sqrt{#x}
%! surd(p,x) = \sqrt[#p]{#x}
%! inv(x) = \frac{1}{#x}
%! frac(x,y) = \frac{#x}{#y}
%! * = \times

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}

\conferenceinfo{POPL'13}{}
\CopyrightYear{}
\copyrightdata{}
\titlebanner{}
\preprintfooter{}

\title{Negative and Fractional Types}

\authorinfo{Roshan P. James}
           {Indiana University}
           {rpjames@indiana.edu}
\authorinfo{Zachary Sparks}
           {Indiana University}
           {zasparks@indiana.edu}
\authorinfo{Jacques Carette}
           {McMaster University}
           {carette@mcmaster.ca}
\authorinfo{Amr Sabry}
           {Indiana University}
           {sabry@indiana.edu}

\maketitle

\begin{abstract}

\end{abstract}

\category{D.3.1}{Formal Definitions and Theory}{}
\category{F.3.2}{Semantics of Programming Languages}{}
\category{F.3.3}{Studies of Program Constructs}{Type structure}

\terms
Languages, Theory

\keywords continuations, information flow, linear logic, logic programming,
quantum computing, reversible logic, symmetric monoidal categories, compact
closed categories.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Introduction}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{The Core Reversible Language: {{Pi}} }
\label{sec:pi}

We review our reversible language {{Pi}}: the presentation in this
section differs from the one in our previous paper~\cite{infeffects} in two
aspects. First, we add the empty type {{0}} which is necessary to express the
additive duality. Second, instead of explaining evaluation using a natural
semantics, we give a small-step operational semantics that is more
appropriate for the connections with continuations explored in this paper.

%%%%%%%%%%%%%%%%%%%%
\subsection{Syntax and Types}
\label{sec:pi-syntax}

Unlike the traditional situation with the $\lambda$-calculus, in {{Pi}},
there is a sharp distinction between data and programs.

\paragraph*{Data.} The sets of values and their types include:
%subcode{bnf} include main
% value types, b ::= 0 | 1 | b + b | b * b
% values, v ::= () | left v | right v | (v, v)
Types include the empty type {{0}}, the unit type {{1}}, sum types {{b1+b2}},
and products types {{b1*b2}}.  Values includes {{()}} which is the only value
of type {{1}}, {{left v}} and {{right v}} which inject {{v}} into a sum type,
and {{(v1,v2)}} which builds a value of product type. There are no values of
type {{0}}.

\paragraph*{Programs.} All the programs of {{Pi}} are special maps of type
{{v \rightarrow v}}. Specifically, they are witnesses to the following type
isomorphisms:
%subcode{bnf} include main
%! columnStyle = r@{\hspace{-0.5pt}}c@{\hspace{-0.5pt}}l
%zeroe :&  0 + b <-> b &: zeroi
%swap+ :&  b1 + b2 <-> b2 + b1 &: swap+
%assocl+ :&  b1 + (b2 + b3) <-> (b1 + b2) + b3 &: assocr+
%identl* :&  1 * b <-> b &: identr*
%swap* :&  b1 * b2 <-> b2 * b1 &: swap*
%assocl* :&  b1 * (b2 * b3) <-> (b1 * b2) * b3 &: assocr*
%dist0 :& 0 * b <-> 0 &: factor0
%dist :&~ (b1 + b2) * b3 <-> (b1 * b3) + (b2 * b3)~ &: factor
Each line of the above table introduces one or two combinators that witness
the isomorphism in the middle. Collectively the isomorphisms state that the
structure {{(b,+,0,*,1)}} is a \emph{commutative semiring}, i.e., that each
of {{(b,+,0)}} and {{(b,*,1)}} is a commutative monoid and that
multiplication distributes over addition. The isomorphisms are extended to
form a congruence relation by adding the following constructors that witness
equivalence and compatible closure:
%subcode{proof} include main
%@  ~
%@@ id : b <-> b
%
%@ c : b1 <-> b2
%@@ sym c : b2 <-> b1
%
%@ c1 : b1 <-> b2
%@ c2 : b2 <-> b3
%@@ c1(;)c2 : b1 <-> b3
%---
%@ c1 : b1 <-> b3
%@ c2 : b2 <-> b4
%@@ c1 (+) c2 : b1 + b2 <-> b3 + b4
%
%@ c1 : b1 <-> b3
%@ c2 : b2 <-> b4
%@@ c1 (*) c2 : b1 * b2 <-> b3 * b4

\noindent
To summarize, the syntax of {{Pi}} is given as follows.

\begin{definition}{(Syntax of {{Pi}})}
\label{def:Pi}
We collect our types, values, and combinators, to get the full language
definition.
%subcode{bnf} include main
% value types, b ::= 0 | 1 | b+b | b*b
% values, v ::= () | left v | right v | (v,v)
%
% comb.~types, t ::= b <-> b
% iso ::= zeroe | zeroi
%     &|& swap+ | assocl+ | assocr+
%     &|& identl* | identr*
%     &|& swap* | assocl* | assocr*
%     &|& dist0 | factor0 | dist | factor
% comb., c ::= iso | id | sym c | c (;) c | c (+) c | c (*) c
\end{definition}

\paragraph*{Adjoint.}
An important property of the language is that every combinator {{c}} has an
adjoint {{c{dagger}}} that reverses the action of {{c}}. This is evident by
construction for the primitive isomorphisms. For the closure combinators, the
adjoint is homomorphic except for the case of sequencing in which the order
is reversed, i.e., {{(c1 (;) c2){dagger} = (c2{dagger}) (;) (c1{dagger}) }}.

\begin{definition}[Size of a type]
\label{def:size}
The size of a type {{b}}, denoted by {{[^ b ^]}}, is a numeric value
and is defined to be:
\vspace{-20pt}
\begin{multicols}{2}
%subcode{opsem} include main
% [^ b1 + b2 ^] '= [^ b1 ^] + [^ b2 ^]
% [^ b1 * b2 ^] '= [^ b1 ^] * [^ b2 ^]

%subcode{opsem} include main
% [^ 0 ^] '= 0
% [^ 1 ^] '= 1
\end{multicols}

where {{+}} is numeric addition and {{*}} is numeric multiplication.

\end{definition}
\noindent
In the setting of {{Pi}} the \emph{size}, {{[^ b ^]}}, may be simply thought of as
the number of inhabitants (an arity) of the type. This intuition will
however become tenuous in the presence of negative and fractional
types.

%%%%%%%%%%%%%%%%%%%
\subsection{Graphical Language}

The syntactic notation above is often obscure and hard to read.
Following the tradition established for monoidal
categories~\cite{springerlink:10.1007/978-3-642-12821-94}, we present
a graphical language that conveys the intuitive semantics of the
language (which is formalized in the next section).

The general idea of the graphical notation is that combinators are modeled by
``wiring diagrams'' or ``circuits'' and that values are modeled as
``particles'' or ``waves'' that may appear on the wires. Evaluation therefore
is modeled by the flow of waves and particles along the wires.

\begin{itemize}
\item The simplest sort of diagram is the {{id : b <-> b}} combinator which
  is simply represented as a wire labeled by its type {{b}}, as shown on the
  left. In more complex diagrams, if the type of a wire is obvious from the
  context, it may be omitted. When tracing a computation, one might imagine a
  value {{v}} of type {{b}} on the wire, as shown on the right.

  \begin{multicols}{2}
\begin{center}
\scalebox{0.95}{
\includegraphics{diagrams/thesis/b-wire.pdf}
}
\end{center}
\begin{center}
\scalebox{0.95}{
\includegraphics{diagrams/thesis/b-wire-value.pdf}
}
\end{center}
  \end{multicols}

\item The product type {{b1*b2}} may be represented using either one wire
  labeled {{b1*b2}} or two parallel wires labeled {{b1}} and {{b2}}. In the
  case of products represented by a pair of wires, when tracing execution
  using particles, one should think of one particle on each wire or
  alternatively as in folklore in the literature on monoidal categories as a
  ``wave.''
\begin{multicols}{2}
\begin{center}
\scalebox{0.95}{
%%subcode-line{pdfimage}[diagrams/thesis/pair-one-wire.pdf]
\includegraphics{diagrams/thesis/product-one-wire.pdf}
}
\end{center}
\begin{center}
\scalebox{0.95}{
\includegraphics{diagrams/thesis/product-one-wire-value.pdf}
}
\end{center}
\end{multicols}
\begin{multicols}{2}
\begin{center}
\scalebox{0.95}{
%%%subcode-line{pdfimage}[diagrams/thesis/pair-of-wires.pdf]
\includegraphics{diagrams/thesis/product-two-wires.pdf}
}
\end{center}
\begin{center}
\scalebox{0.95}{
\includegraphics{diagrams/thesis/product-two-wires-value.pdf}
}
\end{center}
\end{multicols}

\item Sum types may similarly be represented by one wire or using
  parallel wires with a {{+}} operator between them. When tracing the
  execution of two additive wires, a value can reside on only one of the two
  wires.
\begin{multicols}{2}
\begin{center}
\scalebox{0.95}{
%%subcode-line{pdfimage}[diagrams/thesis/sum-one-wire.pdf]
\includegraphics{diagrams/thesis/sum-one-wire.pdf}
}
\end{center}
\begin{center}
\scalebox{0.95}{
\includegraphics{diagrams/thesis/sum-two-wires-left-value.pdf}
}
\end{center}
\end{multicols}
\begin{multicols}{2}
\begin{center}
\scalebox{0.95}{
%%subcode-line{pdfimage}[diagrams/thesis/sum-of-wires.pdf]
\includegraphics{diagrams/thesis/sum-two-wires.pdf}
}
\end{center}
\begin{center}
\scalebox{0.95}{
\includegraphics{diagrams/thesis/sum-two-wires-right-value.pdf}
}
\end{center}
\end{multicols}

\item Associativity is implicit in the graphical language. Three parallel
  wires represent {{b1*(b2*b3)}} or {{(b1*b2)*b3}}, based on the context.
\begin{center}
\scalebox{0.95}{
%%subcode-line{pdfimage}[diagrams/thesis/associate.pdf]
\includegraphics{diagrams/thesis/assoc.pdf}
}
\end{center}

\item Commutativity is represented by crisscrossing wires.
\begin{multicols}{2}
\begin{center}
\scalebox{0.95}{
%%subcode-line{pdfimage}[diagrams/thesis/swap-pair.pdf]
\includegraphics{diagrams/thesis/swap_times.pdf}
}
\end{center}
\begin{center}
\scalebox{0.95}{
%%subcode-line{pdfimage}[diagrams/thesis/swap-sum.pdf]
\includegraphics{diagrams/thesis/swap_plus.pdf}
}
\end{center}
\end{multicols}

By visually tracking the flow of particles on the wires, one can
verify that the expected types for commutativity are satisfied.

\begin{multicols}{2}
\begin{center}
\scalebox{0.95}{
\includegraphics{diagrams/thesis/swap_times_value.pdf}
}
\end{center}
\begin{center}
\scalebox{0.95}{
\includegraphics{diagrams/thesis/swap_plus_value.pdf}
}
\end{center}
\end{multicols}

\item The morphisms that witness that {{0}} and {{1}} are the additive and
  multiplicative units are represented as shown below. Note that since there
  is no value of type 0, there can be no particle on a wire of type {{0}}.
  Also since the monoidal units can be freely introduced and eliminated, in
  many diagrams they are omitted and dealt with explicitly only when they are
  of special interest.
\begin{multicols}{2}
\begin{center}
\scalebox{0.95}{
%%subcode-line{pdfimage}[diagrams/thesis/identr1.pdf]
\includegraphics{diagrams/thesis/uniti.pdf}
}
\end{center}
\begin{center}
\scalebox{0.95}{
%%subcode-line{pdfimage}[diagrams/thesis/identl1.pdf]
\includegraphics{diagrams/thesis/unite.pdf}
}
\end{center}
\end{multicols}
\begin{multicols}{2}
\begin{center}
\scalebox{0.95}{
%%subcode-line{pdfimage}[diagrams/thesis/identr0.pdf]
\includegraphics{diagrams/thesis/zeroi.pdf}
}
\end{center}
\columnbreak
\begin{center}
\scalebox{0.95}{
%%subcode-line{pdfimage}[diagrams/thesis/identl0.pdf]
\includegraphics{diagrams/thesis/zeroe.pdf}
}
\end{center}
\end{multicols}

\item Finally, distributivity and factoring are represented using the dual
  boxes shown below:
\begin{multicols}{2}
\begin{center}
  \includegraphics{diagrams/thesis/dist.pdf}
\end{center}
\begin{center}
  \includegraphics{diagrams/thesis/factor.pdf}
\end{center}
\end{multicols}

Distributivity and factoring are interesting because they represent
interactions between sum and pair types. Distributivity should
essentially be thought of as a multiplexer that redirects the flow of
{{v:b}} depending on what value inhabits the type {{b1+b2}}, as shown
below. Factoring is the corresponding adjoint operation.

\begin{multicols}{2}
\begin{center}
  \includegraphics{diagrams/thesis/dist-wire-value1.pdf}
\end{center}
\begin{center}
  \includegraphics{diagrams/thesis/dist-wire-value2.pdf}
\end{center}
\end{multicols}

\item Composition TODO.

  \begin{multicols}{2}
\dgm{c1c2_par_sum}
\dgm{c1c2_par_times}
  \end{multicols}
\dgm{c1c2_seq}

\end{itemize}

\noindent
\textit{Example.}  We use the type {{bool}} as a shorthand to denote
the type {{1+1}} and use {{left ()}} to be {{true}} and {{right ()}}
to be {{false}}. The following combinator is represented by the given
diagram:

{{c : b * bool <-> b + b}}

{{c = swap* (;) dist (;) (identl* (+) identl*)}}

\begin{center}
\scalebox{0.95}{
%%subcode-line{pdfimage}[diagrams/thesis/example1-crop.pdf]
\includegraphics{diagrams/thesis/example1.pdf}
}
\end{center}

%%%%%%%%%%%%%%%%%%%
\subsection{Semantics}

The operational semantics of {{Pi}} is summarized below (also see
\cite{infeffects}).  The semantics of the primitive combinators is
given by the following single-step reductions below. Since there are
no values of type {{0}}, the rules omit the impossible cases:
\begin{scriptsize}
%subcode{opsem} include main
%! columnStyle = rlcl
% swap+ & (left v) &|-->& right v
% swap+ & (right v) &|-->& left v
% assocl+ & (left v1) &|-->& left (left v1)
% assocl+ & (right (left v2)) &|-->& left (right v2)
% assocl+ & (right (right v3)) &|-->& right v3
% assocr+ & (left (left v1)) &|-->& left v1
% assocr+ & (left (right v2)) &|-->& right (left v2)
% assocr+ & (right v3) &|-->& right (right v3)
% identl* & ((), v) &|-->& v
% identr* & v &|-->& ((), v)
% swap* & (v1, v2) &|-->& (v2, v1)
% assocl* & (v1, (v2, v3)) &|-->& ((v1, v2), v3)
% assocr* & ((v1, v2), v3) &|-->& (v1, (v2, v3))
% dist & (left v1, v3) &|-->& left (v1, v3)
% dist & (right v2, v3) &|-->& right (v2, v3)
% factor & (left (v1, v3)) &|-->& (left v1, v3)
% factor & (right (v2, v3)) &|-->& (right v2, v3)
\end{scriptsize}
The operational semantics of the closure conditions are presented in
the usual big-step style.
\begin{scriptsize}
%subcode{proof} include main
%@ ~
%@@ id v |--> v
%
%@ c{dagger} v1 |--> v2
%@@ (sym c) v1 |--> v2
%
%@ c1  v1 |--> v
%@ c2  v |--> v2
%@@ (c1(;)c2)  v1 |--> v2
%---
%@ c1  v1 |--> v2
%@@ (c1 (+) c2)  (left v1) |--> left v2
%
%@ c2  v1 |--> v2
%@@ (c1 (+) c2)  (right v1) |--> right v2
%---
%@ c1  v1 |--> v3
%@ c2  v2 |--> v4
%@@ (c1 (*) c2)  (v1, v2) |--> (v3, v4)
\end{scriptsize}

The type safety of {{Pi}} follows directly from the correspondence
between inductive definitions of the types and the big-step
semantics. Previous work also established the following properties:

\begin{proposition}[Strong Normalizing]
\label{prop:termination-pi}
{{Pi}} computations always terminate.

{{forall. c:b1<->b2, v:b1, exists v':b2.}}
{{c v |--> v'}}
\end{proposition}

\begin{proposition}[Logical Reversibility]
\label{prop:logrev}
{{c v |--> v'}} iff
{{ c{dagger} v' |--> v}}
\end{proposition}

%%%%%%%%%%%%%%%%%%%
\subsection{Constructions}

\paragraph*{Booleans and Conditionals.}
Given any combinator {{c : b <-> b}} we can construct a combinator called
{{if_c : bool*b <->bool*b}} in terms of {{c}}, where {{if_c}} behaves like a
one-armed $\mathit{if}$-expression. If the supplied boolean is {{true}} then
the combinator {{c}} is used to transform the value of type~{{b}}. If the
boolean is {{false}}, then the value of type {{b}} remains unchanged. We can
write down the combinator for {{if_c}} in terms of {{c}} as
{{ dist (;) ((id (*) c) (+) id) (;) factor }}.

\noindent The diagram below shows the input value of type {{(1+1)*b}}
processed by the distribute operator {{dist}}, which converts it into a value
of type {{(1*b)+(1*b)}}. In the {{left}} branch, which corresponds to the
case when the boolean is {{true}} (i.e. the value was {{left ()}}), the
combinator~{{c}} is applied to the value of type~{{b}}. The right branch
which corresponds to the boolean being {{false}} passes along the value of
type {{b}} unchanged.

\begin{center}
\scalebox{1.0}{
%%subcode-line{pdfimage}[diagrams/if_c.pdf]
\includegraphics{diagrams/thesis/cnot.pdf}
}
\end{center}

The combinator {{if_{not} }} has type {{bool*bool<->bool*bool}} and
negates its second argument if the first argument is {{true}}. This
gate {{if_{not} }} is often referred to as the {{cnot}} gate. An
equivalent construction that is useful is {{else_{not} }} where we
negate the second argument only if the first is {{false}}.

Similarly, we can iterate the construction of {{if_c}} to check several
bits. The gate {{if_{cnot} }}, which we may also write as {{if^2_{not} }},
checks two booleans and negates the result wire only if they are both
{{true}}. The gate {{if^2_{not} }} is well known as the Toffoli gate and is a
universal reversible gate. We can generalize this construction to
{{if^n_{not} }} which checks {{n}} bits and negates the result wire only if
they are all {{true}}.

\paragraph*{Cloning.}
Although cloning is generally not allowed in reversible languages, it is
possible at the cost of having additional constant inputs. For example,
consider the gate {{else_{not} }}. Generally, the gate maps
{{(false,a)}} to {{(false,not a)}} and {{(true,a)}} to {{(true,a)}}. Focusing
on the cases in which the second input is {{true}}, we get that the gate maps
{{(false,true)}} to {{(false,false)}} and {{(true,true)}} to {{(true,true)}},
i.e., the gate clones the first input. A circuit of {{n}} parallel
{{else_{not} }} gates can hence clone {{n}} bits.  They also consume {{n}}
{{true}} inputs in the process.  Let us call this construction
{{clone^n_{bool} }}.

%%%%%%%%%%%%%%%%%
\subsection{Categorical Structure}

For the purpose of establishing equivalence of morphisms in the
categorical presentation we use the following notion of equality:

\begin{lemma}[{{c1  = c2}}]
We say {{c1 = c2}} if {{c1:b1<->b2}} and {{c2:b1<->b2}} and for all
{{v1:b}}, we have {{c1 v1 |--> v2}} iff {{c2 v1 |--> v2}} and {{v2:b2}}.
\end{lemma}

We present the categorical structure with minimum commentary. More
details may be found in excellent references such as Barr and Wells
[CITE] and Selinger \cite{springerlink:10.1007/978-3-642-12821-94}.

\begin{lemma}[{{Pi}} is a category]
  The category {{Pi}} has the types {{b}} as objects and equivalence
  classes of well-typed combinators {{b1<->b2}} as morphisms. One can
  check:
  \begin{enumerate}
  \item Every object {{b}} has an identity morphism {{id : b <->b}}.
  \item Composition {{g (o) f}} of morphisms {{f:b1 <->b2}} and
    {{g:b2<->b3}} is given by sequencing {{f (;) g}}.
  \item Associativity of composition follows from operational
    equivalence of {{f(;)(g(;)h)}} and {{(f(;)g)(;)h}} (where
    {{h:b3<->b4}}).

    Assuming {{v1:b1}}, {{v2:b2}}, {{v3:b3}}, {{v4:b4}},
    {{f~v1|-->v2}}, {{g v2 |--> v3}} and {{h~v3 |--> v4}}, one can
    check:

\vspace{-20pt}
    \begin{multicols}{2}
      \begin{scriptsize}

%subcode{proof} include main
%@@ f v1 |--> v2
%@ g v2 |--> v3
%@ h v3 |--> v4
%@@ g (;) h  v2 |--> v4
%@@@ f (;) (g (;) h) v1 |--> v4
~
%subcode{proof} include main
%@ f v1 |--> v2
%@ g v2 |--> v3
%@@ f (;) g v1 |--> v3
%@@ h v3 |--> v4
%@@@ (f (;) g) (;) h v1 |--> v4

      \end{scriptsize}
    \end{multicols}

  \item Composition respects identity: {{id (;) f = f}} and {{g(;)id=g}}.
  \end{enumerate}
\end{lemma}

\begin{lemma}[Dagger]
  {{Pi}} is a dagger category, where every morphism {{f : b1<->b2}}
  has the adjoint {{f^{dagger}:b2 <->b1}}. The following properties
  hold (where {{g : b2 <-> b3}}):
  \begin{enumerate}
  \item {{id^{dagger} = id : b <-> b}}.
  \item {{(f (;) g)^{dagger} = g^{dagger} (;) f^{dagger}: b3 <-> b1}}.
  \item {{f^{dagger dagger} = f : b1 <-> b2}}.
  \end{enumerate}
\end{lemma}

\begin{lemma}[Symmetric Monoidal (+, 0)]
  {{Pi}} is a symmetric monoidal category with tensor {{+}} and
  monoidal unit {{0}}. The monoidal operation on morphisms is the
  additive composition of combinators {{c1 (+) c2}}.

  \begin{proof}
    To establish that category is monoidal one must show isomorphisms
    \begin{enumerate}
    \item {{b1 + (b2 + b3) <-> (b1 +b2) + b3}} given by {{assocl+}}.
    \item {{0 + b <-> b}} given by {{zeroe}}.
    \item {{b + 0 <-> b}} given by {{swap+ (;) zeroe}}.
    \item We need to check that {{+}} is a bifunctor.
      \begin{enumerate}
      \item {{id_{b1<->b1}(+)id_{b2<->b2} = id_{b1+b2<->b1+b2} }}.
      \item {{(f(+)g)(;)(j(+)k) = (f (;) j) (+) (g (;) k)}}.
      \end{enumerate}
    \item We need to check the naturality of {{assocl+}}, {{zeroe}} and
      {{swap+(;)zeroe}}.
      \begin{enumerate}
      \item {{assocl+ (;) ((f (+)g) (+) h) = (f (+)(g (+) h)) (;) assocl+}}.
      \item {{zeroe (;) f = (id (+) f) (;) zeroe}}.
      \item {{(swap+ (;) zeroe) (;) f = (f (+) id) (;) (swap+ (;) zeroe)}}.
      \end{enumerate}
    \item Satisfy certain coherence conditions which are usually
      called the ``pentagon'' and ``triangle'' axioms (see Sec 3.1
      \cite{springerlink:10.1007/978-3-642-12821-94})
    \end{enumerate}

    The last three points require checking equality of combinators by
    writing out their derivation trees as we did in the case of
    associativity of sequential composition. To show symmetry, we need
    a braiding operation {{b1+b2 <-> b2+b1}} which is given by
    {{swap+}} (see Sec. 3.3 and 3.5
    \cite{springerlink:10.1007/978-3-642-12821-94}).

    \begin{enumerate}
    \item The braiding must satisfy two ``hexagon'' axioms.
    \item The braiding is self inverse,
      {{swap+_{b1+b2}=(swap+_{b2+b1})^{dagger} }}.
    \end{enumerate}

  \end{proof}

\end{lemma}

\begin{lemma}[Symmetric Monoidal {{(*, 1)}}]
  {{Pi}} is a symmetric monoidal category over the tensor {{*}} and
  unit {{1}}. The details mirror those of the {{(0, +)}} monoid.
\end{lemma}

\noindent
Some technical and pedantic comments are due at this point.

\begin{itemize}
\item We have established the categorical structure of {{Pi}} as a
  dagger symmetric monoidal category with two monoidal structures,
  {{(0, +)}} and {{(*, 1)}}. In Sec. \ref{sec:int} we will see that a
  {{trace}} operator can be admitted in this category without any
  change of expressiveness.

\item To be pedantic, what we have shown is that the ``term model'' of
  {{Pi}} that follows from the extensional operational equality of
  combinators has the requisite categorical structure. A consequence
  is that the ``wiring diagrams'' of {{Pi}} correspond closely with ``string
  diagrams'' developed for categories.

  To establish the later rigorously, we will need to show when it is
  valid to slide one wire over the other and that equivalent diagrams
  for syntactically different combinators such as
  {{(f(+)g)(;)(j(+)k)}} and {{(f(;)j)(+)(g(;)k)}} do respect
  operational equivalence.  We don't formalize the graphical notation
  in this work. Joyal et. al's work on ``planar isotopy'' and
  Selinger's survey \cite{springerlink:10.1007/978-3-642-12821-94}
  show how this has been addressed before in the categorical
  setting. In the absence of any prior knowledge of category theory
  however, our wiring diagrams may be read as the ``flow of types'' in
  a combinator-circuit.

\item By definition, in {{Pi}} every morphism is an isomorphism --
  this makes {{Pi}} a groupoid.

\item The category {{Pi}} has no initial and terminal objects. The
  objects {{0}} and {{1}} would be initial and terminal if we admitted
  all functions (as in the category {{CatSet}}).

\item The category {{Pi}} has neither categorical products, nor
  categorical co-products, i.e. {{*}} and {{+}} are merely monoidal
  tensors. This follows from the fact that injection and projection
  maps are not isomorphisms.
\end{itemize}

While we don't do so in this work, if we extend {{Pi}} with products
and co-products (say through the addition of information effects) it
is conceivable that part of its structure collapses. This sort of
collapse, while catastrophic for algebraic structures (effectively
trivializing them), still retains some interest in computing, because
in computing we are interested in the specific operational nature (the
computational content, so to speak) of the morphisms. Anecdotal evidence
follows from the fact several real-world programming language have
inconsistent type systems. blah blah blah...

%%%%%%%%%%%%%
\subsection{Small Step Semantics}

The reductions for the primitive isomorphisms above are exactly the same as
have been presented before~\cite{infeffects}. The reductions for the closure
combinators are however presented in a small-step operational style using the
following definitions of evaluation contexts and machine states:

\begin{scriptsize}
%subcode{bnf} include main
% Combinator Contexts, C = [] | Fst C c | Snd c C
%                  &|& LeftT C c v | RightT c v C
%                  &|& LeftP C c | RightP c C
% Machine states = <c, v, C> | {[c, v, C]}
% Start state = <c, v, []>
% Stop State = {[c, v, []]}
\end{scriptsize}
The machine transitions below track the flow of particles through a
circuit. The start machine state, {{<c,v,[]>}}, denotes the
particle~{{v}} about to be evaluated by the circuit {{c}}. The end
machine state, {{[c, v, [] ]}}, denotes the situation where the particle
{{v}} has exited the circuit {{c}}.

\begin{scriptsize}
%subcode{opsem} include main
%! columnStyle = rclr
% <iso, v, C> &|-->& {[iso, v', C]} & (1)
% & & where iso v |--> v' &
% <c1(;)c2, v, C> &|-->& <c1, v, Fst C c2> & (2)
% {[c1, v, Fst C c2]} &|-->& <c2, v, Snd c1 C> & (3)
% {[c2, v, Snd c1 C]} &|-->& {[ c1(;)c2, v, C ]} & (4)
% <c1(+)c2, left v, C> &|-->& <c1, v, LeftP C c2> & (5)
% {[ c1, v, LeftP C c2 ]} &|-->& {[c1 (+) c2, left v, C ]} & (6)
% <c1(+)c2, right v, C> &|-->& <c2, v, RightP c1 C> & (7)
% {[ c2, v, RightP c1 C ]} &|-->& {[c1 (+) c2, right v, C ]} & (8)
% <c1(*)c2, (v1, v2), C> &|-->& <c1, v1, LeftT C c2 v2> & (9)
% {[ c1, v1, LeftT C c2 v2 ]} &|-->& <c2, v2, RightT c1 v1 C> & (10)
% {[ c2, v2, RightT c1 v1 C ]} &|-->& {[ c1 (*) c2, (v1, v2), C ]} & (11)
\end{scriptsize}
Rule (1) describes evaluation by a primitive isomorphism. Rules (2), (3) and
(4) deal with sequential evaluation. Rule (2) says that for the value {{v}}
to flow through the sequence {{c1 (;) c2}}, it should first flow through
{{c1}} with {{c2}} pending in the context ({{Fst C c2}}). Rule (3) says the
value {{v}} that exits from {{c1}} should proceed to flow
through~{{c2}}. Rule (4) says that when the value {{v}} exits {{c2}}, it also
exits the sequential composition {{c1(;)c2}}. Rules (5) to (8) deal with
{{c1 (+) c2}} in the same way. In the case of sums, the shape of the value,
i.e., whether it is tagged with {{left}} or {{right}}, determines whether
path {{c1}} or path {{c2}} is taken. Rules (9), (10) and (11) deal with
{{c1 (*) c2}} similarly. In the case of products the value should have the
form {{(v1, v2)}} where {{v1}} flows through {{c1}} and {{v2}} flows through
{{c2}}. Both these paths are entirely independent of each other and we could
evaluate either first, or evaluate both in parallel. In this presentation we
have chosen to follow {{c1}} first, but this choice is entirely arbitrary.

The interesting thing about the semantics is that it represents a reversible
abstract machine. In other words, we can compute the start state from the
stop state by changing the reductions {{|-->}} to run backwards
{{<--|}}. When running backwards, we use the isomorphism represented by a
combinator {{c}} in the reverse direction, i.e., we use the adjoint
{{c{dagger}}}.

\begin{proposition}[Correspondence]
  Evaluation in the small-step evaluator corresponds to evaluation in
  the natural semantics.

  {{c v |--> v'}} iff {{<c, v, []> |-->* [c', v', [] ]}}
\end{proposition}
\begin{proof}
  To prove the above, we first show that a more general lemma holds,
  namely that: {{c v |--> v'}} iff {{<c, v, C> |-->* [c', v', C ]}}.

  To show the left-to-right direction we proceed by induction on the
  derivation of {{c v |--> v'}}. In the case of primitive isomorphisms
  the condition holds trivially. In the case of composition, we work
  out the case of {{c1+c2}} as an example. Given {{c1 +c2 v |--> v'}}
  we have to show that there is a small step derivation sequence that
  matches it. Here {{v:b1+b2}} can be of the form {{left v1}} or
  {{right v2}}. Assuming {{left v1}}, we have:

%subcode{proof} include main
%@ c1 v1 |--> v1' ==> <c1, v1, C'> |-->* {[c1',v1',C']}
%@@ c1+c2 (left v1) |--> left v1' ==> ?

Choosing {{C' =Lft c2 C}}, we have the required derivation sequence:
{{ <c1+c2, left v1, C> |--> <c1,v1,LeftP c2 C> |-->* {[c1',v1', LeftP c2 C]} |--> {[c1'+c2,left v1',C]} }}. The proof follows similarly for the {{right v2}} case.

To show the right-to-left direction we proceed by induction of the
sequence of {{|-->*}} derivations. Again the case for primtive
isomorphisms follows trivially. Taking the case of {{c1+c2}} we are
required to show that given a sequence
{{<c1+c2,v,C> |-->* {[c', v', C]} }} there is a derviation tree for
{{c1+c2 v |--> v'}}. In the case that {{v:v1+b2}} has the form
{{left~v1}}, this follows by observing that the given sequence must have the form
{{ <c1+c2, left v1, C> |--> <c1,v1,LeftP c2 C> |-->* {[c1',v1', LeftP c2 C]} |--> {[c1'+c2,left v1',C]} }}.
For the strictly smaller inner subsequence by induction we have
{{c1~v1 |--> v1'}} which lets us complete the derivation of
{{c1+c2~(left v1) |--> left v1'}}. The {{right v2}} follows similarly.

\end{proof}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{small}
\bibliographystyle{abbrvnat}
\bibliography{cites}
\end{small}

\end{document}

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