import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.*;
/**
* Created with IntelliJ IDEA.
* User: rebeccamancy
* Date: 26/10/2012
* Time: 17:27
* To change this template use File | Settings | File Templates.
*/
public class Util {
/**
* Sums elements in an array of doubles
* @param myArray array of doubles
* @return sum of all elements
*/
public static double sumArray(double[] myArray) {
double sum = 0.0;
for (double entry : myArray) {
sum += entry;
}
return sum;
}
/**
* Sums elements in an array of integers
* @param myArray array of integers
* @return sum of all elements
*/
public static int sumArray(int[] myArray) {
int sum = 0;
for (int entry : myArray) {
sum += entry;
}
return sum;
}
/**
* General method that draws from an array of doubles a position proportional to the "size" of the slice
* associated with the entry in the array (imagine that array entries measure slices of a number line)
* @param proportions array of proportions of the line (don't need to sum to 1)
* @return the selected array index
*/
public static int drawPosit(double[] proportions) {
int index = 0;
double p = Math.random() * sumArray(proportions);
double lwb = 0.0; // lower bound for array
for (int ind = 0; ind < proportions.length; ind ++) {
if (lwb <= p && p < lwb + proportions[ind]) { // if p falls in this segment
index = ind;
break;
}
lwb = lwb + proportions[ind];
}
return index;
}
/**
* Returns a pseudo-random number between min and max, inclusive.
* The difference between min and max can be at most
* Integer.MAX_VALUE - 1
.
*
* @param min Minimim value
* @param max Maximim value. Must be greater than min.
* @return Integer between min and max, inclusive.
* @see java.util.Random#nextInt(int)
*/
public static int randInt(int min, int max) {
// Usually this can be a field rather than a method variable
Random rand = new Random();
// nextInt is normally exclusive of the top value,
// so add 1 to make it inclusive
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
/**
* Converts a ListArray of Integers into a primitive int[] array
* @param integers List of integers
* @return Array of integers
*/
public static int[] convertIntegers(List integers)
{
int[] ret = new int[integers.size()];
Iterator iterator = integers.iterator();
for (int i = 0; i < ret.length; i++)
{
ret[i] = iterator.next().intValue();
}
return ret;
}
/**
* Converts a string in the form [1,3,...5] to an integer array
* @param arr String representing an array
* @return integer array
*/
public static int[] str2arr(String arr) {
String[] items = arr.replaceAll("\\[", "").replaceAll("\\]", "").split(",");
int[] results = new int[items.length];
for (int i = 0; i < items.length; i++) {
try {
results[i] = Integer.parseInt(items[i]);
} catch (NumberFormatException nfe) {
nfe.printStackTrace();
System.exit(1);
}
}
return results;
}
public static String arr2str(double[] arr) {
String results = "";
for (int i=0; i makeConfigMap(String configLocation) {
// Read in parameters from configuration file (.properties) from file given in command line call
//HashMap configMap;
HashMap configMap = new HashMap();
Properties configFile = new Properties();
try {
FileInputStream in = new FileInputStream(configLocation);
configFile.load(in);
in.close();
configMap = new HashMap((Map) configFile);
} catch (IOException ex) {
ex.printStackTrace();
System.exit(1);
} catch (ClassCastException ex2) {
ex2.printStackTrace();
System.exit(1);
}
return configMap;
}
public static int firstNonZero(int[] arr) {
final int ZERO = 0;
int currentIndex = 0;
boolean itemFound = false;
while (!itemFound && currentIndex < arr.length) {
if (arr[currentIndex] != ZERO) {
itemFound = true;
} else {
currentIndex++;
}
}
if (!itemFound)
return -1;
else
return currentIndex;
}
public static double largestAbsVal(double[] arr) {
double val = arr[0];
for (double x : arr) {
if (Math.abs(x) > val) {
val = Math.abs(x);
}
}
return val;
}
public static int largestAbsIndex(double[] arr) {
int ind = 0;
double val = arr[0];
for (int i = 0; i < arr.length; i++) {
if (Math.abs(arr[i]) > val) {
ind = i;
val = Math.abs(arr[i]);
}
}
return ind;
}
public static int largestAbsIndexCheck(double[] arr, String where) {
int ind = 0;
double val = arr[0];
for (int i = 0; i < arr.length; i++) {
if (Math.abs(arr[i]) > val) {
ind = i;
val = Math.abs(arr[i]);
}
}
double[] newArray = Util.subArray(arr, ind);
double secLargestVal = newArray[0];
for (int i = 0; i < newArray.length; i++) {
if (Math.abs(newArray[i]) > secLargestVal) {
secLargestVal = Math.abs(newArray[i]);
}
}
//System.out.println("val=" + val + " secLargestVal=" + secLargestVal);
if (almostEqual(val, secLargestVal, 1E-13)) {
System.err.println("Warning: two values indistinguishable - possibility of rounding errors " + where);
}
return ind;
}
public static boolean almostEqual(double a, double b, double eps){
return Math.abs(a-b)