예제 #1
0
def PadeTest(om, Gm, x, gamma, Norder):
    from scipy import poly1d

    zn = om[:Norder] * 1j
    gn = Gm[:Norder]
    an = me.padecof(gn, zn)

    print 'zn=', zn
    print 'an=', an

    Aq_0 = poly1d([0])
    Aq_1 = poly1d([an[0]])
    Bq_0 = poly1d([1.])
    Bq_1 = poly1d([1.])
    for i in range(Norder - 1):
        Aq_2 = Aq_1 + poly1d([1, -zn[i]]) * an[i + 1] * Aq_0
        Aq_0 = Aq_1
        Aq_1 = Aq_2
        Bq_2 = Bq_1 + poly1d([1, -zn[i]]) * an[i + 1] * Bq_0
        Bq_0 = Bq_1
        Bq_1 = Bq_2

    poles = sorted(roots(Bq_2), key=real)
    ezeros = sorted(roots(Aq_2), key=real)
    Bqp = poly1d(poles, r=True)
    Cbq = Bq_2(0.0) / Bqp(0.0)
    print 'ratio=', Cbq

    wgh = []
    print 'poles='
    for i in range(len(poles)):
        Rq = poly1d(poles[:i] + poles[i + 1:], r=True)
        wi = Aq_2(poles[i]) / (Cbq * Rq(poles[i]))
        wgh.append(wi)
        if (poles[i].imag < 0): used = 'Yes'
        else: used = 'No'
        print "%2d %12.4g %12.4g   %12.4g %12.4g  used=%s" % (
            i + 1, poles[i].real, poles[i].imag, wi.real, wi.imag, used)
    wgh = array(wgh)
    #print 'zeros='
    #for i in range(len(ezeros)):
    #    print i+1, ezeros[i]
    #print 'weights=', wgh

    yt = zeros(len(om), dtype=complex)
    for i in range(len(poles)):
        if (poles[i].imag < 0):
            yt += real(wgh[i]) / (om * 1j - poles[i])

    normy = sum(real(yt))
    normg = sum(real(Gm))
    if normy > 1e-6:
        print 'Warning: norm mismatch: ratio=', normg / normy
        print 'Renormalizing'
        wgh *= (normg / normy)
    else:
        print 'Warning: Not enough poles. Bailing out'

    yt = zeros(len(om), dtype=complex)
    for i in range(len(poles)):
        if (poles[i].imag < 0):
            yt += real(wgh[i]) / (om * 1j - poles[i])
    yr = zeros(len(x), dtype=complex)
    for i in range(len(poles)):
        if (poles[i].imag < 0):
            yr += real(wgh[i]) / (x - poles[i] + gamma * 1j)

    savetxt('pade.corrected', vstack((x, real(yr), imag(yr))).transpose())
    G0 = Aq_2(zn) / Bq_2(zn)
    print 'G0=', G0
    plot(om, real(Gm), 'o')
    plot(om, real(yt), ':')
    show()
    plot(x, imag(yr))
    #xlim([0,2.4])
    #ylim([0,0.6])
    show()

    Gx = array([me.padeg(w + gamma * 1j, zn, an) for w in x])
    return Gx
예제 #2
0
def Pade(om, Gm, x, gamma, Norder):
    zn = om[:Norder] * 1j
    gn = Gm[:Norder]
    Pt = me.padecof(gn, zn)
    Gx = array([me.padeg(w + gamma * 1j, zn, Pt) for w in x])
    return Gx