Java Matrix Product matrixProduct(double[][] x, double[] y)

Here you can find the source of matrixProduct(double[][] x, double[] y)

Description

matrix Product

License

Open Source License

Declaration

public static double[] matrixProduct(double[][] x, double[] y) 

Method Source Code

//package com.java2s;
/**//from   w w  w  .jav  a 2 s  .  c om
 * Copyright 2004-2006 DFKI GmbH.
 * All Rights Reserved.  Use is subject to license terms.
 * 
 * Permission is hereby granted, free of charge, to use and distribute
 * this software and its documentation without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of this work, and to
 * permit persons to whom this work is furnished to do so, subject to
 * the following conditions:
 * 
 * 1. The code must retain the above copyright notice, this list of
 *    conditions and the following disclaimer.
 * 2. Any modifications must be clearly marked as such.
 * 3. Original authors' names are not deleted.
 * 4. The authors' names are not used to endorse or promote products
 *    derived from this software without specific prior written
 *    permission.
 *
 * DFKI GMBH AND THE CONTRIBUTORS TO THIS WORK DISCLAIM ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL DFKI GMBH NOR THE
 * CONTRIBUTORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
 * THIS SOFTWARE.
 */

public class Main {
    public static double[] matrixProduct(double[][] x, double[] y) {
        double[][] y2 = new double[y.length][1];
        int i;
        for (i = 0; i < y.length; i++)
            y2[i][0] = y[i];

        y2 = matrixProduct(x, y2);

        double[] y3 = new double[y2.length];
        for (i = 0; i < y2.length; i++)
            y3[i] = y2[i][0];

        return y3;
    }

    public static double[][] matrixProduct(double[] x, double[][] y) {
        double[][] x2 = new double[x.length][1];
        int i;
        for (i = 0; i < x.length; i++)
            x2[i][0] = x[i];

        return matrixProduct(x2, y);
    }

    public static double[][] matrixProduct(double[][] x, double[][] y) {
        double[][] z = null;

        if (x != null && y != null) {
            if (x.length == 1 && y.length == 1) //Special case -- diagonal matrix multiplication, returns a diagonal matrix
            {
                assert x[0].length == y[0].length;
                z = new double[1][x[0].length];
                for (int i = 0; i < x[0].length; i++)
                    z[0][i] = x[0][i] * y[0][i];
            } else {
                int i, j, m;
                int rowSizex = x.length;
                int colSizex = x[0].length;
                int rowSizey = y.length;
                int colSizey = y[0].length;
                for (i = 1; i < x.length; i++)
                    assert x[i].length == colSizex;
                for (i = 1; i < y.length; i++)
                    assert y[i].length == colSizey;
                assert colSizex == rowSizey;

                z = new double[rowSizex][colSizey];
                double tmpSum;
                for (i = 0; i < rowSizex; i++) {
                    for (j = 0; j < colSizey; j++) {
                        tmpSum = 0.0;
                        for (m = 0; m < x[i].length; m++)
                            tmpSum += x[i][m] * y[m][j];

                        z[i][j] = tmpSum;
                    }
                }
            }
        }

        return z;
    }
}

Related

  1. matrix_vector_product(double[][] a, double[] v)
  2. matrixInnerProduct(int M, int N, double m1[][], double m2[][])
  3. matrixProduct(double[][] A, double[][] B)
  4. matrixProduct(double[][] m, double[][] n)
  5. matrixProductVDVT(final double[][] V, final double[] d, final double[][] A, int n)
  6. product(final T[][] args)