\chapter{Versions of Lisp}
The two most widely referenced Lisp-family dialects today are
Common Lisp\cite{ANSILisp} and Scheme\cite{Scheme}. There are
proper standards documents that define each of these. Common Lisp is
huge and attempts to encompass absolutely everything, while Scheme is
sometimes described as ``minimalist''.

Neither of these dialects is used here. That situation clearly needs
justification. The short form of this is that Common Lisp has so many
facilities that a subset short enough to explain will omit so much of
what would make it ``Common'' as to be ridiculous.
Scheme on the other hand is a fairly compact language, but it insists
on ``correct'' treatment of various key features (notably variable scopes,
tail recursion and continuations) that tend to make an implementation
larger, slower and harder to explain.

There have been many other Lisp dialects and implementations. Common Lisp
can be seen as the successor to Maclisp and its variants embodied in various
special-purpose Lisp computers built at MIT. HLISP\cite{hlisp} was developed
\begin{wrapfigure}{r}{3.5in}
{\centering
\includegraphics[width=3.4in]{goto-and-flats.eps}}
\caption{Eiichi Goto and the FLATS machine}
\end{wrapfigure} by Eiichi Goto in Japan with a key feature that memory was saved by using
hashing to implement the {\tx cons} operation so that as much data as
possible could be shared. HLISP led to the development of the FLATS\cite{flats}
computer which combined high performance for both Lisp-style symbolic
computation with good numeric capability -- with hardware acceleration for
hashing and associative operations that made HLISP special.

At the other extreme of the size and performance scale, muLisp (in
due course distributed by the Soft Warehouse Inc) started as a 2 Kbyte
implementation manually toggled into the memory of a 4 Kbyte computer
based on an 8080 processor\cite{Imsai}.

One of the notable implementations of Common Lisp is {\tx gcl} (GNU Common
Lisp). Its full source can be fetched via (for instance) the Free
Software Foundation's website at
{\tx http://www.gnu.org/software/gcl}. The source archive
contains around 180,000 lines of Lisp code, as well as significant amounts
of C code that among other things provide support for arbitrary precision
arithmetic. The ``Visible'' Lisp used here (\vsl) has as its main
source just over 3000 lines of C code, which is already quite enough to
try to explain.

Scheme is smaller, to the extent that some would claim that the basic
version was only suitable for teaching not for real use. There are
an enormous number of implementations of if: one list names 78. But the
vast majority of these are also much bulkier than \vsl{} and hence would be
harder to explain.

The alternative approach used here has been to go a little further back in
history and use the Standard Lisp dialect that originated at the University
of Utah. There were two stages in its definition.
The report defining an initial version of it was by Tony Hearn in
1969\cite{StdLisp1} and was his attempt to codify a Lisp dialect that
could provide portability for his Reduce algebra system\cite{Reduce}. A
decade later Marti, Hearn and Griss produced a substantial update\cite{StdLisp2}.
Griss, Benson and Maguire released PSL\cite{PSL} as a Portable implementation
of Standard Lisp a couple of years later, and others involved with the
Reduce project followed on with at least Cambridge Lisp\cite{Camlisp},
then CSL\cite{CSL} and a Java-coded version known as Jlisp\cite{Jlisp}. These
days PSL, CSL and Jlisp can all be found on the website
{\tx http://reduce-algebra.sourceforge.net} and all are both in use and
continued development.

By following Standard Lisp the code and explanations here steer a line between
excess complexity and being so cut down and simple that they have no
real relevance. The simplicity of \vsl can be assessed by just looking at
the fairly small amount of C code that implemented it, and this code
implements the most important facilities that any Lisp variant will require.
The power of \vsl{} can be seen from the fact that it can be used to build
the roughly 350,000 lines of code that make up the whole of the Reduce algebra
system, with almost every part of that code working as expected. There are
three key limitations that mean that Reduce built in top of \vsl{} may not
be suitable for heavy use. The first is that \vsl{} only supports
integers with values up to 9223372036854775807 in a tolerably
efficient manner, while Reduce needs fully
arbitrary precision arithmetic in many places. The second is that \vsl{}
provides just an interpreter for Lisp, not a compiler, and as a result
calculations performed using it will be somewhat slow. The third issue is that
\vsl{} only supports very basic interaction with the user via the keyboard, and
many of today's users would expect a much richer user-interface. But all those
limitations admitted, it remains that \vsl{} can support Lisp applications
that run up in to the hundreds of thousands of lines of source code!

Despite the adoption of the Standard Lisp dialect for \vsl{} it has to be
conceded that Common Lisp can not properly be ignored. It remains the fact that
a full implementation would be much too large to explain, and indeed even
full coverage of the language from the perspective of a user would need
a seriously lengthy and probably rather dull book. So three things are
done here to provide support for a move onward from \vsl{} into use of
Common Lisp. First all the example programs are provided in Common Lisp
for as part of the down-loadable resources to accompany this book. The
code there has comments in it noting such special features of Common Lisp
as appear. Secondly a modified version of \vsl{} that provides compatibility
with the core of Common Lisp is also provided for download -- it is known
as \vcl. \vcl{} is naturally far from complete as a Common Lisp, and its
documentation here is much less complete than that for \vsl{}, but it can form
a bridge to use of Big Lisp. Many of the features of \vsl{} are implemented
as Lisp code that builds on a fairly small number of primitive operations.
The final aspect of Common Lisp support is that the most interesting or
important of these are described and explained as an extended set of
fragments of Lisp sample code. This explanation can give both insight into
some of the power of Common Lisp and some of the pain it gives any
person trying to provide a high performance implementation.

A definite stance taken with \vsl{} is that it is not intended to be
viewed as a fully-finished Lisp system merely for use. It is more a starting
point for project work that will extend it. Those who are convinced that
Common Lisp or Scheme would be better can use much of the core technology
within \vsl{} and adapt it to implement the dialect that they are keen on.
It would be essentially trivial to just change the names that \vsl{} uses
for the Lisp functions that it supplies. For instance Scheme uses the names
{\tx set!} and {\tx equal?} where \vsl{} uses {\tx setq} and {\tx equal},
and Common Lisp used {\tx defun} where \vsl{} used {\tx de}. Adding extra
functions, and even data-types should be straightforward. Deeper semantic
differences between the dialects would naturally lead to more challenging
re-work.

Anybody who became enthusiastic about using \vsl{} for algebraic computation
might retrofit full-speed arbitrary prevision arithmetic. If they wanted to match the
arithmetic capability of Common Lisp they would also need to add complex
numbers and a full range of elementary functions working on them.

Lisp is a very natural language for compiler-building, and a small and
fairly tidy version like \vsl{} could provide a good starting point for
a project to build one. One of the examples given later here may provide
some sort of a start, but Appendix 3 in Berkeley and Bobrow\cite{BandB} (one
of many important historical Lisp books now available on-line) includes
a listing of a complete real Lisp compiler -- albeit for a machine now
long defunct. There is no reason to believe that a compiler for \vsl{} would
not transform it into a system speed-competitive with all other current
Lisps. The compiler in Berkeley and Bobrow is maybe 1500 lines of
code, and the key parts of if that do the central interesting bits of the work are maybe
only around 500 lines. This is not an outrageously huge project.

User-interface refinement is something that could generate an almost
unlimited amount of activity. Before starting on a project in this area it
would make sense to review what others have done. One possible
place to start would be to investigate LispWorks\cite{LispWorks},
which provides an integrated cross-platform development environment.


%%
%% Automatically generated file from DocOnce source
%% (https://github.com/hplgit/doconce/)
%%
%%
% #ifdef PTEX2TEX_EXPLANATION
%%
%% The file follows the ptex2tex extended LaTeX format, see
%% ptex2tex: http://code.google.com/p/ptex2tex/
%%
%% Run
%%      ptex2tex myfile
%% or
%%      doconce ptex2tex myfile
%%
%% to turn myfile.p.tex into an ordinary LaTeX file myfile.tex.
%% (The ptex2tex program: http://code.google.com/p/ptex2tex)
%% Many preprocess options can be added to ptex2tex or doconce ptex2tex
%%
%%      ptex2tex -DMINTED myfile
%%      doconce ptex2tex myfile envir=minted
%%
%% ptex2tex will typeset code environments according to a global or local
%% .ptex2tex.cfg configure file. doconce ptex2tex will typeset code
%% according to options on the command line (just type doconce ptex2tex to
%% see examples). If doconce ptex2tex has envir=minted, it enables the
%% minted style without needing -DMINTED.
% #endif

% #define PREAMBLE

% #ifdef PREAMBLE
%-------------------- begin preamble ----------------------

\documentclass[%
oneside,                 % oneside: electronic viewing, twoside: printing
final,                   % draft: marks overfull hboxes, figures with paths
10pt]{article}

\listfiles               %  print all files needed to compile this document

\usepackage{relsize,makeidx,color,setspace,amsmath,amsfonts,amssymb}
\usepackage[table]{xcolor}
\usepackage{bm,ltablex,microtype}

\usepackage[pdftex]{graphicx}

\usepackage{ptex2tex}
% #ifdef MINTED
\usepackage{minted}
\usemintedstyle{default}
% #endif

\usepackage[T1]{fontenc}
%\usepackage[latin1]{inputenc}
\usepackage{ucs}
\usepackage[utf8x]{inputenc}

\usepackage{lmodern}         % Latin Modern fonts derived from Computer Modern

% Hyperlinks in PDF:
\definecolor{linkcolor}{rgb}{0,0,0.4}
\usepackage{hyperref}
\hypersetup{
    breaklinks=true,
    colorlinks=true,
    linkcolor=linkcolor,
    urlcolor=linkcolor,
    citecolor=black,
    filecolor=black,
    %filecolor=blue,
    pdfmenubar=true,
    pdftoolbar=true,
    bookmarksdepth=3   % Uncomment (and tweak) for PDF bookmarks with more levels than the TOC
    }
%\hyperbaseurl{}   % hyperlinks are relative to this root

\setcounter{tocdepth}{2}  % levels in table of contents

% prevent orhpans and widows
\clubpenalty = 10000
\widowpenalty = 10000

% --- end of standard preamble for documents ---

% insert custom LaTeX commands...

\raggedbottom
\makeindex
\usepackage[totoc]{idxlayout}   % for index in the toc
\usepackage[nottoc]{tocbibind}  % for references/bibliography in the toc

%-------------------- end preamble ----------------------

\begin{document}

% matching end for #ifdef PREAMBLE
% #endif

\newcommand{\exercisesection}[1]{\subsection*{#1}}

% ------------------- main content ----------------------

% ----------------- title -------------------------

\thispagestyle{empty}

\begin{center}
{\LARGE\bf
\begin{spacing}{1.25}
Second week
\end{spacing}
}
\end{center}

% ----------------- author(s) -------------------------

\begin{center}
{\bf Data Analysis and Machine Learning${}^{}$} \\ [0mm]
\end{center}

\begin{center}
% List of all institutions:
\end{center}

% ----------------- end author(s) -------------------------

% --- begin date ---
\begin{center}
Jul 3, 2020
\end{center}
% --- end date ---

\vspace{1cm}

\subsection{Regression examples, from linear regression, via decision trees and various forests to neural networks}

The main aim of this project is to study some specific
regression problems, starting with the regression algorithms studied
in homework set 3 (exercise 2 in particular).
We will include decision trees, random forests and eventually boosting
methods  and neural network with \textbf{tensorflow} (feel free however to write your own code).

The case we encounter here
is  the so-called Ising model for  our training data and we will
focus on supervised training. We will follow closely the recent
article of \href{{https://arxiv.org/abs/1803.08823}}{Mehta et al, arXiv
1803.08823}. This article stands
out as an excellent review on machine learning (ML) algorithms.
The added benefit is that each figure and
model presented in \href{{https://physics.bu.edu/~pankajm/MLnotebooks.html}}{this article is accompanied by its jupyter
notebook}. This
means that we can start using these and compare with our own results.

You can also look up the \href{{https://compphysics.github.io/MachineLearningECT/doc/pub/Day1/html/Day1.html}}{Regression slides for a discussion of the Ising model} (scroll down to the end).

Alternatively, you can replace the Ising thorughout the exercises with the nuclear binding energies. The choice is yours. Or if you have other data sets suitable for regression, feel free to use those.

What follows here is however a discussion of the Ising model. The nuclear binding energies were discussed during the lectures.

With the abovementioned configurations we will determine, using first
various regression methods, the value of the coupling constant for the
energy of the one-dimensional Ising model.
We will mainly use \textbf{scikit-learn} or \textbf{tensorflow} or other Python packages such as \textbf{keras} or other.

Feel free to use the notebooks to benchmark your code.

\paragraph{Part a): Producing the data for the one-dimensional Ising model.}
The model we will employ in our studies is the so-called \href{{https://en.wikipedia.org/wiki/Ising_model}}{Ising
model}.  Together with
models like the \href{{https://en.wikipedia.org/wiki/Potts_model}}{Potts
model} and similar
so-called lattice models, the Ising model has been widely studied in
mathematics (in statistics in particular), physics, \href{{https://journals.aps.org/pre/abstract/10.1103/PhysRevE.93.062402}}{life
science},
chemistry and even in the \href{{https://www.springer.com/gp/book/9781461420316}}{social sciences in order to model social
behavior}. It is a
simple binary value system where the variables of the model (spins often in
physics) can take two values only, for example $\pm 1$ or $0$ and $1$.
The system exhibits a phase transition in two or higher dimensions and
the first person to find the analytical expressions for various
expectation values was the Norwegian chemist \href{{https://en.wikipedia.org/wiki/Lars_Onsager}}{Lars
Onsager} (Nobel prize in
chemistry) after a tour de force mathematics exercise.

In our discussions here we will stay with a physicist's approach and
call the variables for spin. You could replace this with any other
type of binary variables, ranging from a two political parties to blue
and red spheres.  In its simplest form we define the energy of the
system as

\begin{equation*}
  E=-J\sum_{<kl>}^{N}s_ks_l,
\end{equation*}
with  $s_k=\pm 1$, $N$ is the total number of spins,
$J$ is a coupling constant expressing the strength of the interaction
between neighboring spins.

The symbol $<kl>$ indicates that we sum over nearest
neighbors only.
Notice that for $J>0$ it is energetically favorable for neighboring spins
to be aligned. This feature leads to, at low enough temperatures,
a cooperative phenomenon called spontaneous magnetization. That is,
through interactions between nearest neighbors, a given magnetic
moment can influence the alignment of spins  that are separated
from the given spin by a macroscopic distance. These long range correlations
between spins are associated with a long-range order in which
the lattice has a net magnetization in the absence of a magnetic field.

We start by considering the one-dimensional Ising model with nearest neighbor interactions. This model does not exhibit any phase transition.

Consider the 1D Ising model with nearest-neighbor interactions

\begin{equation*}
  E[\hat{s}]=-J\sum_{j=1}^{N}s_{j}s_{j+1},
\end{equation*}

on a chain of length $N$ with so-called periodic boundary conditions and $S_j=\pm 1$ Ising spin variables.
In one dimension, this model has no phase transition at finite temperature.

In the Python code below we generate, with a coupling coefficient set to $J=1$, a large number of spin configurations say $10000$ as shown in the code below.
It means that our data will be a set of $i=1\ldots n$ points of the form
$\{(E[\boldsymbol{s}^i],\boldsymbol{s}^i)\}$.
Our task is to find the value of $J$ from the data set using linear regression.

Here is the Python code you need to generate the training data, see
also the \href{{https://physics.bu.edu/~pankajm/ML-Notebooks/HTML/NB_CVI-linreg_ising.html}}{notebook of Mehta et
al}.

\bpycod
import numpy as np
import scipy.sparse as sp
np.random.seed(12)

import warnings
#Comment this to turn on warnings
warnings.filterwarnings('ignore')

### define Ising model aprams
# system size
L=40

# create 10000 random Ising states
states=np.random.choice([-1, 1], size=(10000,L))

def ising_energies(states,L):
    """
    This function calculates the energies of the states in the nn Ising Hamiltonian
    """
    J=np.zeros((L,L),)
    for i in range(L):
        J[i,(i+1)%L]-=1.0
    # compute energies
    E = np.einsum('...i,ij,...j->...',states,J,states)

    return E
# calculate Ising energies
energies=ising_energies(states,L)
\epycod

We can now recast the problem as a linear regression model using our codes from homework set 3 (exercise 2 in particular).
The way we are going to build our model mimicks the way we could think of finding say the gravitional constant for the graviational force between two planets.
In the absence of any prior knowledge, one sensible choice is the all-to-all Ising model

\[
E_\mathrm{model}[\boldsymbol{s}^i] = - \sum_{j=1}^N \sum_{k=1}^N J_{j,k}s_{j}^is_{k}^i.
\]

Here $i$ represents a particular spin configuration (one of the possible $n$ configurations we generated with the code above).

This model is uniquely defined by the non-local coupling strengths $J_{jk}$ which we want to learn.
The model is linear in $\mathbf{J}$ which makes it possible to use linear regression.

To apply linear regression, we recast this model in the form
\[
E_\mathrm{model}^i \equiv \mathbf{X}^i \cdot \mathbf{J},
\]

where the vectors $\mathbf{X}^i$ represent all two-body interactions
$\{s_{j}^is_{k}^i \}_{j,k=1}^N$, and the index $i$ runs over the
samples in the data set.  To make the analogy complete, we can also
represent the dot product by a single index $p = \{j,k\}$,
i.e.~$\mathbf{X}^i \cdot \mathbf{J}=X^i_pJ_p$.  Note that the
regression model does not include the minus sign, so we expect to
learn negative $J$'s.

\paragraph{Part b): Estimating the coupling constant of the one-dimensional Ising model using linear regression.}
We start with the one-dimensional Ising model and use the data we have
generated with $J=1$ in the previous point.

\textbf{Use linear regression,
Lasso and Ridge regression as done in homework 3}.
Make an analysis of the guessed coupling constant as function of the hyperparameters $\lambda$.
You can compare your
results with those of \href{{https://physics.bu.edu/~pankajm/ML-Notebooks/HTML/NB_CVI-linreg_ising.html}}{Mehta
et al.}.
Make sure it is the 1D data which is used.

Discuss the methods and how they perform in computing the coupling
constant $J$ and include a bias-variance analysis using the bootstrap resampling method.
Discuss also the mean squared error and
the $R2$ score as measures to assess your model.

Give a critical analysis of your results.

You can replace the Ising model data with the nuclear binding energy data.

\paragraph{Part c): Random forests and boosting.}
Repeat the above analysis but using random forests and boosting (XGboost or normal gradient boosting). You can use the functions
in \textbf{scikit-learn} for random forests and gradient boosting. For \textbf{XGBoost} you need to install it separately. You can still use other functionality in \textbf{scikit-Learn}.

\paragraph{Part d): Regression analysis of the one-dimensional Ising model using neural networks.}
Your aim now is to use either \textbf{scikit-learn} or \textbf{tensorflow} in order to set up a neural network to find the optimal weights and biases.

Train your network and compare the results with those from your linear regression code and random forests/boosting methods.

You can test your results against a similar code using \textbf{scikit-learn} or \textbf{tensorflow/keras}.

A useful reference on the back progagation algorithm is \href{{http://neuralnetworksanddeeplearning.com/}}{Nielsen's book}. It is an excellent read.

Finally, give a critical analysis of your results with pros and cons of the various methods.

\subsection{Background literature}

\begin{enumerate}
\item The text of Michael Nielsen is highly recommended, see \href{{http://neuralnetworksanddeeplearning.com/}}{Nielsen's book}. It is an excellent read.

\item The textbook of \href{{https://www.springer.com/gp/book/9780387848570}}{Trevor Hastie, Robert Tibshirani, Jerome H. Friedman, The Elements of Statistical Learning, Springer}, chapters 3 and 7 are the most relevant ones for the analysis here.

\item \href{{https://arxiv.org/abs/1803.08823}}{Mehta et al, arXiv 1803.08823}, \emph{A high-bias, low-variance introduction to Machine Learning for physicists}, ArXiv:1803.08823.
\end{enumerate}

\noindent
If you wish to read more about the Ising model and statistical physics here are three suggestions.

\begin{enumerate}
\item \href{{http://www.worldscientific.com/worldscibooks/10.1142/5660}}{M. Plischke and B. Bergersen}, \emph{Equilibrium Statistical Physics}, World Scientific, see chapters 5 and 6.

\item \href{{http://www.cambridge.org/no/academic/subjects/physics/computational-science-and-modelling/guide-monte-carlo-simulations-statistical-physics-4th-edition?format=HB}}{D. P. Landau and K. Binder}, \emph{A Guide to Monte Carlo Simulations in Statistical Physics}, Cambridge, see chapters 2,3 and 4.

\item \href{{https://global.oup.com/academic/product/monte-carlo-methods-in-statistical-physics-9780198517979?cc=no&lang=en&}}{M. E. J. Newman and T. Barkema}, \emph{Monte Carlo Methods in Statistical Physics}, Oxford, see chapters 3 and 4.
\end{enumerate}

\noindent

% ------------------- end of main content ---------------

% #ifdef PREAMBLE
\end{document}
% #endif


%----------------------------------------------------------------------------------------
%	PACKAGES AND OTHER DOCUMENT CONFIGURATIONS
%----------------------------------------------------------------------------------------

\documentclass[11pt]{article}
\usepackage{hyperref}
\usepackage{listings}
\usepackage{caption}
\usepackage{subcaption}

\input{structure.tex} % Include the file specifying the document structure and custom commands

%----------------------------------------------------------------------------------------
%	ASSIGNMENT INFORMATION
%----------------------------------------------------------------------------------------

% Required
\newcommand{\assignmentQuestionName}{} % The word to be used as a prefix to question numbers; example alternatives: Problem, Exercise
\newcommand{\assignmentClass}{Zero to ASIC} % Course/class
\newcommand{\assignmentTitle}{Analogue Design} % Assignment title or name
\newcommand{\assignmentAuthorName}{Thomas Parry} % Student name

% Optional (comment lines to remove)
%\newcommand{\assignmentClassInstructor}{Jones 10:30am} % Intructor name/time/description
%\newcommand{\assignmentDueDate}{Monday,\ January\ 24,\ 2019} % Due date

%----------------------------------------------------------------------------------------

\begin{document}

%----------------------------------------------------------------------------------------
%	TITLE PAGE
%----------------------------------------------------------------------------------------

\maketitle % Print the title page

\thispagestyle{empty} % Suppress headers and footers on the title page

\newpage

\section{Fundamentals of the MOSFET}

The MOSFET (Metal-Oxide Semiconductor Field Effect Transistor) is the fundamental building block of modern IC design.

\begin{figure}[h]
\centering{
\includegraphics[scale=1.00]{diagrams/nmos.pdf}
}
\end{figure}

The MOSFET is a \textit{transconductance} device which translates a voltage between it's gate and source terminals to a current between the drain and source - assuming the device is biased correctly.

\subsection{The Saturation Region}

Most devices in a circuit will be operating in saturation where the following description of the current is given for a square-law device:

\begin{equation}
I_D = \frac{1}{2} \mu_n C_{ox} \frac{W}{L} {\left( V_{GS} - V_{TH} \right)}^2
\end{equation}

This can be simplified to:

\begin{equation}
I_D = K_n \frac{W}{L} {V_{OD}}^2
\end{equation}

\begin{equation}
given \; that: \;\;\; V_{DS} > V_{OD}
\end{equation}

The condition is very important, if you want your FET to work as expected you must ensure you have sufficient $V_{DS}$ headroom.  The value $V_{OD}$ is also more commonly called $V_{D_{SAT}}$ and is defined as the minimum drain voltage required to keep the device in saturation. Note that in sub-micron processes (like Skywater 130 nm) the statement that $V_{D_{SAT}} = V_{OD}$ is not fully accurate due to second order effects from device scaling.

\subsection{The Triode Region}

When there is insufficient drain-source voltage (by design or error) the device will work in the triode region where it approximates a voltage controlled resistor:

\begin{equation}
I_D = \mu_n C_{ox} \frac{W}{L} \left( (V_{GS} - V_{TH}) V_{DS} - \frac{V_{DS}^2}{2} \right)
\end{equation}

\subsection{The Cutoff Region}

When there is insufficient voltage across the gate-source terminals the device is 'off':

\begin{equation}
I_D = 0 \;\;\;\; given \; that: \;\; V_{GS} << V_{TH}
\end{equation}

\answer{Always know the region you expect your FET to operate and check that you have it correctly biased or all other assumptions are broken.}

\section{Basics of Transconductance Efficiency}

The transconductance is the amount of current in the drain per gate-source voltage.

\begin{equation}
i_d = g_m \cdot v_{gs}
\end{equation}

This simple diagram shows the most important concept of the FET: a voltage at the input is converted to an output current. This output current will later be converted by an impedance to another voltage.

\begin{figure}[h]
\centering{
\includegraphics[scale=1.00]{diagrams/nmos_smallsignal.pdf}
}
\end{figure}

The transconductance can be expressed as a function of the bias current, giving the $g_m / I_d $ value. This value is related to how inverted the channel of the FET is - a measure of how 'turned on' the FET is. The measure of channel inversion is how many minority charge carriers have been attracted to the conductive channel between the source and drain.

\subsection{Transconductance Efficiency and Overdrive Voltage}

There is a relation to the overdrive voltage mentioned above, $V_{OD}$. The higher the $V_{OD}$, the more the FET is 'turned on', and therefore the more the FET channel is inverted, which leads to a \textit{lower} $g_m / I_d$. Conversely, a lower $V_{OD}$ turns on the FET less, and therefore the FET channel is less inverted, which leads to a \textit{higher} $g_m / I_d$.

\subsection{Realistic Values of Transconductance Efficiency}

There is a range of transconductance efficiencies that can be obtained in standard CMOS processes. It is reasonable to assume a maximum transconductance efficiency of 20 in design equations, although in some specific cases values of 25 can be reached. Typically a device will not be used below a transconductance efficiency of 4.

\subsection{Why Use Transconductance Efficiency?}

While transconductance efficiency, $g_m/I_d$, feels very abstract at first, its use lies in two factors.

\begin{enumerate}
  \item It gives a strong indication of the channel inversion.
  \item It allows quick calculation of circuit currents and other conditions.
\end{enumerate}

For example, if I have determined I will use a FET in weak inversion and I need $1 \; mS$ of transconductance, then I very simply know the required current of $50 \; \mu A$.

\answer{But how do we know what transconductance we should use for a given FET? We'll discuss this in the next section where I will talk about the relation of matching and noise to transconductance.}

\textbf{Further Reading (clickable)}

\hspace{1cm} \href{http://web02.gonzaga.edu/faculty/talarico/EE406/documents/gmid.pdf}{gm/ID - Based Design}

\hspace{1cm} \href{http://web.eecs.utk.edu/~bblalock/ece532/ece532_pres_ekv_bsim.pdf}{An Introduction to the EKV Model and a comparison of EKV to BSIM}

\newpage
\section{Noise, Offset and Transconducance}

\subsection{Noise}

Considering only thermal noise the drain noise in a MOSFET is given by:

\begin{equation}
\frac{i^2_{n,d}}{\Delta f} = 4 K T \gamma g_{do}
\end{equation}

Where the $ K $ is the Boltzmann's constant and $T$ is absolute temperature. We will set the device specific parameter $\gamma=1$ for simplicity. The zero bias drain conductance, $g_{do}$, can be approximated to $ g_m $.

\begin{equation}
\frac{i^2_{n,d}}{\Delta f} \approx 4 K T g_{m}
\end{equation}

So now we can see that the current noise in the drain of a MOSFET \textbf{increases} with increasing transconductance. However, to complete the story we can also refer the noise to the gate. To do this we divide the noise power above by the power transfer function of the MOSFET, $g^2_{m}$, to give:

\begin{equation}
\frac{v^2_{n,d}}{\Delta f} \approx \frac{4 K T}{ g_{m} }
\end{equation}

Now we see that expressed as a input referred noise source the noise \textbf{decreases} with increasing $g_{m}$. This is because the increasing gain of the MOSFET overpowers the increasing noise in the channel. The result also complies with the intuition formed by Friis' equation.

\subsection{Offset}

All MOSFETs are fabricated with random variations which exhibit as mismatch offset between ideally identical devices. The largest contributor to offset is due to the gate-source threshold voltage offset. Pelgrom proposed the following model for offset which is widely adopted:

\begin{equation}
\sigma_{\Delta V_{TH}} = \frac{A_{VTH}}{\sqrt{WL}}
\end{equation}

This states that the standard deviation of the mismatch between two devices is a process specific value divided by the square root of the area of the device (assuming both devices are identical). The SKY130 process does not have a published figure for $A_{VTH}$ but using $10 \ mV \mu m$ is reasonable.

Consider the small signal description of the FETs transfer:

\begin{equation}
i = g_m \cdot v_{GS}
\end{equation}

Where we then add an offset:

\begin{equation}
i = g_m \cdot ( v_{GS} + \Delta V_{TH} )
\end{equation}

We can see that the offset voltage is transferred to the current as a function of $g_m$. So for small drain current offset we need a low $g_m$.

Considering a voltage amplilfier, the original threshold offset cannot be improved but any offset further down the system can be modelled as input referred but divided by $g_m$. Therefore, we want a high $g_m$, this tracks with Friis' equation.

\newpage
\section{The Current Mirror}

The most important circuit in analogue design is the current mirror. This circuit is used as the building block for a huge amount of applications.

\begin{figure}[h]
\centering{
\includegraphics[scale=1.00]{diagrams/current_mirror.pdf}
}
\end{figure}

\subsection{Circuit Operation}

The input current is fed into a diode-connected FET. This diode-connected FET generates a voltage on it's gate which is approximately equal to:

\begin{equation}
V_{GS} = \sqrt{I_{IN} \frac{1}{K_n} \frac{L}{W}} + V_{TH}
\end{equation}

This voltage is then applied to the gate of the second FET. Assuming the sizes are the same, the output current will then match the input current. If the sizes are different then the output current is scaled by:

\begin{equation}
I_{OUT} = {\left(\frac{W}{L}\right)}_{OUT} \cdot {\left(\frac{L}{W}\right)}_{IN}
\end{equation}

To have good matching performance usually $L_{IN} = L_{OUT}$ and $\frac{W_{OUT}}{W_{IN}} $ is a rational number so that unit FETs can be used.

\subsection{Selecting Sizes}

How do we select the value of $\frac{W}{L}$? As we discussed previously the output is a current so to get the best performance for thermal noise and offset we need to use a low $g_m$. This means we need a low transconductance efficiency where the channel is heavily inverted and the overdrive voltage is high.

To achieve this, generally we want a low value of $\frac{W}{L}$. Although the absolute value depends on the device used, PMOS devices have a lower charge mobility than NMOS so will require a higher $\frac{W}{L}$ ratio. Aim for a $g_m/I_d$ of 5-7 or a $V_{DS,sat}$ of 300 - 350 mV.

\newpage
\section{The Differential Pair}

The differential pair is shown below. It is most commonly seen at the input of op-amp circuits but is also seen in many other places.

\begin{figure}[h]
\centering{
\includegraphics[scale=1.00]{diagrams/differential_pair.pdf}
}
\end{figure}

\subsection{Circuit Operation}

The differential pair compares two voltages and splits the bias current depending on the difference between the input voltages.

If the two input voltages are equal then $V_{GS}$ is equal for both devices and both devices get half of the bias voltage. If $V_{IN+}$ is higher than $V_{IN-}$ then more current flows into $V_{OUT+}$ and less into $V_{OUT-}$, but the sum still remains the same bias current.

\subsection{Selecting Sizes}

As already discussed in the section on noise and offset the differential pair devices should operate with a high $g_m$ and therefore a high transconductance efficiency. Therefore the ratio of $\frac{W}{L}$ should be high. Aim for a $g_m/I_d$ of 20-25 or a $V_{DS,sat}$ of 10 - 50 mV.

\newpage
\section{Useful Circuits}

\subsection{Transmission Gate}

A building block switch made of one or more FETs. The control signals are digital and the terminals $A$ and $B$ are analogue.

\begin{figure}[h]
\centering{
\includegraphics[scale=1.00]{diagrams/transmission_gate.pdf}
}
\end{figure}

Important factors:

\begin{itemize}
  \item On resistance of switch
  \item Charge injection
\end{itemize}

\subsection{5T Op-Amp}

A simple two stage op-amp circuit with differential inputs and current output. Stability can be improved with a Miller capacitor across the output stage.

\begin{figure}[h]
\centering{
\includegraphics[scale=1.00]{diagrams/opamp_5t.pdf}
}
\end{figure}

Important factors:

\begin{itemize}
  \item Input/Output Voltage Ranges
  \item Gain
  \item Bandwidth
  \item Stability
  \item Noise
  \item Slew Rate
\end{itemize}

\newpage
\section{Opamp Verification}

There are a number of standard verification measurement points for an op-amp circuit. Some of the later half may be skipped if they are not particularly important in the circuits application. The first half must be performed to have reasonable confidence in the operation of the circuit.

\begin{table}[h]
\resizebox{\textwidth}{!}{%
\centering
\begin{tabular}{llll}
\textbf{Name}      & \textbf{Conditions}           & \textbf{Sim} & \textbf{Description} \\ \hline
Operating Point    & min/max($V_{IN}$/$V_{OUT}$)   & .op          & Ensure all devices are operating in correct region (usually saturation) \\
AC Gain            & min/max($V_{IN}$/$V_{OUT}$)   & .ac          & Simulate AC gain of opamp \\
AC Stability       & min/max($V_{IN}$/$V_{OUT}$)   & .ac          & Simulate phase/loop gain \\
Tranient Stability & small $V_{IN}$ step           & .tran        & Observe transient stability in response to a step function \\
Offset             & min/max($V_{IN}$)             & .dc          & In unity gain what is the input referred offset \\
Slew Rate          & large $V_{IN}$ step           & .tran        & Put opamp into large signal slew and ensure it meets requirements \\
Noise              & min/max($V_{IN}$/$V_{OUT}$)   & .noise       & Simulate input referred noise performance \\
PSRR               & min/max($V_{IN}$/$V_{OUT}$)   & .ac          & Simulate Power Supply Rejection Ratio \\
CMRR               & min/max($V_{IN}$/$V_{OUT}$)   & .ac          & Simulate Common Mode Rejection Ratio \\
\end{tabular}}
\end{table}

These would be repeated in a number of conditions progressively as the design matures:

\begin{itemize}
  \item Typical corner
  \item Across all corners
  \item Monte-Carlo random sampling
\end{itemize}

The typical corner is where the main design phase occurs, ensuring the main functional performance is as expected. Once the circuit works well in a typical conditions, the full range of environmental and manufacturing conditions need to be explored. This is normally achieved by simulating over PVT (Process, Voltage and Temperature). Usually there are five MOS corners (tt, ff, fs, sf, ss) plus passive corners, the voltage range is commonly the nominal supply +/-10\% and temperature depends on application - for commercial $0 - 85^\circ C$ is acceptable.

Once these verification points have been covered and documented the circuit is ready to move to layout.

\newpage
\section{Layout}

Once the schematic has been fully investigated the layout phase can begin to describe the physical implementation on silicon.

\subsection{Wells}

In a bulk CMOS process (like SKY130) everything is built into a common substrate with differently doped regions. Typically the substrate is P- doped before the device fabrication begins.

An NMOS consists of a channel which is P- doped and N+ doped drain/source. As the electrostatic potential of the gate inverts the region below the gate with negative charge carriers (electrons) a conductive channel is formed.

\begin{figure}[h]
\centering{
\includegraphics[scale=0.50]{diagrams/cmos_well.png}
}
\end{figure}

To create PMOS devices a N- bulk is required. To achieve this a well is created in the substrate where PMOS devices can be created. An NWELL is defined and many PMOSs can be manufactured within it. It is very important to note that the NWELL is a PN junction with the substrate which forms a diode. If the NWELL is biased at a lower potential than the substrate a current will begin to flow.

\subsubsection{Latchup}

Once the PMOS devices have been created a structure exists which can be very destructive. The drain/source of the PMOS is P, the bulk N, the P substrate and the N drain/source of the NMOSs. This structure can form a parasitic thyristor structure with positive feedback which once triggered allows very high currents to flow causing damage to the device.

\begin{figure}[h]
\centering
\begin{subfigure}{0.6\textwidth}
  \centering
  \includegraphics[width=1.0\linewidth]{diagrams/latchup_layout.png}
  \caption{layout}
\end{subfigure}%
\begin{subfigure}{0.4\textwidth}
  \centering
  \includegraphics[width=0.6\linewidth]{diagrams/latchup_circuit.png}
  \caption{circuit}
\end{subfigure}
\end{figure}

The likelihood of latchup occurs primarly as a function of voltages applied between PN junctions, the parasitic bulk resistors and bipolar gains. To prevent latchup these causes should be dealt with in that order.

The easiest way to prevent latchup is ensure that the PN junctions never get biased. This is achieved by ensuring the bulk is always at a lower potentional than any sources (for NMOS). The second step is to reduce parasitic resistors which bias the thyristor. This is achieved by reducing the distance between sources and bulk connections.

\subsubsection{Triple Well}

The NMOS devices can be fabricated directly into the bulk substrate but they all share a common bulk which must be connected to the lowest potentional, usually ground. This can be an issue for two main reasons. If the circuit requires the bulk of NMOSs to be connected to their source - commonly to prevent body effect in differential pairs - this is not possible with substrate based NMOSs. Secondly all the NMOS devices share a common connection, there is a bulk resistance between all NMOSs in the IC. This is very bad for interference between circuits, for example switching digital and sensitive analogue receivers. To alleviate these problems some processes provide a triple-well process. Here a deep NWELL is created in the P substrate, then another PWELL is created inside the DNWELL. NMOS devices can then be created in the new PWELL which is isolated from the P substrate.

\begin{figure}[h]
\centering{
\includegraphics[scale=1.00]{diagrams/triple_well.png}
}
\end{figure}

Now another level of PN junctions have been created which need to be carefully biased to ensure that currents don't flow which can cause latchup. A representation of the distrubted diodes in a triple-well NMOS is shown below.

\begin{figure}[h]
\centering{
\includegraphics[scale=1.00]{diagrams/nmos_triplewell.pdf}
}
\end{figure}

\subsubsection{Contact Rings}

One of the most effective layout techniques to ensure high performance is the use of contact rings into the substrate and wells. The rings ensure that a strong connection to the bulk potentional is created. It also helps reduce interference coupling between different sections of the IC by creating lower impedance sinks or minority charge collectors depending on the relative doping of the rings.

It is good practise to place a continous contact ring around all wells, collections of devices and the overall block in the substrate.

\begin{figure}[h]
\centering{
\includegraphics[scale=0.5]{diagrams/contact_rings.png}
}
\end{figure}

\subsubsection{Practical Notes On Wells}

The layout and structure of the wells is the bedrock that the circuit is created in. Ensure you have a plan for the wells arrangement before you begin. The spacing of wells can also have a large impact on the layout size and arrangement. This is especially true for triple wells which require a large spacing to avoid punchthrough under higher voltages.

\newpage
\section{NGSpice Notes}

\subsection{Saving Operating Parameters}

To save the operating parameters in NGSpice outputs the following syntax needs to be used:

\begin{lstlisting}
    .save all @M.x1.XMcas_p.msky130_fd_pr__pfet_01v8[vdsat]
\end{lstlisting}

The addition of all is to prevent NGSpice \textit{only} saving operating parameter and none of the voltages. Note that the string starts with an M for MOSFET then the dot delimited path to the transistor model. The exact transistor model needs to be used. Finally the operating parameter can be specified in square brackets. This is reqiured for every device and paramter you want to investigate.

Some common parameters:

\begin{lstlisting}
    gm vdsat vds id
\end{lstlisting}

\end{document}


\subsection{Contains Duplicate}\label{subsec:contains-duplicate}
Given an integer array nums, return true if any value appears at least twice in the array,
and return false if every element is distinct.


\documentclass{article}
\usepackage[utf8]{inputenc} %sets input encoding to UTF-8, needed for Polish, Japanese, etc.
\usepackage[T1]{fontenc} %needed for Polish characters
\usepackage{lmodern} %this font handles Polish characters properly
\usepackage[cm]{fullpage} %very small margins (around 1.5cm)
\usepackage{multicol} %use \begin{multicols}{#} for # columns
\usepackage{amssymb} %big bracets and other useful symbols
\usepackage{amsmath} %some mathematical symbols
\usepackage{enumitem} %remove vertical space in itemize with: [noitemsep,nolistsep]

\newcommand{\separator}{\noindent \line(1,0){529}}

\begin{document}

\title{Theory for \emph{Algorithms \& Computability} course on MiNI on WUT}
\date{\today}
\author{Mateusz Bysiek, Computer Science, MiNI, WUT}
\maketitle

\tableofcontents

\pagebreak[4]

%\section{A\&C Theory}

\section{Introductory test}

%\subsubsection{Problem, instance, algorithm}

\begin{multicols}{2}

\paragraph{Problem}
cannot be defined exactly. In most broad meaning it is something unknown for which an answer is
needed, and such answer can be sometimes given in form of an algorithm. Problem can be defined as a
set of questions of similar kind. Problem is a set of instances.

\paragraph{Decision problem} is a problem, for which the answer is either ``Yes'' or ``No''.

\paragraph{Optimization problem} is to find solutions and choose the most optimal (the best, ideal,
etc.) one.

\paragraph{Instance of a problem}
Is a concrete question from a set of questions that make a full problem.

\paragraph{Countability of a set of instances}
\ldots the set of instances of a problem can sometimes be shown to be countable, for example in case
of sorting problem for the set of natural numbers.

\paragraph{Algorithm}
solves all instances of the problem. It is a description of how to solve instances of a problem,
based on very basic operations. It is a sequence of instructions.

\end{multicols}

\separator

%\subsubsection{Operations}

Having a problem $\Pi$, set of instances of that problem $D_\Pi$, and instance $d$ of a problem
$\Pi$, i.e. $d \in D_\Pi$ \ldots

\begin{multicols}{2}

\paragraph{Elementary operation}
is an indivisible operation on two basic data cells. Depending on cost criteria used, it takes unit
or logarithmic time.

%\end{multicols}

%\separator

%\begin{multicols}{2}

\paragraph{Time cost function}
$C_t(d) = $ number of elementary operations' executions for the instance $d$.
\begin{gather*}
C_t : D_\Pi \rightarrow N
\end{gather*}

Cost function is a function that goes from set of instances of problem to natural numbers. The value
of cost time function is a number of executions of elementary operations needed to solve given
instance of a problem.

\paragraph{Space cost function}
$C_s(d) = $ number of cells of memory used to solve the instance $d$.
\begin{gather*}
C_s : D_\Pi \rightarrow N
\end{gather*}

\paragraph{Dominant operations}
of algorithm $A$ is a set of operations that are performed
if any other operation of $A$ is performed.

In other words, $\exists k_1, k_2: k_1 d_t(d) \leq C_t(d) \leq k_2 d_t(d)$, where $C_t(d)$ is a
value of cost function for $d$, and $d_t(d)$ is number of dominant operations' executions for the
same instance.

\end{multicols}

\separator

%\subsubsection{Complexity}
\begin{multicols}{2}

\paragraph{Pessimistic time complexity}
is the maximum possible result of the time cost function for a given size of input $n$,
therefore time complexity is a function of $n$.
\begin{gather*}
T_p : | D_\Pi | \rightarrow N \\
T_p(n) = \sup \left\{ C_t(d) : d \in D_\Pi \wedge |d| = n \right\}
\end{gather*}

\paragraph{Average time complexity}
\begin{gather*}
T_a : | D_\Pi | \rightarrow R_+ \\
T_a(n) = \sum_{d \in D_\Pi, |d|=n} C_t(d) * pr(d)
\end{gather*}

Where $pr(d)$ is a probability of encountering a specific instance $d$. In case of average
complexity the set of all instances $D_\Pi$ is finite.

\paragraph{Pessimistic space complexity}
is the maximum possible result of the space cost function for a given size of input $n$,
therefore space complexity is a function of $n$.
\begin{gather*}
S_p : | D_\Pi | \rightarrow N \\
S_p(n) = \sup \left\{  C_s(d) : d \in D_\Pi \wedge |d| = n \right\}
\end{gather*}

\end{multicols}

\separator

%\subsubsection{Cost criteria}

\begin{multicols}{2}

\paragraph{Uniform cost criteria}
Elementary operation needs constant time $t = 1$, ``unit time''.

\paragraph{Logarithmic cost criteria}
Sum of lengths of arguments (or logarithms of their values) is the time needed to perform
an elementary operation.

\end{multicols}

\pagebreak[4]

\section{Test \#1}

\subsection{RAM - random access memory machines}

\begin{itemize}[noitemsep,nolistsep]
  \item Labels: LABEL
  \item Functions: add, del, clr, $\leftarrow$, jmp, jmp$n$ ($n \in \Sigma$), continue
\end{itemize}

%Example implementations of few algorithms for RAM machines

% \begin{multicols}{2}
%
% \begin{minted}[mathescape,linenos,numbersep=5pt,gobble=0,framesep=2mm,xleftmargin=5pt]{c++}
%    clr R
%    clr Q
%    P jmp0 ZERO
%    P jmp1 ONE
%    COPY_RESULT
% ZERO
%    del P
%    jmp WR_Q_0
% ONE
%    del P
%    jmp WR_Q_1
% WR_R_0
% WR_R_1
% WR_Q_0
% WR_Q_1
% \end{minted}
% \begin{minted}[mathescape,linenos,numbersep=5pt,gobble=0,framesep=2mm,xleftmargin=5pt]{c++}
% MOVE_Q_TO_R
% MOVE_R_TO_Q
% COPY_RESULT
%    Q jmp0 COPY_Q
%    Q jmp1 COPY_Q
%    P $\leftarrow$ R
%    jmp END
% COPY_Q
%    P $\leftarrow$ Q
%    jmp END
% END
%    clr R
%    clr Q
%    continue
% \end{minted}
% \end{multicols}

\subsection{PRF - primitive recursive functions}
Proofs that given functions are primitive recursive.

\paragraph{Addition} $+(x,y) = x+y$
\begin{itemize}[noitemsep,nolistsep]
  \item $+(x,0) = x = g(x) = P_{1,1}(x)$
  \item $g = P_{1,1}$
  \item $+(x,y+1) = +(x,y)+1 = h(x,y,+(x,y)) = S(P_{3,3}(x,y,+(x,y)))$
  \item $h = S \circ P_{3,3}$
\end{itemize}

\paragraph{Multiplication} $*(x,y) = x*y$
\begin{itemize}[noitemsep,nolistsep]
  \item $*(x,0) = x = g(x) = P_{1,1}(x)$
  \item $g = P_{1,1}$
  \item $*(x,y+1) = *(x,y)+x = h(x,y,+(x,y)) = +(P_{3,3}(x,y,+(x,y)),P_{3,1}(x,y,+(x,y)))$
  \item $h = + \circ (P_{3,3},P_{3,1})$
\end{itemize}

\paragraph{Factorial} $!(x) = x!$
\begin{itemize}[noitemsep,nolistsep]
  \item $!(0) = 1 = g() = S(Z_0())$
  \item $g = S \circ Z_0$
  \item $!(x+1) = !(x)*(x+1) = g(x,!(x)) = *(P_{2,2}(x,!(x)),S(P_{2,1}(x,!(x))))$
  \item $g = * \circ (P_{2,2}, S \circ P_{2,1})$
\end{itemize}

\paragraph{Exponentiation} $exp(x,y) = x^y$
\begin{itemize}[noitemsep,nolistsep]
  \item $exp(x,0) = 1 = g(x) = S(Z_1(x))$
  \item $g = S \circ Z_1$
  \item $exp(x,y+1) = exp(x,y)*x = g(x,y,exp(x,y)) = *(P_{3,3}(x,y,exp(x,y)),P_{3,1}(x,y,exp(x,y)))$
  \item $g = * \circ (P_{3,3}, P_{3,1})$
\end{itemize}

\paragraph{Predecessor} $pred(x) =
\begin{cases}
x-1 \mbox{ if } x>0 \\
0 \mbox{ otherwise}
\end{cases}$
\begin{itemize}[noitemsep,nolistsep]
  \item $pred(0) = 0 = g() = Z_0()$
  \item $g = Z_0$
  \item $pred(x+1) = x = h(x) = P_{1,1}(x)$
  \item $h = P_{1,1}$
\end{itemize}

\paragraph{Limited difference} $ld(x,y) =
\begin{cases}
x-y \mbox{ if } x>y \\
0 \mbox{ otherwise}
\end{cases}$
\begin{itemize}[noitemsep,nolistsep]
  \item $ld(x,0) = x = g(x) = P_{1,1}(x)$
  \item $g = P_{1,1}$
  \item $ld(x,y+1) = ld(x,y)-1 = h(x,y,ld(x,y)) = pred(P_{3,3}(x,y,ld(x,y)))$
  \item $h = pred \circ P_{3,3}$
\end{itemize}

\paragraph{Sign} $sg(x) = \begin{cases}
0 \mbox{ if } x=0 \\
1 \mbox{ otherwise}
\end{cases}$
\begin{itemize}[noitemsep,nolistsep]
  \item $sg(0) = 0 = g() = Z_0()$
  \item $g = Z_0$
  \item $sg(x+1) = 1 = h(x) = S(Z_1(P_{1,1}(x)))$
  \item $h = S \circ Z_1 \circ P_{1,1}$
\end{itemize}

% \paragraph{Comparison} $gt(x,y)$
%
% $lt(x,y)$

\paragraph{Quotient} $quo(x,y) =
\begin{cases}
\frac{y}{x} \mbox{ if } x>0 \\
0 \mbox{ otherwise}
\end{cases}$
\begin{itemize}[noitemsep,nolistsep]
  \item $quo(x,y) = \mu_{0 \leq i \leq y} \left[ gt(*(x,S(z)),y) \right] $
  %\item $g = Z_1$
  %\item $ $
  %\item $ $
\end{itemize}

% \paragraph{Bounded sum}

% \paragraph{Bounded product}

\paragraph{Bounded minimum} $f(x_1,\ldots,x_n,y) =
\mu_{0 \leq i \leq y} \left[ k(x_1,\ldots,x_n,i) = 0 \right] = $

$\begin{cases}
\mbox{minimal value, for which } k(x_1,\ldots,x_n,i) = 0 \\
0 \mbox{ if such } i \mbox{ doesn't exist}
\end{cases}$
% \begin{itemize}[noitemsep,nolistsep]
%   \item $quo(x,y) = \mu_{0 \leq i \leq y} \left[ gt(*(x,S(z)),y) \right] $
% \end{itemize}

% \paragraph{XOR}
% \begin{itemize}[noitemsep,nolistsep]
%   \item
%   \item
%   \item
%   \item
% \end{itemize}

% \paragraph{Equality test}
% \begin{itemize}[noitemsep,nolistsep]
%   \item
%   \item
%   \item
%   \item
% \end{itemize}

% \paragraph{Primality test}

% \paragraph{Divisibility test}
% \begin{itemize}[noitemsep,nolistsep]
%   \item
%   \item
%   \item
%   \item
% \end{itemize}

\pagebreak[4]

\subsection{A(x,y) - Ackerman function}
\begin{gather*}
A(x,y) = \begin{cases}
A(0,y) = y+1 \\
A(x+1,0) = A(x,1) \\
A(x+1,y+1) = A(x,A(x+1,y))
\end{cases}
\end{gather*}
\noindent \textbf{3 basic properties}:
\begin{enumerate}[noitemsep,nolistsep]
  \item it is increasing
  \item it has values greater than the arguments
  \item first parameter affacts the result much more than the second one, i.e. $A(x+2,y) > A(x,2y) $
\end{enumerate}

\subsubsection{Ackerman funcion is not a PRF}
\textbf{Theorem}: Ackerman funcion is not a PRF, because:
\begin{gather*}
\forall(f(x_1,\ldots,x_n))\exists k \mbox{ such that } A(k,\max\{x_1,\ldots,x_n\}) > f(x_1,\ldots,x_n)
\end{gather*}
where $f$ is any primitive recursive function of $n$ arguments.

\textbf{Proof} by induction
\begin{enumerate}
  \item We show that such constant exists for all 3 basic PRFs.
  \begin{enumerate}[noitemsep,nolistsep]
    \item $A(0,\max\{x_1,\ldots,x_n\}) > Z_n(x_1,\ldots,x_n)$
    \item $A(1,x) > S(x)$
    \item $A(0,\max\{x_1,\ldots,x_n\}) > x = P_{n,i}(x_1,\ldots,x_n)$ where $i$ is index
    of argument with maximum value.
   \end{enumerate}
  \item We show that such constant exists for all compositions of those functions, and, therefore
  for any PRF.

  %emph
  \textbf{Lemma 1}

  If for PRF $g,f_1,\ldots,f_n$ such that $g: \mathbb{N}^m \rightarrow \mathbb{N}$ and
  $f_{1 \ldots n}: \mathbb{N}^n\rightarrow \mathbb{N}$ we have constants defined in the theorem,
  then there is a constant $k$ such that:

  $A(k,\max\{x_1,\ldots,x_n\}) > [g \circ (f_1,\ldots,f_n)](x_1,\ldots,x_n)$

  \textbf{Lemma 2}

  If for PRF $g,h$ such that $g: \mathbb{N}^n \rightarrow \mathbb{N}$ and
  $h: \mathbb{N}^{n+2} \rightarrow \mathbb{N}$ we have constants defined in the theorem,
  then there is a constant $k$ such that:

  $A(k,\max\{x_1,\ldots,x_{n+1}\}) > f(x_1,\ldots,x_{n+1})$ where $f$ is obtained by PRS applied to
  $g$ and $h$.
\end{enumerate}

\subsubsection{Ackerman function is recursive}
\textbf{Theorem}: Ackerman function is recursive, because we can construct it using PRS and
unbounded minimum.

When we evaluate for example $A(2,1)$:
\begin{gather*}
A(2,1) = A(1,A(2,0)) = A(1,A(1,1)) = A(1,A(0,A(1,0))) = \ldots = A(0,4) = 5
\end{gather*}
We can convert the notation by omitting the $A$'s to the following:
$(2,1) = (1,2,0) = (1,1,1) = (1,0,1,0) = \ldots = (0,4) = 5$ \\
and then encode it using Gödel Encoding: $2^2 3^2 5^1 = 2^3 3^1 5^2 7^0 = 2^3 3^1 5^1 7^1
= 2^4 3^1 5^0 7^1 11^0 = \ldots = 2^2 3^0 5^4 = 2^1 3^5$

Then, we may define the Trace function $\psi(x,y,n)$ being the Gödel number of $n$-th step of
computation of $A(x,y)$. Then, we define $n_0$ as a step, in which we reach the final result.

Full definition:
\begin{gather*}
\psi(x,y,n) = \begin{cases}
\mbox{Gödel number of } n\mbox{-th step of computation of } A(x,y) \\
\psi(x,y,n_0) \mbox{ for } n > n_0
\end{cases}
\end{gather*}

$E(n,z)$ is the exponent of Gödel number $z$ at its $n$-th place. Example: $E(0,2^2 3^5 5^1) =
E(0,(2,5,1)) = 2$ means that exponent of 2 (being 0th term of Gödel-encoded number) is equal to 2.
$E(1,2^2 3^4 5^3) = E(1,(2,4,3)) = 4$ means that exponent of 3 (being 1st term of Gödel-encoded
number) is equal 4.
\begin{gather*}
\eta(x,y) = n_0 = \mu_{n \geq 0} \left[ L(\psi(x,y,n)) = 1 \right]
\end{gather*}
\begin{gather*}
A(x,y) = E(1,\psi(x,y,\eta(x,y)))
\end{gather*}

\pagebreak[4]

\section{Test \#2}

Sorry, this section will probably not be updated, because I already passed this subject ;)

\subsection{Recursive functions}

\subsection{Classes of languages, problems, functions}

\paragraph{Universal language}

\paragraph{Diagonal language}

\paragraph{Equivalence of groups of classes of languages, problems, functions}

\subsection{Cook's theorem}

\subsection{P vs. NP}

\end{document}