Interpreter.cc 103 KB
Newer Older
sebastien's avatar
sebastien committed
1
/*
sebastien's avatar
trunk:    
sebastien committed
2
 * Copyright (C) 2007-2009 Dynare Team
sebastien's avatar
sebastien committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 *
 * This file is part of Dynare.
 *
 * Dynare is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Dynare is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
 */
19
20
21
22
23
24
25

#ifdef _MSC_VER
/* In order to get M_PI, M_SQRT2 with Microsoft Visual C++, the following
   must be defined before including <cmath> */
# define _USE_MATH_DEFINES
#endif

26
#include <cstring>
27
#include <sstream>
sebastien's avatar
sebastien committed
28
#include "Interpreter.hh"
29
30
#define BIG 1.0e+8;
#define SMALL 1.0e-5;
31
//#define DEBUG
ferhat's avatar
ferhat committed
32

33
34
35
36
Interpreter::~Interpreter()
{
}

37
38
Interpreter::Interpreter(double *params_arg, double *y_arg, double *ya_arg, double *x_arg, double *steady_y_arg, double *steady_x_arg,
                         double *direction_arg, int y_size_arg,
ferhat's avatar
ferhat committed
39
40
                         int nb_row_x_arg, int nb_row_xd_arg, int periods_arg, int y_kmin_arg, int y_kmax_arg,
                         int maxit_arg_, double solve_tolf_arg, int size_of_direction_arg, double slowc_arg, int y_decal_arg, double markowitz_c_arg,
41
                         string &filename_arg, int minimal_solving_periods_arg, int stack_solve_algo_arg, int solve_algo_arg)
ferhat's avatar
ferhat committed
42
{
sebastien's avatar
sebastien committed
43
44
45
46
  params = params_arg;
  y = y_arg;
  ya = ya_arg;
  x = x_arg;
47
48
  steady_y = steady_y_arg;
  steady_x = steady_x_arg;
sebastien's avatar
sebastien committed
49
50
51
52
53
54
55
56
57
58
59
  direction = direction_arg;
  y_size = y_size_arg;
  nb_row_x = nb_row_x_arg;
  nb_row_xd = nb_row_xd_arg;
  periods = periods_arg;
  y_kmax = y_kmax_arg;
  y_kmin = y_kmin_arg;
  maxit_ = maxit_arg_;
  solve_tolf = solve_tolf_arg;
  size_of_direction = size_of_direction_arg;
  slowc = slowc_arg;
ferhat's avatar
ferhat committed
60
  slowc_save = slowc;
sebastien's avatar
sebastien committed
61
62
63
64
  y_decal = y_decal_arg;
  markowitz_c = markowitz_c_arg;
  filename = filename_arg;
  T = NULL;
65
  error_not_printed = true;
66
  minimal_solving_periods = minimal_solving_periods_arg;
67
68
  stack_solve_algo = stack_solve_algo_arg;
  solve_algo = solve_algo_arg;
ferhat's avatar
ferhat committed
69
70
}

71
72
73
74
75
76
77
78
79
string
Interpreter::remove_white(string str)
{
  string temp;
  for (unsigned int i = 0; i < str.length(); i++)
    if (str[i] != ' ')
      temp += str[i];
  return(temp);
}
80

81
string
82
Interpreter::add_underscore_to_fpe(string str)
83
{
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
  string temp;
  int pos1 = -1, pos2 = -1;
  string tmp_n(str.length(), ' ');
  for (unsigned int i = 0; i < str.length(); i++)
    {
      if (str[i] != '$' && str[i] != '')
        temp += str[i];
      else
        {
          if (str[i] == '$')
            pos1 = temp.length();
          else
            pos2 = temp.length();
          if (pos1>=0 && pos2 >=0)
            {
              tmp_n.erase(pos1, pos2-pos1+1);
              tmp_n.insert(pos1, pos2-pos1, '~');
              pos1 = pos2 = -1;
            }
        }
    }
  return(temp + "\n " + tmp_n );
}


string
110
Interpreter::get_variable(SymbolType variable_type, unsigned int variable_num)
111
112
{
  ostringstream res;
113
#ifndef DEBUG_EX
114
  mxArray *M_;
115
116
  char * P;
  unsigned int R, C;
117
118
119
120
121
122
123
  M_ = mexGetVariable("global", "M_");
  switch(variable_type)
    {
    case eEndogenous:
      C = mxGetN(mxGetFieldByNumber(M_, 0, mxGetFieldNumber(M_, "endo_names")));
      R = mxGetM(mxGetFieldByNumber(M_, 0, mxGetFieldNumber(M_, "endo_names")));
      P = (char*) mxGetPr(mxGetFieldByNumber(M_, 0, mxGetFieldNumber(M_, "endo_names")));
124
      if (variable_num < R)
125
        for (unsigned int i = 0; i < C; i++)
126
127
128
          res << P[2*(variable_num+i*R)];
      else
        mexPrintf("=> Unknown endogenous variable n %d",EQN_dvar1);
129
130
131
132
133
134
      break;
    case eExogenous:
    case eExogenousDet:
      C = mxGetN(mxGetFieldByNumber(M_, 0, mxGetFieldNumber(M_, "exo_names")));
      R = mxGetM(mxGetFieldByNumber(M_, 0, mxGetFieldNumber(M_, "exo_names")));
      P = (char*) mxGetPr(mxGetFieldByNumber(M_, 0, mxGetFieldNumber(M_, "exo_names")));
135
      if (variable_num < R)
136
        for (unsigned int i = 0; i < C; i++)
137
138
139
          res << P[2*(variable_num+i*R)];
      else
        mexPrintf("=> Unknown exogenous variable n %d",EQN_dvar1);
140
141
142
143
144
      break;
    case eParameter:
      C = mxGetN(mxGetFieldByNumber(M_, 0, mxGetFieldNumber(M_, "param_names")));
      R = mxGetM(mxGetFieldByNumber(M_, 0, mxGetFieldNumber(M_, "param_names")));
      P = (char*) mxGetPr(mxGetFieldByNumber(M_, 0, mxGetFieldNumber(M_, "param_names")));
145
      if (variable_num < R)
146
        for (unsigned int i = 0; i < C; i++)
147
148
149
          res << P[2*(variable_num+i*R)];
      else
        mexPrintf("=> Unknown parameter n %d",EQN_dvar1);
150
151
152
153
154
155
156
157
158
159
160
161
162
163
      break;
    default:
      break;
    }
#endif
  return(remove_white(res.str()));
}


string
Interpreter::error_location(bool evaluate)
{
  string tmp;
  stringstream Error_loc("");
164
165
166
  switch(EQN_type)
    {
      case TemporaryTerm:
167
        if (EQN_block_number > 1)
168
169
170
171
172
          Error_loc << "temporary term " << EQN_equation+1 << " in block " << EQN_block+1;
        else
          Error_loc << "temporary term " << EQN_equation+1;
        break;
      case ModelEquation:
173
        if (EQN_block_number > 1)
174
175
176
177
178
          Error_loc << "equation " << EQN_equation+1 << " in block " << EQN_block+1;
        else
          Error_loc << "equation " << EQN_equation+1;
        break;
      case FirstEndoDerivative:
179
180
        if (EQN_block_number > 1)
          Error_loc << "first order derivative of equation " << EQN_equation+1 << " in block " << EQN_block+1 << " with respect to endogenous variable ";
181
        else
182
          Error_loc << "first order derivative of equation " << EQN_equation+1 << " with respect to endogenous variable " << get_variable(eEndogenous, EQN_dvar1);
183
184
        break;
      default:
185
        return("???");
186
187
        break;
    }
188
  Error_loc << "\n " << add_underscore_to_fpe(print_expression(it_code_expr, evaluate));
189
190
191
  return(Error_loc.str());
}

ferhat's avatar
ferhat committed
192
double
193
Interpreter::pow1(double a, double b, bool evaluate)
ferhat's avatar
ferhat committed
194
{
sebastien's avatar
sebastien committed
195
  double r = pow_(a, b);
196
  if (isnan(r))
ferhat's avatar
ferhat committed
197
    {
198
      if (error_not_printed)
sebastien's avatar
sebastien committed
199
        {
200
          mexPrintf("--------------------------------------\nError: X^a with X=%5.15f\n in %s \n--------------------------------------\n", a,error_location(evaluate).c_str());
sebastien's avatar
sebastien committed
201
202
203
204
          error_not_printed = false;
          r = 0.0000000000000000000000001;
        }
      res1 = NAN;
205
      return r;
ferhat's avatar
ferhat committed
206
    }
207
  return r;
ferhat's avatar
ferhat committed
208
209
}

210
double
211
Interpreter::log1(double a, bool evaluate)
212
{
sebastien's avatar
sebastien committed
213
  double r = log(a);
214
  if (isnan(r))
215
216
217
    {
      if (error_not_printed)
        {
218
          mexPrintf("--------------------------------------\nError: log(X) with X=%5.15f\n in %s \n--------------------------------------\n", a,error_location(evaluate).c_str());
219
220
221
222
223
224
225
226
227
228
          error_not_printed = false;
          r = 0.0000000000000000000000001;
        }
      res1 = NAN;
      return r;
    }
  return r;
}

double
229
Interpreter::log10_1(double a, bool evaluate)
230
231
{
  double r = log(a);
232
  if (isnan(r))
233
    {
234
      if (error_not_printed)
sebastien's avatar
sebastien committed
235
        {
236
          mexPrintf("--------------------------------------\nError: log10(X) with X=%5.15f\n in %s \n--------------------------------------\n", a,error_location(evaluate).c_str());
sebastien's avatar
sebastien committed
237
          error_not_printed = false;
238
          r = 0.0000000000000000000000001;
sebastien's avatar
sebastien committed
239
240
        }
      res1 = NAN;
241
      return r;
242
    }
243
  return r;
244
245
}

246
247
248
249
250
251
252
253

string
Interpreter::print_expression(it_code_type it_code, bool evaluate)
{
  int var, lag = 0, op;
  stack<string> Stack;
  stack<double> Stackf;
  ostringstream tmp_out, tmp_out2;
254
255
  string v1, v2, v3;
  double v1f, v2f, v3f;
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
  bool go_on = true;
  double ll;
  ExpressionType equation_type;
  unsigned int equation_num;
  unsigned int dvar1, dvar2, dvar3;
  int lag1, lag2, lag3;
  size_t found;

  while (go_on)
    {
      switch (it_code->first)
        {
        case FNUMEXPR:
          switch (((FNUMEXPR_ *) it_code->second)->get_expression_type())
            {
            case TemporaryTerm:
              equation_type = TemporaryTerm;
              equation_num = ((FNUMEXPR_ *) it_code->second)->get_equation();
              break;
            case ModelEquation:
              equation_type = ModelEquation;
              equation_num = ((FNUMEXPR_ *) it_code->second)->get_equation();
              break;
            case FirstEndoDerivative:
              equation_type = FirstEndoDerivative;
              equation_num = ((FNUMEXPR_ *) it_code->second)->get_equation();
              dvar1 = ((FNUMEXPR_ *) it_code->second)->get_dvariable1();
              lag1 = ((FNUMEXPR_ *) it_code->second)->get_lag1();
              break;
            case FirstExoDerivative:
              equation_type = FirstExoDerivative;
              equation_num = ((FNUMEXPR_ *) it_code->second)->get_equation();
              dvar1 = ((FNUMEXPR_ *) it_code->second)->get_dvariable1();
              lag1 = ((FNUMEXPR_ *) it_code->second)->get_lag1();
              break;
            case FirstExodetDerivative:
              equation_type = FirstExodetDerivative;
              equation_num = ((FNUMEXPR_ *) it_code->second)->get_equation();
              dvar1 = ((FNUMEXPR_ *) it_code->second)->get_dvariable1();
              lag1 = ((FNUMEXPR_ *) it_code->second)->get_lag1();
              break;
            case FirstParamDerivative:
              equation_type = FirstParamDerivative;
              equation_num = ((FNUMEXPR_ *) it_code->second)->get_equation();
              dvar1 = ((FNUMEXPR_ *) it_code->second)->get_dvariable1();
              break;
            case SecondEndoDerivative:
              equation_type = SecondEndoDerivative;
              equation_num = ((FNUMEXPR_ *) it_code->second)->get_equation();
              dvar1 = ((FNUMEXPR_ *) it_code->second)->get_dvariable1();
              lag1 = ((FNUMEXPR_ *) it_code->second)->get_lag1();
              dvar2 = ((FNUMEXPR_ *) it_code->second)->get_dvariable2();
              lag2 = ((FNUMEXPR_ *) it_code->second)->get_lag2();
              break;
            case SecondExoDerivative:
              equation_type = SecondExoDerivative;
              equation_num = ((FNUMEXPR_ *) it_code->second)->get_equation();
              dvar1 = ((FNUMEXPR_ *) it_code->second)->get_dvariable1();
              lag1 = ((FNUMEXPR_ *) it_code->second)->get_lag1();
              dvar2 = ((FNUMEXPR_ *) it_code->second)->get_dvariable2();
              lag2 = ((FNUMEXPR_ *) it_code->second)->get_lag2();
              break;
            case SecondExodetDerivative:
              equation_type = SecondExodetDerivative;
              equation_num = ((FNUMEXPR_ *) it_code->second)->get_equation();
              dvar1 = ((FNUMEXPR_ *) it_code->second)->get_dvariable1();
              lag1 = ((FNUMEXPR_ *) it_code->second)->get_lag1();
              dvar2 = ((FNUMEXPR_ *) it_code->second)->get_dvariable2();
              lag2 = ((FNUMEXPR_ *) it_code->second)->get_lag2();
              break;
            case SecondParamDerivative:
              equation_type = SecondExodetDerivative;
              equation_num = ((FNUMEXPR_ *) it_code->second)->get_equation();
              dvar1 = ((FNUMEXPR_ *) it_code->second)->get_dvariable1();
              dvar2 = ((FNUMEXPR_ *) it_code->second)->get_dvariable2();
              break;
            case ThirdEndoDerivative:
              equation_type = ThirdEndoDerivative;
              equation_num = ((FNUMEXPR_ *) it_code->second)->get_equation();
              dvar1 = ((FNUMEXPR_ *) it_code->second)->get_dvariable1();
              lag1 = ((FNUMEXPR_ *) it_code->second)->get_lag1();
              dvar2 = ((FNUMEXPR_ *) it_code->second)->get_dvariable2();
              lag2 = ((FNUMEXPR_ *) it_code->second)->get_lag2();
              dvar3 = ((FNUMEXPR_ *) it_code->second)->get_dvariable2();
              lag3 = ((FNUMEXPR_ *) it_code->second)->get_lag3();
              break;
            case ThirdExoDerivative:
              equation_type = ThirdExoDerivative;
              equation_num = ((FNUMEXPR_ *) it_code->second)->get_equation();
              dvar1 = ((FNUMEXPR_ *) it_code->second)->get_dvariable1();
              lag1 = ((FNUMEXPR_ *) it_code->second)->get_lag1();
              dvar2 = ((FNUMEXPR_ *) it_code->second)->get_dvariable2();
              lag2 = ((FNUMEXPR_ *) it_code->second)->get_lag2();
              dvar3 = ((FNUMEXPR_ *) it_code->second)->get_dvariable2();
              lag3 = ((FNUMEXPR_ *) it_code->second)->get_lag3();
              break;
            case ThirdExodetDerivative:
              equation_type = ThirdExodetDerivative;
              equation_num = ((FNUMEXPR_ *) it_code->second)->get_equation();
              dvar1 = ((FNUMEXPR_ *) it_code->second)->get_dvariable1();
              lag1 = ((FNUMEXPR_ *) it_code->second)->get_lag1();
              dvar2 = ((FNUMEXPR_ *) it_code->second)->get_dvariable2();
              lag2 = ((FNUMEXPR_ *) it_code->second)->get_lag2();
              dvar3 = ((FNUMEXPR_ *) it_code->second)->get_dvariable2();
              lag3 = ((FNUMEXPR_ *) it_code->second)->get_lag3();
              break;
            case ThirdParamDerivative:
              equation_type = ThirdExodetDerivative;
              equation_num = ((FNUMEXPR_ *) it_code->second)->get_equation();
              dvar1 = ((FNUMEXPR_ *) it_code->second)->get_dvariable1();
              dvar2 = ((FNUMEXPR_ *) it_code->second)->get_dvariable2();
              dvar3 = ((FNUMEXPR_ *) it_code->second)->get_dvariable2();
              break;
369
370
371
            default:
              mexPrintf("Drivatives %d not implemented yet\n", it_code->first);
              mexErrMsgTxt("end of bytecode\n");
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
            }
          break;
        case FLDV:
          //load a variable in the processor
          switch (((FLDV_ *) it_code->second)->get_type())
            {
            case eParameter:
              var = ((FLDV_ *) it_code->second)->get_pos();
              tmp_out.str("");
              tmp_out << get_variable(eParameter, var);
              Stack.push(tmp_out.str());
              Stackf.push(params[var]);
              break;
            case eEndogenous:
              var = ((FLDV_ *) it_code->second)->get_pos();
              lag = ((FLDV_ *) it_code->second)->get_lead_lag();
              tmp_out.str("");
              if(lag != 0)
                tmp_out << get_variable(eEndogenous, var) << "(" << lag << ")";
              else
                tmp_out << get_variable(eEndogenous, var);
              Stack.push(tmp_out.str());
              if (evaluate)
                Stackf.push(ya[(it_+lag)*y_size+var]);
              else
                Stackf.push(y[(it_+lag)*y_size+var]);
              break;
            case eExogenous:
              var = ((FLDV_ *) it_code->second)->get_pos();
              lag = ((FLDV_ *) it_code->second)->get_lead_lag();
              tmp_out.str("");
              if(lag != 0)
                tmp_out << get_variable(eExogenous, var) << "(" << lag << ")";
              else
                tmp_out << get_variable(eExogenous, var);
              Stack.push(tmp_out.str());
              Stackf.push(x[it_+lag+var*nb_row_x]);
              break;
            case eExogenousDet:
              var = ((FLDV_ *) it_code->second)->get_pos();
              lag = ((FLDV_ *) it_code->second)->get_lead_lag();
              tmp_out.str("");
              if(lag != 0)
                tmp_out << get_variable(eExogenousDet, var) << "(" << lag << ")";
              else
                tmp_out << get_variable(eExogenousDet, var);
              Stack.push(tmp_out.str());
              Stackf.push(x[it_+lag+var*nb_row_xd]);
              break;
            case eModelLocalVariable:
              break;
            default:
              mexPrintf("FLDV: Unknown variable type\n");
            }
          break;
        case FLDSV:
        case FLDVS:
          //load a variable in the processor
          switch (((FLDSV_ *) it_code->second)->get_type())
            {
            case eParameter:
              var = ((FLDSV_ *) it_code->second)->get_pos();
              tmp_out.str("");
              tmp_out << get_variable(eParameter, var);
              Stack.push(tmp_out.str());
              Stackf.push(params[var]);
              break;
            case eEndogenous:
              var = ((FLDSV_ *) it_code->second)->get_pos();
              tmp_out.str("");
              tmp_out << get_variable(eEndogenous, var);
              Stack.push(tmp_out.str());
              if (it_code->first == FLDSV)
                {
                  if (evaluate)
                    Stackf.push(ya[var]);
                  else
                    Stackf.push(y[var]);
                }
              else
                Stackf.push(steady_y[var]);
              break;
            case eExogenous:
              var = ((FLDSV_ *) it_code->second)->get_pos();
              tmp_out.str("");
              tmp_out << get_variable(eExogenous, var);
              Stack.push(tmp_out.str());
              break;
              Stackf.push(x[var]);
            case eExogenousDet:
              var = ((FLDSV_ *) it_code->second)->get_pos();
              tmp_out.str("");
              tmp_out << get_variable(eExogenousDet, var);
              Stack.push(tmp_out.str());
              Stackf.push(x[var]);
              break;
            case eModelLocalVariable:
              break;
            default:
              mexPrintf("FLDSV: Unknown variable type\n");
            }
          break;
        case FLDT:
          //load a temporary variable in the processor
          var = ((FLDT_ *) it_code->second)->get_pos();
          tmp_out.str("");
          tmp_out << "T" << var+1;
          Stack.push(tmp_out.str());
          Stackf.push(T[var*(periods+y_kmin+y_kmax)+it_]);
          break;
        case FLDST:
          //load a temporary variable in the processor
          var = ((FLDT_ *) it_code->second)->get_pos();
          tmp_out.str("");
          tmp_out << "T" << var+1;
          Stack.push(tmp_out.str());
          Stackf.push(T[var]);
          break;
        case FLDU:
          //load u variable in the processor
          var = ((FLDU_ *) it_code->second)->get_pos();
          var += Per_u_;
          tmp_out.str("");
          tmp_out << "u[" << var+1 << "]";
          Stack.push(tmp_out.str());
          Stackf.push(u[var]);
          break;
        case FLDSU:
          //load u variable in the processor
          var = ((FLDSU_ *) it_code->second)->get_pos();
          tmp_out.str("");
          tmp_out << "u[" << var+1 << "]";
          Stack.push(tmp_out.str());
          Stackf.push(u[var]);
          break;
        case FLDR:
          var = ((FLDR_ *) it_code->second)->get_pos();
          tmp_out.str("");
          tmp_out << "residual[" << var+1 << "]";
          Stack.push(tmp_out.str());
          Stackf.push(r[var]);
          break;
        case FLDZ:
          //load 0 in the processor
          tmp_out.str("");
          tmp_out << 0;
          Stack.push(tmp_out.str());
          Stackf.push(0.0);
          break;
        case FLDC:
          //load a numerical constant in the processor
          ll = ((FLDC_ *) it_code->second)->get_value();
          tmp_out.str("");
          tmp_out << ll;
          Stack.push(tmp_out.str());
          Stackf.push(ll);
          break;
        case FSTPV:
          //load a variable in the processor
          go_on = false;
          switch (((FSTPV_ *) it_code->second)->get_type())
            {
            case eParameter:
              var = ((FSTPV_ *) it_code->second)->get_pos();
              tmp_out2.str("");
              tmp_out2 << Stack.top();
              tmp_out.str("");
              tmp_out << get_variable(eParameter, var) << " = " << tmp_out2.str();
              Stack.pop();
              params[var] = Stackf.top();
              Stackf.pop();
              break;
            case eEndogenous:
              var = ((FSTPV_ *) it_code->second)->get_pos();
              lag = ((FSTPV_ *) it_code->second)->get_lead_lag();
              tmp_out2.str("");
              tmp_out2 << Stack.top();
              tmp_out.str("");
              tmp_out << get_variable(eEndogenous, var);
              if (lag != 0)
                tmp_out << "(" << lag << ")";
              tmp_out << " = " << tmp_out2.str();
              Stack.pop();
              y[(it_+lag)*y_size+var] = Stackf.top();
              Stackf.pop();
              break;
            case eExogenous:
              var = ((FSTPV_ *) it_code->second)->get_pos();
              lag = ((FSTPV_ *) it_code->second)->get_lead_lag();
              tmp_out2.str("");
              tmp_out2 << Stack.top();
              tmp_out.str("");
              tmp_out << get_variable(eExogenous, var);
              if (lag != 0)
                tmp_out << "(" << lag << ")";
              tmp_out << " = " << tmp_out2.str();
              Stack.pop();
              x[it_+lag+var*nb_row_x]  = Stackf.top();
              Stackf.pop();
              break;
            case eExogenousDet:
              var = ((FSTPV_ *) it_code->second)->get_pos();
              lag = ((FSTPV_ *) it_code->second)->get_lead_lag();
              tmp_out2.str("");
              tmp_out2 << Stack.top();
              tmp_out.str("");
              tmp_out << get_variable(eExogenousDet, var);
              if (lag != 0)
                tmp_out << "(" << lag << ")";
              tmp_out << " = " << tmp_out2.str();
              Stack.pop();
              x[it_+lag+var*nb_row_xd] = Stackf.top();
              Stackf.pop();
              break;
            default:
              mexPrintf("FSTPV: Unknown variable type\n");
            }
          break;
        case FSTPSV:
          go_on = false;
          //load a variable in the processor
          switch (((FSTPSV_ *) it_code->second)->get_type())
            {
            case eParameter:
              var = ((FSTPSV_ *) it_code->second)->get_pos();
              tmp_out2.str("");
              tmp_out2 << Stack.top();
              tmp_out.str("");
              tmp_out << get_variable(eParameter, var);
              tmp_out << " = " << tmp_out2.str();
              Stack.pop();
              params[var] = Stackf.top();
              Stackf.pop();
              break;
            case eEndogenous:
              var = ((FSTPSV_ *) it_code->second)->get_pos();
              tmp_out2.str("");
              tmp_out2 << Stack.top();
              tmp_out.str("");
              tmp_out << get_variable(eEndogenous, var);
              tmp_out << " = " << tmp_out2.str();
              Stack.pop();
              y[var] = Stackf.top();
              Stackf.pop();
              break;
            case eExogenous:
            case eExogenousDet:
              var = ((FSTPSV_ *) it_code->second)->get_pos();
              tmp_out2.str("");
              tmp_out2 << Stack.top();
              tmp_out.str("");
              tmp_out << get_variable(eExogenous, var);
              tmp_out << " = " << tmp_out2.str();
              Stack.pop();
              x[var]  = Stackf.top();
              Stackf.pop();
              break;
            default:
              mexPrintf("FSTPSV: Unknown variable type\n");
            }
          break;
        case FSTPT:
          go_on = false;
          //store in a temporary variable from the processor
          var = ((FSTPT_ *) it_code->second)->get_pos();
          tmp_out.str("");
          tmp_out << "T" << var+1 << " = " << Stack.top();
          Stack.pop();
          T[var*(periods+y_kmin+y_kmax)+it_] = Stackf.top();
          Stackf.pop();
          break;
        case FSTPST:
          go_on = false;
          //store in a temporary variable from the processor
          var = ((FSTPT_ *) it_code->second)->get_pos();
          tmp_out.str("");
          tmp_out << "T" << var+1 << " = " << Stack.top();
          Stack.pop();
          T[var] = Stackf.top();
          Stackf.pop();
          break;
        case FSTPU:
          go_on = false;
          //store in u variable from the processor
          var = ((FSTPU_ *) it_code->second)->get_pos();
          var += Per_u_;
          tmp_out.str("");
          tmp_out << "u[" << var+1 << "] = " << Stack.top();
          Stack.pop();
          u[var] = Stackf.top();
          Stackf.pop();
          break;
        case FSTPSU:
          go_on = false;
          //store in u variable from the processor
          var = ((FSTPU_ *) it_code->second)->get_pos();
          tmp_out.str("");
          tmp_out << "u[" << var+1 << "] = " << Stack.top();
          Stack.pop();
          u[var] = Stackf.top();
          Stackf.pop();
          break;
        case FSTPR:
          go_on = false;
          //store in residual variable from the processor
          var = ((FSTPR_ *) it_code->second)->get_pos();
          tmp_out.str("");
          tmp_out << "residual[" << var+1 << "] = " << Stack.top();
          Stack.pop();
          r[var] = Stackf.top();
          Stackf.pop();
          break;
        case FSTPG:
          go_on = false;
          //store in derivative (g) variable from the processor
          var = ((FSTPG_ *) it_code->second)->get_pos();
          tmp_out.str("");
          tmp_out << "g1[" << var+1 << "] = " << Stack.top();
          Stack.pop();
          g1[var] = Stackf.top();
          Stackf.pop();
          break;
        case FBINARY:
          op = ((FBINARY_ *) it_code->second)->get_op_type();
          v2 = Stack.top();
          Stack.pop();
          v1 = Stack.top();
          Stack.pop();
          v2f = Stackf.top();
          Stackf.pop();
          v1f = Stackf.top();
          Stackf.pop();
          switch (op)
            {
            case oPlus:
              Stackf.push(v1f + v2f);
              tmp_out.str("");
              tmp_out << v1 << " + " << v2;
              Stack.push(tmp_out.str());
              break;
            case oMinus:
              Stackf.push(v1f - v2f);
              tmp_out.str("");
              found = v1.find(" ");
              if (found != string::npos)
                tmp_out << "(";
              tmp_out << v1;
              if (found != string::npos)
                tmp_out << ")";
              tmp_out << " - ";
              found = v2.find(" ");
              if (found != string::npos)
                tmp_out << "(";
              tmp_out << v2;
              if (found != string::npos)
                tmp_out << ")";
              Stack.push(tmp_out.str());
              break;
            case oTimes:
              Stackf.push(v1f * v2f);
              tmp_out.str("");
              found = v1.find(" ");
              if (found != string::npos)
                tmp_out << "(";
              tmp_out << v1;
              if (found != string::npos)
                tmp_out << ")";
              tmp_out << " * ";
              found = v2.find(" ");
              if (found != string::npos)
                tmp_out << "(";
              tmp_out << v2;
              if (found != string::npos)
                tmp_out << ")";
              Stack.push(tmp_out.str());
              break;
            case oDivide:
              double r;
              r = v1f / v2f;
              Stackf.push(r);
              tmp_out.str("");
              found = v1.find(" ");
              if (found != string::npos)
                tmp_out << "(";
              tmp_out << v1;
              if (found != string::npos)
                tmp_out << ")";
              if (isinf(r))
                tmp_out << "$";
              tmp_out << " / ";
              if (isinf(r))
                tmp_out << "";
              found = v2.find(" ");
              if (found != string::npos)
                tmp_out << "(";
              tmp_out << v2;
              if (found != string::npos)
                tmp_out << ")";
              Stack.push(tmp_out.str());
              break;
            case oLess:
              Stackf.push(double (v1f < v2f));
              tmp_out.str("");
              found = v1.find(" ");
              if (found != string::npos)
                tmp_out << "(";
              tmp_out << v1;
              if (found != string::npos)
                tmp_out << ")";
              tmp_out << " < ";
              found = v2.find(" ");
              if (found != string::npos)
                tmp_out << "(";
              tmp_out << v2;
              if (found != string::npos)
                tmp_out << ")";
              Stack.push(tmp_out.str());
              break;
            case oGreater:
              Stackf.push(double (v1f > v2f));
              tmp_out.str("");
              found = v1.find(" ");
              if (found != string::npos)
                tmp_out << "(";
              tmp_out << v1;
              if (found != string::npos)
                tmp_out << ")";
              tmp_out << " > ";
              found = v2.find(" ");
              if (found != string::npos)
                tmp_out << "(";
              tmp_out << v2;
              if (found != string::npos)
                tmp_out << ")";
              Stack.push(tmp_out.str());
              break;
            case oLessEqual:
              Stackf.push(double (v1f <= v2f));
              tmp_out.str("");
              found = v1.find(" ");
              if (found != string::npos)
                tmp_out << "(";
              tmp_out << v1;
              if (found != string::npos)
                tmp_out << ")";
              tmp_out << " <= ";
              found = v2.find(" ");
              if (found != string::npos)
                tmp_out << "(";
              tmp_out << v2;
              if (found != string::npos)
                tmp_out << ")";
              Stack.push(tmp_out.str());
              break;
            case oGreaterEqual:
              Stackf.push(double (v1f >= v2f));
              tmp_out.str("");
              found = v1.find(" ");
              if (found != string::npos)
                tmp_out << "(";
              tmp_out << v1;
              if (found != string::npos)
                tmp_out << ")";
              tmp_out << " >= ";
              found = v2.find(" ");
              if (found != string::npos)
                tmp_out << "(";
              tmp_out << v2;
              if (found != string::npos)
                tmp_out << ")";
              Stack.push(tmp_out.str());
              break;
            case oEqualEqual:
              Stackf.push(double (v1f == v2f));
              tmp_out.str("");
              found = v1.find(" ");
              if (found != string::npos)
                tmp_out << "(";
              tmp_out << v1;
              if (found != string::npos)
                tmp_out << ")";
              tmp_out << " == ";
              found = v2.find(" ");
              if (found != string::npos)
                tmp_out << "(";
              tmp_out << v2;
              if (found != string::npos)
                tmp_out << ")";
              Stack.push(tmp_out.str());
              break;
            case oDifferent:
              Stackf.push(double (v1f != v2f));
              tmp_out.str("");
              found = v1.find(" ");
              if (found != string::npos)
                tmp_out << "(";
              tmp_out << v1;
              if (found != string::npos)
                tmp_out << ")";
              tmp_out << " != ";
              found = v2.find(" ");
              if (found != string::npos)
                tmp_out << "(";
              tmp_out << v2;
              if (found != string::npos)
                tmp_out << ")";
              Stack.push(tmp_out.str());
              break;
            case oPower:
              r = pow(v1f, v2f);
              Stackf.push(r);
              tmp_out.str("");
              found = v1.find(" ");
              if (found != string::npos)
                tmp_out << "(";
              tmp_out << v1;
              if (found != string::npos)
                tmp_out << ")";
              if(isnan(r))
                tmp_out << "$ ^ ";
              else
                tmp_out << " ^ ";
              found = v2.find(" ");
              if (found != string::npos)
                tmp_out << "(";
              tmp_out << v2;
              if (found != string::npos)
                tmp_out << ")";
              Stack.push(tmp_out.str());
              break;
            case oMax:
              Stackf.push(max(v1f, v2f));
              tmp_out.str("");
              tmp_out << "max(" << v1 << ", " << v2 << ")";
              Stack.push(tmp_out.str());
              break;
            case oMin:
              Stackf.push(min(v1f, v2f));
              tmp_out.str("");
              tmp_out << "min(" << v1 << ", " << v2 << ")";
              Stack.push(tmp_out.str());
              break;
            case oEqual:
            default:
              /*throw EvalException();*/
              ;
            }
          break;
        case FUNARY:
          op = ((FUNARY_ *) it_code->second)->get_op_type();
          v1 = Stack.top();
          Stack.pop();
          v1f = Stackf.top();
          Stackf.pop();
          double r;
          switch (op)
            {
            case oUminus:
              Stackf.push(-v1f);
              tmp_out.str("");
              tmp_out << " -" << v1;
              Stack.push(tmp_out.str());
              break;
            case oExp:
              Stackf.push(exp(v1f));
              tmp_out.str("");
              tmp_out << "exp(" << v1 << ")";
              Stack.push(tmp_out.str());
              break;
            case oLog:
              r = log(v1f);
              Stackf.push(r);
              tmp_out.str("");
              if (isnan(r))
                tmp_out << "$log(" << v1 << ")";
              else
                tmp_out << "log(" << v1 << ")";
              Stack.push(tmp_out.str());
              break;
            case oLog10:
              r = log10(v1f);
              Stackf.push(r);
              tmp_out.str("");
              if (isnan(r))
                tmp_out << "$log10(" << v1 << ")";
              else
                tmp_out << "log10(" << v1 << ")";
              Stack.push(tmp_out.str());
              break;
            case oCos:
              Stackf.push(cos(v1f));
              tmp_out.str("");
              tmp_out << "cos(" << v1 << ")";
              Stack.push(tmp_out.str());
              break;
            case oSin:
              Stackf.push(sin(v1f));
              tmp_out.str("");
              tmp_out << "sin(" << v1 << ")";
              Stack.push(tmp_out.str());
              break;
            case oTan:
              Stackf.push(tan(v1f));
              tmp_out.str("");
              tmp_out << "tan(" << v1 << ")";
              Stack.push(tmp_out.str());
              break;
            case oAcos:
              Stackf.push(acos(v1f));
              tmp_out.str("");
              tmp_out << "acos(" << v1 << ")";
              Stack.push(tmp_out.str());
              break;
            case oAsin:
              Stackf.push(asin(v1f));
              tmp_out.str("");
              tmp_out << "asin(" << v1 << ")";
              Stack.push(tmp_out.str());
              break;
            case oAtan:
              Stackf.push(atan(v1f));
              tmp_out.str("");
              tmp_out << "atan(" << v1 << ")";
              Stack.push(tmp_out.str());
              break;
            case oCosh:
              Stackf.push(cosh(v1f));
              tmp_out.str("");
              tmp_out << "cosh(" << v1 << ")";