Project
Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1// Copyright 2019-2023 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
15
16#ifndef RANS_INTERNAL_METRICS_UTILS_H_
17#define RANS_INTERNAL_METRICS_UTILS_H_
18
19#include <cstddef>
20#include <cmath>
21#include <cstdint>
22#include <numeric>
23
28
29namespace o2::rans
30{
31
32template <typename source_T>
34{
35 assert(histogram.getNumSamples() > 0);
36 assert(rescaledHistogram.getNumSamples() > 0);
37
38 using namespace internal;
39 using namespace utils;
40
41 using value_type = typename DenseHistogram<source_T>::value_type;
42
43 const auto histogramView = makeHistogramView(histogram);
44 const auto renormedView = makeHistogramView(rescaledHistogram);
45
46 auto getRescaledFrequency = [&renormedView](source_T sourceSymbol) -> value_type {
47 if (sourceSymbol >= renormedView.getMin() && sourceSymbol <= renormedView.getMax()) {
48 return renormedView[sourceSymbol];
49 } else {
50 return static_cast<value_type>(0);
51 }
52 };
53
54 double_t expectedCodewordLength = 0;
55 value_type trueIncompressibleFrequency = 0;
56
57 assert(countNUsedAlphabetSymbols(histogram) >= countNUsedAlphabetSymbols(rescaledHistogram));
58
59 double_t reciprocalNumSamples = 1.0 / histogram.getNumSamples();
60 double_t reciprocalNumSamplesRescaled = 1.0 / rescaledHistogram.getNumSamples();
61
62 // all "normal symbols"
63 for (value_type sourceSymbol = histogramView.getMin(); sourceSymbol <= histogramView.getMax(); ++sourceSymbol) {
64
65 const value_type frequency = histogramView[sourceSymbol];
66 if (frequency) {
67 const value_type rescaledFrequency = getRescaledFrequency(sourceSymbol);
68 const double_t trueProbability = static_cast<double_t>(frequency) * reciprocalNumSamples;
69
70 if (rescaledFrequency) {
71 const double_t rescaledProbability = static_cast<double_t>(rescaledFrequency) * reciprocalNumSamplesRescaled;
72 expectedCodewordLength -= trueProbability * fastlog2(rescaledProbability);
73 } else {
74 trueIncompressibleFrequency += frequency;
75 }
76 }
77 }
78
79 // incompressibleSymbol:
80 const double_t trueIncompressibleProbability = static_cast<double_t>(trueIncompressibleFrequency) * reciprocalNumSamples;
81 if (trueIncompressibleProbability) {
82 const double_t rescaledProbability = static_cast<double_t>(rescaledHistogram.getIncompressibleSymbolFrequency()) * reciprocalNumSamplesRescaled;
83 expectedCodewordLength -= trueIncompressibleProbability * fastlog2(rescaledProbability);
84 expectedCodewordLength += trueIncompressibleProbability * fastlog2(numBitsForNSymbols(renormedView.size()));
85 }
86
87 return expectedCodewordLength;
88};
89
90} // namespace o2::rans
91
92#endif /* RANS_INTERNAL_METRICS_UTILS_H_ */
Histogram for source symbols used to estimate symbol probabilities for entropy coding.
Non-owning, lightweight structure for histogram manipulation.
Histogram renormed to sum of frequencies being 2^P for use in fast rans coding.
common helper classes and functions
value_type getIncompressibleSymbolFrequency() const noexcept
auto makeHistogramView(container_T &container, std::ptrdiff_t offset) noexcept -> HistogramView< decltype(std::begin(container))>
size_t countNUsedAlphabetSymbols(const AdaptiveHistogram< source_T > &histogram)
double_t computeExpectedCodewordLength(const DenseHistogram< source_T > &histogram, const RenormedDenseHistogram< source_T > &rescaledHistogram)
Definition utils.h:33
Common utility functions.