uk.ac.starlink.ttools.func
Class Arrays

java.lang.Object
  extended by uk.ac.starlink.ttools.func.Arrays

public class Arrays
extends 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:

Since:
14 Jul 2008
Author:
Mark Taylor

Method Summary
static double[] add(Object array, double constant)
          Returns the result of adding a constant value to every element of a numeric array.
static double[] add(Object array1, 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(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 array1, 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 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 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 array, double constant)
          Returns the result of multiplying every element of a numeric array by a constant value.
static double[] multiply(Object array1, Object array2)
          Returns the result of multiplying two numeric arrays element by element.
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 size(Object array)
          Returns the number of elements in the array.
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 array1, Object array2)
          Returns the result of subtracting one numeric array from the other element by element.
static double sum(Object array)
          Returns the sum of all the non-blank elements in the array.
static double variance(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
 

Method Detail

sum

public static double sum(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(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(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(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(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(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(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(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(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(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 String join(Object array,
                          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"

add

public static double[] add(Object array1,
                           Object array2)
Returns the result of adding two numeric arrays element by element. Both arrays must be numeric, and the arrays must have the same length. If either of those conditions is not true, 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.

Parameters:
array1 - first array of numeric values
array2 - second array of numeric values
Returns:
element-by-element sum of array1 and array2, the same length as the input arrays
Examples:
add(array(1,2,3), array(0.1,0.2,0.3)) = [1.1, 2.2, 3.3]

add

public static double[] add(Object array,
                           double constant)
Returns the result of adding a constant value to every element of a numeric array. If the supplied array argument is not a numeric array, null is returned.

Parameters:
array - array input
constant - value to add to each array element
Returns:
array output, the same length as the array parameter
Examples:
add(array(1,2,3), 10) = [11,12,13]

subtract

public static double[] subtract(Object array1,
                                Object array2)
Returns the result of subtracting one numeric array from the other element by element. Both arrays must be numeric, and the arrays must have the same length. If either of those conditions is not true, 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.

Parameters:
array1 - first array of numeric values
array2 - second array of numeric values
Returns:
element-by-element difference of array1 and array2, the same length as the input arrays
Examples:
subtract(array(1,2,3), array(0.1,0.2,0.3)) = [0.9, 1.8, 2.7]

multiply

public static double[] multiply(Object array1,
                                Object array2)
Returns the result of multiplying two numeric arrays element by element. Both arrays must be numeric, and the arrays must have the same length. If either of those conditions is not true, 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.

Parameters:
array1 - first array of numeric values
array2 - second array of numeric values
Returns:
element-by-element product of array1 and array2, the same length as the input arrays
Examples:
multiply(array(1,2,3), array(2,4,6)) = [2, 8, 18]

multiply

public static double[] multiply(Object array,
                                double constant)
Returns the result of multiplying every element of a numeric array by a constant value. If the supplied array argument is not a numeric array, null is returned.

Parameters:
array - array input
constant - value by which to multiply each array element
Returns:
array output, the same length as the array parameter
Examples:
multiply(array(1,2,3), 2) = [2, 4, 6]

divide

public static double[] divide(Object array1,
                              Object array2)
Returns the result of dividing two numeric arrays element by element. Both arrays must be numeric, and the arrays must have the same length. If either of those conditions is not true, 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.

Parameters:
array1 - array of numerator values (numeric)
array2 - array of denominator values (numeric)
Returns:
element-by-element result of array1[i]/array2[i] the same length as the input arrays
Examples:
divide(array(0,9,4), array(1,3,8)) = [0, 3, 0.5]

reciprocal

public static double[] reciprocal(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]

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 String[] stringArray(String... values)
Returns a String array built from the given arguments.

Parameters:
values - one or more array elements
Returns:
array


Copyright © 2017 Central Laboratory of the Research Councils. All Rights Reserved.