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