This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Expand the class heirachy to include functions, ranges, and domains

  • Loading branch information...
javajawa committed Mar 11, 2012
1 parent 13177f5 commit cdf1945c319b4cd6d08979688f490612eeaa1be8
@@ -39,20 +39,23 @@ public static void main(String[] args)
return new Distribution<Integer>()
{
@Override
public double probabilityOf(Integer value)
public Double valueOf(Integer value)
{
return Math.E / value;
}
@Override
public Integer rangeMin()
public Domain<Integer> domain()
{
return 0;
return new IntRange(0, Integer.MAX_VALUE);
}
@Override
public Integer rangeMax()
public Domain<Double> range()
{
return Integer.MAX_VALUE;
return new DoubleRange(0.0D, 1.0D, Double.MIN_NORMAL);
}
};
}
@@ -1,8 +1,6 @@
package networkssim.entropy;
import java.util.Set;
@SuppressWarnings("MarkerInterface")
public interface DiscreteDistribution<T extends Comparable<T> > extends Distribution<T>
{
Set<T> domain();
}
@@ -1,12 +1,10 @@
package networkssim.entropy;
/**
*
* @author Benedict
*/
public interface Distribution<T extends Comparable<T> >
import networkssim.functions.Function;
@SuppressWarnings("MarkerInterface")
public interface Distribution<T extends Comparable<T> > extends Function<T, Double>
{
double probabilityOf(T value);
T rangeMin();
T rangeMax();
@Override
Double valueOf(T value);
}
@@ -7,9 +7,12 @@
import java.util.Random;
import networkssim.entropy.DiscreteDistribution;
import networkssim.entropy.Randomised;
import networkssim.sets.Domain;
import networkssim.sets.DoubleRange;
import networkssim.sets.IntRange;
import networkssim.utilities.Combinatorics;
public class Bernoulli extends Randomised implements DiscreteProcess, DiscreteDistribution<Long>
public class Bernoulli extends Randomised implements DiscreteProcess, DiscreteDistribution<Integer>
{
private final int trials;
private final double p;
@@ -68,30 +71,21 @@ private static long sample(int trials, double p, double rand)
}
@Override
public Set<Long> domain()
public Domain<Integer> domain()
{
Set<Long> s = new HashSet<Long>(trials + 1);
for (long i = 0; i <= trials; ++i)
s.add(i);
return s;
return new IntRange(0, trials);
}
@Override
public double probabilityOf(Long value)
public Domain<Double> range()
{
return probabilities[value.intValue()];
return new DoubleRange(0, 1, Double.MIN_NORMAL);
}
@Override
public Long rangeMin()
public Double valueOf(Integer value)
{
return 0L;
}
@Override
public Long rangeMax()
{
return (long)trials;
return probabilities[value.intValue()];
}
public static double probabilityOf(int trials, double p, int value)
@@ -101,4 +95,5 @@ public static double probabilityOf(int trials, double p, int value)
d = d.multiply(BigDecimal.valueOf(1-p).pow(trials - value));
return d.doubleValue();
}
}
@@ -0,0 +1,10 @@
package networkssim.functions;
import networkssim.sets.Domain;
public interface Function<X,Y>
{
Y valueOf(X value);
Domain<X> domain();
Domain<Y> range();
}
@@ -0,0 +1,60 @@
package networkssim.sets;
import java.util.Collection;
import java.util.Set;
public abstract class Domain<T> implements Set<T>
{
@Override
public final boolean isEmpty()
{
return false;
}
@Override
public final boolean add(T e)
{
throw new UnsupportedOperationException("Domains are immutable");
}
@Override
public final boolean remove(Object o)
{
throw new UnsupportedOperationException("Domains are immutable");
}
@Override
@SuppressWarnings("element-type-mismatch")
public boolean containsAll(Collection<?> c)
{
for (Object o : c)
if (!contains(o)) return false;
return true;
}
@Override
public final boolean addAll(Collection<? extends T> c)
{
throw new UnsupportedOperationException("Domains are immutable");
}
@Override
public final boolean retainAll(Collection<?> c)
{
throw new UnsupportedOperationException("Domains are immutable");
}
@Override
public final boolean removeAll(Collection<?> c)
{
throw new UnsupportedOperationException("Domains are immutable");
}
@Override
public final void clear()
{
throw new UnsupportedOperationException("Domains are immutable");
}
}
@@ -0,0 +1,87 @@
package networkssim.sets;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
public class DoubleRange extends Domain<Double>
{
private final double lower;
private final double upper;
private final double step;
private final int size;
public DoubleRange(double lower, double upper, double step)
{
this.lower = lower;
this.upper = upper;
this.step = step;
this.size = (int)Math.floor((upper - lower) / step);
}
@Override
public int size()
{
return size;
}
@Override
public boolean contains(Object o)
{
if (!(o instanceof Number))
return false;
final double n = ((Number)o).doubleValue() - lower;
if (n < 0 || n > (upper - lower))
return false;
final double del = Math.round(n/step) - n/step;
return (del < step/20 && del > -step/20);
}
@Override
public Iterator<Double> iterator()
{
return new Iterator<Double>() {
private int i = -1;
@Override
public boolean hasNext()
{
return i + 1 < size();
}
@Override
public Double next()
{
++i;
return lower + step * i;
}
@Override
public void remove()
{
throw new UnsupportedOperationException("Ranges are not modifable");
}
};
}
@Override
public Double[] toArray()
{
Double[] ret = new Double[size()];
int i = 0;
for (Double d : this)
ret[i++] = d;
return ret;
}
@Override
public <T> T[] toArray(T[] a)
{
throw new UnsupportedOperationException("Not supported yet.");
}
}
@@ -0,0 +1,90 @@
package networkssim.sets;
import java.util.Iterator;
public class IntRange extends Range<Integer>
{
private final int lower;
private final int upper;
private final int step;
private final int size;
public IntRange(int lower, int upper)
{
this(lower, upper, 1);
}
public IntRange(int lower, int upper, int step)
{
this.lower = lower;
this.upper = upper;
this.step = step;
this.size = (int)Math.floor((upper - lower) / step);
}
@Override
public int size()
{
return size;
}
@Override
public boolean contains(Object o)
{
if (!(o instanceof Number))
return false;
final double n = ((Number)o).doubleValue() - lower;
if (n < 0 || n > (upper - lower))
return false;
final double del = Math.round(n/step) - n/step;
return (del < step/20 && del > -step/20);
}
@Override
public Iterator<Integer> iterator()
{
return new Iterator<Integer>() {
private int i = -1;
@Override
public boolean hasNext()
{
return i + 1 < size();
}
@Override
public Integer next()
{
++i;
return lower + step * i;
}
@Override
public void remove()
{
throw new UnsupportedOperationException("Ranges are not modifable");
}
};
}
@Override
public Integer[] toArray()
{
Integer[] ret = new Integer[size()];
int i = 0;
for (Integer d : this)
ret[i++] = d;
return ret;
}
@Override
public <T> T[] toArray(T[] a)
{
throw new UnsupportedOperationException("Not supported yet.");
}
}
Oops, something went wrong.

0 comments on commit cdf1945

Please sign in to comment.