Graphics.Rendering.Plot.Render.Plot.Legend:renderLegendInside from plot-0.2.3.4

Percentage Accurate: 99.9% → 99.9%
Time: 6.7s
Alternatives: 6
Speedup: 1.0×

Specification

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

\\
\left(\left(\left(\left(x + y\right) + y\right) + x\right) + z\right) + x
\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 6 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.9% accurate, 1.0× speedup?

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

\\
\left(\left(\left(\left(x + y\right) + y\right) + x\right) + z\right) + x
\end{array}

Alternative 1: 99.9% accurate, 1.0× speedup?

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

\\
x + \left(\left(x + \left(y + \left(x + y\right)\right)\right) + z\right)
\end{array}
Derivation
  1. Initial program 99.9%

    \[\left(\left(\left(\left(x + y\right) + y\right) + x\right) + z\right) + x \]
  2. Add Preprocessing
  3. Final simplification99.9%

    \[\leadsto x + \left(\left(x + \left(y + \left(x + y\right)\right)\right) + z\right) \]
  4. Add Preprocessing

Alternative 2: 85.6% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \mathsf{fma}\left(2, x + y, x\right)\\ \mathbf{if}\;x \leq -8.5 \cdot 10^{+14}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;x \leq 1.06 \cdot 10^{-26}:\\ \;\;\;\;\mathsf{fma}\left(2, y, z\right)\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (let* ((t_0 (fma 2.0 (+ x y) x)))
   (if (<= x -8.5e+14) t_0 (if (<= x 1.06e-26) (fma 2.0 y z) t_0))))
double code(double x, double y, double z) {
	double t_0 = fma(2.0, (x + y), x);
	double tmp;
	if (x <= -8.5e+14) {
		tmp = t_0;
	} else if (x <= 1.06e-26) {
		tmp = fma(2.0, y, z);
	} else {
		tmp = t_0;
	}
	return tmp;
}
function code(x, y, z)
	t_0 = fma(2.0, Float64(x + y), x)
	tmp = 0.0
	if (x <= -8.5e+14)
		tmp = t_0;
	elseif (x <= 1.06e-26)
		tmp = fma(2.0, y, z);
	else
		tmp = t_0;
	end
	return tmp
end
code[x_, y_, z_] := Block[{t$95$0 = N[(2.0 * N[(x + y), $MachinePrecision] + x), $MachinePrecision]}, If[LessEqual[x, -8.5e+14], t$95$0, If[LessEqual[x, 1.06e-26], N[(2.0 * y + z), $MachinePrecision], t$95$0]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(2, x + y, x\right)\\
\mathbf{if}\;x \leq -8.5 \cdot 10^{+14}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;x \leq 1.06 \cdot 10^{-26}:\\
\;\;\;\;\mathsf{fma}\left(2, y, z\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < -8.5e14 or 1.06000000000000001e-26 < x

    1. Initial program 99.8%

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

      \[\leadsto \color{blue}{x + \left(2 \cdot x + 2 \cdot y\right)} \]
    4. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \color{blue}{\left(2 \cdot x + 2 \cdot y\right) + x} \]
      2. distribute-lft-outN/A

        \[\leadsto \color{blue}{2 \cdot \left(x + y\right)} + x \]
      3. lower-fma.f64N/A

        \[\leadsto \color{blue}{\mathsf{fma}\left(2, x + y, x\right)} \]
      4. +-commutativeN/A

        \[\leadsto \mathsf{fma}\left(2, \color{blue}{y + x}, x\right) \]
      5. lower-+.f6488.0

        \[\leadsto \mathsf{fma}\left(2, \color{blue}{y + x}, x\right) \]
    5. Applied rewrites88.0%

      \[\leadsto \color{blue}{\mathsf{fma}\left(2, y + x, x\right)} \]

    if -8.5e14 < x < 1.06000000000000001e-26

    1. Initial program 100.0%

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

      \[\leadsto \color{blue}{z + 2 \cdot y} \]
    4. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \color{blue}{2 \cdot y + z} \]
      2. lower-fma.f6495.2

        \[\leadsto \color{blue}{\mathsf{fma}\left(2, y, z\right)} \]
    5. Applied rewrites95.2%

      \[\leadsto \color{blue}{\mathsf{fma}\left(2, y, z\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification91.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -8.5 \cdot 10^{+14}:\\ \;\;\;\;\mathsf{fma}\left(2, x + y, x\right)\\ \mathbf{elif}\;x \leq 1.06 \cdot 10^{-26}:\\ \;\;\;\;\mathsf{fma}\left(2, y, z\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(2, x + y, x\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 84.9% accurate, 0.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y \leq -1.06 \cdot 10^{+114}:\\ \;\;\;\;\mathsf{fma}\left(2, y, z\right)\\ \mathbf{elif}\;y \leq 42000000000000:\\ \;\;\;\;\mathsf{fma}\left(x, 3, z\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(2, y, z\right)\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (<= y -1.06e+114)
   (fma 2.0 y z)
   (if (<= y 42000000000000.0) (fma x 3.0 z) (fma 2.0 y z))))
double code(double x, double y, double z) {
	double tmp;
	if (y <= -1.06e+114) {
		tmp = fma(2.0, y, z);
	} else if (y <= 42000000000000.0) {
		tmp = fma(x, 3.0, z);
	} else {
		tmp = fma(2.0, y, z);
	}
	return tmp;
}
function code(x, y, z)
	tmp = 0.0
	if (y <= -1.06e+114)
		tmp = fma(2.0, y, z);
	elseif (y <= 42000000000000.0)
		tmp = fma(x, 3.0, z);
	else
		tmp = fma(2.0, y, z);
	end
	return tmp
end
code[x_, y_, z_] := If[LessEqual[y, -1.06e+114], N[(2.0 * y + z), $MachinePrecision], If[LessEqual[y, 42000000000000.0], N[(x * 3.0 + z), $MachinePrecision], N[(2.0 * y + z), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;y \leq -1.06 \cdot 10^{+114}:\\
\;\;\;\;\mathsf{fma}\left(2, y, z\right)\\

\mathbf{elif}\;y \leq 42000000000000:\\
\;\;\;\;\mathsf{fma}\left(x, 3, z\right)\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(2, y, z\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < -1.05999999999999993e114 or 4.2e13 < y

    1. Initial program 99.9%

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

      \[\leadsto \color{blue}{z + 2 \cdot y} \]
    4. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \color{blue}{2 \cdot y + z} \]
      2. lower-fma.f6484.6

        \[\leadsto \color{blue}{\mathsf{fma}\left(2, y, z\right)} \]
    5. Applied rewrites84.6%

      \[\leadsto \color{blue}{\mathsf{fma}\left(2, y, z\right)} \]

    if -1.05999999999999993e114 < y < 4.2e13

    1. Initial program 99.9%

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

      \[\leadsto \color{blue}{x + \left(z + 2 \cdot x\right)} \]
    4. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto x + \color{blue}{\left(2 \cdot x + z\right)} \]
      2. associate-+r+N/A

        \[\leadsto \color{blue}{\left(x + 2 \cdot x\right) + z} \]
      3. distribute-rgt1-inN/A

        \[\leadsto \color{blue}{\left(2 + 1\right) \cdot x} + z \]
      4. metadata-evalN/A

        \[\leadsto \color{blue}{3} \cdot x + z \]
      5. *-commutativeN/A

        \[\leadsto \color{blue}{x \cdot 3} + z \]
      6. lower-fma.f6488.3

        \[\leadsto \color{blue}{\mathsf{fma}\left(x, 3, z\right)} \]
    5. Applied rewrites88.3%

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

Alternative 4: 78.0% accurate, 0.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -1.1 \cdot 10^{+20}:\\ \;\;\;\;x \cdot 3\\ \mathbf{elif}\;x \leq 1.45 \cdot 10^{+179}:\\ \;\;\;\;\mathsf{fma}\left(2, y, z\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot 3\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (<= x -1.1e+20) (* x 3.0) (if (<= x 1.45e+179) (fma 2.0 y z) (* x 3.0))))
double code(double x, double y, double z) {
	double tmp;
	if (x <= -1.1e+20) {
		tmp = x * 3.0;
	} else if (x <= 1.45e+179) {
		tmp = fma(2.0, y, z);
	} else {
		tmp = x * 3.0;
	}
	return tmp;
}
function code(x, y, z)
	tmp = 0.0
	if (x <= -1.1e+20)
		tmp = Float64(x * 3.0);
	elseif (x <= 1.45e+179)
		tmp = fma(2.0, y, z);
	else
		tmp = Float64(x * 3.0);
	end
	return tmp
end
code[x_, y_, z_] := If[LessEqual[x, -1.1e+20], N[(x * 3.0), $MachinePrecision], If[LessEqual[x, 1.45e+179], N[(2.0 * y + z), $MachinePrecision], N[(x * 3.0), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.1 \cdot 10^{+20}:\\
\;\;\;\;x \cdot 3\\

\mathbf{elif}\;x \leq 1.45 \cdot 10^{+179}:\\
\;\;\;\;\mathsf{fma}\left(2, y, z\right)\\

\mathbf{else}:\\
\;\;\;\;x \cdot 3\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < -1.1e20 or 1.45000000000000009e179 < x

    1. Initial program 99.8%

      \[\left(\left(\left(\left(x + y\right) + y\right) + x\right) + z\right) + x \]
    2. Add Preprocessing
    3. Taylor expanded in x around inf

      \[\leadsto \color{blue}{3 \cdot x} \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \color{blue}{x \cdot 3} \]
      2. lower-*.f6468.4

        \[\leadsto \color{blue}{x \cdot 3} \]
    5. Applied rewrites68.4%

      \[\leadsto \color{blue}{x \cdot 3} \]

    if -1.1e20 < x < 1.45000000000000009e179

    1. Initial program 100.0%

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

      \[\leadsto \color{blue}{z + 2 \cdot y} \]
    4. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \color{blue}{2 \cdot y + z} \]
      2. lower-fma.f6486.2

        \[\leadsto \color{blue}{\mathsf{fma}\left(2, y, z\right)} \]
    5. Applied rewrites86.2%

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

Alternative 5: 53.2% accurate, 0.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y \leq -520:\\ \;\;\;\;y + y\\ \mathbf{elif}\;y \leq 42000000000000:\\ \;\;\;\;x \cdot 3\\ \mathbf{else}:\\ \;\;\;\;y + y\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (<= y -520.0) (+ y y) (if (<= y 42000000000000.0) (* x 3.0) (+ y y))))
double code(double x, double y, double z) {
	double tmp;
	if (y <= -520.0) {
		tmp = y + y;
	} else if (y <= 42000000000000.0) {
		tmp = x * 3.0;
	} else {
		tmp = y + y;
	}
	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) :: tmp
    if (y <= (-520.0d0)) then
        tmp = y + y
    else if (y <= 42000000000000.0d0) then
        tmp = x * 3.0d0
    else
        tmp = y + y
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double tmp;
	if (y <= -520.0) {
		tmp = y + y;
	} else if (y <= 42000000000000.0) {
		tmp = x * 3.0;
	} else {
		tmp = y + y;
	}
	return tmp;
}
def code(x, y, z):
	tmp = 0
	if y <= -520.0:
		tmp = y + y
	elif y <= 42000000000000.0:
		tmp = x * 3.0
	else:
		tmp = y + y
	return tmp
function code(x, y, z)
	tmp = 0.0
	if (y <= -520.0)
		tmp = Float64(y + y);
	elseif (y <= 42000000000000.0)
		tmp = Float64(x * 3.0);
	else
		tmp = Float64(y + y);
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if (y <= -520.0)
		tmp = y + y;
	elseif (y <= 42000000000000.0)
		tmp = x * 3.0;
	else
		tmp = y + y;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := If[LessEqual[y, -520.0], N[(y + y), $MachinePrecision], If[LessEqual[y, 42000000000000.0], N[(x * 3.0), $MachinePrecision], N[(y + y), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;y \leq -520:\\
\;\;\;\;y + y\\

\mathbf{elif}\;y \leq 42000000000000:\\
\;\;\;\;x \cdot 3\\

\mathbf{else}:\\
\;\;\;\;y + y\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < -520 or 4.2e13 < y

    1. Initial program 99.9%

      \[\left(\left(\left(\left(x + y\right) + y\right) + x\right) + z\right) + x \]
    2. Add Preprocessing
    3. Taylor expanded in y around inf

      \[\leadsto \color{blue}{2 \cdot y} \]
    4. Step-by-step derivation
      1. lower-*.f6464.6

        \[\leadsto \color{blue}{2 \cdot y} \]
    5. Applied rewrites64.6%

      \[\leadsto \color{blue}{2 \cdot y} \]
    6. Step-by-step derivation
      1. Applied rewrites64.6%

        \[\leadsto y + \color{blue}{y} \]

      if -520 < y < 4.2e13

      1. Initial program 99.9%

        \[\left(\left(\left(\left(x + y\right) + y\right) + x\right) + z\right) + x \]
      2. Add Preprocessing
      3. Taylor expanded in x around inf

        \[\leadsto \color{blue}{3 \cdot x} \]
      4. Step-by-step derivation
        1. *-commutativeN/A

          \[\leadsto \color{blue}{x \cdot 3} \]
        2. lower-*.f6455.3

          \[\leadsto \color{blue}{x \cdot 3} \]
      5. Applied rewrites55.3%

        \[\leadsto \color{blue}{x \cdot 3} \]
    7. Recombined 2 regimes into one program.
    8. Add Preprocessing

    Alternative 6: 34.6% accurate, 4.0× speedup?

    \[\begin{array}{l} \\ y + y \end{array} \]
    (FPCore (x y z) :precision binary64 (+ y y))
    double code(double x, double y, double z) {
    	return y + y;
    }
    
    real(8) function code(x, y, z)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        real(8), intent (in) :: z
        code = y + y
    end function
    
    public static double code(double x, double y, double z) {
    	return y + y;
    }
    
    def code(x, y, z):
    	return y + y
    
    function code(x, y, z)
    	return Float64(y + y)
    end
    
    function tmp = code(x, y, z)
    	tmp = y + y;
    end
    
    code[x_, y_, z_] := N[(y + y), $MachinePrecision]
    
    \begin{array}{l}
    
    \\
    y + y
    \end{array}
    
    Derivation
    1. Initial program 99.9%

      \[\left(\left(\left(\left(x + y\right) + y\right) + x\right) + z\right) + x \]
    2. Add Preprocessing
    3. Taylor expanded in y around inf

      \[\leadsto \color{blue}{2 \cdot y} \]
    4. Step-by-step derivation
      1. lower-*.f6442.7

        \[\leadsto \color{blue}{2 \cdot y} \]
    5. Applied rewrites42.7%

      \[\leadsto \color{blue}{2 \cdot y} \]
    6. Step-by-step derivation
      1. Applied rewrites42.7%

        \[\leadsto y + \color{blue}{y} \]
      2. Add Preprocessing

      Reproduce

      ?
      herbie shell --seed 2024233 
      (FPCore (x y z)
        :name "Graphics.Rendering.Plot.Render.Plot.Legend:renderLegendInside from plot-0.2.3.4"
        :precision binary64
        (+ (+ (+ (+ (+ x y) y) x) z) x))