Java Matrix matrixTimesVector(double[][] matrix, double[] vector)

Here you can find the source of matrixTimesVector(double[][] matrix, double[] vector)

Description

matrix Times Vector

License

Open Source License

Declaration

public static double[] matrixTimesVector(double[][] matrix, double[] vector) 

Method Source Code

//package com.java2s;
/*//from ww  w  .  ja  v  a2  s.co m
 * Copyright (C) 2010 Brockmann Consult GmbH (info@brockmann-consult.de)
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 3 of the License, or (at your option)
 * any later version.
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, see http://www.gnu.org/licenses/
 */

public class Main {
    public static double[] matrixTimesVector(double[][] matrix, double[] vector) {
        if (matrix[0].length != vector.length) {
            throw new IllegalArgumentException("matrix[0].length != vector.length");
        }
        double[] res = new double[matrix.length];
        for (int i = 0; i < matrix.length; i++) {
            res[i] = 0;
            for (int j = 0; j < matrix[0].length; j++) {
                res[i] = res[i] + matrix[j][i] * vector[j];
            }
        }
        return res;
    }
}

Related

  1. matrixDeterminant(final float[] m, final int m_offset)
  2. matrixEquals(int[][] firstMatrix, int[][] secondMatrix)
  3. matrixIsWellformed(int numLabels, int[][] matrix)
  4. matrixKLDivergence(double m1[][], double m2[][])
  5. matrixModule(double[][] matrix, int module)