From c25c745ae9129e461912a5c5ea479f9bcccc083d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20M=C3=A4rcker?= Date: Mon, 4 Jun 2018 14:50:04 +0200 Subject: [PATCH] PrismUtils#doublesAreClose with filtering via an IterableInt --- prism/src/prism/PrismUtils.java | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/prism/src/prism/PrismUtils.java b/prism/src/prism/PrismUtils.java index abbc5a9b..94309532 100644 --- a/prism/src/prism/PrismUtils.java +++ b/prism/src/prism/PrismUtils.java @@ -36,6 +36,8 @@ import java.util.PrimitiveIterator; import java.util.regex.Matcher; import java.util.regex.Pattern; +import common.iterable.IterableInt; + /** * Various general-purpose utility methods in Java */ @@ -118,22 +120,32 @@ public class PrismUtils */ public static boolean doublesAreClose(double d1[], double d2[], double epsilon, boolean abs) { - int i, n; - n = Math.min(d1.length, d2.length); + int n = Math.min(d1.length, d2.length); if (abs) { - for (i = 0; i < n; i++) { - if (!PrismUtils.doublesAreCloseAbs(d1[i], d2[i], epsilon)) + for (int i = 0; i < n; i++) { + if (!doublesAreCloseAbs(d1[i], d2[i], epsilon)) return false; } } else { - for (i = 0; i < n; i++) { - if (!PrismUtils.doublesAreCloseRel(d1[i], d2[i], epsilon)) + for (int i = 0; i < n; i++) { + if (!doublesAreCloseRel(d1[i], d2[i], epsilon)) return false; } } return true; } + /** + * See if, for all the entries given by the {@code indizes} + * iterator, two arrays of doubles are all within epsilon of each other (relative or absolute error). + *
+ * Considers Inf == Inf and -Inf == -Inf. + */ + public static boolean doublesAreClose(double d1[], double d2[], IterableInt indizes, double epsilon, boolean abs) + { + return doublesAreClose(d1, d2, indizes.iterator(), epsilon, abs); + } + /** * See if, for all the entries given by the {@code indizes} * iterator, two arrays of doubles are all within epsilon of each other (relative or absolute error). @@ -145,13 +157,13 @@ public class PrismUtils if (abs) { while (indizes.hasNext()) { int i = indizes.nextInt(); - if (!PrismUtils.doublesAreCloseAbs(d1[i], d2[i], epsilon)) + if (!doublesAreCloseAbs(d1[i], d2[i], epsilon)) return false; } } else { while (indizes.hasNext()) { int i = indizes.nextInt(); - if (!PrismUtils.doublesAreCloseRel(d1[i], d2[i], epsilon)) + if (!doublesAreCloseRel(d1[i], d2[i], epsilon)) return false; } }