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) 2021 Simons Foundation
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You may obtain a copy of the License at
14// https://www.gnu.org/licenses/gpl-3.0.txt
15//
16// Authors: Philipp Dumitrescu, Nils Wentzell
17
22
23#pragma once
24
25#include "./concepts.hpp"
26
27#include <nda/nda.hpp>
28
29#include <limits>
30#include <type_traits>
31#include <utility>
32
33namespace triqs::stat {
34
39
51 template <typename T> [[nodiscard]] auto make_real(T &&t) { return nda::make_regular(nda::real(std::forward<T>(t))); }
52
54 template <typename T> using get_real_t = std::remove_cvref_t<decltype(make_real(std::declval<T>()))>;
55
57 template <typename T> using get_regular_t = std::remove_cvref_t<decltype(nda::make_regular(std::declval<T>()))>;
58
66 template <StatCompatible T> [[nodiscard]] auto zeroed_sample([[maybe_unused]] T const &sample) {
67 if constexpr (nda::Scalar<T>) {
68 return T{0};
69 } else {
70 return get_regular_t<T>::zeros(sample.shape());
71 }
72 }
73
86 template <StatCompatible T> [[nodiscard]] auto nan_sample([[maybe_unused]] T const &sample) {
87 if constexpr (nda::Scalar<T>) {
88 using real_t = std::remove_cvref_t<decltype(std::real(std::declval<T>()))>;
89 return T{std::numeric_limits<real_t>::quiet_NaN()};
90 } else {
91 using elem_t = typename get_regular_t<T>::value_type;
92 using real_elem_t = std::remove_cvref_t<decltype(std::real(std::declval<elem_t>()))>;
93 auto res = get_regular_t<T>(sample.shape());
94 res() = elem_t{std::numeric_limits<real_elem_t>::quiet_NaN()};
95 return res;
96 }
97 }
98
107 [[nodiscard]] auto abs_square(auto const &x) { return make_real(nda::hadamard(nda::conj(x), x)); }
108
110
111} // namespace triqs::stat
std::remove_cvref_t< decltype(nda::make_regular(std::declval< T >()))> get_regular_t
Type trait to get the type that would be returned by nda::make_regular.
Definition utils.hpp:57
std::remove_cvref_t< decltype(make_real(std::declval< T >()))> get_real_t
Type trait to get the type that would be returned by triqs::stat::make_real.
Definition utils.hpp:54
auto nan_sample(T const &sample)
Get a sample with all elements set to NaN.
Definition utils.hpp:86
auto zeroed_sample(T const &sample)
Get a sample with all elements set to zero.
Definition utils.hpp:66
auto abs_square(auto const &x)
Calculate the (elementwise) absolute square of an array/view/scalar.
Definition utils.hpp:107
auto make_real(T &&t)
Make a given object real and regular.
Definition utils.hpp:51
Provides various concepts for the Utilities.