Migdal et al, Equation (64)

?

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

?

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

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 12 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

Derivation?

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

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

    [Start]99.6%

    \[ \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.6%

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

    fma-def [=>]99.6%

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

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

    [Start]99.6%

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

    fma-def [<=]99.6%

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

    associate-*r/ [=>]99.6%

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

    associate-*l/ [<=]99.6%

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

    distribute-lft-in [=>]99.6%

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

    associate-*l/ [=>]99.6%

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

    associate-*l/ [=>]99.6%

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

    frac-add [=>]99.3%

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

    fma-def [=>]99.3%

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

    *-commutative [=>]99.3%

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

    add-sqr-sqrt [<=]99.7%

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

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

    [Start]99.7%

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

    fma-udef [=>]99.7%

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

    *-commutative [=>]99.7%

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

    distribute-rgt-out [=>]99.6%

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

    *-commutative [<=]99.6%

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

    distribute-lft-in [<=]99.6%

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

    +-commutative [=>]99.6%

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

    unpow2 [<=]99.7%

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

    unpow2 [<=]99.7%

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

    *-commutative [<=]99.7%

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

    unpow2 [=>]99.6%

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

    unpow2 [=>]99.6%

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

    fma-def [=>]99.6%

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

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

Alternatives

Alternative 1
Accuracy99.6%
Cost19904
\[\frac{\sqrt{2} \cdot \left(\mathsf{fma}\left(a2, a2, a1 \cdot a1\right) \cdot \cos th\right)}{2} \]
Alternative 2
Accuracy78.4%
Cost19780
\[\begin{array}{l} \mathbf{if}\;\cos th \leq -0.005:\\ \;\;\;\;a1 \cdot \frac{a1 \cdot \cos th}{\sqrt{2}}\\ \mathbf{else}:\\ \;\;\;\;\left(a1 \cdot a1 + a2 \cdot a2\right) \cdot \sqrt{0.5}\\ \end{array} \]
Alternative 3
Accuracy99.6%
Cost13568
\[\left(\cos th \cdot {2}^{-0.5}\right) \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
Alternative 4
Accuracy68.7%
Cost13512
\[\begin{array}{l} \mathbf{if}\;a1 \leq -1.9 \cdot 10^{+46}:\\ \;\;\;\;a1 \cdot \frac{a1 \cdot \cos th}{\sqrt{2}}\\ \mathbf{elif}\;a1 \leq -1.75 \cdot 10^{-129}:\\ \;\;\;\;\left(a1 \cdot a1 + a2 \cdot a2\right) \cdot \sqrt{0.5}\\ \mathbf{else}:\\ \;\;\;\;\cos th \cdot \left(\left(a2 \cdot a2\right) \cdot \sqrt{0.5}\right)\\ \end{array} \]
Alternative 5
Accuracy68.7%
Cost13512
\[\begin{array}{l} \mathbf{if}\;a1 \leq -1.25 \cdot 10^{+46}:\\ \;\;\;\;a1 \cdot \frac{a1 \cdot \cos th}{\sqrt{2}}\\ \mathbf{elif}\;a1 \leq -5.3 \cdot 10^{-129}:\\ \;\;\;\;\left(a1 \cdot a1 + a2 \cdot a2\right) \cdot \sqrt{0.5}\\ \mathbf{else}:\\ \;\;\;\;\frac{a2}{\sqrt{2}} \cdot \left(a2 \cdot \cos th\right)\\ \end{array} \]
Alternative 6
Accuracy68.7%
Cost13512
\[\begin{array}{l} \mathbf{if}\;a1 \leq -2 \cdot 10^{+46}:\\ \;\;\;\;a1 \cdot \left(a1 \cdot \left(\cos th \cdot {2}^{-0.5}\right)\right)\\ \mathbf{elif}\;a1 \leq -5.2 \cdot 10^{-129}:\\ \;\;\;\;\left(a1 \cdot a1 + a2 \cdot a2\right) \cdot \sqrt{0.5}\\ \mathbf{else}:\\ \;\;\;\;\frac{a2}{\sqrt{2}} \cdot \left(a2 \cdot \cos th\right)\\ \end{array} \]
Alternative 7
Accuracy99.6%
Cost13504
\[\left(a1 \cdot a1 + a2 \cdot a2\right) \cdot \frac{\cos th}{\sqrt{2}} \]
Alternative 8
Accuracy35.4%
Cost6980
\[\begin{array}{l} \mathbf{if}\;a1 \leq -1.6 \cdot 10^{+70}:\\ \;\;\;\;a1 \cdot \frac{a1}{\sqrt{2}}\\ \mathbf{else}:\\ \;\;\;\;a2 \cdot \sqrt{\left(a2 \cdot a2\right) \cdot 0.5}\\ \end{array} \]
Alternative 9
Accuracy65.5%
Cost6976
\[\left(a1 \cdot a1 + a2 \cdot a2\right) \cdot \sqrt{0.5} \]
Alternative 10
Accuracy47.0%
Cost6852
\[\begin{array}{l} \mathbf{if}\;a1 \leq -2.65 \cdot 10^{+54}:\\ \;\;\;\;a1 \cdot \frac{a1}{\sqrt{2}}\\ \mathbf{else}:\\ \;\;\;\;\left(a2 \cdot a2\right) \cdot \sqrt{0.5}\\ \end{array} \]
Alternative 11
Accuracy38.8%
Cost6720
\[a1 \cdot \frac{a1}{\sqrt{2}} \]
Alternative 12
Accuracy38.8%
Cost6720
\[\left(a1 \cdot a1\right) \cdot \sqrt{0.5} \]

Reproduce?

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