Trowbridge-Reitz Sample, sample surface normal, cosTheta

Percentage Accurate: 99.4% → 99.4%
Time: 2.0min
Alternatives: 3
Speedup: 1.8×

Specification

?
\[\left(\left(\left(2.328306437 \cdot 10^{-10} \leq u0 \land u0 \leq 1\right) \land \left(2.328306437 \cdot 10^{-10} \leq u1 \land u1 \leq 0.5\right)\right) \land \left(0.0001 \leq alphax \land alphax \leq 1\right)\right) \land \left(0.0001 \leq alphay \land alphay \leq 1\right)\]
\[\begin{array}{l} \\ \begin{array}{l} t_0 := \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\left(2 \cdot \pi\right) \cdot u1 + 0.5 \cdot \pi\right)\right)\\ t_1 := \sin t_0\\ t_2 := \cos t_0\\ \frac{1}{\sqrt{1 + \frac{\frac{1}{\frac{t_2 \cdot t_2}{alphax \cdot alphax} + \frac{t_1 \cdot t_1}{alphay \cdot alphay}} \cdot u0}{1 - u0}}} \end{array} \end{array} \]
(FPCore (u0 u1 alphax alphay)
 :precision binary32
 (let* ((t_0
         (atan (* (/ alphay alphax) (tan (+ (* (* 2.0 PI) u1) (* 0.5 PI))))))
        (t_1 (sin t_0))
        (t_2 (cos t_0)))
   (/
    1.0
    (sqrt
     (+
      1.0
      (/
       (*
        (/
         1.0
         (+
          (/ (* t_2 t_2) (* alphax alphax))
          (/ (* t_1 t_1) (* alphay alphay))))
        u0)
       (- 1.0 u0)))))))
float code(float u0, float u1, float alphax, float alphay) {
	float t_0 = atanf(((alphay / alphax) * tanf((((2.0f * ((float) M_PI)) * u1) + (0.5f * ((float) M_PI))))));
	float t_1 = sinf(t_0);
	float t_2 = cosf(t_0);
	return 1.0f / sqrtf((1.0f + (((1.0f / (((t_2 * t_2) / (alphax * alphax)) + ((t_1 * t_1) / (alphay * alphay)))) * u0) / (1.0f - u0))));
}
function code(u0, u1, alphax, alphay)
	t_0 = atan(Float32(Float32(alphay / alphax) * tan(Float32(Float32(Float32(Float32(2.0) * Float32(pi)) * u1) + Float32(Float32(0.5) * Float32(pi))))))
	t_1 = sin(t_0)
	t_2 = cos(t_0)
	return Float32(Float32(1.0) / sqrt(Float32(Float32(1.0) + Float32(Float32(Float32(Float32(1.0) / Float32(Float32(Float32(t_2 * t_2) / Float32(alphax * alphax)) + Float32(Float32(t_1 * t_1) / Float32(alphay * alphay)))) * u0) / Float32(Float32(1.0) - u0)))))
end
function tmp = code(u0, u1, alphax, alphay)
	t_0 = atan(((alphay / alphax) * tan((((single(2.0) * single(pi)) * u1) + (single(0.5) * single(pi))))));
	t_1 = sin(t_0);
	t_2 = cos(t_0);
	tmp = single(1.0) / sqrt((single(1.0) + (((single(1.0) / (((t_2 * t_2) / (alphax * alphax)) + ((t_1 * t_1) / (alphay * alphay)))) * u0) / (single(1.0) - u0))));
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\left(2 \cdot \pi\right) \cdot u1 + 0.5 \cdot \pi\right)\right)\\
t_1 := \sin t_0\\
t_2 := \cos t_0\\
\frac{1}{\sqrt{1 + \frac{\frac{1}{\frac{t_2 \cdot t_2}{alphax \cdot alphax} + \frac{t_1 \cdot t_1}{alphay \cdot alphay}} \cdot u0}{1 - u0}}}
\end{array}
\end{array}

Sampling outcomes in binary32 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: 99.4% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\left(2 \cdot \pi\right) \cdot u1 + 0.5 \cdot \pi\right)\right)\\ t_1 := \sin t_0\\ t_2 := \cos t_0\\ \frac{1}{\sqrt{1 + \frac{\frac{1}{\frac{t_2 \cdot t_2}{alphax \cdot alphax} + \frac{t_1 \cdot t_1}{alphay \cdot alphay}} \cdot u0}{1 - u0}}} \end{array} \end{array} \]
(FPCore (u0 u1 alphax alphay)
 :precision binary32
 (let* ((t_0
         (atan (* (/ alphay alphax) (tan (+ (* (* 2.0 PI) u1) (* 0.5 PI))))))
        (t_1 (sin t_0))
        (t_2 (cos t_0)))
   (/
    1.0
    (sqrt
     (+
      1.0
      (/
       (*
        (/
         1.0
         (+
          (/ (* t_2 t_2) (* alphax alphax))
          (/ (* t_1 t_1) (* alphay alphay))))
        u0)
       (- 1.0 u0)))))))
float code(float u0, float u1, float alphax, float alphay) {
	float t_0 = atanf(((alphay / alphax) * tanf((((2.0f * ((float) M_PI)) * u1) + (0.5f * ((float) M_PI))))));
	float t_1 = sinf(t_0);
	float t_2 = cosf(t_0);
	return 1.0f / sqrtf((1.0f + (((1.0f / (((t_2 * t_2) / (alphax * alphax)) + ((t_1 * t_1) / (alphay * alphay)))) * u0) / (1.0f - u0))));
}
function code(u0, u1, alphax, alphay)
	t_0 = atan(Float32(Float32(alphay / alphax) * tan(Float32(Float32(Float32(Float32(2.0) * Float32(pi)) * u1) + Float32(Float32(0.5) * Float32(pi))))))
	t_1 = sin(t_0)
	t_2 = cos(t_0)
	return Float32(Float32(1.0) / sqrt(Float32(Float32(1.0) + Float32(Float32(Float32(Float32(1.0) / Float32(Float32(Float32(t_2 * t_2) / Float32(alphax * alphax)) + Float32(Float32(t_1 * t_1) / Float32(alphay * alphay)))) * u0) / Float32(Float32(1.0) - u0)))))
end
function tmp = code(u0, u1, alphax, alphay)
	t_0 = atan(((alphay / alphax) * tan((((single(2.0) * single(pi)) * u1) + (single(0.5) * single(pi))))));
	t_1 = sin(t_0);
	t_2 = cos(t_0);
	tmp = single(1.0) / sqrt((single(1.0) + (((single(1.0) / (((t_2 * t_2) / (alphax * alphax)) + ((t_1 * t_1) / (alphay * alphay)))) * u0) / (single(1.0) - u0))));
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\left(2 \cdot \pi\right) \cdot u1 + 0.5 \cdot \pi\right)\right)\\
t_1 := \sin t_0\\
t_2 := \cos t_0\\
\frac{1}{\sqrt{1 + \frac{\frac{1}{\frac{t_2 \cdot t_2}{alphax \cdot alphax} + \frac{t_1 \cdot t_1}{alphay \cdot alphay}} \cdot u0}{1 - u0}}}
\end{array}
\end{array}

Alternative 1: 99.4% accurate, 1.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \mathsf{fma}\left(2, u1, 0.5\right)\right)\\ \frac{1}{\sqrt{1 + \frac{u0}{{\left(\mathsf{hypot}\left(\frac{\sin \tan^{-1} t_0}{alphay}, \frac{\frac{1}{\mathsf{hypot}\left(1, t_0\right)}}{alphax}\right)\right)}^{2} \cdot \left(1 - u0\right)}}} \end{array} \end{array} \]
(FPCore (u0 u1 alphax alphay)
 :precision binary32
 (let* ((t_0 (* (/ alphay alphax) (tan (* PI (fma 2.0 u1 0.5))))))
   (/
    1.0
    (sqrt
     (+
      1.0
      (/
       u0
       (*
        (pow
         (hypot (/ (sin (atan t_0)) alphay) (/ (/ 1.0 (hypot 1.0 t_0)) alphax))
         2.0)
        (- 1.0 u0))))))))
float code(float u0, float u1, float alphax, float alphay) {
	float t_0 = (alphay / alphax) * tanf((((float) M_PI) * fmaf(2.0f, u1, 0.5f)));
	return 1.0f / sqrtf((1.0f + (u0 / (powf(hypotf((sinf(atanf(t_0)) / alphay), ((1.0f / hypotf(1.0f, t_0)) / alphax)), 2.0f) * (1.0f - u0)))));
}
function code(u0, u1, alphax, alphay)
	t_0 = Float32(Float32(alphay / alphax) * tan(Float32(Float32(pi) * fma(Float32(2.0), u1, Float32(0.5)))))
	return Float32(Float32(1.0) / sqrt(Float32(Float32(1.0) + Float32(u0 / Float32((hypot(Float32(sin(atan(t_0)) / alphay), Float32(Float32(Float32(1.0) / hypot(Float32(1.0), t_0)) / alphax)) ^ Float32(2.0)) * Float32(Float32(1.0) - u0))))))
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \mathsf{fma}\left(2, u1, 0.5\right)\right)\\
\frac{1}{\sqrt{1 + \frac{u0}{{\left(\mathsf{hypot}\left(\frac{\sin \tan^{-1} t_0}{alphay}, \frac{\frac{1}{\mathsf{hypot}\left(1, t_0\right)}}{alphax}\right)\right)}^{2} \cdot \left(1 - u0\right)}}}
\end{array}
\end{array}
Derivation
  1. Initial program 99.5%

    \[\frac{1}{\sqrt{1 + \frac{\frac{1}{\frac{\cos \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\left(2 \cdot \pi\right) \cdot u1 + 0.5 \cdot \pi\right)\right) \cdot \cos \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\left(2 \cdot \pi\right) \cdot u1 + 0.5 \cdot \pi\right)\right)}{alphax \cdot alphax} + \frac{\sin \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\left(2 \cdot \pi\right) \cdot u1 + 0.5 \cdot \pi\right)\right) \cdot \sin \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\left(2 \cdot \pi\right) \cdot u1 + 0.5 \cdot \pi\right)\right)}{alphay \cdot alphay}} \cdot u0}{1 - u0}}} \]
  2. Simplified99.5%

    \[\leadsto \color{blue}{\frac{1}{\sqrt{1 + \frac{u0}{\mathsf{fma}\left(\frac{\sin \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \left(2 \cdot u1 + 0.5\right)\right)\right)}{alphay}, \frac{\sin \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \left(2 \cdot u1 + 0.5\right)\right)\right)}{alphay}, \cos \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \left(2 \cdot u1 + 0.5\right)\right)\right) \cdot \frac{\cos \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \left(2 \cdot u1 + 0.5\right)\right)\right)}{alphax \cdot alphax}\right) \cdot \left(1 - u0\right)}}}} \]
  3. Step-by-step derivation
    1. expm1-log1p-u99.5%

      \[\leadsto \frac{1}{\sqrt{1 + \frac{u0}{\color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\mathsf{fma}\left(\frac{\sin \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \left(2 \cdot u1 + 0.5\right)\right)\right)}{alphay}, \frac{\sin \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \left(2 \cdot u1 + 0.5\right)\right)\right)}{alphay}, \cos \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \left(2 \cdot u1 + 0.5\right)\right)\right) \cdot \frac{\cos \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \left(2 \cdot u1 + 0.5\right)\right)\right)}{alphax \cdot alphax}\right)\right)\right)} \cdot \left(1 - u0\right)}}} \]
    2. expm1-udef99.5%

      \[\leadsto \frac{1}{\sqrt{1 + \frac{u0}{\color{blue}{\left(e^{\mathsf{log1p}\left(\mathsf{fma}\left(\frac{\sin \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \left(2 \cdot u1 + 0.5\right)\right)\right)}{alphay}, \frac{\sin \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \left(2 \cdot u1 + 0.5\right)\right)\right)}{alphay}, \cos \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \left(2 \cdot u1 + 0.5\right)\right)\right) \cdot \frac{\cos \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \left(2 \cdot u1 + 0.5\right)\right)\right)}{alphax \cdot alphax}\right)\right)} - 1\right)} \cdot \left(1 - u0\right)}}} \]
  4. Applied egg-rr99.4%

    \[\leadsto \frac{1}{\sqrt{1 + \frac{u0}{\color{blue}{\left(e^{\mathsf{log1p}\left({\left(\mathsf{hypot}\left(\frac{\sin \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \mathsf{fma}\left(2, u1, 0.5\right)\right)\right)}{alphay}, \frac{\cos \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \mathsf{fma}\left(2, u1, 0.5\right)\right)\right)}{alphax}\right)\right)}^{2}\right)} - 1\right)} \cdot \left(1 - u0\right)}}} \]
  5. Step-by-step derivation
    1. expm1-def99.4%

      \[\leadsto \frac{1}{\sqrt{1 + \frac{u0}{\color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left({\left(\mathsf{hypot}\left(\frac{\sin \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \mathsf{fma}\left(2, u1, 0.5\right)\right)\right)}{alphay}, \frac{\cos \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \mathsf{fma}\left(2, u1, 0.5\right)\right)\right)}{alphax}\right)\right)}^{2}\right)\right)} \cdot \left(1 - u0\right)}}} \]
    2. expm1-log1p99.4%

      \[\leadsto \frac{1}{\sqrt{1 + \frac{u0}{\color{blue}{{\left(\mathsf{hypot}\left(\frac{\sin \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \mathsf{fma}\left(2, u1, 0.5\right)\right)\right)}{alphay}, \frac{\cos \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \mathsf{fma}\left(2, u1, 0.5\right)\right)\right)}{alphax}\right)\right)}^{2}} \cdot \left(1 - u0\right)}}} \]
  6. Simplified99.4%

    \[\leadsto \frac{1}{\sqrt{1 + \frac{u0}{\color{blue}{{\left(\mathsf{hypot}\left(\frac{\sin \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \mathsf{fma}\left(2, u1, 0.5\right)\right)\right)}{alphay}, \frac{\cos \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \mathsf{fma}\left(2, u1, 0.5\right)\right)\right)}{alphax}\right)\right)}^{2}} \cdot \left(1 - u0\right)}}} \]
  7. Step-by-step derivation
    1. cos-atan99.5%

      \[\leadsto \frac{1}{\sqrt{1 + \frac{u0}{{\left(\mathsf{hypot}\left(\frac{\sin \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \mathsf{fma}\left(2, u1, 0.5\right)\right)\right)}{alphay}, \frac{\color{blue}{\frac{1}{\sqrt{1 + \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \mathsf{fma}\left(2, u1, 0.5\right)\right)\right) \cdot \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \mathsf{fma}\left(2, u1, 0.5\right)\right)\right)}}}}{alphax}\right)\right)}^{2} \cdot \left(1 - u0\right)}}} \]
    2. hypot-1-def99.5%

      \[\leadsto \frac{1}{\sqrt{1 + \frac{u0}{{\left(\mathsf{hypot}\left(\frac{\sin \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \mathsf{fma}\left(2, u1, 0.5\right)\right)\right)}{alphay}, \frac{\frac{1}{\color{blue}{\mathsf{hypot}\left(1, \frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \mathsf{fma}\left(2, u1, 0.5\right)\right)\right)}}}{alphax}\right)\right)}^{2} \cdot \left(1 - u0\right)}}} \]
  8. Applied egg-rr99.5%

    \[\leadsto \frac{1}{\sqrt{1 + \frac{u0}{{\left(\mathsf{hypot}\left(\frac{\sin \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \mathsf{fma}\left(2, u1, 0.5\right)\right)\right)}{alphay}, \frac{\color{blue}{\frac{1}{\mathsf{hypot}\left(1, \frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \mathsf{fma}\left(2, u1, 0.5\right)\right)\right)}}}{alphax}\right)\right)}^{2} \cdot \left(1 - u0\right)}}} \]
  9. Final simplification99.5%

    \[\leadsto \frac{1}{\sqrt{1 + \frac{u0}{{\left(\mathsf{hypot}\left(\frac{\sin \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \mathsf{fma}\left(2, u1, 0.5\right)\right)\right)}{alphay}, \frac{\frac{1}{\mathsf{hypot}\left(1, \frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \mathsf{fma}\left(2, u1, 0.5\right)\right)\right)}}{alphax}\right)\right)}^{2} \cdot \left(1 - u0\right)}}} \]

Alternative 2: 98.4% accurate, 1.8× speedup?

\[\begin{array}{l} \\ \frac{1}{\sqrt{1 + \frac{u0}{\left(1 - u0\right) \cdot {\left(\mathsf{hypot}\left(\frac{\sin \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot 0.5\right)\right)}{alphay}, \frac{\cos \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \mathsf{fma}\left(2, u1, 0.5\right)\right)\right)}{alphax}\right)\right)}^{2}}}} \end{array} \]
(FPCore (u0 u1 alphax alphay)
 :precision binary32
 (/
  1.0
  (sqrt
   (+
    1.0
    (/
     u0
     (*
      (- 1.0 u0)
      (pow
       (hypot
        (/ (sin (atan (* (/ alphay alphax) (tan (* PI 0.5))))) alphay)
        (/
         (cos (atan (* (/ alphay alphax) (tan (* PI (fma 2.0 u1 0.5))))))
         alphax))
       2.0)))))))
float code(float u0, float u1, float alphax, float alphay) {
	return 1.0f / sqrtf((1.0f + (u0 / ((1.0f - u0) * powf(hypotf((sinf(atanf(((alphay / alphax) * tanf((((float) M_PI) * 0.5f))))) / alphay), (cosf(atanf(((alphay / alphax) * tanf((((float) M_PI) * fmaf(2.0f, u1, 0.5f)))))) / alphax)), 2.0f)))));
}
function code(u0, u1, alphax, alphay)
	return Float32(Float32(1.0) / sqrt(Float32(Float32(1.0) + Float32(u0 / Float32(Float32(Float32(1.0) - u0) * (hypot(Float32(sin(atan(Float32(Float32(alphay / alphax) * tan(Float32(Float32(pi) * Float32(0.5)))))) / alphay), Float32(cos(atan(Float32(Float32(alphay / alphax) * tan(Float32(Float32(pi) * fma(Float32(2.0), u1, Float32(0.5))))))) / alphax)) ^ Float32(2.0)))))))
end
\begin{array}{l}

\\
\frac{1}{\sqrt{1 + \frac{u0}{\left(1 - u0\right) \cdot {\left(\mathsf{hypot}\left(\frac{\sin \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot 0.5\right)\right)}{alphay}, \frac{\cos \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \mathsf{fma}\left(2, u1, 0.5\right)\right)\right)}{alphax}\right)\right)}^{2}}}}
\end{array}
Derivation
  1. Initial program 99.5%

    \[\frac{1}{\sqrt{1 + \frac{\frac{1}{\frac{\cos \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\left(2 \cdot \pi\right) \cdot u1 + 0.5 \cdot \pi\right)\right) \cdot \cos \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\left(2 \cdot \pi\right) \cdot u1 + 0.5 \cdot \pi\right)\right)}{alphax \cdot alphax} + \frac{\sin \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\left(2 \cdot \pi\right) \cdot u1 + 0.5 \cdot \pi\right)\right) \cdot \sin \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\left(2 \cdot \pi\right) \cdot u1 + 0.5 \cdot \pi\right)\right)}{alphay \cdot alphay}} \cdot u0}{1 - u0}}} \]
  2. Simplified99.5%

    \[\leadsto \color{blue}{\frac{1}{\sqrt{1 + \frac{u0}{\mathsf{fma}\left(\frac{\sin \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \left(2 \cdot u1 + 0.5\right)\right)\right)}{alphay}, \frac{\sin \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \left(2 \cdot u1 + 0.5\right)\right)\right)}{alphay}, \cos \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \left(2 \cdot u1 + 0.5\right)\right)\right) \cdot \frac{\cos \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \left(2 \cdot u1 + 0.5\right)\right)\right)}{alphax \cdot alphax}\right) \cdot \left(1 - u0\right)}}}} \]
  3. Step-by-step derivation
    1. expm1-log1p-u99.5%

      \[\leadsto \frac{1}{\sqrt{1 + \frac{u0}{\color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\mathsf{fma}\left(\frac{\sin \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \left(2 \cdot u1 + 0.5\right)\right)\right)}{alphay}, \frac{\sin \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \left(2 \cdot u1 + 0.5\right)\right)\right)}{alphay}, \cos \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \left(2 \cdot u1 + 0.5\right)\right)\right) \cdot \frac{\cos \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \left(2 \cdot u1 + 0.5\right)\right)\right)}{alphax \cdot alphax}\right)\right)\right)} \cdot \left(1 - u0\right)}}} \]
    2. expm1-udef99.5%

      \[\leadsto \frac{1}{\sqrt{1 + \frac{u0}{\color{blue}{\left(e^{\mathsf{log1p}\left(\mathsf{fma}\left(\frac{\sin \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \left(2 \cdot u1 + 0.5\right)\right)\right)}{alphay}, \frac{\sin \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \left(2 \cdot u1 + 0.5\right)\right)\right)}{alphay}, \cos \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \left(2 \cdot u1 + 0.5\right)\right)\right) \cdot \frac{\cos \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \left(2 \cdot u1 + 0.5\right)\right)\right)}{alphax \cdot alphax}\right)\right)} - 1\right)} \cdot \left(1 - u0\right)}}} \]
  4. Applied egg-rr99.4%

    \[\leadsto \frac{1}{\sqrt{1 + \frac{u0}{\color{blue}{\left(e^{\mathsf{log1p}\left({\left(\mathsf{hypot}\left(\frac{\sin \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \mathsf{fma}\left(2, u1, 0.5\right)\right)\right)}{alphay}, \frac{\cos \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \mathsf{fma}\left(2, u1, 0.5\right)\right)\right)}{alphax}\right)\right)}^{2}\right)} - 1\right)} \cdot \left(1 - u0\right)}}} \]
  5. Step-by-step derivation
    1. expm1-def99.4%

      \[\leadsto \frac{1}{\sqrt{1 + \frac{u0}{\color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left({\left(\mathsf{hypot}\left(\frac{\sin \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \mathsf{fma}\left(2, u1, 0.5\right)\right)\right)}{alphay}, \frac{\cos \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \mathsf{fma}\left(2, u1, 0.5\right)\right)\right)}{alphax}\right)\right)}^{2}\right)\right)} \cdot \left(1 - u0\right)}}} \]
    2. expm1-log1p99.4%

      \[\leadsto \frac{1}{\sqrt{1 + \frac{u0}{\color{blue}{{\left(\mathsf{hypot}\left(\frac{\sin \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \mathsf{fma}\left(2, u1, 0.5\right)\right)\right)}{alphay}, \frac{\cos \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \mathsf{fma}\left(2, u1, 0.5\right)\right)\right)}{alphax}\right)\right)}^{2}} \cdot \left(1 - u0\right)}}} \]
  6. Simplified99.4%

    \[\leadsto \frac{1}{\sqrt{1 + \frac{u0}{\color{blue}{{\left(\mathsf{hypot}\left(\frac{\sin \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \mathsf{fma}\left(2, u1, 0.5\right)\right)\right)}{alphay}, \frac{\cos \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \mathsf{fma}\left(2, u1, 0.5\right)\right)\right)}{alphax}\right)\right)}^{2}} \cdot \left(1 - u0\right)}}} \]
  7. Taylor expanded in u1 around 0 98.6%

    \[\leadsto \frac{1}{\sqrt{1 + \frac{u0}{{\left(\mathsf{hypot}\left(\frac{\sin \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \color{blue}{\left(0.5 \cdot \pi\right)}\right)}{alphay}, \frac{\cos \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \mathsf{fma}\left(2, u1, 0.5\right)\right)\right)}{alphax}\right)\right)}^{2} \cdot \left(1 - u0\right)}}} \]
  8. Step-by-step derivation
    1. *-commutative97.7%

      \[\leadsto \frac{1}{\sqrt{1 + \frac{u0}{{\left(\frac{\sin \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \color{blue}{\left(\pi \cdot 0.5\right)}\right)}{alphay}\right)}^{2} \cdot \left(1 - u0\right)}}} \]
  9. Simplified98.6%

    \[\leadsto \frac{1}{\sqrt{1 + \frac{u0}{{\left(\mathsf{hypot}\left(\frac{\sin \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \color{blue}{\left(\pi \cdot 0.5\right)}\right)}{alphay}, \frac{\cos \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \mathsf{fma}\left(2, u1, 0.5\right)\right)\right)}{alphax}\right)\right)}^{2} \cdot \left(1 - u0\right)}}} \]
  10. Final simplification98.6%

    \[\leadsto \frac{1}{\sqrt{1 + \frac{u0}{\left(1 - u0\right) \cdot {\left(\mathsf{hypot}\left(\frac{\sin \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot 0.5\right)\right)}{alphay}, \frac{\cos \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \mathsf{fma}\left(2, u1, 0.5\right)\right)\right)}{alphax}\right)\right)}^{2}}}} \]

Alternative 3: 97.8% accurate, 3.5× speedup?

\[\begin{array}{l} \\ \frac{1}{\sqrt{1 + \frac{u0}{\left(1 - u0\right) \cdot {\left(\frac{\sin \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot 0.5\right)\right)}{alphay}\right)}^{2}}}} \end{array} \]
(FPCore (u0 u1 alphax alphay)
 :precision binary32
 (/
  1.0
  (sqrt
   (+
    1.0
    (/
     u0
     (*
      (- 1.0 u0)
      (pow
       (/ (sin (atan (* (/ alphay alphax) (tan (* PI 0.5))))) alphay)
       2.0)))))))
float code(float u0, float u1, float alphax, float alphay) {
	return 1.0f / sqrtf((1.0f + (u0 / ((1.0f - u0) * powf((sinf(atanf(((alphay / alphax) * tanf((((float) M_PI) * 0.5f))))) / alphay), 2.0f)))));
}
function code(u0, u1, alphax, alphay)
	return Float32(Float32(1.0) / sqrt(Float32(Float32(1.0) + Float32(u0 / Float32(Float32(Float32(1.0) - u0) * (Float32(sin(atan(Float32(Float32(alphay / alphax) * tan(Float32(Float32(pi) * Float32(0.5)))))) / alphay) ^ Float32(2.0)))))))
end
function tmp = code(u0, u1, alphax, alphay)
	tmp = single(1.0) / sqrt((single(1.0) + (u0 / ((single(1.0) - u0) * ((sin(atan(((alphay / alphax) * tan((single(pi) * single(0.5)))))) / alphay) ^ single(2.0))))));
end
\begin{array}{l}

\\
\frac{1}{\sqrt{1 + \frac{u0}{\left(1 - u0\right) \cdot {\left(\frac{\sin \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot 0.5\right)\right)}{alphay}\right)}^{2}}}}
\end{array}
Derivation
  1. Initial program 99.5%

    \[\frac{1}{\sqrt{1 + \frac{\frac{1}{\frac{\cos \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\left(2 \cdot \pi\right) \cdot u1 + 0.5 \cdot \pi\right)\right) \cdot \cos \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\left(2 \cdot \pi\right) \cdot u1 + 0.5 \cdot \pi\right)\right)}{alphax \cdot alphax} + \frac{\sin \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\left(2 \cdot \pi\right) \cdot u1 + 0.5 \cdot \pi\right)\right) \cdot \sin \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\left(2 \cdot \pi\right) \cdot u1 + 0.5 \cdot \pi\right)\right)}{alphay \cdot alphay}} \cdot u0}{1 - u0}}} \]
  2. Simplified99.5%

    \[\leadsto \color{blue}{\frac{1}{\sqrt{1 + \frac{u0}{\mathsf{fma}\left(\frac{\sin \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \left(2 \cdot u1 + 0.5\right)\right)\right)}{alphay}, \frac{\sin \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \left(2 \cdot u1 + 0.5\right)\right)\right)}{alphay}, \cos \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \left(2 \cdot u1 + 0.5\right)\right)\right) \cdot \frac{\cos \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \left(2 \cdot u1 + 0.5\right)\right)\right)}{alphax \cdot alphax}\right) \cdot \left(1 - u0\right)}}}} \]
  3. Taylor expanded in alphay around 0 97.7%

    \[\leadsto \frac{1}{\sqrt{1 + \frac{u0}{\color{blue}{\frac{{\sin \tan^{-1} \left(\frac{alphay \cdot \tan \left(\pi \cdot \left(0.5 + 2 \cdot u1\right)\right)}{alphax}\right)}^{2}}{{alphay}^{2}}} \cdot \left(1 - u0\right)}}} \]
  4. Simplified97.7%

    \[\leadsto \frac{1}{\sqrt{1 + \frac{u0}{\color{blue}{{\left(\frac{\sin \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot \mathsf{fma}\left(2, u1, 0.5\right)\right)\right)}{alphay}\right)}^{2}} \cdot \left(1 - u0\right)}}} \]
  5. Taylor expanded in u1 around 0 97.7%

    \[\leadsto \frac{1}{\sqrt{1 + \frac{u0}{{\left(\frac{\sin \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \color{blue}{\left(0.5 \cdot \pi\right)}\right)}{alphay}\right)}^{2} \cdot \left(1 - u0\right)}}} \]
  6. Step-by-step derivation
    1. *-commutative97.7%

      \[\leadsto \frac{1}{\sqrt{1 + \frac{u0}{{\left(\frac{\sin \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \color{blue}{\left(\pi \cdot 0.5\right)}\right)}{alphay}\right)}^{2} \cdot \left(1 - u0\right)}}} \]
  7. Simplified97.7%

    \[\leadsto \frac{1}{\sqrt{1 + \frac{u0}{{\left(\frac{\sin \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \color{blue}{\left(\pi \cdot 0.5\right)}\right)}{alphay}\right)}^{2} \cdot \left(1 - u0\right)}}} \]
  8. Final simplification97.7%

    \[\leadsto \frac{1}{\sqrt{1 + \frac{u0}{\left(1 - u0\right) \cdot {\left(\frac{\sin \tan^{-1} \left(\frac{alphay}{alphax} \cdot \tan \left(\pi \cdot 0.5\right)\right)}{alphay}\right)}^{2}}}} \]

Reproduce

?
herbie shell --seed 2023287 
(FPCore (u0 u1 alphax alphay)
  :name "Trowbridge-Reitz Sample, sample surface normal, cosTheta"
  :precision binary32
  :pre (and (and (and (and (<= 2.328306437e-10 u0) (<= u0 1.0)) (and (<= 2.328306437e-10 u1) (<= u1 0.5))) (and (<= 0.0001 alphax) (<= alphax 1.0))) (and (<= 0.0001 alphay) (<= alphay 1.0)))
  (/ 1.0 (sqrt (+ 1.0 (/ (* (/ 1.0 (+ (/ (* (cos (atan (* (/ alphay alphax) (tan (+ (* (* 2.0 PI) u1) (* 0.5 PI)))))) (cos (atan (* (/ alphay alphax) (tan (+ (* (* 2.0 PI) u1) (* 0.5 PI))))))) (* alphax alphax)) (/ (* (sin (atan (* (/ alphay alphax) (tan (+ (* (* 2.0 PI) u1) (* 0.5 PI)))))) (sin (atan (* (/ alphay alphax) (tan (+ (* (* 2.0 PI) u1) (* 0.5 PI))))))) (* alphay alphay)))) u0) (- 1.0 u0))))))