public interface NDArray
extends java.lang.AutoCloseable
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
Modifier and Type | Method and Description |
---|---|
NDArray |
abs()
Returns the absolute value of this
NDArray element-wise. |
NDArray |
acos()
Returns the inverse trigonometric cosine of this
NDArray element-wise. |
NDArray |
acosh()
Returns the inverse hyperbolic cosine of this
NDArray element-wise. |
NDArray |
add(NDArray other)
Adds other
NDArray s to this NDArray element-wise. |
NDArray |
add(java.lang.Number n)
Adds a number to this
NDArray element-wise. |
NDArray |
addi(NDArray other)
Adds other
NDArray s to this NDArray element-wise in place. |
NDArray |
addi(java.lang.Number n)
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 |
allClose(NDArray other)
Returns
true if two NDArray s 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 . |
NDArray |
argMax()
Returns the indices of the maximum values into the flattened
NDArray . |
NDArray |
argMax(int axis)
Returns the indices of the maximum values along given axis.
|
NDArray |
argMin()
Returns the indices of the minimum values into the flattened
NDArray . |
NDArray |
argMin(int axis)
Returns the indices of the minimum values along given axis.
|
default NDArray |
argSort()
Returns the indices that would sort this
NDArray . |
default NDArray |
argSort(int axis)
Returns the indices that would sort this
NDArray given the axis. |
NDArray |
argSort(int axis,
boolean ascending)
Returns the indices that would sort this
NDArray given the axis. |
NDArray |
asin()
Returns the inverse trigonometric sine of this
NDArray element-wise. |
NDArray |
asinh()
Returns the inverse hyperbolic sine of this
NDArray element-wise. |
NDArray |
atan()
Returns the inverse trigonometric tangent of this
NDArray element-wise. |
NDArray |
atanh()
Returns the inverse hyperbolic tangent of this
NDArray element-wise. |
NDManager |
attach(NDManager manager)
Attaches this
NDArray to the specified NDManager . |
void |
attachGradient()
Attaches a gradient
NDArray to this NDArray and marks it so GradientCollector.backward(NDArray) can compute the gradient with respect to
it. |
void |
attachGradient(SparseFormat sparseFormat)
Attaches a gradient
NDArray with the given SparseFormat to this NDArray and marks it so GradientCollector.backward(NDArray) can
compute the gradient with respect to it. |
default NDArray |
booleanMask(NDArray index)
Returns portion of this
NDArray given the index boolean NDArray along first
axis. |
NDArray |
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. |
NDArray |
broadcast(Shape shape)
Broadcasts this
NDArray to be the given shape. |
NDArray |
cbrt()
Returns the cube-root of this
NDArray element-wise. |
NDArray |
ceil()
Returns the ceiling of this
NDArray element-wise. |
NDArray |
clip(java.lang.Number min,
java.lang.Number max)
Clips (limit) the values in this
NDArray . |
void |
close() |
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. |
boolean |
contentEquals(NDArray other)
|
boolean |
contentEquals(java.lang.Number number)
Returns
true if all elements in this NDArray are equal to the Number . |
void |
copyTo(NDArray array)
Deep-copies the current
NDArray to the one passed in. |
NDArray |
cos()
Returns the trigonometric cosine of this
NDArray element-wise. |
NDArray |
cosh()
Returns the hyperbolic cosine of this
NDArray element-wise. |
default NDArray |
countNonzero()
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. |
NDArray |
cumSum()
Returns the cumulative sum of the elements in the flattened
NDArray . |
NDArray |
cumSum(int axis)
Return the cumulative sum of the elements along a given axis.
|
static NDArray |
decode(NDManager manager,
byte[] byteArray)
Decodes
NDArray from bytes. |
void |
detach()
Detaches the
NDArray from current NDManager 's lifecycle. |
NDArray |
div(NDArray other)
Divides this
NDArray by the other NDArray element-wise. |
NDArray |
div(java.lang.Number n)
Divides this
NDArray by a number element-wise. |
NDArray |
divi(NDArray other)
Divides this
NDArray by the other NDArray element-wise in place. |
NDArray |
divi(java.lang.Number n)
Divides this
NDArray by a number element-wise in place. |
NDArray |
dot(NDArray other)
Dot product of this
NDArray and the other NDArray . |
default NDArray |
duplicate()
Returns a copy of this
NDArray . |
default byte[] |
encode()
Encodes
NDArray to byte array. |
NDArray |
eq(NDArray other)
Returns the boolean
NDArray for element-wise "Equals" comparison. |
NDArray |
eq(java.lang.Number n)
Returns the boolean
NDArray for element-wise "Equals" comparison. |
NDArray |
erfinv()
Returns element-wise inverse gauss error function of the
NDArray . |
NDArray |
exp()
Returns the exponential value of this
NDArray element-wise. |
NDArray |
expandDims(int axis)
Expands the
Shape of a NDArray . |
NDArray |
flatten()
Flattens this
NDArray into a 1-D NDArray in row-major order. |
NDArray |
flip(int... axes)
Returns the reverse order of elements in an array along the given axis.
|
NDArray |
floor()
Returns the floor of this
NDArray element-wise. |
default NDArray |
get(long... indices)
Returns a partial
NDArray . |
default NDArray |
get(NDArray index)
Returns a partial
NDArray . |
default NDArray |
get(NDIndex index)
Returns a partial
NDArray . |
default NDArray |
get(java.lang.String indices,
java.lang.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 . |
DataType |
getDataType()
Returns the
DataType of this NDArray . |
Device |
getDevice()
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 . |
NDArray |
getGradient()
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 . |
NDManager |
getManager()
Returns the
NDManager used to create this NDArray . |
java.lang.String |
getName()
Returns the name of this
NDArray . |
ai.djl.ndarray.internal.NDArrayEx |
getNDArrayInternal()
Returns an internal representative of Native
NDArray . |
default NDArray |
getScalar(long... indices)
Returns a scalar
NDArray corresponding to a single element. |
Shape |
getShape()
Returns the
Shape of this NDArray . |
SparseFormat |
getSparseFormat()
Returns the
SparseFormat of this NDArray . |
java.lang.String |
getUid()
Returns unique identifier of this
NDArray . |
default int |
getUint8(long... indices)
Returns an integer element from this
NDArray that represent unsigned integer with 8
bits. |
NDArray |
gt(NDArray other)
Returns the boolean
NDArray for element-wise "Greater Than" comparison. |
NDArray |
gt(java.lang.Number n)
Returns the boolean
NDArray for element-wise "Greater" comparison. |
NDArray |
gte(NDArray other)
Returns the boolean
NDArray for element-wise "Greater or equals" comparison. |
NDArray |
gte(java.lang.Number n)
Returns the boolean
NDArray for element-wise "Greater or equals" comparison. |
boolean |
hasGradient()
Returns true if the gradient calculation is required for this
NDArray . |
default boolean |
isEmpty()
Returns
true if this NDArray is special case: no-value NDArray . |
NDArray |
isInfinite()
Returns the boolean
NDArray with value true where this NDArray 's
entries are infinite, or false where they are not infinite. |
NDArray |
isNaN()
Returns the boolean
NDArray with value true where this NDArray 's
entries are NaN, or false where they are not NaN. |
default boolean |
isScalar()
|
default boolean |
isSparse()
|
default NDArray |
like()
Returns an uninitialized
NDArray with the same Shape , DataType and
SparseFormat as the input NDArray . |
NDArray |
log()
Returns the natural logarithmic value of this
NDArray element-wise. |
NDArray |
log10()
Returns the base 10 logarithm of this
NDArray element-wise. |
NDArray |
log2()
Returns the base 2 logarithm of this
NDArray element-wise. |
NDArray |
logicalAnd(NDArray other)
Returns the truth value of this
NDArray AND the other NDArray element-wise. |
NDArray |
logicalNot()
Computes the truth value of NOT this
NDArray element-wise. |
NDArray |
logicalOr(NDArray other)
Computes the truth value of this
NDArray OR the other NDArray element-wise. |
NDArray |
logicalXor(NDArray other)
Computes the truth value of this
NDArray XOR the other NDArray element-wise. |
NDArray |
logSoftmax(int axis)
Applies the softmax function followed by a logarithm.
|
NDArray |
lt(NDArray other)
Returns the boolean
NDArray for element-wise "Less" comparison. |
NDArray |
lt(java.lang.Number n)
Returns the boolean
NDArray for element-wise "Less" comparison. |
NDArray |
lte(NDArray other)
Returns the boolean
NDArray for element-wise "Less or equals" comparison. |
NDArray |
lte(java.lang.Number n)
Returns the boolean
NDArray for element-wise "Less or equals" comparison. |
NDArray |
matMul(NDArray other)
Product matrix of this
NDArray and the other NDArray . |
NDArray |
max()
Returns the maximum of this
NDArray . |
default NDArray |
max(int[] axes)
Returns the maximum of this
NDArray along given axes. |
NDArray |
max(int[] axes,
boolean keepDims)
Returns the maximum of this
NDArray along given axes. |
NDArray |
maximum(NDArray other)
Returns the maximum of this
NDArray and the other NDArray element-wise. |
NDArray |
maximum(java.lang.Number n)
Returns the maximum of this
NDArray and a number element-wise. |
NDArray |
mean()
Returns the average of this
NDArray . |
default NDArray |
mean(int[] axes)
Returns the average of this
NDArray along given axes. |
NDArray |
mean(int[] axes,
boolean keepDims)
Returns the average of this
NDArray along given axes. |
NDArray |
median()
Returns median value for this
NDArray . |
NDArray |
median(int[] axes)
Returns median value along given axes.
|
NDArray |
min()
Returns the minimum of this
NDArray . |
default NDArray |
min(int[] axes)
Returns the minimum of this
NDArray along given axes. |
NDArray |
min(int[] axes,
boolean keepDims)
Returns the minimum of this
NDArray along given axes. |
NDArray |
minimum(NDArray other)
Returns the maximum of this
NDArray and the other NDArray element-wise. |
NDArray |
minimum(java.lang.Number n)
Returns the minimum of this
NDArray and a number element-wise. |
NDArray |
mod(NDArray other)
Returns element-wise remainder of division.
|
NDArray |
mod(java.lang.Number n)
Returns element-wise remainder of division.
|
NDArray |
modi(NDArray other)
Returns in place element-wise remainder of division in place.
|
NDArray |
modi(java.lang.Number n)
Returns element-wise remainder of division in place.
|
NDArray |
mul(NDArray other)
Multiplies this
NDArray by other NDArray s element-wise. |
NDArray |
mul(java.lang.Number n)
Multiplies this
NDArray by a number element-wise. |
NDArray |
muli(NDArray other)
Multiplies this
NDArray by other NDArray element-wise in place. |
NDArray |
muli(java.lang.Number n)
Multiplies this
NDArray by a number element-wise in place. |
NDArray |
neg()
Returns the numerical negative
NDArray element-wise. |
NDArray |
negi()
Returns the numerical negative
NDArray element-wise in place. |
NDArray |
neq(NDArray other)
Returns the boolean
NDArray for element-wise "Not equals" comparison. |
NDArray |
neq(java.lang.Number n)
Returns the boolean
NDArray for element-wise "Not equals" comparison. |
default NDArray |
none()
Returns
true if none of the elements within this NDArray are non-zero or
true . |
NDArray |
nonzero()
Returns the indices of elements that are non-zero.
|
NDArray |
onesLike()
|
NDArray |
percentile(java.lang.Number percentile)
Returns percentile for this
NDArray . |
NDArray |
percentile(java.lang.Number percentile,
int[] axes)
Returns median along given dimension(s).
|
NDArray |
pow(NDArray other)
Takes the power of this
NDArray with the other NDArray element-wise. |
NDArray |
pow(java.lang.Number n)
Takes the power of this
NDArray with a number element-wise. |
NDArray |
powi(NDArray other)
Takes the power of this
NDArray with the other NDArray element-wise in place. |
NDArray |
powi(java.lang.Number n)
Takes the power of this
NDArray with a number element-wise in place. |
NDArray |
prod()
Returns the product of this
NDArray . |
default NDArray |
prod(int[] axes)
Returns the product of this
NDArray elements over the given axes. |
NDArray |
prod(int[] axes,
boolean keepDims)
Returns the product of this
NDArray elements over the given axes. |
NDArray |
repeat(int axis,
long repeats)
Repeats element of this
NDArray the number of times given repeats along given axis. |
NDArray |
repeat(long repeats)
Repeats element of this
NDArray the number of times given repeats. |
NDArray |
repeat(long[] repeats)
Repeats element of this
NDArray the number of times given repeats along each axis. |
NDArray |
repeat(Shape desiredShape)
Repeats element of this
NDArray to match the desired shape. |
default NDArray |
reshape(long... newShape)
Reshapes this
NDArray to the given Shape . |
NDArray |
reshape(Shape shape)
Reshapes this
NDArray to the given Shape . |
NDArray |
round()
Returns the round of this
NDArray element-wise. |
NDArray |
sequenceMask(NDArray sequenceLength)
Sets all elements outside the sequence to 0.
|
NDArray |
sequenceMask(NDArray sequenceLength,
float value)
Sets all elements outside the sequence to a constant value.
|
void |
set(java.nio.Buffer data)
Sets this
NDArray value from Buffer . |
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,
java.util.function.Function<NDArray,NDArray> function)
Sets the specific index by a function.
|
default void |
set(NDIndex index,
NDArray value)
Sets the specified index in this
NDArray with the given values. |
default void |
set(NDIndex index,
java.lang.Number value)
Sets the specified index in this
NDArray with the given value. |
void |
setName(java.lang.String name)
Sets name of this
NDArray . |
default void |
setScalar(NDIndex index,
java.lang.Number value)
Sets the specified scalar in this
NDArray with the given value. |
default boolean |
shapeEquals(NDArray other)
Checks 2
NDArray s for equal shapes. |
NDArray |
sign()
Returns the element-wise sign.
|
NDArray |
signi()
Returns the element-wise sign in-place.
|
NDArray |
sin()
Returns the trigonometric sine of this
NDArray element-wise. |
NDArray |
sinh()
Returns the hyperbolic sine of this
NDArray element-wise. |
default long |
size()
Returns the total number of elements in this
NDArray . |
default long |
size(int axis)
Returns the size of this
NDArray along a given axis. |
NDArray |
softmax(int axis)
Applies the softmax function along the given axis.
|
NDArray |
sort()
Sorts the flattened
NDArray . |
NDArray |
sort(int axis)
Sorts the flattened
NDArray . |
default NDList |
split(long sections)
Splits this
NDArray into multiple subNDArray s given sections along first
axis. |
default NDList |
split(long[] indices)
Splits this
NDArray into multiple sub-NDArray s given indices along first
axis. |
NDList |
split(long[] indices,
int axis)
Splits this
NDArray into multiple sub-NDArray s given indices along given
axis. |
default NDList |
split(long sections,
int axis)
Splits this
NDArray into multiple subNDArray s given sections along the given
axis. |
NDArray |
sqrt()
Returns the square root of this
NDArray element-wise. |
NDArray |
square()
Returns the square of this
NDArray element-wise. |
default NDArray |
squeeze()
Removes all singleton dimensions from this
NDArray Shape . |
default NDArray |
squeeze(int axis)
Removes a singleton dimension at the given axis.
|
NDArray |
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. |
NDArray |
sub(NDArray other)
Subtracts the other
NDArray from this NDArray element-wise. |
NDArray |
sub(java.lang.Number n)
Subtracts a number from this
NDArray element-wise. |
NDArray |
subi(NDArray other)
Subtracts the other
NDArray from this NDArray element-wise in place. |
NDArray |
subi(java.lang.Number n)
Subtracts a number from this
NDArray element-wise in place. |
NDArray |
sum()
Returns the sum of this
NDArray . |
default NDArray |
sum(int[] axes)
Returns the minimum of this
NDArray along given axes. |
NDArray |
sum(int[] axes,
boolean keepDims)
Returns the minimum of this
NDArray along given axes. |
default NDArray |
swapAxes(int axis1,
int axis2)
Interchanges two axes of this
NDArray . |
NDArray |
tan()
Returns the trigonometric tangent of this
NDArray element-wise. |
NDArray |
tanh()
Returns the hyperbolic tangent of this
NDArray element-wise. |
NDArray |
tile(int axis,
long repeats)
Constructs a
NDArray by repeating this NDArray the number of times given by
repeats along given axis. |
NDArray |
tile(long repeats)
Constructs a
NDArray by repeating this NDArray the number of times given
repeats. |
NDArray |
tile(long[] repeats)
Constructs a
NDArray by repeating this NDArray the number of times given by
repeats. |
NDArray |
tile(Shape desiredShape)
Constructs a
NDArray by repeating this NDArray the number of times to match
the desired shape. |
default java.lang.Number[] |
toArray()
Converts this
NDArray to a Number array based on its DataType . |
default boolean[] |
toBooleanArray()
Converts this
NDArray to a boolean array. |
default byte[] |
toByteArray()
Converts this
NDArray to a byte array. |
java.nio.ByteBuffer |
toByteBuffer()
Converts this
NDArray to a ByteBuffer. |
default java.lang.String |
toDebugString(int maxSize,
int maxDepth,
int maxRows,
int maxColumns)
Runs the debug string representation of this
NDArray . |
NDArray |
toDegrees()
Converts this
NDArray from radians to degrees element-wise. |
NDArray |
toDense()
Returns a dense representation of the sparse
NDArray . |
NDArray |
toDevice(Device device,
boolean copy)
Moves this
NDArray to a different Device . |
default double[] |
toDoubleArray()
Converts this
NDArray to a double array. |
default float[] |
toFloatArray()
Converts this
NDArray to a float array. |
default int[] |
toIntArray()
Converts this
NDArray to an int array. |
default long[] |
toLongArray()
Converts this
NDArray to a long array. |
NDArray |
toRadians()
Converts this
NDArray from degrees to radians element-wise. |
NDArray |
toSparse(SparseFormat fmt)
Returns a sparse representation of
NDArray . |
NDArray |
toType(DataType dataType,
boolean copy)
Converts this
NDArray to a different DataType . |
default int[] |
toUint8Array()
Converts this
NDArray to a uint8 array. |
default NDArray |
trace()
Returns the sum along diagonals of this
NDArray . |
default NDArray |
trace(int offset)
Returns the sum along diagonals of this
NDArray . |
NDArray |
trace(int offset,
int axis1,
int axis2)
Returns the sum along diagonals of this
NDArray . |
NDArray |
transpose()
Returns this
NDArray with axes transposed. |
NDArray |
transpose(int... axes)
Returns this
NDArray with given axes transposed. |
NDArray |
trunc()
Returns the truncated value of this
NDArray element-wise. |
NDArray |
zerosLike()
|
static NDArray decode(NDManager manager, byte[] byteArray)
NDArray
from bytes.manager
- NDManager
used to create this NDArray
byteArray
- data used to decodeNDArray
NDManager getManager()
NDManager
used to create this NDArray
.NDManager
used to create this NDArray
java.lang.String getName()
NDArray
.NDArray
void setName(java.lang.String name)
NDArray
.name
- the name of this NDArray
java.lang.String getUid()
NDArray
.NDArray
DataType getDataType()
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
.
DataType
of this NDArray
Device getDevice()
Device
of this NDArray
.
Device
class contains the information where this NDArray
stored in memory,
like CPU/GPU.
Device
of this NDArray
Shape getShape()
Shape
of this NDArray
.
Shape
defines how this NDArray
is represented multi-dimensionally.
Shape
of this NDArray
SparseFormat getSparseFormat()
SparseFormat
of this NDArray
.SparseFormat
of this NDArray
default boolean isSparse()
true
if this NDArray
is a SparseNDArray
default boolean isScalar()
true
if this NDArray
is a scalar NDArray
with empty Shape
default byte[] encode()
NDArray
to byte array.void detach()
NDArray
from current NDManager
's lifecycle.
The NDArray
becomes un-managed, it is the user's responsibility to close the
NDArray
. Failure to close the resource might cause your machine to run out of native
memory.
NDManager
NDArray toType(DataType dataType, boolean copy)
NDArray
to a different DataType
.void attachGradient()
NDArray
to this NDArray
and marks it so GradientCollector.backward(NDArray)
can compute the gradient with respect to
it.void attachGradient(SparseFormat sparseFormat)
NDArray
with the given SparseFormat
to this NDArray
and marks it so GradientCollector.backward(NDArray)
can
compute the gradient with respect to it.sparseFormat
- the SparseFormat
for the gradientNDArray getGradient()
NDArray
attached to this NDArray
.NDArray
java.lang.NullPointerException
- when gradient is not initializedboolean hasGradient()
NDArray
.NDArray
else falsedefault long size(int axis)
NDArray
along a given axis.axis
- the axis to return the size forNDArray
along a given axisdefault long size()
NDArray
.NDArray
default double[] toDoubleArray()
NDArray
to a double array.java.lang.IllegalStateException
- when DataType
of this NDArray
mismatchesdefault float[] toFloatArray()
NDArray
to a float array.java.lang.IllegalStateException
- when DataType
of this NDArray
mismatchesdefault int[] toIntArray()
NDArray
to an int array.java.lang.IllegalStateException
- when DataType
of this NDArray
mismatchesdefault long[] toLongArray()
NDArray
to a long array.java.lang.IllegalStateException
- when DataType
of this NDArray
mismatchesdefault byte[] toByteArray()
NDArray
to a byte array.java.lang.IllegalStateException
- when DataType
of this NDArray
mismatchesdefault int[] toUint8Array()
NDArray
to a uint8 array.java.lang.IllegalStateException
- when DataType
of this NDArray
mismatchesdefault boolean[] toBooleanArray()
NDArray
to a boolean array.java.lang.IllegalStateException
- when DataType
of this NDArray
mismatchesdefault java.lang.Number[] toArray()
NDArray
to a Number array based on its DataType
.java.nio.ByteBuffer toByteBuffer()
NDArray
to a ByteBuffer.void set(java.nio.Buffer data)
NDArray
value from Buffer
.data
- the input buffered datadefault void set(float[] data)
NDArray
value from an array of floats.data
- the array of floats to setdefault void set(int[] data)
NDArray
value from an array of ints.data
- the array of integers to setdefault void set(double[] data)
NDArray
value from an array of doubles.data
- the array of doubles to setdefault void set(long[] data)
NDArray
value from an array of longs.data
- the array of longs to setdefault void set(byte[] data)
NDArray
value from an array of bytes.data
- the array of bytes to setdefault void set(NDIndex index, NDArray value)
NDArray
with the given values.index
- the locations to updatevalue
- the value to replace with. Can broadcast if given smaller dimensions than the
indexdefault void set(NDIndex index, java.lang.Number value)
NDArray
with the given value.index
- the locations to updatevalue
- the value to replace withdefault void set(NDIndex index, java.util.function.Function<NDArray,NDArray> function)
index
- the locations to updatefunction
- the function to change the valuedefault void setScalar(NDIndex index, java.lang.Number value)
NDArray
with the given value.index
- the single index to updatevalue
- the value to replace withjava.lang.IllegalArgumentException
- thrown if the index does not correspond to a single elementdefault NDArray get(NDIndex index)
NDArray
.index
- the section of this NDArray
to returnNDArray
default NDArray get(java.lang.String indices, java.lang.Object... args)
NDArray
.indices
- the indices used to indicate what to getargs
- arguments to replace the varaible "{}" in the indices string. Can be an integer,
long, boolean NDArray
, or integer NDArray
.NDArray
NDIndex(String, Object...)
default NDArray get(long... indices)
NDArray
.indices
- the indices with each index corresponding to the dimensions and negative
indices starting from the endNDArray
default NDArray get(NDArray index)
NDArray
.index
- the boolean NDArray
that indicates what to getNDArray
default NDArray getScalar(long... indices)
NDArray
corresponding to a single element.indices
- the indices of the scalar to return. Must return only a single elementNDArray
corresponding to the elementjava.lang.IllegalArgumentException
- thrown if the result is not a single elementdefault long getLong(long... indices)
NDArray
.indices
- the indices of the long element to returnjava.lang.IllegalArgumentException
- thrown if the result is not a single elementdefault double getDouble(long... indices)
NDArray
.indices
- the indices of the double element to returnjava.lang.IllegalArgumentException
- thrown if the result is not a single elementdefault float getFloat(long... indices)
NDArray
.indices
- the indices of the long element to returnjava.lang.IllegalArgumentException
- thrown if the result is not a single elementdefault int getInt(long... indices)
NDArray
.indices
- the indices of the int element to returnjava.lang.IllegalArgumentException
- thrown if the result is not a single elementdefault byte getByte(long... indices)
NDArray
.indices
- the indices of the byte element to returnjava.lang.IllegalArgumentException
- thrown if the result is not a single elementdefault int getUint8(long... indices)
NDArray
that represent unsigned integer with 8
bits.indices
- the indices of the unsigned 8 bits integer element to returnjava.lang.IllegalArgumentException
- thrown if the result is not a single elementdefault boolean getBoolean(long... indices)
NDArray
.indices
- the indices of the int element to returnjava.lang.IllegalArgumentException
- thrown if the result is not a single elementvoid copyTo(NDArray array)
NDArray
to the one passed in.array
- this NDArray
prepared to be copied todefault NDArray duplicate()
NDArray
.NDArray
default NDArray booleanMask(NDArray index)
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.], ]
index
- boolean NDArray
maskNDArray
NDArray booleanMask(NDArray index, int axis)
NDArray
given the index boolean NDArray
along given
axis.index
- boolean NDArray
maskaxis
- an integer that represents the axis of NDArray
to mask fromNDArray
NDArray sequenceMask(NDArray sequenceLength, float 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].
sequenceLength
- used to handle variable-length sequencesvalue
- the constant value to be setNDArray
NDArray sequenceMask(NDArray sequenceLength)
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].
sequenceLength
- used to handle variable-length sequencesNDArray
NDArray zerosLike()
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.], ]
NDArray
filled with zerosNDArray onesLike()
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.], ]
NDArray
filled with onesdefault NDArray like()
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], ]
NDArray
boolean contentEquals(java.lang.Number number)
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
number
- the number to compareboolean contentEquals(NDArray other)
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
other
- the other NDArray
to comparedefault boolean shapeEquals(NDArray other)
NDArray
s for equal shapes.
Shapes are considered equal if:
NDArray
s have equal rank, and
NDArray
s
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
other
- the other NDArray
true
if the Shape
s are the samedefault boolean allClose(NDArray other)
true
if two NDArray
s 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
other
- the NDArray
to compare withdefault boolean allClose(NDArray other, double rtol, double atol, boolean equalNan)
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
other
- the NDArray
to compare withrtol
- the relative tolerance parameteratol
- the absolute tolerance parameterequalNan
- 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
NDArray eq(java.lang.Number n)
NDArray
for element-wise "Equals" comparison.
Examples
jshell> NDArray array = manager.ones(new Shape(1)); jshell> array.eq(1); ND: (1) cpu() boolean [ true]
n
- the number to compareNDArray
for element-wise "Equals" comparisonNDArray eq(NDArray other)
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]
other
- the NDArray
to compareNDArray
for element-wise "Equals" comparisonNDArray neq(java.lang.Number n)
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], ]
n
- the number to compareNDArray
for element-wise "Not equals" comparisonNDArray neq(NDArray other)
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], ]
other
- the NDArray
to compareNDArray
for element-wise "Not equals" comparisonNDArray gt(java.lang.Number n)
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]
n
- the number to compareNDArray
for element-wise "Greater" comparisonNDArray gt(NDArray other)
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]
other
- the NDArray
to compareNDArray
for element-wis "Greater Than" comparisonNDArray gte(java.lang.Number n)
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]
n
- the number to compareNDArray
for element-wise "Greater or equals" comparisonNDArray gte(NDArray other)
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]
other
- the number to compareNDArray
for "Greater or equals" comparisonNDArray lt(java.lang.Number n)
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]
n
- the number to compareNDArray
for element-wise "Less" comparisonNDArray lt(NDArray other)
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]
other
- the NDArray
to compareNDArray
for element-wise "Less" comparisonNDArray lte(java.lang.Number n)
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]
n
- the number to compareNDArray
for element-wise "Less or equals" comparisonNDArray lte(NDArray other)
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]
other
- the NDArray
to compareNDArray
for element-wise "Less or equals" comparisonNDArray add(java.lang.Number n)
NDArray
element-wise.
Examples
jshell> NDArray array = manager.create(new float[] {1f, 2f}); jshell> array.add(2f); ND: (2) cpu() float32 [3., 4.]
n
- the number to addNDArray
NDArray add(NDArray other)
NDArray
s 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.], ]
other
- the other NDArray
s to addNDArray
java.lang.IllegalArgumentException
- others arrays must have at least one elementNDArray sub(java.lang.Number n)
NDArray
element-wise.
Examples
jshell> NDArray array = manager.create(new float[] {1f, 2f}); jshell> array.sub(2f); ND: (2) cpu() float32 [-1., 0.]
n
- the number to subtract fromNDArray
NDArray sub(NDArray other)
NDArray
from this NDArray
element-wise.
The shapes of this NDArray
and other NDArray
s 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.], ]
other
- the other NDArray
to subtract fromNDArray
NDArray mul(java.lang.Number n)
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.]
n
- the number to multiply byNDArray
NDArray mul(NDArray other)
NDArray
by other NDArray
s 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.], ]
other
- the other NDArray
s to multiply byNDArray
java.lang.IllegalArgumentException
- others arrays must have at least one elementNDArray div(java.lang.Number n)
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. ]
n
- the number to divide byNDArray
NDArray div(NDArray other)
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], ]
other
- the other NDArray
to divide byNDArray
NDArray mod(java.lang.Number n)
Examples
jshell> NDArray array = manager.arange(7f); jshell> array.mod(5f); ND: (7) cpu() float32 [0., 1., 2., 3., 4., 0., 1.]
n
- the divisor numberNDArray
NDArray mod(NDArray other)
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.]
other
- the divisor NDArray
NDArray
NDArray pow(java.lang.Number n)
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.]
n
- the number to take the power withNDArray
NDArray pow(NDArray other)
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.], ]
other
- the other NDArray
to take the power withNDArray
NDArray addi(java.lang.Number n)
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.]
n
- the number to addNDArray
NDArray addi(NDArray other)
NDArray
s to this NDArray
element-wise in place.
The shapes of this NDArray
and other NDArray
s 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.]
other
- the other NDArray
s to addNDArray
NDArray subi(java.lang.Number n)
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.]
n
- the number to subtractNDArray
NDArray subi(NDArray other)
NDArray
from this NDArray
element-wise in place.
The shapes of this NDArray
and other NDArray
s 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.], ]
other
- the other NDArray
to subtract fromNDArray
NDArray muli(java.lang.Number n)
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.]
n
- the number to multiply byNDArray
NDArray muli(NDArray other)
NDArray
by other NDArray
element-wise in place.
The shapes of this NDArray
and other NDArray
s 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.], ]
other
- the other NDArrays to multiply withNDArray
NDArray divi(java.lang.Number n)
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. ]
n
- the number to divide values byNDArray divi(NDArray other)
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], ]
other
- the other NDArray
to divide byNDArray modi(java.lang.Number n)
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.]
n
- the divisor numberNDArray
NDArray modi(NDArray other)
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.]
other
- the divisor NDArray
NDArray powi(java.lang.Number n)
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.]
n
- the number to raise the power toNDArray
NDArray powi(NDArray other)
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.], ]
other
- the other NDArray
to take the power withNDArray
NDArray sign()
NDArray
NDArray signi()
NDArray
NDArray maximum(java.lang.Number n)
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.]
n
- the number to be comparedNDArray
and a number element-wiseNDArray maximum(NDArray other)
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. ], ]
other
- the NDArray
to be comparedNDArray
and the other NDArray
element-wiseNDArray minimum(java.lang.Number n)
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.]
n
- the number to be comparedNDArray
and a number element-wiseNDArray minimum(NDArray other)
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. ], ]
other
- the NDArray
to be comparedNDArray
and the other NDArray
element-wiseNDArray neg()
NDArray
element-wise.
jshell> NDArray array = manager.arange(5f); jshell> array.neg(); ND: (5) cpu() float32 [-0., -1., -2., -3., -4.]
NDArray
NDArray negi()
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.]
NDArray
NDArray abs()
NDArray
element-wise.
Examples
jshell> NDArray array = manager.create(new float[] {-1f, -2f}); jshell> array.abs(); ND: (2) cpu() float32 [1., 2.]
NDArray
NDArray square()
NDArray
element-wise.
Examples
jshell> NDArray array = manager.create(new float[] {2f, -3f}); jshell> array.square(); ND: (2) cpu() float32 [4., 9.]
NDArray
NDArray sqrt()
NDArray
element-wise.
Examples
jshell> NDArray array = manager.create(new float[] {4f}); jshell> array.sqrt(); ND: (1) cpu() float32 [2., ]
NDArray
NDArray cbrt()
NDArray
element-wise.
Examples
jshell> NDArray array = manager.create(new float[] {1f, 8f, 27f}); jshell> array.cbrt(); ND: (3) cpu() float32 [1., 2., 3.]
NDArray
NDArray floor()
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.]
NDArray
NDArray ceil()
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.]
NDArray
NDArray round()
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.]
NDArray
NDArray trunc()
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.]
NDArray
NDArray exp()
NDArray
element-wise.
Examples
jshell> NDArray array = manager.create(new float[] {0f, 2.5f}); jshell> array.exp(); ND: (2) cpu() float32 [ 1. , 12.1825]
NDArray
NDArray log()
NDArray
element-wise.
Examples
jshell> NDArray array = manager.create(new float[] {0f, 2.5f}); jshell> array.log(); ND: (2) cpu() float32 [ -inf, 0.9163]
NDArray
NDArray log10()
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]
NDArray
NDArray log2()
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]
NDArray
NDArray sin()
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. ]
NDArray
NDArray cos()
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],
NDArray
NDArray tan()
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],
NDArray
NDArray asin()
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. ]
NDArray
NDArray acos()
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]
NDArray
NDArray atan()
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]
NDArray
NDArray sinh()
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]
NDArray
NDArray cosh()
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 ]
NDArray
NDArray tanh()
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]
NDArray
NDArray asinh()
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]
NDArray
NDArray acosh()
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]
NDArray
NDArray atanh()
NDArray
element-wise.
Examples
jshell> NDArray array = manager.create(new double[] {0, -0.5}); jshell> array.atanh(); ND: (2) cpu() float64 [ 0. , -0.5493]
NDArray
NDArray toDegrees()
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.]
NDArray
NDArray toRadians()
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 ]
NDArray
NDArray max()
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
NDArray
default NDArray max(int[] axes)
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.]
axes
- the axes along which to operateNDArray
with the specified axes removed from the Shape
containing the maxmax(int[], boolean)
NDArray max(int[] axes, boolean keepDims)
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.], ]
axes
- the axes along which to operatekeepDims
- true
to keep the specified axes as size 1 in the output array, false
to squeeze the values out of the output array.NDArray
NDArray min()
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
NDArray
default NDArray min(int[] axes)
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.]
axes
- the axes along which to operateNDArray
with the specified axes removed from the Shape
containing the minmin(int[], boolean)
NDArray min(int[] axes, boolean keepDims)
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.], ]
axes
- the axes along which to operatekeepDims
- true
to keep the specified axes as size 1 in the output array, false
to squeeze the values out of the output arrayNDArray
NDArray sum()
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.
NDArray
default NDArray sum(int[] axes)
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.]
axes
- the axes along which to operateNDArray
with the specified axes removed from the Shape
containing the sumsum(int[], boolean)
NDArray sum(int[] axes, boolean keepDims)
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.], ]
axes
- the axes along which to operatekeepDims
- true
to keep the specified axes as size 1 in the output array, false
to squeeze the values out of the output arrayNDArray
NDArray prod()
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.
NDArray
default NDArray prod(int[] axes)
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.]
axes
- the axes along which to operateNDArray
with the specified axes removed from the Shape
containing the prodprod(int[], boolean)
NDArray prod(int[] axes, boolean keepDims)
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.], ]
axes
- the axes along which to operatekeepDims
- true
to keep the specified axes as size 1 in the output array, false
to squeeze the values out of the output arrayNDArray
NDArray mean()
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
NDArray
default NDArray mean(int[] axes)
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]
axes
- the axes along which to operateNDArray
with the specified axes removed from the Shape
containing the meanmean(int[], boolean)
NDArray mean(int[] axes, boolean keepDims)
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], ]
axes
- the axes along which to operatekeepDims
- true
to keep the specified axes as size 1 in the output array, false
to squeeze the values out of the output arrayNDArray
default NDArray trace()
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.]
NDArray
default NDArray trace(int offset)
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.]
offset
- offset of the diagonal from the main diagonal. Can be both positive and
negative.NDArray
NDArray trace(int offset, int axis1, int axis2)
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.]
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 takenaxis2
- axes to be used as the second axis of the 2-D sub-arrays from which the
diagonals should be takenNDArray
default NDList split(long sections)
NDArray
into multiple subNDArray
s 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.]
sections
- this NDArray
will be divided into N (sections) equal NDArray
NDList
with size(axis) NDArray
s with Shape
this.shape.remove(axis)
split(long, int)
default NDList split(long[] indices)
NDArray
into multiple sub-NDArray
s 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.]
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.NDArray
s with Shape
this.shape.remove(axis)
split(long[], int)
default NDList split(long sections, int axis)
NDArray
into multiple subNDArray
s 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.], ]
sections
- this NDArray
will be divided into N (sections) equal arrays along
axisaxis
- the axis to split alongNDList
with numOutputs NDArray
s with Shape
(this.shape.axis /= axis)
java.lang.IllegalArgumentException
- thrown if the numOutputs does not equally divide the given
axisNDList split(long[] indices, int axis)
NDArray
into multiple sub-NDArray
s 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.], ]
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 correspondinglyaxis
- the axis to split alongNDList
with numOutputs NDArray
s with Shape
(this.shape.axis /= axis)
NDArray flatten()
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.]
NDArray
of equal sizedefault NDArray reshape(long... newShape)
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.], ]
newShape
- the long array to reshape into. Must have equal size to the current shapeNDArray
java.lang.IllegalArgumentException
- thrown if the given Shape
does not match the size of
the current shapeNDArray reshape(Shape shape)
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.], ]
NDArray expandDims(int axis)
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.], ]
axis
- the position in the expanded axes where the new axis is placedNDArray
. The number of dimensions is one greater than that of the
NDArray
default NDArray squeeze()
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.]
NDArray
of same size and data without singleton dimensionsdefault NDArray squeeze(int 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.], ]
axis
- the axis at which to remove the singleton dimensionNDArray
of same size and data without the axis at part of the shapejava.lang.IllegalArgumentException
- thrown if the given axis is not a singleton dimensionNDArray squeeze(int[] 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.]
axes
- the axes at which to remove the singleton dimensionsNDArray
of same size and data without the axes at part of the shapejava.lang.IllegalArgumentException
- thrown if any of the given axes are not a singleton
dimensiondefault NDArray stack(NDArray array)
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.], ]
array
- the input NDArray
which must have the same Shape
as this NDArray
NDArray
. The stacked NDArray
has one more dimension than
the input NDArray
.default NDArray stack(NDArray array, int axis)
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.], ]
array
- the input NDArray
which must have the same Shape
as this NDArray
axis
- the axis in the result NDArray
along which the input NDArray
are
stackedNDArray
. The stacked NDArray
has one more dimension than
the input NDArray
.default NDArray concat(NDArray array)
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.]
array
- a NDArray
which have the same Shape
as this NDArray
,
except in the dimension corresponding to axisNDArray
default NDArray concat(NDArray array, int axis)
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.]
array
- a NDArray
which have the same Shape
as this NDArray
,
except in the dimension corresponding to axisaxis
- the axis along which this NDArray
will be joinedNDArray
NDArray logicalAnd(NDArray other)
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]
other
- the other NDArray
to operate onNDArray
of the logical AND operation applied to the elements of
this NDArray
and the other NDArray
NDArray logicalOr(NDArray other)
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]
other
- the other NDArray
to operate onNDArray
of the logical OR operation applied to the elements of
this NDArray
and the other NDArray
NDArray logicalXor(NDArray other)
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]
other
- the other NDArray
to operate onNDArray
of the logical XOR operation applied to the elements of
this NDArray
and the other NDArray
NDArray logicalNot()
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]
NDArray
default NDArray argSort()
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], ]
NDArray
of indices corresponding to elements in this NDArray
on the
axis, the output DataType is always DataType.INT64
argSort(int, boolean)
default NDArray argSort(int axis)
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], ]
axis
- the axis to sort alongNDArray
of indices corresponding to elements in this NDArray
on the
axis, the output DataType is always DataType.INT64
argSort(int, boolean)
NDArray argSort(int axis, boolean ascending)
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], ]
axis
- the axis to sort alongascending
- whether to sort ascendingNDArray
of indices corresponding to elements in this NDArray
on the
axis, the output DataType is always DataType.INT64
NDArray sort()
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.], ]
NDArray
NDArray sort(int axis)
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., 4.], [1., 3.], ]
axis
- the axis to sort alongNDArray
NDArray softmax(int axis)
axis
- the axis along which to applyNDArray
softmax(int)
NDArray logSoftmax(int axis)
Mathematically equivalent to calling softmax and then log. This single operator is faster than calling two operators and numerically more stable when computing gradients.
axis
- the axis along which to applyNDArray
NDArray cumSum()
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.]
NDArray
NDArray cumSum(int 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.], ]
axis
- the axis along which the cumulative sum is computedNDArray isInfinite()
NDArray
with value true
where this NDArray
's
entries are infinite, or false
where they are not infinite.NDArray
with value true
if this NDArray
's entries
are infiniteNDArray isNaN()
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]
NDArray
with value true
if this NDArray
's NDArray
are NaNNDArray tile(long repeats)
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.]
repeats
- the number of times to repeat for each dimensionNDArray tile(int axis, long repeats)
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.], ]
axis
- the axis to repeatrepeats
- the number of times to repeat for each axisNDArray
that has been tiledjava.lang.IllegalArgumentException
- thrown for invalid axisNDArray tile(long[] repeats)
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.], ]
repeats
- the number of times to repeat along each axisNDArray
that has been tiledNDArray tile(Shape desiredShape)
NDArray
by repeating this NDArray
the number of times to match
the desired shape.
If the desired Shape
has 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.]
desiredShape
- the Shape
that should be converted toNDArray
that has been tiledNDArray repeat(long repeats)
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.]
repeats
- the number of times to repeat for each axisNDArray
that has been repeatedNDArray repeat(int axis, long repeats)
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.]
axis
- the axis to repeatrepeats
- the number of times to repeat for each axisNDArray
that has been repeatedjava.lang.IllegalArgumentException
- thrown for invalid axisNDArray repeat(long[] repeats)
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.]
repeats
- the number of times to repeat along each axisNDArray
that has been repeatedNDArray repeat(Shape desiredShape)
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.], ]
desiredShape
- the Shape
that should be converted toNDArray
that has been repeatedNDArray dot(NDArray other)
NDArray
and the other NDArray
.
NDArray
and the other NDArray
are 1-D NDArray
s, it
is inner product of vectors (without complex conjugation).
NDArray
and the other NDArray
are 2-D NDArray
s, it
is matrix multiplication.
NDArray
or the other NDArray
is 0-D NDArray
(scalar), it is equivalent to mul.
NDArray
is N-D NDArray
and the other NDArray
is 1-D
NDArray
, it is a sum product over the last axis of those.
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.], ], ]
other
- the other NDArray
to perform dot product withNDArray
NDArray matMul(NDArray other)
NDArray
and the other NDArray
.
The behavior depends on the arguments in the following way.
NDArray
and the other NDArray
are 2-D NDArray
s,
they are multiplied like conventional matrices
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.
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.
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.
other
- the other NDArray
to perform matrix product withNDArray
NDArray clip(java.lang.Number min, java.lang.Number max)
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.]
min
- the minimum valuemax
- the maximum valueNDArray
with the elements of this NDArray
, but where values <
min are replaced with min, and those > max with maxdefault NDArray swapAxes(int axis1, int axis2)
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.], ]
axis1
- the first axisaxis2
- the second axisNDArray
NDArray flip(int... axes)
The shape of the array is preserved, but the elements are reordered.
axes
- the axes to flip onNDArray transpose()
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.], ]
NDArray transpose(int... axes)
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.], ], ]
axes
- the axes to swap toNDArray
java.lang.IllegalArgumentException
- thrown when passing a axis that is greater than the actual
number of dimensionsNDArray broadcast(Shape shape)
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.], ], ]
shape
- the new Shape
of this NDArray
NDArray
default NDArray broadcast(long... shape)
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.], ], ]
shape
- the new Shape
of this NDArray
NDArray
NDArray argMax()
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.
NDArray
containing indicesNDArray argMax(int 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]
axis
- the axis along which to find maximum valuesNDArray
containing indicesNDArray argMin()
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.
NDArray
containing indicesNDArray argMin(int 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]
axis
- the axis along which to find minimum valuesNDArray
containing indicesNDArray percentile(java.lang.Number percentile)
NDArray
.percentile
- the target percentile in range of 0..100NDArray
NDArray percentile(java.lang.Number percentile, int[] axes)
percentile
- the target percentile in range of 0..100axes
- the dimension to calculate percentile forNDArray
NDArrayNDArray median()
NDArray
.NDArray
NDArray median(int[] axes)
axes
- the axes along which to perform the median operationNDArray
along the specified axesNDArray toDense()
NDArray
.NDArray
NDArray toSparse(SparseFormat fmt)
NDArray
.fmt
- the SparseFormat
of this NDArray
NDArray
NDArray nonzero()
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], ]
default boolean isEmpty()
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
true
if this NDArray is emptydefault NDArray all()
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
true
if all elements within this NDArray
are non-zero or true
default NDArray any()
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
true
if any of the elements within this NDArray
are non-zero or
true
default NDArray none()
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
true
if none of the elements within this NDArray
are non-zero or
true
default NDArray countNonzero()
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
NDArray
default NDArray countNonzero(int axis)
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]
axis
- the axis to operate onNDArray
along a given axisNDArray erfinv()
NDArray
.
Examples
jshell> NDArray array = manager.create(new float[] {0f, 0.5f, -1f}); jshell> array.erfinv(); ND: (3) cpu() float32 [0., 0.4769, -inf]
NDArray
, element-wiseai.djl.ndarray.internal.NDArrayEx getNDArrayInternal()
NDArray
.
This method should only be used by Engine provider
NDArray
default java.lang.String toDebugString(int maxSize, int maxDepth, int maxRows, int maxColumns)
NDArray
.maxSize
- the maximum elements to print outmaxDepth
- the maximum depth to print outmaxRows
- the maximum rows to print outmaxColumns
- the maximum columns to print outNDArray
void close()
close
in interface java.lang.AutoCloseable