Jmat.Real.lambertw, newton loop step

Percentage Accurate: 77.9% → 98.2%
Time: 12.6s
Alternatives: 9
Speedup: 27.6×

Specification

?
\[\begin{array}{l} \\ \begin{array}{l} t_0 := wj \cdot e^{wj}\\ wj - \frac{t\_0 - x}{e^{wj} + t\_0} \end{array} \end{array} \]
(FPCore (wj x)
 :precision binary64
 (let* ((t_0 (* wj (exp wj)))) (- wj (/ (- t_0 x) (+ (exp wj) t_0)))))
double code(double wj, double x) {
	double t_0 = wj * exp(wj);
	return wj - ((t_0 - x) / (exp(wj) + t_0));
}
real(8) function code(wj, x)
    real(8), intent (in) :: wj
    real(8), intent (in) :: x
    real(8) :: t_0
    t_0 = wj * exp(wj)
    code = wj - ((t_0 - x) / (exp(wj) + t_0))
end function
public static double code(double wj, double x) {
	double t_0 = wj * Math.exp(wj);
	return wj - ((t_0 - x) / (Math.exp(wj) + t_0));
}
def code(wj, x):
	t_0 = wj * math.exp(wj)
	return wj - ((t_0 - x) / (math.exp(wj) + t_0))
function code(wj, x)
	t_0 = Float64(wj * exp(wj))
	return Float64(wj - Float64(Float64(t_0 - x) / Float64(exp(wj) + t_0)))
end
function tmp = code(wj, x)
	t_0 = wj * exp(wj);
	tmp = wj - ((t_0 - x) / (exp(wj) + t_0));
end
code[wj_, x_] := Block[{t$95$0 = N[(wj * N[Exp[wj], $MachinePrecision]), $MachinePrecision]}, N[(wj - N[(N[(t$95$0 - x), $MachinePrecision] / N[(N[Exp[wj], $MachinePrecision] + t$95$0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := wj \cdot e^{wj}\\
wj - \frac{t\_0 - x}{e^{wj} + t\_0}
\end{array}
\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 9 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: 77.9% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := wj \cdot e^{wj}\\ wj - \frac{t\_0 - x}{e^{wj} + t\_0} \end{array} \end{array} \]
(FPCore (wj x)
 :precision binary64
 (let* ((t_0 (* wj (exp wj)))) (- wj (/ (- t_0 x) (+ (exp wj) t_0)))))
double code(double wj, double x) {
	double t_0 = wj * exp(wj);
	return wj - ((t_0 - x) / (exp(wj) + t_0));
}
real(8) function code(wj, x)
    real(8), intent (in) :: wj
    real(8), intent (in) :: x
    real(8) :: t_0
    t_0 = wj * exp(wj)
    code = wj - ((t_0 - x) / (exp(wj) + t_0))
end function
public static double code(double wj, double x) {
	double t_0 = wj * Math.exp(wj);
	return wj - ((t_0 - x) / (Math.exp(wj) + t_0));
}
def code(wj, x):
	t_0 = wj * math.exp(wj)
	return wj - ((t_0 - x) / (math.exp(wj) + t_0))
function code(wj, x)
	t_0 = Float64(wj * exp(wj))
	return Float64(wj - Float64(Float64(t_0 - x) / Float64(exp(wj) + t_0)))
end
function tmp = code(wj, x)
	t_0 = wj * exp(wj);
	tmp = wj - ((t_0 - x) / (exp(wj) + t_0));
end
code[wj_, x_] := Block[{t$95$0 = N[(wj * N[Exp[wj], $MachinePrecision]), $MachinePrecision]}, N[(wj - N[(N[(t$95$0 - x), $MachinePrecision] / N[(N[Exp[wj], $MachinePrecision] + t$95$0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := wj \cdot e^{wj}\\
wj - \frac{t\_0 - x}{e^{wj} + t\_0}
\end{array}
\end{array}

Alternative 1: 98.2% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := wj \cdot e^{wj}\\ \mathbf{if}\;wj + \frac{x - t\_0}{t\_0 + e^{wj}} \leq 5 \cdot 10^{-24}:\\ \;\;\;\;\mathsf{fma}\left(wj, \mathsf{fma}\left(wj, \mathsf{fma}\left(x, 2.5, 1\right) - \mathsf{fma}\left(wj, \mathsf{fma}\left(x, 0.6666666666666666, x \cdot 2\right), wj\right), x \cdot -2\right), x\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(\left(\frac{wj}{x} - \frac{e^{-wj}}{-1 - wj}\right) - \frac{wj}{\mathsf{fma}\left(wj, x, x\right)}\right)\\ \end{array} \end{array} \]
(FPCore (wj x)
 :precision binary64
 (let* ((t_0 (* wj (exp wj))))
   (if (<= (+ wj (/ (- x t_0) (+ t_0 (exp wj)))) 5e-24)
     (fma
      wj
      (fma
       wj
       (- (fma x 2.5 1.0) (fma wj (fma x 0.6666666666666666 (* x 2.0)) wj))
       (* x -2.0))
      x)
     (* x (- (- (/ wj x) (/ (exp (- wj)) (- -1.0 wj))) (/ wj (fma wj x x)))))))
double code(double wj, double x) {
	double t_0 = wj * exp(wj);
	double tmp;
	if ((wj + ((x - t_0) / (t_0 + exp(wj)))) <= 5e-24) {
		tmp = fma(wj, fma(wj, (fma(x, 2.5, 1.0) - fma(wj, fma(x, 0.6666666666666666, (x * 2.0)), wj)), (x * -2.0)), x);
	} else {
		tmp = x * (((wj / x) - (exp(-wj) / (-1.0 - wj))) - (wj / fma(wj, x, x)));
	}
	return tmp;
}
function code(wj, x)
	t_0 = Float64(wj * exp(wj))
	tmp = 0.0
	if (Float64(wj + Float64(Float64(x - t_0) / Float64(t_0 + exp(wj)))) <= 5e-24)
		tmp = fma(wj, fma(wj, Float64(fma(x, 2.5, 1.0) - fma(wj, fma(x, 0.6666666666666666, Float64(x * 2.0)), wj)), Float64(x * -2.0)), x);
	else
		tmp = Float64(x * Float64(Float64(Float64(wj / x) - Float64(exp(Float64(-wj)) / Float64(-1.0 - wj))) - Float64(wj / fma(wj, x, x))));
	end
	return tmp
end
code[wj_, x_] := Block[{t$95$0 = N[(wj * N[Exp[wj], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[(wj + N[(N[(x - t$95$0), $MachinePrecision] / N[(t$95$0 + N[Exp[wj], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 5e-24], N[(wj * N[(wj * N[(N[(x * 2.5 + 1.0), $MachinePrecision] - N[(wj * N[(x * 0.6666666666666666 + N[(x * 2.0), $MachinePrecision]), $MachinePrecision] + wj), $MachinePrecision]), $MachinePrecision] + N[(x * -2.0), $MachinePrecision]), $MachinePrecision] + x), $MachinePrecision], N[(x * N[(N[(N[(wj / x), $MachinePrecision] - N[(N[Exp[(-wj)], $MachinePrecision] / N[(-1.0 - wj), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - N[(wj / N[(wj * x + x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := wj \cdot e^{wj}\\
\mathbf{if}\;wj + \frac{x - t\_0}{t\_0 + e^{wj}} \leq 5 \cdot 10^{-24}:\\
\;\;\;\;\mathsf{fma}\left(wj, \mathsf{fma}\left(wj, \mathsf{fma}\left(x, 2.5, 1\right) - \mathsf{fma}\left(wj, \mathsf{fma}\left(x, 0.6666666666666666, x \cdot 2\right), wj\right), x \cdot -2\right), x\right)\\

\mathbf{else}:\\
\;\;\;\;x \cdot \left(\left(\frac{wj}{x} - \frac{e^{-wj}}{-1 - wj}\right) - \frac{wj}{\mathsf{fma}\left(wj, x, x\right)}\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (-.f64 wj (/.f64 (-.f64 (*.f64 wj (exp.f64 wj)) x) (+.f64 (exp.f64 wj) (*.f64 wj (exp.f64 wj))))) < 4.9999999999999998e-24

    1. Initial program 66.0%

      \[wj - \frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}} \]
    2. Add Preprocessing
    3. Taylor expanded in wj around 0

      \[\leadsto \color{blue}{x + wj \cdot \left(wj \cdot \left(\left(1 + -1 \cdot \left(wj \cdot \left(1 + \left(-3 \cdot x + \left(-2 \cdot \left(-4 \cdot x + \frac{3}{2} \cdot x\right) + \frac{2}{3} \cdot x\right)\right)\right)\right)\right) - \left(-4 \cdot x + \frac{3}{2} \cdot x\right)\right) - 2 \cdot x\right)} \]
    4. Applied rewrites98.9%

      \[\leadsto \color{blue}{\mathsf{fma}\left(wj, \mathsf{fma}\left(wj, \mathsf{fma}\left(x, 2.5, 1\right) - \mathsf{fma}\left(wj, \mathsf{fma}\left(x, 0.6666666666666666, x \cdot 2\right), wj\right), x \cdot -2\right), x\right)} \]

    if 4.9999999999999998e-24 < (-.f64 wj (/.f64 (-.f64 (*.f64 wj (exp.f64 wj)) x) (+.f64 (exp.f64 wj) (*.f64 wj (exp.f64 wj)))))

    1. Initial program 97.1%

      \[wj - \frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}} \]
    2. Add Preprocessing
    3. Applied rewrites39.1%

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{e^{wj \cdot 3} \cdot \left(wj \cdot \left(wj \cdot wj\right)\right) - x \cdot \left(x \cdot x\right)}{wj + 1}, \frac{\frac{1}{\mathsf{fma}\left(wj, wj \cdot e^{wj + wj}, x \cdot \mathsf{fma}\left(wj, e^{wj}, x\right)\right)}}{-e^{wj}}, wj\right)} \]
    4. Taylor expanded in x around inf

      \[\leadsto \color{blue}{x \cdot \left(-1 \cdot \frac{wj}{x \cdot \left(1 + wj\right)} + \left(\frac{1}{e^{wj} \cdot \left(1 + wj\right)} + \frac{wj}{x}\right)\right)} \]
    5. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \color{blue}{x \cdot \left(-1 \cdot \frac{wj}{x \cdot \left(1 + wj\right)} + \left(\frac{1}{e^{wj} \cdot \left(1 + wj\right)} + \frac{wj}{x}\right)\right)} \]
      2. +-commutativeN/A

        \[\leadsto x \cdot \color{blue}{\left(\left(\frac{1}{e^{wj} \cdot \left(1 + wj\right)} + \frac{wj}{x}\right) + -1 \cdot \frac{wj}{x \cdot \left(1 + wj\right)}\right)} \]
      3. mul-1-negN/A

        \[\leadsto x \cdot \left(\left(\frac{1}{e^{wj} \cdot \left(1 + wj\right)} + \frac{wj}{x}\right) + \color{blue}{\left(\mathsf{neg}\left(\frac{wj}{x \cdot \left(1 + wj\right)}\right)\right)}\right) \]
      4. unsub-negN/A

        \[\leadsto x \cdot \color{blue}{\left(\left(\frac{1}{e^{wj} \cdot \left(1 + wj\right)} + \frac{wj}{x}\right) - \frac{wj}{x \cdot \left(1 + wj\right)}\right)} \]
      5. lower--.f64N/A

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

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

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

        \[\leadsto x \cdot \left(\left(\color{blue}{\frac{wj}{x}} + \frac{1}{e^{wj} \cdot \left(1 + wj\right)}\right) - \frac{wj}{x \cdot \left(1 + wj\right)}\right) \]
      9. associate-/r*N/A

        \[\leadsto x \cdot \left(\left(\frac{wj}{x} + \color{blue}{\frac{\frac{1}{e^{wj}}}{1 + wj}}\right) - \frac{wj}{x \cdot \left(1 + wj\right)}\right) \]
      10. lower-/.f64N/A

        \[\leadsto x \cdot \left(\left(\frac{wj}{x} + \color{blue}{\frac{\frac{1}{e^{wj}}}{1 + wj}}\right) - \frac{wj}{x \cdot \left(1 + wj\right)}\right) \]
      11. rec-expN/A

        \[\leadsto x \cdot \left(\left(\frac{wj}{x} + \frac{\color{blue}{e^{\mathsf{neg}\left(wj\right)}}}{1 + wj}\right) - \frac{wj}{x \cdot \left(1 + wj\right)}\right) \]
      12. lower-exp.f64N/A

        \[\leadsto x \cdot \left(\left(\frac{wj}{x} + \frac{\color{blue}{e^{\mathsf{neg}\left(wj\right)}}}{1 + wj}\right) - \frac{wj}{x \cdot \left(1 + wj\right)}\right) \]
      13. lower-neg.f64N/A

        \[\leadsto x \cdot \left(\left(\frac{wj}{x} + \frac{e^{\color{blue}{\mathsf{neg}\left(wj\right)}}}{1 + wj}\right) - \frac{wj}{x \cdot \left(1 + wj\right)}\right) \]
      14. lower-+.f64N/A

        \[\leadsto x \cdot \left(\left(\frac{wj}{x} + \frac{e^{\mathsf{neg}\left(wj\right)}}{\color{blue}{1 + wj}}\right) - \frac{wj}{x \cdot \left(1 + wj\right)}\right) \]
      15. lower-/.f64N/A

        \[\leadsto x \cdot \left(\left(\frac{wj}{x} + \frac{e^{\mathsf{neg}\left(wj\right)}}{1 + wj}\right) - \color{blue}{\frac{wj}{x \cdot \left(1 + wj\right)}}\right) \]
      16. +-commutativeN/A

        \[\leadsto x \cdot \left(\left(\frac{wj}{x} + \frac{e^{\mathsf{neg}\left(wj\right)}}{1 + wj}\right) - \frac{wj}{x \cdot \color{blue}{\left(wj + 1\right)}}\right) \]
      17. distribute-lft-inN/A

        \[\leadsto x \cdot \left(\left(\frac{wj}{x} + \frac{e^{\mathsf{neg}\left(wj\right)}}{1 + wj}\right) - \frac{wj}{\color{blue}{x \cdot wj + x \cdot 1}}\right) \]
      18. *-commutativeN/A

        \[\leadsto x \cdot \left(\left(\frac{wj}{x} + \frac{e^{\mathsf{neg}\left(wj\right)}}{1 + wj}\right) - \frac{wj}{\color{blue}{wj \cdot x} + x \cdot 1}\right) \]
      19. *-rgt-identityN/A

        \[\leadsto x \cdot \left(\left(\frac{wj}{x} + \frac{e^{\mathsf{neg}\left(wj\right)}}{1 + wj}\right) - \frac{wj}{wj \cdot x + \color{blue}{x}}\right) \]
    6. Applied rewrites99.4%

      \[\leadsto \color{blue}{x \cdot \left(\left(\frac{wj}{x} + \frac{e^{-wj}}{1 + wj}\right) - \frac{wj}{\mathsf{fma}\left(wj, x, x\right)}\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification99.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;wj + \frac{x - wj \cdot e^{wj}}{wj \cdot e^{wj} + e^{wj}} \leq 5 \cdot 10^{-24}:\\ \;\;\;\;\mathsf{fma}\left(wj, \mathsf{fma}\left(wj, \mathsf{fma}\left(x, 2.5, 1\right) - \mathsf{fma}\left(wj, \mathsf{fma}\left(x, 0.6666666666666666, x \cdot 2\right), wj\right), x \cdot -2\right), x\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(\left(\frac{wj}{x} - \frac{e^{-wj}}{-1 - wj}\right) - \frac{wj}{\mathsf{fma}\left(wj, x, x\right)}\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 2: 96.2% accurate, 7.5× speedup?

\[\begin{array}{l} \\ \mathsf{fma}\left(wj, \mathsf{fma}\left(wj, \mathsf{fma}\left(x, 2.5, 1\right) - \mathsf{fma}\left(wj, \mathsf{fma}\left(x, 0.6666666666666666, x \cdot 2\right), wj\right), x \cdot -2\right), x\right) \end{array} \]
(FPCore (wj x)
 :precision binary64
 (fma
  wj
  (fma
   wj
   (- (fma x 2.5 1.0) (fma wj (fma x 0.6666666666666666 (* x 2.0)) wj))
   (* x -2.0))
  x))
double code(double wj, double x) {
	return fma(wj, fma(wj, (fma(x, 2.5, 1.0) - fma(wj, fma(x, 0.6666666666666666, (x * 2.0)), wj)), (x * -2.0)), x);
}
function code(wj, x)
	return fma(wj, fma(wj, Float64(fma(x, 2.5, 1.0) - fma(wj, fma(x, 0.6666666666666666, Float64(x * 2.0)), wj)), Float64(x * -2.0)), x)
end
code[wj_, x_] := N[(wj * N[(wj * N[(N[(x * 2.5 + 1.0), $MachinePrecision] - N[(wj * N[(x * 0.6666666666666666 + N[(x * 2.0), $MachinePrecision]), $MachinePrecision] + wj), $MachinePrecision]), $MachinePrecision] + N[(x * -2.0), $MachinePrecision]), $MachinePrecision] + x), $MachinePrecision]
\begin{array}{l}

\\
\mathsf{fma}\left(wj, \mathsf{fma}\left(wj, \mathsf{fma}\left(x, 2.5, 1\right) - \mathsf{fma}\left(wj, \mathsf{fma}\left(x, 0.6666666666666666, x \cdot 2\right), wj\right), x \cdot -2\right), x\right)
\end{array}
Derivation
  1. Initial program 77.9%

    \[wj - \frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}} \]
  2. Add Preprocessing
  3. Taylor expanded in wj around 0

    \[\leadsto \color{blue}{x + wj \cdot \left(wj \cdot \left(\left(1 + -1 \cdot \left(wj \cdot \left(1 + \left(-3 \cdot x + \left(-2 \cdot \left(-4 \cdot x + \frac{3}{2} \cdot x\right) + \frac{2}{3} \cdot x\right)\right)\right)\right)\right) - \left(-4 \cdot x + \frac{3}{2} \cdot x\right)\right) - 2 \cdot x\right)} \]
  4. Applied rewrites96.2%

    \[\leadsto \color{blue}{\mathsf{fma}\left(wj, \mathsf{fma}\left(wj, \mathsf{fma}\left(x, 2.5, 1\right) - \mathsf{fma}\left(wj, \mathsf{fma}\left(x, 0.6666666666666666, x \cdot 2\right), wj\right), x \cdot -2\right), x\right)} \]
  5. Add Preprocessing

Developer Target 1: 78.9% accurate, 1.4× speedup?

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

\\
wj - \left(\frac{wj}{wj + 1} - \frac{x}{e^{wj} + wj \cdot e^{wj}}\right)
\end{array}

Reproduce

?
herbie shell --seed 2024219 
(FPCore (wj x)
  :name "Jmat.Real.lambertw, newton loop step"
  :precision binary64

  :alt
  (! :herbie-platform default (let ((ew (exp wj))) (- wj (- (/ wj (+ wj 1)) (/ x (+ ew (* wj ew)))))))

  (- wj (/ (- (* wj (exp wj)) x) (+ (exp wj) (* wj (exp wj))))))