summaryrefslogtreecommitdiffstats
path: root/vespajlib
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@verizonmedia.com>2020-01-03 11:23:37 +0100
committerJon Bratseth <bratseth@verizonmedia.com>2020-01-03 11:23:37 +0100
commit4c7de85c3933e4842784a453370247d533ee4102 (patch)
tree476941872a63cf9364fa9da44c3ebcde2b07a35d /vespajlib
parentfe102598a18b21a859d5b802883ccb2f462962f9 (diff)
Require equal lengths if specified
Diffstat (limited to 'vespajlib')
-rw-r--r--vespajlib/abi-spec.json1
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/TensorType.java11
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/functions/Merge.java25
-rw-r--r--vespajlib/src/test/java/com/yahoo/tensor/TensorTestCase.java16
4 files changed, 18 insertions, 35 deletions
diff --git a/vespajlib/abi-spec.json b/vespajlib/abi-spec.json
index 623c965e603..cc0a6dc3a14 100644
--- a/vespajlib/abi-spec.json
+++ b/vespajlib/abi-spec.json
@@ -1324,7 +1324,6 @@
"public abstract com.yahoo.tensor.TensorType$Dimension$Type type()",
"public abstract com.yahoo.tensor.TensorType$Dimension withName(java.lang.String)",
"public boolean isIndexed()",
- "public com.yahoo.tensor.TensorType$Dimension combineWith(java.util.Optional, boolean)",
"public abstract java.lang.String toString()",
"public boolean equals(java.lang.Object)",
"public int hashCode()",
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/TensorType.java b/vespajlib/src/main/java/com/yahoo/tensor/TensorType.java
index 32398c5a1e9..a9412f7f061 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/TensorType.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/TensorType.java
@@ -316,11 +316,11 @@ public class TensorType {
* Returns the dimension resulting from combining two dimensions having the same name but possibly different
* types:
*
- * [N] + [M] = [ minimal ? min(N, M) : max(N, M) ]
+ * [N] + [M] = [ min(N, M) ]
* [N] + [] = []
* [] + {} = {}
*/
- public Dimension combineWith(Optional<Dimension> other, boolean minimal) {
+ Dimension combineWith(Optional<Dimension> other) {
if ( ! other.isPresent()) return this;
if (this instanceof MappedDimension) return this;
if (other.get() instanceof MappedDimension) return other.get();
@@ -330,10 +330,7 @@ public class TensorType {
// both are indexed bound
IndexedBoundDimension thisIb = (IndexedBoundDimension)this;
IndexedBoundDimension otherIb = (IndexedBoundDimension)other.get();
- if (minimal)
return thisIb.size().get() < otherIb.size().get() ? thisIb : otherIb;
- else
- return thisIb.size().get() < otherIb.size().get() ? otherIb : thisIb;
}
@Override
@@ -520,7 +517,7 @@ public class TensorType {
}
else {
for (Dimension dimension : type.dimensions)
- set(dimension.combineWith(Optional.ofNullable(dimensions.get(dimension.name())), true));
+ set(dimension.combineWith(Optional.ofNullable(dimensions.get(dimension.name()))));
}
}
@@ -532,7 +529,7 @@ public class TensorType {
if (containsMapped)
dimension = new MappedDimension(dimension.name());
Dimension existing = dimensions.get(dimension.name());
- set(dimension.combineWith(Optional.ofNullable(existing), true));
+ set(dimension.combineWith(Optional.ofNullable(existing)));
}
}
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/functions/Merge.java b/vespajlib/src/main/java/com/yahoo/tensor/functions/Merge.java
index 350eaaa16f6..cb6b5b90624 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/functions/Merge.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/functions/Merge.java
@@ -25,18 +25,10 @@ import java.util.Set;
import java.util.function.DoubleBinaryOperator;
/**
- * The <i>merge</i> tensor operation produces from two argument tensors having equal dimension names
- * a tensor having the same dimension names, with each dimension the smallest (see below) which can encompass all the
- * values of both tensors, and where the values are the union of the values of both tensors. In the cases where both
+ * The <i>merge</i> tensor operation produces from two argument tensors having equal types
+ * a tensor having the same type where the values are the union of the values of both tensors. In the cases where both
* tensors contain a value for a given cell, and only then, the lambda scalar expression is evaluated to produce
- * the resulting cell value. If none of the argument tensors have a value (but the cell exists due to merging
- * indexed dimensions of uneven size in multidimensional tensors) the resulting cell is 0.
- * <p>
- * The type of each dimension of the result tensor is determined as follows:
- * <ul>
- * <li>If at least one of the two argument dimensions are mapped, the resulting dimension is mapped.
- * <li>Otherwise, the size of the resulting (indexed) dimension is the max size of the argument dimensions.
- * </ul>
+ * the resulting cell value.
*
* @author bratseth
*/
@@ -56,14 +48,9 @@ public class Merge<NAMETYPE extends Name> extends PrimitiveTensorFunction<NAMETY
/** Returns the type resulting from applying Merge to the two given types */
public static TensorType outputType(TensorType a, TensorType b) {
- if ( ! a.dimensionNames().equals(b.dimensionNames()))
- throw new IllegalArgumentException("Cannot merge " + a + " and " + b +
- ": Both arguments must have the same dimension names");
-
- TensorType.Builder builder = new TensorType.Builder(TensorType.combinedValueType(a, b));
- for (TensorType.Dimension dimension : a.dimensions())
- builder.set(dimension.combineWith(b.dimension(dimension.name()), false));
- return builder.build();
+ Optional<TensorType> outputType = a.dimensionwiseGeneralizationWith(b);
+ if (outputType.isPresent()) return outputType.get();
+ throw new IllegalArgumentException("Cannot merge " + a + " and " + b + ": Arguments must have compatible types");
}
public DoubleBinaryOperator merger() { return merger; }
diff --git a/vespajlib/src/test/java/com/yahoo/tensor/TensorTestCase.java b/vespajlib/src/test/java/com/yahoo/tensor/TensorTestCase.java
index 43f9b976978..040111a6fbb 100644
--- a/vespajlib/src/test/java/com/yahoo/tensor/TensorTestCase.java
+++ b/vespajlib/src/test/java/com/yahoo/tensor/TensorTestCase.java
@@ -217,21 +217,21 @@ public class TensorTestCase {
Tensor.from("tensor(x{},y[3])", "{}"),
Tensor.from("tensor(x{},y[3])", "{{x:0,y:0}:1,{x:0,y:1}:2}"));
assertTensorMerge(
- Tensor.from("tensor(x[2]):[5,6]"),
+ Tensor.from("tensor(x[4]):[5,6,7,8]"),
Tensor.from("tensor(x[4]):[1,2,3,4]"),
Tensor.from("tensor(x[4]):[1,2,3,4]"));
assertTensorMerge(
- Tensor.from("tensor(x[4]):[1,2,3,4]"),
- Tensor.from("tensor(x[2]):[5,6]"),
+ Tensor.from("tensor(x[]):{{x:0}:1,{x:1}:2,{x:2}:3,{x:3}:4}"),
+ Tensor.from("tensor(x[]):{{x:0}:5,{x:1}:6}"),
Tensor.from("tensor(x[4]):[5,6,3,4]"));
assertTensorMerge(
- Tensor.from("tensor(x[4],y[2]):[[1,2],[3,4],[5,6],[7,8]]"),
- Tensor.from("tensor(x[2],y[3]):[[9,10,11],[12,13,14]]"),
- Tensor.from("tensor(x[4],y[3]):[[9,10,11],[12,13,14],[5,6,0],[7,8,0]]"));
+ Tensor.from("tensor(x{}):{a:1,b:2}"),
+ Tensor.from("tensor(x{}):{b:3,c:4}"),
+ Tensor.from("tensor(x{}):{a:1,b:3,c:4}"));
assertTensorMerge(
Tensor.from("tensor(key{},x[4]):{a:[1,2,3,4],c:[5,6,7,8]}"),
- Tensor.from("tensor(key{},x[2]):{a:[9,10],b:[11,12]}"),
- Tensor.from("tensor(key{},x[4]):{a:[9,10,3,4],b:[11,12,0,0],c:[5,6,7,8]}"));
+ Tensor.from("tensor(key{},x[4]):{a:[9,10,11,12],b:[13,14,15,16]}"),
+ Tensor.from("tensor(key{},x[4]):{a:[9,10,11,12],b:[13,14,15,16],c:[5,6,7,8]}"));
}
@Test