function [A_mx,B_mx,C_mx,D_mx,E_mx,Delta_mx,S_mx1,S_mx2,state_pos,A_LR]=cond_mom_dyn4_STSTGAP_sub1(dr_,M_,hor,BN)

npred=M_.nspred;
nstatic=M_.nstatic;
lgy_=M_.endo_names;
A_mx_0 = [zeros(size(lgy_,1),nstatic),dr_.ghx,zeros(size(lgy_,1),size(lgy_,1)-npred-nstatic)];
state_pos=nstatic+1:nstatic+size(dr_.ghx,2);

%% B-N long term expectation (first order)
[Usvd,Ssvd,Vsvd]=svd(A_mx_0);
egvaD=diag(Ssvd);
% egvaD(abs(egvaD)>1)=1;
NAx=Usvd*diag(egvaD)*Vsvd';
err=max(max(abs(A_mx_0-NAx)));
if err>1e-7;
    BN=0;
    warning('BN has been set to zero, svd was wrong');
end
long_run=1e9;
A_LR=[];
if BN
    A_LR=A_mx_0^(long_run);
A_mx=A_mx_0-A_LR;
else
    A_mx=A_mx_0;
end
B_mx = dr_.ghu;
C_mx=0.5*dr_.ghxx;
D_mx=0.5*dr_.ghuu;
E_mx=dr_.ghxu;
Delta_mx=0.5*dr_.ghs2;

% y=A*y(-1)+B*u+C*kron(y(-1),y(-1))+D*kron(u,u)+E*kron(y(-1),u)+Delta_mx;
%%%%
% reordering
% rows of the matrices are re-ordered see oo_.dr
%%%%%%%%%%%%
% State transition matrices
S_mx1=dr_.ghx(nstatic+1:nstatic+size(dr_.ghx,2),:);
if BN
S_mx1=S_mx1-S_mx1^(long_run);
end
S_mx2=dr_.ghu(nstatic+1:nstatic+size(dr_.ghx,2),:);