| pMatrix-class {Matrix} | R Documentation |
The "pMatrix" class is the class of permutation
matrices, stored as 1-based integer permutation vectors.
Objects can be created by calls of the form new("pMatrix", ...)
or by coercion from an integer permutation vector, see below.
perm:Dim[1] whose elements form a
permutation of 1:Dim[1].Dim:"integer". The dimensions
of the matrix which must be a two-element vector of equal,
non-negative integers.Dimnames:character vector length
equal the corresponding Dim element.
Class "sparseMatrix" and
"generalMatrix", directly.
signature(x = "matrix", y = "pMatrix") and other
signatures (use showMethods("%*%", class="pMatrix")): ... signature(from = "integer", to = "pMatrix"):
This is enables typical "pMatrix" construction, given
a permutation vector of 1:n, see the first example.signature(from = "numeric", to = "pMatrix"):
a user convenience, to allow as(perm, "pMatrix") for
numeric perm with integer values.signature(from = "pMatrix", to = "matrix"): ... signature(from = "pMatrix", to = "ngTMatrix"):
coercion to sparse logical matrix of class ngTMatrix.signature(a = "pMatrix", b = "missing"): return
the inverse permutation matrix.signature(x = "pMatrix"): return the transpose of
the permuation matrix (which is also the inverse of the
permutation matrix).
The inverse of the typical "pMatrix" constructor,
P <- as(ip, "pMatrix") is simply ip <- P@perm.
Subsetting (“indexing”) "pMatrix" objects treats them as
nonzero-pattern matrices, i.e., as "linkS4class{ngTMatrix}"
such that non-matrix subsetting result in logical
vectors. Sub-assignment (M[i,j] <- v) is not sensible and
hence an error for these permutation matrices.
(pm1 <- as(as.integer(c(2,3,1)), "pMatrix")) t(pm1) # is the same as solve(pm1) pm1 %*% t(pm1) # check that the transpose is the inverse stopifnot(identical(diag(3), as(pm1 %*% t(pm1), "matrix"))) set.seed(11) ## random permutation matrix : (p10 <- as(sample(10),"pMatrix")) ## Permute rows / columns of a numeric matrix : (mm <- round(array(rnorm(3 * 3), c(3, 3)), 2)) mm %*% pm1 pm1 %*% mm try(as(as.integer(c(3,3,1)), "pMatrix"))# Error: not a permutation as(pm1, "ngTMatrix") p10[1:7, 1:4] # gives an "ngTMatrix" (most economic!)