File: SimilarityDecomp.cc

package info (click to toggle)
dynare 4.6.3-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 74,896 kB
  • sloc: cpp: 98,057; ansic: 28,929; pascal: 13,844; sh: 5,947; objc: 4,236; yacc: 4,215; makefile: 2,583; lex: 1,534; fortran: 877; python: 647; ruby: 291; lisp: 152; xml: 22
file content (176 lines) | stat: -rw-r--r-- 4,847 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
89
90
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
 * Copyright © 2004-2011 Ondra Kamenik
 * Copyright © 2019 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/>.
 */

#include "SimilarityDecomp.hh"
#include "SchurDecomp.hh"
#include "SchurDecompEig.hh"
#include "SylvException.hh"

#include <dynlapack.h>

#include <cmath>

SimilarityDecomp::SimilarityDecomp(const ConstVector &d, int d_size, double log10norm)
{
  SchurDecomp sd(SqSylvMatrix(Vector{d}, d_size));
  q = std::make_unique<SqSylvMatrix>(sd.getQ());
  b = std::make_unique<BlockDiagonal>(sd.getT());
  invq = std::make_unique<SqSylvMatrix>(d_size);
  invq->setUnit();
  invq->multLeftTrans(sd.getQ());
  double norm = pow(10.0, log10norm);
  diagonalize(norm);
}

void
SimilarityDecomp::getXDim(diag_iter start, diag_iter end,
                          int &rows, int &cols) const
{
  int si = start->getIndex();
  int ei = end->getIndex();
  cols = b->nrows() - ei;
  rows = ei - si;
}

/* Find solution of X for diagonal block given by start(incl.) and
   end(excl.). If the solution cannot be found, or it is greater than
   norm, X is not changed and flase is returned.
*/
bool
SimilarityDecomp::solveX(diag_iter start, diag_iter end,
                         GeneralMatrix &X, double norm) const
{
  int si = start->getIndex();
  int ei = end->getIndex();

  SqSylvMatrix A(const_cast<const BlockDiagonal &>(*b), si, si, X.nrows());
  SqSylvMatrix B(const_cast<const BlockDiagonal &>(*b), ei, ei, X.ncols());
  GeneralMatrix C(const_cast<const BlockDiagonal &>(*b), si, ei, X.nrows(), X.ncols());

  lapack_int isgn = -1;
  lapack_int m = A.nrows();
  lapack_int n = B.nrows();
  lapack_int lda = A.getLD(), ldb = B.getLD();
  double scale;
  lapack_int info;
  dtrsyl("N", "N", &isgn, &m, &n, A.base(), &lda, B.base(), &ldb,
         C.base(), &m, &scale, &info);
  if (info < -1)
    throw SYLV_MES_EXCEPTION("Wrong parameter to LAPACK dtrsyl.");

  if (info == 1 || scale < 1)
    return false;
  if (C.getData().getMax() > norm)
    return false;

  X = C;
  return true;
}

/*                         ⎛I −X⎞     ⎛I X⎞
  Multiply Q and invQ with ⎝0  I⎠ and ⎝0 I⎠ respectively. This also sets X=−X. */
void
SimilarityDecomp::updateTransform(diag_iter start, diag_iter end,
                                  GeneralMatrix &X)
{
  int si = start->getIndex();
  int ei = end->getIndex();

  SqSylvMatrix iX(q->nrows());
  iX.setUnit();
  iX.place(X, si, ei);
  invq->GeneralMatrix::multLeft(iX);

  iX.setUnit();
  X.mult(-1.0);
  iX.place(X, si, ei);
  q->multRight(iX);
}

void
SimilarityDecomp::bringGuiltyBlock(diag_iter start, diag_iter &end)
{
  double av = b->getAverageDiagSize(start, end);
  diag_iter guilty = b->findClosestDiagBlock(end, b->diag_end(), av);
  SchurDecompEig sd(*b); // works on b including diagonal structure
  end = sd.bubbleEigen(guilty, end); // iterators are valid
  ++end;
  q->multRight(sd.getQ());
  invq->multLeftTrans(sd.getQ());
}

void
SimilarityDecomp::diagonalize(double norm)
{
  diag_iter start = b->diag_begin();
  diag_iter end = start;
  ++end;

  while (end != b->diag_end())
    {
      int xrows;
      int xcols;
      getXDim(start, end, xrows, xcols);
      GeneralMatrix X(xrows, xcols);
      if (solveX(start, end, X, norm))
        {
          updateTransform(start, end, X);
          b->setZeroBlockEdge(end);
          start = end;
          ++end;
        }
      else
        bringGuiltyBlock(start, end); // moves with end
    }
}

void
SimilarityDecomp::check(SylvParams &pars, const GeneralMatrix &m) const
{
  // M − Q·B·Q⁻¹
  SqSylvMatrix c(getQ() * getB());
  c.multRight(getInvQ());
  c.add(-1.0, m);
  pars.f_err1 = c.getNorm1();
  pars.f_errI = c.getNormInf();

  // I − Q·Q⁻¹
  c.setUnit();
  c.mult(-1);
  c.multAndAdd(getQ(), getInvQ());
  pars.viv_err1 = c.getNorm1();
  pars.viv_errI = c.getNormInf();

  // I − Q⁻¹·Q
  c.setUnit();
  c.mult(-1);
  c.multAndAdd(getInvQ(), getQ());
  pars.ivv_err1 = c.getNorm1();
  pars.ivv_errI = c.getNormInf();
}

void
SimilarityDecomp::infoToPars(SylvParams &pars) const
{
  pars.f_blocks = getB().getNumBlocks();
  pars.f_largest = getB().getLargestBlock();
  pars.f_zeros = getB().getNumZeros();
  pars.f_offdiag = getB().getNumOffdiagonal();
}