symmetry log of sum of exp

?

Percentage Accurate: 53.8% → 99.0%
Time: 14.5s
Precision: binary64
Cost: 19524

?

\[ \begin{array}{c}[a, b] = \mathsf{sort}([a, b])\\ \end{array} \]
\[\log \left(e^{a} + e^{b}\right) \]
\[\begin{array}{l} \mathbf{if}\;a \leq -38:\\ \;\;\;\;\frac{b}{e^{a} + 1}\\ \mathbf{else}:\\ \;\;\;\;\log \left(e^{a} + e^{b}\right)\\ \end{array} \]
(FPCore (a b) :precision binary64 (log (+ (exp a) (exp b))))
(FPCore (a b)
 :precision binary64
 (if (<= a -38.0) (/ b (+ (exp a) 1.0)) (log (+ (exp a) (exp b)))))
double code(double a, double b) {
	return log((exp(a) + exp(b)));
}
double code(double a, double b) {
	double tmp;
	if (a <= -38.0) {
		tmp = b / (exp(a) + 1.0);
	} else {
		tmp = log((exp(a) + exp(b)));
	}
	return tmp;
}
real(8) function code(a, b)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    code = log((exp(a) + exp(b)))
end function
real(8) function code(a, b)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    real(8) :: tmp
    if (a <= (-38.0d0)) then
        tmp = b / (exp(a) + 1.0d0)
    else
        tmp = log((exp(a) + exp(b)))
    end if
    code = tmp
end function
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 tmp;
	if (a <= -38.0) {
		tmp = b / (Math.exp(a) + 1.0);
	} else {
		tmp = Math.log((Math.exp(a) + Math.exp(b)));
	}
	return tmp;
}
def code(a, b):
	return math.log((math.exp(a) + math.exp(b)))
def code(a, b):
	tmp = 0
	if a <= -38.0:
		tmp = b / (math.exp(a) + 1.0)
	else:
		tmp = math.log((math.exp(a) + math.exp(b)))
	return tmp
function code(a, b)
	return log(Float64(exp(a) + exp(b)))
end
function code(a, b)
	tmp = 0.0
	if (a <= -38.0)
		tmp = Float64(b / Float64(exp(a) + 1.0));
	else
		tmp = log(Float64(exp(a) + exp(b)));
	end
	return tmp
end
function tmp = code(a, b)
	tmp = log((exp(a) + exp(b)));
end
function tmp_2 = code(a, b)
	tmp = 0.0;
	if (a <= -38.0)
		tmp = b / (exp(a) + 1.0);
	else
		tmp = log((exp(a) + exp(b)));
	end
	tmp_2 = tmp;
end
code[a_, b_] := N[Log[N[(N[Exp[a], $MachinePrecision] + N[Exp[b], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
code[a_, b_] := If[LessEqual[a, -38.0], N[(b / N[(N[Exp[a], $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision], N[Log[N[(N[Exp[a], $MachinePrecision] + N[Exp[b], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\log \left(e^{a} + e^{b}\right)
\begin{array}{l}
\mathbf{if}\;a \leq -38:\\
\;\;\;\;\frac{b}{e^{a} + 1}\\

\mathbf{else}:\\
\;\;\;\;\log \left(e^{a} + 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 12 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 2 regimes
  2. if a < -38

    1. Initial program 7.7%

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

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

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

      [Start]100.0%

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

      log1p-def [=>]100.0%

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

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

    if -38 < a

    1. Initial program 66.2%

      \[\log \left(e^{a} + e^{b}\right) \]
  3. Recombined 2 regimes into one program.
  4. Final simplification73.2%

    \[\leadsto \begin{array}{l} \mathbf{if}\;a \leq -38:\\ \;\;\;\;\frac{b}{e^{a} + 1}\\ \mathbf{else}:\\ \;\;\;\;\log \left(e^{a} + e^{b}\right)\\ \end{array} \]

Alternatives

Alternative 1
Accuracy98.1%
Cost19652
\[\begin{array}{l} \mathbf{if}\;e^{a} \leq 0:\\ \;\;\;\;\frac{b}{e^{a} + 1}\\ \mathbf{else}:\\ \;\;\;\;\log \left(e^{a} + \left(b + 1\right)\right)\\ \end{array} \]
Alternative 2
Accuracy98.5%
Cost19648
\[\mathsf{log1p}\left(e^{a}\right) + \frac{b}{e^{a} + 1} \]
Alternative 3
Accuracy97.9%
Cost19396
\[\begin{array}{l} \mathbf{if}\;e^{a} \leq 0:\\ \;\;\;\;\frac{b}{e^{a} + 1}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{log1p}\left(e^{b}\right)\\ \end{array} \]
Alternative 4
Accuracy98.3%
Cost19392
\[\mathsf{log1p}\left(e^{a} + \mathsf{expm1}\left(b\right)\right) \]
Alternative 5
Accuracy98.4%
Cost13636
\[\begin{array}{l} \mathbf{if}\;a \leq -38:\\ \;\;\;\;\frac{b}{e^{a} + 1}\\ \mathbf{else}:\\ \;\;\;\;\log \left(1 + \left(e^{a} + \left(b + 0.5 \cdot \left(b \cdot b\right)\right)\right)\right)\\ \end{array} \]
Alternative 6
Accuracy97.5%
Cost13508
\[\begin{array}{l} \mathbf{if}\;e^{a} \leq 0:\\ \;\;\;\;\frac{b}{e^{a} + 1}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{b + 2} + \log \left(b + 2\right)\\ \end{array} \]
Alternative 7
Accuracy97.5%
Cost13252
\[\begin{array}{l} \mathbf{if}\;e^{a} \leq 0:\\ \;\;\;\;\frac{b}{e^{a} + 1}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{log1p}\left(a + \left(b + 1\right)\right)\\ \end{array} \]
Alternative 8
Accuracy49.5%
Cost6720
\[b \cdot 0.5 + \log 2 \]
Alternative 9
Accuracy49.3%
Cost6592
\[\log \left(b + 2\right) \]
Alternative 10
Accuracy48.8%
Cost6464
\[\log 2 \]
Alternative 11
Accuracy2.6%
Cost320
\[\frac{a}{b + 2} \]

Reproduce?

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