Multiplies a vector and a matrix. - Java java.lang

Java examples for java.lang:Math Matrix

Description

Multiplies a vector and a matrix.

Demo Code


//package com.java2s;

public class Main {
    /**//from   w ww .j  a v  a2  s .  c o  m
     * Multiplies a vector and a matrix. The multiplication order is: vector *
     * matrix. The result of multiplication is a vector.
     *
     * @param vector   Vector of integers.
     * @param matrix   Matrix of integers.
     * @return   Vector result of multiplication of the vector and matrix.
     */
    public static double[] multiplyVectorAndMatrix(double[] vector,
            double[][] 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;
    }

    /**
     * Multiplies a vector and a matrix. The multiplication order is: vector *
     * matrix. The result of multiplication is a vector.
     *
     * @param vector   Vector of integers.
     * @param matrix   Matrix of integers.
     * @return   Vector result of multiplication of the vector and matrix.
     */
    public static double[] multiplyVectorAndMatrix(int[] vector,
            int[][] 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] += (double) matrix[i][j] * (double) vector[j];
            }
        }

        return res;
    }
}

Related Tutorials