relation              package:relations              R Documentation

_R_e_l_a_t_i_o_n_s

_D_e_s_c_r_i_p_t_i_o_n:

     Creation and manipulation of relations.

_U_s_a_g_e:

     relation(domain = NULL, incidence = NULL, graph = NULL, charfun = NULL)
     as.relation(x)
     is.relation(x)

_A_r_g_u_m_e_n_t_s:

  domain: List (or tuple) of (possibly named) sets (or vectors) used as
          the domain, recycled as needed to fit the arity of the
          relation. If 'domain' is not a list or tuple, it is converted
          to a list.

incidence: Array of 0/1 (or 'TRUE'/'FALSE') values.  Note that
          one-dimensional incidences are also accepted. The
          'names'/'dimnames' attribute of 'incidence' is used as domain
          if this is not explicitly given using the 'domain' argument.

   graph: Either a set of equally sized tuples, or a list of (possibly,
          generic) vectors of same length where each component
          specifies one relation element, or a data frame where each
          row specifies one relation element.  For the latter, the
          columns correspond to the domain sets, and the colnames are
          used as their labels if specified.

 charfun: A characteristic function of the relation, i.e., a predicate
          function taking k arguments, with k equal to the arity of the
          relation.

       x: an R object.

_D_e_t_a_i_l_s:

     Given k sets of objects X_1, ..., X_k, a k-ary relation R on D(R)
     = (X_1, ..., X_k) is a subset G(R) of the Cartesian product X_1 x
     ... x X_k.  We refer to D(R) and G(R) as the _domain_ and the
     _graph_ of the relation, respectively (alternative notions are
     that of _ground_ and _figure_, respectively).  We also refer to s
     = (s_1, ..., s_k), where each s_i gives the cardinality of X_i, as
     the _size_ of the relation.

     Strictly speaking, the relation is the _pair_ (D(R), G(R)); often,
     relations are identified with their graph.  We say that a k-tuple
     t is _contained_ in the relation R iff it is an element of G(R).

     The _characteristic function_ f_R (sometimes also referred to as
     indicator function) of a relation R is the predicate
     (boolean-valued) function on the Cartesian product X_1 x ... x X_k
     such that f_R(t) is true iff the k-tuple t is in G(R).

     Relations with arity 2, 3, and 4 are typically referred to as
     _binary_, _ternary_, and _quaternary_ relations, respectively.  An
     _endorelation_ on X (or binary relation _over_ X) is a binary
     relation with domain (X, X). See predicates for the most important
     types of endorelations.

     Relations with the same domain can naturally be ordered according
     to their graphs.  I.e., R_1 <= R_2 iff G(R_1) is a subset of
     G(R_2), or equivalently, if every k-tuple t contained in R_1 is
     also contained in R_2.  This induces a lattice structure, with
     meet (greatest lower bound) and join (least upper bound) the
     intersection and union of the graphs, respectively, also known as
     the _intersection_ and _union_ of the relations.  The least
     element moves metric on this lattice is the _symmetric difference
     metric_, i.e., the cardinality of the symmetric difference of the
     graphs (the number of tuples in exactly one of the relation
     graphs).

     The _complement_ of a relation R is the relation with domain D(R)
     whose graph is the complement of G(R), i.e., which contains
     exactly the tuples not contained in R.

     For binary relations R, it is customary to write x R y iff  (x, y)
     is contained in R.  For binary relations R and S with domains (X,
     Y) and (Y, Z), the _composition_ of R and S is defined by taking x
     S z iff there is an y such that x R y and y S z.  The _dual_ (or
     _converse_) R^* of the relation R with domain (X, Y) is the
     relation with domain (Y, X) such that x R^* y iff y R x.

     Package 'relations' implements finite relations as an S3 class
     which allows for a variety of representations (even though
     currently, only dense array representations of the incidences are
     employed). Other than by the generator, relations can be obtained
     by coercion via the generic function 'as.relation', which has
     methods for at least logical and numeric vectors, unordered and
     ordered factors, arrays including matrices, and data frames. 
     Unordered factors are coerced to equivalence relations; ordered
     factors and numeric vectors are coerced to order relations.
     Logical vectors give unary relations (predicates).  A (feasible)
     k-dimensional array is taken as the incidence of a k-ary relation.
      Finally, a data frame is taken as a relation table.  Note that
     missing values will be propagated in the coercion.

     Basic relation operations are available as group methods: 'min'
     and 'max' give the meet and join, and 'range' a relation ensemble
     with these two. Comparison operators implement the natural
     ordering in the relation lattice.  Where applicable, '!' gives the
     complement, '&' and '|' intersection and union, and '*'
     composition, respectively.  Finally, 't' gives the dual.

     There is a 'plot' method for certain endorelations provided that
     package 'Rgraphviz' is available.

     There is also the notion of fuzzy relations, for which each tuple
     is contained in the graph with a certain membership value.  (I.e.,
     the graph is a fuzzy subset of the Cartesian product of the
     elements of the domain.)  Basic support for fuzzy relations will
     be added eventually.

_S_e_e _A_l_s_o:

     'relation_incidence' for obtaining incidences; 'relation_domain'
     for determining domain, arity, and size; 'relation_graph' for
     determining the graph of a relation; 'relation_charfun' for
     determining the characteristic function; 'algebra' for further
     operations defined on relations.

_E_x_a_m_p_l_e_s:

     ## A relation created by specifying the graph:
     R <- relation(graph = data.frame(A = c(1, 1:3), B = c(2:4, 4)))
     relation_incidence(R)
     ## extract domain
     relation_domain(R)
     ## extract graph
     relation_graph(R)
     ## both ("a pair of domain and graph" ...)
     as.tuple(R)

     ## (Almost) the same using the set specification
     ## (the domain labels are missing).
     R <- relation(graph = set(tuple(1,2), tuple(1,3), tuple(2,4), tuple(3,4)))
     ## equivalent to:
     ## relation(graph = list(1:2, c(1,3), c(2,4), c(3,4)))
     relation_incidence(R)

     ## Explicitly specifying the domain:
     R <- relation(domain = list(A = letters[1:3], B = LETTERS[1:4]),
                   graph = set(tuple("a","B"), tuple("a","C"),
                               tuple("b","D"), tuple("c","D")))
     relation_incidence(R)

     ## Domains can be composed of arbitrary R objects:
     R <- relation(domain = set(c, "test"),
                   graph = set(tuple(c, c), tuple(c, "test")))
     relation_incidence(R)

     ## Characteristic function ("a divides b"):
     R <- relation(domain = list(1 : 10, 1 : 10),
                   charfun = function(a, b) b %% a == 0)
     relation_incidence(R)
     ## R is a partial order: plot the Hasse diagram provided that
     ## Rgraphviz is available:
     if(require("Rgraphviz")) plot(R)

     ## conversions and operators
     x <- matrix(0, 3, 3)
     R1 <- as.relation(row(x) >= col(x))
     R2 <- as.relation(row(x) <= col(x))
     R3 <- as.relation(row(x) <  col(x))
     relation_incidence(max(R1, R2))
     relation_incidence(min(R1, R2))
     R3 < R2
     relation_incidence(R1 * R2)
     relation_incidence(! R1)
     relation_incidence(t(R2))

