aboutsummaryrefslogtreecommitdiff
path: root/src/km-init-rand.c
blob: 1f59da852b52eb7d57ecbe3fb8146594f4f4c941 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#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 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);
}