Java Array Normalize norm(double[] vector, int n)

Here you can find the source of norm(double[] vector, int n)

Description

This method takes the Ln vector norm of the vector according to the formula: Norm = [E(i=0 to vector.length) |vector[i]|^n]^(1.0/n)

License

Open Source License

Parameter

Parameter Description
vector a parameter
n a parameter

Return

the vector norm of the given vector to the nth degree

Declaration

public static double norm(double[] vector, int n) 

Method Source Code

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

public class Main {
    /**/*from  ww w  .ja v  a  2  s  .  c  o  m*/
     * This method takes the Ln vector norm of the vector according to the formula:
     * Norm = [E(i=0 to vector.length) |vector[i]|^n]^(1.0/n)
     * @param vector
     * @param n
     * @return the vector norm of the given vector to the nth degree
     */
    public static double norm(double[] vector, int n) {
        double norm = 0.0;
        //Go through each component
        for (int i = 0; i < vector.length; ++i) {
            double inner = 1.0;
            double comp = Math.abs(vector[i]);
            //Get the appropriate power of the component
            for (int dim = 0; dim < n; ++dim)
                inner *= comp;
            //Add the |comp|^n to our sum
            norm += inner;
        }
        //Take the appropriate root, don't waste the method call if
        //n is 1.
        if (n != 1)
            norm = Math.pow(norm, 1.0 / n);
        return norm;
    }
}

Related

  1. norm(double[] a)
  2. norm(double[] array)
  3. norm(double[] data)
  4. norm(double[] v)
  5. norm(double[] vector)
  6. norm(final double[] vec)
  7. norm(final double[] x)
  8. norm(float[] data)
  9. norm(int D, double vec[])