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
  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:
  • Constructor Details

    • 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 Details

    • 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 DeepCopyable
      Specified by:
      deepCopy in interface Matrix
      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 AbelianGroup<Matrix>
      Specified by:
      add in interface MatrixRing
      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 AbelianGroup<Matrix>
      Specified by:
      minus in interface MatrixRing
      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 AbelianGroup<Matrix>
      Specified by:
      opposite in interface MatrixRing
      Returns:
      -this
      See Also:
    • 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 AbelianGroup<Matrix>
      Specified by:
      ZERO in interface MatrixRing
      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.

      swapRow(int, int), swapColumn(int, int), moveRow2End(int), moveColumn2End(int),

      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