Average Error: 7.4 → 0.2
Time: 11.0s
Precision: binary64
Cost: 7560
\[\left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.re - \left(x.re \cdot x.im + x.im \cdot x.re\right) \cdot x.im \]
\[\begin{array}{l} t_0 := x.im \cdot \left(\left(x.re \cdot x.im\right) \cdot -2\right)\\ \mathbf{if}\;x.im \leq -1 \cdot 10^{+150}:\\ \;\;\;\;\left(x.re \cdot x.im\right) \cdot \left(x.im \cdot -3\right)\\ \mathbf{elif}\;x.im \leq 2.086925204876773 \cdot 10^{+99}:\\ \;\;\;\;x.re \cdot \left(x.re \cdot x.re - x.im \cdot x.im\right) + t_0\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(x.re + x.im, x.im \cdot \left(-x.re\right), t_0\right)\\ \end{array} \]
(FPCore (x.re x.im)
 :precision binary64
 (-
  (* (- (* x.re x.re) (* x.im x.im)) x.re)
  (* (+ (* x.re x.im) (* x.im x.re)) x.im)))
(FPCore (x.re x.im)
 :precision binary64
 (let* ((t_0 (* x.im (* (* x.re x.im) -2.0))))
   (if (<= x.im -1e+150)
     (* (* x.re x.im) (* x.im -3.0))
     (if (<= x.im 2.086925204876773e+99)
       (+ (* x.re (- (* x.re x.re) (* x.im x.im))) t_0)
       (fma (+ x.re x.im) (* x.im (- x.re)) t_0)))))
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_re) - (((x_46_re * x_46_im) + (x_46_im * x_46_re)) * x_46_im);
}
double code(double x_46_re, double x_46_im) {
	double t_0 = x_46_im * ((x_46_re * x_46_im) * -2.0);
	double tmp;
	if (x_46_im <= -1e+150) {
		tmp = (x_46_re * x_46_im) * (x_46_im * -3.0);
	} else if (x_46_im <= 2.086925204876773e+99) {
		tmp = (x_46_re * ((x_46_re * x_46_re) - (x_46_im * x_46_im))) + t_0;
	} else {
		tmp = fma((x_46_re + x_46_im), (x_46_im * -x_46_re), t_0);
	}
	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_re) - Float64(Float64(Float64(x_46_re * x_46_im) + Float64(x_46_im * x_46_re)) * x_46_im))
end
function code(x_46_re, x_46_im)
	t_0 = Float64(x_46_im * Float64(Float64(x_46_re * x_46_im) * -2.0))
	tmp = 0.0
	if (x_46_im <= -1e+150)
		tmp = Float64(Float64(x_46_re * x_46_im) * Float64(x_46_im * -3.0));
	elseif (x_46_im <= 2.086925204876773e+99)
		tmp = Float64(Float64(x_46_re * Float64(Float64(x_46_re * x_46_re) - Float64(x_46_im * x_46_im))) + t_0);
	else
		tmp = fma(Float64(x_46_re + x_46_im), Float64(x_46_im * Float64(-x_46_re)), t_0);
	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$re), $MachinePrecision] - N[(N[(N[(x$46$re * x$46$im), $MachinePrecision] + N[(x$46$im * x$46$re), $MachinePrecision]), $MachinePrecision] * x$46$im), $MachinePrecision]), $MachinePrecision]
code[x$46$re_, x$46$im_] := Block[{t$95$0 = N[(x$46$im * N[(N[(x$46$re * x$46$im), $MachinePrecision] * -2.0), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[x$46$im, -1e+150], N[(N[(x$46$re * x$46$im), $MachinePrecision] * N[(x$46$im * -3.0), $MachinePrecision]), $MachinePrecision], If[LessEqual[x$46$im, 2.086925204876773e+99], N[(N[(x$46$re * N[(N[(x$46$re * x$46$re), $MachinePrecision] - N[(x$46$im * x$46$im), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + t$95$0), $MachinePrecision], N[(N[(x$46$re + x$46$im), $MachinePrecision] * N[(x$46$im * (-x$46$re)), $MachinePrecision] + t$95$0), $MachinePrecision]]]]
\left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.re - \left(x.re \cdot x.im + x.im \cdot x.re\right) \cdot x.im
\begin{array}{l}
t_0 := x.im \cdot \left(\left(x.re \cdot x.im\right) \cdot -2\right)\\
\mathbf{if}\;x.im \leq -1 \cdot 10^{+150}:\\
\;\;\;\;\left(x.re \cdot x.im\right) \cdot \left(x.im \cdot -3\right)\\

\mathbf{elif}\;x.im \leq 2.086925204876773 \cdot 10^{+99}:\\
\;\;\;\;x.re \cdot \left(x.re \cdot x.re - x.im \cdot x.im\right) + t_0\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(x.re + x.im, x.im \cdot \left(-x.re\right), t_0\right)\\


\end{array}

Error

Target

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

Derivation

  1. Split input into 3 regimes
  2. if x.im < -9.99999999999999981e149

    1. Initial program 61.3

      \[\left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.re - \left(x.re \cdot x.im + x.im \cdot x.re\right) \cdot x.im \]
    2. Simplified61.3

      \[\leadsto \color{blue}{\mathsf{fma}\left(x.re, x.im \cdot \left(x.im \cdot -3\right), {x.re}^{3}\right)} \]
      Proof
      (fma.f64 x.re (*.f64 x.im (*.f64 x.im -3)) (pow.f64 x.re 3)): 0 points increase in error, 0 points decrease in error
      (fma.f64 x.re (*.f64 x.im (*.f64 x.im (Rewrite<= metadata-eval (-.f64 -1 2)))) (pow.f64 x.re 3)): 0 points increase in error, 0 points decrease in error
      (fma.f64 x.re (Rewrite<= associate-*l*_binary64 (*.f64 (*.f64 x.im x.im) (-.f64 -1 2))) (pow.f64 x.re 3)): 13 points increase in error, 16 points decrease in error
      (fma.f64 x.re (Rewrite<= distribute-rgt-out--_binary64 (-.f64 (*.f64 -1 (*.f64 x.im x.im)) (*.f64 2 (*.f64 x.im x.im)))) (pow.f64 x.re 3)): 0 points increase in error, 0 points decrease in error
      (fma.f64 x.re (-.f64 (Rewrite<= neg-mul-1_binary64 (neg.f64 (*.f64 x.im x.im))) (*.f64 2 (*.f64 x.im x.im))) (pow.f64 x.re 3)): 0 points increase in error, 0 points decrease in error
      (fma.f64 x.re (-.f64 (Rewrite<= distribute-lft-neg-out_binary64 (*.f64 (neg.f64 x.im) x.im)) (*.f64 2 (*.f64 x.im x.im))) (pow.f64 x.re 3)): 0 points increase in error, 0 points decrease in error
      (fma.f64 x.re (-.f64 (*.f64 (neg.f64 x.im) x.im) (Rewrite<= associate-*l*_binary64 (*.f64 (*.f64 2 x.im) x.im))) (pow.f64 x.re 3)): 0 points increase in error, 0 points decrease in error
      (fma.f64 x.re (-.f64 (*.f64 (neg.f64 x.im) x.im) (*.f64 (Rewrite<= count-2_binary64 (+.f64 x.im x.im)) x.im)) (pow.f64 x.re 3)): 0 points increase in error, 0 points decrease in error
      (fma.f64 x.re (Rewrite<= unsub-neg_binary64 (+.f64 (*.f64 (neg.f64 x.im) x.im) (neg.f64 (*.f64 (+.f64 x.im x.im) x.im)))) (pow.f64 x.re 3)): 0 points increase in error, 0 points decrease in error
      (fma.f64 x.re (+.f64 (*.f64 (neg.f64 x.im) x.im) (Rewrite<= distribute-rgt-neg-out_binary64 (*.f64 (+.f64 x.im x.im) (neg.f64 x.im)))) (pow.f64 x.re 3)): 0 points increase in error, 0 points decrease in error
      (fma.f64 x.re (+.f64 (*.f64 (neg.f64 x.im) x.im) (*.f64 (+.f64 x.im x.im) (neg.f64 x.im))) (Rewrite=> unpow3_binary64 (*.f64 (*.f64 x.re x.re) x.re))): 20 points increase in error, 0 points decrease in error
      (Rewrite<= fma-def_binary64 (+.f64 (*.f64 x.re (+.f64 (*.f64 (neg.f64 x.im) x.im) (*.f64 (+.f64 x.im x.im) (neg.f64 x.im)))) (*.f64 (*.f64 x.re x.re) x.re))): 0 points increase in error, 0 points decrease in error
      (+.f64 (*.f64 x.re (+.f64 (*.f64 (neg.f64 x.im) x.im) (*.f64 (+.f64 x.im x.im) (neg.f64 x.im)))) (Rewrite=> associate-*l*_binary64 (*.f64 x.re (*.f64 x.re x.re)))): 0 points increase in error, 0 points decrease in error
      (Rewrite=> distribute-lft-out_binary64 (*.f64 x.re (+.f64 (+.f64 (*.f64 (neg.f64 x.im) x.im) (*.f64 (+.f64 x.im x.im) (neg.f64 x.im))) (*.f64 x.re x.re)))): 1 points increase in error, 0 points decrease in error
      (*.f64 x.re (Rewrite<= +-commutative_binary64 (+.f64 (*.f64 x.re x.re) (+.f64 (*.f64 (neg.f64 x.im) x.im) (*.f64 (+.f64 x.im x.im) (neg.f64 x.im)))))): 0 points increase in error, 0 points decrease in error
      (*.f64 x.re (Rewrite<= associate-+l+_binary64 (+.f64 (+.f64 (*.f64 x.re x.re) (*.f64 (neg.f64 x.im) x.im)) (*.f64 (+.f64 x.im x.im) (neg.f64 x.im))))): 0 points increase in error, 1 points decrease in error
      (*.f64 x.re (+.f64 (Rewrite<= cancel-sign-sub-inv_binary64 (-.f64 (*.f64 x.re x.re) (*.f64 x.im x.im))) (*.f64 (+.f64 x.im x.im) (neg.f64 x.im)))): 0 points increase in error, 0 points decrease in error
      (Rewrite<= distribute-lft-out_binary64 (+.f64 (*.f64 x.re (-.f64 (*.f64 x.re x.re) (*.f64 x.im x.im))) (*.f64 x.re (*.f64 (+.f64 x.im x.im) (neg.f64 x.im))))): 18 points increase in error, 13 points decrease in error
      (+.f64 (*.f64 x.re (-.f64 (*.f64 x.re x.re) (*.f64 x.im x.im))) (Rewrite<= associate-*l*_binary64 (*.f64 (*.f64 x.re (+.f64 x.im x.im)) (neg.f64 x.im)))): 1 points increase in error, 12 points decrease in error
      (+.f64 (*.f64 x.re (-.f64 (*.f64 x.re x.re) (*.f64 x.im x.im))) (*.f64 (Rewrite<= distribute-rgt-out_binary64 (+.f64 (*.f64 x.im x.re) (*.f64 x.im x.re))) (neg.f64 x.im))): 0 points increase in error, 0 points decrease in error
      (+.f64 (*.f64 x.re (-.f64 (*.f64 x.re x.re) (*.f64 x.im x.im))) (*.f64 (+.f64 (Rewrite=> *-commutative_binary64 (*.f64 x.re x.im)) (*.f64 x.im x.re)) (neg.f64 x.im))): 0 points increase in error, 0 points decrease in error
      (+.f64 (*.f64 x.re (-.f64 (*.f64 x.re x.re) (*.f64 x.im x.im))) (Rewrite<= distribute-rgt-neg-in_binary64 (neg.f64 (*.f64 (+.f64 (*.f64 x.re x.im) (*.f64 x.im x.re)) x.im)))): 0 points increase in error, 0 points decrease in error
      (+.f64 (Rewrite<= *-commutative_binary64 (*.f64 (-.f64 (*.f64 x.re x.re) (*.f64 x.im x.im)) x.re)) (neg.f64 (*.f64 (+.f64 (*.f64 x.re x.im) (*.f64 x.im x.re)) x.im))): 0 points increase in error, 0 points decrease in error
      (Rewrite<= sub-neg_binary64 (-.f64 (*.f64 (-.f64 (*.f64 x.re x.re) (*.f64 x.im x.im)) x.re) (*.f64 (+.f64 (*.f64 x.re x.im) (*.f64 x.im x.re)) x.im))): 0 points increase in error, 0 points decrease in error
    3. Taylor expanded in x.re around 0 61.3

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

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

      \[\leadsto \color{blue}{\left(1 + -3 \cdot \left(\left(x.re \cdot x.im\right) \cdot x.im\right)\right) - 1} \]
    6. Applied egg-rr0.4

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

    if -9.99999999999999981e149 < x.im < 2.08692520487677295e99

    1. Initial program 0.2

      \[\left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.re - \left(x.re \cdot x.im + x.im \cdot x.re\right) \cdot x.im \]
    2. Applied egg-rr0.2

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

    if 2.08692520487677295e99 < x.im

    1. Initial program 35.3

      \[\left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.re - \left(x.re \cdot x.im + x.im \cdot x.re\right) \cdot x.im \]
    2. Applied egg-rr35.3

      \[\leadsto \left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.re - \color{blue}{\left(\left(x.re \cdot x.im\right) \cdot 2\right)} \cdot x.im \]
    3. Applied egg-rr0.3

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

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

      \[\leadsto \mathsf{fma}\left(x.re + x.im, \color{blue}{x.im \cdot \left(-x.re\right)}, x.im \cdot \left(\left(x.re \cdot x.im\right) \cdot -2\right)\right) \]
      Proof
      (*.f64 x.im (neg.f64 x.re)): 0 points increase in error, 0 points decrease in error
      (Rewrite<= distribute-rgt-neg-in_binary64 (neg.f64 (*.f64 x.im x.re))): 0 points increase in error, 0 points decrease in error
      (neg.f64 (Rewrite<= *-commutative_binary64 (*.f64 x.re x.im))): 0 points increase in error, 0 points decrease in error
      (Rewrite<= mul-1-neg_binary64 (*.f64 -1 (*.f64 x.re x.im))): 0 points increase in error, 0 points decrease in error
  3. Recombined 3 regimes into one program.
  4. Final simplification0.2

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

Alternatives

Alternative 1
Error0.2
Cost7360
\[\mathsf{fma}\left(x.re + x.im, x.re \cdot \left(x.re - x.im\right), x.im \cdot \left(\left(x.re \cdot x.im\right) \cdot -2\right)\right) \]
Alternative 2
Error0.5
Cost3656
\[\begin{array}{l} t_0 := x.re \cdot \left(x.re \cdot x.re - x.im \cdot x.im\right)\\ t_1 := t_0 - x.im \cdot \left(x.re \cdot x.im + x.re \cdot x.im\right)\\ \mathbf{if}\;t_1 \leq -\infty:\\ \;\;\;\;\left(x.re \cdot x.im\right) \cdot \left(x.im \cdot -3\right)\\ \mathbf{elif}\;t_1 \leq 10^{+294}:\\ \;\;\;\;t_0 + x.im \cdot \left(\left(x.re \cdot x.im\right) \cdot -2\right)\\ \mathbf{else}:\\ \;\;\;\;-3 \cdot \left(x.im \cdot \left(x.re \cdot x.im\right)\right)\\ \end{array} \]
Alternative 3
Error0.3
Cost968
\[\begin{array}{l} t_0 := -3 \cdot \left(x.im \cdot \left(x.re \cdot x.im\right)\right)\\ \mathbf{if}\;x.im \leq -9.716332315495849 \cdot 10^{+89}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x.im \leq 10^{+144}:\\ \;\;\;\;x.re \cdot \left(x.re \cdot x.re + -3 \cdot \left(x.im \cdot x.im\right)\right)\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 4
Error19.4
Cost576
\[\left(x.re \cdot x.im\right) \cdot \left(x.im \cdot -3 - x.re\right) \]
Alternative 5
Error19.4
Cost448
\[-3 \cdot \left(x.im \cdot \left(x.re \cdot x.im\right)\right) \]
Alternative 6
Error19.4
Cost448
\[\left(x.re \cdot x.im\right) \cdot \left(x.im \cdot -3\right) \]
Alternative 7
Error46.7
Cost384
\[x.im \cdot \left(x.re \cdot \left(-x.re\right)\right) \]

Error

Reproduce

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

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

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