Title: | Continued Fractions |
---|---|
Description: | Various utilities for evaluating continued fractions. |
Authors: | Robin K. S. Hankin [aut, cre] |
Maintainer: | Robin K. S. Hankin <[email protected]> |
License: | GPL-2 |
Version: | 1.1-13 |
Built: | 2024-12-25 05:12:06 UTC |
Source: | https://github.com/robinhankin/contfrac |
Various utilities for evaluating continued fractions.
The DESCRIPTION file:
Package: | contfrac |
Title: | Continued Fractions |
Version: | 1.1-13 |
Authors@R: | person(given=c("Robin", "K. S."), family="Hankin", role = c("aut","cre"), email="[email protected]", comment = c(ORCID = "0000-0001-5982-0415")) |
Description: | Various utilities for evaluating continued fractions. |
Maintainer: | Robin K. S. Hankin <[email protected]> |
License: | GPL-2 |
URL: | https://github.com/RobinHankin/contfrac.git |
Suggests: | testthat (>= 3.0.0) |
Config/testthat/edition: | 3 |
Repository: | https://robinhankin.r-universe.dev |
RemoteUrl: | https://github.com/robinhankin/contfrac |
RemoteRef: | HEAD |
RemoteSha: | ef04e939db5c479e2dd354ed19d419e7260ada35 |
Author: | Robin K. S. Hankin [aut, cre] (<https://orcid.org/0000-0001-5982-0415>) |
Index of help topics:
CF Continued fraction convergents as_cf Approximates a real number in continued fraction form contfrac-package Continued Fractions convergents Partial convergents of continued fractions
Robin K. S. Hankin [aut, cre] (<https://orcid.org/0000-0001-5982-0415>)
## CF() takes an integer sequence and returns the value of its continued fraction: phi <- (sqrt(5)+1)/2 phi_cf <- CF(rep(1,100)) # phi = [1;1,1,1,1,1,...] phi - phi_cf # should be small ## as_cf() takes a real and returns its continued fraction representation: as_cf(phi) as_cf(pi) as_cf(exp(1),25) # OK up to element 21 (which should be 14) ## GCF() is a generalized continued fraction: GCF(a=2:100,b=2:100,b0=1,finite=FALSE) # This due to Euler ## convergents() gives a sequence of partial convergents: convergents(rep(1,10))
## CF() takes an integer sequence and returns the value of its continued fraction: phi <- (sqrt(5)+1)/2 phi_cf <- CF(rep(1,100)) # phi = [1;1,1,1,1,1,...] phi - phi_cf # should be small ## as_cf() takes a real and returns its continued fraction representation: as_cf(phi) as_cf(pi) as_cf(exp(1),25) # OK up to element 21 (which should be 14) ## GCF() is a generalized continued fraction: GCF(a=2:100,b=2:100,b0=1,finite=FALSE) # This due to Euler ## convergents() gives a sequence of partial convergents: convergents(rep(1,10))
Approximates a real number in continued fraction form using a standard simple algorithm
as_cf(x, n = 10)
as_cf(x, n = 10)
x |
real number to be approximated in continued fraction form |
n |
Number of partial denominators to evaluate; see Notes |
Has difficulties with rational values as expected
Robin K. S. Hankin
phi <- (sqrt(5)+1)/2 as_cf(phi,50) # loses it after about 38 iterations ... not bad ... as_cf(pi) # looks about right as_cf(exp(1),20) f <- function(x){CF(as_cf(x,30),TRUE) - x} x <- runif(40) plot(sapply(x,f))
phi <- (sqrt(5)+1)/2 as_cf(phi,50) # loses it after about 38 iterations ... not bad ... as_cf(pi) # looks about right as_cf(exp(1),20) f <- function(x){CF(as_cf(x,30),TRUE) - x} x <- runif(40) plot(sapply(x,f))
Returns continued fraction convergent using the modified Lentz's
algorithm; function CF()
deals with continued fractions and
GCF()
deals with generalized continued fractions.
CF(a, finite = FALSE, tol=0) GCF(a,b, b0=0, finite = FALSE, tol=0)
CF(a, finite = FALSE, tol=0) GCF(a,b, b0=0, finite = FALSE, tol=0)
a , b
|
In function |
finite |
Boolean, with default |
b0 |
In function |
tol |
tolerance, with default |
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
.
Robin K. S. Hankin
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. Lentz 1976. Generating Bessel functions in Mie scattering calculations using continued fractions. Applied Optics, 15(3):668-671
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
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
Partial convergents of continued fractions or generalized continued fractions
convergents(a) gconvergents(a,b, b0 = 0) nconv(a, give=FALSE) ngconv(a, b, b0 = 0, give=FALSE)
convergents(a) gconvergents(a,b, b0 = 0) nconv(a, give=FALSE) ngconv(a, b, b0 = 0, give=FALSE)
a , b
|
In function |
b0 |
The floor of the fraction |
give |
Boolean, with |
Function convergents()
returns partial convergents of the continued fraction
where a
= (note the
off-by-one issue).
Function gconvergents()
returns partial convergents of the continued fraction
where a
=
Functions nconv()
and ngconv()
are convenience wrappers
that return the numerical values of the convergents.
Returns a list of two elements, A
for the numerators and
B
for the denominators
This classical algorithm generates very large partial numerators and denominators.
To evaluate limits, use functions CF()
or GCF()
.
Robin K. S. Hankin
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”
# Successive approximations to pi: jj <- convergents(c(3,7,15,1,292)) jj$A/jj$B - pi # should get smaller convergents(rep(1,10)) nconv(1:6,give=TRUE)
# Successive approximations to pi: jj <- convergents(c(3,7,15,1,292)) jj$A/jj$B - pi # should get smaller convergents(rep(1,10)) nconv(1:6,give=TRUE)