/** * Returns a scalar (individual element) * of a scalar ndarray * * @return the individual item in this ndarray */ @Override public Object element() { if (!isScalar()) throw new IllegalStateException("Unable to retrieve element from non scalar matrix"); if (data.dataType() == DataBuffer.Type.FLOAT) return data.getFloat(0); return data.getDouble(0); }
@Override public void copyAtStride(DataBuffer buf, long n, long stride, long yStride, long offset, long yOffset) { if (dataType() == Type.FLOAT) { for (int i = 0; i < n; i++) { put(offset + i * stride, buf.getFloat(yOffset + i * yStride)); } } else { for (int i = 0; i < n; i++) { put(offset + i * stride, buf.getDouble(yOffset + i * yStride)); } } }
/** * Returns the float data * for this buffer. * If possible (the offset is 0 representing the whole buffer) * it will return a direct reference to the underlying array * @param buf the ndarray to get the data for * @return the double data for this ndarray */ public static float[] getFloatData(DataBuffer buf) { if (buf.allocationMode() == DataBuffer.AllocationMode.HEAP) { return buf.asFloat(); } else { float[] ret = new float[(int) buf.length()]; for (int i = 0; i < buf.length(); i++) ret[i] = buf.getFloat(i); return ret; } }
/** * 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; }
@Override public float getFloat(long i){ return array.data().getFloat(i); }
/** * Returns a scalar (individual element) * of a scalar ndarray * * @return the individual item in this ndarray */ @Override public Object element() { if (!isScalar()) throw new IllegalStateException("Unable to retrieve element from non scalar matrix"); if (data.dataType() == DataBuffer.Type.FLOAT) return data.getFloat(0); return data.getDouble(0); }
/** * Returns the float data * for this buffer. * If possible (the offset is 0 representing the whole buffer) * it will return a direct reference to the underlying array * @param buf the ndarray to get the data for * @return the double data for this ndarray */ public static float[] getFloatData(DataBuffer buf) { if (buf.allocationMode() == DataBuffer.AllocationMode.HEAP) { return buf.asFloat(); } else { float[] ret = new float[(int) buf.length()]; for (int i = 0; i < buf.length(); i++) ret[i] = buf.getFloat(i); return ret; } }