diff options
author | Lester Solbakken <lesters@oath.com> | 2020-06-18 19:25:35 +0200 |
---|---|---|
committer | Lester Solbakken <lesters@oath.com> | 2020-06-18 19:25:35 +0200 |
commit | 06bde5687b214a97c72e41ee40ac76ad837a3d7d (patch) | |
tree | 2d7ef9a8a2017d4f986744dfde82f8b671f4227b /vespajlib | |
parent | 5688a50eb92fc4459e51dccca45858aecca8264a (diff) |
Add erf (the error function)
Diffstat (limited to 'vespajlib')
-rw-r--r-- | vespajlib/abi-spec.json | 17 | ||||
-rw-r--r-- | vespajlib/src/main/java/com/yahoo/tensor/functions/ScalarFunctions.java | 25 |
2 files changed, 42 insertions, 0 deletions
diff --git a/vespajlib/abi-spec.json b/vespajlib/abi-spec.json index d9467a41f78..154b6871392 100644 --- a/vespajlib/abi-spec.json +++ b/vespajlib/abi-spec.json @@ -2075,6 +2075,22 @@ ], "fields": [] }, + "com.yahoo.tensor.functions.ScalarFunctions$Erf": { + "superClass": "java.lang.Object", + "interfaces": [ + "java.util.function.DoubleUnaryOperator" + ], + "attributes": [ + "public" + ], + "methods": [ + "public void <init>()", + "public double applyAsDouble(double)", + "public java.lang.String toString()", + "public static double erf(double)" + ], + "fields": [] + }, "com.yahoo.tensor.functions.ScalarFunctions$Exp": { "superClass": "java.lang.Object", "interfaces": [ @@ -2506,6 +2522,7 @@ "public static java.util.function.DoubleUnaryOperator square()", "public static java.util.function.DoubleUnaryOperator tan()", "public static java.util.function.DoubleUnaryOperator tanh()", + "public static java.util.function.DoubleUnaryOperator erf()", "public static java.util.function.DoubleUnaryOperator elu()", "public static java.util.function.DoubleUnaryOperator elu(double)", "public static java.util.function.DoubleUnaryOperator leakyrelu()", 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 d9204e24d68..c19b07cf96f 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/functions/ScalarFunctions.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/functions/ScalarFunctions.java @@ -50,6 +50,7 @@ public class ScalarFunctions { public static DoubleUnaryOperator square() { return new Square(); } public static DoubleUnaryOperator tan() { return new Tan(); } public static DoubleUnaryOperator tanh() { return new Tanh(); } + public static DoubleUnaryOperator erf() { return new Erf(); } public static DoubleUnaryOperator elu() { return new Elu(); } public static DoubleUnaryOperator elu(double alpha) { return new Elu(alpha); } @@ -330,6 +331,30 @@ public class ScalarFunctions { public String toString() { return "f(a)(tanh(a))"; } } + public static class Erf implements DoubleUnaryOperator { + @Override + public double applyAsDouble(double operand) { return erf(operand); } + @Override + public String toString() { return "f(a)(erf(a))"; } + + // Use Horner's method + // From https://introcs.cs.princeton.edu/java/21function/ErrorFunction.java.html + public static double erf(double v) { + double t = 1.0 / (1.0 + 0.5 * Math.abs(v)); + double ans = 1 - t * Math.exp(-v*v - 1.26551223 + + t * ( 1.00002368 + + t * ( 0.37409196 + + t * ( 0.09678418 + + t * (-0.18628806 + + t * ( 0.27886807 + + t * (-1.13520398 + + t * ( 1.48851587 + + t * (-0.82215223 + + t * ( 0.17087277)))))))))); + if (v >= 0) return ans; + else return -ans; + } + } // Variable-length operators ----------------------------------------------------------------------------- |