/** * Prunes indices of greater length than the shape * and fills in missing indices if there are any * * @param originalShape the original shape to adjust to * @param indexes the indexes to adjust * @return the adjusted indices */ public static INDArrayIndex[] adjustIndices(int[] originalShape, INDArrayIndex... indexes) { if (Shape.isVector(originalShape) && indexes.length == 1) return indexes; if (indexes.length < originalShape.length) indexes = fillIn(originalShape, indexes); if (indexes.length > originalShape.length) { INDArrayIndex[] ret = new INDArrayIndex[originalShape.length]; System.arraycopy(indexes, 0, ret, 0, originalShape.length); return ret; } if (indexes.length == originalShape.length) return indexes; for (int i = 0; i < indexes.length; i++) { if (indexes[i].end() >= originalShape[i] || indexes[i] instanceof NDArrayIndexAll) indexes[i] = NDArrayIndex.interval(0, originalShape[i] - 1); } return indexes; }
/** * 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. * @param arr the array to compute the offset for * @param indices the offsets for each dimension * @return the offset that should be used for indexing */ public static long offset(INDArray arr, INDArrayIndex... indices) { return offset(arr.stride(), Indices.offsets(arr.shape(), indices)); }
/** * Calculate the strides based on the given indices * * @param ordering the ordering to calculate strides for * @param indexes the indices to calculate stride for * @return the strides for the given indices */ public static int[] strides(char ordering, NDArrayIndex... indexes) { return Nd4j.getStrides(shape(indexes), ordering); }
INDArrayIndex[] indexes = Indices.createFromStartAndEnd(startIndex, endIndex);
INDArrayIndex[] indexes = Indices.createFromStartAndEnd(startIndex, endIndex);
/** * Calculate the shape for the given set of indices and offsets. * <p/> * The shape is defined as (for each dimension) * the difference between the end index + 1 and * the begin index * <p/> * If specified, this will check for whether any of the indices are >= to end - 1 * and if so, prune it down * * @param shape the original shape * @param indices the indices to calculate the shape for * @return the shape for the given indices */ public static int[] shape(int[] shape, INDArrayIndex... indices) { return LongUtils.toInts(shape(LongUtils.toLongs(shape), 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. * @param arr the array to compute the offset for * @param indices the offsets for each dimension * @return the offset that should be used for indexing */ public static long offset(INDArray arr, INDArrayIndex... indices) { return offset(arr.stride(), Indices.offsets(arr.shape(), indices)); }
/** * Prunes indices of greater length than the shape * and fills in missing indices if there are any * * @param originalShape the original shape to adjust to * @param indexes the indexes to adjust * @return the adjusted indices */ public static INDArrayIndex[] adjustIndices(int[] originalShape, INDArrayIndex... indexes) { if (Shape.isVector(originalShape) && indexes.length == 1) return indexes; if (indexes.length < originalShape.length) indexes = fillIn(originalShape, indexes); if (indexes.length > originalShape.length) { INDArrayIndex[] ret = new INDArrayIndex[originalShape.length]; System.arraycopy(indexes, 0, ret, 0, originalShape.length); return ret; } if (indexes.length == originalShape.length) return indexes; for (int i = 0; i < indexes.length; i++) { if (indexes[i].end() >= originalShape[i] || indexes[i] instanceof NDArrayIndexAll) indexes[i] = NDArrayIndex.interval(0, originalShape[i] - 1); } return indexes; }
/** * Calculate the strides based on the given indices * * @param ordering the ordering to calculate strides for * @param indexes the indices to calculate stride for * @return the strides for the given indices */ public static int[] strides(char ordering, NDArrayIndex... indexes) { return Nd4j.getStrides(shape(indexes), ordering); }
/** * Calculate the shape for the given set of indices and offsets. * <p/> * The shape is defined as (for each dimension) * the difference between the end index + 1 and * the begin index * <p/> * If specified, this will check for whether any of the indices are >= to end - 1 * and if so, prune it down * * @param shape the original shape * @param indices the indices to calculate the shape for * @return the shape for the given indices */ public static int[] shape(int[] shape, INDArrayIndex... indices) { return LongUtils.toInts(shape(LongUtils.toLongs(shape), indices)); }