2019-06-19 14:34:30 +02:00
|
|
|
|
/*
|
|
|
|
|
* Copyright © 2004 Ondra Kamenik
|
2023-05-09 21:14:50 +02:00
|
|
|
|
* Copyright © 2019-2023 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
|
|
|
|
|
|
|
|
|
// Tensor concept.
|
|
|
|
|
|
2019-05-06 18:43:54 +02:00
|
|
|
|
/* Here we define a tensor class. A tensor is a mathematical object
|
|
|
|
|
corresponding to an (n+1)-dimensional array. An element of such array is
|
|
|
|
|
denoted [B]_α₁…αₙ^β, where β is a special index and α₁…αₙ are other indices.
|
|
|
|
|
The class Tensor and its subclasses view such array as a 2D matrix, where β
|
|
|
|
|
corresponds to one dimension, and α₁…αₙ unfold to the other dimension.
|
|
|
|
|
Whether β corresponds to rows or columns is decided by tensor subclasses,
|
|
|
|
|
however, most of our tensors will have rows indexed by β, and α₁…αₙ will
|
|
|
|
|
unfold column-wise.
|
2019-01-08 16:09:25 +01:00
|
|
|
|
|
2019-05-06 18:43:54 +02:00
|
|
|
|
There might be some symmetries in the tensor data. For instance, if α₁ is
|
|
|
|
|
interchanged with α₃ and the other elements remain equal for all possible αᵢ
|
|
|
|
|
and β, then there is a symmetry of α₁ and α₃.
|
2019-01-08 16:09:25 +01:00
|
|
|
|
|
|
|
|
|
For any symmetry, there are basically two possible storages of the
|
|
|
|
|
data. The first is unfolded storage, which stores all elements
|
|
|
|
|
regardless the symmetry. The other storage type is folded, which
|
|
|
|
|
stores only elements which do not repeat. We declare abstract classes
|
2019-05-06 18:43:54 +02:00
|
|
|
|
for unfolded and folded tensor alike.
|
2019-01-08 16:09:25 +01:00
|
|
|
|
|
2019-05-06 18:43:54 +02:00
|
|
|
|
Also, here we also define a concept of tensor index which is the n-tuple
|
|
|
|
|
α₁…αₙ. It is an iterator, which iterates in dependence of symmetry and
|
|
|
|
|
storage of the underlying tensor.
|
2019-01-08 16:09:25 +01:00
|
|
|
|
|
2019-05-06 18:43:54 +02:00
|
|
|
|
Although we do not decide about possible symmetries at this point, it is
|
|
|
|
|
worth noting that we implement two kinds of symmetries in subclasses. The
|
|
|
|
|
first one is a full symmetry where all indices are interchangeable. The
|
|
|
|
|
second one is a generalization of the first, where there are a few groups of
|
|
|
|
|
indices interchangeable within a group and not across. Moreover, the groups
|
|
|
|
|
are required to be consequent partitions of the index n-tuple. For example,
|
|
|
|
|
we do not allow α₁ to be interchangeable with α₃ and not with α₂ at the same
|
|
|
|
|
time.
|
2019-01-08 16:09:25 +01:00
|
|
|
|
|
2019-05-06 18:43:54 +02:00
|
|
|
|
However, some intermediate results are, in fact, tensors with a symmetry not
|
|
|
|
|
fitting to our concept. We develop the tensor abstraction for it, but these
|
|
|
|
|
objects are not used very often. They have limited usage due to their
|
|
|
|
|
specialized constructor. */
|
2019-01-08 16:09:25 +01:00
|
|
|
|
|
|
|
|
|
#ifndef TENSOR_H
|
|
|
|
|
#define TENSOR_H
|
|
|
|
|
|
|
|
|
|
#include "int_sequence.hh"
|
|
|
|
|
#include "twod_matrix.hh"
|
|
|
|
|
|
2019-02-12 17:30:10 +01:00
|
|
|
|
#include <iostream>
|
2023-11-29 19:00:21 +01:00
|
|
|
|
#include <memory>
|
2019-01-08 16:09:25 +01:00
|
|
|
|
|
2019-05-06 18:43:54 +02:00
|
|
|
|
/* Here is the Tensor class, which is nothing else than a simple subclass of
|
|
|
|
|
TwoDMatrix. The unique semantically new member is ‘dim’ which is tensor
|
|
|
|
|
dimension (length of α₁…αₙ). We also declare increment(), decrement() and
|
|
|
|
|
getOffset() methods as pure virtual.
|
2019-01-08 16:09:25 +01:00
|
|
|
|
|
2019-05-06 18:43:54 +02:00
|
|
|
|
We also add members for index begin and index end. This is useful, since
|
|
|
|
|
begin() and end() methods do not return instance but only references, which
|
|
|
|
|
prevent making additional copy of index (for example in for cycles as ‘in !=
|
|
|
|
|
end()’ which would do a copy of index for each cycle). The index begin
|
|
|
|
|
‘in_beg’ is constructed as a sequence of all zeros, and ‘in_end’ is
|
|
|
|
|
constructed from the sequence ‘last’ passed to the constructor, since it
|
|
|
|
|
depends on subclasses. Also we have to say, along what coordinate is the
|
|
|
|
|
multidimensional index. This is used only for initialization of ‘in_end’. */
|
2019-01-08 16:09:25 +01:00
|
|
|
|
|
|
|
|
|
class Tensor : public TwoDMatrix
|
|
|
|
|
{
|
|
|
|
|
public:
|
2023-11-29 19:00:21 +01:00
|
|
|
|
enum class indor
|
|
|
|
|
{
|
|
|
|
|
along_row,
|
|
|
|
|
along_col
|
|
|
|
|
};
|
2019-02-12 17:30:10 +01:00
|
|
|
|
|
2019-05-06 18:43:54 +02:00
|
|
|
|
/* The index represents n-tuple α₁…αₙ. Since its movement is dependent on the
|
|
|
|
|
underlying tensor (with storage and symmetry), we maintain a reference to
|
|
|
|
|
that tensor, we maintain the n-tuple (or coordinates) as IntSequence and
|
|
|
|
|
also we maintain the offset number (column, or row) of the index in the
|
|
|
|
|
tensor. The reference is const, since we do not need to change data
|
|
|
|
|
through the index.
|
2019-02-12 17:30:10 +01:00
|
|
|
|
|
2019-05-06 18:43:54 +02:00
|
|
|
|
Here we require the Tensor to implement increment() and decrement()
|
|
|
|
|
methods, which calculate following and preceding n-tuple. Also, we need to
|
|
|
|
|
calculate offset number from the given coordinates, so the tensor must
|
|
|
|
|
implement method getOffset(). This method is used only in construction of
|
|
|
|
|
the index from the given coordinates. As the index is created, the offset
|
|
|
|
|
is automatically incremented, and decremented together with index. The
|
|
|
|
|
getOffset() method can be relatively computationally complex. This must be
|
|
|
|
|
kept in mind. Also we generally suppose that n-tuple of all zeros is the
|
|
|
|
|
first offset (first columns or row).
|
2019-02-12 17:30:10 +01:00
|
|
|
|
|
2019-05-06 18:43:54 +02:00
|
|
|
|
What follows is a definition of index class, the only interesting point is
|
|
|
|
|
operator==() which decides only according to offset, not according to the
|
|
|
|
|
coordinates. This is useful since there can be more than one of coordinate
|
|
|
|
|
representations of past-the-end index. */
|
2019-02-12 17:30:10 +01:00
|
|
|
|
class index
|
|
|
|
|
{
|
2023-11-29 19:00:21 +01:00
|
|
|
|
const Tensor& tensor;
|
2019-02-12 17:30:10 +01:00
|
|
|
|
int offset;
|
|
|
|
|
IntSequence coor;
|
2023-11-29 19:00:21 +01:00
|
|
|
|
|
2019-02-12 17:30:10 +01:00
|
|
|
|
public:
|
2023-11-29 19:00:21 +01:00
|
|
|
|
index(const Tensor& t, int n) : tensor(t), offset(0), coor(n, 0)
|
2019-02-12 17:30:10 +01:00
|
|
|
|
{
|
|
|
|
|
}
|
2023-11-29 19:00:21 +01:00
|
|
|
|
index(const Tensor& t, IntSequence cr, int c) : tensor(t), offset(c), coor(std::move(cr))
|
2019-02-12 17:30:10 +01:00
|
|
|
|
{
|
|
|
|
|
}
|
2023-11-29 19:00:21 +01:00
|
|
|
|
index(const Tensor& t, IntSequence cr) :
|
|
|
|
|
tensor(t), offset(tensor.getOffset(cr)), coor(std::move(cr))
|
2019-02-12 17:30:10 +01:00
|
|
|
|
{
|
|
|
|
|
}
|
2023-11-29 19:00:21 +01:00
|
|
|
|
index(const index&) = default;
|
|
|
|
|
index(index&&) = default;
|
|
|
|
|
index& operator=(const index&) = delete;
|
|
|
|
|
index& operator=(index&&) = delete;
|
|
|
|
|
index&
|
2019-02-12 17:30:10 +01:00
|
|
|
|
operator++()
|
|
|
|
|
{
|
|
|
|
|
tensor.increment(coor);
|
|
|
|
|
offset++;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
2023-11-29 19:00:21 +01:00
|
|
|
|
index&
|
2019-02-12 17:30:10 +01:00
|
|
|
|
operator--()
|
|
|
|
|
{
|
|
|
|
|
tensor.decrement(coor);
|
|
|
|
|
offset--;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
int
|
|
|
|
|
operator*() const
|
|
|
|
|
{
|
|
|
|
|
return offset;
|
|
|
|
|
}
|
|
|
|
|
bool
|
2023-11-29 19:00:21 +01:00
|
|
|
|
operator==(const index& n) const
|
2019-02-12 17:30:10 +01:00
|
|
|
|
{
|
|
|
|
|
return offset == n.offset;
|
|
|
|
|
}
|
|
|
|
|
bool
|
2023-11-29 19:00:21 +01:00
|
|
|
|
operator!=(const index& n) const
|
2019-02-12 17:30:10 +01:00
|
|
|
|
{
|
|
|
|
|
return offset != n.offset;
|
|
|
|
|
}
|
2023-12-05 15:44:23 +01:00
|
|
|
|
[[nodiscard]] const IntSequence&
|
2019-02-12 17:30:10 +01:00
|
|
|
|
getCoor() const
|
|
|
|
|
{
|
|
|
|
|
return coor;
|
|
|
|
|
}
|
|
|
|
|
void
|
|
|
|
|
print() const
|
|
|
|
|
{
|
|
|
|
|
std::cout << offset << ": ";
|
|
|
|
|
coor.print();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-01-08 16:09:25 +01:00
|
|
|
|
protected:
|
|
|
|
|
const index in_beg;
|
|
|
|
|
const index in_end;
|
|
|
|
|
int dim;
|
2023-11-29 19:00:21 +01:00
|
|
|
|
|
2019-01-08 16:09:25 +01:00
|
|
|
|
public:
|
2023-11-29 19:00:21 +01:00
|
|
|
|
Tensor(indor io, IntSequence last, int r, int c, int d) :
|
2023-11-30 14:37:05 +01:00
|
|
|
|
TwoDMatrix(r, c),
|
|
|
|
|
in_beg(*this, d),
|
|
|
|
|
in_end(*this, std::move(last), (io == indor::along_row) ? r : c),
|
|
|
|
|
dim(d)
|
2019-01-08 16:09:25 +01:00
|
|
|
|
{
|
|
|
|
|
}
|
2023-11-29 19:00:21 +01:00
|
|
|
|
Tensor(indor io, IntSequence first, IntSequence last, int r, int c, int d) :
|
2023-11-30 14:37:05 +01:00
|
|
|
|
TwoDMatrix(r, c),
|
|
|
|
|
in_beg(*this, std::move(first), 0),
|
|
|
|
|
in_end(*this, std::move(last), (io == indor::along_row) ? r : c),
|
|
|
|
|
dim(d)
|
2019-01-08 16:09:25 +01:00
|
|
|
|
{
|
|
|
|
|
}
|
2023-11-29 19:00:21 +01:00
|
|
|
|
Tensor(int first_row, int num, Tensor& t) :
|
|
|
|
|
TwoDMatrix(first_row, num, t), in_beg(t.in_beg), in_end(t.in_end), dim(t.dim)
|
2019-01-08 16:09:25 +01:00
|
|
|
|
{
|
|
|
|
|
}
|
2023-11-29 19:00:21 +01:00
|
|
|
|
Tensor(const Tensor& t) :
|
2023-11-30 14:37:05 +01:00
|
|
|
|
TwoDMatrix(t),
|
|
|
|
|
in_beg(*this, t.in_beg.getCoor(), *(t.in_beg)),
|
|
|
|
|
in_end(*this, t.in_end.getCoor(), *(t.in_end)),
|
|
|
|
|
dim(t.dim)
|
2019-01-08 16:09:25 +01:00
|
|
|
|
{
|
|
|
|
|
}
|
2023-11-29 19:00:21 +01:00
|
|
|
|
Tensor(Tensor&&) = default;
|
2019-02-12 17:30:10 +01:00
|
|
|
|
~Tensor() override = default;
|
|
|
|
|
|
2023-11-29 19:00:21 +01:00
|
|
|
|
Tensor& operator=(const Tensor&) = delete;
|
|
|
|
|
Tensor& operator=(Tensor&&) = delete;
|
2019-02-12 17:30:10 +01:00
|
|
|
|
|
2023-11-29 19:00:21 +01:00
|
|
|
|
virtual void increment(IntSequence& v) const = 0;
|
|
|
|
|
virtual void decrement(IntSequence& v) const = 0;
|
2023-12-05 15:44:23 +01:00
|
|
|
|
[[nodiscard]] virtual int getOffset(const IntSequence& v) const = 0;
|
|
|
|
|
[[nodiscard]] int
|
2019-01-08 16:09:25 +01:00
|
|
|
|
dimen() const
|
|
|
|
|
{
|
|
|
|
|
return dim;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-05 15:44:23 +01:00
|
|
|
|
[[nodiscard]] const index&
|
2019-01-08 16:09:25 +01:00
|
|
|
|
begin() const
|
|
|
|
|
{
|
|
|
|
|
return in_beg;
|
|
|
|
|
}
|
2023-12-05 15:44:23 +01:00
|
|
|
|
[[nodiscard]] const index&
|
2019-01-08 16:09:25 +01:00
|
|
|
|
end() const
|
|
|
|
|
{
|
|
|
|
|
return in_end;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-05-06 18:43:54 +02:00
|
|
|
|
/* Here is an abstraction for unfolded tensor. We provide a pure virtual method
|
|
|
|
|
fold() which returns a new instance of folded tensor of the same symmetry.
|
|
|
|
|
Also we provide static methods for incrementing and decrementing an index
|
|
|
|
|
with full symmetry and general symmetry as defined above. */
|
2019-01-08 16:09:25 +01:00
|
|
|
|
|
|
|
|
|
class FTensor;
|
|
|
|
|
class UTensor : public Tensor
|
|
|
|
|
{
|
|
|
|
|
public:
|
2023-11-29 19:00:21 +01:00
|
|
|
|
UTensor(indor io, IntSequence last, int r, int c, int d) : Tensor(io, std::move(last), r, c, d)
|
2019-01-08 16:09:25 +01:00
|
|
|
|
{
|
|
|
|
|
}
|
2023-11-29 19:00:21 +01:00
|
|
|
|
UTensor(const UTensor&) = default;
|
|
|
|
|
UTensor(UTensor&&) = default;
|
|
|
|
|
UTensor(int first_row, int num, UTensor& t) : Tensor(first_row, num, t)
|
2019-01-08 16:09:25 +01:00
|
|
|
|
{
|
|
|
|
|
}
|
2019-02-12 17:30:10 +01:00
|
|
|
|
~UTensor() override = default;
|
2023-12-05 15:44:23 +01:00
|
|
|
|
[[nodiscard]] virtual std::unique_ptr<FTensor> fold() const = 0;
|
2019-02-12 17:30:10 +01:00
|
|
|
|
|
2023-11-29 19:00:21 +01:00
|
|
|
|
UTensor& operator=(const UTensor&) = delete;
|
|
|
|
|
UTensor& operator=(UTensor&&) = delete;
|
2019-01-08 16:09:25 +01:00
|
|
|
|
|
2023-05-09 21:14:50 +02:00
|
|
|
|
// Ensure that the methods of the parent class are not overloaded
|
|
|
|
|
using Tensor::decrement;
|
|
|
|
|
using Tensor::getOffset;
|
2023-11-29 19:00:21 +01:00
|
|
|
|
using Tensor::increment;
|
2023-05-09 21:14:50 +02:00
|
|
|
|
|
2023-11-29 19:00:21 +01:00
|
|
|
|
static void increment(IntSequence& v, int nv);
|
|
|
|
|
static void decrement(IntSequence& v, int nv);
|
|
|
|
|
static void increment(IntSequence& v, const IntSequence& nvmx);
|
|
|
|
|
static void decrement(IntSequence& v, const IntSequence& nvmx);
|
|
|
|
|
static int getOffset(const IntSequence& v, int nv);
|
|
|
|
|
static int getOffset(const IntSequence& v, const IntSequence& nvmx);
|
2019-01-08 16:09:25 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* This is an abstraction for folded tensor. It only provides a method
|
2019-05-06 18:43:54 +02:00
|
|
|
|
unfold(), which returns the unfolded version of the same symmetry, and
|
2019-01-08 16:09:25 +01:00
|
|
|
|
static methods for decrementing indices.
|
|
|
|
|
|
2019-05-06 18:43:54 +02:00
|
|
|
|
We also provide static methods for decrementing the IntSequence in
|
2019-01-08 16:09:25 +01:00
|
|
|
|
folded fashion and also calculating an offset for a given
|
2019-05-06 18:43:54 +02:00
|
|
|
|
IntSequence. However, this is relatively complex calculation, so
|
2019-01-08 16:09:25 +01:00
|
|
|
|
this should be avoided if possible. */
|
|
|
|
|
|
|
|
|
|
class FTensor : public Tensor
|
|
|
|
|
{
|
|
|
|
|
public:
|
2023-11-29 19:00:21 +01:00
|
|
|
|
FTensor(indor io, IntSequence last, int r, int c, int d) : Tensor(io, std::move(last), r, c, d)
|
2019-01-08 16:09:25 +01:00
|
|
|
|
{
|
|
|
|
|
}
|
2023-11-29 19:00:21 +01:00
|
|
|
|
FTensor(const FTensor&) = default;
|
|
|
|
|
FTensor(FTensor&&) = default;
|
|
|
|
|
FTensor(int first_row, int num, FTensor& t) : Tensor(first_row, num, t)
|
2019-01-08 16:09:25 +01:00
|
|
|
|
{
|
|
|
|
|
}
|
2019-02-12 17:30:10 +01:00
|
|
|
|
~FTensor() override = default;
|
2023-12-05 15:44:23 +01:00
|
|
|
|
[[nodiscard]] virtual std::unique_ptr<UTensor> unfold() const = 0;
|
2019-02-12 17:30:10 +01:00
|
|
|
|
|
2023-11-29 19:00:21 +01:00
|
|
|
|
FTensor& operator=(const FTensor&) = delete;
|
|
|
|
|
FTensor& operator=(FTensor&&) = delete;
|
2019-01-08 16:09:25 +01:00
|
|
|
|
|
2023-05-09 21:14:50 +02:00
|
|
|
|
// Ensure that the methods of the parent class are not overloaded
|
|
|
|
|
using Tensor::decrement;
|
|
|
|
|
using Tensor::getOffset;
|
|
|
|
|
|
2023-11-29 19:00:21 +01:00
|
|
|
|
static void decrement(IntSequence& v, int nv);
|
2019-01-08 16:09:25 +01:00
|
|
|
|
static int
|
2023-11-29 19:00:21 +01:00
|
|
|
|
getOffset(const IntSequence& v, int nv)
|
2019-01-08 16:09:25 +01:00
|
|
|
|
{
|
2019-02-12 17:30:10 +01:00
|
|
|
|
IntSequence vtmp(v);
|
|
|
|
|
return getOffsetRecurse(vtmp, nv);
|
2019-01-08 16:09:25 +01:00
|
|
|
|
}
|
2023-11-29 19:00:21 +01:00
|
|
|
|
|
2019-01-08 16:09:25 +01:00
|
|
|
|
private:
|
2023-11-29 19:00:21 +01:00
|
|
|
|
static int getOffsetRecurse(IntSequence& v, int nv);
|
2019-01-08 16:09:25 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif
|