Class PermutationMatrix
- All Implemented Interfaces:
Serializable
Represents a square permutation matrix with rows and columns equal to size
, where each row and column contains exactly
one entry of 1
, and all other entries are 0
. Internally, this class stores a permutation array so that
permutation[i] = j
indicates that there is a 1
at row i
, column j
.
Permutation matrices are orthogonal (and unitary in the complex case), so their inverse is equal to their transpose.
Permutation matrices are useful for permuting rows or columns of another matrix.
- Left multiplying another matrix by a permutation matrix permutes the rows of that matrix.
- Right multiplying another matrix by a permutation matrix permutes the columns of that matrix.
The determinant of any permutation matrix is always +1
or
-1
, depending on the parity of the permutation (i.e. the number of swaps in the matrix).
The identity matrix is a special case of a permutation matrix, corresponding to the identity permutation
{0, 1, ..., n-1}
.
Example usage:
// Construct matrices to permute.
Matrix a = new Vector(ArrayUtils.range(0, 5)).repeat(5, 1);
Matrix b = a.T();
// Create matrix to permute rows according to (4, 2, 3, 0, 1)
PermutationMatrix p1 = new PermutationMatrix(4, 2, 3, 0, 1);
// Permute rows of a according to (0, 1, 2, 3, 4) -> (4, 2, 3, 0, 1)
Matrix aPerm = p1.leftMult(a);
// Permute columns of b according to (0, 1, 2, 3, 4) -> (4, 2, 3, 0, 1)
Matrix bPerm = p1.T().rightMult(b);
// Display original matrices are their permuted counterparts.
System.out.println("a:\n" + a + "\naPerm:\n" + aPerm + "\n");
System.out.println("b:\n" + b + "\nbPerm:\n" + bPerm);
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final int[]
Describes the permutation represented by this permutation matrix.final Shape
Shape of this permutation matrix.final int
Size of this permutation matrix. -
Constructor Summary
ConstructorsConstructorDescriptionPermutationMatrix
(int size) Creates a permutation matrix which is equivalent to the identity matrix of the specified size.PermutationMatrix
(int... permutation) Constructs a permutation matrix from the specifiedpermutation
.PermutationMatrix
(int[] permutation, boolean ensurePermutation) Constructs a permutation matrix from the specifiedpermutation
.PermutationMatrix
(Shape shape) Creates a permutation matrix which is equivalent to the identity matrix of the specified size.Copy constructor which creates a deep copy of thesrc
permutation matrix. -
Method Summary
Modifier and TypeMethodDescriptionint
Computes the number of row/column swaps required for this permutation matrix to be converted to the identity matrix.copy()
Creates a copy of this permutation matrix.int
det()
Computes the determinant of this permutation matrix (will be +/- 1).boolean
Checks if this permutation matrix is equal to the given object.static PermutationMatrix
fromColSwaps
(int[] colPermutation) Creates a permutation matrix with the specified column permutation.int[]
Returns the permutation represented by this permutation matrix.int
hashCode()
Returns a hashcode for this permutation matrix by callingArrays.hashCode(permutation)
.inv()
Computes the inverse/transpose of this permutation matrix.Left multiplies this permutation matrix to the specified matrix.Left multiplies this permutation matrix to the specified vector.Left multiplies this permutation matrix to the specified matrix.Left multiplies this permutation matrix to the specified vector.Computes the matrix-matrix multiplication between two permutation matrices.void
permuteRows
(int[] swaps) Permutes rows of this permutation matrix.Right multiplies this permutation matrix to the specified matrix.Right multiplies this permutation matrix to the specified vector.Right multiplies this permutation matrix to the specified matrix.Right multiplies this permutation matrix to the specified vector.void
swapCols
(int col1, int col2) Swaps two columns in this permutation matrix.void
swapRows
(int row1, int row2) Swaps two rows in this permutation matrix.T()
Computes the transpose/inverse of this permutation matrix.toCoo()
Converts this permutation matrix to areal sparse COO matrix
.toCsr()
Converts this permutation matrix to areal sparse COO matrix
.toDense()
Converts this permutation matrix to areal dense matrix
.toString()
Converts this permutation matrix to a human-readable string.int
tr()
Computes the trace of this permutation matrix.int
trace()
Computes the trace of this permutation matrix.
-
Field Details
-
permutation
protected final int[] permutationDescribes the permutation represented by this permutation matrix.permutation[i] = j
indicates that there is a1
at rowi
, columnj
with in permutation matrix. -
size
public final int sizeSize of this permutation matrix. -
shape
Shape of this permutation matrix.
-
-
Constructor Details
-
PermutationMatrix
public PermutationMatrix(int size) Creates a permutation matrix which is equivalent to the identity matrix of the specified size.- Parameters:
size
- Size of the permutation matrix. That is, the number of rows and columns
-
PermutationMatrix
Creates a permutation matrix which is equivalent to the identity matrix of the specified size.- Parameters:
shape
- Shape of the permutation matrix. That is, the number of rows and columns. Must be a square shape.- Throws:
LinearAlgebraException
- Ifshape
is not square.
-
PermutationMatrix
Copy constructor which creates a deep copy of thesrc
permutation matrix.- Parameters:
src
- The permutation matrix to copy.
-
PermutationMatrix
public PermutationMatrix(int... permutation) Constructs a permutation matrix from the specified
permutation
.This constructor will explicitly verify that
permutation
is a valid permutation. It is highly recommended to do this. However, there is a- Parameters:
permutation
- Array specifying the permutation. Must contain a permutation of{0, 1, ..., permutation.length-1}
.permutation[i] = j
indicates that there is a1
at rowi
, columnj
.- Throws:
IllegalArgumentException
-permutation
is not a permutation ofinvalid @code
{@code {0, 1, ..., permutation.length-1}.
-
PermutationMatrix
public PermutationMatrix(int[] permutation, boolean ensurePermutation) Constructs a permutation matrix from the specified
permutation
. This constructor also accepts a flag indicating if an explicit check should be made to enforce that thepermutation
array is a valid permutation.It is highly recommended to use
PermutationMatrix(int[])
or setensurePermutation = true
. However, if there is absolute confidence in the validity of thepermutation
array, then settingensurePermutation = false
may yield very slight performance benefits.- Parameters:
permutation
- Array specifying the permutation. Must contain a permutation of{0, 1, ..., permutation.length-1}
.permutation[i] = j
indicates that there is a1
at rowi
, columnj
.ensurePermutation
- Flag indicating if an explicit check should be made to verify thatpermutation
is a valid permutation of{0, 1, ..., permutation.length-1}
.- If
true
: an explicit check will be made thatpermutation
is a valid permutation. - If
false
: NO check will be made to ensurepermutation
is a valid permutation.
- If
- Throws:
IllegalArgumentException
- IfensurePermutation == true
andpermutation
is not a permutation ofinvalid @code
{@code {0, 1, ..., permutation.length-1}.
-
-
Method Details
-
getPermutation
public int[] getPermutation()Returns the permutation represented by this permutation matrix.- Returns:
- The permutation represented by this permutation matrix.
-
fromColSwaps
Creates a permutation matrix with the specified column permutation. That is, a permutation matrix such that right multiplying it with another matrix results in permuting th columns of that matrix according tocolPermutation
.- Parameters:
colPermutation
- Array specifying column permutation. The entryx
at indexi
indicates that columni
has been swapped with columnx
.permutation array
.- Returns:
- A permutation matrix that when right multiplied to a matrix results in permuting the columns of that matrix according
to
colPermutation
. - Throws:
IllegalArgumentException
- IfcolPermutation
is not a valid permutation.
-
copy
Creates a copy of this permutation matrix.- Returns:
- A copy of this permutation matrix.
-
equals
Checks if this permutation matrix is equal to the given object. A permutation matrix is considered equal to an object if that object is also a permutation matrix and represents the same matrix. -
hashCode
public int hashCode()Returns a hashcode for this permutation matrix by callingArrays.hashCode(permutation)
. -
mult
Computes the matrix-matrix multiplication between two permutation matrices.- Parameters:
b
- The matrix to multiply to this permutation matrix.- Returns:
- The matrix=matrix product of this permutation matrix with
b
. - Throws:
TensorShapeException
- Ifthis.size != b.size
.
-
leftMult
Left multiplies this permutation matrix to the specified matrix. This will have the effect of swapping rows in the src matrix.- Parameters:
src
- The matrix to left multiply this permutation matrix to.- Returns:
- The result of left multiplying this permutation matrix to the
src
matrix. - Throws:
IllegalArgumentException
- If the number of rows insrc
does not equal the size of this permutation matrix.- See Also:
-
leftMult
Left multiplies this permutation matrix to the specified vector. This will have the effect of swapping rows in the src vector. The vector will be treated as a column vector.- Parameters:
src
- The vector to left multiply this permutation matrix to.- Returns:
- The result of left multiplying this permutation matrix to the
src
vector. - Throws:
IllegalArgumentException
- If size ofsrc
does not equal the size of this permutation matrix.- See Also:
-
leftMult
Left multiplies this permutation matrix to the specified matrix. This will have the effect of swapping rows in the src matrix.- Parameters:
src
- The matrix to left multiply this permutation matrix to.- Returns:
- The result of left multiplying this permutation matrix to the
src
matrix. - Throws:
IllegalArgumentException
- If the number of rows insrc
does not equal the size of this permutation matrix.- See Also:
-
leftMult
Left multiplies this permutation matrix to the specified vector. This will have the effect of swapping rows in the src vector. The vector will be treated as a column vector.- Parameters:
src
- The vector to left multiply this permutation matrix to.- Returns:
- The result of left multiplying this permutation matrix to the
src
vector. - Throws:
IllegalArgumentException
- If size ofsrc
does not equal the size of this permutation matrix.- See Also:
-
rightMult
Right multiplies this permutation matrix to the specified matrix. This is equivalent to swapping columns in thesrc
matrix.- Parameters:
src
- The matrix to right multiply this permutation matrix to.- Returns:
- The result of right multiplying this permutation matrix to the
src
matrix. - Throws:
IllegalArgumentException
- If the number of columns insrc
does not match the size of this permutation matrix.- See Also:
-
rightMult
Right multiplies this permutation matrix to the specified vector. This will have the effect of swapping columns in the src vector. The vector will be treated as a row vector.- Parameters:
src
- The vector to right multiply this permutation matrix to.- Returns:
- The result of right multiplying this permutation matrix to the
src
vector. - Throws:
IllegalArgumentException
- If size ofsrc
does not equal the size of this permutation matrix.- See Also:
-
rightMult
Right multiplies this permutation matrix to the specified matrix. This is equivalent to swapping columns in thesrc
matrix.- Parameters:
src
- The matrix to right multiply this permutation matrix to.- Returns:
- The result of right multiplying this permutation matrix to the
src
matrix. - Throws:
IllegalArgumentException
- If the number of columns insrc
does not match the size of this permutation matrix.- See Also:
-
rightMult
Right multiplies this permutation matrix to the specified vector. This will have the effect of swapping columns in the src vector. The vector will be treated as a row vector.- Parameters:
src
- The vector to right multiply this permutation matrix to.- Returns:
- The result of right multiplying this permutation matrix to the
src
vector. - Throws:
IllegalArgumentException
- If size ofsrc
does not equal the size of this permutation matrix.- See Also:
-
swapRows
public void swapRows(int row1, int row2) Swaps two rows in this permutation matrix.- Parameters:
row1
- First row to swap in the permutation matrix.row2
- Second row to swap in the permutation matrix.- Throws:
ArrayIndexOutOfBoundsException
- If eitherrow1
orrow2
is out of bounds of this permutation matrix.
-
swapCols
public void swapCols(int col1, int col2) Swaps two columns in this permutation matrix.- Parameters:
col1
- First column to swap in the permutation matrix.col2
- Second column to swap in the permutation matrix.- Throws:
ArrayIndexOutOfBoundsException
- If eithercol1
orcol2
is out of bounds of this permutation matrix.
-
permuteRows
public void permuteRows(int[] swaps) Permutes rows of this permutation matrix.- Parameters:
swaps
- Defines row swaps of this permutation matrix. The entryx
at indexi
represents rowi
has been swapped with rowx
. This must be apermutation
array.- Throws:
IllegalArgumentException
- Ifswaps
is not the same length as the number of rows/columns in this permutation matrix. Or, ifswaps
is not apermutation
array.
-
inv
Computes the inverse/transpose of this permutation matrix.- Returns:
- The inverse/transpose of this permutation matrix.
-
T
Computes the transpose/inverse of this permutation matrix.- Returns:
- The transpose/inverse of this permutation matrix.
-
trace
public int trace()Computes the trace of this permutation matrix.- Returns:
- The trace of this permutation matrix.
-
tr
public int tr()Computes the trace of this permutation matrix. Alias fortrace()
.- Returns:
- The trace of this permutation matrix.
-
computeSwaps
public int computeSwaps()Computes the number of row/column swaps required for this permutation matrix to be converted to the identity matrix.- Returns:
- The total number of row/column swaps required to convert this permutation matrix to the identity matrix.
-
det
public int det()Computes the determinant of this permutation matrix (will be +/- 1).- Returns:
- The determinant of this permutation matrix (will be +/- 1).
-
toDense
Converts this permutation matrix to areal dense matrix
.- Returns:
- A real dense matrix which is equivalent to this permutation matrix.
- See Also:
-
toCoo
Converts this permutation matrix to areal sparse COO matrix
.- Returns:
- real sparse COO matrix which is equivalent to this permutation matrix.
- See Also:
-
toCsr
Converts this permutation matrix to areal sparse COO matrix
.- Returns:
- real sparse COO matrix which is equivalent to this permutation matrix
- See Also:
-
toString
-