diff --git a/prism/src/explicit/Distribution.java b/prism/src/explicit/Distribution.java index da2fac6b..0d939c02 100644 --- a/prism/src/explicit/Distribution.java +++ b/prism/src/explicit/Distribution.java @@ -31,29 +31,45 @@ import java.util.Map.Entry; import prism.PrismUtils; +/** + * Explicit representation of a probability distribution. + * Basically, a mapping from (integer-valued) state indices to (non-zero, double-valued) probabilities. + */ public class Distribution implements Iterable> { private HashMap map; + /** + * Create an empty distribution. + */ public Distribution() { clear(); } + /** + * Clear all entries of the distribution. + */ public void clear() { map = new HashMap(); } + /** + * Add 'prob' to the probability for state 'j'. + */ public void add(int j, double prob) { Double d = (Double) map.get(j); if (d == null) map.put(j, prob); else - map.put(j, d + prob); + set(j, d + prob); } + /** + * Set the probability for state 'j' to 'prob'. + */ public void set(int j, double prob) { if (prob == 0.0) @@ -61,6 +77,9 @@ public class Distribution implements Iterable> map.put(j, prob); } + /** + * Get the probability for state j. + */ public double get(int j) { Double d; @@ -90,21 +109,33 @@ public class Distribution implements Iterable> return true; } + /** + * Get an iterator over the entries of the map defining the distribution. + */ public Iterator> iterator() { return map.entrySet().iterator(); } + /** + * Returns true if the distribution is empty. + */ public boolean isEmpty() { return map.isEmpty(); } + /** + * Get the size of the support of the distribution. + */ public int size() { return map.size(); } + /** + * Get the sum of the probabilities in the distribution. + */ public double sum() { double d = 0.0; @@ -116,6 +147,9 @@ public class Distribution implements Iterable> return d; } + /** + * Get the sum of all the probabilities in the distribution except for state j. + */ public double sumAllBut(int j) { double d = 0.0; @@ -128,6 +162,22 @@ public class Distribution implements Iterable> return d; } + /** + * Create a new distribution, based on a mapping from the state indices + * used in this distribution to a different set of state indices. + */ + public Distribution map(int map[]) + { + Distribution distrNew = new Distribution(); + Iterator> i = iterator(); + while (i.hasNext()) { + Map.Entry e = i.next(); + distrNew.add(map[e.getKey()], e.getValue()); + } + return distrNew; + } + + @Override public boolean equals(Object o) { Double d1, d2; @@ -143,12 +193,14 @@ public class Distribution implements Iterable> return true; } + @Override public int hashCode() { // Simple hash code return map.size(); } + @Override public String toString() { return "" + map;