List of utility methods to do Matrix Multiply
int[] | multiplyMatrix(int[][] a, int[] b, int mod) Multiplies the input matrix a with the input vector b % mod int m = b.length; int[] resultMatrix = new int[m]; for (int i = 0; i < m; i++) { int total = 0; for (int j = 0; j < m; j++) { int aValue = a[i][j]; int bValue = b[j]; total += aValue * bValue; ... |
double[][] | multiplyMatrixByMatrix(double[][] a, double[][] b) multiply Matrix By Matrix if (a[0].length != b.length) { throw new IllegalArgumentException("Cannot multiply a with b, columns of a != rows of b. "); double[][] c = new double[a.length][b[0].length]; for (int i = 0; i < a.length; i++) { for (int x = 0; x < b.length; x++) { for (int y = 0; y < a[i].length; y++) { c[i][x] += (a[i][y] * b[y][x]); ... |
double[][] | multiplyMatrixes(double[][] m1, double[][] m2) multiplies matrixes result = m1*m2 double[][] result = new double[3][3]; multiplyMatrixes(m1, m2, result); return result; |
void | multiplyQuad(final double[][] A, final double[][] B, final double[][] dest, int n) Multiplies the quadratic matrix A (n-by-n) with the quadratic matrix B (n-by-n). assert hasShape(A, n, n); assert hasShape(B, n, n); assert hasShape(dest, n, n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { dest[i][j] = A[i][0] * B[0][j]; for (int k = 1; k < n; k++) { dest[i][j] += A[i][k] * B[k][j]; ... |
float[][] | multiplyScalars(float[][] x, float[][] y) 1d arrays can be different sizes within same parent array, but x and y must be same sizes in all ways. if (x.length != y.length) throw new Error("diff sizes"); float[][] ret = new float[x.length][]; for (int i = 0; i < x.length; i++) { int innerSize = x[i].length; ret[i] = new float[innerSize]; if (innerSize != y[i].length) throw new Error("diff sizes"); ... |
float[][] | multiplyScalars_optimizedByKnowingBothAreRectangle(float[][] x, float[][] y) Not matrixMultiply. int b = x.length; int c = x[0].length; if (y.length != b || y[0].length != c) throw new Error("Diff sizes"); float[][] ret = new float[b][c]; for (int j = 0; j < b; j++) { for (int k = 0; k < c; k++) { ret[j][k] = x[j][k] * y[j][k]; ... |
void | multiplySparse2dense(int[][][] as, int A, int[][][] bs, int B, int[][] c) dense version of function above int[][] ax = as[0], aw = as[1], bx = bs[0], bw = bs[1]; int M = ax.length; for (int m = 0; m < M; m++) { for (int i = 0; i < ax[m].length; i++) { int ii = ax[m][i]; for (int j = 0; j < bx[m].length; j++) { int a = aw != null ? aw[m][i] : 1; int b = bw != null ? bw[m][j] : 1; ... |
double[] | multiplyVectorAndMatrix(double[] vector, double[][] matrix) Multiplies a vector and a matrix. double[] res = new double[matrix.length]; for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < vector.length; j++) { res[i] += matrix[i][j] * vector[j]; return res; |
double[] | multiplyVectorByMatrix(double[] v, double[][] m) multiply vector by matrix double[] result = new double[3]; multiplyVectorByMatrix(v, m, result); return result; |