Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Closes #56
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphael Sonabend committed Oct 22, 2020
1 parent 64cee9c commit 48f2dea
Show file tree
Hide file tree
Showing 47 changed files with 1,001 additions and 173 deletions.
2 changes: 2 additions & 0 deletions CRAN-RELEASE
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This package was submitted to CRAN on 2020-07-26.
Once it is accepted, delete this file and tag the release (commit 5a74439f94).
7 changes: 5 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: set6
Title: R6 Mathematical Sets Interface
Version: 0.1.8
Version: 0.1.8.9000
Authors@R:
c(person(given = "Raphael",
family = "Sonabend",
Expand All @@ -22,7 +22,7 @@ BugReports: https://github.com/xoopR/set6/issues
VignetteBuilder:
knitr
Encoding: UTF-8
RoxygenNote: 7.1.0
RoxygenNote: 7.1.1
Roxygen: list(markdown = TRUE, r6 = TRUE)
Collate:
'Properties.R'
Expand All @@ -34,14 +34,17 @@ Collate:
'SetWrapper_PowersetSet.R'
'SetWrapper_ProductSet.R'
'SetWrapper_UnionSet.R'
'Set_Complex.R'
'Set_ConditionalSet.R'
'Set_FuzzySet.R'
'Set_FuzzySet_FuzzyTuple.R'
'Set_Interval.R'
'setSymbol.R'
'Set_Interval_SpecialSet.R'
'Set_LogicalSet.R'
'Set_Logicals.R'
'Set_Tuple.R'
'Set_Universal.R'
'Set_UniversalSet.R'
'asFuzzySet.R'
'asInterval.R'
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export(FuzzyTuple)
export(Integers)
export(Interval)
export(LogicalSet)
export(Logicals)
export(Naturals)
export(NegIntegers)
export(NegRationals)
Expand All @@ -97,6 +98,7 @@ export(Reals)
export(Set)
export(Tuple)
export(UnionSet)
export(Universal)
export(UniversalSet)
export(as.FuzzySet)
export(as.FuzzyTuple)
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# set6 0.1.8.9000

* UniversalSet renamed Universal, old class will be removed in v0.4.0.
* LogicalSet renamed Logicals, old class will be removed in v0.4.0.
* `Complex` now inherits from `Set`, incorrect methods for `isSubset, equals` have been removed.

# set6 0.1.8

* Patch for R-devel
Expand Down
10 changes: 5 additions & 5 deletions R/Set.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ Set <- R6Class("Set",
#' @description Create a new `Set` object.
#' @details Sets are constructed by elements of any types (including R6 classes), excluding lists.
#' `Set`s should be used within `Set`s instead of lists. The `universe` argument is useful for taking the absolute complement
#' of the `Set`. If a universe isn't given then [UniversalSet] is assumed. If the `class` argument is non-NULL, then all elements
#' of the `Set`. If a universe isn't given then [Universal] is assumed. If the `class` argument is non-NULL, then all elements
#' will be coerced to the given class in construction, and if elements of a different class are added these will either be rejected
#' or coerced.
#' @param ... any. Elements in the set.
#' @param universe Set. Universe that the Set lives in, i.e. elements that could be added to the Set. Default is the [UniversalSet].
#' @param universe Set. Universe that the Set lives in, i.e. elements that could be added to the Set. Default is the [Universal].
#' @param elements list. Alternative constructor that may be more efficient if passing objects of multiple classes.
#' @param class character. Optional string naming a class that if supplied gives the set the `typed` property.
#' @return A new `Set` object.
initialize = function(..., universe = UniversalSet$new(), elements = NULL, class = NULL) {
initialize = function(..., universe = Universal$new(), elements = NULL, class = NULL) {

private$.universe <- assertSet(universe)

Expand Down Expand Up @@ -75,7 +75,7 @@ Set <- R6Class("Set",
}
}

if (getR6Class(universe) != "UniversalSet") {
if (getR6Class(universe) != "Universal") {
assertContains(universe, elements, errormsg = "elements are not contained in the given universe")
}

Expand Down Expand Up @@ -289,7 +289,7 @@ Set <- R6Class("Set",
}
}

if (getR6Class(y) %in% c("ConditionalSet", "UniversalSet")) {
if (getR6Class(y) %in% c("ConditionalSet", "Universal")) {
return(FALSE)
} else if (testInterval(y)) {
if (testFinite(y)) {
Expand Down
121 changes: 121 additions & 0 deletions R/Set_Complex.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#' @name Complex
#' @title Set of Complex Numbers
#' @description The mathematical set of complex numbers,
#' defined as the the set of reals with possibly imaginary components. i.e.
#' \deqn{\\{a + bi \\ : \\ a,b \in R\\}}{{a + bi : a,b \epsilon R}}
#' where \eqn{R} is the set of reals.
#'
#' @details There is no inherent ordering in the set of complex numbers, hence only the `contains`
#' method is implemented here.
#'
#' @family special sets
#' @export
Complex <- R6Class("Complex",
inherit = Set,
public = list(
#' @description Create a new `Complex` object.
#' @return A new `Complex` object.
initialize = function() {
private$.properties <- Properties$new(closure = "open", cardinality = Inf)
invisible(self)
},

#' @description Tests to see if \code{x} is contained in the Set.
#'
#' @param x any. Object or vector of objects to test.
#' @param all logical. If `FALSE` tests each `x` separately. Otherwise returns `TRUE` only if all `x` pass test.
#' @param bound logical.
#'
#' @details \code{x} can be of any type, including a Set itself. \code{x} should be a tuple if
#' checking to see if it lies within a set of dimension greater than one. To test for multiple \code{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 [Interval]s, `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`).
#'
#' @return If \code{all} is `TRUE` then returns `TRUE` if all elements of \code{x} are contained in the `Set`, otherwise
#' `FALSE.` If \code{all} is `FALSE` then returns a vector of logicals corresponding to each individual
#' element of \code{x}.
#'
#' The infix operator `%inset%` is available to test if `x` is an element in the `Set`,
#' see examples.
contains = function(x, all = FALSE, bound = NULL) {
returner(
x = sapply(x, is.complex),
all = all
)
},

#' @description Tests if two sets are equal.
#' @param x [Set] or vector of [Set]s.
#' @param all logical. If `FALSE` tests each `x` separately. Otherwise returns `TRUE` only if all `x` pass test.
#' @return 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`.
#'
#' Infix operators can be used for:
#' \tabular{ll}{
#' Equal \tab `==` \cr
#' Not equal \tab `!=` \cr
#' }
#'
#' @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)
equals = function(x, all = FALSE) {
ret <- sapply(listify(x), getR6Class) %in% "Complex"
returner(ret, all)
},

#' @description Test if one set is a (proper) subset of another
#' @param x any. Object or vector of objects to test.
#' @param proper logical. If `TRUE` tests for proper subsets.
#' @param 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.
#'
#' 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:
#' \tabular{ll}{
#' Subset \tab `<` \cr
#' Proper Subset \tab `<=` \cr
#' Superset \tab `>` \cr
#' Proper Superset \tab `>=`
#' }
#'
#' Every `Set` is a subset of a `Universal`. No `Set` is a super set of a `Universal`,
#' and only a `Universal` is not a proper subset of a `Universal`.
#'
#' @return 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
isSubset = function(x, proper = FALSE, all = FALSE) {
stop("Not implemented for complex sets.")
},

#' @description Creates a printable representation of the object.
#' @param n numeric. Number of elements to display on either side of ellipsis when printing.
#' @return A character string representing the object.
strprint = function(n = 2) {
setSymbol(getR6Class(self))
}
)
)
4 changes: 2 additions & 2 deletions R/Set_ConditionalSet.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ ConditionalSet <- R6Class("ConditionalSet",
#' @param argclass list. Optional list of sets that the function arguments live in, see details.
#' @details The `condition` should be given as a function that when evaluated returns
#' either `TRUE` or `FALSE`. Further constraints can be given by providing the universe of the
#' function arguments as [Set]s, if these are not given then the [UniversalSet] is assumed.
#' function arguments as [Set]s, if these are not given then [Universal] is assumed.
#' See examples.
initialize = function(condition, argclass = NULL) {
if (!is.function(condition)) {
Expand All @@ -39,7 +39,7 @@ ConditionalSet <- R6Class("ConditionalSet",
if (!is.null(argclass)) {
assertSetList(argclass)
} else {
argclass <- rep(list(UniversalSet$new()), private$.dimension)
argclass <- rep(list(Universal$new()), private$.dimension)
names(argclass) <- names(formals(condition))
}

Expand Down
52 changes: 1 addition & 51 deletions R/Set_Interval_SpecialSet.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ SpecialSet <- R6Class("SpecialSet",
stop(paste(getR6Class(self, pos = environment()), "is an abstract class that can't be initialized."))
}

super$initialize(lower = lower, upper = upper, type = type, class = class, universe = UniversalSet$new())
super$initialize(lower = lower, upper = upper, type = type, class = class, universe = Universal$new())

invisible(self)
},
Expand Down Expand Up @@ -288,53 +288,3 @@ ExtendedReals <- R6Class("ExtendedReals",
}
)
)

#' @name Complex
#' @title Set of Complex Numbers
#' @description The mathematical set of complex numbers,
#' defined as the the set of reals with possibly imaginary components. i.e.
#' \deqn{\\{a + bi \\ : \\ a,b \in R\\}}{{a + bi : a,b \epsilon R}}
#' where \eqn{R} is the set of reals.
#' @details Unlike the other `SpecialSet`s, `Complex` can be used to define an `Interval`. In this
#' case where values can be complex, as opposed to reals or integers in [Interval].
#' @family special sets
#' @export
Complex <- R6Class("Complex",
inherit = SpecialSet,
public = list(
#' @description Create a new `Complex` object.
#' @return A new `Complex` object.
#' @param lower complex. Where to start the set.
#' @param upper complex. Where to end the set.
initialize = function(lower = -Inf + 0i, upper = Inf + 0i) {
super$initialize(lower = as.complex(lower), upper = as.complex(upper), type = "()", class = "numeric")
},

#' @description Tests to see if \code{x} is contained in the Set.
#'
#' @param x any. Object or vector of objects to test.
#' @param all logical. If `FALSE` tests each `x` separately. Otherwise returns `TRUE` only if all `x` pass test.
#' @param bound logical.
#'
#' @details \code{x} can be of any type, including a Set itself. \code{x} should be a tuple if
#' checking to see if it lies within a set of dimension greater than one. To test for multiple \code{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 [Interval]s, `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`).
#'
#' @return If \code{all} is `TRUE` then returns `TRUE` if all elements of \code{x} are contained in the `Set`, otherwise
#' `FALSE.` If \code{all} is `FALSE` then returns a vector of logicals corresponding to each individual
#' element of \code{x}.
#'
#' The infix operator `%inset%` is available to test if `x` is an element in the `Set`,
#' see examples.
contains = function(x, all = FALSE, bound = NULL) {
returner(
x = sapply(x, is.complex),
all = all
)
}
)
)
2 changes: 1 addition & 1 deletion R/Set_LogicalSet.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#' @name LogicalSet
#' @title Set of Logicals
#' @description The `LogicalSet` is defined as the [Set] containing the elements `TRUE` and `FALSE`.
#' @family sets
#'
#' @examples
#' l <- LogicalSet$new()
Expand All @@ -15,6 +14,7 @@ LogicalSet <- R6::R6Class("LogicalSet",
#' @details The Logical set is the set containing `TRUE` and `FALSE`.
#' @return A new `LogicalSet` object.
initialize = function() {
warning("Deprecated. In the future please use Logicals$new(). This will be removed in v0.4.0.")
private$.elements <- list(TRUE, FALSE)
private$.str_elements <- c("TRUE", "FALSE")
private$.class <- "logical"
Expand Down
28 changes: 28 additions & 0 deletions R/Set_Logicals.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#' @name Logicals
#' @title Set of Logicals
#' @description The `Logicals` is defined as the [Set] containing the elements `TRUE` and `FALSE`.
#' @family special sets
#'
#' @examples
#' l <- Logicals$new()
#' print(l)
#' l$contains(list(TRUE, 1, FALSE))
#' @export
Logicals <- R6::R6Class("Logicals",
inherit = Set,
public = list(
#' @description Create a new `Logicals` object.
#' @details The Logical set is the set containing `TRUE` and `FALSE`.
#' @return A new `Logicals` object.
initialize = function() {
private$.elements <- list(TRUE, FALSE)
private$.str_elements <- c("TRUE", "FALSE")
private$.class <- "logical"
private$.properties <- Properties$new(closure = "closed", cardinality = 2)
private$.lower <- TRUE
private$.upper <- FALSE
private$.universe <- self
invisible(self)
}
)
)
Loading

0 comments on commit 48f2dea

Please sign in to comment.