Package org.nd4j.common.util
Class ArrayUtil
- java.lang.Object
-
- org.nd4j.common.util.ArrayUtil
-
public class ArrayUtil extends Object
- Author:
- Adam Gibson
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static boolean
allUnique(int[] toTest)
Returns true if all of the elements in the given int array are uniquestatic boolean
anyLargerThan(int[] arrs, int check)
static boolean
anyLess(int[] target, int[] test)
static boolean
anyLessThan(int[] arrs, int check)
static boolean
anyMore(int[] target, int[] test)
static int
argMax(int[] in)
Returns the index of the maximum value in the array.static int
argMax(long[] in)
Returns the index of the maximum value in the array.static int
argMin(int[] in)
Returns the index of the minimum value in the array.static int
argMin(long[] in)
Returns the index of the minimum value in the array.static int
argMinOfMax(int[]... arrays)
static int
argMinOfMax(int[] first, int[] second)
static long
argMinOfMax(long[]... arrays)
static long
argMinOfMax(long[] first, long[] second)
static int
argMinOfSum(int[] first, int[] second)
static int[]
argsort(int[] a)
Original credit: https://github.com/alberts/array4j/blob/master/src/main/java/net/lunglet/util/ArrayUtils.javastatic int[]
argsort(int[] a, boolean ascending)
static int
arrayLength(Object current)
Calculate the length of the object or primitive array.static int[]
arrayShape(Object array)
Calculate the shape of an arbitrary multi-dimensional array.static int[]
arrayShape(Object array, boolean allowSize0Dims)
Calculate the shape of an arbitrary multi-dimensional array.
Note that the method assumes the array is rectangular (not ragged) and first elements (i.e., array[0][0][0]...) are non-null
Note also that if allowSize0Dims is true, any elements are length 0, all subsequent dimensions will be reported as 0.static <T> void
assertNotRagged(T[] array)
Assert that the specified array is not ragged (i.e., is rectangular).
Can be used to check Object arrays with any number of dimensions (up to rank 4), or primitive arrays with rank 2 or higher
An IllegalStateException is thrown if the array is raggedstatic void
assertSquare(double[]... d)
static int[]
buildHalfVector(Random rng, int length)
static int[]
buildInterleavedVector(Random rng, int length)
static long[]
buildInterleavedVector(Random rng, long length)
static int
calcOffset(int[] shape, int[] offsets, int[] strides)
Compute the offset based on teh shape strides and offsetsstatic long
calcOffset(long[] shape, long[] offsets, long[] strides)
Compute the offset based on teh shape strides and offsetsstatic int
calcOffset(List<Integer> shape, List<Integer> offsets, List<Integer> strides)
Compute the offset based on teh shape strides and offsetsstatic long
calcOffsetLong(int[] shape, int[] offsets, int[] strides)
Compute the offset based on teh shape strides and offsetsstatic long
calcOffsetLong(List<Integer> shape, List<Integer> offsets, List<Integer> strides)
Compute the offset based on teh shape strides and offsetsstatic long
calcOffsetLong2(List<Long> shape, List<Long> offsets, List<Long> strides)
static int[]
calcStrides(int[] shape)
Computes the standard packed array strides for a given shape.static int[]
calcStrides(int[] shape, int startValue)
Computes the standard packed array strides for a given shape.static long[]
calcStrides(long[] shape)
static long[]
calcStrides(long[] shape, int startValue)
Computes the standard packed array strides for a given shape.static int[]
calcStridesFortran(int[] shape)
Computes the standard packed array strides for a given shape.static int[]
calcStridesFortran(int[] shape, int startNum)
Computes the standard packed array strides for a given shape.static long[]
calcStridesFortran(long[] shape)
static long[]
calcStridesFortran(long[] shape, int startNum)
Computes the standard packed array strides for a given shape.static double[]
combine(float[]... ints)
Combines a apply of int arrays in to one flat int arraystatic int[]
combine(int[]... ints)
Combines a apply of int arrays in to one flat int arraystatic long[]
combine(long[]... ints)
Combines a apply of long arrays in to one flat long arraystatic <E> E[]
combine(E[]... arrs)
static float[]
combine(List<float[]> nums)
Combines a apply of int arrays in to one flat int arraystatic double[]
combineDouble(List<double[]> nums)
Combines a apply of int arrays in to one flat int arraystatic float[]
combineFloat(List<float[]> nums)
Combines a applyTransformToDestination of int arrays in to one flat int arraystatic int[]
consArray(int a, int[] as)
static boolean
contains(int[] arr, int value)
static boolean
contains(long[] arr, int value)
static boolean
containsAnyNegative(int[] arr)
Returns true if any array elements are negative.static boolean
containsAnyNegative(long[] arr)
static int[]
convertNegativeIndices(int range, int[] axes)
Convert all dimensions in the specified axes array to be positive based on the specified range of valuesstatic String[]
convertToString(int[] arr)
Convert a int array to a string arraystatic double[]
copy(double[] data)
static float[]
copy(float[] data)
static int[]
copy(int[] copy)
static long[]
copy(long[] copy)
static int[]
copyOfRangeFrom(int length, int from, int to)
Generate an array from 0 to length and generate take a subsetstatic byte[]
cutBelowZero(byte[] data)
static int[]
cutBelowZero(int[] data)
static long[]
cutBelowZero(long[] data)
static short[]
cutBelowZero(short[] data)
static int
dotProduct(int[] xs, int[] ys)
static int
dotProduct(List<Integer> xs, List<Integer> ys)
static long
dotProductLong(int[] xs, int[] ys)
static long
dotProductLong(List<Integer> xs, List<Integer> ys)
static long
dotProductLong2(List<Long> xs, List<Long> ys)
static double[]
doubleCopyOf(float[] data)
static int[]
empty()
static boolean
equals(float[] data, double[] data2)
static <T> T[]
filterNull(T... in)
static boolean[]
flatten(boolean[][] arr)
static boolean[]
flatten(boolean[][][] arr)
static boolean[]
flatten(boolean[][][][] arr)
static byte[]
flatten(byte[][] arr)
static byte[]
flatten(byte[][][] arr)
static byte[]
flatten(byte[][][][] arr)
static double[]
flatten(double[][] arr)
Convert a 2darray in to a flat array (row wise)static double[]
flatten(double[][][] arr)
static double[]
flatten(double[][][][] arr)
static float[]
flatten(float[][] arr)
static float[]
flatten(float[][][] arr)
static float[]
flatten(float[][][][] arr)
static int[]
flatten(int[][] arr)
static int[]
flatten(int[][][] arr)
static int[]
flatten(int[][][][] arr)
static long[]
flatten(long[][] arr)
static long[]
flatten(long[][][] arr)
static long[]
flatten(long[][][][] arr)
static short[]
flatten(short[][] arr)
static short[]
flatten(short[][][] arr)
static short[]
flatten(short[][][][] arr)
static double[]
flattenDoubleArray(Object doubleArray)
Convert an arbitrary-dimensional rectangular double array to flat vector.
Can pass double[], double[][], double[][][], etc.static double[]
flattenF(double[][] arr)
Convert a 2darray in to a flat array (row wise)static float[]
flattenF(float[][] arr)
static int[]
flattenF(int[][] arr)
static long[]
flattenF(long[][] arr)
static float[]
flattenFloatArray(Object floatArray)
Convert an arbitrary-dimensional rectangular float array to flat vector.
Can pass float[], float[][], float[][][], etc.static float[]
floatCopyOf(double[] data)
static int
fromBoolean(boolean bool)
Convert an intstatic short
fromFloat(float v)
static <T> T
getRandomElement(List<T> list)
static long[]
getTensorMmulShape(long[] aShape, long[] bShape, int[][] axes)
Get the tensor matrix multiply shapestatic boolean
greaterThan(int[] target, int[] test)
static int[]
invertPermutation(int... input)
Compute the inverse permutation indices for a permutation operation
Example: if input is [2, 0, 1] then output is [1, 2, 0]
The idea is that x.permute(input).permute(invertPermutation(input)) == xstatic long[]
invertPermutation(long... input)
static boolean
isEmptyShape(int[] shape)
Is this shape an empty shape? Shape is considered to be an empty shape if it contains any zeros.static boolean
isEmptyShape(long[] shape)
Is this shape an empty shape? Shape is considered to be an empty shape if it contains any zeros.static boolean
isInverse(int[] first, int[] second)
Returns true if the given two arrays are reverse copies of each otherstatic boolean
isZero(int[] as)
Returns true if any of the elements are zerostatic boolean
isZero(long[] as)
static int[]
keep(int[] data, int... index)
Return a copy of this array with only the given index(es) remainingstatic long[]
keep(long[] data, int... index)
Return a copy of this array with only the given index(es) remainingstatic boolean
lessThan(int[] target, int[] test)
static boolean
listOfIntsContains(List<int[]> list, int[] target)
Proper comparison contains for list of int arraysstatic int
max(int[] in)
Returns the maximum value in the arraystatic int
min(int[] in)
Returns the minimum value in the arraystatic int[]
mod(int[] input, int mod)
static void
multiplyBy(int[] arr, int mult)
Multiply the given array by the given scalarstatic int
nonOneStride(int[] arr)
For use with row vectors to ensure consistent strides with varying offsetsstatic int[]
nTimes(int n, int toReplicate)
Repeat a value n timesstatic <T> T[]
nTimes(int n, T toReplicate, Class<T> tClass)
static long[]
nTimes(long n, long toReplicate)
static int[]
of(int... arr)
static int
offsetFor(int[] stride, int i)
Calculate the offset for a given stride arraystatic int[]
permute(int[] shape, int[] dimensions)
Permute the given input switching the dimensions of the input shape array with in the order of the specified dimensionsstatic long[]
permute(long[] shape, int[] dimensions)
static int[]
plus(int[] ints, int mult)
static int[]
plus(int[] ints, int[] mult)
static int
prod(int... mult)
Product of an int arraystatic int
prod(long... mult)
Product of an int arraystatic int
prod(List<Integer> mult)
Product of an int arraystatic long
prodLong(int... mult)
Product of an int arraystatic long
prodLong(long... mult)
static long
prodLong(List<? extends Number> mult)
Product of an int arraystatic int[]
randomPermutation(int size)
Credit to mikio braun from jblasstatic double[]
range(double[] data, int to)
Returns a subset of an array from 0 to "to" (exclusive)static double[]
range(double[] data, int to, int stride)
Returns a subset of an array from 0 to "to" (exclusive) using the specified stridestatic double[]
range(double[] data, int to, int stride, int numElementsEachStride)
Returns a subset of an array from 0 to "to" using the specified stridestatic int[]
range(int from, int to)
Generate an int array ranging from "from" to "to".static int[]
range(int from, int to, int increment)
Generate an int array ranging from "from" to "to".static long[]
range(long from, long to)
static long[]
range(long from, long to, long increment)
static double[]
read(int length, DataInputStream dis)
static double[]
readDouble(int length, DataInputStream dis)
static float[]
readFloat(int length, DataInputStream dis)
static int[]
removeIndex(int[] data, int index)
Return a copy of this array with the given index omittedstatic int[]
removeIndex(int[] data, int... index)
Return a copy of this array with the given index omitted PLEASE NOTE: index to be omitted must exist in source array.static long[]
removeIndex(long[] data, int index)
static long[]
removeIndex(long[] data, int... index)
static Integer[]
removeIndex(Integer[] data, int index)
Returns the array with the item in index removed, if the array is empty it will return the array itselfstatic int[]
replace(int[] data, int index, int newValue)
Return a copy of this array with the given index omittedstatic boolean[][]
reshapeBoolean(boolean[] in, int rows, int cols)
static boolean[][][]
reshapeBoolean(boolean[] in, int d0, int d1, int d2)
static double[][]
reshapeDouble(double[] in, int rows, int cols)
static double[][][]
reshapeDouble(double[] in, int d0, int d1, int d2)
static int[][]
reshapeInt(int[] in, int rows, int cols)
static int[][][]
reshapeInt(int[] in, int d0, int d1, int d2)
static long[][]
reshapeLong(long[] in, int rows, int cols)
static long[][][]
reshapeLong(long[] in, int d0, int d1, int d2)
static <T> T[][]
reshapeObject(T[] in, int rows, int cols)
static <T> T[][][]
reshapeObject(T[] in, int d0, int d1, int d2)
static void
reverse(int[] e)
Reverse the passed in array in placestatic void
reverse(long[] e)
static <E> void
reverse(E[] e)
static float[]
reverseCopy(float[] e)
static int[]
reverseCopy(int[] e)
Create a backwards copy of the given arraystatic long[]
reverseCopy(long[] e)
static <E> E[]
reverseCopy(E[] e)
static <T> void
shuffleWithMap(List<T> objects, int[] map)
static <K,V extends Comparable<? super V>>
Map<K,V>sortMapByValue(Map<K,V> map)
static int
sum(int[] add)
Sum of an int arraystatic int
sum(List<Integer> add)
Sum of an int arraystatic long
sumLong(long... add)
protected static <T> void
swap(List<T> objects, int idxA, int idxB)
static int[]
times(int[] ints, int mult)
static int[]
times(int[] ints, int[] mult)
static int[]
toArray(List<Integer> list)
static double[]
toArrayDouble(List<Double> list)
static long[]
toArrayLong(List<Long> list)
static short
toBFloat16(double data)
static short
toBFloat16(float data)
static short[]
toBfloats(float[] data)
static short[]
toBfloats(int[] data)
static short[]
toBfloats(long[] data)
static byte[]
toByteArray(double[] doubleArray)
static byte[]
toByteArray(float[] doubleArray)
static byte[]
toByteArray(int[] intArray)
static byte[]
toBytes(double[] array)
static byte[]
toBytes(float[] array)
static byte[]
toBytes(int[] array)
static byte[]
toBytes(long[] array)
static double[]
toDouble(int[] data)
static double[][]
toDouble(int[][] arr)
Cast an int array to a double arraystatic double[]
toDouble(long[] data)
static double[]
toDoubleArray(byte[] byteArray)
static double[]
toDoubles(float[] ints)
static double[]
toDoubles(int[] ints)
static double[]
toDoubles(int[][] ints)
static double[]
toDoubles(long[] ints)
static float[]
toFloatArray(byte[] byteArray)
static float[]
toFloats(double[] ints)
static float[]
toFloats(int[] ints)
static float[]
toFloats(int[][] ints)
static float[]
toFloats(long[] ints)
static short
toHalf(double data)
static short
toHalf(float data)
static short[]
toHalfs(double[] data)
static short[]
toHalfs(float[] data)
static short[]
toHalfs(int[] data)
static short[]
toHalfs(long[] data)
static int[]
toIntArray(byte[] byteArray)
static int[]
toInts(double[] data)
static int[]
toInts(float[] data)
static int[]
toInts(long[] array)
static List<Integer>
toList(int... ints)
static long[]
toLongArray(float[] array)
static long[]
toLongArray(int[] intArray)
static long[]
toLongs(boolean[] data)
static long[]
toLongs(byte[] data)
static long[]
toLongs(double[] data)
static long[]
toLongs(float[] data)
static long[]
toLongs(int[] data)
static long[]
toLongs(short[] data)
static int[]
toOutcomeArray(int outcome, int numOutcomes)
static boolean[]
toPrimitives(Boolean[] array)
static boolean[][]
toPrimitives(Boolean[][] array)
static boolean[][][]
toPrimitives(Boolean[][][] array)
static boolean[][][][]
toPrimitives(Boolean[][][][] array)
static byte[]
toPrimitives(Byte[] array)
static byte[][]
toPrimitives(Byte[][] array)
static byte[][][]
toPrimitives(Byte[][][] array)
static byte[][][][]
toPrimitives(Byte[][][][] array)
static double[]
toPrimitives(Double[] array)
static double[][]
toPrimitives(Double[][] array)
static double[][][]
toPrimitives(Double[][][] array)
static double[][][][]
toPrimitives(Double[][][][] array)
static float[]
toPrimitives(Float[] array)
static float[][]
toPrimitives(Float[][] array)
static float[][][]
toPrimitives(Float[][][] array)
static float[][][][]
toPrimitives(Float[][][][] array)
static int[]
toPrimitives(Integer[] array)
static int[][]
toPrimitives(Integer[][] array)
static int[][][]
toPrimitives(Integer[][][] array)
static int[][][][]
toPrimitives(Integer[][][][] array)
static long[]
toPrimitives(Long[] array)
static long[][]
toPrimitives(Long[][] array)
static long[][][]
toPrimitives(Long[][][] array)
static long[][][][]
toPrimitives(Long[][][][] array)
static short[]
toPrimitives(Short[] array)
static short[][]
toPrimitives(Short[][] array)
static short[][][]
toPrimitives(Short[][][] array)
static short[][][][]
toPrimitives(Short[][][][] array)
static short[]
toShorts(double[] ints)
static short[]
toShorts(float[] ints)
static short[]
toShorts(int[] ints)
static short[]
toShorts(long[] ints)
static int[]
valueStartingAt(int valueStarting, int[] copy, int idxFrom, int idxAt, int length)
Create a copy of the given array starting at the given index with the given length.static void
write(double[] data, DataOutputStream dos)
static void
write(float[] data, DataOutputStream dos)
static List<double[]>
zerosMatrix(int... dimensions)
static List<double[]>
zerosMatrix(long... dimensions)
static int[][]
zip(int[] as, int[] bs)
Zip 2 arrays in to:
-
-
-
Method Detail
-
containsAnyNegative
public static boolean containsAnyNegative(int[] arr)
Returns true if any array elements are negative. If the array is null, it returns false- Parameters:
arr
- the array to test- Returns:
-
containsAnyNegative
public static boolean containsAnyNegative(long[] arr)
-
contains
public static boolean contains(int[] arr, int value)
-
contains
public static boolean contains(long[] arr, int value)
-
anyLargerThan
public static boolean anyLargerThan(int[] arrs, int check)
- Parameters:
arrs
-check
-- Returns:
-
anyLessThan
public static boolean anyLessThan(int[] arrs, int check)
- Parameters:
arrs
-check
-- Returns:
-
convertToString
public static String[] convertToString(int[] arr)
Convert a int array to a string array- Parameters:
arr
- the array to convert- Returns:
- the equivalent string array
-
listOfIntsContains
public static boolean listOfIntsContains(List<int[]> list, int[] target)
Proper comparison contains for list of int arrays- Parameters:
list
- the to searchtarget
- the target int array- Returns:
- whether the given target array is contained in the list
-
nTimes
public static int[] nTimes(int n, int toReplicate)
Repeat a value n times- Parameters:
n
- the number of times to repeattoReplicate
- the value to repeat- Returns:
- an array of length n filled with the given value
-
nTimes
public static long[] nTimes(long n, long toReplicate)
-
nTimes
public static <T> T[] nTimes(int n, T toReplicate, Class<T> tClass)
-
allUnique
public static boolean allUnique(int[] toTest)
Returns true if all of the elements in the given int array are unique- Parameters:
toTest
- the array to test- Returns:
- true if all o fthe items are unique false otherwise
-
randomPermutation
public static int[] randomPermutation(int size)
Credit to mikio braun from jblas Create a random permutation of the numbers 0, ..., size - 1. see Algorithm P, D.E. Knuth: The Art of Computer Programming, Vol. 2, p. 145
-
toBFloat16
public static short toBFloat16(float data)
-
toBFloat16
public static short toBFloat16(double data)
-
toHalf
public static short toHalf(float data)
-
toHalf
public static short toHalf(double data)
-
toHalfs
public static short[] toHalfs(float[] data)
-
toHalfs
public static short[] toHalfs(int[] data)
-
toHalfs
public static short[] toHalfs(long[] data)
-
toBfloats
public static short[] toBfloats(float[] data)
-
toBfloats
public static short[] toBfloats(int[] data)
-
toBfloats
public static short[] toBfloats(long[] data)
-
toLongs
public static long[] toLongs(byte[] data)
-
toLongs
public static long[] toLongs(boolean[] data)
-
toLongs
public static long[] toLongs(short[] data)
-
toLongs
public static long[] toLongs(int[] data)
-
toLongs
public static long[] toLongs(float[] data)
-
toLongs
public static long[] toLongs(double[] data)
-
toHalfs
public static short[] toHalfs(double[] data)
-
fromFloat
public static short fromFloat(float v)
-
toInts
public static int[] toInts(float[] data)
-
toInts
public static int[] toInts(double[] data)
-
toBytes
public static byte[] toBytes(int[] array)
-
toBytes
public static byte[] toBytes(float[] array)
-
toBytes
public static byte[] toBytes(double[] array)
-
toBytes
public static byte[] toBytes(long[] array)
-
toInts
public static int[] toInts(long[] array)
-
mod
public static int[] mod(int[] input, int mod)
-
offsetFor
public static int offsetFor(int[] stride, int i)
Calculate the offset for a given stride array- Parameters:
stride
- the stride to usei
- the offset to calculate for- Returns:
- the offset for the given stride
-
sum
public static int sum(List<Integer> add)
Sum of an int array- Parameters:
add
- the elements to calculate the sum for- Returns:
- the sum of this array
-
sum
public static int sum(int[] add)
Sum of an int array- Parameters:
add
- the elements to calculate the sum for- Returns:
- the sum of this array
-
sumLong
public static long sumLong(long... add)
-
prod
public static int prod(List<Integer> mult)
Product of an int array- Parameters:
mult
- the elements to calculate the sum for- Returns:
- the product of this array
-
prod
public static int prod(long... mult)
Product of an int array- Parameters:
mult
- the elements to calculate the sum for- Returns:
- the product of this array
-
prod
public static int prod(int... mult)
Product of an int array- Parameters:
mult
- the elements to calculate the sum for- Returns:
- the product of this array
-
prodLong
public static long prodLong(List<? extends Number> mult)
Product of an int array- Parameters:
mult
- the elements to calculate the sum for- Returns:
- the product of this array
-
prodLong
public static long prodLong(int... mult)
Product of an int array- Parameters:
mult
- the elements to calculate the sum for- Returns:
- the product of this array
-
prodLong
public static long prodLong(long... mult)
-
equals
public static boolean equals(float[] data, double[] data2)
-
consArray
public static int[] consArray(int a, int[] as)
-
isZero
public static boolean isZero(int[] as)
Returns true if any of the elements are zero- Parameters:
as
-- Returns:
-
isZero
public static boolean isZero(long[] as)
-
anyMore
public static boolean anyMore(int[] target, int[] test)
-
anyLess
public static boolean anyLess(int[] target, int[] test)
-
lessThan
public static boolean lessThan(int[] target, int[] test)
-
greaterThan
public static boolean greaterThan(int[] target, int[] test)
-
calcOffset
public static int calcOffset(List<Integer> shape, List<Integer> offsets, List<Integer> strides)
Compute the offset based on teh shape strides and offsets- Parameters:
shape
- the shape to computeoffsets
- the offsets to computestrides
- the strides to compute- Returns:
- the offset for the given shape,offset,and strides
-
calcOffset
public static int calcOffset(int[] shape, int[] offsets, int[] strides)
Compute the offset based on teh shape strides and offsets- Parameters:
shape
- the shape to computeoffsets
- the offsets to computestrides
- the strides to compute- Returns:
- the offset for the given shape,offset,and strides
-
calcOffset
public static long calcOffset(long[] shape, long[] offsets, long[] strides)
Compute the offset based on teh shape strides and offsets- Parameters:
shape
- the shape to computeoffsets
- the offsets to computestrides
- the strides to compute- Returns:
- the offset for the given shape,offset,and strides
-
calcOffsetLong
public static long calcOffsetLong(List<Integer> shape, List<Integer> offsets, List<Integer> strides)
Compute the offset based on teh shape strides and offsets- Parameters:
shape
- the shape to computeoffsets
- the offsets to computestrides
- the strides to compute- Returns:
- the offset for the given shape,offset,and strides
-
calcOffsetLong2
public static long calcOffsetLong2(List<Long> shape, List<Long> offsets, List<Long> strides)
-
calcOffsetLong
public static long calcOffsetLong(int[] shape, int[] offsets, int[] strides)
Compute the offset based on teh shape strides and offsets- Parameters:
shape
- the shape to computeoffsets
- the offsets to computestrides
- the strides to compute- Returns:
- the offset for the given shape,offset,and strides
-
dotProduct
public static int dotProduct(List<Integer> xs, List<Integer> ys)
- Parameters:
xs
-ys
-- Returns:
-
dotProduct
public static int dotProduct(int[] xs, int[] ys)
- Parameters:
xs
-ys
-- Returns:
-
dotProductLong
public static long dotProductLong(List<Integer> xs, List<Integer> ys)
- Parameters:
xs
-ys
-- Returns:
-
dotProductLong2
public static long dotProductLong2(List<Long> xs, List<Long> ys)
- Parameters:
xs
-ys
-- Returns:
-
dotProductLong
public static long dotProductLong(int[] xs, int[] ys)
- Parameters:
xs
-ys
-- Returns:
-
empty
public static int[] empty()
-
of
public static int[] of(int... arr)
-
copy
public static int[] copy(int[] copy)
-
copy
public static long[] copy(long[] copy)
-
doubleCopyOf
public static double[] doubleCopyOf(float[] data)
-
floatCopyOf
public static float[] floatCopyOf(double[] data)
-
range
public static double[] range(double[] data, int to)
Returns a subset of an array from 0 to "to" (exclusive)- Parameters:
data
- the data to getFromOrigin a subset ofto
- the end point of the data- Returns:
- the subset of the data specified
-
range
public static double[] range(double[] data, int to, int stride)
Returns a subset of an array from 0 to "to" (exclusive) using the specified stride- Parameters:
data
- the data to getFromOrigin a subset ofto
- the end point of the datastride
- the stride to go through the array- Returns:
- the subset of the data specified
-
range
public static double[] range(double[] data, int to, int stride, int numElementsEachStride)
Returns a subset of an array from 0 to "to" using the specified stride- Parameters:
data
- the data to getFromOrigin a subset ofto
- the end point of the datastride
- the stride to go through the arraynumElementsEachStride
- the number of elements to collect at each stride- Returns:
- the subset of the data specified
-
range
public static int[] range(int from, int to, int increment)
Generate an int array ranging from "from" to "to". The total number of elements is (from-to)/increment - i.e., range(0,2,1) returns [0,1] If from is > to this method will count backwards- Parameters:
from
- the fromto
- the end point of the dataincrement
- the amount to increment by- Returns:
- the int array with a length equal to absoluteValue(from - to)
-
range
public static long[] range(long from, long to, long increment)
-
range
public static int[] range(int from, int to)
Generate an int array ranging from "from" to "to". The total number of elements is (from-to) - i.e., range(0,2) returns [0,1] If from is > to this method will count backwards- Parameters:
from
- the fromto
- the end point of the data- Returns:
- the int array with a length equal to absoluteValue(from - to)
-
range
public static long[] range(long from, long to)
-
toDoubles
public static double[] toDoubles(int[] ints)
-
toDoubles
public static double[] toDoubles(long[] ints)
-
toDoubles
public static double[] toDoubles(float[] ints)
-
toFloats
public static float[] toFloats(int[][] ints)
-
toDoubles
public static double[] toDoubles(int[][] ints)
-
toShorts
public static short[] toShorts(long[] ints)
-
toShorts
public static short[] toShorts(int[] ints)
-
toShorts
public static short[] toShorts(float[] ints)
-
toShorts
public static short[] toShorts(double[] ints)
-
toFloats
public static float[] toFloats(int[] ints)
-
toFloats
public static float[] toFloats(long[] ints)
-
toFloats
public static float[] toFloats(double[] ints)
-
cutBelowZero
public static int[] cutBelowZero(int[] data)
-
cutBelowZero
public static long[] cutBelowZero(long[] data)
-
cutBelowZero
public static short[] cutBelowZero(short[] data)
-
cutBelowZero
public static byte[] cutBelowZero(byte[] data)
-
replace
public static int[] replace(int[] data, int index, int newValue)
Return a copy of this array with the given index omitted- Parameters:
data
- the data to copyindex
- the index of the item to removenewValue
- the newValue to replace- Returns:
- the new array with the omitted item
-
keep
public static int[] keep(int[] data, int... index)
Return a copy of this array with only the given index(es) remaining- Parameters:
data
- the data to copyindex
- the index of the item to remove- Returns:
- the new array with the omitted item
-
keep
public static long[] keep(long[] data, int... index)
Return a copy of this array with only the given index(es) remaining- Parameters:
data
- the data to copyindex
- the index of the item to remove- Returns:
- the new array with the omitted item
-
removeIndex
public static int[] removeIndex(int[] data, int... index)
Return a copy of this array with the given index omitted PLEASE NOTE: index to be omitted must exist in source array.- Parameters:
data
- the data to copyindex
- the index of the item to remove- Returns:
- the new array with the omitted item
-
removeIndex
public static long[] removeIndex(long[] data, int... index)
-
zip
public static int[][] zip(int[] as, int[] bs)
Zip 2 arrays in to:- Parameters:
as
-bs
-- Returns:
-
getTensorMmulShape
public static long[] getTensorMmulShape(long[] aShape, long[] bShape, int[][] axes)
Get the tensor matrix multiply shape- Parameters:
aShape
- the shape of the first arraybShape
- the shape of the second arrayaxes
- the axes to do the multiply- Returns:
- the shape for tensor matrix multiply
-
permute
public static int[] permute(int[] shape, int[] dimensions)
Permute the given input switching the dimensions of the input shape array with in the order of the specified dimensions- Parameters:
shape
- the shape to permutedimensions
- the dimensions- Returns:
-
permute
public static long[] permute(long[] shape, int[] dimensions)
-
argsort
public static int[] argsort(int[] a)
Original credit: https://github.com/alberts/array4j/blob/master/src/main/java/net/lunglet/util/ArrayUtils.java- Parameters:
a
-- Returns:
-
argsort
public static int[] argsort(int[] a, boolean ascending)
- Parameters:
a
-ascending
-- Returns:
-
convertNegativeIndices
public static int[] convertNegativeIndices(int range, int[] axes)
Convert all dimensions in the specified axes array to be positive based on the specified range of values- Parameters:
range
-axes
-- Returns:
-
copyOfRangeFrom
public static int[] copyOfRangeFrom(int length, int from, int to)
Generate an array from 0 to length and generate take a subset- Parameters:
length
- the length to generate tofrom
- the begin of the interval to taketo
- the end of the interval to take- Returns:
- the generated array
-
toByteArray
public static byte[] toByteArray(double[] doubleArray)
- Parameters:
doubleArray
-- Returns:
-
toDoubleArray
public static double[] toDoubleArray(byte[] byteArray)
- Parameters:
byteArray
-- Returns:
-
toByteArray
public static byte[] toByteArray(float[] doubleArray)
- Parameters:
doubleArray
-- Returns:
-
toLongArray
public static long[] toLongArray(int[] intArray)
-
toLongArray
public static long[] toLongArray(float[] array)
-
toFloatArray
public static float[] toFloatArray(byte[] byteArray)
- Parameters:
byteArray
-- Returns:
-
toByteArray
public static byte[] toByteArray(int[] intArray)
- Parameters:
intArray
-- Returns:
-
toIntArray
public static int[] toIntArray(byte[] byteArray)
- Parameters:
byteArray
-- Returns:
-
removeIndex
public static int[] removeIndex(int[] data, int index)
Return a copy of this array with the given index omitted- Parameters:
data
- the data to copyindex
- the index of the item to remove- Returns:
- the new array with the omitted item
-
removeIndex
public static long[] removeIndex(long[] data, int index)
-
valueStartingAt
public static int[] valueStartingAt(int valueStarting, int[] copy, int idxFrom, int idxAt, int length)
Create a copy of the given array starting at the given index with the given length. The intent here is for striding. For example in slicing, you want the major stride to be first. You achieve this by taking the last index of the matrix's stride and putting this as the first stride of the new ndarray for slicing. All of the elements except the copied elements are initialized as the given value- Parameters:
valueStarting
- the starting valuecopy
- the array to copyidxFrom
- the index to start at in the from arrayidxAt
- the index to start at in the return arraylength
- the length of the array to create- Returns:
- the given array
-
removeIndex
public static Integer[] removeIndex(Integer[] data, int index)
Returns the array with the item in index removed, if the array is empty it will return the array itself- Parameters:
data
- the data to remove data fromindex
- the index of the item to remove- Returns:
- a copy of the array with the removed item, or the array itself if empty
-
calcStridesFortran
public static int[] calcStridesFortran(int[] shape, int startNum)
Computes the standard packed array strides for a given shape.- Parameters:
shape
- the shape of a matrix:startNum
- the start number for the strides- Returns:
- the strides for a matrix of n dimensions
-
calcStridesFortran
public static long[] calcStridesFortran(long[] shape, int startNum)
Computes the standard packed array strides for a given shape.- Parameters:
shape
- the shape of a matrix:startNum
- the start number for the strides- Returns:
- the strides for a matrix of n dimensions
-
calcStridesFortran
public static int[] calcStridesFortran(int[] shape)
Computes the standard packed array strides for a given shape.- Parameters:
shape
- the shape of a matrix:- Returns:
- the strides for a matrix of n dimensions
-
calcStridesFortran
public static long[] calcStridesFortran(long[] shape)
-
calcStrides
public static int[] calcStrides(int[] shape, int startValue)
Computes the standard packed array strides for a given shape.- Parameters:
shape
- the shape of a matrix:startValue
- the startValue for the strides- Returns:
- the strides for a matrix of n dimensions
-
calcStrides
public static long[] calcStrides(long[] shape, int startValue)
Computes the standard packed array strides for a given shape.- Parameters:
shape
- the shape of a matrix:startValue
- the startValue for the strides- Returns:
- the strides for a matrix of n dimensions
-
isInverse
public static boolean isInverse(int[] first, int[] second)
Returns true if the given two arrays are reverse copies of each other- Parameters:
first
-second
-- Returns:
-
plus
public static int[] plus(int[] ints, int mult)
-
plus
public static int[] plus(int[] ints, int[] mult)
-
times
public static int[] times(int[] ints, int mult)
-
times
public static int[] times(int[] ints, int[] mult)
-
nonOneStride
public static int nonOneStride(int[] arr)
For use with row vectors to ensure consistent strides with varying offsets- Parameters:
arr
- the array to get the stride for- Returns:
- the stride
-
calcStrides
public static int[] calcStrides(int[] shape)
Computes the standard packed array strides for a given shape.- Parameters:
shape
- the shape of a matrix:- Returns:
- the strides for a matrix of n dimensions
-
calcStrides
public static long[] calcStrides(long[] shape)
-
reverseCopy
public static int[] reverseCopy(int[] e)
Create a backwards copy of the given array- Parameters:
e
- the array to createComplex a reverse clone of- Returns:
- the reversed copy
-
reverseCopy
public static long[] reverseCopy(long[] e)
-
read
public static double[] read(int length, DataInputStream dis) throws IOException
- Throws:
IOException
-
write
public static void write(double[] data, DataOutputStream dos) throws IOException
- Throws:
IOException
-
readDouble
public static double[] readDouble(int length, DataInputStream dis) throws IOException
- Throws:
IOException
-
readFloat
public static float[] readFloat(int length, DataInputStream dis) throws IOException
- Throws:
IOException
-
write
public static void write(float[] data, DataOutputStream dos) throws IOException
- Throws:
IOException
-
assertSquare
public static void assertSquare(double[]... d)
-
multiplyBy
public static void multiplyBy(int[] arr, int mult)
Multiply the given array by the given scalar- Parameters:
arr
- the array to multilymult
- the scalar to multiply by
-
reverse
public static void reverse(int[] e)
Reverse the passed in array in place- Parameters:
e
- the array to reverse
-
reverse
public static void reverse(long[] e)
-
zerosMatrix
public static List<double[]> zerosMatrix(long... dimensions)
-
zerosMatrix
public static List<double[]> zerosMatrix(int... dimensions)
-
reverseCopy
public static float[] reverseCopy(float[] e)
-
reverseCopy
public static <E> E[] reverseCopy(E[] e)
-
reverse
public static <E> void reverse(E[] e)
-
flatten
public static boolean[] flatten(boolean[][] arr)
-
flatten
public static boolean[] flatten(boolean[][][] arr)
-
flatten
public static float[] flatten(float[][] arr)
-
flatten
public static float[] flatten(float[][][] arr)
-
flatten
public static double[] flatten(double[][][] arr)
-
flatten
public static int[] flatten(int[][][] arr)
-
flatten
public static short[] flatten(short[][][] arr)
-
flatten
public static byte[] flatten(byte[][][] arr)
-
flatten
public static long[] flatten(long[][][][] arr)
-
flatten
public static short[] flatten(short[][][][] arr)
-
flatten
public static byte[] flatten(byte[][][][] arr)
-
flatten
public static boolean[] flatten(boolean[][][][] arr)
-
flatten
public static float[] flatten(float[][][][] arr)
-
flatten
public static double[] flatten(double[][][][] arr)
-
flatten
public static int[] flatten(int[][][][] arr)
-
flatten
public static int[] flatten(int[][] arr)
-
flatten
public static short[] flatten(short[][] arr)
-
flatten
public static byte[] flatten(byte[][] arr)
-
flatten
public static long[] flatten(long[][] arr)
-
flatten
public static long[] flatten(long[][][] arr)
-
flatten
public static double[] flatten(double[][] arr)
Convert a 2darray in to a flat array (row wise)- Parameters:
arr
- the array to flatten- Returns:
- a flattened representation of the array
-
flattenF
public static double[] flattenF(double[][] arr)
Convert a 2darray in to a flat array (row wise)- Parameters:
arr
- the array to flatten- Returns:
- a flattened representation of the array
-
flattenF
public static float[] flattenF(float[][] arr)
-
flattenF
public static int[] flattenF(int[][] arr)
-
flattenF
public static long[] flattenF(long[][] arr)
-
reshapeInt
public static int[][] reshapeInt(int[] in, int rows, int cols)
-
reshapeInt
public static int[][][] reshapeInt(int[] in, int d0, int d1, int d2)
-
reshapeDouble
public static double[][] reshapeDouble(double[] in, int rows, int cols)
-
reshapeDouble
public static double[][][] reshapeDouble(double[] in, int d0, int d1, int d2)
-
reshapeLong
public static long[][] reshapeLong(long[] in, int rows, int cols)
-
reshapeLong
public static long[][][] reshapeLong(long[] in, int d0, int d1, int d2)
-
reshapeBoolean
public static boolean[][] reshapeBoolean(boolean[] in, int rows, int cols)
-
reshapeBoolean
public static boolean[][][] reshapeBoolean(boolean[] in, int d0, int d1, int d2)
-
reshapeObject
public static <T> T[][] reshapeObject(T[] in, int rows, int cols)
-
reshapeObject
public static <T> T[][][] reshapeObject(T[] in, int d0, int d1, int d2)
-
toDouble
public static double[][] toDouble(int[][] arr)
Cast an int array to a double array- Parameters:
arr
- the array to cast- Returns:
- the elements of this array cast as an int
-
combineFloat
public static float[] combineFloat(List<float[]> nums)
Combines a applyTransformToDestination of int arrays in to one flat int array- Parameters:
nums
- the int arrays to combineDouble- Returns:
- one combined int array
-
combine
public static float[] combine(List<float[]> nums)
Combines a apply of int arrays in to one flat int array- Parameters:
nums
- the int arrays to combineDouble- Returns:
- one combined int array
-
combineDouble
public static double[] combineDouble(List<double[]> nums)
Combines a apply of int arrays in to one flat int array- Parameters:
nums
- the int arrays to combineDouble- Returns:
- one combined int array
-
combine
public static double[] combine(float[]... ints)
Combines a apply of int arrays in to one flat int array- Parameters:
ints
- the int arrays to combineDouble- Returns:
- one combined int array
-
combine
public static int[] combine(int[]... ints)
Combines a apply of int arrays in to one flat int array- Parameters:
ints
- the int arrays to combineDouble- Returns:
- one combined int array
-
combine
public static long[] combine(long[]... ints)
Combines a apply of long arrays in to one flat long array- Parameters:
ints
- the int arrays to combineDouble- Returns:
- one combined int array
-
combine
public static <E> E[] combine(E[]... arrs)
-
toOutcomeArray
public static int[] toOutcomeArray(int outcome, int numOutcomes)
-
toDouble
public static double[] toDouble(int[] data)
-
toDouble
public static double[] toDouble(long[] data)
-
copy
public static float[] copy(float[] data)
-
copy
public static double[] copy(double[] data)
-
flattenDoubleArray
public static double[] flattenDoubleArray(Object doubleArray)
Convert an arbitrary-dimensional rectangular double array to flat vector.
Can pass double[], double[][], double[][][], etc.
-
flattenFloatArray
public static float[] flattenFloatArray(Object floatArray)
Convert an arbitrary-dimensional rectangular float array to flat vector.
Can pass float[], float[][], float[][][], etc.
-
arrayShape
public static int[] arrayShape(Object array)
Calculate the shape of an arbitrary multi-dimensional array. Assumes:
(a) array is rectangular (not ragged) and first elements (i.e., array[0][0][0]...) are non-null
(b) First elements have > 0 length. So array[0].length > 0, array[0][0].length > 0, etc.
Can pass any Java array opType: double[], Object[][][], float[][], etc.
Length of returned array is number of dimensions; returned[i] is size of ith dimension.
-
arrayShape
public static int[] arrayShape(Object array, boolean allowSize0Dims)
Calculate the shape of an arbitrary multi-dimensional array.
Note that the method assumes the array is rectangular (not ragged) and first elements (i.e., array[0][0][0]...) are non-null
Note also that if allowSize0Dims is true, any elements are length 0, all subsequent dimensions will be reported as 0. i.e., a double[3][0][2] would be reported as shape [3,0,0]. If allowSize0Dims is false, an exception will be thrown for this case instead. Can pass any Java array opType: double[], Object[][][], float[][], etc.
Length of returned array is number of dimensions; returned[i] is size of ith dimension.
-
max
public static int max(int[] in)
Returns the maximum value in the array
-
min
public static int min(int[] in)
Returns the minimum value in the array
-
argMax
public static int argMax(int[] in)
Returns the index of the maximum value in the array. If two entries have same maximum value, index of the first one is returned.
-
argMin
public static int argMin(int[] in)
Returns the index of the minimum value in the array. If two entries have same minimum value, index of the first one is returned.
-
argMax
public static int argMax(long[] in)
Returns the index of the maximum value in the array. If two entries have same maximum value, index of the first one is returned.
-
argMin
public static int argMin(long[] in)
Returns the index of the minimum value in the array. If two entries have same minimum value, index of the first one is returned.
-
buildHalfVector
public static int[] buildHalfVector(Random rng, int length)
- Returns:
-
buildInterleavedVector
public static int[] buildInterleavedVector(Random rng, int length)
-
buildInterleavedVector
public static long[] buildInterleavedVector(Random rng, long length)
-
swap
protected static <T> void swap(List<T> objects, int idxA, int idxB)
-
shuffleWithMap
public static <T> void shuffleWithMap(List<T> objects, int[] map)
-
argMinOfMax
public static int argMinOfMax(int[] first, int[] second)
-
argMinOfMax
public static long argMinOfMax(long[] first, long[] second)
-
argMinOfMax
public static int argMinOfMax(int[]... arrays)
-
argMinOfMax
public static long argMinOfMax(long[]... arrays)
-
argMinOfSum
public static int argMinOfSum(int[] first, int[] second)
-
sortMapByValue
public static <K,V extends Comparable<? super V>> Map<K,V> sortMapByValue(Map<K,V> map)
-
getRandomElement
public static <T> T getRandomElement(List<T> list)
-
fromBoolean
public static int fromBoolean(boolean bool)
Convert an int- Parameters:
bool
-- Returns:
-
toPrimitives
public static long[] toPrimitives(Long[] array)
-
toPrimitives
public static int[] toPrimitives(Integer[] array)
-
toPrimitives
public static short[] toPrimitives(Short[] array)
-
toPrimitives
public static byte[] toPrimitives(Byte[] array)
-
toPrimitives
public static float[] toPrimitives(Float[] array)
-
toPrimitives
public static double[] toPrimitives(Double[] array)
-
toPrimitives
public static boolean[] toPrimitives(Boolean[] array)
-
toPrimitives
public static long[][] toPrimitives(Long[][] array)
-
toPrimitives
public static int[][] toPrimitives(Integer[][] array)
-
toPrimitives
public static short[][] toPrimitives(Short[][] array)
-
toPrimitives
public static byte[][] toPrimitives(Byte[][] array)
-
toPrimitives
public static double[][] toPrimitives(Double[][] array)
-
toPrimitives
public static float[][] toPrimitives(Float[][] array)
-
toPrimitives
public static boolean[][] toPrimitives(Boolean[][] array)
-
toPrimitives
public static long[][][] toPrimitives(Long[][][] array)
-
toPrimitives
public static int[][][] toPrimitives(Integer[][][] array)
-
toPrimitives
public static short[][][] toPrimitives(Short[][][] array)
-
toPrimitives
public static byte[][][] toPrimitives(Byte[][][] array)
-
toPrimitives
public static double[][][] toPrimitives(Double[][][] array)
-
toPrimitives
public static float[][][] toPrimitives(Float[][][] array)
-
toPrimitives
public static boolean[][][] toPrimitives(Boolean[][][] array)
-
toPrimitives
public static long[][][][] toPrimitives(Long[][][][] array)
-
toPrimitives
public static int[][][][] toPrimitives(Integer[][][][] array)
-
toPrimitives
public static short[][][][] toPrimitives(Short[][][][] array)
-
toPrimitives
public static byte[][][][] toPrimitives(Byte[][][][] array)
-
toPrimitives
public static double[][][][] toPrimitives(Double[][][][] array)
-
toPrimitives
public static float[][][][] toPrimitives(Float[][][][] array)
-
toPrimitives
public static boolean[][][][] toPrimitives(Boolean[][][][] array)
-
assertNotRagged
public static <T> void assertNotRagged(T[] array)
Assert that the specified array is not ragged (i.e., is rectangular).
Can be used to check Object arrays with any number of dimensions (up to rank 4), or primitive arrays with rank 2 or higher
An IllegalStateException is thrown if the array is ragged- Parameters:
array
- Array to check
-
arrayLength
public static int arrayLength(Object current)
Calculate the length of the object or primitive array. If- Parameters:
current
-- Returns:
-
invertPermutation
public static int[] invertPermutation(int... input)
Compute the inverse permutation indices for a permutation operation
Example: if input is [2, 0, 1] then output is [1, 2, 0]
The idea is that x.permute(input).permute(invertPermutation(input)) == x- Parameters:
input
- 1D indices for permutation- Returns:
- 1D inverted permutation
-
invertPermutation
public static long[] invertPermutation(long... input)
- Parameters:
input
- 1D indices for permutation- Returns:
- 1D inverted permutation
- See Also:
invertPermutation(int...)
-
isEmptyShape
public static boolean isEmptyShape(long[] shape)
Is this shape an empty shape? Shape is considered to be an empty shape if it contains any zeros. Note: a length 0 shape is NOT considered empty (it's rank 0 scalar)- Parameters:
shape
- Shape to check- Returns:
- True if shape contains zeros
-
isEmptyShape
public static boolean isEmptyShape(int[] shape)
Is this shape an empty shape? Shape is considered to be an empty shape if it contains any zeros. Note: a length 0 shape is NOT considered empty (it's rank 0 scalar)- Parameters:
shape
- Shape to check- Returns:
- True if shape contains zeros
-
filterNull
public static <T> T[] filterNull(T... in)
-
-