34#ifndef MADNESS_TENSOR_LOWRANKTENSOR_H_
35#define MADNESS_TENSOR_LOWRANKTENSOR_H_
55template <
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;
729template <
class Archive,
typename T>
735 int index1=t.
index();
740 }
else if (index1==1) {
743 }
else if (index1==2) {
752template <
class Archive,
typename T>
753struct ArchiveLoadImpl< Archive,
GenTensor<
T> > {
764 }
else if (index==1) {
768 }
else if (index==2) {
772 }
else if (index==-1) {
786template <
class Q,
class T>
790 if (std::is_same<Q, T>::value)
return copy(other);
817template <
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);
870template <
class T,
class Q>
894template <
typename T,
typename Q>
895typename IsSupported < TensorTypeData<Q>, GenTensor<T> >
::type
909GenTensor<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;
1075template<
typename Q,
typename R>
1080template <
typename R,
typename Q>
1089template <
typename R,
typename Q>
1098template <
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(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
Definition gentensor.h:198
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
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
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:550
friend GenTensor copy(const GenTensor &other)
deep copy
Definition lowranktensor.h:283
void add_SVD(const GenTensor< T > &rhs, const double &eps)
Definition gentensor.h:235
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:508
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 get_tensor() const
Definition gentensor.h:203
GenTensor< TENSOR_RESULT_TYPE(T, Q)> operator*(const Q &x) const
multiply with a number
Definition lowranktensor.h:530
GenTensor & operator=(const SliceLowRankTensor< T > &other)
deep assignment with slices: g0 = g1(s)
Definition lowranktensor.h:188
const Tensor< T > & get_tensor() const
Definition lowranktensor.h:244
long nCoeff() const
Definition lowranktensor.h:486
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:721
Tensor< T > & full_tensor()
Definition lowranktensor.h:447
std::string what_am_i() const
Definition lowranktensor.h:699
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:480
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:594
const SVDTensor< T > & get_svdtensor() const
Definition lowranktensor.h:226
GenTensor & operator-=(const SliceLowRankTensor< T > &other)
Definition lowranktensor.h:573
GenTensor & operator=(const T &number)
assign a number to this tensor
Definition lowranktensor.h:615
GenTensor< T > & emul(const GenTensor< T > &other)
Inplace multiply by corresponding elements of argument Tensor.
Definition lowranktensor.h:631
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
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 lowranktensor.h:443
long size() const
Definition lowranktensor.h:482
GenTensor operator-(const GenTensor &other)
Definition lowranktensor.h:562
long real_size() const
Definition lowranktensor.h:496
GenTensor()=default
empty ctor
void add_SVD(const GenTensor &other, const double &thresh)
Definition lowranktensor.h:621
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:648
TensorType tensor_type() const
return the tensor type
Definition lowranktensor.h:290
const BaseTensor * ptr() const
might return a NULL pointer!
Definition lowranktensor.h:709
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
bool has_data() const
Definition lowranktensor.h:478
GenTensor & operator-=(const GenTensor &other)
Definition lowranktensor.h:568
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:453
GenTensor operator+(const GenTensor &other)
Definition lowranktensor.h:536
float_scalar_type svd_normf() const
Definition lowranktensor.h:412
GenTensor & gaxpy(const T alpha, const GenTensor &other, const T beta)
Definition lowranktensor.h:580
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:426
GenTensor(const long ndim, const long *dims, const TensorType &tt)
Definition lowranktensor.h:77
GenTensor & operator+=(const SliceLowRankTensor< T > &other)
Definition lowranktensor.h:555
SVDTensor< T > & get_tensortrain()
Definition gentensor.h:229
constexpr bool is_svd_tensor() const
Definition gentensor.h:222
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)
inplace addition as in g(s)+=g1(s)
Definition lowranktensor.h:1005
SliceLowRankTensor< T > & operator+=(const GenTensor< T > &rhs)
inplace addition as in g(s)+=g1
Definition lowranktensor.h:989
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 SliceLowRankTensor< T > &rhs)
inplace addition as in g(s)-=g1(s)
Definition lowranktensor.h:1011
SliceLowRankTensor< T > & operator=(const T &number)
inplace zero-ing as in g(s)=0.0
Definition lowranktensor.h:1038
SliceLowRankTensor< T > & operator=(const SliceLowRankTensor< T > &rhs)
assignment as in g(s) = g1(s);
Definition lowranktensor.h:982
SliceLowRankTensor< T > & operator-=(const GenTensor< T > &rhs)
inplace subtraction as in g(s)-=g1
Definition lowranktensor.h:997
friend GenTensor< T > copy(const SliceLowRankTensor< T > &other)
Definition lowranktensor.h:1057
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
Tensor< T > & fill(T x)
Inplace fill with a scalar (legacy name)
Definition tensor.h:562
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 type
C++ typename of this tensor.
Definition tensor.h:406
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
auto T(World &world, response_space &f) -> response_space
Definition global_functions.cc:34
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
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
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 all elements and tools of MADNESS.
Definition DFParameters.h:10
GenTensor< TENSOR_RESULT_TYPE(R, Q)> general_transform(const GenTensor< R > &t, const Tensor< Q > c[])
Definition gentensor.h:274
std::vector< Function< T, NDIM > > reduce_rank(std::vector< Function< T, NDIM > > v, double thresh=0.0, bool fence=true)
reduces the tensor rank of the coefficient tensor (if applicable)
Definition vmra.h:366
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
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
static Tensor< double > weights[max_npt+1]
Definition legendre.cc:99
response_space transpose(response_space &f)
Definition basic_operators.cc:10
std::vector< CCPairFunction< T, NDIM > > operator*(const double fac, const std::vector< CCPairFunction< T, NDIM > > &arg)
Definition ccpairfunction.h:1084
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
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
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
GenTensor< TENSOR_RESULT_TYPE(R, Q)> transform_dir(const GenTensor< R > &t, const Tensor< Q > &c, const int axis)
Definition lowranktensor.h:1099
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:2002
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 m
Definition relops.cc:9
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,...
double norm(const T i1)
Definition test_cloud.cc:72
static const double alpha
Definition testcosine.cc:10
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