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

?

Percentage Accurate: 60.8% → 98.1%
Time: 17.0s
Precision: binary32
Cost: 3744

?

\[\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\]
\[\frac{-\log \left(1 - u0\right)}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}} \]
\[\frac{-\mathsf{log1p}\left(-u0\right)}{\frac{\frac{cos2phi}{alphax}}{alphax} + \frac{1}{alphay \cdot \frac{alphay}{sin2phi}}} \]
(FPCore (alphax alphay u0 cos2phi sin2phi)
 :precision binary32
 (/
  (- (log (- 1.0 u0)))
  (+ (/ cos2phi (* alphax alphax)) (/ sin2phi (* alphay alphay)))))
(FPCore (alphax alphay u0 cos2phi sin2phi)
 :precision binary32
 (/
  (- (log1p (- u0)))
  (+ (/ (/ cos2phi alphax) alphax) (/ 1.0 (* alphay (/ alphay sin2phi))))))
float code(float alphax, float alphay, float u0, float cos2phi, float sin2phi) {
	return -logf((1.0f - u0)) / ((cos2phi / (alphax * alphax)) + (sin2phi / (alphay * alphay)));
}
float code(float alphax, float alphay, float u0, float cos2phi, float sin2phi) {
	return -log1pf(-u0) / (((cos2phi / alphax) / alphax) + (1.0f / (alphay * (alphay / sin2phi))));
}
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 code(alphax, alphay, u0, cos2phi, sin2phi)
	return Float32(Float32(-log1p(Float32(-u0))) / Float32(Float32(Float32(cos2phi / alphax) / alphax) + Float32(Float32(1.0) / Float32(alphay * Float32(alphay / sin2phi)))))
end
\frac{-\log \left(1 - u0\right)}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}}
\frac{-\mathsf{log1p}\left(-u0\right)}{\frac{\frac{cos2phi}{alphax}}{alphax} + \frac{1}{alphay \cdot \frac{alphay}{sin2phi}}}

Local Percentage Accuracy?

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.

Herbie found 16 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

The accuracy (vertical axis) and speed (horizontal axis) of each of Herbie's proposed alternatives. Up and to the right is better. Each dot represents an alternative program; the red square represents the initial program.

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Derivation?

  1. Initial program 58.2%

    \[\frac{-\log \left(1 - u0\right)}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}} \]
  2. Simplified98.5%

    \[\leadsto \color{blue}{\frac{-\mathsf{log1p}\left(-u0\right)}{\frac{\frac{cos2phi}{alphax}}{alphax} + \frac{sin2phi}{alphay \cdot alphay}}} \]
    Step-by-step derivation

    [Start]58.2

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

    neg-sub0 [=>]58.2

    \[ \frac{\color{blue}{0 - \log \left(1 - u0\right)}}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}} \]

    div-sub [=>]58.2

    \[ \color{blue}{\frac{0}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}} - \frac{\log \left(1 - u0\right)}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}}} \]

    --rgt-identity [<=]58.2

    \[ \frac{0}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}} - \frac{\color{blue}{\log \left(1 - u0\right) - 0}}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}} \]

    div-sub [<=]58.2

    \[ \color{blue}{\frac{0 - \left(\log \left(1 - u0\right) - 0\right)}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}}} \]

    --rgt-identity [=>]58.2

    \[ \frac{0 - \color{blue}{\log \left(1 - u0\right)}}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}} \]

    sub-neg [=>]58.2

    \[ \frac{0 - \log \color{blue}{\left(1 + \left(-u0\right)\right)}}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}} \]

    +-commutative [=>]58.2

    \[ \frac{0 - \log \color{blue}{\left(\left(-u0\right) + 1\right)}}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}} \]

    neg-sub0 [=>]58.2

    \[ \frac{0 - \log \left(\color{blue}{\left(0 - u0\right)} + 1\right)}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}} \]

    associate-+l- [=>]58.2

    \[ \frac{0 - \log \color{blue}{\left(0 - \left(u0 - 1\right)\right)}}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}} \]

    sub0-neg [=>]58.2

    \[ \frac{0 - \log \color{blue}{\left(-\left(u0 - 1\right)\right)}}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}} \]

    neg-mul-1 [=>]58.2

    \[ \frac{0 - \log \color{blue}{\left(-1 \cdot \left(u0 - 1\right)\right)}}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}} \]

    log-prod [=>]-0.0

    \[ \frac{0 - \color{blue}{\left(\log -1 + \log \left(u0 - 1\right)\right)}}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}} \]

    associate--r+ [=>]-0.0

    \[ \frac{\color{blue}{\left(0 - \log -1\right) - \log \left(u0 - 1\right)}}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}} \]
  3. Applied egg-rr98.4%

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

    [Start]98.5

    \[ \frac{-\mathsf{log1p}\left(-u0\right)}{\frac{\frac{cos2phi}{alphax}}{alphax} + \frac{sin2phi}{alphay \cdot alphay}} \]

    associate-/r* [=>]98.5

    \[ \frac{-\mathsf{log1p}\left(-u0\right)}{\frac{\frac{cos2phi}{alphax}}{alphax} + \color{blue}{\frac{\frac{sin2phi}{alphay}}{alphay}}} \]

    div-inv [=>]98.4

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

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

    [Start]98.4

    \[ \frac{-\mathsf{log1p}\left(-u0\right)}{\frac{\frac{cos2phi}{alphax}}{alphax} + \frac{sin2phi}{alphay} \cdot \frac{1}{alphay}} \]

    clear-num [=>]98.3

    \[ \frac{-\mathsf{log1p}\left(-u0\right)}{\frac{\frac{cos2phi}{alphax}}{alphax} + \color{blue}{\frac{1}{\frac{alphay}{sin2phi}}} \cdot \frac{1}{alphay}} \]

    frac-times [=>]98.5

    \[ \frac{-\mathsf{log1p}\left(-u0\right)}{\frac{\frac{cos2phi}{alphax}}{alphax} + \color{blue}{\frac{1 \cdot 1}{\frac{alphay}{sin2phi} \cdot alphay}}} \]

    metadata-eval [=>]98.5

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

    \[\leadsto \frac{-\mathsf{log1p}\left(-u0\right)}{\frac{\frac{cos2phi}{alphax}}{alphax} + \frac{1}{alphay \cdot \frac{alphay}{sin2phi}}} \]

Alternatives

Alternative 1
Accuracy92.7%
Cost3684
\[\begin{array}{l} t_0 := \frac{sin2phi}{alphay \cdot alphay}\\ \mathbf{if}\;t_0 \leq 0.014999999664723873:\\ \;\;\;\;\frac{u0 - u0 \cdot \left(u0 \cdot -0.5\right)}{\frac{\frac{cos2phi}{alphax}}{alphax} + t_0}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{log1p}\left(-u0\right) \cdot \left(alphay \cdot \left(-\frac{alphay}{sin2phi}\right)\right)\\ \end{array} \]
Alternative 2
Accuracy98.3%
Cost3680
\[\frac{-\mathsf{log1p}\left(-u0\right)}{\frac{sin2phi}{alphay \cdot alphay} + \frac{cos2phi}{alphax \cdot alphax}} \]
Alternative 3
Accuracy98.3%
Cost3680
\[\frac{-\mathsf{log1p}\left(-u0\right)}{\frac{\frac{cos2phi}{alphax}}{alphax} + \frac{sin2phi}{alphay \cdot alphay}} \]
Alternative 4
Accuracy83.4%
Cost740
\[\begin{array}{l} \mathbf{if}\;\frac{sin2phi}{alphay \cdot alphay} \leq 0.014999999664723873:\\ \;\;\;\;\frac{u0}{\frac{cos2phi}{alphax \cdot alphax} + sin2phi \cdot \frac{-1}{alphay \cdot \left(-alphay\right)}}\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(alphay \cdot alphay\right) \cdot \left(u0 - \left(u0 \cdot u0\right) \cdot \left(-0.5 + u0 \cdot -0.3333333333333333\right)\right)}{sin2phi}\\ \end{array} \]
Alternative 5
Accuracy81.4%
Cost708
\[\begin{array}{l} \mathbf{if}\;\frac{sin2phi}{alphay \cdot alphay} \leq 0.014999999664723873:\\ \;\;\;\;\frac{u0}{\frac{cos2phi}{alphax \cdot alphax} + sin2phi \cdot \frac{-1}{alphay \cdot \left(-alphay\right)}}\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(alphay \cdot alphay\right) \cdot \left(u0 - u0 \cdot \left(u0 \cdot -0.5\right)\right)}{sin2phi}\\ \end{array} \]
Alternative 6
Accuracy81.4%
Cost644
\[\begin{array}{l} \mathbf{if}\;\frac{sin2phi}{alphay \cdot alphay} \leq 0.014999999664723873:\\ \;\;\;\;\frac{u0}{\frac{cos2phi}{alphax \cdot alphax} + \frac{\frac{sin2phi}{alphay}}{alphay}}\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(alphay \cdot alphay\right) \cdot \left(u0 \cdot \left(\left(--1\right) - u0 \cdot -0.5\right)\right)}{sin2phi}\\ \end{array} \]
Alternative 7
Accuracy81.4%
Cost612
\[\begin{array}{l} \mathbf{if}\;\frac{sin2phi}{alphay \cdot alphay} \leq 0.014999999664723873:\\ \;\;\;\;\frac{u0}{\frac{cos2phi}{alphax \cdot alphax} + \frac{\frac{sin2phi}{alphay}}{alphay}}\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(alphay \cdot alphay\right) \cdot \left(u0 - u0 \cdot \left(u0 \cdot -0.5\right)\right)}{sin2phi}\\ \end{array} \]
Alternative 8
Accuracy87.4%
Cost608
\[\frac{u0 - u0 \cdot \left(u0 \cdot -0.5\right)}{\frac{\frac{cos2phi}{alphax}}{alphax} + \frac{sin2phi}{alphay \cdot alphay}} \]
Alternative 9
Accuracy67.2%
Cost420
\[\begin{array}{l} \mathbf{if}\;\frac{sin2phi}{alphay \cdot alphay} \leq 5.000000018137469 \cdot 10^{-16}:\\ \;\;\;\;u0 \cdot \left(alphax \cdot \frac{alphax}{cos2phi}\right)\\ \mathbf{else}:\\ \;\;\;\;\left(alphay \cdot alphay\right) \cdot \frac{u0}{sin2phi}\\ \end{array} \]
Alternative 10
Accuracy67.2%
Cost420
\[\begin{array}{l} \mathbf{if}\;\frac{sin2phi}{alphay \cdot alphay} \leq 5.000000018137469 \cdot 10^{-16}:\\ \;\;\;\;\frac{alphax \cdot \left(u0 \cdot alphax\right)}{cos2phi}\\ \mathbf{else}:\\ \;\;\;\;\left(alphay \cdot alphay\right) \cdot \frac{u0}{sin2phi}\\ \end{array} \]
Alternative 11
Accuracy67.2%
Cost420
\[\begin{array}{l} \mathbf{if}\;\frac{sin2phi}{alphay \cdot alphay} \leq 5.000000018137469 \cdot 10^{-16}:\\ \;\;\;\;\frac{alphax \cdot \left(u0 \cdot alphax\right)}{cos2phi}\\ \mathbf{else}:\\ \;\;\;\;\frac{u0 \cdot \left(alphay \cdot alphay\right)}{sin2phi}\\ \end{array} \]
Alternative 12
Accuracy75.9%
Cost416
\[\frac{u0}{\frac{sin2phi}{alphay \cdot alphay} + \frac{cos2phi}{alphax \cdot alphax}} \]
Alternative 13
Accuracy75.9%
Cost416
\[\frac{u0}{\frac{\frac{cos2phi}{alphax}}{alphax} + \frac{sin2phi}{alphay \cdot alphay}} \]
Alternative 14
Accuracy34.2%
Cost292
\[\begin{array}{l} \mathbf{if}\;sin2phi \leq 21200:\\ \;\;\;\;u0 \cdot \left(alphax \cdot \frac{alphax}{cos2phi}\right)\\ \mathbf{else}:\\ \;\;\;\;0\\ \end{array} \]
Alternative 15
Accuracy16.8%
Cost32
\[0 \]

Reproduce?

herbie shell --seed 2023160 
(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)))))