Calculate the inner product of 2 vectors, where the first vector is a row in the first matrix, and the second vector is a column in the second matrix. - Java java.lang

Java examples for java.lang:Math Matrix

Description

Calculate the inner product of 2 vectors, where the first vector is a row in the first matrix, and the second vector is a column in the second matrix.

Demo Code


//package com.java2s;

public class Main {
    /**// w ww. j a va 2s .  c o m
     * Calculate the inner product of 2 vectors, where the first vector is a row in the first 
     * matrix, and the second vector is a column in the second matrix.
     * 
     * @param m1 left matrix
     * @param row index of m1
     * @param m2 right matrix
     * @param column index of m2
     * @return the sum of the element by element multiplication (inner product).
     */
    static Double innerProductOfRowAndColumn(Double[][] m1, int row,
            Double[][] m2, int column) {

        double sum = 0;
        for (int i = 0; i < m1[row].length; i++) {
            sum += m1[row][i] * m2[i][column];
        }

        return sum;
    }
}

Related Tutorials