TRIQS/TRIQS 4.0.0
Researching Interacting Quantum Systems
Loading...
Searching...
No Matches
flatten.hpp
Go to the documentation of this file.
1// Copyright (c) 2018 Commissariat à l'énergie atomique et aux énergies alternatives (CEA)
2// Copyright (c) 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: Michel Ferrero, Olivier Parcollet, Nils Wentzell
19
24
25#pragma once
26
27#include "./gf.hpp"
29
30#include <utility>
31
32namespace triqs::gfs {
33
38
50 template <int N = 0, nda::MemoryArray A> auto flatten_2d(A const &v) {
51 long nrows = v.extent(N); // # rows of the result, i.e. n-th dim, which is now at 0.
52 long ncols = v.size() / nrows; // # columns of the result. Everything but n-th dim.
53 return reshape(typename A::regular_type{nda::transposed_view<0, N>(v)}, nrows, ncols);
54 }
55
56 // -------------------------------------------------------
57
65 template <int N = 0> void unflatten_2d(nda::MemoryArray auto &&out, nda::MemoryArray auto const &afl) {
66 auto sh_fl = out.shape();
67 std::swap(sh_fl[0], sh_fl[N]);
68 auto data_fl = reshape(afl, sh_fl);
69 out() = nda::transposed_view<0, N>(data_fl);
70 }
71
72 //-------------------------------------
73
85 template <int N = 0, MemoryGf G> auto flatten_gf_2d(G const &g) {
86 auto const &mesh = get_mesh<N>(g);
87 return gf{mesh, flatten_2d<N>(g.data())};
88 }
89
90 //-------------------------------------
91
100 template <int N = 0, MemoryGf Gfl> void unflatten_gf_2d(MemoryGf auto &g, Gfl const &gfl) {
101 static_assert(not mesh::is_product<typename Gfl::mesh_t>, "unflatten_gf_2d: Flattened Green-function must have non-product mesh");
102 EXPECTS(get_mesh<N>(g) == gfl.mesh());
103 unflatten_2d<N>(g.data(), gfl.data());
104 }
105
107
108} // namespace triqs::gfs
The owning Green's function container.
Definition gf.hpp:194
Concept checking that a type behaves like an in-memory Green's function.
Definition gf.hpp:133
Provides the Green's function class.
auto flatten_2d(A const &v)
Flatten an array into two dimensions, keeping one dimension and collapsing the rest.
Definition flatten.hpp:50
void unflatten_2d(nda::MemoryArray auto &&out, nda::MemoryArray auto const &afl)
Inverse of triqs::gfs::flatten_2d: scatter a two-dimensional array back into a higher-rank array.
Definition flatten.hpp:65
void unflatten_gf_2d(MemoryGf auto &g, Gfl const &gfl)
Inverse of triqs::gfs::flatten_gf_2d: scatter a flattened Green's function back into a higher-rank on...
Definition flatten.hpp:100
auto flatten_gf_2d(G const &g)
Flatten a Green's function into a single-mesh, tensor-valued Green's function.
Definition flatten.hpp:85
auto const & get_mesh(BG const &bg)
Get the mesh of a block Green's function, or its N-th component for a product mesh.
Definition block_gf.hpp:158
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
Common macros used in TRIQS.