Diagrams.Solve.Tridiagonal:solveCyclicTriDiagonal from diagrams-solve-0.1, A

Percentage Accurate: 92.0% → 99.7%
Time: 4.2s
Alternatives: 3
Speedup: 0.2×

Specification

?
\[\frac{x \cdot y}{z} \]
(FPCore (x y z)
  :precision binary64
  :pre TRUE
  (/ (* x y) z))
double code(double x, double y, double z) {
	return (x * y) / z;
}
real(8) function code(x, y, z)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = (x * y) / z
end function
public static double code(double x, double y, double z) {
	return (x * y) / z;
}
def code(x, y, z):
	return (x * y) / z
function code(x, y, z)
	return Float64(Float64(x * y) / z)
end
function tmp = code(x, y, z)
	tmp = (x * y) / z;
end
code[x_, y_, z_] := N[(N[(x * y), $MachinePrecision] / z), $MachinePrecision]
f(x, y, z):
	x in [-inf, +inf],
	y in [-inf, +inf],
	z in [-inf, +inf]
code: THEORY
BEGIN
f(x, y, z: real): real =
	(x * y) / z
END code
\frac{x \cdot y}{z}

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: 92.0% accurate, 1.0× speedup?

\[\frac{x \cdot y}{z} \]
(FPCore (x y z)
  :precision binary64
  :pre TRUE
  (/ (* x y) z))
double code(double x, double y, double z) {
	return (x * y) / z;
}
real(8) function code(x, y, z)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = (x * y) / z
end function
public static double code(double x, double y, double z) {
	return (x * y) / z;
}
def code(x, y, z):
	return (x * y) / z
function code(x, y, z)
	return Float64(Float64(x * y) / z)
end
function tmp = code(x, y, z)
	tmp = (x * y) / z;
end
code[x_, y_, z_] := N[(N[(x * y), $MachinePrecision] / z), $MachinePrecision]
f(x, y, z):
	x in [-inf, +inf],
	y in [-inf, +inf],
	z in [-inf, +inf]
code: THEORY
BEGIN
f(x, y, z: real): real =
	(x * y) / z
END code
\frac{x \cdot y}{z}

Alternative 1: 99.7% accurate, 0.2× speedup?

\[\begin{array}{l} t_0 := \mathsf{max}\left(\left|x\right|, \left|y\right|\right)\\ t_1 := \mathsf{min}\left(\left|x\right|, \left|y\right|\right)\\ \mathsf{copysign}\left(1, x\right) \cdot \left(\mathsf{copysign}\left(1, y\right) \cdot \left(\mathsf{copysign}\left(1, z\right) \cdot \begin{array}{l} \mathbf{if}\;\left|z\right| \leq 5.993716184525197 \cdot 10^{-31}:\\ \;\;\;\;t\_0 \cdot \frac{t\_1}{\left|z\right|}\\ \mathbf{else}:\\ \;\;\;\;\frac{t\_1}{\frac{\left|z\right|}{t\_0}}\\ \end{array}\right)\right) \end{array} \]
(FPCore (x y z)
  :precision binary64
  :pre TRUE
  (let* ((t_0 (fmax (fabs x) (fabs y))) (t_1 (fmin (fabs x) (fabs y))))
  (*
   (copysign 1.0 x)
   (*
    (copysign 1.0 y)
    (*
     (copysign 1.0 z)
     (if (<= (fabs z) 5.993716184525197e-31)
       (* t_0 (/ t_1 (fabs z)))
       (/ t_1 (/ (fabs z) t_0))))))))
double code(double x, double y, double z) {
	double t_0 = fmax(fabs(x), fabs(y));
	double t_1 = fmin(fabs(x), fabs(y));
	double tmp;
	if (fabs(z) <= 5.993716184525197e-31) {
		tmp = t_0 * (t_1 / fabs(z));
	} else {
		tmp = t_1 / (fabs(z) / t_0);
	}
	return copysign(1.0, x) * (copysign(1.0, y) * (copysign(1.0, z) * tmp));
}
public static double code(double x, double y, double z) {
	double t_0 = fmax(Math.abs(x), Math.abs(y));
	double t_1 = fmin(Math.abs(x), Math.abs(y));
	double tmp;
	if (Math.abs(z) <= 5.993716184525197e-31) {
		tmp = t_0 * (t_1 / Math.abs(z));
	} else {
		tmp = t_1 / (Math.abs(z) / t_0);
	}
	return Math.copySign(1.0, x) * (Math.copySign(1.0, y) * (Math.copySign(1.0, z) * tmp));
}
def code(x, y, z):
	t_0 = fmax(math.fabs(x), math.fabs(y))
	t_1 = fmin(math.fabs(x), math.fabs(y))
	tmp = 0
	if math.fabs(z) <= 5.993716184525197e-31:
		tmp = t_0 * (t_1 / math.fabs(z))
	else:
		tmp = t_1 / (math.fabs(z) / t_0)
	return math.copysign(1.0, x) * (math.copysign(1.0, y) * (math.copysign(1.0, z) * tmp))
function code(x, y, z)
	t_0 = fmax(abs(x), abs(y))
	t_1 = fmin(abs(x), abs(y))
	tmp = 0.0
	if (abs(z) <= 5.993716184525197e-31)
		tmp = Float64(t_0 * Float64(t_1 / abs(z)));
	else
		tmp = Float64(t_1 / Float64(abs(z) / t_0));
	end
	return Float64(copysign(1.0, x) * Float64(copysign(1.0, y) * Float64(copysign(1.0, z) * tmp)))
end
function tmp_2 = code(x, y, z)
	t_0 = max(abs(x), abs(y));
	t_1 = min(abs(x), abs(y));
	tmp = 0.0;
	if (abs(z) <= 5.993716184525197e-31)
		tmp = t_0 * (t_1 / abs(z));
	else
		tmp = t_1 / (abs(z) / t_0);
	end
	tmp_2 = (sign(x) * abs(1.0)) * ((sign(y) * abs(1.0)) * ((sign(z) * abs(1.0)) * tmp));
end
code[x_, y_, z_] := Block[{t$95$0 = N[Max[N[Abs[x], $MachinePrecision], N[Abs[y], $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[Min[N[Abs[x], $MachinePrecision], N[Abs[y], $MachinePrecision]], $MachinePrecision]}, N[(N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision] * N[(N[With[{TMP1 = Abs[1.0], TMP2 = Sign[y]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision] * N[(N[With[{TMP1 = Abs[1.0], TMP2 = Sign[z]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision] * If[LessEqual[N[Abs[z], $MachinePrecision], 5.993716184525197e-31], N[(t$95$0 * N[(t$95$1 / N[Abs[z], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(t$95$1 / N[(N[Abs[z], $MachinePrecision] / t$95$0), $MachinePrecision]), $MachinePrecision]]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
t_0 := \mathsf{max}\left(\left|x\right|, \left|y\right|\right)\\
t_1 := \mathsf{min}\left(\left|x\right|, \left|y\right|\right)\\
\mathsf{copysign}\left(1, x\right) \cdot \left(\mathsf{copysign}\left(1, y\right) \cdot \left(\mathsf{copysign}\left(1, z\right) \cdot \begin{array}{l}
\mathbf{if}\;\left|z\right| \leq 5.993716184525197 \cdot 10^{-31}:\\
\;\;\;\;t\_0 \cdot \frac{t\_1}{\left|z\right|}\\

\mathbf{else}:\\
\;\;\;\;\frac{t\_1}{\frac{\left|z\right|}{t\_0}}\\


\end{array}\right)\right)
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if z < 5.9937161845251967e-31

    1. Initial program 92.0%

      \[\frac{x \cdot y}{z} \]
    2. Step-by-step derivation
      1. Applied rewrites92.0%

        \[\leadsto y \cdot \frac{x}{z} \]

      if 5.9937161845251967e-31 < z

      1. Initial program 92.0%

        \[\frac{x \cdot y}{z} \]
      2. Step-by-step derivation
        1. Applied rewrites91.9%

          \[\leadsto \left(y \cdot x\right) \cdot \frac{1}{z} \]
        2. Step-by-step derivation
          1. Applied rewrites91.8%

            \[\leadsto \frac{x}{\frac{z}{y}} \]
        3. Recombined 2 regimes into one program.
        4. Add Preprocessing

        Alternative 2: 98.7% accurate, 0.2× speedup?

        \[\begin{array}{l} t_0 := \mathsf{max}\left(\left|x\right|, \left|y\right|\right)\\ t_1 := \mathsf{min}\left(\left|x\right|, \left|y\right|\right)\\ \mathsf{copysign}\left(1, x\right) \cdot \left(\mathsf{copysign}\left(1, y\right) \cdot \left(\mathsf{copysign}\left(1, z\right) \cdot \begin{array}{l} \mathbf{if}\;\left|z\right| \leq 1.8715037001475357 \cdot 10^{-84}:\\ \;\;\;\;t\_0 \cdot \frac{t\_1}{\left|z\right|}\\ \mathbf{else}:\\ \;\;\;\;t\_1 \cdot \frac{t\_0}{\left|z\right|}\\ \end{array}\right)\right) \end{array} \]
        (FPCore (x y z)
          :precision binary64
          :pre TRUE
          (let* ((t_0 (fmax (fabs x) (fabs y))) (t_1 (fmin (fabs x) (fabs y))))
          (*
           (copysign 1.0 x)
           (*
            (copysign 1.0 y)
            (*
             (copysign 1.0 z)
             (if (<= (fabs z) 1.8715037001475357e-84)
               (* t_0 (/ t_1 (fabs z)))
               (* t_1 (/ t_0 (fabs z)))))))))
        double code(double x, double y, double z) {
        	double t_0 = fmax(fabs(x), fabs(y));
        	double t_1 = fmin(fabs(x), fabs(y));
        	double tmp;
        	if (fabs(z) <= 1.8715037001475357e-84) {
        		tmp = t_0 * (t_1 / fabs(z));
        	} else {
        		tmp = t_1 * (t_0 / fabs(z));
        	}
        	return copysign(1.0, x) * (copysign(1.0, y) * (copysign(1.0, z) * tmp));
        }
        
        public static double code(double x, double y, double z) {
        	double t_0 = fmax(Math.abs(x), Math.abs(y));
        	double t_1 = fmin(Math.abs(x), Math.abs(y));
        	double tmp;
        	if (Math.abs(z) <= 1.8715037001475357e-84) {
        		tmp = t_0 * (t_1 / Math.abs(z));
        	} else {
        		tmp = t_1 * (t_0 / Math.abs(z));
        	}
        	return Math.copySign(1.0, x) * (Math.copySign(1.0, y) * (Math.copySign(1.0, z) * tmp));
        }
        
        def code(x, y, z):
        	t_0 = fmax(math.fabs(x), math.fabs(y))
        	t_1 = fmin(math.fabs(x), math.fabs(y))
        	tmp = 0
        	if math.fabs(z) <= 1.8715037001475357e-84:
        		tmp = t_0 * (t_1 / math.fabs(z))
        	else:
        		tmp = t_1 * (t_0 / math.fabs(z))
        	return math.copysign(1.0, x) * (math.copysign(1.0, y) * (math.copysign(1.0, z) * tmp))
        
        function code(x, y, z)
        	t_0 = fmax(abs(x), abs(y))
        	t_1 = fmin(abs(x), abs(y))
        	tmp = 0.0
        	if (abs(z) <= 1.8715037001475357e-84)
        		tmp = Float64(t_0 * Float64(t_1 / abs(z)));
        	else
        		tmp = Float64(t_1 * Float64(t_0 / abs(z)));
        	end
        	return Float64(copysign(1.0, x) * Float64(copysign(1.0, y) * Float64(copysign(1.0, z) * tmp)))
        end
        
        function tmp_2 = code(x, y, z)
        	t_0 = max(abs(x), abs(y));
        	t_1 = min(abs(x), abs(y));
        	tmp = 0.0;
        	if (abs(z) <= 1.8715037001475357e-84)
        		tmp = t_0 * (t_1 / abs(z));
        	else
        		tmp = t_1 * (t_0 / abs(z));
        	end
        	tmp_2 = (sign(x) * abs(1.0)) * ((sign(y) * abs(1.0)) * ((sign(z) * abs(1.0)) * tmp));
        end
        
        code[x_, y_, z_] := Block[{t$95$0 = N[Max[N[Abs[x], $MachinePrecision], N[Abs[y], $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[Min[N[Abs[x], $MachinePrecision], N[Abs[y], $MachinePrecision]], $MachinePrecision]}, N[(N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision] * N[(N[With[{TMP1 = Abs[1.0], TMP2 = Sign[y]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision] * N[(N[With[{TMP1 = Abs[1.0], TMP2 = Sign[z]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision] * If[LessEqual[N[Abs[z], $MachinePrecision], 1.8715037001475357e-84], N[(t$95$0 * N[(t$95$1 / N[Abs[z], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(t$95$1 * N[(t$95$0 / N[Abs[z], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
        
        \begin{array}{l}
        t_0 := \mathsf{max}\left(\left|x\right|, \left|y\right|\right)\\
        t_1 := \mathsf{min}\left(\left|x\right|, \left|y\right|\right)\\
        \mathsf{copysign}\left(1, x\right) \cdot \left(\mathsf{copysign}\left(1, y\right) \cdot \left(\mathsf{copysign}\left(1, z\right) \cdot \begin{array}{l}
        \mathbf{if}\;\left|z\right| \leq 1.8715037001475357 \cdot 10^{-84}:\\
        \;\;\;\;t\_0 \cdot \frac{t\_1}{\left|z\right|}\\
        
        \mathbf{else}:\\
        \;\;\;\;t\_1 \cdot \frac{t\_0}{\left|z\right|}\\
        
        
        \end{array}\right)\right)
        \end{array}
        
        Derivation
        1. Split input into 2 regimes
        2. if z < 1.8715037001475357e-84

          1. Initial program 92.0%

            \[\frac{x \cdot y}{z} \]
          2. Step-by-step derivation
            1. Applied rewrites92.0%

              \[\leadsto y \cdot \frac{x}{z} \]

            if 1.8715037001475357e-84 < z

            1. Initial program 92.0%

              \[\frac{x \cdot y}{z} \]
            2. Step-by-step derivation
              1. Applied rewrites91.9%

                \[\leadsto x \cdot \frac{y}{z} \]
            3. Recombined 2 regimes into one program.
            4. Add Preprocessing

            Alternative 3: 91.9% accurate, 1.0× speedup?

            \[x \cdot \frac{y}{z} \]
            (FPCore (x y z)
              :precision binary64
              :pre TRUE
              (* x (/ y z)))
            double code(double x, double y, double z) {
            	return x * (y / z);
            }
            
            real(8) function code(x, y, z)
            use fmin_fmax_functions
                real(8), intent (in) :: x
                real(8), intent (in) :: y
                real(8), intent (in) :: z
                code = x * (y / z)
            end function
            
            public static double code(double x, double y, double z) {
            	return x * (y / z);
            }
            
            def code(x, y, z):
            	return x * (y / z)
            
            function code(x, y, z)
            	return Float64(x * Float64(y / z))
            end
            
            function tmp = code(x, y, z)
            	tmp = x * (y / z);
            end
            
            code[x_, y_, z_] := N[(x * N[(y / z), $MachinePrecision]), $MachinePrecision]
            
            f(x, y, z):
            	x in [-inf, +inf],
            	y in [-inf, +inf],
            	z in [-inf, +inf]
            code: THEORY
            BEGIN
            f(x, y, z: real): real =
            	x * (y / z)
            END code
            x \cdot \frac{y}{z}
            
            Derivation
            1. Initial program 92.0%

              \[\frac{x \cdot y}{z} \]
            2. Step-by-step derivation
              1. Applied rewrites91.9%

                \[\leadsto x \cdot \frac{y}{z} \]
              2. Add Preprocessing

              Reproduce

              ?
              herbie shell --seed 2026092 
              (FPCore (x y z)
                :name "Diagrams.Solve.Tridiagonal:solveCyclicTriDiagonal from diagrams-solve-0.1, A"
                :precision binary64
                (/ (* x y) z))