public class Arrays
extends java.lang.Object
If you want to calculate aggregating functions like sum, min, max etc
on multiple values which are not part of an array,
it's easier to use the functions from the Lists
class.
Note that none of these functions will calculate statistical functions over a whole column of a table.
The functions fall into a number of categories:
size
,
count
,
countTrue
,
maximum
,
minimum
,
sum
,
mean
,
median
,
quantile
,
stdev
,
variance
,
join
.
add
,
subtract
,
multiply
,
divide
,
reciprocal
,
condition
,
slice
,
pick
.
Mostly these work on any numeric array type and return
floating point (double precision) values,
but some of them (slice
, pick
)
have variants for different array types.
array
,
which lets you assemble a floating point array value from
a list of scalar numbers.
There are variants (intArray
, stringArray
)
for some different array types.
Modifier and Type | Method | Description |
---|---|---|
static double[] |
add(java.lang.Object array,
double constant) |
Returns the result of adding a constant value to every element of
a numeric array.
|
static double[] |
add(java.lang.Object array1,
java.lang.Object array2) |
Returns the result of adding two numeric arrays element by element.
|
static double[] |
array(double... values) |
Returns a floating point numeric array built from the given arguments.
|
static double[] |
condition(boolean[] flagArray,
double trueValue,
double falseValue) |
Maps a boolean array to a numeric array by using supplied numeric
values to represent true and false values from the input array.
|
static int |
count(java.lang.Object array) |
Returns the number of non-blank elements in the array.
|
static int |
countTrue(boolean[] array) |
Returns the number of true elements in an array of boolean values.
|
static double[] |
divide(java.lang.Object array1,
java.lang.Object array2) |
Returns the result of dividing two numeric arrays element by element.
|
static int[] |
intArray(int... values) |
Returns an integer numeric array built from the given arguments.
|
static java.lang.String |
join(java.lang.Object array,
java.lang.String joiner) |
Returns a string composed of concatenating all the elements of an
array, separated by a joiner string.
|
static double |
maximum(java.lang.Object array) |
Returns the largest of the non-blank elements in the array.
|
static double |
mean(java.lang.Object array) |
Returns the mean of all the non-blank elements in the array.
|
static double |
median(java.lang.Object array) |
Returns the median of the non-blank elements in the array.
|
static double |
minimum(java.lang.Object array) |
Returns the smallest of the non-blank elements in the array.
|
static double[] |
multiply(java.lang.Object array,
double constant) |
Returns the result of multiplying every element of a numeric array
by a constant value.
|
static double[] |
multiply(java.lang.Object array1,
java.lang.Object array2) |
Returns the result of multiplying two numeric arrays element by element.
|
static byte[] |
pick(byte[] array,
int... indices) |
|
static double[] |
pick(double[] array,
int... indices) |
Returns a selection of elements from a given array.
|
static float[] |
pick(float[] array,
int... indices) |
|
static int[] |
pick(int[] array,
int... indices) |
|
static long[] |
pick(long[] array,
int... indices) |
|
static short[] |
pick(short[] array,
int... indices) |
|
static java.lang.Object[] |
pick(java.lang.Object[] array,
int... indices) |
|
static java.lang.String[] |
pick(java.lang.String[] array,
int... indices) |
|
static double |
quantile(java.lang.Object array,
double quant) |
Returns a quantile value of the non-blank elements in the array.
|
static double[] |
reciprocal(java.lang.Object array) |
Returns the result of taking the reciprocal of every element of
a numeric array.
|
static int |
size(java.lang.Object array) |
Returns the number of elements in the array.
|
static byte[] |
slice(byte[] array,
int i0,
int i1) |
|
static double[] |
slice(double[] array,
int i0,
int i1) |
Returns a sub-sequence of values from a given array.
|
static float[] |
slice(float[] array,
int i0,
int i1) |
|
static int[] |
slice(int[] array,
int i0,
int i1) |
|
static long[] |
slice(long[] array,
int i0,
int i1) |
|
static short[] |
slice(short[] array,
int i0,
int i1) |
|
static java.lang.Object[] |
slice(java.lang.Object[] array,
int i0,
int i1) |
|
static java.lang.String[] |
slice(java.lang.String[] array,
int i0,
int i1) |
|
static double |
stdev(java.lang.Object array) |
Returns the population standard deviation of all the non-blank elements
in the array.
|
static java.lang.String[] |
stringArray(java.lang.String... values) |
Returns a String array built from the given arguments.
|
static double[] |
subtract(java.lang.Object array1,
java.lang.Object array2) |
Returns the result of subtracting one numeric array from the other
element by element.
|
static double |
sum(java.lang.Object array) |
Returns the sum of all the non-blank elements in the array.
|
static double |
variance(java.lang.Object array) |
Returns the population variance of all the non-blank elements
in the array.
|
public static double sum(java.lang.Object array)
array
is not a numeric array, null
is returned.array
- array of numbersarray
public static double mean(java.lang.Object array)
array
is not a numeric array, null
is returned.array
- array of numbersarray
public static double variance(java.lang.Object array)
array
is not a numeric array,
null
is returned.array
- array of numbersarray
public static double stdev(java.lang.Object array)
array
is not a numeric array,
null
is returned.array
- array of numbersarray
public static double minimum(java.lang.Object array)
array
is not a numeric array, null
is returned.array
- array of numbersarray
public static double maximum(java.lang.Object array)
array
is not a numeric array, null
is returned.array
- array of numbersarray
public static double median(java.lang.Object array)
array
is not a numeric array, null
is returned.array
- array of numbersarray
public static double quantile(java.lang.Object array, double quant)
quant
value;
values of 0, 0.5 and 1 give the minimum, median and maximum
respectively. A value of 0.99 would give the 99th percentile.array
- array of numbersquant
- number in the range 0-1 deterining which quantile
to calculatequant
public static int size(java.lang.Object array)
array
is not an array, zero is returned.array
- arrayarray
public static int count(java.lang.Object array)
array
is not an array, zero is returned.array
- array (may or may not be numeric)array
public static int countTrue(boolean[] array)
array
- array of true/false valuesarray
public static java.lang.String join(java.lang.Object array, java.lang.String joiner)
array
is not an array, null is returned.array
- array of numbers or stringsjoiner
- text string to interpose between adjacent elementsarray
elements separated by
joiner
stringsjoin(array(1.5,2.1,-3.9), "; ") = "1.5; 2.1; -3.9"
public static double[] add(java.lang.Object array1, java.lang.Object array2)
null
is returned.
The types of the arrays do not need to be the same,
so for example it is permitted to add an integer array
to a floating point array.array1
- first array of numeric valuesarray2
- second array of numeric valuesarray1
and array2
,
the same length as the input arraysadd(array(1,2,3), array(0.1,0.2,0.3))
= [1.1, 2.2, 3.3]
public static double[] add(java.lang.Object array, double constant)
array
argument is not a numeric array,
null
is returned.array
- array inputconstant
- value to add to each array elementarray
parameteradd(array(1,2,3), 10) = [11,12,13]
public static double[] subtract(java.lang.Object array1, java.lang.Object array2)
null
is returned.
The types of the arrays do not need to be the same,
so for example it is permitted to subtract an integer array
from a floating point array.array1
- first array of numeric valuesarray2
- second array of numeric valuesarray1
and array2
,
the same length as the input arrayssubtract(array(1,2,3), array(0.1,0.2,0.3))
= [0.9, 1.8, 2.7]
public static double[] multiply(java.lang.Object array1, java.lang.Object array2)
null
is returned.
The types of the arrays do not need to be the same,
so for example it is permitted to multiply an integer array
by a floating point array.array1
- first array of numeric valuesarray2
- second array of numeric valuesarray1
and array2
,
the same length as the input arraysmultiply(array(1,2,3), array(2,4,6)) = [2, 8, 18]
public static double[] multiply(java.lang.Object array, double constant)
array
argument is not a numeric array,
null
is returned.array
- array inputconstant
- value by which to multiply each array elementarray
parametermultiply(array(1,2,3), 2) = [2, 4, 6]
public static double[] divide(java.lang.Object array1, java.lang.Object array2)
null
is returned.
The types of the arrays do not need to be the same,
so for example it is permitted to divide an integer array
by a floating point array.array1
- array of numerator values (numeric)array2
- array of denominator values (numeric)array1[i]/array2[i]
the same length as the input arraysdivide(array(0,9,4), array(1,3,8)) = [0, 3, 0.5]
public static double[] reciprocal(java.lang.Object array)
array
argument is not a numeric array,
null
is returned.array
- array inputarray
parameterreciprocal(array(1,2,0.25) = [1, 0.5, 4]
public static double[] condition(boolean[] flagArray, double trueValue, double falseValue)
This has the same effect as applying the expression
outArray[i] = flagArray[i] ? trueValue : falseValue
.
flagArray
- array of boolean valuestrueValue
- output value corresponding to an input true valuefalseValue
- output value corresponding to an input false valueflagArray
condition([true, false, true], 1, 0) = [1, 0, 1]
public static double[] slice(double[] array, int i0, int i1)
The semantics are like python array slicing, though both limits
have to be specified: the output array contains the sequence of
elements in the input array from i0
(inclusive)
to i1
(exclusive). If a negative value is given
in either case, it is added to the length of the input array,
so that -1 indicates the last element of the input array.
The indices are capped at 0 and the input array length respectively,
so a large positive value may be used to indicate the end of the array.
If the end index is less than or equal to the start index,
a zero-length array is returned.
Note:
This documents the double-precision version of the routine.
Corresponding routines exist for other data types
(float
, long
, int
,
short
, byte
, String
,
Object
).
array
- input arrayi0
- index of first element, inclusive
(may be negative to count back from the end)i1
- index of the last element, exclusive
(may be negative to count back from the end)i0
and i1
slice(array(10,11,12,13), 0, 3) = [10, 11, 12]
, slice(array(10,11,12,13), -2, 999) = [12, 13]
@HideDoc public static float[] slice(float[] array, int i0, int i1)
@HideDoc public static long[] slice(long[] array, int i0, int i1)
@HideDoc public static int[] slice(int[] array, int i0, int i1)
@HideDoc public static short[] slice(short[] array, int i0, int i1)
@HideDoc public static byte[] slice(byte[] array, int i0, int i1)
@HideDoc public static java.lang.String[] slice(java.lang.String[] array, int i0, int i1)
@HideDoc public static java.lang.Object[] slice(java.lang.Object[] array, int i0, int i1)
public static double[] pick(double[] array, int... indices)
The output array consists of one element selected from the input array for each of the supplied index values. If a negative value is supplied for an index value, it is added to the input array length, so that -1 indicates the last element of the input array. If the input array is null, null is returned. If any of the index values is out of the range of the extent of the input array, an error results.
Note:
This documents the double-precision version of the routine.
Corresponding routines exist for other data types
(float
, long
, int
,
short
, byte
, String
,
Object
).
array
- input arrayindices
- one or more index into the input array
(may be negative to count back from the end)indices
pick(array(10,11,12,13), 0, 3) = [10, 13]
, pick(array(10,11,12,13), -1, -2, -3)
= [13, 12, 11]
@HideDoc public static float[] pick(float[] array, int... indices)
@HideDoc public static long[] pick(long[] array, int... indices)
@HideDoc public static int[] pick(int[] array, int... indices)
@HideDoc public static short[] pick(short[] array, int... indices)
@HideDoc public static byte[] pick(byte[] array, int... indices)
@HideDoc public static java.lang.String[] pick(java.lang.String[] array, int... indices)
@HideDoc public static java.lang.Object[] pick(java.lang.Object[] array, int... indices)
public static double[] array(double... values)
values
- one or more array elementspublic static int[] intArray(int... values)
values
- one or more array elementspublic static java.lang.String[] stringArray(java.lang.String... values)
values
- one or more array elementsCopyright © 2018 Central Laboratory of the Research Councils. All Rights Reserved.