matstat.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2018 Eistec AB
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
32 #ifndef MATSTAT_H
33 #define MATSTAT_H
34 
35 #include <stdint.h>
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
44 typedef struct {
45  int64_t sum;
46  uint64_t sum_sq;
47  uint32_t count;
48  int32_t mean;
49  int32_t min;
50  int32_t max;
52 
56 #define MATSTAT_STATE_INIT (const matstat_state_t) { \
57  .sum = 0, \
58  .sum_sq = 0, \
59  .count = 0, \
60  .mean = 0, \
61  .min = INT32_MAX, \
62  .max = INT32_MIN, \
63  }
64 
70 void matstat_clear(matstat_state_t *state);
71 
78 void matstat_add(matstat_state_t *state, int32_t value);
79 
87 static inline int32_t matstat_mean(const matstat_state_t *state)
88 {
89  return state->mean;
90 }
91 
99 uint64_t matstat_variance(const matstat_state_t *state);
100 
110 void matstat_merge(matstat_state_t *dest, const matstat_state_t *src);
111 
112 #ifdef __cplusplus
113 }
114 #endif
115 
116 #endif /* MATSTAT_H */
117 
matstat_state_t
Internal state for computing running statistics.
Definition: matstat.h:44
matstat_add
void matstat_add(matstat_state_t *state, int32_t value)
Add a sample to state.
matstat_merge
void matstat_merge(matstat_state_t *dest, const matstat_state_t *src)
Combine two states.
matstat_clear
void matstat_clear(matstat_state_t *state)
Reset state.
matstat_state_t::max
int32_t max
Maximum value seen.
Definition: matstat.h:50
matstat_variance
uint64_t matstat_variance(const matstat_state_t *state)
Compute the sample variance of all samples so far.
matstat_state_t::sum
int64_t sum
Sum of values added.
Definition: matstat.h:45
matstat_state_t::sum_sq
uint64_t sum_sq
Sum of squared differences.
Definition: matstat.h:46
matstat_state_t::mean
int32_t mean
Mean value.
Definition: matstat.h:48
matstat_state_t::min
int32_t min
Minimum value seen.
Definition: matstat.h:49
matstat_state_t::count
uint32_t count
Number of values added.
Definition: matstat.h:47
matstat_mean
static int32_t matstat_mean(const matstat_state_t *state)
Return the computed mean value of all samples so far.
Definition: matstat.h:87