Toniolo and Linder, Equation (3a)

Percentage Accurate: 98.3% → 99.5%
Time: 15.9s
Alternatives: 5
Speedup: 2.2×

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 5 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: 99.5% accurate, 2.2× speedup?

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

\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 - \frac{-0.5}{\sqrt{1 + \left(2 \cdot \frac{1 - \cos \left(ky\_m \cdot 2\right)}{Om}\right) \cdot \frac{\ell}{\frac{Om}{\ell}}}}}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if ky < 6.6000000000000005e-11

    1. Initial program 96.4%

      \[\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. Add Preprocessing
    3. Applied egg-rr86.3%

      \[\leadsto \sqrt{\color{blue}{0.5 - \frac{-0.5}{\sqrt{1 + \frac{\left(0.5 + -0.5 \cdot \cos \left(2 \cdot kx\right)\right) + \left(0.5 + -0.5 \cdot \cos \left(2 \cdot ky\right)\right)}{\frac{Om}{4 \cdot \left(\ell \cdot \frac{\ell}{Om}\right)}}}}}} \]
    4. Taylor expanded in kx around 0

      \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \mathsf{/.f64}\left(\color{blue}{\left(\frac{1}{2} + \frac{-1}{2} \cdot \cos \left(2 \cdot ky\right)\right)}, \mathsf{/.f64}\left(Om, \mathsf{*.f64}\left(4, \mathsf{*.f64}\left(\ell, \mathsf{/.f64}\left(\ell, Om\right)\right)\right)\right)\right)\right)\right)\right)\right)\right) \]
    5. Step-by-step derivation
      1. +-lowering-+.f64N/A

        \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \mathsf{/.f64}\left(\mathsf{+.f64}\left(\frac{1}{2}, \left(\frac{-1}{2} \cdot \cos \left(2 \cdot ky\right)\right)\right), \mathsf{/.f64}\left(Om, \mathsf{*.f64}\left(4, \mathsf{*.f64}\left(\ell, \mathsf{/.f64}\left(\ell, Om\right)\right)\right)\right)\right)\right)\right)\right)\right)\right) \]
      2. *-lowering-*.f64N/A

        \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \mathsf{/.f64}\left(\mathsf{+.f64}\left(\frac{1}{2}, \mathsf{*.f64}\left(\frac{-1}{2}, \cos \left(2 \cdot ky\right)\right)\right), \mathsf{/.f64}\left(Om, \mathsf{*.f64}\left(4, \mathsf{*.f64}\left(\ell, \mathsf{/.f64}\left(\ell, Om\right)\right)\right)\right)\right)\right)\right)\right)\right)\right) \]
      3. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \mathsf{/.f64}\left(\mathsf{+.f64}\left(\frac{1}{2}, \mathsf{*.f64}\left(\frac{-1}{2}, \mathsf{cos.f64}\left(\left(2 \cdot ky\right)\right)\right)\right), \mathsf{/.f64}\left(Om, \mathsf{*.f64}\left(4, \mathsf{*.f64}\left(\ell, \mathsf{/.f64}\left(\ell, Om\right)\right)\right)\right)\right)\right)\right)\right)\right)\right) \]
      4. *-lowering-*.f6470.6%

        \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \mathsf{/.f64}\left(\mathsf{+.f64}\left(\frac{1}{2}, \mathsf{*.f64}\left(\frac{-1}{2}, \mathsf{cos.f64}\left(\mathsf{*.f64}\left(2, ky\right)\right)\right)\right), \mathsf{/.f64}\left(Om, \mathsf{*.f64}\left(4, \mathsf{*.f64}\left(\ell, \mathsf{/.f64}\left(\ell, Om\right)\right)\right)\right)\right)\right)\right)\right)\right)\right) \]
    6. Simplified70.6%

      \[\leadsto \sqrt{0.5 - \frac{-0.5}{\sqrt{1 + \frac{\color{blue}{0.5 + -0.5 \cdot \cos \left(2 \cdot ky\right)}}{\frac{Om}{4 \cdot \left(\ell \cdot \frac{\ell}{Om}\right)}}}}} \]
    7. Step-by-step derivation
      1. clear-numN/A

        \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \left(\frac{\frac{1}{2} + \frac{-1}{2} \cdot \cos \left(2 \cdot ky\right)}{\frac{Om}{4 \cdot \left(\ell \cdot \frac{1}{\frac{Om}{\ell}}\right)}}\right)\right)\right)\right)\right)\right) \]
      2. div-invN/A

        \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \left(\frac{\frac{1}{2} + \frac{-1}{2} \cdot \cos \left(2 \cdot ky\right)}{\frac{Om}{4 \cdot \frac{\ell}{\frac{Om}{\ell}}}}\right)\right)\right)\right)\right)\right) \]
      3. associate-/r*N/A

        \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \left(\frac{\frac{1}{2} + \frac{-1}{2} \cdot \cos \left(2 \cdot ky\right)}{\frac{\frac{Om}{4}}{\frac{\ell}{\frac{Om}{\ell}}}}\right)\right)\right)\right)\right)\right) \]
      4. metadata-evalN/A

        \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \left(\frac{\frac{1}{2} + \left(\mathsf{neg}\left(\frac{1}{2}\right)\right) \cdot \cos \left(2 \cdot ky\right)}{\frac{\frac{Om}{4}}{\frac{\ell}{\frac{Om}{\ell}}}}\right)\right)\right)\right)\right)\right) \]
      5. cancel-sign-sub-invN/A

        \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \left(\frac{\frac{1}{2} - \frac{1}{2} \cdot \cos \left(2 \cdot ky\right)}{\frac{\frac{Om}{4}}{\frac{\ell}{\frac{Om}{\ell}}}}\right)\right)\right)\right)\right)\right) \]
      6. sqr-sin-aN/A

        \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \left(\frac{\sin ky \cdot \sin ky}{\frac{\frac{Om}{4}}{\frac{\ell}{\frac{Om}{\ell}}}}\right)\right)\right)\right)\right)\right) \]
      7. associate-/r/N/A

        \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \left(\frac{\sin ky \cdot \sin ky}{\frac{\frac{Om}{4}}{\ell} \cdot \frac{Om}{\ell}}\right)\right)\right)\right)\right)\right) \]
      8. times-fracN/A

        \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \left(\frac{\sin ky}{\frac{\frac{Om}{4}}{\ell}} \cdot \frac{\sin ky}{\frac{Om}{\ell}}\right)\right)\right)\right)\right)\right) \]
    8. Applied egg-rr89.9%

      \[\leadsto \sqrt{0.5 - \frac{-0.5}{\sqrt{1 + \color{blue}{\frac{\sin ky}{\frac{\frac{Om}{\ell}}{4}} \cdot \frac{\sin ky}{\frac{Om}{\ell}}}}}} \]
    9. Taylor expanded in ky around 0

      \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \mathsf{*.f64}\left(\color{blue}{\left(4 \cdot \frac{ky \cdot \ell}{Om}\right)}, \mathsf{/.f64}\left(\mathsf{sin.f64}\left(ky\right), \mathsf{/.f64}\left(Om, \ell\right)\right)\right)\right)\right)\right)\right)\right) \]
    10. Step-by-step derivation
      1. associate-*r/N/A

        \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \mathsf{*.f64}\left(\left(\frac{4 \cdot \left(ky \cdot \ell\right)}{Om}\right), \mathsf{/.f64}\left(\mathsf{sin.f64}\left(ky\right), \mathsf{/.f64}\left(Om, \ell\right)\right)\right)\right)\right)\right)\right)\right) \]
      2. /-lowering-/.f64N/A

        \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \mathsf{*.f64}\left(\mathsf{/.f64}\left(\left(4 \cdot \left(ky \cdot \ell\right)\right), Om\right), \mathsf{/.f64}\left(\mathsf{sin.f64}\left(ky\right), \mathsf{/.f64}\left(Om, \ell\right)\right)\right)\right)\right)\right)\right)\right) \]
      3. *-lowering-*.f64N/A

        \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \mathsf{*.f64}\left(\mathsf{/.f64}\left(\mathsf{*.f64}\left(4, \left(ky \cdot \ell\right)\right), Om\right), \mathsf{/.f64}\left(\mathsf{sin.f64}\left(ky\right), \mathsf{/.f64}\left(Om, \ell\right)\right)\right)\right)\right)\right)\right)\right) \]
      4. *-lowering-*.f6479.6%

        \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \mathsf{*.f64}\left(\mathsf{/.f64}\left(\mathsf{*.f64}\left(4, \mathsf{*.f64}\left(ky, \ell\right)\right), Om\right), \mathsf{/.f64}\left(\mathsf{sin.f64}\left(ky\right), \mathsf{/.f64}\left(Om, \ell\right)\right)\right)\right)\right)\right)\right)\right) \]
    11. Simplified79.6%

      \[\leadsto \sqrt{0.5 - \frac{-0.5}{\sqrt{1 + \color{blue}{\frac{4 \cdot \left(ky \cdot \ell\right)}{Om}} \cdot \frac{\sin ky}{\frac{Om}{\ell}}}}} \]

    if 6.6000000000000005e-11 < ky

    1. Initial program 100.0%

      \[\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. Add Preprocessing
    3. Applied egg-rr100.0%

      \[\leadsto \sqrt{\color{blue}{0.5 - \frac{-0.5}{\sqrt{1 + \frac{\left(0.5 + -0.5 \cdot \cos \left(2 \cdot kx\right)\right) + \left(0.5 + -0.5 \cdot \cos \left(2 \cdot ky\right)\right)}{\frac{Om}{4 \cdot \left(\ell \cdot \frac{\ell}{Om}\right)}}}}}} \]
    4. Taylor expanded in kx around 0

      \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \mathsf{/.f64}\left(\color{blue}{\left(\frac{1}{2} + \frac{-1}{2} \cdot \cos \left(2 \cdot ky\right)\right)}, \mathsf{/.f64}\left(Om, \mathsf{*.f64}\left(4, \mathsf{*.f64}\left(\ell, \mathsf{/.f64}\left(\ell, Om\right)\right)\right)\right)\right)\right)\right)\right)\right)\right) \]
    5. Step-by-step derivation
      1. +-lowering-+.f64N/A

        \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \mathsf{/.f64}\left(\mathsf{+.f64}\left(\frac{1}{2}, \left(\frac{-1}{2} \cdot \cos \left(2 \cdot ky\right)\right)\right), \mathsf{/.f64}\left(Om, \mathsf{*.f64}\left(4, \mathsf{*.f64}\left(\ell, \mathsf{/.f64}\left(\ell, Om\right)\right)\right)\right)\right)\right)\right)\right)\right)\right) \]
      2. *-lowering-*.f64N/A

        \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \mathsf{/.f64}\left(\mathsf{+.f64}\left(\frac{1}{2}, \mathsf{*.f64}\left(\frac{-1}{2}, \cos \left(2 \cdot ky\right)\right)\right), \mathsf{/.f64}\left(Om, \mathsf{*.f64}\left(4, \mathsf{*.f64}\left(\ell, \mathsf{/.f64}\left(\ell, Om\right)\right)\right)\right)\right)\right)\right)\right)\right)\right) \]
      3. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \mathsf{/.f64}\left(\mathsf{+.f64}\left(\frac{1}{2}, \mathsf{*.f64}\left(\frac{-1}{2}, \mathsf{cos.f64}\left(\left(2 \cdot ky\right)\right)\right)\right), \mathsf{/.f64}\left(Om, \mathsf{*.f64}\left(4, \mathsf{*.f64}\left(\ell, \mathsf{/.f64}\left(\ell, Om\right)\right)\right)\right)\right)\right)\right)\right)\right)\right) \]
      4. *-lowering-*.f6499.6%

        \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \mathsf{/.f64}\left(\mathsf{+.f64}\left(\frac{1}{2}, \mathsf{*.f64}\left(\frac{-1}{2}, \mathsf{cos.f64}\left(\mathsf{*.f64}\left(2, ky\right)\right)\right)\right), \mathsf{/.f64}\left(Om, \mathsf{*.f64}\left(4, \mathsf{*.f64}\left(\ell, \mathsf{/.f64}\left(\ell, Om\right)\right)\right)\right)\right)\right)\right)\right)\right)\right) \]
    6. Simplified99.6%

      \[\leadsto \sqrt{0.5 - \frac{-0.5}{\sqrt{1 + \frac{\color{blue}{0.5 + -0.5 \cdot \cos \left(2 \cdot ky\right)}}{\frac{Om}{4 \cdot \left(\ell \cdot \frac{\ell}{Om}\right)}}}}} \]
    7. Step-by-step derivation
      1. clear-numN/A

        \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \left(\frac{\frac{1}{2} + \frac{-1}{2} \cdot \cos \left(2 \cdot ky\right)}{\frac{Om}{4 \cdot \left(\ell \cdot \frac{1}{\frac{Om}{\ell}}\right)}}\right)\right)\right)\right)\right)\right) \]
      2. div-invN/A

        \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \left(\frac{\frac{1}{2} + \frac{-1}{2} \cdot \cos \left(2 \cdot ky\right)}{\frac{Om}{4 \cdot \frac{\ell}{\frac{Om}{\ell}}}}\right)\right)\right)\right)\right)\right) \]
      3. associate-/r*N/A

        \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \left(\frac{\frac{1}{2} + \frac{-1}{2} \cdot \cos \left(2 \cdot ky\right)}{\frac{\frac{Om}{4}}{\frac{\ell}{\frac{Om}{\ell}}}}\right)\right)\right)\right)\right)\right) \]
      4. associate-/r/N/A

        \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \left(\frac{\frac{1}{2} + \frac{-1}{2} \cdot \cos \left(2 \cdot ky\right)}{\frac{Om}{4}} \cdot \frac{\ell}{\frac{Om}{\ell}}\right)\right)\right)\right)\right)\right) \]
      5. *-lowering-*.f64N/A

        \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \mathsf{*.f64}\left(\left(\frac{\frac{1}{2} + \frac{-1}{2} \cdot \cos \left(2 \cdot ky\right)}{\frac{Om}{4}}\right), \left(\frac{\ell}{\frac{Om}{\ell}}\right)\right)\right)\right)\right)\right)\right) \]
    8. Applied egg-rr99.6%

      \[\leadsto \sqrt{0.5 - \frac{-0.5}{\sqrt{1 + \color{blue}{\left(\frac{1 - \cos \left(ky \cdot 2\right)}{Om} \cdot 2\right) \cdot \frac{\ell}{\frac{Om}{\ell}}}}}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification84.3%

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

Alternative 2: 99.6% accurate, 1.7× speedup?

\[\begin{array}{l} kx_m = \left|kx\right| \\ ky_m = \left|ky\right| \\ [l, Om, kx_m, ky_m] = \mathsf{sort}([l, Om, kx_m, ky_m])\\ \\ \sqrt{0.5 - \frac{-0.5}{\sqrt{1 + \frac{\sin ky\_m}{\frac{\frac{Om}{\ell}}{4}} \cdot \frac{\sin ky\_m}{\frac{Om}{\ell}}}}} \end{array} \]
kx_m = (fabs.f64 kx)
ky_m = (fabs.f64 ky)
NOTE: l, Om, kx_m, and ky_m should be sorted in increasing order before calling this function.
(FPCore (l Om kx_m ky_m)
 :precision binary64
 (sqrt
  (-
   0.5
   (/
    -0.5
    (sqrt
     (+ 1.0 (* (/ (sin ky_m) (/ (/ Om l) 4.0)) (/ (sin ky_m) (/ Om l)))))))))
kx_m = fabs(kx);
ky_m = fabs(ky);
assert(l < Om && Om < kx_m && kx_m < ky_m);
double code(double l, double Om, double kx_m, double ky_m) {
	return sqrt((0.5 - (-0.5 / sqrt((1.0 + ((sin(ky_m) / ((Om / l) / 4.0)) * (sin(ky_m) / (Om / l))))))));
}
kx_m = abs(kx)
ky_m = abs(ky)
NOTE: l, Om, kx_m, and ky_m should be sorted in increasing order before calling this function.
real(8) function code(l, om, kx_m, ky_m)
    real(8), intent (in) :: l
    real(8), intent (in) :: om
    real(8), intent (in) :: kx_m
    real(8), intent (in) :: ky_m
    code = sqrt((0.5d0 - ((-0.5d0) / sqrt((1.0d0 + ((sin(ky_m) / ((om / l) / 4.0d0)) * (sin(ky_m) / (om / l))))))))
end function
kx_m = Math.abs(kx);
ky_m = Math.abs(ky);
assert l < Om && Om < kx_m && kx_m < ky_m;
public static double code(double l, double Om, double kx_m, double ky_m) {
	return Math.sqrt((0.5 - (-0.5 / Math.sqrt((1.0 + ((Math.sin(ky_m) / ((Om / l) / 4.0)) * (Math.sin(ky_m) / (Om / l))))))));
}
kx_m = math.fabs(kx)
ky_m = math.fabs(ky)
[l, Om, kx_m, ky_m] = sort([l, Om, kx_m, ky_m])
def code(l, Om, kx_m, ky_m):
	return math.sqrt((0.5 - (-0.5 / math.sqrt((1.0 + ((math.sin(ky_m) / ((Om / l) / 4.0)) * (math.sin(ky_m) / (Om / l))))))))
kx_m = abs(kx)
ky_m = abs(ky)
l, Om, kx_m, ky_m = sort([l, Om, kx_m, ky_m])
function code(l, Om, kx_m, ky_m)
	return sqrt(Float64(0.5 - Float64(-0.5 / sqrt(Float64(1.0 + Float64(Float64(sin(ky_m) / Float64(Float64(Om / l) / 4.0)) * Float64(sin(ky_m) / Float64(Om / l))))))))
end
kx_m = abs(kx);
ky_m = abs(ky);
l, Om, kx_m, ky_m = num2cell(sort([l, Om, kx_m, ky_m])){:}
function tmp = code(l, Om, kx_m, ky_m)
	tmp = sqrt((0.5 - (-0.5 / sqrt((1.0 + ((sin(ky_m) / ((Om / l) / 4.0)) * (sin(ky_m) / (Om / l))))))));
end
kx_m = N[Abs[kx], $MachinePrecision]
ky_m = N[Abs[ky], $MachinePrecision]
NOTE: l, Om, kx_m, and ky_m should be sorted in increasing order before calling this function.
code[l_, Om_, kx$95$m_, ky$95$m_] := N[Sqrt[N[(0.5 - N[(-0.5 / N[Sqrt[N[(1.0 + N[(N[(N[Sin[ky$95$m], $MachinePrecision] / N[(N[(Om / l), $MachinePrecision] / 4.0), $MachinePrecision]), $MachinePrecision] * N[(N[Sin[ky$95$m], $MachinePrecision] / N[(Om / l), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
kx_m = \left|kx\right|
\\
ky_m = \left|ky\right|
\\
[l, Om, kx_m, ky_m] = \mathsf{sort}([l, Om, kx_m, ky_m])\\
\\
\sqrt{0.5 - \frac{-0.5}{\sqrt{1 + \frac{\sin ky\_m}{\frac{\frac{Om}{\ell}}{4}} \cdot \frac{\sin ky\_m}{\frac{Om}{\ell}}}}}
\end{array}
Derivation
  1. Initial program 97.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. Add Preprocessing
  3. Applied egg-rr89.5%

    \[\leadsto \sqrt{\color{blue}{0.5 - \frac{-0.5}{\sqrt{1 + \frac{\left(0.5 + -0.5 \cdot \cos \left(2 \cdot kx\right)\right) + \left(0.5 + -0.5 \cdot \cos \left(2 \cdot ky\right)\right)}{\frac{Om}{4 \cdot \left(\ell \cdot \frac{\ell}{Om}\right)}}}}}} \]
  4. Taylor expanded in kx around 0

    \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \mathsf{/.f64}\left(\color{blue}{\left(\frac{1}{2} + \frac{-1}{2} \cdot \cos \left(2 \cdot ky\right)\right)}, \mathsf{/.f64}\left(Om, \mathsf{*.f64}\left(4, \mathsf{*.f64}\left(\ell, \mathsf{/.f64}\left(\ell, Om\right)\right)\right)\right)\right)\right)\right)\right)\right)\right) \]
  5. Step-by-step derivation
    1. +-lowering-+.f64N/A

      \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \mathsf{/.f64}\left(\mathsf{+.f64}\left(\frac{1}{2}, \left(\frac{-1}{2} \cdot \cos \left(2 \cdot ky\right)\right)\right), \mathsf{/.f64}\left(Om, \mathsf{*.f64}\left(4, \mathsf{*.f64}\left(\ell, \mathsf{/.f64}\left(\ell, Om\right)\right)\right)\right)\right)\right)\right)\right)\right)\right) \]
    2. *-lowering-*.f64N/A

      \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \mathsf{/.f64}\left(\mathsf{+.f64}\left(\frac{1}{2}, \mathsf{*.f64}\left(\frac{-1}{2}, \cos \left(2 \cdot ky\right)\right)\right), \mathsf{/.f64}\left(Om, \mathsf{*.f64}\left(4, \mathsf{*.f64}\left(\ell, \mathsf{/.f64}\left(\ell, Om\right)\right)\right)\right)\right)\right)\right)\right)\right)\right) \]
    3. cos-lowering-cos.f64N/A

      \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \mathsf{/.f64}\left(\mathsf{+.f64}\left(\frac{1}{2}, \mathsf{*.f64}\left(\frac{-1}{2}, \mathsf{cos.f64}\left(\left(2 \cdot ky\right)\right)\right)\right), \mathsf{/.f64}\left(Om, \mathsf{*.f64}\left(4, \mathsf{*.f64}\left(\ell, \mathsf{/.f64}\left(\ell, Om\right)\right)\right)\right)\right)\right)\right)\right)\right)\right) \]
    4. *-lowering-*.f6477.4%

      \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \mathsf{/.f64}\left(\mathsf{+.f64}\left(\frac{1}{2}, \mathsf{*.f64}\left(\frac{-1}{2}, \mathsf{cos.f64}\left(\mathsf{*.f64}\left(2, ky\right)\right)\right)\right), \mathsf{/.f64}\left(Om, \mathsf{*.f64}\left(4, \mathsf{*.f64}\left(\ell, \mathsf{/.f64}\left(\ell, Om\right)\right)\right)\right)\right)\right)\right)\right)\right)\right) \]
  6. Simplified77.4%

    \[\leadsto \sqrt{0.5 - \frac{-0.5}{\sqrt{1 + \frac{\color{blue}{0.5 + -0.5 \cdot \cos \left(2 \cdot ky\right)}}{\frac{Om}{4 \cdot \left(\ell \cdot \frac{\ell}{Om}\right)}}}}} \]
  7. Step-by-step derivation
    1. clear-numN/A

      \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \left(\frac{\frac{1}{2} + \frac{-1}{2} \cdot \cos \left(2 \cdot ky\right)}{\frac{Om}{4 \cdot \left(\ell \cdot \frac{1}{\frac{Om}{\ell}}\right)}}\right)\right)\right)\right)\right)\right) \]
    2. div-invN/A

      \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \left(\frac{\frac{1}{2} + \frac{-1}{2} \cdot \cos \left(2 \cdot ky\right)}{\frac{Om}{4 \cdot \frac{\ell}{\frac{Om}{\ell}}}}\right)\right)\right)\right)\right)\right) \]
    3. associate-/r*N/A

      \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \left(\frac{\frac{1}{2} + \frac{-1}{2} \cdot \cos \left(2 \cdot ky\right)}{\frac{\frac{Om}{4}}{\frac{\ell}{\frac{Om}{\ell}}}}\right)\right)\right)\right)\right)\right) \]
    4. metadata-evalN/A

      \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \left(\frac{\frac{1}{2} + \left(\mathsf{neg}\left(\frac{1}{2}\right)\right) \cdot \cos \left(2 \cdot ky\right)}{\frac{\frac{Om}{4}}{\frac{\ell}{\frac{Om}{\ell}}}}\right)\right)\right)\right)\right)\right) \]
    5. cancel-sign-sub-invN/A

      \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \left(\frac{\frac{1}{2} - \frac{1}{2} \cdot \cos \left(2 \cdot ky\right)}{\frac{\frac{Om}{4}}{\frac{\ell}{\frac{Om}{\ell}}}}\right)\right)\right)\right)\right)\right) \]
    6. sqr-sin-aN/A

      \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \left(\frac{\sin ky \cdot \sin ky}{\frac{\frac{Om}{4}}{\frac{\ell}{\frac{Om}{\ell}}}}\right)\right)\right)\right)\right)\right) \]
    7. associate-/r/N/A

      \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \left(\frac{\sin ky \cdot \sin ky}{\frac{\frac{Om}{4}}{\ell} \cdot \frac{Om}{\ell}}\right)\right)\right)\right)\right)\right) \]
    8. times-fracN/A

      \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \left(\frac{\sin ky}{\frac{\frac{Om}{4}}{\ell}} \cdot \frac{\sin ky}{\frac{Om}{\ell}}\right)\right)\right)\right)\right)\right) \]
  8. Applied egg-rr92.2%

    \[\leadsto \sqrt{0.5 - \frac{-0.5}{\sqrt{1 + \color{blue}{\frac{\sin ky}{\frac{\frac{Om}{\ell}}{4}} \cdot \frac{\sin ky}{\frac{Om}{\ell}}}}}} \]
  9. Add Preprocessing

Alternative 3: 71.2% accurate, 2.2× speedup?

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

\mathbf{elif}\;\ell \leq 1.2 \cdot 10^{+107}:\\
\;\;\;\;\sqrt{0.5 - \frac{-0.5}{\sqrt{1 + \frac{\sin ky\_m}{\frac{Om}{\ell}} \cdot \frac{4 \cdot \left(ky\_m \cdot \ell\right)}{Om}}}}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if l < 4.5000000000000004e-171

    1. Initial program 98.8%

      \[\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. Step-by-step derivation
      1. sqrt-lowering-sqrt.f64N/A

        \[\leadsto \mathsf{sqrt.f64}\left(\left(\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)\right)\right) \]
      2. *-commutativeN/A

        \[\leadsto \mathsf{sqrt.f64}\left(\left(\left(1 + \frac{1}{\sqrt{1 + {\left(\frac{2 \cdot \ell}{Om}\right)}^{2} \cdot \left({\sin kx}^{2} + {\sin ky}^{2}\right)}}\right) \cdot \frac{1}{2}\right)\right) \]
      3. +-commutativeN/A

        \[\leadsto \mathsf{sqrt.f64}\left(\left(\left(\frac{1}{\sqrt{1 + {\left(\frac{2 \cdot \ell}{Om}\right)}^{2} \cdot \left({\sin kx}^{2} + {\sin ky}^{2}\right)}} + 1\right) \cdot \frac{1}{2}\right)\right) \]
      4. distribute-rgt1-inN/A

        \[\leadsto \mathsf{sqrt.f64}\left(\left(\frac{1}{2} + \frac{1}{\sqrt{1 + {\left(\frac{2 \cdot \ell}{Om}\right)}^{2} \cdot \left({\sin kx}^{2} + {\sin ky}^{2}\right)}} \cdot \frac{1}{2}\right)\right) \]
      5. +-lowering-+.f64N/A

        \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(\left(\frac{1}{2}\right), \left(\frac{1}{\sqrt{1 + {\left(\frac{2 \cdot \ell}{Om}\right)}^{2} \cdot \left({\sin kx}^{2} + {\sin ky}^{2}\right)}} \cdot \frac{1}{2}\right)\right)\right) \]
      6. metadata-evalN/A

        \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(\frac{1}{2}, \left(\frac{1}{\sqrt{1 + {\left(\frac{2 \cdot \ell}{Om}\right)}^{2} \cdot \left({\sin kx}^{2} + {\sin ky}^{2}\right)}} \cdot \frac{1}{2}\right)\right)\right) \]
      7. associate-*l/N/A

        \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(\frac{1}{2}, \left(\frac{1 \cdot \frac{1}{2}}{\sqrt{1 + {\left(\frac{2 \cdot \ell}{Om}\right)}^{2} \cdot \left({\sin kx}^{2} + {\sin ky}^{2}\right)}}\right)\right)\right) \]
      8. metadata-evalN/A

        \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(\frac{1}{2}, \left(\frac{1 \cdot \frac{1}{2}}{\sqrt{1 + {\left(\frac{2 \cdot \ell}{Om}\right)}^{2} \cdot \left({\sin kx}^{2} + {\sin ky}^{2}\right)}}\right)\right)\right) \]
      9. metadata-evalN/A

        \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(\frac{1}{2}, \left(\frac{\frac{1}{2}}{\sqrt{1 + {\left(\frac{2 \cdot \ell}{Om}\right)}^{2} \cdot \left({\sin kx}^{2} + {\sin ky}^{2}\right)}}\right)\right)\right) \]
      10. metadata-evalN/A

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

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

      \[\leadsto \color{blue}{1} \]
    6. Step-by-step derivation
      1. Simplified72.1%

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

      if 4.5000000000000004e-171 < l < 1.2e107

      1. Initial program 95.9%

        \[\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. Add Preprocessing
      3. Applied egg-rr88.6%

        \[\leadsto \sqrt{\color{blue}{0.5 - \frac{-0.5}{\sqrt{1 + \frac{\left(0.5 + -0.5 \cdot \cos \left(2 \cdot kx\right)\right) + \left(0.5 + -0.5 \cdot \cos \left(2 \cdot ky\right)\right)}{\frac{Om}{4 \cdot \left(\ell \cdot \frac{\ell}{Om}\right)}}}}}} \]
      4. Taylor expanded in kx around 0

        \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \mathsf{/.f64}\left(\color{blue}{\left(\frac{1}{2} + \frac{-1}{2} \cdot \cos \left(2 \cdot ky\right)\right)}, \mathsf{/.f64}\left(Om, \mathsf{*.f64}\left(4, \mathsf{*.f64}\left(\ell, \mathsf{/.f64}\left(\ell, Om\right)\right)\right)\right)\right)\right)\right)\right)\right)\right) \]
      5. Step-by-step derivation
        1. +-lowering-+.f64N/A

          \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \mathsf{/.f64}\left(\mathsf{+.f64}\left(\frac{1}{2}, \left(\frac{-1}{2} \cdot \cos \left(2 \cdot ky\right)\right)\right), \mathsf{/.f64}\left(Om, \mathsf{*.f64}\left(4, \mathsf{*.f64}\left(\ell, \mathsf{/.f64}\left(\ell, Om\right)\right)\right)\right)\right)\right)\right)\right)\right)\right) \]
        2. *-lowering-*.f64N/A

          \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \mathsf{/.f64}\left(\mathsf{+.f64}\left(\frac{1}{2}, \mathsf{*.f64}\left(\frac{-1}{2}, \cos \left(2 \cdot ky\right)\right)\right), \mathsf{/.f64}\left(Om, \mathsf{*.f64}\left(4, \mathsf{*.f64}\left(\ell, \mathsf{/.f64}\left(\ell, Om\right)\right)\right)\right)\right)\right)\right)\right)\right)\right) \]
        3. cos-lowering-cos.f64N/A

          \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \mathsf{/.f64}\left(\mathsf{+.f64}\left(\frac{1}{2}, \mathsf{*.f64}\left(\frac{-1}{2}, \mathsf{cos.f64}\left(\left(2 \cdot ky\right)\right)\right)\right), \mathsf{/.f64}\left(Om, \mathsf{*.f64}\left(4, \mathsf{*.f64}\left(\ell, \mathsf{/.f64}\left(\ell, Om\right)\right)\right)\right)\right)\right)\right)\right)\right)\right) \]
        4. *-lowering-*.f6480.0%

          \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \mathsf{/.f64}\left(\mathsf{+.f64}\left(\frac{1}{2}, \mathsf{*.f64}\left(\frac{-1}{2}, \mathsf{cos.f64}\left(\mathsf{*.f64}\left(2, ky\right)\right)\right)\right), \mathsf{/.f64}\left(Om, \mathsf{*.f64}\left(4, \mathsf{*.f64}\left(\ell, \mathsf{/.f64}\left(\ell, Om\right)\right)\right)\right)\right)\right)\right)\right)\right)\right) \]
      6. Simplified80.0%

        \[\leadsto \sqrt{0.5 - \frac{-0.5}{\sqrt{1 + \frac{\color{blue}{0.5 + -0.5 \cdot \cos \left(2 \cdot ky\right)}}{\frac{Om}{4 \cdot \left(\ell \cdot \frac{\ell}{Om}\right)}}}}} \]
      7. Step-by-step derivation
        1. clear-numN/A

          \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \left(\frac{\frac{1}{2} + \frac{-1}{2} \cdot \cos \left(2 \cdot ky\right)}{\frac{Om}{4 \cdot \left(\ell \cdot \frac{1}{\frac{Om}{\ell}}\right)}}\right)\right)\right)\right)\right)\right) \]
        2. div-invN/A

          \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \left(\frac{\frac{1}{2} + \frac{-1}{2} \cdot \cos \left(2 \cdot ky\right)}{\frac{Om}{4 \cdot \frac{\ell}{\frac{Om}{\ell}}}}\right)\right)\right)\right)\right)\right) \]
        3. associate-/r*N/A

          \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \left(\frac{\frac{1}{2} + \frac{-1}{2} \cdot \cos \left(2 \cdot ky\right)}{\frac{\frac{Om}{4}}{\frac{\ell}{\frac{Om}{\ell}}}}\right)\right)\right)\right)\right)\right) \]
        4. metadata-evalN/A

          \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \left(\frac{\frac{1}{2} + \left(\mathsf{neg}\left(\frac{1}{2}\right)\right) \cdot \cos \left(2 \cdot ky\right)}{\frac{\frac{Om}{4}}{\frac{\ell}{\frac{Om}{\ell}}}}\right)\right)\right)\right)\right)\right) \]
        5. cancel-sign-sub-invN/A

          \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \left(\frac{\frac{1}{2} - \frac{1}{2} \cdot \cos \left(2 \cdot ky\right)}{\frac{\frac{Om}{4}}{\frac{\ell}{\frac{Om}{\ell}}}}\right)\right)\right)\right)\right)\right) \]
        6. sqr-sin-aN/A

          \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \left(\frac{\sin ky \cdot \sin ky}{\frac{\frac{Om}{4}}{\frac{\ell}{\frac{Om}{\ell}}}}\right)\right)\right)\right)\right)\right) \]
        7. associate-/r/N/A

          \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \left(\frac{\sin ky \cdot \sin ky}{\frac{\frac{Om}{4}}{\ell} \cdot \frac{Om}{\ell}}\right)\right)\right)\right)\right)\right) \]
        8. times-fracN/A

          \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \left(\frac{\sin ky}{\frac{\frac{Om}{4}}{\ell}} \cdot \frac{\sin ky}{\frac{Om}{\ell}}\right)\right)\right)\right)\right)\right) \]
      8. Applied egg-rr90.0%

        \[\leadsto \sqrt{0.5 - \frac{-0.5}{\sqrt{1 + \color{blue}{\frac{\sin ky}{\frac{\frac{Om}{\ell}}{4}} \cdot \frac{\sin ky}{\frac{Om}{\ell}}}}}} \]
      9. Taylor expanded in ky around 0

        \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \mathsf{*.f64}\left(\color{blue}{\left(4 \cdot \frac{ky \cdot \ell}{Om}\right)}, \mathsf{/.f64}\left(\mathsf{sin.f64}\left(ky\right), \mathsf{/.f64}\left(Om, \ell\right)\right)\right)\right)\right)\right)\right)\right) \]
      10. Step-by-step derivation
        1. associate-*r/N/A

          \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \mathsf{*.f64}\left(\left(\frac{4 \cdot \left(ky \cdot \ell\right)}{Om}\right), \mathsf{/.f64}\left(\mathsf{sin.f64}\left(ky\right), \mathsf{/.f64}\left(Om, \ell\right)\right)\right)\right)\right)\right)\right)\right) \]
        2. /-lowering-/.f64N/A

          \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \mathsf{*.f64}\left(\mathsf{/.f64}\left(\left(4 \cdot \left(ky \cdot \ell\right)\right), Om\right), \mathsf{/.f64}\left(\mathsf{sin.f64}\left(ky\right), \mathsf{/.f64}\left(Om, \ell\right)\right)\right)\right)\right)\right)\right)\right) \]
        3. *-lowering-*.f64N/A

          \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \mathsf{*.f64}\left(\mathsf{/.f64}\left(\mathsf{*.f64}\left(4, \left(ky \cdot \ell\right)\right), Om\right), \mathsf{/.f64}\left(\mathsf{sin.f64}\left(ky\right), \mathsf{/.f64}\left(Om, \ell\right)\right)\right)\right)\right)\right)\right)\right) \]
        4. *-lowering-*.f6474.1%

          \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(1, \mathsf{*.f64}\left(\mathsf{/.f64}\left(\mathsf{*.f64}\left(4, \mathsf{*.f64}\left(ky, \ell\right)\right), Om\right), \mathsf{/.f64}\left(\mathsf{sin.f64}\left(ky\right), \mathsf{/.f64}\left(Om, \ell\right)\right)\right)\right)\right)\right)\right)\right) \]
      11. Simplified74.1%

        \[\leadsto \sqrt{0.5 - \frac{-0.5}{\sqrt{1 + \color{blue}{\frac{4 \cdot \left(ky \cdot \ell\right)}{Om}} \cdot \frac{\sin ky}{\frac{Om}{\ell}}}}} \]

      if 1.2e107 < l

      1. Initial program 92.5%

        \[\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. Add Preprocessing
      3. Taylor expanded in l around inf

        \[\leadsto \mathsf{sqrt.f64}\left(\color{blue}{\frac{1}{2}}\right) \]
      4. Step-by-step derivation
        1. Simplified83.2%

          \[\leadsto \sqrt{\color{blue}{0.5}} \]
      5. Recombined 3 regimes into one program.
      6. Final simplification74.2%

        \[\leadsto \begin{array}{l} \mathbf{if}\;\ell \leq 4.5 \cdot 10^{-171}:\\ \;\;\;\;1\\ \mathbf{elif}\;\ell \leq 1.2 \cdot 10^{+107}:\\ \;\;\;\;\sqrt{0.5 - \frac{-0.5}{\sqrt{1 + \frac{\sin ky}{\frac{Om}{\ell}} \cdot \frac{4 \cdot \left(ky \cdot \ell\right)}{Om}}}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \]
      7. Add Preprocessing

      Alternative 4: 67.5% accurate, 6.8× speedup?

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

        1. Initial program 97.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. Add Preprocessing
        3. Taylor expanded in l around inf

          \[\leadsto \mathsf{sqrt.f64}\left(\color{blue}{\frac{1}{2}}\right) \]
        4. Step-by-step derivation
          1. Simplified60.6%

            \[\leadsto \sqrt{\color{blue}{0.5}} \]

          if 1.5000000000000001e-70 < Om

          1. Initial program 97.1%

            \[\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. Step-by-step derivation
            1. sqrt-lowering-sqrt.f64N/A

              \[\leadsto \mathsf{sqrt.f64}\left(\left(\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)\right)\right) \]
            2. *-commutativeN/A

              \[\leadsto \mathsf{sqrt.f64}\left(\left(\left(1 + \frac{1}{\sqrt{1 + {\left(\frac{2 \cdot \ell}{Om}\right)}^{2} \cdot \left({\sin kx}^{2} + {\sin ky}^{2}\right)}}\right) \cdot \frac{1}{2}\right)\right) \]
            3. +-commutativeN/A

              \[\leadsto \mathsf{sqrt.f64}\left(\left(\left(\frac{1}{\sqrt{1 + {\left(\frac{2 \cdot \ell}{Om}\right)}^{2} \cdot \left({\sin kx}^{2} + {\sin ky}^{2}\right)}} + 1\right) \cdot \frac{1}{2}\right)\right) \]
            4. distribute-rgt1-inN/A

              \[\leadsto \mathsf{sqrt.f64}\left(\left(\frac{1}{2} + \frac{1}{\sqrt{1 + {\left(\frac{2 \cdot \ell}{Om}\right)}^{2} \cdot \left({\sin kx}^{2} + {\sin ky}^{2}\right)}} \cdot \frac{1}{2}\right)\right) \]
            5. +-lowering-+.f64N/A

              \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(\left(\frac{1}{2}\right), \left(\frac{1}{\sqrt{1 + {\left(\frac{2 \cdot \ell}{Om}\right)}^{2} \cdot \left({\sin kx}^{2} + {\sin ky}^{2}\right)}} \cdot \frac{1}{2}\right)\right)\right) \]
            6. metadata-evalN/A

              \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(\frac{1}{2}, \left(\frac{1}{\sqrt{1 + {\left(\frac{2 \cdot \ell}{Om}\right)}^{2} \cdot \left({\sin kx}^{2} + {\sin ky}^{2}\right)}} \cdot \frac{1}{2}\right)\right)\right) \]
            7. associate-*l/N/A

              \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(\frac{1}{2}, \left(\frac{1 \cdot \frac{1}{2}}{\sqrt{1 + {\left(\frac{2 \cdot \ell}{Om}\right)}^{2} \cdot \left({\sin kx}^{2} + {\sin ky}^{2}\right)}}\right)\right)\right) \]
            8. metadata-evalN/A

              \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(\frac{1}{2}, \left(\frac{1 \cdot \frac{1}{2}}{\sqrt{1 + {\left(\frac{2 \cdot \ell}{Om}\right)}^{2} \cdot \left({\sin kx}^{2} + {\sin ky}^{2}\right)}}\right)\right)\right) \]
            9. metadata-evalN/A

              \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(\frac{1}{2}, \left(\frac{\frac{1}{2}}{\sqrt{1 + {\left(\frac{2 \cdot \ell}{Om}\right)}^{2} \cdot \left({\sin kx}^{2} + {\sin ky}^{2}\right)}}\right)\right)\right) \]
            10. metadata-evalN/A

              \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(\frac{1}{2}, \left(\frac{\frac{1}{2}}{\sqrt{1 + {\left(\frac{2 \cdot \ell}{Om}\right)}^{2} \cdot \left({\sin kx}^{2} + {\sin ky}^{2}\right)}}\right)\right)\right) \]
          3. Simplified86.0%

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

            \[\leadsto \color{blue}{1} \]
          6. Step-by-step derivation
            1. Simplified82.9%

              \[\leadsto \color{blue}{1} \]
          7. Recombined 2 regimes into one program.
          8. Add Preprocessing

          Alternative 5: 62.3% accurate, 722.0× speedup?

          \[\begin{array}{l} kx_m = \left|kx\right| \\ ky_m = \left|ky\right| \\ [l, Om, kx_m, ky_m] = \mathsf{sort}([l, Om, kx_m, ky_m])\\ \\ 1 \end{array} \]
          kx_m = (fabs.f64 kx)
          ky_m = (fabs.f64 ky)
          NOTE: l, Om, kx_m, and ky_m should be sorted in increasing order before calling this function.
          (FPCore (l Om kx_m ky_m) :precision binary64 1.0)
          kx_m = fabs(kx);
          ky_m = fabs(ky);
          assert(l < Om && Om < kx_m && kx_m < ky_m);
          double code(double l, double Om, double kx_m, double ky_m) {
          	return 1.0;
          }
          
          kx_m = abs(kx)
          ky_m = abs(ky)
          NOTE: l, Om, kx_m, and ky_m should be sorted in increasing order before calling this function.
          real(8) function code(l, om, kx_m, ky_m)
              real(8), intent (in) :: l
              real(8), intent (in) :: om
              real(8), intent (in) :: kx_m
              real(8), intent (in) :: ky_m
              code = 1.0d0
          end function
          
          kx_m = Math.abs(kx);
          ky_m = Math.abs(ky);
          assert l < Om && Om < kx_m && kx_m < ky_m;
          public static double code(double l, double Om, double kx_m, double ky_m) {
          	return 1.0;
          }
          
          kx_m = math.fabs(kx)
          ky_m = math.fabs(ky)
          [l, Om, kx_m, ky_m] = sort([l, Om, kx_m, ky_m])
          def code(l, Om, kx_m, ky_m):
          	return 1.0
          
          kx_m = abs(kx)
          ky_m = abs(ky)
          l, Om, kx_m, ky_m = sort([l, Om, kx_m, ky_m])
          function code(l, Om, kx_m, ky_m)
          	return 1.0
          end
          
          kx_m = abs(kx);
          ky_m = abs(ky);
          l, Om, kx_m, ky_m = num2cell(sort([l, Om, kx_m, ky_m])){:}
          function tmp = code(l, Om, kx_m, ky_m)
          	tmp = 1.0;
          end
          
          kx_m = N[Abs[kx], $MachinePrecision]
          ky_m = N[Abs[ky], $MachinePrecision]
          NOTE: l, Om, kx_m, and ky_m should be sorted in increasing order before calling this function.
          code[l_, Om_, kx$95$m_, ky$95$m_] := 1.0
          
          \begin{array}{l}
          kx_m = \left|kx\right|
          \\
          ky_m = \left|ky\right|
          \\
          [l, Om, kx_m, ky_m] = \mathsf{sort}([l, Om, kx_m, ky_m])\\
          \\
          1
          \end{array}
          
          Derivation
          1. Initial program 97.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. Step-by-step derivation
            1. sqrt-lowering-sqrt.f64N/A

              \[\leadsto \mathsf{sqrt.f64}\left(\left(\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)\right)\right) \]
            2. *-commutativeN/A

              \[\leadsto \mathsf{sqrt.f64}\left(\left(\left(1 + \frac{1}{\sqrt{1 + {\left(\frac{2 \cdot \ell}{Om}\right)}^{2} \cdot \left({\sin kx}^{2} + {\sin ky}^{2}\right)}}\right) \cdot \frac{1}{2}\right)\right) \]
            3. +-commutativeN/A

              \[\leadsto \mathsf{sqrt.f64}\left(\left(\left(\frac{1}{\sqrt{1 + {\left(\frac{2 \cdot \ell}{Om}\right)}^{2} \cdot \left({\sin kx}^{2} + {\sin ky}^{2}\right)}} + 1\right) \cdot \frac{1}{2}\right)\right) \]
            4. distribute-rgt1-inN/A

              \[\leadsto \mathsf{sqrt.f64}\left(\left(\frac{1}{2} + \frac{1}{\sqrt{1 + {\left(\frac{2 \cdot \ell}{Om}\right)}^{2} \cdot \left({\sin kx}^{2} + {\sin ky}^{2}\right)}} \cdot \frac{1}{2}\right)\right) \]
            5. +-lowering-+.f64N/A

              \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(\left(\frac{1}{2}\right), \left(\frac{1}{\sqrt{1 + {\left(\frac{2 \cdot \ell}{Om}\right)}^{2} \cdot \left({\sin kx}^{2} + {\sin ky}^{2}\right)}} \cdot \frac{1}{2}\right)\right)\right) \]
            6. metadata-evalN/A

              \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(\frac{1}{2}, \left(\frac{1}{\sqrt{1 + {\left(\frac{2 \cdot \ell}{Om}\right)}^{2} \cdot \left({\sin kx}^{2} + {\sin ky}^{2}\right)}} \cdot \frac{1}{2}\right)\right)\right) \]
            7. associate-*l/N/A

              \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(\frac{1}{2}, \left(\frac{1 \cdot \frac{1}{2}}{\sqrt{1 + {\left(\frac{2 \cdot \ell}{Om}\right)}^{2} \cdot \left({\sin kx}^{2} + {\sin ky}^{2}\right)}}\right)\right)\right) \]
            8. metadata-evalN/A

              \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(\frac{1}{2}, \left(\frac{1 \cdot \frac{1}{2}}{\sqrt{1 + {\left(\frac{2 \cdot \ell}{Om}\right)}^{2} \cdot \left({\sin kx}^{2} + {\sin ky}^{2}\right)}}\right)\right)\right) \]
            9. metadata-evalN/A

              \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(\frac{1}{2}, \left(\frac{\frac{1}{2}}{\sqrt{1 + {\left(\frac{2 \cdot \ell}{Om}\right)}^{2} \cdot \left({\sin kx}^{2} + {\sin ky}^{2}\right)}}\right)\right)\right) \]
            10. metadata-evalN/A

              \[\leadsto \mathsf{sqrt.f64}\left(\mathsf{+.f64}\left(\frac{1}{2}, \left(\frac{\frac{1}{2}}{\sqrt{1 + {\left(\frac{2 \cdot \ell}{Om}\right)}^{2} \cdot \left({\sin kx}^{2} + {\sin ky}^{2}\right)}}\right)\right)\right) \]
          3. Simplified82.6%

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

            \[\leadsto \color{blue}{1} \]
          6. Step-by-step derivation
            1. Simplified64.6%

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

            Reproduce

            ?
            herbie shell --seed 2024164 
            (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))))))))))