Average Error: 37.8 → 13.4
Time: 9.2s
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.8
Target32.8
Herbie13.4
\[\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.8

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

    \[\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.4

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

sample89.0ms

Algorithm
intervals
Results
256×(pre true 80)
173×(body real 80)
22×(body real 5120)
21×(body real 1280)
20×(body real 2560)
10×(body real 640)
(body real 160)
(body real 320)

simplify21.0ms

Counts
1 → 1
Calls
1 calls:
Slowest
21.0ms
(* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re))))

prune37.0ms

Pruning

2 alts after pruning (2 fresh and 0 done)

Merged error: 15.9b

localize15.0ms

Local error

Found 3 expressions with local error:

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

rewrite2.0ms

Algorithm
rewrite-expression-head
Rules
add-sqr-sqrt
add-cube-cbrt
*-un-lft-identity
add-log-exp
log1p-expm1-u
sqrt-prod
add-exp-log
add-cbrt-cube
pow1
expm1-log1p-u
fma-udef
pow1/2
hypot-udef
rem-sqrt-square
Counts
3 → 34
Calls
3 calls:
Slowest
1.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)

series215.0ms

Counts
3 → 9
Calls
3 calls:
Slowest
143.0ms
(sqrt (fma (hypot re im) 2.0 (* re 2.0)))
36.0ms
(hypot re im)
36.0ms
(fma (hypot re im) 2.0 (* re 2.0))

simplify61.0ms

Counts
13 → 43
Calls
13 calls:
Slowest
18.0ms
(sqrt (* (cbrt (fma (hypot re im) 2.0 (* re 2.0))) (cbrt (fma (hypot re im) 2.0 (* re 2.0)))))
17.0ms
(sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))
10.0ms
(sqrt (fma (hypot re im) 2.0 (* re 2.0)))
6.0ms
(+ (* 2.0 re) (* 2.0 im))
2.0ms
(sqrt 1)

prune440.0ms

Pruning

8 alts after pruning (7 fresh and 1 done)

Merged error: 15.7b

localize13.0ms

Local error

Found 4 expressions with local error:

1.5b
(fma (hypot re im) 2.0 (* re 2.0))
1.5b
(fma (hypot re im) 2.0 (* re 2.0))
1.5b
(fma (hypot re im) 2.0 (* re 2.0))
0.4b
(cbrt (fma (hypot re im) 2.0 (* re 2.0)))

rewrite1.0ms

Algorithm
rewrite-expression-head
Rules
add-cube-cbrt
*-un-lft-identity
add-sqr-sqrt
add-log-exp
log1p-expm1-u
add-exp-log
add-cbrt-cube
pow1
expm1-log1p-u
fma-udef
cbrt-prod
pow1/3
Counts
4 → 43
Calls
4 calls:
Slowest
1.0ms
(cbrt (fma (hypot re im) 2.0 (* re 2.0)))
0.0ms
(fma (hypot re im) 2.0 (* re 2.0))
0.0ms
(fma (hypot re im) 2.0 (* re 2.0))
0.0ms
(fma (hypot re im) 2.0 (* re 2.0))

series260.0ms

Counts
4 → 12
Calls
4 calls:
Slowest
134.0ms
(cbrt (fma (hypot re im) 2.0 (* re 2.0)))
45.0ms
(fma (hypot re im) 2.0 (* re 2.0))
43.0ms
(fma (hypot re im) 2.0 (* re 2.0))
38.0ms
(fma (hypot re im) 2.0 (* re 2.0))

simplify482.0ms

Counts
15 → 55
Calls
15 calls:
Slowest
206.0ms
(+ (exp (* 1/3 (+ (log im) (log 2.0)))) (+ (* 0.3333333333333333 (* re (exp (* 1/3 (- (log 2.0) (* 2 (log im))))))) (* 0.05555555555555555 (* (pow re 2) (exp (* 1/3 (- (log 2.0) (* 5 (log im)))))))))
147.0ms
(exp (* 1/3 (- (+ (log (/ -1 re)) (log 1.0)) (* 2 (log (/ -1 im))))))
66.0ms
(exp (* 1/3 (- (log 4.0) (log (/ 1 re)))))
12.0ms
(+ (* 2.0 re) (* 2.0 im))
11.0ms
(+ (* 2.0 re) (* 2.0 im))

prune847.0ms

Pruning

8 alts after pruning (7 fresh and 1 done)

Merged error: 15.7b

localize11.0ms

Local error

Found 4 expressions with local error:

1.5b
(fma (hypot re im) 2.0 (* re 2.0))
1.5b
(fma (hypot re im) 2.0 (* re 2.0))
1.5b
(fma (hypot re im) 2.0 (* re 2.0))
0.4b
(cbrt (fma (hypot re im) 2.0 (* re 2.0)))

rewrite1.0ms

Algorithm
rewrite-expression-head
Rules
add-cube-cbrt
*-un-lft-identity
add-sqr-sqrt
add-log-exp
log1p-expm1-u
add-exp-log
add-cbrt-cube
pow1
expm1-log1p-u
fma-udef
cbrt-prod
pow1/3
Counts
4 → 43
Calls
4 calls:
Slowest
1.0ms
(cbrt (fma (hypot re im) 2.0 (* re 2.0)))
0.0ms
(fma (hypot re im) 2.0 (* re 2.0))
0.0ms
(fma (hypot re im) 2.0 (* re 2.0))
0.0ms
(fma (hypot re im) 2.0 (* re 2.0))

series297.0ms

Counts
4 → 12
Calls
4 calls:
Slowest
163.0ms
(cbrt (fma (hypot re im) 2.0 (* re 2.0)))
47.0ms
(fma (hypot re im) 2.0 (* re 2.0))
44.0ms
(fma (hypot re im) 2.0 (* re 2.0))
43.0ms
(fma (hypot re im) 2.0 (* re 2.0))

simplify450.0ms

Counts
15 → 55
Calls
15 calls:
Slowest
223.0ms
(+ (exp (* 1/3 (+ (log im) (log 2.0)))) (+ (* 0.3333333333333333 (* re (exp (* 1/3 (- (log 2.0) (* 2 (log im))))))) (* 0.05555555555555555 (* (pow re 2) (exp (* 1/3 (- (log 2.0) (* 5 (log im)))))))))
111.0ms
(exp (* 1/3 (- (+ (log (/ -1 re)) (log 1.0)) (* 2 (log (/ -1 im))))))
52.0ms
(exp (* 1/3 (- (log 4.0) (log (/ 1 re)))))
21.0ms
(cbrt (* (cbrt (fma (hypot re im) 2.0 (* re 2.0))) (cbrt (fma (hypot re im) 2.0 (* re 2.0)))))
10.0ms
(cbrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))

prune755.0ms

Pruning

8 alts after pruning (6 fresh and 2 done)

Merged error: 15.7b

localize20.0ms

Local error

Found 4 expressions with local error:

1.5b
(fma (hypot re im) 2.0 (* re 2.0))
1.5b
(fma (hypot re im) 2.0 (* re 2.0))
1.5b
(fma (hypot re im) 2.0 (* re 2.0))
1.5b
(fma (hypot re im) 2.0 (* re 2.0))

rewrite1.0ms

Algorithm
rewrite-expression-head
Rules
add-log-exp
log1p-expm1-u
fma-udef
add-cube-cbrt
add-exp-log
add-cbrt-cube
*-un-lft-identity
pow1
expm1-log1p-u
add-sqr-sqrt
Counts
4 → 40
Calls
4 calls:
Slowest
0.0ms
(fma (hypot re im) 2.0 (* re 2.0))
0.0ms
(fma (hypot re im) 2.0 (* re 2.0))
0.0ms
(fma (hypot re im) 2.0 (* re 2.0))
0.0ms
(fma (hypot re im) 2.0 (* re 2.0))

series182.0ms

Counts
4 → 12
Calls
4 calls:
Slowest
58.0ms
(fma (hypot re im) 2.0 (* re 2.0))
52.0ms
(fma (hypot re im) 2.0 (* re 2.0))
38.0ms
(fma (hypot re im) 2.0 (* re 2.0))
34.0ms
(fma (hypot re im) 2.0 (* re 2.0))

simplify34.0ms

Counts
12 → 52
Calls
12 calls:
Slowest
6.0ms
(+ (* 2.0 re) (* 2.0 im))
6.0ms
(+ (* 2.0 re) (* 2.0 im))
6.0ms
(+ (* 2.0 re) (* 2.0 im))
6.0ms
(+ (* 2.0 re) (* 2.0 im))
1.0ms
(* 4.0 re)

prune730.0ms

Pruning

8 alts after pruning (6 fresh and 2 done)

Merged error: 15.7b

regimes152.0ms

Accuracy

0% (0.2b remaining)

Error of 13.4b against oracle of 13.2b and baseline of 13.4b

bsearch1.0ms

end0.0ms

sample3.1s

Algorithm
intervals
Results
8000×(pre true 80)
5986×(body real 80)
699×(body real 2560)
468×(body real 1280)
333×(body real 5120)
267×(body real 640)
159×(body real 320)
88×(body real 160)