44 result.create_random_positions(_numMeasurements, _dimensions);
52 result.create_random_positions(_numMeasurements, _solution.
dimensions);
59 result.create_random_positions(_numMeasurements, _solution.
dimensions);
66 result.create_random_positions(_numMeasurements, _dimensions);
84 REQUIRE(
positions.empty() || _position.size() ==
positions.back().size(),
"Given _position has incorrect degree " << _position.size() <<
". Expected " <<
positions.back().size() <<
".");
90 const auto comperator = [](
const std::vector<size_t>& _lhs,
const std::vector<size_t>& _rhs) {
91 REQUIRE(_lhs.size() == _rhs.size(),
"Inconsistent degrees in measurment positions.");
92 for (
size_t i = 0; i < _lhs.size(); ++i) {
93 if (_lhs[i] < _rhs[i]) {
return true; }
94 if (_lhs[i] > _rhs[i]) {
return false; }
109 const auto cSize =
size();
111 for(
size_t i = 0; i < cSize; ++i) {
114 return std::sqrt(norm);
119 const auto cSize =
size();
120 for(
size_t i = 0; i < cSize; ++i) {
126 REQUIRE(_solution.
degree() ==
degree(),
"Degrees of solution and measurements must match!");
127 std::vector<TensorNetwork> stack(
degree()+1);
128 stack[0] = _solution;
131 const auto cSize =
size();
132 for(
size_t j = 0; j < cSize; ++j) {
133 size_t rebuildIndex = 0;
137 for(; rebuildIndex <
degree(); ++rebuildIndex) {
145 for(
size_t i = rebuildIndex; i <
degree(); ++i) {
146 stack[i+1] = stack[i];
148 stack[i+1].reduce_representation();
157 const auto cSize =
size();
158 for(
size_t i = 0; i < cSize; ++i) {
165 const auto cSize =
size();
166 double error = 0.0, norm = 0.0;
167 for(
size_t i = 0; i < cSize; ++i) {
171 return std::sqrt(error/norm);
176 const auto cSize =
size();
177 double error = 0.0, norm = 0.0;
179 REQUIRE(_solution.
degree() ==
degree(),
"Degrees of solution and measurements must match!");
180 std::vector<TensorNetwork> stack(
degree()+1);
181 stack[0] = _solution;
184 for(
size_t j = 0; j < cSize; ++j) {
185 size_t rebuildIndex = 0;
189 for(; rebuildIndex <
degree(); ++rebuildIndex) {
197 for(
size_t i = rebuildIndex; i <
degree(); ++i) {
198 stack[i+1] = stack[i];
200 stack[i+1].reduce_representation();
206 return std::sqrt(error/norm);
211 const auto cSize =
size();
212 double error = 0.0, norm = 0.0;
213 for(
size_t i = 0; i < cSize; ++i) {
217 return std::sqrt(error/norm);
222 void SinglePointMeasurementSet::create_random_positions(
const size_t _numMeasurements,
const std::vector<size_t>& _dimensions) {
223 using ::xerus::misc::operator<<;
224 XERUS_REQUIRE(misc::product(_dimensions) >= _numMeasurements,
"It's impossible to perform as many measurements as requested. " << _numMeasurements <<
" > " << _dimensions);
227 std::vector<std::uniform_int_distribution<size_t>> indexDist;
228 for (
size_t i = 0; i < _dimensions.size(); ++i) {
229 indexDist.emplace_back(0, _dimensions[i]-1);
232 std::set<size_t> measuredPositions;
233 std::vector<size_t> multIdx(_dimensions.size());
234 while (
positions.size() < _numMeasurements) {
236 for (
size_t i = 0; i < _dimensions.size(); ++i) {
238 pos *= _dimensions[i]; pos += multIdx[i];
240 if (!misc::contains(measuredPositions, pos)) {
241 measuredPositions.insert(pos);
259 REQUIRE(_other.
degree() == _dimensions.size(),
"Inconsistent degrees.");
260 std::vector<Tensor> zeroPosition; zeroPosition.reserve(_dimensions.size());
261 for(
size_t j = 0; j < _dimensions.size(); ++j) {
262 zeroPosition.emplace_back(
Tensor({_dimensions[j]}));
265 for(
size_t i = 0; i < _other.
size(); ++i) {
267 for(
size_t j = 0; j < _other.
degree(); ++j) {
275 result.create_random_positions(_numMeasurements, _dimensions);
283 result.create_random_positions(_numMeasurements, _solution.
dimensions);
290 result.create_random_positions(_numMeasurements, _solution.
dimensions);
297 result.create_random_positions(_numMeasurements, _dimensions);
317 for(
size_t i = 0; i <
degree(); ++i) {
318 REQUIRE(
positions.back()[i].dimensions == _position[i].dimensions,
"Inconsitend dimensions obtained.");
321 for (
const Tensor& t : _position) {
322 REQUIRE(t.degree() == 1,
"Illegal measurement.");
331 const auto comperator = [](
const std::vector<Tensor>& _lhs,
const std::vector<Tensor>& _rhs) {
332 REQUIRE(_lhs.size() == _rhs.size(),
"Inconsistent degrees in measurment positions.");
333 for (
size_t i = 0; i < _lhs.size(); ++i) {
335 if(res == -1) {
return true; }
336 if(res == 1) {
return false; }
350 for(
size_t i = 0; i <
size(); ++i) {
351 for(
size_t j = 0; j <
degree(); ++j) {
352 const auto norm =
positions[i][j].frob_norm();
361 const auto cSize =
size();
363 for(
size_t i = 0; i < cSize; ++i) {
366 return std::sqrt(norm);
371 REQUIRE(_solution.
degree() ==
degree(),
"Degrees of solution and measurements must match!");
372 std::vector<Tensor> stack(
degree()+1);
373 stack[0] = _solution;
375 const auto cSize =
size();
376 for(
size_t j = 0; j < cSize; ++j) {
377 size_t rebuildIndex = 0;
381 for(; rebuildIndex <
degree(); ++rebuildIndex) {
389 for(
size_t i = rebuildIndex; i <
degree(); ++i) {
398 REQUIRE(_solution.
degree() ==
degree(),
"Degrees of solution and measurements must match!");
399 std::vector<TensorNetwork> stack(
degree()+1);
400 stack[0] = _solution;
405 const auto cSize =
size();
406 for(
size_t j = 0; j < cSize; ++j) {
407 size_t rebuildIndex = 0;
411 for(; rebuildIndex <
degree(); ++rebuildIndex) {
419 for(
size_t i = rebuildIndex; i <
degree(); ++i) {
420 stack[i+1](k&0) =
positions[j][i](l) * stack[i](l, k&1);
421 stack[i+1].reduce_representation();
430 const auto cSize =
size();
431 for(
size_t i = 0; i < cSize; ++i) {
438 REQUIRE(_solution.
degree() ==
degree(),
"Degrees of solution and measurements must match!");
439 const auto cSize =
size();
440 double error = 0.0, norm = 0.0;
442 std::vector<Tensor> stack(
degree()+1);
443 stack[0] = _solution;
445 for(
size_t j = 0; j < cSize; ++j) {
446 size_t rebuildIndex = 0;
450 for(; rebuildIndex <
degree(); ++rebuildIndex) {
458 for(
size_t i = rebuildIndex; i <
degree(); ++i) {
466 return std::sqrt(error/norm);
471 REQUIRE(_solution.
degree() ==
degree(),
"Degrees of solution and measurements must match!");
472 const auto cSize =
size();
475 double error = 0.0, norm = 0.0;
477 #pragma omp parallel reduction(+:error, norm) 479 std::vector<TensorNetwork> stack(
degree()+1);
480 stack[
degree()] = _solution;
481 stack[
degree()].reduce_representation();
485 for(
size_t j = 0; j < cSize; ++j) {
486 size_t unchangedModes = 0;
490 for(; unchangedModes <
degree(); ++unchangedModes) {
493 }
else { init =
false; }
496 for(
long i =
long(
degree())-1-long(unchangedModes); i >= 0; --i) {
497 stack[size_t(i)](k&0) = stack[
size_t(i+1)](k&1, l) *
positions[j][
size_t(i)](l);
498 stack[size_t(i)].reduce_representation();
506 return std::sqrt(error/norm);
511 const auto cSize =
size();
512 double error = 0.0, norm = 0.0;
513 for(
size_t i = 0; i < cSize; ++i) {
517 return std::sqrt(error/norm);
522 void RankOneMeasurementSet::create_random_positions(
const size_t _numMeasurements,
const std::vector<size_t>& _dimensions) {
523 using ::xerus::misc::operator<<;
524 XERUS_REQUIRE(misc::product(_dimensions) >= _numMeasurements,
"It's impossible to perform as many measurements as requested. " << _numMeasurements <<
" > " << _dimensions);
527 std::vector<std::uniform_int_distribution<size_t>> indexDist;
528 for (
size_t i = 0; i < _dimensions.size(); ++i) {
529 indexDist.emplace_back(0, _dimensions[i]-1);
532 std::vector<Tensor> randOnePosition(_dimensions.size());
533 while (
positions.size() < _numMeasurements) {
534 for (
size_t i = 0; i < _dimensions.size(); ++i) {
553 for(
size_t k = 0; k < _a.
size; ++k) {
554 if (_a.
cat(k) < _b.
cat(k)) {
return 1; }
555 if (_a.
cat(k) > _b.
cat(k)) {
return -1; }
565 auto itrA = dataA.begin();
566 auto itrB = dataB.begin();
568 while(itrA != dataA.end() && itrB != dataB.end()) {
569 if(itrA->first == itrB->first) {
570 if(itrA->second < itrB->second) {
573 if(itrA->second > itrB->second) {
577 }
else if(itrA->first < itrB->first) {
578 if(itrA->second < 0.0) {
581 if(itrA->second > 0.0) {
586 if(0.0 < itrB->second) {
589 if(0.0 > itrB->second) {
596 while(itrA != dataA.end()) {
597 if(itrA->second < 0.0) {
600 if(itrA->second > 0.0) {
606 while(itrB != dataB.end()) {
607 if(0.0 < itrB->second) {
610 if(0.0 > itrB->second) {
void simultaneous_sort(std::vector< KeyType > &_keyVector, std::vector< DataType > &_dataVector, Comparator _comp)
Header file for CHECK and REQUIRE macros.
void sort(const bool _positionsOnly=false)
static RankOneMeasurementSet random(const size_t _numMeasurements, const std::vector< size_t > &_dimensions)
size_t degree() const
Returns the degree of the tensor.
double test(const Tensor &_solution) const
Header file for the TTNetwork class (and thus TTTensor and TTOperator).
Header file for the Index class.
static Tensor XERUS_warn_unused random(DimensionTuple _dimensions, distribution &_dist=xerus::misc::defaultNormalDistribution, generator &_rnd=xerus::misc::randomEngine)
Constructs a dense Tensor with the given dimensions and uses the given random generator and distribut...
Very general class used to represent arbitary tensor networks.
Class used to represent a single point measurments.
size_t size
Size of the Tensor – always equal to the product of the dimensions.
DimensionTuple dimensions
Vector containing the individual dimensions of the tensor.
void measure(const Tensor &_solution)
The main namespace of xerus.
Class that handles simple (non-decomposed) tensors in a dense or sparse representation.
value_t cat(const size_t _position) const
Unsanitized read access to a single entry.
std::map< size_t, value_t > & get_unsanitized_sparse_data()
Gives access to the internal sparse map, without any checks.
double test(const Tensor &_solution) const
std::vector< value_t > measuredValues
Header file for the different measurment classes.
Header file for some extended sorting functions.
void reduce_representation()
Contracts all nodes that are joined by a full-rank edge.
size_t degree() const
Gets the degree of the TensorNetwork.
void add(const std::vector< Tensor > &_position, const value_t _measuredValue)
Header file for the Tensor class.
void contract(Tensor &_result, const Tensor &_lhs, const bool _lhsTrans, const Tensor &_rhs, const bool _rhsTrans, const size_t _numModes)
Low-level contraction between Tensors.
bool has_factor() const
Checks whether the tensor has a non-trivial global scaling factor.
void sort(const bool _positionsOnly)
int comp(const Tensor &_a, const Tensor &_b)
#define XERUS_REQUIRE(condition, message)
Checks whether condition is true. logs an error otherwise via XERUS_LOG(error, message).
Header file for comfort functions and macros that should not be exported in the library.
constexpr T sqr(const T &_a) noexcept
: Calculates _a*_a.
double value_t
The type of values to be used by xerus.
Class used to represent indices that can be used to write tensor calculations in index notation...
value_t frob_norm() const
bool is_dense() const
Returns whether the current representation is dense.
std::vector< std::vector< size_t > > positions
bool approx_equal(const xerus::Tensor &_a, const xerus::Tensor &_b, const xerus::value_t _eps=EPSILON)
Checks whether two tensors are approximately equal.
static SinglePointMeasurementSet random(const size_t _numMeasurements, const std::vector< size_t > &_dimensions)
thread_local std::mt19937_64 randomEngine
Header file for some additional math functions.
RankOneMeasurementSet()=default
void measure(const Tensor &_solution)
void add(std::vector< size_t > _position, const value_t _measuredValue)
Header file for the TensorNetwork class.
std::vector< value_t > measuredValues
value_t frob_norm() const
Header file for the IndexedTensor class.
std::vector< size_t > dimensions
Dimensions of the external indices, i.e. the dimensions of the tensor represented by the network...