/* * Created on Aug 24, 2003 by Ravi Mohan * */ package aima.util; import java.util.ArrayList; import java.util.Hashtable; import java.util.List; import java.util.Random; public class Util { public static final String NO = "No"; public static final String YES = "Yes"; private static Random r = new Random(); public static T first(List l) { List newList = new ArrayList(); for(T element : l){ newList.add(element); } return newList.get(0); } public static List rest(List l) { List newList = new ArrayList(); for(T element : l){ newList.add(element); } newList.remove(0); return newList; } public static boolean randomBoolean() { int trueOrFalse = r.nextInt(2); return (!(trueOrFalse == 0)); } public static double[] normalize(double[] probDist) { int len = probDist.length; double total = 0.0; for (double d : probDist) { total = total + d; } double[] normalized = new double[len]; if (total != 0) { for (int i = 0; i < len; i++) { normalized[i] = probDist[i] / total; } } double totalN = 0.0; for (double d : normalized) { totalN = totalN + d; } return normalized; } public static List normalize(List values) { double[] valuesAsArray = new double[values.size()]; for (int i=0;iresults = new ArrayList(); for (int i=0;i j ? j : i); } public static int max(int i, int j) { return (i < j ? j : i); } public static T selectRandomlyFromList(List l) { int index = r.nextInt(l.size()); return l.get(index); } public static T mode(List l) { Hashtable hash = new Hashtable(); for (T obj : l) { if (hash.containsKey(obj)) { hash.put(obj, hash.get(obj).intValue() + 1); } else { hash.put(obj, 1); } } T maxkey = hash.keySet().iterator().next(); for (T key : hash.keySet()) { if (hash.get(key) > hash.get(maxkey)) { maxkey = key; } } return maxkey; } public static String[] yesno() { return new String[] { YES, NO }; } public static double log2(double d) { return Math.log(d) / Math.log(2); } public static double information(double[] probabilities) { double total = 0.0; for (double d : probabilities) { total += (-1.0 * log2(d) * d); } return total; } public static List removeFrom(List list, T member) { List newList = new ArrayList(); for (T s : list) { if (!(s.equals(member))) { newList.add(s); } } return newList; } public static double sumOfSquares(List list) { double accum =0; for (T item :list){ accum = accum + (item.doubleValue() * item.doubleValue()); } return accum; } public static String ntimes(String s,int n){ StringBuffer buf = new StringBuffer(); for (int i = 0; i < n; i++) { buf.append(s); } return buf.toString(); } }