From 5487ab0ee6ea4ca5e050528f0325c4e6586e7edb Mon Sep 17 00:00:00 2001 From: depristo Date: Wed, 5 Aug 2009 00:41:31 +0000 Subject: [PATCH] Added several useful routines to MathUtils for summing and bounds checking of doubles git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@1379 348d0f76-0448-11de-a6fe-93d51630548a --- .../broadinstitute/sting/utils/MathUtils.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/java/src/org/broadinstitute/sting/utils/MathUtils.java b/java/src/org/broadinstitute/sting/utils/MathUtils.java index b93e8f0ae..0edd6427c 100755 --- a/java/src/org/broadinstitute/sting/utils/MathUtils.java +++ b/java/src/org/broadinstitute/sting/utils/MathUtils.java @@ -11,6 +11,42 @@ public class MathUtils { /** Private constructor. No instantiating this class! */ private MathUtils() {} + public static double sum(double[] values) { + double s = 0.0; + for ( double v : values) s += v; + return s; + } + + public static double sumLog10(double[] log10values) { + double s = 0.0; + for ( double v : log10values) s += Math.pow(10.0, v); + return s; + } + + public static boolean wellFormedDouble(double val) { + return ! Double.isInfinite(val) && ! Double.isNaN(val); + } + + public static boolean isBounded(double val, double lower, double upper) { + return val >= lower && val <= upper; + } + + public static boolean isPositive(double val) { + return ! isNegativeOrZero(val); + } + + public static boolean isPositiveOrZero(double val) { + return isBounded(val, 0.0, Double.POSITIVE_INFINITY); + } + + public static boolean isNegativeOrZero(double val) { + return isBounded(val, Double.NEGATIVE_INFINITY, 0.0); + } + + public static boolean isNegative(double val) { + return ! isPositiveOrZero(val); + } + /** * Compares double values for equality (within 1e-6), or inequality. *