@Override public double[] toDoubleVector() { if(!isVector()) { throw new ND4JIllegalStateException("Unable to create a 1d array from a non vector!"); } return dup().data().asDouble(); }
@Override public void serialize(INDArray array, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { if (array.isView()) { array = array.dup(); } double[] dArr = array.data().asDouble(); jsonGenerator.writeObject(dArr); } }
static boolean Equal(INDArray arr1, INDArray arr2) { return ArrayUtil.equals(arr1.data().asFloat(), arr2.data().asDouble()); }
/** * Returns the double 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 buffer */ public static double[] getDoubleData(DataBuffer buf) { if (buf.allocationMode() == DataBuffer.AllocationMode.HEAP) return buf.asDouble(); else { double[] ret = new double[(int) buf.length()]; for (int i = 0; i < buf.length(); i++) ret[i] = buf.getDouble(i); return ret; } }
protected DataBuffer reallocate(DataBuffer buffer) { int newSize = (int) buffer.length() * 2; DataBuffer newBuffer = Nd4j.createBuffer(newSize); switch (buffer.dataType()) { case INT: newBuffer.setData(buffer.asInt()); break; case DOUBLE: newBuffer.setData(buffer.asDouble()); break; case FLOAT: newBuffer.setData(buffer.asFloat()); break; case HALF: //TODO throw new UnsupportedOperationException(); case COMPRESSED: //TODO throw new UnsupportedOperationException(); default: throw new UnsupportedOperationException(); } return newBuffer; }
/** * Returns the double data * for this ndarray. * 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 double[] getDoubleData(INDArray buf) { if (buf.data().dataType() != DataBuffer.Type.DOUBLE) throw new IllegalArgumentException("Double data must be obtained from a double buffer"); if (buf.data().allocationMode() == DataBuffer.AllocationMode.HEAP) { return buf.data().asDouble(); } else { double[] ret = new double[(int) buf.length()]; INDArray linear = buf.linearView(); for (int i = 0; i < buf.length(); i++) ret[i] = linear.getDouble(i); return ret; } }
setValueFor(fieldType,varArr.data().asDouble());
@Override public double[][] toDoubleMatrix() { if(!isMatrix()) { throw new ND4JIllegalStateException("Unable to create a 2d array from a non matrix!"); } if (this.rows() > Integer.MAX_VALUE || this.columns() > Integer.MAX_VALUE) throw new ND4JArraySizeException(); double[][] ret = new double[(int) rows()][(int) columns()]; for(int i = 0; i < ret.length; i++) { ret[i] = getRow(i).dup().data().asDouble(); } return ret; }
on.setValueFor(currentField,tensor.data().asDouble());
double[] doubles = fourByFiveRandomZeroToOne.data().asDouble(); System.out.println("Array doubles: " + Arrays.toString(doubles)); float[] floats = fourByFiveRandomZeroToOne.data().asFloat();
@Override public INDArray getFunctionValues(final INDArray x) { final double period = 6.0; final double[] xd = x.data().asDouble(); final double[] yd = new double[xd.length]; for (int i = 0; i < xd.length; i++) { yd[i] = Math.abs(2 * (xd[i] / period - Math.floor(xd[i] / period + 0.5))); } return Nd4j.create(yd, new int[]{xd.length, 1}); //Column vector }
@Override public INDArray getFunctionValues(final INDArray x) { final double sawtoothPeriod = 4.0; //the input data is the intervals at which the wave is being calculated final double[] xd2 = x.data().asDouble(); final double[] yd2 = new double[xd2.length]; for (int i = 0; i < xd2.length; i++) { //Using the sawtooth wave function, find the values at the given intervals yd2[i] = 2 * (xd2[i] / sawtoothPeriod - Math.floor(xd2[i] / sawtoothPeriod + 0.5)); } return Nd4j.create(yd2, new int[]{xd2.length, 1}); //Column vector }
@Override public double[] asFlatDoubleArray() { return tensor.dup().data().asDouble(); }
@Override public void serialize(INDArray array, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { if (array.isView()) { array = array.dup(); } double[] dArr = array.data().asDouble(); jsonGenerator.writeObject(dArr); } }
@Override protected void dgemm(char Order, char TransA, char TransB, int M, int N, int K, double alpha, INDArray A, int lda, INDArray B, int ldb, double beta, INDArray C, int ldc) { A = Shape.toOffsetZero(A); B = Shape.toOffsetZero(B); DataBuffer aData = A.data(); DataBuffer bData = B.data(); double[] cData = getDoubleData(C); BLAS.getInstance().dgemm(String.valueOf(TransA), String.valueOf(TransB), M, N, K, alpha, aData.asDouble(), getBlasOffset(A), lda, bData.asDouble(), getBlasOffset(B), ldb, beta, cData, getBlasOffset(C), ldc); setData(cData, C); }
@Override public DoubleTensor apply(Function<Double, Double> function) { DataBuffer data = tensor.data().dup(); for (int i = 0; i < data.length(); i++) { data.put(i, function.apply(data.getDouble(i))); } return new Nd4jDoubleTensor(data.asDouble(), this.getShape()); }
/** * Return the index of the max in the given ndarray * * @param x the ndarray to ge tthe max for * @return */ public static int iamax(IComplexNDArray x) { if (x.data().dataType() == DataBuffer.Type.FLOAT) return NativeBlas.icamax(x.length(), x.data().asFloat(), x.offset(), 1) - 1; else return NativeBlas.izamax(x.length(), x.data().asDouble(), x.offset(), 1) - 1; }
/** * @param x * @return */ public static double asum(IComplexNDArray x) { if (x.data().dataType() == DataBuffer.Type.FLOAT) return NativeBlas.scasum(x.length(), x.data().asFloat(), x.offset(), x.majorStride()); else return NativeBlas.dzasum(x.length(), x.data().asDouble(), x.offset(), x.majorStride()); }
/** * @param x * @return */ public static double asum(INDArray x) { if (x.data().dataType() == DataBuffer.Type.FLOAT) { float sum = BLAS.getInstance().sasum(x.length(), x.data().asFloat(), x.offset(), x.majorStride()); return sum; } else { double sum = BLAS.getInstance().dasum(x.length(), x.data().asDouble(), x.offset(), x.majorStride()); return sum; } }
/** * @param x * @return */ public static double asum(INDArray x) { if (x.data().dataType() == DataBuffer.Type.FLOAT) { float sum = BLAS.getInstance().sasum(x.length(), x.data().asFloat(), x.offset(), x.majorStride()); return sum; } else { double sum = BLAS.getInstance().dasum(x.length(), x.data().asDouble(), x.offset(), x.majorStride()); return sum; } }