2019-06-19 14:34:30 +02:00
|
|
|
|
/*
|
|
|
|
|
* Copyright © 2005 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-04 17:27:23 +01:00
|
|
|
|
|
|
|
|
|
#include "product.hh"
|
2019-01-08 16:09:25 +01:00
|
|
|
|
#include "symmetry.hh"
|
2019-01-04 17:27:23 +01:00
|
|
|
|
|
2019-01-14 16:09:49 +01:00
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <iomanip>
|
2019-01-04 17:27:23 +01:00
|
|
|
|
|
2019-06-19 17:33:01 +02:00
|
|
|
|
/* This constructs a product iterator corresponding to index (j0,0,…,0). */
|
2019-01-04 17:27:23 +01:00
|
|
|
|
|
|
|
|
|
prodpit::prodpit(const ProductQuadrature &q, int j0, int l)
|
2019-01-14 16:09:49 +01:00
|
|
|
|
: prodq(q), level(l), npoints(q.uquad.numPoints(l)),
|
2019-02-08 18:38:05 +01:00
|
|
|
|
jseq(q.dimen(), 0),
|
2019-01-14 16:09:49 +01:00
|
|
|
|
end_flag(false),
|
|
|
|
|
sig{q.dimen()},
|
|
|
|
|
p{q.dimen()}
|
2019-01-04 17:27:23 +01:00
|
|
|
|
{
|
|
|
|
|
if (j0 < npoints)
|
|
|
|
|
{
|
2019-01-14 16:09:49 +01:00
|
|
|
|
jseq[0] = j0;
|
2019-01-04 17:27:23 +01:00
|
|
|
|
setPointAndWeight();
|
|
|
|
|
}
|
|
|
|
|
else
|
2019-01-14 16:09:49 +01:00
|
|
|
|
end_flag = true;
|
2019-01-04 17:27:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
prodpit::operator==(const prodpit &ppit) const
|
|
|
|
|
{
|
2019-01-14 16:09:49 +01:00
|
|
|
|
return &prodq == &ppit.prodq && end_flag == ppit.end_flag && jseq == ppit.jseq;
|
2019-01-04 17:27:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
prodpit &
|
|
|
|
|
prodpit::operator++()
|
|
|
|
|
{
|
2019-01-14 16:09:49 +01:00
|
|
|
|
int i = prodq.dimen()-1;
|
|
|
|
|
jseq[i]++;
|
|
|
|
|
while (i >= 0 && jseq[i] == npoints)
|
2019-01-04 17:27:23 +01:00
|
|
|
|
{
|
2019-01-14 16:09:49 +01:00
|
|
|
|
jseq[i] = 0;
|
2019-01-04 17:27:23 +01:00
|
|
|
|
i--;
|
|
|
|
|
if (i >= 0)
|
2019-01-14 16:09:49 +01:00
|
|
|
|
jseq[i]++;
|
2019-01-04 17:27:23 +01:00
|
|
|
|
}
|
2019-01-14 16:09:49 +01:00
|
|
|
|
sig.signalAfter(std::max(i, 0));
|
2019-01-04 17:27:23 +01:00
|
|
|
|
|
|
|
|
|
if (i == -1)
|
|
|
|
|
end_flag = true;
|
|
|
|
|
|
|
|
|
|
if (!end_flag)
|
|
|
|
|
setPointAndWeight();
|
|
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* This calculates the weight and sets point coordinates from the indices. */
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
prodpit::setPointAndWeight()
|
|
|
|
|
{
|
|
|
|
|
w = 1.0;
|
2019-01-14 16:09:49 +01:00
|
|
|
|
for (int i = 0; i < prodq.dimen(); i++)
|
2019-01-04 17:27:23 +01:00
|
|
|
|
{
|
2019-01-14 16:09:49 +01:00
|
|
|
|
p[i] = (prodq.uquad).point(level, jseq[i]);
|
|
|
|
|
w *= (prodq.uquad).weight(level, jseq[i]);
|
2019-01-04 17:27:23 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Debug print. */
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
prodpit::print() const
|
|
|
|
|
{
|
2019-01-14 16:09:49 +01:00
|
|
|
|
auto ff = std::cout.flags();
|
|
|
|
|
std::cout << "j=[";
|
|
|
|
|
for (int i = 0; i < prodq.dimen(); i++)
|
|
|
|
|
std::cout << std::setw(2) << jseq[i];
|
|
|
|
|
std::cout << std::showpos << std::fixed << std::setprecision(3)
|
|
|
|
|
<< "] " << std::setw(4) << w << "*(";
|
|
|
|
|
for (int i = 0; i < prodq.dimen()-1; i++)
|
|
|
|
|
std::cout << std::setw(4) << p[i] << ' ';
|
|
|
|
|
std::cout << std::setw(4) << p[prodq.dimen()-1] << ')' << std::endl;
|
|
|
|
|
std::cout.flags(ff);
|
2019-01-04 17:27:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ProductQuadrature::ProductQuadrature(int d, const OneDQuadrature &uq)
|
|
|
|
|
: QuadratureImpl<prodpit>(d), uquad(uq)
|
|
|
|
|
{
|
2019-06-19 17:33:01 +02:00
|
|
|
|
// TODO: check d≥1
|
2019-01-04 17:27:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-19 17:33:01 +02:00
|
|
|
|
/* This calls prodpit constructor to return an iterator which points
|
|
|
|
|
approximatelly at ‘ti’-th portion out of ‘tn’ portions. First we find
|
2019-01-04 17:27:23 +01:00
|
|
|
|
out how many points are in the level, and then construct an interator
|
2019-06-19 17:33:01 +02:00
|
|
|
|
(j0,0,…,0) where j0=ti·npoints/tn. */
|
2019-01-04 17:27:23 +01:00
|
|
|
|
|
|
|
|
|
prodpit
|
|
|
|
|
ProductQuadrature::begin(int ti, int tn, int l) const
|
|
|
|
|
{
|
2019-06-19 17:33:01 +02:00
|
|
|
|
// TODO: raise if l<dimen()
|
|
|
|
|
// TODO: check l ≤ uquad.numLevels()
|
2019-01-04 17:27:23 +01:00
|
|
|
|
int npoints = uquad.numPoints(l);
|
|
|
|
|
return prodpit(*this, ti*npoints/tn, l);
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-19 17:33:01 +02:00
|
|
|
|
/* This just starts at the first level and goes to a higher level as long as a
|
|
|
|
|
number of evaluations (which is nₖᵈ for k being the level) is less than the
|
|
|
|
|
given number of evaluations. */
|
2019-01-04 17:27:23 +01:00
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
ProductQuadrature::designLevelForEvals(int max_evals, int &lev, int &evals) const
|
|
|
|
|
{
|
|
|
|
|
int last_evals;
|
|
|
|
|
evals = 1;
|
|
|
|
|
lev = 1;
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
lev++;
|
|
|
|
|
last_evals = evals;
|
|
|
|
|
evals = numEvals(lev);
|
|
|
|
|
}
|
|
|
|
|
while (lev < uquad.numLevels()-2 && evals < max_evals);
|
|
|
|
|
lev--;
|
|
|
|
|
evals = last_evals;
|
|
|
|
|
}
|