Learn R Programming

set6 (version 0.2.4)

Interval: Mathematical Finite or Infinite Interval

Description

A general Interval object for mathematical intervals, inheriting from Set. Intervals may be open, closed, or half-open; as well as bounded above, below, or not at all.

Arguments

Super class

set6::Set -> Interval

Active bindings

length

If the Interval is countably finite then returns the number of elements in the Interval, otherwise Inf. See the cardinality property for the type of infinity.

elements

If the Interval is finite then returns all elements in the Interval, otherwise NA.

Methods

Public methods

Method new()

Create a new Interval object.

Usage

Interval$new(
  lower = -Inf,
  upper = Inf,
  type = c("[]", "(]", "[)", "()"),
  class = "numeric",
  universe = ExtendedReals$new()
)

Arguments

lower

numeric. Lower limit of the interval.

upper

numeric. Upper limit of the interval.

type

character. One of: '()', '(]', '[)', '[]', which specifies if interval is open, left-open, right-open, or closed.

class

character. One of: 'numeric', 'integer', which specifies if interval is over the Reals or Integers.

universe

Set. Universe that the interval lives in, default Reals.

Details

Intervals are constructed by specifying the Interval limits, the boundary type, the class, and the possible universe. The universe differs from class as it is primarily used for the setcomplement method. Whereas class specifies if the interval takes integers or numerics, the universe specifies what range the interval could take.

Returns

A new Interval object.

Method strprint()

Creates a printable representation of the object.

Usage

Interval$strprint(...)

Arguments

...

ignored, added for consistency.

Returns

A character string representing the object.

Method equals()

Tests if two sets are equal.

Usage

Interval$equals(x, all = FALSE)

Arguments

x

Set or vector of Sets.

all

logical. If FALSE tests each x separately. Otherwise returns TRUE only if all x pass test.

Details

Two Intervals are equal if they have the same: class, type, and bounds. Infix operators can be used for:

Equal ==
Not equal !=

Returns

If all is TRUE then returns TRUE if all x are equal to the Set, otherwise FALSE. If all is FALSE then returns a vector of logicals corresponding to each individual element of x.

Examples

Interval$new(1,5) == Interval$new(1,5)
Interval$new(1,5, class = "integer") != Interval$new(1,5,class="numeric")

Method contains()

Tests to see if x is contained in the Set.

Usage

Interval$contains(x, all = FALSE, bound = FALSE)

Arguments

x

any. Object or vector of objects to test.

all

logical. If FALSE tests each x separately. Otherwise returns TRUE only if all x pass test.

bound

logical.

Details

x can be of any type, including a Set itself. x should be a tuple if checking to see if it lies within a set of dimension greater than one. To test for multiple x at the same time, then provide these as a list.

If all = TRUE then returns TRUE if all x are contained in the Set, otherwise returns a vector of logicals. For Intervals, bound is used to specify if elements lying on the (possibly open) boundary of the interval are considered contained (bound = TRUE) or not (bound = FALSE).

Returns

If all is TRUE then returns TRUE if all elements of x are contained in the Set, otherwise FALSE. If all is FALSE then returns a vector of logicals corresponding to each individual element of x.

The infix operator %inset% is available to test if x is an element in the Set, see examples.

Examples

s = Set$new(1:5)

# Simplest case s$contains(4) 8 %inset% s

# Test if multiple elements lie in the set s$contains(4:6, all = FALSE) s$contains(4:6, all = TRUE)

# Check if a tuple lies in a Set of higher dimension s2 = s * s s2$contains(Tuple$new(2,1)) c(Tuple$new(2,1), Tuple$new(1,7), 2) %inset% s2

Method isSubset()

Test if one set is a (proper) subset of another

Usage

Interval$isSubset(x, proper = FALSE, all = FALSE)

Arguments

x

any. Object or vector of objects to test.

proper

logical. If TRUE tests for proper subsets.

all

logical. If FALSE tests each x separately. Otherwise returns TRUE only if all x pass test.

Details

If using the method directly, and not via one of the operators then the additional boolean argument proper can be used to specify testing of subsets or proper subsets. A Set is a proper subset of another if it is fully contained by the other Set (i.e. not equal to) whereas a Set is a (non-proper) subset if it is fully contained by, or equal to, the other Set.

When calling isSubset on objects inheriting from Interval, the method treats the interval as if it is a Set, i.e. ordering and class are ignored. Use isSubinterval to test if one interval is a subinterval of another.

Infix operators can be used for:

Subset <
Proper Subset <=
Superset >

Returns

If all is TRUE then returns TRUE if all x are subsets of the Set, otherwise FALSE. If all is FALSE then returns a vector of logicals corresponding to each individual element of x.

Examples

Interval$new(1,3) < Interval$new(1,5)
Set$new(1,3) < Interval$new(0,5)

Method isSubinterval()

Test if one interval is a (proper) subinterval of another

Usage

Interval$isSubinterval(x, proper = FALSE, all = FALSE)

Arguments

x

Set or list

proper

If TRUE then tests if x is a proper subinterval (i.e. subinterval and not equal to) of self, otherwise FALSE tests if x is a (non-proper) subinterval.

all

If TRUE then returns TRUE if all x are subintervals, otherwise returns a vector of logicals.

Details

If x is a Set then will be coerced to an Interval if possible. $isSubinterval differs from $isSubset in that ordering and class are respected in $isSubinterval. See examples for a clearer illustration of the difference.

Returns

If all is TRUE then returns TRUE if all x are subsets of the Set, otherwise FALSE. If all is FALSE then returns a vector of logicals corresponding to each individual element of x.

Examples

Interval$new(1,3)$isSubset(Set$new(1,2)) # TRUE
Interval$new(1,3)$isSubset(Set$new(2, 1)) # TRUE
Interval$new(1,3, class = "integer")$isSubinterval(Set$new(1, 2)) # TRUE
Interval$new(1,3)$isSubinterval(Set$new(1, 2)) # FALSE
Interval$new(1,3)$isSubinterval(Set$new(2, 1)) # FALSE

Reals$new()$isSubset(Integers$new()) # TRUE Reals$new()$isSubinterval(Integers$new()) # FALSE

Method clone()

The objects of this class are cloneable with this method.

Usage

Interval$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Details

The Interval class can be used for finite or infinite intervals, but often Sets will be preferred for integer intervals over a finite continuous range.

See Also

Other sets: ConditionalSet, FuzzyMultiset, FuzzySet, FuzzyTuple, Multiset, Set, Tuple

Examples

Run this code
# NOT RUN {
# Set of Reals
Interval$new()

# Set of Integers
Interval$new(class = "integer")

# Half-open interval
i <- Interval$new(1, 10, "(]")
i$contains(c(1, 10))
i$contains(c(1, 10), bound = TRUE)

# Equivalent Set and Interval
Set$new(1:5) == Interval$new(1, 5, class = "integer")

# SpecialSets can provide more efficient implementation
Interval$new() == ExtendedReals$new()
Interval$new(class = "integer", type = "()") == Integers$new()

## ------------------------------------------------
## Method `Interval$equals`
## ------------------------------------------------

Interval$new(1,5) == Interval$new(1,5)
Interval$new(1,5, class = "integer") != Interval$new(1,5,class="numeric")

## ------------------------------------------------
## Method `Interval$contains`
## ------------------------------------------------

s = Set$new(1:5)

# Simplest case
s$contains(4)
8 %inset% s

# Test if multiple elements lie in the set
s$contains(4:6, all = FALSE)
s$contains(4:6, all = TRUE)

# Check if a tuple lies in a Set of higher dimension
s2 = s * s
s2$contains(Tuple$new(2,1))
c(Tuple$new(2,1), Tuple$new(1,7), 2) %inset% s2

## ------------------------------------------------
## Method `Interval$isSubset`
## ------------------------------------------------

Interval$new(1,3) < Interval$new(1,5)
Set$new(1,3) < Interval$new(0,5)

## ------------------------------------------------
## Method `Interval$isSubinterval`
## ------------------------------------------------

Interval$new(1,3)$isSubset(Set$new(1,2)) # TRUE
Interval$new(1,3)$isSubset(Set$new(2, 1)) # TRUE
Interval$new(1,3, class = "integer")$isSubinterval(Set$new(1, 2)) # TRUE
Interval$new(1,3)$isSubinterval(Set$new(1, 2)) # FALSE
Interval$new(1,3)$isSubinterval(Set$new(2, 1)) # FALSE

Reals$new()$isSubset(Integers$new()) # TRUE
Reals$new()$isSubinterval(Integers$new()) # FALSE
# }

Run the code above in your browser using DataLab