Beckmann Distribution sample, tan2theta, alphax != alphay, u1 <= 0.5

Percentage Accurate: 60.9% → 98.2%
Time: 18.7s
Alternatives: 25
Speedup: 3.5×

Specification

?
\[\left(\left(\left(\left(0.0001 \leq alphax \land alphax \leq 1\right) \land \left(0.0001 \leq alphay \land alphay \leq 1\right)\right) \land \left(2.328306437 \cdot 10^{-10} \leq u0 \land u0 \leq 1\right)\right) \land \left(0 \leq cos2phi \land cos2phi \leq 1\right)\right) \land 0 \leq sin2phi\]
\[\begin{array}{l} \\ \frac{-\log \left(1 - u0\right)}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}} \end{array} \]
(FPCore (alphax alphay u0 cos2phi sin2phi)
 :precision binary32
 (/
  (- (log (- 1.0 u0)))
  (+ (/ cos2phi (* alphax alphax)) (/ sin2phi (* alphay alphay)))))
float code(float alphax, float alphay, float u0, float cos2phi, float sin2phi) {
	return -logf((1.0f - u0)) / ((cos2phi / (alphax * alphax)) + (sin2phi / (alphay * alphay)));
}
real(4) function code(alphax, alphay, u0, cos2phi, sin2phi)
    real(4), intent (in) :: alphax
    real(4), intent (in) :: alphay
    real(4), intent (in) :: u0
    real(4), intent (in) :: cos2phi
    real(4), intent (in) :: sin2phi
    code = -log((1.0e0 - u0)) / ((cos2phi / (alphax * alphax)) + (sin2phi / (alphay * alphay)))
end function
function code(alphax, alphay, u0, cos2phi, sin2phi)
	return Float32(Float32(-log(Float32(Float32(1.0) - u0))) / Float32(Float32(cos2phi / Float32(alphax * alphax)) + Float32(sin2phi / Float32(alphay * alphay))))
end
function tmp = code(alphax, alphay, u0, cos2phi, sin2phi)
	tmp = -log((single(1.0) - u0)) / ((cos2phi / (alphax * alphax)) + (sin2phi / (alphay * alphay)));
end
\begin{array}{l}

\\
\frac{-\log \left(1 - u0\right)}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}}
\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 25 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: 60.9% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{-\log \left(1 - u0\right)}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}} \end{array} \]
(FPCore (alphax alphay u0 cos2phi sin2phi)
 :precision binary32
 (/
  (- (log (- 1.0 u0)))
  (+ (/ cos2phi (* alphax alphax)) (/ sin2phi (* alphay alphay)))))
float code(float alphax, float alphay, float u0, float cos2phi, float sin2phi) {
	return -logf((1.0f - u0)) / ((cos2phi / (alphax * alphax)) + (sin2phi / (alphay * alphay)));
}
real(4) function code(alphax, alphay, u0, cos2phi, sin2phi)
    real(4), intent (in) :: alphax
    real(4), intent (in) :: alphay
    real(4), intent (in) :: u0
    real(4), intent (in) :: cos2phi
    real(4), intent (in) :: sin2phi
    code = -log((1.0e0 - u0)) / ((cos2phi / (alphax * alphax)) + (sin2phi / (alphay * alphay)))
end function
function code(alphax, alphay, u0, cos2phi, sin2phi)
	return Float32(Float32(-log(Float32(Float32(1.0) - u0))) / Float32(Float32(cos2phi / Float32(alphax * alphax)) + Float32(sin2phi / Float32(alphay * alphay))))
end
function tmp = code(alphax, alphay, u0, cos2phi, sin2phi)
	tmp = -log((single(1.0) - u0)) / ((cos2phi / (alphax * alphax)) + (sin2phi / (alphay * alphay)));
end
\begin{array}{l}

\\
\frac{-\log \left(1 - u0\right)}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}}
\end{array}

Alternative 1: 98.2% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{\mathsf{log1p}\left(-u0\right)}{-\mathsf{fma}\left(alphax, alphax \cdot \frac{sin2phi}{alphay}, alphay \cdot cos2phi\right)} \cdot \left(alphax \cdot \left(alphax \cdot alphay\right)\right) \end{array} \]
(FPCore (alphax alphay u0 cos2phi sin2phi)
 :precision binary32
 (*
  (/
   (log1p (- u0))
   (- (fma alphax (* alphax (/ sin2phi alphay)) (* alphay cos2phi))))
  (* alphax (* alphax alphay))))
float code(float alphax, float alphay, float u0, float cos2phi, float sin2phi) {
	return (log1pf(-u0) / -fmaf(alphax, (alphax * (sin2phi / alphay)), (alphay * cos2phi))) * (alphax * (alphax * alphay));
}
function code(alphax, alphay, u0, cos2phi, sin2phi)
	return Float32(Float32(log1p(Float32(-u0)) / Float32(-fma(alphax, Float32(alphax * Float32(sin2phi / alphay)), Float32(alphay * cos2phi)))) * Float32(alphax * Float32(alphax * alphay)))
end
\begin{array}{l}

\\
\frac{\mathsf{log1p}\left(-u0\right)}{-\mathsf{fma}\left(alphax, alphax \cdot \frac{sin2phi}{alphay}, alphay \cdot cos2phi\right)} \cdot \left(alphax \cdot \left(alphax \cdot alphay\right)\right)
\end{array}
Derivation
  1. Initial program 60.6%

    \[\frac{-\log \left(1 - u0\right)}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}} \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. lift-/.f32N/A

      \[\leadsto \color{blue}{\frac{\mathsf{neg}\left(\log \left(1 - u0\right)\right)}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}}} \]
    2. frac-2negN/A

      \[\leadsto \color{blue}{\frac{\mathsf{neg}\left(\left(\mathsf{neg}\left(\log \left(1 - u0\right)\right)\right)\right)}{\mathsf{neg}\left(\left(\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}\right)\right)}} \]
    3. div-invN/A

      \[\leadsto \color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\log \left(1 - u0\right)\right)\right)\right)\right) \cdot \frac{1}{\mathsf{neg}\left(\left(\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}\right)\right)}} \]
    4. lift-neg.f32N/A

      \[\leadsto \left(\mathsf{neg}\left(\color{blue}{\left(\mathsf{neg}\left(\log \left(1 - u0\right)\right)\right)}\right)\right) \cdot \frac{1}{\mathsf{neg}\left(\left(\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}\right)\right)} \]
    5. remove-double-negN/A

      \[\leadsto \color{blue}{\log \left(1 - u0\right)} \cdot \frac{1}{\mathsf{neg}\left(\left(\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}\right)\right)} \]
    6. frac-2negN/A

      \[\leadsto \log \left(1 - u0\right) \cdot \color{blue}{\frac{\mathsf{neg}\left(1\right)}{\mathsf{neg}\left(\left(\mathsf{neg}\left(\left(\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}\right)\right)\right)\right)}} \]
    7. metadata-evalN/A

      \[\leadsto \log \left(1 - u0\right) \cdot \frac{\color{blue}{-1}}{\mathsf{neg}\left(\left(\mathsf{neg}\left(\left(\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}\right)\right)\right)\right)} \]
    8. remove-double-negN/A

      \[\leadsto \log \left(1 - u0\right) \cdot \frac{-1}{\color{blue}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}}} \]
    9. lower-*.f32N/A

      \[\leadsto \color{blue}{\log \left(1 - u0\right) \cdot \frac{-1}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}}} \]
    10. lift-log.f32N/A

      \[\leadsto \color{blue}{\log \left(1 - u0\right)} \cdot \frac{-1}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}} \]
    11. lift--.f32N/A

      \[\leadsto \log \color{blue}{\left(1 - u0\right)} \cdot \frac{-1}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}} \]
    12. sub-negN/A

      \[\leadsto \log \color{blue}{\left(1 + \left(\mathsf{neg}\left(u0\right)\right)\right)} \cdot \frac{-1}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}} \]
    13. lower-log1p.f32N/A

      \[\leadsto \color{blue}{\mathsf{log1p}\left(\mathsf{neg}\left(u0\right)\right)} \cdot \frac{-1}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}} \]
    14. lower-neg.f32N/A

      \[\leadsto \mathsf{log1p}\left(\color{blue}{\mathsf{neg}\left(u0\right)}\right) \cdot \frac{-1}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}} \]
    15. lower-/.f3298.1

      \[\leadsto \mathsf{log1p}\left(-u0\right) \cdot \color{blue}{\frac{-1}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}}} \]
  4. Applied rewrites98.1%

    \[\leadsto \color{blue}{\mathsf{log1p}\left(-u0\right) \cdot \frac{-1}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}}} \]
  5. Step-by-step derivation
    1. lift-*.f32N/A

      \[\leadsto \color{blue}{\mathsf{log1p}\left(\mathsf{neg}\left(u0\right)\right) \cdot \frac{-1}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}}} \]
    2. *-commutativeN/A

      \[\leadsto \color{blue}{\frac{-1}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}} \cdot \mathsf{log1p}\left(\mathsf{neg}\left(u0\right)\right)} \]
    3. lift-/.f32N/A

      \[\leadsto \color{blue}{\frac{-1}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}}} \cdot \mathsf{log1p}\left(\mathsf{neg}\left(u0\right)\right) \]
    4. associate-*l/N/A

      \[\leadsto \color{blue}{\frac{-1 \cdot \mathsf{log1p}\left(\mathsf{neg}\left(u0\right)\right)}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}}} \]
    5. lift-log1p.f32N/A

      \[\leadsto \frac{-1 \cdot \color{blue}{\log \left(1 + \left(\mathsf{neg}\left(u0\right)\right)\right)}}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}} \]
    6. lift-neg.f32N/A

      \[\leadsto \frac{-1 \cdot \log \left(1 + \color{blue}{\left(\mathsf{neg}\left(u0\right)\right)}\right)}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}} \]
    7. sub-negN/A

      \[\leadsto \frac{-1 \cdot \log \color{blue}{\left(1 - u0\right)}}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}} \]
    8. neg-mul-1N/A

      \[\leadsto \frac{\color{blue}{\mathsf{neg}\left(\log \left(1 - u0\right)\right)}}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}} \]
    9. lift-+.f32N/A

      \[\leadsto \frac{\mathsf{neg}\left(\log \left(1 - u0\right)\right)}{\color{blue}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}}} \]
    10. lift-/.f32N/A

      \[\leadsto \frac{\mathsf{neg}\left(\log \left(1 - u0\right)\right)}{\color{blue}{\frac{cos2phi}{alphax \cdot alphax}} + \frac{sin2phi}{alphay \cdot alphay}} \]
    11. lift-/.f32N/A

      \[\leadsto \frac{\mathsf{neg}\left(\log \left(1 - u0\right)\right)}{\frac{cos2phi}{alphax \cdot alphax} + \color{blue}{\frac{sin2phi}{alphay \cdot alphay}}} \]
    12. lift-*.f32N/A

      \[\leadsto \frac{\mathsf{neg}\left(\log \left(1 - u0\right)\right)}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{\color{blue}{alphay \cdot alphay}}} \]
    13. associate-/r*N/A

      \[\leadsto \frac{\mathsf{neg}\left(\log \left(1 - u0\right)\right)}{\frac{cos2phi}{alphax \cdot alphax} + \color{blue}{\frac{\frac{sin2phi}{alphay}}{alphay}}} \]
    14. lift-/.f32N/A

      \[\leadsto \frac{\mathsf{neg}\left(\log \left(1 - u0\right)\right)}{\frac{cos2phi}{alphax \cdot alphax} + \frac{\color{blue}{\frac{sin2phi}{alphay}}}{alphay}} \]
    15. frac-addN/A

      \[\leadsto \frac{\mathsf{neg}\left(\log \left(1 - u0\right)\right)}{\color{blue}{\frac{cos2phi \cdot alphay + \left(alphax \cdot alphax\right) \cdot \frac{sin2phi}{alphay}}{\left(alphax \cdot alphax\right) \cdot alphay}}} \]
    16. associate-/r/N/A

      \[\leadsto \color{blue}{\frac{\mathsf{neg}\left(\log \left(1 - u0\right)\right)}{cos2phi \cdot alphay + \left(alphax \cdot alphax\right) \cdot \frac{sin2phi}{alphay}} \cdot \left(\left(alphax \cdot alphax\right) \cdot alphay\right)} \]
  6. Applied rewrites98.4%

    \[\leadsto \color{blue}{\frac{\mathsf{log1p}\left(-u0\right)}{-\mathsf{fma}\left(alphax, alphax \cdot \frac{sin2phi}{alphay}, cos2phi \cdot alphay\right)} \cdot \left(alphax \cdot \left(alphax \cdot alphay\right)\right)} \]
  7. Final simplification98.4%

    \[\leadsto \frac{\mathsf{log1p}\left(-u0\right)}{-\mathsf{fma}\left(alphax, alphax \cdot \frac{sin2phi}{alphay}, alphay \cdot cos2phi\right)} \cdot \left(alphax \cdot \left(alphax \cdot alphay\right)\right) \]
  8. Add Preprocessing

Alternative 2: 98.4% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \left(alphay \cdot alphay\right) \cdot \left(\left(-alphax \cdot alphax\right) \cdot \frac{\mathsf{log1p}\left(-u0\right)}{\mathsf{fma}\left(alphax \cdot alphax, sin2phi, cos2phi \cdot \left(alphay \cdot alphay\right)\right)}\right) \end{array} \]
(FPCore (alphax alphay u0 cos2phi sin2phi)
 :precision binary32
 (*
  (* alphay alphay)
  (*
   (- (* alphax alphax))
   (/
    (log1p (- u0))
    (fma (* alphax alphax) sin2phi (* cos2phi (* alphay alphay)))))))
float code(float alphax, float alphay, float u0, float cos2phi, float sin2phi) {
	return (alphay * alphay) * (-(alphax * alphax) * (log1pf(-u0) / fmaf((alphax * alphax), sin2phi, (cos2phi * (alphay * alphay)))));
}
function code(alphax, alphay, u0, cos2phi, sin2phi)
	return Float32(Float32(alphay * alphay) * Float32(Float32(-Float32(alphax * alphax)) * Float32(log1p(Float32(-u0)) / fma(Float32(alphax * alphax), sin2phi, Float32(cos2phi * Float32(alphay * alphay))))))
end
\begin{array}{l}

\\
\left(alphay \cdot alphay\right) \cdot \left(\left(-alphax \cdot alphax\right) \cdot \frac{\mathsf{log1p}\left(-u0\right)}{\mathsf{fma}\left(alphax \cdot alphax, sin2phi, cos2phi \cdot \left(alphay \cdot alphay\right)\right)}\right)
\end{array}
Derivation
  1. Initial program 60.9%

    \[\frac{-\log \left(1 - u0\right)}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}} \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. lift-/.f32N/A

      \[\leadsto \color{blue}{\frac{\mathsf{neg}\left(\log \left(1 - u0\right)\right)}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}}} \]
    2. lift-+.f32N/A

      \[\leadsto \frac{\mathsf{neg}\left(\log \left(1 - u0\right)\right)}{\color{blue}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}}} \]
    3. lift-/.f32N/A

      \[\leadsto \frac{\mathsf{neg}\left(\log \left(1 - u0\right)\right)}{\color{blue}{\frac{cos2phi}{alphax \cdot alphax}} + \frac{sin2phi}{alphay \cdot alphay}} \]
    4. lift-/.f32N/A

      \[\leadsto \frac{\mathsf{neg}\left(\log \left(1 - u0\right)\right)}{\frac{cos2phi}{alphax \cdot alphax} + \color{blue}{\frac{sin2phi}{alphay \cdot alphay}}} \]
    5. frac-addN/A

      \[\leadsto \frac{\mathsf{neg}\left(\log \left(1 - u0\right)\right)}{\color{blue}{\frac{cos2phi \cdot \left(alphay \cdot alphay\right) + \left(alphax \cdot alphax\right) \cdot sin2phi}{\left(alphax \cdot alphax\right) \cdot \left(alphay \cdot alphay\right)}}} \]
    6. associate-/r/N/A

      \[\leadsto \color{blue}{\frac{\mathsf{neg}\left(\log \left(1 - u0\right)\right)}{cos2phi \cdot \left(alphay \cdot alphay\right) + \left(alphax \cdot alphax\right) \cdot sin2phi} \cdot \left(\left(alphax \cdot alphax\right) \cdot \left(alphay \cdot alphay\right)\right)} \]
    7. associate-*r*N/A

      \[\leadsto \color{blue}{\left(\frac{\mathsf{neg}\left(\log \left(1 - u0\right)\right)}{cos2phi \cdot \left(alphay \cdot alphay\right) + \left(alphax \cdot alphax\right) \cdot sin2phi} \cdot \left(alphax \cdot alphax\right)\right) \cdot \left(alphay \cdot alphay\right)} \]
    8. lower-*.f32N/A

      \[\leadsto \color{blue}{\left(\frac{\mathsf{neg}\left(\log \left(1 - u0\right)\right)}{cos2phi \cdot \left(alphay \cdot alphay\right) + \left(alphax \cdot alphax\right) \cdot sin2phi} \cdot \left(alphax \cdot alphax\right)\right) \cdot \left(alphay \cdot alphay\right)} \]
  4. Applied rewrites98.4%

    \[\leadsto \color{blue}{\left(\frac{\mathsf{log1p}\left(-u0\right)}{-\mathsf{fma}\left(alphax \cdot alphax, sin2phi, cos2phi \cdot \left(alphay \cdot alphay\right)\right)} \cdot \left(alphax \cdot alphax\right)\right) \cdot \left(alphay \cdot alphay\right)} \]
  5. Final simplification98.4%

    \[\leadsto \left(alphay \cdot alphay\right) \cdot \left(\left(-alphax \cdot alphax\right) \cdot \frac{\mathsf{log1p}\left(-u0\right)}{\mathsf{fma}\left(alphax \cdot alphax, sin2phi, cos2phi \cdot \left(alphay \cdot alphay\right)\right)}\right) \]
  6. Add Preprocessing

Reproduce

?
herbie shell --seed 2024228 
(FPCore (alphax alphay u0 cos2phi sin2phi)
  :name "Beckmann Distribution sample, tan2theta, alphax != alphay, u1 <= 0.5"
  :precision binary32
  :pre (and (and (and (and (and (<= 0.0001 alphax) (<= alphax 1.0)) (and (<= 0.0001 alphay) (<= alphay 1.0))) (and (<= 2.328306437e-10 u0) (<= u0 1.0))) (and (<= 0.0 cos2phi) (<= cos2phi 1.0))) (<= 0.0 sin2phi))
  (/ (- (log (- 1.0 u0))) (+ (/ cos2phi (* alphax alphax)) (/ sin2phi (* alphay alphay)))))