/** * Returns true if this shape is scalar * @param shape the shape that is scalar * @return */ public static boolean shapeIsScalar(int[] shape) { return shape.length == 0 || ArrayUtil.prodLong(shape) == 1; }
public static boolean isVector(long[] shape) { if (shape.length > 2 || shape.length < 1) return false; else { long len = ArrayUtil.prodLong(shape); return shape[0] == len || shape[1] == len; } }
/** * Convert a linear index to * the equivalent nd index. * Infers the number of indices from the specified shape. * * @param shape the shape of the dimensions * @param index the index to map * @return the mapped indexes along each dimension */ public static int[] ind2subC(int[] shape, long index) { return ind2subC(shape, index, ArrayUtil.prodLong(shape)); }
/** * Construct an ndarray of the specified shape. * * @param shape the shape of the ndarray * @param stride the stride of the ndarray * @param offset the desired offset * @param ordering the ordering of the ndarray * @param initialize Whether to initialize the INDArray. If true: initialize. If false: don't. */ public BaseNDArray(int[] shape, int[] stride, long offset, char ordering, boolean initialize) { this(Nd4j.createBuffer(ArrayUtil.prodLong(shape), initialize), shape, stride, offset, ordering); }
/** * Construct an ndarray of the specified shape * with an empty data array * * @param shape the shape of the ndarray * @param stride the stride of the ndarray * @param offset the desired offset * @param ordering the ordering of the ndarray */ public BaseNDArray(int[] shape, int[] stride, long offset, char ordering) { this(Nd4j.createBuffer(ArrayUtil.prodLong(shape)), shape, stride, offset, ordering); }
public static long sliceOffsetForTensor(int index, INDArray arr, long[] tensorShape) { long tensorLength = ArrayUtil.prodLong(tensorShape); long lengthPerSlice = NDArrayMath.lengthPerSlice(arr); long offset = index * tensorLength / lengthPerSlice; return offset; }
protected static void checkShapeValues(int length, int[] shape) { checkShapeValues(shape); if (ArrayUtil.prodLong(shape) > length) throw new ND4JIllegalStateException("Shape of the new array " + Arrays.toString(shape) + " doesn't match data length: " + length); }
@Override public IComplexNDArray createComplex(int[] shape, int[] complexStrides, long offset, char ordering) { //ensure shapes that wind up being scalar end up with the write shape if (shape.length == 1 && shape[0] == 0) { shape = new int[] {1, 1}; } return createComplex(Nd4j.createBuffer(ArrayUtil.prodLong(shape) * 2), shape, complexStrides, offset, ordering); }
@Override public INDArray create(int[] shape, int[] stride, long offset, char ordering) { //ensure shapes that wind up being scalar end up with the write shape if (shape.length == 1 && shape[0] == 0) { shape = new int[] {1, 1}; } return create(Nd4j.createBuffer(ArrayUtil.prodLong(shape)), shape, stride, offset, ordering); }
/** * * @param shape * @param offset * @param ordering */ public BaseComplexNDArray(int[] shape, long offset, char ordering) { this(Nd4j.createBuffer(ArrayUtil.prodLong(shape) * 2), shape, Nd4j.getComplexStrides(shape, ordering), offset, ordering); }
/** * * @param shape */ public BaseComplexNDArray(int[] shape) { this(Nd4j.createBuffer(ArrayUtil.prodLong(shape) * 2), shape, Nd4j.getComplexStrides(shape)); }
@Override public INDArray create(long[] shape, long[] stride, long offset, char ordering) { if (shape.length == 1 && shape[0] == 0) { shape = new long[] {1, 1}; } return create(Nd4j.createBuffer(ArrayUtil.prodLong(shape)), shape, stride, offset, ordering); }
@Override public INDArray create(long[] shape, long[] stride, long offset) { //ensure shapes that wind up being scalar end up with the write shape if (shape.length == 1 && shape[0] == 0) { shape = new long[] {1, 1}; } DataBuffer buffer = Nd4j.createBuffer(ArrayUtil.prodLong(shape)); return create(buffer, shape, stride, offset); }
/** * * @param buffer * @param shape * @param offset */ public BaseNDArray(DataBuffer buffer, int[] shape, long offset) { this(Nd4j.createBuffer(buffer, offset, ArrayUtil.prodLong(shape)), shape, Nd4j.getStrides(shape), offset, Nd4j.order()); }
@Override public INDArray nextFloat(char order, long[] shape) { long length = ArrayUtil.prodLong(shape); INDArray ret = Nd4j.create(shape, order); DataBuffer data = ret.data(); for (long i = 0; i < length; i++) { data.put(i, nextFloat()); } return ret; }
@Override public INDArray nextGaussian(char order, long[] shape) { long length = ArrayUtil.prodLong(shape); INDArray ret = Nd4j.create(shape, order); DataBuffer data = ret.data(); for (long i = 0; i < length; i++) { data.put(i, nextGaussian()); } return ret; }
public static DataBuffer createBufferDetached(int[] shape, DataBuffer.Type type) { long length = ArrayUtil.prodLong(shape); if (type == DataBuffer.Type.INT) return DATA_BUFFER_FACTORY_INSTANCE.createInt(length); if (type == DataBuffer.Type.LONG) return DATA_BUFFER_FACTORY_INSTANCE.createLong(new long[]{length}); else if (type == DataBuffer.Type.HALF) return DATA_BUFFER_FACTORY_INSTANCE.createHalf(length); return type == DataBuffer.Type.DOUBLE ? DATA_BUFFER_FACTORY_INSTANCE.createDouble(length) : DATA_BUFFER_FACTORY_INSTANCE.createFloat(length); }
public static DataBuffer createBufferDetached(long[] shape, DataBuffer.Type type) { long length = ArrayUtil.prodLong(shape); if (type == DataBuffer.Type.INT) return DATA_BUFFER_FACTORY_INSTANCE.createInt(length); else if (type == DataBuffer.Type.LONG) return DATA_BUFFER_FACTORY_INSTANCE.createLong(length); else if (type == DataBuffer.Type.HALF) return DATA_BUFFER_FACTORY_INSTANCE.createHalf(length); return type == DataBuffer.Type.DOUBLE ? DATA_BUFFER_FACTORY_INSTANCE.createDouble(length) : DATA_BUFFER_FACTORY_INSTANCE.createFloat(length); }
public BaseNDArray(DataBuffer buffer, long[] shape, long[] stride, long offset, char ordering) { this.data = offset > 0 ? Nd4j.createBuffer(buffer, offset, ArrayUtil.prodLong(shape)) : buffer; setShapeInformation(Nd4j.getShapeInfoProvider().createShapeInformation(shape, stride, offset, Shape.elementWiseStride(shape, stride, ordering == 'f'), ordering)); init(shape, stride); // Shape.setElementWiseStride(this.shapeInfo(),Shape.elementWiseStride(shape, stride, ordering == 'f')); }
/** * * @param data * @param shape * @param stride * @param offset */ public BaseNDArray(DataBuffer data, int[] shape, int[] stride, long offset) { this.data = Nd4j.createBuffer(data, offset, ArrayUtil.prodLong(shape)); setShapeInformation(Nd4j.getShapeInfoProvider().createShapeInformation(shape, stride, offset, Shape.elementWiseStride(shape, stride, Nd4j.order() == 'f'), Nd4j.order())); init(shape, stride); // Shape.setElementWiseStride(this.shapeInfo(),Shape.elementWiseStride(shape, stride, Nd4j.order() == 'f')); }