Average Error: 37.2 → 25.9
Time: 17.7s
Precision: 64
Internal Precision: 128
\[0.5 \cdot \sqrt{2.0 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\]
\[\begin{array}{l} \mathbf{if}\;\sqrt{re \cdot re + im \cdot im} + re \le 0.0:\\ \;\;\;\;0.5 \cdot \frac{\sqrt{\left(im \cdot 2.0\right) \cdot im}}{\sqrt{\sqrt{re \cdot re + im \cdot im} - re}}\\ \mathbf{elif}\;\sqrt{re \cdot re + im \cdot im} + re \le 4.0702352114533017 \cdot 10^{+148}:\\ \;\;\;\;\sqrt{2.0 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;\sqrt{2.0 \cdot \left(re + im\right)} \cdot 0.5\\ \end{array}\]

Error

Bits error versus re

Bits error versus im

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original37.2
Target32.2
Herbie25.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. Split input into 3 regimes
  2. if (+ (sqrt (+ (* re re) (* im im))) re) < 0.0

    1. Initial program 58.8

      \[0.5 \cdot \sqrt{2.0 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\]
    2. Using strategy rm
    3. Applied flip-+58.6

      \[\leadsto 0.5 \cdot \sqrt{2.0 \cdot \color{blue}{\frac{\sqrt{re \cdot re + im \cdot im} \cdot \sqrt{re \cdot re + im \cdot im} - re \cdot re}{\sqrt{re \cdot re + im \cdot im} - re}}}\]
    4. Applied associate-*r/58.6

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{2.0 \cdot \left(\sqrt{re \cdot re + im \cdot im} \cdot \sqrt{re \cdot re + im \cdot im} - re \cdot re\right)}{\sqrt{re \cdot re + im \cdot im} - re}}}\]
    5. Applied sqrt-div58.6

      \[\leadsto 0.5 \cdot \color{blue}{\frac{\sqrt{2.0 \cdot \left(\sqrt{re \cdot re + im \cdot im} \cdot \sqrt{re \cdot re + im \cdot im} - re \cdot re\right)}}{\sqrt{\sqrt{re \cdot re + im \cdot im} - re}}}\]
    6. Simplified33.6

      \[\leadsto 0.5 \cdot \frac{\color{blue}{\sqrt{im \cdot \left(im \cdot 2.0\right)}}}{\sqrt{\sqrt{re \cdot re + im \cdot im} - re}}\]

    if 0.0 < (+ (sqrt (+ (* re re) (* im im))) re) < 4.0702352114533017e+148

    1. Initial program 4.1

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

    if 4.0702352114533017e+148 < (+ (sqrt (+ (* re re) (* im im))) re)

    1. Initial program 60.5

      \[0.5 \cdot \sqrt{2.0 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\]
    2. Using strategy rm
    3. Applied add-cube-cbrt60.6

      \[\leadsto 0.5 \cdot \sqrt{2.0 \cdot \left(\sqrt{\color{blue}{\left(\sqrt[3]{re \cdot re + im \cdot im} \cdot \sqrt[3]{re \cdot re + im \cdot im}\right) \cdot \sqrt[3]{re \cdot re + im \cdot im}}} + re\right)}\]
    4. Taylor expanded around 0 43.5

      \[\leadsto 0.5 \cdot \sqrt{2.0 \cdot \left(\color{blue}{im} + re\right)}\]
  3. Recombined 3 regimes into one program.
  4. Final simplification25.9

    \[\leadsto \begin{array}{l} \mathbf{if}\;\sqrt{re \cdot re + im \cdot im} + re \le 0.0:\\ \;\;\;\;0.5 \cdot \frac{\sqrt{\left(im \cdot 2.0\right) \cdot im}}{\sqrt{\sqrt{re \cdot re + im \cdot im} - re}}\\ \mathbf{elif}\;\sqrt{re \cdot re + im \cdot im} + re \le 4.0702352114533017 \cdot 10^{+148}:\\ \;\;\;\;\sqrt{2.0 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;\sqrt{2.0 \cdot \left(re + im\right)} \cdot 0.5\\ \end{array}\]

Reproduce

herbie shell --seed 2019018 
(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: 16.6s)Debug log

sample64.0ms

Algorithm
intervals
Results
19.0ms188×body80valid
15.0ms24×body2560valid
8.0ms17×body1280valid
6.0msbody5120valid
4.0ms10×body640valid
1.0msbody320valid
1.0msbody160valid

simplify18.0ms

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

prune30.0ms

Pruning

1 alts after pruning (1 fresh and 0 done)

Merged error: 34.4b

localize42.0ms

Local error

Found 4 expressions with local error:

26.2b
(sqrt (+ (* re re) (* im im)))
1.5b
(+ (sqrt (+ (* re re) (* im im))) re)
0.0b
(+ (* re re) (* im im))
0.0b
(sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))

rewrite27.0ms

Algorithm
rewrite-expression-head
Rules
add-log-exp
add-sqr-sqrt
add-cube-cbrt
*-un-lft-identity
flip-+
sqrt-prod
add-exp-log
sqrt-div
add-cbrt-cube
flip3-+
pow1
associate-*r/
pow1/2
+-commutative
sum-log
rem-sqrt-square
Counts
4 → 46
Calls
4 calls:
Slowest
15.0ms
(+ (sqrt (+ (* re re) (* im im))) re)
5.0ms
(sqrt (+ (* re re) (* im im)))
4.0ms
(+ (* re re) (* im im))
3.0ms
(sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))

series206.0ms

Counts
4 → 12
Calls
4 calls:
Slowest
104.0ms
(sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))
45.0ms
(+ (sqrt (+ (* re re) (* im im))) re)
32.0ms
(sqrt (+ (* re re) (* im im)))
25.0ms
(+ (* re re) (* im im))

simplify679.0ms

Counts
22 → 58
Calls
22 calls:
Slowest
282.0ms
(sqrt (* 2.0 (- (* (sqrt (+ (* re re) (* im im))) (sqrt (+ (* re re) (* im im)))) (* re re))))
156.0ms
(sqrt (* 2.0 (+ (pow (sqrt (+ (* re re) (* im im))) 3) (pow re 3))))
138.0ms
(sqrt (- (* (* re re) (* re re)) (* (* im im) (* im im))))
63.0ms
(sqrt (+ (pow (* re re) 3) (pow (* im im) 3)))
7.0ms
(* (exp (* re re)) (exp (* im im)))

prune651.0ms

Pruning

6 alts after pruning (5 fresh and 1 done)

Merged error: 17.4b

localize20.0ms

Local error

Found 4 expressions with local error:

27.1b
(cbrt (+ (* re re) (* im im)))
27.1b
(cbrt (+ (* re re) (* im im)))
27.1b
(cbrt (+ (* re re) (* im im)))
26.2b
(sqrt (* (* (cbrt (+ (* re re) (* im im))) (cbrt (+ (* re re) (* im im)))) (cbrt (+ (* re re) (* im im)))))

rewrite18.0ms

Algorithm
rewrite-expression-head
Rules
60×cbrt-div
30×flip-+
30×flip3-+
28×frac-times
26×sqrt-div
14×associate-*l/
cbrt-prod
associate-*r/
add-cube-cbrt
*-un-lft-identity
add-sqr-sqrt
add-log-exp
add-exp-log
add-cbrt-cube
pow1
pow1/3
sqrt-prod
pow1/2
Counts
4 → 74
Calls
4 calls:
Slowest
12.0ms
(sqrt (* (* (cbrt (+ (* re re) (* im im))) (cbrt (+ (* re re) (* im im)))) (cbrt (+ (* re re) (* im im)))))
1.0ms
(cbrt (+ (* re re) (* im im)))
1.0ms
(cbrt (+ (* re re) (* im im)))
1.0ms
(cbrt (+ (* re re) (* im im)))

series377.0ms

Counts
4 → 12
Calls
4 calls:
Slowest
129.0ms
(cbrt (+ (* re re) (* im im)))
112.0ms
(cbrt (+ (* re re) (* im im)))
111.0ms
(cbrt (+ (* re re) (* im im)))
24.0ms
(sqrt (* (* (cbrt (+ (* re re) (* im im))) (cbrt (+ (* re re) (* im im)))) (cbrt (+ (* re re) (* im im)))))

simplify4.5s

Counts
54 → 86
Calls
54 calls:
Slowest
459.0ms
(sqrt (* (* (cbrt (- (* (* re re) (* re re)) (* (* im im) (* im im)))) (cbrt (+ (* re re) (* im im)))) (cbrt (- (* (* re re) (* re re)) (* (* im im) (* im im))))))
211.0ms
(sqrt (* (* (cbrt (+ (pow (* re re) 3) (pow (* im im) 3))) (cbrt (- (* (* re re) (* re re)) (* (* im im) (* im im))))) (cbrt (- (* (* re re) (* re re)) (* (* im im) (* im im))))))
202.0ms
(sqrt (* (* (cbrt (+ (pow (* re re) 3) (pow (* im im) 3))) (cbrt (- (* (* re re) (* re re)) (* (* im im) (* im im))))) (cbrt (+ (pow (* re re) 3) (pow (* im im) 3)))))
198.0ms
(sqrt (* (* (cbrt (- (* (* re re) (* re re)) (* (* im im) (* im im)))) (cbrt (+ (pow (* re re) 3) (pow (* im im) 3)))) (cbrt (- (* (* re re) (* re re)) (* (* im im) (* im im))))))
189.0ms
(sqrt (* (* (cbrt (+ (* re re) (* im im))) (cbrt (- (* (* re re) (* re re)) (* (* im im) (* im im))))) (cbrt (- (* (* re re) (* re re)) (* (* im im) (* im im))))))

prune1.3s

Pruning

7 alts after pruning (5 fresh and 2 done)

Merged error: 17.4b

localize32.0ms

Local error

Found 4 expressions with local error:

29.6b
(pow (+ (* re re) (* im im)) 1/3)
27.1b
(cbrt (+ (* re re) (* im im)))
27.1b
(cbrt (+ (* re re) (* im im)))
26.2b
(sqrt (* (* (cbrt (+ (* re re) (* im im))) (cbrt (+ (* re re) (* im im)))) (pow (+ (* re re) (* im im)) 1/3)))

rewrite16.0ms

Algorithm
rewrite-expression-head
Rules
16×cbrt-div
10×associate-*l/
flip-+
sqrt-div
flip3-+
add-cube-cbrt
*-un-lft-identity
add-sqr-sqrt
cbrt-prod
add-exp-log
pow1
add-log-exp
add-cbrt-cube
frac-times
unpow-prod-down
associate-*r/
pow1/3
sqrt-prod
pow-exp
unpow1/3
pow1/2
pow-pow
pow-to-exp
Counts
4 → 57
Calls
4 calls:
Slowest
6.0ms
(sqrt (* (* (cbrt (+ (* re re) (* im im))) (cbrt (+ (* re re) (* im im)))) (pow (+ (* re re) (* im im)) 1/3)))
6.0ms
(pow (+ (* re re) (* im im)) 1/3)
1.0ms
(cbrt (+ (* re re) (* im im)))
1.0ms
(cbrt (+ (* re re) (* im im)))

series358.0ms

Counts
4 → 12
Calls
4 calls:
Slowest
128.0ms
(cbrt (+ (* re re) (* im im)))
113.0ms
(cbrt (+ (* re re) (* im im)))
84.0ms
(pow (+ (* re re) (* im im)) 1/3)
34.0ms
(sqrt (* (* (cbrt (+ (* re re) (* im im))) (cbrt (+ (* re re) (* im im)))) (pow (+ (* re re) (* im im)) 1/3)))

simplify1.5s

Counts
36 → 69
Calls
36 calls:
Slowest
196.0ms
(sqrt (* (* (cbrt (+ (pow (* re re) 3) (pow (* im im) 3))) (cbrt (- (* (* re re) (* re re)) (* (* im im) (* im im))))) (pow (+ (* re re) (* im im)) 1/3)))
185.0ms
(sqrt (* (* (cbrt (+ (* re re) (* im im))) (cbrt (- (* (* re re) (* re re)) (* (* im im) (* im im))))) (pow (+ (* re re) (* im im)) 1/3)))
155.0ms
(sqrt (* (* (cbrt (- (* (* re re) (* re re)) (* (* im im) (* im im)))) (cbrt (- (* (* re re) (* re re)) (* (* im im) (* im im))))) (pow (+ (* re re) (* im im)) 1/3)))
146.0ms
(cbrt (- (* (* re re) (* re re)) (* (* im im) (* im im))))
131.0ms
(cbrt (- (* (* re re) (* re re)) (* (* im im) (* im im))))

prune1.0s

Pruning

7 alts after pruning (5 fresh and 2 done)

Merged error: 17.4b

localize53.0ms

Local error

Found 4 expressions with local error:

27.1b
(cbrt (+ (* re re) (* im im)))
27.1b
(cbrt (+ (* re re) (* im im)))
27.1b
(cbrt (+ (* re re) (* im im)))
27.1b
(cbrt (+ (* re re) (* im im)))

rewrite11.0ms

Algorithm
rewrite-expression-head
Rules
12×cbrt-prod
add-cube-cbrt
*-un-lft-identity
cbrt-div
add-sqr-sqrt
add-log-exp
flip-+
add-exp-log
pow1/3
add-cbrt-cube
flip3-+
pow1
Counts
4 → 52
Calls
4 calls:
Slowest
3.0ms
(cbrt (+ (* re re) (* im im)))
2.0ms
(cbrt (+ (* re re) (* im im)))
2.0ms
(cbrt (+ (* re re) (* im im)))
2.0ms
(cbrt (+ (* re re) (* im im)))

series477.0ms

Counts
4 → 12
Calls
4 calls:
Slowest
133.0ms
(cbrt (+ (* re re) (* im im)))
127.0ms
(cbrt (+ (* re re) (* im im)))
121.0ms
(cbrt (+ (* re re) (* im im)))
96.0ms
(cbrt (+ (* re re) (* im im)))

simplify933.0ms

Counts
32 → 64
Calls
32 calls:
Slowest
143.0ms
(cbrt (- (* (* re re) (* re re)) (* (* im im) (* im im))))
133.0ms
(cbrt (- (* (* re re) (* re re)) (* (* im im) (* im im))))
126.0ms
(cbrt (- (* (* re re) (* re re)) (* (* im im) (* im im))))
119.0ms
(cbrt (- (* (* re re) (* re re)) (* (* im im) (* im im))))
64.0ms
(cbrt (+ (pow (* re re) 3) (pow (* im im) 3)))

prune1.1s

Pruning

7 alts after pruning (5 fresh and 2 done)

Merged error: 17.4b

regimes659.0ms

Accuracy

56.3% (8.8b remaining)

Error of 25.9b against oracle of 17.1b and baseline of 37.2b

bsearch5.0ms

end0.0ms

sample2.5s

Algorithm
intervals
Results
1.2s6034×body80valid
527.0ms676×body2560valid
334.0ms301×body5120valid
240.0ms461×body1280valid
121.0ms292×body640valid
52.0ms166×body320valid
14.0ms70×body160valid