summaryrefslogtreecommitdiffstats
path: root/vespajlib
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@oath.com>2018-02-02 13:24:45 +0100
committerJon Bratseth <bratseth@oath.com>2018-02-02 13:24:45 +0100
commitc288092ac9868efae10437a8c60974050e9fa799 (patch)
tree537e666d2c5ae7b06fa6e7b4f7ed33b6bb66d013 /vespajlib
parent468e0e16a60a5feaf6d5eec971ff06078b6bb694 (diff)
Generalize dimension-wise
Diffstat (limited to 'vespajlib')
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/TensorType.java41
1 files changed, 41 insertions, 0 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/TensorType.java b/vespajlib/src/main/java/com/yahoo/tensor/TensorType.java
index 8ff9774fc7d..14cd3e70866 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/TensorType.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/TensorType.java
@@ -136,6 +136,47 @@ public class TensorType {
return true;
}
+ /**
+ * Returns the dimensionwise generalization of this and the given type, or empty if no generalization exists.
+ * A dimensionwise generalization exists if the two tensors share the same dimensions, and each dimension
+ * is compatible.
+ * For example, the dimensionwise generalization of tensor(x[],y[5]) and tensor(x[5],y[]) is tensor(x[],y[])
+ */
+ public Optional<TensorType> dimensionwiseGeneralizationWith(TensorType other) {
+ if (this.equals(other)) return Optional.of(this); // shortcut
+ if (this.dimensions.size() != other.dimensions.size()) return Optional.empty();
+
+ Builder b = new Builder();
+ for (int i = 0; i < dimensions.size(); i++) {
+ Dimension thisDim = this.dimensions().get(i);
+ Dimension otherDim = other.dimensions().get(i);
+ if ( ! thisDim.name().equals(otherDim.name())) return Optional.empty();
+ if (thisDim.isIndexed() && otherDim.isIndexed()) {
+ if (thisDim.size().isPresent() && otherDim.size().isPresent()) {
+ if ( ! thisDim.size().get().equals(otherDim.size().get()))
+ return Optional.empty();
+ b.dimension(thisDim); // both are equal and bound
+ }
+ else if (thisDim.size().isPresent()) {
+ b.dimension(otherDim); // use the unbound
+ }
+ else if (otherDim.size().isPresent()) {
+ b.dimension(thisDim); // use the unbound
+ }
+ else {
+ b.dimension(thisDim); // both are equal and unbound
+ }
+ }
+ else if ( ! thisDim.isIndexed() && ! otherDim.isIndexed()) {
+ b.dimension(thisDim); // both are equal and mapped
+ }
+ else {
+ return Optional.empty(); // one indexed and one mapped
+ }
+ }
+ return Optional.of(b.build());
+ }
+
@Override
public int hashCode() {
return dimensions.hashCode();