Package edu.cmu.tetrad.util
Class MatrixUtils
java.lang.Object
edu.cmu.tetrad.util.MatrixUtils
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 TypeMethodDescriptionstatic double[][]asRow(double[] v) asRow.static Matrixcholesky.static double[]concatenate(double[][] vectors) Concatenates the vectors rows[i], i = 0...rows.length, into a single vector.static MatrixConverts a covariance matrix to a correlation matrix in place; the same matrix is returned for convenience, but m is modified in the process.static org.ejml.simple.SimpleMatrixconvertCovToCorr(org.ejml.simple.SimpleMatrix covarianceMatrix) Converts a covariance matrix into a correlation matrix.static double[][]convertLowerTriangleToSymmetric(double[][] arr) 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 doubledeterminant(double[][] m) determinant.static double[][]directProduct(double[][] ma, double[][] mb) Computes the direct (Kronecker) outerProduct.static booleanequals(double[][] ma, double[][] mb) Tests two matrices for equality.static booleanequals(double[][] ma, double[][] mb, double tolerance) Tests to see whether two matrices are equal within the given tolerance.static booleanequals(double[] va, double[] vb) Tests two vectors for equality.static booleanequals(double[] va, double[] vb, double tolerance) Tests to see whether two vectors are equal within the given tolerance.static booleanhasDimensions(double[][] m, int i, int j) hasDimensions.static org.ejml.simple.SimpleMatrixidentity(int size) identity.static MatriximpliedCovar(Matrix edgeCoef, Matrix errCovar) Calculates the implied covariance matrix from the given edge coefficient matrix and error covariance matrix.static doubleinnerProduct(double[] ma, double[] mb) innerProduct.static double[][]inverse(double[][] m) Calculates the inverse of a given matrix.static double[][]invVech(double[] vech) invVech.static booleanisPositiveDefinite(Matrix matrix) Return true if the given matrix is symmetric positive definite--that is, if it would make a valid covariance matrix.static booleanisSquare(double[][] m) isSquare.static booleanisSymmetric(double[][] m, double tolerance) isSymmetric.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 Vectorproduct.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 removedstatic 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 intsum0ToN(int n) sum0ToN.static Matrixsymmetrize(Matrix sigma) Computes the symmetrized version of the given square matrix.static StringtoString(boolean[][] m) Copies the given array, starting each line with a tab character..static StringtoString(double[] m) toString.static StringtoString(double[][] m) Copies the given array, using a standard scientific notation number formatter and beginning each line with a tab character.static StringtoString(double[][] m, NumberFormat nf) Converts a 2D array of doubles into a String representation using the specified NumberFormat.static StringtoString(double[][] m, NumberFormat nf, List<String> variables) Converts a two-dimensional array representing a matrix into a string representation using the specified number format and variable names.static StringtoString.static StringtoString(double[] m, NumberFormat nf) toString.static StringtoString(int[] m) toString.static StringtoString(int[][] m) toString.static StringtoString.static StringtoStringSquare(double[][] m, NumberFormat nf, List<String> variables) toStringSquare.static StringtoStringSquare(double[][] m, List<String> variables) toStringSquare.static StringtoStringSquare(int[][] m, List<String> variables) toStringSquare.static doubletrace(double[][] m) trace.static double[][]transpose(double[][] m) transpose.static double[][]vec(double[][] m) vec.static double[][]vech(double[][] m) vech.static double[][]vechToVecLeft(int n) The matrix which, when postmultiplied by vech, return vec.static double[][]zeros(int rows, int cols) zeros.
-
Method Details
-
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 objectsrem- a int- Returns:
- an array of objects
-
inverse
public static double[][] inverse(double[][] m) Calculates the inverse of a given matrix.- Parameters:
m- the input matrix to calculate the inverse of- Returns:
- the inverse of the input matrix as a 2D array of doubles
-
product
public static double[][] product(double[][] ma, double[][] mb) product.
- Parameters:
ma- an array of objectsmb- an array of 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 objectsmb- an array of objects- Returns:
- an array of objects
-
product
product.
-
product
public static double[] product(double[][] ma, double[] mb) product.
- Parameters:
ma- an array of objectsmb- an array of objects- Returns:
- an array of objects
-
innerProduct
public static double innerProduct(double[] ma, double[] mb) innerProduct.
- Parameters:
ma- an array of objectsmb- an array of objects- Returns:
- a double
-
transpose
public static double[][] transpose(double[][] m) transpose.
- Parameters:
m- an array of objects- Returns:
- the transpose of the given matrix.
-
trace
public static double trace(double[][] m) trace.
- Parameters:
m- an array of objects- Returns:
- the trace of the given (square) m.
-
identity
public static org.ejml.simple.SimpleMatrix 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 objectsmb- an array of objects- Returns:
- the sum of ma and mb.
-
sum
public static double[] sum(double[] ma, double[] mb) sum.
- Parameters:
ma- an array of objectsmb- an array of objects- Returns:
- an array of objects
-
subtract
public static double[][] subtract(double[][] ma, double[][] mb) subtract.
- Parameters:
ma- an array of objectsmb- an array of objects- Returns:
- an array of objects
-
subtract
public static double[] subtract(double[] ma, double[] mb) subtract.
- Parameters:
ma- an array of objectsmb- an array of objects- Returns:
- an array of objects
-
directProduct
public static double[][] directProduct(double[][] ma, double[][] mb) Computes the direct (Kronecker) outerProduct.- Parameters:
ma- an array of objectsmb- an array of objects- Returns:
- an array of objects
-
scalarProduct
public static double[][] scalarProduct(double scalar, double[][] m) Multiplies the given matrix through by the given scalar.- Parameters:
scalar- a doublem- an array of objects- Returns:
- an array of objects
-
scalarProduct
public static double[] scalarProduct(double scalar, double[] m) scalarProduct.
- Parameters:
scalar- a doublem- an array of objects- Returns:
- an array of 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 objects- Returns:
- an array of objects
-
asRow
public static double[][] asRow(double[] v) asRow.
- Parameters:
v- an array of objects- Returns:
- the vector as a 1 x n row matrix.
-
impliedCovar
Calculates the implied covariance matrix from the given edge coefficient matrix and error covariance matrix. This method assumes that the provided matrices satisfy the necessary mathematical properties for the computation.- Parameters:
edgeCoef- the edge coefficient matrix, representing direct effects among variables. Must not contain undefined (NaN) values.errCovar- the error covariance matrix, representing variances and covariances of errors. Must not contain undefined (NaN) values.- Returns:
- the implied covariance matrix, computed as ((I - B)â»Â¹) Cov(e) ((I - B)â»Â¹)áµ, where B is the edge coefficient matrix and Cov(e) is the error covariance matrix.
- Throws:
IllegalArgumentException- if either the edge coefficient matrix or the error covariance matrix contains undefined (NaN) values.
-
vech
public static double[][] vech(double[][] m) vech.
- Parameters:
m- an array of 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 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 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 objects
-
hasDimensions
public static boolean hasDimensions(double[][] m, int i, int j) hasDimensions.
- Parameters:
m- an array of objectsi- a intj- 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 intcols- a int- Returns:
- an array of objects
-
isPositiveDefinite
Return true if the given matrix is symmetric positive definite--that is, if it would make a valid covariance matrix.- Parameters:
matrix- aMatrixobject- Returns:
- a boolean
-
cholesky
cholesky.
-
convertCovToCorr
Converts a covariance matrix to a correlation matrix in place; the same matrix is returned for convenience, but m is modified in the process. -
convertCovToCorr
public static org.ejml.simple.SimpleMatrix convertCovToCorr(org.ejml.simple.SimpleMatrix covarianceMatrix) Converts a covariance matrix into a correlation matrix. The correlation matrix is computed by normalizing the covariance matrix using the standard deviations derived from the diagonal elements of the covariance matrix.- Parameters:
covarianceMatrix- the input covariance matrix to be converted. It is assumed to be square and symmetric.- Returns:
- the resulting correlation matrix where each element is scaled by the product of the standard deviations of the corresponding variables.
-
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 objects- Returns:
- an array of objects
-
toString
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 objects- Returns:
- a
Stringobject
-
toString
toString.
-
toString
Converts a 2D array of doubles into a String representation using the specified NumberFormat. This method delegates the formatting process to MatrixUtils.toString.- Parameters:
m- the 2D double array to be converted to a stringnf- the NumberFormat to use for formatting the numbers in the array- Returns:
- a String representation of the 2D array formatted using the specified NumberFormat
-
toString
Converts a two-dimensional array representing a matrix into a string representation using the specified number format and variable names.- Parameters:
m- the matrix to convert, represented as a two-dimensional array of double values. Can be null.nf- the number format to use for formatting the individual matrix elements. Must not be null.variables- a list of variable names corresponding to the columns of the matrix. If null, default variable names will be generated.- Returns:
- a string representation of the matrix. If the matrix is null or empty, a default message is returned.
- Throws:
NullPointerException- if the provided number format is null.
-
toStringSquare
toStringSquare.
-
toStringSquare
toStringSquare.
- Parameters:
m- an array of objectsnf- aNumberFormatobjectvariables- aListobject- Returns:
- a
Stringobject
-
toString
toString.
- Parameters:
m- an array of objects- Returns:
- a
Stringobject
-
toString
toString.
-
toStringSquare
toStringSquare.
-
toString
toString.
- Parameters:
m- an array of objects- Returns:
- a
Stringobject
-
toString
toString.
- Parameters:
m- an array of objectsnf- aNumberFormatobject- Returns:
- a
Stringobject
-
toString
toString.
- Parameters:
m- an array of objects- Returns:
- a
Stringobject
-
toString
Copies the given array, starting each line with a tab character..- Parameters:
m- an array of objects- Returns:
- a
Stringobject
-
copyOf
public static int[] copyOf(int[] arr, int length) copyOf.
- Parameters:
arr- an array of objectslength- a int- Returns:
- an array of objects
-
copyOf
public static double[][] copyOf(double[][] arr) copyOf.
- Parameters:
arr- an array of objects- Returns:
- an array of objects
-
symmetrize
Computes the symmetrized version of the given square matrix. The symmetrized matrix is calculated as (A + A^T) / 2, where A is the input matrix and A^T is its transpose. The method does not modify the original matrix.- Parameters:
sigma- the input square matrix to be symmetrized; must not be null. The matrix must have the same number of rows and columns.- Returns:
- the symmetrized matrix computed as (A + A^T) / 2.
- Throws:
NullPointerException- if the input matrix is null.IllegalArgumentException- if the input matrix is not square.
-