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

?

Percentage Accurate: 92.3% → 95.6%
Time: 2.4s
Precision: binary64
Cost: 1100

?

\[ \begin{array}{c}[x, y] = \mathsf{sort}([x, y])\\ \end{array} \]
\[\frac{x \cdot y}{z} \]
\[\begin{array}{l} t_0 := \frac{x \cdot y}{z}\\ \mathbf{if}\;x \cdot y \leq -4 \cdot 10^{-73}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \cdot y \leq 10^{-166}:\\ \;\;\;\;\frac{x}{\frac{z}{y}}\\ \mathbf{elif}\;x \cdot y \leq 10^{+66}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;x \cdot \frac{y}{z}\\ \end{array} \]
(FPCore (x y z) :precision binary64 (/ (* x y) z))
(FPCore (x y z)
 :precision binary64
 (let* ((t_0 (/ (* x y) z)))
   (if (<= (* x y) -4e-73)
     t_0
     (if (<= (* x y) 1e-166)
       (/ x (/ z y))
       (if (<= (* x y) 1e+66) t_0 (* x (/ y z)))))))
double code(double x, double y, double z) {
	return (x * y) / z;
}
double code(double x, double y, double z) {
	double t_0 = (x * y) / z;
	double tmp;
	if ((x * y) <= -4e-73) {
		tmp = t_0;
	} else if ((x * y) <= 1e-166) {
		tmp = x / (z / y);
	} else if ((x * y) <= 1e+66) {
		tmp = t_0;
	} else {
		tmp = x * (y / 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
    code = (x * y) / z
end function
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 = (x * y) / z
    if ((x * y) <= (-4d-73)) then
        tmp = t_0
    else if ((x * y) <= 1d-166) then
        tmp = x / (z / y)
    else if ((x * y) <= 1d+66) then
        tmp = t_0
    else
        tmp = x * (y / z)
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	return (x * y) / z;
}
public static double code(double x, double y, double z) {
	double t_0 = (x * y) / z;
	double tmp;
	if ((x * y) <= -4e-73) {
		tmp = t_0;
	} else if ((x * y) <= 1e-166) {
		tmp = x / (z / y);
	} else if ((x * y) <= 1e+66) {
		tmp = t_0;
	} else {
		tmp = x * (y / z);
	}
	return tmp;
}
def code(x, y, z):
	return (x * y) / z
def code(x, y, z):
	t_0 = (x * y) / z
	tmp = 0
	if (x * y) <= -4e-73:
		tmp = t_0
	elif (x * y) <= 1e-166:
		tmp = x / (z / y)
	elif (x * y) <= 1e+66:
		tmp = t_0
	else:
		tmp = x * (y / z)
	return tmp
function code(x, y, z)
	return Float64(Float64(x * y) / z)
end
function code(x, y, z)
	t_0 = Float64(Float64(x * y) / z)
	tmp = 0.0
	if (Float64(x * y) <= -4e-73)
		tmp = t_0;
	elseif (Float64(x * y) <= 1e-166)
		tmp = Float64(x / Float64(z / y));
	elseif (Float64(x * y) <= 1e+66)
		tmp = t_0;
	else
		tmp = Float64(x * Float64(y / z));
	end
	return tmp
end
function tmp = code(x, y, z)
	tmp = (x * y) / z;
end
function tmp_2 = code(x, y, z)
	t_0 = (x * y) / z;
	tmp = 0.0;
	if ((x * y) <= -4e-73)
		tmp = t_0;
	elseif ((x * y) <= 1e-166)
		tmp = x / (z / y);
	elseif ((x * y) <= 1e+66)
		tmp = t_0;
	else
		tmp = x * (y / z);
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := N[(N[(x * y), $MachinePrecision] / z), $MachinePrecision]
code[x_, y_, z_] := Block[{t$95$0 = N[(N[(x * y), $MachinePrecision] / z), $MachinePrecision]}, If[LessEqual[N[(x * y), $MachinePrecision], -4e-73], t$95$0, If[LessEqual[N[(x * y), $MachinePrecision], 1e-166], N[(x / N[(z / y), $MachinePrecision]), $MachinePrecision], If[LessEqual[N[(x * y), $MachinePrecision], 1e+66], t$95$0, N[(x * N[(y / z), $MachinePrecision]), $MachinePrecision]]]]]
\frac{x \cdot y}{z}
\begin{array}{l}
t_0 := \frac{x \cdot y}{z}\\
\mathbf{if}\;x \cdot y \leq -4 \cdot 10^{-73}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;x \cdot y \leq 10^{-166}:\\
\;\;\;\;\frac{x}{\frac{z}{y}}\\

\mathbf{elif}\;x \cdot y \leq 10^{+66}:\\
\;\;\;\;t_0\\

\mathbf{else}:\\
\;\;\;\;x \cdot \frac{y}{z}\\


\end{array}

Local Percentage Accuracy?

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.

Herbie found 5 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

The accuracy (vertical axis) and speed (horizontal axis) of each of Herbie's proposed alternatives. Up and to the right is better. Each dot represents an alternative program; the red square represents the initial program.

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original92.3%
Target92.5%
Herbie95.6%
\[\begin{array}{l} \mathbf{if}\;z < -4.262230790519429 \cdot 10^{-138}:\\ \;\;\;\;\frac{x \cdot y}{z}\\ \mathbf{elif}\;z < 1.7042130660650472 \cdot 10^{-164}:\\ \;\;\;\;\frac{x}{\frac{z}{y}}\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{z} \cdot y\\ \end{array} \]

Derivation?

  1. Split input into 3 regimes
  2. if (*.f64 x y) < -3.99999999999999999e-73 or 1.00000000000000004e-166 < (*.f64 x y) < 9.99999999999999945e65

    1. Initial program 96.5%

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

    if -3.99999999999999999e-73 < (*.f64 x y) < 1.00000000000000004e-166

    1. Initial program 91.6%

      \[\frac{x \cdot y}{z} \]
    2. Simplified98.7%

      \[\leadsto \color{blue}{\frac{x}{\frac{z}{y}}} \]
      Step-by-step derivation

      [Start]91.6

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

      associate-/l* [=>]98.7

      \[ \color{blue}{\frac{x}{\frac{z}{y}}} \]

    if 9.99999999999999945e65 < (*.f64 x y)

    1. Initial program 90.6%

      \[\frac{x \cdot y}{z} \]
    2. Simplified96.1%

      \[\leadsto \color{blue}{x \cdot \frac{y}{z}} \]
      Step-by-step derivation

      [Start]90.6

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

      associate-*r/ [<=]96.1

      \[ \color{blue}{x \cdot \frac{y}{z}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification97.2%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \cdot y \leq -4 \cdot 10^{-73}:\\ \;\;\;\;\frac{x \cdot y}{z}\\ \mathbf{elif}\;x \cdot y \leq 10^{-166}:\\ \;\;\;\;\frac{x}{\frac{z}{y}}\\ \mathbf{elif}\;x \cdot y \leq 10^{+66}:\\ \;\;\;\;\frac{x \cdot y}{z}\\ \mathbf{else}:\\ \;\;\;\;x \cdot \frac{y}{z}\\ \end{array} \]

Alternatives

Alternative 1
Accuracy92.2%
Cost585
\[\begin{array}{l} \mathbf{if}\;x \leq -1.3 \cdot 10^{-55} \lor \neg \left(x \leq 9.5 \cdot 10^{-202}\right):\\ \;\;\;\;x \cdot \frac{y}{z}\\ \mathbf{else}:\\ \;\;\;\;y \cdot \frac{x}{z}\\ \end{array} \]
Alternative 2
Accuracy92.2%
Cost584
\[\begin{array}{l} \mathbf{if}\;x \leq -2.1 \cdot 10^{-55}:\\ \;\;\;\;x \cdot \frac{y}{z}\\ \mathbf{elif}\;x \leq -7.5 \cdot 10^{-186}:\\ \;\;\;\;y \cdot \frac{x}{z}\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{\frac{z}{y}}\\ \end{array} \]
Alternative 3
Accuracy92.0%
Cost584
\[\begin{array}{l} \mathbf{if}\;x \leq -1 \cdot 10^{+27}:\\ \;\;\;\;x \cdot \frac{y}{z}\\ \mathbf{elif}\;x \leq -3.6 \cdot 10^{-185}:\\ \;\;\;\;\frac{y}{\frac{z}{x}}\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{\frac{z}{y}}\\ \end{array} \]
Alternative 4
Accuracy91.9%
Cost320
\[x \cdot \frac{y}{z} \]

Reproduce?

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

  :herbie-target
  (if (< z -4.262230790519429e-138) (/ (* x y) z) (if (< z 1.7042130660650472e-164) (/ x (/ z y)) (* (/ x z) y)))

  (/ (* x y) z))