-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d074f5b
commit dadb295
Showing
15 changed files
with
394 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
* Rev 1.0 Initial Release | ||
* Rev 1.0 Initial Release | ||
* Rev 1.1 Improved input validation, added convergents to output, added Pell's eqn. Minor bugfixes | ||
* Rev 1.2 Fixed bug; improved capabilities of 'cfrac2num' function |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
Package: contFracR | ||
Type: Package | ||
Title: Continued Fraction Generators and Evaluators | ||
Version: 1.0 | ||
Date: 2022-03-24 | ||
Version: 1.2 | ||
Date: 2022-11-19 | ||
Author: Carl Witthoft | ||
Maintainer: Carl Witthoft <carl@witthoft.com> | ||
Description: This library provides functions to convert numbers to continued fractions and back again. | ||
Maintainer: Carl Witthoft <cellocgw@gmail.com> | ||
Description: Converts numbers to continued fractions and back again. A solver for Pell's Equation is provided. The method for calculating roots in continued fraction form is provided without published attribution in such places as Professor Emeritus Jonathan Lubin, <http://www.math.brown.edu/jlubin/> and his post to StackOverflow, <https://math.stackexchange.com/questions/2215918> , or Professor Ron Knott, e.g., <https://r-knott.surrey.ac.uk/Fibonacci/cfINTRO.html> . | ||
License: LGPL-3 | ||
Imports: Rmpfr, go2bigq, gmp, methods | ||
NeedsCompilation: no | ||
Packaged: 2022-04-18 20:04:24 UTC; cgw | ||
Packaged: 2022-12-03 02:53:11 UTC; cgw | ||
Repository: CRAN | ||
Date/Publication: 2022-04-20 08:32:29 UTC | ||
Date/Publication: 2022-12-03 08:00:02 UTC |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,21 @@ | ||
205f1ac10db99343d77276c80e7ea1d7 *ChangeLog | ||
0b7f30f752f4b6ee51abca4e7514416b *DESCRIPTION | ||
d76b58ff2e101fba83cafc3fa4c11df7 *NAMESPACE | ||
a17a81180a3185baf4ca4f358a51901a *R/cfrac.R | ||
d33af6dae1cb708a7fcb8f252a71ab6e *R/fracHelpers.r | ||
f25365c7d5ff3826445f4f4808f4505c *R/root2cfrac.R | ||
19bef864ab509e4ae4da8d994df3ff57 *R/sqrt2periodicCfrac.r | ||
5e990ff1b6de9755c37fd4676fd3fde9 *R/transcNumFracs.r | ||
2539525118cfbb1684063ac454c3df22 *ChangeLog | ||
65a4bf981f90c6a9dae2930f3222b66f *DESCRIPTION | ||
dbef16da3dfd743c2445cd2403e250da *NAMESPACE | ||
4d66295c10a4f33a8a3ef1932466f40d *R/cfrac.R | ||
59dcaee4d048da1279313a8ba9f9ddbb *R/fracHelpers.r | ||
dcaf23bc27f8b1fb2c25da92e191bdd0 *R/pell.r | ||
797e04a9c8753dd2a21ae9efabee417d *R/root2cfrac.R | ||
cee3cdb0dde176e55679e4aeaf0475b4 *R/sqrt2periodicCfrac.R | ||
6a955e3ded07a477db5a392e34f2ea78 *R/transcNumFracs.r | ||
87d945ce1990b788fce1cddc01cad3c0 *man/cfrac2latex.Rd | ||
92bbe514e6a99798fb77908b345913a9 *man/cfrac2num.Rd | ||
38104a7f2a9224c9e0fa22c7a7981051 *man/cfrac2num.Rd | ||
527d800f2060fb05f28065bfa7e525ed *man/cfrac2simple.Rd | ||
8e0d93bc392fa417507d9b5fd1db9715 *man/contFracR-package.Rd | ||
8bbd5e8f2ed5553392e5e7c681485345 *man/e2cfrac.Rd | ||
340dfad89cdbe601bb622111c4bb73d2 *man/num2cfrac.Rd | ||
f2d1775ec692815f18733fdaa512ff86 *man/num2cfrac.Rd | ||
b638a525538b6e45ff483ed8dedbb333 *man/pell.Rd | ||
2f2918ec98dbf29f9e16e32c9a7f7ecd *man/phi2cfrac.Rd | ||
fa38fde739e78ae14ddc8e8860c3ea4c *man/pi2cfrac.Rd | ||
69d16dc2fb911b0eaafa0cb1b09eebd2 *man/recipCfrac.Rd | ||
5fa33bd50c47a0250a0de7163dad1b8d *man/root2cfrac.Rd | ||
5633e3bb4f104ff6d7a4ffcc4ce0a0d0 *man/sqrt2periodicCfrac.Rd | ||
f3df8f21ce4c5d7d3d9670af365ca004 *man/sqrt2periodicCfrac.Rd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
exportPattern("^[[:alpha:]]+") | ||
importFrom("gmp", "as.bigz", "as.bigq","pow.bigq", "is.bigz", "numerator" , "denominator") | ||
importFrom("Rmpfr", "mpfr", ".bigq2mpfr", ".mpfr2bigz") | ||
importFrom("Rmpfr", "mpfr", ".bigq2mpfr", ".mpfr2bigz", ".bigz2mpfr") | ||
importFrom("go2bigq", "go2bigq") | ||
importFrom("methods", "is") | ||
importFrom("utils", "tail") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# pell x^2 - d * y^2 = 1 , d a non-square integer | ||
#wiki info: | ||
# Let h_i / k_i denote the sequence of convergents to the regular continued fraction for sqrt(n). This sequence is unique. Then the pair solving Pell's equation and minimizing x satisfies x1 = hi and y1 = ki for some i. This pair is called the fundamental solution. Thus, the fundamental solution may be found by performing the continued fraction expansion and testing each successive convergent until a solution to Pell's equation is found. | ||
# h_i are nums, k_i denoms of each successive layer in contfrac | ||
|
||
# test example for d ==7 | ||
# The sequence of convergents for the square root of seven are | ||
# h/k (convergent) h^2 7k^2 (Pell-type approximation) | ||
# 2/1 3 | ||
# 3/1 +2 | ||
# 5/2 3 | ||
# 8/3 +1 | ||
# so 8 / 3 is the one we want | ||
|
||
|
||
# rev after 1.2: more input validation, done better | ||
# generate more convergents if no "win" found. | ||
# allow A,B inputs to continue a previous run | ||
# make sure $repeated == TRUE before running with the results of | ||
# sqrt2periodicCfrac | ||
|
||
pell <- function (d , maxrep = 10, A = NULL, B = NULL){ | ||
# wins <- NULL # NO! gmp really hates NULL things | ||
wins <- as.bigz( matrix(0, nrow= 1 ,ncol = 2)) | ||
d <- as.bigz(d[1]) | ||
# get the greatest square less than x; make sure mpfr has reasonable precision | ||
dm <- .bigz2mpfr(d, max(5 * ceiling(log10(d)), 500)) | ||
#validate - check that sqrt(d) is not an integer | ||
if (floor(sqrt(dm)) == sqrt(dm)) { | ||
stop('Pell is pointless when d is a square integer') | ||
} | ||
foo <- sqrt2periodicCfrac(d) | ||
# check for success | ||
nterm = 100 # default is 50 | ||
while (!foo$repeated) { | ||
foo <- sqrt2periodicCfrac(d, nterms = nterm) | ||
nterm = nterm *2 | ||
} | ||
repden <- foo$denom[-1] | ||
replen <- length(repden) | ||
jrep = 0 | ||
if (is.null(A)) { | ||
# build initial stuff | ||
# conv{A,B}[1] is the "starter" value A[-1], so ignore | ||
for (jc in 2:length(foo$convA)) { | ||
if (foo$convA[jc]^2 -d * foo$convB[jc]^2 -1 == 0) { | ||
wins <- rbind(wins,c(foo$convA[jc],foo$convB[jc]) ) | ||
} | ||
} | ||
jconv = jc+1 | ||
A = foo$convA | ||
B = foo$convB | ||
# end of if isnull convA | ||
} else { | ||
A <- A | ||
B <- B | ||
jconv = length(A) + 1 | ||
} | ||
# (wins < 4 because of dummy first row) | ||
|
||
while(length(wins ) < 4 && jconv <= maxrep * replen) { | ||
# need more convergents. use the repeated section of foo$denom | ||
A[jconv] <- repden[jrep + 1] *A[jconv-1] + 1 * A[jconv-2] | ||
B[jconv] <- repden[jrep + 1] * B[jconv-1] + 1 * B[jconv-2] | ||
if (A[jconv]^2 - d * B[jconv]^2 -1 == 0) wins <- rbind(wins,c(A[jconv],B[jconv]) ) | ||
# increment jrep mod(length(repden)) | ||
jrep <- (jrep+1) %% replen | ||
jconv = jconv + 1 | ||
} | ||
return(invisible(list(d = d, wins=wins[-1,], cfracdata = foo, A= A, B= B))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.