xerus
a general purpose tensor library
largestEntry.cpp
Go to the documentation of this file.
1 
3 
4 namespace xerus {
5  template<bool isOperator>
6  size_t find_largest_entry(const TTNetwork<isOperator> &_T, const double _accuracy, const value_t _lowerBound) {
8 
9  // There is actual work to be done
10  if(misc::sum(_T.ranks()) >= _T.degree()) {
11  const double alpha = _accuracy;
12 
13  TTNetwork<isOperator> X = _T;
14  X.round(size_t(1));
15  double Xn = std::max(_T[find_largest_entry(X, 0.0, 0.0)], _lowerBound);
16  double tau = (1-alpha)*alpha*Xn*Xn/(2.0*double(_T.degree()-1));
17 
18  X = _T;
19  while(misc::sum(X.ranks()) >= _T.degree()) {
20  X = entrywise_product(X, X);
21 
22  X.soft_threshold(tau, true);
23 
25  Y.round(1);
26  const size_t yMaxPos = find_largest_entry(Y, 0.0, 0.0);
27 
28  Xn = std::max(X[yMaxPos], (1-(1-alpha)*alpha/2.0)*Xn*Xn);
29 
30  const double fNorm = X.frob_norm();
31  Xn /= fNorm;
32  X /= fNorm;
33  tau = (1-alpha)*alpha*Xn*Xn/(2.0*double(_T.degree()-1));
34  }
35  return find_largest_entry(X, 0.0, 0.0);
36  }
37  // We are already rank one
38  const size_t numComponents = _T.degree()/(isOperator?2:1);
39  size_t position = 0;
40  size_t factor = misc::product(_T.dimensions);
41  for(size_t c = 0; c < numComponents; ++c) {
42  const size_t localSize = isOperator ? _T.dimensions[c]*_T.dimensions[numComponents+c] : _T.dimensions[c];
43  factor /= localSize;
44 
45  size_t maxPos = 0;
46  for(size_t i = 1; i < localSize; ++i) {
47  if(std::abs(_T.get_component(c)[i]) > std::abs(_T.get_component(c)[maxPos])) {
48  maxPos = i;
49  }
50  }
51  position += maxPos*factor;
52  }
53  return position;
54  }
55 
56  template size_t find_largest_entry(const TTNetwork<true> &, double, value_t);
57  template size_t find_largest_entry(const TTNetwork<false> &, double, value_t);
58 } // namespace xerus
size_t find_largest_entry(const TTNetwork< isOperator > &_T, double _accuracy, value_t _lowerBound=0.0)
Finds the position of the approximately largest entry.
Definition: largestEntry.cpp:6
Header file for the TT largest entry algorithm.
void soft_threshold(const double _tau, const bool _preventZero=false)
Applies the soft threshholding operation to all ranks.
Definition: ttNetwork.cpp:711
Specialized TensorNetwork class used to represent TTTensor and TToperators.
The main namespace of xerus.
Definition: basic.h:37
size_t degree() const
Gets the degree of the TensorNetwork.
std::vector< size_t > ranks() const
Gets the ranks of the TTNetwork.
Definition: ttNetwork.cpp:717
double value_t
The type of values to be used by xerus.
Definition: basic.h:43
virtual value_t frob_norm() const override
Calculates the frobenious norm of the TensorNetwork.
Definition: ttNetwork.cpp:782
virtual void require_correct_format() const override
Tests whether the network resembles that of a TTTensor and checks consistency with the underlying ten...
Definition: ttNetwork.cpp:290
Tensor entrywise_product(const Tensor &_A, const Tensor &_B)
calculates the entrywise product of two Tensors
Definition: tensor.cpp:1708
void round(const std::vector< size_t > &_maxRanks, const double _eps=EPSILON)
Reduce all ranks up to a given accuracy and maximal number.
Definition: ttNetwork.cpp:644
const Tensor & get_component(const size_t _idx) const
Read access to a specific component of the TT decomposition.
Definition: ttNetwork.cpp:464
std::vector< size_t > dimensions
Dimensions of the external indices, i.e. the dimensions of the tensor represented by the network...