% !TEX root = main.tex
% !TEX spellcheck = en-US

\section{Non-malleability of Plonk}
\label{sec:plonk}
In this section, we show that $\plonkprotfs$ is simulation-extractable. To this end, we first use the unique opening property to show that
$\plonkprotfs$ has the $\ur{3}$ property,
cf.~\cref{lem:plonkprot_ur}.
Next, we show that $\plonkprotfs$ is rewinding-based knowledge sound. That is, given a number of accepting transcripts whose first $3$ messages match, we can either extract a witness for the proven statement or use one of the transcripts to break the $\udlog$ assumption. This result is shown in the AGM, cf.~\cref{lem:plonkprot_ss}. We then show that $\plonkprotfs$ is $3$-programmable trapdoor-less ZK in the AGM, cf.~\cref{lem:plonk_tlzk}.

Given rewinding-based knowledge soundness, $\ur{3}$ and trapdoor-less zero-knowledge of $\plonkprotfs$, we invoke \cref{thm:se} and conclude that $\plonkprotfs$ is simulation-extractable.

\newcommand{\vql}{\vec{q_{L}}}
\newcommand{\vqr}{\vec{q_{R}}}
\newcommand{\vqm}{\vec{q_{M}}}
\newcommand{\vqo}{\vec{q_{O}}}
\newcommand{\vx}{\vec{x}}
\newcommand{\vqc}{\vec{q_{C}}}

\subsection{Plonk Protocol Description}
\label{sec:plonk_explained}
\oursubsub{The constraint system.}
Assume $\CRKT$ is a fan-in two arithmetic circuit, whose
fan-out is unlimited and has $\numberofconstrains$ gates and $\noofw$ wires
($\numberofconstrains \leq \noofw \leq 2\numberofconstrains$). The constraint
system of $\plonk$ is defined as follows:
\begin{compactitem}
	\item Let $\vec{V} = (\va, \vb, \vc)$, where $\va, \vb, \vc
	\in \range{1}{\noofw}^\numberofconstrains$. Entries $\va_i, \vb_i, \vc_i$ represent indices of left,
	right and output wires of the circuit's $i$-th gate.
	\item Vectors $\vec{Q} = (\vql, \vqr, \vqo, \vqm, \vqc) \in
	(\FF^\numberofconstrains)^5$ are called \emph{selector vectors}:
	\begin{inparaenum}[(a)]
		\item If the $i$-th gate is a multiplication gate then $\vql_i = \vqr_i = 0$,
		$\vqm_i = 1$, and $\vqo_i = -1$.
		\item If the $i$-th gate is an addition gate then $\vql_i = \vqr_i  = 1$, $\vqm_i =
		0$, and $\vqo_i = -1$.
		\item $\vqc_i = 0$ for multiplication and addition gates.\footnote{The $\vqc_i$ selector vector is meant to encode (input independent) constants.}
	\end{inparaenum}
\end{compactitem}

We say that vector $\vx \in \FF^\noofw$ satisfies constraint system if for all $i
\in \range{1}{\numberofconstrains}$
\[
\vql_i \cdot \vx_{\va_i} + \vqr_i \cdot \vx_{\vb_i} + \vqo \cdot \vx_{\vc_i} +
\vqm_i \cdot (\vx_{\va_i} \vx_{\vb_i}) + \vqc_i = 0.
\]

Public inputs $\brak{\inp_j}_{j = 1}^{\instsize}$ are enforced by adding the constrains
\[ \va_i = j, \vql_i = 1, \vqm_i = \vqr_i = \vqo_i = 0, \vqc_i = -\inp_j\,,
\]
for some $i \in \range{1}{\noofc}$.

\oursubsub{Algorithms rolled out}
\label{sec:plonk_explained}
\plonk{} argument system is universal. That is, it allows to verify computation
of any arithmetic circuit which has up to $\numberofconstrains$
gates using a single SRS. However, to make computation efficient, for each
circuit there is allowed a preprocessing phase which extends the SRS with
circuit-related polynomial evaluations.

For the sake of simplicity of the security reductions presented in this paper, we
include in the SRS only these elements that cannot be computed without knowing
the secret trapdoor $\chi$. The rest of the preprocessed input can
be computed using these SRS elements. We thus let them to be computed by the
prover, verifier, and simulator separately.

\ourpar{$\plonk$ SRS generating algorithm $\kgen(\REL)$:}
The SRS generating algorithm picks at random $\chi \sample \FF_p$, computes
and outputs
\(
\srs = \left(\gone{\smallset{\chi^i}_{i = 0}^{\numberofconstrains + 5}},
\gtwo{\chi} \right).
\)

\ourpar{Preprocessing:}
Let $H = \smallset{\omega^i}_{i = 1}^{\numberofconstrains }$ be a
(multiplicative) $\numberofconstrains$-element subgroup of a field $\FF$
compound of $\numberofconstrains$-th roots of unity in $\FF$. Let $\lag_i(X)$ be
the $i$-th element of an $\numberofconstrains$-elements Lagrange basis. During
the preprocessing phase polynomials $\p{S_{id j}}, \p{S_{\sigma j}}$, for
$\p{j} \in \range{1}{3}$, are computed:
\begin{equation*}
\begin{aligned}
\p{S_{id 1}}(X) & = X,\\[\myskip]
\p{S_{id 2}}(X) & = k_1 \cdot X,\\[\myskip]
\p{S_{id 3}}(X) & = k_2 \cdot X,
\end{aligned}
\qquad
\begin{aligned}
\p{S_{\sigma 1}}(X) & = {\textstyle{\sum_{i = 1}^{\noofc} \sigma(i) \lag_i(X)}},\\[\myskip]
\p{S_{\sigma 2}}(X) & = {\textstyle \sum_{i = 1}^{\noofc}
	\sigma(\noofc + i) \lag_i(X)},\\[\myskip]
\p{S_{\sigma 3}}(X) & ={\textstyle\sum_{i = 1}^{\noofc} \sigma(2 \noofc + i) \lag_i(X)}.
\end{aligned}
\end{equation*}
Coefficients $k_1$, $k_2$ are such that $H, k_1 \cdot H, k_2 \cdot H$ are
different cosets of $\FF^*$, thus they define $3 \cdot \noofc$
different elements. Gabizon et al.~\cite{EPRINT:GabWilCio19} notes that it is enough to set
$k_1$ to a quadratic residue and $k_2$ to a quadratic non-residue.

Furthermore, we define polynomials $\p{q_L}, \p{q_R}, \p{q_O}, \p{q_M}, \p{q_C}$
such that
\begin{equation*}
\begin{aligned}
\p{q_L}(X) & = {\textstyle \sum_{i = 1}^{\noofc}} \vql_i \lag_i(X), \\
\p{q_R}(X) & = \textstyle \sum_{i = 1}^{\noofc} \vqr_i \lag_i(X), \\
\p{q_M}(X) & = \textstyle \sum_{i = 1}^{\noofc} \vqm_i \lag_i(X),
\end{aligned}
\qquad
\begin{aligned}
\p{q_O}(X) & = \textstyle  \sum_{i = 1}^{\noofc} \vqo_i \lag_i(X), \\
\p{q_C}(X) & =  \textstyle \sum_{i = 1}^{\noofc} \vqc_i \lag_i(X). \\
\vphantom{\p{q_M}(X)  = \textstyle \sum_{i = 1}^{\noofc} \vqm_i \lag_i(X),}
\end{aligned}
\end{equation*}

\ourpar{Proving statements in $\plonkprotfs$} We show how prover's algorithm
$\prover(\srs, \inp=\brak{\wit'_i}_{i = 1}^\instsize, \wit = \brak{\wit_i}_{i=1}^{3 \cdot \noofc})$ operates for
the Fiat--Shamir transformed version of Plonk. Note that for notational convenience $\wit$ also contains the public input wires $\wit'_i=\wit_i$, $i\in \range{1}{\ell}$.
\begin{description}
	\item[Message 1] Sample $b_1, \ldots, b_9 \sample \FF_p$; compute
	$\p{a}(X), \p{b}(X), \p{c}(X)$ as
	\begin{align*}
	\p{a}(X) &= (b_1 X + b_2)\p{Z_H}(X) + \textstyle \sum_{i = 1}^{\noofc} \wit_i \lag_i(X) \\
	\p{b}(X) &= (b_3 X + b_4)\p{Z_H}(X) + \textstyle \sum_{i = 1}^{\noofc} \wit_{\noofc + i} \lag_i(X) \\
	\p{c}(X) &= (b_5 X + b_6)\p{Z_H}(X) + \textstyle \sum_{i = 1}^{\noofc} \wit_{2 \cdot \noofc + i} \lag_i(X)
	\end{align*}
	Output polynomial commitments $\gone{\p{a}(\chi), \p{b}(\chi), \p{c}(\chi)}$.

	\item[Message 2] Compute challenges $\beta, \gamma \in \FF_p$ by querying random oracle
	on partial proof, that is,
	\(
	\beta = \ro(\tzkproof[0..1], 0)\,, \qquad \gamma = \ro(\tzkproof[0..1], 1)\,.
	\)

	Compute permutation polynomial $\p{z}(X)$
	\begin{multline*}
	\p{z}(X) = (b_7 X^2 + b_8 X + b_9)\p{Z_H}(X) + \lag_1(X) + \\
	+ \sum_{i = 1}^{\noofc - 1} \left(\lag_{i + 1} (X) \prod_{j = 1}^{i} \frac{
		(\wit_j +\beta \omega^{j - 1} + \gamma)(\wit_{\noofc + j} + \beta k_1
		\omega^{j - 1} + \gamma)(\wit_{2 \noofc + j} +\beta k_2 \omega^{j- 1} +
		\gamma)} {(\wit_j+\sigma(j) \beta + \gamma)(\wit_{\noofc + j} + \sigma(\noofc
		+ j)\beta + \gamma)(\wit_{2 \noofc + j} + \sigma(2 \noofc + j)\beta +
		\gamma)}\right)
	\end{multline*}
	Output polynomial commitment $\gone{\p{z}(\chi)}$

	\item[Message 3] Compute the challenge $\alpha = \ro(\tzkproof[0..2])$, compute the quotient
	polynomial
	\begin{align*}
	& \p{t}(X)  = \\
	& (\p{a}(X) \p{b}(X) \selmulti(X) + \p{a}(X) \selleft(X) +
	\p{b}(X)\selright(X) + \p{c}(X)\seloutput(X) + \pubinppoly(X) + \selconst(X)) /
	\p{Z_H}(X) +\\
	& + ((\p{a}(X) + \beta X + \gamma) (\p{b}(X) + \beta k_1 X + \gamma)(\p{c}(X)
	+ \beta k_2 X + \gamma)\p{z}(X)) \infrac{\alpha}{\p{Z_H}(X)} \\
	& - (\p{a}(X) + \beta \p{S_{\sigma 1}}(X) + \gamma)(\p{b}(X) + \beta
	\p{S_{\sigma 2}}(X) + \gamma)(\p{c}(X) + \beta \p{S_{\sigma 3}}(X) +
	\gamma)\p{z}(X \omega))  \infrac{\alpha}{\p{Z_H}(X)} \\
	& + (\p{z}(X) - 1) \lag_1(X) \infrac{\alpha^2}{\p{Z_H}(X)}
	\end{align*}
	Split $\p{t}(X)$ into degree less then $\noofc$ polynomials
	$\p{t_{lo}}(X), \p{t_{mid}}(X), \p{t_{hi}}(X)$, such that
	\(
	\p{t}(X) = \p{t_{lo}}(X) + X^{\noofc} \p{t_{mid}}(X) + X^{2 \noofc}
	\p{t_{hi}}(X)\,.
	\)
	Output $\gone{\p{t_{lo}}(\chi), \p{t_{mid}}(\chi), \p{t_{hi}}(\chi)}$.

	\item[Message 4] Get the challenge $\chz \in \FF_p$, $\chz = \ro(\tzkproof[0..3])$.
	Compute opening evaluations
	\(
	\p{a}(\chz), \p{b}(\chz), \p{c}(\chz), \p{S_{\sigma 1}}(\chz), \p{S_{\sigma 2}}(\chz), \p{t}(\chz), \p{z}(\chz \omega),
	\)
	Compute the linearization polynomial
	\[
	\p{r}(X) =
	\begin{aligned}
	& \p{a}(\chz) \p{b}(\chz) \selmulti(X) + \p{a}(\chz) \selleft(X) + \p{b}(\chz) \selright(X) + \p{c}(\chz) \seloutput(X) + \selconst(X) \\
	& + \alpha \cdot \left( (\p{a}(\chz) + \beta \chz + \gamma) (\p{b}(\chz) + \beta k_1 \chz + \gamma)(\p{c}(\chz) + \beta k_2 \chz + \gamma) \cdot \p{z}(X)\right) \\
	& - \alpha \cdot \left( (\p{a}(\chz) + \beta \p{S_{\sigma 1}}(\chz) + \gamma) (\p{b}(\chz) + \beta \p{S_{\sigma 2}}(\chz) + \gamma)\beta \p{z}(\chz\omega) \cdot \p{S_{\sigma 3}}(X)\right) \\
	& + \alpha^2 \cdot \lag_1(\chz) \cdot \p{z}(X)
	\end{aligned}
	\]
	Output
	$\p{a}(\chz), \p{b}(\chz), \p{c}(\chz), \p{S_{\sigma 1}}(\chz), \p{S_{\sigma
			2}}(\chz), \p{t}(\chz), \p{z}(\chz \omega), \p{r}(\chz).$

	\item[Message 5] Compute the opening challenge $v \in \FF_p$,
	$v = \ro(\tzkproof[0..4])$.  Compute the openings for the polynomial commitment
	scheme
	\hspace*{-2cm}\begin{align*}
	& \p{W_\chz}(X) = \frac{1}{X - \chz} \left(
	\begin{aligned}
	& \p{t_{lo}}(X) + \chz^\noofc \p{t_{mid}}(X) + \chz^{2 \noofc} \p{t_{hi}}(X) - \p{t}(\chz)
	+ v(\p{r}(X) - \p{r}(\chz))
	+ v^2 (\p{a}(X) - \p{a}(\chz))\\
	& + v^3 (\p{b}(X) - \p{b}(\chz))
	+ v^4 (\p{c}(X) - \p{c}(\chz))
	+ v^5 (\p{S_{\sigma 1}}(X) - \p{S_{\sigma 1}}(\chz)) \\
	& + v^6 (\p{S_{\sigma 2}}(X) - \p{S_{\sigma 2}}(\chz))
	\end{aligned}
	\right)\\
	& \p{W_{\chz \omega}}(X) = \infrac{(\p{z}(X) - \p{z}(\chz \omega))}{(X - \chz \omega)}
	\end{align*}
	Output $\gone{\p{W_{\chz}}(\chi), \p{W_{\chz \omega}}(\chi)}$.
\end{description}

\ncase{Plonk verifier $\verifier(\srs, \inp, \zkproof)$}\ \newline
The \plonk{} verifier works as follows
\begin{enumerate}
	\item Validate all obtained group elements.
	\item Validate all obtained field elements.
	\item Parse the instance as
	$\smallset{\wit_i}_{i = 1}^\instsize \gets \inp$.
	\item Compute challenges $\beta, \gamma, \alpha, \chz, v, u$ from the transcript.
	\item Compute zero polynomial evaluation
	$\p{Z_H} (\chz) =\chz^\noofc - 1$.
	\item Compute Lagrange polynomial evaluation
	$\lag_1 (\chz) = \frac{\chz^\noofc -1}{\noofc (\chz - 1)}$.
	\item Compute public input polynomial evaluation
	$\pubinppoly (\chz) = \sum_{i \in \range{1}{\instsize}} \wit_i
	\lag_i(\chz)$.
	\item Compute quotient polynomials evaluations
	\begin{multline*}
	\p{t} (\chz) =  \Big(
	\p{r} (\chz) + \pubinppoly(\chz) - (\p{a}(\chz) + \beta \p{S_{\sigma 1}}(\chz) + \gamma) (\p{b}(\chz) + \beta \p{S_{\sigma 2}}(\chz) + \gamma)
	(\p{c}(\chz) + \gamma)\p{z}(\chz \omega) \alpha - \lag_1 (\chz) \alpha^2
	\Big) / {\p{Z_H}(\chz)} \,.
	\end{multline*}
	\item Compute batched polynomial commitment
	$\gone{D} = v \gone{r} + u \gone {z}$ that is
	\begin{align*}
	\gone{D} & = v
	\left(
	\begin{aligned}
	& \p{a}(\chz)\p{b}(\chz) \cdot \gone{\selmulti} + \p{a}(\chz)  \gone{\selleft} + \p{b}  \gone{\selright} + \p{c}  \gone{\seloutput} + \\
	& + (	(\p{a}(\chz) + \beta \chz + \gamma) (\p{b}(\chz) + \beta k_1 \chz + \gamma) (\p{c} + \beta k_2 \chz + \gamma) \alpha  + \lag_1(\chz) \alpha^2)  + \\
	% &   \\
	& - (\p{a}(\chz) + \beta \p{S_{\sigma 1}}(\chz) + \gamma) (\p{b}(\chz)
	+ \beta \p{S_{\sigma 2}}(\chz) + \gamma) \alpha \beta \p{z}(\chz
	\omega) \gone{\p{S_{\sigma 3}}(\chi)})
	\end{aligned}
	\right) + \\
	& + u \gone{\p{z}(\chi)}\,.
	\end{align*}
	\item Computes full batched polynomial commitment $\gone{F}$:
	\begin{align*}
	\gone{F} & = \left(\gone{\p{t_{lo}}(\chi)} + \chz^\noofc \gone{\p{t_{mid}}(\chi)} + \chz^{2 \noofc} \gone{\p{t_{hi}}(\chi)}\right) + u \gone{\p{z}(\chi)} + \\
	& + v
	\left(
	\begin{aligned}
	& \p{a}(\chz)\p{b}(\chz) \cdot \gone{\selmulti} + \p{a}(\chz)  \gone{\selleft} + \p{b}(\chz)   \gone{\selright} + \p{c}(\chz)  \gone{\seloutput} + \\
	& + (	(\p{a}(\chz) + \beta \chz + \gamma) (\p{b}(\chz) + \beta k_1 \chz + \gamma) (\p{c}(\chz)  + \beta k_2 \chz + \gamma) \alpha  + \lag_1(\chz) \alpha^2)  + \\
	% &   \\
	& - (\p{a}(\chz) + \beta \p{S_{\sigma 1}}(\chz) + \gamma) (\p{b}(\chz) + \beta \p{S_{\sigma 2}}(\chz) + \gamma) \alpha  \beta \p{z}(\chz \omega) \gone{\p{S_{\sigma 3}}(\chi)})
	\end{aligned}
	\right) \\
	& + v^2 \gone{\p{a}(\chi)} + v^3 \gone{\p{b}(\chi)} + v^4 \gone{\p{c}(\chi)} + v^5 \gone{\p{S_{\sigma 1}(\chi)}} + v^6 \gone{\p{S_{\sigma 2}}(\chi)}\,.
	\end{align*}
	\item Compute group-encoded batch evaluation $\gone{E}$
	\begin{align*}
	\gone{E}  = \frac{1}{\p{Z_H}(\chz)} & \gone{
		\begin{aligned}
		& \p{r}(\chz) + \pubinppoly(\chz) +  \alpha^2  \lag_1 (\chz) + \\
		& - \alpha \left( (\p{a}(\chz) + \beta \p{S_{\sigma 1}} (\chz) + \gamma) (\p{b}(\chz) + \beta \p{S_{\sigma 2}} (\chz) + \gamma) (\p{c}(\chz) + \gamma) \p{z}(\chz \omega) \right)
		\end{aligned}
	}\\
	+ & \gone{v \p{r}(\chz) + v^2 \p{a}(\chz) + v^3 \p{b}(\chz) + v^4 \p{c}(\chz) + v^5 \p{S_{\sigma 1}}(\chz) + v^6 \p{S_{\sigma 2}}(\chz) + u \p{z}(\chz \omega) }\,.
	\end{align*}
	\item Check whether the verification
	% $\vereq_\zkproof(\chi)$
	equation holds
	\begin{multline}
	\label{eq:ver_eq}
	\left( \gone{\p{W_{\chz}}(\chi)} + u \cdot \gone{\p{W_{\chz
				\omega}}(\chi)} \right) \bullet
	\gtwo{\chi} - \\
	\left( \chz \cdot \gone{\p{W_{\chz}}(\chi)} + u \chz \omega \cdot
	\gone{\p{W_{\chz \omega}}(\chi)} + \gone{F} - \gone{E} \right) \bullet
	\gtwo{1} = 0\,.
	\end{multline}
	The verification equation is a batched version of the verification equation
	from \cite{AC:KatZavGol10} which allows the verifier to check openings of
	multiple polynomials in two points (instead of checking an opening of a single
	polynomial at one point).
\end{enumerate}

\ncase{Plonk simulator $\simulator_\chi(\srs, \td= \chi, \inp)$}\
We describe the simulator in \cref{lem:plonk_tlzk}.

\subsection{Simulation extractability of $\plonk${}}
Due to lack of space, we provide here only theorem statements and intuition for why they hold. Full proofs are given in \cref{sec:plonkse_proofs}.

\oursubsub{Unique Response Property}
\begin{lemma}
	\label{lem:plonkprot_ur}
	Let $\PCOMp$ be a polynomial commitment that is $\epsbind(\secpar)$-binding and has unique opening property with loss $\epsop (\secpar)$. Then $\plonkprotfs$ is $\ur{3}$ against algebraic adversaries, who makes up to $q$ random oracle queries, with security loss $\epsbinding (\secpar) + \epsop ( \secpar )$.
\end{lemma}
\paragraph{Intuition.} We show that an adversary who can break the $3$-unique response property of $\plonkprotfs$ can be either used to break the commitment scheme's evaluation binding or unique opening property. The former happens with the probability upper-bounded by $\epsbinding (\secpar)$, the latter with the probability upper bounded by $\epsop (\secpar)$.

\begin{proof}
	Let $\adv$ be an algebraic adversary tasked to break the $\ur{3}$-ness of
	$\plonkprotfs$. We show that the first three prover's messages determine, along with 	the verifiers challenges, the rest of it. We denote by $\zkproof^0$ and $\zkproof^1$ the two proofs that the adversary outputs. To distinguish polynomials and commitments which an honest prover would send in the proof from the polynomials and commitments computed by the adversary we write the latter using indices $0$ and $1$ (two indices as we have two transcripts), e.g.~to describe the quotient polynomial provided by the adversary we write $\p{t}^0$ and $\p{t}^1$ instead of $\p{t}$ as in the description of the protocol.

	We note that since the unique response property requires from $\zkproof^{0}$ and $\zkproof^{1}$ that the first place they possibly differ is the $4$-th prover's message, then the challenge $\chz$, that is picked by the adversary after the $3$-rd message is the same in both transcripts. This challenge determines the evaluation point of polynomials $\p{a}(X), \p{b}(X), \p{c}(X), \p{t}(X), \p{z}(X)$ which commitments are already sent.

	In its fourth message, the prover provides evaluations of the aforementioned polynomials, along with evaluations of publicly known polynomials $
	\p{S_{\sigma 1}} (\chz), \p{S_{\sigma 2}} (\chz)$, and evaluation of a linearization polynomial $\p{r}(\chz)$.

	Note that the adversary can output two accepting proofs that differ on their fourth message only if it either manages to break evaluation binding of one of the opening, or provides an incorrect opening which is accepted due to a batching error. Since the commitment scheme is evaluation binding with security loss $\epsbinding (\secpar)$, %and the batched verification equation accepts an incorrect opening with probability at most $\infrac{q}{p}$, cf.~\cite{EPRINT:GabWilCio19},
	the adversary can make $\zkproof^{0}$ and $\zkproof^{1}$ differ on the fourth message with the same probability. % at most $8  \cdot \epsbinding (\secpar) + \infrac{q}{p}$.

	Next, assume that the transcripts are the same up to the fourth message, but differ at the fifth. In that message, the adversary provides openings of the evaluations. Since the unique opening property, the adversary can open the valid evaluation of a polynomial to two different values with probability at most $\epsop (\secpar)$. (We note that for the KZG polynomial commitment scheme, as used in \cite{EPRINT:GabWilCio19}, $\epsop (\secpar) \leq \epsudlog (\secpar) + \infrac{q}{p}$, cf.~\cref{lem:pcomp_op}.)
	% , which is upper-bounded by $\epsudlog (\secpar) + \infrac{q}{p}$, cf.~\cref{lem:pcomp_op}.

	By the union bound, the adversary is able to break the unique response property with probability upper bounded by $\epsbinding (\secpar) + \epsop (\secpar)$.
	\qed
\end{proof}

\oursubsub{Rewinding-Based Knowledge Soundness}
\begin{lemma}
	\label{lem:plonkprot_ss}
	$\plonkprotfs$ is $(3, 3 \noofc + 6)$-rewinding-based knowledge sound against algebraic adversaries who make up to $q$ random oracle queries with security loss
	\[
	\epscss(\secpar,\accProb, q) \leq \left(1 - \frac{\accProb - (q + 1) \left(\frac{3 \noofc + 5}{p} \right)}{1 - \frac{3 \noofc + 5}{p}}\right) + (3 \noofc + 6) \cdot \epsudlog (\secpar) %+ (3 \noofc + 6) \cdot \epsid (\secpar)
	\,,
	\]
	Here $\accProb$ is a probability that the adversary outputs an accepting proof,
	%$\epsid(\secpar)$ is a soundness error of the ideal verifier for $\plonkprot$,
	and $\epsudlog(\secpar)$ is security of $(\numberofconstrains + 5, 1)$-$\udlog$
	assumption.
\end{lemma}

\paragraph{Intuition.} We use Attema et al.~\cite[Proposition 2]{EPRINT:AttFehKlo21} to bound the probability that an algorithm $\tdv$ does not obtain a tree of accepting transcripts in an expected number of runs. This happens with probability at most
	\[
	1 - \frac{\accProb - (q + 1) \left(\frac{3 \noofc + 5}{p} \right)}{1 - \frac{3 \noofc + 5}{p}}
	\]
Then we analyze the case that one of the proofs in the tree $\tree$ outputted by $\tdv$ is not accepting by the ideal verifier. This discrepancy can be used to break an instance of an updatable dlog assumption which happens with probability at most $(3 \noofc + 6)  \cdot \epsudlog (\secpar)$. %Additionally, it may be impossible to extract the witness from a tree where each of the transcripts is accepting by the ideal verifier if the adversary broke soundness of the ideal verifier in one of the transcripts. That happens with probability at most $(3 \noofc + 6) \cdot \epsid (\secpar)$.

\begin{proof}
	Let $\adv^{\ro, \initU}(\secparam; r)$ be the adversary who outputs $(\inp, \zkproof)$ such that $\plonkprotfs.\verifier$ accepts the proof. Let $\tdv$ be a tree-building algorithm of \cref{lem:attema} that outputs a tree $\tree$, and let $\extcss$ be an extractor that given the tree output by $\tdv$ reveals the witness for $\inp$. The main idea of the proof is to show that an adversary who breaks rewinding-based knowledge soundness can be used to break a $\udlog$-problem instance. The proof goes by game hops. Note that since the tree branches after $\adv$'s $3$-rd message, the instance $\inp$, commitments $\gone{\p{a} (\chi), \p{b} (\chi), \p{c} (\chi), \p{z} (\chi), \p{t_{lo}} (\chi), \p{t_{mid}} (\chi), \p{t_{hi}} (\chi)}$, and challenges $\alpha, \beta, \gamma$ are the same in all the transcripts. Also, the tree branches after the third adversary's message where the challenge $\chz$ is presented, thus tree $\tree$ is built using different values of $\chz$.	We consider the following games.

	\ncase{Game 0} %
	In this game the adversary wins if it outputs a valid instance--proof pair $(\inp, \zkproof)$, and the extractor $\extcss$ does not manage to output a witness $\wit$ such that $\REL (\inp, \wit)$ holds.

	\ncase{Game 1} %
	In this game the environment aborts the game if the tree building algorithm $\tdv$ fails in building a tree of accepting transcripts $\tree$.

	\ncase{Game 0 to Game 1} %
	By \cref{lem:attema} probability that Game 1 is aborted, while Game 0 is not, is at most
	%\hamid{2.5}{Should this not be "1 minus the following"?}
	\[
	1 - \frac{\accProb - (q + 1) \left(\frac{3 \noofc + 5}{p} \right)} {1 - \frac{3 \noofc + 5}{p}} \,.
	\]

	\ncase{Game 2} %
	In this game the environment additionally aborts if at least one of its proofs in $\tree$ is not accepting by an ideal verifier.

	\ncase{Game 1 to Game 2} %
	As usual, we show a reduction that breaks an instance of a $\udlog$ assumption when Game 2 is aborted, while Game 1 is not.

	Let $\rdvudlog$ be a reduction that gets as input an $(\noofc + 5, 1)$-$\udlog$ instance $\gone{1, \ldots, \chi^{\noofc + 5}}, \gtwo{\chi}$. Then it can update the instance to another one $\gone{1, \ldots, {\chi'}^{\noofc + 5}}, \gtwo{\chi'}$. Eventually, the reduction outputs $\chi'$.
	%
	The reduction $\rdvudlog$ proceeds as follows.
	First, it builds $\adv$'s SRS $\srs$ using the input $\udlog$ instance. Then it processes the adversary's update query by adding it to the list $\Qsrs$ and passing it to its own update oracle getting instance $\gone{1, \ldots, {\chi'}^{\noofc + 5}}, \gtwo{\chi'}$. The updated SRS $\srs'$ is then computed and given to $\adv$. $\rdvdulog$ also takes care of the random oracle queries made by $\adv$. It picks their answers honestly and write them in $\Qro$. The reduction then starts $\tdv(\srs, \adv, r, \Qro, \Qsrs)$.

	Let $(1, \tree)$ be the output returned by $\tdv$. Let $\inp$ be a relation proven in $\tree$.  Consider a transcript $\zkproof \in \tree$ such that $\vereq_{\inp, \zkproof}(X) \neq 0$, but $\vereq_{\inp, \zkproof}(\chi') = 0$. Since $\adv$ is algebraic, all group elements included in $\tree$ are extended by their representation as a combination of the input $\GRP_1$-elements. Hence, all coefficients of the verification equation polynomial $\vereq_{\inp, \zkproof}(X)$ are known.
	Eventually, the reduction finds $\vereq_{\inp, \zkproof}(X)$ zero points and returns $\chi'$ which is one of them.

	Hence, the probability that the adversary wins in Game 2 but does not win in Game 1 is upper-bounded by $(3 \noofc + 6) \cdot \epsudlog (\secpar)$.

	\ncase{Conclusion}

	Note that the adversary can win in Game 2 only if $\tdv$ manages to produce a tree of accepting transcripts $\tree$, such that each of the transcripts in $\tree$ is accepting by an ideal verifier. Note that since $\tdv$ produces $(3 \noofc + 6)$ accepting transcripts for different challenges $\chz$, it obtains the same number of different evaluations of polynomials $\p{a} (X), \p{b} (X), \p{c} (X), \p{z} (X), \p{t} (X)$. Since all the transcripts are accepting by an idealised verifier, the equality between polynomial $\p{t} (X)$ and combination of polynomials $\p{a} (X), \p{b} (X), \p{c} (X), \p{z} (X)$ defined in prover's $3$-rd message description holds. Hence, $\p{a} (X), \p{b} (X), \p{c} (X)$ encodes the valid witness for the proven statement. $\extcss$ can recreate polynomials' coefficients by interpolation and reveal the witness given $(3 \noofc + 6)$ evaluations.
	% Thus, the probability that extraction fails in that case is upper-bounded by $(3 \noofc + 6) \cdot \epsid(\secpar)$.

	Hence, the probability that the adversary wins in Game 0 is upper-bounded by
	\[
	\epscss(\secpar,\accProb, q) \leq \left(1 - \frac{\accProb - (q + 1) \left(\frac{3 \noofc + 5}{p} \right)}{1 - \frac{3 \noofc + 5}{p}}\right) + (3 \noofc + 6) \cdot \epsudlog (\secpar)\,.
	\]
	\qed
\end{proof}

\oursubsub{Trapdoor-Less Zero-Knowledge of Plonk}
\begin{lemma}
	\label{lem:plonk_tlzk}
	$\plonkprotfs$ is 3-programmable trapdoor-less zero-knowledge.
\end{lemma}

\paragraph{Intuition.} The simulator, that does not know the SRS trapdoor can make a simulated proof by programming the random oracle. It proceeds as follows. It picks a random witness and behaves as an honest prover up to the point when a commitment to the polynomial $\p{t}(X)$ is sent. Since the simulator picked a random witness and $\p{t}(X)$ is a polynomial only (modulo some negligible function) when the witness is correct, it cannot compute commitment to $\p{t}(X)$ as it is a rational function. However, the simulator can pick a random challenge $\chz$ and a polynomial $\p{\tilde{t}}(X)$ such that $\p{t} (\chz)  = \p{\tilde{t}} (\chz)$. Then the simulator continues behaving as an honest prover. We argue that such a simulated proof is indistinguishable from a real one.

\begin{proof}
	As noted in \cref{def:upd-scheme}, subvertible zero-knowledge implies updatable zero-knowledge. Hence, here we show that Plonk is TLZK even against adversaries who picks
	the SRS on its own.

	The adversary $\adv(\secparam)$ picks an SRS $\srs$ and instance--witness pair
	$(\inp, \wit)$ and gets a proof $\zkproof$ simulated by the simulator
	$\simulator$ which proceeds as follows.

	For its $1$-st message the simulator  picks randomly both the randomizers $b_1, \ldots, b_6$ and
	sets $\wit_i = 0$ for $i \in \range{1}{3\noofc}$. Then $\simulator$
	outputs $\gone{\p{a}(\chi), \p{b}(\chi), \p{c}(\chi)}$. For the first
	challenge, the simulator picks permutation argument challenges $\beta, \gamma$
	randomly.

	For its $2$-nd message, the simulator computes $\p{z}(X)$ from
	the newly picked randomizers $b_7, b_8, b_9$ and coefficients of polynomials
	$\p{a}(X), \p{b}(X), \p{c}(X)$. Then it evaluates $\p{z}(X)$ honestly and outputs
	$\gone{\p{z}(\chi)}$. Challenge $\alpha$ that should be sent by the verifier
	after the simulator's $2$ message is picked by the simulator at random.

	In its $3$-rd message the simulator starts by picking at random a challenge $\chz$, which
	in the real proof comes as a challenge from the verifier sent \emph{after} the prover
	sends its $3$-rd message. Then $\simulator$ computes evaluations
	\(\p{a}(\chz), \p{b}(\chz), \p{c}(\chz), \p{S_{\sigma 1}}(\chz), \p{S_{\sigma
			2}}(\chz), \pubinppoly(\chz), \lag_1(\chz), \p{Z_H}(\chz),\allowbreak
	\p{z}(\chz\omega)\) and computes $\p{t}(X)$ honestly. Since for a random
	$\p{a}(X), \p{b}(X), \p{c}(X), \p{z}(X)$ the constraint system is (with
	overwhelming probability) not satisfied and the constraints-related polynomials
	are not divisible by $\p{Z_H}(X)$, hence $\p{t}(X)$ is a rational function
	rather than a polynomial. Then, the simulator evaluates $\p{t}(X)$ at $\chz$ and
	picks randomly a degree-$(3 \noofc + 15)$ polynomial $\p{\tilde{t}}(X)$ such that
	$\p{t}(\chz) = \p{\tilde{t}}(\chz)$ and publishes a commitment
	$\gone{\p{\tilde{t}_{lo}}(\chi), \p{\tilde{t}_{mid}}(\chi),
		\p{\tilde{t}_{hi}}(\chi)}$. After that the simulator outputs $\chz$ as a
	challenge.

	For the next message, the simulator computes polynomial $\p{r}(X)$ as an honest
	prover would, cf.~\cref{sec:plonk_explained} and evaluates $\p{r}(X)$ at $\chz$.

	The rest of the evaluations are already computed, thus $\simulator$ simply outputs
	\( \p{a}(\chz), \p{b}(\chz), \p{c}(\chz), \p{S_{\sigma 1}}(\chz), \p{S_{\sigma
			2}}(\chz), \p{t}(\chz), \p{z}(\chz \omega)\,.  \) After that it picks randomly
	the challenge $v$, and prepares the the last message as an honest prover
	would. Eventually, $\simulator$ and outputs the final challenge, $u$, by picking it
	at random as well.

	We argue about zero-knowledge as usual. The property holds since the polynomials that has witness elements at their coefficients are randomized by at least two randomizers and are evaluated at at most two points; and the simulator computes all polynomials as an honest prover would.
	\qed
\end{proof}

\subsection*{Simulation Extractability of~$\plonkprotfs$}
Since \cref{lem:plonkprot_ur,lem:plonkprot_ss,lem:plonk_tlzk} hold, $\plonkprot$ is $\ur{3}$,
rewinding-based knowledge sound and trapdoor-less zero-knowledge. We now make use of \cref{thm:se} and show that
$\plonkprot_\fs$ is simulation-extractable as defined in \cref{def:updsimext}.

\begin{corollary}[Simulation extractability of $\plonkprot_\fs$]
	\label{thm:plonkprotfs_se}
	$\plonkprotfs$ is \emph{updatable simulation-extractable} against any $\ppt$ adversary $\advse$ who makes up to $q$ random oracle queries and returns an accepting proof with probability at least $\accProb$ with extraction failure probability
	\[
	\epsse(\secpar, \accProb, q) \leq \left(1 - \frac{\accProb - \epsur (\secpar) - (q + 1) \epserr (\secpar)} {1 - \epserr (\secpar)}\right) + (3 \noofc + 6) \cdot \epsudlog (\secpar)\COMMENT{ + (3 \noofc + 6) \cdot \epsid (\secpar)},
	\]
	where $\epserr (\secpar) = \frac{3 \noofc + 5}{p}$, $\epsur (\secpar) \leq \epsbind (\secpar) + \epsop (\secpar)$, $p$ is the size of the field, and $\noofc$ is the number of constrains in the circuit.
\end{corollary}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "main"
%%% End:


\hypertarget{haskell}{%
\section{Haskell - Defining Functions}\label{haskell}}

\hypertarget{conditional-expressions}{%
\subsection{Conditional Expressions}\label{conditional-expressions}}

As in most programming languages, functions can be defined using
conditional expressions.

\begin{lstlisting}[language=Haskell]
abs :: Int -> Int
abs n = if n >= 0 then n else -n
\end{lstlisting}

abs takes an integer n and returns n if it is non-negative and -n
otherwise.

Conditional expressions can also be nested:

\begin{lstlisting}[language=Haskell]
signum :: Int -> Int
signum n = if n < 0 then -1 else
if n == 0 then 0 else 1
\end{lstlisting}

\begin{tcolorbox}[colback=red!5!white,colframe=red!75!black]
In Haskell, conditional expressions must always have an else branch.
\end{tcolorbox}

\hypertarget{guarded-equations}{%
\subsubsection{Guarded Equations}\label{guarded-equations}}

As an alternative to conditionals, functions can also be defined using
guarded equations

\begin{lstlisting}[language=Haskell]
abs n | n >= 0 = n
      | otherwise = -n
\end{lstlisting}

Guarded equations can be used to make definitions involving multiple
conditions easier to read.

\hypertarget{pattern-matching}{%
\subsection{Pattern Matching}\label{pattern-matching}}

Many functions have a particularly clear definition using pattern matching on their arguments.

\begin{lstlisting}[language=Haskell]
lucky :: (Integral a) => a -> String
lucky 7 = "LUCKY NUMBER SEVEN!"
lucky x = "Sorry, you're out of luck, pal!"
\end{lstlisting}

When you call lucky, the patterns will be checked from top to bottom and when it conforms to a pattern, the corresponding function body will be used. The only way a number can conform to the first pattern here is if it is 7. If it's not, it falls through to the second pattern, which matches anything and binds it to x.

\begin{lstlisting}[language=Haskell]
factorial :: (Integral a) => a -> a
factorial 0 = 1
factorial n = n * factorial (n - 1)

addVectors :: (Num a) => (a, a) -> (a, a) -> (a, a)
addVectors a b = (fst a + fst b, snd a + snd b)
--addVectors (x1, y1) (x2, y2) = (x1 + x2, y1 + y2)
\end{lstlisting}

\begin{tcolorbox}[colback=red!5!white,colframe=red!75!black]
The underscore symbol \_ is a wildcard pattern that matches any argument value.
Patterns are matched in order.
Patterns may not repeat variables
\end{tcolorbox}

\hypertarget{list-patterns}{%
\subsubsection{List Patterns}\label{list-patterns}}

Since [1,2,3] is just syntactic sugar for 1:2:3:[], you can also use the former pattern. A pattern like x:xs will bind the head of the list to x and the rest of it to xs, even if there's only one element so xs ends up being an empty list.

\begin{tcolorbox}[colback=red!5!white,colframe=red!75!black]
[1,2,3,4] means internal actually 1:(2:(3:(4:[]))). <- Syntactic Sugar
\end{tcolorbox}

Functions on lists can be defined using x:xs patterns. For more operations on lists refer to chapter \ref{sec:Operationonlists}.

\begin{lstlisting}[language=Haskell]
head :: [a] -> a
head (x:_) = x
tail :: [a] -> [a]
tail (_:xs) = xs

head' :: [a] -> a
head' [] = error "Can't call head on an empty list, dummy!"
head' (x:_) = x

ghci> let xs = [(1,3), (4,3), (2,4), (5,3), (5,6), (3,1)]
ghci> [a+b | (a,b) <- xs]
[4,7,6,8,11,4]
\end{lstlisting}

\subsection{Guard}

Whereas patterns are a way of making sure a value conforms to some form and deconstructing it, guards are a way of testing whether some property of a value (or several of them) are true or false. Guards are indicated by pipes that follow a function's name and its parameters. A guard is basically a boolean expression. If it evaluates to True, then the corresponding function body is used. If it evaluates to False, checking drops through to the next guard and so on. Many times, the last guard is otherwise. otherwise is defined simply as otherwise = True and catches everything.

Guards can also be written inline, although I'd advise against that because it's less readable, even for very short functions.

\begin{lstlisting}[language=Haskell]
bmiTell :: (RealFloat a) => a -> a -> String
bmiTell weight height
    | bmi <= skinny = "You're underweight, you emo, you!"
    | bmi <= 25.0 = "You're supposedly normal. Pffft, I bet you're ugly!"
    | bmi <= 30.0 = "You're fat! Lose some weight, fatty!"
    | otherwise   = "You're a whale, congratulations!"
    where bmi = weight / height ^ 2
          skinny = 18.5

max' :: (Ord a) => a -> a -> a
max' a b | a > b = a | otherwise = b
\end{lstlisting}

We put the keyword \textbf{where} after the guards (usually it's best to indent it as much as the pipes are indented) and then we define several names or functions. These names are visible across the guards and give us the advantage of not having to repeat ourselves.

\clearpage
\subsection{Let Statement}

The form is let <bindings> in <expression>. The names that you define in the let part are accessible to the expression after the \textbf{in} part. Let bindings are expressions and are fairly local in their scope, they can't be used across guards (which \textit{where} can.

\begin{lstlisting}[language=Haskell]
cylinder :: (RealFloat a) => a -> a -> a
cylinder r h =
    let sideArea = 2 * pi * r * h
        topArea = pi * r ^2
    in  sideArea + 2 * topArea

--Different variables are seperated with semikolon.
ghci> (let a = 100; b = 200; c = 300 in a*b*c, let foo="Hey "; bar = "there!" in foo ++ bar)
(6000000,"Hey there!")
\end{lstlisting}

\hypertarget{lambda-expressions}{%
\subsection{Lambda Expressions}\label{lambda-expressions}}

Functions can be constructed without naming the functions by using
lambda expressions.

\begin{lstlisting}[language=Haskell]
$\lambda$x -> x + x
\end{lstlisting}

The symbol $\lambda$ is the Greek letter lambda, and is typed at the keyboard as a backslash \textbackslash{}.

\begin{itemize}
\tightlist
\item
  Lambda expressions can be used to give a formal meaning to functions
  defined using currying
\item
  Lambda expressions are also useful when defining functions that return
  functions as results
\item
  Lambda expressions can be used to avoid naming functions that are only
  referenced once
\end{itemize}

\hypertarget{operator-sections}{%
\subsection{Operator Sections}\label{operator-sections}}

An operator written between its two arguments can be converted into a
curried function written before its two arguments by using parentheses.

\begin{lstlisting}[language=Haskell]
> 1+2
3
> (+) 1 2
3

or
> (1+) 2
3
\end{lstlisting}

In general, if $\oplus$ is an operator then functions of the form ($\oplus$), (x$\oplus$) and (y$\oplus$) are called sections.

\clearpage


\chapter{Model-based EMG-driven Control}
\label{ch:ModelControl}

% Atualizar para novo texto presente nos artigos.

\section{Control Description}

The Model-Based control method utilizes a dynamic model of the body to predict the dynamic response according to the input given to the model.

There are basically three ways the dynamic model can be obtained: through mathematical model, system identification model and artificial intelligence model \cite{Anam2012988}.

For this work the chosen model is the system identification model.  The system identification method is often used because of the difficulty in precisely describe the dynamic model through mathematical equations. To do so, a set of inputs and outputs are measured through experiments and then an identification algorithm develops the relationship between the inputs and outputs of the system.

%Four modeling techniques were applied to determine which one better estimates the model which determines the elbow angle with EMG signals as input: ARX, ARMAX, ARIMAX and SS. The model that had the highest fitness value was the ARMAX model.

By using a dynamic model that mimics the user's limb dynamics, the exoskeleton will be capable of performing limb-like movements using the sEMG signals as input.

The main disadvantage of this control method is that, in order to develop the dynamic model, extensive experiments must be conducted on each subject to calculate his/her specific model parameters. Even a slight change in the electrode positioning on the subjects' skin can alter the results from the controller. This would require a calibration procedure every time the user wears the exoskeleton.

\section{Conducted experiment}

This section presents a method to estimate the elbow joint angle from surface electromyography (sEMG) measurements of biceps, triceps and brachioradialis. This estimation is of major importance for the design of human robot interfaces based on sEMG, for the modeling of the muscular system and for the design of bio-inspired mechanisms. However, the interpretation and processing of electromyography signals is challenging due to nonlinearities, unmodeled muscle dynamics noise and interferences. In order to determine an estimation model and a calibration procedure for the model parameters, a set of experiments were carried out with seven subjects. The experiments consisted of series of continuous (cyclical) and discrete elbow flexo-extensions. The sEMG data from the biceps brachii, triceps brachii and brachioradialis and the joint angle were recorded. After the model was selected, a second experiment was performed in order to validate the estimation procedure. The results show an effective model for the EMG-to-angle relation with great values for both correlation and mean-square-root error when compared to the measured angle data.

\subsection{Methods}
\subsubsection{Subjects and experimental setup}
Seven volunteers (age: 34.3 $\pm$ 14.7 years, height: 1.74 $\pm$ 0.1 m, weight: 67.9 $\pm$ 15.7 kg, 4 male, 3 female, all right-handed) with no known neuromuscular deficit participated in the experiments. Elbow joint angle along with the surface Electromyography (sEMG) of three right arm muscles, biceps brachii, triceps brachii and brachioradialis were recorded.
sEMG was measured with 3 pairs of BTS FREEEMG 1000 \textsuperscript{\textregistered} electrodes with an electrode separation of 20mm with the electrode diameter being 4mm. A pair of electrodes was placed on the biceps and other pair on the triceps following the SENIAM guidelines \cite{SENIAM20170110}. To determine the electrode positions of the brachioradialis muscle, the subject was asked to apply force to flex the forearm while keeping it at \(90^{\circ}\). Then, the electrode was placed on the belly of the muscle and its respective pair placed distally at a 20mm following the muscle fiber direction. The sampling rate was of 1kHz with 16 bit resolution. The user interface was the BTS FREEEMG software.

To measure the joint angle, a six degrees of freedom Inertial Measurement Unit (IMU, VN-100 from VectorNav\textsuperscript{\textregistered}), with \(0.01^{\circ}\) precision, was attached on the internal aspect of the forearm, located at two-thirds distance from the elbow to the wrist. The angle values were acquired with a rate of 100 samples per second. The data were collected with Matlab\textsuperscript{\textregistered}

\subsubsection{Experimental Protocol}

\begin{figure}[thpb]
      \centering
      \includegraphics[scale=0.32]{Images/Experiment_Image.jpg}
      \caption{Experimental setup on a test subject}
      \label{Experimental Setup}
   \end{figure}

The subject sat on a chair, with the knees flexed at \(90^{\circ}\), the back perpendicular to the ground with the scapulas pressed against the wall. The back of the arm was leaning against a rubber support that was attached to the wall. This setup guaranteed that the subject was comfortable enough to perform repeated elbow flexions and extensions while maintaining the upper arm steady.

The test protocol had three parts: The first one consisted of an isometric force test to obtain the Maximum Voluntary Contraction (MVC). The elbow of subject was kept in a fixed position at \(90^{\circ}\) and he/she was asked to apply the maximal possible force to flex the elbow. The subject was given a three minute interval before the next set.

In the second part, the subject was asked to perform five consecutive elbow flexion and extension movements from  \(50^{\circ}\) to \(140^{\circ}\) with a frequency of 0.5Hz. To help the subject reach the correct target angles a template was attached to the wall parallel to the subject, to provide visual guidance. To achieve the desired movement speed a metronome was set at the speed of 60 bpm so that the subject could synchronize the movements with the sound of the metronome.

   The subject was given a minute of rest before the third part of the experiments. In this part the subject was asked to make an elbow flexion for 1s, then hold his forearm at \(140^{\circ}\) for 1 second, then a 1 second extension movement and then hold his forearm at \(50^{\circ}\) for another second. This movement should be repeated five times. Another one minute resting time was given to the subject.
Both of the continuous and interval tests were repeated with 1.5kg and 3kg extra weight placed at the subject's hand.

No subjects reported fatigue during the experiment.

The test was repeated in a different day, on all test subjects to further analyze the repeatability of the model proposed in this work.

All the data from the tests were transferred to Matlab\textsuperscript{\textregistered} for further analysis and processing.

\subsubsection{Experimental Data Processing}

The EMG data were processed as follows. Further explanations can be found on the literature \cite{Rose20161112}\cite{hayashibe:lirmm-00429594}
\begin{enumerate}
\item high-pass filtering of the EMG data, using a 2nd order Butterworth filter, with a cutoff frequency of 30 Hz, thus removing movement artifact.
\item Wave rectification
\item Second Order Butterworth Filter, with 1Hz cutoff frequency.
\item normalization with the peak of Maximum Voluntary Contraction (MVC)
\end{enumerate}

This way, the EMG is smoothed and presented as a percentage of the subject MVC instead of Volts.

A low-pass, 5 Hz cutoff frequency, second-order Butterworth filter is applied to the angular data to remove errors and other undesired signals.

Since the position tracking data was sampled at 100 Hz while the EMG data was sampled at 1KHz, all the position tracking data was resampled to 1000 Hz, an antialiasing finite impulse response (FIR) lowpass filter was applied and the delay introduced by the filter was compensated.

Figure \ref{Angle and EMG} shows an example of the recorded elbow angle and processed sEMG for the continuous movement with no extra weight.

\begin{figure}[thpb]
      \centering
      \includegraphics[height = 0.8\textheight]{Images/Angle_and_EMGs.jpg}
      \caption{a) Joint angle for the continuous movement with no extra weight, recorded with the IMU; sEMG values for the b) biceps brachii, c) triceps brachii and d) brachioradialis for the continuous movement with no extra weight.}
      \label{Angle and EMG}
   \end{figure}

\subsection{Linear System Modeling}

\begin{figure}[thpb]
      \centering
      \includegraphics[scale=0.5]{Images/Models_comparison_5.jpg}
      \caption{Estimated elbow angle using the models responses compared to the elbow angle measured with the IMU. The estimated models were ARX, State Space, ARMAX and ARIMAX.}
      \label{Models Comparison}
   \end{figure}

It was assumed that the arm has the same model with different inertia parameters for the different weights attached to the arm of the subject arm. Considering a simple model of the elbow (arm with only 1 degree of freedom):

\begin{equation}\label{eq:simpleModel}
T = (J + M\cdot L^2)\cdot \ddot{\theta}  + B \cdot \dot{\theta}  + (m\cdot l + M \cdot L) \cdot g \cdot cos(\theta)
\end{equation}

Where T is the elbow joint torque, J is the forearm inertia, B is the damping factor of the joint, m is the forearm mass, M is the dumbbell's mass, g is the gravity force and \(\theta\) is the joint angle. From this simple model it is easy to infer that, by changing the dumbbell's mass, the arm model parameters also change.

Four different modeling techniques were applied to determine which one best estimated the model that provides the elbow angle as an output taking the three EMG signals as inputs. These modeling techniques were: Auto-Regressive with Exogenous Input (ARX), Auto-Regressive Moving-Average with Exogenous Input (ARMAX), Auto-Regressive Integrated Moving-Average with Exogenous Input (ARIMAX) and State Space (SS).

To determine the best modeling technique, 10 models of each type were created with random parameter orders. Their estimation of the elbow joint angle was compared with each other. The model with the best fit value was chosen.

With the model estimation technique chosen, it is necessary to determine the order of its parameters. To determine the chosen model order, 400 random combinations were tested for each data set with order values going from 0 to 10. The order chosen was the one that gave the best fit (see eq. \ref{eq:NRMSE}) between the estimated value and the one measured by the experiment. The coefficients of the models were estimated using time-domain data in Matlab\textsuperscript{\textregistered} (The Mathworks Inc, USA), minimizing a quadratic prediction error criterion.

To determine the best fit the normalized Root-mean-square error (NRMSE)  was used:
\begin{equation}
\label{eq:NRMSE}
NRMSE = 100*\left(1- \frac{||y-\hat{y}||}{||y-mean(y)||}\right)
\end{equation}

Where $y$ is the reference signal and $\hat{y}$ is the signal being evaluated.

\subsection{Results}

Figure \ref{Models Comparison} compares the different models with measured elbow angle and shows the fitness value for each data set.
The ARMAX model has the highest fitness value. For this reason it was the chosen model for the consequent estimations.

The ARMAX model has the following form:

\begin{equation}
A(q)y(t) = B(q)u(t-n_k)+C(q)e(t)
\end{equation}

Where y(t) is the output at time t, angle of the elbow joint, in this case; u(t) are the inputs, being the processed sEMG values from biceps brachii, brachioradialis and triceps brachii; e(t) is the white-noise disturbance; \(n_k\)  is the delay for each input; q is the delay operator; A, B and C are the model coefficients, defined by:

\begin{equation}
A(q) = 1 + a_1q^{-1}+\dots+a_{n_a}q^{-n_a}
\end{equation}
\begin{equation}
B(q) = 1 + b_1q^{-1}+\dots+b_{n_b}q^{-n_b+1}
\end{equation}
\begin{equation}
C(q) = 1 + c_1q^{-1}+\dots+c_{n_c}q^{-n_c}
\end{equation}

Where \(n_a\) is the system's number of poles; \(n_b\) is the number of zeroes plus one; \(n_c\) is the number of C coefficients.

The coefficient orders were calculated and can be found in table \ref{ta:order}.

\begin{table}[h]
\caption{Model parameters orders for each subject.}
\label{table_example}
\begin{center}
\begin{tabular}{|c|c|c|c|c|}
\hline
 & \(n_a\) & \(n_b\) & \(n_c\) & \(n_k\)\\
\hline \hline
Subject 1 & 1 & 1, 1, 4 & 1 & 8, 7, 0\\
\hline
Subject 2 & 1 & 1, 1, 4 & 1 & 8, 7, 0\\
\hline
Subject 3 & 1 & 1, 1, 4 & 1 & 8, 7, 0\\
\hline
Subject 4 & 1 & 1, 1, 4 & 1 & 8, 7, 0\\
\hline
Subject 5 & 1 & 1, 1, 4 & 1 & 8, 7, 0\\
\hline
Subject 6 & 1 & 1, 1, 4 &1 & 8, 7, 0\\
\hline
Subject 7 & 5 & 10, 3, 2 & 6 & 5, 3, 4\\
\hline
\end{tabular}
\end{center}

\label{ta:order}
\end{table}

\begin{figure}[thpb]
      \centering
      \includegraphics[scale=0.3]{Images/3kg.jpg}
      \caption{Comparison between the measured angle of the elbow joint and the angle calculated through the use of the estimated model, for subject 5. a) shows the comparison for the continuous movement and b) the comparison for the discrete movement}
      \label{Angle Comparison}
   \end{figure}

Using the values from table \ref{ta:order} as the ARMAX model orders, it was possible to calculate the model for elbow joint angle using the three sEMG measurements and compare it to the experimentally measured values. As an example, figure \ref{Angle Comparison} shows a comparison between the calculated and measured angle, for continuous and intermittent movement, for subject 5.

With the ARMAX model calculated for the test subjects, we aimed at validating the model. Using the same model order and parameters previously calculated we estimated the response of the system using a second batch of recorded data. An example of the result of this process can be seen in figure \ref{Validation Procedure}, where the model calculated for the subject number 3 was used to estimate the elbow joint angle using the input data acquired from the second day of testing.

\begin{figure}[thpb]
      \centering
      \includegraphics[scale=0.5]{Images/validation.jpg}
      \caption{Validation procedure to determine if the calculated model can be applied to the same test subject for tests made in different days. a) shows the comparison for the continuous movement and b) the comparison for the discrete movement}
      \label{Validation Procedure}
   \end{figure}

To better determine the accuracy of the model, two performance parameters were used: The correlation and the root-mean-square error (RMSE)  between the estimated and the measured elbow joint angles. Table \ref{ta:corr} shows the accuracy evaluation parameters for every test subject and every test set.

\begin{table}[h]
\caption{Correlation factor and Root-mean-square error for the estimated and measured angle values}
\label{table_example}
\begin{center}
\resizebox{\columnwidth}{!}{%
\begin{tabular}{|c c|c c|c c|c c|c c|}
\hline
\multicolumn{2}{|c|}{} & \multicolumn{4}{c|}{Calibration Test} & \multicolumn{4}{c|}{Validation Test} \\
\hline
\multicolumn{2}{|c|}{} & \multicolumn{2}{c|}{Continuous} & \multicolumn{2}{c|}{Intermittent} & \multicolumn{2}{c|}{Continuous} & \multicolumn{2}{c|}{Intermittent} \\
\hline
\multicolumn{2}{|c|}{} & Correlation & RMSE & Correlation & RMSE & Correlation & RMSE & Correlation & RMSE\\
\hline \hline

& 0 kg &0.8914 &15.61 &0.8218 &20.65 & 0.8966 & 29.85 & 0.9107 & 18.44 \\
Subject 1 & 1.5 kg &0.7761 &16.66 &0.8251 & 15.19 &0.825 &18.48 & 0.47 & 34.9\\
& 3 kg &0.9497 &13.82 &0.9011 &16.85 & 0.6123 & 29.13 & 0.8488 & 18.85\\
\hline

& 0 kg &0.9285 &11.29 &0.9659 &9.51 & 0.8249 & 19.28 & 0.941 & 20.43\\
Subject 2 & 1.5 kg &0.8011 &26.33 &0.897 & 16.59 & 0.8735 & 17.48 & 0.8935 & 22\\
& 3 kg &0.9314 &11.85 &0.9368 &13.7& 0.8666 & 16.32 & 0.8906 & 18.61\\
\hline

& 0 kg &0.8682 &19.47 &0.9123 &16.29 & 0.9033 & 18.04 & 0.8572 & 20.17\\
Subject 3 & 1.5 kg &0.9383 &16.61 & 0.9413& 12.42 & 0.922 & 16.045 & 0.9109 & 16.61\\
& 3 kg &0.9341 &13.66 &0.9602 &10.43& 0.9852 & 16.4 & 0.9048 & 17.16\\
\hline

& 0 kg &0.8806 &19.63 &0.88 &21.56 & 0.752 & 29.62 & 0.8931 & 21.87\\
Subject 4 & 1.5 kg &0.9408 &16.052 &0.9199 &17.95 & 0.3377 & 66.23 & 0.7704 & 39.44\\
& 3 kg &0.9608 &17.628 &0.862 &21.86 & 0.6104 & 139.73 & 0.7643 & 53.31\\
\hline

& 0 kg &0.8974 &18.805 &0.873 &24.68 & 0.9407 & 44.398 & 0.9192 & 16.322\\
Subject 5 & 1.5 kg &0.9233 &27.954 &0.823 &17.31 & 0.8801 & 15.427 & 0.8784 & 16.912\\
& 3 kg & 0.9621&7.286 &0.9239 &10.89& 0.8652 & 21.81 & 0.8445 & 24.316\\
\hline

& 0 kg &0.9256 &14.311 &0.9029 &20.76 & 0.8793 & 34.11 & 0.9258 & 53.08\\
Subject 6 & 1.5 kg &0.8184 &19.37 &0.828 &19.93 & 0.7782 & 21.96 & 0.9317 & 26.05\\
& 3 kg & 0.8323 & 19.13 &0.9169 &16.3 & 0.7722 & 102.16 & 0.8043 & 31.88\\
\hline

& 0 kg &0.9147 &18.515 &0.9057 &15.48 & 0.8063 & 19.335 & 0.9 & 15.815\\
Subject 7 & 1.5 kg &0.9066 & 12.424&0.9411 & 11.85 &0.8728 & 17.035 & 0.887 & 18.509\\
& 3 kg &0.9194 &12.857 &0.949 &12.6 & 0.7825 & 18.16 & 0.931 & 15.12\\
\hline

\end{tabular}%
}
\end{center}

\label{ta:corr}
\end{table}

\subsection{Non-Linear System Modeling}

\subsection{Results}

\subsection{Discussion and Conclusions}

This chapter proposed a method for determining the elbow joint angle based on the measurement of the sEMG of biceps brachii, triceps brachii and brachioradialis from 7 test subjects. The arm model was estimated using the data collected from the experiment and a system identification method, more specifically, ARMAX. Using the acquired sEMG data as input to the estimated model, it was possible to obtain an estimation of the elbow angle. Using the data from the IMU (real angle value) it was possible to validate the estimation based on sEMG.

The experimental data showed that it is possible to use only the EMG data to estimate a correlation between joint angle and sEMG values. Even though estimating one model for continuous movement and another one for discrete movement gives higher precision, it is possible to calculate a single model for both movements.

As stated before, by lifting different weights the model parameters are altered.
For the same test subject the A(q) and C(q) parameters (see eq. 3) maintained values with less than 1\% difference from one another, while the B(q) values assumed a greater range of values.

Even for different subjects, the estimated A(q) and C(q) parameters also had a difference of less than 1\% from one another.

From the seven subjects, six of them could be estimated by an ARMAX model with the same parameters order. The test subject with different order parameters was the one that presented the worst readings of the brachioradialis muscle EMG. Because of that, a lot of noise is introduced, requiring a higher order system to overcome the modeling errors. The brachioradialis is the most difficult muscle to read the sEMG signals compared to the biceps brachii and the triceps brachii. This difficulty is due to the muscle short length causing the electrodes to stay close to the tendon, which induces  reading errors. Not coincidentally, this test subject was the one with smaller stature.

The repeatability of the model was successful for the cases studied in this work, even though it is possible to note that the model is not as precise as it was for the calibration procedure.

In future works it will be studied if it is possible to define one single estimated model for each subject, independent of the weight being lifted, or even one global model that is capable of estimating joint angles for a big range of individuals. The calculated models will be used for the control of an EMG-driven upper limb exoskeleton.


\documentclass[letterpaper]{article}

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

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

\begin{document}
\title{Homework 1}
\date{September 5, 2014}
\author{Jon Allen}
\maketitle
\section*{2.2 C}
we pick
\section*{2.2 D}
\section*{2.3 D}
\section*{Worksheet 1}
\subsection*{a}
if $x>1$ then $x^2>x$. If $0<x<1$ then $x^2<x$

\subsubsection*{proof}
\begin{align*}
  x^2&=x\cdot x\\
  x\cdot x&>0\text{ by axiom 8}\\
  x\cdot x&=x_1+x_2+...+x_x\\
  0&<x\\
  0+x&<x+x \text{ by axiom 7}\\
  x&<x_1+x_2+...+x_i \text{ by induction}\\
\end{align*}
Therefore $x<x^2$
$\Box$
\subsection*{b}
if $x>0$ then $x^{-1}>0$
\subsubsection*{proof}
let $x^{-1}=z$. Then $xz=1$. Axiom 8 states that if $0>xy$ then $x<0$ or $y<0$. Because multiplication is closed on $\mathbb{R}$ we know that $-xz=-1$. Since $-1<0$ we know that $x<0$ or $-z<0$ because $x>0$ we know that $-z<0$. Or $z>0$ $\Box$
\subsection*{c}
if $0<x<y$ then $0<y^-1<x^-1$
\subsubsection*{proof}
$y^{-1}=m, x^{-1}=n$. $xn=1, ym=1$.
\section*{Worksheet 2}
\subsection*{a}
supremum=$\frac{3}{2}$. infimum=$-1$. No minimum, maximum is $\frac{3}{2}$
\subsection*{b}
this set is all reals. no supremum, infimum or min or max
\section*{Worksheet 3}
let m lower bound for A. Then $m<x$ for all x in A. and $-x < -m$. because -x is in -A then we have an upper bound of -m for -A. Thus inf(A)=-sup(-A)
\end{document}


\documentclass[a4paper,11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{algorithmic}
\usepackage{algorithm}
\usepackage{pst-plot}
\usepackage{graphicx}
\usepackage{endnotes}
\usepackage{graphics}
\usepackage{floatflt}
\usepackage{wrapfig}
\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage{verbatim}
\usepackage{hyperref}
\usepackage{multirow}
\usepackage{pdflscape}
\usepackage{enumitem}
\usepackage[normalem]{ulem}

\usepackage{hyperref}
\hypersetup{pdfborder={0 0 0 0}}

\pdfpagewidth 210mm
\pdfpageheight 297mm
\setlength\topmargin{0mm}
\setlength\headheight{0mm}
\setlength\headsep{0mm}
\setlength\textheight{250mm}
\setlength\textwidth{159.2mm}
\setlength\oddsidemargin{0mm}
\setlength\evensidemargin{0mm}
\setlength\parindent{7mm}
\setlength\parskip{0mm}

\newenvironment{exercise}[3]{\paragraph{Exercise #1: #2 (#3pt)}\ \\}{
\medskip}
\newcommand{\question}[2]{\setlength\parindent{0mm}\ \\$\mathbf{Q_#1:}$ #2\ \\}

\author{\large{Tambet Matiisen, Raul Vicente}}
\title{\huge{Introduction to Computational Neuroscience}\\\LARGE{Practice on Artificial Neural Networks}}

\begin{document}
\maketitle

\textbf{A request:} Please track how long it will take to complete this set of exercises. Add this time to your final report.
\ \\

%
% Intro
%
In this session we are going to have a brief look on artificial neural networks. We start with simplest artificial neuron model called perceptron. Then we will see how simple feed-forward neural networks can be thought of as universal function approximators and what their limitations are. Finally we will use artificial neural network to recognize handwritten digits.

%
% Perceptron
%
\begin{exercise}{1}{Perceptron}{1}

\begin{wrapfigure}{r}{0.3\textwidth}
	\centering
	\vspace{-12pt}
	\includegraphics[width=0.22\textwidth]{perceptron.png}
	\caption{Simple perceptron}
	\label{fig:perceptronexample}
	\vspace{-5pt}
\end{wrapfigure}

Perceptron is the simplest artificial network model invented by Frank Rosenblatt in late 1950s. He added learning rule to McCulloch-Pitts neuron, that allows it to learn certain functions from example inputs and outputs.

Perceptron works on binary data – both its inputs $x_j$ and output $y$ are ones or zeros. Output 1 or 0 can be thought of as binary classification – whether object represented by input  belongs to certain class or not.

Perceptron’s weights $w_j$ can be any real numbers. Its prediction is calculated with following formula:

$$
y = \left\{
	\begin{array}{l l}
		1, \text{if } x_1 w_1 + ... + x_m w_m + b \geq 0\\
		0, \text{otherwise}
	\end{array}
	\right.
$$

Here $b$ is the bias term, that is added to the sum. In practice it is easier to just add additional input, which is always one. Then we don’t have to treat bias as special, it is just additional weight. Learning rule for perceptron is very simple:

$$
w_j = w_j + (t_i - y_i) x_{ij}
$$

Index $i$ is used to denote $i$th data sample. Learning rule must be applied for all datasamples and for each weight. You will continue updating the weights until all data samples are classified correctly.

It turns out, that perceptron is always able to successfully learn classification rule for datasets, which are \textit{linearly separable} (data points can be separated by line in case of two intputs, plane in case of three inputs or hyperplane in case of input of any dimensionality). If the dataset is not linearly separable, perceptron will never \textit{converge} (settle to certain weight values). \newline

Example code for this exercise is in \texttt{perceptron.m}. Your task is to fill in the perceptron learning rule and decide for four example datasets if they are linearly separable or not. In your report include final image with decision boundary for all four datasets. For linearly separable datasets also add approximate number of steps to convergence.

\end{exercise}

%
% Sinusoid
%
\begin{exercise}{2}{Function Approximation}{1.5}

Artificial neural networks can be thought of as universal function approximators. Indeed, Universal Approximation Theorem states, that feed-forward network with single hidden layer containing finite number of neurons can approximate any continous function to any precision. In practice this theorem is of no use, because:
\begin{itemize}
  \item it states only, that these functions can be \uline{represented} by feed-forward network with one hidden layer, it doesn’t say anything if they are \uline{learnable};
  \item construction used in the proof uses huge number of neurons in hidden layer, which would be unreasonable for any practical application;
  \item as the theorem doesn’t consider learnability, it doesn’t state anything about how well the networks generalizes to samples outside of training data.
\end{itemize}

But it still nice result to guide your thinking – if some problem can be described as a function calculating output from several inputs, then it probably can be approximated with artificial neural network.\newline

\begin{wrapfigure}{r}{0.3\textwidth}
	\centering
	\vspace{-12pt}
	\includegraphics[width=0.20\textwidth]{sine.png}
	\label{fig:sineexample}
	\vspace{-5pt}
\end{wrapfigure}

In this practice session we are going to approximate sine function using neural network. This neural network is very simple – it consists of just one input node (the $x$), several hidden nodes and just one output node ($y = sin(x)$).

Hidden nodes use sigmoid activation function to achieve non-linearity. Output node is linear, no activation function is applied. Loss function is simple squared error:

$$
L = \frac{1}{2n}\sum_{i=1}^{n} (t_i - y_i)^2
$$

It calculates average loss over all data points in training set. \newline

For this exercise run the code in \texttt{sinusoid.m} and answer following questions:
\question{1}{Why don't we always get the same result during training? Why we sometimes achieve better approximation and sometimes worse?}
\question{2}{What is the minimum number of hidden nodes to approximate sinusoid with four bumps (like in the example given)? Include figure with your report. I’m asking for occasional (or theoretical) possibility, not that it approximates reliably with that many hidden nodes. \textbf{Bonus:} Why that might be? Function \texttt{plot\_sinusoid\_components(test\_x, nn)}  might give some hints.}
\question{3}{Apply network to inputs outside of training range, for example from $-4\pi$ to $4\pi$. How well the network generalizes to data outside of training range? Include figure.}

\end{exercise}

%
% MNIST
%
\begin{exercise}{3}{Classification of handwritten digits}{1.5pt + bonus 1}

Finally we will use artificial neural network to classify handwritten digits. For that we are going to use MNIST dataset, that was historically very important. This dataset was used to train convolutional neural networks by Yann LeCun et al. and the resulting system was one of the first commercial successes of neural networks in 1990s. At some point it was used to read 10\% of the checks in North America. It is widely used bencmark even today, because it's fast to train even on modest CPU and there is plenty of baseline performances to compare to.

Code for this excercise is in file \texttt{mnist.m}. For neural networks we are making use of DeepLearnToolbox toolkit by Rasmus Berg Palm. The tookit is already included with your download in folder \texttt{DeepLearnToolbox}. Feel free to explore the source in \texttt{DeepLearnToolbox/NN} folder, it is quite clean Matlab code.

After you have run the code and got initial results, your job is to improve classification accuracy. For that you need to tune learning rate, weight decay, number of hidden nodes and number of epochs (iterations over full data set). As tuning neural networks can be quite tedious and frustrating, here are few guidelines:

\begin{enumerate}
  \item Start with learning rate. Try 1 first and then go down by powers of ten, i.e. 1, 0.1, 0.01, 0.001 and so on. Use the first value, when the loss graph is stable (without zig-zags) and goes down. To squeeze out the last bit, you can try values between powers of ten, but usually you don't gain much.
  \item Once you have stabilized the learning, try to increase number of hidden nodes and see if testing error improves.
  \item If your network is well tuned, then you should see overfitting, which means that network learns training examples, but doesn't generalize to the test set. You can detect this from large gap between training and validation misclassification rate. Time to bring in weight decay! Start with very small values and increase in powers of ten till test error starts to get worse, i.e. 0.00001, 0.0001, 0.001, 0.01, 0.1.
  \item Mind that there is a delicate interplay between the parameters – if you increase weight decay, then you might need to increase also learning rate; if you increase number of hidden nodes, you might need to decrease learning rate and so on.
  \item Finally, if loss function seems to have steady downward direction, increase number of epochs and see how low it goes. Usually it plateaus at some point and there is no reason to train further.
\end{enumerate}

Include figure with training and validation error in your report, along with testing error. You are expected to achieve at least 3\% test error rate. \textbf{Bonus point awarded to everybody with test error below 2\%!}

\end{exercise}

%
% Adaline video
%
\begin{exercise}{4*}{Science in Action}{bonus 1}

For this excercise you have to watch one cool video from 1960s. It's about simple analog neuron called ADALINE invented by Bernard Widrow. With all the recent hype around deep learning, it is refreshing to see how much of that was already in place in 1960s.

Watch the video here: \url{https://www.youtube.com/watch?v=IEFRtz68m-8} (until 21:43)

\question{}{What applications for adaptive neurons people foresaw already in 1960s?}

\end{exercise}

\ \\
\ \\
\ \\
\ \\
\ \\
Please submit a \texttt{pdf} report with answers to the questions and comments about your solutions. Include figures, explanations and essential pieces of code. Do not include the code itself as a separate file, your report should give good understanding of what you have done. Please mark how long it took to complete this set of exercises. Upload the \texttt{pdf} to the practice session page on the course website.

\end{document}

2015/Practices/08 - Artificial Neural Networks/text/cns-ann.tex


\documentclass[12pt]{cdblatex}
\usepackage{exercises}
\usepackage{fancyhdr}
\usepackage{footer}

\begin{document}

% --------------------------------------------------------------------------------------------
\section*{Exercise 5.4 Deleting a term using tags}

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

   def add_tags (obj,tag):
      n = 0
      ans = Ex('0')
      for i in obj.top().terms():
         foo = obj[i]
         bah = Ex(tag+'_{'+str(n)+'}')
         ans := @(ans) + @(bah) @(foo).
         n = n + 1
      return ans

   def clear_tags (obj,tag):
      ans := @(obj).
      foo  = Ex(tag+'_{a?} -> 1')
      substitute (ans,foo)
      return ans

   expr := A_{a b} B^{a b} + A_{a b} A_{c d} B^{a b} B^{c d} - C_{a b} B^{a b}.  # cdb (ex-0504.100,expr)

   expr  = add_tags (expr,'\\mu')                                                # cdb (ex-0504.101,expr)

   substitute (expr, $\mu_{1} -> 0$)                                             # cdb (ex-0504.102,expr)

   expr = clear_tags (expr,'\\mu')                                               # cdb (ex-0504.103,expr)

\end{cadabra}

\begin{dgroup*}
   \Dmath*{ \cdb*{ex-0504.100} }
   \Dmath*{ \cdb*{ex-0504.101} }
   \Dmath*{ \cdb*{ex-0504.102} }
   \Dmath*{ \cdb*{ex-0504.103} }
\end{dgroup*}

\end{document}


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[a4paper, total={7in, 10in}]{geometry}
\usepackage[usenames, dvipsnames]{color}
\usepackage{listings}

\definecolor{myred}{RGB}{255, 0, 0}
\definecolor{mywhat}{RGB}{255, 0, 219}
\definecolor{mygreen}{RGB}{0, 255, 0}
\definecolor{myblue}{RGB}{0, 0, 255}

\begin{document}
\title{%
		\LARGE \textbf{Division in relational algebra} \\
        }
\author{
	Nicolas Novalic
}
\begin{center}
	{\Huge Understanding the division operator\\ in Relational Algebra} \\
\end{center}

First lets take a quick look at how the operator works. Say we have two relations, $R$ with schema: $(A, B)$ and $S$ with schema: $(B)$. An instance of them could be:
\vskip 0.2in

\parbox{.45\linewidth}{
\begin{center}
  \begin{tabular}{ll}
  	\hline
    A & B \\
    \hline
    \multicolumn{1}{|c|}{a} & \multicolumn{1}{c|}{1} \\ \hline
	\multicolumn{1}{|c|}{a} & \multicolumn{1}{c|}{2} \\ \hline
    \multicolumn{1}{|c|}{a} & \multicolumn{1}{c|}{7} \\ \hline
    \multicolumn{1}{|c|}{b} & \multicolumn{1}{c|}{1} \\ \hline
  \end{tabular}
\end{center}
}
\parbox{.45\linewidth}{
\begin{center}
  \begin{tabular}{l}
  	\hline
    B \\
    \hline
    \multicolumn{1}{|c|}{1} \\ \hline
	\multicolumn{1}{|c|}{7} \\ \hline
    \multicolumn{1}{|c|}{2} \\ \hline
  \end{tabular}
\end{center}
}
\vskip 0.2in
If we name $P$ and $Q$ these instances of $R$  and $S$ respectively, the result of $P \div Q$ is:
\vskip 0.2in
\begin{center}
  \begin{tabular}{l}
  	\hline
    A \\
    \hline
    \multicolumn{1}{|c|}{a} \\ \hline
  \end{tabular}
\end{center}
\vskip 0.2in

What happened here? The division operator took all the values from attribute $A$ that were in a relation (in $R$) with \textbf{all} the values of the $B$ attribute in $S$.
We were able to perform the operation because the last attribute of $R$ had the same name as the attribute on $S$. In general, the last attributes of the first operator \textbf{must} have the same name as the attributes in the second operator and \textbf{must} be in the same order.
\vskip 0.2in
\noindent\fbox{\parbox{\textwidth}{%
Given two relations, $R1$ and $R2$ and their schema: $(A_1,\dots,A_n,B_1,\dots,B_m)$ and $(B_1,\dots,B_m)$ resp., $R1 \div R2$ retrieves a relation $R3$ with schema $(A_1,\dots,A_n)$ where all the sub-tuples $(A_1,\dots,A_n)$ of $R1$ were in a relation with every tuple of $R2$.
}}
\subsection*{Advanced example and explanation}

Consider the following instances of the relations $R1$ and $R2$ defined before:
\vskip 0.2in

\parbox{.45\linewidth}{
\begin{center}
  \begin{tabular}{lllll}
  	\hline
    A & B & C & D & E\\
    \hline
    \multicolumn{1}{|c|}{$x_1$} & \multicolumn{1}{c|}{$y_2$} & \multicolumn{1}{c|}{$z_2$} & \multicolumn{1}{c|}{$w_a$} & \multicolumn{1}{c|}{$w_b$}\\ \hline
    \multicolumn{1}{|c|}{$x_1$} & \multicolumn{1}{c|}{$y_2$} & \multicolumn{1}{c|}{$z_2$} & \multicolumn{1}{c|}{$w_z$} & \multicolumn{1}{c|}{$w_z$}\\ \hline
    \multicolumn{1}{|c|}{$x_1$} & \multicolumn{1}{c|}{$y_2$} & \multicolumn{1}{c|}{$z_2$} & \multicolumn{1}{c|}{$w_\phi$} & \multicolumn{1}{c|}{$w_z$}\\ \hline
    \multicolumn{1}{|c|}{$x_1$} & \multicolumn{1}{c|}{$y_2$} & \multicolumn{1}{c|}{$z_2$} & \multicolumn{1}{c|}{$w_b$} & \multicolumn{1}{c|}{$w_q$}\\ \hline
    \multicolumn{1}{|c|}{$x_1$} & \multicolumn{1}{c|}{$y_2$} & \multicolumn{1}{c|}{$z_2$} & \multicolumn{1}{c|}{$w_w$} & \multicolumn{1}{c|}{$w_u$}\\ \hline
    \multicolumn{1}{|c|}{$x_1$} & \multicolumn{1}{c|}{$y_2$} & \multicolumn{1}{c|}{$z_2$} & \multicolumn{1}{c|}{$w_b$} & \multicolumn{1}{c|}{$w_b$}\\ \hline
    \multicolumn{1}{|c|}{$x_1$} & \multicolumn{1}{c|}{$y_2$} & \multicolumn{1}{c|}{$z_2$} & \multicolumn{1}{c|}{$w_x$} & \multicolumn{1}{c|}{$w_h$}\\ \hline
    \multicolumn{1}{|c|}{$x_1$} & \multicolumn{1}{c|}{$y_2$} & \multicolumn{1}{c|}{$z_2$} & \multicolumn{1}{c|}{$w_q$} & \multicolumn{1}{c|}{$w_a$}\\ \hline
	\multicolumn{1}{|c|}{$x_2$} & \multicolumn{1}{c|}{$y_5$} & \multicolumn{1}{c|}{$z_1$} & \multicolumn{1}{c|}{$w_a$} & \multicolumn{1}{c|}{$w_b$}\\ \hline
    \multicolumn{1}{|c|}{$x_2$} & \multicolumn{1}{c|}{$y_4$} & \multicolumn{1}{c|}{$z_1$} & \multicolumn{1}{c|}{$w_a$} & \multicolumn{1}{c|}{$w_b$}\\ \hline
    \multicolumn{1}{|c|}{$x_3$} & \multicolumn{1}{c|}{$y_7$} & \multicolumn{1}{c|}{$z_3$} & \multicolumn{1}{c|}{$w_a$} & \multicolumn{1}{c|}{$w_x$}\\ \hline
    \multicolumn{1}{|c|}{$x_3$} & \multicolumn{1}{c|}{$y_7$} & \multicolumn{1}{c|}{$z_3$} & \multicolumn{1}{c|}{$w_a$} & \multicolumn{1}{c|}{$w_b$}\\ \hline
    \multicolumn{1}{|c|}{$x_3$} & \multicolumn{1}{c|}{$y_7$} & \multicolumn{1}{c|}{$z_3$} & \multicolumn{1}{c|}{$w_x$} & \multicolumn{1}{c|}{$w_h$}\\ \hline
    \multicolumn{1}{|c|}{$x_3$} & \multicolumn{1}{c|}{$y_7$} & \multicolumn{1}{c|}{$z_3$} & \multicolumn{1}{c|}{$w_b$} & \multicolumn{1}{c|}{$w_q$}\\ \hline
    \multicolumn{1}{|c|}{$x_3$} & \multicolumn{1}{c|}{$y_7$} & \multicolumn{1}{c|}{$z_3$} & \multicolumn{1}{c|}{$w_w$} & \multicolumn{1}{c|}{$w_u$}\\ \hline
    \multicolumn{1}{|c|}{$x_3$} & \multicolumn{1}{c|}{$y_7$} & \multicolumn{1}{c|}{$z_3$} & \multicolumn{1}{c|}{$w_z$} & \multicolumn{1}{c|}{$w_z$}\\ \hline
    \multicolumn{1}{|c|}{$x_3$} & \multicolumn{1}{c|}{$y_1$} & \multicolumn{1}{c|}{$z_1$} & \multicolumn{1}{c|}{$w_a$} & \multicolumn{1}{c|}{$w_b$}\\ \hline
    \multicolumn{1}{|c|}{$x_3$} & \multicolumn{1}{c|}{$y_7$} & \multicolumn{1}{c|}{$z_3$} & \multicolumn{1}{c|}{$w_b$} & \multicolumn{1}{c|}{$w_b$}\\ \hline
    \multicolumn{1}{|c|}{$x_3$} & \multicolumn{1}{c|}{$y_7$} & \multicolumn{1}{c|}{$z_3$} & \multicolumn{1}{c|}{$w_\phi$} & \multicolumn{1}{c|}{$w_z$}\\ \hline
  \end{tabular}
\end{center}
}
\parbox{.45\linewidth}{
\begin{center}
  \begin{tabular}{ll}
  	\hline
    D & E\\
    \hline
    \multicolumn{1}{|c|}{$w_a$} & \multicolumn{1}{c|}{$w_b$}\\ \hline
    \multicolumn{1}{|c|}{$w_z$} & \multicolumn{1}{c|}{$w_z$}\\ \hline
    \multicolumn{1}{|c|}{$w_\phi$} & \multicolumn{1}{c|}{$w_z$}\\ \hline
    \multicolumn{1}{|c|}{$w_b$} & \multicolumn{1}{c|}{$w_q$}\\ \hline
    \multicolumn{1}{|c|}{$w_w$} & \multicolumn{1}{c|}{$w_u$}\\ \hline
    \multicolumn{1}{|c|}{$w_b$} & \multicolumn{1}{c|}{$w_b$}\\ \hline
    \multicolumn{1}{|c|}{$w_x$} & \multicolumn{1}{c|}{$w_h$}\\ \hline
  \end{tabular}
\end{center}
}
\newpage
\parbox{.70\linewidth}{
Let's name $X$ and $Y$ the previous instances of $R1$ and $R2$ respectively. \\

\vskip 0.2in
Now, I will show a really easy way to understand the operator. It's nothing formal, just a way to see things. It consists on taking the table $X$ and group it by the attributes $(A,B,C)$, just like you do in SQL when using the \textbf{GROUP BY} statement. The groups are then represented by the value of their respective $(A,B,C)$ attributes. Now we assign a set to each one of these groups, which contains all the pairs of values of the attributes $(D,E)$ that are in a relation with the values that represent the group. Then we can check if the tuples of $Y$ are included on each of these sets, if they are, then the values that represent that grouping are added to the solution, otherwise they are not. This is a bit confusing, but really easy to understand with the an example.
\vskip 0.2in
We start by identifying the rows of the table that are equal two to two on their $(A,B,C)$ attributes. On table to the right we colored the identified groups.

\vskip 0.2in
}
\parbox{.25\linewidth}{
\begin{flushright}
  \begin{tabular}{lllll}
  	\hline
    A & B & C & D & E\\
    \hline
    \multicolumn{1}{|c|}{$\textcolor{myred}{x_1}$} & \multicolumn{1}{c|}{$\textcolor{myred}{y_2}$} & \multicolumn{1}{c|}{$\textcolor{myred}{z_2}$} & \multicolumn{1}{c|}{$\textcolor{myred}{w_a}$} & \multicolumn{1}{c|}{$\textcolor{myred}{w_b}$}\\ \hline
    \multicolumn{1}{|c|}{$\textcolor{myred}{x_1}$} & \multicolumn{1}{c|}{$\textcolor{myred}{y_2}$} & \multicolumn{1}{c|}{$\textcolor{myred}{z_2}$} & \multicolumn{1}{c|}{$\textcolor{myred}{w_z}$} & \multicolumn{1}{c|}{$\textcolor{myred}{w_z}$}\\ \hline
    \multicolumn{1}{|c|}{$\textcolor{myred}{x_1}$} & \multicolumn{1}{c|}{$\textcolor{myred}{y_2}$} & \multicolumn{1}{c|}{$\textcolor{myred}{z_2}$} & \multicolumn{1}{c|}{$\textcolor{myred}{w_\phi}$} & \multicolumn{1}{c|}{$\textcolor{myred}{w_z}$}\\ \hline
    \multicolumn{1}{|c|}{$\textcolor{myred}{x_1}$} & \multicolumn{1}{c|}{$\textcolor{myred}{y_2}$} & \multicolumn{1}{c|}{$\textcolor{myred}{z_2}$} & \multicolumn{1}{c|}{$\textcolor{myred}{w_b}$} & \multicolumn{1}{c|}{$\textcolor{myred}{w_q}$}\\ \hline
    \multicolumn{1}{|c|}{$\textcolor{myred}{x_1}$} & \multicolumn{1}{c|}{$\textcolor{myred}{y_2}$} & \multicolumn{1}{c|}{$\textcolor{myred}{z_2}$} & \multicolumn{1}{c|}{$\textcolor{myred}{w_w}$} & \multicolumn{1}{c|}{$\textcolor{myred}{w_u}$}\\ \hline
    \multicolumn{1}{|c|}{$\textcolor{myred}{x_1}$} & \multicolumn{1}{c|}{$\textcolor{myred}{y_2}$} & \multicolumn{1}{c|}{$\textcolor{myred}{z_2}$} & \multicolumn{1}{c|}{$\textcolor{myred}{w_b}$} & \multicolumn{1}{c|}{$\textcolor{myred}{w_b}$}\\ \hline

	\multicolumn{1}{|c|}{$\textcolor{myred}{x_1}$} & \multicolumn{1}{c|}{$\textcolor{myred}{y_2}$} & \multicolumn{1}{c|}{$\textcolor{myred}{z_2}$} & \multicolumn{1}{c|}{$\textcolor{myred}{w_x}$} & \multicolumn{1}{c|}{$\textcolor{myred}{w_h}$}\\ \hline
    \multicolumn{1}{|c|}{$\textcolor{myred}{x_1}$} & \multicolumn{1}{c|}{$\textcolor{myred}{y_2}$} & \multicolumn{1}{c|}{$\textcolor{myred}{z_2}$} & \multicolumn{1}{c|}{$\textcolor{myred}{w_q}$} & \multicolumn{1}{c|}{$\textcolor{myred}{w_a}$}\\ \hline
    \multicolumn{1}{|c|}{$x_2$} & \multicolumn{1}{c|}{$y_5$} & \multicolumn{1}{c|}{$z_1$} & \multicolumn{1}{c|}{$w_a$} & \multicolumn{1}{c|}{$w_b$}\\ \hline
	\multicolumn{1}{|c|}{$\textcolor{mygreen}{x_2}$} & \multicolumn{1}{c|}{$\textcolor{mygreen}{y_4}$} & \multicolumn{1}{c|}{$\textcolor{mygreen}{z_1}$} & \multicolumn{1}{c|}{$\textcolor{mygreen}{w_a}$} & \multicolumn{1}{c|}{$\textcolor{mygreen}{w_b}$}\\ \hline
    \multicolumn{1}{|c|}{$\textcolor{myblue}{x_3}$} & \multicolumn{1}{c|}{$\textcolor{myblue}{y_7}$} & \multicolumn{1}{c|}{$\textcolor{myblue}{z_3}$} & \multicolumn{1}{c|}{$\textcolor{myblue}{w_a}$} & \multicolumn{1}{c|}{$\textcolor{myblue}{w_x}$}\\ \hline
    \multicolumn{1}{|c|}{$\textcolor{myblue}{x_3}$} & \multicolumn{1}{c|}{$\textcolor{myblue}{y_7}$} & \multicolumn{1}{c|}{$\textcolor{myblue}{z_3}$} & \multicolumn{1}{c|}{$\textcolor{myblue}{w_a}$} & \multicolumn{1}{c|}{$\textcolor{myblue}{w_b}$}\\ \hline
    \multicolumn{1}{|c|}{$\textcolor{myblue}{x_3}$} & \multicolumn{1}{c|}{$\textcolor{myblue}{y_7}$} & \multicolumn{1}{c|}{$\textcolor{myblue}{z_3}$} & \multicolumn{1}{c|}{$\textcolor{myblue}{w_x}$} & \multicolumn{1}{c|}{$\textcolor{myblue}{w_h}$}\\ \hline
    \multicolumn{1}{|c|}{$\textcolor{myblue}{x_3}$} & \multicolumn{1}{c|}{$\textcolor{myblue}{y_7}$} & \multicolumn{1}{c|}{$\textcolor{myblue}{z_3}$} & \multicolumn{1}{c|}{$\textcolor{myblue}{w_b}$} & \multicolumn{1}{c|}{$\textcolor{myblue}{w_q}$}\\ \hline
    \multicolumn{1}{|c|}{$\textcolor{myblue}{x_3}$} & \multicolumn{1}{c|}{$\textcolor{myblue}{y_7}$} & \multicolumn{1}{c|}{$\textcolor{myblue}{z_3}$} & \multicolumn{1}{c|}{$\textcolor{myblue}{w_w}$} & \multicolumn{1}{c|}{$\textcolor{myblue}{w_u}$}\\ \hline
    \multicolumn{1}{|c|}{$\textcolor{myblue}{x_3}$} & \multicolumn{1}{c|}{$\textcolor{myblue}{y_7}$} & \multicolumn{1}{c|}{$\textcolor{myblue}{z_3}$} & \multicolumn{1}{c|}{$\textcolor{myblue}{w_z}$} & \multicolumn{1}{c|}{$\textcolor{myblue}{w_z}$}\\ \hline
    \multicolumn{1}{|c|}{$\textcolor{mywhat}{x_3}$} & \multicolumn{1}{c|}{$\textcolor{mywhat}{y_1}$} & \multicolumn{1}{c|}{$\textcolor{mywhat}{z_1}$} & \multicolumn{1}{c|}{$\textcolor{mywhat}{w_a}$} & \multicolumn{1}{c|}{$\textcolor{mywhat}{w_b}$}\\ \hline
    \multicolumn{1}{|c|}{$\textcolor{myblue}{x_3}$} & \multicolumn{1}{c|}{$\textcolor{myblue}{y_7}$} & \multicolumn{1}{c|}{$\textcolor{myblue}{z_3}$} & \multicolumn{1}{c|}{$\textcolor{myblue}{w_b}$} & \multicolumn{1}{c|}{$\textcolor{myblue}{w_b}$}\\ \hline
    \multicolumn{1}{|c|}{$\textcolor{myblue}{x_3}$} & \multicolumn{1}{c|}{$\textcolor{myblue}{y_7}$} & \multicolumn{1}{c|}{$\textcolor{myblue}{z_3}$} & \multicolumn{1}{c|}{$\textcolor{myblue}{w_\phi}$} & \multicolumn{1}{c|}{$\textcolor{myblue}{w_z}$}\\ \hline

\end{tabular}
\end{flushright}
}

Let's split the colored table by color, obtaining five sub-tables:

\vskip 0.2in

\parbox{.45\linewidth}{
\begin{center}
  \begin{tabular}{lllll}
  	\hline
    A & B & C & D & E\\
    \hline
    \multicolumn{1}{|c|}{$x_1$} & \multicolumn{1}{c|}{$y_2$} & \multicolumn{1}{c|}{$z_2$} & \multicolumn{1}{c|}{$w_a$} & \multicolumn{1}{c|}{$w_b$}\\ \hline
    \multicolumn{1}{|c|}{$x_1$} & \multicolumn{1}{c|}{$y_2$} & \multicolumn{1}{c|}{$z_2$} & \multicolumn{1}{c|}{$w_z$} & \multicolumn{1}{c|}{$w_z$}\\ \hline
    \multicolumn{1}{|c|}{$x_1$} & \multicolumn{1}{c|}{$y_2$} & \multicolumn{1}{c|}{$z_2$} & \multicolumn{1}{c|}{$w_\phi$} & \multicolumn{1}{c|}{$w_z$}\\ \hline
    \multicolumn{1}{|c|}{$x_1$} & \multicolumn{1}{c|}{$y_2$} & \multicolumn{1}{c|}{$z_2$} & \multicolumn{1}{c|}{$w_b$} & \multicolumn{1}{c|}{$w_q$}\\ \hline
    \multicolumn{1}{|c|}{$x_1$} & \multicolumn{1}{c|}{$y_2$} & \multicolumn{1}{c|}{$z_2$} & \multicolumn{1}{c|}{$w_w$} & \multicolumn{1}{c|}{$w_u$}\\ \hline
    \multicolumn{1}{|c|}{$x_1$} & \multicolumn{1}{c|}{$y_2$} & \multicolumn{1}{c|}{$z_2$} & \multicolumn{1}{c|}{$w_b$} & \multicolumn{1}{c|}{$w_b$}\\ \hline
    \multicolumn{1}{|c|}{$x_1$} & \multicolumn{1}{c|}{$y_2$} & \multicolumn{1}{c|}{$z_2$} & \multicolumn{1}{c|}{$w_x$} & \multicolumn{1}{c|}{$w_h$}\\ \hline
    \multicolumn{1}{|c|}{$x_1$} & \multicolumn{1}{c|}{$y_2$} & \multicolumn{1}{c|}{$z_2$} & \multicolumn{1}{c|}{$w_q$} & \multicolumn{1}{c|}{$w_a$}\\ \hline
  \end{tabular}

\vskip 0.2in

  \begin{tabular}{lllll}
  	\hline
    A & B & C & D & E\\
    \hline
    \multicolumn{1}{|c|}{$x_2$} & \multicolumn{1}{c|}{$y_5$} & \multicolumn{1}{c|}{$z_1$} & \multicolumn{1}{c|}{$w_a$} & \multicolumn{1}{c|}{$w_b$}\\ \hline
  \end{tabular}

\vskip 0.2in

  \begin{tabular}{lllll}
  	\hline
    A & B & C & D & E\\
    \hline
    \multicolumn{1}{|c|}{$x_2$} & \multicolumn{1}{c|}{$y_4$} & \multicolumn{1}{c|}{$z_1$} & \multicolumn{1}{c|}{$w_a$} & \multicolumn{1}{c|}{$w_b$}\\ \hline
  \end{tabular}
\end{center}
}
\parbox{.45\linewidth}{
\begin{center}
  \begin{tabular}{lllll}
  	\hline
    A & B & C & D & E\\
    \hline
    \multicolumn{1}{|c|}{$x_3$} & \multicolumn{1}{c|}{$y_7$} & \multicolumn{1}{c|}{$z_3$} & \multicolumn{1}{c|}{$w_a$} & \multicolumn{1}{c|}{$w_x$}\\ \hline
    \multicolumn{1}{|c|}{$x_3$} & \multicolumn{1}{c|}{$y_7$} & \multicolumn{1}{c|}{$z_3$} & \multicolumn{1}{c|}{$w_a$} & \multicolumn{1}{c|}{$w_b$}\\ \hline
    \multicolumn{1}{|c|}{$x_3$} & \multicolumn{1}{c|}{$y_7$} & \multicolumn{1}{c|}{$z_3$} & \multicolumn{1}{c|}{$w_x$} & \multicolumn{1}{c|}{$w_h$}\\ \hline
    \multicolumn{1}{|c|}{$x_3$} & \multicolumn{1}{c|}{$y_7$} & \multicolumn{1}{c|}{$z_3$} & \multicolumn{1}{c|}{$w_b$} & \multicolumn{1}{c|}{$w_q$}\\ \hline
    \multicolumn{1}{|c|}{$x_3$} & \multicolumn{1}{c|}{$y_7$} & \multicolumn{1}{c|}{$z_3$} & \multicolumn{1}{c|}{$w_w$} & \multicolumn{1}{c|}{$w_u$}\\ \hline
    \multicolumn{1}{|c|}{$x_3$} & \multicolumn{1}{c|}{$y_7$} & \multicolumn{1}{c|}{$z_3$} & \multicolumn{1}{c|}{$w_z$} & \multicolumn{1}{c|}{$w_z$}\\ \hline
    \multicolumn{1}{|c|}{$x_3$} & \multicolumn{1}{c|}{$y_7$} & \multicolumn{1}{c|}{$z_3$} & \multicolumn{1}{c|}{$w_b$} & \multicolumn{1}{c|}{$w_b$}\\ \hline
    \multicolumn{1}{|c|}{$x_3$} & \multicolumn{1}{c|}{$y_7$} & \multicolumn{1}{c|}{$z_3$} & \multicolumn{1}{c|}{$w_\phi$} & \multicolumn{1}{c|}{$w_z$}\\ \hline
  \end{tabular}

\vskip 0.2in

  \begin{tabular}{lllll}
  	\hline
    A & B & C & D & E\\
    \hline
    \multicolumn{1}{|c|}{$x_3$} & \multicolumn{1}{c|}{$y_1$} & \multicolumn{1}{c|}{$z_1$} & \multicolumn{1}{c|}{$w_a$} & \multicolumn{1}{c|}{$w_b$}\\ \hline
  \end{tabular}
\end{center}
}

\vskip 0.2in

Now let's see each of those tables like this:

\begin{itemize}
\item $(x_1,y_2,z_2) \rightarrow \{ (w_a,w_b), (w_z,w_z),(w_\phi,w_z),(w_b,w_q),(w_w,w_u),(w_b,w_b),(w_x,w_h),(w_q, w_a)\}$
\item $(x_2,y_5,z_1) \rightarrow \{ (w_a,w_b)\}$
\item $(x_2,y_4,z_1) \rightarrow \{ (w_a,w_b)\}$
\item $(x_3,y_7,z_3) \rightarrow \{ (w_a,w_x),(w_a,w_b), (w_x,w_h),(w_b,w_q),(w_w,w_u),(w_z,w_z),(w_b, w_b),(w_\phi, w_z)\}$
\item $(x_3,y_1,z_1) \rightarrow \{ (w_a,w_b)\}$
\end{itemize}

\vskip 0.2in

Also let's see the table $Y$ as a set:

\begin{itemize}
\item $\{ (w_a,w_b),(w_z,w_z), (w_\phi,w_z),(w_b,w_q),(w_w,w_u),(w_b,w_b),(w_x, w_h)\}$
\end{itemize}

\newpage

This is a clear representation of our data and we are ready to calculate the result of $X \div Y$. To simplify notation let's define a "function" in our universe, called \textbf{setOf()} that retrieves the set of tuples of what we pass to it as a parameter. Let's say, if we pass the parameter $Y$, it retrieves the set of it's tuples; if we pass three values $x,y,z$, it retrieves one of the sets defined before.

\vskip 0.2in
It's really easy to check that:

\begin{itemize}
  \item $setOf(Y) \subseteq setOf(x_1,y_2,z_2)$
  \item $setOf(Y) \not\subseteq setOf(x_2,y_5,z_1) $
  \item $setOf(Y) \not\subseteq setOf(x_2,y_4,z_1) $
  \item $setOf(Y) \subseteq setOf(x_3,y_7,z_3) $
  \item $setOf(Y) \not\subseteq setOf(x_3,y_1,z_1) $
\end{itemize}

\vskip 0.2in
And the result of $X \div Y$ is:

\vskip 0.2in
\begin{tabular}{lllll}
  \hline
  A & B & C\\
  \hline
  \multicolumn{1}{|c|}{$x_1$} & \multicolumn{1}{c|}{$y_2$} & \multicolumn{1}{c|}{$z_2$} \\ \hline
  \multicolumn{1}{|c|}{$x_3$} & \multicolumn{1}{c|}{$y_7$} & \multicolumn{1}{c|}{$z_3$} \\ \hline
\end{tabular}

\vskip 0.2in
I think this is a really good way to understand what the division operator does in relational algebra. This operator is the most confusing at first for some people, specially when using it with operands that have more than one attribute each.

\subsection*{Simple query example}

\vskip 0.2in
Let's see a quick example of how to use it to write a query. Let's say we have a simplified version of Blockbuster's database and some relations of it's relational schema are:

\begin{lstlisting}
CLIENTS(clientID, name, lastname)
MOVIES(movieID, title, description, year, duration, language)
RENTALS(clientID, movieID, date)
\end{lstlisting}

We want a relational algebra query that retrieves the ids of the clients that rented all the movies of the year 2010. \\

First of all we can find the ids of all the movies of the year 2010:\\

$A = \prod_{movieID}(\sigma_{year = 2010}MOVIES)$ \\

Now, let's project the relation RENTALS by clientID and movieID so we retrieve all the rentals ever done: \\

$B = \prod_{clientID, movieID}RENTALS$ \\

Let's divide B by A! Note that the second row of B is the same that the only row of A. By doing this we are asking for all the clientIDs that are in a relation with all the movies of 2010. To finish the query we use the JOIN operator and project the result by name and lastname: \\

$Solution = \prod_{name, lastname}(CLIENTS * (B \div A)) $

\end{document}