public boolean isZero(int... indexes) { for (int i = 0; i < length(); i++) { int[] idx = getUnderlyingIndicesOf(i).asInt(); if (Arrays.equals(idx, translateToPhysical(indexes))) { return false; } } return true; }
/** * Return the position of the idx array into the indexes buffer between the lower and upper bound. * @param idx a set of coordinates * @param lowerBound the lower bound of the position * @param upperBound the upper bound of the position * @return the position of the idx array into the indexes buffers, which corresponds to the position of * the corresponding value in the values data. * */ public int indexesBinarySearch(int lowerBound, int upperBound, int[] idx) { int min = lowerBound; int max = upperBound; int mid = (max + min) / 2; int[] midIdx = getUnderlyingIndicesOf(mid).asInt(); if (Arrays.equals(idx, midIdx)) { return mid; } if (ArrayUtil.lessThan(idx, midIdx)) { max = mid; } if (ArrayUtil.greaterThan(idx, midIdx)) { min = mid; } if (min == max) { return -1; } return indexesBinarySearch(min, max, idx); }
/** * Returns the indices of non-zero element of the vector * * @return indices in Databuffer * */ @Override public DataBuffer getVectorCoordinates() { int idx; if (isRowVector()) { idx = 1; } else if (isColumnVector()) { idx = 0; } else { throw new UnsupportedOperationException(); } // FIXME: int cast int[] temp = new int[(int) length()]; for (int i = 0; i < length(); i++) { temp[i] = getUnderlyingIndicesOf(i).getInt(idx); } return Nd4j.createBuffer(temp); }
/** * 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; }
boolean isIn = true; int idxNotFixed = 0; int[] idx = getUnderlyingIndicesOf(i).asInt(); for (int dim = 0; dim < idx.length; dim++) { if (flags()[dim] == 1) {
/** * 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; }
long[] idx = getUnderlyingIndicesOf(i).asLong(); if (Arrays.equals(idx, physicalIndexes)) {
boolean isIn = true; int idxNotFixed = 0; int[] idx = getUnderlyingIndicesOf(i).asInt(); // TODO change for getIndicesOf(i)