Average Error: 37.7 → 12.9
Time: 12.1s
Precision: 64
Internal Precision: 128
\[0.5 \cdot \sqrt{2.0 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\]
\[\sqrt{(\left(\sqrt{re^2 + im^2}^*\right) \cdot 2.0 + \left(re \cdot 2.0\right))_*} \cdot 0.5\]

Error

Bits error versus re

Bits error versus im

Target

Original37.7
Target33.0
Herbie12.9
\[\begin{array}{l} \mathbf{if}\;re \lt 0:\\ \;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot \sqrt{\frac{im \cdot im}{\sqrt{re \cdot re + im \cdot im} - re}}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2.0 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\\ \end{array}\]

Derivation

  1. Initial program 37.7

    \[0.5 \cdot \sqrt{2.0 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\]
  2. Simplified12.9

    \[\leadsto \color{blue}{0.5 \cdot \sqrt{(\left(\sqrt{re^2 + im^2}^*\right) \cdot 2.0 + \left(re \cdot 2.0\right))_*}}\]
  3. Final simplification12.9

    \[\leadsto \sqrt{(\left(\sqrt{re^2 + im^2}^*\right) \cdot 2.0 + \left(re \cdot 2.0\right))_*} \cdot 0.5\]

Reproduce

herbie shell --seed 2019008 +o rules:numerics
(FPCore (re im)
  :name "math.sqrt on complex, real part"

  :herbie-target
  (if (< re 0) (* 0.5 (* (sqrt 2) (sqrt (/ (* im im) (- (sqrt (+ (* re re) (* im im))) re))))) (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))

  (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))

Details

Time bar (total: 10.3s)Debug log

sample64.0ms

Algorithm
intervals

simplify19.0ms

Counts
1 → 1
Calls

1 calls. Slowest were:

19.0ms
(* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re))))

prune26.0ms

Pruning

2 alts after pruning (2 fresh and 0 done)

Merged error: 13.6b

localize33.0ms

Local error

Found 3 expressions with local error:

0.5b
(fma (hypot re im) 2.0 (* re 2.0))
0.0b
(hypot re im)
0.0b
(sqrt (fma (hypot re im) 2.0 (* re 2.0)))

rewrite3.0ms

Algorithm
rewrite-expression-head
Counts
3 → 34
Calls

3 calls. Slowest were:

2.0ms
(sqrt (fma (hypot re im) 2.0 (* re 2.0)))
0.0ms
(fma (hypot re im) 2.0 (* re 2.0))
0.0ms
(hypot re im)

series232.0ms

Counts
3 → 9
Calls

3 calls. Slowest were:

132.0ms
(sqrt (fma (hypot re im) 2.0 (* re 2.0)))
62.0ms
(fma (hypot re im) 2.0 (* re 2.0))
38.0ms
(hypot re im)

simplify54.0ms

Counts
13 → 43
Calls

13 calls. Slowest were:

19.0ms
(sqrt (* (cbrt (fma (hypot re im) 2.0 (* re 2.0))) (cbrt (fma (hypot re im) 2.0 (* re 2.0)))))
14.0ms
(sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))
7.0ms
(sqrt (fma (hypot re im) 2.0 (* re 2.0)))

prune444.0ms

Pruning

5 alts after pruning (4 fresh and 1 done)

Merged error: 13.5b

localize29.0ms

Local error

Found 4 expressions with local error:

8.3b
(fma (* (sqrt (hypot re im)) (sqrt (hypot re im))) 2.0 (* re 2.0))
0.5b
(* (sqrt (hypot re im)) (sqrt (hypot re im)))
0.0b
(hypot re im)
0.0b
(hypot re im)

rewrite13.0ms

Algorithm
rewrite-expression-head
Counts
4 → 62
Calls

4 calls. Slowest were:

10.0ms
(* (sqrt (hypot re im)) (sqrt (hypot re im)))
0.0ms
(fma (* (sqrt (hypot re im)) (sqrt (hypot re im))) 2.0 (* re 2.0))
0.0ms
(hypot re im)

series131.0ms

Counts
4 → 12
Calls

4 calls. Slowest were:

43.0ms
(fma (* (sqrt (hypot re im)) (sqrt (hypot re im))) 2.0 (* re 2.0))
36.0ms
(hypot re im)
27.0ms
(hypot re im)
25.0ms
(* (sqrt (hypot re im)) (sqrt (hypot re im)))

simplify79.0ms

Counts
33 → 74
Calls

33 calls. Slowest were:

32.0ms
(* (* (* (sqrt (hypot re im)) (sqrt (hypot re im))) (sqrt (hypot re im))) (* (* (sqrt (hypot re im)) (sqrt (hypot re im))) (sqrt (hypot re im))))
5.0ms
(+ (* 2.0 re) (* 2.0 im))
3.0ms
(* (hypot re im) (hypot re im))

prune861.0ms

Pruning

8 alts after pruning (6 fresh and 2 done)

Merged error: 13.4b

localize21.0ms

Local error

Found 4 expressions with local error:

9.6b
(fma (* (* (sqrt (hypot re im)) (sqrt (sqrt (hypot re im)))) (sqrt (sqrt (hypot re im)))) 2.0 (* re 2.0))
0.4b
(* (sqrt (hypot re im)) (sqrt (sqrt (hypot re im))))
0.2b
(* (* (sqrt (hypot re im)) (sqrt (sqrt (hypot re im)))) (sqrt (sqrt (hypot re im))))
0.1b
(sqrt (sqrt (hypot re im)))

rewrite22.0ms

Algorithm
rewrite-expression-head
Counts
4 → 94
Calls

4 calls. Slowest were:

14.0ms
(* (* (sqrt (hypot re im)) (sqrt (sqrt (hypot re im)))) (sqrt (sqrt (hypot re im))))
5.0ms
(* (sqrt (hypot re im)) (sqrt (sqrt (hypot re im))))
1.0ms
(sqrt (sqrt (hypot re im)))

series313.0ms

Counts
4 → 12
Calls

4 calls. Slowest were:

115.0ms
(sqrt (sqrt (hypot re im)))
115.0ms
(* (sqrt (hypot re im)) (sqrt (sqrt (hypot re im))))
47.0ms
(fma (* (* (sqrt (hypot re im)) (sqrt (sqrt (hypot re im)))) (sqrt (sqrt (hypot re im)))) 2.0 (* re 2.0))
35.0ms
(* (* (sqrt (hypot re im)) (sqrt (sqrt (hypot re im)))) (sqrt (sqrt (hypot re im))))

simplify535.0ms

Counts
69 → 106
Calls

69 calls. Slowest were:

105.0ms
(* (* (* (* (sqrt (hypot re im)) (sqrt (sqrt (hypot re im)))) (* (sqrt (hypot re im)) (sqrt (sqrt (hypot re im))))) (* (sqrt (hypot re im)) (sqrt (sqrt (hypot re im))))) (* (* (sqrt (sqrt (hypot re im))) (sqrt (sqrt (hypot re im)))) (sqrt (sqrt (hypot re im)))))
91.0ms
(* (* (* (* (sqrt (hypot re im)) (sqrt (hypot re im))) (sqrt (hypot re im))) (* (* (sqrt (sqrt (hypot re im))) (sqrt (sqrt (hypot re im)))) (sqrt (sqrt (hypot re im))))) (* (* (sqrt (sqrt (hypot re im))) (sqrt (sqrt (hypot re im)))) (sqrt (sqrt (hypot re im)))))
50.0ms
(+ (* 1/8 (* (pow re 2) (pow (/ 1 (pow im 7)) 1/4))) (pow im 1/4))

prune1.7s

Pruning

13 alts after pruning (12 fresh and 1 done)

Merged error: 13.4b

localize47.0ms

Local error

Found 4 expressions with local error:

8.4b
(fma (* (* (sqrt (hypot re im)) (sqrt (sqrt (hypot re im)))) (* (fabs (cbrt (sqrt (hypot re im)))) (sqrt (cbrt (sqrt (hypot re im)))))) 2.0 (* re 2.0))
0.5b
(cbrt (sqrt (hypot re im)))
0.5b
(cbrt (sqrt (hypot re im)))
0.4b
(* (sqrt (hypot re im)) (sqrt (sqrt (hypot re im))))

rewrite22.0ms

Algorithm
rewrite-expression-head
Counts
4 → 75
Calls

4 calls. Slowest were:

17.0ms
(* (sqrt (hypot re im)) (sqrt (sqrt (hypot re im))))
2.0ms
(cbrt (sqrt (hypot re im)))
2.0ms
(cbrt (sqrt (hypot re im)))

series537.0ms

Counts
4 → 12
Calls

4 calls. Slowest were:

145.0ms
(* (sqrt (hypot re im)) (sqrt (sqrt (hypot re im))))
138.0ms
(cbrt (sqrt (hypot re im)))
131.0ms
(cbrt (sqrt (hypot re im)))
123.0ms
(fma (* (* (sqrt (hypot re im)) (sqrt (sqrt (hypot re im)))) (* (fabs (cbrt (sqrt (hypot re im)))) (sqrt (cbrt (sqrt (hypot re im)))))) 2.0 (* re 2.0))

simplify591.0ms

Counts
47 → 87
Calls

47 calls. Slowest were:

271.0ms
(+ (* 2.0 re) (+ (* 0.8333333333333334 (* (pow (/ 1 (pow im 7)) 1/6) (* (pow re 2) (fabs (pow (hypot re im) 1/6))))) (* 2.0 (* (pow (pow im 5) 1/6) (fabs (pow (hypot re im) 1/6))))))
54.0ms
(+ (pow im 3/4) (* 3/8 (* (pow re 2) (pow (/ 1 (pow im 5)) 1/4))))
51.0ms
(* (* (* (sqrt (hypot re im)) (sqrt (hypot re im))) (sqrt (hypot re im))) (* (* (sqrt (sqrt (hypot re im))) (sqrt (sqrt (hypot re im)))) (sqrt (sqrt (hypot re im)))))

prune1.5s

Pruning

11 alts after pruning (10 fresh and 1 done)

Merged error: 13.4b

regimes207.0ms

Accuracy

0% (0.2b remaining)

Error of 12.9b against oracle of 12.7b and baseline of 12.9b

bsearch1.0ms

end0.0ms

sample2.9s

Algorithm
intervals