TRIQS/TRIQS 4.0.0
Researching Interacting Quantum Systems
Loading...
Searching...
No Matches
histograms.hpp
Go to the documentation of this file.
1// Copyright (c) 2016-2018 Commissariat à l'énergie atomique et aux énergies alternatives (CEA)
2// Copyright (c) 2016-2018 Centre national de la recherche scientifique (CNRS)
3// Copyright (c) 2018-2022 Simons Foundation
4// Copyright (c) 2016 Igor Krivenko
5//
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// You may obtain a copy of the License at
17// https://www.gnu.org/licenses/gpl-3.0.txt
18//
19// Authors: Philipp Dumitrescu, Igor Krivenko, Olivier Parcollet, Hugo U. R. Strand, Nils Wentzell
20
25
26#pragma once
27
28#include "../arrays.hpp"
30
31#include <h5/h5.hpp>
32#include <nda/mpi.hpp>
33#include <nda/nda.hpp>
34
35#include <cstddef>
36#include <cstdint>
37#include <iosfwd>
38#include <string>
39#include <utility>
40
41namespace triqs::stat {
42
47
69 class histogram {
70 public:
72 histogram() = default;
73
85 histogram(int a, int b);
86
99 histogram(double a, double b, std::size_t nbins);
100
112 histogram &operator<<(double x);
113
115 void clear() {
116 n_lost_pts_ = 0;
117 n_data_pts_ = 0;
118 data_() = 0.0;
119 }
120
127 auto mesh_point(int n) const { return a_ + n * binsize_; }
128
133 auto size() const { return data_.size(); }
134
139 C2PY_PROPERTY_GET(limits) auto limits() const { return std::pair{a_, b_}; }
140
145 C2PY_PROPERTY_GET(data) auto const &data() const { return data_; }
146
151 C2PY_PROPERTY_GET(n_data_pts) auto n_data_pts() const { return n_data_pts_; }
152
157 C2PY_PROPERTY_GET(n_lost_pts) auto n_lost_pts() const { return n_lost_pts_; }
158
171 friend histogram operator+(histogram h1, histogram const &h2);
172
177 bool operator==(histogram const &h) const = default;
178
186 C2PY_IGNORE friend void mpi_broadcast(histogram &h, mpi::communicator c = {}, int root = 0) {
187 mpi::broadcast(h.a_, c, root);
188 mpi::broadcast(h.b_, c, root);
189 mpi::broadcast(h.data_, c, root);
190 mpi::broadcast(h.n_data_pts_, c, root);
191 mpi::broadcast(h.n_lost_pts_, c, root);
192 if (c.rank() != root) h.initialize();
193 }
194
207 C2PY_IGNORE friend histogram mpi_reduce(histogram const &h, mpi::communicator c = {}, int root = 0, bool all = false, MPI_Op op = MPI_SUM) {
208 TRIQS_ASSERT(op == MPI_SUM);
209 histogram h2(h.a_, h.b_, h.size());
210 mpi::reduce_into(h.data_, h2.data_, c, root, all, MPI_SUM);
211 h2.n_data_pts_ = mpi::reduce(h.n_data_pts_, c, root, all, MPI_SUM);
212 h2.n_lost_pts_ = mpi::reduce(h.n_lost_pts_, c, root, all, MPI_SUM);
213 return h2;
214 }
215
217 [[nodiscard]] static std::string hdf5_format() { return "Histogram"; }
218
226 friend void h5_write(h5::group g, std::string const &name, histogram const &h);
227
235 friend void h5_read(h5::group g, std::string const &name, histogram &h);
236
244 friend std::ostream &operator<<(std::ostream &os, histogram const &h);
245
250 void serialize(auto &ar) const { ar & a_ & b_ & n_data_pts_ & n_lost_pts_ & data_ & binsize_; }
251
256 void deserialize(auto &ar) { ar & a_ & b_ & n_data_pts_ & n_lost_pts_ & data_ & binsize_; }
257
269 inline friend histogram pdf(histogram const &h) {
270 auto pdf = h;
271 pdf.data_ /= static_cast<double>(h.n_data_pts());
272 return pdf;
273 }
274
287 inline friend histogram cdf(histogram const &h) {
288 auto cdf = h;
289 for (int i = 1; i < h.size(); ++i) cdf.data_[i] += cdf.data_[i - 1];
290 cdf.data_ /= static_cast<double>(h.n_data_pts());
291 return cdf;
292 }
293
294 private:
295 // Initialize the histogram by checking the interval and setting the bin size.
296 void initialize();
297
298 private:
299 double a_{0.0};
300 double b_{0.0};
301 double binsize_{0.0};
302 std::uint64_t n_data_pts_{0};
303 std::uint64_t n_lost_pts_{0};
304 nda::vector<double> data_{};
305 };
306
308
309} // namespace triqs::stat
Backward-compatibility umbrella header pulling in the nda array library.
Class representing a histogram on a given interval.
friend void h5_write(h5::group g, std::string const &name, histogram const &h)
Write a triqs::stat::histogram to HDF5.
friend histogram cdf(histogram const &h)
Normalize and integrate a histogram.
friend histogram operator+(histogram h1, histogram const &h2)
Add two histograms together.
friend void mpi_broadcast(histogram &h, mpi::communicator c={}, int root=0)
Implementation of an MPI broadcast for triqs::stat::histogram.
friend void h5_read(h5::group g, std::string const &name, histogram &h)
Read a triqs::stat::histogram from HDF5.
bool operator==(histogram const &h) const =default
Default equal-to operator compares the domains, data vectors, number of accumulated and discarded dat...
auto n_data_pts() const
Get the number of data points that have been added to the histogram.
auto mesh_point(int n) const
Get the position of the center of the n-th bin.
static std::string hdf5_format()
Get the HDF5 format tag.
void serialize(auto &ar) const
Serialize the histogram to an archive.
friend histogram mpi_reduce(histogram const &h, mpi::communicator c={}, int root=0, bool all=false, MPI_Op op=MPI_SUM)
Implementation of an MPI reduce for triqs::stat::histogram.
histogram()=default
Default constructor leaves the histogram in a valid but unusable state.
auto const & data() const
Get the data stored in the histogram.
void deserialize(auto &ar)
Deserialize the histogram from an archive.
auto n_lost_pts() const
Get the number of data points that fell outside of the interval and were discarded.
void clear()
Reset the histogram to its initial state, i.e. with no data points added to it.
friend histogram pdf(histogram const &h)
Normalize a histogram.
auto limits() const
Get the domain on which the histogram is defined.
histogram & operator<<(double x)
Add a data point to the histogram.
auto size() const
Get number of bins in the histogram.
TRIQS exception hierarchy and related macros.
#define TRIQS_ASSERT(X)
Throw a triqs::runtime_error if the boolean expression X evaluates to false.