Java Code Examples for java.nio.DoubleBuffer#array()

The following examples show how to use java.nio.DoubleBuffer#array() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ChannelHelper.java    From libcommon with Apache License 2.0 6 votes vote down vote up
/**
 * ByteChannelからdouble配列を読み込む
 * @param channel
 * @return
 * @throws IOException
 */
public static double[] readDoubleArray(@NonNull final ByteChannel channel)
	throws IOException {
	
	final int n = readInt(channel);
	final ByteBuffer buf = ByteBuffer.allocate(n * 8).order(ByteOrder.BIG_ENDIAN);
	final int readBytes = channel.read(buf);
	if (readBytes != n * 8) throw new IOException();
	buf.clear();
	final DoubleBuffer result = buf.asDoubleBuffer();
	if (result.hasArray()) {
		return result.array();
	}  else {
		final double[] b = new double[n];
		result.get(b);
		return b;
	}
}
 
Example 2
Source File: ThriftBmiBridge.java    From OpenDA with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static double[] bufferToDoubleArray(ByteBuffer buffer) {
	buffer.order(ByteOrder.nativeOrder());
	DoubleBuffer doubles = buffer.asDoubleBuffer();

	if (doubles.hasArray()) {
		return doubles.array();
	} else {
		double[] resultArray = new double[doubles.capacity()];
		doubles.get(resultArray);
		return resultArray;
	}
}
 
Example 3
Source File: StoredDoubleTimeSeries.java    From powsybl-core with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public double[] toArray() {
    DoubleBuffer buffer = DoubleBuffer.allocate(metadata.getIndex().getPointCount());
    for (int i = 0; i < metadata.getIndex().getPointCount(); i++) {
        buffer.put(i, Double.NaN);
    }
    fillBuffer(buffer, 0);
    return buffer.array();
}
 
Example 4
Source File: TensorUtil.java    From jpmml-tensorflow with GNU Affero General Public License v3.0 5 votes vote down vote up
static
public double[] toDoubleArray(Tensor tensor){
	DoubleBuffer doubleBuffer = DoubleBuffer.allocate(tensor.numElements());

	tensor.writeTo(doubleBuffer);

	return doubleBuffer.array();
}
 
Example 5
Source File: DoublePointer.java    From tapir with MIT License 5 votes vote down vote up
/**
 * For direct buffers, calls {@link Pointer#Pointer(Buffer)}, while for buffers
 * backed with an array, allocates enough memory for the array and copies it.
 *
 * @param buffer the Buffer to reference or copy
 * @see #put(double[])
 */
public DoublePointer(DoubleBuffer buffer) {
    super(buffer);
    if (buffer != null && buffer.hasArray()) {
        double[] array = buffer.array();
        allocateArray(array.length);
        put(array);
        position(buffer.position());
        limit(buffer.limit());
    }
}
 
Example 6
Source File: DoubleTensorTypeSupport.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Override
public List<Field> createListField(Tensor<Double> tensor, DoubleBuffer doubleBuffer) {
  List<Field> fields = new ArrayList<>();
  tensor.writeTo(doubleBuffer);
  double[] doubles = doubleBuffer.array();
  for (double aDouble : doubles) {
    fields.add(Field.create(aDouble));
  }
  return fields;
}
 
Example 7
Source File: JTensor.java    From zoltar with Apache License 2.0 4 votes vote down vote up
/**
 * Create a new {@link JTensor} instance by extracting data from the underlying {@link Tensor} and
 * closing it afterwards.
 */
public static JTensor create(final Tensor<?> tensor) {
  final JTensor jt;
  try {
    switch (tensor.dataType()) {
      case STRING:
        if (tensor.numDimensions() == 0) {
          final String value = new String(tensor.bytesValue(), UTF_8);
          jt =
              new AutoValue_JTensor(
                  tensor.dataType(), tensor.numDimensions(), tensor.shape(), value);
        } else {
          final int[] dimensions = toIntExact(tensor.shape());
          final Object byteArray =
              tensor.copyTo(Array.newInstance(byte[].class, toIntExact(tensor.shape())));
          jt =
              new AutoValue_JTensor(
                  tensor.dataType(),
                  tensor.numDimensions(),
                  tensor.shape(),
                  toStringArray(byteArray, tensor.numElements(), dimensions));
        }
        break;
      case INT32:
        final IntBuffer intBuf = IntBuffer.allocate(tensor.numElements());
        tensor.writeTo(intBuf);
        jt =
            new AutoValue_JTensor(
                tensor.dataType(), tensor.numDimensions(), tensor.shape(), intBuf.array());
        break;
      case INT64:
        final LongBuffer longBuf = LongBuffer.allocate(tensor.numElements());
        tensor.writeTo(longBuf);
        jt =
            new AutoValue_JTensor(
                tensor.dataType(), tensor.numDimensions(), tensor.shape(), longBuf.array());
        break;
      case FLOAT:
        final FloatBuffer floatBuf = FloatBuffer.allocate(tensor.numElements());
        tensor.writeTo(floatBuf);
        jt =
            new AutoValue_JTensor(
                tensor.dataType(), tensor.numDimensions(), tensor.shape(), floatBuf.array());
        break;
      case DOUBLE:
        final DoubleBuffer doubleBuf = DoubleBuffer.allocate(tensor.numElements());
        tensor.writeTo(doubleBuf);
        jt =
            new AutoValue_JTensor(
                tensor.dataType(), tensor.numDimensions(), tensor.shape(), doubleBuf.array());
        break;
      case BOOL:
        final boolean[] array = new boolean[tensor.numElements()];
        tensor.copyTo(array);
        jt =
            new AutoValue_JTensor(
                tensor.dataType(), tensor.numDimensions(), tensor.shape(), array);
        break;
      default:
        throw new IllegalStateException("Unsupported data type " + tensor.dataType());
    }
  } finally {
    tensor.close();
  }

  return jt;
}
 
Example 8
Source File: CalculatedTimeSeries.java    From powsybl-core with Mozilla Public License 2.0 4 votes vote down vote up
@Override
public double[] toArray() {
    DoubleBuffer buffer = DoubleBuffer.allocate(metadata.getIndex().getPointCount());
    fillBuffer(buffer, 0);
    return buffer.array();
}