Here you can find the source of dotProduct(double[] a, double[] b)
Parameter | Description |
---|---|
a | Vector of doubles. |
b | Vector of doubles. |
Parameter | Description |
---|---|
ArithmeticException | Throws an exception when the vectors are notof the same length. |
public static double dotProduct(double[] a, double[] b) throws ArithmeticException
//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; } } }