MADNESS 0.10.1
lowranktensor.h
Go to the documentation of this file.
1/*
2 This file is part of MADNESS.
3
4 Copyright (C) 2007,2010 Oak Ridge National Laboratory
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
20 For more information please contact:
21
22 Robert J. Harrison
23 Oak Ridge National Laboratory
24 One Bethel Valley Road
25 P.O. Box 2008, MS-6367
26
27 email: harrisonrj@ornl.gov
28 tel: 865-241-3937
29 fax: 865-572-0680
30
31 $Id$
32*/
33
34#ifndef MADNESS_TENSOR_LOWRANKTENSOR_H_
35#define MADNESS_TENSOR_LOWRANKTENSOR_H_
36
37#include <memory>
38#include <vector>
39#include <variant>
40
42#include <madness/world/print.h>
47#include "type_data.h"
49
50
51namespace madness {
52
53
54// forward declaration
55template <class T> class SliceLowRankTensor;
56
57
58template<typename T>
59class GenTensor {
60
61public:
62
63 friend class SliceLowRankTensor<T>;
64
65 /// C++ typename of the real type associated with a complex type.
67
68 /// C++ typename of the floating point type associated with scalar real type
70
71 /// empty ctor
72 GenTensor() = default;
73
74 /// copy ctor, shallow
75 GenTensor(const GenTensor<T>& other) = default;
76
77 GenTensor(const long ndim, const long* dims, const TensorType& tt) {
78 if (tt==TT_FULL) tensor=std::shared_ptr<Tensor<T> >(new Tensor<T>(ndim, dims));
79 if (tt==TT_2D) tensor=std::shared_ptr<SVDTensor<T> >(new SVDTensor<T>(ndim, dims));
80 if (tt==TT_TENSORTRAIN) tensor=std::shared_ptr<TensorTrain<T> >(new TensorTrain<T>(ndim, dims));
81 }
82
83 /// ctor with dimensions; constructs tensor filled with zeros
84 GenTensor(const std::vector<long>& dim, const TensorType& tt) :
85 GenTensor(long(dim.size()),&dim.front(),tt) {
86 }
87
88 /// ctor with dimensions; constructs tensor filled with zeros
89 GenTensor(const std::vector<long>& dim, const TensorArgs& targs) :
90 GenTensor(dim, targs.tt) {
91 }
92
93 /// ctor with dimensions; all dims have the same value k
94 GenTensor(const TensorType& tt, const long k, const long ndim) :
95 GenTensor(std::vector<long>(ndim,k), tt) {
96 }
97
98 /// ctor with a regular Tensor and arguments, deep
99 GenTensor(const Tensor<T>& rhs, const double& thresh, const TensorType& tt) :
101 }
102
103 /// ctor with a regular Tensor and arguments, deep
104 GenTensor(const Tensor<T>& rhs, const TensorArgs& targs) {
105 if (targs.tt==TT_FULL) *this=copy(rhs);
106 else if (targs.tt==TT_2D) {
107 if (rhs.size()==0) {
108 tensor=std::shared_ptr<SVDTensor<T> >(new SVDTensor<T>(rhs,targs.thresh*facReduce()));
109 } else {
110 TensorTrain<T> tt(rhs,targs.thresh*facReduce());
111 GenTensor<T> tmp=tt;
112 *this=tmp.convert(targs);
113 }
114// } else if (targs.tt==TT_DYNAMIC) {
115// if (rhs.size()==0) {
116// tensor=std::shared_ptr<SVDTensor<T> >(new SVDTensor<T>(rhs,targs.thresh*facReduce()));
117// } else {
118//
119// long maxrank=std::max(50.0,floor(0.3*sqrt(rhs.size())));
120// RandomizedMatrixDecomposition<T> rmd=RMDFactory().maxrank(maxrank);
121// Tensor<T> Q=rmd.compute_range(rhs,targs.thresh*facReduce()*0.1,{0,0});
122// if (Q.size()==0) {
123// *this=SVDTensor<T>(rhs.ndim(),rhs.dims());
124// } else if (not rmd.exceeds_maxrank()) {
125// SVDTensor<T> result(rhs.ndim(),rhs.dims());
126// result=SVDTensor<T>::compute_svd_from_range(Q,rhs);
127// *this=result;
128// } else {
129// *this=copy(rhs);
130//// TensorTrain<T> tt(rhs,targs.thresh*facReduce());
131//// GenTensor<T> tmp=tt;
132//// *this=tmp.convert(targs);
133// }
134//// tensor=std::shared_ptr<SVDTensor<T> >(new SVDTensor<T>(rhs,targs.thresh*facReduce()));
135// }
136 } else if (targs.tt==TT_TENSORTRAIN) {
137 tensor=std::shared_ptr<TensorTrain<T> >(new TensorTrain<T>(rhs,targs.thresh*facReduce()));
138 } else {
139 MADNESS_EXCEPTION("unknown tensor type in LowRankTensor constructor",1);
140 }
141 }
142
143 /// ctor with a regular Tensor, deep
144 GenTensor(const Tensor<T>& other) {
145 tensor=std::shared_ptr<Tensor<T> >(new Tensor<T>((other)));
146 }
147
148 /// ctor with a TensorTrain as argument, shallow
149 GenTensor(const TensorTrain<T>& other) {
150 tensor=std::shared_ptr<TensorTrain<T> >(new TensorTrain<T>((other))) ;
151 }
152
153 /// ctor with a SVDTensor as argument, shallow
154 GenTensor(const SVDTensor<T>& other) {
155 tensor=std::shared_ptr<SVDTensor<T> >(new SVDTensor<T>((other)));
156 }
157
158 /// ctor with a SliceLowRankTensor as argument, deep
160 *this=other;
161 }
162
163 /// shallow assignment operator
165 if (this!=&other) tensor=other.tensor;
166 return *this;
167 }
168
169 /// deep assignment operator
171 tensor=std::shared_ptr<Tensor<T> >(new Tensor<T>(copy(other)));
172 return *this;
173 }
174
175 /// deep assignment operator
177 tensor=std::shared_ptr<SVDTensor<T> >(new SVDTensor<T>(copy(other)));
178 return *this;
179 }
180
181 /// deep assignment operator
183 tensor=std::shared_ptr<TensorTrain<T> >(new TensorTrain<T>(copy(other)));
184 return *this;
185 }
186
187 /// deep assignment with slices: g0 = g1(s)
189 const std::array<Slice,TENSOR_MAXDIM>& s=other.thisslice;
191 if (other.is_full_tensor())
192 tensor=std::shared_ptr<Tensor<T> >(new Tensor<T>(copy(other.get_tensor()(s))));
193 else if (other.is_svd_tensor())
194 tensor=std::shared_ptr<SVDTensor<T> >(new SVDTensor<T>(other.get_svdtensor().copy_slice(s)));
195 else if (other.is_tensortrain())
196 tensor=std::shared_ptr<TensorTrain<T> >(new TensorTrain<T>(copy(other.get_tensortrain(),s)));
197 else {
198 MADNESS_EXCEPTION("you should not be here",1);
199 }
200 return *this;
201 }
202
203 /// Type conversion makes a deep copy
204 template <class Q> operator GenTensor<Q>() const { // type conv => deep copy
205
206 GenTensor<Q> result;
207 if (is_full_tensor()) {
208 result=Tensor<Q>(get_tensor());
209 } else if (is_svd_tensor()) {
210 MADNESS_EXCEPTION("no type conversion for TT_2D yes=t",1);
211 } else if (is_tensortrain()) {
212 MADNESS_EXCEPTION("no type conversion for TT_2D yes=t",1);
213 }
214 return result;
215 }
216
219 try {
220 return *(std::get<1>(tensor).get());
221 } catch (...) {
222 MADNESS_EXCEPTION("failure to return SVDTensor from LowRankTensor",1);
223 }
224 }
225
228 try {
229 return *(std::get<1>(tensor).get());
230 } catch (...) {
231 MADNESS_EXCEPTION("failure to return SVDTensor from LowRankTensor",1);
232 }
233 }
234
237 try {
238 return *(std::get<0>(tensor).get());
239 } catch (...) {
240 MADNESS_EXCEPTION("failure to return Tensor from LowRankTensor",1);
241 }
242 }
243
244 const Tensor<T>& get_tensor() const {
246 try {
247 return *(std::get<0>(tensor).get());
248 } catch (...) {
249 MADNESS_EXCEPTION("failure to return Tensor from LowRankTensor",1);
250 }
251 }
252
255 try {
256 return *(std::get<2>(tensor).get());
257 } catch (...) {
258 MADNESS_EXCEPTION("failure to return TensorTrain from LowRankTensor",1);
259 }
260 }
261
264 try {
265 return *(std::get<2>(tensor).get());
266 } catch (...) {
267 MADNESS_EXCEPTION("failure to return TensorTrain from LowRankTensor",1);
268 }
269 }
270
271 /// general slicing, shallow; for temporary use only!
272 SliceLowRankTensor<T> operator()(const std::vector<Slice>& s) {
273 return SliceLowRankTensor<T>(*this,s);
274 }
275
276 /// general slicing, shallow; for temporary use only!
277 const SliceLowRankTensor<T> operator()(const std::vector<Slice>& s) const {
278 return SliceLowRankTensor<T>(*this,s);
279 }
280
281
282 /// deep copy
283 friend GenTensor copy(const GenTensor& other) {
284 GenTensor<T> result;
285 if (other.is_assigned()) std::visit([&result](auto& obj) {result=copy(*obj);}, other.tensor);
286 return result;
287 }
288
289 /// return the tensor type
291 if (index()==0) return TT_FULL;
292 if (index()==1) return TT_2D;
293 if (index()==2) return TT_TENSORTRAIN;
294 MADNESS_EXCEPTION("confused tensor types ",1);
295 }
296
297 constexpr bool is_full_tensor() const {
298 return (index()==0);
299 }
300
301 constexpr bool is_svd_tensor() const {
302 return (index()==1);
303 }
304
305 constexpr bool is_tensortrain() const {
306 return (index()==2);
307 }
308
309 bool is_of_tensortype(const TensorType& tt) const {
310 if ((index()==0) and (tt==TT_FULL)) return true;
311 if ((index()==1) and (tt==TT_2D)) return true;
312 if ((index()==2) and (tt==TT_TENSORTRAIN)) return true;
313 return false;
314 }
315
316 template<typename Q, typename R>
317 friend bool is_same_tensor_type(const GenTensor<R>& rhs, const GenTensor<Q>& lhs);
318
319 int index() const {
320 return is_assigned() ? tensor.index() : -1;
321 }
322
324
325 // fast return
326 if (not is_assigned()) return *this;
327 if (is_of_tensortype(targs.tt)) return *this;
328// if (targs.tt==TT_DYNAMIC) if (is_svd_tensor()) return *this;
329
330 // target is full tensor
331 if (targs.tt == TT_FULL) {
332 *this = this->full_tensor_copy();
333 }
334
335 // source is full tensor: construct the corresponding representation
336 else if (is_full_tensor()) {
337 *this = GenTensor<T>(get_tensor(), targs);
338 }
339
340 // TT_TENSORTRAIN TO TT_2D
341 else if ((is_tensortrain()) and (targs.tt == TT_2D)) {
342 Tensor<T> U, VT;
344 get_tensortrain().two_mode_representation(U, VT, s);
345 long rank = s.size();
346 if (rank == 0) {
347 *this = SVDTensor<T>(get_tensortrain().ndim(), get_tensortrain().dims(), ndim() / 2);
348 return *this;
349 }
350
351 long n = 1, m = 1;
352 for (int i = 0; i < U.ndim() - 1; ++i) n *= U.dim(i);
353 for (int i = 1; i < VT.ndim(); ++i) m *= VT.dim(i);
354 MADNESS_ASSERT(rank * n == U.size());
355 MADNESS_ASSERT(rank * m == VT.size());
356 U = copy(transpose(U.reshape(n, rank))); // make it contiguous
357 VT = VT.reshape(rank, m);
358 SVDTensor<T> svdtensor(s, U, VT, ndim(), dims());
359 svdtensor.normalize();
360 *this = svdtensor;
361 }
362 else if ((is_svd_tensor()) and (targs.tt == TT_TENSORTRAIN)) {
364 *this=tt;
365 } else {
366 print("conversion from type ", index(), "to type", targs.tt, "not supported");
367 MADNESS_EXCEPTION("type conversion not supported in LowRankTensor::convert ", 1);
368 }
369 return *this;
370 }
371
372 /// convert this to a new LowRankTensor of given tensor type
373 GenTensor convert(const TensorArgs& targs) const {
374
375 // deep copy for same type
376 if (is_of_tensortype(targs.tt)) return copy(*this);
377
378 // new LRT will be newly constructed anyways
379 if (is_full_tensor()) return GenTensor<T>(get_tensor(),targs);
380
381 GenTensor<T> result(*this); // shallow
382 result.convert_inplace(targs);
383 return result;
384 }
385
386 long ndim() const {
387 return (is_assigned()) ? ptr()->ndim() : -1;
388 }
389
390 /// return the number of entries in dimension i
391 long dim(const int i) const {
393 return ptr()->dim(i);
394 }
395
396 /// return the number of entries in dimension i
397 const long* dims() const {
399 return ptr()->dims();
400 }
401
402 void normalize() {
403 if (is_svd_tensor()) get_svdtensor().normalize();
404 }
405
408 if (not is_assigned()) return 0.0;
409 std::visit([&norm](auto& obj) {norm=obj->normf();}, tensor);
410 return norm;
411 }
412
415 if (not is_assigned()) return 0.0;
416 if (is_svd_tensor()) return get_svdtensor().svd_normf();
417 std::visit([&norm](auto& obj) {norm=obj->normf();}, tensor);
418 return norm;
419 }
420
421
422 /// Inplace multiplication by scalar of supported type (legacy name)
423
424 /// @param[in] x Scalar value
425 /// @return %Reference to this tensor
426 template <typename Q>
428 scale(Q fac) {
429 if (not is_assigned()) return *this;
430 std::visit([&fac](auto& obj) {obj->scale(T(fac));}, tensor);
431 return *this;
432 }
433
435 if (not is_assigned()) return Tensor<T>();
436 else if (is_full_tensor()) return copy(get_tensor());
437 else if (is_svd_tensor()) return get_svdtensor().reconstruct();
438 else if (is_tensortrain()) return get_tensortrain().reconstruct();
439 else {
440 MADNESS_EXCEPTION("you should not be here",1);
441 }
442 return Tensor<T>();
443 }
444
445 /// return a view (shallow copy) of the full tensor, if possible,
446 /// otherwise reconstruct to full tensor and return a deep copy
448 if (is_full_tensor()) return get_tensor();
449 return full_tensor_copy();
450 }
451
452 /// return a reference of the full tensor, presumably for in-place modification
453 /// WARNING: works only if this is indeed a full tensor
458
459
460 /// reconstruct this to return a full tensor
462
463 if (is_full_tensor()) return full_tensor();
464 if (is_svd_tensor() or is_tensortrain()) return full_tensor_copy();
465 return Tensor<T>();
466 }
467
468
469 static double facReduce() {return 1.e-3;}
470 static double fac_reduce() {return 1.e-3;}
471
472 long rank() const {
473 if (is_full_tensor()) return -1;
474 else if (is_svd_tensor()) return get_svdtensor().rank();
475 else if (is_tensortrain()) {
476 std::vector<long> r=get_tensortrain().ranks();
477 return *(std::max_element(r.begin(), r.end()));
478 }
479 return 0l;
480 }
481
482 bool is_assigned() const {
483 return ptr() ? true : false;
484 }
485
486 bool has_data() const {return size()>0;}
487
488 bool has_no_data() const {return (not has_data());}
489
490 long size() const {
491 return (is_assigned()) ? ptr()->size() : 0;
492 }
493
494 long nCoeff() const {
495 if (is_full_tensor()) return get_tensor().size();
496 else if (is_svd_tensor()) return get_svdtensor().nCoeff();
497 else if (is_tensortrain()) return get_tensortrain().real_size();
498 else {
499 MADNESS_EXCEPTION("you should not be here",1);
500 }
501 return false;
502 }
503
504 long real_size() const {
505 if (is_full_tensor()) return get_tensor().size();
506 else if (is_svd_tensor()) return get_svdtensor().real_size();
507 else if (is_tensortrain()) return get_tensortrain().real_size();
508 else {
509 MADNESS_EXCEPTION("you should not be here",1);
510 }
511 return false;
512 }
513
514 /// returns the trace of <this|rhs>
515 template<typename Q>
516 TENSOR_RESULT_TYPE(T,Q) trace_conj(const GenTensor<Q>& rhs) const {
517
518 if (TensorTypeData<T>::iscomplex) MADNESS_EXCEPTION("no complex trace in LowRankTensor, sorry",1);
519 if (TensorTypeData<Q>::iscomplex) MADNESS_EXCEPTION("no complex trace in LowRankTensor, sorry",1);
520
521 typedef TENSOR_RESULT_TYPE(T,Q) resultT;
522 // fast return if possible
523 if ((this->rank()==0) or (rhs.rank()==0)) return resultT(0.0);
524
526
527 if (is_full_tensor()) return get_tensor().trace_conj(rhs.get_tensor());
528 else if (is_svd_tensor()) return trace(get_svdtensor(),rhs.get_svdtensor());
529 else if (is_tensortrain()) return get_tensortrain().trace(rhs.get_tensortrain());
530 else {
531 MADNESS_EXCEPTION("you should not be here",1);
532 }
533 return TENSOR_RESULT_TYPE(T,Q)(0);
534 }
535
536 /// multiply with a number
537 template<typename Q>
539 GenTensor<TENSOR_RESULT_TYPE(T,Q)> result(copy(*this));
540 result.scale(x);
541 return result;
542 }
543
545 GenTensor<T> result=copy(*this);
546 result.gaxpy(1.0,other,1.0);
547 return result;
548 }
549
551 GenTensor<T> result=copy(*this);
552 std::array<Slice,TENSOR_MAXDIM> s0;
553 s0.fill(_);
554 result.gaxpy(1.0,s0,other,1.0,other.thisslice);
555 return result;
556 }
557
559 gaxpy(1.0,other,1.0);
560 return *this;
561 }
562
564 std::array<Slice,TENSOR_MAXDIM> s0;
565 s0.fill(_);
566 this->gaxpy(1.0,s0,other,1.0,other.thisslice);
567 return *this;
568 }
569
571 GenTensor<T> result=copy(*this);
572 result.gaxpy(1.0,other,-1.0);
573 return result;
574 }
575
577 gaxpy(1.0,other,-1.0);
578 return *this;
579 }
580
582 std::array<Slice,TENSOR_MAXDIM> s0;
583 s0.fill(_);
584 this->gaxpy(1.0,s0,other,-1.0,other.thisslice);
585 return *this;
586 }
587
588 GenTensor& gaxpy(const T alpha, const GenTensor& other, const T beta) {
589
590 // deliberately excluding gaxpys for different tensors due to efficiency considerations!
593 if (is_full_tensor()) get_tensor().gaxpy(alpha,other.get_tensor(),beta);
594 else if (is_svd_tensor()) get_svdtensor().gaxpy(alpha,other.get_svdtensor(),beta);
595 else if (is_tensortrain()) get_tensortrain().gaxpy(alpha,other.get_tensortrain(),beta);
596 else {
597 MADNESS_EXCEPTION("unknown tensor type in LowRankTensor::gaxpy",1);
598 }
599 return *this;
600 }
601
602 GenTensor& gaxpy(const T alpha, std::array<Slice,TENSOR_MAXDIM> s0,
603 const GenTensor& other, const T beta, std::array<Slice,TENSOR_MAXDIM> s1) {
604
605 // deliberately excluding gaxpys for different tensors due to efficiency considerations!
608
609 if (is_full_tensor()) {
610 get_tensor()(s0).gaxpy(alpha,other.get_tensor()(s1),beta);
611 } else if (is_svd_tensor()) {
612 get_svdtensor().inplace_add(other.get_svdtensor(),s0,s1,alpha,beta);
613 } else if (is_tensortrain()) {
614 MADNESS_ASSERT(alpha==1.0);
615 get_tensortrain().gaxpy(s0, other.get_tensortrain(), beta, s1);
616 } else {
617 MADNESS_EXCEPTION("unknown tensor type in LowRankTensor::gaxpy",1);
618 }
619 return *this;
620 }
621
622 /// assign a number to this tensor
623 GenTensor& operator=(const T& number) {
624 std::visit([&number](auto& obj) {*obj=number;}, tensor);
625 return *this;
626
627 }
628
629 void add_SVD(const GenTensor& other, const double& thresh) {
630 if (is_full_tensor()) get_tensor()+=other.get_tensor();
631 else if (is_svd_tensor()) get_svdtensor().add_SVD(other.get_svdtensor(),thresh*facReduce());
632 else if (is_tensortrain()) get_tensortrain()+=(other.get_tensortrain());
633 else {
634 MADNESS_EXCEPTION("unknown tensor type in LowRankTensor::add_SVD",1);
635 }
636 }
637
638 /// Inplace multiply by corresponding elements of argument Tensor
640
641 // deliberately excluding emuls for different tensors due to efficiency considerations!
643
644 // binary operation with the visitor pattern
645 // std::visit([&other](auto& obj) {obj.emul(other.tensor);}, tensor);
646 if (is_full_tensor()) get_tensor().emul(other.get_tensor());
647 else if (is_svd_tensor()) get_svdtensor().emul(other.get_svdtensor());
648 else if (is_tensortrain()) get_tensortrain().emul(other.get_tensortrain());
649 else {
650 MADNESS_EXCEPTION("unknown tensor type in LowRankTensor::gaxpy",1);
651 }
652 return *this;
653
654 }
655
656 void reduce_rank(const double& thresh) {
657 if (is_svd_tensor()) get_svdtensor().divide_and_conquer_reduce(thresh*facReduce());
658 if (is_tensortrain()) get_tensortrain().truncate(thresh*facReduce());
659 }
660
661
662public:
663
664 /// Transform all dimensions of the tensor t by the matrix c
665
666 /// \ingroup tensor
667 /// Often used to transform all dimensions from one basis to another
668 /// \code
669 /// result(i,j,k...) <-- sum(i',j', k',...) t(i',j',k',...) c(i',i) c(j',j) c(k',k) ...
670 /// \endcode
671 /// The input dimensions of \c t must all be the same and agree with
672 /// the first dimension of \c c . The dimensions of \c c may differ in
673 /// size.
674 template <typename R, typename Q>
676 const GenTensor<R>& t, const Tensor<Q>& c);
677
678 /// Transform all dimensions of the tensor t by distinct matrices c
679
680 /// \ingroup tensor
681 /// Similar to transform but each dimension is transformed with a
682 /// distinct matrix.
683 /// \code
684 /// result(i,j,k...) <-- sum(i',j', k',...) t(i',j',k',...) c[0](i',i) c[1](j',j) c[2](k',k) ...
685 /// \endcode
686 /// The first dimension of the matrices c must match the corresponding
687 /// dimension of t. template <typename R, typename Q>
688 template <typename R, typename Q>
690 const GenTensor<R>& t, const Tensor<Q> c[]);
691
692 /// Transforms one dimension of the tensor t by the matrix c, returns new contiguous tensor
693
694 /// \ingroup tensor
695 /// \code
696 /// transform_dir(t,c,1) = r(i,j,k,...) = sum(j') t(i,j',k,...) * c(j',j)
697 /// \endcode
698 /// @param[in] t Tensor to transform (size of dim to be transformed must match size of first dim of \c c )
699 /// @param[in] c Matrix used for the transformation
700 /// @param[in] axis Dimension (or axis) to be transformed
701 /// @result Returns a new, contiguous tensor template <typename R, typename Q>
702 template <typename R, typename Q>
704 const GenTensor<R>& t, const Tensor<Q>& c, const int axis);
705
706
707 std::string what_am_i() const {
709 if (this->is_full_tensor()) tt=TT_FULL;
710 if (this->is_svd_tensor()) tt=TT_2D;
711 if (this->is_tensortrain()) tt=TT_TENSORTRAIN;
713 };
714
715
716 /// might return a NULL pointer!
717 const BaseTensor* ptr() const {
718 const BaseTensor* p;
719 std::visit([&p](auto& obj) {p=dynamic_cast<const BaseTensor*>(obj.get());}, tensor);
720 return p;
721 }
722
723private:
724
725 /// holding the implementation of the low rank tensor representations
726 // std::variant<Tensor<T>, SVDTensor<T>, TensorTrain<T> > tensor;
727 std::variant<std::shared_ptr<Tensor<T> >,
728 std::shared_ptr<SVDTensor<T> >,
729 std::shared_ptr<TensorTrain<T> > > tensor;
730
731};
732
733
734
735namespace archive {
736/// Serialize a tensor
737template <class Archive, typename T>
738struct ArchiveStoreImpl< Archive, GenTensor<T> > {
739
740 friend class GenTensor<T>;
741 /// Stores the GenTensor to an archive
742 static void store(const Archive& ar, const GenTensor<T>& t) {
743 int index1=t.index();
744 ar & index1;
745 if (index1==0) {
746 const Tensor<T>& tt=t.get_tensor();
747 ar & tt;
748 } else if (index1==1) {
749 const SVDTensor<T>& tt=t.get_svdtensor();
750 ar & tt;
751 } else if (index1==2) {
753 ar & tt;
754 }
755 };
756};
757
758
759/// Deserialize a tensor ... existing tensor is replaced
760template <class Archive, typename T>
761struct ArchiveLoadImpl< Archive, GenTensor<T> > {
762
763 friend class GenTensor<T>;
764 /// Replaces this GenTensor with one loaded from an archive
765 static void load(const Archive& ar, GenTensor<T>& tensor) {
766 int index=-2;
767 ar & index;
768 if (index==0) {
770 ar & tt;
771 tensor=tt;
772 } else if (index==1) {
774 ar & tt;
775 tensor=tt;
776 } else if (index==2) {
778 ar & tt;
779 tensor=tt;
780 } else if (index==-1) { // defined value: empty tensor
781 ;
782 } else {
783 MADNESS_EXCEPTION("unknow tensor type",1);
784 }
785
786
787 };
788};
789};
790
791/// type conversion implies a deep copy
792
793/// @result Returns a new tensor that is a deep copy of the input
794template <class Q, class T>
796
797 // simple return
798 if (std::is_same<Q, T>::value) return copy(other);
799
800 GenTensor<Q> result;
801 if (other.is_full_tensor())
802 result=Tensor<Q>(convert<Q,T>(other.get_tensor()));
803 if (other.is_svd_tensor())
804 MADNESS_EXCEPTION("no type conversion for SVDTensors",1);
805 if (other.is_tensortrain())
806 MADNESS_EXCEPTION("no type conversion for TensorTrain",1);
807 return result;
808}
809
810
811/// change representation to targ.tt
812template<typename T>
813void change_tensor_type(GenTensor<T>& t, const TensorArgs& targs) {
814 t.convert_inplace(targs);
815}
816
817/// outer product of two Tensors, yielding a low rank tensor
818
819/// do the outer product of two tensors; distinguish these tensortype cases by
820/// the use of final_tensor_type
821/// - full x full -> full
822/// - full x full -> SVD ( default )
823/// - TensorTrain x TensorTrain -> TensorTrain
824/// all other combinations are currently invalid.
825template <class T, class Q>
827 const GenTensor<Q>& t2, const TensorArgs final_tensor_args=TensorArgs(-1.0,TT_2D)) {
828
829 typedef TENSOR_RESULT_TYPE(T,Q) resultT;
830
831
833
834 if (final_tensor_args.tt==TT_FULL) {
837 return GenTensor<resultT>(t);
838
839 } else if (final_tensor_args.tt==TT_2D) {
841
842 // srconf is shallow, do deep copy here
843 const Tensor<T> lhs=t1.full_tensor_copy();
844 const Tensor<Q> rhs=t2.full_tensor_copy();
845
846 const long k=lhs.dim(0);
847 const long ndim=lhs.ndim()+rhs.ndim();
848 long size=1;
849 for (int i=0; i<lhs.ndim(); ++i) size*=k;
850 MADNESS_ASSERT(size==lhs.size());
851 MADNESS_ASSERT(size==rhs.size());
852 MADNESS_ASSERT(lhs.size()==rhs.size());
853
855 weights=1.0;
856
857 std::array<long,TENSOR_MAXDIM> dims;
858 for (int i=0; i<t1.ndim(); ++i) dims[i]=t1.dim(i);
859 for (int i=0; i<t2.ndim(); ++i) dims[i+t1.ndim()]=t2.dim(i);
860
861 SRConf<resultT> srconf(weights,lhs.reshape(1,lhs.size()),rhs.reshape(1,rhs.size()),ndim,dims.data(),t1.ndim());
862// srconf.normalize();
864
865 } else if (final_tensor_args.tt==TT_TENSORTRAIN) {
868 return outer(t1.get_tensortrain(),t2.get_tensortrain());
869 } else {
870 MADNESS_EXCEPTION("you should not be here",1);
871 }
873
874 }
875
876
877/// outer product of two Tensors, yielding a low rank tensor
878template <class T, class Q>
880 const Tensor<Q>& rhs2, const TensorArgs final_tensor_args) {
881
882 typedef TENSOR_RESULT_TYPE(T,Q) resultT;
883
884 // prepare lo-dim tensors for the outer product
885 TensorArgs targs;
886 targs.thresh=final_tensor_args.thresh;
887 if (final_tensor_args.tt==TT_FULL) targs.tt=TT_FULL;
888 else if (final_tensor_args.tt==TT_2D) targs.tt=TT_FULL;
889 else if (final_tensor_args.tt==TT_TENSORTRAIN) targs.tt=TT_TENSORTRAIN;
890 else {
891 MADNESS_EXCEPTION("confused tensor args in outer_low_rank",1);
892 }
893
894 GenTensor<T> lhs(lhs2,targs);
895 GenTensor<Q> rhs(rhs2,targs);
896 GenTensor<resultT> result=outer(lhs,rhs,final_tensor_args);
897 return result;
898 }
899
900
901/// The class defines tensor op scalar ... here define scalar op tensor.
902template <typename T, typename Q>
903typename IsSupported < TensorTypeData<Q>, GenTensor<T> >::type
904operator*(const Q& x, const GenTensor<T>& t) {
905 return t*x;
906}
907
908/// add all the GenTensors of a given list
909
910 /// If there are many tensors to add it's beneficial to do a sorted addition and start with
911 /// those tensors with low ranks
912 /// @param[in] addends a list with gentensors of same dimensions; will be destroyed upon return
913 /// @param[in] eps the accuracy threshold
914 /// @param[in] are_optimal flag if the GenTensors in the list are already in SVD format (if TT_2D)
915 /// @return the sum GenTensor of the input GenTensors
916 template<typename T>
917GenTensor<T> reduce(std::list<GenTensor<T> >& addends, double eps, bool are_optimal=false) {
918
919 // fast return
920 addends.remove_if([](auto element) {return not element.is_assigned();});
921 addends.remove_if([](auto element) {return element.rank()==0;});
922 if (addends.size()==0) return GenTensor<T>();
923
924
925 if (addends.front().is_svd_tensor()) {
926 std::list<SVDTensor<T> > addends1;
927 for (auto a : addends) addends1.push_back(a.get_svdtensor());
928 return reduce(addends1,eps*GenTensor<T>::facReduce());
929 }
930 // make error relative
931 eps=eps/addends.size();
932
933 // if the addends are not in SVD format do that now so that we can call add_svd later
934 if (not are_optimal) {
935 for (auto element : addends) element.reduce_rank(eps);
936 }
937
938 // remove zero ranks and sort the list according to the gentensor's ranks
939 addends.remove_if([](auto element) {return element.rank()==0;});
940 if (addends.size()==0) return GenTensor<T>();
941 addends.sort([](auto element1, auto element2) {return element1.rank()<element2.rank();});
942
943 // do the additions
944 GenTensor<T> result=copy(addends.front());
945 addends.pop_front();
946 for (auto element : addends) result.add_SVD(element,eps);
947 addends.clear();
948
949 return result;
950}
951
952
953
954
955/// implements a temporary(!) slice of a LowRankTensor
956template<typename T>
957class SliceLowRankTensor : public GenTensor<T> {
958 //class SliceLowRankTensor {
959public:
960
961 std::array<Slice,TENSOR_MAXDIM> thisslice;
962 // GenTensor<T>* lrt;
963
964 // all ctors are private, only accessible by GenTensor
965
966 /// default ctor
968
969 /// ctor with a GenTensor; shallow
970 SliceLowRankTensor(const GenTensor<T>& gt, const std::vector<Slice>& s)
971 : GenTensor<T>(const_cast<GenTensor<T>& > (gt)) {
972 // : Tensor<T>(const_cast<Tensor<T>&>(t)) //!!!!!!!!!!!
973 thisslice.fill(_);
974 for (int i=0; i<s.size(); ++i) thisslice[i]=s[i];
975 }
976
977 /// ctor with a GenTensor; shallow
978 SliceLowRankTensor(const GenTensor<T>& gt, const std::array<Slice,TENSOR_MAXDIM>& s)
979 : GenTensor<T>(&gt), thisslice(s) {}
980
981public:
982
983 /// assignment as in g(s) = g1;
985 print("You don't want to assign to a SliceLowRankTensor; use operator+= instead");
987 return *this;
988 };
989
990 /// assignment as in g(s) = g1(s);
992 print("You don't want to assign to a SliceLowRankTensor; use operator+= instead");
994 return *this;
995 };
996
997 /// inplace addition as in g(s)+=g1
999 std::array<Slice,TENSOR_MAXDIM> rhs_slice;
1000 rhs_slice.fill(_);
1001 gaxpy(thisslice,rhs,rhs_slice,1.0);
1002 return *this;
1003 }
1004
1005 /// inplace subtraction as in g(s)-=g1
1007 std::array<Slice,TENSOR_MAXDIM> rhs_slice;
1008 rhs_slice.fill(_);
1009 gaxpy(thisslice,rhs,rhs_slice,-1.0);
1010 return *this;
1011 }
1012
1013 /// inplace addition as in g(s)+=g1(s)
1015 gaxpy(thisslice,rhs,rhs.thisslice,1.0);
1016 return *this;
1017 }
1018
1019 /// inplace addition as in g(s)-=g1(s)
1021 gaxpy(thisslice,rhs,rhs.thisslice,-1.0);
1022 return *this;
1023 }
1024
1025 /// *this = *this(s) + beta * rhs
1026 void gaxpy(const std::array<Slice,TENSOR_MAXDIM>& lslice, const GenTensor<T>& rhs,
1027 const std::array<Slice,TENSOR_MAXDIM>& rslice, const double& beta) {
1028
1029 // fast return if possible
1030 if (rhs.has_no_data() or rhs.rank()==0) return;
1031
1032 if (this->has_data()) MADNESS_ASSERT(is_same_tensor_type(*this,rhs));
1033
1034 if (this->is_full_tensor()) {
1035 this->get_tensor()(thisslice).gaxpy(1.0,rhs.get_tensor()(rslice),beta);
1036
1037 } else if (this->is_svd_tensor()) {
1038 this->get_svdtensor().inplace_add(rhs.get_svdtensor(),thisslice,rslice, 1.0, beta);
1039
1040 } else if (this->is_tensortrain()) {
1041 this->get_tensortrain().gaxpy(thisslice,rhs.get_tensortrain(),beta,rslice);
1042 }
1043 return ;
1044 }
1045
1046 /// inplace zero-ing as in g(s)=0.0
1048 MADNESS_ASSERT(number==T(0.0));
1049
1050 if (this->is_full_tensor()) {
1051 this->get_tensor()(thisslice)=0.0;
1052
1053 } else if (this->is_svd_tensor()) {
1054 MADNESS_ASSERT(this->get_svdtensor().has_structure());
1055 SliceLowRankTensor<T> tmp(*this);
1056 this->get_svdtensor().inplace_add(tmp.get_svdtensor(),thisslice,thisslice, 1.0, -1.0);
1057
1058 } else if (this->is_tensortrain()) {
1059 this->get_tensortrain().gaxpy(thisslice,this->get_tensortrain(),-1.0,thisslice);
1060 } else {
1061 MADNESS_EXCEPTION("you should not be here",1);
1062 }
1063 return *this;
1064 }
1065
1067 GenTensor<T> result;
1068 const std::array<Slice,TENSOR_MAXDIM> s=other.thisslice;
1069 if (other.is_full_tensor())
1070 result=Tensor<T>(copy(other.get_tensor()(s)));
1071 else if (other.is_svd_tensor())
1072 result=SVDTensor<T>(other.get_svdtensor().copy_slice(s));
1073 else if (other.is_tensortrain())
1074 result=TensorTrain<T>(copy(other.get_tensortrain(),s));
1075 else {
1076 }
1077 return result;
1078 }
1079
1080
1081};
1082
1083
1084template<typename Q, typename R>
1085bool is_same_tensor_type(const GenTensor<R>& rhs, const GenTensor<Q>& lhs) {
1086 return (rhs.tensor.index()==lhs.tensor.index());
1087}
1088
1089template <typename R, typename Q>
1091 const GenTensor<R>& t, const Tensor<Q>& c) {
1092 typedef TENSOR_RESULT_TYPE(R,Q) resultT;
1093 GenTensor<resultT> result;
1094 std::visit([&result, &c](auto& obj) {result=transform(*obj,c);}, t.tensor);
1095 return result;
1096 }
1097
1098template <typename R, typename Q>
1100 const GenTensor<R>& t, const Tensor<Q> c[]) {
1101 typedef TENSOR_RESULT_TYPE(R,Q) resultT;
1102 GenTensor<resultT> result;
1103 std::visit([&result, &c](auto& obj) {result=general_transform(*obj,c);}, t.tensor);
1104 return result;
1105}
1106
1107template <typename R, typename Q>
1109 const GenTensor<R>& t, const Tensor<Q>& c, const int axis) {
1111 std::visit([&result, &c, &axis](auto& obj) {result=transform_dir(*obj,c,axis);}, t.tensor);
1112 return result;
1113
1114 }
1115
1116
1117
1118} // namespace madness
1119
1120#endif /* MADNESS_TENSOR_LOWRANKTENSOR_H_ */
The base class for tensors defines generic capabilities.
Definition basetensor.h:85
long dim(int i) const
Returns the size of dimension i.
Definition basetensor.h:147
const long * dims() const
Returns the array of tensor dimensions.
Definition basetensor.h:153
long ndim() const
Returns the number of dimensions in the tensor.
Definition basetensor.h:144
long size() const
Returns the number of elements in the tensor.
Definition basetensor.h:138
Definition lowranktensor.h:59
TensorTypeData< T >::float_scalar_type float_scalar_type
C++ typename of the floating point type associated with scalar real type.
Definition lowranktensor.h:69
bool is_of_tensortype(const TensorType &tt) const
Definition lowranktensor.h:309
GenTensor(const GenTensor< T > &other)=default
copy ctor, shallow
GenTensor(const TensorTrain< T > &other)
ctor with a TensorTrain as argument, shallow
Definition lowranktensor.h:149
GenTensor convert(const TensorArgs &targs) const
convert this to a new LowRankTensor of given tensor type
Definition lowranktensor.h:373
GenTensor convert(const TensorArgs &) const
Definition gentensor.h:198
long dim(const int i) const
return the number of entries in dimension i
Definition lowranktensor.h:391
GenTensor operator+(const SliceLowRankTensor< T > &other)
Definition lowranktensor.h:550
static double facReduce()
Definition lowranktensor.h:469
friend class SliceLowRankTensor< T >
Definition lowranktensor.h:63
Tensor< T > full_tensor_copy() const
Definition gentensor.h:206
long ndim() const
Definition lowranktensor.h:386
GenTensor & operator+=(const GenTensor &other)
Definition lowranktensor.h:558
friend GenTensor copy(const GenTensor &other)
deep copy
Definition lowranktensor.h:283
constexpr bool is_full_tensor() const
Definition gentensor.h:224
TENSOR_RESULT_TYPE(T, Q) trace_conj(const GenTensor< Q > &rhs) const
returns the trace of <this|rhs>
Definition lowranktensor.h:516
Tensor< T > & get_tensor()
Definition lowranktensor.h:235
TensorTypeData< T >::scalar_type scalar_type
C++ typename of the real type associated with a complex type.
Definition lowranktensor.h:66
SliceLowRankTensor< T > operator()(const std::vector< Slice > &s)
general slicing, shallow; for temporary use only!
Definition lowranktensor.h:272
GenTensor< TENSOR_RESULT_TYPE(T, Q)> operator*(const Q &x) const
multiply with a number
Definition lowranktensor.h:538
GenTensor & operator=(const SliceLowRankTensor< T > &other)
deep assignment with slices: g0 = g1(s)
Definition lowranktensor.h:188
const Tensor< T > & get_tensor() const
Definition gentensor.h:203
long nCoeff() const
Definition lowranktensor.h:494
GenTensor & operator=(const GenTensor< T > &other)
shallow assignment operator
Definition lowranktensor.h:164
std::variant< std::shared_ptr< Tensor< T > >, std::shared_ptr< SVDTensor< T > >, std::shared_ptr< TensorTrain< T > > > tensor
holding the implementation of the low rank tensor representations
Definition lowranktensor.h:729
Tensor< T > & full_tensor()
Definition lowranktensor.h:454
std::string what_am_i() const
Definition lowranktensor.h:707
GenTensor & operator=(const Tensor< T > &other)
deep assignment operator
Definition lowranktensor.h:170
int index() const
Definition lowranktensor.h:319
bool has_no_data() const
Definition lowranktensor.h:488
GenTensor(const SVDTensor< T > &other)
ctor with a SVDTensor as argument, shallow
Definition lowranktensor.h:154
GenTensor & operator=(const TensorTrain< T > &other)
deep assignment operator
Definition lowranktensor.h:182
GenTensor & convert_inplace(const TensorArgs &targs)
Definition lowranktensor.h:323
void normalize()
Definition lowranktensor.h:402
GenTensor(const SliceLowRankTensor< T > &other)
ctor with a SliceLowRankTensor as argument, deep
Definition lowranktensor.h:159
GenTensor & operator=(const SVDTensor< T > &other)
deep assignment operator
Definition lowranktensor.h:176
GenTensor & gaxpy(const T alpha, std::array< Slice, TENSOR_MAXDIM > s0, const GenTensor &other, const T beta, std::array< Slice, TENSOR_MAXDIM > s1)
Definition lowranktensor.h:602
const SVDTensor< T > & get_svdtensor() const
Definition lowranktensor.h:226
GenTensor & operator-=(const SliceLowRankTensor< T > &other)
Definition lowranktensor.h:581
GenTensor & operator=(const T &number)
assign a number to this tensor
Definition lowranktensor.h:623
GenTensor< T > & emul(const GenTensor< T > &other)
Inplace multiply by corresponding elements of argument Tensor.
Definition lowranktensor.h:639
GenTensor(const Tensor< T > &rhs, const TensorArgs &targs)
ctor with a regular Tensor and arguments, deep
Definition lowranktensor.h:104
float_scalar_type normf() const
Definition lowranktensor.h:406
constexpr bool is_tensortrain() const
Definition gentensor.h:223
Tensor< T > full_tensor() const
Definition lowranktensor.h:447
TensorTrain< T > & get_tensortrain()
Definition lowranktensor.h:253
long rank() const
Definition gentensor.h:212
GenTensor(const std::vector< long > &dim, const TensorType &tt)
ctor with dimensions; constructs tensor filled with zeros
Definition lowranktensor.h:84
const Tensor< T > & full_tensor() const
Definition gentensor.h:200
long size() const
Definition lowranktensor.h:490
GenTensor operator-(const GenTensor &other)
Definition lowranktensor.h:570
long real_size() const
Definition lowranktensor.h:504
GenTensor()=default
empty ctor
void add_SVD(const GenTensor &other, const double &thresh)
Definition lowranktensor.h:629
SVDTensor< T > & get_svdtensor()
Definition gentensor.h:228
GenTensor()
Definition gentensor.h:180
GenTensor(const Tensor< T > &rhs, const double &thresh, const TensorType &tt)
ctor with a regular Tensor and arguments, deep
Definition lowranktensor.h:99
GenTensor(const Tensor< T > &other)
ctor with a regular Tensor, deep
Definition lowranktensor.h:144
const SliceLowRankTensor< T > operator()(const std::vector< Slice > &s) const
general slicing, shallow; for temporary use only!
Definition lowranktensor.h:277
const TensorTrain< T > & get_tensortrain() const
Definition lowranktensor.h:262
GenTensor(const std::vector< long > &dim, const TensorArgs &targs)
ctor with dimensions; constructs tensor filled with zeros
Definition lowranktensor.h:89
void reduce_rank(const double &thresh)
Definition lowranktensor.h:656
TensorType tensor_type() const
return the tensor type
Definition lowranktensor.h:290
const BaseTensor * ptr() const
might return a NULL pointer!
Definition lowranktensor.h:717
static double fac_reduce()
Definition lowranktensor.h:470
friend bool is_same_tensor_type(const GenTensor< R > &rhs, const GenTensor< Q > &lhs)
Definition lowranktensor.h:1085
bool has_data() const
Definition lowranktensor.h:486
GenTensor & operator-=(const GenTensor &other)
Definition lowranktensor.h:576
const long * dims() const
return the number of entries in dimension i
Definition lowranktensor.h:397
Tensor< T > reconstruct_tensor() const
reconstruct this to return a full tensor
Definition lowranktensor.h:461
GenTensor operator+(const GenTensor &other)
Definition lowranktensor.h:544
float_scalar_type svd_normf() const
Definition lowranktensor.h:413
GenTensor & gaxpy(const T alpha, const GenTensor &other, const T beta)
Definition lowranktensor.h:588
GenTensor(const TensorType &tt, const long k, const long ndim)
ctor with dimensions; all dims have the same value k
Definition lowranktensor.h:94
bool is_assigned() const
Definition gentensor.h:209
IsSupported< TensorTypeData< Q >, GenTensor< T > & >::type scale(Q fac)
Inplace multiplication by scalar of supported type (legacy name).
Definition lowranktensor.h:428
GenTensor(const long ndim, const long *dims, const TensorType &tt)
Definition lowranktensor.h:77
GenTensor & operator+=(const SliceLowRankTensor< T > &other)
Definition lowranktensor.h:563
SVDTensor< T > & get_tensortrain()
Definition gentensor.h:229
constexpr bool is_svd_tensor() const
Definition gentensor.h:222
Definition srconf.h:68
void normalize()
normalize the vectors (tested)
Definition srconf.h:592
Definition SVDTensor.h:42
implements a temporary(!) slice of a LowRankTensor
Definition lowranktensor.h:957
SliceLowRankTensor< T > & operator+=(const SliceLowRankTensor< T > &rhs)
inplace addition as in g(s)+=g1(s)
Definition lowranktensor.h:1014
SliceLowRankTensor(const GenTensor< T > &gt, const std::vector< Slice > &s)
ctor with a GenTensor; shallow
Definition lowranktensor.h:970
SliceLowRankTensor< T > & operator+=(const GenTensor< T > &rhs)
inplace addition as in g(s)+=g1
Definition lowranktensor.h:998
SliceLowRankTensor< T > & operator=(const GenTensor< T > &rhs)
assignment as in g(s) = g1;
Definition lowranktensor.h:984
std::array< Slice, TENSOR_MAXDIM > thisslice
Definition lowranktensor.h:961
void gaxpy(const std::array< Slice, TENSOR_MAXDIM > &lslice, const GenTensor< T > &rhs, const std::array< Slice, TENSOR_MAXDIM > &rslice, const double &beta)
*this = *this(s) + beta * rhs
Definition lowranktensor.h:1026
SliceLowRankTensor< T > & operator-=(const SliceLowRankTensor< T > &rhs)
inplace addition as in g(s)-=g1(s)
Definition lowranktensor.h:1020
SliceLowRankTensor< T > & operator=(const T &number)
inplace zero-ing as in g(s)=0.0
Definition lowranktensor.h:1047
SliceLowRankTensor(const GenTensor< T > &gt, const std::array< Slice, TENSOR_MAXDIM > &s)
ctor with a GenTensor; shallow
Definition lowranktensor.h:978
SliceLowRankTensor< T > & operator=(const SliceLowRankTensor< T > &rhs)
assignment as in g(s) = g1(s);
Definition lowranktensor.h:991
SliceLowRankTensor< T > & operator-=(const GenTensor< T > &rhs)
inplace subtraction as in g(s)-=g1
Definition lowranktensor.h:1006
SliceLowRankTensor()
default ctor
Definition lowranktensor.h:967
friend GenTensor< T > copy(const SliceLowRankTensor< T > &other)
Definition lowranktensor.h:1066
Definition tensortrain.h:123
Traits class to specify support of numeric types.
Definition type_data.h:56
A tensor is a multidimensional array.
Definition tensor.h:313
Tensor< T > reshape(int ndimnew, const long *d)
Returns new view/tensor reshaping size/number of dimensions to conforming tensor.
Definition tensor.h:1389
Tensor()
Default constructor does not allocate any data and sets ndim=-1, size=0, _p=0, and id.
Definition tensor.h:411
static const double R
Definition csqrt.cc:46
char * p(char *buf, const char *name, int k, int initial_level, double thresh, int order)
Definition derivatives.cc:72
friend GenTensor< TENSOR_RESULT_TYPE(R, Q)> transform_dir(const GenTensor< R > &t, const Tensor< Q > &c, const int axis)
Transforms one dimension of the tensor t by the matrix c, returns new contiguous tensor.
Definition lowranktensor.h:1108
friend GenTensor< TENSOR_RESULT_TYPE(R, Q)> transform(const GenTensor< R > &t, const Tensor< Q > &c)
Transform all dimensions of the tensor t by the matrix c.
Definition lowranktensor.h:1090
friend GenTensor< TENSOR_RESULT_TYPE(R, Q)> general_transform(const GenTensor< R > &t, const Tensor< Q > c[])
Transform all dimensions of the tensor t by distinct matrices c.
Definition gentensor.h:274
Tensor< T > transpose(const Tensor< T > &t)
Returns a new deep copy of the transpose of the input tensor.
Definition tensor.h:2039
const double beta
Definition gygi_soltion.cc:62
Defines madness::MadnessException for exception handling.
#define MADNESS_EXCEPTION(msg, value)
Macro for throwing a MADNESS exception.
Definition madness_exception.h:119
#define MADNESS_ASSERT(condition)
Assert a condition that should be free of side-effects since in release builds this might be a no-op.
Definition madness_exception.h:134
Namespace for I/O tools.
Definition madness.h:49
Namespace for all elements and tools of MADNESS.
Definition DFConvergence.h:9
double FunctionDefaults< NDIM >::thresh
Definition mraimpl.h:3879
GenTensor< TENSOR_RESULT_TYPE(R, Q)> general_transform(const GenTensor< R > &t, const Tensor< Q > c[])
Definition gentensor.h:274
int FunctionDefaults< NDIM >::k
Definition mraimpl.h:3878
Function< Q, NDIM > convert(const Function< T, NDIM > &f, bool fence=true)
Type conversion implies a deep copy. No communication except for optional fence.
Definition mra.h:2286
std::vector< Function< TENSOR_RESULT_TYPE(T, R), NDIM > > transform(World &world, const std::vector< Function< T, NDIM > > &v, const Tensor< R > &c, bool fence=true)
Transforms a vector of functions according to new[i] = sum[j] old[j]*c[j,i].
Definition vmra.h:807
static Tensor< double > weights[max_npt+1]
Definition legendre.cc:99
std::vector< CCPairFunction< T, NDIM > > operator*(const double fac, const std::vector< CCPairFunction< T, NDIM > > &arg)
Definition ccpairfunction.h:1111
static const Slice _(0,-1, 1)
std::enable_if< std::is_base_of< ProjectorBase, projT >::value, OuterProjector< projT, projQ > >::type outer(const projT &p0, const projQ &p1)
Definition projector.h:476
TensorType
low rank representations of tensors (see gentensor.h)
Definition gentensor.h:120
@ TT_TENSORTRAIN
Definition gentensor.h:120
@ TT_2D
Definition gentensor.h:120
@ TT_FULL
Definition gentensor.h:120
GenTensor< T > reduce(std::list< GenTensor< T > > &addends, double, bool=false)
add all the GenTensors of a given list
Definition gentensor.h:246
TensorType FunctionDefaults< NDIM >::tt
Definition mraimpl.h:3891
void print(const T &t, Ts &&... ts)
Print items to std::cout (items separated by spaces) and terminate with a new line.
Definition print.h:232
void change_tensor_type(GenTensor< T > &, const TensorArgs &targs)
change representation to targ.tt
Definition gentensor.h:284
GenTensor< TENSOR_RESULT_TYPE(R, Q)> transform_dir(const GenTensor< R > &t, const Tensor< Q > &c, const int axis)
Definition lowranktensor.h:1108
std::string type(const PairType &n)
Definition PNOParameters.h:18
Function< T, NDIM > copy(const Function< T, NDIM > &f, const std::shared_ptr< WorldDCPmapInterface< Key< NDIM > > > &pmap, bool fence=true)
Create a new copy of the function with different distribution and optional fence.
Definition mra.h:2239
bool is_same_tensor_type(const GenTensor< R > &rhs, const GenTensor< Q > &lhs)
Definition lowranktensor.h:1085
Definition mraimpl.h:53
static const double a
Definition nonlinschro.cc:118
Defines simple templates for printing to std::cout "a la Python".
double Q(double a)
Definition relops.cc:20
static const double c
Definition relops.cc:10
static const double m
Definition relops.cc:9
Declares and implements Slice.
Definition type_data.h:146
TensorArgs holds the arguments for creating a LowRankTensor.
Definition gentensor.h:134
static std::string what_am_i(const TensorType &tt)
Definition gentensor.h:147
double thresh
Definition gentensor.h:135
TensorType tt
Definition gentensor.h:136
static void load(const Archive &ar, GenTensor< T > &tensor)
Replaces this GenTensor with one loaded from an archive.
Definition lowranktensor.h:765
Default store of an object via serialize(ar, t).
Definition archive.h:612
static std::enable_if_t< is_output_archive_v< A > &&!std::is_function< U >::value &&(has_member_serialize_v< U, A >||has_nonmember_serialize_v< U, A >||has_freestanding_serialize_v< U, A >||has_freestanding_default_serialize_v< U, A >), void > store(const A &ar, const U &t)
Definition archive.h:622
static const double s0
Definition tdse4.cc:83
Defines and implements most of Tensor.
Defines and implements the tensor train decomposition as described in I.V. Oseledets,...
double norm(const T i1)
Definition test_cloud.cc:85
static const double alpha
Definition testcosine.cc:10
double gt(const coord_t &r)
Definition testgconv.cc:142
std::size_t axis
Definition testpdiff.cc:59
Defines and implements TensorTypeData, a type traits class.
#define TENSOR_RESULT_TYPE(L, R)
This macro simplifies access to TensorResultType.
Definition type_data.h:205