pub struct PermutationMatrix<T> { /* private fields */ }
Expand description

An efficient implementation of a permutation matrix.

Examples

use rulinalg::matrix::PermutationMatrix;

let ref x = matrix![1, 2, 3;
                    4, 5, 6;
                    7, 8, 9];

// Swap the two first rows of x by left-multiplying a permutation matrix
let expected = matrix![4, 5, 6;
                       1, 2, 3;
                       7, 8, 9];
let mut p = PermutationMatrix::identity(3);
p.swap_rows(0, 1);
assert_eq!(expected, p * x);

// Swap the two last columns of x by right-multiplying a permutation matrix
let expected = matrix![1, 3, 2;
                       4, 6, 5;
                       7, 9, 8];
let mut p = PermutationMatrix::identity(3);
p.swap_rows(1, 2);
assert_eq!(expected, x * p);

// One can also construct the same permutation matrix directly
// from an array representation.
let ref p = PermutationMatrix::from_array(vec![0, 2, 1]).unwrap();
assert_eq!(expected, x * p);

// One may also obtain a full matrix representation of the permutation
assert_eq!(p.as_matrix(), matrix![1, 0, 0;
                                  0, 0, 1;
                                  0, 1, 0]);

// The inverse of a permutation matrix can efficiently be obtained
let p_inv = p.inverse();

// And permutations can be composed through multiplication
assert_eq!(p * p_inv, PermutationMatrix::identity(3));

Rationale and complexity

A permutation matrix is a very special kind of matrix. It is essentially a matrix representation of the more general concept of a permutation. That is, an n x n permutation matrix corresponds to a permutation of ordered sets whose cardinality is n. In particular, given an m x n matrix A and an m x m permutation matrix P, the action of left-multiplying A by P, PA, corresponds to permuting the rows of A by the given permutation represented by P. Conversely, right-multiplication corresponds to column permutation. More precisely, given another permutation matrix Q of size n x n, then AQ is the corresponding permutation of the columns of A.

Due to their unique structure, permutation matrices can be much more efficiently represented and applied than general matrices. Recall that for general matrices X and Y of size m x m and n x n respectively, the storage of X requires O(m2) memory and the storage of Y requires O(n2) memory. Ignoring for the moment the existence of Strassen’s matrix multiplication algorithm and more theoretical alternatives, the multiplication XA requires O(m2n) operations, and the multiplication AY requires O(m``n2) operations.

By contrast, the storage of P requires only O(m) memory, and the storage of K requires O(n) memory. Moreover, the products PA and AK both require merely O(mn) operations.

Representation

A permutation of an ordered set of cardinality n is a map of the form

p: { 1, ..., n } -> { 1, ..., n }.

That is, for any index i, the permutation p sends i to some index j = p(i), and hence the map may be represented as an array of integers of length n.

By convention, an instance of PermutationMatrix represents row permutations. That is, the indices referred to above correspond to row indices, and the internal representation of a PermutationMatrix is an array describing how the permutation sends a row index i to a new row index j in the permuted matrix. Because of this internal representation, one can only efficiently swap rows of a PermutationMatrix. However, keep in mind that while this API only lets one swap individual rows of the permutation matrix itself, the right-multiplication of a general matrix with a permutation matrix will permute the columns of the general matrix, and so in practice this restriction is insignificant.

Implementations

The identity permutation.

Swaps rows i and j in the permutation matrix.

The inverse of the permutation matrix.

The size of the permutation matrix.

A permutation matrix is a square matrix, so size() is equal to both the number of rows, as well as the number of columns.

Constructs a PermutationMatrix from an array.

Errors

The supplied N-length array must satisfy the following:

  • Each element must be in the half-open range [0, N).
  • Each element must be unique.

Constructs a PermutationMatrix from an array, without checking the validity of the supplied permutation.

Safety

The supplied N-length array must satisfy the following:

  • Each element must be in the half-open range [0, N).
  • Each element must be unique.

Note that while this function itself is technically safe regardless of the input array, passing an incorrect permutation matrix may cause undefined behavior in other methods of PermutationMatrix. As such, it may be difficult to debug. The user is strongly encouraged to use the safe function from_array, which for most real world applications only incurs a minor or even insignificant performance hit as a result of the extra validation.

Maps the given row index into the resulting row index in the permuted matrix.

More specifically, if the permutation sends row i to j, then map_row(i) returns j.

Examples
use rulinalg::matrix::PermutationMatrix;
let mut p = PermutationMatrix::<u32>::identity(3);
p.swap_rows(1, 2);
assert_eq!(p.map_row(1), 2);

Computes the parity of the permutation (even- or oddness).

The permutation matrix in an equivalent full matrix representation.

Computes the determinant of the permutation matrix.

The determinant of a permutation matrix is always +1 (if the permutation is even) or -1 (if the permutation is odd).

Permutes the rows of the given matrix in-place.

Panics
  • The number of rows in the matrix is not equal to the size of the permutation matrix.

Permutes the columns of the given matrix in-place.

Panics
  • The number of columns in the matrix is not equal to the size of the permutation matrix.

Permutes the elements of the given vector in-place.

Panics
  • The size of the vector is not equal to the size of the permutation matrix.

Permutes the rows of the given source_matrix and stores the result in buffer.

Panics
  • The number of rows in the source matrix is not equal to the size of the permutation matrix.
  • The dimensions of the source matrix and the buffer are not identical.

Permutes the columns of the given source_matrix and stores the result in buffer.

Panics
  • The number of columns in the source matrix is not equal to the size of the permutation matrix.
  • The dimensions of the source matrix and the buffer are not identical.

Permutes the elements of the given source_vector and stores the result in buffer.

Panics
  • The size of the source vector is not equal to the size of the permutation matrix.
  • The dimensions of the source vector and the buffer are not identical.

Computes the composition of self with the given source_perm and stores the result in buffer.

Panics
  • The size of the permutation matrix (self) is not equal to the size of the source permutation matrix.

Trait Implementations

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Converts to this type from the input type.
Converts this type into the (usually inferred) input type.

Left-multiply a matrix by a permutation matrix.

The resulting type after applying the * operator.
Performs the * operation. Read more

Left-multiply a matrix by a permutation matrix.

The resulting type after applying the * operator.
Performs the * operation. Read more

Left-multiply a matrix by a permutation matrix.

The resulting type after applying the * operator.
Performs the * operation. Read more

Left-multiply a matrix by a permutation matrix.

The resulting type after applying the * operator.
Performs the * operation. Read more

Left-multiply a matrix by a permutation matrix.

The resulting type after applying the * operator.
Performs the * operation. Read more

Left-multiply a matrix by a permutation matrix.

The resulting type after applying the * operator.
Performs the * operation. Read more

Multiply a permutation matrix by a permutation matrix.

The resulting type after applying the * operator.
Performs the * operation. Read more

Right-multiply a matrix by a permutation matrix.

The resulting type after applying the * operator.
Performs the * operation. Read more

Multiply a permutation matrix by a permutation matrix.

The resulting type after applying the * operator.
Performs the * operation. Read more

Left-multiply a vector by a permutation matrix.

The resulting type after applying the * operator.
Performs the * operation. Read more

Left-multiply a vector by a permutation matrix.

The resulting type after applying the * operator.
Performs the * operation. Read more

Right-multiply a matrix by a permutation matrix.

The resulting type after applying the * operator.
Performs the * operation. Read more

Right-multiply a matrix by a permutation matrix.

The resulting type after applying the * operator.
Performs the * operation. Read more

Right-multiply a matrix by a permutation matrix.

The resulting type after applying the * operator.
Performs the * operation. Read more

Right-multiply a matrix by a permutation matrix.

The resulting type after applying the * operator.
Performs the * operation. Read more

Right-multiply a matrix by a permutation matrix.

The resulting type after applying the * operator.
Performs the * operation. Read more

Left-multiply a matrix by a permutation matrix.

The resulting type after applying the * operator.
Performs the * operation. Read more

Left-multiply a matrix by a permutation matrix.

The resulting type after applying the * operator.
Performs the * operation. Read more

Left-multiply a matrix by a permutation matrix.

The resulting type after applying the * operator.
Performs the * operation. Read more

Left-multiply a matrix by a permutation matrix.

The resulting type after applying the * operator.
Performs the * operation. Read more

Left-multiply a matrix by a permutation matrix.

The resulting type after applying the * operator.
Performs the * operation. Read more

Left-multiply a matrix by a permutation matrix.

The resulting type after applying the * operator.
Performs the * operation. Read more

Right-multiply a matrix by a permutation matrix.

The resulting type after applying the * operator.
Performs the * operation. Read more

Right-multiply a matrix by a permutation matrix.

The resulting type after applying the * operator.
Performs the * operation. Read more

Right-multiply a matrix by a permutation matrix.

The resulting type after applying the * operator.
Performs the * operation. Read more

Multiply a permutation matrix by a permutation matrix.

The resulting type after applying the * operator.
Performs the * operation. Read more

Right-multiply a matrix by a permutation matrix.

The resulting type after applying the * operator.
Performs the * operation. Read more

Right-multiply a matrix by a permutation matrix.

The resulting type after applying the * operator.
Performs the * operation. Read more

Right-multiply a matrix by a permutation matrix.

The resulting type after applying the * operator.
Performs the * operation. Read more

Multiply a permutation matrix by a permutation matrix.

The resulting type after applying the * operator.
Performs the * operation. Read more

Left-multiply a vector by a permutation matrix.

The resulting type after applying the * operator.
Performs the * operation. Read more

Left-multiply a vector by a permutation matrix.

The resulting type after applying the * operator.
Performs the * operation. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.