Average Error: 7.4 → 0.2
Time: 9.5s
Precision: binary64
Cost: 7360
\[\left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.im + \left(x.re \cdot x.im + x.im \cdot x.re\right) \cdot x.re \]
\[\mathsf{fma}\left(x.im + x.re, x.im \cdot \left(x.re - x.im\right), \left(x.re + x.re\right) \cdot \left(x.im \cdot x.re\right)\right) \]
(FPCore (x.re x.im)
 :precision binary64
 (+
  (* (- (* x.re x.re) (* x.im x.im)) x.im)
  (* (+ (* x.re x.im) (* x.im x.re)) x.re)))
(FPCore (x.re x.im)
 :precision binary64
 (fma (+ x.im x.re) (* x.im (- x.re x.im)) (* (+ x.re x.re) (* x.im x.re))))
double code(double x_46_re, double x_46_im) {
	return (((x_46_re * x_46_re) - (x_46_im * x_46_im)) * x_46_im) + (((x_46_re * x_46_im) + (x_46_im * x_46_re)) * x_46_re);
}
double code(double x_46_re, double x_46_im) {
	return fma((x_46_im + x_46_re), (x_46_im * (x_46_re - x_46_im)), ((x_46_re + x_46_re) * (x_46_im * x_46_re)));
}
function code(x_46_re, x_46_im)
	return Float64(Float64(Float64(Float64(x_46_re * x_46_re) - Float64(x_46_im * x_46_im)) * x_46_im) + Float64(Float64(Float64(x_46_re * x_46_im) + Float64(x_46_im * x_46_re)) * x_46_re))
end
function code(x_46_re, x_46_im)
	return fma(Float64(x_46_im + x_46_re), Float64(x_46_im * Float64(x_46_re - x_46_im)), Float64(Float64(x_46_re + x_46_re) * Float64(x_46_im * x_46_re)))
end
code[x$46$re_, x$46$im_] := N[(N[(N[(N[(x$46$re * x$46$re), $MachinePrecision] - N[(x$46$im * x$46$im), $MachinePrecision]), $MachinePrecision] * x$46$im), $MachinePrecision] + N[(N[(N[(x$46$re * x$46$im), $MachinePrecision] + N[(x$46$im * x$46$re), $MachinePrecision]), $MachinePrecision] * x$46$re), $MachinePrecision]), $MachinePrecision]
code[x$46$re_, x$46$im_] := N[(N[(x$46$im + x$46$re), $MachinePrecision] * N[(x$46$im * N[(x$46$re - x$46$im), $MachinePrecision]), $MachinePrecision] + N[(N[(x$46$re + x$46$re), $MachinePrecision] * N[(x$46$im * x$46$re), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.im + \left(x.re \cdot x.im + x.im \cdot x.re\right) \cdot x.re
\mathsf{fma}\left(x.im + x.re, x.im \cdot \left(x.re - x.im\right), \left(x.re + x.re\right) \cdot \left(x.im \cdot x.re\right)\right)

Error

Target

Original7.4
Target0.2
Herbie0.2
\[\left(x.re \cdot x.im\right) \cdot \left(2 \cdot x.re\right) + \left(x.im \cdot \left(x.re - x.im\right)\right) \cdot \left(x.re + x.im\right) \]

Derivation

  1. Initial program 7.4

    \[\left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.im + \left(x.re \cdot x.im + x.im \cdot x.re\right) \cdot x.re \]
  2. Taylor expanded in x.re around 0 7.5

    \[\leadsto \left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.im + \color{blue}{2 \cdot \left({x.re}^{2} \cdot x.im\right)} \]
  3. Simplified7.5

    \[\leadsto \left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.im + \color{blue}{x.im \cdot \left(x.re \cdot \left(x.re \cdot 2\right)\right)} \]
    Proof
    (*.f64 x.im (*.f64 x.re (*.f64 x.re 2))): 0 points increase in error, 0 points decrease in error
    (*.f64 x.im (Rewrite<= associate-*l*_binary64 (*.f64 (*.f64 x.re x.re) 2))): 0 points increase in error, 1 points decrease in error
    (*.f64 x.im (*.f64 (Rewrite<= unpow2_binary64 (pow.f64 x.re 2)) 2)): 0 points increase in error, 0 points decrease in error
    (*.f64 x.im (Rewrite<= *-commutative_binary64 (*.f64 2 (pow.f64 x.re 2)))): 0 points increase in error, 0 points decrease in error
    (Rewrite<= *-commutative_binary64 (*.f64 (*.f64 2 (pow.f64 x.re 2)) x.im)): 0 points increase in error, 0 points decrease in error
    (Rewrite<= associate-*r*_binary64 (*.f64 2 (*.f64 (pow.f64 x.re 2) x.im))): 0 points increase in error, 0 points decrease in error
  4. Applied egg-rr0.2

    \[\leadsto \color{blue}{\mathsf{fma}\left(x.im + x.re, \left(x.re - x.im\right) \cdot x.im, \left(x.re + x.re\right) \cdot \left(x.im \cdot x.re\right)\right)} \]
  5. Final simplification0.2

    \[\leadsto \mathsf{fma}\left(x.im + x.re, x.im \cdot \left(x.re - x.im\right), \left(x.re + x.re\right) \cdot \left(x.im \cdot x.re\right)\right) \]

Alternatives

Alternative 1
Error0.5
Cost1352
\[\begin{array}{l} \mathbf{if}\;x.re \leq -1 \cdot 10^{+160}:\\ \;\;\;\;x.re \cdot \left(\left(x.im \cdot x.re\right) \cdot 3\right)\\ \mathbf{elif}\;x.re \leq 10^{+132}:\\ \;\;\;\;x.im \cdot \left(x.re \cdot x.re - x.im \cdot x.im\right) + x.im \cdot \left(x.re \cdot \left(x.re \cdot 2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;x.re \cdot \left(x.re \cdot \left(x.im \cdot 3\right)\right)\\ \end{array} \]
Alternative 2
Error9.9
Cost1096
\[\begin{array}{l} t_0 := \left(x.re + x.re\right) + \left(x.im \cdot \left(x.re - x.im\right)\right) \cdot \left(x.im + x.re\right)\\ \mathbf{if}\;x.im \leq -1.4 \cdot 10^{-5}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x.im \leq 10^{-48}:\\ \;\;\;\;\left(x.im \cdot x.re\right) \cdot \left(x.re \cdot 3\right)\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 3
Error6.0
Cost1096
\[\begin{array}{l} \mathbf{if}\;x.re \leq -126318463067311700:\\ \;\;\;\;x.re \cdot \left(\left(x.im \cdot x.re\right) \cdot 3\right)\\ \mathbf{elif}\;x.re \leq 2.7585520967113443 \cdot 10^{-75}:\\ \;\;\;\;x.im \cdot \left(x.re \cdot \left(x.re \cdot 2\right)\right) - x.im \cdot \left(x.im \cdot x.im\right)\\ \mathbf{else}:\\ \;\;\;\;\left(x.im \cdot x.re\right) \cdot \left(x.re \cdot 3\right)\\ \end{array} \]
Alternative 4
Error5.9
Cost1096
\[\begin{array}{l} \mathbf{if}\;x.re \leq -126318463067311700:\\ \;\;\;\;x.re \cdot \left(\left(x.im \cdot x.re\right) \cdot 2\right) + x.re \cdot \left(x.im \cdot x.re\right)\\ \mathbf{elif}\;x.re \leq 2.7585520967113443 \cdot 10^{-75}:\\ \;\;\;\;x.im \cdot \left(x.re \cdot \left(x.re \cdot 2\right)\right) - x.im \cdot \left(x.im \cdot x.im\right)\\ \mathbf{else}:\\ \;\;\;\;\left(x.im \cdot x.re\right) \cdot \left(x.re \cdot 3\right)\\ \end{array} \]
Alternative 5
Error0.2
Cost1088
\[\left(x.im \cdot \left(x.re - x.im\right)\right) \cdot \left(x.im + x.re\right) + x.re \cdot \left(\left(x.im \cdot x.re\right) \cdot 2\right) \]
Alternative 6
Error42.2
Cost448
\[x.re \cdot \left(x.re \cdot \left(x.im \cdot 2\right)\right) \]
Alternative 7
Error19.1
Cost448
\[\left(x.im \cdot x.re\right) \cdot \left(x.re \cdot 3\right) \]

Error

Reproduce

herbie shell --seed 2022289 
(FPCore (x.re x.im)
  :name "math.cube on complex, imaginary part"
  :precision binary64

  :herbie-target
  (+ (* (* x.re x.im) (* 2.0 x.re)) (* (* x.im (- x.re x.im)) (+ x.re x.im)))

  (+ (* (- (* x.re x.re) (* x.im x.im)) x.im) (* (+ (* x.re x.im) (* x.im x.re)) x.re)))