org.opensourcephysics.numerics
Class ArrayLib

java.lang.Object
  extended by org.opensourcephysics.numerics.ArrayLib

public abstract class ArrayLib
extends java.lang.Object

Library of supplementary array routines not supported by the java.util.Arrays class.

Author:
jeffrey heer

Field Summary
static int IndexMax
           
static int IndexMin
           
static int SORT_THRESHOLD
          Arrays with lengths beneath this value will be insertion sorted.
 
Constructor Summary
ArrayLib()
           
 
Method Summary
static int binarySearch(int[] a, int key)
          Perform a binary search over a sorted array for the given key.
static int binarySearch(int[] a, int key, int length)
          Perform a binary search over a sorted range of an array for the given key.
static int binarySearch(int[] a, int key, int begin, int end)
          Perform a binary search over a sorted range of an array for the given key.
static int binarySearch(java.lang.Object[] a, java.lang.Object key)
          Perform a binary search over a sorted array for the given key.
static int binarySearch(java.lang.Object[] a, java.lang.Object key, java.util.Comparator<java.lang.Object> cp)
          Perform a binary search over a sorted array for the given key.
static int binarySearch(java.lang.Object[] a, java.lang.Object key, java.util.Comparator<java.lang.Object> cp, int length)
          Perform a binary search over a sorted range of an array for the given key.
static int binarySearch(java.lang.Object[] a, java.lang.Object key, java.util.Comparator<java.lang.Object> cp, int begin, int end)
          Perform a binary search over a sorted range of an array for the given key.
static int binarySearch(java.lang.Object[] a, java.lang.Object key, int length)
          Perform a binary search over a sorted range of an array for the given key.
static int binarySearch(java.lang.Object[] a, java.lang.Object key, int begin, int end)
          Perform a binary search over a sorted range of an array for the given key.
static int find(int[] a, int key)
          Linearly search an array for a given key value.
static int find(int[] a, int key, int length)
          Linearly search an array range for a given key value.
static int find(int[] a, int key, int begin, int end)
          Linearly search an array range for a given key value.
static int getIndexMax()
           
static int getIndexMin()
           
static int[] getIntArray(java.lang.String filename)
          Read in a text file as an array of integers.
static double max(double[] a)
          Find the maximum value in an array.
static double min(double[] a)
          Find the minimum value in an array.
static double[] resize(double[] a, int size)
          Resize the given array as needed to meet a target size.
static float[] resize(float[] a, int size)
          Resize the given array as needed to meet a target size.
static int[] resize(int[] a, int size)
          Resize the given array as needed to meet a target size.
static java.lang.Object[] resize(java.lang.Object[] a, int size)
          Resize the given array as needed to meet a target size.
static void shuffle(double[] a, int start, int len, java.util.Random r)
          Randomly permute the contents of a range an array.
static void shuffle(double[] a, java.util.Random r)
          Randomly permute the contents of an array.
static void shuffle(float[] a, int start, int len, java.util.Random r)
          Randomly permute the contents of a range an array.
static void shuffle(float[] a, java.util.Random r)
          Randomly permute the contents of an array.
static void shuffle(int[] a, int start, int len, java.util.Random r)
          Randomly permute the contents of a range an array.
static void shuffle(int[] a, java.util.Random r)
          Randomly permute the contents of an array.
static void shuffle(long[] a, int start, int len, java.util.Random r)
          Randomly permute the contents of a range an array.
static void shuffle(long[] a, java.util.Random r)
          Randomly permute the contents of an array.
static void shuffle(java.lang.Object[] a, int start, int len, java.util.Random r)
          Randomly permute the contents of a range an array.
static void shuffle(java.lang.Object[] a, java.util.Random r)
          Randomly permute the contents of an array.
static void sort(double[] a, int[] b)
          Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
static void sort(double[] a, int[] b, int length)
          Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
static void sort(double[] a, int[] b, int begin, int end)
          Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
static void sort(float[] a, int[] b)
          Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
static void sort(float[] a, int[] b, int length)
          Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
static void sort(float[] a, int[] b, int begin, int end)
          Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
static void sort(int[] a, double[] b)
          Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
static void sort(int[] a, double[] b, int length)
          Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
static void sort(int[] a, double[] b, int begin, int end)
          Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
static void sort(int[] a, int[] b)
          Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
static void sort(int[] a, int[] b, int length)
          Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
static void sort(int[] a, int[] b, int begin, int end)
          Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
static void sort(int[] a, java.lang.Object[] b, int[] abuf, java.lang.Object[] bbuf, int begin, int end)
          Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
static void sort(int[] a, java.lang.Object[] b, int begin, int end)
          Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
static void sort(java.lang.Object[] a, int[] b, java.util.Comparator<java.lang.Object> cmp)
          Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
static void sort(java.lang.Object[] a, int[] b, int length, java.util.Comparator<java.lang.Object> cmp)
          Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
static void sort(java.lang.Object[] a, int[] b, int begin, int end, java.util.Comparator<java.lang.Object> cmp)
          Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
static double sum(double[] a)
          Compute the sum of the values in an array.
static double[] trim(double[] a, int size)
          Trims an array to be exactly the target a size.
static float[] trim(float[] a, int size)
          Trims an array to be exactly the target a size.
static int[] trim(int[] a, int size)
          Trims an array to be exactly the target a size.
static java.lang.Object[] trim(java.lang.Object[] a, int size)
          Trims an array to be exactly the target a size.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

IndexMax

public static int IndexMax

IndexMin

public static int IndexMin

SORT_THRESHOLD

public static final int SORT_THRESHOLD
Arrays with lengths beneath this value will be insertion sorted.

See Also:
Constant Field Values
Constructor Detail

ArrayLib

public ArrayLib()
Method Detail

shuffle

public static final void shuffle(int[] a,
                                 java.util.Random r)
Randomly permute the contents of an array.

Parameters:
a - the array to shuffle
r - the source of randomness to use

shuffle

public static final void shuffle(int[] a,
                                 int start,
                                 int len,
                                 java.util.Random r)
Randomly permute the contents of a range an array.

Parameters:
a - the array to shuffle
start - the starting index of the range to shuffle
len - then length of the range to shuffle
r - the source of randomness to use

shuffle

public static final void shuffle(long[] a,
                                 java.util.Random r)
Randomly permute the contents of an array.

Parameters:
a - the array to shuffle
r - the source of randomness to use

shuffle

public static final void shuffle(long[] a,
                                 int start,
                                 int len,
                                 java.util.Random r)
Randomly permute the contents of a range an array.

Parameters:
a - the array to shuffle
start - the starting index of the range to shuffle
len - then length of the range to shuffle
r - the source of randomness to use

shuffle

public static final void shuffle(float[] a,
                                 java.util.Random r)
Randomly permute the contents of an array.

Parameters:
a - the array to shuffle
r - the source of randomness to use

shuffle

public static final void shuffle(float[] a,
                                 int start,
                                 int len,
                                 java.util.Random r)
Randomly permute the contents of a range an array.

Parameters:
a - the array to shuffle
start - the starting index of the range to shuffle
len - then length of the range to shuffle
r - the source of randomness to use

shuffle

public static final void shuffle(double[] a,
                                 java.util.Random r)
Randomly permute the contents of an array.

Parameters:
a - the array to shuffle
r - the source of randomness to use

shuffle

public static final void shuffle(double[] a,
                                 int start,
                                 int len,
                                 java.util.Random r)
Randomly permute the contents of a range an array.

Parameters:
a - the array to shuffle
start - the starting index of the range to shuffle
len - then length of the range to shuffle
r - the source of randomness to use

shuffle

public static final void shuffle(java.lang.Object[] a,
                                 java.util.Random r)
Randomly permute the contents of an array.

Parameters:
a - the array to shuffle
r - the source of randomness to use

shuffle

public static final void shuffle(java.lang.Object[] a,
                                 int start,
                                 int len,
                                 java.util.Random r)
Randomly permute the contents of a range an array.

Parameters:
a - the array to shuffle
start - the starting index of the range to shuffle
len - then length of the range to shuffle
r - the source of randomness to use

max

public static final double max(double[] a)
Find the maximum value in an array.

Parameters:
a - the array
indMax - the array minimum index
Returns:
the maximum value in the array

min

public static final double min(double[] a)
Find the minimum value in an array.

Parameters:
a - the array
indMin - the array minimum index
Returns:
the minimum value in the array

getIndexMax

public static final int getIndexMax()
Parameters:
IndexMax - the array minimum index
Returns:
IndexMax

getIndexMin

public static final int getIndexMin()
Parameters:
IndexMin - the array minimum index
Returns:
IndexMin

sum

public static final double sum(double[] a)
Compute the sum of the values in an array.

Parameters:
a - the array
Returns:
the sum of the values in the array

binarySearch

public static final int binarySearch(int[] a,
                                     int key)
Perform a binary search over a sorted array for the given key.

Parameters:
a - the array to search
key - the key to search for
Returns:
the index of the given key if it exists in the array, otherwise -1 times the index value at the insertion point that would be used if the key were added to the array.

binarySearch

public static final int binarySearch(int[] a,
                                     int key,
                                     int length)
Perform a binary search over a sorted range of an array for the given key. The range is assumed to start at index 0.

Parameters:
a - the array to search
key - the key to search for
length - the the length of the range to search over.
Returns:
the index of the given key if it exists in the array, otherwise -1 times the index value at the insertion point that would be used if the key were added to the array.

binarySearch

public static final int binarySearch(int[] a,
                                     int key,
                                     int begin,
                                     int end)
Perform a binary search over a sorted range of an array for the given key.

Parameters:
a - the array to search
key - the key to search for
begin - the starting index of the range
end - the ending index of the range, exclusive
Returns:
the index of the given key if it exists in the array, otherwise -1 times the index value at the insertion point that would be used if the key were added to the array.

binarySearch

public static final int binarySearch(java.lang.Object[] a,
                                     java.lang.Object key)
Perform a binary search over a sorted array for the given key.

Parameters:
a - the array to search
key - the key to search for
Returns:
the index of the given key if it exists in the array, otherwise -1 times the index value at the insertion point that would be used if the key were added to the array.

binarySearch

public static final int binarySearch(java.lang.Object[] a,
                                     java.lang.Object key,
                                     int length)
Perform a binary search over a sorted range of an array for the given key. The range is assumed to start at index 0.

Parameters:
a - the array to search
key - the key to search for
length - the the length of the range to search over.
Returns:
the index of the given key if it exists in the array, otherwise -1 times the index value at the insertion point that would be used if the key were added to the array.

binarySearch

public static final int binarySearch(java.lang.Object[] a,
                                     java.lang.Object key,
                                     int begin,
                                     int end)
Perform a binary search over a sorted range of an array for the given key.

Parameters:
a - the array to search
key - the key to search for
begin - the starting index of the range
end - the ending index of the range, exclusive
Returns:
the index of the given key if it exists in the array, otherwise -1 times the index value at the insertion point that would be used if the key were added to the array.

binarySearch

public static final int binarySearch(java.lang.Object[] a,
                                     java.lang.Object key,
                                     java.util.Comparator<java.lang.Object> cp)
Perform a binary search over a sorted array for the given key.

Parameters:
a - the array to search
key - the key to search for
cp - the comparator to use to compare key values
Returns:
the index of the given key if it exists in the array, otherwise -1 times the index value at the insertion point that would be used if the key were added to the array.

binarySearch

public static final int binarySearch(java.lang.Object[] a,
                                     java.lang.Object key,
                                     java.util.Comparator<java.lang.Object> cp,
                                     int length)
Perform a binary search over a sorted range of an array for the given key. The range is assumed to start at index 0.

Parameters:
a - the array to search
key - the key to search for
cp - the comparator to use to compare key values
length - the the length of the range to search over.
Returns:
the index of the given key if it exists in the array, otherwise -1 times the index value at the insertion point that would be used if the key were added to the array.

binarySearch

public static final int binarySearch(java.lang.Object[] a,
                                     java.lang.Object key,
                                     java.util.Comparator<java.lang.Object> cp,
                                     int begin,
                                     int end)
Perform a binary search over a sorted range of an array for the given key.

Parameters:
a - the array to search
key - the key to search for
cp - the comparator to use to compare key values
begin - the starting index of the range
end - the ending index of the range, exclusive
Returns:
the index of the given key if it exists in the array, otherwise -1 times the index value at the insertion point that would be used if the key were added to the array.

find

public static final int find(int[] a,
                             int key)
Linearly search an array for a given key value.

Parameters:
a - the array to search
key - the key to search for
Returns:
the index of the first occurrence of the key in the array, of -1 if the key is not found.

find

public static final int find(int[] a,
                             int key,
                             int length)
Linearly search an array range for a given key value. Assumes that the range begins at index 0.

Parameters:
a - the array to search
key - the key to search for
length - the length of the range to search over
Returns:
the index of the first occurrence of the key in the array, of -1 if the key is not found.

find

public static final int find(int[] a,
                             int key,
                             int begin,
                             int end)
Linearly search an array range for a given key value.

Parameters:
a - the array to search
key - the key to search for
begin - the starting index of the range
end - the ending index of the range, exclusive
Returns:
the index of the first occurrence of the key in the array, of -1 if the key is not found.

resize

public static final int[] resize(int[] a,
                                 int size)
Resize the given array as needed to meet a target size.

Parameters:
a - the array to potentially resize
size - the minimum size of the target array
Returns:
the resized array, if the original array meets the size requirement, it is simply return, otherwise a new array is allocated and the contents of the original array are copied over.

resize

public static final float[] resize(float[] a,
                                   int size)
Resize the given array as needed to meet a target size.

Parameters:
a - the array to potentially resize
size - the minimum size of the target array
Returns:
the resized array, if the original array meets the size requirement, it is simply return, otherwise a new array is allocated and the contents of the original array are copied over.

resize

public static final double[] resize(double[] a,
                                    int size)
Resize the given array as needed to meet a target size.

Parameters:
a - the array to potentially resize
size - the minimum size of the target array
Returns:
the resized array, if the original array meets the size requirement, it is simply return, otherwise a new array is allocated and the contents of the original array are copied over.

resize

public static final java.lang.Object[] resize(java.lang.Object[] a,
                                              int size)
Resize the given array as needed to meet a target size.

Parameters:
a - the array to potentially resize
size - the minimum size of the target array
Returns:
the resized array, if the original array meets the size requirement, it is simply return, otherwise a new array is allocated and the contents of the original array are copied over.

trim

public static final int[] trim(int[] a,
                               int size)
Trims an array to be exactly the target a size.

Parameters:
a - the array to trim
size - the desired size of the array. This value must be lesser than or equal to the size of the input array.
Returns:
a trimmed array instance

trim

public static final float[] trim(float[] a,
                                 int size)
Trims an array to be exactly the target a size.

Parameters:
a - the array to trim
size - the desired size of the array. This value must be lesser than or equal to the size of the input array.
Returns:
a trimmed array instance

trim

public static final double[] trim(double[] a,
                                  int size)
Trims an array to be exactly the target a size.

Parameters:
a - the array to trim
size - the desired size of the array. This value must be lesser than or equal to the size of the input array.
Returns:
a trimmed array instance

trim

public static final java.lang.Object[] trim(java.lang.Object[] a,
                                            int size)
Trims an array to be exactly the target a size.

Parameters:
a - the array to trim
size - the desired size of the array. This value must be lesser than or equal to the size of the input array.
Returns:
a trimmed array instance

sort

public static final void sort(int[] a,
                              double[] b)
Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.

Parameters:
a - the array to sort by
b - the array to re-arrange based on the sort order of the first array.

sort

public static final void sort(int[] a,
                              double[] b,
                              int length)
Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.

Parameters:
a - the array to sort by
b - the array to re-arrange based on the sort order of the first array.
length - the array range length to sort over

sort

public static final void sort(int[] a,
                              double[] b,
                              int begin,
                              int end)
Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.

Parameters:
a - the array to sort by
b - the array to re-arrange based on the sort order of the first array.
begin - the start index of the range to sort
end - the end index, exclusive, of the range to sort

sort

public static final void sort(int[] a,
                              int[] b)
Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.

Parameters:
a - the array to sort by
b - the array to re-arrange based on the sort order of the first array.

sort

public static final void sort(int[] a,
                              int[] b,
                              int length)
Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.

Parameters:
a - the array to sort by
b - the array to re-arrange based on the sort order of the first array.
length - the array range length to sort over

sort

public static final void sort(int[] a,
                              int[] b,
                              int begin,
                              int end)
Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.

Parameters:
a - the array to sort by
b - the array to re-arrange based on the sort order of the first array.
begin - the start index of the range to sort
end - the end index, exclusive, of the range to sort

sort

public static final void sort(int[] a,
                              java.lang.Object[] b,
                              int begin,
                              int end)
Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.

Parameters:
a - the array to sort by
b - the array to re-arrange based on the sort order of the first array.
begin - the start index of the range to sort
end - the end index, exclusive, of the range to sort

sort

public static final void sort(int[] a,
                              java.lang.Object[] b,
                              int[] abuf,
                              java.lang.Object[] bbuf,
                              int begin,
                              int end)
Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.

Parameters:
a - the array to sort by
b - the array to re-arrange based on the sort order of the first array.
abuf - a buffer array to perform the sorting without allocating any additional memory
bbuf - a buffer array to perform the sorting without allocating any additional memory
begin - the start index of the range to sort
end - the end index, exclusive, of the range to sort

sort

public static final void sort(double[] a,
                              int[] b)
Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.

Parameters:
a - the array to sort by
b - the array to re-arrange based on the sort order of the first array.

sort

public static final void sort(double[] a,
                              int[] b,
                              int length)
Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.

Parameters:
a - the array to sort by
b - the array to re-arrange based on the sort order of the first array.
length - the length of the range to be sorted

sort

public static final void sort(double[] a,
                              int[] b,
                              int begin,
                              int end)
Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.

Parameters:
a - the array to sort by
b - the array to re-arrange based on the sort order of the first array.
begin - the start index of the range to sort
end - the end index, exclusive, of the range to sort

sort

public static final void sort(float[] a,
                              int[] b)
Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.

Parameters:
a - the array to sort by
b - the array to re-arrange based on the sort order of the first array.

sort

public static final void sort(float[] a,
                              int[] b,
                              int length)
Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.

Parameters:
a - the array to sort by
b - the array to re-arrange based on the sort order of the first array.
length - the length of the range to be sorted

sort

public static final void sort(float[] a,
                              int[] b,
                              int begin,
                              int end)
Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.

Parameters:
a - the array to sort by
b - the array to re-arrange based on the sort order of the first array.
begin - the start index of the range to sort
end - the end index, exclusive, of the range to sort

sort

public static final void sort(java.lang.Object[] a,
                              int[] b,
                              java.util.Comparator<java.lang.Object> cmp)
Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.

Parameters:
a - the array to sort by
b - the array to re-arrange based on the sort order of the first array.
cmp - the comparator to use to compare key values

sort

public static final void sort(java.lang.Object[] a,
                              int[] b,
                              int length,
                              java.util.Comparator<java.lang.Object> cmp)
Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.

Parameters:
a - the array to sort by
b - the array to re-arrange based on the sort order of the first array.
length - the length of the range to be sorted
cmp - the comparator to use to compare key values

sort

public static final void sort(java.lang.Object[] a,
                              int[] b,
                              int begin,
                              int end,
                              java.util.Comparator<java.lang.Object> cmp)
Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.

Parameters:
a - the array to sort by
b - the array to re-arrange based on the sort order of the first array.
begin - the start index of the range to sort
end - the end index, exclusive, of the range to sort
cmp - the comparator to use to compare key values

getIntArray

public static int[] getIntArray(java.lang.String filename)
Read in a text file as an array of integers. Uses the default java StringTokenizer to segment the text file. Additionally, tokens beginning with the '#' character are ignored.

Parameters:
filename - the name of the file to read in
Returns:
an array of integers parsed from the file