Java Array Normalize normalize(final double[] a)

Here you can find the source of normalize(final double[] a)

Description

normalize a, i.e., scale to unit length.

License

Open Source License

Parameter

Parameter Description
a a parameter

Declaration

public static void normalize(final double[] a) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from w  w  w.j  a v a  2  s . c  o  m
     * normalize a, i.e., scale to unit length.
     * 
     * @param a
     */
    public static void normalize(final double[] a) {
        final int rows = rows(a);
        final double len = length(a);
        for (int i = 0; i < rows; ++i)
            a[i] /= len;
    }

    public static int rows(final double[] a) {
        return a.length;
    }

    public static int rows(final double[][] A) {
        return A.length;
    }

    /**
     * get the length of a.
     * 
     * @param a
     */
    public static double length(final double[] a) {
        return Math.sqrt(squareLength(a));
    }

    /**
     * get the squared length of a.
     * 
     * @param a
     */
    public static double squareLength(final double[] a) {
        final int rows = rows(a);
        double squ_len = 0.0;
        for (int i = 0; i < rows; ++i)
            squ_len += a[i] * a[i];
        return squ_len;
    }
}

Related

  1. normalize(double[] xs)
  2. normalize(double[][] matrix, double lower, double upper)
  3. normalize(double[][] result)
  4. normalize(double[][] X)
  5. normalize(final byte[] input, final int bit)
  6. normalize(final double[] doubles)
  7. normalize(final double[] fir)
  8. normalize(final double[] tab)
  9. normalize(final double[] vec)