/** * Returns a subset of an array from 0 to "to" * using the specified stride * * @param data the data to getFromOrigin a subset of * @param to the end point of the data * @param stride the stride to go through the array * @return the subset of the data specified */ public static double[] range(double[] data, int to, int stride) { return range(data, to, stride, 1); }
public static long[] range(long from, long to) { if (from == to) return new long[0]; return range(from, to, 1); }
/** * Returns a subset of an array from 0 to "to" * * @param data the data to getFromOrigin a subset of * @param to the end point of the data * @return the subset of the data specified */ public static double[] range(double[] data, int to) { return range(data, to, 1); }
/** * Generate an int array ranging from * from to to. * if from is > to this method will * count backwards * * @param from the from * @param to the end point of the data * @return the int array with a length equal to absoluteValue(from - to) */ public static int[] range(int from, int to) { if (from == to) return new int[0]; return range(from, to, 1); }
/** * Generate an array from 0 to length * and generate take a subset * @param length the length to generate to * @param from the begin of the interval to take * @param to the end of the interval to take * @return the generated array */ public static int[] copyOfRangeFrom(int length, int from, int to) { return Arrays.copyOfRange(ArrayUtil.range(0, length), from, to); }
/** * Convert all dimensions in the specified * axes array to be positive * based on the specified range of values * @param range * @param axes * @return */ public static int[] convertNegativeIndices(int range, int[] axes) { int[] axesRet = ArrayUtil.range(0, range); int[] newAxes = ArrayUtil.copy(axes); for (int i = 0; i < axes.length; i++) { newAxes[i] = axes[axesRet[i]]; } return newAxes; }
/** * ND Convolution * * @param input the input to transform * @param kernel the kernel to transform with * @param type the opType of convolution * @return the convolution of the given input and kernel */ @Override public INDArray convn(INDArray input, INDArray kernel, Convolution.Type type) { return convn(input, kernel, type, ArrayUtil.range(0, input.shape().length)); }
/** * Array of evenly spaced values. * * @param begin the begin of the range * @param end the end of the range * @return the range vector */ @Override public INDArray arange(double begin, double end) { return Nd4j.create(ArrayUtil.toDoubles(ArrayUtil.range((int) begin, (int) end))); }
/** * ND Convolution * * @param input the input to transform * @param kernel the kernel to transform with * @param type the opType of convolution * @return the convolution of the given input and kernel */ @Override public IComplexNDArray convn(IComplexNDArray input, IComplexNDArray kernel, Convolution.Type type) { return convn(input, kernel, type, ArrayUtil.range(0, input.shape().length)); }
/** * 2d convolution (aka the last 2 dimensions * * @param input the input to op * @param kernel the kernel to convolve with * @param type * @return */ @Override public INDArray conv2d(INDArray input, INDArray kernel, Convolution.Type type) { int[] axes = input.shape().length < 2 ? ArrayUtil.range(0, 1) : ArrayUtil.range(input.shape().length - 2, input.shape().length); return convn(input, kernel, type, axes); }
/** * Mainly here for people coming from numpy. * This is equivalent to a call to permute * * @param dimension the dimension to swap * @param with the one to swap it with * @return the swapped axes view */ @Override public INDArray swapAxes(int dimension, int with) { int[] shape = ArrayUtil.range(0, shape().length); shape[dimension] = with; shape[with] = dimension; return permute(shape); }
/** * * Return transposed version of this matrix. * * PLEASE NOTE: This method is NOT in place, it will return transposed copy instead. */ @Override public INDArray transposei() { return permute(ArrayUtil.reverseCopy(ArrayUtil.range(0, rank()))); }
/** * Roll the specified axis backwards, * until it lies in a given position. * See numpy's rollaxis * @param a the array to roll * @param axis the axis to roll backwards * @param start the starting point * @return the rolled ndarray */ public static INDArray rollAxis(INDArray a, int axis, int start) { if (axis < 0) axis += a.rank(); if (start < 0) start += a.rank(); if (axis == start) return a; if (axis < start) start--; if (!(axis >= 0 && axis < a.rank())) throw new IllegalArgumentException("Axis must be >= 0 && < start"); if (!(start >= 0 && axis < a.rank() + 1)) throw new IllegalArgumentException("Axis must be >= 0 && < start"); List<Integer> range = new ArrayList<>(Ints.asList(ArrayUtil.range(0, a.rank()))); range.remove(axis); range.add(start, axis); int[] newRange = Ints.toArray(range); return a.permute(newRange); }
@Override public List<long[]> calculateOutputShape() { if (permuteDims == null && arg() != null && arg().getShape() != null) { this.permuteDims = ArrayUtil.reverseCopy(ArrayUtil.range(0, arg().getShape().length)); val permutedShape = ArrayUtil.permute(arg().getShape(), permuteDims); return Arrays.asList(permutedShape); } else if (permuteDims != null && arg() != null && arg().getShape() != null) { val permutedShape = ArrayUtil.permute(arg().getShape(), permuteDims); return Arrays.asList(permutedShape); } return Collections.emptyList(); }
@Override public List<SDVariable> doDiff(List<SDVariable> i_v1) { List<SDVariable> ret = new ArrayList<>(); int[] bAxes = range(0, rarg().getShape().length); int[] aAxes = range(0, larg().getShape().length); int aRank = larg().getShape().length; int bRank = rarg().getShape().length; removeIndex(aAxes, sumAxes[0]), removeIndex(bAxes, sumAxes[1])}; int[] gAxes = range(0, i_v1.get(0).getShape().length); int[][] firstAxes = new int[][]{ Arrays.copyOfRange(gAxes, deletedAxes[0].length, gAxes.length),
if (intendedIndexes[i] instanceof NDArrayIndexAll) { SpecifiedIndex specifiedIndex = new SpecifiedIndex(ArrayUtil.range(0L, (long) shape.getInt(i))); ret[i] = specifiedIndex; } else if (intendedIndexes[i] instanceof NDArrayIndexEmpty) { } else if (intendedIndexes[i] instanceof IntervalIndex) { IntervalIndex intervalIndex = (IntervalIndex) intendedIndexes[i]; ret[i] = new SpecifiedIndex(ArrayUtil.range(intervalIndex.begin, intervalIndex.end(), intervalIndex.stride()));
int[] remove = ArrayUtil.removeIndex(ArrayUtil.range(0, rank()), dimension); int[] newPermuteDims = Ints.concat(remove, reverseDimensions); int[] finalPermuteDims = tadFinalPermuteDimensions[dimension.length];
this.permuteDims = ArrayUtil.reverseCopy(ArrayUtil.range(0, arr.rank()));