Learn R Programming

sets (version 1.0-16)

interval: Intervals

Description

Interval class for countable and uncountable numeric sets.

Usage

interval(l=NULL, r=l,
         bounds=c("[]", "[)", "(]", "()", "[[", "]]", "][",
                  "open", "closed", "left-open", "right-open",
                  "left-closed", "right-closed"),
         domain=NULL)

reals(l=NULL, r=NULL, bounds=c("[]", "[)", "(]", "()", "[[", "]]", "][", "open", "closed", "left-open", "right-open", "left-closed", "right-closed")) integers(l=NULL, r=NULL) naturals(l=NULL, r=NULL) naturals0(l=NULL, r=NULL) l %..% r

interval_domain(x)

as.interval(x) integers2reals(x, min=-Inf, max=Inf) reals2integers(x)

interval_complement(x, y=NULL) interval_intersection(...) interval_symdiff(...) interval_union(...)

interval_difference(...) interval_division(...) interval_product(...) interval_sum(...)

is.interval(x) interval_contains_element(x, y) interval_is_bounded(x) interval_is_closed(x) interval_is_countable(...) interval_is_degenerate(x) interval_is_empty(x) interval_is_equal(x, y) interval_is_less_than_or_equal(x, y) interval_is_less_than(x, y) interval_is_greater_than_or_equal(x, y) interval_is_greater_than(x, y) interval_is_finite(x) interval_is_half_bounded(x) interval_is_left_bounded(x) interval_is_left_closed(x) interval_is_left_open(...) interval_is_left_unbounded(x) interval_measure(x) interval_is_proper(...) interval_is_proper_subinterval(x, y) interval_is_right_bounded(x) interval_is_right_closed(x) interval_is_right_open(...) interval_is_right_unbounded(x) interval_is_subinterval(x, y) interval_is_unbounded(x) interval_is_uncountable(x) interval_power(x, n) x %<% y="" x="" %="">% y x %<=% y="" x="" %="">=% y

Arguments

x
For as.interval() and is.interval(): an Robject. For all other functions: an interval object (or any other Robject coercible to one).
y
An interval object (or any other Robject coercible to one).
min, max
Integers defining the range to be coerced.
l, r
Numeric values defining the bounds of the interval. For integer domains, these will be rounded.
bounds
Character string specifying whether the interval is open, closed, or left/right-open/closed. Symbolic shortcuts such as "()" or "][" for an open interval, etc., are also accepted.
domain
Character string specifying the domain of the interval: "R", "Z", "N", and "N0" for the reals, integers, positive integers and non-negative integers, respectively. If unspecified, the domain
n
Integer exponent.
...
Interval objects (or other Robjects coercible to interval objects).

Value

  • For the predicates: a logical value. For all other functions: an interval object.

Details

An interval object represents a multi-interval, i.e., a union of disjoint, possibly unbounded (i.e., infinite) ranges of numbers---either the extended reals, or sequences of integers. The usual set operations (union, complement, intersection) and predicates (equality, (proper) inclusion) are implemented. If (numeric) sets and interval objects are mixed, the result will be an interval object. Some basic interval arithmetic operations (addition, subtraction, multiplication, division, power) as well mathematical functions (log, log2, log10, exp, abs, sqrt, trunc, round, floor, ceiling, signif, and the trigonometric functions) are defined. Note that the rounding functions will discretize the interval.

Coercion methods for the as.numeric, as.list, and as.set generics are implemented. reals2integers() discretizes a real multi-interval. integers2reals() returns a multi-interval of corresponding (degenerate) real intervals.

The summary functions min, max, range, sum, mean and prod are implemented and work on the interval bounds.

sets_options() allows to change the style of open bounds according to the ISO 31-11 standard using reversed brackets instead of round parentheses (see examples).

See Also

set and gset for finite (generalized) sets.

Examples

Run this code
#### * general interval constructor

interval(1,5)
interval(1,5, "[)")
interval(1,5, "()")

## ambiguous notation -> use alternative style
sets_options("openbounds", "][")
interval(1,5, "()")
sets_options("openbounds", "()")

interval(1,5, domain = "Z")
interval(1L, 5L)

## degenerate interval
interval(3)

## empty interval
interval()

#### * reals
reals()
reals(1,5)
reals(1,5,"()")
reals(1) ## half-unbounded

## (auto-)complement
!reals(1,5)
interval_complement(reals(1,5), reals(2, Inf))

## combine/c(reals(2,4), reals(3,5))
reals(2,4) | reals(3,5)

## intersection
reals(2,4) & reals(3,5)

## overlapping intervals
reals(2,4) & reals(3,5)
reals(2,4) & reals(4,5,"(]")

## non-overlapping
reals(2,4) & reals(7,8)
reals(2,4) | reals(7,8)
reals(2,4,"[)") | reals(4,5,"(]")

## degenerated cases
reals(2,4) | interval()
c(reals(2,4), set())

reals(2,4) | interval(6)
c(reals(2,4), set(6), 9)

## predicates
interval_is_empty(interval())
interval_is_degenerate(interval(4))
interval_is_bounded(reals(1,2))
interval_is_bounded(reals(1,Inf)) ## !! FALSE, because extended reals
interval_is_half_bounded(reals(1,Inf))
interval_is_left_bounded(reals(1,Inf))
interval_is_right_unbounded(reals(1,Inf))
interval_is_left_closed(reals(1,Inf))
interval_is_right_closed(reals(1,Inf)) ## !! TRUE

reals(1,2) <= reals(1,5)
reals(1,2) < reals(1,2)
reals(1,2) <= reals(1,2,"[)")
reals(1,2,"[)") < reals(1,2)

#### * integers
integers()
naturals()
naturals0()

3 %..% 5
integers(3, 5)
integers(3, 5) | integers(6,9)
integers(3, 5) | integers(7,9)

interval_complement(naturals(), integers())

naturals() <= naturals0()
naturals0() <= integers()

## mix reals and integers
c(reals(2,5), integers(7,9))
interval_complement(reals(2,5), integers())
interval_complement(integers(2,5), reals())

try(interval_complement(integers(), reals()), silent = TRUE)
## infeasible --> error

integers() <= reals()
reals() <= integers()

### interval arithmetic
x <- interval(2,4)
y <- interval(3,6)
x + y
x - y
x * y
x / y

## summary functions
min(x, y)
max(y)
range(y)
mean(y)

Run the code above in your browser using DataLab