Class MatrixOps

java.lang.Object
org.cicirello.math.la.MatrixOps

public final class MatrixOps extends Object
Utility class of basic linear algebra matrix operations, where matrices are represented as 2-D Java arrays.
  • Method Summary

    Modifier and Type
    Method
    Description
    static double[][]
    difference(double[][] A, double[][] B)
    Computes C = A - B.
    static double[][]
    difference(double[][] A, double[][] B, double[][] C)
    Computes C = A - B.
    static int[][]
    difference(int[][] A, int[][] B)
    Computes C = A - B.
    static int[][]
    difference(int[][] A, int[][] B, int[][] C)
    Computes C = A - B.
    static double[][]
    product(double[][] A, double[][] B)
    Computes C = A * B.
    static double[][]
    product(double[][] A, double[][] B, double[][] C)
    Computes C = A * B.
    static int[][]
    product(int[][] A, int[][] B)
    Computes C = A * B.
    static int[][]
    product(int[][] A, int[][] B, int[][] C)
    Computes C = A * B.
    static double[][]
    sum(double[][] A, double[][] B)
    Computes C = A + B.
    static double[][]
    sum(double[][] A, double[][] B, double[][] C)
    Computes C = A + B.
    static int[][]
    sum(int[][] A, int[][] B)
    Computes C = A + B.
    static int[][]
    sum(int[][] A, int[][] B, int[][] C)
    Computes C = A + B.
    static double[][]
    transposeSquareMatrixInline(double[][] matrix)
    Transpose a square matrix inline.
    static int[][]
    Transpose a square matrix inline.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • transposeSquareMatrixInline

      public static int[][] transposeSquareMatrixInline(int[][] matrix)
      Transpose a square matrix inline.
      Parameters:
      matrix - the matrix to transpose, with result replacing contents of matrix.
      Returns:
      The matrix is returned for convenience for passing to other methods requiring a matrix as parameter. It is the same object reference that was passed as a parameter.
      Throws:
      NullPointerException - if matrix is null.
      ArrayIndexOutOfBoundsException - if matrix is not square.
    • transposeSquareMatrixInline

      public static double[][] transposeSquareMatrixInline(double[][] matrix)
      Transpose a square matrix inline.
      Parameters:
      matrix - the matrix to transpose, with result replacing contents of matrix.
      Returns:
      The matrix is returned for convenience for passing to other methods requiring a matrix as parameter. It is the same object reference that was passed as a parameter.
      Throws:
      NullPointerException - if matrix is null.
      ArrayIndexOutOfBoundsException - if matrix is not square.
    • sum

      public static int[][] sum(int[][] A, int[][] B, int[][] C)
      Computes C = A + B.
      Parameters:
      A - matrix
      B - matrix
      C - if C is null then a new matrix is constructed for result, otherwise C is used for result.
      Returns:
      A reference to the C matrix.
      Throws:
      NullPointerException - if either A or B is null.
      ArrayIndexOutOfBoundsException - if A and B are of differing dimensions.
      ArrayIndexOutOfBoundsException - if C is non-null, but different dimensions than A and B.
    • sum

      public static double[][] sum(double[][] A, double[][] B, double[][] C)
      Computes C = A + B.
      Parameters:
      A - matrix
      B - matrix
      C - if C is null then a new matrix is constructed for result, otherwise C is used for result.
      Returns:
      A reference to the C matrix.
      Throws:
      NullPointerException - if either A or B is null.
      ArrayIndexOutOfBoundsException - if A and B are of differing dimensions.
      ArrayIndexOutOfBoundsException - if C is non-null, but different dimensions than A and B.
    • sum

      public static int[][] sum(int[][] A, int[][] B)
      Computes C = A + B.
      Parameters:
      A - matrix
      B - matrix
      Returns:
      A reference to a new matrix C containing the sum.
      Throws:
      NullPointerException - if either A or B is null.
      ArrayIndexOutOfBoundsException - if A and B are of differing dimensions.
    • sum

      public static double[][] sum(double[][] A, double[][] B)
      Computes C = A + B.
      Parameters:
      A - matrix
      B - matrix
      Returns:
      A reference to a new matrix C containing the sum.
      Throws:
      NullPointerException - if either A or B is null.
      ArrayIndexOutOfBoundsException - if A and B are of differing dimensions.
    • difference

      public static int[][] difference(int[][] A, int[][] B, int[][] C)
      Computes C = A - B.
      Parameters:
      A - matrix
      B - matrix
      C - if C is null then a new matrix is constructed for result, otherwise C is used for result.
      Returns:
      A reference to the C matrix.
      Throws:
      NullPointerException - if either A or B is null.
      ArrayIndexOutOfBoundsException - if A and B are of differing dimensions.
      ArrayIndexOutOfBoundsException - if C is non-null, but different dimensions than A and B.
    • difference

      public static double[][] difference(double[][] A, double[][] B, double[][] C)
      Computes C = A - B.
      Parameters:
      A - matrix
      B - matrix
      C - if C is null then a new matrix is constructed for result, otherwise C is used for result.
      Returns:
      A reference to the C matrix.
      Throws:
      NullPointerException - if either A or B is null.
      ArrayIndexOutOfBoundsException - if A and B are of differing dimensions.
      ArrayIndexOutOfBoundsException - if C is non-null, but different dimensions than A and B.
    • difference

      public static int[][] difference(int[][] A, int[][] B)
      Computes C = A - B.
      Parameters:
      A - matrix
      B - matrix
      Returns:
      A reference to a new matrix C containing the difference.
      Throws:
      NullPointerException - if either A or B is null.
      ArrayIndexOutOfBoundsException - if A and B are of differing dimensions.
    • difference

      public static double[][] difference(double[][] A, double[][] B)
      Computes C = A - B.
      Parameters:
      A - matrix
      B - matrix
      Returns:
      A reference to a new matrix C containing the difference.
      Throws:
      NullPointerException - if either A or B is null.
      ArrayIndexOutOfBoundsException - if A and B are of differing dimensions.
    • product

      public static int[][] product(int[][] A, int[][] B, int[][] C)
      Computes C = A * B.
      Parameters:
      A - matrix
      B - matrix
      C - if C is null then a new matrix is constructed for result, otherwise C is used for result.
      Returns:
      A reference to the C matrix.
      Throws:
      NullPointerException - if either A or B is null.
      ArrayIndexOutOfBoundsException - if number of columns of A is not equal to number of rows of B.
      ArrayIndexOutOfBoundsException - if C is non-null, but dimensions inconsistent with A and B (must have same number of rows of A and same number of columns of B).
    • product

      public static double[][] product(double[][] A, double[][] B, double[][] C)
      Computes C = A * B.
      Parameters:
      A - matrix
      B - matrix
      C - if C is null then a new matrix is constructed for result, otherwise C is used for result.
      Returns:
      A reference to the C matrix.
      Throws:
      NullPointerException - if either A or B is null.
      ArrayIndexOutOfBoundsException - if number of columns of A is not equal to number of rows of B.
      ArrayIndexOutOfBoundsException - if C is non-null, but dimensions inconsistent with A and B (must have same number of rows of A and same number of columns of B).
    • product

      public static int[][] product(int[][] A, int[][] B)
      Computes C = A * B.
      Parameters:
      A - matrix
      B - matrix
      Returns:
      A reference to a new matrix C containing the product.
      Throws:
      NullPointerException - if either A or B is null.
      ArrayIndexOutOfBoundsException - if number of columns of A is not equal to number of rows of B.
    • product

      public static double[][] product(double[][] A, double[][] B)
      Computes C = A * B.
      Parameters:
      A - matrix
      B - matrix
      Returns:
      A reference to a new matrix C containing the product.
      Throws:
      NullPointerException - if either A or B is null.
      ArrayIndexOutOfBoundsException - if number of columns of A is not equal to number of rows of B.