Expression, p6

Percentage Accurate: 94.3% → 100.0%
Time: 4.5s
Alternatives: 8
Speedup: 3.0×

Specification

?
\[\left(a + \left(b + \left(c + d\right)\right)\right) \cdot 2 \]

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 8 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.

Alternative 1: 100.0% accurate, 0.1× speedup?

\[\mathsf{fma}\left(1, b + c, d + a\right) \cdot 2 \]
Derivation
  1. Initial program 94.6%

    \[\left(a + \left(b + \left(c + d\right)\right)\right) \cdot 2 \]
  2. Step-by-step derivation
    1. +-commutative94.6%

      \[\leadsto \color{blue}{\left(\left(b + \left(c + d\right)\right) + a\right)} \cdot 2 \]
    2. associate-+r+95.9%

      \[\leadsto \left(\color{blue}{\left(\left(b + c\right) + d\right)} + a\right) \cdot 2 \]
    3. associate-+l+100.0%

      \[\leadsto \color{blue}{\left(\left(b + c\right) + \left(d + a\right)\right)} \cdot 2 \]
    4. *-un-lft-identity100.0%

      \[\leadsto \left(\color{blue}{1 \cdot \left(b + c\right)} + \left(d + a\right)\right) \cdot 2 \]
    5. fma-def100.0%

      \[\leadsto \color{blue}{\mathsf{fma}\left(1, b + c, d + a\right)} \cdot 2 \]
  3. Applied egg-rr100.0%

    \[\leadsto \color{blue}{\mathsf{fma}\left(1, b + c, d + a\right)} \cdot 2 \]
  4. Final simplification100.0%

    \[\leadsto \mathsf{fma}\left(1, b + c, d + a\right) \cdot 2 \]

Alternative 2: 94.3% accurate, 1.0× speedup?

\[2 \cdot \left(a + \left(b + \left(c + d\right)\right)\right) \]
Derivation
  1. Initial program 94.6%

    \[\left(a + \left(b + \left(c + d\right)\right)\right) \cdot 2 \]
  2. Final simplification94.6%

    \[\leadsto 2 \cdot \left(a + \left(b + \left(c + d\right)\right)\right) \]

Alternative 3: 95.7% accurate, 1.0× speedup?

\[2 \cdot \left(c + \left(a + \left(b + d\right)\right)\right) \]
Derivation
  1. Initial program 94.6%

    \[\left(a + \left(b + \left(c + d\right)\right)\right) \cdot 2 \]
  2. Taylor expanded in a around 0 95.7%

    \[\leadsto \color{blue}{\left(c + \left(a + \left(d + b\right)\right)\right)} \cdot 2 \]
  3. Final simplification95.7%

    \[\leadsto 2 \cdot \left(c + \left(a + \left(b + d\right)\right)\right) \]

Alternative 4: 100.0% accurate, 1.0× speedup?

\[2 \cdot \left(c + \left(b + \left(d + a\right)\right)\right) \]
Derivation
  1. Initial program 94.6%

    \[\left(a + \left(b + \left(c + d\right)\right)\right) \cdot 2 \]
  2. Taylor expanded in a around 0 95.7%

    \[\leadsto \color{blue}{\left(c + \left(a + \left(d + b\right)\right)\right)} \cdot 2 \]
  3. Step-by-step derivation
    1. associate-+r+100.0%

      \[\leadsto \left(c + \color{blue}{\left(\left(a + d\right) + b\right)}\right) \cdot 2 \]
    2. flip-+98.6%

      \[\leadsto \left(c + \color{blue}{\frac{\left(a + d\right) \cdot \left(a + d\right) - b \cdot b}{\left(a + d\right) - b}}\right) \cdot 2 \]
  4. Applied egg-rr98.6%

    \[\leadsto \left(c + \color{blue}{\frac{\left(a + d\right) \cdot \left(a + d\right) - b \cdot b}{\left(a + d\right) - b}}\right) \cdot 2 \]
  5. Step-by-step derivation
    1. flip-+100.0%

      \[\leadsto \left(c + \color{blue}{\left(\left(a + d\right) + b\right)}\right) \cdot 2 \]
  6. Applied egg-rr100.0%

    \[\leadsto \left(c + \color{blue}{\left(\left(a + d\right) + b\right)}\right) \cdot 2 \]
  7. Final simplification100.0%

    \[\leadsto 2 \cdot \left(c + \left(b + \left(d + a\right)\right)\right) \]

Alternative 5: 14.9% accurate, 1.3× speedup?

\[\begin{array}{l} \mathbf{if}\;a \leq -13.71:\\ \;\;\;\;\left(d + a\right) \cdot 2\\ \mathbf{else}:\\ \;\;\;\;\left(b + c\right) \cdot 2\\ \end{array} \]
Derivation
  1. Split input into 2 regimes
  2. if a < -13.710000000000001

    1. Initial program 93.7%

      \[\left(a + \left(b + \left(c + d\right)\right)\right) \cdot 2 \]
    2. Taylor expanded in b around 0 5.9%

      \[\leadsto \left(a + \color{blue}{\left(c + d\right)}\right) \cdot 2 \]
    3. Taylor expanded in c around 0 13.1%

      \[\leadsto \color{blue}{\left(a + d\right)} \cdot 2 \]
    4. Step-by-step derivation
      1. +-commutative13.1%

        \[\leadsto \color{blue}{\left(d + a\right)} \cdot 2 \]
    5. Simplified13.1%

      \[\leadsto \color{blue}{\left(d + a\right)} \cdot 2 \]

    if -13.710000000000001 < a

    1. Initial program 94.9%

      \[\left(a + \left(b + \left(c + d\right)\right)\right) \cdot 2 \]
    2. Taylor expanded in a around 0 96.0%

      \[\leadsto \color{blue}{\left(c + \left(a + \left(d + b\right)\right)\right)} \cdot 2 \]
    3. Taylor expanded in b around inf 17.1%

      \[\leadsto \left(c + \color{blue}{b}\right) \cdot 2 \]
  3. Recombined 2 regimes into one program.
  4. Final simplification16.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;a \leq -13.71:\\ \;\;\;\;\left(d + a\right) \cdot 2\\ \mathbf{else}:\\ \;\;\;\;\left(b + c\right) \cdot 2\\ \end{array} \]

Alternative 6: 12.6% accurate, 1.8× speedup?

\[\begin{array}{l} \mathbf{if}\;a \leq -13.71:\\ \;\;\;\;b \cdot 2\\ \mathbf{else}:\\ \;\;\;\;c \cdot 2\\ \end{array} \]
Derivation
  1. Split input into 2 regimes
  2. if a < -13.710000000000001

    1. Initial program 93.7%

      \[\left(a + \left(b + \left(c + d\right)\right)\right) \cdot 2 \]
    2. Taylor expanded in b around inf 11.8%

      \[\leadsto \color{blue}{b} \cdot 2 \]

    if -13.710000000000001 < a

    1. Initial program 94.9%

      \[\left(a + \left(b + \left(c + d\right)\right)\right) \cdot 2 \]
    2. Taylor expanded in c around inf 14.0%

      \[\leadsto \color{blue}{c} \cdot 2 \]
  3. Recombined 2 regimes into one program.
  4. Final simplification13.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;a \leq -13.71:\\ \;\;\;\;b \cdot 2\\ \mathbf{else}:\\ \;\;\;\;c \cdot 2\\ \end{array} \]

Alternative 7: 13.8% accurate, 1.8× speedup?

\[\left(b + c\right) \cdot 2 \]
Derivation
  1. Initial program 94.6%

    \[\left(a + \left(b + \left(c + d\right)\right)\right) \cdot 2 \]
  2. Taylor expanded in a around 0 95.7%

    \[\leadsto \color{blue}{\left(c + \left(a + \left(d + b\right)\right)\right)} \cdot 2 \]
  3. Taylor expanded in b around inf 14.2%

    \[\leadsto \left(c + \color{blue}{b}\right) \cdot 2 \]
  4. Final simplification14.2%

    \[\leadsto \left(b + c\right) \cdot 2 \]

Alternative 8: 6.3% accurate, 3.0× speedup?

\[b \cdot 2 \]
Derivation
  1. Initial program 94.6%

    \[\left(a + \left(b + \left(c + d\right)\right)\right) \cdot 2 \]
  2. Taylor expanded in b around inf 6.2%

    \[\leadsto \color{blue}{b} \cdot 2 \]
  3. Final simplification6.2%

    \[\leadsto b \cdot 2 \]

Developer target: 94.0% accurate, 0.8× speedup?

\[\left(a + b\right) \cdot 2 + \left(c + d\right) \cdot 2 \]

Reproduce

?
herbie shell --seed 2023167 
(FPCore (a b c d)
  :name "Expression, p6"
  :precision binary64
  :pre (and (and (and (and (<= -14.0 a) (<= a -13.0)) (and (<= -3.0 b) (<= b -2.0))) (and (<= 3.0 c) (<= c 3.5))) (and (<= 12.5 d) (<= d 13.5)))

  :herbie-target
  (+ (* (+ a b) 2.0) (* (+ c d) 2.0))

  (* (+ a (+ b (+ c d))) 2.0))