Here you can find the source of average(double[] d)
public static double average(double[] d)
//package com.java2s; //License from project: Open Source License import java.util.ArrayList; public class Main { /**// w w w . ja v a 2s . com * Mittelwertberechnung. Versuchts erst schneller und nimmt sonst den * langsameren, aber sicheren Algorithmus. */ public static double average(double[] d) { double average = average1(d); if (Double.isNaN(average) || Double.isInfinite(average)) return average2(d); return average; } /** * Spaltenweise mittelwertberechnung. Versuchts erst schneller und nimmt * sonst den langsameren, aber sicheren Algorithmus. */ public static double[] average(double[][] d) { double[] average = average1(d); if (average == null) return null; // Koomt vor wenn er alle sequenzen nicht mappen kann for (int i = 0; i < average.length; i++) if (Double.isNaN(average[i]) || average[i] == Double.POSITIVE_INFINITY || average[i] == Double.NEGATIVE_INFINITY) return average2(d); return average; } /** * * @param d * @return */ public static double average1(double[] d) { // Schneller if (d == null || d.length < 1) return Double.NaN; double retVal = 0; int countNonNAN = 0; for (int i = 0; i < d.length; i++) { if (Double.isNaN(d[i]) || Double.isInfinite(d[i])) continue; countNonNAN++; retVal += d[i]; } if (countNonNAN <= 0) return Double.NaN; return (retVal / countNonNAN); } /** * * @param d * @return */ public static double[] average1(double[][] d) { // Schneller if (d.length < 1) return new double[0]; double[] retVal = null; int countNonNull = 0; for (int i = 0; i < d.length; i++) { if (d[i] == null) continue; // kommt vor wenn er sequenz i nicht mappen kann countNonNull++; if (retVal == null) retVal = new double[d[i].length]; for (int j = 0; j < d[i].length; j++) retVal[j] += d[i][j]; } if (retVal == null) return null; // Koomt vor wenn er alle sequenzen nicht mappen kann for (int i = 0; i < retVal.length; i++) retVal[i] /= countNonNull; return retVal; } /** * * @param d * @return */ public static double average2(double[] d) { // Keine to-large-numbers if (d.length < 1) return Double.NaN; double retVal = 0; int countNonNAN = 0; for (int i = 0; i < d.length; i++) { if (Double.isNaN(d[i]) || Double.isInfinite(d[i])) continue; countNonNAN++; // retVal[j]=retVal[j] * i/(i+1) + d[i][j] * 1/(i+1); retVal = retVal * (countNonNAN - 1) / (countNonNAN) + d[i] * 1 / (countNonNAN); } // Wenn irgendwo nur NaNs waren, das auch so wiedergeben if (countNonNAN <= 0) return Double.NaN; return retVal; } /** * * @param d * @return */ public static double[] average2(double[][] d) { // Keine to-large-numbers if (d.length < 1) return new double[0]; double[] retVal = null; ArrayList<Integer> spaltenCounter = new ArrayList<Integer>(); for (int i = 0; i < d.length; i++) { if (d[i] == null) continue; // kommt vor wenn er sequenz i nicht mappen kann if (retVal == null) retVal = new double[d[i].length]; for (int j = 0; j < d[i].length; j++) { if (spaltenCounter.size() <= j) spaltenCounter.add(0); if (Double.isNaN(d[i][j])) continue; // Deshalb auch der Spaltencounter: Skip NaN // eintr?ge. // retVal[j]=retVal[j] * i/(i+1) + d[i][j] * 1/(i+1); retVal[j] = retVal[j] * spaltenCounter.get(j) / (spaltenCounter.get(j) + 1) + d[i][j] * 1 / (spaltenCounter.get(j) + 1); spaltenCounter.set(j, spaltenCounter.get(j) + 1); } } // Wenn irgendwo nur NaNs waren, das auch so wiedergeben for (int i = 0; i < spaltenCounter.size(); i++) if (spaltenCounter.get(i) == 0) retVal[i] = Double.NaN; return retVal; } }