Hyperiso 1.0.3
Modular flavour-physics calculations, Wilson coefficients and statistical inference
Loading...
Searching...
No Matches
Indexing.h
Go to the documentation of this file.
1#ifndef INDEXING_H
2#define INDEXING_H
3
4#include <map>
5#include <vector>
6#include <stdexcept>
7
8#include "Matrix.h"
9#include "Include.h"
10#include "ObservableValue.h"
11#include "BinnedObservableId.h"
12
29template <typename T, typename U>
31 std::vector<T> ids;
32 std::vector<U> vals;
33};
34
41template <typename T>
43 std::vector<T> ids;
44 std::vector<std::vector<double>> vals;
45};
46
60template<typename T, typename U>
61std::map<T, U> zip(const std::vector<T>& ids, const std::vector<U>& vals) {
62 if (ids.size() != vals.size())
63 throw std::invalid_argument("Index and value vector sizes don't match.");
64
65 std::map<T, U> indexed;
66 for (size_t i = 0; i < ids.size(); i++) {
67 indexed.emplace(ids.at(i), vals.at(i));
68 }
69
70 return indexed;
71}
72
87template<typename T>
88std::map<T, std::map<T, double>> zip(const std::vector<T>& ids, const std::vector<std::vector<double>>& vals) {
89 if (vals.empty())
90 throw std::invalid_argument("No values to zip.");
91
92 if (ids.size() != vals.size() || ids.size() != vals.at(0).size())
93 throw std::invalid_argument("Index and value sizes don't match or value matrix is not square.");
94
95 std::map<T, std::map<T, double>> indexed;
96 for (size_t i = 0; i < ids.size(); i++) {
97 std::map<T, double> row;
98 for (size_t j = 0; j < ids.size(); j++) {
99 row.emplace(ids[j], vals[i][j]);
100 }
101 indexed.emplace(ids[i], std::move(row));
102 }
103
104 return indexed;
105}
106
118inline UnzipResult1D<BinnedObservableId, double> flatten(std::map<ObservableId, std::vector<ObservableValue>> indexed) {
119 std::vector<BinnedObservableId> ids;
120 std::vector<double> vals;
121
122 for (auto& [id, obs_values]: indexed) {
123 for (auto& obs_val : obs_values) {
124 BinnedObservableId binned_id;
125 binned_id.s = id;
126 binned_id.p = obs_val.bin.value_or(std::pair<double, double> {0., 0.});
127 ids.emplace_back(binned_id);
128 vals.emplace_back(obs_val.value);
129 }
130 }
131
132 return UnzipResult1D {ids, vals};
133}
134
148template<typename T>
149std::map<T, std::map<T, double>> zip(const std::vector<T>& ids, const RealMatrix& vals) {
150 if (ids.size() != vals.rows() || ids.size() != vals.cols())
151 throw std::invalid_argument("Index and value sizes don't match or value matrix is not square.");
152
153 std::map<T, std::map<T, double>> indexed;
154 for (size_t i = 0; i < ids.size(); i++) {
155 std::map<T, double> row;
156 for (size_t j = 0; j < ids.size(); j++) {
157 row.emplace(ids[j], vals.unchecked_at(i, j));
158 }
159 indexed.emplace(ids[i], std::move(row));
160 }
161
162 return indexed;
163}
164
175template<typename T, typename U>
176UnzipResult1D<T, U> unzip(const std::map<T, U>& indexed) {
177 std::vector<T> ids;
178 std::vector<U> vals;
179
180 for (auto& [id, v]: indexed) {
181 ids.emplace_back(id);
182 vals.emplace_back(v);
183 }
184
185 return UnzipResult1D {ids, vals};
186}
187
197template<typename T>
198UnzipResult2D<T> unzip(const std::map<T, std::map<T, double>>& indexed) {
199 std::vector<T> ids;
200 std::vector<std::vector<double>> vals;
201
202 for (auto& [id, row]: indexed) {
203 ids.emplace_back(id);
204 std::vector<double> row_vals;
205 for (auto& [_, v] : row) {
206 row_vals.emplace_back(v);
207 }
208 vals.emplace_back(std::move(row_vals));
209 }
210
211 return UnzipResult2D {ids, vals};
212}
213
214#endif // INDEXING_H
Utilities for identifying observables together with numerical bins.
UnzipResult1D< BinnedObservableId, double > flatten(std::map< ObservableId, std::vector< ObservableValue > > indexed)
Flattens observable values into binned observable ids and values.
Definition Indexing.h:118
std::map< T, U > zip(const std::vector< T > &ids, const std::vector< U > &vals)
Builds a map from parallel id and value vectors.
Definition Indexing.h:61
UnzipResult1D< T, U > unzip(const std::map< T, U > &indexed)
Splits a map into parallel id and value vectors.
Definition Indexing.h:176
Lightweight dense real-matrix utilities built on top of STL storage and GSL backends.
double T(double x)
Wilson coefficient T(x).
Identifies an observable together with a numerical bin.
std::pair< double, double > p
Result of converting a one-dimensional indexed map to dense vectors.
Definition Indexing.h:30
std::vector< U > vals
Values associated with ids.
Definition Indexing.h:32
std::vector< T > ids
Ids in map iteration order.
Definition Indexing.h:31
Result of converting a square indexed matrix to dense vectors.
Definition Indexing.h:42
std::vector< T > ids
Row and column ids in map iteration order.
Definition Indexing.h:43
std::vector< std::vector< double > > vals
Dense square matrix values.
Definition Indexing.h:44