symmetry log of sum of exp

?

Percentage Accurate: 61.6% → 94.1%
Time: 13.4s
Precision: binary64
Cost: 19788

?

\[ \begin{array}{c}[a, b] = \mathsf{sort}([a, b])\\ \end{array} \]
\[\log \left(e^{a} + e^{b}\right) \]
\[\begin{array}{l} t_0 := \frac{b}{e^{a} + 1}\\ \mathbf{if}\;b \leq -1 \cdot 10^{+79}:\\ \;\;\;\;{a}^{3} \cdot \left(b \cdot \left(--0.020833333333333332\right)\right)\\ \mathbf{elif}\;b \leq -24:\\ \;\;\;\;t_0\\ \mathbf{elif}\;b \leq 720:\\ \;\;\;\;\mathsf{log1p}\left(e^{a} + \mathsf{expm1}\left(b\right)\right)\\ \mathbf{elif}\;b \leq 2.4 \cdot 10^{+79}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\mathsf{log1p}\left(e^{b}\right)\\ \end{array} \]
(FPCore (a b) :precision binary64 (log (+ (exp a) (exp b))))
(FPCore (a b)
 :precision binary64
 (let* ((t_0 (/ b (+ (exp a) 1.0))))
   (if (<= b -1e+79)
     (* (pow a 3.0) (* b (- -0.020833333333333332)))
     (if (<= b -24.0)
       t_0
       (if (<= b 720.0)
         (log1p (+ (exp a) (expm1 b)))
         (if (<= b 2.4e+79) t_0 (log1p (exp b))))))))
double code(double a, double b) {
	return log((exp(a) + exp(b)));
}
double code(double a, double b) {
	double t_0 = b / (exp(a) + 1.0);
	double tmp;
	if (b <= -1e+79) {
		tmp = pow(a, 3.0) * (b * -(-0.020833333333333332));
	} else if (b <= -24.0) {
		tmp = t_0;
	} else if (b <= 720.0) {
		tmp = log1p((exp(a) + expm1(b)));
	} else if (b <= 2.4e+79) {
		tmp = t_0;
	} else {
		tmp = log1p(exp(b));
	}
	return tmp;
}
public static double code(double a, double b) {
	return Math.log((Math.exp(a) + Math.exp(b)));
}
public static double code(double a, double b) {
	double t_0 = b / (Math.exp(a) + 1.0);
	double tmp;
	if (b <= -1e+79) {
		tmp = Math.pow(a, 3.0) * (b * -(-0.020833333333333332));
	} else if (b <= -24.0) {
		tmp = t_0;
	} else if (b <= 720.0) {
		tmp = Math.log1p((Math.exp(a) + Math.expm1(b)));
	} else if (b <= 2.4e+79) {
		tmp = t_0;
	} else {
		tmp = Math.log1p(Math.exp(b));
	}
	return tmp;
}
def code(a, b):
	return math.log((math.exp(a) + math.exp(b)))
def code(a, b):
	t_0 = b / (math.exp(a) + 1.0)
	tmp = 0
	if b <= -1e+79:
		tmp = math.pow(a, 3.0) * (b * -(-0.020833333333333332))
	elif b <= -24.0:
		tmp = t_0
	elif b <= 720.0:
		tmp = math.log1p((math.exp(a) + math.expm1(b)))
	elif b <= 2.4e+79:
		tmp = t_0
	else:
		tmp = math.log1p(math.exp(b))
	return tmp
function code(a, b)
	return log(Float64(exp(a) + exp(b)))
end
function code(a, b)
	t_0 = Float64(b / Float64(exp(a) + 1.0))
	tmp = 0.0
	if (b <= -1e+79)
		tmp = Float64((a ^ 3.0) * Float64(b * Float64(-(-0.020833333333333332))));
	elseif (b <= -24.0)
		tmp = t_0;
	elseif (b <= 720.0)
		tmp = log1p(Float64(exp(a) + expm1(b)));
	elseif (b <= 2.4e+79)
		tmp = t_0;
	else
		tmp = log1p(exp(b));
	end
	return tmp
end
code[a_, b_] := N[Log[N[(N[Exp[a], $MachinePrecision] + N[Exp[b], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
code[a_, b_] := Block[{t$95$0 = N[(b / N[(N[Exp[a], $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[b, -1e+79], N[(N[Power[a, 3.0], $MachinePrecision] * N[(b * (--0.020833333333333332)), $MachinePrecision]), $MachinePrecision], If[LessEqual[b, -24.0], t$95$0, If[LessEqual[b, 720.0], N[Log[1 + N[(N[Exp[a], $MachinePrecision] + N[(Exp[b] - 1), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], If[LessEqual[b, 2.4e+79], t$95$0, N[Log[1 + N[Exp[b], $MachinePrecision]], $MachinePrecision]]]]]]
\log \left(e^{a} + e^{b}\right)
\begin{array}{l}
t_0 := \frac{b}{e^{a} + 1}\\
\mathbf{if}\;b \leq -1 \cdot 10^{+79}:\\
\;\;\;\;{a}^{3} \cdot \left(b \cdot \left(--0.020833333333333332\right)\right)\\

\mathbf{elif}\;b \leq -24:\\
\;\;\;\;t_0\\

\mathbf{elif}\;b \leq 720:\\
\;\;\;\;\mathsf{log1p}\left(e^{a} + \mathsf{expm1}\left(b\right)\right)\\

\mathbf{elif}\;b \leq 2.4 \cdot 10^{+79}:\\
\;\;\;\;t_0\\

\mathbf{else}:\\
\;\;\;\;\mathsf{log1p}\left(e^{b}\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 15 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 4 regimes
  2. if b < -9.99999999999999967e78

    1. Initial program 17.2%

      \[\log \left(e^{a} + e^{b}\right) \]
    2. Taylor expanded in b around 0 11.6%

      \[\leadsto \color{blue}{\log \left(1 + e^{a}\right) + \frac{b}{1 + e^{a}}} \]
    3. Simplified11.6%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{a}\right) + \frac{b}{1 + e^{a}}} \]
      Step-by-step derivation

      [Start]11.6%

      \[ \log \left(1 + e^{a}\right) + \frac{b}{1 + e^{a}} \]

      log1p-def [=>]11.6%

      \[ \color{blue}{\mathsf{log1p}\left(e^{a}\right)} + \frac{b}{1 + e^{a}} \]
    4. Taylor expanded in a around 0 5.3%

      \[\leadsto \color{blue}{\left(0.5 - 0.25 \cdot b\right) \cdot a + \left(0.5 \cdot b + \left(-1 \cdot \left({a}^{3} \cdot \left(0.041666666666666664 \cdot b + \left(-0.0625 \cdot b + -0.5 \cdot \left(0.125 \cdot b + -0.125 \cdot b\right)\right)\right)\right) + \left({a}^{2} \cdot \left(0.125 - \left(0.125 \cdot b + -0.125 \cdot b\right)\right) + \log 2\right)\right)\right)} \]
    5. Taylor expanded in a around inf 5.3%

      \[\leadsto \left(0.5 - 0.25 \cdot b\right) \cdot a + \left(0.5 \cdot b + \color{blue}{-1 \cdot \left({a}^{3} \cdot \left(0.041666666666666664 \cdot b + \left(-0.0625 \cdot b + -0.5 \cdot \left(0.125 \cdot b + -0.125 \cdot b\right)\right)\right)\right)}\right) \]
    6. Taylor expanded in a around inf 17.8%

      \[\leadsto \color{blue}{-1 \cdot \left({a}^{3} \cdot \left(0.041666666666666664 \cdot b + \left(-0.0625 \cdot b + -0.5 \cdot \left(0.125 \cdot b + -0.125 \cdot b\right)\right)\right)\right)} \]
    7. Simplified17.8%

      \[\leadsto \color{blue}{\left(b \cdot -0.020833333333333332\right) \cdot \left(-{a}^{3}\right)} \]
      Step-by-step derivation

      [Start]17.8%

      \[ -1 \cdot \left({a}^{3} \cdot \left(0.041666666666666664 \cdot b + \left(-0.0625 \cdot b + -0.5 \cdot \left(0.125 \cdot b + -0.125 \cdot b\right)\right)\right)\right) \]

      mul-1-neg [=>]17.8%

      \[ \color{blue}{-{a}^{3} \cdot \left(0.041666666666666664 \cdot b + \left(-0.0625 \cdot b + -0.5 \cdot \left(0.125 \cdot b + -0.125 \cdot b\right)\right)\right)} \]

      *-commutative [=>]17.8%

      \[ -\color{blue}{\left(0.041666666666666664 \cdot b + \left(-0.0625 \cdot b + -0.5 \cdot \left(0.125 \cdot b + -0.125 \cdot b\right)\right)\right) \cdot {a}^{3}} \]

      distribute-rgt-neg-in [=>]17.8%

      \[ \color{blue}{\left(0.041666666666666664 \cdot b + \left(-0.0625 \cdot b + -0.5 \cdot \left(0.125 \cdot b + -0.125 \cdot b\right)\right)\right) \cdot \left(-{a}^{3}\right)} \]

      distribute-rgt-out [=>]17.8%

      \[ \left(0.041666666666666664 \cdot b + \left(-0.0625 \cdot b + -0.5 \cdot \color{blue}{\left(b \cdot \left(0.125 + -0.125\right)\right)}\right)\right) \cdot \left(-{a}^{3}\right) \]

      metadata-eval [=>]17.8%

      \[ \left(0.041666666666666664 \cdot b + \left(-0.0625 \cdot b + -0.5 \cdot \left(b \cdot \color{blue}{0}\right)\right)\right) \cdot \left(-{a}^{3}\right) \]

      mul0-rgt [=>]17.8%

      \[ \left(0.041666666666666664 \cdot b + \left(-0.0625 \cdot b + -0.5 \cdot \color{blue}{0}\right)\right) \cdot \left(-{a}^{3}\right) \]

      metadata-eval [=>]17.8%

      \[ \left(0.041666666666666664 \cdot b + \left(-0.0625 \cdot b + \color{blue}{0}\right)\right) \cdot \left(-{a}^{3}\right) \]

      +-rgt-identity [=>]17.8%

      \[ \left(0.041666666666666664 \cdot b + \color{blue}{-0.0625 \cdot b}\right) \cdot \left(-{a}^{3}\right) \]

      distribute-rgt-out [=>]17.8%

      \[ \color{blue}{\left(b \cdot \left(0.041666666666666664 + -0.0625\right)\right)} \cdot \left(-{a}^{3}\right) \]

      metadata-eval [=>]17.8%

      \[ \left(b \cdot \color{blue}{-0.020833333333333332}\right) \cdot \left(-{a}^{3}\right) \]

    if -9.99999999999999967e78 < b < -24 or 720 < b < 2.39999999999999986e79

    1. Initial program 15.8%

      \[\log \left(e^{a} + e^{b}\right) \]
    2. Taylor expanded in b around 0 40.3%

      \[\leadsto \color{blue}{\log \left(1 + e^{a}\right) + \frac{b}{1 + e^{a}}} \]
    3. Simplified40.3%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{a}\right) + \frac{b}{1 + e^{a}}} \]
      Step-by-step derivation

      [Start]40.3%

      \[ \log \left(1 + e^{a}\right) + \frac{b}{1 + e^{a}} \]

      log1p-def [=>]40.3%

      \[ \color{blue}{\mathsf{log1p}\left(e^{a}\right)} + \frac{b}{1 + e^{a}} \]
    4. Taylor expanded in b around inf 27.9%

      \[\leadsto \color{blue}{\frac{b}{1 + e^{a}}} \]

    if -24 < b < 720

    1. Initial program 69.9%

      \[\log \left(e^{a} + e^{b}\right) \]
    2. Applied egg-rr69.9%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(\left(e^{a} + e^{b}\right) - 1\right)} \]
      Step-by-step derivation

      [Start]69.9%

      \[ \log \left(e^{a} + e^{b}\right) \]

      log1p-expm1-u [=>]69.9%

      \[ \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\log \left(e^{a} + e^{b}\right)\right)\right)} \]

      expm1-udef [=>]69.9%

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

      add-exp-log [<=]69.9%

      \[ \mathsf{log1p}\left(\color{blue}{\left(e^{a} + e^{b}\right)} - 1\right) \]
    3. Simplified91.2%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{a} + \mathsf{expm1}\left(b\right)\right)} \]
      Step-by-step derivation

      [Start]69.9%

      \[ \mathsf{log1p}\left(\left(e^{a} + e^{b}\right) - 1\right) \]

      associate--l+ [=>]69.9%

      \[ \mathsf{log1p}\left(\color{blue}{e^{a} + \left(e^{b} - 1\right)}\right) \]

      expm1-def [=>]91.2%

      \[ \mathsf{log1p}\left(e^{a} + \color{blue}{\mathsf{expm1}\left(b\right)}\right) \]

    if 2.39999999999999986e79 < b

    1. Initial program 100.0%

      \[\log \left(e^{a} + e^{b}\right) \]
    2. Taylor expanded in a around 0 100.0%

      \[\leadsto \color{blue}{\log \left(1 + e^{b}\right)} \]
    3. Simplified100.0%

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

      [Start]100.0%

      \[ \log \left(1 + e^{b}\right) \]

      log1p-def [=>]100.0%

      \[ \color{blue}{\mathsf{log1p}\left(e^{b}\right)} \]
  3. Recombined 4 regimes into one program.
  4. Final simplification67.2%

    \[\leadsto \begin{array}{l} \mathbf{if}\;b \leq -1 \cdot 10^{+79}:\\ \;\;\;\;{a}^{3} \cdot \left(b \cdot \left(--0.020833333333333332\right)\right)\\ \mathbf{elif}\;b \leq -24:\\ \;\;\;\;\frac{b}{e^{a} + 1}\\ \mathbf{elif}\;b \leq 720:\\ \;\;\;\;\mathsf{log1p}\left(e^{a} + \mathsf{expm1}\left(b\right)\right)\\ \mathbf{elif}\;b \leq 2.4 \cdot 10^{+79}:\\ \;\;\;\;\frac{b}{e^{a} + 1}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{log1p}\left(e^{b}\right)\\ \end{array} \]

Alternatives

Alternative 1
Accuracy94.1%
Cost19788
\[\begin{array}{l} t_0 := \frac{b}{e^{a} + 1}\\ \mathbf{if}\;b \leq -1 \cdot 10^{+79}:\\ \;\;\;\;{a}^{3} \cdot \left(b \cdot \left(--0.020833333333333332\right)\right)\\ \mathbf{elif}\;b \leq -24:\\ \;\;\;\;t_0\\ \mathbf{elif}\;b \leq 720:\\ \;\;\;\;\mathsf{log1p}\left(e^{a} + \mathsf{expm1}\left(b\right)\right)\\ \mathbf{elif}\;b \leq 2.4 \cdot 10^{+79}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\mathsf{log1p}\left(e^{b}\right)\\ \end{array} \]
Alternative 2
Accuracy86.5%
Cost32452
\[\begin{array}{l} t_0 := e^{a} + e^{b}\\ \mathbf{if}\;t_0 \leq 1.5:\\ \;\;\;\;\frac{b}{e^{a} + 1}\\ \mathbf{else}:\\ \;\;\;\;\log t_0\\ \end{array} \]
Alternative 3
Accuracy93.7%
Cost19912
\[\begin{array}{l} \mathbf{if}\;b \leq -1 \cdot 10^{+79}:\\ \;\;\;\;{a}^{3} \cdot \left(b \cdot \left(--0.020833333333333332\right)\right)\\ \mathbf{elif}\;b \leq 2.4 \cdot 10^{+79}:\\ \;\;\;\;\mathsf{log1p}\left(e^{a}\right) + \frac{b}{e^{a} + 1}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{log1p}\left(e^{b}\right)\\ \end{array} \]
Alternative 4
Accuracy65.3%
Cost13260
\[\begin{array}{l} \mathbf{if}\;a \leq -1850:\\ \;\;\;\;\frac{b}{e^{a} + 1}\\ \mathbf{elif}\;a \leq 2.1 \cdot 10^{-245}:\\ \;\;\;\;a \cdot \left(0.5 - b \cdot 0.25\right) + \left(b \cdot 0.5 + \log 2\right)\\ \mathbf{elif}\;a \leq 2.1 \cdot 10^{-44}:\\ \;\;\;\;b \cdot \left(\frac{1}{1 - e^{a \cdot 2}} - \frac{-0.5}{a}\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{log1p}\left(e^{a}\right)\\ \end{array} \]
Alternative 5
Accuracy80.6%
Cost12996
\[\begin{array}{l} \mathbf{if}\;a \leq -1850:\\ \;\;\;\;\frac{b}{e^{a} + 1}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{log1p}\left(e^{b}\right)\\ \end{array} \]
Alternative 6
Accuracy64.8%
Cost7628
\[\begin{array}{l} \mathbf{if}\;a \leq -11:\\ \;\;\;\;\frac{b}{e^{a} + 1}\\ \mathbf{elif}\;a \leq 2.1 \cdot 10^{-245}:\\ \;\;\;\;b \cdot 0.5 + \log 2\\ \mathbf{elif}\;a \leq 2.2:\\ \;\;\;\;b \cdot \left(\frac{1}{1 - e^{a \cdot 2}} - \frac{-0.5}{a}\right)\\ \mathbf{else}:\\ \;\;\;\;{a}^{3} \cdot \left(b \cdot \left(--0.020833333333333332\right)\right)\\ \end{array} \]
Alternative 7
Accuracy65.0%
Cost7628
\[\begin{array}{l} \mathbf{if}\;a \leq -1850:\\ \;\;\;\;\frac{b}{e^{a} + 1}\\ \mathbf{elif}\;a \leq 2.1 \cdot 10^{-245}:\\ \;\;\;\;a \cdot \left(0.5 - b \cdot 0.25\right) + \left(b \cdot 0.5 + \log 2\right)\\ \mathbf{elif}\;a \leq 215:\\ \;\;\;\;b \cdot \left(\frac{1}{1 - e^{a \cdot 2}} - \frac{-0.5}{a}\right)\\ \mathbf{else}:\\ \;\;\;\;{a}^{3} \cdot \left(b \cdot \left(--0.020833333333333332\right)\right)\\ \end{array} \]
Alternative 8
Accuracy62.0%
Cost7432
\[\begin{array}{l} \mathbf{if}\;a \leq -8:\\ \;\;\;\;\frac{b}{e^{a} + 1}\\ \mathbf{elif}\;a \leq 5 \cdot 10^{-22}:\\ \;\;\;\;b \cdot 0.5 + \log 2\\ \mathbf{else}:\\ \;\;\;\;b \cdot \left(0.5 + \left({a}^{3} \cdot 0.020833333333333332 + a \cdot -0.25\right)\right)\\ \end{array} \]
Alternative 9
Accuracy62.0%
Cost7112
\[\begin{array}{l} \mathbf{if}\;a \leq -17:\\ \;\;\;\;\frac{b}{e^{a} + 1}\\ \mathbf{elif}\;a \leq 1000:\\ \;\;\;\;b \cdot 0.5 + \log 2\\ \mathbf{else}:\\ \;\;\;\;{a}^{3} \cdot \left(b \cdot \left(--0.020833333333333332\right)\right)\\ \end{array} \]
Alternative 10
Accuracy31.6%
Cost6852
\[\begin{array}{l} \mathbf{if}\;a \leq -1.4:\\ \;\;\;\;b \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;\log 2 + a \cdot 0.5\\ \end{array} \]
Alternative 11
Accuracy33.1%
Cost6852
\[\begin{array}{l} \mathbf{if}\;a \leq -1850:\\ \;\;\;\;b \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;b \cdot 0.5 + \log 2\\ \end{array} \]
Alternative 12
Accuracy57.9%
Cost6852
\[\begin{array}{l} \mathbf{if}\;a \leq -9.5:\\ \;\;\;\;\frac{b}{e^{a} + 1}\\ \mathbf{else}:\\ \;\;\;\;b \cdot 0.5 + \log 2\\ \end{array} \]
Alternative 13
Accuracy31.3%
Cost6596
\[\begin{array}{l} \mathbf{if}\;a \leq -12.5:\\ \;\;\;\;b \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;\log 2\\ \end{array} \]
Alternative 14
Accuracy15.4%
Cost580
\[\begin{array}{l} \mathbf{if}\;b \leq 1.2 \cdot 10^{+83}:\\ \;\;\;\;b \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;b \cdot \left(0.5 + a \cdot -0.25\right)\\ \end{array} \]
Alternative 15
Accuracy10.4%
Cost192
\[b \cdot 0.5 \]

Reproduce?

herbie shell --seed 2023255 
(FPCore (a b)
  :name "symmetry log of sum of exp"
  :precision binary64
  (log (+ (exp a) (exp b))))