rational-class: Rational Number Classes

Description Usage Arguments Value Fields Slots Note See Also Examples

Description

An S3, S4, and R6 class for a rational number

The classes are designed to be used in a similar way to integers and numerics in R.

generator rational number of class rationalS3, rationalS4, and rationalR6. Each type of class has advantages and disadvantages in performance and flexibility

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
rational(n, d, method = "R6")

## S4 method for signature 'rationalS4'
length(x)

## S3 method for class 'rationalS3'
length(x, ...)

## S3 method for class 'rationalR6'
length(x, ...)

## S4 method for signature 'rationalS4'
x[i, j, ..., drop = TRUE]

## S4 replacement method for signature 'rationalS4'
x[i, j, ...] <- value

## S4 method for signature 'rationalS4'
x[[i, ..., drop]]

## S4 replacement method for signature 'rationalS4'
x[[i, j, ...]] <- value

## S3 method for class 'rationalS3'
x[i, ..., drop = TRUE]

## S3 replacement method for class 'rationalS3'
x[i, ...] <- value

## S3 method for class 'rationalS3'
x[[i, ..., exact = TRUE]]

## S3 replacement method for class 'rationalS3'
x[[i, ...]] <- value

## S3 method for class 'rationalR6'
x[i, ..., drop = TRUE]

## S3 method for class 'rationalR6'
x[[i, ..., exact = TRUE]]

## S4 method for signature 'rationalS4'
print(x)

## S4 method for signature 'rationalS4'
show(object)

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

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

Arguments

n

the numerator

d

the denominator

method

a length = 1 character vector. One of "R6" (default), "S3", "S4"

x

the rational number

...

indices specifying elements to extract or replace. Indices are numeric or character vectors or empty (missing) or NULL.

i

index specifying elements

j

index specifying elements

drop

For matrices and arrays. If TRUE the result is coerced to the lowest possible dimension (see the examples). This only works for extracting elements, not for the replacement. See drop for further details.

value

the replacement value

exact

controls partial matching when extracting by character

object

the object to show

Value

the desired instance of the rational class

Fields

n,d,v

the numerator, denominator, and value field of the S3 class

Slots

n

the numerator of the S4 class

d

the denominator of the S4 class

v

the numeric value of the S4 class

Note

note that Inf, NA, NaN, and NULL all fail on is.integer() and are not permitted

See Also

Extract for more full descriptions

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 a <- rational(1L, 3L, method="S3")
 stopifnot(a$n == 1L && a$d == 3L && abs(a$v - 1/3) < 1E-12)
 stopifnot(class(a) == "rationalS3")
 b <- rational(2L, 5L, method="S4")
 stopifnot(b@n == 2L && b@d == 5L && abs(b@v - 2/5) < 1E-12)
 stopifnot(class(b) == "rationalS4" && isS4(b) && is(b, "rationalS4"))
 d <- rational(3L, 7L, method="R6")
 stopifnot(d$getNumerator() == 3L && d$getDenominator() == 7L && abs(d$getValue() - 3/7) < 1E-12)
 stopifnot(class(d)[1] == "rationalR6" && is(d, "rationalR6") && is(d, "R6"))
  a <- rational(c(3L, 5L, 6L), c(4L, 5L, 7L), "S4")
  stopifnot(length(a) == 3)
  a <- rational(c(3L, 5L, 6L), c(4L, 5L, 7L), "S3")
  stopifnot(length(a) == 3)
  a <- rational(c(3L, 5L, 6L), c(4L, 5L, 7L), "R6")
  stopifnot(length(a) == 3)
  a <- rational(c(3L, 5L, 6L), c(4L, 5L, 7L), "S4")
  stopifnot(a[2]@n == 5L)
  stopifnot(all(a[2:3]@n == c(5,6)))
  a <- rational(c(3L, 5L, 6L), c(4L, 5L, 7L), "S3")
  stopifnot(a[2]$n == 5L)
  stopifnot(all(a[2:3]$n == c(5,6)))
  a <- rational(c(3L, 5L, 6L), c(4L, 5L, 7L), "R6")
  stopifnot(a[2]$getNumerator() == 5L)
  stopifnot(all(a[2:3]$n == c(5,6)))

bertcarnell/rational documentation built on May 10, 2021, 8:32 p.m.