BlockTriangular.cc 48.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
 * Copyright (C) 2007-2008 Dynare Team
 *
 * 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/>.
 */

// TODO Apply Block Decomposition to the static model

#include <iostream>
#include <sstream>
#include <fstream>
25
26
27
28
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <cmath>
29
30
31
32
33
using namespace std;
//------------------------------------------------------------------------------
#include "BlockTriangular.hh"
//------------------------------------------------------------------------------

ferhat's avatar
ferhat committed
34
35
36
/*BlockTriangular::BlockTriangular(const SymbolTable &symbol_table_arg) :
  symbol_table(symbol_table_arg),
  normalization(symbol_table_arg)*/
37
38
BlockTriangular::BlockTriangular(const SymbolTable &symbol_table_arg) :
  symbol_table(symbol_table_arg),
ferhat's avatar
ferhat committed
39
40
  normalization(symbol_table_arg),
  incidencematrix(symbol_table_arg)
41
42
43
44
45
46
{
  bt_verbose = 0;
  ModelBlock = NULL;
  periods = 0;
}

ferhat's avatar
ferhat committed
47
48
49
50
51
52
53
54

IncidenceMatrix::IncidenceMatrix(const SymbolTable &symbol_table_arg) :
  symbol_table(symbol_table_arg)
{
  Model_Max_Lead = Model_Max_Lead_Endo = Model_Max_Lead_Exo = 0;
  Model_Max_Lag = Model_Max_Lag_Endo = Model_Max_Lag_Exo = 0;

}
55
56
57
//------------------------------------------------------------------------------
//For a lead or a lag build the Incidence Matrix structures
List_IM*
ferhat's avatar
ferhat committed
58
IncidenceMatrix::Build_IM(int lead_lag, SymbolType type)
59
60
{
  int i;
ferhat's avatar
ferhat committed
61
62
  List_IM* pIM = new List_IM;
  if(type==eEndogenous)
63
    {
ferhat's avatar
ferhat committed
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
      Last_IM->pNext = pIM;
      pIM->IM = (bool*)malloc(symbol_table.endo_nbr * symbol_table.endo_nbr * sizeof(pIM->IM[0]));
      for(i = 0;i < symbol_table.endo_nbr*symbol_table.endo_nbr;i++)
        pIM->IM[i] = 0;
      pIM->lead_lag = lead_lag;
      if(lead_lag > 0)
        {
          if(lead_lag > Model_Max_Lead_Endo)
            {
              Model_Max_Lead_Endo = lead_lag;
              if(lead_lag > Model_Max_Lead)
                Model_Max_Lead = lead_lag;
            }
        }
      else
        {
          if( -lead_lag > Model_Max_Lag_Endo)
            {
              Model_Max_Lag_Endo = -lead_lag;
              if(-lead_lag > Model_Max_Lag)
                Model_Max_Lag = -lead_lag;
            }
        }
      pIM->pNext = NULL;
      Last_IM = pIM;
89
90
    }
  else
ferhat's avatar
ferhat committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
    {  //eExogenous
      Last_IM_X->pNext = pIM;
      pIM->IM = (bool*)malloc(symbol_table.exo_nbr * symbol_table.endo_nbr * sizeof(pIM->IM[0]));
      for(i = 0;i < symbol_table.exo_nbr*symbol_table.endo_nbr;i++)
        pIM->IM[i] = 0;
      pIM->lead_lag = lead_lag;
      if(lead_lag > 0)
        {
          if(lead_lag > Model_Max_Lead_Exo)
            {
              Model_Max_Lead_Exo = lead_lag;
              if(lead_lag > Model_Max_Lead)
                Model_Max_Lead = lead_lag;
            }
        }
      else
        {
          if( -lead_lag > Model_Max_Lag_Exo)
            {
              Model_Max_Lag_Exo = -lead_lag;
              if(-lead_lag > Model_Max_Lag)
                Model_Max_Lag = -lead_lag;
            }
        }
      pIM->pNext = NULL;
      Last_IM_X = pIM;
117
118
119
120
121
122
123
    }
  return (pIM);
}

//------------------------------------------------------------------------------
// initialize all the incidence matrix structures
void
ferhat's avatar
ferhat committed
124
IncidenceMatrix::init_incidence_matrix()
125
126
127
{
  int i;
  First_IM = new List_IM;
ferhat's avatar
ferhat committed
128
129
  First_IM->IM = (bool*)malloc(symbol_table.endo_nbr * symbol_table.endo_nbr * sizeof(First_IM->IM[0]));
  for(i = 0;i < symbol_table.endo_nbr*symbol_table.endo_nbr;i++)
130
131
132
133
    First_IM->IM[i] = 0;
  First_IM->lead_lag = 0;
  First_IM->pNext = NULL;
  Last_IM = First_IM;
ferhat's avatar
ferhat committed
134
135
136
137
138
139
140
141
142

  First_IM_X = new List_IM;
  First_IM_X->IM = (bool*)malloc(symbol_table.exo_nbr * symbol_table.endo_nbr * sizeof(First_IM_X->IM[0]));
  for(i = 0;i < symbol_table.endo_nbr*symbol_table.exo_nbr;i++)
    First_IM_X->IM[i] = 0;
  First_IM_X->lead_lag = 0;
  First_IM_X->pNext = NULL;
  Last_IM_X = First_IM_X;

143
144
145
146
}


void
ferhat's avatar
ferhat committed
147
IncidenceMatrix::Free_IM() const
148
149
150
151
152
{
  List_IM *Cur_IM, *SFirst_IM;
  Cur_IM = SFirst_IM = First_IM;
  while(Cur_IM)
    {
ferhat's avatar
ferhat committed
153
      SFirst_IM = Cur_IM->pNext;
154
      free(Cur_IM->IM);
155
      delete Cur_IM;
ferhat's avatar
ferhat committed
156
157
158
159
160
161
162
163
164
      Cur_IM = SFirst_IM;
    }
  Cur_IM = SFirst_IM = First_IM_X;
  while(Cur_IM)
    {
      SFirst_IM = Cur_IM->pNext;
      free(Cur_IM->IM);
      delete Cur_IM;
      Cur_IM = SFirst_IM;
165
166
167
168
    }
}

//------------------------------------------------------------------------------
ferhat's avatar
ferhat committed
169
// Return the incidence matrix related to a lead or a lag
170
List_IM*
ferhat's avatar
ferhat committed
171
IncidenceMatrix::Get_IM(int lead_lag, SymbolType type) const
172
173
{
  List_IM* Cur_IM;
ferhat's avatar
ferhat committed
174
175
176
177
  if(type==eEndogenous)
    Cur_IM = First_IM;
  else
    Cur_IM = First_IM_X;
178
179
180
181
182
183
  while ((Cur_IM != NULL) && (Cur_IM->lead_lag != lead_lag))
    Cur_IM = Cur_IM->pNext;
  return (Cur_IM);
}

bool*
ferhat's avatar
ferhat committed
184
IncidenceMatrix::bGet_IM(int lead_lag, SymbolType type) const
185
186
{
  List_IM* Cur_IM;
ferhat's avatar
ferhat committed
187
188
189
190
  if(type==eEndogenous)
    Cur_IM = First_IM;
  else
    Cur_IM = First_IM_X;
191
192
193
194
  while ((Cur_IM != NULL) && (Cur_IM->lead_lag != lead_lag))
    {
      Cur_IM = Cur_IM->pNext;
    }
ferhat's avatar
ferhat committed
195
196
197
198
  if(Cur_IM)
    return (Cur_IM->IM);
  else
    return(0);
199
200
201
202
203
204
}


//------------------------------------------------------------------------------
// Fill the incidence matrix related to a lead or a lag
void
ferhat's avatar
ferhat committed
205
IncidenceMatrix::fill_IM(int equation, int variable, int lead_lag, SymbolType type)
206
207
{
  List_IM* Cur_IM;
ferhat's avatar
ferhat committed
208
209
  Cur_IM = Get_IM(lead_lag, type);
  if(equation >= symbol_table.endo_nbr)
210
    {
ferhat's avatar
ferhat committed
211
      cout << "Error : The model has more equations (at least " << equation + 1 << ") than declared endogenous variables (" << symbol_table.endo_nbr << ")\n";
212
      exit(EXIT_FAILURE);
213
214
    }
  if (!Cur_IM)
ferhat's avatar
ferhat committed
215
216
217
218
219
    Cur_IM = Build_IM(lead_lag, type);
  if(type==eEndogenous)
    Cur_IM->IM[equation*symbol_table.endo_nbr + variable] = 1;
  else
    Cur_IM->IM[equation*symbol_table.exo_nbr + variable] = 1;
220
221
222
223
224
}

//------------------------------------------------------------------------------
// unFill the incidence matrix related to a lead or a lag
void
ferhat's avatar
ferhat committed
225
IncidenceMatrix::unfill_IM(int equation, int variable, int lead_lag, SymbolType type)
226
227
{
  List_IM* Cur_IM;
ferhat's avatar
ferhat committed
228
  Cur_IM = Get_IM(lead_lag, type);
229
  if (!Cur_IM)
ferhat's avatar
ferhat committed
230
231
232
233
234
235
236
237
238
239
240
241
242
243
    Cur_IM = Build_IM(lead_lag, type);
  if(type==eEndogenous)
    Cur_IM->IM[equation*symbol_table.endo_nbr + variable] = 0;
  else
    Cur_IM->IM[equation*symbol_table.exo_nbr + variable] = 0;
}

List_IM*
IncidenceMatrix::Get_First(SymbolType type) const
{
  if(type==eEndogenous)
    return(First_IM);
  else
    return(First_IM_X);
244
245
246
}


ferhat's avatar
ferhat committed
247
248
249
250
251
252
253
254
255
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
//------------------------------------------------------------------------------
//For a lead or a lag build the Incidence Matrix structures
/*List_IM*
IncidenceMatrix::Build_IM_X(int lead_lag)
{
  List_IM* pIM_X = new List_IM;
  int i;
  Last_IM_X->pNext = pIM_X;
  pIM_X->IM = (bool*)malloc(exo_nbr * endo_nbr * sizeof(pIM_X->IM[0]));
  for(i = 0;i < exo_nbr*endo_nbr;i++)
    pIM_X->IM[i] = 0;
  pIM_X->lead_lag = lead_lag;
  if(lead_lag > 0)
    {
      if(lead_lag > Model_Max_Lead_Exo)
        {
          Model_Max_Lead_Exo = lead_lag;
          if(lead_lag > Model_Max_Lead)
            Model_Max_Lead = lead_lag;
        }
    }
  else
    {
      if( -lead_lag > Model_Max_Lag_Exo)
        {
          Model_Max_Lag_Exo = -lead_lag;
          if(-lead_lag > Model_Max_Lag)
            Model_Max_Lag = -lead_lag;
        }
    }
  pIM_X->pNext = NULL;
  Last_IM_X = pIM_X;
  return (pIM_X);
}

//------------------------------------------------------------------------------
// initialize all the incidence matrix structures
void
IncidenceMatrix::init_incidence_matrix_X(int nb_exo)
{
  int i;
  //cout << "init_incidence_matrix_X nb_exo = " << nb_exo << " endo_nbr=" << endo_nbr << "\n";
  exo_nbr = nb_exo;
  First_IM_X = new List_IM;
  First_IM_X->IM = (bool*)malloc(nb_exo * endo_nbr * sizeof(First_IM_X->IM[0]));
  for(i = 0;i < endo_nbr*nb_exo;i++)
    First_IM_X->IM[i] = 0;
  First_IM_X->lead_lag = 0;
  First_IM_X->pNext = NULL;
  Last_IM_X = First_IM_X;
}


void
IncidenceMatrix::Free_IM_X(List_IM* First_IM_X) const
{
  List_IM *Cur_IM_X, *SFirst_IM_X;
  Cur_IM_X = SFirst_IM_X = First_IM_X;
  while(Cur_IM_X)
    {
      First_IM_X = Cur_IM_X->pNext;
      free(Cur_IM_X->IM);
      delete Cur_IM_X;
      Cur_IM_X = First_IM_X;
    }
}


//------------------------------------------------------------------------------
// Return the inceidence matrix related to a lead or a lag
List_IM*
IncidenceMatrix::Get_IM_X(int lead_lag)
{
  List_IM* Cur_IM_X;
  Cur_IM_X = First_IM_X;
  while ((Cur_IM_X != NULL) && (Cur_IM_X->lead_lag != lead_lag))
    Cur_IM_X = Cur_IM_X->pNext;
  return (Cur_IM_X);
}

bool*
IncidenceMatrix::bGet_IM_X(int lead_lag) const
{
  List_IM* Cur_IM_X;
  Cur_IM_X = First_IM_X;
  while ((Cur_IM_X != NULL) && (Cur_IM_X->lead_lag != lead_lag))
    {
      Cur_IM_X = Cur_IM_X->pNext;
    }
  if(Cur_IM_X)
    return (Cur_IM_X->IM);
  else
    return(0);
}


//------------------------------------------------------------------------------
// Fill the incidence matrix related to a lead or a lag
void
IncidenceMatrix::fill_IM_X(int equation, int variable_exo, int lead_lag)
{
  List_IM* Cur_IM_X;
  Cur_IM_X = Get_IM_X(lead_lag);
  if(equation >= endo_nbr)
    {
      cout << "Error : The model has more equations (at least " << equation + 1 << ") than declared endogenous variables (" << endo_nbr << ")\n";
      exit(EXIT_FAILURE);
    }
  if (!Cur_IM_X)
    {
      Cur_IM_X = Build_IM_X(lead_lag);
    }
  Cur_IM_X->IM[equation*exo_nbr + variable_exo] = true;
}
*/

363
364
365
//------------------------------------------------------------------------------
//Print azn incidence matrix
void
ferhat's avatar
ferhat committed
366
IncidenceMatrix::Print_SIM(bool* IM, SymbolType type) const
367
{
ferhat's avatar
ferhat committed
368
369
370
371
372
373
  int i, j, n;
  if(type == eEndogenous)
    n = symbol_table.endo_nbr;
  else
    n = symbol_table.exo_nbr;
  for(i = 0;i < symbol_table.endo_nbr;i++)
374
375
376
377
378
379
380
381
382
383
384
    {
      cout << " ";
      for(j = 0;j < n;j++)
        cout << IM[i*n + j] << " ";
      cout << "\n";
    }
}

//------------------------------------------------------------------------------
//Print all incidence matrix
void
ferhat's avatar
ferhat committed
385
IncidenceMatrix::Print_IM(SymbolType type) const
386
387
{
  List_IM* Cur_IM;
ferhat's avatar
ferhat committed
388
389
390
391
  if(type == eEndogenous)
    Cur_IM = First_IM;
  else
    Cur_IM = First_IM_X;
392
393
394
395
  cout << "-------------------------------------------------------------------\n";
  while(Cur_IM)
    {
      cout << "Incidence matrix for lead_lag = " << Cur_IM->lead_lag << "\n";
ferhat's avatar
ferhat committed
396
      Print_SIM(Cur_IM->IM, type);
397
398
399
400
401
402
403
404
      Cur_IM = Cur_IM->pNext;
    }
}


//------------------------------------------------------------------------------
// Swap rows and columns of the incidence matrix
void
ferhat's avatar
ferhat committed
405
IncidenceMatrix::swap_IM_c(bool *SIM, int pos1, int pos2, int pos3, simple* Index_Var_IM, simple* Index_Equ_IM, int n) const
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
{
  int tmp_i, j;
  bool tmp_b;
  /* We exchange equation (row)...*/
  if(pos1 != pos2)
    {
      tmp_i = Index_Equ_IM[pos1].index;
      Index_Equ_IM[pos1].index = Index_Equ_IM[pos2].index;
      Index_Equ_IM[pos2].index = tmp_i;
      for(j = 0;j < n;j++)
        {
          tmp_b = SIM[pos1 * n + j];
          SIM[pos1*n + j] = SIM[pos2 * n + j];
          SIM[pos2*n + j] = tmp_b;
        }
    }
422
  /* ...and variables (column)*/
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
  if(pos1 != pos3)
    {
      tmp_i = Index_Var_IM[pos1].index;
      Index_Var_IM[pos1].index = Index_Var_IM[pos3].index;
      Index_Var_IM[pos3].index = tmp_i;
      for(j = 0;j < n;j++)
        {
          tmp_b = SIM[j * n + pos1];
          SIM[j*n + pos1] = SIM[j * n + pos3];
          SIM[j*n + pos3] = tmp_b;
        }
    }
}

//------------------------------------------------------------------------------
// Find the prologue and the epilogue of the model
void
440
BlockTriangular::Prologue_Epilogue(bool* IM, int* prologue, int* epilogue, int n, simple* Index_Var_IM, simple* Index_Equ_IM, bool* IM0)
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
{
  bool modifie = 1;
  int i, j, k, l = 0;
  /*Looking for a prologue */
  *prologue = 0;
  while(modifie)
    {
      modifie = 0;
      for(i = *prologue;i < n;i++)
        {
          k = 0;
          for(j = *prologue;j < n;j++)
            {
              if(IM[i*n + j])
                {
                  k++;
                  l = j;
                }
            }
460
          if ((k == 1) && IM0[Index_Equ_IM[i].index*n + Index_Var_IM[l].index])
461
462
            {
              modifie = 1;
ferhat's avatar
ferhat committed
463
              incidencematrix.swap_IM_c(IM, *prologue, i, l, Index_Var_IM, Index_Equ_IM, n);
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
              (*prologue)++;
            }
        }
    }
  *epilogue = 0;
  modifie = 1;
  while(modifie)
    {
      modifie = 0;
      for(i = *prologue;i < n - *epilogue;i++)
        {
          k = 0;
          for(j = *prologue;j < n - *epilogue;j++)
            {
              if(IM[j*n + i])
                {
                  k++;
                  l = j;
                }
            }
484
          if ((k == 1) && IM0[Index_Equ_IM[l].index*n + Index_Var_IM[i].index])
485
486
            {
              modifie = 1;
ferhat's avatar
ferhat committed
487
              incidencematrix.swap_IM_c(IM, n - (1 + *epilogue), l, i, Index_Var_IM, Index_Equ_IM, n);
488
489
490
491
492
493
494
495
              (*epilogue)++;
            }
        }
    }
}


void
sebastien's avatar
sebastien committed
496
BlockTriangular::Allocate_Block(int size, int *count_Equ, int *count_Block, BlockType type, Model_Block * ModelBlock)
497
{
ferhat's avatar
ferhat committed
498
499
  int i, j, k, l, ls, m, i_1, Lead, Lag, first_count_equ, i1;
  int *tmp_size, *tmp_size_exo, *tmp_var, *tmp_endo, *tmp_exo, tmp_nb_exo, nb_lead_lag_endo;
500
501
502
  List_IM *Cur_IM;
  bool *IM, OK;
  ModelBlock->Periods = periods;
ferhat's avatar
ferhat committed
503
  int Lag_Endo, Lead_Endo, Lag_Exo, Lead_Exo;
504
505
506
507
508
509
510
511
512
513
  if ((type == PROLOGUE) || (type == EPILOGUE))
    {
      for(i = 0;i < size;i++)
        {
          ModelBlock->Block_List[*count_Block].is_linear=true;
          ModelBlock->Block_List[*count_Block].Size = 1;
          ModelBlock->Block_List[*count_Block].Type = type;
          ModelBlock->Block_List[*count_Block].Simulation_Type = UNKNOWN;
          ModelBlock->Block_List[*count_Block].Temporary_terms=new temporary_terms_type ();
          ModelBlock->Block_List[*count_Block].Temporary_terms->clear();
ferhat's avatar
ferhat committed
514
515
          tmp_endo = (int*)malloc((incidencematrix.Model_Max_Lead + incidencematrix.Model_Max_Lag + 1) * sizeof(int));
          tmp_size = (int*)malloc((incidencematrix.Model_Max_Lead + incidencematrix.Model_Max_Lag + 1) * sizeof(int));
516
          tmp_var = (int*)malloc(sizeof(int));
ferhat's avatar
ferhat committed
517
518
519
520
          tmp_size_exo = (int*)malloc((incidencematrix.Model_Max_Lead + incidencematrix.Model_Max_Lag + 1) * sizeof(int));
          memset(tmp_size_exo, 0, (incidencematrix.Model_Max_Lead + incidencematrix.Model_Max_Lag + 1)*sizeof(int));
          memset(tmp_size, 0, (incidencematrix.Model_Max_Lead + incidencematrix.Model_Max_Lag + 1)*sizeof(int));
          memset(tmp_endo, 0, (incidencematrix.Model_Max_Lead + incidencematrix.Model_Max_Lag + 1)*sizeof(int));
521
          nb_lead_lag_endo = Lead = Lag = 0;
ferhat's avatar
ferhat committed
522
523
          Lag_Endo = Lead_Endo = Lag_Exo = Lead_Exo = 0;
          Cur_IM = incidencematrix.Get_First(eEndogenous);
524
525
526
          while(Cur_IM)
            {
              k = Cur_IM->lead_lag;
ferhat's avatar
ferhat committed
527
              i_1 = Index_Equ_IM[*count_Equ].index * symbol_table.endo_nbr;
528
529
              if(k > 0)
                {
ferhat's avatar
ferhat committed
530
                  if(Cur_IM->IM[i_1 + Index_Var_IM[*count_Equ].index])
531
532
                    {
                      nb_lead_lag_endo++;
ferhat's avatar
ferhat committed
533
                      tmp_size[incidencematrix.Model_Max_Lag_Endo + k]++;
534
                      if(k > Lead)
ferhat's avatar
ferhat committed
535
                        Lead = k;
536
537
538
539
540
                    }
                }
              else
                {
                  k = -k;
ferhat's avatar
ferhat committed
541
                  if(Cur_IM->IM[i_1 + Index_Var_IM[*count_Equ].index])
542
                    {
ferhat's avatar
ferhat committed
543
                      tmp_size[incidencematrix.Model_Max_Lag_Endo - k]++;
544
545
                      nb_lead_lag_endo++;
                      if(k > Lag)
ferhat's avatar
ferhat committed
546
                        Lag = k;
547
548
549
550
                    }
                }
              Cur_IM = Cur_IM->pNext;
            }
ferhat's avatar
ferhat committed
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

          Lag_Endo = Lag;
          Lead_Endo = Lead;
          tmp_exo = (int*)malloc(symbol_table.exo_nbr * sizeof(int));
          memset(tmp_exo, 0, symbol_table.exo_nbr * sizeof(int));
          tmp_nb_exo = 0;


          Cur_IM = incidencematrix.Get_First(eExogenous);
          k = Cur_IM->lead_lag;
          while(Cur_IM)
            {
              i_1 = Index_Equ_IM[*count_Equ].index * symbol_table.exo_nbr;
              for(j=0;j<symbol_table.exo_nbr;j++)
                if(Cur_IM->IM[i_1 + j])
                  {
                    if(!tmp_exo[j])
                      {
                        tmp_exo[j] = 1;
                        tmp_nb_exo++;
                      }
                    if(k>0 && k>Lead_Exo)
                      Lead_Exo = k;
                    else if(k<0 && (-k)>Lag_Exo)
                      Lag_Exo = -k;
                    if(k>0 && k>Lead)
                      Lead = k;
                    else if(k<0 && (-k)>Lag)
                      Lag = -k;
                    tmp_size_exo[k+incidencematrix.Model_Max_Lag_Exo]++;
                  }
                Cur_IM = Cur_IM->pNext;
                if(Cur_IM)
                  k = Cur_IM->lead_lag;
            }


          ModelBlock->Block_List[*count_Block].nb_exo = tmp_nb_exo;
          ModelBlock->Block_List[*count_Block].Exogenous = (int*)malloc(tmp_nb_exo * sizeof(int));
          k = 0;
          for(j=0;j<symbol_table.exo_nbr;j++)
            if(tmp_exo[j])
              {
                ModelBlock->Block_List[*count_Block].Exogenous[k] = j;
                k++;
              }

          ModelBlock->Block_List[*count_Block].nb_exo_det = 0;

600
601
          ModelBlock->Block_List[*count_Block].Max_Lag = Lag;
          ModelBlock->Block_List[*count_Block].Max_Lead = Lead;
ferhat's avatar
ferhat committed
602
603
604
605
          ModelBlock->Block_List[*count_Block].Max_Lag_Endo = Lag_Endo;
          ModelBlock->Block_List[*count_Block].Max_Lead_Endo = Lead_Endo;
          ModelBlock->Block_List[*count_Block].Max_Lag_Exo = Lag_Exo;
          ModelBlock->Block_List[*count_Block].Max_Lead_Exo = Lead_Exo;
606
607
608
609
610
611
          ModelBlock->Block_List[*count_Block].Equation = (int*)malloc(sizeof(int));
          ModelBlock->Block_List[*count_Block].Variable = (int*)malloc(sizeof(int));
          ModelBlock->Block_List[*count_Block].Own_Derivative = (int*)malloc(sizeof(int));
          ModelBlock->Block_List[*count_Block].Equation[0] = Index_Equ_IM[*count_Equ].index;
          ModelBlock->Block_List[*count_Block].Variable[0] = Index_Var_IM[*count_Equ].index;
          if ((Lead > 0) && (Lag > 0))
612
613
614
615
            ModelBlock->Block_List[*count_Block].Simulation_Type = SOLVE_TWO_BOUNDARIES_SIMPLE;
          else if((Lead > 0) && (Lag == 0))
            ModelBlock->Block_List[*count_Block].Simulation_Type = SOLVE_BACKWARD_SIMPLE;
          else
616
            ModelBlock->Block_List[*count_Block].Simulation_Type = SOLVE_FORWARD_SIMPLE;
ferhat's avatar
ferhat committed
617
618
619
620
621
622

          Cur_IM = incidencematrix.Get_First(eExogenous);
          tmp_exo = (int*)malloc(symbol_table.exo_nbr * sizeof(int));
          memset(tmp_exo, 0, symbol_table.exo_nbr * sizeof(int));
          tmp_nb_exo = 0;
          while(Cur_IM)
623
            {
ferhat's avatar
ferhat committed
624
625
626
627
628
629
630
631
              i_1 = Index_Equ_IM[*count_Equ].index * symbol_table.exo_nbr;
              for(j=0;j<symbol_table.exo_nbr;j++)
                if(Cur_IM->IM[i_1 + j] && (!tmp_exo[j]))
                  {
                    tmp_exo[j] = 1;
                    tmp_nb_exo++;
                  }
              Cur_IM = Cur_IM->pNext;
632
            }
ferhat's avatar
ferhat committed
633
634
635
636
637
638
639
640
641
642
643
644
645
646
          ModelBlock->Block_List[*count_Block].nb_exo = tmp_nb_exo;
          ModelBlock->Block_List[*count_Block].Exogenous = (int*)malloc(tmp_nb_exo * sizeof(int));

          ModelBlock->Block_List[*count_Block].IM_lead_lag = (IM_compact*)malloc((Lead + Lag + 1) * sizeof(IM_compact));
          ModelBlock->Block_List[*count_Block].Nb_Lead_Lag_Endo = nb_lead_lag_endo;


          k = 0;
          for(j=0;j<symbol_table.exo_nbr;j++)
            if(tmp_exo[j])
              {
                ModelBlock->Block_List[*count_Block].Exogenous[k] = j;
                k++;
              }
647
648
649
650
          ls = l = 1;
          i1 = 0;
          for(int li = 0;li < Lead + Lag + 1;li++)
            {
ferhat's avatar
ferhat committed
651
              if(incidencematrix.Model_Max_Lag_Endo - Lag + li>=0)
652
                {
ferhat's avatar
ferhat committed
653
654
655
656
657
658
659
660
661
662
                  //cout << "ModelBlock->Block_List[" << *count_Block << "].IM_lead_lag[" << li << "].size=" << tmp_size[Model_Max_Lag_Endo - Lag + li] << "\n";
                  ModelBlock->Block_List[*count_Block].IM_lead_lag[li].size = tmp_size[incidencematrix.Model_Max_Lag_Endo - Lag + li];
                  ModelBlock->Block_List[*count_Block].IM_lead_lag[li].nb_endo = tmp_size[incidencematrix.Model_Max_Lag_Endo - Lag + li];
                  ModelBlock->Block_List[*count_Block].IM_lead_lag[li].u = (int*)malloc(tmp_size[incidencematrix.Model_Max_Lag_Endo - Lag + li] * sizeof(int));
                  ModelBlock->Block_List[*count_Block].IM_lead_lag[li].us = (int*)malloc(tmp_size[incidencematrix.Model_Max_Lag_Endo - Lag + li] * sizeof(int));
                  ModelBlock->Block_List[*count_Block].IM_lead_lag[li].Var = (int*)malloc(tmp_size[incidencematrix.Model_Max_Lag_Endo - Lag + li] * sizeof(int));
                  ModelBlock->Block_List[*count_Block].IM_lead_lag[li].Equ = (int*)malloc(tmp_size[incidencematrix.Model_Max_Lag_Endo - Lag + li] * sizeof(int));
                  ModelBlock->Block_List[*count_Block].IM_lead_lag[li].Var_Index = (int*)malloc(tmp_size[incidencematrix.Model_Max_Lag_Endo - Lag + li] * sizeof(int));
                  ModelBlock->Block_List[*count_Block].IM_lead_lag[li].Equ_Index = (int*)malloc(tmp_size[incidencematrix.Model_Max_Lag_Endo - Lag + li] * sizeof(int));

663
                  ModelBlock->Block_List[*count_Block].IM_lead_lag[li].u_init = l;
ferhat's avatar
ferhat committed
664
665
                  IM = incidencematrix.bGet_IM(li - Lag, eEndogenous);
                  if(IM)
666
                    {
ferhat's avatar
ferhat committed
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
                      if(IM[Index_Var_IM[*count_Equ].index + Index_Equ_IM[*count_Equ].index*symbol_table.endo_nbr] && nb_lead_lag_endo)
                        {
                          tmp_var[0] = i1;
                          i1++;
                        }
                      m = 0;
                      i_1 = Index_Equ_IM[*count_Equ].index * symbol_table.endo_nbr;
                      if(IM[Index_Var_IM[*count_Equ].index + i_1])
                        {
                          if(li == Lag)
                            {
                              ModelBlock->Block_List[*count_Block].IM_lead_lag[li].us[m] = ls;
                              ls++;
                            }
                          ModelBlock->Block_List[*count_Block].IM_lead_lag[li].u[m] = l;
                          ModelBlock->Block_List[*count_Block].IM_lead_lag[li].Equ[m] = 0;
                          ModelBlock->Block_List[*count_Block].IM_lead_lag[li].Var[m] = 0;
                          ModelBlock->Block_List[*count_Block].IM_lead_lag[li].Equ_Index[m] = Index_Equ_IM[*count_Equ].index;
                          ModelBlock->Block_List[*count_Block].IM_lead_lag[li].Var_Index[m] = Index_Var_IM[*count_Equ].index;
                          l++;
                          m++;
                        }
                      ModelBlock->Block_List[*count_Block].IM_lead_lag[li].u_finish = l - 1;
690
                    }
ferhat's avatar
ferhat committed
691
692
693
694
695
696
697
698
699
700
701
702
703
                }
              else
                ModelBlock->Block_List[*count_Block].IM_lead_lag[li].size = 0;
              if(incidencematrix.Model_Max_Lag_Exo - Lag + li>=0)
                {
                  ModelBlock->Block_List[*count_Block].IM_lead_lag[li].size_exo = tmp_size_exo[incidencematrix.Model_Max_Lag_Exo - Lag + li];
                  //cout << "ModelBlock->Block_List[" << *count_Block << "].IM_lead_lag[" << li << "].Exogenous= malloc(" << tmp_size_exo[Model_Max_Lag_Exo - Lag + li] << ")\n";
                  ModelBlock->Block_List[*count_Block].IM_lead_lag[li].Exogenous = (int*)malloc(tmp_size_exo[incidencematrix.Model_Max_Lag_Exo - Lag + li] * sizeof(int));
                  ModelBlock->Block_List[*count_Block].IM_lead_lag[li].Exogenous_Index = (int*)malloc(tmp_size_exo[incidencematrix.Model_Max_Lag_Exo - Lag + li] * sizeof(int));
                  ModelBlock->Block_List[*count_Block].IM_lead_lag[li].Equ_X = (int*)malloc(tmp_size_exo[incidencematrix.Model_Max_Lag_Exo - Lag + li] * sizeof(int));
                  ModelBlock->Block_List[*count_Block].IM_lead_lag[li].Equ_X_Index = (int*)malloc(tmp_size_exo[incidencematrix.Model_Max_Lag_Exo - Lag + li] * sizeof(int));
                  IM = incidencematrix.bGet_IM(li - Lag, eExogenous);
                  if(IM)
704
                    {
ferhat's avatar
ferhat committed
705
706
707
                      m = 0;
                      i_1 = Index_Equ_IM[*count_Equ].index * symbol_table.exo_nbr;
                      for(k = 0; k<tmp_nb_exo; k++)
708
                        {
ferhat's avatar
ferhat committed
709
710
711
712
713
714
715
716
                          if(IM[ModelBlock->Block_List[*count_Block].Exogenous[k]+i_1])
                            {
                              ModelBlock->Block_List[*count_Block].IM_lead_lag[li].Exogenous[m] = k;
                              ModelBlock->Block_List[*count_Block].IM_lead_lag[li].Exogenous_Index[m] = ModelBlock->Block_List[*count_Block].Exogenous[k];
                              ModelBlock->Block_List[*count_Block].IM_lead_lag[li].Equ_X[m] = 0;
                              ModelBlock->Block_List[*count_Block].IM_lead_lag[li].Equ_X_Index[m] = Index_Equ_IM[*count_Equ].index;
                              m++;
                            }
717
718
                        }
                    }
ferhat's avatar
ferhat committed
719
720
721
                }
              else
                ModelBlock->Block_List[*count_Block].IM_lead_lag[li].size_exo = 0;
722
723
724
725
            }
          (*count_Equ)++;
          (*count_Block)++;
          free(tmp_size);
ferhat's avatar
ferhat committed
726
          free(tmp_size_exo);
727
          free(tmp_endo);
ferhat's avatar
ferhat committed
728
          free(tmp_exo);
729
          free(tmp_var);
730
731
732
733
734
735
736
737
738
739
740
741
        }
    }
  else
    {
      ModelBlock->Block_List[*count_Block].is_linear=true;
      ModelBlock->Block_List[*count_Block].Size = size;
      ModelBlock->Block_List[*count_Block].Type = type;
      ModelBlock->Block_List[*count_Block].Temporary_terms=new temporary_terms_type ();
      ModelBlock->Block_List[*count_Block].Temporary_terms->clear();
      ModelBlock->Block_List[*count_Block].Simulation_Type = UNKNOWN;
      ModelBlock->Block_List[*count_Block].Equation = (int*)malloc(ModelBlock->Block_List[*count_Block].Size * sizeof(int));
      ModelBlock->Block_List[*count_Block].Variable = (int*)malloc(ModelBlock->Block_List[*count_Block].Size * sizeof(int));
ferhat's avatar
ferhat committed
742
      //ModelBlock->Block_List[*count_Block].Variable_Sorted = (int*)malloc(ModelBlock->Block_List[*count_Block].Size * sizeof(int));
743
744
745
746
      ModelBlock->Block_List[*count_Block].Own_Derivative = (int*)malloc(ModelBlock->Block_List[*count_Block].Size * sizeof(int));
      Lead = Lag = 0;
      first_count_equ = *count_Equ;
      tmp_var = (int*)malloc(size * sizeof(int));
ferhat's avatar
ferhat committed
747
748
749
750
751
752
      tmp_endo = (int*)malloc((incidencematrix.Model_Max_Lead + incidencematrix.Model_Max_Lag + 1) * sizeof(int));
      tmp_size = (int*)malloc((incidencematrix.Model_Max_Lead + incidencematrix.Model_Max_Lag + 1) * sizeof(int));
      tmp_size_exo = (int*)malloc((incidencematrix.Model_Max_Lead + incidencematrix.Model_Max_Lag + 1) * sizeof(int));
      memset(tmp_size_exo, 0, (incidencematrix.Model_Max_Lead + incidencematrix.Model_Max_Lag + 1)*sizeof(int));
      memset(tmp_size, 0, (incidencematrix.Model_Max_Lead + incidencematrix.Model_Max_Lag + 1)*sizeof(int));
      memset(tmp_endo, 0, (incidencematrix.Model_Max_Lead + incidencematrix.Model_Max_Lag + 1)*sizeof(int));
753
      nb_lead_lag_endo = 0;
ferhat's avatar
ferhat committed
754
755
      Lag_Endo = Lead_Endo = Lag_Exo = Lead_Exo = 0;

756
757
758
759
      for(i = 0;i < size;i++)
        {
          ModelBlock->Block_List[*count_Block].Equation[i] = Index_Equ_IM[*count_Equ].index;
          ModelBlock->Block_List[*count_Block].Variable[i] = Index_Var_IM[*count_Equ].index;
ferhat's avatar
ferhat committed
760
          Cur_IM = incidencematrix.Get_First(eEndogenous);
761
762
763
764
765
766
767
768
769
          i_1 = Index_Var_IM[*count_Equ].index;
          while(Cur_IM)
            {
              k = Cur_IM->lead_lag;
              OK = false;
              if(k >= 0)
                {
                  for(j = 0;j < size;j++)
                    {
ferhat's avatar
ferhat committed
770
                      if(Cur_IM->IM[i_1 + Index_Equ_IM[first_count_equ + j].index*symbol_table.endo_nbr])
771
                        {
ferhat's avatar
ferhat committed
772
                          tmp_size[incidencematrix.Model_Max_Lag_Endo + k]++;
773
774
                          if (!OK)
                            {
ferhat's avatar
ferhat committed
775
                              tmp_endo[incidencematrix.Model_Max_Lag + k]++;
776
777
778
779
780
781
782
783
784
785
786
787
788
                              nb_lead_lag_endo++;
                              OK = true;
                            }
                          if(k > Lead)
                            Lead = k;
                        }
                    }
                }
              else
                {
                  k = -k;
                  for(j = 0;j < size;j++)
                    {
ferhat's avatar
ferhat committed
789
                      if(Cur_IM->IM[i_1 + Index_Equ_IM[first_count_equ + j].index*symbol_table.endo_nbr])
790
                        {
ferhat's avatar
ferhat committed
791
                          tmp_size[incidencematrix.Model_Max_Lag_Endo - k]++;
792
793
                          if (!OK)
                            {
ferhat's avatar
ferhat committed
794
                              tmp_endo[incidencematrix.Model_Max_Lag - k]++;
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
                              nb_lead_lag_endo++;
                              OK = true;
                            }
                          if(k > Lag)
                            Lag = k;
                        }
                    }
                }
              Cur_IM = Cur_IM->pNext;
            }
          (*count_Equ)++;
        }
      if ((Lag > 0) && (Lead > 0))
        ModelBlock->Block_List[*count_Block].Simulation_Type = SOLVE_TWO_BOUNDARIES_COMPLETE;
      else if(size > 1)
        {
          if(Lead > 0)
            ModelBlock->Block_List[*count_Block].Simulation_Type = SOLVE_BACKWARD_COMPLETE;
          else
814
            ModelBlock->Block_List[*count_Block].Simulation_Type = SOLVE_FORWARD_COMPLETE;
815
816
817
818
819
820
        }
      else
        {
          if(Lead > 0)
            ModelBlock->Block_List[*count_Block].Simulation_Type = SOLVE_BACKWARD_SIMPLE;
          else
821
            ModelBlock->Block_List[*count_Block].Simulation_Type = SOLVE_FORWARD_SIMPLE;
822
        }
ferhat's avatar
ferhat committed
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


      Lag_Endo = Lag;
      Lead_Endo = Lead;
      tmp_exo = (int*)malloc(symbol_table.exo_nbr * sizeof(int));
      memset(tmp_exo, 0, symbol_table.exo_nbr * sizeof(int));
      tmp_nb_exo = 0;
      for(i = 0;i < size;i++)
        {
          Cur_IM = incidencematrix.Get_First(eExogenous);
          k = Cur_IM->lead_lag;
          while(Cur_IM)
            {
              i_1 = Index_Equ_IM[first_count_equ+i].index * symbol_table.exo_nbr;
              for(j=0;j<symbol_table.exo_nbr;j++)
                if(Cur_IM->IM[i_1 + j])
                  {
                    if(!tmp_exo[j])
                      {
                        tmp_exo[j] = 1;
                        tmp_nb_exo++;
                      }
                    if(k>0 && k>Lead_Exo)
                      Lead_Exo = k;
                    else if(k<0 && (-k)>Lag_Exo)
                      Lag_Exo = -k;
                    if(k>0 && k>Lead)
                      Lead = k;
                    else if(k<0 && (-k)>Lag)
                      Lag = -k;
                    tmp_size_exo[k+incidencematrix.Model_Max_Lag_Exo]++;
                  }
                Cur_IM = Cur_IM->pNext;
                if(Cur_IM)
                  k = Cur_IM->lead_lag;
            }
        }


      ModelBlock->Block_List[*count_Block].nb_exo = tmp_nb_exo;
      ModelBlock->Block_List[*count_Block].Exogenous = (int*)malloc(tmp_nb_exo * sizeof(int));
      k = 0;
      for(j=0;j<symbol_table.exo_nbr;j++)
        if(tmp_exo[j])
          {
            ModelBlock->Block_List[*count_Block].Exogenous[k] = j;
            k++;
          }

      ModelBlock->Block_List[*count_Block].nb_exo_det = 0;

874
875
      ModelBlock->Block_List[*count_Block].Max_Lag = Lag;
      ModelBlock->Block_List[*count_Block].Max_Lead = Lead;
ferhat's avatar
ferhat committed
876
877
878
879
      ModelBlock->Block_List[*count_Block].Max_Lag_Endo = Lag_Endo;
      ModelBlock->Block_List[*count_Block].Max_Lead_Endo = Lead_Endo;
      ModelBlock->Block_List[*count_Block].Max_Lag_Exo = Lag_Exo;
      ModelBlock->Block_List[*count_Block].Max_Lead_Exo = Lead_Exo;
880
881
882
883
884
885
      ModelBlock->Block_List[*count_Block].IM_lead_lag = (IM_compact*)malloc((Lead + Lag + 1) * sizeof(IM_compact));
      ls = l = size;
      i1 = 0;
      ModelBlock->Block_List[*count_Block].Nb_Lead_Lag_Endo = nb_lead_lag_endo;
      for(i = 0;i < Lead + Lag + 1;i++)
        {
ferhat's avatar
ferhat committed
886
          if(incidencematrix.Model_Max_Lag_Endo-Lag+i>=0)
887
            {
ferhat's avatar
ferhat committed
888
889
890
891
892
893
894
895
              ModelBlock->Block_List[*count_Block].IM_lead_lag[i].size = tmp_size[incidencematrix.Model_Max_Lag_Endo - Lag + i];
              ModelBlock->Block_List[*count_Block].IM_lead_lag[i].nb_endo = tmp_endo[incidencematrix.Model_Max_Lag_Endo - Lag + i];
              ModelBlock->Block_List[*count_Block].IM_lead_lag[i].u = (int*)malloc(tmp_size[incidencematrix.Model_Max_Lag_Endo - Lag + i] * sizeof(int));
              ModelBlock->Block_List[*count_Block].IM_lead_lag[i].us = (int*)malloc(tmp_size[incidencematrix.Model_Max_Lag_Endo - Lag + i] * sizeof(int));
              ModelBlock->Block_List[*count_Block].IM_lead_lag[i].Var = (int*)malloc(tmp_size[incidencematrix.Model_Max_Lag_Endo - Lag + i] * sizeof(int));
              ModelBlock->Block_List[*count_Block].IM_lead_lag[i].Equ = (int*)malloc(tmp_size[incidencematrix.Model_Max_Lag_Endo - Lag + i] * sizeof(int));
              ModelBlock->Block_List[*count_Block].IM_lead_lag[i].Var_Index = (int*)malloc(tmp_size[incidencematrix.Model_Max_Lag_Endo - Lag + i] * sizeof(int));
              ModelBlock->Block_List[*count_Block].IM_lead_lag[i].Equ_Index = (int*)malloc(tmp_size[incidencematrix.Model_Max_Lag_Endo - Lag + i] * sizeof(int));
896
897
            }
          else
ferhat's avatar
ferhat committed
898
899
            ModelBlock->Block_List[*count_Block].IM_lead_lag[i].size = 0;
          if(incidencematrix.Model_Max_Lag_Exo-Lag+i>=0)
900
            {
ferhat's avatar
ferhat committed
901
902
903
904
905
              ModelBlock->Block_List[*count_Block].IM_lead_lag[i].size_exo = tmp_size_exo[incidencematrix.Model_Max_Lag_Exo - Lag + i];
              ModelBlock->Block_List[*count_Block].IM_lead_lag[i].Exogenous = (int*)malloc(tmp_size_exo[incidencematrix.Model_Max_Lag_Exo - Lag + i] * sizeof(int));
              ModelBlock->Block_List[*count_Block].IM_lead_lag[i].Exogenous_Index = (int*)malloc(tmp_size_exo[incidencematrix.Model_Max_Lag_Exo - Lag + i] * sizeof(int));
              ModelBlock->Block_List[*count_Block].IM_lead_lag[i].Equ_X = (int*)malloc(tmp_size_exo[incidencematrix.Model_Max_Lag_Exo - Lag + i] * sizeof(int));
              ModelBlock->Block_List[*count_Block].IM_lead_lag[i].Equ_X_Index = (int*)malloc(tmp_size_exo[incidencematrix.Model_Max_Lag_Exo - Lag + i] * sizeof(int));
906
            }
ferhat's avatar
ferhat committed
907
908
909
910
911
          else
            ModelBlock->Block_List[*count_Block].IM_lead_lag[i].size_exo = 0;
          ModelBlock->Block_List[*count_Block].IM_lead_lag[i].u_init = l;
          IM = incidencematrix.bGet_IM(i - Lag, eEndogenous);
          if(IM)
912
            {
ferhat's avatar
ferhat committed
913
              for(j = first_count_equ;j < size + first_count_equ;j++)
914
                {
ferhat's avatar
ferhat committed
915
916
917
918
919
920
921
922
923
924
                  i_1 = Index_Var_IM[j].index;
                  m = 0;
                  for(k = first_count_equ;k < size + first_count_equ;k++)
                    if(IM[i_1 + Index_Equ_IM[k].index*symbol_table.endo_nbr])
                      m++;
                  if(m > 0)
                    {
                      tmp_var[j - first_count_equ] = i1;
                      i1++;
                    }
925
                }
ferhat's avatar
ferhat committed
926
927
928
929
930
931
              m = 0;
              for(j = first_count_equ;j < size + first_count_equ;j++)
                {
                  i_1 = Index_Equ_IM[j].index * symbol_table.endo_nbr;
                  for(k = first_count_equ;k < size + first_count_equ;k++)
                    if(IM[Index_Var_IM[k].index + i_1])
932
                      {
ferhat's avatar
ferhat committed
933
                        if(i == Lag)
934
                          {
ferhat's avatar
ferhat committed
935
936
                            ModelBlock->Block_List[*count_Block].IM_lead_lag[i].us[m] = ls;
                            ls++;
937
                          }
ferhat's avatar
ferhat committed
938
939
940
941
942
943
944
                        ModelBlock->Block_List[*count_Block].IM_lead_lag[i].u[m] = l;
                        ModelBlock->Block_List[*count_Block].IM_lead_lag[i].Equ[m] = j - first_count_equ;
                        ModelBlock->Block_List[*count_Block].IM_lead_lag[i].Var[m] = k - first_count_equ;
                        ModelBlock->Block_List[*count_Block].IM_lead_lag[i].Equ_Index[m] = Index_Equ_IM[j].index;
                        ModelBlock->Block_List[*count_Block].IM_lead_lag[i].Var_Index[m] = Index_Var_IM[k].index;
                        l++;
                        m++;
945
                      }
ferhat's avatar
ferhat committed
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
                }
              ModelBlock->Block_List[*count_Block].IM_lead_lag[i].u_finish = l - 1;
            }
          IM = incidencematrix.bGet_IM(i - Lag, eExogenous);
          if(IM)
            {
              m = 0;
              for(j = first_count_equ;j < size + first_count_equ;j++)
                {
                  i_1 = Index_Equ_IM[j].index * symbol_table.exo_nbr;
                  for(k = 0; k<tmp_nb_exo; k++)
                    {
                      if(IM[ModelBlock->Block_List[*count_Block].Exogenous[k]+i_1])
                        {
                          ModelBlock->Block_List[*count_Block].IM_lead_lag[i].Exogenous[m] = k;
                          ModelBlock->Block_List[*count_Block].IM_lead_lag[i].Exogenous_Index[m] = ModelBlock->Block_List[*count_Block].Exogenous[k];
                          ModelBlock->Block_List[*count_Block].IM_lead_lag[i].Equ_X[m] = j - first_count_equ;
                          ModelBlock->Block_List[*count_Block].IM_lead_lag[i].Equ_X_Index[m] = Index_Equ_IM[j].index;
                          m++;
                        }
                    }
                }
968
969
970
971
            }
        }
      (*count_Block)++;
      free(tmp_size);
ferhat's avatar
ferhat committed
972
      free(tmp_size_exo);
973
      free(tmp_endo);
ferhat's avatar
ferhat committed
974
      free(tmp_exo);
975
976
977
978
979
980
      free(tmp_var);
    }
}


void
981
BlockTriangular::Free_Block(Model_Block* ModelBlock) const
982
983
984
985
986
987
988
989
990
{
  int blk, i;
  for(blk = 0;blk < ModelBlock->Size;blk++)
    {

      if ((ModelBlock->Block_List[blk].Type == PROLOGUE) || (ModelBlock->Block_List[blk].Type == EPILOGUE))
        {
          free(ModelBlock->Block_List[blk].Equation);
          free(ModelBlock->Block_List[blk].Variable);
ferhat's avatar
ferhat committed
991
          free(ModelBlock->Block_List[blk].Exogenous);
992
993
994
          free(ModelBlock->Block_List[blk].Own_Derivative);
          for(i = 0;i < ModelBlock->Block_List[blk].Max_Lag + ModelBlock->Block_List[blk].Max_Lead + 1;i++)
            {
ferhat's avatar
ferhat committed
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
              if(ModelBlock->Block_List[blk].IM_lead_lag[i].size)
                {
                  free(ModelBlock->Block_List[blk].IM_lead_lag[i].u);
                  free(ModelBlock->Block_List[blk].IM_lead_lag[i].us);
                  free(ModelBlock->Block_List[blk].IM_lead_lag[i].Var);
                  free(ModelBlock->Block_List[blk].IM_lead_lag[i].Equ);
                  free(ModelBlock->Block_List[blk].IM_lead_lag[i].Var_Index);
                  free(ModelBlock->Block_List[blk].IM_lead_lag[i].Equ_Index);
                }
              if(ModelBlock->Block_List[blk].IM_lead_lag[i].size_exo)
                {
                  free(ModelBlock->Block_List[blk].IM_lead_lag[i].Exogenous);
                  free(ModelBlock->Block_List[blk].IM_lead_lag[i].Exogenous_Index);
                  free(ModelBlock->Block_List[blk].IM_lead_lag[i].Equ_X_Index);
                  free(ModelBlock->Block_List[blk].IM_lead_lag[i].Equ_X);
                }
1011
1012
1013
            }
          free(ModelBlock->Block_List[blk].IM_lead_lag);
          delete(ModelBlock->Block_List[blk].Temporary_terms);
1014
1015
1016
1017
1018
        }
      else
        {
          free(ModelBlock->Block_List[blk].Equation);
          free(ModelBlock->Block_List[blk].Variable);
ferhat's avatar
ferhat committed
1019
          free(ModelBlock->Block_List[blk].Exogenous);
1020
          free(ModelBlock->Block_List[blk].Own_Derivative);
1021
1022
          for(i = 0;i < ModelBlock->Block_List[blk].Max_Lag + ModelBlock->Block_List[blk].Max_Lead + 1;i++)
            {
ferhat's avatar
ferhat committed
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
              if(incidencematrix.Model_Max_Lag_Endo-ModelBlock->Block_List[blk].Max_Lag+i>=0 && ModelBlock->Block_List[blk].IM_lead_lag[i].size)
                {
                  free(ModelBlock->Block_List[blk].IM_lead_lag[i].u);
                  free(ModelBlock->Block_List[blk].IM_lead_lag[i].us);
                  free(ModelBlock->Block_List[blk].IM_lead_lag[i].Equ);
                  free(ModelBlock->Block_List[blk].IM_lead_lag[i].Var);
                  free(ModelBlock->Block_List[blk].IM_lead_lag[i].Equ_Index);
                  free(ModelBlock->Block_List[blk].IM_lead_lag[i].Var_Index);
                }
              if(incidencematrix.Model_Max_Lag_Exo-ModelBlock->Block_List[blk].Max_Lag+i>=0 && ModelBlock->Block_List[blk].IM_lead_lag[i].size_exo)
                {
                  free(ModelBlock->Block_List[blk].IM_lead_lag[i].Exogenous);
                  free(ModelBlock->Block_List[blk].IM_lead_lag[i].Exogenous_Index);
                  free(ModelBlock->Block_List[blk].IM_lead_lag[i].Equ_X_Index);
                  free(ModelBlock->Block_List[blk].IM_lead_lag[i].Equ_X);
                }
1039
1040
            }
          free(ModelBlock->Block_List[blk].IM_lead_lag);
1041
          delete(ModelBlock->Block_List[blk].Temporary_terms);
1042
1043
1044
1045
        }
    }
  free(ModelBlock->Block_List);
  free(ModelBlock);
1046
1047
  free(Index_Equ_IM);
  free(Index_Var_IM);
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
}

//------------------------------------------------------------------------------
// Normalize each equation of the model (endgenous_i = f_i(endogenous_1, ..., endogenous_n) - in order to apply strong connex components search algorithm -
// and find the optimal blocks triangular decomposition
bool
BlockTriangular::Normalize_and_BlockDecompose(bool* IM, Model_Block* ModelBlock, int n, int* prologue, int* epilogue, simple* Index_Var_IM, simple* Index_Equ_IM, bool Do_Normalization, bool mixing, bool* IM_0, jacob_map j_m )
{
  int i, j, Nb_TotalBlocks, Nb_RecursBlocks;
  int count_Block, count_Equ;
  block_result_t* res;
  Equation_set* Equation_gr = (Equation_set*) malloc(sizeof(Equation_set));
  bool* SIM0, *SIM00;
1061
1062
1063
1064
  SIM0 = (bool*)malloc(n * n * sizeof(bool));
  memcpy(SIM0,IM_0,n*n*sizeof(bool));
  Prologue_Epilogue(IM, prologue, epilogue, n, Index_Var_IM, Index_Equ_IM, SIM0);
  free(SIM0);
1065
1066
1067
  if(bt_verbose)
    {
      cout << "prologue : " << *prologue << " epilogue : " << *epilogue << "\n";
1068
      cout << "IM_0\n";
ferhat's avatar
ferhat committed
1069
      incidencematrix.Print_SIM(IM_0, eEndogenous);
1070
      cout << "IM\n";
ferhat's avatar
ferhat committed
1071
      incidencematrix.Print_SIM(IM, eEndogenous);
1072
1073
1074
      for(i = 0;i < n;i++)
        cout << "Index_Var_IM[" << i << "]=" << Index_Var_IM[i].index << " Index_Equ_IM[" << i << "]=" << Index_Equ_IM[i].index << "\n";
    }
1075
  if(*prologue+*epilogue<n)
1076
    {
1077
      if(Do_Normalization)
1078
        {
1079
1080
          cout << "Normalizing the model ...\n";
          if(mixing)
1081
            {
1082
1083
              double* max_val=(double*)malloc(n*sizeof(double));
              memset(max_val,0,n*sizeof(double));
1084
              for( map< pair< int, int >, double >::iterator iter = j_m.begin(); iter != j_m.end(); iter++ )
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
                {
                  if(fabs(iter->second)>max_val[iter->first.first])
                    max_val[iter->first.first]=fabs(iter->second);
                }
              for( map< pair< int, int >, double >::iterator iter = j_m.begin(); iter != j_m.end(); iter++ )
                  iter->second/=max_val[iter->first.first];
              free(max_val);
              bool OK=false;
              double bi=0.99999999;
              int suppressed=0;
              while(!OK && bi>1e-14)
                {
                  int suppress=0;
                  SIM0 = (bool*)malloc(n * n * sizeof(bool));
                  memset(SIM0,0,n*n*sizeof(bool));
                  SIM00 = (bool*)malloc(n * n * sizeof(bool));
                  memset(SIM00,0,n*n*sizeof(bool));
                  for( map< pair< int, int >, double >::iterator iter = j_m.begin(); iter != j_m.end(); iter++ )
                    {
                      if(fabs(iter->second)>bi)
                        {
                          SIM0[iter->first.first*n+iter->first.second]=1;
                          if(!IM_0[iter->first.first*n+iter->first.second])
                            {
                              cout << "Error nothing at IM_0[" << iter->first.first << ", " << iter->first.second << "]=" << IM_0[iter->first.first*n+iter->first.second] << "\n";
                            }
                        }
                      else
                        suppress++;
                    }
                  for(i = 0;i < n;i++)
                    for(j = 0;j < n;j++)
1117
                      {
1118
                        SIM00[i*n + j] = SIM0[Index_Equ_IM[i].index * n + Index_Var_IM[j].index];
1119
                      }
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
                  free(SIM0);
                  if(suppress!=suppressed)
                    {
                      OK=normalization.Normalize(n, *prologue, *epilogue, SIM00, Index_Equ_IM, Equation_gr, 1, IM);
                      if(!OK)
                        normalization.Free_Equation(n-*prologue-*epilogue,Equation_gr);
                    }
                  suppressed=suppress;
                  if(!OK)
                    bi/=1.07;
                  if(bi>1e-14)
                    free(SIM00);
                }
1133
              if(!OK)
1134
1135
1136
1137
                {
                  normalization.Set_fp_verbose(true);
                  OK=normalization.Normalize(n, *prologue, *epilogue, SIM00, Index_Equ_IM, Equation_gr, 1, IM);
                  cout << "Error\n";
1138
                  exit(EXIT_FAILURE);
1139
                }
1140
            }
1141
1142
          else
            normalization.Normalize(n, *prologue, *epilogue, IM, Index_Equ_IM, Equation_gr, 0, 0);
1143
1144
        }
      else
1145
        normalization.Gr_to_IM_basic(n, *prologue, *epilogue, IM, Equation_gr, false);
1146
1147
    }
  cout << "Finding the optimal block decomposition of the model ...\n";
ferhat's avatar
ferhat committed
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
  if(*prologue+*epilogue<n)
    {
      if(bt_verbose)
        blocks.Print_Equation_gr(Equation_gr);
      res = blocks.sc(Equation_gr);
      normalization.Free_Equation(n-*prologue-*epilogue,Equation_gr);
      if(bt_verbose)
        blocks.block_result_print(res);
    }
  else
    {
      res = (block_result_t*)malloc(sizeof(*res));
      res->n_sets=0;
    }
1162
  free(Equation_gr);
1163
1164
1165
1166
1167
  if ((*prologue) || (*epilogue))
    j = 1;
  else
    j = 0;
  for(i = 0;i < res->n_sets;i++)
1168
1169
1170
1171
    {
      if ((res->sets_f[i] - res->sets_s[i] + 1) > j)
        j = res->sets_f[i] - res->sets_s[i] + 1;
    }
1172
1173
1174
1175
1176
1177
1178
1179
  Nb_RecursBlocks = *prologue + *epilogue;
  Nb_TotalBlocks = res->n_sets + Nb_RecursBlocks;
  cout << Nb_TotalBlocks << " block(s) found:\n";
  cout << "  " << Nb_RecursBlocks << " recursive block(s) and " << res->n_sets << " simultaneous block(s). \n";
  cout << "  the largest simultaneous block has " << j << " equation(s). \n";
  ModelBlock->Size = Nb_TotalBlocks;
  ModelBlock->Periods = periods;
  ModelBlock->Block_List = (Block*)malloc(sizeof(ModelBlock->Block_List[0]) * Nb_TotalBlocks);
ferhat's avatar
ferhat committed
1180
  blocks.block_result_to_IM(res, IM, *prologue, symbol_table.endo_nbr, Index_Equ_IM, Index_Var_IM);
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
  count_Equ = count_Block = 0;
  if (*prologue)
    Allocate_Block(*prologue, &count_Equ, &count_Block, PROLOGUE, ModelBlock);
  for(j = 0;j < res->n_sets;j++)
    {
      if(res->sets_f[res->ordered[j]] == res->sets_s[res->ordered[j]])
        Allocate_Block(res->sets_f[res->ordered[j]] - res->sets_s[res->ordered[j]] + 1, &count_Equ, &count_Block, PROLOGUE, ModelBlock);
      else
        Allocate_Block(res->sets_f[res->ordered[j]] - res->sets_s[res->ordered[j]] + 1, &count_Equ, &count_Block, SIMULTANS, ModelBlock);
    }
  if (*epilogue)
    Allocate_Block(*epilogue, &count_Equ, &count_Block, EPILOGUE, ModelBlock);
ferhat's avatar
ferhat committed
1193
1194
1195
1196
  if(res->n_sets)
    blocks.block_result_free(res);
  else
    free(res);
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
  return 0;
}

//------------------------------------------------------------------------------
// normalize each equation of the dynamic model
// and find the optimal block triangular decomposition of the static model
void
BlockTriangular::Normalize_and_BlockDecompose_Static_0_Model(const jacob_map &j_m)
{
  bool* SIM, *SIM_0;
  List_IM* Cur_IM;
  int i;
  //First create a static model incidence matrix
ferhat's avatar
ferhat committed
1210
1211
  SIM = (bool*)malloc(symbol_table.endo_nbr * symbol_table.endo_nbr * sizeof(*SIM));
  for(i = 0;i < symbol_table.endo_nbr*symbol_table.endo_nbr;i++)
1212
1213
    {
      SIM[i] = 0;
ferhat's avatar
ferhat committed
1214
      Cur_IM = incidencematrix.Get_First(eEndogenous);
1215
1216
1217
1218
1219
1220
1221
1222
1223
      while(Cur_IM)
        {
          SIM[i] = (SIM[i]) || (Cur_IM->IM[i]);
          Cur_IM = Cur_IM->pNext;
        }
    }
  if(bt_verbose)
    {
      cout << "incidence matrix for the static model (unsorted) \n";
ferhat's avatar
ferhat committed
1224
      incidencematrix.Print_SIM(SIM, eEndogenous);
1225
    }
ferhat's avatar
ferhat committed
1226
1227
  Index_Equ_IM = (simple*)malloc(symbol_table.endo_nbr * sizeof(*Index_Equ_IM));
  for(i = 0;i < symbol_table.endo_nbr;i++)
1228
1229
1230
    {
      Index_Equ_IM[i].index = i;
    }
ferhat's avatar
ferhat committed
1231
1232
  Index_Var_IM = (simple*)malloc(symbol_table.endo_nbr * sizeof(*Index_Var_IM));
  for(i = 0;i < symbol_table.endo_nbr;i++)
1233
1234
1235
1236
1237
1238
    {
      Index_Var_IM[i].index = i;
    }
  if(ModelBlock != NULL)
    Free_Block(ModelBlock);
  ModelBlock = (Model_Block*)malloc(sizeof(*ModelBlock));
ferhat's avatar
ferhat committed
1239
1240
1241
  Cur_IM = incidencematrix.Get_IM(0, eEndogenous);
  SIM_0 = (bool*)malloc(symbol_table.endo_nbr * symbol_table.endo_nbr * sizeof(*SIM_0));
  for(i = 0;i < symbol_table.endo_nbr*symbol_table.endo_nbr;i++)
1242
    SIM_0[i] = Cur_IM->IM[i];
ferhat's avatar
ferhat committed
1243
  Normalize_and_BlockDecompose(SIM, ModelBlock, symbol_table.endo_nbr, &prologue, &epilogue, Index_Var_IM, Index_Equ_IM, 1, 1, SIM_0, j_m);
1244
1245
  free(SIM_0);
  free(SIM);
1246
}