
(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)))) 5e+301) (+ (* x x) (* y (+ y (* x 2.0)))) (* x (+ x (* y (+ 2.0 (/ y x)))))))
assert(x < y);
double code(double x, double y) {
double tmp;
if (((y * y) + ((x * x) + (y * (x * 2.0)))) <= 5e+301) {
tmp = (x * x) + (y * (y + (x * 2.0)));
} else {
tmp = x * (x + (y * (2.0 + (y / 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)))) <= 5d+301) then
tmp = (x * x) + (y * (y + (x * 2.0d0)))
else
tmp = x * (x + (y * (2.0d0 + (y / 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)))) <= 5e+301) {
tmp = (x * x) + (y * (y + (x * 2.0)));
} else {
tmp = x * (x + (y * (2.0 + (y / x))));
}
return tmp;
}
[x, y] = sort([x, y]) def code(x, y): tmp = 0 if ((y * y) + ((x * x) + (y * (x * 2.0)))) <= 5e+301: tmp = (x * x) + (y * (y + (x * 2.0))) else: tmp = x * (x + (y * (2.0 + (y / 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)))) <= 5e+301) tmp = Float64(Float64(x * x) + Float64(y * Float64(y + Float64(x * 2.0)))); else tmp = Float64(x * Float64(x + Float64(y * Float64(2.0 + Float64(y / 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)))) <= 5e+301)
tmp = (x * x) + (y * (y + (x * 2.0)));
else
tmp = x * (x + (y * (2.0 + (y / 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], 5e+301], N[(N[(x * x), $MachinePrecision] + N[(y * N[(y + N[(x * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(x * N[(x + N[(y * N[(2.0 + N[(y / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $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 5 \cdot 10^{+301}:\\
\;\;\;\;x \cdot x + y \cdot \left(y + x \cdot 2\right)\\
\mathbf{else}:\\
\;\;\;\;x \cdot \left(x + y \cdot \left(2 + \frac{y}{x}\right)\right)\\
\end{array}
\end{array}
if (+.f64 (+.f64 (*.f64 x x) (*.f64 (*.f64 x #s(literal 2 binary64)) y)) (*.f64 y y)) < 5.0000000000000004e301Initial program 99.9%
associate-+l+99.9%
+-commutative99.9%
fma-define100.0%
associate-*l*100.0%
Simplified100.0%
Taylor expanded in y around 0 100.0%
if 5.0000000000000004e301 < (+.f64 (+.f64 (*.f64 x x) (*.f64 (*.f64 x #s(literal 2 binary64)) y)) (*.f64 y y)) Initial program 84.8%
associate-+l+84.8%
+-commutative84.8%
fma-define84.8%
associate-*l*84.8%
Simplified84.8%
Taylor expanded in x around inf 96.4%
*-commutative96.4%
unpow296.4%
associate-/l*92.0%
distribute-lft-out92.0%
Simplified92.0%
distribute-lft-out100.0%
Applied egg-rr100.0%
Final simplification100.0%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (fma x (fma 2.0 y x) (* y y)))
assert(x < y);
double code(double x, double y) {
return fma(x, fma(2.0, y, x), (y * y));
}
x, y = sort([x, y]) function code(x, y) return fma(x, fma(2.0, y, x), Float64(y * y)) end
NOTE: x and y should be sorted in increasing order before calling this function. code[x_, y_] := N[(x * N[(2.0 * y + x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
\mathsf{fma}\left(x, \mathsf{fma}\left(2, y, x\right), y \cdot y\right)
\end{array}
Initial program 93.3%
associate-*l*93.3%
*-commutative93.3%
distribute-lft-out96.9%
fma-define100.0%
+-commutative100.0%
*-commutative100.0%
fma-define100.0%
Simplified100.0%
Final simplification100.0%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (if (<= x -9e-158) (* x (+ x (* y (+ 2.0 (/ y x))))) (* y y)))
assert(x < y);
double code(double x, double y) {
double tmp;
if (x <= -9e-158) {
tmp = x * (x + (y * (2.0 + (y / x))));
} 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 (x <= (-9d-158)) then
tmp = x * (x + (y * (2.0d0 + (y / x))))
else
tmp = y * y
end if
code = tmp
end function
assert x < y;
public static double code(double x, double y) {
double tmp;
if (x <= -9e-158) {
tmp = x * (x + (y * (2.0 + (y / x))));
} else {
tmp = y * y;
}
return tmp;
}
[x, y] = sort([x, y]) def code(x, y): tmp = 0 if x <= -9e-158: tmp = x * (x + (y * (2.0 + (y / x)))) else: tmp = y * y return tmp
x, y = sort([x, y]) function code(x, y) tmp = 0.0 if (x <= -9e-158) tmp = Float64(x * Float64(x + Float64(y * Float64(2.0 + Float64(y / x))))); 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 (x <= -9e-158)
tmp = x * (x + (y * (2.0 + (y / x))));
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[x, -9e-158], N[(x * N[(x + N[(y * N[(2.0 + N[(y / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(y * y), $MachinePrecision]]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq -9 \cdot 10^{-158}:\\
\;\;\;\;x \cdot \left(x + y \cdot \left(2 + \frac{y}{x}\right)\right)\\
\mathbf{else}:\\
\;\;\;\;y \cdot y\\
\end{array}
\end{array}
if x < -9e-158Initial program 92.5%
associate-+l+92.5%
+-commutative92.5%
fma-define92.6%
associate-*l*92.6%
Simplified92.6%
Taylor expanded in x around inf 95.9%
*-commutative95.9%
unpow295.9%
associate-/l*92.7%
distribute-lft-out92.7%
Simplified92.7%
distribute-lft-out96.9%
Applied egg-rr96.9%
if -9e-158 < x Initial program 93.8%
Taylor expanded in x around 0 96.9%
Taylor expanded in x around 0 61.3%
associate-*r*61.3%
*-commutative61.3%
*-commutative61.3%
*-commutative61.3%
Simplified61.3%
Taylor expanded in x around inf 57.4%
distribute-lft-in53.1%
unpow253.1%
associate-*r/53.0%
distribute-lft-in56.1%
*-commutative56.1%
distribute-lft-in56.1%
*-commutative56.1%
associate-*l*60.2%
Simplified60.2%
Taylor expanded in y around inf 65.2%
Final simplification76.8%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (if (<= y 1.35e-95) (+ (* x x) (* 2.0 (* x y))) (* y y)))
assert(x < y);
double code(double x, double y) {
double tmp;
if (y <= 1.35e-95) {
tmp = (x * x) + (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.35d-95) then
tmp = (x * x) + (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.35e-95) {
tmp = (x * x) + (2.0 * (x * y));
} else {
tmp = y * y;
}
return tmp;
}
[x, y] = sort([x, y]) def code(x, y): tmp = 0 if y <= 1.35e-95: tmp = (x * x) + (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.35e-95) tmp = Float64(Float64(x * x) + 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.35e-95)
tmp = (x * x) + (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.35e-95], N[(N[(x * x), $MachinePrecision] + N[(2.0 * N[(x * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(y * y), $MachinePrecision]]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
\begin{array}{l}
\mathbf{if}\;y \leq 1.35 \cdot 10^{-95}:\\
\;\;\;\;x \cdot x + 2 \cdot \left(x \cdot y\right)\\
\mathbf{else}:\\
\;\;\;\;y \cdot y\\
\end{array}
\end{array}
if y < 1.35e-95Initial program 94.2%
associate-+l+94.2%
+-commutative94.2%
fma-define94.2%
associate-*l*94.2%
Simplified94.2%
Taylor expanded in y around 0 67.9%
if 1.35e-95 < y Initial program 91.5%
Taylor expanded in x around 0 96.3%
Taylor expanded in x around 0 72.9%
associate-*r*72.9%
*-commutative72.9%
*-commutative72.9%
*-commutative72.9%
Simplified72.9%
Taylor expanded in x around inf 66.5%
distribute-lft-in59.3%
unpow259.3%
associate-*r/59.2%
distribute-lft-in62.8%
*-commutative62.8%
distribute-lft-in62.8%
*-commutative62.8%
associate-*l*69.6%
Simplified69.6%
Taylor expanded in y around inf 78.2%
Final simplification71.3%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (if (<= y 6.5e-96) (* x (+ x (* 2.0 y))) (* y y)))
assert(x < y);
double code(double x, double y) {
double tmp;
if (y <= 6.5e-96) {
tmp = 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 <= 6.5d-96) then
tmp = 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 <= 6.5e-96) {
tmp = x * (x + (2.0 * y));
} else {
tmp = y * y;
}
return tmp;
}
[x, y] = sort([x, y]) def code(x, y): tmp = 0 if y <= 6.5e-96: tmp = 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 <= 6.5e-96) tmp = 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 <= 6.5e-96)
tmp = 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, 6.5e-96], N[(x * N[(x + N[(2.0 * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(y * y), $MachinePrecision]]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
\begin{array}{l}
\mathbf{if}\;y \leq 6.5 \cdot 10^{-96}:\\
\;\;\;\;x \cdot \left(x + 2 \cdot y\right)\\
\mathbf{else}:\\
\;\;\;\;y \cdot y\\
\end{array}
\end{array}
if y < 6.50000000000000001e-96Initial program 94.2%
associate-+l+94.2%
+-commutative94.2%
fma-define94.2%
associate-*l*94.2%
Simplified94.2%
Taylor expanded in y around 0 67.9%
Taylor expanded in x around 0 70.8%
if 6.50000000000000001e-96 < y Initial program 91.5%
Taylor expanded in x around 0 96.3%
Taylor expanded in x around 0 72.9%
associate-*r*72.9%
*-commutative72.9%
*-commutative72.9%
*-commutative72.9%
Simplified72.9%
Taylor expanded in x around inf 66.5%
distribute-lft-in59.3%
unpow259.3%
associate-*r/59.2%
distribute-lft-in62.8%
*-commutative62.8%
distribute-lft-in62.8%
*-commutative62.8%
associate-*l*69.6%
Simplified69.6%
Taylor expanded in y around inf 78.2%
Final simplification73.2%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (if (<= y 1.38e-95) (* x x) (* y y)))
assert(x < y);
double code(double x, double y) {
double tmp;
if (y <= 1.38e-95) {
tmp = x * x;
} 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.38d-95) then
tmp = x * x
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.38e-95) {
tmp = x * x;
} else {
tmp = y * y;
}
return tmp;
}
[x, y] = sort([x, y]) def code(x, y): tmp = 0 if y <= 1.38e-95: tmp = x * x else: tmp = y * y return tmp
x, y = sort([x, y]) function code(x, y) tmp = 0.0 if (y <= 1.38e-95) tmp = Float64(x * x); 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.38e-95)
tmp = x * x;
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.38e-95], N[(x * x), $MachinePrecision], N[(y * y), $MachinePrecision]]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
\begin{array}{l}
\mathbf{if}\;y \leq 1.38 \cdot 10^{-95}:\\
\;\;\;\;x \cdot x\\
\mathbf{else}:\\
\;\;\;\;y \cdot y\\
\end{array}
\end{array}
if y < 1.38e-95Initial program 94.2%
associate-+l+94.2%
+-commutative94.2%
fma-define94.2%
associate-*l*94.2%
Simplified94.2%
Taylor expanded in x around inf 95.5%
*-commutative95.5%
unpow295.5%
associate-/l*94.3%
distribute-lft-out94.3%
Simplified94.3%
distribute-lft-out97.2%
Applied egg-rr97.2%
Taylor expanded in x around inf 70.3%
if 1.38e-95 < y Initial program 91.5%
Taylor expanded in x around 0 96.3%
Taylor expanded in x around 0 72.9%
associate-*r*72.9%
*-commutative72.9%
*-commutative72.9%
*-commutative72.9%
Simplified72.9%
Taylor expanded in x around inf 66.5%
distribute-lft-in59.3%
unpow259.3%
associate-*r/59.2%
distribute-lft-in62.8%
*-commutative62.8%
distribute-lft-in62.8%
*-commutative62.8%
associate-*l*69.6%
Simplified69.6%
Taylor expanded in y around inf 78.2%
Final simplification72.9%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (* x x))
assert(x < y);
double code(double x, double y) {
return 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 = x * x
end function
assert x < y;
public static double code(double x, double y) {
return x * x;
}
[x, y] = sort([x, y]) def code(x, y): return x * x
x, y = sort([x, y]) function code(x, y) return Float64(x * x) end
x, y = num2cell(sort([x, y])){:}
function tmp = code(x, y)
tmp = x * x;
end
NOTE: x and y should be sorted in increasing order before calling this function. code[x_, y_] := N[(x * x), $MachinePrecision]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
x \cdot x
\end{array}
Initial program 93.3%
associate-+l+93.3%
+-commutative93.3%
fma-define93.3%
associate-*l*93.3%
Simplified93.3%
Taylor expanded in x around inf 92.1%
*-commutative92.1%
unpow292.1%
associate-/l*90.1%
distribute-lft-out90.1%
Simplified90.1%
distribute-lft-out93.6%
Applied egg-rr93.6%
Taylor expanded in x around inf 58.5%
Final simplification58.5%
(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 2024115
(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)))