| Alternative 1 | |
|---|---|
| Accuracy | 99.6% |
| Cost | 19904 |
\[\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}
Herbie found 12 alternatives:
| Alternative | Accuracy | Speedup |
|---|
Initial program 99.6%
Simplified99.6%
[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}}
\] |
Applied egg-rr99.7%
[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}}
\] |
Simplified99.6%
[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}
\] |
Final simplification99.6%
| Alternative 1 | |
|---|---|
| Accuracy | 99.6% |
| Cost | 19904 |
| Alternative 2 | |
|---|---|
| Accuracy | 78.4% |
| Cost | 19780 |
| Alternative 3 | |
|---|---|
| Accuracy | 99.6% |
| Cost | 13568 |
| Alternative 4 | |
|---|---|
| Accuracy | 68.7% |
| Cost | 13512 |
| Alternative 5 | |
|---|---|
| Accuracy | 68.7% |
| Cost | 13512 |
| Alternative 6 | |
|---|---|
| Accuracy | 68.7% |
| Cost | 13512 |
| Alternative 7 | |
|---|---|
| Accuracy | 99.6% |
| Cost | 13504 |
| Alternative 8 | |
|---|---|
| Accuracy | 35.4% |
| Cost | 6980 |
| Alternative 9 | |
|---|---|
| Accuracy | 65.5% |
| Cost | 6976 |
| Alternative 10 | |
|---|---|
| Accuracy | 47.0% |
| Cost | 6852 |
| Alternative 11 | |
|---|---|
| Accuracy | 38.8% |
| Cost | 6720 |
| Alternative 12 | |
|---|---|
| Accuracy | 38.8% |
| Cost | 6720 |
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))))