"pMatrix" class is the class of permutation
matrices, stored as 1-based integer permutation vectors.
Matrix (vector) multiplication with permutation matrices is equivalent to row or column permutation, and is implemented that way in the Matrix package, see the ‘Details’ below.
Matrix multiplication with permutation matrices is equivalent to row or column permutation. Here are the four different cases for an arbitrary matrix M and a permutation matrix P (where we assume matching dimensions):
p is the “permutation vector” corresponding to the
P (see first note), and
i(p) is short
Also one could argue that these are really only two cases if you take
into account that inversion (
solve) and transposition
t) are the same for permutation matrices P.
Objects can be created by calls of the form
or by coercion from an integer permutation vector, see below.
An integer, 1-based permutation vector, i.e.
an integer vector of length
Dim whose elements form a
Object of class
"integer". The dimensions
of the matrix which must be a two-element vector of equal,
list of length two; each component
containing NULL or a
character vector length
equal the corresponding
signature(x = "matrix", y = "pMatrix") and other
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
perm with integer values.
signature(from = "pMatrix", to = "matrix"):
coercion to a traditional FALSE/TRUE
(in earlier version of Matrix, it resulted in a 0/1-integer
logical makes slightly more sense, corresponding
better to the “natural” sparseMatrix counterpart,
signature(from = "pMatrix", to = "ngTMatrix"):
coercion to sparse logical matrix of class
signature(x = "pMatrix", logarithm="logical"):
Since permutation matrices are orthogonal, the determinant must be
+1 or -1. In fact, it is exactly the sign of the
signature(a = "pMatrix", b = "missing"): return
the inverse permutation matrix; note that
t(P) for permutation matrices. See
solve-methods for other methods.
signature(x = "pMatrix"): return the transpose of
the permutation matrix (which is also the inverse of the
For every permutation matrix
P, there is a corresponding
p (of indices, 1:n), and these are related by
P <- as(p, "pMatrix") p <- P@perm
see also the ‘Examples’.
“Row-indexing” a permutation matrix typically returns
"indMatrix" for all other
subsetting/indexing and subassignment (
A[..] <- v) operations.
invPerm(p) computes the inverse permutation of an
integer (index) vector
(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(all(diag(3) == as(pm1 %*% t(pm1), "matrix")), is.logical(as(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, "TsparseMatrix") p10[1:7, 1:4] # gives an "ngTMatrix" (most economic!) ## row-indexing of a <pMatrix> keeps it as an <indMatrix>: p10[1:3, ]
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.