> scalars();
+ /**
+ * Returns a new N-dimensional view of this array with the given {@code shape}.
+ *
+ * The provided {@code shape} must comply to the following characteristics:
+ *
+ * - new shape is known (i.e. has no unknown dimension)
+ * - new shape size is equal to the size of the current shape (i.e. same number of elements)
+ *
+ * For example,
+ * {@code
+ * NdArrays.ofInts(Shape.scalar()).withShape(Shape.of(1, 1)); // ok
+ * NdArrays.ofInts(Shape.of(2, 3).withShape(Shape.of(3, 2)); // ok
+ * NdArrays.ofInts(Shape.scalar()).withShape(Shape.of(1, 2)); // not ok, sizes are different (1 != 2)
+ * NdArrays.ofInts(Shape.of(2, 3)).withShape(Shape.unknown()); // not ok, new shape unknown
+ * }
+ *
+ * Any changes applied to the returned view affect the data of this array as well, as there
+ * is no copy involved.
+ *
+ * @param shape the new shape to apply
+ * @return a new array viewing the data according to the new shape, or this array if shapes are the same
+ * @throws IllegalArgumentException if the provided {@code shape} is not compliant
+ * @throws UnsupportedOperationException if this array does not support this operation
+ */
+ NdArray withShape(Shape shape);
+
/**
* Creates a multi-dimensional view (or slice) of this array by mapping one or more dimensions
* to the given index selectors.
@@ -229,6 +258,18 @@ public interface NdArray extends Shaped {
*/
NdArray setObject(T value, long... coordinates);
+ /**
+ * Retrieve all scalar values of this array as a stream of objects.
+ *
+ * For {@code rank() > 1} arrays, all vectors of the last dimension are collated so that the scalar values are
+ * returned in sequential order.
+ *
+ * @return scalar values as a stream
+ */
+ default Stream streamOfObjects() {
+ return StreamSupport.stream(scalars().spliterator(), false).map(NdArray::getObject);
+ }
+
/**
* Copy the content of this array to the destination array.
*
@@ -244,33 +285,39 @@ public interface NdArray extends Shaped {
NdArray copyTo(NdArray dst);
/**
- * Read the content of this N-dimensional array into the destination buffer.
+ * Copy the content of this N-dimensional array into the destination buffer.
*
* The size of the buffer must be equal or greater to the {@link #size()} of this
* array, or an exception is thrown. After the copy, content of the buffer and of the array can be
* altered independently, without affecting each other.
*
+ *
Note: in version 0.4.0 and earlier, this method was named {@code read(DataBuffer)}. It has been renamed to
+ * explicitly indicate the direction of the data flow to avoid confusion.
+ *
* @param dst the destination buffer
* @return this array
* @throws java.nio.BufferOverflowException if the buffer cannot hold the content of this array
* @see DataBuffer#size()
*/
- NdArray read(DataBuffer dst);
+ NdArray copyTo(DataBuffer dst);
/**
- * Write the content of this N-dimensional array from the source buffer.
+ * Copy the content of the source buffer into this N-dimensional array.
*
* The size of the buffer must be equal or greater to the {@link #size()} of this
* array, or an exception is thrown. After the copy, content of the buffer and of the array can be
* altered independently, without affecting each other.
*
+ *
Note: in version 0.4.0 and earlier, this method was named {@code write(DataBuffer)}. It has been renamed to
+ * explicitly indicate the direction of the data flow to avoid confusion.
+ *
* @param src the source buffer
* @return this array
* @throws java.nio.BufferUnderflowException if the buffer has not enough remaining data to write
* into this array
* @see DataBuffer#size()
*/
- NdArray write(DataBuffer src);
+ NdArray copyFrom(DataBuffer src);
/**
* Checks equality between n-dimensional arrays.
diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/ShortNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/ShortNdArray.java
index f9335b4..022ccf7 100644
--- a/ndarray/src/main/java/org/tensorflow/ndarray/ShortNdArray.java
+++ b/ndarray/src/main/java/org/tensorflow/ndarray/ShortNdArray.java
@@ -68,6 +68,9 @@ public interface ShortNdArray extends NdArray {
*/
ShortNdArray setShort(short value, long... coordinates);
+ @Override
+ ShortNdArray withShape(Shape shape);
+
@Override
ShortNdArray slice(Index... coordinates);
@@ -97,12 +100,12 @@ default ShortNdArray setObject(Short value, long... coordinates) {
ShortNdArray copyTo(NdArray dst);
@Override
- ShortNdArray read(DataBuffer dst);
+ ShortNdArray copyTo(DataBuffer dst);
- ShortNdArray read(ShortDataBuffer dst);
+ ShortNdArray copyTo(ShortDataBuffer dst);
@Override
- ShortNdArray write(DataBuffer src);
+ ShortNdArray copyFrom(DataBuffer src);
- ShortNdArray write(ShortDataBuffer src);
+ ShortNdArray copyFrom(ShortDataBuffer src);
}
diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/StdArrays.java b/ndarray/src/main/java/org/tensorflow/ndarray/StdArrays.java
index 249e69a..7367165 100644
--- a/ndarray/src/main/java/org/tensorflow/ndarray/StdArrays.java
+++ b/ndarray/src/main/java/org/tensorflow/ndarray/StdArrays.java
@@ -2010,7 +2010,7 @@ public static void copyFrom(IntNdArray src, int[] dst) {
if (src.size() > dst.length) {
throw new ArrayIndexOutOfBoundsException(String.valueOf(src.size()) + " > " + dst.length);
}
- src.read(DataBuffers.of(dst, false, false));
+ src.copyTo(DataBuffers.of(dst, false, false));
}
/**
@@ -2113,7 +2113,7 @@ public static void copyFrom(LongNdArray src, long[] dst) {
if (src.size() > dst.length) {
throw new ArrayIndexOutOfBoundsException(String.valueOf(src.size()) + " > " + dst.length);
}
- src.read(DataBuffers.of(dst, false, false));
+ src.copyTo(DataBuffers.of(dst, false, false));
}
/**
@@ -2216,7 +2216,7 @@ public static void copyFrom(FloatNdArray src, float[] dst) {
if (src.size() > dst.length) {
throw new ArrayIndexOutOfBoundsException(String.valueOf(src.size()) + " > " + dst.length);
}
- src.read(DataBuffers.of(dst, false, false));
+ src.copyTo(DataBuffers.of(dst, false, false));
}
/**
@@ -2319,7 +2319,7 @@ public static void copyFrom(DoubleNdArray src, double[] dst) {
if (src.size() > dst.length) {
throw new ArrayIndexOutOfBoundsException(String.valueOf(src.size()) + " > " + dst.length);
}
- src.read(DataBuffers.of(dst, false, false));
+ src.copyTo(DataBuffers.of(dst, false, false));
}
/**
@@ -2422,7 +2422,7 @@ public static void copyFrom(ByteNdArray src, byte[] dst) {
if (src.size() > dst.length) {
throw new ArrayIndexOutOfBoundsException(String.valueOf(src.size()) + " > " + dst.length);
}
- src.read(DataBuffers.of(dst, false, false));
+ src.copyTo(DataBuffers.of(dst, false, false));
}
/**
@@ -2525,7 +2525,7 @@ public static void copyFrom(ShortNdArray src, short[] dst) {
if (src.size() > dst.length) {
throw new ArrayIndexOutOfBoundsException(String.valueOf(src.size()) + " > " + dst.length);
}
- src.read(DataBuffers.of(dst, false, false));
+ src.copyTo(DataBuffers.of(dst, false, false));
}
/**
@@ -2628,7 +2628,7 @@ public static void copyFrom(BooleanNdArray src, boolean[] dst) {
if (src.size() > dst.length) {
throw new ArrayIndexOutOfBoundsException(String.valueOf(src.size()) + " > " + dst.length);
}
- src.read(DataBuffers.of(dst, false, false));
+ src.copyTo(DataBuffers.of(dst, false, false));
}
/**
@@ -2732,7 +2732,7 @@ public static void copyFrom(NdArray src, T[] dst) {
if (src.size() > dst.length) {
throw new ArrayIndexOutOfBoundsException(String.valueOf(src.size()) + " > " + dst.length);
}
- src.read(DataBuffers.of(dst, false, false));
+ src.copyTo(DataBuffers.of(dst, false, false));
}
/**
diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/Validator.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/Validator.java
index 285d099..3e0ba20 100644
--- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/Validator.java
+++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/Validator.java
@@ -30,13 +30,13 @@ public static void copyToNdArrayArgs(NdArray> ndArray, NdArray> otherNdArray
}
}
- public static void readToBufferArgs(NdArray> ndArray, DataBuffer> dst) {
+ public static void copyToBufferArgs(NdArray> ndArray, DataBuffer> dst) {
if (dst.size() < ndArray.size()) {
throw new BufferOverflowException();
}
}
- public static void writeFromBufferArgs(NdArray> ndArray, DataBuffer> src) {
+ public static void copyFromBufferArgs(NdArray> ndArray, DataBuffer> src) {
if (src.size() < ndArray.size()) {
throw new BufferUnderflowException();
}
diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/AbstractDenseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/AbstractDenseNdArray.java
index 30af952..a22518d 100644
--- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/AbstractDenseNdArray.java
+++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/AbstractDenseNdArray.java
@@ -18,6 +18,7 @@
import org.tensorflow.ndarray.NdArray;
import org.tensorflow.ndarray.NdArraySequence;
+import org.tensorflow.ndarray.Shape;
import org.tensorflow.ndarray.impl.AbstractNdArray;
import org.tensorflow.ndarray.impl.dimension.RelativeDimensionalSpace;
import org.tensorflow.ndarray.impl.sequence.FastElementSequence;
@@ -43,7 +44,7 @@ public NdArraySequence elements(int dimensionIdx) {
DimensionalSpace elemDims = dimensions().from(dimensionIdx + 1);
try {
DataBufferWindow extends DataBuffer> elemWindow = buffer().window(elemDims.physicalSize());
- U element = instantiate(elemWindow.buffer(), elemDims);
+ U element = instantiateView(elemWindow.buffer(), elemDims);
return new FastElementSequence(this, dimensionIdx, element, elemWindow);
} catch (UnsupportedOperationException e) {
// If buffer windows are not supported, fallback to slicing (and slower) sequence
@@ -51,10 +52,21 @@ public NdArraySequence elements(int dimensionIdx) {
}
}
+ @Override
+ public U withShape(Shape shape) {
+ if (shape == null || shape.isUnknown() || shape.size() != this.shape().size()) {
+ throw new IllegalArgumentException("Shape " + shape + " cannot be used to reshape ndarray of shape " + this.shape());
+ }
+ if (shape.equals(this.shape())) {
+ return (U)this;
+ }
+ return instantiateView(buffer(), DimensionalSpace.create(shape));
+ }
+
@Override
public U slice(long position, DimensionalSpace sliceDimensions) {
DataBuffer sliceBuffer = buffer().slice(position, sliceDimensions.physicalSize());
- return instantiate(sliceBuffer, sliceDimensions);
+ return instantiateView(sliceBuffer, sliceDimensions);
}
@Override
@@ -89,15 +101,15 @@ public U setObject(T value, long... coords) {
}
@Override
- public U read(DataBuffer dst) {
- Validator.readToBufferArgs(this, dst);
+ public U copyTo(DataBuffer dst) {
+ Validator.copyToBufferArgs(this, dst);
DataTransfer.execute(buffer(), dimensions(), dst, DataTransfer::ofValue);
return (U)this;
}
@Override
- public U write(DataBuffer src) {
- Validator.writeFromBufferArgs(this, src);
+ public U copyFrom(DataBuffer src) {
+ Validator.copyFromBufferArgs(this, src);
DataTransfer.execute(src, buffer(), dimensions(), DataTransfer::ofValue);
return (U)this;
}
@@ -147,7 +159,7 @@ protected AbstractDenseNdArray(DimensionalSpace dimensions) {
abstract protected DataBuffer buffer();
- abstract U instantiate(DataBuffer buffer, DimensionalSpace dimensions);
+ abstract U instantiateView(DataBuffer buffer, DimensionalSpace dimensions);
long positionOf(long[] coords, boolean isValue) {
if (coords == null || coords.length == 0) {
diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/BooleanDenseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/BooleanDenseNdArray.java
index 0764146..c3df6e8 100644
--- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/BooleanDenseNdArray.java
+++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/BooleanDenseNdArray.java
@@ -55,15 +55,15 @@ public BooleanNdArray copyTo(NdArray dst) {
}
@Override
- public BooleanNdArray read(BooleanDataBuffer dst) {
- Validator.readToBufferArgs(this, dst);
+ public BooleanNdArray copyTo(BooleanDataBuffer dst) {
+ Validator.copyToBufferArgs(this, dst);
DataTransfer.execute(buffer, dimensions(), dst, DataTransfer::ofBoolean);
return this;
}
@Override
- public BooleanNdArray write(BooleanDataBuffer src) {
- Validator.writeFromBufferArgs(this, src);
+ public BooleanNdArray copyFrom(BooleanDataBuffer src) {
+ Validator.copyFromBufferArgs(this, src);
DataTransfer.execute(src, buffer, dimensions(), DataTransfer::ofBoolean);
return this;
}
@@ -73,7 +73,7 @@ protected BooleanDenseNdArray(BooleanDataBuffer buffer, Shape shape) {
}
@Override
- BooleanDenseNdArray instantiate(DataBuffer buffer, DimensionalSpace dimensions) {
+ BooleanDenseNdArray instantiateView(DataBuffer buffer, DimensionalSpace dimensions) {
return new BooleanDenseNdArray((BooleanDataBuffer)buffer, dimensions);
}
diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/ByteDenseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/ByteDenseNdArray.java
index 172432b..1f01ce6 100644
--- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/ByteDenseNdArray.java
+++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/ByteDenseNdArray.java
@@ -55,15 +55,15 @@ public ByteNdArray copyTo(NdArray dst) {
}
@Override
- public ByteNdArray read(ByteDataBuffer dst) {
- Validator.readToBufferArgs(this, dst);
+ public ByteNdArray copyTo(ByteDataBuffer dst) {
+ Validator.copyToBufferArgs(this, dst);
DataTransfer.execute(buffer, dimensions(), dst, DataTransfer::ofByte);
return this;
}
@Override
- public ByteNdArray write(ByteDataBuffer src) {
- Validator.writeFromBufferArgs(this, src);
+ public ByteNdArray copyFrom(ByteDataBuffer src) {
+ Validator.copyFromBufferArgs(this, src);
DataTransfer.execute(src, buffer, dimensions(), DataTransfer::ofByte);
return this;
}
@@ -73,7 +73,7 @@ protected ByteDenseNdArray(ByteDataBuffer buffer, Shape shape) {
}
@Override
- ByteDenseNdArray instantiate(DataBuffer buffer, DimensionalSpace dimensions) {
+ ByteDenseNdArray instantiateView(DataBuffer buffer, DimensionalSpace dimensions) {
return new ByteDenseNdArray((ByteDataBuffer)buffer, dimensions);
}
diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/DenseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/DenseNdArray.java
index 819d95d..18b3755 100644
--- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/DenseNdArray.java
+++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/DenseNdArray.java
@@ -45,7 +45,7 @@ protected DenseNdArray(DataBuffer buffer, Shape shape) {
}
@Override
- DenseNdArray instantiate(DataBuffer buffer, DimensionalSpace dimensions) {
+ DenseNdArray instantiateView(DataBuffer buffer, DimensionalSpace dimensions) {
return new DenseNdArray<>(buffer, dimensions);
}
diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/DoubleDenseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/DoubleDenseNdArray.java
index f54b8d0..d983d16 100644
--- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/DoubleDenseNdArray.java
+++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/DoubleDenseNdArray.java
@@ -55,15 +55,15 @@ public DoubleNdArray copyTo(NdArray dst) {
}
@Override
- public DoubleNdArray read(DoubleDataBuffer dst) {
- Validator.readToBufferArgs(this, dst);
+ public DoubleNdArray copyTo(DoubleDataBuffer dst) {
+ Validator.copyToBufferArgs(this, dst);
DataTransfer.execute(buffer, dimensions(), dst, DataTransfer::ofDouble);
return this;
}
@Override
- public DoubleNdArray write(DoubleDataBuffer src) {
- Validator.writeFromBufferArgs(this, src);
+ public DoubleNdArray copyFrom(DoubleDataBuffer src) {
+ Validator.copyFromBufferArgs(this, src);
DataTransfer.execute(src, buffer, dimensions(), DataTransfer::ofDouble);
return this;
}
@@ -73,7 +73,7 @@ protected DoubleDenseNdArray(DoubleDataBuffer buffer, Shape shape) {
}
@Override
- DoubleDenseNdArray instantiate(DataBuffer buffer, DimensionalSpace dimensions) {
+ DoubleDenseNdArray instantiateView(DataBuffer buffer, DimensionalSpace dimensions) {
return new DoubleDenseNdArray((DoubleDataBuffer)buffer, dimensions);
}
diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/FloatDenseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/FloatDenseNdArray.java
index 196b5ef..779e047 100644
--- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/FloatDenseNdArray.java
+++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/FloatDenseNdArray.java
@@ -55,15 +55,15 @@ public FloatNdArray copyTo(NdArray dst) {
}
@Override
- public FloatNdArray read(FloatDataBuffer dst) {
- Validator.readToBufferArgs(this, dst);
+ public FloatNdArray copyTo(FloatDataBuffer dst) {
+ Validator.copyToBufferArgs(this, dst);
DataTransfer.execute(buffer, dimensions(), dst, DataTransfer::ofFloat);
return this;
}
@Override
- public FloatNdArray write(FloatDataBuffer src) {
- Validator.writeFromBufferArgs(this, src);
+ public FloatNdArray copyFrom(FloatDataBuffer src) {
+ Validator.copyFromBufferArgs(this, src);
DataTransfer.execute(src, buffer, dimensions(), DataTransfer::ofFloat);
return this;
}
@@ -73,7 +73,7 @@ protected FloatDenseNdArray(FloatDataBuffer buffer, Shape shape) {
}
@Override
- FloatDenseNdArray instantiate(DataBuffer buffer, DimensionalSpace dimensions) {
+ FloatDenseNdArray instantiateView(DataBuffer buffer, DimensionalSpace dimensions) {
return new FloatDenseNdArray((FloatDataBuffer) buffer, dimensions);
}
diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/IntDenseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/IntDenseNdArray.java
index a7af498..4e2183d 100644
--- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/IntDenseNdArray.java
+++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/IntDenseNdArray.java
@@ -55,15 +55,15 @@ public IntNdArray copyTo(NdArray dst) {
}
@Override
- public IntNdArray read(IntDataBuffer dst) {
- Validator.readToBufferArgs(this, dst);
+ public IntNdArray copyTo(IntDataBuffer dst) {
+ Validator.copyToBufferArgs(this, dst);
DataTransfer.execute(buffer, dimensions(), dst, DataTransfer::ofInt);
return this;
}
@Override
- public IntNdArray write(IntDataBuffer src) {
- Validator.writeFromBufferArgs(this, src);
+ public IntNdArray copyFrom(IntDataBuffer src) {
+ Validator.copyFromBufferArgs(this, src);
DataTransfer.execute(src, buffer, dimensions(), DataTransfer::ofInt);
return this;
}
@@ -73,7 +73,7 @@ protected IntDenseNdArray(IntDataBuffer buffer, Shape shape) {
}
@Override
- IntDenseNdArray instantiate(DataBuffer buffer, DimensionalSpace dimensions) {
+ IntDenseNdArray instantiateView(DataBuffer buffer, DimensionalSpace dimensions) {
return new IntDenseNdArray((IntDataBuffer)buffer, dimensions);
}
diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/LongDenseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/LongDenseNdArray.java
index cd56dad..cde91a3 100644
--- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/LongDenseNdArray.java
+++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/LongDenseNdArray.java
@@ -55,15 +55,15 @@ public LongNdArray copyTo(NdArray dst) {
}
@Override
- public LongNdArray read(LongDataBuffer dst) {
- Validator.readToBufferArgs(this, dst);
+ public LongNdArray copyTo(LongDataBuffer dst) {
+ Validator.copyToBufferArgs(this, dst);
DataTransfer.execute(buffer, dimensions(), dst, DataTransfer::ofLong);
return this;
}
@Override
- public LongNdArray write(LongDataBuffer src) {
- Validator.writeFromBufferArgs(this, src);
+ public LongNdArray copyFrom(LongDataBuffer src) {
+ Validator.copyFromBufferArgs(this, src);
DataTransfer.execute(src, buffer, dimensions(), DataTransfer::ofLong);
return this;
}
@@ -73,7 +73,7 @@ protected LongDenseNdArray(LongDataBuffer buffer, Shape shape) {
}
@Override
- LongDenseNdArray instantiate(DataBuffer buffer, DimensionalSpace dimensions) {
+ LongDenseNdArray instantiateView(DataBuffer buffer, DimensionalSpace dimensions) {
return new LongDenseNdArray((LongDataBuffer)buffer, dimensions);
}
diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/ShortDenseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/ShortDenseNdArray.java
index 291c01a..8d0dfc8 100644
--- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/ShortDenseNdArray.java
+++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/ShortDenseNdArray.java
@@ -55,15 +55,15 @@ public ShortNdArray copyTo(NdArray dst) {
}
@Override
- public ShortNdArray read(ShortDataBuffer dst) {
- Validator.readToBufferArgs(this, dst);
+ public ShortNdArray copyTo(ShortDataBuffer dst) {
+ Validator.copyToBufferArgs(this, dst);
DataTransfer.execute(buffer, dimensions(), dst, DataTransfer::ofShort);
return this;
}
@Override
- public ShortNdArray write(ShortDataBuffer src) {
- Validator.writeFromBufferArgs(this, src);
+ public ShortNdArray copyFrom(ShortDataBuffer src) {
+ Validator.copyFromBufferArgs(this, src);
DataTransfer.execute(src, buffer, dimensions(), DataTransfer::ofShort);
return this;
}
@@ -73,7 +73,7 @@ protected ShortDenseNdArray(ShortDataBuffer buffer, Shape shape) {
}
@Override
- ShortDenseNdArray instantiate(DataBuffer buffer, DimensionalSpace dimensions) {
+ ShortDenseNdArray instantiateView(DataBuffer buffer, DimensionalSpace dimensions) {
return new ShortDenseNdArray((ShortDataBuffer)buffer, dimensions);
}
diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/AbstractSparseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/AbstractSparseNdArray.java
index 8e3892d..e4a2fba 100644
--- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/AbstractSparseNdArray.java
+++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/AbstractSparseNdArray.java
@@ -212,6 +212,11 @@ protected long[] getIndicesCoordinates(LongNdArray l) {
*/
public abstract U toDense();
+ @Override
+ public U withShape(Shape shape) {
+ throw new UnsupportedOperationException("Sparse NdArrays cannot be viewed with a different shape");
+ }
+
/** {@inheritDoc} */
@Override
public NdArray slice(Index... indices) {
diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/BooleanSparseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/BooleanSparseNdArray.java
index f5b984f..d5d8d72 100644
--- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/BooleanSparseNdArray.java
+++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/BooleanSparseNdArray.java
@@ -124,7 +124,7 @@ protected BooleanSparseNdArray(
BooleanDataBuffer dataBuffer, boolean defaultValue, DimensionalSpace dimensions) {
super(defaultValue, dimensions);
// use write to set up the indices and values
- write(dataBuffer);
+ copyFrom(dataBuffer);
}
/**
@@ -266,7 +266,7 @@ public static BooleanSparseNdArray create(
*/
public static BooleanSparseNdArray create(BooleanNdArray src) {
BooleanDataBuffer buffer = DataBuffers.ofBooleans(src.size());
- src.read(buffer);
+ src.copyTo(buffer);
return new BooleanSparseNdArray(buffer, DimensionalSpace.create(src.shape()));
}
/**
@@ -278,7 +278,7 @@ public static BooleanSparseNdArray create(BooleanNdArray src) {
*/
public static BooleanSparseNdArray create(BooleanNdArray src, boolean defaultValue) {
BooleanDataBuffer buffer = DataBuffers.ofBooleans(src.size());
- src.read(buffer);
+ src.copyTo(buffer);
return new BooleanSparseNdArray(buffer, defaultValue, DimensionalSpace.create(src.shape()));
}
@@ -318,13 +318,13 @@ public BooleanNdArray setBoolean(boolean value, long... coordinates) {
/** {@inheritDoc} */
@Override
- public BooleanNdArray read(DataBuffer dst) {
- return read((BooleanDataBuffer) dst);
+ public BooleanNdArray copyTo(DataBuffer dst) {
+ return copyTo((BooleanDataBuffer) dst);
}
/** {@inheritDoc} */
@Override
- public BooleanNdArray read(BooleanDataBuffer dst) {
+ public BooleanNdArray copyTo(BooleanDataBuffer dst) {
// set the values in buf to the default, then overwrite with indices/values
Boolean[] defaults = new Boolean[(int) shape().size()];
Arrays.fill(defaults, getDefaultValue());
@@ -344,7 +344,7 @@ public BooleanNdArray read(BooleanDataBuffer dst) {
/** {@inheritDoc} */
@Override
- public BooleanNdArray write(BooleanDataBuffer src) {
+ public BooleanNdArray copyFrom(BooleanDataBuffer src) {
List indices = new ArrayList<>();
List values = new ArrayList<>();
@@ -368,8 +368,8 @@ public BooleanNdArray write(BooleanDataBuffer src) {
/** {@inheritDoc} */
@Override
- public BooleanNdArray write(DataBuffer src) {
- return write((BooleanDataBuffer) src);
+ public BooleanNdArray copyFrom(DataBuffer src) {
+ return copyFrom((BooleanDataBuffer) src);
}
/**
@@ -379,7 +379,7 @@ public BooleanNdArray write(DataBuffer src) {
*/
public BooleanNdArray toDense() {
BooleanDataBuffer dataBuffer = DataBuffers.ofBooleans(shape().size());
- read(dataBuffer);
+ copyTo(dataBuffer);
return NdArrays.wrap(shape(), dataBuffer);
}
@@ -391,8 +391,8 @@ public BooleanNdArray toDense() {
*/
public BooleanNdArray fromDense(BooleanNdArray src) {
BooleanDataBuffer buffer = DataBuffers.ofBooleans(src.size());
- src.read(buffer);
- write(buffer);
+ src.copyTo(buffer);
+ copyFrom(buffer);
return this;
}
diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/ByteSparseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/ByteSparseNdArray.java
index 259aefe..633ca49 100644
--- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/ByteSparseNdArray.java
+++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/ByteSparseNdArray.java
@@ -118,7 +118,7 @@ protected ByteSparseNdArray(
ByteSparseNdArray(ByteDataBuffer dataBuffer, byte defaultValue, DimensionalSpace dimensions) {
super(defaultValue, dimensions);
// use write to set up the indices and values
- write(dataBuffer);
+ copyFrom(dataBuffer);
}
/**
@@ -255,7 +255,7 @@ public static ByteSparseNdArray create(ByteDataBuffer buffer, byte defaultValue,
*/
public static ByteSparseNdArray create(ByteNdArray src) {
ByteDataBuffer buffer = DataBuffers.ofBytes(src.size());
- src.read(buffer);
+ src.copyTo(buffer);
return new ByteSparseNdArray(buffer, DimensionalSpace.create(src.shape()));
}
@@ -268,7 +268,7 @@ public static ByteSparseNdArray create(ByteNdArray src) {
*/
public static ByteSparseNdArray create(ByteNdArray src, byte defaultValue) {
ByteDataBuffer buffer = DataBuffers.ofBytes(src.size());
- src.read(buffer);
+ src.copyTo(buffer);
return new ByteSparseNdArray(buffer, defaultValue, DimensionalSpace.create(src.shape()));
}
@@ -302,13 +302,13 @@ public ByteNdArray setByte(byte value, long... coordinates) {
/** {@inheritDoc} */
@Override
- public ByteNdArray read(DataBuffer dst) {
- return read((ByteDataBuffer) dst);
+ public ByteNdArray copyTo(DataBuffer dst) {
+ return copyTo((ByteDataBuffer) dst);
}
/** {@inheritDoc} */
@Override
- public ByteNdArray read(ByteDataBuffer dst) {
+ public ByteNdArray copyTo(ByteDataBuffer dst) {
// set the values in buf to the default, then overwrite with indices/values
Byte[] defaults = new Byte[(int) shape().size()];
Arrays.fill(defaults, getDefaultValue());
@@ -328,7 +328,7 @@ public ByteNdArray read(ByteDataBuffer dst) {
/** {@inheritDoc} */
@Override
- public ByteNdArray write(ByteDataBuffer src) {
+ public ByteNdArray copyFrom(ByteDataBuffer src) {
List indices = new ArrayList<>();
List values = new ArrayList<>();
@@ -352,8 +352,8 @@ public ByteNdArray write(ByteDataBuffer src) {
/** {@inheritDoc} */
@Override
- public ByteNdArray write(DataBuffer src) {
- return write((ByteDataBuffer) src);
+ public ByteNdArray copyFrom(DataBuffer src) {
+ return copyFrom((ByteDataBuffer) src);
}
/**
@@ -363,7 +363,7 @@ public ByteNdArray write(DataBuffer src) {
*/
public ByteNdArray toDense() {
ByteDataBuffer dataBuffer = DataBuffers.ofBytes(shape().size());
- read(dataBuffer);
+ copyTo(dataBuffer);
return NdArrays.wrap(shape(), dataBuffer);
}
@@ -375,8 +375,8 @@ public ByteNdArray toDense() {
*/
public ByteNdArray fromDense(ByteNdArray src) {
ByteDataBuffer buffer = DataBuffers.ofBytes(src.size());
- src.read(buffer);
- write(buffer);
+ src.copyTo(buffer);
+ copyFrom(buffer);
return this;
}
diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/DoubleSparseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/DoubleSparseNdArray.java
index 07a6d2a..35b3541 100644
--- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/DoubleSparseNdArray.java
+++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/DoubleSparseNdArray.java
@@ -118,7 +118,7 @@ protected DoubleSparseNdArray(
DoubleDataBuffer dataBuffer, double defaultValue, DimensionalSpace dimensions) {
super(defaultValue, dimensions);
// use write to set up the indices and values
- write(dataBuffer);
+ copyFrom(dataBuffer);
}
/**
@@ -257,7 +257,7 @@ public static DoubleSparseNdArray create(
*/
public static DoubleSparseNdArray create(DoubleNdArray src) {
DoubleDataBuffer buffer = DataBuffers.ofDoubles(src.size());
- src.read(buffer);
+ src.copyTo(buffer);
return new DoubleSparseNdArray(buffer, DimensionalSpace.create(src.shape()));
}
/**
@@ -269,7 +269,7 @@ public static DoubleSparseNdArray create(DoubleNdArray src) {
*/
public static DoubleSparseNdArray create(DoubleNdArray src, double defaultValue) {
DoubleDataBuffer buffer = DataBuffers.ofDoubles(src.size());
- src.read(buffer);
+ src.copyTo(buffer);
return new DoubleSparseNdArray(buffer, defaultValue, DimensionalSpace.create(src.shape()));
}
@@ -303,13 +303,13 @@ public DoubleNdArray setDouble(double value, long... coordinates) {
/** {@inheritDoc} */
@Override
- public DoubleNdArray read(DataBuffer dst) {
- return read((DoubleDataBuffer) dst);
+ public DoubleNdArray copyTo(DataBuffer dst) {
+ return copyTo((DoubleDataBuffer) dst);
}
/** {@inheritDoc} */
@Override
- public DoubleNdArray read(DoubleDataBuffer dst) {
+ public DoubleNdArray copyTo(DoubleDataBuffer dst) {
// set buf to the default values, then overwrite with the indices/values.
Double[] defaults = new Double[(int) shape().size()];
Arrays.fill(defaults, getDefaultValue());
@@ -329,7 +329,7 @@ public DoubleNdArray read(DoubleDataBuffer dst) {
/** {@inheritDoc} */
@Override
- public DoubleNdArray write(DoubleDataBuffer src) {
+ public DoubleNdArray copyFrom(DoubleDataBuffer src) {
List indices = new ArrayList<>();
List values = new ArrayList<>();
@@ -353,8 +353,8 @@ public DoubleNdArray write(DoubleDataBuffer src) {
/** {@inheritDoc} */
@Override
- public DoubleNdArray write(DataBuffer src) {
- return write((DoubleDataBuffer) src);
+ public DoubleNdArray copyFrom(DataBuffer src) {
+ return copyFrom((DoubleDataBuffer) src);
}
/**
@@ -364,7 +364,7 @@ public DoubleNdArray write(DataBuffer src) {
*/
public DoubleNdArray toDense() {
DoubleDataBuffer dataBuffer = DataBuffers.ofDoubles(shape().size());
- read(dataBuffer);
+ copyTo(dataBuffer);
return NdArrays.wrap(shape(), dataBuffer);
}
@@ -376,8 +376,8 @@ public DoubleNdArray toDense() {
*/
public DoubleNdArray fromDense(DoubleNdArray src) {
DoubleDataBuffer buffer = DataBuffers.ofDoubles(src.size());
- src.read(buffer);
- write(buffer);
+ src.copyTo(buffer);
+ copyFrom(buffer);
return this;
}
diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/FloatSparseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/FloatSparseNdArray.java
index 9b224a0..88f34b1 100644
--- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/FloatSparseNdArray.java
+++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/FloatSparseNdArray.java
@@ -118,7 +118,7 @@ protected FloatSparseNdArray(
FloatSparseNdArray(FloatDataBuffer dataBuffer, float defaultValue, DimensionalSpace dimensions) {
super(defaultValue, dimensions);
// use write to set up the indices and values
- write(dataBuffer);
+ copyFrom(dataBuffer);
}
/**
@@ -255,7 +255,7 @@ public static FloatSparseNdArray create(FloatDataBuffer buffer, float defaultVal
*/
public static FloatSparseNdArray create(FloatNdArray src) {
FloatDataBuffer buffer = DataBuffers.ofFloats(src.size());
- src.read(buffer);
+ src.copyTo(buffer);
return new FloatSparseNdArray(buffer, DimensionalSpace.create(src.shape()));
}
/**
@@ -267,7 +267,7 @@ public static FloatSparseNdArray create(FloatNdArray src) {
*/
public static FloatSparseNdArray create(FloatNdArray src, float defaultValue) {
FloatDataBuffer buffer = DataBuffers.ofFloats(src.size());
- src.read(buffer);
+ src.copyTo(buffer);
return new FloatSparseNdArray(buffer, defaultValue, DimensionalSpace.create(src.shape()));
}
@@ -301,13 +301,13 @@ public FloatNdArray setFloat(float value, long... coordinates) {
/** {@inheritDoc} */
@Override
- public FloatNdArray read(DataBuffer dst) {
- return read((FloatDataBuffer) dst);
+ public FloatNdArray copyTo(DataBuffer dst) {
+ return copyTo((FloatDataBuffer) dst);
}
/** {@inheritDoc} */
@Override
- public FloatNdArray read(FloatDataBuffer dst) {
+ public FloatNdArray copyTo(FloatDataBuffer dst) {
// set the values in buf to the default, then overwrite with indices/values
Float[] defaults = new Float[(int) shape().size()];
Arrays.fill(defaults, getDefaultValue());
@@ -327,7 +327,7 @@ public FloatNdArray read(FloatDataBuffer dst) {
/** {@inheritDoc} */
@Override
- public FloatNdArray write(FloatDataBuffer src) {
+ public FloatNdArray copyFrom(FloatDataBuffer src) {
List indices = new ArrayList<>();
List values = new ArrayList<>();
@@ -351,8 +351,8 @@ public FloatNdArray write(FloatDataBuffer src) {
/** {@inheritDoc} */
@Override
- public FloatNdArray write(DataBuffer src) {
- return write((FloatDataBuffer) src);
+ public FloatNdArray copyFrom(DataBuffer src) {
+ return copyFrom((FloatDataBuffer) src);
}
/**
@@ -362,7 +362,7 @@ public FloatNdArray write(DataBuffer src) {
*/
public FloatNdArray toDense() {
FloatDataBuffer dataBuffer = DataBuffers.ofFloats(shape().size());
- read(dataBuffer);
+ copyTo(dataBuffer);
return NdArrays.wrap(shape(), dataBuffer);
}
@@ -374,8 +374,8 @@ public FloatNdArray toDense() {
*/
public FloatNdArray fromDense(FloatNdArray src) {
FloatDataBuffer buffer = DataBuffers.ofFloats(src.size());
- src.read(buffer);
- write(buffer);
+ src.copyTo(buffer);
+ copyFrom(buffer);
return this;
}
diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/IntSparseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/IntSparseNdArray.java
index 757363f..d79c441 100644
--- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/IntSparseNdArray.java
+++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/IntSparseNdArray.java
@@ -116,7 +116,7 @@ protected IntSparseNdArray(
IntSparseNdArray(IntDataBuffer dataBuffer, int defaultValue, DimensionalSpace dimensions) {
super(defaultValue, dimensions);
// use write to set up the indices and values
- write(dataBuffer);
+ copyFrom(dataBuffer);
}
/**
@@ -269,7 +269,7 @@ public static IntSparseNdArray create(IntDataBuffer buffer, int defaultValue, Sh
*/
public static IntSparseNdArray create(IntNdArray src) {
IntDataBuffer buffer = DataBuffers.ofInts(src.size());
- src.read(buffer);
+ src.copyTo(buffer);
return new IntSparseNdArray(buffer, DimensionalSpace.create(src.shape()));
}
@@ -282,7 +282,7 @@ public static IntSparseNdArray create(IntNdArray src) {
*/
public static IntSparseNdArray create(IntNdArray src, int defaultValue) {
IntDataBuffer buffer = DataBuffers.ofInts(src.size());
- src.read(buffer);
+ src.copyTo(buffer);
return new IntSparseNdArray(buffer, defaultValue, DimensionalSpace.create(src.shape()));
}
@@ -316,13 +316,13 @@ public IntNdArray setInt(int value, long... coordinates) {
/** {@inheritDoc} */
@Override
- public IntNdArray read(DataBuffer dst) {
- return read((IntDataBuffer) dst);
+ public IntNdArray copyTo(DataBuffer dst) {
+ return copyTo((IntDataBuffer) dst);
}
/** {@inheritDoc} */
@Override
- public IntNdArray read(IntDataBuffer dst) {
+ public IntNdArray copyTo(IntDataBuffer dst) {
// set the values in buf to the default, then overwrite with indices/values
Integer[] defaults = new Integer[(int) shape().size()];
Arrays.fill(defaults, getDefaultValue());
@@ -342,7 +342,7 @@ public IntNdArray read(IntDataBuffer dst) {
/** {@inheritDoc} */
@Override
- public IntNdArray write(IntDataBuffer src) {
+ public IntNdArray copyFrom(IntDataBuffer src) {
List indices = new ArrayList<>();
List values = new ArrayList<>();
@@ -366,8 +366,8 @@ public IntNdArray write(IntDataBuffer src) {
/** {@inheritDoc} */
@Override
- public IntNdArray write(DataBuffer src) {
- return write((IntDataBuffer) src);
+ public IntNdArray copyFrom(DataBuffer src) {
+ return copyFrom((IntDataBuffer) src);
}
/**
@@ -377,7 +377,7 @@ public IntNdArray write(DataBuffer src) {
*/
public IntNdArray toDense() {
IntDataBuffer dataBuffer = DataBuffers.ofInts(shape().size());
- read(dataBuffer);
+ copyTo(dataBuffer);
return NdArrays.wrap(shape(), dataBuffer);
}
@@ -389,8 +389,8 @@ public IntNdArray toDense() {
*/
public IntNdArray fromDense(IntNdArray src) {
IntDataBuffer buffer = DataBuffers.ofInts(src.size());
- src.read(buffer);
- write(buffer);
+ src.copyTo(buffer);
+ copyFrom(buffer);
return this;
}
diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/LongSparseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/LongSparseNdArray.java
index 242ba50..1c01198 100644
--- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/LongSparseNdArray.java
+++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/LongSparseNdArray.java
@@ -117,7 +117,7 @@ protected LongSparseNdArray(
LongSparseNdArray(LongDataBuffer dataBuffer, long defaultValue, DimensionalSpace dimensions) {
super(defaultValue, dimensions);
// use write to set up the indices and values
- write(dataBuffer);
+ copyFrom(dataBuffer);
}
/**
@@ -254,7 +254,7 @@ public static LongSparseNdArray create(LongDataBuffer buffer, long defaultValue,
*/
public static LongSparseNdArray create(LongNdArray src) {
LongDataBuffer buffer = DataBuffers.ofLongs(src.size());
- src.read(buffer);
+ src.copyTo(buffer);
return new LongSparseNdArray(buffer, DimensionalSpace.create(src.shape()));
}
@@ -267,7 +267,7 @@ public static LongSparseNdArray create(LongNdArray src) {
*/
public static LongSparseNdArray create(LongNdArray src, long defaultValue) {
LongDataBuffer buffer = DataBuffers.ofLongs(src.size());
- src.read(buffer);
+ src.copyTo(buffer);
return new LongSparseNdArray(buffer, defaultValue, DimensionalSpace.create(src.shape()));
}
@@ -301,13 +301,13 @@ public LongNdArray setLong(long value, long... coordinates) {
/** {@inheritDoc} */
@Override
- public LongNdArray read(DataBuffer dst) {
- return read((LongDataBuffer) dst);
+ public LongNdArray copyTo(DataBuffer dst) {
+ return copyTo((LongDataBuffer) dst);
}
/** {@inheritDoc} */
@Override
- public LongNdArray read(LongDataBuffer dst) {
+ public LongNdArray copyTo(LongDataBuffer dst) {
// set the values in buf to the default, then overwrite with indices/values
Long[] defaults = new Long[(int) shape().size()];
Arrays.fill(defaults, getDefaultValue());
@@ -327,7 +327,7 @@ public LongNdArray read(LongDataBuffer dst) {
/** {@inheritDoc} */
@Override
- public LongNdArray write(LongDataBuffer src) {
+ public LongNdArray copyFrom(LongDataBuffer src) {
List indices = new ArrayList<>();
List values = new ArrayList<>();
@@ -351,8 +351,8 @@ public LongNdArray write(LongDataBuffer src) {
/** {@inheritDoc} */
@Override
- public LongNdArray write(DataBuffer src) {
- return write((LongDataBuffer) src);
+ public LongNdArray copyFrom(DataBuffer src) {
+ return copyFrom((LongDataBuffer) src);
}
/**
@@ -362,7 +362,7 @@ public LongNdArray write(DataBuffer src) {
*/
public LongNdArray toDense() {
LongDataBuffer dataBuffer = DataBuffers.ofLongs(shape().size());
- read(dataBuffer);
+ copyTo(dataBuffer);
return NdArrays.wrap(shape(), dataBuffer);
}
@@ -374,8 +374,8 @@ public LongNdArray toDense() {
*/
public LongNdArray fromDense(LongNdArray src) {
LongDataBuffer buffer = DataBuffers.ofLongs(src.size());
- src.read(buffer);
- write(buffer);
+ src.copyTo(buffer);
+ copyFrom(buffer);
return this;
}
diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/ShortSparseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/ShortSparseNdArray.java
index 63dde22..051a7cb 100644
--- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/ShortSparseNdArray.java
+++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/ShortSparseNdArray.java
@@ -118,7 +118,7 @@ protected ShortSparseNdArray(
ShortSparseNdArray(ShortDataBuffer dataBuffer, short defaultValue, DimensionalSpace dimensions) {
super(defaultValue, dimensions);
// use write to set up the indices and values
- write(dataBuffer);
+ copyFrom(dataBuffer);
}
/**
@@ -255,7 +255,7 @@ public static ShortSparseNdArray create(ShortDataBuffer buffer, short defaultVal
*/
public static ShortSparseNdArray create(ShortNdArray src) {
ShortDataBuffer buffer = DataBuffers.ofShorts(src.size());
- src.read(buffer);
+ src.copyTo(buffer);
return new ShortSparseNdArray(buffer, DimensionalSpace.create(src.shape()));
}
@@ -268,7 +268,7 @@ public static ShortSparseNdArray create(ShortNdArray src) {
*/
public static ShortSparseNdArray create(ShortNdArray src, short defaultValue) {
ShortDataBuffer buffer = DataBuffers.ofShorts(src.size());
- src.read(buffer);
+ src.copyTo(buffer);
return new ShortSparseNdArray(buffer, defaultValue, DimensionalSpace.create(src.shape()));
}
@@ -302,13 +302,13 @@ public ShortNdArray setShort(short value, long... coordinates) {
/** {@inheritDoc} */
@Override
- public ShortNdArray read(DataBuffer dst) {
- return read((ShortDataBuffer) dst);
+ public ShortNdArray copyTo(DataBuffer dst) {
+ return copyTo((ShortDataBuffer) dst);
}
/** {@inheritDoc} */
@Override
- public ShortNdArray read(ShortDataBuffer dst) {
+ public ShortNdArray copyTo(ShortDataBuffer dst) {
// set the values in buf to the default, then overwrite with indices/values
Short[] defaults = new Short[(int) shape().size()];
Arrays.fill(defaults, getDefaultValue());
@@ -328,7 +328,7 @@ public ShortNdArray read(ShortDataBuffer dst) {
/** {@inheritDoc} */
@Override
- public ShortNdArray write(ShortDataBuffer src) {
+ public ShortNdArray copyFrom(ShortDataBuffer src) {
List indices = new ArrayList<>();
List values = new ArrayList<>();
@@ -352,8 +352,8 @@ public ShortNdArray write(ShortDataBuffer src) {
/** {@inheritDoc} */
@Override
- public ShortNdArray write(DataBuffer src) {
- return write((ShortDataBuffer) src);
+ public ShortNdArray copyFrom(DataBuffer src) {
+ return copyFrom((ShortDataBuffer) src);
}
/**
@@ -363,7 +363,7 @@ public ShortNdArray write(DataBuffer src) {
*/
public ShortNdArray toDense() {
ShortDataBuffer dataBuffer = DataBuffers.ofShorts(shape().size());
- read(dataBuffer);
+ copyTo(dataBuffer);
return NdArrays.wrap(shape(), dataBuffer);
}
@@ -375,8 +375,8 @@ public ShortNdArray toDense() {
*/
public ShortNdArray fromDense(ShortNdArray src) {
ShortDataBuffer buffer = DataBuffers.ofShorts(src.size());
- src.read(buffer);
- write(buffer);
+ src.copyTo(buffer);
+ copyFrom(buffer);
return this;
}
diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/SparseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/SparseNdArray.java
index 52b3867..c6d93f2 100644
--- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/SparseNdArray.java
+++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/SparseNdArray.java
@@ -123,7 +123,7 @@ protected SparseNdArray(
super(defaultValue, dimensions);
this.type = type;
// use write to set up the indices and values
- write(dataBuffer);
+ copyFrom(dataBuffer);
}
/**
@@ -266,7 +266,7 @@ public static > SparseNdArray create(
*/
public static > SparseNdArray create(Class type, U src) {
DataBuffer buffer = DataBuffers.ofObjects(type, src.size());
- src.read(buffer);
+ src.copyTo(buffer);
return new SparseNdArray<>(type, buffer, DimensionalSpace.create(src.shape()));
}
/**
@@ -279,7 +279,7 @@ public static > SparseNdArray create(Class type
public static > SparseNdArray create(
Class type, U src, T defaultValue) {
DataBuffer buffer = DataBuffers.ofObjects(type, src.size());
- src.read(buffer);
+ src.copyTo(buffer);
return new SparseNdArray<>(type, buffer, defaultValue, DimensionalSpace.create(src.shape()));
}
@@ -312,7 +312,7 @@ public U slice(long position, DimensionalSpace sliceDimensions) {
/** {@inheritDoc} */
@Override
- public NdArray read(DataBuffer dst) {
+ public NdArray copyTo(DataBuffer dst) {
// set the values in buf to the default, then overwrite with indices/values
@SuppressWarnings("unchecked")
T[] defaults = (T[]) Array.newInstance(type, (int) dst.size());
@@ -336,7 +336,7 @@ public NdArray read(DataBuffer dst) {
@SuppressWarnings({
"unchecked",
})
- public NdArray write(DataBuffer src) {
+ public NdArray copyFrom(DataBuffer src) {
List indices = new ArrayList<>();
List values = new ArrayList<>();
@@ -368,7 +368,7 @@ public NdArray write(DataBuffer src) {
@SuppressWarnings("unchecked")
public U toDense() {
DataBuffer dataBuffer = DataBuffers.ofObjects(type, shape().size());
- read(dataBuffer);
+ copyTo(dataBuffer);
// unchecked cast, suppressed.
return (U) NdArrays.wrap(shape(), dataBuffer);
}
@@ -381,8 +381,8 @@ public U toDense() {
*/
public NdArray fromDense(NdArray src) {
DataBuffer buffer = DataBuffers.ofObjects(type, src.size());
- src.read(buffer);
- write(buffer);
+ src.copyTo(buffer);
+ copyFrom(buffer);
return this;
}
diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/BooleanSparseSlice.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/BooleanSparseSlice.java
index 24283db..945ccdc 100644
--- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/BooleanSparseSlice.java
+++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/BooleanSparseSlice.java
@@ -49,7 +49,7 @@ public BooleanSparseSlice(
@Override
public BooleanNdArray toDense() {
BooleanDataBuffer dataBuffer = DataBuffers.ofBooleans(shape().size());
- read(dataBuffer);
+ copyTo(dataBuffer);
return NdArrays.wrap(shape(), dataBuffer);
}
@@ -75,7 +75,7 @@ public BooleanNdArray set(NdArray src, long... coordinates) {
/** {@inheritDoc} */
@Override
- public BooleanNdArray read(DataBuffer dst) {
+ public BooleanNdArray copyTo(DataBuffer dst) {
// zero out buf.
Boolean[] defaults = new Boolean[(int) shape().size()];
dst.write(defaults);
@@ -93,17 +93,17 @@ public BooleanNdArray read(DataBuffer dst) {
}
@Override
- public BooleanNdArray read(BooleanDataBuffer dst) {
- return read((DataBuffer) dst);
+ public BooleanNdArray copyTo(BooleanDataBuffer dst) {
+ return copyTo((DataBuffer) dst);
}
@Override
- public BooleanNdArray write(DataBuffer src) {
+ public BooleanNdArray copyFrom(DataBuffer src) {
throw new ReadOnlyBufferException();
}
@Override
- public BooleanNdArray write(BooleanDataBuffer src) {
+ public BooleanNdArray copyFrom(BooleanDataBuffer src) {
throw new ReadOnlyBufferException();
}
diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/ByteSparseSlice.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/ByteSparseSlice.java
index a8ced3b..35e33d9 100644
--- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/ByteSparseSlice.java
+++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/ByteSparseSlice.java
@@ -48,7 +48,7 @@ public ByteSparseSlice(
@Override
public ByteNdArray toDense() {
ByteDataBuffer dataBuffer = DataBuffers.ofBytes(shape().size());
- read(dataBuffer);
+ copyTo(dataBuffer);
return NdArrays.wrap(shape(), dataBuffer);
}
@@ -74,7 +74,7 @@ public ByteNdArray set(NdArray src, long... coordinates) {
/** {@inheritDoc} */
@Override
- public ByteNdArray read(DataBuffer dst) {
+ public ByteNdArray copyTo(DataBuffer dst) {
// zero out buf.
Byte[] defaults = new Byte[(int) shape().size()];
dst.write(defaults);
@@ -92,17 +92,17 @@ public ByteNdArray read(DataBuffer dst) {
}
@Override
- public ByteNdArray read(ByteDataBuffer dst) {
- return read((DataBuffer) dst);
+ public ByteNdArray copyTo(ByteDataBuffer dst) {
+ return this.copyTo((DataBuffer) dst);
}
@Override
- public ByteNdArray write(DataBuffer src) {
+ public ByteNdArray copyFrom(DataBuffer src) {
throw new ReadOnlyBufferException();
}
@Override
- public ByteNdArray write(ByteDataBuffer src) {
+ public ByteNdArray copyFrom(ByteDataBuffer src) {
throw new ReadOnlyBufferException();
}
diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/DoubleSparseSlice.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/DoubleSparseSlice.java
index 596be18..3f66308 100644
--- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/DoubleSparseSlice.java
+++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/DoubleSparseSlice.java
@@ -49,7 +49,7 @@ public DoubleSparseSlice(
@Override
public DoubleNdArray toDense() {
DoubleDataBuffer dataBuffer = DataBuffers.ofDoubles(shape().size());
- read(dataBuffer);
+ copyTo(dataBuffer);
return NdArrays.wrap(shape(), dataBuffer);
}
@@ -75,7 +75,7 @@ public DoubleNdArray set(NdArray src, long... coordinates) {
/** {@inheritDoc} */
@Override
- public DoubleNdArray read(DataBuffer dst) {
+ public DoubleNdArray copyTo(DataBuffer dst) {
// set the values in buf to the default, then overwrite with indices/values
Double[] defaults = new Double[(int) shape().size()];
Arrays.fill(defaults, getDefaultValue());
@@ -94,17 +94,17 @@ public DoubleNdArray read(DataBuffer dst) {
}
@Override
- public DoubleNdArray read(DoubleDataBuffer dst) {
- return read((DataBuffer) dst);
+ public DoubleNdArray copyTo(DoubleDataBuffer dst) {
+ return this.copyTo((DataBuffer) dst);
}
@Override
- public DoubleNdArray write(DataBuffer src) {
+ public DoubleNdArray copyFrom(DataBuffer src) {
throw new ReadOnlyBufferException();
}
@Override
- public DoubleNdArray write(DoubleDataBuffer src) {
+ public DoubleNdArray copyFrom(DoubleDataBuffer src) {
throw new ReadOnlyBufferException();
}
diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/FloatSparseSlice.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/FloatSparseSlice.java
index 5e6094d..8e2204d 100644
--- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/FloatSparseSlice.java
+++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/FloatSparseSlice.java
@@ -49,7 +49,7 @@ public FloatSparseSlice(
@Override
public FloatNdArray toDense() {
FloatDataBuffer dataBuffer = DataBuffers.ofFloats(shape().size());
- read(dataBuffer);
+ copyTo(dataBuffer);
return NdArrays.wrap(shape(), dataBuffer);
}
@@ -75,7 +75,7 @@ public FloatNdArray set(NdArray src, long... coordinates) {
/** {@inheritDoc} */
@Override
- public FloatNdArray read(DataBuffer dst) {
+ public FloatNdArray copyTo(DataBuffer dst) {
// set the values in buf to the default, then overwrite with indices/values
Float[] defaults = new Float[(int) shape().size()];
Arrays.fill(defaults, getDefaultValue());
@@ -94,17 +94,17 @@ public FloatNdArray read(DataBuffer dst) {
}
@Override
- public FloatNdArray read(FloatDataBuffer dst) {
- return read((DataBuffer) dst);
+ public FloatNdArray copyTo(FloatDataBuffer dst) {
+ return this.copyTo((DataBuffer) dst);
}
@Override
- public FloatNdArray write(DataBuffer src) {
+ public FloatNdArray copyFrom(DataBuffer src) {
throw new ReadOnlyBufferException();
}
@Override
- public FloatNdArray write(FloatDataBuffer src) {
+ public FloatNdArray copyFrom(FloatDataBuffer src) {
throw new ReadOnlyBufferException();
}
diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/IntSparseSlice.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/IntSparseSlice.java
index 067c64b..988551b 100644
--- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/IntSparseSlice.java
+++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/IntSparseSlice.java
@@ -49,7 +49,7 @@ public IntSparseSlice(
@Override
public IntNdArray toDense() {
IntDataBuffer dataBuffer = DataBuffers.ofInts(shape().size());
- read(dataBuffer);
+ copyTo(dataBuffer);
return NdArrays.wrap(shape(), dataBuffer);
}
@@ -75,7 +75,7 @@ public IntNdArray set(NdArray src, long... coordinates) {
/** {@inheritDoc} */
@Override
- public IntNdArray read(DataBuffer dst) {
+ public IntNdArray copyTo(DataBuffer dst) {
// set the values in buf to the default, then overwrite with indices/values
Integer[] defaults = new Integer[(int) shape().size()];
Arrays.fill(defaults, getDefaultValue());
@@ -94,17 +94,17 @@ public IntNdArray read(DataBuffer dst) {
}
@Override
- public IntNdArray read(IntDataBuffer dst) {
- return read((DataBuffer) dst);
+ public IntNdArray copyTo(IntDataBuffer dst) {
+ return this.copyTo((DataBuffer) dst);
}
@Override
- public IntNdArray write(DataBuffer src) {
+ public IntNdArray copyFrom(DataBuffer src) {
throw new ReadOnlyBufferException();
}
@Override
- public IntNdArray write(IntDataBuffer src) {
+ public IntNdArray copyFrom(IntDataBuffer src) {
throw new ReadOnlyBufferException();
}
diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/LongSparseSlice.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/LongSparseSlice.java
index ea182b7..0916293 100644
--- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/LongSparseSlice.java
+++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/LongSparseSlice.java
@@ -49,7 +49,7 @@ public LongSparseSlice(
@Override
public LongNdArray toDense() {
LongDataBuffer dataBuffer = DataBuffers.ofLongs(shape().size());
- read(dataBuffer);
+ copyTo(dataBuffer);
return NdArrays.wrap(shape(), dataBuffer);
}
@@ -75,7 +75,7 @@ public LongNdArray set(NdArray src, long... coordinates) {
/** {@inheritDoc} */
@Override
- public LongNdArray read(DataBuffer dst) {
+ public LongNdArray copyTo(DataBuffer dst) {
// set the values in buf to the default, then overwrite with indices/values
Long[] defaults = new Long[(int) shape().size()];
Arrays.fill(defaults, getDefaultValue());
@@ -94,17 +94,17 @@ public LongNdArray read(DataBuffer dst) {
}
@Override
- public LongNdArray read(LongDataBuffer dst) {
- return read((DataBuffer) dst);
+ public LongNdArray copyTo(LongDataBuffer dst) {
+ return copyTo((DataBuffer) dst);
}
@Override
- public LongNdArray write(DataBuffer src) {
+ public LongNdArray copyFrom(DataBuffer src) {
throw new ReadOnlyBufferException();
}
@Override
- public LongNdArray write(LongDataBuffer src) {
+ public LongNdArray copyFrom(LongDataBuffer src) {
throw new ReadOnlyBufferException();
}
diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/ObjectSparseSlice.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/ObjectSparseSlice.java
index be7f9f4..9f62bf8 100644
--- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/ObjectSparseSlice.java
+++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/ObjectSparseSlice.java
@@ -48,7 +48,7 @@ public ObjectSparseSlice(
@SuppressWarnings("unchecked")
public U toDense() {
DataBuffer dataBuffer = DataBuffers.ofObjects(getType(), shape().size());
- read(dataBuffer);
+ copyTo(dataBuffer);
// unchecked NdArray to U
return (U) NdArrays.wrap(shape(), dataBuffer);
}
@@ -66,7 +66,7 @@ public U set(NdArray src, long... coordinates) {
/** {@inheritDoc} */
@Override
@SuppressWarnings("unchecked")
- public U read(DataBuffer dst) {
+ public U copyTo(DataBuffer dst) {
// unchecked Object to T[]
T[] defaults = (T[]) Array.newInstance(getType(), (int) dst.size());
Arrays.fill(defaults, getDefaultValue());
diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/ShortSparseSlice.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/ShortSparseSlice.java
index 16aa561..2da0f8c 100644
--- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/ShortSparseSlice.java
+++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/ShortSparseSlice.java
@@ -49,7 +49,7 @@ public ShortSparseSlice(
@Override
public ShortNdArray toDense() {
ShortDataBuffer dataBuffer = DataBuffers.ofShorts(shape().size());
- read(dataBuffer);
+ copyTo(dataBuffer);
return NdArrays.wrap(shape(), dataBuffer);
}
@@ -75,7 +75,7 @@ public ShortNdArray set(NdArray src, long... coordinates) {
/** {@inheritDoc} */
@Override
- public ShortNdArray read(DataBuffer dst) {
+ public ShortNdArray copyTo(DataBuffer dst) {
// set the values in buf to the default, then overwrite with indices/values
Short[] defaults = new Short[(int) shape().size()];
Arrays.fill(defaults, getDefaultValue());
@@ -94,17 +94,17 @@ public ShortNdArray read(DataBuffer dst) {
}
@Override
- public ShortNdArray read(ShortDataBuffer dst) {
- return read((DataBuffer) dst);
+ public ShortNdArray copyTo(ShortDataBuffer dst) {
+ return this.copyTo((DataBuffer) dst);
}
@Override
- public ShortNdArray write(DataBuffer src) {
+ public ShortNdArray copyFrom(DataBuffer src) {
throw new ReadOnlyBufferException();
}
@Override
- public ShortNdArray write(ShortDataBuffer src) {
+ public ShortNdArray copyFrom(ShortDataBuffer src) {
throw new ReadOnlyBufferException();
}
diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/SparseSlice.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/SparseSlice.java
index e0db839..3e5be6f 100644
--- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/SparseSlice.java
+++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/SparseSlice.java
@@ -133,7 +133,7 @@ public NdArraySequence elements(int dimensionIdx) {
/** {@inheritDoc} */
@Override
- public NdArray write(DataBuffer src) {
+ public NdArray copyFrom(DataBuffer src) {
throw new ReadOnlyBufferException();
}
diff --git a/ndarray/src/test/java/module-info.test b/ndarray/src/test/java/module-info.test
new file mode 100644
index 0000000..310e500
--- /dev/null
+++ b/ndarray/src/test/java/module-info.test
@@ -0,0 +1,22 @@
+/*
+ Copyright 2022 The TensorFlow Authors. All Rights Reserved.
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+ =======================================================================
+ */
+module org.tensorflow.ndarray {
+ requires java.desktop; // required for java.awt.*
+
+ requires transitive org.junit.jupiter.engine;
+ requires transitive org.junit.jupiter.api;
+}
diff --git a/ndarray/src/test/java/org/tensorflow/ndarray/IndexTest.java b/ndarray/src/test/java/org/tensorflow/ndarray/IndexTest.java
index 6f92dab..10c0806 100644
--- a/ndarray/src/test/java/org/tensorflow/ndarray/IndexTest.java
+++ b/ndarray/src/test/java/org/tensorflow/ndarray/IndexTest.java
@@ -18,6 +18,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import org.junit.jupiter.api.Test;
import org.tensorflow.ndarray.index.Indices;
@@ -43,6 +44,371 @@ public void testNullConversions(){
assertTrue(Indices.slice(null, null).endMask(),
"Passed null for slice end but didn't set end mask");
}
+
+ @Test
+ public void testIndices(){
+
+ String[][] indexData = new String[5][4];
+ for (int i=0 ; i < 5; i++){
+ for (int j=0 ; j < 4; j++)
+ indexData[i][j] = "("+j+", "+i+")";
+ }
+
+ NdArray matrix2d = StdArrays.ndCopyOf(indexData);
+ assertEquals(2, matrix2d.rank());
+
+ /*
+ |(0, 0), (1, 0), (2, 0), (3, 0)|
+ |(0, 1), (1, 1), (2, 1), (3, 1)|
+ |(0, 2), (1, 2), (2, 2), (3, 2)|
+ |(0, 3), (1, 3), (2, 3), (3, 3)|
+ |(0, 4), (1, 4), (2, 4), (3, 4)|
+ */
+ assertArrayEquals(new String[]{"(0, 0)", "(1, 0)", "(2, 0)", "(3, 0)"}, indexData[0]);
+
+ NdArray