\documentclass[nojss]{jss}  
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{subfig}
\usepackage{wrapfig}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% declarations for jss.cls %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%% just as usual
\author{Robin K. S. Hankin}
\title{Continued fractions in \proglang{R}: introducing the \pkg{contfrac} package}


%\VignetteIndexEntry{A vignette for the contfrac package}
%% for pretty printing and a nice hypersummary also set:
%% \Plainauthor{Achim Zeileis, Second Author} %% comma-separated

%% for pretty printing and a nice hypersummary also set:
\Plainauthor{Robin K. S. Hankin}

\Plaintitle{Continued fractions in {R}: introducing the {contfrac} package}
\Shorttitle{The \pkg{contfrac} package}

%% an abstract and keywords
\Abstract{

  Here I introduce the \pkg{contfrac} package, for manipulating real
  or complex continued fractions.  IEEE arithmetic is used and the
  limitations of finite-precision calculation are discussed.
}

%% publication information
%% NOTE: This needs to filled out ONLY IF THE PAPER WAS ACCEPTED.
%% If it was not (yet) accepted, leave them commented.
\Volume{0}
\Issue{0}
%% \Month{MONTH}
\Year{0000}
\Submitdate{0000-00-00}
\Acceptdate{0000-00-00}
%% \Repository{https://github.com/RobinHankin/contfrac} %% this line for Tragula

\Keywords{Continued fractions}


\Address{
Robin K. S. Hankin\\%\orcid{https://orcid.org/0000-0001-5982-0415}\\
University of Stirling\\
Scotland\\
E-mail: \email{hankin.robin@gmail.com}
}

%% need no \usepackage{Sweave.sty}

\begin{document}

\textbf{This document has not been peer-reviewed and is not an
  accepted \textsf{Tragula} publication.  It is intended as an example
  of the style and substance appropriate for submission to
  \textsf{Tragula}.}

\section{Overview}

\setlength{\intextsep}{0pt}
\begin{wrapfigure}{r}{0.2\textwidth}
\begin{center}
\includegraphics[width=1in]{\Sexpr{system.file("help/figures/logo.png",package="contfrac")}}
\end{center}
\end{wrapfigure}


A {\em continued fraction} is an expression of the form

\begin{equation}
a_0+\cfrac{1}{a_1+\cfrac{1}{a_2+\cfrac{1}{a_3+\ddots}}}
\end{equation}

provided that the sequence
\begin{equation}
f_0=a_0, \qquad
f_1=a_0+\frac{1}{a_1}, \qquad
f_2=a_0 + \frac{1}{a_1+\frac{1}{a_2}},
\ldots
\end{equation}

\noindent converges.  This is written
$\left[a_0;a_1,a_2,\ldots\right]$ for convenience.  A {\em generalized
  continued fraction} is of the form

\begin{equation}
b_0+\cfrac{a_1}{b_1+\cfrac{a_2}{b_2+\cfrac{a_3}{b_3+\ddots}}}
\end{equation}

but for reasons of typographical convenience this would usually be
written

\begin{equation}
b_0+
\frac{a_1}{b_1+}\,
\frac{a_2}{b_2+}\,
\frac{a_3}{b_3+}\,
\frac{a_4}{b_4+\cdots}.
\end{equation}

Continued fractions are an important branch of
mathematics~\citep{lorentzen} and are useful in a range of numerical
disciplines~\citep{hankin2015}, and I will give some numerical
examples here.

Continued fractions furnish, in a well-defined sense, the ``best
possible'' rational approximations to a given real
number~\citep{hall_and_knight}.  For example, we may take the
continued fraction

\begin{equation}
  \pi=3+\frac{1}{7+}\,
\frac{1}{15+}\,
\frac{1}{1+}\,
\frac{1}{291+}\,
\frac{1}{1+\cdots}
\end{equation}

to deduce that $\frac{22}{7}$ and $\frac{355}{113}$ are close to
$\pi$, being the second and fourth convergents respectively.  The
\proglang{R} idiom for this would use \code{as_cf()} which uses
numerical methods to calculate the first few terms:

<<>>=
library("contfrac")
as_cf(pi, n=7)  # calculate the first 7 terms
@ 

We can expand the first few terms of the series and verify that the
series does converge to $\pi$ with function
\code{convergents()}:


<<>>=
(jj <- convergents(as_cf(pi,n=7)))
jj$A/jj$B - pi
@ 

(the signs alternate and decrease in absolute value).  Because the
package uses standard IEEE precision, the continued fraction expansion
for any non-rational number will eventually succumb to rounding error.
We may investigate this using quadratic surds whose expansions are
repeating patterns; the most famous would be that of
$\phi=\frac{1+\sqrt{5}}{2}=\left[1;1,1,1,1,\ldots\right]$:

<<>>=
(phi <- (1+sqrt(5))/2)
as_cf(phi,n=50)
rle(as_cf(phi,n=50))
@ 

so in this case we can see that the first 38 terms are accurate.

\subsection*{Generalized continued fractions}

The package provides functionality for generalized continued
fractions, which give alternatives to Taylor or Maclaurin series for
many functions.  For example, we have

\begin{equation}
\tan(z)=
\frac{z}{1-}\,
\frac{z^2}{3-}\,
\frac{z^2}{5-}\,
\frac{z^2}{7-}\,
\frac{z^2}{9-}\cdots\qquad z/\pi+1/2\not\in\mathbb{Z}
\end{equation}

and we can show this series in \proglang{R} as follows:

<<>>=
tan_cf <- function(z,n=20){GCF(c(z,rep(-z^2,n-1)),seq(from=1,by=2,len=n))}
z <- 1+1i
tan(z) - tan_cf(z) # should be small
@ 

\bibliography{contfrac}
\end{document}
