summaryrefslogtreecommitdiffstats
path: root/vespajlib
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@yahoo-inc.com>2017-11-29 09:39:06 -0800
committerJon Bratseth <bratseth@yahoo-inc.com>2017-11-29 09:39:06 -0800
commite0a9e9978266016823b33e1b4f3a6008b641feac (patch)
treec967f733ed0bf4308ab626891b48375af311630a /vespajlib
parentdcc9ad075c80d207bec47567c8e45dcb775c7f5d (diff)
More functions. Cleanup.
Diffstat (limited to 'vespajlib')
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/functions/Join.java2
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/functions/Map.java3
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/functions/Matmul.java4
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/functions/Reduce.java4
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/functions/ScalarFunctions.java97
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/functions/Softmax.java4
6 files changed, 53 insertions, 61 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/functions/Join.java b/vespajlib/src/main/java/com/yahoo/tensor/functions/Join.java
index c89f63c0395..9a37127e1f0 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/functions/Join.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/functions/Join.java
@@ -47,7 +47,7 @@ public class Join extends PrimitiveTensorFunction {
}
/** Returns the type resulting from applying Join to the two given types */
- public static TensorType resultType(TensorType a, TensorType b) {
+ public static TensorType outputType(TensorType a, TensorType b) {
TensorType.Builder typeBuilder = new TensorType.Builder();
for (int i = 0; i < a.dimensions().size(); ++i) {
TensorType.Dimension aDim = a.dimensions().get(i);
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/functions/Map.java b/vespajlib/src/main/java/com/yahoo/tensor/functions/Map.java
index a9872bb42d8..d322a6ab497 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/functions/Map.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/functions/Map.java
@@ -6,6 +6,7 @@ import com.google.common.collect.ImmutableMap;
import com.yahoo.tensor.MappedTensor;
import com.yahoo.tensor.Tensor;
import com.yahoo.tensor.TensorAddress;
+import com.yahoo.tensor.TensorType;
import com.yahoo.tensor.evaluation.EvaluationContext;
import java.util.Collections;
@@ -31,6 +32,8 @@ public class Map extends PrimitiveTensorFunction {
this.argument = argument;
this.mapper = mapper;
}
+
+ public static TensorType outputType(TensorType inputType) { return inputType; }
public TensorFunction argument() { return argument; }
public DoubleUnaryOperator mapper() { return mapper; }
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/functions/Matmul.java b/vespajlib/src/main/java/com/yahoo/tensor/functions/Matmul.java
index cbb3f159623..5e102454487 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/functions/Matmul.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/functions/Matmul.java
@@ -22,8 +22,8 @@ public class Matmul extends CompositeTensorFunction {
this.dimension = dimension;
}
- public static TensorType resultType(TensorType a, TensorType b, String dimension) {
- return Reduce.resultType(Join.resultType(a, b), ImmutableList.of(dimension));
+ public static TensorType outputType(TensorType a, TensorType b, String dimension) {
+ return Reduce.outputType(Join.outputType(a, b), ImmutableList.of(dimension));
}
@Override
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/functions/Reduce.java b/vespajlib/src/main/java/com/yahoo/tensor/functions/Reduce.java
index aa28a26deb2..a51df12e522 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/functions/Reduce.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/functions/Reduce.java
@@ -61,9 +61,9 @@ public class Reduce extends PrimitiveTensorFunction {
this.dimensions = ImmutableList.copyOf(dimensions);
}
- public static TensorType resultType(TensorType type, List<String> reduceDimensions) {
+ public static TensorType outputType(TensorType inputType, List<String> reduceDimensions) {
TensorType.Builder b = new TensorType.Builder();
- for (TensorType.Dimension dimension : type.dimensions()) {
+ for (TensorType.Dimension dimension : inputType.dimensions()) {
if ( ! reduceDimensions.contains(dimension.name()))
b.dimension(dimension);
}
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/functions/ScalarFunctions.java b/vespajlib/src/main/java/com/yahoo/tensor/functions/ScalarFunctions.java
index 99f79cb735a..fb5029fbfd6 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/functions/ScalarFunctions.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/functions/ScalarFunctions.java
@@ -21,101 +21,87 @@ import java.util.stream.Collectors;
@Beta
public class ScalarFunctions {
- public static DoubleBinaryOperator add() { return new Addition(); }
- public static DoubleBinaryOperator multiply() { return new Multiplication(); }
- public static DoubleBinaryOperator divide() { return new Division(); }
+ public static DoubleBinaryOperator add() { return new Add(); }
+ public static DoubleBinaryOperator divide() { return new Divide(); }
public static DoubleBinaryOperator equal() { return new Equal(); }
- public static DoubleUnaryOperator square() { return new Square(); }
+ public static DoubleBinaryOperator multiply() { return new Multiply(); }
+
+ public static DoubleUnaryOperator acos() { return new Acos(); }
+ public static DoubleUnaryOperator exp() { return new Exp(); }
public static DoubleUnaryOperator sqrt() { return new Sqrt(); }
- public static DoubleUnaryOperator exp() { return new Exponent(); }
+ public static DoubleUnaryOperator square() { return new Square(); }
+
public static Function<List<Integer>, Double> random() { return new Random(); }
public static Function<List<Integer>, Double> equal(List<String> argumentNames) { return new EqualElements(argumentNames); }
public static Function<List<Integer>, Double> sum(List<String> argumentNames) { return new SumElements(argumentNames); }
- public static class Addition implements DoubleBinaryOperator {
+ // Binary operators -----------------------------------------------------------------------------
+ public static class Add implements DoubleBinaryOperator {
@Override
public double applyAsDouble(double left, double right) { return left + right; }
-
@Override
public String toString() { return "f(a,b)(a + b)"; }
-
}
- public static class Multiplication implements DoubleBinaryOperator {
-
+ public static class Equal implements DoubleBinaryOperator {
@Override
- public double applyAsDouble(double left, double right) { return left * right; }
-
+ public double applyAsDouble(double left, double right) { return left == right ? 1 : 0; }
@Override
- public String toString() { return "f(a,b)(a * b)"; }
-
+ public String toString() { return "f(a,b)(a==b)"; }
}
- public static class Division implements DoubleBinaryOperator {
-
+ public static class Exp implements DoubleUnaryOperator {
@Override
- public double applyAsDouble(double left, double right) { return left / right; }
-
+ public double applyAsDouble(double operand) { return Math.exp(operand); }
@Override
- public String toString() { return "f(a,b)(a / b)"; }
+ public String toString() { return "f(a)(exp(a))"; }
}
- public static class Equal implements DoubleBinaryOperator {
-
+ public static class Multiply implements DoubleBinaryOperator {
@Override
- public double applyAsDouble(double left, double right) { return left == right ? 1 : 0; }
+ public double applyAsDouble(double left, double right) { return left * right; }
+ @Override
+ public String toString() { return "f(a,b)(a * b)"; }
+ }
+ public static class Divide implements DoubleBinaryOperator {
@Override
- public String toString() { return "f(a,b)(a==b)"; }
+ public double applyAsDouble(double left, double right) { return left / right; }
+ @Override
+ public String toString() { return "f(a,b)(a / b)"; }
}
- public static class Square implements DoubleUnaryOperator {
+ // Unary operators ------------------------------------------------------------------------------
+ public static class Acos implements DoubleUnaryOperator {
@Override
- public double applyAsDouble(double operand) { return operand * operand; }
-
+ public double applyAsDouble(double operand) { return Math.acos(operand); }
@Override
- public String toString() { return "f(a)(a * a)"; }
-
+ public String toString() { return "f(a)(acos(a))"; }
}
public static class Sqrt implements DoubleUnaryOperator {
-
@Override
public double applyAsDouble(double operand) { return Math.sqrt(operand); }
-
@Override
public String toString() { return "f(a)(sqrt(a))"; }
-
}
- public static class Exponent implements DoubleUnaryOperator {
+ public static class Square implements DoubleUnaryOperator {
@Override
- public double applyAsDouble(double operand) { return Math.exp(operand); }
+ public double applyAsDouble(double operand) { return operand * operand; }
@Override
- public String toString() { return "f(a)(exp(a))"; }
+ public String toString() { return "f(a)(a * a)"; }
}
- public static class Random implements Function<List<Integer>, Double> {
-
- @Override
- public Double apply(List<Integer> values) {
- return ThreadLocalRandom.current().nextDouble();
- }
-
- @Override
- public String toString() { return "random"; }
+ // Variable-length operators -----------------------------------------------------------------------------
- }
-
- public static class EqualElements implements Function<List<Integer>, Double> {
-
- private final ImmutableList<String> argumentNames;
-
+ public static class EqualElements implements Function<List<Integer>, Double> {
+ private final ImmutableList<String> argumentNames;
private EqualElements(List<String> argumentNames) {
this.argumentNames = ImmutableList.copyOf(argumentNames);
}
@@ -128,7 +114,6 @@ public class ScalarFunctions {
return 0.0;
return 1.0;
}
-
@Override
public String toString() {
if (argumentNames.size() == 0) return "1";
@@ -143,13 +128,19 @@ public class ScalarFunctions {
}
return b.toString();
}
+ }
+ public static class Random implements Function<List<Integer>, Double> {
+ @Override
+ public Double apply(List<Integer> values) {
+ return ThreadLocalRandom.current().nextDouble();
+ }
+ @Override
+ public String toString() { return "random"; }
}
public static class SumElements implements Function<List<Integer>, Double> {
-
private final ImmutableList<String> argumentNames;
-
private SumElements(List<String> argumentNames) {
this.argumentNames = ImmutableList.copyOf(argumentNames);
}
@@ -161,12 +152,10 @@ public class ScalarFunctions {
sum += value;
return (double)sum;
}
-
@Override
public String toString() {
return argumentNames.stream().collect(Collectors.joining("+"));
}
-
}
}
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/functions/Softmax.java b/vespajlib/src/main/java/com/yahoo/tensor/functions/Softmax.java
index 45f78389c16..c856b548180 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/functions/Softmax.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/functions/Softmax.java
@@ -22,8 +22,8 @@ public class Softmax extends CompositeTensorFunction {
this.dimension = dimension;
}
- public static TensorType resultType(TensorType type, String dimension) {
- return Reduce.resultType(type, ImmutableList.of(dimension));
+ public static TensorType outputType(TensorType inputType, String dimension) {
+ return Reduce.outputType(inputType, ImmutableList.of(dimension));
}
@Override