INDArray ret = Nd4j.createSparseCOO(new double[] {}, new int[][] {}, shape); int count = 0; int maxValue = ArrayUtil.prod(shape()); while (count < maxValue) { try {
/** * 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; }
long upperBound = sparseOffsets()[dim] + shape()[idxNotFixed]; if (!(idx[dim] >= lowerBound && idx[dim] < upperBound)) { isIn = false;
/** * 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; }
@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); }
/** * 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; }