34 #ifndef MADNESS_TENSOR_LOWRANKTENSOR_H_
35 #define MADNESS_TENSOR_LOWRANKTENSOR_H_
55 template <
class T>
class SliceLowRankTensor;
189 const std::array<Slice,TENSOR_MAXDIM>& s=other.
thisslice;
220 return *(std::get<1>(
tensor).get());
229 return *(std::get<1>(
tensor).get());
238 return *(std::get<0>(
tensor).get());
247 return *(std::get<0>(
tensor).get());
256 return *(std::get<2>(
tensor).get());
265 return *(std::get<2>(
tensor).get());
311 if ((
index()==1) and (tt==
TT_2D))
return true;
316 template<
typename Q,
typename R>
345 long rank = s.size();
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);
366 print(
"conversion from type ",
index(),
"to type", targs.
tt,
"not supported");
367 MADNESS_EXCEPTION(
"type conversion not supported in LowRankTensor::convert ", 1);
391 long dim(
const int i)
const {
424 template <
typename Q>
428 std::visit([&fac](
auto& obj) {obj->scale(
T(fac));},
tensor);
469 return *(std::max_element(r.begin(), r.end()));
475 return ptr() ? true :
false;
515 if ((this->
rank()==0) or (rhs.
rank()==0))
return resultT(0.0);
538 result.
gaxpy(1.0,other,1.0);
544 std::array<Slice,TENSOR_MAXDIM>
s0;
551 gaxpy(1.0,other,1.0);
556 std::array<Slice,TENSOR_MAXDIM>
s0;
564 result.
gaxpy(1.0,other,-1.0);
569 gaxpy(1.0,other,-1.0);
574 std::array<Slice,TENSOR_MAXDIM>
s0;
595 const GenTensor& other,
const T beta, std::array<Slice,TENSOR_MAXDIM> s1) {
616 std::visit([&number](
auto& obj) {*obj=number;},
tensor);
666 template <
typename R,
typename Q>
680 template <
typename R,
typename Q>
694 template <
typename R,
typename Q>
719 std::variant<std::shared_ptr<Tensor<T> >,
720 std::shared_ptr<SVDTensor<T> >,
721 std::shared_ptr<TensorTrain<T> > >
tensor;
729 template <
class Archive,
typename T>
735 int index1=t.
index();
740 }
else if (index1==1) {
743 }
else if (index1==2) {
752 template <
class Archive,
typename T>
753 struct ArchiveLoadImpl< Archive,
GenTensor<
T> > {
764 }
else if (index==1) {
768 }
else if (index==2) {
772 }
else if (index==-1) {
786 template <
class Q,
class T>
790 if (std::is_same<Q, T>::value)
return copy(other);
806 t.convert_inplace(targs);
817 template <
class T,
class Q>
826 if (final_tensor_args.tt==
TT_FULL) {
831 }
else if (final_tensor_args.tt==
TT_2D) {
838 const long k=lhs.
dim(0);
839 const long ndim=lhs.
ndim()+rhs.
ndim();
841 for (
int i=0; i<lhs.
ndim(); ++i) size*=
k;
849 std::array<long,TENSOR_MAXDIM> dims;
850 for (
int i=0; i<t1.
ndim(); ++i) dims[i]=t1.
dim(i);
851 for (
int i=0; i<t2.
ndim(); ++i) dims[i+t1.
ndim()]=t2.
dim(i);
870 template <
class T,
class Q>
894 template <
typename T,
typename Q>
895 typename IsSupported < TensorTypeData<Q>, GenTensor<T> >
::type
909 GenTensor<T>
reduce(std::list<GenTensor<T> >& addends,
double eps,
bool are_optimal=
false) {
912 addends.remove_if([](
auto element) {
return not element.is_assigned();});
913 addends.remove_if([](
auto element) {
return element.rank()==0;});
914 if (addends.size()==0)
return GenTensor<T>();
917 if (addends.front().is_svd_tensor()) {
918 std::list<SVDTensor<T> > addends1;
919 for (
auto a : addends) addends1.push_back(
a.get_svdtensor());
923 eps=eps/addends.size();
926 if (not are_optimal) {
927 for (
auto element : addends) element.reduce_rank(eps);
931 addends.remove_if([](
auto element) {
return element.rank()==0;});
932 if (addends.size()==0)
return GenTensor<T>();
933 addends.sort([](
auto element1,
auto element2) {
return element1.rank()<element2.rank();});
936 GenTensor<T> result=
copy(addends.front());
938 for (
auto element : addends) result.add_SVD(element,eps);
965 for (
int i=0; i<s.size(); ++i)
thisslice[i]=s[i];
976 print(
"You don't want to assign to a SliceLowRankTensor; use operator+= instead");
983 print(
"You don't want to assign to a SliceLowRankTensor; use operator+= instead");
990 std::array<Slice,TENSOR_MAXDIM> rhs_slice;
998 std::array<Slice,TENSOR_MAXDIM> rhs_slice;
1018 const std::array<Slice,TENSOR_MAXDIM>& rslice,
const double&
beta) {
1059 const std::array<Slice,TENSOR_MAXDIM> s=other.
thisslice;
1075 template<
typename Q,
typename R>
1080 template <
typename R,
typename Q>
1089 template <
typename R,
typename Q>
1098 template <
typename R,
typename Q>
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 & operator=(const SliceLowRankTensor< T > &other)
deep assignment with slices: g0 = g1(s)
Definition: lowranktensor.h:188
GenTensor & operator-=(const SliceLowRankTensor< T > &other)
Definition: lowranktensor.h:573
GenTensor & operator+=(const GenTensor &other)
Definition: lowranktensor.h:550
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< T > & emul(const GenTensor< T > &other)
Inplace multiply by corresponding elements of argument Tensor.
Definition: lowranktensor.h:631
GenTensor convert(const TensorArgs &targs) const
Definition: gentensor.h:198
GenTensor & operator+=(const SliceLowRankTensor< T > &other)
Definition: lowranktensor.h:555
GenTensor full_tensor() const
Definition: gentensor.h:200
long dim(const int i) const
return the number of entries in dimension i
Definition: lowranktensor.h:391
const SVDTensor< T > & get_svdtensor() const
Definition: lowranktensor.h:226
GenTensor operator+(const SliceLowRankTensor< T > &other)
Definition: lowranktensor.h:542
static double facReduce()
Definition: lowranktensor.h:461
friend class SliceLowRankTensor< T >
Definition: lowranktensor.h:63
long ndim() const
Definition: lowranktensor.h:386
GenTensor & operator=(const GenTensor< T > &other)
shallow assignment operator
Definition: lowranktensor.h:164
GenTensor & operator=(const Tensor< T > &other)
deep assignment operator
Definition: lowranktensor.h:170
friend GenTensor copy(const GenTensor &other)
deep copy
Definition: lowranktensor.h:283
constexpr bool is_full_tensor() const
Definition: gentensor.h:224
GenTensor & operator=(const T &number)
assign a number to this tensor
Definition: lowranktensor.h:615
TENSOR_RESULT_TYPE(T, Q) trace_conj(const GenTensor< Q > &rhs) const
returns the trace of <this|rhs>
Definition: lowranktensor.h:508
TensorTypeData< T >::scalar_type scalar_type
C++ typename of the real type associated with a complex type.
Definition: lowranktensor.h:66
GenTensor get_tensor() const
Definition: gentensor.h:203
IsSupported< TensorTypeData< Q >, GenTensor< T > & >::type scale(Q fac)
Inplace multiplication by scalar of supported type (legacy name)
Definition: lowranktensor.h:426
long nCoeff() const
Definition: lowranktensor.h:486
SliceLowRankTensor< T > operator()(const std::vector< Slice > &s)
general slicing, shallow; for temporary use only!
Definition: lowranktensor.h:272
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:721
const Tensor< T > & get_tensor() const
Definition: lowranktensor.h:244
std::string what_am_i() const
Definition: lowranktensor.h:699
int index() const
Definition: lowranktensor.h:319
bool has_no_data() const
Definition: lowranktensor.h:480
GenTensor(const SVDTensor< T > &other)
ctor with a SVDTensor as argument, shallow
Definition: lowranktensor.h:154
Tensor< T > full_tensor_copy() const
Definition: gentensor.h:206
void normalize()
Definition: lowranktensor.h:402
GenTensor(const SliceLowRankTensor< T > &other)
ctor with a SliceLowRankTensor as argument, deep
Definition: lowranktensor.h:159
GenTensor< TENSOR_RESULT_TYPE(T, Q)> operator*(const Q &x) const
multiply with a number
Definition: lowranktensor.h:530
const SliceLowRankTensor< T > operator()(const std::vector< Slice > &s) const
general slicing, shallow; for temporary use only!
Definition: lowranktensor.h:277
const BaseTensor * ptr() const
might return a NULL pointer!
Definition: lowranktensor.h:709
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
TensorTrain< T > & get_tensortrain()
Definition: lowranktensor.h:253
constexpr bool is_tensortrain() const
Definition: gentensor.h:223
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
Tensor< T > & full_tensor()
Definition: lowranktensor.h:447
const Tensor< T > & full_tensor() const
Definition: lowranktensor.h:443
GenTensor & gaxpy(const T alpha, const GenTensor &other, const T beta)
Definition: lowranktensor.h:580
long size() const
Definition: lowranktensor.h:482
GenTensor operator-(const GenTensor &other)
Definition: lowranktensor.h:562
long real_size() const
Definition: lowranktensor.h:496
SVDTensor< T > & get_svdtensor()
Definition: gentensor.h:228
GenTensor()=default
empty ctor
void add_SVD(const GenTensor &other, const double &thresh)
Definition: lowranktensor.h:621
GenTensor()
Definition: gentensor.h:180
SVDTensor< T > & get_tensortrain()
Definition: gentensor.h:229
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
GenTensor & operator=(const TensorTrain< T > &other)
deep assignment operator
Definition: lowranktensor.h:182
GenTensor & operator-=(const GenTensor &other)
Definition: lowranktensor.h:568
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:648
TensorType tensor_type() const
return the tensor type
Definition: lowranktensor.h:290
static double fac_reduce()
Definition: lowranktensor.h:462
friend bool is_same_tensor_type(const GenTensor< R > &rhs, const GenTensor< Q > &lhs)
Definition: lowranktensor.h:1076
Tensor< T > reconstruct_tensor() const
reconstruct this to return a full tensor
Definition: lowranktensor.h:453
bool has_data() const
Definition: lowranktensor.h:478
GenTensor operator+(const GenTensor &other)
Definition: lowranktensor.h:536
float_scalar_type svd_normf() const
Definition: lowranktensor.h:412
GenTensor & operator=(const SVDTensor< T > &other)
deep assignment operator
Definition: lowranktensor.h:176
GenTensor(const TensorType &tt, const long k, const long ndim)
ctor with dimensions; all dims have the same value k
Definition: lowranktensor.h:94
const TensorTrain< T > & get_tensortrain() const
Definition: lowranktensor.h:262
bool is_assigned() const
Definition: gentensor.h:209
Tensor< T > & get_tensor()
Definition: lowranktensor.h:235
const long * dims() const
return the number of entries in dimension i
Definition: lowranktensor.h:397
GenTensor(const long ndim, const long *dims, const TensorType &tt)
Definition: lowranktensor.h:77
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:594
constexpr bool is_svd_tensor() const
Definition: gentensor.h:222
GenTensor & convert_inplace(const TensorArgs &targs)
Definition: lowranktensor.h:323
void normalize()
normalize the vectors (tested)
Definition: srconf.h:584
Definition: SVDTensor.h:42
implements a temporary(!) slice of a LowRankTensor
Definition: lowranktensor.h:949
SliceLowRankTensor< T > & operator=(const SliceLowRankTensor< T > &rhs)
assignment as in g(s) = g1(s);
Definition: lowranktensor.h:982
SliceLowRankTensor< T > & operator+=(const SliceLowRankTensor< T > &rhs)
inplace addition as in g(s)+=g1(s)
Definition: lowranktensor.h:1005
SliceLowRankTensor< T > & operator=(const T &number)
inplace zero-ing as in g(s)=0.0
Definition: lowranktensor.h:1038
SliceLowRankTensor< T > & operator-=(const GenTensor< T > &rhs)
inplace subtraction as in g(s)-=g1
Definition: lowranktensor.h:997
SliceLowRankTensor< T > & operator=(const GenTensor< T > &rhs)
assignment as in g(s) = g1;
Definition: lowranktensor.h:975
std::array< Slice, TENSOR_MAXDIM > thisslice
Definition: lowranktensor.h:953
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:1017
SliceLowRankTensor< T > & operator+=(const GenTensor< T > &rhs)
inplace addition as in g(s)+=g1
Definition: lowranktensor.h:989
friend GenTensor< T > copy(const SliceLowRankTensor< T > &other)
Definition: lowranktensor.h:1057
SliceLowRankTensor< T > & operator-=(const SliceLowRankTensor< T > &rhs)
inplace addition as in g(s)-=g1(s)
Definition: lowranktensor.h:1011
Definition: tensortrain.h:123
Traits class to specify support of numeric types.
Definition: type_data.h:56
A tensor is a multidimension array.
Definition: tensor.h:317
T type
C++ typename of this tensor.
Definition: tensor.h:406
Tensor< T > reshape(int ndimnew, const long *d)
Returns new view/tensor reshaping size/number of dimensions to conforming tensor.
Definition: tensor.h:1384
T trace(const Tensor< T > &t) const
Return the trace of two tensors (no complex conjugate invoked)
Definition: tensor.h:1776
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
const double m
Definition: gfit.cc:199
auto T(World &world, response_space &f) -> response_space
Definition: global_functions.cc:34
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
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:1099
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:1081
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
double norm(const T &t)
Definition: adquad.h:42
File holds all helper structures necessary for the CC_Operator and CC2 class.
Definition: DFParameters.h:10
GenTensor< TENSOR_RESULT_TYPE(R, Q)> general_transform(const GenTensor< R > &t, const Tensor< Q > c[])
Definition: gentensor.h:274
GenTensor< TENSOR_RESULT_TYPE(R, Q)> transform_dir(const GenTensor< R > &t, const Tensor< Q > &c, const int axis)
Definition: lowranktensor.h:1099
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:2002
static Tensor< double > weights[max_npt+1]
Definition: legendre.cc:99
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:2032
response_space transpose(response_space &f)
Definition: basic_operators.cc:10
static const Slice _(0,-1, 1)
void change_tensor_type(GenTensor< T > &t, const TensorArgs &targs)
change representation to targ.tt
Definition: gentensor.h:284
TENSOR_RESULT_TYPE(T, R) inner(const Function< T
Computes the scalar/inner product between two functions.
Definition: vmra.h:1059
void print(const T &t, const Ts &... ts)
Print items to std::cout (items separated by spaces) and terminate with a new line.
Definition: print.h:225
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 eps, bool are_optimal=false)
add all the GenTensors of a given list
Definition: gentensor.h:246
std::vector< CCPairFunction< T, NDIM > > operator*(const double fac, const std::vector< CCPairFunction< T, NDIM > > &arg)
Definition: ccpairfunction.h:1084
std::string type(const PairType &n)
Definition: PNOParameters.h:18
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:689
std::enable_if< std::is_base_of< ProjectorBase, projT >::value, OuterProjector< projT, projQ > >::type outer(const projT &p0, const projQ &p1)
Definition: projector.h:457
bool is_same_tensor_type(const GenTensor< R > &rhs, const GenTensor< Q > &lhs)
Definition: lowranktensor.h:1076
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 thresh
Definition: rk.cc:45
static const long k
Definition: rk.cc:44
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:757
Default store of an object via serialize(ar, t).
Definition: archive.h:611
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:621
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,...
const double alpha
Definition: test_coulomb.cc:54
std::size_t axis
Definition: testpdiff.cc:59
Defines and implements TensorTypeData, a type traits class.