contfrac (version 1.1-12)

CF: Continued fraction convergents

Description

Returns continued fraction convergent using the modified Lenz's algorithm; function CF() deals with continued fractions and GCF() deals with generalized continued fractions.

Usage

CF(a, finite = FALSE, tol=0)
GCF(a,b, b0=0, finite = FALSE, tol=0)

Arguments

a,b

In function CF(), the elements of a are the partial denominators; in GCF() the elements of a are the partial numerators and the elements of b the partial denominators

finite

Boolean, with default FALSE meaning to iterate Lenz's algorithm until convergence (a warning is given if the sequence has not converged); and TRUE meaning to evaluate the finite continued fraction

b0

In function GCF(), floor of the continued fraction

tol

tolerance, with default 0 silently replaced with .Machine$double.eps

Details

Function CF() treats the first element of its argument as the integer part of the convergent.

Function CF() is a wrapper for GCF(); it includes special dispensation for infinite values (in which case the value of the appropriate finite CF is returned).

The implementation is in C; the real and complex cases are treated separately in the interests of efficiency.

The algorithm terminates when the convergence criterion is achieved irrespective of the value of finite.

References

  • W. H. Press, B. P. Flannery, S. A. Teukolsky, and W. T. Vetterling 1992. Numerical recipes 3rd edition: the art of scientific computing. Cambridge University Press; section 5.2 “Evaluation of continued fractions”

  • W. J. Lenz 1976. Generating Bessel functions in Mie scattering calculations using continued fractions. Applied Optics, 15(3):668-671

See Also

convergents

Examples

Run this code
# NOT RUN {
phi <- (sqrt(5)+1)/2
phi_cf <- CF(rep(1,100))     # phi = [1;1,1,1,1,1,...]
phi - phi_cf     # should be small

# The tan function:
"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

# approximate real numbers with continued fraction:
as_cf(pi)

as_cf(exp(1),25)    # OK up to element 21 (which should be 14)

  # Some convergents of pi:
  jj <- convergents(c(3,7,15,1,292))
  jj$A / jj$B - pi


  # An identity of Euler's:
  jj <- GCF(a=seq(from=2,by=2,len=30), b=seq(from=3,by=2,len=30), b0=1) 
  jj - 1/(exp(0.5)-1)   # should be small

# }

Run the code above in your browser using DataLab