From 41bf3d1f0f2f1350aca0b5cb5455851314f01fc4 Mon Sep 17 00:00:00 2001 From: Joachim Klein Date: Wed, 30 May 2018 15:35:32 +0200 Subject: [PATCH] PrismUtils.normalise() for vector and subset of states --- prism/src/prism/PrismUtils.java | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/prism/src/prism/PrismUtils.java b/prism/src/prism/PrismUtils.java index 19e20e0f..abbc5a9b 100644 --- a/prism/src/prism/PrismUtils.java +++ b/prism/src/prism/PrismUtils.java @@ -393,7 +393,31 @@ public class PrismUtils } /** - * Format a large integer, represented by a double, as a string. + * Normalise the given entries in the vector in-place such that that they sum to 1, + * I.e., for all indizes of entries, set
+ * {@code vector[s] = vector[s] / sum}, where
+ * {@code sum = sum_{s in entries} (vector[s])
+ * If {@code sum = 0.0}, all entries are set to {@code NaN}. + * @param vector the vector + * @param entries Iterable over the entries (must not contain duplicates) + * @return the altered vector + */ + public static double[] normalise(double[] vector, IterableInt entries) + { + double sum = 0.0; + for (PrimitiveIterator.OfInt iter = entries.iterator(); iter.hasNext();) { + int state = iter.nextInt(); + sum += vector[state]; + } + for (PrimitiveIterator.OfInt iter = entries.iterator(); iter.hasNext();) { + int state = iter.nextInt(); + vector[state] /= sum; + } + return vector; + } + + /** + * Format a large integer, represented by a double, as a string. Un */ public static String bigIntToString(double d) {