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

The following examples show how to use java.nio.DoubleBuffer#allocate() . 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: BufferUtils.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Creates a new DoubleBuffer with the same contents as the given
 * DoubleBuffer. The new DoubleBuffer is separate from the old one and
 * changes are not reflected across. If you want to reflect changes,
 * consider using Buffer.duplicate().
 *
 * @param buf
 *            the DoubleBuffer to copy
 * @return the copy
 */
public static DoubleBuffer clone(DoubleBuffer buf) {
    if (buf == null) {
        return null;
    }
    buf.rewind();

    DoubleBuffer copy;
    if (isDirect(buf)) {
        copy = createDoubleBuffer(buf.limit());
    } else {
        copy = DoubleBuffer.allocate(buf.limit());
    }
    copy.put(buf);

    return copy;
}
 
Example 2
Source File: BufferUtils.java    From Ultraino with MIT License 6 votes vote down vote up
/**
 * Creates a new DoubleBuffer with the same contents as the given
 * DoubleBuffer. The new DoubleBuffer is separate from the old one and
 * changes are not reflected across. If you want to reflect changes,
 * consider using Buffer.duplicate().
 *
 * @param buf
 *            the DoubleBuffer to copy
 * @return the copy
 */
public static DoubleBuffer clone(DoubleBuffer buf) {
    if (buf == null) {
        return null;
    }
    buf.rewind();

    DoubleBuffer copy;
    if (isDirect(buf)) {
        copy = createDoubleBuffer(buf.limit());
    } else {
        copy = DoubleBuffer.allocate(buf.limit());
    }
    copy.put(buf);

    return copy;
}
 
Example 3
Source File: BufferUtils.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Creates a new DoubleBuffer with the same contents as the given
 * DoubleBuffer. The new DoubleBuffer is seperate from the old one and
 * changes are not reflected across. If you want to reflect changes,
 * consider using Buffer.duplicate().
 *
 * @param buf
 *            the DoubleBuffer to copy
 * @return the copy
 */
public static DoubleBuffer clone(DoubleBuffer buf) {
    if (buf == null) {
        return null;
    }
    buf.rewind();

    DoubleBuffer copy;
    if (buf.isDirect()) {
        copy = createDoubleBuffer(buf.limit());
    } else {
        copy = DoubleBuffer.allocate(buf.limit());
    }
    copy.put(buf);

    return copy;
}
 
Example 4
Source File: Spidee.java    From Robot-Overlord-App with GNU General Public License v2.0 6 votes vote down vote up
void Draw_Body(GL2 gl2) {
	gl2.glPushMatrix();

	DoubleBuffer m=DoubleBuffer.allocate(16);

	m.put( 0,-body.left.x);
	m.put( 1,-body.left.y);
	m.put( 2,-body.left.z);
	m.put( 4,body.forward.x);
	m.put( 5,body.forward.y);
	m.put( 6,body.forward.z);
	m.put( 8,body.up.x);
	m.put( 9,body.up.y);
	m.put(10,body.up.z);
	m.put(15,1);

	matBody.render(gl2);
    gl2.glTranslated(body.pos.x + 7.5f * body.up.x,
                 body.pos.y + 7.5f * body.up.y,
                 body.pos.z + 7.5f * body.up.z );
    gl2.glMultMatrixd(m);
    gl2.glRotatef(180,0,1,0);
    modelBody.render(gl2);

    gl2.glPopMatrix();
}
 
Example 5
Source File: TestableModel.java    From vespa with Apache License 2.0 5 votes vote down vote up
private org.tensorflow.Tensor<?> tensorFlowDoubleInputArgument(int d0Size, int d1Size) {
    DoubleBuffer fb1 = DoubleBuffer.allocate(d0Size * d1Size);
    int i = 0;
    for (int d0 = 0; d0 < d0Size; d0++)
        for (int d1 = 0; d1 < d1Size; ++d1)
            fb1.put(i++, (float)(d1 * 1.0 / d1Size));
    return org.tensorflow.Tensor.create(new long[]{ d0Size, d1Size }, fb1);
}
 
Example 6
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 7
Source File: DoubleDataChunkTest.java    From powsybl-core with Mozilla Public License 2.0 5 votes vote down vote up
@Test
public void baseTest() throws IOException {
    UncompressedDoubleDataChunk chunk = new UncompressedDoubleDataChunk(1, new double[] {1d, 2d, 3d});
    assertEquals(1, chunk.getOffset());
    assertEquals(3, chunk.getLength());
    assertArrayEquals(new double[]{1d, 2d, 3d}, chunk.getValues(), 0d);
    assertEquals(24, chunk.getEstimatedSize());
    assertFalse(chunk.isCompressed());
    assertEquals(1d, chunk.getCompressionFactor(), 0d);
    DoubleBuffer buffer = DoubleBuffer.allocate(4);
    for (int i = 0; i < 4; i++) {
        buffer.put(i, Double.NaN);
    }
    chunk.fillBuffer(buffer, 0);
    assertArrayEquals(new double[] {Double.NaN, 1d, 2d, 3d}, buffer.array(), 0d);
    String jsonRef = String.join(System.lineSeparator(),
            "{",
            "  \"offset\" : 1,",
            "  \"values\" : [ 1.0, 2.0, 3.0 ]",
            "}");
    assertEquals(jsonRef, JsonUtil.toJson(chunk::writeJson));
    RegularTimeSeriesIndex index = RegularTimeSeriesIndex.create(Interval.parse("2015-01-01T00:00:00Z/2015-01-01T00:45:00Z"),
                                                                 Duration.ofMinutes(15));
    assertEquals(ImmutableList.of(new DoublePoint(1, Instant.parse("2015-01-01T00:15:00Z").toEpochMilli(), 1d),
                                  new DoublePoint(2, Instant.parse("2015-01-01T00:30:00Z").toEpochMilli(), 2d),
                                  new DoublePoint(3, Instant.parse("2015-01-01T00:45:00Z").toEpochMilli(), 3d)),
            chunk.stream(index).collect(Collectors.toList()));
}
 
Example 8
Source File: NDManager.java    From djl with Apache License 2.0 5 votes vote down vote up
/**
 * Creates and initializes a 2D {@link NDArray}.
 *
 * @param data the float array that needs to be set
 * @return a new instance of {@link NDArray}
 */
default NDArray create(double[][] data) {
    DoubleBuffer buffer = DoubleBuffer.allocate(data.length * data[0].length);
    for (double[] d : data) {
        buffer.put(d);
    }
    buffer.rewind();
    return create(buffer, new Shape(data.length, data[0].length));
}
 
Example 9
Source File: DoubleDataChunkTest.java    From powsybl-core with Mozilla Public License 2.0 4 votes vote down vote up
@Test
public void compressTest() throws IOException {
    UncompressedDoubleDataChunk chunk = new UncompressedDoubleDataChunk(1, new double[] {1d, 2d, 2d, 2d, 2d, 3d});
    DoubleDataChunk maybeCompressedChunk = chunk.tryToCompress();
    assertTrue(maybeCompressedChunk instanceof CompressedDoubleDataChunk);
    CompressedDoubleDataChunk compressedChunk = (CompressedDoubleDataChunk) maybeCompressedChunk;
    assertEquals(1, compressedChunk.getOffset());
    assertEquals(6, compressedChunk.getLength());
    assertTrue(compressedChunk.isCompressed());
    assertEquals(36, compressedChunk.getEstimatedSize());
    assertEquals(36d / 48, compressedChunk.getCompressionFactor(), 0d);
    assertArrayEquals(new double[] {1d, 2d, 3d}, compressedChunk.getStepValues(), 0d);
    assertArrayEquals(new int[] {1, 4, 1}, compressedChunk.getStepLengths());
    DoubleBuffer buffer = DoubleBuffer.allocate(7);
    for (int i = 0; i < 7; i++) {
        buffer.put(i, Double.NaN);
    }
    compressedChunk.fillBuffer(buffer, 0);
    assertArrayEquals(new double[] {Double.NaN, 1d, 2d, 2d, 2d, 2d, 3d}, buffer.array(), 0d);

    // json test
    String jsonRef = String.join(System.lineSeparator(),
            "{",
            "  \"offset\" : 1,",
            "  \"uncompressedLength\" : 6,",
            "  \"stepValues\" : [ 1.0, 2.0, 3.0 ],",
            "  \"stepLengths\" : [ 1, 4, 1 ]",
            "}");
    assertEquals(jsonRef, JsonUtil.toJson(compressedChunk::writeJson));

    // test json with object mapper
    ObjectMapper objectMapper = JsonUtil.createObjectMapper()
            .registerModule(new TimeSeriesJsonModule());

    List<DoubleDataChunk> chunks = objectMapper.readValue(objectMapper.writeValueAsString(Arrays.asList(chunk, compressedChunk)),
                                                           TypeFactory.defaultInstance().constructCollectionType(List.class, DoubleDataChunk.class));
    assertEquals(2, chunks.size());
    assertEquals(chunk, chunks.get(0));
    assertEquals(compressedChunk, chunks.get(1));

    // check base class (DataChunk) deserializer
    assertTrue(objectMapper.readValue(objectMapper.writeValueAsString(chunk), DataChunk.class) instanceof DoubleDataChunk);

    // stream test
    RegularTimeSeriesIndex index = RegularTimeSeriesIndex.create(Interval.parse("2015-01-01T00:00:00Z/2015-01-01T01:30:00Z"),
                                                                 Duration.ofMinutes(15));
    assertEquals(ImmutableList.of(new DoublePoint(1, Instant.parse("2015-01-01T00:15:00Z").toEpochMilli(), 1d),
                                  new DoublePoint(2, Instant.parse("2015-01-01T00:30:00Z").toEpochMilli(), 2d),
                                  new DoublePoint(6, Instant.parse("2015-01-01T01:30:00Z").toEpochMilli(), 3d)),
                 compressedChunk.stream(index).collect(Collectors.toList()));
}
 
Example 10
Source File: DoubleTensorTypeSupport.java    From datacollector with Apache License 2.0 4 votes vote down vote up
@Override
public DoubleBuffer allocateBuffer(long[] shape) {
  return DoubleBuffer.allocate(calculateCapacityForShape(shape));
}
 
Example 11
Source File: BufferTools.java    From OSPREY3 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public DoubleBuffer makeDouble(int size) {
	return DoubleBuffer.allocate(size);
}
 
Example 12
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();
}
 
Example 13
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 14
Source File: DoubleNioDataBufferTest.java    From java with Apache License 2.0 4 votes vote down vote up
@Override
protected DoubleDataBuffer allocate(long size) {
  return new DoubleNioDataBuffer(DoubleBuffer.allocate((int)size));
}
 
Example 15
Source File: BufferArray.java    From openjdk-jdk9 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Constructor
 * @param size initial size
 */
public BufferArray(final int size) {
    buf = DoubleBuffer.allocate(size);
}
 
Example 16
Source File: BufferArray.java    From openjdk-jdk8u with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Constructor
 * @param size initial size
 */
public BufferArray(final int size) {
    buf = DoubleBuffer.allocate(size);
}
 
Example 17
Source File: BufferArray.java    From jdk8u60 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Constructor
 * @param size initial size
 */
public BufferArray(final int size) {
    buf = DoubleBuffer.allocate(size);
}
 
Example 18
Source File: BufferArray.java    From hottub with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Constructor
 * @param size initial size
 */
public BufferArray(final int size) {
    buf = DoubleBuffer.allocate(size);
}
 
Example 19
Source File: BufferArray.java    From jdk8u_nashorn with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Constructor
 * @param size initial size
 */
public BufferArray(final int size) {
    buf = DoubleBuffer.allocate(size);
}
 
Example 20
Source File: BufferArray.java    From TencentKona-8 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Constructor
 * @param size initial size
 */
public BufferArray(final int size) {
    buf = DoubleBuffer.allocate(size);
}