Java Vector Product vectorProduct(double[] x, boolean isColumnVectorX, double[] y, boolean isColumnVectorY)

Here you can find the source of vectorProduct(double[] x, boolean isColumnVectorX, double[] y, boolean isColumnVectorY)

Description

vector Product

License

Open Source License

Declaration

public static double[][] vectorProduct(double[] x, boolean isColumnVectorX, double[] y,
            boolean isColumnVectorY) 

Method Source Code

//package com.java2s;
/**/* w  w w .j ava2s  .  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[][] vectorProduct(double[] x, boolean isColumnVectorX, double[] y,
            boolean isColumnVectorY) {
        double[][] xx = null;
        double[][] yy = null;
        int i;
        if (isColumnVectorX) {
            xx = new double[x.length][1];
            for (i = 0; i < x.length; i++)
                xx[i][0] = x[i];
        } else {
            xx = new double[1][x.length];
            System.arraycopy(x, 0, xx[0], 0, x.length);
        }

        if (isColumnVectorY) {
            yy = new double[y.length][1];
            for (i = 0; i < y.length; i++)
                yy[i][0] = y[i];
        } else {
            yy = new double[1][y.length];
            System.arraycopy(y, 0, yy[0], 0, y.length);
        }

        return matrixProduct(xx, yy);
    }

    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. vector_product(double[] v, double[] w)
  2. vectorDotProduct(double X1, double Y1, double Z1, double X2, double Y2, double Z2)
  3. vectorDotProduct(double[] v1, double[] v2)
  4. vectorProduct(float[] x, float[] y)