//package com.nowjava;
public class Main {
/**
时代Java - N o w J a v a . c o m 提供
**/
public static void main(String[] argv) throws Exception {
double[] w0 = new double[] { 34.45, 35.45, 36.67, 37.78, 37.0000,
37.1234, 67.2344, 68.34534, 69.87700 };
double[] w1 = new double[] { 34.45, 35.45, 36.67, 37.78, 37.0000,
37.1234, 67.2344, 68.34534, 69.87700 };
System.out.println(scalarProduct(w0, w1));
}
/**
* Computes the scalar product of two array as if they were vectors.
*
* @throws IllegalArgumentException if the don't have the same length
*/
public static double scalarProduct(double[] w0, double[] w1) {
if (w0.length != w1.length) {
throw new IllegalArgumentException(
"Arrays must have the same length : " + w0.length
+ ", " + w1.length);
}
if (w0.length == 0)
throw new IllegalArgumentException(//时 代 J a v a - nowjava.com 提供
"Nothing to compute! Arrays must have at least one element.");
double sortie = 0.0;
for (int k = 0; k < w0.length; k++) {
sortie += w0[k] * w1[k];
}
return (sortie);
}
/**
* Computes the scalar product of two array as if they were matrices.
*
* @throws IllegalArgumentException if the don't have the same length
*/
public static double scalarProduct(double[][] w0, double[][] w1) {
if (w0.length != w1.length) {
throw new IllegalArgumentException(
"Arrays must have the same length : " + w0.length
+ ", " + w1.length);
}
if (w0.length == 0)
throw new IllegalArgumentException(
"Nothing to compute! Arrays must have at least one element.");
double sortie = 0.0;
for (int k = 0; k < w0.length; k++) {
sortie = sortie + scalarProduct(w0[k], w1[k]);
}
return (sortie);
}
/**
* Computes the scalar product of two array
* as if they were vectors.
*
* @throws IllegalArgumentException if the
* don't have the same length.
*/
public static int scalarProduct(int[] w0, int[] w1) {
if (w0.length != w1.length) {
throw new IllegalArgumentException(
"Arrays must have the same length : " + w0.length
+ ", " + w1.length);
}
if (w0.length == 0)
throw new IllegalArgumentException(
"Nothing to compute! Arrays must have at least one element.");
int sortie = 0;
for (int k = 0; k < w0.length; k++) {
sortie = sortie + w0[k] * w1[k];
}
return (sortie);
}
/**
* Computes the scalar product of two array
* as if they were matrices.
*
* @throws IllegalArgumentException if the
* don't have the same length.
*/
/**代码未完, 请加载全部代码(NowJava.com).**/
本文系作者在时代Java发表,未经许可,不得转载。如有侵权,请联系nowjava@qq.com删除。