% Hua-sheng XIE, huashengxie@gmail.com, IFTS-ZJU, 2014-08-24 00:34
% Calculate J-pole coefficient bj and cj for Z_J(zeta)=sum(bj/(zeta-cj))
% Ref: Martin1980, Ronnmark1982 p11
% Solve the matrix for pj and qj, then using residue() to get bj and cj.
% This is easy, since we need just the numerical data for bj and cj. The
% analytical form of pj and pj would be more difficult to obtain, which we 
% do not care at present.
close all; clear; clc;
digits(40);
J=12; % set J-pole
I=16; % Default for Ronnmark is Z_{10,6}, i.e., I=10

anm=zeros(2*J+1,1); anp=0.*anm;
for n=0:J+1                        % Z(zeta) two-side
     % for zeta -> 0
    anp(2*n+1)=(-1)^n*1i*sqrt(pi)/gamma(n+1); % a_{2n}
    anp(2*n+2)=-(-1)^n*gamma(1/2)/gamma(n+3/2); % a_{2n+1}
    
     % for zeta -> infty
    anm(2*n+1)=0; % a_{-2n}
    anm(2*n+2)=-gamma(n+1/2)/gamma(1/2); % a_{-(2n+1)}
end

Mpq=zeros(2*J); B=zeros(2*J,1);
% I equations for zeta -> 0; K for zeta -> infty; I+K=2J
K=2*J-I;
for j=1:I % for zeta -> 0
    B(j)=anp(j); % ap_j*q_0
    if(j<=J) % p_j
        Mpq(j,j)=1;
    end
    for k=1:j-1 % ap_k*q_{j-k}
        if(j<=J+k)
            Mpq(j,J+j-k)=-anp(k);
        end
    end
end

for j=1:K % for zeta -> infty
    B(I+j)=0; % 0
    if(j<=J) % p_j
        Mpq(I+j,J-j+1)=1; % p_{J-j}
    end
    for k=1:j % am_k*q_{J+1-j}
        if(j<2*J+k)
            Mpq(I+j,2*J-j+k)=-anm(k+1);
        end
    end
    if(j>J)
        B(I+j)=-anm(2)*1; % from q(1)=1;
    end
end
pq=Mpq\B; % solve p_j (0, 1, ..., J-1) and q_j (1, 2, ..., J)

% obtain c_j and b_j
p=pq(1:J); q=pq(J+1:2*J);
p=flipud(p); q=flipud(q); q=[q;1];
[b, c, r] = residue(p,q)

% plt_ZJI_pole;
