Class AbstractNDArray<T,​T2 extends Number>

    • Constructor Detail

      • AbstractNDArray

        public AbstractNDArray()
    • Method Detail

      • similar

        public NDArray<T> similar()
        Description copied from interface: NDArray
        Returns a new array of the same shape as this NDArray filled with zeros.
        Specified by:
        similar in interface NDArray<T>
        Returns:
        a new array of the same shape as this NDArray filled with zeros.
      • iterator

        public Iterator<T> iterator()
        Description copied from interface: NDArray
        Returns 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.
        Specified by:
        iterator in interface Iterable<T>
        Specified by:
        iterator in interface NDArray<T>
        Returns:
        an Iterator over the elements in this collection
      • toArray

        public Object[] toArray()
        Description copied from interface: NDArray
        Returns 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.

        Specified by:
        toArray in interface NDArray<T>
        Returns:
        an array, whose runtime component type is Object, containing all of the elements in this collection
      • toArray

        public <A> A toArray​(A array)
        Description copied from interface: NDArray
        Returns 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:
        toArray in interface NDArray<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: NDArray
        Returns 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.

        Specified by:
        toArray in interface NDArray<T>
        Type Parameters:
        A - the component type of the array to contain the collection
        Parameters:
        generator - a function which produces a new array of the desired type and the provided length
        Returns:
        an array containing all of the elements in this collection
      • spliterator

        public Spliterator<T> spliterator()
        Description copied from interface: NDArray
        Creates 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:
        spliterator in interface Iterable<T>
        Specified by:
        spliterator in interface NDArray<T>
        Returns:
        a Spliterator over the elements in this collection
      • toString

        public String toString()
        Description copied from interface: NDArray
        Returns a String containing type and shape information.
        Specified by:
        toString in interface NDArray<T>
        Overrides:
        toString in class Object
        Returns:
        a String containing type and shape information.
      • contentToString

        public String contentToString()
        Description copied from interface: NDArray
        Returns 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:
        contentToString in interface NDArray<T>
        Returns:
        a String containing tabular representation of the array.
      • contentToString

        public String contentToString​(String format)
        Description copied from interface: NDArray
        Returns 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:
        contentToString in interface NDArray<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: NDArray
        Returns the number of elements within this NDArray.
        Specified by:
        length in interface NDArray<T>
        Returns:
        the number of elements in this collection
      • ndim

        public int ndim()
        Description copied from interface: NDArray
        Returns the number of dimensions.
        Specified by:
        ndim in interface NDArray<T>
        Returns:
        the number of dimensions
      • shape

        public int[] shape()
        Description copied from interface: NDArray
        Returns the dimensions of the array.
        Specified by:
        shape in interface NDArray<T>
        Returns:
        the dimensions of the array
      • shape

        public int shape​(int axis)
        Description copied from interface: NDArray
        Returns the shape of the array along a specific dimension/axis.
        Specified by:
        shape in interface NDArray<T>
        Parameters:
        axis - Selected dimension/axis
        Returns:
        the shape of the array along a specific dimension/axis
      • get

        public T get​(int linearIndex)
        Description copied from interface: NDArray
        Returns 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.

        Specified by:
        get in interface NDArray<T>
        Parameters:
        linearIndex - linear coordinates
        Returns:
        an element specified by cartesian indexing
      • getReal

        public T2 getReal​(int linearIndex)
      • getImag

        public T2 getImag​(int linearIndex)
      • get

        public T get​(int... indices)
        Description copied from interface: NDArray
        Returns 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].

        Specified by:
        get in interface NDArray<T>
        Parameters:
        indices - cartesian coordinates
        Returns:
        an element specified by cartesian indexing
      • getReal

        public T2 getReal​(int... indices)
      • getImag

        public T2 getImag​(int... indices)
      • set

        public void set​(T value,
                        int linearIndex)
        Description copied from interface: NDArray
        Sets 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.

        Specified by:
        set in interface NDArray<T>
        Parameters:
        value - value to be assigned
        linearIndex - linear coordinates
      • 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: NDArray
        Sets 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].

        Specified by:
        set in interface NDArray<T>
        Parameters:
        value - value to be assigned
        indices - cartesian coordinates
      • 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: NDArray
        Compares 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.
        Specified by:
        equals in interface NDArray<T>
        Overrides:
        equals in class Object
        Parameters:
        obj - Object to be compared for equality
        Returns:
        true if the specified object equals with this NDArray
      • hashCode

        public int hashCode()
        Description copied from interface: NDArray
        This method is unsupported because NDArrays should not be used as keys in hash-based collections.
        Specified by:
        hashCode in interface NDArray<T>
        Overrides:
        hashCode in class Object
        Returns:
        always throws UnsupportedOperationsException
      • stream

        public Stream<T> stream()
        Description copied from interface: NDArray
        Returns a sequential Stream with this collection as its source.
        Specified by:
        stream in interface NDArray<T>
        Returns:
        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: NDArray
        Returns a parallel Stream with this collection as its source.
        Specified by:
        parallelStream in interface NDArray<T>
        Returns:
        a parallel Stream with this collection as its source.
      • fillUsingLinearIndices

        public NDArray<T> fillUsingLinearIndices​(IntFunction<T> func)
        Description copied from interface: NDArray
        Apply 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:
        fillUsingLinearIndices in interface NDArray<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: NDArray
        Apply 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:
        fillUsingCartesianIndices in interface NDArray<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: NDArray
        Apply 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:
        apply in interface NDArray<T>
        Parameters:
        func - function that receives the value of the current entry, and returns the new value
        Returns:
        itself after the update
      • applyWithLinearIndices

        public NDArray<T> applyWithLinearIndices​(BiFunction<T,​Integer,​T> func)
        Description copied from interface: NDArray
        Apply 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:
        applyWithLinearIndices in interface NDArray<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: NDArray
        Apply 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:
        applyWithCartesianIndices in interface NDArray<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: NDArray
        Apply 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:
        mapOnSlices in interface NDArray<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 slice
        iterationDims - 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: NDArray
        Apply 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:
        applyOnSlices in interface NDArray<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 array
        iterationDims - 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: NDArray
        Reduces slices along the specified dimensions in this NDArray to scalar values, reducing the number of dimensions.

        For example, if A is a [5 × 8 × 3] array, then B = A.reduce(func, 2) returns a [5 × 8] array, and B.get(1,1) == func(A.slice(1, 1, ":"), new int[] { 1, 1 }).

        Specified by:
        reduceSlices in interface NDArray<T>
        Parameters:
        func - reduction function that accepts a slice and its index and returns reduction result
        iterationDims - 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: NDArray
        Apply 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:
        forEach in interface Iterable<T>
        Specified by:
        forEach in interface NDArray<T>
        Parameters:
        func - function that receives the value of the current entry
      • forEachWithLinearIndices

        public void forEachWithLinearIndices​(ObjIntConsumer<T> func)
        Description copied from interface: NDArray
        Apply 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:
        forEachWithLinearIndices in interface NDArray<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: NDArray
        Apply 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:
        forEachWithCartesianIndices in interface NDArray<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: NDArray
        Returns a copy of this NDArray.
        Specified by:
        copy in interface NDArray<T>
        Returns:
        a copy of this NDArray.
      • sum

        public T sum()
        Description copied from interface: NDArray
        Returns the sum of all elements in this NDArray.
        Specified by:
        sum in interface NDArray<T>
        Returns:
        sum of all elements
      • prod

        public T prod()
        Description copied from interface: NDArray
        Returns the product of all elements in this NDArray.
        Specified by:
        prod in interface NDArray<T>
        Returns:
        product of all elements
      • accumulate

        public T accumulate​(BinaryOperator<T> func)
        Description copied from interface: NDArray
        Returns the result of accumulation of all elements in this NDArray.
        Specified by:
        accumulate in interface NDArray<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: NDArray
        Returns 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.

        Specified by:
        norm in interface NDArray<T>
        Returns:
        the 2-norm (Euclidean norm) of the vectorized array
      • norm

        public double norm​(Double p)
        Description copied from interface: NDArray
        Returns 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)
        Specified by:
        norm in interface NDArray<T>
        Parameters:
        p - type of norm
        Returns:
        the p-(quasi)norm of the array
      • streamLinearIndices

        public IntStream streamLinearIndices()
        Description copied from interface: NDArray
        Returns a stream of linear indices.
        Specified by:
        streamLinearIndices in interface NDArray<T>
        Returns:
        a stream of linear indices
      • streamCartesianIndices

        public Stream<int[]> streamCartesianIndices()
        Description copied from interface: NDArray
        Returns 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:
        streamCartesianIndices in interface NDArray<T>
        Returns:
        a stream of cartesian indices
      • writeToFile

        public void writeToFile​(File file)
                         throws IOException
        Description copied from interface: NDArray
        Save 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:
        writeToFile in interface NDArray<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