symmetry log of sum of exp

?

Percentage Accurate: 53.4% → 97.8%
Time: 15.5s
Precision: binary64
Cost: 19392

?

\[ \begin{array}{c}[a, b] = \mathsf{sort}([a, b])\\ \end{array} \]
\[\log \left(e^{a} + e^{b}\right) \]
\[\mathsf{log1p}\left(e^{a} + \mathsf{expm1}\left(b\right)\right) \]
(FPCore (a b) :precision binary64 (log (+ (exp a) (exp b))))
(FPCore (a b) :precision binary64 (log1p (+ (exp a) (expm1 b))))
double code(double a, double b) {
	return log((exp(a) + exp(b)));
}
double code(double a, double b) {
	return log1p((exp(a) + expm1(b)));
}
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) {
	return Math.log1p((Math.exp(a) + Math.expm1(b)));
}
def code(a, b):
	return math.log((math.exp(a) + math.exp(b)))
def code(a, b):
	return math.log1p((math.exp(a) + math.expm1(b)))
function code(a, b)
	return log(Float64(exp(a) + exp(b)))
end
function code(a, b)
	return log1p(Float64(exp(a) + expm1(b)))
end
code[a_, b_] := N[Log[N[(N[Exp[a], $MachinePrecision] + N[Exp[b], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
code[a_, b_] := N[Log[1 + N[(N[Exp[a], $MachinePrecision] + N[(Exp[b] - 1), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\log \left(e^{a} + e^{b}\right)
\mathsf{log1p}\left(e^{a} + \mathsf{expm1}\left(b\right)\right)

Local Percentage Accuracy?

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 8 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

The accuracy (vertical axis) and speed (horizontal axis) of each of Herbie's proposed alternatives. Up and to the right is better. Each dot represents an alternative program; the red square represents the initial program.

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Derivation?

  1. Initial program 56.9%

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

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

    [Start]56.9

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

    add-sqr-sqrt [=>]55.7

    \[ \log \color{blue}{\left(\sqrt{e^{a} + e^{b}} \cdot \sqrt{e^{a} + e^{b}}\right)} \]

    log-prod [=>]56.1

    \[ \color{blue}{\log \left(\sqrt{e^{a} + e^{b}}\right) + \log \left(\sqrt{e^{a} + e^{b}}\right)} \]
  3. Simplified75.8%

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

    [Start]56.1

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

    log-prod [<=]55.7

    \[ \color{blue}{\log \left(\sqrt{e^{a} + e^{b}} \cdot \sqrt{e^{a} + e^{b}}\right)} \]

    rem-square-sqrt [=>]56.9

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

    log1p-expm1 [<=]56.5

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

    expm1-def [<=]56.5

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

    rem-exp-log [=>]56.5

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

    associate--l+ [=>]56.6

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

    expm1-def [=>]75.8

    \[ \mathsf{log1p}\left(e^{a} + \color{blue}{\mathsf{expm1}\left(b\right)}\right) \]
  4. Final simplification75.8%

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

Alternatives

Alternative 1
Accuracy51.9%
Cost12996
\[\begin{array}{l} \mathbf{if}\;b \leq 5.4 \cdot 10^{-73}:\\ \;\;\;\;\mathsf{log1p}\left(e^{a}\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{log1p}\left(e^{b}\right)\\ \end{array} \]
Alternative 2
Accuracy95.7%
Cost12992
\[\mathsf{log1p}\left(e^{a} + b\right) \]
Alternative 3
Accuracy50.2%
Cost12864
\[\mathsf{log1p}\left(e^{a}\right) \]
Alternative 4
Accuracy49.2%
Cost6720
\[b \cdot 0.5 + \log 2 \]
Alternative 5
Accuracy48.9%
Cost6592
\[\log \left(b + 2\right) \]
Alternative 6
Accuracy48.4%
Cost6464
\[\log 2 \]
Alternative 7
Accuracy2.6%
Cost320
\[\frac{a}{b + 2} \]

Reproduce?

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