Toniolo and Linder, Equation (3a)

Percentage Accurate: 98.3% → 98.6%
Time: 10.8s
Alternatives: 3
Speedup: 1.0×

Specification

?
\[\begin{array}{l} \\ \sqrt{\frac{1}{2} \cdot \left(1 + \frac{1}{\sqrt{1 + {\left(\frac{2 \cdot \ell}{Om}\right)}^{2} \cdot \left({\sin kx}^{2} + {\sin ky}^{2}\right)}}\right)} \end{array} \]
(FPCore (l Om kx ky)
 :precision binary64
 (sqrt
  (*
   (/ 1.0 2.0)
   (+
    1.0
    (/
     1.0
     (sqrt
      (+
       1.0
       (*
        (pow (/ (* 2.0 l) Om) 2.0)
        (+ (pow (sin kx) 2.0) (pow (sin ky) 2.0))))))))))
double code(double l, double Om, double kx, double ky) {
	return sqrt(((1.0 / 2.0) * (1.0 + (1.0 / sqrt((1.0 + (pow(((2.0 * l) / Om), 2.0) * (pow(sin(kx), 2.0) + pow(sin(ky), 2.0)))))))));
}
real(8) function code(l, om, kx, ky)
    real(8), intent (in) :: l
    real(8), intent (in) :: om
    real(8), intent (in) :: kx
    real(8), intent (in) :: ky
    code = sqrt(((1.0d0 / 2.0d0) * (1.0d0 + (1.0d0 / sqrt((1.0d0 + ((((2.0d0 * l) / om) ** 2.0d0) * ((sin(kx) ** 2.0d0) + (sin(ky) ** 2.0d0)))))))))
end function
public static double code(double l, double Om, double kx, double ky) {
	return Math.sqrt(((1.0 / 2.0) * (1.0 + (1.0 / Math.sqrt((1.0 + (Math.pow(((2.0 * l) / Om), 2.0) * (Math.pow(Math.sin(kx), 2.0) + Math.pow(Math.sin(ky), 2.0)))))))));
}
def code(l, Om, kx, ky):
	return math.sqrt(((1.0 / 2.0) * (1.0 + (1.0 / math.sqrt((1.0 + (math.pow(((2.0 * l) / Om), 2.0) * (math.pow(math.sin(kx), 2.0) + math.pow(math.sin(ky), 2.0)))))))))
function code(l, Om, kx, ky)
	return sqrt(Float64(Float64(1.0 / 2.0) * Float64(1.0 + Float64(1.0 / sqrt(Float64(1.0 + Float64((Float64(Float64(2.0 * l) / Om) ^ 2.0) * Float64((sin(kx) ^ 2.0) + (sin(ky) ^ 2.0)))))))))
end
function tmp = code(l, Om, kx, ky)
	tmp = sqrt(((1.0 / 2.0) * (1.0 + (1.0 / sqrt((1.0 + ((((2.0 * l) / Om) ^ 2.0) * ((sin(kx) ^ 2.0) + (sin(ky) ^ 2.0)))))))));
end
code[l_, Om_, kx_, ky_] := N[Sqrt[N[(N[(1.0 / 2.0), $MachinePrecision] * N[(1.0 + N[(1.0 / N[Sqrt[N[(1.0 + N[(N[Power[N[(N[(2.0 * l), $MachinePrecision] / Om), $MachinePrecision], 2.0], $MachinePrecision] * N[(N[Power[N[Sin[kx], $MachinePrecision], 2.0], $MachinePrecision] + N[Power[N[Sin[ky], $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
\sqrt{\frac{1}{2} \cdot \left(1 + \frac{1}{\sqrt{1 + {\left(\frac{2 \cdot \ell}{Om}\right)}^{2} \cdot \left({\sin kx}^{2} + {\sin ky}^{2}\right)}}\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 3 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: 98.3% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \sqrt{\frac{1}{2} \cdot \left(1 + \frac{1}{\sqrt{1 + {\left(\frac{2 \cdot \ell}{Om}\right)}^{2} \cdot \left({\sin kx}^{2} + {\sin ky}^{2}\right)}}\right)} \end{array} \]
(FPCore (l Om kx ky)
 :precision binary64
 (sqrt
  (*
   (/ 1.0 2.0)
   (+
    1.0
    (/
     1.0
     (sqrt
      (+
       1.0
       (*
        (pow (/ (* 2.0 l) Om) 2.0)
        (+ (pow (sin kx) 2.0) (pow (sin ky) 2.0))))))))))
double code(double l, double Om, double kx, double ky) {
	return sqrt(((1.0 / 2.0) * (1.0 + (1.0 / sqrt((1.0 + (pow(((2.0 * l) / Om), 2.0) * (pow(sin(kx), 2.0) + pow(sin(ky), 2.0)))))))));
}
real(8) function code(l, om, kx, ky)
    real(8), intent (in) :: l
    real(8), intent (in) :: om
    real(8), intent (in) :: kx
    real(8), intent (in) :: ky
    code = sqrt(((1.0d0 / 2.0d0) * (1.0d0 + (1.0d0 / sqrt((1.0d0 + ((((2.0d0 * l) / om) ** 2.0d0) * ((sin(kx) ** 2.0d0) + (sin(ky) ** 2.0d0)))))))))
end function
public static double code(double l, double Om, double kx, double ky) {
	return Math.sqrt(((1.0 / 2.0) * (1.0 + (1.0 / Math.sqrt((1.0 + (Math.pow(((2.0 * l) / Om), 2.0) * (Math.pow(Math.sin(kx), 2.0) + Math.pow(Math.sin(ky), 2.0)))))))));
}
def code(l, Om, kx, ky):
	return math.sqrt(((1.0 / 2.0) * (1.0 + (1.0 / math.sqrt((1.0 + (math.pow(((2.0 * l) / Om), 2.0) * (math.pow(math.sin(kx), 2.0) + math.pow(math.sin(ky), 2.0)))))))))
function code(l, Om, kx, ky)
	return sqrt(Float64(Float64(1.0 / 2.0) * Float64(1.0 + Float64(1.0 / sqrt(Float64(1.0 + Float64((Float64(Float64(2.0 * l) / Om) ^ 2.0) * Float64((sin(kx) ^ 2.0) + (sin(ky) ^ 2.0)))))))))
end
function tmp = code(l, Om, kx, ky)
	tmp = sqrt(((1.0 / 2.0) * (1.0 + (1.0 / sqrt((1.0 + ((((2.0 * l) / Om) ^ 2.0) * ((sin(kx) ^ 2.0) + (sin(ky) ^ 2.0)))))))));
end
code[l_, Om_, kx_, ky_] := N[Sqrt[N[(N[(1.0 / 2.0), $MachinePrecision] * N[(1.0 + N[(1.0 / N[Sqrt[N[(1.0 + N[(N[Power[N[(N[(2.0 * l), $MachinePrecision] / Om), $MachinePrecision], 2.0], $MachinePrecision] * N[(N[Power[N[Sin[kx], $MachinePrecision], 2.0], $MachinePrecision] + N[Power[N[Sin[ky], $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
\sqrt{\frac{1}{2} \cdot \left(1 + \frac{1}{\sqrt{1 + {\left(\frac{2 \cdot \ell}{Om}\right)}^{2} \cdot \left({\sin kx}^{2} + {\sin ky}^{2}\right)}}\right)}
\end{array}

Alternative 1: 98.6% accurate, 1.0× speedup?

\[\begin{array}{l} l_m = \left|\ell\right| \\ Om_m = \left|Om\right| \\ ky_m = \left|ky\right| \\ [l_m, Om_m, kx, ky_m] = \mathsf{sort}([l_m, Om_m, kx, ky_m])\\ \\ \begin{array}{l} \mathbf{if}\;\frac{2 \cdot l\_m}{Om\_m} \leq 2 \cdot 10^{+151}:\\ \;\;\;\;\sqrt{0.5 + 0.5 \cdot \frac{1}{\sqrt{1 + {\left(2 \cdot \frac{l\_m}{Om\_m}\right)}^{2} \cdot \left({\sin kx}^{2} + {\sin ky\_m}^{2}\right)}}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 + 0.25 \cdot \frac{\frac{Om\_m}{ky\_m}}{l\_m}}\\ \end{array} \end{array} \]
l_m = (fabs.f64 l)
Om_m = (fabs.f64 Om)
ky_m = (fabs.f64 ky)
NOTE: l_m, Om_m, kx, and ky_m should be sorted in increasing order before calling this function.
(FPCore (l_m Om_m kx ky_m)
 :precision binary64
 (if (<= (/ (* 2.0 l_m) Om_m) 2e+151)
   (sqrt
    (+
     0.5
     (*
      0.5
      (/
       1.0
       (sqrt
        (+
         1.0
         (*
          (pow (* 2.0 (/ l_m Om_m)) 2.0)
          (+ (pow (sin kx) 2.0) (pow (sin ky_m) 2.0)))))))))
   (sqrt (+ 0.5 (* 0.25 (/ (/ Om_m ky_m) l_m))))))
l_m = fabs(l);
Om_m = fabs(Om);
ky_m = fabs(ky);
assert(l_m < Om_m && Om_m < kx && kx < ky_m);
double code(double l_m, double Om_m, double kx, double ky_m) {
	double tmp;
	if (((2.0 * l_m) / Om_m) <= 2e+151) {
		tmp = sqrt((0.5 + (0.5 * (1.0 / sqrt((1.0 + (pow((2.0 * (l_m / Om_m)), 2.0) * (pow(sin(kx), 2.0) + pow(sin(ky_m), 2.0)))))))));
	} else {
		tmp = sqrt((0.5 + (0.25 * ((Om_m / ky_m) / l_m))));
	}
	return tmp;
}
l_m = abs(l)
Om_m = abs(om)
ky_m = abs(ky)
NOTE: l_m, Om_m, kx, and ky_m should be sorted in increasing order before calling this function.
real(8) function code(l_m, om_m, kx, ky_m)
    real(8), intent (in) :: l_m
    real(8), intent (in) :: om_m
    real(8), intent (in) :: kx
    real(8), intent (in) :: ky_m
    real(8) :: tmp
    if (((2.0d0 * l_m) / om_m) <= 2d+151) then
        tmp = sqrt((0.5d0 + (0.5d0 * (1.0d0 / sqrt((1.0d0 + (((2.0d0 * (l_m / om_m)) ** 2.0d0) * ((sin(kx) ** 2.0d0) + (sin(ky_m) ** 2.0d0)))))))))
    else
        tmp = sqrt((0.5d0 + (0.25d0 * ((om_m / ky_m) / l_m))))
    end if
    code = tmp
end function
l_m = Math.abs(l);
Om_m = Math.abs(Om);
ky_m = Math.abs(ky);
assert l_m < Om_m && Om_m < kx && kx < ky_m;
public static double code(double l_m, double Om_m, double kx, double ky_m) {
	double tmp;
	if (((2.0 * l_m) / Om_m) <= 2e+151) {
		tmp = Math.sqrt((0.5 + (0.5 * (1.0 / Math.sqrt((1.0 + (Math.pow((2.0 * (l_m / Om_m)), 2.0) * (Math.pow(Math.sin(kx), 2.0) + Math.pow(Math.sin(ky_m), 2.0)))))))));
	} else {
		tmp = Math.sqrt((0.5 + (0.25 * ((Om_m / ky_m) / l_m))));
	}
	return tmp;
}
l_m = math.fabs(l)
Om_m = math.fabs(Om)
ky_m = math.fabs(ky)
[l_m, Om_m, kx, ky_m] = sort([l_m, Om_m, kx, ky_m])
def code(l_m, Om_m, kx, ky_m):
	tmp = 0
	if ((2.0 * l_m) / Om_m) <= 2e+151:
		tmp = math.sqrt((0.5 + (0.5 * (1.0 / math.sqrt((1.0 + (math.pow((2.0 * (l_m / Om_m)), 2.0) * (math.pow(math.sin(kx), 2.0) + math.pow(math.sin(ky_m), 2.0)))))))))
	else:
		tmp = math.sqrt((0.5 + (0.25 * ((Om_m / ky_m) / l_m))))
	return tmp
l_m = abs(l)
Om_m = abs(Om)
ky_m = abs(ky)
l_m, Om_m, kx, ky_m = sort([l_m, Om_m, kx, ky_m])
function code(l_m, Om_m, kx, ky_m)
	tmp = 0.0
	if (Float64(Float64(2.0 * l_m) / Om_m) <= 2e+151)
		tmp = sqrt(Float64(0.5 + Float64(0.5 * Float64(1.0 / sqrt(Float64(1.0 + Float64((Float64(2.0 * Float64(l_m / Om_m)) ^ 2.0) * Float64((sin(kx) ^ 2.0) + (sin(ky_m) ^ 2.0)))))))));
	else
		tmp = sqrt(Float64(0.5 + Float64(0.25 * Float64(Float64(Om_m / ky_m) / l_m))));
	end
	return tmp
end
l_m = abs(l);
Om_m = abs(Om);
ky_m = abs(ky);
l_m, Om_m, kx, ky_m = num2cell(sort([l_m, Om_m, kx, ky_m])){:}
function tmp_2 = code(l_m, Om_m, kx, ky_m)
	tmp = 0.0;
	if (((2.0 * l_m) / Om_m) <= 2e+151)
		tmp = sqrt((0.5 + (0.5 * (1.0 / sqrt((1.0 + (((2.0 * (l_m / Om_m)) ^ 2.0) * ((sin(kx) ^ 2.0) + (sin(ky_m) ^ 2.0)))))))));
	else
		tmp = sqrt((0.5 + (0.25 * ((Om_m / ky_m) / l_m))));
	end
	tmp_2 = tmp;
end
l_m = N[Abs[l], $MachinePrecision]
Om_m = N[Abs[Om], $MachinePrecision]
ky_m = N[Abs[ky], $MachinePrecision]
NOTE: l_m, Om_m, kx, and ky_m should be sorted in increasing order before calling this function.
code[l$95$m_, Om$95$m_, kx_, ky$95$m_] := If[LessEqual[N[(N[(2.0 * l$95$m), $MachinePrecision] / Om$95$m), $MachinePrecision], 2e+151], N[Sqrt[N[(0.5 + N[(0.5 * N[(1.0 / N[Sqrt[N[(1.0 + N[(N[Power[N[(2.0 * N[(l$95$m / Om$95$m), $MachinePrecision]), $MachinePrecision], 2.0], $MachinePrecision] * N[(N[Power[N[Sin[kx], $MachinePrecision], 2.0], $MachinePrecision] + N[Power[N[Sin[ky$95$m], $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[Sqrt[N[(0.5 + N[(0.25 * N[(N[(Om$95$m / ky$95$m), $MachinePrecision] / l$95$m), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
l_m = \left|\ell\right|
\\
Om_m = \left|Om\right|
\\
ky_m = \left|ky\right|
\\
[l_m, Om_m, kx, ky_m] = \mathsf{sort}([l_m, Om_m, kx, ky_m])\\
\\
\begin{array}{l}
\mathbf{if}\;\frac{2 \cdot l\_m}{Om\_m} \leq 2 \cdot 10^{+151}:\\
\;\;\;\;\sqrt{0.5 + 0.5 \cdot \frac{1}{\sqrt{1 + {\left(2 \cdot \frac{l\_m}{Om\_m}\right)}^{2} \cdot \left({\sin kx}^{2} + {\sin ky\_m}^{2}\right)}}}\\

\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 + 0.25 \cdot \frac{\frac{Om\_m}{ky\_m}}{l\_m}}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (/.f64 (*.f64 #s(literal 2 binary64) l) Om) < 2.00000000000000003e151

    1. Initial program 98.7%

      \[\sqrt{\frac{1}{2} \cdot \left(1 + \frac{1}{\sqrt{1 + {\left(\frac{2 \cdot \ell}{Om}\right)}^{2} \cdot \left({\sin kx}^{2} + {\sin ky}^{2}\right)}}\right)} \]
    2. Simplified98.7%

      \[\leadsto \color{blue}{\sqrt{0.5 + 0.5 \cdot \frac{1}{\sqrt{1 + {\left(2 \cdot \frac{\ell}{Om}\right)}^{2} \cdot \left({\sin kx}^{2} + {\sin ky}^{2}\right)}}}} \]
    3. Add Preprocessing

    if 2.00000000000000003e151 < (/.f64 (*.f64 #s(literal 2 binary64) l) Om)

    1. Initial program 90.2%

      \[\sqrt{\frac{1}{2} \cdot \left(1 + \frac{1}{\sqrt{1 + {\left(\frac{2 \cdot \ell}{Om}\right)}^{2} \cdot \left({\sin kx}^{2} + {\sin ky}^{2}\right)}}\right)} \]
    2. Simplified90.2%

      \[\leadsto \color{blue}{\sqrt{0.5 + 0.5 \cdot \frac{1}{\sqrt{1 + {\left(2 \cdot \frac{\ell}{Om}\right)}^{2} \cdot \left({\sin kx}^{2} + {\sin ky}^{2}\right)}}}} \]
    3. Add Preprocessing
    4. Taylor expanded in l around inf 90.8%

      \[\leadsto \sqrt{0.5 + 0.5 \cdot \frac{1}{\color{blue}{2 \cdot \left(\frac{\ell}{Om} \cdot \sqrt{{\sin kx}^{2} + {\sin ky}^{2}}\right)}}} \]
    5. Step-by-step derivation
      1. unpow290.8%

        \[\leadsto \sqrt{0.5 + 0.5 \cdot \frac{1}{2 \cdot \left(\frac{\ell}{Om} \cdot \sqrt{\color{blue}{\sin kx \cdot \sin kx} + {\sin ky}^{2}}\right)}} \]
      2. unpow290.8%

        \[\leadsto \sqrt{0.5 + 0.5 \cdot \frac{1}{2 \cdot \left(\frac{\ell}{Om} \cdot \sqrt{\sin kx \cdot \sin kx + \color{blue}{\sin ky \cdot \sin ky}}\right)}} \]
      3. hypot-undefine100.0%

        \[\leadsto \sqrt{0.5 + 0.5 \cdot \frac{1}{2 \cdot \left(\frac{\ell}{Om} \cdot \color{blue}{\mathsf{hypot}\left(\sin kx, \sin ky\right)}\right)}} \]
    6. Simplified100.0%

      \[\leadsto \sqrt{0.5 + 0.5 \cdot \frac{1}{\color{blue}{2 \cdot \left(\frac{\ell}{Om} \cdot \mathsf{hypot}\left(\sin kx, \sin ky\right)\right)}}} \]
    7. Taylor expanded in kx around 0 95.5%

      \[\leadsto \color{blue}{\sqrt{0.5 + 0.25 \cdot \frac{Om}{\ell \cdot \sin ky}}} \]
    8. Step-by-step derivation
      1. +-commutative95.5%

        \[\leadsto \sqrt{\color{blue}{0.25 \cdot \frac{Om}{\ell \cdot \sin ky} + 0.5}} \]
    9. Simplified95.5%

      \[\leadsto \color{blue}{\sqrt{0.25 \cdot \frac{Om}{\ell \cdot \sin ky} + 0.5}} \]
    10. Taylor expanded in ky around 0 95.5%

      \[\leadsto \sqrt{\color{blue}{0.25 \cdot \frac{Om}{ky \cdot \ell}} + 0.5} \]
    11. Step-by-step derivation
      1. associate-/r*95.7%

        \[\leadsto \sqrt{0.25 \cdot \color{blue}{\frac{\frac{Om}{ky}}{\ell}} + 0.5} \]
    12. Simplified95.7%

      \[\leadsto \sqrt{\color{blue}{0.25 \cdot \frac{\frac{Om}{ky}}{\ell}} + 0.5} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification98.3%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{2 \cdot \ell}{Om} \leq 2 \cdot 10^{+151}:\\ \;\;\;\;\sqrt{0.5 + 0.5 \cdot \frac{1}{\sqrt{1 + {\left(2 \cdot \frac{\ell}{Om}\right)}^{2} \cdot \left({\sin kx}^{2} + {\sin ky}^{2}\right)}}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 + 0.25 \cdot \frac{\frac{Om}{ky}}{\ell}}\\ \end{array} \]
  5. Add Preprocessing

Alternative 2: 78.5% accurate, 6.8× speedup?

\[\begin{array}{l} l_m = \left|\ell\right| \\ Om_m = \left|Om\right| \\ ky_m = \left|ky\right| \\ [l_m, Om_m, kx, ky_m] = \mathsf{sort}([l_m, Om_m, kx, ky_m])\\ \\ \begin{array}{l} \mathbf{if}\;l\_m \leq 1.15 \cdot 10^{-37}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \end{array} \]
l_m = (fabs.f64 l)
Om_m = (fabs.f64 Om)
ky_m = (fabs.f64 ky)
NOTE: l_m, Om_m, kx, and ky_m should be sorted in increasing order before calling this function.
(FPCore (l_m Om_m kx ky_m)
 :precision binary64
 (if (<= l_m 1.15e-37) 1.0 (sqrt 0.5)))
l_m = fabs(l);
Om_m = fabs(Om);
ky_m = fabs(ky);
assert(l_m < Om_m && Om_m < kx && kx < ky_m);
double code(double l_m, double Om_m, double kx, double ky_m) {
	double tmp;
	if (l_m <= 1.15e-37) {
		tmp = 1.0;
	} else {
		tmp = sqrt(0.5);
	}
	return tmp;
}
l_m = abs(l)
Om_m = abs(om)
ky_m = abs(ky)
NOTE: l_m, Om_m, kx, and ky_m should be sorted in increasing order before calling this function.
real(8) function code(l_m, om_m, kx, ky_m)
    real(8), intent (in) :: l_m
    real(8), intent (in) :: om_m
    real(8), intent (in) :: kx
    real(8), intent (in) :: ky_m
    real(8) :: tmp
    if (l_m <= 1.15d-37) then
        tmp = 1.0d0
    else
        tmp = sqrt(0.5d0)
    end if
    code = tmp
end function
l_m = Math.abs(l);
Om_m = Math.abs(Om);
ky_m = Math.abs(ky);
assert l_m < Om_m && Om_m < kx && kx < ky_m;
public static double code(double l_m, double Om_m, double kx, double ky_m) {
	double tmp;
	if (l_m <= 1.15e-37) {
		tmp = 1.0;
	} else {
		tmp = Math.sqrt(0.5);
	}
	return tmp;
}
l_m = math.fabs(l)
Om_m = math.fabs(Om)
ky_m = math.fabs(ky)
[l_m, Om_m, kx, ky_m] = sort([l_m, Om_m, kx, ky_m])
def code(l_m, Om_m, kx, ky_m):
	tmp = 0
	if l_m <= 1.15e-37:
		tmp = 1.0
	else:
		tmp = math.sqrt(0.5)
	return tmp
l_m = abs(l)
Om_m = abs(Om)
ky_m = abs(ky)
l_m, Om_m, kx, ky_m = sort([l_m, Om_m, kx, ky_m])
function code(l_m, Om_m, kx, ky_m)
	tmp = 0.0
	if (l_m <= 1.15e-37)
		tmp = 1.0;
	else
		tmp = sqrt(0.5);
	end
	return tmp
end
l_m = abs(l);
Om_m = abs(Om);
ky_m = abs(ky);
l_m, Om_m, kx, ky_m = num2cell(sort([l_m, Om_m, kx, ky_m])){:}
function tmp_2 = code(l_m, Om_m, kx, ky_m)
	tmp = 0.0;
	if (l_m <= 1.15e-37)
		tmp = 1.0;
	else
		tmp = sqrt(0.5);
	end
	tmp_2 = tmp;
end
l_m = N[Abs[l], $MachinePrecision]
Om_m = N[Abs[Om], $MachinePrecision]
ky_m = N[Abs[ky], $MachinePrecision]
NOTE: l_m, Om_m, kx, and ky_m should be sorted in increasing order before calling this function.
code[l$95$m_, Om$95$m_, kx_, ky$95$m_] := If[LessEqual[l$95$m, 1.15e-37], 1.0, N[Sqrt[0.5], $MachinePrecision]]
\begin{array}{l}
l_m = \left|\ell\right|
\\
Om_m = \left|Om\right|
\\
ky_m = \left|ky\right|
\\
[l_m, Om_m, kx, ky_m] = \mathsf{sort}([l_m, Om_m, kx, ky_m])\\
\\
\begin{array}{l}
\mathbf{if}\;l\_m \leq 1.15 \cdot 10^{-37}:\\
\;\;\;\;1\\

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


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

    1. Initial program 98.3%

      \[\sqrt{\frac{1}{2} \cdot \left(1 + \frac{1}{\sqrt{1 + {\left(\frac{2 \cdot \ell}{Om}\right)}^{2} \cdot \left({\sin kx}^{2} + {\sin ky}^{2}\right)}}\right)} \]
    2. Simplified98.3%

      \[\leadsto \color{blue}{\sqrt{0.5 + 0.5 \cdot \frac{1}{\sqrt{1 + {\left(2 \cdot \frac{\ell}{Om}\right)}^{2} \cdot \left({\sin kx}^{2} + {\sin ky}^{2}\right)}}}} \]
    3. Add Preprocessing
    4. Taylor expanded in l around 0 71.2%

      \[\leadsto \sqrt{0.5 + 0.5 \cdot \frac{1}{\color{blue}{1}}} \]
    5. Step-by-step derivation
      1. metadata-eval71.2%

        \[\leadsto \sqrt{0.5 + 0.5 \cdot \color{blue}{1}} \]
      2. metadata-eval71.2%

        \[\leadsto \sqrt{0.5 + \color{blue}{0.5}} \]
      3. metadata-eval71.2%

        \[\leadsto \sqrt{\color{blue}{1}} \]
      4. metadata-eval71.2%

        \[\leadsto \color{blue}{1} \]
    6. Applied egg-rr71.2%

      \[\leadsto \color{blue}{1} \]

    if 1.15e-37 < l

    1. Initial program 95.7%

      \[\sqrt{\frac{1}{2} \cdot \left(1 + \frac{1}{\sqrt{1 + {\left(\frac{2 \cdot \ell}{Om}\right)}^{2} \cdot \left({\sin kx}^{2} + {\sin ky}^{2}\right)}}\right)} \]
    2. Simplified95.7%

      \[\leadsto \color{blue}{\sqrt{0.5 + 0.5 \cdot \frac{1}{\sqrt{1 + {\left(2 \cdot \frac{\ell}{Om}\right)}^{2} \cdot \left({\sin kx}^{2} + {\sin ky}^{2}\right)}}}} \]
    3. Add Preprocessing
    4. Taylor expanded in l around inf 70.2%

      \[\leadsto \sqrt{0.5 + 0.5 \cdot \frac{1}{\color{blue}{2 \cdot \left(\frac{\ell}{Om} \cdot \sqrt{{\sin kx}^{2} + {\sin ky}^{2}}\right)}}} \]
    5. Step-by-step derivation
      1. unpow270.2%

        \[\leadsto \sqrt{0.5 + 0.5 \cdot \frac{1}{2 \cdot \left(\frac{\ell}{Om} \cdot \sqrt{\color{blue}{\sin kx \cdot \sin kx} + {\sin ky}^{2}}\right)}} \]
      2. unpow270.2%

        \[\leadsto \sqrt{0.5 + 0.5 \cdot \frac{1}{2 \cdot \left(\frac{\ell}{Om} \cdot \sqrt{\sin kx \cdot \sin kx + \color{blue}{\sin ky \cdot \sin ky}}\right)}} \]
      3. hypot-undefine73.0%

        \[\leadsto \sqrt{0.5 + 0.5 \cdot \frac{1}{2 \cdot \left(\frac{\ell}{Om} \cdot \color{blue}{\mathsf{hypot}\left(\sin kx, \sin ky\right)}\right)}} \]
    6. Simplified73.0%

      \[\leadsto \sqrt{0.5 + 0.5 \cdot \frac{1}{\color{blue}{2 \cdot \left(\frac{\ell}{Om} \cdot \mathsf{hypot}\left(\sin kx, \sin ky\right)\right)}}} \]
    7. Taylor expanded in l around inf 77.5%

      \[\leadsto \color{blue}{\sqrt{0.5}} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 3: 62.3% accurate, 722.0× speedup?

\[\begin{array}{l} l_m = \left|\ell\right| \\ Om_m = \left|Om\right| \\ ky_m = \left|ky\right| \\ [l_m, Om_m, kx, ky_m] = \mathsf{sort}([l_m, Om_m, kx, ky_m])\\ \\ 1 \end{array} \]
l_m = (fabs.f64 l)
Om_m = (fabs.f64 Om)
ky_m = (fabs.f64 ky)
NOTE: l_m, Om_m, kx, and ky_m should be sorted in increasing order before calling this function.
(FPCore (l_m Om_m kx ky_m) :precision binary64 1.0)
l_m = fabs(l);
Om_m = fabs(Om);
ky_m = fabs(ky);
assert(l_m < Om_m && Om_m < kx && kx < ky_m);
double code(double l_m, double Om_m, double kx, double ky_m) {
	return 1.0;
}
l_m = abs(l)
Om_m = abs(om)
ky_m = abs(ky)
NOTE: l_m, Om_m, kx, and ky_m should be sorted in increasing order before calling this function.
real(8) function code(l_m, om_m, kx, ky_m)
    real(8), intent (in) :: l_m
    real(8), intent (in) :: om_m
    real(8), intent (in) :: kx
    real(8), intent (in) :: ky_m
    code = 1.0d0
end function
l_m = Math.abs(l);
Om_m = Math.abs(Om);
ky_m = Math.abs(ky);
assert l_m < Om_m && Om_m < kx && kx < ky_m;
public static double code(double l_m, double Om_m, double kx, double ky_m) {
	return 1.0;
}
l_m = math.fabs(l)
Om_m = math.fabs(Om)
ky_m = math.fabs(ky)
[l_m, Om_m, kx, ky_m] = sort([l_m, Om_m, kx, ky_m])
def code(l_m, Om_m, kx, ky_m):
	return 1.0
l_m = abs(l)
Om_m = abs(Om)
ky_m = abs(ky)
l_m, Om_m, kx, ky_m = sort([l_m, Om_m, kx, ky_m])
function code(l_m, Om_m, kx, ky_m)
	return 1.0
end
l_m = abs(l);
Om_m = abs(Om);
ky_m = abs(ky);
l_m, Om_m, kx, ky_m = num2cell(sort([l_m, Om_m, kx, ky_m])){:}
function tmp = code(l_m, Om_m, kx, ky_m)
	tmp = 1.0;
end
l_m = N[Abs[l], $MachinePrecision]
Om_m = N[Abs[Om], $MachinePrecision]
ky_m = N[Abs[ky], $MachinePrecision]
NOTE: l_m, Om_m, kx, and ky_m should be sorted in increasing order before calling this function.
code[l$95$m_, Om$95$m_, kx_, ky$95$m_] := 1.0
\begin{array}{l}
l_m = \left|\ell\right|
\\
Om_m = \left|Om\right|
\\
ky_m = \left|ky\right|
\\
[l_m, Om_m, kx, ky_m] = \mathsf{sort}([l_m, Om_m, kx, ky_m])\\
\\
1
\end{array}
Derivation
  1. Initial program 97.6%

    \[\sqrt{\frac{1}{2} \cdot \left(1 + \frac{1}{\sqrt{1 + {\left(\frac{2 \cdot \ell}{Om}\right)}^{2} \cdot \left({\sin kx}^{2} + {\sin ky}^{2}\right)}}\right)} \]
  2. Simplified97.6%

    \[\leadsto \color{blue}{\sqrt{0.5 + 0.5 \cdot \frac{1}{\sqrt{1 + {\left(2 \cdot \frac{\ell}{Om}\right)}^{2} \cdot \left({\sin kx}^{2} + {\sin ky}^{2}\right)}}}} \]
  3. Add Preprocessing
  4. Taylor expanded in l around 0 63.1%

    \[\leadsto \sqrt{0.5 + 0.5 \cdot \frac{1}{\color{blue}{1}}} \]
  5. Step-by-step derivation
    1. metadata-eval63.1%

      \[\leadsto \sqrt{0.5 + 0.5 \cdot \color{blue}{1}} \]
    2. metadata-eval63.1%

      \[\leadsto \sqrt{0.5 + \color{blue}{0.5}} \]
    3. metadata-eval63.1%

      \[\leadsto \sqrt{\color{blue}{1}} \]
    4. metadata-eval63.1%

      \[\leadsto \color{blue}{1} \]
  6. Applied egg-rr63.1%

    \[\leadsto \color{blue}{1} \]
  7. Add Preprocessing

Reproduce

?
herbie shell --seed 2024144 
(FPCore (l Om kx ky)
  :name "Toniolo and Linder, Equation (3a)"
  :precision binary64
  (sqrt (* (/ 1.0 2.0) (+ 1.0 (/ 1.0 (sqrt (+ 1.0 (* (pow (/ (* 2.0 l) Om) 2.0) (+ (pow (sin kx) 2.0) (pow (sin ky) 2.0))))))))))