xerus
a general purpose tensor library
decompositionAls.cpp
Go to the documentation of this file.
1 // Xerus - A General Purpose Tensor Library
2 // Copyright (C) 2014-2017 Benjamin Huber and Sebastian Wolf.
3 //
4 // Xerus is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as published
6 // by the Free Software Foundation, either version 3 of the License,
7 // or (at your option) any later version.
8 //
9 // Xerus is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
13 //
14 // You should have received a copy of the GNU Affero General Public License
15 // along with Xerus. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // For further information on Xerus visit https://libXerus.org
18 // or contact us at contact@libXerus.org.
19 
26 #include <xerus/basic.h>
27 #include <xerus/index.h>
29 #include <xerus/tensorNetwork.h>
30 
33 
34 namespace xerus {
35 
36  void decomposition_als(TTTensor& _x, const Tensor& _b, const double _eps, const size_t _maxIterations) {
37  Index k, iU, iX, iL, rU, rL;
38  Tensor newXi;
39 
40  double lastResidual = frob_norm(Tensor(_x) - _b);
41 // LOG(bla, "Initial residual " << lastResidual);
42 
43  for(size_t iteration = 0; iteration < _maxIterations; ++iteration) {
44  // Move right
45  for(size_t pos = 0; pos < _x.degree(); ++pos) { XERUS_REQUIRE_TEST;
46 // LOG(bla, "Optimizing position " << pos);
47  _x.move_core(pos);
48  std::pair<TensorNetwork, TensorNetwork> split = _x.chop(pos);
49  _x.component(pos)(rU, iX, rL) = split.first(iU&1, rU)*split.second(rL, iL&1)*_b(iU^pos, iX, iL&(pos+1));
50  }
51 
52  // Move left
53  for(size_t pos = _x.degree()-2; pos > 0; --pos) { XERUS_REQUIRE_TEST;
54 // LOG(bla, "Optimizing position " << pos);
55  _x.move_core(pos);
56  std::pair<TensorNetwork, TensorNetwork> split = _x.chop(pos);
57  _x.component(pos)(rU, iX, rL) = split.first(iU&1, rU)*split.second(rL, iL&1)*_b(iU^pos, iX, iL&(pos+1));
58  }
59 
60  double residual = frob_norm(Tensor(_x) - _b);
61 // LOG(bla, "New residual " << residual << ", residual change " << (lastResidual-residual)/residual);
62  if(residual < EPSILON || (lastResidual-residual)/residual < _eps) { return; }
63  lastResidual = residual;
64  }
65  }
66 } // namespace xerus
std::pair< TensorNetwork, TensorNetwork > chop(const size_t _position) const
Splits the TTNetwork into two parts by removing the node.
Definition: ttNetwork.cpp:515
Header file for the Index class.
#define XERUS_REQUIRE_TEST
Definition: check.h:51
Header file for the IndexedTensorMoveable class.
Header file defining lists of indexed tensors.
void decomposition_als(TTTensor &_x, const Tensor &_b, const double _eps=EPSILON, const size_t _maxIterations=1000)
Tensor & component(const size_t _idx)
Complete access to a specific component of the TT decomposition.
Definition: ttNetwork.cpp:457
Header file for the classes defining factorisations of Tensors.
The main namespace of xerus.
Definition: basic.h:37
Class that handles simple (non-decomposed) tensors in a dense or sparse representation.
Definition: tensor.h:70
size_t degree() const
Gets the degree of the TensorNetwork.
void move_core(const size_t _position, const bool _keepRank=false)
Move the core to a new position.
Definition: ttNetwork.cpp:582
Header file for the decomposition ALS algorithm. This is likely temporary.
constexpr const value_t EPSILON
The default epsilon value in xerus.
Definition: basic.h:50
static XERUS_force_inline value_t frob_norm(const Tensor &_tensor)
Calculates the frobenius norm of the given tensor.
Definition: tensor.h:931
Header file for shorthand notations that are xerus specific but used throughout the library...
Class used to represent indices that can be used to write tensor calculations in index notation...
Definition: index.h:43
Header file for the TensorNetwork class.