Skip to content

Commit 51ac2c0

Browse files
authored
Adding toString to AbstractDenseNdArray and AbstractSparseNdArray (#8)
1 parent 75df6d0 commit 51ac2c0

File tree

7 files changed

+95
-0
lines changed

7 files changed

+95
-0
lines changed

ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/AbstractDenseNdArray.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,15 @@ public boolean equals(Object obj) {
132132
return buffer().equals(other.buffer());
133133
}
134134

135+
/**
136+
* A String showing the type and shape of this dense ndarray.
137+
* @return A string containing the type and shape.
138+
*/
139+
@Override
140+
public String toString() {
141+
return this.getClass().getSimpleName() + "(shape=" + this.shape() + ")";
142+
}
143+
135144
protected AbstractDenseNdArray(DimensionalSpace dimensions) {
136145
super(dimensions);
137146
}

ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/AbstractSparseNdArray.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,26 @@ public boolean equals(Object obj) {
402402
return values.equals(other.values);
403403
}
404404

405+
/**
406+
* A String showing the type, default value, number of elements and
407+
* the dense shape of this sparse ndarray.
408+
* @return A string containing the type, default value, number of elements and shape.
409+
*/
410+
@Override
411+
public String toString() {
412+
long numElements = values == null ? 0 : values.size();
413+
String strDefault;
414+
if (defaultValue == null) {
415+
strDefault = "<null>";
416+
} else if (defaultValue instanceof Number) {
417+
strDefault = defaultValue.toString();
418+
} else {
419+
strDefault = "'" + defaultValue + "'";
420+
}
421+
return this.getClass().getSimpleName() + "(defaultValue=" + strDefault
422+
+ ", numElements=" + numElements + ", shape=" + this.shape() + ")";
423+
}
424+
405425
/**
406426
* Performs a binary search on the indices array to locate the index of the specified coordinates.
407427
* The indices array must be sorted by coordinates, row major.

ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/SparseNdArray.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,4 +394,25 @@ public NdArray<T> fromDense(NdArray<T> src) {
394394
public Class<T> getType() {
395395
return type;
396396
}
397+
398+
/**
399+
* A String showing the type, default value, number of elements and
400+
* the dense shape of this sparse ndarray.
401+
* @return A string containing the type, default value, number of elements and shape.
402+
*/
403+
@Override
404+
public String toString() {
405+
long numElements = getValues() == null ? 0 : getValues().size();
406+
String strDefault;
407+
T defaultVal = getDefaultValue();
408+
if (defaultVal == null) {
409+
strDefault = "<null>";
410+
} else if (defaultVal instanceof Number) {
411+
strDefault = defaultVal.toString();
412+
} else {
413+
strDefault = "'" + defaultVal + "'";
414+
}
415+
return this.getClass().getSimpleName() + "(type="+type.getSimpleName()+", defaultValue=" + strDefault
416+
+ ", numElements=" + numElements + ", shape=" + this.shape() + ")";
417+
}
397418
}

ndarray/src/test/java/org/tensorflow/ndarray/impl/dense/BooleanDenseNdArrayTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
package org.tensorflow.ndarray.impl.dense;
1818

19+
import org.junit.jupiter.api.Assertions;
20+
import org.junit.jupiter.api.Test;
1921
import org.tensorflow.ndarray.Shape;
2022
import org.tensorflow.ndarray.buffer.DataBuffer;
2123
import org.tensorflow.ndarray.buffer.DataBuffers;
@@ -32,4 +34,12 @@ public class BooleanDenseNdArrayTest extends BooleanNdArrayTestBase {
3234
@Override protected DataBuffer<Boolean> allocateBuffer(long size) {
3335
return DataBuffers.ofBooleans(size);
3436
}
37+
38+
@Test
39+
public void testToString() {
40+
BooleanNdArray matrix3d = allocate(Shape.of(5, 4, 5));
41+
Assertions.assertEquals("BooleanDenseNdArray(shape=[5, 4, 5])",matrix3d.toString());
42+
BooleanNdArray scalar = allocate(Shape.of());
43+
Assertions.assertEquals("BooleanDenseNdArray(shape=[])",scalar.toString());
44+
}
3545
}

ndarray/src/test/java/org/tensorflow/ndarray/impl/dense/DoubleDenseNdArrayTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
package org.tensorflow.ndarray.impl.dense;
1818

19+
import org.junit.jupiter.api.Assertions;
20+
import org.junit.jupiter.api.Test;
1921
import org.tensorflow.ndarray.Shape;
2022
import org.tensorflow.ndarray.buffer.DataBuffer;
2123
import org.tensorflow.ndarray.buffer.DataBuffers;
@@ -32,4 +34,14 @@ public class DoubleDenseNdArrayTest extends DoubleNdArrayTestBase {
3234
@Override protected DataBuffer<Double> allocateBuffer(long size) {
3335
return DataBuffers.ofDoubles(size);
3436
}
37+
38+
@Test
39+
public void testToString() {
40+
DoubleNdArray matrix3d = allocate(Shape.of(5, 4, 5));
41+
Assertions.assertEquals("DoubleDenseNdArray(shape=[5, 4, 5])",matrix3d.toString());
42+
DoubleNdArray vector = allocate(Shape.of(5));
43+
Assertions.assertEquals("DoubleDenseNdArray(shape=[5])",vector.toString());
44+
DoubleNdArray scalar = allocate(Shape.of());
45+
Assertions.assertEquals("DoubleDenseNdArray(shape=[])",scalar.toString());
46+
}
3547
}

ndarray/src/test/java/org/tensorflow/ndarray/impl/sparse/DoubleSparseNdArrayTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.tensorflow.ndarray.impl.sparse;
22

3+
import org.junit.jupiter.api.Assertions;
34
import org.junit.jupiter.api.Test;
45
import org.tensorflow.ndarray.DoubleNdArray;
56
import org.tensorflow.ndarray.LongNdArray;
@@ -302,4 +303,15 @@ public void testSlice() {
302303
.forEachIndexed(
303304
(lidx, f) -> assertEquals(expected[i.getAndIncrement()], f.getDouble())));
304305
}
306+
307+
@Test
308+
public void testToString() {
309+
DoubleNdArray ndArray = StdArrays.ndCopyOf(dense2DArray);
310+
DoubleSparseNdArray instance =
311+
DoubleSparseNdArray.create(DimensionalSpace.create(ndArray.shape()));
312+
instance.fromDense(ndArray);
313+
Assertions.assertEquals("DoubleSparseNdArray(defaultValue=0.0, numElements=2, shape=[3, 4])",instance.toString());
314+
DoubleSparseNdArray empty = DoubleSparseNdArray.create(DimensionalSpace.create(Shape.of(5)));
315+
Assertions.assertEquals("DoubleSparseNdArray(defaultValue=0.0, numElements=0, shape=[5])",empty.toString());
316+
}
305317
}

ndarray/src/test/java/org/tensorflow/ndarray/impl/sparse/StringSparseNdArrayTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
=======================================================================*/
1515
package org.tensorflow.ndarray.impl.sparse;
1616

17+
import org.junit.jupiter.api.Assertions;
1718
import org.junit.jupiter.api.Test;
1819
import org.tensorflow.ndarray.LongNdArray;
1920
import org.tensorflow.ndarray.NdArray;
@@ -338,4 +339,14 @@ public void testNullDefault() {
338339
assertNotNull(dArray.getObject());
339340
assertEquals("a default", dArray.getObject());
340341
}
342+
343+
@Test
344+
public void testToString() {
345+
SparseNdArray<String, NdArray<String>> instance =
346+
new SparseNdArray<>(String.class, indices, values, DimensionalSpace.create(shape));
347+
Assertions.assertEquals("SparseNdArray(type=String, defaultValue=<null>, numElements=2, shape=[3, 4])",instance.toString());
348+
instance = new SparseNdArray<>(
349+
String.class, indices, values, "a default", DimensionalSpace.create(shape));
350+
Assertions.assertEquals("SparseNdArray(type=String, defaultValue='a default', numElements=2, shape=[3, 4])",instance.toString());
351+
}
341352
}

0 commit comments

Comments
 (0)