
(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}
(FPCore (x y) :precision binary64 (if (<= y 4.5e+150) (+ (* x (+ x (* y 2.0))) (* y y)) (* y (+ y (* x 2.0)))))
double code(double x, double y) {
double tmp;
if (y <= 4.5e+150) {
tmp = (x * (x + (y * 2.0))) + (y * y);
} else {
tmp = y * (y + (x * 2.0));
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (y <= 4.5d+150) then
tmp = (x * (x + (y * 2.0d0))) + (y * y)
else
tmp = y * (y + (x * 2.0d0))
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (y <= 4.5e+150) {
tmp = (x * (x + (y * 2.0))) + (y * y);
} else {
tmp = y * (y + (x * 2.0));
}
return tmp;
}
def code(x, y): tmp = 0 if y <= 4.5e+150: tmp = (x * (x + (y * 2.0))) + (y * y) else: tmp = y * (y + (x * 2.0)) return tmp
function code(x, y) tmp = 0.0 if (y <= 4.5e+150) 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
function tmp_2 = code(x, y) tmp = 0.0; if (y <= 4.5e+150) tmp = (x * (x + (y * 2.0))) + (y * y); else tmp = y * (y + (x * 2.0)); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, 4.5e+150], 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}
\\
\begin{array}{l}
\mathbf{if}\;y \leq 4.5 \cdot 10^{+150}:\\
\;\;\;\;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 < 4.5e150Initial program 96.5%
+-commutative96.5%
associate-*r*96.5%
distribute-lft-out100.0%
*-commutative100.0%
Applied egg-rr100.0%
if 4.5e150 < y Initial program 92.0%
+-commutative92.0%
associate-*r*92.0%
distribute-lft-out92.0%
*-commutative92.0%
Applied egg-rr92.0%
Taylor expanded in x around 0 92.0%
+-commutative92.0%
unpow292.0%
associate-*r*92.0%
*-commutative92.0%
distribute-rgt-out100.0%
*-commutative100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (x y) :precision binary64 (fma x x (* y (+ y (* x 2.0)))))
double code(double x, double y) {
return fma(x, x, (y * (y + (x * 2.0))));
}
function code(x, y) return fma(x, x, Float64(y * Float64(y + Float64(x * 2.0)))) end
code[x_, y_] := N[(x * x + N[(y * N[(y + N[(x * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(x, x, y \cdot \left(y + x \cdot 2\right)\right)
\end{array}
Initial program 96.1%
associate-+l+96.1%
associate-*l*96.1%
*-commutative96.1%
*-commutative96.1%
+-commutative96.1%
fma-def96.1%
*-commutative96.1%
*-commutative96.1%
associate-*l*96.1%
+-commutative96.1%
distribute-rgt-out96.9%
Simplified96.9%
Final simplification96.9%
(FPCore (x y) :precision binary64 (+ (* x x) (pow y 2.0)))
double code(double x, double y) {
return (x * x) + pow(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) + Math.pow(y, 2.0);
}
def code(x, y): return (x * x) + math.pow(y, 2.0)
function code(x, y) return Float64(Float64(x * x) + (y ^ 2.0)) end
function tmp = code(x, y) tmp = (x * x) + (y ^ 2.0); end
code[x_, y_] := N[(N[(x * x), $MachinePrecision] + N[Power[y, 2.0], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot x + {y}^{2}
\end{array}
Initial program 96.1%
associate-+l+96.1%
associate-*l*96.1%
*-commutative96.1%
*-commutative96.1%
+-commutative96.1%
fma-def96.1%
*-commutative96.1%
*-commutative96.1%
Simplified96.1%
Taylor expanded in y around inf 98.7%
Final simplification98.7%
(FPCore (x y) :precision binary64 (if (or (<= y 9.5e-123) (and (not (<= y 4.2e-60)) (<= y 1.75e-26))) (+ (* x x) (* 2.0 (* x y))) (* y (+ y (* x 2.0)))))
double code(double x, double y) {
double tmp;
if ((y <= 9.5e-123) || (!(y <= 4.2e-60) && (y <= 1.75e-26))) {
tmp = (x * x) + (2.0 * (x * y));
} else {
tmp = y * (y + (x * 2.0));
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if ((y <= 9.5d-123) .or. (.not. (y <= 4.2d-60)) .and. (y <= 1.75d-26)) then
tmp = (x * x) + (2.0d0 * (x * y))
else
tmp = y * (y + (x * 2.0d0))
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((y <= 9.5e-123) || (!(y <= 4.2e-60) && (y <= 1.75e-26))) {
tmp = (x * x) + (2.0 * (x * y));
} else {
tmp = y * (y + (x * 2.0));
}
return tmp;
}
def code(x, y): tmp = 0 if (y <= 9.5e-123) or (not (y <= 4.2e-60) and (y <= 1.75e-26)): tmp = (x * x) + (2.0 * (x * y)) else: tmp = y * (y + (x * 2.0)) return tmp
function code(x, y) tmp = 0.0 if ((y <= 9.5e-123) || (!(y <= 4.2e-60) && (y <= 1.75e-26))) tmp = Float64(Float64(x * x) + Float64(2.0 * Float64(x * y))); else tmp = Float64(y * Float64(y + Float64(x * 2.0))); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((y <= 9.5e-123) || (~((y <= 4.2e-60)) && (y <= 1.75e-26))) tmp = (x * x) + (2.0 * (x * y)); else tmp = y * (y + (x * 2.0)); end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[y, 9.5e-123], And[N[Not[LessEqual[y, 4.2e-60]], $MachinePrecision], LessEqual[y, 1.75e-26]]], N[(N[(x * x), $MachinePrecision] + N[(2.0 * N[(x * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(y * N[(y + N[(x * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq 9.5 \cdot 10^{-123} \lor \neg \left(y \leq 4.2 \cdot 10^{-60}\right) \land y \leq 1.75 \cdot 10^{-26}:\\
\;\;\;\;x \cdot x + 2 \cdot \left(x \cdot y\right)\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(y + x \cdot 2\right)\\
\end{array}
\end{array}
if y < 9.5000000000000002e-123 or 4.19999999999999982e-60 < y < 1.74999999999999992e-26Initial program 97.1%
associate-+l+97.1%
associate-*l*97.1%
*-commutative97.1%
*-commutative97.1%
+-commutative97.1%
fma-def97.1%
*-commutative97.1%
*-commutative97.1%
Simplified97.1%
Taylor expanded in y around 0 60.1%
if 9.5000000000000002e-123 < y < 4.19999999999999982e-60 or 1.74999999999999992e-26 < y Initial program 93.8%
+-commutative93.8%
associate-*r*93.8%
distribute-lft-out97.5%
*-commutative97.5%
Applied egg-rr97.5%
Taylor expanded in x around 0 70.1%
+-commutative70.1%
unpow270.1%
associate-*r*70.1%
*-commutative70.1%
distribute-rgt-out72.6%
*-commutative72.6%
Simplified72.6%
Final simplification64.1%
(FPCore (x y) :precision binary64 (* y (+ y (* x 2.0))))
double code(double x, double y) {
return y * (y + (x * 2.0));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = y * (y + (x * 2.0d0))
end function
public static double code(double x, double y) {
return y * (y + (x * 2.0));
}
def code(x, y): return y * (y + (x * 2.0))
function code(x, y) return Float64(y * Float64(y + Float64(x * 2.0))) end
function tmp = code(x, y) tmp = y * (y + (x * 2.0)); end
code[x_, y_] := N[(y * N[(y + N[(x * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
y \cdot \left(y + x \cdot 2\right)
\end{array}
Initial program 96.1%
+-commutative96.1%
associate-*r*96.1%
distribute-lft-out99.2%
*-commutative99.2%
Applied egg-rr99.2%
Taylor expanded in x around 0 56.3%
+-commutative56.3%
unpow256.3%
associate-*r*56.3%
*-commutative56.3%
distribute-rgt-out57.1%
*-commutative57.1%
Simplified57.1%
Final simplification57.1%
(FPCore (x y) :precision binary64 (* 2.0 (* x y)))
double code(double x, double y) {
return 2.0 * (x * y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = 2.0d0 * (x * y)
end function
public static double code(double x, double y) {
return 2.0 * (x * y);
}
def code(x, y): return 2.0 * (x * y)
function code(x, y) return Float64(2.0 * Float64(x * y)) end
function tmp = code(x, y) tmp = 2.0 * (x * y); end
code[x_, y_] := N[(2.0 * N[(x * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
2 \cdot \left(x \cdot y\right)
\end{array}
Initial program 96.1%
associate-+l+96.1%
associate-*l*96.1%
*-commutative96.1%
*-commutative96.1%
+-commutative96.1%
fma-def96.1%
*-commutative96.1%
*-commutative96.1%
Simplified96.1%
Taylor expanded in y around 0 51.2%
Taylor expanded in x around 0 12.1%
Final simplification12.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 2024021
(FPCore (x y)
:name "Examples.Basics.ProofTests:f4 from sbv-4.4"
:precision binary64
:herbie-target
(+ (* x x) (+ (* y y) (* (* x y) 2.0)))
(+ (+ (* x x) (* (* x 2.0) y)) (* y y)))