/** * Gets the rank given the shape info buffer * @param buffer the buffer to get the rank for * @return the rank for the shape buffer */ public static int rank(DataBuffer buffer) { return buffer.getInt(0); }
private static int[] toIntArray(int length, DataBuffer buffer) { int[] ret = new int[length]; for (int i = 0; i < length; i++) { ret[i] = buffer.getInt(i); } return ret; }
/** * Get the size of the specified dimension. Identical to Shape.size(...), but does not perform any input validation * @param buffer The buffer to get the shape from * @param dimension The dimension to get. * @return The size of the specified dimension */ public static int sizeUnsafe(DataBuffer buffer, int dimension) { return buffer.getInt(1 + dimension); }
/** * Get the stride of the specified dimension, without any input validation * @param buffer The buffer to get the stride from * @param dimension The dimension to get. * @param rank Rank of the array * @return The stride of the specified dimension */ public static int strideUnsafe(DataBuffer buffer, int dimension, int rank) { return buffer.getInt(1 + rank + dimension); }
protected int[] doPermuteSwap(DataBuffer shape, int[] rearrange) { int[] ret = new int[rearrange.length]; for (int i = 0; i < rearrange.length; i++) { ret[i] = shape.getInt(rearrange[i]); } return ret; }
public static int[] hiddenDimension(DataBuffer buffer) { int flagsLength = buffer.getInt(0); int offLength = buffer.getInt(flagsLength + 1); int hiddenDimLength = buffer.getInt(flagsLength + offLength + 2); int[] ret = new int[hiddenDimLength]; for (int i = 0; i < hiddenDimLength; i++) { ret[i] = buffer.getInt(i + flagsLength + offLength + 3); } return ret; }
/** * Get the element wise stride for the * shape info buffer * @param buffer the buffer to get the element * wise stride from * @return the element wise stride for the buffer */ public static int elementWiseStride(DataBuffer buffer) { int length2 = shapeInfoLength(buffer.getInt(0)); return buffer.getInt(length2 - 2); }
/** * Get array shape from the buffer, as an int[] * @param buffer Buffer to get the shape from * @return Shape array */ public static long[] shape(DataBuffer buffer) { val ret = new long[rank(buffer)]; for (int i = 0; i < ret.length; i++) ret[i] = buffer.getInt(1 + i); return ret; }
/** * Get the size of the specified dimension. Equivalent to shape()[dimension] * @param buffer The buffer to get the shape from * @param dimension The dimension to get. * @return The size of the specified dimension */ public static int size(DataBuffer buffer, int dimension) { int rank = rank(buffer); if (dimension >= rank) throw new IllegalArgumentException("Invalid dimension " + dimension + " for rank " + rank + " array"); return buffer.getInt(1 + dimension); }
/** * Get the stride of the specified dimension * @param buffer The buffer to get the stride from * @param dimension The dimension to get. * @return The stride of the specified dimension */ public static int stride(DataBuffer buffer, int dimension) { int rank = rank(buffer); if (dimension >= rank) throw new IllegalArgumentException("Invalid dimension " + dimension + " for rank " + rank + " array"); return buffer.getInt(1 + rank + dimension); }
/** * Returns the order given the shape information * @param buffer the buffer * @return */ public static char order(DataBuffer buffer) { int length = Shape.shapeInfoLength(Shape.rank(buffer)); return (char) buffer.getInt(length - 1); }
/** * Returns true if the given shape is of length 1 * or provided the shape length is 2: * element 0 is 1 * @param shapeInfo the shape info to check * @return true if the above conditions hold,false otherwise */ public static boolean isRowVectorShape(DataBuffer shapeInfo) { int rank = Shape.rank(shapeInfo); DataBuffer shape = Shape.shapeOf(shapeInfo); return (rank == 2 && shape.getInt(0) == 1) || rank == 1; }
/** * Get array shape from the buffer, as an int[] * @param buffer Buffer to get the shape from * @return Shape array */ public static long[] strideArr(DataBuffer buffer) { val ret = new long[rank(buffer)]; DataBuffer stride = Shape.stride(buffer); for (int i = 0; i < ret.length; i++) ret[i] = stride.getInt(i); return ret; }
@Override public boolean isScalar() { if (isScalar != null) return isScalar; if (Shape.rank(shapeInformation) > 2) { isScalar = false; } else if (Shape.rank(shapeInformation) == 1) { isScalar = shapeOf().getInt(0) == 1; } else if (Shape.rank(shapeInformation) == 2) { isScalar = shapeOf().getInt(0) == 1 && shapeOf().getInt(1) == 1; } else isScalar = false; return isScalar; }
@Override public INDArray toDense() { // Dummy way - going to use the conversion routines in level2 (?) INDArray result = Nd4j.zeros(shape()); int[] pointersB = pointerB.asInt(); int[] pointersE = pointerE.asInt(); for (int row = 0; row < rows(); row++) { for (int idx = pointersB[row]; idx < pointersE[row]; idx++) { result.put(row, columnsPointers.getInt(idx), values.getNumber(idx)); } } return result; }