Class MatrixUtils

java.lang.Object
edu.cmu.tetrad.util.MatrixUtils

public final class MatrixUtils extends Object
Class Matrix includes several public static functions performing matrix operations. These function include: determinant, GJinverse, inverse, multiple, difference, transpose, trace, duplicate, minor, identity, mprint, impliedCovar, SEMimpliedCovar.
Version:
$Id: $Id
Author:
Tianjiao Chu, josephramsey, Kevin V. Bui
  • Method Summary

    Modifier and Type
    Method
    Description
    static double[][]
    asRow(double[] v)
    asRow.
    static Matrix
    cholesky.
    static double[]
    concatenate(double[][] vectors)
    Concatenates the vectors rows[i], i = 0...rows.length, into a single vector.
    static Matrix
    Converts a covariance matrix to a correlation matrix in place; the same matrix is returned for convenience, but m is modified in the process.
    static double[][]
    Converts a matrix in lower triangular form to a symmetric matrix in square form.
    static double[][]
    copyOf(double[][] arr)
    copyOf.
    static int[]
    copyOf(int[] arr, int length)
    copyOf.
    static double
    determinant(double[][] m)
    determinant.
    static double[][]
    directProduct(double[][] ma, double[][] mb)
    Computes the direct (Kronecker) outerProduct.
    static boolean
    equals(double[][] ma, double[][] mb)
    Tests two matrices for equality.
    static boolean
    equals(double[][] ma, double[][] mb, double tolerance)
    Tests to see whether two matrices are equal within the given tolerance.
    static boolean
    equals(double[] va, double[] vb)
    Tests two vectors for equality.
    static boolean
    equals(double[] va, double[] vb, double tolerance)
    Tests to see whether two vectors are equal within the given tolerance.
    static boolean
    hasDimensions(double[][] m, int i, int j)
    hasDimensions.
    static double[][]
    identity(int size)
    identity.
    static Matrix
    impliedCovar(Matrix edgeCoef, Matrix errCovar)
    impliedCovar.
    static Matrix
    impliedCovar2(Matrix edgeCoef, Matrix errCovar)
    impliedCovar2.
    static double
    innerProduct(double[] ma, double[] mb)
    innerProduct.
    static double[][]
    inverse(double[][] m)
    inverse.
    static double[][]
    invVech(double[] vech)
    invVech.
    static boolean
    Return true if the given matrix is symmetric positive definite--that is, if it would make a valid covariance matrix.
    static boolean
    isSquare(double[][] m)
    isSquare.
    static boolean
    isSymmetric(double[][] m, double tolerance)
    isSymmetric.
    static double[][]
    outerProduct(double[] ma, double[] mb)
    outerProduct.
    static double[]
    product(double[][] ma, double[] mb)
    product.
    static double[][]
    product(double[][] ma, double[][] mb)
    product.
    static double[]
    product(double[] ma, double[][] mb)
    product.
    static Vector
    product.
    static double[][]
    pseudoInverse(double[][] x)
    pseudoInverse.
    static double[][]
    repmat(double[][] mat, int n)
    Make a n repeat copy of the rows and columns of the matrix mat.
    static double[][]
    repmat(double[][] mat, int nRow, int mColumn)
    Make a repeat copy of matrix mat.
    static double[]
    scalarProduct(double scalar, double[] m)
    scalarProduct.
    static double[][]
    scalarProduct(double scalar, double[][] m)
    Multiplies the given matrix through by the given scalar.
    static double[][]
    submatrix(double[][] m, int rem)
    A copy of the original (square) matrix with the stated index row/column removed
    static double[][]
    subtract(double[][] ma, double[][] mb)
    subtract.
    static double[]
    subtract(double[] ma, double[] mb)
    subtract.
    static double[][]
    sum(double[][] ma, double[][] mb)
    sum.
    static double[]
    sum(double[] ma, double[] mb)
    sum.
    static int
    sum0ToN(int n)
    sum0ToN.
    static String
    toString(boolean[][] m)
    Copies the given array, starting each line with a tab character..
    static String
    toString(double[] m)
    toString.
    static String
    toString(double[][] m)
    Copies the given array, using a standard scientific notation number formatter and beginning each line with a tab character.
    static String
    toString(double[][] m, List<String> variables)
    toString.
    static String
    toString(double[] m, NumberFormat nf)
    toString.
    static String
    toString(int[] m)
    toString.
    static String
    toString(int[][] m)
    toString.
    static String
    toString(int[][] m, List<String> variables)
    toString.
    static String
    toStringSquare(double[][] m, NumberFormat nf, List<String> variables)
    toStringSquare.
    static String
    toStringSquare(double[][] m, List<String> variables)
    toStringSquare.
    static String
    toStringSquare(int[][] m, List<String> variables)
    toStringSquare.
    static double
    trace(double[][] m)
    trace.
    static double[][]
    transpose(double[][] m)
    transpose.
    static org.apache.commons.math3.linear.RealMatrix
    transposeWithoutCopy(org.apache.commons.math3.linear.RealMatrix apacheData)
    transposeWithoutCopy.
    static double[][]
    vec(double[][] m)
    vec.
    static double[][]
    vech(double[][] m)
    vech.
    static double[][]
    The matrix which, when postmultiplied by vech, return vec.
    static double[][]
    zeros(int rows, int cols)
    zeros.

    Methods inherited from class java.lang.Object

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

    • repmat

      public static double[][] repmat(double[][] mat, int nRow, int mColumn)
      Make a repeat copy of matrix mat.
      Parameters:
      mat - matrix to copy
      nRow - number of repeat copy of row
      mColumn - number of repeat copy of column
      Returns:
      an array of double objects
    • repmat

      public static double[][] repmat(double[][] mat, int n)
      Make a n repeat copy of the rows and columns of the matrix mat.
      Parameters:
      n - number of repeat copy
      mat - an array of double objects
      Returns:
      an array of double objects
    • equals

      public static boolean equals(double[][] ma, double[][] mb)
      Tests two matrices for equality.
      Parameters:
      ma - The first 2D matrix to check.
      mb - The second 2D matrix to check.
      Returns:
      True iff the first and second matrices are equal.
    • equals

      public static boolean equals(double[] va, double[] vb)
      Tests two vectors for equality.
      Parameters:
      va - The first 1D matrix to check.
      vb - The second 1D matrix to check.
      Returns:
      True iff the first and second matrices are equal.
    • equals

      public static boolean equals(double[][] ma, double[][] mb, double tolerance)
      Tests to see whether two matrices are equal within the given tolerance. If any two corresponding elements differ by more than the given tolerance, false is returned.
      Parameters:
      ma - The first 2D matrix to check.
      mb - The second 2D matrix to check.
      tolerance - A double >= 0.
      Returns:
      Ibid.
    • equals

      public static boolean equals(double[] va, double[] vb, double tolerance)
      Tests to see whether two vectors are equal within the given tolerance. If any two corresponding elements differ by more than the given tolerance, false is returned.
      Parameters:
      va - The first matrix to check.
      vb - The second matrix to check.
      tolerance - A double >= 0.
      Returns:
      Ibid.
    • isSquare

      public static boolean isSquare(double[][] m)

      isSquare.

      Parameters:
      m - A 2D double matrix.
      Returns:
      Ibid.
    • isSymmetric

      public static boolean isSymmetric(double[][] m, double tolerance)

      isSymmetric.

      Parameters:
      m - The matrix to check.
      tolerance - A double >= 0.
      Returns:
      Ibid.
    • determinant

      public static double determinant(double[][] m)

      determinant.

      Parameters:
      m - The matrix whose determinant is sought. Must be square.
      Returns:
      Ibid.
    • submatrix

      public static double[][] submatrix(double[][] m, int rem)
      A copy of the original (square) matrix with the stated index row/column removed
      Parameters:
      m - an array of double objects
      rem - a int
      Returns:
      an array of double objects
    • inverse

      public static double[][] inverse(double[][] m)

      inverse.

      Parameters:
      m - an array of double objects
      Returns:
      the inverse of the given square matrix if it is nonsingular, otherwise the pseudoinverse.
    • pseudoInverse

      public static double[][] pseudoInverse(double[][] x)

      pseudoInverse.

      Parameters:
      x - an array of double objects
      Returns:
      an array of double objects
    • product

      public static double[][] product(double[][] ma, double[][] mb)

      product.

      Parameters:
      ma - an array of double objects
      mb - an array of double objects
      Returns:
      the outerProduct of ma and mb. The dimensions of ma and mb must be compatible for multiplication.
    • product

      public static double[] product(double[] ma, double[][] mb)

      product.

      Parameters:
      ma - an array of double objects
      mb - an array of double objects
      Returns:
      an array of double objects
    • product

      public static Vector product(Vector ma, Matrix mb)

      product.

      Parameters:
      ma - a Vector object
      mb - a Matrix object
      Returns:
      a Vector object
    • product

      public static double[] product(double[][] ma, double[] mb)

      product.

      Parameters:
      ma - an array of double objects
      mb - an array of double objects
      Returns:
      an array of double objects
    • outerProduct

      public static double[][] outerProduct(double[] ma, double[] mb)

      outerProduct.

      Parameters:
      ma - an array of double objects
      mb - an array of double objects
      Returns:
      an array of double objects
    • innerProduct

      public static double innerProduct(double[] ma, double[] mb)

      innerProduct.

      Parameters:
      ma - an array of double objects
      mb - an array of double objects
      Returns:
      a double
    • transpose

      public static double[][] transpose(double[][] m)

      transpose.

      Parameters:
      m - an array of double objects
      Returns:
      the transpose of the given matrix.
    • trace

      public static double trace(double[][] m)

      trace.

      Parameters:
      m - an array of double objects
      Returns:
      the trace of the given (square) m.
    • identity

      public static double[][] identity(int size)

      identity.

      Parameters:
      size - a int
      Returns:
      the identity matrix of the given order.
    • sum

      public static double[][] sum(double[][] ma, double[][] mb)

      sum.

      Parameters:
      ma - an array of double objects
      mb - an array of double objects
      Returns:
      the sum of ma and mb.
    • sum

      public static double[] sum(double[] ma, double[] mb)

      sum.

      Parameters:
      ma - an array of double objects
      mb - an array of double objects
      Returns:
      an array of double objects
    • subtract

      public static double[][] subtract(double[][] ma, double[][] mb)

      subtract.

      Parameters:
      ma - an array of double objects
      mb - an array of double objects
      Returns:
      an array of double objects
    • subtract

      public static double[] subtract(double[] ma, double[] mb)

      subtract.

      Parameters:
      ma - an array of double objects
      mb - an array of double objects
      Returns:
      an array of double objects
    • directProduct

      public static double[][] directProduct(double[][] ma, double[][] mb)
      Computes the direct (Kronecker) outerProduct.
      Parameters:
      ma - an array of double objects
      mb - an array of double objects
      Returns:
      an array of double objects
    • scalarProduct

      public static double[][] scalarProduct(double scalar, double[][] m)
      Multiplies the given matrix through by the given scalar.
      Parameters:
      scalar - a double
      m - an array of double objects
      Returns:
      an array of double objects
    • scalarProduct

      public static double[] scalarProduct(double scalar, double[] m)

      scalarProduct.

      Parameters:
      scalar - a double
      m - an array of double objects
      Returns:
      an array of double objects
    • concatenate

      public static double[] concatenate(double[][] vectors)
      Concatenates the vectors rows[i], i = 0...rows.length, into a single vector.
      Parameters:
      vectors - an array of double objects
      Returns:
      an array of double objects
    • asRow

      public static double[][] asRow(double[] v)

      asRow.

      Parameters:
      v - an array of double objects
      Returns:
      the vector as a 1 x n row matrix.
    • impliedCovar2

      public static Matrix impliedCovar2(Matrix edgeCoef, Matrix errCovar)

      impliedCovar2.

      Parameters:
      edgeCoef - The edge covariance matrix. edgeCoef(i, j) is a parameter in this matrix just in case i-->j is an edge in the model. All other entries in the matrix are zero.
      errCovar - The error covariance matrix. errCovar(i, i) is the variance of i; off-diagonal errCovar(i, j) are covariance parameters that are specified in the model. All other matrix entries are zero.
      Returns:
      The implied covariance matrix, which is the covariance matrix over the measured variables that is implied by all the given information.
      Throws:
      IllegalArgumentException - if edgeCoef or errCovar contains an undefined value (Double.NaN).
    • impliedCovar

      public static Matrix impliedCovar(Matrix edgeCoef, Matrix errCovar)

      impliedCovar.

      Parameters:
      edgeCoef - a Matrix object
      errCovar - a Matrix object
      Returns:
      a Matrix object
    • vech

      public static double[][] vech(double[][] m)

      vech.

      Parameters:
      m - an array of double objects
      Returns:
      vech of the given array. (This is what you get when you stack all of the elements of m in the lower triangular of m to form a vector. The elements are stacked in columns left to right, top to bottom.)
    • invVech

      public static double[][] invVech(double[] vech)

      invVech.

      Parameters:
      vech - an array of double objects
      Returns:
      the symmetric matrix for which the given array is the vech.
    • vec

      public static double[][] vec(double[][] m)

      vec.

      Parameters:
      m - an array of double objects
      Returns:
      vech of the given array. (This is what you get when you stack all of the elements of m to form a vector. The elements are stacked in columns left to right, top to bottom.)
    • sum0ToN

      public static int sum0ToN(int n)

      sum0ToN.

      Parameters:
      n - a int
      Returns:
      the sum of integers from 0 up to n.
    • vechToVecLeft

      public static double[][] vechToVecLeft(int n)
      The matrix which, when postmultiplied by vech, return vec.
      Parameters:
      n - the size of the square matrix that vec and vech come from.
      Returns:
      an array of double objects
    • hasDimensions

      public static boolean hasDimensions(double[][] m, int i, int j)

      hasDimensions.

      Parameters:
      m - an array of double objects
      i - a int
      j - a int
      Returns:
      true just in case the given matrix has the given dimensions --that is, just in case m.length == i and m[0].length == j.
    • zeros

      public static double[][] zeros(int rows, int cols)

      zeros.

      Parameters:
      rows - a int
      cols - a int
      Returns:
      an array of double objects
    • isPositiveDefinite

      public static boolean isPositiveDefinite(Matrix matrix)
      Return true if the given matrix is symmetric positive definite--that is, if it would make a valid covariance matrix.
      Parameters:
      matrix - a Matrix object
      Returns:
      a boolean
    • cholesky

      public static Matrix cholesky(Matrix covar)

      cholesky.

      Parameters:
      covar - a Matrix object
      Returns:
      a Matrix object
    • convertCovToCorr

      public static Matrix convertCovToCorr(Matrix m)
      Converts a covariance matrix to a correlation matrix in place; the same matrix is returned for convenience, but m is modified in the process.
      Parameters:
      m - a Matrix object
      Returns:
      a Matrix object
    • convertLowerTriangleToSymmetric

      public static double[][] convertLowerTriangleToSymmetric(double[][] arr)
      Converts a matrix in lower triangular form to a symmetric matrix in square form. The lower triangular matrix need not contain matrix elements to represent elements in the upper triangle.
      Parameters:
      arr - an array of double objects
      Returns:
      an array of double objects
    • toString

      public static String toString(double[][] m)
      Copies the given array, using a standard scientific notation number formatter and beginning each line with a tab character. The number format is DecimalFormat(" 0.0000;-0.0000").
      Parameters:
      m - an array of double objects
      Returns:
      a String object
    • toString

      public static String toString(double[][] m, List<String> variables)

      toString.

      Parameters:
      m - an array of double objects
      variables - a List object
      Returns:
      a String object
    • toStringSquare

      public static String toStringSquare(double[][] m, List<String> variables)

      toStringSquare.

      Parameters:
      m - an array of double objects
      variables - a List object
      Returns:
      a String object
    • toStringSquare

      public static String toStringSquare(double[][] m, NumberFormat nf, List<String> variables)

      toStringSquare.

      Parameters:
      m - an array of double objects
      nf - a NumberFormat object
      variables - a List object
      Returns:
      a String object
    • toString

      public static String toString(int[] m)

      toString.

      Parameters:
      m - an array of int objects
      Returns:
      a String object
    • toString

      public static String toString(int[][] m, List<String> variables)

      toString.

      Parameters:
      m - an array of int objects
      variables - a List object
      Returns:
      a String object
    • toStringSquare

      public static String toStringSquare(int[][] m, List<String> variables)

      toStringSquare.

      Parameters:
      m - an array of int objects
      variables - a List object
      Returns:
      a String object
    • toString

      public static String toString(double[] m)

      toString.

      Parameters:
      m - an array of double objects
      Returns:
      a String object
    • toString

      public static String toString(double[] m, NumberFormat nf)

      toString.

      Parameters:
      m - an array of double objects
      nf - a NumberFormat object
      Returns:
      a String object
    • toString

      public static String toString(int[][] m)

      toString.

      Parameters:
      m - an array of int objects
      Returns:
      a String object
    • toString

      public static String toString(boolean[][] m)
      Copies the given array, starting each line with a tab character..
      Parameters:
      m - an array of boolean objects
      Returns:
      a String object
    • copyOf

      public static int[] copyOf(int[] arr, int length)

      copyOf.

      Parameters:
      arr - an array of int objects
      length - a int
      Returns:
      an array of int objects
    • copyOf

      public static double[][] copyOf(double[][] arr)

      copyOf.

      Parameters:
      arr - an array of double objects
      Returns:
      an array of double objects
    • transposeWithoutCopy

      public static org.apache.commons.math3.linear.RealMatrix transposeWithoutCopy(org.apache.commons.math3.linear.RealMatrix apacheData)

      transposeWithoutCopy.

      Parameters:
      apacheData - a RealMatrix object
      Returns:
      a RealMatrix object