diff options
Diffstat (limited to 'km-init-rand.c')
-rw-r--r-- | km-init-rand.c | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/km-init-rand.c b/km-init-rand.c new file mode 100644 index 0000000..f9d6eb6 --- /dev/null +++ b/km-init-rand.c @@ -0,0 +1,40 @@ +#include <stdbool.h> // bool +#include <string.h> // 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 size_t num_floats, + km_rand_t * const rs +) { + // 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_fill(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); +} |