Adding some generic sum and average functions to MathUtils

This commit is contained in:
Mauricio Carneiro 2012-02-01 19:35:09 -05:00
parent e1d69e4060
commit 3dd6a1f962
1 changed files with 25 additions and 0 deletions

View File

@ -138,6 +138,10 @@ public class MathUtils {
return size; return size;
} }
public static double average(Collection<Integer> x) {
return (double) sum(x) / x.size();
}
public static double average(Collection<Number> numbers, boolean ignoreNan) { public static double average(Collection<Number> numbers, boolean ignoreNan) {
if (ignoreNan) { if (ignoreNan) {
return sum(numbers, true) / nonNanSize(numbers); return sum(numbers, true) / nonNanSize(numbers);
@ -176,6 +180,13 @@ public class MathUtils {
return s; 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 * Calculates the log10 cumulative sum of an array with log10 probabilities
@ -722,6 +733,13 @@ public class MathUtils {
return average(vals, vals.size()); 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) { public static byte average(byte[] vals) {
int sum = 0; int sum = 0;
for (byte v : vals) { for (byte v : vals) {
@ -1079,6 +1097,13 @@ public class MathUtils {
return getQScoreOrderStatistic(reads, offsets, (int) Math.floor(reads.size() / 2.)); return getQScoreOrderStatistic(reads, offsets, (int) Math.floor(reads.size() / 2.));
} }
public static long sum(Collection<Integer> 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. * 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 * The number of observations does not have to be known in advance, and can be also very big (so that