Java Utililty Methods Matrix Multiply

List of utility methods to do Matrix Multiply

Description

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

Method

double[][]multiply(double[][] x, double[][] y)
matrix multiplication
final int rowsX = x.length;
final int colsX = x[0].length;
final int rowsY = y.length;
final int colsY = y[0].length;
if (colsX != rowsY)
    throw new RuntimeException("multiply(x,y): incompatible dimensions");
final double[][] z = new double[rowsX][colsY];
for (int a = 0; a < rowsX; a++) {
...
double[][]multiply(final double[][] A, final double[][] B)
multiply
final int n = A.length;
final int m = B.length;
if (m == 0 || n == 0) {
    return new double[0][0];
final int p = B[0].length;
if (m != A[0].length) {
    throw new IllegalArgumentException(
...
voidmultiply(float[][] a, float num)
Computes the pointwise multiplication of an array by num , leaving the result in a .
checkDimension(a);
int M = a.length;
int N = a[0].length;
for (int i = 0; i < M; i++) {
    for (int j = 0; j < N; j++) {
        a[i][j] = a[i][j] * num;
float[][]multiply(float[][] a, float[][] b)
Matrix multiplication of 4x4 float matrices
float[][] c = new float[4][4];
c[0][0] = a[0][0] * b[0][0] + a[0][1] * b[1][0] + a[0][2] * b[2][0] + a[0][3] * b[3][0];
c[0][1] = a[0][0] * b[0][1] + a[0][1] * b[1][1] + a[0][2] * b[2][1] + a[0][3] * b[3][1];
c[0][2] = a[0][0] * b[0][2] + a[0][1] * b[1][2] + a[0][2] * b[2][2] + a[0][3] * b[3][2];
c[0][3] = a[0][0] * b[0][3] + a[0][1] * b[1][3] + a[0][2] * b[2][3] + a[0][3] * b[3][3];
c[1][0] = a[1][0] * b[0][0] + a[1][1] * b[1][0] + a[1][2] * b[2][0] + a[1][3] * b[3][0];
c[1][1] = a[1][0] * b[0][1] + a[1][1] * b[1][1] + a[1][2] * b[2][1] + a[1][3] * b[3][1];
c[1][2] = a[1][0] * b[0][2] + a[1][1] * b[1][2] + a[1][2] * b[2][2] + a[1][3] * b[3][2];
...
int[][]multiply(int[][] mat1, int[][] mat2)
multiply
int sizeY = mat1.length;
int sizeX = mat1[0].length;
int tmp = 0;
int[][] result = new int[sizeX][sizeY];
for (int i = 0; i < sizeX; i++) {
    for (int j = 0; j < sizeX; j++) {
        tmp = 0;
        for (int k = 0; k < sizeY; k++) {
...
String[]multiply(String[] tempResult, int nextIndex, String[][] pys)
multiply
String[] next = pys[nextIndex];
int pl = tempResult.length;
int npl = next.length;
String[] result = new String[pl * npl];
int count = 0;
for (int i = 0; i < pl; i++) {
    for (int j = 0; j < npl; j++) {
        result[count] = tempResult[i] + next[j];
...
voidmultiplyAffine(final double[][] A, final double[][] B, final double[][] dest, int n)
Multiplies two affine matrices A and B (n by n+1, linear part plus translation).
assert A != null;
assert B != null;
assert dest != null;
assert hasShape(A, n, n + 1);
assert hasShape(B, n, n + 1);
assert hasShape(dest, n, n + 1);
for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
...
double[][]multiplyMatrices(double[][] a, double[][] b)
Multiplies two matrices using an implementation faster than the straightforward one.
double[][] c = new double[a.length][b[0].length];
for (int i = 0; i < a.length; i++) {
    for (int k = 0; k < b.length; k++) {
        for (int j = 0; j < b[0].length; j++) {
            c[i][j] += a[i][k] * b[k][j];
return c;
float[][]multiplyMatrices(float[][] left, float[][] right)
Returns the matrix product left * right.
if (left[0].length != right.length) {
    throw new IllegalArgumentException("Columns of left matric must match rows of right matrix.");
float[][] result = new float[left.length][right[0].length];
for (int i = 0; i < left.length; ++i) {
    for (int j = 0; j < right[0].length; ++j) {
        result[i][j] = 0;
        for (int k = 0; k < right.length; ++k) {
...
int[][]multiplyMatrices(int[][] mat1, int[][] mat2)
multiply Matrices
int[][] ret = new int[mat1.length][mat1[0].length];
for (int i = 0; i < mat1.length; i++) {
    for (int q = 0; q < mat1[i].length; q++) {
        ret[i][q] = mat1[i][q] * mat2[i][q];
return ret;