Diagrams.Solve.Polynomial:cubForm from diagrams-solve-0.1, B

Percentage Accurate: 99.8% → 99.8%
Time: 2.9s
Alternatives: 4
Speedup: 1.0×

Specification

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

\\
\left(x \cdot 3\right) \cdot y - 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 4 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: 99.8% accurate, 1.0× speedup?

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

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

Alternative 1: 99.8% accurate, 1.0× speedup?

\[\begin{array}{l} [x, y] = \mathsf{sort}([x, y])\\ \\ \left(x \cdot 3\right) \cdot y - z \end{array} \]
NOTE: x and y should be sorted in increasing order before calling this function.
(FPCore (x y z) :precision binary64 (- (* (* x 3.0) y) z))
assert(x < y);
double code(double x, double y, double z) {
	return ((x * 3.0) * y) - z;
}
NOTE: x and y should be sorted in increasing order before calling this function.
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = ((x * 3.0d0) * y) - z
end function
assert x < y;
public static double code(double x, double y, double z) {
	return ((x * 3.0) * y) - z;
}
[x, y] = sort([x, y])
def code(x, y, z):
	return ((x * 3.0) * y) - z
x, y = sort([x, y])
function code(x, y, z)
	return Float64(Float64(Float64(x * 3.0) * y) - z)
end
x, y = num2cell(sort([x, y])){:}
function tmp = code(x, y, z)
	tmp = ((x * 3.0) * y) - z;
end
NOTE: x and y should be sorted in increasing order before calling this function.
code[x_, y_, z_] := N[(N[(N[(x * 3.0), $MachinePrecision] * y), $MachinePrecision] - z), $MachinePrecision]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
\left(x \cdot 3\right) \cdot y - z
\end{array}
Derivation
  1. Initial program 99.8%

    \[\left(x \cdot 3\right) \cdot y - z \]
  2. Final simplification99.8%

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

Alternative 2: 99.8% accurate, 1.0× speedup?

\[\begin{array}{l} [x, y] = \mathsf{sort}([x, y])\\ \\ 3 \cdot \left(x \cdot y\right) - z \end{array} \]
NOTE: x and y should be sorted in increasing order before calling this function.
(FPCore (x y z) :precision binary64 (- (* 3.0 (* x y)) z))
assert(x < y);
double code(double x, double y, double z) {
	return (3.0 * (x * y)) - z;
}
NOTE: x and y should be sorted in increasing order before calling this function.
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 * (x * y)) - z
end function
assert x < y;
public static double code(double x, double y, double z) {
	return (3.0 * (x * y)) - z;
}
[x, y] = sort([x, y])
def code(x, y, z):
	return (3.0 * (x * y)) - z
x, y = sort([x, y])
function code(x, y, z)
	return Float64(Float64(3.0 * Float64(x * y)) - z)
end
x, y = num2cell(sort([x, y])){:}
function tmp = code(x, y, z)
	tmp = (3.0 * (x * y)) - z;
end
NOTE: x and y should be sorted in increasing order before calling this function.
code[x_, y_, z_] := N[(N[(3.0 * N[(x * y), $MachinePrecision]), $MachinePrecision] - z), $MachinePrecision]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
3 \cdot \left(x \cdot y\right) - z
\end{array}
Derivation
  1. Initial program 99.8%

    \[\left(x \cdot 3\right) \cdot y - z \]
  2. Taylor expanded in x around 0 99.8%

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

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

Alternative 3: 51.4% accurate, 3.5× speedup?

\[\begin{array}{l} [x, y] = \mathsf{sort}([x, y])\\ \\ -z \end{array} \]
NOTE: x and y should be sorted in increasing order before calling this function.
(FPCore (x y z) :precision binary64 (- z))
assert(x < y);
double code(double x, double y, double z) {
	return -z;
}
NOTE: x and y should be sorted in increasing order before calling this function.
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = -z
end function
assert x < y;
public static double code(double x, double y, double z) {
	return -z;
}
[x, y] = sort([x, y])
def code(x, y, z):
	return -z
x, y = sort([x, y])
function code(x, y, z)
	return Float64(-z)
end
x, y = num2cell(sort([x, y])){:}
function tmp = code(x, y, z)
	tmp = -z;
end
NOTE: x and y should be sorted in increasing order before calling this function.
code[x_, y_, z_] := (-z)
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
-z
\end{array}
Derivation
  1. Initial program 99.8%

    \[\left(x \cdot 3\right) \cdot y - z \]
  2. Taylor expanded in x around 0 45.5%

    \[\leadsto \color{blue}{-1 \cdot z} \]
  3. Step-by-step derivation
    1. mul-1-neg45.5%

      \[\leadsto \color{blue}{-z} \]
  4. Simplified45.5%

    \[\leadsto \color{blue}{-z} \]
  5. Final simplification45.5%

    \[\leadsto -z \]

Alternative 4: 2.2% accurate, 7.0× speedup?

\[\begin{array}{l} [x, y] = \mathsf{sort}([x, y])\\ \\ z \end{array} \]
NOTE: x and y should be sorted in increasing order before calling this function.
(FPCore (x y z) :precision binary64 z)
assert(x < y);
double code(double x, double y, double z) {
	return z;
}
NOTE: x and y should be sorted in increasing order before calling this function.
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = z
end function
assert x < y;
public static double code(double x, double y, double z) {
	return z;
}
[x, y] = sort([x, y])
def code(x, y, z):
	return z
x, y = sort([x, y])
function code(x, y, z)
	return z
end
x, y = num2cell(sort([x, y])){:}
function tmp = code(x, y, z)
	tmp = z;
end
NOTE: x and y should be sorted in increasing order before calling this function.
code[x_, y_, z_] := z
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
z
\end{array}
Derivation
  1. Initial program 99.8%

    \[\left(x \cdot 3\right) \cdot y - z \]
  2. Step-by-step derivation
    1. *-commutative99.8%

      \[\leadsto \color{blue}{y \cdot \left(x \cdot 3\right)} - z \]
    2. add-sqr-sqrt49.8%

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

      \[\leadsto \color{blue}{\left(y \cdot \sqrt{x \cdot 3}\right) \cdot \sqrt{x \cdot 3}} - z \]
    4. fma-neg49.8%

      \[\leadsto \color{blue}{\mathsf{fma}\left(y \cdot \sqrt{x \cdot 3}, \sqrt{x \cdot 3}, -z\right)} \]
    5. add-sqr-sqrt27.1%

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

      \[\leadsto \mathsf{fma}\left(y \cdot \sqrt{x \cdot 3}, \sqrt{x \cdot 3}, \color{blue}{\sqrt{\left(-z\right) \cdot \left(-z\right)}}\right) \]
    7. sqr-neg33.0%

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

      \[\leadsto \mathsf{fma}\left(y \cdot \sqrt{x \cdot 3}, \sqrt{x \cdot 3}, \color{blue}{\sqrt{z} \cdot \sqrt{z}}\right) \]
    9. add-sqr-sqrt25.6%

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

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

    \[\leadsto \color{blue}{z} \]
  5. Final simplification2.3%

    \[\leadsto z \]

Developer target: 99.8% accurate, 1.0× speedup?

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

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

Reproduce

?
herbie shell --seed 2023207 
(FPCore (x y z)
  :name "Diagrams.Solve.Polynomial:cubForm  from diagrams-solve-0.1, B"
  :precision binary64

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

  (- (* (* x 3.0) y) z))