(z, prec, rnd=round_fast)
return re, im
Complex square root (principal branch).

    We have sqrt(a+bi) = sqrt((r+a)/2) + b/sqrt(2*(r+a))*i where
    r = abs(a+bi), when a+bi is not a negative real number.
Complex square root (principal branch).
Complex square root ( principal branch ) .
def mpc_sqrt(z, prec, rnd=round_fast):
    """Complex square root (principal branch).

    We have sqrt(a+bi) = sqrt((r+a)/2) + b/sqrt(2*(r+a))*i where
    r = abs(a+bi), when a+bi is not a negative real number."""
    a, b = z
    if b == fzero:
        if a == fzero:
            return (a, b)
        # When a+bi is a negative real number, we get a real sqrt times i
        if a[0]:
            im = mpf_sqrt(mpf_neg(a), prec, rnd)
            return (fzero, im)
        else:
            re = mpf_sqrt(a, prec, rnd)
            return (re, fzero)
    wp = prec+20
    if not a[0]:                               # case a positive
        t  = mpf_add(mpc_abs((a, b), wp), a, wp)  # t = abs(a+bi) + a
        u = mpf_shift(t, -1)                      # u = t/2
        re = mpf_sqrt(u, prec, rnd)               # re = sqrt(u)
        v = mpf_shift(t, 1)                       # v = 2*t
        w  = mpf_sqrt(v, wp)                      # w = sqrt(v)
        im = mpf_div(b, w, prec, rnd)             # im = b / w
    else:                                      # case a negative
        t = mpf_sub(mpc_abs((a, b), wp), a, wp)   # t = abs(a+bi) - a
        u = mpf_shift(t, -1)                      # u = t/2
        im = mpf_sqrt(u, prec, rnd)               # im = sqrt(u)
        v = mpf_shift(t, 1)                       # v = 2*t
        w  = mpf_sqrt(v, wp)                      # w = sqrt(v)
        re = mpf_div(b, w, prec, rnd)             # re = b/w
        if b[0]:
            re = mpf_neg(re)
            im = mpf_neg(im)
    return re, im
def mpc_sqrt ( z , prec , rnd = round_fast ) : a , b = z if b == fzero : if a == fzero : return ( a , b ) # When a+bi is a negative real number, we get a real sqrt times i if a [ 0 ] : im = mpf_sqrt ( mpf_neg ( a ) , prec , rnd ) return ( fzero , im ) else : re = mpf_sqrt ( a , prec , rnd ) return ( re , fzero ) wp = prec + 20 if not a [ 0 ] : # case a positive t = mpf_add ( mpc_abs ( ( a , b ) , wp ) , a , wp ) # t = abs(a+bi) + a u = mpf_shift ( t , - 1 ) # u = t/2 re = mpf_sqrt ( u , prec , rnd ) # re = sqrt(u) v = mpf_shift ( t , 1 ) # v = 2*t w = mpf_sqrt ( v , wp ) # w = sqrt(v) im = mpf_div ( b , w , prec , rnd ) # im = b / w else : # case a negative t = mpf_sub ( mpc_abs ( ( a , b ) , wp ) , a , wp ) # t = abs(a+bi) - a u = mpf_shift ( t , - 1 ) # u = t/2 im = mpf_sqrt ( u , prec , rnd ) # im = sqrt(u) v = mpf_shift ( t , 1 ) # v = 2*t w = mpf_sqrt ( v , wp ) # w = sqrt(v) re = mpf_div ( b , w , prec , rnd ) # re = b/w if b [ 0 ] : re = mpf_neg ( re ) im = mpf_neg ( im ) return re , im