4 % @brief CoMoM algorithm
for computing the normalizing constant.
8function lG=pfqn_comom(L,N,Z,atol)
11 % @brief CoMoM algorithm
for computing the normalizing constant.
12 % @fn pfqn_comom(L, N, Z, atol)
13 % @param L Service demand matrix.
14 % @param N Population vector.
15 % @param Z Think time vector.
16 % @param atol Tolerance.
17 % @
return lG Logarithm of the normalizing constant.
26Lmax(Lmax<atol)=Z(Lmax<atol); % unless zero
28L = L./repmat(Lmax,M,1);
29Z = Z./repmat(Lmax,M,1);
30% sort from smallest to largest
31%[~,rsort] = sort(Z,
'ascend');
34% prepare comom data structures
41lh=log(h) + factln(sum(nvec)+M-1) - sum(factln(nvec));
49 [A,B,DA]=genmatrix(L,nvec,Z,r);
53 b = B*h*nvec(r)/(sum(nvec)+M-1);
56 scale(nt)=abs(sum(sort(h)));
57 h = abs(h)/scale(nt); % rescale so that |h|=1
60% unscale and
return the log of the normalizing constant
61lG=log(h(end-(R-1))) + factln(sum(N)+M-1) - sum(factln(N)) + N*log(Lmax)
' + sum(log(scale));
63 function [A,B,DA,rr]=genmatrix(L,N,Z,r)
65 A=zeros(nchoosek(M+R-1,M)*(M+1));
66 DA=zeros(nchoosek(M+R-1,M)*(M+1));
67 B=zeros(nchoosek(M+R-1,M)*(M+1));
69 rr=[]; % artificial rows
72 hnnz=hashnnz(Dn(d,:),R);
78 if sum(Dn(d,(r):R-1))>0
79 % dummy rows for unused norm consts
82 col = hash(N,N-Dn(d,:),k+1);
84 if sum(Dn(d,(r+1):R-1))>0
85 col = hash(N,N-Dn(d,:),k+1);
88 er=zeros(1,R); er(r)=1;
89 col = hash(N,N-Dn(d,:)+er,k+1);
98 A(row,hash(N,N-Dn(d,:),k+1))=1;
99 A(row,hash(N,N-Dn(d,:),0+1))=-1;
101 A(row,hash(N,oner(N-Dn(d,:),s),k+1))=-L(k,s);
103 B(row,hash(N,N-Dn(d,:),k+1))=L(k,r);
109 A(row,hash(N,n,0+1))=n(s);
110 A(row,hash(N,oner(n,s),0+1))=-Z(s);
112 A(row,hash(N,oner(n,s),k+1))=-L(k,s);
121 if sum(Dn(d,(r):R-1))<=0
124 A(row,hash(N,n,0+1))=n(r);
125 DA(row,hash(N,n,0+1))=1;
126 B(row,hash(N,n,0+1))=Z(r);
128 B(row,hash(N,n,k+1))=L(k,r);
135 function val=hashnnz(dn,R)
144 function col=hash(N,n,i)
146 col=size(Dn,1)*M+matchrow(Dn,N-n);
148 col=(matchrow(Dn,N-n)-1)*M+i-1;
154 g=zeros(size(Dn,1)*(M+1),1);
161 function I=sortbynnzpos(I)
162 % sorts a set of combinations with repetition according to the number of
166 if nnzcmp(I(i,:),I(j,:))==1
175 function r=nnzcmp(i1,i2) % return 1 if i1<i2
179 r=1; % i2 has more zeros and is thus greater
181 r=0; % i1 has more zeros and is thus greater
184 if i1(j)==0 & i2(j)>0
185 r=1; % i2 has the left-most zero
187 elseif i1(j)>0 & i2(j)==0