Java Array Normalize normaliseSum(double[] a)

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

Description

normalise Sum

License

Open Source License

Parameter

Parameter Description
a The array to normalise (the values in this array are altered).

Return

The given array with normalised values such that sum(a) will return 1.

Declaration

public static double[] normaliseSum(double[] a) 

Method Source Code

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

public class Main {
    /**/*from ww w.  j  a va2 s  .  c o  m*/
     * @param a The array to normalise (the values in this array are altered).
     * @return The given array with normalised values such that sum(a) will return 1.
     */
    public static double[] normaliseSum(double[] a) {
        double sumInv = 1.0 / sum(a);
        for (int i = 0; i < a.length; i++)
            a[i] *= sumInv;
        return a;
    }

    /**
     * @param a The array to sum.
     * @return The sum over all elements in the array.
     */
    public static double sum(double[] a) {
        double sum = 0;
        for (int i = 0; i < a.length; i++)
            sum += a[i];
        return sum;
    }

    /**
     * @param a The array to sum.
     * @return The sum over all elements in the array.
     */
    public static long sum(int[] a) {
        long sum = 0;
        for (int i = 0; i < a.length; i++)
            sum += a[i];
        return sum;
    }
}

Related

  1. normalise(double[] A)
  2. normalise(float[] array)
  3. normaliseActions(double[] actions)
  4. normaliseFloats(final float[] table, final int position, final int length)
  5. normaliseHeaders(String[] headers)
  6. normaliseUnicode(String unicodeText, char[] mappings)
  7. normalize(byte[] data, byte maximum, byte minimum)
  8. normalize(double arr[])
  9. normalize(double wheelSpeeds[])