Numeric.Integration.TanhSinh:everywhere from integration-0.2.1

Percentage Accurate: 94.0% → 99.9%
Time: 4.4s
Alternatives: 4
Speedup: 0.8×

Specification

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

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

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

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

Alternative 1: 99.9% accurate, 0.1× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y \cdot y \leq 10^{+23}:\\ \;\;\;\;\frac{\left(1 - {y}^{4}\right) \cdot x}{1 - y \cdot y}\\ \mathbf{else}:\\ \;\;\;\;y \cdot \left(y \cdot x\right)\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (if (<= (* y y) 1e+23)
   (/ (* (- 1.0 (pow y 4.0)) x) (- 1.0 (* y y)))
   (* y (* y x))))
double code(double x, double y) {
	double tmp;
	if ((y * y) <= 1e+23) {
		tmp = ((1.0 - pow(y, 4.0)) * x) / (1.0 - (y * y));
	} else {
		tmp = y * (y * x);
	}
	return tmp;
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: tmp
    if ((y * y) <= 1d+23) then
        tmp = ((1.0d0 - (y ** 4.0d0)) * x) / (1.0d0 - (y * y))
    else
        tmp = y * (y * x)
    end if
    code = tmp
end function
public static double code(double x, double y) {
	double tmp;
	if ((y * y) <= 1e+23) {
		tmp = ((1.0 - Math.pow(y, 4.0)) * x) / (1.0 - (y * y));
	} else {
		tmp = y * (y * x);
	}
	return tmp;
}
def code(x, y):
	tmp = 0
	if (y * y) <= 1e+23:
		tmp = ((1.0 - math.pow(y, 4.0)) * x) / (1.0 - (y * y))
	else:
		tmp = y * (y * x)
	return tmp
function code(x, y)
	tmp = 0.0
	if (Float64(y * y) <= 1e+23)
		tmp = Float64(Float64(Float64(1.0 - (y ^ 4.0)) * x) / Float64(1.0 - Float64(y * y)));
	else
		tmp = Float64(y * Float64(y * x));
	end
	return tmp
end
function tmp_2 = code(x, y)
	tmp = 0.0;
	if ((y * y) <= 1e+23)
		tmp = ((1.0 - (y ^ 4.0)) * x) / (1.0 - (y * y));
	else
		tmp = y * (y * x);
	end
	tmp_2 = tmp;
end
code[x_, y_] := If[LessEqual[N[(y * y), $MachinePrecision], 1e+23], N[(N[(N[(1.0 - N[Power[y, 4.0], $MachinePrecision]), $MachinePrecision] * x), $MachinePrecision] / N[(1.0 - N[(y * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(y * N[(y * x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;y \cdot y \leq 10^{+23}:\\
\;\;\;\;\frac{\left(1 - {y}^{4}\right) \cdot x}{1 - y \cdot y}\\

\mathbf{else}:\\
\;\;\;\;y \cdot \left(y \cdot x\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 y y) < 9.9999999999999992e22

    1. Initial program 100.0%

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

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

        \[\leadsto \color{blue}{\frac{1 \cdot 1 - \left(y \cdot y\right) \cdot \left(y \cdot y\right)}{1 - y \cdot y}} \cdot x \]
      3. associate-*l/100.0%

        \[\leadsto \color{blue}{\frac{\left(1 \cdot 1 - \left(y \cdot y\right) \cdot \left(y \cdot y\right)\right) \cdot x}{1 - y \cdot y}} \]
      4. metadata-eval100.0%

        \[\leadsto \frac{\left(\color{blue}{1} - \left(y \cdot y\right) \cdot \left(y \cdot y\right)\right) \cdot x}{1 - y \cdot y} \]
      5. pow2100.0%

        \[\leadsto \frac{\left(1 - \color{blue}{{y}^{2}} \cdot \left(y \cdot y\right)\right) \cdot x}{1 - y \cdot y} \]
      6. pow2100.0%

        \[\leadsto \frac{\left(1 - {y}^{2} \cdot \color{blue}{{y}^{2}}\right) \cdot x}{1 - y \cdot y} \]
      7. pow-prod-up100.0%

        \[\leadsto \frac{\left(1 - \color{blue}{{y}^{\left(2 + 2\right)}}\right) \cdot x}{1 - y \cdot y} \]
      8. metadata-eval100.0%

        \[\leadsto \frac{\left(1 - {y}^{\color{blue}{4}}\right) \cdot x}{1 - y \cdot y} \]
    3. Applied egg-rr100.0%

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

    if 9.9999999999999992e22 < (*.f64 y y)

    1. Initial program 87.7%

      \[x \cdot \left(1 + y \cdot y\right) \]
    2. Taylor expanded in y around inf 87.7%

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

        \[\leadsto \color{blue}{\left(y \cdot y\right)} \cdot x \]
      2. associate-*r*99.8%

        \[\leadsto \color{blue}{y \cdot \left(y \cdot x\right)} \]
    4. Simplified99.8%

      \[\leadsto \color{blue}{y \cdot \left(y \cdot x\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification99.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \cdot y \leq 10^{+23}:\\ \;\;\;\;\frac{\left(1 - {y}^{4}\right) \cdot x}{1 - y \cdot y}\\ \mathbf{else}:\\ \;\;\;\;y \cdot \left(y \cdot x\right)\\ \end{array} \]

Alternative 2: 99.9% accurate, 0.6× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;y \cdot y \leq 2 \cdot 10^{+272}:\\
\;\;\;\;x \cdot \left(y \cdot y + 1\right)\\

\mathbf{else}:\\
\;\;\;\;y \cdot \left(y \cdot x\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 y y) < 2.0000000000000001e272

    1. Initial program 99.9%

      \[x \cdot \left(1 + y \cdot y\right) \]

    if 2.0000000000000001e272 < (*.f64 y y)

    1. Initial program 79.5%

      \[x \cdot \left(1 + y \cdot y\right) \]
    2. Taylor expanded in y around inf 79.5%

      \[\leadsto \color{blue}{{y}^{2} \cdot x} \]
    3. Step-by-step derivation
      1. unpow279.5%

        \[\leadsto \color{blue}{\left(y \cdot y\right)} \cdot x \]
      2. associate-*r*99.8%

        \[\leadsto \color{blue}{y \cdot \left(y \cdot x\right)} \]
    4. Simplified99.8%

      \[\leadsto \color{blue}{y \cdot \left(y \cdot x\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification99.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \cdot y \leq 2 \cdot 10^{+272}:\\ \;\;\;\;x \cdot \left(y \cdot y + 1\right)\\ \mathbf{else}:\\ \;\;\;\;y \cdot \left(y \cdot x\right)\\ \end{array} \]

Alternative 3: 98.8% accurate, 0.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y \cdot y \leq 2 \cdot 10^{-7}:\\ \;\;\;\;x\\ \mathbf{else}:\\ \;\;\;\;y \cdot \left(y \cdot x\right)\\ \end{array} \end{array} \]
(FPCore (x y) :precision binary64 (if (<= (* y y) 2e-7) x (* y (* y x))))
double code(double x, double y) {
	double tmp;
	if ((y * y) <= 2e-7) {
		tmp = x;
	} else {
		tmp = y * (y * x);
	}
	return tmp;
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: tmp
    if ((y * y) <= 2d-7) then
        tmp = x
    else
        tmp = y * (y * x)
    end if
    code = tmp
end function
public static double code(double x, double y) {
	double tmp;
	if ((y * y) <= 2e-7) {
		tmp = x;
	} else {
		tmp = y * (y * x);
	}
	return tmp;
}
def code(x, y):
	tmp = 0
	if (y * y) <= 2e-7:
		tmp = x
	else:
		tmp = y * (y * x)
	return tmp
function code(x, y)
	tmp = 0.0
	if (Float64(y * y) <= 2e-7)
		tmp = x;
	else
		tmp = Float64(y * Float64(y * x));
	end
	return tmp
end
function tmp_2 = code(x, y)
	tmp = 0.0;
	if ((y * y) <= 2e-7)
		tmp = x;
	else
		tmp = y * (y * x);
	end
	tmp_2 = tmp;
end
code[x_, y_] := If[LessEqual[N[(y * y), $MachinePrecision], 2e-7], x, N[(y * N[(y * x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;y \cdot y \leq 2 \cdot 10^{-7}:\\
\;\;\;\;x\\

\mathbf{else}:\\
\;\;\;\;y \cdot \left(y \cdot x\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 y y) < 1.9999999999999999e-7

    1. Initial program 100.0%

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

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

    if 1.9999999999999999e-7 < (*.f64 y y)

    1. Initial program 87.9%

      \[x \cdot \left(1 + y \cdot y\right) \]
    2. Taylor expanded in y around inf 87.9%

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

        \[\leadsto \color{blue}{\left(y \cdot y\right)} \cdot x \]
      2. associate-*r*99.8%

        \[\leadsto \color{blue}{y \cdot \left(y \cdot x\right)} \]
    4. Simplified99.8%

      \[\leadsto \color{blue}{y \cdot \left(y \cdot x\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification99.7%

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \cdot y \leq 2 \cdot 10^{-7}:\\ \;\;\;\;x\\ \mathbf{else}:\\ \;\;\;\;y \cdot \left(y \cdot x\right)\\ \end{array} \]

Alternative 4: 52.6% accurate, 7.0× speedup?

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

\\
x
\end{array}
Derivation
  1. Initial program 93.3%

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

    \[\leadsto \color{blue}{x} \]
  3. Final simplification46.9%

    \[\leadsto x \]

Developer target: 99.9% accurate, 1.0× speedup?

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

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

Reproduce

?
herbie shell --seed 2023196 
(FPCore (x y)
  :name "Numeric.Integration.TanhSinh:everywhere from integration-0.2.1"
  :precision binary64

  :herbie-target
  (+ x (* (* x y) y))

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