\documentclass[a4paper, 12pt]{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{bm}						% Typesetting matrices
\usepackage{authblk}				% Affiliation in title
\usepackage{amsmath}				% All kinds of shit
\usepackage{amssymb}				% For blackboard bold
\usepackage{commath}				% Differetial operators
\usepackage{array}					% Fancy tables
\usepackage{units}					% Slanted fractions
\usepackage{enumitem}				% Sep options
\usepackage{fouridx}				% Pre-superscript
\usepackage{float}					% Force float placement
\usepackage{graphicx}				% For images etc.
\usepackage[hidelinks]{hyperref}	% Clickable toc
\usepackage[usenames, dvipsnames]{xcolor}
\newcommand{\comment}[1]{\textcolor{RedOrange}{#1}}

\title{Summary of \\ TTK4130 Modeling and Simulation}
\author{Morten Fyhn Amundsen}
\affil{NTNU}

\begin{document}
\maketitle
\tableofcontents
\newpage

\newcommand{\enforall}{\enskip \forall \enskip}
\newcommand{\residual}{\operatornamewithlimits{Res}}
\newcommand{\V}[1]{\mathbf{#1}}							% Bold vector
\newcommand{\M}[1]{\bm{#1}}								% Matrix
\newcommand{\I}{\mathbf{I}}								% Identity matrix
\newcommand{\y}{\V{y}}									% y vector
\newcommand{\ydot}{\V{\dot{y}}}							% ydot vector
\newcommand{\R}{\M{R}}									% Rotation matrix
\newcommand{\T}{^{\text{T}}}							% Transpose
\newcommand{\half}{\nicefrac{1}{2}}						% Slanted 1/2 fraction
\newcommand{\nf}[2]{\nicefrac{#1}{#2}}					% Shorthand for slanted fractions
\newcommand{\angvel}{\boldsymbol{\omega}}
\newcommand{\gv}[1]{\boldsymbol{#1}}
\newcommand{\lagr}{\mathcal{L}}
\newcommand{\presuper}[1]{\fourIdx{#1}{}{}{}}

\renewcommand{\c}{\operatorname{c}}						% Shorthand for cos, used in large matrices
\newcommand{\s}{\operatorname{s}}						% Ditto sin
\renewcommand{\b}[1]{\textbf{#1}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Introduction}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
I can't be bothered to write much here. You know what you're doing.

\subsection{Todo}
\begin{itemize}
	\item Force and torque matrices \( \M{F}^b_{bc} \).
	\item \comment{p. 533}
\end{itemize}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Things that ought to be obvious}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\paragraph{Eigenvalues} Values of \( \lambda \) such that: \( \det{\lambda \I - \M{A}} = 0 \).
\paragraph{Eigenvectors} Vectors \( \V{v} \) such that: \( (\M{A} - \lambda \I)\V{v} = 0 \).
\paragraph{Differentiation of \(x^2\):} \(\od{x^2}{t} = 2 \dot{x} x\)
\paragraph{Relation of velocity to ang. vel.:} \(v = r \omega\)
\paragraph{Matrix transposes:} \( (\M{A} + \M{B})\T = \M{A}\T + \M{B}\T, \quad (\M{AB})\T = \M{B}\T \M{A}\T \)
\paragraph{Inverse of \(2 \times 2\) matrix:}
\[
\M{A}^{-1} =
\begin{pmatrix} a & b \\ c & d \end{pmatrix}^{-1} =
\frac{1}{\det \M{A}} \begin{pmatrix} d & -b \\ -c & a \end{pmatrix}
\]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Energy-flow modelling (19)}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
A modular, rather than monolithic approach to modelling a dynamic system. Modules are connected with one \emph{potential} and one \emph{flow} variable, similar to their physical connection. At a connection, potentials must be equal, and flows must sum to zero.

\begin{table}[H]
\small
\begin{tabular}{llll}
	\b{Domain}  & \b{Potential}           & \b{Flow}                                 & \b{Result} \\
	\hline
	Translation & Velocity [m/s]          & Force [N]                                & Power [W]  \\
	Rotation    & Angular velocity [/s]   & Torque [Nm]                              & Power [W]  \\
	Electrical  & Voltage [V]             & Current [A]                              & Power [W]  \\
	Magnetic    & Mag.mot. force [A]      & Mag. flux rate [V]                       & Power [W]  \\
	Hydraulic   & Pressure [Pa]           & Volume flow rate [m\(^3\)/s]             & Power [W]  \\
	Thermal     & Temperature [K]         & Heat flow rate [J/Ks]                    & Power [W]  \\
	Chemical    & Chem. potential [J/mol] & Molar flow rate [mol/s]                  & Power [W]
\end{tabular}
\end{table}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Energy (\emph{Lyapunov}) functions and passivity (46)}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\paragraph{Energy functions} describe the change of internal `energy' in a system. \emph{Decreasing energy implies stability.}

The time derivative of the energy function is
\begin{equation}
	\dot{V} = \pd{V}{t} + \pd{V}{\V{x}} \V{f}(\V{x}, \V{u}, t).
\end{equation}

\paragraph{Passivity} describes whether a system produces energy to its surroundings.
(Example: Power in a resistor: \(P = u i = R i^2 \implies P \geq 0 \implies\) Passive!)
\begin{itemize}
	\item Connected stable systems are not always stable...
	\item Connected passive systems are always passive, and therefore stable!
\end{itemize}
A system with input \(u\) and output \(y\) is passive if
\begin{equation}
	\int_0^t y(\tau) u(\tau) \dif \tau \geq - E_0 \enforall t \geq 0
\end{equation}
for all inputs.

\subsection{Positive realness of a transfer function (56)}
A system is passive iff its transfer function is positive real.
\paragraph{Definition:} The t.f. \(H(s)\) (rational or not) is positive real if:
\begin{enumerate}
	\item \(H(s)\) is analytic \( \enforall \Re[s] > 0 \)
	\item \(H(s)\) is real for all positive and real \(s\).
	\item \(\Re[H(s)] \geq 0 \enforall \Re [s] > 0\).
\end{enumerate}
But this is easier to use:
\paragraph{Theorem:} A rational, proper t.f. \(H(s)\) is positive real iff:\footnote{if and only if}
\begin{enumerate}
	\item \( H(s) \) has no poles in the right half plane.
	\item \( \Re[H(\jmath \omega)] \geq 0 \enforall \omega \) such that \( \jmath \omega \) is not a pole of \( H(s) \).
	\item If \( \jmath \omega_0 \) is a pole in \( H(s) \), it is a simple pole, and the residual in \( s = \jmath \omega_0 \) is positive and real, i.e.,
	\[ \residual_{s = \jmath \omega_0} H(s) = \lim_{s \to \jmath \omega_0} (s - \jmath \omega_0) H(s) > 0 \] (And some stuff about poles at infinity.)
\end{enumerate}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Simulation (509)}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Notation (517)}
\begin{equation}\label{eq:ivp}
	\underbrace{
		\underbrace{
			\V{\ydot} = \V{f}(\y, t)
		}_{
			\text{ODE}
		}
		,\quad \y(t_0) = \y_0
	}_{\text{IVP}}
\end{equation}
Given this, we want to approximate \( \V{y}(t) \).

\subsection{Error (517)}
The \emph{local solution} \( \y_L(t_n; t) \) is the exact solution of \eqref{eq:ivp} with initial condition \( \y_n \) at time \( t_n \):
\begin{equation}
	\ydot_L(t_n; t) = \V{f}[\y_L(t_n; t)], \quad \y_L(t_n; t_n) = \y_n
\end{equation}
I.e., if the system really is in state \( \y_n \) at time \( t_n \), the local solution is the true behaviour.

This lets us define the \emph{local error}: the difference between the computed solution and the local solution at \( t_{n+1} \):
\begin{equation}
	\V{e}_{n+1} = \y_{n+1} - \y_L(t_n; t_{n+1})
\end{equation}

The \emph{global error} is the difference between the computed solution and the exact solution at \( t_{n+1} \):
\begin{equation}
	\V{E}_{n+1} = \y_{n+1} - \y(t_{n+1})
\end{equation}

\subsection{Order of a one-step method (517)}
A method is of order \( p \) if \( \V{e}_{n+1} = O(h^{p+1}) \).

Given the IVP in \eqref{eq:ivp} and a one-step method (of step length \( h \))
\begin{equation}\label{eq:onestep}
	\y_{n+1} = \y_n + h \phi(\y_n, t_n),
\end{equation}
if then
\begin{equation}
	\y_{n+1} = \y_n + h \V{f}(\y_n, t) + \frac{h^2}{2} \od{\V{f}(\y_n, t)}{t} + \hdots + \frac{h^p}{p!} \od[p-1]{\V{f}(\y_n, t)}{t} + O(h^{p+1})
\end{equation}
holds, the local error is \( O(h^{p+1}) \) and the method is of order \( p \).

\subsection{Linearisation (518)}
The linearisation of \eqref{eq:ivp} around \( \y^* \) is
\begin{equation}
	\Delta \ydot = \M{J} \Delta \y,
\end{equation}
where \( \M{J} \) is
\begin{equation}
	\M{J}
	= \pd{\V{f}(\y,t)}{\y} \biggr\rvert_{\y = \y^*}
	= \left\{ \dpd{f_i(\y,t)}{y_j} \biggr\rvert_{\y = \y^*} \right\}.
\end{equation}

\subsection{Stability (531)}
\subsubsection{Scalar test system}
Given the scalar test system
\begin{equation}
	\dot{y} = \lambda y
\end{equation}
we get
\begin{equation}
	y_{n+1} = R(h \lambda) y_n
\end{equation}
where \( R(h \lambda )\) is the stability function of a numerical method.
\subsubsection{Requirement for stability}
The numerical solution \( \Delta \y_n \) of the linearised system is stable if:
\begin{equation}
	| R(h\lambda_i) | \leq 1
\end{equation}
for all eigenvalues \( \lambda_i \) of \( \M{J} \).

\subsection{Explicit Runge-Kutta methods (ERK) (526)}
Given the IVP in \eqref{eq:ivp} and the generic one-step method in \eqref{eq:onestep}, the general ERK is
\begin{equation}
	\begin{aligned}
		\V{k}_1 &= \V{f}(\y_n, \enspace t_n) \\
		\V{k}_2 &= \V{f}(\y_n + h a_{21}\V{k}_1, \enspace t_n + c_2 h) \\
		\V{k}_3 &= \V{f}(\y_n + h (a_{31}\V{k}_1 + a_{32} \V{k}_2), \enspace t_n + c_3 h) \\
				& \enspace \vdots \\
		\V{k}_\sigma &= \V{f}(\y_n + h (a_{\sigma 1}\V{k}_1 + \hdots + a_{\sigma,\sigma-1}\V{k}_{\sigma-1}), \enspace t_n + c_\sigma h) \\
		\y_{n+1} &= \y_n + h (b_1 \V{k}_1 + \hdots + b_\sigma \V{k}_\sigma).
	\end{aligned}
\end{equation}
The weights can be arranged in a \emph{Butcher array}:
\begin{equation}
\begin{array}{c|ccccc}
0        &              &              &        &                      &          \\
c_2      & a_{21}       &              &        &                      &          \\
c_3      & a_{31}       & a_{32}       &        &                      &          \\
\vdots   & \vdots       & \vdots       & \ddots &                      &          \\
c_\sigma & a_{\sigma 1} & a_{\sigma 2} & \hdots & a_{\sigma, \sigma-1} &          \\ \hline
         & b_1          & b_2          & \hdots & b_{\sigma-1}         & b_\sigma \\
\end{array}
\quad \text{or} \quad
\begin{array}{c|c}
    \V{c} & \M{A}   \\ \hline
          & \V{b}\T \\
\end{array}
\end{equation}
Where \( \sigma \) is the number of stages.

\subsubsection{ERK stability}
The stability function of an ERK is
\begin{equation}
	R_E(h\lambda) = \det \left[ \I -\lambda h \left( \M{A} - \V{1 b}\T \right) \right]
\end{equation}
where \(\M{A}\) and \(\V{b}\) are given by the Butcher array.

For ERKs of order \( p = \sigma \leq 4 \):
\begin{equation}
	R_E(h \lambda) = 1 + h \lambda + \dots + \frac{h^p \lambda^p}{p!}
\end{equation}
Valid for Euler's method, Modified Euler, RK4, and at least one other. These are the methods that are actually used.

\subsubsection{Euler's method (Forward Euler)}
\begin{equation}
	\y_{n+1} = \y_n + h\V{f}(\y_n, t_n)
	\iff
	\begin{array}{c|c}
		0 &   \\ \hline
		  & 1 \\
	\end{array}
\end{equation}

\subsubsection{Improved Euler}
\begin{equation}
	\begin{array}{rcl}
		\V{k}_1  &=& \V{f}(\y_n, t_n) \\
		\V{k}_2  &=& \V{f}(\y_n + h \V{k}_1, t_n + h) \\
		\y_{n+1} &=& \y_n + \frac{h}{2}(\V{k}_1 + \V{k}_2) \\
	\end{array}
	\iff
	\begin{array}{c|cc}
		0 &       &       \\
		1 & 1     &       \\ \hline
		  & \half & \half \\
	\end{array}
\end{equation}

\subsubsection{Modified Euler (Explicit midpoint)}
\begin{equation}
	\begin{array}{rcl}
		\V{k}_1  &=& \V{f}(\y_n, t_n) \\
		\V{k}_2  &=& \V{f}(\y_n + \frac{h}{2}\V{k}_1, t_n + \frac{h}{2}) \\
		\y_{n+1} &=& \y_n + h\V{k}_2 \\
	\end{array}
	\iff
	\begin{array}{c|cc}
		0     &       &   \\
		\half & \half &   \\ \hline
		      & 0     & 1 \\
	\end{array}
\end{equation}

\subsubsection{Heun's method}
\begin{equation}
	\begin{array}{c|ccc}
		0         &           &           &           \\
		\nf{1}{3} & \nf{1}{3} &           &           \\
		\nf{2}{3} & 0         & \nf{2}{3} &           \\
		\hline
		          & \nf{1}{4} & 0         & \nf{3}{4} \\
	\end{array}
\end{equation}

\subsubsection{Runge-Kutta 4}
\begin{equation}
    \begin{array}{c|cccc}
		0     &           &           &           &           \\
		\half & \half     &           &           &           \\
		\half & 0         & \nf{1}{2} &           &           \\
		1     & 0         & 0         & 1         &           \\ \hline
              & \nf{1}{6} & \nf{2}{6} & \nf{2}{6} & \nf{1}{6} \\
    \end{array}
\end{equation}

\subsection{Implicit Runge-Kutta methods (IRK) (534)}
Defined as
\begin{equation}
\begin{aligned}
\V{k}_1      &= \V{f}(\y_n + h(a_{11}\V{k}_1 + \hdots + a_{1 \sigma}\V{k}_\sigma), \enspace t_n + c_1 h) \\
             &  \enskip \vdots \\
\V{k}_\sigma &= \V{f}(\y_n + h(a_{\sigma 1}\V{k}_1 + \hdots + a_{\sigma \sigma}\V{k}_\sigma), \enspace t_n + c_\sigma h) \\
\y_{n+1}     &= \y_n + h (b_1\V{k}_1 + \hdots + b_\sigma\V{k}_\sigma)
\end{aligned}
\end{equation}

\begin{equation}
\begin{array}{c|ccccc}
c_1      & a_{11}       & a_{12}       & \dots  & \dots               & a_{1\sigma}      \\
c_2      & a_{21}       & a_{22}       & \dots  & \dots               & a_{1\sigma}      \\
\vdots   & \vdots       & \vdots       & \ddots & \ddots              & \vdots           \\
c_\sigma & a_{\sigma 1} & a_{\sigma 2} & \dots  & a_{\sigma,\sigma-1} & a_{\sigma\sigma} \\ \hline
         & b_1          & b_2          & \dots & b_{\sigma-1}         & b_\sigma         \\
\end{array}
\quad \text{or} \quad
\begin{array}{c|c}
    \V{c} & \M{A}   \\ \hline
          & \V{b}\T \\
\end{array}
\end{equation}
IRKs efficiently solve stiff systems (i.e. systems with a large spread in eigenvalues). ERKs do not. Stability is generally the motivation for using IRKs, not accuracy.

\subsubsection{IRK stability}
The stability of an IRK is given by
\begin{align}
	R(h\lambda)	&= 1 + \lambda h \V{b}\T ( \I - h \lambda \M{A} )^{-1} \V{1} \\
				&= \frac{
					\det \left( \I - \lambda h \left[ \M{A} - \V{1b}\T \right] \right)
					}{
					\det ( \I - \lambda h \M{A} )
					}
\end{align}

\subsubsection{Implicit Euler (Radau IIA)}
\begin{equation}
	\begin{array}{rcl}
		\V{k}_1  &=& \V{f}(\y_n + h \V{k}_1, t_{n+1}) \\
		\y_{n+1} &=& \y_n + h \V{k}_1 \\
	\end{array}
	\iff
	\begin{array}{c|c}
		1 & 1 \\ \hline
		  & 1 \\
	\end{array}
\end{equation}
This is stable for all eigenvalues \emph{outside} a unit circle with center \((1, 0)\)!

\subsection{A- and L-stability (546)}
\paragraph{A-stability:} A method is A-stable if \( |R(\lambda h)| \leq 1 \enforall \Re(\lambda) \leq 0 \).

This implies that the method is stable for all stable test systems. Thus, it is also stable for systems with very fast dynamics compared to \(h\), but note that aliasing can occur. High frequency oscillations appear in the solution as oscillations slower than the Nyquist frequency \( \frac{\pi}{h}\).

Note that no explicit methods are A-stable.

\paragraph{L-stability:} A method is L-stable if it is A-stable, and \(\lim_{\omega \rightarrow \infty} |R(\jmath \omega h)| = 0\) for all systems \( \dot{y} = \lambda y \) where \( \lambda = \jmath \omega \).

L-stable methods dampen out the inaccurate, fast dynamics that can occur with A-stable methods.

\subsection{Padé-approximations (548)}
The local solution of the test system \( \dot{y} = \lambda y \) over a time step, and the numerical solution of the same system, is
\begin{gather}
	\dot{y}_L(t_n; t_{n+1}) = e^{\lambda h} y_n \\
	y_{n+1} = R(\lambda h) y_n
\end{gather}
The accuracy of the numerical solution depends on how well \( R(\lambda h) \) approximates the exponential function.

A Padé approximation \( P^k_m(s) \) is the best rational approximation of \( e^s \) with a numerator of order \(k\) and a denominator of order \(m\).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Rotation matrices (218)}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Vectors (209)}
The vector
\begin{equation}
	\vec{v} = v_1 \vec{a_1} + v_2 \vec{a_2} + v_3 \vec{a_3}
\end{equation}
has `built in' information about its frame of reference (it is \emph{coordinate free}). However, the coordinate vector
\begin{equation}
	\V{u}^a =
	\begin{pmatrix}
		u_1 & u_2 & u_3 \\
	\end{pmatrix}
	\T
\end{equation}
does not, and we must indicate which frame of reference it is given in (frame \(a\) in this case).

\subsection{Skew-symmetric form (211)}
\begin{equation}
	\V{u}^\times :=
	\begin{pmatrix}
		0    & -u_3 & u_2  \\
		u_3  & 0    & -u_1 \\
		-u_2 & u_1  & 0    \\
	\end{pmatrix}
\end{equation}

\subsection{Vector cross product (211)}
\begin{equation}
	\V{w} = \V{u}^\times \V{v}
	\iff
	\vec{w} = \vec{u} \times \vec{v}
\end{equation}
Alternatively:
\begin{equation}
	\V{w} = \V{u}^\times \V{v} =
	\begin{pmatrix}
		u_2 v_3 - u_3 v_2 \\
		u_3 v_1 - u_1 v_3 \\
		u_1 v_2 - u_2 v_1 \\
	\end{pmatrix}
\end{equation}

\subsection{Properties of the rotation matrix (219)}
\paragraph{Notation:} \( \V{v}^a = \begin{pmatrix} v_1^a & v_2^a & v_3^a \end{pmatrix}\T \) is vector \( \V{v} \) given in the coordinates of \(a\).

\begin{equation}
	\begin{aligned}
		\V{v}^a				&= \R_b^a \V{v}^b \\
		\R_a^b				&= (\R_b^a)^{-1} = (\R_b^a)\T \\
		\R^a_c				&= \R^a_b \R^b_c \\
		(\V{u}^b)^\times	&= \R^b_a (\V{u}^a)^\times \R^a_b \\
	\end{aligned}
\end{equation}
\paragraph{Definition:} A matrix \(\R\) is a rotation matrix iff \(\R \in SO(3)\):
\begin{equation}
	SO(3) = \lbrace \R \enspace |
	\enspace \R \in \mathbb{R}^{3 \times 3},
	\enspace \R\T\R = \I,
	\enspace \det \R = 1 \rbrace
\end{equation}

\subsection{Simple rotations (221)}
A \emph{simple rotation} is a rotation about a fixed axis. Rotation matrices for rotation around the \( x \), \( y \), and \( z \) axes, respectively, are as follows:
\begin{gather}\label{eq:simplerot}
	\R_x(\phi) =
	\begin{pmatrix}
		1 & 0         & 0          \\
		0 & \cos \phi & -\sin \phi \\
		0 & \sin \phi & \cos \phi  \\
	\end{pmatrix} \\
	\R_y(\theta) =
	\begin{pmatrix}
		\cos \theta  & 0 & \sin \theta \\
		0            & 1 & 0           \\
		-\sin \theta & 0 & \cos \theta \\
	\end{pmatrix} \\
	\R_z(\psi) =
	\begin{pmatrix}
		\cos \psi & -\sin \psi & 0 \\
		\sin \psi & \cos \psi  & 0 \\
		0         & 0          & 1 \\
	\end{pmatrix}
\end{gather}

\subsection{Euler angles (224)}
A parametrisation of rotation about three axes. Three parameters \( \psi, \theta, \phi \). Singularities exist.
\paragraph{Roll, pitch, yaw:} A rotation \( \psi \) about the \( z \)-axis, then \( \theta \) about the (rotated) \( y \)-axis, then \( \phi \) about the (also rotated) \( x \)-axis.
\begin{equation}
	\begin{aligned}
		\R^a_b &= \R_z(\psi) \R_y(\theta) \R_{\underline{x}}(\phi) \\
		&=
		\begin{pmatrix}
			\c\psi \s\theta & \c\psi \s\theta \s\phi - \s\psi \c\phi & \s\psi \s\phi + \c\psi \s\theta \c\phi \\
			\s\psi \c\theta & \c\psi \c\phi + \s\psi \s\theta \s\phi & \s\psi \s\theta \c\phi - \c\psi \s\phi \\
			-\s\theta       & \c\theta \s\phi                        & \c\theta \c\phi                        \\
		\end{pmatrix}
	\end{aligned}
\end{equation}
\paragraph{Classical Euler angles:} A rotation \( \psi \) about the \( z \)-axis, then \( \theta \) about the (rotated) \( y \)-axis, then \( \phi \) about the (also rotated) \( z \)-axis.
\begin{equation}
	\begin{aligned}
	\R^a_b &= \R_z(\psi) \R_y(\theta) \R_{\underline{z}}(\phi) \\
	&=
	\begin{pmatrix}
		\c\psi \c\theta \c\phi - \s\psi \s\phi & -\c\psi \c\theta \s\phi - \s\psi \c\phi & \c\psi \s\theta \\
		\s\psi \c\theta \c\phi + \c\psi \s\phi & \c\psi \c\phi - \s\psi \c\theta \s\phi  & \s\psi \s\theta \\
		-\s\theta \c\phi                       & \s\theta \s\phi                         & \c\theta        \\
	\end{pmatrix}
	\end{aligned}
\end{equation}

\subsection{Angle-axis description of rotation (226)}
Describes a rotation by an axis of rotation \( \V{k} \) about which we move by an angle \( \theta \) (four parameters \( k_1, k_2, k_3, \theta \)).
\begin{equation}
	\R^a_b \V{k} = \V{k}, \quad \V{k}^a = \V{k}^b = \V{k}
\end{equation}
With this method, the rotation matrix becomes
\begin{equation}
	\R^a_b = \cos \theta \I
	+ \sin \theta (\V{k}^a)^\times
	+ (1-\cos \theta) \V{k}^a(\V{k}^a)\T
\end{equation}

\subsection{Euler parameters (231)}
The parameters
\begin{equation}
	\eta = \cos \frac{\theta}{2}, \quad
	\gv{\epsilon} = \V{k} \sin \frac{\theta}{2}
\end{equation}
lead to the rotation matrix
\begin{equation}
	\R_e(\eta, \gv{\epsilon}) = \I
	+ 2 \eta \gv{\epsilon}^\times
	+ 2 \gv{\epsilon}^\times \gv{\epsilon}^\times
	.
\end{equation}
This is nice, because:
\begin{itemize}[nosep]
	\item No singularities!
	\item No trigonometry!
	\item \( \eta^2 + \vec{\epsilon} \cdot \vec{\epsilon} = 1 \): Easy to normalise (avoid roundoff errors).
\end{itemize}

\subsection{Homogenous transformation matrices (223)}
The position and orientation of frame \( b \) relative to frame \( a \) is given by the homogenous transformation matrix
\begin{equation}
	\M{T}^a_b =
	\begin{pmatrix}
		\R^a_b  & \V{r}^a_{ab} \\
		\V{0}\T & 1
	\end{pmatrix}
	\in SE(3)
\end{equation}
where \( \V{r}^a_{ab} \) is the position of frame \( b \) relative to frame \( a \), expressed in the coordinates of frame \( a \). (Position as in position of the origin.)
The set \(SE(3)\) is defined as:
\begin{equation}
	SE(3) = \left\lbrace \M{T} \enspace |
	\enspace \M{T} = \begin{pmatrix} \R & \V{r} \\ \V{0}\T & 1 \end{pmatrix},
	\enspace \R \in SO(3),
	\enspace \V{r} \in \mathbb{R}^3
	\right\rbrace
\end{equation}

\subsection{Angular velocity (239)}
This is super funky and magical: The vector \( \angvel^a_{ab} \) defined by satisfying
\begin{equation}
	( \angvel^a_{ab} )^\times =
	\M{\dot{R}}^a_b ( \R^a_b )\T
\end{equation}
turns out to represent the angular velocity of \( b \) relative to \( a \).

Then, the kinematic differential equations for the rotation matrix is given in two forms
\begin{equation}
	\begin{aligned}
		\M{\dot{R}}^a_b &= (\angvel^a_{ab})^\times \R^a_b \\
		\M{\dot{R}}^a_b &= \R^a_b (\angvel^b_{ab})^\times
	\end{aligned}
\end{equation}

\paragraph{Example} (See \eqref{eq:simplerot} for the definition of \( \R_x \).)
\begin{equation}
	\M{\dot{R}}_x(\phi) \R_x\T(\phi) =
	\begin{pmatrix}
		0 & 0       & 0       \\
		0 & -\s\phi & -\c\phi \\
		0 &  \c\phi & -\s\phi \\
	\end{pmatrix}
	\dot{\phi}
	\underbrace{
		\begin{pmatrix}
			1 &  0      & 0      \\
			0 &  \c\phi & \s\phi \\
			0 & -\s\phi & \c\phi \\
		\end{pmatrix}
	}_{\R_x\T(\phi)}
	=
	\begin{pmatrix}
		0 & 0          & 0           \\
		0 & 0          & -\dot{\phi} \\
		0 & \dot{\phi} & 0           \\
	\end{pmatrix}
\end{equation}
which gives \( \angvel_x = \begin{pmatrix} \dot{\phi} & 0 & 0 \end{pmatrix}\T \).
Similarly, \( \angvel_y = \begin{pmatrix} 0 & \dot{\theta} & 0 \end{pmatrix}\T \)
and \( \angvel_z = \left( 0 \enskip 0 \enskip \dot{\psi} \right) \T \)

\subsubsection{Simple rotation}
Angular velocity \( \vec{\omega}_{ab} \) about the axis of rotation \( \vec{k} \) is:
\begin{equation}
	\vec{\omega}_{ab} = \dot{\theta} \vec{k}
\end{equation}

\subsubsection{Composite rotation}
Angular velocity of a composite rotation matrix \( \R^a_d = \R^a_b \R^b_c \R^c_d \)  is
\begin{equation}
	\vec{\omega}_{ad} = \vec{\omega}_{ab} + \vec{\omega}_{bc} + \vec{\omega}_{cd}
\end{equation}
which on coordinate form is
\begin{equation}
	\begin{aligned}
		\angvel^a_{ad} &= \angvel^a_{ab} +        \angvel^a_{bc} +        \angvel^a_{cd} \\
		               &= \angvel^a_{ab} + \R^a_b \angvel^b_{bc} + \R^a_b \R^b_c \angvel^c_{cd} \\
	\end{aligned}
\end{equation}

\subsubsection{Differentiation of vectors}
\begin{gather}
	\V{\dot{u}}^a = \R^a_b \left[ \V{\dot{u}}^b + (\angvel^b_{ab})^\times \V{u}^b \right] \\
	\frac{\fourIdx{a}{}{}{}\dif}{\dif t} \vec{u} = \frac{\fourIdx{b}{}{}{}\dif}{\dif t} \vec{u} + \vec{\omega}_{ab} \times \vec{u}
\end{gather}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Kinematic differential equations (244)}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Rotational deviation cannot be described by subtraction. Instead, it is described by
\begin{equation}
	\widetilde{\R}_a := \R \R_d\T \implies \R = \widetilde{\R}_a \R_d
	, \quad \widetilde{\R}_a \in SO(3)
\end{equation}
where \( \R = \R^a_b \) is the orientation and \( \R_d \) is the desired orientation.

Then the kinematic differential equations for the deviation are
\begin{gather}
	\widetilde{\angvel}^a = \angvel^a - \angvel^a_d \\
	\dod{}{t} \widetilde{\R}_a = \left( \widetilde{\angvel}^a \right)^\times \widetilde{\R}_a
\end{gather}

\subsection{Rigid body kinematics (259)}
\subsubsection{Configuration}
\begin{figure}[H]
	\centering
	\includegraphics{rigid_body}
\end{figure}

The orientation \(\R^i_b\) of a rigid body and the position \(\vec{r}_o\) of a point \(o\) in the body, both in relation to a reference frame \(i\), define its configuration. The position of any point \(p\) in the body is given by
\begin{equation}
	\vec{r}_p = \vec{r}_o + \vec{r}.
\end{equation}
In the reference frame, \(\vec{r}\) is
\begin{equation}
	\V{r}^i = \R^i_b \V{r}^b
\end{equation}

\subsubsection{Velocity}
Definition:
\begin{equation}
	\vec{v}_o := \frac{\presuper{i}\dif}{\dif t} \vec{r}_o, \quad
	\vec{v}_p := \frac{\presuper{i}\dif}{\dif t} \vec{r}_p
\end{equation}
Alternatively:
\begin{equation}
	\vec{v}_p = \vec{v}_o + \frac{\presuper{b} \dif}{\dif t} \vec{r} + \vec{\omega}_{ib} \times \vec{r}
\end{equation}

\subsubsection{Acceleration}
Definition (translation):
\begin{equation}
	\vec{a}_o := \frac{\presuper{i}\dif\,^2}{\dif t^2} \vec{r}_o, \quad
	\vec{a}_p := \frac{\presuper{i}\dif\,^2}{\dif t^2} \vec{r}_p
\end{equation}
Definition (rotation):
\begin{equation}
	\vec{\alpha}_{ib} := \frac{\presuper{i}\dif\,^2}{\dif t^2} \vec{\omega}_{ib}
	= \frac{\presuper{b}\dif\,^2}{\dif t^2} \vec{\omega}_{ib}
\end{equation}
In terms of accelerion, ang. acceleration and velocities:
\begin{multline}
		\underbrace{\vec{a}_p}_{\text{Acceleration of \(p\)}} =
		\underbrace{\vec{a}_o}_{\text{Acceleration of \(o\)}} +
		\underbrace{\frac{\presuper{b}\dif\,^2}{\dif t^2} \vec{r}}_{\text{Second derivative of \(\vec{r}\) in \(b\)}} \\ +
		\underbrace{2 \vec{\omega}_{ib} \times \frac{\presuper{b}\dif}{\dif t} \vec{r} }_{\text{Coriolis acceleration}} +
		\underbrace{\vec{\alpha}_{ib} \times \vec{r}}_{\text{Transversal acceleration}} +
		\underbrace{\vec{\omega}_{ib} \times \left( \vec{\omega}_{ib} \times \vec{r} \right)}_{\text{Centripetal acceleration}}
\end{multline}
Alternatively:
\begin{equation}
	\vec{a}_p =
	\frac{\presuper{b}\dif}{\dif t} \vec{v}_o +
	\vec{\omega}_{ib} \times \vec{v}_o +
	\frac{\presuper{b}\dif\,^2}{\dif t^2} \vec{r} +
	2 \vec{\omega}_{ib} \times \frac{\presuper{b}\dif}{\dif t} \vec{r} +
	\vec{\alpha}_{ib} \times \vec{r} +
	\vec{\omega}_{ib} \times \left( \vec{\omega}_{ib} \times \vec{r} \right)
\end{equation}
As well as:
\begin{equation}
	\vec{v}_p = \vec{v}_o + \vec{\omega}_{ib} \times \vec{r},
	\quad \vec{r} \text{ fixed in } b
\end{equation}

\subsection{EoM for rigid body (269)}
\begin{equation}
	\begin{aligned}
		\vec{F}_{bc} &= m \vec{a}_c \\
		\vec{T}_{bc} &= \vec{M}_{b/c} \cdot \vec{\alpha}_{ib}
		+ \vec{\omega}_{ib} \times ( \vec{M}_{b/c} \cdot \vec{\omega}_{ib}) \\
	\end{aligned}
\end{equation}
where
\begin{itemize}
	\item \( \vec{F}_{bc} \) is the force on body \( b \) acting through the centre of mass.
	\item \( \vec{T}_{bc} \) is the torque, or moment about the centre of mass.
	\item \( \vec{M}_{b/c} \) is the inertia dyadic of \( b \) about \( c \).
\end{itemize}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Lagrangian dynamics (313)}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Lagrange versus Newton-Euler (313)}

\begin{center}
	\begin{tabular}{rl}
		\b{Newton-Euler} & \b{Lagrange} \\
		\hline
		Vectors & Algebra \\
		Forces and moments & Energy and work \\
		Must consider all forces & Forces of constraint eliminated \\
		Somewhat complicated & Easier to do by hand \\
		Suitable for computers & Less suitable for computers \\
	\end{tabular}
\end{center}

Newton-Euler is based on Newton's (second) law and it's exention to rotational dynamics. Lagrangian equations of motion are instead based on algebraic operations on energy expressions, and are better suited for things related to energy conservation and passivity.

Lagrangean methods make sense when there are guiding forces involved.

\subsection{Lagrange EoM (315)}
The Lagrangian is
\begin{equation}
	\lagr(\V{q}, \V{\dot{q}}, t) = T(\V{q}, \V{\dot{q}}, t) - U(\V{q})
\end{equation}
and the EoMs are
\begin{equation}
	\od{}{t} \left( \dpd{\lagr}{\dot{q}_i} \right) - \pd{\lagr}{q_i} = \tau_i
\end{equation}
where \(\tau_i\) is the generalised actuator force.

\section{Reynolds' transport theorem (413)}
Time-variant volume:
\begin{equation}
	\od{}{t} \iiint_{V_c(t)} \phi(\V{x}, t) \dif V
	= \iiint_{V_c(t)} \pd{\phi(\V{x}, t)}{t} \dif V
	+ \iint_{\partial V_c(t)} \phi \vec{v}_c \cdot \vec{n} \dif A
\end{equation}
Material volume:
\begin{equation}
	\od{}{t} \iiint_{V_m(t)} \phi(\V{x}, t) \dif V
	= \iiint_{V_m(t)} \pd{\phi(\V{x}, t)}{t} \dif V
	+ \iint_{\partial V_m(t)} \phi \vec{v} \cdot \vec{n} \dif A
\end{equation}
Material derivative:
\begin{equation}
	\frac{\Dif}{\Dif t} \iiint_{V_c(t)} \phi(\V{x}, t) \dif V
	:= \iiint_{V_c(t)} \pd{\phi(\V{x}, t)}{t} \dif V
	+ \iint_{\partial V_c(t)} \phi \vec{v} \cdot \vec{n} \dif A
\end{equation}
Differential formulation, material form:
\begin{equation}
	\frac{\Dif}{\Dif t} \iiint_{V_c(t)} \phi(\V{x}, t) \dif V
	= \iiint_{V_c(t)} \frac{\Dif \phi(\V{x}, t)}{\Dif t}
	+ \phi(\V{x}, t) \left( \vec{\nabla} \cdot \vec{v} \right) \dif V
\end{equation}
Differential formulation, divergence form:
\begin{equation}
	\frac{\Dif}{\Dif t} \iiint_{V_c(t)} \phi(\V{x}, t) \dif V
	= \iiint_{V_c(t)} \pd{\phi(\V{x}, t)}{t}
	+ \vec{\nabla} \cdot \left( \phi(\V{x}, t) \vec{v} \right) \dif V
\end{equation}
Integral formulation:
\begin{equation}
	\od{}{t} \iiint_{V_f} \phi(\V{x}, t) \dif V
	= \frac{\Dif}{\Dif t} \iiint_{V_f} \phi(\V{x}, t) \dif V
	- \iint_{\partial V_f} \phi \vec{v} \cdot \vec{n} \dif A
\end{equation}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Mass balance (417)}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Integral formulation:
\begin{equation}
	\od{}{t} \iiint_{V_f} \rho(\V{x}, t) \dif V
	= - \iint_{\partial V_f} \rho \vec{v} \cdot \vec{n} \dif A
\end{equation}
Differential formulation:
\begin{equation}
	\pd{\rho(\V{x}, t)}{t}
	+ \vec{\nabla} \cdot ( \rho(\V{x}, t) \vec{v})
	= 0
\end{equation}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Some mechanics (141)}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\paragraph{Valve equation (141)}
\begin{equation}
	q = C_d A \sqrt{\frac{2}{\rho} \Delta p}
\end{equation}

\paragraph{Bulk modulus (151)}
\begin{equation}
	\frac{\dif \rho}{\rho} = \frac{\dif p}{\beta}
\end{equation}

\paragraph{Spring}
\begin{gather}
	F = - k x \\
	E_{pot} = \frac{1}{2} k x^2
\end{gather}

\end{document}

TTK4130 Modelling and simulation/TTK4130-Summary.tex


\documentclass[10pt,mathserif]{beamer}

\usepackage{beamerthemesplit}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm,listings,cite}
\usepackage{graphicx}
%\usepackage[latin1]{inputenc}

\useoutertheme{infolines}

\title{SMT Solver Theory and Applications}

\author{Dionna Glaze}

\date{November 18, 2009}

\definecolor{blue}{rgb}{0,0,1.0}
\newcommand{\blue}[1]{{\color{blue}#1}}
\definecolor{green}{rgb}{0,1.0,0}
\newcommand{\green}[1]{{\color{green}#1}}
\definecolor{red}{rgb}{1.0,0,0}
\newcommand{\red}[1]{{\color{red}#1}}
\definecolor{grey}{rgb}{0.6,0.6,0.6}
\newcommand{\grey}[1]{{\color{grey}#1}}

\AtBeginSection[]
{
  \begin{frame}<beamer>
    \frametitle{Outline}
    \tableofcontents[currentsection]
  \end{frame}
}

\begin{document}

\frame{\titlepage}

\frame{
\frametitle{Outline}
\tableofcontents
}

\section{Vocabulary and Preliminaries}

\frame{
\frametitle{What is the SMT problem?}
SMT stands for Satisfiability Modulo Theories, and is essentially a generalization of the SAT problem. \\
We say ``modulo theories'' because the Boolean predicates of SAT are now first order sentences in a logic.
\begin{itemize}
\item At least NP-complete and at most unbounded complexity problem with applications in AI, formal methods
\item Input usually given in SMT-LIB format (CNF with sugar)
\end{itemize}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\frame{
\frametitle{Definitions}
\begin{itemize}
 \item A theory $T$ is a set of first order sentences.
 \item A formula $F$ is \emph{$T$-satisfiable} or \emph{$T$-consistent} if $F\wedge T$ is satisfiable in the first order sense. Otherwise $F$ is \emph{$T$-inconsistent}.
 \item A partial assignment $M$ is a $T$-model of a formula $F$ if $M$ is a $T$-consistent partial assignment and $M\models F$ (in the propositional sense).
 \item For two formulas $F$ and $G$, we say $F \models_T G$ if $F \wedge \neg G$ is $T$-inconsistent.
 \item A \emph{theory lemma} is a clause $C$ such that $\emptyset \models_T C$.
 \item A \emph{$T$-solver} is a decision$^*$ procedure that decides the $T$-satisfiability of conjunctions of ground literals.
\end{itemize}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\frame{
\frametitle{A Short Look at DPLL}
Based on the idea of unit propagation:\\
$M\parallel F, C\vee l \Rightarrow M l \parallel F,C\vee l \mbox{ if }\left\{\begin{array}{l}M\models \neg C \\ l \mbox{ is undefined in } M\end{array}\right.$ \\
conflict-driven backjumping: \\
$M l^d N \parallel F,C \Rightarrow M l' \parallel F,C \mbox{ if }\left\{\begin{array}{l}
M l^d N \models \neg C \mbox{ and there is}\\
\mbox{some clause } C'\vee l'\mbox { such that:} \\
\quad F,C\models C'\vee l' \mbox{ and } M\models \neg C', \\
\quad l' \mbox{ is undefined in } M, \mbox{ and } \\
\quad l' \mbox{ or } \neg l' \mbox{occurs in } F \mbox{ or in } M l^d N
\end{array}\right.$ \\
and conflict-driven learning: \\
$M \parallel F \Rightarrow M \parallel F, C \mbox{ if }\left\{\begin{array}{l}\mbox{each atom of } C\mbox{ occurs in }F \mbox{ or in } M \\ F\models C\end{array}\right.$
}

\section{Previous Strategies}

\subsection{Eager Approaches}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\frame{
\frametitle{Propositional Translation}
Satisfiability-preserving translation to a propositional CNF formula. \\
\pause
Pros:
\begin{itemize}
 \item Easy to do translations.
 \item Leverages the existant SAT-solving technology.
\end{itemize}
\pause
Cons:
\begin{itemize}
 \item Not all theories can be translated this way.
 \item Translation causes unacceptable blow-up in problem size.
 \item Search starts only after entire problem is translated.
 \item Size of the problem usually consumes all resources before starting the search.
\end{itemize}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\frame{
\frametitle{Calling External SAT Solver}
The $T$-solver calls a SAT solver on the formula to get a (propositionally) satisfying assignment and checks its $T$-consistency. If inconsistent, the conflicting clause is added to the formula and sent back to the SAT solver. \\
\pause
Pros:
\begin{itemize}
 \item Only have to write the $T$-solver.
 \item Again leverages the existant SAT-solving technology.
\end{itemize}
\pause
Cons:
\begin{itemize}
 \item Search must complete entirely before $T$-inconsistency is reported.
 \item Search must start over at the beginning if last assignment failed.
\end{itemize}

}

\subsection{Lazy Approaches}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\frame{
\frametitle{Incremental $T$-solving}
Communicates with the DPLL module to inform of $T$-inconsistency before an entire model is constructed, either at every decision or on every $k$ decisions. \\
\pause
Pros:
\begin{itemize}
 \item Early pruning of search space.
\end{itemize}
\pause
Cons:
\begin{itemize}
 \item Not always effective. The $T$-solver should be faster in processing one additional input literal than in reprocessing from scratch, but for some theories this is impossible.
 \item Finding the ``sweet spot'', or the right $k$ for the best performance is guess work.
\end{itemize}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\frame{
\frametitle{On-line SAT solving}
Builds off the incremental approach by allowing the $T$-solver to generate conflicting clauses to aid with backjumping. \\
\pause
Pros:
\begin{itemize}
 \item Early pruning
 \item More aggressive pruning.
\end{itemize}
\pause
Cons:
\begin{itemize}
 \item Conflicting clauses hard to generate.
\end{itemize}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\frame{
\frametitle{Theory Propagation}
Guides the search process by taking the current partial assignment and deriving other subterms of the formula. $T$-solver is no longer a \textit{validator} for the DPLL search. \\
\pause
Pros:
\begin{itemize}
 \item Analagous to the importance of unit propagation in DPLL.
 \item For many theories, this process exhaustively executed gives a great increase of performance.
 \item Exhaustively executed, this eliminates the need for unit propagation on theory lemmas.
\end{itemize}
\pause
Cons:
\begin{itemize}
 \item Conflict analysis highly non-trivial.
 \item If not performed exhaustively, duplicate results are extraneously generated.
\end{itemize}

}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{The DPLL(T) Framework}
\frame{
\frametitle{Transactions between DPLL and T-Solver}
Sufficient communication for an incremental on-line solver with theory propagation is given in the following set of messages:
\begin{itemize}
 \item Notify $T$-Solver that a certain literal has been set to true.
\pause
 \item Ask $T$-Solver to check the current partial assignment is $T$-inconsistent (with \textit{strength}) and give an \textit{explanation}.
\pause
 \item Ask $T$-Solver to identify currently undefined input literals that are $T$-consequences of $M$.
\pause
 \item Ask $T$-Solver to provide a justification for a $T$-entailment of a theory-propagated literal for conflict clause learning.
\pause
 \item Ask $T$-Solver to undo the last $n$ notifications that a literal has been set to true.
\end{itemize}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Useful Theories}
\frame{
\frametitle{Equality of Uninterpreted Functions (EUF)}
Finds basic unsatisfiable errors such as
\begin{equation*}
(f(f(a))\neq b \vee f(f(f(b)))\neq b) \wedge f(a) =a \wedge a = b
\end{equation*}
by using a congruence closure algorithm to create congruence classes and checking the results against a list of suspected equalities and disequalities, inconsistencies are found.
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\frame{
\frametitle{Difference Logic}
(Still NP-Complete) subset of integer linear arithmetic problems where all constraints are of the form
\begin{equation*}
 x-y \le c
\end{equation*}
Questions in bounded model checking of timed automata along with questions of circuit timing analysis can be answered with this logic.\\
(Na\"ively solvable using an iterative Bellman-Ford method, but better negative-weight cycle detection algorithms are known.)
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\frame{
\frametitle{Bit-Vectors}
NP-Complete theory that allows fixed-width bit vectors to have the following operators executed on them:
\begin{itemize}
 \item Assignment $=$
 \item Named selection $[i:j]$
 \item Concatenation $::$
 \item Arithmetic $\{+,-,*,<\}$ where $*$ is multiplication by a scalar
 \item Bitwise operators $\{{\bf AND}, {\bf OR}, {\bf NOT}\}$
\end{itemize}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\frame{
\frametitle{Interpreted Sets and Bounded Quantification}
An expressive NP-Complete theory of heap-manipulating loop-free and procedure-free programs. Strategy for proving a program $T$ correct: compute $wp(T, true)$ and decide satisfiability of $\neg wp(T,true)$, giving $\neg wp(T,true)$ is unsatisfiable if and only if $T$ does not go wrong. \\
\begin{figure}
{\scriptsize
\begin{align*}
T \in Stmt ::= & Assert(\varphi) \,\mid\, Assume(\varphi) \,\mid\, \\
	       & x := new \,\mid\, free(x) \,\mid\, x := t \,\mid\, \\
               & f(x) := y \,\mid\, T_1;T_2 \,\mid\, T_1\square T_2
\end{align*}
\begin{align*}
\begin{array}{lll}
c &\in Integer \\
x &\in Variable \\
f &\in Function \\
\varphi &\in Formula &::= \alpha \,\mid\, \varphi_1 \wedge \varphi_2 \,\mid\, \varphi_1 \vee \varphi_2 \,\mid\, \neg \varphi \\
\alpha &\in \forall Formula &::= \gamma \,\mid\, \alpha_1 \wedge \alpha_2 \,\mid\, \alpha_1 \wedge \alpha_2 \,\mid\, \forall x \in S.\alpha \\
\gamma &\in GFormula &::= t_1 = t_2 \,\mid\, t_1 < t_2 \,\mid\, t_1\xrightarrow{f} t_2\xrightarrow{f} t_3 \,\mid\, \neg \gamma \\
t &\in Term &::= c \,\mid\, x \,\mid\, t_1 - t_2 \,\mid\, t_1 + t_2 \,\mid\, f(t) \,\mid\, ite(t = t', t_1,t_2) \\
S &\in Set &::= g^{-1}(t) \,\mid\, Btwn(f,t_1,t_2)
\end{array}
\end{align*}
}
\caption{Program statement syntax (top) and formula syntax (bottom)}
\label{fig:congruence_classes}
\end{figure}
}

\section{Combining Theories}
\frame{
\frametitle{Nelson-Oppen Method}
A solver for the union of theories $T_1$ and $T_2$ can be constructed using the Nelson-Oppen procedure if
\begin{itemize}
 \item The two have disjoint signatures ($\Sigma_1 \cap \Sigma_2 = \{=\}$)
 \item Both are \textit{stably infinite} (i.e., every satisfiable quantifier-free formula is satisfiable in an infinite model)
\end{itemize}
\pause
The procedure is based on the process of \textit{purification}. \\
\pause
For $\Gamma$ a set of literals of $\Sigma_1\cup \Sigma_2$,  the purified form of $\Gamma$ is $\Gamma_1\wedge\Gamma_2$ such that \\
\begin{equation*}
\Gamma_i \subseteq (\Sigma_i \cup \alpha) \text{ for } \alpha = \{{\cal V}(\Gamma_1)\cap{\cal V}(\Gamma_2)\}
\end{equation*}
${\cal V}(\Gamma_i)$ is the set of variables in $\Gamma_i$.
}

\frame{
\frametitle{Nelson-Oppen Algorithm}
Recall: $\alpha = \{{\cal V}(\Gamma_1)\cap{\cal V}(\Gamma_2)\}$ \\
A partition $\phi$ (conjuction of many equalities and disequalities) of $\alpha$ is guessed and the individual solvers return if $\Gamma_i\wedge\phi$ is satisfiable. \\
\pause
Theoretically-based optimization: \\
A theory is \emph{convex} iff for for all finite sets $\Gamma$ of literals and for all non-empty disjunctions $\bigvee_{i\in I}{u_i \simeq v_i}$ of variables,
\begin{equation*}
\Gamma \models_T \bigvee_{i\in I}{u_i\simeq v_i} \text{ iff } \Gamma \models_T u_i \simeq v_i \text{ for some } i\in I
\end{equation*} \\
If the two theories are convex, then this guessing can be changed into deducing the correct partition by propagating equalities.\\
Let $\Gamma_2$ know if $T_1\cup \Gamma_1 \models x \simeq y$ and vice versa.
}

\section{Reading}

\frame{
\frametitle{References}
\begin{itemize}
\item R. Nieuwenhuis and A. Oliveras: Solving SAT and SAT Modulo Theories: from an Abstract Davis-Putnam-Logemann-Loveland Procedure to DPLL(T)
\item R. Bruttomesso et al: A Lazy and Layered SMT(BV) Solver for Hard Industrial Verification Problems
\item S. Lahiri and S. Qadeer: Back to the Future: Revisiting Precise Program Verification using SMT Solvers
\item L. de Moura et al: A Tutorial on Satisfiability Modulo Theories
\end{itemize}
}

\end{document}


\documentclass[11pt]{article}
\usepackage{cite}
\usepackage{a4}

\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{amsmath,amssymb,amsthm}

\usepackage{url}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% layout commands
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newtheorem{thm}{Theorem}
\newcommand{\prog}[3]{\langle #1, #2, #3\rangle}
\newcommand{\slp}[1]{\mathbb{#1}}

\newcommand{\probleminput}[1]{\noindent \textsc{Input:} #1}
\newcommand{\problemoutput}[1]{\noindent \textsc{Output:} #1}
\newcommand{\problem}[3]{
            \noindent {\sc Problem:} \textbf{#1} \newline
            \noindent {\sc Input:} #2 \newline
            \noindent {\sc Output:} #3
}

\begin{document}
\title{Computing All Squares in Compressed Texts}
\author{Lesha Khvorost\thanks{The author acknowledges support from the Russian Foundation for Basic Research, grant
10-01-00793.}\\
Ural Federal University\\
jaamal@mail.ru}
\date{}
\maketitle

\begin{abstract}
We consider the problem of computing all squares in a string represented by a straight-line program (SLP). An
instance of the problem is an SLP $\slp{S}$ that derives some string $S$ and we seek a solution in the form of a table
that contains information about all squares in $S$ in a compressed form. We present an algorithm that solves the
problem in $O(|\slp{S}|^4\cdot \log^2{|S|})$ time and requires $O(|\slp{S}|\cdot \max\{ |\slp{S}|, \log{|S|}\})$ space,
where $|\slp{S}|$ (respectively $|S|$) is the size of the SLP $\slp{S}$ (respectively the length of the string $S$).
\end{abstract}

\section{Introduction}

Various compressed representations of strings are known: straight-line programs (SLPs), collage-systems, string
representations using antidictionaries, etc. Nowadays text compression based on context-free grammars such as
SLPs attracts much attention. The reason for this is not only that grammars provide well-structured compression but
also that the SLP-based compression is in a sense polynomially equivalent to the compression achieved by the Lempel-Ziv
algorithm that is widely used in practice. It means that, given a string $S$, there is a polynomial relation between
the size of an SLP that derives $S$ and the size of the dictionary stored by the Lempel-Ziv algorithm \cite{18}.

While compressed representations save storage space, there is a price to pay: some classical problems on strings
become computationally hard when one deals with compressed data and measures algorithms' speed in terms of the
size of compressed representations. As examples we mention here the problems \textbf{Hamming distance} \cite{15} and
\textbf{Literal shuffle} \cite{4}. On the other hand, there exist problems that admit algorithms working rather well on
compressed representations: \textbf{Pattern matching} \cite{15}, \textbf{Longest common substring} \cite{16},
\textbf{Computing all palindromes} \cite{16}. This dichotomy gives rise to the following research direction: to
classify important string problems by their behavior with respect to compressed data.

The \textbf{Computing All Squares} (\textbf{CAS}) problem is a well-known problem on strings. It is of importance, for
example, in molecular biology. Up to recently it is was not known whether or not \textbf{CAS} admits an algorithm
polynomial in the size of a compressed representation of a given string.\footnote{A polynomial algorithm that solves
\textbf{CAS} for strings represented by Lempel-Ziv encodings was announced in~\cite{8}. This representation is slightly
more general than that by SLPs. However no details of the algorithm have ever been published.} In general, a string can
have exponentially many squares with respect to the size of its compressed representation. For example, the string
$a^n$ has $\Theta(n^2)$ squares, while it is easy to build an SLP of size $O(\log n)$ that derives $a^n$. So we must
store information about squares in a compressed form. Also this implies that we cannot search for squares consecutively
by moving from one square to the ``next'' one. Squares should be somehow grouped in relatively large families that are
to be discovered at once.

We formulate the \textbf{CAS} problem in terms of SLPs as follows:

\problem{CAS}{an SLP $\slp{S}$ that derives some text $S$;}{a data structure (a S-table) that contains
information about all squares in $S$ in a compressed form.}

\begin{thm}
There is an algorithm that solves the \textbf{CAS} problem using $O(|\slp{S}|^4 \cdot \log^2{|S|})$ time and
$O(|\slp{S}| \cdot \max(|\slp{S}|, \log{|S|}))$ space.
\end{thm}

We would like to emphasize main features of the algorithm:

\begin{itemize}
\item This algorithm is divided into independent steps in contrast to classical algorithms in this area which
consecutively accumulate information about required objects. As a result it can be parallelized.

\item This algorithm presents a new technique for SLPs processing..

\item The algorithm is quite difficult for practical implementation. It is not excluded that constants hidden in the
"O" notation are actually very big.

\item The present upper bound for the time complexity is rather high and is not matched by any lower bound. The question
whether the upper bound can be lowered to say, cubic in $|\mathbb{S}|$ remains open.
\end{itemize}

{\small
	\bibliography{cas_problem}{}
	\bibliographystyle{plain}
}
\end{document}


\subsection{The Problem}

Consider passing a cell from its enclosing row, to its enclosing table, then down to its column. If there is only one cell sent along the path, we can statically allocate one attribute to hold the reference, e.g., \code{Table.cell}, or, under our encoding, set of attributes, e.g., \code{Table.cell_w}, \code{Table.cell_h}, etc. However, a column should receive a statically unbounded number of cells: we cannot statically name the necessary intermediate attributes for passing all of these through any intermediate node along the path.

The traditional solution is to support \emph{collections} that can contain multiple references; our challenge is in scheduling them. For simplicity, we restrict  collections to \emph{only} contain references.

\subsection{Collection primitives}

A collection should be creatable and queryable. Consider defining column $n$ of a table to have the maximal width of its corresponding cells. The column should contain a collection of (references to) its cells: from each row, it should extract cell $n$. We solve this in two steps: calculating the set of cells for each column and then query these sets for their maximum.

Our example introduces several special forms:

\begin{itemize}
\item \textbf{Type [ref $\tau$]}: a homogenous collection of non-local nodes
\item \textbf{reduce a in b where c : d}: is a nestable inorder reduction expresion over nodes in \code{b}, introducing identifier \code{a} to access an item. Note that the collection is either an input set of a local production such as \code{Row*} or a computed collection such as \code{cells}. Variable \code{c} of type \code{d} is local to the loop and may be accumulated over it. Finally, reductions are inorder \emph{locally}; i.e., during a top down traversal, the operation on each node may be to compute the entire local reduction and, after completion, return control to continue the top-down traversal.
\item \textbf{effects}: for reductions over a local production, attributes of child nodes may be (singly) assigned.
\item \textbf{ id' := init .. f(id) ..}: accumulation or temporary variables are defined in the form of initial value \code{init}, and, for the next element, as a function of the current one (or other current accumulator variables). Other expressions in the loop referencing \code{id} observe the intermediate value, not the final accumulation. This is true even when a loop is split over multiple traversals (e.g., a loop counter reused for calculating different attributes).
\item \textbf{ return' := init .. return .. }: reductions are expressions and thus have a value, denoted by special accumulator variable \code{return}.
\item \textbf{ [ a $|$ b ], [ a $@$ b ] }: an item may be inserted at the head of a collection or two collections appended. E.g., to place \code{a} at the end of collection \code{b}, use expression \code{[b @ [a]]}.
\end{itemize}

This example is useful in several ways. First, we show it is useful for a loop to introduce a new variable binding (though, we note, only one such binding needs to be active in a runtime at any point in time). Second, nesting loop iterations is also useful. Finally, we can restrict our attention to collections of homogenous nodes (our technique can likely be extended for collections of different types of nodes, but, for simplicity and lack of motivation, we do not).

\subsection{Transformation Overview}
Our transformation proceeds in several steps:
\begin{enumerate}
\item \textbf{Collection rewriting.} The input is a grammar with collection creation, reductions, and references. The output is a grammar without collections. Instead, it uses special loop functions that are opaque to the scheduler. We also generate an auxilarlly data structure to provide the code generator so that it can reconstitute a scheduled sequence of statements into the appropriate loop nests.

Our basic insight is that we can schedule homogenous collections as single nodes by hoisting the bodies of reductions to be top-level statements. Dependencies within an iteration are handled as usual, with the additional benefit of automatically \emph{fissioning} loop bodies across multiple tree traversals. Loop-carried dependencies are handled by using an inorder traversal over a collection. The only subtlety is in handling temporary intermediate values in cases of loop fission.
\item \textbf{Reference type and usage analysis}. Rewriting collections and loops reduces the output to a RAG, complete with nested references. As with our original first-class reference encoding, we perform a basic type analysis, from which, for every node type, we statically determine the extent of references. As before, we (temporarily) reject grammars with cycles in the static reference graph. %Later, after showing how to encode grammars over input graphs, we use a similar technique for lifting the cycle restriction here.
\item \textbf{Reference rewriting.} Using the type and reference analysis, we rewrite reference passing and dereferencing into attribute passing and accessing, again using phantom variables and new operators (masked as functions) to separatley guide downstream scheduling and code generation. The output is a traditional attribute grammar, albeit with phantom annotations and special loop and reference functions opaque to
\item \textbf{AG scheduling.} We reuse our basic AG scheduler.
\item \textbf{Code generation.} Our code generator examines the computed schedule and precomputed auxillary information to generate code. Special functions for looping and collections have custom handling rather than normal function dispatch.
\end{enumerate}

%%%%%%%%%%%%%%%%%%%%%%%%

\subsection{Collection Rewriting}

We must rewrite AG extensions for creating collections and operating over them. Our rewrite into traditional AGs is to reduce operations over collections into operations over a single node, which informs our various transformations. We consider each special form in turn:

\begin{itemize}
\item \textbf{Kleene star productions. } We erase Kleene star repetitions so productions are over single non-terminals:
$$Table \rightarrow Row^{*} ~ Column^{*} \Rightarrow Table \rightarrow Row~Column$$
\item \textbf{Kleene star references.} References over repeated reductions also lose their repetition. E.g.: $@Cell* \Rightarrow @Cell$
\item \textbf{Reduction expressions.} We recursively hoist reduction body statements to be top-level statements and introduce attributes to support the various loop variables. E.g.,

\begin{lstlisting}[mathescape]
reduce c in Column where i : int { $\ldots$ c.n := i $\ldots$ }
\end{lstlisting}

becomes

\begin{lstlisting}[mathescape]
c := loopVar(@Column);
$\ldots$ c.n := loopBody(c, $f_b$, i) $\ldots$
\end{lstlisting}

where $f_b$ is a pure function over $i$ lifted from the original expression. The transformation also adds fresh fields $c$ and $i$ to the enclosing node type.

We syntactically encode the nesting structure by associating each body statement ($loopBody$) with its enclosing loop ($loopVar$) through the first par. Code generation  reconstitutes loop nests using this encoding.
\item \textbf{Local vs. non-local reductions. } We distinguish between local vs. non-local reductions. For example, local collections support assignment to their attributes, as in the case of looping over a row to label each cell by increasing column number. Code generation for the loops is different, so we syntactically  propagate this distinction in our rewrite by including the reference creation in the loop variable creation. A full type analysis is unnecessary for this step as the mode is clear by examining the enclosing scope and node schema.
\end{itemize}

Eventual code generation must include loops, even if they are not visible to the scheduler, so we must somehow propagate the loop structure through the transformation. We already discussed reconstituting loop nests, so the only remaining issue is handling the erased Kleene stars.  For each node type $\tau$, we keep a set $\chi_{\tau}$ indicating which children are collections. E.g., $\chi_{\text{Table}} = \{Row, Column\}$.

\subsection{Reference type and usage analysis.}
Our support for references is the same as in the case of references without collections.
\subsection{Reference rewriting.}
Reference rewriting proceeds the same as in the case of references without collections.
\subsection{Scheduling.}
Scheduling proceeds with an attribute grammar schedule with no knowledge of our extensions.

\subsection{Code generation.}
Code generation occurs much in the same way as with just the reference extension, but we use custom handling for \code{lhs := loopVar(...)} and \code{loopBody(...)} statements. The former, \code{loopVar}, is unnecessary, so we elide it. Perhaps not immediately intuitive, the generated loop nest structure is not the same as that of the input due to the declarative nature of the input. What was 1 loop in the input may be multiple under the new schedule.

Consider the following declarative loop to sum children and broadcast the sum to them:

\begin{lstlisting}[mathescape]
maxWidth :=
  reduce cell in Column*
    return$`$ := 0 .. max(return, cell.minimumWidth)
    cell.width := maxWidth
\end{lstlisting}

Imperative code might be scheduled to do this in two loops, perhaps in even two different traversals of the tree:

\begin{lstlisting}[mathescape]
maxWidth := 0;
for (cell in Column*)
  maxWidth := max(maxWidth, cell.minimumWidth)
$\ldots$
for (cell in Column*)
    cell.width := maxWidth
\end{lstlisting}

Reconstituting loop nests is a parsing problem: we must place opening and closing braces, where an opening brace represents starting a loop. Parsing can be done in one linear left-to-right pass using a stack. Given the original loop nesting, there is some partial order $<$ where loop variable $a < b$ if a loop with index variable $a$ is enclosed by a loop with index variable $b$. Note that we do not need to create this operator during initial rewriting; it can be extracted from the loop encoding. We denote string $a_0 \ldots a_j$ maximal if, for any $0 \leq i < j$, $a_i < a_{i+1}$ and $\neg \exists b . a_i < b ~\wedge~ b < a_{i+1}$. For every nesting of loops, let $path_{a}^{b}$ denote the maximal string between loops $a$ and $b$ (i.e., the nesting sequence of loops).

For each statement $lhs := loopBody(a, ...)$:

\begin{itemize}
\item $a = peak(stack)$: proceed.
\item $a < peak(stack)$: push string $path_{peak(stack)}^{a}$, emit the corresponding loop nestings ($|path_{peak(stack)}^{a}|$ of them), and then proceed.
\item $a \not< peak(stack)$: close 1 level of nesting and pop the stack. Repeat entire process (do not proceed).
\end{itemize}

Statements not involving loops (neither \code{loopBody} nor \code{loopVar}) are top-level statements; the stack is emptied and all loops are enclosed.

A final subtlety is the use of intermediate variables in the case of loop fission. For example, we repeatedly count the step of an iteration using variable $i$: it might be accessed in statements in either split loop body, so must be available to both. We can either store these computations, or simpler for our implementation, recompute them. Our code generator thus checks the accessed set of local fields within each loop body and includes the computation of any used loop variables (this can be viewed as a time vs. memory exchange).

%First, we eliminate collective operations. Consider collections over items such as \code{Column*}. One option is rewriting the grammar to use \emph{cons} lists, e.g., $A\rightarrow B^{*}$ becomes $A\rightarrow B_{List}$ and $B_{List} \rightarrow B ~B{List}~ |~ B_{\epsilon}$, where the initial, iteration, and termination code of a loop is split between $A$, $B_{List}$, and $B_{\epsilon}$, respectively. This encoding does not support first-class collections and artificially restricts our ability to generate parallel schedules.

%We uniformly rewrite reductions over first-class and AST collections. AST collections, e.g., \code{Column*}, are eliminated by rewriting to singletons, e.g., \code{Column\_Star}, and moving loop operations to these intermediate nodes. Class fields are duplicated. For purely scheduling, we might just rewrite \code{Column*} to \code{Column}, but propagating the use of a collection simplifies code generation.

%All reductions are eliminated: reduction variables are hoisted to class variables and statements within a reduction are rewritten to use a \code{loop} function. E.g., \code{i` := 0 .. i .. } is hoisted out of the enclosing reduction and scheduled as \code{i := loop(0)}, where the arguments to loop propagate any dependencies. For code generation, we must also propagate code for the iteration step.

\begin{figure}
\begin{lstlisting}[mathescape]
//idealized declarative input:
//  AG with reference passing, collections
@Start Table $\rightarrow$ Row* Column* { }
Cell $\rightarrow$ { input width : int }
Row $\rightarrow$ Cell* { var cells : [ref Cell] }
Column $\rightarrow$ {
  var rows : [ref Row];
  var n, width : int;
  var cells : [ref Cell];
}
Table {
  reduce c in Column* where i : int {
    i$'$ := 0 .. i + 1 ..;
    c.n := i;
    c.rows := @Row*;
  }
}
Row { cells := @Cell*}
Column {
  cells := reduce r in rows {
    nCells$'$ := reduce c in r$\rightarrow$cells where i : int {
      i$'$ := 0 .. i + 1 ..;
      return$'$ :=
        [] .. i = n ? [c | return] : return ..;
    }
    return$'$ := [] ..  [nCells @ return] ..;
  }
  width := reduce child in cells
    return$'$ := 0 .. max(return, child$\rightarrow$width) ..;
}
\end{lstlisting}

\begin{lstlisting}[mathescape]
//reduce collections and lift loops
@Start Table $\rightarrow$ Row Column {
  var i : int
  phantom var c : ref Column;
}
Cell $\rightarrow$ { input width : int }
Row $\rightarrow$ Cell { var cells : ref Cell }
Column $\rightarrow$ {
  var rows : ref Row;
  var n : int;
  var cells : ref Cell;
  var nCells : ref Cell;
  var width : int;
  var i : int;
  phantom var r : ref Row;
  phantom var c : ref Cell;
  phantom var child : ref Cell;
}
Table {
  c := loopVar(@Column);
  i := loopBody(c, $f_{a}$, 0);
  Column.n := loopBody(c, $f_b$, i);
  Column.rows := loopBody(c, $f_c$, @Row);
}
Row { cells := @Cell }
Column {
  r := loopVar(rows);
  c := loopVar(r$\rightarrow$cells);
  cells := loopBody(r, $f_d$, nCells);
  nCells := loopBody(c, $f_e$, i, n, c);
  i := loopBody(c, $f_f$, 0);
  child := loopVar(cells);
  width := loopBody(child, $f_g$, 0, child$\rightarrow$width);
}
$f_{a}$ = $\lambda$i.i + 1
$f_{b}$ = $\lambda$i.i
$f_{c}$ = $\lambda$Row.Row
$f_{d}$ = $\lambda$cells,nCells.append(nCells, cells)
$f_{e}$ = $\lambda$i,n,c,nCells.i = n ? concat(c,nCells) : nCells
$f_{f}$ = $\lambda$i.i + 1
$f_{g}$ = $\lambda$c_width,width.max(width,c_width)
\end{lstlisting}
\end{figure}

\begin{figure}
\begin{lstlisting}[mathescape]
//expand references
@Start Table $\rightarrow$ Row Column {
  var i : int
  phantom var c : ref Column;
}
Cell $\rightarrow$ { input width : int }
Row $\rightarrow$ Cell {
  var cells : ref Cell;
  phantom var cells$_{width}$ : int;
}
Column $\rightarrow$ {
  var rows : ref Row;
  phantom var rows$_{cells}$, cells, nCells, r$_{cells}$, c,
              child : ref Cell;
  phantom var rows$_{cells{_{width}}}$, cells$_{width}$, nCells$_{width}$,
              r$_{cells_{width}}$, c$_{width}$, child$_{width}$ : int;
  var n, width, i : int;
  phantom var r : ref Row;
}
Table {
  c := loopVar(@($``\&Column"$));
  i := loopBody(c, $f_a$, 0);
  Column.n := loopBody(c, $f_b$, i);
  Column.rows := loopBody(c, $f_c$, @($``\&Row"$));
  Column.rows_cells := $\O$(Row.cells);
  Column.rows_cells_width := $\O$(Row.cells_width);
}
Row {
  cells := @($``\&Cell"$)
  cells_width := $\O$(Cell.width);
}
Column {
  r := loopVar(rows);
  r_cells := $\O$(rows_cells);
  r_cells_width := $\O$(rows_cells_width);
  c := loopVar($\rightarrow$(r_cells));
  c_width := $\O$(r_cells_width);
  cells := loopBody(r, $f_d$, nCells);
  cells_width := $\O$(nCells_width);
  nCells := loopBody(c, $f_e$, i, n, c);
  nCells_width := $\O$(c_width);
  i := loopBody(c, $f_f$, 0);
  child := loopVar(cells);
  child_width := $\O$(cells_width);
  width := loopBody(child, $f_g$, 0, $\rightarrow$(child_width));
}
\end{lstlisting}

\begin{lstlisting}[mathescape]
//generated visits
class Pass0 : BottomUp {
   void visit (Table n) {
    i = 0;
    for (Column column : n.ColumnStar) {
      i = i + 1;
      column.n = i;
      column.rows = n.RowStar;
    } } }
class Pass1 : TopDown {
   void visit (Row n) { cells = n.CellStar; }
   void visit (Column n) {
    cells = new ArrayList<Cell>();
    for (Row r : n.rows) {
      nCells = new ArrayList<Cell>();
      int i = 0;
      for (Column c : r.cells) {
        i = i + 1;
        nCells = n.n == i++ ? [c | nCells] : nCells;
      }
      cells = [nCells @ cells];
    }
    n.width = 0;
    for (Cell child : n.cells)
      n.width = Math.max(n.width, child.width); } }
\end{lstlisting}
%$\ldots$
%(new Pass0()).visit(tree);
%(new Pass1()).visit(tree);
\end{figure}

Note that syntax nodes are not directly in dependencies. E.g., we do not use \code{Column} as a parameter. Reductions over first-class collections must include the collection as a dependency. We distinguish these two forms as \code{loopBody} and \code{loopBody}, respectively.

\subsection{Eliminating References}
We repeat our transformation from the previous section to eliminating our use of references here.

\subsection{Primitives vs. Foreign Functions}

As a grammar is likely embedded in some generic \emph{host} language, we might pass references through foreign functions. However, we can no longer schedule their use, and the host likewise would not know when reachable attributes are available. [[iffy..]]

\subsection{Rewrite rules}

%\input{rewritescollections}
\begin{figure*}
\caption{$GAG_{dyn~collect}$: Translation of an AG with first-class references and collections to $GAG_{dynamic}$ and extended code generation rules. \textbf{[[TODO; previous version combined the two rewrites in one place, which was a mess]]} }
\label{fig:statrewrites}
\end{figure*}