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
44
45
46
47
48
49
50
51
|
#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 initial points from the set
bool
km_init_forgy(
km_set_t * const cs,
const size_t num_clusters,
const km_set_t * const set,
km_rand_t * const rs
) {
const size_t num_floats = set->shape.num_floats,
stride = sizeof(float) * num_floats;
// init cluster shape
const km_shape_t shape = {
.num_floats = num_floats,
.num_ints = 1,
};
// get random row offsets
size_t rows[num_clusters];
if (!km_rand_fill_sizes(rs, num_clusters, rows)) {
// return failure
return false;
}
// generate random cluster centers
float floats[num_floats * num_clusters];
for (size_t i = 0; i < num_clusters; i++) {
const size_t row_num = rows[i] % set->num_rows;
const float * const row_floats = km_set_get_row(set, row_num);
memcpy(floats + i * num_floats, row_floats, stride);
}
// 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);
}
|