fractional: Representation of a numeric vector in vulgar fractional form

Description Usage Arguments Value Methods (by generic) See Also Examples

View source: R/fractionl.R

Description

The object is flagged so that if it is coerced to character, or printed, the numerical quantities are represented by a rational approximation. In other respects the numerical object behaves as normally.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
fractional(x, eps = 1e-06, maxConv = 20, sync = FALSE)

## S3 method for class 'fractional'
as.character(x, eps = attr(x, "eps"), maxConv = attr(x,
  "maxConv"), ...)

## S3 method for class 'charFrac'
print(x, ...)

## S3 method for class 'fractional'
print(x, ...)

Arguments

x

A numeric object

eps

An absolute error tolerance

maxConv

An upper limit on the number of convergents to use in the continued fractions.

sync

A logical value. Should the numerical value be changed to match the rational approximation, as closely as possible with floating point, (TRUE)? Or, should it be left and used in its original state (FALSE)?

...

Currently ignored.

Value

A numeric object of class "fractional".

Methods (by generic)

See Also

fractions for a similar functionality.

Examples

1
2
3
4
5
6
7
8
9
(M <- solve(cbind(1, contr.helmert(5))))
(Mf <- fractional(M))     ## print method right justifies
(Mc <- as.character(Mf))  ## print method left justifies
(Mn <- numerical(Mc))
set.seed(123)
u <- matrix(runif(10), 2, 5)
(uf <- fractional(u))
(us <- fractional(u, sync = TRUE))  ## may look different!
unfractional(uf) - unfractional(us)  ## rational approximation errors

Example output

               1           2           3     4   5
[1,]  0.20000000  0.20000000  0.20000000  0.20 0.2
[2,] -0.50000000  0.50000000  0.00000000  0.00 0.0
[3,] -0.16666667 -0.16666667  0.33333333  0.00 0.0
[4,] -0.08333333 -0.08333333 -0.08333333  0.25 0.0
[5,] -0.05000000 -0.05000000 -0.05000000 -0.05 0.2
     1     2     3     4     5    
[1,]   1/5   1/5   1/5   1/5   1/5
[2,]  -1/2   1/2     .     .     .
[3,]  -1/6  -1/6   1/3     .     .
[4,] -1/12 -1/12 -1/12   1/4     .
[5,] -1/20 -1/20 -1/20 -1/20   1/5
     1     2     3     4     5  
[1,] 1/5   1/5   1/5   1/5   1/5
[2,] -1/2  1/2   .     .     .  
[3,] -1/6  -1/6  1/3   .     .  
[4,] -1/12 -1/12 -1/12 1/4   .  
[5,] -1/20 -1/20 -1/20 -1/20 1/5
               1           2           3     4   5
[1,]  0.20000000  0.20000000  0.20000000  0.20 0.2
[2,] -0.50000000  0.50000000  0.00000000  0.00 0.0
[3,] -0.16666667 -0.16666667  0.33333333  0.00 0.0
[4,] -0.08333333 -0.08333333 -0.08333333  0.25 0.0
[5,] -0.05000000 -0.05000000 -0.05000000 -0.05 0.2
     [,1]      [,2]      [,3]      [,4]      [,5]     
[1,]  419/1457   164/401 1248/1327  761/1441   461/836
[2,] 1065/1351 2189/2479   61/1339 1236/1385   321/703
     [,1]      [,2]      [,3]      [,4]      [,5]     
[1,]  419/1457   164/401 1248/1327  761/1441   461/836
[2,] 1065/1351 2189/2479   61/1339 1236/1385   321/703
             [,1]          [,2]         [,3]         [,4]          [,5]
[1,] 3.066723e-07 -6.342980e-07 6.500221e-08 5.743048e-09 -3.922327e-07
[2,] 1.761544e-07  5.830102e-08 1.140277e-07 2.718309e-07  2.260574e-07

fractional documentation built on May 2, 2019, 4:16 a.m.