(FPCore (i n) :precision binary64 (* 2.0 (cos (- (* 2.0 (+ i 1.0)) (/ (* 1.0 (PI)) (* 2.0 n))))))
\begin{array}{l} \\ 2 \cdot \cos \left(2 \cdot \left(i + 1\right) - \frac{1 \cdot \mathsf{PI}\left(\right)}{2 \cdot n}\right) \end{array}
Sampling outcomes in binary64 precision:
Herbie found 7 alternatives:
Alternative | Accuracy | Speedup |
---|
(FPCore (i n) :precision binary64 (* 2.0 (cos (- (* 2.0 (+ i 1.0)) (/ (* 1.0 (PI)) (* 2.0 n))))))
\begin{array}{l} \\ 2 \cdot \cos \left(2 \cdot \left(i + 1\right) - \frac{1 \cdot \mathsf{PI}\left(\right)}{2 \cdot n}\right) \end{array}
(FPCore (i n) :precision binary64 (let* ((t_0 (/ (PI) n))) (* 2.0 (- (* (cos (* i 2.0)) (fma (cos (* t_0 -0.5)) (cos 2.0) (* (sin 2.0) (sin (* t_0 0.5))))) (* (sin (* i 2.0)) (sin (- 2.0 (/ (PI) (* n 2.0)))))))))
\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{\mathsf{PI}\left(\right)}{n}\\ 2 \cdot \left(\cos \left(i \cdot 2\right) \cdot \mathsf{fma}\left(\cos \left(t\_0 \cdot -0.5\right), \cos 2, \sin 2 \cdot \sin \left(t\_0 \cdot 0.5\right)\right) - \sin \left(i \cdot 2\right) \cdot \sin \left(2 - \frac{\mathsf{PI}\left(\right)}{n \cdot 2}\right)\right) \end{array} \end{array}
Initial program 96.4%
lift-cos.f64
N/A
lift--.f64
N/A
lift-*.f64
N/A
lift-+.f64
N/A
distribute-rgt-in
N/A
metadata-eval
N/A
associate--l+
N/A
cos-sum
N/A
lower--.f64
N/A
Applied rewrites98.7%
lift-cos.f64
N/A
lift--.f64
N/A
cos-diff
N/A
Applied rewrites99.0%
(FPCore (i n) :precision binary64 (let* ((t_0 (fma (/ (PI) n) -0.5 2.0))) (* 2.0 (fma (sin t_0) (- (sin (* 2.0 i))) (* (cos t_0) (cos (* 2.0 i)))))))
\begin{array}{l} \\ \begin{array}{l} t_0 := \mathsf{fma}\left(\frac{\mathsf{PI}\left(\right)}{n}, -0.5, 2\right)\\ 2 \cdot \mathsf{fma}\left(\sin t\_0, -\sin \left(2 \cdot i\right), \cos t\_0 \cdot \cos \left(2 \cdot i\right)\right) \end{array} \end{array}
Initial program 96.4%
lift-cos.f64
N/A
lift--.f64
N/A
lift-*.f64
N/A
lift-+.f64
N/A
distribute-rgt-in
N/A
metadata-eval
N/A
associate--l+
N/A
cos-sum
N/A
lower--.f64
N/A
Applied rewrites98.7%
lift--.f64
N/A
sub-neg
N/A
+-commutative
N/A
lift-*.f64
N/A
*-commutative
N/A
distribute-rgt-neg-in
N/A
lower-fma.f64
N/A
Applied rewrites98.8%
(FPCore (i n) :precision binary64 (let* ((t_0 (fma (/ (PI) n) -0.5 2.0))) (* 2.0 (- (* (cos t_0) (cos (* 2.0 i))) (* (sin t_0) (sin (* 2.0 i)))))))
\begin{array}{l} \\ \begin{array}{l} t_0 := \mathsf{fma}\left(\frac{\mathsf{PI}\left(\right)}{n}, -0.5, 2\right)\\ 2 \cdot \left(\cos t\_0 \cdot \cos \left(2 \cdot i\right) - \sin t\_0 \cdot \sin \left(2 \cdot i\right)\right) \end{array} \end{array}
Initial program 96.4%
lift-cos.f64
N/A
lift--.f64
N/A
lift-*.f64
N/A
lift-+.f64
N/A
distribute-rgt-in
N/A
metadata-eval
N/A
associate--l+
N/A
cos-sum
N/A
lower--.f64
N/A
Applied rewrites98.7%
Applied rewrites98.7%
(FPCore (i n) :precision binary64 (* 2.0 (fma (cos (fma i 2.0 2.0)) (cos (* -0.5 (/ (PI) n))) (* (sin (/ (PI) (* n 2.0))) (sin (fma i 2.0 2.0))))))
\begin{array}{l} \\ 2 \cdot \mathsf{fma}\left(\cos \left(\mathsf{fma}\left(i, 2, 2\right)\right), \cos \left(-0.5 \cdot \frac{\mathsf{PI}\left(\right)}{n}\right), \sin \left(\frac{\mathsf{PI}\left(\right)}{n \cdot 2}\right) \cdot \sin \left(\mathsf{fma}\left(i, 2, 2\right)\right)\right) \end{array}
Initial program 96.4%
lift-cos.f64
N/A
lift--.f64
N/A
cos-diff
N/A
lower-fma.f64
N/A
Applied rewrites98.5%
(FPCore (i n) :precision binary64 (* 2.0 (cos (fma i 2.0 (fma (/ (PI) n) -0.5 2.0)))))
\begin{array}{l} \\ 2 \cdot \cos \left(\mathsf{fma}\left(i, 2, \mathsf{fma}\left(\frac{\mathsf{PI}\left(\right)}{n}, -0.5, 2\right)\right)\right) \end{array}
Initial program 96.4%
lift--.f64
N/A
lift-*.f64
N/A
lift-+.f64
N/A
distribute-rgt-in
N/A
metadata-eval
N/A
associate--l+
N/A
lower-fma.f64
N/A
lower--.f64
96.6
lift-*.f64
N/A
*-lft-identity
96.6
lift-*.f64
N/A
*-commutative
N/A
lower-*.f64
96.6
Applied rewrites96.6%
lift--.f64
N/A
lift-/.f64
N/A
lift-*.f64
N/A
associate-/r*
N/A
lift-/.f64
N/A
div-inv
N/A
metadata-eval
N/A
*-commutative
N/A
cancel-sign-sub-inv
N/A
metadata-eval
N/A
lift-*.f64
N/A
+-commutative
N/A
lift-*.f64
N/A
*-commutative
N/A
lower-fma.f64
96.6
Applied rewrites96.6%
(FPCore (i n) :precision binary64 (* 2.0 (cos (fma 2.0 i 2.0))))
double code(double i, double n) { return 2.0 * cos(fma(2.0, i, 2.0)); }
function code(i, n) return Float64(2.0 * cos(fma(2.0, i, 2.0))) end
code[i_, n_] := N[(2.0 * N[Cos[N[(2.0 * i + 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l} \\ 2 \cdot \cos \left(\mathsf{fma}\left(2, i, 2\right)\right) \end{array}
Initial program 96.4%
Taylor expanded in n around inf
distribute-lft-in
N/A
metadata-eval
N/A
+-commutative
N/A
lower-fma.f64
19.4
Applied rewrites19.4%
(FPCore (i n) :precision binary64 (* 2.0 (cos (* 2.0 i))))
double code(double i, double n) { return 2.0 * cos((2.0 * i)); }
real(8) function code(i, n) real(8), intent (in) :: i real(8), intent (in) :: n code = 2.0d0 * cos((2.0d0 * i)) end function
public static double code(double i, double n) { return 2.0 * Math.cos((2.0 * i)); }
def code(i, n): return 2.0 * math.cos((2.0 * i))
function code(i, n) return Float64(2.0 * cos(Float64(2.0 * i))) end
function tmp = code(i, n) tmp = 2.0 * cos((2.0 * i)); end
code[i_, n_] := N[(2.0 * N[Cos[N[(2.0 * i), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l} \\ 2 \cdot \cos \left(2 \cdot i\right) \end{array}
Initial program 96.4%
Taylor expanded in i around inf
lower-*.f64
9.2
Applied rewrites9.2%
herbie shell --seed 1
(FPCore (i n)
:name "2 * cos ( 2* (i+1) -1 * PI / (2*n))"
:precision binary64
:pre (and (and (<= 2.0 i) (<= i 16.0)) (and (<= 2.0 n) (<= n 16.0)))
(* 2.0 (cos (- (* 2.0 (+ i 1.0)) (/ (* 1.0 (PI)) (* 2.0 n))))))