Java Matrix Multiply multiplyMatrices(int[][] mat1, int[][] mat2)

Here you can find the source of multiplyMatrices(int[][] mat1, int[][] mat2)

Description

multiply Matrices

License

Open Source License

Parameter

Parameter Description
mat1 a parameter
mat2 a parameter

Declaration

public static int[][] multiplyMatrices(int[][] mat1, int[][] mat2) 

Method Source Code

//package com.java2s;
/*// w  w  w .  j  a v a  2  s . co m
 * The MIT License
 *
 * Copyright 2015 Ned Hyett.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

public class Main {
    /**
     * @param mat1
     * @param mat2
     * @return
     */
    public static int[][] multiplyMatrices(int[][] mat1, int[][] mat2) {
        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;
    }

    /**
     * @param mat1
     * @param mat2
     * @return
     */
    public static double[][] multiplyMatrices(double[][] mat1, double[][] mat2) {
        double[][] ret = new double[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;
    }

    /**
     * @param mat1
     * @param mat2
     * @return
     */
    public static float[][] multiplyMatrices(float[][] mat1, float[][] mat2) {
        float[][] ret;
        int x = mat1.length;
        int y = mat2.length;
        ret = new float[x][x];
        for (int i = 0; i < x; i++) {
            for (int j = 0; j < y - 1; j++) {
                for (int k = 0; k < y; k++) {
                    ret[i][j] += mat1[i][k] * mat2[k][j];
                }
            }
        }
        return ret;
    }
}

Related

  1. multiply(int[][] mat1, int[][] mat2)
  2. multiply(String[] tempResult, int nextIndex, String[][] pys)
  3. multiplyAffine(final double[][] A, final double[][] B, final double[][] dest, int n)
  4. multiplyMatrices(double[][] a, double[][] b)
  5. multiplyMatrices(float[][] left, float[][] right)
  6. multiplyMatrix(int[][] a, int[] b, int mod)
  7. multiplyMatrixByMatrix(double[][] a, double[][] b)
  8. multiplyMatrixes(double[][] m1, double[][] m2)
  9. multiplyQuad(final double[][] A, final double[][] B, final double[][] dest, int n)