Optimisation.CirclePacking:place from circle-packing-0.1.0.4, I

Percentage Accurate: 100.0% → 100.0%
Time: 1.6s
Alternatives: 6
Speedup: 1.0×

Specification

?
\[\begin{array}{l} \\ \left(x + y\right) + z \end{array} \]
(FPCore (x y z) :precision binary64 (+ (+ x y) z))
double code(double x, double y, double z) {
	return (x + 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 + 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]
\begin{array}{l}

\\
\left(x + y\right) + 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 6 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: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \left(x + y\right) + z \end{array} \]
(FPCore (x y z) :precision binary64 (+ (+ x y) z))
double code(double x, double y, double z) {
	return (x + 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 + 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]
\begin{array}{l}

\\
\left(x + y\right) + z
\end{array}

Alternative 1: 100.0% accurate, 1.0× speedup?

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

\\
z + \left(x + y\right)
\end{array}
Derivation
  1. Initial program 100.0%

    \[\left(x + y\right) + z \]
  2. Final simplification100.0%

    \[\leadsto z + \left(x + y\right) \]

Alternative 2: 75.1% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -1 \cdot 10^{+118}:\\ \;\;\;\;x + y\\ \mathbf{elif}\;x \leq -8 \cdot 10^{+75}:\\ \;\;\;\;y + z\\ \mathbf{elif}\;x \leq -2.4 \cdot 10^{+53}:\\ \;\;\;\;x\\ \mathbf{else}:\\ \;\;\;\;y + z\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (<= x -1e+118)
   (+ x y)
   (if (<= x -8e+75) (+ y z) (if (<= x -2.4e+53) x (+ y z)))))
double code(double x, double y, double z) {
	double tmp;
	if (x <= -1e+118) {
		tmp = x + y;
	} else if (x <= -8e+75) {
		tmp = y + z;
	} else if (x <= -2.4e+53) {
		tmp = x;
	} else {
		tmp = 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
    real(8) :: tmp
    if (x <= (-1d+118)) then
        tmp = x + y
    else if (x <= (-8d+75)) then
        tmp = y + z
    else if (x <= (-2.4d+53)) then
        tmp = x
    else
        tmp = y + z
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double tmp;
	if (x <= -1e+118) {
		tmp = x + y;
	} else if (x <= -8e+75) {
		tmp = y + z;
	} else if (x <= -2.4e+53) {
		tmp = x;
	} else {
		tmp = y + z;
	}
	return tmp;
}
def code(x, y, z):
	tmp = 0
	if x <= -1e+118:
		tmp = x + y
	elif x <= -8e+75:
		tmp = y + z
	elif x <= -2.4e+53:
		tmp = x
	else:
		tmp = y + z
	return tmp
function code(x, y, z)
	tmp = 0.0
	if (x <= -1e+118)
		tmp = Float64(x + y);
	elseif (x <= -8e+75)
		tmp = Float64(y + z);
	elseif (x <= -2.4e+53)
		tmp = x;
	else
		tmp = Float64(y + z);
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if (x <= -1e+118)
		tmp = x + y;
	elseif (x <= -8e+75)
		tmp = y + z;
	elseif (x <= -2.4e+53)
		tmp = x;
	else
		tmp = y + z;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := If[LessEqual[x, -1e+118], N[(x + y), $MachinePrecision], If[LessEqual[x, -8e+75], N[(y + z), $MachinePrecision], If[LessEqual[x, -2.4e+53], x, N[(y + z), $MachinePrecision]]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq -1 \cdot 10^{+118}:\\
\;\;\;\;x + y\\

\mathbf{elif}\;x \leq -8 \cdot 10^{+75}:\\
\;\;\;\;y + z\\

\mathbf{elif}\;x \leq -2.4 \cdot 10^{+53}:\\
\;\;\;\;x\\

\mathbf{else}:\\
\;\;\;\;y + z\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if x < -9.99999999999999967e117

    1. Initial program 100.0%

      \[\left(x + y\right) + z \]
    2. Taylor expanded in z around 0 75.7%

      \[\leadsto \color{blue}{y + x} \]

    if -9.99999999999999967e117 < x < -7.99999999999999941e75 or -2.4e53 < x

    1. Initial program 100.0%

      \[\left(x + y\right) + z \]
    2. Taylor expanded in x around 0 73.4%

      \[\leadsto \color{blue}{y + z} \]

    if -7.99999999999999941e75 < x < -2.4e53

    1. Initial program 100.0%

      \[\left(x + y\right) + z \]
    2. Taylor expanded in x around inf 51.4%

      \[\leadsto \color{blue}{x} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification73.3%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -1 \cdot 10^{+118}:\\ \;\;\;\;x + y\\ \mathbf{elif}\;x \leq -8 \cdot 10^{+75}:\\ \;\;\;\;y + z\\ \mathbf{elif}\;x \leq -2.4 \cdot 10^{+53}:\\ \;\;\;\;x\\ \mathbf{else}:\\ \;\;\;\;y + z\\ \end{array} \]

Alternative 3: 43.3% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -4 \cdot 10^{+117}:\\ \;\;\;\;x\\ \mathbf{elif}\;x \leq -3.8 \cdot 10^{+76}:\\ \;\;\;\;z\\ \mathbf{elif}\;x \leq -4.2 \cdot 10^{+52}:\\ \;\;\;\;x\\ \mathbf{else}:\\ \;\;\;\;z\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (<= x -4e+117) x (if (<= x -3.8e+76) z (if (<= x -4.2e+52) x z))))
double code(double x, double y, double z) {
	double tmp;
	if (x <= -4e+117) {
		tmp = x;
	} else if (x <= -3.8e+76) {
		tmp = z;
	} else if (x <= -4.2e+52) {
		tmp = x;
	} else {
		tmp = 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
    real(8) :: tmp
    if (x <= (-4d+117)) then
        tmp = x
    else if (x <= (-3.8d+76)) then
        tmp = z
    else if (x <= (-4.2d+52)) then
        tmp = x
    else
        tmp = z
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double tmp;
	if (x <= -4e+117) {
		tmp = x;
	} else if (x <= -3.8e+76) {
		tmp = z;
	} else if (x <= -4.2e+52) {
		tmp = x;
	} else {
		tmp = z;
	}
	return tmp;
}
def code(x, y, z):
	tmp = 0
	if x <= -4e+117:
		tmp = x
	elif x <= -3.8e+76:
		tmp = z
	elif x <= -4.2e+52:
		tmp = x
	else:
		tmp = z
	return tmp
function code(x, y, z)
	tmp = 0.0
	if (x <= -4e+117)
		tmp = x;
	elseif (x <= -3.8e+76)
		tmp = z;
	elseif (x <= -4.2e+52)
		tmp = x;
	else
		tmp = z;
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if (x <= -4e+117)
		tmp = x;
	elseif (x <= -3.8e+76)
		tmp = z;
	elseif (x <= -4.2e+52)
		tmp = x;
	else
		tmp = z;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := If[LessEqual[x, -4e+117], x, If[LessEqual[x, -3.8e+76], z, If[LessEqual[x, -4.2e+52], x, z]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq -4 \cdot 10^{+117}:\\
\;\;\;\;x\\

\mathbf{elif}\;x \leq -3.8 \cdot 10^{+76}:\\
\;\;\;\;z\\

\mathbf{elif}\;x \leq -4.2 \cdot 10^{+52}:\\
\;\;\;\;x\\

\mathbf{else}:\\
\;\;\;\;z\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < -4.0000000000000002e117 or -3.80000000000000024e76 < x < -4.2e52

    1. Initial program 100.0%

      \[\left(x + y\right) + z \]
    2. Taylor expanded in x around inf 64.2%

      \[\leadsto \color{blue}{x} \]

    if -4.0000000000000002e117 < x < -3.80000000000000024e76 or -4.2e52 < x

    1. Initial program 100.0%

      \[\left(x + y\right) + z \]
    2. Taylor expanded in z around inf 33.0%

      \[\leadsto \color{blue}{z} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification39.5%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -4 \cdot 10^{+117}:\\ \;\;\;\;x\\ \mathbf{elif}\;x \leq -3.8 \cdot 10^{+76}:\\ \;\;\;\;z\\ \mathbf{elif}\;x \leq -4.2 \cdot 10^{+52}:\\ \;\;\;\;x\\ \mathbf{else}:\\ \;\;\;\;z\\ \end{array} \]

Alternative 4: 45.6% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -2.1 \cdot 10^{+117}:\\ \;\;\;\;x + y\\ \mathbf{elif}\;x \leq -1.3 \cdot 10^{+76}:\\ \;\;\;\;z\\ \mathbf{elif}\;x \leq -1.42 \cdot 10^{+53}:\\ \;\;\;\;x\\ \mathbf{else}:\\ \;\;\;\;z\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (<= x -2.1e+117)
   (+ x y)
   (if (<= x -1.3e+76) z (if (<= x -1.42e+53) x z))))
double code(double x, double y, double z) {
	double tmp;
	if (x <= -2.1e+117) {
		tmp = x + y;
	} else if (x <= -1.3e+76) {
		tmp = z;
	} else if (x <= -1.42e+53) {
		tmp = x;
	} else {
		tmp = 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
    real(8) :: tmp
    if (x <= (-2.1d+117)) then
        tmp = x + y
    else if (x <= (-1.3d+76)) then
        tmp = z
    else if (x <= (-1.42d+53)) then
        tmp = x
    else
        tmp = z
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double tmp;
	if (x <= -2.1e+117) {
		tmp = x + y;
	} else if (x <= -1.3e+76) {
		tmp = z;
	} else if (x <= -1.42e+53) {
		tmp = x;
	} else {
		tmp = z;
	}
	return tmp;
}
def code(x, y, z):
	tmp = 0
	if x <= -2.1e+117:
		tmp = x + y
	elif x <= -1.3e+76:
		tmp = z
	elif x <= -1.42e+53:
		tmp = x
	else:
		tmp = z
	return tmp
function code(x, y, z)
	tmp = 0.0
	if (x <= -2.1e+117)
		tmp = Float64(x + y);
	elseif (x <= -1.3e+76)
		tmp = z;
	elseif (x <= -1.42e+53)
		tmp = x;
	else
		tmp = z;
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if (x <= -2.1e+117)
		tmp = x + y;
	elseif (x <= -1.3e+76)
		tmp = z;
	elseif (x <= -1.42e+53)
		tmp = x;
	else
		tmp = z;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := If[LessEqual[x, -2.1e+117], N[(x + y), $MachinePrecision], If[LessEqual[x, -1.3e+76], z, If[LessEqual[x, -1.42e+53], x, z]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq -2.1 \cdot 10^{+117}:\\
\;\;\;\;x + y\\

\mathbf{elif}\;x \leq -1.3 \cdot 10^{+76}:\\
\;\;\;\;z\\

\mathbf{elif}\;x \leq -1.42 \cdot 10^{+53}:\\
\;\;\;\;x\\

\mathbf{else}:\\
\;\;\;\;z\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if x < -2.1000000000000001e117

    1. Initial program 100.0%

      \[\left(x + y\right) + z \]
    2. Taylor expanded in z around 0 75.7%

      \[\leadsto \color{blue}{y + x} \]

    if -2.1000000000000001e117 < x < -1.3e76 or -1.41999999999999999e53 < x

    1. Initial program 100.0%

      \[\left(x + y\right) + z \]
    2. Taylor expanded in z around inf 33.0%

      \[\leadsto \color{blue}{z} \]

    if -1.3e76 < x < -1.41999999999999999e53

    1. Initial program 100.0%

      \[\left(x + y\right) + z \]
    2. Taylor expanded in x around inf 51.4%

      \[\leadsto \color{blue}{x} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification41.3%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -2.1 \cdot 10^{+117}:\\ \;\;\;\;x + y\\ \mathbf{elif}\;x \leq -1.3 \cdot 10^{+76}:\\ \;\;\;\;z\\ \mathbf{elif}\;x \leq -1.42 \cdot 10^{+53}:\\ \;\;\;\;x\\ \mathbf{else}:\\ \;\;\;\;z\\ \end{array} \]

Alternative 5: 66.7% accurate, 1.7× speedup?

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

\\
x + z
\end{array}
Derivation
  1. Initial program 100.0%

    \[\left(x + y\right) + z \]
  2. Taylor expanded in y around 0 64.7%

    \[\leadsto \color{blue}{z + x} \]
  3. Final simplification64.7%

    \[\leadsto x + z \]

Alternative 6: 34.0% accurate, 5.0× speedup?

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

\\
x
\end{array}
Derivation
  1. Initial program 100.0%

    \[\left(x + y\right) + z \]
  2. Taylor expanded in x around inf 35.7%

    \[\leadsto \color{blue}{x} \]
  3. Final simplification35.7%

    \[\leadsto x \]

Reproduce

?
herbie shell --seed 2023242 
(FPCore (x y z)
  :name "Optimisation.CirclePacking:place from circle-packing-0.1.0.4, I"
  :precision binary64
  (+ (+ x y) z))