
(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 91.0%
+-commutative91.0%
associate-+l+91.0%
+-commutative91.0%
associate-+l+91.0%
distribute-rgt-out95.7%
fma-define100.0%
fma-define100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (x y) :precision binary64 (let* ((t_0 (+ (* y y) (+ (* x x) (* y (* x 2.0)))))) (if (<= t_0 INFINITY) t_0 (pow y 2.0))))
double code(double x, double y) {
double t_0 = (y * y) + ((x * x) + (y * (x * 2.0)));
double tmp;
if (t_0 <= ((double) INFINITY)) {
tmp = t_0;
} else {
tmp = pow(y, 2.0);
}
return tmp;
}
public static double code(double x, double y) {
double t_0 = (y * y) + ((x * x) + (y * (x * 2.0)));
double tmp;
if (t_0 <= Double.POSITIVE_INFINITY) {
tmp = t_0;
} else {
tmp = Math.pow(y, 2.0);
}
return tmp;
}
def code(x, y): t_0 = (y * y) + ((x * x) + (y * (x * 2.0))) tmp = 0 if t_0 <= math.inf: tmp = t_0 else: tmp = math.pow(y, 2.0) return tmp
function code(x, y) t_0 = Float64(Float64(y * y) + Float64(Float64(x * x) + Float64(y * Float64(x * 2.0)))) tmp = 0.0 if (t_0 <= Inf) tmp = t_0; else tmp = y ^ 2.0; end return tmp end
function tmp_2 = code(x, y) t_0 = (y * y) + ((x * x) + (y * (x * 2.0))); tmp = 0.0; if (t_0 <= Inf) tmp = t_0; else tmp = y ^ 2.0; end tmp_2 = tmp; end
code[x_, y_] := Block[{t$95$0 = N[(N[(y * y), $MachinePrecision] + N[(N[(x * x), $MachinePrecision] + N[(y * N[(x * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, Infinity], t$95$0, N[Power[y, 2.0], $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := y \cdot y + \left(x \cdot x + y \cdot \left(x \cdot 2\right)\right)\\
\mathbf{if}\;t\_0 \leq \infty:\\
\;\;\;\;t\_0\\
\mathbf{else}:\\
\;\;\;\;{y}^{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%
Taylor expanded in x around 0 72.0%
Final simplification97.5%
(FPCore (x y) :precision binary64 (+ (pow x 2.0) (* y y)))
double code(double x, double y) {
return pow(x, 2.0) + (y * y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (x ** 2.0d0) + (y * y)
end function
public static double code(double x, double y) {
return Math.pow(x, 2.0) + (y * y);
}
def code(x, y): return math.pow(x, 2.0) + (y * y)
function code(x, y) return Float64((x ^ 2.0) + Float64(y * y)) end
function tmp = code(x, y) tmp = (x ^ 2.0) + (y * y); end
code[x_, y_] := N[(N[Power[x, 2.0], $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
{x}^{2} + y \cdot y
\end{array}
Initial program 91.0%
Taylor expanded in x around inf 99.0%
Final simplification99.0%
(FPCore (x y) :precision binary64 (let* ((t_0 (+ (* y y) (+ (* x x) (* y (* x 2.0)))))) (if (<= t_0 INFINITY) t_0 (* x (+ x (* y 2.0))))))
double code(double x, double y) {
double t_0 = (y * y) + ((x * x) + (y * (x * 2.0)));
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 = (y * y) + ((x * x) + (y * (x * 2.0)));
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 = (y * y) + ((x * x) + (y * (x * 2.0))) 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(y * y) + Float64(Float64(x * x) + Float64(y * Float64(x * 2.0)))) 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 = (y * y) + ((x * x) + (y * (x * 2.0))); 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[(y * y), $MachinePrecision] + N[(N[(x * x), $MachinePrecision] + N[(y * N[(x * 2.0), $MachinePrecision]), $MachinePrecision]), $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 := y \cdot y + \left(x \cdot x + y \cdot \left(x \cdot 2\right)\right)\\
\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%
fma-define0.0%
distribute-rgt-in52.2%
fma-undefine52.2%
Applied egg-rr52.2%
Taylor expanded in x around inf 0.0%
+-commutative0.0%
unpow20.0%
*-commutative0.0%
associate-*r*0.0%
distribute-rgt-out47.8%
*-commutative47.8%
Simplified47.8%
Final simplification95.3%
(FPCore (x y) :precision binary64 (if (or (<= x -55000.0) (and (not (<= x -7.5e-25)) (<= x -6.5e-81))) (* x (+ x (* y 2.0))) (+ (* y y) (* x (* y 2.0)))))
double code(double x, double y) {
double tmp;
if ((x <= -55000.0) || (!(x <= -7.5e-25) && (x <= -6.5e-81))) {
tmp = x * (x + (y * 2.0));
} else {
tmp = (y * y) + (x * (y * 2.0));
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if ((x <= (-55000.0d0)) .or. (.not. (x <= (-7.5d-25))) .and. (x <= (-6.5d-81))) then
tmp = x * (x + (y * 2.0d0))
else
tmp = (y * y) + (x * (y * 2.0d0))
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((x <= -55000.0) || (!(x <= -7.5e-25) && (x <= -6.5e-81))) {
tmp = x * (x + (y * 2.0));
} else {
tmp = (y * y) + (x * (y * 2.0));
}
return tmp;
}
def code(x, y): tmp = 0 if (x <= -55000.0) or (not (x <= -7.5e-25) and (x <= -6.5e-81)): tmp = x * (x + (y * 2.0)) else: tmp = (y * y) + (x * (y * 2.0)) return tmp
function code(x, y) tmp = 0.0 if ((x <= -55000.0) || (!(x <= -7.5e-25) && (x <= -6.5e-81))) tmp = Float64(x * Float64(x + Float64(y * 2.0))); else tmp = Float64(Float64(y * y) + Float64(x * Float64(y * 2.0))); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((x <= -55000.0) || (~((x <= -7.5e-25)) && (x <= -6.5e-81))) tmp = x * (x + (y * 2.0)); else tmp = (y * y) + (x * (y * 2.0)); end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[x, -55000.0], And[N[Not[LessEqual[x, -7.5e-25]], $MachinePrecision], LessEqual[x, -6.5e-81]]], N[(x * N[(x + N[(y * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(y * y), $MachinePrecision] + N[(x * N[(y * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -55000 \lor \neg \left(x \leq -7.5 \cdot 10^{-25}\right) \land x \leq -6.5 \cdot 10^{-81}:\\
\;\;\;\;x \cdot \left(x + y \cdot 2\right)\\
\mathbf{else}:\\
\;\;\;\;y \cdot y + x \cdot \left(y \cdot 2\right)\\
\end{array}
\end{array}
if x < -55000 or -7.49999999999999989e-25 < x < -6.5000000000000002e-81Initial program 84.7%
associate-+l+84.7%
fma-define84.7%
distribute-rgt-in91.8%
fma-undefine91.8%
Applied egg-rr91.8%
Taylor expanded in x around inf 73.4%
+-commutative73.4%
unpow273.4%
*-commutative73.4%
associate-*r*73.4%
distribute-rgt-out81.6%
*-commutative81.6%
Simplified81.6%
if -55000 < x < -7.49999999999999989e-25 or -6.5000000000000002e-81 < x Initial program 94.1%
Taylor expanded in x around 0 57.7%
associate-*r*57.7%
*-commutative57.7%
associate-*r*57.7%
Simplified57.7%
Final simplification65.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 91.0%
associate-+l+91.0%
fma-define91.0%
distribute-rgt-in95.7%
fma-undefine95.7%
Applied egg-rr95.7%
Taylor expanded in x around inf 56.7%
+-commutative56.7%
unpow256.7%
*-commutative56.7%
associate-*r*56.7%
distribute-rgt-out61.0%
*-commutative61.0%
Simplified61.0%
Final simplification61.0%
(FPCore (x y) :precision binary64 (* 2.0 (* y x)))
double code(double x, double y) {
return 2.0 * (y * x);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = 2.0d0 * (y * x)
end function
public static double code(double x, double y) {
return 2.0 * (y * x);
}
def code(x, y): return 2.0 * (y * x)
function code(x, y) return Float64(2.0 * Float64(y * x)) end
function tmp = code(x, y) tmp = 2.0 * (y * x); end
code[x_, y_] := N[(2.0 * N[(y * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
2 \cdot \left(y \cdot x\right)
\end{array}
Initial program 91.0%
Taylor expanded in x around 0 46.7%
associate-*r*46.7%
*-commutative46.7%
associate-*r*46.7%
Simplified46.7%
Taylor expanded in x around inf 12.9%
*-commutative12.9%
Simplified12.9%
Final simplification12.9%
(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 2024034
(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)))