Interface NDArray

All Superinterfaces:
AutoCloseable, BytesSupplier, NDResource
All Known Subinterfaces:
LazyNDArray, SparseNDArray
All Known Implementing Classes:
NDArrayAdapter

public interface NDArray extends NDResource, BytesSupplier
An interface representing an n-dimensional array.

NDArray is the core data structure for all mathematical computations. An NDArray represents a multidimensional, fixed-size homogeneous array. It has very similar behaviour to the Numpy python package with the addition of efficient computing. To understand how to manage NDArray lifecycle, please refer to NDArray Memory Management Guide

  • Method Summary

    Modifier and Type
    Method
    Description
    abs()
    Returns the absolute value of this NDArray element-wise.
    Returns the inverse trigonometric cosine of this NDArray element-wise.
    Returns the inverse hyperbolic cosine of this NDArray element-wise.
    add(NDArray other)
    Adds other NDArrays to this NDArray element-wise.
    Adds a number to this NDArray element-wise.
    addi(NDArray other)
    Adds other NDArrays to this NDArray element-wise in place.
    Adds a number to this NDArray element-wise in place.
    default NDArray
    all()
    Returns true if all elements within this NDArray are non-zero or true.
    default boolean
    Returns true if two NDArrays are element-wise equal within a tolerance.
    default boolean
    allClose(NDArray other, double rtol, double atol, boolean equalNan)
    Returns true if two NDArray are element-wise equal within a tolerance.
    default NDArray
    any()
    Returns true if any of the elements within this NDArray are non-zero or true.
    Returns the indices of the maximum values into the flattened NDArray.
    argMax(int axis)
    Returns the indices of the maximum values along given axis.
    Returns the indices of the minimum values into the flattened NDArray.
    argMin(int axis)
    Returns the indices of the minimum values along given axis.
    default NDArray
    Returns the indices that would sort this NDArray.
    default NDArray
    argSort(int axis)
    Returns the indices that would sort this NDArray given the axis.
    argSort(int axis, boolean ascending)
    Returns the indices that would sort this NDArray given the axis.
    Returns the inverse trigonometric sine of this NDArray element-wise.
    Returns the inverse hyperbolic sine of this NDArray element-wise.
    Returns the inverse trigonometric tangent of this NDArray element-wise.
    atan2(NDArray other)
    Returns the element-wise arc-tangent of this/other choosing the quadrant correctly.
    Returns the inverse hyperbolic tangent of this NDArray element-wise.
    Batchwise product of this NDArray and the other NDArray.
    Batch product matrix of this NDArray and the other NDArray.
    default NDArray
    Returns portion of this NDArray given the index boolean NDArray along first axis.
    booleanMask(NDArray index, int axis)
    Returns portion of this NDArray given the index boolean NDArray along given axis.
    default NDArray
    broadcast(long... shape)
    Broadcasts this NDArray to be the given shape.
    Broadcasts this NDArray to be the given shape.
    Returns the cube-root of this NDArray element-wise.
    Returns the ceiling of this NDArray element-wise.
    clip(Number min, Number max)
    Clips (limit) the values in this NDArray.
    void
    Convert a general NDArray to its complex math format.
    default NDArray
    concat(NDArray array)
    Joins a NDArray along the first axis.
    default NDArray
    concat(NDArray array, int axis)
    Joins a NDArray along an existing axis.
    Conjugate complex array.
    boolean
    Returns true if all elements in this NDArray are equal to the other NDArray.
    boolean
    Returns true if all elements in this NDArray are equal to the Number.
    default void
    copyTo(NDArray array)
    Deep-copies the current NDArray to the one passed in.
    cos()
    Returns the trigonometric cosine of this NDArray element-wise.
    Returns the hyperbolic cosine of this NDArray element-wise.
    default NDArray
    Counts the number of non-zero values in this NDArray.
    default NDArray
    countNonzero(int axis)
    Counts the number of non-zero values in this NDArray along a given axis.
    cumProd(int axis)
    Returns the cumulative product of elements of input in the dimension dim.
    cumProd(int axis, DataType dataType)
    Returns the cumulative product of elements of input in the dimension dim.
    Returns the cumulative sum of the elements in the flattened NDArray.
    cumSum(int axis)
    Return the cumulative sum of the elements along a given axis.
    static NDArray
    decode(NDManager manager, byte[] byteArray)
    Decodes NDArray from bytes.
    div(NDArray other)
    Divides this NDArray by the other NDArray element-wise.
    Divides this NDArray by a number element-wise.
    divi(NDArray other)
    Divides this NDArray by the other NDArray element-wise in place.
    Divides this NDArray by a number element-wise in place.
    dot(NDArray other)
    Dot product of this NDArray and the other NDArray.
    default NDArray
    Returns a copy of this NDArray.
    default byte[]
    Encodes NDArray to byte array.
    eq(NDArray other)
    Returns the boolean NDArray for element-wise "Equals" comparison.
    Returns the boolean NDArray for element-wise "Equals" comparison.
    erf()
    Returns element-wise gauss error function of the NDArray.
    Returns element-wise inverse gauss error function of the NDArray.
    exp()
    Returns the exponential value of this NDArray element-wise.
    expandDims(int axis)
    Expands the Shape of a NDArray.
    default NDArray
    fft(long length)
    Computes the one-dimensional discrete Fourier Transform.
    fft(long length, long axis)
    Computes the one-dimensional discrete Fourier Transform.
    default NDArray
    fft2(long[] sizes)
    Computes the two-dimensional Discrete Fourier Transform along the last 2 axes.
    fft2(long[] sizes, long[] axes)
    Computes the two-dimensional Discrete Fourier Transform.
    Flattens this NDArray into a 1-D NDArray in row-major order.
    flatten(int startDim, int endDim)
    Flattens this NDArray into a partially flatten NDArray.
    flip(int... axes)
    Returns the reverse order of elements in an array along the given axis.
    Returns the floor of this NDArray element-wise.
    Return the log of the absolute value of the gamma function of this NDArray element-wise.
    gather(NDArray index, int axis)
    Returns a partial NDArray pointed by the indexed array.
    Returns a partial NDArray pointed by the indexed array.
    default NDArray
    get(long... indices)
    Returns a partial NDArray.
    default NDArray
    get(NDIndex index)
    Returns a partial NDArray.
    default NDArray
    get(NDArray index)
    Returns a partial NDArray.
    default NDArray
    get(NDManager manager, long... indices)
    Returns a partial NDArray.
    default NDArray
    get(NDManager manager, NDIndex index)
    Returns a partial NDArray.
    default NDArray
    get(String indices, Object... args)
    Returns a partial NDArray.
    default boolean
    getBoolean(long... indices)
    Returns a boolean element from this NDArray.
    default byte
    getByte(long... indices)
    Returns an byte element from this NDArray.
    Returns the DataType of this NDArray.
    Returns the Device of this NDArray.
    default double
    getDouble(long... indices)
    Returns a double element from this NDArray.
    default float
    getFloat(long... indices)
    Returns a float element from this NDArray.
    Returns the gradient NDArray attached to this NDArray.
    default int
    getInt(long... indices)
    Returns an int element from this NDArray.
    default long
    getLong(long... indices)
    Returns a long element from this NDArray.
    Returns the name of this NDArray.
    ai.djl.ndarray.internal.NDArrayEx
    Returns an internal representative of Native NDArray.
    default List<NDArray>
    Returns the NDArray or NDArrays contained within this resource.
    default NDArray
    getScalar(long... indices)
    Returns a scalar NDArray corresponding to a single element.
    Returns the Shape of this NDArray.
    Returns the SparseFormat of this NDArray.
    Returns unique identifier of this NDArray.
    default int
    getUint8(long... indices)
    Returns an integer element from this NDArray that represent unsigned integer with 8 bits.
    gt(NDArray other)
    Returns the boolean NDArray for element-wise "Greater Than" comparison.
    Returns the boolean NDArray for element-wise "Greater" comparison.
    gte(NDArray other)
    Returns the boolean NDArray for element-wise "Greater or equals" comparison.
    Returns the boolean NDArray for element-wise "Greater or equals" comparison.
    boolean
    Returns true if the gradient calculation is required for this NDArray.
    default NDArray
    ifft(long length)
    Computes the one dimensional inverse discrete Fourier transform.
    ifft(long length, long axis)
    Computes the one dimensional inverse discrete Fourier transform.
    default NDArray
    ifft2(long[] sizes)
    Computes the two-dimensional inverse Discrete Fourier Transform along the last 2 axes.
    ifft2(long[] sizes, long[] axes)
    Computes the two-dimensional inverse Discrete Fourier Transform.
    void
    intern(NDArray replaced)
    Replace the handle of the NDArray with the other.
    Computes the inverse of square NDArray if it exists.
    default NDArray
    irfft(long length)
    Computes the one dimensional inverse Fourier transform of real-valued input.
    irfft(long length, long axis)
    Computes the one dimensional inverse Fourier transform of real-valued input.
    default boolean
    Returns true if this NDArray is special case: no-value NDArray.
    Returns the boolean NDArray with value true where this NDArray's entries are infinite, or false where they are not infinite.
    Returns the boolean NDArray with value true where this NDArray's entries are NaN, or false where they are not NaN.
    boolean
    Returns true if this NDArray has been released.
    default boolean
    Returns true if this NDArray is a scalar NDArray with empty Shape.
    default boolean
    Returns true if this NDArray is a SparseNDArray.
    default NDArray
    Returns an uninitialized NDArray with the same Shape, DataType and SparseFormat as the input NDArray.
    log()
    Returns the natural logarithmic value of this NDArray element-wise.
    Returns the base 10 logarithm of this NDArray element-wise.
    Returns the base 2 logarithm of this NDArray element-wise.
    Returns the truth value of this NDArray AND the other NDArray element-wise.
    Computes the truth value of NOT this NDArray element-wise.
    Computes the truth value of this NDArray OR the other NDArray element-wise.
    Computes the truth value of this NDArray XOR the other NDArray element-wise.
    logSoftmax(int axis)
    Applies the softmax function followed by a logarithm.
    lt(NDArray other)
    Returns the boolean NDArray for element-wise "Less" comparison.
    Returns the boolean NDArray for element-wise "Less" comparison.
    lte(NDArray other)
    Returns the boolean NDArray for element-wise "Less or equals" comparison.
    Returns the boolean NDArray for element-wise "Less or equals" comparison.
    matMul(NDArray other)
    Product matrix of this NDArray and the other NDArray.
    max()
    Returns the maximum of this NDArray.
    default NDArray
    max(int[] axes)
    Returns the maximum of this NDArray along given axes.
    max(int[] axes, boolean keepDims)
    Returns the maximum of this NDArray along given axes.
    Returns the maximum of this NDArray and the other NDArray element-wise.
    Returns the maximum of this NDArray and a number element-wise.
    Returns the average of this NDArray.
    default NDArray
    mean(int[] axes)
    Returns the average of this NDArray along given axes.
    mean(int[] axes, boolean keepDims)
    Returns the average of this NDArray along given axes.
    Returns median value for this NDArray.
    median(int[] axes)
    Returns median value along given axes.
    min()
    Returns the minimum of this NDArray.
    default NDArray
    min(int[] axes)
    Returns the minimum of this NDArray along given axes.
    min(int[] axes, boolean keepDims)
    Returns the minimum of this NDArray along given axes.
    Returns the minimum of this NDArray and the other NDArray element-wise.
    Returns the minimum of this NDArray and a number element-wise.
    mod(NDArray other)
    Returns element-wise remainder of division.
    Returns element-wise remainder of division.
    modi(NDArray other)
    Returns in place element-wise remainder of division in place.
    Returns element-wise remainder of division in place.
    mul(NDArray other)
    Multiplies this NDArray by other NDArrays element-wise.
    Multiplies this NDArray by a number element-wise.
    muli(NDArray other)
    Multiplies this NDArray by other NDArray element-wise in place.
    Multiplies this NDArray by a number element-wise in place.
    neg()
    Returns the numerical negative NDArray element-wise.
    Returns the numerical negative NDArray element-wise in place.
    neq(NDArray other)
    Returns the boolean NDArray for element-wise "Not equals" comparison.
    Returns the boolean NDArray for element-wise "Not equals" comparison.
    default NDArray
    Returns true if none of the elements within this NDArray are non-zero or true.
    Returns the indices of elements that are non-zero.
    default NDArray
    Returns the norm of this NDArray.
    norm(boolean keepDims)
    Returns the norm of this NDArray.
    default NDArray
    norm(int[] axes)
    Returns the norm of this NDArray.
    default NDArray
    norm(int[] axes, boolean keepDims)
    Returns the norm of this NDArray.
    norm(int ord, int[] axes, boolean keepDims)
    Returns the norm of this NDArray.
    default NDArray
    Performs Lp normalization of the array over specified dimension.
    default NDArray
    normalize(double exponent, long dim)
    Performs Lp normalization of the array over specified dimension.
    normalize(double exponent, long dim, double eps)
    Performs Lp normalization of the array over specified dimension.
    default NDArray
    oneHot(int depth)
    Returns a one-hot NDArray.
    oneHot(int depth, float onValue, float offValue, DataType dataType)
    Returns a one-hot NDArray.
    default NDArray
    oneHot(int depth, DataType dataType)
    Returns a one-hot NDArray.
    default NDArray
    Returns an NDArray of ones with the same Shape, DataType and SparseFormat as the input NDArray.
    pad(Shape padding, double value)
    Pads this NDArray with the given Shape.
    percentile(Number percentile)
    Returns percentile for this NDArray.
    percentile(Number percentile, int[] axes)
    Returns median along given dimension(s).
    pow(NDArray other)
    Takes the power of this NDArray with the other NDArray element-wise.
    Takes the power of this NDArray with a number element-wise.
    powi(NDArray other)
    Takes the power of this NDArray with the other NDArray element-wise in place.
    Takes the power of this NDArray with a number element-wise in place.
    Returns the product of this NDArray.
    default NDArray
    prod(int[] axes)
    Returns the product of this NDArray elements over the given axes.
    prod(int[] axes, boolean keepDims)
    Returns the product of this NDArray elements over the given axes.
    put(NDArray index, NDArray value)
    Sets the entries of NDArray pointed by index, according to linear indexing, to be the numbers in value.
    Convert a complex NDArray to its real math format.
    repeat(int axis, long repeats)
    Repeats element of this NDArray the number of times given repeats along given axis.
    repeat(long repeats)
    Repeats element of this NDArray the number of times given repeats.
    repeat(long[] repeats)
    Repeats element of this NDArray the number of times given repeats along each axis.
    repeat(Shape desiredShape)
    Repeats element of this NDArray to match the desired shape.
    default NDArray
    reshape(long... newShape)
    Reshapes this NDArray to the given Shape.
    reshape(Shape shape)
    Reshapes this NDArray to the given Shape.
    default NDArray
    rfft(long length)
    Computes the one dimensional Fourier transform of real-valued input.
    rfft(long length, long axis)
    Computes the one dimensional Fourier transform of real-valued input.
    rotate90(int times, int[] axes)
    Rotates an array by 90 degrees in the plane specified by axes.
    Returns the round of this NDArray element-wise.
    default NDArray
    scaleGradient(double scale)
    Returns an NDArray equal to this that magnifies the gradient propagated to this by a constant.
    scatter(NDArray index, NDArray value, int axis)
    Writes all values from the tensor value into self at the indices specified in the index tensor.
    sequenceMask(NDArray sequenceLength)
    Sets all elements outside the sequence to 0.
    sequenceMask(NDArray sequenceLength, float value)
    Sets all elements outside the sequence to a constant value.
    default void
    set(byte[] data)
    Sets this NDArray value from an array of bytes.
    default void
    set(double[] data)
    Sets this NDArray value from an array of doubles.
    default void
    set(float[] data)
    Sets this NDArray value from an array of floats.
    default void
    set(int[] data)
    Sets this NDArray value from an array of ints.
    default void
    set(long[] data)
    Sets this NDArray value from an array of longs.
    default void
    set(NDIndex index, NDArray value)
    Sets the specified index in this NDArray with the given values.
    default void
    set(NDIndex index, Number value)
    Sets the specified index in this NDArray with the given value.
    default void
    set(NDIndex index, Function<NDArray,NDArray> function)
    Sets the specific index by a function.
    default void
    set(NDArray index, Number value)
    Sets the NDArray by boolean mask or integer index.
    void
    set(Buffer buffer)
    Sets this NDArray value from Buffer.
    void
    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.
    default void
    setScalar(NDIndex index, Number value)
    Sets the specified scalar in this NDArray with the given value.
    default boolean
    Checks 2 NDArrays for equal shapes.
    Returns the element-wise sign.
    Returns the element-wise sign in-place.
    sin()
    Returns the trigonometric sine of this NDArray element-wise.
    Returns the hyperbolic sine of this NDArray element-wise.
    default long
    Returns the total number of elements in this NDArray.
    default long
    size(int axis)
    Returns the size of this NDArray along a given axis.
    softmax(int axis)
    Applies the softmax function along the given axis.
    Sorts the flattened NDArray.
    sort(int axis)
    Sorts the flattened NDArray.
    default NDList
    split(long sections)
    Splits this NDArray into multiple subNDArrays given sections along first axis.
    default NDList
    split(long[] indices)
    Splits this NDArray into multiple sub-NDArrays given indices along first axis.
    split(long[] indices, int axis)
    Splits this NDArray into multiple sub-NDArrays given indices along given axis.
    default NDList
    split(long sections, int axis)
    Splits this NDArray into multiple subNDArrays given sections along the given axis.
    Returns the square root of this NDArray element-wise.
    Returns the square of this NDArray element-wise.
    default NDArray
    Removes all singleton dimensions from this NDArray Shape.
    default NDArray
    squeeze(int axis)
    Removes a singleton dimension at the given axis.
    squeeze(int[] axes)
    Removes singleton dimensions at the given axes.
    default NDArray
    stack(NDArray array)
    Joins a NDArray along the first axis.
    default NDArray
    stack(NDArray array, int axis)
    Joins a NDArray along a new axis.
    default NDArray
    stft(long nFft, long hopLength, boolean center, NDArray window, boolean returnComplex)
    Computes the Short Time Fourier Transform (STFT).
    stft(long nFft, long hopLength, boolean center, NDArray window, boolean normalize, boolean returnComplex)
    Computes the Short Time Fourier Transform (STFT).
    Returns an NDArray equal to this that stop gradient propagation through it.
    sub(NDArray other)
    Subtracts the other NDArray from this NDArray element-wise.
    Subtracts a number from this NDArray element-wise.
    subi(NDArray other)
    Subtracts the other NDArray from this NDArray element-wise in place.
    Subtracts a number from this NDArray element-wise in place.
    sum()
    Returns the sum of this NDArray.
    default NDArray
    sum(int[] axes)
    Returns the sum of this NDArray along given axes.
    sum(int[] axes, boolean keepDims)
    Returns the sum of this NDArray along given axes.
    default NDArray
    swapAxes(int axis1, int axis2)
    Interchanges two axes of this NDArray.
    default NDArray
    take(NDArray index)
    Returns a partial NDArray pointed by index according to linear indexing, and the of output is of the same shape as index.
    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.
    tan()
    Returns the trigonometric tangent of this NDArray element-wise.
    Returns the hyperbolic tangent of this NDArray element-wise.
    tile(int axis, long repeats)
    Constructs a NDArray by repeating this NDArray the number of times given by repeats along given axis.
    tile(long repeats)
    Constructs a NDArray by repeating this NDArray the number of times given repeats.
    tile(long[] repeats)
    Constructs a NDArray by repeating this NDArray the number of times given by repeats.
    tile(Shape desiredShape)
    Constructs a NDArray by repeating this NDArray the number of times to match the desired shape.
    default Number[]
    Converts this NDArray to a Number array based on its DataType.
    default boolean[]
    Converts this NDArray to a boolean array.
    default byte[]
    Converts this NDArray to a byte array.
    default ByteBuffer
    Returns the ByteBuffer presentation of the object.
    toByteBuffer(boolean tryDirect)
    Returns the ByteBuffer presentation of the object.
    default String
    Runs the debug string representation of this NDArray.
    default String
    toDebugString(boolean withContent)
    Runs the debug string representation of this NDArray.
    default String
    toDebugString(int maxSize, int maxDepth, int maxRows, int maxColumns, boolean withContent)
    Runs the debug string representation of this NDArray.
    Converts this NDArray from radians to degrees element-wise.
    Returns a dense representation of the sparse NDArray.
    toDevice(Device device, boolean copy)
    Moves this NDArray to a different Device.
    default double[]
    Converts this NDArray to a double array.
    default float[]
    Converts this NDArray to a float array.
    default int[]
    Converts this NDArray to an int array.
    default long[]
    Converts this NDArray to a long array.
    default NDList
    topK(int k, int axis)
    Returns (values, indices) of the top k values along given axis.
    topK(int k, int axis, boolean largest, boolean sorted)
    Returns (values, indices) of the top k values along given axis.
    Converts this NDArray from degrees to radians element-wise.
    default short[]
    Converts this NDArray to an short array.
    Returns a sparse representation of NDArray.
    default String[]
    Converts this NDArray to a String array.
    Converts this NDArray to a String array with the specified charset.
    toType(DataType dataType, boolean copy)
    Converts this NDArray to a different DataType.
    default int[]
    Converts this NDArray to a uint8 array.
    default long[]
    Converts this NDArray to an unsigned int array.
    default int[]
    Converts this NDArray to an short array.
    default NDArray
    Returns the sum along diagonals of this NDArray.
    default NDArray
    trace(int offset)
    Returns the sum along diagonals of this NDArray.
    trace(int offset, int axis1, int axis2)
    Returns the sum along diagonals of this NDArray.
    Returns this NDArray with axes transposed.
    transpose(int... axes)
    Returns this NDArray with given axes transposed.
    Returns the truncated value of this NDArray element-wise.
    default NDList
    Returns the unique elements of the input tensor.
    default NDList
    unique(boolean sorted, boolean returnInverse, boolean returnCounts)
    Returns the unique elements of the input tensor.
    unique(Integer dim, boolean sorted, boolean returnInverse, boolean returnCounts)
    Returns the unique elements of the input tensor.
    xlogy(NDArray other)
    Computes this * log(other).
    default NDArray
    Returns an NDArray of zeros with the same Shape, DataType and SparseFormat as the input NDArray.

    Methods inherited from interface ai.djl.ndarray.BytesSupplier

    getAsBytes, getAsObject, getAsString

    Methods inherited from interface ai.djl.ndarray.NDResource

    attach, detach, getManager, returnResource, tempAttach
  • Method Details

    • decode

      static NDArray decode(NDManager manager, byte[] byteArray)
      Decodes NDArray from bytes.
      Parameters:
      manager - NDManager used to create this NDArray
      byteArray - data used to decode
      Returns:
      decoded NDArray
    • getName

      String getName()
      Returns the name of this NDArray.
      Returns:
      the name of this NDArray
    • setName

      void setName(String name)
      Sets name of this NDArray.
      Parameters:
      name - the name of this NDArray
    • getUid

      String getUid()
      Returns unique identifier of this NDArray.
      Returns:
      unique identifier of this NDArray
    • getDataType

      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.

      Returns:
      the DataType of this NDArray
    • getDevice

      Device getDevice()
      Returns the Device of this NDArray.

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

      Returns:
      the Device of this NDArray
    • getShape

      Shape getShape()
      Returns the Shape of this NDArray.

      Shape defines how this NDArray is represented multi-dimensionally.

      Returns:
      the Shape of this NDArray
    • getSparseFormat

      SparseFormat getSparseFormat()
      Returns the SparseFormat of this NDArray.
      Returns:
      the SparseFormat of this NDArray
    • isSparse

      default boolean isSparse()
      Returns true if this NDArray is a SparseNDArray.
      Returns:
      true if this NDArray is a SparseNDArray
    • isScalar

      default boolean isScalar()
      Returns true if this NDArray is a scalar NDArray with empty Shape.
      Returns:
      true if this NDArray is a scalar NDArray with empty Shape
    • encode

      default byte[] encode()
      Encodes NDArray to byte array.
      Returns:
      byte array
    • toDevice

      NDArray toDevice(Device device, boolean copy)
      Moves this NDArray to a different Device.
      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

      NDArray toType(DataType dataType, boolean copy)
      Converts this NDArray to a different DataType.
      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

      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.
      Parameters:
      requiresGrad - if NDArray requires gradient or not
    • getGradient

      NDArray getGradient()
      Returns the gradient NDArray attached to this NDArray.
      Returns:
      the gradient NDArray
      Throws:
      NullPointerException - when gradient is not initialized
    • hasGradient

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

      NDArray stopGradient()
      Returns an NDArray equal to this that stop gradient propagation through it.
      Returns:
      an NDArray equal to this that stops gradient propagation through it
    • scaleGradient

      default NDArray scaleGradient(double scale)
      Returns an NDArray equal to this that magnifies the gradient propagated to this by a constant.
      Parameters:
      scale - how to much to magnify the gradient propagated to this
      Returns:
      an NDArray equal to this that magnifies the gradient propagated to this by a constant
    • size

      default long size(int axis)
      Returns the size of this NDArray along a given axis.
      Parameters:
      axis - the axis to return the size for
      Returns:
      the size of this NDArray along a given axis
    • size

      default long size()
      Returns the total number of elements in this NDArray.
      Returns:
      the number of elements in this NDArray
    • toByteBuffer

      default ByteBuffer toByteBuffer()
      Returns the ByteBuffer presentation of the object.
      Specified by:
      toByteBuffer in interface BytesSupplier
      Returns:
      the ByteBuffer presentation of the object
    • toByteBuffer

      ByteBuffer toByteBuffer(boolean tryDirect)
      Returns the ByteBuffer presentation of the object.

      If returned ByteBuffer is a DirectByteBuffer, it shared the same native memory as the NDArray. The native memory will be deleted when NDArray is closed.

      Not all the engine support return DirectByteBuffer.

      Parameters:
      tryDirect - use DirectBuffer if possible
      Returns:
      the ByteBuffer presentation of the object
    • toDoubleArray

      default double[] toDoubleArray()
      Converts this NDArray to a double array.
      Returns:
      a double array
      Throws:
      IllegalStateException - when DataType of this NDArray mismatches
    • toFloatArray

      default float[] toFloatArray()
      Converts this NDArray to a float array.
      Returns:
      a float array
      Throws:
      IllegalStateException - when DataType of this NDArray mismatches
    • toShortArray

      default short[] toShortArray()
      Converts this NDArray to an short array.
      Returns:
      an int array
      Throws:
      IllegalStateException - when DataType of this NDArray mismatches
    • toUnsignedShortArray

      default int[] toUnsignedShortArray()
      Converts this NDArray to an short array.
      Returns:
      an int array
      Throws:
      IllegalStateException - when DataType of this NDArray mismatches
    • toIntArray

      default int[] toIntArray()
      Converts this NDArray to an int array.
      Returns:
      an int array
      Throws:
      IllegalStateException - when DataType of this NDArray mismatches
    • toUnsignedIntArray

      default long[] toUnsignedIntArray()
      Converts this NDArray to an unsigned int array.
      Returns:
      a long array
      Throws:
      IllegalStateException - when DataType of this NDArray mismatches
    • toLongArray

      default long[] toLongArray()
      Converts this NDArray to a long array.
      Returns:
      a long array
      Throws:
      IllegalStateException - when DataType of this NDArray mismatches
    • toByteArray

      default byte[] toByteArray()
      Converts this NDArray to a byte array.
      Returns:
      a byte array
      Throws:
      IllegalStateException - when DataType of this NDArray mismatches
    • toUint8Array

      default int[] toUint8Array()
      Converts this NDArray to a uint8 array.
      Returns:
      a uint8 array
      Throws:
      IllegalStateException - when DataType of this NDArray mismatches
    • toBooleanArray

      default boolean[] toBooleanArray()
      Converts this NDArray to a boolean array.
      Returns:
      a boolean array
      Throws:
      IllegalStateException - when DataType of this NDArray mismatches
    • toStringArray

      default String[] toStringArray()
      Converts this NDArray to a String array.

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

      Returns:
      Array of Strings
    • toStringArray

      String[] toStringArray(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

      Parameters:
      charset - to charset for the string
      Returns:
      Array of Strings
    • toArray

      default Number[] toArray()
      Converts this NDArray to a Number array based on its DataType.
      Returns:
      a Number array
    • set

      void set(Buffer buffer)
      Sets this NDArray value from Buffer.
      Parameters:
      buffer - the input buffered data
    • set

      default void set(float[] data)
      Sets this NDArray value from an array of floats.
      Parameters:
      data - the array of floats to set
    • set

      default void set(int[] data)
      Sets this NDArray value from an array of ints.
      Parameters:
      data - the array of integers to set
    • set

      default void set(double[] data)
      Sets this NDArray value from an array of doubles.
      Parameters:
      data - the array of doubles to set
    • set

      default void set(long[] data)
      Sets this NDArray value from an array of longs.
      Parameters:
      data - the array of longs to set
    • set

      default void set(byte[] data)
      Sets this NDArray value from an array of bytes.
      Parameters:
      data - the array of bytes to set
    • set

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

      default void set(NDIndex index, Number value)
      Sets the specified index in this NDArray with the given value.
      Parameters:
      index - the locations to update
      value - the value to replace with
    • set

      default void set(NDIndex index, Function<NDArray,NDArray> function)
      Sets the specific index by a function.
      Parameters:
      index - the locations to update
      function - the function to change the value
    • set

      default void set(NDArray index, Number value)
      Sets the NDArray by boolean mask or integer index.
      Parameters:
      index - the boolean or integer NDArray that indicates what to get
      value - the value to replace with
    • setScalar

      default void setScalar(NDIndex index, Number value)
      Sets the specified scalar in this NDArray with the given value.
      Parameters:
      index - the single index to update
      value - the value to replace with
      Throws:
      IllegalArgumentException - thrown if the index does not correspond to a single element
    • get

      default NDArray get(NDIndex index)
      Returns a partial NDArray.
      Parameters:
      index - the section of this NDArray to return
      Returns:
      the partial NDArray
    • get

      default NDArray get(NDManager manager, NDIndex index)
      Returns a partial NDArray.
      Parameters:
      manager - the manager used to create the arrays
      index - the section of this NDArray to return
      Returns:
      the partial NDArray
    • get

      default NDArray get(NDArray index)
      Returns a partial NDArray.
      Parameters:
      index - the boolean or integer NDArray that indicates what to get
      Returns:
      the partial NDArray
    • get

      default NDArray get(String indices, Object... args)
      Returns a partial NDArray.
      Parameters:
      indices - the indices used to indicate what to get
      args - arguments to replace the varaible "{}" in the indices string. Can be an integer, long, boolean NDArray, or integer NDArray.
      Returns:
      the partial NDArray
      See Also:
    • get

      default NDArray get(long... indices)
      Returns a partial NDArray.
      Parameters:
      indices - the indices with each index corresponding to the dimensions and negative indices starting from the end
      Returns:
      the partial NDArray
    • get

      default NDArray get(NDManager manager, long... indices)
      Returns a partial NDArray.
      Parameters:
      manager - the manager used to create the arrays
      indices - the indices with each index corresponding to the dimensions and negative indices starting from the end
      Returns:
      the partial NDArray
    • gather

      NDArray gather(NDArray index, int axis)
      Returns a partial NDArray pointed by the indexed array.
       out[i][j][k] = input[index[i][j][k]][j][k] # if axis == 0
       out[i][j][k] = input[i][index[i][j][k]][k] # if axis == 1
       out[i][j][k] = input[i][j][index[i][j][k]] # if axis == 2
       
      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
    • gatherNd

      NDArray gatherNd(NDArray index)
      Returns a partial NDArray pointed by the indexed array.
       Given NDArray arr and NDArray idx. idx is the following structure:
       \( idx = [ idx[0, ...], idx[1, ...],..., idx[indexingDepth,...] ] \)
       corresponding to x, y, z index, i.e. [idx_x, idx_y, idx_z, ...].
       

      So indexingDepth smaller than or equal to data.shape[0] If indexingDepth is smaller than data.shape[0], for instance, data.shape[0]=3, i.e. x,y,z but indexingDepth = 2, i.e. [idx_x, idx_y], then the tail co-rank = data.shape[0] - indexingDepth will be kept.

      With it, the output shape = idx_y.shape appended by data.shape[indexingDepth:] mx.symbol.gather_nd

      Parameters:
      index - picks the elements of an NDArray to the same position as index
      Returns:
      the partial NDArray of the same shape as index
    • take

      default NDArray take(NDArray index)
      Returns a partial NDArray pointed by index according to linear indexing, and the of output is of the same shape as index.
      Parameters:
      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
    • take

      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.
      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

      NDArray put(NDArray index, NDArray value)
      Sets the entries of NDArray pointed by index, according to linear indexing, to be the numbers in value.

      Value has to be of the same shape as index.

      Parameters:
      index - select the entries of an NDArray
      value - numbers to assign to the indexed entries
      Returns:
      the NDArray with updated values
    • scatter

      NDArray scatter(NDArray index, NDArray value, int axis)
      Writes all values from the tensor value into self at the indices specified in the index tensor.
       This is the reverse operation of the manner described in gather().
      
       self[index[i][j][k]][j][k] = value[i][j][k] # if axis == 0
       self[i][index[i][j][k]][k] = value[i][j][k] # if axis == 1
       self[i][j][index[i][j][k]] = value[i][j][k] # if axis == 2
       
      torch.Tensor.scatter_
      Parameters:
      axis - the axis along which to index
      index - the indices of elements to scatter, can be either empty or of the same dimensionality as value. When empty, the operation returns self unchanged
      value - the source element(s) to scatter
      Returns:
      the NDArray with updated values
    • getScalar

      default NDArray getScalar(long... indices)
      Returns a scalar NDArray corresponding to a single element.
      Parameters:
      indices - the indices of the scalar to return. Must return only a single element
      Returns:
      a scalar NDArray corresponding to the element
      Throws:
      IllegalArgumentException - thrown if the result is not a single element
    • getLong

      default long getLong(long... indices)
      Returns a long element from this NDArray.
      Parameters:
      indices - the indices of the long element to return
      Returns:
      the element in the specified index as a long
      Throws:
      IllegalArgumentException - thrown if the result is not a single element
    • getDouble

      default double getDouble(long... indices)
      Returns a double element from this NDArray.
      Parameters:
      indices - the indices of the double element to return
      Returns:
      the element in the specified index as a double
      Throws:
      IllegalArgumentException - thrown if the result is not a single element
    • getFloat

      default float getFloat(long... indices)
      Returns a float element from this NDArray.
      Parameters:
      indices - the indices of the long element to return
      Returns:
      the element in the specified index as a float
      Throws:
      IllegalArgumentException - thrown if the result is not a single element
    • getInt

      default int getInt(long... indices)
      Returns an int element from this NDArray.
      Parameters:
      indices - the indices of the int element to return
      Returns:
      the element in the specified index as an integer
      Throws:
      IllegalArgumentException - thrown if the result is not a single element
    • getByte

      default byte getByte(long... indices)
      Returns an byte element from this NDArray.
      Parameters:
      indices - the indices of the byte element to return
      Returns:
      the element in the specified index as a byte
      Throws:
      IllegalArgumentException - thrown if the result is not a single element
    • getUint8

      default int getUint8(long... indices)
      Returns an integer element from this NDArray that represent unsigned integer with 8 bits.
      Parameters:
      indices - the indices of the unsigned 8 bits integer element to return
      Returns:
      the element in the specified index as a uint8
      Throws:
      IllegalArgumentException - thrown if the result is not a single element
    • getBoolean

      default boolean getBoolean(long... indices)
      Returns a boolean element from this NDArray.
      Parameters:
      indices - the indices of the int element to return
      Returns:
      the element in the specified index as a boolean
      Throws:
      IllegalArgumentException - thrown if the result is not a single element
    • copyTo

      default void copyTo(NDArray array)
      Deep-copies the current NDArray to the one passed in.
      Parameters:
      array - this NDArray prepared to be copied to
    • duplicate

      default NDArray duplicate()
      Returns a copy of this NDArray.
      Returns:
      a copy of this NDArray
    • booleanMask

      default NDArray booleanMask(NDArray index)
      Returns portion of this NDArray given the index boolean NDArray along first axis.

      Examples

       jshell> NDArray array = manager.create(new float[] {1f, 2f, 3f, 4f, 5f, 6f}, new Shape(3, 2));
       jshell> NDArray mask = manager.create(new boolean[] {true, false, true});
       jshell> array.booleanMask(mask);
       ND: (2, 2) cpu() float32
       [[1., 2.],
        [5., 6.],
       ]
       
      Parameters:
      index - boolean NDArray mask
      Returns:
      the result NDArray
    • booleanMask

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

      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].

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

      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].

      Parameters:
      sequenceLength - used to handle variable-length sequences
      Returns:
      the result NDArray
    • zerosLike

      default NDArray zerosLike()
      Returns an NDArray of zeros with the same Shape, DataType and SparseFormat as the input 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.zerosLike();
       ND: (2, 3) cpu() float32
       [[0., 0., 0.],
        [0., 0., 0.],
       ]
       
      Returns:
      a NDArray filled with zeros
    • onesLike

      default NDArray onesLike()
      Returns an NDArray of ones with the same Shape, DataType and SparseFormat as the input 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.onesLike();
       ND: (2, 3) cpu() float32
       [[1., 1., 1.],
        [1., 1., 1.],
       ]
       
      Returns:
      a NDArray filled with ones
    • like

      default NDArray like()
      Returns an uninitialized NDArray with the same Shape, DataType and SparseFormat as the input 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.like(); // uninitialized NDArray
       ND: (2, 3) cpu() float32
       [[ 9.80908925e-45,  0.00000000e+00,  0.00000000e+00],
        [ 0.00000000e+00,  7.61595174e-07,  2.80259693e-44],
       ]
       
      Returns:
      the result NDArray
    • contentEquals

      boolean contentEquals(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
       
      Parameters:
      number - the number to compare
      Returns:
      the boolean result
    • contentEquals

      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
       
      Parameters:
      other - the other NDArray to compare
      Returns:
      the boolean result
    • shapeEquals

      default boolean shapeEquals(NDArray other)
      Checks 2 NDArrays for equal shapes.

      Shapes are considered equal if:

      • Both NDArrays have equal rank, and
      • size(0)...size(rank()-1) are equal for both NDArrays

      Examples

       jshell> NDArray array1 = manager.ones(new Shape(1, 2, 3));
       jshell> NDArray array2 = manager.create(new Shape(1, 2, 3));
       jshell> array1.shapeEquals(array2); // return true instead of boolean NDArray
       true
       
      Parameters:
      other - the other NDArray
      Returns:
      true if the Shapes are the same
    • allClose

      default boolean allClose(NDArray other)
      Returns true if two NDArrays are element-wise equal within a tolerance.

      Examples

       jshell> NDArray array1 = manager.create(new double[] {1e10, 1e-7});
       jshell> NDArray array2 = manager.create(new double[] {1.00001e10, 1e-8});
       jshell> array1.allClose(array2); // return false instead of boolean NDArray
       false
       jshell> NDArray array1 = manager.create(new double[] {1e10, 1e-8});
       jshell> NDArray array2 = manager.create(new double[] {1.00001e10, 1e-9});
       jshell> array1.allClose(array2); // return true instead of boolean NDArray
       true
       
      Parameters:
      other - the NDArray to compare with
      Returns:
      the boolean result
    • allClose

      default boolean allClose(NDArray other, double rtol, double atol, boolean equalNan)
      Returns true if two NDArray are element-wise equal within a tolerance.

      Examples

       jshell> NDArray array1 = manager.create(new double[] {1e10, 1e-7});
       jshell> NDArray array2 = manager.create(new double[] {1.00001e10, 1e-8});
       jshell> array1.allClose(array2, 1e-05, 1e-08, false); // return false instead of boolean NDArray
       false
       jshell> NDArray array1 = manager.create(new double[] {1e10, 1e-8});
       jshell> NDArray array2 = manager.create(new double[] {1.00001e10, 1e-9});
       jshell> array1.allClose(array2, 1e-05, 1e-08, false); // return true instead of boolean NDArray
       true
       jshell> NDArray array1 = manager.create(new float[] {1f, Float.NaN});
       jshell> NDArray array2 = manager.create(new float[] {1f, Float.NaN});
       jshell> array1.allClose(array2, 1e-05, 1e-08, true); // return true instead of boolean NDArray
       true
       
      Parameters:
      other - the NDArray to compare with
      rtol - the relative tolerance parameter
      atol - the absolute tolerance parameter
      equalNan - whether to compare NaN’s as equal. If true, NaN’s in the NDArray will be considered equal to NaN’s in the other NDArray
      Returns:
      the boolean result
    • eq

      NDArray eq(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]
       
      Parameters:
      n - the number to compare
      Returns:
      the boolean NDArray for element-wise "Equals" comparison
    • eq

      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]
       
      Parameters:
      other - the NDArray to compare
      Returns:
      the boolean NDArray for element-wise "Equals" comparison
    • neq

      NDArray neq(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],
       ]
       
      Parameters:
      n - the number to compare
      Returns:
      the boolean NDArray for element-wise "Not equals" comparison
    • neq

      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],
       ]
       
      Parameters:
      other - the NDArray to compare
      Returns:
      the boolean NDArray for element-wise "Not equals" comparison
    • gt

      NDArray gt(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]
       
      Parameters:
      n - the number to compare
      Returns:
      the boolean NDArray for element-wise "Greater" comparison
    • gt

      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]
       
      Parameters:
      other - the NDArray to compare
      Returns:
      the boolean NDArray for element-wis "Greater Than" comparison
    • gte

      NDArray gte(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]
       
      Parameters:
      n - the number to compare
      Returns:
      the boolean NDArray for element-wise "Greater or equals" comparison
    • gte

      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]
       
      Parameters:
      other - the number to compare
      Returns:
      the boolean NDArray for "Greater or equals" comparison
    • lt

      NDArray lt(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]
       
      Parameters:
      n - the number to compare
      Returns:
      the boolean NDArray for element-wise "Less" comparison
    • lt

      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]
       
      Parameters:
      other - the NDArray to compare
      Returns:
      the boolean NDArray for element-wise "Less" comparison
    • lte

      NDArray lte(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]
       
      Parameters:
      n - the number to compare
      Returns:
      the boolean NDArray for element-wise "Less or equals" comparison
    • lte

      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]
       
      Parameters:
      other - the NDArray to compare
      Returns:
      the boolean NDArray for element-wise "Less or equals" comparison
    • add

      NDArray add(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.]
       
      Parameters:
      n - the number to add
      Returns:
      the result NDArray
    • add

      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.],
       ]
       
      Parameters:
      other - the other NDArrays to add
      Returns:
      the result NDArray
      Throws:
      IllegalArgumentException - others arrays must have at least one element
    • sub

      NDArray sub(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.]
       
      Parameters:
      n - the number to subtract from
      Returns:
      the result NDArray
    • sub

      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.],
       ]
       
      Parameters:
      other - the other NDArray to subtract from
      Returns:
      the result NDArray
    • mul

      NDArray mul(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.]
       
      Parameters:
      n - the number to multiply by
      Returns:
      the result NDArray
    • mul

      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.],
       ]
       
      Parameters:
      other - the other NDArrays to multiply by
      Returns:
      the result NDArray
      Throws:
      IllegalArgumentException - others arrays must have at least one element
    • div

      NDArray div(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.  ]
       
      Parameters:
      n - the number to divide by
      Returns:
      the result NDArray
    • div

      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],
       ]
       
      Parameters:
      other - the other NDArray to divide by
      Returns:
      the result NDArray
    • mod

      NDArray mod(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.]
       
      Parameters:
      n - the divisor number
      Returns:
      the result NDArray
    • mod

      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.]
       
      Parameters:
      other - the divisor NDArray
      Returns:
      the result NDArray
    • pow

      NDArray pow(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.]
       
      Parameters:
      n - the number to take the power with
      Returns:
      the result NDArray
    • pow

      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.],
       ]
       
      Parameters:
      other - the other NDArray to take the power with
      Returns:
      the result NDArray
    • xlogy

      NDArray xlogy(NDArray other)
      Computes this * log(other).
      Parameters:
      other - other the other NDArray
      Returns:
      the result NDArray
    • addi

      NDArray addi(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.]
       
      Parameters:
      n - the number to add
      Returns:
      the result NDArray
    • addi

      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.]
       
      Parameters:
      other - the other NDArrays to add
      Returns:
      the result NDArray
    • subi

      NDArray subi(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.]
       
      Parameters:
      n - the number to subtract
      Returns:
      the result NDArray
    • subi

      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.],
       ]
       
      Parameters:
      other - the other NDArray to subtract from
      Returns:
      the result NDArray
    • muli

      NDArray muli(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.]
       
      Parameters:
      n - the number to multiply by
      Returns:
      the result NDArray
    • muli

      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.],
       ]
       
      Parameters:
      other - the other NDArrays to multiply with
      Returns:
      the result NDArray
    • divi

      NDArray divi(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.  ]
       
      Parameters:
      n - the number to divide values by
      Returns:
      the array after applying division operation
    • divi

      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],
       ]
       
      Parameters:
      other - the other NDArray to divide by
      Returns:
      the result of the divide
    • modi

      NDArray modi(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.]
       
      Parameters:
      n - the divisor number
      Returns:
      the result NDArray
    • modi

      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.]
       
      Parameters:
      other - the divisor NDArray
      Returns:
      the result of the divide
    • powi

      NDArray powi(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.]
       
      Parameters:
      n - the number to raise the power to
      Returns:
      the result NDArray
    • powi

      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.],
       ]
       
      Parameters:
      other - the other NDArray to take the power with
      Returns:
      the result NDArray
    • sign

      NDArray sign()
      Returns the element-wise sign.
      Returns:
      the result NDArray
    • signi

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

      NDArray maximum(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.]
       
      Parameters:
      n - the number to be compared
      Returns:
      the maximum of this NDArray and a number element-wise
    • maximum

      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. ],
       ]
       
      Parameters:
      other - the NDArray to be compared
      Returns:
      the maximum of this NDArray and the other NDArray element-wise
    • minimum

      NDArray minimum(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.]
       
      Parameters:
      n - the number to be compared
      Returns:
      the minimum of this NDArray and a number element-wise
    • minimum

      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. ],
       ]
       
      Parameters:
      other - the NDArray to be compared
      Returns:
      the minimum of this NDArray and the other NDArray element-wise
    • neg

      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.]
       
      Returns:
      the result NDArray
    • negi

      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.]
       
      Returns:
      the result NDArray
    • abs

      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.]
       
      Returns:
      the result NDArray
    • square

      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.]
       
      Returns:
      the result NDArray
    • sqrt

      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., ]
       
      Returns:
      the result NDArray
    • cbrt

      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.]
       
      Returns:
      the result NDArray
    • floor

      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.]
       
      Returns:
      the result NDArray
    • ceil

      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.]
       
      Returns:
      the result NDArray
    • round

      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.]
       
      Returns:
      the result NDArray
    • trunc

      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.]
       
      Returns:
      the result NDArray
    • exp

      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]
       
      Returns:
      the result NDArray
    • gammaln

      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]
       
      Returns:
      the result NDArray
    • log

      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]
       
      Returns:
      the result NDArray
    • log10

      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]
       
      Returns:
      the result NDArray
    • log2

      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]
       
      Returns:
      the result NDArray
    • sin

      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.    ]
       
      Returns:
      the result NDArray
    • cos

      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],
       
      Returns:
      the result NDArray
    • tan

      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],
       
      Returns:
      the result NDArray
    • asin

      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.    ]
       
      Returns:
      the result NDArray
    • acos

      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]
       
      Returns:
      the result NDArray
    • atan

      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]
       
      Returns:
      the result NDArray
    • atan2

      NDArray atan2(NDArray other)
      Returns the element-wise arc-tangent of this/other choosing the quadrant correctly.

      Examples

       jshell> NDArray x = manager.create(new float[] {0f, 1f});
       jshell> NDArray y = manager.create(new float[] {0f, -6f});
       jshell> x.atan2(y);
       ND: (2) cpu() float64
       [0.    , 2.9764]
       
      Parameters:
      other - The other NDArray
      Returns:
      the result NDArray
    • sinh

      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]
       
      Returns:
      the result NDArray
    • cosh

      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 ]
       
      Returns:
      the result NDArray
    • tanh

      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]
       
      Returns:
      the result NDArray
    • asinh

      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]
       
      Returns:
      the result NDArray
    • acosh

      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]
       
      Returns:
      the result NDArray
    • atanh

      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]
       
      Returns:
      the result NDArray
    • toDegrees

      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.]
       
      Returns:
      the result NDArray
    • toRadians

      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 ]
       
      Returns:
      the result NDArray
    • max

      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
       
      Returns:
      the maximum of this NDArray
    • max

      default NDArray max(int[] axes)
      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}); // Maximum along the first axis
       ND: (2) cpu() float32
       [2., 3.]
       jshell> array.max(new int[]{1}); // Maximum along the second axis
       ND: (2) cpu() float32
       [1., 3.]
       
      Parameters:
      axes - the axes along which to operate
      Returns:
      the maximum of this NDArray with the specified axes removed from the Shape containing the max
      See Also:
    • max

      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.],
       ]
       
      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

      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
       
      Returns:
      the minimum of this NDArray
    • min

      default NDArray min(int[] axes)
      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}); // Minimum along the first axis
       ND: (2) cpu() float32
       [0., 1.]
       jshell> array.min(new int[]{1}); // Minimum along the second axis
       ND: (2) cpu() float32
       [0., 2.]
       
      Parameters:
      axes - the axes along which to operate
      Returns:
      the minimum of this NDArray with the specified axes removed from the Shape containing the min
      See Also:
    • min

      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.],
       ]
       
      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

      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.
       
      Returns:
      the sum of this NDArray
    • sum

      default NDArray sum(int[] axes)
      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});
       ND: (2) cpu() float32
       [0., 6.]
       jshell> array.sum(new int[] {1});
       ND: (2) cpu() float32
       [1., 5.]
       
      Parameters:
      axes - the axes along which to operate
      Returns:
      the sum of this NDArray with the specified axes removed from the Shape containing the sum
      See Also:
    • sum

      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.],
       ]
       
      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

      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 ...]
      Parameters:
      axis - the axis along which to operate
      Returns:
      the cumulative product of this
    • cumProd

      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 ...]
      Parameters:
      axis - the axis along which to operate
      dataType - the datatype of the output
      Returns:
      the cumulative product of this
    • prod

      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.
       
      Returns:
      the product of this NDArray
    • prod

      default NDArray prod(int[] axes)
      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});
       ND: (2) cpu() float32
       [3., 8.]
       jshell> array.prod(new int[] {1});
       ND: (2) cpu() float32
       [ 2., 12.]
       
      Parameters:
      axes - the axes along which to operate
      Returns:
      the product of this NDArray with the specified axes removed from the Shape containing the prod
      See Also:
    • prod

      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.],
       ]
       
      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

      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
       
      Returns:
      the average of this NDArray
    • mean

      default NDArray mean(int[] axes)
      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});
       ND: (2) cpu() float32
       [2., 3.]
       jshell> array.mean(new int[] {1});
       ND: (2) cpu() float32
       [1.5, 3.5]
       
      Parameters:
      axes - the axes along which to operate
      Returns:
      the average of this NDArray with the specified axes removed from the Shape containing the mean
      See Also:
    • mean

      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],
       ]
       
      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

      default NDArray normalize()
      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();
       ND: (2, 3) cpu() float32
       [[0.2673, 0.5345, 0.8018],
        [0.4558, 0.5698, 0.6838],
       ]
       
      Returns:
      the normalized NDArray
    • normalize

      default NDArray normalize(double exponent, long dim)
      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);
       ND: (2, 3) cpu() float32
       [[0.2673, 0.5345, 0.8018],
        [0.4558, 0.5698, 0.6838],
       ]
       
      Parameters:
      exponent - the exponent value in the norm formulation
      dim - the dimension to reduce
      Returns:
      the normalized NDArray
    • normalize

      NDArray normalize(double exponent, 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],
       ]
       
      Parameters:
      exponent - 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

      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.

      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

      default NDArray trace()
      Returns the sum along diagonals of this NDArray.

      If this NDArray is 2-D, the sum along its diagonal is returned. If the 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 NDArray is the same as that of a with axis1 and axis2 removed.

      Examples

       jshell> NDArray array = manager.eye(3);
       jshell> array;
       ND: (3, 3) cpu() float32
       [[1., 0., 0.],
        [0., 1., 0.],
        [0., 0., 1.],
       ]
       jshell> array.trace();
       ND: () cpu() float32
       3.
       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();
       ND: (2) cpu() float32
       [6., 8.]
       
      Returns:
      the sum along diagonals of this NDArray
    • trace

      default NDArray trace(int offset)
      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.eye(3);
       jshell> array;
       ND: (3, 3) cpu() float32
       [[1., 0., 0.],
        [0., 1., 0.],
        [0., 0., 1.],
       ]
       jshell> array.trace(1);
       ND: () cpu() float32
       0.
       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);
       ND: (2) cpu() float32
       [2., 3.]
       
      Parameters:
      offset - offset of the diagonal from the main diagonal. Can be both positive and negative.
      Returns:
      the sum along diagonals of this NDArray
    • trace

      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.]
       
      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

      default NDList split(long sections)
      Splits this NDArray into multiple subNDArrays given sections along first axis.

      Examples

       jshell> NDArray array = manager.arange(9f);
       jshell> array.split(3).forEach(System.out::println);
       ND: (3) cpu() float32
       [0., 1., 2.]
      
       ND: (3) cpu() float32
       [3., 4., 5.]
      
       ND: (3) cpu() float32
       [6., 7., 8.]
       
      Parameters:
      sections - this NDArray will be divided into N (sections) equal NDArray
      Returns:
      an NDList with size(axis) NDArrays with Shape this.shape.remove(axis)
      See Also:
    • split

      default NDList split(long[] indices)
      Splits this NDArray into multiple sub-NDArrays given indices along first axis.

      Examples

       jshell> NDArray array = manager.arange(8f);
       jshell> array.split(new int[] {3, 5, 6}).forEach(System.out::println);
       ND: (3) cpu() float32
       [0., 1., 2.]
      
       ND: (2) cpu() float32
       [3., 4.]
      
       ND: (1) cpu() float32
       [5.]
      
       ND: (2) cpu() float32
       [6., 7.]
       
      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-NDArray is returned correspondingly.
      Returns:
      an NDList with size(axis) NDArrays with Shape this.shape.remove(axis)
      See Also:
    • split

      default 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.],
       ]
       
      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)
      Throws:
      IllegalArgumentException - thrown if the numOutputs does not equally divide the given axis
    • split

      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.],
       ]
       
      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

      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.]
       
      Returns:
      a 1-D NDArray of equal size
    • flatten

      NDArray flatten(int startDim, int endDim)
      Flattens this NDArray into a partially flatten NDArray.

      Examples

       jshell> NDArray array = manager.create(new float[]{1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f}, new Shape(2, 2, 2));
       jshell> array.flatten(0, 1);
       ND: (4) cpu() float32
       [[1., 2], [3., 4.], [5., 6.], [7., 8.]]
       
      Parameters:
      startDim - the first dim to flatten, inclusive
      endDim - the last dim to flatten, inclusive
      Returns:
      a partially fallen NDArray
    • fft

      default NDArray fft(long length)
      Computes the one-dimensional discrete Fourier Transform.
      Parameters:
      length - Length of the transformed axis of the output.
      Returns:
      The truncated or zero-padded input, transformed along the axis indicated by axis, or the last one if axis is not specified.
    • fft

      NDArray fft(long length, long axis)
      Computes the one-dimensional discrete Fourier Transform.
      Parameters:
      length - Length of the transformed axis of the output.
      axis - Axis over which to compute the FFT.
      Returns:
      The truncated or zero-padded input, transformed along the axis indicated by axis, or the last one if axis is not specified.
    • ifft

      NDArray ifft(long length, long axis)
      Computes the one dimensional inverse discrete Fourier transform.
      Parameters:
      length - Length of the transformed axis of the output.
      axis - Axis over which to compute the IFFT.
      Returns:
      The truncated or zero-padded input, transformed along the axis indicated by axis, or the last one if axis is not specified.
    • ifft

      default NDArray ifft(long length)
      Computes the one dimensional inverse discrete Fourier transform.
      Parameters:
      length - Length of the transformed axis of the output.
      Returns:
      The truncated or zero-padded input, transformed along the axis indicated by axis, or the last one if axis is not specified.
    • rfft

      default NDArray rfft(long length)
      Computes the one dimensional Fourier transform of real-valued input.
      Parameters:
      length - Length of the transformed axis of the output.
      Returns:
      The truncated or zero-padded input, transformed along the axis indicated by axis, or the last one if axis is not specified.
    • rfft

      NDArray rfft(long length, long axis)
      Computes the one dimensional Fourier transform of real-valued input.
      Parameters:
      length - Length of the transformed axis of the output.
      axis - Axis over which to compute the FFT.
      Returns:
      The truncated or transformed along the axis indicated by axis, or the last one if axis is not specified.
    • irfft

      NDArray irfft(long length, long axis)
      Computes the one dimensional inverse Fourier transform of real-valued input.
      Parameters:
      length - Length of the transformed axis of the output.
      axis - Axis over which to compute the IRFFT.
      Returns:
      The truncated or transformed along the axis indicated by axis, or the last one if axis is not specified.
    • irfft

      default NDArray irfft(long length)
      Computes the one dimensional inverse Fourier transform of real-valued input.
      Parameters:
      length - Length of the transformed axis of the output.
      Returns:
      The truncated or transformed along the axis indicated by axis, or the last one if axis is not specified.
    • stft

      default NDArray stft(long nFft, long hopLength, boolean center, NDArray window, boolean returnComplex)
      Computes the Short Time Fourier Transform (STFT).
      Parameters:
      nFft - size of Fourier transform
      hopLength - the distance between neighboring sliding window frames. Default can be: floor(n_fft / 4)
      center - whether to pad input on both sides.
      window - Desired window to use. Recommend for HanningWindow
      returnComplex - whether to return a complex tensor, or a real tensor with an extra last dimension for the real and imaginary components.
      Returns:
      A NDArray containing the STFT result with shape described above
    • stft

      NDArray stft(long nFft, long hopLength, boolean center, NDArray window, boolean normalize, boolean returnComplex)
      Computes the Short Time Fourier Transform (STFT).
      Parameters:
      nFft - size of Fourier transform
      hopLength - the distance between neighboring sliding window frames. Default can be: floor(n_fft / 4)
      center - whether to pad input on both sides.
      window - Desired window to use. Recommend for HanningWindow
      normalize - controls whether to return the normalized STFT results
      returnComplex - whether to return a complex tensor, or a real tensor with an extra last dimension for the real and imaginary components.
      Returns:
      A NDArray containing the STFT result with shape described above
    • fft2

      NDArray fft2(long[] sizes, long[] axes)
      Computes the two-dimensional Discrete Fourier Transform.
      Parameters:
      sizes - Sizes of the transformed axes of the output. Will be zero-padded or trimmed to this size.
      axes - Axes over which to compute the 2D-FFT.
      Returns:
      The truncated or zero-padded input, transformed along the axes.
    • fft2

      default NDArray fft2(long[] sizes)
      Computes the two-dimensional Discrete Fourier Transform along the last 2 axes.
      Parameters:
      sizes - Sizes of the transformed axes of the output. Will be zero-padded or trimmed to this size.
      Returns:
      The truncated or zero-padded input, transformed along the last two axes
    • ifft2

      NDArray ifft2(long[] sizes, long[] axes)
      Computes the two-dimensional inverse Discrete Fourier Transform.
      Parameters:
      sizes - Sizes of the transformed axes of the output. Will be zero-padded or trimmed to this size.
      axes - Axes over which to compute the 2D-Inverse-FFT.
      Returns:
      The truncated or zero-padded input, transformed along the axes.
    • ifft2

      default NDArray ifft2(long[] sizes)
      Computes the two-dimensional inverse Discrete Fourier Transform along the last 2 axes.
      Parameters:
      sizes - Sizes of the transformed axes of the output. Will be zero-padded or trimmed to this size.
      Returns:
      The truncated or zero-padded input, transformed along the axes.
    • pad

      NDArray pad(Shape padding, double value)
      Pads this NDArray with the given Shape.

      Examples

       NDArray array = manager.zeros(3, 3, 4, 2);
       array.pad(new Shape(1, 1), 0); # pad last dim by 1 on each side
       array.getShape() => (3, 3, 4, 4)
       
      Parameters:
      padding - the padding shape, must be even
      Returns:
      a padded NDArray
      Throws:
      IllegalArgumentException - thrown if the given padding does not match the size of the current shape
    • reshape

      default NDArray reshape(long... newShape)
      Reshapes this NDArray to the given Shape.

      Examples

       jshell> NDArray array = manager.arange(6f);
       jshell> array;
       ND: (6) cpu() float32
       [0., 1., 2., 3., 4., 5.]
       jshell> array.reshape(2, 3);
       ND: (2, 3) cpu() float32
       [[0., 1., 2.],
        [3., 4., 5.],
       ]
       
      Parameters:
      newShape - the long array to reshape into. Must have equal size to the current shape
      Returns:
      a reshaped NDArray
      Throws:
      IllegalArgumentException - thrown if the given Shape does not match the size of the current shape
    • reshape

      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.],
       ]
       
      Parameters:
      shape - the Shape to reshape into. Must have equal size to the current shape
      Returns:
      a reshaped NDArray
      Throws:
      IllegalArgumentException - thrown if the given Shape does not match the size of the current shape
    • expandDims

      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.],
       ]
       
      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

      default NDArray squeeze()
      Removes all singleton dimensions from this NDArray Shape.

      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();
       ND: (3) cpu() float32
       [0., 1., 2.]
       
      Returns:
      a result NDArray of same size and data without singleton dimensions
    • squeeze

      default NDArray squeeze(int axis)
      Removes a singleton dimension at the given axis.

      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(0);
       ND: (3, 1) cpu() float32
       [[0.],
        [1.],
        [2.],
       ]
       jshell> array.squeeze(2);
       ND: (1, 3) cpu() float32
       [[0., 1., 2.],
       ]
       
      Parameters:
      axis - the axis at which to remove the singleton dimension
      Returns:
      a result NDArray of same size and data without the axis at part of the shape
      Throws:
      IllegalArgumentException - thrown if the given axis is not a singleton dimension
    • squeeze

      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.]
       
      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
      Throws:
      IllegalArgumentException - thrown if any of the given axes are not a singleton dimension
    • unique

      NDList unique(Integer dim, boolean sorted, boolean returnInverse, boolean returnCounts)
      Returns the unique elements of the input tensor.

      Examples

       jshell> NDArray array = manager.create(new float[] {3f, 1f, 2f, 3f, 1f, 2f, 1f, 3f, 2f}, new Shape(3, 3));
       jshell> array;
       ND: (3, 3) cpu() float32
       [[[3., 1., 2.],
         [3., 1., 2.],
         [1., 3., 2.],
        ],
       ]
       jshell> NDList output = array.unique(0, true, true, true);
       jshell> output.get(0);
       jshell> output.get(1);
       jshell> output.get(2);
      
       ND: (2, 3) cpu() float32
       [[1., 3., 2.],
        [3., 1., 2.],
       ]
      
       ND: (3) cpu() int64
       [ 1,  1,  0]
      
       ND: (2) cpu() int64
       [ 1,  2]
      
       
      Parameters:
      dim - the dimension to apply unique
      sorted - whether to sort the unique elements in ascending order before returning as output
      returnInverse - return the indices which, fed into the output unique array as indices, will recover the original array
      returnCounts - return the counts for each unique element
      Returns:
      An NDList containing: output (Tensor): the output list of unique elements or low-rank tensors. inverse_indices (Tensor): (optional) if return_inverse is True, there will be an additional returned tensor (same shape as input) representing the indices for where elements in the original input map to in the output; otherwise, this function will only return a single tensor. counts (Tensor): (optional) if return_counts is True, there will be an additional returned tensor (same shape as output or output.size(dim), if dim was specified) representing the number of occurrences for each unique value or tensor.
    • unique

      default NDList unique(boolean sorted, boolean returnInverse, boolean returnCounts)
      Returns the unique elements of the input tensor. The output is flattened.
      Parameters:
      sorted - whether to sort the unique elements in ascending order before returning as output
      returnInverse - return the indices which, fed into the output unique array as indices, will recover the original array
      returnCounts - return the counts for each unique element
      Returns:
      An NDList containing: output (Tensor): the output list of unique elements or low-rank tensors. inverse_indices (Tensor): (optional) if return_inverse is True, there will be an additional returned tensor (same shape as input) representing the indices for where elements in the original input map to in the output; otherwise, this function will only return a single tensor. counts (Tensor): (optional) if return_counts is True, there will be an additional returned tensor (same shape as output or output.size(dim), if dim was specified) representing the number of occurrences for each unique value or tensor.
    • unique

      default NDList unique()
      Returns the unique elements of the input tensor. The output is flattened.
      Returns:
      An NDList containing: output (Tensor): the output list of unique elements or low-rank tensors. inverse_indices (Tensor): (optional) if return_inverse is True, there will be an additional returned tensor (same shape as input) representing the indices for where elements in the original input map to in the output; otherwise, this function will only return a single tensor. counts (Tensor): (optional) if return_counts is True, there will be an additional returned tensor (same shape as output or output.size(dim), if dim was specified) representing the number of occurrences for each unique value or tensor.
    • stack

      default NDArray stack(NDArray array)
      Joins a NDArray along the first axis.

      Examples

       jshell> NDArray array1 = manager.create(new float[] {0f, 1f});
       jshell> NDArray array2 = manager.create(new float[] {2f, 3f});
       jshell> array1.stack(array2)
       ND: (2, 2) cpu() float32
       [[0., 1.],
        [2., 3.],
       ]
       
      Parameters:
      array - the input NDArray which must have the same Shapeas this NDArray
      Returns:
      the result NDArray. The stacked NDArray has one more dimension than the input NDArray.
    • stack

      default NDArray stack(NDArray array, int axis)
      Joins a NDArray along a new axis.

      Examples

       jshell> NDArray array1 = manager.create(new float[] {0f, 1f});
       jshell> NDArray array2 = manager.create(new float[] {2f, 3f});
       jshell> array1.stack(array2, 0);
       ND: (2, 2) cpu() float32
       [[0., 1.],
        [2., 3.],
       ]
       jshell> array1.stack(array2, 1);
       ND: (2, 2) cpu() float32
       [[0., 2.],
        [1., 3.],
       ]
       
      Parameters:
      array - the input NDArray which must have the same Shapeas this NDArray
      axis - the axis in the result NDArray along which the input NDArray are stacked
      Returns:
      the result NDArray. The stacked NDArray has one more dimension than the input NDArray.
    • concat

      default NDArray concat(NDArray array)
      Joins a NDArray along the first axis.

      Examples

       jshell> NDArray array1 = manager.create(new float[] {0f, 1f});
       jshell> NDArray array2 = manager.create(new float[] {2f, 3f});
       jshell> array1.concat(array2)
       ND: (4) cpu() float32
       [0., 1., 2., 3.]
       
      Parameters:
      array - a NDArray which have the same Shapeas this NDArray, except in the dimension corresponding to axis
      Returns:
      the concatenated NDArray
    • concat

      default NDArray concat(NDArray array, int axis)
      Joins a NDArray along an existing axis.

      Examples

       jshell> NDArray array1 = manager.create(new float[] {0f, 1f});
       jshell> NDArray array2 = manager.create(new float[] {2f, 3f});
       jshell> array1.concat(array2, 0);
       ND: (4) cpu() float32
       [0., 1., 2., 3.]
       
      Parameters:
      array - a NDArray which have the same Shapeas this NDArray, except in the dimension corresponding to axis
      axis - the axis along which this NDArray will be joined
      Returns:
      the concatenated NDArray
    • logicalAnd

      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]
       
      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

      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]
       
      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

      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]
       
      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

      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]
       
      Returns:
      the boolean NDArray
    • argSort

      default NDArray argSort()
      Returns the indices that would sort this NDArray.

      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[] {3f, 1f, 2f});
       jshell> array.argSort();
       ND: (3) cpu() int64
       [ 1,  2,  0]
      
       jshell> array = manager.create(new float[] {0f, 3f, 2f, 2f}, new Shape(2, 2));
       jshell> array.argSort();
       ND: (2, 2) cpu() int64
       [[ 0,  1],
        [ 0,  1],
       ]
       
      Returns:
      a NDArray of indices corresponding to elements in this NDArray on the axis, the output DataType is always DataType.INT64
      See Also:
    • argSort

      default NDArray argSort(int axis)
      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);
       ND: (2, 2) cpu() int64
       [[ 0,  1],
        [ 1,  0],
       ]
       jshell> array.argSort(1);
       ND: (2, 2) cpu() int64
       [[ 0,  1],
        [ 0,  1],
       ]
       
      Parameters:
      axis - the axis to sort along
      Returns:
      a NDArray of indices corresponding to elements in this NDArray on the axis, the output DataType is always DataType.INT64
      See Also:
    • argSort

      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],
       ]
       
      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

      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.],
       ]
       
      Returns:
      the sorted NDArray
    • sort

      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.],
       ]
       
      Parameters:
      axis - the axis to sort along
      Returns:
      the sorted NDArray
    • softmax

      NDArray softmax(int axis)
      Applies the softmax function along the given axis.
      Parameters:
      axis - the axis along which to apply
      Returns:
      the result NDArray
      See Also:
    • logSoftmax

      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.

      Parameters:
      axis - the axis along which to apply
      Returns:
      the result NDArray
    • cumSum

      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.]
       
      Returns:
      the cumulative sum of the elements in the flattened NDArray
    • cumSum

      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.],
       ]
       
      Parameters:
      axis - the axis along which the cumulative sum is computed
      Returns:
      the cumulative sum along the specified axis
    • intern

      void intern(NDArray replaced)
      Replace the handle of the NDArray with the other. The NDArray used for replacement will be killed.

      Please use with caution, this method will make the input argument unusable.

      Parameters:
      replaced - the handle provider that will be killed
    • isInfinite

      NDArray isInfinite()
      Returns the boolean NDArray with value true where this NDArray's entries are infinite, or false where they are not infinite.
      Returns:
      the boolean NDArray with value true if this NDArray's entries are infinite
    • inverse

      NDArray inverse()
      Computes the inverse of square NDArray if it exists.
      Returns:
      the inverse of square NDArray.
    • isNaN

      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]
       
      Returns:
      the boolean NDArray with value true if this NDArray's NDArray are NaN
    • tile

      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.]
       
      Parameters:
      repeats - the number of times to repeat for each dimension
      Returns:
      a NDArray that has been tiled
    • tile

      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.],
       ]
       
      Parameters:
      axis - the axis to repeat
      repeats - the number of times to repeat for each axis
      Returns:
      a NDArray that has been tiled
      Throws:
      IllegalArgumentException - thrown for invalid axis
    • tile

      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.],
       ]
       
      Parameters:
      repeats - the number of times to repeat along each axis
      Returns:
      a NDArray that has been tiled
    • tile

      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.]
       
      Parameters:
      desiredShape - the Shapethat should be converted to
      Returns:
      a NDArray that has been tiled
    • repeat

      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.]
       
      Parameters:
      repeats - the number of times to repeat for each axis
      Returns:
      an NDArray that has been repeated
    • repeat

      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., 3., 3.]]
       
      Parameters:
      axis - the axis to repeat
      repeats - the number of times to repeat for each axis
      Returns:
      an NDArray that has been repeated
      Throws:
      IllegalArgumentException - thrown for invalid axis
    • repeat

      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.]
       
      Parameters:
      repeats - the number of times to repeat along each axis
      Returns:
      a NDArray that has been repeated
    • repeat

      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.],
       ]
       
      Parameters:
      desiredShape - the Shape that should be converted to
      Returns:
      an NDArray that has been repeated
    • dot

      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.],
        ],
       ]
       
      Parameters:
      other - the other NDArray to perform dot product with
      Returns:
      the result NDArray
    • matMul

      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.
       
      Parameters:
      other - the other NDArray to perform matrix product with
      Returns:
      the result NDArray
    • batchMatMul

      NDArray batchMatMul(NDArray other)
      Batch product matrix of this NDArray and the other NDArray.
      Parameters:
      other - the other NDArray to perform matrix product with
      Returns:
      the result NDArray
    • clip

      NDArray clip(Number min, 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.]
       
      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
    • swapAxes

      default NDArray swapAxes(int axis1, int axis2)
      Interchanges two axes of this NDArray.

      Examples

       jshell> NDArray array = manager.create(new float[] {1f, 2f ,3f}, new Shape(1, 3));
       jshell> array;
       ND: (1, 3) cpu() float32
       [[1., 2., 3.],
       ]
       jshell> array.swapAxes(0, 1);
       ND: (3, 1) cpu() float32
       [[1.],
        [2.],
        [3.],
       ]
       
      Parameters:
      axis1 - the first axis
      axis2 - the second axis
      Returns:
      the swapped axes NDArray
    • flip

      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.

      Parameters:
      axes - the axes to flip on
      Returns:
      the newly flipped array
    • transpose

      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.],
       ]
       
      Returns:
      the newly permuted array
    • transpose

      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.],
        ],
       ]
       
      Parameters:
      axes - the axes to swap to
      Returns:
      the transposed NDArray
      Throws:
      IllegalArgumentException - thrown when passing a axis that is greater than the actual number of dimensions
    • broadcast

      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.],
        ],
       ]
       
      Parameters:
      shape - the new Shape of this NDArray
      Returns:
      the broadcasted NDArray
    • broadcast

      default NDArray broadcast(long... 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(2, 2, 2);
       ND: (2, 2, 2) cpu() float32
       [[[1., 2.],
         [3., 4.],
        ],
        [[1., 2.],
         [3., 4.],
        ],
       ]
       
      Parameters:
      shape - the new Shape of this NDArray
      Returns:
      the broadcasted NDArray
    • argMax

      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.
       
      Returns:
      a NDArray containing indices
    • argMax

      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]
       
      Parameters:
      axis - the axis along which to find maximum values
      Returns:
      a NDArray containing indices
    • topK

      default NDList topK(int k, int axis)
      Returns (values, indices) of the top k values along given axis.
      Parameters:
      k - the number of returned values
      axis - the axis to sort along, whose shape is reduced to k
      Returns:
      a NDList containing (values, indices)
    • topK

      NDList topK(int k, int axis, boolean largest, boolean sorted)
      Returns (values, indices) of the top k values along given axis.
      Parameters:
      k - the number of returned values
      axis - the axis to sort along, whose shape is reduced to k
      largest - whether the largest or the smallest
      sorted - whether the sorted or not
      Returns:
      a NDList containing (values, indices)
    • argMin

      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.
       
      Returns:
      a NDArray containing indices
    • argMin

      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]
       
      Parameters:
      axis - the axis along which to find minimum values
      Returns:
      a NDArray containing indices
    • percentile

      NDArray percentile(Number percentile)
      Returns percentile for this NDArray.
      Parameters:
      percentile - the target percentile in range of 0..100
      Returns:
      the result NDArray
    • percentile

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

      NDArray median()
      Returns median value for this NDArray.
      Returns:
      the median NDArray
    • median

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

      NDArray toDense()
      Returns a dense representation of the sparse NDArray.
      Returns:
      the result NDArray
    • toSparse

      NDArray toSparse(SparseFormat fmt)
      Returns a sparse representation of NDArray.
      Parameters:
      fmt - the SparseFormat of this NDArray
      Returns:
      the result NDArray
    • nonzero

      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],
       ]
       
      Returns:
      the indices of the elements that are non-zero
    • isEmpty

      default boolean isEmpty()
      Returns true if this NDArray is special case: no-value NDArray.

      Examples

       jshell> NDArray array = manager.create(new Shape(2, 0, 1));
       jshell> array;
       ND: (2, 0, 1) cpu() float32
       []
       jshell> array.isEmpty();
       true
       
      Returns:
      true if this NDArray is empty
    • all

      default NDArray all()
      Returns true if all elements within this NDArray are non-zero or true.

      Examples

       jshell> NDArray array = manager.create(new boolean[] {true, false, true, true}, new Shape(2, 2));
       jshell> array.all();
       ND: () cpu() boolean
       false
       jshell> NDArray array = manager.create(new float[] {-1f, 4f, 5f});
       jshell> array.all(); // all elements are non-zero
       ND: () cpu() boolean
       true
       
      Returns:
      true if all elements within this NDArray are non-zero or true
    • any

      default NDArray any()
      Returns true if any of the elements within this NDArray are non-zero or true.

      Examples

       jshell> NDArray array = manager.create(new boolean[] {true, false, true, true}, new Shape(2, 2));
       jshell> array.any();
       ND: () cpu() boolean
        true
       jshell> NDArray array = manager.create(new float[] {-1, 0, 5});
       jshell> array.any() // all elements are non-zero
       ND: () cpu() boolean
        true
       
      Returns:
      true if any of the elements within this NDArray are non-zero or true
    • none

      default NDArray none()
      Returns true if none of the elements within this NDArray are non-zero or true.

      Examples

       jshell> NDArray array = manager.create(new boolean[] {false, false});
       jshell> array.none();
       ND: () cpu() boolean
        true
       jshell> NDArray array = manager.create(new float[] {-1f, 0f, 5f});
       jshell> array.none() // all elements are non-zero
       ND: () cpu() boolean
       false
       
      Returns:
      true if none of the elements within this NDArray are non-zero or true
    • countNonzero

      default NDArray countNonzero()
      Counts the number of non-zero values in this NDArray.

      Examples

       jshell> NDArray array = manager.create(new float[] {0f, 0f, 1f, 2f, 7f, 0f}, new Shape(2, 3));
       jshell> array.countNonzero()
       ND: () cpu() int64
       3
       
      Returns:
      the number of non-zero values in this NDArray
    • countNonzero

      default NDArray countNonzero(int axis)
      Counts the number of non-zero values in this NDArray along a given axis.

      Examples

       jshell> NDArray array = manager.create(new float[] {0f, 0f, 1f, 2f, 7f, 0f}, new Shape(2, 3));
       jshell> array;
       ND: (2, 3) cpu() float32
       [[0., 0., 1.],
        [2., 7., 0.],
       ]
       jshell> array.countNonzero(0);
       ND: (3) cpu() int64
       [ 1,  1,  1]
       jshell> array.countNonzero(1);
       ND: (2) cpu() int64
       [ 1,  2]
       
      Parameters:
      axis - the axis to operate on
      Returns:
      the number of non-zero values in this NDArray along a given axis
    • erfinv

      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]
       
      Returns:
      The inverse of gauss error of the NDArray, element-wise
    • erf

      NDArray erf()
      Returns element-wise gauss error function of the NDArray.

      Examples

       jshell> NDArray array = manager.create(new float[] {0f, 0.4769f, Float.NEGATIVE_INFINITY});
       jshell> array.erf();
       ND: (3) cpu() float32
       [0., 0.5, -1]
       
      Returns:
      The gauss error of the NDArray, element-wise
    • getResourceNDArrays

      default List<NDArray> getResourceNDArrays()
      Returns the NDArray or NDArrays contained within this resource.
      Specified by:
      getResourceNDArrays in interface NDResource
      Returns:
      the NDArray or NDArrays contained within this resource
    • getNDArrayInternal

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

      This method should only be used by Engine provider

      Returns:
      an internal representative of Native NDArray
    • isReleased

      boolean isReleased()
      Returns true if this NDArray has been released.
      Returns:
      true if this NDArray has been released
    • toDebugString

      default String toDebugString()
      Runs the debug string representation of this NDArray.
      Returns:
      the debug string representation of this NDArray
    • toDebugString

      default String toDebugString(boolean withContent)
      Runs the debug string representation of this NDArray.
      Parameters:
      withContent - true to show the content of NDArray
      Returns:
      the debug string representation of this NDArray
    • toDebugString

      default String toDebugString(int maxSize, int maxDepth, int maxRows, int maxColumns, boolean withContent)
      Runs the debug string representation of this NDArray.
      Parameters:
      maxSize - the maximum elements to print out
      maxDepth - the maximum depth to print out
      maxRows - the maximum rows to print out
      maxColumns - the maximum columns to print out
      withContent - true to show the content of NDArray
      Returns:
      the debug string representation of this NDArray
    • close

      void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface NDResource
    • norm

      default NDArray norm()
      Returns the norm of this NDArray.

      Examples

       jshell> NDArray array = manager.create(new float[] {-3f, -4f});
       jshell> array.norm();
       ND: () cpu() float32
       5.
       jshell> NDArray array = manager.create(new float[] {1f, 2f, 3f, 4f}, new Shape(2, 2));
       jshell> array.norm();
       ND: () cpu() float32
       5.4472
       
      Returns:
      the norm of this NDArray
    • norm

      default NDArray norm(int[] axes)
      Returns the norm of this NDArray.

      Examples

       jshell> NDArray array = manager.create(new float[] {-3f, -4f});
       jshell> array.norm(new int[] {0});
       ND: () cpu() float32
       5.
       jshell> NDArray array = manager.create(new float[] {1f, 2f, 3f, 4f}, new Shape(2, 2));
       jshell> array.norm(new int[] {0});
       ND: (2) cpu() float32
       [3.1623, 4.4721]
       
      Parameters:
      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.
      Returns:
      the norm of this NDArray
    • norm

      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],
       ]
       
      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

      default NDArray norm(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(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(new int[] {0}, false);
       ND: (2) cpu() float32
       [3.1623, 4.4721]
       
      Parameters:
      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
    • norm

      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]
       
      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

      default NDArray oneHot(int depth)
      Returns a one-hot NDArray.
      • The locations represented by indices take value 1, while all other locations take value 0.
      • 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);
       ND: (4, 3) cpu() float32
       [[0., 1., 0.],
        [1., 0., 0.],
        [0., 0., 1.],
        [1., 0., 0.],
       ]
       jshell> NDArray array = manager.create(new int[][] {{1, 0}, {1, 0}, {2, 0}});
       jshell> array.oneHot(3);
       ND: (3, 2, 3) cpu() float32
       [[[0., 1., 0.],
         [1., 0., 0.],
        ],
        [[0., 1., 0.],
         [1., 0., 0.],
        ],
        [[0., 0., 1.],
         [1., 0., 0.],
        ],
       ]
       
      Parameters:
      depth - Depth of the one hot dimension.
      Returns:
      one-hot encoding of this NDArray
      See Also:
    • oneHot

      default NDArray oneHot(int depth, DataType dataType)
      Returns a one-hot NDArray.
      • The locations represented by indices take value 1, while all other locations take value 0.
      • 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);
       ND: (4, 3) cpu() float32
       [[0., 1., 0.],
        [1., 0., 0.],
        [0., 0., 1.],
        [1., 0., 0.],
       ]
       jshell> NDArray array = manager.create(new int[][] {{1, 0}, {1, 0}, {2, 0}});
       jshell> array.oneHot(3);
       ND: (3, 2, 3) cpu() float32
       [[[0., 1., 0.],
         [1., 0., 0.],
        ],
        [[0., 1., 0.],
         [1., 0., 0.],
        ],
        [[0., 0., 1.],
         [1., 0., 0.],
        ],
       ]
       
      Parameters:
      depth - Depth of the one hot dimension.
      dataType - dataType of the output.
      Returns:
      one-hot encoding of this NDArray
      See Also:
    • oneHot

      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],
       ]
       
      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:
    • batchDot

      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.],
        ],
       ]
       
      Parameters:
      other - the other NDArray to perform batch dot product with
      Returns:
      the result NDArray
    • complex

      NDArray complex()
      Convert a general NDArray to its complex math format.

      example: [10f, 12f] float32 -> [10+12j] in complex64

      Returns:
      the complex NDArray
    • real

      NDArray real()
      Convert a complex NDArray to its real math format. example: [10+12j] in complex64 -> [10f, 12f] float32
      Returns:
      tje real NDArray
    • conj

      NDArray conj()
      Conjugate complex array.
      Returns:
      Returns a view of input with a flipped conjugate bit. If input has a non-complex type, this function just returns input.