% computeHi constructs the matrix
%
% Hi = [T_(epsilonhat(1))^(epsilonhat(i))(N_1) ...
%       T_(epsilonhat(i-1))^(epsilonhat(i))(N_(i-1))]
%
% for the cspsolve routine. The individual component matrices
% are created via calls to the helper function computeTdk.

function Hi = computeHi(i,n,p,q,X,rho,epsilonh)

k = epsilonh(i);
Hi = zeros((k+1)*n,(k+1)*p-q);

lastcolX = 0;
lastcolHi = 0;
for l=1:i-1
  d = epsilonh(l);
  Nl = X(1:(d+1)*n,lastcolX+1:lastcolX+rho(l));
  Hi(:,lastcolHi+1:lastcolHi+(k-d+1)*rho(l)) = computeTdk(d,k,Nl);
  lastcolX = lastcolX+rho(l);
  lastcolHi = lastcolHi+(k-d+1)*rho(l);
end
