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 Matrix
add(Matrix that)
this + thatPermutationMatrix
deepCopy()
The implementation returns an instance created fromthis
by the copy constructor of the class, or justthis
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 - thatvoid
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 insteaddouble
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.
-
-
-
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
- ifdata
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 fromthis
by the copy constructor of the class, or justthis
if the instance itself is immutable.- Specified by:
deepCopy
in interfaceDeepCopyable
- Specified by:
deepCopy
in interfaceMatrix
- 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.
-
nCols
public int nCols()
Description copied from interface:Table
Gets 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 MatrixAccessException
Description copied from interface:MatrixAccess
Get the matrix entry at [i,j].- Specified by:
get
in 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:Matrix
Get the specified row in the matrix as a vector.- Specified by:
getRow
in 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:Matrix
Get the specified column in the matrix as a vector.- Specified by:
getColumn
in 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:
multiply
in interfaceMatrixRing
- Specified by:
multiply
in interfaceMonoid<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 interfaceAbelianGroup<Matrix>
- Specified by:
add
in interfaceMatrixRing
- Parameters:
that
- a matrix- Returns:
- the sum of
this
andthat
-
minus
public Matrix minus(Matrix that)
Description copied from interface:MatrixRing
this - that- Specified by:
minus
in interfaceAbelianGroup<Matrix>
- Specified by:
minus
in interfaceMatrixRing
- Parameters:
that
- a matrix- Returns:
- the difference between
this
andthat
-
scaled
public Matrix scaled(double scalar)
Description copied from interface:Matrix
Scale this matrix, A, by a constant.
-
opposite
public Matrix opposite()
Description copied from interface:MatrixRing
Get the opposite of this matrix.- Specified by:
opposite
in interfaceAbelianGroup<Matrix>
- Specified by:
opposite
in 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:
t
in interfaceMatrixRing
- 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 interfaceMatrixRing
- Specified by:
ONE
in interfaceMonoid<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 interfaceAbelianGroup<Matrix>
- Specified by:
ZERO
in 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:
set
in 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
-
-