\documentclass[twocolumn,a4paper]{revtex4}
\usepackage{graphicx}
\usepackage{listings}
\begin{document}

\title{Implementation of error function using integral representation}
\author{D.V~Fedorov}
\affiliation{Aarhus University}


	\begin{abstract}
The error function and the complimentary error function are implemented
using direct integration of their integral representations with GSL's
Gauss-Kronrod integration rutines.
	\end{abstract}

\maketitle

\section{Introduction}

The error function, $\mathrm{erf}(x)$, and the
complementary error function, $\mathrm{erfc}(x)$, are defined as~\cite{andrews}
	\begin{equation}\label{eq:erf}
\mathrm{erf}(x) \doteq \frac{2}{\sqrt{\pi}}\int_{0}^x e^{-t^2}dt \,,
	\end{equation}
	\begin{equation}\label{eq:erfc}
\mathrm{erfc}(x) \doteq \frac{2}{\sqrt{\pi}}\int_{x}^\infty e^{-t^2}dt
=1-\mathrm{erf}(x) \,.
	\end{equation}
They have the following reflection properties,
	\begin{equation}\label{eq:-erf}
\mathrm{erf}(-x) = -\mathrm{erf}(x) \;,
	\end{equation}
	\begin{equation}\label{eq:-erfc}
\mathrm{erfc}(-x) = 2-\mathrm{erfc}(x) \;.
	\end{equation}

The error function appears---among many other things---in the
electrostatic potential of a spherically symmetric Gaussian charge
disribution.

This report describes the implementation of the error and the complimentary error functions by
direct integration of their integral representations~(\ref{eq:-erf})
and~(\ref{eq:-erfc}). Section~\ref{seq:imp} describes the details of the implementation, section~\ref{seq:res} illustrates
the results, section~\ref{seq:conc} presents the conclusions, and the appendix lists the source code of the implemented
functions.

	\begin{figure}[b]
\centerline{\includegraphics{plot.pdf}}
\caption{The implemented error function {\tt myerf(x)} together with the 
{\tt erf(x)} function from C~standard library.}
\label{fig:erf}
	\end{figure}

\section{Implementation}\label{seq:imp}

First, for negative $x$ the reflection formulae~(\ref{eq:-erf})
and~(\ref{eq:-erfc}) can be used to switch the argument to positive
values.

The definition~(\ref{eq:erf}) of the $\mathrm{erf}(x)$ function represents
an integral over a smooth integrand which can be calculated using the
high precision {\verb|gsl_integration_qag|} routine with 61-points as
long a $x$ is not very large.

For large $x$ one needs to switch to the definition~(\ref{eq:erfc}) of
the $\mathrm{erfc}(x)$ function and use the {\verb|gsl_integration_qagiu|}
routine which calculates integrals over a semi-infitine interval.

Experiments show that the optimal point to switch from~(\ref{eq:erf})
to~(\ref{eq:erfc}) is around $x=3$. Then absolute accuracy of about
$10^{-14}$ is reached with at most 75 evaluations~\cite{log} of the integrand for
any $x\in[-\infty,+\infty]$.

\section{Results}\label{seq:res}

Figure~\ref{fig:erf} shows the implemented {\tt myerf} function compared to the
{\tt erf} function from the C~standard library.
The figure was made with {\tt pyxplot} using the {\tt pdf} terminal.
It is included via the {\tt graphicx} package.

The implemented complementary error function {\tt myerfc} is illustrated
on figure~\ref{fig:erfc} together with the {\tt erfc} function from the
C~standard library.  The figure was made with {\tt gnuplot} using the {\tt latex}
terminal and was directly inlcluded using the \verb|\intput{plot2.tex}|
command.

	\begin{figure}[b]
\centerline{\input{plot2.tex}}
\caption{The implemented complimentary error function {\tt myerfc(x)}
together with the {\tt erfc(x)} function from the standard libraty.}
	\label{fig:erfc} \end{figure}

\section{Conclusion}\label{seq:conc}
The error function and the complimentary error function are implemented
by direct numerical calculation of the defining integrals using
the numerical integration routines {\verb|gsl_integration_qag|} and
{\verb|gsl_integration_qagiu|}. The absolute error of $10^{-14}$ is
achieved for any argument with at most 75 evaluations of the integrands.

\onecolumngrid
\section{Appendix}
\lstinputlisting{myerf.c}

\begin{thebibliography}{9}
\bibitem{andrews}Andrews, Larry C.; Special functions of mathematics for engineers.
\bibitem{log}The log file from the archive of the project.
\end{thebibliography}


\end{document}
