Diagrams.TwoD.Apollonian:descartes from diagrams-contrib-1.3.0.5

?

Percentage Accurate: 70.2% → 94.7%
Time: 16.3s
Precision: binary64
Cost: 20364

?

\[ \begin{array}{c}[x, y, z] = \mathsf{sort}([x, y, z])\\ \end{array} \]
\[2 \cdot \sqrt{\left(x \cdot y + x \cdot z\right) + y \cdot z} \]
\[\begin{array}{l} t_0 := 2 \cdot e^{\left(\log \left(\left(-y\right) - z\right) - \log \left(\frac{-1}{x}\right)\right) \cdot 0.5}\\ \mathbf{if}\;y \leq -5.6 \cdot 10^{+27}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;y \leq -6 \cdot 10^{-221}:\\ \;\;\;\;2 \cdot \sqrt{x \cdot \left(y + z\right)}\\ \mathbf{elif}\;y \leq 6.2 \cdot 10^{-271}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;2 \cdot \left(\sqrt{z} \cdot \sqrt{y}\right)\\ \end{array} \]
(FPCore (x y z)
 :precision binary64
 (* 2.0 (sqrt (+ (+ (* x y) (* x z)) (* y z)))))
(FPCore (x y z)
 :precision binary64
 (let* ((t_0 (* 2.0 (exp (* (- (log (- (- y) z)) (log (/ -1.0 x))) 0.5)))))
   (if (<= y -5.6e+27)
     t_0
     (if (<= y -6e-221)
       (* 2.0 (sqrt (* x (+ y z))))
       (if (<= y 6.2e-271) t_0 (* 2.0 (* (sqrt z) (sqrt y))))))))
double code(double x, double y, double z) {
	return 2.0 * sqrt((((x * y) + (x * z)) + (y * z)));
}
double code(double x, double y, double z) {
	double t_0 = 2.0 * exp(((log((-y - z)) - log((-1.0 / x))) * 0.5));
	double tmp;
	if (y <= -5.6e+27) {
		tmp = t_0;
	} else if (y <= -6e-221) {
		tmp = 2.0 * sqrt((x * (y + z)));
	} else if (y <= 6.2e-271) {
		tmp = t_0;
	} else {
		tmp = 2.0 * (sqrt(z) * sqrt(y));
	}
	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 = 2.0d0 * sqrt((((x * y) + (x * z)) + (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 = 2.0d0 * exp(((log((-y - z)) - log(((-1.0d0) / x))) * 0.5d0))
    if (y <= (-5.6d+27)) then
        tmp = t_0
    else if (y <= (-6d-221)) then
        tmp = 2.0d0 * sqrt((x * (y + z)))
    else if (y <= 6.2d-271) then
        tmp = t_0
    else
        tmp = 2.0d0 * (sqrt(z) * sqrt(y))
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	return 2.0 * Math.sqrt((((x * y) + (x * z)) + (y * z)));
}
public static double code(double x, double y, double z) {
	double t_0 = 2.0 * Math.exp(((Math.log((-y - z)) - Math.log((-1.0 / x))) * 0.5));
	double tmp;
	if (y <= -5.6e+27) {
		tmp = t_0;
	} else if (y <= -6e-221) {
		tmp = 2.0 * Math.sqrt((x * (y + z)));
	} else if (y <= 6.2e-271) {
		tmp = t_0;
	} else {
		tmp = 2.0 * (Math.sqrt(z) * Math.sqrt(y));
	}
	return tmp;
}
def code(x, y, z):
	return 2.0 * math.sqrt((((x * y) + (x * z)) + (y * z)))
def code(x, y, z):
	t_0 = 2.0 * math.exp(((math.log((-y - z)) - math.log((-1.0 / x))) * 0.5))
	tmp = 0
	if y <= -5.6e+27:
		tmp = t_0
	elif y <= -6e-221:
		tmp = 2.0 * math.sqrt((x * (y + z)))
	elif y <= 6.2e-271:
		tmp = t_0
	else:
		tmp = 2.0 * (math.sqrt(z) * math.sqrt(y))
	return tmp
function code(x, y, z)
	return Float64(2.0 * sqrt(Float64(Float64(Float64(x * y) + Float64(x * z)) + Float64(y * z))))
end
function code(x, y, z)
	t_0 = Float64(2.0 * exp(Float64(Float64(log(Float64(Float64(-y) - z)) - log(Float64(-1.0 / x))) * 0.5)))
	tmp = 0.0
	if (y <= -5.6e+27)
		tmp = t_0;
	elseif (y <= -6e-221)
		tmp = Float64(2.0 * sqrt(Float64(x * Float64(y + z))));
	elseif (y <= 6.2e-271)
		tmp = t_0;
	else
		tmp = Float64(2.0 * Float64(sqrt(z) * sqrt(y)));
	end
	return tmp
end
function tmp = code(x, y, z)
	tmp = 2.0 * sqrt((((x * y) + (x * z)) + (y * z)));
end
function tmp_2 = code(x, y, z)
	t_0 = 2.0 * exp(((log((-y - z)) - log((-1.0 / x))) * 0.5));
	tmp = 0.0;
	if (y <= -5.6e+27)
		tmp = t_0;
	elseif (y <= -6e-221)
		tmp = 2.0 * sqrt((x * (y + z)));
	elseif (y <= 6.2e-271)
		tmp = t_0;
	else
		tmp = 2.0 * (sqrt(z) * sqrt(y));
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := N[(2.0 * N[Sqrt[N[(N[(N[(x * y), $MachinePrecision] + N[(x * z), $MachinePrecision]), $MachinePrecision] + N[(y * z), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
code[x_, y_, z_] := Block[{t$95$0 = N[(2.0 * N[Exp[N[(N[(N[Log[N[((-y) - z), $MachinePrecision]], $MachinePrecision] - N[Log[N[(-1.0 / x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[y, -5.6e+27], t$95$0, If[LessEqual[y, -6e-221], N[(2.0 * N[Sqrt[N[(x * N[(y + z), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[y, 6.2e-271], t$95$0, N[(2.0 * N[(N[Sqrt[z], $MachinePrecision] * N[Sqrt[y], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]]
2 \cdot \sqrt{\left(x \cdot y + x \cdot z\right) + y \cdot z}
\begin{array}{l}
t_0 := 2 \cdot e^{\left(\log \left(\left(-y\right) - z\right) - \log \left(\frac{-1}{x}\right)\right) \cdot 0.5}\\
\mathbf{if}\;y \leq -5.6 \cdot 10^{+27}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;y \leq -6 \cdot 10^{-221}:\\
\;\;\;\;2 \cdot \sqrt{x \cdot \left(y + z\right)}\\

\mathbf{elif}\;y \leq 6.2 \cdot 10^{-271}:\\
\;\;\;\;t_0\\

\mathbf{else}:\\
\;\;\;\;2 \cdot \left(\sqrt{z} \cdot \sqrt{y}\right)\\


\end{array}

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.

Herbie found 12 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

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.

Bogosity?

Bogosity

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original70.2%
Target82.7%
Herbie94.7%
\[\begin{array}{l} \mathbf{if}\;z < 7.636950090573675 \cdot 10^{+176}:\\ \;\;\;\;2 \cdot \sqrt{\left(x + y\right) \cdot z + x \cdot y}\\ \mathbf{else}:\\ \;\;\;\;\left(\left(0.25 \cdot \left(\left({y}^{-0.75} \cdot \left({z}^{-0.75} \cdot x\right)\right) \cdot \left(y + z\right)\right) + {z}^{0.25} \cdot {y}^{0.25}\right) \cdot \left(0.25 \cdot \left(\left({y}^{-0.75} \cdot \left({z}^{-0.75} \cdot x\right)\right) \cdot \left(y + z\right)\right) + {z}^{0.25} \cdot {y}^{0.25}\right)\right) \cdot 2\\ \end{array} \]

Derivation?

  1. Split input into 3 regimes
  2. if y < -5.5999999999999999e27 or -6.0000000000000003e-221 < y < 6.1999999999999998e-271

    1. Initial program 65.2%

      \[2 \cdot \sqrt{\left(x \cdot y + x \cdot z\right) + y \cdot z} \]
    2. Simplified65.3%

      \[\leadsto \color{blue}{2 \cdot \sqrt{x \cdot \left(y + z\right) + y \cdot z}} \]
      Step-by-step derivation

      [Start]65.2%

      \[ 2 \cdot \sqrt{\left(x \cdot y + x \cdot z\right) + y \cdot z} \]

      distribute-lft-out [=>]65.3%

      \[ 2 \cdot \sqrt{\color{blue}{x \cdot \left(y + z\right)} + y \cdot z} \]
    3. Taylor expanded in x around inf 48.5%

      \[\leadsto 2 \cdot \sqrt{\color{blue}{\left(y + z\right) \cdot x}} \]
    4. Applied egg-rr45.0%

      \[\leadsto 2 \cdot \color{blue}{e^{\log \left(x \cdot \left(y + z\right)\right) \cdot 0.5}} \]
      Step-by-step derivation

      [Start]48.5%

      \[ 2 \cdot \sqrt{\left(y + z\right) \cdot x} \]

      pow1/2 [=>]48.6%

      \[ 2 \cdot \color{blue}{{\left(\left(y + z\right) \cdot x\right)}^{0.5}} \]

      pow-to-exp [=>]45.0%

      \[ 2 \cdot \color{blue}{e^{\log \left(\left(y + z\right) \cdot x\right) \cdot 0.5}} \]

      *-commutative [=>]45.0%

      \[ 2 \cdot e^{\log \color{blue}{\left(x \cdot \left(y + z\right)\right)} \cdot 0.5} \]
    5. Taylor expanded in x around -inf 44.2%

      \[\leadsto 2 \cdot e^{\color{blue}{\left(-1 \cdot \log \left(\frac{-1}{x}\right) + \log \left(-1 \cdot \left(y + z\right)\right)\right)} \cdot 0.5} \]
    6. Simplified44.2%

      \[\leadsto 2 \cdot e^{\color{blue}{\left(\log \left(\left(-y\right) - z\right) - \log \left(\frac{-1}{x}\right)\right)} \cdot 0.5} \]
      Step-by-step derivation

      [Start]44.2%

      \[ 2 \cdot e^{\left(-1 \cdot \log \left(\frac{-1}{x}\right) + \log \left(-1 \cdot \left(y + z\right)\right)\right) \cdot 0.5} \]

      +-commutative [=>]44.2%

      \[ 2 \cdot e^{\color{blue}{\left(\log \left(-1 \cdot \left(y + z\right)\right) + -1 \cdot \log \left(\frac{-1}{x}\right)\right)} \cdot 0.5} \]

      mul-1-neg [=>]44.2%

      \[ 2 \cdot e^{\left(\log \left(-1 \cdot \left(y + z\right)\right) + \color{blue}{\left(-\log \left(\frac{-1}{x}\right)\right)}\right) \cdot 0.5} \]

      unsub-neg [=>]44.2%

      \[ 2 \cdot e^{\color{blue}{\left(\log \left(-1 \cdot \left(y + z\right)\right) - \log \left(\frac{-1}{x}\right)\right)} \cdot 0.5} \]

      distribute-lft-in [=>]44.2%

      \[ 2 \cdot e^{\left(\log \color{blue}{\left(-1 \cdot y + -1 \cdot z\right)} - \log \left(\frac{-1}{x}\right)\right) \cdot 0.5} \]

      neg-mul-1 [<=]44.2%

      \[ 2 \cdot e^{\left(\log \left(-1 \cdot y + \color{blue}{\left(-z\right)}\right) - \log \left(\frac{-1}{x}\right)\right) \cdot 0.5} \]

      unsub-neg [=>]44.2%

      \[ 2 \cdot e^{\left(\log \color{blue}{\left(-1 \cdot y - z\right)} - \log \left(\frac{-1}{x}\right)\right) \cdot 0.5} \]

      mul-1-neg [=>]44.2%

      \[ 2 \cdot e^{\left(\log \left(\color{blue}{\left(-y\right)} - z\right) - \log \left(\frac{-1}{x}\right)\right) \cdot 0.5} \]

    if -5.5999999999999999e27 < y < -6.0000000000000003e-221

    1. Initial program 83.8%

      \[2 \cdot \sqrt{\left(x \cdot y + x \cdot z\right) + y \cdot z} \]
    2. Simplified83.8%

      \[\leadsto \color{blue}{2 \cdot \sqrt{x \cdot \left(y + z\right) + y \cdot z}} \]
      Step-by-step derivation

      [Start]83.8%

      \[ 2 \cdot \sqrt{\left(x \cdot y + x \cdot z\right) + y \cdot z} \]

      distribute-lft-out [=>]83.8%

      \[ 2 \cdot \sqrt{\color{blue}{x \cdot \left(y + z\right)} + y \cdot z} \]
    3. Taylor expanded in x around inf 47.2%

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

    if 6.1999999999999998e-271 < y

    1. Initial program 70.3%

      \[2 \cdot \sqrt{\left(x \cdot y + x \cdot z\right) + y \cdot z} \]
    2. Simplified70.3%

      \[\leadsto \color{blue}{2 \cdot \sqrt{x \cdot \left(y + z\right) + y \cdot z}} \]
      Step-by-step derivation

      [Start]70.3%

      \[ 2 \cdot \sqrt{\left(x \cdot y + x \cdot z\right) + y \cdot z} \]

      distribute-lft-out [=>]70.3%

      \[ 2 \cdot \sqrt{\color{blue}{x \cdot \left(y + z\right)} + y \cdot z} \]
    3. Taylor expanded in x around 0 28.0%

      \[\leadsto 2 \cdot \color{blue}{\sqrt{y \cdot z}} \]
    4. Applied egg-rr35.7%

      \[\leadsto 2 \cdot \color{blue}{\left(\sqrt{z} \cdot \sqrt{y}\right)} \]
      Step-by-step derivation

      [Start]28.0%

      \[ 2 \cdot \sqrt{y \cdot z} \]

      *-commutative [=>]28.0%

      \[ 2 \cdot \sqrt{\color{blue}{z \cdot y}} \]

      sqrt-prod [=>]35.7%

      \[ 2 \cdot \color{blue}{\left(\sqrt{z} \cdot \sqrt{y}\right)} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification40.5%

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -5.6 \cdot 10^{+27}:\\ \;\;\;\;2 \cdot e^{\left(\log \left(\left(-y\right) - z\right) - \log \left(\frac{-1}{x}\right)\right) \cdot 0.5}\\ \mathbf{elif}\;y \leq -6 \cdot 10^{-221}:\\ \;\;\;\;2 \cdot \sqrt{x \cdot \left(y + z\right)}\\ \mathbf{elif}\;y \leq 6.2 \cdot 10^{-271}:\\ \;\;\;\;2 \cdot e^{\left(\log \left(\left(-y\right) - z\right) - \log \left(\frac{-1}{x}\right)\right) \cdot 0.5}\\ \mathbf{else}:\\ \;\;\;\;2 \cdot \left(\sqrt{z} \cdot \sqrt{y}\right)\\ \end{array} \]

Alternatives

Alternative 1
Accuracy94.7%
Cost20364
\[\begin{array}{l} t_0 := 2 \cdot e^{\left(\log \left(\left(-y\right) - z\right) - \log \left(\frac{-1}{x}\right)\right) \cdot 0.5}\\ \mathbf{if}\;y \leq -5.6 \cdot 10^{+27}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;y \leq -6 \cdot 10^{-221}:\\ \;\;\;\;2 \cdot \sqrt{x \cdot \left(y + z\right)}\\ \mathbf{elif}\;y \leq 6.2 \cdot 10^{-271}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;2 \cdot \left(\sqrt{z} \cdot \sqrt{y}\right)\\ \end{array} \]
Alternative 2
Accuracy94.2%
Cost19972
\[\begin{array}{l} \mathbf{if}\;y \leq -8.4 \cdot 10^{+27}:\\ \;\;\;\;2 \cdot e^{0.5 \cdot \left(\log \left(-x\right) - \log \left(\frac{-1}{y}\right)\right)}\\ \mathbf{elif}\;y \leq 4.8 \cdot 10^{-256}:\\ \;\;\;\;2 \cdot \sqrt{\mathsf{fma}\left(x, y, z \cdot \left(y + x\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;2 \cdot \left(\sqrt{z} \cdot \sqrt{y}\right)\\ \end{array} \]
Alternative 3
Accuracy89.3%
Cost13892
\[\begin{array}{l} \mathbf{if}\;y \leq -3 \cdot 10^{+25}:\\ \;\;\;\;2 \cdot \left(\sqrt{y \cdot y - z \cdot z} \cdot \sqrt{\frac{x}{y - z}}\right)\\ \mathbf{elif}\;y \leq 4.8 \cdot 10^{-256}:\\ \;\;\;\;2 \cdot \sqrt{\mathsf{fma}\left(x, y, z \cdot \left(y + x\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;2 \cdot \left(\sqrt{z} \cdot \sqrt{y}\right)\\ \end{array} \]
Alternative 4
Accuracy89.2%
Cost13892
\[\begin{array}{l} \mathbf{if}\;y \leq -4.1 \cdot 10^{+27}:\\ \;\;\;\;2 \cdot \frac{\sqrt{y \cdot y - z \cdot z}}{\sqrt{\frac{y - z}{x}}}\\ \mathbf{elif}\;y \leq 4.8 \cdot 10^{-256}:\\ \;\;\;\;2 \cdot \sqrt{\mathsf{fma}\left(x, y, z \cdot \left(y + x\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;2 \cdot \left(\sqrt{z} \cdot \sqrt{y}\right)\\ \end{array} \]
Alternative 5
Accuracy83.1%
Cost13508
\[\begin{array}{l} \mathbf{if}\;y \leq 4.8 \cdot 10^{-256}:\\ \;\;\;\;2 \cdot \sqrt{\mathsf{fma}\left(z, y + x, y \cdot x\right)}\\ \mathbf{else}:\\ \;\;\;\;2 \cdot \left(\sqrt{z} \cdot \sqrt{y}\right)\\ \end{array} \]
Alternative 6
Accuracy83.1%
Cost13252
\[\begin{array}{l} \mathbf{if}\;y \leq 4.8 \cdot 10^{-256}:\\ \;\;\;\;2 \cdot \sqrt{x \cdot \left(y + z\right) + y \cdot z}\\ \mathbf{else}:\\ \;\;\;\;2 \cdot \left(\sqrt{z} \cdot \sqrt{y}\right)\\ \end{array} \]
Alternative 7
Accuracy70.3%
Cost7104
\[2 \cdot \sqrt{x \cdot \left(y + z\right) + y \cdot z} \]
Alternative 8
Accuracy69.0%
Cost6980
\[\begin{array}{l} \mathbf{if}\;y \leq -1.06 \cdot 10^{-262}:\\ \;\;\;\;2 \cdot \sqrt{y \cdot x}\\ \mathbf{else}:\\ \;\;\;\;2 \cdot \sqrt{z \cdot \left(y + x\right)}\\ \end{array} \]
Alternative 9
Accuracy70.2%
Cost6980
\[\begin{array}{l} \mathbf{if}\;y \leq -1.8 \cdot 10^{-276}:\\ \;\;\;\;2 \cdot \sqrt{x \cdot \left(y + z\right)}\\ \mathbf{else}:\\ \;\;\;\;2 \cdot \sqrt{z \cdot \left(y + x\right)}\\ \end{array} \]
Alternative 10
Accuracy68.0%
Cost6916
\[\begin{array}{l} \mathbf{if}\;y \leq -1.65 \cdot 10^{-276}:\\ \;\;\;\;2 \cdot \sqrt{y \cdot x}\\ \mathbf{else}:\\ \;\;\;\;2 \cdot {\left(y \cdot z\right)}^{0.5}\\ \end{array} \]
Alternative 11
Accuracy68.0%
Cost6852
\[\begin{array}{l} \mathbf{if}\;y \leq -1.65 \cdot 10^{-276}:\\ \;\;\;\;2 \cdot \sqrt{y \cdot x}\\ \mathbf{else}:\\ \;\;\;\;2 \cdot \sqrt{y \cdot z}\\ \end{array} \]
Alternative 12
Accuracy35.2%
Cost6720
\[2 \cdot \sqrt{y \cdot x} \]

Reproduce?

herbie shell --seed 2023271 
(FPCore (x y z)
  :name "Diagrams.TwoD.Apollonian:descartes from diagrams-contrib-1.3.0.5"
  :precision binary64

  :herbie-target
  (if (< z 7.636950090573675e+176) (* 2.0 (sqrt (+ (* (+ x y) z) (* x y)))) (* (* (+ (* 0.25 (* (* (pow y -0.75) (* (pow z -0.75) x)) (+ y z))) (* (pow z 0.25) (pow y 0.25))) (+ (* 0.25 (* (* (pow y -0.75) (* (pow z -0.75) x)) (+ y z))) (* (pow z 0.25) (pow y 0.25)))) 2.0))

  (* 2.0 (sqrt (+ (+ (* x y) (* x z)) (* y z)))))