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));
44matrixDim = nchoosek(M+R-1,M)*(M+1);
53 [A,B,DA]=genmatrix(L,nvec,Z,r);
57 b = B*h*nvec(r)/(sum(nvec)+M-1);
60 scale(nt)=abs(sum(sort(h)));
61 h = abs(h)/scale(nt); % rescale so that |h|=1
64% unscale and
return the log of the normalizing constant
65lG=log(h(end-(R-1))) + factln(sum(N)+M-1) - sum(factln(N)) + N*log(Lmax)
' + sum(log(scale));
67 function [A,B,DA]=genmatrix(L,N,Z,r)
69 A=zeros(nchoosek(M+R-1,M)*(M+1));
70 DA=zeros(nchoosek(M+R-1,M)*(M+1));
71 B=zeros(nchoosek(M+R-1,M)*(M+1));
75 hnnz=hashnnz(Dn(d,:),R);
81 if sum(Dn(d,(r):R-1))>0
82 % dummy rows for unused norm consts
85 col = hash(N,N-Dn(d,:),k+1);
87 if sum(Dn(d,(r+1):R-1))>0
88 col = hash(N,N-Dn(d,:),k+1);
91 er=zeros(1,R); er(r)=1;
92 col = hash(N,N-Dn(d,:)+er,k+1);
101 A(row,hash(N,N-Dn(d,:),k+1))=1;
102 A(row,hash(N,N-Dn(d,:),0+1))=-1;
104 A(row,hash(N,oner(N-Dn(d,:),s),k+1))=-L(k,s);
106 B(row,hash(N,N-Dn(d,:),k+1))=L(k,r);
112 A(row,hash(N,n,0+1))=n(s);
113 A(row,hash(N,oner(n,s),0+1))=-Z(s);
115 A(row,hash(N,oner(n,s),k+1))=-L(k,s);
124 if sum(Dn(d,(r):R-1))<=0
127 A(row,hash(N,n,0+1))=n(r);
128 DA(row,hash(N,n,0+1))=1;
129 B(row,hash(N,n,0+1))=Z(r);
131 B(row,hash(N,n,k+1))=L(k,r);
137 function val=hashnnz(dn,R)
146 function col=hash(N,n,i)
148 col=size(Dn,1)*M+matchrow(Dn,N-n);
150 col=(matchrow(Dn,N-n)-1)*M+i-1;
156 g=zeros(size(Dn,1)*(M+1),1);
163 function I=sortbynnzpos(I)
164 % sorts a set of combinations with repetition according to the number of
168 if nnzcmp(I(i,:),I(j,:))==1
177 function r=nnzcmp(i1,i2) % return 1 if i1<i2
181 r=1; % i2 has more zeros and is thus greater
183 r=0; % i1 has more zeros and is thus greater
186 if i1(j)==0 & i2(j)>0
187 r=1; % i2 has the left-most zero
189 elseif i1(j)>0 & i2(j)==0