Class Arrays


  • public class Arrays
    extends java.lang.Object
    Functions which operate on array-valued cells. The array parameters of these functions can only be used on values which are already arrays (usually, numeric arrays). In most cases that means on values in table columns which are declared as array-valued. FITS and VOTable tables can have columns which contain array values, but other formats such as CSV cannot.

    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:

    • Aggregating operations, which map an array value to a scalar, including size, count, countTrue, maximum, minimum, sum, mean, median, quantile, stdev, variance, join.
    • Operations on one or more arrays which produce array results, including 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.
    • The function 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.
    Since:
    14 Jul 2008
    Author:
    Mark Taylor
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String ARRAY_ELEMENT_VARNAME
      Array element variable name in arrayFunc expressions.
      static java.lang.String ARRAY_INDEX_VARNAME
      Array index variable name in arrayFunc expressions.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static double[] add​(java.lang.Object arrayOrScalar1, java.lang.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​(java.lang.String expr, java.lang.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​(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 arrayOrScalar1, java.lang.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​(java.lang.Object array1, java.lang.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​(short[] array, int item)  
      static int indexOf​(java.lang.Object[] array, java.lang.Object item)
      Returns the position in a supplied array at which a given item appears.
      static int[] intArray​(int... values)
      Returns an integer numeric array built from the given arguments.
      static int[] intArrayFunc​(java.lang.String expr, java.lang.Object inArray)
      Returns an integer array resulting from applying a given function expression element-by-element to an input array.
      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 arrayOrScalar1, java.lang.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 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[] 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 double array of a given length with values starting at a given value and increasing with a given increment.
      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 arrayOrScalar1, java.lang.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​(java.lang.Object array)
      Returns the sum of all the non-blank elements in the array.
      static java.lang.Object untypedArrayFunc​(java.lang.String expr, java.lang.Object inArray)
      Returns an untyped array resulting from applying a given function expression element-by-element to an input array.
      static double variance​(java.lang.Object array)
      Returns the population variance of all the non-blank elements in the array.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • ARRAY_ELEMENT_VARNAME

        @HideDoc
        public static final java.lang.String ARRAY_ELEMENT_VARNAME
        Array element variable name in arrayFunc expressions.
        See Also:
        Constant Field Values
      • ARRAY_INDEX_VARNAME

        @HideDoc
        public static final java.lang.String ARRAY_INDEX_VARNAME
        Array index variable name in arrayFunc expressions.
        See Also:
        Constant Field Values
    • Method Detail

      • sum

        public static double sum​(java.lang.Object array)
        Returns the sum of all the non-blank elements in the array. If array is not a numeric array, null is returned.
        Parameters:
        array - array of numbers
        Returns:
        sum of all the numeric values in array
      • mean

        public static double mean​(java.lang.Object array)
        Returns the mean of all the non-blank elements in the array. If array is not a numeric array, null is returned.
        Parameters:
        array - array of numbers
        Returns:
        mean of all the numeric values in array
      • variance

        public static double variance​(java.lang.Object array)
        Returns the population variance of all the non-blank elements in the array. If array is not a numeric array, null is returned.
        Parameters:
        array - array of numbers
        Returns:
        variance of the numeric values in array
      • stdev

        public static double stdev​(java.lang.Object array)
        Returns the population standard deviation of all the non-blank elements in the array. If array is not a numeric array, null is returned.
        Parameters:
        array - array of numbers
        Returns:
        standard deviation of the numeric values in array
      • minimum

        public static double minimum​(java.lang.Object array)
        Returns the smallest of the non-blank elements in the array. If array is not a numeric array, null is returned.
        Parameters:
        array - array of numbers
        Returns:
        minimum of the numeric values in array
      • maximum

        public static double maximum​(java.lang.Object array)
        Returns the largest of the non-blank elements in the array. If array is not a numeric array, null is returned.
        Parameters:
        array - array of numbers
        Returns:
        maximum of the numeric values in array
      • median

        public static double median​(java.lang.Object array)
        Returns the median of the non-blank elements in the array. If array is not a numeric array, null is returned.
        Parameters:
        array - array of numbers
        Returns:
        median of the numeric values in array
      • quantile

        public static double quantile​(java.lang.Object array,
                                      double quant)
        Returns a quantile value of the non-blank elements in the array. Which quantile is determined by the 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.
        Parameters:
        array - array of numbers
        quant - number in the range 0-1 deterining which quantile to calculate
        Returns:
        quantile corresponding to quant
      • size

        public static int size​(java.lang.Object array)
        Returns the number of elements in the array. If array is not an array, zero is returned.
        Parameters:
        array - array
        Returns:
        size of array
      • count

        public static int count​(java.lang.Object array)
        Returns the number of non-blank elements in the array. If array is not an array, zero is returned.
        Parameters:
        array - array (may or may not be numeric)
        Returns:
        number of non-blank elements in array
      • countTrue

        public static int countTrue​(boolean[] array)
        Returns the number of true elements in an array of boolean values.
        Parameters:
        array - array of true/false values
        Returns:
        number of true values in array
      • join

        public 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. If array is not an array, null is returned.
        Parameters:
        array - array of numbers or strings
        joiner - text string to interpose between adjacent elements
        Returns:
        string composed of array elements separated by joiner strings
        Examples:
        join(array(1.5,2.1,-3.9), "; ") = "1.5; 2.1; -3.9"
      • dotProduct

        public static double dotProduct​(java.lang.Object array1,
                                        java.lang.Object array2)
        Returns the dot (scalar) product of two numeric arrays. If either argument is not an array, or if the arrays are not of the same length, a blank value is returned.
        Parameters:
        array1 - first array
        array2 - second array
        Returns:
        sum of element-wise products of input arrays
        Examples:
        dotProduct(array(3,4,5), array(1,2,3)) = 26
      • add

        public static double[] add​(java.lang.Object arrayOrScalar1,
                                   java.lang.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.

        If the arguments are not as expected (e.g. arrays of different lengths, both scalars, not numeric) then null is returned.

        Parameters:
        arrayOrScalar1 - first numeric array/scalar
        arrayOrScalar2 - second numeric array/scalar
        Returns:
        element-by-element result of arrayOrScalar1 + arrayOrScalar2, the same length as the input array(s)
        Examples:
        add(array(1,2,3), array(0.1,0.2,0.3)) = [1.1, 2.2, 3.3], add(array(1,2,3), 10) = [11,12,13]
      • subtract

        public static double[] subtract​(java.lang.Object arrayOrScalar1,
                                        java.lang.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.

        If the arguments are not as expected (e.g. arrays of different lengths, both scalars, not numeric) then null is returned.

        Parameters:
        arrayOrScalar1 - first numeric array/scalar
        arrayOrScalar2 - second numeric array/scalar
        Returns:
        element-by-element result of arrayOrScalar1 - arrayOrScalar2, the same length as the input array(s)
        Examples:
        subtract(array(1,2,3), array(0.1,0.2,0.3)) = [0.9, 1.8, 2.7], subtract(array(1,2,3), 1.0) = [0, 1, 2]
      • multiply

        public static double[] multiply​(java.lang.Object arrayOrScalar1,
                                        java.lang.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.

        If the arguments are not as expected (e.g. arrays of different lengths, both scalars, not numeric) then null is returned.

        Parameters:
        arrayOrScalar1 - first numeric array/scalar
        arrayOrScalar2 - second numeric array/scalar
        Returns:
        element-by-element result of arrayOrScalar1 * arrayOrScalar2, the same length as the input array(s)
        Examples:
        multiply(array(1,2,3), array(2,4,6)) = [2, 8, 18], multiply(2, array(1,2,3)) = [2, 4, 6]
      • divide

        public static double[] divide​(java.lang.Object arrayOrScalar1,
                                      java.lang.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.

        If the arguments are not as expected (e.g. arrays of different lengths, both scalars, not numeric) then null is returned.

        Parameters:
        arrayOrScalar1 - first numeric array/scalar
        arrayOrScalar2 - second numeric array/scalar
        Returns:
        element-by-element result of arrayOrScalar1 / arrayOrScalar2, the same length as the input array(s)
        Examples:
        divide(array(0,9,4), array(1,3,8)) = [0, 3, 0.5], divide(array(50,60,70), 10) = [5, 6, 7]
      • reciprocal

        public static double[] reciprocal​(java.lang.Object array)
        Returns the result of taking the reciprocal of every element of a numeric array. If the supplied array argument is not a numeric array, null is returned.
        Parameters:
        array - array input
        Returns:
        array output, the same length as the array parameter
        Examples:
        reciprocal(array(1,2,0.25) = [1, 0.5, 4]
      • condition

        public 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.

        This has the same effect as applying the expression outArray[i] = flagArray[i] ? trueValue : falseValue.

        Parameters:
        flagArray - array of boolean values
        trueValue - output value corresponding to an input true value
        falseValue - output value corresponding to an input false value
        Returns:
        output numeric array, same length as flagArray
        Examples:
        condition([true, false, true], 1, 0) = [1, 0, 1]
      • constant

        public static double[] constant​(int n,
                                        double value)
        Returns a fixed-size array filled with a given constant value.

        Note: This documents the double-precision version of the routine. Corresponding routines exist for other data types (float, long, int, short, byte).

        Parameters:
        n - size of output array
        value - value of every element in the output array
        Returns:
        n-element array with every element set to value
        Examples:
        constant(5, 23.5) = [23.5, 23.5, 23.5, 23.5, 23.5]
      • constant

        @HideDoc
        public static float[] constant​(int n,
                                       float value)
      • constant

        @HideDoc
        public static byte[] constant​(int n,
                                      byte value)
      • constant

        @HideDoc
        public static short[] constant​(int n,
                                       short value)
      • constant

        @HideDoc
        public static int[] constant​(int n,
                                     int value)
      • constant

        @HideDoc
        public static long[] constant​(int n,
                                      long value)
      • slice

        public static double[] slice​(double[] array,
                                     int i0,
                                     int i1)
        Returns a sub-sequence of values from a given array.

        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).

        Parameters:
        array - input array
        i0 - 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)
        Returns:
        array giving the sequence of elements specified by i0 and i1
        Examples:
        slice(array(10,11,12,13), 0, 3) = [10, 11, 12], slice(array(10,11,12,13), -2, 999) = [12, 13]
      • slice

        @HideDoc
        public static float[] slice​(float[] array,
                                    int i0,
                                    int i1)
      • slice

        @HideDoc
        public static long[] slice​(long[] array,
                                   int i0,
                                   int i1)
      • slice

        @HideDoc
        public static int[] slice​(int[] array,
                                  int i0,
                                  int i1)
      • slice

        @HideDoc
        public static short[] slice​(short[] array,
                                    int i0,
                                    int i1)
      • slice

        @HideDoc
        public static byte[] slice​(byte[] array,
                                   int i0,
                                   int i1)
      • slice

        @HideDoc
        public static java.lang.String[] slice​(java.lang.String[] array,
                                               int i0,
                                               int i1)
      • slice

        @HideDoc
        public static java.lang.Object[] slice​(java.lang.Object[] array,
                                               int i0,
                                               int i1)
      • pick

        public static double[] pick​(double[] array,
                                    int... indices)
        Returns a selection of elements from a given array.

        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).

        Parameters:
        array - input array
        indices - one or more index into the input array (may be negative to count back from the end)
        Returns:
        array giving the elements specified by indices
        Examples:
        pick(array(10,11,12,13), 0, 3) = [10, 13], pick(array(10,11,12,13), -1, -2, -3) = [13, 12, 11]
      • pick

        @HideDoc
        public static float[] pick​(float[] array,
                                   int... indices)
      • pick

        @HideDoc
        public static long[] pick​(long[] array,
                                  int... indices)
      • pick

        @HideDoc
        public static int[] pick​(int[] array,
                                 int... indices)
      • pick

        @HideDoc
        public static short[] pick​(short[] array,
                                   int... indices)
      • pick

        @HideDoc
        public static byte[] pick​(byte[] array,
                                  int... indices)
      • pick

        @HideDoc
        public static java.lang.String[] pick​(java.lang.String[] array,
                                              int... indices)
      • pick

        @HideDoc
        public static java.lang.Object[] pick​(java.lang.Object[] array,
                                              int... indices)
      • arrayFunc

        public static double[] arrayFunc​(java.lang.String expr,
                                         java.lang.Object inArray)
        Returns a floating-point array resulting from applying a given function expression element-by-element to an input array. The output array is the same length as the input array.

        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.

        Parameters:
        expr - expression mapping input to output array values
        inArray - input array
        Returns:
        floating point array with the same number of elements as inArray, or null for a bad expr
        Examples:
        arrayFunc("3*x",array(0,1,2,3,NaN)) = [0, 3, 6, 9, NaN], arrayFunc("pow(2,i)+x", array(0.5,0.5,0.5,0.5)) = [1.5, 2.5, 4.5, 8.5]
      • intArrayFunc

        public static int[] intArrayFunc​(java.lang.String expr,
                                         java.lang.Object inArray)
        Returns an integer array resulting from applying a given function expression element-by-element to an input array. The output array is the same length as the input array.

        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.

        Parameters:
        expr - expression mapping input to output array values
        inArray - input array
        Returns:
        floating point array with the same number of elements as inArray, or null for a bad expr
        Examples:
        intArrayFunc("-x",sequence(5)) = [0, -1, -2, -3, -4]
      • untypedArrayFunc

        @HideDoc
        public static java.lang.Object untypedArrayFunc​(java.lang.String expr,
                                                        java.lang.Object inArray)
        Returns an untyped array resulting from applying a given function expression element-by-element to an input array. The runtime type of the output value is determined by the compiled type of the supplied expression, but since this method only has type Object, it has to be cast manually to the actual type before anything much can be done with it. For that reason it's not publicised in the user documentation.
        Parameters:
        expr - expression mapping input to output array values
        inArray - input array
        Returns:
        untyped array with the same number of elements as inArray, or null for a bad expr
      • indexOf

        public static int indexOf​(java.lang.Object[] array,
                                  java.lang.Object item)
        Returns the position in a supplied array at which a given item appears. The result is zero-based, so if the supplied 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).

        Parameters:
        array - array which may contain the supplied item
        item - entry to look for in the array
        Returns:
        the index of item in array, or -1
        Examples:
        indexOf(stringArray("QSO", "BCG", "SNR"), "BCG") = 1, indexOf(stringArray("QSO", "BCG", "SNR"), "TLA") = -1
      • indexOf

        @HideDoc
        public static int indexOf​(double[] array,
                                  double item)
      • indexOf

        @HideDoc
        public static int indexOf​(float[] array,
                                  double item)
      • indexOf

        @HideDoc
        public static int indexOf​(long[] array,
                                  long item)
      • indexOf

        @HideDoc
        public static int indexOf​(int[] array,
                                  int item)
      • indexOf

        @HideDoc
        public static int indexOf​(short[] array,
                                  int item)
      • sequence

        public static int[] sequence​(int n)
        Returns an integer array of a given length with the values 0, 1, 2, ....
        Parameters:
        n - length of array
        Returns:
        n-element array, (0, 1, 2, ... n-1)
        Examples:
        sequence(4) = (0, 1, 2, 3)
      • sequence

        public static double[] sequence​(int n,
                                        double start,
                                        double step)
        Returns a double array of a given length with values starting at a given value and increasing with a given increment.
        Parameters:
        n - length of array
        start - value of first element
        step - increment to apply to each element
        Returns:
        n-element array, (start, start+step, start+2*step, ... start+(n-1)*step)
        Examples:
        sequence(4, 100, 0.1) = (100.0, 100.1, 100.2, 100.3)
      • array

        public static double[] array​(double... values)
        Returns a floating point numeric array built from the given arguments.
        Parameters:
        values - one or more array elements
        Returns:
        array
      • intArray

        public static int[] intArray​(int... values)
        Returns an integer numeric array built from the given arguments.
        Parameters:
        values - one or more array elements
        Returns:
        array
      • stringArray

        public static java.lang.String[] stringArray​(java.lang.String... values)
        Returns a String array built from the given arguments.
        Parameters:
        values - one or more array elements
        Returns:
        array