Java Array Normalize normalizeVector(Double[] vector)

Here you can find the source of normalizeVector(Double[] vector)

Description

Takes a vector and returns it normal

License

Open Source License

Parameter

Parameter Description
a arbitrary vector in \mathbb{R}^n

Return

The normal of the given vector

Declaration

public static Double[] normalizeVector(Double[] vector) 

Method Source Code

//package com.java2s;

public class Main {
    /**/* w ww.  j a  v  a 2s  .co m*/
     * Takes a vector and returns it normal
     * 
     * @param a arbitrary vector in \mathbb{R}^n
     * @return The normal of the given vector
     */
    public static Double[] normalizeVector(Double[] vector) {
        Double norm = vectorLength(vector);
        for (int i = 0; i < vector.length; i++) {
            vector[i] = vector[i] / norm;
        }

        return vector;
    }

    /**
     * Computes the 2-Norm of a given vector
     * 
     * @param vector
     * @return The 2-Norm of the given vector
     */
    public static Double vectorLength(Double[] vector) {

        Double sum = 0.0;
        for (Double d : vector) {
            sum += Math.pow(d, 2);
        }
        sum = Math.sqrt(sum);

        return sum;

    }
}

Related

  1. normalizeToOne(double[] doubles)
  2. normalizeToSumUpTo(double[] x, double sumUp)
  3. normalizeTrimmedText(char[] ch)
  4. NormalizeVec2D(double[] vec)
  5. normalizeVector(double[] input)
  6. normalizeVector(final double[] v)
  7. normalizeVector(float[] samples)
  8. normalizeVectorMax(double[] input)
  9. normalizeVectorMaxMin(float[] samples)