Interface NdArray<T>
- Type Parameters:
T- the type of values to be mapped
- All Superinterfaces:
Shaped
- All Known Subinterfaces:
BooleanNdArray, ByteNdArray, DoubleNdArray, FloatNdArray, IntNdArray, LongNdArray, ShortNdArray, SparseNdArray<T,U>
- All Known Implementing Classes:
AbstractDenseNdArray, AbstractSparseNdArray, BooleanDenseNdArray, BooleanSparseNdArray, BooleanSparseSlice, ByteDenseNdArray, ByteSparseNdArray, ByteSparseSlice, DenseNdArray, DoubleDenseNdArray, DoubleSparseNdArray, DoubleSparseSlice, FloatDenseNdArray, FloatSparseNdArray, FloatSparseSlice, IntDenseNdArray, IntSparseNdArray, IntSparseSlice, LongDenseNdArray, LongSparseNdArray, LongSparseSlice, ObjectSparseSlice, ShortDenseNdArray, ShortSparseNdArray, ShortSparseSlice, SparseNdArray, SparseSlice
The `NdArray` interface creates an abstraction between the physical storage of a data record, which can be linear or segmented, and its logical representation. In general, they achieve better performances than standard multi-dimensional arrays in Java by mapping directly linear data segments in memory.
Like DataBuffer, NdArray instances support 64-bits indexing so they can be
used to map very large data records. They also support special coordinates that allow traversing
their values in any direction or to select only a subset of them.
Example of usage:
// Creates a 2x3x2 matrix (of rank 3)
FloatNdArray matrix3d = NdArrays.ofFloats(shape(2, 3, 2));
// Initialize sub-matrices data with vectors
matrix.set(NdArrays.vectorOf(1.0f, 2.0f), 0, 0)
.set(NdArrays.vectorOf(3.0f, 4.0f), 0, 1)
.set(NdArrays.vectorOf(5.0f, 6.0f), 0, 2)
.set(NdArrays.vectorOf(7.0f, 8.0f), 1, 0)
.set(NdArrays.vectorOf(9.0f, 10.0f), 1, 1)
.set(NdArrays.vectorOf(11.0f, 12.0f), 1, 2);
// Access the second 3x2 matrix (of rank 2)
FloatNdArray matrix = matrix3d.get(1);
// Access directly the float value at (1, 0) from the second matrix
assertEquals(9.0f, matrix.getFloat(1, 0));
-
Method Summary
Modifier and TypeMethodDescriptioncopyFrom(DataBuffer<T> src) Copy the content of the source buffer into this N-dimensional array.copyTo(DataBuffer<T> dst) Copy the content of this N-dimensional array into the destination buffer.Copy the content of this array to the destination array.NdArraySequence<? extends NdArray<T>> elements(int dimensionIdx) Returns a sequence of all elements at a given dimension.booleanChecks equality between n-dimensional arrays.get(long... coordinates) Returns the N-dimensional element of this array at the given coordinates.getObject(long... coordinates) Returns the value of the scalar found at the given coordinates.NdArraySequence<? extends NdArray<T>> scalars()Returns a sequence of all scalars in this array.Assigns the value of the N-dimensional element found at the given coordinates.Assigns the value of the scalar found at the given coordinates.Creates a multi-dimensional view (or slice) of this array by mapping one or more dimensions to the given index selectors.Retrieve all scalar values of this array as a stream of objects.Returns a new N-dimensional view of this array with the givenshape.
-
Method Details
-
elements
Returns a sequence of all elements at a given dimension.Logically, the N-dimensional array can be flatten in a single vector, where the scalars of the
(n - 1)th element precedes those of the(n)th element, for a total ofShaped.size()values.For example, given a
n x mmatrix on the[x, y]axes, elements are iterated in the following order:x0y0, x0y1, ..., x0ym-1, x1y0, x1y1, ..., xn-1ym-1
The returned sequence can then be iterated to visit each elements, either by calling
Iterable.forEach(Consumer)orNdArraySequence.forEachIndexed(BiConsumer).// Iterate matrix for initializing each of its vectors matrixOfFloats.elements(0).forEach(v -> { v.set(vector(1.0f, 2.0f, 3.0f)); }); // Iterate a vector for reading each of its scalar vectorOfFloats.scalars().forEachIdx((coords, s) -> { System.out.println("Value " + s.getFloat() + " found at " + coords); });- Parameters:
dimensionIdx- index of the dimension- Returns:
- an
NdArraysequence - Throws:
IllegalArgumentException- ifdimensionIdxis greater or equal to the total number of dimensions of this array
-
scalars
NdArraySequence<? extends NdArray<T>> scalars()Returns a sequence of all scalars in this array.This is equivalent to call
elements(shape().numDimensions() - 1)- Returns:
- an
NdArraysequence
-
withShape
Returns a new N-dimensional view of this array with the givenshape.The provided
shapemust comply to the following characteristics:- new shape is known (i.e. has no unknown dimension)
- new shape size is equal to the size of the current shape (i.e. same number of elements)
NdArrays.ofInts(Shape.scalar()).withShape(Shape.of(1, 1)); // ok NdArrays.ofInts(Shape.of(2, 3).withShape(Shape.of(3, 2)); // ok NdArrays.ofInts(Shape.scalar()).withShape(Shape.of(1, 2)); // not ok, sizes are different (1 != 2) NdArrays.ofInts(Shape.of(2, 3)).withShape(Shape.unknown()); // not ok, new shape unknownAny changes applied to the returned view affect the data of this array as well, as there is no copy involved.
- Parameters:
shape- the new shape to apply- Returns:
- a new array viewing the data according to the new shape, or this array if shapes are the same
- Throws:
IllegalArgumentException- if the providedshapeis not compliantUnsupportedOperationException- if this array does not support this operation
-
slice
Creates a multi-dimensional view (or slice) of this array by mapping one or more dimensions to the given index selectors.Slices allow to traverse an N-dimensional array in any of its axis and/or to filter only elements of interest. For example, for a given matrix on the
[x, y]axes, it is possible to iterate elements aty=0for allx.Any changes applied to the returned slice affect the data of this array as well, as there is no copy involved.
Example of usage:
FloatNdArray matrix3d = NdArrays.ofFloats(shape(3, 2, 4)); // with [x, y, z] axes // Iterates elements on the x axis by preserving only the 3rd value on the z axis, // (i.e. [x, y, 2]) matrix3d.slice(all(), all(), at(2)).elements(0).forEach(m -> { assertEquals(shape(2), m); // y=2, z=0 (scalar) }); // Creates a slice that contains only the last element of the y axis and elements with an // odd `z` coordinate. FloatNdArray slice = matrix3d.slice(all(), at(1), odd()); assertEquals(shape(3, 2), slice.shape()); // x=3, y=0 (scalar), z=2 (odd coordinates) // Iterates backward the elements on the x axis matrix3d.slice(flip()).elements(0).forEach(m -> { assertEquals(shape(2, 4), m); // y=2, z=4 });- Parameters:
indices- index selectors per dimensions, starting from dimension 0 of this array.- Returns:
- the element resulting of the index selection
- Throws:
IndexOutOfBoundsException- if some coordinates are outside the limits of their respective dimension
-
get
Returns the N-dimensional element of this array at the given coordinates.Elements of any of the dimensions of this array can be retrieved. For example, if the number of coordinates is equal to the number of dimensions of this array, then a rank-0 (scalar) array is returned, which value can then be obtained by calling `array.getObject()`.
Any changes applied to the returned elements affect the data of this array as well, as there is no copy involved.
Note that invoking this method is an equivalent and more efficient way to slice this array on single scalar, i.e.
array.get(x, y, z)is equal toarray.slice(at(x), at(y), at(z))- Parameters:
coordinates- coordinates of the element to access, none will return this array- Returns:
- the element at this index
- Throws:
IndexOutOfBoundsException- if some coordinates are outside the limits of their respective dimension
-
set
Assigns the value of the N-dimensional element found at the given coordinates.The number of coordinates provided can be anywhere between 0 and rank - 1. For example:
FloatNdArray matrix = NdArrays.ofFloats(shape(2, 2)); // matrix rank = 2 matrix.set(vector(10.0f, 20.0f), 0); // success matrix.set(scalar(10.0f), 1, 0); // success- Parameters:
src- an array of the values to assigncoordinates- coordinates of the element to assign- Returns:
- this array
- Throws:
IndexOutOfBoundsException- if some coordinates are outside the limits of their respective dimension
-
getObject
Returns the value of the scalar found at the given coordinates.To access the scalar element, the number of coordinates provided must be equal to the number of dimensions of this array (i.e. its rank). For example:
Note: if this array stores values of a primitive type, prefer the usage of the specialized method in the subclass for that type. For example,FloatNdArray matrix = NdArrays.ofFloats(shape(2, 2)); // matrix rank = 2 matrix.getObject(0, 1); // succeeds, returns 0.0f matrix.getObject(0); // throws IllegalRankException FloatNdArray scalar = matrix.get(0, 1); // scalar rank = 0 scalar.getObject(); // succeeds, returns 0.0ffloatArray.getFloat(0);.- Parameters:
coordinates- coordinates of the scalar to resolve- Returns:
- value of that scalar
- Throws:
IndexOutOfBoundsException- if some coordinates are outside the limits of their respective dimensionIllegalRankException- if number of coordinates is not sufficient to access a scalar element
-
setObject
Assigns the value of the scalar found at the given coordinates.To access the scalar element, the number of coordinates provided must be equal to the number of dimensions of this array (i.e. its rank). For example:
Note: if this array stores values of a primitive type, prefer the usage of the specialized method in the subclass for that type. For example,FloatNdArray matrix = NdArrays.ofFloats(shape(2, 2)); // matrix rank = 2 matrix.setObject(10.0f, 0, 1); // succeeds matrix.setObject(10.0f, 0); // throws IllegalRankException FloatNdArray scalar = matrix.get(0, 1); // scalar rank = 0 scalar.setObject(10.0f); // succeedsfloatArray.setFloat(10.0f, 0);- Parameters:
value- the value to assigncoordinates- coordinates of the scalar to assign- Returns:
- this array
- Throws:
IndexOutOfBoundsException- if some coordinates are outside the limits of their respective dimensionIllegalRankException- if number of coordinates is not sufficient to access a scalar element
-
streamOfObjects
-
copyTo
Copy the content of this array to the destination array.The
Shaped.shape()of the destination array must be equal to the shape of this array, or an exception is thrown. After the copy, the content of both arrays can be altered independently, without affecting each other.- Parameters:
dst- array to receive a copy of the content of this array- Returns:
- this array
- Throws:
IllegalArgumentException- if the shape ofdstis not equal to the shape of this array
-
copyTo
Copy the content of this N-dimensional array into the destination buffer.The size of the buffer must be equal or greater to the
Shaped.size()of this array, or an exception is thrown. After the copy, content of the buffer and of the array can be altered independently, without affecting each other.Note: in version 0.4.0 and earlier, this method was named
read(DataBuffer<T>). It has been renamed to explicitly indicate the direction of the data flow to avoid confusion.- Parameters:
dst- the destination buffer- Returns:
- this array
- Throws:
BufferOverflowException- if the buffer cannot hold the content of this array- See Also:
-
copyFrom
Copy the content of the source buffer into this N-dimensional array.The size of the buffer must be equal or greater to the
Shaped.size()of this array, or an exception is thrown. After the copy, content of the buffer and of the array can be altered independently, without affecting each other.Note: in version 0.4.0 and earlier, this method was named
write(DataBuffer<T>). It has been renamed to explicitly indicate the direction of the data flow to avoid confusion.- Parameters:
src- the source buffer- Returns:
- this array
- Throws:
BufferUnderflowException- if the buffer has not enough remaining data to write into this array- See Also:
-
equals
Checks equality between n-dimensional arrays.An array is equal to another object if this object is another
NdArrayof the same shape, type and the elements are equal and in the same order. For example:IntNdArray array = NdArrays.ofInts(Shape.of(2, 2)) .set(NdArrays.vectorOf(1, 2), 0) .set(NdArrays.vectorOf(3, 4), 1); assertEquals(array, StdArrays.ndCopyOf(new int[][] {{1, 2}, {3, 4}})); // true assertEquals(array, StdArrays.ndCopyOf(new Integer[][] {{1, 2}, {3, 4}})); // true, as Integers are equal to ints assertNotEquals(array, NdArrays.vectorOf(1, 2, 3, 4)); // false, different shapes assertNotEquals(array, StdArrays.ndCopyOf(new int[][] {{3, 4}, {1, 2}})); // false, different order assertNotEquals(array, StdArrays.ndCopyOf(new long[][] {{1L, 2L}, {3L, 4L}})); // false, different typesNote that the computation required to verify equality between two arrays can be expensive in some cases and therefore, it is recommended to not use this method in a critical path where performances matter.
-