Beckmann Distribution sample, tan2theta, alphax == alphay

Percentage Accurate: 55.8% → 98.9%
Time: 6.6s
Alternatives: 5
Speedup: 21.6×

Specification

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

\\
\left(\left(-\alpha\right) \cdot \alpha\right) \cdot \log \left(1 - u0\right)
\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 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: 55.8% accurate, 1.0× speedup?

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

\\
\left(\left(-\alpha\right) \cdot \alpha\right) \cdot \log \left(1 - u0\right)
\end{array}

Alternative 1: 98.9% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \alpha \cdot \left(\left(-\alpha\right) \cdot \mathsf{log1p}\left(-u0\right)\right) \end{array} \]
(FPCore (alpha u0) :precision binary32 (* alpha (* (- alpha) (log1p (- u0)))))
float code(float alpha, float u0) {
	return alpha * (-alpha * log1pf(-u0));
}
function code(alpha, u0)
	return Float32(alpha * Float32(Float32(-alpha) * log1p(Float32(-u0))))
end
\begin{array}{l}

\\
\alpha \cdot \left(\left(-\alpha\right) \cdot \mathsf{log1p}\left(-u0\right)\right)
\end{array}
Derivation
  1. Initial program 54.8%

    \[\left(\left(-\alpha\right) \cdot \alpha\right) \cdot \log \left(1 - u0\right) \]
  2. Step-by-step derivation
    1. associate-*l*54.8%

      \[\leadsto \color{blue}{\left(-\alpha\right) \cdot \left(\alpha \cdot \log \left(1 - u0\right)\right)} \]
    2. sub-neg54.8%

      \[\leadsto \left(-\alpha\right) \cdot \left(\alpha \cdot \log \color{blue}{\left(1 + \left(-u0\right)\right)}\right) \]
    3. log1p-def99.1%

      \[\leadsto \left(-\alpha\right) \cdot \left(\alpha \cdot \color{blue}{\mathsf{log1p}\left(-u0\right)}\right) \]
  3. Simplified99.1%

    \[\leadsto \color{blue}{\left(-\alpha\right) \cdot \left(\alpha \cdot \mathsf{log1p}\left(-u0\right)\right)} \]
  4. Final simplification99.1%

    \[\leadsto \alpha \cdot \left(\left(-\alpha\right) \cdot \mathsf{log1p}\left(-u0\right)\right) \]

Alternative 2: 91.4% accurate, 7.2× speedup?

\[\begin{array}{l} \\ \left(\alpha \cdot \alpha\right) \cdot \left(u0 + \left(u0 \cdot u0\right) \cdot \left(u0 \cdot 0.3333333333333333 + 0.5\right)\right) \end{array} \]
(FPCore (alpha u0)
 :precision binary32
 (* (* alpha alpha) (+ u0 (* (* u0 u0) (+ (* u0 0.3333333333333333) 0.5)))))
float code(float alpha, float u0) {
	return (alpha * alpha) * (u0 + ((u0 * u0) * ((u0 * 0.3333333333333333f) + 0.5f)));
}
real(4) function code(alpha, u0)
    real(4), intent (in) :: alpha
    real(4), intent (in) :: u0
    code = (alpha * alpha) * (u0 + ((u0 * u0) * ((u0 * 0.3333333333333333e0) + 0.5e0)))
end function
function code(alpha, u0)
	return Float32(Float32(alpha * alpha) * Float32(u0 + Float32(Float32(u0 * u0) * Float32(Float32(u0 * Float32(0.3333333333333333)) + Float32(0.5)))))
end
function tmp = code(alpha, u0)
	tmp = (alpha * alpha) * (u0 + ((u0 * u0) * ((u0 * single(0.3333333333333333)) + single(0.5))));
end
\begin{array}{l}

\\
\left(\alpha \cdot \alpha\right) \cdot \left(u0 + \left(u0 \cdot u0\right) \cdot \left(u0 \cdot 0.3333333333333333 + 0.5\right)\right)
\end{array}
Derivation
  1. Initial program 54.8%

    \[\left(\left(-\alpha\right) \cdot \alpha\right) \cdot \log \left(1 - u0\right) \]
  2. Step-by-step derivation
    1. associate-*l*54.8%

      \[\leadsto \color{blue}{\left(-\alpha\right) \cdot \left(\alpha \cdot \log \left(1 - u0\right)\right)} \]
    2. sub-neg54.8%

      \[\leadsto \left(-\alpha\right) \cdot \left(\alpha \cdot \log \color{blue}{\left(1 + \left(-u0\right)\right)}\right) \]
    3. log1p-def99.1%

      \[\leadsto \left(-\alpha\right) \cdot \left(\alpha \cdot \color{blue}{\mathsf{log1p}\left(-u0\right)}\right) \]
  3. Simplified99.1%

    \[\leadsto \color{blue}{\left(-\alpha\right) \cdot \left(\alpha \cdot \mathsf{log1p}\left(-u0\right)\right)} \]
  4. Taylor expanded in u0 around 0 91.8%

    \[\leadsto \color{blue}{u0 \cdot {\alpha}^{2} + \left(0.3333333333333333 \cdot \left({u0}^{3} \cdot {\alpha}^{2}\right) + 0.5 \cdot \left({u0}^{2} \cdot {\alpha}^{2}\right)\right)} \]
  5. Step-by-step derivation
    1. *-commutative91.8%

      \[\leadsto \color{blue}{{\alpha}^{2} \cdot u0} + \left(0.3333333333333333 \cdot \left({u0}^{3} \cdot {\alpha}^{2}\right) + 0.5 \cdot \left({u0}^{2} \cdot {\alpha}^{2}\right)\right) \]
    2. +-commutative91.8%

      \[\leadsto {\alpha}^{2} \cdot u0 + \color{blue}{\left(0.5 \cdot \left({u0}^{2} \cdot {\alpha}^{2}\right) + 0.3333333333333333 \cdot \left({u0}^{3} \cdot {\alpha}^{2}\right)\right)} \]
    3. associate-*r*91.8%

      \[\leadsto {\alpha}^{2} \cdot u0 + \left(\color{blue}{\left(0.5 \cdot {u0}^{2}\right) \cdot {\alpha}^{2}} + 0.3333333333333333 \cdot \left({u0}^{3} \cdot {\alpha}^{2}\right)\right) \]
    4. associate-*r*91.8%

      \[\leadsto {\alpha}^{2} \cdot u0 + \left(\left(0.5 \cdot {u0}^{2}\right) \cdot {\alpha}^{2} + \color{blue}{\left(0.3333333333333333 \cdot {u0}^{3}\right) \cdot {\alpha}^{2}}\right) \]
    5. distribute-rgt-out91.8%

      \[\leadsto {\alpha}^{2} \cdot u0 + \color{blue}{{\alpha}^{2} \cdot \left(0.5 \cdot {u0}^{2} + 0.3333333333333333 \cdot {u0}^{3}\right)} \]
    6. distribute-lft-out91.9%

      \[\leadsto \color{blue}{{\alpha}^{2} \cdot \left(u0 + \left(0.5 \cdot {u0}^{2} + 0.3333333333333333 \cdot {u0}^{3}\right)\right)} \]
    7. unpow291.9%

      \[\leadsto \color{blue}{\left(\alpha \cdot \alpha\right)} \cdot \left(u0 + \left(0.5 \cdot {u0}^{2} + 0.3333333333333333 \cdot {u0}^{3}\right)\right) \]
    8. +-commutative91.9%

      \[\leadsto \left(\alpha \cdot \alpha\right) \cdot \left(u0 + \color{blue}{\left(0.3333333333333333 \cdot {u0}^{3} + 0.5 \cdot {u0}^{2}\right)}\right) \]
    9. cube-mult91.9%

      \[\leadsto \left(\alpha \cdot \alpha\right) \cdot \left(u0 + \left(0.3333333333333333 \cdot \color{blue}{\left(u0 \cdot \left(u0 \cdot u0\right)\right)} + 0.5 \cdot {u0}^{2}\right)\right) \]
    10. unpow291.9%

      \[\leadsto \left(\alpha \cdot \alpha\right) \cdot \left(u0 + \left(0.3333333333333333 \cdot \left(u0 \cdot \color{blue}{{u0}^{2}}\right) + 0.5 \cdot {u0}^{2}\right)\right) \]
    11. associate-*r*91.9%

      \[\leadsto \left(\alpha \cdot \alpha\right) \cdot \left(u0 + \left(\color{blue}{\left(0.3333333333333333 \cdot u0\right) \cdot {u0}^{2}} + 0.5 \cdot {u0}^{2}\right)\right) \]
    12. distribute-rgt-out91.9%

      \[\leadsto \left(\alpha \cdot \alpha\right) \cdot \left(u0 + \color{blue}{{u0}^{2} \cdot \left(0.3333333333333333 \cdot u0 + 0.5\right)}\right) \]
    13. unpow291.9%

      \[\leadsto \left(\alpha \cdot \alpha\right) \cdot \left(u0 + \color{blue}{\left(u0 \cdot u0\right)} \cdot \left(0.3333333333333333 \cdot u0 + 0.5\right)\right) \]
    14. *-commutative91.9%

      \[\leadsto \left(\alpha \cdot \alpha\right) \cdot \left(u0 + \left(u0 \cdot u0\right) \cdot \left(\color{blue}{u0 \cdot 0.3333333333333333} + 0.5\right)\right) \]
  6. Simplified91.9%

    \[\leadsto \color{blue}{\left(\alpha \cdot \alpha\right) \cdot \left(u0 + \left(u0 \cdot u0\right) \cdot \left(u0 \cdot 0.3333333333333333 + 0.5\right)\right)} \]
  7. Final simplification91.9%

    \[\leadsto \left(\alpha \cdot \alpha\right) \cdot \left(u0 + \left(u0 \cdot u0\right) \cdot \left(u0 \cdot 0.3333333333333333 + 0.5\right)\right) \]

Alternative 3: 87.3% accurate, 9.8× speedup?

\[\begin{array}{l} \\ \left(\alpha \cdot \alpha\right) \cdot \left(u0 + \left(u0 \cdot u0\right) \cdot 0.5\right) \end{array} \]
(FPCore (alpha u0)
 :precision binary32
 (* (* alpha alpha) (+ u0 (* (* u0 u0) 0.5))))
float code(float alpha, float u0) {
	return (alpha * alpha) * (u0 + ((u0 * u0) * 0.5f));
}
real(4) function code(alpha, u0)
    real(4), intent (in) :: alpha
    real(4), intent (in) :: u0
    code = (alpha * alpha) * (u0 + ((u0 * u0) * 0.5e0))
end function
function code(alpha, u0)
	return Float32(Float32(alpha * alpha) * Float32(u0 + Float32(Float32(u0 * u0) * Float32(0.5))))
end
function tmp = code(alpha, u0)
	tmp = (alpha * alpha) * (u0 + ((u0 * u0) * single(0.5)));
end
\begin{array}{l}

\\
\left(\alpha \cdot \alpha\right) \cdot \left(u0 + \left(u0 \cdot u0\right) \cdot 0.5\right)
\end{array}
Derivation
  1. Initial program 54.8%

    \[\left(\left(-\alpha\right) \cdot \alpha\right) \cdot \log \left(1 - u0\right) \]
  2. Step-by-step derivation
    1. associate-*l*54.8%

      \[\leadsto \color{blue}{\left(-\alpha\right) \cdot \left(\alpha \cdot \log \left(1 - u0\right)\right)} \]
    2. sub-neg54.8%

      \[\leadsto \left(-\alpha\right) \cdot \left(\alpha \cdot \log \color{blue}{\left(1 + \left(-u0\right)\right)}\right) \]
    3. log1p-def99.1%

      \[\leadsto \left(-\alpha\right) \cdot \left(\alpha \cdot \color{blue}{\mathsf{log1p}\left(-u0\right)}\right) \]
  3. Simplified99.1%

    \[\leadsto \color{blue}{\left(-\alpha\right) \cdot \left(\alpha \cdot \mathsf{log1p}\left(-u0\right)\right)} \]
  4. Taylor expanded in u0 around 0 88.1%

    \[\leadsto \color{blue}{u0 \cdot {\alpha}^{2} + 0.5 \cdot \left({u0}^{2} \cdot {\alpha}^{2}\right)} \]
  5. Step-by-step derivation
    1. associate-*r*88.1%

      \[\leadsto u0 \cdot {\alpha}^{2} + \color{blue}{\left(0.5 \cdot {u0}^{2}\right) \cdot {\alpha}^{2}} \]
    2. distribute-rgt-out88.2%

      \[\leadsto \color{blue}{{\alpha}^{2} \cdot \left(u0 + 0.5 \cdot {u0}^{2}\right)} \]
    3. unpow288.1%

      \[\leadsto \color{blue}{\left(\alpha \cdot \alpha\right)} \cdot \left(u0 + 0.5 \cdot {u0}^{2}\right) \]
    4. unpow288.1%

      \[\leadsto \left(\alpha \cdot \alpha\right) \cdot \left(u0 + 0.5 \cdot \color{blue}{\left(u0 \cdot u0\right)}\right) \]
  6. Simplified88.1%

    \[\leadsto \color{blue}{\left(\alpha \cdot \alpha\right) \cdot \left(u0 + 0.5 \cdot \left(u0 \cdot u0\right)\right)} \]
  7. Final simplification88.1%

    \[\leadsto \left(\alpha \cdot \alpha\right) \cdot \left(u0 + \left(u0 \cdot u0\right) \cdot 0.5\right) \]

Alternative 4: 74.6% accurate, 21.6× speedup?

\[\begin{array}{l} \\ u0 \cdot \left(\alpha \cdot \alpha\right) \end{array} \]
(FPCore (alpha u0) :precision binary32 (* u0 (* alpha alpha)))
float code(float alpha, float u0) {
	return u0 * (alpha * alpha);
}
real(4) function code(alpha, u0)
    real(4), intent (in) :: alpha
    real(4), intent (in) :: u0
    code = u0 * (alpha * alpha)
end function
function code(alpha, u0)
	return Float32(u0 * Float32(alpha * alpha))
end
function tmp = code(alpha, u0)
	tmp = u0 * (alpha * alpha);
end
\begin{array}{l}

\\
u0 \cdot \left(\alpha \cdot \alpha\right)
\end{array}
Derivation
  1. Initial program 54.8%

    \[\left(\left(-\alpha\right) \cdot \alpha\right) \cdot \log \left(1 - u0\right) \]
  2. Step-by-step derivation
    1. associate-*l*54.8%

      \[\leadsto \color{blue}{\left(-\alpha\right) \cdot \left(\alpha \cdot \log \left(1 - u0\right)\right)} \]
    2. sub-neg54.8%

      \[\leadsto \left(-\alpha\right) \cdot \left(\alpha \cdot \log \color{blue}{\left(1 + \left(-u0\right)\right)}\right) \]
    3. log1p-def99.1%

      \[\leadsto \left(-\alpha\right) \cdot \left(\alpha \cdot \color{blue}{\mathsf{log1p}\left(-u0\right)}\right) \]
  3. Simplified99.1%

    \[\leadsto \color{blue}{\left(-\alpha\right) \cdot \left(\alpha \cdot \mathsf{log1p}\left(-u0\right)\right)} \]
  4. Taylor expanded in u0 around 0 75.8%

    \[\leadsto \color{blue}{u0 \cdot {\alpha}^{2}} \]
  5. Step-by-step derivation
    1. *-commutative75.8%

      \[\leadsto \color{blue}{{\alpha}^{2} \cdot u0} \]
    2. unpow275.8%

      \[\leadsto \color{blue}{\left(\alpha \cdot \alpha\right)} \cdot u0 \]
  6. Simplified75.8%

    \[\leadsto \color{blue}{\left(\alpha \cdot \alpha\right) \cdot u0} \]
  7. Final simplification75.8%

    \[\leadsto u0 \cdot \left(\alpha \cdot \alpha\right) \]

Alternative 5: 74.6% accurate, 21.6× speedup?

\[\begin{array}{l} \\ \alpha \cdot \left(\alpha \cdot u0\right) \end{array} \]
(FPCore (alpha u0) :precision binary32 (* alpha (* alpha u0)))
float code(float alpha, float u0) {
	return alpha * (alpha * u0);
}
real(4) function code(alpha, u0)
    real(4), intent (in) :: alpha
    real(4), intent (in) :: u0
    code = alpha * (alpha * u0)
end function
function code(alpha, u0)
	return Float32(alpha * Float32(alpha * u0))
end
function tmp = code(alpha, u0)
	tmp = alpha * (alpha * u0);
end
\begin{array}{l}

\\
\alpha \cdot \left(\alpha \cdot u0\right)
\end{array}
Derivation
  1. Initial program 54.8%

    \[\left(\left(-\alpha\right) \cdot \alpha\right) \cdot \log \left(1 - u0\right) \]
  2. Step-by-step derivation
    1. associate-*l*54.8%

      \[\leadsto \color{blue}{\left(-\alpha\right) \cdot \left(\alpha \cdot \log \left(1 - u0\right)\right)} \]
    2. sub-neg54.8%

      \[\leadsto \left(-\alpha\right) \cdot \left(\alpha \cdot \log \color{blue}{\left(1 + \left(-u0\right)\right)}\right) \]
    3. log1p-def99.1%

      \[\leadsto \left(-\alpha\right) \cdot \left(\alpha \cdot \color{blue}{\mathsf{log1p}\left(-u0\right)}\right) \]
  3. Simplified99.1%

    \[\leadsto \color{blue}{\left(-\alpha\right) \cdot \left(\alpha \cdot \mathsf{log1p}\left(-u0\right)\right)} \]
  4. Taylor expanded in u0 around 0 75.8%

    \[\leadsto \left(-\alpha\right) \cdot \color{blue}{\left(-1 \cdot \left(u0 \cdot \alpha\right)\right)} \]
  5. Step-by-step derivation
    1. associate-*r*75.8%

      \[\leadsto \left(-\alpha\right) \cdot \color{blue}{\left(\left(-1 \cdot u0\right) \cdot \alpha\right)} \]
    2. mul-1-neg75.8%

      \[\leadsto \left(-\alpha\right) \cdot \left(\color{blue}{\left(-u0\right)} \cdot \alpha\right) \]
  6. Simplified75.8%

    \[\leadsto \left(-\alpha\right) \cdot \color{blue}{\left(\left(-u0\right) \cdot \alpha\right)} \]
  7. Final simplification75.8%

    \[\leadsto \alpha \cdot \left(\alpha \cdot u0\right) \]

Reproduce

?
herbie shell --seed 2023188 
(FPCore (alpha u0)
  :name "Beckmann Distribution sample, tan2theta, alphax == alphay"
  :precision binary32
  :pre (and (and (<= 0.0001 alpha) (<= alpha 1.0)) (and (<= 2.328306437e-10 u0) (<= u0 1.0)))
  (* (* (- alpha) alpha) (log (- 1.0 u0))))