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

\section{Non-malleability of $\sonicprotfs$}
\label{sec:sonic}
\subsection{\sonic{} protocol rolled out}
In this section we present $\sonic$'s constraint system and algorithms. Reader
familiar with them may jump directly to the next section.

\hamid{we should put the following figure \ref{fig:pcoms} somewhere in this section.}
 \begin{figure}[h!]
 \centering
 	\begin{pcvstack}[center,boxed]
 		\begin{pchstack}
 			\procedure{$\kgen(\secparam, \maxdeg)$} {
 				\alpha, \chi \sample \FF^2_p \\ [\myskip]
 				\pcreturn \gone{\smallset{\chi^i}_{i = -\multconstr}^{\multconstr},
           \smallset{\alpha \chi^i}_{i = -\multconstr, i \neq
             0}^{\multconstr}},\\
         \pcind \gtwo{\smallset{\chi^i, \alpha \chi^i}_{i =
             -\multconstr}^{\multconstr}}, \gtar{\alpha}\\
 				%\markulf{03.11.2020}{} \\
 			%	\hphantom{\pcind \p{o}_i(X) \gets \sum_{j = 1}^{t_i} \gamma_i^{j - 1} \frac{\p{f}_{i,j}(X) - \p{f}_{i, j}(z_i)}{X - z_i}}
 				\hphantom{\hspace*{5.5cm}}
 		}

 			\pchspace

 			\procedure{$\com(\srs, \maxconst, \p{f}(X))$} {
 				\p{c}(X) \gets \alpha \cdot X^{\dconst - \maxconst} \p{f}(X) \\ [\myskip]
 				\pcreturn \gone{c} = \gone{\p{c}(\chi)}\\ [\myskip]
 				\hphantom{\pcind \pcif \sum_{i = 1}^{\abs{\vec{z}}} r_i \cdot
           \gone{\sum_{j = 1}^{t_j} \gamma_i^{j - 1} c_{i, j} - \sum_{j = 1}^{t_j}
             s_{i, j}} \bullet \gtwo{1} + } }
 		\end{pchstack}
 		% \pcvspace

 		\begin{pchstack}
 			\procedure{$\open(\srs, z, s, f(X))$}
 			{
 				\p{o}(X) \gets \frac{\p{f}(X) - \p{f}(z)}{X - z}\\ [\myskip]
 				\pcreturn \gone{\p{o}(\chi)}\\ [\myskip]
 				\hphantom{\hspace*{5.5cm}}
 			}

 			\pchspace

 			\procedure{$\verify(\srs, \maxconst, \gone{c}, z, s, \gone{\p{o}(\chi)})$}
       {
         \pcif \gone{\p{o}(\chi)} \bullet \gtwo{\alpha \chi} + \gone{s - z
         \p{o}(\chi)} \bullet \gtwo{\alpha} = \\ [\myskip] \pcind \gone{c}
         \bullet \gtwo{\chi^{- \dconst + \maxconst}} \pcthen  \pcreturn 1\\
         [\myskip]
         \rlap{\pcelse \pcreturn 0.} \hphantom{\pcind \pcif \sum_{i =
             1}^{\abs{\vec{z}}} r_i \cdot \gone{\sum_{j = 1}^{t_j} \gamma_i^{j -
               1} c_{i, j} - \sum{j = 1}^{t_j} s_{i, j}} \bullet \gtwo{1} + } }
 		\end{pchstack}
 	\end{pcvstack}

 	\caption{$\PCOMs$ polynomial commitment scheme.}
 	\label{fig:pcoms}
 \end{figure}

\oursubsub{The constraint system}
\label{sec:sonic_constraint_system}
\sonic's system of constraints composes of three $\multconstr$-long vectors
$\va, \vb, \vc$ which corresponds to left and right inputs to multiplication
gates and their outputs. It hence holds $\va \cdot \vb = \vc$.

There is also $\linconstr$ linear constrains of the form
\[
  \va \vec{u_q} + \vb \vec{v_q} + \vc \vec{w_q} = k_q,
\]
where $\vec{u_q}, \vec{v_q}, \vec{w_q}$ are vectors for the $q$-th linear
constraint with instance value $k_q \in \FF_p$. Furthermore define polynomials
\begin{equation}
  \begin{split}
    \p{u_i}(Y) & = \sum_{q = 1}^\linconstr Y^{q + \multconstr} u_{q, i}\,,\\
    \p{v_i}(Y) & = \sum_{q = 1}^\linconstr Y^{q + \multconstr} v_{q, i}\,,\\
  \end{split}
  \qquad
  \begin{split}
    \p{w_i}(Y) & = -Y^i - Y^{-i} + \sum_{q = 1}^\linconstr Y^{q +
      \multconstr} w_{q, i}\,,\\
    \p{k}(Y) & = \sum_{q = 1}^\linconstr Y^{q + \multconstr} k_{q}.
  \end{split}
\end{equation}

$\sonic$ constraint system requires that
\begin{align}
  \label{eq:sonic_constraint}
  \vec{a}^\top \cdot \vec{\p{u}} (Y) + \vec{b}^\top \cdot \vec{\p{v}} (Y) +
  \vec{c}^\top \cdot \vec{\p{w}} (Y) + \sum_{i = 1}^{\multconstr} a_i b_i (Y^i +
  Y^{-i}) - \p{k} (Y) = 0.
\end{align}

In \sonic{} we will use commitments to the following polynomials.
\begin{align*}
  \pr(X, Y) & = \sum_{i = 1}^{\multconstr} \left(a_i X^i Y^i + b_i X^{-i} Y^{-i}
              + c_i X^{-i - \multconstr} Y^{-i - \multconstr}\right) \\
  \p{s}(X, Y) & = \sum_{i = 1}^{\multconstr} \left( u_i (Y) X^{-i} +
                v_i(Y) X^i + w_i(Y) X^{i + \multconstr}\right)\\
  \pt(X, Y) & = \pr(X, 1) (\pr(X, Y) + \p{s}(X, Y)) - \p{k}(Y)\,.
\end{align*}

Polynomials $\p{r} (X, Y), \p{s} (X, Y), \p{t} (X, Y)$ are designed such that
$\p{t} (0, Y) = \vec{a}^\top \cdot \vec{\p{u}} (Y) + \vec{b}^\top \cdot
\vec{\p{v}} (Y) + \vec{c}^\top \cdot \vec{\p{w}} (Y) + \sum_{i =
  1}^{\multconstr} a_i b_i (Y^i + Y^{-i}) - \p{k} (Y) $. That is, the prover is
asked to show that $\p{t} (0, Y) = 0$, cf.~\cref{eq:sonic_constraint}.

Furthermore, the commitment system in $\sonic$ is designed such that it is
infeasible for a $\ppt$ algorithm to commit to a polynomial with non-zero
constant term.

\oursubsub{Algorithms rolled out}
\ourpar{$\sonic$ SRS generation $\kgen(\REL)$.} The SRS generating algorithm picks
randomly $\alpha, \chi \sample \FF_p$ and outputs
	\[
      \srs = \left( \gone{\smallset{\chi^i}_{i = -\dconst}^{\dconst},
          \smallset{\alpha \chi^i}_{i = -\dconst, i \neq 0}^{\dconst}},
        \gtwo{\smallset{\chi^i, \alpha \chi^i}_{i = - \dconst}^{\dconst}},
        \gtar{\alpha} \right)
	\]
\ourpar{$\sonic$ prover $\prover(\srs, \inp, \wit=\va, \vb, \vc)$.}
\begin{description}
\item[Round 1] The prover picks randomly randomisers
  $c_{\multconstr + 1}, c_{\multconstr + 2}, c_{\multconstr + 3}, c_{\multconstr
    + 4} \sample \FF_p$. Sets
  $\pr(X, Y) \gets \pr(X, Y) + \sum_{i = 1}^4 c_{\multconstr + i} X^{- 2
    \multconstr - i}$. Commits to $\pr(X, 1)$ and outputs
  $\gone{r} \gets \com(\srs, \multconstr, \pr(X, 1))$.  Then it gets challenge $y$ from
  the verifier.
\item[Round 2] $\prover$ commits to $\pt(X, y)$ and outputs
  $\gone{t} \gets \com(\srs, \dconst, \pt(X, y))$. Then it gets a challenge $z$ from
  the verifier.
\item[Round 3] The prover computes commitment openings. That is, it outputs
  \begin{align*}
    \gone{o_a} & = \open(\srs, z, \pr(z, 1), \pr(X, 1)) \\
    \gone{o_b} & = \open(\srs, yz, \pr(yz, 1), \pr(X, 1)) \\
    \gone{o_t} & = \open(\srs, z, \pt(z, y), \pt(X, y))
  \end{align*}
  along with evaluations $a' = \pr(z, 1), b' = \pr(y, z), t' = \pt(z, y)$.  Then it
  engages in the signature of correct computation playing the role of the
  helper, i.e.~it commits to $\p{s}(X, y)$ and sends the commitment $\gone{s}$, commitment opening
  \begin{align*}
    \gone{o_s} & = \open(\srs, z, \p{s}(z, y), \p{s}(X, y)), \\
  \end{align*} and $s'=\p{s}(z, y)$.
%
  Then
  it obtains a challenge $u$ from the verifier.
\item[Round 4] In the next round the prover computes
  $\gone{c} \gets \com(\srs, \dconst, \p{s}(u, Y))$ and
  computes commitments' openings
  \begin{align*}
    \gone{w} & = \open(\srs, u, \p{s}(u, y), \p{s}(X, y)), \\
    \gone{q_y} & = \open(\srs, y,\p{s}(u, y), \p{s}(u, Y)),
  \end{align*}
  and returns $\gone{w}, \gone{q_y}, s = \p{s}(u, y)$. Eventually the prover gets the last challenge
  from the verifier---$z'$.
\item[Round 5] In the final round, $\prover$ computes opening
  $\gone{q_{z'}} = \open(\srs, z', \p{s}(u, z'), \p{s}(u, X))$ and outputs $\gone{q_{z'}}$.
\end{description}

\ourpar{$\sonic$ verifier $\verifier(\srs, \inp, \zkproof)$.} The verifier
in \sonic{} runs as subroutines the verifier for the polynomial commitment. That
is it sets $t' = a'(b' + s') - \p{k}(y)$ and checks the following:
\begin{equation*}
  \begin{split}
    &\PCOMs.\verifier(\srs, \multconstr, \gone{r}, z, a', \gone{o_a}), \\
    &\PCOMs.\verifier(\srs, \multconstr, \gone{r}, yz, b', \gone{o_b}),\\
    &\PCOMs.\verifier(\srs, \dconst, \gone{t}, z, t', \gone{o_t}),\\
    &\PCOMs.\verifier(\srs, \dconst, \gone{s}, z, s', \gone{o_s}),\\
  \end{split}
  \qquad
  \begin{split}
    &\PCOMs.\verifier(\srs, \dconst, \gone{s}, u, s, \gone{w}),\\
    &\PCOMs.\verifier(\srs, \dconst, \gone{c}, y, s, \gone{q_y}),\\
    &\PCOMs.\verifier(\srs, \dconst, \gone{c}, z', \p{s}(u, z'), \gone{q_{z'}}),
  \end{split}
\end{equation*}
and accepts the proof iff all the checks holds. Note that the value
$\p{s}(u, z')$ that is recomputed by the verifier uses separate challenges $u$
and $z'$. This enables the batching of many proof and outsourcing of this
part of the proof to an untrusted helper.

\subsection{Unique opening property of $\PCOMs$}
\begin{lemma}
\label{lem:pcoms_unique_op}
$\PCOMs$ has the unique opening property in the AGM.
\end{lemma}
\begin{proof}
Let
$z \in \FF_p$ be the attribute the polynomial is evaluated at,
$\gone{c} \in \GRP$ be the commitment,
$s \in \FF_p$ the evaluation value, and
$o \in \GRP$ be the commitment opening.
We need to show that for every $\ppt$ adversary $\adv$ probability
\[
  \Pr \left[
    \begin{aligned}
      & \verify(\srs, \gone{c}, z, s, \gone{o}) = 1, \\
      & \verify(\srs, \gone{c}, z, \tilde{s}, \gone{\tilde{o}}) = 1
    \end{aligned}
    \,\left|\, \vphantom{\begin{aligned}
          & \verify(\srs, \gone{c}, z, s, \gone{o}),\\
          & \verify(\srs, \gone{c}, z, s, \gone{\tilde{o}}) \\
          &o \neq \tilde{o})
		\end{aligned}}
      \begin{aligned}
        %& \srs \gets \kgen(\secparam, \maxdeg), \\
        & (\gone{c}, z, s, \gone{o}, \gone{\tilde{o}}) \gets \adv^{\initU}(1^\secpar, \maxdeg)
      \end{aligned}
    \right.\right]
  % \leq \negl.
\]
is at most negligible.

As noted in \cite[Lemma 2.2]{EPRINT:GabWilCio19} it is enough to upper bound the
probability of the adversary succeeding using the idealised verification
equation---which considers equality between polynomials---instead of the real
verification equation---which considers equality of the polynomials' evaluations.

For a polynomial $f$, its degree upper bound $\maxconst$, evaluation point $z$,
evaluation result $s$, and opening $\gone{o(X)}$ the idealised check verifies that
\begin{equation}
  \alpha (X^{\dconst - \maxconst}f(X) \cdot X^{-\dconst + \maxconst} -  s) \equiv \alpha \cdot o(X) (X - z)\,,
\end{equation}
what is equivalent to
\begin{equation}
	f(X) -  s \equiv o(X) (X - z)\,.
	\label{eq:pcoms_idealised_check}
\end{equation}
Since $o(X)(X - z) \in \FF_p[X]$ then from the uniqueness of polynomial
composition, there is only one $o(X)$ that fulfils the equation above.
\qed
\end{proof}

\subsection{Unique response property}
The unique response property of $\sonicprot$ follows from the unique opening
property of the used polynomial commitment scheme $\PCOMs$.
\begin{lemma}
\label{lem:sonicprot_ur}
If a polynomial commitment scheme $\PCOMs$ is evaluation binding with
parameter $\epsbind(\secpar)$ and has unique openings property with parameter
$\epsop(\secpar)$, then $\sonicprot$ is $\ur{1}$ with parameter $\epsur(\secpar) \leq
\epsbind(\secpar) + \epsop(\secpar)$.
\end{lemma}
\begin{proof}
  Let $\adv$ be an adversary that breaks $\ur{1}$-ness of $\sonicprot$.  We
  consider two cases, depending on which round $\adv$ is able to provide at
  least two different outputs such that the resulting transcripts are
  acceptable.  For the first case we show that $\adv$ can be used to break the
  evaluation binding property of $\PCOMs$, while for the second case we show
  that it can be used to break the unique opening property of $\PCOMs$.

  The proof goes similarly to the proof of \cref{lem:plonkprot_ur} thus we
  provide only draft of it here.  In each Round $i$, for $i > 1$, the prover
  either commits to some well-defined polynomials (deterministically), evaluates
  these on randomly picked points, or shows that the evaluations were performed
  correctly.  Obviously, for a committed polynomial $\p{p}$ evaluated at point
  $x$ only one value $y = \p{p}(x)$ is correct. If the adversary was able to
  provide two different values $y$ and $\tilde{y}$ that would be accepted as an
  evaluation of $\p{p}$ at $x$ then the $\PCOMs$'s evaluation binding would be
  broken.  Alternatively, if $\adv$ was able to provide two openings $\p{W}$ and
  $\p{\tilde{W}}$ for $y = \p{p}(x)$ then the unique opening property would be
  broken.
%
Hence the probability that $\adv$ breaks $\ur{1}$-property of $\PCOMs$ is
upper-bounded by $\epsbind(\secpar) + \epsop(\secpar)$.
\qed

\end{proof}

\subsection{Forking special soundness}
\begin{lemma}
	\label{lem:sonicprot_ss}
	$\sonicprot$ is $(\epsss(\secpar), 2, \multconstr + \linconstr + 1)$-forking special sound against
	algebraic adversaries with
	\[
	\epss(\secpar) \leq \epsid(\secpar) + \epsldlog(\secpar) \,,
	\]
	where $\epsid(\secpar)$ is a soundness error of the idealized verifier, and
	$\epsldlog(\secpar)$ is security of $(\dconst, \dconst)$-$\ldlog$ assumption.
\end{lemma}
\begin{proof}
	Similarly as in the case of $\plonk$, the main idea of the proof is to show
	that an adversary who breaks forking special soundness can be used to break a $\dlog$ problem
	instance. The proof goes by game hops. Let $\tree$ be the tree produced by
	$\tdv$ by rewinding $\adv$. Note that since the tree branches after Round 2,
	the instance $\inp$, commitments
	$\gone{\p{r} (\chi, 1), \p{r} (\chi, y), \p{s} (\chi, y), \p{t} (\chi, y)}$, and challenge
	$y$ are the same. The tree branches after the second round
	of the protocol where the challenge $z$ is presented, thus tree $\tree$ is
	build using different values of $z$.
	%
	We consider the following games.

	\ncase{Game 0} In this game the adversary wins if all the transcripts it
	produced are acceptable by the ideal verifier,
	i.e.~$\vereq_{\inp, \zkproof}(X) = 0$, cf.~\cref{eq:ver_eq}, and none of
	commitments
	$\gone{\p{r} (\chi, 1), \p{r} (\chi, y), \p{s} (\chi, y), \p{t} (\chi, y)}$ use
	elements from a simulated proof, and the extractor fails to extract a valid
	witness out of the proof.

	\ncase{Probability that $\adv$ wins Game 0 is negligible} Probability of
	$\adv$ winning this game is $\epsid(\secpar)$ as the protocol $\sonicprot$,
	instantiated with the idealised verification equation, is perfectly
	knowledge sound except with negligible probability of the idealised verifier
	failure $\epsid(\secpar)$. Hence for a valid proof $\zkproof$ for a
	statement $\inp$ there exists a witness $\wit$, such that $\REL(\inp, \wit)$
	holds. Note that since the $\tdv$ produces $(\multconstr + \linconstr + 1)$
	acceptable transcripts for different challenges $z$. As noted in
	\cite{CCS:MBKM19} this assures that the correct witness is encoded in
	$\p{r} (X, Y)$. Hence $\extt$ can recreate polynomials' coefficients by
	interpolation and reveal the witness with probability $1$. Moreover, the
	probability that extraction fails in that case is upper-bounded by
	probability of an idealised verifier failing $\epsid(\secpar)$, which is
	negligible.

	\ncase{Game 1} In this game the adversary additionally wins if it produces a
	transcript in $\tree$ such that $\vereq_{\inp, \zkproof}(\chi) = 0$, but
	$\vereq_{\inp, \zkproof}(X) \neq 0$, and none of commitments
	$\gone{\p{r} (\chi, 1), \p{r} (\chi, y), \p{s} (\chi, y), \p{t} (\chi, y)}$
	use elements from a simulated proof.  The first condition means that the
	ideal verifier does not accept the proof, but the real verifier does.

	\ncase{Game 0 to Game 1} Assume the adversary wins in Game 1, but does not
	win in Game 0. We show that such adversary may be used to break an
	instance of a $\ldlog$ assumption. More precisely, let $\tdv$ be an
	algorithm that for relation $\REL$ and randomly picked
	$\srs \sample \kgen(\REL)$ produces a tree of acceptable transcripts such
	that the winning condition of the game holds. Let $\rdvdlog$ be a
	reduction that gets as input an
	$(\dconst, \dconst)$-ldlog instance
	$\gone{\chi^{-\dconst}, \ldots, \chi^{\dconst}}, \gtwo{\chi^{-\dconst},
		\ldots, \chi^{\dconst}}$ and is tasked to output $\chi$.

	The reduction $\rdvdlog$ proceeds as follows.
	\begin{enumerate}
  \item Pick a random $\alpha$ and compute
    $\gone{\alpha \chi^{- \dconst}, \ldots, \alpha \chi^{-1}, \alpha \chi,
      \ldots, \alpha \chi^{\dconst}}$,
    $\gtwo{\alpha \chi^{- \dconst}, \ldots, \alpha \chi^{-1}, \alpha \chi,
      \ldots, \alpha \chi^{\dconst}}$. Set a SRS $\srs$ to be the
    $(\dconst, \dconst)$-ldlog instance and its multiplication with $\alpha$ as
    computed above.
    % \hamid{Is this clear?}
  \item Build $\sonicprot$'s SRS in the updatable setting by answering $\adv$'s
    queries for SRS updates and setting the honest update of the SRS to be
    $\srs$. Let $\srs'$ be the finalised SRS.
		\item Let $(1, \tree)$ be the output returned by $\tdv$. Let $\inp$ be a
		relation proven in $\tree$.  Consider a transcript $\zkproof \in \tree$ such
		that $\vereq_{\inp, \zkproof}(X) \neq 0$, but
		$\vereq_{\inp, \zkproof}(\chi') = 0$. Since $\adv$ is algebraic, all group
		elements included in $\tree$ are extended by their representation as a
		combination of the input $\GRP_1$-elements. Hence, all coefficients of the
		verification equation polynomial $\vereq_{\inp, \zkproof}(X)$ are known.
		\item Find $\vereq_{\inp, \zkproof}(X)$ zero points and find $\chi'$ among
		them.
  \item Let $\chi_1, \ldots, \chi_\ell$ be the partial trapdoors of $\adv$'s SRS
    updates, extracted by the reduction from the update proofs given by $\adv$.
		\item Return  $\chi = \chi' (\chi_1 \chi_2 \ldots \chi_\ell)^{-1}$.
	\end{enumerate}
	Hence, the probability that the adversary wins Game 1 is upper-bounded by
	$\epsldlog(\secpar)$.
\end{proof}

\subsection{Trapdoor-less simulatability of Sonic}
\begin{lemma}
\label{lem:sonic_hvzk}
$\sonic$ is 2-progrmmable trapdoor-less simulatable.
\end{lemma}
\begin{proof}
  The simulator proceeds as follows.
  \begin{enumerate}
  \item Pick randomly vectors $\vec{a}$, $\vec{b}$ and set
    \begin{equation}
      \label{eq:ab_eq_c}
      \vec{c} = \vec{a} \cdot \vec{b}.
    \end{equation}
  \item Pick randomisers $c_{\multconstr + 1}, \ldots, c_{\multconstr + 4}$,
    honestly compute polynomials $\p{r}(X, Y), \p{r'}(X, Y), \p{s}(X, Y)$ and
    pick randomly challenges $y$, $z$.
  \item Output commitment $\gone{r} \gets \com(\srs, \multconstr, \p{r} (X,
    1))$ and challenge $y$.
  \item Compute
    \begin{align*}
      & a' = \p{r}(z, 1),\\
      & b' = \p{r}(z, y),\\
      & s' = \p{s}(z, y).
    \end{align*}
  \item Pick polynomial $\p{t}(X, Y)$ such that
    \begin{align*}
      & \p{t} (X, y) = \p{r} (X, 1) (\p{r}(X, y) + \p{s} (X, y)) - \p{k} (Y)\\
      & \p{t} (0, y) = 0
    \end{align*}
  \item Output commitment $\gone{t} = \com (\srs, \dconst, \p{t} (X, y))$ and
    challenge $z$.
  \item Continue following the protocol.
  \end{enumerate}

  We note that the simulation is perfect. This comes since, except polynomial
  $\p{t} (X, Y)$ all polynomials are computed following the protocol. For
  polynomial $\p{t} (X, Y)$ we observe that in a case of both real and simulated
  proof the verifier only learns commitment $\gone{t} = \p{t} (\chi, y)$ and
  evaluation $t' = \p{t} (z, y)$. Since the simulator picks $\p{t} (X, Y)$ such
  that
  \begin{align*}
      \p{t} (X, y) = \p{r} (X, 1) (\p{r}(X, y) + \p{s} (X, y)) - \p{k} (Y)
  \end{align*}
  Values of $\gone{t}$ are equal in both proofs.
  Furthermore, the simulator picks its polynomial such that $\p{t}(0, y) = 0$,
  hence it does not need the trapdoor to commit to it. (Note that the proof
  system's SRS does not allow to commit to polynomials which have non-zero
  constant term). \qed
\end{proof}
\begin{remark}
  As noted in \cite{CCS:MBKM19}, $\sonic$ is statistically subversion-zero
  knowledge (Sub-ZK). As noted in \cite{AC:ABLZ17}, one way to achieve
  subversion zero knowledge is to utilise an extractor that extracts a SRS
  trapdoor from a SRS-generator. Unfortunately, a NIZK made subversion
  zero-knowledge by this approach cannot achieve perfect Sub-ZK as one has to
  count in the probability of extraction failure. However, with the simulation
  presented in \cref{lem:sonic_hvzk}, the trapdoor is not required for the
  simulator as it is able to simulate the execution of the protocol just by
  picking appropriate (honest) verifier's challenges. This result transfers to
  $\sonicprotfs$, where the simulator can program the random oracle to provide
  challenges that fits it.
\end{remark}

\subsection{From forking special soundness and unique response property to \COMMENT{forking
  }simulation extractability of $\sonicprotfs$}
Since \cref{lem:sonicprot_ur,lem:sonicprot_ss} hold, $\sonicprot$ is $\ur{1}$
and forking special sound. We now make use
of \cref{thm:se} and show that $\sonicprotfs$ is \COMMENT{ forking }simulation-extractable as defined in \cref{def:simext}.

\begin{corollary}[\COMMENT{Forking s}Simulation extractability of $\sonicprotfs$]
  \label{thm:sonicprotfs_se}
  Assume that $\sonicprot$ is $\ur{1}$ with security
  $\epsur(\secpar) = \epsbind(\secpar) + \epsop(\secpar)$ -- where
  $\epsbind (\secpar)$ is polynomial commitment's binding security, $\epsop$ is
  polynomial commitment unique opening security -- and forking-sound with
  security $\epsss(\secpar)$. Let $\ro\colon \bin^* \to \bin^\secpar$ be a
  random oracle. Let $\advse$ be an adversary that can make up to $q$
  random oracle queries, up to $S$ simulation oracle queries, and outputs an
  acceptable proof for $\sonicprotfs$ with probability at least $\accProb$. Then
  $\sonicprotfs$ is \COMMENT{forking }simulation-extractable with extraction error
  $\eta = \epsur(\secpar)$. The extraction probability $\extProb$ is at least
\[
		\extProb  \geq \frac{1}{q^{\multconstr + \linconstr}} (\accProb - \epsur(\secpar))^{\multconstr +
		\linconstr + 1} - \eps(\secpar).
	\]
	for some negligible $\eps(\secpar)$, $\multconstr$ and $\linconstr$ being,
  respectively, the number of multiplicative and linear constrains of the system.
\end{corollary}

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


% !TEX root = ../main.tex
%-------------------------------------------------------------------------------
\section{Computational implementation}\label{Computational implementation}
%-------------------------------------------------------------------------------
We use the same computational implementation as in \citet{Keane.1997}. We outline the immediate utility functions for each of the five alternatives. We first focus on their common overall structure and then present their parameterization. Throughout we provide the economic motivation for their specification.\\

\noindent We follow individuals over their working life from young adulthood at age 16 to retirement at age 65. The decision period $t = 16, \dots, 65$  is a school year and individuals decide $a\in\mathcal{A}$ whether to work in a blue-collar or white-collar occupation ($a = 1, 2$), to serve in the military $(a = 3)$, to attend school $(a = 4)$, or to stay at home $(a = 5)$.\\

\noindent Individuals are initially heterogeneous. They differ with respect to their initial level of completed schooling $h_{16}$ and have one of four different $\mathcal{J} = \{1, \hdots, 4\}$ alternative-specific skill endowments $\bm{e} = \left(e_{j,a}\right)_{\mathcal{J} \times \mathcal{A}}$.\\

\noindent The immediate utility $u_a(\cdot)$ of each alternative consists of a non-pecuniary utility $\zeta_a(\cdot)$ and, at least for the working alternatives, an additional wage component $w_a(\cdot)$. Both depend on the level of human capital as measured by their alternative-specific skill endowment $\bm{e}$, years of completed schooling $h_t$, and occupation-specific work experience $\bm{k_t} = \left(k_{a,t}\right)_{a\in\{1, 2, 3\}}$. The immediate utility functions are influenced by last-period choices $a_{t -1}$ and alternative-specific productivity shocks $\bm{\epsilon_t} = \left(\epsilon_{a,t}\right)_{a\in\mathcal{A}}$ as well. Their general form is given by:
%
\begin{align*}
u_a(\cdot) =
\begin{cases}
    \zeta_a(\bm{k_t}, h_t, t, a_{t -1})  + w_a(\bm{k_t}, h_t, t, a_{t -1}, e_{j, a}, \epsilon_{a,t})                & \text{if}\, a \in \{1, 2, 3\}  \\
    \zeta_a(\bm{k_t}, h_t, t, a_{t-1}, e_{j,a}, \epsilon_{a,t})                                                  &  \text{if}\, a \in \{4, 5\}.
\end{cases}
\end{align*}
%
Work experience $\bm{k_t}$  and years of completed schooling $h_t$ evolve deterministically.
%
\begin{align*}
k_{a,t+1} = k_{a,t} + \ind[a_t = a]  &\qquad \text{if}\, a \in \{1, 2, 3\} \\
h_{t + 1\phantom{,a}} = h_{t\phantom{,a}} +   \ind[a_t = 4]  &\qquad
\end{align*}
%
\noindent The productivity shocks are uncorrelated across time and follow a multivariate normal distribution with mean $\bm{0}$ and covariance matrix $\bm{\Sigma}$. Given the structure of the utility functions and the distribution of the shocks, the state at time $t$ is $s_t = \{\bm{k_t}, h_t, t, a_{t -1}, \bm{e},\bm{\epsilon_t}\}$.\\

\noindent Empirical and theoretical research from specialized disciplines within economics informs the exact specification of $u_a(\cdot)$. We now discuss each of its components in detail.
%-------------------------------------------------------------------------------
\subsection{Non-pecuniary utility}
%-------------------------------------------------------------------------------
We present the parameterization of the non-pecuniary utility for all five alternatives.
%-------------------------------------------------------------------------------
\subsubsection*{Blue-collar}
%-------------------------------------------------------------------------------
\noindent Equation (\ref{eq:NonWageBLueCollar}) shows the parameterization of the non-pecuniary utility from working in a blue-collar occupation.
%
\begin{align}\label{eq:NonWageBLueCollar}
\zeta_{1}(\bm{k_t}, h_t, a_{t-1})  = \alpha_1  &+ c_{1,1} \cdot \ind[a_{t-1} \neq 1] + c_{1,2} \cdot \ind[k_{1,t} = 0] \\ \nonumber
                            & + \vartheta_1 \cdot \ind[h_t \geq 12] + \vartheta_2 \cdot \ind[h_t \geq 16] + \vartheta_3 \cdot \ind[k_{3,t} = 1]
\end{align}
%
A constant $\alpha_1$ captures the net monetary-equivalent of on the job amenities. The non-pecuniary utility includes mobility and search costs $c_{1,1}$, which are higher for individuals who never worked in a blue-collar occupation before $c_{1,2}$. The non-pecuniary utilities capture returns from a high school $\vartheta_1$ and a college $\vartheta_2$ degree. Additionally, there is a detrimental effect of leaving the military early after one year $\vartheta_3$.
%-------------------------------------------------------------------------------
\subsubsection*{White-collar}
%-------------------------------------------------------------------------------
The non-pecuniary utility from working in a white-collar occupation is specified analogously. Equation (\ref{eq:UtilityWhiteCollar}) shows its parameterization.
%
\begin{align}\label{eq:UtilityWhiteCollar}
\zeta_{2}( \bm{k_t}, h_t, a_{t-1} ) = \,\alpha_2 & + c_{2,1} \cdot \ind[a_{t-1} \neq 2] + c_{2,2} \cdot \ind[k_{2,t} = 0]\\\nonumber
                            & + \vartheta_1 \cdot \ind[h_t \geq 12] + \vartheta_2 \cdot \ind[h_t \geq 16] + \vartheta_3 \cdot \ind[k_{3,t} = 1]
\end{align}
%-------------------------------------------------------------------------------
\subsubsection*{Military}
%-------------------------------------------------------------------------------
\noindent Equation (\ref{eq:UtilityMilitary}) shows the parameterization of the non-pecuniary utility from working in the military.
%
\begin{align}\label{eq:UtilityMilitary}
\zeta_{3}( k_{3.t}, h_t)  = \,& c_{3,2} \cdot \ind[k_{3,t} = 0]+ \vartheta_1 \cdot \ind[h_t \geq 12] + \vartheta_2 \cdot \ind[h_t \geq 16]
\end{align}
%
Search costs $c_{3, 1} = 0$ are absent but there is a mobility cost if an individual has never served in the military before $c_{3,2}$. Individuals still experience a non-pecuniary utility from finishing high-school $\vartheta_1$ and college $\vartheta_2$.
%-------------------------------------------------------------------------------
\subsubsection*{School}
%-------------------------------------------------------------------------------
Equation (\ref{eq:UtilitySchooling}) shows the parameterization of the non-pecuniary utility from schooling.
%
\begin{align}\label{eq:UtilitySchooling}
	\zeta_4(k_{3,t}, h_t, t, a_{t-1}, e_{j,4}, \epsilon_{4,t})  = e_{j,4} & + \beta_{tc_1} \cdot \ind[h_t \geq 12] + \beta_{tc_2} \cdot \ind[h_t \geq 16]   \\\nonumber
    							  & + \beta_{rc_1} \cdot \ind[a_{t-1} \neq 4, h_t < 12] + \beta_{rc_2} \cdot \ind[a_{t-1} \neq 4, h_t \geq 12] \\\nonumber
    							  & + \gamma_{4,4} \cdot t + \gamma_{4,5} \cdot \ind[t < 18] 																					  \\\nonumber
     							  & + \vartheta_1 \cdot \ind[h_t \geq 12] + \vartheta_2 \cdot \ind[h_t \geq 16] + \vartheta_3 \cdot \ind[k_{3,t} = 1]\\\nonumber
      							& + \epsilon_{4,t}
\end{align}
%
There is a direct cost of attending school such as tuition for continuing education after high school $\beta_{tc_1}$ and college $\beta_{tc_2}$. The decision to leave school is reversible, but entails adjustment costs that differ by schooling category ($\beta_{rc_1}, \beta_{rc_2}$). Schooling is defined as time spent in school and not by formal credentials acquired. Once individuals reach a certain amount of schooling, they acquire a degree. There is no uncertainty about grade completion \citep{Altonji.1993} and no part-time enrollment. Individuals value the completion of high-school and graduate school ($\vartheta_1, \vartheta_2$).
%-------------------------------------------------------------------------------
\subsubsection*{Home}
%-------------------------------------------------------------------------------
Equation (\ref{eq:UtilityHome}) shows the parameterization of the non-pecuniary utility from staying at home.
%
\begin{align}\label{eq:UtilityHome}
	\zeta_5(k_{3,t}, h_t, t, e_{j,5}, \epsilon_{5,1}) =  e_{j,5} & + \gamma_{5,4} \cdot \ind[18 \leq t \leq 20] + \gamma_{5,5} \cdot \ind[t \geq 21] \\ \nonumber
    							   & +\vartheta_{1} \cdot \ind[h_t \geq 12] + \vartheta_{2} \cdot \ind[h_t \geq 16] +  \vartheta_3 \cdot \ind[k_{3,t} = 1]  \\ \nonumber
    							   & + \epsilon_{5,t}
\end{align}
%
Staying at home as a young adult $\gamma_{5, 4}$ is less stigmatic as doing so while already being an adult $\gamma_{5,5}$. Additionally, possessing a degree  $(\vartheta_1, \vartheta_2)$ or leaving the military prematurely $\vartheta_3$ influences the immediate utility.
%-------------------------------------------------------------------------------
\subsection{Wage component}
%-------------------------------------------------------------------------------
The wage component $w_{a}(\cdot)$ for the working alternatives is given by the product of the market-equilibrium rental price $r_{a}$ and an occupation-specific skill level $x_{a}(\cdot)$. The latter is determined by the overall level of human capital.
%
\begin{align*}
w_{a}(\cdot) = r_{a} \, x_{a}(\cdot)
\end{align*}
%
This specification leads to a standard logarithmic wage equation in which the constant term is the skill rental price $\ln(r_{a})$ and wages follow a log-normal distribution.\\

\noindent The occupation-specific skill level $x_{a}(\cdot)$ is determined by a skill production function, which includes a deterministic component $\Gamma_a(\cdot)$ and a multiplicative stochastic productivity shock $\epsilon_{a,t}$.
%
\begin{align}
    x_{a}(\bm{k_t}, h_t, t, a_{t-1}, e_{j, a}, \epsilon_{a,t}) & = \exp \big( \Gamma_{a}(\bm{k_t},  h_t, t, a_{t-1}, e_{j,a}) \cdot \epsilon_{a,t} \big) \nonumber
\end{align}
%-------------------------------------------------------------------------------
\subsubsection*{Blue-collar}
%-------------------------------------------------------------------------------
Equation (\ref{eq:SkillLevelBlueCollar}) shows the parameterization of the deterministic component of the skill production function.
%
\begin{align}\label{eq:SkillLevelBlueCollar}
    \Gamma_1(\bm{k_t}, h_t, t, a_{t-1}, e_{j, 1}) = e_{j,1} & + \beta_{1,1} \cdot h_t + \beta_{1, 2} \cdot \ind[h_t \geq 12] + \beta_{1,3} \cdot \ind[h_t\geq 16]\\ \nonumber
                                  & + \gamma_{1, 1} \cdot  k_{1,t} + \gamma_{1,2} \cdot  (k_{1,t})^2 + \gamma_{1,3} \cdot  \ind[k_{1,t} > 0] \\ \nonumber
                                & + \gamma_{1,4} \cdot  t + \gamma_{1,5} \cdot \ind[t < 18]\\ \nonumber
                                  & + \gamma_{1,6} \cdot \ind[a_{t-1} = 1] + \gamma_{1,7} \cdot  k_{2,t} + \gamma_{1,8} \cdot  k_{3,t} \nonumber
\end{align}
%
There are several notable features. The first part of the skill production function is motivated by \citet{Mincer.1958, Mincer.1974} and hence linear in years of completed schooling $\beta_{1,1}$, quadratic in experience ($\gamma_{1,1}, \gamma_{1,2}$), and separable between the two of them. There are so-called sheep-skin effects \citep{Spence.1973, Jaeger.1996} associated with completing a high school $\beta_{1,2}$ and graduate $\beta_{1,3}$ education that capture the impact of completing a degree beyond just the associated years of schooling. Also, skills depreciate when not employed in a blue-collar occupation in the preceding period $\gamma_{1,6}$. Other work experience ($\gamma_{1,7}, \gamma_{1,8}$) is transferable.
%-------------------------------------------------------------------------------
\subsubsection*{White-collar}
%-------------------------------------------------------------------------------
The wage component from working in a white-collar occupation is specified analogously. Equation (\ref{eq:SkillLevelWhiteCollar}) shows the parameterization of the deterministic component of the skill production function.
%
\begin{align}\label{eq:SkillLevelWhiteCollar}
    \Gamma_2(\bm{k_t}, h_t, t, a_{t-1}, e_{j,2}) = e_{j,2} & + \beta_{2,1} \cdot h_t + \beta_{2, 2} \cdot \ind[h_t \geq 12] + \beta_{2,3} \cdot \ind[h_t\geq 16] \\\nonumber
    							 & + \gamma_{2, 1} \cdot  k_{2,t} + \gamma_{2,2} \cdot  (k_{2,t})^2 + \gamma_{2,3} \cdot  \ind[k_{2,t} > 0] \\\nonumber
                                   & + \gamma_{2,4} \cdot  t + \gamma_{2,5} \cdot \ind[t < 18] \\\nonumber
                                  & + \gamma_{2,6} \cdot  \ind[a_{t-1} = 2]  + \gamma_{2,7} \cdot  k_{1,t} + \gamma_{2,8} \cdot  k_{3,t}
\end{align}
%-------------------------------------------------------------------------------
\subsubsection*{Military}
%-------------------------------------------------------------------------------
Equation (\ref{eq:SkillLevelMilitary}) shows the parameterization of the deterministic component of the skill production function.
%
\begin{align}\label{eq:SkillLevelMilitary}
    \Gamma_3( k_{3,t}, h_t, t, e_{j,3}) = e_{j,3} & + \beta_{3,1} \cdot h_t \\\nonumber
	               \nonumber &+ \gamma_{3,1} \cdot  k_{3,t} + \gamma_{3,2} \cdot (k_{3,t})^2 + \gamma_{3,3} \cdot \ind[k_{3,t} > 0]\\\nonumber
									 & + \gamma_{3,4} \cdot t + \gamma_{3,5} \cdot \ind[t < 18]
\end{align}
%
Contrary to the civilian sector there are no sheep-skin effects from graduation ($\beta_{3,2} = \beta_{3,3}= 0$). The previous occupational choice has no influence ($\gamma_{3,6}= 0$) and any experience other than military is non-transferable ($\gamma_{3,7} = \gamma_{3,8} = 0$).

\begin{Remark} Our parameterization for the immediate utility of serving in the military differs from \citet{Keane.1997} as we remain unsure about their exact specification. The authors state in Footnote 31 (p.498) that the constant for the non-pecuniary utility $\alpha_{3,t}$ depends on age. However, we are unable to determine the precise nature of the relationship. Equation (C3) (p.521) also indicates no productivity shock $\epsilon_{a,t}$ in the wage component. Table 7 (p.500) reports such estimates.
\end{Remark}
%-------------------------------------------------------------------------------
\FloatBarrier\subsection{Overview parameters}
%-------------------------------------------------------------------------------

\input{../material/tab-model-parameters}


Dispersion (DSP) Package information is read from the file that is specified by ``DSP6'' as the file type.  Only one DSP Package can be specified for a GWT model.  The DSP Package is based on the mathematical formulation presented for the XT3D option of the NPF Package available to represent full three-dimensional anisotropy in groundwater flow.  XT3D can be computationally expensive and can be turned off to use a simplified and approximate form of the dispersion equations.  For most problems, however, XT3D will be required to accurately represent dispersion.

\vspace{5mm}
\subsubsection{Structure of Blocks}
\lstinputlisting[style=blockdefinition]{./mf6ivar/tex/gwt-dsp-options.dat}
\lstinputlisting[style=blockdefinition]{./mf6ivar/tex/gwt-dsp-griddata.dat}

\vspace{5mm}
\subsubsection{Explanation of Variables}
\begin{description}
\input{./mf6ivar/tex/gwt-dsp-desc.tex}
\end{description}

\vspace{5mm}
\subsubsection{Example Input File}
\lstinputlisting[style=inputfile]{./mf6ivar/examples/gwt-dsp-example.dat}


\section{Nuisance parameters}
\label{sec:NPs}

The expected numbers and pdf shapes of signal and background events also depend on a series of systematic uncertainties,
which are described as a set of nuisance parameters (NPs).
As showed in equation~\ref{eq:likelihoodf}, $\pmb{\theta}$ is a set of NPs that plays as an additional ``penalty" term to likelihood function,
which will increase the negative log likelihood when any nuisance parameter is shifted from its nominal value.
Usually those NPs are constrained by using Gaussian function with their estimated uncertainties provided by the experiment condition.


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

\section{Dirac equation coupled to an external EM field}

\marginpar{Thursday\\ 2020-4-30, \\ compiled \\ \today}

We can use a complex solution to the Dirac equation to describe a spin \(1/2\) particle.

As we did before, we make the minimal coupling ansatz:
%
\begin{align}
\partial_{\mu } \to \DD_{\mu } = \partial_{\mu } + i q A_{\mu }
\,,
\end{align}
%
so that our Dirac equation will read
%
\begin{align}
\qty(i\slashed{\DD} - M ) \psi = \qty(i \slashed{\partial} - q \slashed{A} - M ) = 0
\,,
\end{align}
%
which in three-vector notation (after the computation in the equations \eqref{eq:dirac-equation-from-ansatz-to-gamma}) reads
%
\begin{align}
i \partial_0 \psi = \qty[i \vec{\alpha} \cdot \qty(- \vec{\nabla} + i q \vec{A}) + \beta M + q A_0 ] \psi
\,,
\end{align}
%
where we are using the definition \(\partial_{i} = \vec{\nabla}\), while \(A^{i} = \vec{A} \): the ``natural'' placement for the spatial index of the derivative is lower, while for other vectors it is upper.

\subsection{Nonrelativistic limit of the Dirac equation}

As we did in the case of the Klein-Gordon equation, we start out by factoring out the time-evolution: we define
%
\begin{align}
\psi (\vec{x}, t) = e^{-i M t} \psi' (\vec{x}, t)
\,,
\end{align}
%
which we can plug into the expression for the Dirac equation: we find
%
\begin{subequations}
\begin{align}
i \partial_0 \qty(e^{-iMt} \psi ') &= \qty[i \vec{\alpha} \cdot \qty(- \vec{\nabla} + i q \vec{A}) + \beta M + q A_0 ] \qty(e^{-iMt} \psi')  \\
e^{-iMt } \qty(- i^2 + i \partial_0 ) \psi' &=
e^{-iMt} \qty[i \vec{\alpha} \cdot \qty(- \vec{\nabla} + i q \vec{A}) + \beta M + q A_0 ] \psi'  \\
i \partial_0 \psi' &= \qty[i \vec{\alpha} \cdot \qty(- \vec{\nabla} + i q \vec{A}) + (\beta - \mathbb{1}) M + q A_0 ] \psi'
\,,
\end{align}
\end{subequations}
%
which we can write using the explicit spinorial expressions for the \(\vec{\alpha}\) and \(\beta \) matrices; for clarity we also divide \(\psi '\) into two two-component spinors \(\varphi'\) and \(\chi '\):
%
\begin{subequations}
\begin{align}
\begin{split}
i \partial_0
\left[\begin{array}{c}
\varphi' \\
\chi '
\end{array}\right]
 &=
\left[\begin{array}{cc}
0 & i \vec{\sigma} \cdot (-\vec{\nabla} + i q \vec{A})  \\
i \vec{\sigma} \cdot (-\vec{\nabla} + i q \vec{A}) & 0
\end{array}\right]
\left[\begin{array}{c}
    \varphi' \\
    \chi '
\end{array}\right] \\
&
+ \left[\begin{array}{cc}
0 & 0 \\
0 & -2M
\end{array}\right] \left[\begin{array}{c}
\varphi' \\
\chi '
\end{array}\right]
\\
&
+ \left[\begin{array}{cc}
qA_0  & 0 \\
0 & q A_0
\end{array}\right] \left[\begin{array}{c}
\varphi' \\
\chi '
\end{array}\right]
\,.
\end{split}
\end{align}
\end{subequations}

Then we can read off the two \emph{coupled} equations for these 2D spinors:
%
\begin{subequations}
\begin{align}
i \partial_0 \varphi ' &= i \vec{\sigma} \cdot \qty(- \vec{\nabla} + i q \vec{A}) \chi '
+ q A_0 \varphi '  \\
i \partial_0 \chi ' &= i \vec{\sigma} \cdot \qty(- \vec{\nabla} + i q \vec{A}) \varphi ' +
(- 2 M + q A_0 ) \chi '
\,.
\end{align}
\end{subequations}

Notice that the mass only appears in the second equation: so, we can apply the nonrelativistic approximation, which amounts to saying that the mass \(M\) is the largest energy at play; formally, this means
%
\begin{align}
\abs{q A_0} \ll M
\qquad \text{and} \qquad
\abs{\frac{\partial_0 \chi '}{\chi '}} \ll M
\,,
\end{align}
%
so we remove those terms in the second equation; if we bring the term \(2 M \chi '\) to the left hand side we get:
%
\begin{align}
\chi ' =  \frac{i}{2M} \vec{\sigma} \cdot \qty(- \vec{\nabla} + i q \vec{A}) \varphi '
\,,
\end{align}
%
so we have an explicit constraint on the value of \(\chi '\) if we know \(\varphi '\).

We do not know \emph{a priori} the asymptotic relation of \(\chi '\) to \(\varphi '\), so we only made cancellations in terms which were comparable since they were all applied to \(\chi '\).

Now, then we have found that, since \(\chi ' \sim \varphi ' / M\), the magnitude of \(\varphi '\) is much larger than that of \(\chi '\).

We can substitute what we found for \(\chi '\) into the first equation: we find
%
\begin{subequations}
\begin{align}
i \partial_0 \varphi ' &= \frac{1}{2M} \qty[i \vec{\sigma} \cdot \qty(- \vec{\nabla} + iq \vec{A})]^2 \varphi ' + q A_0 \varphi '  \\
&= q A_0 \varphi ' - \frac{1}{2M} \qty[- \vec{\sigma} \cdot \vec{\nabla} + iq \vec{\sigma} \cdot \vec{A}]^2 \varphi '
\,.
\end{align}
\end{subequations}

This is the \emph{Pauli equation coupled to an external magnetic field}, the generalization of the Schrödinger equation to spin \(1/2\) charged particles.

We can make it more explicit by squaring the differential operator.

The sign convention in the notes is weird, in that a 3D vector written as \(\vec{x}\) does not consistently mean neither \(x^{i}\) nor \(x_{i}\).
Instead, we have \(\vec{\nabla} = \partial_{i}  \) and \(\vec{A} = A^{i}\).

When we write out a scalar product we imply that one index should be upper and one should be lower, if this is not the case already one of them must be raised or lowered. Once we are in component notation, though, we can move indices around as we like.

So, explicitly we have
%
\begin{subequations}
\begin{align}
\qty(- \vec{\sigma} \cdot \vec{\nabla} + i q \vec{\sigma} \cdot \vec{A})^2 \varphi'
&= \qty(- \sigma^{i} \partial_{i} + i q \sigma^{i} A^{i})^2 \varphi'  \\
&= \qty(- \sigma^{i} \partial_{i} - i q \sigma^{i} A_{i})^2 \varphi'  \\
&= \qty( \sigma_{i} \partial_{i} + i q \sigma_{i} A_{i})^2 \varphi'  \\
&= \qty( \sigma_{i} \partial_{i} + i q \sigma_{i} A_{i})
\qty( \sigma_{j} \partial_{j} + i q \sigma_{j} A_{j}) \varphi '  \\
&= \qty( \sigma_{i} \partial_{i} + i q \sigma_{i} A_{i})
\qty(\sigma_{j} \qty(\partial_{j} \varphi ') + i q \sigma_{j} A_{j} \varphi ')  \\
&= \sigma_{i} \sigma_{j} \qty[\partial_{i} \partial_{j} + iq \qty(\partial_{i} A_{j} + A_{j} \partial_{i} + A_{i} \partial_{j} ) - q^2 A_{i} A_{j}] \varphi '
\,,
\end{align}
\end{subequations}
%
and now we notice that all the terms except for \(\partial_{i} A_{j}\) are symmetric in \(ij\): so, we split the product \(\sigma_{i} \sigma_{j}\) into its symmetric and antisymmetric parts.
This yields
%
\begin{align}
\sigma_{i} \sigma_{j} = \frac{1}{2} \qty{\sigma_{i}, \sigma_{j}} + \frac{1}{2} \qty[\sigma_{i}, \sigma_{j}] = \delta_{ij} + i \epsilon_{ijk} \sigma_{k}
\,,
\end{align}
%
so we find
%
\begin{subequations}
\begin{align}
\qty(- \vec{\sigma} \cdot \vec{\nabla} + i q \vec{\sigma} \cdot \vec{A})^2 \varphi'
&=
\qty(\delta_{ij} + i \epsilon_{ijk} \sigma_{k})
\qty[\partial_{i} \partial_{j} + iq \qty(\partial_{i} A_{j} + A_{j} \partial_{i} + A_{i} \partial_{j} ) - q^2 A_{i} A_{j}] \varphi '  \\
&= \qty[\partial_{i} \partial_{i} + i q \qty( \partial_{i} A_{i} + 2A_{i} \partial_{i}) - q^2 A_{i} A_{i}] \varphi '
+ \qty[i^2 q \partial_{i} A_{j} \epsilon^{ijk} \sigma_{k}] \varphi '  \\
&= \qty[\vec{\nabla}^2 - iq \qty(\vec{\nabla} \cdot \vec{A} + 2 \vec{A} \cdot \vec{\nabla}) - q^2 \vec{A}^2
+ \vec{\sigma} \cdot \vec{B} ] \varphi'  \\
&= \qty[- \vec{\nabla} + iq \vec{A}]^2 \varphi + q\vec{\sigma} \cdot \vec{B} \varphi '
\,,
\end{align}
\end{subequations}
%
where we used the fact that \(\partial_{i} A_{j} \epsilon_{ijk} = \frac{1}{2} F_{ij} \epsilon_{ijk}  = B_{k}\), and in the last step we set \(\nabla \cdot \vec{A} = 0\), the Coulomb gauge condition.

Notice that we had to raise the indices of both \(\vec{A}\) and \(\vec{B}\), so we had to switch the sign of those terms.

Then, we can finally write the full Pauli equation:
%
\begin{align}
i \partial_0 \varphi ' = \qty{- \frac{1}{2M} \qty[- \vec{\nabla} + i q \vec{A}]^2  + q A_0 - \frac{q}{2M} \vec{\sigma} \cdot \vec{B} } \varphi '
\,,
\end{align}
%
which differs from the minimally-coupled Schrödinger equation by the spin coupling to the magnetic field.
This new term in the Hamiltonian is
%
\begin{align}
H _{\text{dip}}= - \frac{q}{2M} \vec{\sigma} \cdot \vec{B} = - \vec{\mu}_{s} \cdot \vec{B}
\,,
\end{align}
%
where we introduced the \textbf{intrinsic magnetic moment}
%
\begin{align} \label{eq:intrinsic-magnetic-moment}
\vec{\mu}_{s} = \frac{q}{2M} \vec{\sigma} = \frac{q}{M} \vec{\Sigma}^{(3)}
\,.
\end{align}

In nonrelativistic quantum mechanics this term is introduced by hand: if the wavefunction is a scalar there is no way for this term to come about. Once we start describing it as a spinor, though, we can see where the term comes from.

The \textbf{magnetic dipole moment} associated with the \emph{orbital angular momentum}, as opposed to the spin, is defined as
%
\begin{align}
\vec{\mu}_{L} = \frac{q}{2M } \vec{L}
\,,
\end{align}
%
so in this case the ratio of magnetic moment to momentum is
%
\begin{align}
\frac{\abs{\vec{\mu}_{L}}}{\abs{\vec{L}}} = \frac{q}{2M}
\,,
\end{align}
%
while for the spin we defined (already in nonrelativistic QM)
%
\begin{align}
\vec{\mu}_{s} = \frac{q}{2M} g_e \vec{\Sigma}^{(3)}
\qquad \implies \qquad
\frac{\abs{\vec{\mu}_{s}}}{\abs{\vec{\Sigma}}} = \frac{q}{2M} g_{e}
\,,
\end{align}
%
where we define the \textbf{electron gyromagnetic factor} \(g_e\).
This can be compared with the equation we found before for the intrinsic magnetic moment \eqref{eq:intrinsic-magnetic-moment}, to yield our prediction:
%
\begin{align}
g_e = 2
\,.
\end{align}

\begin{claim}
We have the relation
%
\begin{align} \label{eq:antisymmetric-covariant-derivative-field-strength}
\qty[i \partial_{\mu } - q A_{\mu }, i \partial_{\nu } - q A_{\nu }] \psi
= -iq F_{\mu \nu } \psi
\,.
\end{align}
\end{claim}

\begin{proof}
We write the terms of the product out, antisymmetrizing everything:
%
\begin{subequations}
\begin{align}
&\qty[i \partial_{\mu } - q A_{\mu }, i \partial_{\nu } - q A_{\nu }] \psi = \\
&=2 \qty(i \partial_{[\mu }i \partial_{\nu ]}
- i q \qty(\partial_{[\mu } A_{\nu ]} )
-iq A_{[\nu } \partial_{\mu ]}
-iq A_{[\mu } \partial_{\nu ]}
+ q^2 A_{[\mu } A_{\nu ]}) \psi   \\
&= -2iq \partial_{[\mu } A_{\nu ]} \psi
= -iq F_{\mu \nu } \psi
\,,
\end{align}
\end{subequations}
%
where we removed all the terms which were symmetric in \(\mu \leftrightarrow \nu \).

Note that this is the commutator of the covariant derivatives on the manifold: it yields the Riemann tensor, which we then have shown to be given by the electromagnetic field-strength.
\end{proof}

\begin{claim}
We have the relation
%
\begin{align}
\Sigma^{\mu \nu }F_{\mu \nu }
=i \vec{\alpha} \cdot \vec{E} + \vec{\Sigma} \cdot \vec{B}
\,,
\end{align}
%
where
%
\begin{subequations}
\begin{align}
\vec{\alpha} = \left[\begin{array}{cc}
0 & \vec{\sigma} \\
\vec{\sigma} & 0
\end{array}\right]
\qquad \text{and} \qquad
\vec{\Sigma} = \frac{1}{2} \left[\begin{array}{cc}
\vec{\sigma} & 0 \\
0 & \vec{\sigma}
\end{array}\right]
\,.
\end{align}
\end{subequations}
\end{claim}

\begin{proof}
Recall the definitions of
%
\begin{align}
\Sigma^{\mu \nu } = \frac{i}{4} \qty[\gamma^{\mu }, \gamma^{\nu }]
\,
\end{align}
%
and of the the electric and magnetic fields in terms of the field-strength tensor:
%
\begin{align}
E^{i} = F^{i0} = - F^{0i} = +F_{0i}
\qquad \text{and} \qquad
B^{k} = \frac{1}{2} F^{ij} \epsilon^{ijk}
\,.
\end{align}

For the magnetic field we can also write the inverse expression:
%
\begin{align}
F^{ij} = \epsilon^{ijk} B^{k}
\,,
\end{align}
%

So, we distinguish the cases where \(\mu = 0\) and \(\mu = i\), a spatial 3D index.

In the first case, the index \(\nu \) must be nonzero by antisymmetry, so we find
%
\begin{align}
\Sigma^{0j}F_{0j} &= \frac{i}{4} \qty[\gamma^{0}, \gamma^{j}] E^{j}
\,,
\end{align}
%
and since different \(\gamma \) matrices anticommute we can replace the commutator with twice the product:
%
\begin{align}
\Sigma^{0j} F_{0j} &= \frac{i}{2} \gamma^{0} \gamma^{j} E^{j} = \frac{i}{2} \gamma^{0} \gamma^{0} \alpha^{j} E^{j} = \frac{i}{2} \vec{\alpha} \cdot \vec{E}
\,.
\end{align}

In the final expression we are summing over \(\mu \) and \(\nu \), so the contribution will be twice this, since we need to account for the case where \(\nu =0\) as well as \(\mu =0 \).

In the other case, we apply a similar reasoning; in this case we also need to recall the definition of the vector \(\vec{\Sigma}\):
%
\begin{align}
\Sigma^{i} = \frac{1}{2} \epsilon^{ijk} \Sigma^{jk}
\,,
\end{align}
%
so we can substitute this into the expression and find:
%
\begin{subequations}
\begin{align}
\Sigma^{ij} F_{ij} &= \Sigma^{ij} F^{ij}  \\
&= \Sigma^{ij} \tensor{\epsilon }{^{ijk}} B_{k}  \\
&= 2 \vec{\Sigma}^{k} B_{k}
\,,
\end{align}
\end{subequations}
%
\todo[inline]{which has an extra factor two\dots should figure out why this is the case}
\end{proof}

\begin{claim}
We can also derive the prediction \(g_e = 2\) from the full relativistic Dirac equation.
\end{claim}

\begin{proof}
We start by applying the operator \(i\slashed{\DD} + M\) to the relativistic Dirac equation to an external electromagnetic field, just like what we did to recover the Klein-Gordon equation: we find
%
\begin{subequations}
\begin{align}
\qty(-\slashed{\DD}^2 - M^2) \psi &= 0  \\
\qty[ - \qty(\partial_{\mu } +ieA_{\mu }) \gamma^{\mu } \qty(\partial_{\nu } + ieA_{\nu }) \gamma^{\nu } - M^2] \psi  & =0
\,,
\end{align}
\end{subequations}
%
since \(\DD_{\mu } = \partial_{\mu } + ieA_{\mu }\).
We have a product of gamma matrices: we can decompose it into its symmetric and antisymmetric parts, as
%
\begin{subequations}
\begin{align}
\gamma^{\mu } \gamma^{\nu } &= \frac{1}{2} \qty{\gamma^{\mu }, \gamma^{\nu }}  + \frac{1}{2} \qty[\gamma^{\mu } , \gamma^{\nu }]\\
&= \eta^{\mu \nu } -2i \Sigma^{\mu \nu }
\,,
\end{align}
\end{subequations}
%
since
%
\begin{align}
\Sigma^{\mu \nu } = \frac{i}{4} \qty[\gamma^{\mu }, \gamma^{\nu }]
\,.
\end{align}

Therefore, we find
%
\begin{subequations}
\begin{align}
\qty[\qty(\partial_{\mu } + i e A_{\mu }) \qty(\partial^{\mu } + i e A^{\mu }) - 2i \Sigma^{\mu \nu } \qty(\partial_{\mu } +ieA_{\mu }) \qty(\partial_{\nu } + ieA_{\nu }) - M^2] \psi &= 0  \\
\qty[-\DD_{\mu } \DD^{\mu} + 2i \Sigma^{\mu \nu } \DD_{[\mu } \DD_{\nu ]}  - M^2] \psi &= 0
\,,
\end{align}
\end{subequations}
%
and we can expand the antisymmetrized covariant derivative given what we know from equation \eqref{eq:antisymmetric-covariant-derivative-field-strength}:
%
\begin{subequations}
\begin{align}
-iq F_{\mu \nu } \psi &=
\qty[i \partial_{\mu } - q A_{\mu}, i \partial_{\nu } - q A_{\nu }] \psi \\
&= \qty[i \DD_{\mu }, i \DD_{\nu }]\psi  \\
&= - 2 \DD_{[\mu } \DD_{\nu ]} \psi  \\
\,,
\end{align}
\end{subequations}
%
so we can write
%
\begin{subequations}
\begin{align}
\qty[-\DD_{\mu } \DD^{\mu} + 2i \Sigma^{\mu \nu } \qty(\frac{1}{2} iq F_{\mu \nu }) - M^2] \psi &= 0  \\
\qty[-\DD_{\mu } \DD^{\mu} - \Sigma^{\mu \nu }  q F_{\mu \nu } - M^2] \psi &= 0  \\
\qty[-\DD_{\mu } \DD^{\mu} - g_e \frac{q}{2} \Sigma^{\mu \nu }  F_{\mu \nu } - M^2] \psi &= 0
\,,
\end{align}
\end{subequations}
where \(g_e = 2\).
\end{proof}

\end{document}


% This LaTeX was auto-generated from an M-file by MATLAB.
% To make changes, update the M-file and republish this document.

\documentclass{article}
\usepackage{graphicx}
\usepackage{color}

\sloppy
\definecolor{lightgray}{gray}{0.5}
\setlength{\parindent}{0pt}

\begin{document}

\section*{THE FACTORIZE OBJECT for solving linear systems}

\begin{par}
Copyright 2011-2012, Timothy A. Davis,
\begin{verbatim}http://www.suitesparse.com\end{verbatim}
\begin{verbatim}DrTimothyAldenDavis@gmail.com\end{verbatim} \end{par}
\vspace{1em}
\begin{par}
This is a demonstration of the FACTORIZE object for solving linear systems and
least-squares problems, and for computations with the matrix inverse and
pseudo-inverse.
\end{par} \vspace{1em}

\subsection*{Contents}

\begin{itemize}
\setlength{\itemsep}{-1ex}
   \item Rule Number One: never multiply by the inverse, inv(A)
   \item Rule Number Two:  never break Rule Number One
   \item How to use BACKSLASH solve A*x=b
   \item BACKSLASH versus INV ... let the battle begin
   \item LU and LINSOLVE are fast and accurate but complicated to use
   \item INV is easy to use, but slow and inaccurate
   \item So the winner is ... nobody
   \item The FACTORIZE object to the rescue
   \item Least-squares problems
   \item Underdetermined systems
   \item Computing selected entries in the inverse or pseudo-inverse
   \item Computing the entire inverse or pseudo-inverse
   \item Update/downdate of a dense Cholesky factorization
   \item Caveat Executor
   \item Summary
\end{itemize}

\subsection*{Rule Number One: never multiply by the inverse, inv(A)}

\begin{par}
Use backslash or a matrix factorization instead (LU, CHOL, or QR).
\end{par} \vspace{1em}

\subsection*{Rule Number Two:  never break Rule Number One}

\begin{par}
However, the problem with Rule Number One is that it can be hard to figure out
which matrix factorization to use and how to use it.  Using LU, CHOL, or QR is
complicated, particularly if you want the best performance.  BACKSLASH
(MLDIVIDE) is great, but it can't be reused when solving multiple systems
(x=A\ensuremath{\backslash}b and y=A\ensuremath{\backslash}c).  Its syntax
doesn't match the use of the inverse in mathematical expressions, either.
\end{par} \vspace{1em}
\begin{par}
The goal of the FACTORIZE object is to solve this problem ...
\end{par} \vspace{1em}
\begin{par}
"Don't let that INV go past your eyes; to solve that system, FACTORIZE!"
\end{par} \vspace{1em}

\subsection*{How to use BACKSLASH solve A*x=b}

\begin{par}
First, let's create a square matrix A and a right-hand-side b for a linear
system A*x=b.  There are many ways to solve this system.  The best way is to
use x=A\ensuremath{\backslash}b.  The residual r is a vector of what's left
over in each equation, and its norm tells you how accurately the system was
solved.
\end{par} \vspace{1em}
\begin{verbatim}
format compact ;
A = rand (3)
b = rand (3,1)
x = A\b
r = b-A*x ;
norm (r)
\end{verbatim}

        \color{lightgray} \begin{verbatim}A =
    0.2748    0.2719    0.3696
    0.2217    0.8329    0.5006
    0.9315    0.5791    0.0753
b =
    0.1293
    0.9138
    0.4589
x =
   -0.3827
    1.4657
   -0.4437
ans =
   1.2413e-16
\end{verbatim} \color{black}

\subsection*{BACKSLASH versus INV ... let the battle begin}

\begin{par}
The backslash operation x=A\ensuremath{\backslash}b is mathematically the same
as x=inv(A)*b. However, backslash is faster and more accurate since it uses a
matrix factorization instead of multiplying by the inverse.  Even though your
linear algebra textbook might write x=A\^{}(-1)*b as the solution to the system
A*x=b, your textbook author never means for you to compute the inverse.
\end{par} \vspace{1em}
\begin{par}
These next statements give the same answer, so what's the big deal?
\end{par} \vspace{1em}
\begin{verbatim}
S = inv(A) ;
x = S*b
x = A\b
\end{verbatim}

        \color{lightgray} \begin{verbatim}x =
   -0.3827
    1.4657
   -0.4437
x =
   -0.3827
    1.4657
   -0.4437
\end{verbatim} \color{black}
    \begin{par}
The big deal is that you should care about speed and you should care even more
about accuracy.  BACKSLASH relies on matrix factorization (LU, CHOL, QR, or
other specialized methods).  It's faster and more reliable than multiplying by
the inverse, particularly for large matrices and sparse matrices.  Here's an
illustration of how pathetic inv(A)*b can be.
\end{par} \vspace{1em}
\begin{verbatim}
A = gallery ('frank',16) ; xtrue = ones (16,1) ; b = A*xtrue ;

x = inv(A)*b ; norm (b-A*x)
x = A\b      ; norm (b-A*x)
\end{verbatim}

        \color{lightgray} \begin{verbatim}ans =
    0.0606
ans =
   1.7764e-15
\end{verbatim} \color{black}
    \begin{par}
The performance difference between BACKSLASH and INV for even small sparse
matrices is striking.
\end{par} \vspace{1em}
\begin{verbatim}
load west0479 ;
A = west0479 ;
n = size (A,1)
b = rand (n,1) ;
tic ; x = A\b ; toc
norm (b-A*x)
tic ; x = inv(A)*b ; toc
norm (b-A*x)
\end{verbatim}

        \color{lightgray} \begin{verbatim}n =
   479
Elapsed time is 0.002425 seconds.
ans =
   1.9174e-10
Elapsed time is 0.041780 seconds.
ans =
   3.4438e-09
\end{verbatim} \color{black}
    \begin{par}
What if you want to solve multiple systems?  Use a matrix factorization. But
which one?  And how do you use it?  Here are some alternatives using LU for the
sparse west0479 matrix, but some are faster than others.
\end{par} \vspace{1em}
\begin{verbatim}
tic ; [L,U]     = lu(A) ; x1 = U \ (L \ b)         ; t1=toc ; nz1=nnz(L+U);
tic ; [L,U,P]   = lu(A) ; x2 = U \ (L \ P*b)       ; t2=toc ; nz2=nnz(L+U);
tic ; [L,U,P,Q] = lu(A) ; x3 = Q * (U \ (L \ P*b)) ; t3=toc ; nz3=nnz(L+U);

fprintf ('1: nnz(L+U): %5d time: %8.4f resid: %e\n', nz1,t1, norm(b-A*x1));
fprintf ('2: nnz(L+U): %5d time: %8.4f resid: %e\n', nz2,t2, norm(b-A*x2));
fprintf ('3: nnz(L+U): %5d time: %8.4f resid: %e\n', nz3,t3, norm(b-A*x3));
\end{verbatim}

        \color{lightgray} \begin{verbatim}1: nnz(L+U): 16151 time:   0.0031 resid: 1.346712e-10
2: nnz(L+U): 15826 time:   0.0081 resid: 1.757002e-10
3: nnz(L+U):  3704 time:   0.0017 resid: 1.691750e-10
\end{verbatim} \color{black}

\subsection*{LU and LINSOLVE are fast and accurate but complicated to use}

\begin{par}
A quick look at ``help lu'' will scroll off your screen.  For full matrices,
[L,U,p] = lu (A,'vector') is fastest.  Then for the forward/backsolves, use
LINSOLVE instead of BACKSLASH for even faster performance.  But for sparse
matrices, use the optional 'Q' output of LU so you get a good fill-reducing
ordering.  But you can't use 'Q' if the matrix is full.  But LINSOLVE doesn't
work on sparse matrices.
\end{par} \vspace{1em}
\begin{par}
But ... Ack!  That's getting complicated ...
\end{par} \vspace{1em}
\begin{par}
Here's the best way to solve A*x=b and A*y=c when A is full and unsymmetric:
\end{par} \vspace{1em}
\begin{verbatim}
n = 1000 ;
A = rand (n) ;
b = rand (n,1) ;
c = rand (n,1) ;
tic ; [L,U,p] = lu (A, 'vector') ; LUtime = toc

tic ; x = U \ (L \ b (p,:)) ;
      y = U \ (L \ c (p,:)) ; toc

tic ; opL = struct ('LT', true) ;
      opU = struct ('UT', true) ;
      x = linsolve (U, linsolve (L, b(p,:), opL), opU) ;
      y = linsolve (U, linsolve (L, c(p,:), opL), opU) ; toc
\end{verbatim}

        \color{lightgray} \begin{verbatim}LUtime =
    0.0155
Elapsed time is 0.004917 seconds.
Elapsed time is 0.002092 seconds.
\end{verbatim} \color{black}

\subsection*{INV is easy to use, but slow and inaccurate}

\begin{par}
Oh bother!  Using LU and LINSOLVE is too complicated.  You just want to solve
your system.  Let's just compute inv(A) and use it twice.  Easy to write, but
slower and less accurate ...
\end{par} \vspace{1em}
\begin{verbatim}
S = inv (A) ;
x = S*b ; norm (b-A*x)
y = S*c ; norm (c-A*y)
\end{verbatim}

        \color{lightgray} \begin{verbatim}ans =
   2.3292e-11
ans =
   1.7292e-11
\end{verbatim} \color{black}
    \begin{par}
Sometimes using the inverse seems inevitable.  For example, your textbook might
show the Schur complement formula as S = A-B*inv(D)*C.  This can be done
without inv(D) in one of two ways: SLASH or BACKSLASH (MRDIVIDE or MLDIVIDE to
be precise).
\end{par} \vspace{1em}
\begin{par}
inv(A)*B and A\ensuremath{\backslash}B are mathematically equivalent, as are
B*inv(A) and B/A, so these three methods give the same results (ignoring
computational errors, which are worse for inv(D)).  Only the first equation
looks like the equation in your textbook, however.
\end{par} \vspace{1em}
\begin{verbatim}
A = rand (200) ; B = rand (200) ; C = rand (200) ; D = rand (200) ;

tic ; S1 = A - B*inv(D)*C ; toc ;
tic ; S2 = A - B*(D\C) ;    toc ;
tic ; S3 = A - (B/D)*C ;    toc ;
\end{verbatim}

        \color{lightgray} \begin{verbatim}Elapsed time is 0.002398 seconds.
Elapsed time is 0.001632 seconds.
Elapsed time is 0.001474 seconds.
\end{verbatim} \color{black}

\subsection*{So the winner is ... nobody}

\begin{par}
BACKSLASH: mostly simple to use (except remember that Schur complement
formula?).  Fast and accurate ... but slow if you want to solve       two
linear systems with the same matrix A.
\end{par} \vspace{1em}
\begin{par}
LU, QR, CHOL: fast and accurate.  Awful syntax to use.  Drag out your
linear algebra textbook if you want to use these in MATLAB.       Whenever I
use them I have to derive them from scratch, even       though I \textbf{wrote}
most of the sparse factorizations used in MATLAB!
\end{par} \vspace{1em}
\begin{par}
INV: slow and inaccurate.  Wins big on ease-of-use, though, since it's a
direct plug-in for all your nice mathematical formulas.
\end{par} \vspace{1em}
\begin{par}
No method is best on all three criterion: speed, accuracy, and ease of use.
\end{par} \vspace{1em}
\begin{par}
Is there a solution?  Yes ... keeping reading ...
\end{par} \vspace{1em}

\subsection*{The FACTORIZE object to the rescue}

\begin{par}
The FACTORIZE method is just as easy to use as INV, but just as fast and
accurate as BACKSLASH, LU, QR, CHOL, and LINSOLVE.
\end{par} \vspace{1em}
\begin{par}
F = factorize(A) computes the factorization of A and returns it as an object
that you can reuse to solve a linear system with x=F\ensuremath{\backslash}b.
It picks LU, QR, or Cholesky for you, just like BACKSLASH.
\end{par} \vspace{1em}
\begin{par}
S = inverse(A) is simpler yet.  It does NOT compute inv(A), but factorizes A.
When multiplying S*b, it doesn't mulitply by the inverse, but uses the correct
forward/backsolve equations to solve the linear system.
\end{par} \vspace{1em}
\begin{verbatim}
n = 1000 ;
A = rand (n) ;
b = rand (n,1) ;
c = rand (n,1) ;

tic ;                       x = A\b ; y = A\c ; toc
tic ; S = inv(A) ;          x = S*b ; y = S*c ; toc
tic ; F = factorize(A) ;    x = F\b ; y = F\c ; toc
tic ; S = inverse(A) ;      x = S*b ; y = S*c ; toc
\end{verbatim}

        \color{lightgray} \begin{verbatim}Elapsed time is 0.051483 seconds.
Elapsed time is 0.053813 seconds.
Elapsed time is 0.025811 seconds.
Elapsed time is 0.029840 seconds.
\end{verbatim} \color{black}

\subsection*{Least-squares problems}

\begin{par}
Here are some different methods for solving a least-squares problem when your
system is over-determined.  The last two methods are the same.
\end{par} \vspace{1em}
\begin{verbatim}
A = rand (1000,200) ;
b = rand (1000,1) ;

tic ; x = A\b            ; toc, norm (A'*A*x-A'*b)
tic ; x = pinv(A)*b      ; toc, norm (A'*A*x-A'*b)
tic ; x = inverse(A)*b   ; toc, norm (A'*A*x-A'*b)
tic ; x = factorize(A)\b ; toc, norm (A'*A*x-A'*b)
\end{verbatim}

        \color{lightgray} \begin{verbatim}Elapsed time is 0.010705 seconds.
ans =
   2.7575e-12
Elapsed time is 0.025339 seconds.
ans =
   2.3477e-12
Elapsed time is 0.008903 seconds.
ans =
   2.5140e-12
Elapsed time is 0.008408 seconds.
ans =
   2.5140e-12
\end{verbatim} \color{black}
    \begin{par}
FACTORIZE is better than BACKSLASH because you can reuse the factorization for
different right-hand-sides.  For full-rank matrices, it's better than PINV
because it's faster (and PINV fails for sparse matrices).
\end{par} \vspace{1em}
\begin{verbatim}
A = rand (1000,200) ;
b = rand (1000,1) ;
c = rand (1000,1) ;

tic ;                  ; x = A\b ; y = A\c ; toc
tic ; S = pinv(A)      ; x = S*b ; y = S*c ; toc
tic ; S = inverse(A)   ; x = S*b ; y = S*c ; toc
tic ; F = factorize(A) ; x = F\b ; y = F\c ; toc
\end{verbatim}

        \color{lightgray} \begin{verbatim}Elapsed time is 0.021934 seconds.
Elapsed time is 0.027198 seconds.
Elapsed time is 0.009692 seconds.
Elapsed time is 0.010197 seconds.
\end{verbatim} \color{black}

\subsection*{Underdetermined systems}

\begin{par}
The under-determined system A*x=b where A has more columns than rows has many
solutions.  x=A\ensuremath{\backslash}b finds a basic solution (some of the
entries in x are zero).  pinv(A)*b finds a minimum 2-norm solution, but it's
slow.  QR factorization will do the same if A has full rank.  That's what the
factorize(A) and inverse(A) methods do.
\end{par} \vspace{1em}
\begin{verbatim}
A = rand (200,1000) ;
b = rand (200,1) ;

tic ; x = A\b            ; toc, norm (x)
tic ; x = pinv(A)*b      ; toc, norm (x)
tic ; x = inverse(A)*b   ; toc, norm (x)
tic ; x = factorize(A)\b ; toc, norm (x)
\end{verbatim}

        \color{lightgray} \begin{verbatim}Elapsed time is 0.016247 seconds.
ans =
    2.5580
Elapsed time is 0.034924 seconds.
ans =
    0.5154
Elapsed time is 0.012460 seconds.
ans =
    0.5154
Elapsed time is 0.009924 seconds.
ans =
    0.5154
\end{verbatim} \color{black}

\subsection*{Computing selected entries in the inverse or pseudo-inverse}

\begin{par}
If you want just a few entries from the inverse, it's still better to formulate
the problem as a system of linear equations and use a matrix factorization
instead of computing inv(A).  The FACTORIZE object does this for you, by
overloading the subsref operator.
\end{par} \vspace{1em}
\begin{verbatim}
A = rand (1000) ;

tic ; S = inv (A)     ; S (2:3,4), toc
tic ; S = inverse (A) ; S (2:3,4), toc
\end{verbatim}

        \color{lightgray} \begin{verbatim}ans =
   -0.2554
    0.0993
Elapsed time is 0.047095 seconds.
ans =
   -0.2554
    0.0993
Elapsed time is 0.030233 seconds.
\end{verbatim} \color{black}

\subsection*{Computing the entire inverse or pseudo-inverse}

\begin{par}
Rarely, and I mean RARELY, you really do need the inverse.  More frequently
what you want is the pseudo-inverse.  You can force a factorization to become a
plain matrix by converting it to float.  Note that inverse(A) only handles
full-rank matrices (either dense or sparse), whereas pinv(A) works for all
dense matrices (not sparse).
\end{par} \vspace{1em}
\begin{par}
The explicit need for inv(A) (or S=A\ensuremath{\backslash}eye(n), which is the
same thing) is RARE.  If you ever find yourself multiplying by the inverse,
then you know one thing for sure.  You know with certainty that you don't know
what you're doing.
\end{par} \vspace{1em}
\begin{verbatim}
A = rand (500) ;
tic ; S1 = inv (A) ;            ; toc
tic ; S2 = float (inverse (A)) ; toc
norm (S1-S2)

A = rand (500,400) ;
tic ; S1 = pinv (A)             ; toc
tic ; S2 = float (inverse (A)) ; toc
norm (S1-S2)
\end{verbatim}

        \color{lightgray} \begin{verbatim}Elapsed time is 0.009174 seconds.
Elapsed time is 0.013046 seconds.
ans =
   1.6446e-12
Elapsed time is 0.073158 seconds.
Elapsed time is 0.016360 seconds.
ans =
   1.7565e-14
\end{verbatim} \color{black}

\subsection*{Update/downdate of a dense Cholesky factorization}

\begin{par}
Wilkinson considered the update/downdate of a matrix factorization to be a key
problem in computational linear algebra.  The idea is that you first factorize
a matrix.  Next, make a low-rank change to A, and patch up (or down...) the
factorization so that it becomes the factorization of the new matrix.  In
MATLAB, this only works for dense symmetric positive definite matrices, via
cholupdate.  This is much faster than computing the new factorization from
scratch.
\end{par} \vspace{1em}
\begin{verbatim}
n = 1000 ;
A = rand (n) ;
A = A*A' + n*eye (n) ;
w = rand (n,1) ; t = rand (n,1) ; b = rand (n,1) ;
F = factorize (A) ;

tic ; F = cholupdate (F,w,'+') ; x = F\b ; toc
tic ; y = (A+w*w')\b ;      toc
norm (x-y)

tic ; F = cholupdate (F,t,'-') ; x = F\b ; toc
tic ; y = (A+w*w'-t*t')\b ; toc
norm (x-y)
\end{verbatim}

        \color{lightgray} \begin{verbatim}Elapsed time is 0.010284 seconds.
Elapsed time is 0.015608 seconds.
ans =
   3.6395e-17
Elapsed time is 0.011662 seconds.
Elapsed time is 0.019173 seconds.
ans =
   3.5119e-17
\end{verbatim} \color{black}

\subsection*{Caveat Executor}

\begin{par}
One caveat:  If you have a large number of very small systems to solve, the
object-oriented overhead of creating and using an object can dominate the run
time, at least in MATLAB R2011a.  For this case, if you want the best
performance, stick with BACKSLASH, or LU and LINSOLVE (just extract the
appropriate formulas from the M-files in the FACTORIZE package).
\end{par} \vspace{1em}
\begin{par}
Hopefully the object-oriented overhead will drop in future versions of MATLAB,
and you can ignore this caveat.
\end{par} \vspace{1em}
\begin{verbatim}
A = rand (10) ; b = rand (10,1) ; F = factorize (A) ;

tic ; for k = 1:10000, x = F\b ; end ; toc

tic ; for k = 1:10000, x = A\b ; end ; toc

[L,U,p] = lu (A, 'vector') ;
opL = struct ('LT', true) ;
opU = struct ('UT', true) ;
tic ;
for k = 1:10000
    x = linsolve (U, linsolve (L, b(p,:), opL), opU) ;
end
toc
\end{verbatim}

        \color{lightgray} \begin{verbatim}Elapsed time is 1.156836 seconds.
Elapsed time is 0.123091 seconds.
Elapsed time is 0.057961 seconds.
\end{verbatim} \color{black}

\subsection*{Summary}

\begin{par}
So ... don't use INV, and don't worry about how to use LU, CHOL, or QR
factorization.  Just install the FACTORIZE package, and you're on your way.
Assuming you are now in the Factorize/ directory, cut-and-paste these commands
into your command window:
\end{par} \vspace{1em}
\begin{verbatim}addpath (pwd)
savepath\end{verbatim}
\begin{par}
And remember ...
\end{par} \vspace{1em}
\begin{par}
{\em Don't let that INV go past your eyes; to solve that system, FACTORIZE!}
\end{par} \vspace{1em}

\end{document}


\documentclass[11pt,twoside]{article}

\usepackage[headings]{fullpage}
\usepackage{hyperref}
\pagestyle{myheadings}
\markboth{Fire}{Fire}

\usepackage[utopia]{mathdesign}

\input{../../fncextra}

\begin{document}

\begin{center}
  \bf We didn't start the fire
\end{center}

The initial-value problem
\begin{equation}
  \label{eq:fire}
  \frac{dr}{dt} = r^2(1-r), \qquad t > 0, \quad r(0)=r_0,
\end{equation}
is a simple model for the radius of a \href{https://youtu.be/Q58-la_yAB4}{spherical flame ball in zero gravity}. If $r$ is initially small, it grows slowly for a while before rapidly increasing to a value close to 1, which it approaches asymptotically.

Although the solution is quite simple, it proves to be surprisingly challenging for some IVP solvers, including Euler's method.

\subsection*{Preparation}

Read sections 6.1 and 6.2.

\subsection*{Goals}

You will solve the flame ball equation numerically using Euler's method, observing its convergence.

\subsection*{Procedure}

Download the template code and edit it to perform the following steps.

\begin{enumerate}
	\item Follow the script to create a high-accuracy reference solution of the problem with $r_0=0.01$. Plot the reference solution for $0\le t\le 500$.
  \item For $n = 100,200,400,800,1600,6400,25600$, use the
    \texttt{eulerivp} function to solve the IVP with $r_0=0.01$ up to
    time $t=100$. Find the error in the Euler solution by taking the
    difference between its value of $r$ at
   and the reference solution at $t=100$. Make a log-log plot of the errors and verify that the method is first-order convergent.
  \item Repeat part 2 but at time $t=200$. This time, the convergence behavior is a lot less smooth than first-order accuracy might seem to imply.
  \item Plot the solution $r(t)$ over $0\le t \le 500$ using \texttt{eulerivp} with $n=150$, 200, 250, and 300. Even though the errors are getting smaller, these could hardly be called ``good'' solutions qualitatively!
\end{enumerate}

\end{document}