@Test public void testLogAdd() { assertEquals(logAdd(Double.NEGATIVE_INFINITY, 1), 1.0, 0.0); assertEquals(logAdd(10, Double.NEGATIVE_INFINITY), 10.0, 0.0); double[] d = new double[] {-15, -1, 0, 1, 3}; for (double x : d) { for (double y : d) { double l1 = logAdd(x, y); double l2 = Math.log(Math.exp(x) + Math.exp(y)); assertEquals(true, epsilonEquals(l1, l2)); } } }
public static double logAdd(double x1, double x2) { if (x1 == Double.NEGATIVE_INFINITY) return x2; else if (x2 == Double.NEGATIVE_INFINITY) return x1; else if (x1 > x2) return x2 + log(1 + exp(x1 - x2)); else return x1 + log(1 + exp(x2 - x1)); }
MathUtilities { public static double logAdd(double x1, double x2) { if (x1 == Double.NEGATIVE_INFINITY) return x2; else if (x2 == Double.NEGATIVE_INFINITY) return x1; else if (x1 > x2) return x2 + log(1 + exp(x1 - x2)); else return x1 + log(1 + exp(x2 - x1)); } }
MathUtilities { public static double logAdd(double x1, double x2) { if (x1 == Double.NEGATIVE_INFINITY) return x2; else if (x2 == Double.NEGATIVE_INFINITY) return x1; else if (x1 > x2) return x2 + log(1 + exp(x1 - x2)); else return x1 + log(1 + exp(x2 - x1)); }  }
MathUtilities { public static double logAdd(double x1, double x2) { if (x1 == Double.NEGATIVE_INFINITY) return x2; else if (x2 == Double.NEGATIVE_INFINITY) return x1; else if (x1 > x2) return x2 + log(1 + exp(x1 - x2)); else return x1 + log(1 + exp(x2 - x1)); }  static Pair<Integer, Double> max(double[] array); static Pair<Integer, Integer> max(int[] array); static Pair<Integer, T> max(Collection<T> list); static Pair<Integer, T> max(T[] array); static Pair<T, S> max(Map<T, S> map); static Pair<Integer, Double> min(double[] array); static Pair<Integer, Integer> min(int[] array); static Pair<Integer, T> min(Collection<T> list); static Pair<T, S> min(Map<T, S> map); static Pair<Integer, T> min(T[] array); static List<Double> multiply(List<Double> l, double m); static List<Double> multiply(List<Double> d1, List<Double> d2); static double[] multiply(double[] d1, double[] d2); static double[] multiply(double[] d, double m); static double[] softmax(double[] input); static double[] softmax(double[] input, double[] multiplier); static List<Double> softmax(List<Double> input); static List<Double> softmax(List<Double> input, List<Double> multiplier); static boolean epsilonEquals(double d1, double d2); static double logAdd(double x1, double x2); static double logAdd(double[] doubles); static double logAdd(Collection<Double> doubles); static double lnGamma(double xx); static double lnFactorial(int x); static int binomialCoeffs(int n, int k); static double beta(double x, double w); static double incompleteGammaP(double a, double x); static double incompleteGammaQ(double a, double x); static double erf(double x); static double erfc(double x); }
MathUtilities { public static double logAdd(double x1, double x2) { if (x1 == Double.NEGATIVE_INFINITY) return x2; else if (x2 == Double.NEGATIVE_INFINITY) return x1; else if (x1 > x2) return x2 + log(1 + exp(x1 - x2)); else return x1 + log(1 + exp(x2 - x1)); }  static Pair<Integer, Double> max(double[] array); static Pair<Integer, Integer> max(int[] array); static Pair<Integer, T> max(Collection<T> list); static Pair<Integer, T> max(T[] array); static Pair<T, S> max(Map<T, S> map); static Pair<Integer, Double> min(double[] array); static Pair<Integer, Integer> min(int[] array); static Pair<Integer, T> min(Collection<T> list); static Pair<T, S> min(Map<T, S> map); static Pair<Integer, T> min(T[] array); static List<Double> multiply(List<Double> l, double m); static List<Double> multiply(List<Double> d1, List<Double> d2); static double[] multiply(double[] d1, double[] d2); static double[] multiply(double[] d, double m); static double[] softmax(double[] input); static double[] softmax(double[] input, double[] multiplier); static List<Double> softmax(List<Double> input); static List<Double> softmax(List<Double> input, List<Double> multiplier); static boolean epsilonEquals(double d1, double d2); static double logAdd(double x1, double x2); static double logAdd(double[] doubles); static double logAdd(Collection<Double> doubles); static double lnGamma(double xx); static double lnFactorial(int x); static int binomialCoeffs(int n, int k); static double beta(double x, double w); static double incompleteGammaP(double a, double x); static double incompleteGammaQ(double a, double x); static double erf(double x); static double erfc(double x); final static double EPSILON; }