Logistic function from Lakshay Garg

?

Percentage Accurate: 54.2% → 99.7%
Time: 11.1s
Precision: binary64
Cost: 32776

?

\[\frac{2}{1 + e^{-2 \cdot x}} - 1 \]
\[\begin{array}{l} t_0 := {\left(e^{-2}\right)}^{x}\\ \mathbf{if}\;-2 \cdot x \leq -0.1:\\ \;\;\;\;\log \left(e^{\frac{2}{t_0 + 1} + -1}\right)\\ \mathbf{elif}\;-2 \cdot x \leq 2 \cdot 10^{-12}:\\ \;\;\;\;x + -0.3333333333333333 \cdot {x}^{3}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{expm1}\left(\log 2 - \mathsf{log1p}\left(t_0\right)\right)\\ \end{array} \]
(FPCore (x y) :precision binary64 (- (/ 2.0 (+ 1.0 (exp (* -2.0 x)))) 1.0))
(FPCore (x y)
 :precision binary64
 (let* ((t_0 (pow (exp -2.0) x)))
   (if (<= (* -2.0 x) -0.1)
     (log (exp (+ (/ 2.0 (+ t_0 1.0)) -1.0)))
     (if (<= (* -2.0 x) 2e-12)
       (+ x (* -0.3333333333333333 (pow x 3.0)))
       (expm1 (- (log 2.0) (log1p t_0)))))))
double code(double x, double y) {
	return (2.0 / (1.0 + exp((-2.0 * x)))) - 1.0;
}
double code(double x, double y) {
	double t_0 = pow(exp(-2.0), x);
	double tmp;
	if ((-2.0 * x) <= -0.1) {
		tmp = log(exp(((2.0 / (t_0 + 1.0)) + -1.0)));
	} else if ((-2.0 * x) <= 2e-12) {
		tmp = x + (-0.3333333333333333 * pow(x, 3.0));
	} else {
		tmp = expm1((log(2.0) - log1p(t_0)));
	}
	return tmp;
}
public static double code(double x, double y) {
	return (2.0 / (1.0 + Math.exp((-2.0 * x)))) - 1.0;
}
public static double code(double x, double y) {
	double t_0 = Math.pow(Math.exp(-2.0), x);
	double tmp;
	if ((-2.0 * x) <= -0.1) {
		tmp = Math.log(Math.exp(((2.0 / (t_0 + 1.0)) + -1.0)));
	} else if ((-2.0 * x) <= 2e-12) {
		tmp = x + (-0.3333333333333333 * Math.pow(x, 3.0));
	} else {
		tmp = Math.expm1((Math.log(2.0) - Math.log1p(t_0)));
	}
	return tmp;
}
def code(x, y):
	return (2.0 / (1.0 + math.exp((-2.0 * x)))) - 1.0
def code(x, y):
	t_0 = math.pow(math.exp(-2.0), x)
	tmp = 0
	if (-2.0 * x) <= -0.1:
		tmp = math.log(math.exp(((2.0 / (t_0 + 1.0)) + -1.0)))
	elif (-2.0 * x) <= 2e-12:
		tmp = x + (-0.3333333333333333 * math.pow(x, 3.0))
	else:
		tmp = math.expm1((math.log(2.0) - math.log1p(t_0)))
	return tmp
function code(x, y)
	return Float64(Float64(2.0 / Float64(1.0 + exp(Float64(-2.0 * x)))) - 1.0)
end
function code(x, y)
	t_0 = exp(-2.0) ^ x
	tmp = 0.0
	if (Float64(-2.0 * x) <= -0.1)
		tmp = log(exp(Float64(Float64(2.0 / Float64(t_0 + 1.0)) + -1.0)));
	elseif (Float64(-2.0 * x) <= 2e-12)
		tmp = Float64(x + Float64(-0.3333333333333333 * (x ^ 3.0)));
	else
		tmp = expm1(Float64(log(2.0) - log1p(t_0)));
	end
	return tmp
end
code[x_, y_] := N[(N[(2.0 / N[(1.0 + N[Exp[N[(-2.0 * x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - 1.0), $MachinePrecision]
code[x_, y_] := Block[{t$95$0 = N[Power[N[Exp[-2.0], $MachinePrecision], x], $MachinePrecision]}, If[LessEqual[N[(-2.0 * x), $MachinePrecision], -0.1], N[Log[N[Exp[N[(N[(2.0 / N[(t$95$0 + 1.0), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision]], $MachinePrecision]], $MachinePrecision], If[LessEqual[N[(-2.0 * x), $MachinePrecision], 2e-12], N[(x + N[(-0.3333333333333333 * N[Power[x, 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(Exp[N[(N[Log[2.0], $MachinePrecision] - N[Log[1 + t$95$0], $MachinePrecision]), $MachinePrecision]] - 1), $MachinePrecision]]]]
\frac{2}{1 + e^{-2 \cdot x}} - 1
\begin{array}{l}
t_0 := {\left(e^{-2}\right)}^{x}\\
\mathbf{if}\;-2 \cdot x \leq -0.1:\\
\;\;\;\;\log \left(e^{\frac{2}{t_0 + 1} + -1}\right)\\

\mathbf{elif}\;-2 \cdot x \leq 2 \cdot 10^{-12}:\\
\;\;\;\;x + -0.3333333333333333 \cdot {x}^{3}\\

\mathbf{else}:\\
\;\;\;\;\mathsf{expm1}\left(\log 2 - \mathsf{log1p}\left(t_0\right)\right)\\


\end{array}

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Herbie found 10 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Bogosity?

Bogosity

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Derivation?

  1. Split input into 3 regimes
  2. if (*.f64 -2 x) < -0.10000000000000001

    1. Initial program 99.9%

      \[\frac{2}{1 + e^{-2 \cdot x}} - 1 \]
    2. Applied egg-rr99.9%

      \[\leadsto \color{blue}{0 + \mathsf{expm1}\left(\log 2 - \mathsf{log1p}\left({\left(e^{-2}\right)}^{x}\right)\right)} \]
      Step-by-step derivation

      [Start]99.9%

      \[ \frac{2}{1 + e^{-2 \cdot x}} - 1 \]

      add-log-exp [=>]99.9%

      \[ \color{blue}{\log \left(e^{\frac{2}{1 + e^{-2 \cdot x}} - 1}\right)} \]

      *-un-lft-identity [=>]99.9%

      \[ \log \color{blue}{\left(1 \cdot e^{\frac{2}{1 + e^{-2 \cdot x}} - 1}\right)} \]

      log-prod [=>]99.9%

      \[ \color{blue}{\log 1 + \log \left(e^{\frac{2}{1 + e^{-2 \cdot x}} - 1}\right)} \]

      metadata-eval [=>]99.9%

      \[ \color{blue}{0} + \log \left(e^{\frac{2}{1 + e^{-2 \cdot x}} - 1}\right) \]

      add-log-exp [<=]99.9%

      \[ 0 + \color{blue}{\left(\frac{2}{1 + e^{-2 \cdot x}} - 1\right)} \]

      add-exp-log [=>]99.9%

      \[ 0 + \left(\color{blue}{e^{\log \left(\frac{2}{1 + e^{-2 \cdot x}}\right)}} - 1\right) \]

      expm1-def [=>]99.9%

      \[ 0 + \color{blue}{\mathsf{expm1}\left(\log \left(\frac{2}{1 + e^{-2 \cdot x}}\right)\right)} \]

      log-div [=>]99.9%

      \[ 0 + \mathsf{expm1}\left(\color{blue}{\log 2 - \log \left(1 + e^{-2 \cdot x}\right)}\right) \]

      log1p-udef [<=]99.9%

      \[ 0 + \mathsf{expm1}\left(\log 2 - \color{blue}{\mathsf{log1p}\left(e^{-2 \cdot x}\right)}\right) \]

      exp-prod [=>]99.9%

      \[ 0 + \mathsf{expm1}\left(\log 2 - \mathsf{log1p}\left(\color{blue}{{\left(e^{-2}\right)}^{x}}\right)\right) \]
    3. Simplified99.9%

      \[\leadsto \color{blue}{\mathsf{expm1}\left(\log 2 - \mathsf{log1p}\left({\left(e^{-2}\right)}^{x}\right)\right)} \]
      Step-by-step derivation

      [Start]99.9%

      \[ 0 + \mathsf{expm1}\left(\log 2 - \mathsf{log1p}\left({\left(e^{-2}\right)}^{x}\right)\right) \]

      +-lft-identity [=>]99.9%

      \[ \color{blue}{\mathsf{expm1}\left(\log 2 - \mathsf{log1p}\left({\left(e^{-2}\right)}^{x}\right)\right)} \]
    4. Applied egg-rr99.9%

      \[\leadsto \color{blue}{\log \left(e^{\frac{2}{{\left(e^{-2}\right)}^{x} + 1} + -1}\right)} \]
      Step-by-step derivation

      [Start]99.9%

      \[ \mathsf{expm1}\left(\log 2 - \mathsf{log1p}\left({\left(e^{-2}\right)}^{x}\right)\right) \]

      add-log-exp [=>]99.9%

      \[ \color{blue}{\log \left(e^{\mathsf{expm1}\left(\log 2 - \mathsf{log1p}\left({\left(e^{-2}\right)}^{x}\right)\right)}\right)} \]

      expm1-udef [=>]99.9%

      \[ \log \left(e^{\color{blue}{e^{\log 2 - \mathsf{log1p}\left({\left(e^{-2}\right)}^{x}\right)} - 1}}\right) \]

      sub-neg [=>]99.9%

      \[ \log \left(e^{\color{blue}{e^{\log 2 - \mathsf{log1p}\left({\left(e^{-2}\right)}^{x}\right)} + \left(-1\right)}}\right) \]

      exp-diff [=>]99.9%

      \[ \log \left(e^{\color{blue}{\frac{e^{\log 2}}{e^{\mathsf{log1p}\left({\left(e^{-2}\right)}^{x}\right)}}} + \left(-1\right)}\right) \]

      add-exp-log [<=]99.9%

      \[ \log \left(e^{\frac{\color{blue}{2}}{e^{\mathsf{log1p}\left({\left(e^{-2}\right)}^{x}\right)}} + \left(-1\right)}\right) \]

      log1p-udef [=>]99.9%

      \[ \log \left(e^{\frac{2}{e^{\color{blue}{\log \left(1 + {\left(e^{-2}\right)}^{x}\right)}}} + \left(-1\right)}\right) \]

      add-exp-log [<=]99.9%

      \[ \log \left(e^{\frac{2}{\color{blue}{1 + {\left(e^{-2}\right)}^{x}}} + \left(-1\right)}\right) \]

      +-commutative [=>]99.9%

      \[ \log \left(e^{\frac{2}{\color{blue}{{\left(e^{-2}\right)}^{x} + 1}} + \left(-1\right)}\right) \]

      metadata-eval [=>]99.9%

      \[ \log \left(e^{\frac{2}{{\left(e^{-2}\right)}^{x} + 1} + \color{blue}{-1}}\right) \]

    if -0.10000000000000001 < (*.f64 -2 x) < 1.99999999999999996e-12

    1. Initial program 8.0%

      \[\frac{2}{1 + e^{-2 \cdot x}} - 1 \]
    2. Taylor expanded in x around 0 100.0%

      \[\leadsto \color{blue}{-0.3333333333333333 \cdot {x}^{3} + x} \]

    if 1.99999999999999996e-12 < (*.f64 -2 x)

    1. Initial program 99.9%

      \[\frac{2}{1 + e^{-2 \cdot x}} - 1 \]
    2. Applied egg-rr100.0%

      \[\leadsto \color{blue}{0 + \mathsf{expm1}\left(\log 2 - \mathsf{log1p}\left({\left(e^{-2}\right)}^{x}\right)\right)} \]
      Step-by-step derivation

      [Start]99.9%

      \[ \frac{2}{1 + e^{-2 \cdot x}} - 1 \]

      add-log-exp [=>]99.9%

      \[ \color{blue}{\log \left(e^{\frac{2}{1 + e^{-2 \cdot x}} - 1}\right)} \]

      *-un-lft-identity [=>]99.9%

      \[ \log \color{blue}{\left(1 \cdot e^{\frac{2}{1 + e^{-2 \cdot x}} - 1}\right)} \]

      log-prod [=>]99.9%

      \[ \color{blue}{\log 1 + \log \left(e^{\frac{2}{1 + e^{-2 \cdot x}} - 1}\right)} \]

      metadata-eval [=>]99.9%

      \[ \color{blue}{0} + \log \left(e^{\frac{2}{1 + e^{-2 \cdot x}} - 1}\right) \]

      add-log-exp [<=]99.9%

      \[ 0 + \color{blue}{\left(\frac{2}{1 + e^{-2 \cdot x}} - 1\right)} \]

      add-exp-log [=>]99.9%

      \[ 0 + \left(\color{blue}{e^{\log \left(\frac{2}{1 + e^{-2 \cdot x}}\right)}} - 1\right) \]

      expm1-def [=>]99.9%

      \[ 0 + \color{blue}{\mathsf{expm1}\left(\log \left(\frac{2}{1 + e^{-2 \cdot x}}\right)\right)} \]

      log-div [=>]99.9%

      \[ 0 + \mathsf{expm1}\left(\color{blue}{\log 2 - \log \left(1 + e^{-2 \cdot x}\right)}\right) \]

      log1p-udef [<=]100.0%

      \[ 0 + \mathsf{expm1}\left(\log 2 - \color{blue}{\mathsf{log1p}\left(e^{-2 \cdot x}\right)}\right) \]

      exp-prod [=>]100.0%

      \[ 0 + \mathsf{expm1}\left(\log 2 - \mathsf{log1p}\left(\color{blue}{{\left(e^{-2}\right)}^{x}}\right)\right) \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{\mathsf{expm1}\left(\log 2 - \mathsf{log1p}\left({\left(e^{-2}\right)}^{x}\right)\right)} \]
      Step-by-step derivation

      [Start]100.0%

      \[ 0 + \mathsf{expm1}\left(\log 2 - \mathsf{log1p}\left({\left(e^{-2}\right)}^{x}\right)\right) \]

      +-lft-identity [=>]100.0%

      \[ \color{blue}{\mathsf{expm1}\left(\log 2 - \mathsf{log1p}\left({\left(e^{-2}\right)}^{x}\right)\right)} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification100.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;-2 \cdot x \leq -0.1:\\ \;\;\;\;\log \left(e^{\frac{2}{{\left(e^{-2}\right)}^{x} + 1} + -1}\right)\\ \mathbf{elif}\;-2 \cdot x \leq 2 \cdot 10^{-12}:\\ \;\;\;\;x + -0.3333333333333333 \cdot {x}^{3}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{expm1}\left(\log 2 - \mathsf{log1p}\left({\left(e^{-2}\right)}^{x}\right)\right)\\ \end{array} \]

Alternatives

Alternative 1
Accuracy99.6%
Cost26372
\[\begin{array}{l} \mathbf{if}\;-2 \cdot x \leq -0.1:\\ \;\;\;\;\log \left(e^{\frac{2}{{\left(e^{-2}\right)}^{x} + 1} + -1}\right)\\ \mathbf{elif}\;-2 \cdot x \leq 2 \cdot 10^{-12}:\\ \;\;\;\;x + -0.3333333333333333 \cdot {x}^{3}\\ \mathbf{else}:\\ \;\;\;\;-1 + \frac{2}{1 + {\left(e^{x}\right)}^{-2}}\\ \end{array} \]
Alternative 2
Accuracy99.6%
Cost26308
\[\begin{array}{l} t_0 := 1 + {\left(e^{x}\right)}^{-2}\\ \mathbf{if}\;-2 \cdot x \leq -0.1:\\ \;\;\;\;\mathsf{expm1}\left(-\log \left(t_0 \cdot 0.5\right)\right)\\ \mathbf{elif}\;-2 \cdot x \leq 2 \cdot 10^{-12}:\\ \;\;\;\;x + -0.3333333333333333 \cdot {x}^{3}\\ \mathbf{else}:\\ \;\;\;\;-1 + \frac{2}{t_0}\\ \end{array} \]
Alternative 3
Accuracy99.6%
Cost13832
\[\begin{array}{l} \mathbf{if}\;-2 \cdot x \leq -0.1:\\ \;\;\;\;-1 + \frac{2}{1 + e^{-2 \cdot x}}\\ \mathbf{elif}\;-2 \cdot x \leq 2 \cdot 10^{-12}:\\ \;\;\;\;x + -0.3333333333333333 \cdot {x}^{3}\\ \mathbf{else}:\\ \;\;\;\;-1 + \frac{2}{1 + {\left(e^{x}\right)}^{-2}}\\ \end{array} \]
Alternative 4
Accuracy99.6%
Cost7497
\[\begin{array}{l} \mathbf{if}\;-2 \cdot x \leq -0.1 \lor \neg \left(-2 \cdot x \leq 2 \cdot 10^{-12}\right):\\ \;\;\;\;-1 + \frac{2}{1 + e^{-2 \cdot x}}\\ \mathbf{else}:\\ \;\;\;\;x + -0.3333333333333333 \cdot {x}^{3}\\ \end{array} \]
Alternative 5
Accuracy78.6%
Cost708
\[\begin{array}{l} \mathbf{if}\;x \leq -0.68:\\ \;\;\;\;-1\\ \mathbf{else}:\\ \;\;\;\;\left(x \cdot 2\right) \cdot \frac{1}{x + 2}\\ \end{array} \]
Alternative 6
Accuracy79.1%
Cost584
\[\begin{array}{l} \mathbf{if}\;x \leq -1:\\ \;\;\;\;-1\\ \mathbf{elif}\;x \leq 2.6:\\ \;\;\;\;x\\ \mathbf{else}:\\ \;\;\;\;2 - \frac{4}{x}\\ \end{array} \]
Alternative 7
Accuracy79.1%
Cost328
\[\begin{array}{l} \mathbf{if}\;x \leq -1:\\ \;\;\;\;-1\\ \mathbf{elif}\;x \leq 2:\\ \;\;\;\;x\\ \mathbf{else}:\\ \;\;\;\;2\\ \end{array} \]
Alternative 8
Accuracy32.4%
Cost196
\[\begin{array}{l} \mathbf{if}\;x \leq 1.2 \cdot 10^{-308}:\\ \;\;\;\;-1\\ \mathbf{else}:\\ \;\;\;\;2\\ \end{array} \]
Alternative 9
Accuracy27.3%
Cost64
\[-1 \]

Reproduce?

herbie shell --seed 2023164 
(FPCore (x y)
  :name "Logistic function from Lakshay Garg"
  :precision binary64
  (- (/ 2.0 (+ 1.0 (exp (* -2.0 x)))) 1.0))