Java Array Normalize normalizeToLogProbs(double[] x)

Here you can find the source of normalizeToLogProbs(double[] x)

Description

normalize To Log Probs

License

BSD License

Declaration

public static void normalizeToLogProbs(double[] x) 

Method Source Code

//package com.java2s;
//License from project: BSD License 

public class Main {
    public static void normalizeToLogProbs(double[] x) {
        double max = max(x, x.length);
        double sumExp = 0;
        for (int i = 0; i < x.length; i++) {
            if (!Double.isNaN(x[i]))
                sumExp += Math.exp(x[i] - max);
        }/*from   w ww  . j  ava 2  s . c  om*/
        double logSumExp = max + Math.log(sumExp);
        for (int i = 0; i < x.length; i++)
            if (!Double.isNaN(x[i]))
                x[i] -= logSumExp;
    }

    public static int max(int[] x, int length) {
        int m = Integer.MIN_VALUE;
        for (int i = 0; i < length; i++)
            if (x[i] > m)
                m = x[i];
        return m;
    }

    public static int max(int[] x) {
        return max(x, x.length);
    }

    public static double max(double[] x, int length) {
        double m = Double.NEGATIVE_INFINITY;
        for (int i = 0; i < length; i++)
            if (x[i] > m)
                m = x[i];
        return m;
    }
}

Related

  1. normalizeRecorderColumn(int column, int tabSize, int[] tabIndices)
  2. normalizeRows(float[][] input)
  3. normalizeSpaces(char[] buf, int origStart, int origEnd)
  4. normalizeTo(float[] in, float normsum)
  5. normalizeToInPlace(double[] in, double normsum)
  6. normalizeToOne(double[] doubles)
  7. normalizeToSumUpTo(double[] x, double sumUp)
  8. normalizeTrimmedText(char[] ch)
  9. NormalizeVec2D(double[] vec)