(*******************************************************************
This file was generated automatically by the Mathematica front end.
It contains Initialization cells from a Notebook file, which
typically will have the same name as this file except ending in
".nb" instead of ".m".

This file is intended to be loaded into the Mathematica kernel using
the package loading commands Get or Needs.  Doing so is equivalent
to using the Evaluate Initialization Cells menu command in the front
end.

DO NOT EDIT THIS FILE.  This entire file is regenerated
automatically each time the parent Notebook file is saved in the
Mathematica front end.  Any changes you make to this file will be
overwritten.
***********************************************************************)

BeginPackage["ratexpmodtranssolver`",{"SingularValueDecompositionUnstandard`",
    "LinearAlgebra`MatrixManipulation`","NumericalMath`","ControlSystems`",
    "NumericalControlMethods`"}]



ratexpmodtranssolver::usage=
  "Calculates the coefficients for the simulation formula from F. Hespeler, On boundary conditions within the solution of macroeocnomic dynamic models with rational expectations, forthcoming in Computation Economics, p. 5,6 using the results from the generalized schur decomposition and the original system matrices for the case that a transversality condition is fulfilled by those coefficients.      Inputs: The matrices obtained from the system's matrix pencil \!\(\*
StyleBox[\"ss\",\nFontSlant->\"Italic\"]\)\!\(\*
StyleBox[\",\",\nFontSlant->\"Italic\"]\)\!\(\*
StyleBox[\" \",\nFontSlant->\"Italic\"]\)\!\(\*
StyleBox[\"tt\",\nFontSlant->\"Italic\"]\)\!\(\*
StyleBox[\",\",\nFontSlant->\"Italic\"]\)\!\(\*
StyleBox[\" \",\nFontSlant->\"Italic\"]\)\!\(\*
StyleBox[\"qq\",\nFontSlant->\"Italic\"]\)\!\(\*
StyleBox[\",\",\nFontSlant->\"Italic\"]\)\!\(\*
StyleBox[\" \",\nFontSlant->\"Italic\"]\)\!\(\*
StyleBox[\"zz\",\nFontSlant->\"Italic\"]\), the system's coefficient matrix for the expectational error \!\(\*
StyleBox[\"gga\",\nFontSlant->\"Italic\"]\), the system's coefficient matrix for the shock term \!\(\*
StyleBox[\"schockcoff\",\nFontSlant->\"Italic\"]\), the system's coefficient for the tranversality constraint \!\(\*
StyleBox[\"transvers\",\nFontSlant->\"Italic\"]\), the number of predetermined variables \!\(\*
StyleBox[\"predet\",\nFontSlant->\"Italic\"]\), the number of state and nonstate variables \!\(\*
StyleBox[\"n1\",\nFontSlant->\"Italic\"]\) and \!\(\*
StyleBox[\"n2\",\nFontSlant->\"Italic\"]\), the values for a switch form stable to unstable eigenvalues \!\(\*
StyleBox[\"cutoff\",\nFontSlant->\"Italic\"]\), the value for the rate of persistence in the shock \!\(\*
StyleBox[\"inert\",\nFontSlant->\"Italic\"]\)\!\(\*
StyleBox[\",\",\nFontSlant->\"Italic\"]\)\!\(\*
StyleBox[\" \",\nFontSlant->\"Italic\"]\)two optional arguments: the measure \!\(\*
StyleBox[\"precise\",\nFontSlant->\"Italic\"]\) for the internally minimally required precision and \!\(\*
StyleBox[\"tol\",\nFontSlant->\"Italic\"]\) for the tolerance level in the computation of the pseudoinverses and, finally,the maximal computation time allocated to the entire computation \!\(\*
StyleBox[\"time\",\nFontSlant->\"Italic\"]\).        Output: the coefficient linking the stable variables of t+1 to the stable variables in t \!\(\*
StyleBox[\"coffx1x1\",\nFontSlant->\"Italic\"]\)\!\(\*
StyleBox[\",\",\nFontSlant->\"Italic\"]\)\!\(\*
StyleBox[\" \",\nFontSlant->\"Italic\"]\)the coefficient linking the stable variables of t+1 to the unstable variables in t \!\(\*
StyleBox[\"coffx1x2\",\nFontSlant->\"Italic\"]\)\!\(\*
StyleBox[\",\",\nFontSlant->\"Italic\"]\)\!\(\*
StyleBox[\" \",\nFontSlant->\"Italic\"]\)the coefficient linking the unstable variables of t+1 to the stable variables in t \!\(\*
StyleBox[\"coffx2x1\",\nFontSlant->\"Italic\"]\)\!\(\*
StyleBox[\",\",\nFontSlant->\"Italic\"]\)\!\(\*
StyleBox[\" \",\nFontSlant->\"Italic\"]\)the coefficient linking the unstable variables of t+1 to the unstable variables in t \!\(\*
StyleBox[\"coffx2x2\",\nFontSlant->\"Italic\"]\)\!\(\*
StyleBox[\",\",\nFontSlant->\"Italic\"]\)\!\(\*
StyleBox[\" \",\nFontSlant->\"Italic\"]\)the coefficient linking the stable variables of t+1 to the system's shock in t \!\(\*
StyleBox[\"coffx1z\",\nFontSlant->\"Italic\"]\)\!\(\*
StyleBox[\",\",\nFontSlant->\"Italic\"]\)\!\(\*
StyleBox[\" \",\nFontSlant->\"Italic\"]\)the coefficient linking the unstable variables of t+1 to the system's shock in t \!\(\*
StyleBox[\"coffx2z\",\nFontSlant->\"Italic\"]\)\!\(\*
StyleBox[\",\",\nFontSlant->\"Italic\"]\)\!\(\*
StyleBox[\" \",\nFontSlant->\"Italic\"]\)the coefficient linking the stable variables of t+1 to the system's correction of expectations in t \!\(\*
StyleBox[\"coffx1zfut\",\nFontSlant->\"Italic\"]\)\!\(\*
StyleBox[\",\",\nFontSlant->\"Italic\"]\)\!\(\*
StyleBox[\" \",\nFontSlant->\"Italic\"]\)the coefficient linking the unstable variables of t+1 to the system's correction of expectations in t \!\(\*
StyleBox[\"coffx2zfut\",\nFontSlant->\"Italic\"]\)\!\(\*
StyleBox[\",\",\nFontSlant->\"Italic\"]\)\!\(\*
StyleBox[\" \",\nFontSlant->\"Italic\"]\)eight messages giving some information on the chosen type of the solution and on whether the transversality condition is successfully integrated or not, and the number of stable eigenvalues \!\(\*
StyleBox[\"stabcon\",\nFontSlant->\"Italic\"]\)"

Begin["`Private`"]

ratexpmodtranssolver[ss_,tt_,qq_,zz_,gga_,schockcoff_,transvers_,predet_,n1_,
    n2_,cutoff_,inert_,precise_:10^(-15),tol_:Automatic,time_]:=
  TimeConstrained[test=Table[tt[[i,i]]/ss[[i,i]],{i,1,Length[tt]}];
    test=Replace[test,ComplexInfinity\[Rule]10^10,5];
    stabcon=
      Count[Table[TrueQ[Abs[test[[i]]]\[LessEqual]cutoff],{i,1,Length[test]}],
        True];
    s11=SubMatrix[ss,{1,1},{stabcon,stabcon}];
    s12=SubMatrix[ss,{1,stabcon+1},{stabcon,Length[tt]-stabcon}];
          s21=SubMatrix[ss,{stabcon+1,1},{Length[tt]-stabcon,stabcon}];
    s22=SubMatrix[
        ss,{stabcon+1,stabcon+1},{Length[tt]-stabcon,Length[tt]-stabcon}];
    t11=SubMatrix[tt,{1,1},{stabcon,stabcon}];
    t12=SubMatrix[tt,{1,stabcon+1},{stabcon,Length[tt]-stabcon}];
    t21=SubMatrix[tt,{stabcon+1,1},{Length[tt]-stabcon,stabcon}];
    t22=SubMatrix[
        tt,{stabcon+1,stabcon+1},{Length[tt]-stabcon,Length[tt]-stabcon}];
    If[(MatrixRank[s11]\[NotEqual]stabcon||
          MatrixRank[t22]\[NotEqual]
            Length[ss]-stabcon),({{},{},{},{},{},{},{},{},
          ToString[
            "problem with invertibility of s11 and/or t22: no solution computable"]\
,Null,Null,Null,Null,Null,Null,Null,Null,Null,Null,Null,stabcon}),(s11in=
          Inverse[SubMatrix[ss,{1,1},{stabcon,stabcon}]];
        t22in=Inverse[
            SubMatrix[
              tt,{stabcon+1,stabcon+1},{Length[tt]-stabcon,
                Length[tt]-stabcon}]];
        z11=SubMatrix[zz,{1,1},{predet,stabcon}];
        z12=SubMatrix[zz,{1,stabcon+1},{predet,Length[tt]-stabcon}];
        z21=SubMatrix[zz,{predet+1,1},{Length[tt]-predet,stabcon}];
        z22=
          SubMatrix[
            zz,{predet+1,stabcon+1},{Length[tt]-predet,Length[tt]-stabcon}];
        q11con=SubMatrix[ConjugateTranspose[qq],{1,1},{stabcon,stabcon}];
        q12con=SubMatrix[
            ConjugateTranspose[qq],{1,stabcon+1},{stabcon,
              Length[tt]-stabcon}];
        q21con=SubMatrix[
            ConjugateTranspose[qq],{stabcon+1,1},{Length[tt]-stabcon,
              stabcon}];
        q22con=
          SubMatrix[
            ConjugateTranspose[qq],{stabcon+1,stabcon+1},{Length[tt]-stabcon,
              Length[tt]-stabcon}];
        z11con=SubMatrix[ConjugateTranspose[zz],{1,1},{stabcon,predet}];
        z12con=SubMatrix[
            ConjugateTranspose[zz],{1,predet+1},{stabcon,Length[tt]-predet}];
        z21con=SubMatrix[
            ConjugateTranspose[zz],{stabcon+1,1},{Length[tt]-stabcon,
              predet}];
        z22con=
          SubMatrix[
            ConjugateTranspose[zz],{stabcon+1,predet+1},{Length[tt]-stabcon,
              Length[tt]-predet}];
        z1=SubMatrix[zz,{1,1},{stabcon,Length[tt]}];
        q1con=SubMatrix[ConjugateTranspose[qq],{1,1},{stabcon,Length[tt]}];
        q2con=SubMatrix[
            ConjugateTranspose[qq],{stabcon+1,1},{Length[tt]-stabcon,
              Length[tt]}];
        result1=-s22.t22in.q2con.schockcoff;
        Do[result1=
            AppendRows[
              result1,-inert^(i-1)*
                t22.MatrixPower[t22in.s22,i].t22in.q2con.schockcoff],{i,2,
            Length[tt]-stabcon}];
        (*result1=-s22.Inverse[
                  IdentityMatrix[Length[t22in]]-
                    inert*t22in.s22].t22in.q2con.schockcoff*);
        {test1,test2,test3}=
          SingularValueDecompositionUnstandard[
            AppendRows[q2con.gga,result1]];
        {tes1,tes2,tes3}=SingularValueDecompositionUnstandard[q2con.gga];
        If[Length[Select[Tr[test2,List],(#\[GreaterEqual]precise)&]]==
            Length[Select[Tr[tes2,List],(#\[GreaterEqual]precise)&]],
          mess1=ToString["solution existent"];(*helpa1=
              LinearSolve[
                q2con.gga,(-s22.Inverse[
                        Chop[IdentityMatrix[Length[t22in]]-
                            inert*t22in.s22]].t22in.q2con.schockcoff)]*);  
          {{tratestb1b,tratestb2b,tratestb3b},{tratestd1a,tratestd2a,
                tratestd3a}}=
            If[MatrixRank[
                    TakeRows[
                      Transpose[NullSpace[transvers.zz]],-Length[t22]]]<
                  Dimensions[
                      TakeRows[
                        Transpose[
                          NullSpace[transvers.zz]],-Length[t22]]][[2]]&&
                MatrixRank[transvers.zz]<
                  Dimensions[
                      transvers.zz][[2]],{\
SingularValueDecompositionUnstandard[(IdentityMatrix[Length[q1con]]-
                        q1con.gga.(IdentityMatrix[
                                Length[PseudoInverse[q2con.gga]]]-
                              PseudoInverse[
                                  q2con.gga].q2con.gga).PseudoInverse[
                            q1con.gga.(IdentityMatrix[
                                    Length[PseudoInverse[q2con.gga]]]-
                                  PseudoInverse[
                                      q2con.gga].q2con.gga)]).TakeRows[
                      Transpose[NullSpace[transvers.zz]],
                      Length[s11]].Transpose[
                      NullSpace[
                        TakeRows[
                          Transpose[NullSpace[transvers.zz]],-Length[t22]]]]],
                SingularValueDecompositionUnstandard[
                  AppendRows[(IdentityMatrix[Length[q1con]]-
                          q1con.gga.(IdentityMatrix[
                                  Length[PseudoInverse[q2con.gga]]]-
                                PseudoInverse[
                                    q2con.gga].q2con.gga).PseudoInverse[
                              q1con.gga.(IdentityMatrix[
                                      Length[PseudoInverse[q2con.gga]]]-
                                    PseudoInverse[
                                        q2con.gga].q2con.gga)]).TakeRows[
                        Transpose[NullSpace[transvers.zz]],
                        Length[s11]].Transpose[
                        NullSpace[
                          TakeRows[
                            Transpose[
                              NullSpace[transvers.zz]],-Length[t22]]]],
                    q1con.(IdentityMatrix[Length[gga]]-
                          gga.PseudoInverse[q2con.gga].s22.t22in.q2con -
                          gga.(IdentityMatrix[
                                  Length[PseudoInverse[q2con.gga]]]-
                                PseudoInverse[
                                    q2con.gga].q2con.gga).PseudoInverse[(\
q1con.gga).(IdentityMatrix[Length[PseudoInverse[q2con.gga]]]-
                                    PseudoInverse[
                                        q2con.gga].q2con.gga)].(q1con-
                                q1con.gga.PseudoInverse[
                                    q2con.gga].s22.t22in.q2con)).schockcoff]]}\
,transinttestb=
                2;{{tratestb1b,tratestb2b,tratestb3b},{tratestd1a,tratestd2a,
                  tratestd3a}}];
          
          If[MatrixRank[
                  TakeRows[Transpose[NullSpace[transvers.zz]],-Length[t22]]]<
                Dimensions[
                    TakeRows[
                      Transpose[NullSpace[transvers.zz]],-Length[t22]]][[2]]&&
              MatrixRank[transvers.zz]<Dimensions[transvers.zz][[2]],
            If[Length[
                  Select[Tr[tratestd2a,
                      List],(#\[GreaterEqual]precise)&]]\[Equal]Length[
                  Select[Tr[tratestb2b,List],(#\[GreaterEqual]precise)&]],
              transinttestb=1;
              transmess=
                ToString["tranversality condition sucessfully integrated"],
              transinttestb=0;
              transmess=
                ToString[
                  "tranversality could not be integrated, solution does not hold"]\
],transmess=ToString[
                "tranversality could not be integrated, because null space it empty, solution given with transversality ignored"]\
];If[transinttestb\[Equal]1,
            freedom2=
              Table[Table[
                  1,{Dimensions[schockcoff][[2]]}],{Dimensions[
                      Transpose[
                        NullSpace[
                          TakeRows[
                            Transpose[
                              NullSpace[transvers.zz]],-Length[
                                t22]]]]][[2]]}],
            freedom2=
              ZeroMatrix[Dimensions[gga][[2]],Dimensions[schockcoff][[2]]]];
          
          Lambda=-PseudoInverse[q2con.gga,Tolerance\[Rule]tol].If[
                    inert\[NotEqual]0,(-inert*
                        s22.Inverse[
                            Chop[IdentityMatrix[Length[t22in]]-
                                inert*t22in.s22]].t22in.q2con.schockcoff),
                    s22.t22in.q2con.schockcoff]-(IdentityMatrix[
                      Dimensions[
                          PseudoInverse[q2con.gga,Tolerance\[Rule]tol]][[1]]]-
                    PseudoInverse[q2con.gga,
                        Tolerance\[Rule]tol].q2con.gga).If[
                  transinttestb\[Equal]1,
                  PseudoInverse[
                      q1con.gga.(IdentityMatrix[
                              Dimensions[PseudoInverse[q2con.gga]][[1]]]-
                            PseudoInverse[
                                q2con.gga].q2con.gga)].(q1con.schockcoff-
                        TakeRows[Transpose[NullSpace[transvers.zz]],
                            Length[s11]].Transpose[
                            NullSpace[
                              TakeRows[
                                Transpose[
                                  NullSpace[transvers.zz]],-Length[
                                    t22]]]].freedom2-
                        q1con.gga.PseudoInverse[
                            q2con.gga].s22.t22in.q2con.schockcoff),freedom2];
          If[transinttestb\[Equal]1,
            freedom1=
              ZeroMatrix[
                Dimensions[
                    Transpose[
                      NullSpace[
                        TakeRows[
                          Transpose[
                            NullSpace[transvers.zz]],-Length[t22]]]]][[2]],
                Length[t22in]];
            
            helpb=t12.t22in-
                s11.TakeRows[Transpose[NullSpace[transvers.zz]],
                    Length[s11]].Transpose[
                    NullSpace[
                      TakeRows[
                        Transpose[
                          NullSpace[transvers.zz]],-Length[
                            t22]]]].freedom1.t22in,
            helpb=ZeroMatrix[Length[s11],Length[s22]]];
          ifcon5=1,ifcon5=0;
          mess1={ToString[
                "no solution existent, absolue value of biggest deviation:"],
              Max[Abs[Chop[(IdentityMatrix[Length[test1]]-
                          test1.ConjugateTranspose[test1]).tes1,precise]]]}];
        If[ifcon5\[NotEqual]1,
          Clear[transmess];{coffx1x1,coffx1x2,coffx2x1,coffx2x2,coffx1z,
              coffx1zfut,coffx2z,coffx2zfut,mess1,mess2,mess3,mess4,mess5,
              mess6,mess7,mess8,mess9,mess10,mess11,transmess,
              stabcon}={{},{},{},{},{},{},{},{},mess1,mess2,mess3,mess4,mess5,
              mess6,mess7,mess8,mess9,mess10,mess11,transmess,stabcon},
          {test11b,test22b,test33b}=
            SingularValueDecompositionUnstandard[
              ConjugateTranspose[q2con.gga]];
          {test11,test22,test33}=
            SingularValueDecompositionUnstandard[
              AppendRows[ConjugateTranspose[q2con.gga],
                ConjugateTranspose[q1con.gga]]];{tes11a,tes22a,tes33a}=
            SingularValueDecompositionUnstandard[
              ConjugateTranspose[q1con.gga]];
          
          If[Length[Select[Tr[test22,List],(#\[GreaterEqual]precise)&]]==
              Length[Select[Tr[test22b,List],(#\[GreaterEqual]precise)&]],
            If[Norm[(IdentityMatrix[Length[test11b]]-
                        test11b.ConjugateTranspose[test11b]).tes11a]<=
                precise*Length[gg],
              help=q1con.gga.ConjugateTranspose[
                    ConjugateTranspose[test33b].Inverse[
                        test22b].ConjugateTranspose[test11b]];
              mess2=ToString["unique solution"];ifcon1=1,
              mess2=ToString[
                  "no unique solution, but solution to sims procedure existent"]\
;
              
              If[MatrixRank[
                      TakeRows[
                        Transpose[NullSpace[transvers.zz]],-Length[t22]]]<
                    Dimensions[
                        TakeRows[
                          Transpose[
                            NullSpace[transvers.zz]],-Length[t22]]][[2]]&&
                  MatrixRank[transvers.zz]<Dimensions[transvers.zz][[2]],
                nullspacefunc1=
                  TakeRows[Transpose[NullSpace[transvers.zz]],
                      Length[s11]].Transpose[
                      NullSpace[
                        TakeRows[
                          Transpose[NullSpace[transvers.zz]],-Length[t22]]]];
                
                nullspacefunc=
                  BlockMatrix[{{nullspacefunc1,-nullspacefunc1}}];{tratesta1a,
                    tratesta2a,tratesta3a}=
                  SingularValueDecompositionUnstandard[
                    AppendRows[nullspacefunc,
                      s11in.(q1con-t12.t22in.q2con).schockcoff]];{tratesta1b,
                    tratesta2b,tratesta3b}=
                  SingularValueDecompositionUnstandard[nullspacefunc];
                (*{trateste1a,trateste2a,trateste3a}=
                    SingularValueDecompositionUnstandard[
                      ConjugateTranspose[t22in.q2con.schockcoff]];{trateste1b,
                      trateste2b,trateste3b}=
                    SingularValueDecompositionUnstandard[
                      ConjugateTranspose[
                        TakeRows[
                          PseudoInverse[
                              nullspacefunc].(s11in.(q1con-
                                    t12.t22in.q2con)).schockcoff,-Length[
                              nullspacefunc1]]]];
                  {tratestf1a,tratestf2a,tratestf3a}=
                    SingularValueDecompositionUnstandard[
                      ConjugateTranspose[
                        IdentityMatrix[Length[q2con.gga]]-
                          q2con.gga.PseudoInverse[q2con.gga]]];{tratestf1b,
                      tratestf2b,tratestf3b}=
                    SingularValueDecompositionUnstandard[ConjugateTranspose[
                        (-s11.nullspacefunc1.TakeRows[
                                  PseudoInverse[
                                      nullspacefunc].(s11in.(q1con-
                                        t12.t22in.q2con)),-Length[
                                      nullspacefunc1]].PseudoInverse[
                                  t22in.q2con.schockcoff]+t12.t22in+
                            q1con.gga.PseudoInverse[q2con.gga])]];*)
                  nullspacetest=1,nullspacetest=0];
              
              If[nullspacetest\[NotEqual]0&&
                  Length[Select[
                        Tr[tratesta2a,List],(#\[GreaterEqual]precise)&]]==
                    Length[Select[
                        Tr[tratesta2b,List],(#\[GreaterEqual]precise)&]](*&&
                      Norm[(IdentityMatrix[Length[trateste1a]]-
                                trateste1a.ConjugateTranspose[
                                    trateste1a]).trateste1b]\[LessEqual]
                        precise*Length[trateste1b]&&
                      Norm[(IdentityMatrix[Length[tratestf1a]]-
                                tratestf1a.ConjugateTranspose[
                                    tratestf1a]).tratestf1b]\[LessEqual]
                        precise*Length[tratestf1b],False]*),transinttesta=1;
                transmess=
                  ToString["tranversality condition sucessfully integrated"],
                transinttesta=0;
                If[nullspacetest\[NotEqual]0,
                  transmess=
                    ToString[
                      "tranversality could not be integrated, solution does not hold"]\
,transmess=ToString[
                      "tranversality could not be integrated, because nullspace empty, solution does not hold for included transversality condition"]\
]];ifcon1=1;help=(q1con.gga).PseudoInverse[q2con.gga]+
                  If[transinttesta\[Equal]1,
                    freedom3=
                      ZeroMatrix[Dimensions[nullspacefunc][[2]],
                        Dimensions[schockcoff][[2]]];
                    freedom4=
                      ZeroMatrix[Dimensions[nullspacefunc1][[2]],
                        Length[q2con]];
                    (-s11.nullspacefunc1.(TakeRows[(PseudoInverse[
                                        nullspacefunc].s11in.(q1con-
                                        t12.t22in.q2con).schockcoff+(\
IdentityMatrix[Dimensions[nullspacefunc][[2]]]-
                                        PseudoInverse[
                                        nullspacefunc].nullspacefunc).\
freedom3),-Dimensions[nullspacefunc1][[2]]].PseudoInverse[
                                      t22in.q2con.schockcoff]+
                                  freedom4.(IdentityMatrix[Length[q2con]]-
                                        q2con.schockcoff.PseudoInverse[
                                        q2con.schockcoff]))+t12.t22in+
                          q1con.gga.PseudoInverse[q2con.gga]).PseudoInverse[
                        IdentityMatrix[Length[q2con.gga]]-
                          q2con.gga.PseudoInverse[
                              q2con.gga]].PseudoInverse[(IdentityMatrix[
                              Length[q2con.gga]]-
                            q2con.gga.PseudoInverse[
                                q2con.gga])].(IdentityMatrix[
                            Length[q2con.gga]]-
                          q2con.gga.PseudoInverse[q2con.gga]),
                    ZeroMatrix[Dimensions[q1con.gga][[1]],
                      Dimensions[q2con.gga][[1]]]],
              ZeroMatrix[Dimensions[(q1con.gga)][[1]]]],
            mess2=ToString[
                "no unique solution, and no solution using sims procedure"];
            ifcon1=0];
          
          If[(q1con.gga).PseudoInverse[q2con.gga]\[Equal]
              ZeroMatrix[Dimensions[q1con][[1]],Dimensions[gga][[2]]],
            mess3=ToString["Cf.mess 2"] ,
            mess3=ToString[
                "no influence of expectation error on stable part"]];
          
          If[MatrixRank[z11]\[Equal]Length[z11],
            mess4=ToString[
                "x2 can be expressed as linear function of x1, Cf. Paul Klein"]\
,mess4=ToString[
                "x2 can not be expressed as linear function of x1, Cf. Paul Klein"]\
];
          
          If[q11con.SubMatrix[gga,{1,1},{stabcon,predet}]==
              q12con.SubMatrix[
                  gga,{stabcon+1,1},{Length[tt]-stabcon,predet}],
            
            mess5=ToString[{"influence of endogenous expectations error on stable part is zero",\
(stabcon-ifcon3)*(Length[tt]-stabcon) ,"degrees of freedom"}],
            mess5=ToString[
                "influence of endogenous expectations error on stable var. does not cancel out"]\
];
          
          mess6=ToString[{"Matrix rank q2con times gga:" ,
                MatrixRank[q2con.gga,Tolerance\[Rule]tol]}];
          
          mess7=ToString[{"matrix rank q1con times gga:", 
                MatrixRank[q1con.gga,Tolerance\[Rule]tol]}];
          If[ifcon1==1,coffx1x1=z11.s11in.(t11.z11con+(t12-help.t22).z21con);
            coffx1x2=z11.s11in.(t11.z12con+(t12-help.t22).z22con);
            coffx2x1=z21.s11in.(t11.z11con+(t12-help.t22).z21con);
            coffx2x2=z21.s11in.(t11.z12con+(t12-help.t22).z22con);
            coffx1z=z11.s11in.(q1con-help.q2con).schockcoff;
            coffx2z=z21.s11in.(q1con-help.q2con).schockcoff;
            
            coffx1zfut={(z11.s11in.(s12-help.s22)-z12),t22in.s22,
                t22in.q2con.schockcoff};
            
            coffx2zfut={(z21.s11in.(s12-help.s22)-z22),t22in.s22,
                t22in.q2con.schockcoff},Null];
          
          If[ifcon5\[Equal]1&&ifcon1\[Equal]0,
            coffx1x1=z11.s11in.(t11.z11con+(t12-helpb.t22).z21con);
            coffx1x2=z11.s11in.(t11.z12con+(t12-helpb.t22).z22con);
            coffx2x1=z21.s11in.(t11.z11con+(t12-helpb.t22).z21con);
            coffx2x2=z21.s11in.(t11.z12con+(t12-helpb.t22).z22con);
            coffx1z=z11.q1con.schockcoff+z11.q1con.gga.Lambda;
            coffx2z=z21.q1con.schockcoff+z21.q1con.gga.Lambda;
            
            coffx1zfut={(z11.s11in.(s12-helpb.s22)-z12),t22in.s22,
                t22in.q2con.schockcoff};
            
            coffx2zfut={(z21.s11in.(s12-helpb.s22)-z22),t22in.s22,
                t22in.q2con.schockcoff};
            mess1=ToString["solution existent: general solution"],Null]];
        {coffx1x1,coffx1x2,coffx2x1,coffx2x2,coffx1z,coffx1zfut,coffx2z,
          coffx2zfut,mess1,mess2,mess3,mess4,mess5,mess6,mess7,mess8,mess9,
          mess10,mess11,transmess,stabcon})],
    time,{{},{},{},{},{},{},{},{},
      ToString["prespecified time limit exceeded: computation stopped"],mess2,
      mess3,mess4,mess5,mess6,mess7,transmess,stabcon}]

ClearAll["ratexpmodtranssolver`*`*"]
End[]
EndPackage[]