Graphics.Rendering.Chart.Plot.Vectors:renderPlotVectors from Chart-1.5.3

Percentage Accurate: 78.0% → 86.9%
Time: 1.1s
Alternatives: 3
Speedup: 0.6×

Specification

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

\\
x + \left(1 - x\right) \cdot \left(1 - y\right)
\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 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: 78.0% accurate, 1.0× speedup?

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

\\
x + \left(1 - x\right) \cdot \left(1 - y\right)
\end{array}

Alternative 1: 86.9% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \left(1 - x\right) \cdot \left(1 - y\right)\\ \mathbf{if}\;y \leq -9.8 \cdot 10^{-107}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;y \leq 4.8 \cdot 10^{-46}:\\ \;\;\;\;1 - y\\ \mathbf{else}:\\ \;\;\;\;x + t\_0\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (let* ((t_0 (* (- 1.0 x) (- 1.0 y))))
   (if (<= y -9.8e-107) t_0 (if (<= y 4.8e-46) (- 1.0 y) (+ x t_0)))))
double code(double x, double y) {
	double t_0 = (1.0 - x) * (1.0 - y);
	double tmp;
	if (y <= -9.8e-107) {
		tmp = t_0;
	} else if (y <= 4.8e-46) {
		tmp = 1.0 - y;
	} else {
		tmp = x + t_0;
	}
	return tmp;
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: t_0
    real(8) :: tmp
    t_0 = (1.0d0 - x) * (1.0d0 - y)
    if (y <= (-9.8d-107)) then
        tmp = t_0
    else if (y <= 4.8d-46) then
        tmp = 1.0d0 - y
    else
        tmp = x + t_0
    end if
    code = tmp
end function
public static double code(double x, double y) {
	double t_0 = (1.0 - x) * (1.0 - y);
	double tmp;
	if (y <= -9.8e-107) {
		tmp = t_0;
	} else if (y <= 4.8e-46) {
		tmp = 1.0 - y;
	} else {
		tmp = x + t_0;
	}
	return tmp;
}
def code(x, y):
	t_0 = (1.0 - x) * (1.0 - y)
	tmp = 0
	if y <= -9.8e-107:
		tmp = t_0
	elif y <= 4.8e-46:
		tmp = 1.0 - y
	else:
		tmp = x + t_0
	return tmp
function code(x, y)
	t_0 = Float64(Float64(1.0 - x) * Float64(1.0 - y))
	tmp = 0.0
	if (y <= -9.8e-107)
		tmp = t_0;
	elseif (y <= 4.8e-46)
		tmp = Float64(1.0 - y);
	else
		tmp = Float64(x + t_0);
	end
	return tmp
end
function tmp_2 = code(x, y)
	t_0 = (1.0 - x) * (1.0 - y);
	tmp = 0.0;
	if (y <= -9.8e-107)
		tmp = t_0;
	elseif (y <= 4.8e-46)
		tmp = 1.0 - y;
	else
		tmp = x + t_0;
	end
	tmp_2 = tmp;
end
code[x_, y_] := Block[{t$95$0 = N[(N[(1.0 - x), $MachinePrecision] * N[(1.0 - y), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[y, -9.8e-107], t$95$0, If[LessEqual[y, 4.8e-46], N[(1.0 - y), $MachinePrecision], N[(x + t$95$0), $MachinePrecision]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \left(1 - x\right) \cdot \left(1 - y\right)\\
\mathbf{if}\;y \leq -9.8 \cdot 10^{-107}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;y \leq 4.8 \cdot 10^{-46}:\\
\;\;\;\;1 - y\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if y < -9.79999999999999959e-107

    1. Initial program 90.9%

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

      \[\leadsto \color{blue}{\left(1 + x \cdot \left(1 + -1 \cdot \left(1 - y\right)\right)\right) - y} \]
    4. Applied rewrites91.3%

      \[\leadsto \color{blue}{\left(1 - x\right) \cdot \left(1 - y\right)} \]

    if -9.79999999999999959e-107 < y < 4.80000000000000027e-46

    1. Initial program 53.0%

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

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

      \[\leadsto \color{blue}{\left(1 - x\right) \cdot \left(1 - y\right)} \]
    5. Taylor expanded in x around inf

      \[\leadsto x \cdot \color{blue}{\left(\left(-1 \cdot \left(1 - y\right) + \frac{1}{x}\right) - \frac{y}{x}\right)} \]
    6. Applied rewrites78.9%

      \[\leadsto 1 - \color{blue}{y} \]

    if 4.80000000000000027e-46 < y

    1. Initial program 98.3%

      \[x + \left(1 - x\right) \cdot \left(1 - y\right) \]
    2. Add Preprocessing
  3. Recombined 3 regimes into one program.
  4. Add Preprocessing

Alternative 2: 86.3% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \left(1 - x\right) \cdot \left(1 - y\right)\\ \mathbf{if}\;y \leq -9.8 \cdot 10^{-107}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;y \leq 1:\\ \;\;\;\;1 - y\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (let* ((t_0 (* (- 1.0 x) (- 1.0 y))))
   (if (<= y -9.8e-107) t_0 (if (<= y 1.0) (- 1.0 y) t_0))))
double code(double x, double y) {
	double t_0 = (1.0 - x) * (1.0 - y);
	double tmp;
	if (y <= -9.8e-107) {
		tmp = t_0;
	} else if (y <= 1.0) {
		tmp = 1.0 - y;
	} else {
		tmp = t_0;
	}
	return tmp;
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: t_0
    real(8) :: tmp
    t_0 = (1.0d0 - x) * (1.0d0 - y)
    if (y <= (-9.8d-107)) then
        tmp = t_0
    else if (y <= 1.0d0) then
        tmp = 1.0d0 - y
    else
        tmp = t_0
    end if
    code = tmp
end function
public static double code(double x, double y) {
	double t_0 = (1.0 - x) * (1.0 - y);
	double tmp;
	if (y <= -9.8e-107) {
		tmp = t_0;
	} else if (y <= 1.0) {
		tmp = 1.0 - y;
	} else {
		tmp = t_0;
	}
	return tmp;
}
def code(x, y):
	t_0 = (1.0 - x) * (1.0 - y)
	tmp = 0
	if y <= -9.8e-107:
		tmp = t_0
	elif y <= 1.0:
		tmp = 1.0 - y
	else:
		tmp = t_0
	return tmp
function code(x, y)
	t_0 = Float64(Float64(1.0 - x) * Float64(1.0 - y))
	tmp = 0.0
	if (y <= -9.8e-107)
		tmp = t_0;
	elseif (y <= 1.0)
		tmp = Float64(1.0 - y);
	else
		tmp = t_0;
	end
	return tmp
end
function tmp_2 = code(x, y)
	t_0 = (1.0 - x) * (1.0 - y);
	tmp = 0.0;
	if (y <= -9.8e-107)
		tmp = t_0;
	elseif (y <= 1.0)
		tmp = 1.0 - y;
	else
		tmp = t_0;
	end
	tmp_2 = tmp;
end
code[x_, y_] := Block[{t$95$0 = N[(N[(1.0 - x), $MachinePrecision] * N[(1.0 - y), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[y, -9.8e-107], t$95$0, If[LessEqual[y, 1.0], N[(1.0 - y), $MachinePrecision], t$95$0]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \left(1 - x\right) \cdot \left(1 - y\right)\\
\mathbf{if}\;y \leq -9.8 \cdot 10^{-107}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;y \leq 1:\\
\;\;\;\;1 - y\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < -9.79999999999999959e-107 or 1 < y

    1. Initial program 95.5%

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

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

      \[\leadsto \color{blue}{\left(1 - x\right) \cdot \left(1 - y\right)} \]

    if -9.79999999999999959e-107 < y < 1

    1. Initial program 55.7%

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

      \[\leadsto \color{blue}{\left(1 + x \cdot \left(1 + -1 \cdot \left(1 - y\right)\right)\right) - y} \]
    4. Applied rewrites52.5%

      \[\leadsto \color{blue}{\left(1 - x\right) \cdot \left(1 - y\right)} \]
    5. Taylor expanded in x around inf

      \[\leadsto x \cdot \color{blue}{\left(\left(-1 \cdot \left(1 - y\right) + \frac{1}{x}\right) - \frac{y}{x}\right)} \]
    6. Applied rewrites77.9%

      \[\leadsto 1 - \color{blue}{y} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 3: 62.2% accurate, 3.8× speedup?

\[\begin{array}{l} \\ 1 - y \end{array} \]
(FPCore (x y) :precision binary64 (- 1.0 y))
double code(double x, double y) {
	return 1.0 - y;
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = 1.0d0 - y
end function
public static double code(double x, double y) {
	return 1.0 - y;
}
def code(x, y):
	return 1.0 - y
function code(x, y)
	return Float64(1.0 - y)
end
function tmp = code(x, y)
	tmp = 1.0 - y;
end
code[x_, y_] := N[(1.0 - y), $MachinePrecision]
\begin{array}{l}

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

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

    \[\leadsto \color{blue}{\left(1 + x \cdot \left(1 + -1 \cdot \left(1 - y\right)\right)\right) - y} \]
  4. Applied rewrites77.8%

    \[\leadsto \color{blue}{\left(1 - x\right) \cdot \left(1 - y\right)} \]
  5. Taylor expanded in x around inf

    \[\leadsto x \cdot \color{blue}{\left(\left(-1 \cdot \left(1 - y\right) + \frac{1}{x}\right) - \frac{y}{x}\right)} \]
  6. Applied rewrites61.7%

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

Developer Target 1: 100.0% accurate, 1.3× speedup?

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

\\
y \cdot x - \left(y - 1\right)
\end{array}

Reproduce

?
herbie shell --seed 2024321 
(FPCore (x y)
  :name "Graphics.Rendering.Chart.Plot.Vectors:renderPlotVectors from Chart-1.5.3"
  :precision binary64
  :pre (TRUE)

  :alt
  (! :herbie-platform default (- (* y x) (- y 1)))

  (+ x (* (- 1.0 x) (- 1.0 y))))