Average Error: 0.5 → 1.0
Time: 18.8s
Precision: 64
\[\log \left(1 + e^{x}\right) - x \cdot y\]
\[\mathsf{fma}\left(\left(\sqrt{\mathsf{log1p}\left(\left(e^{x}\right)\right)}\right), \left(\sqrt{\mathsf{log1p}\left(\left(e^{x}\right)\right)}\right), \left(x \cdot \left(-y\right)\right)\right)\]
\log \left(1 + e^{x}\right) - x \cdot y
\mathsf{fma}\left(\left(\sqrt{\mathsf{log1p}\left(\left(e^{x}\right)\right)}\right), \left(\sqrt{\mathsf{log1p}\left(\left(e^{x}\right)\right)}\right), \left(x \cdot \left(-y\right)\right)\right)
double f(double x, double y) {
        double r5401434 = 1.0;
        double r5401435 = x;
        double r5401436 = exp(r5401435);
        double r5401437 = r5401434 + r5401436;
        double r5401438 = log(r5401437);
        double r5401439 = y;
        double r5401440 = r5401435 * r5401439;
        double r5401441 = r5401438 - r5401440;
        return r5401441;
}

double f(double x, double y) {
        double r5401442 = x;
        double r5401443 = exp(r5401442);
        double r5401444 = log1p(r5401443);
        double r5401445 = sqrt(r5401444);
        double r5401446 = y;
        double r5401447 = -r5401446;
        double r5401448 = r5401442 * r5401447;
        double r5401449 = fma(r5401445, r5401445, r5401448);
        return r5401449;
}

Error

Bits error versus x

Bits error versus y

Target

Original0.5
Target0.0
Herbie1.0
\[\begin{array}{l} \mathbf{if}\;x \le 0:\\ \;\;\;\;\log \left(1 + e^{x}\right) - x \cdot y\\ \mathbf{else}:\\ \;\;\;\;\log \left(1 + e^{-x}\right) - \left(-x\right) \cdot \left(1 - y\right)\\ \end{array}\]

Derivation

  1. Initial program 0.5

    \[\log \left(1 + e^{x}\right) - x \cdot y\]
  2. Simplified0.5

    \[\leadsto \color{blue}{\mathsf{log1p}\left(\left(e^{x}\right)\right) - y \cdot x}\]
  3. Using strategy rm
  4. Applied add-sqr-sqrt1.0

    \[\leadsto \color{blue}{\sqrt{\mathsf{log1p}\left(\left(e^{x}\right)\right)} \cdot \sqrt{\mathsf{log1p}\left(\left(e^{x}\right)\right)}} - y \cdot x\]
  5. Applied fma-neg1.0

    \[\leadsto \color{blue}{\mathsf{fma}\left(\left(\sqrt{\mathsf{log1p}\left(\left(e^{x}\right)\right)}\right), \left(\sqrt{\mathsf{log1p}\left(\left(e^{x}\right)\right)}\right), \left(-y \cdot x\right)\right)}\]
  6. Final simplification1.0

    \[\leadsto \mathsf{fma}\left(\left(\sqrt{\mathsf{log1p}\left(\left(e^{x}\right)\right)}\right), \left(\sqrt{\mathsf{log1p}\left(\left(e^{x}\right)\right)}\right), \left(x \cdot \left(-y\right)\right)\right)\]

Reproduce

herbie shell --seed 2019132 +o rules:numerics
(FPCore (x y)
  :name "Logistic regression 2"

  :herbie-target
  (if (<= x 0) (- (log (+ 1 (exp x))) (* x y)) (- (log (+ 1 (exp (- x)))) (* (- x) (- 1 y))))

  (- (log (+ 1 (exp x))) (* x y)))