\documentclass[letterpaper]{article}

\usepackage{fullpage}
\usepackage{nopageno}
\usepackage{amsmath}
\usepackage{amssymb}
\allowdisplaybreaks

\newcommand{\abs}[1]{\left\lvert #1 \right\rvert}

\begin{document}
\title{Homework}
\date{September 17, 2014}
\author{Jon Allen}
\maketitle
Section 1.3: \#12, 20
Section 2.1: \# 18, 8
\renewcommand{\labelenumi}{1.\arabic{enumi}}
\renewcommand{\labelenumii}{\arabic{enumii}.}
\renewcommand{\labelenumiii}{(\alph{enumiii})}
\begin{enumerate}
\setcounter{enumi}{2}
\item
\begin{enumerate}
\setcounter{enumii}{11}
%1.2 12
\item
Show that $4\cdot(n^2+1)$ is never divisible by 11.
\subsubsection*{proof}
%We prove the result by assuming that 11 divides $4\cdot(n^2+1)$ and finding a contradiction.
First we note that $\gcd(4,11)=1$ and 11 is prime, so then if $11|4\cdot(n^2+1)$ then by the fundamental theorem of arithmetic, $11|(n^2+1)$.
That is to say
\begin{align*}
  n^2+1&\equiv0\mod11\\
  n^2&\equiv-1\mod11\\
  n^2&\equiv10\mod11\\
\end{align*}
Noting that $\mathbb{Z}_{11}$ partitions $\mathbb{Z}$ we see that there must exist some $[a]_{11}\in\mathbb{Z}_{11}$ such that $[a^2]_{11}=[10]_{11}$ So looking at all the elements of $\mathbb{Z}_{11}$:
\begin{align*}
  [0^2]&=[0]&
  [1^2]&=[1]&
  [2^2]&=[4]&
  [3^2]&=[9]&
  [4^2]&=[5]&
  [5^2]&=[3]\\
  [6^2]&=[3]&
  [7^2]&=[2]&
  [8^2]&=[-2]=[9]&
  [9^2]&=[4]&
  [10^2]&=[1]&
\end{align*}
We see by exhaustion that $[a]_{11}\not\in\mathbb{Z}_{11}$ such that $[a^2]_{11}=[10]_{11}$ and so $11$ does not divide $4\cdot(n^2+1)$.
$\Box$
\setcounter{enumii}{19}
%1.2 20
\item
Solve the following system of congruences.
\begin{align*}
  2x&\equiv 5\mod 7&3x&\equiv4\mod 8
\end{align*}
\emph{Hint:} First reduce to the usual form.
\begin{align*}
  2x&\equiv 5\mod 7&3x&\equiv4\mod 8\\
  \gcd(2,7)&=1&\gcd(3,8)&=1\\
  \intertext{So both congruencies have one solution}
  c\cdot2&\equiv1\mod 7&c\cdot3&\equiv1\mod8\\
  4\cdot2&\equiv1\mod 7&3\cdot3&\equiv1\mod8\\
  x&\equiv5\cdot4\mod7&x&\equiv3\cdot4\mod8\\
  x&\equiv6\mod7&x&\equiv4\mod8\\
\end{align*}
Now because $\gcd(7,8)=1$ we can apply the Chinese Remainder Theorem.
\begin{align*}
  7a+8b&=1\\
  7(-1)+8(1)&=1\\
  4(7)(-1)+6(1)(8)&=48-28=20\text{ is a specific solution}\\
  20+7\cdot8t&=20+56t\text{ is all solutions}
\end{align*}
\end{enumerate}
\renewcommand{\labelenumi}{2.\arabic{enumi}}
\setcounter{enumi}{0}
\item
\begin{enumerate}
\setcounter{enumii}{7}
%2.1 8
\item
Which of the following formulas define functions from the set of rational numbers into itself? (Assume in each case the $n,m$ are integers and that $n$ is nonzero.)
\begin{enumerate}
\item
$\displaystyle f\left(\frac{m}{n}\right)=\frac{m+1}{n+1}$

Not a function from $\mathbb{Q}\to\mathbb{Q}$ because when $n=-1$ there is no image.
\item
$\displaystyle g\left(\frac{m}{n}\right)=\frac{2m}{3n}$

This is a function because rational numbers are closed under multiplication so for any $q\in\mathbb{Q}$ we know that $\frac{2}{3}q\in\mathbb{Q}$
\item
$\displaystyle h\left(\frac{m}{n}\right)=\frac{m+n}{n^2}$

This is not a function. Counterexample: $\frac{1}{2}=\frac{2}{4}$. $\frac{1+2}{2^2}=\frac{3}{4}\ne\frac{2+4}{4^2}=\frac{6}{16}=\frac{3}{8}$. $\frac{1}{2}$ has more than one image so the map is not well defined and not a function.
\item
$\displaystyle k\left(\frac{m}{n}\right)=\frac{(m-n)^2}{n^2}$

$\displaystyle \frac{(m-n)^2}{n^2}=\frac{m^2-2mn+n^2}{n^2}=\left(\frac{m}{n}\right)^2-2\frac{m}{n}+1$. Looks like a good function. It will have the same result independent of representation of the rational number, and has an image for every element of $\mathbb{Q}$.
\item
$\displaystyle p\left(\frac{m}{n}\right)=\frac{4m^2}{7n^2}-\frac{m}{n}$

Is a function of rationals. They are closed under multiplication and subtraction. all equivalent elements will have the same image, regardless if their representation in terms of $m,n$.
\item
$\displaystyle q\left(\frac{m}{n}\right)=\frac{m+1}{m}$

Not a function. No representation of zero has an image. For example $\frac{0}{1}$ does not have an image as $\frac{1}{0}$ is undefined.
\end{enumerate}
\setcounter{enumii}{17}
%2.1 18
\item
Let $A$ be a nonempty set, and let $f:A\to B$ be a function. Prove that $f$ is one-to-one if and only if there exists a function $g:B\to A$ such that $g\circ f=1_A$
\subsubsection*{proof}
Lets start by assuming that $f$ is a one to one function. Because $f$ is a function, we know that for every $x\in A$ there exists some $x'\in B$. Furthermore, because $f$ is one to one, we know that $x'$ is unique. Now we simply define $g(x')=x$. If $B$ has more elements than $A$ then we can define those elements that aren't images of $A$ under $f$ to map to random $a\in A$. Now we see that $g(f(x))=g(x')=x$ and so we've found a function that satisfies our result.

Now let us assume that the function $f$ is not one to one. Because $f$ is a function, we can't have any elements of $A$ map to more than one element in $B$. Therefore $\left\lvert f\right\rvert\le \left\lvert A\right\rvert$. Now because $f$ is not one to one, we know that there are two elements in $A$ that have the same image in $B$. This makes our cardinality inequality strict: $\left\lvert f\right\rvert<\left\lvert A\right\rvert$. This means that if we have a function $g:B\to A$ and feed it the images created by $f$ it will only be able to spit out at most $\left\lvert A\right\rvert-1$ images of it's own. So we have $\left\lvert g\circ f\right\rvert<\left\lvert A\right\rvert$. Because $\left\lvert g\circ f\right\rvert\ne\left\lvert A\right\rvert$ it is certain that $g\circ f\ne1_A$
$\Box$
\end{enumerate}
\end{enumerate}
\end{document}


\documentclass[a4paper,11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{aas_macros}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{enumitem}
\usepackage{listings}
\usepackage{xcolor} % kvuli barvickam v pythonim textu
\usepackage{xspace}
\usepackage{physics}
\usepackage[margin=2.5cm]{geometry} % nastavuje sirku okraju stranky

\author{Anna Juranova}
\title{Ferrers Potential in galpy}

% Dots following numbers of section:
\renewcommand{\thesection}{\arabic{section}.}
\renewcommand{\thesubsection}{\thesection\arabic{subsection}.}
% Only one letter wide space between the number and the title of the chapter:
\makeatletter \def\@seccntformat#1{\csname the#1\endcsname\hspace{1ex}} \makeatother

\lstdefinestyle{customc}{
	%belowcaptionskip=1\baselineskip,
	breaklines=true,
	%frame=L,
	%xleftmargin=\parindent,
	language=Python,
	showstringspaces=false,
	keepspaces=true,
	basicstyle=\footnotesize\ttfamily,
	keywordstyle=\bfseries\color{green!40!black},
	commentstyle=\itshape\color{purple!40!black},
	%breakatwhitespace=true,
	identifierstyle=\color{blue},
	stringstyle=\color{orange},
}

\lstset{escapechar=@,style=customc}

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

	\begin{center}
		\huge{Ferrers Potential in galpy}\\
	\end{center}

	\section{Variables and Units}
%	VARIABLES AND UNITS
           $ \mathrm{amp} $ -- Amplitude to be applied to the potential (default: 1); can be a Quantity with units of mass or $ G \times \mathrm{mass} $\\
           $ a $ -- Scale radius (can be Quantity)\\
           $ n $ -- Power of Ferrers density ($ n > 0 $), not necessarilly integer, calculations generally fail for $ n > 4 $\\
           $ b $ -- y-to-x axis ratio of the density, usually $ b < 1 $\\
           $ c $ -- z-to-x axis ratio of the density, usually $ c < b $\\
           $ \Omega_b $ -- pattern speed of the bar, $ \vec{\Omega}_b = \hat{e}_z\,\Omega_b $\\
           $ \mathrm{pa} $ -- If set, the initial position angle of the bar in the xy plane measured from x (rad or Quantity)\\
           normalize -- if True, normalize such that $ v_c(1.,0.)=1. $, or, if given as a number, such that the force is this fraction of the force necessary to make $ v_c(1.,0.)=1. $.\\
           $ r_0 $, $ v_0 $ -- Distance and velocity scales for translation into internal units (default from configuration file)\\
           Variables $ \alpha $	appearing in the code in form $ \_\alpha2 $ carry the value of $ \alpha^2 $ initialized in the begining of the class \_\_init\_\_ function.

	\section{Functions}
	 ...implemented for rotating potential in a frame of reference in which the bar lies aligned with x axis.)
%	FUNCTIONS

		\subsection{Potential} % POTENTIAL ITSELF
			Purpose: Evaluation of the bar potential as a function of cartesian coordinates in corotating frame of reference. \\
   		\begin{equation}
   		\Phi(\vec{x}) = \frac{-\mathrm{amp}\,b\,c}{4(n+1)}\,\int_{\lambda}^{\infty} A^{n+1}(\tau)\,\mathrm{d} \tau
   		\end{equation}
   			where
		\begin{equation}
		A^{\nu}(\tau) = \frac{\left(1- \sum_{i=1}^{3} \frac{x_i^2}{\tau + a_i^2}\right)^{\nu}}{[(\tau + a^2)(\tau + a^2b^2)(\tau + a^2c^2)]^\frac{1}{2}}
		\end{equation}
   			is part of an integrand which is repeatedly used in following functions. Lower limit for the integral based on definition of the density distribution is given by relation:

		\begin{equation}
		\lambda = \begin{cases}
		\text{unique positive solution of} \, m^2(\lambda)=1, &  \text{for}\ m \geq 1\\
		\lambda = 0\,, & \text{for}\ m < 1
		\end{cases} \\
		\end{equation}
		where $ m^2(\lambda) = \sum_{i=1}^{3} \frac{x_i^2}{\lambda + a_i^2} $.

	% they have the units in density \rho_0 wrong in paper one (probably because of the constants before the physical variables obtained somehow??) but still! [\rho] \neq s^{-2} !!
		\subsection{Density} % DENSITY
			Purpose: Evaluation of the density as a function of (x,y,z) in the aligned coordinate frame from cylindrical coordinates given as input\\
		\begin{equation*}
		\rho(m^2) = \begin{cases}
		\frac{\mathrm{amp}}{4\,\pi\,a^3}\,(1-(m/a)^2)^n, & \text{for}\, m < a \\
		0, & \text{for}\, m \geq a
		\end{cases}
		\end{equation*}
			where
		\begin{equation}
		m^2 = x^2 + \frac{y^2}{b^2}+\frac{z^2}{c^2}
		\end{equation}

		\subsection{Force} % FORCE
			Evaluation of the x component of the force as a function of (x,y,z) in the aligned coordinate frame, which is used for evaluation of the force in cylindrical coordinates and then in orbit integration; does not take into account bar's rotation or initial position and therefore shall not be used directly.\\
   		\begin{equation}
   		F_i = \frac{-\mathrm{amp}\,b\,c}{2}\, \int_{\lambda}^{\infty} \frac{x_i}{a_i^2 + \tau} A^{n}(\tau)\,\mathrm{d} \tau
   		\end{equation}
		where
		\begin{equation*}
			a_1 = a,~ a_2 = ab,~a_3 = ac
		\end{equation*}

		% SECOND DERIVATIVE
		\subsection{General Second Derivative}
			General 2nd derivative of the potential as a function of (x,y,z) in the aligned coordinate frame \\
		\begin{equation}
		\Phi_{\mathrm{ij}} = -\frac{1}{4}\,b\,c \,\Phi'_{\mathrm{ij}}
		\end{equation}

		% INTEGRATION FOR SECOND DERIVATIVE
		\subsection{Integration for Second Derivative}
			Integral that gives the 2nd derivative of the potential in x,y,z \\

			\noindent The derivative is generally $ \pdv{\Phi}{x_i}{x_j} $; for $ i=j $ the integral to be evaluated is:

		\begin{equation}
		\Phi'_{\mathrm{ii}} = \int_{\lambda}^{\infty}
		 \frac{4\,n\,x_i^2}{(\tau + a_i^2)^2} \,A^{\nu-1}(\tau)
		- \frac{2}{\tau + a_i^2} A^{\nu}(\tau) \mathrm{d}\,\tau
		\end{equation}

			\noindent In all other cases, the integral has this form:
		\begin{equation}
		\Phi'_{\mathrm{ij}} = \int_{\lambda}^{\infty}
		\frac{4\,n\,x_i\,x_j}{(\tau + a_i^2)(\tau + a_j^2)} A^{\nu-1}(\tau) \mathrm{d}\tau
		\end{equation}

	% INERTIAL FRAME
		\subsection{Second Derivative in Nonrotating FoR}
			Transformation of the second derivative into the frame of reference which is corotating with the potential\\
		\begin{equation}
			\Phi_{x'x'} = \cos[2](\Omega_b t)\Phi_{xx} - 2 \sin(\Omega_b t) \cos(\Omega_b t)\Phi_{xy} + \sin[2](\Omega_b t)\Phi_{yy}\\
		\end{equation}

		\begin{equation}
			\Phi_{y'y'} = \cos[2](\Omega_b t)\Phi_{yy} + 2 \sin(\Omega_b t) \cos(\Omega_b t)\Phi_{xy} + \sin[2](\Omega_b t)\Phi_{xx}
		\end{equation}

		\begin{equation}
			\Phi_{z'z'} = \Phi_{zz}
		\end{equation}

		\begin{equation}
			\Phi_{x'y'} = [\cos[2](\Omega_b t) - \sin[2](\Omega_b t)]\Phi_{xy} + \sin(\Omega_b t) \cos(\Omega_b t)[\Phi_{xx} - \Phi_{yy}]
		\end{equation}

		\begin{equation}
			\Phi_{x'z'} = \cos(\Omega_b t)\Phi_{xz} - \sin(\Omega_b t) \Phi_{yz}
		\end{equation}

		\begin{equation}
			\Phi_{y'z'} = \cos(\Omega_b t)\Phi_{xz} + \sin(\Omega_b t) \Phi_{yz}
		\end{equation}

	%MILKY WAY BAR POTENTIAL
	\section{Milky Way Bar Potential}

		Values of parameters used for further work are set as the final state values in \cite{MachadoManos:2016}, that is:

		\begin{equation*}
			a = 8\,kpc,~
			b = 0.35,~
			c = 0.2375,~
			\mathrm{amp} = 3.3 \times 10^{10}\, M_{\odot},~
			\Omega_b = 10\,km/s/kpc.
		\end{equation*}

		\nocite{*} % Insert publications even if they are not cited in the poster

		\small{\bibliographystyle{unsrt}
			\bibliography{sample}\vspace{0.01in}}

\end{document}


\section{Evaluation}\label{sec:evaluation:clustering}
Clustering for \bslongs{} has two aspects that we must evaluate: how pure the \bslongs{} (clusters) are and how many of the \isols{} in \cplop{} end up in a cluster (as opposed to noise).
The former tests whether the \ecoli{} strains stay relatively unique to the \spec{} from which they come from, the core theory of library-based \mst{}.
The latter tells us how effective
\autoref{sec:methodology:clustering} describes how we can use clustering as a \mst{} method --- cluster an unknown \isol{} along with the \isols{} in \cplop{} and classify it as the most plural species of the cluster.
Cluster purity can easily gauge how effective this technique is at \mst{}, since the concept readily summarizes to how pure, from a \spec{} perspective, a cluster is.
\Dbased{} clustering algorithms such as \dbscan{} may not cluster every datapoint, as mentioned in \autoref{sec:background:dbscan},  labeling some datapoints (\isols{}) as noise and thus leaving them unclustered.
%Thus, it is crucial that our \mst{} method has the ability to assert a classification on any \isol{} we provide it, driving us to use \kNN{} for classification.
%Nevertheless, by applying \dbscan{} to \cplop{} \isols{}, we gained insight into the nature of \ecoli{} strains that we discuss in \autoref{sec:results:clustering}.

\subsection{Cluster and Clustering Purity}
In this paper we look at the results of clustering \cplop{} data using this algorithm from the perspective of cluster purity. We call a cluster (\bslong{}) \textit{100\% pure}
if all isolates that belong to it come from the same \spec{}.

Of interest to us is the following information:
\begin{enumerate}
    \item The number of 100\% pure clusters and the percentage of bacterial isolates from \cplop{} clustered into pure clusters.
    \item The structure of impure clusters: specifically, whether a dominant \spec{} can
    be clearly identified in each cluster.
    \item Coverage: the total number of \cplop{} isolates found to belong to a strain.
    \item MST Accuracy: the percentage of isolates for which the strain-based MST procedure produces the correct response.
\end{enumerate}
Thus, our core measure is \textit{cluster purity}, the proportion of a cluster that comes from the most plural \spec{} of that particular cluster.
\index{cluster purity}
A \textit{100\% pure cluster} is a cluster which only contains data points (\isols{}) with the same class label (same \spec{} of origin).

Consider a cluster $C=\{c_1,\ldots, c_K\}$. Let $s(c)$ refer to the species of isolate $c$.
Let $m$ be the plurality species label for data points in $C$, and let the total number of points in
$C$ with $s(c) = m$ be $s_m$. Then the \textit{individual cluster purity} $\nu$ of cluster $C$ is:
\[
    \nu(C) = \frac{s_m}{K}
\]

In addition to computing the purity of individual clusters we want to have an understanding of the overall purity on the entire dataset.
Given a \textit{clustering} $\mathcal{C} = \{C_1,\dots,C_n\}$ on a dataset, we define the size $\mathcal{M}$ of the set of clusters:
\index{clustering}
\begin{equation}\label{eq:num_isols}
\mathcal{M} = \sum_{i = 1}^{n} |C_i|
\end{equation}
The \textit{overall clustering purity} is:
\index{overall clustering purity}
\begin{equation}\label{eq:overall_clustering}
\sum_{i=1}^{n} \frac{|C_i|}{\mathcal{M}}\cdot\nu(C_i)
\end{equation}
One can think of \eqref{eq:overall_clustering} as a form of weighted arithmetic mean of the purities, where the size of the cluster adds more weight to the value.

\subsection{Clustering Coverage}

% COVERAGE --------------------
%\subsection{Clustering Coverage}
\label{sec:validation:coverage}
Coverage of the dataset is important to an effective \mst{} method.
The density-based clustering method we use has one key disadvantage: a clustering run  with the parameter \minneigh{}, treats all points that do not fit into a cluster of size  of at least \minneigh{} as noise.
This means that as the value of \minneigh{} grows, so will the number of \isols{} that do not cluster into a strain.

Given the parameter \minneigh{} of the clustering algorithm, we collect the following four measures, that collectively represent the breakdown of all data points (\isols{}) in \cplop{}:
\begin{enumerate}
    \item \textit{Noise.} Number/percentage of \isols{} clustered as noise points.
    \item \textit{Misses.} Number/percentage of \isols{} from  minority species
    in impure clusters.
     \item \textit{Hits.} Number/percentage of \isols{} from plurality species in
     impure clusters.
     \item \textit{Pure points.} Number/percentage of \isols{} in 100\% pure clusters.
\end{enumerate}
\index{noise}
\index{misses}
\index{hits}
\index{pure points}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% fphw Assignment
% LaTeX Template
% Version 1.0 (27/04/2019)
%
% This template originates from:
% https://www.LaTeXTemplates.com
%
% Authors:
% Class by Felipe Portales-Oliva (f.portales.oliva@gmail.com) with template
% content and modifications by Vel (vel@LaTeXTemplates.com)
%
% Template (this file) License:
% CC BY-NC-SA 3.0 (http://creativecommons.org/licenses/by-nc-sa/3.0/)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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

\documentclass[
	12pt, % Default font size, values between 10pt-12pt are allowed
	%letterpaper, % Uncomment for US letter paper size
	%spanish, % Uncomment for Spanish
]{fphw}

% Template-specific packages
\usepackage[T1]{fontenc} % Output font encoding for international characters
\usepackage{fontspec,unicode-math} % Required for using utf8 characters in math mode
\usepackage{parskip}  % To add extra space between paragraphs
\usepackage{graphicx} % Required for including images
\usepackage{booktabs} % Required for better horizontal rules in tables
\usepackage{enumerate}% To modify the enumerate environment
\usepackage{mathtools}% Important mathematicals environments and other things
\usepackage{physics}  % Defines lots of commands for vectors, matrices, derivatives...
\setlength{\parindent}{15pt}
\setlength{\headheight}{22.66pt}

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

\title{Task 3 \\ Frenet's Family} % Assignment title

\author{Emilio Domínguez Sánchez} % Student name

\date{October 22nd, 2020} % Due date

\institute{University of Murcia \\ Faculty of Mathematics} % Institute or school name

\class{Geometría de Superficies} % Course or class name

\professor{Dr. Pascual Lucas Saorin} % Professor or teacher in charge of the assignment

%----------------------------------------------------------------------------------------
%	Definitions
%----------------------------------------------------------------------------------------

\DeclareMathOperator{\proy}{proy}
\newcommand{\R}{\mathbb{R}}
\newcommand{\T}{{\vb{T}}}
\newcommand{\N}{{\vb{N}}}
\newcommand{\B}{{\vb{B}}}
\newcommand{\α}{{\vb*{α}}}
\newcommand{\inner}[2]{\left\langle #1, \; #2 \right\rangle}
\renewcommand{\dv}{\prime}
\newcommand{\ddv}{\dprime}
\newcommand{\dddv}{\trprime}

\begin{document}

\maketitle % Output the assignment title, created automatically using the information in the custom commands above

%----------------------------------------------------------------------------------------
%	ASSIGNMENT CONTENT
%----------------------------------------------------------------------------------------

\section*{Problem}

\begin{problem}
    Given a regular curve $\α : I \subset \R \to \R^3$ in $\R^3$,
its Frenet's family for some $t$ is the family $\Pmqty{κ(t), τ(t), \T(t), \N(t), \B(t)}$,
where $κ(t)$, $τ(t)$, $\T(t)$, $\N(t)$ and $\B(t)$ are
the curvature; the torsion; and the tangent, normal and bi-normal vectors
at the point $\α(t)$.

\noindent
Find the Frenet's family for the curve $\α(t) = \Pmqty{3t-t^3 & 3t^2 & 3t+t^3}$.
\end{problem}

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

\subsection*{Answer}

    We need the derivatives of the curve for our calculations.

\begin{align*}
    \α(t) &= \Pmqty{3t-t^3 & 3t^2 & 3t+t^3}, \\
    \α\dv(t) &= \Pmqty{3-3t^2 & 6t & 3+3t^2} = 3\Pmqty{1-t^2 & 2t & 1+t^2}, \\
    \α\ddv(t) &= \Pmqty{-6t & 6 & 6t} = 6\Pmqty{-t & 1 & t}, \\
    \α\dddv(t) &= \Pmqty{-6 & 0 & 6} = 6\Pmqty{-1 & 0 & 1}.
\end{align*}

    We will start by finding the Fermat's frame ($\T$, $\N$ and $\B$).
$\α\dv(t)$ is already a vector proportional to $\T$ and with the same sense.
We will find a vector proportional to $\N$ and with the same sense
by removing the tangential component from $\α\ddv(t)$.
To keep the notation simple, we will call this vector $\vb{n}$.

\begin{equation*}
    \inner{\α\ddv{t}}{\α\dv(t)} =
    6 \cdot 3 \inner{\Pmqty{-t & 1 & t}}{\Pmqty{1-t^2 & 2t & 1+t^2}} =
    18\Pmqty{3t^3 + 6t + 3t^3} = 36t(1+t^2). \\
\end{equation*}

\begin{multline*}
    \vb{n}(t) = \proy_\N (\α\ddv(t)) = \\
    \α\ddv(t) -
        \frac{\inner{\α\ddv{t}}{\α\dv{t}}}{\norm{\α\dv(t)}^2}
    \α\dv(t) =
    \α\ddv(t) - \frac{36t(1+t^2)}{18(1+t^2)^2} \α\dv(t) = \\
    \α\ddv(t) - \frac{2t}{(1+t^2)} \α\dv(t) =
    \frac{1}{1+t^2} \qty\Big[(1+t^2)\α\ddv(t) - 2t \α\dv(t)] = \\
    \frac{6}{1+t^2} \qty\Big[
        (1+t^2)\Pmqty{-t & 1 & t} - t\Pmqty{1 - t^2 & 2t & 1 + t^2}
    ] = \\
    \frac{6}{1+t^2} \Pmqty{-t(1+t^2) - t + t^3 & (1+t^2) - 2t^2 & t(1+t^2) - t - t^3} = \\
    \frac{6}{1+t^2} \Pmqty{-2t & 1 - t^2 & 0}.
\end{multline*}

    Now that we have two vectors $\α\dv(t)$ and $\vb{n}(t)$
in the same direction and sense than $\T$ and $\N$,
we can find another vector with the same sense and direction as $\B$
using the cross product.

\begin{multline*}
    \vb{b}(t) = \α\dv(t) \times \vb{n} = \\
    3\Pmqty{1-t^2 & 2t & 1+t^2} \times \frac{6}{1+t^2} \Pmqty{-2t & 1 - t^2 & 0} = \\
    \frac{18}{1+t^2} \Pmqty{1-t^2 & 2t & 1+t^2} \times \Pmqty{-2t & 1 - t^2 & 0} = \\
    \frac{18}{1+t^2} \Pmqty{-(1+t^2)(1-t^2) & (1+t^2)(-2t) & (1-t^2)^2 + 4t^2} = \\
    18 \Pmqty{t^2-1 & -2t & 1+t^2}.
\end{multline*}

    All that reamins is to normalize the ortoghonal base $\qty{\α\dv(t), \vb{n}, \vb{b}}$
to obtain $\qty{\T, \N, \B}$.

\begin{align*}
    \T(t) &=
    \frac{\α\dv(t)}{\norm{\α\dv(t)}} =
    \frac{3\Pmqty{1-t^2 & 2t & 1+t^2}}{3\sqrt{2}(1+t^2)} =
    \frac{\sqrt{2}}{2} \Pmqty{\frac{1-t^2}{1+t^2} & \frac{2t}{1+t^2} & 1}, \\
%
    \N(t) &=
    \frac{\vb{n}(t)}{\norm{\vb{n}(t)}} =
    \frac{\Pmqty{-2t & 1 - t^2 & 0}}{\norm{\Pmqty{-2t & 1 - t^2 & 0}}} =
    \frac{\Pmqty{-2t & 1 - t^2 & 0}}{1+t^2} =
    \Pmqty{\frac{-2t}{1+t^2} & \frac{1-t^2}{1+t^2} & 0}, \\
%
    \B(t) &=
    \frac{\vb{b}(t)}{\norm{\vb{b}(t)}} = \\ &
    \begin{multlined}
        \frac{\Pmqty{t^2-1 & -2t & 1+t^2}}{\norm{\Pmqty{t^2-1 & -2t & 1+t^2}}} =
        \frac{\Pmqty{t^2-1 & -2t & 1+t^2}}{\frac{1}{3}\norm{\α\dv(t)}} =
        \frac{\Pmqty{t^2-1 & -2t & 1+t^2}}{\sqrt{2}(1+t^2)} = \\
        \frac{\sqrt{2}}{2}\Pmqty{\frac{t^2-1}{1+t^2} & \frac{-2t}{1+t^2} & 1}.
    \end{multlined}
\end{align*}

    In the last equality, we have writen
$\norm{\Pmqty{t^2-1 & -2t & 1+t^2}} = \frac{1}{3}\norm{\α\dv(t)}$
recognizing the similarity in the coordinates of both vectors,
and we will use $\norm{\vb{b}} = 6\norm{\α\dv(t)}$ whenever it comes in handy.
This is just a coincidence, and not an identity for any curve.

    Lastly, we will use the formulas for the curvature and the torsion.
We will try to make use of the operations that we have already made.
In particular, the relation
$\norm{\α\dv(t) \times \α\ddv(t)} = \norm{\α\dv(t) \times \vb{n}(t)}$
is a useful identity.

\begin{align*}
    κ_{\α}(t) =
    \frac{\norm{\α\dv(t) \times \α\ddv(t)}}{\norm{\α\dv(t)}^3} =
    \frac{\norm{\α\dv(t) \times \proy_\N (\α\ddv(t))}}{\norm{\α\dv(t)}^3} =
    \frac{\norm{\α\dv(t) \times \vb{n}(t)}}{\norm{\α\dv(t)}^3} = \\
    \frac{\norm{\vb{b}(t)}}{\norm{\α\dv(t)}^3} =
    \frac{6\norm{\α\dv(t)}}{\norm{\α\dv(t)}^3} =
    \frac{6}{\norm{\α\dv(t)}^2} =
    \frac{6}{18(1+t^2)^2} =
    \frac{1}{3(1+t^2)^2}.
\end{align*}

\begin{align*}
    τ_{\α}(t) =
    -\frac{\det\Pmqty{\α\dv(t) & \α\ddv(t) & \α\dddv(t)}}
        {\norm{\α\dv(t) \times \α\ddv(t)}^2} =
    -\frac{\qty(\α\dv(t) \times \α\ddv(t)) \cdot \α\dddv(t)}
        {\norm{\α\dv(t) \times \α\ddv(t)}^2} = \\
    -\frac{\vb{b}(t) \cdot \α\dddv(t)}
        {\qty(6\norm{\α\dv(t)})^2} =
    -\frac{18 \Pmqty{t^2-1 & -2t & 1+t^2} \cdot 6\Pmqty{-1 & 0 & 1}}
        {36\cdot 18(1+t^2)^2} = \\
    -\frac{1}{3(1+t^2)^2}.
\end{align*}

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

\end{document}


% !TEX program = xelatex
\documentclass[12pt]{article}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{fancyhdr}
\usepackage{enumerate}
\usepackage{multicol}
\usepackage{bm}
\usepackage{caption}
\usepackage{float}
\usepackage{mathtools}
\usepackage{colortbl}
\usepackage{xcolor}
\usepackage{cancel}
\usepackage{comment}
\usepackage{stmaryrd}
\usepackage{ulem}
\usepackage{alltt}
\usepackage[T1]{fontenc}
\usepackage{rotating}
\usepackage{fancyvrb}
\usepackage{hyperref}
\usepackage{pdfpages}

\usepackage{tikzducks}
\usepackage[document]{ragged2e}
\usepackage{fontspec}
\usepackage{listings}

\newcommand{\Z}{\mathbb{Z}}
\newcommand{\R}{\mathbb{R}}
\newcommand{\N}{\mathbb{N}}
\newcommand{\Q}{\mathbb{Q}}
\newcommand{\K}{\mathbb{K}}
\newcommand{\F}{\mathbb{F}}
\renewcommand{\O}{\mathcal{O}}
\renewcommand{\P}{\mathcal{P}}
\renewcommand{\U}{\mathcal{U}}
\newcommand{\V}{\mathcal{V}}
\newcommand{\W}{\mathcal{W}}
\newcommand{\A}{\mathcal{A}}
\newcommand{\B}{\mathcal{B}}
\newcommand{\E}{\mathbb{E}}
\renewcommand{\L}{\mathcal{L}}

\newcommand{\where}{\text{ where }}
\newcommand{\via}{\text{ via }}
\newcommand{\of}{\circ}
\newcommand{\id}{\mathrm{id}}
\newcommand{\union}{\cup}
\newcommand{\intersect}{\cap}
\newcommand{\inv}{^{-1}}
\newcommand{\lang}{\Sigma^*}
\newcommand{\ceil}[1]{\left\lceil #1 \right\rceil}
\newcommand{\floor}[1]{\left\lfloor #1 \right\rfloor}

\newcommand{\Int}{\displaystyle\int}
\newcommand{\Lim}{\displaystyle\lim}
\newcommand{\Union}{\displaystyle\bigcup}
\newcommand{\Intersect}{\displaystyle\bigcap}
\newcommand{\Sum}{\displaystyle\sum}
\newcommand{\Prod}{\displaystyle\prod}
\newcommand{\Binom}{\displaystyle\binom}

\newcommand{\proj}{\mathrm{proj}}
\newcommand{\Span}{\mathrm{Span}}
\newcommand{\Null}{\mathrm{Null}}
\newcommand{\rank}{\mathrm{rank}}
\newcommand{\chr}{\mathsf{char}}
\newcommand{\Prec}{\mathsf{Prec}}
\newcommand{\dec}{\mathsf{dec}}
\newcommand{\len}{\mathsf{len}}
\newcommand{\nullity}{\mathrm{nullity}}
\renewcommand{\vec}[1]{\mathbf{#1}}
\newcommand{\otherwise}{\text{otherwise}}

\newcommand{\Var}{\mathsf{Var}}
\newcommand{\Skew}{\mathsf{Skew}}
\newcommand{\Binomial}{\mathsf{Binomial}}
\newcommand{\Poisson}{\mathsf{Poisson}}
\newcommand{\Geometric}{\mathsf{Geometric}}
\newcommand{\Bernoulli}{\mathsf{Bernoulli}}
\newcommand{\Exponential}{\mathsf{Exponential}}
\newcommand{\Uniform}{\mathsf{Uniform}}
\newcommand{\Normal}{\mathsf{Normal}}

\newcommand{\WP}{\text{w.p. }}

\newcommand{\dx}{\, dx}
\newcommand{\dy}{\, dy}
\newcommand{\du}{\, du}
%\newcommand{\dp}{\, dp}
\newcommand{\ddx}{\frac{d}{dx}}

\newcommand{\highlight}[1]{%
  \colorbox{yellow!30}{$\displaystyle#1$}}
\newenvironment{amatrix}[1]{%
  \left[\begin{array}{@{}*{#1}{c}|c@{}}
}{%
  \end{array}\right]
}
\renewcommand{\arraystretch}{1.3}

\oddsidemargin0cm
\topmargin=-1cm
\textwidth16.5cm
\textheight22.5cm

\setmonofont[
  Contextuals={Alternate}
]{Fira Code}

\makeatletter
\def\verbatim@nolig@list{}
\makeatother

\lstset{
  basicstyle={\small\ttfamily},
  columns=fullflexible,
  keepspaces=true,
  mathescape,
  numbers=left,
  stringstyle=\color[HTML]{a31515},
  commentstyle=\color[rgb]{0,0.5,0},
  keywordstyle=\color[HTML]{AF00DB},
  keywordstyle=[2]\color[rgb]{0,0,0.7},
  keywordstyle=[3]\color[HTML]{267F99},
  morecomment=[s]{(*}{*)},
  morecomment=[l]{--},
  keywords={let,letp,in,end,if,then,else,iter,with,fn},
  keywords=[2]{true,false,nil,cons},
  keywords=[3]{int,unit,list,<>, bool}
}

\newcommand{\code}{\lstinline}

\newcounter{questioncounter}
\newcommand{\question}[1]{
\stepcounter{questioncounter}
\subsection*{Question #1}
}

\newcounter{taskcounter}[questioncounter]
\newcommand{\task}[1]{
\stepcounter{taskcounter}
\subsubsection*{(\arabic{questioncounter}.\arabic{taskcounter}) #1 }
}

\fancyhf{} % sets both header and footer to nothing
\renewcommand{\headrulewidth}{0pt}

\pagestyle{fancyplain}
\lhead{\fancyplain{}{{\myhwname} --- \mycourse}}
\rhead{\fancyplain{}{{\myname}}}
\cfoot{\thepage}

\newcommand{\myname}{Ishan Bhargava}

\newcommand{\myhwname}{Homework 14} % confirm
\newcommand{\mycourse}{15-819}  % confirm

\setlength{\parindent}{0pt}
\setlength{\parskip}{5pt plus 1pt}
\setlength{\headheight}{15pt}

% \pagecolor{black}
% \color{white}

\title{LFPL Typechecker and Interpreter}
\author{Ishan Bhargava}
\date{15-819 Resource Aware Programming Languages}

\begin{document}

\maketitle

\section{Introduction}

Although computers these days seem to have more memory than we know what to do with, there are still many areas of computing which require precise control over resource usage. For example, phones contain tiny SoCs which are dedicated to tasks such as processing Bluetooth radio events. These chips have small amounts of memory, but still need to fulfill difficult tasks in real time, such as decoding a high quality audio stream. Other classical examples include Blockchain-based applications, as space is also expensive in that domain.

\subsection{LFPL Background}

The LFPL programming language was designed to represent `non-size-increasing programs'. LFPL programs produce a result whose size can never exceed the input size. This is achieved by using `diamonds' to represent an `allocation' for a node of a recursive type. For example, linked list nodes each require one diamond. This means that if you start your program with 5 diamonds, you cannot create more than 5 list nodes. Although our implementation of LFPL only includes linked lists, there's no reason this could not be generalized to other structures such as binary trees, where each node would require a diamond, or arrays, where $n$ diamonds would be required to allocate an array of length $n$.

This has the consequence that non-constant-space data structures such as lists cannot be copied, since copying diamonds is illegal. Therefore, LFPL uses an affine type system for diamonds and lists. However, integers, booleans, and other constant-space types can be copied without any problem. This is because these so-called `heap-free' types cannot end up taking more than a constant amount of space, which can be found statically.

\section{Design and Syntax}

The LFPL interpreter implements all features of LFPL as described in the lecture notes. In addition, integers have also been added as a heap-free type. Note for some syntactic forms, there is a Unicode alternative. LFPL programs are all one expression.
\begin{center}
  \begin{tabular}{rl}
    Comments
      & \code|-- Line comment| \\
      & \code|(* Block comment *)| \\
    Types
      & \code|unit|, \code|int|, \code|bool| \\
      & \code|<>|, $\diamondsuit$ \\
      & \code|t1 * t2| \\
      & \code|t1 -> t2|, \code|t1 -o t2| \\
      & \code|t list| \\
    Variables
      & \code|ident| \\
    Lambda/anonymous function
      & \code|fn (x : t) => e| \\
      & \code|$\lambda$ (x : t) => e| \\
    Let binding
      & \code|let x : t = e1 in e2 end| \\
    Function application
      & \code|e1 e2| \\
    Boolean literals
      & \code|true|, \code|false| \\
    Integer literals
      & \code|..., -2, -1, 0, 1, 2, 3, ...| \\
    Unit literal
      & \code|()| \\
    Diamond literal (*)
      & \code|<>|, $\diamondsuit$ \\
    If expression
      & \code|if e1 then e2 else e3| \\
    Pair creation
      & \code|(e1, e2)| \\
    Pair destructuring
      & \code|letp (x, y) = e1 in e2 end| \\
    Empty list
      & \code|nil: t|, \code|[]: t| \\
    List construction
      & \code|cons(diamond, head, tail)| \\
    List literal (*)
      & \code|[e1, e2, e3, ...]: t| \\
    List iteration
      & \code@iter e1 { nil => e2 | cons(x, y, _) with z => e2 }@
  \end{tabular}
\end{center}

Forms marked with (*) `create' diamonds and therefore cannot appear in a regular LFPL program. They can only be used to specify input to an LFPL program.

Note that the empty list must be annotated with the type of its elements. This avoids ambiguity for programs like \code|fn x : unit => []| which otherwise cannot be assigned a type. The same applies for list literals. For example, \code|[1, 2, 3]: int| is a list of ints.

\subsection{Enforcing linearity}

It is clear that lists are not copyable and therefore not heap-free. However, it is less clear whether we should consider functions to be heap-free or not. On one hand, functions can `capture' arbitrary values, some of which may be non-heap-free such as lists or other functions. But many functions don't do this, and could be considered heap-free. In order to avoid having to classify some functions as `heap-free' and others as not, we choose the more restrictive path and enforce linearity for all functions. These programs can be made to typecheck by inlining the function in question

Similarly, inside of the \code|cons| case for the list iterator, the program cannot access any variables other than those bound by the iterator. This means that the following program does not typecheck:
\begin{lstlisting}
let
  insert : <> -> int -> int list -> int list =
    fn d: <> => fn v: int => fn L: int list => (
      iter L {
        [] => (fn d2: <> => fn x: int => cons(d2, x, []: int))
      | cons(d1, x1, _) with f => (
          fn d2: <> => fn x: int =>
              if x >= x1
                then cons(d1, x1, f d2 x)
                else cons(d1, x, f d2 x1)
        )
      }
    ) d v
in
let isort : int list -> int list =
  fn L : int list =>
      iter L {
          [] => []: int
        | cons(d, x, _) with sortedTail =>
            insert d x sortedTail
      }
in
  isort
end
end
\end{lstlisting}
Compiling this program fails:
\begin{lstlisting}[numbers=none]
bad_isort.lfpl: 20:13 - 20:20: Variable 'insert' not declared or out of scope
\end{lstlisting}
Instead, the programmer must inline the \code|insert| function manually

\section{LFPL Interpreter}

The LFPL interpreter is appropriately named `\verb|lfpl|'. We will illustrate the usage of the LFPL interpreter on a program which concatenates its input.

Suppose the following code is in a file called \verb|concat.lfpl|.
\begin{lstlisting}
let
  concat: int list list -> int list =
    fn xss: int list list =>
      iter xss {
          [] => []: int
        | cons(d, xs, _) with y =>
            iter xs {
                [] => y
              | cons(d', x, _) with y' => cons(d', x, y')
            }

            -- Note 'd' is lost here. We don't need it
      }
in
  concat
end
\end{lstlisting}

We can typecheck this program by running \verb|lfpl concat.lfpl|:
\begin{lstlisting}[numbers=none]
% lfpl concat.lfpl
<lambda>: (((int) list) list) -> ((int) list)
\end{lstlisting}
Since the program right now just returns a function, the interpreter stops and reports its type. We can provide input to the function as another argument on the command line
\begin{lstlisting}[numbers=none]
% lfpl concat.lfpl "([[1,2,3]: int, [4,5,6]: int, [7,8,9]: int]: int list)"
[1, 2, 3, 4, 5, 6, 7, 8, 9]: (int) list
\end{lstlisting}
Now that there is some input for the function, the interpreter performs the function application, reduces it to a value, and displays it.

The interpreter also will display all parsing/typechecking problems. Consider the following program which attempts to use a diamond multiple times:
\begin{lstlisting}
let
  append: int list -> int list -> int list =
      fn xs: int list => fn ys: int list =>
        iter xs {
            [] => ys
          | cons(d, x, _) with y => cons(d, x, cons(d, x, y))
        }
in
  append
end
\end{lstlisting}
This results in an error:
\begin{lstlisting}[numbers=none]
badappend.lfpl: 6:57 - 6:58: Variable 'd' of type '<>' is not heap-free
                             and cannot be used multiple times
Last usage: badappend.lfpl: 6:46 - 6:47
Declared at: badappend.lfpl: 4:13 - 8:1
\end{lstlisting}

\section{Further work}
Currently, the interpreter uses a simple substitution-based approach when evaluating expressions. However, we could also compile LFPL to C without having to dynamically allocate more memory than is available in the input. In addition, we could relax some of the rules on linearity involving function types, by checking if a lambda contains free variables or if those free variables are all heap-free. We could also offer a new kind of definition \code|x := e| which would physically substitute \code|e| wherever \code|x| appears. This could make programming in LFPL easier.

\end{document}


\title{Self Organizing Systems Exercise 2}
\author{
        Alexander Dobler 01631858\\
        Thomas Kaufmann 01129115
}
\date{\today}

\documentclass[12pt]{article}

\usepackage{hyperref}
\usepackage{booktabs}
\usepackage{graphics}
\usepackage{multirow}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{mwe}
\usepackage{amsmath,amssymb}
\usepackage{subcaption}
\usepackage[section]{placeins}

\begin{document}
\maketitle

\begin{abstract}
    In the second exercise of the lecture Self Organizing Systems we are implementing a simple Particle Swarm Optimization (PSO) algorithm and experimenting with different parameters, fitness functions, constraints and constraint handling methods.
    More specifically, we are given a PSO framework in NetLogo for optimizing functions $f$ from $\mathbb{R}^2$ to $\mathbb{R}$.
    Our task is to implement 3 different fitness functions, 3 different constraints and constraint handling methods using penalization or solution rejection.
    Furthermore, we are to conduct several experiments, to observe different effects of parameters on the performance of the convergence behaviour of the PSO algorithm.
    Here we are inspecting \textit{population size}, \textit{particle speed limit}, \textit{particle inertia}, both \textit{acceleration coefficients} and the difference between constraint handling using penalization and rejection.
\end{abstract}

\section{Implementation}
In this section we will describe how we implemented the required tasks given in the exercise.
We will divide this section into explanations for \textit{constraints}, \textit{fitness functions} and \textit{constraint handling with penalization}.

\subsection{Constraints}
As we are already given skeletons for constraints, implementing is as easy as returning \textit{true}, if the constraint is violated and \textit{false} otherwise.
We opted for implementing the following constraints.
\begin{enumerate}
        \item $x^2+y^2<6000$
        \item $x^2+y^2<9000\text{ and }x^2+y^2>4000$
        \item $\tan(2x)<\tan(4y)$
\end{enumerate}
So, if for example for the first constraint it holds that $x^2+y^2>=6000$, the constraint is violated and we return true.
We selected these constraints, as we wanted to have functions with one connected region (constraint 1 and 2) and also constraints with miltiple separated regions (constraint 3).
\subsection{Fitness Functions}
Here we have a similar setting as for constraints, because we already have skeletons for fitness functions.
We opted to implement the following functions.
\begin{enumerate}
        \item Schaffer function
        \item Booth's function
        \item Schwefel function
\end{enumerate}
Scaling $x$ and $y$ variables for the input of the functions is done as already shown in the template.
It is also important to mention that the NetLogo $\sin$-function expects angles in degrees as input, so we had to convert radians to degrees first.
For the Schwefel function we had to set $n:=2$ and $x_1=x,x_2=y$ for our purpose of two dimensions.

We chose these functions as we wanted to have both optima at the corners of the grid and optima in the middle of the grid.
Furthermore we also have a diversity of how many local optima the search-space of the different functions have.
\subsection{Constraint Handling with Penalization}
The implementation for penalization is more interesting.
First we created 4 different functions to calculate penalization values for each constraint (the example constraint included).
So for each constraint we can compute its penalty value at patch $x,y\in\mathbb{R}^2$, if the constraint is violated at this patch.
This penalty value is $C(x,y)\cdot d$ for constraints $C(x,y)<0$ and a constant $d$.
For example for constraint 1 we have $(x^2+y^2-6000)\cdot d$ as penalty value if $x^2+y^2\ge 6000$.
Furthermore we wanted penalty values to be between 0 and 1, so we chose constants $d$ appropriately.
So for example for constraint 1 we set $d:=\frac{1}{14000}$ as $C(x,y)=x^2+y^2-6000$ can be as big as $14000$ for $x=y=100$.

We then add these penalty values at position $(x,y)$ to the value of the patch at position $(x,y)$ if the selected contraint is violated at position $(x,y)$.
This, of course, is only done if penalization is selected as constraint handling method.
Due to this variant of implementation, we do not have to update anything in the \textit{update-particle-positions}, as fitness-functions at patches are already considering penalization by the selected constraint.

\section{Experiments and Analysis}
\subsection{Experimental Setup \& Evaluation Metrics}
In order to compare the convergence behaviour, we use a fixed number of up to $200$ iterations, without any time limit.
Furthermore, we disabled the premature termination criterion once the optimum is found, since this would have led to bias results in our stepwise average approach.

We use the following metrics to capture characteristics of solutions:
\begin{itemize}
	\item \textbf{Fitness}: a value between 0 and 1
	\item \textbf{Number of Clusters}: To get a feeling of the distribution of particles in the search space.
	We use the clustering algorithm of the netlogo plugin \emph{dbscan}, where a cluster is constituted of at least three agents with a maximum distance of $5$.
	These values have been selected manually based on some manual tweaking and tuning.

	\begin{figure}[h!]
		\centering
			\includegraphics[width=5cm]{figures/clusters.png}
			\caption{Illustration of Clusters in a constrained Schwefel Function Optimization Instance.}
			\label{fig:clusters}
	\end{figure}

	Figure~\ref{fig:clusters} illustrates a few clusters in an early iteration in a Schaffer optimization instance.
	With this metric, in combination with others, we aim to identify scenarios where agents split up into separate groups, converging towards different regions in the search space.
	We normalized the number of clusters by the population size, to foster comparability in plots.

	\item \textbf{Average Distance to the Optimal Solution}: Should be monotonically decreasing in convex functions. However, in rugged landscapes with several nearly optimal solutions may not necessarily decrease to $\approx 0$.
	\item \textbf{Average Distance among Particles}: As a measure for the distribution of particles in the search space.
	Thus, in the beginning it is relatively high, as particles are randomly scattered among the search space, but should decrease continuously as particles strive towards the (single or few) global optima.
	In case this value is high, a disconnected and highly constrained search may be indicated.
	\item \textbf{Average Path Length}: Average length of the paths of each particle throughout the entire execution.

\end{itemize}

Finally, for statistically stable results, each configuration in our experiments was executed $15$ times, and metrics where obtained by the average of those executions.
%\subsection{Experiment 1: Strong Personal Focus}
%\begin{figure}
%	\centering
%	\includegraphics[width=0.75\textwidth]{figures/ex1/ex1-1.pdf}
%	\label{fig:ex1-1}
%\end{figure}
%\begin{figure}
%	\centering
%	\includegraphics[width=0.75\textwidth]{figures/ex1/ex1-2.pdf}
%	\label{fig:ex1-2}
%\end{figure}
%
%\begin{figure}
%	\centering
%	\includegraphics[width=0.75\textwidth]{figures/ex1/ex1-3.pdf}
%	\label{fig:ex1-3}
%\end{figure}
%strong personal focus and scarce distribution in the beginning, hard to find neighbour that guides somewhere
%in easier functions even small population sizes
%\begin{figure}
%\begin{subfigure}[c]{0.5\textwidth}
%\centering
%\includegraphics[width=0.75\textwidth]{figures/ex1/f1.png}
%\subcaption{F1}
%\end{subfigure}
%\begin{subfigure}[c]{0.5\textwidth}
%\centering
%\includegraphics[width=0.75\textwidth]{figures/ex1/f3.png}
%\subcaption{F3}
%\end{subfigure}
%
%\caption{Comparison of representative solutions for fitness function $1$ ($P=100$) and $3$ ($P=20$).}
%
%\end{figure}
\FloatBarrier
\subsection{Different Population Sizes}
In the first experiment we compare the impact of the population size on the convergence behaviour.
For each function, we compared three different population sizes with fixed parameters for the others (inertia $\omega=0.33$, swarm-confidence $c_s=1$, personal-confidence $c_p=1$ and particle-speed-limit $V_{max}=10$).
Observations based on Figures~\ref{fig:ex2-1},\ref{fig:ex2-2},\ref{fig:ex2-3}:
\begin{itemize}
	\item Larger populations are in favour of finding the global optimum or at least converge faster.
	One reason for this is certainly the relatively restricted search space, where it's rather likely that a random initialization hits high quality regions right in the beginning.
	We conducted some small experiments with completely unsuitable parameter settings, but large populations that still obtained the global optimum in just a couple of iterations.
	\item For extraordinarily small populations, e.g. $P=10$, the experiments show that only for rather simple functions without a rugged landscape convergence towards the optimum can be achieved (Booth's function seems to be \emph{convexish}, although it is quadratic).
	\item For more complex functions, like Schaffer, those populations converge at several poor local optima around the global one, indicated by the relatively high number of different clusters and the high average distance to the optimum.
	Still, after $30$ iterations they seem to concentrate on a certain subspace, where they get stuck in local optima.
	\item Although convergence towards a suboptimal solution w.r.t to the fitness sets in rather early, the path lengths still increases, indicating stagnation behaviour.
\end{itemize}

\begin{figure}[h!]
	\centering
	\includegraphics[width=0.75\textwidth]{figures/ex2/ex2-1.pdf}
	\caption{Population sizes: Metrics for Schaffer Function}
	\label{fig:ex2-1}
\end{figure}
\begin{figure}[h!]
	\centering
	\includegraphics[width=0.75\textwidth]{figures/ex2/ex2-2.pdf}
	\caption{Population sizes: Metrics for Booth Function}
	\label{fig:ex2-2}
\end{figure}
\begin{figure}[h!]
	\centering
	\includegraphics[width=0.75\textwidth]{figures/ex2/ex2-3.pdf}
	\caption{Population sizes: Metrics for Schwefel Function}
	\label{fig:ex2-3}
\end{figure}

\FloatBarrier
\subsection{Acceleration Coefficients}
Based on our findings we proceeded with experiments concerning acceleration coefficients.
From manual tests and the previous experiment, we know that for larger populations our functions can be optimized quite easily with standard parameters, $c_s=1$, $c_p=1$ and $V_{max}=10$, so in this experiment we turned our focus on values off the mean (i.e. $1$) and tested all four combinations for the values $0.3$ and $1.7$ for medium and larger population sizes for each of the selected functions.

Observations based on Figures~\ref{fig:ex4-1-20},\ref{fig:ex4-1-50},\ref{fig:ex4-2-20},\ref{fig:ex4-2-50},\ref{fig:ex4-3-20},\ref{fig:ex4-3-50}:
\begin{itemize}
	\item Our main observation from this experiment was that in the selected functions, a dominance of swarm-confidence is favourable in terms of convergence.
	Even for smaller populations convergence towards a high quality optimum can be obtained even below $20$ iterations.
	\item Even further, the number of particles temporarily stuck in suboptimal local seems to be lower, indicated by our cluster metric, which in combination with the distance to the global optimum, shows a relatively smooth convergence in just $20$ iterations.
	\item For other configurations, on the other hand, these metrics show a different picture.
	First, there's a lot of fluctuations in the number of clusters, indicating that particles frequently change influencing neighbours, although eventually they still converge towards the optimum on average (but with far more particles being in many other regions in the search space).
	It is important to note that this is actually not necessarily a poor characteristic.
	For other functions, particularly with larger and potentially more rugged search spaces, we claim that such a behaviour is actually to some degree advantageous since it fosters diversity in the search (still it perfectly shows that parameters highly depend on the instance).
	\item For the inverse dominance relationship between swarm and personal confidence a rather poor behaviour can be observed.
	First, this configuration lacks in convergence towards the optimum w.r.t to fitness, but also the other metrics show poor behaviour.
	Throughout the entire search, the number of distinct clusters remains relatively high, indicating that particles likely do not profit from each other, but rather seem to be stuck in intensifications phases (the path length is still relatively high, indicating that particles still make significant steps but without any real progress in terms of fitness, also shown by the high average distance to the optimum).
	\item Figure~\ref{fig:ex4-example} shows two representative examples of this behaviour.
	On the one hand, ~\ref{fig:ex4-example-a} shows almost arbitrarily scattered particles in the search space after $100$ iterations, while in ~\ref{fig:ex4-example-b} all particles were directly guided towards the optimum.
	\item Again, it can be observed that Booth's function is among the easier ones, where even poor configurations obtain the optimum relatively fast.
\end{itemize}

\begin{figure}[h!]
	\centering
	\includegraphics[width=1\textwidth]{figures/ex4/ex4-1-20.pdf}
	\caption{Acceleration coefficients: Metrics for Schaffer function and $P=20$}
	\label{fig:ex4-1-20}
\end{figure}
\begin{figure}[h!]
	\centering
	\includegraphics[width=1\textwidth]{figures/ex4/ex4-1-50.pdf}
	\caption{Acceleration coefficients: Metrics for Schaffer function and $P=50$}
	\label{fig:ex4-1-50}
\end{figure}
\begin{figure}[h!]
	\centering
	\includegraphics[width=1\textwidth]{figures/ex4/ex4-2-20.pdf}
	\caption{Acceleration coefficients: Metrics for Booth function and $P=20$}
	\label{fig:ex4-2-20}
\end{figure}
\begin{figure}[h!]
	\centering
	\includegraphics[width=1\textwidth]{figures/ex4/ex4-2-50.pdf}
	\caption{Acceleration coefficients: Metrics for Booth function and $P=50$}
	\label{fig:ex4-2-50}
\end{figure}
\begin{figure}[h!]
	\centering
	\includegraphics[width=1\textwidth]{figures/ex4/ex4-3-20.pdf}
	\caption{Acceleration coefficients: Metrics for Schwefel function and $P=20$}
	\label{fig:ex4-3-20}
\end{figure}
\begin{figure}[h!]
	\centering
	\includegraphics[width=1\textwidth]{figures/ex4/ex4-3-50.pdf}
	\caption{Acceleration coefficients: Metrics for Schwefel function and $P=50$}
	\label{fig:ex4-3-50}
\end{figure}

\begin{figure}[h!]
\begin{subfigure}[c]{0.5\textwidth}
\centering
\includegraphics[width=0.75\textwidth]{figures/ex4/ex4-03-17-f3.png}
\subcaption{$c_s=0.3$, $c_p=1.7$}
\label{fig:ex4-example-a}
\end{subfigure}
\begin{subfigure}[c]{0.5\textwidth}
\centering
\includegraphics[width=0.75\textwidth]{figures/ex4/ex4-17-03-f3.png}
\subcaption{$c_s=1.7$, $c_p=0.3$}
\label{fig:ex4-example-b}
\end{subfigure}

\caption{Comparison of representative solutions for fitness function $1$ ($P=100$) and $3$ ($P=20$).}
\label{fig:ex4-example}

\end{figure}

\FloatBarrier
\subsection{Inertia}
In the following experiment we studied the impact of the particle inertia on all functions with default values for other parameters: $P=30$, $c_s=1$, $c_p=1$ and $V_{max}=10$.

Observations:
\begin{itemize}
	\item It is again illustrated that for a reasonable parameter setting with a sufficiently a large population size, it seems to be rather easy to identify (nearly) optimal regions in the search space, although a too low inertia seems to slow down convergence.
	\item For high $\omega$ values larger fluctuations can be observed, simply cause it affects the magnitude of the velocity. This behaviour is also reflected in the path lengths being significantly larger than for smaller $\omega$ values. In combination with these, the relatively high distance to the optimum further suggests that we encounter some kind of stagnation behaviour, where particles continuously move in the search space, without achieving any significant progress (e.g. in Schwefel function, $\omega=1$ converges on average to some fitness $<1$, with the particle distance being still relatively high with lots of frequently changing clusters, which suggests that many particles are (relatively loosely) gathered around some suboptimal region without achieving any progress on the fitness, but cannot escape this region due to the high concentration of particles in this region. An illustrative example of such a situation is shown in Figure~\ref{fig:ex3-example}).
	\item Medium value for $\omega$ seem to be most suitable and provide a good trade-off between fitness convergence as well as dampening fluctuations among clusters, i.e. we suspect that not too many particles get stuck in low-quality local optima, while it seems that the population can identify the optimum soon and guides a majority of particles towards this region. This is also illustrated by the average distance to the real optimum that smoothly converges. Throughout all functions it can be observed that $\omega=0.5$ shows a continuously slower increasing average path length, even slower than low omega values. Here, it is again important to stress the following: in more complex optimization problems, we would assume that such a behaviour can be observed rather on a region level than for \"particular optima\" as it seems to be the case for these functions.
\end{itemize}

\begin{figure}[h!]
	\centering
	\includegraphics[width=1\textwidth]{figures/ex3/ex3-1.pdf}
	\label{fig:ex3-1}
	\caption{Inertia: Metrics for Schaffer function for inertia comparison.}
\end{figure}

\begin{figure}[h!]
	\centering
	\includegraphics[width=1\textwidth]{figures/ex3/ex3-2.pdf}
	\label{fig:ex3-2}
	\caption{Inertia: Metrics for Booth function for inertia comparison.}
\end{figure}

\begin{figure}[h!]
	\centering
	\includegraphics[width=1\textwidth]{figures/ex3/ex3-3.pdf}
	\label{fig:ex3-3}
	\caption{Inertia: Metrics for Schwefel function for inertia comparison.}
\end{figure}

\begin{figure}[h!]
\begin{subfigure}[c]{0.5\textwidth}
\centering
\includegraphics[width=0.75\textwidth]{figures/ex3/wrong-optimum-init.png}
\subcaption{$c_s=0.3$, $c_p=1.7$}
\label{fig:ex3-example-a}
\end{subfigure}
\begin{subfigure}[c]{0.5\textwidth}
\centering
\includegraphics[width=0.75\textwidth]{figures/ex3/wrong-optimum.png}
\subcaption{$c_s=1.7$, $c_p=0.3$}
\label{fig:ex3-example-b}
\end{subfigure}

\caption{Comparison of a representative execution (\ref{fig:ex3-example-a}: the initial solution and \ref{fig:ex3-example-b}: the final solution after 100 iterations) with $\omega=1$ converging towards a suboptimal optimum.}
\label{fig:ex3-example}

\end{figure}

\FloatBarrier
\subsection{Speed Limit}
The next few experiments should illustrate the impact of particle speed limit on the behaviour of the algorithm with regards to convergence.
Parameters used were $P=20,\omega=0.33,c_s=1,c_p=1$.
We compared four different particle speed limits.

The plots are illustrated in Figures~\ref{fig:ex5-1}-\ref{fig:ex5-3} and the main observations are:
\begin{itemize}
	\item For smaller speed-limit convergence to the global optimum is slower, but this has to be expected.
	\item Furthermore also the number of clusters for smaller speed limits is higher. This is visible in the Schwefel function, where we have a lot of clusters, even after 100 iterations for $V_{\max}=3$.
	\item Interestingly, the path length is not always longest for the highest speed limit.
	\item Overall one could say, that the speed limit does not have that much of an impact on the Schaffer and Booth function, whereas too low speed limits lead to a lot of scattered particles in the search space for the Schwefel function.
	But upon closer look, we can also see that some suboptimal speed limits lead to early convergence, not leading to the global optimum.
	This is also illustrated in Figure~\ref{fig:ex5comp}, where the search space for the Schwefel function is illustrated after 100 iterations.
	For $V_{\max}=3$ we can see a lot of scattered particles stuck in different local optima, whereas for $V_{\max}=20$ all particles are stuck in one single local optimum.
\end{itemize}

\begin{figure}[h!]
	\centering
	\includegraphics[width=1\textwidth]{figures/ex5/ex5-1.pdf}
	\caption{Speed limit: Metrics for Schaffer function.}
	\label{fig:ex5-1}
\end{figure}
\begin{figure}[h!]
	\centering
	\includegraphics[width=1\textwidth]{figures/ex5/ex5-2.pdf}
	\caption{Speed limit: Metrics for Booth function.}
	\label{fig:ex5-2}
\end{figure}
\begin{figure}[h!]
	\centering
	\includegraphics[width=1\textwidth]{figures/ex5/ex5-3.pdf}
	\caption{Speed limit: Metrics for Schwefel function.}
	\label{fig:ex5-3}
\end{figure}

\begin{figure}[h!]
\begin{subfigure}[c]{0.5\textwidth}
\centering
\includegraphics[width=0.75\textwidth]{figures/ex5/f3-3.png}
\subcaption{$V_{max}=3$}
\end{subfigure}
\begin{subfigure}[c]{0.5\textwidth}
\centering
\includegraphics[width=0.75\textwidth]{figures/ex5/f3-20.png}
\subcaption{$V_{max}=20$}
\end{subfigure}
\caption{Comparison of representative solutions for fitness function $1$ ($P=100$) and $3$ ($P=20$).}
\label{fig:ex5comp}
\end{figure}

\FloatBarrier
\subsection{Constraint Handling}
Our last conducted experiment compares the two constraint handling methods, namely penalty and rejection.
For this we try out each of the $3\cdot 3=9$ combinations of fitness function and constraints and compare constraint handling by rejection and penalty.
The parameters for all experiments are $P=50,c_s=1,c_p=1,V_{\max}=10,\omega=0.33$.
All 9 plots are illustrated in Figures~\ref{fig:ex6-1-1}-\ref{fig:ex6-3-3} and we could observe the following:

\begin{itemize}
	\item For constraint 1 and the Schaffer function we have almost the same behaviour for rejection and penalty.
	This is most certainly due to the fact that the global optimum is at coordinate $(0,0)$, whereas constraint 1 imposes a constraint, where all valid solutions lie in the middle.
	\item For all other combinations of fitness functions and constraints the penalty method always leads to particles having smaller distance to each other.
	This is because particles can overcome barriers with the penalty method, whereas with the rejection method particles can be ``stuck'' in some regions.
	The same arguments also lead to fewer clusters, when using the penalty method.
	\item It is also worth mentioning that the path length also increases for the penalty method, as particles can walk into invalid regions.
	\item Regarding convergence to global optima we have similar behaviour for both constraint handling methods, as we used a population size of 50, which is quite high.
	But for example for the Schaffer function and constraint 3 we have faster convergence to the global optimum for the penalty method, whereas for the rejection method particles converges slowly to a local optimum, which is not globally optimal.
	\item Overall it can be said, that the penalty method outperforms the rejection method in nearly every aspect.
	This can of course be the result of the choice of our fitness function and constraints, and also the particular implementation we chose.
	But we would still prefer penalty over rejection in nearly every scenario.
\end{itemize}

\begin{figure}[h!]
	\centering
	\includegraphics[width=1\textwidth]{figures/ex6/ex6-1-Constraint_1.pdf}
	\caption{Constraint handling: Schaffer function, constraint 1}
	\label{fig:ex6-1-1}
\end{figure}
\begin{figure}[h!]
	\centering
	\includegraphics[width=1\textwidth]{figures/ex6/ex6-1-Constraint_2.pdf}
	\caption{Constraint handling: Schaffer function, constraint 2}
	\label{fig:ex6-1-2}
\end{figure}
\begin{figure}[h!]
	\centering
	\includegraphics[width=1\textwidth]{figures/ex6/ex6-1-Constraint_3.pdf}
	\caption{Constraint handling: Schaffer function, constraint 3}
	\label{fig:ex6-1-3}
\end{figure}
\begin{figure}[h!]
	\centering
	\includegraphics[width=1\textwidth]{figures/ex6/ex6-2-Constraint_1.pdf}
	\caption{Constraint handling: Booth function, constraint 1}
	\label{fig:ex6-2-1}
\end{figure}
\begin{figure}[h!]
	\centering
	\includegraphics[width=1\textwidth]{figures/ex6/ex6-2-Constraint_2.pdf}
	\caption{Constraint handling: Booth function, constraint 2}
	\label{fig:ex6-2-2}
\end{figure}
\begin{figure}[h!]
	\centering
	\includegraphics[width=1\textwidth]{figures/ex6/ex6-2-Constraint_3.pdf}
	\caption{Constraint handling: Booth function, constraint 3}
	\label{fig:ex6-2-3}
\end{figure}
\begin{figure}[h!]
	\centering
	\includegraphics[width=1\textwidth]{figures/ex6/ex6-3-Constraint_1.pdf}
	\caption{Constraint handling: Schwefel function, constraint 1}
	\label{fig:ex6-3-1}
\end{figure}
\begin{figure}[h!]
	\centering
	\includegraphics[width=1\textwidth]{figures/ex6/ex6-3-Constraint_2.pdf}
	\caption{Constraint handling: Schwefel function, constraint 2}
	\label{fig:ex6-3-2}
\end{figure}
\begin{figure}[h!]
	\centering
	\includegraphics[width=1\textwidth]{figures/ex6/ex6-3-Constraint_3.pdf}
	\caption{Constraint handling: Schwefel function, constraint 3}
	\label{fig:ex6-3-3}
\end{figure}

\FloatBarrier
\section{Conclusion}
We have seen how different parameters influence the behaviour of a PSO algorithm.
In particular we have considered population size, particle inertia, acceleration coefficients and particle speed limits.
We have evaluated different choices for these parameters on 3 different fitness functions.
The main observation was that for default choices of parameters the PSO algorithm performs really well on all 3 different fitness functions.
One would have to use really abstract parameter choices (small population sizes or suboptimal acceleration coefficients) to lead the PSO algorithm to early convergence and other undesirable behaviour.

Furthermore we compared rejection and penalty methods for constraint handling using 3 different constraints.
We have seen that the penalty method performs better in nearly all aspects and occasions.
This could be due to our choice of fitness functions, constraints and particular implementation.
But still we think that some form of penalization has to be prefered over rejection in almost all cases.

\end{document}


\documentclass[12pt]{amsart}
\usepackage{geometry} % see geometry.pdf on how to lay out the page. There's lots.
\usepackage{unitb}
\usepackage{calculational}
\usepackage{ulem}
\normalem
\geometry{a4paper} % or letter or a5paper or ... etc
% \geometry{landscape} % rotated page geometry

% See the ``Article customise'' template for some common customisations

\title{}
\author{}
\date{} % delete this line to display the current date

%%% BEGIN DOCUMENT
\begin{document}

\maketitle
\tableofcontents

%\section{}
%\subsection{}

\begin{machine}{m0}

\hide{
	\begin{align*}
\variable{		n,a : \Int}
	\end{align*}
}

\begin{align*}
\invariant{inv0}
{	a = n^3	}
\end{align*}
%
%%\begin{proof}{INIT/INV/inv0}
%%\begin{calculation}
%%
%%\end{calculation}
%%\end{proof}

\newevent{evt}

%
\begin{align*}
\initialization{init0}
{	n = 0 \land a = 0	}
\end{align*}
%
%
\begin{align*}
\evassignment{evt}{a0}
{	n' = n + 1	}
\end{align*}

We use the proof obligation of \ref{inv0} to deduce a proper assignment to $a$:

\begin{proof}{evt/INV/inv0}
	\begin{calculation}
		(n')^3
	\hint{=}{ \ref{a0} }
		(n+1)^3
	\hint{=}{ arithmetic }
		n^3 + 3 \cdot n^2 + 3 \cdot n + 1
	\hint{=}{ \ref{inv0} }
		a + 3 \cdot n^2 + 3 \cdot n + 1
	\hint{=}{ we add a variable $b$: \ref{inv1}, see below }
		a + b
	\hint{=}{ \ref{a1} }
		a'
	\end{calculation}
\end{proof}

\begin{align*}
\variable{	b : \Int}
\end{align*}

\begin{align*}
\invariant{inv1}
{	b = 3 \cdot n^2 + 3 \cdot n + 1	}
\end{align*}
%%%%\begin{invariant}{inv2}
%%%%	b ~=~ 3 \cdot n^2 + 3 \cdot n + 1
%%%%\end{invariant}
%%%
\begin{align*}
\evassignment{evt}{a1}
{	a' = a + b	}
\end{align*}

We now have a new invariant to preserve. It is easy to see how to establish it initially:
%
\begin{align*}
\initialization{in1}
{	b = 1	}
\end{align*}

\begin{itemize}
\item label initialization predicates
\item spacing commands
\item \sout{dummy section}
\item \sout{test case: train system}
\item refinement

	add refinement environments for: changing schedules, transforming progress properties
\item po labels: check that proofs match a po
\item translate the tags in proof obligations

	create toc entries with the proof environment, in unitb.sty
\end{itemize}

\section{Todo:}
\begin{itemize}
\item grammar: make operator grammar generic
\item grammar: include prefix operators and quantifiers
\item
\item in Z3.Z3.merge, remove the exceptions to integrate it to error handling
\item spacing in the middle of event and set declarations
\item testing the input validation, error messages
\item proof structures (proof by cases, etc)
\item types
\item invariant theorems
\item error checking
\item better LaTeX formatting
\item lazy proof checking
\item \sout{continuous checking}
\item \sout{\emph{bug}: last of empty list of calculation steps}
\item \sout{error report: report line number instead of step number when proof is incorrect}
\item generate documentation
\item handle the ``\textbackslash input'' commands in latex and use it to produce a project hierarchy
\end{itemize}

I'm now describing how I came up with the proof. It came to me in a dream and I forgot it in another dream.
%
\begin{proof}{evt/INV/inv1}
	\begin{calculation}
		3 \cdot (n')^2 + 3 \cdot n' + 1
	\hint{=}{ \ref{a0} }
		3 \cdot (n+1)^2 + 3 \cdot (n+1) + 1
	\hint{=}{ arithmetic }
		3 \cdot (n^2+2\cdot n+1) + 3 \cdot (n+1) + 1
	\hint{=}{ arithmetic }
		3 \cdot n^2+6\cdot n+3 + 3 \cdot n + 3 + 1
	\hint{=}{ \ref{inv1} }
		b+6\cdot n+3+3
	\hint{=}{ \ref{inv2}, see below }
		b+c
	\hint{=}{ \ref{a2}, see below }
		b'
	\end{calculation}
\end{proof}

\begin{align*}
\variable{	c : \Int}
\end{align*}

\begin{align*}
\invariant{inv2}
{	c = 6 \cdot n + 6	}
\end{align*}

\begin{align*}
\evassignment{evt}{a2}
{	b' = b + c	}
\end{align*}

Invariant \ref{inv2} is easy to satisfy initially:

\begin{align*}
\initialization{in2}
{	c = 6	}
\end{align*}

\begin{proof}{evt/INV/inv2}
	\begin{calculation}
		6 \cdot (n') + 6
	\hint{=}{ \ref{a0} }
		6 \cdot (n+1) + 6
	\hint{=}{ arithmetic }
		6 \cdot n + 6 + 6
	\hint{=}{ \ref{inv2} }
		c + 6
	\hint{=}{ \ref{a3} }
		c'
	\end{calculation}
\end{proof}
%
\begin{align*}
\evassignment{evt}{a3}
{	c' = c + 6	}
\end{align*}

\end{machine}

\end{document}