public DataBuffer shiftLeft(DataBuffer buffer, int from, int offset, long datalength) { for (int i = from; i < datalength; i++) { buffer.put(i - offset, buffer.getDouble(i)); } return buffer; }
private DataBuffer addAtPosition(DataBuffer buf, long dataSize, int pos, double value) { DataBuffer buffer = (buf.length() == dataSize) ? reallocate(buf) : buf; double[] tail = buffer.getDoublesAt(pos, (int) dataSize - pos); buffer.put(pos, value); for (int i = 0; i < tail.length; i++) { buffer.put(i + pos + 1, tail[i]); } return buffer; }
/** * 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 void setElementWiseStride(DataBuffer buffer, int elementWiseStride) { int length2 = shapeInfoLength(Shape.rank(buffer)); //if (1 > 0) throw new RuntimeException("setElementWiseStride called: [" + elementWiseStride + "], buffer: " + buffer); buffer.put(length2 - 2, elementWiseStride); }
@Override public IComplexNDArray putImag(int rowIndex, int columnIndex, double value) { data.put(index(rowIndex, columnIndex) + 1 + offset(), value); return this; }
@Override public INDArray putScalarUnsafe(long offset, double value) { autoProcessScalarCall(); data().put(offset, value); return this; }
@Override public IComplexNDArray putReal(int rowIndex, int columnIndex, double value) { data.put(2 * index(rowIndex, columnIndex) + offset(), value); return this; }
/** * Inserts the element at the specified index * * @param i the index insert into * @param element a scalar ndarray * @return a scalar ndarray of the element at this index */ @Override public IComplexNDArray put(int i, IComplexNDArray element) { if (element == null) throw new IllegalArgumentException("Unable to insert null element"); assert element.isScalar() : "Unable to insert non scalar element"; long idx = linearIndex(i); IComplexNumber n = element.getComplex(0); data.put(idx, n.realComponent().doubleValue()); data.put(idx + 1, n.imaginaryComponent().doubleValue()); return this; }
@Override public DataBuffer dup() { DataBuffer ret = create(length); for (int i = 0; i < ret.length(); i++) ret.put(i, getDouble(i)); return ret; }
@Override public Buffer extraArgsBuff() { if (extraArgs != null) { DataBuffer retBuff; if (x.data().dataType() == DataBuffer.Type.FLOAT) { retBuff = Nd4j.createBuffer(new float[extraArgs.length]); for (int i = 0; i < extraArgs.length; i++) { Number val = (Number) extraArgs[i]; retBuff.put(i, val.floatValue()); } return retBuff.asNioFloat(); } else { retBuff = Nd4j.createBuffer(new double[extraArgs.length]); for (int i = 0; i < extraArgs.length; i++) { Number val = (Number) extraArgs[i]; retBuff.put(i, val.doubleValue()); } return retBuff.asNioDouble(); } } return null; }
@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 nextInt(long[] shape) { int length = ArrayUtil.prod(shape); INDArray ret = Nd4j.create(shape); DataBuffer data = ret.data(); for (int i = 0; i < length; i++) { data.put(i, nextInt()); } return ret; }
@Override public INDArray nextDouble(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, nextDouble()); } return ret; }
public INDArray putScalar(int row, int col, double value) { checkArgument(row < rows && 0 <= rows); checkArgument(col < columns && 0 <= columns); int idx = pointerB.getInt(row); int idxNextRow = pointerE.getInt(row); while (columnsPointers.getInt(idx) < col && columnsPointers.getInt(idx) < idxNextRow) { idx++; } if (columnsPointers.getInt(idx) == col) { values.put(idx, value); } else { //Add a new entry in both buffers at a given position values = addAtPosition(values, length, idx, value); columnsPointers = addAtPosition(columnsPointers, length, idx, col); length++; // shift the indices of the next rows pointerE.put(row, pointerE.getInt(row) + 1); for (int i = row + 1; i < rows; i++) { pointerB.put(i, pointerB.getInt(i) + 1); pointerE.put(i, pointerE.getInt(i) + 1); } } return this; }
@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; }
@Override public INDArray nextInt(int n, long[] shape) { int length = ArrayUtil.prod(shape); INDArray ret = Nd4j.create(shape); DataBuffer data = ret.data(); for (int i = 0; i < length; i++) { data.put(i, nextInt(n)); } return ret; }
@Override public INDArray putScalar(long row, long col, double value) { Nd4j.getCompressor().autoDecompress(this); autoProcessScalarCall(); if (rank() > 2) throw new IllegalStateException("Cannot use putScalar(int,int,double) on a rank " + rank() + " INDArray"); long offset = Shape.getOffsetUnsafe(javaShapeInformation, row, col); data.put(offset, value); return this; }
@Override public INDArray putScalar(long dim0, long dim1, long dim2, long dim3, double value) { Nd4j.getCompressor().autoDecompress(this); autoProcessScalarCall(); if (rank() != 4) throw new IllegalStateException( "Cannot use putScalar(int,int,int,int,double) on a rank " + rank() + " INDArray"); long offset = Shape.getOffsetUnsafe(javaShapeInformation, dim0, dim1, dim2, dim3); data.put(offset, value); return this; }
@Override public INDArray putScalar(long dim0, long dim1, long dim2, double value) { Nd4j.getCompressor().autoDecompress(this); autoProcessScalarCall(); if (rank() != 3) throw new IllegalStateException( "Cannot use putScalar(int,int,int,double) on a rank " + rank() + " INDArray"); long offset = 0; // Shape.getOffsetUnsafe(javaShapeInformation, dim0, dim1, dim2); long size_0 = javaShapeInformation[1]; long size_1 = javaShapeInformation[1 + 1]; long size_2 = javaShapeInformation[1 + 2]; if (size_0 != 1) offset += dim0 * javaShapeInformation[1 + 0 + 3]; if (size_1 != 1) offset += dim1 * javaShapeInformation[1 + 1 + 3]; if (size_2 != 1) offset += dim2 * javaShapeInformation[1 + 2 + 3]; data.put(offset, value); return this; }
@Override public INDArray putScalar(long i, double value) { if (i < 0) i += rank(); // TODO: i'm not sure that rank == 1 has fair shortcut here if (isScalar() || rank() == 1) { autoProcessScalarCall(); data.put(i, value); return this; } // we cant raise rank here, if original rank is 1 if (isRowVector() && rank() == 2) { return putScalar(0, i, value); } else if (isColumnVector() && rank() == 2) { return putScalar(i, 0, value); } long[] indexes = ordering() == 'c' ? Shape.ind2subC(this, i) : Shape.ind2sub(this, i); return putScalar(indexes, value); }