Package org.nd4j.linalg.indexing
Class NDArrayIndex
- java.lang.Object
-
- org.nd4j.linalg.indexing.NDArrayIndex
-
- All Implemented Interfaces:
INDArrayIndex
public abstract class NDArrayIndex extends Object implements INDArrayIndex
NDArray indexing- Author:
- Adam Gibson
-
-
Constructor Summary
Constructors Constructor Description NDArrayIndex(long... indices)
NDArrayIndexing based on the given indexes
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static INDArrayIndex
all()
Represents collecting all elementsstatic INDArrayIndex[]
allFor(INDArray arr)
Generate an all index equal to the rank of the given arraystatic INDArrayIndex[]
createCoveringShape(int[] shape)
Creates an index covering the given shape (for each dimension 0,shape[i])static INDArrayIndex[]
createCoveringShape(long[] shape)
static INDArrayIndex[]
deepCopy(INDArrayIndex[] indices)
Deep copy all elements of this array ofINDArrayIndex
invokingINDArrayIndex.dup()
on each entrylong
end()
The ending for this indexboolean
equals(Object o)
int
hashCode()
static INDArrayIndex[]
indexesFor(long... shape)
Add indexes for the given shapestatic INDArrayIndex
indices(long... indices)
Returns an instance ofSpecifiedIndex
.void
init(long begin, long end)
Initialize based on the specified begin and endvoid
init(long begin, long end, long max)
void
init(INDArray arr, int dimension)
Init the index wrt the dimension and the given nd arrayvoid
init(INDArray arr, long begin, int dimension)
Init the index wrt the dimension and the given nd arraystatic INDArrayIndex
interval(int begin, int end)
Generates an interval from begin (inclusive) to end (exclusive)static INDArrayIndex
interval(int begin, int stride, int end, boolean inclusive)
Generates an interval from begin (inclusive) to end (exclusive)static INDArrayIndex
interval(long begin, long end)
static INDArrayIndex
interval(long begin, long end, boolean inclusive)
Generates an interval from begin (inclusive) to end (exclusive)static INDArrayIndex
interval(long begin, long stride, long end)
Generates an interval from begin (inclusive) to end (exclusive)static INDArrayIndex
interval(long begin, long stride, long end, boolean inclusive)
static INDArrayIndex
interval(long begin, long stride, long end, long max)
Generates an interval from begin (inclusive) to end (exclusive)static INDArrayIndex
interval(long begin, long stride, long end, long max, boolean inclusive)
long
length()
Returns the length of the indicesstatic INDArrayIndex
newAxis()
Represents adding a new dimensionstatic INDArrayIndex[]
nTimes(INDArrayIndex copy, int n)
Repeat a copy of copy n timesstatic int
numNewAxis(INDArrayIndex... axes)
Given an array of indexes return the number of new axis elements in teh arraystatic int
numPoints(INDArrayIndex... indexes)
Number of point indexeslong
offset()
The start of this indexstatic long
offset(int[] strides, long[] offsets)
static long
offset(long[] strides, long[] offsets)
Compute the offset given an array of offsets.static long
offset(INDArray arr, long... offsets)
Compute the offset given an array of offsets.static long
offset(INDArray arr, INDArrayIndex... indices)
Compute the offset given an array of offsets.static INDArrayIndex
point(long point)
Returns a point indexstatic INDArrayIndex[]
rangeOfLength(INDArrayIndex[] indexes)
Create a range based on the given indexes.static INDArrayIndex[]
resolve(int[] shape, INDArrayIndex... intendedIndexes)
Given an all index and the intended indexes, return an index array containing a combination of all elements for slicing and overriding particular indexes where necessarystatic INDArrayIndex[]
resolve(long[] shape, INDArrayIndex... intendedIndexes)
static INDArrayIndex[]
resolve(INDArray arr, INDArrayIndex... intendedIndexes)
Given an all index and the intended indexes, return an index array containing a combination of all elements for slicing and overriding particular indexes where necessarystatic INDArrayIndex[]
resolve(INDArrayIndex[] allIndex, INDArrayIndex... intendedIndexes)
Given an all index and the intended indexes, return an index array containing a combination of all elements for slicing and overriding particular indexes where necessarystatic INDArrayIndex[]
resolveLong(long[] shapeInfo, INDArrayIndex... intendedIndexes)
Given an all index and the intended indexes, return an index array containing a combination of all elements for slicing and overriding particular indexes where necessaryvoid
reverse()
Reverse the indexeslong
stride()
The stride for the index (most of the time will be 1)String
toString()
protected static INDArrayIndex
validate(long size, INDArrayIndex index)
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.nd4j.linalg.indexing.INDArrayIndex
dup, initialized, isInterval
-
-
-
-
Method Detail
-
deepCopy
public static INDArrayIndex[] deepCopy(INDArrayIndex[] indices)
Deep copy all elements of this array ofINDArrayIndex
invokingINDArrayIndex.dup()
on each entry- Parameters:
indices
- the indices to copy- Returns:
-
point
public static INDArrayIndex point(long point)
Returns a point index- Parameters:
point
- the point index- Returns:
- the point index based on the specified point
-
indexesFor
public static INDArrayIndex[] indexesFor(long... shape)
Add indexes for the given shape- Parameters:
shape
- the shape ot convert to indexes- Returns:
- the indexes for the given shape
-
offset
public static long offset(INDArray arr, long... offsets)
Compute the offset given an array of offsets. The offset is computed(for both fortran an d c ordering) as: sum from i to n - 1 o[i] * s[i] where i is the index o is the offset and s is the stride Notice the -1 at the end.- Parameters:
arr
- the array to compute the offset foroffsets
- the offsets for each dimension- Returns:
- the offset that should be used for indexing
-
offset
public static long offset(INDArray arr, INDArrayIndex... indices)
Compute the offset given an array of offsets. The offset is computed(for both fortran an d c ordering) as: sum from i to n - 1 o[i] * s[i] where i is the index o is the offset and s is the stride Notice the -1 at the end.- Parameters:
arr
- the array to compute the offset forindices
- the offsets for each dimension- Returns:
- the offset that should be used for indexing
-
offset
public static long offset(long[] strides, long[] offsets)
Compute the offset given an array of offsets. The offset is computed(for both fortran an d c ordering) as: sum from i to n - 1 o[i] * s[i] where i is the index o is the offset and s is the stride Notice the -1 at the end.- Parameters:
strides
- the strides to compute the offset foroffsets
- the offsets for each dimension- Returns:
- the offset that should be used for indexing
-
offset
public static long offset(int[] strides, long[] offsets)
-
nTimes
public static INDArrayIndex[] nTimes(INDArrayIndex copy, int n)
Repeat a copy of copy n times- Parameters:
copy
- the ndarray index to copyn
- the number of times to copy- Returns:
- an array of length n containing copies of the given ndarray index
-
all
public static INDArrayIndex all()
Represents collecting all elements- Returns:
- an ndarray index meaning collect all elements
-
indices
public static INDArrayIndex indices(long... indices)
Returns an instance ofSpecifiedIndex
. Note that SpecifiedIndex works differently than the other indexing options, in that it always returns a copy of the (subset of) the underlying array, for get operations. This means that INDArray.get(..., indices(x,y,z), ...) will be a copy of the relevant subset of the array.- Parameters:
indices
- Indices to get
-
newAxis
public static INDArrayIndex newAxis()
Represents adding a new dimension- Returns:
- the indexing for adding a new dimension
-
resolve
public static INDArrayIndex[] resolve(INDArray arr, INDArrayIndex... intendedIndexes)
Given an all index and the intended indexes, return an index array containing a combination of all elements for slicing and overriding particular indexes where necessary- Parameters:
arr
- the array to resolve indexes forintendedIndexes
- the indexes specified by the user- Returns:
- the resolved indexes (containing all where nothing is specified, and the intended index for a particular dimension otherwise)
-
numPoints
public static int numPoints(INDArrayIndex... indexes)
Number of point indexes- Parameters:
indexes
- the indexes to count for points- Returns:
- the number of point indexes in the array
-
resolveLong
public static INDArrayIndex[] resolveLong(long[] shapeInfo, INDArrayIndex... intendedIndexes)
Given an all index and the intended indexes, return an index array containing a combination of all elements for slicing and overriding particular indexes where necessary- Parameters:
shapeInfo
- the index containing all elementsintendedIndexes
- the indexes specified by the user- Returns:
- the resolved indexes (containing all where nothing is specified, and the intended index for a particular dimension otherwise)
-
resolve
public static INDArrayIndex[] resolve(int[] shape, INDArrayIndex... intendedIndexes)
Given an all index and the intended indexes, return an index array containing a combination of all elements for slicing and overriding particular indexes where necessary- Parameters:
shape
- the index containing all elementsintendedIndexes
- the indexes specified by the user- Returns:
- the resolved indexes (containing all where nothing is specified, and the intended index for a particular dimension otherwise)
-
resolve
public static INDArrayIndex[] resolve(long[] shape, INDArrayIndex... intendedIndexes)
-
validate
protected static INDArrayIndex validate(long size, INDArrayIndex index)
-
resolve
public static INDArrayIndex[] resolve(INDArrayIndex[] allIndex, INDArrayIndex... intendedIndexes)
Given an all index and the intended indexes, return an index array containing a combination of all elements for slicing and overriding particular indexes where necessary- Parameters:
allIndex
- the index containing all elementsintendedIndexes
- the indexes specified by the user- Returns:
- the resolved indexes (containing all where nothing is specified, and the intended index for a particular dimension otherwise)
-
numNewAxis
public static int numNewAxis(INDArrayIndex... axes)
Given an array of indexes return the number of new axis elements in teh array- Parameters:
axes
- the indexes to get the number of new axes for- Returns:
- the number of new axis elements in the given array
-
allFor
public static INDArrayIndex[] allFor(INDArray arr)
Generate an all index equal to the rank of the given array- Parameters:
arr
- the array to generate the all index for- Returns:
- an ndarray index array containing of length arr.rank() containing all elements
-
createCoveringShape
public static INDArrayIndex[] createCoveringShape(int[] shape)
Creates an index covering the given shape (for each dimension 0,shape[i])- Parameters:
shape
- the shape to cover- Returns:
- the ndarray indexes to cover
-
createCoveringShape
public static INDArrayIndex[] createCoveringShape(long[] shape)
-
rangeOfLength
public static INDArrayIndex[] rangeOfLength(INDArrayIndex[] indexes)
Create a range based on the given indexes. This is similar to create covering shape in that it approximates the length of each dimension (ignoring elements) and reproduces an index of the same dimension and length.- Parameters:
indexes
- the indexes to create the range for- Returns:
- the index ranges.
-
interval
public static INDArrayIndex interval(long begin, long stride, long end, long max)
Generates an interval from begin (inclusive) to end (exclusive)- Parameters:
begin
- the beginstride
- the stride at which to incrementend
- the end indexmax
- the max length for this domain- Returns:
- the interval
-
interval
public static INDArrayIndex interval(long begin, long stride, long end)
Generates an interval from begin (inclusive) to end (exclusive)- Parameters:
begin
- the beginstride
- the stride at which to incrementend
- the end index- Returns:
- the interval
-
interval
public static INDArrayIndex interval(int begin, int stride, int end, boolean inclusive)
Generates an interval from begin (inclusive) to end (exclusive)- Parameters:
begin
- the beginstride
- the stride at which to incrementend
- the end indexinclusive
- whether the end should be inclusive or not- Returns:
- the interval
-
interval
public static INDArrayIndex interval(long begin, long stride, long end, long max, boolean inclusive)
-
interval
public static INDArrayIndex interval(long begin, long stride, long end, boolean inclusive)
-
interval
public static INDArrayIndex interval(int begin, int end)
Generates an interval from begin (inclusive) to end (exclusive)- Parameters:
begin
- the beginend
- the end index- Returns:
- the interval
-
interval
public static INDArrayIndex interval(long begin, long end)
-
interval
public static INDArrayIndex interval(long begin, long end, boolean inclusive)
Generates an interval from begin (inclusive) to end (exclusive)- Parameters:
begin
- the beginend
- the end indexinclusive
- whether the end should be inclusive or not- Returns:
- the interval
-
end
public long end()
Description copied from interface:INDArrayIndex
The ending for this index- Specified by:
end
in interfaceINDArrayIndex
- Returns:
-
offset
public long offset()
Description copied from interface:INDArrayIndex
The start of this index- Specified by:
offset
in interfaceINDArrayIndex
- Returns:
-
length
public long length()
Returns the length of the indices- Specified by:
length
in interfaceINDArrayIndex
- Returns:
- the length of the range
-
stride
public long stride()
Description copied from interface:INDArrayIndex
The stride for the index (most of the time will be 1)- Specified by:
stride
in interfaceINDArrayIndex
- Returns:
-
reverse
public void reverse()
Description copied from interface:INDArrayIndex
Reverse the indexes- Specified by:
reverse
in interfaceINDArrayIndex
-
init
public void init(INDArray arr, long begin, int dimension)
Description copied from interface:INDArrayIndex
Init the index wrt the dimension and the given nd array- Specified by:
init
in interfaceINDArrayIndex
- Parameters:
arr
- the array to initialize onbegin
- the beginning indexdimension
- the dimension to initialize on
-
init
public void init(INDArray arr, int dimension)
Description copied from interface:INDArrayIndex
Init the index wrt the dimension and the given nd array- Specified by:
init
in interfaceINDArrayIndex
- Parameters:
arr
- the array to initialize ondimension
- the dimension to initialize on
-
init
public void init(long begin, long end, long max)
- Specified by:
init
in interfaceINDArrayIndex
-
init
public void init(long begin, long end)
Description copied from interface:INDArrayIndex
Initialize based on the specified begin and end- Specified by:
init
in interfaceINDArrayIndex
-
-