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.
- Author:
- Tianjiao Chu, Joseph Ramsey, Kevin V. Bui
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic double[][]asRow(double[] v) static Matrixstatic 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 double[][]convertLowerTriangleToSymmetric(double[][] arr) Converts a matrix in lower triangular form to a symmetric matrix in square form.static double[][]copyOf(double[][] arr) static int[]copyOf(int[] arr, int length) static doubledeterminant(double[][] m) 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) static double[][]identity(int size) static MatriximpliedCovar(Matrix edgeCoef, Matrix errCovar) static MatriximpliedCovar2(Matrix edgeCoef, Matrix errCovar) static doubleinnerProduct(double[] ma, double[] mb) static double[][]inverse(double[][] m) static double[][]invVech(double[] vech) 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) static booleanisSymmetric(double[][] m, double tolerance) static double[][]outerProduct(double[] ma, double[] mb) static double[]product(double[][] ma, double[] mb) static double[][]product(double[][] ma, double[][] mb) static double[]product(double[] ma, double[][] mb) static Vectorstatic double[][]pseudoInverse(double[][] x) 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) 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) static double[]subtract(double[] ma, double[] mb) static double[][]sum(double[][] ma, double[][] mb) static double[]sum(double[] ma, double[] mb) static intsum0ToN(int n) static StringtoString(boolean[][] m) Copies the given array, starting each line with a tab character..static StringtoString(double[] m) static StringtoString(double[][] m) Copies the given array, using a standard scientific notation number formatter and beginning each line with a tab character.static Stringstatic StringtoString(double[] m, NumberFormat nf) static StringtoString(int[] m) static StringtoString(int[][] m) static Stringstatic StringtoStringSquare(double[][] m, NumberFormat nf, List<String> variables) static StringtoStringSquare(double[][] m, List<String> variables) static StringtoStringSquare(int[][] m, List<String> variables) static doubletrace(double[][] m) static double[][]transpose(double[][] m) static org.apache.commons.math3.linear.RealMatrixtransposeWithoutCopy(org.apache.commons.math3.linear.RealMatrix apacheData) static double[][]vec(double[][] m) static double[][]vech(double[][] m) static double[][]vechToVecLeft(int n) The matrix which, when postmultiplied by vech, return vec.static double[][]zeros(int rows, int cols) static doublezSum(double[][] m)
-
Constructor Details
-
MatrixUtils
public MatrixUtils()
-
-
Method Details
-
repmat
public static double[][] repmat(double[][] mat, int nRow, int mColumn) Make a repeat copy of matrix mat.- Parameters:
mat- matrix to copynRow- number of repeat copy of rowmColumn- number of repeat copy of column
-
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
-
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) - Parameters:
m- A 2D double matrix.- Returns:
- Ibid.
-
isSymmetric
public static boolean isSymmetric(double[][] m, double tolerance) - Parameters:
m- The matrix to check.tolerance- A double >= 0.- Returns:
- Ibid.
-
determinant
public static double determinant(double[][] m) - 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 -
inverse
public static double[][] inverse(double[][] m) - Returns:
- the inverse of the given square matrix if it is nonsingular, otherwise the pseudoinverse.
-
pseudoInverse
public static double[][] pseudoInverse(double[][] x) -
product
public static double[][] product(double[][] ma, double[][] mb) - 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
-
product
public static double[] product(double[][] ma, double[] mb) -
outerProduct
public static double[][] outerProduct(double[] ma, double[] mb) -
innerProduct
public static double innerProduct(double[] ma, double[] mb) -
transpose
public static double[][] transpose(double[][] m) - Returns:
- the transpose of the given matrix.
-
trace
public static double trace(double[][] m) - Returns:
- the trace of the given (square) m.
-
zSum
public static double zSum(double[][] m) -
identity
public static double[][] identity(int size) - Returns:
- the identity matrix of the given order.
-
sum
public static double[][] sum(double[][] ma, double[][] mb) - Returns:
- the sum of ma and mb.
-
sum
public static double[] sum(double[] ma, double[] mb) -
subtract
public static double[][] subtract(double[][] ma, double[][] mb) -
subtract
public static double[] subtract(double[] ma, double[] mb) -
directProduct
public static double[][] directProduct(double[][] ma, double[][] mb) Computes the direct (Kronecker) outerProduct. -
scalarProduct
public static double[][] scalarProduct(double scalar, double[][] m) Multiplies the given matrix through by the given scalar. -
scalarProduct
public static double[] scalarProduct(double scalar, double[] m) -
concatenate
public static double[] concatenate(double[][] vectors) Concatenates the vectors rows[i], i = 0...rows.length, into a single vector. -
asRow
public static double[][] asRow(double[] v) - Returns:
- the vector as a 1 x n row matrix.
-
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
-
vech
public static double[][] vech(double[][] m) - 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) - Returns:
- the symmetric matrix for which the given array is the vech.
-
vec
public static double[][] vec(double[][] m) - 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) - 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.
-
hasDimensions
public static boolean hasDimensions(double[][] m, int i, int j) - 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) -
isPositiveDefinite
Return true if the given matrix is symmetric positive definite--that is, if it would make a valid covariance matrix. -
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. -
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. -
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"). -
toString
-
toStringSquare
-
toStringSquare
-
toString
-
toString
-
toStringSquare
-
toString
-
toString
-
toString
-
toString
Copies the given array, starting each line with a tab character.. -
copyOf
public static int[] copyOf(int[] arr, int length) -
copyOf
public static double[][] copyOf(double[][] arr) -
transposeWithoutCopy
public static org.apache.commons.math3.linear.RealMatrix transposeWithoutCopy(org.apache.commons.math3.linear.RealMatrix apacheData)
-