| 1 |
/* |
| 2 |
* Copyright © 2004 Ondra Kamenik |
| 3 |
* Copyright © 2019-2023 Dynare Team |
| 4 |
* |
| 5 |
* This file is part of Dynare. |
| 6 |
* |
| 7 |
* Dynare is free software: you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation, either version 3 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* Dynare is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with Dynare. If not, see <https://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "monoms.hh" |
| 22 |
#include "fs_tensor.hh" |
| 23 |
#include "tl_exception.hh" |
| 24 |
|
| 25 |
#include <iostream> |
| 26 |
|
| 27 |
IntGenerator intgen; |
| 28 |
|
| 29 |
void |
| 30 |
IntGenerator::init(int nf, int ny, int nv, int nw, int nu, int mx, double prob) |
| 31 |
{ |
| 32 |
maxim = mx; |
| 33 |
probab = prob; |
| 34 |
decltype(mtgen)::result_type seed = nf; |
| 35 |
seed = 256 * seed + ny; |
| 36 |
seed = 256 * seed + nv; |
| 37 |
seed = 256 * seed + nw; |
| 38 |
seed = 256 * seed + nu; |
| 39 |
mtgen.seed(seed); |
| 40 |
} |
| 41 |
|
| 42 |
int |
| 43 |
IntGenerator::get() |
| 44 |
{ |
| 45 |
double d = dis(mtgen); |
| 46 |
auto num_inter = static_cast<int>(static_cast<double>(2 * maxim) / (1.0 - probab)); |
| 47 |
int num_zero_inter = num_inter - 2 * maxim; |
| 48 |
if (d < static_cast<double>(num_zero_inter) / num_inter) |
| 49 |
return 0; |
| 50 |
return static_cast<int>((d * num_inter) - num_zero_inter - maxim); |
| 51 |
} |
| 52 |
|
| 53 |
Monom::Monom(int len) : IntSequence(len) |
| 54 |
{ |
| 55 |
for (int i = 0; i < len; i++) |
| 56 |
operator[](i) = intgen.get(); |
| 57 |
} |
| 58 |
|
| 59 |
Monom::Monom(int len, int item) : IntSequence(len, item) |
| 60 |
{ |
| 61 |
} |
| 62 |
|
| 63 |
double |
| 64 |
Monom::deriv(const IntSequence& vars) const |
| 65 |
{ |
| 66 |
double res = 1.0; |
| 67 |
int first_same_i = 0; |
| 68 |
for (int i = 0; i < vars.size(); i++) |
| 69 |
{ |
| 70 |
TL_RAISE_IF(vars[i] < 0 || vars[i] >= size(), "Wrong variable index in Monom::deriv"); |
| 71 |
if (vars[i] != vars[first_same_i]) |
| 72 |
first_same_i = i; |
| 73 |
int mult = operator[](vars[i]) - (i - first_same_i); |
| 74 |
if (mult == 0) |
| 75 |
return 0; |
| 76 |
res *= mult; |
| 77 |
} |
| 78 |
return res; |
| 79 |
} |
| 80 |
|
| 81 |
void |
| 82 |
Monom::multiplyWith(int ex, const Monom& m) |
| 83 |
{ |
| 84 |
TL_RAISE_IF(size() != m.size(), "Wrong sizes of monoms in Monom::multiplyWith"); |
| 85 |
if (ex == 0) |
| 86 |
return; |
| 87 |
for (int i = 0; i < size(); i++) |
| 88 |
operator[](i) += m[i] * ex; |
| 89 |
} |
| 90 |
|
| 91 |
void |
| 92 |
Monom::print() const |
| 93 |
{ |
| 94 |
std::cout << '['; |
| 95 |
for (int i = 0; i < size(); i++) |
| 96 |
std::cout << operator[](i); |
| 97 |
std::cout << ']'; |
| 98 |
} |
| 99 |
|
| 100 |
Monom1Vector::Monom1Vector(int nxx, int l) : nx(nxx), len(l) |
| 101 |
{ |
| 102 |
for (int i = 0; i < len; i++) |
| 103 |
x.emplace_back(nx); |
| 104 |
} |
| 105 |
|
| 106 |
void |
| 107 |
Monom1Vector::deriv(const IntSequence& c, Vector& out) const |
| 108 |
{ |
| 109 |
TL_RAISE_IF(out.length() != len, "Wrong length of output vector in Monom1Vector::deriv"); |
| 110 |
|
| 111 |
for (int i = 0; i < len; i++) |
| 112 |
out[i] = x[i].deriv(c); |
| 113 |
} |
| 114 |
|
| 115 |
std::unique_ptr<FGSTensor> |
| 116 |
Monom1Vector::deriv(int dim) const |
| 117 |
{ |
| 118 |
auto res = std::make_unique<FGSTensor>(len, TensorDimens(Symmetry {dim}, IntSequence {nx})); |
| 119 |
for (Tensor::index it = res->begin(); it != res->end(); ++it) |
| 120 |
{ |
| 121 |
Vector outcol {res->getCol(*it)}; |
| 122 |
deriv(it.getCoor(), outcol); |
| 123 |
} |
| 124 |
return res; |
| 125 |
} |
| 126 |
|
| 127 |
void |
| 128 |
Monom1Vector::print() const |
| 129 |
{ |
| 130 |
std::cout << "Variables: x(" << nx << ")\n" |
| 131 |
<< "Rows: " << len << '\n'; |
| 132 |
for (int i = 0; i < len; i++) |
| 133 |
{ |
| 134 |
std::cout << i << ": "; |
| 135 |
x[i].print(); |
| 136 |
std::cout << '\n'; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
Monom2Vector::Monom2Vector(int nyy, int nuu, int l) : ny(nyy), nu(nuu), len(l) |
| 141 |
{ |
| 142 |
for (int i = 0; i < len; i++) |
| 143 |
{ |
| 144 |
y.emplace_back(ny); |
| 145 |
u.emplace_back(nu); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
Monom2Vector::Monom2Vector(const Monom1Vector& g, const Monom2Vector& xmon) : |
| 150 |
ny(xmon.ny), nu(xmon.nu), len(g.len) |
| 151 |
{ |
| 152 |
TL_RAISE_IF(xmon.len != g.nx, "Wrong number of x's in Monom2Vector constructor"); |
| 153 |
|
| 154 |
for (int i = 0; i < len; i++) |
| 155 |
{ |
| 156 |
y.emplace_back(ny, 0); |
| 157 |
u.emplace_back(nu, 0); |
| 158 |
} |
| 159 |
|
| 160 |
for (int i = 0; i < len; i++) |
| 161 |
// Multiply from xmon |
| 162 |
for (int j = 0; j < g.nx; j++) |
| 163 |
{ |
| 164 |
int ex = g.x[i].operator[](j); |
| 165 |
y[i].multiplyWith(ex, xmon.y[j]); |
| 166 |
u[i].multiplyWith(ex, xmon.u[j]); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
void |
| 171 |
Monom2Vector::deriv(const Symmetry& s, const IntSequence& c, Vector& out) const |
| 172 |
{ |
| 173 |
TL_RAISE_IF(out.length() != len, "Wrong length of output vector in Monom2Vector::deriv"); |
| 174 |
TL_RAISE_IF(s.num() != 2, "Wrong symmetry for Monom2Vector::deriv"); |
| 175 |
TL_RAISE_IF(s.dimen() != c.size(), |
| 176 |
"Incompatible symmetry and coordinates in Monom2Vector::deriv"); |
| 177 |
IntSequence cy(c, 0, s[0]); |
| 178 |
IntSequence cu(c, s[0], s.dimen()); |
| 179 |
for (int i = 0; i < len; i++) |
| 180 |
out[i] = y[i].deriv(cy) * u[i].deriv(cu); |
| 181 |
} |
| 182 |
|
| 183 |
std::unique_ptr<FGSTensor> |
| 184 |
Monom2Vector::deriv(const Symmetry& s) const |
| 185 |
{ |
| 186 |
IntSequence nvs {ny, nu}; |
| 187 |
auto t = std::make_unique<FGSTensor>(len, TensorDimens(s, nvs)); |
| 188 |
for (Tensor::index it = t->begin(); it != t->end(); ++it) |
| 189 |
{ |
| 190 |
Vector col {t->getCol(*it)}; |
| 191 |
deriv(s, it.getCoor(), col); |
| 192 |
} |
| 193 |
return t; |
| 194 |
} |
| 195 |
|
| 196 |
FGSContainer |
| 197 |
Monom2Vector::deriv(int maxdim) const |
| 198 |
{ |
| 199 |
FGSContainer res(2); |
| 200 |
for (int dim = 1; dim <= maxdim; dim++) |
| 201 |
for (int ydim = 0; ydim <= dim; ydim++) |
| 202 |
{ |
| 203 |
int udim = dim - ydim; |
| 204 |
res.insert(deriv(Symmetry {ydim, udim})); |
| 205 |
} |
| 206 |
return res; |
| 207 |
} |
| 208 |
|
| 209 |
void |
| 210 |
Monom2Vector::print() const |
| 211 |
{ |
| 212 |
std::cout << "Variables: y(" << ny << ") u(" << nu << ")\n" |
| 213 |
<< "Rows: " << len << '\n'; |
| 214 |
for (int i = 0; i < len; i++) |
| 215 |
{ |
| 216 |
std::cout << i; |
| 217 |
y[i].print(); |
| 218 |
std::cout << " "; |
| 219 |
u[i].print(); |
| 220 |
std::cout << '\n'; |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
void |
| 225 |
Monom4Vector::init_random() |
| 226 |
{ |
| 227 |
for (int i = 0; i < len; i++) |
| 228 |
{ |
| 229 |
x1.emplace_back(nx1); |
| 230 |
x2.emplace_back(nx2); |
| 231 |
x3.emplace_back(nx3); |
| 232 |
x4.emplace_back(nx4); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
Monom4Vector::Monom4Vector(int l, int ny, int nu) : len(l), nx1(ny), nx2(nu), nx3(0), nx4(1) |
| 237 |
{ |
| 238 |
init_random(); |
| 239 |
} |
| 240 |
|
| 241 |
Monom4Vector::Monom4Vector(int l, int ny, int nu, int nup) : |
| 242 |
len(l), nx1(ny), nx2(nu), nx3(nup), nx4(1) |
| 243 |
{ |
| 244 |
init_random(); |
| 245 |
} |
| 246 |
|
| 247 |
Monom4Vector::Monom4Vector(int l, int nbigg, int ng, int ny, int nu) : |
| 248 |
len(l), nx1(nbigg), nx2(ng), nx3(ny), nx4(nu) |
| 249 |
{ |
| 250 |
init_random(); |
| 251 |
} |
| 252 |
|
| 253 |
Monom4Vector::Monom4Vector(const Monom4Vector& f, const Monom4Vector& bigg, const Monom4Vector& g) : |
| 254 |
len(f.len), nx1(bigg.nx1), nx2(bigg.nx2), nx3(bigg.nx3), nx4(1) |
| 255 |
{ |
| 256 |
TL_RAISE_IF( |
| 257 |
!(bigg.nx1 == g.nx1 && bigg.nx2 == g.nx2 && g.nx3 == 0 && bigg.nx4 == 1 && g.nx4 == 1), |
| 258 |
"Incompatible g with G"); |
| 259 |
TL_RAISE_IF(!(bigg.len == f.nx1 && g.len == f.nx2 && bigg.nx1 == f.nx3 && bigg.nx2 == f.nx4), |
| 260 |
"Incompatible g or G with f"); |
| 261 |
|
| 262 |
for (int i = 0; i < len; i++) |
| 263 |
{ |
| 264 |
x1.emplace_back(nx1, 0); |
| 265 |
x2.emplace_back(nx2, 0); |
| 266 |
x3.emplace_back(nx3, 0); |
| 267 |
x4.emplace_back(nx4, 0); |
| 268 |
} |
| 269 |
|
| 270 |
for (int i = 0; i < len; i++) |
| 271 |
{ |
| 272 |
// Multiply from G (first argument) |
| 273 |
for (int j = 0; j < f.nx1; j++) |
| 274 |
{ |
| 275 |
int ex = f.x1[i].operator[](j); |
| 276 |
x1[i].multiplyWith(ex, bigg.x1[j]); |
| 277 |
x2[i].multiplyWith(ex, bigg.x2[j]); |
| 278 |
x3[i].multiplyWith(ex, bigg.x3[j]); |
| 279 |
x4[i].multiplyWith(ex, bigg.x4[j]); |
| 280 |
} |
| 281 |
// Multiply from g (second argument) |
| 282 |
for (int j = 0; j < f.nx2; j++) |
| 283 |
{ |
| 284 |
int ex = f.x2[i].operator[](j); |
| 285 |
x1[i].multiplyWith(ex, g.x1[j]); |
| 286 |
x2[i].multiplyWith(ex, g.x2[j]); |
| 287 |
x4[i].multiplyWith(ex, g.x4[j]); |
| 288 |
} |
| 289 |
// Add y as third argument of f |
| 290 |
x1[i].add(1, f.x3[i]); |
| 291 |
// Add u as fourth argument of f |
| 292 |
x2[i].add(1, f.x4[i]); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
void |
| 297 |
Monom4Vector::deriv(const Symmetry& s, const IntSequence& coor, Vector& out) const |
| 298 |
{ |
| 299 |
TL_RAISE_IF(out.length() != len, "Wrong length of output vector in Monom4Vector::deriv"); |
| 300 |
TL_RAISE_IF(s.num() != 4, "Wrong symmetry for Monom4Vector::deriv"); |
| 301 |
TL_RAISE_IF(s.dimen() != coor.size(), |
| 302 |
"Incompatible symmetry and coordinates in Monom4Vector::deriv"); |
| 303 |
|
| 304 |
for (int i = 0; i < len; i++) |
| 305 |
{ |
| 306 |
out[i] = 1; |
| 307 |
int off = 0; |
| 308 |
out[i] *= x1[i].deriv(IntSequence(coor, off, off + s[0])); |
| 309 |
off += s[0]; |
| 310 |
out[i] *= x2[i].deriv(IntSequence(coor, off, off + s[1])); |
| 311 |
off += s[1]; |
| 312 |
out[i] *= x3[i].deriv(IntSequence(coor, off, off + s[2])); |
| 313 |
off += s[2]; |
| 314 |
out[i] *= x4[i].deriv(IntSequence(coor, off, off + s[3])); |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
std::unique_ptr<FGSTensor> |
| 319 |
Monom4Vector::deriv(const Symmetry& s) const |
| 320 |
{ |
| 321 |
IntSequence nvs {nx1, nx2, nx3, nx4}; |
| 322 |
auto res = std::make_unique<FGSTensor>(len, TensorDimens(s, nvs)); |
| 323 |
for (Tensor::index run = res->begin(); run != res->end(); ++run) |
| 324 |
{ |
| 325 |
Vector col {res->getCol(*run)}; |
| 326 |
deriv(s, run.getCoor(), col); |
| 327 |
} |
| 328 |
return res; |
| 329 |
} |
| 330 |
|
| 331 |
FSSparseTensor |
| 332 |
Monom4Vector::deriv(int dim) const |
| 333 |
{ |
| 334 |
IntSequence cum {0, nx1, nx1 + nx2, nx1 + nx2 + nx3}; |
| 335 |
|
| 336 |
FSSparseTensor res(dim, nx1 + nx2 + nx3 + nx4, len); |
| 337 |
|
| 338 |
FFSTensor dummy(0, nx1 + nx2 + nx3 + nx4, dim); |
| 339 |
for (Tensor::index run = dummy.begin(); run != dummy.end(); ++run) |
| 340 |
{ |
| 341 |
Symmetry ind_sym {0, 0, 0, 0}; |
| 342 |
IntSequence ind(run.getCoor()); |
| 343 |
for (int i = 0; i < ind.size(); i++) |
| 344 |
{ |
| 345 |
int j = 3; |
| 346 |
while (j >= 0 && ind[i] < cum[j]) |
| 347 |
j--; |
| 348 |
ind_sym[j]++; |
| 349 |
ind[i] -= cum[j]; |
| 350 |
} |
| 351 |
|
| 352 |
Vector col(len); |
| 353 |
deriv(ind_sym, ind, col); |
| 354 |
for (int i = 0; i < len; i++) |
| 355 |
if (col[i] != 0.0) |
| 356 |
res.insert(run.getCoor(), i, col[i]); |
| 357 |
} |
| 358 |
|
| 359 |
return res; |
| 360 |
} |
| 361 |
|
| 362 |
void |
| 363 |
Monom4Vector::print() const |
| 364 |
{ |
| 365 |
std::cout << "Variables: x1(" << nx1 << ") x2(" << nx2 << ") x3(" << nx3 << ") x4(" << nx4 |
| 366 |
<< ")\n" |
| 367 |
<< "Rows: " << len << '\n'; |
| 368 |
for (int i = 0; i < len; i++) |
| 369 |
{ |
| 370 |
std::cout << i << ": "; |
| 371 |
x1[i].print(); |
| 372 |
std::cout << " "; |
| 373 |
x2[i].print(); |
| 374 |
std::cout << " "; |
| 375 |
x3[i].print(); |
| 376 |
std::cout << " "; |
| 377 |
x4[i].print(); |
| 378 |
std::cout << '\n'; |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
SparseDerivGenerator::SparseDerivGenerator(int nf, int ny, int nu, int nup, int nbigg, int ng, |
| 383 |
int mx, double prob, int maxdim) : |
| 384 |
maxdimen(maxdim), bigg(4), g(4), rcont(4) |
| 385 |
{ |
| 386 |
intgen.init(nf, ny, nu, nup, nbigg, mx, prob); |
| 387 |
|
| 388 |
Monom4Vector bigg_m(nbigg, ny, nu, nup); |
| 389 |
Monom4Vector g_m(ng, ny, nu); |
| 390 |
Monom4Vector f(nf, nbigg, ng, ny, nu); |
| 391 |
Monom4Vector r(f, bigg_m, g_m); |
| 392 |
|
| 393 |
for (int dim = 1; dim <= maxdimen; dim++) |
| 394 |
{ |
| 395 |
for (auto& si : SymmetrySet(dim, 4)) |
| 396 |
{ |
| 397 |
bigg.insert(bigg_m.deriv(si)); |
| 398 |
rcont.insert(r.deriv(si)); |
| 399 |
if (si[2] == 0) |
| 400 |
g.insert(g_m.deriv(si)); |
| 401 |
} |
| 402 |
|
| 403 |
ts.push_back(f.deriv(dim)); |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
DenseDerivGenerator::DenseDerivGenerator(int ng, int nx, int ny, int nu, int mx, double prob, |
| 408 |
int maxdim) : |
| 409 |
maxdimen(maxdim), xcont(0), rcont(0), ts(maxdimen), uxcont(0), uts(maxdimen) |
| 410 |
{ |
| 411 |
intgen.init(ng, nx, ny, nu, nu, mx, prob); |
| 412 |
Monom1Vector g(nx, ng); |
| 413 |
Monom2Vector x(ny, nu, nx); |
| 414 |
Monom2Vector r(g, x); |
| 415 |
xcont = x.deriv(maxdimen); |
| 416 |
rcont = r.deriv(maxdimen); |
| 417 |
for (int d = 1; d <= maxdimen; d++) |
| 418 |
ts[d - 1] = g.deriv(d); |
| 419 |
} |
| 420 |
|
| 421 |
void |
| 422 |
DenseDerivGenerator::unfold() |
| 423 |
{ |
| 424 |
uxcont = UGSContainer(xcont); |
| 425 |
for (int i = 0; i < maxdimen; i++) |
| 426 |
uts[i] = std::make_unique<UGSTensor>(*(ts[i])); |
| 427 |
} |