@Override public double getDoubleUnsafe(long offset) { return data().getDouble(offset); }
/** * Returns the start of where the ndarray is for the original data buffer * * @return */ @Override public long originalOffset() { if (data().originalOffset() >= Integer.MAX_VALUE) throw new IllegalArgumentException("Original offset of buffer can not be >= Integer.MAX_VALUE"); return data().originalOffset(); }
@Override public INDArray putScalarUnsafe(long offset, double value) { autoProcessScalarCall(); data().put(offset, value); return this; }
protected INDArray newShape(long[] newShape, char ordering) { return Nd4j.create(data(), newShape, stride(), 0, ordering); }
@Override public boolean isView() { /* We don't really use Shape offset value anywhere And it's possible to be not a view, and have non-empty originalBuffer */ // length/data.length can be different in case of Threshold conversion return Shape.offset(javaShapeInformation) > 0 || (length() < data().length() && data.dataType() != DataBuffer.Type.INT) || data().originalDataBuffer() != null; }
protected INDArray createScalarForIndex(long i, boolean applyOffset) { if(isVector()) return getScalar(i); return Nd4j.create(data(), new long[] {1, 1}, new long[] {1, 1}, i); }
@Override public int toFlatArray(FlatBufferBuilder builder) { int shape = FlatArray.createShapeVector(builder, this.shapeInfoDataBuffer().asLong()); int buffer = FlatArray.createBufferVector(builder, this.data().asBytes()); int array = FlatArray.createFlatArray(builder, shape, buffer, SameDiff.getDataTypeAsByte(this.data().dataType()), ByteOrder.BE); return array; }
@Override public INDArray subArray(long[] offsets, int[] shape, int[] stride) { Nd4j.getCompressor().autoDecompress(this); int n = shape.length; // FIXME: shapeInfo should be used here if (shape.length < 1) return create(Nd4j.createBufferDetached(shape)); if (offsets.length != n) throw new IllegalArgumentException("Invalid offset " + Arrays.toString(offsets)); if (stride.length != n) throw new IllegalArgumentException("Invalid stride " + Arrays.toString(stride)); if (Shape.contentEquals(shape, shapeOf())) { if (ArrayUtil.isZero(offsets)) { return this; } else { throw new IllegalArgumentException("Invalid subArray offsets"); } } long[] dotProductOffsets = offsets; int[] dotProductStride = stride; long offset = Shape.offset(javaShapeInformation) + NDArrayIndex.offset(dotProductStride, dotProductOffsets); if (offset >= data().length()) offset = ArrayUtil.sumLong(offsets); return create(data, Arrays.copyOf(shape, shape.length), stride, offset, ordering()); }
@Override public INDArray dup() { WorkspaceUtils.assertValidArray(this, "Cannot duplicate INDArray"); if (this.isCompressed() && this.ordering() == Nd4j.order().charValue()) { INDArray ret = Nd4j.createArrayFromShapeBuffer(data().dup(), this.shapeInfoDataBuffer()); ret.markAsCompressed(true); return ret; } Nd4j.getCompressor().autoDecompress(this); INDArray ret = Shape.toOffsetZeroCopy(this); return ret; }
protected void write(ObjectOutputStream out) throws IOException { if (this.isView()) { //As per Nd4j.write, duplicate before writing to the output stream //BaseDataBuffer.write(...) doesn't know about strides etc, so dup (or equiv. strategy) is necessary here //Furthermore, because we only want to save the *actual* data for a view (not the full data), the shape info // (mainly strides, offset, element-wise stride) may be different in the duped array vs. the view array INDArray copy = this.dup(); copy.shapeInfoDataBuffer().write(out); copy.data().write(out); } else { shapeInformation.write(out); data().write(out); } }
@Override public double getDouble(long i) { Nd4j.getCompressor().autoDecompress(this); if (i >= length()) { throw new IllegalArgumentException("Unable to get linear index >= " + length()); } autoProcessScalarCall(); if (i == 0) return data().getDouble(i); long[] dimensions = ordering() == 'c' ? Shape.ind2subC(this, i) : Shape.ind2sub(this, i); Shape.assertShapeLessThan(dimensions, shape()); return getDouble(dimensions); }
@Override public INDArray dup(char order) { WorkspaceUtils.assertValidArray(this, "Cannot duplicate INDArray"); if (this.isCompressed() && this.ordering() == order) { INDArray ret = Nd4j.createArrayFromShapeBuffer(data().dup(), this.shapeInfoDataBuffer()); ret.markAsCompressed(true); return ret; } Nd4j.getCompressor().autoDecompress(this); return Shape.toOffsetZeroCopy(this, order); }
protected void read(ObjectInputStream s) { shapeInformation = Nd4j.createBuffer(new int[Shape.shapeInfoLength(rank())], 0); shapeInformation.read(s); setShapeInformation(Pair.create(shapeInformation, shapeInformation.asLong())); data = Nd4j.createBuffer(length(), false); data().read(s); }
@Override public INDArray convertToFloats() { if (data.dataType() == DataBuffer.Type.FLOAT) return this; val factory = Nd4j.getNDArrayFactory(); val buffer = Nd4j.createBuffer(new long[]{this.length()}, DataBuffer.Type.FLOAT); factory.convertDataEx(convertType(data.dataType()), this.data().addressPointer(), DataBuffer.TypeEx.FLOAT, buffer.addressPointer(), buffer.length()); return Nd4j.createArrayFromShapeBuffer(buffer, this.shapeInformation); }
@Override public INDArray convertToDoubles() { if (data.dataType() == DataBuffer.Type.DOUBLE) return this; val factory = Nd4j.getNDArrayFactory(); val buffer = Nd4j.createBuffer(new long[]{this.length()}, DataBuffer.Type.DOUBLE); factory.convertDataEx(convertType(data.dataType()), this.data().addressPointer(), DataBuffer.TypeEx.DOUBLE, buffer.addressPointer(), buffer.length()); return Nd4j.createArrayFromShapeBuffer(buffer, this.shapeInformation); }
@Override public double getDouble(long... indices) { autoProcessScalarCall(); Nd4j.getCompressor().autoDecompress(this); for (int i = 0; i < indices.length; i++) { if (indices[i] < 0) indices[i] += rank(); } if (indices.length == 1) { if (rank() == 1) return Shape.getDouble(this, indices[0]); else if (isRowVector()) return Shape.getDouble(this, 0, indices[0]); else if (isColumnVector()) return Shape.getDouble(this, indices[0], 0); else if (isScalar() && indices[0] == 0) return data().getDouble(0); else throw new IllegalStateException("Indexes length must be > 1 for non vectors and scalars"); } return Shape.getDouble(this, indices); }
/** * Returns the elements at the the specified indices * * @param indices the indices to get * @return the array with the specified elements */ @Override public double getDouble(int... indices) { autoProcessScalarCall(); Nd4j.getCompressor().autoDecompress(this); for (int i = 0; i < indices.length; i++) { if (indices[i] < 0) indices[i] += rank(); } if (indices.length == 1) { if (rank() == 1) return Shape.getDouble(this, indices[0]); else if (isRowVector()) return Shape.getDouble(this, 0, indices[0]); else if (isColumnVector()) return Shape.getDouble(this, indices[0], 0); else if (isScalar() && indices[0] == 0) return data().getDouble(0); else throw new IllegalStateException("Indexes length must be > 1 for non vectors and scalars"); } return Shape.getDouble(this, indices); }
/** * See: http://www.mathworks.com/help/matlab/ref/permute.html * * @param rearrange the dimensions to swap to * @return the newly permuted array */ @Override public INDArray permute(int... rearrange) { Nd4j.getCompressor().autoDecompress(this); if (rearrange.length != rank()) return dup(); boolean alreadyInOrder = true; //IntBuffer shapeInfo = shapeInfo(); int rank = Shape.rank(javaShapeInformation); for (int i = 0; i < rank; i++) { if (rearrange[i] != i) { alreadyInOrder = false; break; } } if (alreadyInOrder) return this; checkArrangeArray(rearrange); int[] newShape = doPermuteSwap(shapeOf(), rearrange); int[] newStride = doPermuteSwap(strideOf(), rearrange); char newOrder = Shape.getOrder(newShape, newStride, elementStride()); INDArray value = create(data(), newShape, newStride, offset(), newOrder); return value; }
if (this.data() == vector.data()) vector = vector.dup(); switch (operation) {