Class NDArrayAdapter

  • All Implemented Interfaces:
    BytesSupplier, NDArray, NDResource, java.lang.AutoCloseable

    public abstract class NDArrayAdapter
    extends java.lang.Object
    implements NDArray
    A base implementation of the NDArray that does nothing. This can be used for overriding the NDArray with only part of the interface implemented.

    This interface should only be used for the NDArray implementations that do not plan to implement a large portion of the interface. For the ones that do, they should directly implement NDArray so that the unsupported operations are better highlighted in the code.

    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      NDArray abs()
      Returns the absolute value of this NDArray element-wise.
      NDArray acos()
      Returns the inverse trigonometric cosine of this NDArray element-wise.
      NDArray acosh()
      Returns the inverse hyperbolic cosine of this NDArray element-wise.
      NDArray add​(NDArray other)
      Adds other NDArrays to this NDArray element-wise.
      NDArray add​(java.lang.Number n)
      Adds a number to this NDArray element-wise.
      NDArray addi​(NDArray other)
      Adds other NDArrays to this NDArray element-wise in place.
      NDArray addi​(java.lang.Number n)
      Adds a number to this NDArray element-wise in place.
      NDArray argMax()
      Returns the indices of the maximum values into the flattened NDArray.
      NDArray argMax​(int axis)
      Returns the indices of the maximum values along given axis.
      NDArray argMin()
      Returns the indices of the minimum values into the flattened NDArray.
      NDArray argMin​(int axis)
      Returns the indices of the minimum values along given axis.
      NDArray argSort​(int axis, boolean ascending)
      Returns the indices that would sort this NDArray given the axis.
      NDArray asin()
      Returns the inverse trigonometric sine of this NDArray element-wise.
      NDArray asinh()
      Returns the inverse hyperbolic sine of this NDArray element-wise.
      NDArray atan()
      Returns the inverse trigonometric tangent of this NDArray element-wise.
      NDArray atanh()
      Returns the inverse hyperbolic tangent of this NDArray element-wise.
      void attach​(NDManager manager)
      Attaches this NDResource to the specified NDManager.
      NDArray batchDot​(NDArray other)
      Batchwise product of this NDArray and the other NDArray.
      NDArray booleanMask​(NDArray index, int axis)
      Returns portion of this NDArray given the index boolean NDArray along given axis.
      NDArray broadcast​(Shape shape)
      Broadcasts this NDArray to be the given shape.
      NDArray cbrt()
      Returns the cube-root of this NDArray element-wise.
      NDArray ceil()
      Returns the ceiling of this NDArray element-wise.
      NDArray clip​(java.lang.Number min, java.lang.Number max)
      Clips (limit) the values in this NDArray.
      void close()
      boolean contentEquals​(NDArray other)
      Returns true if all elements in this NDArray are equal to the other NDArray.
      boolean contentEquals​(java.lang.Number number)
      Returns true if all elements in this NDArray are equal to the Number.
      void copyTo​(NDArray array)
      Deep-copies the current NDArray to the one passed in.
      NDArray cos()
      Returns the trigonometric cosine of this NDArray element-wise.
      NDArray cosh()
      Returns the hyperbolic cosine of this NDArray element-wise.
      NDArray cumProd​(int axis)
      Returns the cumulative product of elements of input in the dimension dim.
      NDArray cumProd​(int axis, DataType dataType)
      Returns the cumulative product of elements of input in the dimension dim.
      NDArray cumSum()
      Returns the cumulative sum of the elements in the flattened NDArray.
      NDArray cumSum​(int axis)
      Return the cumulative sum of the elements along a given axis.
      NDArray div​(NDArray other)
      Divides this NDArray by the other NDArray element-wise.
      NDArray div​(java.lang.Number n)
      Divides this NDArray by a number element-wise.
      NDArray divi​(NDArray other)
      Divides this NDArray by the other NDArray element-wise in place.
      NDArray divi​(java.lang.Number n)
      Divides this NDArray by a number element-wise in place.
      NDArray dot​(NDArray other)
      Dot product of this NDArray and the other NDArray.
      NDArray eq​(NDArray other)
      Returns the boolean NDArray for element-wise "Equals" comparison.
      NDArray eq​(java.lang.Number n)
      Returns the boolean NDArray for element-wise "Equals" comparison.
      NDArray erfinv()
      Returns element-wise inverse gauss error function of the NDArray.
      NDArray exp()
      Returns the exponential value of this NDArray element-wise.
      NDArray expandDims​(int axis)
      Expands the Shape of a NDArray.
      NDArray flatten()
      Flattens this NDArray into a 1-D NDArray in row-major order.
      NDArray flip​(int... axes)
      Returns the reverse order of elements in an array along the given axis.
      NDArray floor()
      Returns the floor of this NDArray element-wise.
      NDArray gammaln()
      Return the log of the absolute value of the gamma function of this NDArray element-wise.
      NDArray gather​(NDArray index, int axis)
      Returns a partial NDArray pointed by the indexed array.
      NDArray get​(NDIndex index)
      Returns a partial NDArray.
      DataType getDataType()
      Returns the DataType of this NDArray.
      Device getDevice()
      Returns the Device of this NDArray.
      NDArray getGradient()
      Returns the gradient NDArray attached to this NDArray.
      NDManager getManager()
      Returns the NDManager that manages this.
      java.lang.String getName()
      Returns the name of this NDArray.
      ai.djl.ndarray.internal.NDArrayEx getNDArrayInternal()
      Returns an internal representative of Native NDArray.
      Shape getShape()
      Returns the Shape of this NDArray.
      SparseFormat getSparseFormat()
      Returns the SparseFormat of this NDArray.
      java.lang.String getUid()
      Returns unique identifier of this NDArray.
      NDArray gt​(NDArray other)
      Returns the boolean NDArray for element-wise "Greater Than" comparison.
      NDArray gt​(java.lang.Number n)
      Returns the boolean NDArray for element-wise "Greater" comparison.
      NDArray gte​(NDArray other)
      Returns the boolean NDArray for element-wise "Greater or equals" comparison.
      NDArray gte​(java.lang.Number n)
      Returns the boolean NDArray for element-wise "Greater or equals" comparison.
      boolean hasGradient()
      Returns true if the gradient calculation is required for this NDArray.
      NDArray inverse()
      Computes the inverse of square NDArray if it exists.
      NDArray isInfinite()
      Returns the boolean NDArray with value true where this NDArray's entries are infinite, or false where they are not infinite.
      NDArray isNaN()
      Returns the boolean NDArray with value true where this NDArray's entries are NaN, or false where they are not NaN.
      NDArray log()
      Returns the natural logarithmic value of this NDArray element-wise.
      NDArray log10()
      Returns the base 10 logarithm of this NDArray element-wise.
      NDArray log2()
      Returns the base 2 logarithm of this NDArray element-wise.
      NDArray logicalAnd​(NDArray other)
      Returns the truth value of this NDArray AND the other NDArray element-wise.
      NDArray logicalNot()
      Computes the truth value of NOT this NDArray element-wise.
      NDArray logicalOr​(NDArray other)
      Computes the truth value of this NDArray OR the other NDArray element-wise.
      NDArray logicalXor​(NDArray other)
      Computes the truth value of this NDArray XOR the other NDArray element-wise.
      NDArray logSoftmax​(int axis)
      Applies the softmax function followed by a logarithm.
      NDArray lt​(NDArray other)
      Returns the boolean NDArray for element-wise "Less" comparison.
      NDArray lt​(java.lang.Number n)
      Returns the boolean NDArray for element-wise "Less" comparison.
      NDArray lte​(NDArray other)
      Returns the boolean NDArray for element-wise "Less or equals" comparison.
      NDArray lte​(java.lang.Number n)
      Returns the boolean NDArray for element-wise "Less or equals" comparison.
      NDArray matMul​(NDArray other)
      Product matrix of this NDArray and the other NDArray.
      NDArray max()
      Returns the maximum of this NDArray.
      NDArray max​(int[] axes, boolean keepDims)
      Returns the maximum of this NDArray along given axes.
      NDArray maximum​(NDArray other)
      Returns the maximum of this NDArray and the other NDArray element-wise.
      NDArray maximum​(java.lang.Number n)
      Returns the maximum of this NDArray and a number element-wise.
      NDArray mean()
      Returns the average of this NDArray.
      NDArray mean​(int[] axes, boolean keepDims)
      Returns the average of this NDArray along given axes.
      NDArray median()
      Returns median value for this NDArray.
      NDArray median​(int[] axes)
      Returns median value along given axes.
      NDArray min()
      Returns the minimum of this NDArray.
      NDArray min​(int[] axes, boolean keepDims)
      Returns the minimum of this NDArray along given axes.
      NDArray minimum​(NDArray other)
      Returns the minimum of this NDArray and the other NDArray element-wise.
      NDArray minimum​(java.lang.Number n)
      Returns the minimum of this NDArray and a number element-wise.
      NDArray mod​(NDArray other)
      Returns element-wise remainder of division.
      NDArray mod​(java.lang.Number n)
      Returns element-wise remainder of division.
      NDArray modi​(NDArray other)
      Returns in place element-wise remainder of division in place.
      NDArray modi​(java.lang.Number n)
      Returns element-wise remainder of division in place.
      NDArray mul​(NDArray other)
      Multiplies this NDArray by other NDArrays element-wise.
      NDArray mul​(java.lang.Number n)
      Multiplies this NDArray by a number element-wise.
      NDArray muli​(NDArray other)
      Multiplies this NDArray by other NDArray element-wise in place.
      NDArray muli​(java.lang.Number n)
      Multiplies this NDArray by a number element-wise in place.
      NDArray neg()
      Returns the numerical negative NDArray element-wise.
      NDArray negi()
      Returns the numerical negative NDArray element-wise in place.
      NDArray neq​(NDArray other)
      Returns the boolean NDArray for element-wise "Not equals" comparison.
      NDArray neq​(java.lang.Number n)
      Returns the boolean NDArray for element-wise "Not equals" comparison.
      NDArray nonzero()
      Returns the indices of elements that are non-zero.
      NDArray norm​(boolean keepDims)
      Returns the norm of this NDArray.
      NDArray norm​(int ord, int[] axes, boolean keepDims)
      Returns the norm of this NDArray.
      NDArray normalize​(double p, long dim, double eps)
      Performs Lp normalization of the array over specified dimension.
      NDArray oneHot​(int depth, float onValue, float offValue, DataType dataType)
      Returns a one-hot NDArray.
      NDArray percentile​(java.lang.Number percentile)
      Returns percentile for this NDArray.
      NDArray percentile​(java.lang.Number percentile, int[] axes)
      Returns median along given dimension(s).
      NDArray pow​(NDArray other)
      Takes the power of this NDArray with the other NDArray element-wise.
      NDArray pow​(java.lang.Number n)
      Takes the power of this NDArray with a number element-wise.
      NDArray powi​(NDArray other)
      Takes the power of this NDArray with the other NDArray element-wise in place.
      NDArray powi​(java.lang.Number n)
      Takes the power of this NDArray with a number element-wise in place.
      NDArray prod()
      Returns the product of this NDArray.
      NDArray prod​(int[] axes, boolean keepDims)
      Returns the product of this NDArray elements over the given axes.
      NDArray put​(NDArray index, NDArray data)
      Set the entries of NDArray pointed by index according to linear indexing, to be the numbers in data, which is of the same shape as index.
      NDArray repeat​(int axis, long repeats)
      Repeats element of this NDArray the number of times given repeats along given axis.
      NDArray repeat​(long repeats)
      Repeats element of this NDArray the number of times given repeats.
      NDArray repeat​(long[] repeats)
      Repeats element of this NDArray the number of times given repeats along each axis.
      NDArray repeat​(Shape desiredShape)
      Repeats element of this NDArray to match the desired shape.
      NDArray reshape​(Shape shape)
      Reshapes this NDArray to the given Shape.
      NDArray rotate90​(int times, int[] axes)
      Rotates an array by 90 degrees in the plane specified by axes.
      NDArray round()
      Returns the round of this NDArray element-wise.
      NDArray sequenceMask​(NDArray sequenceLength)
      Sets all elements outside the sequence to 0.
      NDArray sequenceMask​(NDArray sequenceLength, float value)
      Sets all elements outside the sequence to a constant value.
      void set​(NDIndex index, NDArray value)
      Sets the specified index in this NDArray with the given values.
      void set​(NDIndex index, java.lang.Number value)
      Sets the specified index in this NDArray with the given value.
      void set​(NDIndex index, java.util.function.Function<NDArray,​NDArray> function)
      Sets the specific index by a function.
      void set​(NDArray index, java.lang.Number value)
      Sets the NDArray by boolean mask or integer index.
      void set​(java.nio.Buffer buffer)
      Sets this NDArray value from Buffer.
      void setName​(java.lang.String name)
      Sets name of this NDArray.
      void setRequiresGradient​(boolean requiresGrad)
      Attaches a gradient NDArray to this NDArray and marks it so GradientCollector.backward(NDArray) can compute the gradient with respect to it.
      void setScalar​(NDIndex index, java.lang.Number value)
      Sets the specified scalar in this NDArray with the given value.
      NDArray sign()
      Returns the element-wise sign.
      NDArray signi()
      Returns the element-wise sign in-place.
      NDArray sin()
      Returns the trigonometric sine of this NDArray element-wise.
      NDArray sinh()
      Returns the hyperbolic sine of this NDArray element-wise.
      NDArray softmax​(int axis)
      Applies the softmax function along the given axis.
      NDArray sort()
      Sorts the flattened NDArray.
      NDArray sort​(int axis)
      Sorts the flattened NDArray.
      NDList split​(long[] indices, int axis)
      Splits this NDArray into multiple sub-NDArrays given indices along given axis.
      NDList split​(long sections, int axis)
      Splits this NDArray into multiple subNDArrays given sections along the given axis.
      NDArray sqrt()
      Returns the square root of this NDArray element-wise.
      NDArray square()
      Returns the square of this NDArray element-wise.
      NDArray squeeze​(int[] axes)
      Removes singleton dimensions at the given axes.
      NDArray stopGradient()
      Returns an NDArray equal to this that stop gradient propagation through it.
      NDArray sub​(NDArray other)
      Subtracts the other NDArray from this NDArray element-wise.
      NDArray sub​(java.lang.Number n)
      Subtracts a number from this NDArray element-wise.
      NDArray subi​(NDArray other)
      Subtracts the other NDArray from this NDArray element-wise in place.
      NDArray subi​(java.lang.Number n)
      Subtracts a number from this NDArray element-wise in place.
      NDArray sum()
      Returns the sum of this NDArray.
      NDArray sum​(int[] axes, boolean keepDims)
      Returns the sum of this NDArray along given axes.
      NDArray take​(NDManager manager, NDArray index)
      Returns a partial NDArray pointed by index according to linear indexing, and the of output is of the same shape as index.
      NDArray tan()
      Returns the trigonometric tangent of this NDArray element-wise.
      NDArray tanh()
      Returns the hyperbolic tangent of this NDArray element-wise.
      void tempAttach​(NDManager manager)
      Temporarily attaches this NDResource to the specified NDManager.
      NDArray tile​(int axis, long repeats)
      Constructs a NDArray by repeating this NDArray the number of times given by repeats along given axis.
      NDArray tile​(long repeats)
      Constructs a NDArray by repeating this NDArray the number of times given repeats.
      NDArray tile​(long[] repeats)
      Constructs a NDArray by repeating this NDArray the number of times given by repeats.
      NDArray tile​(Shape desiredShape)
      Constructs a NDArray by repeating this NDArray the number of times to match the desired shape.
      NDArray toDegrees()
      Converts this NDArray from radians to degrees element-wise.
      NDArray toDense()
      Returns a dense representation of the sparse NDArray.
      NDArray toDevice​(Device device, boolean copy)
      Moves this NDArray to a different Device.
      NDArray toRadians()
      Converts this NDArray from degrees to radians element-wise.
      NDArray toSparse​(SparseFormat fmt)
      Returns a sparse representation of NDArray.
      java.lang.String toString()
      java.lang.String[] toStringArray​(java.nio.charset.Charset charset)
      Converts this NDArray to a String array with the specified charset.
      NDArray toType​(DataType dataType, boolean copy)
      Converts this NDArray to a different DataType.
      NDArray trace​(int offset, int axis1, int axis2)
      Returns the sum along diagonals of this NDArray.
      NDArray transpose()
      Returns this NDArray with axes transposed.
      NDArray transpose​(int... axes)
      Returns this NDArray with given axes transposed.
      NDArray trunc()
      Returns the truncated value of this NDArray element-wise.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • alternativeManager

        protected NDManager alternativeManager
      • shape

        protected Shape shape
      • name

        protected java.lang.String name
      • isClosed

        protected boolean isClosed
      • uid

        protected java.lang.String uid
    • Constructor Detail

    • Method Detail

      • getName

        public java.lang.String getName()
        Returns the name of this NDArray.
        Specified by:
        getName in interface NDArray
        Returns:
        the name of this NDArray
      • setName

        public void setName​(java.lang.String name)
        Sets name of this NDArray.
        Specified by:
        setName in interface NDArray
        Parameters:
        name - the name of this NDArray
      • getUid

        public java.lang.String getUid()
        Returns unique identifier of this NDArray.
        Specified by:
        getUid in interface NDArray
        Returns:
        unique identifier of this NDArray
      • getDevice

        public Device getDevice()
        Returns the Device of this NDArray.

        Device class contains the information where this NDArray stored in memory, like CPU/GPU.

        Specified by:
        getDevice in interface NDArray
        Returns:
        the Device of this NDArray
      • getDataType

        public DataType getDataType()
        Returns the DataType of this NDArray.

        DataType is a definition of the precision level of the NDArray. All values inside the same NDArray would have the same DataType.

        Specified by:
        getDataType in interface NDArray
        Returns:
        the DataType of this NDArray
      • getShape

        public Shape getShape()
        Returns the Shape of this NDArray.

        Shape defines how this NDArray is represented multi-dimensionally.

        Specified by:
        getShape in interface NDArray
        Returns:
        the Shape of this NDArray
      • toDevice

        public NDArray toDevice​(Device device,
                                boolean copy)
        Moves this NDArray to a different Device.
        Specified by:
        toDevice in interface NDArray
        Parameters:
        device - the Device to be set
        copy - set true if you want to return a copy of the Existing NDArray
        Returns:
        the result NDArray with the new Device
      • toType

        public NDArray toType​(DataType dataType,
                              boolean copy)
        Converts this NDArray to a different DataType.
        Specified by:
        toType in interface NDArray
        Parameters:
        dataType - the DataType to be set
        copy - set true if you want to return a copy of the Existing NDArray
        Returns:
        the result NDArray with the new DataType
      • setRequiresGradient

        public void setRequiresGradient​(boolean requiresGrad)
        Attaches a gradient NDArray to this NDArray and marks it so GradientCollector.backward(NDArray) can compute the gradient with respect to it.
        Specified by:
        setRequiresGradient in interface NDArray
        Parameters:
        requiresGrad - if NDArray requires gradient or not
      • getGradient

        public NDArray getGradient()
        Returns the gradient NDArray attached to this NDArray.
        Specified by:
        getGradient in interface NDArray
        Returns:
        the gradient NDArray
      • hasGradient

        public boolean hasGradient()
        Returns true if the gradient calculation is required for this NDArray.
        Specified by:
        hasGradient in interface NDArray
        Returns:
        true if the gradient calculation is required for this NDArray else false
      • stopGradient

        public NDArray stopGradient()
        Returns an NDArray equal to this that stop gradient propagation through it.
        Specified by:
        stopGradient in interface NDArray
        Returns:
        an NDArray equal to this that stops gradient propagation through it
      • toStringArray

        public java.lang.String[] toStringArray​(java.nio.charset.Charset charset)
        Converts this NDArray to a String array with the specified charset.

        This method is only applicable to the String typed NDArray and not for printing purpose

        Specified by:
        toStringArray in interface NDArray
        Parameters:
        charset - to charset for the string
        Returns:
        Array of Strings
      • gather

        public NDArray gather​(NDArray index,
                              int axis)
        Returns a partial NDArray pointed by the indexed array. Given NDArray arr, NDArray idx, and long axis, the output is out_{ijk} = arr_{idx_{ijk}, j, k} if axis=0 or arr_{i, idx_{ijk}, k} if axis=1 or arr_{i, j, idx_{ijk}} if axis=2
        Specified by:
        gather in interface NDArray
        Parameters:
        index - picks the elements of an NDArray to the same position as index
        axis - the entries of index are indices of axis
        Returns:
        the partial NDArray of the same shape as index
      • take

        public NDArray take​(NDManager manager,
                            NDArray index)
        Returns a partial NDArray pointed by index according to linear indexing, and the of output is of the same shape as index.
        Specified by:
        take in interface NDArray
        Parameters:
        manager - the manager used to create the arrays
        index - picks the elements of an NDArray and output to the same entry as in index
        Returns:
        the partial NDArray of the same shape as index
      • put

        public NDArray put​(NDArray index,
                           NDArray data)
        Set the entries of NDArray pointed by index according to linear indexing, to be the numbers in data, which is of the same shape as index.
        Specified by:
        put in interface NDArray
        Parameters:
        index - select the entries of an NDArray
        data - numbers to assign to the indexed entries
        Returns:
        the NDArray with updated values
      • get

        public NDArray get​(NDIndex index)
        Returns a partial NDArray.
        Specified by:
        get in interface NDArray
        Parameters:
        index - the section of this NDArray to return
        Returns:
        the partial NDArray
      • set

        public void set​(java.nio.Buffer buffer)
        Sets this NDArray value from Buffer.
        Specified by:
        set in interface NDArray
        Parameters:
        buffer - the input buffered data
      • set

        public void set​(NDIndex index,
                        NDArray value)
        Sets the specified index in this NDArray with the given values.
        Specified by:
        set in interface NDArray
        Parameters:
        index - the locations to update
        value - the value to replace with. Can broadcast if given smaller dimensions than the index
      • set

        public void set​(NDIndex index,
                        java.lang.Number value)
        Sets the specified index in this NDArray with the given value.
        Specified by:
        set in interface NDArray
        Parameters:
        index - the locations to update
        value - the value to replace with
      • set

        public void set​(NDIndex index,
                        java.util.function.Function<NDArray,​NDArray> function)
        Sets the specific index by a function.
        Specified by:
        set in interface NDArray
        Parameters:
        index - the locations to update
        function - the function to change the value
      • set

        public void set​(NDArray index,
                        java.lang.Number value)
        Sets the NDArray by boolean mask or integer index.
        Specified by:
        set in interface NDArray
        Parameters:
        index - the boolean or integer NDArray that indicates what to get
        value - the value to replace with
      • setScalar

        public void setScalar​(NDIndex index,
                              java.lang.Number value)
        Sets the specified scalar in this NDArray with the given value.
        Specified by:
        setScalar in interface NDArray
        Parameters:
        index - the single index to update
        value - the value to replace with
      • copyTo

        public void copyTo​(NDArray array)
        Deep-copies the current NDArray to the one passed in.
        Specified by:
        copyTo in interface NDArray
        Parameters:
        array - this NDArray prepared to be copied to
      • booleanMask

        public NDArray booleanMask​(NDArray index,
                                   int axis)
        Returns portion of this NDArray given the index boolean NDArray along given axis.
        Specified by:
        booleanMask in interface NDArray
        Parameters:
        index - boolean NDArray mask
        axis - an integer that represents the axis of NDArray to mask from
        Returns:
        the result NDArray
      • sequenceMask

        public NDArray sequenceMask​(NDArray sequenceLength,
                                    float value)
        Sets all elements outside the sequence to a constant value.

        This function takes an n-dimensional input array of the form [batch_size, max_sequence_length, ....] and returns an array of the same shape. Parameter sequenceLength is used to handle variable-length sequences. sequence_length should be an input array of positive ints of dimension [batch_size].

        Specified by:
        sequenceMask in interface NDArray
        Parameters:
        sequenceLength - used to handle variable-length sequences
        value - the constant value to be set
        Returns:
        the result NDArray
      • sequenceMask

        public NDArray sequenceMask​(NDArray sequenceLength)
        Sets all elements outside the sequence to 0.

        This function takes an n-dimensional input array of the form [batch_size, max_sequence_length, ....] and returns an array of the same shape. Parameter sequenceLength is used to handle variable-length sequences. sequence_length should be an input array of positive ints of dimension [batch_size].

        Specified by:
        sequenceMask in interface NDArray
        Parameters:
        sequenceLength - used to handle variable-length sequences
        Returns:
        the result NDArray
      • contentEquals

        public boolean contentEquals​(java.lang.Number number)
        Returns true if all elements in this NDArray are equal to the Number.

        Examples

         jshell> NDArray array = manager.ones(new Shape(2, 3));
         jshell> array.contentEquals(1); // return true instead of boolean NDArray
         true
         
        Specified by:
        contentEquals in interface NDArray
        Parameters:
        number - the number to compare
        Returns:
        the boolean result
      • contentEquals

        public boolean contentEquals​(NDArray other)
        Returns true if all elements in this NDArray are equal to the other NDArray.

        Examples

         jshell> NDArray array1 = manager.arange(6f).reshape(2, 3);
         jshell> NDArray array2 = manager.create(new float[] {0f, 1f, 2f, 3f, 4f, 5f}, new Shape(2, 3));
         jshell> array1.contentEquals(array2); // return true instead of boolean NDArray
         true
         
        Specified by:
        contentEquals in interface NDArray
        Parameters:
        other - the other NDArray to compare
        Returns:
        the boolean result
      • eq

        public NDArray eq​(java.lang.Number n)
        Returns the boolean NDArray for element-wise "Equals" comparison.

        Examples

         jshell> NDArray array = manager.ones(new Shape(1));
         jshell> array.eq(1);
         ND: (1) cpu() boolean
         [ true]
         
        Specified by:
        eq in interface NDArray
        Parameters:
        n - the number to compare
        Returns:
        the boolean NDArray for element-wise "Equals" comparison
      • eq

        public NDArray eq​(NDArray other)
        Returns the boolean NDArray for element-wise "Equals" comparison.

        Examples

         jshell> NDArray array1 = manager.create(new float[] {0f, 1f, 3f});
         jshell> NDArray array2 = manager.arange(3f);
         jshell> array1.eq(array2);
         ND: (3) cpu() boolean
         [ true,  true, false]
         
        Specified by:
        eq in interface NDArray
        Parameters:
        other - the NDArray to compare
        Returns:
        the boolean NDArray for element-wise "Equals" comparison
      • neq

        public NDArray neq​(java.lang.Number n)
        Returns the boolean NDArray for element-wise "Not equals" comparison.

        Examples

         jshell> NDArray array = manager.arange(4f).reshape(2, 2);
         jshell> array.neq(1);
         ND: (2, 2) cpu() boolean
         [[ true, false],
          [ true,  true],
         ]
         
        Specified by:
        neq in interface NDArray
        Parameters:
        n - the number to compare
        Returns:
        the boolean NDArray for element-wise "Not equals" comparison
      • neq

        public NDArray neq​(NDArray other)
        Returns the boolean NDArray for element-wise "Not equals" comparison.

        Examples

         jshell> NDArray array1 = manager.create(new float[] {1f, 2f});
         jshell> NDArray array2 = manager.create(new float[] {1f, 3f});
         jshell> array1.neq(array2);
         ND: (2) cpu() boolean
         [false,  true]
         jshell> NDArray array1 = manager.create(new float[] {1f, 2f});
         jshell> NDArray array2 = manager.create(new float[] {1f, 3f, 1f, 4f}, new Shape(2, 2));
         jshell> array1.neq(array2); // broadcasting
         ND: (2, 2) cpu() boolean
         [[false,  true],
          [false,  true],
         ]
         
        Specified by:
        neq in interface NDArray
        Parameters:
        other - the NDArray to compare
        Returns:
        the boolean NDArray for element-wise "Not equals" comparison
      • gt

        public NDArray gt​(java.lang.Number n)
        Returns the boolean NDArray for element-wise "Greater" comparison.

        Examples

         jshell> NDArray array = manager.create(new float[] {4f, 2f});
         jshell> array.gt(2f);
         ND: (2) cpu() boolean
         [ true, false]
         
        Specified by:
        gt in interface NDArray
        Parameters:
        n - the number to compare
        Returns:
        the boolean NDArray for element-wise "Greater" comparison
      • gt

        public NDArray gt​(NDArray other)
        Returns the boolean NDArray for element-wise "Greater Than" comparison.

        Examples

         jshell> NDArray array1 = manager.create(new float[] {4f, 2f});
         jshell> NDArray array2 = manager.create(new float[] {2f, 2f});
         jshell> array1.neq(array2);
         ND: (2) cpu() boolean
         [ true, false]
         
        Specified by:
        gt in interface NDArray
        Parameters:
        other - the NDArray to compare
        Returns:
        the boolean NDArray for element-wis "Greater Than" comparison
      • gte

        public NDArray gte​(java.lang.Number n)
        Returns the boolean NDArray for element-wise "Greater or equals" comparison.
         jshell> NDArray array = manager.create(new float[] {4f, 2f});
         jshell> array.gte(2f);
         ND: (2) cpu() boolean
         [ true, true]
         
        Specified by:
        gte in interface NDArray
        Parameters:
        n - the number to compare
        Returns:
        the boolean NDArray for element-wise "Greater or equals" comparison
      • gte

        public NDArray gte​(NDArray other)
        Returns the boolean NDArray for element-wise "Greater or equals" comparison.

        Examples

         jshell> NDArray array1 = manager.create(new float[] {4f, 2f});
         jshell> NDArray array2 = manager.create(new float[] {2f, 2f});
         jshell> array1.gte(array2);
         ND: (2) cpu() boolean
         [ true, true]
         
        Specified by:
        gte in interface NDArray
        Parameters:
        other - the number to compare
        Returns:
        the boolean NDArray for "Greater or equals" comparison
      • lt

        public NDArray lt​(java.lang.Number n)
        Returns the boolean NDArray for element-wise "Less" comparison.

        Examples

         jshell> NDArray array = manager.create(new float[] {1f, 2f});
         jshell> array.lt(2f);
         ND: (2) cpu() boolean
         [ true, false]
         
        Specified by:
        lt in interface NDArray
        Parameters:
        n - the number to compare
        Returns:
        the boolean NDArray for element-wise "Less" comparison
      • lt

        public NDArray lt​(NDArray other)
        Returns the boolean NDArray for element-wise "Less" comparison.

        Examples

         jshell> NDArray array1 = manager.create(new float[] {1f, 2f});
         jshell> NDArray array2 = manager.create(new float[] {2f, 2f});
         jshell> array1.lt(array2);
         ND: (2) cpu() boolean
         [ true, false]
         
        Specified by:
        lt in interface NDArray
        Parameters:
        other - the NDArray to compare
        Returns:
        the boolean NDArray for element-wise "Less" comparison
      • lte

        public NDArray lte​(java.lang.Number n)
        Returns the boolean NDArray for element-wise "Less or equals" comparison.

        Examples

         jshell> NDArray array = manager.create(new float[] {1f, 2f});
         jshell> array.lte(2f);
         ND: (2) cpu() boolean
         [ true, true]
         
        Specified by:
        lte in interface NDArray
        Parameters:
        n - the number to compare
        Returns:
        the boolean NDArray for element-wise "Less or equals" comparison
      • lte

        public NDArray lte​(NDArray other)
        Returns the boolean NDArray for element-wise "Less or equals" comparison.

        Examples

         jshell> NDArray array1 = manager.create(new float[] {1f, 2f});
         jshell> NDArray array2 = manager.create(new float[] {2f, 2f});
         jshell> array1.lte(array2);
         ND: (2) cpu() boolean
         [ true, true]
         
        Specified by:
        lte in interface NDArray
        Parameters:
        other - the NDArray to compare
        Returns:
        the boolean NDArray for element-wise "Less or equals" comparison
      • add

        public NDArray add​(java.lang.Number n)
        Adds a number to this NDArray element-wise.

        Examples

         jshell> NDArray array = manager.create(new float[] {1f, 2f});
         jshell> array.add(2f);
         ND: (2) cpu() float32
         [3., 4.]
         
        Specified by:
        add in interface NDArray
        Parameters:
        n - the number to add
        Returns:
        the result NDArray
      • add

        public NDArray add​(NDArray other)
        Adds other NDArrays to this NDArray element-wise.

        The shapes of this NDArray and other NDArray must be broadcastable.

        Examples

         jshell> NDArray array1 = manager.arange(9f).reshape(3, 3);
         jshell> NDArray array2 = manager.arange(3f);
         jshell> array1.add(array2); // broadcasting
         ND: (3, 3) cpu() float32
         [[ 0.,  2.,  4.],
          [ 3.,  5.,  7.],
          [ 6.,  8., 10.],
         ]
         
        Specified by:
        add in interface NDArray
        Parameters:
        other - the other NDArrays to add
        Returns:
        the result NDArray
      • sub

        public NDArray sub​(java.lang.Number n)
        Subtracts a number from this NDArray element-wise.

        Examples

         jshell> NDArray array = manager.create(new float[] {1f, 2f});
         jshell> array.sub(2f);
         ND: (2) cpu() float32
         [-1.,  0.]
         
        Specified by:
        sub in interface NDArray
        Parameters:
        n - the number to subtract from
        Returns:
        the result NDArray
      • sub

        public NDArray sub​(NDArray other)
        Subtracts the other NDArray from this NDArray element-wise.

        The shapes of this NDArray and other NDArrays must be broadcastable.

        Examples

         jshell> NDArray array1 = manager.arange(9).reshape(3, 3);
         jshell> NDArray array2 = manager.arange(3);
         jshell> array1.sub(array2); // broadcasting
         ND: (3, 3) cpu() float32
         [[0., 0., 0.],
          [3., 3., 3.],
          [6., 6., 6.],
         ]
         
        Specified by:
        sub in interface NDArray
        Parameters:
        other - the other NDArray to subtract from
        Returns:
        the result NDArray
      • mul

        public NDArray mul​(java.lang.Number n)
        Multiplies this NDArray by a number element-wise.

        Examples

         jshell> NDArray array = manager.create(new float[] {1f, 2f});
         jshell> array.mul(3f);
         ND: (2) cpu() float32
         [3., 6.]
         
        Specified by:
        mul in interface NDArray
        Parameters:
        n - the number to multiply by
        Returns:
        the result NDArray
      • mul

        public NDArray mul​(NDArray other)
        Multiplies this NDArray by other NDArrays element-wise.

        The shapes of this NDArray and other NDArray must be broadcastable.

        Examples

         jshell> NDArray array1 = manager.arange(9f).reshape(3, 3);
         jshell> NDArray array2 = manager.arange(3f);
         jshell> array1.mul(array2); // broadcasting
         ND: (3, 3) cpu() float32
         [[ 0.,  1.,  4.],
          [ 0.,  4., 10.],
          [ 0.,  7., 16.],
         ]
         
        Specified by:
        mul in interface NDArray
        Parameters:
        other - the other NDArrays to multiply by
        Returns:
        the result NDArray
      • div

        public NDArray div​(java.lang.Number n)
        Divides this NDArray by a number element-wise.

        Examples

         jshell> NDArray array = manager.arange(5f);
         jshell> array.div(4f);
         ND: (5) cpu() float32
         [0.  , 0.25, 0.5 , 0.75, 1.  ]
         
        Specified by:
        div in interface NDArray
        Parameters:
        n - the number to divide by
        Returns:
        the result NDArray
      • div

        public NDArray div​(NDArray other)
        Divides this NDArray by the other NDArray element-wise.

        The shapes of this NDArray and the other NDArray must be broadcastable.

        Examples

         jshell> NDArray array1 = manager.arange(9f).reshape(3, 3);
         jshell> NDArray array2 = manager.ones(new Shape(3)).mul(10);
         jshell> array1.div(array2); // broadcasting
         ND: (3, 3) cpu() float32
         [[0. , 0.1, 0.2],
          [0.3, 0.4, 0.5],
          [0.6, 0.7, 0.8],
         ]
         
        Specified by:
        div in interface NDArray
        Parameters:
        other - the other NDArray to divide by
        Returns:
        the result NDArray
      • mod

        public NDArray mod​(java.lang.Number n)
        Returns element-wise remainder of division.

        Examples

         jshell> NDArray array = manager.arange(7f);
         jshell> array.mod(5f);
         ND: (7) cpu() float32
         [0., 1., 2., 3., 4., 0., 1.]
         
        Specified by:
        mod in interface NDArray
        Parameters:
        n - the divisor number
        Returns:
        the result NDArray
      • mod

        public NDArray mod​(NDArray other)
        Returns element-wise remainder of division.

        The shapes of this NDArray and the other NDArray must be broadcastable.

        Examples

         jshell> NDArray array1 = manager.create(new float[] {4f, 7f});
         jshell> NDArray array2 = manager.create(new float[] {2f, 3f});
         jshell> array1.mod(array2);
         ND: (2) cpu() float32
         [0., 1.]
         
        Specified by:
        mod in interface NDArray
        Parameters:
        other - the divisor NDArray
        Returns:
        the result NDArray
      • pow

        public NDArray pow​(java.lang.Number n)
        Takes the power of this NDArray with a number element-wise.

        Examples

         jshell> NDArray array = manager.arange(5f);
         jshell> array.pow(4f);
         ND: (6) cpu() float32
         [  0.,   1.,   8.,  27.,  64., 125.]
         
        Specified by:
        pow in interface NDArray
        Parameters:
        n - the number to take the power with
        Returns:
        the result NDArray
      • pow

        public NDArray pow​(NDArray other)
        Takes the power of this NDArray with the other NDArray element-wise.

        Examples

         jshell> NDArray array1 = manager.arange(6f).reshape(3, 2);
         jshell> NDArray array2 = manager.create(new float[] {2f, 3f});
         jshell> array1.pow(array2); // broadcasting
         ND: (3, 2) cpu() float32
         [[  0.,   1.],
          [  4.,  27.],
          [ 16., 125.],
         ]
         
        Specified by:
        pow in interface NDArray
        Parameters:
        other - the other NDArray to take the power with
        Returns:
        the result NDArray
      • addi

        public NDArray addi​(java.lang.Number n)
        Adds a number to this NDArray element-wise in place.

        Examples

         jshell> NDArray array = manager.create(new float[] {1f, 2f});
         jshell> array.addi(2f);
         ND: (2) cpu() float32
         [3., 4.]
         jshell> array;
         ND: (2) cpu() float32
         [3., 4.]
         
        Specified by:
        addi in interface NDArray
        Parameters:
        n - the number to add
        Returns:
        the result NDArray
      • addi

        public NDArray addi​(NDArray other)
        Adds other NDArrays to this NDArray element-wise in place.

        The shapes of this NDArray and other NDArrays must be broadcastable.

        Examples

         jshell> NDArray array1 = manager.create(new float[] {1f, 2f});
         jshell> NDArray array2 = manager.create(new float[] {3f, 4f});
         jshell> array1.addi(array2);
         ND: (2) cpu() float32
         [4., 6.]
         jshell> array;
         ND: (2) cpu() float32
         [4., 6.]
         
        Specified by:
        addi in interface NDArray
        Parameters:
        other - the other NDArrays to add
        Returns:
        the result NDArray
      • subi

        public NDArray subi​(java.lang.Number n)
        Subtracts a number from this NDArray element-wise in place.

        Examples

         jshell> NDArray array = manager.create(new float[] {1f, 2f});
         jshell> array.subi(2f);
         ND: (2) cpu() float32
         [-1.,  0.]
         jshell> array;
         ND: (2) cpu() float32
         [-1.,  0.]
         
        Specified by:
        subi in interface NDArray
        Parameters:
        n - the number to subtract
        Returns:
        the result NDArray
      • subi

        public NDArray subi​(NDArray other)
        Subtracts the other NDArray from this NDArray element-wise in place.

        The shapes of this NDArray and other NDArrays must be broadcastable.

        Examples

         jshell> NDArray array1 = manager.arange(9f).reshape(3, 3);
         jshell> NDArray array2 = manager.arange(3f);
         jshell> array1.subi(array2); // broadcasting
         ND: (3, 3) cpu() float32
         [[0., 0., 0.],
          [3., 3., 3.],
          [6., 6., 6.],
         ]
         jshell> array1;
         [[0., 0., 0.],
          [3., 3., 3.],
          [6., 6., 6.],
         ]
         
        Specified by:
        subi in interface NDArray
        Parameters:
        other - the other NDArray to subtract from
        Returns:
        the result NDArray
      • muli

        public NDArray muli​(java.lang.Number n)
        Multiplies this NDArray by a number element-wise in place.

        Examples

         jshell> NDArray array = manager.create(new float[] {1f, 2f});
         jshell> array.muli(3f);
         ND: (2) cpu() float32
         [3., 6.]
         jshell> array;
         ND: (2) cpu() float32
         [3., 6.]
         
        Specified by:
        muli in interface NDArray
        Parameters:
        n - the number to multiply by
        Returns:
        the result NDArray
      • muli

        public NDArray muli​(NDArray other)
        Multiplies this NDArray by other NDArray element-wise in place.

        The shapes of this NDArray and other NDArrays must be broadcastable.

        Examples

         jshell> NDArray array1 = manager.arange(9f).reshape(3, 3);
         jshell> NDArray array2 = manager.arange(3f);
         jshell> array1.muli(array2); // broadcasting
         ND: (3, 3) cpu() float32
         [[ 0.,  1.,  4.],
          [ 0.,  4., 10.],
          [ 0.,  7., 16.],
         ]
         jshell> array1;
         ND: (3, 3) cpu() float32
         [[ 0.,  1.,  4.],
          [ 0.,  4., 10.],
          [ 0.,  7., 16.],
         ]
         
        Specified by:
        muli in interface NDArray
        Parameters:
        other - the other NDArrays to multiply with
        Returns:
        the result NDArray
      • divi

        public NDArray divi​(java.lang.Number n)
        Divides this NDArray by a number element-wise in place.

        Examples

         jshell> NDArray array = manager.arange(5f);
         jshell> array.divi(4f);
         ND: (5) cpu() float32
         [0.  , 0.25, 0.5 , 0.75, 1.  ]
         jshell> array;
         ND: (5) cpu() float32
         [0.  , 0.25, 0.5 , 0.75, 1.  ]
         
        Specified by:
        divi in interface NDArray
        Parameters:
        n - the number to divide values by
        Returns:
        the array after applying division operation
      • divi

        public NDArray divi​(NDArray other)
        Divides this NDArray by the other NDArray element-wise in place.

        The shapes of this NDArray and the other NDArray must be broadcastable.

        Examples

         jshell> NDArray array1 = manager.arange(9f).reshape(3, 3);
         jshell> NDArray array2 = manager.ones(new Shape(3)).mul(10);
         jshell> array1.divi(array2); // broadcasting
         ND: (3, 3) cpu() float32
         [[0. , 0.1, 0.2],
          [0.3, 0.4, 0.5],
          [0.6, 0.7, 0.8],
         ]
         jshell> array1;
         [[0. , 0.1, 0.2],
          [0.3, 0.4, 0.5],
          [0.6, 0.7, 0.8],
         ]
         
        Specified by:
        divi in interface NDArray
        Parameters:
        other - the other NDArray to divide by
        Returns:
        the result of the divide
      • modi

        public NDArray modi​(java.lang.Number n)
        Returns element-wise remainder of division in place.

        Examples

         jshell> NDArray array = manager.arange(7f);
         jshell> array.modi(5f);
         ND: (7) cpu() float32
         [0., 1., 2., 3., 4., 0., 1.]
         jshell> array;
         ND: (7) cpu() float32
         [0., 1., 2., 3., 4., 0., 1.]
         
        Specified by:
        modi in interface NDArray
        Parameters:
        n - the divisor number
        Returns:
        the result NDArray
      • modi

        public NDArray modi​(NDArray other)
        Returns in place element-wise remainder of division in place.

        The shapes of this NDArray and the other NDArray must be broadcastable.

        Examples

         jshell> NDArray array1 = manager.create(new float[] {4f, 7f});
         jshell> NDArray array2 = manager.create(new float[] {2f, 3f});
         jshell> array1.modi(array2);
         ND: (2) cpu() float32
         [0., 1.]
         jshell> array1;
         ND: (2) cpu() float32
         [0., 1.]
         
        Specified by:
        modi in interface NDArray
        Parameters:
        other - the divisor NDArray
        Returns:
        the result of the divide
      • powi

        public NDArray powi​(java.lang.Number n)
        Takes the power of this NDArray with a number element-wise in place.

        Examples

         jshell> NDArray array = manager.arange(5f);
         jshell> array.powi(4f);
         ND: (6) cpu() float32
         [  0.,   1.,   8.,  27.,  64., 125.]
         jshell> array;
         ND: (6) cpu() float32
         [  0.,   1.,   8.,  27.,  64., 125.]
         
        Specified by:
        powi in interface NDArray
        Parameters:
        n - the number to raise the power to
        Returns:
        the result NDArray
      • powi

        public NDArray powi​(NDArray other)
        Takes the power of this NDArray with the other NDArray element-wise in place.

        The shapes of this NDArray and the other NDArray must be broadcastable.

        Examples

         jshell> NDArray array1 = manager.arange(6f).reshape(3, 2);
         jshell> NDArray array2 = manager.create(new float[] {2f, 3f});
         jshell> array1.powi(array2); // broadcasting
         ND: (3, 2) cpu() float32
         [[  0.,   1.],
          [  4.,  27.],
          [ 16., 125.],
         ]
         jshell> array1;
         ND: (3, 2) cpu() float32
         [[  0.,   1.],
          [  4.,  27.],
          [ 16., 125.],
         ]
         
        Specified by:
        powi in interface NDArray
        Parameters:
        other - the other NDArray to take the power with
        Returns:
        the result NDArray
      • sign

        public NDArray sign()
        Returns the element-wise sign.
        Specified by:
        sign in interface NDArray
        Returns:
        the result NDArray
      • signi

        public NDArray signi()
        Returns the element-wise sign in-place.
        Specified by:
        signi in interface NDArray
        Returns:
        the result NDArray
      • maximum

        public NDArray maximum​(java.lang.Number n)
        Returns the maximum of this NDArray and a number element-wise.

        Examples

         jshell> NDArray array = manager.create(new float[] {2f, 3f, 4f});
         jshell> array.maximum(3f);
         ND: (3) cpu() float32
         [3., 3., 4.]
         
        Specified by:
        maximum in interface NDArray
        Parameters:
        n - the number to be compared
        Returns:
        the maximum of this NDArray and a number element-wise
      • maximum

        public NDArray maximum​(NDArray other)
        Returns the maximum of this NDArray and the other NDArray element-wise.

        The shapes of this NDArray and the other NDArray must be broadcastable.

        Examples

         jshell> NDArray array1 = manager.create(new float[] {2f, 3f, 4f});
         jshell> NDArray array2 = manager.create(new float[] {1f, 5f, 2f});
         jshell> array1.maximum(array2);
         ND: (3) cpu() float32
         [2., 5., 4.]
         jshell> NDArray array1 = manager.eye(2);
         jshell> NDArray array2 = manager.create(new float[] {0.5f, 2f});
         jshell> array1.maximum(array2); // broadcasting
         ND: (2, 2) cpu() float32
         [[1. , 2. ],
          [0.5, 2. ],
         ]
         
        Specified by:
        maximum in interface NDArray
        Parameters:
        other - the NDArray to be compared
        Returns:
        the maximum of this NDArray and the other NDArray element-wise
      • minimum

        public NDArray minimum​(java.lang.Number n)
        Returns the minimum of this NDArray and a number element-wise.

        Examples

         jshell> NDArray array = manager.create(new float[] {2f, 3f, 4f});
         jshell> array.minimum(3f);
         ND: (3) cpu() float32
         [2., 3., 3.]
         
        Specified by:
        minimum in interface NDArray
        Parameters:
        n - the number to be compared
        Returns:
        the minimum of this NDArray and a number element-wise
      • minimum

        public NDArray minimum​(NDArray other)
        Returns the minimum of this NDArray and the other NDArray element-wise.

        The shapes of this NDArray and the other NDArray must be broadcastable.

        Examples

         jshell> NDArray array1 = manager.create(new float[] {2f, 3f, 4f});
         jshell> NDArray array2 = manager.create(new float[] {1f, 5f, 2f});
         jshell> array1.minimum(array2);
         ND: (3) cpu() float32
         [1., 3., 2.]
         jshell> NDArray array1 = manager.eye(2);
         jshell> NDArray array2 = manager.create(new float[] {0.5f, 2f});
         jshell> array1.minimum(array2); // broadcasting
         ND: (2, 2) cpu() float32
         [[0.5, 0. ],
          [0. , 1. ],
         ]
         
        Specified by:
        minimum in interface NDArray
        Parameters:
        other - the NDArray to be compared
        Returns:
        the minimum of this NDArray and the other NDArray element-wise
      • neg

        public NDArray neg()
        Returns the numerical negative NDArray element-wise.
         jshell> NDArray array = manager.arange(5f);
         jshell> array.neg();
         ND: (5) cpu() float32
         [-0., -1., -2., -3., -4.]
         
        Specified by:
        neg in interface NDArray
        Returns:
        the result NDArray
      • negi

        public NDArray negi()
        Returns the numerical negative NDArray element-wise in place.
         jshell> NDArray array = manager.arange(5f);
         jshell> array.negi();
         jshell> array;
         ND: (5) cpu() float32
         [-0., -1., -2., -3., -4.]
         
        Specified by:
        negi in interface NDArray
        Returns:
        the result NDArray
      • abs

        public NDArray abs()
        Returns the absolute value of this NDArray element-wise.

        Examples

         jshell> NDArray array = manager.create(new float[] {-1f, -2f});
         jshell> array.abs();
         ND: (2) cpu() float32
         [1., 2.]
         
        Specified by:
        abs in interface NDArray
        Returns:
        the result NDArray
      • square

        public NDArray square()
        Returns the square of this NDArray element-wise.

        Examples

         jshell> NDArray array = manager.create(new float[] {2f, -3f});
         jshell> array.square();
         ND: (2) cpu() float32
         [4., 9.]
         
        Specified by:
        square in interface NDArray
        Returns:
        the result NDArray
      • sqrt

        public NDArray sqrt()
        Returns the square root of this NDArray element-wise.

        Examples

         jshell> NDArray array = manager.create(new float[] {4f});
         jshell> array.sqrt();
         ND: (1) cpu() float32
         [2., ]
         
        Specified by:
        sqrt in interface NDArray
        Returns:
        the result NDArray
      • cbrt

        public NDArray cbrt()
        Returns the cube-root of this NDArray element-wise.

        Examples

         jshell> NDArray array = manager.create(new float[] {1f, 8f, 27f});
         jshell> array.cbrt();
         ND: (3) cpu() float32
         [1., 2., 3.]
         
        Specified by:
        cbrt in interface NDArray
        Returns:
        the result NDArray
      • floor

        public NDArray floor()
        Returns the floor of this NDArray element-wise.

        The floor of the scalar x is the largest integer i, such that i <= x.

        Examples

         jshell> NDArray array = manager.create(new float[] {-1.7f, -1.5f, -0.2f, 0.2f, 1.5f, 1.7f, 2.0f});
         jshell> array.floor();
         ND: (7) cpu() float32
         [-2., -2., -1.,  0.,  1.,  1.,  2.]
         
        Specified by:
        floor in interface NDArray
        Returns:
        the result NDArray
      • ceil

        public NDArray ceil()
        Returns the ceiling of this NDArray element-wise.

        The ceil of the scalar x is the smallest integer i, such that i >= x.

        Examples

         jshell> NDArray array = manager.create(new float[] {-1.7f, -1.5f, -0.2f, 0.2f, 1.5f, 1.7f, 2.0f});
         jshell> array.ceil();
         ND: (7) cpu() float32
         [-1., -1., -0.,  1.,  2.,  2.,  2.]
         
        Specified by:
        ceil in interface NDArray
        Returns:
        the result NDArray
      • round

        public NDArray round()
        Returns the round of this NDArray element-wise.

        Examples

         jshell> NDArray array = manager.create(new float[] {-1.7f, -1.5f, -0.2f, 0.2f, 1.5f, 1.7f, 2.0f});
         jshell> array.round();
         ND: (7) cpu() float32
         [-2., -2., -0.,  0.,  2.,  2.,  2.]
         
        Specified by:
        round in interface NDArray
        Returns:
        the result NDArray
      • trunc

        public NDArray trunc()
        Returns the truncated value of this NDArray element-wise.

        Examples

         jshell> NDArray array = manager.create(new float[] {-1.7f, -1.5f, -0.2f, 0.2f, 1.5f, 1.7f, 2.0f});
         jshell> array.trunc();
         ND: (7) cpu() float32
         [-1., -1., -0.,  0.,  1.,  1.,  2.]
         
        Specified by:
        trunc in interface NDArray
        Returns:
        the result NDArray
      • exp

        public NDArray exp()
        Returns the exponential value of this NDArray element-wise.

        Examples

         jshell> NDArray array = manager.create(new float[] {0f, 2.5f});
         jshell> array.exp();
         ND: (2) cpu() float32
         [ 1.    , 12.1825]
         
        Specified by:
        exp in interface NDArray
        Returns:
        the result NDArray
      • gammaln

        public NDArray gammaln()
        Return the log of the absolute value of the gamma function of this NDArray element-wise.

        Examples

         jshell> NDArray array = manager.create(new float[] {0.5f, 1f, 1.5f});
         jshell> array.gammaln();
         ND: (2) cpu() float32
         [ 0.5724,  0.0000, -0.1208]
         
        Specified by:
        gammaln in interface NDArray
        Returns:
        the result NDArray
      • log

        public NDArray log()
        Returns the natural logarithmic value of this NDArray element-wise.

        Examples

         jshell> NDArray array = manager.create(new float[] {0f, 2.5f});
         jshell> array.log();
         ND: (2) cpu() float32
         [  -inf, 0.9163]
         
        Specified by:
        log in interface NDArray
        Returns:
        the result NDArray
      • log10

        public NDArray log10()
        Returns the base 10 logarithm of this NDArray element-wise.

        Examples

         jshell> NDArray array = manager.create(new float[] {1000f, 1f, 150f});
         jshell> array.log10();
         ND: (3) cpu() float32
         [3.    , 0.    , 2.1761]
         
        Specified by:
        log10 in interface NDArray
        Returns:
        the result NDArray
      • log2

        public NDArray log2()
        Returns the base 2 logarithm of this NDArray element-wise.

        Examples

         jshell> NDArray array = manager.create(new float[] {8, 1f, 5f});
         jshell> array.log2();
         ND: (3) cpu() float32
         [3.    , 0.    , 2.3219]
         
        Specified by:
        log2 in interface NDArray
        Returns:
        the result NDArray
      • sin

        public NDArray sin()
        Returns the trigonometric sine of this NDArray element-wise.

        The input should be in radians (2 Pi radians equals 360 degrees).

        Examples

         jshell> NDArray array = manager.create(new float[] {0f, 30f, 45f, 60f, 90f});
         jshell> array = array.mul(Math.PI).div(180f);
         jshell> array.sin();
         ND: (5) cpu() float32
         [0.    , 0.5   , 0.7071, 0.866 , 1.    ]
         
        Specified by:
        sin in interface NDArray
        Returns:
        the result NDArray
      • cos

        public NDArray cos()
        Returns the trigonometric cosine of this NDArray element-wise.

        The input should be in radians (2 Pi radians equals 360 degrees).

        Examples

         jshell> NDArray array = manager.create(new double[] {0, Math.PI/2, Math.PI});
         jshell> array.cos();
         ND: (3) cpu() float64
         [  1.0000000e+00,   6.1232340e-17,  -1.0000000e+00],
         
        Specified by:
        cos in interface NDArray
        Returns:
        the result NDArray
      • tan

        public NDArray tan()
        Returns the trigonometric tangent of this NDArray element-wise.

        The input should be in radians (2 Pi radians equals 360 degrees).

        Examples

         jshell> NDArray array = manager.create(new double[] {-Math.PI, Math.PI/2, Math.PI});
         jshell> array.tan();
         ND: (3) cpu() float64
         [  1.2246468e-16,   1.6331239e+16,  -1.2246468e-16],
         
        Specified by:
        tan in interface NDArray
        Returns:
        the result NDArray
      • asin

        public NDArray asin()
        Returns the inverse trigonometric sine of this NDArray element-wise.

        The input should be in the range [-1, 1]. The output is in the closed interval of [-Pi/2, Pi/2].

        Examples

         jshell> NDArray array = manager.create(new float[] {1f, -1f, 0f});
         jshell> array.asin();
         ND: (3) cpu() float64
         [ 1.5708, -1.5708,  0.    ]
         
        Specified by:
        asin in interface NDArray
        Returns:
        the result NDArray
      • acos

        public NDArray acos()
        Returns the inverse trigonometric cosine of this NDArray element-wise.

        The input should be in the range [-1, 1]. The output is in the closed interval of [-Pi/2, Pi/2].

        Examples

         jshell> NDArray array = manager.create(new float[] {1f, -1f});
         jshell> array.acos();
         ND: (2) cpu() float64
         [0.    , 3.1416]
         
        Specified by:
        acos in interface NDArray
        Returns:
        the result NDArray
      • atan

        public NDArray atan()
        Returns the inverse trigonometric tangent of this NDArray element-wise.

        The input should be in the range [-1, 1]. The output is in the closed interval of [-Pi/2, Pi/2].

        Examples

         jshell> NDArray array = manager.create(new float[] {0f, 1f});
         jshell> array.atan();
         ND: (2) cpu() float64
         [0.    , 0.7854]
         
        Specified by:
        atan in interface NDArray
        Returns:
        the result NDArray
      • sinh

        public NDArray sinh()
        Returns the hyperbolic sine of this NDArray element-wise.

        sinh(x)=0.5*(exp(x) - exp(-x))

        Examples

         jshell> NDArray array = manager.create(new double[] {0, Math.PI});
         jshell> array.sinh();
         ND: (2) cpu() float64
         [ 0.    , 11.5487]
         
        Specified by:
        sinh in interface NDArray
        Returns:
        the result NDArray
      • cosh

        public NDArray cosh()
        Returns the hyperbolic cosine of this NDArray element-wise.

        cosh(x)=0.5*(exp(x)+exp(-x))

        Examples

         jshell> NDArray array = manager.create(new double[] {0, Math.PI});
         jshell> array.cosh();
         ND: (2) cpu() float64
         [ 1.    , 11.592 ]
         
        Specified by:
        cosh in interface NDArray
        Returns:
        the result NDArray
      • tanh

        public NDArray tanh()
        Returns the hyperbolic tangent of this NDArray element-wise.

        tanh(x)=sinh(x)/cosh(x)

        Examples

         jshell> NDArray array = manager.create(new double[] {0, Math.PI});
         jshell> array.tanh();
         ND: (2) cpu() float64
         [0.    , 0.9963]
         
        Specified by:
        tanh in interface NDArray
        Returns:
        the result NDArray
      • asinh

        public NDArray asinh()
        Returns the inverse hyperbolic sine of this NDArray element-wise.

        Examples

         jshell> NDArray array = manager.create(new double[] {Math.E, 10});
         jshell> array.asinh();
         ND: (2) cpu() float64
         [1.7254, 2.9982]
         
        Specified by:
        asinh in interface NDArray
        Returns:
        the result NDArray
      • acosh

        public NDArray acosh()
        Returns the inverse hyperbolic cosine of this NDArray element-wise.

        Examples

         jshell> NDArray array = manager.create(new double[] {Math.E, 10});
         jshell> array.acosh();
         ND: (2) cpu() float64
         [1.6575, 2.9932]
         
        Specified by:
        acosh in interface NDArray
        Returns:
        the result NDArray
      • atanh

        public NDArray atanh()
        Returns the inverse hyperbolic tangent of this NDArray element-wise.

        Examples

         jshell> NDArray array = manager.create(new double[] {0, -0.5});
         jshell> array.atanh();
         ND: (2) cpu() float64
         [ 0.    , -0.5493]
         
        Specified by:
        atanh in interface NDArray
        Returns:
        the result NDArray
      • toDegrees

        public NDArray toDegrees()
        Converts this NDArray from radians to degrees element-wise.

        Examples

         jshell> NDArray array = manager.arange(6f).mul(Math.PI / 3);
         jshell> array.toDegrees();
         ND: (6) cpu() float32
         [  0.,  60., 120., 180., 240., 300.]
         
        Specified by:
        toDegrees in interface NDArray
        Returns:
        the result NDArray
      • toRadians

        public NDArray toRadians()
        Converts this NDArray from degrees to radians element-wise.

        Examples

         jshell> NDArray array = manager.arange(6f).mul(60);
         jshell> array.toRadians();
         ND: (6) cpu() float32
         [0.    , 1.0472, 2.0944, 3.1416, 4.1888, 5.236 ]
         
        Specified by:
        toRadians in interface NDArray
        Returns:
        the result NDArray
      • max

        public NDArray max()
        Returns the maximum of this NDArray.

        Examples

         jshell> NDArray array = manager.arange(4f).reshape(2,2);
         jshell> array;
         ND: (2, 2) cpu() float32
         [[0., 1.],
          [2., 3.],
         ]
         jshell> array.max(); // Maximum of the flattened array
         ND: () cpu() float32
         3.
         jshell> array.max().getFloat() // Use getFloat() to get native float
         3.0
         
        Specified by:
        max in interface NDArray
        Returns:
        the maximum of this NDArray
      • max

        public NDArray max​(int[] axes,
                           boolean keepDims)
        Returns the maximum of this NDArray along given axes.

        Examples

         jshell> NDArray array = manager.arange(4f).reshape(2,2);
         jshell> array;
         ND: (2, 2) cpu() float32
         [[0., 1.],
          [2., 3.],
         ]
         jshell> array.max(new int[]{0}, true); // Maximum along the first axis and keep dimension
         ND: (1, 2) cpu() float32
         [[2., 3.],
         ]
         jshell> array.max(new int[]{1}, true); // Maximum along the second axis and keep dimension
         ND: (2, 1) cpu() float32
         [[1.],
          [3.],
         ]
         
        Specified by:
        max in interface NDArray
        Parameters:
        axes - the axes along which to operate
        keepDims - true to keep the specified axes as size 1 in the output array, false to squeeze the values out of the output array.
        Returns:
        the maximum of this NDArray
      • min

        public NDArray min()
        Returns the minimum of this NDArray.

        Examples

         jshell> NDArray array = manager.arange(4f).reshape(2,2);
         jshell> array;
         ND: (2, 2) cpu() float32
         [[0., 1.],
          [2., 3.],
         ]
         jshell> array.min(); // Minimum of the flattened array
         ND: () cpu() float32
         0.
         jshell> array.min().getFloat(); // Use getFloat() to get native float
         0.0
         
        Specified by:
        min in interface NDArray
        Returns:
        the minimum of this NDArray
      • min

        public NDArray min​(int[] axes,
                           boolean keepDims)
        Returns the minimum of this NDArray along given axes.

        Examples

         jshell> NDArray array = manager.arange(4f).reshape(2,2);
         jshell> array
         ND: (2, 2) cpu() float32
         [[0., 1.],
          [2., 3.],
         ]
         jshell> array.min(new int[]{0}, true) // Minimum along the first axis and keep dimension
         ND: (1, 2) cpu() float32
         [[0., 1.],
         ]
         jshell> array.min(new int[]{1}, true) // Minimum along the second axis and keep dimension
         ND: (2, 1) cpu() float32
         [[0.],
          [2.],
         ]
         
        Specified by:
        min in interface NDArray
        Parameters:
        axes - the axes along which to operate
        keepDims - true to keep the specified axes as size 1 in the output array, false to squeeze the values out of the output array
        Returns:
        the minimum of this NDArray
      • sum

        public NDArray sum()
        Returns the sum of this NDArray.

        Examples

         jshell> NDArray array = manager.create(new float[] {0.5f, 1.5f});
         jshell> array.sum();
         ND: () cpu() float32
         2.
         jshell> array.sum().getFloat(); // Use getFloat() to get native float
         2.0
         jshell> NDArray array = manager.create(new float[] {0f, 1f, 0f, 5f}, new Shape(2, 2));
         jshell> array.sum();
         ND: () cpu() float32
         6.
         
        Specified by:
        sum in interface NDArray
        Returns:
        the sum of this NDArray
      • sum

        public NDArray sum​(int[] axes,
                           boolean keepDims)
        Returns the sum of this NDArray along given axes.

        Examples

         jshell> NDArray array = manager.create(new float[] {0f, 1f, 0f, 5f}, new Shape(2, 2));
         jshell> array;
         ND: (2, 2) cpu() float32
         [[0., 1.],
          [0., 5.],
         ]
         jshell> array.sum(new int[] {0}, true);
         ND: (1, 2) cpu() float32
         [[0., 6.],
         ]
         jshell> array.sum(new int[] {1}, true);
         ND: (2, 2) cpu() float32
         [[0., 1.],
          [0., 5.],
         ]
         
        Specified by:
        sum in interface NDArray
        Parameters:
        axes - the axes along which to operate
        keepDims - true to keep the specified axes as size 1 in the output array, false to squeeze the values out of the output array
        Returns:
        the sum of this NDArray
      • cumProd

        public NDArray cumProd​(int axis)
        Returns the cumulative product of elements of input in the dimension dim. For example, if input is a vector of size N, the result will also be a vector of size N, with elements. [x1, x1 * x2, x1 * x2 *x3 ...]
        Specified by:
        cumProd in interface NDArray
        Parameters:
        axis - the axis along which to operate
        Returns:
        the cumulative product of this
      • cumProd

        public NDArray cumProd​(int axis,
                               DataType dataType)
        Returns the cumulative product of elements of input in the dimension dim. For example, if input is a vector of size N, the result will also be a vector of size N, with elements. [x1, x1 * x2, x1 * x2 *x3 ...]
        Specified by:
        cumProd in interface NDArray
        Parameters:
        axis - the axis along which to operate
        dataType - the datatype of the output
        Returns:
        the cumulative product of this
      • prod

        public NDArray prod()
        Returns the product of this NDArray.

        Examples

         jshell> NDArray array = manager.create(new float[] {2f, 3f});
         jshell> array.prod();
         ND: () cpu() float32
         6.
         jshell> array.prod().getFloat(); // Use getFloat to get native float
         6.0
         jshell> NDArray array = manager.create(new float[] {1f, 2f, 3f, 4f}, new Shape(2, 2));
         jshell> array.prod();
         ND: () cpu() float32
         24.
         
        Specified by:
        prod in interface NDArray
        Returns:
        the product of this NDArray
      • prod

        public NDArray prod​(int[] axes,
                            boolean keepDims)
        Returns the product of this NDArray elements over the given axes.

        Examples

         jshell> NDArray array = manager.create(new float[] {1f, 2f, 3f, 4f}, new Shape(2, 2));
         jshell> array;
         ND: (2, 2) cpu() float32
         [[1., 2.],
          [3., 4.],
         ]
         jshell> array.prod(new int[] {0}, true);
         ND: (1, 2) cpu() float32
         [[3., 8.],
         ]
         jshell> array.prod(new int[] {1}, true);
         ND: (2, 1) cpu() float32
         [[ 2.],
          [12.],
         ]
         
        Specified by:
        prod in interface NDArray
        Parameters:
        axes - the axes along which to operate
        keepDims - true to keep the specified axes as size 1 in the output array, false to squeeze the values out of the output array
        Returns:
        the product of this NDArray
      • mean

        public NDArray mean()
        Returns the average of this NDArray.

        Examples

         jshell> NDArray array = manager.create(new float[] {2f, 3f});
         jshell> array.mean();
         ND: () cpu() float32
         2.5
         jshell> array.mean().getFloat(); // Use getFloat() to get native float
         2.5
         jshell> NDArray array = manager.create(new float[] {1f, 2f, 3f, 4f}, new Shape(2, 2));
         jshell> array.mean();
         ND: () cpu() float32
         2.5
         
        Specified by:
        mean in interface NDArray
        Returns:
        the average of this NDArray
      • mean

        public NDArray mean​(int[] axes,
                            boolean keepDims)
        Returns the average of this NDArray along given axes.

        Examples

         jshell> NDArray array = manager.create(new float[] {1f, 2f, 3f, 4f}, new Shape(2, 2));
         jshell> array;
         ND: (2, 2) cpu() float32
         [[1., 2.],
          [3., 4.],
         ]
         jshell> array.mean(new int[] {0}, true);
         ND: (1, 2) cpu() float32
         [[2., 3.],
         ]
         jshell> array.mean(new int[] {1}, true);
         ND: (2, 1) cpu() float32
         [[1.5],
          [3.5],
         ]
         
        Specified by:
        mean in interface NDArray
        Parameters:
        axes - the axes along which to operate
        keepDims - true to keep the specified axes as size 1 in the output array, false to squeeze the values out of the output array
        Returns:
        the average of this NDArray
      • normalize

        public NDArray normalize​(double p,
                                 long dim,
                                 double eps)
        Performs Lp normalization of the array over specified dimension.

        Examples

         jshell> NDArray array = manager.create(new float[] {1, 2, 3, 4, 5, 6}, new Shape(2, 3));
         jshell> array;
         ND: (2, 2) cpu() float32
         [[1., 2., 3.],
          [4., 5., 6.],
         ]
         jshell> array.normalize(2, 1, 1e-12);
         ND: (2, 3) cpu() float32
         [[0.2673, 0.5345, 0.8018],
          [0.4558, 0.5698, 0.6838],
         ]
         
        Specified by:
        normalize in interface NDArray
        Parameters:
        p - the exponent value in the norm formulation
        dim - the dimension to reduce
        eps - the small value to avoid division by zero
        Returns:
        the normalized NDArray
      • rotate90

        public NDArray rotate90​(int times,
                                int[] axes)
        Rotates an array by 90 degrees in the plane specified by axes.

        Rotation direction is from the first towards the second axis.

        Specified by:
        rotate90 in interface NDArray
        Parameters:
        times - Number of times the array is rotated by 90 degrees.
        axes - The array is rotated in the plane defined by the axes. Axes must be different.
        Returns:
        the rotated NDArray
      • trace

        public NDArray trace​(int offset,
                             int axis1,
                             int axis2)
        Returns the sum along diagonals of this NDArray.

        If this NDArray is 2-D, the sum along its diagonal with the given offset is returned, i.e., the sum of elements a[i,i+offset] for all i. If this NDArray has more than two dimensions, then the axes specified by axis1 and axis2 are used to determine the 2-D sub-arrays whose traces are returned. The Shape of the resulting array is the same as this NDArray with axis1 and axis2 removed.

        Examples

         jshell> NDArray array = manager.arange(8f).reshape(2, 2, 2);
         jshell> array;
         ND: (2, 2, 2) cpu() float32
         [[[0., 1.],
           [2., 3.],
          ],
          [[4., 5.],
           [6., 7.],
          ],
         ]
         jshell> array.trace(1,1,2);
         ND: (2) cpu() float32
         [1., 5.]
         
        Specified by:
        trace in interface NDArray
        Parameters:
        offset - offset of the diagonal from the main diagonal. Can be both positive and negative.
        axis1 - axes to be used as the first axis of the 2-D sub-arrays from which the diagonals should be taken
        axis2 - axes to be used as the second axis of the 2-D sub-arrays from which the diagonals should be taken
        Returns:
        the sum along diagonals of this NDArray
      • split

        public NDList split​(long sections,
                            int axis)
        Splits this NDArray into multiple subNDArrays given sections along the given axis.

        Examples

         jshell> NDArray array = manager.arange(18f).reshape(2, 9);
         jshell> array;
         ND: (2, 9) cpu() float32
         [[ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.],
          [ 9., 10., 11., 12., 13., 14., 15., 16., 17.],
         ]
         jshell> array.split(3, 1).forEach(System.out::println);
         ND: (2, 3) cpu() float32
         [[ 0.,  1.,  2.],
          [ 9., 10., 11.],
         ]
        
         ND: (2, 3) cpu() float32
         [[ 3.,  4.,  5.],
          [12., 13., 14.],
         ]
        
         ND: (2, 3) cpu() float32
         [[ 6.,  7.,  8.],
          [15., 16., 17.],
         ]
         
        Specified by:
        split in interface NDArray
        Parameters:
        sections - this NDArray will be divided into N (sections) equal arrays along axis
        axis - the axis to split along
        Returns:
        an NDList with numOutputs NDArrays with Shape (this.shape.axis /= axis)
      • split

        public NDList split​(long[] indices,
                            int axis)
        Splits this NDArray into multiple sub-NDArrays given indices along given axis.

        Examples

         jshell> NDArray array = manager.arange(18f).reshape(2, 9);
         jshell> array;
         ND: (2, 9) cpu() float32
         [[ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.],
          [ 9., 10., 11., 12., 13., 14., 15., 16., 17.],
         ]
         jshell> array.split(new int[] {2,4,5}, 1).forEach(System.out::println);
         ND: (2, 2) cpu() float32
         [[ 0.,  1.],
          [ 9., 10.],
         ]
        
         ND: (2, 2) cpu() float32
         [[ 2.,  3.],
          [11., 12.],
         ]
        
         ND: (2, 1) cpu() float32
         [[ 4.],
          [13.],
         ]
        
         ND: (2, 4) cpu() float32
         [[ 5.,  6.,  7.,  8.],
          [14., 15., 16., 17.],
         ]
         
        Specified by:
        split in interface NDArray
        Parameters:
        indices - the entries indicate where along axis this NDArray is split. If an index exceeds the dimension of this NDArray along axis, an empty sub-array is returned correspondingly
        axis - the axis to split along
        Returns:
        an NDList with numOutputs NDArrays with Shape (this.shape.axis /= axis)
      • flatten

        public NDArray flatten()
        Flattens this NDArray into a 1-D NDArray in row-major order.

        To flatten in column-major order, first transpose this NDArray

        Examples

         jshell> NDArray array = manager.create(new float[]{1f, 2f, 3f, 4f}, new Shape(2, 2));
         jshell> array.flatten();
         ND: (4) cpu() float32
         [1., 2., 3., 4.]
         
        Specified by:
        flatten in interface NDArray
        Returns:
        a 1-D NDArray of equal size
      • reshape

        public NDArray reshape​(Shape shape)
        Reshapes this NDArray to the given Shape.

        You can reshape it to match another NDArray by calling a.reshape(b.getShape())

        Examples

         jshell> NDArray array = manager.arange(6f);
         jshell> array;
         ND: (6) cpu() float32
         [0., 1., 2., 3., 4., 5.]
         jshell> array.reshape(new Shape(2, 3));
         ND: (2, 3) cpu() float32
         [[0., 1., 2.],
          [3., 4., 5.],
         ]
         
        Specified by:
        reshape in interface NDArray
        Parameters:
        shape - the Shape to reshape into. Must have equal size to the current shape
        Returns:
        a reshaped NDArray
      • expandDims

        public NDArray expandDims​(int axis)
        Expands the Shape of a NDArray.

        Inserts a new axis that will appear at the axis position in the expanded NDArray shape.

        Examples

         jshell> NDArray array = manager.create(new float[] {1f, 2f});
         jshell> array;
         ND: (2) cpu() float32
         [1., 2.]
         jshell> array.expandDims(0);
         ND: (1, 2) cpu() float32
         [[1., 2.],
         ]
         jshell> array.expandDims(1);
         ND: (2, 1) cpu() float32
         [[1.],
          [2.],
         ]
         
        Specified by:
        expandDims in interface NDArray
        Parameters:
        axis - the position in the expanded axes where the new axis is placed
        Returns:
        the result NDArray. The number of dimensions is one greater than that of the NDArray
      • squeeze

        public NDArray squeeze​(int[] axes)
        Removes singleton dimensions at the given axes.

        Examples

         jshell> NDArray array = manager.create(new float[] {0f, 1f, 2f}, new Shape(1, 3, 1));
         jshell> array;
         ND: (1, 3, 1) cpu() float32
         [[[0.],
           [1.],
           [2.],
          ],
         ]
         jshell> array.squeeze(new int[] {0, 2});
         ND: (3) cpu() float32
         [0., 1., 2.]
         
        Specified by:
        squeeze in interface NDArray
        Parameters:
        axes - the axes at which to remove the singleton dimensions
        Returns:
        a result NDArray of same size and data without the axes at part of the shape
      • logicalAnd

        public NDArray logicalAnd​(NDArray other)
        Returns the truth value of this NDArray AND the other NDArray element-wise.

        The shapes of this NDArray and the other NDArray must be broadcastable.

        Examples

         jshell> NDArray array1 = manager.create(new boolean[] {true});
         jshell> NDArray array2 = manager.create(new boolean[] {false});
         jshell> array1.logicalAnd(array2);
         ND: (1) cpu() boolean
         [false]
         jshell> array1 = manager.create(new boolean[] {true, false});
         jshell> array2 = manager.create(new boolean[] {false, false});
         jshell> array1.logicalAnd(array2);
         ND: (2) cpu() boolean
         [false, false]
         
         jshell> NDArray array = manager.arange(5f);
         jshell> array.gt(1).logicalAnd(array.lt(4));
         ND: (5) cpu() boolean
         [false, false,  true,  true, false]
         
        Specified by:
        logicalAnd in interface NDArray
        Parameters:
        other - the other NDArray to operate on
        Returns:
        the boolean NDArray of the logical AND operation applied to the elements of this NDArray and the other NDArray
      • logicalOr

        public NDArray logicalOr​(NDArray other)
        Computes the truth value of this NDArray OR the other NDArray element-wise.

        The shapes of this NDArray and the other NDArray must be broadcastable.

        Examples

         jshell> NDArray array1 = manager.create(new boolean[] {true});
         jshell> NDArray array2 = manager.create(new boolean[] {false});
         jshell> array1.logicalOr(array2);
         ND: (1) cpu() boolean
         [ true]
         jshell> array1 = manager.create(new boolean[] {true, false});
         jshell> array2 = manager.create(new boolean[] {false, false});
         jshell> array1.logicalOr(array2);
         ND: (2) cpu() boolean
         [ true, false]
         
         jshell> NDArray array = manager.arange(5f);
         jshell> array.lt(1).logicalOr(array.gt(3));
         ND: (5) cpu() boolean
         [ true, false, false, false,  true]
         
        Specified by:
        logicalOr in interface NDArray
        Parameters:
        other - the other NDArray to operate on
        Returns:
        the boolean NDArray of the logical OR operation applied to the elements of this NDArray and the other NDArray
      • logicalXor

        public NDArray logicalXor​(NDArray other)
        Computes the truth value of this NDArray XOR the other NDArray element-wise.

        The shapes of this NDArray and the other NDArray must be broadcastable.

        Examples

         jshell> NDArray array = manager.create(new boolean[] {true});
         jshell> array1.logicalXor(array2);
         ND: (1) cpu() boolean
         [ true]
         jshell> array1 = manager.create(new boolean[] {true, false});
         jshell> array2 = manager.create(new boolean[] {false, false});
         jshell> array1.logicalXor(array2);
         ND: (2) cpu() boolean
         [ true, false]
         
         jshell> NDArray array = manager.arange(5f);
         jshell> array.lt(1).logicalXor(array.gt(3));
         ND: (5) cpu() boolean
         [ true, false, false, false,  true]
         
        Specified by:
        logicalXor in interface NDArray
        Parameters:
        other - the other NDArray to operate on
        Returns:
        the boolean NDArray of the logical XOR operation applied to the elements of this NDArray and the other NDArray
      • logicalNot

        public NDArray logicalNot()
        Computes the truth value of NOT this NDArray element-wise.

        Examples

         jshell> NDArray array = manager.create(new boolean[] {true});
         jshell> array.logicalNot();
         ND: (1) cpu() boolean
         [ false]
         
         jshell> NDArray array = manager.arange(5f);
         jshell> array.lt(1).logicalNot();
         ND: (5) cpu() boolean
         [false, true, true,  true,  true]
         
        Specified by:
        logicalNot in interface NDArray
        Returns:
        the boolean NDArray
      • argSort

        public NDArray argSort​(int axis,
                               boolean ascending)
        Returns the indices that would sort this NDArray given the axis.

        Perform an indirect sort along the given axis. It returns a NDArray of indices of the same Shape as this NDArray.

        Examples

         jshell> NDArray array = manager.create(new float[] {0f, 3f, 2f, 2f}, new Shape(2, 2));
         jshell> array.argSort(0, false);
         ND: (2, 2) cpu() int64
         [[ 1,  0],
          [ 0,  1],
         ]
         
        Specified by:
        argSort in interface NDArray
        Parameters:
        axis - the axis to sort along
        ascending - whether to sort ascending
        Returns:
        a NDArray of indices corresponding to elements in this NDArray on the axis, the output DataType is always DataType.INT64
      • sort

        public NDArray sort()
        Sorts the flattened NDArray.

        Examples

         jshell> NDArray array = manager.create(new float[] {1f, 4f, 3f, 1f}, new Shape(2, 2));
         jshell> array;
         ND: (2, 2) cpu() float32
         [[1., 4.],
          [3., 1.],
         ]
         jshell> array.sort(); // sort the flattened array
         ND: (2, 2) cpu() float32
         [[1., 4.],
          [1., 3.],
         ]
         
        Specified by:
        sort in interface NDArray
        Returns:
        the sorted NDArray
      • sort

        public NDArray sort​(int axis)
        Sorts the flattened NDArray.

        Examples

         jshell> NDArray array = manager.create(new float[] {1f, 4f, 3f, 1f}, new Shape(2, 2));
         jshell> array;
         ND: (2, 2) cpu() float32
         [[1., 4.],
          [3., 1.],
         ]
         jshell> array.sort(0); // sort along the first axis
         ND: (2, 2) cpu() float32
         [[1., 1.],
          [3., 4.],
         ]
         
        Specified by:
        sort in interface NDArray
        Parameters:
        axis - the axis to sort along
        Returns:
        the sorted NDArray
      • softmax

        public NDArray softmax​(int axis)
        Applies the softmax function along the given axis.
        Specified by:
        softmax in interface NDArray
        Parameters:
        axis - the axis along which to apply
        Returns:
        the result NDArray
        See Also:
        softmax, NDArray.softmax(int)
      • logSoftmax

        public NDArray logSoftmax​(int axis)
        Applies the softmax function followed by a logarithm.

        Mathematically equivalent to calling softmax and then log. This single operator is faster than calling two operators and numerically more stable when computing gradients.

        Specified by:
        logSoftmax in interface NDArray
        Parameters:
        axis - the axis along which to apply
        Returns:
        the result NDArray
      • cumSum

        public NDArray cumSum()
        Returns the cumulative sum of the elements in the flattened NDArray.

        Examples

         jshell> NDArray array = manager.create(new float[] {1f, 2f, 3f, 4f, 5f, 6f}, new Shape(2, 3));
         jshell> array;
         ND: (2, 3) cpu() float32
         [[1., 2., 3.],
          [4., 5., 6.],
         ]
         jshell> array.cumSum(); // cumSum on flattened array
         ND: (6) cpu() float32
         [ 1.,  3.,  6., 10., 15., 21.]
         
        Specified by:
        cumSum in interface NDArray
        Returns:
        the cumulative sum of the elements in the flattened NDArray
      • cumSum

        public NDArray cumSum​(int axis)
        Return the cumulative sum of the elements along a given axis.

        Examples

         jshell> NDArray array = manager.create(new float[] {1f, 2f, 3f, 4f, 5f, 6f}, new Shape(2, 3));
         jshell> array;
         ND: (2, 3) cpu() float32
         [[1., 2., 3.],
          [4., 5., 6.],
         ]
         jshell> array.cumSum(0);
         ND: (2, 3) cpu() float32
         [[1., 2., 3.],
          [5., 7., 9.],
         ]
         jshell> array.cumSum(1);
         ND: (2, 3) cpu() float32
         [[ 1.,  3.,  6.],
          [ 4.,  9., 15.],
         ]
         
        Specified by:
        cumSum in interface NDArray
        Parameters:
        axis - the axis along which the cumulative sum is computed
        Returns:
        the cumulative sum along the specified axis
      • isInfinite

        public NDArray isInfinite()
        Returns the boolean NDArray with value true where this NDArray's entries are infinite, or false where they are not infinite.
        Specified by:
        isInfinite in interface NDArray
        Returns:
        the boolean NDArray with value true if this NDArray's entries are infinite
      • isNaN

        public NDArray isNaN()
        Returns the boolean NDArray with value true where this NDArray's entries are NaN, or false where they are not NaN.

        Examples

         jshell> NDArray array = manager.create(new float[] {Float.POSITIVE_INFINITY, 0, Float.NaN});
         jshell> array.isNaN();
         ND: (3) cpu() boolean
         [false, false,  true]
         
        Specified by:
        isNaN in interface NDArray
        Returns:
        the boolean NDArray with value true if this NDArray's NDArray are NaN
      • tile

        public NDArray tile​(long repeats)
        Constructs a NDArray by repeating this NDArray the number of times given repeats.

        Examples

         jshell> NDArray array = manager.create(new float[] {0f, 1f, 2f});
         jshell> array.tile(2);
         ND: (6) cpu() float32
         [0., 1., 2., 0., 1., 2.]
         
        Specified by:
        tile in interface NDArray
        Parameters:
        repeats - the number of times to repeat for each dimension
        Returns:
        a NDArray that has been tiled
      • tile

        public NDArray tile​(int axis,
                            long repeats)
        Constructs a NDArray by repeating this NDArray the number of times given by repeats along given axis.

        Examples

         jshell> NDArray array = manager.create(new float[] {0f, 1f, 2f});
         jshell> array.tile(1, 2);
         ND: (1, 6) cpu() float32
         [[0., 1., 2., 0., 1., 2.],
         ]
         
        Specified by:
        tile in interface NDArray
        Parameters:
        axis - the axis to repeat
        repeats - the number of times to repeat for each axis
        Returns:
        a NDArray that has been tiled
      • tile

        public NDArray tile​(long[] repeats)
        Constructs a NDArray by repeating this NDArray the number of times given by repeats.

        Examples

         jshell> NDArray array = manager.create(new float[] {0f, 1f, 2f});
         jshell> array.tile(new long[] {2, 2});
         ND: (2, 6) cpu() float32
         [[0., 1., 2., 0., 1., 2.],
          [0., 1., 2., 0., 1., 2.],
         ]
         
        Specified by:
        tile in interface NDArray
        Parameters:
        repeats - the number of times to repeat along each axis
        Returns:
        a NDArray that has been tiled
      • tile

        public NDArray tile​(Shape desiredShape)
        Constructs a NDArray by repeating this NDArray the number of times to match the desired shape.

        If the desired Shapehas fewer dimensions than this NDArray, it will tile against the last axis.

        Examples

         jshell> NDArray array = manager.create(new float[] {0f, 1f, 2f});
         jshell> array.tile(new Shape(6));
         ND: (6) cpu() float32
         [0., 1., 2., 0., 1., 2.]
         
        Specified by:
        tile in interface NDArray
        Parameters:
        desiredShape - the Shapethat should be converted to
        Returns:
        a NDArray that has been tiled
      • repeat

        public NDArray repeat​(long repeats)
        Repeats element of this NDArray the number of times given repeats.

        Examples

         jshell> NDArray array = manager.create(new float[] {0f, 1f, 2f});
         jshell> array.repeat(2);
         ND: (6) cpu() float32
         [0., 0., 1., 1., 2., 2.]
         
        Specified by:
        repeat in interface NDArray
        Parameters:
        repeats - the number of times to repeat for each axis
        Returns:
        an NDArray that has been repeated
      • repeat

        public NDArray repeat​(int axis,
                              long repeats)
        Repeats element of this NDArray the number of times given repeats along given axis.

        Examples

         jshell> NDArray array = manager.create(new float[] {0f, 1f, 2f, 3f}, new Shape(2, 2));
         jshell> array.repeat(1, 2);
         ND: (6) cpu() float32
         [0., 0., 1., 1., 2., 2.]
         
        Specified by:
        repeat in interface NDArray
        Parameters:
        axis - the axis to repeat
        repeats - the number of times to repeat for each axis
        Returns:
        an NDArray that has been repeated
      • repeat

        public NDArray repeat​(long[] repeats)
        Repeats element of this NDArray the number of times given repeats along each axis.

        Examples

         jshell> NDArray array = manager.create(new float[] {0f, 1f, 2f, 3f}, new Shape(2, 2));
         jshell> array.repeat(new long[] {2, 2});
         ND: (12) cpu() float32
         [0., 0., 0., 0., 1., 1., 1., 1., 2., 2., 2., 2.]
         
        Specified by:
        repeat in interface NDArray
        Parameters:
        repeats - the number of times to repeat along each axis
        Returns:
        a NDArray that has been repeated
      • repeat

        public NDArray repeat​(Shape desiredShape)
        Repeats element of this NDArray to match the desired shape.

        If the desired Shape has fewer dimensions that the array, it will repeat against the last axis.

        Examples

         jshell> NDArray array = manager.create(new float[] {0f, 1f, 2f, 3f}, new Shape(2, 2));
         jshell> array.repeat(new Shape(4, 4));
         ND: (4, 4) cpu() float32
         [[0., 0., 1., 1.],
          [0., 0., 1., 1.],
          [2., 2., 3., 3.],
          [2., 2., 3., 3.],
         ]
         
        Specified by:
        repeat in interface NDArray
        Parameters:
        desiredShape - the Shape that should be converted to
        Returns:
        an NDArray that has been repeated
      • dot

        public NDArray dot​(NDArray other)
        Dot product of this NDArray and the other NDArray.
        • If both this NDArray and the other NDArray are 1-D NDArrays, it is inner product of vectors (without complex conjugation).
        • If both this NDArray and the other NDArray are 2-D NDArrays, it is matrix multiplication.
        • If either this NDArray or the other NDArray is 0-D NDArray (scalar), it is equivalent to mul.
        • If this NDArray is N-D NDArray and the other NDArray is 1-D NDArray, it is a sum product over the last axis of those.
        • If this NDArray is N-D NDArray and the other NDArray is M-D NDArray(where M>=2), it is a sum product over the last axis of this NDArray and the second-to-last axis of the other NDArray

        Examples

         jshell> NDArray array1 = manager.create(new float[] {1f, 2f, 3f});
         jshell> NDArray array2 = manager.create(new float[] {4f, 5f, 6f});
         jshell> array1.dot(array2); // inner product
         ND: () cpu() float32
         32.
         jshell> array1 = manager.create(new float[] {1f, 2f, 3f, 4f}, new Shape(2, 2));
         jshell> array2 = manager.create(new float[] {5f, 6f, 7f, 8f}, new Shape(2, 2));
         jshell> array1.dot(array2); // matrix multiplication
         ND: (2, 2) cpu() float32
         [[19., 22.],
          [43., 50.],
         ]
         jshell> array1 = manager.create(new float[] {1f, 2f, 3f, 4f}, new Shape(2, 2));
         jshell> array2 = manager.create(5f);
         jshell> array1.dot(array2);
         ND: (2, 2) cpu() float32
         [[ 5., 10.],
          [15., 20.],
         ]
         jshell> array1 = manager.create(new float[] {1f, 2f, 3f, 4f}, new Shape(2, 2));
         jshell> array2 = manager.create(new float[] {1f, 2f});
         jshell> array1.dot(array2);
         ND: (2) cpu() float32
         [ 5., 11.]
         jshell> array1 = manager.create(new float[] {1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f}, new Shape(2, 2, 2));
         jshell> array2 = manager.create(new float[] {1f, 2f, 3f ,4f}, new Shape(2, 2));
         jshell> array1.dot(array2);
         ND: (2, 2, 2) cpu() float32
         [[[ 7., 10.],
           [15., 22.],
          ],
          [[23., 34.],
           [31., 46.],
          ],
         ]
         
        Specified by:
        dot in interface NDArray
        Parameters:
        other - the other NDArray to perform dot product with
        Returns:
        the result NDArray
      • matMul

        public NDArray matMul​(NDArray other)
        Product matrix of this NDArray and the other NDArray.

        The behavior depends on the arguments in the following way.

        • If both this NDArray and the other NDArray are 2-D NDArrays, they are multiplied like conventional matrices
        • If either this NDArray or the other NDArray is N-D NDArray, N > 2 , it is treated as a stack of matrices residing in the last two indexes and broadcast accordingly.
        • If this NDArray is 1-D NDArray, it is promoted to a matrix by prepending a 1 to its dimensions. After matrix multiplication the prepended 1 is removed.
        • If other NDArray is 1-D NDArray, it is promoted to a matrix by appending a 1 to its dimensions. After matrix multiplication the appended 1 is removed.

        Examples

         jshell> NDArray array1 = manager.create(new float[] {1f, 0f, 0f, 1f}, new Shape(2, 2));
         jshell> NDArray array2 = manager.create(new float[] {4f, 1f, 2f, 2f}, new Shape(2, 2));
         jshell> array1.matMul(array2); // for 2-D arrays, it is the matrix product
         ND: (2, 2) cpu() float32
         [[4., 1.],
          [2., 2.],
         ]
         jshell> array1 = manager.create(new float[] {1f, 0f, 0f, 1f}, new Shape(2, 2));
         jshell> array2 = manager.create(new float[] {1f, 2f});
         jshell> array1.matMul(array2);
         ND: (2) cpu() float32
         [1., 2.]
         jshell> array1 = manager.create(new float[] {1f, 0f, 0f, 1f}, new Shape(2, 2));
         jshell> array2 = manager.create(new float[] {1f, 2f});
         jshell> array1.matMul(array2);
         ND: (2) cpu() float32
         [1., 2.]
         jshell> array1 = manager.arange(2f * 2f * 4f).reshape(2, 2, 4);
         jshell> array2 = manager.arange(2f * 2f * 4f).reshape(2, 4, 2);
         jshell> array1.matMul(array2).get("0, 1, 1");
         ND: () cpu() float32
         98.
         
        Specified by:
        matMul in interface NDArray
        Parameters:
        other - the other NDArray to perform matrix product with
        Returns:
        the result NDArray
      • clip

        public NDArray clip​(java.lang.Number min,
                            java.lang.Number max)
        Clips (limit) the values in this NDArray.

        Given an interval, values outside the interval are clipped to the interval edges. For example, if an interval of [0, 1] is specified, values smaller than 0 become 0, and values larger than 1 become 1.

        Examples

         jshell> NDArray array = manager.arange(10f);
         jshell> array.clip(1, 8);
         ND: (10) cpu() float32
         [1., 1., 2., 3., 4., 5., 6., 7., 8., 8.]
         
        Specified by:
        clip in interface NDArray
        Parameters:
        min - the minimum value
        max - the maximum value
        Returns:
        an NDArray with the elements of this NDArray, but where values < min are replaced with min, and those > max with max
      • flip

        public NDArray flip​(int... axes)
        Returns the reverse order of elements in an array along the given axis.

        The shape of the array is preserved, but the elements are reordered.

        Specified by:
        flip in interface NDArray
        Parameters:
        axes - the axes to flip on
        Returns:
        the newly flipped array
      • transpose

        public NDArray transpose()
        Returns this NDArray with axes transposed.

        Examples

         jshell> NDArray array = manager.create(new float[] {1f, 2f ,3f, 4f}, new Shape(2, 2));
         jshell> array;
         ND: (2, 2) cpu() float32
         [[1., 2.],
          [3., 4.],
         ]
         jshell> array.transpose();
         ND: (2, 2) cpu() float32
         [[1., 3.],
          [2., 4.],
         ]
         
        Specified by:
        transpose in interface NDArray
        Returns:
        the newly permuted array
      • transpose

        public NDArray transpose​(int... axes)
        Returns this NDArray with given axes transposed.

        Examples

         jshell> NDArray array = manager.create(new float[] {1f, 2f ,3f, 4f}, new Shape(2, 2));
         jshell> array;
         ND: (2, 2) cpu() float32
         [[1., 2.],
          [3., 4.],
         ]
         jshell> array.transpose(1, 0);
         ND: (2, 2) cpu() float32
         [[1., 3.],
          [2., 4.],
         ]
         jshell> array = manager.arange(8f).reshape(2, 2, 2);
         jshell> array;
         ND: (2, 2, 2) cpu() float32
         [[[0., 1.],
           [2., 3.],
          ],
          [[4., 5.],
           [6., 7.],
          ],
         ]
         jshell> array.transpose(1, 0, 2);
         ND: (2, 2, 2) cpu() float32
         [[[0., 1.],
           [4., 5.],
          ],
          [[2., 3.],
           [6., 7.],
          ],
         ]
         
        Specified by:
        transpose in interface NDArray
        Parameters:
        axes - the axes to swap to
        Returns:
        the transposed NDArray
      • broadcast

        public NDArray broadcast​(Shape shape)
        Broadcasts this NDArray to be the given shape.

        Examples

         jshell> NDArray array = manager.create(new float[] {1f, 2f ,3f, 4f}, new Shape(2, 2));
         jshell> array;
         ND: (2, 2) cpu() float32
         [[1., 2.],
          [3., 4.],
         ]
         jshell> array.broadcast(new Shape(2, 2, 2));
         ND: (2, 2, 2) cpu() float32
         [[[1., 2.],
           [3., 4.],
          ],
          [[1., 2.],
           [3., 4.],
          ],
         ]
         
        Specified by:
        broadcast in interface NDArray
        Parameters:
        shape - the new Shape of this NDArray
        Returns:
        the broadcasted NDArray
      • argMax

        public NDArray argMax()
        Returns the indices of the maximum values into the flattened NDArray.

        Examples

         jshell> NDArray array = manager.arange(6f).reshape(2, 3);
         jshell> array;
         ND: (2, 3) cpu() float32
         [[0., 1., 2.],
          [3., 4., 5.],
         ]
         jshell> array.argMax();
         ND: () cpu() int64
         5.
         
        Specified by:
        argMax in interface NDArray
        Returns:
        a NDArray containing indices
      • argMax

        public NDArray argMax​(int axis)
        Returns the indices of the maximum values along given axis.

        Examples

         jshell> NDArray array = manager.arange(6f).reshape(2, 3);
         jshell> array;
         ND: (2, 3) cpu() float32
         [[0., 1., 2.],
          [3., 4., 5.],
         ]
         jshell> array.argMax(0);
         ND: (3) cpu() int64
         [1, 1, 1]
         jshell> array.argMax(1);
         ND: (2) cpu() int64
         [2, 2]
         
        Specified by:
        argMax in interface NDArray
        Parameters:
        axis - the axis along which to find maximum values
        Returns:
        a NDArray containing indices
      • argMin

        public NDArray argMin()
        Returns the indices of the minimum values into the flattened NDArray.

        Examples

         jshell> NDArray array = manager.arange(6f).reshape(2, 3);
         jshell> array;
         ND: (2, 3) cpu() float32
         [[0., 1., 2.],
          [3., 4., 5.],
         ]
         jshell> array.argMin();
         ND: () cpu() int64
         0.
         
        Specified by:
        argMin in interface NDArray
        Returns:
        a NDArray containing indices
      • argMin

        public NDArray argMin​(int axis)
        Returns the indices of the minimum values along given axis.

        Examples

         jshell> NDArray array = manager.arange(6f).reshape(2, 3);
         jshell> array;
         ND: (2, 3) cpu() float32
         [[0., 1., 2.],
          [3., 4., 5.],
         ]
         jshell> array.argMin(0);
         ND: (3) cpu() int64
         [0, 0, 0]
         jshell> array.argMin(1);
         ND: (2) cpu() int64
         [0, 0]
         
        Specified by:
        argMin in interface NDArray
        Parameters:
        axis - the axis along which to find minimum values
        Returns:
        a NDArray containing indices
      • percentile

        public NDArray percentile​(java.lang.Number percentile)
        Returns percentile for this NDArray.
        Specified by:
        percentile in interface NDArray
        Parameters:
        percentile - the target percentile in range of 0..100
        Returns:
        the result NDArray
      • percentile

        public NDArray percentile​(java.lang.Number percentile,
                                  int[] axes)
        Returns median along given dimension(s).
        Specified by:
        percentile in interface NDArray
        Parameters:
        percentile - the target percentile in range of 0..100
        axes - the dimension to calculate percentile for
        Returns:
        the result NDArray NDArray
      • median

        public NDArray median()
        Returns median value for this NDArray.
        Specified by:
        median in interface NDArray
        Returns:
        the median NDArray
      • median

        public NDArray median​(int[] axes)
        Returns median value along given axes.
        Specified by:
        median in interface NDArray
        Parameters:
        axes - the axes along which to perform the median operation
        Returns:
        the median NDArray along the specified axes
      • toDense

        public NDArray toDense()
        Returns a dense representation of the sparse NDArray.
        Specified by:
        toDense in interface NDArray
        Returns:
        the result NDArray
      • nonzero

        public NDArray nonzero()
        Returns the indices of elements that are non-zero.

        Note that the behavior is slightly different from numpy.nonzero. Numpy returns a tuple of NDArray, one for each dimension of NDArray. DJL nonzero returns only one NDArray with last dimension containing all dimension of indices.

        Examples

         jshell> NDArray array = manager.create(new float[] {1f, 1f, 1f, 0f, 1f});
         jshell> array.nonzero();
         ND: (4, 1) cpu() int64
         [[ 0],
          [ 1],
          [ 2],
          [ 4],
         ]
         jshell> array = manager.create(new float[] {3f, 0f, 0f, 0f, 4f, 0f, 5f, 6f, 0f}).reshape(3, 3);
         jshell> array;
         ND: (3, 3) cpu() float32
         [[3., 0., 0.],
          [0., 4., 0.],
          [5., 6., 0.],
         ]
         jshell> array.nonzero();
         ND: (4, 2) cpu() int64
         [[ 0,  0],
          [ 1,  1],
          [ 2,  0],
          [ 2,  1],
         ]
         
        Specified by:
        nonzero in interface NDArray
        Returns:
        the indices of the elements that are non-zero
      • erfinv

        public NDArray erfinv()
        Returns element-wise inverse gauss error function of the NDArray.

        Examples

         jshell> NDArray array = manager.create(new float[] {0f, 0.5f, -1f});
         jshell> array.erfinv();
         ND: (3) cpu() float32
         [0., 0.4769, -inf]
         
        Specified by:
        erfinv in interface NDArray
        Returns:
        The inverse of gauss error of the NDArray, element-wise
      • inverse

        public NDArray inverse()
        Computes the inverse of square NDArray if it exists.
        Specified by:
        inverse in interface NDArray
        Returns:
        the inverse of square NDArray.
      • norm

        public NDArray norm​(boolean keepDims)
        Returns the norm of this NDArray.

        Examples

         jshell> NDArray array = manager.create(new float[] {-3f, -4f});
         jshell> array.norm(true);
         ND: () cpu() float32
         5.
         jshell> NDArray array = manager.create(new float[] {1f, 2f, 3f, 4f}, new Shape(2, 2));
         jshell> array.norm(true);
         ND: () cpu() float32
         [[5.4772],
         ]
         
        Specified by:
        norm in interface NDArray
        Parameters:
        keepDims - If this is set to True, the axes which are normed over are left in the result as dimensions with size one. With this option the result will broadcast correctly against the original x.
        Returns:
        the norm of this NDArray
      • norm

        public NDArray norm​(int ord,
                            int[] axes,
                            boolean keepDims)
        Returns the norm of this NDArray.

        Examples

         jshell> NDArray array = manager.create(new float[] {1f, 2f, 3f, 4f}, new Shape(2, 2));
         jshell> array.norm(2, new int[] {0}, true);
         ND: (1, 2) cpu() float32
         [[3.1623, 4.4721],
         ]
         jshell> NDArray array = manager.create(new float[] {1f, 2f, 3f, 4f}, new Shape(2, 2));
         jshell> array.norm(2, new int[] {0}, false);
         ND: (2) cpu() float32
         [3.1623, 4.4721]
         
        Specified by:
        norm in interface NDArray
        Parameters:
        ord - Order of the norm.
        axes - If axes contains an integer, it specifies the axis of x along which to compute the vector norms. If axis contains 2 integers, it specifies the axes that hold 2-D matrices, and the matrix norms of these matrices are computed.
        keepDims - keepDims If this is set to True, the axes which are normed over are left in the result as dimensions with size one. With this option the result will broadcast correctly against the original x.
        Returns:
        the norm of this NDArray
      • oneHot

        public NDArray oneHot​(int depth,
                              float onValue,
                              float offValue,
                              DataType dataType)
        Returns a one-hot NDArray.
        • The locations represented by indices take value onValue, while all other locations take value offValue.
        • If the input NDArray is rank N, the output will have rank N+1. The new axis is appended at the end.
        • If NDArray is a scalar the output shape will be a vector of length depth.
        • If NDArray is a vector of length features, the output shape will be features x depth.
        • If NDArray is a matrix with shape [batch, features], the output shape will be batch x features x depth.

        Examples

         jshell> NDArray array = manager.create(new int[] {1, 0, 2, 0});
         jshell> array.oneHot(3, 8f, 1f, array.getDataType());
         ND: (4, 3) cpu() int32
         [[ 1,  8,  1],
          [ 8,  1,  1],
          [ 1,  1,  8],
          [ 8,  1,  1],
         ]
         
        Specified by:
        oneHot in interface NDArray
        Parameters:
        depth - Depth of the one hot dimension.
        onValue - The value assigned to the locations represented by indices.
        offValue - The value assigned to the locations not represented by indices.
        dataType - dataType of the output.
        Returns:
        one-hot encoding of this NDArray
        See Also:
        Classification-problems
      • batchDot

        public NDArray batchDot​(NDArray other)
        Batchwise product of this NDArray and the other NDArray.
        • batchDot is used to compute dot product of x and y when x and y are data in batch, namely N-D (N greater or equal to 3) arrays in shape of (B0, …, B_i, :, :). For example, given x with shape (B_0, …, B_i, N, M) and y with shape (B_0, …, B_i, M, K), the result array will have shape (B_0, …, B_i, N, K), which is computed by: batch_dot(x,y)[b_0, ..., b_i, :, :] = dot(x[b_0, ..., b_i, :, :], y[b_0, ..., b_i, :, :])

        Examples

         jshell> NDArray array1 = manager.ones(new Shape(2, 1, 4));
         jshell> NDArray array2 = manager.ones(new Shape(2, 4, 6));
         jshell> array1.batchDot(array2);
         ND: (2, 1, 6) cpu() float32
         [[[4., 4., 4., 4., 4., 4.],
          ],
          [[4., 4., 4., 4., 4., 4.],
          ],
         ]
         
        Specified by:
        batchDot in interface NDArray
        Parameters:
        other - the other NDArray to perform batch dot product with
        Returns:
        the result NDArray
      • getNDArrayInternal

        public ai.djl.ndarray.internal.NDArrayEx getNDArrayInternal()
        Returns an internal representative of Native NDArray.

        This method should only be used by Engine provider

        Specified by:
        getNDArrayInternal in interface NDArray
        Returns:
        an internal representative of Native NDArray
      • close

        public void close()
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface NDArray
        Specified by:
        close in interface NDResource
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object