集册 Java实例教程 计算两个数组的标量积,就好像它们是向量一样。

计算两个数组的标量积,就好像它们是向量一样。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

508
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
计算两个数组的标量积,就好像它们是矢量一样。


//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.

     */

    public static double scalarProduct(int[][] w0, 
展开阅读全文