Title: | Sets, Generalized Sets, Customizable Sets and Intervals |
---|---|
Description: | Data structures and basic operations for ordinary sets, generalizations such as fuzzy sets, multisets, and fuzzy multisets, customizable sets, and intervals. |
Authors: | David Meyer [aut, cre] , Kurt Hornik [aut] , Christian Buchta [ctb] |
Maintainer: | David Meyer <[email protected]> |
License: | GPL-2 |
Version: | 1.0-25 |
Built: | 2024-11-19 06:36:03 UTC |
Source: | CRAN |
Helper function that canonicalizes set elements, and possibly reorders a given mapping accordingly.
canonicalize_set_and_mapping(x, mapping = NULL, margin = NULL)
canonicalize_set_and_mapping(x, mapping = NULL, margin = NULL)
x |
An object to be transformed into a set. |
mapping |
A list, array or data frame representing a mapping of the set. |
margin |
Margins to be reordered (ignored if |
This helper function can be used when a set is to be created from some
object x
, and another object contains some meta-information
on the set elements in the same order than the elements
of x
. The set creation can cause the input elements to be
permuted. By the use of this function, the meta information can be kept in
sync with the result of iterating over the associated set.
A list with three named components:
set |
The set created from |
mapping |
|
order |
The order used for rearranging the mapping. |
set
.
L <- list(c, "a", 3) M1 <- list("a","b","c") M2 <- matrix(1:9, ncol = 3) canonicalize_set_and_mapping(L, M1) canonicalize_set_and_mapping(L, M2) canonicalize_set_and_mapping(L, M2, 1)
L <- list(c, "a", 3) M1 <- list("a","b","c") M2 <- matrix(1:9, ncol = 3) canonicalize_set_and_mapping(L, M1) canonicalize_set_and_mapping(L, M2) canonicalize_set_and_mapping(L, M2, 1)
Closure and reduction of (g)sets.
## S3 method for class 'set' closure(x, operation = c("union", "intersection"), ...) binary_closure(x, operation = c("union", "intersection")) ## S3 method for class 'set' reduction(x, operation = c("union", "intersection"), ...) binary_reduction(x, operation = c("union", "intersection"))
## S3 method for class 'set' closure(x, operation = c("union", "intersection"), ...) binary_closure(x, operation = c("union", "intersection")) ## S3 method for class 'set' reduction(x, operation = c("union", "intersection"), ...) binary_reduction(x, operation = c("union", "intersection"))
x |
For |
operation |
The set operation under which the closure or reduction shall be computed. |
... |
Currently not used. |
The closure of a set under some operation
contains all elements
of
, and the results of
applied to all element pairs
of
.
The reduction of a set under some operation
is the minimal subset
of
having the same closure than
under
.
Note that the closure and reduction methods for sets are currently only implemented for sets of (g)sets (families) and will give an error for other cases.
binary_closure
and binary_reduction
interface efficient C code for computing
closures and reductions of binary patterns.
They are used by the
high-level methods if x
contains only objects of class sets
.
An object of same type than x
.
The C code for binary closures is provided by Christian Buchta.
## ordinary set s <- set(set(1),set(2),set(3)) (cl <- closure(s)) (re <- reduction(cl)) stopifnot(s == re) (cl <- closure(s, "intersection")) (re <- reduction(cl, "intersection")) stopifnot(s == re) ## multi set s <- set(gset(1,1),gset(2,2),gset(3,3)) (cl <- closure(s)) (re <- reduction(cl)) stopifnot(s == re) ## fuzzy set s <- set(gset(1,1/3),gset(2,2/3),gset(3,3/3)) (cl <- closure(s)) (re <- reduction(cl)) stopifnot(s == re) ## fuzzy multiset s <- set(gset(1,list(set(1,0.8))), gset(2, list(gset(1,3))), gset(3,0.3)) (cl <- closure(s)) (re <- reduction(cl)) stopifnot(s == re)
## ordinary set s <- set(set(1),set(2),set(3)) (cl <- closure(s)) (re <- reduction(cl)) stopifnot(s == re) (cl <- closure(s, "intersection")) (re <- reduction(cl, "intersection")) stopifnot(s == re) ## multi set s <- set(gset(1,1),gset(2,2),gset(3,3)) (cl <- closure(s)) (re <- reduction(cl)) stopifnot(s == re) ## fuzzy set s <- set(gset(1,1/3),gset(2,2/3),gset(3,3/3)) (cl <- closure(s)) (re <- reduction(cl)) stopifnot(s == re) ## fuzzy multiset s <- set(gset(1,list(set(1,0.8))), gset(2, list(gset(1,3))), gset(3,0.3)) (cl <- closure(s)) (re <- reduction(cl)) stopifnot(s == re)
Creation and manipulation of customizable sets.
cset(gset, orderfun = sets_options("orderfun"), matchfun = sets_options("matchfun")) cset_support(x) cset_core(x, na.rm = FALSE) cset_peak(x, na.rm = FALSE) cset_height(x, na.rm = FALSE) cset_memberships(x, filter = NULL) cset_universe(x) cset_bound(x) cset_transform_memberships(x, FUN, ...) cset_concentrate(x) cset_dilate(x) cset_normalize(x, height = 1) cset_defuzzify(x, method = c("meanofmax", "smallestofmax", "largestofmax", "centroid")) matchfun(FUN) cset_orderfun(x) cset_matchfun(x) cset_orderfun(x) <- value cset_matchfun(x) <- value as.cset(x) is.cset(x) cset_is_empty(x, na.rm = FALSE) cset_is_subset(x, y, na.rm = FALSE) cset_is_proper_subset(x, y, na.rm = FALSE) cset_is_equal(x, y, na.rm = FALSE) cset_contains_element(x, e) cset_is_set(x, na.rm = FALSE) cset_is_multiset(x, na.rm = FALSE) cset_is_fuzzy_set(x, na.rm = FALSE) cset_is_set_or_multiset(x, na.rm = FALSE) cset_is_set_or_fuzzy_set(x, na.rm = FALSE) cset_is_fuzzy_multiset(x) cset_is_crisp(x, na.rm = FALSE) cset_has_missings(x) cset_cardinality(x, type = c("absolute", "relative"), na.rm = FALSE) cset_union(...) cset_mean(x, y, type = c("arithmetic", "geometric", "harmonic")) cset_product(...) cset_difference(...) cset_intersection(...) cset_symdiff(...) cset_complement(x, y) cset_power(x) cset_cartesian(...) cset_combn(x, m) ## S3 method for class 'cset' cut(x, level = 1, type = c("alpha", "nu"), strict = FALSE, ...) ## S3 method for class 'cset' mean(x, ..., na.rm = FALSE) ## S3 method for class 'cset' ## median(x, na.rm = FALSE, ...) [R >= 3.4.0] ## median(x, na.rm) [R < 3.4.0] ## S3 method for class 'cset' length(x) ## S3 method for class 'cset' lengths(x, use.names = TRUE)
cset(gset, orderfun = sets_options("orderfun"), matchfun = sets_options("matchfun")) cset_support(x) cset_core(x, na.rm = FALSE) cset_peak(x, na.rm = FALSE) cset_height(x, na.rm = FALSE) cset_memberships(x, filter = NULL) cset_universe(x) cset_bound(x) cset_transform_memberships(x, FUN, ...) cset_concentrate(x) cset_dilate(x) cset_normalize(x, height = 1) cset_defuzzify(x, method = c("meanofmax", "smallestofmax", "largestofmax", "centroid")) matchfun(FUN) cset_orderfun(x) cset_matchfun(x) cset_orderfun(x) <- value cset_matchfun(x) <- value as.cset(x) is.cset(x) cset_is_empty(x, na.rm = FALSE) cset_is_subset(x, y, na.rm = FALSE) cset_is_proper_subset(x, y, na.rm = FALSE) cset_is_equal(x, y, na.rm = FALSE) cset_contains_element(x, e) cset_is_set(x, na.rm = FALSE) cset_is_multiset(x, na.rm = FALSE) cset_is_fuzzy_set(x, na.rm = FALSE) cset_is_set_or_multiset(x, na.rm = FALSE) cset_is_set_or_fuzzy_set(x, na.rm = FALSE) cset_is_fuzzy_multiset(x) cset_is_crisp(x, na.rm = FALSE) cset_has_missings(x) cset_cardinality(x, type = c("absolute", "relative"), na.rm = FALSE) cset_union(...) cset_mean(x, y, type = c("arithmetic", "geometric", "harmonic")) cset_product(...) cset_difference(...) cset_intersection(...) cset_symdiff(...) cset_complement(x, y) cset_power(x) cset_cartesian(...) cset_combn(x, m) ## S3 method for class 'cset' cut(x, level = 1, type = c("alpha", "nu"), strict = FALSE, ...) ## S3 method for class 'cset' mean(x, ..., na.rm = FALSE) ## S3 method for class 'cset' ## median(x, na.rm = FALSE, ...) [R >= 3.4.0] ## median(x, na.rm) [R < 3.4.0] ## S3 method for class 'cset' length(x) ## S3 method for class 'cset' lengths(x, use.names = TRUE)
x |
For |
y |
A (c)set object. |
gset |
A generalized set (or some other R object coercible to it). |
matchfun |
A function for matching similar elements, comparable
to |
FUN |
A predicate testing for equality of two objects. |
orderfun |
A function taking a list and returning an integer vector, specifying the order in which an iterator processes the set elements. Alternatively, the index vector can be specified directly. |
value |
A new match function (order function). |
type |
For |
strict |
Logical indicating whether the cut level must be exceeded strictly (“greater than”) or not (“greater than or equal”). |
height |
Double from the unit interval for scaling memberships. |
e |
An object of class |
filter |
Optional vector of elements to be filtered. |
m |
Number of elements to choose. |
method |
Currently, only |
level |
The minimum membership level. |
use.names |
logical; should the names of |
na.rm |
logical indicating whether |
... |
For |
Customizable sets extend generalized sets in two ways: First, users
can control the way elements are matched, i.e., define equivalence
classes of elements. Second, an order function (or permutation index)
can be specified for each set for changing the order in which
iterators such as as.list
process the elements. The latter in
particular influences the labeling and print methods for
customizable sets.
The match function needs to be vectorized in a similar way than
match
. matchfun
can be used to create such a
function from a “simple” predicate testing for equality
(such as, e.g., identical
). Make sure, however, to
create the same function only once.
Note that operations on customizable sets require the same match function for all sets involved. The order function can differ, but will then be stripped from the result.
sets_options
can be used to
conveniently switch the default match and/or
order function if a number of cset
objects need to be created.
D. Meyer and K. Hornik (2009), Generalized and customizable sets in R, Journal of Statistical Software 31(2), 1–27. doi:10.18637/jss.v031.i02.
set
for (“ordinary”) sets,
gset
for generalized sets,
cset_outer
, and
tuple
for tuples (“vectors”).
## default behavior of sets: matching of elements is very strict ## Note that on most systems, 3.3 - 2.2 != 1.1 x <- set("1", 1L, 1, 3.3 - 2.2, 1.1) print(x) y <- set(1, 1.1, 2L, "2") print(y) 1L %e% y set_union(x, y) set_intersection(x, y) set_complement(x, y) ## Now use the more sloppy match()-function. ## Note that 1 == "1" == 1L ... X <- cset(x, matchfun = match) print(X) Y <- cset(y, matchfun = match) print(Y) 1L %e% Y cset_union(X, Y) cset_intersection(X, Y) cset_complement(X, Y) ## Same using all.equal(). ## This is a non-vectorized predicate, so use matchfun ## to generate a vectorized version: FUN <- matchfun(function(x, y) isTRUE(all.equal(x, y))) X <- cset(x, matchfun = FUN) print(X) Y <- cset(y, matchfun = FUN) print(Y) 1L %e% Y cset_union(X, Y) cset_intersection(X, Y) cset_complement(X, Y) ### change default functions via set_option sets_options("matchfun", match) cset(x) cset(y) cset(1:3) <= cset(c(1,2,3)) ### restore package defaults sets_options("matchfun", NULL) ### customized order function FUN <- function(x) order(as.character(x), decreasing = TRUE) Z <- cset(letters[1:5], orderfun = FUN) print(Z) as.character(Z) ## converter for ordered factors keeps order o <- ordered(c("a", "b", "a"), levels = c("b", "a")) as.set(o) as.cset(o) ## converter for other data types keep order if the elements are unique: as.cset(c("A", "quick", "brown", "fox")) as.cset(c("A", "quick", "brown", "fox", "quick"))
## default behavior of sets: matching of elements is very strict ## Note that on most systems, 3.3 - 2.2 != 1.1 x <- set("1", 1L, 1, 3.3 - 2.2, 1.1) print(x) y <- set(1, 1.1, 2L, "2") print(y) 1L %e% y set_union(x, y) set_intersection(x, y) set_complement(x, y) ## Now use the more sloppy match()-function. ## Note that 1 == "1" == 1L ... X <- cset(x, matchfun = match) print(X) Y <- cset(y, matchfun = match) print(Y) 1L %e% Y cset_union(X, Y) cset_intersection(X, Y) cset_complement(X, Y) ## Same using all.equal(). ## This is a non-vectorized predicate, so use matchfun ## to generate a vectorized version: FUN <- matchfun(function(x, y) isTRUE(all.equal(x, y))) X <- cset(x, matchfun = FUN) print(X) Y <- cset(y, matchfun = FUN) print(Y) 1L %e% Y cset_union(X, Y) cset_intersection(X, Y) cset_complement(X, Y) ### change default functions via set_option sets_options("matchfun", match) cset(x) cset(y) cset(1:3) <= cset(c(1,2,3)) ### restore package defaults sets_options("matchfun", NULL) ### customized order function FUN <- function(x) order(as.character(x), decreasing = TRUE) Z <- cset(letters[1:5], orderfun = FUN) print(Z) as.character(Z) ## converter for ordered factors keeps order o <- ordered(c("a", "b", "a"), levels = c("b", "a")) as.set(o) as.cset(o) ## converter for other data types keep order if the elements are unique: as.cset(c("A", "quick", "brown", "fox")) as.cset(c("A", "quick", "brown", "fox", "quick"))
Fuzzy Logic
fuzzy_logic(new, ...) .N.(x) .T.(x, y) .S.(x, y) .I.(x, y)
fuzzy_logic(new, ...) .N.(x) .T.(x, y) .S.(x, y) .I.(x, y)
x , y
|
Numeric vectors. |
new |
A character string specifying one of the available fuzzy logic “families” (see details). |
... |
optional parameters for the selected family. |
A call to fuzzy_logic()
without arguments returns the currently
set fuzzy logic, i.e., a named list with four
components N
, T
, S
, and I
containing the
corresponding functions for negation, conjunction
(“t-norm”), disjunction (“t-conorm”), and residual
implication (which may not be available).
The package provides several fuzzy logic families.
A concrete fuzzy logic is selected
by calling fuzzy_logic
with a character
string specifying the family name, and optional parameters. Let us
refer to as the standard negation, and,
for a t-norm
, let
be the
dual (or complementary) t-conorm. Available specifications and
corresponding families are as follows, with the standard negation used
unless stated otherwise.
"Zadeh"
Zadeh's logic with and
. Note that the minimum t-norm, also known as the
Gödel t-norm, is the pointwise largest t-norm, and that the
maximum t-conorm is the smallest t-conorm.
"drastic"
the drastic logic with t-norm
if
,
if
, and 0
otherwise, and complementary t-conorm
if
,
if
, and 1 otherwise. Note that
the drastic t-norm and t-conorm are the smallest t-norm and
largest t-conorm, respectively.
"product"
the family with the product t-norm
and dual t-conorm
.
"Lukasiewicz"
the Lukasiewicz logic with t-norm
and dual t-conorm
.
"Fodor"
the family with Fodor's nilpotent
minimum t-norm given by if
, and 0 otherwise, and the dual t-conorm given by
if
, and 1 otherwise.
"Frank"
the family of Frank t-norms ,
, which gives the Zadeh, product and Lukasiewicz
t-norms for
, 1, and
, respectively,
and otherwise is given by
.
"Hamacher"
the three-parameter family of Hamacher,
with negation ,
t-norm
,
and t-conorm
,
where
and
. This
gives a deMorgan triple (for which
iff
. The parameters can
be specified as
alpha
, beta
and gamma
,
respectively. If is not given, it is taken as
.
The default values for
and
are 0, so that
by default, the product family is obtained.
The following parametric families are obtained by combining the corresponding families of t-norms with the standard negation.
"Schweizer-Sklar"
the Schweizer-Sklar family
,
, which
gives the Zadeh (minimum), product and drastic t-norms for
,
, and
,
respectively, and otherwise is given by
.
"Yager"
the Yager family ,
,
which gives the drastic and minimum t-norms for
and
, respectively, and otherwise is given by
.
"Dombi"
the Dombi family ,
,
which gives the drastic and minimum t-norms for
and
, respectively, and otherwise is given by
if
or
, and
if
both
and
.
"Aczel-Alsina"
the family of t-norms ,
, introduced by Aczél and Alsina, which gives the
drastic and minimum t-norms for
and
, respectively, and otherwise is given by
.
"Sugeno-Weber"
the family of t-norms ,
, introduced by Weber
with dual t-conorms introduced by Sugeno, which gives the
drastic and product t-norms for
and
, respectively, and otherwise is given by
.
"Dubois-Prade"
the family of t-norms ,
, introduced by Dubois and Prade, which gives
the minimum and product t-norms for
and
,
respectively, and otherwise is given by
.
"Yu"
the family of t-norms ,
,
introduced by Yu, which gives the product and drastic t-norms for
and
, respectively, and otherwise is
given by
.
By default, the Zadeh logic is used.
.N.
, .T.
, .S.
, and .I.
are dynamic
functions, i.e., wrappers that call the corresponding function of the
current fuzzy logic. Thus, the behavior of code using these
functions will change according to the chosen logic.
C. Alsina, M. J. Frank and B. Schweizer (2006), Associative Functions: Triangular Norms and Copulas. World Scientific. ISBN 981-256-671-6.
J. Dombi (1982), A general class of fuzzy operators, the De Morgan class of fuzzy operators and fuzziness measures induced by fuzzy operators, Fuzzy Sets and Systems 8, 149–163.
J. Fodor and M. Roubens (1994), Fuzzy Preference Modelling and Multicriteria Decision Support. Kluwer Academic Publishers, Dordrecht.
D. Meyer and K. Hornik (2009), Generalized and customizable sets in R, Journal of Statistical Software 31(2), 1–27. doi:10.18637/jss.v031.i02.
B. Schweizer and A. Sklar (1983), Probabilistic Metric Spaces. North-Holland, New York. ISBN 0-444-00666-4.
x <- c(0.7, 0.8) y <- c(0.2, 0.3) ## Use default family ("Zadeh") .N.(x) .T.(x, y) .S.(x, y) .I.(x, y) ## Switch family and try again fuzzy_logic("Fodor") .N.(x) .T.(x, y) .S.(x, y) .I.(x, y)
x <- c(0.7, 0.8) y <- c(0.2, 0.3) ## Use default family ("Zadeh") .N.(x) .T.(x, y) .S.(x, y) .I.(x, y) ## Switch family and try again fuzzy_logic("Fodor") .N.(x) .T.(x, y) .S.(x, y) .I.(x, y)
Occurence of three terms (neural networks, fuzzy, and image) in 30 documents retrieved from a Japanese article data base on fuzzy theory and systems.
data("fuzzy_docs")
data("fuzzy_docs")
fuzzy_docs
is a list of 30 fuzzy multisets, representing the
occurrence of the terms “neural networks”, “fuzzy”, and
“image” in each document. Each term appears with up to
three membership values representing weights,
depending on whether the term occurred
in the abstract (0.2), the keywords section (0.6), and/or the title
(1). The first 12 documents concern neural networks, the remaining 18
image processing. In the reference, various clustering methods have
been employed to recover the two groups in the data set.
K. Mizutani, R. Inokuchi, and S. Miyamoto (2008), Algorithms of Nonlinear Document Clustering Based on Fuzzy Multiset Model, International Journal of Intelligent Systems, 23, 176–198.
data(fuzzy_docs) ## compute distance matrix using Jaccard dissimilarity d <- as.dist(set_outer(fuzzy_docs, gset_dissimilarity)) ## apply hierarchical clustering (Ward method) cl <- hclust(d, "ward") ## retrieve two clusters cutree(cl, 2) ## -> clearly, the clusters are formed by docs 1--12 and 13--30, ## respectively.
data(fuzzy_docs) ## compute distance matrix using Jaccard dissimilarity d <- as.dist(set_outer(fuzzy_docs, gset_dissimilarity)) ## apply hierarchical clustering (Ward method) cl <- hclust(d, "ward") ## retrieve two clusters cutree(cl, 2) ## -> clearly, the clusters are formed by docs 1--12 and 13--30, ## respectively.
Fuzzy membership and set creator functions.
charfun_generator(FUN, height = 1) fuzzy_tuple(FUN = fuzzy_normal, n = 5, ..., universe = NULL, names = NULL) is.charfun_generator(x) fuzzy_normal(mean = NULL, sd = 1, log = FALSE, height = 1, chop = 0) fuzzy_two_normals(mean = NULL, sd = c(1,1), log = c(FALSE, FALSE), height = 1, chop = 0) fuzzy_bell(center = NULL, cross = NULL, slope = 4, height = 1, chop = 0) fuzzy_sigmoid(cross = NULL, slope = 0.5, height = 1, chop = 0) fuzzy_trapezoid(corners = NULL, height = c(1,1), return_base_corners = TRUE) fuzzy_triangular(corners = NULL, height = 1, return_base_corners = TRUE) fuzzy_cone(center = NULL, radius = 2, height = 1, return_base_corners = TRUE) fuzzy_pi3(mid = NULL, min = NULL, max = NULL, height = 1, return_base_corners = TRUE) fuzzy_pi4(knots, height = 1, return_base_corners = TRUE) fuzzy_normal_gset(mean = NULL, sd = 1, log = FALSE, height = 1, chop = 0, universe = NULL) fuzzy_two_normals_gset(mean = NULL, sd = c(1,1), log = c(FALSE, FALSE), height = 1, chop = 0, universe = NULL) fuzzy_bell_gset(center = NULL, cross = NULL, slope = 4, height = 1, chop = 0, universe = NULL) fuzzy_sigmoid_gset(cross = NULL, slope = 0.5, height = 1, chop = 0, universe = NULL) fuzzy_trapezoid_gset(corners = NULL, height = c(1,1), universe = NULL, return_base_corners = TRUE) fuzzy_triangular_gset(corners = NULL, height = 1, universe = NULL, return_base_corners = TRUE) fuzzy_cone_gset(center = NULL, radius = 2, height = 1, universe = NULL, return_base_corners = TRUE) fuzzy_pi3_gset(mid = NULL, min = NULL, max = NULL, height = 1, universe = NULL, return_base_corners = TRUE) fuzzy_pi4_gset(knots, height = 1, universe = NULL, return_base_corners = TRUE)
charfun_generator(FUN, height = 1) fuzzy_tuple(FUN = fuzzy_normal, n = 5, ..., universe = NULL, names = NULL) is.charfun_generator(x) fuzzy_normal(mean = NULL, sd = 1, log = FALSE, height = 1, chop = 0) fuzzy_two_normals(mean = NULL, sd = c(1,1), log = c(FALSE, FALSE), height = 1, chop = 0) fuzzy_bell(center = NULL, cross = NULL, slope = 4, height = 1, chop = 0) fuzzy_sigmoid(cross = NULL, slope = 0.5, height = 1, chop = 0) fuzzy_trapezoid(corners = NULL, height = c(1,1), return_base_corners = TRUE) fuzzy_triangular(corners = NULL, height = 1, return_base_corners = TRUE) fuzzy_cone(center = NULL, radius = 2, height = 1, return_base_corners = TRUE) fuzzy_pi3(mid = NULL, min = NULL, max = NULL, height = 1, return_base_corners = TRUE) fuzzy_pi4(knots, height = 1, return_base_corners = TRUE) fuzzy_normal_gset(mean = NULL, sd = 1, log = FALSE, height = 1, chop = 0, universe = NULL) fuzzy_two_normals_gset(mean = NULL, sd = c(1,1), log = c(FALSE, FALSE), height = 1, chop = 0, universe = NULL) fuzzy_bell_gset(center = NULL, cross = NULL, slope = 4, height = 1, chop = 0, universe = NULL) fuzzy_sigmoid_gset(cross = NULL, slope = 0.5, height = 1, chop = 0, universe = NULL) fuzzy_trapezoid_gset(corners = NULL, height = c(1,1), universe = NULL, return_base_corners = TRUE) fuzzy_triangular_gset(corners = NULL, height = 1, universe = NULL, return_base_corners = TRUE) fuzzy_cone_gset(center = NULL, radius = 2, height = 1, universe = NULL, return_base_corners = TRUE) fuzzy_pi3_gset(mid = NULL, min = NULL, max = NULL, height = 1, universe = NULL, return_base_corners = TRUE) fuzzy_pi4_gset(knots, height = 1, universe = NULL, return_base_corners = TRUE)
x |
An R object. |
n |
Positive integer indicating the number of sets to be created, or integer vector of location parameters used to create the sets. |
FUN |
Function to be used for creating a membership function. Needs to be vectorized, i.e., is expected to take a vector of set elements and to return a vector of numeric values. |
height |
Numeric value in the unit interval specifying the height of the set resulting from applying the membership function to the universe, i.e., the maximum value to which the values will be scaled to. |
chop |
Threshold value below which the membership function is truncated, i.e., has a value of 0. |
center , mean
|
Numeric mean value(s) used for the resulting membership function. |
sd |
Numeric scale factor(s) (standard deviation(s)) used for the resulting membership function. |
radius |
Double added/subtracted from |
log |
Logical (vector), indicating whether normal or log-normal distributions should be used. |
cross |
Double indicating the cross-over point for the sigmoidal distribution. |
slope |
Double indicating the slope at the cross-over point. |
corners |
Double values (vector of length four) indicating the abscissas of the four corners of the resulting trapezoid. |
min , mid , max
|
Doubles indicating the abscissas of the three
spline knots |
knots |
Vector of doubles of length four indicating the abscissas of the spline knots the curve passes through. |
return_base_corners |
Logical indicating whether membership grades
for the base line corner elements should be returned as small values
( |
universe |
Universal set used for computing the memberships grades. |
names |
optional character vector of component labels for the return value. |
... |
Further arguments passed to |
These functions can be used to create sets with certain membership patterns.
The core functions are function generators, taking parameters
and returning a corresponding fuzzy function (i.e., with values in the
unit interval). All of them are normalized, i.e., scaled to have a
maximum value of height
(default: 1):
fuzzy_normal
is simply based on dnorm
.
fuzzy_two_normals
returns a function composed of the left and right parts of two normal distributions (each normalized), with possibly different means and standard deviations.
fuzzy_bell
returns a function defined as:
with center
,
crossover points
, and slope at the crossover
points of
.
fuzzy_sigmoid
yields a function whose values are
computed as with slope
at crossover point
.
fuzzy_trapezoid
creates a function with
trapezoidal shape, defined by four corners
elements and two
height
values for the second and third corner (the heights of
the first and fourth corner being fixed at 0).
fuzzy_triangular
similar to the above with only three corners.
fuzzy_cone
is a special case of fuzzy_triangular
,
defining an isosceles triangle with corners (element, membership degree):
,
, and
.
fuzzi_pi3
constructs a spline based on two quadratic
functions, passing through the knot points ,
and
.
fuzzi_pi4
constructs a spline based on an S-shaped and
a Z-shaped curve forming a -shaped one, passing through
the four knots
,
,
, and
.
charfun_generator
takes a vectorized function as argument,
returning a function normalized by height
.
The fuzzy_foo_gset
functions directly generate
generalized sets from fuzzy_foo
, using the values defined by
universe
, sets_options("universe")
, or seq(0, 20, by
= 0.1)
(in that order, whichever is not NULL
).
fuzzy_tuple
generates a sequence of n
sets based on any of the generating functions (except
fuzzy_trapezoid
and fuzzy_triangular
). The chosen
generating function FUN
is called with n
different
values chosen along the universe
passed to the
first argument, thus varying the position or the resulting graph.
For charfun_generator
, a generating function
taking an argument list of parameters, and returning a membership
function, mapping elements to membership values (from of the unit
interval).
For fuzzy_tuple
, a tuple of n
fuzzy sets.
For is.charfun_generator
, a logical.
For fuzzy_foo_gset
, a fuzzy set.
For the other functions, a membership function.
set
, gset
, and tuple
for the
set types, and plot.gset
for the available plot functions.
## creating a fuzzy normal function N <- fuzzy_normal(mean = 0, sd = 1) N(-3:3) ## create a fuzzy set with it gset(charfun = N, universe = -3:3) ## same using wrapper fuzzy_normal_gset(universe = -3:3) ## creating a user-defined fuzzy function fuzzy_poisson <- charfun_generator(dpois) gset(charfun = fuzzy_poisson(10), universe = seq(0, 20, 2)) ## creating a series of fuzzy normal sets fuzzy_tuple(fuzzy_normal, 5) ## creating a series of fuzzy cones with specific locations fuzzy_tuple(fuzzy_cone, c(2,3,7))
## creating a fuzzy normal function N <- fuzzy_normal(mean = 0, sd = 1) N(-3:3) ## create a fuzzy set with it gset(charfun = N, universe = -3:3) ## same using wrapper fuzzy_normal_gset(universe = -3:3) ## creating a user-defined fuzzy function fuzzy_poisson <- charfun_generator(dpois) gset(charfun = fuzzy_poisson(10), universe = seq(0, 20, 2)) ## creating a series of fuzzy normal sets fuzzy_tuple(fuzzy_normal, 5) ## creating a series of fuzzy cones with specific locations fuzzy_tuple(fuzzy_cone, c(2,3,7))
Basic infrastructure for building and using fuzzy inference systems.
fuzzy_inference(system, values, implication = c("minimum", "product")) fuzzy_rule(antecedent, consequent) fuzzy_system(variables, rules) fuzzy_partition(varnames, FUN = fuzzy_normal, universe = NULL, ...) fuzzy_variable(...) x %is% y
fuzzy_inference(system, values, implication = c("minimum", "product")) fuzzy_rule(antecedent, consequent) fuzzy_system(variables, rules) fuzzy_partition(varnames, FUN = fuzzy_normal, universe = NULL, ...) fuzzy_variable(...) x %is% y
... |
For |
antecedent , consequent
|
parts of an inference rule (see details). |
variables |
Set or tuple of fuzzy variables (note that tuples must be used if two variables have the same definition). |
rules |
Set of rules. |
system |
A fuzzy system. |
values |
Named list of input values to the system. The names must match the labels of the variable set. |
implication |
A vectorized function taking two arguments, or a character string indicating the parallel minimum or the product function. |
varnames |
Either a character vector of fuzzy category labels, to be used with the default locations, or a named numeric vector of locations. |
FUN |
Function generator for membership functions to be used for the fuzzy partition. |
universe |
Universal set used for computing the memberships grades. |
x |
The name of a fuzzy variable. |
y |
The name of a category, belonging to a fuzzy variable. |
These functions can be used to create simple fuzzy inference machines based on fuzzy (“linguistic”) variables and fuzzy rules. This involves five steps:
Fuzzification of the input variables.
Application of fuzzy operators (AND, OR, NOT) in the antecedents of some given rules.
Implication from the antecedent to the consequent.
Aggregation of the consequents across the rules.
Defuzzification of the resulting fuzzy set.
Implication is based on either the minimum or the product. The evaluation of the logical expressions in the antecedents, as well as the aggregation of the evaluation result for each single rule, depends on the fuzzy logic currently set.
For fuzzy_inference
: a generalized set. For
fuzzy_rule
and fuzzy_system
: an object of class
fuzzy_rule
and fuzzy_system
, respectively.
For fuzzy_variable
and fuzzy_partition
: an object of
class fuzzy_variable
, inheriting from tuple
.
set
and gset
for the
set types, fuzzy_tuple
for available fuzzy functions,
and fuzzy_logic
on the behavior of the implemented fuzzy
operators.
## set universe sets_options("universe", seq(from = 0, to = 25, by = 0.1)) ## set up fuzzy variables variables <- set(service = fuzzy_partition(varnames = c(poor = 0, good = 5, excellent = 10), sd = 1.5), food = fuzzy_variable(rancid = fuzzy_trapezoid(corners = c(-2, 0, 2, 4)), delicious = fuzzy_trapezoid(corners = c(7, 9, 11, 13))), tip = fuzzy_partition(varnames = c(cheap = 5, average = 12.5, generous = 20), FUN = fuzzy_cone, radius = 5) ) ## set up rules rules <- set( fuzzy_rule(service %is% poor || food %is% rancid, tip %is% cheap), fuzzy_rule(service %is% good, tip %is% average), fuzzy_rule(service %is% excellent || food %is% delicious, tip %is% generous) ) ## combine to a system system <- fuzzy_system(variables, rules) print(system) plot(system) ## plots variables ## do inference fi <- fuzzy_inference(system, list(service = 3, food = 8)) ## plot resulting fuzzy set plot(fi) ## defuzzify gset_defuzzify(fi, "centroid") ## reset universe sets_options("universe", NULL)
## set universe sets_options("universe", seq(from = 0, to = 25, by = 0.1)) ## set up fuzzy variables variables <- set(service = fuzzy_partition(varnames = c(poor = 0, good = 5, excellent = 10), sd = 1.5), food = fuzzy_variable(rancid = fuzzy_trapezoid(corners = c(-2, 0, 2, 4)), delicious = fuzzy_trapezoid(corners = c(7, 9, 11, 13))), tip = fuzzy_partition(varnames = c(cheap = 5, average = 12.5, generous = 20), FUN = fuzzy_cone, radius = 5) ) ## set up rules rules <- set( fuzzy_rule(service %is% poor || food %is% rancid, tip %is% cheap), fuzzy_rule(service %is% good, tip %is% average), fuzzy_rule(service %is% excellent || food %is% delicious, tip %is% generous) ) ## combine to a system system <- fuzzy_system(variables, rules) print(system) plot(system) ## plots variables ## do inference fi <- fuzzy_inference(system, list(service = 3, food = 8)) ## plot resulting fuzzy set plot(fi) ## defuzzify gset_defuzzify(fi, "centroid") ## reset universe sets_options("universe", NULL)
Creation and manipulation of generalized sets.
gset(support, memberships, charfun, elements, universe, bound, assume_numeric_memberships) as.gset(x) is.gset(x) gset_support(x) gset_core(x, na.rm = FALSE) gset_peak(x, na.rm = FALSE) gset_height(x, na.rm = FALSE) gset_universe(x) gset_bound(x) gset_memberships(x, filter = NULL) gset_transform_memberships(x, FUN, ...) gset_concentrate(x) gset_dilate(x) gset_normalize(x, height = 1) gset_defuzzify(x, method = c("meanofmax", "smallestofmax", "largestofmax", "centroid")) gset_is_empty(x, na.rm = FALSE) gset_is_subset(x, y, na.rm = FALSE) gset_is_proper_subset(x, y, na.rm = FALSE) gset_is_equal(x, y, na.rm = FALSE) gset_contains_element(x, e) gset_is_set(x, na.rm = FALSE) gset_is_multiset(x, na.rm = FALSE) gset_is_fuzzy_set(x, na.rm = FALSE) gset_is_set_or_multiset(x, na.rm = FALSE) gset_is_set_or_fuzzy_set(x, na.rm = FALSE) gset_is_fuzzy_multiset(x) gset_is_crisp(x, na.rm = FALSE) gset_has_missings(x) gset_cardinality(x, type = c("absolute", "relative"), na.rm = FALSE) gset_union(...) gset_sum(...) gset_difference(...) gset_product(...) gset_mean(x, y, type = c("arithmetic", "geometric", "harmonic")) gset_intersection(...) gset_symdiff(...) gset_complement(x, y) gset_power(x) gset_cartesian(...) gset_combn(x, m) e(x, memberships = 1L) is_element(e) ## S3 method for class 'gset' cut(x, level = 1, type = c("alpha", "nu"), strict = FALSE, ...) ## S3 method for class 'gset' mean(x, ..., na.rm = FALSE) ## S3 method for class 'gset' ## median(x, na.rm = FALSE, ...) [R >= 3.4.0] ## median(x, na.rm) [R < 3.4.0] ## S3 method for class 'gset' length(x) ## S3 method for class 'gset' lengths(x, use.names = TRUE)
gset(support, memberships, charfun, elements, universe, bound, assume_numeric_memberships) as.gset(x) is.gset(x) gset_support(x) gset_core(x, na.rm = FALSE) gset_peak(x, na.rm = FALSE) gset_height(x, na.rm = FALSE) gset_universe(x) gset_bound(x) gset_memberships(x, filter = NULL) gset_transform_memberships(x, FUN, ...) gset_concentrate(x) gset_dilate(x) gset_normalize(x, height = 1) gset_defuzzify(x, method = c("meanofmax", "smallestofmax", "largestofmax", "centroid")) gset_is_empty(x, na.rm = FALSE) gset_is_subset(x, y, na.rm = FALSE) gset_is_proper_subset(x, y, na.rm = FALSE) gset_is_equal(x, y, na.rm = FALSE) gset_contains_element(x, e) gset_is_set(x, na.rm = FALSE) gset_is_multiset(x, na.rm = FALSE) gset_is_fuzzy_set(x, na.rm = FALSE) gset_is_set_or_multiset(x, na.rm = FALSE) gset_is_set_or_fuzzy_set(x, na.rm = FALSE) gset_is_fuzzy_multiset(x) gset_is_crisp(x, na.rm = FALSE) gset_has_missings(x) gset_cardinality(x, type = c("absolute", "relative"), na.rm = FALSE) gset_union(...) gset_sum(...) gset_difference(...) gset_product(...) gset_mean(x, y, type = c("arithmetic", "geometric", "harmonic")) gset_intersection(...) gset_symdiff(...) gset_complement(x, y) gset_power(x) gset_cartesian(...) gset_combn(x, m) e(x, memberships = 1L) is_element(e) ## S3 method for class 'gset' cut(x, level = 1, type = c("alpha", "nu"), strict = FALSE, ...) ## S3 method for class 'gset' mean(x, ..., na.rm = FALSE) ## S3 method for class 'gset' ## median(x, na.rm = FALSE, ...) [R >= 3.4.0] ## median(x, na.rm) [R < 3.4.0] ## S3 method for class 'gset' length(x) ## S3 method for class 'gset' lengths(x, use.names = TRUE)
x |
For |
y |
A (g)set object. |
e |
An object of class |
filter |
Optional vector of elements to be filtered. |
m |
Number of elements to choose. |
support |
A set of elements giving the support of the gset (elements with non-zero memberships). Must be a subset of the universe, if specified. |
memberships |
For an (“ordinary”) set: 1L (or simply missing).
For a fuzzy set: a value between 0 and 1. For a multiset: a
positive integer. For a fuzzy multiset: a list of
multisets with elements from the unit interval (or a list of vectors
interpreted as such).
Otherwise, the argument will be transformed using |
elements |
A set (or list) of |
charfun |
A function taking an object and returning the membership. |
bound |
Integer used to compute the absolute complement for
(fuzzy) multisets. If |
assume_numeric_memberships |
When applying |
FUN |
A function, to be applied to a membership vector. |
type |
For |
strict |
Logical indicating whether the cut level must be exceeded strictly (“greater than”) or not (“greater than or equal”). |
height |
Double from the unit interval for scaling memberships. |
universe |
An optional set of elements. If |
method |
|
level |
The minimum membership level. |
na.rm |
logical indicating whether |
use.names |
logical; should the names of |
... |
For |
These functions represent basic infrastructure for handling generalized sets of general (R) objects.
A generalized set (or gset) is set of pairs , where
is some set element and
is the characteristic (or
membership) function. For (“ordinary”) sets
maps to
,
for fuzzy sets into the unit interval, for multisets into the natural
numbers, and for fuzzy multisets
maps to the set of multisets
over the unit interval.
The gset_is_foo()
predicates
are vectorized. In addition
to the methods defined, one can use the following operators:
|
for the union, &
for the
intersection, +
for the sum, -
for
the difference, %D%
for the symmetric difference,
*
and ^n
for the
(-fold) cartesian product,
2^
for the power set,
%e%
for the element-of predicate,
<
and <=
for
the (proper) subset predicate, >
and >=
for
the (proper) superset predicate, and ==
and !=
for
(in)equality.
The Summary
methods do also work if
defined for the set elements.
The mean
and median
methods try to convert the object to a numeric vector before calling
the default methods. set_combn
returns the gset of all
subsets of specified length.
gset_support
, gset_core
, and gset_peak
return the set of elements with memberships greater than zero, equal
to one, and equal to the maximum membership, respectively.
gset_memberships
returns the membership
vector(s) of a given (tuple of) gset(s), optionally
restricted to the elements specified by filter
.
gset_height
returns only
the largest membership degree.
gset_cardinality
computes either the absolute or the
relative cardinality, i.e. the memberships sum, or the absolute
cardinality divided by the number of elements, respectively.
The length
method for gsets gives the (absolute) cardinality.
The lengths
method coerces the set to a list
before applying the length
method on its elements.
gset_transform_memberships
applies function FOO
to
the membership vector of the supplied gset and returns the transformed
gset. The transformed memberships are guaranteed to be in the unit
interval.
gset_concentrate
and gset_dilate
are convenience
functions, using the square and the square root,
respectively. gset_normalize
divides the memberships by their
maximum and scales with height
.
gset_product
(gset_mean
) of some gsets
compute the gset with the corresponding memberships multiplied (averaged).
The cut
method provides both - and
-cuts.
-cuts “filter” all elements with memberships
greater than (or equal to)
level
—the result, thus, is a crisp
(multi)set. -cuts select those elements with a
multiplicity exceeding
level
(only sensible for (fuzzy) multisets).
Because set elements are unordered, it is not allowed to use
positional indexing. However, it is possible to
do indexing using element labels or
simply the elements themselves (useful, e.g., for subassignment).
In addition, it is possible to iterate over
all elements using for
and lapply
/sapply
.
gset_contains_element
is vectorized in e
, that is, if e
is an atomic vector or list, the is-element operation is performed
element-wise, and a logical vector returned. Note that, however,
objects of class tuple
are taken as atomic objects to
correctly handle sets of tuples.
D. Meyer and K. Hornik (2009), Generalized and customizable sets in R, Journal of Statistical Software 31(2), 1–27. doi:10.18637/jss.v031.i02.
set
for “ordinary” sets,
gset_outer
, and
tuple
for tuples (“vectors”).
## multisets (A <- gset(letters[1:5], memberships = c(3, 2, 1, 1, 1))) (B <- gset(c("a", "c", "e", "f"), memberships = c(2, 2, 1, 2))) rep(B, 2) gset_memberships(tuple(A, B), c("a","c")) gset_union(A, B) gset_intersection(A, B) gset_complement(A, B) gset_is_multiset(A) gset_sum(A, B) gset_difference(A, B) ## fuzzy sets (A <- gset(letters[1:5], memberships = c(1, 0.3, 0.8, 0.6, 0.2))) (B <- gset(c("a", "c", "e", "f"), memberships = c(0.7, 1, 0.4, 0.9))) cut(B, 0.5) A * B A <- gset(3L, memberships = 0.5, universe = 1:5) !A ## fuzzy multisets (A <- gset(c("a", "b", "d"), memberships = list(c(0.3, 1, 0.5), c(0.9, 0.1), gset(c(0.4, 0.7), c(1, 2))))) (B <- gset(c("a", "c", "d", "e"), memberships = list(c(0.6, 0.7), c(1, 0.3), c(0.4, 0.5), 0.9))) gset_union(A, B) gset_intersection(A, B) gset_complement(A, B) ## other operations mean(gset(1:3, c(0.1,0.5,0.9))) median(gset(1:3, c(0.1,0.5,0.9))) ## vectorization list(gset(1, 0.5), gset(2, 2L), gset()) <= gset(1, 2L)
## multisets (A <- gset(letters[1:5], memberships = c(3, 2, 1, 1, 1))) (B <- gset(c("a", "c", "e", "f"), memberships = c(2, 2, 1, 2))) rep(B, 2) gset_memberships(tuple(A, B), c("a","c")) gset_union(A, B) gset_intersection(A, B) gset_complement(A, B) gset_is_multiset(A) gset_sum(A, B) gset_difference(A, B) ## fuzzy sets (A <- gset(letters[1:5], memberships = c(1, 0.3, 0.8, 0.6, 0.2))) (B <- gset(c("a", "c", "e", "f"), memberships = c(0.7, 1, 0.4, 0.9))) cut(B, 0.5) A * B A <- gset(3L, memberships = 0.5, universe = 1:5) !A ## fuzzy multisets (A <- gset(c("a", "b", "d"), memberships = list(c(0.3, 1, 0.5), c(0.9, 0.1), gset(c(0.4, 0.7), c(1, 2))))) (B <- gset(c("a", "c", "d", "e"), memberships = list(c(0.6, 0.7), c(1, 0.3), c(0.4, 0.5), 0.9))) gset_union(A, B) gset_intersection(A, B) gset_complement(A, B) ## other operations mean(gset(1:3, c(0.1,0.5,0.9))) median(gset(1:3, c(0.1,0.5,0.9))) ## vectorization list(gset(1, 0.5), gset(2, 2L), gset()) <= gset(1, 2L)
Interval class for countable and uncountable numeric sets.
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
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
x |
For |
y |
An interval object (or any other R object 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
|
domain |
Character string specifying the domain of the interval:
|
n |
Integer exponent. |
... |
Interval objects (or other R objects coercible to interval objects). |
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).
For the predicates: a logical value. For all other functions: an interval object.
set
and gset
for finite (generalized) sets.
#### * 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)
#### * 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)
Creates “nice” labels from objects.
LABELS(x, max_width = NULL, dots = "...", unique = FALSE, limit = NULL, ...) LABEL(x, limit = NULL, ...) ## S3 method for class 'character' LABEL(x, limit = NULL, quote = sets_options("quote"), ...)
LABELS(x, max_width = NULL, dots = "...", unique = FALSE, limit = NULL, ...) LABEL(x, limit = NULL, ...) ## S3 method for class 'character' LABEL(x, limit = NULL, quote = sets_options("quote"), ...)
x |
For |
max_width |
Integer vector (recycled as needed) specifying the
maximum label width for each component of |
dots |
A character string appended to a truncated label.
If |
unique |
Logical indicating whether
|
limit |
Maximum length of vectors or sets to be represented as is. Longer elements will be replaced by a label. |
quote |
Should character strings be quoted, or not?
(default: |
... |
Optional arguments passed to the |
A character vector of labels generated from the supplied object(s).
LABELS
first checks whether the object has names and uses these
if any; otherwise, LABEL
is called for each element to generate
a “short” representation.
LABEL
is generic to allow user extensions.
The current methods return the result of format
if the
argument is of length 1 (for objects of classes set
and
tuple
: by default of length 5), and create a simple class
information otherwise.
LABELS(list(1, "test", X = "1", 1:5)) LABELS(set(X = as.tuple(1:20), "test", list(list(list(1,2))))) LABELS(set(pair(1,2), set("a", 2), as.tuple(1:10))) LABELS(set(pair(1,2), set("a", 2), as.tuple(1:10)), limit = 11)
LABELS(list(1, "test", X = "1", 1:5)) LABELS(set(X = as.tuple(1:20), "test", list(list(list(1,2))))) LABELS(set(pair(1,2), set("a", 2), as.tuple(1:10))) LABELS(set(pair(1,2), set("a", 2), as.tuple(1:10)), limit = 11)
Function for getting and setting options for the sets package.
sets_options(option, value)
sets_options(option, value)
option |
character string indicating the option to get or set (see details). If missing, all options are returned as a list. |
value |
Value to be set. If omitted, the current value is returned. |
Currently, the following options are available:
"quote"
:logical specifying whether labels for
character elements are quoted or not (default: TRUE
).
"hash"
:logical specifying whether set elements
are hashed or not (default: TRUE
).
"matchfun"
:the default matching function for
cset
(default: NULL
).
"orderfun"
:the default ordering function for
cset
(default: NULL
).
"universe"
:the default universe for
generalized sets (default: NULL
).
sets_options() sets_options("quote", TRUE) print(set("a")) sets_options("quote", FALSE) print(set("a"))
sets_options() sets_options("quote", TRUE) print(set("a")) sets_options("quote", FALSE) print(set("a"))
Outer “product” of (g)sets (tuples).
set_outer(X, Y, FUN = "*", ..., SIMPLIFY = TRUE, quote = FALSE) gset_outer(X, Y, FUN = "*", ..., SIMPLIFY = TRUE, quote = FALSE) cset_outer(X, Y, FUN = "*", ..., SIMPLIFY = TRUE, quote = FALSE) tuple_outer(X, Y, FUN = "*", ..., SIMPLIFY = TRUE, quote = FALSE)
set_outer(X, Y, FUN = "*", ..., SIMPLIFY = TRUE, quote = FALSE) gset_outer(X, Y, FUN = "*", ..., SIMPLIFY = TRUE, quote = FALSE) cset_outer(X, Y, FUN = "*", ..., SIMPLIFY = TRUE, quote = FALSE) tuple_outer(X, Y, FUN = "*", ..., SIMPLIFY = TRUE, quote = FALSE)
X , Y
|
Set (tuple) objects or vectors. If |
FUN |
A function or function name (character string). |
SIMPLIFY |
Logical. If |
quote |
logical indicating whether the character strings used for the row and column names of the returned matrix should be quoted. |
... |
Additional arguments passed to the |
This function applies FUN
to all pairs of elements specified in
X
and Y
. Basically intended as a replacement for
outer
for sets (tuples), it will also accept any vector for
X
and Y
. The return value will be a matrix of dimension
length(X)
times length(Y)
, atomic or recursive
depending on the complexity of FUN
's return type and the
SIMPLIFY
argument.
set_outer(set(1,2), set(1,2,3), "/") X <- set_outer(set(1,2), set(1,2,3), pair) X[[1,1]] Y <- set_outer(set(1,2), set(1,2,3), set) Y[[1,1]] set_outer(2 ^ set(1,2,3), set_is_subset) tuple_outer(pair(1,2), triple(1,2,3)) tuple_outer(1:5, 1:4, "^")
set_outer(set(1,2), set(1,2,3), "/") X <- set_outer(set(1,2), set(1,2,3), pair) X[[1,1]] Y <- set_outer(set(1,2), set(1,2,3), set) Y[[1,1]] set_outer(2 ^ set(1,2,3), set_is_subset) tuple_outer(pair(1,2), triple(1,2,3)) tuple_outer(1:5, 1:4, "^")
Plot and lines functions for (tuples of) generalized sets and function generators of characteristic functions.
## S3 method for class 'gset' plot(x, type = NULL, ylim = NULL, xlab = "Universe", ylab = "Membership Grade", ...) ## S3 method for class 'cset' plot(x, ...) ## S3 method for class 'set' plot(x, ...) ## S3 method for class 'tuple' plot(x, type = "l", ylim = NULL, xlab = "Universe", ylab = "Membership Grade", col = 1, continuous = TRUE, ...) ## S3 method for class 'charfun_generator' plot(x, universe = NULL, ...) ## S3 method for class 'gset' lines(x, type = "l", col = 1, continuous = TRUE, universe = NULL, ...) ## S3 method for class 'cset' lines(x, ...) ## S3 method for class 'set' lines(x, ...) ## S3 method for class 'tuple' lines(x, col = 1, universe = NULL, ...) ## S3 method for class 'charfun_generator' lines(x, universe = NULL, ...)
## S3 method for class 'gset' plot(x, type = NULL, ylim = NULL, xlab = "Universe", ylab = "Membership Grade", ...) ## S3 method for class 'cset' plot(x, ...) ## S3 method for class 'set' plot(x, ...) ## S3 method for class 'tuple' plot(x, type = "l", ylim = NULL, xlab = "Universe", ylab = "Membership Grade", col = 1, continuous = TRUE, ...) ## S3 method for class 'charfun_generator' plot(x, universe = NULL, ...) ## S3 method for class 'gset' lines(x, type = "l", col = 1, continuous = TRUE, universe = NULL, ...) ## S3 method for class 'cset' lines(x, ...) ## S3 method for class 'set' lines(x, ...) ## S3 method for class 'tuple' lines(x, col = 1, universe = NULL, ...) ## S3 method for class 'charfun_generator' lines(x, universe = NULL, ...)
x |
For a method for class foo, an object of class foo. |
type |
Same as the |
universe |
Universal set used for setting up the plot region. By default, this is deduced from the object(s) to be plotted. |
col |
Character or integer vector specifying the color of the object(s) to be plotted. |
continuous |
Logical indicating whether zero membership degrees “inside” the graph should be ignored. |
xlab , ylab
|
Character labels for the axes. |
ylim |
Double vector of length 2 defining the range of the y axis. |
... |
Further arguments passed to the default plot methods. |
The main argument (invisibly).
set
, gset
, and tuple
for the
set types, and fuzzy_normal
for available characteristic
functions.
## basic plots plot(gset(1:3, 1:3/3)) plot(gset(1:3, 1:3/3, universe = 0:4)) plot(gset(c("a", "b"), list(1:2/2, 0.3))) ## characteristic functions plot(fuzzy_normal) plot(tuple(fuzzy_normal, fuzzy_bell), col = 1:2) plot(fuzzy_pi3_gset(min = 2, max = 15)) ## superposing plots using lines() x <- fuzzy_normal_gset() y <- fuzzy_trapezoid_gset(corners = c(5, 10, 15, 17), height = c(0.7, 1)) plot(tuple(x, y)) lines(x | y, col = 2) lines(x & y, col = 3) ## another example using gset_mean x <- fuzzy_two_normals_gset(sd = c(2, 1)) y <- fuzzy_trapezoid_gset(corners = c(5, 9, 11, 15)) plot(tuple(x, y)) lines(tuple(gset_mean(x, y), gset_mean(x, y, "geometric"), gset_mean(x, y, "harmonic")), col = 2:4) ## creating a sequence of sets plot(fuzzy_tuple(fuzzy_cone, 10), col = gray.colors(10))
## basic plots plot(gset(1:3, 1:3/3)) plot(gset(1:3, 1:3/3, universe = 0:4)) plot(gset(c("a", "b"), list(1:2/2, 0.3))) ## characteristic functions plot(fuzzy_normal) plot(tuple(fuzzy_normal, fuzzy_bell), col = 1:2) plot(fuzzy_pi3_gset(min = 2, max = 15)) ## superposing plots using lines() x <- fuzzy_normal_gset() y <- fuzzy_trapezoid_gset(corners = c(5, 10, 15, 17), height = c(0.7, 1)) plot(tuple(x, y)) lines(x | y, col = 2) lines(x & y, col = 3) ## another example using gset_mean x <- fuzzy_two_normals_gset(sd = c(2, 1)) y <- fuzzy_trapezoid_gset(corners = c(5, 9, 11, 15)) plot(tuple(x, y)) lines(tuple(gset_mean(x, y), gset_mean(x, y, "geometric"), gset_mean(x, y, "harmonic")), col = 2:4) ## creating a sequence of sets plot(fuzzy_tuple(fuzzy_cone, 10), col = gray.colors(10))
Creation and manipulation of sets.
set(...) as.set(x) make_set_with_order(x) is.set(x) set_is_empty(x) set_is_subset(x, y) set_is_proper_subset(x, y) set_is_equal(x, y) set_contains_element(x, e) set_union(...) set_intersection(...) set_symdiff(...) set_complement(x, y) set_cardinality(x) ## S3 method for class 'set' length(x) ## S3 method for class 'set' lengths(x, use.names = TRUE) set_power(x) set_cartesian(...) set_combn(x, m)
set(...) as.set(x) make_set_with_order(x) is.set(x) set_is_empty(x) set_is_subset(x, y) set_is_proper_subset(x, y) set_is_equal(x, y) set_contains_element(x, e) set_union(...) set_intersection(...) set_symdiff(...) set_complement(x, y) set_cardinality(x) ## S3 method for class 'set' length(x) ## S3 method for class 'set' lengths(x, use.names = TRUE) set_power(x) set_cartesian(...) set_combn(x, m)
x |
For |
y |
A set object. |
e |
An R object. |
m |
Number of elements to choose. |
use.names |
logical; should the names of |
... |
For |
These functions represent basic infrastructure for handling sets
of general (R) objects. The set_is_foo()
predicates
are vectorized. In addition
to the methods defined, one can use the following operators:
|
for the union,
-
for the difference (or complement), &
for the
intersection, %D%
for the symmetric difference,
*
and ^n
for the
(-fold) cartesian product,
2^
for the power set,
%e%
for the element-of predicate,
<
and <=
for
the (proper) subset predicate, >
and >=
for
the (proper) superset predicate, and ==
and !=
for
(in)equality. The length
method for sets gives the
cardinality. The lengths
method coerces the set to a list
before applying the length
method on its elements.
set_combn
returns the set of all
subsets of specified length. The Summary
methods do also work if
defined for the set elements. The mean
and
median
methods try to convert the object to a numeric vector before calling
the default methods.
Because set elements are unordered, it is not allowed to use
positional indexing. However, it is possible to
do indexing using element labels or
simply the elements themselves (useful, e.g., for subassignment).
In addition, it is possible to iterate over
all elements using for
and lapply
/sapply
.
Note that converting objects to sets may change the internal order
of the elements, so that iterating over the original data
might give different results than iterating over the corresponding
set. The permutation can be obtained using the generic function
make_set_with_order
, returning both the set and the ordering.
as.set
simply calls
make_set_with_order
internally and strips the order
information, so user-defined
methods for coercion have to be provided for the latter and not for
as.set
.
Note that set_union
, set_intersection
, and
set_symdiff
accept any number of arguments. The -ary
symmetric difference of sets contains
just elements which are in an odd number of the sets.
set_contains_element
is vectorized in e
, that is, if e
is an atomic vector or list, the is-element operation is performed
element-wise, and a logical vector returned. Note that, however,
objects of class tuple
are taken as atomic objects to
correctly handle sets of tuples.
For the predicate functions, a vector of logicals.
For make_set_with_order
,
a list with two components "set"
and "order"
. For
set_cardinality
and the length method, an integer value.
For the lengths
method, an integer vector. For all
others, a set.
D. Meyer and K. Hornik (2009), Generalized and customizable sets in R, Journal of Statistical Software 31(2), 1–27. doi:10.18637/jss.v031.i02.
set_outer
,
gset
for generalized sets,
and tuple
for tuples (“vectors”).
## constructor s <- set(1L, 2L, 3L) s ## named elements snamed <- set(one = 1, 2, three = 3) snamed ## indexing by label snamed[["one"]] ## subassignment snamed[c(2,3)] <- c("a","b") snamed ## a more complex set set(c, "test", list(1, 2, 3)) ## converter s2 <- as.set(2:5) s2 ## converter with order make_set_with_order(5:1) ## set of sets set(set(), set(1)) ## cartesian product s * s2 s * s s ^ 2 # same as above s ^ 3 ## power set 2 ^ s ## tuples s3 <- set(tuple(1,2,3), tuple(2,3,4)) s3 ## Predicates: ## element 1:2 %e% s tuple(1,2,3) %e% s3 ## subset s <= s2 s2 >= s # same ## proper subset s < s ## complement, union, intersection, symmetric difference: s - set(1L) s + set("a") # or use: s | set("a") s & s s %D% s2 set(1,2,3) - set(1,2) set_intersection(set(1,2,3), set(2,3,4), set(3,4,5)) set_union(set(1,2,3), set(2,3,4), set(3,4,5)) set_symdiff(set(1,2,3), set(2,3,4), set(3,4,5)) ## subsets: set_combn(as.set(1:3),2) ## iterators: sapply(s, sqrt) for (i in s) print(i) ## Summary methods sum(s) range(s) ## mean / median mean(s) median(s) ## cardinality s <- set(1, list(1, 2)) length(s) lengths(s) ## vectorization list(set(1), set(2), set()) == set(1)
## constructor s <- set(1L, 2L, 3L) s ## named elements snamed <- set(one = 1, 2, three = 3) snamed ## indexing by label snamed[["one"]] ## subassignment snamed[c(2,3)] <- c("a","b") snamed ## a more complex set set(c, "test", list(1, 2, 3)) ## converter s2 <- as.set(2:5) s2 ## converter with order make_set_with_order(5:1) ## set of sets set(set(), set(1)) ## cartesian product s * s2 s * s s ^ 2 # same as above s ^ 3 ## power set 2 ^ s ## tuples s3 <- set(tuple(1,2,3), tuple(2,3,4)) s3 ## Predicates: ## element 1:2 %e% s tuple(1,2,3) %e% s3 ## subset s <= s2 s2 >= s # same ## proper subset s < s ## complement, union, intersection, symmetric difference: s - set(1L) s + set("a") # or use: s | set("a") s & s s %D% s2 set(1,2,3) - set(1,2) set_intersection(set(1,2,3), set(2,3,4), set(3,4,5)) set_union(set(1,2,3), set(2,3,4), set(3,4,5)) set_symdiff(set(1,2,3), set(2,3,4), set(3,4,5)) ## subsets: set_combn(as.set(1:3),2) ## iterators: sapply(s, sqrt) for (i in s) print(i) ## Summary methods sum(s) range(s) ## mean / median mean(s) median(s) ## cardinality s <- set(1, list(1, 2)) length(s) lengths(s) ## vectorization list(set(1), set(2), set()) == set(1)
Similarities and dissimilarities for (generalized) sets.
set_similarity(x, y, method = "Jaccard") gset_similarity(x, y, method = "Jaccard") cset_similarity(x, y, method = "Jaccard") set_dissimilarity(x, y, method = c("Jaccard", "Manhattan", "Euclidean", "L1", "L2")) gset_dissimilarity(x, y, method = c("Jaccard", "Manhattan", "Euclidean", "L1", "L2")) cset_dissimilarity(x, y, method = c("Jaccard", "Manhattan", "Euclidean", "L1", "L2"))
set_similarity(x, y, method = "Jaccard") gset_similarity(x, y, method = "Jaccard") cset_similarity(x, y, method = "Jaccard") set_dissimilarity(x, y, method = c("Jaccard", "Manhattan", "Euclidean", "L1", "L2")) gset_dissimilarity(x, y, method = c("Jaccard", "Manhattan", "Euclidean", "L1", "L2")) cset_dissimilarity(x, y, method = c("Jaccard", "Manhattan", "Euclidean", "L1", "L2"))
x , y
|
Two (generalized/customizable) sets. |
method |
Character string specifying the proximity method (see below). |
For two generalized sets and
, the
Jaccard
similarity is where
denotes the cardinality for
generalized sets (sum of memberships). The
Jaccard
dissimilarity is 1 minus the similarity.
The L1
(or Manhattan
) and L2
(or
Euclidean
)
dissimilarities are defined as
follows. For two fuzzy multisets and
on a
given universe
with elements
, let
and
be functions returning the memberships of an
element
in sets
and
, respectively. The
memberships are returned in standard form,
i.e. as an infinite vector of decreasing membership
values, e.g.
.
Let
and
denote the
th components of these
membership vectors. Then the L1 distance is defined as:
and the L2 distance as:
A numeric value (similarity or dissimilarity, as specified).
T. Matthe, R. De Caluwe, G. de Tre, A. Hallez, J. Verstraete, M. Leman, O. Cornelis, D. Moelants, and J. Gansemans (2006), Similarity Between Multi-valued Thesaurus Attributes: Theory and Application in Multimedia Systems, Flexible Query Answering Systems, Lecture Notes in Computer Science, Springer, 331–342.
K. Mizutani, R. Inokuchi, and S. Miyamoto (2008), Algorithms of Nonlinear Document Clustering Based on Fuzzy Multiset Model, International Journal of Intelligent Systems, 23, 176–198.
set
.
A <- set("a", "b", "c") B <- set("c", "d", "e") set_similarity(A, B) set_dissimilarity(A, B) A <- gset(c("a", "b", "c"), c(0.3, 0.7, 0.9)) B <- gset(c("c", "d", "e"), c(0.2, 0.4, 0.5)) gset_similarity(A, B, "Jaccard") gset_dissimilarity(A, B, "Jaccard") gset_dissimilarity(A, B, "L1") gset_dissimilarity(A, B, "L2") A <- gset(c("a", "b", "c"), list(c(0.3, 0.7), 0.1, 0.9)) B <- gset(c("c", "d", "e"), list(0.2, c(0.4, 0.5), 0.8)) gset_similarity(A, B, "Jaccard") gset_dissimilarity(A, B, "Jaccard") gset_dissimilarity(A, B, "L1") gset_dissimilarity(A, B, "L2")
A <- set("a", "b", "c") B <- set("c", "d", "e") set_similarity(A, B) set_dissimilarity(A, B) A <- gset(c("a", "b", "c"), c(0.3, 0.7, 0.9)) B <- gset(c("c", "d", "e"), c(0.2, 0.4, 0.5)) gset_similarity(A, B, "Jaccard") gset_dissimilarity(A, B, "Jaccard") gset_dissimilarity(A, B, "L1") gset_dissimilarity(A, B, "L2") A <- gset(c("a", "b", "c"), list(c(0.3, 0.7), 0.1, 0.9)) B <- gset(c("c", "d", "e"), list(0.2, c(0.4, 0.5), 0.8)) gset_similarity(A, B, "Jaccard") gset_dissimilarity(A, B, "Jaccard") gset_dissimilarity(A, B, "L1") gset_dissimilarity(A, B, "L2")
Creation and manipulation of tuples.
tuple(...) as.tuple(x) is.tuple(x) singleton(...) pair(...) triple(...) tuple_is_singleton(x) tuple_is_pair(x) tuple_is_triple(x) tuple_is_ntuple(x, n)
tuple(...) as.tuple(x) is.tuple(x) singleton(...) pair(...) triple(...) tuple_is_singleton(x) tuple_is_pair(x) tuple_is_triple(x) tuple_is_ntuple(x, n)
x |
An R object. |
n |
A non-negative integer. |
... |
Possibly named R objects (for |
These functions represent basic infrastructure for handling tuples of
general (R) objects. Class tuple
is used in particular to
correctly handle cartesian products of sets. Although tuple objects
should behave like “ordinary” vectors, some operations might
yield unexpected results since tuple objects are in fact list objects
internally. The Summary
methods do work if
defined for the set elements. The mean
and
median
methods try to convert the object to a numeric vector before calling
the default methods.
set
.
## Constructor. tuple(1,2,3, TRUE) triple(1,2,3) pair(Name = "David", Height = 185) tuple_is_triple(triple(1,2,3)) tuple_is_ntuple(tuple(1,2,3,4), 4) ## Converter. as.tuple(1:3) ## Operations. c(tuple("a","b"), 1) tuple(1,2,3) * tuple(2,3,4) rep(tuple(1,2,3), 2) min(tuple(1,2,3)) sum(tuple(1,2,3))
## Constructor. tuple(1,2,3, TRUE) triple(1,2,3) pair(Name = "David", Height = 185) tuple_is_triple(triple(1,2,3)) tuple_is_ntuple(tuple(1,2,3,4), 4) ## Converter. as.tuple(1:3) ## Operations. c(tuple("a","b"), 1) tuple(1,2,3) * tuple(2,3,4) rep(tuple(1,2,3), 2) min(tuple(1,2,3)) sum(tuple(1,2,3))