Average Error: 37.9 → 13.6
Time: 11.9s
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.9
Target32.7
Herbie13.6
\[\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.9

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

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

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

sample88.0ms

Algorithm
intervals
Results
24.0ms180×body80valid
19.0ms22×body2560valid
17.0ms25×body1280valid
8.0msbody5120valid
5.0msbody640valid
2.0msbody320valid
2.0msbody160valid

simplify13.0ms

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

prune27.0ms

Pruning

2 alts after pruning (2 fresh and 0 done)

Merged error: 16.7b

localize15.0ms

Local error

Found 3 expressions with local error:

1.8b
(fma (hypot re im) 2.0 (* re 2.0))
0.1b
(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)

series236.0ms

Counts
3 → 9
Calls
3 calls:
Slowest
151.0ms
(sqrt (fma (hypot re im) 2.0 (* re 2.0)))
51.0ms
(fma (hypot re im) 2.0 (* re 2.0))
34.0ms
(hypot re im)

simplify52.0ms

Counts
13 → 43
Calls
13 calls:
Slowest
12.0ms
(sqrt (fma (hypot re im) 2.0 (* re 2.0)))
11.0ms
(+ (* 2.0 re) (* 2.0 im))
9.0ms
(sqrt (* (cbrt (fma (hypot re im) 2.0 (* re 2.0))) (cbrt (fma (hypot re im) 2.0 (* re 2.0)))))
8.0ms
(sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))
3.0ms
(* -1 re)

prune487.0ms

Pruning

7 alts after pruning (6 fresh and 1 done)

Merged error: 16.4b

localize17.0ms

Local error

Found 4 expressions with local error:

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

rewrite16.0ms

Algorithm
rewrite-expression-head
Rules
21×sqrt-prod
12×add-sqr-sqrt
11×add-cube-cbrt
11×*-un-lft-identity
associate-*l*
associate-*r*
pow1
add-exp-log
add-cbrt-cube
pow1/2
add-log-exp
log1p-expm1-u
expm1-log1p-u
fma-udef
pow-prod-up
pow-prod-down
cbrt-unprod
*-commutative
prod-exp
sqrt-unprod
pow-plus
rem-square-sqrt
rem-sqrt-square
pow2
Counts
4 → 72
Calls
4 calls:
Slowest
12.0ms
(* (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))) (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))))
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
(fma (hypot re im) 2.0 (* re 2.0))

series409.0ms

Counts
4 → 12
Calls
4 calls:
Slowest
149.0ms
(sqrt (fma (hypot re im) 2.0 (* re 2.0)))
149.0ms
(* (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))) (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))))
59.0ms
(fma (hypot re im) 2.0 (* re 2.0))
52.0ms
(fma (hypot re im) 2.0 (* re 2.0))

simplify420.0ms

Counts
44 → 84
Calls
44 calls:
Slowest
69.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))))))
20.0ms
(+ (log (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))) (log (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))))
17.0ms
(* (cbrt (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))) (cbrt (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))))
17.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 (fma (hypot re im) 2.0 (* re 2.0))) (sqrt (fma (hypot re im) 2.0 (* re 2.0))))

prune1.2s

Pruning

7 alts after pruning (5 fresh and 2 done)

Merged error: 16.4b

localize28.0ms

Local error

Found 4 expressions with local error:

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

rewrite16.0ms

Algorithm
rewrite-expression-head
Rules
10×add-sqr-sqrt
sqrt-prod
add-cube-cbrt
*-un-lft-identity
pow1
add-exp-log
associate-*l*
associate-*r*
add-cbrt-cube
pow1/2
add-log-exp
log1p-expm1-u
expm1-log1p-u
pow-prod-up
pow-prod-down
fma-udef
cbrt-unprod
*-commutative
prod-exp
sqrt-unprod
pow-plus
rem-square-sqrt
hypot-udef
rem-sqrt-square
pow2
Counts
4 → 66
Calls
4 calls:
Slowest
14.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))
0.0ms
(hypot re im)

series247.0ms

Counts
4 → 12
Calls
4 calls:
Slowest
137.0ms
(sqrt (fma (* (sqrt (hypot re im)) (sqrt (hypot re im))) 2.0 (* re 2.0)))
46.0ms
(fma (* (sqrt (hypot re im)) (sqrt (hypot re im))) 2.0 (* re 2.0))
39.0ms
(* (sqrt (hypot re im)) (sqrt (hypot re im)))
25.0ms
(hypot re im)

simplify175.0ms

Counts
37 → 78
Calls
37 calls:
Slowest
34.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))))
29.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)))))
26.0ms
(sqrt (fma (* (sqrt (hypot re im)) (sqrt (hypot re im))) 2.0 (* re 2.0)))
26.0ms
(sqrt (sqrt (fma (* (sqrt (hypot re im)) (sqrt (hypot re im))) 2.0 (* re 2.0))))
11.0ms
(+ (* 2.0 re) (* 2.0 im))

prune924.0ms

Pruning

14 alts after pruning (13 fresh and 1 done)

Merged error: 16.4b

localize39.0ms

Local error

Found 4 expressions with local error:

9.3b
(fma (* (* (sqrt (hypot re im)) (sqrt (* (cbrt (hypot re im)) (cbrt (hypot re im))))) (sqrt (cbrt (hypot re im)))) 2.0 (* re 2.0))
0.5b
(* (cbrt (hypot re im)) (cbrt (hypot re im)))
0.5b
(cbrt (hypot re im))
0.5b
(cbrt (hypot re im))

rewrite8.0ms

Algorithm
rewrite-expression-head
Rules
12×cbrt-prod
10×add-cube-cbrt
10×*-un-lft-identity
10×add-sqr-sqrt
pow1
add-exp-log
associate-*l*
associate-*r*
pow1/3
add-log-exp
log1p-expm1-u
add-cbrt-cube
expm1-log1p-u
pow-prod-up
pow-prod-down
fma-udef
cbrt-unprod
*-commutative
prod-exp
pow-plus
pow2
Counts
4 → 66
Calls
4 calls:
Slowest
5.0ms
(* (cbrt (hypot re im)) (cbrt (hypot re im)))
1.0ms
(cbrt (hypot re im))
1.0ms
(cbrt (hypot re im))
0.0ms
(fma (* (* (sqrt (hypot re im)) (sqrt (* (cbrt (hypot re im)) (cbrt (hypot re im))))) (sqrt (cbrt (hypot re im)))) 2.0 (* re 2.0))

series458.0ms

Counts
4 → 12
Calls
4 calls:
Slowest
148.0ms
(* (cbrt (hypot re im)) (cbrt (hypot re im)))
141.0ms
(cbrt (hypot re im))
122.0ms
(cbrt (hypot re im))
47.0ms
(fma (* (* (sqrt (hypot re im)) (sqrt (* (cbrt (hypot re im)) (cbrt (hypot re im))))) (sqrt (cbrt (hypot re im)))) 2.0 (* re 2.0))

simplify178.0ms

Counts
38 → 78
Calls
38 calls:
Slowest
43.0ms
(+ (* 1/6 (* (pow re 2) (pow (/ 1 (pow im 5)) 1/3))) (pow im 1/3))
41.0ms
(+ (pow im 2/3) (* 1/3 (* (pow re 2) (pow (/ 1 (pow im 4)) 1/3))))
31.0ms
(+ (* 1/6 (* (pow re 2) (pow (/ 1 (pow im 5)) 1/3))) (pow im 1/3))
5.0ms
(+ (* 2.0 re) (* 2.0 im))
4.0ms
(pow (/ 1 re) -1/3)

prune1.2s

Pruning

17 alts after pruning (16 fresh and 1 done)

Merged error: 16.4b

regimes276.0ms

Accuracy

0% (0.2b remaining)

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

bsearch1.0ms

end0.0ms

sample2.7s

Algorithm
intervals
Results
1.2s5985×body80valid
467.0ms657×body2560valid
330.0ms486×body1280valid
285.0ms310×body5120valid
191.0ms315×body640valid
73.0ms153×body320valid
21.0ms94×body160valid