> 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/NdArrays.java b/ndarray/src/main/java/org/tensorflow/ndarray/NdArrays.java
index 64ab7eb..d79b781 100644
--- a/ndarray/src/main/java/org/tensorflow/ndarray/NdArrays.java
+++ b/ndarray/src/main/java/org/tensorflow/ndarray/NdArrays.java
@@ -108,12 +108,12 @@ public static ByteNdArray wrap(Shape shape, ByteDataBuffer buffer) {
*
* @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the
* elements in the sparse array that contain non-default values (elements are zero-indexed).
- * For example, {@code indices=[[1,3], [2,4]]} specifies that the elements with indexes of
- * {@code [1,3]} and {@code [2,4]} have non-default values.
+ * For example, {@code indices=[[1,3,1], [2,4,0]]} specifies that the elements with indexes of
+ * {@code [1,3,1]} and {@code [2,4,0]} have non-default values.
* @param values A 1-D ByteNdArray of shape {@code [N]}, which supplies the values for each
- * element in indices. For example, given {@code indices=[[1,3], [2,4]]}, the parameter {@code
- * values=[18, 3]} specifies that element {@code [1,3]} of the sparse NdArray has a value of
- * {@code 18}, and element {@code [2,4]} of the NdArray has a value of {@code 3}.
+ * element in indices. For example, given {@code indices=[[1,3,1], [2,4,0]]}, the parameter {@code
+ * values=[18, 3]} specifies that element {@code [1,3,1]} of the sparse NdArray has a value of
+ * {@code 18}, and element {@code [2,4,0]} of the NdArray has a value of {@code 3}.
* @param shape the shape of the dense array represented by this sparse array.
* @return the byte sparse array.
*/
@@ -126,12 +126,12 @@ public static ByteSparseNdArray sparseOf(LongNdArray indices, ByteNdArray values
*
* @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the
* elements in the sparse array that contain non-default values (elements are zero-indexed).
- * For example, {@code indices=[[1,3], [2,4]]} specifies that the elements with indexes of
- * {@code [1,3]} and {@code [2,4]} have non default values.
+ * For example, {@code indices=[[1,3,1], [2,4,0]]} specifies that the elements with indexes of
+ * {@code [1,3,1]} and {@code [2,4,0]} have non default values.
* @param values A 1-D ByteNdArray of shape {@code [N]}, which supplies the values for each
- * element in indices. For example, given {@code indices=[[1,3], [2,4]]}, the parameter {@code
- * values=[18, 3]} specifies that element {@code [1,3]} of the sparse NdArray has a value of
- * {@code 18}, and element {@code [2,4]} of the NdArray has a value of {@code 3}.
+ * element in indices. For example, given {@code indices=[[1,3,1], [2,4,0]]}, the parameter {@code
+ * values=[18, 3]} specifies that element {@code [1,3,1]} of the sparse NdArray has a value of
+ * {@code 18}, and element {@code [2,4,0]} of the NdArray has a value of {@code 3}.
* @param defaultValue Scalar value to set for indices not specified in 'indices'
* @param shape the shape of the dense array represented by this sparse array.
* @return the byte sparse array.
@@ -201,12 +201,12 @@ public static LongNdArray wrap(Shape shape, LongDataBuffer buffer) {
*
* @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the
* elements in the sparse array that contain non-default values (elements are zero-indexed).
- * For example, {@code indices=[[1,3], [2,4]]} specifies that the elements with indexes of
- * {@code [1,3]} and {@code [2,4]} have non-default values.
+ * For example, {@code indices=[[1,3,1], [2,4,0]]} specifies that the elements with indexes of
+ * {@code [1,3,1]} and {@code [2,4,0]} have non-default values.
* @param values A 1-D LongNdArray of shape {@code [N]}, which supplies the values for each
- * element in indices. For example, given {@code indices=[[1,3], [2,4]]}, the parameter {@code
- * values=[18, 3]} specifies that element {@code [1,3]} of the sparse NdArray has a value of
- * {@code 18}, and element {@code [2,4]} of the NdArray has a value of {@code 3}.
+ * element in indices. For example, given {@code indices=[[1,3,1], [2,4,0]]}, the parameter {@code
+ * values=[18L, 3L]} specifies that element {@code [1,3,1]} of the sparse NdArray has a value of
+ * {@code 18L}, and element {@code [2,4,0]} of the NdArray has a value of {@code 3L}.
* @param shape the shape of the dense array represented by this sparse array.
* @return the long sparse array.
*/
@@ -219,12 +219,12 @@ public static LongSparseNdArray sparseOf(LongNdArray indices, LongNdArray values
*
* @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the
* elements in the sparse array that contain non-default values (elements are zero-indexed).
- * For example, {@code indices=[[1,3], [2,4]]} specifies that the elements with indexes of
- * {@code [1,3]} and {@code [2,4]} have non-default values.
+ * For example, {@code indices=[[1,3,1], [2,4,0]]} specifies that the elements with indexes of
+ * {@code [1,3,1]} and {@code [2,4,0]} have non-default values.
* @param values A 1-D LongNdArray of shape {@code [N]}, which supplies the values for each
- * element in indices. For example, given {@code indices=[[1,3], [2,4]]}, the parameter {@code
- * values=[18, 3]} specifies that element {@code [1,3]} of the sparse NdArray has a value of
- * {@code 18}, and element {@code [2,4]} of the NdArray has a value of {@code 3}.
+ * element in indices. For example, given {@code indices=[[1,3,1], [2,4,0]]}, the parameter {@code
+ * values=[18L, 3L]} specifies that element {@code [1,3,1]} of the sparse NdArray has a value of
+ * {@code 18L}, and element {@code [2,4,0]} of the NdArray has a value of {@code 3L}.
* @param defaultValue Scalar value to set for indices not specified in 'indices'
* @param shape the shape of the dense array represented by this sparse array.
* @return the long sparse array.
@@ -294,12 +294,12 @@ public static IntNdArray wrap(Shape shape, IntDataBuffer buffer) {
*
* @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the
* elements in the sparse array that contain non-default values (elements are zero-indexed).
- * For example, {@code indices=[[1,3], [2,4]]} specifies that the elements with indexes of
- * {@code [1,3]} and {@code [2,4]} have non-default values.
+ * For example, {@code indices=[[1,3,1], [2,4,0]]} specifies that the elements with indexes of
+ * {@code [1,3,1]} and {@code [2,4,0]} have non-default values.
* @param values A 1-D IntNdArray of shape {@code [N]}, which supplies the values for each element
- * in indices. For example, given {@code indices=[[1,3], [2,4]]}, the parameter {@code
- * values=[18, 3]} specifies that element {@code [1,3]} of the sparse NdArray has a value of
- * {@code 18}, and element {@code [2,4]} of the NdArray has a value of {@code 3}.
+ * in indices. For example, given {@code indices=[[1,3,1], [2,4,0]]}, the parameter {@code
+ * values=[18, 3]} specifies that element {@code [1,3,1]} of the sparse NdArray has a value of
+ * {@code 18}, and element {@code [2,4,0]} of the NdArray has a value of {@code 3}.
* @param shape the shape of the dense array represented by this sparse array.
* @return the int sparse array.
*/
@@ -312,12 +312,12 @@ public static IntSparseNdArray sparseOf(LongNdArray indices, IntNdArray values,
*
* @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the
* elements in the sparse array that contain non-default values (elements are zero-indexed).
- * For example, {@code indices=[[1,3], [2,4]]} specifies that the elements with indexes of
- * {@code [1,3]} and {@code [2,4]} have non-default values.
+ * For example, {@code indices=[[1,3,1], [2,4,0]]} specifies that the elements with indexes of
+ * {@code [1,3,1]} and {@code [2,4,0]} have non-default values.
* @param values A 1-D IntNdArray of shape {@code [N]}, which supplies the values for each element
- * in indices. For example, given {@code indices=[[1,3], [2,4]]}, the parameter {@code
- * values=[18, 3]} specifies that element {@code [1,3]} of the sparse NdArray has a value of
- * {@code 18}, and element {@code [2,4]} of the NdArray has a value of {@code 3}.
+ * in indices. For example, given {@code indices=[[1,3,1], [2,4,0]]}, the parameter {@code
+ * values=[18, 3]} specifies that element {@code [1,3,1]} of the sparse NdArray has a value of
+ * {@code 18}, and element {@code [2,4,0]} of the NdArray has a value of {@code 3}.
* @param defaultValue Scalar value to set for indices not specified in 'indices'
* @param shape the shape of the dense array represented by this sparse array.
* @return the int sparse array.
@@ -387,12 +387,12 @@ public static ShortNdArray wrap(Shape shape, ShortDataBuffer buffer) {
*
* @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the
* elements in the sparse array that contain non-default values (elements are zero-indexed).
- * For example, {@code indices=[[1,3], [2,4]]} specifies that the elements with indexes of
- * {@code [1,3]} and {@code [2,4]} have non-default values.
+ * For example, {@code indices=[[1,3,1], [2,4,0]]} specifies that the elements with indexes of
+ * {@code [1,3,1]} and {@code [2,4,0]} have non-default values.
* @param values A 1-D ShortNdArray of shape {@code [N]}, which supplies the values for each
- * element in indices. For example, given {@code indices=[[1,3], [2,4]]}, the parameter {@code
- * values=[18, 3]} specifies that element {@code [1,3]} of the sparse NdArray has a value of
- * {@code 18}, and element {@code [2,4]} of the NdArray has a value of {@code 3}.
+ * element in indices. For example, given {@code indices=[[1,3,1], [2,4,0]]}, the parameter {@code
+ * values=[18, 3]} specifies that element {@code [1,3,1]} of the sparse NdArray has a value of
+ * {@code 18}, and element {@code [2,4,0]} of the NdArray has a value of {@code 3}.
* @param shape the shape of the dense array represented by this sparse array.
* @return the short sparse array.
*/
@@ -405,12 +405,12 @@ public static ShortSparseNdArray sparseOf(LongNdArray indices, ShortNdArray valu
*
* @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the
* elements in the sparse array that contain non-default values (elements are zero-indexed).
- * For example, {@code indices=[[1,3], [2,4]]} specifies that the elements with indexes of
- * {@code [1,3]} and {@code [2,4]} have non-default values.
+ * For example, {@code indices=[[1,3,1], [2,4,0]]} specifies that the elements with indexes of
+ * {@code [1,3,1]} and {@code [2,4,0]} have non-default values.
* @param values A 1-D ShortNdArray of shape {@code [N]}, which supplies the values for each
- * element in indices. For example, given {@code indices=[[1,3], [2,4]]}, the parameter {@code
- * values=[18, 3]} specifies that element {@code [1,3]} of the sparse NdArray has a value of
- * {@code 18}, and element {@code [2,4]} of the NdArray has a value of {@code 3}.
+ * element in indices. For example, given {@code indices=[[1,3,1], [2,4,0]]}, the parameter {@code
+ * values=[18, 3]} specifies that element {@code [1,3,1]} of the sparse NdArray has a value of
+ * {@code 18}, and element {@code [2,4,0]} of the NdArray has a value of {@code 3}.
* @param defaultValue Scalar value to set for indices not specified in 'indices'
* @param shape the shape of the dense array represented by this sparse array.
* @return the short sparse array.
@@ -480,12 +480,12 @@ public static FloatNdArray wrap(Shape shape, FloatDataBuffer buffer) {
*
* @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the
* elements in the sparse array that contain non-default values (elements are zero-indexed).
- * For example, {@code indices=[[1,3], [2,4]]} specifies that the elements with indexes of
- * {@code [1,3]} and {@code [2,4]} have non-default values.
+ * For example, {@code indices=[[1,3,1], [2,4,0]]} specifies that the elements with indexes of
+ * {@code [1,3,1]} and {@code [2,4,0]} have non-default values.
* @param values A 1-D FloatNdArray of shape {@code [N]}, which supplies the values for each
- * element in indices. For example, given {@code indices=[[1,3], [2,4]]}, the parameter {@code
- * values=[18, 3.8]} specifies that element {@code [1,3]} of the sparse NdArray has a value of
- * {@code 18}, and element {@code [2,4]} of the NdArray has a value of {@code 3.8}.
+ * element in indices. For example, given {@code indices=[[1,3,1], [2,4,0]]}, the parameter {@code
+ * values=[18f, 3.8f]} specifies that element {@code [1,3,1]} of the sparse NdArray has a value of
+ * {@code 18f}, and element {@code [2,4,0]} of the NdArray has a value of {@code 3.8f}.
* @param shape the shape of the dense array represented by this sparse array.
* @return the float sparse array.
*/
@@ -498,12 +498,12 @@ public static FloatSparseNdArray sparseOf(LongNdArray indices, FloatNdArray valu
*
* @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the
* elements in the sparse array that contain non-default values (elements are zero-indexed).
- * For example, {@code indices=[[1,3], [2,4]]} specifies that the elements with indexes of
- * {@code [1,3]} and {@code [2,4]} have non-default values.
+ * For example, {@code indices=[[1,3,1], [2,4,0]]} specifies that the elements with indexes of
+ * {@code [1,3,1]} and {@code [2,4,0]} have non-default values.
* @param values A 1-D FloatNdArray of shape {@code [N]}, which supplies the values for each
- * element in indices. For example, given {@code indices=[[1,3], [2,4]]}, the parameter {@code
- * values=[18, 3.8]} specifies that element {@code [1,3]} of the sparse NdArray has a value of
- * {@code 18}, and element {@code [2,4]} of the NdArray has a value of {@code 3.8}.
+ * element in indices. For example, given {@code indices=[[1,3,1], [2,4,0]]}, the parameter {@code
+ * values=[18f, 3.8f]} specifies that element {@code [1,3,1]} of the sparse NdArray has a value of
+ * {@code 18f}, and element {@code [2,4,0]} of the NdArray has a value of {@code 3.8f}.
* @param defaultValue Scalar value to set for indices not specified in 'indices'
* @param shape the shape of the dense array represented by this sparse array.
* @return the float sparse array.
@@ -573,12 +573,12 @@ public static DoubleNdArray wrap(Shape shape, DoubleDataBuffer buffer) {
*
* @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the
* elements in the sparse array that contain non-default values (elements are zero-indexed).
- * For example, {@code indices=[[1,3], [2,4]]} specifies that the elements with indexes of
- * {@code [1,3]} and {@code [2,4]} have non-default values.
+ * For example, {@code indices=[[1,3,1], [2,4,0]]} specifies that the elements with indexes of
+ * {@code [1,3,1]} and {@code [2,4,0]} have non-default values.
* @param values A 1-D DoubleNdArray of shape {@code [N]}, which supplies the values for each
- * element in indices. For example, given {@code indices=[[1,3], [2,4]]}, the parameter {@code
- * values=[18, 3.8]} specifies that element {@code [1,3]} of the sparse NdArray has a value of
- * {@code 18}, and element {@code [2,4]} of the NdArray has a value of {@code 3.8}.
+ * element in indices. For example, given {@code indices=[[1,3,1], [2,4,0]]}, the parameter {@code
+ * values=[18, 3.8]} specifies that element {@code [1,3,1]} of the sparse NdArray has a value of
+ * {@code 18}, and element {@code [2,4,0]} of the NdArray has a value of {@code 3.8}.
* @param shape the shape of the dense array represented by this sparse array.
* @return the float sparse array.
*/
@@ -592,12 +592,12 @@ public static DoubleSparseNdArray sparseOf(
*
* @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the
* elements in the sparse array that contain non-default values (elements are zero-indexed).
- * For example, {@code indices=[[1,3], [2,4]]} specifies that the elements with indexes of
- * {@code [1,3]} and {@code [2,4]} have non-default values.
+ * For example, {@code indices=[[1,3,1], [2,4,0]]} specifies that the elements with indexes of
+ * {@code [1,3,1]} and {@code [2,4,0]} have non-default values.
* @param values A 1-D DoubleNdArray of shape {@code [N]}, which supplies the values for each
- * element in indices. For example, given {@code indices=[[1,3], [2,4]]}, the parameter {@code
- * values=[18, 3.8]} specifies that element {@code [1,3]} of the sparse NdArray has a value of
- * {@code 18}, and element {@code [2,4]} of the NdArray has a value of {@code 3.8}.
+ * element in indices. For example, given {@code indices=[[1,3,1], [2,4,0]]}, the parameter {@code
+ * values=[18, 3.8]} specifies that element {@code [1,3,1]} of the sparse NdArray has a value of
+ * {@code 18}, and element {@code [2,4,0]} of the NdArray has a value of {@code 3.8}.
* @param defaultValue Scalar value to set for indices not specified in 'indices'
* @param shape the shape of the dense array represented by this sparse array.
* @return the float sparse array.
@@ -668,12 +668,12 @@ public static BooleanNdArray wrap(Shape shape, BooleanDataBuffer buffer) {
*
* @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the
* elements in the sparse array that contain non-default values (elements are zero-indexed).
- * For example, {@code indices=[[1,3], [2,4]]} specifies that the elements with indexes of
- * {@code [1,3]} and {@code [2,4]} have non-default values.
+ * For example, {@code indices=[[1,3,1], [2,4,0]]} specifies that the elements with indexes of
+ * {@code [1,3,1]} and {@code [2,4,0]} have non-default values.
* @param values A 1-D BooleanNdArray of shape {@code [N]}, which supplies the values for each
- * element in indices. For example, given {@code indices=[[1,3], [2,4]]}, the parameter {@code
- * values=[true, true]} specifies that element {@code [1,3]} of the sparse NdArray has a value
- * of true, and element {@code [2,4]} of the NdArray has a value of true. All other values are
+ * element in indices. For example, given {@code indices=[[1,3,1], [2,4,0]]}, the parameter {@code
+ * values=[true, true]} specifies that element {@code [1,3,1]} of the sparse NdArray has a value
+ * of true, and element {@code [2,4,0]} of the NdArray has a value of true. All other values are
* false.
* @param shape the shape of the dense array represented by this sparse array.
* @return the float sparse array.
@@ -688,12 +688,12 @@ public static BooleanSparseNdArray sparseOf(
*
* @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the
* elements in the sparse array that contain non-default values (elements are zero-indexed).
- * For example, {@code indices=[[1,3], [2,4]]} specifies that the elements with indexes of
- * {@code [1,3]} and {@code [2,4]} have non-default values.
+ * For example, {@code indices=[[1,3,1], [2,4,0]]} specifies that the elements with indexes of
+ * {@code [1,3,1]} and {@code [2,4,0]} have non-default values.
* @param values A 1-D BooleanNdArray of shape {@code [N]}, which supplies the values for each
- * element in indices. For example, given {@code indices=[[1,3], [2,4]]}, the parameter {@code
- * values=[true, true]} specifies that element {@code [1,3]} of the sparse NdArray has a value
- * of true, and element {@code [2,4]} of the NdArray has a value of true. All other values are
+ * element in indices. For example, given {@code indices=[[1,3,1], [2,4,0]]}, the parameter {@code
+ * values=[true, true]} specifies that element {@code [1,3,1]} of the sparse NdArray has a value
+ * of true, and element {@code [2,4,0]} of the NdArray has a value of true. All other values are
* false.
* @param defaultValue Scalar value to set for indices not specified in 'indices'
* @param shape the shape of the dense array represented by this sparse array.
@@ -776,12 +776,12 @@ public static NdArray wrap(Shape shape, DataBuffer buffer) {
* @param type the class type represented by this sparse array.
* @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the
* elements in the sparse array that contain non-default values (elements are zero-indexed).
- * For example, {@code indices=[[1,3], [2,4]]} specifies that the elements with indexes of
- * {@code [1,3]} and {@code [2,4]} have non-default values.
+ * For example, {@code indices=[[1,3,1], [2,4,0]]} specifies that the elements with indexes of
+ * {@code [1,3,1]} and {@code [2,4,0]} have non-default values.
* @param values A 1-D NdArray of shape {@code [N]}, which supplies the values for each element in
- * indices. For example, given {@code indices=[[1,3], [2,4]]}, the parameter {@code
- * values=["one", "two"]} specifies that element {@code [1,3]} of the sparse NdArray has a
- * value of "one", and element {@code [2,4]} of the NdArray has a value of "two"". All other
+ * indices. For example, given {@code indices=[[1,3,1], [2,4,0]]}, the parameter {@code
+ * values=["one", "two"]} specifies that element {@code [1,3,1]} of the sparse NdArray has a
+ * value of "one", and element {@code [2,4,0]} of the NdArray has a value of "two"". All other
* values are null.
* @param shape the shape of the dense array represented by this sparse array.
* @return the float sparse array.
@@ -798,12 +798,12 @@ public static NdArray sparseOfObjects(
* @param type the class type represented by this sparse array.
* @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the
* elements in the sparse array that contain non-default values (elements are zero-indexed).
- * For example, {@code indices=[[1,3], [2,4]]} specifies that the elements with indexes of
- * {@code [1,3]} and {@code [2,4]} have non-default values.
+ * For example, {@code indices=[[1,3,1], [2,4,0]]} specifies that the elements with indexes of
+ * {@code [1,3,1]} and {@code [2,4,0]} have non-default values.
* @param values A 1-D NdArray of shape {@code [N]}, which supplies the values for each element in
- * indices. For example, given {@code indices=[[1,3], [2,4]]}, the parameter {@code
- * values=["one", "two"]} specifies that element {@code [1,3]} of the sparse NdArray has a
- * value of "one", and element {@code [2,4]} of the NdArray has a value of "two"". All other
+ * indices. For example, given {@code indices=[[1,3,1], [2,4,0]]}, the parameter {@code
+ * values=["one", "two"]} specifies that element {@code [1,3,1]} of the sparse NdArray has a
+ * value of "one", and element {@code [2,4,0]} of the NdArray has a value of "two"". All other
* values are null.
* @param defaultValue Scalar value to set for indices not specified in 'indices'
* @param shape the shape of the dense array represented by this sparse array.
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/buffer/raw/UnsafeReference.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/buffer/raw/UnsafeReference.java
index 7b95eac..a8cc16d 100644
--- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/buffer/raw/UnsafeReference.java
+++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/buffer/raw/UnsafeReference.java
@@ -36,24 +36,24 @@ static boolean isAvailable() {
theUnsafe.setAccessible(true);
Object instance = theUnsafe.get(null);
if (instance.getClass() == clazz) {
- // Validate that this Unsafe instance exposes all methods we need
- clazz.getDeclaredMethod("getByte", Object.class, long.class);
- clazz.getDeclaredMethod("putByte", Object.class, long.class, byte.class);
- clazz.getDeclaredMethod("getShort", Object.class, long.class);
- clazz.getDeclaredMethod("putShort", Object.class, long.class, short.class);
- clazz.getDeclaredMethod("getInt", Object.class, long.class);
- clazz.getDeclaredMethod("putInt", Object.class, long.class, int.class);
- clazz.getDeclaredMethod("getLong", Object.class, long.class);
- clazz.getDeclaredMethod("putLong", Object.class, long.class, long.class);
- clazz.getDeclaredMethod("getFloat", Object.class, long.class);
- clazz.getDeclaredMethod("putFloat", Object.class, long.class, float.class);
- clazz.getDeclaredMethod("getDouble", Object.class, long.class);
- clazz.getDeclaredMethod("putDouble", Object.class, long.class, double.class);
- clazz.getDeclaredMethod("getBoolean", Object.class, long.class);
- clazz.getDeclaredMethod("putBoolean", Object.class, long.class, boolean.class);
- clazz.getDeclaredMethod("copyMemory", Object.class, long.class, Object.class, long.class, long.class);
- clazz.getDeclaredMethod("arrayBaseOffset", Class.class);
- clazz.getDeclaredMethod("arrayIndexScale", Class.class);
+ checkMethod(clazz, "getByte", Object.class, long.class);
+ checkMethod(clazz, "putByte", Object.class, long.class, byte.class);
+ checkMethod(clazz, "getShort", Object.class, long.class);
+ checkMethod(clazz, "putShort", Object.class, long.class, short.class);
+ checkMethod(clazz, "getInt", Object.class, long.class);
+ checkMethod(clazz, "putInt", Object.class, long.class, int.class);
+ checkMethod(clazz, "getLong", Object.class, long.class);
+ checkMethod(clazz, "putLong", Object.class, long.class, long.class);
+ checkMethod(clazz, "getFloat", Object.class, long.class);
+ checkMethod(clazz, "putFloat", Object.class, long.class, float.class);
+ checkMethod(clazz, "getDouble", Object.class, long.class);
+ checkMethod(clazz, "putDouble", Object.class, long.class, double.class);
+ checkMethod(clazz, "getBoolean", Object.class, long.class);
+ checkMethod(clazz, "putBoolean", Object.class, long.class, boolean.class);
+ checkMethod(clazz, "copyMemory", Object.class, long.class, Object.class, long.class, long.class);
+ checkMethod(clazz, "arrayBaseOffset", Class.class);
+ checkMethod(clazz, "arrayIndexScale", Class.class);
+
unsafe = (Unsafe) instance;
}
} catch (ClassNotFoundException | NoSuchMethodException | NoSuchFieldException | SecurityException | IllegalAccessException | ClassCastException ex) {
@@ -61,4 +61,14 @@ static boolean isAvailable() {
}
UNSAFE = unsafe;
}
+
+ /**
+ * Validate that this Unsafe instance exposes this method
+ *
+ * ErrorProne does not like that we do nothing with the returned method... but there is nothing to do with it, so disable the check
+ */
+ @SuppressWarnings("ReturnValueIgnored")
+ private static void checkMethod(Class> unsafeClass, String methodName, Class>... parameterTypes) throws NoSuchMethodException {
+ unsafeClass.getDeclaredMethod(methodName, parameterTypes);
+ }
}
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 0497095..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;
}
@@ -132,13 +144,22 @@ public boolean equals(Object obj) {
return buffer().equals(other.buffer());
}
+ /**
+ * A String showing the type and shape of this dense ndarray.
+ * @return A string containing the type and shape.
+ */
+ @Override
+ public String toString() {
+ return this.getClass().getSimpleName() + "(shape=" + this.shape() + ")";
+ }
+
protected AbstractDenseNdArray(DimensionalSpace dimensions) {
super(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 daffad9..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) {
@@ -402,6 +407,26 @@ public boolean equals(Object obj) {
return values.equals(other.values);
}
+ /**
+ * A String showing the type, default value, number of elements and
+ * the dense shape of this sparse ndarray.
+ * @return A string containing the type, default value, number of elements and shape.
+ */
+ @Override
+ public String toString() {
+ long numElements = values == null ? 0 : values.size();
+ String strDefault;
+ if (defaultValue == null) {
+ strDefault = "";
+ } else if (defaultValue instanceof Number) {
+ strDefault = defaultValue.toString();
+ } else {
+ strDefault = "'" + defaultValue + "'";
+ }
+ return this.getClass().getSimpleName() + "(defaultValue=" + strDefault
+ + ", numElements=" + numElements + ", shape=" + this.shape() + ")";
+ }
+
/**
* Performs a binary search on the indices array to locate the index of the specified coordinates.
* The indices array must be sorted by coordinates, row major.
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 f9e41bf..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
@@ -63,7 +63,7 @@ public class BooleanSparseNdArray extends AbstractSparseNdArray 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 540c79f..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
@@ -63,7 +63,7 @@ public class ByteSparseNdArray extends AbstractSparseNdArray
implements ByteNdArray {
/**
- * Creates a ByteSparseNdArray
+ * Creates a ByteSparseNdArray with a default value of zero.
*
* @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the
* elements in the sparse array that contain non-default values (elements are zero-indexed).
@@ -73,14 +73,16 @@ public class ByteSparseNdArray extends AbstractSparseNdArray
* each element in indices. For example, given {@code indices=[[1,3], [2,4]]}, the parameter
* {@code values=[18, 3.6]} specifies that element {@code [1,3]} of the sparse NdArray has a
* value of {@code 18}, and element {@code [2,4]} of the NdArray has a value of {@code 3.6}.
+ * @param defaultValue Scalar value to set for indices not specified in {@link #getIndices()}
* @param dimensions the dimensional space for the dense object represented by this sparse array,
*/
- ByteSparseNdArray(LongNdArray indices, ByteNdArray values, DimensionalSpace dimensions) {
- this(indices, values, (byte) 0, dimensions);
+ protected ByteSparseNdArray(
+ LongNdArray indices, ByteNdArray values, byte defaultValue, DimensionalSpace dimensions) {
+ super(indices, values, defaultValue, dimensions);
}
/**
- * Creates a ByteSparseNdArray with a default value of zero.
+ * Creates a ByteSparseNdArray
*
* @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the
* elements in the sparse array that contain non-default values (elements are zero-indexed).
@@ -90,12 +92,10 @@ public class ByteSparseNdArray extends AbstractSparseNdArray
* each element in indices. For example, given {@code indices=[[1,3], [2,4]]}, the parameter
* {@code values=[18, 3.6]} specifies that element {@code [1,3]} of the sparse NdArray has a
* value of {@code 18}, and element {@code [2,4]} of the NdArray has a value of {@code 3.6}.
- * @param defaultValue Scalar value to set for indices not specified in {@link #getIndices()}
* @param dimensions the dimensional space for the dense object represented by this sparse array,
*/
- ByteSparseNdArray(
- LongNdArray indices, ByteNdArray values, byte defaultValue, DimensionalSpace dimensions) {
- super(indices, values, defaultValue, dimensions);
+ ByteSparseNdArray(LongNdArray indices, ByteNdArray values, DimensionalSpace dimensions) {
+ this(indices, values, (byte) 0, dimensions);
}
/**
@@ -118,7 +118,7 @@ public class ByteSparseNdArray extends AbstractSparseNdArray
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 27a0832..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
@@ -63,7 +63,7 @@ public class DoubleSparseNdArray extends AbstractSparseNdArray 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 11a3b05..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
@@ -63,7 +63,7 @@ public class FloatSparseNdArray extends AbstractSparseNdArray 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 f822a42..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
@@ -63,7 +63,7 @@ public class IntSparseNdArray extends AbstractSparseNdArray
implements IntNdArray {
/**
- * Creates a IntSparseNdArray with a default value of zero.
+ * Creates a IntSparseNdArray
*
* @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the
* elements in the sparse array that contain non-default values (elements are zero-indexed).
@@ -73,14 +73,16 @@ public class IntSparseNdArray extends AbstractSparseNdArray
* in indices. For example, given {@code indices=[[1,3], [2,4]]}, the parameter {@code
* values=[18, 3.6]} specifies that element {@code [1,3]} of the sparse NdArray has a value of
* {@code 18}, and element {@code [2,4]} of the NdArray has a value of {@code 3.6}.
+ * @param defaultValue Scalar value to set for indices not specified in {@link #getIndices()}
* @param dimensions the dimensional space for the dense object represented by this sparse array,
*/
- IntSparseNdArray(LongNdArray indices, IntNdArray values, DimensionalSpace dimensions) {
- this(indices, values, 0, dimensions);
+ protected IntSparseNdArray(
+ LongNdArray indices, IntNdArray values, int defaultValue, DimensionalSpace dimensions) {
+ super(indices, values, defaultValue, dimensions);
}
/**
- * Creates a IntSparseNdArray
+ * Creates a IntSparseNdArray with a default value of zero.
*
* @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the
* elements in the sparse array that contain non-default values (elements are zero-indexed).
@@ -90,12 +92,10 @@ public class IntSparseNdArray extends AbstractSparseNdArray
* in indices. For example, given {@code indices=[[1,3], [2,4]]}, the parameter {@code
* values=[18, 3.6]} specifies that element {@code [1,3]} of the sparse NdArray has a value of
* {@code 18}, and element {@code [2,4]} of the NdArray has a value of {@code 3.6}.
- * @param defaultValue Scalar value to set for indices not specified in {@link #getIndices()}
* @param dimensions the dimensional space for the dense object represented by this sparse array,
*/
- IntSparseNdArray(
- LongNdArray indices, IntNdArray values, int defaultValue, DimensionalSpace dimensions) {
- super(indices, values, defaultValue, dimensions);
+ IntSparseNdArray(LongNdArray indices, IntNdArray values, DimensionalSpace dimensions) {
+ this(indices, values, 0, dimensions);
}
/**
@@ -116,7 +116,7 @@ public class IntSparseNdArray extends AbstractSparseNdArray
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 3385022..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
@@ -62,7 +62,7 @@ public class LongSparseNdArray extends AbstractSparseNdArray
implements LongNdArray {
/**
- * Creates a LongSparseNdArray with a default value of zero.
+ * Creates a LongSparseNdArray
*
* @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the
* elements in the sparse array that contain non-default values (elements are zero-indexed).
@@ -72,14 +72,16 @@ public class LongSparseNdArray extends AbstractSparseNdArray
* element in indices. For example, given {@code indices=[[1,3], [2,4]]}, the parameter {@code
* values=[18, 3.6]} specifies that element {@code [1,3]} of the sparse NdArray has a value of
* {@code 18}, and element {@code [2,4]} of the NdArray has a value of {@code 3.6}.
+ * @param defaultValue Scalar value to set for indices not specified in {@link #getIndices()}
* @param dimensions the dimensional space for the dense object represented by this sparse array,
*/
- LongSparseNdArray(LongNdArray indices, LongNdArray values, DimensionalSpace dimensions) {
- this(indices, values, 0L, dimensions);
+ protected LongSparseNdArray(
+ LongNdArray indices, LongNdArray values, long defaultValue, DimensionalSpace dimensions) {
+ super(indices, values, defaultValue, dimensions);
}
/**
- * Creates a LongSparseNdArray
+ * Creates a LongSparseNdArray with a default value of zero.
*
* @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the
* elements in the sparse array that contain non-default values (elements are zero-indexed).
@@ -89,12 +91,10 @@ public class LongSparseNdArray extends AbstractSparseNdArray
* element in indices. For example, given {@code indices=[[1,3], [2,4]]}, the parameter {@code
* values=[18, 3.6]} specifies that element {@code [1,3]} of the sparse NdArray has a value of
* {@code 18}, and element {@code [2,4]} of the NdArray has a value of {@code 3.6}.
- * @param defaultValue Scalar value to set for indices not specified in {@link #getIndices()}
* @param dimensions the dimensional space for the dense object represented by this sparse array,
*/
- LongSparseNdArray(
- LongNdArray indices, LongNdArray values, long defaultValue, DimensionalSpace dimensions) {
- super(indices, values, defaultValue, dimensions);
+ LongSparseNdArray(LongNdArray indices, LongNdArray values, DimensionalSpace dimensions) {
+ this(indices, values, 0L, dimensions);
}
/**
@@ -117,7 +117,7 @@ public class LongSparseNdArray extends AbstractSparseNdArray
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 50793b9..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
@@ -63,7 +63,7 @@ public class ShortSparseNdArray extends AbstractSparseNdArray 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 2c69fc9..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
@@ -63,7 +63,7 @@ public class SparseNdArray> extends AbstractSparseNdArra
private final Class type;
/**
- * Creates a SparseNdArray with a default value of null.
+ * Creates a SparseNdArray
*
* @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the
* elements in the sparse array that contain non-default values (elements are zero-indexed).
@@ -74,14 +74,17 @@ public class SparseNdArray> extends AbstractSparseNdArra
* parameter {@code values=[18, 3.6]} specifies that element {@code [1,3]} of the sparse
* NdArray has a value of {@code 18}, and element {@code [2,4]} of the NdArray has a value of
* {@code 3.6}.
+ * @param defaultValue Scalar value to set for indices not specified in {@link #getIndices()}
* @param dimensions the dimensional space for the dense object represented by this sparse array,
*/
- SparseNdArray(Class type, LongNdArray indices, U values, DimensionalSpace dimensions) {
- this(type, indices, values, null, dimensions);
+ protected SparseNdArray(
+ Class type, LongNdArray indices, U values, T defaultValue, DimensionalSpace dimensions) {
+ super(indices, values, defaultValue, dimensions);
+ this.type = type;
}
/**
- * Creates a SparseNdArray
+ * Creates a SparseNdArray with a default value of null.
*
* @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the
* elements in the sparse array that contain non-default values (elements are zero-indexed).
@@ -92,13 +95,10 @@ public class SparseNdArray> extends AbstractSparseNdArra
* parameter {@code values=[18, 3.6]} specifies that element {@code [1,3]} of the sparse
* NdArray has a value of {@code 18}, and element {@code [2,4]} of the NdArray has a value of
* {@code 3.6}.
- * @param defaultValue Scalar value to set for indices not specified in {@link #getIndices()}
* @param dimensions the dimensional space for the dense object represented by this sparse array,
*/
- SparseNdArray(
- Class type, LongNdArray indices, U values, T defaultValue, DimensionalSpace dimensions) {
- super(indices, values, defaultValue, dimensions);
- this.type = type;
+ SparseNdArray(Class type, LongNdArray indices, U values, DimensionalSpace dimensions) {
+ this(type, indices, values, null, dimensions);
}
/**
@@ -123,7 +123,7 @@ public class SparseNdArray> extends AbstractSparseNdArra
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;
}
@@ -394,4 +394,25 @@ public NdArray fromDense(NdArray src) {
public Class getType() {
return type;
}
+
+ /**
+ * A String showing the type, default value, number of elements and
+ * the dense shape of this sparse ndarray.
+ * @return A string containing the type, default value, number of elements and shape.
+ */
+ @Override
+ public String toString() {
+ long numElements = getValues() == null ? 0 : getValues().size();
+ String strDefault;
+ T defaultVal = getDefaultValue();
+ if (defaultVal == null) {
+ strDefault = "";
+ } else if (defaultVal instanceof Number) {
+ strDefault = defaultVal.toString();
+ } else {
+ strDefault = "'" + defaultVal + "'";
+ }
+ return this.getClass().getSimpleName() + "(type="+type.getSimpleName()+", defaultValue=" + strDefault
+ + ", numElements=" + numElements + ", shape=" + this.shape() + ")";
+ }
}
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