Learn R Programming

Matrix (version 1.2-8)

pMatrix-class: Permutation matrices

Description

The "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.

Arguments

Objects from the Class

Objects can be created by calls of the form new("pMatrix", ...) or by coercion from an integer permutation vector, see below.

Slots

Extends

Class "indMatrix", directly.

Methods

Details

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):
$MP $ = M %*% P =
M[, i(p)] $PM $ = P %*% M
= M[ p , ] $P'M$ =
crossprod(P,M) ($~=$t(P) %*% M) = M[i(p), ] $MP'$
= tcrossprod(M,P) ($~=$M %*% t(P)) = M[ , p ]
where p is the “permutation vector” corresponding to the permutation matrix P (see first note), and i(p) is short for invPerm(p).

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$.

See Also

invPerm(p) computes the inverse permutation of an integer (index) vector p.

Examples

Run this code
(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, "ngTMatrix")
p10[1:7, 1:4] # gives an "ngTMatrix" (most economic!)

## row-indexing of a <pMatrix> keeps it as an <indMatrix>:
p10[1:3, ]

Run the code above in your browser using DataLab