ArrayUtils

public class ArrayUtils

Utils for quick manipulation with (numeric) arrays. Warning: most methods work only on rectangular arrays.

Author:pipek

Methods

add

public static double[] add(double[] array, double offset)

Add a constant to all elements of an array.

Returns:a new array

asDoubleArray

public static double[] asDoubleArray(Object array)

Cast all values of an array to double (runtime version).

Parameters:
  • array – Array of allowed type.
Throws:

asDoubleArray

public static double[] asDoubleArray(long[] array)

Cast all values of an array to double.

asDoubleArray

public static double[] asDoubleArray(int[] array)

Cast all values of an array to double.

asDoubleArray

public static double[] asDoubleArray(short[] array)

Cast all values of an array to double.

asDoubleArray

public static double[] asDoubleArray(float[] array)

Cast all values of an array to double.

asIntArray

public static int[] asIntArray(long[] array)

Cast all values of an array to int.

asLongArray

public static long[] asLongArray(int[] array)

Cast all values of an array to long.

dimensions

public static long[] dimensions(Object array)

Get the dimensions along all axes of an array. Works only on rectangular arrays.

get

public static Object get(Object array, int[] index)

Get an element from a multidimensional array. Works on all arrays.

linearTransform

public static double[] linearTransform(double[] array, double multiplyBy, double add)

Apply a linear transformation y = ax + b on an array.

Parameters:
  • multiplyBy – Multiplication constant
  • add – Addition constant
Returns:

a new array

linearTransform

public static Object linearTransform(Object array, double multiplyBy, double add)

A general linear transformation of multidimensional array.

Parameters:
  • array
    • N-dimensional rectangular array of double/long/int/short/float
  • multiplyBy
    • multiplicative factor
  • add
    • additive factor
Returns:

  • N-dimensional array of the same shape as input

max

public static double max(double[] array)

Maximum value found in the array.

min

public static double min(double[] array)

Minimum value found in the array.

multiply

public static double[] multiply(double[] array, double coefficient)

Multiply all elements of an array by a constant.

Returns:a new array

product

public static long product(int[] array)

product

public static long product(long[] array)

range

public static int[] range(int xmin, int xmax)

rank

public static int rank(Object array)

Get the number of dimensions of an array. Works only on rectangular arrays.

reshape

public static Object reshape(Object array, int[] newDimensions)

Reshape array. Create a new array with the same total size but with different dimensions. Last index is moving fastest, while the elements are copied one after another. Works only on rectangular arrays.

Parameters:
  • array
  • newDimensions
Returns:

new array

set

public static void set(Object array, int[] index, Object value)

Set an element in a multidimensional array. Works on all arrays.

size

public static long size(Object array)

Total number of items in array. Works only on rectangular arrays.