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

Percentage Accurate: 44.9% → 97.9%
Time: 2.5s
Alternatives: 3
Speedup: 2.3×

Specification

?
\[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}} \]
(FPCore (x y z)
  :precision binary64
  :pre TRUE
  (sqrt (/ (+ (+ (* x x) (* y y)) (* z z)) 3.0)))
double code(double x, double y, double z) {
	return sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0));
}
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 = sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0d0))
end function
public static double code(double x, double y, double z) {
	return Math.sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0));
}
def code(x, y, z):
	return math.sqrt(((((x * x) + (y * y)) + (z * z)) / 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 tmp = code(x, y, z)
	tmp = sqrt(((((x * x) + (y * y)) + (z * z)) / 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]
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 =
	sqrt(((((x * x) + (y * y)) + (z * z)) / (3)))
END code
\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{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.

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

\[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}} \]
(FPCore (x y z)
  :precision binary64
  :pre TRUE
  (sqrt (/ (+ (+ (* x x) (* y y)) (* z z)) 3.0)))
double code(double x, double y, double z) {
	return sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0));
}
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 = sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0d0))
end function
public static double code(double x, double y, double z) {
	return Math.sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0));
}
def code(x, y, z):
	return math.sqrt(((((x * x) + (y * y)) + (z * z)) / 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 tmp = code(x, y, z)
	tmp = sqrt(((((x * x) + (y * y)) + (z * z)) / 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]
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 =
	sqrt(((((x * x) + (y * y)) + (z * z)) / (3)))
END code
\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}

Alternative 1: 97.9% accurate, 1.6× speedup?

\[\mathsf{max}\left(\left|y\right|, \mathsf{max}\left(\left|x\right|, \left|z\right|\right)\right) \cdot 0.5773502691896257 \]
(FPCore (x y z)
  :precision binary64
  :pre TRUE
  (* (fmax (fabs y) (fmax (fabs x) (fabs z))) 0.5773502691896257))
double code(double x, double y, double z) {
	return fmax(fabs(y), fmax(fabs(x), fabs(z))) * 0.5773502691896257;
}
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 = fmax(abs(y), fmax(abs(x), abs(z))) * 0.5773502691896257d0
end function
public static double code(double x, double y, double z) {
	return fmax(Math.abs(y), fmax(Math.abs(x), Math.abs(z))) * 0.5773502691896257;
}
def code(x, y, z):
	return fmax(math.fabs(y), fmax(math.fabs(x), math.fabs(z))) * 0.5773502691896257
function code(x, y, z)
	return Float64(fmax(abs(y), fmax(abs(x), abs(z))) * 0.5773502691896257)
end
function tmp = code(x, y, z)
	tmp = max(abs(y), max(abs(x), abs(z))) * 0.5773502691896257;
end
code[x_, y_, z_] := N[(N[Max[N[Abs[y], $MachinePrecision], N[Max[N[Abs[x], $MachinePrecision], N[Abs[z], $MachinePrecision]], $MachinePrecision]], $MachinePrecision] * 0.5773502691896257), $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 =
	LET tmp_1 = IF ((abs(x)) > (abs(z))) THEN (abs(x)) ELSE (abs(z)) ENDIF IN
	LET tmp_2 = IF ((abs(x)) > (abs(z))) THEN (abs(x)) ELSE (abs(z)) ENDIF IN
	LET tmp = IF ((abs(y)) > tmp_1) THEN (abs(y)) ELSE tmp_2 ENDIF IN
	tmp * (577350269189625731058868041145615279674530029296875e-51)
END code
\mathsf{max}\left(\left|y\right|, \mathsf{max}\left(\left|x\right|, \left|z\right|\right)\right) \cdot 0.5773502691896257
Derivation
  1. Initial program 44.9%

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

    \[\leadsto z \cdot \sqrt{\frac{1}{3}} \]
  3. Step-by-step derivation
    1. Applied rewrites18.1%

      \[\leadsto z \cdot \sqrt{0.3333333333333333} \]
    2. Evaluated real constant18.1%

      \[\leadsto z \cdot 0.5773502691896257 \]
    3. Add Preprocessing

    Alternative 2: 67.7% accurate, 2.3× speedup?

    \[\mathsf{max}\left(\left|x\right|, \left|y\right|\right) \cdot 0.5773502691896257 \]
    (FPCore (x y z)
      :precision binary64
      :pre TRUE
      (* (fmax (fabs x) (fabs y)) 0.5773502691896257))
    double code(double x, double y, double z) {
    	return fmax(fabs(x), fabs(y)) * 0.5773502691896257;
    }
    
    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 = fmax(abs(x), abs(y)) * 0.5773502691896257d0
    end function
    
    public static double code(double x, double y, double z) {
    	return fmax(Math.abs(x), Math.abs(y)) * 0.5773502691896257;
    }
    
    def code(x, y, z):
    	return fmax(math.fabs(x), math.fabs(y)) * 0.5773502691896257
    
    function code(x, y, z)
    	return Float64(fmax(abs(x), abs(y)) * 0.5773502691896257)
    end
    
    function tmp = code(x, y, z)
    	tmp = max(abs(x), abs(y)) * 0.5773502691896257;
    end
    
    code[x_, y_, z_] := N[(N[Max[N[Abs[x], $MachinePrecision], N[Abs[y], $MachinePrecision]], $MachinePrecision] * 0.5773502691896257), $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 =
    	LET tmp = IF ((abs(x)) > (abs(y))) THEN (abs(x)) ELSE (abs(y)) ENDIF IN
    	tmp * (577350269189625731058868041145615279674530029296875e-51)
    END code
    \mathsf{max}\left(\left|x\right|, \left|y\right|\right) \cdot 0.5773502691896257
    
    Derivation
    1. Initial program 44.9%

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

      \[\leadsto y \cdot \sqrt{\frac{1}{3}} \]
    3. Step-by-step derivation
      1. Applied rewrites18.9%

        \[\leadsto y \cdot \sqrt{0.3333333333333333} \]
      2. Evaluated real constant18.9%

        \[\leadsto y \cdot 0.5773502691896257 \]
      3. Add Preprocessing

      Alternative 3: 36.2% accurate, 4.2× speedup?

      \[\left|x\right| \cdot 0.5773502691896257 \]
      (FPCore (x y z)
        :precision binary64
        :pre TRUE
        (* (fabs x) 0.5773502691896257))
      double code(double x, double y, double z) {
      	return fabs(x) * 0.5773502691896257;
      }
      
      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 = abs(x) * 0.5773502691896257d0
      end function
      
      public static double code(double x, double y, double z) {
      	return Math.abs(x) * 0.5773502691896257;
      }
      
      def code(x, y, z):
      	return math.fabs(x) * 0.5773502691896257
      
      function code(x, y, z)
      	return Float64(abs(x) * 0.5773502691896257)
      end
      
      function tmp = code(x, y, z)
      	tmp = abs(x) * 0.5773502691896257;
      end
      
      code[x_, y_, z_] := N[(N[Abs[x], $MachinePrecision] * 0.5773502691896257), $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 =
      	(abs(x)) * (577350269189625731058868041145615279674530029296875e-51)
      END code
      \left|x\right| \cdot 0.5773502691896257
      
      Derivation
      1. Initial program 44.9%

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

        \[\leadsto x \cdot \sqrt{\frac{1}{3}} \]
      3. Step-by-step derivation
        1. Applied rewrites18.6%

          \[\leadsto x \cdot \sqrt{0.3333333333333333} \]
        2. Evaluated real constant18.6%

          \[\leadsto x \cdot 0.5773502691896257 \]
        3. Add Preprocessing

        Reproduce

        ?
        herbie shell --seed 2026092 
        (FPCore (x y z)
          :name "Data.Array.Repa.Algorithms.Pixel:doubleRmsOfRGB8 from repa-algorithms-3.4.0.1"
          :precision binary64
          (sqrt (/ (+ (+ (* x x) (* y y)) (* z z)) 3.0)))