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/>.
28 T inline sqdist(int dim, T *x, T *y) {
30 for(int k = 0; k < dim; k++) result += sq(x[k] - y[k]);
35 T inline sum(int dim, T *x) {
37 for(int k = 0; k < dim; k++) result += x[k];
42 T inline dotprod(int dim, T *x, T *y) {
44 for(int k = 0; k < dim; k++) result += x[k] * y[k];
49 void fill_vector(int dim, T *vector, T v) {
50 for(int k = 0; k < dim; k++) {
56 void copy_vector(int dim, T *vector_dst, T *vector_src) {
57 for(int k = 0; k < dim; k++) {
58 vector_dst[k] = vector_src[k];
63 T **allocate_array(int a, int b) {
64 T *whole = new T[a * b];
65 T **array = new T *[a];
66 for(int k = 0; k < a; k++) {
74 void deallocate_array(T **array) {
82 void fill_array(int a, int b, T **array, T v) {
83 for(int k = 0; k < a * b; k++) {
89 T ***allocate_volume(int a, int b, int c) {
90 T *whole = new T[a * b * c];
91 T **column = new T *[a * b];
92 T ***volume = new T **[a];
94 for(int k = 0; k < a; k++) {
96 for(int l = 0; l < b; l++) {
107 void deallocate_volume(T ***volume) {
109 delete[] volume[0][0];
116 void fill_volume(int a, int b, int c, T ***volume, T v) {
117 for(int k = 0; k < a * b * c; k++) {