matstat.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2018 Eistec AB
3  * SPDX-License-Identifier: LGPL-2.1-only
4  */
5 
6 #pragma once
7 
31 #include <stdint.h>
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
40 typedef struct {
41  int64_t sum;
42  uint64_t sum_sq;
43  uint32_t count;
44  int32_t mean;
45  int32_t min;
46  int32_t max;
48 
52 #define MATSTAT_STATE_INIT (const matstat_state_t) { \
53  .sum = 0, \
54  .sum_sq = 0, \
55  .count = 0, \
56  .mean = 0, \
57  .min = INT32_MAX, \
58  .max = INT32_MIN, \
59  }
60 
67 
74 void matstat_add(matstat_state_t *state, int32_t value);
75 
83 static inline int32_t matstat_mean(const matstat_state_t *state)
84 {
85  return state->mean;
86 }
87 
95 uint64_t matstat_variance(const matstat_state_t *state);
96 
107 
108 #ifdef __cplusplus
109 }
110 #endif
111 
static int32_t matstat_mean(const matstat_state_t *state)
Return the computed mean value of all samples so far.
Definition: matstat.h:83
uint64_t matstat_variance(const matstat_state_t *state)
Compute the sample variance of all samples so far.
void matstat_add(matstat_state_t *state, int32_t value)
Add a sample to state.
void matstat_clear(matstat_state_t *state)
Reset state.
void matstat_merge(matstat_state_t *dest, const matstat_state_t *src)
Combine two states.
Internal state for computing running statistics.
Definition: matstat.h:40
uint32_t count
Number of values added.
Definition: matstat.h:43
int64_t sum
Sum of values added.
Definition: matstat.h:41
int32_t mean
Mean value.
Definition: matstat.h:44
uint64_t sum_sq
Sum of squared differences.
Definition: matstat.h:42
int32_t max
Maximum value seen.
Definition: matstat.h:46
int32_t min
Minimum value seen.
Definition: matstat.h:45