Class SparseNdArray<T, U extends NdArray<T>>
- All Implemented Interfaces:
NdArray<T>, Shaped, SparseNdArray<T,U>
A sparse array has two separate dense arrays: indices, values, and a shape that represents the dense shape.
NOTE: all Sparse Arrays are readonly for the AbstractSparseNdArray.set(NdArray, long...) and
AbstractSparseNdArray.setObject(Object, long...) methods
SparseNdArray<String> st = new SparseNdArray<>(
StdArrays.of(new long[][] {{0, 0}, {1, 2}}),
NdArrays.vectorOf("first", "second"),
Shape.of(3, 4));
represents the dense array:
[[true, false, false, false]
[false, false, true, false]
[false, false, false, false]]
-
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedSparseNdArray(Class<T> type, LongNdArray indices, U values, T defaultValue, DimensionalSpace dimensions) Creates a SparseNdArray -
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.static <T, U extends NdArray<T>>
SparseNdArray<T, U> create(Class<T> type, DataBuffer<T> dataBuffer, DimensionalSpace dimensions) Creates a new SparseNdArray from a data bufferstatic <T, U extends NdArray<T>>
SparseNdArray<T, U> create(Class<T> type, DataBuffer<T> buffer, Shape shape) Creates a new empty SparseNdArray from a float data bufferstatic <T, U extends NdArray<T>>
SparseNdArray<T, U> create(Class<T> type, DataBuffer<T> dataBuffer, T defaultValue, DimensionalSpace dimensions) Creates a new SparseNdArray from a data bufferstatic <T, U extends NdArray<T>>
SparseNdArray<T, U> create(Class<T> type, DataBuffer<T> buffer, T defaultValue, Shape shape) Creates a new empty SparseNdArray from a float data bufferstatic <T, U extends NdArray<T>>
SparseNdArray<T, U> create(Class<T> type, DimensionalSpace dimensions) Creates a new empty SparseNdArray from a data bufferstatic <T, U extends NdArray<T>>
SparseNdArray<T, U> create(Class<T> type, LongNdArray indices, U values, DimensionalSpace dimensions) Creates a new SparseNdArraystatic <T, U extends NdArray<T>>
SparseNdArray<T, U> create(Class<T> type, LongNdArray indices, U values, T defaultValue, DimensionalSpace dimensions) Creates a new SparseNdArraystatic <T, U extends NdArray<T>>
SparseNdArray<T, U> create(Class<T> type, T defaultValue, DimensionalSpace dimensions) Creates a new empty SparseNdArray from a data bufferstatic <T, U extends NdArray<T>>
SparseNdArray<T, U> Creates a new SparseNdArray from a NdArraystatic <T, U extends NdArray<T>>
SparseNdArray<T, U> Creates a new SparseNdArray from a NdArrayCreates the NdArray with the default value as a scalarcreateValues(Shape shape) Creates a NdArray of the specified shapePopulates this sparse array from a dense arraygetType()Gets the class type for this sparse arrayscalars()Returns a sequence of all scalars in this array.shape()slice(long position, DimensionalSpace sliceDimensions) protected booleanslowEquals(NdArray<?> array) protected inttoDense()Converts the sparse array to a dense arraytoString()A String showing the type, default value, number of elements and the dense shape of this sparse ndarray.Methods inherited from class AbstractSparseNdArray
copyTo, elements, equals, get, getDefaultArray, getDefaultValue, getIndices, getIndicesCoordinates, getObject, getValues, hashCode, locateIndex, positionOf, set, setDefaultValue, setIndices, setObject, setValues, slice, slowCopyTo, sortIndicesAndValues, toCoordinates, withShapeMethods inherited from interface NdArray
copyTo, elements, equals, get, getObject, scalars, set, setObject, slice, streamOfObjects, withShapeMethods inherited from interface SparseNdArray
getIndices, getValues
-
Field Details
-
dimensions
-
-
Constructor Details
-
SparseNdArray
protected SparseNdArray(Class<T> type, LongNdArray indices, U values, T defaultValue, DimensionalSpace dimensions) Creates a SparseNdArray- Parameters:
indices- A 2-D LongNdArray of shape[N, ndims], that specifies the indices of the elements in the sparse array that contain non-default values (elements are zero-indexed). For example,indices=[[1,3], [2,4]]specifies that the elements with indexes of[1,3]and[2,4]have non-default values.values- A 1-D NdArray of Boolean type and shape[N], which supplies the values for each element in indices. For example, givenindices=[[1,3], [2,4]], the parametervalues=[18, 3.6]specifies that element[1,3]of the sparse NdArray has a value of18, and element[2,4]of the NdArray has a value of3.6.defaultValue- Scalar value to set for indices not specified inAbstractSparseNdArray.getIndices()dimensions- the dimensional space for the dense object represented by this sparse array,
-
-
Method Details
-
create
public static <T, U extends NdArray<T>> SparseNdArray<T,U> create(Class<T> type, LongNdArray indices, U values, DimensionalSpace dimensions) Creates a new SparseNdArray- Parameters:
indices- A 2-D LongNdArray of shape[N, ndims], that specifies the indices of the elements in the sparse array that contain non-default values (elements are zero-indexed). For example,indices=[[1,3], [2,4]]specifies that the elements with indexes of[1,3]and[2,4]have non-default values.values- A 1-D NdArray of any type and shape[N], which supplies the values for each element in indices. For example, givenindices=[[1,3], [2,4]], the parametervalues=[18, 3.6]specifies that element[1,3]of the sparse NdArray has a value of18, and element[2,4]of the NdArray has a value of3.6.dimensions- the dimensional space for the dense object represented by this sparse array.- Returns:
- the new Sparse Array
-
create
public static <T, U extends NdArray<T>> SparseNdArray<T,U> create(Class<T> type, LongNdArray indices, U values, T defaultValue, DimensionalSpace dimensions) Creates a new SparseNdArray- Parameters:
indices- A 2-D LongNdArray of shape[N, ndims], that specifies the indices of the elements in the sparse array that contain non-default values (elements are zero-indexed). For example,indices=[[1,3], [2,4]]specifies that the elements with indexes of[1,3]and[2,4]have non-default values.values- A 1-D NdArray of any type and shape[N], which supplies the values for each element in indices. For example, givenindices=[[1,3], [2,4]], the parametervalues=[18, 3.6]specifies that element[1,3]of the sparse NdArray has a value of18, and element[2,4]of the NdArray has a value of3.6.defaultValue- Scalar value to set for indices not specified inAbstractSparseNdArray.getIndices()dimensions- the dimensional space for the dense object represented by this sparse array.- Returns:
- the new Sparse Array
-
create
public static <T, U extends NdArray<T>> SparseNdArray<T,U> create(Class<T> type, DataBuffer<T> dataBuffer, DimensionalSpace dimensions) Creates a new SparseNdArray from a data buffer- Parameters:
dataBuffer- the databuffer containing the dense arraydimensions- the dimensional space for the sparse array- Returns:
- the new Sparse Array
-
create
public static <T, U extends NdArray<T>> SparseNdArray<T,U> create(Class<T> type, DataBuffer<T> dataBuffer, T defaultValue, DimensionalSpace dimensions) Creates a new SparseNdArray from a data buffer- Parameters:
dataBuffer- the databuffer containing the dense arraydefaultValue- Scalar value to set for indices not specified inAbstractSparseNdArray.getIndices()dimensions- the dimensional space for the sparse array- Returns:
- the new Sparse Array
-
create
public static <T, U extends NdArray<T>> SparseNdArray<T,U> create(Class<T> type, DimensionalSpace dimensions) Creates a new empty SparseNdArray from a data buffer- Parameters:
dimensions- the dimensions array- Returns:
- the new Sparse Array
-
create
public static <T, U extends NdArray<T>> SparseNdArray<T,U> create(Class<T> type, T defaultValue, DimensionalSpace dimensions) Creates a new empty SparseNdArray from a data buffer- Parameters:
defaultValue- Scalar value to set for indices not specified inAbstractSparseNdArray.getIndices()dimensions- the dimensions array- Returns:
- the new Sparse Array
-
create
public static <T, U extends NdArray<T>> SparseNdArray<T,U> create(Class<T> type, DataBuffer<T> buffer, Shape shape) Creates a new empty SparseNdArray from a float data buffer- Parameters:
buffer- the data buffershape- the shape of the sparse array.- Returns:
- the new Sparse Array
-
create
public static <T, U extends NdArray<T>> SparseNdArray<T,U> create(Class<T> type, DataBuffer<T> buffer, T defaultValue, Shape shape) Creates a new empty SparseNdArray from a float data buffer- Parameters:
buffer- the data bufferdefaultValue- Scalar value to set for indices not specified inAbstractSparseNdArray.getIndices()shape- the shape of the sparse array.- Returns:
- the new Sparse Array
-
create
Creates a new SparseNdArray from a NdArray- Parameters:
src- the NdArray- Returns:
- the new Sparse Array
-
create
public static <T, U extends NdArray<T>> SparseNdArray<T,U> create(Class<T> type, U src, T defaultValue) Creates a new SparseNdArray from a NdArray- Parameters:
src- the NdArraydefaultValue- Scalar value to set for indices not specified inAbstractSparseNdArray.getIndices()- Returns:
- the new Sparse Array
-
createDefaultArray
Creates the NdArray with the default value as a scalar- Specified by:
createDefaultArrayin classAbstractSparseNdArray<T, U extends NdArray<T>>- Returns:
- the default NdArray of the default value as a scalar
-
createValues
Creates a NdArray of the specified shape- Specified by:
createValuesin classAbstractSparseNdArray<T, U extends NdArray<T>>- Parameters:
shape- the shape of the dense array.- Returns:
- a NdArray of the specified shape
-
slice
-
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. -
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. -
toDense
-
fromDense
-
getType
-
toString
-
dimensions
-
shape
-
scalars
Description copied from interface:NdArrayReturns a sequence of all scalars in this array.This is equivalent to call
elements(shape().numDimensions() - 1) -
slowHashCode
protected int slowHashCode() -
slowEquals
-