Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
irregular_space.hpp
Go to the documentation of this file.
1
7#pragma once
8
11
12#include <algorithm>
13#include <concepts>
14#include <cstddef>
15#include <initializer_list>
16#include <numeric>
17#include <ranges>
18#include <span>
19#include <stdexcept>
20#include <string>
21#include <utility>
22#include <vector>
23
24namespace uni20
25{
26
31 public:
35 explicit IrregularSpace(Symmetry sym, std::string label = {}) : sym_(sym), label_(std::move(label))
36 {
37 this->verify_symmetry();
38 }
39
44 IrregularSpace(Symmetry sym, std::initializer_list<BlockSector> sectors, std::string label = {})
45 : IrregularSpace(sym, std::move(label))
46 {
47 this->append_sectors(sectors);
48 }
49
55 template <std::ranges::input_range Sectors>
56 requires std::constructible_from<BlockSector, std::ranges::range_reference_t<Sectors>>
57 IrregularSpace(Symmetry sym, Sectors&& sectors, std::string label = {}) : IrregularSpace(sym, std::move(label))
58 {
59 this->append_sectors(std::forward<Sectors>(sectors));
60 }
61
64 auto symmetry() const -> Symmetry { return sym_; }
65
68 auto size() const -> std::size_t { return sectors_.size(); }
69
72 auto empty() const -> bool { return sectors_.empty(); }
73
76 auto total_dim() const -> std::size_t
77 {
78 return std::accumulate(sectors_.begin(), sectors_.end(), std::size_t{0},
79 [](std::size_t total, BlockSector const& sector) { return total + sector.dim; });
80 }
81
85 auto contains(QNum q) const -> bool
86 {
87 this->verify_symmetry(q);
88 return std::ranges::find(sectors_, q, &BlockSector::q) != sectors_.end();
89 }
90
94 auto operator[](std::size_t index) const -> BlockSector const& { return sectors_[index]; }
95
98 auto sectors() const -> std::span<BlockSector const> { return sectors_; }
99
102 auto begin() const { return sectors_.begin(); }
103
106 auto end() const { return sectors_.end(); }
107
110 auto label() const -> std::string const& { return label_; }
111
114 void set_label(std::string label) { label_ = std::move(label); }
115
119 auto operator==(IrregularSpace const& other) const -> bool = default;
120
121 private:
123 void verify_symmetry() const
124 {
125 if (!sym_.valid())
126 {
127 throw std::invalid_argument("IrregularSpace requires a valid symmetry");
128 }
129 }
130
133 void verify_symmetry(QNum const& q) const
134 {
135 if (!q.valid() || q.symmetry() != sym_)
136 {
137 throw std::invalid_argument("IrregularSpace block has the wrong symmetry");
138 }
139 }
140
144 template <std::ranges::input_range Sectors> void append_sectors(Sectors&& sectors)
145 {
146 for (auto&& input : sectors)
147 {
148 BlockSector sector(std::forward<decltype(input)>(input));
149 this->verify_symmetry(sector.q);
150 if (sector.dim == 0)
151 {
152 throw std::invalid_argument("IrregularSpace blocks must have positive dimension");
153 }
154 sectors_.push_back(std::move(sector));
155 }
156 }
157
158 Symmetry sym_;
159 std::vector<BlockSector> sectors_;
160 std::string label_;
161};
162
163static_assert(SymmetrySpace<IrregularSpace>);
164
165} // namespace uni20
Defines one quantum-number sector and its degeneracy dimension.
Ordered block space whose quantum-number sectors may repeat.
Definition irregular_space.hpp:30
auto symmetry() const -> Symmetry
Return the symmetry shared by all blocks.
Definition irregular_space.hpp:64
auto label() const -> std::string const &
Return the semantic leg label.
Definition irregular_space.hpp:110
IrregularSpace(Symmetry sym, Sectors &&sectors, std::string label={})
Construct an irregular space from an input range.
Definition irregular_space.hpp:57
auto begin() const
Return an iterator to the first block.
Definition irregular_space.hpp:102
auto end() const
Return an iterator past the last block.
Definition irregular_space.hpp:106
auto total_dim() const -> std::size_t
Return the total dimension across all blocks.
Definition irregular_space.hpp:76
auto sectors() const -> std::span< BlockSector const >
Return the ordered block list.
Definition irregular_space.hpp:98
auto size() const -> std::size_t
Return the number of stored blocks.
Definition irregular_space.hpp:68
auto contains(QNum q) const -> bool
Return whether any block carries the requested quantum number.
Definition irregular_space.hpp:85
auto empty() const -> bool
Return whether the space contains no blocks.
Definition irregular_space.hpp:72
IrregularSpace(Symmetry sym, std::string label={})
Construct an empty irregular space tied to one symmetry.
Definition irregular_space.hpp:35
auto operator[](std::size_t index) const -> BlockSector const &
Return indexed block access.
Definition irregular_space.hpp:94
auto operator==(IrregularSpace const &other) const -> bool=default
Compare symmetry, ordered segmented blocks, and label.
void set_label(std::string label)
Replace the semantic leg label without changing block structure.
Definition irregular_space.hpp:114
IrregularSpace(Symmetry sym, std::initializer_list< BlockSector > sectors, std::string label={})
Construct an irregular space from an ordered block list.
Definition irregular_space.hpp:44
One irrep label together with the symmetry context needed to interpret it.
Definition qnum.hpp:29
auto symmetry() const -> Symmetry
Return the symmetry context of this quantum number.
Definition qnum.hpp:51
auto valid() const -> bool
Return whether the quantum number carries a valid symmetry context.
Definition qnum.hpp:47
Public handle to a canonicalized direct-product symmetry specification.
Definition symmetry.hpp:24
auto valid() const -> bool
Return whether this handle refers to a valid symmetry.
Definition symmetry.hpp:44
Definition qnum.hpp:403
Definition async/async.hpp:26
std::size_t span
Definition presentation.cpp:2017
Defines the common tensor-space concepts.
One symmetry sector inside a block-structured space.
Definition block_sector.hpp:18
QNum q
Quantum number carried by the block.
Definition block_sector.hpp:20