Dynare++ tensor library: use smart pointers in fold() and unfold() methods
parent
72a445c348
commit
02d6f86487
|
@ -86,10 +86,10 @@ FFSTensor::FFSTensor(const UFSTensor &ut)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Here just make a new instance and return the reference. */
|
/* Here just make a new instance and return the reference. */
|
||||||
UTensor &
|
std::unique_ptr<UTensor>
|
||||||
FFSTensor::unfold() const
|
FFSTensor::unfold() const
|
||||||
{
|
{
|
||||||
return *(new UFSTensor(*this));
|
return std::make_unique<UFSTensor>(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Incrementing is easy. We have to increment by calling static method
|
/* Incrementing is easy. We have to increment by calling static method
|
||||||
|
@ -203,11 +203,10 @@ UFSTensor::UFSTensor(const FFSTensor &ft)
|
||||||
unfoldData();
|
unfoldData();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Here we just return a reference to new instance of folded tensor. */
|
std::unique_ptr<FTensor>
|
||||||
FTensor &
|
|
||||||
UFSTensor::fold() const
|
UFSTensor::fold() const
|
||||||
{
|
{
|
||||||
return *(new FFSTensor(*this));
|
return std::make_unique<FFSTensor>(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
// |UFSTensor| increment and decrement
|
// |UFSTensor| increment and decrement
|
||||||
|
|
|
@ -70,7 +70,7 @@ public:
|
||||||
|
|
||||||
void increment(IntSequence &v) const override;
|
void increment(IntSequence &v) const override;
|
||||||
void decrement(IntSequence &v) const override;
|
void decrement(IntSequence &v) const override;
|
||||||
UTensor&unfold() const override;
|
std::unique_ptr<UTensor> unfold() const override;
|
||||||
Symmetry
|
Symmetry
|
||||||
getSym() const
|
getSym() const
|
||||||
{
|
{
|
||||||
|
@ -114,7 +114,7 @@ public:
|
||||||
|
|
||||||
void increment(IntSequence &v) const override;
|
void increment(IntSequence &v) const override;
|
||||||
void decrement(IntSequence &v) const override;
|
void decrement(IntSequence &v) const override;
|
||||||
FTensor&fold() const override;
|
std::unique_ptr<FTensor> fold() const override;
|
||||||
Symmetry
|
Symmetry
|
||||||
getSym() const
|
getSym() const
|
||||||
{
|
{
|
||||||
|
|
|
@ -272,10 +272,10 @@ FGSTensor::increment(IntSequence &v) const
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Return unfolded version of the tensor. */
|
/* Return unfolded version of the tensor. */
|
||||||
UTensor &
|
std::unique_ptr<UTensor>
|
||||||
FGSTensor::unfold() const
|
FGSTensor::unfold() const
|
||||||
{
|
{
|
||||||
return *(new UGSTensor(*this));
|
return std::make_unique<UGSTensor>(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Here we implement the contraction
|
/* Here we implement the contraction
|
||||||
|
@ -410,10 +410,10 @@ UGSTensor::decrement(IntSequence &v) const
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Return a new instance of folded version. */
|
/* Return a new instance of folded version. */
|
||||||
FTensor &
|
std::unique_ptr<FTensor>
|
||||||
UGSTensor::fold() const
|
UGSTensor::fold() const
|
||||||
{
|
{
|
||||||
return *(new FGSTensor(*this));
|
return std::make_unique<FGSTensor>(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Return an offset of a given index. */
|
/* Return an offset of a given index. */
|
||||||
|
|
|
@ -178,7 +178,7 @@ public:
|
||||||
{
|
{
|
||||||
tdims.decrement(v);
|
tdims.decrement(v);
|
||||||
}
|
}
|
||||||
UTensor&unfold() const override;
|
std::unique_ptr<UTensor> unfold() const override;
|
||||||
const TensorDimens &
|
const TensorDimens &
|
||||||
getDims() const
|
getDims() const
|
||||||
{
|
{
|
||||||
|
@ -242,7 +242,7 @@ public:
|
||||||
|
|
||||||
void increment(IntSequence &v) const override;
|
void increment(IntSequence &v) const override;
|
||||||
void decrement(IntSequence &v) const override;
|
void decrement(IntSequence &v) const override;
|
||||||
FTensor&fold() const override;
|
std::unique_ptr<FTensor> fold() const override;
|
||||||
const TensorDimens &
|
const TensorDimens &
|
||||||
getDims() const
|
getDims() const
|
||||||
{
|
{
|
||||||
|
|
|
@ -58,12 +58,11 @@ UPSTensor::decrement(IntSequence &v) const
|
||||||
UTensor::decrement(v, tdims.getNVX());
|
UTensor::decrement(v, tdims.getNVX());
|
||||||
}
|
}
|
||||||
|
|
||||||
FTensor &
|
std::unique_ptr<FTensor>
|
||||||
UPSTensor::fold() const
|
UPSTensor::fold() const
|
||||||
{
|
{
|
||||||
TL_RAISE("Never should come to this place in UPSTensor::fold");
|
TL_RAISE("Never should come to this place in UPSTensor::fold");
|
||||||
auto *nothing = new FFSTensor(0, 0, 0);
|
return std::make_unique<FFSTensor>(0, 0, 0);
|
||||||
return *nothing;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
|
@ -316,12 +315,11 @@ FPSTensor::decrement(IntSequence &v) const
|
||||||
TL_RAISE("FPSTensor::decrement not implemented");
|
TL_RAISE("FPSTensor::decrement not implemented");
|
||||||
}
|
}
|
||||||
|
|
||||||
UTensor &
|
std::unique_ptr<UTensor>
|
||||||
FPSTensor::unfold() const
|
FPSTensor::unfold() const
|
||||||
{
|
{
|
||||||
TL_RAISE("Unfolding of FPSTensor not implemented");
|
TL_RAISE("Unfolding of FPSTensor not implemented");
|
||||||
auto *nothing = new UFSTensor(0, 0, 0);
|
return std::make_unique<UFSTensor>(0, 0, 0);
|
||||||
return *nothing;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* We only call |calcOffset| of the |PerTensorDimens2|. */
|
/* We only call |calcOffset| of the |PerTensorDimens2|. */
|
||||||
|
|
|
@ -206,7 +206,7 @@ public:
|
||||||
|
|
||||||
void increment(IntSequence &v) const override;
|
void increment(IntSequence &v) const override;
|
||||||
void decrement(IntSequence &v) const override;
|
void decrement(IntSequence &v) const override;
|
||||||
FTensor&fold() const override;
|
std::unique_ptr<FTensor> fold() const override;
|
||||||
|
|
||||||
int getOffset(const IntSequence &v) const override;
|
int getOffset(const IntSequence &v) const override;
|
||||||
void addTo(FGSTensor &out) const;
|
void addTo(FGSTensor &out) const;
|
||||||
|
@ -370,7 +370,7 @@ public:
|
||||||
|
|
||||||
void increment(IntSequence &v) const override;
|
void increment(IntSequence &v) const override;
|
||||||
void decrement(IntSequence &v) const override;
|
void decrement(IntSequence &v) const override;
|
||||||
UTensor&unfold() const override;
|
std::unique_ptr<UTensor> unfold() const override;
|
||||||
|
|
||||||
int getOffset(const IntSequence &v) const override;
|
int getOffset(const IntSequence &v) const override;
|
||||||
void addTo(FGSTensor &out) const;
|
void addTo(FGSTensor &out) const;
|
||||||
|
|
|
@ -27,10 +27,10 @@ FRTensor::FRTensor(const URTensor &ut)
|
||||||
|
|
||||||
/* Here just make a new instance and return the reference. */
|
/* Here just make a new instance and return the reference. */
|
||||||
|
|
||||||
UTensor &
|
std::unique_ptr<UTensor>
|
||||||
FRTensor::unfold() const
|
FRTensor::unfold() const
|
||||||
{
|
{
|
||||||
return *(new URTensor(*this));
|
return std::make_unique<URTensor>(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Incrementing is easy. The same as for |FFSTensor|. */
|
/* Incrementing is easy. The same as for |FFSTensor|. */
|
||||||
|
@ -77,10 +77,10 @@ URTensor::URTensor(const FRTensor &ft)
|
||||||
|
|
||||||
/* Here we just return a reference to new instance of folded tensor. */
|
/* Here we just return a reference to new instance of folded tensor. */
|
||||||
|
|
||||||
FTensor &
|
std::unique_ptr<FTensor>
|
||||||
URTensor::fold() const
|
URTensor::fold() const
|
||||||
{
|
{
|
||||||
return *(new FRTensor(*this));
|
return std::make_unique<FRTensor>(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Here we just call |UTensor| respective static methods. */
|
/* Here we just call |UTensor| respective static methods. */
|
||||||
|
@ -163,10 +163,10 @@ URSingleTensor::URSingleTensor(const ConstVector &v, int d)
|
||||||
/* Here we construct |FRSingleTensor| from |URSingleTensor| and return
|
/* Here we construct |FRSingleTensor| from |URSingleTensor| and return
|
||||||
its reference. */
|
its reference. */
|
||||||
|
|
||||||
FTensor &
|
std::unique_ptr<FTensor>
|
||||||
URSingleTensor::fold() const
|
URSingleTensor::fold() const
|
||||||
{
|
{
|
||||||
return *(new FRSingleTensor(*this));
|
return std::make_unique<FRSingleTensor>(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
// |FRSingleTensor| conversion from unfolded
|
// |FRSingleTensor| conversion from unfolded
|
||||||
|
|
|
@ -62,7 +62,7 @@ public:
|
||||||
|
|
||||||
void increment(IntSequence &v) const override;
|
void increment(IntSequence &v) const override;
|
||||||
void decrement(IntSequence &v) const override;
|
void decrement(IntSequence &v) const override;
|
||||||
FTensor&fold() const override;
|
std::unique_ptr<FTensor> fold() const override;
|
||||||
|
|
||||||
int getOffset(const IntSequence &v) const override;
|
int getOffset(const IntSequence &v) const override;
|
||||||
int
|
int
|
||||||
|
@ -98,7 +98,7 @@ public:
|
||||||
|
|
||||||
void increment(IntSequence &v) const override;
|
void increment(IntSequence &v) const override;
|
||||||
void decrement(IntSequence &v) const override;
|
void decrement(IntSequence &v) const override;
|
||||||
UTensor&unfold() const override;
|
std::unique_ptr<UTensor> unfold() const override;
|
||||||
|
|
||||||
int
|
int
|
||||||
nvar() const
|
nvar() const
|
||||||
|
@ -138,7 +138,7 @@ public:
|
||||||
= default;
|
= default;
|
||||||
~URSingleTensor()
|
~URSingleTensor()
|
||||||
override = default;
|
override = default;
|
||||||
FTensor&fold() const override;
|
std::unique_ptr<FTensor> fold() const override;
|
||||||
};
|
};
|
||||||
|
|
||||||
/* This class represents one column row-oriented tensor. The only way
|
/* This class represents one column row-oriented tensor. The only way
|
||||||
|
|
|
@ -49,6 +49,7 @@
|
||||||
#include "int_sequence.hh"
|
#include "int_sequence.hh"
|
||||||
#include "twod_matrix.hh"
|
#include "twod_matrix.hh"
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
/* Here is the |Tensor| class, which is nothing else than a simple subclass
|
/* Here is the |Tensor| class, which is nothing else than a simple subclass
|
||||||
|
@ -240,7 +241,7 @@ public:
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
~UTensor() override = default;
|
~UTensor() override = default;
|
||||||
virtual FTensor &fold() const = 0;
|
virtual std::unique_ptr<FTensor> fold() const = 0;
|
||||||
|
|
||||||
UTensor &operator=(const UTensor &) = delete;
|
UTensor &operator=(const UTensor &) = delete;
|
||||||
UTensor &operator=(UTensor &&) = delete;
|
UTensor &operator=(UTensor &&) = delete;
|
||||||
|
@ -276,7 +277,7 @@ public:
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
~FTensor() override = default;
|
~FTensor() override = default;
|
||||||
virtual UTensor &unfold() const = 0;
|
virtual std::unique_ptr<UTensor> unfold() const = 0;
|
||||||
|
|
||||||
FTensor &operator=(const FTensor &) = delete;
|
FTensor &operator=(const FTensor &) = delete;
|
||||||
FTensor &operator=(FTensor &&) = delete;
|
FTensor &operator=(FTensor &&) = delete;
|
||||||
|
|
|
@ -196,8 +196,8 @@ TestRunnable::index_offset(const Symmetry &s, const IntSequence &nvs)
|
||||||
bool
|
bool
|
||||||
TestRunnable::fold_unfold(const FTensor *folded)
|
TestRunnable::fold_unfold(const FTensor *folded)
|
||||||
{
|
{
|
||||||
UTensor *unfolded = &(folded->unfold());
|
auto unfolded = folded->unfold();
|
||||||
FTensor *folded2 = &(unfolded->fold());
|
auto folded2 = unfolded->fold();
|
||||||
folded2->add(-1.0, *folded);
|
folded2->add(-1.0, *folded);
|
||||||
double normInf = folded2->getNormInf();
|
double normInf = folded2->getNormInf();
|
||||||
double norm1 = folded2->getNorm1();
|
double norm1 = folded2->getNorm1();
|
||||||
|
@ -207,8 +207,6 @@ TestRunnable::fold_unfold(const FTensor *folded)
|
||||||
<< "\tdifference norm1: " << norm1 << '\n';
|
<< "\tdifference norm1: " << norm1 << '\n';
|
||||||
|
|
||||||
delete folded;
|
delete folded;
|
||||||
delete unfolded;
|
|
||||||
delete folded2;
|
|
||||||
|
|
||||||
return normInf < 1.0e-15;
|
return normInf < 1.0e-15;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue