Learn R Programming

set6 (version 0.2.4)

Set: Mathematical Set

Description

A general Set object for mathematical sets. This also serves as the parent class to intervals, tuples, and fuzzy variants.

Arguments

Active bindings

properties

Returns an object of class Properties, which lists the properties of the Set. Set properties include:

  • empty - is the Set empty or does it contain elements?

  • singleton - is the Set a singleton? i.e. Does it contain only one element?

  • cardinality - number of elements in the Set

  • countability - One of: countably finite, countably infinite, uncountable

  • closure - One of: closed, open, half-open

traits

List the traits of the Set. Set traits include:

  • crisp - is the Set crisp or fuzzy?

type

Returns the type of the Set. One of: (), (], [), [], {}

max

If the Set consists of numerics only then returns the maximum element in the Set. For open or half-open sets, then the maximum is defined by $$upper - 1e-15$$

min

If the Set consists of numerics only then returns the minimum element in the Set. For open or half-open sets, then the minimum is defined by $$lower + 1e-15$$

upper

If the Set consists of numerics only then returns the upper bound of the Set.

lower

If the Set consists of numerics only then returns the lower bound of the Set.

class

If all elements in the Set are the same class then returns that class, otherwise "ANY".

elements

If the Set is finite then returns all elements in the Set as a list, otherwise "NA".

universe

Returns the universe of the Set, i.e. the set of values that can be added to the Set.

range

If the Set consists of numerics only then returns the range of the Set defined by $$upper - lower$$

length

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

Methods

Public methods

Method new()

Create a new Set object.

Usage

Set$new(..., universe = Universal$new(), elements = NULL, class = NULL)

Arguments

...

ANY Elements can be of any class except list, as long as there is a unique as.character coercion method available.

universe

Set. Universe that the Set lives in, i.e. elements that could be added to the Set. Default is Universal.

elements

list. Alternative constructor that may be more efficient if passing objects of multiple classes.

class

character. Optional string naming a class that if supplied gives the set the typed property. All elements will be coerced to this class and therefore there must be a coercion method to this class available.

Returns

A new Set object.

Method print()

Prints a symbolic representation of the Set.

Usage

Set$print(n = 2)

Arguments

n

numeric. Number of elements to display on either side of ellipsis when printing.

Details

The function useUnicode() can be used to determine if unicode should be used when printing the Set. Internally print first calls strprint to create a printable representation of the Set.

Method strprint()

Creates a printable representation of the object.

Usage

Set$strprint(n = 2)

Arguments

n

numeric. Number of elements to display on either side of ellipsis when printing.

Returns

A character string representing the object.

Method summary()

Summarises the Set.

Usage

Set$summary(n = 2)

Arguments

n

numeric. Number of elements to display on either side of ellipsis when printing.

Details

The function useUnicode() can be used to determine if unicode should be used when printing the Set. Summarised details include the Set class, properties, and traits.

Method contains()

Tests to see if x is contained in the Set.

Usage

Set$contains(x, all = FALSE, bound = NULL)

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

ignored, added for consistency.

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.

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(elements = 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 equals()

Tests if two sets are equal.

Usage

Set$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 sets are equal if they contain the same elements. 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

# Equals
Set$new(1,2)$equals(Set$new(5,6))
Set$new(1,2)$equals(Interval$new(1,2))
Set$new(1,2) == Interval$new(1,2, class = "integer")

# Not equal !Set$new(1,2)$equals(Set$new(1,2)) Set$new(1,2) != Set$new(1,5)

Method isSubset()

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

Usage

Set$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.

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

Set$new(1,2,3)$isSubset(Set$new(1,2), proper = TRUE)
Set$new(1,2) < Set$new(1,2,3) # proper subset

c(Set$new(1,2,3), Set$new(1)) < Set$new(1,2,3) # not proper Set$new(1,2,3) <= Set$new(1,2,3) # proper

Method add()

Add elements to a set.

Usage

Set$add(...)

Arguments

...

elements to add

Details

$add is a wrapper around the setunion method with setunion(self, Set$new(...)). Note a key difference is that any elements passed to ... are first converted to a Set, this important difference is illustrated in the examples by adding an Interval to a Set.

Additionally, $add first coerces ... to $class if self is a typed-set (i.e. $class != "ANY"), and $add checks if elements in ... live in the universe of self.

Returns

An object inheriting from Set.

Examples

Set$new(1,2)$add(3)$print()
Set$new(1,2,universe = Interval$new(1,3))$add(3)$print()
\dontrun{
# errors as 4 is not in [1,3]
Set$new(1,2,universe = Interval$new(1,3))$add(4)$print()
}
# coerced to complex
Set$new(0+1i, 2i, class = "complex")$add(4)$print()

# setunion vs. add Set$new(1,2)$add(Interval$new(5,6))$print() Set$new(1,2) + Interval$new(5,6)

Method remove()

Remove elements from a set.

Usage

Set$remove(...)

Arguments

...

elements to remove

Details

$remove is a wrapper around the setcomplement method with setcomplement(self, Set$new(...)). Note a key difference is that any elements passed to ... are first converted to a Set, this important difference is illustrated in the examples by removing an Interval from a Set.

Returns

If the complement cannot be simplified to a Set then a ComplementSet is returned otherwise an object inheriting from Set is returned.

Examples

Set$new(1,2,3)$remove(1,2)$print()
Set$new(1,Set$new(1),2)$remove(Set$new(1))$print()
Interval$new(1,5)$remove(5)$print()
Interval$new(1,5)$remove(4)$print()

# setcomplement vs. remove Set$new(1,2,3)$remove(Interval$new(5,7))$print() Set$new(1,2,3) - Interval$new(5,7)

Method multiplicity()

Returns the number of times an element appears in a set,

Usage

Set$multiplicity(element = NULL)

Arguments

element

element or list of elements in the set, if NULL returns multiplicity of all elements

Returns

Value, or list of values, in R+.

Examples

Set$new(1, 1, 2)$multiplicity()
Set$new(1, 1, 2)$multiplicity(1)
Set$new(1, 1, 2)$multiplicity(list(1, 2))
Tuple$new(1, 1, 2)$multiplicity(1)
Tuple$new(1, 1, 2)$multiplicity(2)

Method clone()

The objects of this class are cloneable with this method.

Usage

Set$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Details

Mathematical sets can loosely be thought of as a collection of objects of any kind. The Set class is used for sets of finite elements, for infinite sets use Interval. These can be expanded for fuzzy logic by using FuzzySets. Elements in a set cannot be duplicated and ordering of elements does not matter, Tuples can be used if duplicates or ordering are required.

See Also

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

Examples

Run this code
# NOT RUN {
# Set of integers
Set$new(1:5)

# Set of multiple types
Set$new("a", 5, Set$new(1))

# Each Set has properties and traits
s <- Set$new(1, 2, 3)
s$traits
s$properties

# Elements cannot be duplicated
Set$new(2, 2) == Set$new(2)

# Ordering does not matter
Set$new(1, 2) == Set$new(2, 1)

## ------------------------------------------------
## Method `Set$contains`
## ------------------------------------------------

s = Set$new(elements = 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 `Set$equals`
## ------------------------------------------------

# Equals
Set$new(1,2)$equals(Set$new(5,6))
Set$new(1,2)$equals(Interval$new(1,2))
Set$new(1,2) == Interval$new(1,2, class = "integer")

# Not equal
!Set$new(1,2)$equals(Set$new(1,2))
Set$new(1,2) != Set$new(1,5)

## ------------------------------------------------
## Method `Set$isSubset`
## ------------------------------------------------

Set$new(1,2,3)$isSubset(Set$new(1,2), proper = TRUE)
Set$new(1,2) < Set$new(1,2,3) # proper subset

c(Set$new(1,2,3), Set$new(1)) < Set$new(1,2,3) # not proper
Set$new(1,2,3) <= Set$new(1,2,3) # proper

## ------------------------------------------------
## Method `Set$add`
## ------------------------------------------------

Set$new(1,2)$add(3)$print()
Set$new(1,2,universe = Interval$new(1,3))$add(3)$print()
# }
# NOT RUN {
# errors as 4 is not in [1,3]
Set$new(1,2,universe = Interval$new(1,3))$add(4)$print()
# }
# NOT RUN {
# coerced to complex
Set$new(0+1i, 2i, class = "complex")$add(4)$print()

# setunion vs. add
Set$new(1,2)$add(Interval$new(5,6))$print()
Set$new(1,2) + Interval$new(5,6)

## ------------------------------------------------
## Method `Set$remove`
## ------------------------------------------------

Set$new(1,2,3)$remove(1,2)$print()
Set$new(1,Set$new(1),2)$remove(Set$new(1))$print()
Interval$new(1,5)$remove(5)$print()
Interval$new(1,5)$remove(4)$print()

# setcomplement vs. remove
Set$new(1,2,3)$remove(Interval$new(5,7))$print()
Set$new(1,2,3) - Interval$new(5,7)

## ------------------------------------------------
## Method `Set$multiplicity`
## ------------------------------------------------

Set$new(1, 1, 2)$multiplicity()
Set$new(1, 1, 2)$multiplicity(1)
Set$new(1, 1, 2)$multiplicity(list(1, 2))
Tuple$new(1, 1, 2)$multiplicity(1)
Tuple$new(1, 1, 2)$multiplicity(2)
# }

Run the code above in your browser using DataLab