\documentclass[12pt]{article}

\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{subfigure}
\usepackage{float}
\usepackage{ulem}
\usepackage{bm}
\usepackage{anysize}
\usepackage{pythonhighlight}

\marginsize{2cm}{2cm}{0.9cm}{1.8cm}

\title{EECE 5639 Computer Vision\\ [2ex] \begin{large} Homework \#5 \end{large} }
\author{Jiyu Tian}
\date{}

\begin{document}
\maketitle
\pagestyle{empty}
%%---------------------------------------------------------------
%% Question 1
%%---------------------------------------------------------------
\section{Solution:}
In the simple stereo system,
\begin{equation*}
    Z = f\frac{T}{d},\ d= x_r - x_l
\end{equation*}
Since the only source of noise is the localization of corresponding points in the two images, i.e. the disparity $d$, we have the error estimation as:
\begin{equation*}
    \Delta Z = \sqrt{\left( \frac{\partial Z}{\partial d}\Delta d\right)^2+ \left( \frac{\partial Z}{\partial f}\Delta f\right)^2 + \left( \frac{\partial Z}{\partial T}\Delta T\right)^2} = |\frac{\partial Z}{\partial d}|\Delta d = \frac{fT}{d^2}\Delta d
\end{equation*}
The error in depth is directly proportional to the baseline $T$, focal length $f$, error in disparity $\Delta d$, and $\frac{1}{d^2}$. So in order to have less error in depth we should have more disparity, small baseline and focal length, and small error in disparity.

%%---------------------------------------------------------------
%% Question 2
%%---------------------------------------------------------------
\section{Solution:}
(a)
\begin{equation*}
T = \left[ \begin{array}{cccc}
1 & 0 & 0 & -7\\
0 & 1 & 0 & -1\\
0 & 0 & 1 & -2\\
0 & 0 & 0 & 1
\end{array} \right],\ R_{Y90}  = \left[ \begin{array}{cccc}
0 & 0 & 1 & 0\\
0 & 1 & 0 & 0\\
-1 & 0 & 0 & 0\\
0 & 0 & 0 & 1
\end{array} \right],\ R_{Z90}  = \left[ \begin{array}{cccc}
0 & -1 & 0 & 0\\
1 & 0 & 0 & 0\\
0 & 0 & 1 & 0\\
0 & 0 & 0 & 1\\
\end{array} \right]
\end{equation*}

\begin{equation*}
M_1 =  R_{Z90} R_{Y90} T = \left[ \begin{array}{cccc}
0 & -1 & 0 & 1\\
0 & 0 & 1 & -3\\
-1 & 0 & 0 & 10\\
0 & 0 & 0 & 1
\end{array} \right]
\end{equation*}

\noindent (b)
\begin{equation*}
T = \left[ \begin{array}{cccc}
1 & 0 & 0 & -10\\
0 & 1 & 0 & -1\\
0 & 0 & 1 & -3\\
0 & 0 & 0 & 1
\end{array} \right],\ R_{Y90}  = \left[ \begin{array}{cccc}
0 & 0 & 1 & 0\\
0 & 1 & 0 & 0\\
-1 & 0 & 0 & 0\\
0 & 0 & 0 & 1
\end{array} \right],\ R_{Z90}  = \left[ \begin{array}{cccc}
0 & -1 & 0 & 0\\
1 & 0 & 0 & 0\\
0 & 0 & 1 & 0\\
0 & 0 & 0 & 1\\
\end{array} \right]
\end{equation*}

\begin{equation*}
M_2 =  R_{Z90} R_{Y90} T = \left[ \begin{array}{cccc}
0 & -1 & 0 & 1\\
0 & 0 & 1 & -2\\
-1 & 0 & 0 & 7\\
0 & 0 & 0 & 1
\end{array} \right]
\end{equation*}

\noindent (c) Consider the homogeneous coordinates of $p_1$ and $p_2$ in camera 1 and 2 coordinates are:

\begin{equation*}
p_1 = \left[ \begin{array}{c}
-\frac{3}{10}k\\
\frac{1}{10}k\\
k\\
1
\end{array} \right],\ p_2 = \left[ \begin{array}{c}
-\frac{3}{7}l\\
\frac{2}{7}l\\
l\\
1
\end{array} \right]
\end{equation*}

\noindent The world coordinates will be:
\begin{equation*}
p_1' = M_1^{-1}p1 = \left[ \begin{array}{c}
10 - k\\
1 + \frac{3}{10}k\\
3 + \frac{1}{10}k\\
1
\end{array} \right],\ p_2' = M_2^{-1}p_2 = \left[ \begin{array}{c}
7 - l\\
1 + \frac{3}{7}l\\
2 + \frac{2}{7}l\\
1
\end{array} \right]
\end{equation*}
Letting $p_1' = p_2'$ we have $k = 10$ and $l = 7$, and thus the world coordinate of $p$ is $(0, 4, 4)$.

%%---------------------------------------------------------------
%% Question 3
%%---------------------------------------------------------------
\section{Solution:}
(a)
\begin{equation*}
    x = f\frac{X}{Z} = 3, \ y = f\frac{Y}{Z} = 6,\ z = f = 1
\end{equation*}
The location of the object at time $t = 0$ is (3, 6).\\
(b)

\begin{equation*}
    p_0  = \left[ \begin{array}{c}
f\frac{T_x}{T_z}\\
f\frac{T_y}{T_z}\\
f
\end{array} \right] = \left[ \begin{array}{c}
5\\
10\\
1
\end{array} \right]
\end{equation*}
The image coordinates of the focus of expansion is $(5, 10)$.\\
(c) Time to collision
\begin{equation*}
    \tau = \frac{Z}{w} = 10
\end{equation*}

%%---------------------------------------------------------------
%% Question 4
%%---------------------------------------------------------------
\section{Solution:}
The acceleration of object is
\begin{equation*}
    a = \frac{2\Delta S}{t^2} = \left[ \begin{array}{c}
20\\
40\\
10
\end{array} \right]
\end{equation*}
The location of object at time $t$ is
\begin{equation*}
P_t = P_0 + \frac{1}{2}at^2 = \left[ \begin{array}{c}
10 +10t^2\\
20 + 20t^2\\
5t^2
\end{array} \right]
\end{equation*}
Its image coordinates
\begin{equation*}
p_t = \left[ \begin{array}{c}
f\frac{X}{Z}\\
f\frac{Y}{Z}\\
\end{array} \right]=\left[ \begin{array}{c}
\frac{10 +10t^2}{5t^2}\\
\frac{20 + 20t^2}{5t^2}\\
\end{array} \right]
\end{equation*}
The focus of expansion is
\begin{equation*}
p_0= \lim\limits_{t\to\infty}p_t = \lim\limits_{t\to\infty}\left[ \begin{array}{c}
\frac{10 +10t^2}{5t^2}\\
\frac{20 + 20t^2}{5t^2}\\
\end{array} \right] = \left[ \begin{array}{c}
2\\
4
\end{array} \right ]
\end{equation*}

\end{document}


\chapter{confCheck} \label{confcheck}

\section{Introduction}

Confluence checking is the analysis of an LPE to find ISTEP summands that are \emph{confluent}.
A confluent ISTEP summand is a summand with the property that the possible behavior of a system is the same up to branching bisimulation before and after applying that summand.

If branching bisimulation is sufficient in terms of accuracy, confluence information can be used to speed up state space generation by prioritizing confluent ISTEP summands.
Note that this means that confluence checking does not bring benefits on its own!

\section{Algorithm}

Consider all possible pairs of summands $(s, t)$ of the LPE where $s$ is a summand that communicates exclusively over the channel ISTEP and does a recursive process instantiation.
Then

\begin{lstlisting}[mathescape]
$s$ = ISTEP [[$c_s$]] >-> P($v_s(x_1)$, $\cdots{}$, $v_s(x_n)$)
$t$ = $A$ ? $a_1$ ? $\cdots{}$ ? $a_m$ [[$c_t \land a_1 = h_1 \land \cdots{} \land a_m = h_m$]]
                      >-> P($v_t(x_1)$, $\cdots{}$, $v_t(x_n)$)
\end{lstlisting}

where

\begin{itemize}
\item $x_1$ to $x_n$ are all process parameters of the LPE;
\item $v_s$ and $v_t$ are functions that yield the expressions that summands $s$ and $t$, respectively, assign to a given process parameter in their recursive process instantiation.
\end{itemize}

Furthermore, let
\begin{align*}
\rho_s &= [x_1 \rightarrow v_s(x_1), \cdots{}, x_n \rightarrow v_s(x_n)] \\
\rho_t &= [x_1 \rightarrow v_t(x_1), \cdots{}, x_n \rightarrow v_t(x_n)]
\end{align*}

If for a particular ISTEP summand $s$ the following condition holds for all pairs $(s, t)$ such that $s \neq t$, then $s$ is confluent:
\begin{align*}
c_s \land c_t \rightarrow{} &c_s[\rho_t] \land c_t[\rho_s] \\
&{} \land h_1 = h_1[\rho_s] \land \cdots{} \land h_m = h_m[\rho_s] \\
&{} \land x_1[\rho_s][\rho_t] = x_1[\rho_t][\rho_s] \land \cdots{} \land x_n[\rho_t][\rho_s] = x_n[\rho_s][\rho_t]
\end{align*}

\section{Example}

Consider the following example:

\begin{lstlisting}
//Process definition:
PROCDEF example[A :: Int](x, y :: Int)
  = A ? i [[x<=9 && x==i]] >-> example[A](x+1, y)
  + ISTEP [[y<=9]] >-> example[A](x, y+1)
  ;

//Initialization:
example[A](0, 0);
\end{lstlisting}

Is the second summand confluent?

If $c_s \land c_t$ holds, then $c_s[\rho_t] \land c_t[\rho_s]$ holds as well.
Since $h_i$ does not use $y$, it is unaffected by $\rho_s$.
Finally,
\begin{align*}
x[\rho_s][\rho_t] = x[\rho_t][\rho_s] = x+1 \\
y[\rho_s][\rho_t] = y[\rho_t][\rho_s] = y+1
\end{align*}

and therefore the confluence condition holds.

So yes, the second summand is confluent.

To store the new information about the second summand, the channel is renamed to CISTEP:

\begin{lstlisting}
//Process definition:
PROCDEF example[A :: Int](x, y :: Int)
  = A ? i [[x<=9 && x==i]] >-> example[A](x+1, y)
  + CISTEP [[y<=9]] >-> example[A](x, y+1)
  ;

//Initialization:
example[A](0, 0);
\end{lstlisting}


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

\begin{document}

\section*{The metric connection}

This is a very standard computation that shows if
\begin{align}
   \Gamma^{a}_{b c} = \frac{1}{2} g^{a d} (  \partial_{b}{g_{d c}}
                                          + \partial_{c}{g_{b d}}
                                          - \partial_{d}{g_{b c}} )
\end{align}
then
\begin{align}
   g_{ab;c} = 0.
\end{align}

This example might well be regarded as the Cadabra counterpart to the familiar \emph{Hello World} program of undergraduate programming classes.

\vspace{15pt}

\begin{cadabra}
   {a,b,c,d,e,f,h,i,j,k,l,m,n,o,p,q,r,s,t,u#}::Indices.

   g_{a b}::Metric.
   g_{a}^{b}::KroneckerDelta.

   \partial_{#}::PartialDerivative.

   cderiv:=\partial_{c}{g_{a b}} - g_{a d}\Gamma^{d}_{b c}
                                 - g_{d b}\Gamma^{d}_{a c}.             # cdb (term31,cderiv)

   Gamma:=\Gamma^{a}_{b c} -> (1/2) g^{a d} (  \partial_{b}{g_{d c}}
                                             + \partial_{c}{g_{b d}}
                                             - \partial_{d}{g_{b c}} ). # cdb (term32,Gamma)

   substitute          (cderiv,Gamma);     # cdb (term33,cderiv)
   distribute          (cderiv)            # cdb (term34,cderiv)
   eliminate_metric    (cderiv)            # cdb (term35,cderiv)
   eliminate_kronecker (cderiv)            # cdb (term36,cderiv)
   canonicalise        (cderiv)            # cdb (term37,cderiv)
\end{cadabra}

\subsection*{The metric connection}

\vspace{-15pt}

\begin{minipage}[t]{0.65\textwidth}
\begin{align*}
   &\cdb*{term31}\\
   &\cdb*{term32}\\
   &\cdb*{term33}\\
   &\cdb*{term34}\\
   &\cdb*{term35}\\
   &\cdb*{term36}\\
   &\cdb*{term37}
\end{align*}
\end{minipage}
\hskip 1cm
\lower16pt\hbox{%
\begin{minipage}[t]{0.35\textwidth}
\begin{latex}
   \begin{align*}
      &\cdb*{term31}\\
      &\cdb*{term32}\\
      &\cdb*{term33}\\
      &\cdb*{term34}\\
      &\cdb*{term35}\\
      &\cdb*{term36}\\
      &\cdb*{term37}
   \end{align*}
\end{latex}
\end{minipage}}

\end{document}


%
% LaTeX report template
%

% This is a comment: in LaTeX everything that in a line comes
% after a "%" symbol is treated as comment

\documentclass[11pt, a4paper]{article}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{listings}

\title{Assignment No 3} % Title

%\author{M.V.A.Suhas kumar (EE17B109)} % Author name
\author{M.V.A.Suhas Kumar \\ {\small EE17B109}}

\date{\today} % Date for the report
\begin{document}

\maketitle % Insert the title, author and date
\section{Fitting Data to Models}
%Create new section;it is autonumbered
This week’s Python assignment is mainly focused on studying the effect of noise on the fitting process.\\
Firstly,we generated a file fitting.dat with 10 columns with first column as time ,while the remaining columns are data\\
The data columns correspond to the function with different amounts of noise added.Here,noise random fluctuations in the value due to many small random effects.Noise is assumed to be normally distributed.\\

Each column function with given $\sigma$:\\
\begin{equation*}
f(t) = 1.05J_{2}(t) - 0.105t + n(t)
\end{equation*}

with \begin{equation*}
Pr(n(t)|\sigma) = \frac{1}{\sigma\sqrt{2\pi}}exp(\frac{-n(t)^2}{2\sigma^2})
\end{equation*}

\begin{figure}[!tbh]
   	\centering
   	\includegraphics[scale=0.4]{q4_plot.png}  % Mention the image name within the curly braces. Image should be in the same folder as the tex file.
   	\caption{Plot of data to be fitted}
   	\label{fig:Data plot}
   \end{figure}
\newpage
Here I am plotting the error bar for one data column:\\
\begin{figure}[!tbh]
   	\centering
   	\includegraphics[scale=0.4]{q5_plot.png}  % Mention the image name within the curly braces. Image should be in the same folder as the tex file.
   	\caption{Data points for $\sigma$ = 0.1 along with exact function}
   	\label{fig:Error bar}
   \end{figure}

True value function is plotted by defining a function:\\
\begin{equation*}
g(t,A,B) = AJ_{2}(t)+Bt
\end{equation*}
Next we assume that there exist some function which fits the noise data  with general form  $g(t,A,B) = AJ_{2}(t)+Bt$\\
\par
Next we will find the (A,B) values by minimising the mean square error between the predicted values from given (A,B) and data column.\\

Contour Plot of $MS\ error$ for w.r.t data column1 for range of (A,B):
\begin{figure}[!tbh]
   	\centering
   	\includegraphics[scale=0.5]{q8_plot.png}  % Mention the image name within the curly braces. Image should be in the same folder as the tex file.
   	\caption{contour plot for $\epsilon_{ij}$ }
   	\label{fig:Contour plot}
   \end{figure}

From the above plot we can clearly see that there exist a single minima.
\newpage
Using the Python function lstsq from scipy.linalg to obtain the best estimate of A and B for different data columns:\\

The following plots show the error in A and B for different data columns:\\

In the first plot B error is appearing to be constant but in reality it increases by a small amount.\\

\begin{figure}[!tbh]
   	\centering
   	\includegraphics[scale=0.5]{q10_plot.png}  % Mention the image name within the curly braces. Image should be in the same folder as the tex file.
   	\caption{A and B error in linear scale}
   	\label{fig:A and B err in linear scale}
   \end{figure}
\begin{figure}[!tbh]
   	\centering
   	\includegraphics[scale=0.5]{q11_plot.png}  % Mention the image name within the curly braces. Image should be in the same folder as the tex file.
   	\caption{A and B error in log scale}
   	\label{fig:A and B err in log scale}
   \end{figure}
\newpage

Here I am plotting mean square error of predicted points w.r.t to true points versus sigma of noise.

\begin{figure}[!tbh]
   	\centering
   	\includegraphics[scale=0.5]{Figure_1.png}  % Mention the image name within the curly braces. Image should be in the same folder as the tex file.
   	\caption{Mean square error for predicted points}
   	\label{fig:pred mean square}
   \end{figure}

\textbf{Conclusion:}
If we calculate the mean square error for the predicted points w.r.t to the data points,In the log scale we observe that there will be a linear variation.

\end{document}


\documentclass[12pt, titlepage, oneside]{article}

\input{settings}

\begin{document}

	\textbf{ELECENG 3TQ3}\\
	\textbf{Elston A.}

\section{Lecture 4}
We can calculate the probability of the union between two sets in the following manner:
\begin{align*}
P[A\u B\u C] &= P[A] + P[B \u C] - P[A \n (B \u C)] \\
&= P[A] + P[B] + P[C] - P[A\u B] - P[A \u C] - P[B \u C] + P[A \u B \u C]
\end{align*}

\subsection{Magic Formula}

Since we know the probability of $P[A|B]$ is as follows
\begin{align*}
P[A|B] = \frac{P[A \n B]}{P[B]}
\end{align*}
so if we instead want the probability of B given A, that is $P[B|A]$ then we have the following formula
\begin{align*}
P[B|A] = \frac{P[A | B]P[B]}{P[A]}
\end{align*}
which we can replace the $P[A \n B]$ term in the numerator by $P[A|B] P[B]$
\begin{align}
P[B|A] = \frac{P[AB]}{P[A]} = \frac{P[A|B] P[B]}{P[A]}
\end{align}

\subsection{Consequences}

Total Probability of an event A when dealing with partitions is given as
\begin{align}
	P[A] = \sum_{i=1}^m P[A|B_i]P[B_i]
\end{align}
so if we use Bayes theorem
\begin{align}
P[B_j | A] = \frac{P[A|B_j] P[B_j]}{\sum_{i=1}^m P[A|B_i]  P[B_i] }
\end{align}

\ex Consider tossing two different coins. Let C1 be the unfair tossing heads with a probability of 3/4 and let C2 be a fair coin tossing heads with probability 1/2. Lets assume that you randomly chose a coin and toss it. What is the probability that you selected C1 if you find out the coin toss was heads.

In this case we would be looking for the probability we selected C1 given the outcome was H
\begin{equation}
P[C1|H] = \frac{ P[H|C1]P[C1] }{P[H|C1]P[C1] + P[H|C2]P[C2]} = \frac{P[HC1]}{P[HC1] + P[HC2]} = \frac{3/8}{3/8+1/4} = \frac{3}{5}
\end{equation}

\subsection{Independent Events}

Two events are independent if and only if
\begin{align}
P[AB] = P[A]P[B]
\end{align}
We should note that disjoint and independent are not the same. Mutually exclusive events mean they cannot happen at the same time, that is, if A and B are mutually exclusive then $P[AB] = 0$.

Independent events are such events where observing one events does not change the outcome of the other. In other words, the conditional probability of an event does not change with the extra information about another event since they have no correlation.

Given two independent events A and B,
\begin{align}
P[A|B] = P[A] \enspace \enspace P[B|A] = P[B]
\end{align}

\ex Consider rolling two fair die. Let $A$ be the event that the product of the two numbers is 12, and let $B$ be the event the sum of the two numbers is 6. Are the events independent?

Since we know 6*2 = 2*6 = 4*3 = 3*4 = 12
\begin{align}
P[A] = \frac{4}{36}
\end{align}
Since we know 1+5 = 5+1 = 3+3 = 2+4 = 4+2
\begin{align}
P[B] = 5/36
\end{align}
So we can see that $P[AB] = 0$ so these events are not independent, but disjoint

\subsection{Properties}
Let A and B be independent events, then A and $B^c$ are independent as well
\begin{align}
P[A\n B^c] = P[A] - P[A \n B] = P[A] - P[A]P[B]  = P[A](1-P[B]) = P[A]P[B^c]
\end{align}

\subsection{Three Events or More Events}
Consider 3 events A, B, C. They are independent if and only if
\items
\item A and B are independent
\item A and C are independent
\item B and C are independent
\eitems

Thus we can say
\begin{align}
P[A \n B \n C] = P[ABC] = P[A]\n P[B]\n P[C] = P[A]P[B]P[C]
\end{align}
\b{Generalization}

For any $n$ events where $n \geq 3$ the sets $A_1,A_2,\dots,A_n$ where each set is independent from the other n-1 other sets.
\begin{align}
P[A_1 \n A_2 \n \dots \n A_n] = P[A_1]P[A_2]\dots P[A_n]
\end{align}
and in general
\begin{align}
P[A_1\u A_2 \u \dots \u A_n] = 1-P[A_1\n A_2 \n \dots \n A_n]
\end{align}

\ex Consider three independent events A, B, C and assume their probabilities are 0.5, 0.3, and 0.1. What is the probability that at least one event happens
\begin{align}
P[A\u B \u C] = 1- P[A]P[B]P[C] = 0.685
\end{align}
\subsection{Sequential Experiments}
Many experiments are done in stages where the next stage depends on the previous one. You can think of this as branches.

As an example we have shown the sequential experiment of tossing a coin two times in a row.
\begin{figure}[h]
	\centering
	\includegraphics[width=0.5\linewidth]{images/toss}
	\caption{Coin toss outcomes}
	\label{fig:toss}
\end{figure}

Consider tossing a coin 5 times, recording the outcomes and repeating the whole process 10 times. We can define a success trial in many different ways. This type of method is often used in drug design and research. In these cases you perform n tests and record successes as 0 or 1. You can call a trial a success if a certain number of successes are recorded.

\end{document}


\section{Homomorphic Encryption}\label{s:homomorphic-encryption}

Some encryption schemes are inherently ``malleable", which means that it is possible to transform a ciphertext into another ciphertext which decrypts to a related plaintext.
Homomorphic encryption is a malleable encryption scheme that allows operations directly on encrypted data so that the results after decryption would correspond to applying matching operations on unencrypted data.
For example, having an encryption mechanism $Enc$, the product of any two ciphertexts is equal to a ciphertext of the sum of the two corresponding plaintexts ($M_1$ and $M_2$). Or more generally the application of a function to the ciphertexts corresponds to another function on the plaintexts, as follows:
\begin{equation}\label{eq:homomorphic}
  Enc(M_1) \otimes Enc(M_2) = Enc(M_1 \oplus M_2)
\end{equation}

for some operations $\otimes$ and $\oplus$.

Homomorphic encryption enables outsourcing computations to a third party, as for example on the cloud.
Cloud computing is designed to deal with difficult operations and with computationally demanding algorithms.
However, if user's data are unencrypted they can be exposed to attacks from both the cloud provider and third parties (hackers, government agencies, data breaches, etc.).
Even if the data are stored \textit{(data at rest)} and transferred \textit{(data in transit)} securely, when they are in use they can be exposed to security risks, such as side-channel attacks \cite{zhang2012cross} and hardware Trojans \cite{becker2013stealthy, tsoutsos2014advanced}.
\textit{Data at rest} implies data that is stored physically in any digital form, while \textit{data in transit} means data traversing the network.
Active data under constant change which is stored in a non-persistent digital state typically in computer random access memory (RAM), CPU caches, or CPU registers are called \textit{data in use}.

It is possible, to construct an entire system to work over encrypted data, manipulating and returning encrypted results.
This system would be based on homomorphic encryption, which allows to apply a function to the ciphertexts that corresponds to another function on the plaintexts, and in general enables computation on encrypted data.
The final results can then be obtained by a single decryption.
For instance, given $Enc(M_1)$ and $Enc(M_2)$ (the encryption of $M_1$ and $M_2$), you can compute $Enc(M_1 + M_2)$ without knowing $M_1$, $M_2$ nor the decryption key.
The only point in the process where data would be decrypted is when the user wants to see the result, and that would presumably happen in the application or client software, not in the database server in the cloud, rendering the host incapable of leaking any type of information.

One challenge that homomorphic encryption schemes, and in general every encrypted computation framework, face is the inability to make runtime decisions based on encrypted data.
Using homomorphic operations requires special care to ensure that branching does not reveal any sensitive data by observing side-channel information (\textit{e.g.} the branch target).
For instance, the host is unable to perform operations like ``\texttt{if (x > 0) return;}" when \texttt{x} is an encrypted variable.
This is known as the ``termination problem", introduced in \cite{brenner2011secret}, since performing runtime branch decisions is not possible and therefore rendering the algorithms implemented on top of those systems by design more complex.

To address this problem traditional algorithms must be changed to their homomorphic equivalents, a not straightforward process.
Some work has been done in \cite{mouris2018terminator} by developing benchmarks targeted for computer architectures based on homomorphic operations.
Those benchmarks avoid termination problems while maintaining data privacy.

There is not a sole type of homomorphic encryption, different schemes have been proposed for different types of applications.

\subsection{Partially Homomorphic Encryption}\label{ss:phe}
The most widely used type of homomorphic encryption is partially homomorphic encryption (PHE), with schemes like RSA, ElGamal, Benaloh and Paillier.
PHE has been expanded with applications such as Helios (e-voting PHE based system) \cite{adida2008helios}, Cryptoleq (Heterogeneous abstract machine for both encrypted and unencrypted computation based on PHE) \cite{mazonka2016cryptoleq}, and others.
All those systems, perform manipulations directly on encrypted data without decrypting them, thus no information leakage is possible.
When the result is eventually decrypted, it will be the same as applying the same manipulations on plaintexts.

The two most common cases (but not the only ones) of partially homomorphic cryptosystems are \textit{additively homomorphic} and \textit{multiplicatively homomorphic}.
\begin{itemize}
  \item Additively homomorphic systems enable computation over ciphertexts (encrypted data) that result in the encryption of the sum (addition) of two plaintexts. More formally, a cryptosystem is considered to be additively homomorphic iff:

  \begin{equation}\label{eq:additively-homomorphic}
    Enc(M_1) \otimes Enc(M_2) = Enc(M_1 + M_2)
  \end{equation}

  for some operation $ \otimes $.
  \item Multiplicatively homomorphic systems enable computation over ciphertexts that decrypt to the product (multiplication) of two plaintexts. More formally, a cryptosystem is considered to be multiplicatively homomorphic iff:

  \begin{equation}\label{eq:multiplicatively-homomorphic}
    Enc(M_1) \otimes Enc(M_2) = Enc(M_1 \cdot M_2)
  \end{equation}

  for some operation $ \otimes $.
\end{itemize}

Below we describe some widely known cryptosystems that are partially homomorphic.

\subsubsection{RSA Cryptosystem}\label{ss:rsa}
Plain RSA encryption is multiplicatively homomorphic. Consider the encryption algorithm described in section \ref{s:pk-rsa}. We know that $Enc(m) = {m_1}^{e}\pmod{n}$. Given two ciphertexts $Enc(m_1)$ and $Enc(m_2)$ of plaintexts $m_1$ and $m_2$ respectively,  we can see that the following holds.

\begin{equation}
  \label{eq:homomorphic-rsa}
  \begin{gathered}
Enc(m_1) \cdot Enc(m_2) = {m_1}^{e}\pmod{n} \cdot {m_2}^{e}\pmod{n} =\\ ({m_1}\cdot{m_2})^{e}\pmod{n} = Enc({m_1}\cdot{m_2})
  \end{gathered}
\end{equation}

So the product of the two ciphertexts corresponds to the ciphertext of the product of the two plaintexts.

\subsubsection{ElGamal Cryptosystem}\label{ss:elgamal}
The ElGamal cryptosystem is also multiplicative homomorphic. Based on the algorithm described in \ref{s:pk-elgamal} we know that $Enc(m) = (G,M) = (g^{r} \pmod{p}, m$, for some random $r \in_{R} \mathbb{Z}_{q}$. Consider we have two ciphertexts $Enc(m_1)$ and $Enc(m_2)$ of plaintexts $m_1$ and $m_2$, respectively. We can see that the following holds.

\begin{equation}
  \label{eq:homomorphic-elgamal}
  \begin{gathered}
Enc(m_1) \cdot Enc(m_2) = (G1, M1) \cdot (G2, M2) =\\ (g^{r_1} \pmod{p}, m_1 \cdot y^{r_1} \pmod{p}) \cdot (g^{r_2} \pmod{p}, m_2 \cdot y^{r_2} \pmod{p}) =\\ (g^{r_1+r_2} \pmod{p}, (m_1 \cdot m_2) \cdot y^{r_1+r_2} \pmod{p}) = Enc({m_1} \cdot {m_2})
  \end{gathered}
\end{equation}

We can see again that the product of the encryptions of two plaintexts results to the encryption of the encryption of the product of the two plaintexts.

\subsubsection{Paillier Cryptosystem}\label{ss:paillier}
The Paillier cryptosystem is additively homomorphic. According to \ref{s:pk-paillier}, we know that $Enc(m) = g^{m} \cdot r^n \pmod{n^2}$, for some random $ r \in_{R} \mathbb{Z}_{n}^{*}$ . Given two ciphertexts $Enc(m_1)$ and $Enc(m_2)$ of plaintexts $m_1$ and $m_2$ respectively, we can see that the following holds.

\begin{equation}
  \label{eq:homomorphic-paillier}
  \begin{gathered}
Enc(m_1) \cdot Enc(m_2) = (g^{m_1} \cdot {r_1}^n \pmod{n^2}) \cdot (g^{m_2} \cdot {r_2}^n \pmod{n^2}) =\\ (g^{m_1} \cdot {r_1}^n) \cdot (g^{m_2} \cdot {r_2}^n)\pmod{n^2} = g^{m_1 + m_2} \cdot (r_1 \cdot r_2) \pmod{n^2} =\\ Enc({m_1} + {m_2})
  \end{gathered}
\end{equation}

We can see that the product of the two ciphertexts will decrypt to the sum of their corresponding plaintexts.

\subsection{Fully Homomorphic Encryption}\label{ss:fhe}
Another form of homomorphic encryption is Fully homomorphic encryption (FHE), first invented by Gentry in \cite{gentry2009fully}.
Due to the fact that FHE enables \textit{arbitrary computation} on ciphertexts, is far more powerful than PHE (which was just called Homomorphic Encryption before FHE systems were discovered) and its appearance sparked the academic interest.
Consecutively a lot of FHE schemes arise, but unfortunately they come along with a huge performance overhead.
This overhead has been a concern and this is the reason that there are not many applications that leverage FHE, despite the wide range of that can benefit from FHE schemes.
Some implementations are the HElib \cite{halevi2014algorithms} and the TFHE \cite{chillotti2016faster}.


\documentclass{article}
\usepackage{latexsym}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{amsthm}

\newtheorem{theorem}{Theorem}[section]
\newtheorem*{problem}{Problem}

\begin{document}
\title{Finding Formulae for Power Sums}
\author{Dave Neary}

\maketitle

\section{Introduction}

One of the first formulae you learn when starting to study sequences and series is the
formula for the sum of the first $n$ positive integers. This
series has been made famous by an anecdote that when Gauss was a school boy, his
teacher, wanting to keep the class busy for a few minutes while they stepped out,
told the class to calculate the sum of the positive whole numbers to 100. Before he
had reached the door, a young Gauss had shouted out the answer.

The "trick" that Gauss used was to realize that by inverting the order of the series
and aligning terms, then adding the two lines, he could simplify things:

\begin{alignat*}{6}
	S &=& 1   &+& 2  &+& \cdots &+& 99 &+& 100 \\
	 S &=& 100 &+& 99 &+& \cdots &+& 2  &+& 1 \\
	2S &=& 101 &+& 101 &+& \cdots &+& 101 &+& 101 \\
\end{alignat*}

In other words, $2S = 100(101), S=5050$, and in general, $S(n) = \frac{(n)(n+1)}{2}$.

The challenge of deriving a closed formula for the powers of integers is an
interesting one, which has resulted in some beautiful mathematics. I will present
one way to prove a given formula using induction, and three different ways to derive
the formula for:

\[ S_k(n) = \sum_{i=1}^{n} i^k \]

These three methods will use the Binomial theorem, a combinatorical identity, and
exponential generating functions.

\section{Proof by induction}

Can we prove, given a formula for $S_k(n)$, that it is true for all $n$? One method
to do so is to use induction. The inductive principle says that if something is true
of the first member of a list, and whenever it is true of a member of the list,
it is also true for the next member of the list, then it is true for every member.

The inductive step starts with the assumption that the condition is true for the
$n^\text{th}$ element of the list, and then starting from that assumption, proving
that the condition must also be true for the next. This can be a little confusing at
first, so let's see it in action.

\begin{problem} Prove that for all $n \in \mathbb{N}$:
	\[ \sum_{i=1}^{n} i^2 = \frac{(n)(n+1)(2n+1)}{6} \]
\end{problem}

\begin{proof}
	If $n = 1$, then $1^2 = \frac{(1)(2)(3)}{6} = 1$, so for the base case, the formula is valid.

	Now assume it is true for $n=k$ -- that is:
	\[ \sum_{i=1}^{k} i^2 = \frac{(k)(k+1)(2k+1)}{6} \]

	Then for $n=k+1$:
	\begin{align*}
		\sum_{i=1}^{k+1} i^2 &= \sum_{i=1}^{k} i^2 + (k+1)^2 \\
		 &= \frac{(k)(k+1)(2k+1)}{6} + (k+1)^2 \\
		 &= \left(k+1\right)\left(\frac{(k)(2k+1) + 6(k+1)}{6}\right) \\
		 &= \left(k+1\right)\left(\frac{2k^2 +7k +6}{6}\right) \\
		 &= \left(k+1\right)\left(\frac{(k+2)(2k+3)}{6}\right) \\
		 &= \frac{(k+1)((k+1)+1)(2(k+1)+1)}{6}
	\end{align*}

	This is the same formula as before, with $(k+1)$ in the place of $k$. We have
	shown that if the formula is valid for $n=k$, then it must also be valid for
	$n=k+1$. Since we have shown that the formula is valid for $n=1$, then it must also
	be true for 2, and therefore also for 3, and, by induction, it is true for all
	$n \in \mathbb{N}$.
\end{proof}

\section{Binomial expansion}

The Binomial theorem states that:
\[ (x + y)^n = \sum_{i=0}^{n} \binom{n}{i}x^{n-i}y^{i} \]
where $\binom{n}{i} = \frac{(n)(n-1)(\cdots)(n-i+1)}{i!}$ for
$n \in \mathbb{R}, i \in \mathbb{N}_0$, and $i! = i\times(i-1)\times (i-2)\times \cdots
\times 2 \times 1$ is the factorial function. For positive integer values of $n$, we can
write $\binom{n}{k} = \frac{n!}{k!(n-k)!}$.

For example:
\begin{align*}
	(x+y)^5 &= \binom{5}{0}x^5 + \binom{5}{1}x^4y + \binom{5}{2}x^3y^2 +
	\binom{5}{3}x^2y^3 + \binom{5}{4}xy^4 + \binom{5}{5}y^5 \\
	 &= x^5 + 5x^4y + 10x^3y^2 + 10x^2y^3 + 5xy^4 + y^5
\end{align*}

Unsurprisingly, since $(x+y)^n = (y+x)^n$, the binomial coefficients are symmetrical.
That is, $\binom{n}{k} = \binom{n}{n-k}$.

To find a formula for the sum of the squares of the sequence of integers, we will use the
Binomial expansion of the next power up. That is:
\[(n+1)^3 = n^3 + 3n^2 + 3n + 1 \]

This gives us an expression for the difference between consecutive cubes, which we can add for
all integers less than $n$:

\begin{alignat*}{5}
	(n+1)^3 &-& n^3     &=& 3n^2     &+& 3n     &+& 1 \\
	n^3     &-& (n-1)^3 &=& 3(n-1)^2 &+& 3(n-1) &+& 1 \\
	      &\vdots&      &=&       &\vdots&  &\vdots& \\
	3^3     &-& 2^3     &=& 3(2^2)   &+& 3(2) &+& 1 \\
	2^3     &-& 1^3     &=& 3(1^2)   &+& 3(1) &+& 1 \\
\end{alignat*}

Now if we add the left-hand side, we see that a number of terms cancel out, and we can replace
$\sum_{i=1}^{n} i$ with the formula we derived earlier to get a closed formula for
$\sum_{i=1}^{n} i^2$.

\begin{align*}
	3\sum_{i=1}^{n} i^2 + 3\sum_{i=1}^{n} i + n &= (n+1)^3 - 1^3 \\
	3\sum_{i=1}^{n} i^2 &= n^3 +3n^2+3n - \frac{3(n)(n+1)}{2} - n \\
	3\sum_{i=1}^{n} i^2 &= \frac{1}{2}\left(2n^3 +6n^2+6n - 3n^2 -3n - 2n\right) \\
	\sum_{i=1}^{n} i^2  &= \frac{1}{6}\left(2n^3 +3n^2+n\right) \\
	\sum_{i=1}^{n} i^2  &= \frac{1}{6}\left((n)(n+1)(2n+1)\right)
\end{align*}

We can use this method for higher exponents $k$ and, as long as we have formulae for all
of the lower exponents, we can always generate an explicit formula using this method for
$\sum_{i=1}^{n} i^k$.

\section{Using a Binomial identity}

\subsection{Pascal's Triangle}

The second method is closely related to the first, but avoids the requirement to derive
a formula for all of the lower order exponents. We will use a binomial identity which
we can derive from the well-known property of binomial coefficients which gives us
Pascal's triangle, namely:
\[\binom{n}{k} + \binom{n}{k+1} = \binom{n+1}{k+1} \]

We can prove this identity as follows:

\begin{align*}
	\binom{n}{k} + \binom{n}{k+1} &= \frac{n!}{k!(n-k)!} + \frac{n!}{(k+1)!(n-k-1)!} \\
	&= \frac{n!}{k!(n-k-1)!}\left(\frac{1}{n-k} + \frac{1}{k+1}\right) \\
	&= \frac{n!}{k!(n-k-1)!}\left(\frac{(k+1)+(n-k)}{(n-k)(k+1)}\right) \\
	&= \frac{(n+1)!}{(k+1)!((n+1)-(k+1))!} \\
	&= \binom{n+1}{k+1}
\end{align*}

For those who have not heard of it, Pascal's Triangle is an arrangement of the
binomial coefficients in a triangle, and each term in the triangle is the sum of the two
terms directly above it. The first five rows are as follows:

\begin{tabular}{rccccccccc}
$n=0$:&    &    &    &    &  1\\\noalign{\smallskip\smallskip}
$n=1$:&    &    &    &  1 &    &  1\\\noalign{\smallskip\smallskip}
$n=2$:&    &    &  1 &    &  2 &    &  1\\\noalign{\smallskip\smallskip}
$n=3$:&    &  1 &    &  3 &    &  3 &    &  1\\\noalign{\smallskip\smallskip}
$n=4$:&  1 &    &  4 &    &  6 &    &  4 &    &  1\\\noalign{\smallskip\smallskip}
\end{tabular}

Each line represents the coefficients of a binomial expansion. For example, the line $n=3$
corresponds to $\binom{3}{0}, \binom{3}{1}, \binom{3}{2}, \binom{3}{3}$.

By examining Pascal's Triangle (or repeatedly applying the identity above), you can generate
some interesting identities. The one we are interested in is:
\[ \sum_{i=1}^{n} \binom{i}{k} = \binom{n+1}{k+1} \]

This does require some clarification for the value of $\binom{n}{k}$ when $k>n$. In this case, we use the formula for the binomial coefficient we introduced originally:
\[ \binom{n}{k} = \frac{(n)(n-1)(\cdots)(n-k+1)}{k!} \]

For $n<k$ this will yield $\binom{n}{k} = 0$ - and in the context of an interpretation of
this meaning the number of ways we can "choose $k$ items from $n$ options", this makes sense, as it is impossible to choose $k$ items from fewer than $k$ choices.

This identity corresponds to adding up all of the numbers on the diagonal one over from the
number of interest in Pascal's Triangle. For example:
\[ \binom{5}{3} = \binom{4}{2} + \binom{3}{2} + \binom{2}{2} = 6 + 3 + 1 \]

We can justify this by repeatedly applying the binomial identity:
\begin{align*}
	\binom{n+1}{k+1} &= \binom{n}{k} + \binom{n}{k+1} \\
	&= \binom{n}{k} + \binom{n-1}{k} + \binom{n-1}{k+1} \\
	&= \binom{n}{k} + \binom{n-1}{k} + \binom{n-2}{k} + \binom{n-2}{k+1} \\
	&\vdots \\
	&= \sum_{i=1}^{n} \binom{i}{k}
\end{align*}

\subsection{Applying the Identity}

$\binom{n}{k}$ is a polynomial in $n$ of order $k$. For example:
\[ \binom{n}{3} = \frac{1}{6}(n)(n-1)(n-2) = \frac{1}{6}(n^3-3n^2+2n) \]

We can express $n^k$ as a linear combination of $\binom{n}{i}$ for $i\leq k$. For $n=3$,
to continue with that example, we have that:
\begin{align*}
	n^3 &= 6\binom{n}{3} +3n^2-2n \\
	&= 6\binom{n}{3} + 6 \binom{n}{2} +\binom{n}{1}
\end{align*}

In this case, we can now apply this identity along with the one above to get a formula
for the sum of cubes:

\begin{align*}
	\sum_{i=1}^{n} i^3 &= 6\sum_{i=1}^{n} \binom{i}{3} + 6\sum_{i=1}^{n} \binom{i}{2}
	+ \sum_{i=1}^{n} \binom{i}{1} \\
	&= 6\binom{n+1}{4} +6\binom{n+1}{3} + \binom{n+1}{2} \\
	&= 6\frac{(n+1)(n)(n-1)(n-2)}{24} +6 \frac{(n+1)(n)(n-1)}{6} + \frac{(n+1)(n)}{2} \\
	&= (n+1)(n) \frac{(n-1)(n-2) + 4(n-1) + 2}{4} \\
	&= \left(\frac{n(n+1)}{2}\right)^2
\end{align*}

Again, we can use this method for arbitrary values of $k$.

It is straightforward to calculate the values of the coefficients of $\binom{n}{i}$ in
the linear combination when we realize that the equation must be true for all values of
$n$, and (as we saw earlier) $\binom{n}{k} = 0$ if $n<k$. So for example, if we wanted to
calculate the sum $\sum_{i=1}^{n} i^4$, we could use:
\[ n^4 = a_1\binom{n}{1} + a_2\binom{n}{2} + a_3 \binom{n}{3} + a_4 \binom{n}{4} \]
Setting $n=1$, we get the equation:
\[ 1^4 = a_1\binom{1}{1} \implies a_1 = 1 \]

Now setting $n=2$:
\[ 2^4 = a_1\binom{2}{1} + a_2\binom{2}{2} \implies a_2 = 16-2 = 14 \]

With $n=3$:
\[ 3^4 = a_1\binom{3}{1} + a_2\binom{3}{2} + a_3\binom{3}{3}\]
\[\implies a_3 = 81 -3(14) - 3(1)  = 36 \]

Finally, for $n=4$:
\[ 4^4 = a_1\binom{4}{1} + a_2\binom{4}{2} + a_3\binom{4}{3} + a_4\binom{4}{4}\]
\[\implies a_3 = 256 -4(36) - 6(14) - 4(1))  = 24\]

So we have:
\[ n^4 = \binom{n}{1} + 14\binom{n}{2} + 36 \binom{n}{3} + 24 \binom{n}{4} \]

And:
\begin{align*}
	\sum_{i=1}^{n} i^4 &= \binom{n+1}{2} + 14\binom{n+1}{3} + 36 \binom{n+1}{4}
	                      + 24 \binom{n+1}{5} \\
			   &= \frac{1}{30} (n)(n + 1)(2n + 1)(3n^2 +3n - 1)
\end{align*}

\section{Exponential Generating Functions}

I think my favourite method for deriving these formulae is to use generating functions.
This feels the most like magic, and it has a deep connection to the Bernoulli numbers,
which arise often in number theory.

A Generating Function is a polynomial representation of a sequence $\{a_n\}$ such that
$a_i$ is the coefficient of $x^i$ for each $i \geq 0$. For example, the sequence
$\{1, \frac{1}{2}, \frac{1}{4}, \frac{1}{8}, \cdots \}$ corresponds to the polynomial
\[ P(x) = 1 + \frac{x}{2} + \frac{x^2}{4} + \frac{x^3}{8} + \cdots \]

We have a lot of tools for manipulating generating functions, as long as they satisfy
certain constraints. For example, we can take term-wise derivatives or integrals of
the terms to generate new identities or to calculate complicated infinite sums.

In the case of $P(x)$, you might recognize it as a geometric series. If it is convergent,
we can manipulate it as follows:

\[ \frac{x}{2} P(x) = \frac{x}{2} + \frac{x^2}{4} + \frac{x^3}{8} + \cdots \]
\[ P(x) - \frac{x}{2} P(x) = 1 \]
\[ P(x) = \frac{1}{1-\frac{x}{2}} \]

An exponential generating function is similar, but with one difference. Given the sequence
$\{a_n\}$, the generating function is:
\[ P(x) = a_0 + \frac{a_1x}{1!} + \frac{a_2x^2}{2!} + \frac{a_3x^3}{3!} + \cdots \]

That is, each term is of the form $\frac{a_nx^n}{n!}$. What this gains us is that we are
guaranteed convergence for any sequence which does not grow exponentially. It also gives
us the ability to manipulate sequences using exponential rules.

We can use exponential power series to generate formulae for the sum of $i^k$ for all
values of $k$ simultaneously. Define
\[ S_k(n) = \sum_{i=1}^{n} i^k \]

and consider the corresponding exponential generating function:

\begin{align*}
	P(x) &= S_0(n) + \frac{S_1(n)x^1}{1!} + \frac{S_2(n)x^2}{2!} + \cdots
	&  & \text{By definintion} \\
	&= \sum_{i=0}^{\infty} \frac{S_i(n)x^i}{i!} &  & \text{Using summation notation} \\
	&= \sum_{i=0}^{n} \sum_{j=1}^{\infty} \frac{j^ix^i}{i!} &  &
	\text{Breaking apart and distributing } S_i(n) \\
	&= \sum_{j=1}^{n} \sum_{i=0}^{\infty} \frac{j^ix^i}{i!} &  &
	\text{Swapping order of summation} \\
	&= \sum_{j=1}^{n} e^{jx} &  & \text{Definition of the exponential function} \\
	&= \frac{e^{i(n+1)x}-e^x}{e^{x}-1} &  & \text{Summing a geometric series}
\end{align*}

Expanding $e^x$ and $e^{(n+1)x}$, grouping and cross-multiplying, we get:

\[ \left( \sum_{i=1}^{\infty} \frac{x^i}{i!} \right)
	\left( \sum_{i=0}^{\infty} \frac{S_i(n)x^i}{i!}\right) =
	\sum_{i=1}^{\infty} \frac{((n+1)^i - 1)x^i}{i!} \]

\begin{multline*}
	\left( x+\frac{1}{2!}x^2 + \frac{1}{3!}x^3 + \cdots \right)
	\left( S_0(n) + \frac{S_1(n)}{1!}x + \frac{S_2(n)}{2!}x^2 + \cdots \right) = \\
	\left( ((n+1) - 1)x+\frac{((n+1)^2-1)}{2!}x^2 + \frac{(n+1)^3-1)}{3!}x^3 + \cdots \right)
\end{multline*}

By equating the coefficients of $x^{i+1}$ on each side of this equation, we obtain a formula
for $S_i(n)$.

\begin{align*}
	S_0(n) &= n \\
	S_1(n) + \frac{S_0(n)}{2!} &= \frac{1}{2!}((n+1)^2 -1) \\
	\frac{S_2(n)}{2!} + \frac{S_1(n)}{2!} + \frac{S_0(n)}{3!}
	&= \frac{1}{3!}\left( (n+1)^3 - 1 \right) \\
	\frac{S_3(n)}{3!} + \frac{S_2(n)}{2!2!} + \frac{S_1(n)}{3!} + \frac{S_0(n)}{4!}
	&= \frac{1}{4!}\left( (n+1)^4 - 1 \right) \\
\end{align*}

This gives the familiar formulae for $S_i(n)$, and the ability to further expand to
calculate $S_4(n)$ and beyond with a recursion formula.

\begin{align*}
	S_1(n) &= \frac{1}{2}n^2 + \frac{1}{2}n \\
	S_2(n) &= \frac{1}{3}n^3 + \frac{1}{2}n^2 + \frac{1}{6}n \\
	S_3(n) &= \frac{1}{4}n^4 + \frac{1}{2}n^3 + \frac{1}{4}n^2 \\
	S_4(n) &= \frac{1}{5}n^5 + \frac{1}{2}n^4 + \frac{1}{3}n^3 - \frac{1}{30}n
\end{align*}

In general:

\[ \sum_{i=0}^{k} \frac{S_i(n)}{(k - i + 1)!i!} = \frac{(n+1)^{k+1} - 1}{(k+1)!} \]

\end{document}


\documentclass{article}

\usepackage{amsmath}
\usepackage{numprint}

\author{Daniel Fernandes Martins (danielfmt)}
\title{Question \#10 Solution}

\begin{document}

\maketitle

\textbf{Disclaimer.} This is the reasoning I used to solve the problem; it
may be wrong though. This is intended just as food for thought.

\section{Finding The Lower Bound}

Let's say we have the hypothesis sets $\mathcal{H}_1$ and $\mathcal{H}_2$,
with VC dimensions $d_{vc}(\mathcal{H}_1)=2$ and $d_{vc}(\mathcal{H}_2)=3$.
This means that $\mathcal{H}_1$ shatters at most $2$ points, and
$\mathcal{H}_2$ shatters at most $3$ points.

If we join these two hypothesis sets in a single $\mathcal{H}$, we are
guaranteed to shatter at least $3$ points. So, the lower bound is the
maximum number of points that can be shattered by any $\mathcal{H}_k$.

\section{Finding The Upper Bound}

We are given two upper bound candidates, $\sum_{k=1}^Kd_{vc}(\mathcal{H}_k)$
or $K-1+\sum_{k=1}^Kd_{vc}(\mathcal{H}_k)$.

\subsection {Intuition on $\sum_{k=1}^Kd_{vc}(\mathcal{H}_k)$}

Let's start with two hypothesis sets with break point $k=2$ and $N=2$. This
means that these hypothesis sets cannot shatter any combination of 2 points,
which implies that $d_{vc}=1$ for both of them.

These are the dichotomies realized by $\mathcal{H}_1$ on these 2 points:

\begin{equation*}
\begin{split}
\{a=-1, b=+1\} \\
\{a=+1, b=-1\}
\end{split}
\end{equation*}

For $\mathcal{H}_2$:

\begin{equation*}
\begin{split}
\{a=-1, b=-1\} \\
\{a=+1, b=+1\}
\end{split}
\end{equation*}

The union hypothesis set $\mathcal{H}_1\bigcup\mathcal{H}_2$ have $d_{vc}=2$,
so the option where the upper bound is the sum of $d_{vc}$ over all
$\mathcal{H}_k$ seems a reasonable choice, at least for these two hypothesis
sets.

\subsection{Intuition on $K-1+\sum_{k=1}^Kd_{vc}(\mathcal{H}_k)$}

Now let's take $N=3$ points and two new hypothesis sets,
$\mathcal{H}_3$ and $\mathcal{H}_4$. What if we could divide all eight
dichotomies carefully between these two hypothesis sets so that both
hypothesis sets have $d_{vc}=1$?

After moving the dichotomies around, this is $\mathcal{H}_3$:

\begin{equation*}
\begin{split}
\{a=-1, b=-1, c=-1\} \\
\{a=-1, b=-1, c=+1\} \\
\{a=-1, b=+1, c=-1\} \\
\{a=+1, b=-1, c=-1\}
\end{split}
\end{equation*}

This is $\mathcal{H}_4$:

\begin{equation*}
\begin{split}
\{a=-1, b=+1, c=+1\} \\
\{a=+1, b=-1, c=+1\} \\
\{a=+1, b=+1, c=-1\} \\
\{a=+1, b=+1, c=+1\}
\end{split}
\end{equation*}

Noticed something? Well, both hypothesis sets $\mathcal{H}_3$ and
$\mathcal{H}_4$ have $d_{vc}=1$, but the union hypothesis set
$\mathcal{H}_3\bigcup\mathcal{H}_4$ have $d_{vc}=3$. Also, since in this
example $K=2$, the bound seems to hold.

\subsubsection{Relation With $B(N,k)$}

The analysis definitely has to do with $B(N, k)$, although I don't know how
to prove it yet.

\section{Solution}

$$
\max\{d_{vc}(\mathcal{H}_k)\}_{k=1}^K \leq d_{vc}(\bigcup _{k=1}^K)\mathcal{H}_k \leq K-1+\sum_{k=1}^Kd_{vc}(\mathcal{H}_k)
$$

\end{document}


\documentclass[main.tex]{subfiles}
\begin{document}

%\begin{appendices}

\appendixpage
\noappendicestocpagenum
\addappheadtotoc

\chapter{Tables}
\label{appendix:tables}

\epigraph{A surprise, to be sure, but a welcome one.}{Senator Palpatine}

% todo fix
%\dosecttoc
%\faketableofcontents
%\secttoc
\minitoc
%\startcontents[sections]
%\printcontents[sections]{l}{1}{\setcounter{tocdepth}{2}}

We list helpful tables here.

\section{Logical Equivalences}

\begin{table}[H]
	\centering
	\begin{tabular}{lc}
		\toprule
		Commutativity & \(\begin{aligned} p \land q &\equiv q \land p \\ p \lor q &\equiv q \lor p \end{aligned}\) \\
		\midrule
		Associativity & \(\begin{aligned} (p \land q) \land r &\equiv p \land (q \land r) \\ (p \lor q) \lor r &\equiv p \lor (q \lor r) \end{aligned}\) \\
		\midrule
		Distributivity & \(\begin{aligned} p \land (q \lor r) &\equiv (p \land q) \lor (p \land r) \\ p \lor (q \land r) &\equiv (p \lor q) \land (p \lor r) \end{aligned}\) \\
		\midrule
		Identity & \(\begin{aligned} p \land \taut &\equiv p \\ p \lor \cont &\equiv p \end{aligned}\) \\
		\midrule
		Negation & \(\begin{aligned} p \land \lnot p &\equiv \cont \\ p \lor \lnot p &\equiv \taut \end{aligned}\) \\
		\midrule
		Double Negation & \(\begin{aligned} \lnot (\lnot p) &\equiv p \end{aligned}\) \\
		\midrule
		Idempotency & \(\begin{aligned} p \land p &\equiv p \\ p \lor p &\equiv p \end{aligned}\) \\
		\midrule
		Universal Bound & \(\begin{aligned} p \lor \taut &\equiv \taut \\ p \land \cont &\equiv \cont \end{aligned}\) \\
		\midrule
		De Morgan's & \(\begin{aligned} \lnot (p \lor q) &\equiv \lnot p \land \lnot q \\ \lnot (p \land q) &\equiv \lnot p \lor \lnot q \end{aligned}\) \\
		\midrule
		Absorption & \(\begin{aligned} p \lor (p \land q) &\equiv p \\ p \land (p \lor q) &\equiv p \end{aligned}\) \\
		\midrule
		Negation of \(\taut\) and \(\cont\) & \(\begin{aligned} \lnot \taut &\equiv \cont \\ \lnot \cont &\equiv \taut \end{aligned}\) \\
		\midrule
		Implication Equivalence & \(\begin{aligned} p \Rightarrow q &\equiv \lnot p \lor q \end{aligned}\) \\
		\midrule
		Bi-conditional Equivalence & \(\begin{aligned} p \Leftrightarrow q &\equiv (p \Rightarrow q) \land (q \Rightarrow p) \end{aligned}\) \\
		\bottomrule
	\end{tabular}
\end{table}
\pagebreak

\section{Rules of Inference}

\begin{table}[h]
	\centering
	\begin{tabular}{lrr}
		\toprule
		Modus Ponens & & \(\begin{aligned} p \\ p \Rightarrow q \\ \therefore q \end{aligned}\) \\
		\midrule
		Modus Tollens & & \(\begin{aligned} p \Rightarrow q \\ \lnot q \\ \therefore \lnot p \end{aligned}\) \\
		\midrule
		Generalization & \(\begin{aligned} p \\ \therefore p \lor q \end{aligned}\) & \(\begin{aligned} q \\ \therefore p \lor q \end{aligned}\) \\
		\midrule
		Specialization & \(\begin{aligned} p \land q \\ \therefore p \end{aligned}\) & \(\begin{aligned} p \land q \\ \therefore q \end{aligned}\) \\
		\midrule
		Conjunction & & \(\begin{aligned} p \\ q \\ \therefore p \land q \end{aligned}\) \\
		\midrule
		Elimination & \(\begin{aligned} p \lor q \\ \lnot p \\ \therefore q \end{aligned}\) & \(\begin{aligned} p \lor q \\ \lnot q \\ \therefore p \end{aligned}\) \\
		\midrule
		Transitivity & & \(\begin{aligned} p \Rightarrow q \\ q \Rightarrow r \\ \therefore p \Rightarrow r \end{aligned}\) \\
		\midrule
		Cases & & \(\begin{aligned} p \lor q \\ p \Rightarrow r \\ q \Rightarrow r \\ \therefore r \end{aligned}\) \\
		\midrule
		Contradiction & & \(\begin{aligned} \lnot p \Rightarrow \cont \\ \therefore p \end{aligned}\) \\
		\bottomrule
	\end{tabular}
\end{table}
\pagebreak

\section{Set Equivalences}

\begin{table}[h]
	\centering
	\begin{tabular}{lc}
		\toprule
		Commutativity & \(\begin{aligned} A \cup B &= B \cup A \\ A \cap B &= B \cap A \end{aligned}\) \\
		\midrule
		Associativity & \(\begin{aligned} (A \cup B) \cup C &= A \cup (B \cup C) \\ (A \cap B) \cap C &= A \cap (B \cap C) \end{aligned}\) \\
		\midrule
		Distributivity & \(\begin{aligned} A \cap (B \cup C) &= (A \cap B) \cup (A \cap C) \\ A \cup (B \cap C) &= (A \cup B) \cap (A \cup C) \end{aligned}\) \\
		\midrule
		Identity & \(\begin{aligned} A \cup \emptyset &= A \\ A \cap U &= A \end{aligned}\) \\
		\midrule
		Inverse & \(\begin{aligned} A \cup A^{\mathsf{c}} &= U \\ A \cap A^{\mathsf{c}} &= \emptyset \end{aligned}\) \\
		\midrule
		Double Compliment & \(\begin{aligned} (A^{\mathsf{c}})^{\mathsf{c}} &= A \end{aligned}\) \\
		\midrule
		Idempotency & \(\begin{aligned} A \cup A &= A \\ A \cap A &= A \end{aligned}\) \\
		\midrule
		Universal Bound (Domination) & \(\begin{aligned} A \cup U &= U \\ A \cap \emptyset &= \emptyset \end{aligned}\) \\
		\midrule
		De Morgan's & \(\begin{aligned} (A \cup B)^{\mathsf{c}} &= A^{\mathsf{c}} \cap B^{\mathsf{c}} \\ (A \cap B)^{\mathsf{c}} &= A^{\mathsf{c}} \cup B^{\mathsf{c}} \end{aligned}\) \\
		\midrule
		Absorption & \(\begin{aligned} A \cup (A \cap B) &= A \\ A \cap (A \cup B) &= A \end{aligned}\) \\
		\midrule
		Absolute Compliment & \(\begin{aligned} \emptyset^{\mathsf{c}} &= U \\ U^{\mathsf{c}} &= \emptyset \end{aligned}\) \\
		\midrule
		Set Subtraction Equality & \(\begin{aligned} A - B &= A \cap B^{\mathsf{c}} \end{aligned}\) \\
		\bottomrule
	\end{tabular}
\end{table}
\pagebreak

\section{Common Growth-rates}

\begin{table}[h]
	\centering
	\begin{tabular}{ll}
		\toprule
		Constant & \(\Theta(1)\) \\
		\midrule
		Logarithmic & \(\Theta(\log(n))\) \\
		\midrule
		Linear & \(\Theta(n)\) \\
		\midrule
		-- & \(\Theta(n\log(n))\) \\
		\midrule
		Quadratic & \(\Theta(n^2)\) \\
		\midrule
		-- & \(\Theta(n^2\log(n))\) \\
		\midrule
		Polynomial & \(\Theta(n^c)\) \\
		\midrule
		-- & \(\Theta(2^n)\) \\
		\midrule
		Exponential & \(\Theta(c^n)\) \\
		\midrule
		Factorial & \(\Theta(n!)\) \\
		\midrule
		-- & \(\Theta(n^n)\) \\
		\bottomrule
	\end{tabular}
	\caption{\textit{In ascending order of growth-rate.}}
\end{table}

%\end{appendices}

\end{document}


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

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

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

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

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

\subsection{The inductive definition of identity types}

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

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

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

\begin{rmk}
  Since the identity types require getting used to, we provide the formal rules
  for identity types. The identity type is formed by the formation rule:
  \begin{prooftree}
    \AxiomC{$\Gamma\vdash a:A$}
    \UnaryInfC{$\Gamma,x:A\vdash a=_A x~\mathrm{type}$}
  \end{prooftree}
  The constructor of the identity type is then given by the introduction rule:
  \begin{prooftree}
    \AxiomC{$\Gamma\vdash a:A$}
    \UnaryInfC{$\Gamma\vdash \refl{a}:a=_A a$}
  \end{prooftree}
  The induction principle is now given by the elimination rule:
  \begin{prooftree}
    \AxiomC{$\Gamma\vdash a:A$}
    \AxiomC{$\Gamma,x:A,p:a=_A x\vdash P(x,p)~\mathrm{type}$}
    \BinaryInfC{$\Gamma\vdash \mathsf{path\usc{}ind}_a:P(a,\refl{a})\to\prd{x:A}{p:a=_A x}P(x,p)$}
  \end{prooftree}
  And finally the computation rule is:
  \begin{prooftree}
    \AxiomC{$\Gamma\vdash a:A$}
    \AxiomC{$\Gamma,x:A,p:a=_A x\vdash P(x,p)~\mathrm{type}$}
    \BinaryInfC{$\Gamma\vdash \mathsf{path\usc{}ind}_a(p,a,\refl{a})\jdeq p : P(a,\refl{a})$}
  \end{prooftree}
  Furthermore we postulate that any universe is closed under identity types, i.e., that there is a function
  \begin{equation*}
    \check{\mathsf{Id}}:\prd{A:\UU}\mathsf{Ty}(A)\to\mathsf{Ty}(A)\to\UU
  \end{equation*}
  satisfying
  \begin{equation*}
    \mathsf{Ty}(\check{\mathsf{Id}}(A,a,x))\jdeq a=_{\mathsf{Ty}(A)} x.
  \end{equation*}
\end{rmk}

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

\begin{comment}
In the following lemma we show that the identity type on $A$ is contained in any reflexive relation on $A$.

\begin{lem}
Let $\Gamma,x:A,y:A\vdash R(x,y)~\mathrm{type}$\index{reflexive relation|textit}\index{relation!reflexive}, and suppose that $R$ is reflexive in the sense that there is a term
\begin{equation*}
\rho:\prd{x:A}R(x,x)
\end{equation*}
Then there is a term of type
\begin{equation*}
\prd{y:A} (x=_A y)\to R(x,y)
\end{equation*}
in context $\Gamma,x:A$.
\end{lem}

\begin{constr}
By weakening the reflexive relation $R$ we obtain
\begin{equation*}
\Gamma,x:A,y:A,\alpha:x=_A y\vdash R(x,y)~\mathrm{type},
\end{equation*}
on which the induction principle is applicable.
Thus we see that by the induction principle for identity types we have a term
\begin{equation*}
\mathsf{path\usc{}ind}_x : R(x,x)\to \prd{y:A}(x=_A y)\to R(x,y)
\end{equation*}
so it suffices to construct a term of type $R(x,x)$, which we have by reflexivity of $R$.
\end{constr}
\end{comment}

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

Furthermore, we observe that we can iteratively take identity types, i.e., we can take identity types of identity types,
\begin{equation*}
p =_{(x=_Ay)} q,
\end{equation*}
and so on. In other words, for any two identifications $p,q:x=_A y$, there is a type of identifications of $p$ with $y$. One way to think about this is that the identifications $p,q:x=_A y$ are paths in the type (space) $A$, and an identification of $p$ with $q$ is a \emph{higher path} from $p$ to $q$, i.e., a \emph{homotopy}.

Using the observation that identity types can be iterated we show that concatenation is \emph{associative}, satisfies the left and right \emph{unit laws}, and satisfies the left and right \emph{inverse laws}. These are the \define{groupoid operations} on the identity type.

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

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

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

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

\begin{defn}\label{defn:id_assoc}
Let $A$ be a type. We define the \define{associativity operation}\index{associativity operation!for identifications|textbf}, which assigns to each $p:x=y$, $q:y=z$, and $r:z=w$ the \define{associator}
\begin{equation*}
\mathsf{assoc}(p,q,r) : \ct{(\ct{p}{q})}{r}=\ct{p}{(\ct{q}{r})}.
\end{equation*}
\end{defn}

\begin{constr}
By identification elimination it suffices to show that
\begin{equation*}
\prd{z:A}{q:x=z}{z':A}{r:z=w} \ct{(\ct{\refl{x}}{q})}{r}= \ct{\refl{x}}{(\ct{q}{r})}.
\end{equation*}
Let $q:x=z$ and $r:z=w$. Note that by the computation rule $\ct{\refl{x}}{q}\jdeq q$, so $\ct{(\ct{\refl{x}}{q})}{r}\jdeq \ct{q}{r}$. Similarly we have $\ct{\refl{x}}{(\ct{q}{r})}\jdeq \ct{q}{r}$. Therefore we can simply take $\refl{\ct{q}{r}}$.
\end{constr}

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

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

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

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

\subsection{The action on paths of functions}

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

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

\begin{constr}
First we define $\apfunc{f}$ by identity elimination, taking
\begin{equation*}
\apfunc{f}(\refl{x})\defeq \refl{f(x)}.
\end{equation*}
Next, we construct $\mathsf{ap\usc{}idfun}_A$ by identity elimination, taking
\begin{equation*}
\mathsf{ap\usc{}idfun}_A(\refl{x}) \defeq \refl{\refl{x}}.
\end{equation*}
Finally, we construct $\mathsf{ap\usc{}comp}(f,g)$ by identity elimination, taking
\begin{equation*}
\mathsf{ap\usc{}comp}(f,g,\refl{x}) \defeq \refl{g(f(x))}.\qedhere
\end{equation*}
\end{constr}

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

\begin{constr}
To construct $\mathsf{ap\usc{}refl}(f,x)$ we simply observe that ${\ap{f}{\refl{x}}}\jdeq {\refl{f}(x)}$, so we take
\begin{equation*}
\mathsf{ap\usc{}refl}(f,x)\defeq\refl{\refl{f(x)}}.
\end{equation*}
We construct $\mathsf{ap\usc{}inv}(f,p)$ by identification elimination on $p$, taking
\begin{equation*}
\mathsf{ap\usc{}inv}(f,\refl{x}) \defeq \refl{\ap{f}{\refl{x}}}.
\end{equation*}
Finally we construct $\mathsf{ap\usc{}concat}(f,p,q)$ by identification elimination on $p$, taking
\begin{equation*}
\mathsf{ap\usc{}concat}(f,\refl{x},q)  \defeq \refl{\ap{f}{q}}.\qedhere
\end{equation*}
\end{constr}

\subsection{Transport}

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

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

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

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

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

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

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

%\begin{defn}\label{defn:path_lifting}
%Let $A$ be a type, and let $B:A\to\type$ be a type family over $A$.
%We will construct a \define{path lifting} operation
%\begin{equation*}
%\mathsf{lift}^B : \prd*{x,y:A}{p:\id{x}{y}}{b:B(x)} \id{\pairr{x,b}}{\pairr{y,\trans{p}{b}}}.
%\end{equation*}
%\end{defn}
%
%\cref{defn:path_lifting} gives a way to lift a path $p:x=y$ in the base type of a type family, to a path in the $\Sigma$-type. This, along with the basic groupoid operations developed in \cref{sec:groupoid}, inspired the \emph{homotopy interpretation} of type theory.

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

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

\begin{comment}
\item \label{ex:trans_triv}Consider two types $A$ and $B$, and let $p:x=y$ in $A$, and $b:B$.
  \begin{subexenum}
  \item Construct an identification
    \begin{align*}
      \mathsf{tr\usc{}triv}(p,b):\mathsf{tr}_{W_A(B)}(p,b)=b
    \end{align*}
    where $W_A(B)$ is the family $B$ weakened by $A$.
  \item Construct for any $f:A\to B$, an identification
    \begin{equation*}
      \apd{f}{p}=\ct{\mathsf{tr\usc{}triv}(p,f(x))}{\mathsf{ap}_f(p)},
    \end{equation*}
    witnessing that the triangle
    \begin{equation*}
      \begin{tikzcd}[trim right=(a),column sep=0em]
        \mathsf{tr}_{W_A(B)}(p,f(x)) \arrow[dr,equals,swap,"\apd{f}{p}"] \arrow[rr,equals,"{\mathsf{tr\usc{}triv}(p,f(x))}"] & & |[alias=a,right]|f(x) \arrow[dl,equals,"\ap{f}{p}"] \\
        & f(y) & \phantom{\mathsf{tr}_{W_A(B)}(p,f(x))}
      \end{tikzcd}
    \end{equation*}
    commutes.
  \end{subexenum}
\item \label{ex:trans_ap}Let $f:A\to B$ be a map, and consider $p:x=y$ in $A$.
  \begin{subexenum}
  \item Construct for any $q:f(x)=b$ in $B$ an identification
    \begin{equation*}
      \mathsf{tr\usc{}id\usc{}left\usc{}subst}(p,q):\id{\mathsf{tr}_{f(\blank)=b}(p,q)}{\ct{\ap{f}{p}^{-1}}{q}}.
    \end{equation*}
  \item Similarly, construct for any $q':b=f(x)$ in $B$ an identification
    \begin{equation*}
      \mathsf{tr\usc{}id\usc{}right\usc{}subst}(p,q'):\id{\mathsf{tr}_{b=f(\blank)}(p,q)}{\ct{q}{\ap{f}{p}}}.
    \end{equation*}
  \end{subexenum}
\end{comment}


\chapter{Constraints}
\label{cha:conGen}
For some optimization problems it is necessary to impose constraints
on the independent variables and/or the dependent variables, as the following example shows.

\begin{example}{\em
Suppose we want to minimize the heating energy of a building, and suppose that
the normalized mass flow $\dot m$ of the heating system is an independent variable, with constraints
$0 \le \dot m \le 1$.
Without using constraints, the minimum energy consumption would be achieved
for $\dot m = 0$, since then the heating system is switched off.
To solve this problem, we can impose a constraint on a dependent variable.
One possibility is to add a ``penalty'' term to the energy consumption.
This could be such that every time a thermal comfort criterion
(which is a dependent variable)
is violated, a large positive number is added to the energy consumption.
Thus, if $\mathrm{ppd}(x)$, with $\mathrm{ppd} \colon \Re^n \to \Re$,
denotes the predicted percent of dissatisfied people (in percentage), and
if we require that $\mathrm{ppd}(x) \le 10 \%$, we could use the inequality constraint
$g(x) \triangleq \mathrm{ppd}(x) - 10 \le 0$.
}
\phantom{abc} \rbox
\end{example}

In Section~\ref{sec:boxConFre}, the method that is used in GenOpt to implement box constraints is
described.
In Section~\ref{sec:conDepVarGen}, penalty and barrier methods that can be used to
implement constraints on dependent variables are described.
They involve reformulating the cost function and, hence, are problem specific and
have to be implemented by the user.

% ============================================
\section{Constraints on Independent Variables}
\label{sec:conFreParGen}
\subsection{Box Constraints}
\label{sec:boxConFre}

Box constraints are constant inequality constraints that define a feasible set as
\begin{equation}
\mathbf X \triangleq \bigl\{ x \in \Re^n \ | \
l^i \le x^i \le u^i, \ i \in \{1, \ldots, n \} \bigr\},
\end{equation}
where $-\infty \le l^i < u^i \le \infty$ for $i \in \{1, \ldots, n \}$.

In GenOpt, box constraints are either implemented directly
in the optimization algorithm by setting $f(x) = \infty$ for unfeasible iterates,
or, for some algorithms, the independent variable $x \in \mathbf X$ is transformed
to a new unconstrained variable which we will denote in this section by $t \in \Re^n$.

\begin{subequations}
Instead of optimizing the constrained variable $x \in \mathbf X$,
we optimize with respect to the unconstrained variable $t \in \Re^n$.
The transformation ensures that all variables stay feasible during the iteration process.
In GenOpt, the following transformations are used:\\

\noindent
If $l^i \le x^i$, for some $i \in \{1, \ldots, n\}$,
\begin{eqnarray}
  t^i & = &\sqrt{x^i - l^i}, \\
  x^i & = &l^i + (t^i)^2.
\end{eqnarray}
If $l^i \le x^i \le u^i$, for some $i \in \{1, \ldots, n\}$,
\begin{eqnarray}
  t^i & = & \arcsin\left(
    \sqrt{\frac{x^i - l^i}{u^i-l^i}}
    \right), \\
 x^i & = &l^i + (u^i - l^i) \, \sin^2 t^i.
\end{eqnarray}
If $x^i \le u^i$, for some $i \in \{1, \ldots, n\}$,
\begin{eqnarray}
  t^i & = & \sqrt{u^i - x^i}, \\
   x^i & = & u^i - (t^i)^2.
\end{eqnarray}
\label{sub:traBoxCon}
\end{subequations}
% ----------------------------
\subsection{Coupled Linear Constraints}
In some cases the constraints have to be formulated in terms of a linear system of equations of the form
\begin{equation}
   A \, x = b,
\end{equation}
where $A \in \Re^m \times \Re^n$, $x \in \Re^n$, $b \in \Re^m$, and $\mathrm{rank}(A) = m$.\\

There are various algorithms that take this kind of restriction into account. However, such restrictions are rare in building simulation and thus not implemented in GenOpt. If there is a need to impose such restrictions, they can be included by adding an appropriate optimization algorithm and retrieving the coefficients by using the methods offered in GenOpt's class \texttt{Optimizer}.

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

\section{Constraints on Dependent Variables}
\label{sec:conDepVarGen}
We now discuss the situation where the constraints are non-linear and
defined by
\begin{equation}
   g(x) \le 0,
\label{eq:conFunG}
\end{equation}
where $g \colon \Re^n \rightarrow \Re^m$ is once continuously differentiable.
\eqref{eq:conFunG} also allows formulating equality constraints of the form
\begin{equation}
  h(x) = 0,
\label{eq:equCon}
\end{equation}
for $h \colon \Re^n \to \Re^m$, which can be implemented by using penalty functions.
In example, one can define $g^i(x) \triangleq h^i(x)^2$ for $i \in \{ 1, \ldots, m \}$. Then, since $g^i(\cdot)$ is non-negative, the only feasible value is $g(\cdot) = 0$.
Thus, we will only discuss the case of inequality constraints of the form~\eqref{eq:conFunG}.\\

Such a constraint can be taken into account by adding \emph{penalty} or \emph{barrier} functions to the cost function, which are multiplied by a positive weighting factor $\mu$
that is monotonically increased (for penalty functions) or monotonically decreased to zero (for barrier functions).\\

We now discuss the implementation of barrier and penalty functions.
% ---------------------------
\subsection{Barrier Functions}
Barrier functions impose a punishment if the dependent variable gets close to the
boundary of the feasible region.
The closer the variable is to the boundary,
the higher the value of the barrier function becomes.

\noindent To implement a barrier function for $g(x) \le 0$,
where $g \colon \Re^n \to \Re^m$ is a continuously differentiable function whose
elements are strictly monotone increasing,
the cost function $f \colon \Re^n \to \Re$ can
be modified to
\begin{equation}
\widetilde f(x, \mu) \triangleq f(x) + \mu  \frac{1}{\sum_{i=1}^m g^i(x)}
\label{eq:barFun}
\end{equation}
where $\widetilde f \colon \Re^n \times \Re \to \Re$.
The optimization algorithm is then applied to the new function $\widetilde f(x,\mu)$.
Note that~\eqref{eq:barFun} requires that $x$ is in the interior of the feasible set\footnote{I.e., $x$ satisfies the strict inequality $g(x) > 0$.}.

\indent A drawback of barrier functions is that the boundary of the feasible set
can not be reached.
By selecting the weighting factors small, one can get close to the boundary.
However, too small a weighting factor can cause the cost function to be ill-conditioned,
which can cause problems for the optimization algorithm.

Moreover, if the variation of the iterates between successive iterations is too big,
then the feasible boundary can be crossed. Such a behavior must be prevented
by the optimization algorithm, which can produce additional problems.\\

For barrier functions, one can start with a moderately large weighting factor $\mu$
and let $\mu$ tend to zero during the optimization process.
That is, one constructs a sequence
\begin{equation}
  \mu_0 > \ldots > \mu_i > \mu_{i+1} > \ldots > 0.
  \label{eq:barFunWeiFac}
\end{equation}
Section~\ref{sec:ImpWeiFac} shows how $\mu_i$ can be computed in the coarse of the optimization.

Barrier functions do not allow formulating equality constraints of the form~\eqref{eq:equCon}.

% --------------------
\subsection{Penalty Functions}

In contrast to barrier functions,
penalty functions allow crossing the boundary of the feasible set, and they allow
implementation of equality constraints of the form~\eqref{eq:equCon}.
Penalty functions add a positive term to the cost function if a constraint is violated.

\noindent To implement a penalty function for $g(x) \le 0$, where
$g \colon \Re^n \to \Re^m$ is once continuously differentiable and each element is strictly
monotone decreasing,
the cost function $f \colon \Re^n \to \Re$ can
be modified to
\begin{equation}
\widetilde f(x, \mu) \triangleq f(x) + \mu  \sum_{i=1}^m \max(0,  g^i(x))^2,
\label{eq:penFun}
\end{equation}
where $\widetilde f \colon \Re^n \times \Re \to \Re$ is once continuously differentiable in $x$.
The optimization algorithm is then applied to the new function $\widetilde f(x,\mu)$.

As for the barrier method, selecting the weighting factor $\mu$ is not trivial.
Too small a value for $\mu$ produces too big a violation of the constraint.
Hence, the boundary of the feasible set can be exceeded by an unacceptable amount.
Too large a value of $\mu$ can lead to ill-conditioning of the cost function,
which can cause numerical problems.\\

The weighting factors have to satisfy
\begin{equation}
   0 < \mu_0 < \ldots < \mu_i < \mu_{i+1} < \ldots,
  \label{eq:penFunWeiFac}
\end{equation}
with $\mu_i \to \infty$, as $i \to \infty$.
See Section~\ref{sec:ImpWeiFac} for how to adjust $\mu_i$.
% ---------------------------------------

\subsection{Implementation of Barrier and Penalty Functions}
\label{sec:ImpWeiFac}

We now discuss how the weighting factors $\mu_i$ can be adjusted.
For $i \in \Na$, let $x^*(\mu_i)$ be defined as the solution
\begin{equation}
 x^*(\mu_i) \triangleq \arg \min_{x \in \mathbf X} \widetilde f(x, \mu_i),
\end{equation}
where $\widetilde f(x, \mu_i)$ is as in~\eqref{eq:barFun} or~\eqref{eq:penFun}, respectively.
Then, we initialize $i=0$, select an initial value $\mu_0 > 0$ and compute $x^*(\mu_0)$.
Next, we select a $\mu_{i+1}$ such that it satisfies~\eqref{eq:barFunWeiFac}
(for barrier functions) or~\eqref{eq:penFunWeiFac} (for penalty functions),
and compute $x^*(\mu_{i+1})$, using the initial iterate $x^*(\mu_i)$, and increase the counter
$i$ to $i+1$.
This procedure is repeated until $\mu_i$ is sufficiently close to zero (for barrier functions)
or sufficiently large (for penalty functions).\\

To recompute the weighting factors $\mu_i$,
users can request GenOpt to write a counter to the simulation input file, and then compute
$\mu_i$ as a function of this counter.
The value of this counter can be retrieved by
setting the keyword \texttt{WriteStepNumber} in the optimization command file to \texttt{true},
and specifying the string \texttt{\%stepNumber\%} in the simulation input template file.
GenOpt will replace the string \texttt{\%stepNumber\%} with the current counter value
when it writes the simulation input file.
The counter starts with the value $1$ and its increment is $1$.\\

Users who implement their own optimization algorithm in GenOpt can call the method
\texttt{increaseStepNumber(...)} in the class \texttt{Optimizer} to increase the counter.
If the keyword \texttt{WriteStepNumber} in the optimization command file is set to \texttt{true}, the method calls the simulation to evaluate the cost function for the new value of this counter. If \texttt{WriteStepNumber} is \texttt{false}, no new function evaluation is performed by this method since the cost function does not depend on this counter.\\