% !TEX encoding = UTF-8 Unicode
% !TEX spellcheck = en_US
% !TEX root = ../../../ICMA2020.tex

\subsection{Dynamics Model of Serial Robots}
This subsection describes the dynamics model of the 6-axis robot.
The joint torques $\boldsymbol{\tau}$ of all axes required for the feedforward control can be calculated by
%
\begin{equation}
\label{eq:model_equation1}
\begin{split}
\boldsymbol{\tau}=
\boldsymbol{M}(\boldsymbol{q}) \ddot{\boldsymbol{q}}+\boldsymbol{c}(\boldsymbol{q}, \dot{\boldsymbol{q}})+\boldsymbol{g}(\boldsymbol{q})+\boldsymbol{h}(\dot{\boldsymbol{q}}).
\end{split}
\end{equation}
%
Herein $\boldsymbol{q}$, $\dot{\boldsymbol{q}}$ and $\ddot{\boldsymbol{q}}$ represent the joint angles, velocities and accelerations given by the motion planning. $\boldsymbol{M}$ denotes the mass matrix, $\boldsymbol{c}$ the Coriolis effects and $\boldsymbol{g}$ the gravitational effects.
The friction model is represented by $\boldsymbol{h}$ with
%
\begin{equation}
h_{j}=f_{\mathrm{c}, j} \operatorname{sgn}\left(\dot{{q}}_{j}\right)+f_{\mathrm{v}, j} \dot{q}_{j},
\end{equation}
%
where $f_{\mathrm{c}, j}$ represents the Coulomb friction coefficient for joint $j$ and $f_{\mathrm{v}, j}$ the viscous friction coefficient.
The gear transmission between motor and link side is omitted for the sake of simplifying the equations. Measured motor velocities and estimated motor torques are transformed accordingly.

By expressing the rigid body dynamics with the inertial parameters (drive train inertia, mass, first and second moment of mass) and by using a linear friction model, (\ref{eq:model_equation1}) can be expressed in a linear form \cite{Khalil.2006}.
%
This regressor form
%
\begin{equation}
\boldsymbol{\tau}
=
\boldsymbol{X}'(\boldsymbol{q}, \dot{\boldsymbol{q}},\ddot{\boldsymbol{q}}) \boldsymbol{\theta}'
=
\boldsymbol{X}(\boldsymbol{q}, \dot{\boldsymbol{q}},\ddot{\boldsymbol{q}}) \boldsymbol{\theta}
\label{eq:model_regressor}
\end{equation}
%
with regression matrix $\boldsymbol{X}$ and parameter vector $\boldsymbol{\theta}$
can be given with a full set of parameters (on the left hand side, noted with a dash) or in a parameter minimal form (right hand side).
Only the latter form of the parameters can be identified within one identification cycle.
The general model $\boldsymbol{X}'$/$\boldsymbol{\theta}'$ uses 66 parameters for the rigid body dynamics.
For each of the six joints two additional parameters represent the Coulomb and viscous friction.

The right hand side of (\ref{eq:model_regressor}) expresses the minimal parametric model with 23 base inertial parameters.
The chosen parameter vector $\boldsymbol{\theta}$ to be identified therefore contains 35 entries and is shown in appendix \ref{sec:MinparamVector}.
%
Since some of the robot links possess symmetries, assumptions about their center of mass and products of inertia were made.
Further, a differential gear couples joints 5 and 6, similar to the robot example in \cite{M.Gautier.1995}.
This prevents the use of the direct determination of the base inertial parameters with the geometric approach from \cite{Khalil.2006}.
Instead, a purely symbolic approach is chosen: the relation $\boldsymbol{\theta}=\boldsymbol{K} \boldsymbol{\theta}'$ leads to a linear system of equations $\boldsymbol{X}' = \boldsymbol{X}'\boldsymbol{K}$.
It is solved symbolically for the unknown $\boldsymbol{K}$ with the computer algebra system \textsc{Maple}, leading to the base parameter formulation $\boldsymbol{X}$/$\boldsymbol{\theta}$.
For the implementation the regressor matrix of the energy instead of the dynamics is used, following \cite{M.Gautier.1995, Khalil.2006}.


%!TEX root = ../TTT4150-Summary.tex
\section{Position, velocity, and time (PVT) estimation}

\subsection{Position estimation with pseudoranges}
The pseudorange measurement from satellite $k$ after correction is
\begin{equation}\label{eq:pseudorange}
    \rho_c^{(k)} = r^{(k)} + c \cdot \delta t_u + \tilde{\epsilon}_\rho^{(k)}
\end{equation}
where $\delta t_u$ is the user clock bias, and $\tilde{\epsilon}_\rho^{(k)}$ is the combined effect of residual errors. The position of the user at the time of measurement is
\begin{equation}
    \V{x} = (x, y, z)
\end{equation}
and the position of satellite $k$ at signal transmission time is
\begin{equation}
    \V{x}^{(k)} = \left( x^{(k)}, y^{(k)}, z^{(k)} \right).
\end{equation}
The user-to-satellite range is
\begin{equation}
    r^{(k)} = \norm{\V{x}^{(k)} - \V{x}}
\end{equation}
and \eqref{eq:pseudorange} can be written as
\begin{equation}
    \rho_c^{(k)} = r^{(k)} + b + \tilde{\epsilon}_\rho^{(k)}.
\end{equation}

The \emph{geometry matrix} is
\begin{equation}
    \M{G}
    =
    \begin{bmatrix}
        (-\M{1}^{(1)})\T & 1     \\
        \vdots          & \vdots \\
        (-\M{1}^{(K)})\T & 1
    \end{bmatrix}
\end{equation}
where $K$ is the number of satellites in view, and $\M{1}^{(k)}$ is the line of sight unit vector from the initial user position estimate to satellite $k$.

\subsection{Dilution of precision (DOP)}
DOP quantifies the effect of satellite geometry on the accuracy of positioning. A good geometry gives a low DOP value, and high accuracy. There are several variants, using $\M{H} = \left( \M{G}\T \M{G} \right)^{-1}$:
\begin{itemize}
    \item
        Position dilution of precision
        \begin{equation}
            \mathrm{PDOP} = \sqrt{H_{11} + H_{22} + H_{33}}
        \end{equation}
    \item
        Time dilution of precision
        \begin{equation}
            \mathrm{TDOP} = \sqrt{H_{44}}
        \end{equation}
    \item
        Geometric dilution of precision
        \begin{equation}
            \mathrm{GDOP} = \sqrt{H_{11} + H_{22} + H_{33} + H_{44}}
        \end{equation}
    \item
        Horizontal dilution of precision
        \begin{equation}
            \mathrm{HDOP} = \sqrt{H_{11} + H_{22}}
        \end{equation}
    \item
        Vertical dilution of precision
        \begin{equation}
            \mathrm{VDOP} = \sqrt{H_{33}}
        \end{equation}
\end{itemize}


\subsection{Minimization algorithm}
\label{minimization}

In this thesis, many data treatment approaches needed efficient nonlinear
minimization algorithm (background subtraction, estimation of band intensities,
estimation of parameters of chemical model from spectral series).
The Levenberg-Marquardt method
\parencite{Marquardt1963}
was used as a basis for the minimization algorithm used in this thesis, and
slight modifications were applied to improve its performance.

The Levenberg-Marquardt method is based on Newton method where
the function $f(\vec{a})$ which needs to be minimized is approximated by its
Taylor series around $\vec{a_\text{cur}}$ point
\begin{align*}
	f(\vec{a}) =& f(\vec{a}_\text{cur})
		+ \sum_k \left.\frac{\partial{}f}{\partial{}a_k}\right
			\rvert_{\vec{a}_\text{cur}}(a_k - a_{k,\text{cur}})\\
		&+ \sum_{k,l} \left.\frac{
			\partial^2f
		}{
			\partial{}a_k\partial{}a_l
		}\right\rvert_{\vec{a}_\text{cur}}
		(a_k - a_{k,\text{cur}})(a_l - a_{l,\text{cur}})
		+ \dots,
\end{align*}
which can be written as a quadratic form
\begin{equation}
	\chi^2(\vec{a}) \approx
		\gamma
		- \vec{d}\cdot\vec{a}
		+ \frac{1}{2}\vec{a}\cdot{}\mat{D}\cdot\vec{a},
	\label{\eqnlabel{minimization:chi_approx}}
\end{equation}
where $\vec{d}$ is an $M$ vector, $\mat{D}$ is an $M\times{}M$ matrix, and
$\chi^2(\vec{a})$ is chi-square function
\begin{equation*}
	\chi^2(\vec{a}) \equiv \sum_{i=1}^N\left(
			\frac{y_i - y(x_i;\vec{a})}{\sigma_i}
	\right)^2,
\end{equation*}
where $y_i$ are data points from the measured spectrum,
$y(x_i;\vec{a})$ are values of the model at these data points and
$\sigma_i$ are standard deviations of measured points $y_i$.

If
\eqnref{minimization:chi_approx}
is a good approximation of the $\chi^2(\vec{a})$, the minimum can be found
directly by adjusting the currently estimated parameters $\vec{a}_\text{cur}$
by
\begin{equation}
	\vec{a}_\text{min} = \vec{a}_\text{cur}
		+ \mat{D}^{-1}\cdot(-\vec{\nabla}\chi^2(\vec{a}_\text{cur})).
	\label{\eqnlabel{minimization:newton_min}}
\end{equation}

On the other hand, if
\eqnref{minimization:chi_approx}
does not locally approximate the shape of $\chi^2(\vec{a})$ well, the steepest
descend method instead of the above described Newton method can be used for
convergence to the minimum
\begin{equation}
	a_\text{min} = a_\text{cur} - k\vec{\nabla}\chi^2(\vec{a_\text{cur}}),
	\label{\eqnlabel{minimization:gradient_min}}
\end{equation}
where $k$ needs to be a sufficiently small constant.

Let us calculate the partial derivatives needed in terms from
\eqnref{minimization:newton_min} and \eqnref{minimization:gradient_min}:
\begin{align*}
	\frac{\partial\chi^2}{\partial{}a_k} &= -2\sum_{i=1}^N{
		\frac{y_i - y(x_i;\vec{a})}{\sigma_i^2}
		\frac{\partial{}y(x_i;\vec{a})}{\partial{}a_k}
	},\\
	\frac{\partial^2\chi^2}{\partial{}a_k\partial{}a_l} &= 2\sum_{i=1}^N{
		\frac{1}{\sigma_i^2}\left[
			\frac{\partial{}y(x_i;\vec{a})}{\partial{}a_k}
			\frac{\partial{}y(x_i;\vec{a})}{\partial{}a_l}
			- [y_i - y(x_i;\vec{a})]
				\frac{\partial^2{}y(x_i;\vec{a})}{\partial{}a_k\partial{}a_l}
		\right]
	},
\end{align*}
where the second term in the second equation can be neglected because the
summation over all spectral points $y_i$ is equal to 0 if the
$y(x_i;\vec{a})$ models $y_i$ well. Therefore the simplified formula for
Hessian can be written as
\begin{equation*}
	\frac{\partial^2\chi^2}{\partial{}a_k\partial{}a_l} = 2\sum_{i=1}^N{
		\frac{1}{\sigma_i^2}
		\frac{\partial{}y(x_i;\vec{a})}{\partial{}a_k}
		\frac{\partial{}y(x_i;\vec{a})}{\partial{}a_l}
	}.
\end{equation*}
It can be seen that this approximation of Hessian does not need second
derivatives of $\chi^2(\vec{a})$ and can be written as the product of two
Jacobians $\mat{J}$
\begin{align*}
	\mat{J} &=
		\begin{pmatrix}
			\frac{\partial{}y(x_1;\vec{a})}{\partial{}a_1} & \cdots & \frac{\partial{}y(x_1;\vec{a})}{\partial{}a_M} \\
			\vdots                                         & \ddots & \vdots \\
			\frac{\partial{}y(x_N;\vec{a})}{\partial{}a_1} & \cdots & \frac{\partial{}y(x_N;\vec{a})}{\partial{}a_M}
		\end{pmatrix}\\
	\mat{W} &=
		\begin{pmatrix}
			\frac{1}{\sigma_1^2} \\
			& \ddots \\
			&& \frac{1}{\sigma_N^2}
		\end{pmatrix}\\
	[\alpha] &= \mat{J}^\mathsf{T}(\mat{W}\mat{J}).
\end{align*}

Let us introduce substitution by defining
\begin{align}
	\beta_k &\equiv -\frac{1}{2}\frac{\partial\chi^2}{\partial{}a_k}
		= \sum_{i=1}^N{
			\frac{y_i - y(x_i;\vec{a})}{\sigma_i^2}
			\frac{\partial{}y(x_i;\vec{a})}{\partial{}a_k}
		}
		\label{\eqnlabel{minimization:beta}}\\
	\alpha_{kl} &\equiv \frac{1}{2}
		\frac{\partial^2\chi^2}{\partial{}a_k\partial{}a_l}
		= \sum_{i=1}^N{
			\frac{1}{\sigma_i^2}
			\frac{\partial{}y(x_i;\vec{a})}{\partial{}a_k}
			\frac{\partial{}y(x_i;\vec{a})}{\partial{}a_l}
			\label{\eqnlabel{minimization:alpha}}
		}
\end{align}
and rewrite equations
\cref{%
	\eqnlabel{minimization:newton_min},%
	\eqnlabel{minimization:gradient_min}%
}
to forms which are defining the step towards the minimum $\text{\g{d}}a_l$
\begin{align}
	\sum_{l=1}^N\alpha_{kl}\text{\g{d}}a_l &= \beta_k,
	\label{\eqnlabel{minimization:newton_delta}}\\
	\text{\g{d}}a_l &= k\cdot\beta_l,
	\label{\eqnlabel{minimization:gradient_delta}}
\end{align}
where the matrix $[\alpha]$ which is equal to one-half times the Hessian
matrix, is usually called the \emph{curvature matrix}.

Marquardt combined the
\cref{%
	\eqnlabel{minimization:newton_delta},%
	\eqnlabel{minimization:gradient_delta}%
},
together in one equation
\begin{equation}
	\sum_{l=1}^N\alpha'_{kl}\text{\g{d}}a_l = \beta_k,
	\label{\eqnlabel{minimization:lm}}
\end{equation}
with
\begin{equation*}
	\alpha'_{kl} = \alpha_{kl}(1+\text{\g{d}}_{kl}\lambda),
\end{equation*}
where $\text{\g{d}}_{kl}$ is Kronecker delta (equal 1 if $k = l$ and 0
otherwise) and $\lambda$ is damping factor.
The new $\alpha'_{kl}$ is diagonally dominant with large $\lambda$ and behaves
like the gradient method and is equal to $\alpha_{kl}$ for small $\lambda$ and
behaves like the Newton method.

The only unknown is an estimation of the damping factor $\lambda$.
\textcite{Marquardt1963}
suggested a heuristic for its computation using an additional parameter
called multiplication factor $\mu$ and supposing initial guess of parameters $\vec{a}$
and $\lambda = \lambda_0$:
\begin{docenum}
	\item Estimate $\chi^2(\vec{a})$.
	\item Solve the \eqnref{minimization:lm} for $\text{\g{d}}\vec{a}$ and
		evaluate $\chi^2(\vec{a} + \text{\g{d}}\vec{a})$.
		\label{enum:minimization:lm_steps:item:solve}
	\item If $\chi^2(\vec{a} + \text{\g{d}}\vec{a}) \geq \chi^2(\vec{a})$ update
		$\lambda$ to $\lambda\cdot\mu$ and go to
		\cref{enum:minimization:lm_steps:item:solve}.
		It means that you are probably far from the minimum so switch from Newton
		to gradient method with large jump.
	\item If $\chi^2(\vec{a} + \text{\g{d}}\vec{a}) < \chi^2(\vec{a})$ update
		$\lambda$ to $\lambda/\mu$, update the trial solution $\vec{a}$ to
		$\vec{a} + \text{\g{d}}\vec{a}$ and go to
		\cref{enum:minimization:lm_steps:item:solve}.
		It means that you are probably near to the minimum so try to jump directly
		to the minimum approximated by quadratic function as per Newton method.
\end{docenum}

We enhanced the damping factor update algorithm for search in a wider range and
both directions because the above-proposed method was susceptible to falling
into a local minimum.
The items 3 and 4 update of lambda searched larger region of $\mu^k$ where,
for example, $k \in \{-10, -9, \dots, 9, 10\}$.
It was also found that it is more stable, especially for the cases with more
complicated models $y(x;\vec{a})$, to calculate Jacobian $\mat{J}$ numerically
than using analytic derivatives because of the numeric instability of the
solution.

This regression algorithm was programmed using the MATLAB programming
environment
\parencite{Matlab}
and used throughout many different applications in this work.
The algorithm was also modified so that it was possible to specify
different weight functions than least squares, for example, penalize positive
differences as was then used in
\cref{background}.
\\


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Written By Michael Brodskiy
% Class: Analytic Geometry & Calculus III (Math-292)
% Professor: V. Cherkassky
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\documentclass[12pt]{article}
\usepackage{alphalph}
\usepackage[utf8]{inputenc}
\usepackage[russian,english]{babel}
\usepackage{titling}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{enumitem}
\usepackage{amssymb}
\usepackage[super]{nth}
\usepackage{everysel}
\usepackage{ragged2e}
\usepackage{geometry}
\usepackage{fancyhdr}
\usepackage{cancel}
\geometry{top=1.0in,bottom=1.0in,left=1.0in,right=1.0in}
\newcommand{\subtitle}[1]{%
  \posttitle{%
    \par\end{center}
    \begin{center}\large#1\end{center}
    \vskip0.5em}%

}
\usepackage{hyperref}
\hypersetup{
colorlinks=true,
linkcolor=blue,
filecolor=magenta,
urlcolor=blue,
citecolor=blue,
}

\urlstyle{same}

\title{Lecture XXII Notes}
\date{\today}
\author{Michael Brodskiy\\ \small Professor: V. Cherkassky}

% Mathematical Operations:

% Sum: $$\sum_{n=a}^{b} f(x) $$
% Integral: $$\int_{lower}^{upper} f(x) dx$$
% Limit: $$\lim_{x\to\infty} f(x)$$

\begin{document}

\maketitle

\section{Green's Theorem $-$ 16.4}

For this theorem, let $C$ be a curve that is:

\begin{enumerate}

  \item positively oriented (moving counterclockwise)

  \item piecewise smooth

  \item simple

  \item closed

\end{enumerate}

$D$ is the region enclosed by the curve.

If $P$ and $Q$ have continuous first-order partial derivatives on $D$, then:

$$\int_C P\,dx+Q\,dy=\iint_D \left(\frac{\partial Q}{\partial x}-\frac{\partial P}{\partial y}\right)\,dA$$

For Green's Theorem, $D$ must have boundaries.

Notation (If $C$ is closed):

$$\int_C P\,dx+Q\,dy=\oint_C P\,dx+Q\,dy$$

If $\frac{\partial Q}{\partial x}-\frac{\partial P}{\partial y}=1$, then Green's theorem yields the area of the region $D$:

$$\iint_D 1\,dA=A(D)$$

\end{document}


\section{Attribute Grammars}

\subsection{Left-Right attributes}
Given a rule of type $A_0 \rarr A_1A_2\ldots$

\begin{description}
    \item[Left attribute] $\sigma_0 = f(\ldots)$ associated to $A_0$
    \item[Right attribute] $\delta_i = f(\ldots), i \ge 1$, associated to $A_i$
\end{description}

An attribute cannot be both left and right.

\subsection{Dependence Graph}

Given a rule $p$, its dependence graph $dep_p$ is a directed graph:
\begin{itemize}
    \item Nodes are the attributes
    \item There is an arc from every argument to the result
    \item Left attributes are on the left, right to the right
\end{itemize}

If the dependence graph is acyclic there exists a set of attribute values. A grammar is \textbf{loop-free} if the dependence graph is always acyclic.

\subsection{Depth-First visit}
\begin{itemize}
    \item Start from the root
    \item Visit the child nodes in some specified order
    \item Foreach subtree $t_N$ rooted at node $N$ :
    \begin{itemize}
        \item Before visiting $t_N$ compute the right attributes of $N$ and pass them down to the visit
        \item After visiting $t_N$ compute the left attributes using the return value of the visit
    \end{itemize}
\end{itemize}

\subsection{Depth-First Visit Conditions (one-sweep)}
\begin{itemize}
    \item The graph $dep_p$ has no circuit
    \item In $dep_p$ there exists no path from a left attribute to a right attribute of the same node
    \item In $dep_p$ there exists no arc from a left attribute of the father to a right attribute of any child
    \item There are no circuits in $sibl_p$ graph
\end{itemize}

There is an arc $D_i \rarr D_j$ in $sibl_p$ iff in $dep_p$ there is an arc $\alpha_i \rarr \beta_j$ with $\alpha_i \in \text{attr}(D_i)$ and $\beta_j \in \text{attr}(D_j)$.

\subsection{One-Sweep Evaluator}
There is a procedure for each nonterminal, its parameters are the subtree rooted at the nonterminal and the right attributes of the root; it visits the subtree and returns the left attributes of the root.

For the rule $p: D_0 \rarr D_1D_2\ldots D_r$
\begin{itemize}
    \item Choose a Topological Order of Siblings $TOS$ compatible with $sibl_p$
    \item For each symbol $D_i, i>0$ choose a topological order of the right attributes $TOR$
    \item Choose a topological order of the left attributes of $D_0$
\end{itemize}

\subsection{Condition L}
Used to embed semantic evaluator inside recursive descent syntax parser.
\begin{itemize}
    \item Support syntax is of type LL(k)
    \item Depth-First Visit Conditions
    \item The natural order ($D_1, D_2, \ldots$) is a valid TOS is $sibl_p$ (i.e. no arc $D_j\rarr D_i$ with $j>i$)
\end{itemize}


\subsection{States, messages, representatives}

The full-node protocol states are {\em sets} of messages,
each message being a triple $(c, v, j)$, where:
\begin{itemize}
    \item $c$ is a (proposed) consensus value;
    \item $v$ identifies the message sender;
    \item $j$, the justification, is the {\em protocol state} seen by the sender
        at the time of message sending.
\end{itemize}

There are two technical difficulties with the above definition:
\begin{itemize}
    \item States and messages are mutually recursive: states are sets of messages, each containing a state;
    \item Message ordering should not matter
\end{itemize}

To solve the first issue, we choose to postpone the definition of messages for now and first define states inductively as follows:

\begin{coq}
Inductive state : Type :=
  | Empty : state
  | Next : C ->  V -> state -> state -> state.
\end{coq}

This definition says that a state can be built by extending an existing state
given a consensus value \verb|C|, a validator \verb|V|, and another state representing
the justification. To clarify that the three are the components of a message, we introduce the following notation for \verb"Next":

\begin{coq}
Notation "'add' ( c , v , j ) 'to' sigma" :=
  (Next c v j sigma)
  (at level 20).
\end{coq}

This definition addresses the first difficulty mentioned above by capturing the recursive nature of states.

The second issue critical to the notion of state equality. Defining an equivalence between states which disregards message ordering is possible but non-trivial: the definition would itself need to be recursive, as it requires an equivalence
on messages, which in turn is defined in terms of the same state equivalence.

To circumvent the hindrance of working with mutually recursive state equality, we instead took the approach of using canonical representatives for states,
which we call LocallySorted states, on which we express state equality as syntactic equality in Coq.  Although this definition is still recursive, it is easier to express and to work with than with the former equivalence because it is defined in terms of a single state.

To define sorted states, we need to be able to compare messages,
which amounts to defining a total order relation on states.
This can be defined as a lexicographic ordering on the state seen as a list
of messages, tweaked slightly to perform recursion in order to compare justifications:
\begin{coq}
Fixpoint state_compare (sigma1 sigma2 : state) : comparison :=
  match sigma1, sigma2 with
  | Empty, Empty => Eq
  | Empty, _ => Lt
  | _, Empty => Gt
  | add (c1, v1, j1) to sigma1, add (c2, v2, j2) to sigma2 =>
    match compare c1 c2 with
    | Eq =>
      match compare v1 v2 with
      | Eq =>
        match state_compare j1 j2 with
        | Eq => state_compare sigma1 sigma2
        | cmp_j => cmp_j
        end
      | cmp_v => cmp_v
      end
    | cmp_c => cmp_c
    end
  end.
\end{coq}

Note that defining this ordering requires that there exist orderings on
consensus values and validators, but these can be any orderings,
and a total ordering is guaranteed to exists for any set in set theory
by the axiom of choice \cite{Gonzalez}. The existence of these orderings is reflected in our \verb|StrictlyComparable| types for consensus values and validators.

This ordering naturally induces an ordering on messages, and thus allows us
to define the notion of a \verb|LocallySorted| state, i.e., one in which
each message is smaller than the next and all justifications are themselves
\verb|LocallySorted|.

Choosing \verb|LocallySorted| states as representatives for states thus reducing the equality testing between states to syntactic equality checking.

Protocol states are defined by means of an inductive predicate on states as follows:
\begin{coq}
Inductive protocol_state : state -> Prop :=
  | protocol_state_empty : protocol_state Empty
  | protocol_state_next
         : forall s j
         , protocol_state s
        -> protocol_state j
        -> incl_messages j s
        -> forall c v
         , valid_estimate c j
        -> not_heavy (add_in_sorted_fn (c,v,j) s)
        -> protocol_state (add_in_sorted_fn (c,v,j) s).
\end{coq}

The above definition reads as:
\begin{itemize}
    \item a protocol state is either empty; or
    \item it can be obtained from an existing protocol state $s$ by extending
        it with a message $(c, v, j)$ such that:
        \begin{itemize}
            \item $j$ is a protocol state included in $s$;
            \item $c$ is a consensus value which the estimator agrees on for $j$;
            \item adding $(c,v,j)$ to $s$ does not exceed the fault tolerance threshold.
        \end{itemize}
\end{itemize}


\graphicspath{ {./img/TheFEM/} }
\chapter{Review of Boundary Value Problems}

\section{Boundary value problems}
In this section we will define a general initial boundary value problem (I-BVP). In the first part we will introduce the differential formulation given in terms of a set of governing equations and properly specified boundary conditions. The resulting equations are obtained after using a generalized balance law. Following this classical and well known approach we formally re-state these equations in the so-called strong form. Subsequently we re-write and prove an equivalent form of the balance law in the form of an integral representation highly friendly for a numerical solution. Since in the integral description of the problem the order of the derivatives in the field functions decreases by one, the resulting statement is called a weak formulation.

\subsection{Differential formulation-Generalized balance law}
Let $\dd{S}$ be a differential surface element, $\dd{V}$ a differential volume element, $u(\vb{x},t)$ a scalar (or vector) function of space and time. The flux or rate of flow of the quantity $u(\vb x, t)$ through $\dd{S}$ at time $t$ is defined like
\[p(\vb x)\grad u \cdot \hat n \dd{S} \enspace ,\]
where $p(\vb x)$ is a positive function, assumed known and time independent. Similarly, the time rate of change of $u(\vb x, t)$ in an element $\dd{V}$ is given by
\[\rho (\vb x)\pdv{u}{t} \dd{V} \enspace ,\]
where once again $\rho (\vb x)$ is a known, given, time independent positive function. Additional effects occurring in the element $\dd{V}$ at the time time can be expressed like
\[H(\vb x, t)\dd{V} \equiv  - q(\vb x)u(\vb x, t) + \hat F(\vb x,t)\]
where $\hat F(\vb x, t) = \rho (\vb x)F(\vb x, t)$. In the above the term $q u$ represent internal effects due to changes proportional to $u$ while $\hat F(\vb x, t)$ are other external influences in the medium.

Balancing the internal and external changes yields
\[\int\limits_V \rho (\vb x)\pdv{u}{t}\dd{V} = \int\limits_S p(\vb x)\vb \grad u \cdot \hat n\dd{S}  + \int\limits_V H(\vb x, t)\dd{V} \enspace ,\]
or equivalently
\[\int\limits_V \rho (\vb x)\pdv{u}{t}\dd{V} = \int\limits_S p(\vb x)\vb \grad u \cdot \hat n\dd{S}   - \int\limits_V q(\vb x)u(\vb x, t)\dd{V}  + \int\limits_V \rho (\vb x)F(\vb x, t)\dd{V} \enspace .\]

Using the divergence theorem as
\[\int\limits_S p(\vb x) \grad u \cdot \hat n \dd{S}  = \int\limits_V \vb \div  (p\vb \grad u)\dd{V}\]
yields after substitution
\[\int\limits_V \left[\rho (\vb x)\pdv{u}{t} - \div (p(\vb x)\grad u) + q(\vb x)u(\vb x, t) - \rho (\vb x)F(\vb x, t)\right]\dd{V}  = 0\]
Assuming a continuous integrand, the arbitrariness of $V$ implies
\[\rho (\vb x)\pdv{u}{t} - \div(p(\vb x)\grad u) + q(\vb x)u(\vb x, t) - \rho (\vb x)F(\vb x, t) = 0\]

Letting
\[\mathcal{L} \equiv  - \div p(\vb x)\grad + q(\vb x)\]
the generalized set of partial differential equations can be written like
\begin{equation}
\rho(\vb x) \pdv{u(\vb x, t)}{t} + \mathcal{L}u(\vb x, t) = \rho (\vb x)F(\vb x, t) \enspace .
\label{eq:GenPDE}
\end{equation}

And they are categorized as:
\begin{itemize}
    \item Hyperbolic;
    \[\rho(\vb x) \pdv[2]{u(\vb x, t)}{t} + \mathcal{L}u(\vb x,t) = 0\]

    \item Parabolic; and
    \[\rho(\vb x) \pdv{u(\vb x,t)}{t} + \mathcal{L}u(\vb x, t) = 0\]

    \item Elliptic
    \[\mathcal{L}u(\vb x, t) = \rho (\vb x)F(\vb x, t) \enspace .\]
\end{itemize}

It can be shown that $\mathcal{L}$ satisfies the \emph{symmetry} condition
\[\int\limits_V \mathcal{L}(u)v\dd{V} =  \int\limits_V \mathcal{L}(v)u \dd{V}\]
and positive definiteness \cite{book:sepulveda_fismat, book:arfken}
\[\int\limits_V \mathcal{L}(u)u\dd{V}  > 0, \quad \forall u \enspace .\]

\subsection{Strong form}
Given $\rho(\vb x)$, $q(\vb x)$, $p(\vb x)$, $F(\vb x, t)$ and $\bar u$ find $u(\vb x , t):V \to \mathbb{R}$ such:
\[\rho(\vb x) \pdv{u}{t} - \div \left[p(\vb x) \grad u\right] + q(\vb x) u(\vb x, t) - \rho(\vb x) F(\vb x, t) = 0 \quad \forall \vb x \in V \]
and
\begin{align*}
    &u = \bar u \quad \forall \vb x \in S_u\\
    &p(\vb x)u_{,i} \hat n_i= B(\vb x,t)\quad \forall \vb x \in  S_t \enspace .
\end{align*}

In the FEM we will look for approximate solutions to $u$ subject to the following conditions:
\[u = \bar u \quad \forall \vb x \in S_u \quad \text{(Essential boundary conditions)}\]
and
\[\int\limits_S \left(\pdv{u}{x_j}\right)^2 \dd{S} < \infty \enspace ,\]
which corresponds to the functions being square integrable. We will denote this space by $\mathbb{H}$ .

The space of functions satisfying the above two conditions will be denoted by $\zeta$ and termed the space of trial functions, formally defined like:
\[\zeta  = \left\{ u \mid u \in \mathbb{H},u = \bar u \quad \forall\vb x  \in S_u \right\}\]

On the other hand, to validate (or test) the correctness of the approximated or proposed trial functions $u$ it is also necessary to introduce test functions $w$ which are arbitrary except that they satisfy the following conditions:
\[w = 0\quad \forall \vb x \in S_u\]
and
\[\int\limits_S \left(\pdv{w}{x_j}\right)^2 \dd{S} < \infty \enspace ,\]
which corresponds to the functions being square integrable. The space of functions satisfying the above two conditions will be denoted by $\pounds$ and termed the space of test functions, formally defined like:
\[\pounds  = \left\{w \mid w \in \mathbb{H},w = 0 \quad \forall \vb x \in S_u\right\}\]

\subsection{Weak form}
Given $\rho(\vb x)$, $q(\vb x)$, $p(\vb x)$, $F(\vb x, t)$ and $\bar u$ find $u(\vb x , t):V \to \mathbb{R}$ and $\forall w \in \pounds$ such:
\begin{align*}
\int\limits_V p(\vb x) u_{,i}\, w_{,i}\dd{V} - \int\limits_{S_t} B(\vb x, t)w \dd{S}  + \int\limits_V q(\vb x)u(\vb x, t)w\dd{V}  &+ \int\limits_V \rho(\vb x)\pdv{u}{t}w \dd{V} \\& - \int\limits_V \rho(\vb x) F(\vb x, t)w\dd{V} = 0
\end{align*}
and
\[u = \bar u\quad \forall\vb x \in S_u \enspace .\]

\subsection{Equivalence between the strong and weak forms}
\begin{multline}
    -\int\limits_V [p(\vb x)u_{,i}]_{,i}w \dd{V} + \int\limits_{S_t} [p(\vb x)u_{,i}] \hat n_i w\dd{S} - \int\limits_{S_t} B(\vb x, t)w\dd{S}\\
    + \int\limits_V q(\vb x)u(\vb x, t)w \dd{V}  + \int\limits_V \rho(\vec x)\pdv{u}{t}w\dd{V} - \int\limits_V \rho(\vb x)F(\vb x, t)w\dd{V} = 0
\end{multline}

Grouping together common terms yields
\begin{align*}
&\int\limits_V \left\{\rho(\vb x)\pdv{u}{t} - [p(\vb x)u_{,i}]_{,i} + q(\vb x)u(\vb x, t) - \rho(\vb x)F(\vb x, t)\right\} w\dd{V}\\
&+ \int \limits_{S_t} \left\{[p(\vb x)u_{,i}]\hat n_i - B(\vb x, t) \right\} w\dd{S} = 0
\end{align*}
from which
\[\rho(\vb x)\pdv{u}{t} - [p(\vb x)u_{,i}]_{,i} + q(\vb x)u(\vb x, t) - \rho(\vb x)F(\vb x, t) = 0\]
and
\[p(\vb x)u_{,i}\hat n_i = B(\vb x, t)  \quad\forall \vb x \in S_t \enspace .\]

\section{Brief review of the linearized theory of elasticity model}
Here we present a brief description of the boundary value problem governing the response of an elastic body. For a full discussion of the model and its mathematical aspects the reader is referred to \cite{shames1997elastic}.

The governing equations (in terms of stresses) stem from the principle of conservation of linear momentum and conservation of moment of linear momentum. The former leads to a set of 3 partial differential equations in the components of the stress tensor while the latter leads to the symmetries in the stress tensor.
\begin{equation} \label{eq:pde}
\begin{aligned}
&\sigma_{ij,j} + {f_i} = \rho\ddot{u}_i \quad \forall\ \vb{x} \in V,\, t \in \mathbb{R}^{+}\\
&\sigma_{ij}=\sigma _{ji}.
\end{aligned}
\end{equation}

In \cref{eq:pde} $\sigma_{ij}$ is the stress tensor; $f_i$ is the vector of body forces; and $u_i$ is the displacements vector.

Denoting the tractions vector associated with a surface with normal direction $\hat{n}_{j}$ by $t_i^{\hat n}$ we have the complete BVP as follows
\begin{equation} \label{eq:bcs}
t_i^{\hat n} = \sigma_{ij} \hat{n}_{j} \quad \forall \in \vb{x} \in S.
\end{equation}

\Cref{eq:pde} correspond to 6 equations with 12 unknowns (the 9 components of the stress tensor and the 3 components of the displacements vector ) and the system is undetermined. In order to have a solvable BVP we must introduce kinematic strain-displacement relations and a stress-strain law. In the case of infinitesimal theory of elasticity the strain-displacement relation is given by:

\begin{equation}\label{eq:kin}
\varepsilon_{ij} = \frac{1}{2}(u_{i,j} + u_{j,i})
\end{equation}

where the term $\epsilon_{ij}$ is the symmetric component of the displacements gradient tensor. The components of the strain tensor describe the distortions and changes in magnitude (volumetric changes) of the material point in the continuum model. Now the simplest stress-strain (constitutive) relationship is given by Hooke's law\footnote{Despite the name of \emph{law} used, this relation is not always valid, but is a good approximation for small strains.}
\begin{equation} \label{eq:Hooke}
\sigma_{ij} = 2\mu \varepsilon_{ij} + \lambda \varepsilon_{kk}\delta_{ij} \enspace .
\end{equation}

where $\mu$ and $\lambda$ are material constants. The problem involves now a total of 18 equations and 18 unknowns can be solved if subjected to properly specified boundary conditions.

\subsection*{Displacement formulation}
Substituting \cref{eq:kin} in \cref{eq:Hooke} and the result in \cref{eq:pde} yields after some manipulation:
\begin{equation} \label{eq:navier}
(\lambda  + \mu)u_{j,ij} + \mu u_{i,jj} + {f_i} = \rho \ddot{u}_i \quad \forall \vb{x} \in V,\, t \in \mathbb{R}^{+}.
\end{equation}

Since \cref{eq:navier} ( simultaneously describing equilibrium, kinematic relations and constitutive response) is a second order equation governing the displacement field possible boundary conditions are in terms of the variable itself or its first oder derivatives. For a well-possed problem the following is a set of valid boundary conditions:
\begin{equation}\label{eq:Wellbcs}
\begin{split}
&t_i^{\hat n} = \sigma _{ij} \hat n_{ij} \quad \forall\ \vb{x} \in S_t\\
& {u_i} = \bar{u}_i \quad \forall \vb x \in S_u
\end{split}
\end{equation}

and where ${S_t} \cup {S_u} = S$ and ${S_t} \cap {S_u} = \emptyset $.

In the particular case in which $u_i$ is not a function of time, we obtain the static version of the BVP, i.e,
\begin{equation}
\begin{split}
&\left(\lambda  + \mu \right)u_{j,ij} + \mu u_{i,jj} + {f_i} = 0 \quad \forall \vb{x} \in V \\
&t_i^{\hat n} = \sigma _{ij} \hat n_{ij} \quad \forall\ \vb{x} \in S_t\\
& {u_i} = \bar{u}_i \quad \forall \vb x \in S_u
\end{split}
\end{equation}

Notice that the tractions BC
\[t_i^{\hat n} = \mu (u_{i,j} + u_{j,i}) \hat{n}_j + \lambda u_{k,k} \delta_{ij}\hat{n}_j \enspace ,\]

actually involves first order displacements derivative and as such it is a Neumann boundary condition on $u_i$.

\subsection{Equivalence between strong and weak forms}
\subsubsection{Strong form}
The strong form corresponds to the differential formulation of the problem, it is denoted by $\{ S \}$ and it reads:

Given $f_i$, $t_i^{\hat n}$ and ${\bar u_i}$ find ${u_i}:V \to \mathbb{R}$ such:
\begin{equation} \label{eq:navier_2}
\begin{split}
&(\lambda  + \mu)u_{j,ij} + \mu u_{i,jj} + f_i = 0 \quad \forall \vb{x} \in V \\
&t_i^{\hat n} = \sigma _{ij} \hat{n}_{ij} \quad \forall \vb{x} \in S_t\\
&u_i = \bar{u}_i \quad \forall \vb{x} \in S_u
\end{split}
\end{equation}

In \cref{eq:navier_2} the boundary conditions specified by the traction vector $t_i^{\hat n}$ correspond to the natural boundary conditions, while those specified in terms of the displacements vector $\bar u_i$ represent the essential boundary conditions.

\begin{itemize}
\item We are interested in developing methods to obtain approximate solutions to $\{S\}$.
\item The FEM is formulated starting from a statement equivalent to $\{ S \}$ in which we use trial functions until certain prescribed conditions are met.
\item We will look for solutions $u_i$ subject to the following conditions:
\begin{align*}
&u_i = \bar u_i \qquad \text{in} \qquad S_u\\
&\intL_S \left(\pdv{u_i}{x_j} \right)^2 \dd{S} < \infty
\end{align*}

\end{itemize}

The first condition corresponds to the satisfaction of the essential boundary condition, while the second corresponds to the functions being square integrable. The space of functions satisfying the above two conditions is denoted by $\varsigma$ and formally defined like
\[\varsigma = \left\{u_i\left| {u_i} \in H, {u_i} = \bar{u}_i \in S_u \right. \right\} \enspace .\]

On the other hand, in order to validate the introduced trial functions we also need testing functions $w_i$ also called in the FEM literature weighting or distribution functions. These functions are arbitrary apart from having to satisfy the following conditions:
\begin{align*}
&w_i = 0 \quad in \quad {S_u}\\
&\intL_S \left(\pdv{w_i}{x_j}\right)^2 \dd{S} < \infty
\end{align*}

In what follows we formally denote the space of these functions by $V$ and define it like
\[V = \left\{ w_i\left| w_i \in H, u_i = w_i=0 \in S_u \right. \right\} \enspace .\]

\subsubsection{Weak form}
Here we will show that the equilibrium statement represented in the differential formulation can be described in alternative forms. In such description the continuity requirement for the trial functions is weaker than in the strong form leading to the term "weak" statement. Here this alternative representation will be denoted like $\{W\}$ and it reads;

Given $f_i$, $t_i^{\hat n}$ and ${\bar u_i}$ find ${u_i}:V \to \mathbb{R}$ and $\forall {w_i} \in V$ such:
\[\intL_V \sigma_{ij} w_{i,j}\, \dd{V} - \intL_V f_i w_i\, dV  - \intL_{S_t} t_i^{\hat n} w_i\, \dd{S} = 0\]

\subsubsection*{Proof 1:}
Let $u_i \in \varsigma $ be a solution to $\{S\}$ and let $w_i \in V $. Forming the inner product of the equilibrium statement given in \cref{eq:pde} with $w_i$ and forcing the integral over the domain to be zero we have
\[\intL_V (\sigma_{ij,j} + f_i ){w_i}\, dV = 0 \enspace ,\]
expanding the terms in the integrand and integrating by parts the first term on the left we have
\begin{align*}
&\intL_V \sigma _{ij,j} w_i\, \dd{V} + \intL_V f_i w_i\, \dd{V} = 0 \, ,\\
& -\intL_V w_{i,j} \sigma _{ij}\, \dd{V}  + \intL_S \sigma _{ij} \hat{n}_j w_i\, \dd{S}  + \intL\limits_V w_i f_i\, \dd{V} = 0\, ,
\end{align*}
since $w_i \in V$ it follows that $w_i = 0$ in $S_u$ from which
\begin{equation}\label{eq:weak}
\intL_V \sigma_{ij} w_{i,j}\, \dd{V} - \intL_V f_i w_i\, \dd{V}  - \intL_{S_t} t_i^{\hat n} w_i\, \dd{S} = 0\, .
\end{equation}

Now, considering that $u_i$ is solution of the strong form $\{S\}$ it must satisfy $u_i = \bar u_{i} \quad \in \quad S_u$ and as a result $u_i \in \varsigma$. On the other hand, since $u_i$ satisfies \cref{eq:weak} $\forall {w_i} \in V$ we have that $u_i$ satisfies the definition of weak solution specified in $\{ W \}$.

\subsubsection*{Proof 2:}
Let $u_i$ be a solution of $\{W\}$ and thus $u_i \in \varsigma$ which means that
\[u_i = \bar u_{i} \quad \in \quad S_u\]
and that it satisfies
\[\intL_V \sigma _{ij} w_{i,j}\, \dd{V} - \intL_V f_i w_i\, \dd{V} - \intL_{S_t} t_i^n w_i\, \dd{S} = 0 \enspace ,\]
integrating by parts,
\[-\intL_V \sigma_{ij,j} w_i \dd{V} + \intL_S \sigma_{ij} n_j w_i \dd{S}  - \intL_V f_i w_i \dd{V} - \intL_{S_t} {t_i^n} w_i \dd{S} = 0\]

Since $w_i \in V$ we have that $w_i=0$ in $S_u$ and therefore
\[\intL_V w_i(\sigma_{ij,j} + f_i) \dd{V} + \intL_{S_t} w_i( \sigma_{ij} n_j - t_i^n ) \dd{S} = 0 \]
from which
\begin{equation} \label{equil_2}
\begin{split}
&\sigma_{ij,j} + f_i = 0 \quad \vb{x} \in V \\
&t_i^n = \sigma_{ij} n_j \quad \forall \vb{x} \in S_t\\
&{u_i} = \bar{u}_i \quad \forall \vb{x} \in S_u
\end{split}
\end{equation}

which is once again the strong form of the problem given in \cref{eq:pde}.

\subsection{Simple wedge under self-equilibrated loads}
Consider the double wedge of side $\ell$ and internal angle $2 \phi$ shown in \cref{fig:WEDGE}. It is assumed to be contained in the $X-Y$ plane, with loading conditions satisfying a plane strain (or plane stress) idealization. The material is elastic with Lame constants $\lambda$ and $\mu$. The wedge is loaded by uniform tractions of intensity $S$ applied over its four faces in such a way that the wedge is self-equilibrated. We wish to find the closed-form elasticity solution for the stress, strain and displacement fields throughout the problem domain.
\begin{figure}[h]
\centering
\includegraphics[width=7cm]{wedge.pdf}
\caption{2D Self-equilibrated wedge.}
\label{fig:WEDGE}
\end{figure}

Under plane strain conditions the general 3D stress equilibrium equations (see \cref{eq:pde}) reduce to:
\begin{equation}
\begin{aligned}
&\pdv{\sigma_{xx}}{x} + \pdv{\tau_{xy}}{y}=0\\
&\pdv{\tau_{xy}}{x} + \pdv{\sigma_{yy}}{y}=0
\end{aligned}
\label{eq:equilibrium}
\end{equation}
while the kinematic relation (\cref{eq:kin}) reads
\begin{equation}
\begin{aligned}
\epsilon_{xx}& = \pdv{u}{x}\\
\epsilon_{yy}& = \pdv{v}{y}\\
\gamma_{xy}& = \pdv{u}{y} + \pdv{v}{x}
\end{aligned}
\label{eq:strain}
\end{equation}
where $u$ and $v$ are the horizontal and vertical displacements respectively.

\subsubsection*{Stress field}

The stress field can be obtained by simple inspection from the traction boundary conditions prescribed over the inclined surfaces yielding
\begin{align*}
\sum F_x &= 0 \longrightarrow - \ell S\cos(\phi)  + \sigma_{xx}\ell \sin(\phi) = 0\\
\sum F_y &= 0 \longrightarrow - \ell S\sin(\phi) - \sigma_{yy}\ell \cos(\phi)=0
\end{align*}
and the following stress solution:
\begin{equation}
\begin{aligned}
\sigma_{xx}& = S \cot(\phi)\\
\sigma_{yy}& = -S\tan(\phi)\\
\tau_{xy}& = 0\, .
\end{aligned}
\label{eq:solution}
\end{equation}

In \cref{eq:solution} the condition $\tau_{xy}=0$ is due to the symmetries in the problem.

\subsubsection*{Traction boundary conditions}
Let us verify that the above stress solution satisfies the traction BC using the expression:
\[t_i^{\hat n} = \sigma_{ij} \hat n_{ij}.\]

Denoting the outward normals to the inclined surfaces of the wedge by $\hat{n}^1$,  $\hat{n}^2$, $\hat{n}^3$, $\hat{n}^4$ these are given by:
\begin{align*}
\hat{n}^1 &= -\sin(\phi)\hat{e}_{x}+\cos(\phi)\hat{e}_{y}\\
\hat{n}^2 &= -\sin(\phi)\hat{e}_{x}-\cos(\phi)\hat{e}_{y}\\
\hat{n}^3 &= +\sin(\phi)\hat{e}_{x}+\cos(\phi)\hat{e}_{y}\\
\hat{n}^4 &= +\sin(\phi)\hat{e}_{x}-\cos(\phi)\hat{e}_{y} \enspace ,
\end{align*}
where $\hat{e}_{x}$ and $\hat{e}_{y}$ are the reference unit vectors. Now, the components of the traction vector follow directly like
\[t_i = \sigma_{ij}\hat{n}_j\]
then over the face with normal $\hat{n}^1$ we have
\begin{align*}
t_{x} &= -S\cos(\phi)\\
t_{y} &= -S\sin(\phi)
\end{align*}
similarly, over the face with normal $\hat{n}^2$
\begin{align*}
t_{x} &= -S\cos(\phi)\\
t_{y} &= +S\sin(\phi)
\end{align*}
over the face with normal $\hat{n}^3$
\begin{align*}
t_{x} &= +S\cos(\phi)\\
t_{y} &= -S\sin(\phi)
\end{align*}
and finally, over the face with normal $\hat{n}^4$;
\begin{align*}
t_{x} &= +S\cos(\phi)\\
t_{y} &= +S\sin(\phi) \enspace .
\end{align*}

\subsubsection*{Strain field}
The strain field can be obtained after using the stress solution found in \cref{eq:solution} together with the constitutive law given by \cref{eq:Hooke} which for a plane strain idealization takes the form:
\begin{equation}
\begin{aligned}
\epsilon_{xx}& = \frac{1}{E}(\sigma_{xx} - \nu \sigma_{yy}) \\
\epsilon_{yy}& = \frac{1}{E}(\sigma_{yy} - \nu \sigma_{xx}) \\
\gamma_{xy}& = \frac{\tau _{xy}}{\mu}
\end{aligned}
\label{eq:cons model}
\end{equation}
which for the particular case yields
\begin{equation}
\begin{aligned}
\epsilon_{xx}& = +\dfrac{S}{E}\left[\cot(\phi)+\nu \tan(\phi)\right] = +\dfrac{S}{E}K_{1}(\nu , \phi)\\
\epsilon_{yy}& = -\dfrac{S}{E}\left[\tan(\phi)+\nu \cot(\phi)\right] = -\dfrac{S}{E}K_{2}(\nu , \phi)\\
\gamma_{xy}& = 0.
\end{aligned}
\label{eq:strain part}
\end{equation}

\subsubsection*{Displacement field}
The displacement field is obtained after direct integration of the strains after using the fact that
\[\dd{u}_i = \epsilon_{ij} \dd{x}_j + \omega_{ij}\dd{x}_j\]
and the condition $\omega_{xy}=0$ also due to symmetries , as follows
\begin{align*}
u &= +\dfrac{S}{E} K_{1}(\nu , \phi)x + A\\
v &= -\dfrac{S}{E} K_{2}(\nu , \phi)y + B
\end{align*}
and where $A$ and $B$ are integration constants.

From the condition $u=0$ at $x=\ell\cos(\phi)$ we have that
\[A=-\dfrac{S}{E} K_{1}(\nu , \phi)\ell\cos(\phi)\]
then it follows that
\[u=\dfrac{S}{E} K_{1}(\nu , \phi)(x-\ell\cos(\phi)).\]

Similarly, from the condition $v=0$ at $y=0$ we have that $B=0$ from which
\[v=-\dfrac{S}{E} K_{2}(\nu , \phi)y\]

\subsection{Variational formulation}
In this section we formulate the boundary value problem using the approach of the calculus of variations in which the governing PDEs and boundary conditions are obtained after finding the minimum (or maximum) of a functional according to a variational principle\footnote{According to Wikipedia \cite{wiki:variational_principle}

\begin{quotation}
A variational principle is a scientific principle used within the calculus of variations, which develops general methods for finding functions which minimize or maximize the value of quantities that depend upon those functions. For example, to answer this question: ``What is the shape of a chain suspended at both ends?" we can use the variational principle that the shape must minimize the gravitational potential energy.

According to Cornelius Lanczos, any physical law which can be expressed as a variational principle describes an expression which is self-adjoint. These expressions are also called Hermitian. Such an expression describes an invariant under a Hermitian transformation.
\end{quotation}}.

We will see that the weak form (and therefore also the strong form) can be obtained alternatively through the process of finding extreme values for a functional. We will illustrate this idea for the general case of theory of elasticity and then we will present particular examples.

\subsubsection*{Some vague definitions in the calculus of variations}
In variational calculus a {\bf functional} can be understood as a ``function" having as independent variables or arguments a space of vector functions and producing as a result (or dependent variable) a scalar. For instance, in the particular case of the theory of elasticity such a ``function" corresponds to the total potential energy functional $\Pi$ given by;
\begin{equation}\label{eq:Potential}
    \Pi(u_i) = \frac{1}{2}\int\limits_V \sigma_{ij}\varepsilon_{ij}\dd{V}  - \int\limits_V f_i\, u_i\dd{V}  - \int\limits_S t_i^{(n)} u_i\dd{S}
\end{equation}
and where the first term in the left hand side corresponds to the internal strain energy, while the last two terms are the work done by the external body and traction forces. The above functional has as independent variables the displacement vector and its spatial derivatives. This is indicated by the presence of the displacement vector $u_i$ in the expression $\Pi(u_i)$.

In variational calculus we are interested in finding a function $u_i$ that renders the functional $\Pi$ a maximum or a minimum. In loose terms, the analogous to the differential operator in calculus of functions is now termed the variational operator $\var$ (i.e., $\var$ is analogous to $\pdv{x_i}$). As such $\var{\Pi}$ acts over the function $u_i$ and its derivatives as follows
\[\var{\Pi}  = \pdv{\Pi}{u_i}\var{u_i} + \pdv{\Pi}{\left( \pdv{u_i}{x_j}\right)} \delta\left(\pdv{u_i}{x_j}\right) + \cdots + \pdv{\Pi}{\left( \pdv{^{n}u_i}{x_j \cdots \partial x_k}\right)} \delta\left( \pdv{^{n}u_i}{x_j \cdots \partial x_k}\right) \enspace .\]

The following rules apply to the variational operator $\delta$:
\begin{itemize}
\item For functionals $\Pi$ and $\Phi$ it follows that \[\var(\Pi  + \Phi) = \var{\Pi}  + \var{\Phi}\]
\item For functionals $\Pi$ and $\Phi$ it follows that \[\var(\Pi \Phi ) = \var{\Pi} \Phi  + \Pi \var{\Phi}\]
\item For a functional $\Pi$ and an integer $n$ it follows that \[\var(\Pi^n) = n(\Pi^{n - 1})\var{\Pi}\]
\item For a functional $\Pi$ it follows that \[\var{\int \Pi \dd{x}}  = \int \var{\Pi} \dd{x} \]
\end{itemize}

If the variational operator is applied to the functional $\Pi(u_i)$ it produces functions or variations in $u_i$ which are arbitrary and such $\var{u_i} \in V$ and $\var{u_i} = 0$ in $S_u$.

To find an extreme function in the calculus of variations we proceed like in differential calculus. Here we compute the first variation of the functional $\var{\Pi}$ and solve the variational equation
\begin{equation}\label{eq:vareq}
    \delta \Pi  = 0
\end{equation}
in the unknown function $u_i$.

In the particular case of the total potential energy functional $\Pi$ this yields
\begin{equation}\label{eq:ClaPVW}
    \int\limits_V \sigma _{ij}\var{\varepsilon_{ij}}\dd{V}  - \int\limits_V f_i \var{u_i} \dd{V}  - \int\limits_{S_t} t_i^{(n)} \var{u_i}\dd{S}  = 0
\end{equation}
where we recognize the weak form of the BVP stated previously. It becomes evident that the functions $\delta {u_i}$ in \cref{eq:ClaPVW} play the role of the test functions $w_i$ introduced in the weak form. On the other hand, since we have already shown that the weak and strong forms are equivalent we conclude that having the functional and the essential boundary conditions is equivalent to having the strong form of the problem.

\subsubsection*{Principle of minimum potential energy}
In the theory of elasticity the total potential energy $\Pi$ is the result of adding the elastic strain energy which is stored in the body upon deformation and the potential energy (work) imparted to the body by the applied forces. The principle states that the body is in equilibrium when this total potential energy reaches a minimum. This is equivalent to stating that an equilibrium configuration is attained when an infinitesimal variation from the position of minimum potential energy involves null changes in energy. This implies the variational condition:
\begin{equation}\label{vareq2}
    \delta \Pi  = 0.
\end{equation}

The above principle leads to the so-called principle of virtual displacements stated as follows\footnote{See Bathe pp 156.}:
``The equilibrium of the body requires that for any compatible small virtual displacements satisfying the condition of being zero at $S_u$, imposed on the body in its state of equilibrium, the total internal virtual work is equal to the total external virtual work"
\[\int\limits_V \sigma _{ij}\var{\varepsilon_{ij}}\dd{V}  - \int\limits_V f_i\delta u_i\dd{V}  - \int\limits_{S_t} t_i^{(n)}\var{u_i}\dd{S}  = 0\]
where $\var{u_i}$ are the virtual displacements and $\var{ \varepsilon_{ij}}$ are the corresponding virtual strains.

Comparing the virtual work principle with the weak formulation given in \cref{eq:weak} we identify $\var{u_i}$ with the test functions $w_i$. As such the PVW takes the form of a powerful tool to test if a body is in equilibrium for a given solution (represented by the trial functions). In what follows we illustrate the use of the principle through some examples corresponding to problems in Bathe's textbook.

\subsubsection*{Problem\footnote{3.15 from Finite Element Procedures}}
Establish the differential equation of equilibrium of the problem shown and the boundary conditions. Determine whether the differential operator of the problem is symmetric and positive definite and prove your answer.\cite{book:bathe}
\begin{figure}[H]
    \centering
    \includegraphics[width=10cm]{{Bathe3.15}.pdf}
    \caption{Rod with varying cross-sectional area. The Young's modulus is E.}
    \label{fig:bathe3.15}
\end{figure}

\begin{align*}
\Pi &= \frac{1}{2}\int\limits_0^L \sigma_{xx}\varepsilon_{xx}A(x)\dd{x} + \frac{1}{2}ku_0^2 - R u_L \\
 &= \frac{1}{2}\int\limits_0^L EA(x)\left( \dv{u}{x}\right)^2 \dd{x} + \frac{1}{2}ku_0^2  - R u_L \enspace.
\end{align*}

The first variation for this functional is
\[\var{\Pi}  = \int\limits_0^L EA(x)\dv{u}{x}\dv{\var{u}}{x}\dd{x} + k u_0\var{u_0}  - R\var{u_L} \enspace .\]

If we integrate by parts, we obtain
\[\var{\Pi}  =  - \int\limits_0^L \dv{x}\left[EA(x)\dv{u}{x}\right]\var{u}\dd{x} + \left. EA(x)\dv{u}{x}\var{u} \right|_0^L + k{u_0}\var{u_0}  - R\var{u_L}\]
from which
\begin{align*}
&\dv{x}\left[EA(x)\dv{u}{x}\right] = 0\\
&\left. EA(x)\dv{u}{x}\right|_{x=0} = k u_0\\
&\left. EA(x)\dv{u}{x}\right|_{x=L} = R
\end{align*}

\subsubsection*{Problem: The Hu-Washizu Variational Principle\footnote{4.35 from Finite Element Procedures}}

Consider the Hu-Washizu functional:
\begin{equation}
\Pi^* = \Pi  - \intL_V \lambda_{ij}^\varepsilon (\varepsilon_{ij} - L_{ijk} u_k)\dd{V}  - \intL_{S_u} \lambda_i^u(u_i^{S_u} - \bar{ u}_i)\dd{S}
\label{eq:Hu}
\end{equation}
where
\begin{itemize}
\item $\Pi$: is the potential energy functional.
\item $L_{ijk}$ is a differential operator such $\varepsilon_{ij} = L_{ijk} u_k$.
\item $S_u$ surface where essential boundary conditions are prescribed.
\item $\lambda_{ij}^\varepsilon $ and $\lambda_i^u$ are Lagrange multipliers.
\end{itemize}

Using the condition $\var{\Pi} = 0$ derive for the interior of the body the equilibrium equations
\[\sigma_{ij,j} + f_i = 0\]
the strain-displacement relationship
\[\varepsilon_{ij} = L_{ijk} u_k\]
and the constitutive equation
\[\sigma_{ij} = C_{ijkl} \varepsilon_{kl}\]
and at the surface of the body the relation between the stress tensor and the applied tractions vector at $S_t$
\[t_i = \sigma_{ij} n_j\]
the relation between the stress tensor and the unknown tractions vector (or reactions) at $S_u$
\[t_i = \tilde{\sigma_{ij}} n_j\]
and the essential boundary condition at $S_u$
\[u_i = \tilde{u}_i\]
We want to determine the Euler equations resulting from the condition $\var{\Pi}^* = 0$. Applying the variational operator we have
\begin{equation}
\begin{aligned}
\var{\Pi}^*& = \var{\Pi}  - \intL_V \var{\lambda}_{ij}^{\varepsilon}  (\varepsilon_{ij} - L_{ijk} u_k)\dd{V}- \intL_V \lambda_{ij}^\varepsilon (\var{\varepsilon}_{ij} - L_{ijk}\var{u}_k)\dd{V} \\
&-\intL_V \var{\lambda}_i^u (u_i^{S_u} - \bar u_i)\dd{S} - \intL_V \lambda _i^u \var{u}_i^{S_u} \dd{S}
\end{aligned}
\end{equation}
and
\begin{equation}
\begin{aligned}
\var{\Pi}^* =& \intL_V C_{ijkl} \varepsilon_{kl} \var{ \varepsilon}_{ij}\dd{V} - \intL_{S_t} t_i \var{u_i} \dd{S}  - \intL_V f_i \var{u_i}\dd{V}\\
  &- \intL_V \lambda_{ij}^{\varepsilon} \var{\varepsilon}_{ij}\dd{V}  + \intL_V \lambda_{ij}^{\varepsilon} L_{ijk}\var{u_k}\dd{V} - \intL_V \delta \lambda_{ij}^\varepsilon (\varepsilon_{ij}\\
  &- L_{ijk} u_k)\dd{V} - \intL_S \var{\lambda}_i^u (u_i^{S_u} - \bar {u}_i)\dd{S} - \intL_{S_u} \lambda _i^u\var{u_i}\dd{S} = 0
\end{aligned}
\end{equation}
using
\[\intL_V (\lambda_{ij}^\varepsilon \var{u_i}){,_j}\dd{V} =  \intL_V \lambda_{ij}^\varepsilon \var{u}_{i,j}\dd{V}  + \intL_V \lambda_{ij,j}^\varepsilon \var{u_i}\dd{V} \]

In the above we can write
\begin{align*}
\intL_V \lambda_{ij}^\varepsilon L_{ijk}\var{u_k}\dd{V} & = \intL_V (\lambda_{ij}^\varepsilon \var{u_i})_{,j}\dd{V} - \intL_V \lambda_{ij,j}^\varepsilon \delta {u_i}\dd{V}\\
& = \intL_{S_t} \lambda_{ij}^\varepsilon \var{u_i}\hat {n}_j\dd{S}  - \intL_V \lambda_{ij,j}^\varepsilon \var{u_i}\dd{V}
\end{align*}
therefore
\begin{align*}
\delta \Pi^* =& \intL_V (C_{ijkl}\varepsilon_{kl} - \lambda_{ij}^\varepsilon) \var{\varepsilon}_{ij}\dd{V} - \intL_{S_t} t_i \var{u}_i\dd{S} - \intL_V f_i \var{u}_i\dd{V}\\
  &+ \intL_{S_t} \lambda_{ij}^\varepsilon \var{u_i}\hat{n}_j\dd{S}  - \intL_V \lambda_{ij,j}^\varepsilon \delta {u_i}\dd{V} - \intL_V \delta \lambda _{ij}^\varepsilon (\varepsilon_{ij}\\
  &- L_{ijk} u_k)\dd{V}  - \intL_{S_u} \var{\lambda}_i^u (u_i^{S_u} - \bar{u}_i)\dd{S} - \intL_{S_u} \lambda_i^u \var{u_i} \dd{S}  = 0\\
=& \intL_V (C_{ijkl}\varepsilon_{kl} - \lambda_{ij}^\varepsilon) \var{\varepsilon}_{ij}\dd{V}
+ \intL_{S_t} (\lambda_{ij}^\varepsilon \hat{n}_j - t_i)\delta {u_i}\dd{S}\\
  &- \intL_V (\lambda_{ij,j}^\varepsilon  + f_i) \var{u_i}\dd{V}
- \intL_V (\varepsilon_{ij}- L_{ijk} u_k) \var{\lambda} _{ij}^\varepsilon\dd{V}\\
  &- \intL_{S_u} (u_i^{S_u} - \bar{u}_i) \var{\lambda}_i^u\dd{S}  - \cancel{\intL_{S_u} \lambda _i^u \var{u}_i^{S_u}\dd{S}} = 0
\end{align*}

Now, imposing the conditions $\var{\varepsilon}_{ij} \neq 0$, $\var{\lambda}_{ij}^\varepsilon  \neq 0$, $var{u}_i \neq 0$ in $S_t$, $\var{u}_i \neq 0$ in $V$ and $\var{\lambda}_i^u \neq 0$ in $S_u$ we have
\begin{align}
&\lambda_{ij}^\varepsilon  = C_{ijkl} \varepsilon_{kl}\\
&\varepsilon_{ij} = L_{ijk} u_k\\
t_i &= \lambda_{ij}^\varepsilon \hat{n}_j\\
&\lambda_{ij,j}^\varepsilon  + {f_i} = 0\\
&u_i^{S_u} = \bar{u}_i \enspace .
\end{align}

\subsection{Weighted residual methods}
This section introduces the concept of residual or difference from zero in a differential equation once its solution is approximated. For that purpose we will take as prototype equation the one obtained as our general model of BVP (see \cref{eq:GenPDE}) and recalled here for completeness
\begin{equation}
\rho(\vb x) \pdv{u(\vb x, t)}{t} + \mathcal{L}u(\vb x, t) = \rho (\vb x)F(\vb x, t) \enspace .
\label{eq:GenPDE2}
\end{equation}

We will assume that the actual solution to the generalized BVP given by \cref{eq:GenPDE2} is approximated by $\tilde u(\vb{x})$ through a superposition like
\begin{equation}
\tilde u (\vb{x}) = {N^I}(\vb{x}){u^I}
\label{basicsuper}
\end{equation}
where ${N^I}(\vb{x})$ are interpolating functions and $I$ denotes a superposition index  varying like $I=1,2,...,K$ with $K$ being the number of points where the solution is known. In what follows we will use $u(\vb{x})$ instead of $\tilde u (\vb{x})$ but will keep in mind that we are actually using the approximation given by \cref{basicsuper}. Similarly, in order to keep the discussion simple for the time being we will drop the time effects reducing the generalized PDE to the simple form:
\begin{equation}
\mathcal{L}u(\vb x) = \rho (\vb x)F(\vb x) \enspace .
\label{eq:GenPDE3}
\end{equation}

Now, since we are using the approximation given by \cref{basicsuper} this equation is not strictly satisifed but instead we will have the following ``unbalanced" condition
\[\mathcal{L}u(\vb{x}) - \rho (\vb{x})F(\vb{x}) \equiv R \ne 0\]
where the term $R$ corresponds to a residual error which is to be distributed throughout the solution domain. The so-called weighted residual methods differ in the form in which they distribute the residual between the different $K$ points conforming the computational domain.

Using \cref{basicsuper} in \cref{eq:GenPDE2} and the linearity in the differential operator yields
\[R = \mathcal{L}({N^P}){u^P} - \rho F\, .\]

We can see that the residual $R$ is a function defined over the domain of interest. The residual would be exactly zero for the solution of the differential equation, but it will not be zero in general. Thus, we want to make the function $R$ as close to zero as possible. To make $R$ as small as possible we need a function (a functional) where we can compare different approximation functions. After getting this functional we can minimize its value. For this minimization we could use the norm of the function, another option is to compute a weighted \emph{average} of the function over the domain. This is what we call a weighted residual
\[\Pi[u, w] = \int\limits_V w R(u) \dd{V}\, ,\]
and we want to minimize it by making
\[\var{\Pi}[u, w] = 0\, ,\]

In what follows we will consider different strategies to distribute or weight the residual $R$ over the computational domain.

\subsubsection{Galerkin method}
In the Galerkin scheme the interpolation functions are used also as weighting functions leading to:
\[\int\limits_V N^Q R\dd{V} = 0 \]
or explicitly
\begin{equation}
  \int\limits_V N^Q \mathcal{L} (N^P)\dd{V} u^P = \int\limits_V N^Q\rho F\dd{V}\, .
  \label{eq:Galer}
\end{equation}

Imposing \cref{eq:Galer} in the $K$ points conforming the computational domain or equivalently ranging $Q$ from $1$ to $K$ leads to the following system of algebraic equation
\begin{equation}
{K^{QP}}{U^P} = {f^Q}
\label{eq:DGaler}
\end{equation}

where $U^P$ is a vector that stores the point values of the function $u$ along te $K$ points of the computational domain, while $f^Q$ stores the corresponding point excitations.

\subsubsection{Least squares method}
In this method the integral of the square of the residual is minimized with respect to the $K$ point parameters or nodal values of the function. Accordingly,
\begin{align*}
  &\pdv{u^I}\int\limits_V R^2 \dd{V} = 0\\
  &\int\limits_V R \pdv{R}{u^I} \dd{V} = 0\, ,
\end{align*}

The least squares method is a special case of the weighted residual method for the weight functions
\[w^I = \pdv{R}{u^I}\, .\]

Expanding the residual, and considering the operator $\mathcal{L}$ as linear, we obtain
\begin{align*}
  &\pdv{u^I}\int\limits_V [\mathcal{L}(N^P u^P) - \rho F]^2 \dd{V} = 0\\
  &\int\limits_V [\mathcal{L} N^P u^P - \rho F] \mathcal{L}(N^I) \dd{V} = 0\\
 &\int\limits_V \mathcal{L}(N^I) \mathcal{L}(N^P) \dd{V} u^P - \rho \int\limits_V \mathcal{L}(N^I) F \dd{V} = 0
\end{align*}
which can be written like
\begin{equation}
  K^{IP} U^P = f^I
  \label{eq:Dsquares}
\end{equation}

\subsubsection{Collocation method}
In the collocation method the coefficients of the approximation are determined by forcing the residual to be exactly zero at $K$ points over the computational domain, i.e.,
\[\mathcal{L}(N^I) u^I - \rho F = 0\, ,\]
or
\[\mathcal{L}[N^I(x^J)] u^I - \rho F(x^J) = 0\, ,\]
where $J$ ranges between $1$ and $K$. This equation can be rewritten as a weighted-residual if we consider the residual to be $\delta(x - x^I)$, the Dirac delta function over the selected points

The resulting system of algebraic equation can be written as
\begin{equation}
K^{IP} U^P = f^I\, .
\label{eq:Colo}
\end{equation}

\subsubsection{Subdomain method}
The zero value of the residual is imposed upon $K$ subdomains
\[\int\limits_{V^I} \mathcal{L}(N^P)\dd{V^I} u^P  - \rho \int\limits_{V^I}  F\dd{V^I}  = 0 \qquad I=1,\cdots,K\, .\]

For instance, for the $N$-th element it follows that
\[\int\limits_{V^N} \mathcal{L}(N^P)\dd{V^N} u^P  - \rho \int\limits_{V^N} F\dd{V^N}  = 0 \qquad P=1,\cdots,K\, .\]

Applying the equation over the $K$ subdomains leads to the discrete system;
\begin{equation}
K^{IP} U^P = f^I \quad \quad I=1,\cdots,K\, .
\label{eq:Subdomain}
\end{equation}

\subsubsection{Ritz method}
It operates directly upon the variational statement of the problem. For a given functional
\[\Pi  = \Pi (N^Q u^Q)\]
the variational equation reads
\[\var{\Pi}  \equiv \pdv{\Pi}{u^Q} \var{u^Q} = 0\]
from which
\[\pdv{\Pi}{u^Q} = 0\, .\]

\subsubsection*{Problem: Discretization of the generalized parabolic equation.}
Let us consider the case of the generalized parabolic equation and its discretization following the Galerkin method
\[\rho(\vb x) \pdv{u(\vb x,t)}{t} + \mathcal{L}u(\vb x, t) = 0\]
which can also be written using indicial notation
\[\pdv{x_i}\left[{p(x)\pdv{u}{x_i} \right] + q(x)u + \rho \pdv{u}{t} = \rho F.\]

Assuming that $p(x)=1$ yields
\begin{align*}
  &-\int\limits_V N^P N_{,ii}^Q \dd{V} u^Q + \int\limits_V q N^P N^Q\dd{V} u^Q  + \rho \int\limits_V N^P N^Q \dd{V} v^Q\\
     &- \rho \int\limits_V N^P F \dd{V} = 0\\
  &\int\limits_V N_{,i}^P N_{,i}^Q \dd{V} u^Q - \int\limits_S N^P N_{,i}^Q \hat{n}_i \dd{S} u^Q + \int\limits_V q N^P N^Q \dd{V} u^Q\\
    &+ \rho \int\limits_V N^P N^Q \dd{V} v^Q  - \rho \int\limits_V N^P F \dd{V} = 0 \\
  &\int\limits_V \left(N_{,i}^P N_{,i}^Q + q N^PN^Q \right)\dd{V} u^Q  + \rho \int\limits_V N^P N^Q \dd{V} V^Q  =\\
      &\int\limits_S N^P N_{,i}^Q \hat{n}_i \dd{S} u^Q + \rho \int\limits_V N^P F \dd{V}
\end{align*}
which can be written in discrete form as
\[K^{PQ} U^Q + C^{PQ} V^Q = f^p\, .\]

\subsubsection*{Problem: Discretization of Navier equations.}
In this case the differential equations are written as
\[(\lambda  + \mu ){u_{j,ij}} + \mu {u_{i,jj}} + {f_i} = 0 \enspace .\]

We can write the differential operator as
\[L_{ij} \equiv (\lambda  + \mu )\pdv[2]{}{x_i}{x_j} + \mu \pdv[2]{}{x_k}{x_k}\delta_{ij}\]
\todo{Where was defined $r_i$. Some cohesion is missing here.}
\begin{align*}
&r_i =  - f_i\\
&u_i = N_i^Q u^Q\\
&\mathcal{L}_{ij}(u_j) \equiv (\lambda  + \mu )(N_j^Q u^Q)_{,ij} + \mu (N_j^Q u^Q)_{,kk}\delta_{ij}\\
&\mathcal{L}_{ij}(u_j) \equiv (\lambda  + \mu )N_{j,ij}^Q u^Q + \mu N_{i,kk}^Q u^Q\\
&\mathcal{L}_{ij}(u_j) \equiv \mathcal{L}_{ij}(N_j^Q) u^Q \enspace .
\end{align*}

In the Galerkin scheme we use the trial function as weighting function.
\[R_i \equiv L_{ij}(N_j^Q) u^Q + f_i\]
and we state
\[&\int\limits_V N_i^P R_i \dd{V} = 0 \qquad P=1,2,\cdots N\, .\]

Thus
\begin{align*}
&\int\limits_V N_i^P \mathcal{L}_{ij} (N_j^K)\dd{V} u^K  + \int\limits_V N_i^P f_i \dd{V} = 0 \\
&(\lambda  + \mu )\int\limits_V N_i^PN_{j,ij}^K \dd{V} u^K  + \mu \int\limits_V N_i^PN_{i,kk}^K \dd{V} u^K  + \int\limits_V N_i^P f_i \dd{V} = 0
\end{align*}
integrating by parts
\begin{align*}
- (\lambda  + \mu )\int\limits_V N_{i,j}^P N_{j,i}^K \dd{V} u^K + (\lambda  + \mu )\int\limits_S N_i^P N_{j,i}^K \hat{n}_j \dd{S} u^K - \mu \int\limits_V N_{i,k}^P N_{i,k}^K \dd{V} u^K \\
+ \mu \int\limits_S N_i^P N_{i,k}^K \hat{n}_k \dd{S} u^K  + \int\limits_V N_i^P f_i \dd{V} = 0
\end{align*}
which can be written like
\[K^{PQ} U^Q = F^P\]
where
\[K^{PQ} = (\lambda  + \mu )\int\limits_V N_{i,j}^P N_{j,i}^Q \dd{V}  + \mu \int\limits_V N_{i,k}^P N_{i,k}^Q\dd{V} \]
and
\[F^P = \int\limits_S N_i^P t_i^{(\hat n)} \dd{S} + \int\limits_V N_i^P f_i \dd{V} = 0 \, .\]

\subsubsection*{Problem: Discretization of the wave equation.}
In this case the differential equation reads
\[\div \left[ \frac{1}{\rho} \grad p(\vb{x})\right] - \pdv{t} \left(\frac{1}{\lambda} \pdv{\rho}{t}\right) - q(\vb{x}) = 0\]
where we recognize that
\[\mathcal{L} \equiv \div \left(\frac{1}{\rho} \grad \right) - \pdv{t}\left(\frac{1}{\lambda}\pdv{t}\right)\, .\]

Let
\[p(x) = N^K p^K\]
then
\[\mathcal{L}(p) \equiv \vec \nabla  \cdot \left( {\frac{1}{\rho }\vec \nabla {N^K}{p^K}} \right) - \frac{\partial }{{\partial t}}\left( {\frac{1}{\lambda }\frac{{\partial {N^K}{p^K}}}{{\partial t}}} \right)\]
or in index notation
\[\mathcal{L}(p) \equiv {\left( {\frac{1}{\rho }N_{,i}^K} \right)_{,i}}{p^K} - \frac{\partial }{{\partial t}}\left( {\frac{1}{\lambda }\frac{{\partial {N^K}}}{{\partial t}}} \right){p^K}\]
which is equivalent to
\[\mathcal{L}(p) \equiv \mathcal{L}({N^K}){p^K}\]
using the trial functions as weighting function and recalling the definition of the residual which in this case reads
\[R = \mathcal{L}({N^K}){p^K} - q\, ,\]
and yields
\[\int\limits_V {{N^J}RdV = 0} \quad \quad J=1,2,...,K \]

\[\int\limits_V {{N^J}L({N^K})dV{p^K}}  - \int\limits_V {{N^J}qdV}  = 0\]

\[\int\limits_V {{N^J}{{\left( {\frac{1}{\rho }N_{,i}^K} \right)}_{,i}}dV{p^K} - \int\limits_V {{N^J}\frac{\partial }{{\partial t}}\left( {\frac{1}{\lambda }\frac{{\partial {N^K}}}{{\partial t}}} \right)dV{p^K}} }  - \int\limits_V {{N^J}qdV}  = 0\]

Integrating by parts the first term on the right-hand-side gives us
\[ - \int\limits_V {N_{,i}^J\frac{1}{\rho }N_{,i}^KdV} {p^K} + \int\limits_S {{N^J}\frac{1}{\rho }N_{,i}^K{{\hat n}_i}dS{p^K}}  = \int\limits_V {{N^J}\frac{\partial }{{\partial t}}\left( {\frac{1}{\lambda }\frac{{\partial {N^K}}}{{\partial t}}} \right)dV{p^K}}  + \int\limits_V {{N^J}qdV} \]

\[\int\limits_V {N_{,i}^J\frac{1}{\rho }N_{,i}^KdV} {p^K} + \int\limits_V {{N^J}\frac{1}{\lambda }{N^K}dV{{\ddot p}^K}}  = \int\limits_S {{N^J}\frac{1}{\rho }N_{,i}^K{{\hat n}_i}dS{p^K}}  + \int\limits_V {{N^J}qdV} \]

\[{K^{JK}}{P^K} + {M^{JK}}{{\ddot P}^K} + {f^J} = 0\]


\documentclass{article}
\input{../general}

\MakeTitle{6}

\begin{document}
\maketitle

\gotosection{2}{5}
\subsection{Kernels, images, and the dimension formula}

\begin{exercise}{1}
\begin{enumerate}
\item $A\Vect{v}_1 = \Vect{0}$, $\Vect{v}_1 \in \ker{A}$.

$A\Vect{v}_2 = \xmat{2\\3\\3}$, $\Vect{v}_2 \notin \ker{a}$.

$A\Vect{v}_3 = \Vect{0}$, $\Vect{v}_3 \in \ker{A}$.

\item Since \FunSS{T}{\mathbb{R}^5}{\mathbb{R}^3}, $\Vect{w}_4$ has the right height to be in the kernel of $T$, while $\Vect{w}_1$ and $\Vect{w}_3$ has the right height to be in the image of $T$.
$$\widetilde{T} = \xmat{1&0&0&2&1\\0&1&0&5&0\\0&0&1&1&-1}$$
\begin{align*}
  x_1 &= -2x_4 - x_5 \\
  x_2 &= -5x_4 \\
  x_3 &= -x_4 + x_5
\end{align*}

Let $x_4=1, x_5=0$, $\xmat{-2\\-5\\-1\\1\\0}$ is in the kernel of $T$.
\end{enumerate}
\end{exercise}

\begin{exercise}{2}
\begin{enumerate}
\item False, should span $\mathbb{R}^m$.
\item True.
\item True.
\item False.
\item False, should be $n - m$.
\item False, unless $T$ is also injective.
\item False, unless $T$ is also injective.
\end{enumerate}
\end{exercise}

\begin{exercise}{3}
\newcolumntype{L}{>{\centering\arraybackslash}m{4cm}}
\newcolumntype{I}{>{\centering\arraybackslash}m{4.2cm}}
\begin{center}
\begin{tabular}{|L|I|c|}
\hline
rank $T$ & nullity $T$ & rank $T$ + nullity $T$\\
\hline
dim image $T$ & dim ker $T$ & dim domain $T$ \\
\hline
\# pivotal columns of $T$ & \# nonpivotal columns of $T$ & \\
\hline
\# linearly independent columns of $T$ && \\
\hline
\end{tabular}
\end{center}

`\#' stands for ``no. of".
\end{exercise}

\def \img{\mathrm{img}}

\begin{exercise}{5}
\begin{enumerate}
\item $T(\Vect{0}) = \Vect{0}$, therefore $\Vect{0} \in \ker T$.
  For any $\Vect{u}_1, \Vect{u}_2 \in \ker T$, $T(\Vect{u}_1 + \Vect{u}_2) = T(\Vect{u}_1) + T(\Vect{u}_2) = \Vect{0} + \Vect{0} = \Vect{0}$, therefore, $\Vect{u}_1 + \Vect{u}_2 \in \ker T$.
  For any $\Vect{u} \in \ker T$, $c \in \mathbb{R}$, $T(c\Vect{u}) = c T(\Vect{u}) = c\Vect{0} = \Vect{0}$, therefore, $c\Vect{u} \in \ker T$.
  Hence, $\ker T$ is a subspace of $\mathbb{R}^n$. \rQED

\item $T(\Vect{0}) = \Vect{0}$, therefore $\Vect{0} \in \img T$. For any $\Vect{u}_1 = T(\Vect{v}_1), \Vect{u}_2 = T(\Vect{v}_2) \in \img T$, $\Vect{u}_1 + \Vect{u}_2 = T(\Vect{v}_1) + T(\Vect{v}_2) = T(\Vect{v}_1 + T\Vect{v}_2) \in \img T$. For any $\Vect{u} = T(\Vect{v}) \in \img T$, $c \in \mathbb{R}$, $c\Vect{u} = cT(\Vect{v}) = T(c\Vect{v}) \in \img T$. Therefore, $\img T$ is a subspace of $\mathbb{R}^m$. \rQED
\end{enumerate}
\end{exercise}

\begin{exercise}{6}
\begin{enumerate}
\item $A = \xmat{1&1&3\\2&2&6}$, $\widetilde{A} = \xmat{1&1&3\\0&0&0}$. A basis for the image: $\xmat{1\\2}$. A basis for the kernel: $\xmat{-1\\1\\0}$, $\xmat{-3\\0\\1}$.

\item $A = \xmat{1&2&3\\-1&1&1\\-1&4&5}$, $\widetilde{A} = \xmat{1&0&1/3\\0&1&4/3\\0&0&0}$. A basis for the image: $\xmat{1&-1&-1}$, $\xmat{2\\1\\4}$. A basis for the kernel: $\xmat{-1/3\\-4/3\\1}$.

\item $A = \xmat{1&1&1\\1&2&3\\2&3&4}$, $\widetilde{A} = \xmat{1&0&-1\\0&1&2\\0&0&0}$. A basis for the image: $\xmat{1\\1\\2}$, $\xmat{1\\2\\3}$. A basis for the kernel: $\xmat{1\\-2\\1}$.
\end{enumerate}
\end{exercise}

\begin{exercise}{8}
False.

Let $[f]$ be a matrix with all entries being zero. Then, for any $\Vect{v} \in \mathbb{R}^m$, $f(\Vect{v}) = 0$, therefore, $\ker f = \mathbb{R}^m$. Let $g$ be a non-surjective function, i.e. $n < m$, then $\img g \subset \mathbb{R}^m$ and $\img g \neq \mathbb{R}^m$. In other words, $\img g$ can only be a proper subset of $\ker g$. \rQED
\end{exercise}

\begin{exercise}{9}
\begin{enumerate}
\item $T(a+bx+cx^2) = bx+4cx^2$, $[T] = \cmat{0&0&0\\0&1&0\\0&0&4}$.
\item Let $A = [T]$, $\widetilde{A} = \cmat{0&1&0\\0&0&1\\0&0&0}$. The second and the third columns (pivotal columns) of $A$ then form the basis of $\img T$, namely $\cmat{0\\1\\0}$ and $\cmat{0\\0\\4}$. $\dim{\ker T} = 1$, and a basis of $\ker T$ is $\cmat{1\\0\\0}$, which can be derived from either the method mentioned in the textbook or the fact that only constant polynomials are in the kernel.
\end{enumerate}
\end{exercise}

\begin{exercise}{10}
Fix $k$, for the sake of clarity.

The problem can be seen as finding a matrix $A \in M_{(1,k+1)}$ such that:
$$\xmat{c_0 & \hdots
& c_k}\cmat{p(0)\\\vdots\\p(k)} = \int^k_0p(t)dt$$

Let's call the vector a polynomial vector. In the subsection of ``Interpolation and the dimension formula", the textbook has proved that for each polynomial $p \in P_k$, there exists one and only one such polynomial vector $\Vect{v} \in \mathbb{R}^{k+1}$, and for each polynomial vector $\Vect{v} \in \mathbb{R}^{k+1}$, there exists one and only one $p \in P_k$.\footnote{Formula 2.5.15, and ``in particular, $T_k$ is invertible}

For $A$ to be exists, the relation between $\cmat{p(0)\\\vdots\\p(k)}$ and $\int^k_0p(t)dt$ must be linear, because there is a bijective relation between matrix and linear transformation. To put it more explicit, \FunSS{T}{\mathbb{R}^{k+1}}{\mathbb{R}} such that:

$$T(\cmat{p(0)\\\vdots\\p(k)}) = \int^k_0p(t)dt$$

must be a linear transformation.

Let's denote the polynomial vector of a polynomial $p$ as $\Vect{p}$. For any two polynomials $p, q \in P_k$ such that,
$p = \sum_{i=0}^kp_ix^i$,
$q = \sum_{i=0}^kq_ix^i$.
The sum of $p$ and $q$, denoted as $+_{p,q}$, is
$\sum_{i=0}^k(p_i+q_i)x^i$. It's obvious that for each $x \in \mathbb{R}$, $+_{p,q}(x) = p(x) + q(x)$. Then we have:
$$\Vect{v}_{+_{p,q}} = \sum_{i=0}^ke_i(p_i+q_i)i^i = \sum_{i=0}^ke_ip_ii^i + \sum_{i=0}^ke_iq_ii^i = \Vect{v}_p + \Vect{v}_q$$
where $e_i$ is a standard basis vector, and:
$$T(\Vect{v}_{+_{p,q}})=\int^k_0+_{p,q}(t)dt=\int^k_0p(t)dt+\int^k_0p(t)dt = T(\Vect{v}_p) + T(\Vect{v}_q)$$

Let $*_{c,p} = \sum_{i=0}^k cp_ix^i$ for any real number $c$. It is clear that $*_{c,p} = c\sum_{i=0}^k p_ix^i$, or $*_{c,p}(x) = cp(x)$ for each $x \in \mathbb{R}$. Then we have:
$$\Vect{v}_{*_{c,p}} = c\Vect{v}_p$$
, and:
$$T(\Vect{v}_{*_{c,p}}) = \int^k_0*_{c,p}(t)dt = \int^k_0cp(t)dt = c\int^k_0p(t)dt = T(\Vect{v}_p)$$

Therefore, $T$ is indeed a linear transformation, $A = [T]$, and $c_0,\hdots,c_k$ do exist. \rQED
\end{exercise}

\begin{exercise}{15}
Since $AB$ is invertible, the linear transformation $T_{AB}$ is bijective. Then, due to $T_{AB} = T_A \circ T_B$, $T_A$ has to be surjective, otherwise there exists a $\Vect{v} \in \mathbb{R}^n$ such that $T_A(\Vect{x}) = \Vect{v}$ has no solution, which means $T_A \circ T_B(\Vect{x}) = \Vect{v}$ also has no solution, contradicting the surjectiveness of $T_{AB}$. Because $T_A$ is surjective and $A$ is square, $A$ is invertible.

Now assume that $T_B$ is not injective. We can find $\Vect{v}_1, \Vect{v}_2 \in \mathbb{R}^n$ such that $T_B(\Vect{v}_1) = T_B(\Vect{v}_2)$. As a result, $T_A \circ T_B(\Vect{v}_1) = T_A \circ T_B(\Vect{v}_2)$, which contradicts the injectiveness of $T_{AB}$. Therefore, $T_B$ is injective, and since $B$ is square, $B$ is invertible.

This completes the proof. \rQED
\end{exercise}
\end{document}


\documentclass[11pt]{article}
\usepackage{mathptmx}
\usepackage[small,compact]{titlesec}
\usepackage{geometry}                % See geometry.pdf to learn the layout options. There are lots.
\usepackage{url}
\usepackage{hyperref}
\usepackage{breakurl}
\geometry{letterpaper}                   % ... or a4paper or a5paper or ...
%\geometry{landscape}                % Activate for for rotated page geometry
\usepackage[parfill]{parskip}    % Activate to begin paragraphs with an empty line rather than an indent
\usepackage{graphicx}
\usepackage{amssymb, amsmath}
\usepackage{epstopdf}

\DeclareGraphicsRule{.tif}{png}{.png}{`convert #1 `dirname #1`/`basename #1 .tif`.png}

\title{Raytracing Quadrics}
\author{Miles Macklin}
%\date{}                                           % Activate to display a given date or no date

\renewcommand{\v}[1]{\ensuremath{\mathbf{#1}}} % for vectors
\newcommand{\uv}[1]{\ensuremath{\mathbf{\hat{#1}}}} % for unit vectors
\newcommand\ddx[1]{\frac{\partial#1}{\partial \v{x} }}
\newcommand\dd[2]{\frac{\partial#1}{\partial #2}}

\begin{document}
\maketitle

\section{Introduction}

This short article shows how to define and manipulate quadric surface equations in matrix form and intersect rays against them for rendering. The treatment here is inspired by \cite{Sigg:2006:GRQ:2386388.2386396} and work by Simon Green at NVIDIA.

\section{Quadrics}

The quadratic equation in 3 variables can be written as:

\begin{equation}
f(x,y,z) = Ax^2 +2Bxy+2Cxz+2Dx+Ey^2+2Fyz+Gy+Hz^2 +2Iz+J = 0
\end{equation}

It can also be written in matrix form using homogenous coordinates:

\begin{equation}
\v{x}^T\v{Q}\v{x} = 0
\end{equation}

where $\v{Q}$ is the matrix of coefficients:

\begin{equation}
\v{Q} =
\begin{bmatrix}
  A & B & C & D \\
  B & E & F & G \\
  C & F & H & I \\
  D & G & I & J
 \end{bmatrix}
 \qquad
  \v{x} =
 \begin{bmatrix}
 x \\ y \\ z \\ 1
 \end{bmatrix}
\end{equation}

Because this matrix is symmetric it can be diagonalised by a matrix $\v{T}$. With appropriate scaling, our matrix $\v{Q}$ can be expressed as a diagonal matrix $\v{D}$ with entries $\pm 1$ or $0$:

\begin{equation}
\v{Q}= \v{T}^{-T}\v{D}\v{T}^{-1}
\end{equation}

We refer to the basis that diagonalises $\v{Q}$ as the parameter space.

$\v{Q}$ can may be transformed to a different basis by any affine transformation $\v{M}$ by multiplying by $\v{M}^{-1}$ on both sides. This is equivalent to moving a point $\v{x}'$ back to the basis for $\v{Q}$,

\begin{equation}
\v{Q'} = \v{M}^{-T}\v{Q}\v{M}^{-1}.
\end{equation}
%
\begin{equation}
\v{x}' = \v{M}\v{x}
\end{equation}
%
\begin{equation}
\v{x'}^T\v{Q'}\v{x'} = \v{x'}^T\v{M}^{-T}\v{Q}\v{M}^{-1}\v{x'} = \v{x}^T\v{Q}\v{x}
\end{equation}

As we will soon see, view (or eye) space is a convenient space for ray-tracing. To transform our quadric equation to view space we multiply by the inverse model view matrix $\v{MV}$:

\begin{equation}
\v{Q'} = \v{MV}^{-T}\v{Q}\v{MV}^{-1} = \v{MV}^{-T}\v{T}^{-T}\v{D}\v{T}^{-1}\v{MV}^{-1} = (\v{MV}\cdot\v{T})^{-T}\v{D}(\v{MV}\cdot\v{T})^{-1}
\end{equation}

To ray trace the quadric we parameterise the view ray in the usual way,

\begin{equation}
\v{x}_v = \v{o} + t\v{d},
\end{equation}

and transform this back to parameter space:

\begin{equation}
\v{x}_p = (\v{MV}\cdot\v{T})^{-1}\v{o} + t(\v{MV}\cdot\v{T})^{-1}\v{d} = \v{o}_p + t\v{d}_p
\end{equation}

Inserting this into our quadratic equation,

\begin{equation}
\v{x}^T_p\v{Q}\v{x}_p = (\v{o}_p + t\v{d}_p)^T\v{D}(\v{o}_p + t\v{d}_p) = 0,
\end{equation}

and expanding and gathering for $t$, we have:

\begin{equation}
t^2(\v{d}^T_p\v{D}\v{d}_p) + 2t(\v{o}^T_p\v{D}\v{d}_p) + \v{o}^T_p\v{D}\v{o}_p = 0
\end{equation}

which we can solve with the quadratic formula.

The advantage of defining our rays in view space is that the ray origin, or eye position $\v{o}$ is simply the homogenous zero vector:

\begin{equation}
\v{o} =
\begin{bmatrix}
0 \\
0 \\
0 \\
1
\end{bmatrix}
\end{equation}

Which simply extracts the last column of ($\v{MV}\cdot\v{T})^{-1}$ when we form $\v{o}_p$:

\begin{equation}
\v{o}_p = \v{c}_4
\end{equation}

\bibliographystyle{alpha}
\bibliography{quadric}

\end{document}


\chapter{Univalent logic}

\section{Propositional truncation}

Using the same idea of higher inductive types, we can also turn every type into a mere proposition via an operation we call \emph{propositional truncation}.

\begin{defn}
Let $A:\UU$ be a type. We define the \define{propositional truncation} $\brck{A}:\UU$ as a higher inductive type with constructors
\begin{align*}
\eta & : A\to \brck{A} \\
\mu & : \prd{x,y:\brck{A}} x=y.
\end{align*}
The induction principle of $\brck{A}$ asserts that for any type family $P:\brck{A}\to\type$, if we have
\begin{align*}
f & : \prd{x:A}P(\eta(x)) \\
g & : \prd{x,y:\brck{A}}{p:P(x)}{q:P(y)} \trans{\mu(x,y)}{p}=q,
\end{align*}
then we obtain a section $\rec{\brck{\blank}}(f,g):\prd{x:\brck{A}}P(x)$ satisfying
\begin{align*}
\rec{\brck{\blank}}(f,g,\eta(x)) & = f(x).
\end{align*}
\end{defn}

\begin{rmk}
We will not need a computation rule corresponding to the path constructor $\mu$.
\end{rmk}

\begin{lem}
For any $A:\UU$, the type $\brck{A}$ is a proposition.
\end{lem}

\begin{proof}
The path constructor $\mu:\prd{x,y:\brck{A}}x=y$ directly provides a proof that $\brck{A}$ is a proposition.
\end{proof}

The following theorem asserts that for any map $f:A\to P$ into a proposition $P$, there is a unique map $g:\brck{A}\to P$ such that $g\circ\eta=f$, as indicated in the following diagram
\begin{equation*}
\begin{tikzcd}
A \arrow[dr,"\forall"] \arrow[d,swap,"\eta"] \\
\brck{A} \arrow[r,densely dotted,swap,"\exists!"] & P
\end{tikzcd}
\end{equation*}
We call this property the \define{universal property of propositional truncation}.

\begin{thm}
Let $A:\UU$ be a type. Then for any proposition $P$, the map
\begin{equation*}
\lam{g} g\circ \eta : (\brck{A}\to P)\to (A\to P)
\end{equation*}
is an equivalence.
\end{thm}

\begin{proof}

\end{proof}

\section{Surjective maps}

\begin{defn}
We say that a type $A$ is \define{inhabited} if there is a term of type $\brck{A}$.
\end{defn}

\begin{defn}
A function $f:A\to B$ is said to be surjective if its fibers are inhabited.
More explicitly, we define
\begin{equation*}
\mathsf{is\usc{}surj}(f)\defeq \prd{b:B}\brck{\fib{f}{b}}.
\end{equation*}
\end{defn}

\section{First order logic in type theory}
\begin{table}
\caption{Logic in type theory}
\begin{center}
\begin{tabular}{ll}
\toprule
\emph{Logical connective} & \emph{Interpretation in HoTT} \\
\midrule
$\top$ & $\unit$ \\
$\bot$ & $\emptyt$ \\
$P\land Q$ & $P\times Q$ \\
$P\lor Q$ & $\brck{P+Q}$ \\
$P\to Q$ & $P\to Q$ \\
$P\leftrightarrow Q$ & $P=Q$ \\
$\neg P$ & $P\to\emptyt$ \\
$\forall x.P(x)$ & $\prd{x:A}P(x)$ \\
$\exists x.P(x)$ & $\brck{\sm{x:A}P(x)}$ \\
$\exists! x.P(x)$ & $\iscontr(\sm{x:A}P(x))$ \\
\bottomrule
\end{tabular}
\end{center}
\end{table}
\begin{enumerate}
\item First order logic in type theory. Stress difference between $\exists$ and $\Sigma$.
\item Propositional extensionality
\end{enumerate}

\begin{exercises}
\item Show that
\begin{equation*}
\eqv{\brck{A}}{\prd{P:\prop}(A\to P)\to P}.
\end{equation*}
\item For any $B:A\to\UU$, construct an equivalence
\begin{equation*}
\eqv{\Big(\exists a.\brck{B(a)}\Big)}{\Big(\brck{\sm{a:A}B(a)}\Big)}
\end{equation*}
\item \label{also}(Mart\'in Escard\'o) For any two propositions $P$ and $Q$, define
\begin{equation*}
P\boxplus Q \defeq ((P\to Q)\to Q)\times ((Q\to P)\to P).
\end{equation*}
\begin{subexenum}
\item Show that $P\lor Q\to P\boxplus Q$ and $P\boxplus Q\to\neg(\neg P\land \neg Q)$.
\end{subexenum}
\item Show that for any mere proposition $Q$, and any type $X$, the following are equivalent:
\begin{enumerate}
\item The map $(Q\to X)\to(\emptyt\to X)$ is an equivalence.
\item The type $X^Q$ is contractible.
\item $Q\to\iscontr(X)$.
\end{enumerate}
\item \label{ex:brck_comp} Formulate the computation rule corresponding to the path constructor $\mu$. That is, compute the type of $\apd{\rec{\brck{\blank}}(f,g)}{\mu(x,y)}$, and find a canonical element in it.
\end{exercises}


\hypertarget{naive-bayes-classification}{%
\chapter{Naïve Bayes
Classification}\label{naive-bayes-classification}}

Naïve Bayes classification algorithm is based on Bayes' Theorem. The
dataset is divided into two parts, namely, feature matrix and the
response vector.

\hypertarget{bayes-theorem}{%
\section{Bayes' Theorem}\label{bayes-theorem}}

Bayes' Theorem provides a way that we can calculate the probability of a
piece of data belonging to a given class, given our prior knowledge.
Bayes' Theorem is stated as:

\[
\Large P(\alpha |\beta) = \frac{P(\beta |\alpha) * P(\alpha)}{P(\beta)}
\]

Naive Bayes is a classification algorithm for binary (two-class) and
multiclass classification problems. It is called Naive Bayes or idiot
Bayes because the calculations of the probabilities for each class are
simplified to make their calculations tractable.

\section{Bayes' Theorem Calculation}

In our example, we shall use \texttt{sklearn.naive\_bayes} to classify
on the basis of previous data wheater or not a person has gotten a flu
or not. We shall also check the accuracy and generate confusion matrix
of the model. However, to gain a general understanding, here is how it works:

Step 1: Study the dataset Our dataset include 4 feature columns and 1
target column.

\begin{verbatim}
Chills  Runny_nose  Headache    Fever  |  Flu
\end{verbatim}

Step 2: Calculate probability of target

\[ P(Flu|Y) = 10/14 = 0.714285714 \] \[ P(Flu|N) = 4/14 = 0.285714286 \]

Step 3: Calculate probability of feature columns for each

For each column of feature, calculate probability of all cases features
X target. Such as - \[ P(Chills=Y|Flu=Y) = 6/10 = .6 \]
\[ P(Chills=Y|Flu=N) = 1/4 = .25 \] And so on\ldots{}

Step 4:
Calculate for the given case \[
\Large P(\alpha |\beta) = \frac{P(\beta |\alpha) * P(\alpha)}{P(\beta)}
\] Here,
\begin{enumerate}
    \item \(\alpha\), \(\beta\) = Event
    \item \(P(\alpha)\), \(P(\beta)\) = Probability of event occurring
    \item \(P(\alpha |\beta)\) = Probability of \(\alpha\) happening such that \(\beta\) is true
\end{enumerate}

\section{Introduction}
    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{1}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{k+kn}{import} \PY{n+nn}{numpy} \PY{k}{as} \PY{n+nn}{np}
\PY{k+kn}{import} \PY{n+nn}{matplotlib}\PY{n+nn}{.}\PY{n+nn}{pyplot} \PY{k}{as} \PY{n+nn}{plt}
\PY{k+kn}{import} \PY{n+nn}{pandas} \PY{k}{as} \PY{n+nn}{pd}
\PY{k+kn}{from} \PY{n+nn}{sklearn}\PY{n+nn}{.}\PY{n+nn}{naive\PYZus{}bayes} \PY{k+kn}{import} \PY{n}{GaussianNB}
\PY{k+kn}{from} \PY{n+nn}{sklearn}\PY{n+nn}{.}\PY{n+nn}{model\PYZus{}selection} \PY{k+kn}{import} \PY{n}{train\PYZus{}test\PYZus{}split}
\PY{k+kn}{from} \PY{n+nn}{sklearn}\PY{n+nn}{.}\PY{n+nn}{metrics} \PY{k+kn}{import} \PY{n}{confusion\PYZus{}matrix}\PY{p}{,} \PY{n}{accuracy\PYZus{}score}
\end{Verbatim}
\end{tcolorbox}

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{2}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n}{dataset} \PY{o}{=} \PY{n}{pd}\PY{o}{.}\PY{n}{read\PYZus{}csv}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{./data/flu.csv}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}
\end{Verbatim}
\end{tcolorbox}

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{3}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n}{X\PYZus{}raw} \PY{o}{=} \PY{n}{dataset}\PY{o}{.}\PY{n}{iloc}\PY{p}{[}\PY{p}{:}\PY{p}{,}\PY{l+m+mi}{0}\PY{p}{:}\PY{o}{\PYZhy{}}\PY{l+m+mi}{1}\PY{p}{]}
\PY{n}{y} \PY{o}{=} \PY{n}{dataset}\PY{o}{.}\PY{n}{iloc}\PY{p}{[}\PY{p}{:}\PY{p}{,}\PY{o}{\PYZhy{}}\PY{l+m+mi}{1}\PY{p}{]}
\PY{n}{map\PYZus{}dict} \PY{o}{=} \PY{p}{\PYZob{}}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{Y}\PY{l+s+s2}{\PYZdq{}}\PY{p}{:} \PY{l+m+mi}{1}\PY{p}{,} \PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{N}\PY{l+s+s2}{\PYZdq{}}\PY{p}{:} \PY{l+m+mi}{0}\PY{p}{,} \PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{No}\PY{l+s+s2}{\PYZdq{}}\PY{p}{:} \PY{l+m+mi}{0}\PY{p}{,} \PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{Mild}\PY{l+s+s2}{\PYZdq{}}\PY{p}{:} \PY{l+m+mi}{1}\PY{p}{,} \PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{Strong}\PY{l+s+s2}{\PYZdq{}}\PY{p}{:} \PY{l+m+mi}{2}\PY{p}{\PYZcb{}}
\end{Verbatim}
\end{tcolorbox}

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{4}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n}{dataset}
\end{Verbatim}
\end{tcolorbox}

            \begin{tcolorbox}[breakable, size=fbox, boxrule=.5pt, pad at break*=1mm, opacityfill=0]
\prompt{Out}{outcolor}{4}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
   Chills Runny\_nose Headache Fever Flu
0       Y          N     Mild     Y   N
1       Y          Y       No     N   Y
2       Y          N   Strong     Y   Y
3       N          Y     Mild     Y   Y
4       N          N       No     N   N
5       N          Y   Strong     Y   Y
6       N          Y   Strong     N   N
7       Y          Y     Mild     Y   Y
8       N          Y   Strong     Y   Y
9       Y          Y     Mild     Y   Y
10      N          N       No     N   N
11      Y          Y   Strong     Y   Y
12      Y          N   Strong     Y   Y
13      Y          N     Mild     Y   Y
\end{Verbatim}
\end{tcolorbox}

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{5}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n}{X} \PY{o}{=} \PY{n}{pd}\PY{o}{.}\PY{n}{DataFrame}\PY{p}{(}\PY{p}{)}
\PY{k}{for} \PY{n}{x} \PY{o+ow}{in} \PY{n}{X\PYZus{}raw}\PY{p}{:}
    \PY{n}{X}\PY{p}{[}\PY{n}{x}\PY{p}{]} \PY{o}{=} \PY{n}{X\PYZus{}raw}\PY{p}{[}\PY{n}{x}\PY{p}{]}\PY{o}{.}\PY{n}{map}\PY{p}{(}\PY{n}{map\PYZus{}dict}\PY{p}{)}
\end{Verbatim}
\end{tcolorbox}

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{6}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n}{X}\PY{o}{.}\PY{n}{head}\PY{p}{(}\PY{p}{)}
\end{Verbatim}
\end{tcolorbox}

            \begin{tcolorbox}[breakable, size=fbox, boxrule=.5pt, pad at break*=1mm, opacityfill=0]
\prompt{Out}{outcolor}{6}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
   Chills  Runny\_nose  Headache  Fever
0       1           0         1      1
1       1           1         0      0
2       1           0         2      1
3       0           1         1      1
4       0           0         0      0
\end{Verbatim}
\end{tcolorbox}

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{7}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n}{y}\PY{o}{.}\PY{n}{head}\PY{p}{(}\PY{p}{)}
\end{Verbatim}
\end{tcolorbox}

            \begin{tcolorbox}[breakable, size=fbox, boxrule=.5pt, pad at break*=1mm, opacityfill=0]
\prompt{Out}{outcolor}{7}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
0    N
1    Y
2    Y
3    Y
4    N
Name: Flu, dtype: object
\end{Verbatim}
\end{tcolorbox}

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{8}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n}{X\PYZus{}train}\PY{p}{,} \PY{n}{X\PYZus{}test}\PY{p}{,} \PY{n}{y\PYZus{}train}\PY{p}{,} \PY{n}{y\PYZus{}test} \PY{o}{=} \PY{n}{train\PYZus{}test\PYZus{}split}\PY{p}{(}\PY{n}{X}\PY{p}{,} \PY{n}{y}\PY{p}{,} \PY{n}{test\PYZus{}size}\PY{o}{=}\PY{o}{.}\PY{l+m+mi}{5}\PY{p}{,} \PY{n}{random\PYZus{}state}\PY{o}{=}\PY{l+m+mi}{42}\PY{p}{)}
\PY{n}{model} \PY{o}{=} \PY{n}{GaussianNB}\PY{p}{(}\PY{p}{)}\PY{o}{.}\PY{n}{fit}\PY{p}{(}\PY{n}{X\PYZus{}train}\PY{p}{,} \PY{n}{y\PYZus{}train}\PY{o}{.}\PY{n}{values}\PY{o}{.}\PY{n}{ravel}\PY{p}{(}\PY{p}{)}\PY{p}{)}
\PY{n}{predicted\PYZus{}y} \PY{o}{=} \PY{n}{model}\PY{o}{.}\PY{n}{predict}\PY{p}{(}\PY{n}{X\PYZus{}test}\PY{p}{)}
\end{Verbatim}
\end{tcolorbox}

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{11}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n+nb}{print}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{Accuracy:}\PY{l+s+s2}{\PYZdq{}}\PY{p}{,} \PY{n}{accuracy\PYZus{}score}\PY{p}{(}\PY{n}{y\PYZus{}test}\PY{p}{,} \PY{n}{predicted\PYZus{}y}\PY{p}{)}\PY{p}{)}
\PY{n+nb}{print}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{Confusion Matrix:}\PY{l+s+se}{\PYZbs{}n}\PY{l+s+s2}{\PYZdq{}}\PY{p}{,} \PY{n}{confusion\PYZus{}matrix}\PY{p}{(}\PY{n}{y\PYZus{}test}\PY{p}{,} \PY{n}{predicted\PYZus{}y}\PY{p}{)}\PY{p}{)}
\end{Verbatim}
\end{tcolorbox}

    \begin{Verbatim}[commandchars=\\\{\}]
Accuracy: 0.8571428571428571
Confusion Matrix:
 [[0 1]
 [0 6]]
    \end{Verbatim}

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{12}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{k}{def} \PY{n+nf}{make\PYZus{}prediction}\PY{p}{(}\PY{n}{case}\PY{p}{)}\PY{p}{:}
    \PY{n}{df} \PY{o}{=} \PY{n}{pd}\PY{o}{.}\PY{n}{DataFrame}\PY{p}{(}\PY{n}{case}\PY{p}{)}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{o}{.}\PY{n}{map}\PY{p}{(}\PY{n}{map\PYZus{}dict}\PY{p}{)}
    \PY{k}{return} \PY{n}{model}\PY{o}{.}\PY{n}{predict}\PY{p}{(}\PY{p}{[}\PY{n}{df}\PY{p}{]}\PY{p}{)}
\end{Verbatim}
\end{tcolorbox}

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{13}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n+nb}{print}\PY{p}{(}\PY{n}{make\PYZus{}prediction}\PY{p}{(}\PY{p}{[}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{Y}\PY{l+s+s2}{\PYZdq{}}\PY{p}{,} \PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{N}\PY{l+s+s2}{\PYZdq{}}\PY{p}{,} \PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{Mild}\PY{l+s+s2}{\PYZdq{}}\PY{p}{,} \PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{Y}\PY{l+s+s2}{\PYZdq{}}\PY{p}{]}\PY{p}{)}\PY{p}{)}
\end{Verbatim}
\end{tcolorbox}

    \begin{Verbatim}[commandchars=\\\{\}]
['Y']
    \end{Verbatim}

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{14}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n+nb}{print}\PY{p}{(}\PY{n}{make\PYZus{}prediction}\PY{p}{(}\PY{p}{[}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{N}\PY{l+s+s2}{\PYZdq{}}\PY{p}{,} \PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{N}\PY{l+s+s2}{\PYZdq{}}\PY{p}{,} \PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{Mild}\PY{l+s+s2}{\PYZdq{}}\PY{p}{,} \PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{N}\PY{l+s+s2}{\PYZdq{}}\PY{p}{]}\PY{p}{)}\PY{p}{)}
\end{Verbatim}
\end{tcolorbox}

    \begin{Verbatim}[commandchars=\\\{\}]
['N']
    \end{Verbatim}

MCA/Machine Learning/Project file/Tex source/Naive Bayes.tex


% When using TeXShop on the Mac, let it know the root document. The following must be one of the first 20 lines.
% !TEX root = ../design.tex

\chapter[Sandwich Estimators]{Sandwich Estimators}

\section{Introduction}
Given a regression setup of $n$ data points, each defined by a feature  vector $x_i$ and a category $y_i$, we assume that $y_i$ is controlled by a $k$-dimensional parameter vector $\theta$.  Generally, we are interested in finding the values of $\theta$ that best predict $y_i$ from $x_i$, with \textit{best} being defined as the values that maximize some likelihood function $L(y,x,\theta)$.  The maximization is typically solved using the derivative of the likelihood $\psi$  and the Hessian $H$.  More formally, $\psi$ is defined as
\begin{align}
  \psi(y_i,x_i, \theta) = \frac{\partial l(x_i,y_i,\theta)}{\partial \theta}
\end{align}
and $H$ is defined as
\begin{align}
H(y,x, \theta) = \frac{\partial^2 L(x,y,\theta)}{\partial \theta^2}.
\end{align}

In addition to the values of $\theta$, we may also be interested in the covariance matrix $S(\theta)$ of $\theta$.  This can be expressed in a \textit{sandwich formulation}, of the form
\begin{align}
S(\theta) = B(\theta) M(\theta) B(\theta).
\end{align}
The $B(\theta)$ matrix is commonly called the \textit{bread}, whereas the $M(\theta)$ matrix is the \textit{meat}.

\subsection{The Bread}
Computing $B$ is relatively straightforward,
\begin{align}
B(\theta) = n\left(\sum_i^n -H(y_i, x_i, \theta) \right)^{-1}
\end{align}

\subsection{The Meat}
There are several choices for the $M$ matrix, each with different robustness properties.  The estimators we are interested in for this implementation are the Huber/White estimator, and the clustered estimator.

In the Huber/White estimator, the matrix $M$ is defined as
\begin{align}
M_{H} = \sum_i^n \psi(y_i,x_i, \theta)^T  \psi(y_i,x_i, \theta).
\end{align}

%The Huber/White estimator is a diagonal matrix defined as
%\begin{align}
%M_W =\frac{1}{n} X^T \left(
%      \begin{array}{cccc}
%        r(y_1, x_1^T\theta) & 0&\ldots &0 \\
%        0&r(y_2, x_2^T\theta) &\dots& \vdots \\
%	\vdots & \vdots & \ddots&\vdots \\
%        0 & \ldots & \ldots &r(y_n, x_n^T\theta)
%      \end{array} \right) X.
%\end{align}
%The matrix $X$ is the data matrix, and $r$ is the residual function.  The residual function is defined by the application, but it has the relationship
%\begin{align}
%  \psi(y_i,x_i, \theta) = r(y_i, x_i^T\theta)x_i.
%\end{align}
%This means that it can be computed in the general case as
%\begin{align}
% r(y_i, x_i^T\theta) =  \frac{\psi(y_i,x_i, \theta)}{x_i}
%\end{align}
%
%In the case of the clustered sandwich estimator, $M$ is defined as

%In addition to the computational expense of computing the Hessian, the inverse Hessian must be computed as well.  Unfortunately, matrix inversion is an expensive operation, and  cannot be parallelized.