## rmarkdown::tufte_handout library(knitr) opts_chunk$set(comment = "", warning = FALSE, message = FALSE, fig.height = 4.94, fig.width = 8, out.width = "690px", out.height = "426px") oldOpt <- options(scipen = 10, width = 95)

The `MASS`

package contains two small helper functions called `fractions`

and `rational`

. Originally these were included to allow patterns in patterned matrices to be more easily
exhibited and hence, detected. This happens when the entries are expressible in the form
of "vulgar" fractions, i.e. numerator/denominator, rather than as recurring decimals.

For example a Hilbert matrix:

Hilbert <- function(n) { structure(outer(1:n, 1:n, function(r, c) 1/(r+c-1)), dimnames = list(paste0("R",1:n), paste0("C", 1:n))) } H5 <- Hilbert(5) kable(H5, align = "c")

has a pattern much easier to appreciate if the entries are expressed as vulgar fractions:

library(dplyr) library(fractional) H5 %>% fractional %>% as.character %>% kable(align = "r")

This package facilitates the calculation and presentation of numerical quantities as rational approximations.

To be more specific, a rational approximation to a real number is a ratio of two ** integers**,
the

A second vignette, *Theory*, outlines, for the curious reader, the standard method used
to find them, using continued fractions, and some basic mathematical properties. It is
of mathematical interest, but not necessary to use the package itself.

This small package offers merely a way to present numerical values by means of close rational approximations, displayed as vulgar fractions. It does *not* provide exact rational arithmetic. Such a facility would require unlimited precision rational arithmetic, which is well beyond the scope of this package. In fact, integer overflow is a constant limitation in our use of fractions in the present package.

The package provides two main functions: ** fractional** and

`numerical`

Readers interested in the details of the algorithm used and its implementation in `R`

should refer to the *Theory* vignette.

`fractional`

and `unfractional`

`fractional`

is the main function of the package. The argument list is as follows:

function (x, eps = 1e-06, maxConv = 20, sync = FALSE)

A numeric vector, matrix or array.`x`

An absolute error tolerance. The rational approximation is the first convergent for which the absolute error falls below this bound.`eps = 1e-06`

A secondary stopping criterion. An upper limit on the number of convergents (See`maxConv = 20`

*Theory*vignette) to consider.Should the numerical value be "synchronized" with the displayed numerical approximation? See below.`sync = FALSE`

The value returned is a numeric vector like `x`

, but inheriting from class `fractional`

. It may be used in arithmetic operations in the normal way and retains full floating point accuracy, unless `sync = TRUE`

. When printed, or coerced to character, however, the rational approximation is used as the representation, which as a numerical value may not precisely equal the full floating point value.

If `sync = TRUE`

is set, then the numerical value is changed to agree with the numerical value and this agreement is maintained in synchrony, as much as possible with floating point arithmetic, during future numerical operations.

`unfractional`

The function ** unfractional** is a convenience function that strips the class and some additional attributes from an object of class

`"fractional"`

, thus demoting it back to a numerical object like the one from which it was generated.`fractional`

objects in arithmetic operations.Methods are provided for the `S3`

group generic functions `Ops`

and `Math`

, allowing the mathematical operators and simple mathematical functions to be used with them. The normal rules apply. In an expression `e1 + e2`

(where `+`

can be any of the operators) the `fractional`

method is dispatched if *either* `e1`

*or* `e2`

*(or both)* is/are of class `fractional`

, and the result is of class `fractional`

.

For the `Math`

group generic, operations are conducted but treating the objects as a normal numeric object, that is, ignoring the `fractional`

class.

`as.character`

The package provides a method for the primitive generic function `base::as.character`

for objects of class `"fractional"`

. The arguments are as follows:

function (x, eps = attr(x, "eps"), maxConv = attr(x, "maxConv"), ...)

That is, the values for `eps`

and `maxConv`

are carried forward from the original call. This is because it is only when the object is displayed or coerced to character that the computation of the continued fraction is carried out.

The result is a character string object of primary class `charFrac`

. This primary class membership essentially ensures that the object is printed without quotation marks.

`numerators`

and `denominators`

These two `S3`

generic functions will extract the numerators and denominators, respectively.

The argument is a single object, `x`

, and the result is an object of type `integer`

, but carrying attributes such as `dim`

or `dimnames`

from the original.

Methods are provided for objects of `S3`

class `fractional`

or `charFrac`

`numerical`

This is essentially a method function for `base::as.numeric`

providing coercion to numeric for objects of `S3`

class `fractional`

or `charFrac`

.

It is written as an S3 generic function, and the main methods uses `numerators`

and `denominators`

in an obvious way.

The `default`

method passes the object to `base::as.numeric`

.

`rat`

, `ratr`

and `.ratr`

The function `rat`

is a front-end to the `C++`

function that carries out the continued fraction computations. It would normally not be called directly by users.

The functions `ratr`

and `.ratr`

, in combination, provide the identical computation but done in pure `R`

. They are provided for pedagogical and timing purposes only.

`vfractional`

This is a variant on `fractional`

, without the `sync`

argument, but *vectorized* with respect to the three arguments `x`

, `eps`

and `maxConv`

. It is mainly used for examples.

The *Theory* vignette provides many example in context, but the following very elementary examples may give the flavour of how the functions are used.

library(dplyr) (x <- matrix(1:9/12, 3, 3) %>% fractional) (xr <- 1/x) x + 10 (x2 <- x^2) (sx2 <- sqrt(x2)) ## demoted back to numeric by sqrt() fractional(sx2) ## reinstated solve(xr) %>% fractional ## xr is non-singular with a simple inverse numerators(x2) ## numerators and denominators are available denominators(x2)

The *golden ratio* of the ancient Greeks,
$\phi = \left(\sqrt{5}+1\right)/2 = 1.61803398874989490\dots$ is well-known to be the limit of the ratio of successive Fibonacci numbers. As a partial indication of this result, the following demonstration is at least suggestive:

oldWidth <- options(width = 80)

F <- c(1,1,numeric(15)) for(i in 3:17) ## Fibonacci sequence by recurrence F[i] <- F[i-1] + F[i-2] F (phi <- (sqrt(5) + 1)/2)

The function * vfractional* may be used to provide a sequence of optimal^[Optimal in a sense to be described in the

vfractional(phi, eps = 0, maxConv = 1:16)

```
options(oldWidth)
```

With random numbers it is mildly interesting from a theoretical point of view to look at the distribution of the number of convergents in the continued fraction expansion needed to achieve a specific error tolerance. We have the tools to explore this somewhat arcane issue:

library(ggplot2) N <- 500000 set.seed(3210) rat(rnorm(N), eps = 1.0e-07)[, "n"] %>% table(x = .) %>% as.data.frame(responseName = "f") %>% ggplot(.) + aes(x = x, y = f/N) + geom_bar(stat = "identity", fill = "steel blue") + ylab("Relative frequency") + xlab("Convergents")

In particular this appears to justify the choice of `maxConv = 20`

as a suitable default^[The visual aspect ratio of this figure is $y/x \approx 1/\phi$, giving it nearly the ideal proportions according to the ancient Greek aesthetic rules.].

**Any scripts or data that you put into this service are public.**

Embedding an R snippet on your website

Add the following code to your website.

For more information on customizing the embed code, read Embedding Snippets.