Average Error: 6.9 → 0.2
Time: 6.8s
Precision: binary64
Cost: 7364
\[\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 \]
\[\begin{array}{l} \mathbf{if}\;x.re \leq -1.7556158237614024 \cdot 10^{+107}:\\ \;\;\;\;\mathsf{fma}\left(x.re \cdot x.im, x.re - x.im, 2 \cdot \left(x.re \cdot \left(x.re \cdot x.im\right)\right)\right)\\ \mathbf{elif}\;x.re \leq 5.178071457101999 \cdot 10^{+114}:\\ \;\;\;\;x.im \cdot \left(x.re \cdot \left(x.re \cdot 3\right)\right) - {x.im}^{3}\\ \mathbf{else}:\\ \;\;\;\;x.re \cdot \left(x.im \cdot \left(x.re \cdot 3 - x.im\right)\right)\\ \end{array} \]
(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
 (if (<= x.re -1.7556158237614024e+107)
   (fma (* x.re x.im) (- x.re x.im) (* 2.0 (* x.re (* x.re x.im))))
   (if (<= x.re 5.178071457101999e+114)
     (- (* x.im (* x.re (* x.re 3.0))) (pow x.im 3.0))
     (* x.re (* x.im (- (* x.re 3.0) x.im))))))
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) {
	double tmp;
	if (x_46_re <= -1.7556158237614024e+107) {
		tmp = fma((x_46_re * x_46_im), (x_46_re - x_46_im), (2.0 * (x_46_re * (x_46_re * x_46_im))));
	} else if (x_46_re <= 5.178071457101999e+114) {
		tmp = (x_46_im * (x_46_re * (x_46_re * 3.0))) - pow(x_46_im, 3.0);
	} else {
		tmp = x_46_re * (x_46_im * ((x_46_re * 3.0) - x_46_im));
	}
	return tmp;
}
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)
	tmp = 0.0
	if (x_46_re <= -1.7556158237614024e+107)
		tmp = fma(Float64(x_46_re * x_46_im), Float64(x_46_re - x_46_im), Float64(2.0 * Float64(x_46_re * Float64(x_46_re * x_46_im))));
	elseif (x_46_re <= 5.178071457101999e+114)
		tmp = Float64(Float64(x_46_im * Float64(x_46_re * Float64(x_46_re * 3.0))) - (x_46_im ^ 3.0));
	else
		tmp = Float64(x_46_re * Float64(x_46_im * Float64(Float64(x_46_re * 3.0) - x_46_im)));
	end
	return tmp
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_] := If[LessEqual[x$46$re, -1.7556158237614024e+107], N[(N[(x$46$re * x$46$im), $MachinePrecision] * N[(x$46$re - x$46$im), $MachinePrecision] + N[(2.0 * N[(x$46$re * N[(x$46$re * x$46$im), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[x$46$re, 5.178071457101999e+114], N[(N[(x$46$im * N[(x$46$re * N[(x$46$re * 3.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - N[Power[x$46$im, 3.0], $MachinePrecision]), $MachinePrecision], N[(x$46$re * N[(x$46$im * N[(N[(x$46$re * 3.0), $MachinePrecision] - x$46$im), $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
\begin{array}{l}
\mathbf{if}\;x.re \leq -1.7556158237614024 \cdot 10^{+107}:\\
\;\;\;\;\mathsf{fma}\left(x.re \cdot x.im, x.re - x.im, 2 \cdot \left(x.re \cdot \left(x.re \cdot x.im\right)\right)\right)\\

\mathbf{elif}\;x.re \leq 5.178071457101999 \cdot 10^{+114}:\\
\;\;\;\;x.im \cdot \left(x.re \cdot \left(x.re \cdot 3\right)\right) - {x.im}^{3}\\

\mathbf{else}:\\
\;\;\;\;x.re \cdot \left(x.im \cdot \left(x.re \cdot 3 - x.im\right)\right)\\


\end{array}

Error

Target

Original6.9
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. Split input into 3 regimes
  2. if x.re < -1.75561582376140238e107

    1. Initial program 35.6

      \[\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 35.6

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(x.im \cdot \left(x.re + x.im\right), x.re - x.im, 2 \cdot \left(x.re \cdot \left(x.re \cdot x.im\right)\right)\right)} \]
    4. Taylor expanded in x.im around 0 0.3

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

    if -1.75561582376140238e107 < x.re < 5.1780714571019992e114

    1. Initial program 0.2

      \[\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 0.1

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

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

    if 5.1780714571019992e114 < x.re

    1. Initial program 40.9

      \[\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 40.9

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(x.im \cdot \left(x.re + x.im\right), x.re - x.im, 2 \cdot \left(x.re \cdot \left(x.re \cdot x.im\right)\right)\right)} \]
    4. Taylor expanded in x.im around 0 0.3

      \[\leadsto \mathsf{fma}\left(\color{blue}{x.re \cdot x.im}, x.re - x.im, 2 \cdot \left(x.re \cdot \left(x.re \cdot x.im\right)\right)\right) \]
    5. Taylor expanded in x.re around 0 40.9

      \[\leadsto \color{blue}{-1 \cdot \left(x.re \cdot {x.im}^{2}\right) + {x.re}^{2} \cdot \left(x.im + 2 \cdot x.im\right)} \]
    6. Simplified0.4

      \[\leadsto \color{blue}{x.re \cdot \left(x.im \cdot \left(x.re \cdot 3 - x.im\right)\right)} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification0.2

    \[\leadsto \begin{array}{l} \mathbf{if}\;x.re \leq -1.7556158237614024 \cdot 10^{+107}:\\ \;\;\;\;\mathsf{fma}\left(x.re \cdot x.im, x.re - x.im, 2 \cdot \left(x.re \cdot \left(x.re \cdot x.im\right)\right)\right)\\ \mathbf{elif}\;x.re \leq 5.178071457101999 \cdot 10^{+114}:\\ \;\;\;\;x.im \cdot \left(x.re \cdot \left(x.re \cdot 3\right)\right) - {x.im}^{3}\\ \mathbf{else}:\\ \;\;\;\;x.re \cdot \left(x.im \cdot \left(x.re \cdot 3 - x.im\right)\right)\\ \end{array} \]

Alternatives

Alternative 1
Error0.2
Cost7304
\[\begin{array}{l} \mathbf{if}\;x.re \leq -1.7556158237614024 \cdot 10^{+107}:\\ \;\;\;\;\left(x.re \cdot \left(x.re \cdot x.im\right)\right) \cdot 3\\ \mathbf{elif}\;x.re \leq 5.178071457101999 \cdot 10^{+114}:\\ \;\;\;\;x.im \cdot \left(x.re \cdot \left(x.re \cdot 3\right)\right) - {x.im}^{3}\\ \mathbf{else}:\\ \;\;\;\;x.re \cdot \left(x.im \cdot \left(x.re \cdot 3 - x.im\right)\right)\\ \end{array} \]
Alternative 2
Error0.3
Cost1352
\[\begin{array}{l} \mathbf{if}\;x.re \leq -1 \cdot 10^{+146}:\\ \;\;\;\;x.re \cdot \left(\left(x.re \cdot x.im\right) \cdot 3\right)\\ \mathbf{elif}\;x.re \leq 6.044736954456755 \cdot 10^{+81}:\\ \;\;\;\;x.im \cdot \left(x.re \cdot x.re - x.im \cdot x.im\right) + x.im \cdot \left(2 \cdot \left(x.re \cdot x.re\right)\right)\\ \mathbf{else}:\\ \;\;\;\;x.re \cdot \left(x.im \cdot \left(x.re \cdot 3 - x.im\right)\right)\\ \end{array} \]
Alternative 3
Error19.3
Cost576
\[x.re \cdot \left(x.im \cdot \left(x.re \cdot 3 - x.im\right)\right) \]
Alternative 4
Error19.3
Cost448
\[\left(x.re \cdot x.im\right) \cdot \left(x.re \cdot 3\right) \]
Alternative 5
Error19.3
Cost448
\[x.re \cdot \left(\left(x.re \cdot x.im\right) \cdot 3\right) \]
Alternative 6
Error19.3
Cost448
\[\left(x.re \cdot \left(x.re \cdot x.im\right)\right) \cdot 3 \]
Alternative 7
Error46.1
Cost384
\[-\left(x.re \cdot x.im\right) \cdot x.im \]

Error

Reproduce

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