Debian Codegraph @

mex/sources/libkorder/tl/tests/monoms.hh

raw
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 #ifndef MONOMS_HH
22 #define MONOMS_HH
23
24 #include <memory>
25 #include <random>
26 #include <vector>
27
28 #include "Vector.hh"
29 #include "gs_tensor.hh"
30 #include "int_sequence.hh"
31 #include "sparse_tensor.hh"
32 #include "t_container.hh"
33
34 class IntGenerator
35 {
36 int maxim {5};
37 double probab {0.3};
38 std::mt19937 mtgen;
39 std::uniform_real_distribution<> dis;
40
41 public:
42 IntGenerator() = default;
43 void init(int nf, int ny, int nv, int nw, int nu, int mx, double prob);
44 int get();
45 };
46
47 extern IntGenerator intgen;
48
49 class Monom : public IntSequence
50 {
51 public:
52 Monom(int len); // generate a random monom
53 Monom(int len, int item); // generate monom whose items are the given item
54 [[nodiscard]] double deriv(const IntSequence& vars) const;
55 // this = this·mᵉˣ (in monomial sense)
56 void multiplyWith(int ex, const Monom& m);
57 void print() const;
58 };
59
60 class Monom2Vector;
61 class Monom1Vector
62 {
63 friend class Monom2Vector;
64 int nx;
65 int len;
66 std::vector<Monom> x;
67
68 public:
69 Monom1Vector(int nxx, int l);
70 ~Monom1Vector() = default;
71 void deriv(const IntSequence& c, Vector& out) const;
72 [[nodiscard]] std::unique_ptr<FGSTensor> deriv(int dim) const;
73 void print() const;
74 };
75
76 class Monom2Vector
77 {
78 int ny, nu;
79 int len;
80 std::vector<Monom> y, u;
81
82 public:
83 // Generate random vector of monom two vector
84 Monom2Vector(int nyy, int nuu, int l);
85 // Calculate g(x(y,u))
86 Monom2Vector(const Monom1Vector& g, const Monom2Vector& xmon);
87 ~Monom2Vector() = default;
88 void deriv(const Symmetry& s, const IntSequence& c, Vector& out) const;
89 [[nodiscard]] std::unique_ptr<FGSTensor> deriv(const Symmetry& s) const;
90 [[nodiscard]] FGSContainer deriv(int maxdim) const;
91 void print() const;
92 };
93
94 class Monom4Vector
95 {
96 int len;
97 int nx1, nx2, nx3, nx4;
98 std::vector<Monom> x1, x2, x3, x4;
99
100 public:
101 /* Random for g(y,u,σ) */
102 Monom4Vector(int l, int ny, int nu);
103 /* Random for G(y,u,u′,σ) */
104 Monom4Vector(int l, int ny, int nu, int nup);
105 /* Random for f(y⁺,y,y⁻,u) */
106 Monom4Vector(int l, int nbigg, int ng, int ny, int nu);
107 /* Substitution f(G(y,u,u′,σ),g(y,u,σ),y,u) */
108 Monom4Vector(const Monom4Vector& f, const Monom4Vector& bigg, const Monom4Vector& g);
109 ~Monom4Vector() = default;
110 [[nodiscard]] FSSparseTensor deriv(int dim) const;
111 [[nodiscard]] std::unique_ptr<FGSTensor> deriv(const Symmetry& s) const;
112 void deriv(const Symmetry& s, const IntSequence& coor, Vector& out) const;
113 void print() const;
114
115 protected:
116 void init_random();
117 };
118
119 struct SparseDerivGenerator
120 {
121 int maxdimen;
122 FGSContainer bigg;
123 FGSContainer g;
124 FGSContainer rcont;
125 std::vector<FSSparseTensor> ts;
126 SparseDerivGenerator(int nf, int ny, int nu, int nup, int nbigg, int ng, int mx, double prob,
127 int maxdim);
128 };
129
130 struct DenseDerivGenerator
131 {
132 int maxdimen;
133 FGSContainer xcont;
134 FGSContainer rcont;
135 std::vector<std::unique_ptr<FGSTensor>> ts;
136 UGSContainer uxcont;
137 std::vector<std::unique_ptr<UGSTensor>> uts;
138 DenseDerivGenerator(int ng, int nx, int ny, int nu, int mx, double prob, int maxdim);
139 void unfold();
140 };
141
142 #endif