Diagrams.Solve.Polynomial:cubForm from diagrams-solve-0.1, D

Percentage Accurate: 97.9% → 98.7%
Time: 34.4s
Alternatives: 3
Speedup: 0.5×

Specification

?
\[\begin{array}{l} \\ \frac{1}{3} \cdot \cos^{-1} \left(\frac{3 \cdot \frac{x}{y \cdot 27}}{z \cdot 2} \cdot \sqrt{t}\right) \end{array} \]
(FPCore (x y z t)
 :precision binary64
 (* (/ 1.0 3.0) (acos (* (/ (* 3.0 (/ x (* y 27.0))) (* z 2.0)) (sqrt t)))))
double code(double x, double y, double z, double t) {
	return (1.0 / 3.0) * acos((((3.0 * (x / (y * 27.0))) / (z * 2.0)) * sqrt(t)));
}
real(8) function code(x, y, z, t)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8), intent (in) :: t
    code = (1.0d0 / 3.0d0) * acos((((3.0d0 * (x / (y * 27.0d0))) / (z * 2.0d0)) * sqrt(t)))
end function
public static double code(double x, double y, double z, double t) {
	return (1.0 / 3.0) * Math.acos((((3.0 * (x / (y * 27.0))) / (z * 2.0)) * Math.sqrt(t)));
}
def code(x, y, z, t):
	return (1.0 / 3.0) * math.acos((((3.0 * (x / (y * 27.0))) / (z * 2.0)) * math.sqrt(t)))
function code(x, y, z, t)
	return Float64(Float64(1.0 / 3.0) * acos(Float64(Float64(Float64(3.0 * Float64(x / Float64(y * 27.0))) / Float64(z * 2.0)) * sqrt(t))))
end
function tmp = code(x, y, z, t)
	tmp = (1.0 / 3.0) * acos((((3.0 * (x / (y * 27.0))) / (z * 2.0)) * sqrt(t)));
end
code[x_, y_, z_, t_] := N[(N[(1.0 / 3.0), $MachinePrecision] * N[ArcCos[N[(N[(N[(3.0 * N[(x / N[(y * 27.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(z * 2.0), $MachinePrecision]), $MachinePrecision] * N[Sqrt[t], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{1}{3} \cdot \cos^{-1} \left(\frac{3 \cdot \frac{x}{y \cdot 27}}{z \cdot 2} \cdot \sqrt{t}\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 3 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: 97.9% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{1}{3} \cdot \cos^{-1} \left(\frac{3 \cdot \frac{x}{y \cdot 27}}{z \cdot 2} \cdot \sqrt{t}\right) \end{array} \]
(FPCore (x y z t)
 :precision binary64
 (* (/ 1.0 3.0) (acos (* (/ (* 3.0 (/ x (* y 27.0))) (* z 2.0)) (sqrt t)))))
double code(double x, double y, double z, double t) {
	return (1.0 / 3.0) * acos((((3.0 * (x / (y * 27.0))) / (z * 2.0)) * sqrt(t)));
}
real(8) function code(x, y, z, t)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8), intent (in) :: t
    code = (1.0d0 / 3.0d0) * acos((((3.0d0 * (x / (y * 27.0d0))) / (z * 2.0d0)) * sqrt(t)))
end function
public static double code(double x, double y, double z, double t) {
	return (1.0 / 3.0) * Math.acos((((3.0 * (x / (y * 27.0))) / (z * 2.0)) * Math.sqrt(t)));
}
def code(x, y, z, t):
	return (1.0 / 3.0) * math.acos((((3.0 * (x / (y * 27.0))) / (z * 2.0)) * math.sqrt(t)))
function code(x, y, z, t)
	return Float64(Float64(1.0 / 3.0) * acos(Float64(Float64(Float64(3.0 * Float64(x / Float64(y * 27.0))) / Float64(z * 2.0)) * sqrt(t))))
end
function tmp = code(x, y, z, t)
	tmp = (1.0 / 3.0) * acos((((3.0 * (x / (y * 27.0))) / (z * 2.0)) * sqrt(t)));
end
code[x_, y_, z_, t_] := N[(N[(1.0 / 3.0), $MachinePrecision] * N[ArcCos[N[(N[(N[(3.0 * N[(x / N[(y * 27.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(z * 2.0), $MachinePrecision]), $MachinePrecision] * N[Sqrt[t], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{1}{3} \cdot \cos^{-1} \left(\frac{3 \cdot \frac{x}{y \cdot 27}}{z \cdot 2} \cdot \sqrt{t}\right)
\end{array}

Alternative 1: 98.7% accurate, 0.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_1 := \sin^{-1} \left(\frac{0.05555555555555555 \cdot \left(\sqrt{t} \cdot x\right)}{y \cdot z}\right)\\ \left(\left(\pi \cdot \pi\right) \cdot 0.25 - {t\_1}^{2}\right) \cdot \left(\frac{1}{\mathsf{fma}\left(\pi, 0.5, t\_1\right)} \cdot 0.3333333333333333\right) \end{array} \end{array} \]
(FPCore (x y z t)
 :precision binary64
 (let* ((t_1 (asin (/ (* 0.05555555555555555 (* (sqrt t) x)) (* y z)))))
   (*
    (- (* (* PI PI) 0.25) (pow t_1 2.0))
    (* (/ 1.0 (fma PI 0.5 t_1)) 0.3333333333333333))))
double code(double x, double y, double z, double t) {
	double t_1 = asin(((0.05555555555555555 * (sqrt(t) * x)) / (y * z)));
	return (((((double) M_PI) * ((double) M_PI)) * 0.25) - pow(t_1, 2.0)) * ((1.0 / fma(((double) M_PI), 0.5, t_1)) * 0.3333333333333333);
}
function code(x, y, z, t)
	t_1 = asin(Float64(Float64(0.05555555555555555 * Float64(sqrt(t) * x)) / Float64(y * z)))
	return Float64(Float64(Float64(Float64(pi * pi) * 0.25) - (t_1 ^ 2.0)) * Float64(Float64(1.0 / fma(pi, 0.5, t_1)) * 0.3333333333333333))
end
code[x_, y_, z_, t_] := Block[{t$95$1 = N[ArcSin[N[(N[(0.05555555555555555 * N[(N[Sqrt[t], $MachinePrecision] * x), $MachinePrecision]), $MachinePrecision] / N[(y * z), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, N[(N[(N[(N[(Pi * Pi), $MachinePrecision] * 0.25), $MachinePrecision] - N[Power[t$95$1, 2.0], $MachinePrecision]), $MachinePrecision] * N[(N[(1.0 / N[(Pi * 0.5 + t$95$1), $MachinePrecision]), $MachinePrecision] * 0.3333333333333333), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_1 := \sin^{-1} \left(\frac{0.05555555555555555 \cdot \left(\sqrt{t} \cdot x\right)}{y \cdot z}\right)\\
\left(\left(\pi \cdot \pi\right) \cdot 0.25 - {t\_1}^{2}\right) \cdot \left(\frac{1}{\mathsf{fma}\left(\pi, 0.5, t\_1\right)} \cdot 0.3333333333333333\right)
\end{array}
\end{array}
Derivation
  1. Initial program 98.5%

    \[\frac{1}{3} \cdot \cos^{-1} \left(\frac{3 \cdot \frac{x}{y \cdot 27}}{z \cdot 2} \cdot \sqrt{t}\right) \]
  2. Add Preprocessing
  3. Applied rewrites98.5%

    \[\leadsto \color{blue}{\frac{\left(\left(\pi \cdot \left(\pi \cdot \pi\right)\right) \cdot 0.125 - {\sin^{-1} \left(\frac{3 \cdot \left(x \cdot \sqrt{t}\right)}{y \cdot \left(z \cdot 54\right)}\right)}^{3}\right) \cdot 0.3333333333333333}{\mathsf{fma}\left(\sin^{-1} \left(\frac{3 \cdot \left(x \cdot \sqrt{t}\right)}{y \cdot \left(z \cdot 54\right)}\right), \mathsf{fma}\left(\pi, 0.5, \sin^{-1} \left(\frac{3 \cdot \left(x \cdot \sqrt{t}\right)}{y \cdot \left(z \cdot 54\right)}\right)\right), \left(\pi \cdot \pi\right) \cdot 0.25\right)}} \]
  4. Applied rewrites100.0%

    \[\leadsto \color{blue}{\left(\cos^{-1} \left(\frac{\left(0.05555555555555555 \cdot \sqrt{t}\right) \cdot x}{y \cdot z}\right) \cdot \frac{0.3333333333333333}{\mathsf{fma}\left(\sin^{-1} \left(\frac{\left(0.05555555555555555 \cdot \sqrt{t}\right) \cdot x}{y \cdot z}\right), \mathsf{fma}\left(\pi, 0.5, \sin^{-1} \left(\frac{\left(0.05555555555555555 \cdot \sqrt{t}\right) \cdot x}{y \cdot z}\right)\right), -0.25 \cdot \left(\pi \cdot \pi\right)\right)}\right) \cdot \mathsf{fma}\left(\sin^{-1} \left(\frac{\left(0.05555555555555555 \cdot \sqrt{t}\right) \cdot x}{y \cdot z}\right), \mathsf{fma}\left(\pi, 0.5, \sin^{-1} \left(\frac{\left(0.05555555555555555 \cdot \sqrt{t}\right) \cdot x}{y \cdot z}\right)\right), -0.25 \cdot \left(\pi \cdot \pi\right)\right)} \]
  5. Applied rewrites100.0%

    \[\leadsto \color{blue}{\left(\left(\pi \cdot \pi\right) \cdot 0.25 - {\sin^{-1} \left(\frac{0.05555555555555555 \cdot \left(\sqrt{t} \cdot x\right)}{y \cdot z}\right)}^{2}\right) \cdot \left(\frac{1}{\mathsf{fma}\left(\pi, 0.5, \sin^{-1} \left(\frac{0.05555555555555555 \cdot \left(\sqrt{t} \cdot x\right)}{y \cdot z}\right)\right)} \cdot 0.3333333333333333\right)} \]
  6. Add Preprocessing

Alternative 2: 98.6% accurate, 0.5× speedup?

\[\begin{array}{l} \\ 0.3333333333333333 \cdot {\left({\left(\sqrt{\sqrt{\cos^{-1} \left(\frac{0.05555555555555555 \cdot \left(\sqrt{t} \cdot x\right)}{y \cdot z}\right)}}\right)}^{2}\right)}^{2} \end{array} \]
(FPCore (x y z t)
 :precision binary64
 (*
  0.3333333333333333
  (pow
   (pow
    (sqrt (sqrt (acos (/ (* 0.05555555555555555 (* (sqrt t) x)) (* y z)))))
    2.0)
   2.0)))
double code(double x, double y, double z, double t) {
	return 0.3333333333333333 * pow(pow(sqrt(sqrt(acos(((0.05555555555555555 * (sqrt(t) * x)) / (y * z))))), 2.0), 2.0);
}
real(8) function code(x, y, z, t)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8), intent (in) :: t
    code = 0.3333333333333333d0 * ((sqrt(sqrt(acos(((0.05555555555555555d0 * (sqrt(t) * x)) / (y * z))))) ** 2.0d0) ** 2.0d0)
end function
public static double code(double x, double y, double z, double t) {
	return 0.3333333333333333 * Math.pow(Math.pow(Math.sqrt(Math.sqrt(Math.acos(((0.05555555555555555 * (Math.sqrt(t) * x)) / (y * z))))), 2.0), 2.0);
}
def code(x, y, z, t):
	return 0.3333333333333333 * math.pow(math.pow(math.sqrt(math.sqrt(math.acos(((0.05555555555555555 * (math.sqrt(t) * x)) / (y * z))))), 2.0), 2.0)
function code(x, y, z, t)
	return Float64(0.3333333333333333 * ((sqrt(sqrt(acos(Float64(Float64(0.05555555555555555 * Float64(sqrt(t) * x)) / Float64(y * z))))) ^ 2.0) ^ 2.0))
end
function tmp = code(x, y, z, t)
	tmp = 0.3333333333333333 * ((sqrt(sqrt(acos(((0.05555555555555555 * (sqrt(t) * x)) / (y * z))))) ^ 2.0) ^ 2.0);
end
code[x_, y_, z_, t_] := N[(0.3333333333333333 * N[Power[N[Power[N[Sqrt[N[Sqrt[N[ArcCos[N[(N[(0.05555555555555555 * N[(N[Sqrt[t], $MachinePrecision] * x), $MachinePrecision]), $MachinePrecision] / N[(y * z), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]], $MachinePrecision]], $MachinePrecision], 2.0], $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
0.3333333333333333 \cdot {\left({\left(\sqrt{\sqrt{\cos^{-1} \left(\frac{0.05555555555555555 \cdot \left(\sqrt{t} \cdot x\right)}{y \cdot z}\right)}}\right)}^{2}\right)}^{2}
\end{array}
Derivation
  1. Initial program 97.9%

    \[\frac{1}{3} \cdot \cos^{-1} \left(\frac{3 \cdot \frac{x}{y \cdot 27}}{z \cdot 2} \cdot \sqrt{t}\right) \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0

    \[\leadsto \color{blue}{\frac{1}{3} \cdot \cos^{-1} \left(\frac{1}{18} \cdot \left(\sqrt{t} \cdot \frac{x}{y \cdot z}\right)\right)} \]
  4. Step-by-step derivation
    1. lower-*.f64N/A

      \[\leadsto \color{blue}{\frac{1}{3} \cdot \cos^{-1} \left(\frac{1}{18} \cdot \left(\sqrt{t} \cdot \frac{x}{y \cdot z}\right)\right)} \]
    2. lower-acos.f64N/A

      \[\leadsto \frac{1}{3} \cdot \color{blue}{\cos^{-1} \left(\frac{1}{18} \cdot \left(\sqrt{t} \cdot \frac{x}{y \cdot z}\right)\right)} \]
    3. associate-*r/N/A

      \[\leadsto \frac{1}{3} \cdot \cos^{-1} \left(\frac{1}{18} \cdot \color{blue}{\frac{\sqrt{t} \cdot x}{y \cdot z}}\right) \]
    4. associate-*r/N/A

      \[\leadsto \frac{1}{3} \cdot \cos^{-1} \color{blue}{\left(\frac{\frac{1}{18} \cdot \left(\sqrt{t} \cdot x\right)}{y \cdot z}\right)} \]
    5. lower-/.f64N/A

      \[\leadsto \frac{1}{3} \cdot \cos^{-1} \color{blue}{\left(\frac{\frac{1}{18} \cdot \left(\sqrt{t} \cdot x\right)}{y \cdot z}\right)} \]
    6. lower-*.f64N/A

      \[\leadsto \frac{1}{3} \cdot \cos^{-1} \left(\frac{\color{blue}{\frac{1}{18} \cdot \left(\sqrt{t} \cdot x\right)}}{y \cdot z}\right) \]
    7. *-commutativeN/A

      \[\leadsto \frac{1}{3} \cdot \cos^{-1} \left(\frac{\frac{1}{18} \cdot \color{blue}{\left(x \cdot \sqrt{t}\right)}}{y \cdot z}\right) \]
    8. lower-*.f64N/A

      \[\leadsto \frac{1}{3} \cdot \cos^{-1} \left(\frac{\frac{1}{18} \cdot \color{blue}{\left(x \cdot \sqrt{t}\right)}}{y \cdot z}\right) \]
    9. lower-sqrt.f64N/A

      \[\leadsto \frac{1}{3} \cdot \cos^{-1} \left(\frac{\frac{1}{18} \cdot \left(x \cdot \color{blue}{\sqrt{t}}\right)}{y \cdot z}\right) \]
    10. lower-*.f6497.2

      \[\leadsto 0.3333333333333333 \cdot \cos^{-1} \left(\frac{0.05555555555555555 \cdot \left(x \cdot \sqrt{t}\right)}{\color{blue}{y \cdot z}}\right) \]
  5. Applied rewrites97.2%

    \[\leadsto \color{blue}{0.3333333333333333 \cdot \cos^{-1} \left(\frac{0.05555555555555555 \cdot \left(x \cdot \sqrt{t}\right)}{y \cdot z}\right)} \]
  6. Applied rewrites97.2%

    \[\leadsto 0.3333333333333333 \cdot \left(\left(\left(\pi \cdot \pi\right) \cdot 0.25 - {\sin^{-1} \left(\frac{\left(0.05555555555555555 \cdot \sqrt{t}\right) \cdot x}{y \cdot z}\right)}^{2}\right) \cdot \color{blue}{\frac{1}{\mathsf{fma}\left(\pi, 0.5, \sin^{-1} \left(\frac{\left(0.05555555555555555 \cdot \sqrt{t}\right) \cdot x}{y \cdot z}\right)\right)}}\right) \]
  7. Step-by-step derivation
    1. Applied rewrites96.3%

      \[\leadsto 0.3333333333333333 \cdot {\left(\sqrt{\cos^{-1} \left(\frac{0.05555555555555555 \cdot \left(\sqrt{t} \cdot x\right)}{y \cdot z}\right)}\right)}^{\color{blue}{2}} \]
    2. Step-by-step derivation
      1. Applied rewrites98.6%

        \[\leadsto 0.3333333333333333 \cdot {\left({\left(\sqrt{\sqrt{\cos^{-1} \left(\frac{0.05555555555555555 \cdot \left(\sqrt{t} \cdot x\right)}{y \cdot z}\right)}}\right)}^{2}\right)}^{2} \]
      2. Add Preprocessing

      Developer Target 1: 98.2% accurate, 1.0× speedup?

      \[\begin{array}{l} \\ \frac{\cos^{-1} \left(\frac{\frac{x}{27}}{y \cdot z} \cdot \frac{\sqrt{t}}{\frac{2}{3}}\right)}{3} \end{array} \]
      (FPCore (x y z t)
       :precision binary64
       (/ (acos (* (/ (/ x 27.0) (* y z)) (/ (sqrt t) (/ 2.0 3.0)))) 3.0))
      double code(double x, double y, double z, double t) {
      	return acos((((x / 27.0) / (y * z)) * (sqrt(t) / (2.0 / 3.0)))) / 3.0;
      }
      
      real(8) function code(x, y, z, t)
          real(8), intent (in) :: x
          real(8), intent (in) :: y
          real(8), intent (in) :: z
          real(8), intent (in) :: t
          code = acos((((x / 27.0d0) / (y * z)) * (sqrt(t) / (2.0d0 / 3.0d0)))) / 3.0d0
      end function
      
      public static double code(double x, double y, double z, double t) {
      	return Math.acos((((x / 27.0) / (y * z)) * (Math.sqrt(t) / (2.0 / 3.0)))) / 3.0;
      }
      
      def code(x, y, z, t):
      	return math.acos((((x / 27.0) / (y * z)) * (math.sqrt(t) / (2.0 / 3.0)))) / 3.0
      
      function code(x, y, z, t)
      	return Float64(acos(Float64(Float64(Float64(x / 27.0) / Float64(y * z)) * Float64(sqrt(t) / Float64(2.0 / 3.0)))) / 3.0)
      end
      
      function tmp = code(x, y, z, t)
      	tmp = acos((((x / 27.0) / (y * z)) * (sqrt(t) / (2.0 / 3.0)))) / 3.0;
      end
      
      code[x_, y_, z_, t_] := N[(N[ArcCos[N[(N[(N[(x / 27.0), $MachinePrecision] / N[(y * z), $MachinePrecision]), $MachinePrecision] * N[(N[Sqrt[t], $MachinePrecision] / N[(2.0 / 3.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] / 3.0), $MachinePrecision]
      
      \begin{array}{l}
      
      \\
      \frac{\cos^{-1} \left(\frac{\frac{x}{27}}{y \cdot z} \cdot \frac{\sqrt{t}}{\frac{2}{3}}\right)}{3}
      \end{array}
      

      Reproduce

      ?
      herbie shell --seed 2024223 
      (FPCore (x y z t)
        :name "Diagrams.Solve.Polynomial:cubForm  from diagrams-solve-0.1, D"
        :precision binary64
      
        :alt
        (! :herbie-platform default (/ (acos (* (/ (/ x 27) (* y z)) (/ (sqrt t) (/ 2 3)))) 3))
      
        (* (/ 1.0 3.0) (acos (* (/ (* 3.0 (/ x (* y 27.0))) (* z 2.0)) (sqrt t)))))