2 * clueless-kmean is a variant of k-mean which enforces balanced
3 * distribution of classes in every cluster
5 * Copyright (c) 2013 Idiap Research Institute, http://www.idiap.ch/
6 * Written by Francois Fleuret <francois.fleuret@idiap.ch>
8 * This file is part of clueless-kmean.
10 * clueless-kmean is free software: you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * version 3 as published by the Free Software Foundation.
14 * clueless-kmean is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with selector. If not, see <http://www.gnu.org/licenses/>.
36 typedef double scalar_t;
37 // typedef float scalar_t;
39 const int buffer_size = 1024;
44 #define ASSERT(x) if(!(x)) { \
45 std::cerr << "ASSERT FAILED IN " << __FILE__ << ":" << __LINE__ << endl; \
53 T smooth_min(T x, T y) {
55 return 0.5 * (x + y - (x - y)/(1 + 1/z) - (y - x)/(1 + z));
59 void write_var(ostream *os, const T *x) { os->write((char *) x, sizeof(T)); }
62 void read_var(istream *is, T *x) { is->read((char *) x, sizeof(T)); }
65 void grow(int *nb_max, int nb, T** current, int factor) {
68 T *tmp = new T[*nb_max * factor];
69 memcpy(tmp, *current, *nb_max * sizeof(T));
81 inline scalar_t log2(scalar_t x) {
82 return log(x)/log(2.0);
85 inline scalar_t xi(scalar_t x) {
86 if(x <= 0.0) return 0.0;
87 else return - x * log(x)/log(2.0);
90 scalar_t discrete_entropy(int *n, int nb);
92 char *basename(char *name);
94 char *next_word(char *buffer, char *r, int buffer_size);
96 void random_permutation(int *val, int nb);
97 void tag_subset(bool *val, int nb_total, int nb_to_tag);
104 int compare_couple(const void *a, const void *b);