-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpermutations_pv.R
118 lines (109 loc) · 3.63 KB
/
permutations_pv.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# Permutations Pseudo-vector
setClass(
Class = "PPV",
representation(k = "numeric", items = "vector"),
)
setMethod(
f = "show",
signature = "PPV",
definition = function(object) cat(
sprintf(
paste(
"Instance of class PPV\n",
"Pseudo-vector containing %s %s-permutations\n",
"of items taken from the list:\n[%s]",
collapse = ""
),
length(object),
object@k,
paste(object@items, collapse = ", ")
)
)
)
#' Permutations Pseudo-Vector Length
#' @description
#' Get the length of a \code{PPV} instance.
#' @param x an instance of \code{PPV}
#' @return the number of permutations in pseudo-vector \code{x}
#' @details
#' Since \code{x} contains all the \code{k}-permutations of objects in vector
#' \code{items}, \code{length(x)} will return
#' \code{choose(length(items), k) * factorial(k)}.
#' @seealso Combinations Pseudo-Vector \code{\link{cpv}}
#' @seealso Amalgams Pseudo-Vector \code{\link{apv}}
#' @seealso Selections Pseudo-Vector \code{\link{spv}}
#' @seealso Subsets Pseudo-Vector \code{\link{sspv}}
setMethod(
f = "length",
signature = "PPV",
definition = function(x) choose(length(x@items), x@k) * factorial(x@k)
)
#' Retrieve a Permutation by Index
#' @description
#' Access a permutation stored in a \code{PPV} instance by index.
#' @param x an instance of \code{PPV}.
#' @param i an index specifying the position of the sought permutation.
#' @param j not used.
#' @param drop not used.
#' @return the permutation located at position \code{i} in pseudo-vector \code{x}
#' @details
#' The permutation at index \code{i} of pseudo-vector \code{x} is not actually
#' stored in memory but calculated as needed. The extract method is used solely
#' for interpretation.
#'
#' @seealso Combinations Pseudo-Vector \code{\link{cpv}}
#' @seealso Amalgams Pseudo-Vector \code{\link{apv}}
#' @seealso Selections Pseudo-Vector \code{\link{spv}}
#' @seealso Subsets Pseudo-Vector \code{\link{sspv}}
setMethod(
f = "[",
signature = "PPV",
definition = function(x, i, j, drop) {
i <- index.check(i, length(x))
if (!missing(j)) cat("Warning: Only first coordinate used.\n")
permutation(i, x@k, x@items)
}
)
# Export #######################################################################
#' Permutations Pseudo-Vector Constructor
#' @description
#' The \code{PPV} class defines a pseudo-vector containing all
#' the \code{k}-permutations of the objects stored
#' in \code{items}. The function \code{ppv} is a constructor for this class.
#' @aliases
#' permutation
#' permutations
#' @param k the number of objects taken at a time.
#' @param items a vector of objects to be permuted.
#' @return an instance of \code{PPV}.
#' @author Richard Ambler
#' @details
#' The arrangement of permutations is similar, but in many cases not identical,
#' to that obtained from the
#' Steinhaus-Johnson-Trotter algorithm (see references).
#' @examples
#' # create a pseudo-vector of 5-permutations from the first 10 letters
#' p <- ppv(5, letters[1:10])
#' # generate a description
#' print(p)
#' # compatable with length
#' length(p)
#' # inspect a few of the permutations "stored" in p
#' p[1]
#' p[1000]
#' p[30240]
#' @seealso Combinations Pseudo-Vector \code{\link{cpv}}
#' @seealso Amalgams Pseudo-Vector \code{\link{apv}}
#' @seealso Selections Pseudo-Vector \code{\link{spv}}
#' @seealso Subsets Pseudo-Vector \code{\link{sspv}}
#' @references
#' Steinhaus-Johnson-Trotter algorithm. (2014, April 29).
#' In \emph{Wikipedia, The Free Encyclopedia}.
#' Retrieved 13:24, September 5, 2014
#' @export
#' @import methods
ppv <- function(k, items) new(
Class = "PPV",
k = k,
items = items
)