Multiply two matrices by each other and store the result. - Android java.lang

Android examples for java.lang:Math

Description

Multiply two matrices by each other and store the result.

Demo Code


//package com.java2s;

public class Main {
    /**/*from ww w.  j  av a 2 s . c  om*/
     * Multiply two matrices by each other and store the result. 
     * result = m1 x m2
     * @param m1 The first matrix
     * @param m2 The second matrix
     * @param reuslt Where to store the product of m1 x m2
     **/
    public static void multiply(float[][] m1, float[][] m2, float[][] result) {
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                result[i][j] = m1[i][0] * m2[0][j] + m1[i][1] * m2[1][j]
                        + m1[i][2] * m2[2][j] + m1[i][3] * m2[3][j];
            }
        }
    }

    /**
     * Multiply a vector and a matrix.  result = matrix x vector
     * @param matrix The matrix.
     * @param vector The vector
     * @param result The result of the multiplication
     **/
    public static void multiply(float[][] matrix, float[] vector,
            float[] res) {
        for (int i = 0; i < 4; i++) {
            res[i] = matrix[i][0] * vector[0] + matrix[i][1] * vector[1]
                    + matrix[i][2] * vector[2] + matrix[i][3] * vector[3];
        }
    }
}

Related Tutorials