From f557d1f49a2914c6084dd18efc783395228d8ce0 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Tue, 5 Feb 2019 00:22:15 -0500 Subject: mv *.[hc] src/ --- src/km-init-rand.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/km-init-rand.c (limited to 'src/km-init-rand.c') diff --git a/src/km-init-rand.c b/src/km-init-rand.c new file mode 100644 index 0000000..1f59da8 --- /dev/null +++ b/src/km-init-rand.c @@ -0,0 +1,43 @@ +#include // bool +#include // memset() +#include "util.h" +#include "km.h" + +// init a set with num_clusters clusters of shape num_floats by picking +// random cluster centers +bool +km_init_rand( + km_set_t * const cs, + const size_t num_clusters, + const km_set_t * const set, + km_rand_t * const rs +) { + // get number of floats from data set + const size_t num_floats = set->shape.num_floats; + + // init cluster shape + const km_shape_t shape = { + .num_floats = num_floats, + .num_ints = 1, + }; + + // generate random cluster centers + float floats[num_floats * num_clusters]; + if (!km_rand_get_floats(rs, num_floats * num_clusters, floats)) { + // return failure + return false; + } + + // FIXME: should probably be heap-allocated + int ints[num_clusters]; + memset(ints, 0, sizeof(ints)); + + // init cluster set + if (!km_set_init(cs, &shape, num_clusters)) { + // return failure + return false; + } + + // add data, return result + return km_set_push(cs, num_clusters, floats, ints); +} -- cgit v1.2.3