diff options
author | Paul Duncan <pabs@pablotron.org> | 2019-02-05 00:22:15 -0500 |
---|---|---|
committer | Paul Duncan <pabs@pablotron.org> | 2019-02-05 00:22:15 -0500 |
commit | f557d1f49a2914c6084dd18efc783395228d8ce0 (patch) | |
tree | 51111fc899f01956cbab948b87c241478576870d /src/util.h | |
parent | b5065ea43cb13c0b553874305b53963176c70f59 (diff) | |
download | kmeans-f557d1f49a2914c6084dd18efc783395228d8ce0.tar.bz2 kmeans-f557d1f49a2914c6084dd18efc783395228d8ce0.zip |
mv *.[hc] src/
Diffstat (limited to 'src/util.h')
-rw-r--r-- | src/util.h | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/util.h b/src/util.h new file mode 100644 index 0000000..821c3e6 --- /dev/null +++ b/src/util.h @@ -0,0 +1,49 @@ +#ifndef UTIL_H +#define UTIL_H + +#include <stdio.h> // fprintf() +#include <stdlib.h> // exit() + +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define MAX(a, b) ((a) > (b) ? (a) : (b)) +#define UNUSED(a) ((void) (a)) + +#define D(...) do { \ + fprintf(stderr, "DEBUG: %s(): ", __func__); \ + fprintf(stderr, __VA_ARGS__); \ + fputs("\n", stderr); \ +} while (0) + +#define die(...) do { \ + fputs("FATAL: ", stderr); \ + fprintf(stderr, __VA_ARGS__); \ + fputs("\n", stderr); \ + exit(EXIT_FAILURE); \ +} while (0) + +// calculate squared euclidean distance between two points +static inline float +distance_squared( + const size_t num_floats, + const float * const a, + const float * const b +) { + float r = 0.0; + + for (size_t i = 0; i < num_floats; i++) { + r += (b[i] - a[i]) * (b[i] - a[i]); + } + + // return squared distance + return r; +} + +static inline float +km_score( + const float mean_distance, + const size_t num_empty +) { + return 1.0 / (mean_distance + num_empty); +} + +#endif /* UTIL_H */ |