1#ifndef XPED_TENSOR_HPP_
2#define XPED_TENSOR_HPP_
10#include "spdlog/spdlog.h"
12#include "yas/serialize.hpp"
13#include "yas/std_types.hpp"
39template <
typename, std::
size_t, std::
size_t,
typename,
bool = false,
typename = HeapPolicy>
42template <
typename Scalar_, std::
size_t Rank_, std::
size_t CoRank_,
typename Symmetry_,
typename AllocationPolicy_>
45 static constexpr std::size_t Rank = Rank_;
46 static constexpr std::size_t CoRank = CoRank_;
49 typedef typename Symmetry::qType
qType;
53template <
typename Scalar_, std::
size_t Rank, std::
size_t CoRank,
typename Symmetry_,
typename AllocationPolicy_>
54class Tensor<Scalar_, Rank, CoRank, Symmetry_, false, AllocationPolicy_>
55 :
public TensorBase<Tensor<Scalar_, Rank, CoRank, Symmetry_, false, AllocationPolicy_>>
57 template <
typename Derived>
60 friend class Tensor<Scalar_, Rank, CoRank, Symmetry_, true, AllocationPolicy_>;
67 using qType =
typename Symmetry::qType;
85 using DictType = std::unordered_map<
qType,
89 typename AllocationPolicy::template Allocator<std::pair<const qType, std::size_t>>>;
101 : storage_(basis_domain, basis_codomain, world)
115 : storage_(basis_domain, basis_codomain, data, size, world)
118 template <
typename OtherDerived>
124 static constexpr std::size_t
rank() {
return Rank; }
125 static constexpr std::size_t
corank() {
return CoRank; }
127 inline void set_data(
const Scalar* data, std::size_t size) { storage_.set_data(data, size); }
129 inline const auto&
sector()
const {
return storage_.sector(); }
130 inline const qType sector(std::size_t i)
const {
return storage_.sector(i); }
137 static_assert(Storage::IS_CONTIGUOUS());
138 return storage_.data();
142 static_assert(Storage::IS_CONTIGUOUS());
143 return storage_.data();
146 const std::size_t
plainSize()
const {
return storage_.plainSize(); }
148 const DictType&
dict()
const {
return storage_.dict(); }
155 const std::array<Qbasis<Symmetry, 1, AllocationPolicy>, Rank>&
uncoupledDomain()
const {
return storage_.uncoupledDomain(); }
156 const std::array<Qbasis<Symmetry, 1, AllocationPolicy>, CoRank>&
uncoupledCodomain()
const {
return storage_.uncoupledCodomain(); }
163 const std::string
name()
const {
return "Xped(" + std::to_string(rank()) +
"," + std::to_string(corank()) +
")"; }
185 const auto left_offset_domain = coupledDomain().leftOffset(f1);
186 const auto left_offset_codomain = coupledCodomain().leftOffset(f2);
187 const auto it = dict().find(f1.
q_coupled);
188 assert(it != dict().end());
197 assert(f1.
q_coupled == f2.
q_coupled and
"FusionTrees needs to have the same coupled qn.");
199 const auto left_offset_domain = coupledDomain().leftOffset(f1);
200 const auto left_offset_codomain = coupledCodomain().leftOffset(f2);
201 const auto it = dict().find(f1.
q_coupled);
202 assert(it != dict().end());
211 void print(std::ostream& o,
bool PRINT_MATRICES =
false)
const;
224 Self out(basis_domain, basis_codomain, world);
260 template <
int shift, std::size_t... p>
263 template <
int shift, std::size_t... p>
266 return permute<shift, p...>();
269 template <
int shift, std::size_t... p,
bool b>
272 return permute<shift, p...>();
275 template <
int shift, std::size_t... p,
bool TRACK>
281 template <std::
size_t leg>
285 template <
bool = false>
288 template <std::size_t... legs>
296 typename OtherScalar,
297 std::size_t OtherRank,
298 std::size_t OtherCoRank,
302 constexpr auto perms = util::constFct::get_permutations<a1, Rank, a2, OtherRank, ResRank>();
303 constexpr auto p1 = std::get<0>(perms);
304 constexpr auto shift1 = std::get<1>(perms);
305 SPDLOG_INFO(
"shift1={}, p1={}", shift1, p1);
306 constexpr auto p2 = std::get<2>(perms);
307 constexpr auto shift2 = std::get<3>(perms);
308 SPDLOG_INFO(
"shift2={}, p2={}", shift2, p2);
309 constexpr auto pres = std::get<4>(perms);
310 constexpr auto shiftres = std::get<5>(perms);
311 SPDLOG_INFO(
"shiftres={}, pres={}", shiftres, pres);
312 return operator*<TRACK>(this->
template permute<shift1>(util::constFct::as_sequence<p1>(),
Bool<TRACK>{}),
313 other.template permute<shift2>(util::constFct::as_sequence<p2>(),
Bool<TRACK>{}))
314 .
template permute<shiftres>(util::constFct::as_sequence<pres>(),
Bool<TRACK>{});
318 std::tuple<Tensor<Scalar, Rank, 1, Symmetry, false, AllocationPolicy>,
319 Tensor<RealScalar, 1, 1, Symmetry, false, AllocationPolicy>,
320 Tensor<Scalar, 1, CoRank, Symmetry, false, AllocationPolicy>>
326 bool PRESERVE_MULTIPLETS =
true,
327 bool RETURN_SPEC =
true) XPED_CONST;
332 tSVD(std::
size_t maxKeep,
RealScalar eps_svd,
RealScalar& truncWeight,
bool PRESERVE_MULTIPLETS = true) XPED_CONST
335 std::map<qarray<Symmetry::Nq>,
VectorType> SVspec_dumb;
336 return tSVD(maxKeep, eps_svd, truncWeight, S_dumb, SVspec_dumb, PRESERVE_MULTIPLETS,
false);
339 std::pair<Tensor<Scalar, Rank, 1, Symmetry, false, AllocationPolicy>,
Tensor<Scalar, 1, CoRank, Symmetry, false, AllocationPolicy>>
340 tQR(
bool RETURN_LQ =
false) XPED_CONST;
342 std::pair<
Tensor<
RealScalar, 1, 1,
Symmetry, false,
AllocationPolicy>,
Tensor<
RealScalar, Rank, 1,
Symmetry, false,
AllocationPolicy>>
345 const auto& domainTrees(const
qType& q)
const {
return coupledDomain().tree(q); }
350 auto begin() {
return storage_.begin(); }
351 auto end() {
return storage_.end(); }
353 const auto cbegin()
const {
return storage_.cbegin(); }
354 const auto cend()
const {
return storage_.cend(); }
356 template <
typename Ar>
359 ar& YAS_OBJECT_NVP(
"Tensor", (
"storage", storage_));
365 template <std::size_t... p_domain, std::size_t... p_codomain>
366 Self permute_impl(seq::iseq<std::size_t, p_domain...> pd, seq::iseq<std::size_t, p_codomain...> pc)
const;
368 template <
int shift, std::size_t... ps>
369 Tensor<Scalar, Rank - shift, CoRank + shift, Symmetry,
false, AllocationPolicy> permute_impl(seq::iseq<std::size_t, ps...> per)
const;
372template <
typename Scalar_, std::
size_t Rank, std::
size_t CoRank,
typename Symmetry,
typename AllocationPolicy>
373template <
typename OtherDerived>
374Tensor<Scalar_, Rank, CoRank, Symmetry, false, AllocationPolicy>::Tensor(
const TensorBase<OtherDerived>& other)
376 storage_ = Storage(other.derived().uncoupledDomain(), other.derived().uncoupledCodomain(), other.derived().world());
377 storage_.reserve(other.derived().sector().size());
378 for(std::size_t i = 0; i < other.derived().sector().size(); ++i) { storage_.push_back(other.derived().sector(i), other.derived().block(i)); }
381template <
bool TRACK = false,
typename Scalar,
typename OtherScalar, std::
size_t Rank, std::
size_t M
iddleRank, std::
size_t CoRank,
typename Symmetry>
382Tensor<std::common_type_t<Scalar, OtherScalar>, Rank, CoRank, Symmetry,
false>
386 return left.template
operator*<TRACK>(right);
389template <
bool TRACK = false,
typename Scalar,
typename OtherScalar, std::
size_t Rank, std::
size_t M
iddleRank, std::
size_t CoRank,
typename Symmetry>
390Tensor<std::common_type_t<Scalar, OtherScalar>, Rank, CoRank, Symmetry,
false>
393 return left.template
operator*<TRACK>(right);
397template <
typename Scalar, std::
size_t Rank, std::
size_t CoRank,
typename Symmetry,
bool ENABLE_AD = false>
398using ArenaTensor = Tensor<Scalar, Rank, CoRank, Symmetry, ENABLE_AD, StanArenaPolicy>;
403#ifndef XPED_COMPILED_LIB
Definition: Qbasis.hpp:39
Definition: TensorBase.hpp:36
const Qbasis< Symmetry, Rank, AllocationPolicy > & coupledDomain() const
Definition: Tensor.hpp:158
PlainInterface::MType< Scalar > MatrixType
Definition: Tensor.hpp:73
const auto cbegin() const
Definition: Tensor.hpp:353
TensorType subBlock(const FusionTree< Rank, Symmetry > &f1, const FusionTree< CoRank, Symmetry > &f2) const
std::tuple< Tensor< Scalar, Rank, 1, Symmetry, false, AllocationPolicy >, Tensor< RealScalar, 1, 1, Symmetry, false, AllocationPolicy >, Tensor< Scalar, 1, CoRank, Symmetry, false, AllocationPolicy > > tSVD(std::size_t maxKeep, RealScalar eps_svd, RealScalar &truncWeight, RealScalar &entropy, std::map< qarray< Symmetry::Nq >, VectorType > &SVspec, bool PRESERVE_MULTIPLETS=true, bool RETURN_SPEC=true) XPED_CONST
auto end()
Definition: Tensor.hpp:351
PlainInterface::MapTType< Scalar, Rank+CoRank > TensorMapType
Definition: Tensor.hpp:77
const Scalar * data() const
Definition: Tensor.hpp:135
PlainInterface::MapMType< Scalar > MatrixMapType
Definition: Tensor.hpp:74
void clear()
Definition: Tensor.hpp:218
Symmetry_ Symmetry
Definition: Tensor.hpp:66
auto view(const FusionTree< Rank, Symmetry > &f1, const FusionTree< CoRank, Symmetry > &f2) const
void setRandom(std::mt19937 &engine)
const auto & sector() const
Definition: Tensor.hpp:129
const std::size_t plainSize() const
Definition: Tensor.hpp:146
Tensor(const std::array< Qbasis< Symmetry, 1, AllocationPolicy >, Rank > &basis_domain, const std::array< Qbasis< Symmetry, 1, AllocationPolicy >, CoRank > &basis_codomain, const mpi::XpedWorld &world=mpi::getUniverse())
Definition: Tensor.hpp:98
Tensor< Scalar, Rank - shift, CoRank+shift, Symmetry, false, AllocationPolicy > permute(Bool< b >) const
Definition: Tensor.hpp:270
auto view(const FusionTree< Rank, Symmetry > &f1, const FusionTree< CoRank, Symmetry > &f2)
Self twist(std::size_t leg) const
constexpr bool AD_TENSOR()
Definition: Tensor.hpp:122
auto begin()
Definition: Tensor.hpp:350
auto subMatrix(const FusionTree< Rank, Symmetry > &f1, const FusionTree< CoRank, Symmetry > &f2)
Definition: Tensor.hpp:195
std::pair< Tensor< Scalar, Rank, 1, Symmetry, false, AllocationPolicy >, Tensor< Scalar, 1, CoRank, Symmetry, false, AllocationPolicy > > tQR(bool RETURN_LQ=false) XPED_CONST
PlainInterface::Indextype IndexType
Definition: Tensor.hpp:71
void print(std::ostream &o, bool PRINT_MATRICES=false) const
PlainInterface::cMapTType< Scalar, Rank+CoRank > TensorcMapType
Definition: Tensor.hpp:78
const auto subMatrix(const FusionTree< Rank, Symmetry > &f1, const FusionTree< CoRank, Symmetry > &f2) const
Definition: Tensor.hpp:181
const std::array< Qbasis< Symmetry, 1, AllocationPolicy >, Rank > & uncoupledDomain() const
Definition: Tensor.hpp:155
Tensor(const std::array< Qbasis< Symmetry, 1, AllocationPolicy >, Rank > &basis_domain, const std::array< Qbasis< Symmetry, 1, AllocationPolicy >, CoRank > &basis_codomain, const Scalar *data, std::size_t size, const mpi::XpedWorld &world)
Definition: Tensor.hpp:110
static Self Identity(const std::array< Qbasis< Symmetry, 1, AllocationPolicy >, Rank > &basis_domain, const std::array< Qbasis< Symmetry, 1, AllocationPolicy >, CoRank > &basis_codomain, const mpi::XpedWorld &world=mpi::getUniverse())
Definition: Tensor.hpp:220
PlainInterface::VType< Scalar > VectorType
Definition: Tensor.hpp:72
TensorType plainTensor() const
Storage::ConstMatrixReturnType operator()(const qType &q_coupled) const
Definition: Tensor.hpp:161
auto view(const FusionTree< Rank, Symmetry > &f1, const FusionTree< CoRank, Symmetry > &f2, std::size_t block_number) const
const auto & codomainTrees(const qType &q) const
Definition: Tensor.hpp:346
PlainInterface::TType< Scalar, Rank+CoRank > TensorType
Definition: Tensor.hpp:76
typename Symmetry::qType qType
Definition: Tensor.hpp:67
PlainInterface::cMapMType< Scalar > MatrixcMapType
Definition: Tensor.hpp:75
Tensor(const TensorBase< OtherDerived > &other)
Storage::MatrixReturnType block(std::size_t i)
Definition: Tensor.hpp:133
Tensor< Scalar, Rank, CoRank, Symmetry, false, AllocationPolicy > shiftQN(std::array< qType, sizeof...(legs)> charges) const
Storage & storage()
Definition: Tensor.hpp:151
Tensor< Scalar, util::constFct::trimDim< Rank >(leg), Rank+CoRank - 1 - util::constFct::trimDim< Rank >(leg), Symmetry, false, AllocationPolicy > trim() const
const mpi::XpedWorld & world() const
Definition: Tensor.hpp:153
void serialize(Ar &ar)
Definition: Tensor.hpp:357
const std::array< Qbasis< Symmetry, 1, AllocationPolicy >, CoRank > & uncoupledCodomain() const
Definition: Tensor.hpp:156
Tensor< Scalar, Rank - shift, CoRank+shift, Symmetry, false, AllocationPolicy > permute(seq::iseq< std::size_t, p... >) const
Definition: Tensor.hpp:264
typename ScalarTraits< Scalar >::Real RealScalar
Definition: Tensor.hpp:64
Storage::ConstMatrixReturnType block(std::size_t i) const
Definition: Tensor.hpp:132
void setConstant(Scalar val)
void set_data(const Scalar *data, std::size_t size)
Definition: Tensor.hpp:127
const qType sector(std::size_t i) const
Definition: Tensor.hpp:130
const Storage & storage() const
Definition: Tensor.hpp:150
const std::string name() const
Definition: Tensor.hpp:163
AllocationPolicy_ AllocationPolicy
Definition: Tensor.hpp:69
static constexpr std::size_t rank()
Definition: Tensor.hpp:124
static constexpr std::size_t corank()
Definition: Tensor.hpp:125
Scalar_ Scalar
Definition: Tensor.hpp:63
const DictType & dict() const
Definition: Tensor.hpp:148
Scalar * data()
Definition: Tensor.hpp:140
constexpr bool CONTIGUOUS_STORAGE()
Definition: Tensor.hpp:121
Tensor< Scalar, Rank - shift, CoRank+shift, Symmetry, false, AllocationPolicy > permute(seq::iseq< std::size_t, p... >, Bool< TRACK >) const
Definition: Tensor.hpp:276
const auto cend() const
Definition: Tensor.hpp:354
auto view(const FusionTree< Rank, Symmetry > &f1, const FusionTree< CoRank, Symmetry > &f2, std::size_t block_number)
TensorType subBlock(const FusionTree< Rank, Symmetry > &f1, const FusionTree< CoRank, Symmetry > &f2, std::size_t block_number) const
void push_back(const qType &q, const MatrixType &M)
Definition: Tensor.hpp:348
const Qbasis< Symmetry, CoRank, AllocationPolicy > & coupledCodomain() const
Definition: Tensor.hpp:159
Tensor< Scalar, Rank - shift, CoRank+shift, Symmetry, false, AllocationPolicy > permute() const
Definition: Tensor.hpp:40
XpedWorld & getUniverse()
Definition: Mpi.hpp:49
XTensor< TRACK, Scalar, Rank, CoRank, Symmetry > operator*(const Tensor< Scalar, Rank, CoRank, Symmetry, true > &t, Scalar s)
Definition: ADTensor.hpp:452
Definition: FusionTree.hpp:24
std::size_t dim
Definition: FusionTree.hpp:30
qType q_coupled
Definition: FusionTree.hpp:29
CTF::Matrix< Scalar > MapMType
Definition: MatrixInterface_Cyclops_impl.hpp:45
static MType< Scalar > block(const MType< Scalar > &M, const MIndextype &row_off, const MIndextype &col_off, const MIndextype &rows, const MIndextype &cols)
Definition: MatrixInterface_Cyclops_impl.cpp:287
const CTF::Matrix< Scalar > cMapMType
Definition: MatrixInterface_Cyclops_impl.hpp:47
CTF::Matrix< Scalar > MType
Definition: MatrixInterface_Cyclops_impl.hpp:40
int Indextype
Definition: PlainInterface_Cyclops_impl.hpp:11
Definition: ScalarTraits.hpp:10
Definition: StorageType_contiguous.hpp:14
MapMatrixType MatrixReturnType
Definition: StorageType_contiguous.hpp:20
cMapMatrixType ConstMatrixReturnType
Definition: StorageType_contiguous.hpp:21
nda::dense_array< Scalar, Rank > TType
Definition: TensorInterface_Array_impl.hpp:44
nda::dense_array_ref< Scalar, Rank > MapTType
Definition: TensorInterface_Array_impl.hpp:49
nda::const_dense_array_ref< Scalar, Rank > cMapTType
Definition: TensorInterface_Array_impl.hpp:51
Symmetry_ Symmetry
Definition: Tensor.hpp:48
Scalar_ Scalar
Definition: Tensor.hpp:47
AllocationPolicy_ AllocationPolicy
Definition: Tensor.hpp:50
Symmetry::qType qType
Definition: Tensor.hpp:49
Definition: TensorBase.hpp:10
CTF::Vector< Scalar > VType
Definition: VectorInterface_Cyclops_impl.hpp:12
Definition: qarray.hpp:30