public class Arrays extends 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 | Field and Description |
---|---|
static String |
ARRAY_ELEMENT_VARNAME
Array element variable name in arrayFunc expressions.
|
static String |
ARRAY_INDEX_VARNAME
Array index variable name in arrayFunc expressions.
|
Modifier and Type | Method and Description |
---|---|
static double[] |
add(Object arrayOrScalar1,
Object arrayOrScalar2)
Returns the element-by-element result of adding
either two numeric arrays of the same length,
or an array and a scalar considered as if an array of the right length.
|
static double[] |
array(double... values)
Returns a floating point numeric array built from the given arguments.
|
static double[] |
arrayFunc(String expr,
Object inArray)
Returns a floating-point array resulting from applying a given
function expression element-by-element to an input array.
|
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 byte[] |
constant(int n,
byte value) |
static double[] |
constant(int n,
double value)
Returns a fixed-size array filled with a given constant value.
|
static float[] |
constant(int n,
float value) |
static int[] |
constant(int n,
int value) |
static long[] |
constant(int n,
long value) |
static short[] |
constant(int n,
short value) |
static int |
count(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(Object arrayOrScalar1,
Object arrayOrScalar2)
Returns the element-by-element result of dividing
either two numeric arrays of the same length,
or an array and a scalar considered as if an array of the right length.
|
static double |
dotProduct(Object array1,
Object array2)
Returns the dot (scalar) product of two numeric arrays.
|
static int |
indexOf(double[] array,
double item) |
static int |
indexOf(float[] array,
double item) |
static int |
indexOf(int[] array,
int item) |
static int |
indexOf(long[] array,
long item) |
static int |
indexOf(Object[] array,
Object item)
Returns the position in a supplied array at which a given item appears.
|
static int |
indexOf(short[] array,
int item) |
static int[] |
intArray(int... values)
Returns an integer numeric array built from the given arguments.
|
static int[] |
intArrayFunc(String expr,
Object inArray)
Returns an integer array resulting from applying a given
function expression element-by-element to an input array.
|
static String |
join(Object array,
String joiner)
Returns a string composed of concatenating all the elements of an
array, separated by a joiner string.
|
static double[] |
loop(double start,
double end,
double step)
Returns a floating point array like the values taken in a for-loop
with given start, end, and step arguments.
|
static int[] |
loop(int start,
int end)
Returns an integer array like the values taken in a for-loop
with given start and end elements and a step of 1.
|
static double |
maximum(Object array)
Returns the largest of the non-blank elements in the array.
|
static double |
mean(Object array)
Returns the mean of all the non-blank elements in the array.
|
static double |
median(Object array)
Returns the median of the non-blank elements in the array.
|
static double |
minimum(Object array)
Returns the smallest of the non-blank elements in the array.
|
static double[] |
multiply(Object arrayOrScalar1,
Object arrayOrScalar2)
Returns the element-by-element result of multiplying
either two numeric arrays of the same length,
or an array and a scalar considered as if an array of the right length.
|
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 Object[] |
pick(Object[] array,
int... indices) |
static short[] |
pick(short[] array,
int... indices) |
static String[] |
pick(String[] array,
int... indices) |
static double |
quantile(Object array,
double quant)
Returns a quantile value of the non-blank elements in the array.
|
static double[] |
reciprocal(Object array)
Returns the result of taking the reciprocal of every element of
a numeric array.
|
static int[] |
sequence(int n)
Returns an integer array of a given length with the values
0, 1, 2, ....
|
static double[] |
sequence(int n,
double start,
double step)
Returns a floating point array of a given length with values starting
at a given value and increasing with a given increment.
|
static int |
size(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 Object[] |
slice(Object[] array,
int i0,
int i1) |
static short[] |
slice(short[] array,
int i0,
int i1) |
static String[] |
slice(String[] array,
int i0,
int i1) |
static double |
stdev(Object array)
Returns the population standard deviation of all the non-blank elements
in the array.
|
static String[] |
stringArray(String... values)
Returns a String array built from the given arguments.
|
static double[] |
subtract(Object arrayOrScalar1,
Object arrayOrScalar2)
Returns the element-by-element result of subtracting
either two numeric arrays of the same length,
or an array and a scalar considered as if an array of the right length.
|
static double |
sum(Object array)
Returns the sum of all the non-blank elements in the array.
|
static Object |
untypedArrayFunc(String expr,
Object inArray)
Returns an untyped array resulting from applying a given
function expression element-by-element to an input array.
|
static double |
variance(Object array)
Returns the population variance of all the non-blank elements
in the array.
|
@HideDoc public static final String ARRAY_ELEMENT_VARNAME
@HideDoc public static final String ARRAY_INDEX_VARNAME
public static double sum(Object array)
array
is not a numeric array, null
is returned.array
- array of numbersarray
public static double mean(Object array)
array
is not a numeric array, null
is returned.array
- array of numbersarray
public static double variance(Object array)
array
is not a numeric array,
null
is returned.array
- array of numbersarray
public static double stdev(Object array)
array
is not a numeric array,
null
is returned.array
- array of numbersarray
public static double minimum(Object array)
array
is not a numeric array, null
is returned.array
- array of numbersarray
public static double maximum(Object array)
array
is not a numeric array, null
is returned.array
- array of numbersarray
public static double median(Object array)
array
is not a numeric array, null
is returned.array
- array of numbersarray
public static double quantile(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(Object array)
array
is not an array, zero is returned.array
- arrayarray
public static int count(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 String join(Object array, 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
stringspublic static double dotProduct(Object array1, Object array2)
array1
- first arrayarray2
- second arraypublic static double[] add(Object arrayOrScalar1, Object arrayOrScalar2)
If the arguments are not as expected (e.g. arrays of different lengths, both scalars, not numeric) then null is returned.
arrayOrScalar1
- first numeric array/scalararrayOrScalar2
- second numeric array/scalararrayOrScalar1 + arrayOrScalar2
,
the same length as the input array(s)public static double[] subtract(Object arrayOrScalar1, Object arrayOrScalar2)
If the arguments are not as expected (e.g. arrays of different lengths, both scalars, not numeric) then null is returned.
arrayOrScalar1
- first numeric array/scalararrayOrScalar2
- second numeric array/scalararrayOrScalar1 - arrayOrScalar2
,
the same length as the input array(s)public static double[] multiply(Object arrayOrScalar1, Object arrayOrScalar2)
If the arguments are not as expected (e.g. arrays of different lengths, both scalars, not numeric) then null is returned.
arrayOrScalar1
- first numeric array/scalararrayOrScalar2
- second numeric array/scalararrayOrScalar1 * arrayOrScalar2
,
the same length as the input array(s)public static double[] divide(Object arrayOrScalar1, Object arrayOrScalar2)
If the arguments are not as expected (e.g. arrays of different lengths, both scalars, not numeric) then null is returned.
arrayOrScalar1
- first numeric array/scalararrayOrScalar2
- second numeric array/scalararrayOrScalar1 / arrayOrScalar2
,
the same length as the input array(s)public static double[] reciprocal(Object array)
array
argument is not a numeric array,
null
is returned.array
- array inputarray
parameterpublic 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
public static double[] constant(int n, double value)
Note:
This documents the double-precision version of the routine.
Corresponding routines exist for other data types
(float
, long
, int
,
short
, byte
).
n
- size of output arrayvalue
- value of every element in the output arrayn
-element array with every element set to
value
@HideDoc public static float[] constant(int n, float value)
@HideDoc public static byte[] constant(int n, byte value)
@HideDoc public static short[] constant(int n, short value)
@HideDoc public static int[] constant(int n, int value)
@HideDoc public static long[] constant(int n, long value)
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
@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)
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
@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)
public static double[] arrayFunc(String expr, Object inArray)
The supplied expression can use the variable "x
"
to refer to the corresponding element of the input array, and
"i
"
to refer to its (zero-based) index.
The various functions and operators from the expression language
can all be used, but it is currently not possible
to reference other table column values.
If there is an error in the expression, a blank value (not an array) will be returned.
expr
- expression mapping input to output array valuesinArray
- input arrayinArray
, or null for a bad expr
public static int[] intArrayFunc(String expr, Object inArray)
The supplied expression can use the variable "x
"
to refer to the corresponding element of the input array, and
"i
"
to refer to its (zero-based) index.
The various functions and operators from the expression language
can all be used, but it is currently not possible
to reference other table column values.
If there is an error in the expression, a blank value (not an array) will be returned.
expr
- expression mapping input to output array valuesinArray
- input arrayinArray
, or null for a bad expr
@HideDoc public static Object untypedArrayFunc(String expr, Object inArray)
expr
- expression mapping input to output array valuesinArray
- input arrayinArray
, or null for a bad expr
public static int indexOf(Object[] array, Object item)
item
is the first entry in the array
, the return value
will be zero.
If the item does not appear in the array, -1 is returned. If it appears multiple times, the index of its first appearance is returned.
If indexOf(array, item)==n
, then
array[n]
is equal to item
.
Note:
This documents the Object
version of the routine.
Corresponding routines exist for other data types
(double
, float
, long
,
int
, short
).
array
- array which may contain the supplied itemitem
- entry to look for in the arrayitem
in array
,
or -1@HideDoc public static int indexOf(double[] array, double item)
@HideDoc public static int indexOf(float[] array, double item)
@HideDoc public static int indexOf(long[] array, long item)
@HideDoc public static int indexOf(int[] array, int item)
@HideDoc public static int indexOf(short[] array, int item)
public static int[] sequence(int n)
See also the loop
functions, which provide
similar functionality.
n
- length of arrayn
-element array,
(0, 1, 2, ... n
-1)public static double[] sequence(int n, double start, double step)
See also the loop
functions, which provide
similar functionality.
n
- length of arraystart
- value of first elementstep
- increment to apply to each elementn
-element array,
(start, start+step, start+2*step, ... start+(n-1)*step)public static int[] loop(int start, int end)
for (int x = start; x < end; x++)
If you want a floating point array, or one with a non-unit step,
you can use the three-parameter version of
the loop
function.
See also the sequence
functions.
start
- value for first element of output arrayend
- value one greater than last element of output arrayend-start
(or 0) elements
(start, start+1, start+2, ..., end-1)
public static double[] loop(double start, double end, double step)
for (double x = start; x < end; x += step)
Note that numerical precision issues may result in the output
array differing from its expected length by 1
(it is generally risky to rely on exact comparison of
floating point values).
If you want to be sure of the number of elements you can use
the sequence
function instead.
start
- value for first element of output arrayend
- first value beyond last element of output arraystep
- increment between output array values; may not be zero(end-start)/step
(or 0) elementspublic static double[] array(double... values)
values
- one or more array elementspublic static int[] intArray(int... values)
values
- one or more array elementsCopyright © 2024 Central Laboratory of the Research Councils. All Rights Reserved.