xerus
a general purpose tensor library
tensor.h
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 #pragma once
26 
27 #include <map>
28 #include <limits>
29 #include <memory>
30 #include <random>
31 #include <functional>
32 
33 #include "basic.h"
34 #include "misc/containerSupport.h"
35 #include "misc/fileIO.h"
36 #include "misc/random.h"
37 
38 #include "indexedTensor.h"
39 
40 namespace xerus {
41  //Forwad declarations
42  class TensorNetwork;
43 
44  // NOTE these two functions are used in the template functions of Tensor so they have to be declared before the class..
45  class Tensor;
46 
56  void contract(Tensor& _result, const Tensor& _lhs, const bool _lhsTrans, const Tensor& _rhs, const bool _rhsTrans, const size_t _numModes);
57  Tensor contract(const Tensor& _lhs, const bool _lhsTrans, const Tensor& _rhs, const bool _rhsTrans, const size_t _numModes);
58 
59 
60 
65  void reshuffle(Tensor& _out, const Tensor& _base, const std::vector<size_t>& _shuffle);
66  Tensor reshuffle(const Tensor& _base, const std::vector<size_t>& _shuffle);
67 
68 
70  class Tensor final {
71  public:
72  static size_t sparsityFactor; // NOTE not const so that users can modify this value!
73 
74  /*- - - - - - - - - - - - - - - - - - - - - - - - - - Auxiliary types- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
75 
81  enum class Initialisation : bool { None, Zero };
82 
89  enum class Representation : bool { Dense, Sparse };
90 
92  typedef std::vector<size_t> DimensionTuple; // NOTE must not be declared as "using.." (internal segfault in gcc4.8.1)
93 
95  typedef std::vector<size_t> MultiIndex; // NOTE must not be declared as "using.." (internal segfault in gcc4.8.1)
96 
97  /*- - - - - - - - - - - - - - - - - - - - - - - - - - Member variables - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
99  DimensionTuple dimensions;
100 
102  size_t size = 1;
103 
106 
109 
110  private:
116  std::shared_ptr<value_t> denseData;
117 
123  std::shared_ptr<std::map<size_t, value_t>> sparseData;
124 
125  public:
126  /*- - - - - - - - - - - - - - - - - - - - - - - - - - Constructors - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
127 
129  explicit Tensor(const Representation _representation = Representation::Sparse);
130 
131 
133  Tensor( const Tensor& _other ) = default;
134 
135 
137  Tensor( Tensor&& _other ) noexcept = default;
138 
145  explicit Tensor(DimensionTuple _dimensions, const Representation _representation = Representation::Sparse, const Initialisation _init = Initialisation::Zero);
146 
147 
153  template<XERUS_ADD_MOVE(Vec, DimensionTuple), XERUS_ADD_MOVE(SPtr, std::shared_ptr<value_t>)>
154  explicit Tensor(Vec&& _dimensions, SPtr&& _data)
155  : dimensions(std::forward<Vec>(_dimensions)), size(misc::product(dimensions)), representation(Representation::Dense), denseData(std::forward<SPtr>(_data)) { }
156 
157 
163  explicit Tensor(DimensionTuple _dimensions, std::unique_ptr<value_t[]>&& _data);
164 
165 
173  explicit Tensor(DimensionTuple _dimensions, const std::function<value_t()>& _f);
174 
175 
182  explicit Tensor(DimensionTuple _dimensions, const std::function<value_t(const size_t)>& _f);
183 
184 
191  explicit Tensor(DimensionTuple _dimensions, const std::function<value_t(const MultiIndex&)>& _f);
192 
193 
202  Tensor(DimensionTuple _dimensions, const size_t _N, const std::function<std::pair<size_t, value_t>(size_t, size_t)>& _f);
203 
204 
212  template<class distribution=std::normal_distribution<value_t>, class generator=std::mt19937_64>
213  static Tensor XERUS_warn_unused random(DimensionTuple _dimensions, distribution& _dist=xerus::misc::defaultNormalDistribution, generator& _rnd=xerus::misc::randomEngine) {
214  Tensor result(std::move(_dimensions), Representation::Dense, Initialisation::None);
215  value_t* const dataPtr = result.denseData.get();
216  for(size_t i = 0; i < result.size; ++i) {
217  dataPtr[i] = _dist(_rnd);
218  }
219  return result;
220  }
221 
222 
227  template<class distribution=std::normal_distribution<value_t>, class generator=std::mt19937_64>
228  XERUS_force_inline static Tensor XERUS_warn_unused random(std::initializer_list<size_t>&& _dimensions, distribution& _dist=xerus::misc::defaultNormalDistribution, generator& _rnd=xerus::misc::randomEngine) {
229  return Tensor::random(DimensionTuple(std::move(_dimensions)), _dist, _rnd);
230  }
231 
232 
240  template<class generator=std::mt19937_64>
241  static Tensor XERUS_warn_unused random_orthogonal(DimensionTuple _dimensions1, DimensionTuple _dimensions2, generator& _rnd=xerus::misc::randomEngine) {
242  std::vector<size_t> dimensions = _dimensions1;
243  dimensions.insert(dimensions.end(), _dimensions2.begin(), _dimensions2.end());
244  const size_t m = misc::product(_dimensions1);
245  const size_t n = misc::product(_dimensions2);
246  const size_t max = std::max(m,n);
247  const size_t min = std::min(m,n);
249 
250  typename generator::result_type randomness = 0;
251  const size_t restart = size_t(std::log2(generator::max()));
252  for (size_t i=0; i<min; ++i) {
253  auto idx = i%restart;
254  if (idx == 0) {
255  randomness = _rnd();
256  }
257  if (randomness & (1<<idx)) {
258  result[{i,i}] = 1;
259  } else {
260  result[{i,i}] = -1;
261  }
262  }
263 
264  for (size_t i=0; i<min-1; ++i) { // do k = n-1 to 1 by -1;
266  u[0] -= u.frob_norm();
267  u /= u.frob_norm();
268  u.apply_factor();
269  contract(u, u, false, u, false, 0);
270  u *= -2.0;
271  Tensor p = Tensor::identity({max,max});
272  p.offset_add(u, {i,i});
273  contract(result, p, false, result, false, 1);
274  }
275 
276  if (m != max) {
277  reshuffle(result, result, {1,0});
278  }
279  result.reinterpret_dimensions(std::move(dimensions));
280 
281  return result;
282  }
283 
284 
293  template<class distribution=std::normal_distribution<value_t>, class generator=std::mt19937_64>
294  static Tensor XERUS_warn_unused random(DimensionTuple _dimensions, const size_t _N, distribution& _dist=xerus::misc::defaultNormalDistribution, generator& _rnd=xerus::misc::randomEngine) {
295  Tensor result(std::move(_dimensions), Representation::Sparse, Initialisation::Zero);
296  XERUS_REQUIRE(_N <= result.size, " Cannot create " << _N << " non zero entries in a tensor with only " << result.size << " total entries!");
297 
298  std::uniform_int_distribution<size_t> entryDist(0, result.size-1);
299  while(result.sparseData->size() < _N) {
300  result.sparseData->emplace(entryDist(_rnd), _dist(_rnd));
301  }
302  return result;
303  }
304 
305 
310  template<class distribution=std::normal_distribution<value_t>, class generator=std::mt19937_64>
311  XERUS_force_inline static Tensor XERUS_warn_unused random(std::initializer_list<size_t>&& _dimensions, const size_t _N, distribution& _dist, generator& _rnd) {
312  return Tensor::random(DimensionTuple(_dimensions), _N, _rnd, _dist);
313  }
314 
315 
320  static Tensor XERUS_warn_unused ones(DimensionTuple _dimensions);
321 
322 
328  static Tensor XERUS_warn_unused identity(DimensionTuple _dimensions);
329 
330 
336  static Tensor XERUS_warn_unused kronecker(DimensionTuple _dimensions);
337 
338 
344  static Tensor XERUS_warn_unused dirac(DimensionTuple _dimensions, const MultiIndex& _position);
345 
346 
352  static Tensor XERUS_warn_unused dirac(DimensionTuple _dimensions, const size_t _position);
353 
354 
356  Tensor dense_copy() const;
357 
358 
360  Tensor sparse_copy() const;
361 
362  /*- - - - - - - - - - - - - - - - - - - - - - - - - - Standard operators - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
368  Tensor& operator=(const Tensor& _other) = default;
369 
370 
376  Tensor& operator=( Tensor&& _other) = default;
377 
378 
384  Tensor& operator=(const TensorNetwork& _network);
385 
386 
387  /*- - - - - - - - - - - - - - - - - - - - - - - - - - Information - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
388 
394  size_t degree() const;
395 
400  bool has_factor() const;
401 
403  bool is_dense() const;
404 
406  bool is_sparse() const;
407 
413  size_t sparsity() const;
414 
420  size_t count_non_zero_entries(const value_t _eps = std::numeric_limits<value_t>::epsilon()) const;
421 
426  bool all_entries_valid() const;
427 
432  size_t reorder_cost() const;
433 
438  value_t frob_norm() const;
439 
444  value_t one_norm() const;
445 
446 
447  /*- - - - - - - - - - - - - - - - - - - - - - - - - - Basic arithmetics - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
454  Tensor& operator+=(const Tensor& _other);
455 
462  Tensor& operator-=(const Tensor& _other);
463 
470  Tensor& operator*=(const value_t _factor);
471 
478  Tensor& operator/=(const value_t _divisor);
479 
480 
481  /*- - - - - - - - - - - - - - - - - - - - - - - - - - Access - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
482 
488  value_t& operator[](const size_t _position);
489 
495  value_t operator[](const size_t _position) const;
496 
502  value_t& operator[](const MultiIndex& _positions);
503 
509  value_t operator[](const MultiIndex& _positions) const;
510 
511 
517  value_t& at(const size_t _position);
518 
524  value_t cat(const size_t _position) const;
525 
532 
540 
547  const value_t* get_unsanitized_dense_data() const;
548 
556 
563  const std::shared_ptr<value_t>& get_internal_dense_data();
564 
570  std::map<size_t, value_t>& get_sparse_data();
571 
578  std::map<size_t, value_t>& get_unsanitized_sparse_data();
579 
586  const std::map<size_t, value_t>& get_unsanitized_sparse_data() const;
587 
594  std::map<size_t, value_t>& override_sparse_data();
595 
602  const std::shared_ptr<std::map<size_t, value_t>>& get_internal_sparse_data();
603 
604 
605  /*- - - - - - - - - - - - - - - - - - - - - - - - - - Indexing - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
606 
612  template<typename... args>
614  return internal::IndexedTensor<Tensor>(this, std::vector<Index>({_args...}), false);
615  }
616 
617 
623  template<typename... args>
625  return internal::IndexedTensorReadOnly<Tensor>(this, std::vector<Index>({_args...}));
626  }
627 
628 
634  internal::IndexedTensor<Tensor> operator()(const std::vector<Index>& _indices);
635 
636 
642  internal::IndexedTensor<Tensor> operator()( std::vector<Index>&& _indices);
643 
644 
650  internal::IndexedTensorReadOnly<Tensor> operator()(const std::vector<Index>& _indices) const;
651 
652 
658  internal::IndexedTensorReadOnly<Tensor> operator()( std::vector<Index>&& _indices) const;
659 
660 
661 
662  /*- - - - - - - - - - - - - - - - - - - - - - - - - - Modifiers - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
663 
671  void reset(DimensionTuple _newDim, const Representation _representation, const Initialisation _init = Initialisation::Zero);
672 
673 
679  void reset(DimensionTuple _newDim, const Initialisation _init = Initialisation::Zero);
680 
684  void reset();
685 
686 
692  void reset(DimensionTuple _newDim, const std::shared_ptr<value_t>& _newData);
693 
694 
700  void reset(DimensionTuple _newDim, std::unique_ptr<value_t[]>&& _newData);
701 
702 
706  void reset(DimensionTuple _newDim, std::map<size_t, value_t>&& _newData);
707 
708 
716  void reinterpret_dimensions(DimensionTuple _newDimensions);
717 
727  void resize_mode(const size_t _mode, const size_t _newDim, size_t _cutPos=~0ul);
728 
729 
735  void fix_mode(const size_t _mode, const size_t _slatePosition);
736 
737 
743  void remove_slate(const size_t _mode, const size_t _pos);
744 
745 
751  void perform_trace(size_t _firstMode, size_t _secondMode);
752 
753 
759  void modify_diagonal_entries(const std::function<void(value_t&)>& _f);
760 
761 
767  void modify_diagonal_entries(const std::function<void(value_t&, const size_t)>& _f);
768 
769 
775  void modify_entries(const std::function<void(value_t&)>& _f);
776 
777 
783  void modify_entries(const std::function<void(value_t&, const size_t)>& _f);
784 
785 
791  void modify_entries(const std::function<void(value_t&, const MultiIndex&)>& _f);
792 
798  void offset_add(const Tensor& _other, const std::vector<size_t>& _offsets);
799 
804 
805 
810 
811 
815  void use_sparse_representation(const value_t _eps = std::numeric_limits<value_t>::epsilon());
816 
817  /*- - - - - - - - - - - - - - - - - - - - - - - - - - Miscellaneous - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
818 
824  std::string to_string() const;
825 
826 
827  /*- - - - - - - - - - - - - - - - - - - - - - - - - - Auxiliary functions - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
828 
829  static size_t multiIndex_to_position(const MultiIndex& _multiIndex, const DimensionTuple& _dimensions);
830 
831  static MultiIndex position_to_multiIndex(size_t _position, const DimensionTuple& _dimensions);
832 
833 
834  /*- - - - - - - - - - - - - - - - - - - - - - - - - - Internal Helper functions - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
835  protected:
836  template<int sign>
837  static void plus_minus_equal(Tensor& _me, const Tensor& _other);
838 
840  static void add_sparse_to_full(const std::shared_ptr<value_t>& _denseData, const value_t _factor, const std::shared_ptr<const std::map<size_t, value_t>>& _sparseData);
841 
843  static void add_sparse_to_sparse(const std::shared_ptr<std::map<size_t, value_t>>& _sum, const value_t _factor, const std::shared_ptr<const std::map<size_t, value_t>>& _summand);
844 
845  public:
846 
848  void ensure_own_data();
849 
852 
854  void apply_factor();
855 
858  };
859 
860  /*- - - - - - - - - - - - - - - - - - - - - - - - - - External functions - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
861 
869  XERUS_force_inline void contract(Tensor& _result, const Tensor& _lhs, const Tensor& _rhs, const size_t _numModes) {
870  contract(_result, _lhs, false, _rhs, false, _numModes);
871  }
872 
873  XERUS_force_inline Tensor contract(const Tensor& _lhs, const Tensor& _rhs, const size_t _numModes) {
874  return contract(_lhs, false, _rhs, false, _numModes);
875  }
876 
877  /*- - - - - - - - - - - - - - - - - - - - - - - - - - Basic arithmetics - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
878 
886  Tensor operator+(Tensor _lhs, const Tensor& _rhs);
887 
895  Tensor operator-(Tensor _lhs, const Tensor& _rhs);
896 
904  Tensor operator*(const value_t _factor, Tensor _tensor);
905 
913  Tensor operator*(Tensor _tensor, const value_t _factor);
914 
922  Tensor operator/(Tensor _tensor, const value_t _divisor);
923 
924 
925 
931  static XERUS_force_inline value_t frob_norm(const Tensor& _tensor) { return _tensor.frob_norm(); }
932 
938  static XERUS_force_inline value_t one_norm(const Tensor& _tensor) { return _tensor.one_norm(); }
939 
948  void calculate_svd(Tensor& _U, Tensor& _S, Tensor& _Vt, Tensor _input, const size_t _splitPos, const size_t _maxRank, const value_t _eps);
949 
957  void calculate_qr(Tensor& _Q, Tensor& _R, Tensor _input, const size_t _splitPos);
958 
966  void calculate_rq(Tensor& _R, Tensor& _Q, Tensor _input, const size_t _splitPos);
967 
977  void calculate_qc(Tensor& _Q, Tensor& _C, Tensor _input, const size_t _splitPos);
978 
988  void calculate_cq(Tensor& _C, Tensor& _Q, Tensor _input, const size_t _splitPos);
989 
997  void pseudo_inverse(Tensor& _inverse, const Tensor& _input, const size_t _splitPos);
998  Tensor pseudo_inverse(const Tensor& _input, const size_t _splitPos);
999 
1007  void solve_least_squares(Tensor& _X, const Tensor& _A, const Tensor& _B, const size_t _extraDegree = 0);
1008 
1016  void solve(Tensor &_X, const Tensor &_A, const Tensor &_B, size_t _extraDegree = 0);
1017 
1021  Tensor entrywise_product(const Tensor &_A, const Tensor &_B);
1022 
1031  bool approx_equal(const xerus::Tensor& _a, const xerus::Tensor& _b, const xerus::value_t _eps = EPSILON);
1032 
1041  bool approx_entrywise_equal(const xerus::Tensor& _a, const xerus::Tensor& _b, const xerus::value_t _eps = EPSILON);
1042 
1043 
1051  bool approx_entrywise_equal(const xerus::Tensor& _tensor, const std::vector<value_t>& _values, const xerus::value_t _eps = EPSILON);
1052 
1059  std::ostream& operator<<(std::ostream& _out, const Tensor& _tensor);
1060 
1061  namespace misc {
1066  void stream_writer(std::ostream &_stream, const Tensor &_obj, const FileFormat _format);
1067 
1071  void stream_reader(std::istream &_stream, Tensor &_obj, const FileFormat _format);
1072  }
1073 }
Initialisation
Flags determining the initialisation of the data of Tensor objects.
Definition: tensor.h:81
void use_dense_representation_if_desirable()
Converts the Tensor to a dense representation if sparsity * sparsityFactor >= size.
Definition: tensor.cpp:1043
Tensor sparse_copy() const
Returns a copy of this Tensor that uses a sparse representation.
Definition: tensor.cpp:194
value_t frob_norm() const
Calculates the frobenious norm of the tensor.
Definition: tensor.cpp:286
value_t factor
Single value representing a constant scaling factor.
Definition: tensor.h:105
static size_t sparsityFactor
Definition: tensor.h:72
size_t degree() const
Returns the degree of the tensor.
Definition: tensor.cpp:206
void ensure_own_data()
Ensures that this tensor is the sole owner of its data. If needed new space is allocated and all entr...
Definition: tensor.cpp:1158
void pseudo_inverse(Tensor &_inverse, const Tensor &_input, const size_t _splitPos)
Low-Level calculation of the pseudo inverse of a given Tensor.
Definition: tensor.cpp:1568
static void add_sparse_to_sparse(const std::shared_ptr< std::map< size_t, value_t >> &_sum, const value_t _factor, const std::shared_ptr< const std::map< size_t, value_t >> &_summand)
Adds the given sparse data to the given sparse data.
Definition: tensor.cpp:1121
void reshuffle(Tensor &_out, const Tensor &_base, const std::vector< size_t > &_shuffle)
: Performs a simple reshuffle. Much less powerfull then a full evaluate, but more efficient...
static Tensor XERUS_warn_unused identity(DimensionTuple _dimensions)
: Returns a Tensor representation of the identity operator with the given dimensions.
Definition: tensor.cpp:131
static Tensor XERUS_warn_unused random_orthogonal(DimensionTuple _dimensions1, DimensionTuple _dimensions2, generator &_rnd=xerus::misc::randomEngine)
Constructs a dense Tensor with the given dimensions and uses the given random generator and distribut...
Definition: tensor.h:241
Header file for the standard contaienr support functions.
std::string to_string() const
Creates a string representation of the Tensor.
Definition: tensor.cpp:1067
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...
Definition: tensor.h:213
Very general class used to represent arbitary tensor networks.
Definition: tensorNetwork.h:42
size_t size
Size of the Tensor – always equal to the product of the dimensions.
Definition: tensor.h:102
Internal representation of an readable indexed Tensor or TensorNetwork.
Tensor & operator=(const Tensor &_other)=default
Standard assignment operator.
void stream_reader(std::istream &_stream, Tensor &_obj, const FileFormat _format)
tries to restore the tensor from a stream of data.
Definition: tensor.cpp:1807
value_t one_norm() const
Calculates the 1-norm of the tensor.
Definition: tensor.cpp:275
DimensionTuple dimensions
Vector containing the individual dimensions of the tensor.
Definition: tensor.h:99
void calculate_rq(Tensor &_R, Tensor &_Q, Tensor _input, const size_t _splitPos)
Low-Level RQ calculation of a given Tensor _input = _R _Q.
Definition: tensor.cpp:1511
void calculate_qr(Tensor &_Q, Tensor &_R, Tensor _input, const size_t _splitPos)
Low-Level QR calculation of a given Tensor _input = _Q _R.
Definition: tensor.cpp:1492
Tensor(const Representation _representation=Representation::Sparse)
Constructs an order zero Tensor with the given inital representation.
Definition: tensor.cpp:49
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
static XERUS_force_inline Tensor XERUS_warn_unused random(std::initializer_list< size_t > &&_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...
Definition: tensor.h:228
value_t * get_dense_data()
Returns a pointer for direct access to the dense data array in row major order.
Definition: tensor.cpp:401
void resize_mode(const size_t _mode, const size_t _newDim, size_t _cutPos=~0ul)
Resizes a specific mode of the Tensor.
Definition: tensor.cpp:626
value_t cat(const size_t _position) const
Unsanitized read access to a single entry.
Definition: tensor.cpp:384
value_t & operator[](const size_t _position)
Read/Write access a single entry.
Definition: tensor.cpp:324
STL namespace.
void fix_mode(const size_t _mode, const size_t _slatePosition)
Fixes a specific mode to a specific value, effectively reducing the order by one. ...
Definition: tensor.cpp:732
thread_local std::normal_distribution< double > defaultNormalDistribution
Definition: random.cpp:30
Header file for templates to store and restore objects from / to files / streams. ...
std::map< size_t, value_t > & get_unsanitized_sparse_data()
Gives access to the internal sparse map, without any checks.
Definition: tensor.cpp:445
internal::IndexedTensor< Tensor > operator()(args... _args)
Indexes the Tensor for read/write use.
Definition: tensor.h:613
Tensor(Vec &&_dimensions, SPtr &&_data)
: Creates a new (dense) tensor with the given dimensions, using a provided data.
Definition: tensor.h:154
const std::shared_ptr< value_t > & get_internal_dense_data()
Gives access to the internal shared data pointer, without any checks.
Definition: tensor.cpp:431
void ensure_own_data_and_apply_factor()
Checks whether there is a non-trivial factor and applies it. Even if no factor is applied ensure_own_...
Definition: tensor.cpp:1210
Tensor operator*(const value_t _factor, Tensor _tensor)
Calculates the entrywise multiplication of the Tensor _tensor with the constant _factor.
Definition: tensor.cpp:1233
Tensor & operator/=(const value_t _divisor)
Performs the entrywise divison by a constant _divisor.
Definition: tensor.cpp:317
static Tensor XERUS_warn_unused random(DimensionTuple _dimensions, const size_t _N, distribution &_dist=xerus::misc::defaultNormalDistribution, generator &_rnd=xerus::misc::randomEngine)
Constructs a random sparse Tensor with the given dimensions.
Definition: tensor.h:294
std::vector< size_t > DimensionTuple
: Represention of the dimensions of a Tensor.
Definition: tensor.h:92
void ensure_own_data_no_copy()
Ensures that this tensor is the sole owner of its data space. If needed new space is allocated with e...
Definition: tensor.cpp:1173
void apply_factor()
Checks whether there is a non-trivial scaling factor and applies it if nessecary. ...
Definition: tensor.cpp:1186
void modify_diagonal_entries(const std::function< void(value_t &)> &_f)
Modifies the diagonal entries according to the given function.
Definition: tensor.cpp:841
static XERUS_force_inline Tensor XERUS_warn_unused random(std::initializer_list< size_t > &&_dimensions, const size_t _N, distribution &_dist, generator &_rnd)
Constructs a random sparse Tensor with the given dimensions.
Definition: tensor.h:311
static Tensor XERUS_warn_unused ones(DimensionTuple _dimensions)
: Returns a Tensor with all entries equal to one.
Definition: tensor.cpp:122
std::ostream & operator<<(std::ostream &_out, const xerus::Index &_idx)
Allows to pretty print indices, giving the valueId and span.
Definition: index.cpp:175
void reinterpret_dimensions(DimensionTuple _newDimensions)
Reinterprets the dimensions of the tensor.
Definition: tensor.cpp:620
void solve(internal::IndexedTensorWritable< Tensor > &&_x, internal::IndexedTensorReadOnly< Tensor > &&_A, internal::IndexedTensorReadOnly< Tensor > &&_b)
static Tensor XERUS_warn_unused kronecker(DimensionTuple _dimensions)
: Returns a Tensor representation of the kronecker delta.
Definition: tensor.cpp:160
size_t reorder_cost() const
Approximates the cost to reorder the tensor.
Definition: tensor.cpp:270
#define XERUS_warn_unused
Definition: standard.h:78
Internal representation of an readable and writeable indexed Tensor or TensorNetwork.
Definition: indexedTensor.h:37
void calculate_cq(Tensor &_C, Tensor &_Q, Tensor _input, const size_t _splitPos)
Low-Level CQ calculation of a given Tensor _input = _C _Q.
Definition: tensor.cpp:1548
static Tensor XERUS_warn_unused dirac(DimensionTuple _dimensions, const MultiIndex &_position)
: Returns a Tensor with a single entry equals one and all other zero.
Definition: tensor.cpp:173
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.
Definition: tensor.cpp:1252
void reset()
Resets the tensor as if default initialized.
Definition: tensor.cpp:559
static void add_sparse_to_full(const std::shared_ptr< value_t > &_denseData, const value_t _factor, const std::shared_ptr< const std::map< size_t, value_t >> &_sparseData)
Adds the given sparse data to the given full data.
Definition: tensor.cpp:1114
FileFormat
possible file formats for tensor storage
Definition: fileIO.h:43
Tensor & operator*=(const value_t _factor)
Performs the entrywise multiplication with a constant _factor.
Definition: tensor.cpp:311
size_t count_non_zero_entries(const value_t _eps=std::numeric_limits< value_t >::epsilon()) const
Determines the number of non-zero entries.
Definition: tensor.cpp:240
bool has_factor() const
Checks whether the tensor has a non-trivial global scaling factor.
Definition: tensor.cpp:211
Tensor operator-(Tensor _lhs, const Tensor &_rhs)
Calculates the entrywise difference between _lhs and _rhs.
Definition: tensor.cpp:1227
Tensor & operator+=(const Tensor &_other)
Adds the _other Tensor entrywise to this one.
Definition: tensor.cpp:299
static size_t multiIndex_to_position(const MultiIndex &_multiIndex, const DimensionTuple &_dimensions)
Definition: tensor.cpp:1131
#define XERUS_REQUIRE(condition, message)
Checks whether condition is true. logs an error otherwise via XERUS_LOG(error, message).
Definition: check.h:65
constexpr const value_t EPSILON
The default epsilon value in xerus.
Definition: basic.h:50
Tensor operator+(Tensor _lhs, const Tensor &_rhs)
Calculates the entrywise sum of _lhs and _rhs.
Definition: tensor.cpp:1221
std::map< size_t, value_t > & override_sparse_data()
Returns a pointer to the internal sparse data map for complete rewrite purpose ONLY.
Definition: tensor.cpp:457
bool all_entries_valid() const
Checks the tensor for illegal entries, e.g. nan, inf,...
Definition: tensor.cpp:256
const std::shared_ptr< std::map< size_t, value_t > > & get_internal_sparse_data()
Gives access to the internal shared sparse data pointer, without any checks.
Definition: tensor.cpp:472
value_t & at(const size_t _position)
Unsanitized access to a single entry.
Definition: tensor.cpp:365
Header file for shorthand notations that are xerus specific but used throughout the library...
double value_t
The type of values to be used by xerus.
Definition: basic.h:43
void use_sparse_representation(const value_t _eps=std::numeric_limits< value_t >::epsilon())
Converts the Tensor to a sparse representation.
Definition: tensor.cpp:1050
void calculate_svd(Tensor &_U, Tensor &_S, Tensor &_Vt, Tensor _input, const size_t _splitPos, const size_t _maxRank, const value_t _eps)
Low-Level SVD calculation of a given Tensor _input = _U _S _Vt.
Definition: tensor.cpp:1424
void stream_writer(std::ostream &_stream, const Tensor &_obj, const FileFormat _format)
pipes all information necessary to restore the current tensor into _stream.
Definition: tensor.cpp:1781
bool approx_entrywise_equal(const xerus::Tensor &_a, const xerus::Tensor &_b, const xerus::value_t _eps=EPSILON)
Checks whether two Tensors are approximately entrywise equal.
Definition: tensor.cpp:1745
bool is_dense() const
Returns whether the current representation is dense.
Definition: tensor.cpp:220
bool approx_equal(const xerus::Tensor &_a, const xerus::Tensor &_b, const xerus::value_t _eps=EPSILON)
Checks whether two tensors are approximately equal.
Definition: tensor.cpp:1738
void solve_least_squares(Tensor &_X, const Tensor &_A, const Tensor &_B, const size_t _extraDegree=0)
Solves the least squares problem ||_A _X - _B||_F.
Definition: tensor.cpp:1583
std::map< size_t, value_t > & get_sparse_data()
Returns a reference for direct access to the sparse data map.
Definition: tensor.cpp:437
internal::IndexedTensorReadOnly< Tensor > operator()(args... _args) const
Indexes the Tensor for read only use.
Definition: tensor.h:624
void offset_add(const Tensor &_other, const std::vector< size_t > &_offsets)
Adds the given Tensor with the given offsets to this one.
Definition: tensor.cpp:969
bool is_sparse() const
Returns whether the current representation is sparse.
Definition: tensor.cpp:226
static void plus_minus_equal(Tensor &_me, const Tensor &_other)
Definition: tensor.cpp:1087
thread_local std::mt19937_64 randomEngine
static MultiIndex position_to_multiIndex(size_t _position, const DimensionTuple &_dimensions)
Definition: tensor.cpp:1144
Tensor dense_copy() const
Returns a copy of this Tensor that uses a dense representation.
Definition: tensor.cpp:187
void remove_slate(const size_t _mode, const size_t _pos)
Removes a single slate from the Tensor, reducing dimension[_mode] by one.
Definition: tensor.cpp:772
void perform_trace(size_t _firstMode, size_t _secondMode)
Performs the trace over the given modes.
Definition: tensor.cpp:781
value_t * get_unsanitized_dense_data()
Gives access to the internal data pointer, without any checks.
Definition: tensor.cpp:408
Header file for some additional math functions.
void modify_entries(const std::function< void(value_t &)> &_f)
Modifies every entry according to the given function.
Definition: tensor.cpp:883
Tensor & operator-=(const Tensor &_other)
Subtracts the _other Tensor entrywise from this one.
Definition: tensor.cpp:305
Tensor entrywise_product(const Tensor &_A, const Tensor &_B)
calculates the entrywise product of two Tensors
Definition: tensor.cpp:1708
#define XERUS_force_inline
Collection of attributes to force gcc to inline a specific function.
Definition: standard.h:75
std::vector< size_t > MultiIndex
: Represention of a MultiIndex, i.e. the tuple of positions for each dimension determining a single p...
Definition: tensor.h:95
value_t * override_dense_data()
Returns a pointer to the internal dense data array for complete rewrite purpose ONLY.
Definition: tensor.cpp:420
Representation representation
The current representation of the Tensor (i.e Dense or Sparse)
Definition: tensor.h:108
void use_dense_representation()
Converts the Tensor to a dense representation.
Definition: tensor.cpp:1028
size_t sparsity() const
Returns the number currently saved entries.
Definition: tensor.cpp:232
internal::IndexedTensorMoveable< Tensor > operator/(internal::IndexedTensorReadOnly< Tensor > &&_b, internal::IndexedTensorReadOnly< Tensor > &&_A)
Header file for the IndexedTensor class.
Representation
Flags indicating the internal representation of the data of Tensor objects.
Definition: tensor.h:89
void calculate_qc(Tensor &_Q, Tensor &_C, Tensor _input, const size_t _splitPos)
Low-Level QC calculation of a given Tensor _input = _Q _C.
Definition: tensor.cpp:1528