TRIQS/triqs_ctseg 4.0.0
A TRIQS application
Loading...
Searching...
No Matches
util.hpp
1// Copyright (c) 2022--present, The Simons Foundation
2// Copyright (c) 2022--present, Max Planck Institute for Polymer Research, Mainz, Germany
3// This file is part of TRIQS/ctseg and is licensed under the terms of GPLv3 or later.
4// SPDX-License-Identifier: GPL-3.0-or-later
5// See LICENSE in the root of this distribution for details.
6
7#pragma once
8
9#include <triqs/gfs.hpp>
10using namespace triqs::gfs;
11
12namespace triqs_ctseg {
13
14 inline long modulo(long i, long N) { return (i + N) % N; }
15
16 // Same as std::lower_bound, but i-th element of vector is returned by f[i]
17 // f is called on 0:N strictly
18 long lower_bound(auto f, long N, auto const &value) {
19 long first = 0, count = N;
20
21 while (count > 0) {
22 long step = count / 2;
23 assert(first + step < N);
24 if (f(first + step) < value) {
25 first += step + 1;
26 count -= step + 1;
27 } else
28 count = step;
29 }
30 return first;
31 }
32
33 // If we have an ordered det_manip d, we get the lower_bound index for x
34 long det_lower_bound_x(auto const &d, auto const &x) {
35 return lower_bound([&d](long i) { return d.get_x(i).first; }, d.size(), x);
36 }
37 long det_lower_bound_y(auto const &d, auto const &y) {
38 return lower_bound([&d](long i) { return d.get_y(i).first; }, d.size(), y);
39 }
40
41 // Integer power
42 constexpr unsigned int ipow(unsigned int n, unsigned int m) { return m == 0 ? 1 : m == 1 ? n : n * ipow(n, m - 1); }
43
44 // Block2Gf constructor
45 template <typename M> block2_gf<M> make_block2_gf(M const &m, gf_struct_t const &gf_struct) {
46
47 std::vector<std::vector<gf<M>>> gf_vecvec;
48 std::vector<std::string> block_names;
49
50 for (auto const &[bl1, bl1_size] : gf_struct) {
51 block_names.push_back(bl1);
52 std::vector<gf<M>> gf_vec;
53 for (auto const &[bl2, bl2_size] : gf_struct) { gf_vec.emplace_back(m, make_shape(bl1_size, bl2_size)); }
54 gf_vecvec.emplace_back(std::move(gf_vec));
55 }
56
57 return make_block2_gf(block_names, block_names, std::move(gf_vecvec));
58 }
59
60} // namespace triqs_ctseg