summaryrefslogtreecommitdiffstats
path: root/vespajlib
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@yahoo-inc.com>2017-01-18 10:09:21 +0100
committerJon Bratseth <bratseth@yahoo-inc.com>2017-01-18 10:09:21 +0100
commitbad625e3565d83a72436224ed5ccbc2649ab89db (patch)
tree13e360ecb0329debd81a1755e844f95d659c4022 /vespajlib
parentf1921848eff763bc99c46e53733df7bcae04fa7b (diff)
parentc61907a3458d565910b0a3b4286c87759469da12 (diff)
Merge with master
Diffstat (limited to 'vespajlib')
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java10
-rw-r--r--vespajlib/src/test/java/com/yahoo/tensor/IndexedTensorTestCase.java10
2 files changed, 18 insertions, 2 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java b/vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java
index bee93ddb4e0..a66caa8dd35 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java
@@ -355,10 +355,16 @@ public class IndexedTensor implements Tensor {
offset + productOfDimensionsAfter(currentDimensionIndex, sizes) * i,
(List<Object>) currentDimension.get(i), sizes, values);
} else { // last dimension - fill values
- for (int i = 0; i < currentDimension.size(); i++)
- values[offset + i] = (double) currentDimension.get(i);
+ for (int i = 0; i < currentDimension.size(); i++) {
+ values[offset + i] = nullAsZero((Double)currentDimension.get(i)); // fill missing values as zero
+ }
}
}
+
+ private double nullAsZero(Double value) {
+ if (value == null) return 0;
+ return value;
+ }
@Override
public CellBuilder cell() {
diff --git a/vespajlib/src/test/java/com/yahoo/tensor/IndexedTensorTestCase.java b/vespajlib/src/test/java/com/yahoo/tensor/IndexedTensorTestCase.java
index 3a6a7f2f65d..09b6e79a101 100644
--- a/vespajlib/src/test/java/com/yahoo/tensor/IndexedTensorTestCase.java
+++ b/vespajlib/src/test/java/com/yahoo/tensor/IndexedTensorTestCase.java
@@ -3,6 +3,7 @@ package com.yahoo.tensor;
import junit.framework.TestCase;
import org.junit.Test;
+import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
@@ -63,6 +64,15 @@ public class IndexedTensorTestCase {
assertBuildingVWXYZ(type);
}
+ @Test
+ public void testUnderspecifiedBuilding() {
+ TensorType type = new TensorType.Builder().indexed("x").build();
+ Tensor.Builder builder = Tensor.Builder.of(type);
+ builder.cell(47.0, 98);
+ Tensor tensor = builder.build();
+ assertEquals(47.0, tensor.sum(Collections.singletonList("x")).asDouble(), 0.000001);
+ }
+
private void assertBuildingVWXYZ(TensorType type) {
IndexedTensor.Builder builder = IndexedTensor.Builder.of(type);
// Build in scrambled order