Diagrams.Tangent:$catParam from diagrams-lib-1.3.0.3, D

Percentage Accurate: 99.8% → 99.8%
Time: 4.3s
Alternatives: 6
Speedup: 1.4×

Specification

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

\\
3 \cdot \left(\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) + 1\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 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.8% accurate, 1.0× speedup?

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

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

Alternative 1: 99.8% accurate, 1.2× speedup?

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

\\
\left(9 \cdot \left(x \cdot x\right) + x \cdot -12\right) + 3
\end{array}
Derivation
  1. Initial program 99.8%

    \[3 \cdot \left(\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) + 1\right) \]
  2. Step-by-step derivation
    1. *-commutative99.8%

      \[\leadsto \color{blue}{\left(\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) + 1\right) \cdot 3} \]
    2. distribute-lft1-in99.9%

      \[\leadsto \color{blue}{\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) \cdot 3 + 3} \]
    3. *-commutative99.9%

      \[\leadsto \left(\color{blue}{x \cdot \left(x \cdot 3\right)} - x \cdot 4\right) \cdot 3 + 3 \]
    4. distribute-lft-out--99.9%

      \[\leadsto \color{blue}{\left(x \cdot \left(x \cdot 3 - 4\right)\right)} \cdot 3 + 3 \]
    5. associate-*l*99.9%

      \[\leadsto \color{blue}{x \cdot \left(\left(x \cdot 3 - 4\right) \cdot 3\right)} + 3 \]
    6. fma-def99.9%

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, \left(x \cdot 3 - 4\right) \cdot 3, 3\right)} \]
    7. *-commutative99.9%

      \[\leadsto \mathsf{fma}\left(x, \color{blue}{3 \cdot \left(x \cdot 3 - 4\right)}, 3\right) \]
    8. sub-neg99.9%

      \[\leadsto \mathsf{fma}\left(x, 3 \cdot \color{blue}{\left(x \cdot 3 + \left(-4\right)\right)}, 3\right) \]
    9. distribute-lft-in99.9%

      \[\leadsto \mathsf{fma}\left(x, \color{blue}{3 \cdot \left(x \cdot 3\right) + 3 \cdot \left(-4\right)}, 3\right) \]
    10. *-commutative99.9%

      \[\leadsto \mathsf{fma}\left(x, \color{blue}{\left(x \cdot 3\right) \cdot 3} + 3 \cdot \left(-4\right), 3\right) \]
    11. associate-*l*99.9%

      \[\leadsto \mathsf{fma}\left(x, \color{blue}{x \cdot \left(3 \cdot 3\right)} + 3 \cdot \left(-4\right), 3\right) \]
    12. fma-def99.9%

      \[\leadsto \mathsf{fma}\left(x, \color{blue}{\mathsf{fma}\left(x, 3 \cdot 3, 3 \cdot \left(-4\right)\right)}, 3\right) \]
    13. metadata-eval99.9%

      \[\leadsto \mathsf{fma}\left(x, \mathsf{fma}\left(x, \color{blue}{9}, 3 \cdot \left(-4\right)\right), 3\right) \]
    14. metadata-eval99.9%

      \[\leadsto \mathsf{fma}\left(x, \mathsf{fma}\left(x, 9, 3 \cdot \color{blue}{-4}\right), 3\right) \]
    15. metadata-eval99.9%

      \[\leadsto \mathsf{fma}\left(x, \mathsf{fma}\left(x, 9, \color{blue}{-12}\right), 3\right) \]
  3. Simplified99.9%

    \[\leadsto \color{blue}{\mathsf{fma}\left(x, \mathsf{fma}\left(x, 9, -12\right), 3\right)} \]
  4. Step-by-step derivation
    1. fma-udef99.9%

      \[\leadsto \color{blue}{x \cdot \mathsf{fma}\left(x, 9, -12\right) + 3} \]
  5. Applied egg-rr99.9%

    \[\leadsto \color{blue}{x \cdot \mathsf{fma}\left(x, 9, -12\right) + 3} \]
  6. Step-by-step derivation
    1. fma-udef99.9%

      \[\leadsto x \cdot \color{blue}{\left(x \cdot 9 + -12\right)} + 3 \]
    2. distribute-lft-in99.9%

      \[\leadsto \color{blue}{\left(x \cdot \left(x \cdot 9\right) + x \cdot -12\right)} + 3 \]
  7. Applied egg-rr99.9%

    \[\leadsto \color{blue}{\left(x \cdot \left(x \cdot 9\right) + x \cdot -12\right)} + 3 \]
  8. Taylor expanded in x around 0 99.9%

    \[\leadsto \left(\color{blue}{9 \cdot {x}^{2}} + x \cdot -12\right) + 3 \]
  9. Step-by-step derivation
    1. unpow299.9%

      \[\leadsto \left(9 \cdot \color{blue}{\left(x \cdot x\right)} + x \cdot -12\right) + 3 \]
  10. Simplified99.9%

    \[\leadsto \left(\color{blue}{9 \cdot \left(x \cdot x\right)} + x \cdot -12\right) + 3 \]
  11. Final simplification99.9%

    \[\leadsto \left(9 \cdot \left(x \cdot x\right) + x \cdot -12\right) + 3 \]

Alternative 2: 97.7% accurate, 1.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -0.56 \lor \neg \left(x \leq 1.68\right):\\ \;\;\;\;9 \cdot \left(x \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;3\\ \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (if (or (<= x -0.56) (not (<= x 1.68))) (* 9.0 (* x x)) 3.0))
double code(double x) {
	double tmp;
	if ((x <= -0.56) || !(x <= 1.68)) {
		tmp = 9.0 * (x * x);
	} else {
		tmp = 3.0;
	}
	return tmp;
}
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: tmp
    if ((x <= (-0.56d0)) .or. (.not. (x <= 1.68d0))) then
        tmp = 9.0d0 * (x * x)
    else
        tmp = 3.0d0
    end if
    code = tmp
end function
public static double code(double x) {
	double tmp;
	if ((x <= -0.56) || !(x <= 1.68)) {
		tmp = 9.0 * (x * x);
	} else {
		tmp = 3.0;
	}
	return tmp;
}
def code(x):
	tmp = 0
	if (x <= -0.56) or not (x <= 1.68):
		tmp = 9.0 * (x * x)
	else:
		tmp = 3.0
	return tmp
function code(x)
	tmp = 0.0
	if ((x <= -0.56) || !(x <= 1.68))
		tmp = Float64(9.0 * Float64(x * x));
	else
		tmp = 3.0;
	end
	return tmp
end
function tmp_2 = code(x)
	tmp = 0.0;
	if ((x <= -0.56) || ~((x <= 1.68)))
		tmp = 9.0 * (x * x);
	else
		tmp = 3.0;
	end
	tmp_2 = tmp;
end
code[x_] := If[Or[LessEqual[x, -0.56], N[Not[LessEqual[x, 1.68]], $MachinePrecision]], N[(9.0 * N[(x * x), $MachinePrecision]), $MachinePrecision], 3.0]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq -0.56 \lor \neg \left(x \leq 1.68\right):\\
\;\;\;\;9 \cdot \left(x \cdot x\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < -0.56000000000000005 or 1.67999999999999994 < x

    1. Initial program 99.7%

      \[3 \cdot \left(\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) + 1\right) \]
    2. Step-by-step derivation
      1. *-commutative99.7%

        \[\leadsto \color{blue}{\left(\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) + 1\right) \cdot 3} \]
      2. distribute-lft1-in99.7%

        \[\leadsto \color{blue}{\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) \cdot 3 + 3} \]
      3. *-commutative99.7%

        \[\leadsto \left(\color{blue}{x \cdot \left(x \cdot 3\right)} - x \cdot 4\right) \cdot 3 + 3 \]
      4. distribute-lft-out--99.7%

        \[\leadsto \color{blue}{\left(x \cdot \left(x \cdot 3 - 4\right)\right)} \cdot 3 + 3 \]
      5. associate-*l*99.7%

        \[\leadsto \color{blue}{x \cdot \left(\left(x \cdot 3 - 4\right) \cdot 3\right)} + 3 \]
      6. fma-def99.7%

        \[\leadsto \color{blue}{\mathsf{fma}\left(x, \left(x \cdot 3 - 4\right) \cdot 3, 3\right)} \]
      7. *-commutative99.7%

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{3 \cdot \left(x \cdot 3 - 4\right)}, 3\right) \]
      8. sub-neg99.7%

        \[\leadsto \mathsf{fma}\left(x, 3 \cdot \color{blue}{\left(x \cdot 3 + \left(-4\right)\right)}, 3\right) \]
      9. distribute-lft-in99.7%

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{3 \cdot \left(x \cdot 3\right) + 3 \cdot \left(-4\right)}, 3\right) \]
      10. *-commutative99.7%

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{\left(x \cdot 3\right) \cdot 3} + 3 \cdot \left(-4\right), 3\right) \]
      11. associate-*l*99.8%

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{x \cdot \left(3 \cdot 3\right)} + 3 \cdot \left(-4\right), 3\right) \]
      12. fma-def99.8%

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{\mathsf{fma}\left(x, 3 \cdot 3, 3 \cdot \left(-4\right)\right)}, 3\right) \]
      13. metadata-eval99.8%

        \[\leadsto \mathsf{fma}\left(x, \mathsf{fma}\left(x, \color{blue}{9}, 3 \cdot \left(-4\right)\right), 3\right) \]
      14. metadata-eval99.8%

        \[\leadsto \mathsf{fma}\left(x, \mathsf{fma}\left(x, 9, 3 \cdot \color{blue}{-4}\right), 3\right) \]
      15. metadata-eval99.8%

        \[\leadsto \mathsf{fma}\left(x, \mathsf{fma}\left(x, 9, \color{blue}{-12}\right), 3\right) \]
    3. Simplified99.8%

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, \mathsf{fma}\left(x, 9, -12\right), 3\right)} \]
    4. Taylor expanded in x around inf 98.4%

      \[\leadsto \color{blue}{9 \cdot {x}^{2}} \]
    5. Step-by-step derivation
      1. unpow298.4%

        \[\leadsto 9 \cdot \color{blue}{\left(x \cdot x\right)} \]
    6. Simplified98.4%

      \[\leadsto \color{blue}{9 \cdot \left(x \cdot x\right)} \]

    if -0.56000000000000005 < x < 1.67999999999999994

    1. Initial program 100.0%

      \[3 \cdot \left(\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) + 1\right) \]
    2. Step-by-step derivation
      1. *-commutative100.0%

        \[\leadsto \color{blue}{\left(\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) + 1\right) \cdot 3} \]
      2. distribute-lft1-in100.0%

        \[\leadsto \color{blue}{\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) \cdot 3 + 3} \]
      3. *-commutative100.0%

        \[\leadsto \left(\color{blue}{x \cdot \left(x \cdot 3\right)} - x \cdot 4\right) \cdot 3 + 3 \]
      4. distribute-lft-out--100.0%

        \[\leadsto \color{blue}{\left(x \cdot \left(x \cdot 3 - 4\right)\right)} \cdot 3 + 3 \]
      5. associate-*l*100.0%

        \[\leadsto \color{blue}{x \cdot \left(\left(x \cdot 3 - 4\right) \cdot 3\right)} + 3 \]
      6. fma-def100.0%

        \[\leadsto \color{blue}{\mathsf{fma}\left(x, \left(x \cdot 3 - 4\right) \cdot 3, 3\right)} \]
      7. *-commutative100.0%

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{3 \cdot \left(x \cdot 3 - 4\right)}, 3\right) \]
      8. sub-neg100.0%

        \[\leadsto \mathsf{fma}\left(x, 3 \cdot \color{blue}{\left(x \cdot 3 + \left(-4\right)\right)}, 3\right) \]
      9. distribute-lft-in100.0%

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{3 \cdot \left(x \cdot 3\right) + 3 \cdot \left(-4\right)}, 3\right) \]
      10. *-commutative100.0%

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{\left(x \cdot 3\right) \cdot 3} + 3 \cdot \left(-4\right), 3\right) \]
      11. associate-*l*100.0%

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{x \cdot \left(3 \cdot 3\right)} + 3 \cdot \left(-4\right), 3\right) \]
      12. fma-def100.0%

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{\mathsf{fma}\left(x, 3 \cdot 3, 3 \cdot \left(-4\right)\right)}, 3\right) \]
      13. metadata-eval100.0%

        \[\leadsto \mathsf{fma}\left(x, \mathsf{fma}\left(x, \color{blue}{9}, 3 \cdot \left(-4\right)\right), 3\right) \]
      14. metadata-eval100.0%

        \[\leadsto \mathsf{fma}\left(x, \mathsf{fma}\left(x, 9, 3 \cdot \color{blue}{-4}\right), 3\right) \]
      15. metadata-eval100.0%

        \[\leadsto \mathsf{fma}\left(x, \mathsf{fma}\left(x, 9, \color{blue}{-12}\right), 3\right) \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, \mathsf{fma}\left(x, 9, -12\right), 3\right)} \]
    4. Taylor expanded in x around 0 97.5%

      \[\leadsto \color{blue}{3} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification97.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -0.56 \lor \neg \left(x \leq 1.68\right):\\ \;\;\;\;9 \cdot \left(x \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;3\\ \end{array} \]

Alternative 3: 98.2% accurate, 1.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -1.5 \lor \neg \left(x \leq 1\right):\\ \;\;\;\;9 \cdot \left(x \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot -12 + 3\\ \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (if (or (<= x -1.5) (not (<= x 1.0))) (* 9.0 (* x x)) (+ (* x -12.0) 3.0)))
double code(double x) {
	double tmp;
	if ((x <= -1.5) || !(x <= 1.0)) {
		tmp = 9.0 * (x * x);
	} else {
		tmp = (x * -12.0) + 3.0;
	}
	return tmp;
}
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: tmp
    if ((x <= (-1.5d0)) .or. (.not. (x <= 1.0d0))) then
        tmp = 9.0d0 * (x * x)
    else
        tmp = (x * (-12.0d0)) + 3.0d0
    end if
    code = tmp
end function
public static double code(double x) {
	double tmp;
	if ((x <= -1.5) || !(x <= 1.0)) {
		tmp = 9.0 * (x * x);
	} else {
		tmp = (x * -12.0) + 3.0;
	}
	return tmp;
}
def code(x):
	tmp = 0
	if (x <= -1.5) or not (x <= 1.0):
		tmp = 9.0 * (x * x)
	else:
		tmp = (x * -12.0) + 3.0
	return tmp
function code(x)
	tmp = 0.0
	if ((x <= -1.5) || !(x <= 1.0))
		tmp = Float64(9.0 * Float64(x * x));
	else
		tmp = Float64(Float64(x * -12.0) + 3.0);
	end
	return tmp
end
function tmp_2 = code(x)
	tmp = 0.0;
	if ((x <= -1.5) || ~((x <= 1.0)))
		tmp = 9.0 * (x * x);
	else
		tmp = (x * -12.0) + 3.0;
	end
	tmp_2 = tmp;
end
code[x_] := If[Or[LessEqual[x, -1.5], N[Not[LessEqual[x, 1.0]], $MachinePrecision]], N[(9.0 * N[(x * x), $MachinePrecision]), $MachinePrecision], N[(N[(x * -12.0), $MachinePrecision] + 3.0), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.5 \lor \neg \left(x \leq 1\right):\\
\;\;\;\;9 \cdot \left(x \cdot x\right)\\

\mathbf{else}:\\
\;\;\;\;x \cdot -12 + 3\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < -1.5 or 1 < x

    1. Initial program 99.7%

      \[3 \cdot \left(\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) + 1\right) \]
    2. Step-by-step derivation
      1. *-commutative99.7%

        \[\leadsto \color{blue}{\left(\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) + 1\right) \cdot 3} \]
      2. distribute-lft1-in99.7%

        \[\leadsto \color{blue}{\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) \cdot 3 + 3} \]
      3. *-commutative99.7%

        \[\leadsto \left(\color{blue}{x \cdot \left(x \cdot 3\right)} - x \cdot 4\right) \cdot 3 + 3 \]
      4. distribute-lft-out--99.7%

        \[\leadsto \color{blue}{\left(x \cdot \left(x \cdot 3 - 4\right)\right)} \cdot 3 + 3 \]
      5. associate-*l*99.7%

        \[\leadsto \color{blue}{x \cdot \left(\left(x \cdot 3 - 4\right) \cdot 3\right)} + 3 \]
      6. fma-def99.7%

        \[\leadsto \color{blue}{\mathsf{fma}\left(x, \left(x \cdot 3 - 4\right) \cdot 3, 3\right)} \]
      7. *-commutative99.7%

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{3 \cdot \left(x \cdot 3 - 4\right)}, 3\right) \]
      8. sub-neg99.7%

        \[\leadsto \mathsf{fma}\left(x, 3 \cdot \color{blue}{\left(x \cdot 3 + \left(-4\right)\right)}, 3\right) \]
      9. distribute-lft-in99.7%

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{3 \cdot \left(x \cdot 3\right) + 3 \cdot \left(-4\right)}, 3\right) \]
      10. *-commutative99.7%

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{\left(x \cdot 3\right) \cdot 3} + 3 \cdot \left(-4\right), 3\right) \]
      11. associate-*l*99.8%

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{x \cdot \left(3 \cdot 3\right)} + 3 \cdot \left(-4\right), 3\right) \]
      12. fma-def99.8%

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{\mathsf{fma}\left(x, 3 \cdot 3, 3 \cdot \left(-4\right)\right)}, 3\right) \]
      13. metadata-eval99.8%

        \[\leadsto \mathsf{fma}\left(x, \mathsf{fma}\left(x, \color{blue}{9}, 3 \cdot \left(-4\right)\right), 3\right) \]
      14. metadata-eval99.8%

        \[\leadsto \mathsf{fma}\left(x, \mathsf{fma}\left(x, 9, 3 \cdot \color{blue}{-4}\right), 3\right) \]
      15. metadata-eval99.8%

        \[\leadsto \mathsf{fma}\left(x, \mathsf{fma}\left(x, 9, \color{blue}{-12}\right), 3\right) \]
    3. Simplified99.8%

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, \mathsf{fma}\left(x, 9, -12\right), 3\right)} \]
    4. Taylor expanded in x around inf 98.4%

      \[\leadsto \color{blue}{9 \cdot {x}^{2}} \]
    5. Step-by-step derivation
      1. unpow298.4%

        \[\leadsto 9 \cdot \color{blue}{\left(x \cdot x\right)} \]
    6. Simplified98.4%

      \[\leadsto \color{blue}{9 \cdot \left(x \cdot x\right)} \]

    if -1.5 < x < 1

    1. Initial program 100.0%

      \[3 \cdot \left(\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) + 1\right) \]
    2. Step-by-step derivation
      1. *-commutative100.0%

        \[\leadsto \color{blue}{\left(\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) + 1\right) \cdot 3} \]
      2. distribute-lft1-in100.0%

        \[\leadsto \color{blue}{\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) \cdot 3 + 3} \]
      3. *-commutative100.0%

        \[\leadsto \left(\color{blue}{x \cdot \left(x \cdot 3\right)} - x \cdot 4\right) \cdot 3 + 3 \]
      4. distribute-lft-out--100.0%

        \[\leadsto \color{blue}{\left(x \cdot \left(x \cdot 3 - 4\right)\right)} \cdot 3 + 3 \]
      5. associate-*l*100.0%

        \[\leadsto \color{blue}{x \cdot \left(\left(x \cdot 3 - 4\right) \cdot 3\right)} + 3 \]
      6. fma-def100.0%

        \[\leadsto \color{blue}{\mathsf{fma}\left(x, \left(x \cdot 3 - 4\right) \cdot 3, 3\right)} \]
      7. *-commutative100.0%

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{3 \cdot \left(x \cdot 3 - 4\right)}, 3\right) \]
      8. sub-neg100.0%

        \[\leadsto \mathsf{fma}\left(x, 3 \cdot \color{blue}{\left(x \cdot 3 + \left(-4\right)\right)}, 3\right) \]
      9. distribute-lft-in100.0%

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{3 \cdot \left(x \cdot 3\right) + 3 \cdot \left(-4\right)}, 3\right) \]
      10. *-commutative100.0%

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{\left(x \cdot 3\right) \cdot 3} + 3 \cdot \left(-4\right), 3\right) \]
      11. associate-*l*100.0%

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{x \cdot \left(3 \cdot 3\right)} + 3 \cdot \left(-4\right), 3\right) \]
      12. fma-def100.0%

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{\mathsf{fma}\left(x, 3 \cdot 3, 3 \cdot \left(-4\right)\right)}, 3\right) \]
      13. metadata-eval100.0%

        \[\leadsto \mathsf{fma}\left(x, \mathsf{fma}\left(x, \color{blue}{9}, 3 \cdot \left(-4\right)\right), 3\right) \]
      14. metadata-eval100.0%

        \[\leadsto \mathsf{fma}\left(x, \mathsf{fma}\left(x, 9, 3 \cdot \color{blue}{-4}\right), 3\right) \]
      15. metadata-eval100.0%

        \[\leadsto \mathsf{fma}\left(x, \mathsf{fma}\left(x, 9, \color{blue}{-12}\right), 3\right) \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, \mathsf{fma}\left(x, 9, -12\right), 3\right)} \]
    4. Taylor expanded in x around 0 98.6%

      \[\leadsto \color{blue}{-12 \cdot x + 3} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification98.5%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -1.5 \lor \neg \left(x \leq 1\right):\\ \;\;\;\;9 \cdot \left(x \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot -12 + 3\\ \end{array} \]

Alternative 4: 99.9% accurate, 1.4× speedup?

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

\\
3 + x \cdot \left(-12 + 9 \cdot x\right)
\end{array}
Derivation
  1. Initial program 99.8%

    \[3 \cdot \left(\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) + 1\right) \]
  2. Step-by-step derivation
    1. *-commutative99.8%

      \[\leadsto \color{blue}{\left(\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) + 1\right) \cdot 3} \]
    2. distribute-lft1-in99.9%

      \[\leadsto \color{blue}{\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) \cdot 3 + 3} \]
    3. *-commutative99.9%

      \[\leadsto \left(\color{blue}{x \cdot \left(x \cdot 3\right)} - x \cdot 4\right) \cdot 3 + 3 \]
    4. distribute-lft-out--99.9%

      \[\leadsto \color{blue}{\left(x \cdot \left(x \cdot 3 - 4\right)\right)} \cdot 3 + 3 \]
    5. associate-*l*99.9%

      \[\leadsto \color{blue}{x \cdot \left(\left(x \cdot 3 - 4\right) \cdot 3\right)} + 3 \]
    6. fma-def99.9%

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, \left(x \cdot 3 - 4\right) \cdot 3, 3\right)} \]
    7. *-commutative99.9%

      \[\leadsto \mathsf{fma}\left(x, \color{blue}{3 \cdot \left(x \cdot 3 - 4\right)}, 3\right) \]
    8. sub-neg99.9%

      \[\leadsto \mathsf{fma}\left(x, 3 \cdot \color{blue}{\left(x \cdot 3 + \left(-4\right)\right)}, 3\right) \]
    9. distribute-lft-in99.9%

      \[\leadsto \mathsf{fma}\left(x, \color{blue}{3 \cdot \left(x \cdot 3\right) + 3 \cdot \left(-4\right)}, 3\right) \]
    10. *-commutative99.9%

      \[\leadsto \mathsf{fma}\left(x, \color{blue}{\left(x \cdot 3\right) \cdot 3} + 3 \cdot \left(-4\right), 3\right) \]
    11. associate-*l*99.9%

      \[\leadsto \mathsf{fma}\left(x, \color{blue}{x \cdot \left(3 \cdot 3\right)} + 3 \cdot \left(-4\right), 3\right) \]
    12. fma-def99.9%

      \[\leadsto \mathsf{fma}\left(x, \color{blue}{\mathsf{fma}\left(x, 3 \cdot 3, 3 \cdot \left(-4\right)\right)}, 3\right) \]
    13. metadata-eval99.9%

      \[\leadsto \mathsf{fma}\left(x, \mathsf{fma}\left(x, \color{blue}{9}, 3 \cdot \left(-4\right)\right), 3\right) \]
    14. metadata-eval99.9%

      \[\leadsto \mathsf{fma}\left(x, \mathsf{fma}\left(x, 9, 3 \cdot \color{blue}{-4}\right), 3\right) \]
    15. metadata-eval99.9%

      \[\leadsto \mathsf{fma}\left(x, \mathsf{fma}\left(x, 9, \color{blue}{-12}\right), 3\right) \]
  3. Simplified99.9%

    \[\leadsto \color{blue}{\mathsf{fma}\left(x, \mathsf{fma}\left(x, 9, -12\right), 3\right)} \]
  4. Step-by-step derivation
    1. fma-udef99.9%

      \[\leadsto \color{blue}{x \cdot \mathsf{fma}\left(x, 9, -12\right) + 3} \]
  5. Applied egg-rr99.9%

    \[\leadsto \color{blue}{x \cdot \mathsf{fma}\left(x, 9, -12\right) + 3} \]
  6. Step-by-step derivation
    1. fma-udef99.9%

      \[\leadsto x \cdot \color{blue}{\left(x \cdot 9 + -12\right)} + 3 \]
    2. distribute-lft-in99.9%

      \[\leadsto \color{blue}{\left(x \cdot \left(x \cdot 9\right) + x \cdot -12\right)} + 3 \]
  7. Applied egg-rr99.9%

    \[\leadsto \color{blue}{\left(x \cdot \left(x \cdot 9\right) + x \cdot -12\right)} + 3 \]
  8. Taylor expanded in x around 0 99.9%

    \[\leadsto \left(\color{blue}{9 \cdot {x}^{2}} + x \cdot -12\right) + 3 \]
  9. Step-by-step derivation
    1. unpow299.9%

      \[\leadsto \left(9 \cdot \color{blue}{\left(x \cdot x\right)} + x \cdot -12\right) + 3 \]
  10. Simplified99.9%

    \[\leadsto \left(\color{blue}{9 \cdot \left(x \cdot x\right)} + x \cdot -12\right) + 3 \]
  11. Step-by-step derivation
    1. +-commutative99.9%

      \[\leadsto \color{blue}{\left(x \cdot -12 + 9 \cdot \left(x \cdot x\right)\right)} + 3 \]
    2. *-commutative99.9%

      \[\leadsto \left(x \cdot -12 + \color{blue}{\left(x \cdot x\right) \cdot 9}\right) + 3 \]
    3. metadata-eval99.9%

      \[\leadsto \left(x \cdot -12 + \left(x \cdot x\right) \cdot \color{blue}{\left(3 \cdot 3\right)}\right) + 3 \]
    4. swap-sqr99.8%

      \[\leadsto \left(x \cdot -12 + \color{blue}{\left(x \cdot 3\right) \cdot \left(x \cdot 3\right)}\right) + 3 \]
    5. associate-*l*99.9%

      \[\leadsto \left(x \cdot -12 + \color{blue}{x \cdot \left(3 \cdot \left(x \cdot 3\right)\right)}\right) + 3 \]
    6. distribute-lft-out99.9%

      \[\leadsto \color{blue}{x \cdot \left(-12 + 3 \cdot \left(x \cdot 3\right)\right)} + 3 \]
    7. *-commutative99.9%

      \[\leadsto x \cdot \left(-12 + 3 \cdot \color{blue}{\left(3 \cdot x\right)}\right) + 3 \]
    8. associate-*r*99.9%

      \[\leadsto x \cdot \left(-12 + \color{blue}{\left(3 \cdot 3\right) \cdot x}\right) + 3 \]
    9. metadata-eval99.9%

      \[\leadsto x \cdot \left(-12 + \color{blue}{9} \cdot x\right) + 3 \]
    10. *-commutative99.9%

      \[\leadsto x \cdot \left(-12 + \color{blue}{x \cdot 9}\right) + 3 \]
  12. Applied egg-rr99.9%

    \[\leadsto \color{blue}{x \cdot \left(-12 + x \cdot 9\right)} + 3 \]
  13. Final simplification99.9%

    \[\leadsto 3 + x \cdot \left(-12 + 9 \cdot x\right) \]

Alternative 5: 97.7% accurate, 1.9× speedup?

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

\\
9 \cdot \left(x \cdot x\right) + 3
\end{array}
Derivation
  1. Initial program 99.8%

    \[3 \cdot \left(\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) + 1\right) \]
  2. Step-by-step derivation
    1. *-commutative99.8%

      \[\leadsto \color{blue}{\left(\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) + 1\right) \cdot 3} \]
    2. distribute-lft1-in99.9%

      \[\leadsto \color{blue}{\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) \cdot 3 + 3} \]
    3. *-commutative99.9%

      \[\leadsto \left(\color{blue}{x \cdot \left(x \cdot 3\right)} - x \cdot 4\right) \cdot 3 + 3 \]
    4. distribute-lft-out--99.9%

      \[\leadsto \color{blue}{\left(x \cdot \left(x \cdot 3 - 4\right)\right)} \cdot 3 + 3 \]
    5. associate-*l*99.9%

      \[\leadsto \color{blue}{x \cdot \left(\left(x \cdot 3 - 4\right) \cdot 3\right)} + 3 \]
    6. fma-def99.9%

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, \left(x \cdot 3 - 4\right) \cdot 3, 3\right)} \]
    7. *-commutative99.9%

      \[\leadsto \mathsf{fma}\left(x, \color{blue}{3 \cdot \left(x \cdot 3 - 4\right)}, 3\right) \]
    8. sub-neg99.9%

      \[\leadsto \mathsf{fma}\left(x, 3 \cdot \color{blue}{\left(x \cdot 3 + \left(-4\right)\right)}, 3\right) \]
    9. distribute-lft-in99.9%

      \[\leadsto \mathsf{fma}\left(x, \color{blue}{3 \cdot \left(x \cdot 3\right) + 3 \cdot \left(-4\right)}, 3\right) \]
    10. *-commutative99.9%

      \[\leadsto \mathsf{fma}\left(x, \color{blue}{\left(x \cdot 3\right) \cdot 3} + 3 \cdot \left(-4\right), 3\right) \]
    11. associate-*l*99.9%

      \[\leadsto \mathsf{fma}\left(x, \color{blue}{x \cdot \left(3 \cdot 3\right)} + 3 \cdot \left(-4\right), 3\right) \]
    12. fma-def99.9%

      \[\leadsto \mathsf{fma}\left(x, \color{blue}{\mathsf{fma}\left(x, 3 \cdot 3, 3 \cdot \left(-4\right)\right)}, 3\right) \]
    13. metadata-eval99.9%

      \[\leadsto \mathsf{fma}\left(x, \mathsf{fma}\left(x, \color{blue}{9}, 3 \cdot \left(-4\right)\right), 3\right) \]
    14. metadata-eval99.9%

      \[\leadsto \mathsf{fma}\left(x, \mathsf{fma}\left(x, 9, 3 \cdot \color{blue}{-4}\right), 3\right) \]
    15. metadata-eval99.9%

      \[\leadsto \mathsf{fma}\left(x, \mathsf{fma}\left(x, 9, \color{blue}{-12}\right), 3\right) \]
  3. Simplified99.9%

    \[\leadsto \color{blue}{\mathsf{fma}\left(x, \mathsf{fma}\left(x, 9, -12\right), 3\right)} \]
  4. Step-by-step derivation
    1. fma-udef99.9%

      \[\leadsto \color{blue}{x \cdot \mathsf{fma}\left(x, 9, -12\right) + 3} \]
  5. Applied egg-rr99.9%

    \[\leadsto \color{blue}{x \cdot \mathsf{fma}\left(x, 9, -12\right) + 3} \]
  6. Taylor expanded in x around inf 97.9%

    \[\leadsto \color{blue}{9 \cdot {x}^{2}} + 3 \]
  7. Step-by-step derivation
    1. unpow297.9%

      \[\leadsto 9 \cdot \color{blue}{\left(x \cdot x\right)} + 3 \]
  8. Simplified97.9%

    \[\leadsto \color{blue}{9 \cdot \left(x \cdot x\right)} + 3 \]
  9. Final simplification97.9%

    \[\leadsto 9 \cdot \left(x \cdot x\right) + 3 \]

Alternative 6: 50.5% accurate, 13.0× speedup?

\[\begin{array}{l} \\ 3 \end{array} \]
(FPCore (x) :precision binary64 3.0)
double code(double x) {
	return 3.0;
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = 3.0d0
end function
public static double code(double x) {
	return 3.0;
}
def code(x):
	return 3.0
function code(x)
	return 3.0
end
function tmp = code(x)
	tmp = 3.0;
end
code[x_] := 3.0
\begin{array}{l}

\\
3
\end{array}
Derivation
  1. Initial program 99.8%

    \[3 \cdot \left(\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) + 1\right) \]
  2. Step-by-step derivation
    1. *-commutative99.8%

      \[\leadsto \color{blue}{\left(\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) + 1\right) \cdot 3} \]
    2. distribute-lft1-in99.9%

      \[\leadsto \color{blue}{\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) \cdot 3 + 3} \]
    3. *-commutative99.9%

      \[\leadsto \left(\color{blue}{x \cdot \left(x \cdot 3\right)} - x \cdot 4\right) \cdot 3 + 3 \]
    4. distribute-lft-out--99.9%

      \[\leadsto \color{blue}{\left(x \cdot \left(x \cdot 3 - 4\right)\right)} \cdot 3 + 3 \]
    5. associate-*l*99.9%

      \[\leadsto \color{blue}{x \cdot \left(\left(x \cdot 3 - 4\right) \cdot 3\right)} + 3 \]
    6. fma-def99.9%

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, \left(x \cdot 3 - 4\right) \cdot 3, 3\right)} \]
    7. *-commutative99.9%

      \[\leadsto \mathsf{fma}\left(x, \color{blue}{3 \cdot \left(x \cdot 3 - 4\right)}, 3\right) \]
    8. sub-neg99.9%

      \[\leadsto \mathsf{fma}\left(x, 3 \cdot \color{blue}{\left(x \cdot 3 + \left(-4\right)\right)}, 3\right) \]
    9. distribute-lft-in99.9%

      \[\leadsto \mathsf{fma}\left(x, \color{blue}{3 \cdot \left(x \cdot 3\right) + 3 \cdot \left(-4\right)}, 3\right) \]
    10. *-commutative99.9%

      \[\leadsto \mathsf{fma}\left(x, \color{blue}{\left(x \cdot 3\right) \cdot 3} + 3 \cdot \left(-4\right), 3\right) \]
    11. associate-*l*99.9%

      \[\leadsto \mathsf{fma}\left(x, \color{blue}{x \cdot \left(3 \cdot 3\right)} + 3 \cdot \left(-4\right), 3\right) \]
    12. fma-def99.9%

      \[\leadsto \mathsf{fma}\left(x, \color{blue}{\mathsf{fma}\left(x, 3 \cdot 3, 3 \cdot \left(-4\right)\right)}, 3\right) \]
    13. metadata-eval99.9%

      \[\leadsto \mathsf{fma}\left(x, \mathsf{fma}\left(x, \color{blue}{9}, 3 \cdot \left(-4\right)\right), 3\right) \]
    14. metadata-eval99.9%

      \[\leadsto \mathsf{fma}\left(x, \mathsf{fma}\left(x, 9, 3 \cdot \color{blue}{-4}\right), 3\right) \]
    15. metadata-eval99.9%

      \[\leadsto \mathsf{fma}\left(x, \mathsf{fma}\left(x, 9, \color{blue}{-12}\right), 3\right) \]
  3. Simplified99.9%

    \[\leadsto \color{blue}{\mathsf{fma}\left(x, \mathsf{fma}\left(x, 9, -12\right), 3\right)} \]
  4. Taylor expanded in x around 0 52.3%

    \[\leadsto \color{blue}{3} \]
  5. Final simplification52.3%

    \[\leadsto 3 \]

Developer target: 99.8% accurate, 1.2× speedup?

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

\\
3 + \left(\left(9 \cdot x\right) \cdot x - 12 \cdot x\right)
\end{array}

Reproduce

?
herbie shell --seed 2023238 
(FPCore (x)
  :name "Diagrams.Tangent:$catParam from diagrams-lib-1.3.0.3, D"
  :precision binary64

  :herbie-target
  (+ 3.0 (- (* (* 9.0 x) x) (* 12.0 x)))

  (* 3.0 (+ (- (* (* x 3.0) x) (* x 4.0)) 1.0)))