Data.Array.Repa.Algorithms.ColorRamp:rampColorHotToCold from repa-algorithms-3.4.0.1, B

Percentage Accurate: 99.8% → 100.0%
Time: 1.3s
Alternatives: 5
Speedup: 1.0×

Specification

?
\[\mathsf{TRUE}\left(\right)\]
\[\begin{array}{l} \\ \frac{4 \cdot \left(\left(x - y\right) - z \cdot 0.5\right)}{z} \end{array} \]
(FPCore (x y z) :precision binary64 (/ (* 4.0 (- (- x y) (* z 0.5))) z))
double code(double x, double y, double z) {
	return (4.0 * ((x - y) - (z * 0.5))) / z;
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = (4.0d0 * ((x - y) - (z * 0.5d0))) / z
end function
public static double code(double x, double y, double z) {
	return (4.0 * ((x - y) - (z * 0.5))) / z;
}
def code(x, y, z):
	return (4.0 * ((x - y) - (z * 0.5))) / z
function code(x, y, z)
	return Float64(Float64(4.0 * Float64(Float64(x - y) - Float64(z * 0.5))) / z)
end
function tmp = code(x, y, z)
	tmp = (4.0 * ((x - y) - (z * 0.5))) / z;
end
code[x_, y_, z_] := N[(N[(4.0 * N[(N[(x - y), $MachinePrecision] - N[(z * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / z), $MachinePrecision]
\begin{array}{l}

\\
\frac{4 \cdot \left(\left(x - y\right) - z \cdot 0.5\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 5 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: 99.8% accurate, 1.0× speedup?

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

\\
\frac{4 \cdot \left(\left(x - y\right) - z \cdot 0.5\right)}{z}
\end{array}

Alternative 1: 100.0% accurate, 1.0× speedup?

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

\\
4 \cdot \frac{\left(x - y\right) - z \cdot 0.5}{z}
\end{array}
Derivation
  1. Initial program 99.6%

    \[\frac{4 \cdot \left(\left(x - y\right) - z \cdot 0.5\right)}{z} \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0

    \[\leadsto \color{blue}{-4 \cdot \frac{y + \frac{1}{2} \cdot z}{z} + 4 \cdot \frac{x}{z}} \]
  4. Applied rewrites3.1%

    \[\leadsto \color{blue}{4 \cdot \left(\left(x - y\right) - z \cdot 0.5\right)} \]
  5. Taylor expanded in x around 0

    \[\leadsto 4 \cdot \left(-1 \cdot \color{blue}{\left(y + \frac{1}{2} \cdot z\right)}\right) \]
  6. Applied rewrites62.9%

    \[\leadsto 4 \cdot \frac{x - y}{\color{blue}{z}} \]
  7. Taylor expanded in x around inf

    \[\leadsto 4 \cdot \frac{x \cdot \left(1 + -1 \cdot \frac{y}{x}\right)}{z} \]
  8. Applied rewrites100.0%

    \[\leadsto 4 \cdot \frac{\left(x - y\right) - z \cdot 0.5}{z} \]
  9. Add Preprocessing

Alternative 2: 70.7% accurate, 0.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := 4 \cdot \frac{x - y}{z}\\ t_1 := \left(x - y\right) - z \cdot 0.5\\ t_2 := \frac{4 \cdot t\_1}{z}\\ \mathbf{if}\;t\_2 \leq -10:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;t\_2 \leq -1:\\ \;\;\;\;\frac{t\_1}{z}\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (let* ((t_0 (* 4.0 (/ (- x y) z)))
        (t_1 (- (- x y) (* z 0.5)))
        (t_2 (/ (* 4.0 t_1) z)))
   (if (<= t_2 -10.0) t_0 (if (<= t_2 -1.0) (/ t_1 z) t_0))))
double code(double x, double y, double z) {
	double t_0 = 4.0 * ((x - y) / z);
	double t_1 = (x - y) - (z * 0.5);
	double t_2 = (4.0 * t_1) / z;
	double tmp;
	if (t_2 <= -10.0) {
		tmp = t_0;
	} else if (t_2 <= -1.0) {
		tmp = t_1 / z;
	} else {
		tmp = t_0;
	}
	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) :: t_0
    real(8) :: t_1
    real(8) :: t_2
    real(8) :: tmp
    t_0 = 4.0d0 * ((x - y) / z)
    t_1 = (x - y) - (z * 0.5d0)
    t_2 = (4.0d0 * t_1) / z
    if (t_2 <= (-10.0d0)) then
        tmp = t_0
    else if (t_2 <= (-1.0d0)) then
        tmp = t_1 / z
    else
        tmp = t_0
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double t_0 = 4.0 * ((x - y) / z);
	double t_1 = (x - y) - (z * 0.5);
	double t_2 = (4.0 * t_1) / z;
	double tmp;
	if (t_2 <= -10.0) {
		tmp = t_0;
	} else if (t_2 <= -1.0) {
		tmp = t_1 / z;
	} else {
		tmp = t_0;
	}
	return tmp;
}
def code(x, y, z):
	t_0 = 4.0 * ((x - y) / z)
	t_1 = (x - y) - (z * 0.5)
	t_2 = (4.0 * t_1) / z
	tmp = 0
	if t_2 <= -10.0:
		tmp = t_0
	elif t_2 <= -1.0:
		tmp = t_1 / z
	else:
		tmp = t_0
	return tmp
function code(x, y, z)
	t_0 = Float64(4.0 * Float64(Float64(x - y) / z))
	t_1 = Float64(Float64(x - y) - Float64(z * 0.5))
	t_2 = Float64(Float64(4.0 * t_1) / z)
	tmp = 0.0
	if (t_2 <= -10.0)
		tmp = t_0;
	elseif (t_2 <= -1.0)
		tmp = Float64(t_1 / z);
	else
		tmp = t_0;
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	t_0 = 4.0 * ((x - y) / z);
	t_1 = (x - y) - (z * 0.5);
	t_2 = (4.0 * t_1) / z;
	tmp = 0.0;
	if (t_2 <= -10.0)
		tmp = t_0;
	elseif (t_2 <= -1.0)
		tmp = t_1 / z;
	else
		tmp = t_0;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := Block[{t$95$0 = N[(4.0 * N[(N[(x - y), $MachinePrecision] / z), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(x - y), $MachinePrecision] - N[(z * 0.5), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(N[(4.0 * t$95$1), $MachinePrecision] / z), $MachinePrecision]}, If[LessEqual[t$95$2, -10.0], t$95$0, If[LessEqual[t$95$2, -1.0], N[(t$95$1 / z), $MachinePrecision], t$95$0]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := 4 \cdot \frac{x - y}{z}\\
t_1 := \left(x - y\right) - z \cdot 0.5\\
t_2 := \frac{4 \cdot t\_1}{z}\\
\mathbf{if}\;t\_2 \leq -10:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;t\_2 \leq -1:\\
\;\;\;\;\frac{t\_1}{z}\\

\mathbf{else}:\\
\;\;\;\;t\_0\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (/.f64 (*.f64 #s(literal 4 binary64) (-.f64 (-.f64 x y) (*.f64 z #s(literal 1/2 binary64)))) z) < -10 or -1 < (/.f64 (*.f64 #s(literal 4 binary64) (-.f64 (-.f64 x y) (*.f64 z #s(literal 1/2 binary64)))) z)

    1. Initial program 99.4%

      \[\frac{4 \cdot \left(\left(x - y\right) - z \cdot 0.5\right)}{z} \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0

      \[\leadsto \color{blue}{-4 \cdot \frac{y + \frac{1}{2} \cdot z}{z} + 4 \cdot \frac{x}{z}} \]
    4. Applied rewrites3.1%

      \[\leadsto \color{blue}{4 \cdot \left(\left(x - y\right) - z \cdot 0.5\right)} \]
    5. Taylor expanded in x around 0

      \[\leadsto 4 \cdot \left(-1 \cdot \color{blue}{\left(y + \frac{1}{2} \cdot z\right)}\right) \]
    6. Applied rewrites96.7%

      \[\leadsto 4 \cdot \frac{x - y}{\color{blue}{z}} \]

    if -10 < (/.f64 (*.f64 #s(literal 4 binary64) (-.f64 (-.f64 x y) (*.f64 z #s(literal 1/2 binary64)))) z) < -1

    1. Initial program 100.0%

      \[\frac{4 \cdot \left(\left(x - y\right) - z \cdot 0.5\right)}{z} \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0

      \[\leadsto \frac{\color{blue}{-4 \cdot \left(y + \frac{1}{2} \cdot z\right)}}{z} \]
    4. Applied rewrites17.2%

      \[\leadsto \frac{\color{blue}{\left(x - y\right) - z \cdot 0.5}}{z} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 3: 66.2% accurate, 1.4× speedup?

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

\\
4 \cdot \frac{x - y}{z}
\end{array}
Derivation
  1. Initial program 99.6%

    \[\frac{4 \cdot \left(\left(x - y\right) - z \cdot 0.5\right)}{z} \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0

    \[\leadsto \color{blue}{-4 \cdot \frac{y + \frac{1}{2} \cdot z}{z} + 4 \cdot \frac{x}{z}} \]
  4. Applied rewrites3.1%

    \[\leadsto \color{blue}{4 \cdot \left(\left(x - y\right) - z \cdot 0.5\right)} \]
  5. Taylor expanded in x around 0

    \[\leadsto 4 \cdot \left(-1 \cdot \color{blue}{\left(y + \frac{1}{2} \cdot z\right)}\right) \]
  6. Applied rewrites62.9%

    \[\leadsto 4 \cdot \frac{x - y}{\color{blue}{z}} \]
  7. Add Preprocessing

Alternative 4: 29.7% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \frac{x - y}{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}

\\
\frac{x - y}{z}
\end{array}
Derivation
  1. Initial program 99.6%

    \[\frac{4 \cdot \left(\left(x - y\right) - z \cdot 0.5\right)}{z} \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0

    \[\leadsto \frac{\color{blue}{-4 \cdot \left(y + \frac{1}{2} \cdot z\right) + 4 \cdot x}}{z} \]
  4. Applied rewrites25.1%

    \[\leadsto \frac{\color{blue}{x - y}}{z} \]
  5. Add Preprocessing

Alternative 5: 3.6% accurate, 7.0× speedup?

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

\\
x - y
\end{array}
Derivation
  1. Initial program 99.6%

    \[\frac{4 \cdot \left(\left(x - y\right) - z \cdot 0.5\right)}{z} \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0

    \[\leadsto \color{blue}{-4 \cdot \frac{y + \frac{1}{2} \cdot z}{z} + 4 \cdot \frac{x}{z}} \]
  4. Applied rewrites3.1%

    \[\leadsto \color{blue}{4 \cdot \left(\left(x - y\right) - z \cdot 0.5\right)} \]
  5. Taylor expanded in x around 0

    \[\leadsto -4 \cdot \left(y + \frac{1}{2} \cdot z\right) + \color{blue}{4 \cdot x} \]
  6. Applied rewrites3.4%

    \[\leadsto x - \color{blue}{y} \]
  7. Add Preprocessing

Developer Target 1: 97.9% accurate, 0.7× speedup?

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

\\
4 \cdot \frac{x}{z} - \left(2 + 4 \cdot \frac{y}{z}\right)
\end{array}

Reproduce

?
herbie shell --seed 2024321 
(FPCore (x y z)
  :name "Data.Array.Repa.Algorithms.ColorRamp:rampColorHotToCold from repa-algorithms-3.4.0.1, B"
  :precision binary64
  :pre (TRUE)

  :alt
  (! :herbie-platform default (- (* 4 (/ x z)) (+ 2 (* 4 (/ y z)))))

  (/ (* 4.0 (- (- x y) (* z 0.5))) z))