Quotient of sum of exps

Percentage Accurate: 99.0% → 99.1%
Time: 8.7s
Alternatives: 17
Speedup: 1.0×

Specification

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

\\
\frac{e^{a}}{e^{a} + e^{b}}
\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 17 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: 99.0% accurate, 1.0× speedup?

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

\\
\frac{e^{a}}{e^{a} + e^{b}}
\end{array}

Alternative 1: 99.1% accurate, 0.8× speedup?

\[\begin{array}{l} \\ e^{\mathsf{fma}\left(\log \left(e^{a} + e^{b}\right), -1, a\right)} \end{array} \]
(FPCore (a b) :precision binary64 (exp (fma (log (+ (exp a) (exp b))) -1.0 a)))
double code(double a, double b) {
	return exp(fma(log((exp(a) + exp(b))), -1.0, a));
}
function code(a, b)
	return exp(fma(log(Float64(exp(a) + exp(b))), -1.0, a))
end
code[a_, b_] := N[Exp[N[(N[Log[N[(N[Exp[a], $MachinePrecision] + N[Exp[b], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * -1.0 + a), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
e^{\mathsf{fma}\left(\log \left(e^{a} + e^{b}\right), -1, a\right)}
\end{array}
Derivation
  1. Initial program 99.2%

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

      \[\leadsto \color{blue}{\frac{e^{a}}{e^{a} + e^{b}}} \]
    2. clear-numN/A

      \[\leadsto \color{blue}{\frac{1}{\frac{e^{a} + e^{b}}{e^{a}}}} \]
    3. associate-/r/N/A

      \[\leadsto \color{blue}{\frac{1}{e^{a} + e^{b}} \cdot e^{a}} \]
    4. inv-powN/A

      \[\leadsto \color{blue}{{\left(e^{a} + e^{b}\right)}^{-1}} \cdot e^{a} \]
    5. pow-to-expN/A

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

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

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

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

      \[\leadsto e^{\color{blue}{\mathsf{fma}\left(\log \left(e^{a} + e^{b}\right), -1, a\right)}} \]
    10. lower-log.f6499.2

      \[\leadsto e^{\mathsf{fma}\left(\color{blue}{\log \left(e^{a} + e^{b}\right)}, -1, a\right)} \]
  4. Applied rewrites99.2%

    \[\leadsto \color{blue}{e^{\mathsf{fma}\left(\log \left(e^{a} + e^{b}\right), -1, a\right)}} \]
  5. Add Preprocessing

Alternative 2: 67.8% accurate, 0.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\frac{e^{a}}{e^{a} + e^{b}} \leq 0:\\ \;\;\;\;\frac{1}{b \cdot \mathsf{fma}\left(b, 0.5, \mathsf{fma}\left(b, b \cdot \left(0.16666666666666666 + \frac{2}{b \cdot \left(b \cdot b\right)}\right), 1\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5\\ \end{array} \end{array} \]
(FPCore (a b)
 :precision binary64
 (if (<= (/ (exp a) (+ (exp a) (exp b))) 0.0)
   (/
    1.0
    (*
     b
     (fma
      b
      0.5
      (fma b (* b (+ 0.16666666666666666 (/ 2.0 (* b (* b b))))) 1.0))))
   0.5))
double code(double a, double b) {
	double tmp;
	if ((exp(a) / (exp(a) + exp(b))) <= 0.0) {
		tmp = 1.0 / (b * fma(b, 0.5, fma(b, (b * (0.16666666666666666 + (2.0 / (b * (b * b))))), 1.0)));
	} else {
		tmp = 0.5;
	}
	return tmp;
}
function code(a, b)
	tmp = 0.0
	if (Float64(exp(a) / Float64(exp(a) + exp(b))) <= 0.0)
		tmp = Float64(1.0 / Float64(b * fma(b, 0.5, fma(b, Float64(b * Float64(0.16666666666666666 + Float64(2.0 / Float64(b * Float64(b * b))))), 1.0))));
	else
		tmp = 0.5;
	end
	return tmp
end
code[a_, b_] := If[LessEqual[N[(N[Exp[a], $MachinePrecision] / N[(N[Exp[a], $MachinePrecision] + N[Exp[b], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.0], N[(1.0 / N[(b * N[(b * 0.5 + N[(b * N[(b * N[(0.16666666666666666 + N[(2.0 / N[(b * N[(b * b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.5]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;\frac{e^{a}}{e^{a} + e^{b}} \leq 0:\\
\;\;\;\;\frac{1}{b \cdot \mathsf{fma}\left(b, 0.5, \mathsf{fma}\left(b, b \cdot \left(0.16666666666666666 + \frac{2}{b \cdot \left(b \cdot b\right)}\right), 1\right)\right)}\\

\mathbf{else}:\\
\;\;\;\;0.5\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (/.f64 (exp.f64 a) (+.f64 (exp.f64 a) (exp.f64 b))) < 0.0

    1. Initial program 100.0%

      \[\frac{e^{a}}{e^{a} + e^{b}} \]
    2. Add Preprocessing
    3. Taylor expanded in a around 0

      \[\leadsto \color{blue}{\frac{1}{1 + e^{b}}} \]
    4. Step-by-step derivation
      1. lower-/.f64N/A

        \[\leadsto \color{blue}{\frac{1}{1 + e^{b}}} \]
      2. lower-+.f64N/A

        \[\leadsto \frac{1}{\color{blue}{1 + e^{b}}} \]
      3. lower-exp.f6461.5

        \[\leadsto \frac{1}{1 + \color{blue}{e^{b}}} \]
    5. Applied rewrites61.5%

      \[\leadsto \color{blue}{\frac{1}{1 + e^{b}}} \]
    6. Taylor expanded in b around 0

      \[\leadsto \frac{1}{2 + \color{blue}{b \cdot \left(1 + b \cdot \left(\frac{1}{2} + \frac{1}{6} \cdot b\right)\right)}} \]
    7. Step-by-step derivation
      1. Applied rewrites42.4%

        \[\leadsto \frac{1}{\mathsf{fma}\left(b, \color{blue}{\mathsf{fma}\left(b, \mathsf{fma}\left(b, 0.16666666666666666, 0.5\right), 1\right)}, 2\right)} \]
      2. Taylor expanded in b around inf

        \[\leadsto \frac{1}{{b}^{3} \cdot \left(\frac{1}{6} + \color{blue}{\left(\frac{1}{2} \cdot \frac{1}{b} + \left(\frac{1}{{b}^{2}} + 2 \cdot \frac{1}{{b}^{3}}\right)\right)}\right)} \]
      3. Step-by-step derivation
        1. Applied rewrites48.5%

          \[\leadsto \frac{1}{b \cdot \left(\mathsf{fma}\left(b, 0.5, 1\right) + \color{blue}{\left(\frac{2}{b \cdot \left(b \cdot b\right)} + 0.16666666666666666\right) \cdot \left(b \cdot b\right)}\right)} \]
        2. Step-by-step derivation
          1. Applied rewrites67.3%

            \[\leadsto \frac{1}{\mathsf{fma}\left(b, 0.5, \mathsf{fma}\left(b, b \cdot \left(0.16666666666666666 + \frac{2}{b \cdot \left(b \cdot b\right)}\right), 1\right)\right) \cdot b} \]

          if 0.0 < (/.f64 (exp.f64 a) (+.f64 (exp.f64 a) (exp.f64 b)))

          1. Initial program 98.2%

            \[\frac{e^{a}}{e^{a} + e^{b}} \]
          2. Add Preprocessing
          3. Taylor expanded in a around 0

            \[\leadsto \color{blue}{\frac{1}{1 + e^{b}}} \]
          4. Step-by-step derivation
            1. lower-/.f64N/A

              \[\leadsto \color{blue}{\frac{1}{1 + e^{b}}} \]
            2. lower-+.f64N/A

              \[\leadsto \frac{1}{\color{blue}{1 + e^{b}}} \]
            3. lower-exp.f6497.4

              \[\leadsto \frac{1}{1 + \color{blue}{e^{b}}} \]
          5. Applied rewrites97.4%

            \[\leadsto \color{blue}{\frac{1}{1 + e^{b}}} \]
          6. Taylor expanded in b around 0

            \[\leadsto \frac{1}{2} \]
          7. Step-by-step derivation
            1. Applied rewrites68.2%

              \[\leadsto 0.5 \]
          8. Recombined 2 regimes into one program.
          9. Final simplification67.8%

            \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{e^{a}}{e^{a} + e^{b}} \leq 0:\\ \;\;\;\;\frac{1}{b \cdot \mathsf{fma}\left(b, 0.5, \mathsf{fma}\left(b, b \cdot \left(0.16666666666666666 + \frac{2}{b \cdot \left(b \cdot b\right)}\right), 1\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5\\ \end{array} \]
          10. Add Preprocessing

          Developer Target 1: 100.0% accurate, 2.7× speedup?

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

          Reproduce

          ?
          herbie shell --seed 2024222 
          (FPCore (a b)
            :name "Quotient of sum of exps"
            :precision binary64
          
            :alt
            (! :herbie-platform default (/ 1 (+ 1 (exp (- b a)))))
          
            (/ (exp a) (+ (exp a) (exp b))))