@Override public INDArray putScalar(int[] indexes, double value) { for (int i = 0; i < indexes.length; i++) { if (indexes[i] < 0) indexes[i] += rank(); } if (indexes.length == 1) { return putScalar(indexes[0], value); } if (indexes.length != rank) { throw new IllegalStateException( "Cannot use putScalar with indexes length " + indexes.length + " on rank " + rank); } addOrUpdate(ArrayUtil.toLongArray(indexes), value); return this; }
@Override public INDArray putRow(long row, INDArray toPut) { if (isRowVector() && toPut.isVector()) { return assign(toPut); } return put(new INDArrayIndex[] {NDArrayIndex.point(row), NDArrayIndex.all()}, toPut); }
/** * Count the number of value that are included in the ndarray (view) according to the sparse offsets and the shape * @return nnz * */ public long countNNZ() { long count = 0; for (int i = 0; i < values.length(); i++) { int[] idx = getUnderlyingIndicesOf(i).asInt(); boolean isIn = true; int idxNotFixed = 0; for (int dim = 0; dim < idx.length; dim++) { if (flags()[dim] == 1) { if (sparseOffsets()[dim] != idx[dim]) { isIn = false; break; } } else { int lowerBound = sparseOffsets()[dim]; long upperBound = sparseOffsets()[dim] + shape()[idxNotFixed]; if (!(idx[dim] >= lowerBound && idx[dim] < upperBound)) { isIn = false; break; } idxNotFixed++; } } count = isIn ? count + 1 : count; } return count; }
int[] extendedFlags = new int[underlyingRank()]; int notFixedDim = 0; for (int dim = 0; dim < underlyingRank(); dim++) { int[] temp = flags(); if (flags()[dim] == 0) { extendedFlags[dim] = viewFlags[notFixedDim]; notFixedDim++;
/** * Compute the sparse offsets of the view we are getting, for each dimension according to the original ndarray * @param offset the offset of the view * @return an int array containing the sparse offsets * */ private long[] createSparseOffsets(long offset) { // resolve the offsets in the view dimension int underlyingRank = sparseOffsets().length; long[] newOffsets = new long[rank()]; List<Long> shapeList = Longs.asList(shape()); int penultimate = rank() - 1; for (int i = 0; i < penultimate; i++) { long prod = ArrayUtil.prodLong(shapeList.subList(i + 1, rank())); newOffsets[i] = offset / prod; offset = offset - newOffsets[i] * prod; } newOffsets[rank() - 1] = offset % shape()[rank() - 1]; // Merge the offsets with the original sparseOffsets long[] finalOffsets = new long[underlyingRank]; int dimNotFixed = 0; for (int dim = 0; dim < underlyingRank; dim++) { if (flags()[dim] == 1) { finalOffsets[dim] = sparseOffsets()[dim]; } else { finalOffsets[dim] = newOffsets[dimNotFixed] + sparseOffsets()[dim]; dimNotFixed++; } } return finalOffsets; }
public INDArray get(INDArrayIndex... indexes) { sort(); if (indexes.length == 1 && indexes[0] instanceof NDArrayIndexAll || (indexes.length == 2 && (isRowVector() && indexes[0] instanceof PointIndex && indexes[0].offset() == 0 && indexes[1] instanceof NDArrayIndexAll || isColumnVector() && indexes[1] instanceof PointIndex && indexes[0].offset() == 0 && indexes[0] instanceof NDArrayIndexAll))) return this; indexes = NDArrayIndex.resolve(shapeInfoDataBuffer(), indexes); ShapeOffsetResolution resolution = new ShapeOffsetResolution(this); resolution.exec(indexes); INDArray ret = Nd4j.createSparseCOO(new double[] {}, new int[][] {}, shape); int count = 0; int maxValue = ArrayUtil.prod(shape()); while (count < maxValue) { try { if (!isZero(idx)) { double val = getDouble(idx); ret.putScalar(filterOutFixedDimensions(resolution.getFixed(), cooIdx), val); INDArray ret = subArray(resolution); return ret;
@Override public INDArray subArray(ShapeOffsetResolution resolution) { long[] offsets = resolution.getOffsets(); int[] shape = LongUtils.toInts(resolution.getShapes()); int[] stride = LongUtils.toInts(resolution.getStrides()); int[] flags = resolution.getFixed(); flags = updateFlags(flags, shape); long offset = (int) (offset() + resolution.getOffset()); int newRank = shape.length; long[] sparseOffsets = createSparseOffsets(offset); int[] newAxis = createHiddenDimensions(resolution.getPrependAxis()); if (offset() + resolution.getOffset() >= Integer.MAX_VALUE) throw new IllegalArgumentException("Offset of array can not be >= Integer.MAX_VALUE"); if (offsets.length != newRank) throw new IllegalArgumentException("Invalid offset " + Arrays.toString(offsets)); if (stride.length != newRank) throw new IllegalArgumentException("Invalid stride " + Arrays.toString(stride)); if (shape.length == rank() && Shape.contentEquals(shape, shapeOf())) { if (ArrayUtil.isZero(offsets)) { return this; } else { throw new IllegalArgumentException("Invalid subArray offsets"); } } DataBuffer newSparseInformation = Nd4j.getSparseInfoProvider().createSparseInformation(flags, sparseOffsets, newAxis, underlyingRank()); return create(values, indices, newSparseInformation, Arrays.copyOf(shape, shape.length)); }
@Override public INDArray putScalar(long i, double value) { if (i < 0) i += rank(); if (isScalar()) { if (Nd4j.getExecutioner().getProfilingMode() != OpExecutioner.ProfilingMode.DISABLED && Nd4j.getExecutioner().getProfilingMode() != OpExecutioner.ProfilingMode.SCOPE_PANIC) OpProfiler.getInstance().processScalarCall(); addOrUpdate(new long[] {0, 0}, value); return this; } if (isRowVector()) { addOrUpdate(new long[] {0, i}, value); return this; } else if (isColumnVector()) { addOrUpdate(new long[] {i, 0}, value); return this; } long[] indexes = ordering() == 'c' ? Shape.ind2subC(this, i) : Shape.ind2sub(this, i); return putScalar(indexes, value); }
long[] physicalIndexes = isView() ? translateToPhysical(indexes) : indexes; long[] idx = getUnderlyingIndicesOf(i).asLong(); if (Arrays.equals(idx, physicalIndexes)) { removeEntry(i); length--; } else { while (!canInsert(values, 1)) { long size = (long) Math.ceil((values.capacity() * THRESHOLD_MEMORY_ALLOCATION)); values.reallocate(size); while (!canInsert(indices, physicalIndexes.length)) { long size = (long) Math.ceil((indices.capacity() * THRESHOLD_MEMORY_ALLOCATION)); indices.reallocate(size); indices.put(length * rank() + i, physicalIndexes[i]);
@Override public double getDouble(long i) { if (i >= length()) { throw new IllegalArgumentException("Unable to get linear index >= " + length()); } if (Nd4j.getExecutioner().getProfilingMode() != OpExecutioner.ProfilingMode.DISABLED && Nd4j.getExecutioner().getProfilingMode() != OpExecutioner.ProfilingMode.SCOPE_PANIC) OpProfiler.getInstance().processScalarCall(); 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 putColumn(int column, INDArray toPut) { if (isColumnVector() && toPut.isVector()) { return assign(toPut); } return put(new INDArrayIndex[] {NDArrayIndex.all(), NDArrayIndex.point(column)}, toPut); }
public BaseSparseNDArrayCOO(DataBuffer values, DataBuffer indices, DataBuffer sparseInformation, int[] shape) { this.values = Nd4j.createBuffer(values, 0, values.length()); this.indices = indices; setShapeInformation(Nd4j.getShapeInfoProvider().createShapeInformation(shape)); init(shape); this.sparseInformation = sparseInformation; this.length = countNNZ(); }
/** * Converts the sparse ndarray into a dense one * @return a dense ndarray */ @Override public INDArray toDense() { // TODO support view conversion INDArray result = Nd4j.zeros(shape()); switch (data().dataType()) { case DOUBLE: for (int i = 0; i < length; i++) { int[] idx = getUnderlyingIndicesOf(i).asInt(); double value = values.getDouble(i); result.putScalar(idx, value); } break; case FLOAT: for (int i = 0; i < length; i++) { int[] idx = getUnderlyingIndicesOf(i).asInt(); float value = values.getFloat(i); result.putScalar(idx, value); } break; default: throw new UnsupportedOperationException(); } return result; }
/** * Return if the dimension in argument is a fixed dimension. * */ public boolean isDimensionFixed(int i) { return flags()[i] == 1; }
@Override public INDArray put(int[] indexes, INDArray element) { if (!element.isScalar()) throw new IllegalArgumentException("Unable to insert anything but a scalar"); if (indexes.length != rank) throw new IllegalStateException( "Cannot use putScalar with indexes length " + indexes.length + " on rank " + rank); addOrUpdate(ArrayUtil.toLongArray(indexes), element.getDouble(0)); return this; }
@Override public boolean isView() { return Shape.offset(shapeInformation) > 0 || data().originalDataBuffer() != null; // TODO or if sparseOffset/flags != [0, ..,0] }
if (isScalar()) { return Nd4j.createBuffer(new int[] {0, 0}); boolean isIn = true; int idxNotFixed = 0; int[] idx = getUnderlyingIndicesOf(i).asInt(); // TODO change for getIndicesOf(i) if (flags()[dim] == 1) { if (sparseOffsets()[dim] != idx[dim]) { isIn = false; break; int lowerBound = sparseOffsets()[dim]; long upperBound = sparseOffsets()[dim] + shape()[idxNotFixed]; if (!(idx[dim] >= lowerBound && idx[dim] < upperBound)) { isIn = false; int notFixedDim = 0; for (int dim = 0; dim < idx.length; dim++) { if (flags()[dim] == 0) { if (shape()[notFixedDim] == 1) { ind.add(0); notFixedDim++; } else { ind.add(idx[dim] - sparseOffsets()[dim]);
boolean isIn = true; int idxNotFixed = 0; int[] idx = getUnderlyingIndicesOf(i).asInt(); for (int dim = 0; dim < idx.length; dim++) { if (flags()[dim] == 1) { if (sparseOffsets()[dim] != idx[dim]) { isIn = false; break; int lowerBound = sparseOffsets()[dim]; long upperBound = sparseOffsets()[dim] + shape()[idxNotFixed]; if (!(idx[dim] >= lowerBound && idx[dim] < upperBound)) { isIn = false;