?

Average Accuracy: 99.2% → 99.3%
Time: 16.4s
Precision: binary64
Cost: 19776

?

\[\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(\sqrt{0.5} \cdot \mathsf{fma}\left(a1, a1, a2 \cdot a2\right)\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) (* (sqrt 0.5) (fma a1 a1 (* a2 a2)))))
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) * (sqrt(0.5) * fma(a1, a1, (a2 * a2)));
}
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(sqrt(0.5) * fma(a1, a1, Float64(a2 * a2))))
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[Sqrt[0.5], $MachinePrecision] * N[(a1 * a1 + N[(a2 * a2), $MachinePrecision]), $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(\sqrt{0.5} \cdot \mathsf{fma}\left(a1, a1, a2 \cdot a2\right)\right)

Error?

Derivation?

  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. Simplified99.3%

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

    [Start]99.2

    \[ \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.2

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

    associate-*l/ [=>]99.3

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

    associate-*r/ [<=]99.3

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

    fma-def [=>]99.3

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

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

    [Start]99.3

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

    div-inv [=>]99.2

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

    add-sqr-sqrt [=>]99.1

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

    pow2 [=>]99.1

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

    fma-udef [=>]99.1

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

    hypot-def [=>]99.1

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

    pow1/2 [=>]99.1

    \[ \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.3

    \[ \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.3

    \[ \cos th \cdot \left({\left(\mathsf{hypot}\left(a1, a2\right)\right)}^{2} \cdot {2}^{\color{blue}{-0.5}}\right) \]
  4. Applied egg-rr99.3%

    \[\leadsto \cos th \cdot \left(\color{blue}{\left(a2 \cdot a2 + a1 \cdot a1\right)} \cdot {2}^{-0.5}\right) \]
    Proof

    [Start]99.3

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

    unpow2 [=>]99.3

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

    hypot-udef [=>]99.3

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

    hypot-udef [=>]99.3

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

    add-sqr-sqrt [<=]99.3

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

    +-commutative [=>]99.3

    \[ \cos th \cdot \left(\color{blue}{\left(a2 \cdot a2 + a1 \cdot a1\right)} \cdot {2}^{-0.5}\right) \]
  5. Taylor expanded in a2 around 0 99.3%

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

    \[\leadsto \cos th \cdot \color{blue}{\left(\sqrt{0.5} \cdot \mathsf{fma}\left(a1, a1, a2 \cdot a2\right)\right)} \]
    Proof

    [Start]99.3

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

    unpow2 [=>]99.3

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

    unpow2 [=>]99.3

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

    distribute-lft-in [<=]99.3

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

    +-commutative [=>]99.3

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

    fma-udef [<=]99.3

    \[ \cos th \cdot \left(\sqrt{0.5} \cdot \color{blue}{\mathsf{fma}\left(a1, a1, a2 \cdot a2\right)}\right) \]
  7. Final simplification99.3%

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

Alternatives

Alternative 1
Accuracy76.7%
Cost19780
\[\begin{array}{l} \mathbf{if}\;\cos th \leq 0.9999:\\ \;\;\;\;a2 \cdot \left(a2 \cdot \left(\cos th \cdot \sqrt{0.5}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5} \cdot \left(a2 \cdot a2 + a1 \cdot a1\right)\\ \end{array} \]
Alternative 2
Accuracy99.3%
Cost13504
\[\cos th \cdot \left(\sqrt{0.5} \cdot \left(a2 \cdot a2 + a1 \cdot a1\right)\right) \]
Alternative 3
Accuracy67.4%
Cost13380
\[\begin{array}{l} \mathbf{if}\;a2 \leq 5.3 \cdot 10^{-148}:\\ \;\;\;\;\cos th \cdot \left(\sqrt{0.5} \cdot \left(a1 \cdot a1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;a2 \cdot \left(a2 \cdot \left(\cos th \cdot \sqrt{0.5}\right)\right)\\ \end{array} \]
Alternative 4
Accuracy67.4%
Cost13380
\[\begin{array}{l} \mathbf{if}\;a2 \leq 5.3 \cdot 10^{-148}:\\ \;\;\;\;\cos th \cdot \frac{a1}{\frac{\sqrt{2}}{a1}}\\ \mathbf{else}:\\ \;\;\;\;a2 \cdot \left(a2 \cdot \left(\cos th \cdot \sqrt{0.5}\right)\right)\\ \end{array} \]
Alternative 5
Accuracy67.4%
Cost13380
\[\begin{array}{l} \mathbf{if}\;a2 \leq 5.3 \cdot 10^{-148}:\\ \;\;\;\;\frac{a1}{\frac{\sqrt{2}}{\cos th \cdot a1}}\\ \mathbf{else}:\\ \;\;\;\;a2 \cdot \left(a2 \cdot \left(\cos th \cdot \sqrt{0.5}\right)\right)\\ \end{array} \]
Alternative 6
Accuracy42.6%
Cost6980
\[\begin{array}{l} \mathbf{if}\;a2 \leq 6 \cdot 10^{-118}:\\ \;\;\;\;\frac{a1}{\frac{\sqrt{2}}{a1}}\\ \mathbf{else}:\\ \;\;\;\;a2 \cdot \sqrt{\frac{a2 \cdot a2}{2}}\\ \end{array} \]
Alternative 7
Accuracy58.8%
Cost6976
\[\sqrt{0.5} \cdot \left(a2 \cdot a2 + a1 \cdot a1\right) \]
Alternative 8
Accuracy28.8%
Cost6852
\[\begin{array}{l} \mathbf{if}\;a1 \leq -5.2 \cdot 10^{-168}:\\ \;\;\;\;a1 \cdot \left(\sqrt{0.5} \cdot a1\right)\\ \mathbf{else}:\\ \;\;\;\;a2 \cdot \left(0.5 \cdot a2\right)\\ \end{array} \]
Alternative 9
Accuracy42.6%
Cost6852
\[\begin{array}{l} \mathbf{if}\;a2 \leq 6.4 \cdot 10^{-118}:\\ \;\;\;\;a1 \cdot \left(\sqrt{0.5} \cdot a1\right)\\ \mathbf{else}:\\ \;\;\;\;a2 \cdot \frac{a2}{\sqrt{2}}\\ \end{array} \]
Alternative 10
Accuracy42.6%
Cost6852
\[\begin{array}{l} \mathbf{if}\;a2 \leq 5.6 \cdot 10^{-118}:\\ \;\;\;\;a1 \cdot \left(\sqrt{0.5} \cdot a1\right)\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5} \cdot \left(a2 \cdot a2\right)\\ \end{array} \]
Alternative 11
Accuracy42.5%
Cost6852
\[\begin{array}{l} \mathbf{if}\;a2 \leq 6.2 \cdot 10^{-118}:\\ \;\;\;\;\frac{a1}{\frac{\sqrt{2}}{a1}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5} \cdot \left(a2 \cdot a2\right)\\ \end{array} \]
Alternative 12
Accuracy19.8%
Cost320
\[a2 \cdot \left(0.5 \cdot a2\right) \]
Alternative 13
Accuracy19.5%
Cost192
\[a2 \cdot a2 \]

Error

Reproduce?

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