Class PermutationMatrix
- java.lang.Object
-
- dev.nm.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- P * A swaps the rows of A;
- A * P swaps the columns of A;
- See Also:
- Wikipedia: Permutation matrix
-
-
Constructor Summary
Constructors Constructor Description PermutationMatrix(int dim)Construct an identity permutation matrix.PermutationMatrix(int[] data)Construct a permutation matrix from an 1Ddouble[].PermutationMatrix(PermutationMatrix P)Copy constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description Matrixadd(Matrix that)this + thatPermutationMatrixdeepCopy()The implementation returns an instance created fromthisby the copy constructor of the class, or justthisif the instance itself is immutable.booleanequals(Object obj)doubleget(int i, int j)Get the matrix entry at [i,j].VectorgetColumn(int j)Get the specified column in the matrix as a vector.VectorgetRow(int i)Get the specified row in the matrix as a vector.inthashCode()Matrixminus(Matrix that)this - thatvoidmoveColumn2End(int j)Swaps a column of a permutation matrix with the last column.voidmoveRow2End(int i)Swaps a row of the permutation matrix with the last row.Matrixmultiply(Matrix A)Left multiplication by P.Vectormultiply(Vector v)Left multiplication by P.intnCols()Gets the number of columns.intnRows()Gets the number of rows.PermutationMatrixONE()Get an identity matrix that has the same dimension as this matrix.Matrixopposite()Get the opposite of this matrix.MatrixrightMultiply(Matrix A)Right multiplication by P.Matrixscaled(double scalar)Scale this matrix, A, by a constant.voidset(int i, int j, double value)Deprecated.use the swap functions insteaddoublesign()Gets the sign of the permutation matrix which is also the determinant.voidswapColumn(int j1, int j2)Swaps two columns of a permutation matrix.voidswapRow(int i1, int i2)Swaps two rows of a permutation matrix.PermutationMatrixt()The transpose of a permutation matric is the same as its inverse.StringtoString()MatrixZERO()Get a zero matrix that has the same dimension as this matrix.
-
-
-
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 1Ddouble[].- Parameters:
data- the 1Ddouble[]indicating the column position of the 1 in each row- Throws:
IllegalArgumentException- ifdatais 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:DeepCopyableThe implementation returns an instance created fromthisby the copy constructor of the class, or justthisif the instance itself is immutable.- Specified by:
deepCopyin interfaceDeepCopyable- Specified by:
deepCopyin interfaceMatrix- Returns:
- an independent (deep) copy of the instance
-
nRows
public int nRows()
Description copied from interface:TableGets the number of rows. Rows count from 1.
-
nCols
public int nCols()
Description copied from interface:TableGets the number of columns. Columns count from 1.
-
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 1i2- row 2
-
swapColumn
public void swapColumn(int j1, int j2)Swaps two columns of a permutation matrix.- Parameters:
j1- column 1j2- 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 MatrixAccessExceptionDescription copied from interface:MatrixAccessGet the matrix entry at [i,j].- Specified by:
getin interfaceMatrixAccess- Parameters:
i- the row indexj- 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:MatrixGet the specified row in the matrix as a vector.- Specified by:
getRowin interfaceMatrix- 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:MatrixGet the specified column in the matrix as a vector.- Specified by:
getColumnin interfaceMatrix- 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.
-
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:
multiplyin interfaceMatrixRing- Specified by:
multiplyin interfaceMonoid<Matrix>- Parameters:
A- a matrix- Returns:
- the matrix with the rows swapped
-
add
public Matrix add(Matrix that)
Description copied from interface:MatrixRingthis + that- Specified by:
addin interfaceAbelianGroup<Matrix>- Specified by:
addin interfaceMatrixRing- Parameters:
that- a matrix- Returns:
- the sum of
thisandthat
-
minus
public Matrix minus(Matrix that)
Description copied from interface:MatrixRingthis - that- Specified by:
minusin interfaceAbelianGroup<Matrix>- Specified by:
minusin interfaceMatrixRing- Parameters:
that- a matrix- Returns:
- the difference between
thisandthat
-
scaled
public Matrix scaled(double scalar)
Description copied from interface:MatrixScale this matrix, A, by a constant.
-
opposite
public Matrix opposite()
Description copied from interface:MatrixRingGet the opposite of this matrix.- Specified by:
oppositein interfaceAbelianGroup<Matrix>- Specified by:
oppositein interfaceMatrixRing- 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:
tin interfaceMatrixRing- Returns:
- a copy of itself
-
ONE
public PermutationMatrix ONE()
Description copied from interface:MatrixRingGet 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:
ONEin interfaceMatrixRing- Specified by:
ONEin interfaceMonoid<Matrix>- Returns:
- an identity matrix
-
ZERO
public Matrix ZERO()
Description copied from interface:MatrixRingGet a zero matrix that has the same dimension as this matrix.- Specified by:
ZEROin interfaceAbelianGroup<Matrix>- Specified by:
ZEROin interfaceMatrixRing- Returns:
- the 0 matrix
-
set
@Deprecated public void set(int i, int j, double value) throws MatrixAccessException
Deprecated.use the swap functions insteadDon't use this function to change entries in a Permutation matrix. Use the swap functions instead.swapRow(int, int),swapColumn(int, int),moveRow2End(int),moveColumn2End(int),- Specified by:
setin interfaceMatrixAccess- Parameters:
i- the row indexj- the column indexvalue- the value to set A[i,j] to- Throws:
UnsupportedOperationException- when calledMatrixAccessException- if i or j is out of range
-
-