Migdal et al, Equation (64)

Percentage Accurate: 99.6% → 99.1%
Time: 15.8s
Alternatives: 12
Speedup: N/A×

Specification

?
\[\begin{array}{l} \\ \begin{array}{l} t_1 := \frac{\cos th}{\sqrt{2}}\\ t_1 \cdot \left(a1 \cdot a1\right) + t_1 \cdot \left(a2 \cdot a2\right) \end{array} \end{array} \]
(FPCore (a1 a2 th)
 :precision binary64
 (let* ((t_1 (/ (cos th) (sqrt 2.0))))
   (+ (* t_1 (* a1 a1)) (* t_1 (* a2 a2)))))
double code(double a1, double a2, double th) {
	double t_1 = cos(th) / sqrt(2.0);
	return (t_1 * (a1 * a1)) + (t_1 * (a2 * a2));
}
real(8) function code(a1, a2, th)
    real(8), intent (in) :: a1
    real(8), intent (in) :: a2
    real(8), intent (in) :: th
    real(8) :: t_1
    t_1 = cos(th) / sqrt(2.0d0)
    code = (t_1 * (a1 * a1)) + (t_1 * (a2 * a2))
end function
public static double code(double a1, double a2, double th) {
	double t_1 = Math.cos(th) / Math.sqrt(2.0);
	return (t_1 * (a1 * a1)) + (t_1 * (a2 * a2));
}
def code(a1, a2, th):
	t_1 = math.cos(th) / math.sqrt(2.0)
	return (t_1 * (a1 * a1)) + (t_1 * (a2 * a2))
function code(a1, a2, th)
	t_1 = Float64(cos(th) / sqrt(2.0))
	return Float64(Float64(t_1 * Float64(a1 * a1)) + Float64(t_1 * Float64(a2 * a2)))
end
function tmp = code(a1, a2, th)
	t_1 = cos(th) / sqrt(2.0);
	tmp = (t_1 * (a1 * a1)) + (t_1 * (a2 * a2));
end
code[a1_, a2_, th_] := Block[{t$95$1 = N[(N[Cos[th], $MachinePrecision] / N[Sqrt[2.0], $MachinePrecision]), $MachinePrecision]}, N[(N[(t$95$1 * N[(a1 * a1), $MachinePrecision]), $MachinePrecision] + N[(t$95$1 * N[(a2 * a2), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_1 := \frac{\cos th}{\sqrt{2}}\\
t_1 \cdot \left(a1 \cdot a1\right) + t_1 \cdot \left(a2 \cdot a2\right)
\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 12 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 99.6% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_1 := \frac{\cos th}{\sqrt{2}}\\ t_1 \cdot \left(a1 \cdot a1\right) + t_1 \cdot \left(a2 \cdot a2\right) \end{array} \end{array} \]
(FPCore (a1 a2 th)
 :precision binary64
 (let* ((t_1 (/ (cos th) (sqrt 2.0))))
   (+ (* t_1 (* a1 a1)) (* t_1 (* a2 a2)))))
double code(double a1, double a2, double th) {
	double t_1 = cos(th) / sqrt(2.0);
	return (t_1 * (a1 * a1)) + (t_1 * (a2 * a2));
}
real(8) function code(a1, a2, th)
    real(8), intent (in) :: a1
    real(8), intent (in) :: a2
    real(8), intent (in) :: th
    real(8) :: t_1
    t_1 = cos(th) / sqrt(2.0d0)
    code = (t_1 * (a1 * a1)) + (t_1 * (a2 * a2))
end function
public static double code(double a1, double a2, double th) {
	double t_1 = Math.cos(th) / Math.sqrt(2.0);
	return (t_1 * (a1 * a1)) + (t_1 * (a2 * a2));
}
def code(a1, a2, th):
	t_1 = math.cos(th) / math.sqrt(2.0)
	return (t_1 * (a1 * a1)) + (t_1 * (a2 * a2))
function code(a1, a2, th)
	t_1 = Float64(cos(th) / sqrt(2.0))
	return Float64(Float64(t_1 * Float64(a1 * a1)) + Float64(t_1 * Float64(a2 * a2)))
end
function tmp = code(a1, a2, th)
	t_1 = cos(th) / sqrt(2.0);
	tmp = (t_1 * (a1 * a1)) + (t_1 * (a2 * a2));
end
code[a1_, a2_, th_] := Block[{t$95$1 = N[(N[Cos[th], $MachinePrecision] / N[Sqrt[2.0], $MachinePrecision]), $MachinePrecision]}, N[(N[(t$95$1 * N[(a1 * a1), $MachinePrecision]), $MachinePrecision] + N[(t$95$1 * N[(a2 * a2), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_1 := \frac{\cos th}{\sqrt{2}}\\
t_1 \cdot \left(a1 \cdot a1\right) + t_1 \cdot \left(a2 \cdot a2\right)
\end{array}
\end{array}

Alternative 1: 99.1% accurate, 2.0× speedup?

\[\begin{array}{l} a1 = |a1|\\ a2 = |a2|\\ [a1, a2] = \mathsf{sort}([a1, a2])\\ \\ \left(a2 \cdot \cos th\right) \cdot \left(a2 \cdot \sqrt{0.5}\right) \end{array} \]
NOTE: a1 should be positive before calling this function
NOTE: a2 should be positive before calling this function
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
(FPCore (a1 a2 th) :precision binary64 (* (* a2 (cos th)) (* a2 (sqrt 0.5))))
a1 = abs(a1);
a2 = abs(a2);
assert(a1 < a2);
double code(double a1, double a2, double th) {
	return (a2 * cos(th)) * (a2 * sqrt(0.5));
}
NOTE: a1 should be positive before calling this function
NOTE: a2 should be positive before calling this function
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
real(8) function code(a1, a2, th)
    real(8), intent (in) :: a1
    real(8), intent (in) :: a2
    real(8), intent (in) :: th
    code = (a2 * cos(th)) * (a2 * sqrt(0.5d0))
end function
a1 = Math.abs(a1);
a2 = Math.abs(a2);
assert a1 < a2;
public static double code(double a1, double a2, double th) {
	return (a2 * Math.cos(th)) * (a2 * Math.sqrt(0.5));
}
a1 = abs(a1)
a2 = abs(a2)
[a1, a2] = sort([a1, a2])
def code(a1, a2, th):
	return (a2 * math.cos(th)) * (a2 * math.sqrt(0.5))
a1 = abs(a1)
a2 = abs(a2)
a1, a2 = sort([a1, a2])
function code(a1, a2, th)
	return Float64(Float64(a2 * cos(th)) * Float64(a2 * sqrt(0.5)))
end
a1 = abs(a1)
a2 = abs(a2)
a1, a2 = num2cell(sort([a1, a2])){:}
function tmp = code(a1, a2, th)
	tmp = (a2 * cos(th)) * (a2 * sqrt(0.5));
end
NOTE: a1 should be positive before calling this function
NOTE: a2 should be positive before calling this function
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
code[a1_, a2_, th_] := N[(N[(a2 * N[Cos[th], $MachinePrecision]), $MachinePrecision] * N[(a2 * N[Sqrt[0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
a1 = |a1|\\
a2 = |a2|\\
[a1, a2] = \mathsf{sort}([a1, a2])\\
\\
\left(a2 \cdot \cos th\right) \cdot \left(a2 \cdot \sqrt{0.5}\right)
\end{array}
Derivation
  1. Initial program 98.9%

    \[\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1\right) + \frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2\right) \]
  2. Step-by-step derivation
    1. distribute-lft-out98.9%

      \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)} \]
    2. cos-neg98.9%

      \[\leadsto \frac{\color{blue}{\cos \left(-th\right)}}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
    3. associate-*l/99.0%

      \[\leadsto \color{blue}{\frac{\cos \left(-th\right) \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)}{\sqrt{2}}} \]
    4. cos-neg99.0%

      \[\leadsto \frac{\color{blue}{\cos th} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)}{\sqrt{2}} \]
    5. fma-def99.0%

      \[\leadsto \frac{\cos th \cdot \color{blue}{\mathsf{fma}\left(a1, a1, a2 \cdot a2\right)}}{\sqrt{2}} \]
  3. Simplified99.0%

    \[\leadsto \color{blue}{\frac{\cos th \cdot \mathsf{fma}\left(a1, a1, a2 \cdot a2\right)}{\sqrt{2}}} \]
  4. Taylor expanded in a1 around 0 59.2%

    \[\leadsto \frac{\color{blue}{{a2}^{2} \cdot \cos th}}{\sqrt{2}} \]
  5. Step-by-step derivation
    1. unpow259.2%

      \[\leadsto \frac{\color{blue}{\left(a2 \cdot a2\right)} \cdot \cos th}{\sqrt{2}} \]
    2. associate-*l*59.5%

      \[\leadsto \frac{\color{blue}{a2 \cdot \left(a2 \cdot \cos th\right)}}{\sqrt{2}} \]
  6. Simplified59.5%

    \[\leadsto \frac{\color{blue}{a2 \cdot \left(a2 \cdot \cos th\right)}}{\sqrt{2}} \]
  7. Step-by-step derivation
    1. div-inv59.5%

      \[\leadsto \color{blue}{\left(a2 \cdot \left(a2 \cdot \cos th\right)\right) \cdot \frac{1}{\sqrt{2}}} \]
    2. pow1/259.5%

      \[\leadsto \left(a2 \cdot \left(a2 \cdot \cos th\right)\right) \cdot \frac{1}{\color{blue}{{2}^{0.5}}} \]
    3. pow-flip59.5%

      \[\leadsto \left(a2 \cdot \left(a2 \cdot \cos th\right)\right) \cdot \color{blue}{{2}^{\left(-0.5\right)}} \]
    4. metadata-eval59.5%

      \[\leadsto \left(a2 \cdot \left(a2 \cdot \cos th\right)\right) \cdot {2}^{\color{blue}{-0.5}} \]
    5. *-commutative59.5%

      \[\leadsto \color{blue}{\left(\left(a2 \cdot \cos th\right) \cdot a2\right)} \cdot {2}^{-0.5} \]
    6. associate-*l*59.5%

      \[\leadsto \color{blue}{\left(a2 \cdot \cos th\right) \cdot \left(a2 \cdot {2}^{-0.5}\right)} \]
    7. add-sqr-sqrt59.2%

      \[\leadsto \left(a2 \cdot \cos th\right) \cdot \left(a2 \cdot \color{blue}{\left(\sqrt{{2}^{-0.5}} \cdot \sqrt{{2}^{-0.5}}\right)}\right) \]
    8. sqrt-unprod59.5%

      \[\leadsto \left(a2 \cdot \cos th\right) \cdot \left(a2 \cdot \color{blue}{\sqrt{{2}^{-0.5} \cdot {2}^{-0.5}}}\right) \]
    9. pow-prod-up59.5%

      \[\leadsto \left(a2 \cdot \cos th\right) \cdot \left(a2 \cdot \sqrt{\color{blue}{{2}^{\left(-0.5 + -0.5\right)}}}\right) \]
    10. metadata-eval59.5%

      \[\leadsto \left(a2 \cdot \cos th\right) \cdot \left(a2 \cdot \sqrt{{2}^{\color{blue}{-1}}}\right) \]
    11. metadata-eval59.5%

      \[\leadsto \left(a2 \cdot \cos th\right) \cdot \left(a2 \cdot \sqrt{\color{blue}{0.5}}\right) \]
  8. Applied egg-rr59.5%

    \[\leadsto \color{blue}{\left(a2 \cdot \cos th\right) \cdot \left(a2 \cdot \sqrt{0.5}\right)} \]
  9. Final simplification59.5%

    \[\leadsto \left(a2 \cdot \cos th\right) \cdot \left(a2 \cdot \sqrt{0.5}\right) \]

Alternative 2: 99.1% accurate, 2.0× speedup?

\[\begin{array}{l} a1 = |a1|\\ a2 = |a2|\\ [a1, a2] = \mathsf{sort}([a1, a2])\\ \\ a2 \cdot \left(a2 \cdot \frac{\cos th}{\sqrt{2}}\right) \end{array} \]
NOTE: a1 should be positive before calling this function
NOTE: a2 should be positive before calling this function
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
(FPCore (a1 a2 th) :precision binary64 (* a2 (* a2 (/ (cos th) (sqrt 2.0)))))
a1 = abs(a1);
a2 = abs(a2);
assert(a1 < a2);
double code(double a1, double a2, double th) {
	return a2 * (a2 * (cos(th) / sqrt(2.0)));
}
NOTE: a1 should be positive before calling this function
NOTE: a2 should be positive before calling this function
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
real(8) function code(a1, a2, th)
    real(8), intent (in) :: a1
    real(8), intent (in) :: a2
    real(8), intent (in) :: th
    code = a2 * (a2 * (cos(th) / sqrt(2.0d0)))
end function
a1 = Math.abs(a1);
a2 = Math.abs(a2);
assert a1 < a2;
public static double code(double a1, double a2, double th) {
	return a2 * (a2 * (Math.cos(th) / Math.sqrt(2.0)));
}
a1 = abs(a1)
a2 = abs(a2)
[a1, a2] = sort([a1, a2])
def code(a1, a2, th):
	return a2 * (a2 * (math.cos(th) / math.sqrt(2.0)))
a1 = abs(a1)
a2 = abs(a2)
a1, a2 = sort([a1, a2])
function code(a1, a2, th)
	return Float64(a2 * Float64(a2 * Float64(cos(th) / sqrt(2.0))))
end
a1 = abs(a1)
a2 = abs(a2)
a1, a2 = num2cell(sort([a1, a2])){:}
function tmp = code(a1, a2, th)
	tmp = a2 * (a2 * (cos(th) / sqrt(2.0)));
end
NOTE: a1 should be positive before calling this function
NOTE: a2 should be positive before calling this function
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
code[a1_, a2_, th_] := N[(a2 * N[(a2 * N[(N[Cos[th], $MachinePrecision] / N[Sqrt[2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
a1 = |a1|\\
a2 = |a2|\\
[a1, a2] = \mathsf{sort}([a1, a2])\\
\\
a2 \cdot \left(a2 \cdot \frac{\cos th}{\sqrt{2}}\right)
\end{array}
Derivation
  1. Initial program 98.9%

    \[\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1\right) + \frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2\right) \]
  2. Step-by-step derivation
    1. +-commutative98.9%

      \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2\right) + \frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1\right)} \]
    2. distribute-lft-out98.9%

      \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2 + a1 \cdot a1\right)} \]
  3. Simplified98.9%

    \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2 + a1 \cdot a1\right)} \]
  4. Taylor expanded in a2 around inf 59.2%

    \[\leadsto \color{blue}{\frac{{a2}^{2} \cdot \cos th}{\sqrt{2}}} \]
  5. Step-by-step derivation
    1. unpow259.2%

      \[\leadsto \frac{\color{blue}{\left(a2 \cdot a2\right)} \cdot \cos th}{\sqrt{2}} \]
    2. associate-*r/59.1%

      \[\leadsto \color{blue}{\left(a2 \cdot a2\right) \cdot \frac{\cos th}{\sqrt{2}}} \]
    3. associate-*r*59.5%

      \[\leadsto \color{blue}{a2 \cdot \left(a2 \cdot \frac{\cos th}{\sqrt{2}}\right)} \]
  6. Simplified59.5%

    \[\leadsto \color{blue}{a2 \cdot \left(a2 \cdot \frac{\cos th}{\sqrt{2}}\right)} \]
  7. Final simplification59.5%

    \[\leadsto a2 \cdot \left(a2 \cdot \frac{\cos th}{\sqrt{2}}\right) \]

Alternative 3: 64.0% accurate, 3.6× speedup?

\[\begin{array}{l} a1 = |a1|\\ a2 = |a2|\\ [a1, a2] = \mathsf{sort}([a1, a2])\\ \\ \begin{array}{l} \mathbf{if}\;th \leq 2.35 \cdot 10^{+72}:\\ \;\;\;\;\left(a2 \cdot \sqrt{0.5}\right) \cdot \left(a2 + -0.5 \cdot \left(a2 \cdot \left(th \cdot th\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;a2 \cdot \frac{a2}{\sqrt{2}}\\ \end{array} \end{array} \]
NOTE: a1 should be positive before calling this function
NOTE: a2 should be positive before calling this function
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
(FPCore (a1 a2 th)
 :precision binary64
 (if (<= th 2.35e+72)
   (* (* a2 (sqrt 0.5)) (+ a2 (* -0.5 (* a2 (* th th)))))
   (* a2 (/ a2 (sqrt 2.0)))))
a1 = abs(a1);
a2 = abs(a2);
assert(a1 < a2);
double code(double a1, double a2, double th) {
	double tmp;
	if (th <= 2.35e+72) {
		tmp = (a2 * sqrt(0.5)) * (a2 + (-0.5 * (a2 * (th * th))));
	} else {
		tmp = a2 * (a2 / sqrt(2.0));
	}
	return tmp;
}
NOTE: a1 should be positive before calling this function
NOTE: a2 should be positive before calling this function
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
real(8) function code(a1, a2, th)
    real(8), intent (in) :: a1
    real(8), intent (in) :: a2
    real(8), intent (in) :: th
    real(8) :: tmp
    if (th <= 2.35d+72) then
        tmp = (a2 * sqrt(0.5d0)) * (a2 + ((-0.5d0) * (a2 * (th * th))))
    else
        tmp = a2 * (a2 / sqrt(2.0d0))
    end if
    code = tmp
end function
a1 = Math.abs(a1);
a2 = Math.abs(a2);
assert a1 < a2;
public static double code(double a1, double a2, double th) {
	double tmp;
	if (th <= 2.35e+72) {
		tmp = (a2 * Math.sqrt(0.5)) * (a2 + (-0.5 * (a2 * (th * th))));
	} else {
		tmp = a2 * (a2 / Math.sqrt(2.0));
	}
	return tmp;
}
a1 = abs(a1)
a2 = abs(a2)
[a1, a2] = sort([a1, a2])
def code(a1, a2, th):
	tmp = 0
	if th <= 2.35e+72:
		tmp = (a2 * math.sqrt(0.5)) * (a2 + (-0.5 * (a2 * (th * th))))
	else:
		tmp = a2 * (a2 / math.sqrt(2.0))
	return tmp
a1 = abs(a1)
a2 = abs(a2)
a1, a2 = sort([a1, a2])
function code(a1, a2, th)
	tmp = 0.0
	if (th <= 2.35e+72)
		tmp = Float64(Float64(a2 * sqrt(0.5)) * Float64(a2 + Float64(-0.5 * Float64(a2 * Float64(th * th)))));
	else
		tmp = Float64(a2 * Float64(a2 / sqrt(2.0)));
	end
	return tmp
end
a1 = abs(a1)
a2 = abs(a2)
a1, a2 = num2cell(sort([a1, a2])){:}
function tmp_2 = code(a1, a2, th)
	tmp = 0.0;
	if (th <= 2.35e+72)
		tmp = (a2 * sqrt(0.5)) * (a2 + (-0.5 * (a2 * (th * th))));
	else
		tmp = a2 * (a2 / sqrt(2.0));
	end
	tmp_2 = tmp;
end
NOTE: a1 should be positive before calling this function
NOTE: a2 should be positive before calling this function
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
code[a1_, a2_, th_] := If[LessEqual[th, 2.35e+72], N[(N[(a2 * N[Sqrt[0.5], $MachinePrecision]), $MachinePrecision] * N[(a2 + N[(-0.5 * N[(a2 * N[(th * th), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(a2 * N[(a2 / N[Sqrt[2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
a1 = |a1|\\
a2 = |a2|\\
[a1, a2] = \mathsf{sort}([a1, a2])\\
\\
\begin{array}{l}
\mathbf{if}\;th \leq 2.35 \cdot 10^{+72}:\\
\;\;\;\;\left(a2 \cdot \sqrt{0.5}\right) \cdot \left(a2 + -0.5 \cdot \left(a2 \cdot \left(th \cdot th\right)\right)\right)\\

\mathbf{else}:\\
\;\;\;\;a2 \cdot \frac{a2}{\sqrt{2}}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if th < 2.35000000000000017e72

    1. Initial program 99.2%

      \[\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1\right) + \frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2\right) \]
    2. Step-by-step derivation
      1. distribute-lft-out99.2%

        \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)} \]
      2. cos-neg99.2%

        \[\leadsto \frac{\color{blue}{\cos \left(-th\right)}}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
      3. associate-*l/99.3%

        \[\leadsto \color{blue}{\frac{\cos \left(-th\right) \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)}{\sqrt{2}}} \]
      4. cos-neg99.3%

        \[\leadsto \frac{\color{blue}{\cos th} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)}{\sqrt{2}} \]
      5. fma-def99.3%

        \[\leadsto \frac{\cos th \cdot \color{blue}{\mathsf{fma}\left(a1, a1, a2 \cdot a2\right)}}{\sqrt{2}} \]
    3. Simplified99.3%

      \[\leadsto \color{blue}{\frac{\cos th \cdot \mathsf{fma}\left(a1, a1, a2 \cdot a2\right)}{\sqrt{2}}} \]
    4. Taylor expanded in a1 around 0 58.5%

      \[\leadsto \frac{\color{blue}{{a2}^{2} \cdot \cos th}}{\sqrt{2}} \]
    5. Step-by-step derivation
      1. unpow258.5%

        \[\leadsto \frac{\color{blue}{\left(a2 \cdot a2\right)} \cdot \cos th}{\sqrt{2}} \]
      2. associate-*l*58.9%

        \[\leadsto \frac{\color{blue}{a2 \cdot \left(a2 \cdot \cos th\right)}}{\sqrt{2}} \]
    6. Simplified58.9%

      \[\leadsto \frac{\color{blue}{a2 \cdot \left(a2 \cdot \cos th\right)}}{\sqrt{2}} \]
    7. Step-by-step derivation
      1. div-inv58.9%

        \[\leadsto \color{blue}{\left(a2 \cdot \left(a2 \cdot \cos th\right)\right) \cdot \frac{1}{\sqrt{2}}} \]
      2. pow1/258.9%

        \[\leadsto \left(a2 \cdot \left(a2 \cdot \cos th\right)\right) \cdot \frac{1}{\color{blue}{{2}^{0.5}}} \]
      3. pow-flip58.9%

        \[\leadsto \left(a2 \cdot \left(a2 \cdot \cos th\right)\right) \cdot \color{blue}{{2}^{\left(-0.5\right)}} \]
      4. metadata-eval58.9%

        \[\leadsto \left(a2 \cdot \left(a2 \cdot \cos th\right)\right) \cdot {2}^{\color{blue}{-0.5}} \]
      5. *-commutative58.9%

        \[\leadsto \color{blue}{\left(\left(a2 \cdot \cos th\right) \cdot a2\right)} \cdot {2}^{-0.5} \]
      6. associate-*l*58.9%

        \[\leadsto \color{blue}{\left(a2 \cdot \cos th\right) \cdot \left(a2 \cdot {2}^{-0.5}\right)} \]
      7. add-sqr-sqrt58.6%

        \[\leadsto \left(a2 \cdot \cos th\right) \cdot \left(a2 \cdot \color{blue}{\left(\sqrt{{2}^{-0.5}} \cdot \sqrt{{2}^{-0.5}}\right)}\right) \]
      8. sqrt-unprod58.9%

        \[\leadsto \left(a2 \cdot \cos th\right) \cdot \left(a2 \cdot \color{blue}{\sqrt{{2}^{-0.5} \cdot {2}^{-0.5}}}\right) \]
      9. pow-prod-up58.9%

        \[\leadsto \left(a2 \cdot \cos th\right) \cdot \left(a2 \cdot \sqrt{\color{blue}{{2}^{\left(-0.5 + -0.5\right)}}}\right) \]
      10. metadata-eval58.9%

        \[\leadsto \left(a2 \cdot \cos th\right) \cdot \left(a2 \cdot \sqrt{{2}^{\color{blue}{-1}}}\right) \]
      11. metadata-eval58.9%

        \[\leadsto \left(a2 \cdot \cos th\right) \cdot \left(a2 \cdot \sqrt{\color{blue}{0.5}}\right) \]
    8. Applied egg-rr58.9%

      \[\leadsto \color{blue}{\left(a2 \cdot \cos th\right) \cdot \left(a2 \cdot \sqrt{0.5}\right)} \]
    9. Taylor expanded in th around 0 40.5%

      \[\leadsto \color{blue}{\left(a2 + -0.5 \cdot \left(a2 \cdot {th}^{2}\right)\right)} \cdot \left(a2 \cdot \sqrt{0.5}\right) \]
    10. Step-by-step derivation
      1. unpow240.5%

        \[\leadsto \frac{a2 \cdot \left(a2 + -0.5 \cdot \left(a2 \cdot \color{blue}{\left(th \cdot th\right)}\right)\right)}{\sqrt{2}} \]
    11. Simplified40.5%

      \[\leadsto \color{blue}{\left(a2 + -0.5 \cdot \left(a2 \cdot \left(th \cdot th\right)\right)\right)} \cdot \left(a2 \cdot \sqrt{0.5}\right) \]

    if 2.35000000000000017e72 < th

    1. Initial program 97.9%

      \[\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1\right) + \frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2\right) \]
    2. Step-by-step derivation
      1. +-commutative97.9%

        \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2\right) + \frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1\right)} \]
      2. distribute-lft-out97.9%

        \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2 + a1 \cdot a1\right)} \]
    3. Simplified97.9%

      \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2 + a1 \cdot a1\right)} \]
    4. Taylor expanded in a2 around inf 61.7%

      \[\leadsto \color{blue}{\frac{{a2}^{2} \cdot \cos th}{\sqrt{2}}} \]
    5. Step-by-step derivation
      1. unpow261.7%

        \[\leadsto \frac{\color{blue}{\left(a2 \cdot a2\right)} \cdot \cos th}{\sqrt{2}} \]
      2. associate-*r/61.7%

        \[\leadsto \color{blue}{\left(a2 \cdot a2\right) \cdot \frac{\cos th}{\sqrt{2}}} \]
      3. associate-*r*61.8%

        \[\leadsto \color{blue}{a2 \cdot \left(a2 \cdot \frac{\cos th}{\sqrt{2}}\right)} \]
    6. Simplified61.8%

      \[\leadsto \color{blue}{a2 \cdot \left(a2 \cdot \frac{\cos th}{\sqrt{2}}\right)} \]
    7. Taylor expanded in th around 0 25.2%

      \[\leadsto a2 \cdot \color{blue}{\frac{a2}{\sqrt{2}}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification37.3%

    \[\leadsto \begin{array}{l} \mathbf{if}\;th \leq 2.35 \cdot 10^{+72}:\\ \;\;\;\;\left(a2 \cdot \sqrt{0.5}\right) \cdot \left(a2 + -0.5 \cdot \left(a2 \cdot \left(th \cdot th\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;a2 \cdot \frac{a2}{\sqrt{2}}\\ \end{array} \]

Alternative 4: 66.2% accurate, 4.0× speedup?

\[\begin{array}{l} a1 = |a1|\\ a2 = |a2|\\ [a1, a2] = \mathsf{sort}([a1, a2])\\ \\ a2 \cdot \left(a2 \cdot \sqrt{0.5}\right) \end{array} \]
NOTE: a1 should be positive before calling this function
NOTE: a2 should be positive before calling this function
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
(FPCore (a1 a2 th) :precision binary64 (* a2 (* a2 (sqrt 0.5))))
a1 = abs(a1);
a2 = abs(a2);
assert(a1 < a2);
double code(double a1, double a2, double th) {
	return a2 * (a2 * sqrt(0.5));
}
NOTE: a1 should be positive before calling this function
NOTE: a2 should be positive before calling this function
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
real(8) function code(a1, a2, th)
    real(8), intent (in) :: a1
    real(8), intent (in) :: a2
    real(8), intent (in) :: th
    code = a2 * (a2 * sqrt(0.5d0))
end function
a1 = Math.abs(a1);
a2 = Math.abs(a2);
assert a1 < a2;
public static double code(double a1, double a2, double th) {
	return a2 * (a2 * Math.sqrt(0.5));
}
a1 = abs(a1)
a2 = abs(a2)
[a1, a2] = sort([a1, a2])
def code(a1, a2, th):
	return a2 * (a2 * math.sqrt(0.5))
a1 = abs(a1)
a2 = abs(a2)
a1, a2 = sort([a1, a2])
function code(a1, a2, th)
	return Float64(a2 * Float64(a2 * sqrt(0.5)))
end
a1 = abs(a1)
a2 = abs(a2)
a1, a2 = num2cell(sort([a1, a2])){:}
function tmp = code(a1, a2, th)
	tmp = a2 * (a2 * sqrt(0.5));
end
NOTE: a1 should be positive before calling this function
NOTE: a2 should be positive before calling this function
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
code[a1_, a2_, th_] := N[(a2 * N[(a2 * N[Sqrt[0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
a1 = |a1|\\
a2 = |a2|\\
[a1, a2] = \mathsf{sort}([a1, a2])\\
\\
a2 \cdot \left(a2 \cdot \sqrt{0.5}\right)
\end{array}
Derivation
  1. Initial program 98.9%

    \[\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1\right) + \frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2\right) \]
  2. Step-by-step derivation
    1. distribute-lft-out98.9%

      \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)} \]
    2. cos-neg98.9%

      \[\leadsto \frac{\color{blue}{\cos \left(-th\right)}}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
    3. associate-*l/99.0%

      \[\leadsto \color{blue}{\frac{\cos \left(-th\right) \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)}{\sqrt{2}}} \]
    4. cos-neg99.0%

      \[\leadsto \frac{\color{blue}{\cos th} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)}{\sqrt{2}} \]
    5. fma-def99.0%

      \[\leadsto \frac{\cos th \cdot \color{blue}{\mathsf{fma}\left(a1, a1, a2 \cdot a2\right)}}{\sqrt{2}} \]
  3. Simplified99.0%

    \[\leadsto \color{blue}{\frac{\cos th \cdot \mathsf{fma}\left(a1, a1, a2 \cdot a2\right)}{\sqrt{2}}} \]
  4. Taylor expanded in a1 around 0 59.2%

    \[\leadsto \frac{\color{blue}{{a2}^{2} \cdot \cos th}}{\sqrt{2}} \]
  5. Step-by-step derivation
    1. unpow259.2%

      \[\leadsto \frac{\color{blue}{\left(a2 \cdot a2\right)} \cdot \cos th}{\sqrt{2}} \]
    2. associate-*l*59.5%

      \[\leadsto \frac{\color{blue}{a2 \cdot \left(a2 \cdot \cos th\right)}}{\sqrt{2}} \]
  6. Simplified59.5%

    \[\leadsto \frac{\color{blue}{a2 \cdot \left(a2 \cdot \cos th\right)}}{\sqrt{2}} \]
  7. Step-by-step derivation
    1. div-inv59.5%

      \[\leadsto \color{blue}{\left(a2 \cdot \left(a2 \cdot \cos th\right)\right) \cdot \frac{1}{\sqrt{2}}} \]
    2. pow1/259.5%

      \[\leadsto \left(a2 \cdot \left(a2 \cdot \cos th\right)\right) \cdot \frac{1}{\color{blue}{{2}^{0.5}}} \]
    3. pow-flip59.5%

      \[\leadsto \left(a2 \cdot \left(a2 \cdot \cos th\right)\right) \cdot \color{blue}{{2}^{\left(-0.5\right)}} \]
    4. metadata-eval59.5%

      \[\leadsto \left(a2 \cdot \left(a2 \cdot \cos th\right)\right) \cdot {2}^{\color{blue}{-0.5}} \]
    5. *-commutative59.5%

      \[\leadsto \color{blue}{\left(\left(a2 \cdot \cos th\right) \cdot a2\right)} \cdot {2}^{-0.5} \]
    6. associate-*l*59.5%

      \[\leadsto \color{blue}{\left(a2 \cdot \cos th\right) \cdot \left(a2 \cdot {2}^{-0.5}\right)} \]
    7. add-sqr-sqrt59.2%

      \[\leadsto \left(a2 \cdot \cos th\right) \cdot \left(a2 \cdot \color{blue}{\left(\sqrt{{2}^{-0.5}} \cdot \sqrt{{2}^{-0.5}}\right)}\right) \]
    8. sqrt-unprod59.5%

      \[\leadsto \left(a2 \cdot \cos th\right) \cdot \left(a2 \cdot \color{blue}{\sqrt{{2}^{-0.5} \cdot {2}^{-0.5}}}\right) \]
    9. pow-prod-up59.5%

      \[\leadsto \left(a2 \cdot \cos th\right) \cdot \left(a2 \cdot \sqrt{\color{blue}{{2}^{\left(-0.5 + -0.5\right)}}}\right) \]
    10. metadata-eval59.5%

      \[\leadsto \left(a2 \cdot \cos th\right) \cdot \left(a2 \cdot \sqrt{{2}^{\color{blue}{-1}}}\right) \]
    11. metadata-eval59.5%

      \[\leadsto \left(a2 \cdot \cos th\right) \cdot \left(a2 \cdot \sqrt{\color{blue}{0.5}}\right) \]
  8. Applied egg-rr59.5%

    \[\leadsto \color{blue}{\left(a2 \cdot \cos th\right) \cdot \left(a2 \cdot \sqrt{0.5}\right)} \]
  9. Taylor expanded in th around 0 39.6%

    \[\leadsto \color{blue}{{a2}^{2} \cdot \sqrt{0.5}} \]
  10. Step-by-step derivation
    1. unpow239.6%

      \[\leadsto \color{blue}{\left(a2 \cdot a2\right)} \cdot \sqrt{0.5} \]
    2. associate-*l*39.6%

      \[\leadsto \color{blue}{a2 \cdot \left(a2 \cdot \sqrt{0.5}\right)} \]
  11. Simplified39.6%

    \[\leadsto \color{blue}{a2 \cdot \left(a2 \cdot \sqrt{0.5}\right)} \]
  12. Final simplification39.6%

    \[\leadsto a2 \cdot \left(a2 \cdot \sqrt{0.5}\right) \]

Alternative 5: 25.7% accurate, 41.2× speedup?

\[\begin{array}{l} a1 = |a1|\\ a2 = |a2|\\ [a1, a2] = \mathsf{sort}([a1, a2])\\ \\ \begin{array}{l} \mathbf{if}\;a2 \cdot a2 \leq 5 \cdot 10^{-233}:\\ \;\;\;\;\left(a1 \cdot a1\right) \cdot \left(-64\right)\\ \mathbf{else}:\\ \;\;\;\;a2 \cdot \left(a2 \cdot th\right)\\ \end{array} \end{array} \]
NOTE: a1 should be positive before calling this function
NOTE: a2 should be positive before calling this function
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
(FPCore (a1 a2 th)
 :precision binary64
 (if (<= (* a2 a2) 5e-233) (* (* a1 a1) (- 64.0)) (* a2 (* a2 th))))
a1 = abs(a1);
a2 = abs(a2);
assert(a1 < a2);
double code(double a1, double a2, double th) {
	double tmp;
	if ((a2 * a2) <= 5e-233) {
		tmp = (a1 * a1) * -64.0;
	} else {
		tmp = a2 * (a2 * th);
	}
	return tmp;
}
NOTE: a1 should be positive before calling this function
NOTE: a2 should be positive before calling this function
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
real(8) function code(a1, a2, th)
    real(8), intent (in) :: a1
    real(8), intent (in) :: a2
    real(8), intent (in) :: th
    real(8) :: tmp
    if ((a2 * a2) <= 5d-233) then
        tmp = (a1 * a1) * -64.0d0
    else
        tmp = a2 * (a2 * th)
    end if
    code = tmp
end function
a1 = Math.abs(a1);
a2 = Math.abs(a2);
assert a1 < a2;
public static double code(double a1, double a2, double th) {
	double tmp;
	if ((a2 * a2) <= 5e-233) {
		tmp = (a1 * a1) * -64.0;
	} else {
		tmp = a2 * (a2 * th);
	}
	return tmp;
}
a1 = abs(a1)
a2 = abs(a2)
[a1, a2] = sort([a1, a2])
def code(a1, a2, th):
	tmp = 0
	if (a2 * a2) <= 5e-233:
		tmp = (a1 * a1) * -64.0
	else:
		tmp = a2 * (a2 * th)
	return tmp
a1 = abs(a1)
a2 = abs(a2)
a1, a2 = sort([a1, a2])
function code(a1, a2, th)
	tmp = 0.0
	if (Float64(a2 * a2) <= 5e-233)
		tmp = Float64(Float64(a1 * a1) * Float64(-64.0));
	else
		tmp = Float64(a2 * Float64(a2 * th));
	end
	return tmp
end
a1 = abs(a1)
a2 = abs(a2)
a1, a2 = num2cell(sort([a1, a2])){:}
function tmp_2 = code(a1, a2, th)
	tmp = 0.0;
	if ((a2 * a2) <= 5e-233)
		tmp = (a1 * a1) * -64.0;
	else
		tmp = a2 * (a2 * th);
	end
	tmp_2 = tmp;
end
NOTE: a1 should be positive before calling this function
NOTE: a2 should be positive before calling this function
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
code[a1_, a2_, th_] := If[LessEqual[N[(a2 * a2), $MachinePrecision], 5e-233], N[(N[(a1 * a1), $MachinePrecision] * (-64.0)), $MachinePrecision], N[(a2 * N[(a2 * th), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
a1 = |a1|\\
a2 = |a2|\\
[a1, a2] = \mathsf{sort}([a1, a2])\\
\\
\begin{array}{l}
\mathbf{if}\;a2 \cdot a2 \leq 5 \cdot 10^{-233}:\\
\;\;\;\;\left(a1 \cdot a1\right) \cdot \left(-64\right)\\

\mathbf{else}:\\
\;\;\;\;a2 \cdot \left(a2 \cdot th\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 a2 a2) < 5.00000000000000012e-233

    1. Initial program 99.4%

      \[\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1\right) + \frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2\right) \]
    2. Step-by-step derivation
      1. +-commutative99.4%

        \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2\right) + \frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1\right)} \]
      2. distribute-lft-out99.4%

        \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2 + a1 \cdot a1\right)} \]
    3. Simplified99.4%

      \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2 + a1 \cdot a1\right)} \]
    4. Taylor expanded in th around 0 66.7%

      \[\leadsto \color{blue}{\frac{{a1}^{2} + {a2}^{2}}{\sqrt{2}}} \]
    5. Step-by-step derivation
      1. unpow266.7%

        \[\leadsto \frac{\color{blue}{a1 \cdot a1} + {a2}^{2}}{\sqrt{2}} \]
      2. unpow266.7%

        \[\leadsto \frac{a1 \cdot a1 + \color{blue}{a2 \cdot a2}}{\sqrt{2}} \]
      3. +-commutative66.7%

        \[\leadsto \frac{\color{blue}{a2 \cdot a2 + a1 \cdot a1}}{\sqrt{2}} \]
    6. Simplified66.7%

      \[\leadsto \color{blue}{\frac{a2 \cdot a2 + a1 \cdot a1}{\sqrt{2}}} \]
    7. Taylor expanded in a2 around 0 64.3%

      \[\leadsto \color{blue}{\frac{{a1}^{2}}{\sqrt{2}}} \]
    8. Step-by-step derivation
      1. unpow264.3%

        \[\leadsto \frac{\color{blue}{a1 \cdot a1}}{\sqrt{2}} \]
    9. Simplified64.3%

      \[\leadsto \color{blue}{\frac{a1 \cdot a1}{\sqrt{2}}} \]
    10. Step-by-step derivation
      1. frac-2neg64.3%

        \[\leadsto \color{blue}{\frac{-a1 \cdot a1}{-\sqrt{2}}} \]
      2. div-inv64.2%

        \[\leadsto \color{blue}{\left(-a1 \cdot a1\right) \cdot \frac{1}{-\sqrt{2}}} \]
    11. Applied egg-rr64.2%

      \[\leadsto \color{blue}{\left(-a1 \cdot a1\right) \cdot \frac{1}{-\sqrt{2}}} \]
    12. Applied egg-rr25.1%

      \[\leadsto \left(-a1 \cdot a1\right) \cdot \color{blue}{64} \]

    if 5.00000000000000012e-233 < (*.f64 a2 a2)

    1. Initial program 98.7%

      \[\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1\right) + \frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2\right) \]
    2. Step-by-step derivation
      1. distribute-lft-out98.7%

        \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)} \]
      2. cos-neg98.7%

        \[\leadsto \frac{\color{blue}{\cos \left(-th\right)}}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
      3. associate-*l/98.7%

        \[\leadsto \color{blue}{\frac{\cos \left(-th\right) \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)}{\sqrt{2}}} \]
      4. cos-neg98.7%

        \[\leadsto \frac{\color{blue}{\cos th} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)}{\sqrt{2}} \]
      5. fma-def98.7%

        \[\leadsto \frac{\cos th \cdot \color{blue}{\mathsf{fma}\left(a1, a1, a2 \cdot a2\right)}}{\sqrt{2}} \]
    3. Simplified98.7%

      \[\leadsto \color{blue}{\frac{\cos th \cdot \mathsf{fma}\left(a1, a1, a2 \cdot a2\right)}{\sqrt{2}}} \]
    4. Taylor expanded in a1 around 0 75.7%

      \[\leadsto \frac{\color{blue}{{a2}^{2} \cdot \cos th}}{\sqrt{2}} \]
    5. Step-by-step derivation
      1. unpow275.7%

        \[\leadsto \frac{\color{blue}{\left(a2 \cdot a2\right)} \cdot \cos th}{\sqrt{2}} \]
      2. associate-*l*76.2%

        \[\leadsto \frac{\color{blue}{a2 \cdot \left(a2 \cdot \cos th\right)}}{\sqrt{2}} \]
    6. Simplified76.2%

      \[\leadsto \frac{\color{blue}{a2 \cdot \left(a2 \cdot \cos th\right)}}{\sqrt{2}} \]
    7. Taylor expanded in th around 0 45.8%

      \[\leadsto \frac{a2 \cdot \color{blue}{\left(a2 + -0.5 \cdot \left(a2 \cdot {th}^{2}\right)\right)}}{\sqrt{2}} \]
    8. Step-by-step derivation
      1. unpow245.8%

        \[\leadsto \frac{a2 \cdot \left(a2 + -0.5 \cdot \left(a2 \cdot \color{blue}{\left(th \cdot th\right)}\right)\right)}{\sqrt{2}} \]
    9. Simplified45.8%

      \[\leadsto \frac{a2 \cdot \color{blue}{\left(a2 + -0.5 \cdot \left(a2 \cdot \left(th \cdot th\right)\right)\right)}}{\sqrt{2}} \]
    10. Taylor expanded in th around inf 12.7%

      \[\leadsto \frac{\color{blue}{-0.5 \cdot \left({a2}^{2} \cdot {th}^{2}\right)}}{\sqrt{2}} \]
    11. Step-by-step derivation
      1. unpow212.7%

        \[\leadsto \frac{-0.5 \cdot \left(\color{blue}{\left(a2 \cdot a2\right)} \cdot {th}^{2}\right)}{\sqrt{2}} \]
      2. unpow212.7%

        \[\leadsto \frac{-0.5 \cdot \left(\left(a2 \cdot a2\right) \cdot \color{blue}{\left(th \cdot th\right)}\right)}{\sqrt{2}} \]
      3. associate-*l*12.8%

        \[\leadsto \frac{-0.5 \cdot \color{blue}{\left(a2 \cdot \left(a2 \cdot \left(th \cdot th\right)\right)\right)}}{\sqrt{2}} \]
      4. associate-*l*12.8%

        \[\leadsto \frac{\color{blue}{\left(-0.5 \cdot a2\right) \cdot \left(a2 \cdot \left(th \cdot th\right)\right)}}{\sqrt{2}} \]
      5. *-commutative12.8%

        \[\leadsto \frac{\color{blue}{\left(a2 \cdot \left(th \cdot th\right)\right) \cdot \left(-0.5 \cdot a2\right)}}{\sqrt{2}} \]
      6. associate-*l*12.8%

        \[\leadsto \frac{\color{blue}{a2 \cdot \left(\left(th \cdot th\right) \cdot \left(-0.5 \cdot a2\right)\right)}}{\sqrt{2}} \]
      7. *-commutative12.8%

        \[\leadsto \frac{a2 \cdot \left(\left(th \cdot th\right) \cdot \color{blue}{\left(a2 \cdot -0.5\right)}\right)}{\sqrt{2}} \]
    12. Simplified12.8%

      \[\leadsto \frac{\color{blue}{a2 \cdot \left(\left(th \cdot th\right) \cdot \left(a2 \cdot -0.5\right)\right)}}{\sqrt{2}} \]
    13. Applied egg-rr20.9%

      \[\leadsto \color{blue}{\left(th \cdot a2\right) \cdot a2} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification22.3%

    \[\leadsto \begin{array}{l} \mathbf{if}\;a2 \cdot a2 \leq 5 \cdot 10^{-233}:\\ \;\;\;\;\left(a1 \cdot a1\right) \cdot \left(-64\right)\\ \mathbf{else}:\\ \;\;\;\;a2 \cdot \left(a2 \cdot th\right)\\ \end{array} \]

Alternative 6: 25.8% accurate, 51.5× speedup?

\[\begin{array}{l} a1 = |a1|\\ a2 = |a2|\\ [a1, a2] = \mathsf{sort}([a1, a2])\\ \\ \begin{array}{l} \mathbf{if}\;a2 \leq 2.1 \cdot 10^{+29}:\\ \;\;\;\;\left(a1 \cdot a1\right) \cdot \left(--2\right)\\ \mathbf{else}:\\ \;\;\;\;a2 \cdot \left(a2 \cdot th\right)\\ \end{array} \end{array} \]
NOTE: a1 should be positive before calling this function
NOTE: a2 should be positive before calling this function
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
(FPCore (a1 a2 th)
 :precision binary64
 (if (<= a2 2.1e+29) (* (* a1 a1) (- -2.0)) (* a2 (* a2 th))))
a1 = abs(a1);
a2 = abs(a2);
assert(a1 < a2);
double code(double a1, double a2, double th) {
	double tmp;
	if (a2 <= 2.1e+29) {
		tmp = (a1 * a1) * -(-2.0);
	} else {
		tmp = a2 * (a2 * th);
	}
	return tmp;
}
NOTE: a1 should be positive before calling this function
NOTE: a2 should be positive before calling this function
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
real(8) function code(a1, a2, th)
    real(8), intent (in) :: a1
    real(8), intent (in) :: a2
    real(8), intent (in) :: th
    real(8) :: tmp
    if (a2 <= 2.1d+29) then
        tmp = (a1 * a1) * -(-2.0d0)
    else
        tmp = a2 * (a2 * th)
    end if
    code = tmp
end function
a1 = Math.abs(a1);
a2 = Math.abs(a2);
assert a1 < a2;
public static double code(double a1, double a2, double th) {
	double tmp;
	if (a2 <= 2.1e+29) {
		tmp = (a1 * a1) * -(-2.0);
	} else {
		tmp = a2 * (a2 * th);
	}
	return tmp;
}
a1 = abs(a1)
a2 = abs(a2)
[a1, a2] = sort([a1, a2])
def code(a1, a2, th):
	tmp = 0
	if a2 <= 2.1e+29:
		tmp = (a1 * a1) * -(-2.0)
	else:
		tmp = a2 * (a2 * th)
	return tmp
a1 = abs(a1)
a2 = abs(a2)
a1, a2 = sort([a1, a2])
function code(a1, a2, th)
	tmp = 0.0
	if (a2 <= 2.1e+29)
		tmp = Float64(Float64(a1 * a1) * Float64(-(-2.0)));
	else
		tmp = Float64(a2 * Float64(a2 * th));
	end
	return tmp
end
a1 = abs(a1)
a2 = abs(a2)
a1, a2 = num2cell(sort([a1, a2])){:}
function tmp_2 = code(a1, a2, th)
	tmp = 0.0;
	if (a2 <= 2.1e+29)
		tmp = (a1 * a1) * -(-2.0);
	else
		tmp = a2 * (a2 * th);
	end
	tmp_2 = tmp;
end
NOTE: a1 should be positive before calling this function
NOTE: a2 should be positive before calling this function
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
code[a1_, a2_, th_] := If[LessEqual[a2, 2.1e+29], N[(N[(a1 * a1), $MachinePrecision] * (--2.0)), $MachinePrecision], N[(a2 * N[(a2 * th), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
a1 = |a1|\\
a2 = |a2|\\
[a1, a2] = \mathsf{sort}([a1, a2])\\
\\
\begin{array}{l}
\mathbf{if}\;a2 \leq 2.1 \cdot 10^{+29}:\\
\;\;\;\;\left(a1 \cdot a1\right) \cdot \left(--2\right)\\

\mathbf{else}:\\
\;\;\;\;a2 \cdot \left(a2 \cdot th\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if a2 < 2.1000000000000002e29

    1. Initial program 98.7%

      \[\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1\right) + \frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2\right) \]
    2. Step-by-step derivation
      1. +-commutative98.7%

        \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2\right) + \frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1\right)} \]
      2. distribute-lft-out98.7%

        \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2 + a1 \cdot a1\right)} \]
    3. Simplified98.7%

      \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2 + a1 \cdot a1\right)} \]
    4. Taylor expanded in th around 0 63.9%

      \[\leadsto \color{blue}{\frac{{a1}^{2} + {a2}^{2}}{\sqrt{2}}} \]
    5. Step-by-step derivation
      1. unpow263.9%

        \[\leadsto \frac{\color{blue}{a1 \cdot a1} + {a2}^{2}}{\sqrt{2}} \]
      2. unpow263.9%

        \[\leadsto \frac{a1 \cdot a1 + \color{blue}{a2 \cdot a2}}{\sqrt{2}} \]
      3. +-commutative63.9%

        \[\leadsto \frac{\color{blue}{a2 \cdot a2 + a1 \cdot a1}}{\sqrt{2}} \]
    6. Simplified63.9%

      \[\leadsto \color{blue}{\frac{a2 \cdot a2 + a1 \cdot a1}{\sqrt{2}}} \]
    7. Taylor expanded in a2 around 0 46.0%

      \[\leadsto \color{blue}{\frac{{a1}^{2}}{\sqrt{2}}} \]
    8. Step-by-step derivation
      1. unpow246.0%

        \[\leadsto \frac{\color{blue}{a1 \cdot a1}}{\sqrt{2}} \]
    9. Simplified46.0%

      \[\leadsto \color{blue}{\frac{a1 \cdot a1}{\sqrt{2}}} \]
    10. Step-by-step derivation
      1. frac-2neg46.0%

        \[\leadsto \color{blue}{\frac{-a1 \cdot a1}{-\sqrt{2}}} \]
      2. div-inv45.9%

        \[\leadsto \color{blue}{\left(-a1 \cdot a1\right) \cdot \frac{1}{-\sqrt{2}}} \]
    11. Applied egg-rr45.9%

      \[\leadsto \color{blue}{\left(-a1 \cdot a1\right) \cdot \frac{1}{-\sqrt{2}}} \]
    12. Applied egg-rr33.9%

      \[\leadsto \left(-a1 \cdot a1\right) \cdot \color{blue}{-2} \]

    if 2.1000000000000002e29 < a2

    1. Initial program 99.7%

      \[\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1\right) + \frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2\right) \]
    2. Step-by-step derivation
      1. distribute-lft-out99.7%

        \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)} \]
      2. cos-neg99.7%

        \[\leadsto \frac{\color{blue}{\cos \left(-th\right)}}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
      3. associate-*l/99.8%

        \[\leadsto \color{blue}{\frac{\cos \left(-th\right) \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)}{\sqrt{2}}} \]
      4. cos-neg99.8%

        \[\leadsto \frac{\color{blue}{\cos th} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)}{\sqrt{2}} \]
      5. fma-def99.8%

        \[\leadsto \frac{\cos th \cdot \color{blue}{\mathsf{fma}\left(a1, a1, a2 \cdot a2\right)}}{\sqrt{2}} \]
    3. Simplified99.8%

      \[\leadsto \color{blue}{\frac{\cos th \cdot \mathsf{fma}\left(a1, a1, a2 \cdot a2\right)}{\sqrt{2}}} \]
    4. Taylor expanded in a1 around 0 90.4%

      \[\leadsto \frac{\color{blue}{{a2}^{2} \cdot \cos th}}{\sqrt{2}} \]
    5. Step-by-step derivation
      1. unpow290.4%

        \[\leadsto \frac{\color{blue}{\left(a2 \cdot a2\right)} \cdot \cos th}{\sqrt{2}} \]
      2. associate-*l*90.3%

        \[\leadsto \frac{\color{blue}{a2 \cdot \left(a2 \cdot \cos th\right)}}{\sqrt{2}} \]
    6. Simplified90.3%

      \[\leadsto \frac{\color{blue}{a2 \cdot \left(a2 \cdot \cos th\right)}}{\sqrt{2}} \]
    7. Taylor expanded in th around 0 50.9%

      \[\leadsto \frac{a2 \cdot \color{blue}{\left(a2 + -0.5 \cdot \left(a2 \cdot {th}^{2}\right)\right)}}{\sqrt{2}} \]
    8. Step-by-step derivation
      1. unpow250.9%

        \[\leadsto \frac{a2 \cdot \left(a2 + -0.5 \cdot \left(a2 \cdot \color{blue}{\left(th \cdot th\right)}\right)\right)}{\sqrt{2}} \]
    9. Simplified50.9%

      \[\leadsto \frac{a2 \cdot \color{blue}{\left(a2 + -0.5 \cdot \left(a2 \cdot \left(th \cdot th\right)\right)\right)}}{\sqrt{2}} \]
    10. Taylor expanded in th around inf 10.9%

      \[\leadsto \frac{\color{blue}{-0.5 \cdot \left({a2}^{2} \cdot {th}^{2}\right)}}{\sqrt{2}} \]
    11. Step-by-step derivation
      1. unpow210.9%

        \[\leadsto \frac{-0.5 \cdot \left(\color{blue}{\left(a2 \cdot a2\right)} \cdot {th}^{2}\right)}{\sqrt{2}} \]
      2. unpow210.9%

        \[\leadsto \frac{-0.5 \cdot \left(\left(a2 \cdot a2\right) \cdot \color{blue}{\left(th \cdot th\right)}\right)}{\sqrt{2}} \]
      3. associate-*l*11.1%

        \[\leadsto \frac{-0.5 \cdot \color{blue}{\left(a2 \cdot \left(a2 \cdot \left(th \cdot th\right)\right)\right)}}{\sqrt{2}} \]
      4. associate-*l*11.1%

        \[\leadsto \frac{\color{blue}{\left(-0.5 \cdot a2\right) \cdot \left(a2 \cdot \left(th \cdot th\right)\right)}}{\sqrt{2}} \]
      5. *-commutative11.1%

        \[\leadsto \frac{\color{blue}{\left(a2 \cdot \left(th \cdot th\right)\right) \cdot \left(-0.5 \cdot a2\right)}}{\sqrt{2}} \]
      6. associate-*l*11.1%

        \[\leadsto \frac{\color{blue}{a2 \cdot \left(\left(th \cdot th\right) \cdot \left(-0.5 \cdot a2\right)\right)}}{\sqrt{2}} \]
      7. *-commutative11.1%

        \[\leadsto \frac{a2 \cdot \left(\left(th \cdot th\right) \cdot \color{blue}{\left(a2 \cdot -0.5\right)}\right)}{\sqrt{2}} \]
    12. Simplified11.1%

      \[\leadsto \frac{\color{blue}{a2 \cdot \left(\left(th \cdot th\right) \cdot \left(a2 \cdot -0.5\right)\right)}}{\sqrt{2}} \]
    13. Applied egg-rr30.0%

      \[\leadsto \color{blue}{\left(th \cdot a2\right) \cdot a2} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification33.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;a2 \leq 2.1 \cdot 10^{+29}:\\ \;\;\;\;\left(a1 \cdot a1\right) \cdot \left(--2\right)\\ \mathbf{else}:\\ \;\;\;\;a2 \cdot \left(a2 \cdot th\right)\\ \end{array} \]

Alternative 7: 25.7% accurate, 58.8× speedup?

\[\begin{array}{l} a1 = |a1|\\ a2 = |a2|\\ [a1, a2] = \mathsf{sort}([a1, a2])\\ \\ \begin{array}{l} \mathbf{if}\;a2 \leq 5.1 \cdot 10^{-113}:\\ \;\;\;\;\frac{a1 \cdot a1}{-2}\\ \mathbf{else}:\\ \;\;\;\;a2 \cdot \left(a2 \cdot th\right)\\ \end{array} \end{array} \]
NOTE: a1 should be positive before calling this function
NOTE: a2 should be positive before calling this function
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
(FPCore (a1 a2 th)
 :precision binary64
 (if (<= a2 5.1e-113) (/ (* a1 a1) -2.0) (* a2 (* a2 th))))
a1 = abs(a1);
a2 = abs(a2);
assert(a1 < a2);
double code(double a1, double a2, double th) {
	double tmp;
	if (a2 <= 5.1e-113) {
		tmp = (a1 * a1) / -2.0;
	} else {
		tmp = a2 * (a2 * th);
	}
	return tmp;
}
NOTE: a1 should be positive before calling this function
NOTE: a2 should be positive before calling this function
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
real(8) function code(a1, a2, th)
    real(8), intent (in) :: a1
    real(8), intent (in) :: a2
    real(8), intent (in) :: th
    real(8) :: tmp
    if (a2 <= 5.1d-113) then
        tmp = (a1 * a1) / (-2.0d0)
    else
        tmp = a2 * (a2 * th)
    end if
    code = tmp
end function
a1 = Math.abs(a1);
a2 = Math.abs(a2);
assert a1 < a2;
public static double code(double a1, double a2, double th) {
	double tmp;
	if (a2 <= 5.1e-113) {
		tmp = (a1 * a1) / -2.0;
	} else {
		tmp = a2 * (a2 * th);
	}
	return tmp;
}
a1 = abs(a1)
a2 = abs(a2)
[a1, a2] = sort([a1, a2])
def code(a1, a2, th):
	tmp = 0
	if a2 <= 5.1e-113:
		tmp = (a1 * a1) / -2.0
	else:
		tmp = a2 * (a2 * th)
	return tmp
a1 = abs(a1)
a2 = abs(a2)
a1, a2 = sort([a1, a2])
function code(a1, a2, th)
	tmp = 0.0
	if (a2 <= 5.1e-113)
		tmp = Float64(Float64(a1 * a1) / -2.0);
	else
		tmp = Float64(a2 * Float64(a2 * th));
	end
	return tmp
end
a1 = abs(a1)
a2 = abs(a2)
a1, a2 = num2cell(sort([a1, a2])){:}
function tmp_2 = code(a1, a2, th)
	tmp = 0.0;
	if (a2 <= 5.1e-113)
		tmp = (a1 * a1) / -2.0;
	else
		tmp = a2 * (a2 * th);
	end
	tmp_2 = tmp;
end
NOTE: a1 should be positive before calling this function
NOTE: a2 should be positive before calling this function
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
code[a1_, a2_, th_] := If[LessEqual[a2, 5.1e-113], N[(N[(a1 * a1), $MachinePrecision] / -2.0), $MachinePrecision], N[(a2 * N[(a2 * th), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
a1 = |a1|\\
a2 = |a2|\\
[a1, a2] = \mathsf{sort}([a1, a2])\\
\\
\begin{array}{l}
\mathbf{if}\;a2 \leq 5.1 \cdot 10^{-113}:\\
\;\;\;\;\frac{a1 \cdot a1}{-2}\\

\mathbf{else}:\\
\;\;\;\;a2 \cdot \left(a2 \cdot th\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if a2 < 5.09999999999999979e-113

    1. Initial program 98.6%

      \[\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1\right) + \frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2\right) \]
    2. Step-by-step derivation
      1. +-commutative98.6%

        \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2\right) + \frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1\right)} \]
      2. distribute-lft-out98.6%

        \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2 + a1 \cdot a1\right)} \]
    3. Simplified98.6%

      \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2 + a1 \cdot a1\right)} \]
    4. Taylor expanded in th around 0 66.5%

      \[\leadsto \color{blue}{\frac{{a1}^{2} + {a2}^{2}}{\sqrt{2}}} \]
    5. Step-by-step derivation
      1. unpow266.5%

        \[\leadsto \frac{\color{blue}{a1 \cdot a1} + {a2}^{2}}{\sqrt{2}} \]
      2. unpow266.5%

        \[\leadsto \frac{a1 \cdot a1 + \color{blue}{a2 \cdot a2}}{\sqrt{2}} \]
      3. +-commutative66.5%

        \[\leadsto \frac{\color{blue}{a2 \cdot a2 + a1 \cdot a1}}{\sqrt{2}} \]
    6. Simplified66.5%

      \[\leadsto \color{blue}{\frac{a2 \cdot a2 + a1 \cdot a1}{\sqrt{2}}} \]
    7. Taylor expanded in a2 around 0 49.1%

      \[\leadsto \color{blue}{\frac{{a1}^{2}}{\sqrt{2}}} \]
    8. Step-by-step derivation
      1. unpow249.1%

        \[\leadsto \frac{\color{blue}{a1 \cdot a1}}{\sqrt{2}} \]
    9. Simplified49.1%

      \[\leadsto \color{blue}{\frac{a1 \cdot a1}{\sqrt{2}}} \]
    10. Step-by-step derivation
      1. frac-2neg49.1%

        \[\leadsto \color{blue}{\frac{-a1 \cdot a1}{-\sqrt{2}}} \]
      2. div-inv49.1%

        \[\leadsto \color{blue}{\left(-a1 \cdot a1\right) \cdot \frac{1}{-\sqrt{2}}} \]
    11. Applied egg-rr49.1%

      \[\leadsto \color{blue}{\left(-a1 \cdot a1\right) \cdot \frac{1}{-\sqrt{2}}} \]
    12. Applied egg-rr16.3%

      \[\leadsto \color{blue}{\frac{a1}{\frac{-2}{a1}}} \]
    13. Step-by-step derivation
      1. associate-/l*16.3%

        \[\leadsto \color{blue}{\frac{a1 \cdot a1}{-2}} \]
    14. Simplified16.3%

      \[\leadsto \color{blue}{\frac{a1 \cdot a1}{-2}} \]

    if 5.09999999999999979e-113 < a2

    1. Initial program 99.6%

      \[\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1\right) + \frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2\right) \]
    2. Step-by-step derivation
      1. distribute-lft-out99.6%

        \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)} \]
      2. cos-neg99.6%

        \[\leadsto \frac{\color{blue}{\cos \left(-th\right)}}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
      3. associate-*l/99.6%

        \[\leadsto \color{blue}{\frac{\cos \left(-th\right) \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)}{\sqrt{2}}} \]
      4. cos-neg99.6%

        \[\leadsto \frac{\color{blue}{\cos th} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)}{\sqrt{2}} \]
      5. fma-def99.6%

        \[\leadsto \frac{\cos th \cdot \color{blue}{\mathsf{fma}\left(a1, a1, a2 \cdot a2\right)}}{\sqrt{2}} \]
    3. Simplified99.6%

      \[\leadsto \color{blue}{\frac{\cos th \cdot \mathsf{fma}\left(a1, a1, a2 \cdot a2\right)}{\sqrt{2}}} \]
    4. Taylor expanded in a1 around 0 85.4%

      \[\leadsto \frac{\color{blue}{{a2}^{2} \cdot \cos th}}{\sqrt{2}} \]
    5. Step-by-step derivation
      1. unpow285.4%

        \[\leadsto \frac{\color{blue}{\left(a2 \cdot a2\right)} \cdot \cos th}{\sqrt{2}} \]
      2. associate-*l*85.4%

        \[\leadsto \frac{\color{blue}{a2 \cdot \left(a2 \cdot \cos th\right)}}{\sqrt{2}} \]
    6. Simplified85.4%

      \[\leadsto \frac{\color{blue}{a2 \cdot \left(a2 \cdot \cos th\right)}}{\sqrt{2}} \]
    7. Taylor expanded in th around 0 43.7%

      \[\leadsto \frac{a2 \cdot \color{blue}{\left(a2 + -0.5 \cdot \left(a2 \cdot {th}^{2}\right)\right)}}{\sqrt{2}} \]
    8. Step-by-step derivation
      1. unpow243.7%

        \[\leadsto \frac{a2 \cdot \left(a2 + -0.5 \cdot \left(a2 \cdot \color{blue}{\left(th \cdot th\right)}\right)\right)}{\sqrt{2}} \]
    9. Simplified43.7%

      \[\leadsto \frac{a2 \cdot \color{blue}{\left(a2 + -0.5 \cdot \left(a2 \cdot \left(th \cdot th\right)\right)\right)}}{\sqrt{2}} \]
    10. Taylor expanded in th around inf 8.9%

      \[\leadsto \frac{\color{blue}{-0.5 \cdot \left({a2}^{2} \cdot {th}^{2}\right)}}{\sqrt{2}} \]
    11. Step-by-step derivation
      1. unpow28.9%

        \[\leadsto \frac{-0.5 \cdot \left(\color{blue}{\left(a2 \cdot a2\right)} \cdot {th}^{2}\right)}{\sqrt{2}} \]
      2. unpow28.9%

        \[\leadsto \frac{-0.5 \cdot \left(\left(a2 \cdot a2\right) \cdot \color{blue}{\left(th \cdot th\right)}\right)}{\sqrt{2}} \]
      3. associate-*l*9.1%

        \[\leadsto \frac{-0.5 \cdot \color{blue}{\left(a2 \cdot \left(a2 \cdot \left(th \cdot th\right)\right)\right)}}{\sqrt{2}} \]
      4. associate-*l*9.1%

        \[\leadsto \frac{\color{blue}{\left(-0.5 \cdot a2\right) \cdot \left(a2 \cdot \left(th \cdot th\right)\right)}}{\sqrt{2}} \]
      5. *-commutative9.1%

        \[\leadsto \frac{\color{blue}{\left(a2 \cdot \left(th \cdot th\right)\right) \cdot \left(-0.5 \cdot a2\right)}}{\sqrt{2}} \]
      6. associate-*l*9.1%

        \[\leadsto \frac{\color{blue}{a2 \cdot \left(\left(th \cdot th\right) \cdot \left(-0.5 \cdot a2\right)\right)}}{\sqrt{2}} \]
      7. *-commutative9.1%

        \[\leadsto \frac{a2 \cdot \left(\left(th \cdot th\right) \cdot \color{blue}{\left(a2 \cdot -0.5\right)}\right)}{\sqrt{2}} \]
    12. Simplified9.1%

      \[\leadsto \frac{\color{blue}{a2 \cdot \left(\left(th \cdot th\right) \cdot \left(a2 \cdot -0.5\right)\right)}}{\sqrt{2}} \]
    13. Applied egg-rr23.2%

      \[\leadsto \color{blue}{\left(th \cdot a2\right) \cdot a2} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification18.5%

    \[\leadsto \begin{array}{l} \mathbf{if}\;a2 \leq 5.1 \cdot 10^{-113}:\\ \;\;\;\;\frac{a1 \cdot a1}{-2}\\ \mathbf{else}:\\ \;\;\;\;a2 \cdot \left(a2 \cdot th\right)\\ \end{array} \]

Alternative 8: 23.9% accurate, 83.0× speedup?

\[\begin{array}{l} a1 = |a1|\\ a2 = |a2|\\ [a1, a2] = \mathsf{sort}([a1, a2])\\ \\ a2 \cdot \left(a2 \cdot th\right) \end{array} \]
NOTE: a1 should be positive before calling this function
NOTE: a2 should be positive before calling this function
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
(FPCore (a1 a2 th) :precision binary64 (* a2 (* a2 th)))
a1 = abs(a1);
a2 = abs(a2);
assert(a1 < a2);
double code(double a1, double a2, double th) {
	return a2 * (a2 * th);
}
NOTE: a1 should be positive before calling this function
NOTE: a2 should be positive before calling this function
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
real(8) function code(a1, a2, th)
    real(8), intent (in) :: a1
    real(8), intent (in) :: a2
    real(8), intent (in) :: th
    code = a2 * (a2 * th)
end function
a1 = Math.abs(a1);
a2 = Math.abs(a2);
assert a1 < a2;
public static double code(double a1, double a2, double th) {
	return a2 * (a2 * th);
}
a1 = abs(a1)
a2 = abs(a2)
[a1, a2] = sort([a1, a2])
def code(a1, a2, th):
	return a2 * (a2 * th)
a1 = abs(a1)
a2 = abs(a2)
a1, a2 = sort([a1, a2])
function code(a1, a2, th)
	return Float64(a2 * Float64(a2 * th))
end
a1 = abs(a1)
a2 = abs(a2)
a1, a2 = num2cell(sort([a1, a2])){:}
function tmp = code(a1, a2, th)
	tmp = a2 * (a2 * th);
end
NOTE: a1 should be positive before calling this function
NOTE: a2 should be positive before calling this function
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
code[a1_, a2_, th_] := N[(a2 * N[(a2 * th), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
a1 = |a1|\\
a2 = |a2|\\
[a1, a2] = \mathsf{sort}([a1, a2])\\
\\
a2 \cdot \left(a2 \cdot th\right)
\end{array}
Derivation
  1. Initial program 98.9%

    \[\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1\right) + \frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2\right) \]
  2. Step-by-step derivation
    1. distribute-lft-out98.9%

      \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)} \]
    2. cos-neg98.9%

      \[\leadsto \frac{\color{blue}{\cos \left(-th\right)}}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
    3. associate-*l/99.0%

      \[\leadsto \color{blue}{\frac{\cos \left(-th\right) \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)}{\sqrt{2}}} \]
    4. cos-neg99.0%

      \[\leadsto \frac{\color{blue}{\cos th} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)}{\sqrt{2}} \]
    5. fma-def99.0%

      \[\leadsto \frac{\cos th \cdot \color{blue}{\mathsf{fma}\left(a1, a1, a2 \cdot a2\right)}}{\sqrt{2}} \]
  3. Simplified99.0%

    \[\leadsto \color{blue}{\frac{\cos th \cdot \mathsf{fma}\left(a1, a1, a2 \cdot a2\right)}{\sqrt{2}}} \]
  4. Taylor expanded in a1 around 0 59.2%

    \[\leadsto \frac{\color{blue}{{a2}^{2} \cdot \cos th}}{\sqrt{2}} \]
  5. Step-by-step derivation
    1. unpow259.2%

      \[\leadsto \frac{\color{blue}{\left(a2 \cdot a2\right)} \cdot \cos th}{\sqrt{2}} \]
    2. associate-*l*59.5%

      \[\leadsto \frac{\color{blue}{a2 \cdot \left(a2 \cdot \cos th\right)}}{\sqrt{2}} \]
  6. Simplified59.5%

    \[\leadsto \frac{\color{blue}{a2 \cdot \left(a2 \cdot \cos th\right)}}{\sqrt{2}} \]
  7. Taylor expanded in th around 0 35.8%

    \[\leadsto \frac{a2 \cdot \color{blue}{\left(a2 + -0.5 \cdot \left(a2 \cdot {th}^{2}\right)\right)}}{\sqrt{2}} \]
  8. Step-by-step derivation
    1. unpow235.8%

      \[\leadsto \frac{a2 \cdot \left(a2 + -0.5 \cdot \left(a2 \cdot \color{blue}{\left(th \cdot th\right)}\right)\right)}{\sqrt{2}} \]
  9. Simplified35.8%

    \[\leadsto \frac{a2 \cdot \color{blue}{\left(a2 + -0.5 \cdot \left(a2 \cdot \left(th \cdot th\right)\right)\right)}}{\sqrt{2}} \]
  10. Taylor expanded in th around inf 12.8%

    \[\leadsto \frac{\color{blue}{-0.5 \cdot \left({a2}^{2} \cdot {th}^{2}\right)}}{\sqrt{2}} \]
  11. Step-by-step derivation
    1. unpow212.8%

      \[\leadsto \frac{-0.5 \cdot \left(\color{blue}{\left(a2 \cdot a2\right)} \cdot {th}^{2}\right)}{\sqrt{2}} \]
    2. unpow212.8%

      \[\leadsto \frac{-0.5 \cdot \left(\left(a2 \cdot a2\right) \cdot \color{blue}{\left(th \cdot th\right)}\right)}{\sqrt{2}} \]
    3. associate-*l*12.5%

      \[\leadsto \frac{-0.5 \cdot \color{blue}{\left(a2 \cdot \left(a2 \cdot \left(th \cdot th\right)\right)\right)}}{\sqrt{2}} \]
    4. associate-*l*12.5%

      \[\leadsto \frac{\color{blue}{\left(-0.5 \cdot a2\right) \cdot \left(a2 \cdot \left(th \cdot th\right)\right)}}{\sqrt{2}} \]
    5. *-commutative12.5%

      \[\leadsto \frac{\color{blue}{\left(a2 \cdot \left(th \cdot th\right)\right) \cdot \left(-0.5 \cdot a2\right)}}{\sqrt{2}} \]
    6. associate-*l*12.5%

      \[\leadsto \frac{\color{blue}{a2 \cdot \left(\left(th \cdot th\right) \cdot \left(-0.5 \cdot a2\right)\right)}}{\sqrt{2}} \]
    7. *-commutative12.5%

      \[\leadsto \frac{a2 \cdot \left(\left(th \cdot th\right) \cdot \color{blue}{\left(a2 \cdot -0.5\right)}\right)}{\sqrt{2}} \]
  12. Simplified12.5%

    \[\leadsto \frac{\color{blue}{a2 \cdot \left(\left(th \cdot th\right) \cdot \left(a2 \cdot -0.5\right)\right)}}{\sqrt{2}} \]
  13. Applied egg-rr19.0%

    \[\leadsto \color{blue}{\left(th \cdot a2\right) \cdot a2} \]
  14. Final simplification19.0%

    \[\leadsto a2 \cdot \left(a2 \cdot th\right) \]

Alternative 9: 13.5% accurate, 138.3× speedup?

\[\begin{array}{l} a1 = |a1|\\ a2 = |a2|\\ [a1, a2] = \mathsf{sort}([a1, a2])\\ \\ a2 \cdot th \end{array} \]
NOTE: a1 should be positive before calling this function
NOTE: a2 should be positive before calling this function
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
(FPCore (a1 a2 th) :precision binary64 (* a2 th))
a1 = abs(a1);
a2 = abs(a2);
assert(a1 < a2);
double code(double a1, double a2, double th) {
	return a2 * th;
}
NOTE: a1 should be positive before calling this function
NOTE: a2 should be positive before calling this function
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
real(8) function code(a1, a2, th)
    real(8), intent (in) :: a1
    real(8), intent (in) :: a2
    real(8), intent (in) :: th
    code = a2 * th
end function
a1 = Math.abs(a1);
a2 = Math.abs(a2);
assert a1 < a2;
public static double code(double a1, double a2, double th) {
	return a2 * th;
}
a1 = abs(a1)
a2 = abs(a2)
[a1, a2] = sort([a1, a2])
def code(a1, a2, th):
	return a2 * th
a1 = abs(a1)
a2 = abs(a2)
a1, a2 = sort([a1, a2])
function code(a1, a2, th)
	return Float64(a2 * th)
end
a1 = abs(a1)
a2 = abs(a2)
a1, a2 = num2cell(sort([a1, a2])){:}
function tmp = code(a1, a2, th)
	tmp = a2 * th;
end
NOTE: a1 should be positive before calling this function
NOTE: a2 should be positive before calling this function
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
code[a1_, a2_, th_] := N[(a2 * th), $MachinePrecision]
\begin{array}{l}
a1 = |a1|\\
a2 = |a2|\\
[a1, a2] = \mathsf{sort}([a1, a2])\\
\\
a2 \cdot th
\end{array}
Derivation
  1. Initial program 98.9%

    \[\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1\right) + \frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2\right) \]
  2. Step-by-step derivation
    1. distribute-lft-out98.9%

      \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)} \]
    2. cos-neg98.9%

      \[\leadsto \frac{\color{blue}{\cos \left(-th\right)}}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
    3. associate-*l/99.0%

      \[\leadsto \color{blue}{\frac{\cos \left(-th\right) \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)}{\sqrt{2}}} \]
    4. cos-neg99.0%

      \[\leadsto \frac{\color{blue}{\cos th} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)}{\sqrt{2}} \]
    5. fma-def99.0%

      \[\leadsto \frac{\cos th \cdot \color{blue}{\mathsf{fma}\left(a1, a1, a2 \cdot a2\right)}}{\sqrt{2}} \]
  3. Simplified99.0%

    \[\leadsto \color{blue}{\frac{\cos th \cdot \mathsf{fma}\left(a1, a1, a2 \cdot a2\right)}{\sqrt{2}}} \]
  4. Taylor expanded in a1 around 0 59.2%

    \[\leadsto \frac{\color{blue}{{a2}^{2} \cdot \cos th}}{\sqrt{2}} \]
  5. Step-by-step derivation
    1. unpow259.2%

      \[\leadsto \frac{\color{blue}{\left(a2 \cdot a2\right)} \cdot \cos th}{\sqrt{2}} \]
    2. associate-*l*59.5%

      \[\leadsto \frac{\color{blue}{a2 \cdot \left(a2 \cdot \cos th\right)}}{\sqrt{2}} \]
  6. Simplified59.5%

    \[\leadsto \frac{\color{blue}{a2 \cdot \left(a2 \cdot \cos th\right)}}{\sqrt{2}} \]
  7. Taylor expanded in th around 0 35.8%

    \[\leadsto \frac{a2 \cdot \color{blue}{\left(a2 + -0.5 \cdot \left(a2 \cdot {th}^{2}\right)\right)}}{\sqrt{2}} \]
  8. Step-by-step derivation
    1. unpow235.8%

      \[\leadsto \frac{a2 \cdot \left(a2 + -0.5 \cdot \left(a2 \cdot \color{blue}{\left(th \cdot th\right)}\right)\right)}{\sqrt{2}} \]
  9. Simplified35.8%

    \[\leadsto \frac{a2 \cdot \color{blue}{\left(a2 + -0.5 \cdot \left(a2 \cdot \left(th \cdot th\right)\right)\right)}}{\sqrt{2}} \]
  10. Taylor expanded in th around inf 12.8%

    \[\leadsto \frac{\color{blue}{-0.5 \cdot \left({a2}^{2} \cdot {th}^{2}\right)}}{\sqrt{2}} \]
  11. Step-by-step derivation
    1. unpow212.8%

      \[\leadsto \frac{-0.5 \cdot \left(\color{blue}{\left(a2 \cdot a2\right)} \cdot {th}^{2}\right)}{\sqrt{2}} \]
    2. unpow212.8%

      \[\leadsto \frac{-0.5 \cdot \left(\left(a2 \cdot a2\right) \cdot \color{blue}{\left(th \cdot th\right)}\right)}{\sqrt{2}} \]
    3. associate-*l*12.5%

      \[\leadsto \frac{-0.5 \cdot \color{blue}{\left(a2 \cdot \left(a2 \cdot \left(th \cdot th\right)\right)\right)}}{\sqrt{2}} \]
    4. associate-*l*12.5%

      \[\leadsto \frac{\color{blue}{\left(-0.5 \cdot a2\right) \cdot \left(a2 \cdot \left(th \cdot th\right)\right)}}{\sqrt{2}} \]
    5. *-commutative12.5%

      \[\leadsto \frac{\color{blue}{\left(a2 \cdot \left(th \cdot th\right)\right) \cdot \left(-0.5 \cdot a2\right)}}{\sqrt{2}} \]
    6. associate-*l*12.5%

      \[\leadsto \frac{\color{blue}{a2 \cdot \left(\left(th \cdot th\right) \cdot \left(-0.5 \cdot a2\right)\right)}}{\sqrt{2}} \]
    7. *-commutative12.5%

      \[\leadsto \frac{a2 \cdot \left(\left(th \cdot th\right) \cdot \color{blue}{\left(a2 \cdot -0.5\right)}\right)}{\sqrt{2}} \]
  12. Simplified12.5%

    \[\leadsto \frac{\color{blue}{a2 \cdot \left(\left(th \cdot th\right) \cdot \left(a2 \cdot -0.5\right)\right)}}{\sqrt{2}} \]
  13. Applied egg-rr11.3%

    \[\leadsto \color{blue}{th \cdot a2} \]
  14. Final simplification11.3%

    \[\leadsto a2 \cdot th \]

Alternative 10: 3.5% accurate, 415.0× speedup?

\[\begin{array}{l} a1 = |a1|\\ a2 = |a2|\\ [a1, a2] = \mathsf{sort}([a1, a2])\\ \\ 1 \end{array} \]
NOTE: a1 should be positive before calling this function
NOTE: a2 should be positive before calling this function
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
(FPCore (a1 a2 th) :precision binary64 1.0)
a1 = abs(a1);
a2 = abs(a2);
assert(a1 < a2);
double code(double a1, double a2, double th) {
	return 1.0;
}
NOTE: a1 should be positive before calling this function
NOTE: a2 should be positive before calling this function
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
real(8) function code(a1, a2, th)
    real(8), intent (in) :: a1
    real(8), intent (in) :: a2
    real(8), intent (in) :: th
    code = 1.0d0
end function
a1 = Math.abs(a1);
a2 = Math.abs(a2);
assert a1 < a2;
public static double code(double a1, double a2, double th) {
	return 1.0;
}
a1 = abs(a1)
a2 = abs(a2)
[a1, a2] = sort([a1, a2])
def code(a1, a2, th):
	return 1.0
a1 = abs(a1)
a2 = abs(a2)
a1, a2 = sort([a1, a2])
function code(a1, a2, th)
	return 1.0
end
a1 = abs(a1)
a2 = abs(a2)
a1, a2 = num2cell(sort([a1, a2])){:}
function tmp = code(a1, a2, th)
	tmp = 1.0;
end
NOTE: a1 should be positive before calling this function
NOTE: a2 should be positive before calling this function
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
code[a1_, a2_, th_] := 1.0
\begin{array}{l}
a1 = |a1|\\
a2 = |a2|\\
[a1, a2] = \mathsf{sort}([a1, a2])\\
\\
1
\end{array}
Derivation
  1. Initial program 98.9%

    \[\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1\right) + \frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2\right) \]
  2. Step-by-step derivation
    1. distribute-lft-out98.9%

      \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)} \]
    2. cos-neg98.9%

      \[\leadsto \frac{\color{blue}{\cos \left(-th\right)}}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
    3. associate-*l/99.0%

      \[\leadsto \color{blue}{\frac{\cos \left(-th\right) \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)}{\sqrt{2}}} \]
    4. cos-neg99.0%

      \[\leadsto \frac{\color{blue}{\cos th} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)}{\sqrt{2}} \]
    5. fma-def99.0%

      \[\leadsto \frac{\cos th \cdot \color{blue}{\mathsf{fma}\left(a1, a1, a2 \cdot a2\right)}}{\sqrt{2}} \]
  3. Simplified99.0%

    \[\leadsto \color{blue}{\frac{\cos th \cdot \mathsf{fma}\left(a1, a1, a2 \cdot a2\right)}{\sqrt{2}}} \]
  4. Taylor expanded in a1 around 0 59.2%

    \[\leadsto \frac{\color{blue}{{a2}^{2} \cdot \cos th}}{\sqrt{2}} \]
  5. Step-by-step derivation
    1. unpow259.2%

      \[\leadsto \frac{\color{blue}{\left(a2 \cdot a2\right)} \cdot \cos th}{\sqrt{2}} \]
    2. associate-*l*59.5%

      \[\leadsto \frac{\color{blue}{a2 \cdot \left(a2 \cdot \cos th\right)}}{\sqrt{2}} \]
  6. Simplified59.5%

    \[\leadsto \frac{\color{blue}{a2 \cdot \left(a2 \cdot \cos th\right)}}{\sqrt{2}} \]
  7. Step-by-step derivation
    1. div-inv59.5%

      \[\leadsto \color{blue}{\left(a2 \cdot \left(a2 \cdot \cos th\right)\right) \cdot \frac{1}{\sqrt{2}}} \]
    2. pow1/259.5%

      \[\leadsto \left(a2 \cdot \left(a2 \cdot \cos th\right)\right) \cdot \frac{1}{\color{blue}{{2}^{0.5}}} \]
    3. pow-flip59.5%

      \[\leadsto \left(a2 \cdot \left(a2 \cdot \cos th\right)\right) \cdot \color{blue}{{2}^{\left(-0.5\right)}} \]
    4. metadata-eval59.5%

      \[\leadsto \left(a2 \cdot \left(a2 \cdot \cos th\right)\right) \cdot {2}^{\color{blue}{-0.5}} \]
    5. *-commutative59.5%

      \[\leadsto \color{blue}{\left(\left(a2 \cdot \cos th\right) \cdot a2\right)} \cdot {2}^{-0.5} \]
    6. associate-*l*59.5%

      \[\leadsto \color{blue}{\left(a2 \cdot \cos th\right) \cdot \left(a2 \cdot {2}^{-0.5}\right)} \]
    7. add-sqr-sqrt59.2%

      \[\leadsto \left(a2 \cdot \cos th\right) \cdot \left(a2 \cdot \color{blue}{\left(\sqrt{{2}^{-0.5}} \cdot \sqrt{{2}^{-0.5}}\right)}\right) \]
    8. sqrt-unprod59.5%

      \[\leadsto \left(a2 \cdot \cos th\right) \cdot \left(a2 \cdot \color{blue}{\sqrt{{2}^{-0.5} \cdot {2}^{-0.5}}}\right) \]
    9. pow-prod-up59.5%

      \[\leadsto \left(a2 \cdot \cos th\right) \cdot \left(a2 \cdot \sqrt{\color{blue}{{2}^{\left(-0.5 + -0.5\right)}}}\right) \]
    10. metadata-eval59.5%

      \[\leadsto \left(a2 \cdot \cos th\right) \cdot \left(a2 \cdot \sqrt{{2}^{\color{blue}{-1}}}\right) \]
    11. metadata-eval59.5%

      \[\leadsto \left(a2 \cdot \cos th\right) \cdot \left(a2 \cdot \sqrt{\color{blue}{0.5}}\right) \]
  8. Applied egg-rr59.5%

    \[\leadsto \color{blue}{\left(a2 \cdot \cos th\right) \cdot \left(a2 \cdot \sqrt{0.5}\right)} \]
  9. Taylor expanded in th around 0 39.6%

    \[\leadsto \color{blue}{{a2}^{2} \cdot \sqrt{0.5}} \]
  10. Step-by-step derivation
    1. unpow239.6%

      \[\leadsto \color{blue}{\left(a2 \cdot a2\right)} \cdot \sqrt{0.5} \]
    2. associate-*l*39.6%

      \[\leadsto \color{blue}{a2 \cdot \left(a2 \cdot \sqrt{0.5}\right)} \]
  11. Simplified39.6%

    \[\leadsto \color{blue}{a2 \cdot \left(a2 \cdot \sqrt{0.5}\right)} \]
  12. Applied egg-rr3.3%

    \[\leadsto \color{blue}{\frac{a2}{a2}} \]
  13. Step-by-step derivation
    1. *-inverses3.3%

      \[\leadsto \color{blue}{1} \]
  14. Simplified3.3%

    \[\leadsto \color{blue}{1} \]
  15. Final simplification3.3%

    \[\leadsto 1 \]

Alternative 11: 3.8% accurate, 415.0× speedup?

\[\begin{array}{l} a1 = |a1|\\ a2 = |a2|\\ [a1, a2] = \mathsf{sort}([a1, a2])\\ \\ a1 \end{array} \]
NOTE: a1 should be positive before calling this function
NOTE: a2 should be positive before calling this function
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
(FPCore (a1 a2 th) :precision binary64 a1)
a1 = abs(a1);
a2 = abs(a2);
assert(a1 < a2);
double code(double a1, double a2, double th) {
	return a1;
}
NOTE: a1 should be positive before calling this function
NOTE: a2 should be positive before calling this function
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
real(8) function code(a1, a2, th)
    real(8), intent (in) :: a1
    real(8), intent (in) :: a2
    real(8), intent (in) :: th
    code = a1
end function
a1 = Math.abs(a1);
a2 = Math.abs(a2);
assert a1 < a2;
public static double code(double a1, double a2, double th) {
	return a1;
}
a1 = abs(a1)
a2 = abs(a2)
[a1, a2] = sort([a1, a2])
def code(a1, a2, th):
	return a1
a1 = abs(a1)
a2 = abs(a2)
a1, a2 = sort([a1, a2])
function code(a1, a2, th)
	return a1
end
a1 = abs(a1)
a2 = abs(a2)
a1, a2 = num2cell(sort([a1, a2])){:}
function tmp = code(a1, a2, th)
	tmp = a1;
end
NOTE: a1 should be positive before calling this function
NOTE: a2 should be positive before calling this function
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
code[a1_, a2_, th_] := a1
\begin{array}{l}
a1 = |a1|\\
a2 = |a2|\\
[a1, a2] = \mathsf{sort}([a1, a2])\\
\\
a1
\end{array}
Derivation
  1. Initial program 98.9%

    \[\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1\right) + \frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2\right) \]
  2. Step-by-step derivation
    1. +-commutative98.9%

      \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2\right) + \frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1\right)} \]
    2. distribute-lft-out98.9%

      \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2 + a1 \cdot a1\right)} \]
  3. Simplified98.9%

    \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2 + a1 \cdot a1\right)} \]
  4. Taylor expanded in th around 0 65.4%

    \[\leadsto \color{blue}{\frac{{a1}^{2} + {a2}^{2}}{\sqrt{2}}} \]
  5. Step-by-step derivation
    1. unpow265.4%

      \[\leadsto \frac{\color{blue}{a1 \cdot a1} + {a2}^{2}}{\sqrt{2}} \]
    2. unpow265.4%

      \[\leadsto \frac{a1 \cdot a1 + \color{blue}{a2 \cdot a2}}{\sqrt{2}} \]
    3. +-commutative65.4%

      \[\leadsto \frac{\color{blue}{a2 \cdot a2 + a1 \cdot a1}}{\sqrt{2}} \]
  6. Simplified65.4%

    \[\leadsto \color{blue}{\frac{a2 \cdot a2 + a1 \cdot a1}{\sqrt{2}}} \]
  7. Taylor expanded in a2 around 0 41.1%

    \[\leadsto \color{blue}{\frac{{a1}^{2}}{\sqrt{2}}} \]
  8. Step-by-step derivation
    1. unpow241.1%

      \[\leadsto \frac{\color{blue}{a1 \cdot a1}}{\sqrt{2}} \]
  9. Simplified41.1%

    \[\leadsto \color{blue}{\frac{a1 \cdot a1}{\sqrt{2}}} \]
  10. Step-by-step derivation
    1. frac-2neg41.1%

      \[\leadsto \color{blue}{\frac{-a1 \cdot a1}{-\sqrt{2}}} \]
    2. div-inv41.1%

      \[\leadsto \color{blue}{\left(-a1 \cdot a1\right) \cdot \frac{1}{-\sqrt{2}}} \]
  11. Applied egg-rr41.1%

    \[\leadsto \color{blue}{\left(-a1 \cdot a1\right) \cdot \frac{1}{-\sqrt{2}}} \]
  12. Applied egg-rr3.4%

    \[\leadsto \color{blue}{0 + a1} \]
  13. Step-by-step derivation
    1. +-lft-identity3.4%

      \[\leadsto \color{blue}{a1} \]
  14. Simplified3.4%

    \[\leadsto \color{blue}{a1} \]
  15. Final simplification3.4%

    \[\leadsto a1 \]

Alternative 12: 5.5% accurate, 415.0× speedup?

\[\begin{array}{l} a1 = |a1|\\ a2 = |a2|\\ [a1, a2] = \mathsf{sort}([a1, a2])\\ \\ a2 \end{array} \]
NOTE: a1 should be positive before calling this function
NOTE: a2 should be positive before calling this function
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
(FPCore (a1 a2 th) :precision binary64 a2)
a1 = abs(a1);
a2 = abs(a2);
assert(a1 < a2);
double code(double a1, double a2, double th) {
	return a2;
}
NOTE: a1 should be positive before calling this function
NOTE: a2 should be positive before calling this function
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
real(8) function code(a1, a2, th)
    real(8), intent (in) :: a1
    real(8), intent (in) :: a2
    real(8), intent (in) :: th
    code = a2
end function
a1 = Math.abs(a1);
a2 = Math.abs(a2);
assert a1 < a2;
public static double code(double a1, double a2, double th) {
	return a2;
}
a1 = abs(a1)
a2 = abs(a2)
[a1, a2] = sort([a1, a2])
def code(a1, a2, th):
	return a2
a1 = abs(a1)
a2 = abs(a2)
a1, a2 = sort([a1, a2])
function code(a1, a2, th)
	return a2
end
a1 = abs(a1)
a2 = abs(a2)
a1, a2 = num2cell(sort([a1, a2])){:}
function tmp = code(a1, a2, th)
	tmp = a2;
end
NOTE: a1 should be positive before calling this function
NOTE: a2 should be positive before calling this function
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
code[a1_, a2_, th_] := a2
\begin{array}{l}
a1 = |a1|\\
a2 = |a2|\\
[a1, a2] = \mathsf{sort}([a1, a2])\\
\\
a2
\end{array}
Derivation
  1. Initial program 98.9%

    \[\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1\right) + \frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2\right) \]
  2. Step-by-step derivation
    1. distribute-lft-out98.9%

      \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)} \]
    2. cos-neg98.9%

      \[\leadsto \frac{\color{blue}{\cos \left(-th\right)}}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
    3. associate-*l/99.0%

      \[\leadsto \color{blue}{\frac{\cos \left(-th\right) \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)}{\sqrt{2}}} \]
    4. cos-neg99.0%

      \[\leadsto \frac{\color{blue}{\cos th} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)}{\sqrt{2}} \]
    5. fma-def99.0%

      \[\leadsto \frac{\cos th \cdot \color{blue}{\mathsf{fma}\left(a1, a1, a2 \cdot a2\right)}}{\sqrt{2}} \]
  3. Simplified99.0%

    \[\leadsto \color{blue}{\frac{\cos th \cdot \mathsf{fma}\left(a1, a1, a2 \cdot a2\right)}{\sqrt{2}}} \]
  4. Taylor expanded in a1 around 0 59.2%

    \[\leadsto \frac{\color{blue}{{a2}^{2} \cdot \cos th}}{\sqrt{2}} \]
  5. Step-by-step derivation
    1. unpow259.2%

      \[\leadsto \frac{\color{blue}{\left(a2 \cdot a2\right)} \cdot \cos th}{\sqrt{2}} \]
    2. associate-*l*59.5%

      \[\leadsto \frac{\color{blue}{a2 \cdot \left(a2 \cdot \cos th\right)}}{\sqrt{2}} \]
  6. Simplified59.5%

    \[\leadsto \frac{\color{blue}{a2 \cdot \left(a2 \cdot \cos th\right)}}{\sqrt{2}} \]
  7. Step-by-step derivation
    1. div-inv59.5%

      \[\leadsto \color{blue}{\left(a2 \cdot \left(a2 \cdot \cos th\right)\right) \cdot \frac{1}{\sqrt{2}}} \]
    2. pow1/259.5%

      \[\leadsto \left(a2 \cdot \left(a2 \cdot \cos th\right)\right) \cdot \frac{1}{\color{blue}{{2}^{0.5}}} \]
    3. pow-flip59.5%

      \[\leadsto \left(a2 \cdot \left(a2 \cdot \cos th\right)\right) \cdot \color{blue}{{2}^{\left(-0.5\right)}} \]
    4. metadata-eval59.5%

      \[\leadsto \left(a2 \cdot \left(a2 \cdot \cos th\right)\right) \cdot {2}^{\color{blue}{-0.5}} \]
    5. *-commutative59.5%

      \[\leadsto \color{blue}{\left(\left(a2 \cdot \cos th\right) \cdot a2\right)} \cdot {2}^{-0.5} \]
    6. associate-*l*59.5%

      \[\leadsto \color{blue}{\left(a2 \cdot \cos th\right) \cdot \left(a2 \cdot {2}^{-0.5}\right)} \]
    7. add-sqr-sqrt59.2%

      \[\leadsto \left(a2 \cdot \cos th\right) \cdot \left(a2 \cdot \color{blue}{\left(\sqrt{{2}^{-0.5}} \cdot \sqrt{{2}^{-0.5}}\right)}\right) \]
    8. sqrt-unprod59.5%

      \[\leadsto \left(a2 \cdot \cos th\right) \cdot \left(a2 \cdot \color{blue}{\sqrt{{2}^{-0.5} \cdot {2}^{-0.5}}}\right) \]
    9. pow-prod-up59.5%

      \[\leadsto \left(a2 \cdot \cos th\right) \cdot \left(a2 \cdot \sqrt{\color{blue}{{2}^{\left(-0.5 + -0.5\right)}}}\right) \]
    10. metadata-eval59.5%

      \[\leadsto \left(a2 \cdot \cos th\right) \cdot \left(a2 \cdot \sqrt{{2}^{\color{blue}{-1}}}\right) \]
    11. metadata-eval59.5%

      \[\leadsto \left(a2 \cdot \cos th\right) \cdot \left(a2 \cdot \sqrt{\color{blue}{0.5}}\right) \]
  8. Applied egg-rr59.5%

    \[\leadsto \color{blue}{\left(a2 \cdot \cos th\right) \cdot \left(a2 \cdot \sqrt{0.5}\right)} \]
  9. Taylor expanded in th around 0 39.6%

    \[\leadsto \color{blue}{{a2}^{2} \cdot \sqrt{0.5}} \]
  10. Step-by-step derivation
    1. unpow239.6%

      \[\leadsto \color{blue}{\left(a2 \cdot a2\right)} \cdot \sqrt{0.5} \]
    2. associate-*l*39.6%

      \[\leadsto \color{blue}{a2 \cdot \left(a2 \cdot \sqrt{0.5}\right)} \]
  11. Simplified39.6%

    \[\leadsto \color{blue}{a2 \cdot \left(a2 \cdot \sqrt{0.5}\right)} \]
  12. Applied egg-rr4.8%

    \[\leadsto \color{blue}{\left|a2\right|} \]
  13. Step-by-step derivation
    1. unpow14.8%

      \[\leadsto \left|\color{blue}{{a2}^{1}}\right| \]
    2. *-inverses4.8%

      \[\leadsto \left|{a2}^{\color{blue}{\left(\frac{a2}{a2}\right)}}\right| \]
    3. sqr-pow2.4%

      \[\leadsto \left|\color{blue}{{a2}^{\left(\frac{\frac{a2}{a2}}{2}\right)} \cdot {a2}^{\left(\frac{\frac{a2}{a2}}{2}\right)}}\right| \]
    4. fabs-sqr2.4%

      \[\leadsto \color{blue}{{a2}^{\left(\frac{\frac{a2}{a2}}{2}\right)} \cdot {a2}^{\left(\frac{\frac{a2}{a2}}{2}\right)}} \]
    5. sqr-pow3.6%

      \[\leadsto \color{blue}{{a2}^{\left(\frac{a2}{a2}\right)}} \]
    6. *-inverses3.6%

      \[\leadsto {a2}^{\color{blue}{1}} \]
    7. unpow13.6%

      \[\leadsto \color{blue}{a2} \]
  14. Simplified3.6%

    \[\leadsto \color{blue}{a2} \]
  15. Final simplification3.6%

    \[\leadsto a2 \]

Reproduce

?
herbie shell --seed 2023285 
(FPCore (a1 a2 th)
  :name "Migdal et al, Equation (64)"
  :precision binary64
  (+ (* (/ (cos th) (sqrt 2.0)) (* a1 a1)) (* (/ (cos th) (sqrt 2.0)) (* a2 a2))))