Java Utililty Methods Matrix

List of utility methods to do Matrix

Description

The list of methods to do Matrix are organized into topic(s).

Method

double[][]matrix_x_matrix(double[][] A, double[][] B)
Multiplies two matrices.
final int nA = A[0].length;
final int mC = A.length;
final int nC = B[0].length;
final double[][] C = new double[mC][nC];
for (int i = 0; i < mC; i++) {
    final double[] C_row = C[i];
    final double[] A_row = A[i];
    for (int j = 0; j < nC; j++) {
...
double[]matrix_x_vectorMatrix(double[][] mat, double[][] vector, int vectorCol)
Multiplies a matrix with another matrix that only has one column.
final int m = mat.length;
final int n = mat[0].length;
double[] res = new double[m];
for (int row = 0; row < m; row++) {
    for (int col = 0; col < n; col++) {
        res[col] += mat[row][col] * vector[col][vectorCol];
return res;
doublematrixAbsDiff(double m1[][], double m2[][])
matrix Abs Diff
if (m1.length != m2.length)
    return Double.MAX_VALUE;
int numElements = 0;
double diff = 0.0;
for (int i = 0; i < m1.length; i++) {
    if (m1[i].length != m2[i].length)
        return Double.MAX_VALUE;
    numElements += m1[i].length;
...
double[][]matrixATrans_x_matrixB(double[][] matA, double[][] matB)
First transposes matrix matA, then multiplies it with matrix matB.
final int m = matA[0].length;
final int n = matB[0].length;
double[][] res = new double[m][n];
for (int r = 0; r < m; r++) {
    for (int c = 0; c < n; c++) {
        for (int i = 0; i < matA.length; i++) {
            res[r][c] += matA[i][r] * matB[i][c];
return res;
double[][]matrixConcatLR(double[][] LMatrix, double[][] RMatrix)
matrix Concat LR
if (LMatrix.length != RMatrix.length) {
    System.out.println("matrixConcatLR error: the number of rows in two matrix should be same");
    System.exit(0);
int numRows = LMatrix.length;
int numColsForLMatrix = LMatrix[0].length;
int numColsForRMatrix = RMatrix[0].length;
double[][] newMatrix = new double[numRows][numColsForLMatrix + numColsForRMatrix];
...
double[][]matrixConcatUL(double[][] UMatrix, double[][] LMatrix)
matrix Concat UL
if (LMatrix[0].length != UMatrix[0].length) {
    System.out.println("matrixConcatUL error: the number of columns in two matrix should be same");
    System.exit(0);
int numURows = UMatrix.length;
int numLRows = LMatrix.length;
int numCols = UMatrix[0].length;
double[][] newMatrix = new double[numURows + numLRows][numCols];
...
voidmatrixDestructAdd(double[][] m1, double[][] m2)
matrix Destruct Add
assert (m1.length == m2.length);
assert (m1[0].length == m2[0].length);
for (int i = 0; i < m1.length; i++)
    for (int j = 0; j < m1[0].length; j++)
        m1[i][j] = m1[i][j] + m2[i][j];
floatmatrixDeterminant(final float[] m, final int m_offset)
Returns the determinant of the given matrix
float a11 = m[1 + 4 * 1 + m_offset];
float a21 = m[2 + 4 * 1 + m_offset];
float a31 = m[3 + 4 * 1 + m_offset];
float a12 = m[1 + 4 * 2 + m_offset];
float a22 = m[2 + 4 * 2 + m_offset];
float a32 = m[3 + 4 * 2 + m_offset];
float a13 = m[1 + 4 * 3 + m_offset];
float a23 = m[2 + 4 * 3 + m_offset];
...
booleanmatrixEquals(int[][] firstMatrix, int[][] secondMatrix)
matrix Equals
for (int i = 0; i < firstMatrix.length; i++)
    for (int j = 0; j < firstMatrix[0].length; j++)
        if (firstMatrix[i][j] != secondMatrix[i][j])
            return false;
return true;
booleanmatrixIsWellformed(int numLabels, int[][] matrix)
matrix Is Wellformed
boolean isWellformed = true;
if (matrix.length != numLabels) {
    isWellformed = false;
for (int[] gold : matrix) {
    if (gold.length != numLabels) {
        isWellformed = false;
    for (int pred : gold) {
        if (pred < 0) {
            isWellformed = false;
return isWellformed;