symmetry log of sum of exp

Percentage Accurate: 53.7% → 98.9%
Time: 13.2s
Alternatives: 19
Speedup: 2.8×

Specification

?
\[\begin{array}{l} \\ \log \left(e^{a} + e^{b}\right) \end{array} \]
(FPCore (a b) :precision binary64 (log (+ (exp a) (exp b))))
double code(double a, double b) {
	return log((exp(a) + exp(b)));
}
real(8) function code(a, b)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    code = log((exp(a) + exp(b)))
end function
public static double code(double a, double b) {
	return Math.log((Math.exp(a) + Math.exp(b)));
}
def code(a, b):
	return math.log((math.exp(a) + math.exp(b)))
function code(a, b)
	return log(Float64(exp(a) + exp(b)))
end
function tmp = code(a, b)
	tmp = log((exp(a) + exp(b)));
end
code[a_, b_] := N[Log[N[(N[Exp[a], $MachinePrecision] + N[Exp[b], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
\log \left(e^{a} + e^{b}\right)
\end{array}

Sampling outcomes in binary64 precision:

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.

Accuracy vs Speed?

Herbie found 19 alternatives:

AlternativeAccuracySpeedup
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.

Initial Program: 53.7% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \log \left(e^{a} + e^{b}\right) \end{array} \]
(FPCore (a b) :precision binary64 (log (+ (exp a) (exp b))))
double code(double a, double b) {
	return log((exp(a) + exp(b)));
}
real(8) function code(a, b)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    code = log((exp(a) + exp(b)))
end function
public static double code(double a, double b) {
	return Math.log((Math.exp(a) + Math.exp(b)));
}
def code(a, b):
	return math.log((math.exp(a) + math.exp(b)))
function code(a, b)
	return log(Float64(exp(a) + exp(b)))
end
function tmp = code(a, b)
	tmp = log((exp(a) + exp(b)));
end
code[a_, b_] := N[Log[N[(N[Exp[a], $MachinePrecision] + N[Exp[b], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
\log \left(e^{a} + e^{b}\right)
\end{array}

Alternative 1: 98.9% accurate, 1.0× speedup?

\[\begin{array}{l} [a, b] = \mathsf{sort}([a, b])\\ \\ \begin{array}{l} \mathbf{if}\;a \leq -37:\\ \;\;\;\;\frac{b}{1 + e^{a}}\\ \mathbf{else}:\\ \;\;\;\;\log \left(e^{a} + e^{b}\right)\\ \end{array} \end{array} \]
NOTE: a and b should be sorted in increasing order before calling this function.
(FPCore (a b)
 :precision binary64
 (if (<= a -37.0) (/ b (+ 1.0 (exp a))) (log (+ (exp a) (exp b)))))
assert(a < b);
double code(double a, double b) {
	double tmp;
	if (a <= -37.0) {
		tmp = b / (1.0 + exp(a));
	} else {
		tmp = log((exp(a) + exp(b)));
	}
	return tmp;
}
NOTE: a and b should be sorted in increasing order before calling this function.
real(8) function code(a, b)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    real(8) :: tmp
    if (a <= (-37.0d0)) then
        tmp = b / (1.0d0 + exp(a))
    else
        tmp = log((exp(a) + exp(b)))
    end if
    code = tmp
end function
assert a < b;
public static double code(double a, double b) {
	double tmp;
	if (a <= -37.0) {
		tmp = b / (1.0 + Math.exp(a));
	} else {
		tmp = Math.log((Math.exp(a) + Math.exp(b)));
	}
	return tmp;
}
[a, b] = sort([a, b])
def code(a, b):
	tmp = 0
	if a <= -37.0:
		tmp = b / (1.0 + math.exp(a))
	else:
		tmp = math.log((math.exp(a) + math.exp(b)))
	return tmp
a, b = sort([a, b])
function code(a, b)
	tmp = 0.0
	if (a <= -37.0)
		tmp = Float64(b / Float64(1.0 + exp(a)));
	else
		tmp = log(Float64(exp(a) + exp(b)));
	end
	return tmp
end
a, b = num2cell(sort([a, b])){:}
function tmp_2 = code(a, b)
	tmp = 0.0;
	if (a <= -37.0)
		tmp = b / (1.0 + exp(a));
	else
		tmp = log((exp(a) + exp(b)));
	end
	tmp_2 = tmp;
end
NOTE: a and b should be sorted in increasing order before calling this function.
code[a_, b_] := If[LessEqual[a, -37.0], N[(b / N[(1.0 + N[Exp[a], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[Log[N[(N[Exp[a], $MachinePrecision] + N[Exp[b], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\begin{array}{l}
\mathbf{if}\;a \leq -37:\\
\;\;\;\;\frac{b}{1 + e^{a}}\\

\mathbf{else}:\\
\;\;\;\;\log \left(e^{a} + e^{b}\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if a < -37

    1. Initial program 10.4%

      \[\log \left(e^{a} + e^{b}\right) \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-log.f64N/A

        \[\leadsto \color{blue}{\log \left(e^{a} + e^{b}\right)} \]
      2. lift-+.f64N/A

        \[\leadsto \log \color{blue}{\left(e^{a} + e^{b}\right)} \]
      3. flip-+N/A

        \[\leadsto \log \color{blue}{\left(\frac{e^{a} \cdot e^{a} - e^{b} \cdot e^{b}}{e^{a} - e^{b}}\right)} \]
      4. clear-numN/A

        \[\leadsto \log \color{blue}{\left(\frac{1}{\frac{e^{a} - e^{b}}{e^{a} \cdot e^{a} - e^{b} \cdot e^{b}}}\right)} \]
      5. log-recN/A

        \[\leadsto \color{blue}{\mathsf{neg}\left(\log \left(\frac{e^{a} - e^{b}}{e^{a} \cdot e^{a} - e^{b} \cdot e^{b}}\right)\right)} \]
      6. lower-neg.f64N/A

        \[\leadsto \color{blue}{\mathsf{neg}\left(\log \left(\frac{e^{a} - e^{b}}{e^{a} \cdot e^{a} - e^{b} \cdot e^{b}}\right)\right)} \]
      7. lower-log.f64N/A

        \[\leadsto \mathsf{neg}\left(\color{blue}{\log \left(\frac{e^{a} - e^{b}}{e^{a} \cdot e^{a} - e^{b} \cdot e^{b}}\right)}\right) \]
      8. clear-numN/A

        \[\leadsto \mathsf{neg}\left(\log \color{blue}{\left(\frac{1}{\frac{e^{a} \cdot e^{a} - e^{b} \cdot e^{b}}{e^{a} - e^{b}}}\right)}\right) \]
      9. flip-+N/A

        \[\leadsto \mathsf{neg}\left(\log \left(\frac{1}{\color{blue}{e^{a} + e^{b}}}\right)\right) \]
      10. lift-+.f64N/A

        \[\leadsto \mathsf{neg}\left(\log \left(\frac{1}{\color{blue}{e^{a} + e^{b}}}\right)\right) \]
      11. lower-/.f6410.4

        \[\leadsto -\log \color{blue}{\left(\frac{1}{e^{a} + e^{b}}\right)} \]
    4. Applied rewrites10.4%

      \[\leadsto \color{blue}{-\log \left(\frac{1}{e^{a} + e^{b}}\right)} \]
    5. Taylor expanded in b around 0

      \[\leadsto \color{blue}{\frac{b}{1 + e^{a}} - \log \left(\frac{1}{1 + e^{a}}\right)} \]
    6. Step-by-step derivation
      1. sub-negN/A

        \[\leadsto \color{blue}{\frac{b}{1 + e^{a}} + \left(\mathsf{neg}\left(\log \left(\frac{1}{1 + e^{a}}\right)\right)\right)} \]
      2. *-rgt-identityN/A

        \[\leadsto \frac{\color{blue}{b \cdot 1}}{1 + e^{a}} + \left(\mathsf{neg}\left(\log \left(\frac{1}{1 + e^{a}}\right)\right)\right) \]
      3. associate-*r/N/A

        \[\leadsto \color{blue}{b \cdot \frac{1}{1 + e^{a}}} + \left(\mathsf{neg}\left(\log \left(\frac{1}{1 + e^{a}}\right)\right)\right) \]
      4. log-recN/A

        \[\leadsto b \cdot \frac{1}{1 + e^{a}} + \left(\mathsf{neg}\left(\color{blue}{\left(\mathsf{neg}\left(\log \left(1 + e^{a}\right)\right)\right)}\right)\right) \]
      5. remove-double-negN/A

        \[\leadsto b \cdot \frac{1}{1 + e^{a}} + \color{blue}{\log \left(1 + e^{a}\right)} \]
      6. +-commutativeN/A

        \[\leadsto \color{blue}{\log \left(1 + e^{a}\right) + b \cdot \frac{1}{1 + e^{a}}} \]
      7. lower-+.f64N/A

        \[\leadsto \color{blue}{\log \left(1 + e^{a}\right) + b \cdot \frac{1}{1 + e^{a}}} \]
      8. lower-log1p.f64N/A

        \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{a}\right)} + b \cdot \frac{1}{1 + e^{a}} \]
      9. lower-exp.f64N/A

        \[\leadsto \mathsf{log1p}\left(\color{blue}{e^{a}}\right) + b \cdot \frac{1}{1 + e^{a}} \]
      10. associate-*r/N/A

        \[\leadsto \mathsf{log1p}\left(e^{a}\right) + \color{blue}{\frac{b \cdot 1}{1 + e^{a}}} \]
      11. *-rgt-identityN/A

        \[\leadsto \mathsf{log1p}\left(e^{a}\right) + \frac{\color{blue}{b}}{1 + e^{a}} \]
      12. lower-/.f64N/A

        \[\leadsto \mathsf{log1p}\left(e^{a}\right) + \color{blue}{\frac{b}{1 + e^{a}}} \]
      13. lower-+.f64N/A

        \[\leadsto \mathsf{log1p}\left(e^{a}\right) + \frac{b}{\color{blue}{1 + e^{a}}} \]
      14. lower-exp.f64100.0

        \[\leadsto \mathsf{log1p}\left(e^{a}\right) + \frac{b}{1 + \color{blue}{e^{a}}} \]
    7. Applied rewrites100.0%

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

      \[\leadsto \frac{b}{\color{blue}{1 + e^{a}}} \]
    9. Step-by-step derivation
      1. Applied rewrites100.0%

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

      if -37 < a

      1. Initial program 94.9%

        \[\log \left(e^{a} + e^{b}\right) \]
      2. Add Preprocessing
    10. Recombined 2 regimes into one program.
    11. Add Preprocessing

    Alternative 2: 98.6% accurate, 0.5× speedup?

    \[\begin{array}{l} [a, b] = \mathsf{sort}([a, b])\\ \\ \begin{array}{l} t_0 := 1 + e^{a}\\ \mathsf{fma}\left(b, \mathsf{fma}\left(\mathsf{fma}\left(b, 0.5, 1\right), \frac{1}{t\_0}, \frac{b \cdot -0.5}{{t\_0}^{2}}\right), \mathsf{log1p}\left(e^{a}\right)\right) \end{array} \end{array} \]
    NOTE: a and b should be sorted in increasing order before calling this function.
    (FPCore (a b)
     :precision binary64
     (let* ((t_0 (+ 1.0 (exp a))))
       (fma
        b
        (fma (fma b 0.5 1.0) (/ 1.0 t_0) (/ (* b -0.5) (pow t_0 2.0)))
        (log1p (exp a)))))
    assert(a < b);
    double code(double a, double b) {
    	double t_0 = 1.0 + exp(a);
    	return fma(b, fma(fma(b, 0.5, 1.0), (1.0 / t_0), ((b * -0.5) / pow(t_0, 2.0))), log1p(exp(a)));
    }
    
    a, b = sort([a, b])
    function code(a, b)
    	t_0 = Float64(1.0 + exp(a))
    	return fma(b, fma(fma(b, 0.5, 1.0), Float64(1.0 / t_0), Float64(Float64(b * -0.5) / (t_0 ^ 2.0))), log1p(exp(a)))
    end
    
    NOTE: a and b should be sorted in increasing order before calling this function.
    code[a_, b_] := Block[{t$95$0 = N[(1.0 + N[Exp[a], $MachinePrecision]), $MachinePrecision]}, N[(b * N[(N[(b * 0.5 + 1.0), $MachinePrecision] * N[(1.0 / t$95$0), $MachinePrecision] + N[(N[(b * -0.5), $MachinePrecision] / N[Power[t$95$0, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[Log[1 + N[Exp[a], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
    
    \begin{array}{l}
    [a, b] = \mathsf{sort}([a, b])\\
    \\
    \begin{array}{l}
    t_0 := 1 + e^{a}\\
    \mathsf{fma}\left(b, \mathsf{fma}\left(\mathsf{fma}\left(b, 0.5, 1\right), \frac{1}{t\_0}, \frac{b \cdot -0.5}{{t\_0}^{2}}\right), \mathsf{log1p}\left(e^{a}\right)\right)
    \end{array}
    \end{array}
    
    Derivation
    1. Initial program 53.7%

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

      \[\leadsto \color{blue}{\log \left(1 + e^{a}\right) + b \cdot \left(\frac{1}{2} \cdot \left(b \cdot \left(\frac{1}{1 + e^{a}} - \frac{1}{{\left(1 + e^{a}\right)}^{2}}\right)\right) + \frac{1}{1 + e^{a}}\right)} \]
    4. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \color{blue}{b \cdot \left(\frac{1}{2} \cdot \left(b \cdot \left(\frac{1}{1 + e^{a}} - \frac{1}{{\left(1 + e^{a}\right)}^{2}}\right)\right) + \frac{1}{1 + e^{a}}\right) + \log \left(1 + e^{a}\right)} \]
      2. associate-*r*N/A

        \[\leadsto b \cdot \left(\color{blue}{\left(\frac{1}{2} \cdot b\right) \cdot \left(\frac{1}{1 + e^{a}} - \frac{1}{{\left(1 + e^{a}\right)}^{2}}\right)} + \frac{1}{1 + e^{a}}\right) + \log \left(1 + e^{a}\right) \]
      3. *-commutativeN/A

        \[\leadsto b \cdot \left(\color{blue}{\left(b \cdot \frac{1}{2}\right)} \cdot \left(\frac{1}{1 + e^{a}} - \frac{1}{{\left(1 + e^{a}\right)}^{2}}\right) + \frac{1}{1 + e^{a}}\right) + \log \left(1 + e^{a}\right) \]
      4. associate-*r*N/A

        \[\leadsto b \cdot \left(\color{blue}{b \cdot \left(\frac{1}{2} \cdot \left(\frac{1}{1 + e^{a}} - \frac{1}{{\left(1 + e^{a}\right)}^{2}}\right)\right)} + \frac{1}{1 + e^{a}}\right) + \log \left(1 + e^{a}\right) \]
      5. lower-fma.f64N/A

        \[\leadsto \color{blue}{\mathsf{fma}\left(b, b \cdot \left(\frac{1}{2} \cdot \left(\frac{1}{1 + e^{a}} - \frac{1}{{\left(1 + e^{a}\right)}^{2}}\right)\right) + \frac{1}{1 + e^{a}}, \log \left(1 + e^{a}\right)\right)} \]
    5. Applied rewrites98.6%

      \[\leadsto \color{blue}{\mathsf{fma}\left(b, \mathsf{fma}\left(\mathsf{fma}\left(b, 0.5, 1\right), \frac{1}{1 + e^{a}}, \frac{b \cdot -0.5}{{\left(1 + e^{a}\right)}^{2}}\right), \mathsf{log1p}\left(e^{a}\right)\right)} \]
    6. Add Preprocessing

    Reproduce

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