
(FPCore (a b angle x-scale y-scale)
:precision binary64
(let* ((t_0 (* (/ angle 180.0) (PI)))
(t_1 (sin t_0))
(t_2 (cos t_0))
(t_3
(/ (/ (+ (pow (* a t_2) 2.0) (pow (* b t_1) 2.0)) y-scale) y-scale))
(t_4
(/ (/ (+ (pow (* a t_1) 2.0) (pow (* b t_2) 2.0)) x-scale) x-scale))
(t_5 (* (* b a) (* b (- a))))
(t_6 (/ (* 4.0 t_5) (pow (* x-scale y-scale) 2.0))))
(/
(-
(sqrt
(*
(* (* 2.0 t_6) t_5)
(-
(+ t_4 t_3)
(sqrt
(+
(pow (- t_4 t_3) 2.0)
(pow
(/
(/ (* (* (* 2.0 (- (pow b 2.0) (pow a 2.0))) t_1) t_2) x-scale)
y-scale)
2.0)))))))
t_6)))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{angle}{180} \cdot \mathsf{PI}\left(\right)\\
t_1 := \sin t\_0\\
t_2 := \cos t\_0\\
t_3 := \frac{\frac{{\left(a \cdot t\_2\right)}^{2} + {\left(b \cdot t\_1\right)}^{2}}{y-scale}}{y-scale}\\
t_4 := \frac{\frac{{\left(a \cdot t\_1\right)}^{2} + {\left(b \cdot t\_2\right)}^{2}}{x-scale}}{x-scale}\\
t_5 := \left(b \cdot a\right) \cdot \left(b \cdot \left(-a\right)\right)\\
t_6 := \frac{4 \cdot t\_5}{{\left(x-scale \cdot y-scale\right)}^{2}}\\
\frac{-\sqrt{\left(\left(2 \cdot t\_6\right) \cdot t\_5\right) \cdot \left(\left(t\_4 + t\_3\right) - \sqrt{{\left(t\_4 - t\_3\right)}^{2} + {\left(\frac{\frac{\left(\left(2 \cdot \left({b}^{2} - {a}^{2}\right)\right) \cdot t\_1\right) \cdot t\_2}{x-scale}}{y-scale}\right)}^{2}}\right)}}{t\_6}
\end{array}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 4 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (a b angle x-scale y-scale)
:precision binary64
(let* ((t_0 (* (/ angle 180.0) (PI)))
(t_1 (sin t_0))
(t_2 (cos t_0))
(t_3
(/ (/ (+ (pow (* a t_2) 2.0) (pow (* b t_1) 2.0)) y-scale) y-scale))
(t_4
(/ (/ (+ (pow (* a t_1) 2.0) (pow (* b t_2) 2.0)) x-scale) x-scale))
(t_5 (* (* b a) (* b (- a))))
(t_6 (/ (* 4.0 t_5) (pow (* x-scale y-scale) 2.0))))
(/
(-
(sqrt
(*
(* (* 2.0 t_6) t_5)
(-
(+ t_4 t_3)
(sqrt
(+
(pow (- t_4 t_3) 2.0)
(pow
(/
(/ (* (* (* 2.0 (- (pow b 2.0) (pow a 2.0))) t_1) t_2) x-scale)
y-scale)
2.0)))))))
t_6)))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{angle}{180} \cdot \mathsf{PI}\left(\right)\\
t_1 := \sin t\_0\\
t_2 := \cos t\_0\\
t_3 := \frac{\frac{{\left(a \cdot t\_2\right)}^{2} + {\left(b \cdot t\_1\right)}^{2}}{y-scale}}{y-scale}\\
t_4 := \frac{\frac{{\left(a \cdot t\_1\right)}^{2} + {\left(b \cdot t\_2\right)}^{2}}{x-scale}}{x-scale}\\
t_5 := \left(b \cdot a\right) \cdot \left(b \cdot \left(-a\right)\right)\\
t_6 := \frac{4 \cdot t\_5}{{\left(x-scale \cdot y-scale\right)}^{2}}\\
\frac{-\sqrt{\left(\left(2 \cdot t\_6\right) \cdot t\_5\right) \cdot \left(\left(t\_4 + t\_3\right) - \sqrt{{\left(t\_4 - t\_3\right)}^{2} + {\left(\frac{\frac{\left(\left(2 \cdot \left({b}^{2} - {a}^{2}\right)\right) \cdot t\_1\right) \cdot t\_2}{x-scale}}{y-scale}\right)}^{2}}\right)}}{t\_6}
\end{array}
\end{array}
x-scale_m = (fabs.f64 x-scale)
b_m = (fabs.f64 b)
a_m = (fabs.f64 a)
(FPCore (a_m b_m angle x-scale_m y-scale)
:precision binary64
(let* ((t_0 (/ (* b_m b_m) x-scale_m))
(t_1 (* (* (PI) angle) 0.005555555555555556))
(t_2 (pow (cos t_1) 2.0))
(t_3 (/ t_2 x-scale_m))
(t_4 (pow (sin t_1) 2.0)))
(if (<= b_m 4.2e-204)
(*
(* 0.25 (* (* (sqrt 8.0) y-scale) x-scale_m))
(sqrt
(-
(fma (/ (* b_m b_m) y-scale) (/ t_4 y-scale) (* t_3 t_0))
(sqrt
(fma
(/ 4.0 (* y-scale y-scale))
(* (/ (* (pow b_m 4.0) t_2) x-scale_m) (/ t_4 x-scale_m))
(pow
(fma t_3 t_0 (* (* b_m b_m) (/ t_4 (* (- y-scale) y-scale))))
2.0))))))
(* a_m x-scale_m))))\begin{array}{l}
x-scale_m = \left|x-scale\right|
\\
b_m = \left|b\right|
\\
a_m = \left|a\right|
\\
\begin{array}{l}
t_0 := \frac{b\_m \cdot b\_m}{x-scale\_m}\\
t_1 := \left(\mathsf{PI}\left(\right) \cdot angle\right) \cdot 0.005555555555555556\\
t_2 := {\cos t\_1}^{2}\\
t_3 := \frac{t\_2}{x-scale\_m}\\
t_4 := {\sin t\_1}^{2}\\
\mathbf{if}\;b\_m \leq 4.2 \cdot 10^{-204}:\\
\;\;\;\;\left(0.25 \cdot \left(\left(\sqrt{8} \cdot y-scale\right) \cdot x-scale\_m\right)\right) \cdot \sqrt{\mathsf{fma}\left(\frac{b\_m \cdot b\_m}{y-scale}, \frac{t\_4}{y-scale}, t\_3 \cdot t\_0\right) - \sqrt{\mathsf{fma}\left(\frac{4}{y-scale \cdot y-scale}, \frac{{b\_m}^{4} \cdot t\_2}{x-scale\_m} \cdot \frac{t\_4}{x-scale\_m}, {\left(\mathsf{fma}\left(t\_3, t\_0, \left(b\_m \cdot b\_m\right) \cdot \frac{t\_4}{\left(-y-scale\right) \cdot y-scale}\right)\right)}^{2}\right)}}\\
\mathbf{else}:\\
\;\;\;\;a\_m \cdot x-scale\_m\\
\end{array}
\end{array}
if b < 4.20000000000000018e-204Initial program 0.0%
Taylor expanded in a around 0
Applied rewrites13.9%
if 4.20000000000000018e-204 < b Initial program 0.0%
Taylor expanded in angle around 0
associate-*r*N/A
lower-*.f64N/A
lower-*.f64N/A
associate-*r*N/A
lower-*.f64N/A
lower-*.f64N/A
lower-sqrt.f64N/A
lower-sqrt.f6426.7
Applied rewrites26.7%
Applied rewrites26.9%
Taylor expanded in a around 0
Applied rewrites26.9%
Final simplification18.8%
x-scale_m = (fabs.f64 x-scale)
b_m = (fabs.f64 b)
a_m = (fabs.f64 a)
(FPCore (a_m b_m angle x-scale_m y-scale)
:precision binary64
(let* ((t_0 (* 0.005555555555555556 (* angle (PI))))
(t_1 (pow (cos t_0) 2.0))
(t_2 (pow (sin t_0) 2.0)))
(if (<= b_m 3.85e-205)
(*
(* 0.25 (* b_m (* x-scale_m (* y-scale (sqrt 8.0)))))
(sqrt
(-
(/ t_2 (* y-scale y-scale))
(* 0.5 (/ (* (/ (* t_1 t_2) (* y-scale y-scale)) 2.0) t_1)))))
(* a_m x-scale_m))))\begin{array}{l}
x-scale_m = \left|x-scale\right|
\\
b_m = \left|b\right|
\\
a_m = \left|a\right|
\\
\begin{array}{l}
t_0 := 0.005555555555555556 \cdot \left(angle \cdot \mathsf{PI}\left(\right)\right)\\
t_1 := {\cos t\_0}^{2}\\
t_2 := {\sin t\_0}^{2}\\
\mathbf{if}\;b\_m \leq 3.85 \cdot 10^{-205}:\\
\;\;\;\;\left(0.25 \cdot \left(b\_m \cdot \left(x-scale\_m \cdot \left(y-scale \cdot \sqrt{8}\right)\right)\right)\right) \cdot \sqrt{\frac{t\_2}{y-scale \cdot y-scale} - 0.5 \cdot \frac{\frac{t\_1 \cdot t\_2}{y-scale \cdot y-scale} \cdot 2}{t\_1}}\\
\mathbf{else}:\\
\;\;\;\;a\_m \cdot x-scale\_m\\
\end{array}
\end{array}
if b < 3.85e-205Initial program 0.0%
Taylor expanded in b around inf
Applied rewrites2.8%
Taylor expanded in x-scale around 0
Applied rewrites19.2%
if 3.85e-205 < b Initial program 0.0%
Taylor expanded in angle around 0
associate-*r*N/A
lower-*.f64N/A
lower-*.f64N/A
associate-*r*N/A
lower-*.f64N/A
lower-*.f64N/A
lower-sqrt.f64N/A
lower-sqrt.f6426.7
Applied rewrites26.7%
Applied rewrites26.9%
Taylor expanded in a around 0
Applied rewrites26.9%
x-scale_m = (fabs.f64 x-scale)
b_m = (fabs.f64 b)
a_m = (fabs.f64 a)
(FPCore (a_m b_m angle x-scale_m y-scale)
:precision binary64
(let* ((t_0 (* 0.005555555555555556 (* angle (PI))))
(t_1 (pow (sin t_0) 2.0))
(t_2 (pow (cos t_0) 2.0)))
(if (<= b_m 1.8e-241)
(*
(* 0.25 (* b_m (* x-scale_m (sqrt 8.0))))
(sqrt
(fma
-0.5
(/
(*
(* x-scale_m x-scale_m)
(* (/ (* t_2 t_1) (* x-scale_m x-scale_m)) 2.0))
t_2)
t_1)))
(* a_m x-scale_m))))\begin{array}{l}
x-scale_m = \left|x-scale\right|
\\
b_m = \left|b\right|
\\
a_m = \left|a\right|
\\
\begin{array}{l}
t_0 := 0.005555555555555556 \cdot \left(angle \cdot \mathsf{PI}\left(\right)\right)\\
t_1 := {\sin t\_0}^{2}\\
t_2 := {\cos t\_0}^{2}\\
\mathbf{if}\;b\_m \leq 1.8 \cdot 10^{-241}:\\
\;\;\;\;\left(0.25 \cdot \left(b\_m \cdot \left(x-scale\_m \cdot \sqrt{8}\right)\right)\right) \cdot \sqrt{\mathsf{fma}\left(-0.5, \frac{\left(x-scale\_m \cdot x-scale\_m\right) \cdot \left(\frac{t\_2 \cdot t\_1}{x-scale\_m \cdot x-scale\_m} \cdot 2\right)}{t\_2}, t\_1\right)}\\
\mathbf{else}:\\
\;\;\;\;a\_m \cdot x-scale\_m\\
\end{array}
\end{array}
if b < 1.7999999999999999e-241Initial program 0.0%
Taylor expanded in y-scale around inf
Applied rewrites2.5%
Taylor expanded in b around inf
Applied rewrites11.4%
if 1.7999999999999999e-241 < b Initial program 0.0%
Taylor expanded in angle around 0
associate-*r*N/A
lower-*.f64N/A
lower-*.f64N/A
associate-*r*N/A
lower-*.f64N/A
lower-*.f64N/A
lower-sqrt.f64N/A
lower-sqrt.f6425.7
Applied rewrites25.7%
Applied rewrites25.8%
Taylor expanded in a around 0
Applied rewrites25.8%
x-scale_m = (fabs.f64 x-scale) b_m = (fabs.f64 b) a_m = (fabs.f64 a) (FPCore (a_m b_m angle x-scale_m y-scale) :precision binary64 (* a_m x-scale_m))
x-scale_m = fabs(x_45_scale);
b_m = fabs(b);
a_m = fabs(a);
double code(double a_m, double b_m, double angle, double x_45_scale_m, double y_45_scale) {
return a_m * x_45_scale_m;
}
x-scale_m = abs(x_45scale)
b_m = abs(b)
a_m = abs(a)
real(8) function code(a_m, b_m, angle, x_45scale_m, y_45scale)
real(8), intent (in) :: a_m
real(8), intent (in) :: b_m
real(8), intent (in) :: angle
real(8), intent (in) :: x_45scale_m
real(8), intent (in) :: y_45scale
code = a_m * x_45scale_m
end function
x-scale_m = Math.abs(x_45_scale);
b_m = Math.abs(b);
a_m = Math.abs(a);
public static double code(double a_m, double b_m, double angle, double x_45_scale_m, double y_45_scale) {
return a_m * x_45_scale_m;
}
x-scale_m = math.fabs(x_45_scale) b_m = math.fabs(b) a_m = math.fabs(a) def code(a_m, b_m, angle, x_45_scale_m, y_45_scale): return a_m * x_45_scale_m
x-scale_m = abs(x_45_scale) b_m = abs(b) a_m = abs(a) function code(a_m, b_m, angle, x_45_scale_m, y_45_scale) return Float64(a_m * x_45_scale_m) end
x-scale_m = abs(x_45_scale); b_m = abs(b); a_m = abs(a); function tmp = code(a_m, b_m, angle, x_45_scale_m, y_45_scale) tmp = a_m * x_45_scale_m; end
x-scale_m = N[Abs[x$45$scale], $MachinePrecision] b_m = N[Abs[b], $MachinePrecision] a_m = N[Abs[a], $MachinePrecision] code[a$95$m_, b$95$m_, angle_, x$45$scale$95$m_, y$45$scale_] := N[(a$95$m * x$45$scale$95$m), $MachinePrecision]
\begin{array}{l}
x-scale_m = \left|x-scale\right|
\\
b_m = \left|b\right|
\\
a_m = \left|a\right|
\\
a\_m \cdot x-scale\_m
\end{array}
Initial program 0.0%
Taylor expanded in angle around 0
associate-*r*N/A
lower-*.f64N/A
lower-*.f64N/A
associate-*r*N/A
lower-*.f64N/A
lower-*.f64N/A
lower-sqrt.f64N/A
lower-sqrt.f6423.7
Applied rewrites23.7%
Applied rewrites23.8%
Taylor expanded in a around 0
Applied rewrites23.8%
herbie shell --seed 2024323
(FPCore (a b angle x-scale y-scale)
:name "b from scale-rotated-ellipse"
:precision binary64
(/ (- (sqrt (* (* (* 2.0 (/ (* 4.0 (* (* b a) (* b (- a)))) (pow (* x-scale y-scale) 2.0))) (* (* b a) (* b (- a)))) (- (+ (/ (/ (+ (pow (* a (sin (* (/ angle 180.0) (PI)))) 2.0) (pow (* b (cos (* (/ angle 180.0) (PI)))) 2.0)) x-scale) x-scale) (/ (/ (+ (pow (* a (cos (* (/ angle 180.0) (PI)))) 2.0) (pow (* b (sin (* (/ angle 180.0) (PI)))) 2.0)) y-scale) y-scale)) (sqrt (+ (pow (- (/ (/ (+ (pow (* a (sin (* (/ angle 180.0) (PI)))) 2.0) (pow (* b (cos (* (/ angle 180.0) (PI)))) 2.0)) x-scale) x-scale) (/ (/ (+ (pow (* a (cos (* (/ angle 180.0) (PI)))) 2.0) (pow (* b (sin (* (/ angle 180.0) (PI)))) 2.0)) y-scale) y-scale)) 2.0) (pow (/ (/ (* (* (* 2.0 (- (pow b 2.0) (pow a 2.0))) (sin (* (/ angle 180.0) (PI)))) (cos (* (/ angle 180.0) (PI)))) x-scale) y-scale) 2.0))))))) (/ (* 4.0 (* (* b a) (* b (- a)))) (pow (* x-scale y-scale) 2.0))))