KSLArrays

object KSLArrays

This class has some array manipulation methods that I have found useful over the years.

Functions

Link copied to clipboard
fun abs(array: DoubleArray)

Performs element-wise absolute value on the array. The array is changed in place.

Link copied to clipboard

This operation is in-place.

Link copied to clipboard

Adds the two arrays element by element. Arrays must have same length and must not be null.

Link copied to clipboard
fun apply(array: Array<DoubleArray>, function: FunctionIfc)
fun apply(array: DoubleArray, function: FunctionIfc)

Element-wise application of the supplied function. The array is changed in place. Using FunctionIfc avoids autoboxing when dealing with primitive doubles.

Link copied to clipboard
fun <T> asStringArray(array: Array<T>?): Array<String?>
Link copied to clipboard

Returns a BoxPlotSummary that summarizes the passed in array of values

Link copied to clipboard

Computes the cartesian product of the two arrays. Returns a list of pairs where the first element of the pair is from the first array and the second element of the pair is from the second array. This produces all possible combinations of the elements as the pairs. If the first array has n elements and the second array has m elements then the number of pairs produced is n x m.

fun <F, S> cartesian(first: Collection<F>, second: Collection<S>): List<Pair<F, S>>

Computes the cartesian product of the two collections. Returns a list of pairs where the first element of the pair is from the first collection and the second element of the pair is from the second collection. This produces all possible combinations of the elements as the pairs. If the first collection has n elements and the second collection has m elements then the number of pairs produced is n x m.

Link copied to clipboard
fun cartesianProduct(a: Set<*>, b: Set<*>, vararg sets: Set<*>): List<List<*>>

Computes the cartesian product of the supplied sets and returns a list holding the rows of the cartesian product

Link copied to clipboard

Computes the cartesian product of the sets of doubles and returns a list holding the rows of the cartesian product with each row represented as a list.

Link copied to clipboard
fun cartesianProductOfInts(a: Set<Int>, b: Set<Int>, vararg sets: Set<Int>): List<List<Int>>

Computes the cartesian product of the sets of ints and returns a list holding the rows of the cartesian product with each row represented as a list.

Link copied to clipboard

The rows of the sets array are treated like elements in sets. The function returns the element stored at the index of the cartesian product of the sets. The index must be between 0 and (n-1), where n is the number of elements in the cartesian product. The elements start at 0. Since the elements of the arrays represent a set, the values must be unique. That is, no duplicates are permitted within an individual array.

Link copied to clipboard

Checks if any element of the array is equal to Double.NaN

Link copied to clipboard
inline fun <T> column(index: Int, matrix: Array<Array<T>>): Array<T>
fun column(k: Int, matrix: Array<IntArray>): IntArray
fun column(k: Int, matrix: Array<LongArray>): LongArray
Link copied to clipboard
Link copied to clipboard

Converts the 2-D array to a 1-D array by processing the source src array row-wise and concatenating the rows. For example if the data is organized as follows:

Link copied to clipboard

Each labeled array in the map becomes a column in the returned array. Each array in the map must have the same number of elements.

Link copied to clipboard

Each labeled array in the map becomes a row in the returned array, which may be ragged because each row in the array may have a different length.

Link copied to clipboard
fun copyWithout(index: Int, fromA: DoubleArray, toB: DoubleArray = DoubleArray(fromA.size - 1)): DoubleArray

Copies all but element index of array fromA into array toB If fromA has 1 element, toB will be empty

Link copied to clipboard
fun countElements(objects: List<*>, targetClass: Class<*>): Int

Returns a count of the elements that are of the same type as the target class.

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
fun diff(x: DoubleArray, k: Int = 1): DoubleArray

Computes the difference, (di = xi+k - xi) for i = 0 until x.size - k This is the discrete difference operator. For example, if k = 1, then d0 = x1 - x0, d1 = x2 - x1, ..., dx.size - 2 = xx.size -1 - xx.size -2 and returns the new array of differences.

Link copied to clipboard

This operation is in-place.

Link copied to clipboard

Divides the arrays' element by element. Arrays must have same length and must not be null.

Link copied to clipboard
Link copied to clipboard
fun expandToRectangular(array2D: Array<DoubleArray>, fillValue: Double = 0.0): Array<DoubleArray>

Copies the supplied 2-D array by expanding to the maximum number of columns of the supplied (ragged) array so that the returned array is rectangular, where all row arrays have the same number of elements (columns).

Link copied to clipboard
fun fill(array: Array<DoubleArray>, theValue: GetValueIfc = ConstantRV.ZERO)
fun fill(array: DoubleArray, theValue: GetValueIfc = ConstantRV.ZERO)
Link copied to clipboard
fun fillColumn(col: Int, src: DoubleArray, dest: Array<DoubleArray>)

The destination array is mutated by this method

Link copied to clipboard
fun findIndex(element: Double, array: DoubleArray): Int
fun findIndex(element: Long, array: LongArray): Int
fun findIndex(element: String, array: Array<String>): Int

If the array is empty or the element is not found, -1 is returned.

fun findIndex(element: Int, array: IntArray): Int

If the array is empty, -1 is returned.

Link copied to clipboard
fun <T> getElements(objects: List<*>, targetClass: Class<T>): List<T>

Returns a list of the elements that are of the same type as the target class. Usage: getElements(objects, String.class);

Link copied to clipboard
fun hasElement(element: Double, array: DoubleArray): Boolean
fun hasElement(element: Int, array: IntArray): Boolean
fun hasElement(element: Long, array: LongArray): Boolean
Link copied to clipboard
Link copied to clipboard

Returns the index associated with the maximum element in the array For ties, this returns the first found.

Returns the index associated with the maximum element in the array For ties, this returns the first found

Link copied to clipboard

Returns the index associated with the minimum element in the array For ties, this returns the first found.

Returns the index associated with the minimum element in the array For ties, this returns the first found

Link copied to clipboard
fun insertAt(arr: DoubleArray, value: Double, index: Int): DoubleArray
fun insertAt(arr: IntArray, value: Int, index: Int): IntArray

Returns a new array with the value inserted at the index.

Link copied to clipboard
fun isAllDifferent(array: DoubleArray, precision: Double = KSLMath.defaultNumericalPrecision): Boolean

Examines each element, a_i starting at 0, and determines if all the elements are equal a_0 != a_1 != a_2, etc.

Link copied to clipboard
fun isAllEqual(array: DoubleArray, precision: Double = KSLMath.defaultNumericalPrecision): Boolean

Examines each element, a_i starting at 0, and determines if all the elements are equal a_0 = a_1 = a_2, etc.

Link copied to clipboard

Examines each element, a_i starting at 0, and determines if all the elements are decreasing a_0 gte a_1 gte a_2, etc.

Link copied to clipboard
Link copied to clipboard

Examines each element, a_i starting at 0, and determines if all the elements are increasing a_0 lte a_1 lte a_2, etc.

Link copied to clipboard

The arrays must be rectangular and n columns of first must be same and n rows for second

Link copied to clipboard
fun <T> isRectangular(array2D: Array<Array<T>>): Boolean

A 2-D array is rectangular if all rows have the same number of elements (columns).

An array is rectangular if all rows have the same number of elements (columns).

The collection of arrays is considered rectangular if all arrays in the collection have the same number of elements.

Link copied to clipboard
Link copied to clipboard

Examines each element, a_i starting at 0, and determines if all the elements are strictly decreasing a_0 gt a_1 gt a_2, etc.

Link copied to clipboard

Examines each element, a_i starting at 0, and determines if all the elements are strictly increasing a_0 lt a_1 lt a_2, etc.

Link copied to clipboard

Examines each element, a_i starting at 0, and determines if any element is less than or equal to 0.0.

Link copied to clipboard
inline fun <T> lag(x: Array<T>, k: Int = 1): Array<T>
fun lag(x: DoubleArray, k: Int = 1): DoubleArray

Returns a new array of size (x.size -k) that is lagged by k elements yi = xi+k for i=0,1,...

Link copied to clipboard
fun matrixOfDoubles(nRows: Int, nCols: Int, value: Double = 0.0): Array<DoubleArray>

Creates a matrix of Doubles with nRows and nCols containing the supplied value

Creates a matrix of doubles with nRows and nCols containing values from the supplied x

Link copied to clipboard
fun matrixOfInts(nRows: Int, nCols: Int, value: Int = 0): Array<IntArray>

Creates a matrix of Ints with nRows and nCols containing the supplied value

Link copied to clipboard
fun matrixOfLongs(nRows: Int, nCols: Int, value: Long = 0): Array<LongArray>

Creates a matrix of Longs with nRows and nCols containing the supplied value

Link copied to clipboard
Link copied to clipboard

Assumes that the array can be ragged. Returns the number of elements in the row array that has the most elements.

Link copied to clipboard

Subtracts the arrays element by element. Arrays must have same length and must not be empty. Computes the average of the squares of the differences.

Link copied to clipboard
Link copied to clipboard

Returns a new array that has been scaled so that the values are between the minimum and maximum values of the supplied array

Link copied to clipboard

Assumes that the 2-D array can be ragged. Returns the number of columns necessary that would cause the array to not be ragged. In other words, the minimum number of columns to make the array an un-ragged array (matrix) where all row arrays have the same number of elements.

Link copied to clipboard
fun mround(x: Double, granularity: Double): Double

contributed by Andrew Gibson round a scalar double to a multiple of granularity note that 0 a granularity value is interpreted as "no rounding"

fun mround(x: DoubleArray, granularity: Double): DoubleArray

contributed by Andrew Gibson round a 1D array x to a multiple of a scalar granularity value note that 0 or null granularity values are interpreted as "no rounding"

fun mround(x: DoubleArray, granularity: DoubleArray?): DoubleArray

contributed by Andrew Gibson round the 1D array x to a multiple of granularity (double[]) note that 0 or null granularity values are interpreted as "no rounding"

Link copied to clipboard

The arrays must be rectangular with the number of rows of the first array equal to the number of columns of the second array.

Link copied to clipboard

This operation is in-place.

Link copied to clipboard

Multiplies the two arrays element by element. Arrays must have same length.

Link copied to clipboard

Returns a new array that has been scaled so that the values are the (x - avg)/sd values of the supplied array

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard

Assumes that the entries in the list are string representations of double values. Each String[] can have a different number of elements. Thus, the returned array may be ragged.

Link copied to clipboard
fun parseToDoubles(dblStrings: Array<String>, parseFail: Double = Double.NaN): DoubleArray
fun parseToDoubles(dblStrings: List<String>, parseFail: Double = Double.NaN): DoubleArray

Converts the array of strings to Doubles

Link copied to clipboard
Link copied to clipboard
fun range(array: DoubleArray): Double
fun range(array: IntArray): Int
fun range(array: LongArray): Long
Link copied to clipboard
fun remainder(array: DoubleArray, divisor: Double)

Performs element-wise modulo (%) operator on the array. The array is changed in place.

Link copied to clipboard
fun removeAt(arr: DoubleArray, index: Int): DoubleArray
fun removeAt(arr: IntArray, index: Int): IntArray

Removes the element at the index. If the index is out of bounds, then a copy of the array is returned.

Link copied to clipboard

Returns a new array with duplicate data values removed from the original array, preserving the order of the observations.

Link copied to clipboard

Returns a new array in the same order as the original array but with the specified value removed. All instances of the value will be removed.

Link copied to clipboard

contributed by Andrew Gibson simple way to create a n-element vector of the same value (x)

Link copied to clipboard
fun sigFigDecimals(value: Double, sf: Int): Int

contributed by Andrew Gibson calculate the number of decimal places needed to give sf digits

fun sigFigDecimals(values: DoubleArray, sf: Int): Int

contributed by Andrew Gibson calculate the number of decimal places needed to give AT LEAST sf digits to all values

Link copied to clipboard
fun sigFigFormat(value: Double, sf: Int): String

contributed by Andrew Gibson

Link copied to clipboard

Returns a statistic that summarizes the passed in array of values

Link copied to clipboard

This operation is in-place.

Link copied to clipboard

Subtracts the arrays element by element. Arrays must have same length and must not be null.

Link copied to clipboard

Subtracts the arrays element by element. Arrays must have same length and must not be empty. Computes the sum of the squares of the differences.

Link copied to clipboard

The array must have non-negative elements and not be empty

Link copied to clipboard

The array must not be null.

Link copied to clipboard

Converts the list of arrays to an array of arrays.

Link copied to clipboard
Link copied to clipboard

Converts the two-dimensional array into a list containing the arrays. The rows of the array become the elements of the list.

Link copied to clipboard

Convert the 2D array of double to a 2D array of Double with each element the corresponding value

Convert the array of int to an array of double with each element the corresponding value

Convert the array of long to an array of double with each element the corresponding value

Link copied to clipboard

Convert the 2D array of int to a 2D array of Integer with each element the corresponding value

Link copied to clipboard

Convert the 2D array of int to a 2D array of Long with each element the corresponding value

Link copied to clipboard
fun toPrimitives(array: Array<Double?>, replaceNull: Double = 0.0): DoubleArray

Converts any null values to replaceNull. For Array use toDoubleArray()

fun toPrimitives(array: Array<Int?>, replaceNull: Int = 0): IntArray

Converts any null values to replaceNull, for Array use toIntArray()

fun toPrimitives(array: Array<Long?>, replaceNull: Long = 0): LongArray

Converts any null values to replaceNull, for Array use toLongArray()

fun toPrimitives(doubleList: List<Double?>, replaceNull: Double = 0.0): DoubleArray

Converts any nulls to replaceNull. For List use toDoubleArray()

fun toPrimitives(list: List<Int?>, replaceNull: Int = 0): IntArray

Converts any nulls to zero, for List use toIntArray()

fun toPrimitives(list: List<Long?>, replaceNull: Long = 0): LongArray

Converts any nulls to replaceNull

Link copied to clipboard
fun toStrings(array: DoubleArray, df: DecimalFormat? = null): Array<String>

Convert the array of doubles to an array of strings with each element the corresponding value

Link copied to clipboard
inline fun <T> transpose(array: Array<Array<T>>): Array<Array<T>>

Transposes the array returned transposey = arrayx

Link copied to clipboard

Copies the supplied array by trimming to the minimum number of columns of the supplied (potentially ragged) array so that the returned array is rectangular, where all row arrays have the same number of elements (columns)