Regions combine with | (union), & (intersection), - (difference) and
== (set equality), decided in exact rational arithmetic. Base R's
union(), intersect(), setdiff() and setequal() do not accept regions.
Arguments
- e1, e2
regions.
Value
== and != return TRUE or FALSE. The other operators return a
region: x | y returns the same as union_region(); x & y returns a
union_region of the surviving cells, a single cell alone when it is the
only survivor, or an empty_region() if the intersection is empty; and
x - y likewise returns an empty_region() when nothing remains.
Details
x | y is structural: it forwards to union_region(). x & y intersects
every part of x with every part of y and drops the empty ones; parts of
the result may overlap.
x - y is the closed difference. A part of y that meets x only in a
lower-dimensional slice subtracts nothing and raises a warning. To remove
several regions, subtract their union: x - (y1 | y2). - errors past
getOption("ripr.max_cells", 1000L) cells; the same option sets the default
max_cells for cells() and null_model().
x == y returns a single TRUE or FALSE, testing containment both ways
(exactly, from generators, when the containing side is convex; by a
difference otherwise). It compares sets, not objects (use identical() for
that); regions of different dimensions are unequal.
See also
union_region(), empty_region(), and disjoin(), which turns a
union into one whose parts do not overlap.
Examples
# The two cells of the K = 3 plurality null: the union of the regions where
# candidate 2 beats candidate 1, and where candidate 3 beats candidate 1.
loses_2 <- simplex_region(
vertices = rbind(c(0.5, 0.5, 0), c(0, 1, 0), c(0, 0, 1))
)
loses_3 <- simplex_region(
vertices = rbind(c(0.5, 0, 0.5), c(0, 1, 0), c(0, 0, 1))
)
# The null itself, by operator rather than constructor:
plurality <- loses_2 | loses_3
plurality
#> <union_region>
#> 2 parts, dimension 3
#> simplex_region: 3 vertices in R^3
#> simplex_region: 3 vertices in R^3
# Its two cells meet where candidate 1 trails both others:
loses_2 & loses_3
#> <simplex_region>
#> 3 vertices in R^3
#> facets: 3 inequalities, 1 equality
#> vertices:
#> [,1] [,2] [,3]
#> [1,] 0.0000000 0.0000000 1.0000000
#> [2,] 0.3333333 0.3333333 0.3333333
#> [3,] 0.0000000 1.0000000 0.0000000
# Disjoint regions intersect in nothing:
point_region(theta = c(1, 0, 0)) & point_region(theta = c(0, 1, 0))
#> empty_region: the empty region
# The complement of the null within the simplex is the region where
# candidate 1 wins -- the alternative, as a region:
simplex <- simplex_region(vertices = diag(3))
simplex - plurality
#> <polytope_region>
#> 4 vertices in R^3
#> facets: 4 inequalities, 1 equality
#> vertices:
#> [,1] [,2] [,3]
#> [1,] 0.5000000 0.0000000 0.5000000
#> [2,] 0.3333333 0.3333333 0.3333333
#> [3,] 0.5000000 0.5000000 0.0000000
#> [4,] 1.0000000 0.0000000 0.0000000
# Several regions are subtracted as their union, so this is the same set:
(simplex - (trails_2 | trails_3)) == (simplex - plurality)
#> Error: object 'trails_2' not found
# A square, and the same square cut into two triangles: different objects,
# the same set. One of those triangles alone is not.
square <- polytope_region(vertices = rbind(c(0, 0), c(1, 0), c(1, 1), c(0, 1)))
square == union_region(cells(square))
#> [1] TRUE
square == cells(square)[[1]]
#> [1] FALSE
square != cells(square)[[1]]
#> [1] TRUE
# The cap on a difference's decomposition is an option:
old <- options(ripr.max_cells = 5000L)
simplex - plurality
#> <polytope_region>
#> 4 vertices in R^3
#> facets: 4 inequalities, 1 equality
#> vertices:
#> [,1] [,2] [,3]
#> [1,] 0.5000000 0.0000000 0.5000000
#> [2,] 0.3333333 0.3333333 0.3333333
#> [3,] 0.5000000 0.5000000 0.0000000
#> [4,] 1.0000000 0.0000000 0.0000000
options(old)