xerus
a general purpose tensor library
indexedTensor_tensor_solve.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 
25 #include <xerus/misc/internal.h>
27 
28 #include <xerus/index.h>
29 #include <xerus/indexedTensor.h>
31 #include <xerus/tensor.h>
32 
33 namespace xerus {
35  _A.assign_indices();
36  _b.assign_indices();
37 
38  size_t extraDims = 0;
39  std::vector<Index> orderA;
40  std::vector<Index> orderB;
41  std::vector<Index> orderX;
42 
43  // If possible we don't want to reorder A, so first divide A into those shared with b and x.
44  for(const Index& idx : _A.indices) {
45  if(misc::contains(_b.indices, idx)) {
46  orderA.push_back(idx);
47  } else {
48  orderX.push_back(idx);
49  }
50  }
51 
52  // This far the dimensions of A and B coincide
53  orderB = orderA;
54 
55  // Add the second part of indices in the order obtained for X
56  orderA.insert(orderA.end(), orderX.begin(), orderX.end());
57 
58  // Now complete indices of b and x with those not shared with A ( in order of b as we don't want to reorder b if possible).
59  for(const Index& idx : _b.indices) {
60  if(!misc::contains(_A.indices, idx)) {
61  orderB.push_back(idx);
62  orderX.push_back(idx);
63  for(size_t i = 0; i < idx.span; ++i) {
64  extraDims++;
65  }
66  }
67  }
68 
69  // If indices coincide no reordering occours (only shared data pointer is set).
70  Tensor reorderedA, reorderedB;
71  reorderedA(orderA) = std::move(_A);
72  reorderedB(orderB) = std::move(_b);
73 
74  internal::IndexedTensorMoveable<Tensor> tmpX(new Tensor(), std::move(orderX));
75 
76  //solve_least_squares(*tmpX.tensorObject, reorderedA, reorderedB, extraDims);
77  solve(*tmpX.tensorObject, reorderedA, reorderedB, extraDims);
78 
79  return tmpX;
80  }
81 } // namespace xerus
Internal representation of an read and write and moveable indexed Tensor or TensorNetwork.
Header file for the Index class.
Header file for the IndexedTensorMoveable class.
Header file for the standard contaienr support functions.
Internal representation of an readable indexed Tensor or TensorNetwork.
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
void solve(internal::IndexedTensorWritable< Tensor > &&_x, internal::IndexedTensorReadOnly< Tensor > &&_A, internal::IndexedTensorReadOnly< Tensor > &&_b)
Header file for the Tensor class.
tensor_type * tensorObject
Non-const pointer to the tensor object.
Header file for comfort functions and macros that should not be exported in the library.
Class used to represent indices that can be used to write tensor calculations in index notation...
Definition: index.h:43
size_t span
The span states how many dimensions are covered by the index.
Definition: index.h:65
internal::IndexedTensorMoveable< Tensor > operator/(internal::IndexedTensorReadOnly< Tensor > &&_b, internal::IndexedTensorReadOnly< Tensor > &&_A)
Header file for the IndexedTensor class.