Java Array Dot Product dotProduct(double[] a, double[] b)

Here you can find the source of dotProduct(double[] a, double[] b)

Description

Computes the dot product of two vectors.

License

Open Source License

Parameter

Parameter Description
a Vector of doubles.
b Vector of doubles.

Exception

Parameter Description
ArithmeticException Throws an exception when the vectors are notof the same length.

Return

Dot product of the two vectors.

Declaration

public static double dotProduct(double[] a, double[] b) throws ArithmeticException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//w  ww  .  j a  va 2 s  . c om
     * Computes the dot product of two vectors.
     *
     * @param a   Vector of doubles.
     * @param b   Vector of doubles.
     * @return   Dot product of the two vectors.
     * @throws ArithmeticException   Throws an exception when the vectors are not
     * of the same length.
     */
    public static double dotProduct(double[] a, double[] b) throws ArithmeticException {

        if (a.length != b.length) {
            throw new ArithmeticException("The length of the vectors does not match!");
        } else {
            double sum = 0;
            for (int i = 0; i < a.length; i++) {
                sum += a[i] * b[i];
            }
            return sum;
        }

    }

    /**
     * Computes the dot product of two vectors.
     *
     * @param a   Vector of integers.
     * @param b   Vector of integers.
     * @return   Dot product of the two vectors.
     * @throws ArithmeticException   Throws an exception when the two vectors are
     * not of the same length.
     */
    public static int dotProduct(int[] a, int[] b) throws ArithmeticException {

        if (a.length != b.length) {
            throw new ArithmeticException();
        } else {
            int sum = 0;
            for (int i = 0; i < a.length; i++) {
                sum += (double) a[i] * (double) b[i];
            }
            return sum;
        }

    }

    /**
     * Computes the dot product of two vectors.
     *
     * @param a   Vector of igs.
     * @param b   Vector of floats.
     * @return   Dot product of the two vectors.
     * @throws ArithmeticException   Throws an exception when the two vectors are
     * not of the same length.
     */
    public static float dotProduct(float[] a, float[] b) throws ArithmeticException {

        if (a.length != b.length) {
            throw new ArithmeticException();
        } else {
            float sum = 0;
            for (int i = 0; i < a.length; i++) {
                sum += (double) a[i] * (double) b[i];
            }
            return sum;
        }

    }

    /**
     * Computes the dot product of two vectors.
     *
     * @param a   Vector of integers.
     * @param b   Vector of integers.
     * @return   Dot product of the two vectors.
     * @throws ArithmeticException   Throws an exception when the two vectors are
     * not of the same length.
     */
    public static long dotProduct(long[] a, long[] b) throws ArithmeticException {

        if (a.length != b.length) {
            throw new ArithmeticException();
        } else {
            long sum = 0;
            for (int i = 0; i < a.length; i++) {
                sum += (double) a[i] * (double) b[i];
            }
            return sum;
        }

    }

    /**
     * Computes the dot product of two vectors.
     *
     * @param a   Vector of longs.
     * @param b   Vector of longs.
     * @return   Dot product of the two vectors.
     * @throws ArithmeticException   Throws an exception when the two vectors are
     * not of the same length.
     */
    public static byte dotProduct(byte[] a, byte[] b) throws ArithmeticException {

        if (a.length != b.length) {
            throw new ArithmeticException();
        } else {
            byte sum = 0;
            for (int i = 0; i < a.length; i++) {
                sum += (double) a[i] * (double) b[i];
            }
            return sum;
        }

    }

    /**
     * Computes the dot product of two vectors.
     *
     * @param a   Vector of integers.
     * @param b   Vector of doubles.
     * @return   Dot product of the two vectors.
     * @throws ArithmeticException   Throws an exception when the two vectors are
     * not of the same length.
     */
    public static double dotProduct(double[] a, int[] b) throws ArithmeticException {

        if (a.length != b.length) {
            throw new ArithmeticException();
        } else {
            double sum = 0;
            for (int i = 0; i < a.length; i++) {
                sum += a[i] * b[i];
            }
            return sum;
        }

    }
}

Related

  1. dotprod(double[] xlist, double[] ylist)
  2. dotProd(float[] a, float[] b)
  3. dotProduct(double x1, double y1, double x2, double y2)
  4. dotProduct(double x1, double y1, double x2, double y2, double x3, double y3)
  5. dotProduct(double[] a, double[] b)
  6. dotProduct(double[] a, double[] b)
  7. dotProduct(Double[] a, Double[] b)
  8. dotProduct(double[] array, int[] indices, double[] values)
  9. dotProduct(double[] fv1, double[] fv2)