Linear.Quaternion:$c/ from linear-1.19.1.3, A

Percentage Accurate: 98.4% → 99.7%
Time: 7.3s
Alternatives: 8
Speedup: 0.7×

Specification

?
\[\begin{array}{l} \\ \left(\left(x \cdot y + z \cdot z\right) + z \cdot z\right) + z \cdot z \end{array} \]
(FPCore (x y z)
 :precision binary64
 (+ (+ (+ (* x y) (* z z)) (* z z)) (* z z)))
double code(double x, double y, double z) {
	return (((x * y) + (z * z)) + (z * z)) + (z * z);
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = (((x * y) + (z * z)) + (z * z)) + (z * z)
end function
public static double code(double x, double y, double z) {
	return (((x * y) + (z * z)) + (z * z)) + (z * z);
}
def code(x, y, z):
	return (((x * y) + (z * z)) + (z * z)) + (z * z)
function code(x, y, z)
	return Float64(Float64(Float64(Float64(x * y) + Float64(z * z)) + Float64(z * z)) + Float64(z * z))
end
function tmp = code(x, y, z)
	tmp = (((x * y) + (z * z)) + (z * z)) + (z * z);
end
code[x_, y_, z_] := N[(N[(N[(N[(x * y), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(\left(x \cdot y + z \cdot z\right) + z \cdot z\right) + z \cdot z
\end{array}

Sampling outcomes in binary64 precision:

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.

Accuracy vs Speed?

Herbie found 8 alternatives:

AlternativeAccuracySpeedup
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.

Initial Program: 98.4% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \left(\left(x \cdot y + z \cdot z\right) + z \cdot z\right) + z \cdot z \end{array} \]
(FPCore (x y z)
 :precision binary64
 (+ (+ (+ (* x y) (* z z)) (* z z)) (* z z)))
double code(double x, double y, double z) {
	return (((x * y) + (z * z)) + (z * z)) + (z * z);
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = (((x * y) + (z * z)) + (z * z)) + (z * z)
end function
public static double code(double x, double y, double z) {
	return (((x * y) + (z * z)) + (z * z)) + (z * z);
}
def code(x, y, z):
	return (((x * y) + (z * z)) + (z * z)) + (z * z)
function code(x, y, z)
	return Float64(Float64(Float64(Float64(x * y) + Float64(z * z)) + Float64(z * z)) + Float64(z * z))
end
function tmp = code(x, y, z)
	tmp = (((x * y) + (z * z)) + (z * z)) + (z * z);
end
code[x_, y_, z_] := N[(N[(N[(N[(x * y), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(\left(x \cdot y + z \cdot z\right) + z \cdot z\right) + z \cdot z
\end{array}

Alternative 1: 99.7% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;z \cdot z \leq 10^{+303}:\\ \;\;\;\;z \cdot z + \left(z \cdot z + \left(x \cdot y + z \cdot z\right)\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(y + \frac{\frac{z \cdot 3}{x}}{\frac{1}{z}}\right)\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (<= (* z z) 1e+303)
   (+ (* z z) (+ (* z z) (+ (* x y) (* z z))))
   (* x (+ y (/ (/ (* z 3.0) x) (/ 1.0 z))))))
double code(double x, double y, double z) {
	double tmp;
	if ((z * z) <= 1e+303) {
		tmp = (z * z) + ((z * z) + ((x * y) + (z * z)));
	} else {
		tmp = x * (y + (((z * 3.0) / x) / (1.0 / z)));
	}
	return tmp;
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8) :: tmp
    if ((z * z) <= 1d+303) then
        tmp = (z * z) + ((z * z) + ((x * y) + (z * z)))
    else
        tmp = x * (y + (((z * 3.0d0) / x) / (1.0d0 / z)))
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double tmp;
	if ((z * z) <= 1e+303) {
		tmp = (z * z) + ((z * z) + ((x * y) + (z * z)));
	} else {
		tmp = x * (y + (((z * 3.0) / x) / (1.0 / z)));
	}
	return tmp;
}
def code(x, y, z):
	tmp = 0
	if (z * z) <= 1e+303:
		tmp = (z * z) + ((z * z) + ((x * y) + (z * z)))
	else:
		tmp = x * (y + (((z * 3.0) / x) / (1.0 / z)))
	return tmp
function code(x, y, z)
	tmp = 0.0
	if (Float64(z * z) <= 1e+303)
		tmp = Float64(Float64(z * z) + Float64(Float64(z * z) + Float64(Float64(x * y) + Float64(z * z))));
	else
		tmp = Float64(x * Float64(y + Float64(Float64(Float64(z * 3.0) / x) / Float64(1.0 / z))));
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if ((z * z) <= 1e+303)
		tmp = (z * z) + ((z * z) + ((x * y) + (z * z)));
	else
		tmp = x * (y + (((z * 3.0) / x) / (1.0 / z)));
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := If[LessEqual[N[(z * z), $MachinePrecision], 1e+303], N[(N[(z * z), $MachinePrecision] + N[(N[(z * z), $MachinePrecision] + N[(N[(x * y), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(x * N[(y + N[(N[(N[(z * 3.0), $MachinePrecision] / x), $MachinePrecision] / N[(1.0 / z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;z \cdot z \leq 10^{+303}:\\
\;\;\;\;z \cdot z + \left(z \cdot z + \left(x \cdot y + z \cdot z\right)\right)\\

\mathbf{else}:\\
\;\;\;\;x \cdot \left(y + \frac{\frac{z \cdot 3}{x}}{\frac{1}{z}}\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 z z) < 1e303

    1. Initial program 99.8%

      \[\left(\left(x \cdot y + z \cdot z\right) + z \cdot z\right) + z \cdot z \]
    2. Add Preprocessing

    if 1e303 < (*.f64 z z)

    1. Initial program 90.0%

      \[\left(\left(x \cdot y + z \cdot z\right) + z \cdot z\right) + z \cdot z \]
    2. Step-by-step derivation
      1. associate-+l+90.0%

        \[\leadsto \color{blue}{\left(x \cdot y + \left(z \cdot z + z \cdot z\right)\right)} + z \cdot z \]
      2. associate-+l+90.0%

        \[\leadsto \color{blue}{x \cdot y + \left(\left(z \cdot z + z \cdot z\right) + z \cdot z\right)} \]
      3. +-commutative90.0%

        \[\leadsto \color{blue}{\left(\left(z \cdot z + z \cdot z\right) + z \cdot z\right) + x \cdot y} \]
      4. distribute-lft-out90.0%

        \[\leadsto \left(\color{blue}{z \cdot \left(z + z\right)} + z \cdot z\right) + x \cdot y \]
      5. distribute-lft-out90.0%

        \[\leadsto \color{blue}{z \cdot \left(\left(z + z\right) + z\right)} + x \cdot y \]
      6. fma-define95.0%

        \[\leadsto \color{blue}{\mathsf{fma}\left(z, \left(z + z\right) + z, x \cdot y\right)} \]
      7. remove-double-neg95.0%

        \[\leadsto \mathsf{fma}\left(z, \left(z + z\right) + \color{blue}{\left(-\left(-z\right)\right)}, x \cdot y\right) \]
      8. unsub-neg95.0%

        \[\leadsto \mathsf{fma}\left(z, \color{blue}{\left(z + z\right) - \left(-z\right)}, x \cdot y\right) \]
      9. count-295.0%

        \[\leadsto \mathsf{fma}\left(z, \color{blue}{2 \cdot z} - \left(-z\right), x \cdot y\right) \]
      10. neg-mul-195.0%

        \[\leadsto \mathsf{fma}\left(z, 2 \cdot z - \color{blue}{-1 \cdot z}, x \cdot y\right) \]
      11. distribute-rgt-out--95.0%

        \[\leadsto \mathsf{fma}\left(z, \color{blue}{z \cdot \left(2 - -1\right)}, x \cdot y\right) \]
      12. metadata-eval95.0%

        \[\leadsto \mathsf{fma}\left(z, z \cdot \color{blue}{3}, x \cdot y\right) \]
    3. Simplified95.0%

      \[\leadsto \color{blue}{\mathsf{fma}\left(z, z \cdot 3, x \cdot y\right)} \]
    4. Add Preprocessing
    5. Taylor expanded in x around inf 95.0%

      \[\leadsto \color{blue}{x \cdot \left(y + 3 \cdot \frac{{z}^{2}}{x}\right)} \]
    6. Step-by-step derivation
      1. unpow295.0%

        \[\leadsto x \cdot \left(y + 3 \cdot \frac{\color{blue}{z \cdot z}}{x}\right) \]
      2. associate-/l*100.0%

        \[\leadsto x \cdot \left(y + 3 \cdot \color{blue}{\left(z \cdot \frac{z}{x}\right)}\right) \]
    7. Applied egg-rr100.0%

      \[\leadsto x \cdot \left(y + 3 \cdot \color{blue}{\left(z \cdot \frac{z}{x}\right)}\right) \]
    8. Step-by-step derivation
      1. clear-num100.0%

        \[\leadsto x \cdot \left(y + 3 \cdot \left(z \cdot \color{blue}{\frac{1}{\frac{x}{z}}}\right)\right) \]
      2. un-div-inv100.0%

        \[\leadsto x \cdot \left(y + 3 \cdot \color{blue}{\frac{z}{\frac{x}{z}}}\right) \]
    9. Applied egg-rr100.0%

      \[\leadsto x \cdot \left(y + 3 \cdot \color{blue}{\frac{z}{\frac{x}{z}}}\right) \]
    10. Step-by-step derivation
      1. associate-*r/100.0%

        \[\leadsto x \cdot \left(y + \color{blue}{\frac{3 \cdot z}{\frac{x}{z}}}\right) \]
      2. div-inv100.0%

        \[\leadsto x \cdot \left(y + \frac{3 \cdot z}{\color{blue}{x \cdot \frac{1}{z}}}\right) \]
      3. associate-/r*100.0%

        \[\leadsto x \cdot \left(y + \color{blue}{\frac{\frac{3 \cdot z}{x}}{\frac{1}{z}}}\right) \]
    11. Applied egg-rr100.0%

      \[\leadsto x \cdot \left(y + \color{blue}{\frac{\frac{3 \cdot z}{x}}{\frac{1}{z}}}\right) \]
  3. Recombined 2 regimes into one program.
  4. Final simplification99.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \cdot z \leq 10^{+303}:\\ \;\;\;\;z \cdot z + \left(z \cdot z + \left(x \cdot y + z \cdot z\right)\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(y + \frac{\frac{z \cdot 3}{x}}{\frac{1}{z}}\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 2: 98.9% accurate, 0.1× speedup?

\[\begin{array}{l} \\ \mathsf{fma}\left(z, z \cdot 3, x \cdot y\right) \end{array} \]
(FPCore (x y z) :precision binary64 (fma z (* z 3.0) (* x y)))
double code(double x, double y, double z) {
	return fma(z, (z * 3.0), (x * y));
}
function code(x, y, z)
	return fma(z, Float64(z * 3.0), Float64(x * y))
end
code[x_, y_, z_] := N[(z * N[(z * 3.0), $MachinePrecision] + N[(x * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\mathsf{fma}\left(z, z \cdot 3, x \cdot y\right)
\end{array}
Derivation
  1. Initial program 96.7%

    \[\left(\left(x \cdot y + z \cdot z\right) + z \cdot z\right) + z \cdot z \]
  2. Step-by-step derivation
    1. associate-+l+96.8%

      \[\leadsto \color{blue}{\left(x \cdot y + \left(z \cdot z + z \cdot z\right)\right)} + z \cdot z \]
    2. associate-+l+96.8%

      \[\leadsto \color{blue}{x \cdot y + \left(\left(z \cdot z + z \cdot z\right) + z \cdot z\right)} \]
    3. +-commutative96.8%

      \[\leadsto \color{blue}{\left(\left(z \cdot z + z \cdot z\right) + z \cdot z\right) + x \cdot y} \]
    4. distribute-lft-out96.8%

      \[\leadsto \left(\color{blue}{z \cdot \left(z + z\right)} + z \cdot z\right) + x \cdot y \]
    5. distribute-lft-out96.8%

      \[\leadsto \color{blue}{z \cdot \left(\left(z + z\right) + z\right)} + x \cdot y \]
    6. fma-define98.3%

      \[\leadsto \color{blue}{\mathsf{fma}\left(z, \left(z + z\right) + z, x \cdot y\right)} \]
    7. remove-double-neg98.3%

      \[\leadsto \mathsf{fma}\left(z, \left(z + z\right) + \color{blue}{\left(-\left(-z\right)\right)}, x \cdot y\right) \]
    8. unsub-neg98.3%

      \[\leadsto \mathsf{fma}\left(z, \color{blue}{\left(z + z\right) - \left(-z\right)}, x \cdot y\right) \]
    9. count-298.3%

      \[\leadsto \mathsf{fma}\left(z, \color{blue}{2 \cdot z} - \left(-z\right), x \cdot y\right) \]
    10. neg-mul-198.3%

      \[\leadsto \mathsf{fma}\left(z, 2 \cdot z - \color{blue}{-1 \cdot z}, x \cdot y\right) \]
    11. distribute-rgt-out--98.3%

      \[\leadsto \mathsf{fma}\left(z, \color{blue}{z \cdot \left(2 - -1\right)}, x \cdot y\right) \]
    12. metadata-eval98.3%

      \[\leadsto \mathsf{fma}\left(z, z \cdot \color{blue}{3}, x \cdot y\right) \]
  3. Simplified98.3%

    \[\leadsto \color{blue}{\mathsf{fma}\left(z, z \cdot 3, x \cdot y\right)} \]
  4. Add Preprocessing
  5. Add Preprocessing

Alternative 3: 86.2% accurate, 1.1× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;z \cdot z \leq 10000000000000:\\ \;\;\;\;x \cdot y + z \cdot z\\ \mathbf{else}:\\ \;\;\;\;z \cdot \left(z \cdot 3\right)\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (<= (* z z) 10000000000000.0) (+ (* x y) (* z z)) (* z (* z 3.0))))
double code(double x, double y, double z) {
	double tmp;
	if ((z * z) <= 10000000000000.0) {
		tmp = (x * y) + (z * z);
	} else {
		tmp = z * (z * 3.0);
	}
	return tmp;
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8) :: tmp
    if ((z * z) <= 10000000000000.0d0) then
        tmp = (x * y) + (z * z)
    else
        tmp = z * (z * 3.0d0)
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double tmp;
	if ((z * z) <= 10000000000000.0) {
		tmp = (x * y) + (z * z);
	} else {
		tmp = z * (z * 3.0);
	}
	return tmp;
}
def code(x, y, z):
	tmp = 0
	if (z * z) <= 10000000000000.0:
		tmp = (x * y) + (z * z)
	else:
		tmp = z * (z * 3.0)
	return tmp
function code(x, y, z)
	tmp = 0.0
	if (Float64(z * z) <= 10000000000000.0)
		tmp = Float64(Float64(x * y) + Float64(z * z));
	else
		tmp = Float64(z * Float64(z * 3.0));
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if ((z * z) <= 10000000000000.0)
		tmp = (x * y) + (z * z);
	else
		tmp = z * (z * 3.0);
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := If[LessEqual[N[(z * z), $MachinePrecision], 10000000000000.0], N[(N[(x * y), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision], N[(z * N[(z * 3.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;z \cdot z \leq 10000000000000:\\
\;\;\;\;x \cdot y + z \cdot z\\

\mathbf{else}:\\
\;\;\;\;z \cdot \left(z \cdot 3\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 z z) < 1e13

    1. Initial program 99.9%

      \[\left(\left(x \cdot y + z \cdot z\right) + z \cdot z\right) + z \cdot z \]
    2. Add Preprocessing
    3. Taylor expanded in x around inf 89.9%

      \[\leadsto \left(\color{blue}{x \cdot y} + z \cdot z\right) + z \cdot z \]
    4. Taylor expanded in x around inf 89.6%

      \[\leadsto \color{blue}{x \cdot y} + z \cdot z \]

    if 1e13 < (*.f64 z z)

    1. Initial program 93.9%

      \[\left(\left(x \cdot y + z \cdot z\right) + z \cdot z\right) + z \cdot z \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0 88.3%

      \[\leadsto \color{blue}{2 \cdot {z}^{2} + {z}^{2}} \]
    4. Step-by-step derivation
      1. distribute-lft1-in88.3%

        \[\leadsto \color{blue}{\left(2 + 1\right) \cdot {z}^{2}} \]
      2. metadata-eval88.3%

        \[\leadsto \color{blue}{3} \cdot {z}^{2} \]
      3. *-commutative88.3%

        \[\leadsto \color{blue}{{z}^{2} \cdot 3} \]
    5. Simplified88.3%

      \[\leadsto \color{blue}{{z}^{2} \cdot 3} \]
    6. Step-by-step derivation
      1. *-commutative88.3%

        \[\leadsto \color{blue}{3 \cdot {z}^{2}} \]
      2. unpow288.3%

        \[\leadsto 3 \cdot \color{blue}{\left(z \cdot z\right)} \]
      3. associate-*r*88.2%

        \[\leadsto \color{blue}{\left(3 \cdot z\right) \cdot z} \]
      4. *-commutative88.2%

        \[\leadsto \color{blue}{\left(z \cdot 3\right)} \cdot z \]
    7. Applied egg-rr88.2%

      \[\leadsto \color{blue}{\left(z \cdot 3\right) \cdot z} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification88.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \cdot z \leq 10000000000000:\\ \;\;\;\;x \cdot y + z \cdot z\\ \mathbf{else}:\\ \;\;\;\;z \cdot \left(z \cdot 3\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 93.5% accurate, 1.2× speedup?

\[\begin{array}{l} \\ x \cdot \left(y + \frac{\frac{z \cdot 3}{x}}{\frac{1}{z}}\right) \end{array} \]
(FPCore (x y z) :precision binary64 (* x (+ y (/ (/ (* z 3.0) x) (/ 1.0 z)))))
double code(double x, double y, double z) {
	return x * (y + (((z * 3.0) / x) / (1.0 / z)));
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = x * (y + (((z * 3.0d0) / x) / (1.0d0 / z)))
end function
public static double code(double x, double y, double z) {
	return x * (y + (((z * 3.0) / x) / (1.0 / z)));
}
def code(x, y, z):
	return x * (y + (((z * 3.0) / x) / (1.0 / z)))
function code(x, y, z)
	return Float64(x * Float64(y + Float64(Float64(Float64(z * 3.0) / x) / Float64(1.0 / z))))
end
function tmp = code(x, y, z)
	tmp = x * (y + (((z * 3.0) / x) / (1.0 / z)));
end
code[x_, y_, z_] := N[(x * N[(y + N[(N[(N[(z * 3.0), $MachinePrecision] / x), $MachinePrecision] / N[(1.0 / z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x \cdot \left(y + \frac{\frac{z \cdot 3}{x}}{\frac{1}{z}}\right)
\end{array}
Derivation
  1. Initial program 96.7%

    \[\left(\left(x \cdot y + z \cdot z\right) + z \cdot z\right) + z \cdot z \]
  2. Step-by-step derivation
    1. associate-+l+96.8%

      \[\leadsto \color{blue}{\left(x \cdot y + \left(z \cdot z + z \cdot z\right)\right)} + z \cdot z \]
    2. associate-+l+96.8%

      \[\leadsto \color{blue}{x \cdot y + \left(\left(z \cdot z + z \cdot z\right) + z \cdot z\right)} \]
    3. +-commutative96.8%

      \[\leadsto \color{blue}{\left(\left(z \cdot z + z \cdot z\right) + z \cdot z\right) + x \cdot y} \]
    4. distribute-lft-out96.8%

      \[\leadsto \left(\color{blue}{z \cdot \left(z + z\right)} + z \cdot z\right) + x \cdot y \]
    5. distribute-lft-out96.8%

      \[\leadsto \color{blue}{z \cdot \left(\left(z + z\right) + z\right)} + x \cdot y \]
    6. fma-define98.3%

      \[\leadsto \color{blue}{\mathsf{fma}\left(z, \left(z + z\right) + z, x \cdot y\right)} \]
    7. remove-double-neg98.3%

      \[\leadsto \mathsf{fma}\left(z, \left(z + z\right) + \color{blue}{\left(-\left(-z\right)\right)}, x \cdot y\right) \]
    8. unsub-neg98.3%

      \[\leadsto \mathsf{fma}\left(z, \color{blue}{\left(z + z\right) - \left(-z\right)}, x \cdot y\right) \]
    9. count-298.3%

      \[\leadsto \mathsf{fma}\left(z, \color{blue}{2 \cdot z} - \left(-z\right), x \cdot y\right) \]
    10. neg-mul-198.3%

      \[\leadsto \mathsf{fma}\left(z, 2 \cdot z - \color{blue}{-1 \cdot z}, x \cdot y\right) \]
    11. distribute-rgt-out--98.3%

      \[\leadsto \mathsf{fma}\left(z, \color{blue}{z \cdot \left(2 - -1\right)}, x \cdot y\right) \]
    12. metadata-eval98.3%

      \[\leadsto \mathsf{fma}\left(z, z \cdot \color{blue}{3}, x \cdot y\right) \]
  3. Simplified98.3%

    \[\leadsto \color{blue}{\mathsf{fma}\left(z, z \cdot 3, x \cdot y\right)} \]
  4. Add Preprocessing
  5. Taylor expanded in x around inf 92.5%

    \[\leadsto \color{blue}{x \cdot \left(y + 3 \cdot \frac{{z}^{2}}{x}\right)} \]
  6. Step-by-step derivation
    1. unpow292.5%

      \[\leadsto x \cdot \left(y + 3 \cdot \frac{\color{blue}{z \cdot z}}{x}\right) \]
    2. associate-/l*94.1%

      \[\leadsto x \cdot \left(y + 3 \cdot \color{blue}{\left(z \cdot \frac{z}{x}\right)}\right) \]
  7. Applied egg-rr94.1%

    \[\leadsto x \cdot \left(y + 3 \cdot \color{blue}{\left(z \cdot \frac{z}{x}\right)}\right) \]
  8. Step-by-step derivation
    1. clear-num94.0%

      \[\leadsto x \cdot \left(y + 3 \cdot \left(z \cdot \color{blue}{\frac{1}{\frac{x}{z}}}\right)\right) \]
    2. un-div-inv94.1%

      \[\leadsto x \cdot \left(y + 3 \cdot \color{blue}{\frac{z}{\frac{x}{z}}}\right) \]
  9. Applied egg-rr94.1%

    \[\leadsto x \cdot \left(y + 3 \cdot \color{blue}{\frac{z}{\frac{x}{z}}}\right) \]
  10. Step-by-step derivation
    1. associate-*r/94.1%

      \[\leadsto x \cdot \left(y + \color{blue}{\frac{3 \cdot z}{\frac{x}{z}}}\right) \]
    2. div-inv94.1%

      \[\leadsto x \cdot \left(y + \frac{3 \cdot z}{\color{blue}{x \cdot \frac{1}{z}}}\right) \]
    3. associate-/r*94.1%

      \[\leadsto x \cdot \left(y + \color{blue}{\frac{\frac{3 \cdot z}{x}}{\frac{1}{z}}}\right) \]
  11. Applied egg-rr94.1%

    \[\leadsto x \cdot \left(y + \color{blue}{\frac{\frac{3 \cdot z}{x}}{\frac{1}{z}}}\right) \]
  12. Final simplification94.1%

    \[\leadsto x \cdot \left(y + \frac{\frac{z \cdot 3}{x}}{\frac{1}{z}}\right) \]
  13. Add Preprocessing

Alternative 5: 93.5% accurate, 1.4× speedup?

\[\begin{array}{l} \\ x \cdot \left(y + 3 \cdot \frac{z}{\frac{x}{z}}\right) \end{array} \]
(FPCore (x y z) :precision binary64 (* x (+ y (* 3.0 (/ z (/ x z))))))
double code(double x, double y, double z) {
	return x * (y + (3.0 * (z / (x / z))));
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = x * (y + (3.0d0 * (z / (x / z))))
end function
public static double code(double x, double y, double z) {
	return x * (y + (3.0 * (z / (x / z))));
}
def code(x, y, z):
	return x * (y + (3.0 * (z / (x / z))))
function code(x, y, z)
	return Float64(x * Float64(y + Float64(3.0 * Float64(z / Float64(x / z)))))
end
function tmp = code(x, y, z)
	tmp = x * (y + (3.0 * (z / (x / z))));
end
code[x_, y_, z_] := N[(x * N[(y + N[(3.0 * N[(z / N[(x / z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x \cdot \left(y + 3 \cdot \frac{z}{\frac{x}{z}}\right)
\end{array}
Derivation
  1. Initial program 96.7%

    \[\left(\left(x \cdot y + z \cdot z\right) + z \cdot z\right) + z \cdot z \]
  2. Step-by-step derivation
    1. associate-+l+96.8%

      \[\leadsto \color{blue}{\left(x \cdot y + \left(z \cdot z + z \cdot z\right)\right)} + z \cdot z \]
    2. associate-+l+96.8%

      \[\leadsto \color{blue}{x \cdot y + \left(\left(z \cdot z + z \cdot z\right) + z \cdot z\right)} \]
    3. +-commutative96.8%

      \[\leadsto \color{blue}{\left(\left(z \cdot z + z \cdot z\right) + z \cdot z\right) + x \cdot y} \]
    4. distribute-lft-out96.8%

      \[\leadsto \left(\color{blue}{z \cdot \left(z + z\right)} + z \cdot z\right) + x \cdot y \]
    5. distribute-lft-out96.8%

      \[\leadsto \color{blue}{z \cdot \left(\left(z + z\right) + z\right)} + x \cdot y \]
    6. fma-define98.3%

      \[\leadsto \color{blue}{\mathsf{fma}\left(z, \left(z + z\right) + z, x \cdot y\right)} \]
    7. remove-double-neg98.3%

      \[\leadsto \mathsf{fma}\left(z, \left(z + z\right) + \color{blue}{\left(-\left(-z\right)\right)}, x \cdot y\right) \]
    8. unsub-neg98.3%

      \[\leadsto \mathsf{fma}\left(z, \color{blue}{\left(z + z\right) - \left(-z\right)}, x \cdot y\right) \]
    9. count-298.3%

      \[\leadsto \mathsf{fma}\left(z, \color{blue}{2 \cdot z} - \left(-z\right), x \cdot y\right) \]
    10. neg-mul-198.3%

      \[\leadsto \mathsf{fma}\left(z, 2 \cdot z - \color{blue}{-1 \cdot z}, x \cdot y\right) \]
    11. distribute-rgt-out--98.3%

      \[\leadsto \mathsf{fma}\left(z, \color{blue}{z \cdot \left(2 - -1\right)}, x \cdot y\right) \]
    12. metadata-eval98.3%

      \[\leadsto \mathsf{fma}\left(z, z \cdot \color{blue}{3}, x \cdot y\right) \]
  3. Simplified98.3%

    \[\leadsto \color{blue}{\mathsf{fma}\left(z, z \cdot 3, x \cdot y\right)} \]
  4. Add Preprocessing
  5. Taylor expanded in x around inf 92.5%

    \[\leadsto \color{blue}{x \cdot \left(y + 3 \cdot \frac{{z}^{2}}{x}\right)} \]
  6. Step-by-step derivation
    1. unpow292.5%

      \[\leadsto x \cdot \left(y + 3 \cdot \frac{\color{blue}{z \cdot z}}{x}\right) \]
    2. associate-/l*94.1%

      \[\leadsto x \cdot \left(y + 3 \cdot \color{blue}{\left(z \cdot \frac{z}{x}\right)}\right) \]
  7. Applied egg-rr94.1%

    \[\leadsto x \cdot \left(y + 3 \cdot \color{blue}{\left(z \cdot \frac{z}{x}\right)}\right) \]
  8. Step-by-step derivation
    1. clear-num94.0%

      \[\leadsto x \cdot \left(y + 3 \cdot \left(z \cdot \color{blue}{\frac{1}{\frac{x}{z}}}\right)\right) \]
    2. un-div-inv94.1%

      \[\leadsto x \cdot \left(y + 3 \cdot \color{blue}{\frac{z}{\frac{x}{z}}}\right) \]
  9. Applied egg-rr94.1%

    \[\leadsto x \cdot \left(y + 3 \cdot \color{blue}{\frac{z}{\frac{x}{z}}}\right) \]
  10. Add Preprocessing

Alternative 6: 93.5% accurate, 1.4× speedup?

\[\begin{array}{l} \\ x \cdot \left(y + 3 \cdot \left(z \cdot \frac{z}{x}\right)\right) \end{array} \]
(FPCore (x y z) :precision binary64 (* x (+ y (* 3.0 (* z (/ z x))))))
double code(double x, double y, double z) {
	return x * (y + (3.0 * (z * (z / x))));
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = x * (y + (3.0d0 * (z * (z / x))))
end function
public static double code(double x, double y, double z) {
	return x * (y + (3.0 * (z * (z / x))));
}
def code(x, y, z):
	return x * (y + (3.0 * (z * (z / x))))
function code(x, y, z)
	return Float64(x * Float64(y + Float64(3.0 * Float64(z * Float64(z / x)))))
end
function tmp = code(x, y, z)
	tmp = x * (y + (3.0 * (z * (z / x))));
end
code[x_, y_, z_] := N[(x * N[(y + N[(3.0 * N[(z * N[(z / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x \cdot \left(y + 3 \cdot \left(z \cdot \frac{z}{x}\right)\right)
\end{array}
Derivation
  1. Initial program 96.7%

    \[\left(\left(x \cdot y + z \cdot z\right) + z \cdot z\right) + z \cdot z \]
  2. Step-by-step derivation
    1. associate-+l+96.8%

      \[\leadsto \color{blue}{\left(x \cdot y + \left(z \cdot z + z \cdot z\right)\right)} + z \cdot z \]
    2. associate-+l+96.8%

      \[\leadsto \color{blue}{x \cdot y + \left(\left(z \cdot z + z \cdot z\right) + z \cdot z\right)} \]
    3. +-commutative96.8%

      \[\leadsto \color{blue}{\left(\left(z \cdot z + z \cdot z\right) + z \cdot z\right) + x \cdot y} \]
    4. distribute-lft-out96.8%

      \[\leadsto \left(\color{blue}{z \cdot \left(z + z\right)} + z \cdot z\right) + x \cdot y \]
    5. distribute-lft-out96.8%

      \[\leadsto \color{blue}{z \cdot \left(\left(z + z\right) + z\right)} + x \cdot y \]
    6. fma-define98.3%

      \[\leadsto \color{blue}{\mathsf{fma}\left(z, \left(z + z\right) + z, x \cdot y\right)} \]
    7. remove-double-neg98.3%

      \[\leadsto \mathsf{fma}\left(z, \left(z + z\right) + \color{blue}{\left(-\left(-z\right)\right)}, x \cdot y\right) \]
    8. unsub-neg98.3%

      \[\leadsto \mathsf{fma}\left(z, \color{blue}{\left(z + z\right) - \left(-z\right)}, x \cdot y\right) \]
    9. count-298.3%

      \[\leadsto \mathsf{fma}\left(z, \color{blue}{2 \cdot z} - \left(-z\right), x \cdot y\right) \]
    10. neg-mul-198.3%

      \[\leadsto \mathsf{fma}\left(z, 2 \cdot z - \color{blue}{-1 \cdot z}, x \cdot y\right) \]
    11. distribute-rgt-out--98.3%

      \[\leadsto \mathsf{fma}\left(z, \color{blue}{z \cdot \left(2 - -1\right)}, x \cdot y\right) \]
    12. metadata-eval98.3%

      \[\leadsto \mathsf{fma}\left(z, z \cdot \color{blue}{3}, x \cdot y\right) \]
  3. Simplified98.3%

    \[\leadsto \color{blue}{\mathsf{fma}\left(z, z \cdot 3, x \cdot y\right)} \]
  4. Add Preprocessing
  5. Taylor expanded in x around inf 92.5%

    \[\leadsto \color{blue}{x \cdot \left(y + 3 \cdot \frac{{z}^{2}}{x}\right)} \]
  6. Step-by-step derivation
    1. unpow292.5%

      \[\leadsto x \cdot \left(y + 3 \cdot \frac{\color{blue}{z \cdot z}}{x}\right) \]
    2. associate-/l*94.1%

      \[\leadsto x \cdot \left(y + 3 \cdot \color{blue}{\left(z \cdot \frac{z}{x}\right)}\right) \]
  7. Applied egg-rr94.1%

    \[\leadsto x \cdot \left(y + 3 \cdot \color{blue}{\left(z \cdot \frac{z}{x}\right)}\right) \]
  8. Add Preprocessing

Alternative 7: 69.2% accurate, 1.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;z \leq 1.42 \cdot 10^{-22}:\\ \;\;\;\;x \cdot y\\ \mathbf{else}:\\ \;\;\;\;z \cdot \left(z \cdot 3\right)\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (<= z 1.42e-22) (* x y) (* z (* z 3.0))))
double code(double x, double y, double z) {
	double tmp;
	if (z <= 1.42e-22) {
		tmp = x * y;
	} else {
		tmp = z * (z * 3.0);
	}
	return tmp;
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8) :: tmp
    if (z <= 1.42d-22) then
        tmp = x * y
    else
        tmp = z * (z * 3.0d0)
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double tmp;
	if (z <= 1.42e-22) {
		tmp = x * y;
	} else {
		tmp = z * (z * 3.0);
	}
	return tmp;
}
def code(x, y, z):
	tmp = 0
	if z <= 1.42e-22:
		tmp = x * y
	else:
		tmp = z * (z * 3.0)
	return tmp
function code(x, y, z)
	tmp = 0.0
	if (z <= 1.42e-22)
		tmp = Float64(x * y);
	else
		tmp = Float64(z * Float64(z * 3.0));
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if (z <= 1.42e-22)
		tmp = x * y;
	else
		tmp = z * (z * 3.0);
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := If[LessEqual[z, 1.42e-22], N[(x * y), $MachinePrecision], N[(z * N[(z * 3.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;z \leq 1.42 \cdot 10^{-22}:\\
\;\;\;\;x \cdot y\\

\mathbf{else}:\\
\;\;\;\;z \cdot \left(z \cdot 3\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if z < 1.4200000000000001e-22

    1. Initial program 97.2%

      \[\left(\left(x \cdot y + z \cdot z\right) + z \cdot z\right) + z \cdot z \]
    2. Step-by-step derivation
      1. associate-+l+97.2%

        \[\leadsto \color{blue}{\left(x \cdot y + \left(z \cdot z + z \cdot z\right)\right)} + z \cdot z \]
      2. associate-+l+97.2%

        \[\leadsto \color{blue}{x \cdot y + \left(\left(z \cdot z + z \cdot z\right) + z \cdot z\right)} \]
      3. +-commutative97.2%

        \[\leadsto \color{blue}{\left(\left(z \cdot z + z \cdot z\right) + z \cdot z\right) + x \cdot y} \]
      4. distribute-lft-out97.2%

        \[\leadsto \left(\color{blue}{z \cdot \left(z + z\right)} + z \cdot z\right) + x \cdot y \]
      5. distribute-lft-out97.2%

        \[\leadsto \color{blue}{z \cdot \left(\left(z + z\right) + z\right)} + x \cdot y \]
      6. fma-define97.7%

        \[\leadsto \color{blue}{\mathsf{fma}\left(z, \left(z + z\right) + z, x \cdot y\right)} \]
      7. remove-double-neg97.7%

        \[\leadsto \mathsf{fma}\left(z, \left(z + z\right) + \color{blue}{\left(-\left(-z\right)\right)}, x \cdot y\right) \]
      8. unsub-neg97.7%

        \[\leadsto \mathsf{fma}\left(z, \color{blue}{\left(z + z\right) - \left(-z\right)}, x \cdot y\right) \]
      9. count-297.7%

        \[\leadsto \mathsf{fma}\left(z, \color{blue}{2 \cdot z} - \left(-z\right), x \cdot y\right) \]
      10. neg-mul-197.7%

        \[\leadsto \mathsf{fma}\left(z, 2 \cdot z - \color{blue}{-1 \cdot z}, x \cdot y\right) \]
      11. distribute-rgt-out--97.7%

        \[\leadsto \mathsf{fma}\left(z, \color{blue}{z \cdot \left(2 - -1\right)}, x \cdot y\right) \]
      12. metadata-eval97.7%

        \[\leadsto \mathsf{fma}\left(z, z \cdot \color{blue}{3}, x \cdot y\right) \]
    3. Simplified97.7%

      \[\leadsto \color{blue}{\mathsf{fma}\left(z, z \cdot 3, x \cdot y\right)} \]
    4. Add Preprocessing
    5. Taylor expanded in x around inf 95.9%

      \[\leadsto \color{blue}{x \cdot \left(y + 3 \cdot \frac{{z}^{2}}{x}\right)} \]
    6. Taylor expanded in x around inf 60.3%

      \[\leadsto \color{blue}{x \cdot y} \]

    if 1.4200000000000001e-22 < z

    1. Initial program 95.7%

      \[\left(\left(x \cdot y + z \cdot z\right) + z \cdot z\right) + z \cdot z \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0 77.6%

      \[\leadsto \color{blue}{2 \cdot {z}^{2} + {z}^{2}} \]
    4. Step-by-step derivation
      1. distribute-lft1-in77.6%

        \[\leadsto \color{blue}{\left(2 + 1\right) \cdot {z}^{2}} \]
      2. metadata-eval77.6%

        \[\leadsto \color{blue}{3} \cdot {z}^{2} \]
      3. *-commutative77.6%

        \[\leadsto \color{blue}{{z}^{2} \cdot 3} \]
    5. Simplified77.6%

      \[\leadsto \color{blue}{{z}^{2} \cdot 3} \]
    6. Step-by-step derivation
      1. *-commutative77.6%

        \[\leadsto \color{blue}{3 \cdot {z}^{2}} \]
      2. unpow277.6%

        \[\leadsto 3 \cdot \color{blue}{\left(z \cdot z\right)} \]
      3. associate-*r*77.5%

        \[\leadsto \color{blue}{\left(3 \cdot z\right) \cdot z} \]
      4. *-commutative77.5%

        \[\leadsto \color{blue}{\left(z \cdot 3\right)} \cdot z \]
    7. Applied egg-rr77.5%

      \[\leadsto \color{blue}{\left(z \cdot 3\right) \cdot z} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification65.2%

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \leq 1.42 \cdot 10^{-22}:\\ \;\;\;\;x \cdot y\\ \mathbf{else}:\\ \;\;\;\;z \cdot \left(z \cdot 3\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 8: 53.2% accurate, 5.0× speedup?

\[\begin{array}{l} \\ x \cdot y \end{array} \]
(FPCore (x y z) :precision binary64 (* x y))
double code(double x, double y, double z) {
	return x * y;
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = x * y
end function
public static double code(double x, double y, double z) {
	return x * y;
}
def code(x, y, z):
	return x * y
function code(x, y, z)
	return Float64(x * y)
end
function tmp = code(x, y, z)
	tmp = x * y;
end
code[x_, y_, z_] := N[(x * y), $MachinePrecision]
\begin{array}{l}

\\
x \cdot y
\end{array}
Derivation
  1. Initial program 96.7%

    \[\left(\left(x \cdot y + z \cdot z\right) + z \cdot z\right) + z \cdot z \]
  2. Step-by-step derivation
    1. associate-+l+96.8%

      \[\leadsto \color{blue}{\left(x \cdot y + \left(z \cdot z + z \cdot z\right)\right)} + z \cdot z \]
    2. associate-+l+96.8%

      \[\leadsto \color{blue}{x \cdot y + \left(\left(z \cdot z + z \cdot z\right) + z \cdot z\right)} \]
    3. +-commutative96.8%

      \[\leadsto \color{blue}{\left(\left(z \cdot z + z \cdot z\right) + z \cdot z\right) + x \cdot y} \]
    4. distribute-lft-out96.8%

      \[\leadsto \left(\color{blue}{z \cdot \left(z + z\right)} + z \cdot z\right) + x \cdot y \]
    5. distribute-lft-out96.8%

      \[\leadsto \color{blue}{z \cdot \left(\left(z + z\right) + z\right)} + x \cdot y \]
    6. fma-define98.3%

      \[\leadsto \color{blue}{\mathsf{fma}\left(z, \left(z + z\right) + z, x \cdot y\right)} \]
    7. remove-double-neg98.3%

      \[\leadsto \mathsf{fma}\left(z, \left(z + z\right) + \color{blue}{\left(-\left(-z\right)\right)}, x \cdot y\right) \]
    8. unsub-neg98.3%

      \[\leadsto \mathsf{fma}\left(z, \color{blue}{\left(z + z\right) - \left(-z\right)}, x \cdot y\right) \]
    9. count-298.3%

      \[\leadsto \mathsf{fma}\left(z, \color{blue}{2 \cdot z} - \left(-z\right), x \cdot y\right) \]
    10. neg-mul-198.3%

      \[\leadsto \mathsf{fma}\left(z, 2 \cdot z - \color{blue}{-1 \cdot z}, x \cdot y\right) \]
    11. distribute-rgt-out--98.3%

      \[\leadsto \mathsf{fma}\left(z, \color{blue}{z \cdot \left(2 - -1\right)}, x \cdot y\right) \]
    12. metadata-eval98.3%

      \[\leadsto \mathsf{fma}\left(z, z \cdot \color{blue}{3}, x \cdot y\right) \]
  3. Simplified98.3%

    \[\leadsto \color{blue}{\mathsf{fma}\left(z, z \cdot 3, x \cdot y\right)} \]
  4. Add Preprocessing
  5. Taylor expanded in x around inf 92.5%

    \[\leadsto \color{blue}{x \cdot \left(y + 3 \cdot \frac{{z}^{2}}{x}\right)} \]
  6. Taylor expanded in x around inf 50.3%

    \[\leadsto \color{blue}{x \cdot y} \]
  7. Add Preprocessing

Developer target: 98.4% accurate, 1.7× speedup?

\[\begin{array}{l} \\ \left(3 \cdot z\right) \cdot z + y \cdot x \end{array} \]
(FPCore (x y z) :precision binary64 (+ (* (* 3.0 z) z) (* y x)))
double code(double x, double y, double z) {
	return ((3.0 * z) * z) + (y * x);
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = ((3.0d0 * z) * z) + (y * x)
end function
public static double code(double x, double y, double z) {
	return ((3.0 * z) * z) + (y * x);
}
def code(x, y, z):
	return ((3.0 * z) * z) + (y * x)
function code(x, y, z)
	return Float64(Float64(Float64(3.0 * z) * z) + Float64(y * x))
end
function tmp = code(x, y, z)
	tmp = ((3.0 * z) * z) + (y * x);
end
code[x_, y_, z_] := N[(N[(N[(3.0 * z), $MachinePrecision] * z), $MachinePrecision] + N[(y * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(3 \cdot z\right) \cdot z + y \cdot x
\end{array}

Reproduce

?
herbie shell --seed 2024096 
(FPCore (x y z)
  :name "Linear.Quaternion:$c/ from linear-1.19.1.3, A"
  :precision binary64

  :alt
  (+ (* (* 3.0 z) z) (* y x))

  (+ (+ (+ (* x y) (* z z)) (* z z)) (* z z)))