Xped
Loading...
Searching...
No Matches
Matlab.hpp
Go to the documentation of this file.
1#ifndef XPED_MATLAB_HPP_
2#define XPED_MATLAB_HPP_
3
4#include <highfive/H5File.hpp>
5
7
8namespace Xped::IO {
9template <typename Scalar, std::size_t Rank, std::size_t CoRank, typename Symmetry, typename AllocationPolicy>
11loadMatlabTensor(const HighFive::Group& t,
12 const HighFive::Group& base,
13 std::array<bool, Rank + CoRank> conj = std::array<bool, Rank + CoRank>{},
14 int qn_scale = 1)
15{
16 constexpr std::size_t full_rank = Rank + CoRank;
17 auto get_indices = [](auto combined_index, std::array<std::size_t, full_rank> dims) -> auto
18 {
19 std::array<std::size_t, dims.size()> out{};
20 std::size_t divisor = combined_index;
21 std::size_t count = 0;
22 while(divisor > 0) {
23 std::size_t tmp = divisor / dims[count];
24 std::size_t remainder = divisor % dims[count];
25 assert(count < dims.size());
26 out[count] = remainder;
27 count++;
28 divisor = tmp;
29 }
30 return out;
31 };
32
33 std::array<Qbasis<Symmetry, 1>, full_rank> basis;
34 std::array<std::size_t, full_rank> dims;
35
36 auto charges_ref_dat = t.getDataSet("charges");
37 std::vector<HighFive::Reference> charges_ref;
38 charges_ref_dat.read(charges_ref);
39 std::array<std::vector<std::vector<double>>, full_rank> charges;
40 for(std::size_t r = 0; r < full_rank; ++r) {
41 auto ch_dat = charges_ref[r].template dereference<HighFive::DataSet>(base);
42 std::vector<std::vector<double>> ch;
43 ch_dat.read(ch);
44 charges[r] = ch;
45 for(std::size_t j = 0; j < ch[0].size(); ++j) {
46 basis[r].push_back({(qn_scale * static_cast<int>(std::round(ch[0][j]))) % Symmetry::MOD_N[0]}, ch[1][j]);
47 }
48 if(conj[r]) {
49 basis[r].SET_CONJ();
50 for(auto& [q, trees] : basis[r].allTrees()) {
51 for(auto& tree : trees) { tree.IS_DUAL[0] = true; }
52 }
53 }
54 basis[r].sort();
55 dims[r] = ch[0].size();
56 }
57
58 auto raw_dat = t.getDataSet("vdat");
59 std::vector<double> raw;
60 raw_dat.read(raw);
61
62 auto off_dat = t.getDataSet("voffs");
63 std::vector<std::vector<Scalar>> off;
64 off_dat.read(off);
65
66 Tensor<Scalar, full_rank, 0, Symmetry, false, AllocationPolicy> tmp(basis, {{}});
67 tmp.setZero();
68 for(std::size_t i = 0; i < off.size(); ++i) {
69 auto indices = get_indices(off[i][0], dims);
70 FusionTree<full_rank, Symmetry> tree;
71 tree.q_coupled = Symmetry::qvacuum();
72 for(std::size_t r = 0; r < full_rank; ++r) {
73 tree.q_uncoupled[r] = {(qn_scale * static_cast<int>(std::round(charges[r][0][indices[r]]))) % Symmetry::MOD_N[0]};
74 tree.dims[r] = charges[r][1][indices[r]];
75 if(conj[r]) { tree.IS_DUAL[r] = true; }
76 }
77 tree.computeDim();
78 tree.computeIntermediates();
79 // if constexpr(full_rank > 2) {
80 // tree.q_intermediates[0] = Symmetry::reduceSilent(tree.q_uncoupled[0], tree.q_uncoupled[1])[0];
81 // for(std::size_t intermediate = 1; intermediate < full_rank - 2; ++intermediate) {
82 // tree.q_intermediates[intermediate] =
83 // Symmetry::reduceSilent(tree.q_intermediates[intermediate - 1], tree.q_uncoupled[intermediate + 1])[0];
84 // }
85 // }
86 FusionTree<0, Symmetry> trivial;
87 trivial.dim = 1;
88 trivial.q_coupled = Symmetry::qvacuum();
89
90 auto submatrix = tmp.subMatrix(tree, trivial);
91 std::size_t begin = off[i][1];
92 std::size_t end = (i == off.size() - 1) ? raw.size() : off[i + 1][1];
93 for(auto i = begin; i < end; ++i) { PlainInterface::setVal(submatrix, i - begin, 0, raw[i]); }
94 }
95 return tmp.template permute<full_rank - Rank>(seq::make<std::size_t, full_rank>{});
96}
97
98} // namespace Xped::IO
99#endif
Definition: Tensor.hpp:40
Definition: Matlab.hpp:8
Tensor< Scalar, Rank, CoRank, Symmetry, false, AllocationPolicy > loadMatlabTensor(const HighFive::Group &t, const HighFive::Group &base, std::array< bool, Rank+CoRank > conj=std::array< bool, Rank+CoRank >{}, int qn_scale=1)
Definition: Matlab.hpp:11
std::unordered_map< std::pair< FusionTree< Rank - shift, Symmetry >, FusionTree< CoRank+shift, Symmetry > >, typename Symmetry::Scalar > permute(const FusionTree< Rank, Symmetry > &t1, const FusionTree< CoRank, Symmetry > &t2, const util::Permutation &p)
Definition: treepair.cpp:141
static void setVal(MType< Scalar > &M, const MIndextype row, const MIndextype col, const Scalar &val)