Diagrams.Color.HSV:lerp from diagrams-contrib-1.3.0.5

Percentage Accurate: 98.0% → 100.0%
Time: 1.9s
Alternatives: 4
Speedup: 1.4×

Specification

?
\[\left(1 - x\right) \cdot y + x \cdot z \]
(FPCore (x y z)
  :precision binary64
  :pre TRUE
  (+ (* (- 1.0 x) y) (* x z)))
double code(double x, double y, double z) {
	return ((1.0 - x) * y) + (x * z);
}
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 = ((1.0d0 - x) * y) + (x * z)
end function
public static double code(double x, double y, double z) {
	return ((1.0 - x) * y) + (x * z);
}
def code(x, y, z):
	return ((1.0 - x) * y) + (x * z)
function code(x, y, z)
	return Float64(Float64(Float64(1.0 - x) * y) + Float64(x * z))
end
function tmp = code(x, y, z)
	tmp = ((1.0 - x) * y) + (x * z);
end
code[x_, y_, z_] := N[(N[(N[(1.0 - x), $MachinePrecision] * y), $MachinePrecision] + N[(x * z), $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 =
	(((1) - x) * y) + (x * z)
END code
\left(1 - x\right) \cdot y + x \cdot z

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

\[\left(1 - x\right) \cdot y + x \cdot z \]
(FPCore (x y z)
  :precision binary64
  :pre TRUE
  (+ (* (- 1.0 x) y) (* x z)))
double code(double x, double y, double z) {
	return ((1.0 - x) * y) + (x * z);
}
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 = ((1.0d0 - x) * y) + (x * z)
end function
public static double code(double x, double y, double z) {
	return ((1.0 - x) * y) + (x * z);
}
def code(x, y, z):
	return ((1.0 - x) * y) + (x * z)
function code(x, y, z)
	return Float64(Float64(Float64(1.0 - x) * y) + Float64(x * z))
end
function tmp = code(x, y, z)
	tmp = ((1.0 - x) * y) + (x * z);
end
code[x_, y_, z_] := N[(N[(N[(1.0 - x), $MachinePrecision] * y), $MachinePrecision] + N[(x * z), $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 =
	(((1) - x) * y) + (x * z)
END code
\left(1 - x\right) \cdot y + x \cdot z

Alternative 1: 100.0% accurate, 1.4× speedup?

\[\mathsf{fma}\left(x, z - y, y\right) \]
(FPCore (x y z)
  :precision binary64
  :pre TRUE
  (fma x (- z y) y))
double code(double x, double y, double z) {
	return fma(x, (z - y), y);
}
function code(x, y, z)
	return fma(x, Float64(z - y), y)
end
code[x_, y_, z_] := N[(x * N[(z - y), $MachinePrecision] + y), $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 =
	(x * (z - y)) + y
END code
\mathsf{fma}\left(x, z - y, y\right)
Derivation
  1. Initial program 98.0%

    \[\left(1 - x\right) \cdot y + x \cdot z \]
  2. Step-by-step derivation
    1. Applied rewrites100.0%

      \[\leadsto \mathsf{fma}\left(-x, y - z, y\right) \]
    2. Step-by-step derivation
      1. Applied rewrites100.0%

        \[\leadsto \mathsf{fma}\left(x, z - y, y\right) \]
      2. Add Preprocessing

      Alternative 2: 98.7% accurate, 0.9× speedup?

      \[\begin{array}{l} t_0 := x \cdot \left(z - y\right)\\ \mathbf{if}\;x \leq -771.6157302664199:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;x \leq 857.8504450292068:\\ \;\;\;\;\mathsf{fma}\left(x, z, y\right)\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \]
      (FPCore (x y z)
        :precision binary64
        :pre TRUE
        (let* ((t_0 (* x (- z y))))
        (if (<= x -771.6157302664199)
          t_0
          (if (<= x 857.8504450292068) (fma x z y) t_0))))
      double code(double x, double y, double z) {
      	double t_0 = x * (z - y);
      	double tmp;
      	if (x <= -771.6157302664199) {
      		tmp = t_0;
      	} else if (x <= 857.8504450292068) {
      		tmp = fma(x, z, y);
      	} else {
      		tmp = t_0;
      	}
      	return tmp;
      }
      
      function code(x, y, z)
      	t_0 = Float64(x * Float64(z - y))
      	tmp = 0.0
      	if (x <= -771.6157302664199)
      		tmp = t_0;
      	elseif (x <= 857.8504450292068)
      		tmp = fma(x, z, y);
      	else
      		tmp = t_0;
      	end
      	return tmp
      end
      
      code[x_, y_, z_] := Block[{t$95$0 = N[(x * N[(z - y), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[x, -771.6157302664199], t$95$0, If[LessEqual[x, 857.8504450292068], N[(x * z + y), $MachinePrecision], t$95$0]]]
      
      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 t_0 = (x * (z - y)) IN
      		LET tmp_1 = IF (x <= (8578504450292067531336215324699878692626953125e-43)) THEN ((x * z) + y) ELSE t_0 ENDIF IN
      		LET tmp = IF (x <= (-771615730266419859617599286139011383056640625e-42)) THEN t_0 ELSE tmp_1 ENDIF IN
      	tmp
      END code
      \begin{array}{l}
      t_0 := x \cdot \left(z - y\right)\\
      \mathbf{if}\;x \leq -771.6157302664199:\\
      \;\;\;\;t\_0\\
      
      \mathbf{elif}\;x \leq 857.8504450292068:\\
      \;\;\;\;\mathsf{fma}\left(x, z, y\right)\\
      
      \mathbf{else}:\\
      \;\;\;\;t\_0\\
      
      
      \end{array}
      
      Derivation
      1. Split input into 2 regimes
      2. if x < -771.61573026641986 or 857.85044502920675 < x

        1. Initial program 98.0%

          \[\left(1 - x\right) \cdot y + x \cdot z \]
        2. Step-by-step derivation
          1. Applied rewrites100.0%

            \[\leadsto \mathsf{fma}\left(-x, y - z, y\right) \]
          2. Step-by-step derivation
            1. Applied rewrites100.0%

              \[\leadsto \mathsf{fma}\left(x, z - y, y\right) \]
            2. Taylor expanded in x around inf

              \[\leadsto x \cdot \left(z - y\right) \]
            3. Step-by-step derivation
              1. Applied rewrites65.2%

                \[\leadsto x \cdot \left(z - y\right) \]

              if -771.61573026641986 < x < 857.85044502920675

              1. Initial program 98.0%

                \[\left(1 - x\right) \cdot y + x \cdot z \]
              2. Taylor expanded in x around 0

                \[\leadsto y + x \cdot z \]
              3. Step-by-step derivation
                1. Applied rewrites76.2%

                  \[\leadsto y + x \cdot z \]
                2. Step-by-step derivation
                  1. Applied rewrites76.2%

                    \[\leadsto \mathsf{fma}\left(x, z, y\right) \]
                3. Recombined 2 regimes into one program.
                4. Add Preprocessing

                Alternative 3: 76.2% accurate, 2.0× speedup?

                \[\mathsf{fma}\left(x, z, y\right) \]
                (FPCore (x y z)
                  :precision binary64
                  :pre TRUE
                  (fma x z y))
                double code(double x, double y, double z) {
                	return fma(x, z, y);
                }
                
                function code(x, y, z)
                	return fma(x, z, y)
                end
                
                code[x_, y_, z_] := N[(x * z + y), $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 =
                	(x * z) + y
                END code
                \mathsf{fma}\left(x, z, y\right)
                
                Derivation
                1. Initial program 98.0%

                  \[\left(1 - x\right) \cdot y + x \cdot z \]
                2. Taylor expanded in x around 0

                  \[\leadsto y + x \cdot z \]
                3. Step-by-step derivation
                  1. Applied rewrites76.2%

                    \[\leadsto y + x \cdot z \]
                  2. Step-by-step derivation
                    1. Applied rewrites76.2%

                      \[\leadsto \mathsf{fma}\left(x, z, y\right) \]
                    2. Add Preprocessing

                    Alternative 4: 42.2% accurate, 3.0× speedup?

                    \[x \cdot z \]
                    (FPCore (x y z)
                      :precision binary64
                      :pre TRUE
                      (* x z))
                    double code(double x, double y, double z) {
                    	return x * z;
                    }
                    
                    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 = 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]
                    
                    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 =
                    	x * z
                    END code
                    x \cdot z
                    
                    Derivation
                    1. Initial program 98.0%

                      \[\left(1 - x\right) \cdot y + x \cdot z \]
                    2. Step-by-step derivation
                      1. Applied rewrites100.0%

                        \[\leadsto \mathsf{fma}\left(-x, y - z, y\right) \]
                      2. Step-by-step derivation
                        1. Applied rewrites100.0%

                          \[\leadsto \mathsf{fma}\left(x, z - y, y\right) \]
                        2. Taylor expanded in y around 0

                          \[\leadsto x \cdot z \]
                        3. Step-by-step derivation
                          1. Applied rewrites42.2%

                            \[\leadsto x \cdot z \]
                          2. Add Preprocessing

                          Reproduce

                          ?
                          herbie shell --seed 2026092 
                          (FPCore (x y z)
                            :name "Diagrams.Color.HSV:lerp  from diagrams-contrib-1.3.0.5"
                            :precision binary64
                            (+ (* (- 1.0 x) y) (* x z)))