29#include <fmt/format.h>
30#include <fmt/ranges.h>
32#include <itertools/itertools.hpp>
39namespace triqs::hilbert_space {
44 struct variant_visitor {
46 std::string
operator()(std::string
const &s)
const {
return "s" + s; }
48 std::string
operator()(std::array<long, 3>
const &a)
const {
54 std::variant<long, std::string, double, std::array<long, 3>> string_to_variant(std::string
const &s) {
55 if (s.substr(0, 3) ==
"idx") {
57 std::array<long, 3> arr{};
58 auto rest = s.substr(3);
59 size_t pos1 = rest.find(
',');
60 size_t pos2 = rest.find(
',', pos1 + 1);
61 arr[0] = std::stol(rest.substr(0, pos1));
62 arr[1] = std::stol(rest.substr(pos1 + 1, pos2 - pos1 - 1));
63 arr[2] = std::stol(rest.substr(pos2 + 1));
67 case 'i':
return std::stol(s.c_str() + 1);
68 case 's':
return std::string(s.c_str() + 1);
69 case 'd':
return std::stod(s.c_str() + 1);
76 std::vector<std::vector<std::string>> vvs(fops.size());
77 for (
auto const &[n, alpha] : itertools::enumerate(fops.data())) {
78 for (
auto const &beta : alpha) vvs[
n].push_back(visit(variant_visitor{}, beta));
87 if (!beta.empty()) alpha.push_back(string_to_variant(beta));
93 std::string
format_indices(
indices_t const &alpha, std::string_view sep, std::string_view prefix, std::string_view suffix) {
95 if (alpha.empty())
return fmt::format(
"{}{}", prefix, suffix);
98 auto idx_to_str = [](
auto const &x) {
100 [](
auto const &v) -> std::string {
101 if constexpr (std::is_same_v<std::decay_t<
decltype(v)>, std::string>)
102 return fmt::format(
"'{}'", v);
103 else if constexpr (std::is_same_v<std::decay_t<
decltype(v)>, std::array<long, 3>>)
104 return fmt::format(
"({})", fmt::join(v,
","));
106 return fmt::format(
"{}", v);
112 std::string str = fmt::format(
"{}{}", prefix, idx_to_str(alpha.front()));
113 for (
int i = 1; i < alpha.size(); ++i) str += fmt::format(
"{}{}", sep, idx_to_str(alpha[i]));
114 str += fmt::format(
"{}", suffix);
119 for (
auto const &vs : vvs) idxs_.push_back(to_indices(vs));
122 void h5_write_attribute(h5::object obj, std::string
const &name, fundamental_operator_set
const &fops) {
123 h5::write_attribute(obj, name, to_vec_vec_string(fops));
126 void h5_read_attribute(h5::object obj, std::string
const &name, fundamental_operator_set &fops) {
127 std::vector<std::vector<std::string>> vvs;
128 h5::read_attribute(obj, name, vvs);
129 fops = fundamental_operator_set(vvs);
const_view_type operator()() const
Make a const view of *this.
Class representing a fundamental operator set.
friend void h5_write_attribute(h5::object obj, std::string const &name, fundamental_operator_set const &fops)
Write a triqs::hilbert_space::fundamental_operator_set to HDF5 as an attribute.
friend void h5_read_attribute(h5::object obj, std::string const &name, fundamental_operator_set &fops)
Read a triqs::hilbert_space::fundamental_operator_set from an HDF5 attribute.
triqs::hilbert_space::indices_t indices_t
Index type to represent a single .
fundamental_operator_set()=default
Default constructor leaves the set of indices empty, i.e. .
TRIQS exception hierarchy and related macros.
std::vector< std::variant< long, std::string, double, std::array< long, 3 > > > indices_t
Index type for single particle state indices .
std::string format_indices(indices_t const &alpha, std::string_view sep, std::string_view prefix, std::string_view suffix)
String representation of a single particle state index .
many_body_operator_generic< T > n(IndexTypes... indices)
Create a number operator .
#define TRIQS_RUNTIME_ERROR
Throw a triqs::runtime_error with the current source location.
string to_string(string const &str)
Identity overload for std::string.
Provides a fundamental operator set class.