%!TEX root = main.tex

\section{Maximizing earnings under uncertainty}
\label{sec:sensitivity}
The primary source of variability in the input of the {\originalproblem} problem
is the set of empirical transition matrices {\empiricaltransitionmatrix}.  In a typical application,
we expect that predictive models would be employed to generate estimates of these matrices based upon observations
from historical data (as we do in our own experiments).  Empirically observed transition matrices may
suffer from estimation errors due to the presence of external confounding factors (e.g.,
weather, special
events inside the city) while gathering the data. As a result, the dynamic programming solution
to {\originalproblem}  may also be sensitive to the transition probabilities. In this section,
we address the question of how the results of the solutions we described in the previous section change
under the assumption that there is some uncertainty (and thus noise) in the underlying empirical transition
matrices we use as part of our input.

Concretely, we now assume that the empirical transition matrix~(\empiricaltransitionmatrix)  is generated from an
underlying traffic matrix, or count matrix, recording trips between locations $i$ and $j$.

\spara{Count matrix (\countmatrix)}:
Every edge $e(i\rightarrow j) \in E$ is associated with an
integer-valued weight $c(i,j)$ that denotes the number of requests
at zone $i$ that had node $j$ as their destination.
Then, we compute frequencies $f(i,j) = \frac{c(i,j)}{\sum_k c(i,k)}$, for all outbound trips from $i$.

With this, we now describe how to quantify uncertainty in the rows of {\empiricaltransitionmatrix}
(and the underlying {\countmatrix}, by construction). This will enable us to
modify the {\originalproblem} into the {\robustproblem} problem following ideas developed
by \citet{nilim2004robustness}.

%\subsection{Likelihood model}
%\label{sec:likelihood_model}

\spara{Modeling uncertainty:}
We now assume that there is an underlying \emph{true} transition matrix
{\truetransitionmatrix}, and the question we explore is
our confidence that the
{\countmatrix} we observe is actually
generated by the true transition matrix {\truetransitionmatrix}.  As before, both
{\truetransitionmatrix} and {\countmatrix} are clearly time-dependent in practice,
but for ease of exposition, we ignore the time-dependency aspect of the problem here.

We consider each row of the true transition matrix and the count matrix
separately; let {\rowp} and {\rowc} denote
any particular row of {\truetransitionmatrix} and {\countmatrix} respectively.
Following the ideas of Kullback {\etal}~\cite{kullback62tests}, we have a discriminatory random variable $2\hat{I}$, which follows a $\chi^2$ distribution with $(n-1)$ degrees of freedom. Heuristically, $2\hat{I}$ can be considered as a measure of the ``divergence'' of {\rowc} from {\rowp}.
Thus,
for $\rowc$ to be in the  \emph{$(1-\alpha)$ (or $100(1-\alpha)\%$)
confidence interval of $\rowp$}, we need:
\begin{align*}\label{eq:ul}
\text{F}_{\chi^2_{n-1}}\left[2\hat{I}\right] &=  \text{F}_{\chi^2_{n-1}}\left[2\sum_{i=1}^n\rowc(i)\log\rowc(i)-2n\log n -2\sum_{i=1}^n\rowc(i)\log\rowp(i)\right] \nonumber\\
&= 1 - \alpha,
\end{align*}
where
 $\rowp(i)$ (resp. $\rowc(i)$) is the $i$-th element of vector $\rowp$ (resp.\ $\rowc$).
 In the above equation, $\alpha$ quantifies the uncertainty that
 one can tolerate and is an upper bound on what one believes actually exists in the set of observations $\rowp$.
Thus, we call $\alpha$ the \emph{input uncertainty level}.

By setting $\beta_\text{max} = \sum_{i=1}^n\rowc(i)\log \rowc(i)$,
we get
\begin{equation}\label{eq:constraint}
\sum_{i=1}^n\rowc(i)\log\rowp(i) = \frac{2(\beta_\text{max}-n\log n)-\text{F}^{-1}_{\chi^2_{n-1}}(1 - \alpha)}{2},
\end{equation}
where $\text{F}^{-1}_{\chi^2_{n-1}}$ is the inverse of the $\chi^2$ cdf.
In other words, for all vectors $\rowp$ for which Equation~\eqref{eq:constraint}
is satisfied, $\rowc$ is within the $(1-\alpha)$-confidence interval of $\rowp$.

Thus given $\countmatrix$ and $\alpha$, we define
the \emph{$\alpha$-feasible
matrices} $\mathcal{P}_{\alpha}$ to be
the
set of true transition matrices  such that
for every matrix {\truetransitionmatrix} in $\mathcal{P}_{\alpha}$
and every row $\rowp$ of {\truetransitionmatrix}, Equation~\eqref{eq:constraint}
is satisfied.

% \iffalse
% \spara{The likelihood model:}
% Here, we will assume there is an underlying \emph{true} transition matrix
% {\truetransitionmatrix}, and the count matrix {\countmatrix}, which is used to
% obtain the empirical transition matrix {\empiricaltransitionmatrix} is the solution
% to the maximum likelihood problem

% \begin{equation}
% \max_\truetransitionmatrix L(\truetransitionmatrix) := \sum_{i,j}c(i,j) \log{p(i,j)} : \truetransitionmatrix \geq 0, \truetransitionmatrix\vec{1} = \vec{1}
% \end{equation}
% The optimal log-likelihood is $\betamax= \sum_{i,j}c(i,j)\log{f(i,j)}$.
% A classical description of uncertainty in a maximum-likelihood setting is via the likelihood region (\citet{lehmann2006theory}, \citet{poor2013introduction})

% \begin{equation*}
% \likelihoodregion(\beta) := \bigg\{\truetransitionmatrix \in \mathbb{R}^{n \times n}: \truetransitionmatrix \geq 0, \truetransitionmatrix \vec{1}=\vec{1}, \sum_{i,j}f(i,j)\log{p(i,j)} \geq \beta\bigg\} \label{eq:likelihood_region}
% \end{equation*}
% where $\beta < \betamax$ is a user provided number, which represents the uncertainty level. In practice, one can choose uncertainty level and $\beta$ using the guidelines detailed by \citet{nilim2004robustness}.

% We only need to work with uncertainty on each row {\truetransitionmatrix}, that is projection of the set $\likelihoodregion(\beta)$. Due to the separable nature of the log-likelihood function, the projection of the above set onto the $\truetransitionmatrix_i$ variables of the matrix {\truetransitionmatrix} can be given as,

% \begin{equation*}
% \likelihoodregion_i(\beta_i) := \bigg\{p \in \Delta^n : \sum_{j}f(i,j)\log{p(i,j)} \geq \beta_i \bigg\},
% \end{equation*}
% where,
% \begin{equation*}
% \beta_i = \beta - \sum_{k \neq i}\sum_{j}c(k,j)\log{f(k,j)},
% \end{equation*}
% and  $\Delta^n = \{\vec{p} \in \mathbb{R}^n_+ : \transpose{p} \vec{1} = 1 \}$

% \fi

%\subsection{\textsc{The robust control problem}}
\spara{The {\robustproblem} problem:}
%%Clearly, there are a large number of matrices in the set $\mathcal{P}_\alpha$.
Our approach is to compute the \emph{worst-case}{ {\totalexpectedearnings} for a driver, by
  finding the {\truetransitionmatrix} among all matrices in $\mathcal{P}_\alpha$
 such that the {\totalexpectedearnings} of the driver
 are minimized.
This quantifies the worst-case difference between the earnings computed
as a solution to the {\originalproblem} and the worst-case earnings of the
driver, given bounded uncertainty $\alpha$.
%%
%%
We formalized this as the following problem definition:

\begin{problem}[{\robustproblem}]\label{problem:robust}
Given sets of time evolving {\countmatrix}, {\traveltimematrix} and {\rewardsmatrix}, the driver's budget $B$ and input uncertainty level $\alpha$,
find $\widehat{\policy}$
such that:
\[  \widehat{\policy} = \argmax_{\policy\in \policyspace}
\min_{\truetransitionmatrix\in\mathcal{P}_\alpha}\mathcal{E}(\policy,\truetransitionmatrix,\traveltimematrix,\rewardsmatrix,B).
\]
\end{problem}
Note that the above problem requires searching among all possible true transition
matrices in $\mathcal{P}_\alpha$, which is a non-enumerable set.
In fact, we can show (details omitted due to space constraints) that
Problem~\ref{problem:robust} can be solved by enhancing the \emph{total expected future earnings} associated with {\getpassenger} action in the  dynamic-programming routines we described in Section~\ref{sec:driver_strategies}
with an optimization problem. We use an off-the-shelf minimizer to solve this optimization problem.
Alternatively, a bisection algorithm can approximate this problem within an accuracy $\delta$ in $O(\log(V_{\max}/\delta))$ time, where $V_{\max}$ is the maximum value of the value function~\cite{nilim2004robustness}.

% \iffalse
% which assumes that every ${\empiricaltransitionmatrix}^t$ has an uncertainty level of $\beta$, and a likelihood region $\likelihoodregion^t$ associated with them. While the {\originalproblem} problem maximizes the {\totalexpectedearnings}, the {\nominalproblem} maximises the `worst-case' {\totalexpectedearnings}. In order to do that, we devise a situation where the driver seeks to maximize the {\totalexpectedearnings}
% while the ``nature" selects the worst-case time varying transition matrices from the likelihood region {\likelihoodregion} in order to minimize the {\totalexpectedearnings}.

% We define \textit{policy of nature} as a specific ordered selection of time varying true transition matrices
% %\begin{equation}
% $\mathbb{P} = \big( \truetransitionmatrix^t \big)_{t \leq N}$.
% %\end{equation}
% Similarly, the corresponding ordered set of likelihood regions is,
% %\begin{equation}
% $\mathcal{T} = \big(\mathcal{P}^t \big)_{t \leq N}$.
% %\end{equation}
% Using these notations, we define the {\robustproblem} problem as follows,
% \begin{equation}
% \label{eq:robust_control_problem}
% \phi(\policyspace, \mathcal{T}, \traveltimematrix, \rewardsmatrix) = \max_{\policy \in \policyspace} \min_{\mathbb{P} \in \mathcal{T}} \mathcal{E}^{N,B}(\policy, \mathbb{P}, \traveltimematrix, \rewardsmatrix) \\
% \end{equation}
% It should be noted that the ordered set $\mathbb{P}$ is not enumerable, thereby making it impossible to solve Eq.(\ref{eq:robust_control_problem}) by conventional dynamic programming.
% \fi

% \iffalse
% \subsection{\textsc{Robust Markov Decision Process}}

% As described in previous section, the uncertainty in empirically observed transition matrix affects the cumulative earning only in the case of {\getpassenger} action, we express Eq.(\ref{eq:cumulative_earning_get_passenger}) in form of an optimization problem, which we refer to as the `inner problem'.
% \begin{equation}
% \cumulativeearning{i}{t,b} = \sigma_{\likelihoodregion_{i}^{t}} \bigg(\rewardsmatrix_{i}^{t} + \inducedearningvector{i}{t,b}{\getpassengeraction}\bigg)
% \end{equation}
% where, the $\sigma_{\likelihoodregion}(\vec{v})$ is a minimization problem of the form,
% \begin{equation}
% \sigma_{\likelihoodregion}(\vec{v}) = \inf\big\{ \transpose{p}\vec{v}: \vec{p} \in \likelihoodregion \big\}
% \end{equation}
% We now update the recursive equations for calculating {\totalexpectedearnings} for each of the strategies using the above transformation. For brevity, we only show the updated equation for {\relocationflexible} below.
% \begin{eqnarray}
% \label{eq:robust_relocationflexible_strategy}
% \cumulativeearning{i}{t,b} &=& \max_{a \in \actionsset}
%     \begin{cases}
%     \sigma_{\likelihoodregion_{i}^{t}} \bigg(\rewardsmatrix_{i}^{t} + \inducedearningvector{i}{t,b}{a}\bigg), &\textrm{  if } a = \getpassengeraction\\ \\
%     r^t(i,\homezone) + \cumulativeearning{\homezone}{t',b}, &\textrm{  if } a = \gohomeaction \\ \\
%     \max_{j} \bigg\{r^t(i,j) + \cumulativeearning{j}{t',b'}\bigg\}, &\textrm{  if } a = \relocateaction
%     \end{cases}
% \end{eqnarray}

% \subsection{\textsc{The bisection algorithm}}

% The inner problem defined in the previous section is solved in every step of the dynamic program to find an optimal policy for any of the strategies. In this section, we describe an approximation algorithm to solve the inner problem in Eq.(\ref{eq:inner_problem}) within a bounded error of $\delta > 0$.
% \begin{equation}
% \sigma_{\mathcal{P}}(\vec{v}) = \min_{\vec{p} \in \mathcal{P}} \transpose{p}\vec{v}
% \label{eq:inner_problem}
% \end{equation}
% Assuming that $\vec{v} \in \mathbb{R}^n_+$, the bisection algorithm gives an output of the form,
% \begin{eqnarray}
% \hat{\sigma}_{\mathcal{P}} (\vec{v}) &=& \sigma_{\mathcal{P}} (\vec{v}) - \delta_{\mathcal{P}}(\vec{v})
% \end{eqnarray}
% where $0 \leq \delta_{\mathcal{P}}(\vec{v}) \leq \delta$.

% The inner problem can be expressed as an optimization problem as follows.
% \begin{equation}
% \sigma^* = \min_{\vec{p}} \transpose{\vec{p}}\vec{v}: \vec{p} \in \Delta^n, \sum_{j} \vec{f}(j)\log{\vec{p}(j)} \geq \beta
% \end{equation}
% The Lagrangian $\mathbf{L}: \mathbb{R}^n \times \mathbb{R}^n \times \mathbb{R} \times \mathbb{R} \rightarrow \mathbb{R}$ associated with it is,
% \begin{equation}
% \mathbf{L}(\vec{v}, \zeta, \mu, \lambda) = \transpose{\vec{p}}\vec{v} - \transpose{\zeta}\vec{p} + \mu(1 - \transpose{\vec{p}}\mathbf{1}) + \lambda(\beta - \transpose{f}\log{\vec{p}})
% \end{equation}
% where $\zeta, \mu$ and $\lambda$ are Lagrange multipliers. We also define $\vmin = \min_j{\vec{v}(j)}$ and $\vmax = \max_j{\vec{v}(j)}$. We can define the dual problem as,
% \begin{equation}
% \sigma^* = \max_{\lambda, \mu} h(\lambda, \mu)
% \end{equation}
% where,
% \begin{eqnarray}
% h(\lambda, \mu) =
%     \begin{cases}
%     \lambda(1 + \beta) + \mu - \lambda \sum_j \vec{f}(j) \log\bigg({\frac{\lambda \vec{f}(j)}{\vec{v}(j) - \mu}}\bigg), &\textrm{  if } \lambda > 0, \mu < \vmin, \\
%     -\infty, &\textrm{  otherwise } \\
%     \end{cases} \nonumber
% \end{eqnarray}
% which further reduces to a 1-dimensional optimization problem,
% \begin{equation}
% \sigma^* = \max_{\mu < \vmin} \sigma(\mu)
% \end{equation}
% where,
% \begin{eqnarray*}
% \sigma(\mu) &=& h(\lambda(\mu), \mu), \\
% \lambda(\mu) &=& \bigg(\sum_{j} \frac{\vec{f}(j)}{\vec{v}(j) - \mu}\bigg)^{-1}
% \end{eqnarray*}
% \begin{lemma}
% The global maximiser of $\sigma(\mu)$ is contained within the interval $[\mu_{-}, \mu_{+})$ where, $\mu_{-} = \frac{\vmin - e^{(\beta - \betamax)}\vmax}{1 - e^{(\beta - \betamax)}}$ and $\mu_{+} = \vmin$.
% \todo[Harshal]{Include the proof in supplementary material and provide a link here.}
% \end{lemma}
% \begin{lemma}
% After $\mathcal{N} \approx \log_2(V/ \delta)$ steps, the bisection algorithm provides an optimal solution to the inner problem within a bounded error of $\delta > 0$, when
% $V = \max(\sigma^* - \sigma(\mu_+), \sigma^* - \sigma(\mu_-))$.
% \end{lemma}
% \todo[Harshal]{Include the proof in supplementary material and provide a link here.}
% \begin{algorithm}
% \LinesNumbered
% \KwIn{$\mu_{+}, \mu_{-}, \mathcal{N}$}
% \KwOut{Solution to problem (\ref{eq:inner_problem})}

% \For{$k = 1 \cdots \mathcal{N}$}{
%     $\mu_k = (\mu_+ + \mu_-)/2$ ;\\
%     \uIf{$\sigma^{'}(\mu_k) > 0$}{
%         $\mu_- = \mu_k$ \;
%     }
%     \uElseIf{$\sigma^{'}(\mu_k) < 0$}{
%         $\mu_+ = \mu_k$ \;
%     }
%     \Else{
%         \Return $\sigma(\mu_k)$
%     }
% $k = k + 1$ ;\\
% }
% \Return $\argmax_i \{\sigma(\mu_i)\}$;
% \caption{Bisection algorithm}
% \label{alg:bisection_algorithm}
% \end{algorithm}

% \todo[Harshal]{Add note on the complexity of solving the inner problem.}

% \subsection{\textsc{\texorpdfstring{$\epsilon$}{epsilon}-suboptimal algorithm}}

% An {\epsilonsuboptimal}, {\epsilonsuboptimalpolicy} is a policy such that the worst-case {\totalexpectedearnings} under it, i.e.,
% \begin{equation}
% \phi(\hat{\policy}, \mathcal{T}, \traveltimematrix, \rewardsmatrix) = \min_{\mathbb{P} \in \mathcal{T}} \mathcal{E}^{N,B}(\hat{\policy}, \mathbb{P}, \traveltimematrix, \rewardsmatrix)
% \end{equation}
% satisfy the condition,
% \begin{equation*}
% \phi(\hat{\policy}, \mathcal{T}, \traveltimematrix, \rewardsmatrix) \leq \phi(\policyspace, \empiricaltransitionmatrix, \traveltimematrix, \rewardsmatrix) \leq \phi(\hat{\policy}, \mathcal{T}, \traveltimematrix, \rewardsmatrix) + \epsilon
% \end{equation*}
% Here, $\epsilon > 0$ is user input. Using the uncertainty model from section (\ref{sec:likelihood_model}), we solve the bisection algorithm with an accuracy $\delta = \epsilon / N$.
% \note[Harshal]{Only in the case where every optimal action in a policy is {\getpassenger}, the entire $\epsilon$ error buffer is used. In practice, the error buffer used will be $(n \times \delta)$ where $n$ is the number of times the optimal action choice happens to be {\getpassenger}}
% This gives us the robust finite horizon dynamic programming algorithm. The algorithm for the most {\relocationflexible} is shown below. However, it can be adapted for any of the other strategies described earlier.
% \begin{algorithm}
% \LinesNumbered
% \KwIn{$\empiricaltransitionmatrix, \traveltimematrix, \rewardsmatrix, \epsilon, \beta$}
% \KwOut{Solution to problem (\ref{eq:robust_relocationflexible_strategy})}

% Initialize $t = N$ and $b = B$; \\
% \While{$t > 0$}{
%     \While{$b > 0$}{
%         \For{$i \in \cityzones$}{
%         Use Algorithm (\ref{alg:bisection_algorithm}), to calculate a value $\hat{\sigma}_{i}$,
%         $\hat{\sigma}_{\likelihoodregion_i} \leq \sigma_{\likelihoodregion_i}(\rewardsmatrix_{i}^{t} + \inducedearningvector{i}{t,b}{\getpassengeraction}) \leq \hat{\sigma}_{\likelihoodregion_i} + \delta$ \;
%         Solve Eq.(\ref{eq:robust_relocationflexible_strategy})
%         \begin{eqnarray*}
%         \cumulativeearning{i}{t,b} = \max_{a \in \actionsset}
%             \begin{cases}
%             \hat{\sigma}_{i}, &\textrm{  if } a = \getpassengeraction\\
%             r^t(i,\homezone) + \cumulativeearning{\homezone}{t',b}, &\textrm{  if } a = \gohomeaction \\
%             \max_{j} \bigg\{r^t(i,j) + \cumulativeearning{j}{t',b'}\bigg\}, &\textrm{  if } a = \relocateaction
%         \end{cases}
%         \end{eqnarray*}
%         }
%     $b = b - 1$ \;
%     }
% $t = t - 1$ \;
% }
% \Return $\cumulativeearning{\homezone}{N,B}$ and $\epsilonsuboptimalpolicy$;
% \caption{Robust Finite Horizon Dynamic Program}
% \label{alg:robust_dynamic_program}
% \end{algorithm}
% \fi


\chapter{Application of cohomology}
In this final chapter on topology, I'll state (mostly without proof)
some nice properties of cohomology groups, and in particular
introduce the so-called cup product.
For an actual treatise on the cup product,
see \cite{ref:hatcher} or \cite{ref:maxim752}.

\section{Poincar\'e duality}
First cool result:
you may have noticed symmetry in the (co)homology groups of
``nice'' spaces like the torus or $S^n$.
In fact this is predicted by:
\begin{theorem}
	[Poincar\'e duality]
	If $M$ is a smooth oriented compact $n$-manifold,
	then we have a natural isomorphism
	\[ H^k(M; \ZZ) \cong H_{n-k}(M) \]
	for every $k$.
	In particular, $H^k(M) = 0$ for $k > n$.
\end{theorem}
So for smooth oriented compact manifolds,
cohomology and homology groups are not so different.

From this follows the symmetry that we mentioned
when we first defined the Betti numbers:
\begin{corollary}
	[Symmetry of Betti numbers]
	Let $M$ be a smooth oriented compact $n$-manifold,
	and let $b_k$ denote its Betti number.
	Then \[ b_k = b_{n-k}. \]
\end{corollary}
\begin{proof}
	\Cref{prob:betti}.
\end{proof}

\section{de Rham cohomology}
We now reveal the connection between
differential forms and singular cohomology.

Let $M$ be a smooth manifold.
We are interested in the homology and cohomology groups of $M$.
We specialize to the case $G = \RR$, the additive group of real numbers.
\begin{ques}
	Check that $\Ext(H, \RR) = 0$ for any finitely generated abelian group $H$.
\end{ques}
Thus, with real coefficients the universal coefficient theorem says that
\[ H^k(M; \RR) \cong \Hom(H_k(M), \RR) = \left( H_k(M) \right)^\vee \]
where we view $H_k(X)$ as a real vector space.
So, we'd like to get a handle on either $H_k(M$) or $H^k(M; \RR)$.

Consider the cochain complex
\[
	0 \to \Omega^0(M)
	\taking d \Omega^1(M)
	\taking d \Omega^2(M)
	\taking d \Omega^3(M)
	\taking d \dots
\]
and let $\HdR^k(M)$ denote its cohomology groups.
Thus the de Rham cohomology is the closed forms modulo the exact forms.
\[
	\text{Cochain} : \text{Cocycle} : \text{Coboundary}
	= \text{$k$-form} : \text{Closed form} : \text{Exact form}.
\]

The whole punch line is:
\begin{theorem}
	[de Rham's theorem]
	For any smooth manifold $M$, we have a natural isomorphism
	\[ H^k(M; \RR) \cong \HdR^k(M). \]
\end{theorem}
So the theorem is that the real cohomology groups of manifolds $M$
are actually just given by the behavior of differential forms.
Thus,
\begin{moral}
	One can metaphorically think of elements of cohomology groups
	as $G$-valued differential forms on the space.
\end{moral}

Why does this happen?
In fact, we observed already behavior of differential
forms which reflects holes in the space.
For example, let $M = S^1$ be a circle
and consider the \textbf{angle form} $\alpha$
(see \Cref{ex:angle_form}).
The from $\alpha$ is closed, but not exact,
because it is possible to run a full circle around $S^1$.
So the failure of $\alpha$ to be exact is signaling
that $H_1(S^1) \cong \ZZ$.

\section{Graded rings}
\prototype{Polynomial rings are commutative graded rings,
while $\Lambda^\bullet(V)$ is anticommutative.}
In the de Rham cohomology, the differential forms can interact in another way:
given a $k$-form $\alpha$ and an $\ell$-form $\beta$, we can consider
a $(k+\ell)$-form
\[ \alpha \wedge \beta. \]
So we can equip the set of forms with a ``product'', satisfying
$\beta \wedge \alpha = (-1)^{k\ell} \alpha \wedge \beta$
This is a special case of a more general structure:

\begin{definition}
	A \vocab{graded pseudo-ring} $R$ is an abelian group
	\[ R = \bigoplus_{d \ge 0} R^d \]
	where $R^0$, $R^1$, \dots, are abelian groups,
	with an additional associative binary operation $\times : R \to R$.
	We require that if $r \in R^d$ and $s \in R^e$, we have $rs \in R^{d+e}$.
	Elements of an $R^d$ are called \vocab{homogeneous elements};
	if $r \in R^d$ and $r \neq 0$, we write $|r| = d$.
\end{definition}
Note that we do \emph{not} assume commutativity.
In fact, these ``rings'' may not even have an identity $1$.
We use other words if there are additional properties:
\begin{definition}
	A \vocab{graded ring} is a graded pseudo-ring with $1$.
	If it is commutative we say it is a \vocab{commutative graded ring}.
\end{definition}
\begin{definition}
	A graded (pseudo-)ring $R$ is \vocab{anticommutative} if
	for any homogeneous $r$ and $s$ we have
	\[ rs = (-1)^{|r| |s|} sr. \]
\end{definition}

To summarize:
\begin{center}
	\small
	\begin{tabular}[h]{|c|cc|}
		\hline
		\textbf{Flavors of graded rings} &
		Need not have $1$ & Must have a $1$ \\ \hline
		No Assumption & graded pseudo-ring & graded ring \\
		Anticommutative & anticommutative pseudo-ring & anticommutative ring \\
		Commutative &  & commutative graded ring \\ \hline
	\end{tabular}
\end{center}

\begin{example}[Examples of graded rings]
	\listhack
	\begin{enumerate}[(a)]
		\ii The ring $R = \ZZ[x]$ is a \textbf{commutative graded ring},
		with the $d$th component being the multiples of $x^d$.
		\ii The ring $R = \ZZ[x,y,z]$ is a \textbf{commutative graded ring},
		with the $d$th component being the abelian group
		of homogeneous degree $d$ polynomials (and $0$).
		\ii Let $V$ be a vector space, and consider
		the abelian group
		\[ \Lambda^\bullet(V) = \bigoplus_{d \ge 0} \Lambda^d(V). \]
		For example, $e_1 + (e_2 \wedge e_3) \in \Lambda^\bullet(V)$, say.
		We endow $\Lambda^\bullet(V)$ with the product $\wedge$,
		which makes it into an \textbf{anticommutative ring}.
		\ii Consider the set of differential forms of a manifold $M$,
		say \[ \Omega^\bullet(M) = \bigoplus_{d \ge 0} \Omega^d(M) \]
		endowed with the product $\wedge$.
		This is an \textbf{anticommutative ring}.
	\end{enumerate}
	All four examples have a multiplicative identity.
\end{example}

Let's return to the situation of $\Omega^\bullet(M)$.
Consider again the de Rham cohomology groups $\HdR^k(M)$,
whose elements are closed forms modulo exact forms.
We claim that:
\begin{lemma}
	[Wedge product respects de Rham cohomology]
	The wedge product induces a map
	\[ \wedge : \HdR^k(M) \times \HdR^\ell(M) \to \HdR^{k+\ell}(M). \]
\end{lemma}
\begin{proof}
	First, we recall that the operator $d$ satisfies
	\[
		d(\alpha \wedge \beta)
		= (d\alpha) \wedge \beta + \alpha \wedge (d\beta).
	\]
	Now suppose $\alpha$ and $\beta$ are closed forms.
	Then from the above, $\alpha \wedge \beta$ is clearly closed.
	Also if $\alpha$ is closed and $\beta = d\omega$ is exact,
	then $\alpha \wedge \beta$ is exact, from the identity
	\[ d(\alpha \wedge \omega)
		= d\alpha \wedge\omega + \alpha \wedge d\omega = \alpha \wedge \beta. \]
	Similarly if $\alpha$ is exact and $\beta$ is closed
	then $\alpha \wedge \beta$ is exact.
	Thus it makes sense to take the product modulo exact forms,
	giving the theorem above.
\end{proof}

Therefore, we can obtain a \emph{anticommutative ring}
\[ \HdR^\bullet(M) = \bigoplus_{k \ge 0} \HdR^k(M) \]
with $\wedge$ as a product, and $1 \in \Lambda^0(\RR) = \RR$ as the identity

\section{Cup products}
Inspired by this, we want to see if we can construct a similar product
on $\bigoplus_{k \ge 0} H^k(X; R)$ for any topological space $X$ and ring $R$
(where $R$ is commutative with $1$ as always).
The way to do this is via the \emph{cup product}.

Then this gives us a way to multiply two cochains, as follows.
\begin{definition}
	Suppose $\phi \in C^k(X;R)$ and $\psi \in C^\ell(X;R)$.
	Then we can define their \vocab{cup product}
	$\phi\smile\psi \in C^{k+\ell}(X;R)$ to be
	\[
		(\phi\smile\psi)([v_0, \dots, v_{k+\ell}])
		=
		\phi\left( [v_0, \dots, v_k] \right)
		\cdot
		\psi\left( [v_k, \dots, v_{k+\ell}] \right)
	\]
	where the multiplication is in $R$.
\end{definition}

\begin{ques}
	Assuming $R$ has a $1$, which $0$-cochain is the identity for $\smile$?
\end{ques}

First, we prove an analogous result as before:
\begin{lemma}[$\delta$ with cup products]
	We have
	$\delta(\phi\smile\psi) = \delta\phi\smile\psi
	+ (-1)^k\phi\smile\delta\psi$.
\end{lemma}
\begin{proof}
	Direct $\sum$ computations.
\end{proof}
Thus, by the same routine we used for de Rham cohomology, we get
an induced map
\[ \smile : H^k(X;R) \times H^\ell(X;R) \to H^{k+\ell}(X;R).  \]
We then define the \vocab{singular cohomology ring}
whose elements are finite sums in
\[ H^\bullet(X;R) = \bigoplus_{k \ge 0} H^k(X;R) \]
and with multiplication given by $\smile$.
Thus it is a graded ring (with $1_R \in R$ the identity)
and is in fact anticommutative:
\begin{proposition}[Cohomology is anticommutative]
	$H^\bullet(X; R)$ is an anticommutative ring,
	meaning $\phi \smile \psi = (-1)^{k\ell} \psi \smile \phi$.
\end{proposition}
For a proof, see \cite[Theorem 3.11, pages 210-212]{ref:hatcher}.
Moreover, we have the de Rham isomorphism
\begin{theorem}
	[de Rham extends to ring isomorphism]
	For any smooth manifold $M$, the isomorphism
	of de Rham cohomology groups to singular cohomology
	groups in facts gives an isomorphism
	\[ H^\bullet(M; \RR) \cong \HdR^\bullet(M) \]
	of anticommutative rings.
\end{theorem}

Therefore, if ``differential forms'' are the way to visualize
the elements of a cohomology group, the wedge product is the
correct way to visualize the cup product.

We now present (mostly without proof)
the cohomology rings of some common spaces.

\begin{example}
	[Cohomology of torus]
	The cohomology ring $H^\bullet(S^1 \times S^1; \ZZ)$
	of the torus is generated by elements $|\alpha| = |\beta| = 1$
	which satisfy the relations
	$\alpha \smile \alpha = \beta \smile \beta = 0$,
	and $\alpha \smile \beta = -\beta \smile \alpha$.
	(It also includes an identity $1$.)
	Thus as a $\ZZ$-module it is
	\[ H^\bullet(S^1 \times S^1; \ZZ)
		\cong \ZZ \oplus \left[ \alpha \ZZ \oplus \beta \ZZ \right]
		\oplus (\alpha \smile \beta) \ZZ. \]
	This gives the expected dimensions $1+2+1=4$.
	It is anti-commutative.
\end{example}

\begin{example}[Cohomology ring of $S^n$]
	Consider $S^n$ for $n \ge 1$.
	The nontrivial cohomology groups are given by
	$H^0(S^n; \ZZ) \cong H^n(S^n; \ZZ) \cong \ZZ$.
	So as an abelian group
	\[ H^\bullet(S^n; \ZZ) \cong \ZZ \oplus \alpha \ZZ \]
	where $\alpha$ is the generator of $H^n(S^n, \ZZ)$.

	Now, observe that $|\alpha\smile\alpha| = 2n$, but
	since $H^{2n}(S^n; \ZZ) = 0$ we must have $\alpha\smile\alpha=0$.
	So even more succinctly,
	\[ H^\bullet(S^n; \ZZ) \cong \ZZ[\alpha]/(\alpha^2). \]
	Confusingly enough, this graded ring is both
	commutative \emph{and} anti-commutative.
	The reason is that $\alpha \smile \alpha = 0 = -(\alpha \smile \alpha)$.
\end{example}

\begin{example}[Cohmology ring of real and complex projective space]
	It turns out that
	\begin{align*}
		H^\bullet(\RP^n; \Zc2) &\cong \Zc2[\alpha]/(\alpha^{n+1}) \\
		H^\bullet(\CP^n; \ZZ) &\cong \ZZ[\beta]/(\beta^{n+1})
	\end{align*}
	where $|\alpha| = 1$ is a generator of $H^1(\RP^n; \Zc2)$
	and $|\beta| = 2$ is a generator of $H^2(\CP^n; \ZZ)$.

	Confusingly enough, both graded rings are commutative \emph{and} anti-commutative.
	In the first case it is because we work in $\Zc 2$, for which $1 = -1$,
	so anticommutative is actually equivalent to commutative.
	In the second case, all nonzero homogeneous elements have degree $2$.
\end{example}

\section{Relative cohomology pseudo-rings}
For $A \subseteq X$, one can also define a relative cup product
\[ H^k(X,A;R) \times H^\ell(X,A;R) \to H^{k+\ell}(X,A;R). \]
After all, if either cochain vanishes on chains in $A$,
then so does their cup product.
This lets us define \vocab{relative cohomology pseudo-ring}
and \vocab{reduced cohomology pseudo-ring} (by $A = \{\ast\}$), say
\begin{align*}
H^\bullet(X,A;R) &= \bigoplus_{k \ge 0} H^k(X,A; R) \\
\wt H^\bullet(X;R) &= \bigoplus_{k \ge 0} \wt H^k(X;R).
\end{align*}
These are both \textbf{anticommutative pseudo-rings}.
Indeed, often we have $\wt H^0(X;R) = 0$ and thus there is no identity at all.

Once again we have functoriality:
\begin{theorem}
	[Cohomology (pseudo-)rings are functorial]
	Fix a ring $R$ (commutative with $1$).
	Then we have functors
	\begin{align*}
		H^\bullet(-; R) &: \catname{hTop}\op \to \catname{GradedRings} \\
		H^\bullet(-,-; R) &: \catname{hPairTop}\op \to \catname{GradedPseudoRings}.
	\end{align*}
\end{theorem}

Unfortunately, unlike with (co)homology groups,
it is a nontrivial task to determine the cup product
for even nice spaces like CW complexes.
So we will not do much in the way of computation.
However, there is a little progress we can make.

\section{Wedge sums}
Our goal is to now compute $\wt H^\bullet(X \vee Y)$.
To do this, we need to define the product of two graded pseudo-rings:
\begin{definition}
	Let $R$ and $S$ be two graded pseudo-rings.
	The \vocab{product pseudo-ring} $R \times S$ is the graded pseudo-ring
	defined by taking the underlying abelian group as
	\[ R \oplus S = \bigoplus_{d \ge 0} (R^d \oplus S^d). \]
	Multiplication comes from $R$ and $S$, followed by
	declaring $r \cdot s = 0$ for $r \in R$, $s \in S$.
\end{definition}
Note that this is just graded version of the product ring
defined in \Cref{ex:product_ring}.
\begin{exercise}
	Show that if $R$ and $S$ are graded rings (meaning they have $1_R$ and $1_S$),
	then so is $R \times S$.
\end{exercise}

Now, the theorem is that:
\begin{theorem}
	[Cohomology pseudo-rings of wedge sums]
	We have
	\[
		\wt H^\bullet(X \wedge Y; R)
		\cong \wt H^\bullet(X;R)
		\times \wt H^\bullet(Y;R)
	\]
	as graded pseudo-rings.
\end{theorem}

This allows us to resolve the first question posed at the beginning.
Let $X = \CP^2$ and $Y = S^2 \vee S^4$.
We have that
\[ H^\bullet(\CP^2; \ZZ) \cong \ZZ[\alpha] / (\alpha^3). \]
Hence this is a graded ring generated by there elements:
\begin{itemize}
	\ii $1$, in dimension $0$.
	\ii $\alpha$, in dimension $2$.
	\ii $\alpha^2$, in dimension $4$.
\end{itemize}
Next, consider the reduced cohomology pseudo-ring
\[ \wt H^\bullet(S^2 \vee S^4; \ZZ) \cong
	\wt H^\bullet(S^2; \ZZ)
	\oplus \wt H^\bullet(S^4 ; \ZZ).
\]
Thus the absolute cohomology ring $H^\bullet(S^2 \vee S^4 \; \ZZ)$
is a graded ring also generated by three elements.
\begin{itemize}
	\ii $1$, in dimension $0$ (once we add back in the $0$th dimension).
	\ii $a_2$, in dimension $2$ (from $H^\bullet(S^2 ; \ZZ)$).
	\ii $a_4$, in dimension $4$ (from $H^\bullet(S^4 ; \ZZ)$).
\end{itemize}
Each graded component is isomorphic, like we expected.
However, in the former, the product of two degree $2$ generators is
\[ \alpha \cdot \alpha = \alpha^2. \]
In the latter, the product of two degree $2$ generators is
\[ a_2 \cdot a_2 = a_2^2 = 0 \]
since $a_2 \smile a_2 = 0 \in H^\bullet(S^2; \ZZ)$.

Thus $S^2 \vee S^4$ and $\CP^2$ are not homotopy equivalent.

\section{K\"unneth formula}
We now wish to tell apart the spaces $S^2 \times S^4$ and $\CP^3$.
In order to do this, we will need a formula
for $H^n(X \times Y; R)$ in terms of $H^n(X;R)$ and $H^n(Y;R)$.
Thus formulas are called \vocab{K\"unneth formulas}.
In this section we will only use a very special case,
which involves the tensor product of two graded rings.

\begin{definition}
	Let $A$ and $B$ be two graded rings which are also $R$-modules
	(where $R$ is a commutative ring with $1$).
	We define the \vocab{tensor product} $A \otimes_R B$ as follows.
	As an abelian group, it is
	\[ A \otimes_R B = \bigoplus_{d \ge 0}
		\left( \bigoplus_{k=0}^{d} A^k \otimes_R B^{d-k}  \right). \]
	The multiplication is given on basis elements by
	\[ \left( a_1 \otimes b_1 \right)\left( a_2 \otimes b_2 \right)
		= (a_1a_2) \otimes (b_1b_2).
	\]
	Of course the multiplicative identity is $1_A \otimes 1_B$.
\end{definition}

Now let $X$ and $Y$ be topological spaces, and take the product:
we have a diagram
\begin{diagram}
	&& X \times Y && \\
	X &\ldTo(2,1)^{\pi_X} && \rdTo(2,1)^{\pi_Y} & Y \\
\end{diagram}
where $\pi_X$ and $\pi_Y$ are projections.
As $H^k(-; R)$ is functorial, this gives induced maps
\begin{align*}
	\pi_X^\ast &: H^k(X \times Y; R) \to H^k(X; R) \\
	\pi_Y^\ast &: H^k(X \times Y; R) \to H^k(Y; R)
\end{align*}
for every $k$.

By using this, we can define a so-called cross product.
\begin{definition}
	Let $R$ be a ring, and $X$ and $Y$ spaces.
	Let $\pi_X$ and $\pi_Y$ be the projections of $X \times Y$
	onto $X$ and $Y$.
	Then the \vocab{cross product} is the map
	\[
		H^\bullet(X; R) \otimes_R H^\bullet(Y;R)
		\taking{\times} H^\bullet(X \times Y; R)
	\]
	acting on cocycles as follows:
	$\phi \times \psi = \pi_X^\ast(\phi) \smile \pi_Y^\ast(\psi)$.
\end{definition}

This is just the most natural way to take a $k$-cycle
on $X$ and an $\ell$-cycle on $Y$, and create a $(k+\ell)$-cycle
on the product space $X \times Y$.

\begin{theorem}
	[K\"unneth formula]
	Let $X$ and $Y$ be CW complexes such that $H^k(Y;R)$
	is a finitely generated free $R$-module for every $k$.
	Then the cross product is an isomorphism of anticommutative rings
	\[
		H^\bullet(X;R) \otimes_R H^\bullet(Y;R)
		\to H^\bullet(X \times Y; R).
	\]
\end{theorem}

In any case, this finally lets us resolve the question
set out at the beginning.
We saw that $H_n(\CP^3) \cong H_n(S^2 \times S^4)$ for every $n$,
and thus it follows that $H^n(\CP^3; \ZZ) \cong H^n(S^2 \times S^4; \ZZ)$ too.

But now let us look at the cohomology rings. First, we have
\[ H^\bullet(\CP^3; \ZZ) \cong \ZZ[\alpha] / (\alpha^3)
	\cong \ZZ \oplus \alpha\ZZ \oplus \alpha^2\ZZ \oplus \alpha^3\ZZ
\]
where $|\alpha| = 2$; hence this is a graded ring generated by
\begin{itemize}
	\ii $1$, in degree $0$.
	\ii $\alpha$, in degree $2$.
	\ii $\alpha^2$, in degree $4$.
	\ii $\alpha^3$, in degree $6$.
\end{itemize}

Now let's analyze
\[ H^\bullet(S^2 \times S^4; \ZZ) \cong
	\ZZ[\beta] / (\beta^2)
	\otimes
	\ZZ[\gamma] / (\gamma^2).
\]
It is thus generated thus by the following elements:
\begin{itemize}
	\ii $1 \otimes 1$, in degree $0$.
	\ii $\beta \otimes 1$, in degree $2$.
	\ii $1 \otimes \gamma$, in degree $4$.
	\ii $\beta \otimes \gamma$, in degree $6$.
\end{itemize}
Again in each dimension we have the same abelian group.
But notice that if we square $\beta \otimes 1$ we get
\[ (\beta \otimes 1)(\beta \otimes 1) = \beta^2 \otimes 1 = 0. \]
Yet the degree $2$ generator of $H^\bullet(\CP^3; \ZZ)$
does not have this property.
Hence these two graded rings are not isomorphic.

So it follows that $\CP^3$ and $S^2 \times S^4$ are not homotopy equivalent.

% Borsuk Ulam

\section\problemhead

\begin{dproblem}
	[Symmetry of Betti numbers by Poincar\'e duality]
	\label{prob:betti}
	Let $M$ be a smooth oriented compact $n$-manifold,
	and let $b_k$ denote its Betti number.
	Prove that $b_k = b_{n-k}$.
	\begin{hint}
		Write $H^k(M; \ZZ)$ in terms of $H_k(M)$
		using the UCT, and analyze the ranks.
	\end{hint}
\end{dproblem}

\begin{problem}
	Show that $\RP^n$ is not orientable for even $n$.
	\begin{hint}
		Use the previous result on Betti numbers.
	\end{hint}
\end{problem}

\begin{problem}
	Show that $\RP^3$ is not homotopy equivalent to $\RP^2 \vee S^3$.
	\begin{hint}
		Use the $\Zc2$ cohomologies, and find the cup product.
	\end{hint}
\end{problem}

\begin{problem}
	\gim
	Show that $S^m \vee S^n$ is not a deformation retract
	of $S^m \times S^n$ for any $m,n \ge 1$.
	\begin{hint}
		Assume that $r : S^m \times S^n \to S^m \vee S^n$ is such a map.
		Show that the induced map
		$H^\bullet(S^m \vee S^n; \ZZ) \to H^\bullet(S^m \times S^n; \ZZ)$
		between their cohomology rings is monic
		(since there exists an inverse map $i$).
	\end{hint}
	\begin{sol}
		See \cite[Example 3.3.14, pages 68-69]{ref:maxim752}.
	\end{sol}
\end{problem}


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

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

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

\begin{document}
\maketitle

\section{Implicit Integration}
The backward Euler integration method described in [1][2] requires the Jacobian of spring forces with respect to a particle's position and velocity. This document shows how to derive these Jacobians for use in a semi-implicit integrator.

\subsection{Vector Calculus Basics}

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

In order to calculate the force Jacobians we will need to know how to calculate the derivatives of some basic quantities with respect to a vector.

In general the derivative of a scalar valued function with respect to a vector is defined as:

\[\ddx{f} = \begin{bmatrix} \dd{f}{x_i} & \dd{f}{x_j} & \dd{f}{x_k} \end{bmatrix}\]

And for a vector valued function with respect to a vector:

\[\ddx{\v{f}} = \begin{bmatrix} \dd{f_i}{x_i} & \dd{f_i}{x_j} & \dd{f_i}{x_k} \\ \dd{f_j}{x_i} & \dd{f_j}{x_j} & \dd{f_j}{x_k} \\ \dd{f_k}{x_i} & \dd{f_k}{x_j} & \dd{f_k}{x_k} \end{bmatrix}\]

From these definitions we can work out the derivative of some basic geometric quantities. First, the derivative of a dot product of two vectors with respect to one vector:

\[\ddx{\v{x}^T \cdot \v{y}} = \v{y}^T \]

We will explicitly keep track of whether vectors are row vectors or column vectors as it will be important when taking derivatives of spring forces.

The derivative of a vector magnitude with respect to the vector, gives the normalized vector transposed:

\[\ddx{|\v{x}|} = \left(\frac{\v{x}}{|\v{x}|}\right)^T = \uv{x}^T \]

The derivative of a normalized vector $\v{\hat{x}} = \frac{\v{x}}{|\v{x}|} $ can be obtained using the quotient rule:

\[\ddx{\uv{x}} = \frac{\v{I}|\v{x}| - \v{x}\cdot\uv{x}^T}{|\v{x}|^2}\]

Where $\v{I}$ is the $n$ x $n$ identity matrix where n is dimension of $x$, and the product of a column vector and a row vector $\uv{x}\cdot\uv{x}^T$ is the outer product which is an $n$ x $n$ matrix that can be constructed using standard matrix multiplication definition.

Dividing through by $|\v{x}|$ we have:

\[\ddx{\uv{x}} = \frac{\v{I} - \uv{x}\cdot\uv{x}^T}{\v{|x|}}\]

\subsection{Jacobian of Stretch Force}

Recall the equation for the stretch force on a particle $i$ due to an undamped Hookean spring:

\[\v{F_s} = -k_s(|\v{x}_{ij}| - r)\uv{x}_{ij}\]

Where $\v{x}_{ij} = \v{x}_i - \v{x}_j$ is the vector between the two connected particles positions and $r$ is the rest length.

The Jacobian of the stretch force with respect to particle $i$'s position is obtained by using the product rule for the two $\v{x}_i$ dependent terms in $\v{F_s}$:

\[\dd{\v{F_s}}{\v{x}_i} = -ks\left[(|\v{x}_{ij}| - r)\dd{\uv{x}_{ij}}{\v{x}_i} + \uv{x}_{ij}\dd{(|\v{x}_{ij}| - r)}{\v{x}_i}\right]\]

Using the previously derived formulas for the derivative of a vector magnitude and normalized vector we have:

\[\dd{\v{F_s}}{\v{x}_i} = -ks\left[(|\v{x}_{ij}| - r)\left(\frac{\v{I} - \uv{x}_{ij}\cdot \uv{x}_{ij}^T}{|\v{x}_{ij}|}\right) + \uv{x}_{ij}\cdot \uv{x}_{ij}^T\right]\]

Dividing the first two terms through by $|\v{x}_{ij}|$:

\[\dd{\v{F_s}}{\v{x}_i} = -ks\left[(1 - \frac{r}{|\v{x}_{ij}|})\left(\v{I} - \uv{x}_{ij}\cdot \uv{x}_{ij}^T\right) + \uv{x}_{ij}\cdot \uv{x}_{ij}^T\right]\]

Due to the symmetry in the definition of $\v{x}_{ij}$ we have:

\[\dd{\v{F_s}}{\v{x}_j}  = -\dd{\v{F_s}}{\v{x}_i}\]

\subsection{Jacobian of Damping Force}

The equation for the damping force on a particle $i$ due to a spring:

\[\v{F_d} = -k_d\cdot\uv{x}(\v{v}_{ij}\cdot \uv{x}_{ij})\]

Where $\v{v}_{ij} = \v{v}_i-\v{v}_j$ is the relative velocities of the two particles. This is the preferred formulation because it damps only velocities along the spring axis.

Taking the derivative with respect to $\v{v}_i$:

\[\dd{\v{F_d}}{\v{v}_i} = -k_d\cdot\uv{x}\cdot\uv{x}^T\]

As with stretching, the force on the opposite particle is simply negated:

\[\dd{\v{F_d}}{\v{v}_j} = -\dd{\v{F_d}}{\v{v}_i} \]

\section {References}

\begin{itemize}
\item{[Baraff Witkin] - Physically Based Modelling, SIGGRAPH course - \url{http://www.pixar.com/companyinfo/research/pbm2001/}}
\item{[Baraff Witkin] - Large Steps in Cloth Simulation - \url{http://run.usc.edu/cs599-s10/cloth/baraff-witkin98.pdf}}
\item{[N. Joubert] - An Introduction to Simulation (\url{http://njoubert.com/teaching/cs184_sp09/section/simulation.pdf})}
\item{[D Prichard] - Implementing Baraff and Witkin's Cloth Simulation - \url{http://davidpritchard.org/freecloth/docs/report.pdf}}
\item{[Choi] - Stable but Responsive Cloth - \url{http://graphics.snu.ac.kr/~kjchoi/publication/cloth.pdf}}
\item{Numerical Recipes, 3rd edition 2007 - ch17.5}
\end{itemize}
\end{document}


\documentclass[twoside]{MATH77}
\usepackage[\graphtype]{mfpic}
\usepackage{multicol}
\usepackage[fleqn,reqno,centertags]{amsmath}
\begin{document}
\opengraphsfile{pl02-16}
\begmath 2.16 Complex Error Function $w(z)$

\silentfootnote{$^\copyright$1997 Calif. Inst. of Technology, \thisyear \ Math \`a la Carte, Inc.}

\subsection{Purpose}

Compute the Fadeeva function $w(z)$, defined by Equation~7.1.3 in \cite{ams55}:%
\begin{equation}\label{w_def}
w(z)=e^{-z^2}\left[ 1+\frac{2i}{\sqrt{\pi }}\int_0^ze^{t^2}dt\right] .
\end{equation}
The Fadeeva function is related to the error function by%
\begin{equation*}
w(z)=e^{-z^2}\erfc (-iz),
\end{equation*}
to Dawson's integral%
\begin{equation*}
F(z)=e^{-z^2}\int_0^ze^{t^2}dt,\text{ by}\quad \frac{2i}{\sqrt{\pi }}%
F(z)=w(z)-e^{-z^2},
\end{equation*}
to the Fresnel integrals%
\begin{equation*}
\hspace{-10pt}C(z)=\int_0^z\cos \left( \frac \pi 2t^2\right) dt,\ \text{and}\ \
S(z)=\int_0^z\sin \left( \frac \pi 2t^2\right) dt,
\end{equation*}
by
\begin{equation*}
C(z)+iS(z)=\frac{1+i}2\left[ 1- e^{i \pi z^2 / 2}w\left( \frac{\sqrt{\pi
}}2(1+i)z\right) \right] ,
\end{equation*}
and to the Voigt functions $V(x,y)$ and $L(x,y)$ (used in spectroscopy and
astronomy --- see \cite{Penner:1959:QMS}) by $V(x,y)=\Re w(z)$ and
$L(x,y)=\Im w(z)$, where $z=x+iy.$

\subsection{Usage}

\subsubsection{Program Prototype, Single Precision}

\begin{description}
\item[COMPLEX]  \ {\bf Z, W}

\item[INTEGER]  \ {\bf IFLAG}
\end{description}

$$
\fbox{{\bf CALL CWOFZ (Z, W, IFLAG)}}
$$

\subsubsection{Argument Definitions}

\begin{description}
\item[Z]  \ [in] Argument at which the function is to be evaluated.

\item[W]  \ [out] Value of $w(z)$, where $z$ is given by the argument Z.

\item[IFLAG]  \ [out] A flag indicating success or failure of the
computation:

\begin{itemize}
\item[0]  means $w(z)$ was successfully calculated,

\item[$+1$]  means $w(z)$ was not successfully calculated because it would
have no significant digits.

\item[$-1$]  means $w(z)$ was not successfully calculated because it would
overflow.
\end{itemize}

See Section E, Error Procedures and Restrictions, for a discussion of values
of Z that result in nonzero values of IFLAG. If IFLAG is nonzero, the real
and imaginary parts of W are set to the largest representable number.
\end{description}

\subsubsection{Modifications for Double Precision}

Change the subprogram name to ZWOFZ. On computers that support a double
precision complex data type, frequently spelled COMPLEX*16, and on which
double precision complex is represented by an array of two double precision
numbers, with the first being the real part, and the second being the
imaginary part, change the type declarations of Z and W so that Z and W are
declared to be of type double precision complex. On machines that do not
support double precision complex, change the declarations of Z and W to

{\bf DOUBLE PRECISION \ Z(2), W(2)}

In the latter case, put the real part of $z$ into Z(1), and the imaginary
part of $z$ into Z(2), and find the real and imaginary parts of $w(z)$ in
W(1) and W(2), respectively.

\subsection{Examples and Remarks}

See DRCWOFZ and ODCWOFZ for an example of the usage of this subprogram.

\subsection{Functional Description}

From Eq.~\eqref{w_def} it is clear that $w(z)$ is analytic in the entire
plane.  In the upper half-plane, $0 \leq |w(z)| < 1$, which may have been
the original reason to consider $w(z)$ to be a generalization of erfc.

$w(z)$ satisfies the differential equation $w^{\prime}(z) + 2zw(z) = 2i/%
\sqrt{\pi }$.  Since $w(z)$ is analytic it satisfies the Cauchy-Riemann
conditions.  If we let $w(z) = u(z) + iv(z)$, and $z = x + iy$, where $u$,
$v$, $x$ and $y$ are real, the Cauchy-Riemann conditions, together with
the differential equation, give us $u_x = 2yv - 2xu \text{ and } u_y = 2xv
+ 2yu - 2\pi ^{1/2} $.  In some applications, particularly related to
spectroscopy, one needs $V(x,y)$, $L(x,y)$, $V_x(x,y)$ and $V_y(x,y)$.  We
have already, above, identified $V(x,y)$ as $u(z)$ and $L(x,y)$ as $v(z)$.
Thus, one can compute these four functions by computing $w(z)$ alone.

These subprograms are based on a subprogram by G. P. M. Poppe and C. M. J.
Wijers described in \cite{Poppe:1990:MEC} and \cite{Poppe:1990:AEC}. The subprogram by Poppe and
Wijers was modified to work in either single or double precision, and some
tests of argument range were changed to be invoked only when necessary.
Machine characteristics are discovered, and errors are processed, using
MATH77 conventions. The subprogram name was changed to conform to
MATH77 naming conventions.

The algorithm uses three different methods. Inside the ellipse $%
(x/6.3)^2+(y/4.4)^2\leq .292^2$ the algorithm uses the series approximation
given by Equation~7.1.6 in \cite{ams55}. In the elliptic annulus $%
.292^2<(x/6.3)^2+(y/4.4)^2\leq 1.0$ the algorithm uses a Taylor series, with
derivatives calculated by the Laplace continued fraction. Outside the latter
ellipse, a Laplace continued fraction is used.
% \input prepic.tex
% \input pictex.tex
% \input postpic.tex
% \beginpicture \ninepoint
% \setcoordinatesystem units <   .786in,   .864in>
% \setplotarea x from    .000 to   3.500, y from  -2.500 to   3.000
% \axis left label {$\Im z$\hspace{-6pt}} ticks in numbered from -2.5 to
% 3.0 by .5 /
% \axis right ticks in quantity 12 /
% \axis top ticks in quantity 8 /
% \axis bottom label {} ticks in withvalues 0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 / at 0 .5 1 1.5 2 2.5 3 3.5 / /
% \put {$0.2$} at   1.080  2.460
% \put {$0.3$} at    .750  1.500
% \put {$0.5$} at    .420   .700
% \put {$1$} at    .280  -.040
% \put {$2$} at    .250  -.550
% \put {$5$} at    .250 -1.010
% \put {$10$} at    .250 -1.300
% \put {$30$} at    .250 -1.670
% \put {$100$} at    .250 -2.000
% \put {$300$} at    .250 -2.260
% \put {$45$} at   1.100   .640
% \put {$90$} at   1.260  -.400
% \put {$135$} at   1.280  -.920
% \put {$180$} at   1.350 -1.200
% \put {$-90$} at   1.480 -1.600
% \put {$0$} at   1.640 -1.900
% \put {$90$} at   1.830 -2.120
% \put {$180$} at   2.050 -2.300
% \put {$-90$} at   2.290 -2.400
% \put {$0$} at   2.590 -2.400
% \put {$90$} at   2.840 -2.400
% \setsolid
% \setquadratic \plot
%   .1225 -2.2415   .1050 -2.2406   .0700 -2.2392   .0350 -2.2384
%   .0000 -2.2382
% /
% \setquadratic \plot
%   .1225 -1.9820   .0700 -1.9796   .0000 -1.9785
% /
% \setquadratic \plot
%   .1575 -1.6553   .0875 -1.6506   .0000 -1.6486
% /
% \setquadratic \plot
%   .1575 -1.2904   .0875 -1.2848   .0000 -1.2823
% /
% \setquadratic \plot
%   .1750 -1.0094   .0875 -1.0015   .0000  -.9989
% /
% \setquadratic \plot
%   .1750  -.5257   .1050  -.5190   .0000  -.5151
% /
% \setquadratic \plot
%   .3150  -.5498   .3850  -.5671   .4375  -.5826   .4900  -.6005
%   .5425  -.6203   .5775  -.6350   .6265  -.6575   .6650  -.6764
%   .7000  -.6950   .7350  -.7150   .7761  -.7400   .8183  -.7675
%   .8575  -.7949   .8925  -.8209   .9275  -.8485   .9622  -.8775
%    .9934  -.9050   1.0150  -.9247   1.0500  -.9585   1.0787  -.9875
%   1.1047 -1.0150   1.1375 -1.0509   1.1725 -1.0913   1.2004 -1.1250
%   1.2250 -1.1555
% /
% \setquadratic \plot
%   1.3271 -1.2900   1.3475 -1.3178   1.3825 -1.3652   1.4081 -1.4000
%   1.4350 -1.4355   1.4700 -1.4812   1.4930 -1.5100
% /
% \setquadratic \plot
%   1.6100 -1.6430   1.6416 -1.6750   1.6702 -1.7025   1.6999 -1.7300
%   1.7325 -1.7588   1.7675 -1.7886   1.8200 -1.8317   1.8550 -1.8599
%   1.8982 -1.8950   1.9318 -1.9225   1.9775 -1.9608   2.0125 -1.9916
%   2.0562 -2.0325   2.0842 -2.0600   2.1350 -2.1131   2.1616 -2.1425
%   2.2050 -2.1924   2.2324 -2.2250   2.2575 -2.2550   2.2784 -2.2800
%   2.3018 -2.3075
% /
% \setquadratic \plot
%   2.4150 -2.4307   2.4500 -2.4649   2.4877 -2.5000
% /
% \setquadratic \plot
%   2.5200 -2.4074   2.4850 -2.3730   2.4500 -2.3351   2.4274 -2.3075
%   2.3865 -2.2525   2.3625 -2.2169   2.3317 -2.1700   2.3100 -2.1361
%   2.2750 -2.0833   2.2400 -2.0343   2.2050 -1.9897   2.1695 -1.9500
%   2.1175 -1.8986   2.0825 -1.8683   2.0473 -1.8400   2.0110 -1.8125
%   1.9600 -1.7754   1.9075 -1.7378   1.8601 -1.7025   1.8200 -1.6702
%   1.7675 -1.6233   1.7369 -1.5925   1.7120 -1.5650   1.6800 -1.5253
%   1.6489 -1.4825   1.6275 -1.4492   1.5989 -1.4000   1.5750 -1.3536
%   1.5575 -1.3173   1.5334 -1.2625   1.5108 -1.2075   1.4998 -1.1800
%   1.4875 -1.1486
% /
% \setquadratic \plot
%   1.4675 -1.0975   1.4458 -1.0425   1.4175  -.9737
% /
% \setquadratic \plot
%   1.3469  -.8225   1.3174  -.7675   1.2852  -.7125   1.2600  -.6727
%   1.2250  -.6215   1.1904  -.5750   1.1686  -.5475   1.1375  -.5104
%   1.1025  -.4715   1.0850  -.4530   1.0500  -.4181   1.0150  -.3854
%   .9802  -.3550   .9625  -.3400   .9275  -.3122   .8925  -.2860
%   .8575  -.2614   .8326  -.2450   .8050  -.2273   .7700  -.2062
%   .7175  -.1770   .6891  -.1625   .6650  -.1505   .6314  -.1350
%   .5950  -.1190   .5663  -.1075   .5425  -.0981   .5075  -.0855
%   .4725  -.07376   .4025  -.05318   .3500  -.03987
% /
% \setquadratic \plot
%   .2100  -.01412   .1050  -.00344   .0000   .00006
% /
% \setquadratic \plot
%   .2800   .7484   .1400   .7640   .0000   .7691
% /
% \setquadratic \plot
%   .5600   .6844   .6257   .6625   .6825   .6415   .7350   .6201
%   .7875   .5967   .8225   .5799   .8759   .5525   .9256   .5250
%    .9717   .4975   1.0149   .4700   1.0555   .4425   1.0937   .4150
%   1.1298   .3875   1.1639   .3600   1.1964   .3325   1.2272   .3050
%   1.2600   .2742   1.2845   .2500   1.3125   .2210   1.3475   .1827
%   1.3825   .1415   1.4058   .1125   1.4350   .0740   1.4660   .0300
%   1.4875  -.0025   1.5182  -.0525   1.5488  -.1075   1.5630  -.1350
%   1.5890  -.1900   1.6009  -.2175   1.6227  -.2725   1.6417  -.3275
%   1.6581  -.3825   1.6720  -.4375   1.6834  -.4925   1.6924  -.5475
%   1.6991  -.6025   1.7063  -.7125   1.7071  -.7675   1.7063  -.8225
%   1.7043  -.8775   1.6998  -.9600   1.6968 -1.0150   1.6938 -1.0975
%   1.6935 -1.1250   1.6947 -1.1800   1.6975 -1.2190   1.7027 -1.2625
%   1.7135 -1.3175   1.7210 -1.3450   1.7413 -1.4000   1.7544 -1.4275
%   1.7850 -1.4775   1.8102 -1.5100   1.8375 -1.5391   1.8900 -1.5830
%   1.9425 -1.6175   1.9978 -1.6475   2.0825 -1.6886   2.1350 -1.7156
%   2.2022 -1.7575   2.2400 -1.7871   2.2923 -1.8400   2.3322 -1.8950
%   2.3753 -1.9775   2.3974 -2.0325   2.4178 -2.0875   2.4325 -2.1261
%   2.4502 -2.1700   2.4850 -2.2376   2.5353 -2.3075
% /
% \setquadratic \plot
%   2.6101 -2.3075   2.5809 -2.2800   2.5419 -2.2250   2.5290 -2.1975
%   2.5119 -2.1425   2.5026 -2.0875   2.5000 -2.0600   2.4966 -2.0050
%   2.4911 -1.9225   2.4850 -1.8779   2.4692 -1.8125   2.4458 -1.7575
%   2.4077 -1.7025   2.3625 -1.6614   2.3100 -1.6310   2.2575 -1.6118
%   2.1700 -1.5932   2.1000 -1.5816   2.0125 -1.5617   1.9522 -1.5375
%   1.8900 -1.4958   1.8725 -1.4793   1.8375 -1.4334   1.8201 -1.4000
%   1.8035 -1.3450   1.7995 -1.3175   1.7981 -1.2900   1.8020 -1.2350
%   1.8072 -1.2075   1.8200 -1.1616   1.8340 -1.1250   1.8465 -1.0975
%   1.8725 -1.0480   1.8900 -1.0187   1.9075  -.9913   1.9250  -.9650
%   1.9477  -.9325   1.9670  -.9050   1.9950  -.8643   2.0125  -.8387
%   2.0416  -.7950   2.0586  -.7675   2.0825  -.7256   2.1000  -.6927
%   2.1172  -.6575   2.1294  -.6300   2.1508  -.5750   2.1602  -.5475
%   2.1762  -.4925   2.1875  -.4433   2.1940  -.4100   2.2020  -.3550
%   2.2073  -.3000   2.2100  -.2450   2.2103  -.2175   2.2092  -.1625
%   2.2057  -.1075   2.2031  -.0800   2.1963  -.0250   2.1875   .0278
%   2.1818   .0575   2.1700   .1096   2.1623   .1400   2.1525   .1752
%   2.1380   .2225   2.1289   .2500   2.1089   .3050   2.0866   .3600
%   2.0650   .4088   2.0475   .4456   2.0211   .4975   1.9907   .5525
%   1.9578   .6075   1.9036   .6900   1.8375   .7796   1.7850   .8440
%   1.7264   .9100   1.6800   .9586   1.6275  1.0099   1.5867  1.0475
%   1.5230  1.1025   1.4700  1.1452   1.4172  1.1850   1.3650  1.2222
%   1.2966  1.2675   1.2425  1.3010   1.1725  1.3413   1.1043  1.3775
%   1.0325  1.4127    .9625  1.4441    .8750  1.4796
% /
% \setquadratic \plot
%   .6125  1.5633   .5425  1.5803   .4550  1.5983   .3500  1.6157
%   .2450  1.6284   .1225  1.6376   .0000  1.6407
% /
% \setquadratic \plot
%   .0000  2.6529   .1400  2.6499   .2800  2.6408   .4200  2.6258
%   .5425  2.6075   .6499  2.5875   .7729  2.5600   .8575  2.5382
%   .9450  2.5130
% /
% \setquadratic \plot
%   1.2075  2.4206   1.2950  2.3840   1.3914  2.3400   1.4875  2.2922
%   1.5750  2.2450   1.6483  2.2025   1.7325  2.1504   1.8025  2.1042
%   1.8900  2.0424   1.9600  1.9896   2.0371  1.9275   2.1013  1.8725
%   2.1875  1.7935   2.2575  1.7244   2.3275  1.6504   2.4212  1.5425
%   2.5076  1.4325   2.5725  1.3416   2.6388  1.2400   2.6775  1.1757
%   2.7300  1.0813   2.7615  1.0200   2.8000   .9390   2.8175   .8995
%   2.8475   .8275   2.8686   .7725   2.8875   .7197   2.9050   .6671
%   2.9225   .6100   2.9386   .5525   2.9526   .4975   2.9651   .4425
%   2.9813   .3600   2.9903   .3050   2.9979   .2500   3.0042   .1950
%   3.0090   .1400   3.0109   .1125   3.0136   .0575   3.0148   .0025
%   3.0149  -.0250   3.0139  -.0800   3.0115  -.1350   3.0097  -.1625
%   3.0049  -.2175   2.9985  -.2725   2.9905  -.3275   2.9808  -.3825
%   2.9693  -.4375   2.9575  -.4865   2.9487  -.5200   2.9400  -.5501
%   2.9225  -.6057   2.9043  -.6575   2.8827  -.7125   2.8525  -.7809
%   2.8175  -.8507   2.7871  -.9050   2.7530  -.9600   2.7300  -.9945
%   2.6950 -1.0425   2.6775 -1.0651   2.6502 -1.0975   2.6253 -1.1250
%   2.6075 -1.1436   2.5900 -1.1592   2.5395 -1.1800   2.5368 -1.1800
%   2.5025 -1.1697   2.4738 -1.1525   2.4500 -1.1405   2.4221 -1.1250
%   2.3800 -1.1065   2.3450 -1.0932   2.2925 -1.0783   2.2050 -1.0660
%   2.1175 -1.0704   2.0650 -1.0818   2.0125 -1.1016   1.9775 -1.1206
%   1.9425 -1.1459   1.9250 -1.1618   1.8900 -1.2044   1.8556 -1.2900
%   1.8529 -1.3175   1.8550 -1.3500   1.8696 -1.4000   1.8856 -1.4275
%   1.9075 -1.4528   1.9489 -1.4825   2.0125 -1.5039   2.1000 -1.5042
%   2.1875 -1.4811   2.2400 -1.4581   2.2750 -1.4393   2.2969 -1.4275
%   2.3275 -1.4078   2.3625 -1.3843   2.3975 -1.3593   2.4184 -1.3450
%   2.4500 -1.3204   2.4675 -1.3064   2.5025 -1.2817   2.5550 -1.2687
%   2.5900 -1.2935   2.6103 -1.3175   2.6425 -1.3656   2.6597 -1.4000
%   2.6775 -1.4446   2.6995 -1.5375   2.7039 -1.5925   2.7021 -1.6475
%   2.6950 -1.6971   2.6882 -1.7300   2.6717 -1.7850   2.6600 -1.8159
%   2.6425 -1.8578   2.6264 -1.8950   2.6142 -1.9225   2.6022 -1.9500
%   2.5807 -2.0050   2.5651 -2.0600   2.5582 -2.1150   2.5630 -2.1700
%   2.5840 -2.2250   2.6035 -2.2525   2.6337 -2.2800   2.6775 -2.3041
%   2.7125 -2.3149
% /
% \setquadratic \plot
%   2.7125 -2.3640   2.6950 -2.3568   2.6600 -2.3402
% /
% \setquadratic \plot
%   2.6600 -2.4085   2.6775 -2.4194   2.7125 -2.4397
% /
% \setquadratic \plot
%   2.9750 -2.3417   3.0176 -2.3625   3.0625 -2.4036   3.0871 -2.4450
%   3.1036 -2.5000
% /
% \setquadratic \plot
%   2.1525 -2.4999   2.1175 -2.4686   2.0650 -2.4221   2.0475 -2.4065
%   2.0282 -2.3900
% /
% \setquadratic \plot
%   1.9250 -2.3011   1.8725 -2.2575   1.8323 -2.2250
% /
% \setquadratic \plot
%   1.7325 -2.1461   1.6975 -2.1191   1.6625 -2.0923   1.6275 -2.0657
%   1.5832 -2.0325   1.5467 -2.0050   1.5050 -1.9735   1.4700 -1.9473
%   1.4175 -1.9075   1.3825 -1.8813   1.3300 -1.8422   1.2950 -1.8160
%   1.2600 -1.7899   1.2250 -1.7641   1.1900 -1.7386   1.1550 -1.7133
%   1.1025 -1.6766   1.0675 -1.6524   1.0325 -1.6288    .9975 -1.6059
%   .9450 -1.5728   .9100 -1.5516   .8575 -1.5212   .8225 -1.5019
%   .7852 -1.4825   .7350 -1.4573   .6825 -1.4328   .6300 -1.4102
%   .5775 -1.3895   .5250 -1.3709   .4550 -1.3486   .4200 -1.3387
%   .3500 -1.3214
% /
% \setquadratic \plot
%   .3150 -1.0326   .3566 -1.0425   .4200 -1.0592   .4725 -1.0756
%   .5339 -1.0975   .5775 -1.1144   .6300 -1.1370   .6650 -1.1535
%   .7176 -1.1800   .7682 -1.2075   .8151 -1.2350   .8575 -1.2613
%   .8925 -1.2841   .9275 -1.3080   .9625 -1.3330   .9975 -1.3589
%   1.0500 -1.4000   1.0841 -1.4275   1.1200 -1.4572   1.1550 -1.4868
%   1.1900 -1.5170   1.2250 -1.5478   1.2600 -1.5789   1.3055 -1.6200
%   1.3475 -1.6574
% /
% \setquadratic \plot
%   1.3977 -1.7025   1.4289 -1.7300   1.4604 -1.7575   1.4923 -1.7850
%   1.5245 -1.8125   1.5572 -1.8400   1.5750 -1.8544
% /
% \setquadratic \plot
%   1.7150 -1.9682   1.7500 -1.9963   1.7945 -2.0325
% /
% \setquadratic \plot
%   1.9250 -2.1418   1.9572 -2.1700   1.9878 -2.1975
% /
% \setquadratic \plot
%   1.8834 -2.5000   1.8470 -2.4725   1.8025 -2.4391   1.7675 -2.4135
%   1.7150 -2.3753   1.6800 -2.3504   1.6450 -2.3259   1.6100 -2.3017
%   1.5575 -2.2657   1.5225 -2.2421   1.4875 -2.2190   1.4350 -2.1843
%   1.3825 -2.1505   1.3475 -2.1282   1.2950 -2.0953   1.2600 -2.0737
%   1.2075 -2.0419   1.1725 -2.0211   1.1200 -1.9905   1.0850 -1.9708
%   1.0325 -1.9418    .9959 -1.9225    .9450 -1.8961    .9100 -1.8783
%   .8575 -1.8531   .8225 -1.8373   .7655 -1.8125   .7175 -1.7925
%   .6650 -1.7723   .6229 -1.7575   .5600 -1.7364   .5075 -1.7207
%   .4378 -1.7025   .4025 -1.6938   .3500 -1.6828
% /
% \setquadratic \plot
%   .3850 -2.0145   .4550 -2.0291   .5250 -2.0455   .5775 -2.0600
%   .6300 -2.0748   .6825 -2.0915   .7506 -2.1150   .8050 -2.1346
%   .8575 -2.1550   .8933 -2.1700   .9578 -2.1975   .9975 -2.2148
%   1.0500 -2.2390   1.0850 -2.2560   1.1375 -2.2819   1.1900 -2.3086
%   1.2425 -2.3361   1.2950 -2.3644   1.3475 -2.3934   1.3897 -2.4175
%   1.4372 -2.4450   1.4837 -2.4725   1.5293 -2.5000
% /
% \setquadratic \plot
%   1.1130 -2.5000   1.0675 -2.4797   1.0150 -2.4574    .9625 -2.4362
%   .9100 -2.4163   .8575 -2.3967   .8050 -2.3782   .7525 -2.3614
%   .6651 -2.3350   .5950 -2.3156   .5250 -2.2986   .4550 -2.2839
%   .3850 -2.2708
% /
% \setquadratic \plot
%   1.9189 -1.3725   1.9334 -1.4000   1.9600 -1.4231   1.9950 -1.4345
%   2.0437 -1.4275   2.0977 -1.3725   2.1035 -1.3450   2.1000 -1.3161
%   2.0650 -1.2678   2.0300 -1.2529   1.9672 -1.2625   1.9195 -1.3175
%   1.9189 -1.3725
% /
% \setquadratic \plot
%   2.6238 -2.1975   2.6414 -2.2250   2.6775 -2.2483   2.7125 -2.2539
%   2.7475 -2.2466   2.7825 -2.2261   2.8065 -2.1975   2.8175 -2.1662
%   2.8197 -2.1425   2.8135 -2.1150   2.7957 -2.0875   2.7475 -2.0606
%   2.7125 -2.0591   2.6775 -2.0692   2.6301 -2.1150   2.6196 -2.1425
%   2.6238 -2.1975
% /
% \setdashes
% \setquadratic \plot
%    .1570 -2.5000    .1606 -2.4450    .1662 -2.3625
% /
% \setquadratic \plot
%    .1808 -2.1700    .1831 -2.1425    .1878 -2.0875
% /
% \setquadratic \plot
%    .2065 -1.8950    .2124 -1.8400    .2220 -1.7575
% /
% \setquadratic \plot
%    .2474 -1.5650    .2598 -1.4825    .2732 -1.4000
% /
% \setquadratic \plot
%   .3090 -1.2075   .3265 -1.1250   .3500 -1.0229   .3675  -.9527
%   .3875  -.8775   .4030  -.8225   .4276  -.7400   .4449  -.6850
%   .4724  -.6025   .4900  -.5525   .5075  -.5045   .5250  -.4582
%   .5439  -.4100   .5600  -.3701   .5777  -.3275   .5950  -.2871
%   .6135  -.2450   .6300  -.2085   .6514  -.1625   .6650  -.1337
%   .6912  -.08000   .7049  -.05250   .7330   .00250   .7474   .03000
%   .7620   .0575   .7769   .0850   .8050   .1362   .8225   .1672
%   .8384   .1950   .8543   .2225   .8750   .2577   .8925   .2870
%   .9202   .3325   .9450   .3726   .9719   .4150   .9975   .4550
%   1.0253   .4975   1.0435   .5250   1.0619   .5525
% /
% \setquadratic \plot
%   1.1900   .7367   1.2159   .7725   1.2562   .8275   1.2775   .8563
%   1.3178   .9100   1.3475   .9492   1.3825   .9948   1.4235  1.0475
%   1.4668  1.1025   1.5050  1.1505   1.5400  1.1941   1.5773  1.2400
%   1.6275  1.3013   1.6679  1.3500   1.7138  1.4050   1.7500  1.4480
%   1.8025  1.5099   1.8375  1.5508   1.8900  1.6118   1.9425  1.6723
%   1.9950  1.7323   2.0458  1.7900   2.0945  1.8450   2.1434  1.9000
%   2.1926  1.9550   2.2421  2.0100   2.2925  2.0658   2.3450  2.1237
%   2.4150  2.2005   2.4672  2.2575   2.5200  2.3149   2.5725  2.3718
%   2.6425  2.4474   2.6950  2.5039   2.7475  2.5602   2.8175  2.6351
%   2.8761  2.6975   2.9400  2.7656   3.0100  2.8398   3.0835  2.9175
%   3.1618  3.0000
% /
% \setquadratic \plot
%   3.5000  -.00009   3.0625  -.00113   2.6075  -.00962   2.4675  -.01671
%   2.3275  -.02768   2.2225  -.03918   2.1284  -.05250   2.0650  -.06316
%   1.9807  -.0800   1.9250  -.0927   1.8550  -.1109   1.8025  -.1262
%   1.7500  -.1430   1.7150  -.1551   1.6800  -.1680   1.6450  -.1816
%   1.6100  -.1960   1.5750  -.2113   1.5400  -.2274   1.5050  -.2446
%   1.4700  -.2625   1.4516  -.2725   1.4029  -.3000   1.4000  -.3016
%   1.3650  -.3227
% /
% \setquadratic \plot
%   1.1550  -.4757   1.1357  -.4925   1.1025  -.5223   1.0758  -.5475
%   1.0477  -.5750   1.0150  -.6087    .9800  -.6467    .9468  -.6850
%   .9241  -.7125   .8925  -.7523   .8575  -.7993   .8225  -.8494
%   .7875  -.9029   .7691  -.9325   .7350  -.9905   .7000 -1.0546
%   .6650 -1.1240   .6388 -1.1800   .6030 -1.2625   .5704 -1.3450
%   .5407 -1.4275   .5050 -1.5375   .4725 -1.6498   .4317 -1.8125
%   .3964 -1.9775   .3911 -2.0050   .3850 -2.0371
% /
% \setquadratic \plot
%   .3758 -2.0875   .3675 -2.1352   .3617 -2.1700
% /
% \setquadratic \plot
%   .3323 -2.3625   .3212 -2.4450   .3141 -2.5000
% /
% \setquadratic \plot
%   .4712 -2.5000   .5166 -2.2800   .5775 -2.0388   .6210 -1.8950
%   .6795 -1.7300   .7175 -1.6370   .7700 -1.5231   .8203 -1.4275
%   .8692 -1.3450   .9052 -1.2900   .9450 -1.2341   .9873 -1.1800
%   1.0346 -1.1250   1.0850 -1.0724   1.1200 -1.0392   1.1375 -1.0235
%   1.1550 -1.0085
% /
% \setquadratic \plot
%   1.2491 -1.2900   1.2075 -1.3271   1.1550 -1.3798   1.0896 -1.4550
%   1.0325 -1.5304    .9625 -1.6373    .8925 -1.7628    .8060 -1.9500
%   .7333 -2.1425   .6808 -2.3075   .6283 -2.5000
% /
% \setquadratic \plot
%   1.2566 -2.5000   1.3475 -2.3310   1.4525 -2.1615   1.5050 -2.0851
%   1.5750 -1.9902
% /
% \setquadratic \plot
%   1.7150 -1.8177   1.7500 -1.7765   1.8025 -1.7145   1.8351 -1.6750
%   1.8725 -1.6276   1.8981 -1.5925   1.9337 -1.5375   1.9488 -1.5100
%   1.9729 -1.4550   1.9815 -1.4275   1.9903 -1.3725
% /
% \setquadratic \plot
%   1.9950 -1.3666   2.0087 -1.3725   2.0262 -1.4000   2.0475 -1.4630
%   2.0516 -1.5100   2.0475 -1.5480   2.0369 -1.5925   2.0148 -1.6475
%   1.9950 -1.6852   1.9487 -1.7575   1.9075 -1.8131   1.8375 -1.9009
%   1.7528 -2.0050   1.6461 -2.1425   1.5400 -2.2933   1.4782 -2.3900
%   1.4135 -2.5000
% /
% \setquadratic \plot
%   1.5704 -2.5000   1.6612 -2.3625   1.7621 -2.2250
% /
% \setquadratic \plot
%   1.9200 -2.0325   1.9600 -1.9862   2.0125 -1.9253   2.0383 -1.8950
%   2.0825 -1.8410   2.1046 -1.8125   2.1436 -1.7575   2.1609 -1.7300
%   2.1875 -1.6794   2.2160 -1.5925   2.2195 -1.5375   2.2087 -1.4825
%   2.1961 -1.4550   2.1767 -1.4275   2.1525 -1.4048   2.1175 -1.3825
%   2.0825 -1.3680   2.0475 -1.3597   1.9950 -1.3581
% /
% \setquadratic \plot
%   1.9950 -1.3496   2.0047 -1.3450   2.0274 -1.3175   2.0475 -1.2923
%   2.0650 -1.2686   2.0825 -1.2421   2.1000 -1.2101   2.1199 -1.1525
%   2.1253 -1.0975   2.1176 -1.0425   2.1088 -1.0150   2.0825  -.9640
%   2.0475  -.9197   1.9996  -.8775   1.9569  -.8500   1.9075  -.8262
%   1.8550  -.8081   1.8025  -.7963   1.7500  -.7895   1.6625  -.7885
%   1.5925  -.7963   1.5225  -.8110   1.4700  -.8267   1.4525  -.8328
%   1.4175  -.8463
% /
% \setquadratic \plot
%   1.4875 -1.1440   1.5400 -1.1263   1.5925 -1.1134   1.6975 -1.1042
%   1.8156 -1.1250   1.8550 -1.1413   1.8900 -1.1624   1.9116 -1.1800
%   1.9385 -1.2075   1.9583 -1.2350   1.9725 -1.2625   1.9775 -1.2734
%   1.9884 -1.3175
% /
% \setquadratic \plot
%   1.9648 -2.1975   2.0151 -2.1425   2.0681 -2.0875   2.1175 -2.0388
%   2.1535 -2.0050   2.1875 -1.9744   2.2400 -1.9296   2.2750 -1.9018
%   2.3100 -1.8757   2.3450 -1.8516   2.3975 -1.8197   2.4500 -1.7936
%   2.5025 -1.7744   2.6075 -1.7607   2.7125 -1.7884   2.7650 -1.8278
%   2.8000 -1.8784   2.8145 -1.9225   2.8171 -1.9500   2.8101 -2.0050
%   2.7866 -2.0600   2.7693 -2.0875   2.7481 -2.1150   2.7253 -2.1425
%   2.6950 -2.1732
% /
% \setquadratic \plot
%   2.6950 -2.1913   2.7092 -2.1975   2.7235 -2.2250   2.7311 -2.2525
%   2.7329 -2.3075
% /
% \setquadratic \plot
%   2.7125 -2.3800   2.6940 -2.4175   2.6600 -2.4710
% /
% \setquadratic \plot
%   2.6517 -2.3075   2.6772 -2.2525   2.6896 -2.1975
% /
% \setquadratic \plot
%   2.6950 -2.1822   2.7475 -2.1854   2.7825 -2.2003   2.8141 -2.2250
%   2.8336 -2.2525   2.8350 -2.2559   2.8500 -2.3075
% /
% \setquadratic \plot
%   2.9976 -2.5000   3.0416 -2.4725   3.0800 -2.4525   3.1150 -2.4391
%   3.1675 -2.4278   3.2375 -2.4330   3.2900 -2.4570   3.3091 -2.4725
%   3.3322 -2.5000
% /
% \setquadratic \plot
%   2.6875 -2.1425   2.6775 -2.1093   2.6425 -2.0605   2.6075 -2.0351
%   2.5725 -2.0205   2.4850 -2.0133   2.3975 -2.0349   2.3427 -2.0600
%   2.2925 -2.0896   2.2400 -2.1266   2.1875 -2.1694   2.1868 -2.1700
%   2.1559 -2.1975
% /
% \setquadratic \plot
%   1.9735 -2.3900   1.9285 -2.4450   1.8856 -2.5000
% /
% \setquadratic \plot
%   1.9250 -2.2431   1.8550 -2.3281   1.7866 -2.4175   1.7665 -2.4450
%   1.7277 -2.5000
% /
% \setquadratic \plot
%   2.5023 -2.5000   2.5025 -2.4998   2.5200 -2.4802
% /
% \setquadratic \plot
%    .7855 -2.5000    .8828 -2.2250   1.0087 -1.9500   1.1047 -1.7850
%   1.2250 -1.6199   1.3300 -1.5067   1.4525 -1.4054   1.5225 -1.3611
%   1.6100 -1.3192   1.6625 -1.3012   1.7325 -1.2860   1.8200 -1.2825
%   1.8900 -1.2952   1.9250 -1.3079   1.9600 -1.3264   1.9775 -1.3393
%   1.9950 -1.3534
% /
% \setquadratic \plot
%   1.9950 -1.3592   1.9425 -1.3581   1.8900 -1.3673   1.8550 -1.3768
%   1.7908 -1.4000   1.7327 -1.4275   1.6839 -1.4550   1.6450 -1.4799
%   1.6023 -1.5100
% /
% \setquadratic \plot
%   1.3955 -1.7025   1.3300 -1.7811   1.2659 -1.8675   1.1937 -1.9775
%   1.1200 -2.1056   1.0338 -2.2800    .9625 -2.4483    .9531 -2.4725
%    .9426 -2.5000
% /
% \setquadratic \plot
%   1.0996 -2.5000   1.1900 -2.3103   1.2950 -2.1234   1.3650 -2.0150
%   1.4350 -1.9173   1.4875 -1.8501   1.5422 -1.7850   1.5916 -1.7300
%   1.6441 -1.6750   1.6800 -1.6393   1.7288 -1.5925   1.7585 -1.5650
%   1.7889 -1.5375   1.8200 -1.5099   1.8550 -1.4793   1.8822 -1.4550
%   1.9075 -1.4334   1.9250 -1.4183   1.9600 -1.3878   1.9719 -1.3725
%   1.9950 -1.3650
% /
% \setquadratic \plot
%   2.2254 -2.3075   2.3100 -2.2365   2.3975 -2.1791   2.4500 -2.1540
%   2.5200 -2.1334   2.5900 -2.1303   2.6425 -2.1441   2.6775 -2.1609
%   2.6950 -2.1791
% /
% \setquadratic \plot
%   2.6950 -2.1847   2.6425 -2.1822   2.5900 -2.1963   2.5375 -2.2192
%   2.4850 -2.2490   2.4500 -2.2725   2.4034 -2.3075
% /
% \setquadratic \plot
%   2.4150 -2.4399   2.4653 -2.3900   2.5200 -2.3377
% /
% \setquadratic \plot
%   2.5522 -2.3075   2.5725 -2.2891   2.6075 -2.2568   2.6250 -2.2412
%   2.6600 -2.2097   2.6687 -2.1975   2.6950 -2.1902
% /
% \setquadratic \plot
%   2.1419 -2.3900   2.0915 -2.4450   2.0442 -2.5000
% /
% \endpicture\vspace{-5pt}
%\centerline{$\Re z$}\vspace{5pt}
%\centerline{\bf Modulus and phase of $w(z).$}
%
\subparagraph{Accuracy Tests}

The accuracy of the approximations was analyzed by Poppe and Wijers. They
concluded that relative accuracy is maintained within~14 significant digits,
except inside a circle of radius~0.126 around a zero of the function (all
zeros of $w(z)$ are in the lower half-plane), where absolute accuracy is
retained but relative accuracy is not. They also compared the accuracy of $%
w(z)$ to $\erfc z$ for several purely imaginary values of $z$. Their testing
procedure is reported in \cite{Poppe:1990:MEC}.

We tested CWOFZ in four regions along the imaginary axis, where $w(z)$ is
real, on an IBM PC/AT, by comparing to DERFCE. Each region was divided into
500 subregions, and a point was randomly selected in each subregion. The
maximum errors are shown below, where ULP means ``error in units of last
position of the reference result," and $\rho \approx 1.192\times 10^{- 7}$ is the
round-off level for IEEE single precision arithmetic.\vspace{-5pt}
\begin{center}
\begin{tabular}{lrrr}
\multicolumn{1}{c}{\bf Range} & {\bf ULP} &
\multicolumn{1}{c}{\bf Absolute} & \multicolumn{1}{c}{\bf Relative}\\
$[$0..0.75$]$ & 2.42 & 1.21 $\rho $\rule{.1in}{0pt} & 2.29 $\rho $\rule{.05in}{0pt}\\
$[$0.75..1.2848$]$ & 19.05 & 4.76 $\rho $\rule{.1in}{0pt} & 12.95 $\rho $\rule{.05in}{0pt}\\
$[$1.2848..4.4$]$ & 6.05 & 1.23 $\rho $\rule{.1in}{0pt} & 3.49 $\rho $\rule{.05in}{0pt}\\
$[$4.4..100$]$ & 2.19 & 0.12 $\rho $\rule{.1in}{0pt} & 1.43 $\rho $\rule{.05in}{0pt}
\end{tabular}
\end{center}\vspace{-5pt}
In the range [0.75..1.2848] errors grow as $x$ increases. Errors are four
times larger near~1.2848 than near~0.75.

We tested ZWOFZ in five regions along the imaginary axis, where $w(z)$ is
real, on an IBM PC/AT, by comparing to an extended precision calculation of $%
\exp (x^2) \erfc x.$ Each region was divided into~200 subregions, and a
point was randomly selected in each subregion. The maximum errors are shown
below, where $\rho \approx 2.22\times 10^{-16}$ is the round-off level for IEEE
double precision arithmetic.\vspace{-5pt}
\begin{center}
\begin{tabular}{lrrr}
\multicolumn{1}{c}{\bf Range} & {\bf ULP} &
\multicolumn{1}{c}{\bf Absolute} & \multicolumn{1}{c}{\bf Relative}\\
$[$0..0.75$]$ & 1.47 & 0.73 $\rho $\rule{.1in}{0pt} & 1.35 $\rho $\rule{.05in}{0pt}\\
$[$0.75..1.2848$]$ & 14.29 & 3.57 $\rho $\rule{.1in}{0pt} & 9.84 $\rho $\rule{.05in}{0pt}\\
$[$1.2848..3.4469$]$ & 6.23 & 0.78 $\rho $\rule{.1in}{0pt} & 4.44 $\rho $\rule{.05in}{0pt}\\
$[$3.4469..4.4$]$ & 14.20 & 1.78 $\rho $\rule{.1in}{0pt} & 13.42 $\rho $\rule{.05in}{0pt}\\
$[$4.4..100$]$ & 3.02 & 0.19 $\rho $\rule{.1in}{0pt} & 1.52 $\rho $\rule{.05in}{0pt}
\end{tabular}
\end{center}\vspace{-5pt}
In the range [0.75..1.2848] errors grow as $x$ increases. Errors are four
times larger near~1.2848 than near~0.75.

We tested ZWOFZ in four regions along the real axis on an IBM PC/AT, by
comparing the imaginary part of the result to an extended precision
calculation of Dawson's integral, $F(x)$. Each region was divided into~200
subregions, and a point was randomly selected in each subregion. The maximum
errors are as follows.\vspace{-5pt}
\begin{center}
\begin{tabular}{lrrr}
\multicolumn{1}{c}{\bf Range} & {\bf ULP} &
\multicolumn{1}{c}{\bf Absolute} & \multicolumn{1}{c}{\bf Relative}\\
$[$0..1.83960$]$ & 2.21 & 0.93 $\rho $\rule{.1in}{0pt} & 1.73 $\rho $\rule{.05in}{0pt}\\
$[$1.8396..2.95$]$ & 17.28 & 3.90 $\rho $\rule{.1in}{0pt} & 11.20 $\rho $\rule{.05in}{0pt}\\
$[$2.95..6.3$]$ & 8.99 & 1.12 $\rho $\rule{.1in}{0pt} & 6.24 $\rho $\rule{.05in}{0pt}\\
$[$6.3..100$]$ & 1.59 & 0.06 $\rho $\rule{.1in}{0pt} & 0.90 $\rho $\rule{.05in}{0pt}
\end{tabular}
\end{center}\vspace{-5pt}
We tested ZWOFZ along the real axis on an IBM PC/AT, by comparing the real
part of the result to an extended precision calculation of $\exp (-x^2)$, in
the range [0..10]. The maximum error was 2.04~ULP; the maximum absolute
error was 0.26\ $\rho $; the maximum relative error was 0.45\ $\rho $. The
largest errors occurred near the change of method at $x = 1.8396.$

\bibliography{math77}
\bibliographystyle{math77}

\subsection{Error Procedures and Restrictions}

$w(z)$ is computable throughout the upper half plane without restriction. To
compute $w(z)$ in the lower half plane we use $w(-z) = 2 \exp (-z^2)-w(z)$.
Since $\exp (-z^2) = \exp (\Re z^2) [\cos (\Im z^2) + i \sin(\Im z^2)]$, we
must have $x^2 - y^2 < \ln \ \Omega $, where $\Omega $ is the largest
representable floating point number. Argument reduction in computing sin and
cos causes loss of precision, and so we restrict $2|xy| < \rho ^{-1/2}$,
where $\rho $ is the smallest positive number such that the floating point
representation of $1.0 + \rho $ is different from~1.0. If $y < 0$ and $x^2 -
y^2 \geq \ln \ \Omega $ the error processor (see Chapter~19.2) is invoked
with LEVEL = 2, and IFLAG is set to $-$1. If $y < 0$ and $2|xy| \geq \rho
^{-1/2}$ the error message processor is invoked with LEVEL = 2, and IFLAG is
set to +1. The usual action of the error message processor when invoked with
LEVEL = 2 is to halt execution of the program. This action may be altered by
calling ERMSET (see Chapter~19.2).

\subsection{Supporting Information}

\begin{tabular}{@{\bf}l@{\hspace{5pt}}l}
\bf Entry & \hspace{.35in} {\bf Required Files}\vspace{2pt} \\
CWOFZ & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
AMACH, CWOFZ, ERFIN, ERMOR, ERMSG, SERV1\rule[-5pt]{0pt}{8pt}}\\ZWOFZ & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
AMACH, DERV1, ERFIN, ERMOR, ERMSG, ZWOFZ\rule[-5pt]{0pt}{8pt}}\\\end{tabular}

Present version converted from ACM TOMS Algorithm~680, \cite{Poppe:1990:AEC}, by W.
V. Snyder, 1991.

\begcodenp

\medskip\

\lstset{language=[77]Fortran,showstringspaces=false}
\lstset{xleftmargin=.8in}

\centerline{\bf \large DRCWOFZ}\vspace{5pt}
\lstinputlisting{\codeloc{cwofz}}

\vspace{10pt}\centerline{\bf \large ODCWOFZ}\vspace{5pt}
\lstset{language={}}
\lstinputlisting{\outputloc{cwofz}}
\closegraphsfile
\end{document}