jbil.util
Class ArrayUtilities

java.lang.Object
  extended by jbil.util.ArrayUtilities

public class ArrayUtilities
extends java.lang.Object

Assorted array utility functions.

Author:
Paulo G. S. da Fonseca

Constructor Summary
ArrayUtilities()
           
 
Method Summary
static void fillInSequence(int[] a, int startValue)
          Fills a vector with successive values starting from a given value.
static char[] intersection(char[] a, char[] b)
          Computes the intersection between two char arrays.
static int max(int[] a)
          Returns the maximum of an int array.
static int min(int[] a)
          Returns the minimum of an int array.
static double[] normalize(double[] v)
          Returns the normalised values of an array so that they sum up to one.
static int[] sequence(int start, int length)
          Returns an int array with succesive elements.
static int[] sortIndexes(double[] a)
          Sorts the elements of an array and returns their indexes in order.
static double[] subArray(double[] v, boolean[] indexMask)
          Return the subarray of a double array by picking the entries indicated by true entries of a boolean mask array of the same size.
static int[] subArray(int[] v, boolean[] indexMask)
          Return the subarray of an int array by picking the entries indicated by true entries of a boolean mask array of the same size.
static int sum(int[] a)
          Returns the sum of the elements of an int array.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ArrayUtilities

public ArrayUtilities()
Method Detail

intersection

public static char[] intersection(char[] a,
                                  char[] b)
Computes the intersection between two char arrays. The returned array shows the intersection elements in the order they appear in the first vector (a). To obtain the elements in the order they appear in the second vector, it suffices to invert the argument order in the function call.

Parameters:
a - The first vector.
b - The second vector.
Returns:
The intersection between a and b.

min

public static int min(int[] a)
Returns the minimum of an int array.


max

public static int max(int[] a)
Returns the maximum of an int array.


sum

public static int sum(int[] a)
Returns the sum of the elements of an int array.


normalize

public static double[] normalize(double[] v)
Returns the normalised values of an array so that they sum up to one. That is, for (X0,...,Xn) returns (Y0,...,Yn) where Yi=Xi/Sum(X), and Sum(X)=X0+...+Xn. This method may throw a runtime exception indicating its incapacity to normalise the values due to numerical precision problems.


fillInSequence

public static void fillInSequence(int[] a,
                                  int startValue)
Fills a vector with successive values starting from a given value.

Parameters:
a - The vector to be filled.
startValue - The value of the first element of the array.

sequence

public static int[] sequence(int start,
                             int length)
Returns an int array with succesive elements.

Parameters:
start - The start value of the sequence.
length - The length of the sequence.
Returns:
The int array {start, start+1,...,start+length-1}

subArray

public static int[] subArray(int[] v,
                             boolean[] indexMask)
Return the subarray of an int array by picking the entries indicated by true entries of a boolean mask array of the same size. For example, subArray({0,1,2,3,4,5}, {true, false, false, true, false, true}) gives {0,3,5}.

Parameters:
v - The base array.
indexMask - The indexes boolean mask.

subArray

public static double[] subArray(double[] v,
                                boolean[] indexMask)
Return the subarray of a double array by picking the entries indicated by true entries of a boolean mask array of the same size.

See Also:
subArray(int[], boolean[])

sortIndexes

public static int[] sortIndexes(double[] a)
Sorts the elements of an array and returns their indexes in order. That is, for an array (X0,...,Xn), returns the permutation (i0,..,in) of (0,...,n) s.t. Xi0<=Xi1<=...<=Xin.

Example: for (3,1,5,4,2) returns (1,4,0,3,2)

Parameters:
a - a double[] array.
Returns:
The sequence of indexes of the sorted elements of a.