Examples.Basics.BasicTests:f2 from sbv-4.4

Percentage Accurate: 94.2% → 100.0%
Time: 4.8s
Alternatives: 3
Speedup: 1.2×

Specification

?
\[\begin{array}{l} \\ x \cdot x - y \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(Float64(x * x) - Float64(y * y))
end
function tmp = code(x, y)
	tmp = (x * x) - (y * y);
end
code[x_, y_] := N[(N[(x * x), $MachinePrecision] - N[(y * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x \cdot x - y \cdot y
\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: 94.2% accurate, 1.0× speedup?

\[\begin{array}{l} \\ x \cdot x - y \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(Float64(x * x) - Float64(y * y))
end
function tmp = code(x, y)
	tmp = (x * x) - (y * y);
end
code[x_, y_] := N[(N[(x * x), $MachinePrecision] - N[(y * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x \cdot x - y \cdot y
\end{array}

Alternative 1: 100.0% accurate, 1.2× speedup?

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

\\
\left(y + x\right) \cdot \left(x - y\right)
\end{array}
Derivation
  1. Initial program 91.4%

    \[x \cdot x - y \cdot y \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. lift--.f64N/A

      \[\leadsto \color{blue}{x \cdot x - y \cdot y} \]
    2. lift-*.f64N/A

      \[\leadsto \color{blue}{x \cdot x} - y \cdot y \]
    3. lift-*.f64N/A

      \[\leadsto x \cdot x - \color{blue}{y \cdot y} \]
    4. difference-of-squaresN/A

      \[\leadsto \color{blue}{\left(x + y\right) \cdot \left(x - y\right)} \]
    5. *-commutativeN/A

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

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

      \[\leadsto \color{blue}{\left(x - y\right)} \cdot \left(x + y\right) \]
    8. +-commutativeN/A

      \[\leadsto \left(x - y\right) \cdot \color{blue}{\left(y + x\right)} \]
    9. lower-+.f64100.0

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

    \[\leadsto \color{blue}{\left(x - y\right) \cdot \left(y + x\right)} \]
  5. Final simplification100.0%

    \[\leadsto \left(y + x\right) \cdot \left(x - y\right) \]
  6. Add Preprocessing

Alternative 2: 96.2% accurate, 0.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := x \cdot x - y \cdot y\\ t_1 := \left(-y\right) \cdot y\\ \mathbf{if}\;t\_0 \leq -4 \cdot 10^{-300}:\\ \;\;\;\;t\_1\\ \mathbf{elif}\;t\_0 \leq \infty:\\ \;\;\;\;x \cdot x\\ \mathbf{else}:\\ \;\;\;\;t\_1\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (let* ((t_0 (- (* x x) (* y y))) (t_1 (* (- y) y)))
   (if (<= t_0 -4e-300) t_1 (if (<= t_0 INFINITY) (* x x) t_1))))
double code(double x, double y) {
	double t_0 = (x * x) - (y * y);
	double t_1 = -y * y;
	double tmp;
	if (t_0 <= -4e-300) {
		tmp = t_1;
	} else if (t_0 <= ((double) INFINITY)) {
		tmp = x * x;
	} else {
		tmp = t_1;
	}
	return tmp;
}
public static double code(double x, double y) {
	double t_0 = (x * x) - (y * y);
	double t_1 = -y * y;
	double tmp;
	if (t_0 <= -4e-300) {
		tmp = t_1;
	} else if (t_0 <= Double.POSITIVE_INFINITY) {
		tmp = x * x;
	} else {
		tmp = t_1;
	}
	return tmp;
}
def code(x, y):
	t_0 = (x * x) - (y * y)
	t_1 = -y * y
	tmp = 0
	if t_0 <= -4e-300:
		tmp = t_1
	elif t_0 <= math.inf:
		tmp = x * x
	else:
		tmp = t_1
	return tmp
function code(x, y)
	t_0 = Float64(Float64(x * x) - Float64(y * y))
	t_1 = Float64(Float64(-y) * y)
	tmp = 0.0
	if (t_0 <= -4e-300)
		tmp = t_1;
	elseif (t_0 <= Inf)
		tmp = Float64(x * x);
	else
		tmp = t_1;
	end
	return tmp
end
function tmp_2 = code(x, y)
	t_0 = (x * x) - (y * y);
	t_1 = -y * y;
	tmp = 0.0;
	if (t_0 <= -4e-300)
		tmp = t_1;
	elseif (t_0 <= Inf)
		tmp = x * x;
	else
		tmp = t_1;
	end
	tmp_2 = tmp;
end
code[x_, y_] := Block[{t$95$0 = N[(N[(x * x), $MachinePrecision] - N[(y * y), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[((-y) * y), $MachinePrecision]}, If[LessEqual[t$95$0, -4e-300], t$95$1, If[LessEqual[t$95$0, Infinity], N[(x * x), $MachinePrecision], t$95$1]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := x \cdot x - y \cdot y\\
t_1 := \left(-y\right) \cdot y\\
\mathbf{if}\;t\_0 \leq -4 \cdot 10^{-300}:\\
\;\;\;\;t\_1\\

\mathbf{elif}\;t\_0 \leq \infty:\\
\;\;\;\;x \cdot x\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (-.f64 (*.f64 x x) (*.f64 y y)) < -4.0000000000000001e-300 or +inf.0 < (-.f64 (*.f64 x x) (*.f64 y y))

    1. Initial program 82.8%

      \[x \cdot x - y \cdot y \]
    2. Add Preprocessing
    3. Taylor expanded in y around inf

      \[\leadsto \color{blue}{-1 \cdot {y}^{2}} \]
    4. Step-by-step derivation
      1. mul-1-negN/A

        \[\leadsto \color{blue}{\mathsf{neg}\left({y}^{2}\right)} \]
      2. unpow2N/A

        \[\leadsto \mathsf{neg}\left(\color{blue}{y \cdot y}\right) \]
      3. distribute-lft-neg-inN/A

        \[\leadsto \color{blue}{\left(\mathsf{neg}\left(y\right)\right) \cdot y} \]
      4. lower-*.f64N/A

        \[\leadsto \color{blue}{\left(\mathsf{neg}\left(y\right)\right) \cdot y} \]
      5. lower-neg.f6493.6

        \[\leadsto \color{blue}{\left(-y\right)} \cdot y \]
    5. Applied rewrites93.6%

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

    if -4.0000000000000001e-300 < (-.f64 (*.f64 x x) (*.f64 y y)) < +inf.0

    1. Initial program 100.0%

      \[x \cdot x - y \cdot y \]
    2. Add Preprocessing
    3. Taylor expanded in y around 0

      \[\leadsto \color{blue}{{x}^{2}} \]
    4. Step-by-step derivation
      1. unpow2N/A

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

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

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

Alternative 3: 53.5% accurate, 2.3× speedup?

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

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

    \[x \cdot x - y \cdot y \]
  2. Add Preprocessing
  3. Taylor expanded in y around 0

    \[\leadsto \color{blue}{{x}^{2}} \]
  4. Step-by-step derivation
    1. unpow2N/A

      \[\leadsto \color{blue}{x \cdot x} \]
    2. lower-*.f6453.6

      \[\leadsto \color{blue}{x \cdot x} \]
  5. Applied rewrites53.6%

    \[\leadsto \color{blue}{x \cdot x} \]
  6. Add Preprocessing

Reproduce

?
herbie shell --seed 2024277 
(FPCore (x y)
  :name "Examples.Basics.BasicTests:f2 from sbv-4.4"
  :precision binary64
  (- (* x x) (* y y)))