TRIQS/TRIQS 4.0.0
Researching Interacting Quantum Systems
Loading...
Searching...
No Matches
utils.hpp
Go to the documentation of this file.
1// Copyright (c) 2013-2018 Commissariat à l'énergie atomique et aux énergies alternatives (CEA)
2// Copyright (c) 2013-2018 Centre national de la recherche scientifique (CNRS)
3// Copyright (c) 2018-2023 Simons Foundation
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You may obtain a copy of the License at
16// https://www.gnu.org/licenses/gpl-3.0.txt
17//
18// Authors: Philipp Dumitrescu, Olivier Parcollet, Nils Wentzell
19
24
25#pragma once
26
27#include "./concepts.hpp"
28#include "../utility/macros.hpp"
29
30#include <nda/nda.hpp>
31
32#include <cstddef>
33#include <cstdint>
34#include <functional>
35#include <ranges>
36#include <span>
37#include <string_view>
38#include <tuple>
39#include <type_traits>
40#include <utility>
41
42namespace triqs::mesh {
43
44 // Aliases and type definitions.
45 using dcomplex = std::complex<double>;
46 using all_t = nda::range::all_t;
47 using nda::array;
48 using nda::array_view;
49 using nda::ellipsis;
50 using nda::eye;
51 using nda::matrix;
52 using nda::matrix_const_view;
53 using nda::matrix_view;
54 using nda::range;
55
60
70 template <typename... Ts> [[nodiscard]] uint64_t hash(Ts &&...ts) { return (std::hash<std::decay_t<Ts>>()(std::forward<Ts>(ts)) + ...); }
71
80 [[nodiscard]] C2PY_IGNORE inline std::size_t hash_bytes(std::span<std::byte const> bytes) {
81 return std::hash<std::string_view>{}(std::string_view{reinterpret_cast<char const *>(bytes.data()), bytes.size()});
82 }
83
93 template <nda::MemoryArray R> [[nodiscard]] std::size_t hash_bytes(R const &r) {
94 EXPECTS(r.is_contiguous());
95 return hash_bytes(std::as_bytes(std::span{r.data(), static_cast<std::size_t>(r.size())}));
96 }
97
111 [[nodiscard]] C2PY_IGNORE inline long positive_modulo(long x, long y) {
112 EXPECTS(y >= 0);
113 long res = x % y;
114 return (res >= 0 ? res : res + y);
115 }
116
124 template <MeshWithValues M> [[nodiscard]] auto values(M const &m) {
125 auto res = nda::vector<typename M::value_t>(m.size());
126 for (auto i : nda::range(m.size())) res(i) = m[i].value();
127 return res;
128 }
129
137 template <Mesh M> [[nodiscard]] M copy(M const &m) { return m; }
138
148 template <Mesh M> void copy_from(M &m1, M const &m2) { m1 = m2; }
149
151 template <Mesh M> static constexpr bool is_product = false;
152
154 template <Mesh M> static constexpr int n_variables = 1;
155
163 enum statistic_enum { Boson = 0, Fermion = 1 };
164
171 [[nodiscard]] C2PY_IGNORE inline int sign(statistic_enum s) { return (s == Boson ? 1 : -1); }
172
180 [[nodiscard]] C2PY_IGNORE inline auto operator*(statistic_enum s1, statistic_enum s2) { return (s1 == s2 ? Boson : Fermion); }
181
186 template <typename T> struct closest_mesh_point_t {
189 };
190
199 template <typename... Ts> [[nodiscard]] auto closest_mesh_pt(Ts &&...ts) {
200 if constexpr (sizeof...(Ts) == 1)
201 return closest_mesh_point_t<std::decay_t<Ts>...>{std::forward<Ts>(ts)...};
202 else
203 return std::tuple{closest_mesh_point_t<std::decay_t<Ts>>{std::forward<Ts>(ts)}...};
204 }
205
207
208 namespace detail {
209
210 // Apply a function to each element of a range and sum the results into a regular type.
211 [[nodiscard]] auto sum_to_regular(std::ranges::forward_range auto &&rg, auto f) {
212 auto it = std::ranges::begin(rg);
213 auto e = std::ranges::end(rg);
214 auto res = nda::make_regular(f(*it));
215 for (++it; it != e; ++it) res += f(*it);
216 return res;
217 }
218
219 } // namespace detail
220
221} // namespace triqs::mesh
k_expr<' *', long, R > operator*(Int l, R &&r)
Lazy multiplication of a triqs::mesh::BzMeshPoint object with a scalar.
Definition brzone.hpp:670
int sign(statistic_enum s)
Get the sign associated with the given particle statistics.
Definition utils.hpp:171
auto values(M const &m)
Get the values of all mesh points in a mesh.
Definition utils.hpp:124
auto closest_mesh_pt(Ts &&...ts)
Construct a triqs::mesh::closest_mesh_point_t object for a single value or a std::tuple of triqs::mes...
Definition utils.hpp:199
void copy_from(M &m1, M const &m2)
Copy one mesh into another (for Python bindings).
Definition utils.hpp:148
statistic_enum
Enum to specify particle statistics.
Definition utils.hpp:163
static constexpr int n_variables
Constexpr variable that holds the number of meshes in a triqs::mesh::Mesh type ( for non-product mes...
Definition utils.hpp:154
std::size_t hash_bytes(std::span< std::byte const > bytes)
Hash the raw bytes of a span via the standard library's std::hash<std::string_view>.
Definition utils.hpp:80
uint64_t hash(Ts &&...ts)
Generic hash function for multiple arguments.
Definition utils.hpp:70
static constexpr bool is_product
Constexpr bool that is true if the given triqs::mesh::Mesh type is a product of meshes,...
Definition utils.hpp:151
long positive_modulo(long x, long y)
Calculate the positive modulo of two integer numbers.
Definition utils.hpp:111
M copy(M const &m)
Get a copy of a mesh (for Python bindings).
Definition utils.hpp:137
Common macros used in TRIQS.
Provides concepts for mesh points and meshes.
Lazy struct used in various function overloads as a placeholder for the closest mesh point to a given...
Definition utils.hpp:186
T value
Use the mesh point closest to this value.
Definition utils.hpp:188