From 3dd6a1f96272a6794108d88ac14ab8f892996ae4 Mon Sep 17 00:00:00 2001 From: Mauricio Carneiro Date: Wed, 1 Feb 2012 19:35:09 -0500 Subject: [PATCH] Adding some generic sum and average functions to MathUtils --- .../broadinstitute/sting/utils/MathUtils.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/public/java/src/org/broadinstitute/sting/utils/MathUtils.java b/public/java/src/org/broadinstitute/sting/utils/MathUtils.java index 2f2dbd47e..814cb2765 100644 --- a/public/java/src/org/broadinstitute/sting/utils/MathUtils.java +++ b/public/java/src/org/broadinstitute/sting/utils/MathUtils.java @@ -137,6 +137,10 @@ public class MathUtils { return size; } + + public static double average(Collection x) { + return (double) sum(x) / x.size(); + } public static double average(Collection numbers, boolean ignoreNan) { if (ignoreNan) { @@ -176,6 +180,13 @@ public class MathUtils { return s; } + public static long sum(int[] x) { + long total = 0; + for (int v : x) + total += v; + return total; + } + /** * Calculates the log10 cumulative sum of an array with log10 probabilities @@ -722,6 +733,13 @@ public class MathUtils { return average(vals, vals.size()); } + public static double average(int[] x) { + int sum = 0; + for (int v : x) + sum += v; + return (double) sum / x.length; + } + public static byte average(byte[] vals) { int sum = 0; for (byte v : vals) { @@ -1079,6 +1097,13 @@ public class MathUtils { return getQScoreOrderStatistic(reads, offsets, (int) Math.floor(reads.size() / 2.)); } + public static long sum(Collection x) { + long sum = 0; + for (int v : x) + sum += v; + return sum; + } + /** * A utility class that computes on the fly average and standard deviation for a stream of numbers. * The number of observations does not have to be known in advance, and can be also very big (so that