2019-06-19 14:34:30 +02:00
|
|
|
|
/*
|
|
|
|
|
* Copyright © 2004 Ondra Kamenik
|
2024-01-05 18:08:54 +01:00
|
|
|
|
* Copyright © 2019-2024 Dynare Team
|
2019-06-19 14:34:30 +02:00
|
|
|
|
*
|
|
|
|
|
* 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 16:09:25 +01:00
|
|
|
|
|
|
|
|
|
// Symmetry.
|
|
|
|
|
|
2019-05-06 18:43:54 +02:00
|
|
|
|
/* Symmetry is an abstraction for a term of the form y³u². It manages only
|
|
|
|
|
indices, not the variable names. So if one uses this abstraction, it must
|
|
|
|
|
be kept in mind that y is the first and u is the second.
|
2019-01-08 16:09:25 +01:00
|
|
|
|
|
|
|
|
|
In fact, the symmetry is a special case of equivalence, but its
|
|
|
|
|
implementation is much simpler. We do not need an abstraction for the
|
2019-05-06 18:43:54 +02:00
|
|
|
|
term “yyuyu” but due to Green theorem we can have term y³u². That
|
2019-01-08 16:09:25 +01:00
|
|
|
|
is why the equivalence is too general for our purposes.
|
|
|
|
|
|
|
|
|
|
One of a main purposes of the tensor library is to calculate something like:
|
2019-05-06 18:43:54 +02:00
|
|
|
|
|
|
|
|
|
ₗ
|
|
|
|
|
[B_y²u³]_α₁α₂β₁β₂β₃ = [f_zˡ]_γ₁…γₗ ∑ ∏ [g_{s^|cₘ|}]_cₘ(α,β)^γₘ
|
|
|
|
|
c∈ℳₗ,₅ ᵐ⁼¹
|
|
|
|
|
|
|
|
|
|
We must be able to calculate a symmetry induced by symmetry y²u³ and by an
|
|
|
|
|
equivalence class from equivalence c. For equivalence class {0,4} the
|
|
|
|
|
induced symmetry is “yu”, since we pick first and fifth variable from y²u³.
|
|
|
|
|
For a given outer symmetry, the class InducedSymmetries does this for all
|
|
|
|
|
classes of a given equivalence.
|
2019-01-08 16:09:25 +01:00
|
|
|
|
|
|
|
|
|
We need also to cycle through all possible symmetries yielding the
|
2019-05-06 18:43:54 +02:00
|
|
|
|
given dimension. For this purpose we define classes SymmetrySet and
|
|
|
|
|
symiterator.
|
2019-01-08 16:09:25 +01:00
|
|
|
|
|
2019-05-06 18:43:54 +02:00
|
|
|
|
The symmetry is implemented as IntSequence, in fact, it inherits
|
2019-01-08 16:09:25 +01:00
|
|
|
|
from it. */
|
|
|
|
|
|
2023-12-13 16:50:28 +01:00
|
|
|
|
#ifndef SYMMETRY_HH
|
|
|
|
|
#define SYMMETRY_HH
|
2019-01-08 16:09:25 +01:00
|
|
|
|
|
|
|
|
|
#include "equivalence.hh"
|
|
|
|
|
#include "int_sequence.hh"
|
|
|
|
|
|
2019-02-08 18:38:05 +01:00
|
|
|
|
#include <initializer_list>
|
2023-11-29 19:00:21 +01:00
|
|
|
|
#include <list>
|
2019-02-08 18:38:05 +01:00
|
|
|
|
#include <memory>
|
2023-11-29 19:00:21 +01:00
|
|
|
|
#include <utility>
|
|
|
|
|
#include <vector>
|
2019-01-08 16:09:25 +01:00
|
|
|
|
|
2019-05-06 18:43:54 +02:00
|
|
|
|
/* Clear. The method isFull() returns true if and only if the symmetry
|
2019-02-08 18:38:05 +01:00
|
|
|
|
allows for any permutation of indices.
|
|
|
|
|
|
|
|
|
|
WARNING: Symmetry(n) and Symmetry{n} are not the same. The former
|
|
|
|
|
initializes a symmetry of n elements, while the latter is a full symmetry of
|
|
|
|
|
order n. This is similar to the behaviour of std::vector. */
|
2019-01-08 16:09:25 +01:00
|
|
|
|
|
|
|
|
|
class Symmetry : public IntSequence
|
|
|
|
|
{
|
|
|
|
|
public:
|
2019-02-08 18:38:05 +01:00
|
|
|
|
// Constructor allocating a given length of (zero-initialized) data
|
2023-11-29 19:00:21 +01:00
|
|
|
|
explicit Symmetry(int len) : IntSequence(len, 0)
|
2019-01-08 16:09:25 +01:00
|
|
|
|
{
|
|
|
|
|
}
|
2019-02-08 18:38:05 +01:00
|
|
|
|
/* Constructor using an initializer list, that gives the contents of the
|
2019-05-06 18:43:54 +02:00
|
|
|
|
Symmetry. Typically used for symmetries of the form yⁿ, yⁿuᵐ, yⁿuᵐσᵏ */
|
2023-11-29 19:00:21 +01:00
|
|
|
|
Symmetry(std::initializer_list<int> init) : IntSequence(std::move(init))
|
2019-01-08 16:09:25 +01:00
|
|
|
|
{
|
|
|
|
|
}
|
2019-02-08 18:38:05 +01:00
|
|
|
|
// Constructor of implied symmetry for a symmetry and an equivalence class
|
2023-11-29 19:00:21 +01:00
|
|
|
|
Symmetry(const Symmetry& s, const OrdSequence& cl);
|
2019-02-08 18:38:05 +01:00
|
|
|
|
/* Subsymmetry, which takes the given length of symmetry from the end (shares
|
|
|
|
|
data pointer) */
|
2023-11-29 19:00:21 +01:00
|
|
|
|
Symmetry(Symmetry& s, int len) : IntSequence(s, s.size() - len, s.size())
|
2019-01-08 16:09:25 +01:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-05 15:44:23 +01:00
|
|
|
|
[[nodiscard]] int
|
2019-01-08 16:09:25 +01:00
|
|
|
|
num() const
|
|
|
|
|
{
|
|
|
|
|
return size();
|
|
|
|
|
}
|
2023-12-05 15:44:23 +01:00
|
|
|
|
[[nodiscard]] int
|
2019-01-08 16:09:25 +01:00
|
|
|
|
dimen() const
|
|
|
|
|
{
|
|
|
|
|
return sum();
|
|
|
|
|
}
|
2023-12-05 15:44:23 +01:00
|
|
|
|
[[nodiscard]] int findClass(int i) const;
|
|
|
|
|
[[nodiscard]] bool isFull() const;
|
2019-01-08 16:09:25 +01:00
|
|
|
|
};
|
|
|
|
|
|
2019-02-12 12:15:56 +01:00
|
|
|
|
/* This is an iterator that iterates over all symmetries of given length and
|
|
|
|
|
dimension (see the SymmetrySet class for details).
|
2019-01-08 16:09:25 +01:00
|
|
|
|
|
2019-02-12 12:15:56 +01:00
|
|
|
|
The beginning iterator is (0, …, 0, dim).
|
2019-05-06 18:43:54 +02:00
|
|
|
|
Increasing it gives (0, … , 1, dim−1)
|
2019-02-12 12:15:56 +01:00
|
|
|
|
The just-before-end iterator is (dim, 0, …, 0)
|
|
|
|
|
The past-the-end iterator is (dim+1, 0, …, 0)
|
2019-01-08 16:09:25 +01:00
|
|
|
|
|
2019-02-12 12:15:56 +01:00
|
|
|
|
The constructor creates the iterator which starts from the given symmetry
|
|
|
|
|
symmetry (beginning). */
|
2019-01-08 16:09:25 +01:00
|
|
|
|
|
2019-02-12 12:15:56 +01:00
|
|
|
|
class symiterator
|
2019-01-08 16:09:25 +01:00
|
|
|
|
{
|
2019-02-12 12:15:56 +01:00
|
|
|
|
const int dim;
|
2019-01-08 16:09:25 +01:00
|
|
|
|
Symmetry run;
|
2023-11-29 19:00:21 +01:00
|
|
|
|
|
2019-01-08 16:09:25 +01:00
|
|
|
|
public:
|
2019-02-12 12:15:56 +01:00
|
|
|
|
symiterator(int dim_arg, Symmetry run_arg);
|
|
|
|
|
~symiterator() = default;
|
2023-11-29 19:00:21 +01:00
|
|
|
|
symiterator& operator++();
|
|
|
|
|
const Symmetry&
|
2019-02-12 12:15:56 +01:00
|
|
|
|
operator*() const
|
2019-01-08 16:09:25 +01:00
|
|
|
|
{
|
|
|
|
|
return run;
|
|
|
|
|
}
|
2024-01-09 13:04:33 +01:00
|
|
|
|
[[nodiscard]] bool
|
2024-01-24 09:48:09 +01:00
|
|
|
|
operator==(const symiterator& it) const
|
2019-01-08 16:09:25 +01:00
|
|
|
|
{
|
2019-02-12 12:15:56 +01:00
|
|
|
|
return dim == it.dim && run == it.run;
|
2019-01-08 16:09:25 +01:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-05-06 18:43:54 +02:00
|
|
|
|
/* The class SymmetrySet defines a set of symmetries of the given length
|
2019-02-12 12:15:56 +01:00
|
|
|
|
having given dimension (i.e. it represents all the lists of integers of
|
2019-05-06 18:43:54 +02:00
|
|
|
|
length ‘len’ and of sum equal to ‘dim’). It does not store all the
|
2019-02-12 12:15:56 +01:00
|
|
|
|
symmetries, it is just a convenience class for iteration.
|
2019-01-08 16:09:25 +01:00
|
|
|
|
|
2019-05-06 18:43:54 +02:00
|
|
|
|
The typical usage of the abstractions for SymmetrySet and
|
|
|
|
|
symiterator is as follows:
|
2019-01-08 16:09:25 +01:00
|
|
|
|
|
2019-02-12 12:15:56 +01:00
|
|
|
|
for (auto &si : SymmetrySet(6, 4))
|
|
|
|
|
|
|
|
|
|
It goes through all symmetries of lenght 4 having dimension 6. One can use
|
2019-05-06 18:43:54 +02:00
|
|
|
|
‘si’ as the symmetry in the body. */
|
2019-02-12 12:15:56 +01:00
|
|
|
|
|
|
|
|
|
class SymmetrySet
|
2019-01-08 16:09:25 +01:00
|
|
|
|
{
|
|
|
|
|
public:
|
2019-02-12 12:15:56 +01:00
|
|
|
|
const int len;
|
|
|
|
|
const int dim;
|
2023-11-29 19:00:21 +01:00
|
|
|
|
SymmetrySet(int dim_arg, int len_arg) : len(len_arg), dim(dim_arg)
|
2019-01-08 16:09:25 +01:00
|
|
|
|
{
|
|
|
|
|
}
|
2023-12-05 15:44:23 +01:00
|
|
|
|
[[nodiscard]] symiterator
|
2019-02-12 12:15:56 +01:00
|
|
|
|
begin() const
|
|
|
|
|
{
|
|
|
|
|
Symmetry run(len);
|
2023-11-29 19:00:21 +01:00
|
|
|
|
run[len - 1] = dim;
|
|
|
|
|
return {dim, run};
|
2019-02-12 12:15:56 +01:00
|
|
|
|
}
|
2023-12-05 15:44:23 +01:00
|
|
|
|
[[nodiscard]] symiterator
|
2019-02-12 12:15:56 +01:00
|
|
|
|
end() const
|
2019-01-08 16:09:25 +01:00
|
|
|
|
{
|
2019-02-12 12:15:56 +01:00
|
|
|
|
Symmetry run(len);
|
2023-11-29 19:00:21 +01:00
|
|
|
|
run[0] = dim + 1;
|
|
|
|
|
return {dim, run};
|
2019-01-08 16:09:25 +01:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* This simple abstraction just constructs a vector of induced
|
|
|
|
|
symmetries from the given equivalence and outer symmetry. A
|
|
|
|
|
permutation might optionally permute the classes of the equivalence. */
|
|
|
|
|
|
2019-02-06 15:50:01 +01:00
|
|
|
|
class InducedSymmetries : public std::vector<Symmetry>
|
2019-01-08 16:09:25 +01:00
|
|
|
|
{
|
|
|
|
|
public:
|
2023-11-29 19:00:21 +01:00
|
|
|
|
InducedSymmetries(const Equivalence& e, const Symmetry& s);
|
|
|
|
|
InducedSymmetries(const Equivalence& e, const Permutation& p, const Symmetry& s);
|
2019-01-08 16:09:25 +01:00
|
|
|
|
void print() const;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif
|