Commit 85bdbe25 authored by Philipp  Trunschke's avatar Philipp Trunschke

allow uq_ra_adf to take precomputed positions

parent 2466ba66
Pipeline #1044 passed with stages
in 7 minutes and 38 seconds
......@@ -37,5 +37,7 @@ namespace xerus { namespace uq {
TTTensor uq_ra_adf(const UQMeasurementSet& _measurments, const PolynomBasis _basisType, const std::vector<size_t>& _dimensions, const double _targetEps = 1e-8, const size_t _maxItr = 0);
TTTensor uq_ra_adf(const std::vector<std::vector<Tensor>>& _positions, const std::vector<Tensor>& _solutions, const std::vector<size_t>& _dimensions, const double _targetEps = 1e-8, const size_t _maxItr = 0);
TTTensor uq_ra_adf_iv(TTTensor& _x, const UQMeasurementSet& _measurments, const PolynomBasis _basisType, const double _targetEps = 1e-8, const size_t _maxItr = 0);
}}
......@@ -94,6 +94,26 @@ namespace xerus { namespace uq { namespace impl_uqRaAdf {
}
static std::vector<std::vector<Tensor>> transpose_positions(const TTTensor& _x, const std::vector<std::vector<Tensor>>& _positions, const std::vector<Tensor>& _solutions) {
REQUIRE(_positions.size() == _solutions.size(), "Incompatible positions and solutions vector");
for(size_t sample=0; sample < _positions.size(); ++sample) {
REQUIRE(_positions[sample].size() == _x.degree()-1, "Invalid measurement");
}
std::vector<std::vector<Tensor>> positions(_x.degree());
for(size_t corePosition=1; corePosition < _x.degree(); ++corePosition) {
positions[corePosition].reserve(_positions.size());
for(size_t sample=0; sample < _positions.size(); ++sample) {
REQUIRE(_positions[sample][corePosition-1].dimensions.size() == 1, "Invalid measurement component");
REQUIRE(_positions[sample][corePosition-1].size == _x.dimensions[corePosition], "Invalid measurement component");
positions[corePosition].push_back(_positions[sample][corePosition-1]);
}
}
return positions;
}
void shuffle_sets() {
sets = std::vector<std::vector<size_t>>(P);
controlSet.clear();
......@@ -156,6 +176,27 @@ namespace xerus { namespace uq { namespace impl_uqRaAdf {
}
InternalSolver(TTTensor& _x, const std::vector<std::vector<Tensor>>& _positions, const std::vector<Tensor>& _solutions, const size_t _maxItr, const double _targetEps, const double _initalRankEps) :
N(_solutions.size()),
d(_x.degree()),
targetResidual(_targetEps),
maxIterations(_maxItr),
positions(transpose_positions(_x, _positions, _solutions)),
solutions(_solutions),
outX(_x),
x(_x, 0, P),
rightStack(d, std::vector<Tensor>(N)),
leftIsStack(d, std::vector<Tensor>(N)),
leftOughtStack(d, std::vector<Tensor>(N)),
rankEps(_initalRankEps),
prevRanks(tracking+1, _x.ranks())
{
LOG(uqADF, "Set size: " << N);
shuffle_sets();
}
void calc_left_stack(const size_t _position) {
REQUIRE(_position+1 < d, "Invalid corePosition");
......@@ -520,6 +561,29 @@ namespace xerus { namespace uq { namespace impl_uqRaAdf {
return x;
}
TTTensor uq_ra_adf(const std::vector<std::vector<Tensor>>& _positions, const std::vector<Tensor>& _solutions, const std::vector<size_t>& _dimensions, const double _targetEps, const size_t _maxItr) {
REQUIRE(_positions.size() == _solutions.size(), "Invalid measurments");
REQUIRE(_dimensions.front() == _solutions.front().size, "Inconsitent spacial dimension");
LOG(UQ, "Calculating Average as start.");
TTTensor x(_dimensions);
Tensor mean = sample_mean(_solutions);
// Set mean
mean.reinterpret_dimensions({1, x.dimensions[0], 1});
x.set_component(0, mean);
for(size_t k = 1; k < x.degree(); ++k) {
x.set_component(k, Tensor::dirac({1, x.dimensions[k], 1}, 0));
}
x.assume_core_position(0);
impl_uqRaAdf::InternalSolver<2> solver(x, _positions, _solutions, _maxItr, _targetEps, 1e-1);
solver.solve();
return x;
}
TTTensor uq_ra_adf_iv(TTTensor& _x, const UQMeasurementSet& _measurments, const PolynomBasis _basisType, const double _targetEps, const size_t _maxItr) {
REQUIRE(_measurments.parameterVectors.size() == _measurments.solutions.size(), "Invalid measurments");
REQUIRE(_x.dimensions.front() == _measurments.solutions.front().size, "Inconsitent spacial dimension");
......
......@@ -181,6 +181,11 @@ void expose_recoveryAlgorithms() {
}, (arg("measurements"), arg("polynombasis"), arg("dimensions"), arg("targeteps"), arg("maxitr"))
);
def("uq_ra_adf", +[](const std::vector<std::vector<Tensor>>& _positions, const std::vector<Tensor>& _solutions, const std::vector<size_t>& _dimensions, const double _targetEps, const size_t _maxItr){
return uq::uq_ra_adf(_positions, _solutions, _dimensions, _targetEps, _maxItr);
}, (arg("positions"), arg("solutions"), arg("dimensions"), arg("targeteps"), arg("maxitr"))
);
def("uq_ra_adf_iv", +[](TTTensor& _x, const uq::UQMeasurementSet& _measurements, const uq::PolynomBasis _basisType, const double _targetEps, const size_t _maxItr){
return uq::uq_ra_adf_iv(_x, _measurements, _basisType, _targetEps, _maxItr);
}, (arg("initial guess"), arg("measurements"), arg("polynombasis"), arg("targeteps"), arg("maxitr"))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment