
(FPCore (x y) :precision binary64 (+ (+ (* x x) (* (* x 2.0) y)) (* y y)))
double code(double x, double y) {
return ((x * x) + ((x * 2.0) * y)) + (y * y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = ((x * x) + ((x * 2.0d0) * y)) + (y * y)
end function
public static double code(double x, double y) {
return ((x * x) + ((x * 2.0) * y)) + (y * y);
}
def code(x, y): return ((x * x) + ((x * 2.0) * y)) + (y * y)
function code(x, y) return Float64(Float64(Float64(x * x) + Float64(Float64(x * 2.0) * y)) + Float64(y * y)) end
function tmp = code(x, y) tmp = ((x * x) + ((x * 2.0) * y)) + (y * y); end
code[x_, y_] := N[(N[(N[(x * x), $MachinePrecision] + N[(N[(x * 2.0), $MachinePrecision] * y), $MachinePrecision]), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(x \cdot x + \left(x \cdot 2\right) \cdot y\right) + y \cdot y
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 7 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y) :precision binary64 (+ (+ (* x x) (* (* x 2.0) y)) (* y y)))
double code(double x, double y) {
return ((x * x) + ((x * 2.0) * y)) + (y * y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = ((x * x) + ((x * 2.0d0) * y)) + (y * y)
end function
public static double code(double x, double y) {
return ((x * x) + ((x * 2.0) * y)) + (y * y);
}
def code(x, y): return ((x * x) + ((x * 2.0) * y)) + (y * y)
function code(x, y) return Float64(Float64(Float64(x * x) + Float64(Float64(x * 2.0) * y)) + Float64(y * y)) end
function tmp = code(x, y) tmp = ((x * x) + ((x * 2.0) * y)) + (y * y); end
code[x_, y_] := N[(N[(N[(x * x), $MachinePrecision] + N[(N[(x * 2.0), $MachinePrecision] * y), $MachinePrecision]), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(x \cdot x + \left(x \cdot 2\right) \cdot y\right) + y \cdot y
\end{array}
(FPCore (x y) :precision binary64 (fma y (fma x 2.0 y) (* x x)))
double code(double x, double y) {
return fma(y, fma(x, 2.0, y), (x * x));
}
function code(x, y) return fma(y, fma(x, 2.0, y), Float64(x * x)) end
code[x_, y_] := N[(y * N[(x * 2.0 + y), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(y, \mathsf{fma}\left(x, 2, y\right), x \cdot x\right)
\end{array}
Initial program 94.5%
+-commutative94.5%
associate-+l+94.5%
+-commutative94.5%
associate-+l+94.5%
distribute-rgt-out96.9%
fma-define100.0%
fma-define100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (x y) :precision binary64 (let* ((t_0 (+ (+ (* x x) (* y (* x 2.0))) (* y y)))) (if (<= t_0 INFINITY) t_0 (pow x 2.0))))
double code(double x, double y) {
double t_0 = ((x * x) + (y * (x * 2.0))) + (y * y);
double tmp;
if (t_0 <= ((double) INFINITY)) {
tmp = t_0;
} else {
tmp = pow(x, 2.0);
}
return tmp;
}
public static double code(double x, double y) {
double t_0 = ((x * x) + (y * (x * 2.0))) + (y * y);
double tmp;
if (t_0 <= Double.POSITIVE_INFINITY) {
tmp = t_0;
} else {
tmp = Math.pow(x, 2.0);
}
return tmp;
}
def code(x, y): t_0 = ((x * x) + (y * (x * 2.0))) + (y * y) tmp = 0 if t_0 <= math.inf: tmp = t_0 else: tmp = math.pow(x, 2.0) return tmp
function code(x, y) t_0 = Float64(Float64(Float64(x * x) + Float64(y * Float64(x * 2.0))) + Float64(y * y)) tmp = 0.0 if (t_0 <= Inf) tmp = t_0; else tmp = x ^ 2.0; end return tmp end
function tmp_2 = code(x, y) t_0 = ((x * x) + (y * (x * 2.0))) + (y * y); tmp = 0.0; if (t_0 <= Inf) tmp = t_0; else tmp = x ^ 2.0; end tmp_2 = tmp; end
code[x_, y_] := Block[{t$95$0 = N[(N[(N[(x * x), $MachinePrecision] + N[(y * N[(x * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, Infinity], t$95$0, N[Power[x, 2.0], $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(x \cdot x + y \cdot \left(x \cdot 2\right)\right) + y \cdot y\\
\mathbf{if}\;t\_0 \leq \infty:\\
\;\;\;\;t\_0\\
\mathbf{else}:\\
\;\;\;\;{x}^{2}\\
\end{array}
\end{array}
if (+.f64 (+.f64 (*.f64 x x) (*.f64 (*.f64 x 2) y)) (*.f64 y y)) < +inf.0Initial program 100.0%
if +inf.0 < (+.f64 (+.f64 (*.f64 x x) (*.f64 (*.f64 x 2) y)) (*.f64 y y)) Initial program 0.0%
associate-+l+0.0%
associate-*l*0.0%
*-commutative0.0%
*-commutative0.0%
+-commutative0.0%
fma-define0.0%
*-commutative0.0%
*-commutative0.0%
Simplified0.0%
Taylor expanded in x around inf 80.4%
Final simplification98.9%
(FPCore (x y) :precision binary64 (if (<= x -3.6e+188) (* x (+ x (* y 2.0))) (+ (* x x) (* y (fma x 2.0 y)))))
double code(double x, double y) {
double tmp;
if (x <= -3.6e+188) {
tmp = x * (x + (y * 2.0));
} else {
tmp = (x * x) + (y * fma(x, 2.0, y));
}
return tmp;
}
function code(x, y) tmp = 0.0 if (x <= -3.6e+188) tmp = Float64(x * Float64(x + Float64(y * 2.0))); else tmp = Float64(Float64(x * x) + Float64(y * fma(x, 2.0, y))); end return tmp end
code[x_, y_] := If[LessEqual[x, -3.6e+188], N[(x * N[(x + N[(y * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(x * x), $MachinePrecision] + N[(y * N[(x * 2.0 + y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -3.6 \cdot 10^{+188}:\\
\;\;\;\;x \cdot \left(x + y \cdot 2\right)\\
\mathbf{else}:\\
\;\;\;\;x \cdot x + y \cdot \mathsf{fma}\left(x, 2, y\right)\\
\end{array}
\end{array}
if x < -3.60000000000000021e188Initial program 75.9%
associate-+l+75.9%
associate-*l*75.9%
*-commutative75.9%
*-commutative75.9%
+-commutative75.9%
fma-define75.9%
*-commutative75.9%
*-commutative75.9%
Simplified75.9%
Taylor expanded in y around 0 75.9%
*-commutative75.9%
*-commutative75.9%
associate-*r*75.9%
*-commutative75.9%
Simplified75.9%
+-commutative75.9%
associate-*r*75.9%
distribute-rgt-out100.0%
Applied egg-rr100.0%
if -3.60000000000000021e188 < x Initial program 96.9%
associate-+l+96.9%
associate-*l*96.9%
*-commutative96.9%
*-commutative96.9%
+-commutative96.9%
fma-define96.9%
*-commutative96.9%
*-commutative96.9%
Simplified96.9%
fma-undefine96.9%
associate-*r*96.9%
distribute-rgt-out99.5%
+-commutative99.5%
fma-undefine99.5%
*-commutative99.5%
Applied egg-rr99.5%
Final simplification99.6%
(FPCore (x y) :precision binary64 (fma y y (* x (+ x (* y 2.0)))))
double code(double x, double y) {
return fma(y, y, (x * (x + (y * 2.0))));
}
function code(x, y) return fma(y, y, Float64(x * Float64(x + Float64(y * 2.0)))) end
code[x_, y_] := N[(y * y + N[(x * N[(x + N[(y * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(y, y, x \cdot \left(x + y \cdot 2\right)\right)
\end{array}
Initial program 94.5%
+-commutative94.5%
fma-define94.5%
associate-*l*94.5%
*-commutative94.5%
distribute-lft-out97.7%
*-commutative97.7%
Simplified97.7%
Final simplification97.7%
(FPCore (x y) :precision binary64 (let* ((t_0 (+ (+ (* x x) (* y (* x 2.0))) (* y y)))) (if (<= t_0 INFINITY) t_0 (* x (+ x (* y 2.0))))))
double code(double x, double y) {
double t_0 = ((x * x) + (y * (x * 2.0))) + (y * y);
double tmp;
if (t_0 <= ((double) INFINITY)) {
tmp = t_0;
} else {
tmp = x * (x + (y * 2.0));
}
return tmp;
}
public static double code(double x, double y) {
double t_0 = ((x * x) + (y * (x * 2.0))) + (y * y);
double tmp;
if (t_0 <= Double.POSITIVE_INFINITY) {
tmp = t_0;
} else {
tmp = x * (x + (y * 2.0));
}
return tmp;
}
def code(x, y): t_0 = ((x * x) + (y * (x * 2.0))) + (y * y) tmp = 0 if t_0 <= math.inf: tmp = t_0 else: tmp = x * (x + (y * 2.0)) return tmp
function code(x, y) t_0 = Float64(Float64(Float64(x * x) + Float64(y * Float64(x * 2.0))) + Float64(y * y)) tmp = 0.0 if (t_0 <= Inf) tmp = t_0; else tmp = Float64(x * Float64(x + Float64(y * 2.0))); end return tmp end
function tmp_2 = code(x, y) t_0 = ((x * x) + (y * (x * 2.0))) + (y * y); tmp = 0.0; if (t_0 <= Inf) tmp = t_0; else tmp = x * (x + (y * 2.0)); end tmp_2 = tmp; end
code[x_, y_] := Block[{t$95$0 = N[(N[(N[(x * x), $MachinePrecision] + N[(y * N[(x * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, Infinity], t$95$0, N[(x * N[(x + N[(y * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(x \cdot x + y \cdot \left(x \cdot 2\right)\right) + y \cdot y\\
\mathbf{if}\;t\_0 \leq \infty:\\
\;\;\;\;t\_0\\
\mathbf{else}:\\
\;\;\;\;x \cdot \left(x + y \cdot 2\right)\\
\end{array}
\end{array}
if (+.f64 (+.f64 (*.f64 x x) (*.f64 (*.f64 x 2) y)) (*.f64 y y)) < +inf.0Initial program 100.0%
if +inf.0 < (+.f64 (+.f64 (*.f64 x x) (*.f64 (*.f64 x 2) y)) (*.f64 y y)) Initial program 0.0%
associate-+l+0.0%
associate-*l*0.0%
*-commutative0.0%
*-commutative0.0%
+-commutative0.0%
fma-define0.0%
*-commutative0.0%
*-commutative0.0%
Simplified0.0%
Taylor expanded in y around 0 0.0%
*-commutative0.0%
*-commutative0.0%
associate-*r*0.0%
*-commutative0.0%
Simplified0.0%
+-commutative0.0%
associate-*r*0.0%
distribute-rgt-out57.1%
Applied egg-rr57.1%
Final simplification97.7%
(FPCore (x y) :precision binary64 (* x (+ x (* y 2.0))))
double code(double x, double y) {
return x * (x + (y * 2.0));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = x * (x + (y * 2.0d0))
end function
public static double code(double x, double y) {
return x * (x + (y * 2.0));
}
def code(x, y): return x * (x + (y * 2.0))
function code(x, y) return Float64(x * Float64(x + Float64(y * 2.0))) end
function tmp = code(x, y) tmp = x * (x + (y * 2.0)); end
code[x_, y_] := N[(x * N[(x + N[(y * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot \left(x + y \cdot 2\right)
\end{array}
Initial program 94.5%
associate-+l+94.5%
associate-*l*94.5%
*-commutative94.5%
*-commutative94.5%
+-commutative94.5%
fma-define94.5%
*-commutative94.5%
*-commutative94.5%
Simplified94.5%
Taylor expanded in y around 0 53.1%
*-commutative53.1%
*-commutative53.1%
associate-*r*53.1%
*-commutative53.1%
Simplified53.1%
+-commutative53.1%
associate-*r*53.1%
distribute-rgt-out56.2%
Applied egg-rr56.2%
Final simplification56.2%
(FPCore (x y) :precision binary64 (* x (* y 2.0)))
double code(double x, double y) {
return x * (y * 2.0);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = x * (y * 2.0d0)
end function
public static double code(double x, double y) {
return x * (y * 2.0);
}
def code(x, y): return x * (y * 2.0)
function code(x, y) return Float64(x * Float64(y * 2.0)) end
function tmp = code(x, y) tmp = x * (y * 2.0); end
code[x_, y_] := N[(x * N[(y * 2.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot \left(y \cdot 2\right)
\end{array}
Initial program 94.5%
associate-+l+94.5%
associate-*l*94.5%
*-commutative94.5%
*-commutative94.5%
+-commutative94.5%
fma-define94.5%
*-commutative94.5%
*-commutative94.5%
Simplified94.5%
Taylor expanded in y around 0 53.1%
*-commutative53.1%
*-commutative53.1%
associate-*r*53.1%
*-commutative53.1%
Simplified53.1%
Taylor expanded in x around 0 11.1%
*-commutative11.1%
associate-*r*11.1%
*-commutative11.1%
Simplified11.1%
Final simplification11.1%
(FPCore (x y) :precision binary64 (+ (* x x) (+ (* y y) (* (* x y) 2.0))))
double code(double x, double y) {
return (x * x) + ((y * y) + ((x * y) * 2.0));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (x * x) + ((y * y) + ((x * y) * 2.0d0))
end function
public static double code(double x, double y) {
return (x * x) + ((y * y) + ((x * y) * 2.0));
}
def code(x, y): return (x * x) + ((y * y) + ((x * y) * 2.0))
function code(x, y) return Float64(Float64(x * x) + Float64(Float64(y * y) + Float64(Float64(x * y) * 2.0))) end
function tmp = code(x, y) tmp = (x * x) + ((y * y) + ((x * y) * 2.0)); end
code[x_, y_] := N[(N[(x * x), $MachinePrecision] + N[(N[(y * y), $MachinePrecision] + N[(N[(x * y), $MachinePrecision] * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot x + \left(y \cdot y + \left(x \cdot y\right) \cdot 2\right)
\end{array}
herbie shell --seed 2024046
(FPCore (x y)
:name "Examples.Basics.ProofTests:f4 from sbv-4.4"
:precision binary64
:alt
(+ (* x x) (+ (* y y) (* (* x y) 2.0)))
(+ (+ (* x x) (* (* x 2.0) y)) (* y y)))