Diagrams.Solve.Polynomial:quartForm from diagrams-solve-0.1, C

Percentage Accurate: 97.7% → 98.4%
Time: 8.6s
Alternatives: 10
Speedup: 0.5×

Specification

?
\[\begin{array}{l} \\ \left(\left(x \cdot y + \frac{z \cdot t}{16}\right) - \frac{a \cdot b}{4}\right) + c \end{array} \]
(FPCore (x y z t a b c)
 :precision binary64
 (+ (- (+ (* x y) (/ (* z t) 16.0)) (/ (* a b) 4.0)) c))
double code(double x, double y, double z, double t, double a, double b, double c) {
	return (((x * y) + ((z * t) / 16.0)) - ((a * b) / 4.0)) + c;
}
real(8) function code(x, y, z, t, a, b, c)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8), intent (in) :: t
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    real(8), intent (in) :: c
    code = (((x * y) + ((z * t) / 16.0d0)) - ((a * b) / 4.0d0)) + c
end function
public static double code(double x, double y, double z, double t, double a, double b, double c) {
	return (((x * y) + ((z * t) / 16.0)) - ((a * b) / 4.0)) + c;
}
def code(x, y, z, t, a, b, c):
	return (((x * y) + ((z * t) / 16.0)) - ((a * b) / 4.0)) + c
function code(x, y, z, t, a, b, c)
	return Float64(Float64(Float64(Float64(x * y) + Float64(Float64(z * t) / 16.0)) - Float64(Float64(a * b) / 4.0)) + c)
end
function tmp = code(x, y, z, t, a, b, c)
	tmp = (((x * y) + ((z * t) / 16.0)) - ((a * b) / 4.0)) + c;
end
code[x_, y_, z_, t_, a_, b_, c_] := N[(N[(N[(N[(x * y), $MachinePrecision] + N[(N[(z * t), $MachinePrecision] / 16.0), $MachinePrecision]), $MachinePrecision] - N[(N[(a * b), $MachinePrecision] / 4.0), $MachinePrecision]), $MachinePrecision] + c), $MachinePrecision]
\begin{array}{l}

\\
\left(\left(x \cdot y + \frac{z \cdot t}{16}\right) - \frac{a \cdot b}{4}\right) + c
\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 10 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: 97.7% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \left(\left(x \cdot y + \frac{z \cdot t}{16}\right) - \frac{a \cdot b}{4}\right) + c \end{array} \]
(FPCore (x y z t a b c)
 :precision binary64
 (+ (- (+ (* x y) (/ (* z t) 16.0)) (/ (* a b) 4.0)) c))
double code(double x, double y, double z, double t, double a, double b, double c) {
	return (((x * y) + ((z * t) / 16.0)) - ((a * b) / 4.0)) + c;
}
real(8) function code(x, y, z, t, a, b, c)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8), intent (in) :: t
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    real(8), intent (in) :: c
    code = (((x * y) + ((z * t) / 16.0d0)) - ((a * b) / 4.0d0)) + c
end function
public static double code(double x, double y, double z, double t, double a, double b, double c) {
	return (((x * y) + ((z * t) / 16.0)) - ((a * b) / 4.0)) + c;
}
def code(x, y, z, t, a, b, c):
	return (((x * y) + ((z * t) / 16.0)) - ((a * b) / 4.0)) + c
function code(x, y, z, t, a, b, c)
	return Float64(Float64(Float64(Float64(x * y) + Float64(Float64(z * t) / 16.0)) - Float64(Float64(a * b) / 4.0)) + c)
end
function tmp = code(x, y, z, t, a, b, c)
	tmp = (((x * y) + ((z * t) / 16.0)) - ((a * b) / 4.0)) + c;
end
code[x_, y_, z_, t_, a_, b_, c_] := N[(N[(N[(N[(x * y), $MachinePrecision] + N[(N[(z * t), $MachinePrecision] / 16.0), $MachinePrecision]), $MachinePrecision] - N[(N[(a * b), $MachinePrecision] / 4.0), $MachinePrecision]), $MachinePrecision] + c), $MachinePrecision]
\begin{array}{l}

\\
\left(\left(x \cdot y + \frac{z \cdot t}{16}\right) - \frac{a \cdot b}{4}\right) + c
\end{array}

Alternative 1: 98.4% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_1 := \left(y \cdot x + \frac{t \cdot z}{16}\right) - \frac{b \cdot a}{4}\\ \mathbf{if}\;t\_1 \leq \infty:\\ \;\;\;\;c + t\_1\\ \mathbf{else}:\\ \;\;\;\;\left(-0.25 \cdot a\right) \cdot b\\ \end{array} \end{array} \]
(FPCore (x y z t a b c)
 :precision binary64
 (let* ((t_1 (- (+ (* y x) (/ (* t z) 16.0)) (/ (* b a) 4.0))))
   (if (<= t_1 INFINITY) (+ c t_1) (* (* -0.25 a) b))))
double code(double x, double y, double z, double t, double a, double b, double c) {
	double t_1 = ((y * x) + ((t * z) / 16.0)) - ((b * a) / 4.0);
	double tmp;
	if (t_1 <= ((double) INFINITY)) {
		tmp = c + t_1;
	} else {
		tmp = (-0.25 * a) * b;
	}
	return tmp;
}
public static double code(double x, double y, double z, double t, double a, double b, double c) {
	double t_1 = ((y * x) + ((t * z) / 16.0)) - ((b * a) / 4.0);
	double tmp;
	if (t_1 <= Double.POSITIVE_INFINITY) {
		tmp = c + t_1;
	} else {
		tmp = (-0.25 * a) * b;
	}
	return tmp;
}
def code(x, y, z, t, a, b, c):
	t_1 = ((y * x) + ((t * z) / 16.0)) - ((b * a) / 4.0)
	tmp = 0
	if t_1 <= math.inf:
		tmp = c + t_1
	else:
		tmp = (-0.25 * a) * b
	return tmp
function code(x, y, z, t, a, b, c)
	t_1 = Float64(Float64(Float64(y * x) + Float64(Float64(t * z) / 16.0)) - Float64(Float64(b * a) / 4.0))
	tmp = 0.0
	if (t_1 <= Inf)
		tmp = Float64(c + t_1);
	else
		tmp = Float64(Float64(-0.25 * a) * b);
	end
	return tmp
end
function tmp_2 = code(x, y, z, t, a, b, c)
	t_1 = ((y * x) + ((t * z) / 16.0)) - ((b * a) / 4.0);
	tmp = 0.0;
	if (t_1 <= Inf)
		tmp = c + t_1;
	else
		tmp = (-0.25 * a) * b;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_, t_, a_, b_, c_] := Block[{t$95$1 = N[(N[(N[(y * x), $MachinePrecision] + N[(N[(t * z), $MachinePrecision] / 16.0), $MachinePrecision]), $MachinePrecision] - N[(N[(b * a), $MachinePrecision] / 4.0), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$1, Infinity], N[(c + t$95$1), $MachinePrecision], N[(N[(-0.25 * a), $MachinePrecision] * b), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_1 := \left(y \cdot x + \frac{t \cdot z}{16}\right) - \frac{b \cdot a}{4}\\
\mathbf{if}\;t\_1 \leq \infty:\\
\;\;\;\;c + t\_1\\

\mathbf{else}:\\
\;\;\;\;\left(-0.25 \cdot a\right) \cdot b\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (-.f64 (+.f64 (*.f64 x y) (/.f64 (*.f64 z t) #s(literal 16 binary64))) (/.f64 (*.f64 a b) #s(literal 4 binary64))) < +inf.0

    1. Initial program 100.0%

      \[\left(\left(x \cdot y + \frac{z \cdot t}{16}\right) - \frac{a \cdot b}{4}\right) + c \]
    2. Add Preprocessing

    if +inf.0 < (-.f64 (+.f64 (*.f64 x y) (/.f64 (*.f64 z t) #s(literal 16 binary64))) (/.f64 (*.f64 a b) #s(literal 4 binary64)))

    1. Initial program 0.0%

      \[\left(\left(x \cdot y + \frac{z \cdot t}{16}\right) - \frac{a \cdot b}{4}\right) + c \]
    2. Add Preprocessing
    3. Taylor expanded in a around inf

      \[\leadsto \color{blue}{\frac{-1}{4} \cdot \left(a \cdot b\right)} \]
    4. Step-by-step derivation
      1. associate-*r*N/A

        \[\leadsto \color{blue}{\left(\frac{-1}{4} \cdot a\right) \cdot b} \]
      2. lower-*.f64N/A

        \[\leadsto \color{blue}{\left(\frac{-1}{4} \cdot a\right) \cdot b} \]
      3. lower-*.f6471.4

        \[\leadsto \color{blue}{\left(-0.25 \cdot a\right)} \cdot b \]
    5. Applied rewrites71.4%

      \[\leadsto \color{blue}{\left(-0.25 \cdot a\right) \cdot b} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification99.2%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\left(y \cdot x + \frac{t \cdot z}{16}\right) - \frac{b \cdot a}{4} \leq \infty:\\ \;\;\;\;c + \left(\left(y \cdot x + \frac{t \cdot z}{16}\right) - \frac{b \cdot a}{4}\right)\\ \mathbf{else}:\\ \;\;\;\;\left(-0.25 \cdot a\right) \cdot b\\ \end{array} \]
  5. Add Preprocessing

Alternative 2: 77.9% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_1 := \mathsf{fma}\left(0.0625 \cdot z, t, y \cdot x\right)\\ t_2 := y \cdot x + \frac{t \cdot z}{16}\\ \mathbf{if}\;t\_2 \leq -2 \cdot 10^{+165}:\\ \;\;\;\;t\_1\\ \mathbf{elif}\;t\_2 \leq 2 \cdot 10^{+114}:\\ \;\;\;\;\mathsf{fma}\left(-0.25 \cdot a, b, c\right)\\ \mathbf{else}:\\ \;\;\;\;t\_1\\ \end{array} \end{array} \]
(FPCore (x y z t a b c)
 :precision binary64
 (let* ((t_1 (fma (* 0.0625 z) t (* y x))) (t_2 (+ (* y x) (/ (* t z) 16.0))))
   (if (<= t_2 -2e+165) t_1 (if (<= t_2 2e+114) (fma (* -0.25 a) b c) t_1))))
double code(double x, double y, double z, double t, double a, double b, double c) {
	double t_1 = fma((0.0625 * z), t, (y * x));
	double t_2 = (y * x) + ((t * z) / 16.0);
	double tmp;
	if (t_2 <= -2e+165) {
		tmp = t_1;
	} else if (t_2 <= 2e+114) {
		tmp = fma((-0.25 * a), b, c);
	} else {
		tmp = t_1;
	}
	return tmp;
}
function code(x, y, z, t, a, b, c)
	t_1 = fma(Float64(0.0625 * z), t, Float64(y * x))
	t_2 = Float64(Float64(y * x) + Float64(Float64(t * z) / 16.0))
	tmp = 0.0
	if (t_2 <= -2e+165)
		tmp = t_1;
	elseif (t_2 <= 2e+114)
		tmp = fma(Float64(-0.25 * a), b, c);
	else
		tmp = t_1;
	end
	return tmp
end
code[x_, y_, z_, t_, a_, b_, c_] := Block[{t$95$1 = N[(N[(0.0625 * z), $MachinePrecision] * t + N[(y * x), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(N[(y * x), $MachinePrecision] + N[(N[(t * z), $MachinePrecision] / 16.0), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$2, -2e+165], t$95$1, If[LessEqual[t$95$2, 2e+114], N[(N[(-0.25 * a), $MachinePrecision] * b + c), $MachinePrecision], t$95$1]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_1 := \mathsf{fma}\left(0.0625 \cdot z, t, y \cdot x\right)\\
t_2 := y \cdot x + \frac{t \cdot z}{16}\\
\mathbf{if}\;t\_2 \leq -2 \cdot 10^{+165}:\\
\;\;\;\;t\_1\\

\mathbf{elif}\;t\_2 \leq 2 \cdot 10^{+114}:\\
\;\;\;\;\mathsf{fma}\left(-0.25 \cdot a, b, c\right)\\

\mathbf{else}:\\
\;\;\;\;t\_1\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (+.f64 (*.f64 x y) (/.f64 (*.f64 z t) #s(literal 16 binary64))) < -1.9999999999999998e165 or 2e114 < (+.f64 (*.f64 x y) (/.f64 (*.f64 z t) #s(literal 16 binary64)))

    1. Initial program 95.5%

      \[\left(\left(x \cdot y + \frac{z \cdot t}{16}\right) - \frac{a \cdot b}{4}\right) + c \]
    2. Add Preprocessing
    3. Taylor expanded in a around 0

      \[\leadsto \color{blue}{c + \left(\frac{1}{16} \cdot \left(t \cdot z\right) + x \cdot y\right)} \]
    4. Step-by-step derivation
      1. associate-+r+N/A

        \[\leadsto \color{blue}{\left(c + \frac{1}{16} \cdot \left(t \cdot z\right)\right) + x \cdot y} \]
      2. +-commutativeN/A

        \[\leadsto \color{blue}{\left(\frac{1}{16} \cdot \left(t \cdot z\right) + c\right)} + x \cdot y \]
      3. associate-+l+N/A

        \[\leadsto \color{blue}{\frac{1}{16} \cdot \left(t \cdot z\right) + \left(c + x \cdot y\right)} \]
      4. *-commutativeN/A

        \[\leadsto \frac{1}{16} \cdot \color{blue}{\left(z \cdot t\right)} + \left(c + x \cdot y\right) \]
      5. associate-*r*N/A

        \[\leadsto \color{blue}{\left(\frac{1}{16} \cdot z\right) \cdot t} + \left(c + x \cdot y\right) \]
      6. lower-fma.f64N/A

        \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{1}{16} \cdot z, t, c + x \cdot y\right)} \]
      7. *-commutativeN/A

        \[\leadsto \mathsf{fma}\left(\color{blue}{z \cdot \frac{1}{16}}, t, c + x \cdot y\right) \]
      8. lower-*.f64N/A

        \[\leadsto \mathsf{fma}\left(\color{blue}{z \cdot \frac{1}{16}}, t, c + x \cdot y\right) \]
      9. +-commutativeN/A

        \[\leadsto \mathsf{fma}\left(z \cdot \frac{1}{16}, t, \color{blue}{x \cdot y + c}\right) \]
      10. *-commutativeN/A

        \[\leadsto \mathsf{fma}\left(z \cdot \frac{1}{16}, t, \color{blue}{y \cdot x} + c\right) \]
      11. lower-fma.f6486.9

        \[\leadsto \mathsf{fma}\left(z \cdot 0.0625, t, \color{blue}{\mathsf{fma}\left(y, x, c\right)}\right) \]
    5. Applied rewrites86.9%

      \[\leadsto \color{blue}{\mathsf{fma}\left(z \cdot 0.0625, t, \mathsf{fma}\left(y, x, c\right)\right)} \]
    6. Taylor expanded in y around inf

      \[\leadsto \mathsf{fma}\left(z \cdot \frac{1}{16}, t, x \cdot y\right) \]
    7. Step-by-step derivation
      1. Applied rewrites80.4%

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

      if -1.9999999999999998e165 < (+.f64 (*.f64 x y) (/.f64 (*.f64 z t) #s(literal 16 binary64))) < 2e114

      1. Initial program 99.9%

        \[\left(\left(x \cdot y + \frac{z \cdot t}{16}\right) - \frac{a \cdot b}{4}\right) + c \]
      2. Add Preprocessing
      3. Taylor expanded in x around 0

        \[\leadsto \color{blue}{\left(c + \frac{1}{16} \cdot \left(t \cdot z\right)\right) - \frac{1}{4} \cdot \left(a \cdot b\right)} \]
      4. Step-by-step derivation
        1. cancel-sign-sub-invN/A

          \[\leadsto \color{blue}{\left(c + \frac{1}{16} \cdot \left(t \cdot z\right)\right) + \left(\mathsf{neg}\left(\frac{1}{4}\right)\right) \cdot \left(a \cdot b\right)} \]
        2. metadata-evalN/A

          \[\leadsto \left(c + \frac{1}{16} \cdot \left(t \cdot z\right)\right) + \color{blue}{\frac{-1}{4}} \cdot \left(a \cdot b\right) \]
        3. +-commutativeN/A

          \[\leadsto \color{blue}{\frac{-1}{4} \cdot \left(a \cdot b\right) + \left(c + \frac{1}{16} \cdot \left(t \cdot z\right)\right)} \]
        4. associate-*r*N/A

          \[\leadsto \color{blue}{\left(\frac{-1}{4} \cdot a\right) \cdot b} + \left(c + \frac{1}{16} \cdot \left(t \cdot z\right)\right) \]
        5. lower-fma.f64N/A

          \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{-1}{4} \cdot a, b, c + \frac{1}{16} \cdot \left(t \cdot z\right)\right)} \]
        6. lower-*.f64N/A

          \[\leadsto \mathsf{fma}\left(\color{blue}{\frac{-1}{4} \cdot a}, b, c + \frac{1}{16} \cdot \left(t \cdot z\right)\right) \]
        7. +-commutativeN/A

          \[\leadsto \mathsf{fma}\left(\frac{-1}{4} \cdot a, b, \color{blue}{\frac{1}{16} \cdot \left(t \cdot z\right) + c}\right) \]
        8. *-commutativeN/A

          \[\leadsto \mathsf{fma}\left(\frac{-1}{4} \cdot a, b, \frac{1}{16} \cdot \color{blue}{\left(z \cdot t\right)} + c\right) \]
        9. associate-*r*N/A

          \[\leadsto \mathsf{fma}\left(\frac{-1}{4} \cdot a, b, \color{blue}{\left(\frac{1}{16} \cdot z\right) \cdot t} + c\right) \]
        10. lower-fma.f64N/A

          \[\leadsto \mathsf{fma}\left(\frac{-1}{4} \cdot a, b, \color{blue}{\mathsf{fma}\left(\frac{1}{16} \cdot z, t, c\right)}\right) \]
        11. *-commutativeN/A

          \[\leadsto \mathsf{fma}\left(\frac{-1}{4} \cdot a, b, \mathsf{fma}\left(\color{blue}{z \cdot \frac{1}{16}}, t, c\right)\right) \]
        12. lower-*.f6487.4

          \[\leadsto \mathsf{fma}\left(-0.25 \cdot a, b, \mathsf{fma}\left(\color{blue}{z \cdot 0.0625}, t, c\right)\right) \]
      5. Applied rewrites87.4%

        \[\leadsto \color{blue}{\mathsf{fma}\left(-0.25 \cdot a, b, \mathsf{fma}\left(z \cdot 0.0625, t, c\right)\right)} \]
      6. Taylor expanded in z around 0

        \[\leadsto c + \color{blue}{\frac{-1}{4} \cdot \left(a \cdot b\right)} \]
      7. Step-by-step derivation
        1. Applied rewrites75.3%

          \[\leadsto \mathsf{fma}\left(-0.25 \cdot a, \color{blue}{b}, c\right) \]
      8. Recombined 2 regimes into one program.
      9. Final simplification77.9%

        \[\leadsto \begin{array}{l} \mathbf{if}\;y \cdot x + \frac{t \cdot z}{16} \leq -2 \cdot 10^{+165}:\\ \;\;\;\;\mathsf{fma}\left(0.0625 \cdot z, t, y \cdot x\right)\\ \mathbf{elif}\;y \cdot x + \frac{t \cdot z}{16} \leq 2 \cdot 10^{+114}:\\ \;\;\;\;\mathsf{fma}\left(-0.25 \cdot a, b, c\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(0.0625 \cdot z, t, y \cdot x\right)\\ \end{array} \]
      10. Add Preprocessing

      Reproduce

      ?
      herbie shell --seed 2024230 
      (FPCore (x y z t a b c)
        :name "Diagrams.Solve.Polynomial:quartForm  from diagrams-solve-0.1, C"
        :precision binary64
        (+ (- (+ (* x y) (/ (* z t) 16.0)) (/ (* a b) 4.0)) c))