aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/tensor/functions/ScalarFunctions.java
blob: eb21cdf6bfc05b7e45099d569c6aa25ade6ab218 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package com.yahoo.tensor.functions;

import com.google.common.annotations.Beta;
import com.google.common.collect.ImmutableList;

import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.DoubleBinaryOperator;
import java.util.function.DoubleSupplier;
import java.util.function.DoubleUnaryOperator;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Factory of scalar Java functions.
 * The purpose of this is to embellish anonymous functions with a runtime type
 * such that they can be inspected and will return a parseable toString.
 * 
 * @author bratseth
 */
@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 DoubleUnaryOperator square() { return new Square(); }
    public static DoubleUnaryOperator sqrt() { return new Sqrt(); }
    public static DoubleUnaryOperator exp() { return new Exponent(); }
    public static Function<List<Integer>, Double> random() { return new Random(); }
    public static Function<List<Integer>, Double> equalArguments(List<String> argumentNames) { 
        return new EqualArguments(argumentNames); 
    }
    public static Function<List<Integer>, Double> sumArguments(List<String> argumentNames) {
        return new SumArguments(argumentNames);
    }

    public static class Addition 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 {

        @Override
        public double applyAsDouble(double left, double right) { return left * right; }
        
        @Override
        public String toString() { return "f(a,b)(a * b)"; }

    }

    public static class Division 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 Square implements DoubleUnaryOperator {

        @Override
        public double applyAsDouble(double operand) { return operand * operand; }

        @Override
        public String toString() { return "f(a)(a * 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 {

        @Override
        public double applyAsDouble(double operand) { return Math.exp(operand); }

        @Override
        public String toString() { return "f(a)(exp(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()"; }

    }

    public static class EqualArguments implements Function<List<Integer>, Double> {
        
        private final ImmutableList<String> argumentNames;
        
        private EqualArguments(List<String> argumentNames) {
            this.argumentNames = ImmutableList.copyOf(argumentNames);
        }

        @Override
        public Double apply(List<Integer> values) {
            if (values.isEmpty()) return 1.0;
            for (Integer value : values)
                if ( ! value.equals(values.get(0)))
                    return 0.0;
            return 1.0;
        }

        @Override
        public String toString() { 
            if (argumentNames.size() == 0) return "1";
            if (argumentNames.size() == 1) return "1";
            if (argumentNames.size() == 2) return argumentNames.get(0) + "==" + argumentNames.get(1);
            
            StringBuilder b = new StringBuilder();
            for (int i = 0; i < argumentNames.size() -1; i++) {
                b.append("(").append(argumentNames.get(i)).append("==").append(argumentNames.get(i+1)).append(")");
                if ( i < argumentNames.size() -2)
                    b.append("*");
            }
            return b.toString();
        }

    }

    public static class SumArguments implements Function<List<Integer>, Double> {

        private final ImmutableList<String> argumentNames;

        private SumArguments(List<String> argumentNames) {
            this.argumentNames = ImmutableList.copyOf(argumentNames);
        }

        @Override
        public Double apply(List<Integer> values) {
            int sum = 0;
            for (Integer value : values)
                sum += value;
            return (double)sum;
        }

        @Override
        public String toString() {
            return argumentNames.stream().collect(Collectors.joining("+"));
        }

    }

}