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.
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.
Other sets:
ConditionalSet
,
FuzzyMultiset
,
FuzzySet
,
FuzzyTuple
,
Multiset
,
Set
,
Tuple
set6::Set
-> Interval
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
.
new()
Create a new Interval
object.
Interval$new( lower = -Inf, upper = Inf, type = c("[]", "(]", "[)", "()"), class = "numeric", universe = ExtendedReals$new() )
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.
Interval
s 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.
A new Interval
object.
strprint()
Creates a printable representation of the object.
Interval$strprint(...)
...
ignored, added for consistency.
A character string representing the object.
equals()
Tests if two sets are equal.
Interval$equals(x, all = FALSE)
Two Interval
s are equal if they have the same: class, type, and bounds.
Infix operators can be used for:
Equal | == |
Not equal | != |
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
.
Interval$new(1,5) == Interval$new(1,5) Interval$new(1,5, class = "integer") != Interval$new(1,5,class="numeric")
contains()
Tests to see if x
is contained in the Set.
Interval$contains(x, all = FALSE, bound = FALSE)
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.
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
).
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.
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
isSubset()
Test if one set is a (proper) subset of another
Interval$isSubset(x, proper = FALSE, all = FALSE)
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.
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 | > |
Proper Superset | >= |
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
.
Interval$new(1,3) < Interval$new(1,5) Set$new(1,3) < Interval$new(0,5)
isSubinterval()
Test if one interval is a (proper) subinterval of another
Interval$isSubinterval(x, proper = FALSE, all = FALSE)
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.
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.
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
.
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
clone()
The objects of this class are cloneable with this method.
Interval$clone(deep = FALSE)
deep
Whether to make a deep clone.
# 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)) #> [1] FALSE TRUE i$contains(c(1, 10), bound = TRUE) #> [1] TRUE TRUE # Equivalent Set and Interval Set$new(1:5) == Interval$new(1, 5, class = "integer") #> Called from: FUN(X[[i]], ...) #> debug: if (!testSet(y)) { #> return(FALSE) #> } #> debug: if (testFuzzy(y)) { #> if (!all(y$membership() == 1)) { #> return(FALSE) #> } #> } #> debug: if (testConditionalSet(y)) { #> return(FALSE) #> } else if (testInterval(y)) { #> if (testCountablyFinite(y)) { #> return(all(suppressWarnings(y$elements %in% self$elements & #> self$elements %in% y$elements))) #> } #> else { #> return(FALSE) #> } #> } else if (sum(testEmpty(self), testEmpty(y)) == 1) { #> return(FALSE) #> } else { #> comp <- suppressWarnings(y$.__enclos_env__$private$.str_elements %in% #> private$.str_elements & private$.str_elements %in% y$.__enclos_env__$private$.str_elements) #> return(all(comp)) #> } #> debug: if (testInterval(y)) { #> if (testCountablyFinite(y)) { #> return(all(suppressWarnings(y$elements %in% self$elements & #> self$elements %in% y$elements))) #> } #> else { #> return(FALSE) #> } #> } else if (sum(testEmpty(self), testEmpty(y)) == 1) { #> return(FALSE) #> } else { #> comp <- suppressWarnings(y$.__enclos_env__$private$.str_elements %in% #> private$.str_elements & private$.str_elements %in% y$.__enclos_env__$private$.str_elements) #> return(all(comp)) #> } #> debug: if (testCountablyFinite(y)) { #> return(all(suppressWarnings(y$elements %in% self$elements & #> self$elements %in% y$elements))) #> } else { #> return(FALSE) #> } #> debug: return(all(suppressWarnings(y$elements %in% self$elements & self$elements %in% #> y$elements))) #> [1] TRUE # SpecialSets can provide more efficient implementation Interval$new() == ExtendedReals$new() #> [1] TRUE Interval$new(class = "integer", type = "()") == Integers$new() #> [1] TRUE ## ------------------------------------------------ ## Method `Interval$equals` ## ------------------------------------------------ Interval$new(1,5) == Interval$new(1,5) #> [1] TRUE Interval$new(1,5, class = "integer") != Interval$new(1,5,class="numeric") #> Called from: FUN(X[[i]], ...) #> debug: if (!testSet(y)) { #> return(FALSE) #> } #> debug: if (testFuzzy(y)) { #> if (!all(y$membership() == 1)) { #> return(FALSE) #> } #> } #> debug: if (testConditionalSet(y)) { #> return(FALSE) #> } else if (testInterval(y)) { #> if (testCountablyFinite(y)) { #> return(all(suppressWarnings(y$elements %in% self$elements & #> self$elements %in% y$elements))) #> } #> else { #> return(FALSE) #> } #> } else if (sum(testEmpty(self), testEmpty(y)) == 1) { #> return(FALSE) #> } else { #> comp <- suppressWarnings(y$.__enclos_env__$private$.str_elements %in% #> private$.str_elements & private$.str_elements %in% y$.__enclos_env__$private$.str_elements) #> return(all(comp)) #> } #> debug: if (testInterval(y)) { #> if (testCountablyFinite(y)) { #> return(all(suppressWarnings(y$elements %in% self$elements & #> self$elements %in% y$elements))) #> } #> else { #> return(FALSE) #> } #> } else if (sum(testEmpty(self), testEmpty(y)) == 1) { #> return(FALSE) #> } else { #> comp <- suppressWarnings(y$.__enclos_env__$private$.str_elements %in% #> private$.str_elements & private$.str_elements %in% y$.__enclos_env__$private$.str_elements) #> return(all(comp)) #> } #> debug: if (testCountablyFinite(y)) { #> return(all(suppressWarnings(y$elements %in% self$elements & #> self$elements %in% y$elements))) #> } else { #> return(FALSE) #> } #> debug: return(FALSE) #> [1] TRUE ## ------------------------------------------------ ## Method `Interval$contains` ## ------------------------------------------------ s = Set$new(1:5) # Simplest case s$contains(4) #> [1] TRUE 8 %inset% s #> [1] FALSE # Test if multiple elements lie in the set s$contains(4:6, all = FALSE) #> [1] TRUE TRUE FALSE s$contains(4:6, all = TRUE) #> [1] FALSE # Check if a tuple lies in a Set of higher dimension s2 = s * s s2$contains(Tuple$new(2,1)) #> [1] TRUE c(Tuple$new(2,1), Tuple$new(1,7), 2) %inset% s2 #> [1] TRUE FALSE FALSE ## ------------------------------------------------ ## Method `Interval$isSubset` ## ------------------------------------------------ Interval$new(1,3) < Interval$new(1,5) #> [1] TRUE Set$new(1,3) < Interval$new(0,5) #> [1] TRUE ## ------------------------------------------------ ## Method `Interval$isSubinterval` ## ------------------------------------------------ Interval$new(1,3)$isSubset(Set$new(1,2)) # TRUE #> [1] TRUE Interval$new(1,3)$isSubset(Set$new(2, 1)) # TRUE #> [1] TRUE Interval$new(1,3, class = "integer")$isSubinterval(Set$new(1, 2)) # TRUE #> [1] TRUE Interval$new(1,3)$isSubinterval(Set$new(1, 2)) # FALSE #> [1] FALSE Interval$new(1,3)$isSubinterval(Set$new(2, 1)) # FALSE #> [1] FALSE Reals$new()$isSubset(Integers$new()) # TRUE #> [1] TRUE Reals$new()$isSubinterval(Integers$new()) # FALSE #> [1] FALSE