From 06bde5687b214a97c72e41ee40ac76ad837a3d7d Mon Sep 17 00:00:00 2001 From: Lester Solbakken Date: Thu, 18 Jun 2020 19:25:35 +0200 Subject: Add erf (the error function) --- vespajlib/abi-spec.json | 17 +++++++++++++++ .../yahoo/tensor/functions/ScalarFunctions.java | 25 ++++++++++++++++++++++ 2 files changed, 42 insertions(+) (limited to 'vespajlib') 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 ()", + "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 ----------------------------------------------------------------------------- -- cgit v1.2.3