Skip to content
This repository was archived by the owner on Jul 15, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@ 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,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 = "<null>";
} 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,4 +394,25 @@ public NdArray<T> fromDense(NdArray<T> src) {
public Class<T> 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 = "<null>";
} 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() + ")";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.tensorflow.ndarray.impl.dense;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.tensorflow.ndarray.Shape;
import org.tensorflow.ndarray.buffer.DataBuffer;
import org.tensorflow.ndarray.buffer.DataBuffers;
Expand All @@ -32,4 +34,12 @@ public class BooleanDenseNdArrayTest extends BooleanNdArrayTestBase {
@Override protected DataBuffer<Boolean> allocateBuffer(long size) {
return DataBuffers.ofBooleans(size);
}

@Test
public void testToString() {
BooleanNdArray matrix3d = allocate(Shape.of(5, 4, 5));
Assertions.assertEquals("BooleanDenseNdArray(shape=[5, 4, 5])",matrix3d.toString());
BooleanNdArray scalar = allocate(Shape.of());
Assertions.assertEquals("BooleanDenseNdArray(shape=[])",scalar.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.tensorflow.ndarray.impl.dense;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.tensorflow.ndarray.Shape;
import org.tensorflow.ndarray.buffer.DataBuffer;
import org.tensorflow.ndarray.buffer.DataBuffers;
Expand All @@ -32,4 +34,14 @@ public class DoubleDenseNdArrayTest extends DoubleNdArrayTestBase {
@Override protected DataBuffer<Double> allocateBuffer(long size) {
return DataBuffers.ofDoubles(size);
}

@Test
public void testToString() {
DoubleNdArray matrix3d = allocate(Shape.of(5, 4, 5));
Assertions.assertEquals("DoubleDenseNdArray(shape=[5, 4, 5])",matrix3d.toString());
DoubleNdArray vector = allocate(Shape.of(5));
Assertions.assertEquals("DoubleDenseNdArray(shape=[5])",vector.toString());
DoubleNdArray scalar = allocate(Shape.of());
Assertions.assertEquals("DoubleDenseNdArray(shape=[])",scalar.toString());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.tensorflow.ndarray.impl.sparse;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.tensorflow.ndarray.DoubleNdArray;
import org.tensorflow.ndarray.LongNdArray;
Expand Down Expand Up @@ -302,4 +303,15 @@ public void testSlice() {
.forEachIndexed(
(lidx, f) -> assertEquals(expected[i.getAndIncrement()], f.getDouble())));
}

@Test
public void testToString() {
DoubleNdArray ndArray = StdArrays.ndCopyOf(dense2DArray);
DoubleSparseNdArray instance =
DoubleSparseNdArray.create(DimensionalSpace.create(ndArray.shape()));
instance.fromDense(ndArray);
Assertions.assertEquals("DoubleSparseNdArray(defaultValue=0.0, numElements=2, shape=[3, 4])",instance.toString());
DoubleSparseNdArray empty = DoubleSparseNdArray.create(DimensionalSpace.create(Shape.of(5)));
Assertions.assertEquals("DoubleSparseNdArray(defaultValue=0.0, numElements=0, shape=[5])",empty.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
=======================================================================*/
package org.tensorflow.ndarray.impl.sparse;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.tensorflow.ndarray.LongNdArray;
import org.tensorflow.ndarray.NdArray;
Expand Down Expand Up @@ -338,4 +339,14 @@ public void testNullDefault() {
assertNotNull(dArray.getObject());
assertEquals("a default", dArray.getObject());
}

@Test
public void testToString() {
SparseNdArray<String, NdArray<String>> instance =
new SparseNdArray<>(String.class, indices, values, DimensionalSpace.create(shape));
Assertions.assertEquals("SparseNdArray(type=String, defaultValue=<null>, numElements=2, shape=[3, 4])",instance.toString());
instance = new SparseNdArray<>(
String.class, indices, values, "a default", DimensionalSpace.create(shape));
Assertions.assertEquals("SparseNdArray(type=String, defaultValue='a default', numElements=2, shape=[3, 4])",instance.toString());
}
}