Average Error: 37.5 → 13.1
Time: 13.3s
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.5
Target32.8
Herbie13.1
\[\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.5

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

    \[\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 simplification13.1

    \[\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 2019005 +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: 11.6s)Debug log

sample69.0ms

Algorithm
intervals

simplify27.0ms

Counts
1 → 1
Calls

1 calls. Slowest were:

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

prune23.0ms

Pruning

2 alts after pruning (2 fresh and 0 done)

Merged error: 12.9b

localize17.0ms

Local error

Found 3 expressions with local error:

0.3b
(fma (hypot re im) 2.0 (* re 2.0))
0.2b
(sqrt (fma (hypot re im) 2.0 (* re 2.0)))
0.0b
(hypot re im)

rewrite13.0ms

Algorithm
rewrite-expression-head
Counts
3 → 34
Calls

3 calls. Slowest were:

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

series189.0ms

Counts
3 → 9
Calls

3 calls. Slowest were:

131.0ms
(sqrt (fma (hypot re im) 2.0 (* re 2.0)))
34.0ms
(fma (hypot re im) 2.0 (* re 2.0))
24.0ms
(hypot re im)

simplify49.0ms

Counts
13 → 43
Calls

13 calls. Slowest were:

11.0ms
(+ (* 2.0 re) (* 2.0 im))
10.0ms
(sqrt (fma (hypot re im) 2.0 (* re 2.0)))
9.0ms
(sqrt (* (cbrt (fma (hypot re im) 2.0 (* re 2.0))) (cbrt (fma (hypot re im) 2.0 (* re 2.0)))))

prune475.0ms

Pruning

8 alts after pruning (7 fresh and 1 done)

Merged error: 12.8b

localize9.0ms

Local error

Found 4 expressions with local error:

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

rewrite9.0ms

Algorithm
rewrite-expression-head
Counts
4 → 72
Calls

4 calls. Slowest were:

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

series385.0ms

Counts
4 → 12
Calls

4 calls. Slowest were:

150.0ms
(* (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))) (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))))
145.0ms
(sqrt (fma (hypot re im) 2.0 (* re 2.0)))
52.0ms
(fma (hypot re im) 2.0 (* re 2.0))
37.0ms
(fma (hypot re im) 2.0 (* re 2.0))

simplify479.0ms

Counts
44 → 84
Calls

44 calls. Slowest were:

94.0ms
(* (* (* (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))) (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))) (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))) (* (* (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))) (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))) (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))))
18.0ms
(cbrt (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))))
18.0ms
(sqrt (* (cbrt (fma (hypot re im) 2.0 (* re 2.0))) (cbrt (fma (hypot re im) 2.0 (* re 2.0)))))

prune1.0s

Pruning

6 alts after pruning (5 fresh and 1 done)

Merged error: 12.8b

localize24.0ms

Local error

Found 4 expressions with local error:

0.3b
(* (sqrt (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))) (* (sqrt (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))) (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))))
0.3b
(* (sqrt (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))) (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))))
0.3b
(fma (hypot re im) 2.0 (* re 2.0))
0.3b
(fma (hypot re im) 2.0 (* re 2.0))

rewrite51.0ms

Algorithm
rewrite-expression-head
Counts
4 → 97
Calls

4 calls. Slowest were:

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

series519.0ms

Counts
4 → 12
Calls

4 calls. Slowest were:

273.0ms
(* (sqrt (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))) (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))))
153.0ms
(* (sqrt (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))) (* (sqrt (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))) (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))))
55.0ms
(fma (hypot re im) 2.0 (* re 2.0))
39.0ms
(fma (hypot re im) 2.0 (* re 2.0))

simplify2.1s

Counts
70 → 109
Calls

70 calls. Slowest were:

347.0ms
(exp (* 1/8 (- (+ (* 3 (log (/ -1 re))) (log 1.0)) (* 6 (log (/ -1 im))))))
258.0ms
(* (* (* (sqrt (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))) (sqrt (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))))) (sqrt (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))))) (* (* (* (sqrt (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))) (sqrt (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))))) (sqrt (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))))) (* (* (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))) (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))) (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))))))
232.0ms
(* (* (* (sqrt (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))) (sqrt (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))))) (sqrt (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))))) (* (* (* (sqrt (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))) (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))) (* (sqrt (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))) (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))))) (* (sqrt (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))) (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))))))

prune1.6s

Pruning

6 alts after pruning (4 fresh and 2 done)

Merged error: 12.8b

localize14.0ms

Local error

Found 4 expressions with local error:

7.5b
(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.2b
(sqrt (fma (* (sqrt (hypot re im)) (sqrt (hypot re im))) 2.0 (* re 2.0)))
0.0b
(hypot re im)

rewrite7.0ms

Algorithm
rewrite-expression-head
Counts
4 → 66
Calls

4 calls. Slowest were:

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

series241.0ms

Counts
4 → 12
Calls

4 calls. Slowest were:

151.0ms
(sqrt (fma (* (sqrt (hypot re im)) (sqrt (hypot re im))) 2.0 (* re 2.0)))
35.0ms
(fma (* (sqrt (hypot re im)) (sqrt (hypot re im))) 2.0 (* re 2.0))
28.0ms
(hypot re im)
26.0ms
(* (sqrt (hypot re im)) (sqrt (hypot re im)))

simplify138.0ms

Counts
37 → 78
Calls

37 calls. Slowest were:

30.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))))
24.0ms
(sqrt (fma (* (sqrt (hypot re im)) (sqrt (hypot re im))) 2.0 (* re 2.0)))
19.0ms
(sqrt (* (cbrt (fma (* (sqrt (hypot re im)) (sqrt (hypot re im))) 2.0 (* re 2.0))) (cbrt (fma (* (sqrt (hypot re im)) (sqrt (hypot re im))) 2.0 (* re 2.0)))))

prune898.0ms

Pruning

11 alts after pruning (9 fresh and 2 done)

Merged error: 12.7b

regimes239.0ms

Accuracy

0% (0.2b remaining)

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

bsearch1.0ms

end0.0ms

sample3.1s

Algorithm
intervals