Package org.opensourcephysics.numerics
Class ArrayLib
java.lang.Object
org.opensourcephysics.numerics.ArrayLib
Library of supplementary array routines not
supported by the java.util.Arrays class.
- Author:
- jeffrey heer
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic intstatic intstatic final intArrays with lengths beneath this value will be insertion sorted. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic final intbinarySearch(int[] a, int key) Perform a binary search over a sorted array for the given key.static final intbinarySearch(int[] a, int key, int length) Perform a binary search over a sorted range of an array for the given key.static final intbinarySearch(int[] a, int key, int begin, int end) Perform a binary search over a sorted range of an array for the given key.static final intbinarySearch(Object[] a, Object key) Perform a binary search over a sorted array for the given key.static final intbinarySearch(Object[] a, Object key, int length) Perform a binary search over a sorted range of an array for the given key.static final intbinarySearch(Object[] a, Object key, int begin, int end) Perform a binary search over a sorted range of an array for the given key.static final intbinarySearch(Object[] a, Object key, Comparator<Object> cp) Perform a binary search over a sorted array for the given key.static final intbinarySearch(Object[] a, Object key, Comparator<Object> cp, int length) Perform a binary search over a sorted range of an array for the given key.static final intbinarySearch(Object[] a, Object key, Comparator<Object> cp, int begin, int end) Perform a binary search over a sorted range of an array for the given key.static final intfind(int[] a, int key) Linearly search an array for a given key value.static final intfind(int[] a, int key, int length) Linearly search an array range for a given key value.static final intfind(int[] a, int key, int begin, int end) Linearly search an array range for a given key value.static final intstatic final intstatic int[]getIntArray(String filename) Read in a text file as an array of integers.protected static final voidinsertionsort(double[] a, int[] b, int p, int r) protected static final voidinsertionsort(float[] a, int[] b, int p, int r) protected static final voidinsertionsort(int[] a, double[] b, int p, int r) protected static final voidinsertionsort(int[] a, int[] b, int p, int r) protected static final voidinsertionsort(int[] a, Object[] b, int p, int r) protected static final voidinsertionsort(Object[] a, int[] b, int p, int r, Comparator<Object> cmp) static final doublemax(double[] a) Find the maximum value in an array.protected static final voidmerge(double[] a, int[] b, int p, int q, int r) protected static final voidmerge(float[] a, int[] b, int p, int q, int r) protected static final voidmerge(int[] a, double[] b, int p, int q, int r) protected static final voidmerge(int[] a, int[] b, int p, int q, int r) protected static final voidprotected static final voidmerge(Object[] a, int[] b, int p, int q, int r, Comparator<Object> cmp) protected static final voidmergesort(double[] a, int[] b, int p, int r) protected static final voidmergesort(float[] a, int[] b, int p, int r) protected static final voidmergesort(int[] a, double[] b, int p, int r) protected static final voidmergesort(int[] a, int[] b, int p, int r) protected static voidprotected static final voidmergesort(Object[] a, int[] b, int p, int r, Comparator<Object> cmp) static final doublemin(double[] a) Find the minimum value in an array.static final double[]resize(double[] a, int size) Resize the given array as needed to meet a target size.static final float[]resize(float[] a, int size) Resize the given array as needed to meet a target size.static final int[]resize(int[] a, int size) Resize the given array as needed to meet a target size.static final Object[]Resize the given array as needed to meet a target size.static final voidsort(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 final voidsort(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 final voidsort(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 final voidsort(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 final voidsort(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 final voidsort(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 final voidsort(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 final voidsort(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 final voidsort(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 final voidsort(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 final voidsort(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 final voidsort(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 final voidSort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.static final voidSort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.static final voidsort(Object[] a, int[] b, int begin, int end, Comparator<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 final voidsort(Object[] a, int[] b, int length, Comparator<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 final voidsort(Object[] a, int[] b, Comparator<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 final doublesum(double[] a) Compute the sum of the values in an array.static final double[]trim(double[] a, int size) Trims an array to be exactly the target a size.static final float[]trim(float[] a, int size) Trims an array to be exactly the target a size.static final int[]trim(int[] a, int size) Trims an array to be exactly the target a size.static final Object[]Trims an array to be exactly the target a size.
-
Field Details
-
IndexMax
public static int IndexMax -
IndexMin
public static int IndexMin -
SORT_THRESHOLD
public static final int SORT_THRESHOLDArrays with lengths beneath this value will be insertion sorted.- See Also:
-
-
Constructor Details
-
ArrayLib
public ArrayLib()
-
-
Method Details
-
max
public static final double max(double[] a) Find the maximum value in an array.- Parameters:
a- the arrayindMax- 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 arrayindMin- 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 searchkey- 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 searchkey- the key to search forlength- 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 searchkey- the key to search forbegin- the starting index of the rangeend- 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
Perform a binary search over a sorted array for the given key.- Parameters:
a- the array to searchkey- 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
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 searchkey- the key to search forlength- 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
Perform a binary search over a sorted range of an array for the given key.- Parameters:
a- the array to searchkey- the key to search forbegin- the starting index of the rangeend- 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
Perform a binary search over a sorted array for the given key.- Parameters:
a- the array to searchkey- the key to search forcp- 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
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 searchkey- the key to search forcp- the comparator to use to compare key valueslength- 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(Object[] a, Object key, Comparator<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 searchkey- the key to search forcp- the comparator to use to compare key valuesbegin- the starting index of the rangeend- 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 searchkey- 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 searchkey- the key to search forlength- 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 searchkey- the key to search forbegin- the starting index of the rangeend- 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 resizesize- 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 resizesize- 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 resizesize- 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
Resize the given array as needed to meet a target size.- Parameters:
a- the array to potentially resizesize- 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 trimsize- 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 trimsize- 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 trimsize- 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
Trims an array to be exactly the target a size.- Parameters:
a- the array to trimsize- 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 byb- 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 byb- 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 byb- the array to re-arrange based on the sort order of the first array.begin- the start index of the range to sortend- the end index, exclusive, of the range to sort
-
insertionsort
protected static final void insertionsort(int[] a, double[] b, int p, int r) -
mergesort
protected static final void mergesort(int[] a, double[] b, int p, int r) -
merge
protected static final void merge(int[] a, double[] b, int p, int q, int r) -
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 byb- 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 byb- 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 byb- the array to re-arrange based on the sort order of the first array.begin- the start index of the range to sortend- the end index, exclusive, of the range to sort
-
insertionsort
protected static final void insertionsort(int[] a, int[] b, int p, int r) -
mergesort
protected static final void mergesort(int[] a, int[] b, int p, int r) -
merge
protected static final void merge(int[] a, int[] b, int p, int q, int r) -
sort
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 byb- the array to re-arrange based on the sort order of the first array.begin- the start index of the range to sortend- the end index, exclusive, of the range to sort
-
sort
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 byb- 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 memorybbuf- a buffer array to perform the sorting without allocating any additional memorybegin- the start index of the range to sortend- the end index, exclusive, of the range to sort
-
insertionsort
-
mergesort
-
merge
-
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 byb- 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 byb- 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 byb- the array to re-arrange based on the sort order of the first array.begin- the start index of the range to sortend- the end index, exclusive, of the range to sort
-
insertionsort
protected static final void insertionsort(double[] a, int[] b, int p, int r) -
mergesort
protected static final void mergesort(double[] a, int[] b, int p, int r) -
merge
protected static final void merge(double[] a, int[] b, int p, int q, int r) -
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 byb- 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 byb- 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 byb- the array to re-arrange based on the sort order of the first array.begin- the start index of the range to sortend- the end index, exclusive, of the range to sort
-
insertionsort
protected static final void insertionsort(float[] a, int[] b, int p, int r) -
mergesort
protected static final void mergesort(float[] a, int[] b, int p, int r) -
merge
protected static final void merge(float[] a, int[] b, int p, int q, int r) -
sort
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 byb- the array to re-arrange based on the sort order of the first array.cmp- the comparator to use to compare key values
-
sort
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 byb- the array to re-arrange based on the sort order of the first array.length- the length of the range to be sortedcmp- the comparator to use to compare key values
-
sort
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 byb- the array to re-arrange based on the sort order of the first array.begin- the start index of the range to sortend- the end index, exclusive, of the range to sortcmp- the comparator to use to compare key values
-
insertionsort
protected static final void insertionsort(Object[] a, int[] b, int p, int r, Comparator<Object> cmp) -
mergesort
-
merge
-
getIntArray
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
-