
(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 6 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 0.0005) (+ (* x (+ x (* y 2.0))) (* y y)) (* (pow y 2.0) (+ 1.0 (* (/ x y) (+ 2.0 (/ x y)))))))
assert(x < y);
double code(double x, double y) {
double tmp;
if (y <= 0.0005) {
tmp = (x * (x + (y * 2.0))) + (y * y);
} else {
tmp = pow(y, 2.0) * (1.0 + ((x / y) * (2.0 + (x / 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 <= 0.0005d0) then
tmp = (x * (x + (y * 2.0d0))) + (y * y)
else
tmp = (y ** 2.0d0) * (1.0d0 + ((x / y) * (2.0d0 + (x / y))))
end if
code = tmp
end function
assert x < y;
public static double code(double x, double y) {
double tmp;
if (y <= 0.0005) {
tmp = (x * (x + (y * 2.0))) + (y * y);
} else {
tmp = Math.pow(y, 2.0) * (1.0 + ((x / y) * (2.0 + (x / y))));
}
return tmp;
}
[x, y] = sort([x, y]) def code(x, y): tmp = 0 if y <= 0.0005: tmp = (x * (x + (y * 2.0))) + (y * y) else: tmp = math.pow(y, 2.0) * (1.0 + ((x / y) * (2.0 + (x / y)))) return tmp
x, y = sort([x, y]) function code(x, y) tmp = 0.0 if (y <= 0.0005) tmp = Float64(Float64(x * Float64(x + Float64(y * 2.0))) + Float64(y * y)); else tmp = Float64((y ^ 2.0) * Float64(1.0 + Float64(Float64(x / y) * Float64(2.0 + Float64(x / y))))); end return tmp end
x, y = num2cell(sort([x, y])){:}
function tmp_2 = code(x, y)
tmp = 0.0;
if (y <= 0.0005)
tmp = (x * (x + (y * 2.0))) + (y * y);
else
tmp = (y ^ 2.0) * (1.0 + ((x / y) * (2.0 + (x / 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, 0.0005], N[(N[(x * N[(x + N[(y * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision], N[(N[Power[y, 2.0], $MachinePrecision] * N[(1.0 + N[(N[(x / y), $MachinePrecision] * N[(2.0 + N[(x / y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
\begin{array}{l}
\mathbf{if}\;y \leq 0.0005:\\
\;\;\;\;x \cdot \left(x + y \cdot 2\right) + y \cdot y\\
\mathbf{else}:\\
\;\;\;\;{y}^{2} \cdot \left(1 + \frac{x}{y} \cdot \left(2 + \frac{x}{y}\right)\right)\\
\end{array}
\end{array}
if y < 5.0000000000000001e-4Initial program 94.6%
+-commutative94.6%
associate-*l*94.6%
distribute-lft-out97.3%
Applied egg-rr97.3%
if 5.0000000000000001e-4 < y Initial program 81.6%
Taylor expanded in y around inf 81.7%
unpow281.7%
unpow281.7%
times-frac99.9%
distribute-rgt-out99.9%
Simplified99.9%
Final simplification98.0%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (if (<= y 8.2e+132) (+ (* x (+ x (* y 2.0))) (* y y)) (pow y 2.0)))
assert(x < y);
double code(double x, double y) {
double tmp;
if (y <= 8.2e+132) {
tmp = (x * (x + (y * 2.0))) + (y * y);
} else {
tmp = pow(y, 2.0);
}
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 <= 8.2d+132) then
tmp = (x * (x + (y * 2.0d0))) + (y * y)
else
tmp = y ** 2.0d0
end if
code = tmp
end function
assert x < y;
public static double code(double x, double y) {
double tmp;
if (y <= 8.2e+132) {
tmp = (x * (x + (y * 2.0))) + (y * y);
} else {
tmp = Math.pow(y, 2.0);
}
return tmp;
}
[x, y] = sort([x, y]) def code(x, y): tmp = 0 if y <= 8.2e+132: tmp = (x * (x + (y * 2.0))) + (y * y) else: tmp = math.pow(y, 2.0) return tmp
x, y = sort([x, y]) function code(x, y) tmp = 0.0 if (y <= 8.2e+132) tmp = Float64(Float64(x * Float64(x + Float64(y * 2.0))) + Float64(y * y)); else tmp = y ^ 2.0; end return tmp end
x, y = num2cell(sort([x, y])){:}
function tmp_2 = code(x, y)
tmp = 0.0;
if (y <= 8.2e+132)
tmp = (x * (x + (y * 2.0))) + (y * y);
else
tmp = y ^ 2.0;
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, 8.2e+132], N[(N[(x * N[(x + N[(y * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision], N[Power[y, 2.0], $MachinePrecision]]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
\begin{array}{l}
\mathbf{if}\;y \leq 8.2 \cdot 10^{+132}:\\
\;\;\;\;x \cdot \left(x + y \cdot 2\right) + y \cdot y\\
\mathbf{else}:\\
\;\;\;\;{y}^{2}\\
\end{array}
\end{array}
if y < 8.19999999999999983e132Initial program 93.5%
+-commutative93.5%
associate-*l*93.5%
distribute-lft-out97.6%
Applied egg-rr97.6%
if 8.19999999999999983e132 < y Initial program 77.5%
Taylor expanded in x around 0 95.6%
Final simplification97.3%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (if (<= y 1.2e+204) (+ (* x (+ x (* y 2.0))) (* y y)) (* y (+ y (* x 2.0)))))
assert(x < y);
double code(double x, double y) {
double tmp;
if (y <= 1.2e+204) {
tmp = (x * (x + (y * 2.0))) + (y * y);
} else {
tmp = y * (y + (x * 2.0));
}
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.2d+204) then
tmp = (x * (x + (y * 2.0d0))) + (y * y)
else
tmp = y * (y + (x * 2.0d0))
end if
code = tmp
end function
assert x < y;
public static double code(double x, double y) {
double tmp;
if (y <= 1.2e+204) {
tmp = (x * (x + (y * 2.0))) + (y * y);
} else {
tmp = y * (y + (x * 2.0));
}
return tmp;
}
[x, y] = sort([x, y]) def code(x, y): tmp = 0 if y <= 1.2e+204: tmp = (x * (x + (y * 2.0))) + (y * y) else: tmp = y * (y + (x * 2.0)) return tmp
x, y = sort([x, y]) function code(x, y) tmp = 0.0 if (y <= 1.2e+204) tmp = Float64(Float64(x * Float64(x + Float64(y * 2.0))) + Float64(y * y)); else tmp = Float64(y * Float64(y + Float64(x * 2.0))); end return tmp end
x, y = num2cell(sort([x, y])){:}
function tmp_2 = code(x, y)
tmp = 0.0;
if (y <= 1.2e+204)
tmp = (x * (x + (y * 2.0))) + (y * y);
else
tmp = y * (y + (x * 2.0));
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.2e+204], N[(N[(x * N[(x + N[(y * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision], N[(y * N[(y + N[(x * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
\begin{array}{l}
\mathbf{if}\;y \leq 1.2 \cdot 10^{+204}:\\
\;\;\;\;x \cdot \left(x + y \cdot 2\right) + y \cdot y\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(y + x \cdot 2\right)\\
\end{array}
\end{array}
if y < 1.2e204Initial program 92.2%
+-commutative92.2%
associate-*l*92.2%
distribute-lft-out97.8%
Applied egg-rr97.8%
if 1.2e204 < y Initial program 80.0%
Taylor expanded in x around 0 80.0%
associate-*r*80.0%
distribute-rgt-out96.0%
Applied egg-rr96.0%
Final simplification97.6%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (if (<= y 3.5e-128) (* x (+ x (* y 2.0))) (* y (+ y (* x 2.0)))))
assert(x < y);
double code(double x, double y) {
double tmp;
if (y <= 3.5e-128) {
tmp = x * (x + (y * 2.0));
} else {
tmp = y * (y + (x * 2.0));
}
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 <= 3.5d-128) then
tmp = x * (x + (y * 2.0d0))
else
tmp = y * (y + (x * 2.0d0))
end if
code = tmp
end function
assert x < y;
public static double code(double x, double y) {
double tmp;
if (y <= 3.5e-128) {
tmp = x * (x + (y * 2.0));
} else {
tmp = y * (y + (x * 2.0));
}
return tmp;
}
[x, y] = sort([x, y]) def code(x, y): tmp = 0 if y <= 3.5e-128: tmp = x * (x + (y * 2.0)) else: tmp = y * (y + (x * 2.0)) return tmp
x, y = sort([x, y]) function code(x, y) tmp = 0.0 if (y <= 3.5e-128) tmp = Float64(x * Float64(x + Float64(y * 2.0))); else tmp = Float64(y * Float64(y + Float64(x * 2.0))); end return tmp end
x, y = num2cell(sort([x, y])){:}
function tmp_2 = code(x, y)
tmp = 0.0;
if (y <= 3.5e-128)
tmp = x * (x + (y * 2.0));
else
tmp = y * (y + (x * 2.0));
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, 3.5e-128], N[(x * N[(x + N[(y * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(y * N[(y + N[(x * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
\begin{array}{l}
\mathbf{if}\;y \leq 3.5 \cdot 10^{-128}:\\
\;\;\;\;x \cdot \left(x + y \cdot 2\right)\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(y + x \cdot 2\right)\\
\end{array}
\end{array}
if y < 3.5e-128Initial program 93.8%
Taylor expanded in y around inf 46.7%
unpow246.7%
unpow246.7%
times-frac63.1%
distribute-rgt-out66.2%
Simplified66.2%
+-commutative66.2%
flip3-+47.1%
pow347.1%
metadata-eval47.1%
+-commutative47.1%
pow347.1%
associate-*l/47.1%
associate-/l*45.9%
Applied egg-rr45.9%
Taylor expanded in y around 0 65.1%
associate-*r*65.1%
+-commutative65.1%
unpow265.1%
*-commutative65.1%
associate-*r*65.1%
distribute-lft-in68.2%
Simplified68.2%
if 3.5e-128 < y Initial program 86.3%
Taylor expanded in x around 0 68.1%
associate-*r*68.1%
distribute-rgt-out72.3%
Applied egg-rr72.3%
Final simplification69.8%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (* x (+ x (* y 2.0))))
assert(x < y);
double code(double x, double y) {
return x * (x + (y * 2.0));
}
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 + (y * 2.0d0))
end function
assert x < y;
public static double code(double x, double y) {
return x * (x + (y * 2.0));
}
[x, y] = sort([x, y]) def code(x, y): return x * (x + (y * 2.0))
x, y = sort([x, y]) function code(x, y) return Float64(x * Float64(x + Float64(y * 2.0))) end
x, y = num2cell(sort([x, y])){:}
function tmp = code(x, y)
tmp = x * (x + (y * 2.0));
end
NOTE: x and y should be sorted in increasing order before calling this function. code[x_, y_] := N[(x * N[(x + N[(y * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
x \cdot \left(x + y \cdot 2\right)
\end{array}
Initial program 91.0%
Taylor expanded in y around inf 61.0%
unpow261.0%
unpow261.0%
times-frac76.3%
distribute-rgt-out78.7%
Simplified78.7%
+-commutative78.7%
flip3-+59.3%
pow359.3%
metadata-eval59.3%
+-commutative59.3%
pow359.3%
associate-*l/59.3%
associate-/l*58.5%
Applied egg-rr58.5%
Taylor expanded in y around 0 51.7%
associate-*r*51.7%
+-commutative51.7%
unpow251.7%
*-commutative51.7%
associate-*r*51.7%
distribute-lft-in57.2%
Simplified57.2%
Final simplification57.2%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (* 2.0 (* y x)))
assert(x < y);
double code(double x, double y) {
return 2.0 * (y * 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 = 2.0d0 * (y * x)
end function
assert x < y;
public static double code(double x, double y) {
return 2.0 * (y * x);
}
[x, y] = sort([x, y]) def code(x, y): return 2.0 * (y * x)
x, y = sort([x, y]) function code(x, y) return Float64(2.0 * Float64(y * x)) end
x, y = num2cell(sort([x, y])){:}
function tmp = code(x, y)
tmp = 2.0 * (y * x);
end
NOTE: x and y should be sorted in increasing order before calling this function. code[x_, y_] := N[(2.0 * N[(y * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
2 \cdot \left(y \cdot x\right)
\end{array}
Initial program 91.0%
Taylor expanded in x around 0 54.7%
Taylor expanded in x around inf 16.0%
Final simplification16.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 2024085
(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)))