2-ancestry mixing, negative discriminant

Percentage Accurate: 98.5% → 99.9%
Time: 9.8s
Alternatives: 4
Speedup: 1.0×

Specification

?
\[\begin{array}{l} \\ 2 \cdot \cos \left(\frac{2 \cdot \pi}{3} + \frac{\cos^{-1} \left(\frac{-g}{h}\right)}{3}\right) \end{array} \]
(FPCore (g h)
 :precision binary64
 (* 2.0 (cos (+ (/ (* 2.0 PI) 3.0) (/ (acos (/ (- g) h)) 3.0)))))
double code(double g, double h) {
	return 2.0 * cos((((2.0 * ((double) M_PI)) / 3.0) + (acos((-g / h)) / 3.0)));
}
public static double code(double g, double h) {
	return 2.0 * Math.cos((((2.0 * Math.PI) / 3.0) + (Math.acos((-g / h)) / 3.0)));
}
def code(g, h):
	return 2.0 * math.cos((((2.0 * math.pi) / 3.0) + (math.acos((-g / h)) / 3.0)))
function code(g, h)
	return Float64(2.0 * cos(Float64(Float64(Float64(2.0 * pi) / 3.0) + Float64(acos(Float64(Float64(-g) / h)) / 3.0))))
end
function tmp = code(g, h)
	tmp = 2.0 * cos((((2.0 * pi) / 3.0) + (acos((-g / h)) / 3.0)));
end
code[g_, h_] := N[(2.0 * N[Cos[N[(N[(N[(2.0 * Pi), $MachinePrecision] / 3.0), $MachinePrecision] + N[(N[ArcCos[N[((-g) / h), $MachinePrecision]], $MachinePrecision] / 3.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
2 \cdot \cos \left(\frac{2 \cdot \pi}{3} + \frac{\cos^{-1} \left(\frac{-g}{h}\right)}{3}\right)
\end{array}

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 4 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 98.5% accurate, 1.0× speedup?

\[\begin{array}{l} \\ 2 \cdot \cos \left(\frac{2 \cdot \pi}{3} + \frac{\cos^{-1} \left(\frac{-g}{h}\right)}{3}\right) \end{array} \]
(FPCore (g h)
 :precision binary64
 (* 2.0 (cos (+ (/ (* 2.0 PI) 3.0) (/ (acos (/ (- g) h)) 3.0)))))
double code(double g, double h) {
	return 2.0 * cos((((2.0 * ((double) M_PI)) / 3.0) + (acos((-g / h)) / 3.0)));
}
public static double code(double g, double h) {
	return 2.0 * Math.cos((((2.0 * Math.PI) / 3.0) + (Math.acos((-g / h)) / 3.0)));
}
def code(g, h):
	return 2.0 * math.cos((((2.0 * math.pi) / 3.0) + (math.acos((-g / h)) / 3.0)))
function code(g, h)
	return Float64(2.0 * cos(Float64(Float64(Float64(2.0 * pi) / 3.0) + Float64(acos(Float64(Float64(-g) / h)) / 3.0))))
end
function tmp = code(g, h)
	tmp = 2.0 * cos((((2.0 * pi) / 3.0) + (acos((-g / h)) / 3.0)));
end
code[g_, h_] := N[(2.0 * N[Cos[N[(N[(N[(2.0 * Pi), $MachinePrecision] / 3.0), $MachinePrecision] + N[(N[ArcCos[N[((-g) / h), $MachinePrecision]], $MachinePrecision] / 3.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
2 \cdot \cos \left(\frac{2 \cdot \pi}{3} + \frac{\cos^{-1} \left(\frac{-g}{h}\right)}{3}\right)
\end{array}

Alternative 1: 99.9% accurate, 0.1× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{\sqrt{3}}{2}\\ t_1 := t\_0 \cdot t\_0\\ t_2 := \cos^{-1} \left(-\frac{g}{h}\right) \cdot 0.3333333333333333\\ t_3 := \cos t\_2\\ t_4 := \left(0.25 - t\_1\right) \cdot t\_3\\ t_5 := \sin t\_2 \cdot \left(\left(t\_0 \cdot 0.5\right) \cdot -2\right)\\ 2 \cdot \frac{{t\_4}^{3} + {t\_5}^{3}}{{t\_4}^{2} + \left(t\_5 \cdot t\_5 + t\_5 \cdot \left(t\_3 \cdot \left(t\_1 - 0.25\right)\right)\right)} \end{array} \end{array} \]
(FPCore (g h)
 :precision binary64
 (let* ((t_0 (/ (sqrt 3.0) 2.0))
        (t_1 (* t_0 t_0))
        (t_2 (* (acos (- (/ g h))) 0.3333333333333333))
        (t_3 (cos t_2))
        (t_4 (* (- 0.25 t_1) t_3))
        (t_5 (* (sin t_2) (* (* t_0 0.5) -2.0))))
   (*
    2.0
    (/
     (+ (pow t_4 3.0) (pow t_5 3.0))
     (+ (pow t_4 2.0) (+ (* t_5 t_5) (* t_5 (* t_3 (- t_1 0.25)))))))))
double code(double g, double h) {
	double t_0 = sqrt(3.0) / 2.0;
	double t_1 = t_0 * t_0;
	double t_2 = acos(-(g / h)) * 0.3333333333333333;
	double t_3 = cos(t_2);
	double t_4 = (0.25 - t_1) * t_3;
	double t_5 = sin(t_2) * ((t_0 * 0.5) * -2.0);
	return 2.0 * ((pow(t_4, 3.0) + pow(t_5, 3.0)) / (pow(t_4, 2.0) + ((t_5 * t_5) + (t_5 * (t_3 * (t_1 - 0.25))))));
}
real(8) function code(g, h)
    real(8), intent (in) :: g
    real(8), intent (in) :: h
    real(8) :: t_0
    real(8) :: t_1
    real(8) :: t_2
    real(8) :: t_3
    real(8) :: t_4
    real(8) :: t_5
    t_0 = sqrt(3.0d0) / 2.0d0
    t_1 = t_0 * t_0
    t_2 = acos(-(g / h)) * 0.3333333333333333d0
    t_3 = cos(t_2)
    t_4 = (0.25d0 - t_1) * t_3
    t_5 = sin(t_2) * ((t_0 * 0.5d0) * (-2.0d0))
    code = 2.0d0 * (((t_4 ** 3.0d0) + (t_5 ** 3.0d0)) / ((t_4 ** 2.0d0) + ((t_5 * t_5) + (t_5 * (t_3 * (t_1 - 0.25d0))))))
end function
public static double code(double g, double h) {
	double t_0 = Math.sqrt(3.0) / 2.0;
	double t_1 = t_0 * t_0;
	double t_2 = Math.acos(-(g / h)) * 0.3333333333333333;
	double t_3 = Math.cos(t_2);
	double t_4 = (0.25 - t_1) * t_3;
	double t_5 = Math.sin(t_2) * ((t_0 * 0.5) * -2.0);
	return 2.0 * ((Math.pow(t_4, 3.0) + Math.pow(t_5, 3.0)) / (Math.pow(t_4, 2.0) + ((t_5 * t_5) + (t_5 * (t_3 * (t_1 - 0.25))))));
}
def code(g, h):
	t_0 = math.sqrt(3.0) / 2.0
	t_1 = t_0 * t_0
	t_2 = math.acos(-(g / h)) * 0.3333333333333333
	t_3 = math.cos(t_2)
	t_4 = (0.25 - t_1) * t_3
	t_5 = math.sin(t_2) * ((t_0 * 0.5) * -2.0)
	return 2.0 * ((math.pow(t_4, 3.0) + math.pow(t_5, 3.0)) / (math.pow(t_4, 2.0) + ((t_5 * t_5) + (t_5 * (t_3 * (t_1 - 0.25))))))
function code(g, h)
	t_0 = Float64(sqrt(3.0) / 2.0)
	t_1 = Float64(t_0 * t_0)
	t_2 = Float64(acos(Float64(-Float64(g / h))) * 0.3333333333333333)
	t_3 = cos(t_2)
	t_4 = Float64(Float64(0.25 - t_1) * t_3)
	t_5 = Float64(sin(t_2) * Float64(Float64(t_0 * 0.5) * -2.0))
	return Float64(2.0 * Float64(Float64((t_4 ^ 3.0) + (t_5 ^ 3.0)) / Float64((t_4 ^ 2.0) + Float64(Float64(t_5 * t_5) + Float64(t_5 * Float64(t_3 * Float64(t_1 - 0.25)))))))
end
function tmp = code(g, h)
	t_0 = sqrt(3.0) / 2.0;
	t_1 = t_0 * t_0;
	t_2 = acos(-(g / h)) * 0.3333333333333333;
	t_3 = cos(t_2);
	t_4 = (0.25 - t_1) * t_3;
	t_5 = sin(t_2) * ((t_0 * 0.5) * -2.0);
	tmp = 2.0 * (((t_4 ^ 3.0) + (t_5 ^ 3.0)) / ((t_4 ^ 2.0) + ((t_5 * t_5) + (t_5 * (t_3 * (t_1 - 0.25))))));
end
code[g_, h_] := Block[{t$95$0 = N[(N[Sqrt[3.0], $MachinePrecision] / 2.0), $MachinePrecision]}, Block[{t$95$1 = N[(t$95$0 * t$95$0), $MachinePrecision]}, Block[{t$95$2 = N[(N[ArcCos[(-N[(g / h), $MachinePrecision])], $MachinePrecision] * 0.3333333333333333), $MachinePrecision]}, Block[{t$95$3 = N[Cos[t$95$2], $MachinePrecision]}, Block[{t$95$4 = N[(N[(0.25 - t$95$1), $MachinePrecision] * t$95$3), $MachinePrecision]}, Block[{t$95$5 = N[(N[Sin[t$95$2], $MachinePrecision] * N[(N[(t$95$0 * 0.5), $MachinePrecision] * -2.0), $MachinePrecision]), $MachinePrecision]}, N[(2.0 * N[(N[(N[Power[t$95$4, 3.0], $MachinePrecision] + N[Power[t$95$5, 3.0], $MachinePrecision]), $MachinePrecision] / N[(N[Power[t$95$4, 2.0], $MachinePrecision] + N[(N[(t$95$5 * t$95$5), $MachinePrecision] + N[(t$95$5 * N[(t$95$3 * N[(t$95$1 - 0.25), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{\sqrt{3}}{2}\\
t_1 := t\_0 \cdot t\_0\\
t_2 := \cos^{-1} \left(-\frac{g}{h}\right) \cdot 0.3333333333333333\\
t_3 := \cos t\_2\\
t_4 := \left(0.25 - t\_1\right) \cdot t\_3\\
t_5 := \sin t\_2 \cdot \left(\left(t\_0 \cdot 0.5\right) \cdot -2\right)\\
2 \cdot \frac{{t\_4}^{3} + {t\_5}^{3}}{{t\_4}^{2} + \left(t\_5 \cdot t\_5 + t\_5 \cdot \left(t\_3 \cdot \left(t\_1 - 0.25\right)\right)\right)}
\end{array}
\end{array}
Derivation
  1. Initial program 98.4%

    \[2 \cdot \cos \left(\frac{2 \cdot \pi}{3} + \frac{\cos^{-1} \left(\frac{-g}{h}\right)}{3}\right) \]
  2. Add Preprocessing
  3. Applied rewrites100.0%

    \[\leadsto 2 \cdot \color{blue}{\frac{{\left(\left(0.25 - \frac{\sqrt{3}}{2} \cdot \frac{\sqrt{3}}{2}\right) \cdot \cos \left(\cos^{-1} \left(\frac{g}{-h}\right) \cdot 0.3333333333333333\right)\right)}^{3} + {\left(\sin \left(\cos^{-1} \left(\frac{g}{-h}\right) \cdot 0.3333333333333333\right) \cdot \left(-2 \cdot \left(\frac{\sqrt{3}}{2} \cdot 0.5\right)\right)\right)}^{3}}{{\left(\left(0.25 - \frac{\sqrt{3}}{2} \cdot \frac{\sqrt{3}}{2}\right) \cdot \cos \left(\cos^{-1} \left(\frac{g}{-h}\right) \cdot 0.3333333333333333\right)\right)}^{2} + \left(\left(\sin \left(\cos^{-1} \left(\frac{g}{-h}\right) \cdot 0.3333333333333333\right) \cdot \left(-2 \cdot \left(\frac{\sqrt{3}}{2} \cdot 0.5\right)\right)\right) \cdot \left(\sin \left(\cos^{-1} \left(\frac{g}{-h}\right) \cdot 0.3333333333333333\right) \cdot \left(-2 \cdot \left(\frac{\sqrt{3}}{2} \cdot 0.5\right)\right)\right) - \left(\left(0.25 - \frac{\sqrt{3}}{2} \cdot \frac{\sqrt{3}}{2}\right) \cdot \cos \left(\cos^{-1} \left(\frac{g}{-h}\right) \cdot 0.3333333333333333\right)\right) \cdot \left(\sin \left(\cos^{-1} \left(\frac{g}{-h}\right) \cdot 0.3333333333333333\right) \cdot \left(-2 \cdot \left(\frac{\sqrt{3}}{2} \cdot 0.5\right)\right)\right)\right)}} \]
  4. Final simplification100.0%

    \[\leadsto 2 \cdot \frac{{\left(\left(0.25 - \frac{\sqrt{3}}{2} \cdot \frac{\sqrt{3}}{2}\right) \cdot \cos \left(\cos^{-1} \left(-\frac{g}{h}\right) \cdot 0.3333333333333333\right)\right)}^{3} + {\left(\sin \left(\cos^{-1} \left(-\frac{g}{h}\right) \cdot 0.3333333333333333\right) \cdot \left(\left(\frac{\sqrt{3}}{2} \cdot 0.5\right) \cdot -2\right)\right)}^{3}}{{\left(\left(0.25 - \frac{\sqrt{3}}{2} \cdot \frac{\sqrt{3}}{2}\right) \cdot \cos \left(\cos^{-1} \left(-\frac{g}{h}\right) \cdot 0.3333333333333333\right)\right)}^{2} + \left(\left(\sin \left(\cos^{-1} \left(-\frac{g}{h}\right) \cdot 0.3333333333333333\right) \cdot \left(\left(\frac{\sqrt{3}}{2} \cdot 0.5\right) \cdot -2\right)\right) \cdot \left(\sin \left(\cos^{-1} \left(-\frac{g}{h}\right) \cdot 0.3333333333333333\right) \cdot \left(\left(\frac{\sqrt{3}}{2} \cdot 0.5\right) \cdot -2\right)\right) + \left(\sin \left(\cos^{-1} \left(-\frac{g}{h}\right) \cdot 0.3333333333333333\right) \cdot \left(\left(\frac{\sqrt{3}}{2} \cdot 0.5\right) \cdot -2\right)\right) \cdot \left(\cos \left(\cos^{-1} \left(-\frac{g}{h}\right) \cdot 0.3333333333333333\right) \cdot \left(\frac{\sqrt{3}}{2} \cdot \frac{\sqrt{3}}{2} - 0.25\right)\right)\right)} \]
  5. Add Preprocessing

Alternative 2: 100.0% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos^{-1} \left(-\frac{g}{h}\right) \cdot 0.3333333333333333\\ -\mathsf{fma}\left(\sqrt{3}, \sin t\_0, \cos t\_0\right) \end{array} \end{array} \]
(FPCore (g h)
 :precision binary64
 (let* ((t_0 (* (acos (- (/ g h))) 0.3333333333333333)))
   (- (fma (sqrt 3.0) (sin t_0) (cos t_0)))))
double code(double g, double h) {
	double t_0 = acos(-(g / h)) * 0.3333333333333333;
	return -fma(sqrt(3.0), sin(t_0), cos(t_0));
}
function code(g, h)
	t_0 = Float64(acos(Float64(-Float64(g / h))) * 0.3333333333333333)
	return Float64(-fma(sqrt(3.0), sin(t_0), cos(t_0)))
end
code[g_, h_] := Block[{t$95$0 = N[(N[ArcCos[(-N[(g / h), $MachinePrecision])], $MachinePrecision] * 0.3333333333333333), $MachinePrecision]}, (-N[(N[Sqrt[3.0], $MachinePrecision] * N[Sin[t$95$0], $MachinePrecision] + N[Cos[t$95$0], $MachinePrecision]), $MachinePrecision])]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos^{-1} \left(-\frac{g}{h}\right) \cdot 0.3333333333333333\\
-\mathsf{fma}\left(\sqrt{3}, \sin t\_0, \cos t\_0\right)
\end{array}
\end{array}
Derivation
  1. Initial program 98.5%

    \[2 \cdot \cos \left(\frac{2 \cdot \pi}{3} + \frac{\cos^{-1} \left(\frac{-g}{h}\right)}{3}\right) \]
  2. Add Preprocessing
  3. Applied rewrites99.9%

    \[\leadsto 2 \cdot \color{blue}{\frac{{\left(\left(0.25 - \frac{\sqrt{3}}{2} \cdot \frac{\sqrt{3}}{2}\right) \cdot \cos \left(\cos^{-1} \left(\frac{g}{-h}\right) \cdot 0.3333333333333333\right)\right)}^{3} + {\left(\sin \left(\cos^{-1} \left(\frac{g}{-h}\right) \cdot 0.3333333333333333\right) \cdot \left(-2 \cdot \left(\frac{\sqrt{3}}{2} \cdot 0.5\right)\right)\right)}^{3}}{{\left(\left(0.25 - \frac{\sqrt{3}}{2} \cdot \frac{\sqrt{3}}{2}\right) \cdot \cos \left(\cos^{-1} \left(\frac{g}{-h}\right) \cdot 0.3333333333333333\right)\right)}^{2} + \left(\left(\sin \left(\cos^{-1} \left(\frac{g}{-h}\right) \cdot 0.3333333333333333\right) \cdot \left(-2 \cdot \left(\frac{\sqrt{3}}{2} \cdot 0.5\right)\right)\right) \cdot \left(\sin \left(\cos^{-1} \left(\frac{g}{-h}\right) \cdot 0.3333333333333333\right) \cdot \left(-2 \cdot \left(\frac{\sqrt{3}}{2} \cdot 0.5\right)\right)\right) - \left(\left(0.25 - \frac{\sqrt{3}}{2} \cdot \frac{\sqrt{3}}{2}\right) \cdot \cos \left(\cos^{-1} \left(\frac{g}{-h}\right) \cdot 0.3333333333333333\right)\right) \cdot \left(\sin \left(\cos^{-1} \left(\frac{g}{-h}\right) \cdot 0.3333333333333333\right) \cdot \left(-2 \cdot \left(\frac{\sqrt{3}}{2} \cdot 0.5\right)\right)\right)\right)}} \]
  4. Applied rewrites100.0%

    \[\leadsto 2 \cdot \color{blue}{\mathsf{fma}\left(\sqrt{3} \cdot -0.5, \sin \left(\cos^{-1} \left(-\frac{g}{h}\right) \cdot 0.3333333333333333\right), -0.5 \cdot \cos \left(\cos^{-1} \left(-\frac{g}{h}\right) \cdot 0.3333333333333333\right)\right)} \]
  5. Taylor expanded in g around 0

    \[\leadsto \color{blue}{2 \cdot \left(\frac{-1}{2} \cdot \cos \left(\frac{1}{3} \cdot \cos^{-1} \left(\mathsf{neg}\left(\frac{g}{h}\right)\right)\right) + \frac{-1}{2} \cdot \left(\sin \left(\frac{1}{3} \cdot \cos^{-1} \left(\mathsf{neg}\left(\frac{g}{h}\right)\right)\right) \cdot \sqrt{3}\right)\right)} \]
  6. Applied rewrites100.0%

    \[\leadsto \color{blue}{-\mathsf{fma}\left(\sqrt{3}, \sin \left(\cos^{-1} \left(\frac{-g}{h}\right) \cdot 0.3333333333333333\right), \cos \left(\cos^{-1} \left(\frac{-g}{h}\right) \cdot 0.3333333333333333\right)\right)} \]
  7. Final simplification100.0%

    \[\leadsto -\mathsf{fma}\left(\sqrt{3}, \sin \left(\cos^{-1} \left(-\frac{g}{h}\right) \cdot 0.3333333333333333\right), \cos \left(\cos^{-1} \left(-\frac{g}{h}\right) \cdot 0.3333333333333333\right)\right) \]
  8. Add Preprocessing

Reproduce

?
herbie shell --seed 2024227 
(FPCore (g h)
  :name "2-ancestry mixing, negative discriminant"
  :precision binary64
  (* 2.0 (cos (+ (/ (* 2.0 PI) 3.0) (/ (acos (/ (- g) h)) 3.0)))))