# Function to map vectorized PWL solution coefficients and impose
# continuity restrictions
#--------------------------------------------------------------------------

function  get_coefs_plc(theta_in::Array{Float64,1},params_in::Dict{String}{Float64})

ncoefs::Int64 = 7     ; # Number of coefficients per decision rule per regime (this is hard wired)

r = params_in["r"];
psi1 = params_in["psi1"];
psi2 = params_in["psi2"];
rho_r = params_in["rho_r"];
pi_ss = params_in["pi_ss"];

theta_nozlb = theta_in[1:(3*ncoefs)];
theta_zlb = theta_in[((3*ncoefs)+1):end];

theta_pi_1  = theta_nozlb[1:ncoefs];
theta_yy_1  = theta_nozlb[ncoefs+1:2*ncoefs];
theta_cc_1  = theta_nozlb[2*ncoefs+1:3*ncoefs];

theta_pi_2_temp = theta_zlb[1];
theta_yy_2_temp = theta_zlb[2];
theta_cc_2_temp = theta_zlb[3];

#Impose Coefficient Restrictions
a0 = theta_pi_1[1]; # const
a1 = theta_pi_1[2]; # eps_r
a2 = theta_pi_1[3]; # g-shock
a3 = theta_pi_1[4]; # z-shock
a4 = theta_pi_1[5]; # d-shock
a5 = theta_pi_1[6]; # yhat(-1)
a6 = theta_pi_1[7]; # Rhat(-1)

b0 = theta_yy_1[1]; # const
b1 = theta_yy_1[2]; # eps-r
b2 = theta_yy_1[3]; # g-shock
b3 = theta_yy_1[4]; # z-shock
b4 = theta_yy_1[5]; # d-shock
b5 = theta_yy_1[6]; # yhat(-1)
b6 = theta_yy_1[7]; # Rhat(-1)

c0 = theta_cc_1[1]; # const
c1 = theta_cc_1[2]; # eps-r
c2 = theta_cc_1[3]; # g-shock
c3 = theta_cc_1[4]; # z-shock
c4 = theta_cc_1[5]; # d-shock
c5 = theta_cc_1[6]; # yhat(-1)
c6 = theta_cc_1[7]; # Rhat(-1)

# ZLB regime - free coefficients
a13 = theta_pi_2_temp[1];
b13 = theta_yy_2_temp[1];
c13 = theta_cc_2_temp[1];

rhot = rho_r/(1-rho_r);

#---------------------------------------------------------
# Coefficient restrictions on inflation decision rule
#---------------------------------------------------------

# constant term
a7 = (log(r*pi_ss)*(a13-a6))/((1-rho_r)*(a6*psi1+rhot+b6*psi2))+a0*(a13*psi1+rhot+b6*psi2
    )/(a6*psi1+rhot+b6*psi2)+b0*psi2*(a13-a6)/(a6*psi1+rhot+b6*psi2);

# Coefficient on er-shock
a8 = (a13-a6)/((1-rho_r)*(a6*psi1+rhot+b6*psi2))+a1*(a13*psi1+rhot+b6*psi2
     )/(a6*psi1+rhot+b6*psi2)+b1*psi2*(a13-a6)/(a6*psi1+rhot+b6*psi2);

# Coefficient on g-shock
a9 = a2*(a13*psi1+rhot+b6*psi2)/(a6*psi1+rhot+b6*psi2)+b2*psi2*(a13-a6)/(a6*psi1+rhot+b6*psi2);

# Coefficient on z-shock
a10 = psi2*(a13-a6)/(a6*psi1+rhot+b6*psi2) + a3*(a13*psi1+rhot+b6*psi2)/(a6*psi1+rhot+b6*psi2
     )+b3*psi2*(a13-a6)/(a6*psi1+rhot+b6*psi2);

# Coefficient on d-shock
a11 = a4*(a13*psi1+rhot+b6*psi2)/(a6*psi1+rhot+b6*psi2)+b4*psi2*(a13-a6)/(a6*psi1+rhot+b6*psi2);

# Coefficient on yhat(-1)
a12 = -psi2*(a13-a6)/((a6*psi1+rhot+b6*psi2))+a5*(a13*psi1+rhot+b6*psi2)/(a6*psi1+rhot+b6*psi2
     )+b5*psi2*(a13-a6)/(a6*psi1+rhot+b6*psi2);


# Kink function
d0 = (a0 - a7)/(a13 - a6);
d1 = (a1 - a8)/(a13 - a6);
d2 = (a2 - a9)/(a13 - a6);
d3 = (a3 - a10)/(a13 - a6);
d4 = (a4 - a11)/(a13 - a6);
d5 = (a5 - a12)/(a13 - a6);

# Coefficient restrictions on output decision rule
b7  = b0 - (b13 - b6)*d0;
b8  = b1 - (b13 - b6)*d1;
b9  = b2 - (b13 - b6)*d2;
b10 = b3 - (b13 - b6)*d3;
b11 = b4 - (b13 - b6)*d4;
b12 = b5 - (b13 - b6)*d5;

# Coefficient restrictions on output decision rule
c7  = c0 - (c13 - c6)*d0;
c8  = c1 - (c13 - c6)*d1;
c9  = c2 - (c13 - c6)*d2;
c10 = c3 - (c13 - c6)*d3;
c11 = c4 - (c13 - c6)*d4;
c12 = c5 - (c13 - c6)*d5;

# Map coefficients for decision rule
theta_yy_2=zeros(7)
theta_pi_2=zeros(7)
theta_cc_2=zeros(7)

theta_yy_2[1] = b7;    # const
theta_yy_2[2] = b8;    # eps-r
theta_yy_2[3] = b9;    # g-shock
theta_yy_2[4] = b10;   # z-shock
theta_yy_2[5] = b11;   # d-shock
theta_yy_2[6] = b12;   # yhat(-1)
theta_yy_2[7] = b13;   # Rhat(-1)

theta_pi_2[1] = a7;    # const
theta_pi_2[2] = a8;    # eps-r
theta_pi_2[3] = a9;    # g-shock
theta_pi_2[4] = a10;   # g-shock
theta_pi_2[5] = a11;   # d-shock
theta_pi_2[6] = a12;   # yhat(-1)
theta_pi_2[7] = a13;   # Rhat(-1)

theta_cc_2[1] = c7;    # const
theta_cc_2[2] = c8;    # eps-r
theta_cc_2[3] = c9;    # g-shock
theta_cc_2[4] = c10;   # g-shock
theta_cc_2[5] = c11;   # d-shock
theta_cc_2[6] = c12;   # yhat(-1)
theta_cc_2[7] = c13;   # Rhat(-1)

return theta_pi_1, theta_yy_1, theta_cc_1, theta_pi_2, theta_yy_2,theta_cc_2,d0,d1,d2,d3,d4,d5
end
