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

Percentage Accurate: 98.4% → 99.5%
Time: 4.7s
Alternatives: 6
Speedup: 1.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 6 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.5% accurate, 0.1× speedup?

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

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

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

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

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

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

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

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

    \[\leadsto \color{blue}{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, y, z \cdot \left(z + z\right)\right)\right)} \]
  4. Final simplification98.8%

    \[\leadsto \mathsf{fma}\left(z, z, \mathsf{fma}\left(x, y, z \cdot \left(z + z\right)\right)\right) \]

Alternative 2: 99.4% accurate, 0.1× speedup?

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

\\
\mathsf{fma}\left(x, y, z \cdot \left(z \cdot 3\right)\right)
\end{array}
Derivation
  1. Initial program 98.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+98.7%

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

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

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

      \[\leadsto \mathsf{fma}\left(x, y, z \cdot z + \color{blue}{2 \cdot \left(z \cdot z\right)}\right) \]
    5. distribute-rgt1-in98.7%

      \[\leadsto \mathsf{fma}\left(x, y, \color{blue}{\left(2 + 1\right) \cdot \left(z \cdot z\right)}\right) \]
    6. *-commutative98.7%

      \[\leadsto \mathsf{fma}\left(x, y, \color{blue}{\left(z \cdot z\right) \cdot \left(2 + 1\right)}\right) \]
    7. associate-*l*98.7%

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

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

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

    \[\leadsto \mathsf{fma}\left(x, y, z \cdot \left(z \cdot 3\right)\right) \]

Alternative 3: 84.3% accurate, 0.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;z \cdot z \leq 10^{-134} \lor \neg \left(z \cdot z \leq 5 \cdot 10^{-97}\right) \land z \cdot z \leq 100:\\ \;\;\;\;x \cdot y\\ \mathbf{else}:\\ \;\;\;\;3 \cdot \left(z \cdot z\right)\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (or (<= (* z z) 1e-134) (and (not (<= (* z z) 5e-97)) (<= (* z z) 100.0)))
   (* x y)
   (* 3.0 (* z z))))
double code(double x, double y, double z) {
	double tmp;
	if (((z * z) <= 1e-134) || (!((z * z) <= 5e-97) && ((z * z) <= 100.0))) {
		tmp = x * y;
	} else {
		tmp = 3.0 * (z * 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-134) .or. (.not. ((z * z) <= 5d-97)) .and. ((z * z) <= 100.0d0)) then
        tmp = x * y
    else
        tmp = 3.0d0 * (z * z)
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double tmp;
	if (((z * z) <= 1e-134) || (!((z * z) <= 5e-97) && ((z * z) <= 100.0))) {
		tmp = x * y;
	} else {
		tmp = 3.0 * (z * z);
	}
	return tmp;
}
def code(x, y, z):
	tmp = 0
	if ((z * z) <= 1e-134) or (not ((z * z) <= 5e-97) and ((z * z) <= 100.0)):
		tmp = x * y
	else:
		tmp = 3.0 * (z * z)
	return tmp
function code(x, y, z)
	tmp = 0.0
	if ((Float64(z * z) <= 1e-134) || (!(Float64(z * z) <= 5e-97) && (Float64(z * z) <= 100.0)))
		tmp = Float64(x * y);
	else
		tmp = Float64(3.0 * Float64(z * z));
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if (((z * z) <= 1e-134) || (~(((z * z) <= 5e-97)) && ((z * z) <= 100.0)))
		tmp = x * y;
	else
		tmp = 3.0 * (z * z);
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := If[Or[LessEqual[N[(z * z), $MachinePrecision], 1e-134], And[N[Not[LessEqual[N[(z * z), $MachinePrecision], 5e-97]], $MachinePrecision], LessEqual[N[(z * z), $MachinePrecision], 100.0]]], N[(x * y), $MachinePrecision], N[(3.0 * N[(z * z), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;z \cdot z \leq 10^{-134} \lor \neg \left(z \cdot z \leq 5 \cdot 10^{-97}\right) \land z \cdot z \leq 100:\\
\;\;\;\;x \cdot y\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 z z) < 1.00000000000000004e-134 or 4.9999999999999995e-97 < (*.f64 z z) < 100

    1. Initial program 99.9%

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

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

    if 1.00000000000000004e-134 < (*.f64 z z) < 4.9999999999999995e-97 or 100 < (*.f64 z z)

    1. Initial program 97.5%

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

      \[\leadsto \color{blue}{{z}^{2} + 2 \cdot {z}^{2}} \]
    3. Step-by-step derivation
      1. unpow279.5%

        \[\leadsto \color{blue}{z \cdot z} + 2 \cdot {z}^{2} \]
      2. unpow279.5%

        \[\leadsto z \cdot z + 2 \cdot \color{blue}{\left(z \cdot z\right)} \]
      3. distribute-rgt1-in79.5%

        \[\leadsto \color{blue}{\left(2 + 1\right) \cdot \left(z \cdot z\right)} \]
      4. metadata-eval79.5%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \cdot z \leq 10^{-134} \lor \neg \left(z \cdot z \leq 5 \cdot 10^{-97}\right) \land z \cdot z \leq 100:\\ \;\;\;\;x \cdot y\\ \mathbf{else}:\\ \;\;\;\;3 \cdot \left(z \cdot z\right)\\ \end{array} \]

Alternative 4: 98.4% accurate, 1.7× speedup?

\[\begin{array}{l} \\ z \cdot \left(z \cdot 3\right) + x \cdot y \end{array} \]
(FPCore (x y z) :precision binary64 (+ (* z (* z 3.0)) (* x y)))
double code(double x, double y, double z) {
	return (z * (z * 3.0)) + (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 = (z * (z * 3.0d0)) + (x * y)
end function
public static double code(double x, double y, double z) {
	return (z * (z * 3.0)) + (x * y);
}
def code(x, y, z):
	return (z * (z * 3.0)) + (x * y)
function code(x, y, z)
	return Float64(Float64(z * Float64(z * 3.0)) + Float64(x * y))
end
function tmp = code(x, y, z)
	tmp = (z * (z * 3.0)) + (x * y);
end
code[x_, y_, z_] := N[(N[(z * N[(z * 3.0), $MachinePrecision]), $MachinePrecision] + N[(x * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
z \cdot \left(z \cdot 3\right) + x \cdot y
\end{array}
Derivation
  1. Initial program 98.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+98.7%

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

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

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

      \[\leadsto \mathsf{fma}\left(x, y, z \cdot z + \color{blue}{2 \cdot \left(z \cdot z\right)}\right) \]
    5. distribute-rgt1-in98.7%

      \[\leadsto \mathsf{fma}\left(x, y, \color{blue}{\left(2 + 1\right) \cdot \left(z \cdot z\right)}\right) \]
    6. *-commutative98.7%

      \[\leadsto \mathsf{fma}\left(x, y, \color{blue}{\left(z \cdot z\right) \cdot \left(2 + 1\right)}\right) \]
    7. associate-*l*98.7%

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

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

    \[\leadsto \color{blue}{\mathsf{fma}\left(x, y, z \cdot \left(z \cdot 3\right)\right)} \]
  4. Step-by-step derivation
    1. fma-udef98.7%

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

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

    \[\leadsto \color{blue}{z \cdot \left(z \cdot 3\right) + x \cdot y} \]
  6. Final simplification98.7%

    \[\leadsto z \cdot \left(z \cdot 3\right) + x \cdot y \]

Alternative 5: 52.8% 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 98.7%

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

    \[\leadsto \color{blue}{y \cdot x} \]
  3. Final simplification57.9%

    \[\leadsto x \cdot y \]

Alternative 6: 2.2% accurate, 15.0× speedup?

\[\begin{array}{l} \\ -2 \end{array} \]
(FPCore (x y z) :precision binary64 -2.0)
double code(double x, double y, double z) {
	return -2.0;
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = -2.0d0
end function
public static double code(double x, double y, double z) {
	return -2.0;
}
def code(x, y, z):
	return -2.0
function code(x, y, z)
	return -2.0
end
function tmp = code(x, y, z)
	tmp = -2.0;
end
code[x_, y_, z_] := -2.0
\begin{array}{l}

\\
-2
\end{array}
Derivation
  1. Initial program 98.7%

    \[\left(\left(x \cdot y + z \cdot z\right) + z \cdot z\right) + z \cdot z \]
  2. Step-by-step derivation
    1. add-cube-cbrt97.7%

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

      \[\leadsto \left(\color{blue}{{\left(\sqrt[3]{x \cdot y + z \cdot z}\right)}^{3}} + z \cdot z\right) + z \cdot z \]
    3. fma-def97.7%

      \[\leadsto \left({\left(\sqrt[3]{\color{blue}{\mathsf{fma}\left(x, y, z \cdot z\right)}}\right)}^{3} + z \cdot z\right) + z \cdot z \]
  3. Applied egg-rr97.7%

    \[\leadsto \left(\color{blue}{{\left(\sqrt[3]{\mathsf{fma}\left(x, y, z \cdot z\right)}\right)}^{3}} + z \cdot z\right) + z \cdot z \]
  4. Taylor expanded in x around 0 48.6%

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

      \[\leadsto \left({\left({\color{blue}{\left(z \cdot z\right)}}^{0.3333333333333333}\right)}^{3} + z \cdot z\right) + z \cdot z \]
    2. unpow1/350.3%

      \[\leadsto \left({\color{blue}{\left(\sqrt[3]{z \cdot z}\right)}}^{3} + z \cdot z\right) + z \cdot z \]
  6. Simplified50.3%

    \[\leadsto \left({\color{blue}{\left(\sqrt[3]{z \cdot z}\right)}}^{3} + z \cdot z\right) + z \cdot z \]
  7. Taylor expanded in z around 0 31.1%

    \[\leadsto \color{blue}{2 \cdot {z}^{2}} \]
  8. Simplified2.5%

    \[\leadsto \color{blue}{-2} \]
  9. Final simplification2.5%

    \[\leadsto -2 \]

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 2023213 
(FPCore (x y z)
  :name "Linear.Quaternion:$c/ from linear-1.19.1.3, A"
  :precision binary64

  :herbie-target
  (+ (* (* 3.0 z) z) (* y x))

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