2019-06-19 14:34:30 +02:00
|
|
|
|
/*
|
|
|
|
|
* 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
|
2021-06-09 17:33:48 +02:00
|
|
|
|
* along with Dynare. If not, see <https://www.gnu.org/licenses/>.
|
2019-06-19 14:34:30 +02:00
|
|
|
|
*/
|
2019-01-08 17:12:05 +01:00
|
|
|
|
|
|
|
|
|
#include "GeneralSylvester.hh"
|
|
|
|
|
#include "SchurDecomp.hh"
|
|
|
|
|
#include "SylvException.hh"
|
|
|
|
|
#include "TriangularSylvester.hh"
|
|
|
|
|
#include "IterativeSylvester.hh"
|
2019-02-19 14:50:00 +01:00
|
|
|
|
#include "int_power.hh"
|
2019-01-08 17:12:05 +01:00
|
|
|
|
|
|
|
|
|
#include <ctime>
|
|
|
|
|
|
|
|
|
|
GeneralSylvester::GeneralSylvester(int ord, int n, int m, int zero_cols,
|
Dynare++ / sylvester equation solver: refactor Vector and ConstVector classes
- these classes now encapsulate a std::shared_ptr<{const, }double>, so that
they do not perform memory management, and several {Const,}Vector instances
can transparently share the same underlying data
- make converting constructor from ConstVector to Vector explicit, since that
entails memory allocation (but the reverse conversion is almost costless, so
keep it implicit); do the same for GeneralMatrix/ConstGeneralMatrix,
TwoDMatrix/ConstTwoDMatrix
- remove the constructors that were extracting a row/column from a matrix, and
replace them by getRow() and getCol() methods on {Const,}GeneralMatrix
- rename and change the API of the complex version Vector::add(), so that it is
explicit that it deals with complex numbers
- add constructors that take a MATLAB mxArray
2019-01-22 16:07:44 +01:00
|
|
|
|
const ConstVector &da, const ConstVector &db,
|
|
|
|
|
const ConstVector &dc, const ConstVector &dd,
|
2019-01-08 17:12:05 +01:00
|
|
|
|
const SylvParams &ps)
|
|
|
|
|
: pars(ps),
|
2019-01-25 15:29:14 +01:00
|
|
|
|
order(ord), a(Vector{da}, n),
|
2019-01-24 15:22:36 +01:00
|
|
|
|
b(Vector{db}, n, n-zero_cols), c(Vector{dc}, m), d(Vector{dd}, n, power(m, order)),
|
2019-01-08 17:12:05 +01:00
|
|
|
|
solved(false)
|
|
|
|
|
{
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GeneralSylvester::GeneralSylvester(int ord, int n, int m, int zero_cols,
|
Dynare++ / sylvester equation solver: refactor Vector and ConstVector classes
- these classes now encapsulate a std::shared_ptr<{const, }double>, so that
they do not perform memory management, and several {Const,}Vector instances
can transparently share the same underlying data
- make converting constructor from ConstVector to Vector explicit, since that
entails memory allocation (but the reverse conversion is almost costless, so
keep it implicit); do the same for GeneralMatrix/ConstGeneralMatrix,
TwoDMatrix/ConstTwoDMatrix
- remove the constructors that were extracting a row/column from a matrix, and
replace them by getRow() and getCol() methods on {Const,}GeneralMatrix
- rename and change the API of the complex version Vector::add(), so that it is
explicit that it deals with complex numbers
- add constructors that take a MATLAB mxArray
2019-01-22 16:07:44 +01:00
|
|
|
|
const ConstVector &da, const ConstVector &db,
|
|
|
|
|
const ConstVector &dc, Vector &dd,
|
2019-01-08 17:12:05 +01:00
|
|
|
|
const SylvParams &ps)
|
|
|
|
|
: pars(ps),
|
2019-01-25 15:29:14 +01:00
|
|
|
|
order(ord), a(Vector{da}, n),
|
2019-01-24 15:22:36 +01:00
|
|
|
|
b(Vector{db}, n, n-zero_cols), c(Vector{dc}, m), d(dd, n, power(m, order)),
|
2019-01-08 17:12:05 +01:00
|
|
|
|
solved(false)
|
|
|
|
|
{
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GeneralSylvester::GeneralSylvester(int ord, int n, int m, int zero_cols,
|
Dynare++ / sylvester equation solver: refactor Vector and ConstVector classes
- these classes now encapsulate a std::shared_ptr<{const, }double>, so that
they do not perform memory management, and several {Const,}Vector instances
can transparently share the same underlying data
- make converting constructor from ConstVector to Vector explicit, since that
entails memory allocation (but the reverse conversion is almost costless, so
keep it implicit); do the same for GeneralMatrix/ConstGeneralMatrix,
TwoDMatrix/ConstTwoDMatrix
- remove the constructors that were extracting a row/column from a matrix, and
replace them by getRow() and getCol() methods on {Const,}GeneralMatrix
- rename and change the API of the complex version Vector::add(), so that it is
explicit that it deals with complex numbers
- add constructors that take a MATLAB mxArray
2019-01-22 16:07:44 +01:00
|
|
|
|
const ConstVector &da, const ConstVector &db,
|
|
|
|
|
const ConstVector &dc, const ConstVector &dd,
|
2019-01-08 17:12:05 +01:00
|
|
|
|
bool alloc_for_check)
|
|
|
|
|
: pars(alloc_for_check),
|
2019-01-25 15:29:14 +01:00
|
|
|
|
order(ord), a(Vector{da}, n),
|
2019-01-24 15:22:36 +01:00
|
|
|
|
b(Vector{db}, n, n-zero_cols), c(Vector{dc}, m), d(Vector{dd}, n, power(m, order)),
|
2019-01-08 17:12:05 +01:00
|
|
|
|
solved(false)
|
|
|
|
|
{
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GeneralSylvester::GeneralSylvester(int ord, int n, int m, int zero_cols,
|
Dynare++ / sylvester equation solver: refactor Vector and ConstVector classes
- these classes now encapsulate a std::shared_ptr<{const, }double>, so that
they do not perform memory management, and several {Const,}Vector instances
can transparently share the same underlying data
- make converting constructor from ConstVector to Vector explicit, since that
entails memory allocation (but the reverse conversion is almost costless, so
keep it implicit); do the same for GeneralMatrix/ConstGeneralMatrix,
TwoDMatrix/ConstTwoDMatrix
- remove the constructors that were extracting a row/column from a matrix, and
replace them by getRow() and getCol() methods on {Const,}GeneralMatrix
- rename and change the API of the complex version Vector::add(), so that it is
explicit that it deals with complex numbers
- add constructors that take a MATLAB mxArray
2019-01-22 16:07:44 +01:00
|
|
|
|
const ConstVector &da, const ConstVector &db,
|
|
|
|
|
const ConstVector &dc, Vector &dd,
|
2019-01-08 17:12:05 +01:00
|
|
|
|
bool alloc_for_check)
|
|
|
|
|
: pars(alloc_for_check),
|
2019-01-25 15:29:14 +01:00
|
|
|
|
order(ord), a(Vector{da}, n),
|
2019-01-24 15:22:36 +01:00
|
|
|
|
b(Vector{db}, n, n-zero_cols), c(Vector{dc}, m), d(dd, n, power(m, order)),
|
2019-01-08 17:12:05 +01:00
|
|
|
|
solved(false)
|
|
|
|
|
{
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
GeneralSylvester::init()
|
|
|
|
|
{
|
|
|
|
|
GeneralMatrix ainvb(b);
|
|
|
|
|
double rcond1;
|
|
|
|
|
double rcondinf;
|
|
|
|
|
a.multInvLeft2(ainvb, d, rcond1, rcondinf);
|
|
|
|
|
pars.rcondA1 = rcond1;
|
|
|
|
|
pars.rcondAI = rcondinf;
|
2019-01-15 18:55:09 +01:00
|
|
|
|
bdecomp = std::make_unique<SchurDecompZero>(ainvb);
|
2019-04-16 12:40:50 +02:00
|
|
|
|
cdecomp = std::make_unique<SimilarityDecomp>(c.getData(), c.nrows(), *(pars.bs_norm));
|
2019-01-08 17:12:05 +01:00
|
|
|
|
cdecomp->check(pars, c);
|
|
|
|
|
cdecomp->infoToPars(pars);
|
2019-01-25 15:27:20 +01:00
|
|
|
|
if (*(pars.method) == SylvParams::solve_method::recurse)
|
2019-01-15 18:55:09 +01:00
|
|
|
|
sylv = std::make_unique<TriangularSylvester>(*bdecomp, *cdecomp);
|
2019-01-08 17:12:05 +01:00
|
|
|
|
else
|
2019-01-15 18:55:09 +01:00
|
|
|
|
sylv = std::make_unique<IterativeSylvester>(*bdecomp, *cdecomp);
|
2019-01-08 17:12:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
GeneralSylvester::solve()
|
|
|
|
|
{
|
|
|
|
|
if (solved)
|
|
|
|
|
throw SYLV_MES_EXCEPTION("Attempt to run solve() more than once.");
|
|
|
|
|
|
|
|
|
|
clock_t start = clock();
|
|
|
|
|
// multiply d
|
|
|
|
|
d.multLeftITrans(bdecomp->getQ());
|
|
|
|
|
d.multRightKron(cdecomp->getQ(), order);
|
|
|
|
|
// convert to KronVector
|
|
|
|
|
KronVector dkron(d.getData(), getM(), getN(), order);
|
|
|
|
|
// solve
|
|
|
|
|
sylv->solve(pars, dkron);
|
|
|
|
|
// multiply d back
|
|
|
|
|
d.multLeftI(bdecomp->getQ());
|
|
|
|
|
d.multRightKron(cdecomp->getInvQ(), order);
|
|
|
|
|
clock_t end = clock();
|
2019-03-08 15:32:13 +01:00
|
|
|
|
pars.cpu_time = static_cast<double>(end-start)/CLOCKS_PER_SEC;
|
2019-01-08 17:12:05 +01:00
|
|
|
|
|
|
|
|
|
solved = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
Dynare++ / sylvester equation solver: refactor Vector and ConstVector classes
- these classes now encapsulate a std::shared_ptr<{const, }double>, so that
they do not perform memory management, and several {Const,}Vector instances
can transparently share the same underlying data
- make converting constructor from ConstVector to Vector explicit, since that
entails memory allocation (but the reverse conversion is almost costless, so
keep it implicit); do the same for GeneralMatrix/ConstGeneralMatrix,
TwoDMatrix/ConstTwoDMatrix
- remove the constructors that were extracting a row/column from a matrix, and
replace them by getRow() and getCol() methods on {Const,}GeneralMatrix
- rename and change the API of the complex version Vector::add(), so that it is
explicit that it deals with complex numbers
- add constructors that take a MATLAB mxArray
2019-01-22 16:07:44 +01:00
|
|
|
|
GeneralSylvester::check(const ConstVector &ds)
|
2019-01-08 17:12:05 +01:00
|
|
|
|
{
|
|
|
|
|
if (!solved)
|
|
|
|
|
throw SYLV_MES_EXCEPTION("Cannot run check on system, which is not solved yet.");
|
|
|
|
|
|
2019-03-28 18:56:46 +01:00
|
|
|
|
// calculate xcheck = A·X+B·X·⊗ⁱC−D
|
2019-04-16 12:40:50 +02:00
|
|
|
|
SylvMatrix dcheck(d.nrows(), d.ncols());
|
|
|
|
|
dcheck.multLeft(b.nrows()-b.ncols(), b, d);
|
2019-01-08 17:12:05 +01:00
|
|
|
|
dcheck.multRightKron(c, order);
|
|
|
|
|
dcheck.multAndAdd(a, d);
|
Dynare++ / sylvester equation solver: refactor Vector and ConstVector classes
- these classes now encapsulate a std::shared_ptr<{const, }double>, so that
they do not perform memory management, and several {Const,}Vector instances
can transparently share the same underlying data
- make converting constructor from ConstVector to Vector explicit, since that
entails memory allocation (but the reverse conversion is almost costless, so
keep it implicit); do the same for GeneralMatrix/ConstGeneralMatrix,
TwoDMatrix/ConstTwoDMatrix
- remove the constructors that were extracting a row/column from a matrix, and
replace them by getRow() and getCol() methods on {Const,}GeneralMatrix
- rename and change the API of the complex version Vector::add(), so that it is
explicit that it deals with complex numbers
- add constructors that take a MATLAB mxArray
2019-01-22 16:07:44 +01:00
|
|
|
|
dcheck.getData().add(-1.0, ds);
|
2019-01-08 17:12:05 +01:00
|
|
|
|
// calculate relative norms
|
|
|
|
|
pars.mat_err1 = dcheck.getNorm1()/d.getNorm1();
|
|
|
|
|
pars.mat_errI = dcheck.getNormInf()/d.getNormInf();
|
|
|
|
|
pars.mat_errF = dcheck.getData().getNorm()/d.getData().getNorm();
|
|
|
|
|
pars.vec_err1 = dcheck.getData().getNorm1()/d.getData().getNorm1();
|
|
|
|
|
pars.vec_errI = dcheck.getData().getMax()/d.getData().getMax();
|
|
|
|
|
}
|