% function [dleft,dright] = unwrapd(dvec,blk)
%
%   Unwraps the D scaling matrices in DVEC (from an
%   upper bound MU calculation) into the block diagonal form.
%
%
%   See also: MU and UNWRAPP.

function [dl,dr] = unwrapd(rvd,blk)
 if nargin ~= 2
   disp(['usage: [dleft,dright] = unwrapd(dvec,blk)'])
   return
 end
 [nblk,dum] = size(blk);
 if dum ~= 2
   error('block data incorrect format')
   return
 end
 wd = 0;
 dimdr = 0;
 dimdl = 0;
 for i=1:nblk
   dimdr = dimdr + blk(i,1);
   if blk(i,2) == 0
     wd = wd + blk(i,1)*blk(i,1);
     dimdl = dimdl + blk(i,1);
   else
     dimdl = dimdl + blk(i,2);
     wd = wd + 1;
   end
 end
 [mtype,mrows,mcols,mnum] = minfo(rvd);
 if mtype == 'vary'
   if mrows == 1 & mcols == wd
     [vd,rp,indv] = vunpck(rvd);
     dl = zeros(dimdl*mnum+1,dimdl+1);
     dl(dimdl*mnum+1,dimdl+1) = inf;
     dl(dimdl*mnum+1,dimdl) = mnum;
     dl(1:mnum,dimdl+1) = indv;
     dr = zeros(dimdr*mnum+1,dimdr+1);
     dr(dimdr*mnum+1,dimdr+1) = inf;
     dr(dimdr*mnum+1,dimdr) = mnum;
     dr(1:mnum,dimdr+1) = indv;
     fl = (mnum+1)*dimdl;
     fr = (mnum+1)*dimdr;
     pl = 1:dimdl:fl;
     pr = 1:dimdr:fr;
     plm1 = pl(2:mnum+1) - 1;
     prm1 = pr(2:mnum+1) - 1;
     for i=1:mnum
       [tl,tr] = mkdfrv(rvd(i,1:mcols),blk,dimdl,dimdr);
       dl(pl(i):plm1(i),1:dimdl) = tl;
       dr(pr(i):prm1(i),1:dimdr) = tr;
     end
   else
     error('incompatible dimensions')
     return
   end
 elseif mtype == 'cons'
   if mrows == 1 & mcols == wd
     [dl,dr] = mkdfrv(rvd,blk,dimdl,dimdr);
   else
     error('incompatible dimensions')
     return
   end
 else
  error('row vector should not be a SYSTEM matrix')
  return
 end
%
% Copyright MUSYN INC 1991,  All Rights Reserved
