Toniolo and Linder, Equation (3b), real

Percentage Accurate: 94.3% → 94.3%
Time: 2.9s
Alternatives: 5
Speedup: 1.0×

Specification

?
\[\mathsf{TRUE}\left(\right)\]
\[\begin{array}{l} \\ \frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \sin th \end{array} \]
(FPCore (kx ky th)
 :precision binary64
 (* (/ (sin ky) (sqrt (+ (pow (sin kx) 2.0) (pow (sin ky) 2.0)))) (sin th)))
double code(double kx, double ky, double th) {
	return (sin(ky) / sqrt((pow(sin(kx), 2.0) + pow(sin(ky), 2.0)))) * sin(th);
}
real(8) function code(kx, ky, th)
    real(8), intent (in) :: kx
    real(8), intent (in) :: ky
    real(8), intent (in) :: th
    code = (sin(ky) / sqrt(((sin(kx) ** 2.0d0) + (sin(ky) ** 2.0d0)))) * sin(th)
end function
public static double code(double kx, double ky, double th) {
	return (Math.sin(ky) / Math.sqrt((Math.pow(Math.sin(kx), 2.0) + Math.pow(Math.sin(ky), 2.0)))) * Math.sin(th);
}
def code(kx, ky, th):
	return (math.sin(ky) / math.sqrt((math.pow(math.sin(kx), 2.0) + math.pow(math.sin(ky), 2.0)))) * math.sin(th)
function code(kx, ky, th)
	return Float64(Float64(sin(ky) / sqrt(Float64((sin(kx) ^ 2.0) + (sin(ky) ^ 2.0)))) * sin(th))
end
function tmp = code(kx, ky, th)
	tmp = (sin(ky) / sqrt(((sin(kx) ^ 2.0) + (sin(ky) ^ 2.0)))) * sin(th);
end
code[kx_, ky_, th_] := N[(N[(N[Sin[ky], $MachinePrecision] / N[Sqrt[N[(N[Power[N[Sin[kx], $MachinePrecision], 2.0], $MachinePrecision] + N[Power[N[Sin[ky], $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * N[Sin[th], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \sin th
\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 5 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: 94.3% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \sin th \end{array} \]
(FPCore (kx ky th)
 :precision binary64
 (* (/ (sin ky) (sqrt (+ (pow (sin kx) 2.0) (pow (sin ky) 2.0)))) (sin th)))
double code(double kx, double ky, double th) {
	return (sin(ky) / sqrt((pow(sin(kx), 2.0) + pow(sin(ky), 2.0)))) * sin(th);
}
real(8) function code(kx, ky, th)
    real(8), intent (in) :: kx
    real(8), intent (in) :: ky
    real(8), intent (in) :: th
    code = (sin(ky) / sqrt(((sin(kx) ** 2.0d0) + (sin(ky) ** 2.0d0)))) * sin(th)
end function
public static double code(double kx, double ky, double th) {
	return (Math.sin(ky) / Math.sqrt((Math.pow(Math.sin(kx), 2.0) + Math.pow(Math.sin(ky), 2.0)))) * Math.sin(th);
}
def code(kx, ky, th):
	return (math.sin(ky) / math.sqrt((math.pow(math.sin(kx), 2.0) + math.pow(math.sin(ky), 2.0)))) * math.sin(th)
function code(kx, ky, th)
	return Float64(Float64(sin(ky) / sqrt(Float64((sin(kx) ^ 2.0) + (sin(ky) ^ 2.0)))) * sin(th))
end
function tmp = code(kx, ky, th)
	tmp = (sin(ky) / sqrt(((sin(kx) ^ 2.0) + (sin(ky) ^ 2.0)))) * sin(th);
end
code[kx_, ky_, th_] := N[(N[(N[Sin[ky], $MachinePrecision] / N[Sqrt[N[(N[Power[N[Sin[kx], $MachinePrecision], 2.0], $MachinePrecision] + N[Power[N[Sin[ky], $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * N[Sin[th], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \sin th
\end{array}

Alternative 1: 94.3% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \sin th \end{array} \]
(FPCore (kx ky th)
 :precision binary64
 (* (/ (sin ky) (sqrt (+ (pow (sin kx) 2.0) (pow (sin ky) 2.0)))) (sin th)))
double code(double kx, double ky, double th) {
	return (sin(ky) / sqrt((pow(sin(kx), 2.0) + pow(sin(ky), 2.0)))) * sin(th);
}
real(8) function code(kx, ky, th)
    real(8), intent (in) :: kx
    real(8), intent (in) :: ky
    real(8), intent (in) :: th
    code = (sin(ky) / sqrt(((sin(kx) ** 2.0d0) + (sin(ky) ** 2.0d0)))) * sin(th)
end function
public static double code(double kx, double ky, double th) {
	return (Math.sin(ky) / Math.sqrt((Math.pow(Math.sin(kx), 2.0) + Math.pow(Math.sin(ky), 2.0)))) * Math.sin(th);
}
def code(kx, ky, th):
	return (math.sin(ky) / math.sqrt((math.pow(math.sin(kx), 2.0) + math.pow(math.sin(ky), 2.0)))) * math.sin(th)
function code(kx, ky, th)
	return Float64(Float64(sin(ky) / sqrt(Float64((sin(kx) ^ 2.0) + (sin(ky) ^ 2.0)))) * sin(th))
end
function tmp = code(kx, ky, th)
	tmp = (sin(ky) / sqrt(((sin(kx) ^ 2.0) + (sin(ky) ^ 2.0)))) * sin(th);
end
code[kx_, ky_, th_] := N[(N[(N[Sin[ky], $MachinePrecision] / N[Sqrt[N[(N[Power[N[Sin[kx], $MachinePrecision], 2.0], $MachinePrecision] + N[Power[N[Sin[ky], $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * N[Sin[th], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \sin th
\end{array}
Derivation
  1. Initial program 92.7%

    \[\frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \sin th \]
  2. Add Preprocessing
  3. Add Preprocessing

Alternative 2: 13.9% accurate, 2.1× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\sin th \leq 1.15 \cdot 10^{-50}:\\ \;\;\;\;{\sin ky}^{2}\\ \mathbf{else}:\\ \;\;\;\;\sin ky\\ \end{array} \end{array} \]
(FPCore (kx ky th)
 :precision binary64
 (if (<= (sin th) 1.15e-50) (pow (sin ky) 2.0) (sin ky)))
double code(double kx, double ky, double th) {
	double tmp;
	if (sin(th) <= 1.15e-50) {
		tmp = pow(sin(ky), 2.0);
	} else {
		tmp = sin(ky);
	}
	return tmp;
}
real(8) function code(kx, ky, th)
    real(8), intent (in) :: kx
    real(8), intent (in) :: ky
    real(8), intent (in) :: th
    real(8) :: tmp
    if (sin(th) <= 1.15d-50) then
        tmp = sin(ky) ** 2.0d0
    else
        tmp = sin(ky)
    end if
    code = tmp
end function
public static double code(double kx, double ky, double th) {
	double tmp;
	if (Math.sin(th) <= 1.15e-50) {
		tmp = Math.pow(Math.sin(ky), 2.0);
	} else {
		tmp = Math.sin(ky);
	}
	return tmp;
}
def code(kx, ky, th):
	tmp = 0
	if math.sin(th) <= 1.15e-50:
		tmp = math.pow(math.sin(ky), 2.0)
	else:
		tmp = math.sin(ky)
	return tmp
function code(kx, ky, th)
	tmp = 0.0
	if (sin(th) <= 1.15e-50)
		tmp = sin(ky) ^ 2.0;
	else
		tmp = sin(ky);
	end
	return tmp
end
function tmp_2 = code(kx, ky, th)
	tmp = 0.0;
	if (sin(th) <= 1.15e-50)
		tmp = sin(ky) ^ 2.0;
	else
		tmp = sin(ky);
	end
	tmp_2 = tmp;
end
code[kx_, ky_, th_] := If[LessEqual[N[Sin[th], $MachinePrecision], 1.15e-50], N[Power[N[Sin[ky], $MachinePrecision], 2.0], $MachinePrecision], N[Sin[ky], $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;\sin th \leq 1.15 \cdot 10^{-50}:\\
\;\;\;\;{\sin ky}^{2}\\

\mathbf{else}:\\
\;\;\;\;\sin ky\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (sin.f64 th) < 1.1500000000000001e-50

    1. Initial program 94.2%

      \[\frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \sin th \]
    2. Add Preprocessing
    3. Taylor expanded in kx around 0

      \[\leadsto \color{blue}{1} \cdot \sin th \]
    4. Applied rewrites19.0%

      \[\leadsto \color{blue}{{\sin ky}^{2}} \cdot \sin th \]
    5. Taylor expanded in kx around 0

      \[\leadsto \color{blue}{\sin th + {kx}^{2} \cdot \left(\frac{-1}{2} \cdot \frac{\sin th}{{\sin ky}^{2}} + \frac{1}{2} \cdot \left({kx}^{2} \cdot \left({\sin ky}^{2} \cdot \left(\sin th \cdot \left(\frac{1}{3} \cdot \frac{1}{{\sin ky}^{4}} + \frac{3}{4} \cdot \frac{1}{{\sin ky}^{6}}\right)\right)\right)\right)\right)} \]
    6. Applied rewrites13.2%

      \[\leadsto \color{blue}{{\sin ky}^{2}} \]

    if 1.1500000000000001e-50 < (sin.f64 th)

    1. Initial program 88.7%

      \[\frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \sin th \]
    2. Add Preprocessing
    3. Taylor expanded in kx around 0

      \[\leadsto \color{blue}{\sin th + {kx}^{2} \cdot \left(\frac{-1}{2} \cdot \frac{\sin th}{{\sin ky}^{2}} + \frac{1}{2} \cdot \left({kx}^{2} \cdot \left({\sin ky}^{2} \cdot \left(\sin th \cdot \left(\frac{1}{3} \cdot \frac{1}{{\sin ky}^{4}} + \frac{3}{4} \cdot \frac{1}{{\sin ky}^{6}}\right)\right)\right)\right)\right)} \]
    4. Applied rewrites14.2%

      \[\leadsto \color{blue}{\sin ky} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 3: 24.1% accurate, 3.1× speedup?

\[\begin{array}{l} \\ \sin ky \cdot \sin th \end{array} \]
(FPCore (kx ky th) :precision binary64 (* (sin ky) (sin th)))
double code(double kx, double ky, double th) {
	return sin(ky) * sin(th);
}
real(8) function code(kx, ky, th)
    real(8), intent (in) :: kx
    real(8), intent (in) :: ky
    real(8), intent (in) :: th
    code = sin(ky) * sin(th)
end function
public static double code(double kx, double ky, double th) {
	return Math.sin(ky) * Math.sin(th);
}
def code(kx, ky, th):
	return math.sin(ky) * math.sin(th)
function code(kx, ky, th)
	return Float64(sin(ky) * sin(th))
end
function tmp = code(kx, ky, th)
	tmp = sin(ky) * sin(th);
end
code[kx_, ky_, th_] := N[(N[Sin[ky], $MachinePrecision] * N[Sin[th], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\sin ky \cdot \sin th
\end{array}
Derivation
  1. Initial program 92.7%

    \[\frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \sin th \]
  2. Add Preprocessing
  3. Taylor expanded in kx around 0

    \[\leadsto \color{blue}{1} \cdot \sin th \]
  4. Applied rewrites16.1%

    \[\leadsto \color{blue}{{\sin ky}^{2}} \cdot \sin th \]
  5. Taylor expanded in kx around 0

    \[\leadsto \color{blue}{1} \cdot \sin th \]
  6. Applied rewrites23.8%

    \[\leadsto \color{blue}{\sin ky} \cdot \sin th \]
  7. Add Preprocessing

Alternative 4: 7.2% accurate, 6.3× speedup?

\[\begin{array}{l} \\ \sin ky \end{array} \]
(FPCore (kx ky th) :precision binary64 (sin ky))
double code(double kx, double ky, double th) {
	return sin(ky);
}
real(8) function code(kx, ky, th)
    real(8), intent (in) :: kx
    real(8), intent (in) :: ky
    real(8), intent (in) :: th
    code = sin(ky)
end function
public static double code(double kx, double ky, double th) {
	return Math.sin(ky);
}
def code(kx, ky, th):
	return math.sin(ky)
function code(kx, ky, th)
	return sin(ky)
end
function tmp = code(kx, ky, th)
	tmp = sin(ky);
end
code[kx_, ky_, th_] := N[Sin[ky], $MachinePrecision]
\begin{array}{l}

\\
\sin ky
\end{array}
Derivation
  1. Initial program 92.7%

    \[\frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \sin th \]
  2. Add Preprocessing
  3. Taylor expanded in kx around 0

    \[\leadsto \color{blue}{\sin th + {kx}^{2} \cdot \left(\frac{-1}{2} \cdot \frac{\sin th}{{\sin ky}^{2}} + \frac{1}{2} \cdot \left({kx}^{2} \cdot \left({\sin ky}^{2} \cdot \left(\sin th \cdot \left(\frac{1}{3} \cdot \frac{1}{{\sin ky}^{4}} + \frac{3}{4} \cdot \frac{1}{{\sin ky}^{6}}\right)\right)\right)\right)\right)} \]
  4. Applied rewrites6.6%

    \[\leadsto \color{blue}{\sin ky} \]
  5. Add Preprocessing

Alternative 5: 4.8% accurate, 6.3× speedup?

\[\begin{array}{l} \\ \sin kx \end{array} \]
(FPCore (kx ky th) :precision binary64 (sin kx))
double code(double kx, double ky, double th) {
	return sin(kx);
}
real(8) function code(kx, ky, th)
    real(8), intent (in) :: kx
    real(8), intent (in) :: ky
    real(8), intent (in) :: th
    code = sin(kx)
end function
public static double code(double kx, double ky, double th) {
	return Math.sin(kx);
}
def code(kx, ky, th):
	return math.sin(kx)
function code(kx, ky, th)
	return sin(kx)
end
function tmp = code(kx, ky, th)
	tmp = sin(kx);
end
code[kx_, ky_, th_] := N[Sin[kx], $MachinePrecision]
\begin{array}{l}

\\
\sin kx
\end{array}
Derivation
  1. Initial program 92.7%

    \[\frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \sin th \]
  2. Add Preprocessing
  3. Taylor expanded in kx around 0

    \[\leadsto \color{blue}{\sin th + {kx}^{2} \cdot \left(\frac{-1}{2} \cdot \frac{\sin th}{{\sin ky}^{2}} + {kx}^{2} \cdot \left(\frac{-1}{2} \cdot \left({kx}^{2} \cdot \left({\sin ky}^{2} \cdot \left(\sin th \cdot \left(\frac{-1}{2} \cdot \frac{\frac{1}{3} \cdot \frac{1}{{\sin ky}^{4}} + \frac{3}{4} \cdot \frac{1}{{\sin ky}^{6}}}{{\sin ky}^{2}} + \left(\frac{2}{45} \cdot \frac{1}{{\sin ky}^{4}} + \left(\frac{2}{3} \cdot \frac{1}{{\sin ky}^{6}} + \frac{1}{{\sin ky}^{8}}\right)\right)\right)\right)\right)\right) + \frac{1}{2} \cdot \left({\sin ky}^{2} \cdot \left(\sin th \cdot \left(\frac{1}{3} \cdot \frac{1}{{\sin ky}^{4}} + \frac{3}{4} \cdot \frac{1}{{\sin ky}^{6}}\right)\right)\right)\right)\right)} \]
  4. Applied rewrites5.4%

    \[\leadsto \color{blue}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \]
  5. Taylor expanded in kx around 0

    \[\leadsto \sqrt{{kx}^{2} + {\sin ky}^{2}} \]
  6. Applied rewrites3.6%

    \[\leadsto \sqrt{\sin kx + {\sin ky}^{2}} \]
  7. Taylor expanded in kx around 0

    \[\leadsto \sin ky + \color{blue}{\frac{1}{2} \cdot \frac{{kx}^{2}}{\sin ky}} \]
  8. Applied rewrites4.6%

    \[\leadsto \sin kx \]
  9. Add Preprocessing

Reproduce

?
herbie shell --seed 2024321 
(FPCore (kx ky th)
  :name "Toniolo and Linder, Equation (3b), real"
  :precision binary64
  :pre (TRUE)
  (* (/ (sin ky) (sqrt (+ (pow (sin kx) 2.0) (pow (sin ky) 2.0)))) (sin th)))