Data.Array.Repa.Algorithms.Pixel:doubleRmsOfRGB8 from repa-algorithms-3.4.0.1

?

Percentage Accurate: 44.2% → 99.4%
Time: 8.5s
Precision: binary64
Cost: 19520

?

\[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}} \]
\[\frac{\mathsf{hypot}\left(x, \mathsf{hypot}\left(z, y\right)\right)}{\sqrt{3}} \]
(FPCore (x y z)
 :precision binary64
 (sqrt (/ (+ (+ (* x x) (* y y)) (* z z)) 3.0)))
(FPCore (x y z) :precision binary64 (/ (hypot x (hypot z y)) (sqrt 3.0)))
double code(double x, double y, double z) {
	return sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0));
}
double code(double x, double y, double z) {
	return hypot(x, hypot(z, y)) / sqrt(3.0);
}
public static double code(double x, double y, double z) {
	return Math.sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0));
}
public static double code(double x, double y, double z) {
	return Math.hypot(x, Math.hypot(z, y)) / Math.sqrt(3.0);
}
def code(x, y, z):
	return math.sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0))
def code(x, y, z):
	return math.hypot(x, math.hypot(z, y)) / math.sqrt(3.0)
function code(x, y, z)
	return sqrt(Float64(Float64(Float64(Float64(x * x) + Float64(y * y)) + Float64(z * z)) / 3.0))
end
function code(x, y, z)
	return Float64(hypot(x, hypot(z, y)) / sqrt(3.0))
end
function tmp = code(x, y, z)
	tmp = sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0));
end
function tmp = code(x, y, z)
	tmp = hypot(x, hypot(z, y)) / sqrt(3.0);
end
code[x_, y_, z_] := N[Sqrt[N[(N[(N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision] / 3.0), $MachinePrecision]], $MachinePrecision]
code[x_, y_, z_] := N[(N[Sqrt[x ^ 2 + N[Sqrt[z ^ 2 + y ^ 2], $MachinePrecision] ^ 2], $MachinePrecision] / N[Sqrt[3.0], $MachinePrecision]), $MachinePrecision]
\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}
\frac{\mathsf{hypot}\left(x, \mathsf{hypot}\left(z, y\right)\right)}{\sqrt{3}}

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 7 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

Original44.2%
Target61.3%
Herbie99.4%
\[\begin{array}{l} \mathbf{if}\;z < -6.396479394109776 \cdot 10^{+136}:\\ \;\;\;\;\frac{-z}{\sqrt{3}}\\ \mathbf{elif}\;z < 7.320293694404182 \cdot 10^{+117}:\\ \;\;\;\;\frac{\sqrt{\left(z \cdot z + x \cdot x\right) + y \cdot y}}{\sqrt{3}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.3333333333333333} \cdot z\\ \end{array} \]

Derivation?

  1. Initial program 49.2%

    \[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}} \]
  2. Applied egg-rr98.6%

    \[\leadsto \color{blue}{\mathsf{hypot}\left(x, \mathsf{hypot}\left(y, z\right)\right) \cdot \frac{1}{\sqrt{3}}} \]
    Step-by-step derivation

    [Start]49.2%

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

    sqrt-div [=>]49.1%

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

    div-inv [=>]48.8%

    \[ \color{blue}{\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z} \cdot \frac{1}{\sqrt{3}}} \]

    associate-+l+ [=>]48.8%

    \[ \sqrt{\color{blue}{x \cdot x + \left(y \cdot y + z \cdot z\right)}} \cdot \frac{1}{\sqrt{3}} \]

    add-sqr-sqrt [=>]48.8%

    \[ \sqrt{x \cdot x + \color{blue}{\sqrt{y \cdot y + z \cdot z} \cdot \sqrt{y \cdot y + z \cdot z}}} \cdot \frac{1}{\sqrt{3}} \]

    hypot-def [=>]62.8%

    \[ \color{blue}{\mathsf{hypot}\left(x, \sqrt{y \cdot y + z \cdot z}\right)} \cdot \frac{1}{\sqrt{3}} \]

    hypot-def [=>]98.6%

    \[ \mathsf{hypot}\left(x, \color{blue}{\mathsf{hypot}\left(y, z\right)}\right) \cdot \frac{1}{\sqrt{3}} \]
  3. Simplified99.4%

    \[\leadsto \color{blue}{\frac{\mathsf{hypot}\left(x, \mathsf{hypot}\left(z, y\right)\right)}{\sqrt{3}}} \]
    Step-by-step derivation

    [Start]98.6%

    \[ \mathsf{hypot}\left(x, \mathsf{hypot}\left(y, z\right)\right) \cdot \frac{1}{\sqrt{3}} \]

    associate-*r/ [=>]99.4%

    \[ \color{blue}{\frac{\mathsf{hypot}\left(x, \mathsf{hypot}\left(y, z\right)\right) \cdot 1}{\sqrt{3}}} \]

    *-rgt-identity [=>]99.4%

    \[ \frac{\color{blue}{\mathsf{hypot}\left(x, \mathsf{hypot}\left(y, z\right)\right)}}{\sqrt{3}} \]

    hypot-def [<=]63.2%

    \[ \frac{\mathsf{hypot}\left(x, \color{blue}{\sqrt{y \cdot y + z \cdot z}}\right)}{\sqrt{3}} \]

    +-commutative [<=]63.2%

    \[ \frac{\mathsf{hypot}\left(x, \sqrt{\color{blue}{z \cdot z + y \cdot y}}\right)}{\sqrt{3}} \]

    hypot-def [=>]99.4%

    \[ \frac{\mathsf{hypot}\left(x, \color{blue}{\mathsf{hypot}\left(z, y\right)}\right)}{\sqrt{3}} \]
  4. Final simplification99.4%

    \[\leadsto \frac{\mathsf{hypot}\left(x, \mathsf{hypot}\left(z, y\right)\right)}{\sqrt{3}} \]

Alternatives

Alternative 1
Accuracy99.4%
Cost19520
\[\frac{\mathsf{hypot}\left(x, \mathsf{hypot}\left(z, y\right)\right)}{\sqrt{3}} \]
Alternative 2
Accuracy37.7%
Cost13188
\[\begin{array}{l} \mathbf{if}\;x \leq -2.45 \cdot 10^{+148}:\\ \;\;\;\;\sqrt{0.3333333333333333} \cdot \mathsf{hypot}\left(y, x\right)\\ \mathbf{elif}\;x \leq -1.9 \cdot 10^{-111}:\\ \;\;\;\;\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\\ \mathbf{else}:\\ \;\;\;\;\frac{z}{\sqrt{3}}\\ \end{array} \]
Alternative 3
Accuracy67.4%
Cost13056
\[\sqrt{0.3333333333333333} \cdot \mathsf{hypot}\left(z, x\right) \]
Alternative 4
Accuracy36.1%
Cost7496
\[\begin{array}{l} \mathbf{if}\;x \leq -7.8 \cdot 10^{+149}:\\ \;\;\;\;x \cdot \left(-\sqrt{0.3333333333333333}\right)\\ \mathbf{elif}\;x \leq -4.7 \cdot 10^{-110}:\\ \;\;\;\;\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\\ \mathbf{else}:\\ \;\;\;\;\frac{z}{\sqrt{3}}\\ \end{array} \]
Alternative 5
Accuracy30.7%
Cost6788
\[\begin{array}{l} \mathbf{if}\;x \leq -750000000000:\\ \;\;\;\;x \cdot \left(-\sqrt{0.3333333333333333}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{z}{\sqrt{3}}\\ \end{array} \]
Alternative 6
Accuracy18.4%
Cost6592
\[z \cdot \sqrt{0.3333333333333333} \]
Alternative 7
Accuracy18.4%
Cost6592
\[\frac{z}{\sqrt{3}} \]

Reproduce?

herbie shell --seed 2023272 
(FPCore (x y z)
  :name "Data.Array.Repa.Algorithms.Pixel:doubleRmsOfRGB8 from repa-algorithms-3.4.0.1"
  :precision binary64

  :herbie-target
  (if (< z -6.396479394109776e+136) (/ (- z) (sqrt 3.0)) (if (< z 7.320293694404182e+117) (/ (sqrt (+ (+ (* z z) (* x x)) (* y y))) (sqrt 3.0)) (* (sqrt 0.3333333333333333) z)))

  (sqrt (/ (+ (+ (* x x) (* y y)) (* z z)) 3.0)))