Class AbstractNDArray<T,T2 extends Number>
- java.lang.Object
-
- io.github.hakkelt.ndarrays.internal.AbstractNDArray<T,T2>
-
- Direct Known Subclasses:
AbstractComplexNDArray,AbstractNDArrayView,AbstractRealNDArray
public abstract class AbstractNDArray<T,T2 extends Number> extends Object implements NDArray<T>
Base class for all NDArrays and NDArray views.
-
-
Constructor Summary
Constructors Constructor Description AbstractNDArray()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Taccumulate(BinaryOperator<T> func)Returns the result of accumulation of all elements in this NDArray.NDArray<T>apply(UnaryOperator<T> func)Apply the given function to each element of the array, and override each entry with the calculated new values.NDArray<T>applyOnSlices(BiFunction<NDArray<T>,int[],NDArray<?>> func, int... iterationDims)Apply the given function to each slices of the array, and override each entry with the returned slice.NDArray<T>applyWithCartesianIndices(BiFunction<T,int[],T> func)Apply the given function to each element of the array, and override each entry with the calculated new values.NDArray<T>applyWithLinearIndices(BiFunction<T,Integer,T> func)Apply the given function to each element of the array, and override each entry with the calculated new values.booleancontains(Object o)StringcontentToString()Returns a String containing tabular representation of the array.StringcontentToString(String format)Returns a String containing tabular representation of the array.NDArray<T>copy()Returns a copy of this NDArray.booleanequals(Object obj)Compares the specified object with this NDArray for equality.NDArray<T>fillUsingCartesianIndices(Function<int[],T> func)Apply the given function to each element of the array, and override each entry with the calculated new values.NDArray<T>fillUsingLinearIndices(IntFunction<T> func)Apply the given function to each element of the array, and override each entry with the calculated new values.voidforEach(Consumer<? super T> func)Apply the given function to each element of the array, and override each entry with the calculated new values.voidforEachWithCartesianIndices(BiConsumer<T,int[]> func)Apply the given function to each element of the array, and override each entry with the calculated new values.voidforEachWithLinearIndices(ObjIntConsumer<T> func)Apply the given function to each element of the array, and override each entry with the calculated new values.Tget(int linearIndex)Returns an element specified by linear indexing.Tget(int... indices)Returns an element specified by cartesian indexing.T2getImag(int linearIndex)T2getImag(int... indices)T2getReal(int linearIndex)T2getReal(int... indices)inthashCode()This method is unsupported because NDArrays should not be used as keys in hash-based collections.Iterator<T>iterator()Returns an iterator over the elements in this collection.intlength()Returns the number of elements within this NDArray.NDArray<T>mapOnSlices(BiFunction<NDArray<T>,int[],NDArray<?>> func, int... iterationDims)Apply the given function to each slices of the array, and create a new NDArray with the calculated new values.Stream<T>maybeParallelStream()Returns a sequential Stream with this collection as its source.intndim()Returns the number of dimensions.doublenorm()Returns the 2-norm (Euclidean norm) of the vectorized array.doublenorm(Double p)Returns the p-norm of the vectorized array.Stream<T>parallelStream()Returns a parallel Stream with this collection as its source.Tprod()Returns the product of all elements in this NDArray.NDArray<T>reduceSlices(BiFunction<NDArray<T>,int[],T> func, int... iterationDims)Reduces slices along the specified dimensions in this NDArray to scalar values, reducing the number of dimensions.voidset(T value, int linearIndex)Sets the value of an element specified by linear indexing.voidset(T value, int... indices)Sets the value of an element specified by cartesian indexing.voidsetImag(T2 value, int linearIndex)voidsetImag(T2 value, int... indices)voidsetReal(T2 value, int linearIndex)voidsetReal(T2 value, int... indices)int[]shape()Returns the dimensions of the array.intshape(int axis)Returns the shape of the array along a specific dimension/axis.NDArray<T>similar()Returns a new array of the same shape as this NDArray filled with zeros.Spliterator<T>spliterator()Creates a Spliterator over the elements in this collection.Stream<T>stream()Returns a sequential Stream with this collection as its source.Stream<int[]>streamCartesianIndices()Returns a stream of cartesian indices.IntStreamstreamLinearIndices()Returns a stream of linear indices.Tsum()Returns the sum of all elements in this NDArray.Object[]toArray()Returns an array containing all of the elements in this collection.<A> AtoArray(A array)Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array.<A> AtoArray(IntFunction<A> generator)Returns an array containing all of the elements in this collection, using the provided generator function to allocate the returned array.StringtoString()Returns a String containing type and shape information.voidwriteToFile(File file)Save content of the NDArray to the given file.-
Methods inherited from interface io.github.hakkelt.ndarrays.NDArray
accumulate, add, add, add, add, add, add, add, add, addInplace, addInplace, addInplace, addInplace, addInplace, addInplace, addInplace, addInplace, concatenate, copyFrom, copyFrom, copyFrom, copyFrom, copyFrom, copyFrom, copyFrom, copyFrom, dataTypeAsString, divide, divide, divide, divide, divide, divide, divide, divide, divideInplace, divideInplace, divideInplace, divideInplace, divideInplace, divideInplace, divideInplace, divideInplace, dropDims, dtype, fill, fill, getNamePrefix, inverseMask, map, mapWithCartesianIndices, mapWithLinearIndices, mask, mask, maskWithCartesianIndices, maskWithLinearIndices, multiply, multiply, multiply, multiply, multiply, multiply, multiply, multiply, multiplyInplace, multiplyInplace, multiplyInplace, multiplyInplace, multiplyInplace, multiplyInplace, multiplyInplace, multiplyInplace, norm, permuteDims, prod, reshape, selectDims, set, set, slice, squeeze, subtract, subtract, subtract, subtract, subtract, subtract, subtract, subtract, subtractInplace, subtractInplace, subtractInplace, subtractInplace, subtractInplace, subtractInplace, subtractInplace, subtractInplace, sum
-
-
-
-
Method Detail
-
similar
public NDArray<T> similar()
Description copied from interface:NDArrayReturns a new array of the same shape as this NDArray filled with zeros.
-
iterator
public Iterator<T> iterator()
Description copied from interface:NDArrayReturns an iterator over the elements in this collection. The iteration is done in column-first order similarly to linear indexing. This function is most likely not used directly, but it is required to be overloaded in order to make streaming of elements possible.
-
toArray
public Object[] toArray()
Description copied from interface:NDArrayReturns an array containing all of the elements in this collection. If this collection makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order. The returned array's runtime component type is Object.The returned array will be "safe" in that no references to it are maintained by this collection. (In other words, this method must allocate a new array even if this collection is backed by an array). The caller is thus free to modify the returned array.
-
toArray
public <A> A toArray(A array)
Description copied from interface:NDArrayReturns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array. If the collection fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the shape of this collection.If this collection fits in the specified array with room to spare (i.e., the array has more elements than this collection), the element in the array immediately following the end of the collection is set to null. (This is useful in determining the length of this collection only if the caller knows that this collection does not contain any null elements.)
If this collection makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order.
- Specified by:
toArrayin interfaceNDArray<T>- Type Parameters:
A- the component type of the array to contain the collection- Parameters:
array- the array into which the elements of this collection are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.- Returns:
- an array containing all of the elements in this collection
-
toArray
public <A> A toArray(IntFunction<A> generator)
Description copied from interface:NDArrayReturns an array containing all of the elements in this collection, using the provided generator function to allocate the returned array.If this collection makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order.
-
spliterator
public Spliterator<T> spliterator()
Description copied from interface:NDArrayCreates a Spliterator over the elements in this collection. The iteration is done in column-first order similarly to linear indexing, and the splits are done equally on the remaining elements. This function is most likely not used directly, but it is required to be overloaded in order to make parallel streaming of elements possible.- Specified by:
spliteratorin interfaceIterable<T>- Specified by:
spliteratorin interfaceNDArray<T>- Returns:
- a Spliterator over the elements in this collection
-
toString
public String toString()
Description copied from interface:NDArrayReturns a String containing type and shape information.
-
contentToString
public String contentToString()
Description copied from interface:NDArrayReturns a String containing tabular representation of the array. For arrays that have more than 2 dimensions, the function prints 2D slices and iterates over all other dimensions incrementally. Note: this function might produce enormous output as it doesn't truncate result even for large arrays!- Specified by:
contentToStringin interfaceNDArray<T>- Returns:
- a String containing tabular representation of the array.
-
contentToString
public String contentToString(String format)
Description copied from interface:NDArrayReturns a String containing tabular representation of the array. For arrays that have more than 2 dimensions, the function prints 2D slices and iterates over all other dimensions incrementally. Note: this function might produce enormous output as it doesn't truncate result even for large arrays!- Specified by:
contentToStringin interfaceNDArray<T>- Parameters:
format- format specifier for individual elements (e.g. 5.3f)- Returns:
- a String containing tabular representation of the array.
-
length
public int length()
Description copied from interface:NDArrayReturns the number of elements within this NDArray.
-
ndim
public int ndim()
Description copied from interface:NDArrayReturns the number of dimensions.
-
shape
public int[] shape()
Description copied from interface:NDArrayReturns the dimensions of the array.
-
shape
public int shape(int axis)
Description copied from interface:NDArrayReturns the shape of the array along a specific dimension/axis.
-
get
public T get(int linearIndex)
Description copied from interface:NDArrayReturns an element specified by linear indexing.Linear indexing: It selects the ith element using the column-major iteration order that linearly spans the entire array.
Negative indexing is supported, e.g. -1 refers to the last element, -2 refers to the item before the last one, etc.
-
getReal
public T2 getReal(int linearIndex)
-
getImag
public T2 getImag(int linearIndex)
-
get
public T get(int... indices)
Description copied from interface:NDArrayReturns an element specified by cartesian indexing.Cartesian indexing: The ordinary way to index into an N-dimensional array is to use exactly N indices; each index selects the position(s) in its particular dimension.
Negative indexing is supported, e.g. assuming a 3×4 NDArray, index [2,-1] equals to [2,3], and [-1,-3] is an equivalent of [2,1].
-
getReal
public T2 getReal(int... indices)
-
getImag
public T2 getImag(int... indices)
-
set
public void set(T value, int linearIndex)
Description copied from interface:NDArraySets the value of an element specified by linear indexing.Linear indexing: It selects the ith element using the column-major iteration order that linearly spans the entire array. in its particular dimension.
Negative indexing is supported, e.g. -1 refers to the last element, -2 refers to the item before the last one, etc.
-
setReal
public void setReal(T2 value, int linearIndex)
-
setImag
public void setImag(T2 value, int linearIndex)
-
set
public void set(T value, int... indices)
Description copied from interface:NDArraySets the value of an element specified by cartesian indexing.Cartesian indexing: The ordinary way to index into an N-dimensional array is to use exactly N indices; each index selects the position(s) in its particular dimension.
Negative indexing is supported, e.g. assuming a 3×4 NDArray, index [2,-1] equals to [2,3], and [-1,-3] is an equivalent of [2,1].
-
setReal
public void setReal(T2 value, int... indices)
-
setImag
public void setImag(T2 value, int... indices)
-
equals
public boolean equals(Object obj)
Description copied from interface:NDArrayCompares the specified object with this NDArray for equality. Two arrays are equal, if they are both real or both complex, and they are element-wise equal. If the specified object is not NDArray then the function returns false.
-
hashCode
public int hashCode()
Description copied from interface:NDArrayThis method is unsupported because NDArrays should not be used as keys in hash-based collections.
-
stream
public Stream<T> stream()
Description copied from interface:NDArrayReturns a sequential Stream with this collection as its source.
-
maybeParallelStream
public Stream<T> maybeParallelStream()
Returns a sequential Stream with this collection as its source.- Returns:
- a sequential Stream with this collection as its source.
-
parallelStream
public Stream<T> parallelStream()
Description copied from interface:NDArrayReturns a parallel Stream with this collection as its source.- Specified by:
parallelStreamin interfaceNDArray<T>- Returns:
- a parallel Stream with this collection as its source.
-
fillUsingLinearIndices
public NDArray<T> fillUsingLinearIndices(IntFunction<T> func)
Description copied from interface:NDArrayApply the given function to each element of the array, and override each entry with the calculated new values. Please note that entries might not be processed in a sequential order!- Specified by:
fillUsingLinearIndicesin interfaceNDArray<T>- Parameters:
func- function that receives the linear index of the current entry, and returns the new value- Returns:
- itself after the update
-
fillUsingCartesianIndices
public NDArray<T> fillUsingCartesianIndices(Function<int[],T> func)
Description copied from interface:NDArrayApply the given function to each element of the array, and override each entry with the calculated new values. Please note that entries might not be processed in a sequential order!- Specified by:
fillUsingCartesianIndicesin interfaceNDArray<T>- Parameters:
func- function that receives the Cartesian coordinate of the current entry, and returns the new value- Returns:
- itself after the update
-
apply
public NDArray<T> apply(UnaryOperator<T> func)
Description copied from interface:NDArrayApply the given function to each element of the array, and override each entry with the calculated new values. Please note that entries might not be processed in a sequential order!
-
applyWithLinearIndices
public NDArray<T> applyWithLinearIndices(BiFunction<T,Integer,T> func)
Description copied from interface:NDArrayApply the given function to each element of the array, and override each entry with the calculated new values. Please note that entries might not be processed in a sequential order!- Specified by:
applyWithLinearIndicesin interfaceNDArray<T>- Parameters:
func- function that receives the value of the current entry and its linear index, and returns the new value- Returns:
- itself after the update
-
applyWithCartesianIndices
public NDArray<T> applyWithCartesianIndices(BiFunction<T,int[],T> func)
Description copied from interface:NDArrayApply the given function to each element of the array, and override each entry with the calculated new values. Please note that entries might not be processed in a sequential order!- Specified by:
applyWithCartesianIndicesin interfaceNDArray<T>- Parameters:
func- function that receives the value of the current entry and its Cartesian coordinate, and returns the new value- Returns:
- itself after the update
-
mapOnSlices
public NDArray<T> mapOnSlices(BiFunction<NDArray<T>,int[],NDArray<?>> func, int... iterationDims)
Description copied from interface:NDArrayApply the given function to each slices of the array, and create a new NDArray with the calculated new values. Please note that slices might not be processed in a sequential order!- Specified by:
mapOnSlicesin interfaceNDArray<T>- Parameters:
func- function that receives slice and its Cartesian coordinate along the iteration dimensions, and returns a new array with the same size as the sliceiterationDims- dimensions along which iteration is performed- Returns:
- the new NDArray with the calculated new values
-
applyOnSlices
public NDArray<T> applyOnSlices(BiFunction<NDArray<T>,int[],NDArray<?>> func, int... iterationDims)
Description copied from interface:NDArrayApply the given function to each slices of the array, and override each entry with the returned slice. Please note that slices might not be processed in a sequential order!- Specified by:
applyOnSlicesin interfaceNDArray<T>- Parameters:
func- function that receives the value of the current entry and its Cartesian coordinate, and returns a slice from which values are copied to original arrayiterationDims- dimensions along which iteration is performed- Returns:
- itself after the update
-
reduceSlices
public NDArray<T> reduceSlices(BiFunction<NDArray<T>,int[],T> func, int... iterationDims)
Description copied from interface:NDArrayReduces slices along the specified dimensions in this NDArray to scalar values, reducing the number of dimensions.For example, if
Ais a [5 × 8 × 3] array, thenB = A.reduce(func, 2)returns a [5 × 8] array, andB.get(1,1) == func(A.slice(1, 1, ":"), new int[] { 1, 1 }).- Specified by:
reduceSlicesin interfaceNDArray<T>- Parameters:
func- reduction function that accepts a slice and its index and returns reduction resultiterationDims- dimensions along which the reduction should be performed- Returns:
- result of reduction of all slices along the specified dimensions
-
forEach
public void forEach(Consumer<? super T> func)
Description copied from interface:NDArrayApply the given function to each element of the array, and override each entry with the calculated new values. Please note that entries might not be processed in a sequential order!
-
forEachWithLinearIndices
public void forEachWithLinearIndices(ObjIntConsumer<T> func)
Description copied from interface:NDArrayApply the given function to each element of the array, and override each entry with the calculated new values. Please note that entries might not be processed in a sequential order!- Specified by:
forEachWithLinearIndicesin interfaceNDArray<T>- Parameters:
func- function that receives the value of the current entry and its linear index
-
forEachWithCartesianIndices
public void forEachWithCartesianIndices(BiConsumer<T,int[]> func)
Description copied from interface:NDArrayApply the given function to each element of the array, and override each entry with the calculated new values. Please note that entries might not be processed in a sequential order!- Specified by:
forEachWithCartesianIndicesin interfaceNDArray<T>- Parameters:
func- function that receives the value of the current entry and its Cartesian coordinate
-
copy
public NDArray<T> copy()
Description copied from interface:NDArrayReturns a copy of this NDArray.
-
sum
public T sum()
Description copied from interface:NDArrayReturns the sum of all elements in this NDArray.
-
prod
public T prod()
Description copied from interface:NDArrayReturns the product of all elements in this NDArray.
-
accumulate
public T accumulate(BinaryOperator<T> func)
Description copied from interface:NDArrayReturns the result of accumulation of all elements in this NDArray.- Specified by:
accumulatein interfaceNDArray<T>- Parameters:
func- accumulation function that accepts two elements and returns accumulation result- Returns:
- result of accumulation
-
norm
public double norm()
Description copied from interface:NDArrayReturns the 2-norm (Euclidean norm) of the vectorized array.Note: All N-dimensional arrays are treated as if they were reshaped to a 1D vector.
-
norm
public double norm(Double p)
Description copied from interface:NDArrayReturns the p-norm of the vectorized array.Note: All N-dimensional arrays are treated as if they were reshaped to a 1D vector.
Possible values:- p = 0: Hamming distance of the vector from zero (number of non-zero entries) -- it is not a true norm!
- 0 < p < 1: Hamming distance of the vector from zero (number of non-zero entries) -- it is only a quasi norm (triargument inequality doesn't hold)!
- 1: Absolute-value norm (sum of the absolute values of the entries)
- 2: Euclidean norm (square root of sum of the squared entry values)
- 1 < p: General p-norm (Σ(|p|)ᵖ)^(1/p)
- p = Double.POSITIVE_INFINITY: Infinity norm (returns the entry with the maximal absolute value)
-
streamLinearIndices
public IntStream streamLinearIndices()
Description copied from interface:NDArrayReturns a stream of linear indices.- Specified by:
streamLinearIndicesin interfaceNDArray<T>- Returns:
- a stream of linear indices
-
streamCartesianIndices
public Stream<int[]> streamCartesianIndices()
Description copied from interface:NDArrayReturns a stream of cartesian indices. Each item in the stream is a int[] that holds cartesian indices.Cartesian indexing: The ordinary way to index into an N-dimensional array is to use exactly N indices; each index selects the position(s) in its particular dimension.
- Specified by:
streamCartesianIndicesin interfaceNDArray<T>- Returns:
- a stream of cartesian indices
-
writeToFile
public void writeToFile(File file) throws IOException
Description copied from interface:NDArraySave content of the NDArray to the given file.Files written that way can be later loaded by the static function readFromFile.
- Example:
NDArray<Float> array = new BasicFloatNDArray(128, 128).fill(5); array.writeToFile(new File("array.nda")); NDArray<Integer> array2 = BasicIntegerNDArray.readFromFile(new File("array.nda")); assertEquals(array, array2);- Specified by:
writeToFilein interfaceNDArray<T>- Parameters:
file- file into which the content of the NDArray is written (the extension of the file can be arbitrary, but .nda is recommended)- Throws:
IOException- when the given file cannot be opened for write
-
-