
(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}
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (if (<= (+ (* y y) (+ (* x x) (* y (* x 2.0)))) 1e+260) (+ (* x x) (* y (+ y (* x 2.0)))) (+ (* y y) (* x x))))
assert(x < y);
double code(double x, double y) {
double tmp;
if (((y * y) + ((x * x) + (y * (x * 2.0)))) <= 1e+260) {
tmp = (x * x) + (y * (y + (x * 2.0)));
} else {
tmp = (y * y) + (x * x);
}
return tmp;
}
NOTE: x and y should be sorted in increasing order before calling this function.
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (((y * y) + ((x * x) + (y * (x * 2.0d0)))) <= 1d+260) then
tmp = (x * x) + (y * (y + (x * 2.0d0)))
else
tmp = (y * y) + (x * x)
end if
code = tmp
end function
assert x < y;
public static double code(double x, double y) {
double tmp;
if (((y * y) + ((x * x) + (y * (x * 2.0)))) <= 1e+260) {
tmp = (x * x) + (y * (y + (x * 2.0)));
} else {
tmp = (y * y) + (x * x);
}
return tmp;
}
[x, y] = sort([x, y]) def code(x, y): tmp = 0 if ((y * y) + ((x * x) + (y * (x * 2.0)))) <= 1e+260: tmp = (x * x) + (y * (y + (x * 2.0))) else: tmp = (y * y) + (x * x) return tmp
x, y = sort([x, y]) function code(x, y) tmp = 0.0 if (Float64(Float64(y * y) + Float64(Float64(x * x) + Float64(y * Float64(x * 2.0)))) <= 1e+260) tmp = Float64(Float64(x * x) + Float64(y * Float64(y + Float64(x * 2.0)))); else tmp = Float64(Float64(y * y) + Float64(x * x)); end return tmp end
x, y = num2cell(sort([x, y])){:}
function tmp_2 = code(x, y)
tmp = 0.0;
if (((y * y) + ((x * x) + (y * (x * 2.0)))) <= 1e+260)
tmp = (x * x) + (y * (y + (x * 2.0)));
else
tmp = (y * y) + (x * x);
end
tmp_2 = tmp;
end
NOTE: x and y should be sorted in increasing order before calling this function. code[x_, y_] := If[LessEqual[N[(N[(y * y), $MachinePrecision] + N[(N[(x * x), $MachinePrecision] + N[(y * N[(x * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 1e+260], N[(N[(x * x), $MachinePrecision] + N[(y * N[(y + N[(x * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(y * y), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
\begin{array}{l}
\mathbf{if}\;y \cdot y + \left(x \cdot x + y \cdot \left(x \cdot 2\right)\right) \leq 10^{+260}:\\
\;\;\;\;x \cdot x + y \cdot \left(y + x \cdot 2\right)\\
\mathbf{else}:\\
\;\;\;\;y \cdot y + x \cdot x\\
\end{array}
\end{array}
if (+.f64 (+.f64 (*.f64 x x) (*.f64 (*.f64 x #s(literal 2 binary64)) y)) (*.f64 y y)) < 1.00000000000000007e260Initial program 99.8%
associate-+l+99.8%
distribute-rgt-out99.8%
*-commutative99.8%
remove-double-neg99.8%
distribute-rgt-neg-out99.8%
unsub-neg99.8%
fmm-undef99.8%
distribute-rgt-neg-out99.8%
remove-double-neg99.8%
*-commutative99.8%
Simplified99.8%
Taylor expanded in y around 0 99.8%
unpow299.8%
Applied egg-rr99.8%
if 1.00000000000000007e260 < (+.f64 (+.f64 (*.f64 x x) (*.f64 (*.f64 x #s(literal 2 binary64)) y)) (*.f64 y y)) Initial program 83.5%
associate-+l+83.5%
distribute-rgt-out92.9%
*-commutative92.9%
remove-double-neg92.9%
distribute-rgt-neg-out92.9%
unsub-neg92.9%
fmm-undef92.9%
distribute-rgt-neg-out92.9%
remove-double-neg92.9%
*-commutative92.9%
Simplified92.9%
Taylor expanded in y around 0 92.9%
unpow292.9%
Applied egg-rr92.9%
Taylor expanded in y around inf 100.0%
Final simplification99.9%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (fma x (+ x (* 2.0 y)) (* y y)))
assert(x < y);
double code(double x, double y) {
return fma(x, (x + (2.0 * y)), (y * y));
}
x, y = sort([x, y]) function code(x, y) return fma(x, Float64(x + Float64(2.0 * y)), Float64(y * y)) end
NOTE: x and y should be sorted in increasing order before calling this function. code[x_, y_] := N[(x * N[(x + N[(2.0 * y), $MachinePrecision]), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
\mathsf{fma}\left(x, x + 2 \cdot y, y \cdot y\right)
\end{array}
Initial program 91.7%
associate-*l*91.7%
*-commutative91.7%
distribute-lft-out95.2%
fma-define99.9%
+-commutative99.9%
*-commutative99.9%
fma-define99.9%
Simplified99.9%
fma-undefine99.9%
Applied egg-rr99.9%
Final simplification99.9%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (if (<= y 2.25e+114) (+ (* y y) (* x (+ x (* 2.0 y)))) (* y y)))
assert(x < y);
double code(double x, double y) {
double tmp;
if (y <= 2.25e+114) {
tmp = (y * y) + (x * (x + (2.0 * y)));
} else {
tmp = y * y;
}
return tmp;
}
NOTE: x and y should be sorted in increasing order before calling this function.
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (y <= 2.25d+114) then
tmp = (y * y) + (x * (x + (2.0d0 * y)))
else
tmp = y * y
end if
code = tmp
end function
assert x < y;
public static double code(double x, double y) {
double tmp;
if (y <= 2.25e+114) {
tmp = (y * y) + (x * (x + (2.0 * y)));
} else {
tmp = y * y;
}
return tmp;
}
[x, y] = sort([x, y]) def code(x, y): tmp = 0 if y <= 2.25e+114: tmp = (y * y) + (x * (x + (2.0 * y))) else: tmp = y * y return tmp
x, y = sort([x, y]) function code(x, y) tmp = 0.0 if (y <= 2.25e+114) tmp = Float64(Float64(y * y) + Float64(x * Float64(x + Float64(2.0 * y)))); else tmp = Float64(y * y); end return tmp end
x, y = num2cell(sort([x, y])){:}
function tmp_2 = code(x, y)
tmp = 0.0;
if (y <= 2.25e+114)
tmp = (y * y) + (x * (x + (2.0 * y)));
else
tmp = y * y;
end
tmp_2 = tmp;
end
NOTE: x and y should be sorted in increasing order before calling this function. code[x_, y_] := If[LessEqual[y, 2.25e+114], N[(N[(y * y), $MachinePrecision] + N[(x * N[(x + N[(2.0 * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(y * y), $MachinePrecision]]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
\begin{array}{l}
\mathbf{if}\;y \leq 2.25 \cdot 10^{+114}:\\
\;\;\;\;y \cdot y + x \cdot \left(x + 2 \cdot y\right)\\
\mathbf{else}:\\
\;\;\;\;y \cdot y\\
\end{array}
\end{array}
if y < 2.25e114Initial program 95.0%
Taylor expanded in x around 0 97.9%
if 2.25e114 < y Initial program 78.4%
Taylor expanded in x around 0 84.3%
Taylor expanded in x around 0 76.7%
Taylor expanded in y around 0 92.4%
Taylor expanded in y around inf 98.2%
Final simplification98.0%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (if (<= y 8e-88) (* y (+ y (* x 2.0))) (* y y)))
assert(x < y);
double code(double x, double y) {
double tmp;
if (y <= 8e-88) {
tmp = y * (y + (x * 2.0));
} else {
tmp = y * y;
}
return tmp;
}
NOTE: x and y should be sorted in increasing order before calling this function.
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (y <= 8d-88) then
tmp = y * (y + (x * 2.0d0))
else
tmp = y * y
end if
code = tmp
end function
assert x < y;
public static double code(double x, double y) {
double tmp;
if (y <= 8e-88) {
tmp = y * (y + (x * 2.0));
} else {
tmp = y * y;
}
return tmp;
}
[x, y] = sort([x, y]) def code(x, y): tmp = 0 if y <= 8e-88: tmp = y * (y + (x * 2.0)) else: tmp = y * y return tmp
x, y = sort([x, y]) function code(x, y) tmp = 0.0 if (y <= 8e-88) tmp = Float64(y * Float64(y + Float64(x * 2.0))); else tmp = Float64(y * y); end return tmp end
x, y = num2cell(sort([x, y])){:}
function tmp_2 = code(x, y)
tmp = 0.0;
if (y <= 8e-88)
tmp = y * (y + (x * 2.0));
else
tmp = y * y;
end
tmp_2 = tmp;
end
NOTE: x and y should be sorted in increasing order before calling this function. code[x_, y_] := If[LessEqual[y, 8e-88], N[(y * N[(y + N[(x * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(y * y), $MachinePrecision]]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
\begin{array}{l}
\mathbf{if}\;y \leq 8 \cdot 10^{-88}:\\
\;\;\;\;y \cdot \left(y + x \cdot 2\right)\\
\mathbf{else}:\\
\;\;\;\;y \cdot y\\
\end{array}
\end{array}
if y < 7.99999999999999947e-88Initial program 93.6%
Taylor expanded in x around 0 97.4%
Taylor expanded in x around 0 44.7%
Taylor expanded in y around 0 47.2%
if 7.99999999999999947e-88 < y Initial program 88.4%
Taylor expanded in x around 0 91.6%
Taylor expanded in x around 0 70.3%
Taylor expanded in y around 0 78.7%
Taylor expanded in y around inf 79.9%
Final simplification59.3%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (if (<= y -1.05e-301) (* 2.0 (* x y)) (* y y)))
assert(x < y);
double code(double x, double y) {
double tmp;
if (y <= -1.05e-301) {
tmp = 2.0 * (x * y);
} else {
tmp = y * y;
}
return tmp;
}
NOTE: x and y should be sorted in increasing order before calling this function.
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (y <= (-1.05d-301)) then
tmp = 2.0d0 * (x * y)
else
tmp = y * y
end if
code = tmp
end function
assert x < y;
public static double code(double x, double y) {
double tmp;
if (y <= -1.05e-301) {
tmp = 2.0 * (x * y);
} else {
tmp = y * y;
}
return tmp;
}
[x, y] = sort([x, y]) def code(x, y): tmp = 0 if y <= -1.05e-301: tmp = 2.0 * (x * y) else: tmp = y * y return tmp
x, y = sort([x, y]) function code(x, y) tmp = 0.0 if (y <= -1.05e-301) tmp = Float64(2.0 * Float64(x * y)); else tmp = Float64(y * y); end return tmp end
x, y = num2cell(sort([x, y])){:}
function tmp_2 = code(x, y)
tmp = 0.0;
if (y <= -1.05e-301)
tmp = 2.0 * (x * y);
else
tmp = y * y;
end
tmp_2 = tmp;
end
NOTE: x and y should be sorted in increasing order before calling this function. code[x_, y_] := If[LessEqual[y, -1.05e-301], N[(2.0 * N[(x * y), $MachinePrecision]), $MachinePrecision], N[(y * y), $MachinePrecision]]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
\begin{array}{l}
\mathbf{if}\;y \leq -1.05 \cdot 10^{-301}:\\
\;\;\;\;2 \cdot \left(x \cdot y\right)\\
\mathbf{else}:\\
\;\;\;\;y \cdot y\\
\end{array}
\end{array}
if y < -1.0499999999999999e-301Initial program 90.9%
Taylor expanded in x around 0 96.2%
Taylor expanded in x around 0 52.6%
Taylor expanded in x around inf 13.8%
*-commutative13.8%
Simplified13.8%
if -1.0499999999999999e-301 < y Initial program 92.4%
Taylor expanded in x around 0 94.4%
Taylor expanded in x around 0 55.5%
Taylor expanded in y around 0 61.0%
Taylor expanded in y around inf 61.6%
Final simplification40.7%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (+ (* y y) (* x x)))
assert(x < y);
double code(double x, double y) {
return (y * y) + (x * x);
}
NOTE: x and y should be sorted in increasing order before calling this function.
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (y * y) + (x * x)
end function
assert x < y;
public static double code(double x, double y) {
return (y * y) + (x * x);
}
[x, y] = sort([x, y]) def code(x, y): return (y * y) + (x * x)
x, y = sort([x, y]) function code(x, y) return Float64(Float64(y * y) + Float64(x * x)) end
x, y = num2cell(sort([x, y])){:}
function tmp = code(x, y)
tmp = (y * y) + (x * x);
end
NOTE: x and y should be sorted in increasing order before calling this function. code[x_, y_] := N[(N[(y * y), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
y \cdot y + x \cdot x
\end{array}
Initial program 91.7%
associate-+l+91.7%
distribute-rgt-out96.4%
*-commutative96.4%
remove-double-neg96.4%
distribute-rgt-neg-out96.4%
unsub-neg96.4%
fmm-undef96.4%
distribute-rgt-neg-out96.4%
remove-double-neg96.4%
*-commutative96.4%
Simplified96.4%
Taylor expanded in y around 0 96.4%
unpow296.4%
Applied egg-rr96.4%
Taylor expanded in y around inf 98.3%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (* y y))
assert(x < y);
double code(double x, double y) {
return y * y;
}
NOTE: x and y should be sorted in increasing order before calling this function.
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = y * y
end function
assert x < y;
public static double code(double x, double y) {
return y * y;
}
[x, y] = sort([x, y]) def code(x, y): return y * y
x, y = sort([x, y]) function code(x, y) return Float64(y * y) end
x, y = num2cell(sort([x, y])){:}
function tmp = code(x, y)
tmp = y * y;
end
NOTE: x and y should be sorted in increasing order before calling this function. code[x_, y_] := N[(y * y), $MachinePrecision]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
y \cdot y
\end{array}
Initial program 91.7%
Taylor expanded in x around 0 95.2%
Taylor expanded in x around 0 54.2%
Taylor expanded in y around 0 58.9%
Taylor expanded in y around inf 59.0%
(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 2024172
(FPCore (x y)
:name "Examples.Basics.ProofTests:f4 from sbv-4.4"
:precision binary64
:alt
(! :herbie-platform default (+ (* x x) (+ (* y y) (* (* x y) 2))))
(+ (+ (* x x) (* (* x 2.0) y)) (* y y)))