# SuanShu, a Java numerical and statistical library

com.numericalmethod.suanshu.algebra.linear.matrix.doubles.matrixtype

## Class PermutationMatrix

• java.lang.Object
• com.numericalmethod.suanshu.algebra.linear.matrix.doubles.matrixtype.PermutationMatrix
• All Implemented Interfaces:
Matrix, MatrixAccess, MatrixRing, MatrixTable, AbelianGroup<Matrix>, Monoid<Matrix>, Ring<Matrix>, Table, DeepCopyable

public class PermutationMatrix
extends Object
implements Matrix
A permutation matrix is a square matrix that has exactly one entry '1' in each row and each column and 0's elsewhere. Suppose P is a permutation matrix, A any matrix, then
1. P * A swaps the rows of A;
2. A * P swaps the columns of A;
To ensure that the matrix represents a consistent permutation, there is no method 'set' in this class. The matrix entries can only be modified by the swap functions.
See Also:
Wikipedia: Permutation matrix
• ### Constructor Summary

Constructors
Constructor and Description
PermutationMatrix(int dim)
Construct an identity permutation matrix.
PermutationMatrix(int[] data)
Construct a permutation matrix from an 1D double[].
PermutationMatrix(PermutationMatrix P)
Copy constructor.
• ### Method Summary

All Methods
Modifier and Type Method and Description
Matrix add(Matrix that)
this + that
PermutationMatrix deepCopy()
The implementation returns an instance created from this by the copy constructor of the class, or just this if the instance itself is immutable.
boolean equals(Object obj)
double get(int i, int j)
Get the matrix entry at [i,j].
Vector getColumn(int j)
Get the specified column in the matrix as a vector.
Vector getRow(int i)
Get the specified row in the matrix as a vector.
int hashCode()
Matrix minus(Matrix that)
this - that
void moveColumn2End(int j)
Swaps a column of a permutation matrix with the last column.
void moveRow2End(int i)
Swaps a row of the permutation matrix with the last row.
Matrix multiply(Matrix A)
Left multiplication by P.
Vector multiply(Vector v)
Left multiplication by P.
int nCols()
Gets the number of columns.
int nRows()
Gets the number of rows.
PermutationMatrix ONE()
Get an identity matrix that has the same dimension as this matrix.
Matrix opposite()
Get the opposite of this matrix.
Matrix rightMultiply(Matrix A)
Right multiplication by P.
Matrix scaled(double scalar)
Scale this matrix, A, by a constant.
void set(int i, int j, double value)
Deprecated.
use the swap functions instead
double sign()
Gets the sign of the permutation matrix which is also the determinant.
void swapColumn(int j1, int j2)
Swaps two columns of a permutation matrix.
void swapRow(int i1, int i2)
Swaps two rows of a permutation matrix.
PermutationMatrix t()
The transpose of a permutation matric is the same as its inverse.
String toString()
Matrix ZERO()
Get a zero matrix that has the same dimension as this matrix.
• ### Methods inherited from class java.lang.Object

clone, finalize, getClass, notify, notifyAll, wait, wait, wait
• ### Constructor Detail

• #### PermutationMatrix

public PermutationMatrix(int dim)
Construct an identity permutation matrix.
Parameters:
dim - the matrix dimension
• #### PermutationMatrix

public PermutationMatrix(int[] data)
Construct a permutation matrix from an 1D double[].
Parameters:
data - the 1D double[] indicating the column position of the 1 in each row
Throws:
IllegalArgumentException - if data is not a permutation matrix
• #### PermutationMatrix

public PermutationMatrix(PermutationMatrix P)
Copy constructor.
Parameters:
P - a permutation matrix
• ### Method Detail

• #### deepCopy

public PermutationMatrix deepCopy()
Description copied from interface: DeepCopyable
The implementation returns an instance created from this by the copy constructor of the class, or just this if the instance itself is immutable.
Specified by:
deepCopy in interface Matrix
Specified by:
deepCopy in interface DeepCopyable
Returns:
an independent (deep) copy of the instance
• #### nRows

public int nRows()
Description copied from interface: Table
Gets the number of rows. Rows count from 1.
Specified by:
nRows in interface Table
Returns:
the number of rows
• #### nCols

public int nCols()
Description copied from interface: Table
Gets the number of columns. Columns count from 1.
Specified by:
nCols in interface Table
Returns:
the number of columns
• #### sign

public double sign()
Gets the sign of the permutation matrix which is also the determinant. It is +1 for an even (or 0) number of swaps and -1 for an odd number of swaps.
Returns:
the sign
• #### swapRow

public void swapRow(int i1,
int i2)
Swaps two rows of a permutation matrix.
Parameters:
i1 - row 1
i2 - row 2
• #### swapColumn

public void swapColumn(int j1,
int j2)
Swaps two columns of a permutation matrix.
Parameters:
j1 - column 1
j2 - column 2
• #### moveRow2End

public void moveRow2End(int i)
Swaps a row of the permutation matrix with the last row.
Parameters:
i - the row to be swapped with the last row
• #### moveColumn2End

public void moveColumn2End(int j)
Swaps a column of a permutation matrix with the last column.
Parameters:
j - the column to be swapped with the last column
• #### get

public double get(int i,
int j)
throws MatrixAccessException
Description copied from interface: MatrixAccess
Get the matrix entry at [i,j].
Specified by:
get in interface MatrixAccess
Parameters:
i - the row index
j - the column index
Returns:
A[i,j]
Throws:
MatrixAccessException - if i or j is out of range
• #### getRow

public Vector getRow(int i)
throws MatrixAccessException
Description copied from interface: Matrix
Get the specified row in the matrix as a vector.
Specified by:
getRow in interface Matrix
Parameters:
i - the row index
Returns:
the vector A[i, ]
Throws:
MatrixAccessException - when i < 1, or when i > the number of rows
• #### getColumn

public Vector getColumn(int j)
throws MatrixAccessException
Description copied from interface: Matrix
Get the specified column in the matrix as a vector.
Specified by:
getColumn in interface Matrix
Parameters:
j - the column index
Returns:
a vector A[, j]
Throws:
MatrixAccessException - when j < 1, or when j > the number of columns
• #### rightMultiply

public Matrix rightMultiply(Matrix A)
Right multiplication by P. A * P is the same as swapping columns in A according to P.
Parameters:
A - a matrix
Returns:
the matrix with the columns swapped
• #### multiply

public Vector multiply(Vector v)
Left multiplication by P. P * v is the same as swapping the vector entries/rows according to P.
Specified by:
multiply in interface Matrix
Parameters:
v - a column vector
Returns:
the vector with the rows swapped
• #### multiply

public Matrix multiply(Matrix A)
Left multiplication by P. P * A is the same as swapping rows in A according to P.
Specified by:
multiply in interface MatrixRing
Specified by:
multiply in interface Monoid<Matrix>
Parameters:
A - a matrix
Returns:
the matrix with the rows swapped
• #### add

public Matrix add(Matrix that)
Description copied from interface: MatrixRing
this + that
Specified by:
add in interface MatrixRing
Specified by:
add in interface AbelianGroup<Matrix>
Parameters:
that - a matrix
Returns:
the sum of this and that
• #### minus

public Matrix minus(Matrix that)
Description copied from interface: MatrixRing
this - that
Specified by:
minus in interface MatrixRing
Specified by:
minus in interface AbelianGroup<Matrix>
Parameters:
that - a matrix
Returns:
the difference between this and that
• #### scaled

public Matrix scaled(double scalar)
Description copied from interface: Matrix
Scale this matrix, A, by a constant.
Specified by:
scaled in interface Matrix
Parameters:
scalar - a double
Returns:
cA
• #### opposite

public Matrix opposite()
Description copied from interface: MatrixRing
Get the opposite of this matrix.
Specified by:
opposite in interface MatrixRing
Specified by:
opposite in interface AbelianGroup<Matrix>
Returns:
-this
See Also:
Wikipedia: Additive inverse
• #### t

public PermutationMatrix t()
The transpose of a permutation matric is the same as its inverse. That is,
P.multiply(P.t()) == P.t().multiply(P) == P.toDense().ONE()
Specified by:
t in interface MatrixRing
Returns:
a copy of itself
• #### ONE

public PermutationMatrix ONE()
Description copied from interface: MatrixRing
Get an identity matrix that has the same dimension as this matrix. For a non-square matrix, it zeros out the rows (columns) with index > nCols (nRows).
Specified by:
ONE in interface MatrixRing
Specified by:
ONE in interface Monoid<Matrix>
Returns:
an identity matrix
• #### ZERO

public Matrix ZERO()
Description copied from interface: MatrixRing
Get a zero matrix that has the same dimension as this matrix.
Specified by:
ZERO in interface MatrixRing
Specified by:
ZERO in interface AbelianGroup<Matrix>
Returns:
the 0 matrix
• #### equals

public boolean equals(Object obj)
Overrides:
equals in class Object
• #### hashCode

public int hashCode()
Overrides:
hashCode in class Object
• #### toString

public String toString()
Overrides:
toString in class Object
• #### set

@Deprecated
public void set(int i,
int j,
double value)
throws MatrixAccessException
Deprecated. use the swap functions instead
Don't use this function to change entries in a Permutation matrix. Use the swap functions instead.
Specified by:
set in interface MatrixAccess
Parameters:
i - the row index
j - the column index
value - the value to set A[i,j] to
Throws:
UnsupportedOperationException - when called
MatrixAccessException - if i or j is out of range

Copyright © 2010-2018 Numerical Method Incorporation Limited. All Rights Reserved.