math.cube on complex, imaginary part

?

Percentage Accurate: 82.7% → 96.7%
Time: 6.1s
Precision: binary64
Cost: 7492

?

\[\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} x.re = |x.re|\\ \\ \begin{array}{l} \mathbf{if}\;x.re \leq 7.6 \cdot 10^{+153}:\\ \;\;\;\;x.im \cdot \left(3 \cdot \left(x.re \cdot x.re\right) - x.im \cdot x.im\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(x.im \cdot \left(x.re \cdot 2\right), x.re, \left(x.im \cdot \left(x.re + x.im\right)\right) \cdot \left(x.re - x.im\right)\right)\\ \end{array} \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)))
NOTE: x.re should be positive before calling this function
(FPCore (x.re x.im)
 :precision binary64
 (if (<= x.re 7.6e+153)
   (* x.im (- (* 3.0 (* x.re x.re)) (* x.im x.im)))
   (fma (* x.im (* x.re 2.0)) x.re (* (* x.im (+ x.re x.im)) (- x.re 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);
}
x.re = abs(x.re);
double code(double x_46_re, double x_46_im) {
	double tmp;
	if (x_46_re <= 7.6e+153) {
		tmp = x_46_im * ((3.0 * (x_46_re * x_46_re)) - (x_46_im * x_46_im));
	} else {
		tmp = fma((x_46_im * (x_46_re * 2.0)), x_46_re, ((x_46_im * (x_46_re + x_46_im)) * (x_46_re - 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
x.re = abs(x.re)
function code(x_46_re, x_46_im)
	tmp = 0.0
	if (x_46_re <= 7.6e+153)
		tmp = Float64(x_46_im * Float64(Float64(3.0 * Float64(x_46_re * x_46_re)) - Float64(x_46_im * x_46_im)));
	else
		tmp = fma(Float64(x_46_im * Float64(x_46_re * 2.0)), x_46_re, Float64(Float64(x_46_im * Float64(x_46_re + x_46_im)) * Float64(x_46_re - 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]
NOTE: x.re should be positive before calling this function
code[x$46$re_, x$46$im_] := If[LessEqual[x$46$re, 7.6e+153], N[(x$46$im * N[(N[(3.0 * N[(x$46$re * x$46$re), $MachinePrecision]), $MachinePrecision] - N[(x$46$im * x$46$im), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(x$46$im * N[(x$46$re * 2.0), $MachinePrecision]), $MachinePrecision] * x$46$re + N[(N[(x$46$im * N[(x$46$re + x$46$im), $MachinePrecision]), $MachinePrecision] * N[(x$46$re - 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}
x.re = |x.re|\\
\\
\begin{array}{l}
\mathbf{if}\;x.re \leq 7.6 \cdot 10^{+153}:\\
\;\;\;\;x.im \cdot \left(3 \cdot \left(x.re \cdot x.re\right) - x.im \cdot x.im\right)\\

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


\end{array}
\end{array}

Local Percentage Accuracy vs ?

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.

Herbie found 7 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Bogosity?

Bogosity

Target

Original82.7%
Target91.6%
Herbie96.7%
\[\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 x.re < 7.59999999999999933e153

    1. Initial program 87.7%

      \[\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 87.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. Simplified87.6%

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

      [Start]87.6%

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

      associate-*r* [=>]87.6%

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

      *-commutative [=>]87.6%

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

      unpow2 [=>]87.6%

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

      associate-*r* [=>]87.6%

      \[ \left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.im + x.im \cdot \color{blue}{\left(\left(2 \cdot x.re\right) \cdot x.re\right)} \]
    4. Applied egg-rr46.1%

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

      [Start]87.6%

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

      expm1-log1p-u [=>]61.4%

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

      expm1-udef [=>]42.0%

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

      +-commutative [=>]42.0%

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

      *-commutative [=>]42.0%

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

      distribute-lft-out [=>]46.1%

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

      *-commutative [=>]46.1%

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

      *-commutative [=>]46.1%

      \[ e^{\mathsf{log1p}\left(x.im \cdot \left(x.re \cdot \color{blue}{\left(x.re \cdot 2\right)} + \left(x.re \cdot x.re - x.im \cdot x.im\right)\right)\right)} - 1 \]
    5. Simplified94.1%

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

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

      expm1-def [=>]65.6%

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

      expm1-log1p [=>]94.0%

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

      unpow2 [<=]94.0%

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

      associate-+r- [=>]94.1%

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

      associate-*r* [=>]94.1%

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

      unpow2 [<=]94.1%

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

      *-commutative [<=]94.1%

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

      distribute-lft1-in [=>]94.1%

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

      metadata-eval [=>]94.1%

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

      unpow2 [=>]94.1%

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

    if 7.59999999999999933e153 < x.re

    1. Initial program 48.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. Taylor expanded in x.re around 0 48.0%

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

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

      [Start]48.0%

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

      associate-*r* [=>]48.0%

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

      *-commutative [=>]48.0%

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

      unpow2 [=>]48.0%

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

      associate-*r* [=>]48.0%

      \[ \left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.im + x.im \cdot \color{blue}{\left(\left(2 \cdot x.re\right) \cdot x.re\right)} \]
    4. Applied egg-rr89.1%

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

      [Start]48.0%

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

      +-commutative [=>]48.0%

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

      associate-*r* [=>]48.0%

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

      fma-def [=>]48.0%

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

      *-commutative [=>]48.0%

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

      *-commutative [=>]48.0%

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

      difference-of-squares [=>]64.2%

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

      associate-*r* [=>]89.1%

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

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

Alternatives

Alternative 1
Accuracy96.7%
Cost7492
\[\begin{array}{l} \mathbf{if}\;x.re \leq 7.6 \cdot 10^{+153}:\\ \;\;\;\;x.im \cdot \left(3 \cdot \left(x.re \cdot x.re\right) - x.im \cdot x.im\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(x.im \cdot \left(x.re \cdot 2\right), x.re, \left(x.im \cdot \left(x.re + x.im\right)\right) \cdot \left(x.re - x.im\right)\right)\\ \end{array} \]
Alternative 2
Accuracy96.7%
Cost836
\[\begin{array}{l} \mathbf{if}\;x.re \leq 7.6 \cdot 10^{+153}:\\ \;\;\;\;x.im \cdot \left(3 \cdot \left(x.re \cdot x.re\right) - x.im \cdot x.im\right)\\ \mathbf{else}:\\ \;\;\;\;\left(x.re \cdot 3\right) \cdot \left(x.re \cdot x.im\right)\\ \end{array} \]
Alternative 3
Accuracy77.4%
Cost580
\[\begin{array}{l} \mathbf{if}\;x.re \leq 6.1 \cdot 10^{-15}:\\ \;\;\;\;x.im \cdot \left(x.im \cdot \left(-x.im\right)\right)\\ \mathbf{else}:\\ \;\;\;\;3 \cdot \left(x.im \cdot \left(x.re \cdot x.re\right)\right)\\ \end{array} \]
Alternative 4
Accuracy83.0%
Cost580
\[\begin{array}{l} \mathbf{if}\;x.re \leq 1.9 \cdot 10^{-14}:\\ \;\;\;\;x.im \cdot \left(x.im \cdot \left(-x.im\right)\right)\\ \mathbf{else}:\\ \;\;\;\;3 \cdot \left(x.re \cdot \left(x.re \cdot x.im\right)\right)\\ \end{array} \]
Alternative 5
Accuracy83.0%
Cost580
\[\begin{array}{l} \mathbf{if}\;x.re \leq 2.2 \cdot 10^{-14}:\\ \;\;\;\;x.im \cdot \left(x.im \cdot \left(-x.im\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\left(x.re \cdot 3\right) \cdot \left(x.re \cdot x.im\right)\\ \end{array} \]
Alternative 6
Accuracy59.9%
Cost384
\[x.im \cdot \left(x.im \cdot \left(-x.im\right)\right) \]
Alternative 7
Accuracy2.7%
Cost64
\[-3 \]

Reproduce?

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