Java Array Normalize norm(double[] data)

Here you can find the source of norm(double[] data)

Description

Computes the L2 norm of an array (Euclidean norm or "length").

License

Open Source License

Declaration

public static double norm(double[] data) 

Method Source Code

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

public class Main {
    /**/*from  w  w  w  .  j a  v a2s.c  om*/
     * Computes the L2 norm of an array (Euclidean norm or "length").
     */
    public static double norm(double[] data) {
        return (Math.sqrt(sumSquares(data)));
    }

    /**
     * Computes the L2 norm of an array (Euclidean norm or "length").
     */
    public static double norm(int[] data) {
        return (Math.sqrt(sumSquares(data)));
    }

    /**
     * Sums the squares of all components;
     * also called the energy of the array.
     */
    public static double sumSquares(double[] data) {
        double ans = 0.0;
        for (int k = 0; k < data.length; k++) {
            ans += data[k] * data[k];
        }
        return (ans);
    }

    /**
     * Sums the squares of all components;
     * also called the energy of the array.
     */
    public static double sumSquares(double[][] data) {
        double ans = 0.0;
        for (int k = 0; k < data.length; k++) {
            for (int l = 0; l < data[k].length; l++) {
                ans += data[k][l] * data[k][l];
            }
        }
        return (ans);
    }

    /**
     * Sums the squares of all components;
     * also called the energy of the array.
     */
    public static int sumSquares(int[] data) {
        int ans = 0;
        for (int k = 0; k < data.length; k++) {
            ans += data[k] * data[k];
        }
        return (ans);
    }

    /**
     * Sums the squares of all components;
     * also called the energy of the array.
     */
    public static int sumSquares(int[][] data) {
        int ans = 0;
        for (int k = 0; k < data.length; k++) {
            for (int l = 0; l < data[k].length; l++) {
                ans += data[k][l] * data[k][l];
            }
        }
        return (ans);
    }
}

Related

  1. norm(byte[] tab)
  2. norm(double[] a)
  3. norm(double[] a)
  4. norm(double[] a)
  5. norm(double[] array)
  6. norm(double[] v)
  7. norm(double[] vector)
  8. norm(double[] vector, int n)
  9. norm(final double[] vec)