Class AbstractMatrix

java.lang.Object
no.uib.cipr.matrix.AbstractMatrix
All Implemented Interfaces:
Iterable<MatrixEntry>, Matrix
Direct Known Subclasses:
BandMatrix, CompColMatrix, CompDiagMatrix, CompRowMatrix, DenseMatrix, DistColMatrix, DistRowMatrix, FlexCompColMatrix, FlexCompRowMatrix, LowerSymmBandMatrix, LowerSymmDenseMatrix, LowerSymmPackMatrix, LowerTriangBandMatrix, LowerTriangDenseMatrix, LowerTriangPackMatrix, SymmTridiagMatrix, TridiagMatrix, UpperSymmBandMatrix, UpperSymmDenseMatrix, UpperSymmPackMatrix, UpperTriangBandMatrix, UpperTriangDenseMatrix, UpperTriangPackMatrix

public abstract class AbstractMatrix extends Object implements Matrix
Partial implementation of Matrix. The following methods throw UnsupportedOperationException, and should be overridden by a subclass:
  • get(int,int)
  • set(int,int,double)
  • copy
  • All the direct solution methods

For the rest of the methods, simple default implementations using a matrix iterator has been provided. There are some kernel operations which the simpler operations forward to, for instance, mult(Matrix,Matrix) forwards to multAdd(double,Matrix,Matrix). Subclasses can thus focus on overriding the kernel operations, which are:

  • multAdd(double,Vector,Vector) and transMultAdd(double,Vector,Vector).
  • rank1(double,Vector,Vector) and rank1(double,Vector,Vector).
  • multAdd(double,Matrix,Matrix), transAmultAdd(double,Matrix,Matrix), transBmultAdd(double,Matrix,Matrix), and transABmultAdd(double,Matrix,Matrix).
  • scale(double).
  • set(double,Matrix) and add(double,Matrix).
  • transpose and transpose(Matrix).
  • All the norms.

Finally, a default iterator is provided by this class, which works by calling the get function. A tailored replacement should be used by subclasses.

  • Field Details

    • numRows

      protected int numRows
      Number of rows
    • numColumns

      protected int numColumns
      Number of columns
  • Constructor Details

    • AbstractMatrix

      protected AbstractMatrix(int numRows, int numColumns)
      Constructor for AbstractMatrix
    • AbstractMatrix

      protected AbstractMatrix(Matrix A)
      Constructor for AbstractMatrix, same size as A. The invoking constructor should set this matrix equal the argument matrix
  • Method Details

    • numRows

      public int numRows()
      Description copied from interface: Matrix
      Number of rows in the matrix
      Specified by:
      numRows in interface Matrix
    • numColumns

      public int numColumns()
      Description copied from interface: Matrix
      Number of columns in the matrix
      Specified by:
      numColumns in interface Matrix
    • isSquare

      public boolean isSquare()
      Description copied from interface: Matrix
      Returns true if the matrix is square
      Specified by:
      isSquare in interface Matrix
    • set

      public void set(int row, int column, double value)
      Description copied from interface: Matrix
      A(row,column) = value
      Specified by:
      set in interface Matrix
    • add

      public void add(int row, int column, double value)
      Description copied from interface: Matrix
      A(row,column) += value
      Specified by:
      add in interface Matrix
    • get

      public double get(int row, int column)
      Description copied from interface: Matrix
      Returns A(row,column)
      Specified by:
      get in interface Matrix
    • check

      protected void check(int row, int column)
      Checks the passed row and column indices
    • copy

      public Matrix copy()
      Description copied from interface: Matrix
      Creates a deep copy of the matrix
      Specified by:
      copy in interface Matrix
      Returns:
      A
    • zero

      public Matrix zero()
      Description copied from interface: Matrix
      Zeros all the entries in the matrix, while preserving any underlying structure. Useful for general, unstructured matrices.
      Specified by:
      zero in interface Matrix
      Returns:
      A
    • mult

      public Vector mult(Vector x, Vector y)
      Description copied from interface: Matrix
      y = A*x
      Specified by:
      mult in interface Matrix
      Parameters:
      x - Vector of size A.numColumns()
      y - Vector of size A.numRows()
      Returns:
      y
    • mult

      public Vector mult(double alpha, Vector x, Vector y)
      Description copied from interface: Matrix
      y = alpha*A*x
      Specified by:
      mult in interface Matrix
      Parameters:
      x - Vector of size A.numColumns()
      y - Vector of size A.numRows()
      Returns:
      y
    • multAdd

      public Vector multAdd(Vector x, Vector y)
      Description copied from interface: Matrix
      y = A*x + y
      Specified by:
      multAdd in interface Matrix
      Parameters:
      x - Vector of size A.numColumns()
      y - Vector of size A.numRows()
      Returns:
      y
    • multAdd

      public Vector multAdd(double alpha, Vector x, Vector y)
      Description copied from interface: Matrix
      y = alpha*A*x + y
      Specified by:
      multAdd in interface Matrix
      Parameters:
      x - Vector of size A.numColumns()
      y - Vector of size A.numRows()
      Returns:
      y
    • checkMultAdd

      protected void checkMultAdd(Vector x, Vector y)
      Checks the arguments to mult and multAdd
    • transMult

      public Vector transMult(Vector x, Vector y)
      Description copied from interface: Matrix
      y = AT*x
      Specified by:
      transMult in interface Matrix
      Parameters:
      x - Vector of size A.numRows()
      y - Vector of size A.numColumns()
      Returns:
      y
    • transMult

      public Vector transMult(double alpha, Vector x, Vector y)
      Description copied from interface: Matrix
      y = alpha*AT*x
      Specified by:
      transMult in interface Matrix
      Parameters:
      x - Vector of size A.numRows()
      y - Vector of size A.numColumns()
      Returns:
      y
    • transMultAdd

      public Vector transMultAdd(Vector x, Vector y)
      Description copied from interface: Matrix
      y = AT*x + y
      Specified by:
      transMultAdd in interface Matrix
      Parameters:
      x - Vector of size A.numRows()
      y - Vector of size A.numColumns()
      Returns:
      y
    • transMultAdd

      public Vector transMultAdd(double alpha, Vector x, Vector y)
      Description copied from interface: Matrix
      y = alpha*AT*x + y
      Specified by:
      transMultAdd in interface Matrix
      Parameters:
      x - Vector of size A.numRows()
      y - Vector of size A.numColumns()
      Returns:
      y
    • checkTransMultAdd

      protected void checkTransMultAdd(Vector x, Vector y)
      Checks the arguments to transMult and transMultAdd
    • solve

      public Vector solve(Vector b, Vector x)
      Description copied from interface: Matrix
      x = A\b. Not all matrices support this operation, those that do not throw UnsupportedOperationException. Note that it is often more efficient to use a matrix decomposition and its associated solver
      Specified by:
      solve in interface Matrix
      Parameters:
      b - Vector of size A.numRows()
      x - Vector of size A.numColumns()
      Returns:
      x
    • transSolve

      public Vector transSolve(Vector b, Vector x)
      Description copied from interface: Matrix
      x = AT\b. Not all matrices support this operation, those that do not throw UnsupportedOperationException. Note that it is often more efficient to use a matrix decomposition and its associated solver
      Specified by:
      transSolve in interface Matrix
      Parameters:
      b - Vector of size A.numColumns()
      x - Vector of size A.numRows()
      Returns:
      x
    • checkSolve

      protected void checkSolve(Vector b, Vector x)
      Checks that a matrix inversion is legal for the given arguments. This is for the square case, not for least-squares problems
    • rank1

      public Matrix rank1(Vector x)
      Description copied from interface: Matrix
      A = x*xT + A. The matrix must be square, and the vector of the same length
      Specified by:
      rank1 in interface Matrix
      Returns:
      A
    • rank1

      public Matrix rank1(double alpha, Vector x)
      Description copied from interface: Matrix
      A = alpha*x*xT + A. The matrix must be square, and the vector of the same length
      Specified by:
      rank1 in interface Matrix
      Returns:
      A
    • rank1

      public Matrix rank1(Vector x, Vector y)
      Description copied from interface: Matrix
      A = x*yT + A. The matrix must be square, and the vectors of the same length
      Specified by:
      rank1 in interface Matrix
      Returns:
      A
    • rank1

      public Matrix rank1(double alpha, Vector x, Vector y)
      Description copied from interface: Matrix
      A = alpha*x*yT + A. The matrix must be square, and the vectors of the same length
      Specified by:
      rank1 in interface Matrix
      Returns:
      A
    • checkRank1

      protected void checkRank1(Vector x, Vector y)
      Checks that a vector rank1 update is possible for the given vectors
    • rank2

      public Matrix rank2(Vector x, Vector y)
      Description copied from interface: Matrix
      A = x*yT + y*xT + A. The matrix must be square, and the vectors of the same length
      Specified by:
      rank2 in interface Matrix
      Returns:
      A
    • rank2

      public Matrix rank2(double alpha, Vector x, Vector y)
      Description copied from interface: Matrix
      A = alpha*x*yT + alpha*y*xT + A. The matrix must be square, and the vectors of the same length
      Specified by:
      rank2 in interface Matrix
      Returns:
      A
    • checkRank2

      protected void checkRank2(Vector x, Vector y)
      Checks that a vector rank2 update is legal with the given vectors
    • mult

      public Matrix mult(Matrix B, Matrix C)
      Description copied from interface: Matrix
      C = A*B
      Specified by:
      mult in interface Matrix
      Parameters:
      B - Matrix such that B.numRows() == A.numColumns() and B.numColumns() == C.numColumns()
      C - Matrix such that C.numRows() == A.numRows() and B.numColumns() == C.numColumns()
      Returns:
      C
    • mult

      public Matrix mult(double alpha, Matrix B, Matrix C)
      Description copied from interface: Matrix
      C = alpha*A*B
      Specified by:
      mult in interface Matrix
      Parameters:
      B - Matrix such that B.numRows() == A.numColumns() and B.numColumns() == C.numColumns()
      C - Matrix such that C.numRows() == A.numRows() and B.numColumns() == C.numColumns()
      Returns:
      C
    • multAdd

      public Matrix multAdd(Matrix B, Matrix C)
      Description copied from interface: Matrix
      C = A*B + C
      Specified by:
      multAdd in interface Matrix
      Parameters:
      B - Matrix such that B.numRows() == A.numColumns() and B.numColumns() == C.numColumns()
      C - Matrix such that C.numRows() == A.numRows() and B.numColumns() == C.numColumns()
      Returns:
      C
    • multAdd

      public Matrix multAdd(double alpha, Matrix B, Matrix C)
      Description copied from interface: Matrix
      C = alpha*A*B + C
      Specified by:
      multAdd in interface Matrix
      Parameters:
      B - Matrix such that B.numRows() == A.numColumns() and B.numColumns() == C.numColumns()
      C - Matrix such that C.numRows() == A.numRows() and B.numColumns() == C.numColumns()
      Returns:
      C
    • checkMultAdd

      protected void checkMultAdd(Matrix B, Matrix C)
      Checks the arguments to mult and multAdd
    • transAmult

      public Matrix transAmult(Matrix B, Matrix C)
      Description copied from interface: Matrix
      C = AT*B
      Specified by:
      transAmult in interface Matrix
      Parameters:
      B - Matrix such that B.numRows() == A.numRows() and B.numColumns() == C.numColumns()
      C - Matrix such that C.numRows() == A.numColumns() and B.numColumns() == C.numColumns()
      Returns:
      C
    • transAmult

      public Matrix transAmult(double alpha, Matrix B, Matrix C)
      Description copied from interface: Matrix
      C = alpha*AT*B
      Specified by:
      transAmult in interface Matrix
      Parameters:
      B - Matrix such that B.numRows() == A.numRows() and B.numColumns() == C.numColumns()
      C - Matrix such that C.numRows() == A.numColumns() and B.numColumns() == C.numColumns()
      Returns:
      C
    • transAmultAdd

      public Matrix transAmultAdd(Matrix B, Matrix C)
      Description copied from interface: Matrix
      C = AT*B + C
      Specified by:
      transAmultAdd in interface Matrix
      Parameters:
      B - Matrix such that B.numRows() == A.numRows() and B.numColumns() == C.numColumns()
      C - Matrix such that C.numRows() == A.numColumns() and B.numColumns() == C.numColumns()
      Returns:
      C
    • transAmultAdd

      public Matrix transAmultAdd(double alpha, Matrix B, Matrix C)
      Description copied from interface: Matrix
      C = alpha*AT*B + C
      Specified by:
      transAmultAdd in interface Matrix
      Parameters:
      B - Matrix such that B.numRows() == A.numRows() and B.numColumns() == C.numColumns()
      C - Matrix such that C.numRows() == A.numColumns() and B.numColumns() == C.numColumns()
      Returns:
      C
    • checkTransAmultAdd

      protected void checkTransAmultAdd(Matrix B, Matrix C)
      Checks the arguments to transAmult and transAmultAdd
    • transBmult

      public Matrix transBmult(Matrix B, Matrix C)
      Description copied from interface: Matrix
      C = A*BT
      Specified by:
      transBmult in interface Matrix
      Parameters:
      B - Matrix such that B.numRows() == A.numRows() and B.numColumns() == C.numColumns()
      C - Matrix such that C.numRows() == A.numColumns() and B.numColumns() == C.numColumns()
      Returns:
      C
    • transBmult

      public Matrix transBmult(double alpha, Matrix B, Matrix C)
      Description copied from interface: Matrix
      C = alpha*A*BT
      Specified by:
      transBmult in interface Matrix
      Parameters:
      B - Matrix such that B.numRows() == A.numRows() and B.numColumns() == C.numColumns()
      C - Matrix such that C.numRows() == A.numColumns() and B.numColumns() == C.numColumns()
      Returns:
      C
    • transBmultAdd

      public Matrix transBmultAdd(Matrix B, Matrix C)
      Description copied from interface: Matrix
      C = A*BT + C
      Specified by:
      transBmultAdd in interface Matrix
      Parameters:
      B - Matrix such that B.numRows() == A.numRows() and B.numColumns() == C.numColumns()
      C - Matrix such that C.numRows() == A.numColumns() and B.numColumns() == C.numColumns()
      Returns:
      C
    • transBmultAdd

      public Matrix transBmultAdd(double alpha, Matrix B, Matrix C)
      Description copied from interface: Matrix
      C = alpha*A*BT + C
      Specified by:
      transBmultAdd in interface Matrix
      Parameters:
      B - Matrix such that B.numRows() == A.numRows() and B.numColumns() == C.numColumns()
      C - Matrix such that C.numRows() == A.numColumns() and B.numColumns() == C.numColumns()
      Returns:
      C
    • checkTransBmultAdd

      protected void checkTransBmultAdd(Matrix B, Matrix C)
      Checks the arguments to transBmult and transBmultAdd
    • transABmult

      public Matrix transABmult(Matrix B, Matrix C)
      Description copied from interface: Matrix
      C = AT*BT
      Specified by:
      transABmult in interface Matrix
      Parameters:
      B - Matrix such that B.numColumns() == A.numRows() and B.numRows() == C.numColumns()
      C - Matrix such that C.numRows() == A.numColumns() and B.numRows() == C.numColumns()
      Returns:
      C
    • transABmult

      public Matrix transABmult(double alpha, Matrix B, Matrix C)
      Description copied from interface: Matrix
      C = alpha*AT*BT
      Specified by:
      transABmult in interface Matrix
      Parameters:
      B - Matrix such that B.numColumns() == A.numRows() and B.numRows() == C.numColumns()
      C - Matrix such that C.numRows() == A.numColumns() and B.numRows() == C.numColumns()
      Returns:
      C
    • transABmultAdd

      public Matrix transABmultAdd(Matrix B, Matrix C)
      Description copied from interface: Matrix
      C = AT*BT + C
      Specified by:
      transABmultAdd in interface Matrix
      Parameters:
      B - Matrix such that B.numColumns() == A.numRows() and B.numRows() == C.numColumns()
      C - Matrix such that C.numRows() == A.numColumns() and B.numRows() == C.numColumns()
      Returns:
      C
    • transABmultAdd

      public Matrix transABmultAdd(double alpha, Matrix B, Matrix C)
      Description copied from interface: Matrix
      C = alpha*AT*BT + C
      Specified by:
      transABmultAdd in interface Matrix
      Parameters:
      B - Matrix such that B.numColumns() == A.numRows() and B.numRows() == C.numColumns()
      C - Matrix such that C.numRows() == A.numColumns() and B.numRows() == C.numColumns()
      Returns:
      C
    • checkTransABmultAdd

      protected void checkTransABmultAdd(Matrix B, Matrix C)
      Checks the arguments to transABmultAdd and transABmultAdd
    • solve

      public Matrix solve(Matrix B, Matrix X)
      Description copied from interface: Matrix
      X = A\B. Not all matrices support this operation, those that do not throw UnsupportedOperationException. Note that it is often more efficient to use a matrix decomposition and its associated solver
      Specified by:
      solve in interface Matrix
      Parameters:
      B - Matrix with the same number of rows as A, and the same number of columns as X
      X - Matrix with a number of rows equal A.numColumns(), and the same number of columns as B
      Returns:
      X
    • transSolve

      public Matrix transSolve(Matrix B, Matrix X)
      Description copied from interface: Matrix
      X = AT\B. Not all matrices support this operation, those that do not throw UnsupportedOperationException. Note that it is often more efficient to use a matrix decomposition and its associated transpose solver
      Specified by:
      transSolve in interface Matrix
      Parameters:
      B - Matrix with a number of rows equal A.numColumns(), and the same number of columns as X
      X - Matrix with the same number of rows as A, and the same number of columns as B
      Returns:
      X
    • checkSolve

      protected void checkSolve(Matrix B, Matrix X)
      Checks that a matrix inversion is legal for the given arguments. This is for the square case, not for least-squares problems
    • rank1

      public Matrix rank1(Matrix C)
      Description copied from interface: Matrix
      A = C*CT + A. The matrices must be square and of the same size
      Specified by:
      rank1 in interface Matrix
      Returns:
      A
    • rank1

      public Matrix rank1(double alpha, Matrix C)
      Description copied from interface: Matrix
      A = alpha*C*CT + A. The matrices must be square and of the same size
      Specified by:
      rank1 in interface Matrix
      Returns:
      A
    • checkRank1

      protected void checkRank1(Matrix C)
      Checks that a matrix rank1 update is possible for the given matrix
    • transRank1

      public Matrix transRank1(Matrix C)
      Description copied from interface: Matrix
      A = CT*C + A The matrices must be square and of the same size
      Specified by:
      transRank1 in interface Matrix
      Returns:
      A
    • transRank1

      public Matrix transRank1(double alpha, Matrix C)
      Description copied from interface: Matrix
      A = alpha*CT*C + A The matrices must be square and of the same size
      Specified by:
      transRank1 in interface Matrix
      Returns:
      A
    • checkTransRank1

      protected void checkTransRank1(Matrix C)
      Checks that a transposed rank1 update is leagal with the given argument
    • rank2

      public Matrix rank2(Matrix B, Matrix C)
      Description copied from interface: Matrix
      A = B*CT + C*BT + A. This matrix must be square
      Specified by:
      rank2 in interface Matrix
      Parameters:
      B - Matrix with the same number of rows as A and the same number of columns as C
      C - Matrix with the same number of rows as A and the same number of columns as B
      Returns:
      A
    • rank2

      public Matrix rank2(double alpha, Matrix B, Matrix C)
      Description copied from interface: Matrix
      A = alpha*B*CT + alpha*C*BT + A. This matrix must be square
      Specified by:
      rank2 in interface Matrix
      Parameters:
      B - Matrix with the same number of rows as A and the same number of columns as C
      C - Matrix with the same number of rows as A and the same number of columns as B
      Returns:
      A
    • checkRank2

      protected void checkRank2(Matrix B, Matrix C)
      Checks that a rank2 update is legal for the given arguments
    • transRank2

      public Matrix transRank2(Matrix B, Matrix C)
      Description copied from interface: Matrix
      A = BT*C + CT*B + A. This matrix must be square
      Specified by:
      transRank2 in interface Matrix
      Parameters:
      B - Matrix with the same number of rows as C and the same number of columns as A
      C - Matrix with the same number of rows as B and the same number of columns as A
      Returns:
      A
    • transRank2

      public Matrix transRank2(double alpha, Matrix B, Matrix C)
      Description copied from interface: Matrix
      A = alpha*BT*C + alpha*CT*B + A. This matrix must be square
      Specified by:
      transRank2 in interface Matrix
      Parameters:
      B - Matrix with the same number of rows as C and the same number of columns as A
      C - Matrix with the same number of rows as B and the same number of columns as A
      Returns:
      A
    • checkTransRank2

      protected void checkTransRank2(Matrix B, Matrix C)
      Checks that a transposed rank2 update is leagal with the given arguments
    • scale

      public Matrix scale(double alpha)
      Description copied from interface: Matrix
      A = alpha*A
      Specified by:
      scale in interface Matrix
      Returns:
      A
    • set

      public Matrix set(Matrix B)
      Description copied from interface: Matrix
      A=B. The matrices must be of the same size
      Specified by:
      set in interface Matrix
      Returns:
      A
    • set

      public Matrix set(double alpha, Matrix B)
      Description copied from interface: Matrix
      A=alpha*B. The matrices must be of the same size
      Specified by:
      set in interface Matrix
      Returns:
      A
    • add

      public Matrix add(Matrix B)
      Description copied from interface: Matrix
      A = B + A. The matrices must be of the same size
      Specified by:
      add in interface Matrix
      Returns:
      A
    • add

      public Matrix add(double alpha, Matrix B)
      Description copied from interface: Matrix
      A = alpha*B + A. The matrices must be of the same size
      Specified by:
      add in interface Matrix
      Returns:
      A
    • checkSize

      protected void checkSize(Matrix B)
      Checks that the sizes of this matrix and the given conform
    • transpose

      public Matrix transpose()
      Description copied from interface: Matrix
      Transposes the matrix in-place. In most cases, the matrix must be square for this to work.
      Specified by:
      transpose in interface Matrix
      Returns:
      This matrix
    • checkTranspose

      protected void checkTranspose()
      Checks that the matrix may be transposed
    • transpose

      public Matrix transpose(Matrix B)
      Description copied from interface: Matrix
      Sets the tranpose of this matrix into B. Matrix dimensions must be compatible
      Specified by:
      transpose in interface Matrix
      Parameters:
      B - Matrix with as many rows as this matrix has columns, and as many columns as this matrix has rows
      Returns:
      The matrix B=AT
    • checkTranspose

      protected void checkTranspose(Matrix B)
      Checks that this matrix can be transposed into the given matrix
    • norm

      public double norm(Matrix.Norm type)
      Description copied from interface: Matrix
      Computes the given norm of the matrix
      Specified by:
      norm in interface Matrix
      Parameters:
      type - The type of norm to compute
    • norm1

      protected double norm1()
      Computes the 1 norm
    • normF

      protected double normF()
      Computes the Frobenius norm. This implementation is overflow resistant
    • normInf

      protected double normInf()
      Computes the infinity norm
    • max

      protected double max()
      Returns the largest absolute value
    • max

      protected double max(double[] x)
      Returns the largest element of the passed array
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • iterator

      public Iterator<MatrixEntry> iterator()
      Specified by:
      iterator in interface Iterable<MatrixEntry>