function res=dynareAMACheck
global M_ oo_ it_
    oo_.exo_simul = ones(M_.maximum_lead+M_.maximum_lag+1,1)*oo_.exo_steady_state';
it_ = M_.maximum_lag + 1 ;
try
  tempex = oo_.exo_simul;
  klen = M_.maximum_endo_lag + M_.maximum_endo_lead + 1;
  z = repmat(oo_.steady_state,1,klen);
iyv = M_.lead_lag_incidence';
iyv = iyv(:);
iyr0 = find(iyv) ;
  z = z(iyr0) ;
    [junk,jacobia_] = feval([M_.fname '_dynamic'],z,tempex,M_.params,it_);
catch
error('dynareAMACheck: error accessing dynare global info about model')
end
try
hMat=createHMat(M_.lead_lag_incidence,jacobia_);
catch
error('dynareAMACheck: createHMat for SPAmalg failed');
end
try
[bb,rts,ia,nexact,nnumeric,lgroots,aimcode] =...
SPAmalg(hMat,size(hMat,1),M_.maximum_lag,M_.maximum_lead,1e-8,1.0+1e-8);
if(aimcode==1)
disp('unique convergent solution')
rts
res=1;
else
disp('no unique convergent solution');
SPAmerr(aimcode);
rts
res=0;
end
catch
error('dynareAMACheck: call to SPAmalg failed')
end
% Copyright (C) 2001 Michel Juillard
%





function hmat=createHMat(incMat,jacob)
[nldlgp1,neq]=size(incMat);
hmat=sparse(neq,nldlgp1*neq);
toCols=[];
for ii=1:nldlgp1
for jj=1:neq
if(incMat(ii,jj)>0)
toCols=[toCols (ii-1)*neq+jj];
end
end
end
for ii=1:neq
hmat(ii,toCols)=jacob(ii,1:max(max(incMat)));
end
