set6 is an object-oriented interface for constructing and manipulating mathematical sets using R6. set6 allows a variety of mathematical sets, including Sets, Tuple, Intervals and Fuzzy variants; there are also Conditional sets for creating sets out of complex logical instructions. A full set of tutorials can be found here. In this introductory vignette we briefly demonstrate how to construct a set, access its properties and traits, and some basic algebra of sets.

Before we get started note that by default `set6`

uses unicode for printing representations of sets. Whilst this is preferred for neater printing, some machines, operating systems, or R versions may not behave as expected when printing unicode and therefore we allow the option to turn this off.

`useUnicode(FALSE)`

This vignette does use the unicode representations.

Classes in `set6`

can be split into two groups: set objects, and wrappers. When we refer to sets (lower-case ‘s’) we refer to objects inheriting from the base class `Set`

. All sets in `set6`

inherit from `Set`

, meaning that, at a very minimum, they share the same methods and fields as `Set`

.

The simplest set of all is the empty set, we can see how even this has methods for printing and summarising, as well as a list of properties and traits.

```
= Set$new()
empty
empty#> ∅
summary(empty)
#> Set
#> ∅
#> Traits:
#> Crisp
#> Properties:
#> Empty
#> Cardinality = 0 - countably finite
#> Closed
$properties
empty#> $empty
#> [1] TRUE
#>
#> $singleton
#> [1] FALSE
#>
#> $cardinality
#> [1] 0
#>
#> $countability
#> [1] "countably finite"
#>
#> $closure
#> [1] "closed"
$traits
empty#> $crisp
#> [1] TRUE
```

Sets can contain any object in R that has a valid `as.character`

dispatch method that ensures uniqueness in objects. This is a requirement as internally string representations are used to compare sets.

```
$new(1,2,3)
Set#> {1, 2, 3}
$new(letters[1:5])
Set#> {a, b,...,d, e}
$new(1, 2i, "a", Set$new(1))
Set#> {0+2i, 1, a, {1}}
```

Each set class has its own unique mathematical properties, we will not cover these extensively here but summarise each with a short example.

```
# A Set cannot have duplicated elements, and ordering does not matter
$new(1,2,2,3) == Set$new(3,2,1)
Set#> [1] TRUE
# A Tuple can have duplicated elements, and ordering does matter
$new(1,2,2,3) != Tuple$new(1,2,3)
Tuple#> [1] TRUE
$new(1,3) != Tuple$new(3,1)
Tuple#> [1] TRUE
# An interval can be an interval of integers or numerics, but must be continuous
$new(1, 10, class = "integer")
Interval#> {1,...,10}
$new(1, 10) # numeric is default
Interval#> [1,10]
# `type` is used to specify the interval upper and lower closure
$new(1, 10, type = "()")
Interval#> (1,10)
# SpecialSets are useful for common 'special' mathematical sets
# Use listSpecialSets() to see which are available.
$new()
Reals#> ℝ
$new()
PosIntegers#> ℤ+
# ConditionalSets are user-built sets from logical statements.
# For example, the set of even numbers.
$new(function(x) x %% 2 == 0)
ConditionalSet#> {x ∈ 𝕍 : x%%2 == 0}
# Finally FuzzySets and FuzzyTuples expand Sets and Tuples to allow for partial
# membership of elements. These have two constructors, either by specifying the elements
# and membership alternatively, or by passing these separately to the given arguments.
$new(1, 0.1, 2, 0.2, "a", 0.3) ==
FuzzySet$new(elements = c(1,2,"a"), membership = c(0.1,0.2,0.3))
FuzzySet#> [1] TRUE
```

Every set has methods for comparing it to other sets, as well as for checking which elements are contained within in. Operators are overloaded where possible, and where not other infix operators are defined, these are:

`$isSubset(x, proper = TRUE)`

(`<`

) - Is`x`

a proper subset of`self`

`$isSubset(x, proper = FALSE)`

(`<=`

) - Is`x`

a (non-proper) subset of`self`

`$isSubset(x, proper = TRUE)`

(`>`

) - Is`self`

a proper subset of`x`

`$isSubset(x, proper = FALSE)`

(`>=`

) - Is`self`

a (non-proper) subset of`x`

`$equals(x)`

(`==`

) - Is`x`

(mathematically) equal to`self`

`!($equals(x))`

(`!=`

) - Is`x`

(mathematically) not equal to`self`

`$contains(x)`

(`%inset%`

) - Is`x`

contained in`self`

All methods are vectorized for multiple testing.

```
= Set$new(1,2,3)
s $contains(1)
s#> [1] TRUE
$contains(2, 4)
s#> [1] TRUE
c(2, 4) %inset% s
#> [1] TRUE FALSE
$isSubset(Set$new(1,2,3), proper = FALSE)
s#> [1] TRUE
$isSubset(Set$new(1,2,3), proper = TRUE)
s#> [1] FALSE
c(Set$new(1), Set$new(4, 5)) < s
#> [1] TRUE FALSE
# Sets are FuzzySets with membership = 1
$equals(FuzzySet$new(elements = 1:3, membership = 1))
s#> [1] TRUE
$equals(FuzzySet$new(elements = 1:3, membership = 0.1))
s#> [1] FALSE
== Set$new(1, 2, 3)
s #> [1] TRUE
!= c(Set$new(1,2,3), Set$new(1, 2))
s #> [1] FALSE TRUE
1:10 %inset% ConditionalSet$new(function(x) x %% 2 == 0)
#> [1] FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE
# The `bound` argument in `isSubset` is used for specifying
# how open interval containedness should be checked
= Interval$new(1, 10, type = "(]")
i $contains(Set$new(1), bound = FALSE)
i#> [1] FALSE
$contains(Set$new(10), bound = FALSE)
i#> [1] FALSE
$contains(Set$new(1), bound = TRUE)
i#> [1] FALSE
$contains(Set$new(10), bound = TRUE)
i#> [1] FALSE
```

`set6`

includes the following operations:

`setunion`

(`+`

) - Union of multiple sets`powerset`

- Powerset of a given set`setpower`

(`^`

) - n-ary cartesian product of a given set`setcomplement`

(`-`

) - Relative complement, or set difference, of two sets`setintersect`

(`&`

) - Intersection of two sets`setproduct`

(`*`

) - Cartesian product of multiple sets`setsymdiff`

(`%-%`

) - Symmetric difference of two sets

We will look at the most common of these below.

The union of sets is defined the as the set of elements in all the sets of interest.

```
$new(1) + Set$new(2) + Set$new(3)
Set#> {1, 2, 3}
$new(1, 10) + Set$new(1) # no effect
Interval#> [1,10]
setunion(Set$new(1,2), Interval$new(3, 10), Set$new(16))
#> {1,...,2} ∪ [3,10] ∪ {16,...,16}
$new() + NegReals$new()
PosReals#> ℝ
```

The relative complement of two sets, \(A-B\), is defined as the set of elements in \(A\) but not in \(B\).

```
$new(elements = 1:10) - Set$new(elements = 4:10)
Set#> {1, 2, 3}
$new(1,2,3,4) - Set$new(2)
Set#> {1, 3, 4}
$new() - PosReals$new()
Reals#> ℝ-
$new(5, 10) - Interval$new(3, 12)
Interval#> ∅
$new(5, 10) - Interval$new(7, 12)
Interval#> [5,7)
$new(5, 10) - Interval$new(11, 12) # no effect
Interval#> [5,10]
```

The cartesian product of multiple sets is often confused with the n-ary cartesian product, read the help page at `?setproduct`

for a full description of the problem. Both forms are allowed in `set6`

with the `nest`

argument.

```
$new(1, 2) * Set$new(3, 4)
Set#> {1, 2} × {3, 4}
$new(1, 2) * Set$new(3, 4) * Set$new(5, 6) # n-ary
Set#> {1, 2} × {3, 4} × {5, 6}
# nest = FALSE default - we will return to the `simplify` argument below
setproduct(Set$new(1, 2), Set$new(3, 4), Set$new(5, 6), nest = TRUE, simplify = TRUE)
#> {((1, 3), 5), ((1, 3), 6),...,((2, 4), 5), ((2, 4), 6)}
setproduct(Set$new(1, 2), Set$new(3, 4), Set$new(5, 6), nest = FALSE, simplify = TRUE)
#> {(1, 3, 5), (1, 3, 6),...,(2, 4, 5), (2, 4, 6)}
# n-ary cartesian product on the same set
setpower(Set$new(1,2), 3, simplify = TRUE)
#> {(1, 1, 1), (1, 1, 2),...,(2, 2, 1), (2, 2, 2)}
```

The intersection of two sets is defined as the set of elements that lie in both sets.

```
$new(1,2,3) & Set$new(3,5,6)
Set#> {3}
$new(5,6) & Reals$new()
Set#> {5, 6}
$new(1) & Set$new(2)
Set#> ∅
```

Finally we look briefly at wrappers, and the `simplify`

argument. Each operation has an associated wrapper that will be created if `simplify == FALSE`

or if the resulting set is too complicated to return as a single `Set`

object. Wrappers faciliate lazy evaluation and symbolic representation by providing unique character representations of all sets after operations and do not evaluate the set elements unless specifically requested. In general wrappers should not be directly constructed but instead only used as the result of operations. The operations concerned with products, i.e. `setproduct`

, `setpower`

, `powerset`

, all have `simplify == FALSE`

as the default; whereas the others concerned with unions and differences, have `simplify == TRUE`

as the default.

```
# default: simplify = TRUE
setunion(Set$new(1,2,3), Set$new(4,5))
#> {1, 2,...,4, 5}
setunion(Set$new(1,2,3), Set$new(4,5), simplify = FALSE)
#> {1, 2, 3} ∪ {4, 5}
# default: simplify = FALSE
setproduct(Set$new(1,2), Set$new(4,5))
#> {1, 2} × {4, 5}
setproduct(Set$new(1,2), Set$new(4,5), simplify = TRUE)
#> {(1, 4), (1, 5), (2, 4), (2, 5)}
# default: simplify = FALSE
powerset(Set$new(1,2,3))
#> ℘({1, 2, 3})
powerset(Set$new(1,2,3), simplify = TRUE)
#> {{1, 2, 3}, {1, 2},...,{3}, ∅}
```

All wrappers inherit from `Set`

and therefore share the same methods and fields.

```
= setunion(Set$new(1,2,3), Set$new(4,5), simplify = FALSE)
u c(2,5,8) %inset% u
#> [1] TRUE TRUE FALSE
= Set$new(1,2) * Set$new(3,4)
p $contains(Tuple$new(2, 4))
p#> [1] TRUE
```

`set6`

is still relatively slow compared to other sets packages and most short-term updates will focus on improving bottlenecks.

See the website for more tutorials and follow/star on GitHub for updates.