\chapter{Enumerations}
\section{Bad Example}

Enumerations (also often called `enums') allow us to display numbers as text. Take an enemy class for example. The enemy could be a warrior, a rogue or a priest. Depending on that choice, another image would be displayed on the screen. You could use booleans to remember what type of enemy you're dealing with:

\begin{code}
class enemy {
  bool warrior = false;
  bool rogue   = false;
  bool priest  = false;
  Rect r;

  void setWarrior() {
    warrior = true ;
    rogue   = false;
    priest  = false;
  }

  // the methods setRogue and setPriest are similar
  // ...

  void draw() {
    if     (warrior) Images(=== warriorImage ===).draw(r);
    else if(rogue  ) Images(=== rogueImage   ===).draw(r);
    else if(priest ) Images(=== priestImage  ===).draw(r);
  }
}
\end{code}

Even though this works, it isn't very efficient. Right now, we only got 3 types of enemies, but the more possibilities you have, the more variables you need to change when selecting a type. It would be much better to use a single variable.

\section{Slightly less Bad Example}
You could decide to give a warior the number 0, a rogue 1 and a priest 2. This makes your code a lot easier.

\begin{code}
class enemy {
  int type = -1;
	Rect r;

	void setType(int type) {
		T.type = type;
	}

	void draw() {
		switch(type) {
			case 0: Images(=== warriorImage ===).draw(r); break;
			case 1: Images(=== rogueImage   ===).draw(r); break;
			case 2: Images(=== priestImage  ===).draw(r); break;
	  }
	}
}
\end{code}
Although this is better than the first version, you will mess up sooner or later and use the wrong number somewhere. Or maybe you will use a number for which no class exists.

\section{Enumeration time!}
The solution for this problem are enumerations. These are lists of words. Internally the first word is equal to zero and every word after that is a higher number. The interesting part is that you can use these words in your code, but the application will use numbers internally.

\begin{code}
enum ENEMY_TYPE {
  ET_NONE   ,
  ET_WARRIOR,
  ET_ROGUE  ,
  ET_PRIEST ,
}

class enemy {
  ENEMY_TYPE type = ET_NONE;
	Rect r;

	void setType(ENEMY_TYPE type) {
		T.type = type;
	}

	void draw() {
		switch(type) {
			case ET_WARRIOR: Images(=== warriorImage ===).draw(r); break;
			case ET_ROGUE  : Images(=== rogueImage   ===).draw(r); break;
			case ET_PRIEST : Images(=== priestImage  ===).draw(r); break;
		}
	}
}
\end{code}
Values like \texttt{ET\_WARRIOR} or \texttt{ET\_ROGUE} can be used anywhere in your code. The method \eeFunc{setType} won't accept a type of enemy which doesn't exists. And at every moment you will have clear idea of what you're doing.

The values of an enum are often capitals. This is just a convention, but it's a good one. You can instantly see that this is not a variable. And just like with constants (also often written in capitals), the value of an enum cannot be changed. Something like \texttt{ET\_WARRIOR = 3} is not possible.

Starting the enum value with \texttt{ET\_} is also a matter of choice. I like to use the first characters of the enumeration in its value name because it makes them easier to remember. ``ENEMY\_TYPE'' can be remembered as ET. The autocomplete of the editor will help you by showing your options as soon as you've typed \texttt{ET\_}.

\begin{note}
Esenthel also provides an \textsl{enumeration editor}. This is not needed for simple enumerations, but we will use it later in this course because the enumerations defined in there are also available in the world editor.
\end{note}


\documentclass[letter,12pt]{article}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%	\usepackage{graphicx}
%	\usepackage{amsmath}
%	\usepackage{array}
%	\usepackage{amssymb}
%	\usepackage{setspace}
%	%\usepackage[margin=1.5cm,vmargin={0pt,1cm},nohead]{geometry}
%	\usepackage[margin=1in,vmargin={1in,1in}]{geometry}
%	% Package that has the symbol for ``:=''
%	\usepackage{txfonts}
%	% Create fancy headers and footers for this document
%	\usepackage{fancyhdr}
%	%\usepackage{cite}
%	% The ``cite'' package causes the hyperlinks for the in-text references/citations to fail. I believe it is because this package overrides the default package for referencing. Hence, only use the ``cite'' package with the IEEE format.
%	% Package for ``turnstile'' binary relations, where letters are defined above and below symbols
%	\usepackage{turnstile}
%	\usepackage{extarrows}
%	% Package that provides the cross symbol
%	\usepackage{ifsym}
%	\usepackage{marvosym}
%	% Commands for using the package for hyperlinks -
%	\usepackage[pdftex,
%		pdftitle={Graphics and Color with LaTeX},
%		pdfauthor={Patrick W Daly},
%		pdfsubject={Importing images and use of color in LaTeX},
%		pdfkeywords={LaTeX, graphics, color},
%		pdfpagemode=UseOutlines,bookmarks, bookmarksopen,
%		pdfstartview=FitH, colorlinks, linkcolor=blue, citecolor=blue, urlcolor=red,
%	]{hyperref}
%	\hypersetup{colorlinks, linkcolor=blue}
%	% Concatenate references
%	\usepackage{cite}

%	% Package for tyepsetting algorithms and heuristics
%	\usepackage{listings}
%	\lstset{language=[GNU]C++}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%	Additional packages
\input{/data/others/grappanotes/others/preamble}
%	AMS theorem package
\usepackage{amsthm}

% definition of new \LaTeX command for the citation: \cite{Cimatti08} and \cite{Barrett09}
% This allows mathematical/logic symbols to be typeset with the font ``Zapf Chancery'' in ``\LaTeX\ math mode''. To typeset symbols in such font, try: \mathpzc{ABCdef123}
\DeclareMathAlphabet{\mathpzc}{OT1}{pzc}{m}{it}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Start of document
\begin{document}
\title{Logic Synthesis and Verification Homework \#2}
\date{\today}
\author{Zhiyang Ong
	\thanks{Email correspondence to: \href{mailto:ongz@acm.org}{ongz@acm.org}}
}
\maketitle

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section*{Declaration}
\label{sec:declaration}

I did this assignment on my own without any collaborators. %The mathematical programming package that I have used is \cite{Makhorin2012}.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Cofactor}
\label{sec:cofactor}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Proof that $(\neg f)_{v} = \neg (f_{v})$}
\label{ssec:commutativityofnegation}

Direct proof for $(\neg f)_{v} = \neg (f_{v})$. \\

From the left-hand side (LHS):
\begin{eqnarray*}
	\begin{gathered}[b]
	(\neg f)_{v} = (\neg f(v, v_{1}, \dots, v_{i})) |_{v = 1}, \forall i \in (\mathbb{N} \geq 1) {\rm\ and}\ v = 1\ ^{\dagger} \\
	= (\neg f(1, v_{1}, \dots, v_{i})) \\
	= \neg (f(1, v_{1}, \dots, v_{i}))\ ^{\ddagger} \\
	= \neg (f(v, v_{1}, \dots, v_{i}))|_{v = 1}\ ^{\dagger} \\
	= \neg (f_{v}) {\rm \hspace{0.2in} i.e., right\textendash hand\ side\ (RHS)} \\
	\end{gathered}
\end{eqnarray*}

Notes: \\
$^{\dagger}$ By the definition of the cofactor \cite[\S2.5.1, pp. 69, Definition 2.5.1]{DeMicheli1994}. The value of $i$ is determined by the arity of the function $f$ \cite[\S4.1, pp. 118, NL2]{Tourlakis2008}. \\
$^{\ddagger}$ By the definition of the complement operator, from the postulates of boolean algebra \cite[\S3.1, pp. 30]{Brown1990}.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Proof that $(f\ \langle op \rangle\ g)_{v} = (f_{v})\ \langle op \rangle\ (g_{v})$, for $\langle op \rangle = \{\land, \rightarrow\}$}
\label{ssec:commutativityofnegation}

Direct proof for $(f\ \langle op \rangle\ g)_{v} = (f_{v})\ \langle op \rangle\ (g_{v})$, for $\langle op \rangle = \{\land, \rightarrow\}$. \\

From the left-hand side (LHS):
\begin{eqnarray*}
	\begin{gathered}[b]
	(f\ \langle op \rangle\ g)_{v} \\
	= (f(v, v_{1}, \dots, v_{i})\ \langle op \rangle\ g(v, v_{1}, \dots, v_{i}))_{v}, \forall i \in (\mathbb{N} \geq 1) {\rm\ and}\ v = 1 \\
	= (f(v, v_{1}, \dots, v_{i}) \ \langle op \rangle\ g(v, v_{1}, \dots, v_{i})) |_{v = 1}\ ^{\dagger} \\
	= (f(1, v_{1}, \dots, v_{i}) \ \langle op \rangle\ g(1, v_{1}, \dots, v_{i})) \\
	= f(1, v_{1}, \dots, v_{i}) \ \langle op \rangle\ g(1, v_{1}, \dots, v_{i}) \\
	= f(v, v_{1}, \dots, v_{i})|_{v = 1} \ \langle op \rangle\ g(v, v_{1}, \dots, v_{i})|_{v = 1}\ ^{\dagger} \\
	= f(v, v_{1}, \dots, v_{i})_{v} \ \langle op \rangle\ g(v, v_{1}, \dots, v_{i})_{v} \\
	= (f_{v})\ \langle op \rangle\ (g_{v}) {\rm \hspace{0.2in} i.e., right\textendash hand\ side\ (RHS)} \\
	\end{gathered}
\end{eqnarray*}

Note: \\
$^{\dagger}$ By the definition of the cofactor \cite[\S2.5.1, pp. 69, Definition 2.5.1]{DeMicheli1994}. The value of $i$ is determined by the arity of the function $f$ \cite[\S4.1, pp. 118, NL2]{Tourlakis2008}. \\

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{QBF}
\label{sec:qbf}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Prove or disprove $\forall x, \exists y.f(x, y, z) \rightarrow \exists y, \forall x.f(x, y, z)$}
\label{ssec:q2a}

%	The order of quantifiers do not matter if they are of the same type; i.e., the ordering of universal quantifiers do not matter, and the ordering of existential quantifiers do not matter. However, if the quantifiers are of mixed types (both universal and existential quantifiers), the ordering of these quantifiers do matter. Look under nested quantifiers, or nesting quantifiers.
%	\forall x \exists y (Loves(x,y))	Everybody loves somebody; i.e., everybody has someone whom they love.
%	\exists y \forall x (Loves(x,y))	There is someone who is loved by everyone in the universe.
%	\forall x \exists x (Loves(x,y))	Everyone has someone who loves them.
%	\exists x \forall y (Loves(x,y))	There is someone who loves everyone in the universe.
%	Reference: Raymond J. Mooney, ``First-Order Logic (First-Order Predicate Calculus): Nesting Quantifiers (slide 8),'' presentation slides for the class {\it CS 343: Artificial Intelligence} (Fall 2010), Department of Computer Science, College of Natural Sciences, The University of Texas at Austin, Austin, TX. Available online at: \url{http://www.cs.utexas.edu/~mooney/cs343/slide-handouts/fopc.4.pdf}; last accessed on October 29, 2013.

%	Universal quantifiers are used with implications to form rules, while existential quantifiers are used with conjunction to specify properties about an individual.

%	Identities that relate sentences involving \forall and \exists, using De Morgan's laws
%	(\forall x) \neg P(x) \Rightarrow \neg (\exists x) P(x)
%	\neg (\forall x) P(x) \Rightarrow (\exists x) \neg P(x)
%	(\forall x) P(x) \Rightarrow \neg (\exists x) \neg P(x)
%	(\exists x) P(x) \Rightarrow \neg (\forall x) \neg P(x)
%	\neg \forall x.A = \exists x \neg A

%	Quantified inference rules
%	Universal instantiation:		\forall x P(x), \therefore P(A)
%	Universal generalization:		P(A) \land P(B) \dots \therefore \forall x P(x)
%	Existential instantiation:		\exists x P(x), \therefore P(F)
%	Existential generalization:		P(A) \therefore \exists x P(x)

%	James Pustejovsky, ``First-order logic,'' presentation slides for the class {\it LING 130: Foundations of Semantics} (Fall 2005), Computer Science Department and the Linguistics/Computational Linguistics Program, College of Arts and Sciences, Brandeis University, Waltham, MA.
%	http://www.cs.brandeis.edu/~jamesp/classes/LING130/FirstOrderLogic-1.pdf

%	(P \rightarrow Q) = (\neg P \lor Q)
%	(P \leftrightarrow Q) = ((P \rightarrow Q) \land (Q \rightarrow P))

%	For this question, x is a bound variable and y is a free variable

A disprove for $\forall x, \exists y.f(x, y, z) \rightarrow \exists y, \forall x.f(x, y, z)$ is given as follows. \\

On the right-hand side (RHS), we have $\forall x, \exists y.f(x, y, z)$. This means that for each $x$, there is a $y$ such that the function $f(x, y, z)$ is true. However, on the left-hand side (LHS), we have $\exists y, \forall x.f(x, y, z)$. This means that for each $y$, there is a $x$ such that the function $f(x, y, z)$ is true. Here, the LHS is not equivalent to the RHS. The RHS is quantified over all $x$, while the LHS is quantified over all $y$. \\

$\therefore, \forall x, \exists y.f(x, y, z) \not\rightarrow \exists y, \forall x.f(x, y, z)$. That is, the statement is false. \\

This is because the order for nested quantifiers (universal and existential quantifiers) matters for quantified boolean logic \cite[\S8.2.6, pp. 298]{Russell2010}. \\

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Prove or disprove $\exists x.(f(x, y) \land g(y)) \leftrightarrow (\exists x.f(x, y)) \land g(y)$}
\label{ssec:q2b}

Here is a proof for $\exists x.(f(x, y) \land g(y)) \leftrightarrow (\exists x.f(x, y)) \land g(y)$. \\

On the LHS, the variable $y$ in $\exists x.(f(x, y) \land g(y))$ is a free variable, since it is not bound by the quantifier $\exists x$ \cite[\S5.2.2, pp. 211, Definition 5-4; pp. 215, Definition 5-10]{Kropf1999} \cite[\S3.4, pp. 104]{Harel2000} \cite[\S7.2.2, pp. 135, Definition 7.12]{BenAri2009}. \\

$^{\ddagger}$ Also, since function $g(y)$ is independent of the bounded variable $x$, the quantifier $\exists x$ does not apply to function $g(y)$. \\

$^{\dagger}$ By applying the distributivity law from the postulates of boolean algebra \cite[\S7.2.2, pp. 135, Definition 7.12]{BenAri2009} \cite[\S3.1, pp. 29]{Brown1990}, the LHS becomes:\\
\begin{eqnarray*}
	\begin{gathered}[b]
	\exists x.(f(x, y) \land g(y)) \\
	(\exists x.(f(x, y)) \land (\exists x.g(y)) \ ^{\dagger} \\
	(\exists x.(f(x, y)) \land (\exists x.g(y)) \ ^{\ddagger} \\
	(\exists x.(f(x, y)) \land g(y) \ ^{\ddagger}\ \ {\rm ( = RHS)} \\
	\end{gathered}
\end{eqnarray*}

Therefore, $\exists x.(f(x, y) \land g(y)) \leftrightarrow (\exists x.f(x, y)) \land g(y)$ is true.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Prove or disprove $\exists x.(f(x, y) \lor g(x, y)) \leftrightarrow (\exists x.f(x, y)) \lor (\exists x.g(x, y))$}
\label{ssec:q2c}

Here is a proof for $\exists x.(f(x, y) \lor g(x, y)) \leftrightarrow (\exists x.f(x, y)) \lor (\exists x.g(x, y))$. \\

From the LHS, $\exists x.(f(x, y) \lor g(x, y)) = (\exists x.f(x, y)) \lor (\exists x.g(x, y))$ = RHS $^{\dagger}$ (proved).\\

Notes:\\
$^{\dagger}$ The distributivity law from the postulates of boolean algebra applies to quantifiers as well \cite[\S7.2.2, pp. 135, Definition 7.12]{BenAri2009} \cite[\S3.1, pp. 29]{Brown1990}. Therefore, by definition, $\exists x.(f(x, y) \lor g(x, y)) \leftrightarrow (\exists x.f(x, y)) \lor (\exists x.g(x, y))$ is true. \\

Therefore, $\exists x.(f(x, y) \lor g(x, y)) \leftrightarrow (\exists x.f(x, y)) \lor (\exists x.g(x, y))$ is true.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Quantifier Elimination}
\label{sec:q3}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Procedure to derive CNF formula $\psi = \forall x_{1}, \dots, \forall x_{i}. \phi$}
\label{ssec:q3a}

\begin{codebox}
\Procname{$\proc{Prenex\_CNF\_Derivation}({\it \phi}, i)$}
\label{lst:LABEL}
\zi \Comment {\it Input $\phi$: A CNF formula}
\zi \Comment {\it Input $i$: Number of $v_{i}$ variables to quantify, where $0 \leq i \leq n$}
\zi \Comment {\it Output $\psi$: A prenex/quantified CNF formula, where $\psi = \forall x_{1}, \dots, \forall x_{i}. \phi$}
\zi \Comment {\it Computational time complexity: O(n)}
\zi
\zi \Comment {\it Create an empty list of quantifiers and their quantified variables}
\li $\id{list\_quantifiers} \gets \{\}$
\zi \Comment {\it Enumerate the tokens of $\phi$}
\li \For $\id{temp\_token} \gets [first\_token] \To [last\_token]$
	\Do
\zi	\Comment {\it Is this token a $v_{i}$ variable that needs to be quantified?}
\li	\If $\id{temp\_token} \in \{\id{v_{1}}, \dots, \id{v_{i}}\}$
		\Then
\zi		\Comment {\it Yes, add the $\forall$ quantifier and token to the list of quantifiers and their quantified variables}
\li		$\id{list\_quantifiers} \gets \id{list\_quantifiers} + \forall +  \id{temp\_token}$
		\End
	\End
\zi	\Comment {\it Add the list of quantifiers and their quantified variables to $\phi$}
\li	$\id{\psi} \gets \id{list\_quantifiers} + \id{\phi}$
\li	\Return \id{\psi}
\end{codebox}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Procedure to derive DNF formula $\psi = \exists x_{1}, \dots, \exists x_{i}. \phi$}
\label{ssec:q3a}

\begin{codebox}
\Procname{$\proc{Prenex\_DNF\_Derivation}({\it \phi}, i)$}
\label{lst:LABEL}
\zi \Comment {\it Input $\phi$: A DNF formula}
\zi \Comment {\it Input $i$: Number of $v_{i}$ variables to quantify, where $0 \leq i \leq n$}
\zi \Comment {\it Output $\psi$: A prenex/quantified DNF formula, where $\psi = \exists x_{1}, \dots, \exists x_{i}. \phi$}
\zi \Comment {\it Computational time complexity: O(n)}
\zi
\zi \Comment {\it Create an empty list of quantifiers and their quantified variables}
\li $\id{list\_quantifiers} \gets \{\}$
\zi \Comment {\it Enumerate the tokens of $\phi$}
\li \For $\id{temp\_token} \gets [first\_token] \To [last\_token]$
	\Do
\zi	\Comment {\it Is this token a $v_{i}$ variable that needs to be quantified?}
\li	\If $\id{temp\_token} \in \{\id{v_{1}}, \dots, \id{v_{i}}\}$
		\Then
\zi		\Comment {\it Yes, add the $\exists$ quantifier and token to the list of quantifiers and their quantified variables}
\li		$\id{list\_quantifiers} \gets \id{list\_quantifiers} + \exists +  \id{temp\_token}$
		\End
	\End
\zi	\Comment {\it Add the list of quantifiers and their quantified variables to $\phi$}
\li	$\id{\psi} \gets \id{list\_quantifiers} + \id{\phi}$
\li	\Return \id{\psi}
\end{codebox}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Procedure to perform quantifier elimination for a (prenex) CNF formula}
\label{ssec:q3c}

Use expansion-based quantifier elimination \cite[\S9.2.3, pp. 216]{Kroening2008} to remove the quantifiers. Here, $\phi |_{x = 0}$ means that x is replaced with 0 (boolean false) in $\phi$. Likewise, $\phi |_{x = 1}$ means that x is replaced with 1 (boolean true) in $\phi$.

\begin{codebox}
\Procname{$\proc{quantifier\_elimination}({\it \phi})$}
\label{lst:quantifierelimination}
\zi \Comment {\it Precondition: $\phi$ is a (prenex) CNF formula}
\zi \Comment {\it Input $\phi$: A (prenex) CNF formula}
\zi \Comment {\it Output $\psi$: A quantifier-free CNF formula}
\zi
\zi \Comment {\it Copy of $\phi$}
\li $\id{\phi\_copy} \gets \id{\phi}$
\li \If $[ \proc{prenex\_exists}({\it \phi}) ]$
	\Then
\zi	\Comment {\it Enumerate the tokens of $\phi$}
\li	\For $\id{temp\_token} \gets [first\_token] \To [last\_token]$
	\Do
\zi		\Comment {\it Is token a quantifier?}
\li		\If $( \id{temp\_token} = \exists )$ or $( \id{temp\_token} = \forall )$
		\Then
\zi			\Comment {\it Remove the quantifier and its corresponding bounded variable from $\id{\phi\_copy}$}
\zi			\Comment {\it Temporarily store the quantifier}
\li			$\id{temp\_quantifier} \gets \id{temp\_token}$
\zi			\Comment {\it Remove the quantifier}
\li			$\id{\phi\_copy}$ = remove\_token($\id{\phi\_copy}, \id{temp\_token})$
\zi			\Comment {\it Get the next token, which is the quantifier's corresponding bounded variable}
\li			$\id{temp\_token} \gets next(\id{\phi\_copy}, \id{temp\_token})$
\zi			\Comment {\it Temporarily store the bounded variable}
\li			$\id{temp\_bounded\_variable} \gets \id{temp\_token}$
\zi			\Comment {\it Remove the corresponding bounded variable}
\li			$\id{\phi\_copy}$ = remove\_token($\id{\phi\_copy}, \id{temp\_token})$
\zi			\Comment {\it Expand \id{\phi\_copy} based on these equivalences}
\zi			\Comment {\it $\exists x. \varphi = \varphi|_{x = 0} \lor \varphi|_{x = 1}$}
\zi			\Comment {\it $\forall x. \varphi = \varphi|_{x = 0} \land \varphi|_{x = 1}$}
\li			\If $\id{temp\_quantifier} = \exists$
			\Then
\li				$\id{\phi\_copy} \gets \id{\phi\_copy}|_{x=0} \lor \id{\phi\_copy}|_{x =1}$
\li			\ElseIf $\id{temp\_quantifier} = \forall$
\li				\>$\id{\phi\_copy} \gets \id{\phi\_copy}|_{x=0} \land \id{\phi\_copy}|_{x =1}$
			\End
		\End
\zi		\Comment {\it Return the quantifier-free formula}
\li		$\id{\psi} = \id{\phi\_copy}$
\li		\Return $\psi$
	\End
\li \Else
\zi	\>\Comment {\it Return the quantifier-free $\phi$}
\li	\>$\id{\psi} \gets \id{\phi}$
\li	\>\Return $\psi$
	\End
\end{codebox}

\begin{codebox}
\Procname{$\proc{prenex\_exists}({\it \phi})$}
\label{lst: }
\zi \Comment {\it Precondition: $\phi$ is a (prenex) CNF formula}
\zi \Comment {\it Input $\phi$: A (prenex) CNF formula}
\zi \Comment {\it Output: Return boolean true, if $\phi$ is in prenex form (i.e., $\phi$ is quantified). Else, return boolean false}.
\zi
\zi \Comment {\it Enumerate the tokens of $\phi$}
\li	\For $\id{temp\_token} \gets [first\_token] \To [last\_token]$
	\Do
\li		\If $[$token is a quantifier$]$
		\Then
\li			\Return true
		\End
	\End
\zi \Comment {\it There are no quantifiers in $\phi$}
\li	\Return false
\end{codebox}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
{\linespread{1}
\bibliographystyle{plain}
\bibliography{/data/research/antipastobibtex/references}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{document}


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{float}
\usepackage{listings}
\usepackage{fontspec}
\usepackage{gensymb}
\usepackage{amsmath}

% \setromanfont{Times New Roman}
% \setsansfont{Arial}
\setmonofont{Cousine}
\lstset{basicstyle=\footnotesize\ttfamily}

\title{HW1-Gandarillas-Victor\\
MAE 290A}
\author{Victor Gandarillas}
\date{4 November 2019}

% Discuss both tensegrity structures in your HW1report, specifically including the following for each:
% A) Is this structure potentially inconsistent?  What does that mean?
% B) is this structure undetermined?  What does that mean?  If it is underdetermined, is it pretensionable, or only tensionable under load?  Discuss.
% Also include any other salient points regarding these designs.  You are encouraged to explore well beyond the specific questions asked; extra credit will be granted as appropriate...

\begin{document}

\maketitle

\section{Assumptions}

In this assignment, I used the MATLAB scripts provided: {\ttfamily tensegrity\_statics.m} and {\ttfamily tensegrity\_plot.m}.
The scripts are assumed to be correct and consistent with the theory presented in \cite{skelton}.
No attempt was made to verify or debug these scripts.
The twist angle computed initially in Section \ref{sec:p2} does not result in the tensions in the vertical and diagonal strings being equal.
An iterative approach was used to find the twist angle required to equalize the tensions in the vertical and diagonal strings.

\section{P1}\label{sec:p1}

We construct a Michell Truss from Michell Spirals according to Figure 4.6 of \cite{skelton}.
The Michell Spirals are of order $q\in\{0,1,2,3,4\}$.
Figure \ref{fig:mt} shows a Michell Truss with a tip load (pink arrow) with bars shown in red and strings shown in blue.
The fixed nodes are shown as red triangles and the reaction forces at the fixed nodes due to the applied external load are shown as blue arrows.
Listing \ref{lst:p1} shows that all the bars are in compression and all the strings are in tension.
This is favorable since strings cannot support compression, but are much lighter than bars.

\begin{figure}[H]
  \centering
  \includegraphics[clip, trim=3.5cm 7.5cm 2.5cm 7.5cm, width=0.6\textwidth]{images/michell_tip_load.pdf}
  \caption{Michell Truss with nodes defined using Michell Spirals of order 0-4 and tip load (pink arrow); bars shown in red, strings shown in blue; reaction forces at fixed nodes (red triangles) shown as blue arrows}
  \label{fig:mt}
\end{figure}

As Listing \ref{lst:p1} shows, the Michell Truss is not potentially inconsistent.
That is, the left nullspace of $A_{se}$ is $\{0\}$.
Therefore, none of the loads are in the left nullspace of $A_{se}$.
All loads are in the column space of $A_{se}$.
% In other words, that there exists a solution for each load.

The system is also not underdetermined.
That is, the nullspace of $A_{se}$ is $\{0\}$.
Therefore, there are no degrees of freedom available to pretension the strings (i.e. the system is not pretensionable).

Since the system is not potentially inconsistent or underdetermined, the solution given in Listing \ref{lst:p1} corresponding to the truss in Figure \ref{fig:mt} is unique for a given load.

In the case of zero loads, the solution is zero reaction forces with compressions and tensions also equal to zero.

\section{P2}\label{sec:p2}

The regular minimal prism with 3 bars shown in Figure 3.43 of \cite{skelton} is reproduced in Figure \ref{fig:m3}.
For comparison, the top view of the regular nonminimal prism with 4 bars is shown in Figure \ref{fig:np_top}.

\begin{figure}[H]
  \centering
  \includegraphics[clip, trim=3.5cm 7.5cm 2.5cm 7.5cm, width=0.6\textwidth]{images/minimal_prism_3.pdf}
  \caption{Top view of regular minimal prism with 3 bars and 9 strings; bars shown in red, strings shown in black}
  \label{fig:m3}
\end{figure}

\begin{figure}[H]
  \centering
  \includegraphics[clip, trim=3.5cm 7.5cm 2.5cm 7.5cm, width=0.6\textwidth]{images/nonminimal_prism_4_top.pdf}
  \caption{Top view of regular nonminimal prism with 4 bars and 16 strings, $\alpha=71.5651^\degree$; bars shown in red, strings shown in black}
  \label{fig:np_top}
\end{figure}

The default perspective view of the regular nonminimal prism with 4 bars is shown in Figure \ref{fig:np}.
The twist angle $\alpha$ is selected so that the vertical loads $\gamma_v$ and the diagonal loads $\gamma_d$ are equal.
From Equation 3.66 of \cite{skelton}, in order for $\gamma_d = \gamma_v$,

\begin{align*}
2 \cos{\alpha}\cos{\frac{\pi}{p}} &= -\cos{\left(\alpha + \frac{\pi}{p}\right)}
\end{align*}

must be satisfied for some twist angle $\alpha$, where $p$ is the number of bars.
For $p=4$, $s=16$, $\alpha=71.5651^\degree$.
Note that the twist angle $\alpha$ does not depend on the length of the bars or the radii of the polygons.

\begin{figure}[H]
  \centering
  \includegraphics[clip, trim=3.5cm 7.5cm 2.5cm 7.5cm, width=0.6\textwidth]{images/nonminimal_prism_4.pdf}
  \caption{Regular nonminimal prism with 4 bars and 16 strings, $\alpha=71.5651^\degree$; bars shown in red, strings shown in black}
  \label{fig:np}
\end{figure}

The angle $\alpha=71.5651^\degree$ does not produce the same tension in the vertical and diagonal strings, as seen in Listing \ref{lst:p2a}.
Sweeping across the range of valid twist angles, $\alpha=77.4971^\degree$ produced the minimum tension across the vertical and diagonal strings, as seen in Listing \ref{lst:p2b}.
The nonminimal prism with equal tensions in vertical and diagonal strings is shown in Figure \ref{fig:npe} and the top view is shown in Figure \ref{fig:npte}.

\begin{figure}[H]
  \centering
  \includegraphics[clip, trim=3.5cm 7.5cm 2.5cm 7.5cm, width=0.6\textwidth]{images/nonminimal_prism_4.pdf}
  \caption{Regular nonminimal prism with 4 bars and 16 strings, $\alpha=77.4971^\degree$; bars shown in red, strings shown in black}
  \label{fig:npe}
\end{figure}

\begin{figure}[H]
  \centering
  \includegraphics[clip, trim=3.5cm 7.5cm 2.5cm 7.5cm, width=0.6\textwidth]{images/nonminimal_prism_4_top_eq.pdf}
  \caption{Top view of regular nonminimal prism with 4 bars and 16 strings, $\alpha=77.4971^\degree$; bars shown in red, strings shown in black}
  \label{fig:npte}
\end{figure}

As Listings \ref{lst:p2a} and \ref{lst:p2b} show, the regular nonminimal prism is potentially inconsistent.
That is, the left nullspace of $A_{se}$ has nonzero dimension.
Therefore, if a load in the left nullspace of $A_{se}$ is applied, there is no solution.
This gives rise to soft modes, or instability.

% You can choose a solution with no components in the nullspace.
The system is also underdetermined with 3 DOF.
That is, the nullspace of $A_{se}$ has dimension 3.
Therefore, there are 3 degrees of freedom available to pretension the strings (i.e. the system is pretensionable) so that the solution has no components in the nullspace of $A_{se}$.

Since the system is potentially inconsistent and underdetermined, if there exists a solution, then there exist an infinite number of solutions for any given force in the column space of $A_{se}$.
The solution is chosen to have no components in the nullspace of $A_{se}$.

\section{Code Output}\label{sec:output}

\begin{lstlisting}[caption={Ouput of MitchellTruss4.m},captionpos=t, label={lst:p1}]
mhat =

    20

nhat =

    20

r =

    20

Ase is not potentially inconsistent. Good.

Bar compressions and string tensions with loads as specified,
least squares solution (i.e., NO pretensioning):

c_bars =

    1.0000    1.0934    1.1956    0.2253    0.2971    0.2463    1.3073    0.3803    0.3362    0.2693

No bars under tension.  Good.

t_strings =

    1.0000    1.0934    1.1956    1.3073    0.2253    0.2971    0.3803    0.2463    0.3362    0.2693

The 10 strings are all under tension with tau_min=0.22527. Good.

Ase is not underdetermined (thus, it is not tensionable). The above solution is unique.
\end{lstlisting}

\begin{lstlisting}[caption={Ouput of NonminimalPrism4.m},captionpos=t, label={lst:p2a}]
mhat =

    24

nhat =

    20

r =

    17

Warning: Ase is potentially inconsistent, implying the presence of soft modes,
or instability!  More strings or fixed points should fix the problem.

Bar compressions and string tensions with loads as specified,
least squares solution (i.e., NO pretensioning):
u in column space of Ase, so at least one solution exists, with residual 0.

c_bars =

     0     0     0     0

No bars under tension.  Good.

t_strings =

     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0

Some strings not under tension. Needs different tensioning or external loads.

Ase is underdetermined with 3 DOF. Checking now to see if system is pretensionable,
with tension >= 0.1 in all tethers for zero applied load.

Result with external load ZERO, pretensioned with given tau_min
while minimizing the L1 norm of the tensions:

c_bars =

    0.4406    0.4406    0.4406    0.4406

No bars under tension.  Good.

t_strings =

  Columns 1 through 10

    0.2253    0.2253    0.2253    0.2253    0.2253    0.2253    0.2253    0.2253    0.1511    0.1511

  Columns 11 through 16

    0.1511    0.1511    0.1000    0.1000    0.1000    0.1000

The 16 strings are all under tension with tau_min=0.1. Good.

Pretensionable!
Results with external forces u as specified and tensioned with given tau_min
while minimizing the L1 norm of the tensions.

c_bars =

    0.4406    0.4406    0.4406    0.4406

No bars under tension.  Good.

t_strings =

  Columns 1 through 10

    0.2253    0.2253    0.2253    0.2253    0.2253    0.2253    0.2253    0.2253    0.1511    0.1511

  Columns 11 through 16

    0.1511    0.1511    0.1000    0.1000    0.1000    0.1000

The 16 strings are all under tension with tau_min=0.1. Good.
\end{lstlisting}

\begin{lstlisting}[caption={Ouput of NonminimalPrism4.m with equal tensions across vertical and diagonal strings},captionpos=t, label={lst:p2b}]
mhat =

    24

nhat =

    20

r =

    17

Warning: Ase is potentially inconsistent, implying the presence of soft modes,
or instability!  More strings or fixed points should fix the problem.

Bar compressions and string tensions with loads as specified,
least squares solution (i.e., NO pretensioning):
u in column space of Ase, so at least one solution exists, with residual 0.

c_bars =

     0     0     0     0

No bars under tension.  Good.

t_strings =

     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0

Some strings not under tension. Needs different tensioning or external loads.

Ase is underdetermined with 3 DOF. Checking now to see if system is pretensionable,
with tension >= 0.1 in all tethers for zero applied load.

Result with external load ZERO, pretensioned with given tau_min
while minimizing the L1 norm of the tensions:

c_bars =

   0.395484232457561   0.395484232457561   0.395484232457562   0.395484232457561

No bars under tension.  Good.

t_strings =

  Columns 1 through 9

   0.217788637615165   0.217788637615165   0.217788637615165   0.217788637615165   0.217788637615165   0.217788637615165   0.217788637615165   0.217788637615165   0.100018090280014

  Columns 10 through 16

   0.100018090280014   0.100018090280014   0.100018090280014   0.100000000000000   0.100000000000000   0.100000000000000   0.100000000000000

The 16 strings are all under tension with tau_min=0.1. Good.

Pretensionable!
Results with external forces u as specified and tensioned with given tau_min
while minimizing the L1 norm of the tensions.

c_bars =

   0.395484232457561   0.395484232457561   0.395484232457562   0.395484232457561

No bars under tension.  Good.

t_strings =

  Columns 1 through 9

   0.217788637615165   0.217788637615165   0.217788637615165   0.217788637615165   0.217788637615165   0.217788637615165   0.217788637615165   0.217788637615165   0.100018090280014

  Columns 10 through 16

   0.100018090280014   0.100018090280014   0.100018090280014   0.100000000000000   0.100000000000000   0.100000000000000   0.100000000000000

The 16 strings are all under tension with tau_min=0.1. Good.
\end{lstlisting}

\bibliography{bib}
\bibliographystyle{plain}
\end{document}


\documentclass[12pt]{article}
\newcommand{\s}{\mathrm{s}}
\renewcommand{\deg}{\mathrm{deg}}
\newcommand{\m}{\mathrm{m}}
\newcounter{problem}
\begin{document}
\thispagestyle{empty}

\section*{NYU Physics 1---Problem set 4}

Due Tuesday 2009 October 13 at the beginning of lecture.

\paragraph{Problem~\theproblem}\refstepcounter{problem}%
\textsl{(a)}~In lecture we calculated the speed a car must go to get
around a banked circular turn at constant speed, in the absence of
friction.  Compute the value of this speed for a turn with radius
$30\,\m$ banked at $10\,\deg$ to the horizontal.

\textsl{(b)}~Now imagine that the car has a coefficient of static
friction for transverse forces (that is, opposing sliding up or down
the bank) of 0.2.  What is the maximum speed at which the car can go
around the turn?  At the maximum speed, the transverse frictional
force will be just 0.2 times the normal force.

\textsl{(c)}~In coming to your answer, make sure you draw a very clear
free-body diagram.

\paragraph{Problem~\theproblem}\refstepcounter{problem}%
\textsl{(a)}~Estimate the amount of energy (in J) in a gallon of
gasoline by imagining that there is about 4 eV of energy for every
carbon atom.  You will have to make assumptions, since there is no
simple molecular formula for gasoline!  Compare your answer to an
answer on the web.  If they differ substantially, explain why you
think that might be.  Be sure to use a reliable source on the web, or
obtain multiple answers, since web pages can be mistaken.

\textsl{(b)}~Now consider a kiloton of gasoline.  Does it have more or
less energy than a kiloton of TNT?  By what factor?

\textsl{(c)}~How much kinetic energy is there in a rock that is a
cube, $10\,\m$ on a side, moving at speed $\sqrt{g\,R}$, where $g$ is
the acceleration due to gravity and $R$ is the radius of the Earth?
Give your answer in units of kilotons of TNT equivalent.  Why do you
think I ask about that particular velocity?

\paragraph{Problem~\theproblem}\refstepcounter{problem}%
\textsl{(a)}~For a good golfer, a $150\,\m$ drive is easy.  If you
assume that air resistance does not matter, how much kinetic energy
$K$ does the ball have at the beginning of its flight?  (You will have
to look up the mass online.)  Assume it is hit at $45\,\deg$ to the
horizontal.

\textsl{(b)}~Now consider a realistic (though not exact) air
resistance force that is directly opposed to the direction of motion
and has magnitude
\begin{equation}
F_\mathrm{air} = \frac{1}{2}\,\rho\,A\,v^2 \quad ,
\end{equation}
where $\rho$ is the density of air, $A$ is the cross-sectional area of
the golf ball (you will have to look this up online) and $v$ is the
speed of the ball through the air.

Make a spreadsheet or computer program, with time in intervals of
$0.01\,\s$, integrating the trajectory with gravity plus this air
resistance force.  You will want columns or variables that are at
least $t$, $x$, $y$, $v_x$, $v_y$, $v$ (magnitude of velocity),
$F_\mathrm{air}$ (magnitude of air resistance force), $F_x$, and
$F_y$, where the last two are the $x$ and $y$ components of the net
force (gravity plus air resistance).  You will have to make sure you
deal with the direction of the air resistance force correctly.

Now adjust the initial speed until the shot goes approximately
$100\,\m$.  That is, increase the velocity at $t=0$ until $x$ is
approximately $150\,\m$ in the time interval at which $y$ goes
negative (it hits the ground).  Assume that the shot is still at
$45\,\deg$.

Hand your spreadsheet or table of internal variables.

\textsl{(c)}~Compute the initial kinetic energy for your computed
trajectory and compare it to the value you got in part~(a).

\textsl{(d)}~If you try to get your spreadsheet to make a shot that is
$250\,\m$, you will find it is virtually impossible!  Why?  Good
golfers can easily hit a ball $250\,\m$.  What do they do differently?

\end{document}


\documentclass{article}
\usepackage{graphicx}
\usepackage[utf8]{inputenc}
\usepackage{amsmath, amssymb, latexsym}

\usepackage{pgfplots}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\usepackage{tikz}
\usepackage{nicefrac}
\pgfplotsset{every axis legend/.append style={
at={(0,0)},
anchor=north east}}
\usetikzlibrary{shapes,positioning,intersections,quotes}
\usetikzlibrary{arrows.meta,
                bending,
                intersections,
                quotes,
                shapes.geometric}

\definecolor{darkgreen}{rgb}{0.0, 0.6, 0.0}
\definecolor{darkred}{rgb}{0.7, 0.0, 0.0}
\makeatletter
\def\BState{\State\hskip-\ALG@thistlm}
\makeatother
\title{Week 14}
\begin{document}
\pagenumbering{gobble}
\maketitle
\newpage
\pagenumbering{arabic}

\section*{Compression}

\begin{itemize}
  \item Speeds up algorithms.
  \item Saves space.
  \item Dimension reduction: not all features are needed.
  \item Example: different units for same attribute.
\end{itemize}

\includegraphics[width=0.5\textwidth]{resources/compression_units}

~\\
Now we can represent x1 as a 1D number (Z dimension).

\section*{Visualization}

\begin{itemize}
  \item It is difficult to visualize higher dimensional data.
  \item Dimensionality reduction can help us show information in a more readable fashion for human consumption.
  \item Collect a huge data set including numerous facts about a country from around the world.
\end{itemize}

\includegraphics[width=0.8\textwidth]{resources/table}

\begin{itemize}
  \item Assume each country has 50 characteristics.
  \item How can we better comprehend this data?
  \item Plotting 50-dimensional data is quite difficult.
  \item Create a new feature representation (2 z values) that summarizes these features.
  \item Reduce $50D\ ->\ 2D$ (now possible to plot).
\end{itemize}

\section*{Principle Component Analysis (PCA): Problem Formulation}

\begin{itemize}
  \item Assume we have a 2D data collection that we want to reduce to 1D.
  \item How can we choose a single line that best fits our data?
  \item The distance between each point and the projected version should be as little as possible (blue lines below are short).
  \item PCA tries to find a lower dimensional surface so the sum of squares onto that surface is minimized.
  \item PCA tries to find the surface (a straight line in this case) which has the minimum projection error.
\end{itemize}

\includegraphics[width=0.5\textwidth]{resources/pca}

\begin{itemize}
  \item PCA is not linear regression.
  \item For linear regression, fitting a straight line to minimize the straight line between a point and a squared line. VERTICAL distance between point.
  \item For PCA minimizing the magnitude of the shortest orthogonal distance.
  \item With PCA there is no $y$ - instead we have a list of features and all features are treated equally.
\end{itemize}

\section*{PCA Algorithm}

\begin{itemize}
  \item Compute the covariance matrix.

        $$\Sigma = \frac{1}{m} \sum_{i=1}^{n} (x^{(i)})(x^{(i)})^T$$
  \item This is an $[n x n]$ matrix (Remember than $x^i$ is a $[n \times 1]$ matrix).
  \item Next, compute eigenvectors of matrix $\Sigma$.
  \item [U,S,V] = svd(sigma)
  \item $U$ matrix is also an $[n \times n]$ matrix. Turns out the columns of $U$ are the u vectors we want!
  \item Just take the first k-vectors from U.
  \item Next, calculate $z$.  $$z = (U_{reduce})^T \cdot x$$
\end{itemize}

\section*{Reconstruction from Compressed Representation}

\begin{itemize}
  \item Is it possible to decompress data from a low dimensionality format to a higher dimensionality format?

        $$x_{approx} = U_{reduce} \cdot z$$

  \item We lose some information (everything is now precisely aligned on that line), but it is now projected into 2D space.
\end{itemize}

\section*{Choosing the number of Principle Components}

\begin{itemize}
  \item PCA attempts to minimize the averaged squared projection error.

        $$\frac{1}{m} \sum_{i=1}^{m} ||x^{(i)} - x_{approx}^{(i)}||^2$$

  \item Total data variation may be defined as the average over data indicating how distant the training instances are from the origin.

        $$\frac{1}{m} \sum_{i=1}^{m} ||x^{(i)}||^2$$

  \item To determine k, we may use the following formula:

        $$\frac{\frac{1}{m} \sum_{i=1}^{m} ||x^{(i)} - x_{approx}^{(i)}||^2}{\frac{1}{m} \sum_{i=1}^{m} ||x^{(i)}||^2} \leq 0.01$$

\end{itemize}

\section*{Applications of PCA}

\begin{itemize}
  \item Compression: Reduce the amount of memory/disk space required to hold data.
  \item Visualization: k=2 or k=3 for plotting.
  \item A poor application of PCA is to avoid over-fitting. PCA discards certain data without understanding what values it is discarding.
  \item Examine how a system works without PCA first, and then apply PCA only if you have reason to believe it will help.
\end{itemize}

\end{document}


\documentclass{beamer}

\usepackage{beamerthemevictor,comment,verbatim,graphicx,amssymb}

\input{tutmacs}
\input{slidemacs}
\input idxmacs

\begin{document}

\title{NP-completeness}
\author{Victor Eijkhout}
\date{Notes for CS 594 -- Fall 2004}

\frame{\titlepage}

\section{Introduction}

\frame[containsverbatim]{
  \frametitle{Complexity theory}
\begin{itemize}
\item What is the running time of an algorithm as function of its input?
\item What is the memory need of an algorithm as function of its
  input?
\item Best, worst, average, expected.
\item Notation: $f(n)=O(g(n))$, $f(n)=\Theta(g(n))$
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{Tractable and intractable}
\begin{itemize}
\item Tractable: polymomial complexity
\[ f(n)=O(n^k)\quad\hbox{some $k$} \]
\item Intractable: exponential complexity
\[ f(n)=O(2^n) \quad\hbox{or something like that} \]
\end{itemize}
}

\frame{
  \frametitle{Intractable problems}
\begin{itemize}
\item They exist
\item<2-> What do you tell your boss?
\pgfimage[width=3.5in]{np-prove}
\end{itemize}
}

\frame{
  \frametitle{Bounds versus reality}
\begin{itemize}
\item Ideal: algorithm and bound are similar
\item NP: all known algorithms are intractable, but not provably
\item<2-> Wrong way to approach your boss:
\pgfimage[width=3.5in]{np-dumb}
\end{itemize}
}

\frame{
  \frametitle{Some consolation}
\begin{itemize}
\item Problems in NP are equivalent: solve one, solve them all
\item You're not alone: no one knows an efficient algorithm
\item<2-> What to tell your boss:
\pgfimage[width=3.5in]{np-famous}
\end{itemize}
}

\sectionframe{Decision problems}
\subsection{Optimization}

\frame[containsverbatim]{
  \frametitle{Optimization vs decision}
\begin{itemize}
\item Many problems are optimization type:
\item traveling salesman: what is the shortest route
\item For complexity analysis more convenient decision problem
\item translation needed: given a bound $B$, is there a route shorter
  than~$B$
\end{itemize}
}

\subsection{Language of a problem}

\frame{
  \frametitle{Language of a problem}
\begin{itemize}
\item For a problem, there are `instances'
\item example: in traveling salesman problem, set of numbered cities
  $\{(i,c_i)\}_i$
\item<2-> `Yes set' of a problem: $Y_\Pi$ contains the instances that
  give a `yes' decision
\item<2-> Encoding~$e$ gives `language of a problem':
\[ L[\Pi,e]=\{ \hbox{the instances in $Y_\Pi$ encoded under $e$} \} \]
\item<3-> for traveling salesman: ordered list of cities $\langle
  c_1,c_2,\ldots\rangle$
\end{itemize}
}

\subsectionframe{Turing machines}

\frame{
  \frametitle{Accepting strings}
\begin{itemize}
\item A Turing machine can halt in `yes' state~$q_y$, `no'
  state~$q_n$, or not halt.
\item The TM \emph{accepts} a string if it halts in~$q_y$
\item The \emph{language} is the set of all accepted strings
\end{itemize}
}

\frame{
  \frametitle{`Solving' a problem}
 A Deterministic Turing Machine (DTM) $M$ \emph{solves} a problem~$\Pi$\\
 (equivalently: recognizes $L[\Pi,e]$) iff
\begin{itemize}
\item The DTM for all strings over the input alphabet
\item $L_M=L[\Pi,e]$
\item<2-> Example: traveling salesman problem for network of cities
  and bound~$B$,
\item<2-> $M$ always returns yes or no
\item<2-> input is list of cities
\item<2-> $M$ returns yes if list satisfies bound
\end{itemize}
}

\frame{
  \frametitle{Solution checking vs generating}
\begin{itemize}
\item In the traveling salesman problem, `solving' means checking a
  solution
\item<2-> Other example: primality testing
\item<2-> Input is number, output is yes/no (this number is prime~/
  not prime)
\item<2-> $\Rightarrow$ more intuitive notion of solving
\end{itemize}
}

\subsectionframe{Complexity classes}

\frame{
  \frametitle{Class $P$}
\begin{itemize}
\item Definition in terms of languages:
\[ P=\{L:\hbox{there is DTM that recognizes $L$ in polynomial
  time}\} \]
\item<2-> in terms of problems:
\begin{eqnarray*}
 \Pi\in P&\equiv& L[\Pi,e]\in P\quad\hbox{for some encoding $e$}\\
        &\equiv&\hbox{there is a polynomial time DTM
                 that recognizes $L[\Pi,e]$}
\end{eqnarray*}
\item<3-> Interpretation:\\
$\Pi\in P$ means is a polynomial time Turing machine~$M$\\
that recognizes~$Y_\Pi$\\
for strings not in~$Y_\Pi$: it halts in state~$q_N$
\end{itemize}
}

\frame{
  \frametitle{Class $NP$}
\begin{itemize}
\item \emph{Certificate}: guessed solution\\
(e.g.\ list of cities for traveling salesman)
\item Non-Deterministic Turing Machine:\\
first generate certificate by $\epsilon$-moves\\
then check correctness
\item<2-> Let NDTM $M$, then $L_M$ is the set of decision problems for
  which $M$ can generate a certificate
\item<2-> Certificate exists $\Leftrightarrow$ decision problem has
  `true' answer
\item<3-> $\Pi\in NP$ iff there is NDTM~$M$ that recognizes~$L[\Pi]$ in
  polynomial time
\item<4-> $\Pi$ is in~NP iff
there is a polynomial time function~$A(\cdot,\cdot)$ such that
\[ w\in Y_\Pi \Leftrightarrow \exists_C:A(w,C)=\mathrm{true}.\]
\end{itemize}
}

\frame{
  \frametitle{Examples}
\begin{itemize}
\item Trivially in $P$: given $a,b,n$, check whether $a\times b=n$
\item<2-> Non-trivially in $P$: given $n$, check whether $a,b$ exist
\item<3-> Not in $P$: actually find $a,b$\\
\[ \Theta(\exp((n\cdot 64/9)^{1/3})(\log n)^{2/3}) \]
\item<4-> Provably exponential time: best move in chess
\item<5-> Unclear: traveling salesman
\end{itemize}
}

\sectionframe{NP-completeness}
\subsection{Polynomial transformations}
\frame[containsverbatim]{
  \frametitle{Definition of Polynomial transformation}
\begin{itemize}
\item $L_1$~and~$L_2$ languages over alphabets $\sum_1^*$~and~$\sum_2^*$
\item $f:\sum_1^*\mapsto\sum_2^*$ is called polynomial transformation
  of problem~1 into problem~2 if
\begin{itemize}
\item There is DTM that computes~$f(x)$ in time~$T_f(x)\leq p_f(|x|)$ for
  polynomial~$p_f$, and
\item For all $x\in\sum_1^*$, $x\in L_1$ iff $f(x_1)\in L_2$.
\end{itemize}
\item `many-to-one polynomial transformation'
\end{itemize}
}

\frame{
  \frametitle{Relations}
\begin{itemize}
\item Let $f$ polynomial transformation from $L_1$ to~$L_2$,
\item then \[ L_2\in P\Rightarrow L_1\in P \]
\item<2-> Proof: Let $M_2:L_2\rightarrow\{0,1\}$ DTM that
  recognizes~$L_2$,\\ then $M_2\circ f$~is DTM that recognizes~$L_1$,
\item<3-> composite recognizer runs in time
\[ T_{M_2\circ f}(x)\leq p_{T_2}(|p_f(|x|)|) \]
which is polynomial
\end{itemize}
}

\frame{
  \frametitle{Relations'}
\begin{itemize}
\item Notation: $L_1$ transforms to $L_2$ (in polynomial time):
\[ L_1\leq L_2 \]
\item (suggested reading: $L_1$ is easier than~$L_2$.)
\item<2-> Transitivity:
\[ L_1\leq L_2 \wedge L_2\leq L_3 \Rightarrow L_1\leq L_3, \]
\end{itemize}
}

\subsectionframe{NP-complete}

\frame{
  \frametitle{Definition}
\begin{itemize}
\item $L$ is NP-complete if
\begin{itemize}
\item $L\in NP$, and
\item for all $L'\in NP$: $L'\leq L$
\end{itemize}
\item<2-> First condition: NDTM has to halt.
\item<2-> Without that: NP-hard
\item<2-> Example: halting problem
\end{itemize}
}

\frame{
  \frametitle{Relations''}
\begin{itemize}
\item If $L_1,L_2\in NP$, $L_1$ is NP-complete, and $L_1\leq
  L_2$,\\ then $L_2$ is NP-complete.
\item<2-> Proof: need to check $\forall_{L_2\in NP}:L'\leq L_2$
\item<2-> $L_1$ is NP-complete, so $L'\leq L_1$.\\ Now use transitivity
\end{itemize}
}

\subsectionframe{Proof of NP-completeness}

\frame{
  \frametitle{Bootstrap problem}
\begin{itemize}
\item Have one NP-complete problem, construct translation, have
  another
\item Where to begin? How to check that every other problem is easier?
\item<2-> Strategy: for NP-complete problem, NDTM exists
\item<2-> encode that NDTM in some specific problem
\item<3-> First done for \textsc{Sat}, boolean satisfiability\\ Cook, 1971
\end{itemize}
}

\frame{
  \frametitle{Satisfiability}
\begin{itemize}
\item Problem statement: given boolean variables $x_1\ldots x_n$ and a
  logical formula $F(x_1,\ldots,x_n)$\\ is there is an assignment
  $x_i\mapsto \{0,1\}$ such that $F(x_1,\ldots)=1$.
\item<2-> Examples: $x_1\vee\neq x_1$ is always true;\\ $x_1\wedge\neq
  x_1$ is always false\\ $x_1\wedge \neq x_2$ is only true for
  $(x_1=T,x_2=F)$.
\item<2-> second example not satisfiable
\item<3-> \textsc{Sat} is in NP
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{Transformations into \textsc{Sat}}
\begin{itemize}
\item Let $\Pi$ be NP-complete; $M$~an NDTM that solves it
\item construct logical formula such that
\[ \hbox{formula true}\Leftrightarrow\hbox{successful TM run} \]
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{The Turing machine}
\[ M = \langle Q, s, \Sigma, F, \delta\rangle
\]
where
\begin{description}
\item[$Q$] is the set of states, and $s\in Q$ the initial
       state,
\item[$\Sigma$] the alphabet of
     tape symbols,
\item[$F\subset Q$] the set of accepting states, and
\item[$\delta\subset Q\times\Sigma\times Q\times\Sigma\times\{-1,+1\}$] the
  set of transitions,
\end{description}
Assume $M$ accepts or rejects instances in time
$p(n)$ where $n$~is the size of the instance and $p(\cdot)$ is a
polynomial.
}

\frame[containsverbatim]{
  \frametitle{Variables}
For $q\in Q$, $-p(n)\leq i\leq p(n)$, $j\in\Sigma$, $0\leq k\leq p(n)$:
\begin{tabular}{|lp{2in}l|}
\hline
Variables&
Intended interpretation&
How many\\
\hline
$T_{ijk}$&
True iff tape cell $i$ contains symbol~$j$ at step~$k$ of the computation&
$O(p(n)^2)$\\
$H_{ik}$&
True iff the $M$'s read/write head is at tape cell~$i$ at step~$k$ of the computation.&
$O(p(n)^2)$\\
$Q_{qk}$&
True iff~$M$ is in state~$q$ at step~$k$ of the computation.&
$O(p(n))$\\
\hline
\end{tabular}
}

\frame[containsverbatim]{
  \frametitle{Expression}
Conjunction of whole bunch of clauses\\
($-p(n) \leq i\leq p(n)$, $j\in\Sigma$, and~$0\leq k\leq p(n)$)

\emph{Initial conditions}
\begin{tabular}{|p{.8in}p{1.2in}p{1.3in}p{.7in}|}
\hline
For all:&
Add the clauses&
Interpretation&
How many clauses?\\
\hline
Tape cell~$i$ of the input~$I$ contains symbol~$j$.&
$T_{ij0}$&
Initial contents of the tape.&
$O(p(n))$
\\
&
$Q_{s0}$&
Initial  state of~$M$&
$O(1)$
\\
&
$H_{00}$&
Initial position of read/write head.&
$O(1)$
\\
\hline
\end{tabular}
}

\frame[containsverbatim]{
  \frametitle{Constraint clauses}
\begin{tabular}{|p{.8in}p{1.2in}p{1.3in}p{.7in}|}
\hline
For all:&
Add the clauses&
Interpretation&
How many clauses?\\
\hline
symbols\newline $j \not=j'$&
$T_{ijk} \rightarrow \neg T_{ij'k}$&
One symbol per tape cell.&
$O(p(n)^2)$
\\
states\newline $q \not=q'$&
$Q_{qk} \rightarrow  \neg  Q_{q'k}$&
Only one state at a time.&
$O(p(n))$
\\
cells\newline $i \not=i'$&
$H_{ik} \rightarrow  \neg  H_{i'k}$&
Only one head position at a time.&
$O(p(n))$
\\
\hline
\end{tabular}
}

\frame[containsverbatim]{
  \frametitle{Turing machine clauses}
\begin{tabular}{|p{.8in}p{1.2in}p{1.3in}p{.7in}|}
\hline
For all:&
Add the clauses&
Interpretation&
How many clauses?\\
\hline
$i,j,k$&
$T_{ijk} =T_{ij(k+1)} \vee H_{ik}$&
Tape remains unchanged unless written.&
$O(p(n)^2)$
\\
$f\in F$&
The disjunction of the clauses $Q_{f,p(n)}$&
Must finish in an accepting state.&
$O(1)$\\
\\
\hline
\end{tabular}
}

\frame[containsverbatim]{
  \frametitle{Transition table clauses}
\begin{tabular}{|p{.8in}p{1.2in}p{1.3in}p{.7in}|}
\hline
For all:&
Add the clauses&
Interpretation&
How many clauses?\\
\hline
$(q, \sigma ,q', \sigma', d)\allowbreak \in\delta$&
The disjunction of the clauses\newline
$(H_{ik} \wedge  Q_{qk} \wedge T_{i\sigma k})
\rightarrow  (H_{(i+d)(k+1)} \wedge Q_{q'(k+1)} \wedge T_{i\sigma' (k+1)})$&
Possible transitions at computation step~$k$ when head is at position~$i$.&
$O(p(n)^2)$
\\
\hline
\end{tabular}
}

\frame[containsverbatim]{
  \frametitle{Equivalence}
If there is an accepting computation for~$M$ on input~$I$,\\
then~$B$ is satisfiable, by assigning $T_{ijk}$, $H_{ik}$ and~$Q_{ik}$

\medskip
If~$B$ is satisfiable,\\
then there is an accepting computation for~$M$ on input~$I$:\\
follows the steps indicated by the assignments to the variables.
}

\frame[containsverbatim]{
  \frametitle{Polynomial time}
\begin{itemize}
\item $O(p(n)^2)$ Boolean variables, each encodable in space $O(\log
  p(n))$
\item  Number of clauses $O(p(n)^2)$
\item Total size of $B$ is $O((\log p(n))p(n)^2)$
\end{itemize}
}

\end{document}
\frame[containsverbatim]{
  \frametitle{}
\begin{itemize}
\item
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{}
\begin{itemize}
\item
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{}
\begin{itemize}
\item
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{}
\begin{itemize}
\item
\end{itemize}
}

\end{document}


\documentclass{article}
\usepackage{fullpage} %small margins etc.
%\usepackage{algorithmic} %pseudocode
%\usepackage{algorithm} %pseudocode as whole procedures
%\usepackage{program} %more sophisticated pseudocode
%\usepackage{amssymb} % mathematical symbols (if you use program, comment this)
\usepackage{pseudocode}
\usepackage{url} %url addresses
\usepackage{multirow}
%\usepackage{booktabs}
%\usepackage{color}
\usepackage{graphicx} %images
\usepackage{float} %for proper placement of floating figures

\providecommand{\e}[1]{\ensuremath{\times 10^{#1}}}

\newcommand{\comment}[1]{}

\begin{document}

\title{Halley's Method and Jarratt's Method implementations}
\date{May 30, 2011}
\author{Bysiek Mateusz, Witan Maciej (Computer Science A/R)}
\maketitle

\section{Task}

Write a computer program to implement the Halley and the Jarratt's methods for
finding a root of the complex polynomial

\[ p(x) = \sum_{k=0}^{n}{a_k x^k}  \]

Use Horner's scheme. Compare the results.

\section{Theoretical introduction}

\subsection{Basics of approximation}

The approximation of values of functions is a very important topic in mathematics. This is the area
where beautiful mathematical formulas meet with the real problems of engineering and the financial
world. Among the most characteristic problems within the field of approximation, there is a problem
of root finding.

One of the solutions to this problem is iteration. In this approach, you do not go directly to the
final solution, but rather get close to it in your first step, and then get closer and closer in
each subsequent step, until you reach your desired precision. If $x_k$ is the k-th step of
iteration, then:

\[ x_k \stackrel{k \rightarrow \infty}{\longrightarrow} \alpha \Longrightarrow f(\alpha) = 0 \]

For such approach, one needs an initial guess $x_0$. Usually, one should form a vector of initial
guesses in case of $x \in R$ or a matrix in case of $x \in C$. The tollerable error value should
also be chosen. Then, starting from every field, one launches iteration using the chosen method. In
vast majority of cases, if after around 20 iterations the method will not converge, it means that it
diverges and will not find a proper root for this guess at all.

To sum up, no method is 100\% reliable, and some are better in one types of functions, whereas
other are better in approximation of other types of functions.

Our task was to find roots of polynomials of higher orders. There are many methods for those
wishing to find those roots, some of them are fast, but not precise. Other are more precise, but
also slower, and they may require very favorable initial conditions, to end successfully. The
question arises: how fast does your iterative method converge?

To differentiate all the methods, the idea of rate of convergence was born. The higher that rate
is, the faster the given method converges. Almost all widely used approximation methods work well
not only in the field of real numbers, but also with functions of complex variables (polynomials of
complex coefficients and/or complex roots). The exception here is the Bisection Method, although it
is not the role of this paper to describe it in detail.

\subsection{Newton's Method}

We will, however, describe the two methods that we have implemented. Both of the methods we are
analysing in this paper have been derived from the Newton's Method. Thus, the ``parent'' looks like
this:

\begin{center}
\[ x_1 = x_0 - \frac{f(x_0)}{f'(x_0)} \]
(\ldots)
\[ x_{n+1} = x_n - \frac{f(x_n)}{f'(x_n)} \]
\end{center}

In above: $x_0$ is the initial guess, $x_n$ is the solution after the $n^{th}$ iteration. The
Newton's method has rate of convergence equal to $2$. In case of our task, both Halley's and
Jarratt's methods have rate of convergence equal to $3$, so they converge faster. But, do they
behave in the same way?

\subsection{Halley's Method in detail}

\subsubsection{Mathematical properties}

The method is used for functions of one real variable with a continuous second derivative. It is
named after its inventor Edmond Halley, who also discovered Halley's Comet. As mentioned earlier,
the rate of convergence to the root is cubic. That kind of rate means quite quick convergence.
Formula of this method is as follows:

\[ x_{n+1} = x_n - \frac{2 f(x_n) f'(x_n)}{2 [f'(x_n)]^2 - f(x_n)f''(x_n)} \]

\subsubsection{Pseudocode}

For the purpose of being clearer to the reader, as well as better relation to our actual script, we
have divided the pseudocode into two parts.

Single step of the approximation is called halleyStep. This function takes the polynomial $p$ and
current approximation of the root $x0$. It returns the corrected value $x$ and the tollerance $tol$.

\begin{pseudocode}{halleyStep}{p, x_0}
	x \gets x_0 \\
	n \gets length(p)-1 \\
	a \gets n:-1:1 \\
	q \gets a.*p(1:n) \\
	b \gets (n-1):-1:1 \\
	u \gets b.*q(1:n-1) \\
	corr \gets (2 \times polyval(p,x) \times polyval(q,x) ) / (2 \times polyval(q,x)^2 - polyval(p,x)
	\times polyval(u,xi)) \\
	x \gets x - corr \\
	tol \gets corr \\
	\RETURN{x, tol}
\end{pseudocode}

Main algorithm of the whole Halley's method uses the above-described code. The function
takes polynomial $p$, the initial guess $x0$, tollerance $tol$, and maximum number of iterations
$maxIter$. It returns the final solution $x$ and number of iterations $l$. If the function returns
$l = maxIter$, it means that most probably the final solution is wrong, and therefore, our initial
guess was too far from the correct solution.

\begin{pseudocode}{halleyMethod}{p, x_0, tol, maxIter}
	\IF x_0 = 0 \THEN
		x_0 \gets tol \\
	x \gets x_0 \\
	l \gets 0 \\
	flag\gets 0 \\
	\WHILE flag = 0 \DO
	\BEGIN
		\left[ x,corr \right] = halleyStep(p, x) \\
		\IF norm(corr) \leq tol \THEN
			flag \gets 1 \\
		\IF l > maxIter \THEN
			flag \gets 1 \\
		l \gets l+1 \\
	\END \\
	\RETURN{x, l}
\end{pseudocode}

\subsection{Jarratt's Method in detail}

\subsubsection{Mathematical properties}

Jarrat's Method was introduced in 1966. Interestingly, it has rate of convergence equal to 3, the
same as Halley's, but does not require taking second derivative of the given function. Therefore, it
can be used on functions that do not have continous second derivative, but more or less keeps the
convergence rate. The formula is as follows:

\[ x_{n+1} = x_n - \frac{f(x_n)}{f'( x_n-\frac{1}{2}f(x_n)/f'(x_n) )} \]

\subsubsection{Pseudocode}

The procedure of finding the root is similar, however, the core of the algorithm is different.
Still, both functions take the same arguments, and are expected to yield results in exact same
format.

\begin{pseudocode}{jarrattStep}{p, x_0}
	x \gets x_0 \\
	n \gets length(p)-1 \\
	a \gets n:-1:1 \\
	q \gets a.*p(1:n) \\
	t \gets x - 0.5 \times polyval(p,x) / polyval(q,x) \\
	corr \gets polyval(p,x) / polyval(q,t) \\
	x \gets x - corr \\
	tol \gets corr \\
	\RETURN{x, tol}
\end{pseudocode}

\begin{pseudocode}{jarrattMethod}{p, x_0, tol, maxIter}
	\IF x_0 = 0 \THEN
		x_0 \gets tol \\
	x \gets x_0 \\
	l \gets 0 \\
	flag\gets 0 \\
	\WHILE flag = 0 \DO
	\BEGIN
		\left[ x,corr \right] = jarrattStep(p, x) \\
		\IF norm(corr) \leq tol \THEN
			flag \gets 1 \\
		\IF l > maxIter \THEN
			flag \gets 1 \\
		l \gets l+1 \\
	\END \\
	\RETURN{x, l}
\end{pseudocode}

\section{Implementation}

To implement the above-mentioned methods, we have chosen MATLAB environment. The backbone of both
algorithms is very similar, therefore implementations of both methods look very alike.

\subsection{Halley's Method}

Two files: $halleyStep.m$ and $halleyMethod.m$, contain the implementations of Algorithms 2.1 and
2.2 from this document.

\subsection{Jarratt's Method}

The second method is also implemented in two files, each of which has very similar design to the
files comprising the Halley's Method. File $jarrattStep.m$ contains Algorithm 2.3 and file
$jarrattMethod.m$ contains Algorithm 2.4.

\section{Examples}

Using MATLAB's built in tool to draw color maps, we have created four examples with two color maps
for each. Scale on the right of every graph shows the number of iterations needed to reach
convergence. The maximum number of steps (after which the algorithm ends regardless whether it
was converging or not) was set to 25.

The file $examples.m$ contains four example polynomials, which we have described in ``Examples''
section of this document, below. The files $halleyColor.m$ and $jarrattColor.m$ contain example
usage of the methods, together with color maps.

In the examples, we use random number generator, but to get results that may be repeated by the
reader, we do this:

\[rand('state',0)\]

\subsection{First polynomial}
As a warm-up, very easy function, generated with the formula: $p_1 = poly(rand(1,9) + i*rand(1,9))$

\[ p_1(x) \approx 1x^9 + (-5.2-6i)x^8 + (-3.7+27.8i)x^7 + (48.9-32.3i)x^6 + (-76.6-21.3i)x^5 +\]
\[ + (31.2+65.7i)x^4 + (18-41.2i)x^3 + (-17.2+5.4i)x^2 + (3.7+2.2i)x^1 + (-0.1-0.5i) \]

Both Halley's and Jarratt's Methods dealt with this problem quite quickly. Halley's Method (Figure
1) was slightly faster, but as one can see on attached graphs, both of the methods had the same
ares of faster and slower convergence.

\begin{figure}[H]
	\begin{minipage}[b]{0.48\linewidth}
		\centering
		\includegraphics[scale=0.68]{example1halley.jpg}
		\caption{$1^{st}$ example, Halley's Method}
		\label{fig:figure1}
	\end{minipage}
	\hspace{0.5cm}
	\begin{minipage}[b]{0.48\linewidth}
		\centering
		\includegraphics[scale=0.68]{example1jarratt.jpg}
		\caption{$1^{st}$ example, Jarratt's Method}
		\label{fig:figure2}
	\end{minipage}
\end{figure}

\subsection{Second example}
The second polynomial looks as follows: $p_2 = poly([1,1,1,1,i,i,i,-i,1+i,1+i,1+i])$

\[ p_2(x) = (x-1)^4(x-i)^3(x-1-i)^3(x+i) \]

In this example the multiplicity of the polynomial's roots plays the most signifficant role. In case
of multiple roots, the initial guess has to be close to the actual solution, otherwise both methods
fail to converge. In case of simple roots, both methods converge in just a few iterations, and
Halley again requires fewer.

\begin{figure}[H]
	\begin{minipage}[b]{0.48\linewidth}
		\centering
		\includegraphics[scale=0.68]{example2halley.jpg}
		\caption{$2^{nd}$ example, Halley's Method}
		\label{fig:figure3}
	\end{minipage}
	\hspace{0.5cm}
	\begin{minipage}[b]{0.48\linewidth}
		\centering
		\includegraphics[scale=0.68]{example2jarratt.jpg}
		\caption{$2^{nd}$ example, Jarratt's Method}
		\label{fig:figure4}
	\end{minipage}
\end{figure}

\subsection{Third polynomial}
The third example, based on Wilkinson's polynomial: $w_3 = poly(1 : 1 : 100)$

\[w_3(x)=(x-1)(x-2)(x-3) \ldots (x-98)(x-99)(x-100)\]

The Wilkinson's polynomial itself is a specific polynomial which was used by James H. Wilkinson in
1963 to illustrate a difficulty when finding the root of a polynomial: the location of the roots can be
very sensitive to perturbations in the coefficients of the polynomial. It was designed as a
polynomial having an arbitrary number of integer roots: $1, 2, 3, \ldots ,n-1, n$. In our case,
there are 100 roots. The main characteristic of this function is that it has very high coefficients
of the $x$ terms of higher order. This causes it to be very ill conditioned.

Halley's Method converges only when the intitial guess is near to the correct answer. In case of any
signifficant distance between the initial guess and any root, the method diverges. The reason is
that this polynomial has very large values between the roots, since the floating point numbers
lose all of their precision when operating on numbers with large integer part.

Jarratt's Method performs better, but still the so-called ``Willkinson's monster'' gives it the hard
time.

\begin{figure}[H]
	\begin{minipage}[b]{0.48\linewidth}
		\centering
		\includegraphics[scale=0.68]{example3halley.jpg}
		\caption{$3^{rd}$ example, Halley's Method.}
		\label{fig:figure5}
	\end{minipage}
	\hspace{0.5cm}
	\begin{minipage}[b]{0.48\linewidth}
		\centering
		\includegraphics[scale=0.68]{example3jarratt.jpg}
		\caption{$3^{rd}$ example, Jarratt's Method.}
		\label{fig:figure6}
	\end{minipage}
\end{figure}

\subsection{Fourth polynomial}
The fourth and the last example included in our work is the polynomial which is very difficult to
approximate for both methods. The formula is as follows:
\[w_4 = poly(ones(1,10) + i*(1e-10) * rand(1,10)) \]

The roots of this polynomial are very close to each other, and because of that, the initial guess
already has to be close to the exact solution, for any of those two methods to converge. Jattatt's
Mehtod performs good when starting in close proximity of the target. On the other hand, for Halley's
Method, the area on which the correct answers are found is wider, but the total number of initial
guesses that ended in convergence is lower.

\begin{figure}[H]
	\begin{minipage}[b]{0.48\linewidth}
		\centering
		\includegraphics[scale=0.68]{example4halley.jpg}
		\caption{$4^{th}$ example, Halley's Method.}
		\label{fig:figure7}
	\end{minipage}
	\hspace{0.5cm}
	\begin{minipage}[b]{0.48\linewidth}
		\centering
		\includegraphics[scale=0.68]{example4jarratt.jpg}
		\caption{$4^{th}$ example, Jarratt's Method.}
		\label{fig:figure8}
	\end{minipage}
\end{figure}

\section{Comparison of numerical results}

All tables in this section use the following shortcuts:
\begin{itemize}
  \item result: solution provided by the method, after it ended
  \item steps: number of steps for a given method until reaching tolerable solution
\end{itemize}

The tolerance for our examples was set to $0.001$.

\subsection{More details for example polynomial no.1}
In the set of roots of the polynomial mentioned earlier, there are such numbers:
\[0.60684258\underline{35417866} +0.79193703\underline{74270354}i\]
\[0.8214071642\underline{952533} +0.9169044399\underline{134076}i\]

Underlined digits in the above mean that error was at most that big, and underlined digits in table
below emphasise size of the error of calculations.

\begin{tabular}{rrrrr}
\multicolumn{2}{c}{} & \multicolumn{2}{c}{} \\
method & initial guess & result & steps \\
\hline
Halley & \multirow{2}{*}{1 + 5i}
& 0.6068425835\underline{608280} + 0.79193703742\underline{15257}i & 15 \\
Jarratt &
& 0.60684258\underline{28269729} + 0.79193703\underline{88755362}i & 16 \\

Halley & \multirow{2}{*}{1.5 + 5i}
& 0.6068425835\underline{630573} + 0.791937037\underline{3940863}i & 15 \\
Jarratt &
& 0.60684258\underline{45474732} + 0.79193703\underline{59690249}i & 16 \\

Halley & \multirow{2}{*}{2 + 5i}
& 0.6068425835\underline{512001} + 0.7919370374\underline{113323}i & 17 \\
Jarratt &
& 0.6068425835\underline{571063} + 0.79193703742\underline{09735}i & 18 \\

Halley & \multirow{2}{*}{2.5 + 5i}
& 0.8214071642\underline{775634} + 0.9169044399\underline{240325}i & 15 \\
Jarratt &
& 0.8214071642\underline{853524} + 0.91690443991\underline{83010}i & 18 \\

Halley & \multirow{2}{*}{3 + 5i}
& 0.8214071642\underline{860094} + 0.91690443991\underline{81225}i & 15 \\
Jarratt &
& 0.8214071642\underline{793698} + 0.91690443991\underline{67766}i & 17 \\
\end{tabular}
\vspace{10pt}

In this case (case of an ordinary polynomial) Halley's Method converges faster in all provided
examples. Both algorithms present similar precision, in some cases Halley converged closer, in other
it was Jarratt.

%\subsection{New example}

%(\ldots)

\section{Conclusion}

After running some more examples, we have reached the conclusion that, as expected because of common
ancestor (Newton's Method), both Halley's and Jarratt's methods perform in similar fashion, and they
in general have many common strong-points and weak-points. Halley's Method performs slightly better
in general, as the number of steps in most initial guesses is smaller when compared to the number of
steps that Jarratt's Method needed. Jarratt's Method performs slightly better when dealing with
functions that have large values and change rapidly, because the impact on the method that use only
the $1^{st}$ derivative of such function is smaller, when compared to the impact on the method that
derives twice.

To sum up, our observations confirm that both methods should have the same rate of convergence, and
that they both are improved versions of Newton's Method. We hope that this paper will help the
reader with making a choice, which method of approximation to use in his or her real-life problem :)

\section{Sources}

\begin{itemize}

	\item J. M. McNamee - \emph{Numerical methods for roots of polynomials, Part 1}, Elsevier B.V.,
	Amsterdam, First Edition, 2007

	\item \url{http://en.wikipedia.org/wiki/Newton's_method}

	\item \url{http://en.wikipedia.org/wiki/Halley's_method}

	\item \url{http://en.wikipedia.org/wiki/Wilkinson's_polynomial}

\end{itemize}

\section{High quality color maps}
On the following pages there are high resolution images representing convergence for examples from
Section 4. They somehow better illustrate the arguments made earlier in this document.

\includegraphics[scale=0.95]{example1halleyHigh.jpg}

\includegraphics[scale=0.95]{example1jarrattHigh.jpg}

\includegraphics[scale=0.95]{example2halleyHigh.jpg}

\includegraphics[scale=0.95]{example2jarrattHigh.jpg}

\includegraphics[scale=0.95]{example3halleyHigh.jpg}

\includegraphics[scale=0.95]{example3jarrattHigh.jpg}

\includegraphics[scale=0.95]{example4halleyHigh.jpg}

\includegraphics[scale=0.95]{example4jarrattHigh.jpg}

\end{document}