math.cube on complex, imaginary part

?

Percentage Accurate: 82.5% → 96.9%
Time: 9.0s
Precision: binary64
Cost: 2500

?

\[\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} t_0 := x.re \cdot \left(x.re \cdot x.im + x.re \cdot x.im\right)\\ \mathbf{if}\;x.im \cdot \left(x.re \cdot x.re - x.im \cdot x.im\right) + t_0 \leq \infty:\\ \;\;\;\;t_0 + \left(x.im \cdot \left(x.re + x.im\right)\right) \cdot \left(x.re - x.im\right)\\ \mathbf{else}:\\ \;\;\;\;\left(x.im \cdot x.im\right) \cdot \left(-x.im\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
 (let* ((t_0 (* x.re (+ (* x.re x.im) (* x.re x.im)))))
   (if (<= (+ (* x.im (- (* x.re x.re) (* x.im x.im))) t_0) INFINITY)
     (+ t_0 (* (* x.im (+ x.re x.im)) (- x.re x.im)))
     (* (* x.im x.im) (- 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 t_0 = x_46_re * ((x_46_re * x_46_im) + (x_46_re * x_46_im));
	double tmp;
	if (((x_46_im * ((x_46_re * x_46_re) - (x_46_im * x_46_im))) + t_0) <= ((double) INFINITY)) {
		tmp = t_0 + ((x_46_im * (x_46_re + x_46_im)) * (x_46_re - x_46_im));
	} else {
		tmp = (x_46_im * x_46_im) * -x_46_im;
	}
	return tmp;
}
public static 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);
}
public static double code(double x_46_re, double x_46_im) {
	double t_0 = x_46_re * ((x_46_re * x_46_im) + (x_46_re * x_46_im));
	double tmp;
	if (((x_46_im * ((x_46_re * x_46_re) - (x_46_im * x_46_im))) + t_0) <= Double.POSITIVE_INFINITY) {
		tmp = t_0 + ((x_46_im * (x_46_re + x_46_im)) * (x_46_re - x_46_im));
	} else {
		tmp = (x_46_im * x_46_im) * -x_46_im;
	}
	return tmp;
}
def code(x_46_re, 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)
def code(x_46_re, x_46_im):
	t_0 = x_46_re * ((x_46_re * x_46_im) + (x_46_re * x_46_im))
	tmp = 0
	if ((x_46_im * ((x_46_re * x_46_re) - (x_46_im * x_46_im))) + t_0) <= math.inf:
		tmp = t_0 + ((x_46_im * (x_46_re + x_46_im)) * (x_46_re - x_46_im))
	else:
		tmp = (x_46_im * x_46_im) * -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)
	t_0 = Float64(x_46_re * Float64(Float64(x_46_re * x_46_im) + Float64(x_46_re * x_46_im)))
	tmp = 0.0
	if (Float64(Float64(x_46_im * Float64(Float64(x_46_re * x_46_re) - Float64(x_46_im * x_46_im))) + t_0) <= Inf)
		tmp = Float64(t_0 + Float64(Float64(x_46_im * Float64(x_46_re + x_46_im)) * Float64(x_46_re - x_46_im)));
	else
		tmp = Float64(Float64(x_46_im * x_46_im) * Float64(-x_46_im));
	end
	return tmp
end
function tmp = code(x_46_re, x_46_im)
	tmp = (((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);
end
function tmp_2 = code(x_46_re, x_46_im)
	t_0 = x_46_re * ((x_46_re * x_46_im) + (x_46_re * x_46_im));
	tmp = 0.0;
	if (((x_46_im * ((x_46_re * x_46_re) - (x_46_im * x_46_im))) + t_0) <= Inf)
		tmp = t_0 + ((x_46_im * (x_46_re + x_46_im)) * (x_46_re - x_46_im));
	else
		tmp = (x_46_im * x_46_im) * -x_46_im;
	end
	tmp_2 = 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_] := Block[{t$95$0 = N[(x$46$re * N[(N[(x$46$re * x$46$im), $MachinePrecision] + N[(x$46$re * x$46$im), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[(N[(x$46$im * N[(N[(x$46$re * x$46$re), $MachinePrecision] - N[(x$46$im * x$46$im), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + t$95$0), $MachinePrecision], Infinity], N[(t$95$0 + N[(N[(x$46$im * N[(x$46$re + x$46$im), $MachinePrecision]), $MachinePrecision] * N[(x$46$re - x$46$im), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(x$46$im * x$46$im), $MachinePrecision] * (-x$46$im)), $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}
t_0 := x.re \cdot \left(x.re \cdot x.im + x.re \cdot x.im\right)\\
\mathbf{if}\;x.im \cdot \left(x.re \cdot x.re - x.im \cdot x.im\right) + t_0 \leq \infty:\\
\;\;\;\;t_0 + \left(x.im \cdot \left(x.re + x.im\right)\right) \cdot \left(x.re - x.im\right)\\

\mathbf{else}:\\
\;\;\;\;\left(x.im \cdot x.im\right) \cdot \left(-x.im\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.

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original82.5%
Target91.1%
Herbie96.9%
\[\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 2 regimes
  2. if (+.f64 (*.f64 (-.f64 (*.f64 x.re x.re) (*.f64 x.im x.im)) x.im) (*.f64 (+.f64 (*.f64 x.re x.im) (*.f64 x.im x.re)) x.re)) < +inf.0

    1. Initial program 91.3%

      \[\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. Applied egg-rr99.7%

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

      [Start]91.3

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

      add-log-exp [=>]56.1

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

      *-un-lft-identity [=>]56.1

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

      log-prod [=>]56.1

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

      metadata-eval [=>]56.1

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

      add-log-exp [<=]91.3

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

      *-commutative [=>]91.3

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

      difference-of-squares [=>]91.3

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

      associate-*r* [=>]99.7

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

    if +inf.0 < (+.f64 (*.f64 (-.f64 (*.f64 x.re x.re) (*.f64 x.im x.im)) x.im) (*.f64 (+.f64 (*.f64 x.re x.im) (*.f64 x.im x.re)) x.re))

    1. Initial program 0.0%

      \[\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. Simplified0.0%

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

      [Start]0.0

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

      +-commutative [=>]0.0

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

      *-commutative [=>]0.0

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

      sub-neg [=>]0.0

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

      distribute-lft-in [=>]0.0

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

      associate-+r+ [=>]0.0

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

      distribute-rgt-neg-out [=>]0.0

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

      unsub-neg [=>]0.0

      \[ \color{blue}{\left(\left(x.re \cdot x.im + x.im \cdot x.re\right) \cdot x.re + x.im \cdot \left(x.re \cdot x.re\right)\right) - x.im \cdot \left(x.im \cdot x.im\right)} \]
    3. Taylor expanded in x.re around 0 0.0%

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

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

      [Start]0.0

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

      fma-def [=>]0.0

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

      unpow2 [=>]0.0

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

      *-commutative [<=]0.0

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

      neg-mul-1 [<=]0.0

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

      fma-neg [<=]0.0

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

      *-commutative [=>]0.0

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

      associate-*r* [=>]0.0

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

      unpow3 [=>]0.0

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

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

      \[ \color{blue}{x.im \cdot \left(3 \cdot \left(x.re \cdot x.re\right) - x.im \cdot x.im\right)} \]
    5. Taylor expanded in x.re around 0 89.5%

      \[\leadsto x.im \cdot \color{blue}{\left(-1 \cdot {x.im}^{2}\right)} \]
    6. Simplified89.5%

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

      [Start]89.5

      \[ x.im \cdot \left(-1 \cdot {x.im}^{2}\right) \]

      unpow2 [=>]89.5

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

      mul-1-neg [=>]89.5

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

      distribute-rgt-neg-out [<=]89.5

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x.im \cdot \left(x.re \cdot x.re - x.im \cdot x.im\right) + x.re \cdot \left(x.re \cdot x.im + x.re \cdot x.im\right) \leq \infty:\\ \;\;\;\;x.re \cdot \left(x.re \cdot x.im + x.re \cdot x.im\right) + \left(x.im \cdot \left(x.re + x.im\right)\right) \cdot \left(x.re - x.im\right)\\ \mathbf{else}:\\ \;\;\;\;\left(x.im \cdot x.im\right) \cdot \left(-x.im\right)\\ \end{array} \]

Alternatives

Alternative 1
Accuracy96.0%
Cost968
\[\begin{array}{l} \mathbf{if}\;x.re \leq -7.6 \cdot 10^{+153}:\\ \;\;\;\;3 \cdot \left(x.re \cdot \left(x.re \cdot x.im\right)\right)\\ \mathbf{elif}\;x.re \leq 3.1 \cdot 10^{+137}:\\ \;\;\;\;x.im \cdot \left(\left(x.re \cdot x.re\right) \cdot 3 - x.im \cdot x.im\right)\\ \mathbf{else}:\\ \;\;\;\;x.re \cdot \left(\left(x.re \cdot x.im\right) \cdot 3\right)\\ \end{array} \]
Alternative 2
Accuracy74.5%
Cost713
\[\begin{array}{l} \mathbf{if}\;x.im \leq -2.05 \cdot 10^{+62} \lor \neg \left(x.im \leq 7.4 \cdot 10^{+59}\right):\\ \;\;\;\;\left(x.im \cdot x.im\right) \cdot \left(-x.im\right)\\ \mathbf{else}:\\ \;\;\;\;3 \cdot \left(\left(x.re \cdot x.re\right) \cdot x.im\right)\\ \end{array} \]
Alternative 3
Accuracy80.1%
Cost713
\[\begin{array}{l} \mathbf{if}\;x.im \leq -2 \cdot 10^{+62} \lor \neg \left(x.im \leq 1.25 \cdot 10^{+61}\right):\\ \;\;\;\;\left(x.im \cdot x.im\right) \cdot \left(-x.im\right)\\ \mathbf{else}:\\ \;\;\;\;3 \cdot \left(x.re \cdot \left(x.re \cdot x.im\right)\right)\\ \end{array} \]
Alternative 4
Accuracy80.2%
Cost713
\[\begin{array}{l} \mathbf{if}\;x.im \leq -1.45 \cdot 10^{+62} \lor \neg \left(x.im \leq 3.7 \cdot 10^{+59}\right):\\ \;\;\;\;\left(x.im \cdot x.im\right) \cdot \left(-x.im\right)\\ \mathbf{else}:\\ \;\;\;\;x.re \cdot \left(\left(x.re \cdot x.im\right) \cdot 3\right)\\ \end{array} \]
Alternative 5
Accuracy80.2%
Cost713
\[\begin{array}{l} \mathbf{if}\;x.im \leq -1.62 \cdot 10^{+62} \lor \neg \left(x.im \leq 8.5 \cdot 10^{+59}\right):\\ \;\;\;\;\left(x.im \cdot x.im\right) \cdot \left(-x.im\right)\\ \mathbf{else}:\\ \;\;\;\;x.re \cdot \left(x.re \cdot \left(x.im \cdot 3\right)\right)\\ \end{array} \]
Alternative 6
Accuracy58.6%
Cost384
\[\left(x.im \cdot x.im\right) \cdot \left(-x.im\right) \]
Alternative 7
Accuracy4.5%
Cost256
\[x.im \cdot \left(-2\right) \]
Alternative 8
Accuracy2.7%
Cost64
\[-1 \]

Error

Reproduce?

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