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 
9 #pragma once
10 
34 #include <stdint.h>
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
43 typedef struct {
44  int64_t sum;
45  uint64_t sum_sq;
46  uint32_t count;
47  int32_t mean;
48  int32_t min;
49  int32_t max;
51 
55 #define MATSTAT_STATE_INIT (const matstat_state_t) { \
56  .sum = 0, \
57  .sum_sq = 0, \
58  .count = 0, \
59  .mean = 0, \
60  .min = INT32_MAX, \
61  .max = INT32_MIN, \
62  }
63 
70 
77 void matstat_add(matstat_state_t *state, int32_t value);
78 
86 static inline int32_t matstat_mean(const matstat_state_t *state)
87 {
88  return state->mean;
89 }
90 
98 uint64_t matstat_variance(const matstat_state_t *state);
99 
110 
111 #ifdef __cplusplus
112 }
113 #endif
114 
static int32_t matstat_mean(const matstat_state_t *state)
Return the computed mean value of all samples so far.
Definition: matstat.h:86
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:43
uint32_t count
Number of values added.
Definition: matstat.h:46
int64_t sum
Sum of values added.
Definition: matstat.h:44
int32_t mean
Mean value.
Definition: matstat.h:47
uint64_t sum_sq
Sum of squared differences.
Definition: matstat.h:45
int32_t max
Maximum value seen.
Definition: matstat.h:49
int32_t min
Minimum value seen.
Definition: matstat.h:48