Migdal et al, Equation (64)

?

Percentage Accurate: 99.6% → 99.6%
Time: 12.0s
Precision: binary64
Cost: 26112

?

\[\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1\right) + \frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2\right) \]
\[\cos th \cdot \left({\left(\mathsf{hypot}\left(a1, a2\right)\right)}^{2} \cdot {2}^{-0.5}\right) \]
(FPCore (a1 a2 th)
 :precision binary64
 (+
  (* (/ (cos th) (sqrt 2.0)) (* a1 a1))
  (* (/ (cos th) (sqrt 2.0)) (* a2 a2))))
(FPCore (a1 a2 th)
 :precision binary64
 (* (cos th) (* (pow (hypot a1 a2) 2.0) (pow 2.0 -0.5))))
double code(double a1, double a2, double th) {
	return ((cos(th) / sqrt(2.0)) * (a1 * a1)) + ((cos(th) / sqrt(2.0)) * (a2 * a2));
}
double code(double a1, double a2, double th) {
	return cos(th) * (pow(hypot(a1, a2), 2.0) * pow(2.0, -0.5));
}
public static double code(double a1, double a2, double th) {
	return ((Math.cos(th) / Math.sqrt(2.0)) * (a1 * a1)) + ((Math.cos(th) / Math.sqrt(2.0)) * (a2 * a2));
}
public static double code(double a1, double a2, double th) {
	return Math.cos(th) * (Math.pow(Math.hypot(a1, a2), 2.0) * Math.pow(2.0, -0.5));
}
def code(a1, a2, th):
	return ((math.cos(th) / math.sqrt(2.0)) * (a1 * a1)) + ((math.cos(th) / math.sqrt(2.0)) * (a2 * a2))
def code(a1, a2, th):
	return math.cos(th) * (math.pow(math.hypot(a1, a2), 2.0) * math.pow(2.0, -0.5))
function code(a1, a2, th)
	return Float64(Float64(Float64(cos(th) / sqrt(2.0)) * Float64(a1 * a1)) + Float64(Float64(cos(th) / sqrt(2.0)) * Float64(a2 * a2)))
end
function code(a1, a2, th)
	return Float64(cos(th) * Float64((hypot(a1, a2) ^ 2.0) * (2.0 ^ -0.5)))
end
function tmp = code(a1, a2, th)
	tmp = ((cos(th) / sqrt(2.0)) * (a1 * a1)) + ((cos(th) / sqrt(2.0)) * (a2 * a2));
end
function tmp = code(a1, a2, th)
	tmp = cos(th) * ((hypot(a1, a2) ^ 2.0) * (2.0 ^ -0.5));
end
code[a1_, a2_, th_] := N[(N[(N[(N[Cos[th], $MachinePrecision] / N[Sqrt[2.0], $MachinePrecision]), $MachinePrecision] * N[(a1 * a1), $MachinePrecision]), $MachinePrecision] + N[(N[(N[Cos[th], $MachinePrecision] / N[Sqrt[2.0], $MachinePrecision]), $MachinePrecision] * N[(a2 * a2), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
code[a1_, a2_, th_] := N[(N[Cos[th], $MachinePrecision] * N[(N[Power[N[Sqrt[a1 ^ 2 + a2 ^ 2], $MachinePrecision], 2.0], $MachinePrecision] * N[Power[2.0, -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1\right) + \frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2\right)
\cos th \cdot \left({\left(\mathsf{hypot}\left(a1, a2\right)\right)}^{2} \cdot {2}^{-0.5}\right)

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.

Herbie found 14 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

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.

Bogosity?

Bogosity

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Derivation?

  1. Initial program 99.5%

    \[\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1\right) + \frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2\right) \]
  2. Simplified99.5%

    \[\leadsto \color{blue}{\cos th \cdot \frac{\mathsf{fma}\left(a1, a1, a2 \cdot a2\right)}{\sqrt{2}}} \]
    Step-by-step derivation

    [Start]99.5%

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

    distribute-lft-out [=>]99.6%

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

    associate-*l/ [=>]99.6%

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

    associate-*r/ [<=]99.5%

    \[ \color{blue}{\cos th \cdot \frac{a1 \cdot a1 + a2 \cdot a2}{\sqrt{2}}} \]

    fma-def [=>]99.5%

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

    \[\leadsto \cos th \cdot \color{blue}{\left({\left(\mathsf{hypot}\left(a1, a2\right)\right)}^{2} \cdot {2}^{-0.5}\right)} \]
    Step-by-step derivation

    [Start]99.5%

    \[ \cos th \cdot \frac{\mathsf{fma}\left(a1, a1, a2 \cdot a2\right)}{\sqrt{2}} \]

    fma-def [<=]99.5%

    \[ \cos th \cdot \frac{\color{blue}{a1 \cdot a1 + a2 \cdot a2}}{\sqrt{2}} \]

    div-inv [=>]99.5%

    \[ \cos th \cdot \color{blue}{\left(\left(a1 \cdot a1 + a2 \cdot a2\right) \cdot \frac{1}{\sqrt{2}}\right)} \]

    add-sqr-sqrt [=>]99.5%

    \[ \cos th \cdot \left(\color{blue}{\left(\sqrt{a1 \cdot a1 + a2 \cdot a2} \cdot \sqrt{a1 \cdot a1 + a2 \cdot a2}\right)} \cdot \frac{1}{\sqrt{2}}\right) \]

    pow2 [=>]99.5%

    \[ \cos th \cdot \left(\color{blue}{{\left(\sqrt{a1 \cdot a1 + a2 \cdot a2}\right)}^{2}} \cdot \frac{1}{\sqrt{2}}\right) \]

    hypot-def [=>]99.5%

    \[ \cos th \cdot \left({\color{blue}{\left(\mathsf{hypot}\left(a1, a2\right)\right)}}^{2} \cdot \frac{1}{\sqrt{2}}\right) \]

    pow1/2 [=>]99.5%

    \[ \cos th \cdot \left({\left(\mathsf{hypot}\left(a1, a2\right)\right)}^{2} \cdot \frac{1}{\color{blue}{{2}^{0.5}}}\right) \]

    pow-flip [=>]99.6%

    \[ \cos th \cdot \left({\left(\mathsf{hypot}\left(a1, a2\right)\right)}^{2} \cdot \color{blue}{{2}^{\left(-0.5\right)}}\right) \]

    metadata-eval [=>]99.6%

    \[ \cos th \cdot \left({\left(\mathsf{hypot}\left(a1, a2\right)\right)}^{2} \cdot {2}^{\color{blue}{-0.5}}\right) \]
  4. Final simplification99.6%

    \[\leadsto \cos th \cdot \left({\left(\mathsf{hypot}\left(a1, a2\right)\right)}^{2} \cdot {2}^{-0.5}\right) \]

Alternatives

Alternative 1
Accuracy99.6%
Cost26112
\[\cos th \cdot \left({\left(\mathsf{hypot}\left(a1, a2\right)\right)}^{2} \cdot {2}^{-0.5}\right) \]
Alternative 2
Accuracy78.4%
Cost19780
\[\begin{array}{l} \mathbf{if}\;\cos th \leq 0.9:\\ \;\;\;\;a2 \cdot \frac{a2}{\frac{\sqrt{2}}{\cos th}}\\ \mathbf{else}:\\ \;\;\;\;\left(a1 \cdot a1 + a2 \cdot a2\right) \cdot \sqrt{0.5}\\ \end{array} \]
Alternative 3
Accuracy68.6%
Cost13645
\[\begin{array}{l} \mathbf{if}\;a2 \leq 4.5 \cdot 10^{-109}:\\ \;\;\;\;\cos th \cdot \left(\left(a1 \cdot a1\right) \cdot \sqrt{0.5}\right)\\ \mathbf{elif}\;a2 \leq 4 \cdot 10^{-76} \lor \neg \left(a2 \leq 3.6 \cdot 10^{-35}\right):\\ \;\;\;\;a2 \cdot \frac{a2}{\frac{\sqrt{2}}{\cos th}}\\ \mathbf{else}:\\ \;\;\;\;\left(a1 \cdot a1 + a2 \cdot a2\right) \cdot \sqrt{0.5}\\ \end{array} \]
Alternative 4
Accuracy68.6%
Cost13645
\[\begin{array}{l} \mathbf{if}\;a2 \leq 3.25 \cdot 10^{-109}:\\ \;\;\;\;\cos th \cdot \left(\left(a1 \cdot a1\right) \cdot \sqrt{0.5}\right)\\ \mathbf{elif}\;a2 \leq 1.5 \cdot 10^{-76} \lor \neg \left(a2 \leq 2.7 \cdot 10^{-32}\right):\\ \;\;\;\;\cos th \cdot \frac{a2}{\frac{\sqrt{2}}{a2}}\\ \mathbf{else}:\\ \;\;\;\;\left(a1 \cdot a1 + a2 \cdot a2\right) \cdot \sqrt{0.5}\\ \end{array} \]
Alternative 5
Accuracy68.6%
Cost13645
\[\begin{array}{l} \mathbf{if}\;a2 \leq 1.05 \cdot 10^{-109}:\\ \;\;\;\;\sqrt{0.5} \cdot \left(a1 \cdot \left(\cos th \cdot a1\right)\right)\\ \mathbf{elif}\;a2 \leq 1.65 \cdot 10^{-76} \lor \neg \left(a2 \leq 5.3 \cdot 10^{-33}\right):\\ \;\;\;\;\cos th \cdot \frac{a2}{\frac{\sqrt{2}}{a2}}\\ \mathbf{else}:\\ \;\;\;\;\left(a1 \cdot a1 + a2 \cdot a2\right) \cdot \sqrt{0.5}\\ \end{array} \]
Alternative 6
Accuracy68.5%
Cost13645
\[\begin{array}{l} \mathbf{if}\;a2 \leq 3.75 \cdot 10^{-109}:\\ \;\;\;\;\frac{\cos th}{\frac{\sqrt{2}}{a1 \cdot a1}}\\ \mathbf{elif}\;a2 \leq 2.2 \cdot 10^{-76} \lor \neg \left(a2 \leq 2.2 \cdot 10^{-32}\right):\\ \;\;\;\;\cos th \cdot \frac{a2}{\frac{\sqrt{2}}{a2}}\\ \mathbf{else}:\\ \;\;\;\;\left(a1 \cdot a1 + a2 \cdot a2\right) \cdot \sqrt{0.5}\\ \end{array} \]
Alternative 7
Accuracy68.4%
Cost13644
\[\begin{array}{l} t_1 := \frac{\sqrt{2}}{a2}\\ \mathbf{if}\;a2 \leq 4.5 \cdot 10^{-109}:\\ \;\;\;\;\frac{\cos th}{\frac{\sqrt{2}}{a1 \cdot a1}}\\ \mathbf{elif}\;a2 \leq 1.5 \cdot 10^{-76}:\\ \;\;\;\;\cos th \cdot \frac{a2}{t_1}\\ \mathbf{elif}\;a2 \leq 9 \cdot 10^{-33}:\\ \;\;\;\;\left(a1 \cdot a1 + a2 \cdot a2\right) \cdot \sqrt{0.5}\\ \mathbf{else}:\\ \;\;\;\;\frac{\cos th \cdot a2}{t_1}\\ \end{array} \]
Alternative 8
Accuracy99.6%
Cost13568
\[\left(\cos th \cdot {2}^{-0.5}\right) \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
Alternative 9
Accuracy99.6%
Cost13504
\[\left(a1 \cdot a1 + a2 \cdot a2\right) \cdot \frac{\cos th}{\sqrt{2}} \]
Alternative 10
Accuracy67.2%
Cost7364
\[\begin{array}{l} \mathbf{if}\;a1 \leq -1 \cdot 10^{+156}:\\ \;\;\;\;\sqrt{0.5} \cdot \left(a1 \cdot \left(a1 + -0.5 \cdot \left(a1 \cdot \left(th \cdot th\right)\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\left(a1 \cdot a1 + a2 \cdot a2\right) \cdot \sqrt{0.5}\\ \end{array} \]
Alternative 11
Accuracy46.9%
Cost7181
\[\begin{array}{l} \mathbf{if}\;a2 \leq 3.5 \cdot 10^{-66}:\\ \;\;\;\;\left(a1 \cdot a1\right) \cdot \sqrt{0.5}\\ \mathbf{elif}\;a2 \leq 1.9 \cdot 10^{+164} \lor \neg \left(a2 \leq 1.45 \cdot 10^{+173}\right):\\ \;\;\;\;\left(a2 \cdot a2\right) \cdot \sqrt{0.5}\\ \mathbf{else}:\\ \;\;\;\;\frac{a2 \cdot \left(-a2\right)}{\sqrt{2}}\\ \end{array} \]
Alternative 12
Accuracy66.8%
Cost6976
\[\left(a1 \cdot a1 + a2 \cdot a2\right) \cdot \sqrt{0.5} \]
Alternative 13
Accuracy47.2%
Cost6852
\[\begin{array}{l} \mathbf{if}\;a2 \leq 4.4 \cdot 10^{-66}:\\ \;\;\;\;\left(a1 \cdot a1\right) \cdot \sqrt{0.5}\\ \mathbf{else}:\\ \;\;\;\;\left(a2 \cdot a2\right) \cdot \sqrt{0.5}\\ \end{array} \]
Alternative 14
Accuracy39.8%
Cost6720
\[\left(a2 \cdot a2\right) \cdot \sqrt{0.5} \]

Reproduce?

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