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

Percentage Accurate: 99.8% → 99.8%
Time: 6.8s
Alternatives: 3
Speedup: 1.0×

Specification

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

\\
\frac{1}{2} \cdot \left(x + y \cdot \sqrt{z}\right)
\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 3 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} \\ \frac{1}{2} \cdot \left(x + y \cdot \sqrt{z}\right) \end{array} \]
(FPCore (x y z) :precision binary64 (* (/ 1.0 2.0) (+ x (* y (sqrt z)))))
double code(double x, double y, double z) {
	return (1.0 / 2.0) * (x + (y * sqrt(z)));
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = (1.0d0 / 2.0d0) * (x + (y * sqrt(z)))
end function
public static double code(double x, double y, double z) {
	return (1.0 / 2.0) * (x + (y * Math.sqrt(z)));
}
def code(x, y, z):
	return (1.0 / 2.0) * (x + (y * math.sqrt(z)))
function code(x, y, z)
	return Float64(Float64(1.0 / 2.0) * Float64(x + Float64(y * sqrt(z))))
end
function tmp = code(x, y, z)
	tmp = (1.0 / 2.0) * (x + (y * sqrt(z)));
end
code[x_, y_, z_] := N[(N[(1.0 / 2.0), $MachinePrecision] * N[(x + N[(y * N[Sqrt[z], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{1}{2} \cdot \left(x + y \cdot \sqrt{z}\right)
\end{array}

Alternative 1: 99.8% accurate, 1.0× speedup?

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

\\
0.5 \cdot \left(x + y \cdot \sqrt{z}\right)
\end{array}
Derivation
  1. Initial program 99.7%

    \[\frac{1}{2} \cdot \left(x + y \cdot \sqrt{z}\right) \]
  2. Add Preprocessing
  3. Final simplification99.7%

    \[\leadsto 0.5 \cdot \left(x + y \cdot \sqrt{z}\right) \]
  4. Add Preprocessing

Alternative 2: 76.9% accurate, 0.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := y \cdot \sqrt{z}\\ \mathbf{if}\;t\_0 \leq -20000000000000 \lor \neg \left(t\_0 \leq 100\right):\\ \;\;\;\;0.5 \cdot t\_0\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot x\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (let* ((t_0 (* y (sqrt z))))
   (if (or (<= t_0 -20000000000000.0) (not (<= t_0 100.0)))
     (* 0.5 t_0)
     (* 0.5 x))))
double code(double x, double y, double z) {
	double t_0 = y * sqrt(z);
	double tmp;
	if ((t_0 <= -20000000000000.0) || !(t_0 <= 100.0)) {
		tmp = 0.5 * t_0;
	} else {
		tmp = 0.5 * x;
	}
	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) :: t_0
    real(8) :: tmp
    t_0 = y * sqrt(z)
    if ((t_0 <= (-20000000000000.0d0)) .or. (.not. (t_0 <= 100.0d0))) then
        tmp = 0.5d0 * t_0
    else
        tmp = 0.5d0 * x
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double t_0 = y * Math.sqrt(z);
	double tmp;
	if ((t_0 <= -20000000000000.0) || !(t_0 <= 100.0)) {
		tmp = 0.5 * t_0;
	} else {
		tmp = 0.5 * x;
	}
	return tmp;
}
def code(x, y, z):
	t_0 = y * math.sqrt(z)
	tmp = 0
	if (t_0 <= -20000000000000.0) or not (t_0 <= 100.0):
		tmp = 0.5 * t_0
	else:
		tmp = 0.5 * x
	return tmp
function code(x, y, z)
	t_0 = Float64(y * sqrt(z))
	tmp = 0.0
	if ((t_0 <= -20000000000000.0) || !(t_0 <= 100.0))
		tmp = Float64(0.5 * t_0);
	else
		tmp = Float64(0.5 * x);
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	t_0 = y * sqrt(z);
	tmp = 0.0;
	if ((t_0 <= -20000000000000.0) || ~((t_0 <= 100.0)))
		tmp = 0.5 * t_0;
	else
		tmp = 0.5 * x;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := Block[{t$95$0 = N[(y * N[Sqrt[z], $MachinePrecision]), $MachinePrecision]}, If[Or[LessEqual[t$95$0, -20000000000000.0], N[Not[LessEqual[t$95$0, 100.0]], $MachinePrecision]], N[(0.5 * t$95$0), $MachinePrecision], N[(0.5 * x), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := y \cdot \sqrt{z}\\
\mathbf{if}\;t\_0 \leq -20000000000000 \lor \neg \left(t\_0 \leq 100\right):\\
\;\;\;\;0.5 \cdot t\_0\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot x\\


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

    1. Initial program 99.6%

      \[\frac{1}{2} \cdot \left(x + y \cdot \sqrt{z}\right) \]
    2. Add Preprocessing
    3. Taylor expanded in y around inf 98.8%

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

      \[\leadsto \frac{1}{2} \cdot \left(y \cdot \color{blue}{\sqrt{z}}\right) \]
    5. Step-by-step derivation
      1. metadata-eval80.5%

        \[\leadsto \color{blue}{0.5} \cdot \left(y \cdot \sqrt{z}\right) \]
      2. *-commutative80.5%

        \[\leadsto \color{blue}{\left(y \cdot \sqrt{z}\right) \cdot 0.5} \]
    6. Applied egg-rr80.5%

      \[\leadsto \color{blue}{\left(y \cdot \sqrt{z}\right) \cdot 0.5} \]

    if -2e13 < (*.f64 y (sqrt.f64 z)) < 100

    1. Initial program 99.9%

      \[\frac{1}{2} \cdot \left(x + y \cdot \sqrt{z}\right) \]
    2. Add Preprocessing
    3. Taylor expanded in x around inf 81.3%

      \[\leadsto \frac{1}{2} \cdot \color{blue}{x} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification80.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \cdot \sqrt{z} \leq -20000000000000 \lor \neg \left(y \cdot \sqrt{z} \leq 100\right):\\ \;\;\;\;0.5 \cdot \left(y \cdot \sqrt{z}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot x\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 51.4% accurate, 36.3× speedup?

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

\\
0.5 \cdot x
\end{array}
Derivation
  1. Initial program 99.7%

    \[\frac{1}{2} \cdot \left(x + y \cdot \sqrt{z}\right) \]
  2. Add Preprocessing
  3. Taylor expanded in x around inf 49.1%

    \[\leadsto \frac{1}{2} \cdot \color{blue}{x} \]
  4. Final simplification49.1%

    \[\leadsto 0.5 \cdot x \]
  5. Add Preprocessing

Reproduce

?
herbie shell --seed 2024096 
(FPCore (x y z)
  :name "Diagrams.Solve.Polynomial:quadForm from diagrams-solve-0.1, B"
  :precision binary64
  (* (/ 1.0 2.0) (+ x (* y (sqrt z)))))