math.cube on complex, real part

?

Percentage Accurate: 82.5% → 96.7%
Time: 9.9s
Precision: binary64
Cost: 13577

?

\[\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} \mathbf{if}\;x.re \leq -5 \cdot 10^{+103} \lor \neg \left(x.re \leq 5 \cdot 10^{+53}\right):\\ \;\;\;\;x.re \cdot \mathsf{fma}\left(x.re, x.re, x.im \cdot \left(x.im \cdot -3\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(-3 \cdot \left(x.re \cdot x.im\right), x.im, {x.re}^{3}\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
 (if (or (<= x.re -5e+103) (not (<= x.re 5e+53)))
   (* x.re (fma x.re x.re (* x.im (* x.im -3.0))))
   (fma (* -3.0 (* x.re x.im)) x.im (pow x.re 3.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 tmp;
	if ((x_46_re <= -5e+103) || !(x_46_re <= 5e+53)) {
		tmp = x_46_re * fma(x_46_re, x_46_re, (x_46_im * (x_46_im * -3.0)));
	} else {
		tmp = fma((-3.0 * (x_46_re * x_46_im)), x_46_im, pow(x_46_re, 3.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)
	tmp = 0.0
	if ((x_46_re <= -5e+103) || !(x_46_re <= 5e+53))
		tmp = Float64(x_46_re * fma(x_46_re, x_46_re, Float64(x_46_im * Float64(x_46_im * -3.0))));
	else
		tmp = fma(Float64(-3.0 * Float64(x_46_re * x_46_im)), x_46_im, (x_46_re ^ 3.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_] := If[Or[LessEqual[x$46$re, -5e+103], N[Not[LessEqual[x$46$re, 5e+53]], $MachinePrecision]], N[(x$46$re * N[(x$46$re * x$46$re + N[(x$46$im * N[(x$46$im * -3.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(-3.0 * N[(x$46$re * x$46$im), $MachinePrecision]), $MachinePrecision] * x$46$im + N[Power[x$46$re, 3.0], $MachinePrecision]), $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}
\mathbf{if}\;x.re \leq -5 \cdot 10^{+103} \lor \neg \left(x.re \leq 5 \cdot 10^{+53}\right):\\
\;\;\;\;x.re \cdot \mathsf{fma}\left(x.re, x.re, x.im \cdot \left(x.im \cdot -3\right)\right)\\

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


\end{array}

Local Percentage Accuracy?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Target

Original82.5%
Target86.9%
Herbie96.7%
\[\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 2 regimes
  2. if x.re < -5e103 or 5.0000000000000004e53 < x.re

    1. Initial program 69.6%

      \[\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. Simplified92.1%

      \[\leadsto \color{blue}{x.re \cdot \mathsf{fma}\left(x.re, x.re, x.im \cdot \left(x.im \cdot -3\right)\right)} \]
      Step-by-step derivation

      [Start]69.6

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

      *-commutative [<=]69.6

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

      distribute-lft-out [=>]69.6

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

      associate-*l* [=>]69.6

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

      *-commutative [=>]69.6

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

      distribute-rgt-out-- [=>]81.4

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

      associate--l- [=>]81.4

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

      associate--l- [<=]81.4

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

      sub-neg [=>]81.4

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

      associate--l+ [=>]81.4

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

      fma-udef [<=]92.1

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

      neg-mul-1 [=>]92.1

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

      count-2 [=>]92.1

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

      associate-*l* [=>]92.1

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

      distribute-rgt-out-- [=>]92.1

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

      associate-*r* [<=]92.1

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

      metadata-eval [=>]92.1

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

    if -5e103 < x.re < 5.0000000000000004e53

    1. Initial program 91.1%

      \[\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. Simplified91.2%

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

      [Start]91.1

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

      *-commutative [=>]91.1

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

      sub-neg [=>]91.1

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

      distribute-lft-in [=>]91.1

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

      associate--l+ [=>]91.1

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

      cube-unmult [=>]91.2

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

      *-commutative [<=]91.2

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

      distribute-lft-out [=>]91.2

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

      associate-*l* [=>]91.2

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

      distribute-lft-out-- [=>]91.3

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

      neg-mul-1 [=>]91.3

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

      count-2 [=>]91.3

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

      associate-*l* [=>]91.3

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

      distribute-rgt-out-- [=>]91.3

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

      associate-*l* [<=]91.2

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

      metadata-eval [=>]91.2

      \[ {x.re}^{3} + \left(x.re \cdot \left(x.im \cdot x.im\right)\right) \cdot \color{blue}{-3} \]
    3. Applied egg-rr99.8%

      \[\leadsto \color{blue}{\mathsf{fma}\left(-3 \cdot \left(x.re \cdot x.im\right), x.im, {x.re}^{3}\right)} \]
      Step-by-step derivation

      [Start]91.2

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

      +-commutative [=>]91.2

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

      *-commutative [=>]91.2

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

      associate-*r* [=>]99.8

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

      associate-*r* [=>]99.8

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

      fma-def [=>]99.8

      \[ \color{blue}{\mathsf{fma}\left(-3 \cdot \left(x.re \cdot x.im\right), x.im, {x.re}^{3}\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification96.8%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x.re \leq -5 \cdot 10^{+103} \lor \neg \left(x.re \leq 5 \cdot 10^{+53}\right):\\ \;\;\;\;x.re \cdot \mathsf{fma}\left(x.re, x.re, x.im \cdot \left(x.im \cdot -3\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(-3 \cdot \left(x.re \cdot x.im\right), x.im, {x.re}^{3}\right)\\ \end{array} \]

Alternatives

Alternative 1
Accuracy95.8%
Cost7241
\[\begin{array}{l} \mathbf{if}\;x.re \leq -1.5 \cdot 10^{-93} \lor \neg \left(x.re \leq 8 \cdot 10^{-124}\right):\\ \;\;\;\;x.re \cdot \mathsf{fma}\left(x.re, x.re, x.im \cdot \left(x.im \cdot -3\right)\right)\\ \mathbf{else}:\\ \;\;\;\;x.im \cdot \left(-3 \cdot \left(x.re \cdot x.im\right)\right)\\ \end{array} \]
Alternative 2
Accuracy95.2%
Cost1616
\[\begin{array}{l} t_0 := x.re \cdot \left(x.re \cdot x.re\right)\\ t_1 := \left(x.re \cdot \left(x.re + x.im\right)\right) \cdot \left(x.re - x.im\right) - x.re \cdot \left(x.im \cdot \left(x.im + x.im\right)\right)\\ \mathbf{if}\;x.re \leq -4 \cdot 10^{+197}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x.re \leq -2.1 \cdot 10^{-91}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;x.re \leq 2 \cdot 10^{-157}:\\ \;\;\;\;x.im \cdot \left(-3 \cdot \left(x.re \cdot x.im\right)\right)\\ \mathbf{elif}\;x.re \leq 9.2 \cdot 10^{+209}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 3
Accuracy82.4%
Cost978
\[\begin{array}{l} \mathbf{if}\;x.im \leq -4.3 \cdot 10^{+83} \lor \neg \left(x.im \leq -4.7 \cdot 10^{-26} \lor \neg \left(x.im \leq -2.2 \cdot 10^{-40}\right) \land x.im \leq 1500000000000\right):\\ \;\;\;\;-3 \cdot \left(x.im \cdot \left(x.re \cdot x.im\right)\right)\\ \mathbf{else}:\\ \;\;\;\;x.re \cdot \left(x.re \cdot x.re\right)\\ \end{array} \]
Alternative 4
Accuracy82.5%
Cost976
\[\begin{array}{l} t_0 := x.im \cdot \left(-3 \cdot \left(x.re \cdot x.im\right)\right)\\ t_1 := x.re \cdot \left(x.re \cdot x.re\right)\\ \mathbf{if}\;x.im \leq -4.9 \cdot 10^{+82}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x.im \leq -2.5 \cdot 10^{-26}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;x.im \leq -2.4 \cdot 10^{-40}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x.im \leq 5500000000000:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;-3 \cdot \left(x.im \cdot \left(x.re \cdot x.im\right)\right)\\ \end{array} \]
Alternative 5
Accuracy82.4%
Cost976
\[\begin{array}{l} t_0 := x.re \cdot \left(x.re \cdot x.re\right)\\ \mathbf{if}\;x.im \leq -8.5 \cdot 10^{+83}:\\ \;\;\;\;x.im \cdot \left(x.im \cdot \left(x.re \cdot -3\right)\right)\\ \mathbf{elif}\;x.im \leq -2.8 \cdot 10^{-26}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x.im \leq -4.2 \cdot 10^{-40}:\\ \;\;\;\;x.im \cdot \left(-3 \cdot \left(x.re \cdot x.im\right)\right)\\ \mathbf{elif}\;x.im \leq 110000000000:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;-3 \cdot \left(x.im \cdot \left(x.re \cdot x.im\right)\right)\\ \end{array} \]
Alternative 6
Accuracy82.4%
Cost976
\[\begin{array}{l} t_0 := x.re \cdot \left(x.re \cdot x.re\right)\\ \mathbf{if}\;x.im \leq -1.3 \cdot 10^{+80}:\\ \;\;\;\;x.im \cdot \left(x.im \cdot \left(x.re \cdot -3\right)\right)\\ \mathbf{elif}\;x.im \leq -6.6 \cdot 10^{-24}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x.im \leq -1 \cdot 10^{-40}:\\ \;\;\;\;x.re \cdot \left(-3 \cdot \left(x.im \cdot x.im\right)\right)\\ \mathbf{elif}\;x.im \leq 13500000000:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;-3 \cdot \left(x.im \cdot \left(x.re \cdot x.im\right)\right)\\ \end{array} \]
Alternative 7
Accuracy96.6%
Cost969
\[\begin{array}{l} \mathbf{if}\;x.im \leq -2.1 \cdot 10^{+154} \lor \neg \left(x.im \leq 6.6 \cdot 10^{+152}\right):\\ \;\;\;\;-3 \cdot \left(x.im \cdot \left(x.re \cdot x.im\right)\right)\\ \mathbf{else}:\\ \;\;\;\;x.re \cdot \left(x.im \cdot \left(x.im \cdot -3\right) + x.re \cdot x.re\right)\\ \end{array} \]
Alternative 8
Accuracy61.4%
Cost649
\[\begin{array}{l} \mathbf{if}\;x.im \leq -1.58 \cdot 10^{+159} \lor \neg \left(x.im \leq 6.8 \cdot 10^{+195}\right):\\ \;\;\;\;\left(x.re \cdot x.re\right) \cdot \left(-x.im\right)\\ \mathbf{else}:\\ \;\;\;\;x.re \cdot \left(x.re \cdot x.re\right)\\ \end{array} \]
Alternative 9
Accuracy61.4%
Cost648
\[\begin{array}{l} \mathbf{if}\;x.im \leq -2.1 \cdot 10^{+158}:\\ \;\;\;\;\left(x.re \cdot x.re\right) \cdot \left(-x.im\right)\\ \mathbf{elif}\;x.im \leq 5.2 \cdot 10^{+179}:\\ \;\;\;\;x.re \cdot \left(x.re \cdot x.re\right)\\ \mathbf{else}:\\ \;\;\;\;x.re \cdot \left(x.re \cdot \left(-x.im\right)\right)\\ \end{array} \]
Alternative 10
Accuracy59.0%
Cost320
\[x.re \cdot \left(x.re \cdot x.re\right) \]

Error

Reproduce?

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